1 // Copyright (c) 2012 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 #include "chrome/browser/extensions/app_sync_bundle.h"
7 #include "base/location.h"
8 #include "chrome/browser/extensions/extension_sync_service.h"
9 #include "chrome/browser/extensions/extension_util.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/common/extensions/sync_helper.h"
12 #include "extensions/browser/app_sorting.h"
13 #include "extensions/common/extension.h"
14 #include "extensions/common/extension_set.h"
15 #include "sync/api/sync_change_processor.h"
16 #include "sync/api/sync_data.h"
17 #include "sync/api/sync_error_factory.h"
19 namespace extensions
{
21 AppSyncBundle::AppSyncBundle(ExtensionSyncService
* extension_sync_service
)
22 : extension_sync_service_(extension_sync_service
) {}
24 AppSyncBundle::~AppSyncBundle() {}
26 void AppSyncBundle::SetupSync(
27 syncer::SyncChangeProcessor
* sync_change_processor
,
28 syncer::SyncErrorFactory
* sync_error_factory
,
29 const syncer::SyncDataList
& initial_sync_data
) {
30 sync_processor_
.reset(sync_change_processor
);
31 sync_error_factory_
.reset(sync_error_factory
);
33 for (syncer::SyncDataList::const_iterator i
= initial_sync_data
.begin();
34 i
!= initial_sync_data
.end();
36 scoped_ptr
<AppSyncData
> app_sync_data(AppSyncData::CreateFromSyncData(*i
));
37 if (app_sync_data
.get()) {
38 AddApp(app_sync_data
->id());
39 extension_sync_service_
->ProcessAppSyncData(*app_sync_data
);
44 void AppSyncBundle::Reset() {
45 sync_processor_
.reset();
46 sync_error_factory_
.reset();
48 pending_sync_data_
.clear();
51 syncer::SyncChange
AppSyncBundle::CreateSyncChangeToDelete(
52 const Extension
* extension
)
54 AppSyncData sync_data
= extension_sync_service_
->GetAppSyncData(*extension
);
55 return sync_data
.GetSyncChange(syncer::SyncChange::ACTION_DELETE
);
58 void AppSyncBundle::ProcessDeletion(const std::string
& extension_id
,
59 const syncer::SyncChange
& sync_change
) {
60 RemoveApp(extension_id
);
61 sync_processor_
->ProcessSyncChanges(FROM_HERE
,
62 syncer::SyncChangeList(1, sync_change
));
65 syncer::SyncChange
AppSyncBundle::CreateSyncChange(
66 const syncer::SyncData
& sync_data
) {
67 const syncer::SyncDataLocal
sync_data_local(sync_data
);
68 if (HasExtensionId(sync_data_local
.GetTag())) {
69 return syncer::SyncChange(FROM_HERE
,
70 syncer::SyncChange::ACTION_UPDATE
,
73 AddApp(sync_data_local
.GetTag());
74 return syncer::SyncChange(FROM_HERE
,
75 syncer::SyncChange::ACTION_ADD
,
80 syncer::SyncDataList
AppSyncBundle::GetAllSyncData() const {
81 std::vector
<AppSyncData
> app_sync_data
=
82 extension_sync_service_
->GetAppSyncDataList();
83 syncer::SyncDataList
result(app_sync_data
.size());
84 for (int i
= 0; i
< static_cast<int>(app_sync_data
.size()); ++i
) {
85 result
[i
] = app_sync_data
[i
].GetSyncData();
90 void AppSyncBundle::ProcessSyncChange(AppSyncData app_sync_data
) {
91 if (app_sync_data
.uninstalled())
92 RemoveApp(app_sync_data
.id());
94 AddApp(app_sync_data
.id());
95 extension_sync_service_
->ProcessAppSyncData(app_sync_data
);
98 void AppSyncBundle::ProcessSyncChangeList(
99 syncer::SyncChangeList sync_change_list
) {
100 sync_processor_
->ProcessSyncChanges(FROM_HERE
, sync_change_list
);
101 extension_sync_service_
->extension_prefs().app_sorting()->
102 FixNTPOrdinalCollisions();
105 bool AppSyncBundle::HasExtensionId(const std::string
& id
) const {
106 return synced_apps_
.find(id
) != synced_apps_
.end();
109 bool AppSyncBundle::HasPendingExtensionId(const std::string
& id
) const {
110 return pending_sync_data_
.find(id
) != pending_sync_data_
.end();
113 void AppSyncBundle::AddPendingApp(const std::string
& id
,
114 const AppSyncData
& app_sync_data
) {
115 pending_sync_data_
[id
] = app_sync_data
;
118 bool AppSyncBundle::IsSyncing() const {
119 return sync_processor_
!= NULL
;
122 void AppSyncBundle::SyncChangeIfNeeded(const Extension
& extension
) {
123 AppSyncData app_sync_data
= extension_sync_service_
->GetAppSyncData(
126 syncer::SyncChangeList
sync_change_list(1, app_sync_data
.GetSyncChange(
127 HasExtensionId(extension
.id()) ?
128 syncer::SyncChange::ACTION_UPDATE
: syncer::SyncChange::ACTION_ADD
));
129 sync_processor_
->ProcessSyncChanges(FROM_HERE
, sync_change_list
);
130 MarkPendingAppSynced(extension
.id());
133 std::vector
<AppSyncData
> AppSyncBundle::GetPendingData() const {
134 std::vector
<AppSyncData
> pending_apps
;
135 for (std::map
<std::string
, AppSyncData
>::const_iterator
136 i
= pending_sync_data_
.begin();
137 i
!= pending_sync_data_
.end();
139 pending_apps
.push_back(i
->second
);
145 void AppSyncBundle::GetAppSyncDataListHelper(
146 const ExtensionSet
& extensions
,
147 std::vector
<AppSyncData
>* sync_data_list
) const {
148 Profile
* profile
= extension_sync_service_
->profile();
150 for (ExtensionSet::const_iterator it
= extensions
.begin();
151 it
!= extensions
.end(); ++it
) {
152 const Extension
& extension
= *it
->get();
153 // If we have pending app data for this app, then this
154 // version is out of date. We'll sync back the version we got from
156 if (IsSyncing() && util::ShouldSyncApp(&extension
, profile
) &&
157 !HasPendingExtensionId(extension
.id())) {
158 sync_data_list
->push_back(extension_sync_service_
->GetAppSyncData(
164 void AppSyncBundle::AddApp(const std::string
& id
) {
165 synced_apps_
.insert(id
);
168 void AppSyncBundle::RemoveApp(const std::string
& id
) {
169 synced_apps_
.erase(id
);
173 void AppSyncBundle::MarkPendingAppSynced(const std::string
& id
) {
174 pending_sync_data_
.erase(id
);
175 synced_apps_
.insert(id
);
179 } // namespace extensions