OLD | NEW |
---|---|
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" | |
7 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/i18n/file_util_icu.h" | |
8 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
9 #include "base/prefs/pref_member.h" | 11 #include "base/prefs/pref_member.h" |
10 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
11 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
12 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 14 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
13 #include "chrome/browser/download/download_prefs.h" | 15 #include "chrome/browser/download/download_prefs.h" |
14 #include "chrome/browser/platform_util.h" | 16 #include "chrome/browser/platform_util.h" |
15 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/ui/chrome_select_file_policy.h" | 18 #include "chrome/browser/ui/chrome_select_file_policy.h" |
17 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
18 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
19 #include "content/public/browser/download_manager.h" | 21 #include "content/public/browser/download_manager.h" |
20 #include "content/public/browser/render_process_host.h" | 22 #include "content/public/browser/render_process_host.h" |
21 #include "content/public/browser/save_page_type.h" | 23 #include "content/public/browser/save_page_type.h" |
22 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
23 #include "content/public/browser/web_contents_view.h" | 25 #include "content/public/browser/web_contents_view.h" |
24 #include "grit/generated_resources.h" | 26 #include "grit/generated_resources.h" |
25 #include "ui/base/l10n/l10n_util.h" | 27 #include "ui/base/l10n/l10n_util.h" |
26 | 28 |
29 #if defined(OS_CHROMEOS) | |
30 #include "chrome/browser/chromeos/drive/download_handler.h" | |
31 #include "chrome/browser/chromeos/drive/file_system_util.h" | |
32 #endif | |
33 | |
27 using content::RenderProcessHost; | 34 using content::RenderProcessHost; |
28 using content::SavePageType; | 35 using content::SavePageType; |
29 using content::WebContents; | 36 using content::WebContents; |
30 | 37 |
31 namespace { | 38 namespace { |
32 | 39 |
33 // If false, we don't prompt the user as to where to save the file. This | 40 // If false, we don't prompt the user as to where to save the file. This |
34 // exists only for testing. | 41 // exists only for testing. |
35 bool g_should_prompt_for_filename = true; | 42 bool g_should_prompt_for_filename = true; |
36 | 43 |
(...skipping 16 matching lines...) Expand all Loading... | |
53 // Indexes used for specifying which element in the extensions dropdown | 60 // Indexes used for specifying which element in the extensions dropdown |
54 // the user chooses when picking a save type. | 61 // the user chooses when picking a save type. |
55 const int kSelectFileHtmlOnlyIndex = 1; | 62 const int kSelectFileHtmlOnlyIndex = 1; |
56 const int kSelectFileCompleteIndex = 2; | 63 const int kSelectFileCompleteIndex = 2; |
57 | 64 |
58 // Used for mapping between the IDS_ string identifiers and the indexes above. | 65 // Used for mapping between the IDS_ string identifiers and the indexes above. |
59 const int kIndexToIDS[] = { | 66 const int kIndexToIDS[] = { |
60 0, IDS_SAVE_PAGE_DESC_HTML_ONLY, IDS_SAVE_PAGE_DESC_COMPLETE, | 67 0, IDS_SAVE_PAGE_DESC_HTML_ONLY, IDS_SAVE_PAGE_DESC_COMPLETE, |
61 }; | 68 }; |
62 | 69 |
63 void OnSavePackageDownloadCreated( | 70 void OnSavePackageDownloadCreated(content::DownloadItem* download) { |
64 content::DownloadItem* download) { | |
65 ChromeDownloadManagerDelegate::DisableSafeBrowsing(download); | 71 ChromeDownloadManagerDelegate::DisableSafeBrowsing(download); |
66 } | 72 } |
67 | 73 |
74 #if defined(OS_CHROMEOS) | |
75 void OnSavePackageDownloadCreatedChromeOS( | |
76 Profile* profile, | |
77 const base::FilePath& drive_path, | |
78 content::DownloadItem* download) { | |
79 drive::DownloadHandler::GetForProfile(profile)->SetDownloadParams( | |
80 drive_path, download); | |
81 OnSavePackageDownloadCreated(download); | |
82 } | |
83 | |
84 // Trampoline callback between SubstituteDriveDownloadPath() and |callback|. | |
85 void ContinueSettingUpDriveDownload( | |
86 const content::SavePackagePathPickedCallback& callback, | |
87 content::SavePageType save_type, | |
88 Profile* profile, | |
89 const base::FilePath& drive_path, | |
90 const base::FilePath& drive_tmp_download_path) { | |
91 if (drive_tmp_download_path.empty()) // Substitution failed. | |
92 return; | |
93 callback.Run(drive_tmp_download_path, save_type, base::Bind( | |
94 &OnSavePackageDownloadCreatedChromeOS, profile, drive_path)); | |
95 } | |
96 #endif | |
97 | |
68 } // anonymous namespace | 98 } // anonymous namespace |
69 | 99 |
70 bool SavePackageFilePicker::ShouldSaveAsMHTML() const { | 100 bool SavePackageFilePicker::ShouldSaveAsMHTML() const { |
71 return can_save_as_complete_ && | 101 #if !defined(OS_CHROMEOS) |
72 CommandLine::ForCurrentProcess()->HasSwitch( | 102 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
73 switches::kSavePageAsMHTML); | 103 switches::kSavePageAsMHTML)) |
104 return false; | |
105 #endif | |
106 return can_save_as_complete_; | |
74 } | 107 } |
75 | 108 |
76 SavePackageFilePicker::SavePackageFilePicker( | 109 SavePackageFilePicker::SavePackageFilePicker( |
77 content::WebContents* web_contents, | 110 content::WebContents* web_contents, |
78 const base::FilePath& suggested_path_const, | 111 const base::FilePath& suggested_path, |
79 const base::FilePath::StringType& default_extension_const, | 112 const base::FilePath::StringType& default_extension, |
80 bool can_save_as_complete, | 113 bool can_save_as_complete, |
81 DownloadPrefs* download_prefs, | 114 DownloadPrefs* download_prefs, |
82 const content::SavePackagePathPickedCallback& callback) | 115 const content::SavePackagePathPickedCallback& callback) |
83 : render_process_id_(web_contents->GetRenderProcessHost()->GetID()), | 116 : render_process_id_(web_contents->GetRenderProcessHost()->GetID()), |
84 can_save_as_complete_(can_save_as_complete), | 117 can_save_as_complete_(can_save_as_complete), |
118 download_prefs_(download_prefs), | |
85 callback_(callback) { | 119 callback_(callback) { |
86 base::FilePath suggested_path = suggested_path_const; | 120 base::FilePath suggested_path_copy = suggested_path; |
87 base::FilePath::StringType default_extension = default_extension_const; | 121 base::FilePath::StringType default_extension_copy = default_extension; |
88 int file_type_index = SavePackageTypeToIndex( | 122 int file_type_index = 0; |
89 static_cast<SavePageType>(download_prefs->save_file_type())); | 123 ui::SelectFileDialog::FileTypeInfo file_type_info; |
124 | |
125 #if defined(OS_CHROMEOS) | |
126 file_type_info.support_drive = true; | |
127 #else | |
128 file_type_index = SavePackageTypeToIndex( | |
129 static_cast<SavePageType>(download_prefs_->save_file_type())); | |
90 DCHECK_NE(-1, file_type_index); | 130 DCHECK_NE(-1, file_type_index); |
91 | 131 #endif |
92 ui::SelectFileDialog::FileTypeInfo file_type_info; | |
93 | 132 |
94 // TODO(benjhayden): Merge the first branch with the second when all of the | 133 // TODO(benjhayden): Merge the first branch with the second when all of the |
95 // platform-specific file selection dialog implementations fully support | 134 // platform-specific file selection dialog implementations fully support |
96 // switching save-as file formats, and remove the flag/switch. | 135 // switching save-as file formats, and remove the flag/switch. |
97 if (ShouldSaveAsMHTML()) { | 136 if (ShouldSaveAsMHTML()) { |
98 default_extension = FILE_PATH_LITERAL("mhtml"); | 137 default_extension_copy = FILE_PATH_LITERAL("mhtml"); |
99 suggested_path = suggested_path.ReplaceExtension(default_extension); | 138 suggested_path_copy = suggested_path_copy.ReplaceExtension( |
100 } else if (can_save_as_complete) { | 139 default_extension_copy); |
140 } else if (can_save_as_complete_) { | |
141 // NOTE: this branch will never run on chromeos because ShouldSaveAsHTML() | |
142 // == can_save_as_complete_ on chromeos. | |
101 bool add_extra_extension = false; | 143 bool add_extra_extension = false; |
102 base::FilePath::StringType extra_extension; | 144 base::FilePath::StringType extra_extension; |
103 if (!suggested_path.Extension().empty() && | 145 if (!suggested_path_copy.Extension().empty() && |
104 suggested_path.Extension().compare(FILE_PATH_LITERAL("htm")) && | 146 suggested_path_copy.Extension().compare(FILE_PATH_LITERAL("htm")) && |
asanka
2013/05/07 15:15:12
FilePath::Extension() returns a leading period. I.
benjhayden
2013/05/17 20:31:54
Done.
| |
105 suggested_path.Extension().compare(FILE_PATH_LITERAL("html"))) { | 147 suggested_path_copy.Extension().compare(FILE_PATH_LITERAL("html"))) { |
106 add_extra_extension = true; | 148 add_extra_extension = true; |
107 extra_extension = suggested_path.Extension().substr(1); | 149 extra_extension = suggested_path_copy.Extension().substr(1); |
108 } | 150 } |
109 | 151 |
110 static const size_t kNumberExtensions = arraysize(kIndexToIDS) - 1; | 152 static const size_t kNumberExtensions = arraysize(kIndexToIDS) - 1; |
111 file_type_info.extensions.resize(kNumberExtensions); | 153 file_type_info.extensions.resize(kNumberExtensions); |
112 file_type_info.extension_description_overrides.resize(kNumberExtensions); | 154 file_type_info.extension_description_overrides.resize(kNumberExtensions); |
113 | 155 |
114 // Indices into kIndexToIDS are 1-based whereas indices into | 156 // Indices into kIndexToIDS are 1-based whereas indices into |
115 // file_type_info.extensions are 0-based. Hence the '-1's. | 157 // file_type_info.extensions are 0-based. Hence the '-1's. |
116 // If you switch these resize()/direct-assignment patterns to push_back(), | 158 // If you switch these resize()/direct-assignment patterns to push_back(), |
117 // then you risk breaking FileSelected()'s use of |index|. | 159 // then you risk breaking FileSelected()'s use of |index|. |
(...skipping 21 matching lines...) Expand all Loading... | |
139 file_type_info.extensions[kSelectFileCompleteIndex - 1].push_back( | 181 file_type_info.extensions[kSelectFileCompleteIndex - 1].push_back( |
140 extra_extension); | 182 extra_extension); |
141 } | 183 } |
142 | 184 |
143 file_type_info.include_all_files = false; | 185 file_type_info.include_all_files = false; |
144 } else { | 186 } else { |
145 // The contents can not be saved as complete-HTML, so do not show the file | 187 // The contents can not be saved as complete-HTML, so do not show the file |
146 // filters. | 188 // filters. |
147 file_type_info.extensions.resize(1); | 189 file_type_info.extensions.resize(1); |
148 file_type_info.extensions[0].push_back( | 190 file_type_info.extensions[0].push_back( |
149 suggested_path.Extension()); | 191 suggested_path_copy.Extension()); |
150 | 192 |
151 if (!file_type_info.extensions[0][0].empty()) { | 193 if (!file_type_info.extensions[0][0].empty()) { |
152 // Drop the . | 194 // Drop the . |
153 file_type_info.extensions[0][0].erase(0, 1); | 195 file_type_info.extensions[0][0].erase(0, 1); |
154 } | 196 } |
155 | 197 |
156 file_type_info.include_all_files = true; | 198 file_type_info.include_all_files = true; |
157 file_type_index = 1; | 199 file_type_index = 1; |
158 } | 200 } |
159 | 201 |
160 if (g_should_prompt_for_filename) { | 202 if (g_should_prompt_for_filename) { |
161 select_file_dialog_ = ui::SelectFileDialog::Create( | 203 select_file_dialog_ = ui::SelectFileDialog::Create( |
162 this, new ChromeSelectFilePolicy(web_contents)); | 204 this, new ChromeSelectFilePolicy(web_contents)); |
163 select_file_dialog_->SelectFile( | 205 select_file_dialog_->SelectFile( |
164 ui::SelectFileDialog::SELECT_SAVEAS_FILE, | 206 ui::SelectFileDialog::SELECT_SAVEAS_FILE, |
165 string16(), | 207 string16(), |
166 suggested_path, | 208 suggested_path_copy, |
167 &file_type_info, | 209 &file_type_info, |
168 file_type_index, | 210 file_type_index, |
169 default_extension, | 211 default_extension_copy, |
170 platform_util::GetTopLevel(web_contents->GetView()->GetNativeView()), | 212 platform_util::GetTopLevel(web_contents->GetView()->GetNativeView()), |
171 NULL); | 213 NULL); |
172 } else { | 214 } else { |
173 // Just use 'suggested_path' instead of opening the dialog prompt. | 215 // Just use 'suggested_path_copy' instead of opening the dialog prompt. |
174 // Go through FileSelected() for consistency. | 216 // Go through FileSelected() for consistency. |
175 FileSelected(suggested_path, file_type_index, NULL); | 217 FileSelected(suggested_path_copy, file_type_index, NULL); |
176 } | 218 } |
177 } | 219 } |
178 | 220 |
179 SavePackageFilePicker::~SavePackageFilePicker() { | 221 SavePackageFilePicker::~SavePackageFilePicker() { |
180 } | 222 } |
181 | 223 |
182 void SavePackageFilePicker::SetShouldPromptUser(bool should_prompt) { | 224 void SavePackageFilePicker::SetShouldPromptUser(bool should_prompt) { |
183 g_should_prompt_for_filename = should_prompt; | 225 g_should_prompt_for_filename = should_prompt; |
184 } | 226 } |
185 | 227 |
186 void SavePackageFilePicker::FileSelected(const base::FilePath& path, | 228 void SavePackageFilePicker::FileSelected( |
187 int index, | 229 const base::FilePath& path, int index, void* unused_params) { |
188 void* unused_params) { | 230 scoped_ptr<SavePackageFilePicker> delete_this(this); |
189 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id_); | 231 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id_); |
190 if (process) { | 232 if (!process) |
191 SavePageType save_type = content::SAVE_PAGE_TYPE_UNKNOWN; | 233 return; |
192 PrefService* prefs = Profile::FromBrowserContext( | 234 SavePageType save_type = content::SAVE_PAGE_TYPE_UNKNOWN; |
193 process->GetBrowserContext())->GetPrefs(); | |
194 if (ShouldSaveAsMHTML()) { | |
195 save_type = content::SAVE_PAGE_TYPE_AS_MHTML; | |
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 | 235 |
206 UMA_HISTOGRAM_ENUMERATION("Download.SavePageType", | 236 if (ShouldSaveAsMHTML()) { |
207 save_type, | 237 save_type = content::SAVE_PAGE_TYPE_AS_MHTML; |
208 content::SAVE_PAGE_TYPE_MAX); | 238 } else { |
209 | 239 #if defined(OS_CHROMEOS) |
210 StringPrefMember save_file_path; | 240 save_type = content::SAVE_PAGE_TYPE_AS_ONLY_HTML; |
211 save_file_path.Init(prefs::kSaveFileDefaultDirectory, prefs); | 241 #else |
212 #if defined(OS_POSIX) | 242 // The option index is not zero-based. |
213 std::string path_string = path.DirName().value(); | 243 DCHECK(index >= kSelectFileHtmlOnlyIndex && |
214 #elif defined(OS_WIN) | 244 index <= kSelectFileCompleteIndex); |
215 std::string path_string = WideToUTF8(path.DirName().value()); | 245 save_type = kIndexToSaveType[index]; |
246 if (select_file_dialog_ && | |
247 select_file_dialog_->HasMultipleFileTypeChoices()) | |
248 download_prefs_->SetSaveFileType(save_type); | |
216 #endif | 249 #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 } | 250 } |
225 | 251 |
226 delete this; | 252 UMA_HISTOGRAM_ENUMERATION("Download.SavePageType", |
253 save_type, | |
254 content::SAVE_PAGE_TYPE_MAX); | |
255 | |
256 base::FilePath path_copy(path); | |
257 file_util::NormalizeFileNameEncoding(&path_copy); | |
258 | |
259 download_prefs_->SetSaveFilePath(path_copy.DirName()); | |
260 | |
261 #if defined(OS_CHROMEOS) | |
262 if (drive::util::IsUnderDriveMountPoint(path_copy)) { | |
263 // Here's a map to the callback chain: | |
264 // SubstituteDriveDownloadPath -> | |
265 // ContinueSettingUpDriveDownload -> | |
266 // callback_ = SavePackage::OnPathPicked -> | |
267 // download_created_callback = OnSavePackageDownloadCreatedChromeOS | |
268 Profile* profile = Profile::FromBrowserContext( | |
269 process->GetBrowserContext()); | |
270 drive::DownloadHandler* drive_download_handler = | |
271 drive::DownloadHandler::GetForProfile(profile); | |
272 drive_download_handler->SubstituteDriveDownloadPath( | |
273 path_copy, NULL, base::Bind(&ContinueSettingUpDriveDownload, | |
274 callback_, | |
275 save_type, | |
276 profile, | |
277 path_copy)); | |
278 return; | |
279 } | |
280 #endif | |
281 | |
282 callback_.Run(path_copy, save_type, | |
283 base::Bind(&OnSavePackageDownloadCreated)); | |
227 } | 284 } |
228 | 285 |
229 void SavePackageFilePicker::FileSelectionCanceled(void* unused_params) { | 286 void SavePackageFilePicker::FileSelectionCanceled(void* unused_params) { |
230 delete this; | 287 delete this; |
231 } | 288 } |
OLD | NEW |