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 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 for (content::DownloadManager::DownloadVector::const_iterator | 217 for (content::DownloadManager::DownloadVector::const_iterator |
218 it = items.begin(); it != items.end(); ++it) { | 218 it = items.begin(); it != items.end(); ++it) { |
219 OnDownloadCreated(notifier_.GetManager(), *it); | 219 OnDownloadCreated(notifier_.GetManager(), *it); |
220 } | 220 } |
221 history_->QueryDownloads(base::Bind( | 221 history_->QueryDownloads(base::Bind( |
222 &DownloadHistory::QueryCallback, weak_ptr_factory_.GetWeakPtr())); | 222 &DownloadHistory::QueryCallback, weak_ptr_factory_.GetWeakPtr())); |
223 } | 223 } |
224 | 224 |
225 DownloadHistory::~DownloadHistory() { | 225 DownloadHistory::~DownloadHistory() { |
226 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 226 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
227 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadHistoryDestroyed()); | 227 for (Observer& observer : observers_) |
| 228 observer.OnDownloadHistoryDestroyed(); |
228 observers_.Clear(); | 229 observers_.Clear(); |
229 } | 230 } |
230 | 231 |
231 void DownloadHistory::AddObserver(DownloadHistory::Observer* observer) { | 232 void DownloadHistory::AddObserver(DownloadHistory::Observer* observer) { |
232 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 233 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
233 observers_.AddObserver(observer); | 234 observers_.AddObserver(observer); |
234 | 235 |
235 if (initial_history_query_complete_) | 236 if (initial_history_query_complete_) |
236 observer->OnHistoryQueryComplete(); | 237 observer->OnHistoryQueryComplete(); |
237 } | 238 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 item->UpdateObservers(); | 282 item->UpdateObservers(); |
282 } | 283 } |
283 #endif | 284 #endif |
284 DCHECK_EQ(DownloadHistoryData::PERSISTED, | 285 DCHECK_EQ(DownloadHistoryData::PERSISTED, |
285 DownloadHistoryData::Get(item)->state()); | 286 DownloadHistoryData::Get(item)->state()); |
286 ++history_size_; | 287 ++history_size_; |
287 } | 288 } |
288 notifier_.GetManager()->CheckForHistoryFilesRemoval(); | 289 notifier_.GetManager()->CheckForHistoryFilesRemoval(); |
289 | 290 |
290 initial_history_query_complete_ = true; | 291 initial_history_query_complete_ = true; |
291 FOR_EACH_OBSERVER(Observer, observers_, OnHistoryQueryComplete()); | 292 for (Observer& observer : observers_) |
| 293 observer.OnHistoryQueryComplete(); |
292 } | 294 } |
293 | 295 |
294 void DownloadHistory::MaybeAddToHistory(content::DownloadItem* item) { | 296 void DownloadHistory::MaybeAddToHistory(content::DownloadItem* item) { |
295 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 297 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
296 | 298 |
297 uint32_t download_id = item->GetId(); | 299 uint32_t download_id = item->GetId(); |
298 DownloadHistoryData* data = DownloadHistoryData::Get(item); | 300 DownloadHistoryData* data = DownloadHistoryData::Get(item); |
299 bool removing = removing_ids_.find(download_id) != removing_ids_.end(); | 301 bool removing = removing_ids_.find(download_id) != removing_ids_.end(); |
300 | 302 |
301 // TODO(benjhayden): Remove IsTemporary(). | 303 // TODO(benjhayden): Remove IsTemporary(). |
302 if (download_crx_util::IsExtensionDownload(*item) || | 304 if (download_crx_util::IsExtensionDownload(*item) || |
303 item->IsTemporary() || | 305 item->IsTemporary() || |
304 (data->state() != DownloadHistoryData::NOT_PERSISTED) || | 306 (data->state() != DownloadHistoryData::NOT_PERSISTED) || |
305 removing) | 307 removing) |
306 return; | 308 return; |
307 | 309 |
308 data->SetState(DownloadHistoryData::PERSISTING); | 310 data->SetState(DownloadHistoryData::PERSISTING); |
309 if (data->info() == NULL) { | 311 if (data->info() == NULL) { |
310 // 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, |
311 // when ItemAdded() calls OnDownloadUpdated(), it can decide whether to | 313 // when ItemAdded() calls OnDownloadUpdated(), it can decide whether to |
312 // Update the db and/or clear the info. | 314 // Update the db and/or clear the info. |
313 data->set_info(GetDownloadRow(item)); | 315 data->set_info(GetDownloadRow(item)); |
314 } | 316 } |
315 | 317 |
316 history_->CreateDownload(*data->info(), base::Bind( | 318 history_->CreateDownload(*data->info(), base::Bind( |
317 &DownloadHistory::ItemAdded, weak_ptr_factory_.GetWeakPtr(), | 319 &DownloadHistory::ItemAdded, weak_ptr_factory_.GetWeakPtr(), |
318 download_id)); | 320 download_id)); |
319 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadStored( | 321 for (Observer& observer : observers_) |
320 item, *data->info())); | 322 observer.OnDownloadStored(item, *data->info()); |
321 } | 323 } |
322 | 324 |
323 void DownloadHistory::ItemAdded(uint32_t download_id, bool success) { | 325 void DownloadHistory::ItemAdded(uint32_t download_id, bool success) { |
324 if (removed_while_adding_.find(download_id) != | 326 if (removed_while_adding_.find(download_id) != |
325 removed_while_adding_.end()) { | 327 removed_while_adding_.end()) { |
326 removed_while_adding_.erase(download_id); | 328 removed_while_adding_.erase(download_id); |
327 if (success) | 329 if (success) |
328 ScheduleRemoveDownload(download_id); | 330 ScheduleRemoveDownload(download_id); |
329 return; | 331 return; |
330 } | 332 } |
(...skipping 26 matching lines...) Expand all Loading... |
357 | 359 |
358 UMA_HISTOGRAM_CUSTOM_COUNTS("Download.HistorySize2", | 360 UMA_HISTOGRAM_CUSTOM_COUNTS("Download.HistorySize2", |
359 history_size_, | 361 history_size_, |
360 1/*min*/, | 362 1/*min*/, |
361 (1 << 23)/*max*/, | 363 (1 << 23)/*max*/, |
362 (1 << 7)/*num_buckets*/); | 364 (1 << 7)/*num_buckets*/); |
363 ++history_size_; | 365 ++history_size_; |
364 | 366 |
365 // Notify the observer about the change in the persistence state. | 367 // Notify the observer about the change in the persistence state. |
366 if (was_persisted != IsPersisted(item)) { | 368 if (was_persisted != IsPersisted(item)) { |
367 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadStored( | 369 for (Observer& observer : observers_) |
368 item, *data->info())); | 370 observer.OnDownloadStored(item, *data->info()); |
369 } | 371 } |
370 | 372 |
371 // In case the item changed or became temporary while it was being added. | 373 // In case the item changed or became temporary while it was being added. |
372 OnDownloadUpdated(notifier_.GetManager(), item); | 374 OnDownloadUpdated(notifier_.GetManager(), item); |
373 } | 375 } |
374 | 376 |
375 void DownloadHistory::OnDownloadCreated( | 377 void DownloadHistory::OnDownloadCreated( |
376 content::DownloadManager* manager, content::DownloadItem* item) { | 378 content::DownloadManager* manager, content::DownloadItem* item) { |
377 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 379 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
378 | 380 |
(...skipping 24 matching lines...) Expand all Loading... |
403 OnDownloadRemoved(notifier_.GetManager(), item); | 405 OnDownloadRemoved(notifier_.GetManager(), item); |
404 return; | 406 return; |
405 } | 407 } |
406 | 408 |
407 history::DownloadRow current_info(GetDownloadRow(item)); | 409 history::DownloadRow current_info(GetDownloadRow(item)); |
408 bool should_update = ShouldUpdateHistory(data->info(), current_info); | 410 bool should_update = ShouldUpdateHistory(data->info(), current_info); |
409 UMA_HISTOGRAM_ENUMERATION("Download.HistoryPropagatedUpdate", | 411 UMA_HISTOGRAM_ENUMERATION("Download.HistoryPropagatedUpdate", |
410 should_update, 2); | 412 should_update, 2); |
411 if (should_update) { | 413 if (should_update) { |
412 history_->UpdateDownload(current_info); | 414 history_->UpdateDownload(current_info); |
413 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadStored( | 415 for (Observer& observer : observers_) |
414 item, current_info)); | 416 observer.OnDownloadStored(item, current_info); |
415 } | 417 } |
416 if (item->GetState() == content::DownloadItem::IN_PROGRESS) { | 418 if (item->GetState() == content::DownloadItem::IN_PROGRESS) { |
417 data->set_info(current_info); | 419 data->set_info(current_info); |
418 } else { | 420 } else { |
419 data->clear_info(); | 421 data->clear_info(); |
420 } | 422 } |
421 } | 423 } |
422 | 424 |
423 void DownloadHistory::OnDownloadOpened( | 425 void DownloadHistory::OnDownloadOpened( |
424 content::DownloadManager* manager, content::DownloadItem* item) { | 426 content::DownloadManager* manager, content::DownloadItem* item) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 weak_ptr_factory_.GetWeakPtr())); | 460 weak_ptr_factory_.GetWeakPtr())); |
459 } | 461 } |
460 removing_ids_.insert(download_id); | 462 removing_ids_.insert(download_id); |
461 } | 463 } |
462 | 464 |
463 void DownloadHistory::RemoveDownloadsBatch() { | 465 void DownloadHistory::RemoveDownloadsBatch() { |
464 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 466 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
465 IdSet remove_ids; | 467 IdSet remove_ids; |
466 removing_ids_.swap(remove_ids); | 468 removing_ids_.swap(remove_ids); |
467 history_->RemoveDownloads(remove_ids); | 469 history_->RemoveDownloads(remove_ids); |
468 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadsRemoved(remove_ids)); | 470 for (Observer& observer : observers_) |
| 471 observer.OnDownloadsRemoved(remove_ids); |
469 } | 472 } |
OLD | NEW |