Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / ui / app_list / app_list_syncable_service.h
blob49ded4cf44773e5dc3f845ba5d54a868e94d14ac
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 virtual ~AppListSyncableService();
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 virtual 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 virtual void StopSyncing(syncer::ModelType type) override;
98 virtual syncer::SyncDataList GetAllSyncData(
99 syncer::ModelType type) const override;
100 virtual syncer::SyncError ProcessSyncChanges(
101 const tracked_objects::Location& from_here,
102 const syncer::SyncChangeList& change_list) override;
104 private:
105 class ModelObserver;
106 typedef std::map<std::string, SyncItem*> SyncItemMap;
108 // KeyedService
109 virtual void Shutdown() override;
111 // DriveAppUninstallSyncService
112 virtual void TrackUninstalledDriveApp(
113 const std::string& drive_app_id) override;
114 virtual void UntrackUninstalledDriveApp(
115 const std::string& drive_app_id) override;
117 // content::NotificationObserver
118 virtual void Observe(int type,
119 const content::NotificationSource& source,
120 const content::NotificationDetails& details) override;
122 // Builds the model once ExtensionService is ready.
123 void BuildModel();
125 // Returns true if sync has restarted, otherwise runs |flare_|.
126 bool SyncStarted();
128 // If |app_item| matches an existing sync item, returns it. Otherwise adds
129 // |app_item| to |sync_items_| and returns the new item. If |app_item| is
130 // invalid returns NULL.
131 SyncItem* FindOrAddSyncItem(AppListItem* app_item);
133 // Creates a sync item for |app_item| and sends an ADD SyncChange event.
134 SyncItem* CreateSyncItemFromAppItem(AppListItem* app_item);
136 // If a sync item for |app_item| already exists, update |app_item| from the
137 // sync item, otherwise create a new sync item from |app_item|.
138 void AddOrUpdateFromSyncItem(AppListItem* app_item);
140 // Either uninstalling a default app or remove the REMOVE_DEFAULT sync item.
141 // Returns true if the app is removed. Otherwise deletes the existing sync
142 // item and returns false.
143 bool RemoveDefaultApp(AppListItem* item, SyncItem* sync_item);
145 // Deletes a sync item from |sync_items_| and sends a DELETE action.
146 void DeleteSyncItem(SyncItem* sync_item);
148 // Updates existing entry in |sync_items_| from |app_item|.
149 void UpdateSyncItem(AppListItem* app_item);
151 // Removes sync item matching |id|.
152 void RemoveSyncItem(const std::string& id);
154 // Updates folder items that may get created during initial sync.
155 void ResolveFolderPositions();
157 // Removes any empty SyncItem folders and deletes them from sync. Called
158 // after a sync item is removed (which may result in an empty folder).
159 void PruneEmptySyncFolders();
161 // Creates or updates a SyncItem from |specifics|. Returns true if a new item
162 // was created.
163 bool ProcessSyncItemSpecifics(const sync_pb::AppListSpecifics& specifics);
165 // Handles a newly created sync item (e.g. creates a new AppItem and adds it
166 // to the model or uninstalls a deleted default item.
167 void ProcessNewSyncItem(SyncItem* sync_item);
169 // Handles an existing sync item.
170 void ProcessExistingSyncItem(SyncItem* sync_item);
172 // Updates |app_item| from |sync_item| (e.g. updates item positions).
173 void UpdateAppItemFromSyncItem(const SyncItem* sync_item,
174 AppListItem* app_item);
176 // Sends ADD or CHANGED for sync item.
177 void SendSyncChange(SyncItem* sync_item,
178 syncer::SyncChange::SyncChangeType sync_change_type);
180 // Returns an existing SyncItem corresponding to |item_id| or NULL.
181 SyncItem* FindSyncItem(const std::string& item_id);
183 // Creates a new sync item for |item_id|.
184 SyncItem* CreateSyncItem(
185 const std::string& item_id,
186 sync_pb::AppListSpecifics::AppListItemType item_type);
188 // Deletes a SyncItem matching |specifics|.
189 void DeleteSyncItemSpecifics(const sync_pb::AppListSpecifics& specifics);
191 // Creates the OEM folder and sets its name if necessary. Returns the OEM
192 // folder id.
193 std::string FindOrCreateOemFolder();
195 // Gets the location for the OEM folder. Called when the folder is first
196 // created.
197 syncer::StringOrdinal GetOemFolderPos();
199 // Returns true if an extension matching |id| exists and was installed by
200 // an OEM (extension->was_installed_by_oem() is true).
201 bool AppIsOem(const std::string& id);
203 Profile* profile_;
204 extensions::ExtensionSystem* extension_system_;
205 content::NotificationRegistrar registrar_;
206 scoped_ptr<AppListModel> model_;
207 scoped_ptr<ModelObserver> model_observer_;
208 scoped_ptr<ModelPrefUpdater> model_pref_updater_;
209 scoped_ptr<ExtensionAppModelBuilder> apps_builder_;
210 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_;
211 scoped_ptr<syncer::SyncErrorFactory> sync_error_handler_;
212 SyncItemMap sync_items_;
213 syncer::SyncableService::StartSyncFlare flare_;
214 bool initial_sync_data_processed_;
215 bool first_app_list_sync_;
216 std::string oem_folder_name_;
218 // Provides integration with Drive apps.
219 scoped_ptr<DriveAppProvider> drive_app_provider_;
221 DISALLOW_COPY_AND_ASSIGN(AppListSyncableService);
224 } // namespace app_list
226 #endif // CHROME_BROWSER_UI_APP_LIST_APP_LIST_SYNCABLE_SERVICE_H_