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

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

Issue 10069014: Save Page As MHTML (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: whitespace Created 8 years, 8 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/metrics/histogram.h"
7 #include "base/utf_string_conversions.h" 8 #include "base/utf_string_conversions.h"
8 #include "chrome/browser/download/download_prefs.h" 9 #include "chrome/browser/download/download_prefs.h"
9 #include "chrome/browser/platform_util.h" 10 #include "chrome/browser/platform_util.h"
10 #include "chrome/browser/prefs/pref_member.h" 11 #include "chrome/browser/prefs/pref_member.h"
11 #include "chrome/browser/prefs/pref_service.h" 12 #include "chrome/browser/prefs/pref_service.h"
12 #include "chrome/browser/profiles/profile.h" 13 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/common/pref_names.h" 14 #include "chrome/common/pref_names.h"
14 #include "content/public/browser/download_manager.h" 15 #include "content/public/browser/download_manager.h"
15 #include "content/public/browser/render_process_host.h" 16 #include "content/public/browser/render_process_host.h"
16 #include "content/public/browser/save_page_type.h" 17 #include "content/public/browser/save_page_type.h"
17 #include "content/public/browser/web_contents.h" 18 #include "content/public/browser/web_contents.h"
18 #include "grit/generated_resources.h" 19 #include "grit/generated_resources.h"
19 #include "ui/base/l10n/l10n_util.h" 20 #include "ui/base/l10n/l10n_util.h"
20 21
21 using content::RenderProcessHost; 22 using content::RenderProcessHost;
22 using content::SavePageType; 23 using content::SavePageType;
23 using content::WebContents; 24 using content::WebContents;
24 25
25 namespace { 26 namespace {
26 27
27 // If false, we don't prompt the user as to where to save the file. This 28 // If false, we don't prompt the user as to where to save the file. This
28 // exists only for testing. 29 // exists only for testing.
29 bool g_should_prompt_for_filename = true; 30 bool g_should_prompt_for_filename = true;
30 31
31 // Used for mapping between SavePageType constants and the indexes above. 32 // Used for mapping between SavePageType constants and the indexes above.
32 const SavePageType kIndexToSaveType[] = { 33 const SavePageType kIndexToSaveType[] = {
33 content::SAVE_PAGE_TYPE_UNKNOWN, 34 content::SAVE_PAGE_TYPE_UNKNOWN,
34 content::SAVE_PAGE_TYPE_AS_ONLY_HTML, 35 content::SAVE_PAGE_TYPE_AS_ONLY_HTML,
35 content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML, 36 content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML,
37 content::SAVE_PAGE_TYPE_AS_MHTML,
36 }; 38 };
37 39
38 int SavePackageTypeToIndex(SavePageType type) { 40 int SavePackageTypeToIndex(SavePageType type) {
39 for (size_t i = 0; i < arraysize(kIndexToSaveType); ++i) { 41 for (size_t i = 0; i < arraysize(kIndexToSaveType); ++i) {
40 if (kIndexToSaveType[i] == type) 42 if (kIndexToSaveType[i] == type)
41 return i; 43 return i;
42 } 44 }
43 NOTREACHED(); 45 NOTREACHED();
44 return -1; 46 return -1;
45 } 47 }
46 48
47 // Indexes used for specifying which element in the extensions dropdown 49 // Indexes used for specifying which element in the extensions dropdown
48 // the user chooses when picking a save type. 50 // the user chooses when picking a save type.
49 const int kSelectFileHtmlOnlyIndex = 1; 51 const int kSelectFileHtmlOnlyIndex = 1;
50 const int kSelectFileCompleteIndex = 2; 52 const int kSelectFileCompleteIndex = 2;
53 const int kSelectFileCompleteSingleFileIndex = 3;
51 54
52 // Used for mapping between the IDS_ string identifiers and the indexes above. 55 // Used for mapping between the IDS_ string identifiers and the indexes above.
53 const int kIndexToIDS[] = { 56 const int kIndexToIDS[] = {
54 0, IDS_SAVE_PAGE_DESC_HTML_ONLY, IDS_SAVE_PAGE_DESC_COMPLETE, 57 0,
58 IDS_SAVE_PAGE_DESC_HTML_ONLY,
59 IDS_SAVE_PAGE_DESC_COMPLETE,
60 IDS_SAVE_PAGE_DESC_COMPLETE_SINGLE_FILE,
55 }; 61 };
56 62
57 } 63 }
58 64
59 SavePackageFilePicker::SavePackageFilePicker( 65 SavePackageFilePicker::SavePackageFilePicker(
60 content::WebContents* web_contents, 66 content::WebContents* web_contents,
61 const FilePath& suggested_path, 67 const FilePath& suggested_path,
62 const FilePath::StringType& default_extension, 68 const FilePath::StringType& default_extension,
63 bool can_save_as_complete, 69 bool can_save_as_complete,
64 DownloadPrefs* download_prefs, 70 DownloadPrefs* download_prefs,
(...skipping 11 matching lines...) Expand all
76 if (can_save_as_complete) { 82 if (can_save_as_complete) {
77 bool add_extra_extension = false; 83 bool add_extra_extension = false;
78 FilePath::StringType extra_extension; 84 FilePath::StringType extra_extension;
79 if (!suggested_path.Extension().empty() && 85 if (!suggested_path.Extension().empty() &&
80 suggested_path.Extension().compare(FILE_PATH_LITERAL("htm")) && 86 suggested_path.Extension().compare(FILE_PATH_LITERAL("htm")) &&
81 suggested_path.Extension().compare(FILE_PATH_LITERAL("html"))) { 87 suggested_path.Extension().compare(FILE_PATH_LITERAL("html"))) {
82 add_extra_extension = true; 88 add_extra_extension = true;
83 extra_extension = suggested_path.Extension().substr(1); 89 extra_extension = suggested_path.Extension().substr(1);
84 } 90 }
85 91
86 file_type_info.extensions.resize(2); 92 file_type_info.extensions.resize(3);
Randy Smith (Not in Mondays) 2012/04/16 17:45:47 nit, suggestion (i.e. up to you), while you're her
benjhayden 2012/04/16 19:08:06 Done.
93
Randy Smith (Not in Mondays) 2012/04/16 17:45:47 nit, while you're here: Could you put in a comment
benjhayden 2012/04/16 19:08:06 Done.
94 file_type_info.extension_description_overrides.push_back(
95 l10n_util::GetStringUTF16(kIndexToIDS[kSelectFileHtmlOnlyIndex]));
Randy Smith (Not in Mondays) 2012/04/16 17:45:47 nit, suggestion, while you're here: We're dealing
benjhayden 2012/04/16 19:08:06 Done.
87 file_type_info.extensions[kSelectFileHtmlOnlyIndex - 1].push_back( 96 file_type_info.extensions[kSelectFileHtmlOnlyIndex - 1].push_back(
88 FILE_PATH_LITERAL("htm")); 97 FILE_PATH_LITERAL("htm"));
89 file_type_info.extensions[kSelectFileHtmlOnlyIndex - 1].push_back( 98 file_type_info.extensions[kSelectFileHtmlOnlyIndex - 1].push_back(
90 FILE_PATH_LITERAL("html")); 99 FILE_PATH_LITERAL("html"));
91
92 if (add_extra_extension) { 100 if (add_extra_extension) {
93 file_type_info.extensions[kSelectFileHtmlOnlyIndex - 1].push_back( 101 file_type_info.extensions[kSelectFileHtmlOnlyIndex - 1].push_back(
94 extra_extension); 102 extra_extension);
95 } 103 }
96 104
97 file_type_info.extension_description_overrides.push_back( 105 file_type_info.extension_description_overrides.push_back(
98 l10n_util::GetStringUTF16(kIndexToIDS[kSelectFileCompleteIndex - 1])); 106 l10n_util::GetStringUTF16(kIndexToIDS[kSelectFileCompleteIndex]));
99 file_type_info.extensions[kSelectFileCompleteIndex - 1].push_back( 107 file_type_info.extensions[kSelectFileCompleteIndex - 1].push_back(
100 FILE_PATH_LITERAL("htm")); 108 FILE_PATH_LITERAL("htm"));
101 file_type_info.extensions[kSelectFileCompleteIndex - 1].push_back( 109 file_type_info.extensions[kSelectFileCompleteIndex - 1].push_back(
102 FILE_PATH_LITERAL("html")); 110 FILE_PATH_LITERAL("html"));
103
104 if (add_extra_extension) { 111 if (add_extra_extension) {
105 file_type_info.extensions[kSelectFileCompleteIndex - 1].push_back( 112 file_type_info.extensions[kSelectFileCompleteIndex - 1].push_back(
106 extra_extension); 113 extra_extension);
107 } 114 }
108 115
109 file_type_info.extension_description_overrides.push_back( 116 file_type_info.extension_description_overrides.push_back(
110 l10n_util::GetStringUTF16(kIndexToIDS[kSelectFileCompleteIndex])); 117 l10n_util::GetStringUTF16(kIndexToIDS[
118 kSelectFileCompleteSingleFileIndex]));
119 file_type_info.extensions[kSelectFileCompleteSingleFileIndex - 1].push_back(
120 FILE_PATH_LITERAL("mht"));
121 file_type_info.extensions[kSelectFileCompleteSingleFileIndex - 1].push_back(
122 FILE_PATH_LITERAL("mhtml"));
123 if (add_extra_extension) {
124 file_type_info.extensions[
125 kSelectFileCompleteSingleFileIndex - 1].push_back(extra_extension);
126 }
127
111 file_type_info.include_all_files = false; 128 file_type_info.include_all_files = false;
112 } else { 129 } else {
113 file_type_info.extensions.resize(1); 130 file_type_info.extensions.resize(1);
114 file_type_info.extensions[kSelectFileHtmlOnlyIndex - 1].push_back( 131 file_type_info.extensions[kSelectFileHtmlOnlyIndex - 1].push_back(
115 suggested_path.Extension()); 132 suggested_path.Extension());
116 133
117 if (!file_type_info.extensions[kSelectFileHtmlOnlyIndex - 1][0].empty()) { 134 if (!file_type_info.extensions[kSelectFileHtmlOnlyIndex - 1][0].empty()) {
118 // Drop the . 135 // Drop the .
119 file_type_info.extensions[kSelectFileHtmlOnlyIndex - 1][0].erase(0, 1); 136 file_type_info.extensions[kSelectFileHtmlOnlyIndex - 1][0].erase(0, 1);
120 } 137 }
(...skipping 25 matching lines...) Expand all
146 163
147 void SavePackageFilePicker::SetShouldPromptUser(bool should_prompt) { 164 void SavePackageFilePicker::SetShouldPromptUser(bool should_prompt) {
148 g_should_prompt_for_filename = should_prompt; 165 g_should_prompt_for_filename = should_prompt;
149 } 166 }
150 167
151 void SavePackageFilePicker::FileSelected(const FilePath& path, 168 void SavePackageFilePicker::FileSelected(const FilePath& path,
152 int index, 169 int index,
153 void* params) { 170 void* params) {
154 // The option index is not zero-based. 171 // The option index is not zero-based.
155 DCHECK(index >= kSelectFileHtmlOnlyIndex && 172 DCHECK(index >= kSelectFileHtmlOnlyIndex &&
156 index <= kSelectFileCompleteIndex); 173 index <= kSelectFileCompleteSingleFileIndex);
157 174
158 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id_); 175 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id_);
159 if (process) { 176 if (process) {
160 SavePageType save_type = kIndexToSaveType[index]; 177 SavePageType save_type = kIndexToSaveType[index];
178 UMA_HISTOGRAM_ENUMERATION(
179 "Download.SavePageType", save_type, content::SAVE_PAGE_TYPE_MAX);
161 Profile* profile = 180 Profile* profile =
162 Profile::FromBrowserContext(process->GetBrowserContext()); 181 Profile::FromBrowserContext(process->GetBrowserContext());
163 PrefService* prefs = profile->GetPrefs(); 182 PrefService* prefs = profile->GetPrefs();
164 if (select_file_dialog_ && 183 if (select_file_dialog_ &&
165 select_file_dialog_->HasMultipleFileTypeChoices()) 184 select_file_dialog_->HasMultipleFileTypeChoices())
166 prefs->SetInteger(prefs::kSaveFileType, save_type); 185 prefs->SetInteger(prefs::kSaveFileType, save_type);
167 186
168 StringPrefMember save_file_path; 187 StringPrefMember save_file_path;
169 save_file_path.Init(prefs::kSaveFileDefaultDirectory, prefs, NULL); 188 save_file_path.Init(prefs::kSaveFileDefaultDirectory, prefs, NULL);
170 #if defined(OS_POSIX) 189 #if defined(OS_POSIX)
(...skipping 10 matching lines...) Expand all
181 200
182 callback_.Run(path, save_type); 201 callback_.Run(path, save_type);
183 } 202 }
184 203
185 delete this; 204 delete this;
186 } 205 }
187 206
188 void SavePackageFilePicker::FileSelectionCanceled(void* params) { 207 void SavePackageFilePicker::FileSelectionCanceled(void* params) {
189 delete this; 208 delete this;
190 } 209 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/download/save_page_browsertest.cc » ('j') | chrome/browser/download/save_page_browsertest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698