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_
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/google_now_notification_stats_collector.h"
18 #include "chrome/browser/notifications/message_center_stats_collector.h"
19 #include "chrome/browser/notifications/notification.h"
20 #include "chrome/browser/notifications/notification_system_observer.h"
21 #include "chrome/browser/notifications/notification_ui_manager.h"
22 #include "content/public/browser/notification_observer.h"
23 #include "content/public/browser/notification_registrar.h"
24 #include "ui/message_center/message_center.h"
25 #include "ui/message_center/message_center_observer.h"
26 #include "ui/message_center/message_center_tray_delegate.h"
27 #include "ui/message_center/message_center_types.h"
29 class MessageCenterSettingsController
;
31 class PrefRegistrySimple
;
35 namespace message_center
{
36 class NotificationBlocker
;
37 FORWARD_DECLARE_TEST(WebNotificationTrayTest
, ManuallyCloseMessageCenter
);
40 // This class extends NotificationUIManagerImpl and delegates actual display
41 // of notifications to MessageCenter, doing necessary conversions.
42 class MessageCenterNotificationManager
43 : public NotificationUIManager
,
44 public message_center::MessageCenterObserver
,
45 public content::NotificationObserver
{
47 MessageCenterNotificationManager(
48 message_center::MessageCenter
* message_center
,
49 PrefService
* local_state
,
50 scoped_ptr
<message_center::NotifierSettingsProvider
> settings_provider
);
51 virtual ~MessageCenterNotificationManager();
53 // Registers preferences.
54 static void RegisterPrefs(PrefRegistrySimple
* registry
);
56 // NotificationUIManager
57 virtual void Add(const Notification
& notification
,
58 Profile
* profile
) OVERRIDE
;
59 virtual bool Update(const Notification
& notification
,
60 Profile
* profile
) OVERRIDE
;
61 virtual const Notification
* FindById(
62 const std::string
& notification_id
) const OVERRIDE
;
63 virtual bool CancelById(const std::string
& notification_id
) OVERRIDE
;
64 virtual std::set
<std::string
> GetAllIdsByProfileAndSourceOrigin(
66 const GURL
& source
) OVERRIDE
;
67 virtual bool CancelAllBySourceOrigin(const GURL
& source_origin
) OVERRIDE
;
68 virtual bool CancelAllByProfile(Profile
* profile
) OVERRIDE
;
69 virtual void CancelAll() OVERRIDE
;
71 // MessageCenterObserver
72 virtual void OnNotificationRemoved(const std::string
& notification_id
,
73 bool by_user
) OVERRIDE
;
74 virtual void OnCenterVisibilityChanged(message_center::Visibility
) OVERRIDE
;
75 virtual void OnNotificationUpdated(const std::string
& notification_id
)
78 void EnsureMessageCenterClosed();
81 // Called when the pref changes for the first run balloon. The first run
82 // balloon is only displayed on Windows, since the visibility of the tray
84 void DisplayFirstRunBalloon();
86 void SetFirstRunTimeoutForTest(base::TimeDelta timeout
);
87 bool FirstRunTimerIsActive() const;
90 // Takes ownership of |delegate|.
91 void SetMessageCenterTrayDelegateForTest(
92 message_center::MessageCenterTrayDelegate
* delegate
);
95 // content::NotificationObserver override.
96 virtual void Observe(int type
,
97 const content::NotificationSource
& source
,
98 const content::NotificationDetails
& details
) OVERRIDE
;
101 FRIEND_TEST_ALL_PREFIXES(message_center::WebNotificationTrayTest
,
102 ManuallyCloseMessageCenter
);
103 class ImageDownloadsObserver
{
105 virtual void OnDownloadsCompleted() = 0;
108 typedef base::Callback
<void(const gfx::Image
&)> SetImageCallback
;
110 : public base::SupportsWeakPtr
<ImageDownloads
> {
113 message_center::MessageCenter
* message_center
,
114 ImageDownloadsObserver
* observer
);
115 virtual ~ImageDownloads();
117 void StartDownloads(const Notification
& notification
);
118 void StartDownloadWithImage(const Notification
& notification
,
119 const gfx::Image
* image
,
121 const SetImageCallback
& callback
);
122 void StartDownloadByKey(const Notification
& notification
,
125 const SetImageCallback
& callback
);
127 // FaviconHelper callback.
128 void DownloadComplete(const SetImageCallback
& callback
,
130 int http_status_code
,
131 const GURL
& image_url
,
132 const std::vector
<SkBitmap
>& bitmaps
,
133 const std::vector
<gfx::Size
>& original_bitmap_sizes
);
135 // Used to keep track of the number of pending downloads. Once this
136 // reaches zero, we can tell the delegate that we don't need the
137 // RenderViewHost anymore.
138 void AddPendingDownload();
139 void PendingDownloadCompleted();
141 // Weak reference to global message center.
142 message_center::MessageCenter
* message_center_
;
144 // Count of downloads that remain.
145 size_t pending_downloads_
;
148 ImageDownloadsObserver
* observer_
;
150 DISALLOW_COPY_AND_ASSIGN(ImageDownloads
);
153 // This class keeps a set of original Notification objects and corresponding
154 // Profiles, so when MessageCenter calls back with a notification_id, this
155 // class has necessary mapping to other source info - for example, it calls
156 // NotificationDelegate supplied by client when someone clicks on a
157 // Notification in MessageCenter. Likewise, if a Profile or Extension is
158 // being removed, the map makes it possible to revoke the notifications from
159 // MessageCenter. To keep that set, we use the private ProfileNotification
160 // class that stores a superset of all information about a notification.
162 // TODO(dimich): Consider merging all 4 types (Notification,
163 // QueuedNotification, ProfileNotification and NotificationList::Notification)
164 // into a single class.
165 class ProfileNotification
: public ImageDownloadsObserver
{
167 ProfileNotification(Profile
* profile
,
168 const Notification
& notification
,
169 message_center::MessageCenter
* message_center
);
170 virtual ~ProfileNotification();
172 void StartDownloads();
174 // Overridden from ImageDownloadsObserver.
175 virtual void OnDownloadsCompleted() OVERRIDE
;
177 Profile
* profile() const { return profile_
; }
178 const Notification
& notification() const { return notification_
; }
180 // Returns extension_id if the notification originates from an extension,
181 // empty string otherwise.
182 std::string
GetExtensionId();
184 // Route a new notification to an app/extension.
185 void AddToAlternateProvider(const std::string extension_id
);
188 // Weak, guaranteed not to be used after profile removal by parent class.
190 Notification notification_
;
191 // Track the downloads for this notification so the notification can be
193 scoped_ptr
<ImageDownloads
> downloads_
;
196 scoped_ptr
<message_center::MessageCenterTrayDelegate
> tray_
;
197 message_center::MessageCenter
* message_center_
; // Weak, global.
199 // Use a map by notification_id since this mapping is the most often used.
200 typedef std::map
<std::string
, ProfileNotification
*> NotificationMap
;
201 NotificationMap profile_notifications_
;
203 // Helpers that add/remove the notification from local map.
204 // The local map takes ownership of profile_notification object.
205 void AddProfileNotification(ProfileNotification
* profile_notification
);
206 void RemoveProfileNotification(ProfileNotification
* profile_notification
);
208 // Returns the ProfileNotification for the |id|, or NULL if no such
209 // notification is found.
210 ProfileNotification
* FindProfileNotification(const std::string
& id
) const;
212 // Get the extension ID of the extension that the user chose to take over
213 // Chorme Notification Center.
214 std::string
GetExtensionTakingOverNotifications(Profile
* profile
);
217 // This function is run on update to ensure that the notification balloon is
218 // shown only when there are no popups present.
219 void CheckFirstRunTimer();
221 // |first_run_pref_| is used to keep track of whether we've ever shown the
222 // first run balloon before, even across restarts.
223 BooleanPrefMember first_run_pref_
;
225 // The timer after which we will show the first run balloon. This timer is
226 // restarted every time the message center is closed and every time the last
227 // popup disappears from the screen.
228 base::OneShotTimer
<MessageCenterNotificationManager
> first_run_balloon_timer_
;
230 // The first-run balloon will be shown |first_run_idle_timeout_| after all
231 // popups go away and the user has notifications in the message center.
232 base::TimeDelta first_run_idle_timeout_
;
234 // Provides weak pointers for the purpose of the first run timer.
235 base::WeakPtrFactory
<MessageCenterNotificationManager
> weak_factory_
;
238 scoped_ptr
<message_center::NotifierSettingsProvider
> settings_provider_
;
240 // To own the blockers.
241 ScopedVector
<message_center::NotificationBlocker
> blockers_
;
243 // Registrar for the other kind of notifications (event signaling).
244 content::NotificationRegistrar registrar_
;
246 NotificationSystemObserver system_observer_
;
248 // Keeps track of all notification statistics for UMA purposes.
249 MessageCenterStatsCollector stats_collector_
;
251 // Keeps track of notifications specific to Google Now for UMA purposes.
252 GoogleNowNotificationStatsCollector google_now_stats_collector_
;
254 DISALLOW_COPY_AND_ASSIGN(MessageCenterNotificationManager
);
257 #endif // CHROME_BROWSER_NOTIFICATIONS_MESSAGE_CENTER_NOTIFICATION_MANAGER_H_