| 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 CHROME_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ |
| 6 #define CHROME_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ | 6 #define CHROME_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/download/download_manager.h" |
| 19 #include "chrome/browser/ui/shell_dialogs.h" | 20 #include "chrome/browser/ui/shell_dialogs.h" |
| 20 #include "content/browser/tab_contents/tab_contents_observer.h" | 21 #include "content/browser/tab_contents/tab_contents_observer.h" |
| 21 #include "googleurl/src/gurl.h" | 22 #include "googleurl/src/gurl.h" |
| 22 | 23 |
| 23 class DownloadItem; | |
| 24 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; |
| 30 class SaveFileManager; | 29 class SaveFileManager; |
| 31 class SaveItem; | 30 class SaveItem; |
| 32 class SavePackage; | 31 class SavePackage; |
| 33 struct SavePackageParam; | 32 struct SavePackageParam; |
| 34 class TabContentsWrapper; | 33 class TabContentsWrapper; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 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 { | 55 public SelectFileDialog::Listener, |
| 56 public DownloadManager::Observer { |
| 57 public: | 57 public: |
| 58 enum SavePackageType { | 58 enum SavePackageType { |
| 59 // The value of the save type before its set by the user. | 59 // The value of the save type before its set by the user. |
| 60 SAVE_TYPE_UNKNOWN = -1, | 60 SAVE_TYPE_UNKNOWN = -1, |
| 61 // User chose to save only the HTML of the page. | 61 // User chose to save only the HTML of the page. |
| 62 SAVE_AS_ONLY_HTML = 0, | 62 SAVE_AS_ONLY_HTML = 0, |
| 63 // User chose to save complete-html page. | 63 // User chose to save complete-html page. |
| 64 SAVE_AS_COMPLETE_HTML = 1 | 64 SAVE_AS_COMPLETE_HTML = 1 |
| 65 }; | 65 }; |
| 66 | 66 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 // web page. This is available for testing. | 132 // web page. This is available for testing. |
| 133 static void SetShouldPromptUser(bool should_prompt); | 133 static void SetShouldPromptUser(bool should_prompt); |
| 134 | 134 |
| 135 // Check whether we can do the saving page operation for the specified URL. | 135 // Check whether we can do the saving page operation for the specified URL. |
| 136 static bool IsSavableURL(const GURL& url); | 136 static bool IsSavableURL(const GURL& url); |
| 137 | 137 |
| 138 // Check whether we can do the saving page operation for the contents which | 138 // Check whether we can do the saving page operation for the contents which |
| 139 // have the specified MIME type. | 139 // have the specified MIME type. |
| 140 static bool IsSavableContents(const std::string& contents_mime_type); | 140 static bool IsSavableContents(const std::string& contents_mime_type); |
| 141 | 141 |
| 142 // SelectFileDialog::Listener ------------------------------------------------ | 142 // Interfaces ---------------------------------------------------------------- |
| 143 virtual void FileSelected(const FilePath& path, int index, void* params); | 143 |
| 144 virtual void FileSelectionCanceled(void* params); | 144 // SelectFileDialog::Listener. |
| 145 virtual void FileSelected(const FilePath& path, |
| 146 int index, void* params) OVERRIDE; |
| 147 virtual void FileSelectionCanceled(void* params) OVERRIDE; |
| 148 |
| 149 // DownloadManager::Observer. |
| 150 virtual void ModelChanged() OVERRIDE {} |
| 151 virtual void ManagerGoingDown() OVERRIDE; |
| 145 | 152 |
| 146 private: | 153 private: |
| 147 friend class base::RefCountedThreadSafe<SavePackage>; | 154 friend class base::RefCountedThreadSafe<SavePackage>; |
| 148 | 155 |
| 149 // For testing only. | 156 // For testing only. |
| 150 SavePackage(TabContentsWrapper* wrapper, | 157 SavePackage(TabContentsWrapper* wrapper, |
| 151 const FilePath& file_full_path, | 158 const FilePath& file_full_path, |
| 152 const FilePath& directory_full_path); | 159 const FilePath& directory_full_path); |
| 153 | 160 |
| 154 virtual ~SavePackage(); | 161 virtual ~SavePackage(); |
| 155 | 162 |
| 156 // Notes from Init() above applies here as well. | 163 // Notes from Init() above applies here as well. |
| 157 void InternalInit(); | 164 void InternalInit(); |
| 158 | 165 |
| 159 void Stop(); | 166 void Stop(); |
| 160 void CheckFinish(); | 167 void CheckFinish(); |
| 161 void SaveNextFile(bool process_all_remainder_items); | 168 void SaveNextFile(bool process_all_remainder_items); |
| 162 void DoSavingProcess(); | 169 void DoSavingProcess(); |
| 163 | 170 |
| 164 // TabContentsObserver implementation. | 171 // TabContentsObserver implementation. |
| 165 virtual bool OnMessageReceived(const IPC::Message& message); | 172 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| 166 | 173 |
| 167 // Return max length of a path for a specific base directory. | 174 // Return max length of a path for a specific base directory. |
| 168 // This is needed on POSIX, which restrict the length of file names in | 175 // This is needed on POSIX, which restrict the length of file names in |
| 169 // addition to the restriction on the length of path names. | 176 // addition to the restriction on the length of path names. |
| 170 // |base_dir| is assumed to be a directory name with no trailing slash. | 177 // |base_dir| is assumed to be a directory name with no trailing slash. |
| 171 static uint32 GetMaxPathLengthForDirectory(const FilePath& base_dir); | 178 static uint32 GetMaxPathLengthForDirectory(const FilePath& base_dir); |
| 172 | 179 |
| 173 static bool GetSafePureFileName(const FilePath& dir_path, | 180 static bool GetSafePureFileName(const FilePath& dir_path, |
| 174 const FilePath::StringType& file_name_ext, | 181 const FilePath::StringType& file_name_ext, |
| 175 uint32 max_file_path_len, | 182 uint32 max_file_path_len, |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 // Ensures that the file name has a proper extension for supported formats | 252 // Ensures that the file name has a proper extension for supported formats |
| 246 // if necessary. | 253 // if necessary. |
| 247 static FilePath EnsureMimeExtension(const FilePath& name, | 254 static FilePath EnsureMimeExtension(const FilePath& name, |
| 248 const std::string& contents_mime_type); | 255 const std::string& contents_mime_type); |
| 249 | 256 |
| 250 // Returns extension for supported MIME types (for example, for "text/plain" | 257 // Returns extension for supported MIME types (for example, for "text/plain" |
| 251 // it returns "txt"). | 258 // it returns "txt"). |
| 252 static const FilePath::CharType* ExtensionForMimeType( | 259 static const FilePath::CharType* ExtensionForMimeType( |
| 253 const std::string& contents_mime_type); | 260 const std::string& contents_mime_type); |
| 254 | 261 |
| 262 // Callback when Save Page As entry is commited to the history system. |
| 263 void OnDownloadEntryAdded(int32 download_id, int64 db_handle); |
| 264 |
| 265 // Update the download history of this item. |
| 266 void FinalizeDownloadEntry(); |
| 267 |
| 255 // Owning TabContentsWrapper. | 268 // Owning TabContentsWrapper. |
| 256 TabContentsWrapper* wrapper_; | 269 TabContentsWrapper* wrapper_; |
| 257 | 270 |
| 258 typedef std::queue<SaveItem*> SaveItemQueue; | 271 typedef std::queue<SaveItem*> SaveItemQueue; |
| 259 // A queue for items we are about to start saving. | 272 // A queue for items we are about to start saving. |
| 260 SaveItemQueue waiting_item_queue_; | 273 SaveItemQueue waiting_item_queue_; |
| 261 | 274 |
| 262 typedef base::hash_map<int32, SaveItem*> SavedItemMap; | 275 typedef base::hash_map<int32, SaveItem*> SavedItemMap; |
| 263 // saved_success_items_ is map of all saving job which are successfully saved. | 276 // saved_success_items_ is map of all saving job which are successfully saved. |
| 264 SavedItemMap saved_success_items_; | 277 SavedItemMap saved_success_items_; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 // Since for one tab, it can only have one SavePackage in same time. | 321 // 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. | 322 // Now we actually use render_process_id as tab's unique id. |
| 310 const int tab_id_; | 323 const int tab_id_; |
| 311 | 324 |
| 312 // Unique ID for this SavePackage. | 325 // Unique ID for this SavePackage. |
| 313 const int unique_id_; | 326 const int unique_id_; |
| 314 | 327 |
| 315 // For managing select file dialogs. | 328 // For managing select file dialogs. |
| 316 scoped_refptr<SelectFileDialog> select_file_dialog_; | 329 scoped_refptr<SelectFileDialog> select_file_dialog_; |
| 317 | 330 |
| 331 DownloadManager* download_manager_; |
| 332 |
| 318 friend class SavePackageTest; | 333 friend class SavePackageTest; |
| 319 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestSuggestedSaveNames); | 334 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestSuggestedSaveNames); |
| 320 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestLongSafePureFilename); | 335 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestLongSafePureFilename); |
| 321 | 336 |
| 322 ScopedRunnableMethodFactory<SavePackage> method_factory_; | 337 ScopedRunnableMethodFactory<SavePackage> method_factory_; |
| 323 | 338 |
| 324 DISALLOW_COPY_AND_ASSIGN(SavePackage); | 339 DISALLOW_COPY_AND_ASSIGN(SavePackage); |
| 325 }; | 340 }; |
| 326 | 341 |
| 327 #endif // CHROME_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ | 342 #endif // CHROME_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ |
| OLD | NEW |