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_
10 #include "base/memory/scoped_ptr.h"
11 #include "chrome/browser/sync/glue/sync_start_util.h"
12 #include "components/keyed_service/core/keyed_service.h"
13 #include "content/public/browser/notification_observer.h"
14 #include "content/public/browser/notification_registrar.h"
15 #include "sync/api/string_ordinal.h"
16 #include "sync/api/sync_change.h"
17 #include "sync/api/sync_change_processor.h"
18 #include "sync/api/sync_error_factory.h"
19 #include "sync/api/syncable_service.h"
20 #include "sync/protocol/app_list_specifics.pb.h"
22 class DriveAppProvider;
23 class ExtensionAppModelBuilder;
26 namespace extensions {
27 class ExtensionSystem;
31 class AppListSpecifics;
36 class AppListFolderItem;
40 // Keyed Service that owns, stores, and syncs an AppListModel for a profile.
41 class AppListSyncableService : public syncer::SyncableService,
43 public content::NotificationObserver {
46 SyncItem(const std::string& id,
47 sync_pb::AppListSpecifics::AppListItemType type);
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 page_ordinal;
54 syncer::StringOrdinal item_ordinal;
56 std::string ToString() const;
59 // Populates the model when |extension_system| is ready.
60 AppListSyncableService(Profile* profile,
61 extensions::ExtensionSystem* extension_system);
63 virtual ~AppListSyncableService();
65 // Adds |item| to |sync_items_| and |model_|. If a sync item already exists,
66 // updates the existing sync item instead.
67 void AddItem(scoped_ptr<AppListItem> app_item);
69 // Removes sync item matching |id|.
70 void RemoveItem(const std::string& id);
72 // Called when properties of an item may have changed, e.g. default/oem state.
73 void UpdateItem(AppListItem* app_item);
75 // Returns the existing sync item matching |id| or NULL.
76 const SyncItem* GetSyncItem(const std::string& id) const;
78 // Sets the name of the folder for OEM apps.
79 void SetOemFolderName(const std::string& name);
81 Profile* profile() { return profile_; }
82 AppListModel* model() { return model_.get(); }
83 size_t GetNumSyncItemsForTest() const { return sync_items_.size(); }
84 const std::string& GetOemFolderNameForTest() const {
85 return oem_folder_name_;
88 // syncer::SyncableService
89 virtual syncer::SyncMergeResult MergeDataAndStartSyncing(
90 syncer::ModelType type,
91 const syncer::SyncDataList& initial_sync_data,
92 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
93 scoped_ptr<syncer::SyncErrorFactory> error_handler) OVERRIDE;
94 virtual void StopSyncing(syncer::ModelType type) OVERRIDE;
95 virtual syncer::SyncDataList GetAllSyncData(
96 syncer::ModelType type) const OVERRIDE;
97 virtual syncer::SyncError ProcessSyncChanges(
98 const tracked_objects::Location& from_here,
99 const syncer::SyncChangeList& change_list) OVERRIDE;
103 typedef std::map<std::string, SyncItem*> SyncItemMap;
106 virtual void Shutdown() OVERRIDE;
108 // content::NotificationObserver
109 virtual void Observe(int type,
110 const content::NotificationSource& source,
111 const content::NotificationDetails& details) OVERRIDE;
113 // Builds the model once ExtensionService is ready.
116 // Returns true if sync has restarted, otherwise runs |flare_|.
119 // If |app_item| matches an existing sync item, returns it. Otherwise adds
120 // |app_item| to |sync_items_| and returns the new item. If |app_item| is
121 // invalid returns NULL.
122 SyncItem* FindOrAddSyncItem(AppListItem* app_item);
124 // Creates a sync item for |app_item| and sends an ADD SyncChange event.
125 SyncItem* CreateSyncItemFromAppItem(AppListItem* app_item);
127 // If a sync item for |app_item| already exists, update |app_item| from the
128 // sync item, otherwise create a new sync item from |app_item|.
129 void AddOrUpdateFromSyncItem(AppListItem* app_item);
131 // Either uninstalling a default app or remove the REMOVE_DEFAULT sync item.
132 // Returns true if the app is removed. Otherwise deletes the existing sync
133 // item and returns false.
134 bool RemoveDefaultApp(AppListItem* item, SyncItem* sync_item);
136 // Deletes a sync item from |sync_items_| and sends a DELETE action.
137 void DeleteSyncItem(SyncItem* sync_item);
139 // Updates existing entry in |sync_items_| from |app_item|.
140 void UpdateSyncItem(AppListItem* app_item);
142 // Removes sync item matching |id|.
143 void RemoveSyncItem(const std::string& id);
145 // Updates folder items that may get created during initial sync.
146 void ResolveFolderPositions();
148 // Removes any empty SyncItem folders and deletes them from sync. Called
149 // after a sync item is removed (which may result in an empty folder).
150 void PruneEmptySyncFolders();
152 // Creates or updates a SyncItem from |specifics|. Returns true if a new item
154 bool ProcessSyncItemSpecifics(const sync_pb::AppListSpecifics& specifics);
156 // Handles a newly created sync item (e.g. creates a new AppItem and adds it
157 // to the model or uninstalls a deleted default item.
158 void ProcessNewSyncItem(SyncItem* sync_item);
160 // Handles an existing sync item.
161 void ProcessExistingSyncItem(SyncItem* sync_item);
163 // Updates |app_item| from |sync_item| (e.g. updates item positions).
164 void UpdateAppItemFromSyncItem(const SyncItem* sync_item,
165 AppListItem* app_item);
167 // Sends ADD or CHANGED for sync item.
168 void SendSyncChange(SyncItem* sync_item,
169 syncer::SyncChange::SyncChangeType sync_change_type);
171 // Returns an existing SyncItem corresponding to |item_id| or NULL.
172 SyncItem* FindSyncItem(const std::string& item_id);
174 // Creates a new sync item for |item_id|.
175 SyncItem* CreateSyncItem(
176 const std::string& item_id,
177 sync_pb::AppListSpecifics::AppListItemType item_type);
179 // Deletes a SyncItem matching |specifics|.
180 void DeleteSyncItemSpecifics(const sync_pb::AppListSpecifics& specifics);
182 // Creates the OEM folder and sets its name if necessary. Returns the OEM
184 std::string FindOrCreateOemFolder();
186 // Gets the location for the OEM folder. Called when the folder is first
188 syncer::StringOrdinal GetOemFolderPos();
190 // Returns true if an extension matching |id| exists and was installed by
191 // an OEM (extension->was_installed_by_oem() is true).
192 bool AppIsOem(const std::string& id);
195 extensions::ExtensionSystem* extension_system_;
196 content::NotificationRegistrar registrar_;
197 scoped_ptr<AppListModel> model_;
198 scoped_ptr<ModelObserver> model_observer_;
199 scoped_ptr<ExtensionAppModelBuilder> apps_builder_;
200 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_;
201 scoped_ptr<syncer::SyncErrorFactory> sync_error_handler_;
202 SyncItemMap sync_items_;
203 syncer::SyncableService::StartSyncFlare flare_;
204 bool first_app_list_sync_;
205 std::string oem_folder_name_;
207 // Provides integration with Drive apps.
208 scoped_ptr<DriveAppProvider> drive_app_provider_;
210 DISALLOW_COPY_AND_ASSIGN(AppListSyncableService);
213 } // namespace app_list
215 #endif // CHROME_BROWSER_UI_APP_LIST_APP_LIST_SYNCABLE_SERVICE_H_