Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / notifications / message_center_notification_manager.h
blobe3c5929c12afb9aa69a82e2de152fb725fd0f2e8
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/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 "ui/message_center/message_center.h"
23 #include "ui/message_center/message_center_observer.h"
24 #include "ui/message_center/message_center_tray_delegate.h"
25 #include "ui/message_center/message_center_types.h"
27 class MessageCenterSettingsController;
28 class Notification;
29 class PrefRegistrySimple;
30 class PrefService;
31 class Profile;
33 namespace message_center {
34 class NotificationBlocker;
35 FORWARD_DECLARE_TEST(WebNotificationTrayTest, ManuallyCloseMessageCenter);
38 // This class extends NotificationUIManagerImpl and delegates actual display
39 // of notifications to MessageCenter, doing necessary conversions.
40 class MessageCenterNotificationManager
41 : public NotificationUIManager,
42 public message_center::MessageCenterObserver {
43 public:
44 MessageCenterNotificationManager(
45 message_center::MessageCenter* message_center,
46 PrefService* local_state,
47 scoped_ptr<message_center::NotifierSettingsProvider> settings_provider);
48 virtual ~MessageCenterNotificationManager();
50 // Registers preferences.
51 static void RegisterPrefs(PrefRegistrySimple* registry);
53 // NotificationUIManager
54 virtual void Add(const Notification& notification,
55 Profile* profile) override;
56 virtual bool Update(const Notification& notification,
57 Profile* profile) override;
58 virtual const Notification* FindById(const std::string& delegate_id,
59 ProfileID profile_id) const override;
60 virtual bool CancelById(const std::string& delegate_id,
61 ProfileID profile_id) override;
62 virtual std::set<std::string> GetAllIdsByProfileAndSourceOrigin(
63 Profile* profile,
64 const GURL& source) override;
65 virtual bool CancelAllBySourceOrigin(const GURL& source_origin) override;
66 virtual bool CancelAllByProfile(ProfileID profile_id) override;
67 virtual void CancelAll() override;
69 // MessageCenterObserver
70 virtual void OnNotificationRemoved(const std::string& notification_id,
71 bool by_user) override;
72 virtual void OnCenterVisibilityChanged(message_center::Visibility) override;
73 virtual void OnNotificationUpdated(const std::string& notification_id)
74 override;
76 void EnsureMessageCenterClosed();
78 #if defined(OS_WIN)
79 // Called when the pref changes for the first run balloon. The first run
80 // balloon is only displayed on Windows, since the visibility of the tray
81 // icon is limited.
82 void DisplayFirstRunBalloon();
84 void SetFirstRunTimeoutForTest(base::TimeDelta timeout);
85 bool FirstRunTimerIsActive() const;
86 #endif
88 // Takes ownership of |delegate|.
89 void SetMessageCenterTrayDelegateForTest(
90 message_center::MessageCenterTrayDelegate* delegate);
92 // Returns the notification id which this manager will use to add to message
93 // center, for this combination of delegate id and profile.
94 std::string GetMessageCenterNotificationIdForTest(
95 const std::string& delegate_id, Profile* profile);
97 private:
98 FRIEND_TEST_ALL_PREFIXES(message_center::WebNotificationTrayTest,
99 ManuallyCloseMessageCenter);
101 // This class keeps a set of original Notification objects and corresponding
102 // Profiles, so when MessageCenter calls back with a notification_id, this
103 // class has necessary mapping to other source info - for example, it calls
104 // NotificationDelegate supplied by client when someone clicks on a
105 // Notification in MessageCenter. Likewise, if a Profile or Extension is
106 // being removed, the map makes it possible to revoke the notifications from
107 // MessageCenter. To keep that set, we use the private ProfileNotification
108 // class that stores a superset of all information about a notification.
110 // TODO(dimich): Consider merging all 4 types (Notification,
111 // QueuedNotification, ProfileNotification and NotificationList::Notification)
112 // into a single class.
113 class ProfileNotification {
114 public:
115 ProfileNotification(Profile* profile,
116 const Notification& notification,
117 message_center::MessageCenter* message_center);
118 virtual ~ProfileNotification();
120 Profile* profile() const { return profile_; }
121 const Notification& notification() const { return notification_; }
123 // Route a new notification to an app/extension.
124 void AddToAlternateProvider(const std::string extension_id);
126 private:
127 // Weak, guaranteed not to be used after profile removal by parent class.
128 Profile* profile_;
129 Notification notification_;
132 scoped_ptr<message_center::MessageCenterTrayDelegate> tray_;
133 message_center::MessageCenter* message_center_; // Weak, global.
135 // Use a map by notification_id since this mapping is the most often used.
136 typedef std::map<std::string, ProfileNotification*> NotificationMap;
137 NotificationMap profile_notifications_;
139 // Helpers that add/remove the notification from local map.
140 // The local map takes ownership of profile_notification object.
141 void AddProfileNotification(ProfileNotification* profile_notification);
142 void RemoveProfileNotification(ProfileNotification* profile_notification);
144 // Returns the ProfileNotification for the |id|, or NULL if no such
145 // notification is found.
146 ProfileNotification* FindProfileNotification(const std::string& id) const;
148 // Get the extension ID of the extension that the user chose to take over
149 // Chorme Notification Center.
150 std::string GetExtensionTakingOverNotifications(Profile* profile);
152 #if defined(OS_WIN)
153 // This function is run on update to ensure that the notification balloon is
154 // shown only when there are no popups present.
155 void CheckFirstRunTimer();
157 // |first_run_pref_| is used to keep track of whether we've ever shown the
158 // first run balloon before, even across restarts.
159 BooleanPrefMember first_run_pref_;
161 // The timer after which we will show the first run balloon. This timer is
162 // restarted every time the message center is closed and every time the last
163 // popup disappears from the screen.
164 base::OneShotTimer<MessageCenterNotificationManager> first_run_balloon_timer_;
166 // The first-run balloon will be shown |first_run_idle_timeout_| after all
167 // popups go away and the user has notifications in the message center.
168 base::TimeDelta first_run_idle_timeout_;
170 // Provides weak pointers for the purpose of the first run timer.
171 base::WeakPtrFactory<MessageCenterNotificationManager> weak_factory_;
172 #endif
174 scoped_ptr<message_center::NotifierSettingsProvider> settings_provider_;
176 // To own the blockers.
177 ScopedVector<message_center::NotificationBlocker> blockers_;
179 NotificationSystemObserver system_observer_;
181 // Keeps track of all notification statistics for UMA purposes.
182 MessageCenterStatsCollector stats_collector_;
184 // Keeps track of notifications specific to Google Now for UMA purposes.
185 GoogleNowNotificationStatsCollector google_now_stats_collector_;
187 DISALLOW_COPY_AND_ASSIGN(MessageCenterNotificationManager);
190 #endif // CHROME_BROWSER_NOTIFICATIONS_MESSAGE_CENTER_NOTIFICATION_MANAGER_H_