| 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <queue> | 9 #include <queue> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/file_path.h" | 14 #include "base/file_path.h" |
| 15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
| 16 #include "base/hash_tables.h" | 16 #include "base/hash_tables.h" |
| 17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
| 19 #include "base/time.h" | 19 #include "base/time.h" |
| 20 #include "content/common/content_export.h" | 20 #include "content/common/content_export.h" |
| 21 #include "content/public/browser/download_item.h" | 21 #include "content/public/browser/download_item.h" |
| 22 #include "content/public/browser/download_manager_delegate.h" |
| 22 #include "content/public/browser/save_page_type.h" | 23 #include "content/public/browser/save_page_type.h" |
| 23 #include "content/public/browser/web_contents_observer.h" | 24 #include "content/public/browser/web_contents_observer.h" |
| 24 #include "googleurl/src/gurl.h" | 25 #include "googleurl/src/gurl.h" |
| 25 | 26 |
| 26 class GURL; | 27 class GURL; |
| 27 class SaveFileManager; | 28 class SaveFileManager; |
| 28 class SaveItem; | 29 class SaveItem; |
| 29 class SavePackage; | 30 class SavePackage; |
| 30 struct SaveFileCreateInfo; | 31 struct SaveFileCreateInfo; |
| 31 | 32 |
| 32 namespace content { | 33 namespace content { |
| 33 class DownloadManager; | 34 class DownloadManager; |
| 34 class WebContents; | 35 class WebContents; |
| 35 } | 36 } |
| 36 | 37 |
| 37 // The SavePackage object manages the process of saving a page as only-html or | 38 // The SavePackage object manages the process of saving a page as only-html or |
| 38 // complete-html and providing the information for displaying saving status. | 39 // complete-html or MHTML and providing the information for displaying saving |
| 39 // Saving page as only-html means means that we save web page to a single HTML | 40 // status. Saving page as only-html means means that we save web page to a |
| 40 // file regardless internal sub resources and sub frames. | 41 // single HTML file regardless internal sub resources and sub frames. Saving |
| 41 // Saving page as complete-html page means we save not only the main html file | 42 // page as complete-html page means we save not only the main html file the user |
| 42 // the user told it to save but also a directory for the auxiliary files such | 43 // told it to save but also a directory for the auxiliary files such as all |
| 43 // as all sub-frame html files, image files, css files and js files. | 44 // sub-frame html files, image files, css files and js files. Saving page as |
| 45 // MHTML means the same thing as complete-html, but it uses the MHTML format to |
| 46 // contain the html and all auxiliary files in a single text file. |
| 44 // | 47 // |
| 45 // Each page saving job may include one or multiple files which need to be | 48 // Each page saving job may include one or multiple files which need to be |
| 46 // saved. Each file is represented by a SaveItem, and all SaveItems are owned | 49 // saved. Each file is represented by a SaveItem, and all SaveItems are owned |
| 47 // by the SavePackage. SaveItems are created when a user initiates a page | 50 // by the SavePackage. SaveItems are created when a user initiates a page |
| 48 // saving job, and exist for the duration of one contents's life time. | 51 // saving job, and exist for the duration of one contents's life time. |
| 49 class CONTENT_EXPORT SavePackage | 52 class CONTENT_EXPORT SavePackage |
| 50 : public base::RefCountedThreadSafe<SavePackage>, | 53 : public base::RefCountedThreadSafe<SavePackage>, |
| 51 public content::WebContentsObserver, | 54 public content::WebContentsObserver, |
| 52 public content::DownloadItem::Observer, | 55 public content::DownloadItem::Observer, |
| 53 public base::SupportsWeakPtr<SavePackage> { | 56 public base::SupportsWeakPtr<SavePackage> { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 77 explicit SavePackage(content::WebContents* web_contents); | 80 explicit SavePackage(content::WebContents* web_contents); |
| 78 | 81 |
| 79 // This contructor is used only for testing. We can bypass the file and | 82 // This contructor is used only for testing. We can bypass the file and |
| 80 // directory name generation / sanitization by providing well known paths | 83 // directory name generation / sanitization by providing well known paths |
| 81 // better suited for tests. | 84 // better suited for tests. |
| 82 SavePackage(content::WebContents* web_contents, | 85 SavePackage(content::WebContents* web_contents, |
| 83 content::SavePageType save_type, | 86 content::SavePageType save_type, |
| 84 const FilePath& file_full_path, | 87 const FilePath& file_full_path, |
| 85 const FilePath& directory_full_path); | 88 const FilePath& directory_full_path); |
| 86 | 89 |
| 87 // Initialize the SavePackage. Returns true if it initializes properly. | 90 // Initialize the SavePackage. Returns true if it initializes properly. Need |
| 88 // Need to make sure that this method must be called in the UI thread because | 91 // to make sure that this method must be called in the UI thread because using |
| 89 // using g_browser_process on a non-UI thread can cause crashes during | 92 // g_browser_process on a non-UI thread can cause crashes during shutdown. |
| 90 // shutdown. | 93 // |cb| will be called when the DownloadItem is created, before data is |
| 91 bool Init(); | 94 // written to disk. |
| 95 bool Init(const content::SavePackageDownloadCreatedCallback& cb); |
| 92 | 96 |
| 93 // Cancel all in progress request, might be called by user or internal error. | 97 // Cancel all in progress request, might be called by user or internal error. |
| 94 void Cancel(bool user_action); | 98 void Cancel(bool user_action); |
| 95 | 99 |
| 96 void Finish(); | 100 void Finish(); |
| 97 | 101 |
| 98 // Notifications sent from the file thread to the UI thread. | 102 // Notifications sent from the file thread to the UI thread. |
| 99 void StartSave(const SaveFileCreateInfo* info); | 103 void StartSave(const SaveFileCreateInfo* info); |
| 100 bool UpdateSaveProgress(int32 save_id, int64 size, bool write_success); | 104 bool UpdateSaveProgress(int32 save_id, int64 size, bool write_success); |
| 101 void SaveFinished(int32 save_id, int64 size, bool is_success); | 105 void SaveFinished(int32 save_id, int64 size, bool is_success); |
| 102 void SaveFailed(const GURL& save_url); | 106 void SaveFailed(const GURL& save_url); |
| 103 void SaveCanceled(SaveItem* save_item); | 107 void SaveCanceled(SaveItem* save_item); |
| 104 | 108 |
| 105 // Rough percent complete, -1 means we don't know (since we didn't receive a | 109 // Rough percent complete, -1 means we don't know (since we didn't receive a |
| 106 // total size). | 110 // total size). |
| 107 int PercentComplete(); | 111 int PercentComplete(); |
| 108 | 112 |
| 109 bool canceled() const { return user_canceled_ || disk_error_occurred_; } | 113 bool canceled() const { return user_canceled_ || disk_error_occurred_; } |
| 110 bool finished() const { return finished_; } | 114 bool finished() const { return finished_; } |
| 111 content::SavePageType save_type() const { return save_type_; } | 115 content::SavePageType save_type() const { return save_type_; } |
| 112 int contents_id() const { return contents_id_; } | 116 int contents_id() const { return contents_id_; } |
| 113 int id() const { return unique_id_; } | 117 int id() const { return unique_id_; } |
| 114 content::WebContents* web_contents() const; | 118 content::WebContents* web_contents() const; |
| 115 | 119 |
| 116 void GetSaveInfo(); | 120 void GetSaveInfo(); |
| 117 | 121 |
| 118 private: | 122 private: |
| 119 friend class base::RefCountedThreadSafe<SavePackage>; | 123 friend class base::RefCountedThreadSafe<SavePackage>; |
| 120 | 124 |
| 125 // Callback for content::WebContents::GenerateMHTML(). |
| 126 void OnMHTMLGenerated(const FilePath& path, int64 size); |
| 127 |
| 121 // For testing only. | 128 // For testing only. |
| 122 SavePackage(content::WebContents* web_contents, | 129 SavePackage(content::WebContents* web_contents, |
| 123 const FilePath& file_full_path, | 130 const FilePath& file_full_path, |
| 124 const FilePath& directory_full_path); | 131 const FilePath& directory_full_path); |
| 125 | 132 |
| 126 virtual ~SavePackage(); | 133 virtual ~SavePackage(); |
| 127 | 134 |
| 128 // Notes from Init() above applies here as well. | 135 // Notes from Init() above applies here as well. |
| 129 void InternalInit(); | 136 void InternalInit(); |
| 130 | 137 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 | 185 |
| 179 // Retrieves the URL to be saved from the WebContents. | 186 // Retrieves the URL to be saved from the WebContents. |
| 180 GURL GetUrlToBeSaved(); | 187 GURL GetUrlToBeSaved(); |
| 181 | 188 |
| 182 void CreateDirectoryOnFileThread(const FilePath& website_save_dir, | 189 void CreateDirectoryOnFileThread(const FilePath& website_save_dir, |
| 183 const FilePath& download_save_dir, | 190 const FilePath& download_save_dir, |
| 184 const std::string& mime_type, | 191 const std::string& mime_type, |
| 185 const std::string& accept_langs); | 192 const std::string& accept_langs); |
| 186 void ContinueGetSaveInfo(const FilePath& suggested_path, | 193 void ContinueGetSaveInfo(const FilePath& suggested_path, |
| 187 bool can_save_as_complete); | 194 bool can_save_as_complete); |
| 188 void OnPathPicked(const FilePath& final_name, content::SavePageType type); | 195 void OnPathPicked( |
| 196 const FilePath& final_name, |
| 197 content::SavePageType type, |
| 198 const content::SavePackageDownloadCreatedCallback& cb); |
| 189 void OnReceivedSavableResourceLinksForCurrentPage( | 199 void OnReceivedSavableResourceLinksForCurrentPage( |
| 190 const std::vector<GURL>& resources_list, | 200 const std::vector<GURL>& resources_list, |
| 191 const std::vector<GURL>& referrers_list, | 201 const std::vector<GURL>& referrers_list, |
| 192 const std::vector<GURL>& frames_list); | 202 const std::vector<GURL>& frames_list); |
| 193 | 203 |
| 194 void OnReceivedSerializedHtmlData(const GURL& frame_url, | 204 void OnReceivedSerializedHtmlData(const GURL& frame_url, |
| 195 const std::string& data, | 205 const std::string& data, |
| 196 int32 status); | 206 int32 status); |
| 197 | 207 |
| 198 typedef base::hash_map<std::string, SaveItem*> SaveUrlItemMap; | 208 typedef base::hash_map<std::string, SaveItem*> SaveUrlItemMap; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 | 273 |
| 264 // The title of the page the user wants to save. | 274 // The title of the page the user wants to save. |
| 265 string16 title_; | 275 string16 title_; |
| 266 | 276 |
| 267 // Used to calculate package download speed (in files per second). | 277 // Used to calculate package download speed (in files per second). |
| 268 base::TimeTicks start_tick_; | 278 base::TimeTicks start_tick_; |
| 269 | 279 |
| 270 // Indicates whether the actual saving job is finishing or not. | 280 // Indicates whether the actual saving job is finishing or not. |
| 271 bool finished_; | 281 bool finished_; |
| 272 | 282 |
| 283 // Indicates whether a call to Finish() has been scheduled. |
| 284 bool mhtml_finishing_; |
| 285 |
| 273 // Indicates whether user canceled the saving job. | 286 // Indicates whether user canceled the saving job. |
| 274 bool user_canceled_; | 287 bool user_canceled_; |
| 275 | 288 |
| 276 // Indicates whether user get disk error. | 289 // Indicates whether user get disk error. |
| 277 bool disk_error_occurred_; | 290 bool disk_error_occurred_; |
| 278 | 291 |
| 279 // Type about saving page as only-html or complete-html. | 292 // Type about saving page as only-html or complete-html. |
| 280 content::SavePageType save_type_; | 293 content::SavePageType save_type_; |
| 281 | 294 |
| 282 // Number of all need to be saved resources. | 295 // Number of all need to be saved resources. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 307 bool wrote_to_failed_file_; | 320 bool wrote_to_failed_file_; |
| 308 | 321 |
| 309 friend class SavePackageTest; | 322 friend class SavePackageTest; |
| 310 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestSuggestedSaveNames); | 323 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestSuggestedSaveNames); |
| 311 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestLongSafePureFilename); | 324 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestLongSafePureFilename); |
| 312 | 325 |
| 313 DISALLOW_COPY_AND_ASSIGN(SavePackage); | 326 DISALLOW_COPY_AND_ASSIGN(SavePackage); |
| 314 }; | 327 }; |
| 315 | 328 |
| 316 #endif // CONTENT_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ | 329 #endif // CONTENT_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ |
| OLD | NEW |