Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / notifications / message_center_notification_manager.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_NOTIFICATIONS_MESSAGE_CENTER_NOTIFICATION_MANAGER_H_
6 #define CHROME_BROWSER_NOTIFICATIONS_MESSAGE_CENTER_NOTIFICATION_MANAGER_H_
7
8 #include <map>
9 #include <string>
10
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"
26
27 class MessageCenterSettingsController;
28 class Notification;
29 class PrefRegistrySimple;
30 class PrefService;
31 class Profile;
32 class ProfileNotification;
33
34 namespace message_center {
35 class NotificationBlocker;
36 FORWARD_DECLARE_TEST(WebNotificationTrayTest, ManuallyCloseMessageCenter);
37 }
38
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:
45   MessageCenterNotificationManager(
46       message_center::MessageCenter* message_center,
47       PrefService* local_state,
48       scoped_ptr<message_center::NotifierSettingsProvider> settings_provider);
49   ~MessageCenterNotificationManager() override;
50
51   // Registers preferences.
52   static void RegisterPrefs(PrefRegistrySimple* registry);
53
54   // NotificationUIManager
55   void Add(const Notification& notification, Profile* profile) override;
56   bool Update(const Notification& notification, Profile* profile) override;
57   const Notification* FindById(const std::string& delegate_id,
58                                ProfileID profile_id) const override;
59   bool CancelById(const std::string& delegate_id,
60                   ProfileID profile_id) override;
61   std::set<std::string> GetAllIdsByProfileAndSourceOrigin(
62       Profile* profile,
63       const GURL& source) override;
64   bool CancelAllBySourceOrigin(const GURL& source_origin) override;
65   bool CancelAllByProfile(ProfileID profile_id) override;
66   void CancelAll() override;
67
68   // MessageCenterObserver
69   void OnNotificationRemoved(const std::string& notification_id,
70                              bool by_user) override;
71   void OnCenterVisibilityChanged(message_center::Visibility) override;
72   void OnNotificationUpdated(const std::string& notification_id) override;
73
74   void EnsureMessageCenterClosed();
75
76 #if defined(OS_WIN)
77   // Called when the pref changes for the first run balloon. The first run
78   // balloon is only displayed on Windows, since the visibility of the tray
79   // icon is limited.
80   void DisplayFirstRunBalloon();
81
82   void SetFirstRunTimeoutForTest(base::TimeDelta timeout);
83   bool FirstRunTimerIsActive() const;
84 #endif
85
86   // Takes ownership of |delegate|.
87   void SetMessageCenterTrayDelegateForTest(
88       message_center::MessageCenterTrayDelegate* delegate);
89
90   // Returns the notification id which this manager will use to add to message
91   // center, for this combination of delegate id and profile.
92   std::string GetMessageCenterNotificationIdForTest(
93       const std::string& delegate_id, Profile* profile);
94
95  private:
96   // Adds |profile_notification| to an alternative provider extension or app.
97   void AddNotificationToAlternateProvider(
98       ProfileNotification* profile_notification,
99       const std::string& extension_id) const;
100
101   FRIEND_TEST_ALL_PREFIXES(message_center::WebNotificationTrayTest,
102                            ManuallyCloseMessageCenter);
103
104   scoped_ptr<message_center::MessageCenterTrayDelegate> tray_;
105   message_center::MessageCenter* message_center_;  // Weak, global.
106
107   // Use a map by notification_id since this mapping is the most often used.
108   typedef std::map<std::string, ProfileNotification*> NotificationMap;
109   NotificationMap profile_notifications_;
110
111   // Helpers that add/remove the notification from local map.
112   // The local map takes ownership of profile_notification object.
113   void AddProfileNotification(ProfileNotification* profile_notification);
114   void RemoveProfileNotification(ProfileNotification* profile_notification);
115
116   // Returns the ProfileNotification for the |id|, or NULL if no such
117   // notification is found.
118   ProfileNotification* FindProfileNotification(const std::string& id) const;
119
120   // Get the extension ID of the extension that the user chose to take over
121   // Chorme Notification Center.
122   std::string GetExtensionTakingOverNotifications(Profile* profile);
123
124 #if defined(OS_WIN)
125   // This function is run on update to ensure that the notification balloon is
126   // shown only when there are no popups present.
127   void CheckFirstRunTimer();
128
129   // |first_run_pref_| is used to keep track of whether we've ever shown the
130   // first run balloon before, even across restarts.
131   BooleanPrefMember first_run_pref_;
132
133   // The timer after which we will show the first run balloon.  This timer is
134   // restarted every time the message center is closed and every time the last
135   // popup disappears from the screen.
136   base::OneShotTimer<MessageCenterNotificationManager> first_run_balloon_timer_;
137
138   // The first-run balloon will be shown |first_run_idle_timeout_| after all
139   // popups go away and the user has notifications in the message center.
140   base::TimeDelta first_run_idle_timeout_;
141
142   // Provides weak pointers for the purpose of the first run timer.
143   base::WeakPtrFactory<MessageCenterNotificationManager> weak_factory_;
144 #endif
145
146   scoped_ptr<message_center::NotifierSettingsProvider> settings_provider_;
147
148   // To own the blockers.
149   ScopedVector<message_center::NotificationBlocker> blockers_;
150
151   NotificationSystemObserver system_observer_;
152
153   // Keeps track of all notification statistics for UMA purposes.
154   MessageCenterStatsCollector stats_collector_;
155
156   // Keeps track of notifications specific to Google Now for UMA purposes.
157   GoogleNowNotificationStatsCollector google_now_stats_collector_;
158
159   DISALLOW_COPY_AND_ASSIGN(MessageCenterNotificationManager);
160 };
161
162 #endif  // CHROME_BROWSER_NOTIFICATIONS_MESSAGE_CENTER_NOTIFICATION_MANAGER_H_