| 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 // Objects that handle file operations for saving files, on the file thread. | 5 // Objects that handle file operations for saving files, on the file thread. |
| 6 // | 6 // |
| 7 // The SaveFileManager owns a set of SaveFile objects, each of which connects | 7 // The SaveFileManager owns a set of SaveFile objects, each of which connects |
| 8 // with a SaveItem object which belongs to one SavePackage and runs on the file | 8 // with a SaveItem object which belongs to one SavePackage and runs on the file |
| 9 // thread for saving data in order to avoid disk activity on either network IO | 9 // thread for saving data in order to avoid disk activity on either network IO |
| 10 // thread or the UI thread. It coordinates the notifications from the network | 10 // thread or the UI thread. It coordinates the notifications from the network |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 // |----> cancel command ---->| | 42 // |----> cancel command ---->| |
| 43 // | | file_thread (close file) | 43 // | | file_thread (close file) |
| 44 // | |---------------------> cancel command ---->| | 44 // | |---------------------> cancel command ---->| |
| 45 // | io_thread (stops net IO | 45 // | io_thread (stops net IO |
| 46 // ui_thread (user close contents) for saving) | 46 // ui_thread (user close contents) for saving) |
| 47 // |----> cancel command ---->| | 47 // |----> cancel command ---->| |
| 48 // Render process(stop serializing DOM and sending | 48 // Render process(stop serializing DOM and sending |
| 49 // data) | 49 // data) |
| 50 // | 50 // |
| 51 // | 51 // |
| 52 // The SaveFileManager tracks saving requests, mapping from a save ID (unique | 52 // The SaveFileManager tracks saving requests, mapping from a save item id to |
| 53 // integer created in the IO thread) to the SavePackage for the contents where | 53 // the SavePackage for the contents where the saving job was initiated. In the |
| 54 // the saving job was initiated. In the event of a contents closure during | 54 // event of a contents closure during saving, the SavePackage will notify the |
| 55 // saving, the SavePackage will notify the SaveFileManage to cancel all SaveFile | 55 // SaveFileManage to cancel all SaveFile jobs. |
| 56 // jobs. | |
| 57 | 56 |
| 58 #ifndef CONTENT_BROWSER_DOWNLOAD_SAVE_FILE_MANAGER_H_ | 57 #ifndef CONTENT_BROWSER_DOWNLOAD_SAVE_FILE_MANAGER_H_ |
| 59 #define CONTENT_BROWSER_DOWNLOAD_SAVE_FILE_MANAGER_H_ | 58 #define CONTENT_BROWSER_DOWNLOAD_SAVE_FILE_MANAGER_H_ |
| 60 | 59 |
| 61 #include <string> | 60 #include <string> |
| 62 | 61 |
| 63 #include "base/basictypes.h" | 62 #include "base/basictypes.h" |
| 64 #include "base/containers/hash_tables.h" | 63 #include "base/containers/hash_tables.h" |
| 65 #include "base/memory/ref_counted.h" | 64 #include "base/memory/ref_counted.h" |
| 66 #include "content/browser/download/save_types.h" | 65 #include "content/browser/download/save_types.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 82 class SavePackage; | 81 class SavePackage; |
| 83 struct Referrer; | 82 struct Referrer; |
| 84 | 83 |
| 85 class SaveFileManager : public base::RefCountedThreadSafe<SaveFileManager> { | 84 class SaveFileManager : public base::RefCountedThreadSafe<SaveFileManager> { |
| 86 public: | 85 public: |
| 87 SaveFileManager(); | 86 SaveFileManager(); |
| 88 | 87 |
| 89 // Lifetime management. | 88 // Lifetime management. |
| 90 CONTENT_EXPORT void Shutdown(); | 89 CONTENT_EXPORT void Shutdown(); |
| 91 | 90 |
| 92 // Called on the IO thread. This generates unique IDs for | |
| 93 // SaveFileResourceHandler objects (there's one per file in a SavePackage). | |
| 94 // Note that this is different from the SavePackage's id. | |
| 95 int GetNextId(); | |
| 96 | |
| 97 // Save the specified URL. Called on the UI thread and forwarded to the | 91 // Save the specified URL. Called on the UI thread and forwarded to the |
| 98 // ResourceDispatcherHostImpl on the IO thread. | 92 // ResourceDispatcherHostImpl on the IO thread. |
| 99 void SaveURL(const GURL& url, | 93 void SaveURL(int save_item_id, |
| 94 const GURL& url, |
| 100 const Referrer& referrer, | 95 const Referrer& referrer, |
| 101 int render_process_host_id, | 96 int render_process_host_id, |
| 102 int render_view_routing_id, | 97 int render_view_routing_id, |
| 103 int render_frame_routing_id, | 98 int render_frame_routing_id, |
| 104 SaveFileCreateInfo::SaveFileSource save_source, | 99 SaveFileCreateInfo::SaveFileSource save_source, |
| 105 const base::FilePath& file_full_path, | 100 const base::FilePath& file_full_path, |
| 106 ResourceContext* context, | 101 ResourceContext* context, |
| 107 SavePackage* save_package); | 102 SavePackage* save_package); |
| 108 | 103 |
| 109 // Notifications sent from the IO thread and run on the file thread: | 104 // Notifications sent from the IO thread and run on the file thread: |
| 110 void StartSave(SaveFileCreateInfo* info); | 105 void StartSave(SaveFileCreateInfo* info); |
| 111 void UpdateSaveProgress(int save_id, net::IOBuffer* data, int size); | 106 void UpdateSaveProgress(int save_item_id, net::IOBuffer* data, int size); |
| 112 void SaveFinished(int save_id, | 107 void SaveFinished(int save_item_id, int save_package_id, bool is_success); |
| 113 const GURL& save_url, | |
| 114 int save_package_id, | |
| 115 bool is_success); | |
| 116 | 108 |
| 117 // Notifications sent from the UI thread and run on the file thread. | 109 // Notifications sent from the UI thread and run on the file thread. |
| 118 // Cancel a SaveFile instance which has specified save id. | 110 // Cancel a SaveFile instance which has specified save item id. |
| 119 void CancelSave(int save_id); | 111 void CancelSave(int save_item_id); |
| 120 | 112 |
| 121 // Called on the UI thread to remove a save package from SaveFileManager's | 113 // Called on the UI thread to remove a save package from SaveFileManager's |
| 122 // tracking map. | 114 // tracking map. |
| 123 void RemoveSaveFile(int save_id, const GURL& save_url, | 115 void RemoveSaveFile(int save_item_id, SavePackage* package); |
| 124 SavePackage* package); | |
| 125 | 116 |
| 126 // Helper function for deleting specified file. | 117 // Helper function for deleting specified file. |
| 127 void DeleteDirectoryOrFile(const base::FilePath& full_path, bool is_dir); | 118 void DeleteDirectoryOrFile(const base::FilePath& full_path, bool is_dir); |
| 128 | 119 |
| 129 // Runs on file thread to save a file by copying from file system when | 120 // Runs on file thread to save a file by copying from file system when |
| 130 // original url is using file scheme. | 121 // original url is using file scheme. |
| 131 void SaveLocalFile(const GURL& original_file_url, | 122 void SaveLocalFile(const GURL& original_file_url, |
| 132 int save_id, | 123 int save_item_id, |
| 133 int save_package_id); | 124 int save_package_id); |
| 134 | 125 |
| 135 // Renames all the successfully saved files. | 126 // Renames all the successfully saved files. |
| 136 // |final_names| points to a vector which contains pairs of save ids and | 127 void RenameAllFiles(const FinalNamesMap& final_names, |
| 137 // final names of successfully saved files. | |
| 138 void RenameAllFiles(const FinalNameList& final_names, | |
| 139 const base::FilePath& resource_dir, | 128 const base::FilePath& resource_dir, |
| 140 int render_process_id, | 129 int render_process_id, |
| 141 int render_frame_routing_id, | 130 int render_frame_routing_id, |
| 142 int save_package_id); | 131 int save_package_id); |
| 143 | 132 |
| 144 // When the user cancels the saving, we need to remove all remaining saved | 133 // When the user cancels the saving, we need to remove all remaining saved |
| 145 // files of this page saving job from save_file_map_. | 134 // files of this page saving job from save_file_map_. |
| 146 void RemoveSavedFileFromFileMap(const SaveIDList & save_ids); | 135 void RemoveSavedFileFromFileMap(const std::vector<int>& save_item_ids); |
| 147 | 136 |
| 148 private: | 137 private: |
| 149 friend class base::RefCountedThreadSafe<SaveFileManager>; | 138 friend class base::RefCountedThreadSafe<SaveFileManager>; |
| 150 | 139 |
| 151 ~SaveFileManager(); | 140 ~SaveFileManager(); |
| 152 | 141 |
| 153 // A cleanup helper that runs on the file thread. | 142 // A cleanup helper that runs on the file thread. |
| 154 void OnShutdown(); | 143 void OnShutdown(); |
| 155 | 144 |
| 156 // Called only on UI thread to get the SavePackage for a contents's browser | 145 // Called only on UI thread to get the SavePackage for a contents's browser |
| 157 // context. | 146 // context. |
| 158 static SavePackage* GetSavePackageFromRenderIds(int render_process_id, | 147 static SavePackage* GetSavePackageFromRenderIds(int render_process_id, |
| 159 int render_frame_routing_id); | 148 int render_frame_routing_id); |
| 160 | 149 |
| 161 // Register a starting request. Associate the save URL with a | 150 // Look up the SavePackage according to save item id. |
| 162 // SavePackage for further matching. | 151 SavePackage* LookupPackage(int save_item_id); |
| 163 void RegisterStartingRequest(const GURL& save_url, | |
| 164 SavePackage* save_package); | |
| 165 // Unregister a start request according save URL, disassociate | |
| 166 // the save URL and SavePackage. | |
| 167 SavePackage* UnregisterStartingRequest(const GURL& save_url, | |
| 168 int save_package_id); | |
| 169 | |
| 170 // Look up the SavePackage according to save id. | |
| 171 SavePackage* LookupPackage(int save_id); | |
| 172 | 152 |
| 173 // Called only on the file thread. | 153 // Called only on the file thread. |
| 174 // Look up one in-progress saving item according to save id. | 154 // Look up one in-progress saving item according to save item id. |
| 175 SaveFile* LookupSaveFile(int save_id); | 155 SaveFile* LookupSaveFile(int save_item_id); |
| 176 | 156 |
| 177 // Help function for sending notification of canceling specific request. | 157 // Help function for sending notification of canceling specific request. |
| 178 void SendCancelRequest(int save_id); | 158 void SendCancelRequest(int save_item_id); |
| 179 | 159 |
| 180 // Notifications sent from the file thread and run on the UI thread. | 160 // Notifications sent from the file thread and run on the UI thread. |
| 181 | 161 |
| 182 // Lookup the SaveManager for this WebContents' saving browser context and | 162 // Lookup the SaveManager for this WebContents' saving browser context and |
| 183 // inform it the saving job has been started. | 163 // inform it the saving job has been started. |
| 184 void OnStartSave(const SaveFileCreateInfo* info); | 164 void OnStartSave(const SaveFileCreateInfo* info); |
| 185 // Update the SavePackage with the current state of a started saving job. | 165 // Update the SavePackage with the current state of a started saving job. |
| 186 // If the SavePackage for this saving job is gone, cancel the request. | 166 // If the SavePackage for this saving job is gone, cancel the request. |
| 187 void OnUpdateSaveProgress(int save_id, | 167 void OnUpdateSaveProgress(int save_item_id, |
| 188 int64 bytes_so_far, | 168 int64 bytes_so_far, |
| 189 bool write_success); | 169 bool write_success); |
| 190 // Update the SavePackage with the finish state, and remove the request | 170 // Update the SavePackage with the finish state, and remove the request |
| 191 // tracking entries. | 171 // tracking entries. |
| 192 void OnSaveFinished(int save_id, int64 bytes_so_far, bool is_success); | 172 void OnSaveFinished(int save_item_id, int64 bytes_so_far, bool is_success); |
| 193 // For those requests that do not have valid save id, use | |
| 194 // map:(url, SavePackage) to find the request and remove it. | |
| 195 void OnErrorFinished(const GURL& save_url, int save_package_id); | |
| 196 // Notifies SavePackage that the whole page saving job is finished. | 173 // Notifies SavePackage that the whole page saving job is finished. |
| 197 void OnFinishSavePageJob(int render_process_id, | 174 void OnFinishSavePageJob(int render_process_id, |
| 198 int render_frame_routing_id, | 175 int render_frame_routing_id, |
| 199 int save_package_id); | 176 int save_package_id); |
| 200 | 177 |
| 201 // Notifications sent from the UI thread and run on the file thread. | 178 // Notifications sent from the UI thread and run on the file thread. |
| 202 | 179 |
| 203 // Deletes a specified file on the file thread. | 180 // Deletes a specified file on the file thread. |
| 204 void OnDeleteDirectoryOrFile(const base::FilePath& full_path, bool is_dir); | 181 void OnDeleteDirectoryOrFile(const base::FilePath& full_path, bool is_dir); |
| 205 | 182 |
| 206 // Notifications sent from the UI thread and run on the IO thread | 183 // Notifications sent from the UI thread and run on the IO thread |
| 207 | 184 |
| 208 // Initiates a request for URL to be saved. | 185 // Initiates a request for URL to be saved. |
| 209 void OnSaveURL(const GURL& url, | 186 void OnSaveURL(const GURL& url, |
| 210 const Referrer& referrer, | 187 const Referrer& referrer, |
| 188 int save_item_id, |
| 189 int save_package_id, |
| 211 int render_process_host_id, | 190 int render_process_host_id, |
| 212 int render_view_routing_id, | 191 int render_view_routing_id, |
| 213 int render_frame_routing_id, | 192 int render_frame_routing_id, |
| 214 int save_package_id, | |
| 215 ResourceContext* context); | 193 ResourceContext* context); |
| 216 // Handler for a notification sent to the IO thread for generating save id. | |
| 217 void OnRequireSaveJobFromOtherSource(SaveFileCreateInfo* info); | |
| 218 // Call ResourceDispatcherHostImpl's CancelRequest method to execute cancel | 194 // Call ResourceDispatcherHostImpl's CancelRequest method to execute cancel |
| 219 // action in the IO thread. | 195 // action in the IO thread. |
| 220 void ExecuteCancelSaveRequest(int render_process_id, int request_id); | 196 void ExecuteCancelSaveRequest(int render_process_id, int request_id); |
| 221 | 197 |
| 222 // Unique ID for the next SaveFile object. | 198 // A map from save_item_id into SaveFiles. |
| 223 int next_id_; | |
| 224 | |
| 225 // A map of all saving jobs by using save id. | |
| 226 typedef base::hash_map<int, SaveFile*> SaveFileMap; | 199 typedef base::hash_map<int, SaveFile*> SaveFileMap; |
| 227 SaveFileMap save_file_map_; | 200 SaveFileMap save_file_map_; |
| 228 | 201 |
| 229 // Tracks which SavePackage to send data to, called only on UI thread. | 202 // Tracks which SavePackage to send data to, called only on UI thread. |
| 230 // SavePackageMap maps save IDs to their SavePackage. | 203 // SavePackageMap maps save item ids to their SavePackage. |
| 231 typedef base::hash_map<int, SavePackage*> SavePackageMap; | 204 typedef base::hash_map<int, SavePackage*> SavePackageMap; |
| 232 SavePackageMap packages_; | 205 SavePackageMap packages_; |
| 233 | 206 |
| 234 // There is a gap between after calling SaveURL() and before calling | |
| 235 // StartSave(). In this gap, each request does not have save id for tracking. | |
| 236 // But sometimes users might want to stop saving job or ResourceDispatcherHost | |
| 237 // calls SaveFinished with save id -1 for network error. We name the requests | |
| 238 // as starting requests. For tracking those starting requests, we need to | |
| 239 // have some data structure. | |
| 240 // First we use a hashmap to map the request URL to SavePackage, then we use a | |
| 241 // hashmap to map the contents id (we actually use render_process_id) to the | |
| 242 // hashmap since it is possible to save the same URL in different contents at | |
| 243 // same time. | |
| 244 typedef base::hash_map<std::string, SavePackage*> StartingRequestsMap; | |
| 245 typedef base::hash_map<int, StartingRequestsMap> | |
| 246 ContentsToStartingRequestsMap; | |
| 247 ContentsToStartingRequestsMap contents_starting_requests_; | |
| 248 | |
| 249 DISALLOW_COPY_AND_ASSIGN(SaveFileManager); | 207 DISALLOW_COPY_AND_ASSIGN(SaveFileManager); |
| 250 }; | 208 }; |
| 251 | 209 |
| 252 } // namespace content | 210 } // namespace content |
| 253 | 211 |
| 254 #endif // CONTENT_BROWSER_DOWNLOAD_SAVE_FILE_MANAGER_H_ | 212 #endif // CONTENT_BROWSER_DOWNLOAD_SAVE_FILE_MANAGER_H_ |
| OLD | NEW |