| 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 25 matching lines...) Expand all Loading... |
| 36 // |----> stats ---->| | 36 // |----> stats ---->| |
| 37 // ui_thread (feedback for user) | 37 // ui_thread (feedback for user) |
| 38 // | 38 // |
| 39 // | 39 // |
| 40 // Cancel operations perform the inverse order when triggered by a user action: | 40 // Cancel operations perform the inverse order when triggered by a user action: |
| 41 // ui_thread (user click) | 41 // ui_thread (user click) |
| 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 tab) 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 | 52 // The SaveFileManager tracks saving requests, mapping from a save ID (unique |
| 53 // (unique integer created in the IO thread) to the SavePackage for the | 53 // integer created in the IO thread) to the SavePackage for the contents where |
| 54 // tab where the saving job was initiated. In the event of a tab closure | 54 // the saving job was initiated. In the event of a contents closure during |
| 55 // during saving, the SavePackage will notice the SaveFileManage to | 55 // saving, the SavePackage will notify the SaveFileManage to cancel all SaveFile |
| 56 // cancel all SaveFile job. | 56 // jobs. |
| 57 | 57 |
| 58 #ifndef CONTENT_BROWSER_DOWNLOAD_SAVE_FILE_MANAGER_H_ | 58 #ifndef CONTENT_BROWSER_DOWNLOAD_SAVE_FILE_MANAGER_H_ |
| 59 #define CONTENT_BROWSER_DOWNLOAD_SAVE_FILE_MANAGER_H_ | 59 #define CONTENT_BROWSER_DOWNLOAD_SAVE_FILE_MANAGER_H_ |
| 60 #pragma once | 60 #pragma once |
| 61 | 61 |
| 62 #include <string> | 62 #include <string> |
| 63 | 63 |
| 64 #include "base/basictypes.h" | 64 #include "base/basictypes.h" |
| 65 #include "base/hash_tables.h" | 65 #include "base/hash_tables.h" |
| 66 #include "base/memory/ref_counted.h" | 66 #include "base/memory/ref_counted.h" |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 void RemoveSavedFileFromFileMap(const SaveIDList & save_ids); | 145 void RemoveSavedFileFromFileMap(const SaveIDList & save_ids); |
| 146 | 146 |
| 147 private: | 147 private: |
| 148 friend class base::RefCountedThreadSafe<SaveFileManager>; | 148 friend class base::RefCountedThreadSafe<SaveFileManager>; |
| 149 | 149 |
| 150 ~SaveFileManager(); | 150 ~SaveFileManager(); |
| 151 | 151 |
| 152 // A cleanup helper that runs on the file thread. | 152 // A cleanup helper that runs on the file thread. |
| 153 void OnShutdown(); | 153 void OnShutdown(); |
| 154 | 154 |
| 155 // Called only on UI thread to get the SavePackage for a tab's browser | 155 // Called only on UI thread to get the SavePackage for a contents's browser |
| 156 // context. | 156 // context. |
| 157 static SavePackage* GetSavePackageFromRenderIds(int render_process_id, | 157 static SavePackage* GetSavePackageFromRenderIds(int render_process_id, |
| 158 int review_view_id); | 158 int review_view_id); |
| 159 | 159 |
| 160 // Register a starting request. Associate the save URL with a | 160 // Register a starting request. Associate the save URL with a |
| 161 // SavePackage for further matching. | 161 // SavePackage for further matching. |
| 162 void RegisterStartingRequest(const GURL& save_url, | 162 void RegisterStartingRequest(const GURL& save_url, |
| 163 SavePackage* save_package); | 163 SavePackage* save_package); |
| 164 // Unregister a start request according save URL, disassociate | 164 // Unregister a start request according save URL, disassociate |
| 165 // the save URL and SavePackage. | 165 // the save URL and SavePackage. |
| 166 SavePackage* UnregisterStartingRequest(const GURL& save_url, | 166 SavePackage* UnregisterStartingRequest(const GURL& save_url, |
| 167 int tab_id); | 167 int contents_id); |
| 168 | 168 |
| 169 // Look up the SavePackage according to save id. | 169 // Look up the SavePackage according to save id. |
| 170 SavePackage* LookupPackage(int save_id); | 170 SavePackage* LookupPackage(int save_id); |
| 171 | 171 |
| 172 // Called only on the file thread. | 172 // Called only on the file thread. |
| 173 // Look up one in-progress saving item according to save id. | 173 // Look up one in-progress saving item according to save id. |
| 174 SaveFile* LookupSaveFile(int save_id); | 174 SaveFile* LookupSaveFile(int save_id); |
| 175 | 175 |
| 176 // Help function for sending notification of canceling specific request. | 176 // Help function for sending notification of canceling specific request. |
| 177 void SendCancelRequest(int save_id); | 177 void SendCancelRequest(int save_id); |
| 178 | 178 |
| 179 // Notifications sent from the file thread and run on the UI thread. | 179 // Notifications sent from the file thread and run on the UI thread. |
| 180 | 180 |
| 181 // Lookup the SaveManager for this TabContents' saving browser context and | 181 // Lookup the SaveManager for this WebContents' saving browser context and |
| 182 // inform it the saving job has been started. | 182 // inform it the saving job has been started. |
| 183 void OnStartSave(const SaveFileCreateInfo* info); | 183 void OnStartSave(const SaveFileCreateInfo* info); |
| 184 // Update the SavePackage with the current state of a started saving job. | 184 // Update the SavePackage with the current state of a started saving job. |
| 185 // If the SavePackage for this saving job is gone, cancel the request. | 185 // If the SavePackage for this saving job is gone, cancel the request. |
| 186 void OnUpdateSaveProgress(int save_id, | 186 void OnUpdateSaveProgress(int save_id, |
| 187 int64 bytes_so_far, | 187 int64 bytes_so_far, |
| 188 bool write_success); | 188 bool write_success); |
| 189 // Update the SavePackage with the finish state, and remove the request | 189 // Update the SavePackage with the finish state, and remove the request |
| 190 // tracking entries. | 190 // tracking entries. |
| 191 void OnSaveFinished(int save_id, int64 bytes_so_far, bool is_success); | 191 void OnSaveFinished(int save_id, int64 bytes_so_far, bool is_success); |
| 192 // For those requests that do not have valid save id, use | 192 // For those requests that do not have valid save id, use |
| 193 // map:(url, SavePackage) to find the request and remove it. | 193 // map:(url, SavePackage) to find the request and remove it. |
| 194 void OnErrorFinished(const GURL& save_url, int tab_id); | 194 void OnErrorFinished(const GURL& save_url, int contents_id); |
| 195 // Notifies SavePackage that the whole page saving job is finished. | 195 // Notifies SavePackage that the whole page saving job is finished. |
| 196 void OnFinishSavePageJob(int render_process_id, | 196 void OnFinishSavePageJob(int render_process_id, |
| 197 int render_view_id, | 197 int render_view_id, |
| 198 int save_package_id); | 198 int save_package_id); |
| 199 | 199 |
| 200 // Notifications sent from the UI thread and run on the file thread. | 200 // Notifications sent from the UI thread and run on the file thread. |
| 201 | 201 |
| 202 // Deletes a specified file on the file thread. | 202 // Deletes a specified file on the file thread. |
| 203 void OnDeleteDirectoryOrFile(const FilePath& full_path, bool is_dir); | 203 void OnDeleteDirectoryOrFile(const FilePath& full_path, bool is_dir); |
| 204 | 204 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 227 // SavePackageMap maps save IDs to their SavePackage. | 227 // SavePackageMap maps save IDs to their SavePackage. |
| 228 typedef base::hash_map<int, SavePackage*> SavePackageMap; | 228 typedef base::hash_map<int, SavePackage*> SavePackageMap; |
| 229 SavePackageMap packages_; | 229 SavePackageMap packages_; |
| 230 | 230 |
| 231 // There is a gap between after calling SaveURL() and before calling | 231 // There is a gap between after calling SaveURL() and before calling |
| 232 // StartSave(). In this gap, each request does not have save id for tracking. | 232 // StartSave(). In this gap, each request does not have save id for tracking. |
| 233 // But sometimes users might want to stop saving job or ResourceDispatcherHost | 233 // But sometimes users might want to stop saving job or ResourceDispatcherHost |
| 234 // calls SaveFinished with save id -1 for network error. We name the requests | 234 // calls SaveFinished with save id -1 for network error. We name the requests |
| 235 // as starting requests. For tracking those starting requests, we need to | 235 // as starting requests. For tracking those starting requests, we need to |
| 236 // have some data structure. | 236 // have some data structure. |
| 237 // First we use a hashmap to map the request URL to SavePackage, then we | 237 // First we use a hashmap to map the request URL to SavePackage, then we use a |
| 238 // use a hashmap to map the tab id (we actually use render_process_id) to the | 238 // hashmap to map the contents id (we actually use render_process_id) to the |
| 239 // hashmap since it is possible to save same URL in different tab at | 239 // hashmap since it is possible to save the same URL in different contents at |
| 240 // same time. | 240 // same time. |
| 241 typedef base::hash_map<std::string, SavePackage*> StartingRequestsMap; | 241 typedef base::hash_map<std::string, SavePackage*> StartingRequestsMap; |
| 242 typedef base::hash_map<int, StartingRequestsMap> TabToStartingRequestsMap; | 242 typedef base::hash_map<int, StartingRequestsMap> |
| 243 TabToStartingRequestsMap tab_starting_requests_; | 243 ContentsToStartingRequestsMap; |
| 244 ContentsToStartingRequestsMap contents_starting_requests_; |
| 244 | 245 |
| 245 DISALLOW_COPY_AND_ASSIGN(SaveFileManager); | 246 DISALLOW_COPY_AND_ASSIGN(SaveFileManager); |
| 246 }; | 247 }; |
| 247 | 248 |
| 248 #endif // CONTENT_BROWSER_DOWNLOAD_SAVE_FILE_MANAGER_H_ | 249 #endif // CONTENT_BROWSER_DOWNLOAD_SAVE_FILE_MANAGER_H_ |
| OLD | NEW |