| 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" |
| 19 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 20 #include "content/common/content_export.h" | 21 #include "content/common/content_export.h" |
| 21 #include "content/public/browser/download_item.h" | 22 #include "content/public/browser/download_item.h" |
| 22 #include "content/public/browser/download_manager_delegate.h" | 23 #include "content/public/browser/download_manager_delegate.h" |
| 23 #include "content/public/browser/save_page_type.h" | 24 #include "content/public/browser/save_page_type.h" |
| 24 #include "content/public/browser/web_contents_observer.h" | 25 #include "content/public/browser/web_contents_observer.h" |
| 25 #include "content/public/common/referrer.h" | 26 #include "content/public/common/referrer.h" |
| 26 #include "net/base/net_errors.h" | 27 #include "net/base/net_errors.h" |
| 27 #include "url/gurl.h" | 28 #include "url/gurl.h" |
| 28 | 29 |
| 29 class GURL; | 30 class GURL; |
| 30 | 31 |
| 31 namespace content { | 32 namespace content { |
| 32 class DownloadItemImpl; | 33 class DownloadItemImpl; |
| 33 class DownloadManagerImpl; | 34 class DownloadManagerImpl; |
| 34 class WebContents; | 35 class RenderFrameHostImpl; |
| 35 class SaveFileManager; | 36 class SaveFileManager; |
| 36 class SaveItem; | 37 class SaveItem; |
| 37 class SavePackage; | 38 class SavePackage; |
| 38 struct SaveFileCreateInfo; | 39 struct SaveFileCreateInfo; |
| 40 class WebContents; |
| 39 | 41 |
| 40 // The SavePackage object manages the process of saving a page as only-html or | 42 // The SavePackage object manages the process of saving a page as only-html or |
| 41 // complete-html or MHTML and providing the information for displaying saving | 43 // complete-html or MHTML and providing the information for displaying saving |
| 42 // status. Saving page as only-html means means that we save web page to a | 44 // status. Saving page as only-html means means that we save web page to a |
| 43 // single HTML file regardless internal sub resources and sub frames. Saving | 45 // single HTML file regardless internal sub resources and sub frames. Saving |
| 44 // page as complete-html page means we save not only the main html file the user | 46 // 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 | 47 // 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 | 48 // 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 | 49 // 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. | 50 // contain the html and all auxiliary files in a single text file. |
| 49 // | 51 // |
| 50 // Each page saving job may include one or multiple files which need to be | 52 // 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 | 53 // 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 | 54 // 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. | 55 // saving job, and exist for the duration of one contents's life time. |
| 56 // SaveItems go through waiting_item_queue_, in_progress_items_ and finally |
| 57 // end up either in saved_success_items_ or saved_failed_items_. |
| 54 class CONTENT_EXPORT SavePackage | 58 class CONTENT_EXPORT SavePackage |
| 55 : public base::RefCountedThreadSafe<SavePackage>, | 59 : public base::RefCountedThreadSafe<SavePackage>, |
| 56 public WebContentsObserver, | 60 public WebContentsObserver, |
| 57 public DownloadItem::Observer, | 61 public DownloadItem::Observer, |
| 58 public base::SupportsWeakPtr<SavePackage> { | 62 public base::SupportsWeakPtr<SavePackage> { |
| 59 public: | 63 public: |
| 60 enum WaitState { | 64 enum WaitState { |
| 61 // State when created but not initialized. | 65 // State when created but not initialized. |
| 62 INITIALIZE = 0, | 66 INITIALIZE = 0, |
| 63 // State when after initializing, but not yet saving. | 67 // State when after initializing, but not yet saving. |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 bool GenerateFileName(const std::string& disposition, | 178 bool GenerateFileName(const std::string& disposition, |
| 175 const GURL& url, | 179 const GURL& url, |
| 176 bool need_html_ext, | 180 bool need_html_ext, |
| 177 base::FilePath::StringType* generated_name); | 181 base::FilePath::StringType* generated_name); |
| 178 | 182 |
| 179 // Set of methods to get all savable resource links from current web page, | 183 // Set of methods to get all savable resource links from current web page, |
| 180 // including main frame and sub-frames. | 184 // including main frame and sub-frames. |
| 181 void GetSavableResourceLinks(); | 185 void GetSavableResourceLinks(); |
| 182 void GetSavableResourceLinksForFrame(RenderFrameHost* target); | 186 void GetSavableResourceLinksForFrame(RenderFrameHost* target); |
| 183 void OnSavableResourceLinksResponse( | 187 void OnSavableResourceLinksResponse( |
| 184 RenderFrameHost* sender, | 188 RenderFrameHostImpl* sender, |
| 185 const GURL& frame_url, | 189 const GURL& frame_url, |
| 186 const std::vector<GURL>& resources_list, | 190 const std::vector<GURL>& resources_list, |
| 187 const std::vector<Referrer>& referrers_list); | 191 const std::vector<Referrer>& referrers_list); |
| 188 void OnSavableResourceLinksError(RenderFrameHost* sender); | 192 void OnSavableResourceLinksError(RenderFrameHostImpl* sender); |
| 189 void CompleteSavableResourceLinksResponse(); | 193 void CompleteSavableResourceLinksResponse(); |
| 190 | 194 |
| 191 // For each frame in the current page, ask the renderer process associated | 195 // For each frame in the current page, ask the renderer process associated |
| 192 // with that frame to serialize that frame into html. | 196 // with that frame to serialize that frame into html. |
| 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(RenderFrameHostImpl* 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 OnSerializedLocalPathForChildFrame(RenderFrameHostImpl* 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(RenderFrameHostImpl* 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(RenderFrameHostImpl* sender); |
| 223 |
| 224 // Helper used by OnSerializedLocalPathForChildFrame 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(RenderFrameHostImpl* 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 |