| 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/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/i18n/file_util_icu.h" | 9 #include "base/i18n/file_util_icu.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 using content::RenderProcessHost; | 33 using content::RenderProcessHost; |
| 34 using content::SavePageType; | 34 using content::SavePageType; |
| 35 using content::WebContents; | 35 using content::WebContents; |
| 36 | 36 |
| 37 namespace { | 37 namespace { |
| 38 | 38 |
| 39 // If false, we don't prompt the user as to where to save the file. This | 39 // If false, we don't prompt the user as to where to save the file. This |
| 40 // exists only for testing. | 40 // exists only for testing. |
| 41 bool g_should_prompt_for_filename = true; | 41 bool g_should_prompt_for_filename = true; |
| 42 | 42 |
| 43 #if !defined(OS_CHROMEOS) | |
| 44 // Used for mapping between SavePageType constants and the indexes above. | |
| 45 const SavePageType kIndexToSaveType[] = { | |
| 46 content::SAVE_PAGE_TYPE_UNKNOWN, | |
| 47 content::SAVE_PAGE_TYPE_AS_ONLY_HTML, | |
| 48 content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML, | |
| 49 }; | |
| 50 | |
| 51 int SavePackageTypeToIndex(SavePageType type) { | |
| 52 for (size_t i = 0; i < arraysize(kIndexToSaveType); ++i) { | |
| 53 if (kIndexToSaveType[i] == type) | |
| 54 return i; | |
| 55 } | |
| 56 NOTREACHED(); | |
| 57 return -1; | |
| 58 } | |
| 59 #endif | |
| 60 | |
| 61 // Indexes used for specifying which element in the extensions dropdown | |
| 62 // the user chooses when picking a save type. | |
| 63 const int kSelectFileHtmlOnlyIndex = 1; | |
| 64 const int kSelectFileCompleteIndex = 2; | |
| 65 | |
| 66 // Used for mapping between the IDS_ string identifiers and the indexes above. | |
| 67 const int kIndexToIDS[] = { | |
| 68 0, IDS_SAVE_PAGE_DESC_HTML_ONLY, IDS_SAVE_PAGE_DESC_COMPLETE, | |
| 69 }; | |
| 70 | |
| 71 void OnSavePackageDownloadCreated(content::DownloadItem* download) { | 43 void OnSavePackageDownloadCreated(content::DownloadItem* download) { |
| 72 ChromeDownloadManagerDelegate::DisableSafeBrowsing(download); | 44 ChromeDownloadManagerDelegate::DisableSafeBrowsing(download); |
| 73 } | 45 } |
| 74 | 46 |
| 75 #if defined(OS_CHROMEOS) | 47 #if defined(OS_CHROMEOS) |
| 76 void OnSavePackageDownloadCreatedChromeOS( | 48 void OnSavePackageDownloadCreatedChromeOS( |
| 77 Profile* profile, | 49 Profile* profile, |
| 78 const base::FilePath& drive_path, | 50 const base::FilePath& drive_path, |
| 79 content::DownloadItem* download) { | 51 content::DownloadItem* download) { |
| 80 drive::DownloadHandler::GetForProfile(profile)->SetDownloadParams( | 52 drive::DownloadHandler::GetForProfile(profile)->SetDownloadParams( |
| 81 drive_path, download); | 53 drive_path, download); |
| 82 OnSavePackageDownloadCreated(download); | 54 OnSavePackageDownloadCreated(download); |
| 83 } | 55 } |
| 84 | 56 |
| 85 // Trampoline callback between SubstituteDriveDownloadPath() and |callback|. | 57 // Trampoline callback between SubstituteDriveDownloadPath() and |callback|. |
| 86 void ContinueSettingUpDriveDownload( | 58 void ContinueSettingUpDriveDownload( |
| 87 const content::SavePackagePathPickedCallback& callback, | 59 const content::SavePackagePathPickedCallback& callback, |
| 88 content::SavePageType save_type, | 60 content::SavePageType save_type, |
| 89 Profile* profile, | 61 Profile* profile, |
| 90 const base::FilePath& drive_path, | 62 const base::FilePath& drive_path, |
| 91 const base::FilePath& drive_tmp_download_path) { | 63 const base::FilePath& drive_tmp_download_path) { |
| 92 if (drive_tmp_download_path.empty()) // Substitution failed. | 64 if (drive_tmp_download_path.empty()) // Substitution failed. |
| 93 return; | 65 return; |
| 94 callback.Run(drive_tmp_download_path, save_type, base::Bind( | 66 callback.Run(drive_tmp_download_path, save_type, base::Bind( |
| 95 &OnSavePackageDownloadCreatedChromeOS, profile, drive_path)); | 67 &OnSavePackageDownloadCreatedChromeOS, profile, drive_path)); |
| 96 } | 68 } |
| 97 #endif | 69 #endif |
| 98 | 70 |
| 71 // Adds "Webpage, HTML Only" type to FileTypeInfo. |
| 72 void AddHtmlOnlyFileTypeInfo( |
| 73 ui::SelectFileDialog::FileTypeInfo* file_type_info, |
| 74 const base::FilePath::StringType& extra_extension) { |
| 75 file_type_info->extension_description_overrides.push_back( |
| 76 l10n_util::GetStringUTF16(IDS_SAVE_PAGE_DESC_HTML_ONLY)); |
| 77 |
| 78 std::vector<base::FilePath::StringType> extensions; |
| 79 extensions.push_back(FILE_PATH_LITERAL("htm")); |
| 80 extensions.push_back(FILE_PATH_LITERAL("html")); |
| 81 if (!extra_extension.empty()) |
| 82 extensions.push_back(extra_extension); |
| 83 file_type_info->extensions.push_back(extensions); |
| 84 } |
| 85 |
| 86 // Adds "Web Archive, Single File" type to FileTypeInfo. |
| 87 void AddSingleFileFileTypeInfo( |
| 88 ui::SelectFileDialog::FileTypeInfo* file_type_info) { |
| 89 file_type_info->extension_description_overrides.push_back( |
| 90 l10n_util::GetStringUTF16(IDS_SAVE_PAGE_DESC_SINGLE_FILE)); |
| 91 |
| 92 std::vector<base::FilePath::StringType> extensions; |
| 93 extensions.push_back(FILE_PATH_LITERAL("mhtml")); |
| 94 file_type_info->extensions.push_back(extensions); |
| 95 } |
| 96 |
| 97 // Chrome OS doesn't support HTML-Complete. crbug.com/154823 |
| 98 #if !defined(OS_CHROMEOS) |
| 99 // Adds "Webpage, Complete" type to FileTypeInfo. |
| 100 void AddCompleteFileTypeInfo( |
| 101 ui::SelectFileDialog::FileTypeInfo* file_type_info, |
| 102 const base::FilePath::StringType& extra_extension) { |
| 103 file_type_info->extension_description_overrides.push_back( |
| 104 l10n_util::GetStringUTF16(IDS_SAVE_PAGE_DESC_COMPLETE)); |
| 105 |
| 106 std::vector<base::FilePath::StringType> extensions; |
| 107 extensions.push_back(FILE_PATH_LITERAL("htm")); |
| 108 extensions.push_back(FILE_PATH_LITERAL("html")); |
| 109 if (!extra_extension.empty()) |
| 110 extensions.push_back(extra_extension); |
| 111 file_type_info->extensions.push_back(extensions); |
| 112 } |
| 113 #endif |
| 114 |
| 99 } // anonymous namespace | 115 } // anonymous namespace |
| 100 | 116 |
| 101 bool SavePackageFilePicker::ShouldSaveAsMHTML() const { | 117 bool SavePackageFilePicker::ShouldSaveAsMHTML() const { |
| 102 #if !defined(OS_CHROMEOS) | 118 #if !defined(OS_CHROMEOS) |
| 103 if (!CommandLine::ForCurrentProcess()->HasSwitch( | 119 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| 104 switches::kSavePageAsMHTML)) | 120 switches::kSavePageAsMHTML)) |
| 105 return false; | 121 return false; |
| 106 #endif | 122 #endif |
| 107 return can_save_as_complete_; | 123 return can_save_as_complete_; |
| 108 } | 124 } |
| 109 | 125 |
| 110 SavePackageFilePicker::SavePackageFilePicker( | 126 SavePackageFilePicker::SavePackageFilePicker( |
| 111 content::WebContents* web_contents, | 127 content::WebContents* web_contents, |
| 112 const base::FilePath& suggested_path, | 128 const base::FilePath& suggested_path, |
| 113 const base::FilePath::StringType& default_extension, | 129 const base::FilePath::StringType& default_extension, |
| 114 bool can_save_as_complete, | 130 bool can_save_as_complete, |
| 115 DownloadPrefs* download_prefs, | 131 DownloadPrefs* download_prefs, |
| 116 const content::SavePackagePathPickedCallback& callback) | 132 const content::SavePackagePathPickedCallback& callback) |
| 117 : render_process_id_(web_contents->GetRenderProcessHost()->GetID()), | 133 : render_process_id_(web_contents->GetRenderProcessHost()->GetID()), |
| 118 can_save_as_complete_(can_save_as_complete), | 134 can_save_as_complete_(can_save_as_complete), |
| 119 download_prefs_(download_prefs), | 135 download_prefs_(download_prefs), |
| 120 callback_(callback) { | 136 callback_(callback) { |
| 121 base::FilePath suggested_path_copy = suggested_path; | 137 base::FilePath suggested_path_copy = suggested_path; |
| 122 base::FilePath::StringType default_extension_copy = default_extension; | 138 base::FilePath::StringType default_extension_copy = default_extension; |
| 123 int file_type_index = 0; | 139 int file_type_index = 0; |
| 124 ui::SelectFileDialog::FileTypeInfo file_type_info; | 140 ui::SelectFileDialog::FileTypeInfo file_type_info; |
| 125 | 141 |
| 126 #if defined(OS_CHROMEOS) | |
| 127 file_type_info.support_drive = true; | 142 file_type_info.support_drive = true; |
| 128 #else | 143 |
| 129 file_type_index = SavePackageTypeToIndex( | 144 if (can_save_as_complete_) { |
| 130 static_cast<SavePageType>(download_prefs_->save_file_type())); | 145 // The option index is not zero-based. Put a dummy entry. |
| 131 DCHECK_NE(-1, file_type_index); | 146 save_types_.push_back(content::SAVE_PAGE_TYPE_UNKNOWN); |
| 147 |
| 148 base::FilePath::StringType extra_extension; |
| 149 if (ShouldSaveAsMHTML()) { |
| 150 default_extension_copy = FILE_PATH_LITERAL("mhtml"); |
| 151 suggested_path_copy = suggested_path_copy.ReplaceExtension( |
| 152 default_extension_copy); |
| 153 } else { |
| 154 if (!suggested_path_copy.FinalExtension().empty() && |
| 155 !suggested_path_copy.MatchesExtension(FILE_PATH_LITERAL(".htm")) && |
| 156 !suggested_path_copy.MatchesExtension(FILE_PATH_LITERAL(".html"))) { |
| 157 extra_extension = suggested_path_copy.FinalExtension().substr(1); |
| 158 } |
| 159 } |
| 160 |
| 161 AddHtmlOnlyFileTypeInfo(&file_type_info, extra_extension); |
| 162 save_types_.push_back(content::SAVE_PAGE_TYPE_AS_ONLY_HTML); |
| 163 |
| 164 if (ShouldSaveAsMHTML()) { |
| 165 AddSingleFileFileTypeInfo(&file_type_info); |
| 166 save_types_.push_back(content::SAVE_PAGE_TYPE_AS_MHTML); |
| 167 } |
| 168 |
| 169 #if !defined(OS_CHROMEOS) |
| 170 AddCompleteFileTypeInfo(&file_type_info, extra_extension); |
| 171 save_types_.push_back(content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML); |
| 132 #endif | 172 #endif |
| 133 | 173 |
| 134 // TODO(benjhayden): Merge the first branch with the second when all of the | 174 file_type_info.include_all_files = false; |
| 135 // platform-specific file selection dialog implementations fully support | 175 |
| 136 // switching save-as file formats, and remove the flag/switch. | 176 content::SavePageType preferred_save_type = |
| 137 if (ShouldSaveAsMHTML()) { | 177 static_cast<content::SavePageType>(download_prefs_->save_file_type()); |
| 138 default_extension_copy = FILE_PATH_LITERAL("mhtml"); | 178 if (ShouldSaveAsMHTML()) |
| 139 suggested_path_copy = suggested_path_copy.ReplaceExtension( | 179 preferred_save_type = content::SAVE_PAGE_TYPE_AS_MHTML; |
| 140 default_extension_copy); | 180 |
| 141 } else if (can_save_as_complete_) { | 181 // Select the item saved in the pref. |
| 142 // NOTE: this branch will never run on chromeos because ShouldSaveAsHTML() | 182 for (size_t i = 0; i < save_types_.size(); ++i) { |
| 143 // == can_save_as_complete_ on chromeos. | 183 if (save_types_[i] == preferred_save_type) { |
| 144 bool add_extra_extension = false; | 184 file_type_index = i; |
| 145 base::FilePath::StringType extra_extension; | 185 break; |
| 146 if (!suggested_path_copy.FinalExtension().empty() && | 186 } |
| 147 !suggested_path_copy.MatchesExtension(FILE_PATH_LITERAL(".htm")) && | |
| 148 !suggested_path_copy.MatchesExtension(FILE_PATH_LITERAL(".html"))) { | |
| 149 add_extra_extension = true; | |
| 150 extra_extension = suggested_path_copy.FinalExtension().substr(1); | |
| 151 } | 187 } |
| 152 | 188 |
| 153 static const size_t kNumberExtensions = arraysize(kIndexToIDS) - 1; | 189 // If the item saved in the pref was not found, use the last item. |
| 154 file_type_info.extensions.resize(kNumberExtensions); | 190 if (!file_type_index) |
| 155 file_type_info.extension_description_overrides.resize(kNumberExtensions); | 191 file_type_index = save_types_.size() - 1; |
| 156 | |
| 157 // Indices into kIndexToIDS are 1-based whereas indices into | |
| 158 // file_type_info.extensions are 0-based. Hence the '-1's. | |
| 159 // If you switch these resize()/direct-assignment patterns to push_back(), | |
| 160 // then you risk breaking FileSelected()'s use of |index|. | |
| 161 | |
| 162 file_type_info.extension_description_overrides[ | |
| 163 kSelectFileHtmlOnlyIndex - 1] = l10n_util::GetStringUTF16(kIndexToIDS[ | |
| 164 kSelectFileHtmlOnlyIndex]); | |
| 165 file_type_info.extensions[kSelectFileHtmlOnlyIndex - 1].push_back( | |
| 166 FILE_PATH_LITERAL("htm")); | |
| 167 file_type_info.extensions[kSelectFileHtmlOnlyIndex - 1].push_back( | |
| 168 FILE_PATH_LITERAL("html")); | |
| 169 if (add_extra_extension) { | |
| 170 file_type_info.extensions[kSelectFileHtmlOnlyIndex - 1].push_back( | |
| 171 extra_extension); | |
| 172 } | |
| 173 | |
| 174 file_type_info.extension_description_overrides[ | |
| 175 kSelectFileCompleteIndex - 1] = l10n_util::GetStringUTF16(kIndexToIDS[ | |
| 176 kSelectFileCompleteIndex]); | |
| 177 file_type_info.extensions[kSelectFileCompleteIndex - 1].push_back( | |
| 178 FILE_PATH_LITERAL("htm")); | |
| 179 file_type_info.extensions[kSelectFileCompleteIndex - 1].push_back( | |
| 180 FILE_PATH_LITERAL("html")); | |
| 181 if (add_extra_extension) { | |
| 182 file_type_info.extensions[kSelectFileCompleteIndex - 1].push_back( | |
| 183 extra_extension); | |
| 184 } | |
| 185 | |
| 186 file_type_info.include_all_files = false; | |
| 187 } else { | 192 } else { |
| 188 // The contents can not be saved as complete-HTML, so do not show the file | 193 // The contents can not be saved as complete-HTML, so do not show the file |
| 189 // filters. | 194 // filters. |
| 190 file_type_info.extensions.resize(1); | 195 file_type_info.extensions.resize(1); |
| 191 file_type_info.extensions[0].push_back( | 196 file_type_info.extensions[0].push_back( |
| 192 suggested_path_copy.FinalExtension()); | 197 suggested_path_copy.FinalExtension()); |
| 193 | 198 |
| 194 if (!file_type_info.extensions[0][0].empty()) { | 199 if (!file_type_info.extensions[0][0].empty()) { |
| 195 // Drop the . | 200 // Drop the . |
| 196 file_type_info.extensions[0][0].erase(0, 1); | 201 file_type_info.extensions[0][0].erase(0, 1); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 227 } | 232 } |
| 228 | 233 |
| 229 void SavePackageFilePicker::FileSelected( | 234 void SavePackageFilePicker::FileSelected( |
| 230 const base::FilePath& path, int index, void* unused_params) { | 235 const base::FilePath& path, int index, void* unused_params) { |
| 231 scoped_ptr<SavePackageFilePicker> delete_this(this); | 236 scoped_ptr<SavePackageFilePicker> delete_this(this); |
| 232 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id_); | 237 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id_); |
| 233 if (!process) | 238 if (!process) |
| 234 return; | 239 return; |
| 235 SavePageType save_type = content::SAVE_PAGE_TYPE_UNKNOWN; | 240 SavePageType save_type = content::SAVE_PAGE_TYPE_UNKNOWN; |
| 236 | 241 |
| 237 if (ShouldSaveAsMHTML()) { | 242 if (can_save_as_complete_) { |
| 238 save_type = content::SAVE_PAGE_TYPE_AS_MHTML; | 243 DCHECK_LT(index, static_cast<int>(save_types_.size())); |
| 239 } else { | 244 save_type = save_types_[index]; |
| 240 #if defined(OS_CHROMEOS) | |
| 241 save_type = content::SAVE_PAGE_TYPE_AS_ONLY_HTML; | |
| 242 #else | |
| 243 // The option index is not zero-based. | |
| 244 DCHECK(index >= kSelectFileHtmlOnlyIndex && | |
| 245 index <= kSelectFileCompleteIndex); | |
| 246 save_type = kIndexToSaveType[index]; | |
| 247 if (select_file_dialog_.get() && | 245 if (select_file_dialog_.get() && |
| 248 select_file_dialog_->HasMultipleFileTypeChoices()) | 246 select_file_dialog_->HasMultipleFileTypeChoices()) |
| 249 download_prefs_->SetSaveFileType(save_type); | 247 download_prefs_->SetSaveFileType(save_type); |
| 250 #endif | 248 |
| 249 UMA_HISTOGRAM_ENUMERATION("Download.SavePageType", |
| 250 save_type, |
| 251 content::SAVE_PAGE_TYPE_MAX); |
| 252 } else { |
| 253 // Use "HTML Only" type as a dummy. |
| 254 save_type = content::SAVE_PAGE_TYPE_AS_ONLY_HTML; |
| 251 } | 255 } |
| 252 | 256 |
| 253 UMA_HISTOGRAM_ENUMERATION("Download.SavePageType", | |
| 254 save_type, | |
| 255 content::SAVE_PAGE_TYPE_MAX); | |
| 256 | |
| 257 base::FilePath path_copy(path); | 257 base::FilePath path_copy(path); |
| 258 file_util::NormalizeFileNameEncoding(&path_copy); | 258 file_util::NormalizeFileNameEncoding(&path_copy); |
| 259 | 259 |
| 260 download_prefs_->SetSaveFilePath(path_copy.DirName()); | 260 download_prefs_->SetSaveFilePath(path_copy.DirName()); |
| 261 | 261 |
| 262 #if defined(OS_CHROMEOS) | 262 #if defined(OS_CHROMEOS) |
| 263 if (drive::util::IsUnderDriveMountPoint(path_copy)) { | 263 if (drive::util::IsUnderDriveMountPoint(path_copy)) { |
| 264 // Here's a map to the callback chain: | 264 // Here's a map to the callback chain: |
| 265 // SubstituteDriveDownloadPath -> | 265 // SubstituteDriveDownloadPath -> |
| 266 // ContinueSettingUpDriveDownload -> | 266 // ContinueSettingUpDriveDownload -> |
| (...skipping 13 matching lines...) Expand all Loading... |
| 280 } | 280 } |
| 281 #endif | 281 #endif |
| 282 | 282 |
| 283 callback_.Run(path_copy, save_type, | 283 callback_.Run(path_copy, save_type, |
| 284 base::Bind(&OnSavePackageDownloadCreated)); | 284 base::Bind(&OnSavePackageDownloadCreated)); |
| 285 } | 285 } |
| 286 | 286 |
| 287 void SavePackageFilePicker::FileSelectionCanceled(void* unused_params) { | 287 void SavePackageFilePicker::FileSelectionCanceled(void* unused_params) { |
| 288 delete this; | 288 delete this; |
| 289 } | 289 } |
| OLD | NEW |