3030#include " base/logging.h"
3131#include " base/strings/string_util.h"
3232#include " base/strings/utf_string_conversions.h"
33+ #include " chrome/browser/platform_util.h"
3334#include " content/public/browser/browser_context.h"
3435#include " content/public/browser/browser_thread.h"
3536#include " content/public/browser/download_manager.h"
@@ -50,31 +51,47 @@ void ShellDownloadManagerDelegate::ChooseDownloadPath(
5051 if (!item || (item->GetState () != DownloadItem::IN_PROGRESS))
5152 return ;
5253
53- base::FilePath result;
54+ WebContents* web_contents = item->GetWebContents ();
55+ select_file_dialog_ = ui::SelectFileDialog::Create (this , NULL );
56+ ui::SelectFileDialog::FileTypeInfo file_type_info;
57+ // Platform file pickers, notably on Mac and Windows, tend to break
58+ // with double extensions like .tar.gz, so only pass in normal ones.
59+ base::FilePath::StringType extension = suggested_path.FinalExtension ();
60+ if (!extension.empty ()) {
61+ extension.erase (extension.begin ()); // drop the .
62+ file_type_info.extensions .resize (1 );
63+ file_type_info.extensions [0 ].push_back (extension);
64+ }
65+ file_type_info.include_all_files = true ;
66+ file_type_info.support_drive = true ;
67+ gfx::NativeWindow owning_window = web_contents ?
68+ platform_util::GetTopLevel (web_contents->GetNativeView ()) : NULL ;
5469
55- std::wstring file_part = base::FilePath (suggested_path).BaseName ().value ();
56- wchar_t file_name[MAX_PATH];
57- base::wcslcpy (file_name, file_part.c_str (), arraysize (file_name));
58- OPENFILENAME save_as;
59- ZeroMemory (&save_as, sizeof (save_as));
60- save_as.lStructSize = sizeof (OPENFILENAME);
61- save_as.hwndOwner = (HWND)item->GetWebContents ()->GetNativeView ()->
62- GetHost ()->GetAcceleratedWidget ();
63- save_as.lpstrFile = file_name;
64- save_as.nMaxFile = arraysize (file_name);
65-
66- std::wstring directory;
67- if (!suggested_path.empty ())
68- directory = suggested_path.DirName ().value ();
70+ callback_ = callback;
71+ base::FilePath working_path;
72+ select_file_dialog_->SelectFile (ui::SelectFileDialog::SELECT_SAVEAS_FILE,
73+ base::string16 (),
74+ suggested_path,
75+ &file_type_info,
76+ 0 ,
77+ base::FilePath::StringType (),
78+ owning_window,
79+ NULL , working_path);
80+ }
6981
70- save_as.lpstrInitialDir = directory.c_str ();
71- save_as.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLESIZING |
72- OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST;
82+ void ShellDownloadManagerDelegate::OnFileSelected (const base::FilePath& path) {
83+ callback_.Run (path, DownloadItem::TARGET_DISPOSITION_PROMPT,
84+ DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, path);
85+ }
7386
74- if (GetSaveFileName (&save_as))
75- result = base::FilePath (std::wstring (save_as.lpstrFile ));
87+ void ShellDownloadManagerDelegate::FileSelected (const base::FilePath& path,
88+ int index,
89+ void * params) {
90+ OnFileSelected (path);
91+ }
7692
77- callback. Run (result, DownloadItem::TARGET_DISPOSITION_PROMPT,
78- DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, result );
93+ void ShellDownloadManagerDelegate::FileSelectionCanceled ( void * params) {
94+ OnFileSelected ( base::FilePath () );
7995}
96+
8097} // namespace content
0 commit comments