Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / notifications / message_center_notification_manager.h
blob52f3c1652195c90815adeb1ca22ba0c593441438
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.
5 #ifndef CHROME_BROWSER_NOTIFICATIONS_MESSAGE_CENTER_NOTIFICATION_MANAGER_H_
6 #define CHROME_BROWSER_NOTIFICATIONS_MESSAGE_CENTER_NOTIFICATION_MANAGER_H_
8 #include <map>
9 #include <string>
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/prefs/pref_member.h"
15 #include "base/time/time.h"
16 #include "base/timer/timer.h"
17 #include "chrome/browser/notifications/message_center_stats_collector.h"
18 #include "chrome/browser/notifications/notification.h"
19 #include "chrome/browser/notifications/notification_system_observer.h"
20 #include "chrome/browser/notifications/notification_ui_manager.h"
21 #include "content/public/browser/notification_observer.h"
22 #include "content/public/browser/notification_registrar.h"
23 #include "ui/message_center/message_center.h"
24 #include "ui/message_center/message_center_observer.h"
25 #include "ui/message_center/message_center_tray_delegate.h"
26 #include "ui/message_center/message_center_types.h"
28 class MessageCenterSettingsController;
29 class Notification;
30 class PrefRegistrySimple;
31 class PrefService;
32 class Profile;
34 namespace message_center {
35 class NotificationBlocker;
36 FORWARD_DECLARE_TEST(WebNotificationTrayTest, ManuallyCloseMessageCenter);
39 // This class extends NotificationUIManagerImpl and delegates actual display
40 // of notifications to MessageCenter, doing necessary conversions.
41 class MessageCenterNotificationManager
42 : public NotificationUIManager,
43 public message_center::MessageCenterObserver,
44 public content::NotificationObserver {
45 public:
46 MessageCenterNotificationManager(
47 message_center::MessageCenter* message_center,
48 PrefService* local_state,
49 scoped_ptr<message_center::NotifierSettingsProvider> settings_provider);
50 virtual ~MessageCenterNotificationManager();
52 // Registers preferences.
53 static void RegisterPrefs(PrefRegistrySimple* registry);
55 // NotificationUIManager
56 virtual void Add(const Notification& notification,
57 Profile* profile) OVERRIDE;
58 virtual bool Update(const Notification& notification,
59 Profile* profile) OVERRIDE;
60 virtual const Notification* FindById(
61 const std::string& notification_id) const OVERRIDE;
62 virtual bool CancelById(const std::string& notification_id) OVERRIDE;
63 virtual std::set<std::string> GetAllIdsByProfileAndSourceOrigin(
64 Profile* profile,
65 const GURL& source) OVERRIDE;
66 virtual bool CancelAllBySourceOrigin(const GURL& source_origin) OVERRIDE;
67 virtual bool CancelAllByProfile(Profile* profile) OVERRIDE;
68 virtual void CancelAll() OVERRIDE;
70 // MessageCenterObserver
71 virtual void OnNotificationRemoved(const std::string& notification_id,
72 bool by_user) OVERRIDE;
73 virtual void OnCenterVisibilityChanged(message_center::Visibility) OVERRIDE;
74 virtual void OnNotificationUpdated(const std::string& notification_id)
75 OVERRIDE;
77 void EnsureMessageCenterClosed();
79 #if defined(OS_WIN)
80 // Called when the pref changes for the first run balloon. The first run
81 // balloon is only displayed on Windows, since the visibility of the tray
82 // icon is limited.
83 void DisplayFirstRunBalloon();
85 void SetFirstRunTimeoutForTest(base::TimeDelta timeout);
86 bool FirstRunTimerIsActive() const;
87 #endif
89 // Takes ownership of |delegate|.
90 void SetMessageCenterTrayDelegateForTest(
91 message_center::MessageCenterTrayDelegate* delegate);
93 protected:
94 // content::NotificationObserver override.
95 virtual void Observe(int type,
96 const content::NotificationSource& source,
97 const content::NotificationDetails& details) OVERRIDE;
99 private:
100 FRIEND_TEST_ALL_PREFIXES(message_center::WebNotificationTrayTest,
101 ManuallyCloseMessageCenter);
102 class ImageDownloadsObserver {
103 public:
104 virtual void OnDownloadsCompleted() = 0;
107 typedef base::Callback<void(const gfx::Image&)> SetImageCallback;
108 class ImageDownloads
109 : public base::SupportsWeakPtr<ImageDownloads> {
110 public:
111 ImageDownloads(
112 message_center::MessageCenter* message_center,
113 ImageDownloadsObserver* observer);
114 virtual ~ImageDownloads();
116 void StartDownloads(const Notification& notification);
117 void StartDownloadWithImage(const Notification& notification,
118 const gfx::Image* image,
119 const GURL& url,
120 const SetImageCallback& callback);
121 void StartDownloadByKey(const Notification& notification,
122 const char* key,
123 int size,
124 const SetImageCallback& callback);
126 // FaviconHelper callback.
127 void DownloadComplete(const SetImageCallback& callback,
128 int download_id,
129 int http_status_code,
130 const GURL& image_url,
131 const std::vector<SkBitmap>& bitmaps,
132 const std::vector<gfx::Size>& original_bitmap_sizes);
133 private:
134 // Used to keep track of the number of pending downloads. Once this
135 // reaches zero, we can tell the delegate that we don't need the
136 // RenderViewHost anymore.
137 void AddPendingDownload();
138 void PendingDownloadCompleted();
140 // Weak reference to global message center.
141 message_center::MessageCenter* message_center_;
143 // Count of downloads that remain.
144 size_t pending_downloads_;
146 // Weak.
147 ImageDownloadsObserver* observer_;
149 DISALLOW_COPY_AND_ASSIGN(ImageDownloads);
152 // This class keeps a set of original Notification objects and corresponding
153 // Profiles, so when MessageCenter calls back with a notification_id, this
154 // class has necessary mapping to other source info - for example, it calls
155 // NotificationDelegate supplied by client when someone clicks on a
156 // Notification in MessageCenter. Likewise, if a Profile or Extension is
157 // being removed, the map makes it possible to revoke the notifications from
158 // MessageCenter. To keep that set, we use the private ProfileNotification
159 // class that stores a superset of all information about a notification.
161 // TODO(dimich): Consider merging all 4 types (Notification,
162 // QueuedNotification, ProfileNotification and NotificationList::Notification)
163 // into a single class.
164 class ProfileNotification : public ImageDownloadsObserver {
165 public:
166 ProfileNotification(Profile* profile,
167 const Notification& notification,
168 message_center::MessageCenter* message_center);
169 virtual ~ProfileNotification();
171 void StartDownloads();
173 // Overridden from ImageDownloadsObserver.
174 virtual void OnDownloadsCompleted() OVERRIDE;
176 Profile* profile() const { return profile_; }
177 const Notification& notification() const { return notification_; }
179 // Returns extension_id if the notification originates from an extension,
180 // empty string otherwise.
181 std::string GetExtensionId();
183 private:
184 // Weak, guaranteed not to be used after profile removal by parent class.
185 Profile* profile_;
186 Notification notification_;
187 // Track the downloads for this notification so the notification can be
188 // updated properly.
189 scoped_ptr<ImageDownloads> downloads_;
192 scoped_ptr<message_center::MessageCenterTrayDelegate> tray_;
193 message_center::MessageCenter* message_center_; // Weak, global.
195 // Use a map by notification_id since this mapping is the most often used.
196 typedef std::map<std::string, ProfileNotification*> NotificationMap;
197 NotificationMap profile_notifications_;
199 // Helpers that add/remove the notification from local map and MessageCenter.
200 // They take ownership of profile_notification object.
201 void AddProfileNotification(ProfileNotification* profile_notification);
202 void RemoveProfileNotification(ProfileNotification* profile_notification);
204 // Returns the ProfileNotification for the |id|, or NULL if no such
205 // notification is found.
206 ProfileNotification* FindProfileNotification(const std::string& id) const;
208 #if defined(OS_WIN)
209 // This function is run on update to ensure that the notification balloon is
210 // shown only when there are no popups present.
211 void CheckFirstRunTimer();
213 // |first_run_pref_| is used to keep track of whether we've ever shown the
214 // first run balloon before, even across restarts.
215 BooleanPrefMember first_run_pref_;
217 // The timer after which we will show the first run balloon. This timer is
218 // restarted every time the message center is closed and every time the last
219 // popup disappears from the screen.
220 base::OneShotTimer<MessageCenterNotificationManager> first_run_balloon_timer_;
222 // The first-run balloon will be shown |first_run_idle_timeout_| after all
223 // popups go away and the user has notifications in the message center.
224 base::TimeDelta first_run_idle_timeout_;
226 // Provides weak pointers for the purpose of the first run timer.
227 base::WeakPtrFactory<MessageCenterNotificationManager> weak_factory_;
228 #endif
230 scoped_ptr<message_center::NotifierSettingsProvider> settings_provider_;
232 // To own the blockers.
233 ScopedVector<message_center::NotificationBlocker> blockers_;
235 // Registrar for the other kind of notifications (event signaling).
236 content::NotificationRegistrar registrar_;
238 NotificationSystemObserver system_observer_;
240 // Keeps track of all notification statistics for UMA purposes.
241 MessageCenterStatsCollector stats_collector_;
243 DISALLOW_COPY_AND_ASSIGN(MessageCenterNotificationManager);
246 #endif // CHROME_BROWSER_NOTIFICATIONS_MESSAGE_CENTER_NOTIFICATION_MANAGER_H_