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