ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / ui / app_list / app_list_syncable_service.h
blob04ccdcca4a76d574388017d36ee655d4f38a335d
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 "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"
23 class DriveAppProvider;
24 class ExtensionAppModelBuilder;
25 class Profile;
27 namespace extensions {
28 class ExtensionSystem;
31 namespace sync_pb {
32 class AppListSpecifics;
35 namespace app_list {
37 class AppListFolderItem;
38 class AppListItem;
39 class AppListModel;
40 class ModelPrefUpdater;
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;
58 std::string ToString() const;
61 // Populates the model when |extension_system| is ready.
62 AppListSyncableService(Profile* profile,
63 extensions::ExtensionSystem* extension_system);
65 ~AppListSyncableService() override;
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);
71 // Removes sync item matching |id|.
72 void RemoveItem(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 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_;
89 void ResetDriveAppProviderForTest();
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;
103 private:
104 class ModelObserver;
105 typedef std::map<std::string, SyncItem*> SyncItemMap;
107 // KeyedService
108 void Shutdown() override;
110 // DriveAppUninstallSyncService
111 void TrackUninstalledDriveApp(const std::string& drive_app_id) override;
112 void UntrackUninstalledDriveApp(const std::string& drive_app_id) override;
114 // content::NotificationObserver
115 void Observe(int type,
116 const content::NotificationSource& source,
117 const content::NotificationDetails& details) override;
119 // Builds the model once ExtensionService is ready.
120 void BuildModel();
122 // Returns true if sync has restarted, otherwise runs |flare_|.
123 bool SyncStarted();
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);
130 // Creates a sync item for |app_item| and sends an ADD SyncChange event.
131 SyncItem* CreateSyncItemFromAppItem(AppListItem* app_item);
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);
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);
142 // Deletes a sync item from |sync_items_| and sends a DELETE action.
143 void DeleteSyncItem(SyncItem* sync_item);
145 // Updates existing entry in |sync_items_| from |app_item|.
146 void UpdateSyncItem(AppListItem* app_item);
148 // Removes sync item matching |id|.
149 void RemoveSyncItem(const std::string& id);
151 // Updates folder items that may get created during initial sync.
152 void ResolveFolderPositions();
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();
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);
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);
166 // Handles an existing sync item.
167 void ProcessExistingSyncItem(SyncItem* sync_item);
169 // Updates |app_item| from |sync_item| (e.g. updates item positions).
170 void UpdateAppItemFromSyncItem(const SyncItem* sync_item,
171 AppListItem* app_item);
173 // Sends ADD or CHANGED for sync item.
174 void SendSyncChange(SyncItem* sync_item,
175 syncer::SyncChange::SyncChangeType sync_change_type);
177 // Returns an existing SyncItem corresponding to |item_id| or NULL.
178 SyncItem* FindSyncItem(const std::string& item_id);
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);
185 // Deletes a SyncItem matching |specifics|.
186 void DeleteSyncItemSpecifics(const sync_pb::AppListSpecifics& specifics);
188 // Creates the OEM folder and sets its name if necessary. Returns the OEM
189 // folder id.
190 std::string FindOrCreateOemFolder();
192 // Gets the location for the OEM folder. Called when the folder is first
193 // created.
194 syncer::StringOrdinal GetOemFolderPos();
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);
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_;
215 // Provides integration with Drive apps.
216 scoped_ptr<DriveAppProvider> drive_app_provider_;
218 DISALLOW_COPY_AND_ASSIGN(AppListSyncableService);
221 } // namespace app_list
223 #endif // CHROME_BROWSER_UI_APP_LIST_APP_LIST_SYNCABLE_SERVICE_H_