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 "grit/generated_resources.h"
16 #include "ui/base/l10n/l10n_util.h"
18 using content::DownloadItem
;
19 using content::DownloadManager
;
20 using content::WebContents
;
24 enum FilePickerResult
{
26 FILE_PICKER_DIFFERENT_DIR
,
27 FILE_PICKER_DIFFERENT_NAME
,
32 // Record how the File Picker was used during a download. This UMA is only
33 // recorded for profiles that do not always prompt for save locations on
35 void RecordFilePickerResult(const base::FilePath
& suggested_path
,
36 const base::FilePath
& actual_path
) {
37 FilePickerResult result
;
38 if (suggested_path
== actual_path
)
39 result
= FILE_PICKER_SAME
;
40 else if (actual_path
.empty())
41 result
= FILE_PICKER_CANCEL
;
42 else if (suggested_path
.DirName() != actual_path
.DirName())
43 result
= FILE_PICKER_DIFFERENT_DIR
;
45 result
= FILE_PICKER_DIFFERENT_NAME
;
47 UMA_HISTOGRAM_ENUMERATION("Download.FilePickerResult",
54 DownloadFilePicker::DownloadFilePicker(
56 const base::FilePath
& suggested_path
,
57 const FileSelectedCallback
& callback
)
58 : suggested_path_(suggested_path
),
59 file_selected_callback_(callback
),
60 should_record_file_picker_result_(false) {
61 const DownloadPrefs
* prefs
=
62 DownloadPrefs::FromBrowserContext(item
->GetBrowserContext());
64 // Only record UMA if we aren't prompting the user for all downloads.
65 should_record_file_picker_result_
= !prefs
->PromptForDownload();
67 WebContents
* web_contents
= item
->GetWebContents();
68 select_file_dialog_
= ui::SelectFileDialog::Create(
69 this, new ChromeSelectFilePolicy(web_contents
));
70 ui::SelectFileDialog::FileTypeInfo file_type_info
;
71 // Platform file pickers, notably on Mac and Windows, tend to break
72 // with double extensions like .tar.gz, so only pass in normal ones.
73 base::FilePath::StringType extension
= suggested_path_
.FinalExtension();
74 if (!extension
.empty()) {
75 extension
.erase(extension
.begin()); // drop the .
76 file_type_info
.extensions
.resize(1);
77 file_type_info
.extensions
[0].push_back(extension
);
79 file_type_info
.include_all_files
= true;
80 file_type_info
.support_drive
= true;
81 gfx::NativeWindow owning_window
= web_contents
?
82 platform_util::GetTopLevel(web_contents
->GetNativeView()) : NULL
;
84 select_file_dialog_
->SelectFile(ui::SelectFileDialog::SELECT_SAVEAS_FILE
,
89 base::FilePath::StringType(),
94 DownloadFilePicker::~DownloadFilePicker() {
97 void DownloadFilePicker::OnFileSelected(const base::FilePath
& path
) {
98 if (should_record_file_picker_result_
)
99 RecordFilePickerResult(suggested_path_
, path
);
100 file_selected_callback_
.Run(path
);
104 void DownloadFilePicker::FileSelected(const base::FilePath
& path
,
107 OnFileSelected(path
);
111 void DownloadFilePicker::FileSelectionCanceled(void* params
) {
112 OnFileSelected(base::FilePath());
117 void DownloadFilePicker::ShowFilePicker(DownloadItem
* item
,
118 const base::FilePath
& suggested_path
,
119 const FileSelectedCallback
& callback
) {
120 new DownloadFilePicker(item
, suggested_path
, callback
);
121 // DownloadFilePicker deletes itself.