2ab38270ed94e7293145b57ac72a06bd4c729d4c
[platform/framework/web/crosswalk.git] / src / chrome / browser / notifications / sync_notifier / synced_notification_app_info_service.h
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.
4
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_
7
8 #include <string>
9
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"
17
18 class NotificationUIManager;
19 class Profile;
20
21 namespace sync_pb {
22 class SyncedNotificationAppInfo;
23 }  // namespace sync_pb
24
25 namespace notifier {
26
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;
37 };
38
39
40 class ChromeNotifierService;
41
42 // The SyncedNotificationAppInfoService contains and syncs AppInfo protobufs
43 // from the server with metadata about the services sending synced
44 // notifications.
45 class SyncedNotificationAppInfoService : public syncer::SyncableService,
46                                          public KeyedService {
47  public:
48   explicit SyncedNotificationAppInfoService(Profile* profile);
49   virtual ~SyncedNotificationAppInfoService();
50
51   // Methods from KeyedService.
52   virtual void Shutdown() OVERRIDE;
53
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;
61
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;
65
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;
70
71   // Get all data currently on this clients.
72   virtual syncer::SyncDataList GetAllSyncData(syncer::ModelType type)
73       const OVERRIDE;
74
75   // Handles the Add and Update cases.
76   void ProcessIncomingAppInfoProtobuf(
77       const sync_pb::SyncedNotificationAppInfo& app_info);
78
79   // Handles the Removed case.
80   void ProcessRemovedAppInfoProtobuf(
81       const sync_pb::SyncedNotificationAppInfo& app_info);
82
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);
86
87   // Convert the protobuf to our internal format.
88   scoped_ptr<SyncedNotificationAppInfo>
89       CreateSyncedNotificationAppInfoFromProtobuf(
90           const sync_pb::SyncedNotificationAppInfo& app_info);
91
92   // Get the app info that contains this sending service name.
93   SyncedNotificationAppInfo* FindSyncedNotificationAppInfoByName(
94       const std::string& name);
95
96   // Get the app info that contains this app id.
97   SyncedNotificationAppInfo* FindSyncedNotificationAppInfoByAppId(
98       const std::string& app_id);
99
100   // Lookup the sending service name for a given app id.
101   std::string FindSendingServiceNameFromAppId(const std::string app_id);
102
103   // Return a list of all sending service names.
104   std::vector<SyncedNotificationSendingServiceSettingsData>
105   GetAllSendingServiceSettingsData();
106
107   void set_chrome_notifier_service(ChromeNotifierService* notifier) {
108     chrome_notifier_service_ = notifier;
109   }
110
111   // Functions for test.
112   void AddForTest(
113       scoped_ptr<notifier::SyncedNotificationAppInfo> sending_service_info) {
114     Add(sending_service_info.Pass());
115   }
116
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;
123   }
124
125  private:
126   // Add an app_info object to our list.  This takes ownership of the pointer.
127   void Add(
128       scoped_ptr<notifier::SyncedNotificationAppInfo> sending_service_info);
129
130   // Remove this app info.
131   void FreeSyncedNotificationAppInfoByName(const std::string& name);
132
133   size_t sending_service_infos_size() { return sending_service_infos_.size(); }
134
135   // Back pointer to the owning profile.
136   Profile* const profile_;
137
138   // Debug testing member for checking thread is as expected.
139   base::ThreadChecker thread_checker_;
140
141   // Member list of AppInfo objects.
142   ScopedVector<notifier::SyncedNotificationAppInfo> sending_service_infos_;
143
144   // Cache of the sync info.
145   syncer::SyncData sync_data_;
146
147   // Don't let unit tests try to hit the network.
148   static bool avoid_bitmap_fetching_for_test_;
149
150   // Pointer to the ChromeNotifierService.  Its lifetime is controlled by the
151   // ChromeNotifierService itself, which will zero out the pointer when it is
152   // destroyed.
153   ChromeNotifierService* chrome_notifier_service_;
154
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);
177 };
178
179 }  // namespace notifier
180
181 #endif  // CHROME_BROWSER_NOTIFICATIONS_SYNC_NOTIFIER_SYNCED_NOTIFICATION_APP_INFO_SERVICE_H_