Override server-side simple-cache trial with commandline switches.
[chromium-blink-merge.git] / chrome / browser / download / save_package_file_picker.cc
blobfe76d68f70d942175817fc8033de403a12939027
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.
5 #include "chrome/browser/download/save_package_file_picker.h"
7 #include "base/command_line.h"
8 #include "base/metrics/histogram.h"
9 #include "base/prefs/pref_member.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/utf_string_conversions.h"
12 #include "chrome/browser/download/chrome_download_manager_delegate.h"
13 #include "chrome/browser/download/download_prefs.h"
14 #include "chrome/browser/platform_util.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/chrome_select_file_policy.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/pref_names.h"
19 #include "content/public/browser/download_manager.h"
20 #include "content/public/browser/render_process_host.h"
21 #include "content/public/browser/save_page_type.h"
22 #include "content/public/browser/web_contents.h"
23 #include "content/public/browser/web_contents_view.h"
24 #include "grit/generated_resources.h"
25 #include "ui/base/l10n/l10n_util.h"
27 using content::RenderProcessHost;
28 using content::SavePageType;
29 using content::WebContents;
31 namespace {
33 // If false, we don't prompt the user as to where to save the file. This
34 // exists only for testing.
35 bool g_should_prompt_for_filename = true;
37 // Used for mapping between SavePageType constants and the indexes above.
38 const SavePageType kIndexToSaveType[] = {
39 content::SAVE_PAGE_TYPE_UNKNOWN,
40 content::SAVE_PAGE_TYPE_AS_ONLY_HTML,
41 content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML,
44 int SavePackageTypeToIndex(SavePageType type) {
45 for (size_t i = 0; i < arraysize(kIndexToSaveType); ++i) {
46 if (kIndexToSaveType[i] == type)
47 return i;
49 NOTREACHED();
50 return -1;
53 // Indexes used for specifying which element in the extensions dropdown
54 // the user chooses when picking a save type.
55 const int kSelectFileHtmlOnlyIndex = 1;
56 const int kSelectFileCompleteIndex = 2;
58 // Used for mapping between the IDS_ string identifiers and the indexes above.
59 const int kIndexToIDS[] = {
60 0, IDS_SAVE_PAGE_DESC_HTML_ONLY, IDS_SAVE_PAGE_DESC_COMPLETE,
63 void OnSavePackageDownloadCreated(
64 content::DownloadItem* download) {
65 ChromeDownloadManagerDelegate::DisableSafeBrowsing(download);
68 } // anonymous namespace
70 bool SavePackageFilePicker::ShouldSaveAsMHTML() const {
71 return can_save_as_complete_ &&
72 CommandLine::ForCurrentProcess()->HasSwitch(
73 switches::kSavePageAsMHTML);
76 SavePackageFilePicker::SavePackageFilePicker(
77 content::WebContents* web_contents,
78 const base::FilePath& suggested_path_const,
79 const base::FilePath::StringType& default_extension_const,
80 bool can_save_as_complete,
81 DownloadPrefs* download_prefs,
82 const content::SavePackagePathPickedCallback& callback)
83 : render_process_id_(web_contents->GetRenderProcessHost()->GetID()),
84 can_save_as_complete_(can_save_as_complete),
85 callback_(callback) {
86 base::FilePath suggested_path = suggested_path_const;
87 base::FilePath::StringType default_extension = default_extension_const;
88 int file_type_index = SavePackageTypeToIndex(
89 static_cast<SavePageType>(download_prefs->save_file_type()));
90 DCHECK_NE(-1, file_type_index);
92 ui::SelectFileDialog::FileTypeInfo file_type_info;
94 // TODO(benjhayden): Merge the first branch with the second when all of the
95 // platform-specific file selection dialog implementations fully support
96 // switching save-as file formats, and remove the flag/switch.
97 if (ShouldSaveAsMHTML()) {
98 default_extension = FILE_PATH_LITERAL("mhtml");
99 suggested_path = suggested_path.ReplaceExtension(default_extension);
100 } else if (can_save_as_complete) {
101 bool add_extra_extension = false;
102 base::FilePath::StringType extra_extension;
103 if (!suggested_path.Extension().empty() &&
104 suggested_path.Extension().compare(FILE_PATH_LITERAL("htm")) &&
105 suggested_path.Extension().compare(FILE_PATH_LITERAL("html"))) {
106 add_extra_extension = true;
107 extra_extension = suggested_path.Extension().substr(1);
110 static const size_t kNumberExtensions = arraysize(kIndexToIDS) - 1;
111 file_type_info.extensions.resize(kNumberExtensions);
112 file_type_info.extension_description_overrides.resize(kNumberExtensions);
114 // Indices into kIndexToIDS are 1-based whereas indices into
115 // file_type_info.extensions are 0-based. Hence the '-1's.
116 // If you switch these resize()/direct-assignment patterns to push_back(),
117 // then you risk breaking FileSelected()'s use of |index|.
119 file_type_info.extension_description_overrides[
120 kSelectFileHtmlOnlyIndex - 1] = l10n_util::GetStringUTF16(kIndexToIDS[
121 kSelectFileHtmlOnlyIndex]);
122 file_type_info.extensions[kSelectFileHtmlOnlyIndex - 1].push_back(
123 FILE_PATH_LITERAL("htm"));
124 file_type_info.extensions[kSelectFileHtmlOnlyIndex - 1].push_back(
125 FILE_PATH_LITERAL("html"));
126 if (add_extra_extension) {
127 file_type_info.extensions[kSelectFileHtmlOnlyIndex - 1].push_back(
128 extra_extension);
131 file_type_info.extension_description_overrides[
132 kSelectFileCompleteIndex - 1] = l10n_util::GetStringUTF16(kIndexToIDS[
133 kSelectFileCompleteIndex]);
134 file_type_info.extensions[kSelectFileCompleteIndex - 1].push_back(
135 FILE_PATH_LITERAL("htm"));
136 file_type_info.extensions[kSelectFileCompleteIndex - 1].push_back(
137 FILE_PATH_LITERAL("html"));
138 if (add_extra_extension) {
139 file_type_info.extensions[kSelectFileCompleteIndex - 1].push_back(
140 extra_extension);
143 file_type_info.include_all_files = false;
144 } else {
145 // The contents can not be saved as complete-HTML, so do not show the file
146 // filters.
147 file_type_info.extensions.resize(1);
148 file_type_info.extensions[0].push_back(
149 suggested_path.Extension());
151 if (!file_type_info.extensions[0][0].empty()) {
152 // Drop the .
153 file_type_info.extensions[0][0].erase(0, 1);
156 file_type_info.include_all_files = true;
157 file_type_index = 1;
160 if (g_should_prompt_for_filename) {
161 select_file_dialog_ = ui::SelectFileDialog::Create(
162 this, new ChromeSelectFilePolicy(web_contents));
163 select_file_dialog_->SelectFile(
164 ui::SelectFileDialog::SELECT_SAVEAS_FILE,
165 string16(),
166 suggested_path,
167 &file_type_info,
168 file_type_index,
169 default_extension,
170 platform_util::GetTopLevel(web_contents->GetView()->GetNativeView()),
171 NULL);
172 } else {
173 // Just use 'suggested_path' instead of opening the dialog prompt.
174 // Go through FileSelected() for consistency.
175 FileSelected(suggested_path, file_type_index, NULL);
179 SavePackageFilePicker::~SavePackageFilePicker() {
182 void SavePackageFilePicker::SetShouldPromptUser(bool should_prompt) {
183 g_should_prompt_for_filename = should_prompt;
186 void SavePackageFilePicker::FileSelected(const base::FilePath& path,
187 int index,
188 void* unused_params) {
189 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id_);
190 if (process) {
191 SavePageType save_type = content::SAVE_PAGE_TYPE_UNKNOWN;
192 PrefService* prefs = Profile::FromBrowserContext(
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);
206 UMA_HISTOGRAM_ENUMERATION("Download.SavePageType",
207 save_type,
208 content::SAVE_PAGE_TYPE_MAX);
210 StringPrefMember save_file_path;
211 save_file_path.Init(prefs::kSaveFileDefaultDirectory, prefs);
212 #if defined(OS_POSIX)
213 std::string path_string = path.DirName().value();
214 #elif defined(OS_WIN)
215 std::string path_string = WideToUTF8(path.DirName().value());
216 #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);
223 callback_.Run(path, save_type, base::Bind(&OnSavePackageDownloadCreated));
226 delete this;
229 void SavePackageFilePicker::FileSelectionCanceled(void* unused_params) {
230 delete this;