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 #ifndef CONTENT_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ | 5 #ifndef CONTENT_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ |
6 #define CONTENT_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ | 6 #define CONTENT_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ |
7 | 7 |
8 #include <queue> | 8 #include <queue> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 // Waiting for data sent from net IO or from file system. | 66 // Waiting for data sent from net IO or from file system. |
67 NET_FILES, | 67 NET_FILES, |
68 // Waiting for html DOM data sent from render process. | 68 // Waiting for html DOM data sent from render process. |
69 HTML_DATA, | 69 HTML_DATA, |
70 // Saving page finished successfully. | 70 // Saving page finished successfully. |
71 SUCCESSFUL, | 71 SUCCESSFUL, |
72 // Failed to save page. | 72 // Failed to save page. |
73 FAILED | 73 FAILED |
74 }; | 74 }; |
75 | 75 |
76 static const FilePath::CharType kDefaultHtmlExtension[]; | 76 static const base::FilePath::CharType kDefaultHtmlExtension[]; |
77 | 77 |
78 // Constructor for user initiated page saving. This constructor results in a | 78 // Constructor for user initiated page saving. This constructor results in a |
79 // SavePackage that will generate and sanitize a suggested name for the user | 79 // SavePackage that will generate and sanitize a suggested name for the user |
80 // in the "Save As" dialog box. | 80 // in the "Save As" dialog box. |
81 explicit SavePackage(WebContents* web_contents); | 81 explicit SavePackage(WebContents* web_contents); |
82 | 82 |
83 // This contructor is used only for testing. We can bypass the file and | 83 // This contructor is used only for testing. We can bypass the file and |
84 // directory name generation / sanitization by providing well known paths | 84 // directory name generation / sanitization by providing well known paths |
85 // better suited for tests. | 85 // better suited for tests. |
86 SavePackage(WebContents* web_contents, | 86 SavePackage(WebContents* web_contents, |
87 SavePageType save_type, | 87 SavePageType save_type, |
88 const FilePath& file_full_path, | 88 const base::FilePath& file_full_path, |
89 const FilePath& directory_full_path); | 89 const base::FilePath& directory_full_path); |
90 | 90 |
91 // Initialize the SavePackage. Returns true if it initializes properly. Need | 91 // Initialize the SavePackage. Returns true if it initializes properly. Need |
92 // to make sure that this method must be called in the UI thread because using | 92 // to make sure that this method must be called in the UI thread because using |
93 // g_browser_process on a non-UI thread can cause crashes during shutdown. | 93 // g_browser_process on a non-UI thread can cause crashes during shutdown. |
94 // |cb| will be called when the DownloadItem is created, before data is | 94 // |cb| will be called when the DownloadItem is created, before data is |
95 // written to disk. | 95 // written to disk. |
96 bool Init(const SavePackageDownloadCreatedCallback& cb); | 96 bool Init(const SavePackageDownloadCreatedCallback& cb); |
97 | 97 |
98 // Cancel all in progress request, might be called by user or internal error. | 98 // Cancel all in progress request, might be called by user or internal error. |
99 void Cancel(bool user_action); | 99 void Cancel(bool user_action); |
(...skipping 17 matching lines...) Expand all Loading... |
117 int contents_id() const { return contents_id_; } | 117 int contents_id() const { return contents_id_; } |
118 int id() const { return unique_id_; } | 118 int id() const { return unique_id_; } |
119 WebContents* web_contents() const; | 119 WebContents* web_contents() const; |
120 | 120 |
121 void GetSaveInfo(); | 121 void GetSaveInfo(); |
122 | 122 |
123 private: | 123 private: |
124 friend class base::RefCountedThreadSafe<SavePackage>; | 124 friend class base::RefCountedThreadSafe<SavePackage>; |
125 | 125 |
126 // Callback for WebContents::GenerateMHTML(). | 126 // Callback for WebContents::GenerateMHTML(). |
127 void OnMHTMLGenerated(const FilePath& path, int64 size); | 127 void OnMHTMLGenerated(const base::FilePath& path, int64 size); |
128 | 128 |
129 // For testing only. | 129 // For testing only. |
130 SavePackage(WebContents* web_contents, | 130 SavePackage(WebContents* web_contents, |
131 const FilePath& file_full_path, | 131 const base::FilePath& file_full_path, |
132 const FilePath& directory_full_path); | 132 const base::FilePath& directory_full_path); |
133 | 133 |
134 virtual ~SavePackage(); | 134 virtual ~SavePackage(); |
135 | 135 |
136 // Notes from Init() above applies here as well. | 136 // Notes from Init() above applies here as well. |
137 void InternalInit(); | 137 void InternalInit(); |
138 | 138 |
139 void Stop(); | 139 void Stop(); |
140 void CheckFinish(); | 140 void CheckFinish(); |
141 void SaveNextFile(bool process_all_remainder_items); | 141 void SaveNextFile(bool process_all_remainder_items); |
142 void DoSavingProcess(); | 142 void DoSavingProcess(); |
143 | 143 |
144 // WebContentsObserver implementation. | 144 // WebContentsObserver implementation. |
145 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 145 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
146 | 146 |
147 // DownloadItem::Observer implementation. | 147 // DownloadItem::Observer implementation. |
148 virtual void OnDownloadDestroyed(DownloadItem* download) OVERRIDE; | 148 virtual void OnDownloadDestroyed(DownloadItem* download) OVERRIDE; |
149 | 149 |
150 // Update the download history of this item upon completion. | 150 // Update the download history of this item upon completion. |
151 void FinalizeDownloadEntry(); | 151 void FinalizeDownloadEntry(); |
152 | 152 |
153 // Detach from DownloadManager. | 153 // Detach from DownloadManager. |
154 void StopObservation(); | 154 void StopObservation(); |
155 | 155 |
156 // Return max length of a path for a specific base directory. | 156 // Return max length of a path for a specific base directory. |
157 // This is needed on POSIX, which restrict the length of file names in | 157 // This is needed on POSIX, which restrict the length of file names in |
158 // addition to the restriction on the length of path names. | 158 // addition to the restriction on the length of path names. |
159 // |base_dir| is assumed to be a directory name with no trailing slash. | 159 // |base_dir| is assumed to be a directory name with no trailing slash. |
160 static uint32 GetMaxPathLengthForDirectory(const FilePath& base_dir); | 160 static uint32 GetMaxPathLengthForDirectory(const base::FilePath& base_dir); |
161 | 161 |
162 static bool GetSafePureFileName(const FilePath& dir_path, | 162 static bool GetSafePureFileName( |
163 const FilePath::StringType& file_name_ext, | 163 const base::FilePath& dir_path, |
164 uint32 max_file_path_len, | 164 const base::FilePath::StringType& file_name_ext, |
165 FilePath::StringType* pure_file_name); | 165 uint32 max_file_path_len, |
| 166 base::FilePath::StringType* pure_file_name); |
166 | 167 |
167 // Create a file name based on the response from the server. | 168 // Create a file name based on the response from the server. |
168 bool GenerateFileName(const std::string& disposition, | 169 bool GenerateFileName(const std::string& disposition, |
169 const GURL& url, | 170 const GURL& url, |
170 bool need_html_ext, | 171 bool need_html_ext, |
171 FilePath::StringType* generated_name); | 172 base::FilePath::StringType* generated_name); |
172 | 173 |
173 // Get all savable resource links from current web page, include main | 174 // Get all savable resource links from current web page, include main |
174 // frame and sub-frame. | 175 // frame and sub-frame. |
175 void GetAllSavableResourceLinksForCurrentPage(); | 176 void GetAllSavableResourceLinksForCurrentPage(); |
176 // Get html data by serializing all frames of current page with lists | 177 // Get html data by serializing all frames of current page with lists |
177 // which contain all resource links that have local copy. | 178 // which contain all resource links that have local copy. |
178 void GetSerializedHtmlDataForCurrentPageWithLocalLinks(); | 179 void GetSerializedHtmlDataForCurrentPageWithLocalLinks(); |
179 | 180 |
180 // Look up SaveItem by save id from in progress map. | 181 // Look up SaveItem by save id from in progress map. |
181 SaveItem* LookupItemInProcessBySaveId(int32 save_id); | 182 SaveItem* LookupItemInProcessBySaveId(int32 save_id); |
182 | 183 |
183 // Remove SaveItem from in progress map and put it to saved map. | 184 // Remove SaveItem from in progress map and put it to saved map. |
184 void PutInProgressItemToSavedMap(SaveItem* save_item); | 185 void PutInProgressItemToSavedMap(SaveItem* save_item); |
185 | 186 |
186 // Retrieves the URL to be saved from the WebContents. | 187 // Retrieves the URL to be saved from the WebContents. |
187 GURL GetUrlToBeSaved(); | 188 GURL GetUrlToBeSaved(); |
188 | 189 |
189 void CreateDirectoryOnFileThread(const FilePath& website_save_dir, | 190 void CreateDirectoryOnFileThread(const base::FilePath& website_save_dir, |
190 const FilePath& download_save_dir, | 191 const base::FilePath& download_save_dir, |
191 bool skip_dir_check, | 192 bool skip_dir_check, |
192 const std::string& mime_type, | 193 const std::string& mime_type, |
193 const std::string& accept_langs); | 194 const std::string& accept_langs); |
194 void ContinueGetSaveInfo(const FilePath& suggested_path, | 195 void ContinueGetSaveInfo(const base::FilePath& suggested_path, |
195 bool can_save_as_complete); | 196 bool can_save_as_complete); |
196 void OnPathPicked( | 197 void OnPathPicked( |
197 const FilePath& final_name, | 198 const base::FilePath& final_name, |
198 SavePageType type, | 199 SavePageType type, |
199 const SavePackageDownloadCreatedCallback& cb); | 200 const SavePackageDownloadCreatedCallback& cb); |
200 void OnReceivedSavableResourceLinksForCurrentPage( | 201 void OnReceivedSavableResourceLinksForCurrentPage( |
201 const std::vector<GURL>& resources_list, | 202 const std::vector<GURL>& resources_list, |
202 const std::vector<Referrer>& referrers_list, | 203 const std::vector<Referrer>& referrers_list, |
203 const std::vector<GURL>& frames_list); | 204 const std::vector<GURL>& frames_list); |
204 | 205 |
205 void OnReceivedSerializedHtmlData(const GURL& frame_url, | 206 void OnReceivedSerializedHtmlData(const GURL& frame_url, |
206 const std::string& data, | 207 const std::string& data, |
207 int32 status); | 208 int32 status); |
(...skipping 18 matching lines...) Expand all Loading... |
226 | 227 |
227 // The current speed in files per second. This is used to update the | 228 // The current speed in files per second. This is used to update the |
228 // DownloadItem associated to this SavePackage. The files per second is | 229 // DownloadItem associated to this SavePackage. The files per second is |
229 // presented by the DownloadItem to the UI as bytes per second, which is | 230 // presented by the DownloadItem to the UI as bytes per second, which is |
230 // not correct but matches the way the total and received number of files is | 231 // not correct but matches the way the total and received number of files is |
231 // presented as the total and received bytes. | 232 // presented as the total and received bytes. |
232 int64 CurrentSpeed() const; | 233 int64 CurrentSpeed() const; |
233 | 234 |
234 // Helper function for preparing suggested name for the SaveAs Dialog. The | 235 // Helper function for preparing suggested name for the SaveAs Dialog. The |
235 // suggested name is determined by the web document's title. | 236 // suggested name is determined by the web document's title. |
236 FilePath GetSuggestedNameForSaveAs( | 237 base::FilePath GetSuggestedNameForSaveAs( |
237 bool can_save_as_complete, | 238 bool can_save_as_complete, |
238 const std::string& contents_mime_type, | 239 const std::string& contents_mime_type, |
239 const std::string& accept_langs); | 240 const std::string& accept_langs); |
240 | 241 |
241 // Ensures that the file name has a proper extension for HTML by adding ".htm" | 242 // Ensures that the file name has a proper extension for HTML by adding ".htm" |
242 // if necessary. | 243 // if necessary. |
243 static FilePath EnsureHtmlExtension(const FilePath& name); | 244 static base::FilePath EnsureHtmlExtension(const base::FilePath& name); |
244 | 245 |
245 // Ensures that the file name has a proper extension for supported formats | 246 // Ensures that the file name has a proper extension for supported formats |
246 // if necessary. | 247 // if necessary. |
247 static FilePath EnsureMimeExtension(const FilePath& name, | 248 static base::FilePath EnsureMimeExtension(const base::FilePath& name, |
248 const std::string& contents_mime_type); | 249 const std::string& contents_mime_type); |
249 | 250 |
250 // Returns extension for supported MIME types (for example, for "text/plain" | 251 // Returns extension for supported MIME types (for example, for "text/plain" |
251 // it returns "txt"). | 252 // it returns "txt"). |
252 static const FilePath::CharType* ExtensionForMimeType( | 253 static const base::FilePath::CharType* ExtensionForMimeType( |
253 const std::string& contents_mime_type); | 254 const std::string& contents_mime_type); |
254 | 255 |
255 typedef std::queue<SaveItem*> SaveItemQueue; | 256 typedef std::queue<SaveItem*> SaveItemQueue; |
256 // A queue for items we are about to start saving. | 257 // A queue for items we are about to start saving. |
257 SaveItemQueue waiting_item_queue_; | 258 SaveItemQueue waiting_item_queue_; |
258 | 259 |
259 typedef base::hash_map<int32, SaveItem*> SavedItemMap; | 260 typedef base::hash_map<int32, SaveItem*> SavedItemMap; |
260 // saved_success_items_ is map of all saving job which are successfully saved. | 261 // saved_success_items_ is map of all saving job which are successfully saved. |
261 SavedItemMap saved_success_items_; | 262 SavedItemMap saved_success_items_; |
262 | 263 |
263 // Non-owning pointer for handling file writing on the file thread. | 264 // Non-owning pointer for handling file writing on the file thread. |
264 SaveFileManager* file_manager_; | 265 SaveFileManager* file_manager_; |
265 | 266 |
266 // DownloadManager owns the DownloadItem and handles history and UI. | 267 // DownloadManager owns the DownloadItem and handles history and UI. |
267 DownloadManagerImpl* download_manager_; | 268 DownloadManagerImpl* download_manager_; |
268 DownloadItemImpl* download_; | 269 DownloadItemImpl* download_; |
269 | 270 |
270 // The URL of the page the user wants to save. | 271 // The URL of the page the user wants to save. |
271 GURL page_url_; | 272 GURL page_url_; |
272 FilePath saved_main_file_path_; | 273 base::FilePath saved_main_file_path_; |
273 FilePath saved_main_directory_path_; | 274 base::FilePath saved_main_directory_path_; |
274 | 275 |
275 // The title of the page the user wants to save. | 276 // The title of the page the user wants to save. |
276 string16 title_; | 277 string16 title_; |
277 | 278 |
278 // Used to calculate package download speed (in files per second). | 279 // Used to calculate package download speed (in files per second). |
279 base::TimeTicks start_tick_; | 280 base::TimeTicks start_tick_; |
280 | 281 |
281 // Indicates whether the actual saving job is finishing or not. | 282 // Indicates whether the actual saving job is finishing or not. |
282 bool finished_; | 283 bool finished_; |
283 | 284 |
284 // Indicates whether a call to Finish() has been scheduled. | 285 // Indicates whether a call to Finish() has been scheduled. |
285 bool mhtml_finishing_; | 286 bool mhtml_finishing_; |
286 | 287 |
287 // Indicates whether user canceled the saving job. | 288 // Indicates whether user canceled the saving job. |
288 bool user_canceled_; | 289 bool user_canceled_; |
289 | 290 |
290 // Indicates whether user get disk error. | 291 // Indicates whether user get disk error. |
291 bool disk_error_occurred_; | 292 bool disk_error_occurred_; |
292 | 293 |
293 // Type about saving page as only-html or complete-html. | 294 // Type about saving page as only-html or complete-html. |
294 SavePageType save_type_; | 295 SavePageType save_type_; |
295 | 296 |
296 // Number of all need to be saved resources. | 297 // Number of all need to be saved resources. |
297 size_t all_save_items_count_; | 298 size_t all_save_items_count_; |
298 | 299 |
299 typedef std::set<FilePath::StringType, | 300 typedef std::set<base::FilePath::StringType, |
300 bool (*)(const FilePath::StringType&, | 301 bool (*)(const base::FilePath::StringType&, |
301 const FilePath::StringType&)> FileNameSet; | 302 const base::FilePath::StringType&)> FileNameSet; |
302 // This set is used to eliminate duplicated file names in saving directory. | 303 // This set is used to eliminate duplicated file names in saving directory. |
303 FileNameSet file_name_set_; | 304 FileNameSet file_name_set_; |
304 | 305 |
305 typedef base::hash_map<FilePath::StringType, uint32> FileNameCountMap; | 306 typedef base::hash_map<base::FilePath::StringType, uint32> FileNameCountMap; |
306 // This map is used to track serial number for specified filename. | 307 // This map is used to track serial number for specified filename. |
307 FileNameCountMap file_name_count_map_; | 308 FileNameCountMap file_name_count_map_; |
308 | 309 |
309 // Indicates current waiting state when SavePackage try to get something | 310 // Indicates current waiting state when SavePackage try to get something |
310 // from outside. | 311 // from outside. |
311 WaitState wait_state_; | 312 WaitState wait_state_; |
312 | 313 |
313 // Since for one contents, it can only have one SavePackage in same time. | 314 // Since for one contents, it can only have one SavePackage in same time. |
314 // Now we actually use render_process_id as the contents's unique id. | 315 // Now we actually use render_process_id as the contents's unique id. |
315 const int contents_id_; | 316 const int contents_id_; |
316 | 317 |
317 // Unique ID for this SavePackage. | 318 // Unique ID for this SavePackage. |
318 const int unique_id_; | 319 const int unique_id_; |
319 | 320 |
320 // Variables to record errors that happened so we can record them via | 321 // Variables to record errors that happened so we can record them via |
321 // UMA statistics. | 322 // UMA statistics. |
322 bool wrote_to_completed_file_; | 323 bool wrote_to_completed_file_; |
323 bool wrote_to_failed_file_; | 324 bool wrote_to_failed_file_; |
324 | 325 |
325 friend class SavePackageTest; | 326 friend class SavePackageTest; |
326 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestSuggestedSaveNames); | 327 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestSuggestedSaveNames); |
327 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestLongSafePureFilename); | 328 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestLongSafePureFilename); |
328 | 329 |
329 DISALLOW_COPY_AND_ASSIGN(SavePackage); | 330 DISALLOW_COPY_AND_ASSIGN(SavePackage); |
330 }; | 331 }; |
331 | 332 |
332 } // namespace content | 333 } // namespace content |
333 | 334 |
334 #endif // CONTENT_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ | 335 #endif // CONTENT_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ |
OLD | NEW |