1 // Copyright 2015 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/sync_bundle.h"
7 #include "base/location.h"
8 #include "chrome/browser/extensions/extension_sync_data.h"
9 #include "chrome/browser/extensions/extension_sync_service.h"
10 #include "chrome/browser/extensions/extension_util.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "extensions/common/extension.h"
14 namespace extensions
{
16 SyncBundle::SyncBundle() {}
17 SyncBundle::~SyncBundle() {}
19 void SyncBundle::StartSyncing(
20 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor
) {
21 sync_processor_
.reset(sync_processor
.release());
24 void SyncBundle::Reset() {
25 sync_processor_
.reset();
26 synced_extensions_
.clear();
27 pending_sync_data_
.clear();
30 bool SyncBundle::IsSyncing() const {
31 return sync_processor_
!= nullptr;
34 void SyncBundle::PushSyncDataList(
35 const syncer::SyncDataList
& sync_data_list
) {
36 syncer::SyncChangeList sync_change_list
;
37 for (const syncer::SyncData
& sync_data
: sync_data_list
) {
38 const syncer::SyncDataLocal
sync_data_local(sync_data
);
39 const std::string
& extension_id
= sync_data_local
.GetTag();
41 sync_change_list
.push_back(CreateSyncChange(extension_id
, sync_data
));
43 AddSyncedExtension(extension_id
);
46 PushSyncChanges(sync_change_list
);
49 void SyncBundle::PushSyncDeletion(const std::string
& extension_id
,
50 const syncer::SyncData
& sync_data
) {
51 if (!HasSyncedExtension(extension_id
))
54 RemoveSyncedExtension(extension_id
);
55 PushSyncChanges(syncer::SyncChangeList(1,
56 syncer::SyncChange(FROM_HERE
,
57 syncer::SyncChange::ACTION_DELETE
,
61 void SyncBundle::PushSyncAddOrUpdate(const std::string
& extension_id
,
62 const syncer::SyncData
& sync_data
) {
63 PushSyncChanges(syncer::SyncChangeList(
64 1, CreateSyncChange(extension_id
, sync_data
)));
65 AddSyncedExtension(extension_id
);
66 // Now sync and local state agree. If we had any pending change from sync,
68 pending_sync_data_
.erase(extension_id
);
71 void SyncBundle::ApplySyncData(const ExtensionSyncData
& extension_sync_data
) {
72 if (extension_sync_data
.uninstalled())
73 RemoveSyncedExtension(extension_sync_data
.id());
75 AddSyncedExtension(extension_sync_data
.id());
78 bool SyncBundle::HasPendingExtensionId(const std::string
& id
) const {
79 return pending_sync_data_
.find(id
) != pending_sync_data_
.end();
82 void SyncBundle::AddPendingExtension(
83 const std::string
& id
,
84 const ExtensionSyncData
& extension_sync_data
) {
85 pending_sync_data_
.insert(std::make_pair(id
, extension_sync_data
));
88 std::vector
<ExtensionSyncData
> SyncBundle::GetPendingData() const {
89 std::vector
<ExtensionSyncData
> pending_extensions
;
90 for (const auto& data
: pending_sync_data_
)
91 pending_extensions
.push_back(data
.second
);
93 return pending_extensions
;
96 syncer::SyncChange
SyncBundle::CreateSyncChange(
97 const std::string
& extension_id
,
98 const syncer::SyncData
& sync_data
) const {
99 return syncer::SyncChange(
101 HasSyncedExtension(extension_id
) ? syncer::SyncChange::ACTION_UPDATE
102 : syncer::SyncChange::ACTION_ADD
,
106 void SyncBundle::PushSyncChanges(
107 const syncer::SyncChangeList
& sync_change_list
) {
108 sync_processor_
->ProcessSyncChanges(FROM_HERE
, sync_change_list
);
111 void SyncBundle::AddSyncedExtension(const std::string
& id
) {
112 synced_extensions_
.insert(id
);
115 void SyncBundle::RemoveSyncedExtension(const std::string
& id
) {
116 synced_extensions_
.erase(id
);
119 bool SyncBundle::HasSyncedExtension(const std::string
& id
) const {
120 return synced_extensions_
.find(id
) != synced_extensions_
.end();
123 } // namespace extensions