Chromium Code Reviews| 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 <deque> |
| 9 #include <map> | |
| 9 #include <set> | 10 #include <set> |
| 10 #include <string> | 11 #include <string> |
| 11 #include <vector> | 12 #include <vector> |
| 12 | 13 |
| 13 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 14 #include "base/containers/hash_tables.h" | 15 #include "base/containers/hash_tables.h" |
| 15 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
| 16 #include "base/gtest_prod_util.h" | 17 #include "base/gtest_prod_util.h" |
| 17 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
| 18 #include "base/memory/weak_ptr.h" | 19 #include "base/memory/weak_ptr.h" |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 44 // page as complete-html page means we save not only the main html file the user | 45 // page as complete-html page means we save not only the main html file the user |
| 45 // told it to save but also a directory for the auxiliary files such as all | 46 // told it to save but also a directory for the auxiliary files such as all |
| 46 // sub-frame html files, image files, css files and js files. Saving page as | 47 // sub-frame html files, image files, css files and js files. Saving page as |
| 47 // MHTML means the same thing as complete-html, but it uses the MHTML format to | 48 // MHTML means the same thing as complete-html, but it uses the MHTML format to |
| 48 // contain the html and all auxiliary files in a single text file. | 49 // contain the html and all auxiliary files in a single text file. |
| 49 // | 50 // |
| 50 // Each page saving job may include one or multiple files which need to be | 51 // Each page saving job may include one or multiple files which need to be |
| 51 // saved. Each file is represented by a SaveItem, and all SaveItems are owned | 52 // saved. Each file is represented by a SaveItem, and all SaveItems are owned |
| 52 // by the SavePackage. SaveItems are created when a user initiates a page | 53 // by the SavePackage. SaveItems are created when a user initiates a page |
| 53 // saving job, and exist for the duration of one contents's life time. | 54 // saving job, and exist for the duration of one contents's life time. |
| 55 // SaveItems go through waiting_item_queue_, in_progress_items_ and finally | |
| 56 // end up either in saved_success_items_ or saved_failed_items_. | |
| 54 class CONTENT_EXPORT SavePackage | 57 class CONTENT_EXPORT SavePackage |
| 55 : public base::RefCountedThreadSafe<SavePackage>, | 58 : public base::RefCountedThreadSafe<SavePackage>, |
| 56 public WebContentsObserver, | 59 public WebContentsObserver, |
| 57 public DownloadItem::Observer, | 60 public DownloadItem::Observer, |
| 58 public base::SupportsWeakPtr<SavePackage> { | 61 public base::SupportsWeakPtr<SavePackage> { |
| 59 public: | 62 public: |
| 60 enum WaitState { | 63 enum WaitState { |
| 61 // State when created but not initialized. | 64 // State when created but not initialized. |
| 62 INITIALIZE = 0, | 65 INITIALIZE = 0, |
| 63 // State when after initializing, but not yet saving. | 66 // State when after initializing, but not yet saving. |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 182 void GetSavableResourceLinksForFrame(RenderFrameHost* target); | 185 void GetSavableResourceLinksForFrame(RenderFrameHost* target); |
| 183 void OnSavableResourceLinksResponse( | 186 void OnSavableResourceLinksResponse( |
| 184 RenderFrameHost* sender, | 187 RenderFrameHost* sender, |
| 185 const GURL& frame_url, | 188 const GURL& frame_url, |
| 186 const std::vector<GURL>& resources_list, | 189 const std::vector<GURL>& resources_list, |
| 187 const std::vector<Referrer>& referrers_list); | 190 const std::vector<Referrer>& referrers_list); |
| 188 void OnSavableResourceLinksError(RenderFrameHost* sender); | 191 void OnSavableResourceLinksError(RenderFrameHost* sender); |
| 189 void CompleteSavableResourceLinksResponse(); | 192 void CompleteSavableResourceLinksResponse(); |
| 190 | 193 |
| 191 // For each frame in the current page, ask the renderer process associated | 194 // For each frame in the current page, ask the renderer process associated |
| 192 // with that frame to serialize that frame into html. | 195 // with that frame to serialize that frame into html (serialize into html with |
| 196 // "blanks" that we can replace with links to local paths). | |
|
ncarter (slow)
2015/10/08 20:57:55
Make this parenthetical clause its own sentence, s
Łukasz Anforowicz
2015/10/09 16:54:08
I guess this comment is confusing (I have a feelin
| |
| 193 void GetSerializedHtmlWithLocalLinks(); | 197 void GetSerializedHtmlWithLocalLinks(); |
| 194 | 198 |
| 195 // Ask renderer process to serialize |target| frame into html data | 199 // Equivalent of GetSerializedHtmlWithLocalLinks that targets a single |
| 196 // with lists which contain all resource links that have a local copy. | 200 // |target| frame. |
| 197 // - The parameter |saved_links| contains original URLs of all saved links | 201 void GetSerializedHtmlWithLocalLinksForFrame(RenderFrameHost* target); |
| 198 // (which may include URLs referred to from the whole page (not just from | |
| 199 // the |target| frame). | |
| 200 // - The parameter |saved_file_paths| contains corresponding local file paths | |
| 201 // of all saved links, which is matched with |saved_links| vector one by | |
| 202 // one. | |
| 203 // - The parameter |relative_dir_name| is relative path of directory which | |
| 204 // contain all saved auxiliary files included all sub frames and resouces. | |
| 205 void GetSerializedHtmlWithLocalLinksForFrame( | |
| 206 const std::vector<GURL>& saved_links, | |
| 207 const std::vector<base::FilePath>& saved_file_paths, | |
| 208 const base::FilePath& relative_dir_name, | |
| 209 RenderFrameHost* target); | |
| 210 | 202 |
| 211 // Routes html data (sent by renderer process in response to | 203 // Response to GetSerializedHtmlWithLocalLinksForFrame above that routes html |
| 212 // GetSerializedHtmlWithLocalLinksForFrame above) to the associated local file | 204 // fragment into the local file associated with |sender|. |
| 213 // (and also keeps track of when all frames have been completed). | 205 void OnSerializedHtmlFragment(RenderFrameHost* sender, |
| 214 void OnSerializedHtmlWithLocalLinksResponse(RenderFrameHost* sender, | 206 const std::string& data); |
| 215 const GURL& frame_url, | 207 |
| 216 const std::string& data, | 208 // Response to GetSerializedHtmlWithLocalLinksForFrame above that replaces |
| 217 int32 status); | 209 // |render_frame_or_proxy_routing_id| with a corresponding local path |
| 210 // and then writes the result to the local file associated with |sender|. | |
| 211 void OnSerializedLocalPathForSubFrame(RenderFrameHost* sender, | |
| 212 int render_frame_or_proxy_routing_id); | |
| 213 | |
| 214 // Response to GetSerializedHtmlWithLocalLinksForFrame above that replaces | |
| 215 // |savable_resource| with a corresponding local path and then writes the | |
| 216 // result to the local file associated with |sender|. | |
| 217 void OnSerializedLocalPathForSavableResource(RenderFrameHost* sender, | |
| 218 const GURL& savable_resource); | |
| 219 | |
| 220 // Response to GetSerializedHtmlWithLocalLinksForFrame above that keeps track | |
| 221 // of how many frames still need to complete their serialization process. | |
| 222 void OnSerializedEndOfFrame(RenderFrameHost* sender); | |
| 223 | |
| 224 // Helper used by OnSerializedLocalPathForSubFrame and | |
| 225 // OnSerializedLocalPathForSavableResource to convert |save_item| into a local | |
| 226 // path (and falling back to |fallback_url| if |save_item| is null). | |
| 227 void SerializeLocalPathForSaveItem(RenderFrameHost* sender, | |
| 228 SaveItem* save_item, | |
| 229 const GURL& fallback_url); | |
| 230 | |
| 231 // Finds in-progress SaveItem by |frame_tree_node_id|. | |
| 232 SaveItem* FindInProgressSaveItemByFrameTreeNodeID(int frame_tree_node_id); | |
| 233 | |
| 234 // Helper to log telemetry entry for writes to already completed or failed | |
| 235 // SaveItems. | |
| 236 void LogWriteToAlreadyCompletedOrFailedSaveItem(SaveItem* save_item); | |
| 218 | 237 |
| 219 // Look up SaveItem by save id from in progress map. | 238 // Look up SaveItem by save id from in progress map. |
| 220 SaveItem* LookupItemInProcessBySaveId(int32 save_id); | 239 SaveItem* LookupItemInProcessBySaveId(int32 save_id); |
| 221 | 240 |
| 222 // Remove SaveItem from in progress map and put it to saved map. | 241 // Remove SaveItem from in progress map and put it to saved map. |
| 223 void PutInProgressItemToSavedMap(SaveItem* save_item); | 242 void PutInProgressItemToSavedMap(SaveItem* save_item); |
| 224 | 243 |
| 225 // Retrieves the URL to be saved from the WebContents. | 244 // Retrieves the URL to be saved from the WebContents. |
| 226 GURL GetUrlToBeSaved(); | 245 GURL GetUrlToBeSaved(); |
| 227 | 246 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 276 // Ensures that the file name has a proper extension for supported formats | 295 // Ensures that the file name has a proper extension for supported formats |
| 277 // if necessary. | 296 // if necessary. |
| 278 static base::FilePath EnsureMimeExtension(const base::FilePath& name, | 297 static base::FilePath EnsureMimeExtension(const base::FilePath& name, |
| 279 const std::string& contents_mime_type); | 298 const std::string& contents_mime_type); |
| 280 | 299 |
| 281 // Returns extension for supported MIME types (for example, for "text/plain" | 300 // Returns extension for supported MIME types (for example, for "text/plain" |
| 282 // it returns "txt"). | 301 // it returns "txt"). |
| 283 static const base::FilePath::CharType* ExtensionForMimeType( | 302 static const base::FilePath::CharType* ExtensionForMimeType( |
| 284 const std::string& contents_mime_type); | 303 const std::string& contents_mime_type); |
| 285 | 304 |
| 286 typedef std::queue<SaveItem*> SaveItemQueue; | 305 typedef std::deque<SaveItem*> SaveItemQueue; |
| 287 // A queue for items we are about to start saving. | 306 // A queue for items we are about to start saving. |
| 288 SaveItemQueue waiting_item_queue_; | 307 SaveItemQueue waiting_item_queue_; |
| 289 | 308 |
| 290 // Used to de-dupe urls that are being gathered into |waiting_item_queue_|. | 309 // Used to |
| 291 std::set<GURL> unique_urls_to_save_; | 310 // 1) de-dupe urls that are being gathered via GetSavableResourceLinks, |
| 311 // 2) find a SaveItem which needs to be updated with frame_tree_node_id or | |
| 312 // referrer, | |
| 313 // 3) find local path for savable resource. | |
| 314 // Note that |url_to_save_item_| does NOT own SaveItems - they remain owned | |
| 315 // by waiting_item_queue_, in_progress_items_, saved_success_items_, etc. | |
| 316 std::map<GURL, SaveItem*> url_to_save_item_; | |
| 292 | 317 |
| 293 // Temporarily stores urls of savable frames, until we can process them. | 318 // Used to find local path for a subframe. |
| 294 std::vector<GURL> frame_urls_to_save_; | 319 // Note that |frame_tree_node_id_to_save_item_| does NOT own SaveItems - they |
| 320 // remain owned by waiting_item_queue_, in_progress_items_, etc. | |
| 321 std::map<int, SaveItem*> frame_tree_node_id_to_save_item_; | |
| 295 | 322 |
| 296 // Number of frames that we still need to get a response from. | 323 // Number of frames that we still need to get a response from. |
| 297 int number_of_frames_pending_response_; | 324 int number_of_frames_pending_response_; |
| 298 | 325 |
| 299 typedef base::hash_map<int32, SaveItem*> SavedItemMap; | 326 typedef base::hash_map<int32, SaveItem*> SavedItemMap; |
| 300 // saved_success_items_ is map of all saving job which are successfully saved. | 327 // saved_success_items_ is map of all saving job which are successfully saved. |
| 301 SavedItemMap saved_success_items_; | 328 SavedItemMap saved_success_items_; |
| 302 | 329 |
| 303 // Non-owning pointer for handling file writing on the file thread. | 330 // Non-owning pointer for handling file writing on the file thread. |
| 304 SaveFileManager* file_manager_; | 331 SaveFileManager* file_manager_; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 366 friend class SavePackageTest; | 393 friend class SavePackageTest; |
| 367 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestSuggestedSaveNames); | 394 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestSuggestedSaveNames); |
| 368 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestLongSafePureFilename); | 395 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestLongSafePureFilename); |
| 369 | 396 |
| 370 DISALLOW_COPY_AND_ASSIGN(SavePackage); | 397 DISALLOW_COPY_AND_ASSIGN(SavePackage); |
| 371 }; | 398 }; |
| 372 | 399 |
| 373 } // namespace content | 400 } // namespace content |
| 374 | 401 |
| 375 #endif // CONTENT_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ | 402 #endif // CONTENT_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ |
| OLD | NEW |