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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 SaveFileCreateInfo::SaveFileSource save_source, | 104 SaveFileCreateInfo::SaveFileSource save_source, |
105 const base::FilePath& file_full_path, | 105 const base::FilePath& file_full_path, |
106 ResourceContext* context, | 106 ResourceContext* context, |
107 SavePackage* save_package); | 107 SavePackage* save_package); |
108 | 108 |
109 // Notifications sent from the IO thread and run on the file thread: | 109 // Notifications sent from the IO thread and run on the file thread: |
110 void StartSave(SaveFileCreateInfo* info); | 110 void StartSave(SaveFileCreateInfo* info); |
111 void UpdateSaveProgress(int save_id, net::IOBuffer* data, int size); | 111 void UpdateSaveProgress(int save_id, net::IOBuffer* data, int size); |
112 void SaveFinished(int save_id, | 112 void SaveFinished(int save_id, |
113 const GURL& save_url, | 113 const GURL& save_url, |
114 int save_package_id, | 114 SavePackageId save_package_id, |
115 bool is_success); | 115 bool is_success); |
116 | 116 |
117 // Notifications sent from the UI thread and run on the file thread. | 117 // Notifications sent from the UI thread and run on the file thread. |
118 // Cancel a SaveFile instance which has specified save id. | 118 // Cancel a SaveFile instance which has specified save id. |
119 void CancelSave(int save_id); | 119 void CancelSave(int save_id); |
120 | 120 |
121 // Called on the UI thread to remove a save package from SaveFileManager's | 121 // Called on the UI thread to remove a save package from SaveFileManager's |
122 // tracking map. | 122 // tracking map. |
123 void RemoveSaveFile(int save_id, const GURL& save_url, | 123 void RemoveSaveFile(int save_id, const GURL& save_url, |
124 SavePackage* package); | 124 SavePackage* package); |
125 | 125 |
126 // Helper function for deleting specified file. | 126 // Helper function for deleting specified file. |
127 void DeleteDirectoryOrFile(const base::FilePath& full_path, bool is_dir); | 127 void DeleteDirectoryOrFile(const base::FilePath& full_path, bool is_dir); |
128 | 128 |
129 // Runs on file thread to save a file by copying from file system when | 129 // Runs on file thread to save a file by copying from file system when |
130 // original url is using file scheme. | 130 // original url is using file scheme. |
131 void SaveLocalFile(const GURL& original_file_url, | 131 void SaveLocalFile(const GURL& original_file_url, |
132 int save_id, | 132 int save_id, |
133 int save_package_id); | 133 SavePackageId save_package_id); |
134 | 134 |
135 // Renames all the successfully saved files. | 135 // Renames all the successfully saved files. |
136 // |final_names| points to a vector which contains pairs of save ids and | 136 // |final_names| points to a vector which contains pairs of save ids and |
137 // final names of successfully saved files. | 137 // final names of successfully saved files. |
138 void RenameAllFiles(const FinalNameList& final_names, | 138 void RenameAllFiles(const FinalNameList& final_names, |
139 const base::FilePath& resource_dir, | 139 const base::FilePath& resource_dir, |
140 int render_process_id, | 140 int render_process_id, |
141 int render_frame_routing_id, | 141 int render_frame_routing_id, |
142 int save_package_id); | 142 SavePackageId save_package_id); |
143 | 143 |
144 // When the user cancels the saving, we need to remove all remaining saved | 144 // When the user cancels the saving, we need to remove all remaining saved |
145 // files of this page saving job from save_file_map_. | 145 // files of this page saving job from save_file_map_. |
146 void RemoveSavedFileFromFileMap(const SaveIDList & save_ids); | 146 void RemoveSavedFileFromFileMap(const SaveIDList & save_ids); |
147 | 147 |
148 private: | 148 private: |
149 friend class base::RefCountedThreadSafe<SaveFileManager>; | 149 friend class base::RefCountedThreadSafe<SaveFileManager>; |
150 | 150 |
151 ~SaveFileManager(); | 151 ~SaveFileManager(); |
152 | 152 |
153 // A cleanup helper that runs on the file thread. | 153 // A cleanup helper that runs on the file thread. |
154 void OnShutdown(); | 154 void OnShutdown(); |
155 | 155 |
156 // Called only on UI thread to get the SavePackage for a contents's browser | 156 // Called only on UI thread to get the SavePackage for a contents's browser |
157 // context. | 157 // context. |
158 static SavePackage* GetSavePackageFromRenderIds(int render_process_id, | 158 static SavePackage* GetSavePackageFromRenderIds(int render_process_id, |
159 int render_frame_routing_id); | 159 int render_frame_routing_id); |
160 | 160 |
161 // Register a starting request. Associate the save URL with a | 161 // Register a starting request. Associate the save URL with a |
162 // SavePackage for further matching. | 162 // SavePackage for further matching. |
163 void RegisterStartingRequest(const GURL& save_url, | 163 void RegisterStartingRequest(const GURL& save_url, |
164 SavePackage* save_package); | 164 SavePackage* save_package); |
165 // Unregister a start request according save URL, disassociate | 165 // Unregister a start request according save URL, disassociate |
166 // the save URL and SavePackage. | 166 // the save URL and SavePackage. |
167 SavePackage* UnregisterStartingRequest(const GURL& save_url, | 167 SavePackage* UnregisterStartingRequest(const GURL& save_url, |
168 int save_package_id); | 168 SavePackageId save_package_id); |
169 | 169 |
170 // Look up the SavePackage according to save id. | 170 // Look up the SavePackage according to save id. |
171 SavePackage* LookupPackage(int save_id); | 171 SavePackage* LookupPackage(int save_id); |
172 | 172 |
173 // Called only on the file thread. | 173 // Called only on the file thread. |
174 // Look up one in-progress saving item according to save id. | 174 // Look up one in-progress saving item according to save id. |
175 SaveFile* LookupSaveFile(int save_id); | 175 SaveFile* LookupSaveFile(int save_id); |
176 | 176 |
177 // Help function for sending notification of canceling specific request. | 177 // Help function for sending notification of canceling specific request. |
178 void SendCancelRequest(int save_id); | 178 void SendCancelRequest(int save_id); |
179 | 179 |
180 // Notifications sent from the file thread and run on the UI thread. | 180 // Notifications sent from the file thread and run on the UI thread. |
181 | 181 |
182 // Lookup the SaveManager for this WebContents' saving browser context and | 182 // Lookup the SaveManager for this WebContents' saving browser context and |
183 // inform it the saving job has been started. | 183 // inform it the saving job has been started. |
184 void OnStartSave(const SaveFileCreateInfo* info); | 184 void OnStartSave(const SaveFileCreateInfo* info); |
185 // Update the SavePackage with the current state of a started saving job. | 185 // 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. | 186 // If the SavePackage for this saving job is gone, cancel the request. |
187 void OnUpdateSaveProgress(int save_id, | 187 void OnUpdateSaveProgress(int save_id, |
188 int64 bytes_so_far, | 188 int64 bytes_so_far, |
189 bool write_success); | 189 bool write_success); |
190 // Update the SavePackage with the finish state, and remove the request | 190 // Update the SavePackage with the finish state, and remove the request |
191 // tracking entries. | 191 // tracking entries. |
192 void OnSaveFinished(int save_id, int64 bytes_so_far, bool is_success); | 192 void OnSaveFinished(int save_id, int64 bytes_so_far, bool is_success); |
193 // For those requests that do not have valid save id, use | 193 // For those requests that do not have valid save id, use |
194 // map:(url, SavePackage) to find the request and remove it. | 194 // map:(url, SavePackage) to find the request and remove it. |
195 void OnErrorFinished(const GURL& save_url, int save_package_id); | 195 void OnErrorFinished(const GURL& save_url, SavePackageId save_package_id); |
196 // Notifies SavePackage that the whole page saving job is finished. | 196 // Notifies SavePackage that the whole page saving job is finished. |
197 void OnFinishSavePageJob(int render_process_id, | 197 void OnFinishSavePageJob(int render_process_id, |
198 int render_frame_routing_id, | 198 int render_frame_routing_id, |
199 int save_package_id); | 199 SavePackageId save_package_id); |
200 | 200 |
201 // Notifications sent from the UI thread and run on the file thread. | 201 // Notifications sent from the UI thread and run on the file thread. |
202 | 202 |
203 // Deletes a specified file on the file thread. | 203 // Deletes a specified file on the file thread. |
204 void OnDeleteDirectoryOrFile(const base::FilePath& full_path, bool is_dir); | 204 void OnDeleteDirectoryOrFile(const base::FilePath& full_path, bool is_dir); |
205 | 205 |
206 // Notifications sent from the UI thread and run on the IO thread | 206 // Notifications sent from the UI thread and run on the IO thread |
207 | 207 |
208 // Initiates a request for URL to be saved. | 208 // Initiates a request for URL to be saved. |
209 void OnSaveURL(const GURL& url, | 209 void OnSaveURL(const GURL& url, |
210 const Referrer& referrer, | 210 const Referrer& referrer, |
| 211 SavePackageId save_package_id, |
211 int render_process_host_id, | 212 int render_process_host_id, |
212 int render_view_routing_id, | 213 int render_view_routing_id, |
213 int render_frame_routing_id, | 214 int render_frame_routing_id, |
214 int save_package_id, | |
215 ResourceContext* context); | 215 ResourceContext* context); |
216 // Handler for a notification sent to the IO thread for generating save id. | 216 // Handler for a notification sent to the IO thread for generating save id. |
217 void OnRequireSaveJobFromOtherSource(SaveFileCreateInfo* info); | 217 void OnRequireSaveJobFromOtherSource(SaveFileCreateInfo* info); |
218 // Call ResourceDispatcherHostImpl's CancelRequest method to execute cancel | 218 // Call ResourceDispatcherHostImpl's CancelRequest method to execute cancel |
219 // action in the IO thread. | 219 // action in the IO thread. |
220 void ExecuteCancelSaveRequest(int render_process_id, int request_id); | 220 void ExecuteCancelSaveRequest(int render_process_id, int request_id); |
221 | 221 |
222 // Unique ID for the next SaveFile object. | 222 // Unique ID for the next SaveFile object. |
223 int next_id_; | 223 int next_id_; |
224 | 224 |
(...skipping 10 matching lines...) Expand all Loading... |
235 // StartSave(). In this gap, each request does not have save id for tracking. | 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 | 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 | 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 | 238 // as starting requests. For tracking those starting requests, we need to |
239 // have some data structure. | 239 // have some data structure. |
240 // First we use a hashmap to map the request URL to SavePackage, then we use a | 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 | 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 | 242 // hashmap since it is possible to save the same URL in different contents at |
243 // same time. | 243 // same time. |
244 typedef base::hash_map<std::string, SavePackage*> StartingRequestsMap; | 244 typedef base::hash_map<std::string, SavePackage*> StartingRequestsMap; |
245 typedef base::hash_map<int, StartingRequestsMap> | 245 // TODO(lukasza): Use std::unordered_map instead? |
| 246 typedef base::hash_map<SavePackageId, StartingRequestsMap> |
246 ContentsToStartingRequestsMap; | 247 ContentsToStartingRequestsMap; |
247 ContentsToStartingRequestsMap contents_starting_requests_; | 248 ContentsToStartingRequestsMap contents_starting_requests_; |
248 | 249 |
249 DISALLOW_COPY_AND_ASSIGN(SaveFileManager); | 250 DISALLOW_COPY_AND_ASSIGN(SaveFileManager); |
250 }; | 251 }; |
251 | 252 |
252 } // namespace content | 253 } // namespace content |
253 | 254 |
254 #endif // CONTENT_BROWSER_DOWNLOAD_SAVE_FILE_MANAGER_H_ | 255 #endif // CONTENT_BROWSER_DOWNLOAD_SAVE_FILE_MANAGER_H_ |
OLD | NEW |