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 <map> | 7 #include <map> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 // and destroyed on the UI thread. | 40 // and destroyed on the UI thread. |
41 class DownloadItemObserver : public DownloadItem::Observer { | 41 class DownloadItemObserver : public DownloadItem::Observer { |
42 public: | 42 public: |
43 explicit DownloadItemObserver(DownloadItem& download_item); | 43 explicit DownloadItemObserver(DownloadItem& download_item); |
44 | 44 |
45 private: | 45 private: |
46 virtual ~DownloadItemObserver(); | 46 virtual ~DownloadItemObserver(); |
47 | 47 |
48 // DownloadItem::Observer | 48 // DownloadItem::Observer |
49 virtual void OnDownloadUpdated(DownloadItem* download) OVERRIDE; | 49 virtual void OnDownloadUpdated(DownloadItem* download) OVERRIDE; |
50 virtual void OnDownloadOpened(DownloadItem* download) OVERRIDE; | 50 virtual void OnDownloadDestroyed(DownloadItem* download) OVERRIDE; |
51 | 51 |
52 DownloadItem& download_item_; | 52 DownloadItem& download_item_; |
53 | 53 |
54 // Last known target path for the download. | 54 // Last known target path for the download. |
55 FilePath last_target_path_; | 55 FilePath last_target_path_; |
56 | 56 |
57 DISALLOW_COPY_AND_ASSIGN(DownloadItemObserver); | 57 DISALLOW_COPY_AND_ASSIGN(DownloadItemObserver); |
58 }; | 58 }; |
59 | 59 |
60 // Returns true if the given path is in use by a path reservation. | 60 // Returns true if the given path is in use by a path reservation. |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 } | 216 } |
217 | 217 |
218 case DownloadItem::COMPLETE: | 218 case DownloadItem::COMPLETE: |
219 // If the download is complete, then it has already been renamed to the | 219 // If the download is complete, then it has already been renamed to the |
220 // final name. The existence of the file on disk is sufficient to prevent | 220 // final name. The existence of the file on disk is sufficient to prevent |
221 // conflicts from now on. | 221 // conflicts from now on. |
222 | 222 |
223 case DownloadItem::CANCELLED: | 223 case DownloadItem::CANCELLED: |
224 // We no longer need the reservation if the download is being removed. | 224 // We no longer need the reservation if the download is being removed. |
225 | 225 |
226 case DownloadItem::REMOVING: | |
227 // Ditto, but this case shouldn't happen in practice. We should have | |
228 // received another notification beforehand. | |
229 | |
230 case DownloadItem::INTERRUPTED: | 226 case DownloadItem::INTERRUPTED: |
231 // The download filename will need to be re-generated when the download is | 227 // The download filename will need to be re-generated when the download is |
232 // restarted. Holding on to the reservation now would prevent the name | 228 // restarted. Holding on to the reservation now would prevent the name |
233 // from being used for a subsequent retry attempt. | 229 // from being used for a subsequent retry attempt. |
234 | 230 |
235 BrowserThread::PostTask( | 231 BrowserThread::PostTask( |
236 BrowserThread::FILE, FROM_HERE, | 232 BrowserThread::FILE, FROM_HERE, |
237 base::Bind(&RevokeReservation, download->GetGlobalId())); | 233 base::Bind(&RevokeReservation, download->GetGlobalId())); |
238 delete this; | 234 delete this; |
239 break; | 235 break; |
240 | 236 |
241 case DownloadItem::MAX_DOWNLOAD_STATE: | 237 case DownloadItem::MAX_DOWNLOAD_STATE: |
242 // Compiler appeasement. | 238 // Compiler appeasement. |
243 NOTREACHED(); | 239 NOTREACHED(); |
244 } | 240 } |
245 } | 241 } |
246 | 242 |
247 void DownloadItemObserver::OnDownloadOpened(DownloadItem* download) { | 243 void DownloadItemObserver::OnDownloadDestroyed(DownloadItem* download) { |
248 // We shouldn't be tracking reservations for a download that has been | 244 // This shouldn't happen. We should catch either COMPLETE, CANCELLED, or |
249 // externally opened. The tracker should have detached itself when the | 245 // INTERRUPTED first. |
250 // download was complete. | 246 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 247 base::Bind(&RevokeReservation, download->GetGlobalId())); |
| 248 delete this; |
251 } | 249 } |
252 | 250 |
253 } // namespace | 251 } // namespace |
254 | 252 |
255 // static | 253 // static |
256 void DownloadPathReservationTracker::GetReservedPath( | 254 void DownloadPathReservationTracker::GetReservedPath( |
257 DownloadItem& download_item, | 255 DownloadItem& download_item, |
258 const FilePath& target_path, | 256 const FilePath& target_path, |
259 const FilePath& default_path, | 257 const FilePath& default_path, |
260 bool uniquify_path, | 258 bool uniquify_path, |
261 const ReservedPathCallback& callback) { | 259 const ReservedPathCallback& callback) { |
262 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 260 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
263 // Attach an observer to the download item so that we know when the target | 261 // Attach an observer to the download item so that we know when the target |
264 // path changes and/or the download is no longer active. | 262 // path changes and/or the download is no longer active. |
265 new DownloadItemObserver(download_item); | 263 new DownloadItemObserver(download_item); |
266 // DownloadItemObserver deletes itself. | 264 // DownloadItemObserver deletes itself. |
267 | 265 |
268 BrowserThread::PostTask( | 266 BrowserThread::PostTask( |
269 BrowserThread::FILE, FROM_HERE, | 267 BrowserThread::FILE, FROM_HERE, |
270 base::Bind(&CreateReservation, download_item.GetGlobalId(), | 268 base::Bind(&CreateReservation, download_item.GetGlobalId(), |
271 target_path, default_path, uniquify_path, callback)); | 269 target_path, default_path, uniquify_path, callback)); |
272 } | 270 } |
273 | 271 |
274 // static | 272 // static |
275 bool DownloadPathReservationTracker::IsPathInUseForTesting( | 273 bool DownloadPathReservationTracker::IsPathInUseForTesting( |
276 const FilePath& path) { | 274 const FilePath& path) { |
277 return IsPathInUse(path); | 275 return IsPathInUse(path); |
278 } | 276 } |
OLD | NEW |