| 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 27 matching lines...) Expand all Loading... |
| 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. |
| 88 // Need to make sure that this method must be called in the UI thread because | 91 // Need to make sure that this method must be called in the UI thread because |
| 89 // using g_browser_process on a non-UI thread can cause crashes during | 92 // using g_browser_process on a non-UI thread can cause crashes during |
| 90 // shutdown. | 93 // shutdown. |
| 91 bool Init(); | 94 bool Init(content::SaveFileDownloadCreatedCallback download_created_callback); |
| 92 | 95 |
| 93 // Cancel all in progress request, might be called by user or internal error. | 96 // Cancel all in progress request, might be called by user or internal error. |
| 94 void Cancel(bool user_action); | 97 void Cancel(bool user_action); |
| 95 | 98 |
| 96 void Finish(); | 99 void Finish(); |
| 97 | 100 |
| 98 // Notifications sent from the file thread to the UI thread. | 101 // Notifications sent from the file thread to the UI thread. |
| 99 void StartSave(const SaveFileCreateInfo* info); | 102 void StartSave(const SaveFileCreateInfo* info); |
| 100 bool UpdateSaveProgress(int32 save_id, int64 size, bool write_success); | 103 bool UpdateSaveProgress(int32 save_id, int64 size, bool write_success); |
| 101 void SaveFinished(int32 save_id, int64 size, bool is_success); | 104 void SaveFinished(int32 save_id, int64 size, bool is_success); |
| 102 void SaveFailed(const GURL& save_url); | 105 void SaveFailed(const GURL& save_url); |
| 103 void SaveCanceled(SaveItem* save_item); | 106 void SaveCanceled(SaveItem* save_item); |
| 104 | 107 |
| 105 // Rough percent complete, -1 means we don't know (since we didn't receive a | 108 // Rough percent complete, -1 means we don't know (since we didn't receive a |
| 106 // total size). | 109 // total size). |
| 107 int PercentComplete(); | 110 int PercentComplete(); |
| 108 | 111 |
| 109 bool canceled() const { return user_canceled_ || disk_error_occurred_; } | 112 bool canceled() const { return user_canceled_ || disk_error_occurred_; } |
| 110 bool finished() const { return finished_; } | 113 bool finished() const { return finished_; } |
| 111 content::SavePageType save_type() const { return save_type_; } | 114 content::SavePageType save_type() const { return save_type_; } |
| 112 int contents_id() const { return contents_id_; } | 115 int contents_id() const { return contents_id_; } |
| 113 int id() const { return unique_id_; } | 116 int id() const { return unique_id_; } |
| 114 content::WebContents* web_contents() const; | 117 content::WebContents* web_contents() const; |
| 115 | 118 |
| 116 void GetSaveInfo(); | 119 void GetSaveInfo(); |
| 117 | 120 |
| 118 private: | 121 private: |
| 119 friend class base::RefCountedThreadSafe<SavePackage>; | 122 friend class base::RefCountedThreadSafe<SavePackage>; |
| 120 | 123 |
| 124 // Callback for content::WebContents::GenerateMHTML(). |
| 125 void OnMHTMLGenerated(const FilePath& path, int64 size); |
| 126 |
| 121 // For testing only. | 127 // For testing only. |
| 122 SavePackage(content::WebContents* web_contents, | 128 SavePackage(content::WebContents* web_contents, |
| 123 const FilePath& file_full_path, | 129 const FilePath& file_full_path, |
| 124 const FilePath& directory_full_path); | 130 const FilePath& directory_full_path); |
| 125 | 131 |
| 126 virtual ~SavePackage(); | 132 virtual ~SavePackage(); |
| 127 | 133 |
| 128 // Notes from Init() above applies here as well. | 134 // Notes from Init() above applies here as well. |
| 129 void InternalInit(); | 135 void InternalInit(); |
| 130 | 136 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 | 184 |
| 179 // Retrieves the URL to be saved from the WebContents. | 185 // Retrieves the URL to be saved from the WebContents. |
| 180 GURL GetUrlToBeSaved(); | 186 GURL GetUrlToBeSaved(); |
| 181 | 187 |
| 182 void CreateDirectoryOnFileThread(const FilePath& website_save_dir, | 188 void CreateDirectoryOnFileThread(const FilePath& website_save_dir, |
| 183 const FilePath& download_save_dir, | 189 const FilePath& download_save_dir, |
| 184 const std::string& mime_type, | 190 const std::string& mime_type, |
| 185 const std::string& accept_langs); | 191 const std::string& accept_langs); |
| 186 void ContinueGetSaveInfo(const FilePath& suggested_path, | 192 void ContinueGetSaveInfo(const FilePath& suggested_path, |
| 187 bool can_save_as_complete); | 193 bool can_save_as_complete); |
| 188 void OnPathPicked(const FilePath& final_name, content::SavePageType type); | 194 void OnPathPicked( |
| 195 const FilePath& final_name, |
| 196 content::SavePageType type, |
| 197 content::SaveFileDownloadCreatedCallback download_created_callback); |
| 189 void OnReceivedSavableResourceLinksForCurrentPage( | 198 void OnReceivedSavableResourceLinksForCurrentPage( |
| 190 const std::vector<GURL>& resources_list, | 199 const std::vector<GURL>& resources_list, |
| 191 const std::vector<GURL>& referrers_list, | 200 const std::vector<GURL>& referrers_list, |
| 192 const std::vector<GURL>& frames_list); | 201 const std::vector<GURL>& frames_list); |
| 193 | 202 |
| 194 void OnReceivedSerializedHtmlData(const GURL& frame_url, | 203 void OnReceivedSerializedHtmlData(const GURL& frame_url, |
| 195 const std::string& data, | 204 const std::string& data, |
| 196 int32 status); | 205 int32 status); |
| 197 | 206 |
| 198 typedef base::hash_map<std::string, SaveItem*> SaveUrlItemMap; | 207 typedef base::hash_map<std::string, SaveItem*> SaveUrlItemMap; |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 bool wrote_to_failed_file_; | 316 bool wrote_to_failed_file_; |
| 308 | 317 |
| 309 friend class SavePackageTest; | 318 friend class SavePackageTest; |
| 310 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestSuggestedSaveNames); | 319 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestSuggestedSaveNames); |
| 311 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestLongSafePureFilename); | 320 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestLongSafePureFilename); |
| 312 | 321 |
| 313 DISALLOW_COPY_AND_ASSIGN(SavePackage); | 322 DISALLOW_COPY_AND_ASSIGN(SavePackage); |
| 314 }; | 323 }; |
| 315 | 324 |
| 316 #endif // CONTENT_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ | 325 #endif // CONTENT_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ |
| OLD | NEW |