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 "base/macros.h" |
31 #include "base/metrics/histogram.h" | 32 #include "base/metrics/histogram.h" |
32 #include "chrome/browser/download/download_crx_util.h" | 33 #include "chrome/browser/download/download_crx_util.h" |
33 #include "components/history/content/browser/download_constants_utils.h" | 34 #include "components/history/content/browser/download_constants_utils.h" |
34 #include "components/history/core/browser/download_database.h" | 35 #include "components/history/core/browser/download_database.h" |
35 #include "components/history/core/browser/download_row.h" | 36 #include "components/history/core/browser/download_row.h" |
36 #include "components/history/core/browser/history_service.h" | 37 #include "components/history/core/browser/history_service.h" |
37 #include "content/public/browser/browser_thread.h" | 38 #include "content/public/browser/browser_thread.h" |
38 #include "content/public/browser/download_item.h" | 39 #include "content/public/browser/download_item.h" |
39 #include "content/public/browser/download_manager.h" | 40 #include "content/public/browser/download_manager.h" |
40 | 41 |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 const history::HistoryService::DownloadCreateCallback& callback) { | 186 const history::HistoryService::DownloadCreateCallback& callback) { |
186 history_->CreateDownload(info, callback); | 187 history_->CreateDownload(info, callback); |
187 } | 188 } |
188 | 189 |
189 void DownloadHistory::HistoryAdapter::UpdateDownload( | 190 void DownloadHistory::HistoryAdapter::UpdateDownload( |
190 const history::DownloadRow& data) { | 191 const history::DownloadRow& data) { |
191 history_->UpdateDownload(data); | 192 history_->UpdateDownload(data); |
192 } | 193 } |
193 | 194 |
194 void DownloadHistory::HistoryAdapter::RemoveDownloads( | 195 void DownloadHistory::HistoryAdapter::RemoveDownloads( |
195 const std::set<uint32>& ids) { | 196 const std::set<uint32_t>& ids) { |
196 history_->RemoveDownloads(ids); | 197 history_->RemoveDownloads(ids); |
197 } | 198 } |
198 | 199 |
199 DownloadHistory::Observer::Observer() {} | 200 DownloadHistory::Observer::Observer() {} |
200 DownloadHistory::Observer::~Observer() {} | 201 DownloadHistory::Observer::~Observer() {} |
201 | 202 |
202 // static | 203 // static |
203 bool DownloadHistory::IsPersisted(const content::DownloadItem* item) { | 204 bool DownloadHistory::IsPersisted(const content::DownloadItem* item) { |
204 const DownloadHistoryData* data = DownloadHistoryData::Get(item); | 205 const DownloadHistoryData* data = DownloadHistoryData::Get(item); |
205 return data && (data->state() == DownloadHistoryData::PERSISTED); | 206 return data && (data->state() == DownloadHistoryData::PERSISTED); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 DCHECK_EQ(DownloadHistoryData::PERSISTED, | 289 DCHECK_EQ(DownloadHistoryData::PERSISTED, |
289 DownloadHistoryData::Get(item)->state()); | 290 DownloadHistoryData::Get(item)->state()); |
290 ++history_size_; | 291 ++history_size_; |
291 } | 292 } |
292 notifier_.GetManager()->CheckForHistoryFilesRemoval(); | 293 notifier_.GetManager()->CheckForHistoryFilesRemoval(); |
293 } | 294 } |
294 | 295 |
295 void DownloadHistory::MaybeAddToHistory(content::DownloadItem* item) { | 296 void DownloadHistory::MaybeAddToHistory(content::DownloadItem* item) { |
296 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 297 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
297 | 298 |
298 uint32 download_id = item->GetId(); | 299 uint32_t download_id = item->GetId(); |
299 DownloadHistoryData* data = DownloadHistoryData::Get(item); | 300 DownloadHistoryData* data = DownloadHistoryData::Get(item); |
300 bool removing = removing_ids_.find(download_id) != removing_ids_.end(); | 301 bool removing = removing_ids_.find(download_id) != removing_ids_.end(); |
301 | 302 |
302 // TODO(benjhayden): Remove IsTemporary(). | 303 // TODO(benjhayden): Remove IsTemporary(). |
303 if (download_crx_util::IsExtensionDownload(*item) || | 304 if (download_crx_util::IsExtensionDownload(*item) || |
304 item->IsTemporary() || | 305 item->IsTemporary() || |
305 (data->state() != DownloadHistoryData::NOT_PERSISTED) || | 306 (data->state() != DownloadHistoryData::NOT_PERSISTED) || |
306 removing) | 307 removing) |
307 return; | 308 return; |
308 | 309 |
309 data->SetState(DownloadHistoryData::PERSISTING); | 310 data->SetState(DownloadHistoryData::PERSISTING); |
310 if (data->info() == NULL) { | 311 if (data->info() == NULL) { |
311 // Keep the info here regardless of whether the item is in progress so that, | 312 // Keep the info here regardless of whether the item is in progress so that, |
312 // when ItemAdded() calls OnDownloadUpdated(), it can decide whether to | 313 // when ItemAdded() calls OnDownloadUpdated(), it can decide whether to |
313 // Update the db and/or clear the info. | 314 // Update the db and/or clear the info. |
314 data->set_info(GetDownloadRow(item)); | 315 data->set_info(GetDownloadRow(item)); |
315 } | 316 } |
316 | 317 |
317 history_->CreateDownload(*data->info(), base::Bind( | 318 history_->CreateDownload(*data->info(), base::Bind( |
318 &DownloadHistory::ItemAdded, weak_ptr_factory_.GetWeakPtr(), | 319 &DownloadHistory::ItemAdded, weak_ptr_factory_.GetWeakPtr(), |
319 download_id)); | 320 download_id)); |
320 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadStored( | 321 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadStored( |
321 item, *data->info())); | 322 item, *data->info())); |
322 } | 323 } |
323 | 324 |
324 void DownloadHistory::ItemAdded(uint32 download_id, bool success) { | 325 void DownloadHistory::ItemAdded(uint32_t download_id, bool success) { |
325 if (removed_while_adding_.find(download_id) != | 326 if (removed_while_adding_.find(download_id) != |
326 removed_while_adding_.end()) { | 327 removed_while_adding_.end()) { |
327 removed_while_adding_.erase(download_id); | 328 removed_while_adding_.erase(download_id); |
328 if (success) | 329 if (success) |
329 ScheduleRemoveDownload(download_id); | 330 ScheduleRemoveDownload(download_id); |
330 return; | 331 return; |
331 } | 332 } |
332 | 333 |
333 if (!notifier_.GetManager()) | 334 if (!notifier_.GetManager()) |
334 return; | 335 return; |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 ScheduleRemoveDownload(item->GetId()); | 438 ScheduleRemoveDownload(item->GetId()); |
438 // This is important: another OnDownloadRemoved() handler could do something | 439 // This is important: another OnDownloadRemoved() handler could do something |
439 // that synchronously fires an OnDownloadUpdated(). | 440 // that synchronously fires an OnDownloadUpdated(). |
440 data->SetState(DownloadHistoryData::NOT_PERSISTED); | 441 data->SetState(DownloadHistoryData::NOT_PERSISTED); |
441 // ItemAdded increments history_size_ only if the item wasn't | 442 // ItemAdded increments history_size_ only if the item wasn't |
442 // removed_while_adding_, so the next line does not belong in | 443 // removed_while_adding_, so the next line does not belong in |
443 // ScheduleRemoveDownload(). | 444 // ScheduleRemoveDownload(). |
444 --history_size_; | 445 --history_size_; |
445 } | 446 } |
446 | 447 |
447 void DownloadHistory::ScheduleRemoveDownload(uint32 download_id) { | 448 void DownloadHistory::ScheduleRemoveDownload(uint32_t download_id) { |
448 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 449 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
449 | 450 |
450 // For database efficiency, batch removals together if they happen all at | 451 // For database efficiency, batch removals together if they happen all at |
451 // once. | 452 // once. |
452 if (removing_ids_.empty()) { | 453 if (removing_ids_.empty()) { |
453 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 454 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
454 base::Bind(&DownloadHistory::RemoveDownloadsBatch, | 455 base::Bind(&DownloadHistory::RemoveDownloadsBatch, |
455 weak_ptr_factory_.GetWeakPtr())); | 456 weak_ptr_factory_.GetWeakPtr())); |
456 } | 457 } |
457 removing_ids_.insert(download_id); | 458 removing_ids_.insert(download_id); |
458 } | 459 } |
459 | 460 |
460 void DownloadHistory::RemoveDownloadsBatch() { | 461 void DownloadHistory::RemoveDownloadsBatch() { |
461 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 462 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
462 IdSet remove_ids; | 463 IdSet remove_ids; |
463 removing_ids_.swap(remove_ids); | 464 removing_ids_.swap(remove_ids); |
464 history_->RemoveDownloads(remove_ids); | 465 history_->RemoveDownloads(remove_ids); |
465 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadsRemoved(remove_ids)); | 466 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadsRemoved(remove_ids)); |
466 } | 467 } |
OLD | NEW |