| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 CHROME_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ | 5 #ifndef CHROME_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ |
| 6 #define CHROME_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ | 6 #define CHROME_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ |
| 7 | 7 |
| 8 #include <queue> | 8 #include <queue> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 class DownloadItem; | 25 class DownloadItem; |
| 26 class DownloadManager; | 26 class DownloadManager; |
| 27 class GURL; | 27 class GURL; |
| 28 class MessageLoop; | 28 class MessageLoop; |
| 29 class PrefService; | 29 class PrefService; |
| 30 class Profile; | 30 class Profile; |
| 31 class WebContents; | 31 class WebContents; |
| 32 class URLRequestContext; | 32 class URLRequestContext; |
| 33 class WebContents; | 33 class WebContents; |
| 34 | 34 |
| 35 #if defined(OS_WIN) || defined(OS_LINUX) |
| 36 // TODO(port): port this header. |
| 37 #include "chrome/browser/shell_dialogs.h" |
| 38 #elif defined(OS_MACOSX) |
| 39 #include "chrome/common/temp_scaffolding_stubs.h" |
| 40 #endif |
| 41 |
| 35 namespace base { | 42 namespace base { |
| 36 class Thread; | 43 class Thread; |
| 37 class Time; | 44 class Time; |
| 38 } | 45 } |
| 39 | 46 |
| 47 struct SavePackageParam; |
| 48 |
| 40 // The SavePackage object manages the process of saving a page as only-html or | 49 // The SavePackage object manages the process of saving a page as only-html or |
| 41 // complete-html and providing the information for displaying saving status. | 50 // complete-html and providing the information for displaying saving status. |
| 42 // Saving page as only-html means means that we save web page to a single HTML | 51 // Saving page as only-html means means that we save web page to a single HTML |
| 43 // file regardless internal sub resources and sub frames. | 52 // file regardless internal sub resources and sub frames. |
| 44 // Saving page as complete-html page means we save not only the main html file | 53 // Saving page as complete-html page means we save not only the main html file |
| 45 // the user told it to save but also a directory for the auxiliary files such | 54 // the user told it to save but also a directory for the auxiliary files such |
| 46 // as all sub-frame html files, image files, css files and js files. | 55 // as all sub-frame html files, image files, css files and js files. |
| 47 // | 56 // |
| 48 // Each page saving job may include one or multiple files which need to be | 57 // Each page saving job may include one or multiple files which need to be |
| 49 // saved. Each file is represented by a SaveItem, and all SaveItems are owned | 58 // saved. Each file is represented by a SaveItem, and all SaveItems are owned |
| 50 // by the SavePackage. SaveItems are created when a user initiates a page | 59 // by the SavePackage. SaveItems are created when a user initiates a page |
| 51 // saving job, and exist for the duration of one tab's life time. | 60 // saving job, and exist for the duration of one tab's life time. |
| 52 class SavePackage : public base::RefCountedThreadSafe<SavePackage>, | 61 class SavePackage : public base::RefCountedThreadSafe<SavePackage>, |
| 53 public RenderViewHostDelegate::Save { | 62 public RenderViewHostDelegate::Save, |
| 63 public SelectFileDialog::Listener { |
| 54 public: | 64 public: |
| 55 enum SavePackageType { | 65 enum SavePackageType { |
| 56 // User chose to save only the HTML of the page. | 66 // User chose to save only the HTML of the page. |
| 57 SAVE_AS_ONLY_HTML = 0, | 67 SAVE_AS_ONLY_HTML = 0, |
| 58 // User chose to save complete-html page. | 68 // User chose to save complete-html page. |
| 59 SAVE_AS_COMPLETE_HTML = 1 | 69 SAVE_AS_COMPLETE_HTML = 1 |
| 60 }; | 70 }; |
| 61 | 71 |
| 62 enum WaitState { | 72 enum WaitState { |
| 63 // State when created but not initialized. | 73 // State when created but not initialized. |
| 64 INITIALIZE = 0, | 74 INITIALIZE = 0, |
| 65 // State when after initializing, but not yet saving. | 75 // State when after initializing, but not yet saving. |
| 66 START_PROCESS, | 76 START_PROCESS, |
| 67 // Waiting on a list of savable resources from the backend. | 77 // Waiting on a list of savable resources from the backend. |
| 68 RESOURCES_LIST, | 78 RESOURCES_LIST, |
| 69 // Waiting for data sent from net IO or from file system. | 79 // Waiting for data sent from net IO or from file system. |
| 70 NET_FILES, | 80 NET_FILES, |
| 71 // Waiting for html DOM data sent from render process. | 81 // Waiting for html DOM data sent from render process. |
| 72 HTML_DATA, | 82 HTML_DATA, |
| 73 // Saving page finished successfully. | 83 // Saving page finished successfully. |
| 74 SUCCESSFUL, | 84 SUCCESSFUL, |
| 75 // Failed to save page. | 85 // Failed to save page. |
| 76 FAILED | 86 FAILED |
| 77 }; | 87 }; |
| 78 | 88 |
| 89 // Constructor for user initiated page saving. This constructor results in a |
| 90 // SavePackage that will generate and sanitize a suggested name for the user |
| 91 // in the "Save As" dialog box. |
| 92 SavePackage(WebContents* web_content); |
| 93 |
| 94 // This contructor is used only for testing. We can bypass the file and |
| 95 // directory name generation / sanitization by providing well known paths |
| 96 // better suited for tests. |
| 79 SavePackage(WebContents* web_content, | 97 SavePackage(WebContents* web_content, |
| 80 SavePackageType save_type, | 98 SavePackageType save_type, |
| 81 const FilePath& file_full_path, | 99 const FilePath& file_full_path, |
| 82 const FilePath& directory_full_path); | 100 const FilePath& directory_full_path); |
| 83 | 101 |
| 84 ~SavePackage(); | 102 ~SavePackage(); |
| 85 | 103 |
| 86 // Initialize the SavePackage. Returns true if it initializes properly. | 104 // Initialize the SavePackage. Returns true if it initializes properly. |
| 87 // Need to make sure that this method must be called in the UI thread because | 105 // Need to make sure that this method must be called in the UI thread because |
| 88 // using g_browser_process on a non-UI thread can cause crashes during | 106 // using g_browser_process on a non-UI thread can cause crashes during |
| (...skipping 21 matching lines...) Expand all Loading... |
| 110 bool canceled() { return user_canceled_ || disk_error_occurred_; } | 128 bool canceled() { return user_canceled_ || disk_error_occurred_; } |
| 111 | 129 |
| 112 // Accessor | 130 // Accessor |
| 113 bool finished() { return finished_; } | 131 bool finished() { return finished_; } |
| 114 SavePackageType save_type() { return save_type_; } | 132 SavePackageType save_type() { return save_type_; } |
| 115 | 133 |
| 116 // Since for one tab, it can only have one SavePackage in same time. | 134 // Since for one tab, it can only have one SavePackage in same time. |
| 117 // Now we actually use render_process_id as tab's unique id. | 135 // Now we actually use render_process_id as tab's unique id. |
| 118 int tab_id() const { return tab_id_; } | 136 int tab_id() const { return tab_id_; } |
| 119 | 137 |
| 138 void GetSaveInfo(); |
| 139 void ContinueSave(SavePackageParam* param, |
| 140 const std::wstring& final_name, |
| 141 int index); |
| 142 |
| 120 // RenderViewHostDelegate::Save ---------------------------------------------- | 143 // RenderViewHostDelegate::Save ---------------------------------------------- |
| 121 | 144 |
| 122 // Process all of the current page's savable links of subresources, resources | 145 // Process all of the current page's savable links of subresources, resources |
| 123 // referrers and frames (including the main frame and subframes) from the | 146 // referrers and frames (including the main frame and subframes) from the |
| 124 // render view host. | 147 // render view host. |
| 125 virtual void OnReceivedSavableResourceLinksForCurrentPage( | 148 virtual void OnReceivedSavableResourceLinksForCurrentPage( |
| 126 const std::vector<GURL>& resources_list, | 149 const std::vector<GURL>& resources_list, |
| 127 const std::vector<GURL>& referrers_list, | 150 const std::vector<GURL>& referrers_list, |
| 128 const std::vector<GURL>& frames_list); | 151 const std::vector<GURL>& frames_list); |
| 129 | 152 |
| 130 // Process the serialized html content data of a specified web page | 153 // Process the serialized html content data of a specified web page |
| 131 // gotten from render process. | 154 // gotten from render process. |
| 132 virtual void OnReceivedSerializedHtmlData(const GURL& frame_url, | 155 virtual void OnReceivedSerializedHtmlData(const GURL& frame_url, |
| 133 const std::string& data, | 156 const std::string& data, |
| 134 int32 status); | 157 int32 status); |
| 135 | 158 |
| 136 // Statics ------------------------------------------------------------------- | 159 // Statics ------------------------------------------------------------------- |
| 137 | 160 |
| 138 // Used to disable prompting the user for a directory/filename of the saved | 161 // Used to disable prompting the user for a directory/filename of the saved |
| 139 // web page. This is available for testing. | 162 // web page. This is available for testing. |
| 140 static void SetShouldPromptUser(bool should_prompt); | 163 static void SetShouldPromptUser(bool should_prompt); |
| 141 | 164 |
| 142 // Helper function for preparing suggested name for the SaveAs Dialog. The | 165 // Helper function for preparing suggested name for the SaveAs Dialog. The |
| 143 // suggested name is composed of the default save path and the web document's | 166 // suggested name is composed of the default save path and the web document's |
| 144 // title. | 167 // title. |
| 145 static FilePath GetSuggestNameForSaveAs(PrefService* prefs, | 168 static FilePath GetSuggestNameForSaveAs(PrefService* prefs, |
| 146 const FilePath& name); | 169 const FilePath& name); |
| 147 | 170 |
| 148 // This structure is for storing parameters which we will use to create | |
| 149 // a SavePackage object later. | |
| 150 struct SavePackageParam { | |
| 151 // MIME type of current tab contents. | |
| 152 const std::string& current_tab_mime_type; | |
| 153 // Pointer to preference service. | |
| 154 PrefService* prefs; | |
| 155 // Type about saving page as only-html or complete-html. | |
| 156 SavePackageType save_type; | |
| 157 // File path for main html file. | |
| 158 FilePath saved_main_file_path; | |
| 159 // Directory path for saving sub resources and sub html frames. | |
| 160 FilePath dir; | |
| 161 | |
| 162 SavePackageParam(const std::string& mime_type) | |
| 163 : current_tab_mime_type(mime_type) { } | |
| 164 }; | |
| 165 static bool GetSaveInfo(const FilePath& suggest_name, | |
| 166 gfx::NativeView container_window, | |
| 167 SavePackageParam* param, | |
| 168 DownloadManager* download_manager); | |
| 169 | |
| 170 // Check whether we can do the saving page operation for the specified URL. | 171 // Check whether we can do the saving page operation for the specified URL. |
| 171 static bool IsSavableURL(const GURL& url); | 172 static bool IsSavableURL(const GURL& url); |
| 172 | 173 |
| 173 // Check whether we can do the saving page operation for the contents which | 174 // Check whether we can do the saving page operation for the contents which |
| 174 // have the specified MIME type. | 175 // have the specified MIME type. |
| 175 static bool IsSavableContents(const std::string& contents_mime_type); | 176 static bool IsSavableContents(const std::string& contents_mime_type); |
| 176 | 177 |
| 177 // Check whether we can save page as complete-HTML for the contents which | 178 // Check whether we can save page as complete-HTML for the contents which |
| 178 // have specified a MIME type. Now only contents which have the MIME type | 179 // have specified a MIME type. Now only contents which have the MIME type |
| 179 // "text/html" can be saved as complete-HTML. | 180 // "text/html" can be saved as complete-HTML. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 194 // file path. If the length of specified file path is great than | 195 // file path. If the length of specified file path is great than |
| 195 // |max_file_path_len|, the |pure_file_name| will output new pure file name | 196 // |max_file_path_len|, the |pure_file_name| will output new pure file name |
| 196 // part for making sure the length of specified file path is less than | 197 // part for making sure the length of specified file path is less than |
| 197 // specified maximum length of file path. Return false if the function can | 198 // specified maximum length of file path. Return false if the function can |
| 198 // not get a safe pure file name, otherwise it returns true. | 199 // not get a safe pure file name, otherwise it returns true. |
| 199 static bool GetSafePureFileName(const FilePath& dir_path, | 200 static bool GetSafePureFileName(const FilePath& dir_path, |
| 200 const FilePath::StringType& file_name_ext, | 201 const FilePath::StringType& file_name_ext, |
| 201 uint32 max_file_path_len, | 202 uint32 max_file_path_len, |
| 202 FilePath::StringType* pure_file_name); | 203 FilePath::StringType* pure_file_name); |
| 203 | 204 |
| 205 // SelectFileDialog::Listener interface. |
| 206 virtual void FileSelected(const std::wstring& path, int index, void* params); |
| 207 virtual void FileSelectionCanceled(void* params); |
| 208 |
| 204 private: | 209 private: |
| 205 // For testing only. | 210 // For testing only. |
| 206 SavePackage(const FilePath& file_full_path, | 211 SavePackage(const FilePath& file_full_path, |
| 207 const FilePath& directory_full_path); | 212 const FilePath& directory_full_path); |
| 208 | 213 |
| 209 void Stop(); | 214 void Stop(); |
| 210 void CheckFinish(); | 215 void CheckFinish(); |
| 211 void SaveNextFile(bool process_all_remainder_items); | 216 void SaveNextFile(bool process_all_remainder_items); |
| 212 void DoSavingProcess(); | 217 void DoSavingProcess(); |
| 213 | 218 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 // This map is used to track serial number for specified filename. | 299 // This map is used to track serial number for specified filename. |
| 295 FileNameCountMap file_name_count_map_; | 300 FileNameCountMap file_name_count_map_; |
| 296 | 301 |
| 297 // Indicates current waiting state when SavePackage try to get something | 302 // Indicates current waiting state when SavePackage try to get something |
| 298 // from outside. | 303 // from outside. |
| 299 WaitState wait_state_; | 304 WaitState wait_state_; |
| 300 | 305 |
| 301 // Unique id for this SavePackage. | 306 // Unique id for this SavePackage. |
| 302 const int tab_id_; | 307 const int tab_id_; |
| 303 | 308 |
| 309 // For managing select file dialogs. |
| 310 scoped_refptr<SelectFileDialog> select_file_dialog_; |
| 311 |
| 304 friend class SavePackageTest; | 312 friend class SavePackageTest; |
| 305 DISALLOW_COPY_AND_ASSIGN(SavePackage); | 313 DISALLOW_COPY_AND_ASSIGN(SavePackage); |
| 306 }; | 314 }; |
| 307 | 315 |
| 308 #endif // CHROME_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ | 316 #endif // CHROME_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ |
| OLD | NEW |