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_SYNC_NOTIFIER_CHROME_NOTIFIER_SERVICE_H_
6 #define CHROME_BROWSER_NOTIFICATIONS_SYNC_NOTIFIER_CHROME_NOTIFIER_SERVICE_H_
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/prefs/pref_member.h"
13 #include "base/threading/non_thread_safe.h"
14 #include "chrome/browser/notifications/sync_notifier/synced_notification.h"
15 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
16 #include "sync/api/syncable_service.h"
18 class NotificationUIManager
;
21 namespace user_prefs
{
22 class PrefRegistrySyncable
;
25 namespace message_center
{
31 extern const char kFirstSyncedNotificationServiceId
[];
32 extern const char kServiceEnabledOnce
[];
33 extern const char kSyncedNotificationFirstRun
[];
35 enum ChromeNotifierServiceActionType
{
36 CHROME_NOTIFIER_SERVICE_ACTION_UNKNOWN
,
37 CHROME_NOTIFIER_SERVICE_ACTION_FIRST_SERVICE_ENABLED
,
38 CHROME_NOTIFIER_SERVICE_ACTION_FIRST_SERVICE_DISABLED
,
39 // NOTE: Add new action types only immediately above this line. Also,
40 // make sure the enum list in tools/histogram/histograms.xml is
41 // updated with any change in here.
42 CHROME_NOTIFIER_SERVICE_ACTION_COUNT
45 // The ChromeNotifierService holds notifications which represent the state of
46 // delivered notifications for chrome. These are obtained from the sync service
47 // and kept up to date.
48 class ChromeNotifierService
: public syncer::SyncableService
,
49 public BrowserContextKeyedService
{
51 ChromeNotifierService(Profile
* profile
, NotificationUIManager
* manager
);
52 virtual ~ChromeNotifierService();
54 // Methods from BrowserContextKeyedService.
55 virtual void Shutdown() OVERRIDE
;
57 // syncer::SyncableService implementation.
58 virtual syncer::SyncMergeResult
MergeDataAndStartSyncing(
59 syncer::ModelType type
,
60 const syncer::SyncDataList
& initial_sync_data
,
61 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor
,
62 scoped_ptr
<syncer::SyncErrorFactory
> error_handler
) OVERRIDE
;
63 virtual void StopSyncing(syncer::ModelType type
) OVERRIDE
;
64 virtual syncer::SyncDataList
GetAllSyncData(
65 syncer::ModelType type
) const OVERRIDE
;
66 virtual syncer::SyncError
ProcessSyncChanges(
67 const tracked_objects::Location
& from_here
,
68 const syncer::SyncChangeList
& change_list
) OVERRIDE
;
70 // Convert from internal representation to SyncData representation.
71 static syncer::SyncData
CreateSyncDataFromNotification(
72 const SyncedNotification
& notification
);
74 // Convert from SyncData representation to internal representation.
75 static scoped_ptr
<SyncedNotification
> CreateNotificationFromSyncData(
76 const syncer::SyncData
& sync_data
);
78 // Get a pointer to a notification. ChromeNotifierService owns this pointer.
79 virtual notifier::SyncedNotification
* FindNotificationById(
80 const std::string
& notification_id
);
82 // Get the list of synced notification services and fill their meta data to
84 void GetSyncedNotificationServices(
85 std::vector
<message_center::Notifier
*>* notifiers
);
87 // Called when we dismiss a notification. This is virtual so that test
88 // subclasses can override it.
89 virtual void MarkNotificationAsRead(const std::string
& id
);
91 // Called when a notier is enabled or disabled.
92 void OnSyncedNotificationServiceEnabled(
93 const std::string
& notifier_id
,
96 // Register the preferences we use to save state.
97 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
99 Profile
* profile() const { return profile_
; }
101 // Functions for test.
102 void AddForTest(scoped_ptr
<notifier::SyncedNotification
> notification
);
104 // If we allow the tests to do bitmap fetching, they will attempt to fetch
105 // a URL from the web, which will fail. We can already test the majority
106 // of what we want without also trying to fetch bitmaps. Other tests will
107 // cover bitmap fetching.
108 static void set_avoid_bitmap_fetching_for_test(bool avoid
) {
109 avoid_bitmap_fetching_for_test_
= avoid
;
112 // Initialize the preferences we use for the ChromeNotificationService.
113 void InitializePrefs();
116 // Add a notification to our list. This takes ownership of the pointer.
117 void Add(scoped_ptr
<notifier::SyncedNotification
> notification
);
119 // Display this notification in the notification center, or remove it.
120 void UpdateInMessageCenter(notifier::SyncedNotification
* notification
);
122 // Display a notification in the notification center (eventually).
123 void Display(notifier::SyncedNotification
* notification
);
125 // Remove a notification from our store.
126 void FreeNotificationById(const std::string
& notification_id
);
128 // When a service it turned on, scan our cache for any notifications
129 // for that service, and display them if they are unread.
130 void DisplayUnreadNotificationsFromSource(const std::string
& notifier_id
);
132 // When a service it turned off, scan our cache for any notifications
133 // for that service, and remove them from the message center.
134 void RemoveUnreadNotificationsFromSource(const std::string
& notifier_id
);
136 // When we turn a sending service on or off, collect statistics about
137 // how often users turn it on or off.
138 void CollectPerServiceEnablingStatistics(const std::string
& notifier_id
,
141 // When we start up or hear of a new service, turn it on by default.
142 void AddNewSendingServices();
144 // Called when the string list pref has been changed.
145 void OnEnabledSendingServiceListPrefChanged(std::set
<std::string
>* ids_field
);
147 // Called when the string list pref has been changed.
148 void OnInitializedSendingServiceListPrefChanged(
149 std::set
<std::string
>* ids_field
);
151 // Called when our "first run" boolean pref has been changed.
152 void OnSyncedNotificationFirstRunBooleanPrefChanged(bool* new_value
);
154 // Convert our internal set of strings to a list value.
155 // The second param is an outparam which the function fills in.
156 void BuildServiceListValueInplace(
157 std::set
<std::string
> services
, base::ListValue
* list_value
);
159 // Preferences for storing which SyncedNotificationServices are enabled
160 StringListPrefMember enabled_sending_services_prefs_
;
161 StringListPrefMember initialized_sending_services_prefs_
;
163 // Preferences to avoid toasting on SyncedNotification first run.
164 BooleanPrefMember synced_notification_first_run_prefs_
;
166 // Back pointer to the owning profile.
167 Profile
* const profile_
;
168 NotificationUIManager
* const notification_manager_
;
169 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor_
;
170 std::set
<std::string
> enabled_sending_services_
;
171 std::set
<std::string
> initialized_sending_services_
;
172 bool synced_notification_first_run_
;
173 static bool avoid_bitmap_fetching_for_test_
;
175 // TODO(petewil): Consider whether a map would better suit our data.
176 // If there are many entries, lookup time may trump locality of reference.
177 ScopedVector
<notifier::SyncedNotification
> notification_data_
;
179 friend class ChromeNotifierServiceTest
;
180 FRIEND_TEST_ALL_PREFIXES(ChromeNotifierServiceTest
, ServiceEnabledTest
);
181 FRIEND_TEST_ALL_PREFIXES(ChromeNotifierServiceTest
,
182 AddNewSendingServicesTest
);
183 FRIEND_TEST_ALL_PREFIXES(ChromeNotifierServiceTest
,
184 GetEnabledSendingServicesFromPreferencesTest
);
187 DISALLOW_COPY_AND_ASSIGN(ChromeNotifierService
);
190 } // namespace notifier
192 #endif // CHROME_BROWSER_NOTIFICATIONS_SYNC_NOTIFIER_CHROME_NOTIFIER_SERVICE_H_