Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / app_list / app_list_syncable_service.h
1 // Copyright 2013 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_UI_APP_LIST_APP_LIST_SYNCABLE_SERVICE_H_
6 #define CHROME_BROWSER_UI_APP_LIST_APP_LIST_SYNCABLE_SERVICE_H_
7
8 #include <map>
9
10 #include "base/memory/scoped_ptr.h"
11 #include "chrome/browser/apps/drive/drive_app_uninstall_sync_service.h"
12 #include "chrome/browser/sync/glue/sync_start_util.h"
13 #include "components/keyed_service/core/keyed_service.h"
14 #include "content/public/browser/notification_observer.h"
15 #include "content/public/browser/notification_registrar.h"
16 #include "sync/api/string_ordinal.h"
17 #include "sync/api/sync_change.h"
18 #include "sync/api/sync_change_processor.h"
19 #include "sync/api/sync_error_factory.h"
20 #include "sync/api/syncable_service.h"
21 #include "sync/protocol/app_list_specifics.pb.h"
22
23 class DriveAppProvider;
24 class ExtensionAppModelBuilder;
25 class Profile;
26
27 namespace extensions {
28 class ExtensionSystem;
29 }
30
31 namespace sync_pb {
32 class AppListSpecifics;
33 }
34
35 namespace app_list {
36
37 class AppListFolderItem;
38 class AppListItem;
39 class AppListModel;
40 class ModelPrefUpdater;
41
42 // Keyed Service that owns, stores, and syncs an AppListModel for a profile.
43 class AppListSyncableService : public syncer::SyncableService,
44                                public KeyedService,
45                                public DriveAppUninstallSyncService,
46                                public content::NotificationObserver {
47  public:
48   struct SyncItem {
49     SyncItem(const std::string& id,
50              sync_pb::AppListSpecifics::AppListItemType type);
51     ~SyncItem();
52     const std::string item_id;
53     sync_pb::AppListSpecifics::AppListItemType item_type;
54     std::string item_name;
55     std::string parent_id;
56     syncer::StringOrdinal item_ordinal;
57
58     std::string ToString() const;
59   };
60
61   // Populates the model when |extension_system| is ready.
62   AppListSyncableService(Profile* profile,
63                          extensions::ExtensionSystem* extension_system);
64
65   ~AppListSyncableService() override;
66
67   // Adds |item| to |sync_items_| and |model_|. If a sync item already exists,
68   // updates the existing sync item instead.
69   void AddItem(scoped_ptr<AppListItem> app_item);
70
71   // Removes sync item matching |id|.
72   void RemoveItem(const std::string& id);
73
74   // Called when properties of an item may have changed, e.g. default/oem state.
75   void UpdateItem(AppListItem* app_item);
76
77   // Returns the existing sync item matching |id| or NULL.
78   const SyncItem* GetSyncItem(const std::string& id) const;
79
80   // Sets the name of the folder for OEM apps.
81   void SetOemFolderName(const std::string& name);
82
83   Profile* profile() { return profile_; }
84   AppListModel* model() { return model_.get(); }
85   size_t GetNumSyncItemsForTest() const { return sync_items_.size(); }
86   const std::string& GetOemFolderNameForTest() const {
87     return oem_folder_name_;
88   }
89   void ResetDriveAppProviderForTest();
90
91   // syncer::SyncableService
92   syncer::SyncMergeResult MergeDataAndStartSyncing(
93       syncer::ModelType type,
94       const syncer::SyncDataList& initial_sync_data,
95       scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
96       scoped_ptr<syncer::SyncErrorFactory> error_handler) override;
97   void StopSyncing(syncer::ModelType type) override;
98   syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override;
99   syncer::SyncError ProcessSyncChanges(
100       const tracked_objects::Location& from_here,
101       const syncer::SyncChangeList& change_list) override;
102
103  private:
104   class ModelObserver;
105   typedef std::map<std::string, SyncItem*> SyncItemMap;
106
107   // KeyedService
108   void Shutdown() override;
109
110   // DriveAppUninstallSyncService
111   void TrackUninstalledDriveApp(const std::string& drive_app_id) override;
112   void UntrackUninstalledDriveApp(const std::string& drive_app_id) override;
113
114   // content::NotificationObserver
115   void Observe(int type,
116                const content::NotificationSource& source,
117                const content::NotificationDetails& details) override;
118
119   // Builds the model once ExtensionService is ready.
120   void BuildModel();
121
122   // Returns true if sync has restarted, otherwise runs |flare_|.
123   bool SyncStarted();
124
125   // If |app_item| matches an existing sync item, returns it. Otherwise adds
126   // |app_item| to |sync_items_| and returns the new item. If |app_item| is
127   // invalid returns NULL.
128   SyncItem* FindOrAddSyncItem(AppListItem* app_item);
129
130   // Creates a sync item for |app_item| and sends an ADD SyncChange event.
131   SyncItem* CreateSyncItemFromAppItem(AppListItem* app_item);
132
133   // If a sync item for |app_item| already exists, update |app_item| from the
134   // sync item, otherwise create a new sync item from |app_item|.
135   void AddOrUpdateFromSyncItem(AppListItem* app_item);
136
137   // Either uninstalling a default app or remove the REMOVE_DEFAULT sync item.
138   // Returns true if the app is removed. Otherwise deletes the existing sync
139   // item and returns false.
140   bool RemoveDefaultApp(AppListItem* item, SyncItem* sync_item);
141
142   // Deletes a sync item from |sync_items_| and sends a DELETE action.
143   void DeleteSyncItem(SyncItem* sync_item);
144
145   // Updates existing entry in |sync_items_| from |app_item|.
146   void UpdateSyncItem(AppListItem* app_item);
147
148   // Removes sync item matching |id|.
149   void RemoveSyncItem(const std::string& id);
150
151   // Updates folder items that may get created during initial sync.
152   void ResolveFolderPositions();
153
154   // Removes any empty SyncItem folders and deletes them from sync. Called
155   // after a sync item is removed (which may result in an empty folder).
156   void PruneEmptySyncFolders();
157
158   // Creates or updates a SyncItem from |specifics|. Returns true if a new item
159   // was created.
160   bool ProcessSyncItemSpecifics(const sync_pb::AppListSpecifics& specifics);
161
162   // Handles a newly created sync item (e.g. creates a new AppItem and adds it
163   // to the model or uninstalls a deleted default item.
164   void ProcessNewSyncItem(SyncItem* sync_item);
165
166   // Handles an existing sync item.
167   void ProcessExistingSyncItem(SyncItem* sync_item);
168
169   // Updates |app_item| from |sync_item| (e.g. updates item positions).
170   void UpdateAppItemFromSyncItem(const SyncItem* sync_item,
171                                  AppListItem* app_item);
172
173   // Sends ADD or CHANGED for sync item.
174   void SendSyncChange(SyncItem* sync_item,
175                       syncer::SyncChange::SyncChangeType sync_change_type);
176
177   // Returns an existing SyncItem corresponding to |item_id| or NULL.
178   SyncItem* FindSyncItem(const std::string& item_id);
179
180   // Creates a new sync item for |item_id|.
181   SyncItem* CreateSyncItem(
182       const std::string& item_id,
183       sync_pb::AppListSpecifics::AppListItemType item_type);
184
185   // Deletes a SyncItem matching |specifics|.
186   void DeleteSyncItemSpecifics(const sync_pb::AppListSpecifics& specifics);
187
188   // Creates the OEM folder and sets its name if necessary. Returns the OEM
189   // folder id.
190   std::string FindOrCreateOemFolder();
191
192   // Gets the location for the OEM folder. Called when the folder is first
193   // created.
194   syncer::StringOrdinal GetOemFolderPos();
195
196   // Returns true if an extension matching |id| exists and was installed by
197   // an OEM (extension->was_installed_by_oem() is true).
198   bool AppIsOem(const std::string& id);
199
200   Profile* profile_;
201   extensions::ExtensionSystem* extension_system_;
202   content::NotificationRegistrar registrar_;
203   scoped_ptr<AppListModel> model_;
204   scoped_ptr<ModelObserver> model_observer_;
205   scoped_ptr<ModelPrefUpdater> model_pref_updater_;
206   scoped_ptr<ExtensionAppModelBuilder> apps_builder_;
207   scoped_ptr<syncer::SyncChangeProcessor> sync_processor_;
208   scoped_ptr<syncer::SyncErrorFactory> sync_error_handler_;
209   SyncItemMap sync_items_;
210   syncer::SyncableService::StartSyncFlare flare_;
211   bool initial_sync_data_processed_;
212   bool first_app_list_sync_;
213   std::string oem_folder_name_;
214
215   // Provides integration with Drive apps.
216   scoped_ptr<DriveAppProvider> drive_app_provider_;
217
218   DISALLOW_COPY_AND_ASSIGN(AppListSyncableService);
219 };
220
221 }  // namespace app_list
222
223 #endif  // CHROME_BROWSER_UI_APP_LIST_APP_LIST_SYNCABLE_SERVICE_H_