Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / download / all_download_item_notifier.h
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 #ifndef CHROME_BROWSER_DOWNLOAD_ALL_DOWNLOAD_ITEM_NOTIFIER_H_
6 #define CHROME_BROWSER_DOWNLOAD_ALL_DOWNLOAD_ITEM_NOTIFIER_H_
7
8 #include <set>
9
10 #include "content/public/browser/download_manager.h"
11 #include "content/public/browser/download_item.h"
12
13 // AllDownloadItemNotifier observes ALL the DownloadItems on a given
14 // DownloadManager.
15 // Clients should use GetManager() instead of storing their own pointer to the
16 // manager so that they can be sensitive to managers that have gone down.
17
18 // Example Usage:
19 // class DownloadSystemConsumer : public AllDownloadItemNotifier::Observer {
20 //  public:
21 //   DownloadSystemConsumer(content::DownloadManager* original_manager,
22 //            content::DownloadManager* incognito_manager)
23 //     : original_notifier_(original_manager, this),
24 //       incognito_notifier_(incognito_manager, this) {
25 //   }
26 //
27 //   virtual void OnDownloadUpdated(
28 //     content::DownloadManager* manager, content::DownloadItem* item) { ... }
29 //
30 //  private:
31 //   AllDownloadItemNotifier original_notifier_;
32 //   AllDownloadItemNotifier incognito_notifier_;
33 // };
34
35 class AllDownloadItemNotifier : public content::DownloadManager::Observer,
36                                 public content::DownloadItem::Observer {
37  public:
38   // All of the methods take the DownloadManager so that subclasses can observe
39   // multiple managers at once and easily distinguish which manager a given item
40   // belongs to.
41   class Observer {
42    public:
43     Observer() {}
44     virtual ~Observer() {}
45
46     virtual void OnDownloadCreated(
47         content::DownloadManager* manager, content::DownloadItem* item) {}
48     virtual void OnDownloadUpdated(
49         content::DownloadManager* manager, content::DownloadItem* item) {}
50     virtual void OnDownloadOpened(
51         content::DownloadManager* manager, content::DownloadItem* item) {}
52     virtual void OnDownloadRemoved(
53         content::DownloadManager* manager, content::DownloadItem* item) {}
54
55    private:
56     DISALLOW_COPY_AND_ASSIGN(Observer);
57   };
58
59   AllDownloadItemNotifier(content::DownloadManager* manager,
60                           Observer* observer);
61
62   ~AllDownloadItemNotifier() override;
63
64   // Returns NULL if the manager has gone down.
65   content::DownloadManager* GetManager() const { return manager_; }
66
67  private:
68   // content::DownloadManager::Observer
69   void ManagerGoingDown(content::DownloadManager* manager) override;
70   void OnDownloadCreated(content::DownloadManager* manager,
71                          content::DownloadItem* item) override;
72
73   // content::DownloadItem::Observer
74   void OnDownloadUpdated(content::DownloadItem* item) override;
75   void OnDownloadOpened(content::DownloadItem* item) override;
76   void OnDownloadRemoved(content::DownloadItem* item) override;
77   void OnDownloadDestroyed(content::DownloadItem* item) override;
78
79   content::DownloadManager* manager_;
80   AllDownloadItemNotifier::Observer* observer_;
81   std::set<content::DownloadItem*> observing_;
82
83   DISALLOW_COPY_AND_ASSIGN(AllDownloadItemNotifier);
84 };
85
86 #endif  // CHROME_BROWSER_DOWNLOAD_ALL_DOWNLOAD_ITEM_NOTIFIER_H_