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

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

Issue 12662032: Merge SavePackageFilePicker{,ChromeOS} (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: @r198452 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
(Empty)
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.
4
5 #include "chrome/browser/download/save_package_file_picker_chromeos.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/i18n/file_util_icu.h"
10 #include "base/threading/sequenced_worker_pool.h"
11 #include "chrome/browser/chromeos/drive/download_handler.h"
12 #include "chrome/browser/chromeos/drive/file_system_util.h"
13 #include "chrome/browser/platform_util.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/browser/ui/chrome_select_file_policy.h"
16 #include "content/public/browser/download_item.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/browser/web_contents_view.h"
19 #include "ui/shell_dialogs/selected_file_info.h"
20
21 namespace {
22
23 // If false, we don't prompt the user as to where to save the file. This
24 // exists only for testing.
25 bool g_should_prompt_for_filename = true;
26
27 // Trampoline callback between SubstituteDriveDownloadPath() and |callback|.
28 void ContinueSettingUpDriveDownload(
29 const content::SavePackagePathPickedCallback& callback,
30 bool is_html,
31 Profile* profile,
32 const base::FilePath& drive_path,
33 const base::FilePath& drive_tmp_download_path) {
34 if (drive_tmp_download_path.empty()) // Substitution failed.
35 return;
36
37 callback.Run(
38 drive_tmp_download_path,
39 (is_html ?
40 content::SAVE_PAGE_TYPE_AS_MHTML :
41 content::SAVE_PAGE_TYPE_AS_ONLY_HTML),
42 base::Bind(&drive::DownloadHandler::SetDownloadParams,
43 base::Unretained(
44 drive::DownloadHandler::GetForProfile(profile)),
45 drive_path));
46 }
47
48 } // namespace
49
50 SavePackageFilePickerChromeOS::SavePackageFilePickerChromeOS(
51 content::WebContents* web_contents,
52 const base::FilePath& suggested_path,
53 bool is_html,
54 const content::SavePackagePathPickedCallback& callback)
55 : content::WebContentsObserver(web_contents),
56 callback_(callback),
57 is_html_(is_html) {
58 base::FilePath suggested_path_copy(is_html_ ?
59 suggested_path.ReplaceExtension("mhtml") :
60 suggested_path);
61 if (g_should_prompt_for_filename) {
62 select_file_dialog_ = ui::SelectFileDialog::Create(
63 this, new ChromeSelectFilePolicy(web_contents));
64 ui::SelectFileDialog::FileTypeInfo file_types;
65 file_types.support_drive = true;
66 select_file_dialog_->SelectFile(
67 ui::SelectFileDialog::SELECT_SAVEAS_FILE,
68 string16(),
69 suggested_path_copy,
70 &file_types,
71 0,
72 suggested_path_copy.Extension(),
73 platform_util::GetTopLevel(web_contents->GetView()->GetNativeView()),
74 NULL);
75 } else {
76 FileSelected(suggested_path_copy, 0, NULL);
77 }
78 }
79
80 void SavePackageFilePickerChromeOS::SetShouldPromptUser(bool should_prompt) {
81 g_should_prompt_for_filename = should_prompt;
82 }
83
84 SavePackageFilePickerChromeOS::~SavePackageFilePickerChromeOS() {
85 }
86
87 void SavePackageFilePickerChromeOS::FileSelected(
88 const base::FilePath& selected_path,
89 int unused_index,
90 void* unused_params) {
91 FileSelectedWithExtraInfo(
92 ui::SelectedFileInfo(selected_path, selected_path),
93 unused_index,
94 unused_params);
95 }
96
97 void SavePackageFilePickerChromeOS::FileSelectedWithExtraInfo(
98 const ui::SelectedFileInfo& selected_file_info,
99 int unused_index,
100 void* unused_params) {
101 if (!web_contents()) {
102 delete this;
103 return;
104 }
105 base::FilePath selected_path = selected_file_info.file_path;
106 file_util::NormalizeFileNameEncoding(&selected_path);
107 Profile* profile = Profile::FromBrowserContext(
108 web_contents()->GetBrowserContext());
109 DCHECK(profile);
110
111 if (drive::util::IsUnderDriveMountPoint(selected_path)) {
112 // Here's a map to the callback chain:
113 // SubstituteDriveDownloadPath ->
114 // ContinueSettingUpDriveDownload ->
115 // callback_ = SavePackage::OnPathPicked ->
116 // download_created_callback = OnSavePackageDownloadCreated
117 drive::DownloadHandler* drive_download_handler =
118 drive::DownloadHandler::GetForProfile(profile);
119 DCHECK(drive_download_handler);
120 drive_download_handler->
121 SubstituteDriveDownloadPath(selected_path, NULL,
122 base::Bind(&ContinueSettingUpDriveDownload,
123 callback_,
124 is_html_,
125 profile,
126 selected_path));
127 } else {
128 callback_.Run(selected_path,
129 (is_html_ ?
130 content::SAVE_PAGE_TYPE_AS_MHTML :
131 content::SAVE_PAGE_TYPE_AS_ONLY_HTML),
132 content::SavePackageDownloadCreatedCallback());
133 }
134 delete this;
135 }
136
137 void SavePackageFilePickerChromeOS::FileSelectionCanceled(void* params) {
138 delete this;
139 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698