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 // The DownloadManager object manages the process of downloading, including | 5 // The DownloadManager object manages the process of downloading, including |
6 // updates to the history system and providing the information for displaying | 6 // updates to the history system and providing the information for displaying |
7 // the downloads view in the Destinations tab. There is one DownloadManager per | 7 // the downloads view in the Destinations tab. There is one DownloadManager per |
8 // active profile in Chrome. | 8 // active profile in Chrome. |
9 // | 9 // |
10 // Download observers: | 10 // Download observers: |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 bool Init(Profile* profile); | 116 bool Init(Profile* profile); |
117 | 117 |
118 // Notifications sent from the download thread to the UI thread | 118 // Notifications sent from the download thread to the UI thread |
119 void StartDownload(int32 id); | 119 void StartDownload(int32 id); |
120 void UpdateDownload(int32 download_id, int64 size); | 120 void UpdateDownload(int32 download_id, int64 size); |
121 // |hash| is sha256 hash for the downloaded file. It is empty when the hash | 121 // |hash| is sha256 hash for the downloaded file. It is empty when the hash |
122 // is not available. | 122 // is not available. |
123 void OnResponseCompleted(int32 download_id, int64 size, int os_error, | 123 void OnResponseCompleted(int32 download_id, int64 size, int os_error, |
124 const std::string& hash); | 124 const std::string& hash); |
125 | 125 |
126 // Called from a view when a user clicks a UI button or link. | 126 // Called to initiate download cancel on behalf of an off-thread portion |
127 void DownloadCancelled(int32 download_id); | 127 // of the download system; redirects to DownloadItem. |
128 void RemoveDownload(int64 download_handle); | 128 void CancelDownload(int32 download_handle); |
| 129 |
| 130 // This routine is called from the DownloadItem when a |
| 131 // request is cancelled or interrupted. It removes the download |
| 132 // from all internal queues holding in-progress work, and takes care |
| 133 // of the off-thread aspects of the cancel (stopping the request, |
| 134 // cancelling the download on the file thread). |
| 135 void DownloadStopped(int32 download_id); |
| 136 |
| 137 // Called from DownloadItem when the download is being removed. |
| 138 // Takes care of all history operations, modifying internal queues, |
| 139 // and notifying DownloadManager observers, and actually deletes |
| 140 // the DownloadItem. |
| 141 void RemoveDownload(DownloadItem* download); |
129 | 142 |
130 // Determine if the download is ready for completion, i.e. has had | 143 // Determine if the download is ready for completion, i.e. has had |
131 // all data saved, and completed the filename determination and | 144 // all data saved, and completed the filename determination and |
132 // history insertion. | 145 // history insertion. |
133 bool IsDownloadReadyForCompletion(DownloadItem* download); | 146 bool IsDownloadReadyForCompletion(DownloadItem* download); |
134 | 147 |
135 // If all pre-requisites have been met, complete download processing, i.e. | 148 // If all pre-requisites have been met, complete download processing, i.e. |
136 // do internal cleanup, file rename, and potentially auto-open. | 149 // do internal cleanup, file rename, and potentially auto-open. |
137 // (Dangerous downloads still may block on user acceptance after this | 150 // (Dangerous downloads still may block on user acceptance after this |
138 // point.) | 151 // point.) |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 | 273 |
261 // Assert the named download item is on the correct queues | 274 // Assert the named download item is on the correct queues |
262 // in the DownloadManager. For debugging. | 275 // in the DownloadManager. For debugging. |
263 void AssertQueueStateConsistent(DownloadItem* download); | 276 void AssertQueueStateConsistent(DownloadItem* download); |
264 | 277 |
265 // Get the download item from the history map. Useful after the item has | 278 // Get the download item from the history map. Useful after the item has |
266 // been removed from the active map, or was retrieved from the history DB. | 279 // been removed from the active map, or was retrieved from the history DB. |
267 DownloadItem* GetDownloadItem(int id); | 280 DownloadItem* GetDownloadItem(int id); |
268 | 281 |
269 private: | 282 private: |
270 // For testing. | |
271 friend class DownloadManagerTest; | |
272 friend class MockDownloadManager; | |
273 | |
274 // This class is used to let an incognito DownloadManager observe changes to | 283 // This class is used to let an incognito DownloadManager observe changes to |
275 // a normal DownloadManager, to propagate ModelChanged() calls from the parent | 284 // a normal DownloadManager, to propagate ModelChanged() calls from the parent |
276 // DownloadManager to the observers of the incognito DownloadManager. | 285 // DownloadManager to the observers of the incognito DownloadManager. |
277 class OtherDownloadManagerObserver : public Observer { | 286 class OtherDownloadManagerObserver : public Observer { |
278 public: | 287 public: |
279 explicit OtherDownloadManagerObserver( | 288 explicit OtherDownloadManagerObserver( |
280 DownloadManager* observing_download_manager); | 289 DownloadManager* observing_download_manager); |
281 virtual ~OtherDownloadManagerObserver(); | 290 virtual ~OtherDownloadManagerObserver(); |
282 | 291 |
283 // Observer interface. | 292 // Observer interface. |
284 virtual void ModelChanged(); | 293 virtual void ModelChanged(); |
285 virtual void ManagerGoingDown(); | 294 virtual void ManagerGoingDown(); |
286 | 295 |
287 private: | 296 private: |
288 // The incognito download manager. | 297 // The incognito download manager. |
289 DownloadManager* observing_download_manager_; | 298 DownloadManager* observing_download_manager_; |
290 | 299 |
291 // The original profile's download manager. | 300 // The original profile's download manager. |
292 DownloadManager* observed_download_manager_; | 301 DownloadManager* observed_download_manager_; |
293 }; | 302 }; |
294 | 303 |
295 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; | 304 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; |
296 friend class DeleteTask<DownloadManager>; | 305 friend class DeleteTask<DownloadManager>; |
297 friend class OtherDownloadManagerObserver; | 306 friend class OtherDownloadManagerObserver; |
298 | 307 |
| 308 // For testing. |
| 309 friend class DownloadManagerTest; |
| 310 friend class MockDownloadManager; |
| 311 friend class DownloadTest; |
| 312 |
299 virtual ~DownloadManager(); | 313 virtual ~DownloadManager(); |
300 | 314 |
301 // Called on the FILE thread to check the existence of a downloaded file. | 315 // Called on the FILE thread to check the existence of a downloaded file. |
302 void CheckForFileRemovalOnFileThread(int64 db_handle, const FilePath& path); | 316 void CheckForFileRemovalOnFileThread(int64 db_handle, const FilePath& path); |
303 | 317 |
304 // Called on the UI thread if the FILE thread detects the removal of | 318 // Called on the UI thread if the FILE thread detects the removal of |
305 // the downloaded file. The UI thread updates the state of the file | 319 // the downloaded file. The UI thread updates the state of the file |
306 // and then notifies this update to the file's observer. | 320 // and then notifies this update to the file's observer. |
307 void OnFileRemovalDetected(int64 db_handle); | 321 void OnFileRemovalDetected(int64 db_handle); |
308 | 322 |
309 // Called on the download thread to check whether the suggested file path | 323 // Called on the download thread to check whether the suggested file path |
310 // exists. We don't check if the file exists on the UI thread to avoid UI | 324 // exists. We don't check if the file exists on the UI thread to avoid UI |
311 // stalls from interacting with the file system. | 325 // stalls from interacting with the file system. |
312 void CheckIfSuggestedPathExists(int32 download_id, | 326 void CheckIfSuggestedPathExists(int32 download_id, |
313 DownloadStateInfo state, | 327 DownloadStateInfo state, |
314 const FilePath& default_path); | 328 const FilePath& default_path); |
315 | 329 |
316 // Called on the UI thread once the DownloadManager has determined whether the | 330 // Called on the UI thread once the DownloadManager has determined whether the |
317 // suggested file path exists. | 331 // suggested file path exists. |
318 void OnPathExistenceAvailable(int32 download_id, | 332 void OnPathExistenceAvailable(int32 download_id, |
319 const DownloadStateInfo& new_state); | 333 const DownloadStateInfo& new_state); |
320 | 334 |
321 // Called back after a target path for the file to be downloaded to has been | 335 // Called back after a target path for the file to be downloaded to has been |
322 // determined, either automatically based on the suggested file name, or by | 336 // determined, either automatically based on the suggested file name, or by |
323 // the user in a Save As dialog box. | 337 // the user in a Save As dialog box. |
324 void ContinueDownloadWithPath(DownloadItem* download, | 338 void ContinueDownloadWithPath(DownloadItem* download, |
325 const FilePath& chosen_file); | 339 const FilePath& chosen_file); |
326 | 340 |
327 // Download cancel helper function. | |
328 void DownloadCancelledInternal(int download_id, | |
329 const DownloadRequestHandle& request_handle); | |
330 | |
331 // All data has been downloaded. | 341 // All data has been downloaded. |
332 // |hash| is sha256 hash for the downloaded file. It is empty when the hash | 342 // |hash| is sha256 hash for the downloaded file. It is empty when the hash |
333 // is not available. | 343 // is not available. |
334 void OnAllDataSaved(int32 download_id, int64 size, const std::string& hash); | 344 void OnAllDataSaved(int32 download_id, int64 size, const std::string& hash); |
335 | 345 |
336 // An error occurred in the download. | 346 // An error occurred in the download. |
337 void OnDownloadError(int32 download_id, int64 size, int os_error); | 347 void OnDownloadError(int32 download_id, int64 size, int os_error); |
338 | 348 |
339 // Updates the app icon about the overall download progress. | 349 // Updates the app icon about the overall download progress. |
340 void UpdateAppIcon(); | 350 void UpdateAppIcon(); |
341 | 351 |
342 // Inform observers that the model has changed. | 352 // Inform observers that the model has changed. |
343 void NotifyModelChanged(); | 353 void NotifyModelChanged(); |
344 | 354 |
| 355 // Return all in progress downloads. This includes downloads that |
| 356 // have not yet been entered into the history (all other accessors |
| 357 // only return downloads that have been entered into the history). |
| 358 // This is intended to be used for testing only. |
| 359 void GetInProgressDownloads(std::vector<DownloadItem*>* result); |
| 360 |
| 361 // Replace the file selection dialog used for the download manager. |
| 362 // This is intended to be used for testing only. |
| 363 void SetSelectFileDialog(scoped_refptr<SelectFileDialog> file_dialog); |
| 364 |
345 // Get the download item from the active map. Useful when the item is not | 365 // Get the download item from the active map. Useful when the item is not |
346 // yet in the history map. | 366 // yet in the history map. |
347 DownloadItem* GetActiveDownloadItem(int id); | 367 DownloadItem* GetActiveDownloadItem(int id); |
348 | 368 |
349 // Debugging routine to confirm relationship between below | 369 // Debugging routine to confirm relationship between below |
350 // containers; no-op if NDEBUG. | 370 // containers; no-op if NDEBUG. |
351 void AssertContainersConsistent() const; | 371 void AssertContainersConsistent() const; |
352 | 372 |
353 // Add a DownloadItem to history_downloads_. | 373 // Add a DownloadItem to history_downloads_. |
354 void AddDownloadItemToHistory(DownloadItem* item, int64 db_handle); | 374 void AddDownloadItemToHistory(DownloadItem* item, int64 db_handle); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
426 // The "Save As" dialog box used to ask the user where a file should be | 446 // The "Save As" dialog box used to ask the user where a file should be |
427 // saved. | 447 // saved. |
428 scoped_refptr<SelectFileDialog> select_file_dialog_; | 448 scoped_refptr<SelectFileDialog> select_file_dialog_; |
429 | 449 |
430 scoped_ptr<OtherDownloadManagerObserver> other_download_manager_observer_; | 450 scoped_ptr<OtherDownloadManagerObserver> other_download_manager_observer_; |
431 | 451 |
432 DISALLOW_COPY_AND_ASSIGN(DownloadManager); | 452 DISALLOW_COPY_AND_ASSIGN(DownloadManager); |
433 }; | 453 }; |
434 | 454 |
435 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_H_ | 455 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_H_ |
OLD | NEW |