| 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 // DownloadHistory manages persisting DownloadItems to the history service by | 5 // DownloadHistory manages persisting DownloadItems to the history service by |
| 6 // observing a single DownloadManager and all its DownloadItems using an | 6 // observing a single DownloadManager and all its DownloadItems using an |
| 7 // AllDownloadItemNotifier. | 7 // AllDownloadItemNotifier. |
| 8 // | 8 // |
| 9 // DownloadHistory decides whether and when to add items to, remove items from, | 9 // DownloadHistory decides whether and when to add items to, remove items from, |
| 10 // and update items in the database. DownloadHistory uses DownloadHistoryData to | 10 // and update items in the database. DownloadHistory uses DownloadHistoryData to |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 } | 64 } |
| 65 | 65 |
| 66 static const DownloadHistoryData* Get(const content::DownloadItem* item) { | 66 static const DownloadHistoryData* Get(const content::DownloadItem* item) { |
| 67 const base::SupportsUserData::Data* data = item->GetUserData(kKey); | 67 const base::SupportsUserData::Data* data = item->GetUserData(kKey); |
| 68 return (data == NULL) ? NULL | 68 return (data == NULL) ? NULL |
| 69 : static_cast<const DownloadHistoryData*>(data); | 69 : static_cast<const DownloadHistoryData*>(data); |
| 70 } | 70 } |
| 71 | 71 |
| 72 explicit DownloadHistoryData(content::DownloadItem* item) | 72 explicit DownloadHistoryData(content::DownloadItem* item) |
| 73 : state_(NOT_PERSISTED), | 73 : state_(NOT_PERSISTED), |
| 74 was_restored_from_history_(false) { | 74 was_restored_from_history_(false), |
| 75 was_revived_(false) { |
| 75 item->SetUserData(kKey, this); | 76 item->SetUserData(kKey, this); |
| 76 } | 77 } |
| 77 | 78 |
| 78 ~DownloadHistoryData() override {} | 79 ~DownloadHistoryData() override {} |
| 79 | 80 |
| 80 PersistenceState state() const { return state_; } | 81 PersistenceState state() const { return state_; } |
| 81 void SetState(PersistenceState s) { state_ = s; } | 82 void SetState(PersistenceState s) { state_ = s; } |
| 82 | 83 |
| 83 bool was_restored_from_history() const { return was_restored_from_history_; } | 84 bool was_restored_from_history() const { return was_restored_from_history_; } |
| 84 void set_was_restored_from_history(bool value) { | 85 void set_was_restored_from_history(bool value) { |
| 85 was_restored_from_history_ = value; | 86 was_restored_from_history_ = value; |
| 86 } | 87 } |
| 87 | 88 |
| 89 bool was_revived() const { return was_revived_; } |
| 90 void set_was_revived(bool revived) { was_revived_ = revived; } |
| 91 |
| 88 // This allows DownloadHistory::OnDownloadUpdated() to see what changed in a | 92 // This allows DownloadHistory::OnDownloadUpdated() to see what changed in a |
| 89 // DownloadItem if anything, in order to prevent writing to the database | 93 // DownloadItem if anything, in order to prevent writing to the database |
| 90 // unnecessarily. It is nullified when the item is no longer in progress in | 94 // unnecessarily. It is nullified when the item is no longer in progress in |
| 91 // order to save memory. | 95 // order to save memory. |
| 92 history::DownloadRow* info() { return info_.get(); } | 96 history::DownloadRow* info() { return info_.get(); } |
| 93 void set_info(const history::DownloadRow& i) { | 97 void set_info(const history::DownloadRow& i) { |
| 94 info_.reset(new history::DownloadRow(i)); | 98 info_.reset(new history::DownloadRow(i)); |
| 95 } | 99 } |
| 96 void clear_info() { | 100 void clear_info() { |
| 97 info_.reset(); | 101 info_.reset(); |
| 98 } | 102 } |
| 99 | 103 |
| 100 private: | 104 private: |
| 101 static const char kKey[]; | 105 static const char kKey[]; |
| 102 | 106 |
| 103 PersistenceState state_; | 107 PersistenceState state_; |
| 104 scoped_ptr<history::DownloadRow> info_; | 108 scoped_ptr<history::DownloadRow> info_; |
| 105 bool was_restored_from_history_; | 109 bool was_restored_from_history_; |
| 110 bool was_revived_; |
| 106 | 111 |
| 107 DISALLOW_COPY_AND_ASSIGN(DownloadHistoryData); | 112 DISALLOW_COPY_AND_ASSIGN(DownloadHistoryData); |
| 108 }; | 113 }; |
| 109 | 114 |
| 110 const char DownloadHistoryData::kKey[] = | 115 const char DownloadHistoryData::kKey[] = |
| 111 "DownloadItem DownloadHistoryData"; | 116 "DownloadItem DownloadHistoryData"; |
| 112 | 117 |
| 113 history::DownloadRow GetDownloadRow( | 118 history::DownloadRow GetDownloadRow( |
| 114 content::DownloadItem* item) { | 119 content::DownloadItem* item) { |
| 115 std::string by_ext_id, by_ext_name; | 120 std::string by_ext_id, by_ext_name; |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 // observer that can also see data->state(), which is the only thing that | 370 // observer that can also see data->state(), which is the only thing that |
| 366 // ItemAdded() changed. | 371 // ItemAdded() changed. |
| 367 OnDownloadUpdated(notifier_.GetManager(), item); | 372 OnDownloadUpdated(notifier_.GetManager(), item); |
| 368 } | 373 } |
| 369 | 374 |
| 370 void DownloadHistory::OnDownloadCreated( | 375 void DownloadHistory::OnDownloadCreated( |
| 371 content::DownloadManager* manager, content::DownloadItem* item) { | 376 content::DownloadManager* manager, content::DownloadItem* item) { |
| 372 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 377 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 373 | 378 |
| 374 // All downloads should pass through OnDownloadCreated exactly once. | 379 // All downloads should pass through OnDownloadCreated exactly once. |
| 375 CHECK(!DownloadHistoryData::Get(item)); | 380 DownloadHistoryData* data = DownloadHistoryData::Get(item); |
| 376 DownloadHistoryData* data = new DownloadHistoryData(item); | 381 CHECK(!data || data->was_revived()); |
| 382 |
| 383 if (!data) |
| 384 data = new DownloadHistoryData(item); |
| 385 |
| 377 if (item->GetId() == loading_id_) { | 386 if (item->GetId() == loading_id_) { |
| 378 data->SetState(DownloadHistoryData::PERSISTED); | 387 data->SetState(DownloadHistoryData::PERSISTED); |
| 379 data->set_was_restored_from_history(true); | 388 data->set_was_restored_from_history(true); |
| 380 loading_id_ = content::DownloadItem::kInvalidId; | 389 loading_id_ = content::DownloadItem::kInvalidId; |
| 381 } | 390 } |
| 382 if (item->GetState() == content::DownloadItem::IN_PROGRESS) { | 391 if (item->GetState() == content::DownloadItem::IN_PROGRESS) { |
| 383 data->set_info(GetDownloadRow(item)); | 392 data->set_info(GetDownloadRow(item)); |
| 384 } | 393 } |
| 385 MaybeAddToHistory(item); | 394 MaybeAddToHistory(item); |
| 386 } | 395 } |
| 387 | 396 |
| 397 void DownloadHistory::OnDownloadRevived( |
| 398 content::DownloadManager* manager, content::DownloadItem* item) { |
| 399 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 400 |
| 401 DownloadHistoryData* data = DownloadHistoryData::Get(item); |
| 402 if (data) |
| 403 data->set_was_revived(true); |
| 404 } |
| 405 |
| 388 void DownloadHistory::OnDownloadUpdated( | 406 void DownloadHistory::OnDownloadUpdated( |
| 389 content::DownloadManager* manager, content::DownloadItem* item) { | 407 content::DownloadManager* manager, content::DownloadItem* item) { |
| 390 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 408 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 391 | 409 |
| 392 DownloadHistoryData* data = DownloadHistoryData::Get(item); | 410 DownloadHistoryData* data = DownloadHistoryData::Get(item); |
| 393 if (data->state() == DownloadHistoryData::NOT_PERSISTED) { | 411 if (data->state() == DownloadHistoryData::NOT_PERSISTED) { |
| 394 MaybeAddToHistory(item); | 412 MaybeAddToHistory(item); |
| 395 return; | 413 return; |
| 396 } | 414 } |
| 397 if (item->IsTemporary()) { | 415 if (item->IsTemporary()) { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 455 removing_ids_.insert(download_id); | 473 removing_ids_.insert(download_id); |
| 456 } | 474 } |
| 457 | 475 |
| 458 void DownloadHistory::RemoveDownloadsBatch() { | 476 void DownloadHistory::RemoveDownloadsBatch() { |
| 459 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 477 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 460 IdSet remove_ids; | 478 IdSet remove_ids; |
| 461 removing_ids_.swap(remove_ids); | 479 removing_ids_.swap(remove_ids); |
| 462 history_->RemoveDownloads(remove_ids); | 480 history_->RemoveDownloads(remove_ids); |
| 463 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadsRemoved(remove_ids)); | 481 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadsRemoved(remove_ids)); |
| 464 } | 482 } |
| OLD | NEW |