app_list_syncable_service.h revision 23730a6e56a168d1879203e4b3819bb36e3d8f1f
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/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"
21
22class ExtensionAppModelBuilder;
23class Profile;
24
25namespace extensions {
26class ExtensionSystem;
27}
28
29namespace sync_pb {
30class AppListSpecifics;
31}
32
33namespace app_list {
34
35class AppListFolderItem;
36class AppListItem;
37class AppListModel;
38
39// Keyed Service that owns, stores, and syncs an AppListModel for a profile.
40class AppListSyncableService : public syncer::SyncableService,
41                               public KeyedService,
42                               public content::NotificationObserver {
43 public:
44  struct SyncItem {
45    SyncItem(const std::string& id,
46             sync_pb::AppListSpecifics::AppListItemType type);
47    ~SyncItem();
48    const std::string item_id;
49    sync_pb::AppListSpecifics::AppListItemType item_type;
50    std::string item_name;
51    std::string parent_id;
52    syncer::StringOrdinal page_ordinal;
53    syncer::StringOrdinal item_ordinal;
54
55    std::string ToString() const;
56  };
57
58  // Populates the model when |extension_system| is ready.
59  AppListSyncableService(Profile* profile,
60                         extensions::ExtensionSystem* extension_system);
61
62  virtual ~AppListSyncableService();
63
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);
67
68  // Removes sync item matching |id|.
69  void RemoveItem(const std::string& id);
70
71  // Called when properties of an item may have changed, e.g. default/oem state.
72  void UpdateItem(AppListItem* app_item);
73
74  // Returns the existing sync item matching |id| or NULL.
75  const SyncItem* GetSyncItem(const std::string& id) const;
76
77  // Sets the name of the folder for OEM apps.
78  void SetOemFolderName(const std::string& name);
79
80  Profile* profile() { return profile_; }
81  AppListModel* model() { return model_.get(); }
82  size_t GetNumSyncItemsForTest() { return sync_items_.size(); }
83
84  // syncer::SyncableService
85  virtual syncer::SyncMergeResult MergeDataAndStartSyncing(
86      syncer::ModelType type,
87      const syncer::SyncDataList& initial_sync_data,
88      scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
89      scoped_ptr<syncer::SyncErrorFactory> error_handler) OVERRIDE;
90  virtual void StopSyncing(syncer::ModelType type) OVERRIDE;
91  virtual syncer::SyncDataList GetAllSyncData(
92      syncer::ModelType type) const OVERRIDE;
93  virtual syncer::SyncError ProcessSyncChanges(
94      const tracked_objects::Location& from_here,
95      const syncer::SyncChangeList& change_list) OVERRIDE;
96
97 private:
98  class ModelObserver;
99  typedef std::map<std::string, SyncItem*> SyncItemMap;
100
101  // content::NotificationObserver
102  virtual void Observe(int type,
103                       const content::NotificationSource& source,
104                       const content::NotificationDetails& details) OVERRIDE;
105
106  // Builds the model once ExtensionService is ready.
107  void BuildModel();
108
109  // Returns true if sync has restarted, otherwise runs |flare_|.
110  bool SyncStarted();
111
112  // If |app_item| matches an existing sync item, returns it. Otherwise adds
113  // |app_item| to |sync_items_| and returns the new item. If |app_item| is
114  // invalid returns NULL.
115  SyncItem* FindOrAddSyncItem(AppListItem* app_item);
116
117  // Creates a sync item for |app_item| and sends an ADD SyncChange event.
118  SyncItem* CreateSyncItemFromAppItem(AppListItem* app_item);
119
120  // If a sync item for |app_item| already exists, update |app_item| from the
121  // sync item, otherwise create a new sync item from |app_item|.
122  void AddOrUpdateFromSyncItem(AppListItem* app_item);
123
124  // Either uninstalling a default app or remove the REMOVE_DEFAULT sync item.
125  // Returns true if the app is removed. Otherwise deletes the existing sync
126  // item and returns false.
127  bool RemoveDefaultApp(AppListItem* item, SyncItem* sync_item);
128
129  // Deletes a sync item from |sync_items_| and sends a DELETE action.
130  void DeleteSyncItem(SyncItem* sync_item);
131
132  // Updates existing entry in |sync_items_| from |app_item|.
133  void UpdateSyncItem(AppListItem* app_item);
134
135  // Removes sync item matching |id|.
136  void RemoveSyncItem(const std::string& id);
137
138  // Updates folder items that may get created during initial sync.
139  void ResolveFolderPositions();
140
141  // Removes any empty SyncItem folders and deletes them from sync. Called
142  // after a sync item is removed (which may result in an empty folder).
143  void PruneEmptySyncFolders();
144
145  // Creates or updates a SyncItem from |specifics|. Returns true if a new item
146  // was created.
147  bool ProcessSyncItemSpecifics(const sync_pb::AppListSpecifics& specifics);
148
149  // Handles a newly created sync item (e.g. creates a new AppItem and adds it
150  // to the model or uninstalls a deleted default item.
151  void ProcessNewSyncItem(SyncItem* sync_item);
152
153  // Handles an existing sync item.
154  void ProcessExistingSyncItem(SyncItem* sync_item);
155
156  // Updates |app_item| from |sync_item| (e.g. updates item positions).
157  void UpdateAppItemFromSyncItem(const SyncItem* sync_item,
158                                 AppListItem* app_item);
159
160  // Sends ADD or CHANGED for sync item.
161  void SendSyncChange(SyncItem* sync_item,
162                      syncer::SyncChange::SyncChangeType sync_change_type);
163
164  // Returns an existing SyncItem corresponding to |item_id| or NULL.
165  SyncItem* FindSyncItem(const std::string& item_id);
166
167  // Creates a new sync item for |item_id|.
168  SyncItem* CreateSyncItem(
169      const std::string& item_id,
170      sync_pb::AppListSpecifics::AppListItemType item_type);
171
172  // Deletes a SyncItem matching |specifics|.
173  void DeleteSyncItemSpecifics(const sync_pb::AppListSpecifics& specifics);
174
175  // Creates the OEM folder and sets its name if necessary. Returns the OEM
176  // folder id.
177  std::string FindOrCreateOemFolder();
178
179  Profile* profile_;
180  extensions::ExtensionSystem* extension_system_;
181  content::NotificationRegistrar registrar_;
182  scoped_ptr<AppListModel> model_;
183  scoped_ptr<ModelObserver> model_observer_;
184  scoped_ptr<ExtensionAppModelBuilder> apps_builder_;
185  scoped_ptr<syncer::SyncChangeProcessor> sync_processor_;
186  scoped_ptr<syncer::SyncErrorFactory> sync_error_handler_;
187  SyncItemMap sync_items_;
188  syncer::SyncableService::StartSyncFlare flare_;
189  std::string oem_folder_name_;
190
191  DISALLOW_COPY_AND_ASSIGN(AppListSyncableService);
192};
193
194}  // namespace app_list
195
196#endif  // CHROME_BROWSER_UI_APP_LIST_APP_LIST_SYNCABLE_SERVICE_H_
197