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 <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <deque> | 11 #include <deque> |
| 12 #include <map> | 12 #include <map> |
| 13 #include <set> | 13 #include <set> |
| 14 #include <string> | 14 #include <string> |
| 15 #include <unordered_map> | 15 #include <unordered_map> |
| 16 #include <vector> | 16 #include <vector> |
| 17 | 17 |
| 18 #include "base/files/file_path.h" | 18 #include "base/files/file_path.h" |
| 19 #include "base/gtest_prod_util.h" | 19 #include "base/gtest_prod_util.h" |
| 20 #include "base/macros.h" | 20 #include "base/macros.h" |
| 21 #include "base/memory/ref_counted.h" | |
| 22 #include "base/memory/weak_ptr.h" | 21 #include "base/memory/weak_ptr.h" |
| 23 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| 24 #include "content/browser/download/save_types.h" | 23 #include "content/browser/download/save_types.h" |
| 25 #include "content/common/content_export.h" | 24 #include "content/common/content_export.h" |
| 26 #include "content/public/browser/download_item.h" | 25 #include "content/public/browser/download_item.h" |
| 27 #include "content/public/browser/download_manager_delegate.h" | 26 #include "content/public/browser/download_manager_delegate.h" |
| 28 #include "content/public/browser/save_page_type.h" | 27 #include "content/public/browser/save_page_type.h" |
| 29 #include "content/public/browser/web_contents_observer.h" | 28 #include "content/public/browser/web_contents_observer.h" |
| 30 #include "content/public/common/referrer.h" | 29 #include "content/public/common/referrer.h" |
| 31 #include "net/base/net_errors.h" | 30 #include "net/base/net_errors.h" |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 51 // page as complete-html page means we save not only the main html file the user | 50 // page as complete-html page means we save not only the main html file the user |
| 52 // told it to save but also a directory for the auxiliary files such as all | 51 // told it to save but also a directory for the auxiliary files such as all |
| 53 // sub-frame html files, image files, css files and js files. Saving page as | 52 // sub-frame html files, image files, css files and js files. Saving page as |
| 54 // MHTML means the same thing as complete-html, but it uses the MHTML format to | 53 // MHTML means the same thing as complete-html, but it uses the MHTML format to |
| 55 // contain the html and all auxiliary files in a single text file. | 54 // contain the html and all auxiliary files in a single text file. |
| 56 // | 55 // |
| 57 // Each page saving job may include one or multiple files which need to be | 56 // Each page saving job may include one or multiple files which need to be |
| 58 // saved. Each file is represented by a SaveItem, and all SaveItems are owned | 57 // saved. Each file is represented by a SaveItem, and all SaveItems are owned |
| 59 // by the SavePackage. SaveItems are created when a user initiates a page | 58 // by the SavePackage. SaveItems are created when a user initiates a page |
| 60 // saving job, and exist for the duration of one contents's life time. | 59 // saving job, and exist for the duration of one contents's life time. |
| 61 class CONTENT_EXPORT SavePackage | 60 class CONTENT_EXPORT SavePackage : public WebContentsObserver, |
| 62 : public base::RefCountedThreadSafe<SavePackage>, | 61 public DownloadItem::Observer, |
| 63 public WebContentsObserver, | 62 public base::SupportsWeakPtr<SavePackage> { |
| 64 public DownloadItem::Observer, | |
| 65 public base::SupportsWeakPtr<SavePackage> { | |
| 66 public: | 63 public: |
| 67 enum WaitState { | 64 enum WaitState { |
| 68 // State when created but not initialized. | 65 // State when created but not initialized. |
| 69 INITIALIZE = 0, | 66 INITIALIZE = 0, |
| 70 // State when after initializing, but not yet saving. | 67 // State when after initializing, but not yet saving. |
| 71 START_PROCESS, | 68 START_PROCESS, |
| 72 // Waiting on a list of savable resources from the backend. | 69 // Waiting on a list of savable resources from the backend. |
| 73 RESOURCES_LIST, | 70 RESOURCES_LIST, |
| 74 // Waiting for data sent from net IO or from file system. | 71 // Waiting for data sent from net IO or from file system. |
| 75 NET_FILES, | 72 NET_FILES, |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 89 explicit SavePackage(WebContents* web_contents); | 86 explicit SavePackage(WebContents* web_contents); |
| 90 | 87 |
| 91 // This contructor is used only for testing. We can bypass the file and | 88 // This contructor is used only for testing. We can bypass the file and |
| 92 // directory name generation / sanitization by providing well known paths | 89 // directory name generation / sanitization by providing well known paths |
| 93 // better suited for tests. | 90 // better suited for tests. |
| 94 SavePackage(WebContents* web_contents, | 91 SavePackage(WebContents* web_contents, |
| 95 SavePageType save_type, | 92 SavePageType save_type, |
| 96 const base::FilePath& file_full_path, | 93 const base::FilePath& file_full_path, |
| 97 const base::FilePath& directory_full_path); | 94 const base::FilePath& directory_full_path); |
| 98 | 95 |
| 96 ~SavePackage() override; | |
| 97 | |
| 99 // Initialize the SavePackage. Returns true if it initializes properly. Need | 98 // Initialize the SavePackage. Returns true if it initializes properly. Need |
| 100 // to make sure that this method must be called in the UI thread because using | 99 // to make sure that this method must be called in the UI thread because using |
| 101 // g_browser_process on a non-UI thread can cause crashes during shutdown. | 100 // g_browser_process on a non-UI thread can cause crashes during shutdown. |
| 102 // |cb| will be called when the DownloadItem is created, before data is | 101 // |cb| will be called when the DownloadItem is created, before data is |
| 103 // written to disk. | 102 // written to disk. |
| 104 bool Init(const SavePackageDownloadCreatedCallback& cb); | 103 bool Init(const SavePackageDownloadCreatedCallback& cb); |
| 105 | 104 |
| 106 // Cancel all in progress request, might be called by user or internal error. | 105 // Cancel all in progress request, might be called by user or internal error. |
| 107 void Cancel(bool user_action); | 106 void Cancel(bool user_action); |
| 108 | 107 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 124 | 123 |
| 125 bool canceled() const { return user_canceled_ || disk_error_occurred_; } | 124 bool canceled() const { return user_canceled_ || disk_error_occurred_; } |
| 126 bool finished() const { return finished_; } | 125 bool finished() const { return finished_; } |
| 127 SavePageType save_type() const { return save_type_; } | 126 SavePageType save_type() const { return save_type_; } |
| 128 | 127 |
| 129 SavePackageId id() const { return unique_id_; } | 128 SavePackageId id() const { return unique_id_; } |
| 130 | 129 |
| 131 void GetSaveInfo(); | 130 void GetSaveInfo(); |
| 132 | 131 |
| 133 private: | 132 private: |
| 134 friend class base::RefCountedThreadSafe<SavePackage>; | |
| 135 | |
| 136 void InitWithDownloadItem( | 133 void InitWithDownloadItem( |
| 137 const SavePackageDownloadCreatedCallback& download_created_callback, | 134 const SavePackageDownloadCreatedCallback& download_created_callback, |
| 138 DownloadItemImpl* item); | 135 DownloadItemImpl* item); |
| 139 | 136 |
| 140 // Callback for WebContents::GenerateMHTML(). | 137 // Callback for WebContents::GenerateMHTML(). |
| 141 void OnMHTMLGenerated(int64_t size); | 138 void OnMHTMLGenerated(int64_t size); |
| 142 | 139 |
| 143 // For testing only. | 140 // For testing only. |
| 144 SavePackage(WebContents* web_contents, | 141 SavePackage(WebContents* web_contents, |
| 145 const base::FilePath& file_full_path, | 142 const base::FilePath& file_full_path, |
| 146 const base::FilePath& directory_full_path); | 143 const base::FilePath& directory_full_path); |
| 147 | 144 |
| 148 ~SavePackage() override; | |
| 149 | |
| 150 // Notes from Init() above applies here as well. | 145 // Notes from Init() above applies here as well. |
| 151 void InternalInit(); | 146 void InternalInit(); |
| 152 | 147 |
| 153 void Stop(); | 148 void Stop(); |
| 154 void CheckFinish(); | 149 void CheckFinish(); |
| 155 | 150 |
| 156 // Initiate a saving job of a specific URL. We send the request to | 151 // Initiate a saving job of a specific URL. We send the request to |
| 157 // SaveFileManager, which will dispatch it to different approach according to | 152 // SaveFileManager, which will dispatch it to different approach according to |
| 158 // the save source. |process_all_remaining_items| indicates whether we need to | 153 // the save source. |process_all_remaining_items| indicates whether we need to |
| 159 // save all remaining items. | 154 // save all remaining items. |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 270 | 265 |
| 271 // Retrieves the URL to be saved from the WebContents. | 266 // Retrieves the URL to be saved from the WebContents. |
| 272 GURL GetUrlToBeSaved(); | 267 GURL GetUrlToBeSaved(); |
| 273 | 268 |
| 274 void CreateDirectoryOnFileThread(const base::FilePath& website_save_dir, | 269 void CreateDirectoryOnFileThread(const base::FilePath& website_save_dir, |
| 275 const base::FilePath& download_save_dir, | 270 const base::FilePath& download_save_dir, |
| 276 bool skip_dir_check, | 271 bool skip_dir_check, |
| 277 const std::string& mime_type); | 272 const std::string& mime_type); |
| 278 void ContinueGetSaveInfo(const base::FilePath& suggested_path, | 273 void ContinueGetSaveInfo(const base::FilePath& suggested_path, |
| 279 bool can_save_as_complete); | 274 bool can_save_as_complete); |
| 280 void OnPathPicked( | 275 void OnPathPicked(const base::FilePath& final_name, |
| 281 const base::FilePath& final_name, | 276 SavePageType type, |
|
asanka
2016/06/06 18:55:09
There's a bunch of unrelated formatting changes in
| |
| 282 SavePageType type, | 277 const SavePackageDownloadCreatedCallback& cb); |
| 283 const SavePackageDownloadCreatedCallback& cb); | |
| 284 | 278 |
| 285 // Map from SaveItem::id() (aka save_item_id) into a SaveItem. | 279 // Map from SaveItem::id() (aka save_item_id) into a SaveItem. |
| 286 using SaveItemIdMap = | 280 using SaveItemIdMap = |
| 287 std::unordered_map<SaveItemId, SaveItem*, SaveItemId::Hasher>; | 281 std::unordered_map<SaveItemId, SaveItem*, SaveItemId::Hasher>; |
| 288 // Map of all saving job in in-progress state. | 282 // Map of all saving job in in-progress state. |
| 289 SaveItemIdMap in_progress_items_; | 283 SaveItemIdMap in_progress_items_; |
| 290 // Map of all saving job which are failed. | 284 // Map of all saving job which are failed. |
| 291 SaveItemIdMap saved_failed_items_; | 285 SaveItemIdMap saved_failed_items_; |
| 292 | 286 |
| 293 // The number of in process SaveItems. | 287 // The number of in process SaveItems. |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 314 base::FilePath GetSuggestedNameForSaveAs( | 308 base::FilePath GetSuggestedNameForSaveAs( |
| 315 bool can_save_as_complete, | 309 bool can_save_as_complete, |
| 316 const std::string& contents_mime_type); | 310 const std::string& contents_mime_type); |
| 317 | 311 |
| 318 // Ensures that the file name has a proper extension for HTML by adding ".htm" | 312 // Ensures that the file name has a proper extension for HTML by adding ".htm" |
| 319 // if necessary. | 313 // if necessary. |
| 320 static base::FilePath EnsureHtmlExtension(const base::FilePath& name); | 314 static base::FilePath EnsureHtmlExtension(const base::FilePath& name); |
| 321 | 315 |
| 322 // Ensures that the file name has a proper extension for supported formats | 316 // Ensures that the file name has a proper extension for supported formats |
| 323 // if necessary. | 317 // if necessary. |
| 324 static base::FilePath EnsureMimeExtension(const base::FilePath& name, | 318 static base::FilePath EnsureMimeExtension( |
| 319 const base::FilePath& name, | |
| 325 const std::string& contents_mime_type); | 320 const std::string& contents_mime_type); |
| 326 | 321 |
| 327 // Returns extension for supported MIME types (for example, for "text/plain" | 322 // Returns extension for supported MIME types (for example, for "text/plain" |
| 328 // it returns "txt"). | 323 // it returns "txt"). |
| 329 static const base::FilePath::CharType* ExtensionForMimeType( | 324 static const base::FilePath::CharType* ExtensionForMimeType( |
| 330 const std::string& contents_mime_type); | 325 const std::string& contents_mime_type); |
| 331 | 326 |
| 332 using SaveItemQueue = std::deque<SaveItem*>; | 327 using SaveItemQueue = std::deque<SaveItem*>; |
| 333 // A queue for items we are about to start saving. | 328 // A queue for items we are about to start saving. |
| 334 SaveItemQueue waiting_item_queue_; | 329 SaveItemQueue waiting_item_queue_; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 385 | 380 |
| 386 // Indicates whether user get disk error. | 381 // Indicates whether user get disk error. |
| 387 bool disk_error_occurred_; | 382 bool disk_error_occurred_; |
| 388 | 383 |
| 389 // Type about saving page as only-html or complete-html. | 384 // Type about saving page as only-html or complete-html. |
| 390 SavePageType save_type_; | 385 SavePageType save_type_; |
| 391 | 386 |
| 392 // Number of all need to be saved resources. | 387 // Number of all need to be saved resources. |
| 393 size_t all_save_items_count_; | 388 size_t all_save_items_count_; |
| 394 | 389 |
| 395 using FileNameSet = | 390 using FileNameSet = std::set<base::FilePath::StringType, |
| 396 std::set<base::FilePath::StringType, | 391 bool (*)(base::FilePath::StringPieceType, |
| 397 bool (*)(base::FilePath::StringPieceType, | 392 base::FilePath::StringPieceType)>; |
| 398 base::FilePath::StringPieceType)>; | |
| 399 // This set is used to eliminate duplicated file names in saving directory. | 393 // This set is used to eliminate duplicated file names in saving directory. |
| 400 FileNameSet file_name_set_; | 394 FileNameSet file_name_set_; |
| 401 | 395 |
| 402 using FileNameCountMap = | 396 using FileNameCountMap = |
| 403 std::unordered_map<base::FilePath::StringType, uint32_t>; | 397 std::unordered_map<base::FilePath::StringType, uint32_t>; |
| 404 // This map is used to track serial number for specified filename. | 398 // This map is used to track serial number for specified filename. |
| 405 FileNameCountMap file_name_count_map_; | 399 FileNameCountMap file_name_count_map_; |
| 406 | 400 |
| 407 // Indicates current waiting state when SavePackage try to get something | 401 // Indicates current waiting state when SavePackage try to get something |
| 408 // from outside. | 402 // from outside. |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 419 friend class SavePackageTest; | 413 friend class SavePackageTest; |
| 420 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestSuggestedSaveNames); | 414 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestSuggestedSaveNames); |
| 421 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestLongSafePureFilename); | 415 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestLongSafePureFilename); |
| 422 | 416 |
| 423 DISALLOW_COPY_AND_ASSIGN(SavePackage); | 417 DISALLOW_COPY_AND_ASSIGN(SavePackage); |
| 424 }; | 418 }; |
| 425 | 419 |
| 426 } // namespace content | 420 } // namespace content |
| 427 | 421 |
| 428 #endif // CONTENT_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ | 422 #endif // CONTENT_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ |
| OLD | NEW |