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

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

Issue 12662032: Merge SavePackageFilePicker{,ChromeOS} (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: @r197032 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/save_package_file_picker.h" 5 #include "chrome/browser/download/save_package_file_picker.h"
6 6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
7 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/i18n/file_util_icu.h"
8 #include "base/metrics/histogram.h" 11 #include "base/metrics/histogram.h"
9 #include "base/prefs/pref_member.h" 12 #include "base/prefs/pref_member.h"
10 #include "base/prefs/pref_service.h" 13 #include "base/prefs/pref_service.h"
14 #include "base/threading/sequenced_worker_pool.h"
11 #include "base/utf_string_conversions.h" 15 #include "base/utf_string_conversions.h"
12 #include "chrome/browser/download/chrome_download_manager_delegate.h" 16 #include "chrome/browser/download/chrome_download_manager_delegate.h"
13 #include "chrome/browser/download/download_prefs.h" 17 #include "chrome/browser/download/download_prefs.h"
14 #include "chrome/browser/platform_util.h" 18 #include "chrome/browser/platform_util.h"
15 #include "chrome/browser/profiles/profile.h" 19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/browser/ui/chrome_select_file_policy.h" 21 #include "chrome/browser/ui/chrome_select_file_policy.h"
17 #include "chrome/common/chrome_switches.h" 22 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/pref_names.h" 23 #include "chrome/common/pref_names.h"
24 #include "content/public/browser/download_item.h"
19 #include "content/public/browser/download_manager.h" 25 #include "content/public/browser/download_manager.h"
20 #include "content/public/browser/render_process_host.h" 26 #include "content/public/browser/render_process_host.h"
21 #include "content/public/browser/save_page_type.h" 27 #include "content/public/browser/save_page_type.h"
22 #include "content/public/browser/web_contents.h" 28 #include "content/public/browser/web_contents.h"
23 #include "content/public/browser/web_contents_view.h" 29 #include "content/public/browser/web_contents_view.h"
24 #include "grit/generated_resources.h" 30 #include "grit/generated_resources.h"
25 #include "ui/base/l10n/l10n_util.h" 31 #include "ui/base/l10n/l10n_util.h"
32 #include "ui/shell_dialogs/selected_file_info.h"
33
34 #if defined(OS_CHROMEOS)
35 #include "chrome/browser/chromeos/drive/download_handler.h"
36 #include "chrome/browser/chromeos/drive/file_system_util.h"
37 #endif
26 38
27 using content::RenderProcessHost; 39 using content::RenderProcessHost;
28 using content::SavePageType; 40 using content::SavePageType;
29 using content::WebContents; 41 using content::WebContents;
30 42
31 namespace { 43 namespace {
32 44
33 // If false, we don't prompt the user as to where to save the file. This 45 // If false, we don't prompt the user as to where to save the file. This
34 // exists only for testing. 46 // exists only for testing.
35 bool g_should_prompt_for_filename = true; 47 bool g_should_prompt_for_filename = true;
(...skipping 17 matching lines...) Expand all
53 // Indexes used for specifying which element in the extensions dropdown 65 // Indexes used for specifying which element in the extensions dropdown
54 // the user chooses when picking a save type. 66 // the user chooses when picking a save type.
55 const int kSelectFileHtmlOnlyIndex = 1; 67 const int kSelectFileHtmlOnlyIndex = 1;
56 const int kSelectFileCompleteIndex = 2; 68 const int kSelectFileCompleteIndex = 2;
57 69
58 // Used for mapping between the IDS_ string identifiers and the indexes above. 70 // Used for mapping between the IDS_ string identifiers and the indexes above.
59 const int kIndexToIDS[] = { 71 const int kIndexToIDS[] = {
60 0, IDS_SAVE_PAGE_DESC_HTML_ONLY, IDS_SAVE_PAGE_DESC_COMPLETE, 72 0, IDS_SAVE_PAGE_DESC_HTML_ONLY, IDS_SAVE_PAGE_DESC_COMPLETE,
61 }; 73 };
62 74
63 void OnSavePackageDownloadCreated( 75 void OnSavePackageDownloadCreated(content::DownloadItem* download) {
64 content::DownloadItem* download) {
65 ChromeDownloadManagerDelegate::DisableSafeBrowsing(download); 76 ChromeDownloadManagerDelegate::DisableSafeBrowsing(download);
Randy Smith (Not in Mondays) 2013/05/01 19:18:43 If I'm reading the code correctly, this hasn't his
benjhayden 2013/05/06 20:05:19 Code unification. It will continue to do nothing i
66 } 77 }
67 78
79 #if defined(OS_CHROMEOS)
80 void OnSavePackageDownloadCreatedChromeOS(
81 Profile* profile,
82 const base::FilePath& drive_path,
83 content::DownloadItem* download) {
84 drive::DownloadHandler::GetForProfile(profile)->SetDownloadParams(
85 drive_path, download);
86 OnSavePackageDownloadCreated(download);
87 }
88
89 // Trampoline callback between SubstituteDriveDownloadPath() and |callback|.
90 void ContinueSettingUpDriveDownload(
91 const content::SavePackagePathPickedCallback& callback,
92 content::SavePageType save_type,
93 Profile* profile,
94 const base::FilePath& drive_path,
95 const base::FilePath& drive_tmp_download_path) {
96 if (drive_tmp_download_path.empty()) // Substitution failed.
97 return;
98 callback.Run(drive_tmp_download_path, save_type, base::Bind(
99 &OnSavePackageDownloadCreatedChromeOS, profile, drive_path));
100 }
101 #endif
102
68 } // anonymous namespace 103 } // anonymous namespace
69 104
70 bool SavePackageFilePicker::ShouldSaveAsMHTML() const { 105 bool SavePackageFilePicker::ShouldSaveAsMHTML() const {
71 return can_save_as_complete_ && 106 #if !defined(OS_CHROMEOS)
72 CommandLine::ForCurrentProcess()->HasSwitch( 107 if (!CommandLine::ForCurrentProcess()->HasSwitch(
73 switches::kSavePageAsMHTML); 108 switches::kSavePageAsMHTML))
109 return false;
110 #endif
111 return can_save_as_complete_;
74 } 112 }
75 113
76 SavePackageFilePicker::SavePackageFilePicker( 114 SavePackageFilePicker::SavePackageFilePicker(
77 content::WebContents* web_contents, 115 content::WebContents* web_contents,
78 const base::FilePath& suggested_path_const, 116 const base::FilePath& suggested_path_const,
79 const base::FilePath::StringType& default_extension_const, 117 const base::FilePath::StringType& default_extension_const,
80 bool can_save_as_complete, 118 bool can_save_as_complete,
81 DownloadPrefs* download_prefs, 119 DownloadPrefs* download_prefs,
82 const content::SavePackagePathPickedCallback& callback) 120 const content::SavePackagePathPickedCallback& callback)
83 : render_process_id_(web_contents->GetRenderProcessHost()->GetID()), 121 : render_process_id_(web_contents->GetRenderProcessHost()->GetID()),
84 can_save_as_complete_(can_save_as_complete), 122 can_save_as_complete_(can_save_as_complete),
123 download_prefs_(download_prefs),
85 callback_(callback) { 124 callback_(callback) {
86 base::FilePath suggested_path = suggested_path_const; 125 base::FilePath suggested_path = suggested_path_const;
87 base::FilePath::StringType default_extension = default_extension_const; 126 base::FilePath::StringType default_extension = default_extension_const;
88 int file_type_index = SavePackageTypeToIndex( 127 int file_type_index = 0;
89 static_cast<SavePageType>(download_prefs->save_file_type())); 128 ui::SelectFileDialog::FileTypeInfo file_type_info;
129
130 #if defined(OS_CHROMEOS)
131 file_type_info.support_drive = true;
132 #else
133 file_type_index = SavePackageTypeToIndex(
134 static_cast<SavePageType>(download_prefs_->save_file_type()));
90 DCHECK_NE(-1, file_type_index); 135 DCHECK_NE(-1, file_type_index);
91 136 #endif
92 ui::SelectFileDialog::FileTypeInfo file_type_info;
93 137
94 // TODO(benjhayden): Merge the first branch with the second when all of the 138 // TODO(benjhayden): Merge the first branch with the second when all of the
95 // platform-specific file selection dialog implementations fully support 139 // platform-specific file selection dialog implementations fully support
96 // switching save-as file formats, and remove the flag/switch. 140 // switching save-as file formats, and remove the flag/switch.
97 if (ShouldSaveAsMHTML()) { 141 if (ShouldSaveAsMHTML()) {
98 default_extension = FILE_PATH_LITERAL("mhtml"); 142 default_extension = FILE_PATH_LITERAL("mhtml");
99 suggested_path = suggested_path.ReplaceExtension(default_extension); 143 suggested_path = suggested_path.ReplaceExtension(default_extension);
100 } else if (can_save_as_complete) { 144 } else if (can_save_as_complete_) {
145 // NOTE: this branch will never run on chromeos because ShouldSaveAsHTML()
146 // == can_save_as_complete_ on chromeos.
101 bool add_extra_extension = false; 147 bool add_extra_extension = false;
102 base::FilePath::StringType extra_extension; 148 base::FilePath::StringType extra_extension;
103 if (!suggested_path.Extension().empty() && 149 if (!suggested_path.Extension().empty() &&
104 suggested_path.Extension().compare(FILE_PATH_LITERAL("htm")) && 150 suggested_path.Extension().compare(FILE_PATH_LITERAL("htm")) &&
105 suggested_path.Extension().compare(FILE_PATH_LITERAL("html"))) { 151 suggested_path.Extension().compare(FILE_PATH_LITERAL("html"))) {
106 add_extra_extension = true; 152 add_extra_extension = true;
107 extra_extension = suggested_path.Extension().substr(1); 153 extra_extension = suggested_path.Extension().substr(1);
108 } 154 }
109 155
110 static const size_t kNumberExtensions = arraysize(kIndexToIDS) - 1; 156 static const size_t kNumberExtensions = arraysize(kIndexToIDS) - 1;
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 } 222 }
177 } 223 }
178 224
179 SavePackageFilePicker::~SavePackageFilePicker() { 225 SavePackageFilePicker::~SavePackageFilePicker() {
180 } 226 }
181 227
182 void SavePackageFilePicker::SetShouldPromptUser(bool should_prompt) { 228 void SavePackageFilePicker::SetShouldPromptUser(bool should_prompt) {
183 g_should_prompt_for_filename = should_prompt; 229 g_should_prompt_for_filename = should_prompt;
184 } 230 }
185 231
186 void SavePackageFilePicker::FileSelected(const base::FilePath& path, 232 void SavePackageFilePicker::FileSelected(
187 int index, 233 const base::FilePath& path_const, int index, void* unused_params) {
188 void* unused_params) {
189 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id_); 234 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id_);
190 if (process) { 235 if (!process) {
191 SavePageType save_type = content::SAVE_PAGE_TYPE_UNKNOWN; 236 delete this;
Randy Smith (Not in Mondays) 2013/05/01 19:18:43 Suggestion: I'm a bit uncomfortable with the numbe
benjhayden 2013/05/06 20:05:19 Done.
192 PrefService* prefs = Profile::FromBrowserContext( 237 return;
193 process->GetBrowserContext())->GetPrefs(); 238 }
194 if (ShouldSaveAsMHTML()) { 239 Profile* profile = Profile::FromBrowserContext(process->GetBrowserContext());
195 save_type = content::SAVE_PAGE_TYPE_AS_MHTML; 240 SavePageType save_type = content::SAVE_PAGE_TYPE_UNKNOWN;
196 } else {
197 // The option index is not zero-based.
198 DCHECK(index >= kSelectFileHtmlOnlyIndex &&
199 index <= kSelectFileCompleteIndex);
200 save_type = kIndexToSaveType[index];
201 if (select_file_dialog_ &&
202 select_file_dialog_->HasMultipleFileTypeChoices())
203 prefs->SetInteger(prefs::kSaveFileType, save_type);
204 }
205 241
206 UMA_HISTOGRAM_ENUMERATION("Download.SavePageType", 242 if (ShouldSaveAsMHTML()) {
207 save_type, 243 save_type = content::SAVE_PAGE_TYPE_AS_MHTML;
208 content::SAVE_PAGE_TYPE_MAX); 244 } else {
209 245 #if defined(OS_CHROMEOS)
210 StringPrefMember save_file_path; 246 save_type = content::SAVE_PAGE_TYPE_AS_ONLY_HTML;
211 save_file_path.Init(prefs::kSaveFileDefaultDirectory, prefs); 247 #else
212 #if defined(OS_POSIX) 248 // The option index is not zero-based.
213 std::string path_string = path.DirName().value(); 249 DCHECK(index >= kSelectFileHtmlOnlyIndex &&
214 #elif defined(OS_WIN) 250 index <= kSelectFileCompleteIndex);
215 std::string path_string = WideToUTF8(path.DirName().value()); 251 save_type = kIndexToSaveType[index];
252 if (select_file_dialog_ &&
253 select_file_dialog_->HasMultipleFileTypeChoices())
254 download_prefs_->SetSaveFileType(save_type);
216 #endif 255 #endif
217 // If user change the default saving directory, we will remember it just
218 // like IE and FireFox.
219 if (!process->GetBrowserContext()->IsOffTheRecord() &&
220 save_file_path.GetValue() != path_string)
221 save_file_path.SetValue(path_string);
222
223 callback_.Run(path, save_type, base::Bind(&OnSavePackageDownloadCreated));
224 } 256 }
225 257
258 UMA_HISTOGRAM_ENUMERATION("Download.SavePageType",
259 save_type,
260 content::SAVE_PAGE_TYPE_MAX);
261
262 base::FilePath path(path_const);
263 file_util::NormalizeFileNameEncoding(&path);
264
265 if (!profile->IsOffTheRecord())
266 download_prefs_->SetSaveFilePath(path.DirName());
Randy Smith (Not in Mondays) 2013/05/01 19:18:43 This doesn't look right to me, though I'm not sure
benjhayden 2013/05/06 20:05:19 This might be a can of worms. Up until now, Downlo
267
268 #if defined(OS_CHROMEOS)
269 if (drive::util::IsUnderDriveMountPoint(path)) {
270 // Here's a map to the callback chain:
271 // SubstituteDriveDownloadPath ->
272 // ContinueSettingUpDriveDownload ->
273 // callback_ = SavePackage::OnPathPicked ->
274 // download_created_callback = OnSavePackageDownloadCreatedChromeOS
275 drive::DownloadHandler* drive_download_handler =
276 drive::DownloadHandler::GetForProfile(profile);
277 drive_download_handler->SubstituteDriveDownloadPath(path, NULL, base::Bind(
278 &ContinueSettingUpDriveDownload, callback_, save_type, profile, path));
279 delete this;
280 return;
281 }
282 #endif
283
284 callback_.Run(path, save_type, base::Bind(&OnSavePackageDownloadCreated));
226 delete this; 285 delete this;
227 } 286 }
228 287
229 void SavePackageFilePicker::FileSelectionCanceled(void* unused_params) { 288 void SavePackageFilePicker::FileSelectionCanceled(void* unused_params) {
230 delete this; 289 delete this;
231 } 290 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698