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 // 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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 #include "base/hash_tables.h" | 65 #include "base/hash_tables.h" |
66 #include "base/ref_counted.h" | 66 #include "base/ref_counted.h" |
67 #include "chrome/browser/download/save_types.h" | 67 #include "chrome/browser/download/save_types.h" |
68 | 68 |
69 namespace net { | 69 namespace net { |
70 class IOBuffer; | 70 class IOBuffer; |
71 } | 71 } |
72 class GURL; | 72 class GURL; |
73 class SaveFile; | 73 class SaveFile; |
74 class SavePackage; | 74 class SavePackage; |
75 class MessageLoop; | |
76 class ResourceDispatcherHost; | 75 class ResourceDispatcherHost; |
77 class Task; | 76 class Task; |
78 class URLRequestContextGetter; | 77 class URLRequestContextGetter; |
79 | 78 |
80 class SaveFileManager | 79 class SaveFileManager |
81 : public base::RefCountedThreadSafe<SaveFileManager> { | 80 : public base::RefCountedThreadSafe<SaveFileManager> { |
82 public: | 81 public: |
83 SaveFileManager(MessageLoop* ui_loop, | 82 SaveFileManager(ResourceDispatcherHost* rdh); |
84 MessageLoop* io_loop, | |
85 ResourceDispatcherHost* rdh); | |
86 ~SaveFileManager(); | 83 ~SaveFileManager(); |
87 | 84 |
88 // Lifetime management. | 85 // Lifetime management. |
89 void Shutdown(); | 86 void Shutdown(); |
90 | 87 |
91 // Called on the IO thread | 88 // Called on the IO thread |
92 int GetNextId(); | 89 int GetNextId(); |
93 | 90 |
94 // 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 |
95 // ResourceDispatcherHost on the IO thread. | 92 // ResourceDispatcherHost on the IO thread. |
(...skipping 24 matching lines...) Expand all Loading... |
120 #if !defined(OS_MACOSX) | 117 #if !defined(OS_MACOSX) |
121 // Handler for shell operations sent from the UI to the file thread. Mac OS X | 118 // Handler for shell operations sent from the UI to the file thread. Mac OS X |
122 // requires opening downloads on the UI thread, so it does not use this | 119 // requires opening downloads on the UI thread, so it does not use this |
123 // method. | 120 // method. |
124 void OnShowSavedFileInShell(const FilePath full_path); | 121 void OnShowSavedFileInShell(const FilePath full_path); |
125 #endif | 122 #endif |
126 | 123 |
127 // Helper function for deleting specified file. | 124 // Helper function for deleting specified file. |
128 void DeleteDirectoryOrFile(const FilePath& full_path, bool is_dir); | 125 void DeleteDirectoryOrFile(const FilePath& full_path, bool is_dir); |
129 | 126 |
130 // For posting notifications from the UI and file threads. | |
131 MessageLoop* ui_loop() const { return ui_loop_; } | |
132 MessageLoop* file_loop() const { return file_loop_; } | |
133 | |
134 // Runs on file thread to save a file by copying from file system when | 127 // Runs on file thread to save a file by copying from file system when |
135 // original url is using file scheme. | 128 // original url is using file scheme. |
136 void SaveLocalFile(const GURL& original_file_url, | 129 void SaveLocalFile(const GURL& original_file_url, |
137 int save_id, | 130 int save_id, |
138 int render_process_id); | 131 int render_process_id); |
139 | 132 |
140 // Renames all the successfully saved files. | 133 // Renames all the successfully saved files. |
141 // |final_names| points to a vector which contains pairs of save ids and | 134 // |final_names| points to a vector which contains pairs of save ids and |
142 // final names of successfully saved files. | 135 // final names of successfully saved files. |
143 void RenameAllFiles( | 136 void RenameAllFiles( |
144 const FinalNameList& final_names, | 137 const FinalNameList& final_names, |
145 const FilePath& resource_dir, | 138 const FilePath& resource_dir, |
146 int render_process_id, | 139 int render_process_id, |
147 int render_view_id); | 140 int render_view_id); |
148 | 141 |
149 // When the user cancels the saving, we need to remove all remaining saved | 142 // When the user cancels the saving, we need to remove all remaining saved |
150 // files of this page saving job from save_file_map_. | 143 // files of this page saving job from save_file_map_. |
151 void RemoveSavedFileFromFileMap(const SaveIDList & save_ids); | 144 void RemoveSavedFileFromFileMap(const SaveIDList & save_ids); |
152 | 145 |
153 private: | 146 private: |
154 // A cleanup helper that runs on the file thread. | 147 // A cleanup helper that runs on the file thread. |
155 void OnShutdown(); | 148 void OnShutdown(); |
156 | 149 |
157 // The resource does not come from the network, but we still needs to call | |
158 // this function for getting unique save ID by calling | |
159 // OnRequireSaveJobFromOtherSource in the net IO thread and start saving | |
160 // operation. This function is called on the UI thread. | |
161 void RequireSaveJobFromOtherSource(SaveFileCreateInfo* info); | |
162 | |
163 // Called only on UI thread to get the SavePackage for a tab's profile. | 150 // Called only on UI thread to get the SavePackage for a tab's profile. |
164 static SavePackage* GetSavePackageFromRenderIds(int render_process_id, | 151 static SavePackage* GetSavePackageFromRenderIds(int render_process_id, |
165 int review_view_id); | 152 int review_view_id); |
166 | 153 |
167 // Register a starting request. Associate the save URL with a | 154 // Register a starting request. Associate the save URL with a |
168 // SavePackage for further matching. | 155 // SavePackage for further matching. |
169 void RegisterStartingRequest(const GURL& save_url, | 156 void RegisterStartingRequest(const GURL& save_url, |
170 SavePackage* save_package); | 157 SavePackage* save_package); |
171 // Unregister a start request according save URL, disassociate | 158 // Unregister a start request according save URL, disassociate |
172 // the save URL and SavePackage. | 159 // the save URL and SavePackage. |
(...skipping 19 matching lines...) Expand all Loading... |
192 // If the SavePackage for this saving job is gone, cancel the request. | 179 // If the SavePackage for this saving job is gone, cancel the request. |
193 void OnUpdateSaveProgress(int save_id, | 180 void OnUpdateSaveProgress(int save_id, |
194 int64 bytes_so_far, | 181 int64 bytes_so_far, |
195 bool write_success); | 182 bool write_success); |
196 // Update the SavePackage with the finish state, and remove the request | 183 // Update the SavePackage with the finish state, and remove the request |
197 // tracking entries. | 184 // tracking entries. |
198 void OnSaveFinished(int save_id, int64 bytes_so_far, bool is_success); | 185 void OnSaveFinished(int save_id, int64 bytes_so_far, bool is_success); |
199 // For those requests that do not have valid save id, use | 186 // For those requests that do not have valid save id, use |
200 // map:(url, SavePackage) to find the request and remove it. | 187 // map:(url, SavePackage) to find the request and remove it. |
201 void OnErrorFinished(GURL save_url, int tab_id); | 188 void OnErrorFinished(GURL save_url, int tab_id); |
202 // Handler for a notification sent to the UI thread. | |
203 // The user has requested a cancel in the UI thread, so send a cancel request | |
204 // to stop the network requests in net IO thread. | |
205 void OnCancelSaveRequest(int render_process_id, int request_id); | |
206 // Notifies SavePackage that the whole page saving job is finished. | 189 // Notifies SavePackage that the whole page saving job is finished. |
207 void OnFinishSavePageJob(int render_process_id, int render_view_id); | 190 void OnFinishSavePageJob(int render_process_id, int render_view_id); |
208 | 191 |
209 // Notifications sent from the UI thread and run on the file thread. | 192 // Notifications sent from the UI thread and run on the file thread. |
210 | 193 |
211 // Deletes a specified file on the file thread. | 194 // Deletes a specified file on the file thread. |
212 void OnDeleteDirectoryOrFile(const FilePath& full_path, bool is_dir); | 195 void OnDeleteDirectoryOrFile(const FilePath& full_path, bool is_dir); |
213 | 196 |
214 // Notifications sent from the UI thread and run on the IO thread | 197 // Notifications sent from the UI thread and run on the IO thread |
215 | 198 |
216 // Initiates a request for URL to be saved. | 199 // Initiates a request for URL to be saved. |
217 void OnSaveURL(const GURL& url, | 200 void OnSaveURL(const GURL& url, |
218 const GURL& referrer, | 201 const GURL& referrer, |
219 int render_process_host_id, | 202 int render_process_host_id, |
220 int render_view_id, | 203 int render_view_id, |
221 URLRequestContextGetter* request_context_getter); | 204 URLRequestContextGetter* request_context_getter); |
222 // Handler for a notification sent to the IO thread for generating save id. | 205 // Handler for a notification sent to the IO thread for generating save id. |
223 void OnRequireSaveJobFromOtherSource(SaveFileCreateInfo* info); | 206 void OnRequireSaveJobFromOtherSource(SaveFileCreateInfo* info); |
224 // Call ResourceDispatcherHost's CancelRequest method to execute cancel | 207 // Call ResourceDispatcherHost's CancelRequest method to execute cancel |
225 // action in the IO thread. | 208 // action in the IO thread. |
226 void ExecuteCancelSaveRequest(int render_process_id, int request_id); | 209 void ExecuteCancelSaveRequest(int render_process_id, int request_id); |
227 | 210 |
228 // Unique ID for the next SaveFile object. | 211 // Unique ID for the next SaveFile object. |
229 int next_id_; | 212 int next_id_; |
230 | 213 |
231 // A map of all saving jobs by using save id. | 214 // A map of all saving jobs by using save id. |
232 typedef base::hash_map<int, SaveFile*> SaveFileMap; | 215 typedef base::hash_map<int, SaveFile*> SaveFileMap; |
233 SaveFileMap save_file_map_; | 216 SaveFileMap save_file_map_; |
234 | 217 |
235 // Message loop that the SavePackages live on. | |
236 MessageLoop* ui_loop_; | |
237 | |
238 // We cache the IO loop, we will use it to request resources from network. | |
239 MessageLoop* io_loop_; | |
240 | |
241 // We cache the file loop, we will use it to do real file operation. | |
242 // We guarantee that we won't access them incorrectly during the shutdown | |
243 // process | |
244 MessageLoop* file_loop_; | |
245 | |
246 ResourceDispatcherHost* resource_dispatcher_host_; | 218 ResourceDispatcherHost* resource_dispatcher_host_; |
247 | 219 |
248 // Tracks which SavePackage to send data to, called only on UI thread. | 220 // Tracks which SavePackage to send data to, called only on UI thread. |
249 // SavePackageMap maps save IDs to their SavePackage. | 221 // SavePackageMap maps save IDs to their SavePackage. |
250 typedef base::hash_map<int, SavePackage*> SavePackageMap; | 222 typedef base::hash_map<int, SavePackage*> SavePackageMap; |
251 SavePackageMap packages_; | 223 SavePackageMap packages_; |
252 | 224 |
253 // There is a gap between after calling SaveURL() and before calling | 225 // There is a gap between after calling SaveURL() and before calling |
254 // StartSave(). In this gap, each request does not have save id for tracking. | 226 // StartSave(). In this gap, each request does not have save id for tracking. |
255 // But sometimes users might want to stop saving job or ResourceDispatcherHost | 227 // But sometimes users might want to stop saving job or ResourceDispatcherHost |
256 // calls SaveFinished with save id -1 for network error. We name the requests | 228 // calls SaveFinished with save id -1 for network error. We name the requests |
257 // as starting requests. For tracking those starting requests, we need to | 229 // as starting requests. For tracking those starting requests, we need to |
258 // have some data structure. | 230 // have some data structure. |
259 // First we use a hashmap to map the request URL to SavePackage, then we | 231 // First we use a hashmap to map the request URL to SavePackage, then we |
260 // use a hashmap to map the tab id (we actually use render_process_id) to the | 232 // use a hashmap to map the tab id (we actually use render_process_id) to the |
261 // hashmap since it is possible to save same URL in different tab at | 233 // hashmap since it is possible to save same URL in different tab at |
262 // same time. | 234 // same time. |
263 typedef base::hash_map<std::string, SavePackage*> StartingRequestsMap; | 235 typedef base::hash_map<std::string, SavePackage*> StartingRequestsMap; |
264 typedef base::hash_map<int, StartingRequestsMap> TabToStartingRequestsMap; | 236 typedef base::hash_map<int, StartingRequestsMap> TabToStartingRequestsMap; |
265 TabToStartingRequestsMap tab_starting_requests_; | 237 TabToStartingRequestsMap tab_starting_requests_; |
266 | 238 |
267 DISALLOW_COPY_AND_ASSIGN(SaveFileManager); | 239 DISALLOW_COPY_AND_ASSIGN(SaveFileManager); |
268 }; | 240 }; |
269 | 241 |
270 #endif // CHROME_BROWSER_DOWNLOAD_SAVE_FILE_MANAGER_H__ | 242 #endif // CHROME_BROWSER_DOWNLOAD_SAVE_FILE_MANAGER_H__ |
OLD | NEW |