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 base::FilePath::StringType extension = suggested_path_.Extension();
73 if (!extension.empty()) {
74 extension.erase(extension.begin()); // drop the .
75 file_type_info.extensions.resize(1);
76 file_type_info.extensions[0].push_back(extension);
78 file_type_info.include_all_files = true;
79 file_type_info.support_drive = true;
80 gfx::NativeWindow owning_window = web_contents ?
81 platform_util::GetTopLevel(web_contents->GetView()->GetNativeView()) :
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.