1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/download/download_file_picker.h"
7 #include "base/metrics/histogram.h"
8 #include "chrome/browser/download/download_prefs.h"
9 #include "chrome/browser/platform_util.h"
10 #include "chrome/browser/ui/chrome_select_file_policy.h"
11 #include "content/public/browser/browser_context.h"
12 #include "content/public/browser/download_item.h"
13 #include "content/public/browser/download_manager.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/browser/web_contents_view.h"
16 #include "grit/generated_resources.h"
17 #include "ui/base/l10n/l10n_util.h"
19 using content::DownloadItem
;
20 using content::DownloadManager
;
21 using content::WebContents
;
25 enum FilePickerResult
{
27 FILE_PICKER_DIFFERENT_DIR
,
28 FILE_PICKER_DIFFERENT_NAME
,
33 // Record how the File Picker was used during a download. This UMA is only
34 // recorded for profiles that do not always prompt for save locations on
36 void RecordFilePickerResult(const base::FilePath
& suggested_path
,
37 const base::FilePath
& actual_path
) {
38 FilePickerResult result
;
39 if (suggested_path
== actual_path
)
40 result
= FILE_PICKER_SAME
;
41 else if (actual_path
.empty())
42 result
= FILE_PICKER_CANCEL
;
43 else if (suggested_path
.DirName() != actual_path
.DirName())
44 result
= FILE_PICKER_DIFFERENT_DIR
;
46 result
= FILE_PICKER_DIFFERENT_NAME
;
48 UMA_HISTOGRAM_ENUMERATION("Download.FilePickerResult",
55 DownloadFilePicker::DownloadFilePicker(
57 const base::FilePath
& suggested_path
,
58 const FileSelectedCallback
& callback
)
59 : suggested_path_(suggested_path
),
60 file_selected_callback_(callback
),
61 should_record_file_picker_result_(false) {
62 const DownloadPrefs
* prefs
=
63 DownloadPrefs::FromBrowserContext(item
->GetBrowserContext());
65 // Only record UMA if we aren't prompting the user for all downloads.
66 should_record_file_picker_result_
= !prefs
->PromptForDownload();
68 WebContents
* web_contents
= item
->GetWebContents();
69 select_file_dialog_
= ui::SelectFileDialog::Create(
70 this, new ChromeSelectFilePolicy(web_contents
));
71 ui::SelectFileDialog::FileTypeInfo file_type_info
;
72 // Platform file pickers, notably on Mac and Windows, tend to break
73 // with double extensions like .tar.gz, so only pass in normal ones.
74 base::FilePath::StringType extension
= suggested_path_
.FinalExtension();
75 if (!extension
.empty()) {
76 extension
.erase(extension
.begin()); // drop the .
77 file_type_info
.extensions
.resize(1);
78 file_type_info
.extensions
[0].push_back(extension
);
80 file_type_info
.include_all_files
= true;
81 file_type_info
.support_drive
= true;
82 gfx::NativeWindow owning_window
= web_contents
?
83 platform_util::GetTopLevel(web_contents
->GetView()->GetNativeView()) :
86 select_file_dialog_
->SelectFile(ui::SelectFileDialog::SELECT_SAVEAS_FILE
,
91 base::FilePath::StringType(),
96 DownloadFilePicker::~DownloadFilePicker() {
99 void DownloadFilePicker::OnFileSelected(const base::FilePath
& path
) {
100 if (should_record_file_picker_result_
)
101 RecordFilePickerResult(suggested_path_
, path
);
102 file_selected_callback_
.Run(path
);
106 void DownloadFilePicker::FileSelected(const base::FilePath
& path
,
109 OnFileSelected(path
);
113 void DownloadFilePicker::FileSelectionCanceled(void* params
) {
114 OnFileSelected(base::FilePath());
119 void DownloadFilePicker::ShowFilePicker(DownloadItem
* item
,
120 const base::FilePath
& suggested_path
,
121 const FileSelectedCallback
& callback
) {
122 new DownloadFilePicker(item
, suggested_path
, callback
);
123 // DownloadFilePicker deletes itself.