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

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: " 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/utf_string_conversions.h" 7 #include "base/utf_string_conversions.h"
8 #include "chrome/browser/download/download_prefs.h" 8 #include "chrome/browser/download/download_prefs.h"
9 #include "chrome/browser/platform_util.h" 9 #include "chrome/browser/platform_util.h"
10 #include "chrome/browser/prefs/pref_member.h" 10 #include "chrome/browser/prefs/pref_member.h"
(...skipping 15 matching lines...) Expand all
26 26
27 // If false, we don't prompt the user as to where to save the file. This 27 // If false, we don't prompt the user as to where to save the file. This
28 // exists only for testing. 28 // exists only for testing.
29 bool g_should_prompt_for_filename = true; 29 bool g_should_prompt_for_filename = true;
30 30
31 // Used for mapping between SavePageType constants and the indexes above. 31 // Used for mapping between SavePageType constants and the indexes above.
32 const SavePageType kIndexToSaveType[] = { 32 const SavePageType kIndexToSaveType[] = {
33 content::SAVE_PAGE_TYPE_UNKNOWN, 33 content::SAVE_PAGE_TYPE_UNKNOWN,
34 content::SAVE_PAGE_TYPE_AS_ONLY_HTML, 34 content::SAVE_PAGE_TYPE_AS_ONLY_HTML,
35 content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML, 35 content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML,
36 content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML_SINGLE_FILE_MHTML,
achuithb 2012/04/12 19:40:02 Just asking here - does this need to be so long? I
benjhayden 2012/04/13 20:44:11 Done.
36 }; 37 };
37 38
38 int SavePackageTypeToIndex(SavePageType type) { 39 int SavePackageTypeToIndex(SavePageType type) {
39 for (size_t i = 0; i < arraysize(kIndexToSaveType); ++i) { 40 for (size_t i = 0; i < arraysize(kIndexToSaveType); ++i) {
40 if (kIndexToSaveType[i] == type) 41 if (kIndexToSaveType[i] == type)
41 return i; 42 return i;
42 } 43 }
43 NOTREACHED(); 44 NOTREACHED();
44 return -1; 45 return -1;
45 } 46 }
46 47
47 // Indexes used for specifying which element in the extensions dropdown 48 // Indexes used for specifying which element in the extensions dropdown
48 // the user chooses when picking a save type. 49 // the user chooses when picking a save type.
49 const int kSelectFileHtmlOnlyIndex = 1; 50 const int kSelectFileHtmlOnlyIndex = 1;
50 const int kSelectFileCompleteIndex = 2; 51 const int kSelectFileCompleteIndex = 2;
52 const int kSelectFileCompleteSingleFileIndex = 3;
51 53
52 // Used for mapping between the IDS_ string identifiers and the indexes above. 54 // Used for mapping between the IDS_ string identifiers and the indexes above.
53 const int kIndexToIDS[] = { 55 const int kIndexToIDS[] = {
54 0, IDS_SAVE_PAGE_DESC_HTML_ONLY, IDS_SAVE_PAGE_DESC_COMPLETE, 56 0,
57 IDS_SAVE_PAGE_DESC_HTML_ONLY,
58 IDS_SAVE_PAGE_DESC_COMPLETE,
59 IDS_SAVE_PAGE_DESC_COMPLETE_SINGLE_FILE,
55 }; 60 };
56 61
57 } 62 }
58 63
59 SavePackageFilePicker::SavePackageFilePicker( 64 SavePackageFilePicker::SavePackageFilePicker(
60 content::WebContents* web_contents, 65 content::WebContents* web_contents,
61 const FilePath& suggested_path, 66 const FilePath& suggested_path,
62 const FilePath::StringType& default_extension, 67 const FilePath::StringType& default_extension,
63 bool can_save_as_complete, 68 bool can_save_as_complete,
64 DownloadPrefs* download_prefs, 69 DownloadPrefs* download_prefs,
65 content::SaveFilePathPickedCallback callback) 70 content::SaveFilePathPickedCallback callback)
66 : render_process_id_(web_contents->GetRenderProcessHost()->GetID()), 71 : render_process_id_(web_contents->GetRenderProcessHost()->GetID()),
67 callback_(callback) { 72 callback_(callback) {
68 int file_type_index = SavePackageTypeToIndex( 73 int file_type_index = SavePackageTypeToIndex(
69 static_cast<SavePageType>(download_prefs->save_file_type())); 74 static_cast<SavePageType>(download_prefs->save_file_type()));
70 DCHECK_NE(-1, file_type_index); 75 DCHECK_NE(-1, file_type_index);
71 76
72 SelectFileDialog::FileTypeInfo file_type_info; 77 SelectFileDialog::FileTypeInfo file_type_info;
73 78
74 // If the contents can not be saved as complete-HTML, do not show the 79 // If the contents can not be saved as complete-HTML, do not show the
75 // file filters. 80 // file filters.
76 if (can_save_as_complete) { 81 if (can_save_as_complete) {
77 bool add_extra_extension = false; 82 bool add_extra_extension = false;
78 FilePath::StringType extra_extension; 83 FilePath::StringType extra_extension;
79 if (!suggested_path.Extension().empty() && 84 if (!suggested_path.Extension().empty() &&
80 suggested_path.Extension().compare(FILE_PATH_LITERAL("htm")) && 85 suggested_path.Extension().compare(FILE_PATH_LITERAL("htm")) &&
81 suggested_path.Extension().compare(FILE_PATH_LITERAL("html"))) { 86 suggested_path.Extension().compare(FILE_PATH_LITERAL("html"))) {
Randy Smith (Not in Mondays) 2012/04/12 18:00:02 I may be being naive, but doesn't the logic as wri
benjhayden 2012/04/13 20:44:11 Sorry, I don't quite understand. I don't think tha
Randy Smith (Not in Mondays) 2012/04/16 17:45:47 I guess the question is whether you're sure that g
benjhayden 2012/04/16 19:08:05 Sorry, you're right, the suggested_path can be .mh
82 add_extra_extension = true; 87 add_extra_extension = true;
83 extra_extension = suggested_path.Extension().substr(1); 88 extra_extension = suggested_path.Extension().substr(1);
84 } 89 }
85 90
86 file_type_info.extensions.resize(2); 91 file_type_info.extensions.resize(3);
92
93 file_type_info.extension_description_overrides.push_back(
94 l10n_util::GetStringUTF16(kIndexToIDS[kSelectFileHtmlOnlyIndex]));
Randy Smith (Not in Mondays) 2012/04/12 18:00:02 Why did you need to add an override here? (To be
benjhayden 2012/04/13 20:44:11 I didn't actually change any functionality here. I
87 file_type_info.extensions[kSelectFileHtmlOnlyIndex - 1].push_back( 95 file_type_info.extensions[kSelectFileHtmlOnlyIndex - 1].push_back(
88 FILE_PATH_LITERAL("htm")); 96 FILE_PATH_LITERAL("htm"));
89 file_type_info.extensions[kSelectFileHtmlOnlyIndex - 1].push_back( 97 file_type_info.extensions[kSelectFileHtmlOnlyIndex - 1].push_back(
90 FILE_PATH_LITERAL("html")); 98 FILE_PATH_LITERAL("html"));
91
92 if (add_extra_extension) { 99 if (add_extra_extension) {
93 file_type_info.extensions[kSelectFileHtmlOnlyIndex - 1].push_back( 100 file_type_info.extensions[kSelectFileHtmlOnlyIndex - 1].push_back(
94 extra_extension); 101 extra_extension);
95 } 102 }
96 103
104
Randy Smith (Not in Mondays) 2012/04/12 18:00:02 nit: Remove extra blank line.
benjhayden 2012/04/13 20:44:11 Done.
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];
161 Profile* profile = 178 Profile* profile =
162 Profile::FromBrowserContext(process->GetBrowserContext()); 179 Profile::FromBrowserContext(process->GetBrowserContext());
163 PrefService* prefs = profile->GetPrefs(); 180 PrefService* prefs = profile->GetPrefs();
164 if (select_file_dialog_ && 181 if (select_file_dialog_ &&
165 select_file_dialog_->HasMultipleFileTypeChoices()) 182 select_file_dialog_->HasMultipleFileTypeChoices())
166 prefs->SetInteger(prefs::kSaveFileType, save_type); 183 prefs->SetInteger(prefs::kSaveFileType, save_type);
(...skipping 14 matching lines...) Expand all
181 198
182 callback_.Run(path, save_type); 199 callback_.Run(path, save_type);
183 } 200 }
184 201
185 delete this; 202 delete this;
186 } 203 }
187 204
188 void SavePackageFilePicker::FileSelectionCanceled(void* params) { 205 void SavePackageFilePicker::FileSelectionCanceled(void* params) {
189 delete this; 206 delete this;
190 } 207 }
OLDNEW
« no previous file with comments | « no previous file | content/browser/download/save_package.h » ('j') | content/browser/download/save_package.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698