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
3// found in the LICENSE file.
4
5#include "chrome/browser/download/all_download_item_notifier.h"
6
7AllDownloadItemNotifier::AllDownloadItemNotifier(
8    content::DownloadManager* manager,
9    AllDownloadItemNotifier::Observer* observer)
10    : manager_(manager),
11      observer_(observer) {
12  DCHECK(observer_);
13  manager_->AddObserver(this);
14  content::DownloadManager::DownloadVector items;
15  manager_->GetAllDownloads(&items);
16  for (content::DownloadManager::DownloadVector::const_iterator it =
17         items.begin();
18       it != items.end(); ++it) {
19    (*it)->AddObserver(this);
20    observing_.insert(*it);
21  }
22}
23
24AllDownloadItemNotifier::~AllDownloadItemNotifier() {
25  if (manager_)
26    manager_->RemoveObserver(this);
27  for (std::set<content::DownloadItem*>::const_iterator it =
28          observing_.begin();
29        it != observing_.end(); ++it) {
30    (*it)->RemoveObserver(this);
31  }
32  observing_.clear();
33}
34
35void AllDownloadItemNotifier::ManagerGoingDown(
36    content::DownloadManager* manager) {
37  DCHECK_EQ(manager_, manager);
38  manager_->RemoveObserver(this);
39  manager_ = NULL;
40}
41
42void AllDownloadItemNotifier::OnDownloadCreated(
43    content::DownloadManager* manager,
44    content::DownloadItem* item) {
45  item->AddObserver(this);
46  observing_.insert(item);
47  observer_->OnDownloadCreated(manager, item);
48}
49
50void AllDownloadItemNotifier::OnDownloadUpdated(
51    content::DownloadItem* item) {
52  observer_->OnDownloadUpdated(manager_, item);
53}
54
55void AllDownloadItemNotifier::OnDownloadOpened(
56    content::DownloadItem* item) {
57  observer_->OnDownloadOpened(manager_, item);
58}
59
60void AllDownloadItemNotifier::OnDownloadRemoved(
61    content::DownloadItem* item) {
62  observer_->OnDownloadRemoved(manager_, item);
63}
64
65void AllDownloadItemNotifier::OnDownloadDestroyed(
66    content::DownloadItem* item) {
67  item->RemoveObserver(this);
68  observing_.erase(item);
69}
70