Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Side by Side Diff: chrome/browser/download/download_file_picker.cc

Issue 14773004: Move download filename determination into a separate class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/download/download_file_picker.h" 5 #include "chrome/browser/download/download_file_picker.h"
6 6
7 #include "base/metrics/histogram.h" 7 #include "base/metrics/histogram.h"
8 #include "chrome/browser/download/download_prefs.h" 8 #include "chrome/browser/download/download_prefs.h"
9 #include "chrome/browser/platform_util.h" 9 #include "chrome/browser/platform_util.h"
10 #include "chrome/browser/ui/chrome_select_file_policy.h" 10 #include "chrome/browser/ui/chrome_select_file_policy.h"
11 #include "content/public/browser/browser_context.h"
11 #include "content/public/browser/download_item.h" 12 #include "content/public/browser/download_item.h"
12 #include "content/public/browser/download_manager.h" 13 #include "content/public/browser/download_manager.h"
13 #include "content/public/browser/web_contents.h" 14 #include "content/public/browser/web_contents.h"
14 #include "content/public/browser/web_contents_view.h" 15 #include "content/public/browser/web_contents_view.h"
15 #include "grit/generated_resources.h" 16 #include "grit/generated_resources.h"
16 #include "ui/base/l10n/l10n_util.h" 17 #include "ui/base/l10n/l10n_util.h"
17 18
18 using content::DownloadItem; 19 using content::DownloadItem;
19 using content::DownloadManager; 20 using content::DownloadManager;
20 using content::WebContents; 21 using content::WebContents;
21 22
22 namespace { 23 namespace {
23 24
24 enum FilePickerResult { 25 enum FilePickerResult {
25 FILE_PICKER_SAME, 26 FILE_PICKER_SAME,
26 FILE_PICKER_DIFFERENT_DIR, 27 FILE_PICKER_DIFFERENT_DIR,
27 FILE_PICKER_DIFFERENT_NAME, 28 FILE_PICKER_DIFFERENT_NAME,
28 FILE_PICKER_CANCEL, 29 FILE_PICKER_CANCEL,
29 FILE_PICKER_MAX, 30 FILE_PICKER_MAX,
30 }; 31 };
31 32
32 // Record how the File Chooser was used during a download. Only record this 33 // Record how the File Picker was used during a download. This UMA is only
33 // for profiles that do not always prompt for save locations on downloads. 34 // recorded for profiles that do not always prompt for save locations on
34 void RecordFilePickerResult(DownloadManager* manager, 35 // downloads.
35 FilePickerResult result) { 36 void RecordFilePickerResult(const base::FilePath& suggested_path,
36 if (!manager) 37 const base::FilePath& actual_path) {
37 return; 38 FilePickerResult result;
38 const DownloadPrefs* prefs = DownloadPrefs::FromDownloadManager(manager); 39 if (suggested_path == actual_path)
39 if (!prefs) 40 result = FILE_PICKER_SAME;
40 return; 41 else if (actual_path.empty())
41 if (prefs->PromptForDownload()) 42 result = FILE_PICKER_CANCEL;
42 return; 43 else if (suggested_path.DirName() != actual_path.DirName())
44 result = FILE_PICKER_DIFFERENT_DIR;
45 else
46 result = FILE_PICKER_DIFFERENT_NAME;
47
43 UMA_HISTOGRAM_ENUMERATION("Download.FilePickerResult", 48 UMA_HISTOGRAM_ENUMERATION("Download.FilePickerResult",
44 result, 49 result,
45 FILE_PICKER_MAX); 50 FILE_PICKER_MAX);
46 } 51 }
47 52
48 FilePickerResult ComparePaths(const base::FilePath& suggested_path,
49 const base::FilePath& actual_path) {
50 if (suggested_path == actual_path)
51 return FILE_PICKER_SAME;
52 if (suggested_path.DirName() != actual_path.DirName())
53 return FILE_PICKER_DIFFERENT_DIR;
54 return FILE_PICKER_DIFFERENT_NAME;
55 }
56
57 } // namespace 53 } // namespace
58 54
59 DownloadFilePicker::DownloadFilePicker() : download_id_(0) { 55 DownloadFilePicker::DownloadFilePicker(
60 }
61
62 void DownloadFilePicker::Init(
63 DownloadManager* download_manager,
64 DownloadItem* item, 56 DownloadItem* item,
65 const base::FilePath& suggested_path, 57 const base::FilePath& suggested_path,
66 const ChromeDownloadManagerDelegate::FileSelectedCallback& callback) { 58 const FileSelectedCallback& callback)
67 download_manager_ = download_manager; 59 : suggested_path_(suggested_path),
68 download_id_ = item->GetId(); 60 file_selected_callback_(callback),
69 file_selected_callback_ = callback; 61 should_record_file_picker_result_(false) {
70 InitSuggestedPath(item, suggested_path); 62 const DownloadPrefs* prefs =
63 DownloadPrefs::FromBrowserContext(item->GetBrowserContext());
64 DCHECK(prefs);
65 // Only record UMA if we aren't prompting the user for all downloads.
66 should_record_file_picker_result_ = !prefs->PromptForDownload();
71 67
72 DCHECK(download_manager_);
73 WebContents* web_contents = item->GetWebContents(); 68 WebContents* web_contents = item->GetWebContents();
74 select_file_dialog_ = ui::SelectFileDialog::Create( 69 select_file_dialog_ = ui::SelectFileDialog::Create(
75 this, new ChromeSelectFilePolicy(web_contents)); 70 this, new ChromeSelectFilePolicy(web_contents));
76 ui::SelectFileDialog::FileTypeInfo file_type_info; 71 ui::SelectFileDialog::FileTypeInfo file_type_info;
77 base::FilePath::StringType extension = suggested_path_.Extension(); 72 base::FilePath::StringType extension = suggested_path_.Extension();
78 if (!extension.empty()) { 73 if (!extension.empty()) {
79 extension.erase(extension.begin()); // drop the . 74 extension.erase(extension.begin()); // drop the .
80 file_type_info.extensions.resize(1); 75 file_type_info.extensions.resize(1);
81 file_type_info.extensions[0].push_back(extension); 76 file_type_info.extensions[0].push_back(extension);
82 } 77 }
83 file_type_info.include_all_files = true; 78 file_type_info.include_all_files = true;
84 file_type_info.support_drive = true; 79 file_type_info.support_drive = true;
85 gfx::NativeWindow owning_window = web_contents ? 80 gfx::NativeWindow owning_window = web_contents ?
86 platform_util::GetTopLevel(web_contents->GetView()->GetNativeView()) : 81 platform_util::GetTopLevel(web_contents->GetView()->GetNativeView()) :
87 NULL; 82 NULL;
88 83
89 select_file_dialog_->SelectFile(ui::SelectFileDialog::SELECT_SAVEAS_FILE, 84 select_file_dialog_->SelectFile(ui::SelectFileDialog::SELECT_SAVEAS_FILE,
90 string16(), 85 string16(),
91 suggested_path_, 86 suggested_path_,
92 &file_type_info, 87 &file_type_info,
93 0, 88 0,
94 base::FilePath::StringType(), 89 base::FilePath::StringType(),
95 owning_window, 90 owning_window,
96 NULL); 91 NULL);
97 } 92 }
98 93
99 DownloadFilePicker::~DownloadFilePicker() { 94 DownloadFilePicker::~DownloadFilePicker() {
100 } 95 }
101 96
102 void DownloadFilePicker::InitSuggestedPath(
103 DownloadItem* item,
104 const base::FilePath& suggested_path) {
105 set_suggested_path(suggested_path);
106 }
107
108 void DownloadFilePicker::OnFileSelected(const base::FilePath& path) { 97 void DownloadFilePicker::OnFileSelected(const base::FilePath& path) {
98 if (should_record_file_picker_result_)
99 RecordFilePickerResult(suggested_path_, path);
109 file_selected_callback_.Run(path); 100 file_selected_callback_.Run(path);
110 delete this; 101 delete this;
111 } 102 }
112 103
113 void DownloadFilePicker::RecordFileSelected(const base::FilePath& path) {
114 FilePickerResult result = ComparePaths(suggested_path_, path);
115 RecordFilePickerResult(download_manager_, result);
116 }
117
118 void DownloadFilePicker::FileSelected(const base::FilePath& path, 104 void DownloadFilePicker::FileSelected(const base::FilePath& path,
119 int index, 105 int index,
120 void* params) { 106 void* params) {
121 RecordFileSelected(path);
122 OnFileSelected(path); 107 OnFileSelected(path);
123 // Deletes |this| 108 // Deletes |this|
124 } 109 }
125 110
126 void DownloadFilePicker::FileSelectionCanceled(void* params) { 111 void DownloadFilePicker::FileSelectionCanceled(void* params) {
127 RecordFilePickerResult(download_manager_, FILE_PICKER_CANCEL);
128 OnFileSelected(base::FilePath()); 112 OnFileSelected(base::FilePath());
129 // Deletes |this| 113 // Deletes |this|
130 } 114 }
115
116 // static
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.
122 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698