1 // Copyright 2014 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_SYNCED_NOTIFICATION_APP_INFO_SERVICE_H_
6 #define CHROME_BROWSER_NOTIFICATIONS_SYNC_NOTIFIER_SYNCED_NOTIFICATION_APP_INFO_SERVICE_H_
10 #include "base/basictypes.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/threading/thread_checker.h"
14 #include "chrome/browser/notifications/sync_notifier/synced_notification_app_info.h"
15 #include "components/keyed_service/core/keyed_service.h"
16 #include "sync/api/syncable_service.h"
18 class NotificationUIManager;
22 class SyncedNotificationAppInfo;
23 } // namespace sync_pb
27 // Information that the ChromeNotifierService needs from this app info to be
28 // able to properly enable and disable the sending services.
29 struct SyncedNotificationSendingServiceSettingsData {
30 SyncedNotificationSendingServiceSettingsData(
31 std::string settings_display_name,
32 gfx::Image settings_icon,
33 message_center::NotifierId notifier_id);
34 std::string settings_display_name;
35 gfx::Image settings_icon;
36 message_center::NotifierId notifier_id;
40 class ChromeNotifierService;
42 // The SyncedNotificationAppInfoService contains and syncs AppInfo protobufs
43 // from the server with metadata about the services sending synced
45 class SyncedNotificationAppInfoService : public syncer::SyncableService,
48 explicit SyncedNotificationAppInfoService(Profile* profile);
49 virtual ~SyncedNotificationAppInfoService();
51 // Methods from KeyedService.
52 virtual void Shutdown() OVERRIDE;
54 // syncer::SyncableService implementation.
55 // This is called at chrome startup with the data from sync.
56 virtual syncer::SyncMergeResult MergeDataAndStartSyncing(
57 syncer::ModelType type,
58 const syncer::SyncDataList& initial_sync_data,
59 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
60 scoped_ptr<syncer::SyncErrorFactory> error_handler) OVERRIDE;
62 // This does not normally get called, since this is not a user selectable
63 // data type, but it could get called if an error occurs at shutdown.
64 virtual void StopSyncing(syncer::ModelType type) OVERRIDE;
66 // Process incoming changes from the server.
67 virtual syncer::SyncError ProcessSyncChanges(
68 const tracked_objects::Location& from_here,
69 const syncer::SyncChangeList& change_list) OVERRIDE;
71 // Get all data currently on this clients.
72 virtual syncer::SyncDataList GetAllSyncData(syncer::ModelType type)
75 // Handles the Add and Update cases.
76 void ProcessIncomingAppInfoProtobuf(
77 const sync_pb::SyncedNotificationAppInfo& app_info);
79 // Handles the Removed case.
80 void ProcessRemovedAppInfoProtobuf(
81 const sync_pb::SyncedNotificationAppInfo& app_info);
83 // When the bitmaps are all ready, tell ChromeNotifierService about changes.
84 virtual void OnBitmapFetchesDone(std::vector<std::string> added_app_ids,
85 std::vector<std::string> removed_app_ids);
87 // Convert the protobuf to our internal format.
88 scoped_ptr<SyncedNotificationAppInfo>
89 CreateSyncedNotificationAppInfoFromProtobuf(
90 const sync_pb::SyncedNotificationAppInfo& app_info);
92 // Get the app info that contains this sending service name.
93 SyncedNotificationAppInfo* FindSyncedNotificationAppInfoByName(
94 const std::string& name);
96 // Get the app info that contains this app id.
97 SyncedNotificationAppInfo* FindSyncedNotificationAppInfoByAppId(
98 const std::string& app_id);
100 // Lookup the sending service name for a given app id.
101 std::string FindSendingServiceNameFromAppId(const std::string app_id);
103 // Return a list of all sending service names.
104 std::vector<SyncedNotificationSendingServiceSettingsData>
105 GetAllSendingServiceSettingsData();
107 void set_chrome_notifier_service(ChromeNotifierService* notifier) {
108 chrome_notifier_service_ = notifier;
111 // Functions for test.
113 scoped_ptr<notifier::SyncedNotificationAppInfo> sending_service_info) {
114 Add(sending_service_info.Pass());
117 // If we allow the tests to do bitmap fetching, they will attempt to fetch
118 // a URL from the web, which will fail. We can already test the majority
119 // of what we want without also trying to fetch bitmaps. Other tests will
120 // cover bitmap fetching.
121 static void set_avoid_bitmap_fetching_for_test(bool avoid) {
122 avoid_bitmap_fetching_for_test_ = avoid;
126 // Add an app_info object to our list. This takes ownership of the pointer.
128 scoped_ptr<notifier::SyncedNotificationAppInfo> sending_service_info);
130 // Remove this app info.
131 void FreeSyncedNotificationAppInfoByName(const std::string& name);
133 size_t sending_service_infos_size() { return sending_service_infos_.size(); }
135 // Back pointer to the owning profile.
136 Profile* const profile_;
138 // Debug testing member for checking thread is as expected.
139 base::ThreadChecker thread_checker_;
141 // Member list of AppInfo objects.
142 ScopedVector<notifier::SyncedNotificationAppInfo> sending_service_infos_;
144 // Cache of the sync info.
145 syncer::SyncData sync_data_;
147 // Don't let unit tests try to hit the network.
148 static bool avoid_bitmap_fetching_for_test_;
150 // Pointer to the ChromeNotifierService. Its lifetime is controlled by the
151 // ChromeNotifierService itself, which will zero out the pointer when it is
153 ChromeNotifierService* chrome_notifier_service_;
155 friend class SyncedNotificationAppInfoServiceTest;
156 FRIEND_TEST_ALL_PREFIXES(SyncedNotificationAppInfoServiceTest,
157 MergeDataAndStartSyncingTest);
158 FRIEND_TEST_ALL_PREFIXES(SyncedNotificationAppInfoServiceTest,
159 ProcessSyncChangesEmptyModel);
160 FRIEND_TEST_ALL_PREFIXES(SyncedNotificationAppInfoServiceTest,
161 ProcessSyncChangesNonEmptyModel);
162 FRIEND_TEST_ALL_PREFIXES(SyncedNotificationAppInfoServiceTest,
163 FindSyncedNotificationAppInfoByNameTest);
164 FRIEND_TEST_ALL_PREFIXES(SyncedNotificationAppInfoServiceTest,
165 FindSyncedNotificationAppInfoByNameTestTest);
166 FRIEND_TEST_ALL_PREFIXES(SyncedNotificationAppInfoServiceTest,
167 FreeSyncedNotificationAppInfoByNameTest);
168 FRIEND_TEST_ALL_PREFIXES(SyncedNotificationAppInfoServiceTest,
169 CreateSyncedNotificationAppInfoFromProtobufTest);
170 FRIEND_TEST_ALL_PREFIXES(SyncedNotificationAppInfoServiceTest,
171 ProcessIncomingAppInfoProtobufAddTest);
172 FRIEND_TEST_ALL_PREFIXES(SyncedNotificationAppInfoServiceTest,
173 ProcessIncomingAppInfoProtobufUpdateTest);
174 FRIEND_TEST_ALL_PREFIXES(SyncedNotificationAppInfoServiceTest,
175 ProcessRemovedAppInfoProtobufTest);
176 DISALLOW_COPY_AND_ASSIGN(SyncedNotificationAppInfoService);
179 } // namespace notifier
181 #endif // CHROME_BROWSER_NOTIFICATIONS_SYNC_NOTIFIER_SYNCED_NOTIFICATION_APP_INFO_SERVICE_H_