Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / app_list / app_list_syncable_service.h
blob340761605e6ca607f950444d09db2e632845a0e3
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.
5 #ifndef CHROME_BROWSER_UI_APP_LIST_APP_LIST_SYNCABLE_SERVICE_H_
6 #define CHROME_BROWSER_UI_APP_LIST_APP_LIST_SYNCABLE_SERVICE_H_
8 #include <map>
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 "sync/api/string_ordinal.h"
15 #include "sync/api/sync_change.h"
16 #include "sync/api/sync_change_processor.h"
17 #include "sync/api/sync_error_factory.h"
18 #include "sync/api/syncable_service.h"
19 #include "sync/protocol/app_list_specifics.pb.h"
21 class DriveAppProvider;
22 class ExtensionAppModelBuilder;
23 class Profile;
25 namespace extensions {
26 class ExtensionSystem;
29 namespace sync_pb {
30 class AppListSpecifics;
33 namespace app_list {
35 class AppListFolderItem;
36 class AppListItem;
37 class AppListModel;
38 class ModelPrefUpdater;
40 // Keyed Service that owns, stores, and syncs an AppListModel for a profile.
41 class AppListSyncableService : public syncer::SyncableService,
42 public KeyedService,
43 public DriveAppUninstallSyncService {
44 public:
45 struct SyncItem {
46 SyncItem(const std::string& id,
47 sync_pb::AppListSpecifics::AppListItemType type);
48 ~SyncItem();
49 const std::string item_id;
50 sync_pb::AppListSpecifics::AppListItemType item_type;
51 std::string item_name;
52 std::string parent_id;
53 syncer::StringOrdinal item_ordinal;
55 std::string ToString() const;
58 // Populates the model when |extension_system| is ready.
59 AppListSyncableService(Profile* profile,
60 extensions::ExtensionSystem* extension_system);
62 ~AppListSyncableService() override;
64 // Adds |item| to |sync_items_| and |model_|. If a sync item already exists,
65 // updates the existing sync item instead.
66 void AddItem(scoped_ptr<AppListItem> app_item);
68 // Removes sync item matching |id|.
69 void RemoveItem(const std::string& id);
71 // Removes sync item matching |id| after item uninstall.
72 void RemoveUninstalledItem(const std::string& id);
74 // Called when properties of an item may have changed, e.g. default/oem state.
75 void UpdateItem(AppListItem* app_item);
77 // Returns the existing sync item matching |id| or NULL.
78 const SyncItem* GetSyncItem(const std::string& id) const;
80 // Sets the name of the folder for OEM apps.
81 void SetOemFolderName(const std::string& name);
83 // Gets the app list model, building it if it doesn't yet exist.
84 AppListModel* GetModel();
86 Profile* profile() { return profile_; }
87 size_t GetNumSyncItemsForTest();
88 const std::string& GetOemFolderNameForTest() const {
89 return oem_folder_name_;
91 void ResetDriveAppProviderForTest();
93 // syncer::SyncableService
94 syncer::SyncMergeResult MergeDataAndStartSyncing(
95 syncer::ModelType type,
96 const syncer::SyncDataList& initial_sync_data,
97 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
98 scoped_ptr<syncer::SyncErrorFactory> error_handler) override;
99 void StopSyncing(syncer::ModelType type) override;
100 syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override;
101 syncer::SyncError ProcessSyncChanges(
102 const tracked_objects::Location& from_here,
103 const syncer::SyncChangeList& change_list) override;
105 private:
106 class ModelObserver;
107 typedef std::map<std::string, SyncItem*> SyncItemMap;
109 // KeyedService
110 void Shutdown() override;
112 // DriveAppUninstallSyncService
113 void TrackUninstalledDriveApp(const std::string& drive_app_id) override;
114 void UntrackUninstalledDriveApp(const std::string& drive_app_id) override;
116 // Builds the model once ExtensionService is ready.
117 void BuildModel();
119 // Returns true if sync has restarted, otherwise runs |flare_|.
120 bool SyncStarted();
122 // If |app_item| matches an existing sync item, returns it. Otherwise adds
123 // |app_item| to |sync_items_| and returns the new item. If |app_item| is
124 // invalid returns NULL.
125 SyncItem* FindOrAddSyncItem(AppListItem* app_item);
127 // Creates a sync item for |app_item| and sends an ADD SyncChange event.
128 SyncItem* CreateSyncItemFromAppItem(AppListItem* app_item);
130 // If a sync item for |app_item| already exists, update |app_item| from the
131 // sync item, otherwise create a new sync item from |app_item|.
132 void AddOrUpdateFromSyncItem(AppListItem* app_item);
134 // Either uninstalling a default app or remove the REMOVE_DEFAULT sync item.
135 // Returns true if the app is removed. Otherwise deletes the existing sync
136 // item and returns false.
137 bool RemoveDefaultApp(AppListItem* item, SyncItem* sync_item);
139 // Deletes a sync item from |sync_items_| and sends a DELETE action.
140 void DeleteSyncItem(SyncItem* sync_item);
142 // Updates existing entry in |sync_items_| from |app_item|.
143 void UpdateSyncItem(AppListItem* app_item);
145 // Removes sync item matching |id|.
146 void RemoveSyncItem(const std::string& id);
148 // Updates folder items that may get created during initial sync.
149 void ResolveFolderPositions();
151 // Removes any empty SyncItem folders and deletes them from sync. Called
152 // after a sync item is removed (which may result in an empty folder).
153 void PruneEmptySyncFolders();
155 // Creates or updates a SyncItem from |specifics|. Returns true if a new item
156 // was created.
157 bool ProcessSyncItemSpecifics(const sync_pb::AppListSpecifics& specifics);
159 // Handles a newly created sync item (e.g. creates a new AppItem and adds it
160 // to the model or uninstalls a deleted default item.
161 void ProcessNewSyncItem(SyncItem* sync_item);
163 // Handles an existing sync item.
164 void ProcessExistingSyncItem(SyncItem* sync_item);
166 // Updates |app_item| from |sync_item| (e.g. updates item positions).
167 void UpdateAppItemFromSyncItem(const SyncItem* sync_item,
168 AppListItem* app_item);
170 // Sends ADD or CHANGED for sync item.
171 void SendSyncChange(SyncItem* sync_item,
172 syncer::SyncChange::SyncChangeType sync_change_type);
174 // Returns an existing SyncItem corresponding to |item_id| or NULL.
175 SyncItem* FindSyncItem(const std::string& item_id);
177 // Creates a new sync item for |item_id|.
178 SyncItem* CreateSyncItem(
179 const std::string& item_id,
180 sync_pb::AppListSpecifics::AppListItemType item_type);
182 // Deletes a SyncItem matching |specifics|.
183 void DeleteSyncItemSpecifics(const sync_pb::AppListSpecifics& specifics);
185 // Creates the OEM folder and sets its name if necessary. Returns the OEM
186 // folder id.
187 std::string FindOrCreateOemFolder();
189 // Gets the location for the OEM folder. Called when the folder is first
190 // created.
191 syncer::StringOrdinal GetOemFolderPos();
193 // Returns true if an extension matching |id| exists and was installed by
194 // an OEM (extension->was_installed_by_oem() is true).
195 bool AppIsOem(const std::string& id);
197 Profile* profile_;
198 extensions::ExtensionSystem* extension_system_;
199 scoped_ptr<AppListModel> model_;
200 scoped_ptr<ModelObserver> model_observer_;
201 scoped_ptr<ModelPrefUpdater> model_pref_updater_;
202 scoped_ptr<ExtensionAppModelBuilder> apps_builder_;
203 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_;
204 scoped_ptr<syncer::SyncErrorFactory> sync_error_handler_;
205 SyncItemMap sync_items_;
206 syncer::SyncableService::StartSyncFlare flare_;
207 bool initial_sync_data_processed_;
208 bool first_app_list_sync_;
209 std::string oem_folder_name_;
211 // Provides integration with Drive apps.
212 scoped_ptr<DriveAppProvider> drive_app_provider_;
214 DISALLOW_COPY_AND_ASSIGN(AppListSyncableService);
217 } // namespace app_list
219 #endif // CHROME_BROWSER_UI_APP_LIST_APP_LIST_SYNCABLE_SERVICE_H_