OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #ifndef CHROME_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ | 5 #ifndef CHROME_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ |
6 #define CHROME_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ | 6 #define CHROME_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ |
7 | 7 |
| 8 #include <hash_map> |
| 9 #include <queue> |
8 #include <string> | 10 #include <string> |
| 11 #include <utility> |
9 #include <vector> | 12 #include <vector> |
10 #include <queue> | |
11 #include <utility> | |
12 | 13 |
13 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 15 #include "base/file_path.h" |
14 #include "base/hash_tables.h" | 16 #include "base/hash_tables.h" |
15 #include "base/ref_counted.h" | 17 #include "base/ref_counted.h" |
16 #include "base/time.h" | 18 #include "base/time.h" |
17 #include "chrome/common/pref_member.h" | 19 #include "chrome/common/pref_member.h" |
18 #include "chrome/browser/download/save_item.h" | 20 #include "chrome/browser/download/save_item.h" |
19 #include "chrome/browser/download/save_types.h" | 21 #include "chrome/browser/download/save_types.h" |
20 #include "chrome/browser/renderer_host/render_view_host_delegate.h" | 22 #include "chrome/browser/renderer_host/render_view_host_delegate.h" |
21 | 23 |
22 class SaveFileManager; | 24 class SaveFileManager; |
23 class SavePackage; | 25 class SavePackage; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 // Waiting for html DOM data sent from render process. | 72 // Waiting for html DOM data sent from render process. |
71 HTML_DATA, | 73 HTML_DATA, |
72 // Saving page finished successfully. | 74 // Saving page finished successfully. |
73 SUCCESSFUL, | 75 SUCCESSFUL, |
74 // Failed to save page. | 76 // Failed to save page. |
75 FAILED | 77 FAILED |
76 }; | 78 }; |
77 | 79 |
78 SavePackage(WebContents* web_content, | 80 SavePackage(WebContents* web_content, |
79 SavePackageType save_type, | 81 SavePackageType save_type, |
80 const std::wstring& file_full_path, | 82 const FilePath& file_full_path, |
81 const std::wstring& directory_full_path); | 83 const FilePath& directory_full_path); |
82 | 84 |
83 ~SavePackage(); | 85 ~SavePackage(); |
84 | 86 |
85 // Initialize the SavePackage. Returns true if it initializes properly. | 87 // Initialize the SavePackage. Returns true if it initializes properly. |
86 // Need to make sure that this method must be called in the UI thread because | 88 // Need to make sure that this method must be called in the UI thread because |
87 // using g_browser_process on a non-UI thread can cause crashes during | 89 // using g_browser_process on a non-UI thread can cause crashes during |
88 // shutdown. | 90 // shutdown. |
89 bool Init(); | 91 bool Init(); |
90 | 92 |
91 void Cancel(bool user_action); | 93 void Cancel(bool user_action); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 | 136 |
135 // Statics ------------------------------------------------------------------- | 137 // Statics ------------------------------------------------------------------- |
136 | 138 |
137 // Used to disable prompting the user for a directory/filename of the saved | 139 // Used to disable prompting the user for a directory/filename of the saved |
138 // web page. This is available for testing. | 140 // web page. This is available for testing. |
139 static void SetShouldPromptUser(bool should_prompt); | 141 static void SetShouldPromptUser(bool should_prompt); |
140 | 142 |
141 // Helper function for preparing suggested name for the SaveAs Dialog. The | 143 // Helper function for preparing suggested name for the SaveAs Dialog. The |
142 // suggested name is composed of the default save path and the web document's | 144 // suggested name is composed of the default save path and the web document's |
143 // title. | 145 // title. |
144 static std::wstring GetSuggestNameForSaveAs(PrefService* prefs, | 146 static FilePath GetSuggestNameForSaveAs(PrefService* prefs, |
145 const std::wstring& name); | 147 const FilePath& name); |
146 | 148 |
147 // This structure is for storing parameters which we will use to create | 149 // This structure is for storing parameters which we will use to create |
148 // a SavePackage object later. | 150 // a SavePackage object later. |
149 struct SavePackageParam { | 151 struct SavePackageParam { |
150 // MIME type of current tab contents. | 152 // MIME type of current tab contents. |
151 const std::string& current_tab_mime_type; | 153 const std::string& current_tab_mime_type; |
152 // Pointer to preference service. | 154 // Pointer to preference service. |
153 PrefService* prefs; | 155 PrefService* prefs; |
154 // Type about saving page as only-html or complete-html. | 156 // Type about saving page as only-html or complete-html. |
155 SavePackageType save_type; | 157 SavePackageType save_type; |
156 // File path for main html file. | 158 // File path for main html file. |
157 std::wstring saved_main_file_path; | 159 FilePath saved_main_file_path; |
158 // Directory path for saving sub resources and sub html frames. | 160 // Directory path for saving sub resources and sub html frames. |
159 std::wstring dir; | 161 FilePath dir; |
160 | 162 |
161 SavePackageParam(const std::string& mime_type) | 163 SavePackageParam(const std::string& mime_type) |
162 : current_tab_mime_type(mime_type) { } | 164 : current_tab_mime_type(mime_type) { } |
163 }; | 165 }; |
164 static bool GetSaveInfo(const std::wstring& suggest_name, | 166 static bool GetSaveInfo(const FilePath& suggest_name, |
165 HWND container_hwnd, | 167 HWND container_hwnd, |
166 SavePackageParam* param, | 168 SavePackageParam* param, |
167 DownloadManager* download_manager); | 169 DownloadManager* download_manager); |
168 | 170 |
169 // File name is consist of pure file name, dot and file extension name. File | |
170 // name might has no dot and file extension, or has multiple dot inside file | |
171 // name. The dot, which separates the pure file name and file extension name, | |
172 // is last dot in the file name. If the file name matches following patterns: | |
173 // base_file_name(ordinal_number) or base_file_name(ordinal_number).extension, | |
174 // this function will return true and get the base file name part and | |
175 // ordinal_number part via output parameters. The |file_ordinal_number| could | |
176 // be empty if there is no content in ordinal_number part. If the file name | |
177 // does not match the pattern or the ordinal_number part has non-digit | |
178 // content, just return false. | |
179 static bool GetBaseFileNameAndFileOrdinalNumber( | |
180 const std::wstring& file_name, | |
181 std::wstring* base_file_name, | |
182 std::wstring* file_ordinal_number); | |
183 | |
184 // Check whether we can do the saving page operation for the specified URL. | 171 // Check whether we can do the saving page operation for the specified URL. |
185 static bool IsSavableURL(const GURL& url); | 172 static bool IsSavableURL(const GURL& url); |
186 | 173 |
187 // Check whether we can do the saving page operation for the contents which | 174 // Check whether we can do the saving page operation for the contents which |
188 // have the specified MIME type. | 175 // have the specified MIME type. |
189 static bool IsSavableContents(const std::string& contents_mime_type); | 176 static bool IsSavableContents(const std::string& contents_mime_type); |
190 | 177 |
191 // Check whether we can save page as complete-HTML for the contents which | 178 // Check whether we can save page as complete-HTML for the contents which |
192 // have specified a MIME type. Now only contents which have the MIME type | 179 // have specified a MIME type. Now only contents which have the MIME type |
193 // "text/html" can be saved as complete-HTML. | 180 // "text/html" can be saved as complete-HTML. |
194 static bool CanSaveAsComplete(const std::string& contents_mime_type); | 181 static bool CanSaveAsComplete(const std::string& contents_mime_type); |
195 | 182 |
196 // File name is considered being consist of pure file name, dot and file | 183 // File name is considered being consist of pure file name, dot and file |
197 // extension name. File name might has no dot and file extension, or has | 184 // extension name. File name might has no dot and file extension, or has |
198 // multiple dot inside file name. The dot, which separates the pure file | 185 // multiple dot inside file name. The dot, which separates the pure file |
199 // name and file extension name, is last dot in the whole file name. | 186 // name and file extension name, is last dot in the whole file name. |
200 // This function is for making sure the length of specified file path is not | 187 // This function is for making sure the length of specified file path is not |
201 // great than the specified maximum length of file path and getting safe pure | 188 // great than the specified maximum length of file path and getting safe pure |
202 // file name part if the input pure file name is too long. | 189 // file name part if the input pure file name is too long. |
203 // The parameter |dir_path| specifies directory part of the specified | 190 // The parameter |dir_path| specifies directory part of the specified |
204 // file path. The parameter |file_name_ext| specifies file extension | 191 // file path. The parameter |file_name_ext| specifies file extension |
205 // name part of the specified file path (including start dot). The parameter | 192 // name part of the specified file path (including start dot). The parameter |
206 // |max_file_path_len| specifies maximum length of the specified file path. | 193 // |max_file_path_len| specifies maximum length of the specified file path. |
207 // The parameter |pure_file_name| input pure file name part of the specified | 194 // The parameter |pure_file_name| input pure file name part of the specified |
208 // file path. If the length of specified file path is great than | 195 // file path. If the length of specified file path is great than |
209 // |max_file_path_len|, the |pure_file_name| will output new pure file name | 196 // |max_file_path_len|, the |pure_file_name| will output new pure file name |
210 // part for making sure the length of specified file path is less than | 197 // part for making sure the length of specified file path is less than |
211 // specified maximum length of file path. Return false if the function can | 198 // specified maximum length of file path. Return false if the function can |
212 // not get a safe pure file name, otherwise it returns true. | 199 // not get a safe pure file name, otherwise it returns true. |
213 static bool GetSafePureFileName(const std::wstring& dir_path, | 200 static bool GetSafePureFileName(const FilePath& dir_path, |
214 const std::wstring& file_name_ext, | 201 const FilePath::StringType& file_name_ext, |
215 uint32 max_file_path_len, | 202 uint32 max_file_path_len, |
216 std::wstring* pure_file_name); | 203 FilePath::StringType* pure_file_name); |
217 | 204 |
218 private: | 205 private: |
219 // For testing. | 206 // For testing only. |
220 friend class SavePackageTest; | 207 SavePackage(const FilePath::CharType* file_full_path, |
221 SavePackage(const wchar_t* file_full_path, | 208 const FilePath::CharType* directory_full_path); |
222 const wchar_t* directory_full_path); | |
223 | 209 |
224 void Stop(); | 210 void Stop(); |
225 void CheckFinish(); | 211 void CheckFinish(); |
226 void SaveNextFile(bool process_all_remainder_items); | 212 void SaveNextFile(bool process_all_remainder_items); |
227 void DoSavingProcess(); | 213 void DoSavingProcess(); |
228 | 214 |
229 // Create a file name based on the response from the server. | 215 // Create a file name based on the response from the server. |
230 bool GenerateFilename(const std::string& disposition, | 216 bool GenerateFilename(const std::string& disposition, |
231 const std::wstring& url, | 217 const GURL& url, |
232 bool need_html_ext, | 218 bool need_html_ext, |
233 std::wstring* generated_name); | 219 FilePath::StringType* generated_name); |
234 | 220 |
235 // Get all savable resource links from current web page, include main | 221 // Get all savable resource links from current web page, include main |
236 // frame and sub-frame. | 222 // frame and sub-frame. |
237 void GetAllSavableResourceLinksForCurrentPage(); | 223 void GetAllSavableResourceLinksForCurrentPage(); |
238 // Get html data by serializing all frames of current page with lists | 224 // Get html data by serializing all frames of current page with lists |
239 // which contain all resource links that have local copy. | 225 // which contain all resource links that have local copy. |
240 void GetSerializedHtmlDataForCurrentPageWithLocalLinks(); | 226 void GetSerializedHtmlDataForCurrentPageWithLocalLinks(); |
241 | 227 |
242 SaveItem* LookupItemInProcessBySaveId(int32 save_id); | 228 SaveItem* LookupItemInProcessBySaveId(int32 save_id); |
243 void PutInProgressItemToSavedMap(SaveItem* save_item); | 229 void PutInProgressItemToSavedMap(SaveItem* save_item); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 SaveFileManager* file_manager_; | 262 SaveFileManager* file_manager_; |
277 | 263 |
278 WebContents* web_contents_; | 264 WebContents* web_contents_; |
279 | 265 |
280 // We use a fake DownloadItem here in order to reuse the DownloadItemView. | 266 // We use a fake DownloadItem here in order to reuse the DownloadItemView. |
281 // This class owns the pointer. | 267 // This class owns the pointer. |
282 DownloadItem* download_; | 268 DownloadItem* download_; |
283 | 269 |
284 // The URL of the page the user wants to save. | 270 // The URL of the page the user wants to save. |
285 std::wstring page_url_; | 271 std::wstring page_url_; |
286 std::wstring saved_main_file_path_; | 272 FilePath saved_main_file_path_; |
287 std::wstring saved_main_directory_path_; | 273 FilePath saved_main_directory_path_; |
288 | 274 |
289 // Indicates whether the actual saving job is finishing or not. | 275 // Indicates whether the actual saving job is finishing or not. |
290 bool finished_; | 276 bool finished_; |
291 | 277 |
292 // Indicates whether user canceled the saving job. | 278 // Indicates whether user canceled the saving job. |
293 bool user_canceled_; | 279 bool user_canceled_; |
294 | 280 |
295 // Indicates whether user get disk error. | 281 // Indicates whether user get disk error. |
296 bool disk_error_occurred_; | 282 bool disk_error_occurred_; |
297 | 283 |
298 // Type about saving page as only-html or complete-html. | 284 // Type about saving page as only-html or complete-html. |
299 SavePackageType save_type_; | 285 SavePackageType save_type_; |
300 | 286 |
301 // Number of all need to be saved resources. | 287 // Number of all need to be saved resources. |
302 int all_save_items_count_; | 288 int all_save_items_count_; |
303 | 289 |
304 typedef base::hash_set<std::wstring> FileNameSet; | 290 typedef base::hash_set<std::wstring> FileNameSet; |
305 // This set is used to eliminate duplicated file names in saving directory. | 291 // This set is used to eliminate duplicated file names in saving directory. |
306 FileNameSet file_name_set_; | 292 FileNameSet file_name_set_; |
307 | 293 |
308 typedef base::hash_map<std::wstring, uint32> FileNameCountMap; | 294 typedef base::hash_map<FilePath::StringType, uint32> FileNameCountMap; |
309 // This map is used to track serial number for specified filename. | 295 // This map is used to track serial number for specified filename. |
310 FileNameCountMap file_name_count_map_; | 296 FileNameCountMap file_name_count_map_; |
311 | 297 |
312 // Indicates current waiting state when SavePackage try to get something | 298 // Indicates current waiting state when SavePackage try to get something |
313 // from outside. | 299 // from outside. |
314 WaitState wait_state_; | 300 WaitState wait_state_; |
315 | 301 |
316 // Unique id for this SavePackage. | 302 // Unique id for this SavePackage. |
317 const int tab_id_; | 303 const int tab_id_; |
318 | 304 |
| 305 friend class SavePackageTest; |
319 DISALLOW_COPY_AND_ASSIGN(SavePackage); | 306 DISALLOW_COPY_AND_ASSIGN(SavePackage); |
320 }; | 307 }; |
321 | 308 |
322 #endif // CHROME_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ | 309 #endif // CHROME_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ |
| 310 |
OLD | NEW |