| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 CONTENT_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ |
| 6 #define CHROME_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/task.h" | 18 #include "base/task.h" |
| 19 #include "chrome/browser/ui/shell_dialogs.h" | |
| 20 #include "content/browser/tab_contents/tab_contents_observer.h" | 19 #include "content/browser/tab_contents/tab_contents_observer.h" |
| 21 #include "googleurl/src/gurl.h" | 20 #include "googleurl/src/gurl.h" |
| 22 | 21 |
| 23 class DownloadItem; | 22 class DownloadItem; |
| 24 class DownloadManager; | 23 class DownloadManager; |
| 25 class GURL; | 24 class GURL; |
| 26 class MessageLoop; | 25 class MessageLoop; |
| 27 class PrefService; | 26 class PrefService; |
| 28 class Profile; | 27 class Profile; |
| 29 struct SaveFileCreateInfo; | 28 struct SaveFileCreateInfo; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 45 // file regardless internal sub resources and sub frames. | 44 // file regardless internal sub resources and sub frames. |
| 46 // Saving page as complete-html page means we save not only the main html file | 45 // Saving page as complete-html page means we save not only the main html file |
| 47 // the user told it to save but also a directory for the auxiliary files such | 46 // the user told it to save but also a directory for the auxiliary files such |
| 48 // as all sub-frame html files, image files, css files and js files. | 47 // as all sub-frame html files, image files, css files and js files. |
| 49 // | 48 // |
| 50 // Each page saving job may include one or multiple files which need to be | 49 // 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 | 50 // 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 | 51 // by the SavePackage. SaveItems are created when a user initiates a page |
| 53 // saving job, and exist for the duration of one tab's life time. | 52 // saving job, and exist for the duration of one tab's life time. |
| 54 class SavePackage : public base::RefCountedThreadSafe<SavePackage>, | 53 class SavePackage : public base::RefCountedThreadSafe<SavePackage>, |
| 55 public TabContentsObserver, | 54 public TabContentsObserver { |
| 56 public SelectFileDialog::Listener { | |
| 57 public: | 55 public: |
| 58 enum SavePackageType { | 56 enum SavePackageType { |
| 59 // The value of the save type before its set by the user. | 57 // The value of the save type before its set by the user. |
| 60 SAVE_TYPE_UNKNOWN = -1, | 58 SAVE_TYPE_UNKNOWN = -1, |
| 61 // User chose to save only the HTML of the page. | 59 // User chose to save only the HTML of the page. |
| 62 SAVE_AS_ONLY_HTML = 0, | 60 SAVE_AS_ONLY_HTML = 0, |
| 63 // User chose to save complete-html page. | 61 // User chose to save complete-html page. |
| 64 SAVE_AS_COMPLETE_HTML = 1 | 62 SAVE_AS_COMPLETE_HTML = 1 |
| 65 }; | 63 }; |
| 66 | 64 |
| 67 enum WaitState { | 65 enum WaitState { |
| 68 // State when created but not initialized. | 66 // State when created but not initialized. |
| 69 INITIALIZE = 0, | 67 INITIALIZE = 0, |
| 70 // State when after initializing, but not yet saving. | 68 // State when after initializing, but not yet saving. |
| 71 START_PROCESS, | 69 START_PROCESS, |
| 72 // Waiting on a list of savable resources from the backend. | 70 // Waiting on a list of savable resources from the backend. |
| 73 RESOURCES_LIST, | 71 RESOURCES_LIST, |
| 74 // Waiting for data sent from net IO or from file system. | 72 // Waiting for data sent from net IO or from file system. |
| 75 NET_FILES, | 73 NET_FILES, |
| 76 // Waiting for html DOM data sent from render process. | 74 // Waiting for html DOM data sent from render process. |
| 77 HTML_DATA, | 75 HTML_DATA, |
| 78 // Saving page finished successfully. | 76 // Saving page finished successfully. |
| 79 SUCCESSFUL, | 77 SUCCESSFUL, |
| 80 // Failed to save page. | 78 // Failed to save page. |
| 81 FAILED | 79 FAILED |
| 82 }; | 80 }; |
| 83 | 81 |
| 82 static const FilePath::CharType kDefaultHtmlExtension[]; |
| 83 |
| 84 // Constructor for user initiated page saving. This constructor results in a | 84 // Constructor for user initiated page saving. This constructor results in a |
| 85 // SavePackage that will generate and sanitize a suggested name for the user | 85 // SavePackage that will generate and sanitize a suggested name for the user |
| 86 // in the "Save As" dialog box. | 86 // in the "Save As" dialog box. |
| 87 explicit SavePackage(TabContentsWrapper* wrapper); | 87 explicit SavePackage(TabContentsWrapper* wrapper); |
| 88 | 88 |
| 89 // This contructor is used only for testing. We can bypass the file and | 89 // This contructor is used only for testing. We can bypass the file and |
| 90 // directory name generation / sanitization by providing well known paths | 90 // directory name generation / sanitization by providing well known paths |
| 91 // better suited for tests. | 91 // better suited for tests. |
| 92 SavePackage(TabContentsWrapper* wrapper, | 92 SavePackage(TabContentsWrapper* wrapper, |
| 93 SavePackageType save_type, | 93 SavePackageType save_type, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 111 void SaveFailed(const GURL& save_url); | 111 void SaveFailed(const GURL& save_url); |
| 112 void SaveCanceled(SaveItem* save_item); | 112 void SaveCanceled(SaveItem* save_item); |
| 113 | 113 |
| 114 // Rough percent complete, -1 means we don't know (since we didn't receive a | 114 // Rough percent complete, -1 means we don't know (since we didn't receive a |
| 115 // total size). | 115 // total size). |
| 116 int PercentComplete(); | 116 int PercentComplete(); |
| 117 | 117 |
| 118 // Show or Open a saved page via the Windows shell. | 118 // Show or Open a saved page via the Windows shell. |
| 119 void ShowDownloadInShell(); | 119 void ShowDownloadInShell(); |
| 120 | 120 |
| 121 // Called by the embedder once a path is chosen by the user. |
| 122 void OnPathPicked(const FilePath& final_name, SavePackageType type); |
| 123 |
| 121 bool canceled() const { return user_canceled_ || disk_error_occurred_; } | 124 bool canceled() const { return user_canceled_ || disk_error_occurred_; } |
| 122 bool finished() const { return finished_; } | 125 bool finished() const { return finished_; } |
| 123 SavePackageType save_type() const { return save_type_; } | 126 SavePackageType save_type() const { return save_type_; } |
| 124 int tab_id() const { return tab_id_; } | 127 int tab_id() const { return tab_id_; } |
| 125 int id() const { return unique_id_; } | 128 int id() const { return unique_id_; } |
| 129 TabContents* tab_contents() const { |
| 130 return TabContentsObserver::tab_contents(); |
| 131 } |
| 126 | 132 |
| 127 void GetSaveInfo(); | 133 void GetSaveInfo(); |
| 128 | 134 |
| 129 // Statics ------------------------------------------------------------------- | 135 // Statics ------------------------------------------------------------------- |
| 130 | 136 |
| 131 // Used to disable prompting the user for a directory/filename of the saved | |
| 132 // web page. This is available for testing. | |
| 133 static void SetShouldPromptUser(bool should_prompt); | |
| 134 | |
| 135 // Check whether we can do the saving page operation for the specified URL. | 137 // Check whether we can do the saving page operation for the specified URL. |
| 136 static bool IsSavableURL(const GURL& url); | 138 static bool IsSavableURL(const GURL& url); |
| 137 | 139 |
| 138 // Check whether we can do the saving page operation for the contents which | 140 // Check whether we can do the saving page operation for the contents which |
| 139 // have the specified MIME type. | 141 // have the specified MIME type. |
| 140 static bool IsSavableContents(const std::string& contents_mime_type); | 142 static bool IsSavableContents(const std::string& contents_mime_type); |
| 141 | 143 |
| 142 // SelectFileDialog::Listener ------------------------------------------------ | |
| 143 virtual void FileSelected(const FilePath& path, int index, void* params); | |
| 144 virtual void FileSelectionCanceled(void* params); | |
| 145 | |
| 146 private: | 144 private: |
| 147 friend class base::RefCountedThreadSafe<SavePackage>; | 145 friend class base::RefCountedThreadSafe<SavePackage>; |
| 148 | 146 |
| 149 // For testing only. | 147 // For testing only. |
| 150 SavePackage(TabContentsWrapper* wrapper, | 148 SavePackage(TabContentsWrapper* wrapper, |
| 151 const FilePath& file_full_path, | 149 const FilePath& file_full_path, |
| 152 const FilePath& directory_full_path); | 150 const FilePath& directory_full_path); |
| 153 | 151 |
| 154 virtual ~SavePackage(); | 152 virtual ~SavePackage(); |
| 155 | 153 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 void PutInProgressItemToSavedMap(SaveItem* save_item); | 190 void PutInProgressItemToSavedMap(SaveItem* save_item); |
| 193 | 191 |
| 194 // Retrieves the URL to be saved from tab_contents_ variable. | 192 // Retrieves the URL to be saved from tab_contents_ variable. |
| 195 GURL GetUrlToBeSaved(); | 193 GURL GetUrlToBeSaved(); |
| 196 | 194 |
| 197 void CreateDirectoryOnFileThread(const FilePath& website_save_dir, | 195 void CreateDirectoryOnFileThread(const FilePath& website_save_dir, |
| 198 const FilePath& download_save_dir, | 196 const FilePath& download_save_dir, |
| 199 const std::string& mime_type); | 197 const std::string& mime_type); |
| 200 void ContinueGetSaveInfo(const FilePath& suggested_path, | 198 void ContinueGetSaveInfo(const FilePath& suggested_path, |
| 201 bool can_save_as_complete); | 199 bool can_save_as_complete); |
| 202 void ContinueSave(const FilePath& final_name, int index); | |
| 203 | 200 |
| 204 void OnReceivedSavableResourceLinksForCurrentPage( | 201 void OnReceivedSavableResourceLinksForCurrentPage( |
| 205 const std::vector<GURL>& resources_list, | 202 const std::vector<GURL>& resources_list, |
| 206 const std::vector<GURL>& referrers_list, | 203 const std::vector<GURL>& referrers_list, |
| 207 const std::vector<GURL>& frames_list); | 204 const std::vector<GURL>& frames_list); |
| 208 | 205 |
| 209 void OnReceivedSerializedHtmlData(const GURL& frame_url, | 206 void OnReceivedSerializedHtmlData(const GURL& frame_url, |
| 210 const std::string& data, | 207 const std::string& data, |
| 211 int32 status); | 208 int32 status); |
| 212 | 209 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 // from outside. | 302 // from outside. |
| 306 WaitState wait_state_; | 303 WaitState wait_state_; |
| 307 | 304 |
| 308 // Since for one tab, it can only have one SavePackage in same time. | 305 // Since for one tab, it can only have one SavePackage in same time. |
| 309 // Now we actually use render_process_id as tab's unique id. | 306 // Now we actually use render_process_id as tab's unique id. |
| 310 const int tab_id_; | 307 const int tab_id_; |
| 311 | 308 |
| 312 // Unique ID for this SavePackage. | 309 // Unique ID for this SavePackage. |
| 313 const int unique_id_; | 310 const int unique_id_; |
| 314 | 311 |
| 315 // For managing select file dialogs. | |
| 316 scoped_refptr<SelectFileDialog> select_file_dialog_; | |
| 317 | |
| 318 friend class SavePackageTest; | 312 friend class SavePackageTest; |
| 319 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestSuggestedSaveNames); | 313 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestSuggestedSaveNames); |
| 320 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestLongSafePureFilename); | 314 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestLongSafePureFilename); |
| 321 | 315 |
| 322 ScopedRunnableMethodFactory<SavePackage> method_factory_; | 316 ScopedRunnableMethodFactory<SavePackage> method_factory_; |
| 323 | 317 |
| 324 DISALLOW_COPY_AND_ASSIGN(SavePackage); | 318 DISALLOW_COPY_AND_ASSIGN(SavePackage); |
| 325 }; | 319 }; |
| 326 | 320 |
| 327 #endif // CHROME_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ | 321 #endif // CONTENT_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ |
| OLD | NEW |