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/browser_context_keyed_service/browser_context_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 ExtensionAppModelBuilder
;
25 namespace extensions
{
26 class ExtensionSystem
;
30 class AppListSpecifics
;
38 // Keyed Service that owns, stores, and syncs an AppListModel for a profile.
39 class AppListSyncableService
: public syncer::SyncableService
,
40 public BrowserContextKeyedService
,
41 public content::NotificationObserver
{
44 SyncItem(const std::string
& id
,
45 sync_pb::AppListSpecifics::AppListItemType type
);
47 const std::string item_id
;
48 sync_pb::AppListSpecifics::AppListItemType item_type
;
49 std::string item_name
;
50 std::string parent_id
;
51 syncer::StringOrdinal page_ordinal
;
52 syncer::StringOrdinal item_ordinal
;
54 std::string
ToString() const;
57 // Populates the model when |extension_system| is ready.
58 AppListSyncableService(Profile
* profile
,
59 extensions::ExtensionSystem
* extension_system
);
61 virtual ~AppListSyncableService();
63 // Adds |item| to |sync_items_| and |model_|. Does nothing if a sync item
65 void AddItem(AppListItem
* item
);
67 // Updates existing entry in |sync_items_| from |item|.
68 void UpdateItem(AppListItem
* item
);
70 // Removes sync item matching |id|.
71 void RemoveItem(const std::string
& id
);
73 // Returns the existing sync item matching |id| or NULL.
74 const SyncItem
* GetSyncItem(const std::string
& id
) const;
76 Profile
* profile() { return profile_
; }
77 AppListModel
* model() { return model_
.get(); }
78 size_t GetNumSyncItemsForTest() { return sync_items_
.size(); }
80 // syncer::SyncableService
81 virtual syncer::SyncMergeResult
MergeDataAndStartSyncing(
82 syncer::ModelType type
,
83 const syncer::SyncDataList
& initial_sync_data
,
84 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor
,
85 scoped_ptr
<syncer::SyncErrorFactory
> error_handler
) OVERRIDE
;
86 virtual void StopSyncing(syncer::ModelType type
) OVERRIDE
;
87 virtual syncer::SyncDataList
GetAllSyncData(
88 syncer::ModelType type
) const OVERRIDE
;
89 virtual syncer::SyncError
ProcessSyncChanges(
90 const tracked_objects::Location
& from_here
,
91 const syncer::SyncChangeList
& change_list
) OVERRIDE
;
94 typedef std::map
<std::string
, SyncItem
*> SyncItemMap
;
96 // content::NotificationObserver
97 virtual void Observe(int type
,
98 const content::NotificationSource
& source
,
99 const content::NotificationDetails
& details
) OVERRIDE
;
101 // Builds the model once ExtensionService is ready.
104 // Returns true if sync has restarted, otherwise runs |flare_|.
107 // Creates or updates a SyncItem from |specifics|. Returns true if a new item
109 bool ProcessSyncItem(const sync_pb::AppListSpecifics
& specifics
);
111 // Handles a newly created sync item (e.g. creates a new Appitem and adds it
112 // to the model or uninstalls a deleted default item.
113 void ProcessNewSyncItem(SyncItem
* sync_item
);
115 // Handles updating an existing sync item (e.g. updates item positions).
116 void ProcessExistingSyncItem(SyncItem
* sync_item
);
118 // Sends ADD or CHANGED for sync item.
119 void SendSyncChange(SyncItem
* sync_item
,
120 syncer::SyncChange::SyncChangeType sync_change_type
);
122 // Returns an existing SyncItem corresponding to |item_id| or NULL.
123 SyncItem
* FindSyncItem(const std::string
& item_id
);
125 // Creates a new sync item for |item_id|.
126 SyncItem
* CreateSyncItem(
127 const std::string
& item_id
,
128 sync_pb::AppListSpecifics::AppListItemType item_type
);
130 // Deletes a SyncItem matching |specifics|.
131 void DeleteSyncItem(const sync_pb::AppListSpecifics
& specifics
);
134 extensions::ExtensionSystem
* extension_system_
;
135 content::NotificationRegistrar registrar_
;
136 scoped_ptr
<AppListModel
> model_
;
137 scoped_ptr
<ExtensionAppModelBuilder
> apps_builder_
;
138 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor_
;
139 scoped_ptr
<syncer::SyncErrorFactory
> sync_error_handler_
;
140 SyncItemMap sync_items_
;
141 syncer::SyncableService::StartSyncFlare flare_
;
143 DISALLOW_COPY_AND_ASSIGN(AppListSyncableService
);
146 } // namespace app_list
148 #endif // CHROME_BROWSER_UI_APP_LIST_APP_LIST_SYNCABLE_SERVICE_H_