Cleanup ExtensionSyncService and SyncBundle.
[chromium-blink-merge.git] / chrome / browser / extensions / sync_bundle.cc
blob4887b0a58183043a58921c6718562cc31f53c52b
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(ExtensionSyncService* sync_service)
17 : sync_service_(sync_service) {}
19 SyncBundle::~SyncBundle() {}
21 void SyncBundle::MergeDataAndStartSyncing(
22 const syncer::SyncDataList& initial_sync_data,
23 scoped_ptr<syncer::SyncChangeProcessor> sync_processor) {
24 sync_processor_.reset(sync_processor.release());
26 for (const syncer::SyncData& sync_data : initial_sync_data) {
27 scoped_ptr<ExtensionSyncData> extension_sync_data(
28 ExtensionSyncData::CreateFromSyncData(sync_data));
29 if (extension_sync_data.get()) {
30 AddSyncedExtension(extension_sync_data->id());
31 sync_service_->ApplySyncData(*extension_sync_data);
36 void SyncBundle::Reset() {
37 sync_processor_.reset();
38 synced_extensions_.clear();
39 pending_sync_data_.clear();
42 bool SyncBundle::IsSyncing() const {
43 return sync_processor_ != nullptr;
46 bool SyncBundle::HasExtensionId(const std::string& id) const {
47 return synced_extensions_.find(id) != synced_extensions_.end();
50 bool SyncBundle::ShouldIncludeInLocalSyncDataList(
51 const Extension& extension) const {
52 // If there is pending data for this extension, then this version is out of
53 // date. We'll sync back the version we got from sync.
54 return IsSyncing() && !HasPendingExtensionId(extension.id());
57 void SyncBundle::PushSyncDataList(
58 const syncer::SyncDataList& sync_data_list) {
59 syncer::SyncChangeList sync_change_list;
60 for (const syncer::SyncData& sync_data : sync_data_list) {
61 const syncer::SyncDataLocal sync_data_local(sync_data);
62 const std::string& extension_id = sync_data_local.GetTag();
64 sync_change_list.push_back(CreateSyncChange(extension_id, sync_data));
66 AddSyncedExtension(extension_id);
69 PushSyncChanges(sync_change_list);
72 void SyncBundle::PushSyncDeletion(const std::string& extension_id,
73 const syncer::SyncData& sync_data) {
74 RemoveSyncedExtension(extension_id);
75 PushSyncChanges(syncer::SyncChangeList(1,
76 syncer::SyncChange(FROM_HERE,
77 syncer::SyncChange::ACTION_DELETE,
78 sync_data)));
81 void SyncBundle::PushSyncChangeIfNeeded(const Extension& extension) {
82 syncer::SyncChangeList sync_change_list(
84 CreateSyncChange(extension.id(),
85 sync_service_->CreateSyncData(extension).GetSyncData()));
86 PushSyncChanges(sync_change_list);
87 MarkPendingExtensionSynced(extension.id());
90 void SyncBundle::ApplySyncChange(const syncer::SyncChange& sync_change) {
91 scoped_ptr<ExtensionSyncData> extension_sync_data(
92 ExtensionSyncData::CreateFromSyncChange(sync_change));
93 if (!extension_sync_data.get())
94 return; // TODO(treib,kalman): Warning message?
96 if (extension_sync_data->uninstalled())
97 RemoveSyncedExtension(extension_sync_data->id());
98 else
99 AddSyncedExtension(extension_sync_data->id());
100 sync_service_->ApplySyncData(*extension_sync_data);
103 bool SyncBundle::HasPendingExtensionId(const std::string& id) const {
104 return pending_sync_data_.find(id) != pending_sync_data_.end();
107 void SyncBundle::AddPendingExtension(
108 const std::string& id,
109 const ExtensionSyncData& extension_sync_data) {
110 pending_sync_data_.insert(std::make_pair(id, extension_sync_data));
113 std::vector<ExtensionSyncData> SyncBundle::GetPendingData() const {
114 std::vector<ExtensionSyncData> pending_extensions;
115 for (const auto& data : pending_sync_data_)
116 pending_extensions.push_back(data.second);
118 return pending_extensions;
121 syncer::SyncChange SyncBundle::CreateSyncChange(
122 const std::string& extension_id,
123 const syncer::SyncData& sync_data) const {
124 return syncer::SyncChange(
125 FROM_HERE,
126 HasExtensionId(extension_id) ? syncer::SyncChange::ACTION_UPDATE
127 : syncer::SyncChange::ACTION_ADD,
128 sync_data);
131 void SyncBundle::PushSyncChanges(
132 const syncer::SyncChangeList& sync_change_list) {
133 sync_processor_->ProcessSyncChanges(FROM_HERE, sync_change_list);
136 void SyncBundle::AddSyncedExtension(const std::string& id) {
137 synced_extensions_.insert(id);
140 void SyncBundle::RemoveSyncedExtension(const std::string& id) {
141 synced_extensions_.erase(id);
144 void SyncBundle::MarkPendingExtensionSynced(const std::string& id) {
145 pending_sync_data_.erase(id);
146 AddSyncedExtension(id);
149 } // namespace extensions