| 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 10 matching lines...) Expand all Loading... |
| 21 // item is still being added to the database, DownloadHistory uses | 21 // item is still being added to the database, DownloadHistory uses |
| 22 // |removed_while_adding_| to remember to remove the item when its ItemAdded() | 22 // |removed_while_adding_| to remember to remove the item when its ItemAdded() |
| 23 // callback is called. All callbacks are bound with a weak pointer to | 23 // callback is called. All callbacks are bound with a weak pointer to |
| 24 // DownloadHistory to prevent use-after-free bugs. | 24 // DownloadHistory to prevent use-after-free bugs. |
| 25 // ChromeDownloadManagerDelegate owns DownloadHistory, and deletes it in | 25 // ChromeDownloadManagerDelegate owns DownloadHistory, and deletes it in |
| 26 // Shutdown(), which is called by DownloadManagerImpl::Shutdown() after all | 26 // Shutdown(), which is called by DownloadManagerImpl::Shutdown() after all |
| 27 // DownloadItems are destroyed. | 27 // DownloadItems are destroyed. |
| 28 | 28 |
| 29 #include "chrome/browser/download/download_history.h" | 29 #include "chrome/browser/download/download_history.h" |
| 30 | 30 |
| 31 #include <utility> |
| 32 |
| 31 #include "base/macros.h" | 33 #include "base/macros.h" |
| 32 #include "base/metrics/histogram.h" | 34 #include "base/metrics/histogram.h" |
| 33 #include "chrome/browser/download/download_crx_util.h" | 35 #include "chrome/browser/download/download_crx_util.h" |
| 34 #include "components/history/content/browser/download_constants_utils.h" | 36 #include "components/history/content/browser/download_constants_utils.h" |
| 35 #include "components/history/core/browser/download_database.h" | 37 #include "components/history/core/browser/download_database.h" |
| 36 #include "components/history/core/browser/download_row.h" | 38 #include "components/history/core/browser/download_row.h" |
| 37 #include "components/history/core/browser/history_service.h" | 39 #include "components/history/core/browser/history_service.h" |
| 38 #include "content/public/browser/browser_thread.h" | 40 #include "content/public/browser/browser_thread.h" |
| 39 #include "content/public/browser/download_item.h" | 41 #include "content/public/browser/download_item.h" |
| 40 #include "content/public/browser/download_manager.h" | 42 #include "content/public/browser/download_manager.h" |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 DownloadHistory::Observer::~Observer() {} | 203 DownloadHistory::Observer::~Observer() {} |
| 202 | 204 |
| 203 // static | 205 // static |
| 204 bool DownloadHistory::IsPersisted(const content::DownloadItem* item) { | 206 bool DownloadHistory::IsPersisted(const content::DownloadItem* item) { |
| 205 const DownloadHistoryData* data = DownloadHistoryData::Get(item); | 207 const DownloadHistoryData* data = DownloadHistoryData::Get(item); |
| 206 return data && (data->state() == DownloadHistoryData::PERSISTED); | 208 return data && (data->state() == DownloadHistoryData::PERSISTED); |
| 207 } | 209 } |
| 208 | 210 |
| 209 DownloadHistory::DownloadHistory(content::DownloadManager* manager, | 211 DownloadHistory::DownloadHistory(content::DownloadManager* manager, |
| 210 scoped_ptr<HistoryAdapter> history) | 212 scoped_ptr<HistoryAdapter> history) |
| 211 : notifier_(manager, this), | 213 : notifier_(manager, this), |
| 212 history_(history.Pass()), | 214 history_(std::move(history)), |
| 213 loading_id_(content::DownloadItem::kInvalidId), | 215 loading_id_(content::DownloadItem::kInvalidId), |
| 214 history_size_(0), | 216 history_size_(0), |
| 215 weak_ptr_factory_(this) { | 217 weak_ptr_factory_(this) { |
| 216 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 218 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 217 content::DownloadManager::DownloadVector items; | 219 content::DownloadManager::DownloadVector items; |
| 218 notifier_.GetManager()->GetAllDownloads(&items); | 220 notifier_.GetManager()->GetAllDownloads(&items); |
| 219 for (content::DownloadManager::DownloadVector::const_iterator | 221 for (content::DownloadManager::DownloadVector::const_iterator |
| 220 it = items.begin(); it != items.end(); ++it) { | 222 it = items.begin(); it != items.end(); ++it) { |
| 221 OnDownloadCreated(notifier_.GetManager(), *it); | 223 OnDownloadCreated(notifier_.GetManager(), *it); |
| 222 } | 224 } |
| 223 history_->QueryDownloads(base::Bind( | 225 history_->QueryDownloads(base::Bind( |
| 224 &DownloadHistory::QueryCallback, weak_ptr_factory_.GetWeakPtr())); | 226 &DownloadHistory::QueryCallback, weak_ptr_factory_.GetWeakPtr())); |
| 225 } | 227 } |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 458 removing_ids_.insert(download_id); | 460 removing_ids_.insert(download_id); |
| 459 } | 461 } |
| 460 | 462 |
| 461 void DownloadHistory::RemoveDownloadsBatch() { | 463 void DownloadHistory::RemoveDownloadsBatch() { |
| 462 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 464 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 463 IdSet remove_ids; | 465 IdSet remove_ids; |
| 464 removing_ids_.swap(remove_ids); | 466 removing_ids_.swap(remove_ids); |
| 465 history_->RemoveDownloads(remove_ids); | 467 history_->RemoveDownloads(remove_ids); |
| 466 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadsRemoved(remove_ids)); | 468 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadsRemoved(remove_ids)); |
| 467 } | 469 } |
| OLD | NEW |