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 #include "chrome/browser/download/download_path_reservation_tracker.h" | 5 #include "chrome/browser/download/download_path_reservation_tracker.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 public: | 29 public: |
30 DownloadItemObserver(DownloadItem& download_item, | 30 DownloadItemObserver(DownloadItem& download_item, |
31 base::Closure revoke, | 31 base::Closure revoke, |
32 base::Callback<void(const FilePath&)> update); | 32 base::Callback<void(const FilePath&)> update); |
33 | 33 |
34 private: | 34 private: |
35 virtual ~DownloadItemObserver(); | 35 virtual ~DownloadItemObserver(); |
36 | 36 |
37 // DownloadItem::Observer | 37 // DownloadItem::Observer |
38 virtual void OnDownloadUpdated(DownloadItem* download) OVERRIDE; | 38 virtual void OnDownloadUpdated(DownloadItem* download) OVERRIDE; |
39 virtual void OnDownloadDestroyed(DownloadItem* download) OVERRIDE; | 39 virtual void OnDownloadOpened(DownloadItem* download) OVERRIDE; |
40 | 40 |
41 DownloadItem& download_item_; | 41 DownloadItem& download_item_; |
42 | 42 |
43 // Last known target path for the download. | 43 // Last known target path for the download. |
44 FilePath last_target_path_; | 44 FilePath last_target_path_; |
45 | 45 |
46 // Callback to invoke to revoke the path reseration. | 46 // Callback to invoke to revoke the path reseration. |
47 base::Closure revoke_callback_; | 47 base::Closure revoke_callback_; |
48 | 48 |
49 // Callback to invoke to update the path reservation. | 49 // Callback to invoke to update the path reservation. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 } | 82 } |
83 | 83 |
84 case DownloadItem::COMPLETE: | 84 case DownloadItem::COMPLETE: |
85 // If the download is complete, then it has already been renamed to the | 85 // If the download is complete, then it has already been renamed to the |
86 // final name. The existence of the file on disk is sufficient to prevent | 86 // final name. The existence of the file on disk is sufficient to prevent |
87 // conflicts from now on. | 87 // conflicts from now on. |
88 | 88 |
89 case DownloadItem::CANCELLED: | 89 case DownloadItem::CANCELLED: |
90 // We no longer need the reservation if the download is being removed. | 90 // We no longer need the reservation if the download is being removed. |
91 | 91 |
| 92 case DownloadItem::REMOVING: |
| 93 // Ditto, but this case shouldn't happen in practice. We should have |
| 94 // received another notification beforehand. |
| 95 |
92 case DownloadItem::INTERRUPTED: | 96 case DownloadItem::INTERRUPTED: |
93 // The download filename will need to be re-generated when the download is | 97 // The download filename will need to be re-generated when the download is |
94 // restarted. Holding on to the reservation now would prevent the name | 98 // restarted. Holding on to the reservation now would prevent the name |
95 // from being used for a subsequent retry attempt. | 99 // from being used for a subsequent retry attempt. |
96 | 100 |
97 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, revoke_callback_); | 101 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, revoke_callback_); |
98 delete this; | 102 delete this; |
99 break; | 103 break; |
100 | 104 |
101 case DownloadItem::MAX_DOWNLOAD_STATE: | 105 case DownloadItem::MAX_DOWNLOAD_STATE: |
102 // Compiler appeasement. | 106 // Compiler appeasement. |
103 NOTREACHED(); | 107 NOTREACHED(); |
104 } | 108 } |
105 } | 109 } |
106 | 110 |
107 void DownloadItemObserver::OnDownloadDestroyed(DownloadItem* download) { | 111 void DownloadItemObserver::OnDownloadOpened(DownloadItem* download) { |
108 // This shouldn't happen. We should catch either COMPLETE, CANCELLED, or | 112 // We shouldn't be tracking reservations for a download that has been |
109 // INTERRUPTED first. | 113 // externally opened. The tracker should have detached itself when the |
110 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, revoke_callback_); | 114 // download was complete. |
111 delete this; | |
112 } | 115 } |
113 | 116 |
114 } // namespace | 117 } // namespace |
115 | 118 |
116 // static | 119 // static |
117 void DownloadPathReservationTracker::GetReservedPath( | 120 void DownloadPathReservationTracker::GetReservedPath( |
118 DownloadItem& download_item, | 121 DownloadItem& download_item, |
119 const FilePath& target_path, | 122 const FilePath& target_path, |
120 const FilePath& default_path, | 123 const FilePath& default_path, |
121 bool uniquify_path, | 124 bool uniquify_path, |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 reservations_.erase(download_id); | 249 reservations_.erase(download_id); |
247 } | 250 } |
248 | 251 |
249 // static | 252 // static |
250 DownloadPathReservationTracker* DownloadPathReservationTracker::GetInstance() { | 253 DownloadPathReservationTracker* DownloadPathReservationTracker::GetInstance() { |
251 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
252 static base::LazyInstance<DownloadPathReservationTracker> | 255 static base::LazyInstance<DownloadPathReservationTracker> |
253 reservation_tracker = LAZY_INSTANCE_INITIALIZER; | 256 reservation_tracker = LAZY_INSTANCE_INITIALIZER; |
254 return reservation_tracker.Pointer(); | 257 return reservation_tracker.Pointer(); |
255 } | 258 } |
OLD | NEW |