1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/download/content/public/all_download_item_notifier.h"
6
7 #include "base/trace_event/memory_usage_estimator.h"
8
9 namespace download {
10
AllDownloadItemNotifier(content::DownloadManager * manager,AllDownloadItemNotifier::Observer * observer)11 AllDownloadItemNotifier::AllDownloadItemNotifier(
12 content::DownloadManager* manager,
13 AllDownloadItemNotifier::Observer* observer)
14 : manager_(manager), observer_(observer) {
15 DCHECK(observer_);
16 manager_->AddObserver(this);
17 content::DownloadManager::DownloadVector items;
18 manager_->GetAllDownloads(&items);
19 for (content::DownloadManager::DownloadVector::const_iterator it =
20 items.begin();
21 it != items.end(); ++it) {
22 (*it)->AddObserver(this);
23 observing_.insert(*it);
24 }
25
26 if (manager_->IsManagerInitialized())
27 observer_->OnManagerInitialized(manager_);
28 }
29
~AllDownloadItemNotifier()30 AllDownloadItemNotifier::~AllDownloadItemNotifier() {
31 if (manager_)
32 manager_->RemoveObserver(this);
33 for (auto it = observing_.begin(); it != observing_.end(); ++it) {
34 (*it)->RemoveObserver(this);
35 }
36 observing_.clear();
37 }
38
EstimateMemoryUsage() const39 size_t AllDownloadItemNotifier::EstimateMemoryUsage() const {
40 return base::trace_event::EstimateMemoryUsage(observing_);
41 }
42
OnManagerInitialized()43 void AllDownloadItemNotifier::OnManagerInitialized() {
44 observer_->OnManagerInitialized(manager_);
45 }
46
ManagerGoingDown(content::DownloadManager * manager)47 void AllDownloadItemNotifier::ManagerGoingDown(
48 content::DownloadManager* manager) {
49 DCHECK_EQ(manager_, manager);
50 observer_->OnManagerGoingDown(manager);
51 manager_->RemoveObserver(this);
52 manager_ = nullptr;
53 }
54
OnDownloadCreated(content::DownloadManager * manager,DownloadItem * item)55 void AllDownloadItemNotifier::OnDownloadCreated(
56 content::DownloadManager* manager,
57 DownloadItem* item) {
58 item->AddObserver(this);
59 observing_.insert(item);
60 observer_->OnDownloadCreated(manager, item);
61 }
62
OnDownloadUpdated(DownloadItem * item)63 void AllDownloadItemNotifier::OnDownloadUpdated(DownloadItem* item) {
64 observer_->OnDownloadUpdated(manager_, item);
65 }
66
OnDownloadOpened(DownloadItem * item)67 void AllDownloadItemNotifier::OnDownloadOpened(DownloadItem* item) {
68 observer_->OnDownloadOpened(manager_, item);
69 }
70
OnDownloadRemoved(DownloadItem * item)71 void AllDownloadItemNotifier::OnDownloadRemoved(DownloadItem* item) {
72 observer_->OnDownloadRemoved(manager_, item);
73 }
74
OnDownloadDestroyed(DownloadItem * item)75 void AllDownloadItemNotifier::OnDownloadDestroyed(DownloadItem* item) {
76 item->RemoveObserver(this);
77 observing_.erase(item);
78 }
79
80 } // namespace download
81