Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / extensions / extension_sync_bundle.cc
blob9d91af92cc71df98cfa160aa71a34da7ce1d4ed3
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/extension_sync_bundle.h"
7 #include "base/location.h"
8 #include "chrome/browser/extensions/extension_sync_service.h"
9 #include "chrome/common/extensions/sync_helper.h"
10 #include "extensions/common/extension.h"
11 #include "extensions/common/extension_set.h"
12 #include "sync/api/sync_change_processor.h"
13 #include "sync/api/sync_error_factory.h"
15 namespace extensions {
17 ExtensionSyncBundle::ExtensionSyncBundle(
18 ExtensionSyncService* extension_sync_service)
19 : extension_sync_service_(extension_sync_service) {}
21 ExtensionSyncBundle::~ExtensionSyncBundle() {}
23 void ExtensionSyncBundle::SetupSync(
24 syncer::SyncChangeProcessor* sync_processor,
25 syncer::SyncErrorFactory* sync_error_factory,
26 const syncer::SyncDataList& initial_sync_data) {
27 sync_processor_.reset(sync_processor);
28 sync_error_factory_.reset(sync_error_factory);
30 for (syncer::SyncDataList::const_iterator i = initial_sync_data.begin();
31 i != initial_sync_data.end();
32 ++i) {
33 ExtensionSyncData extension_sync_data(*i);
34 AddExtension(extension_sync_data.id());
35 extension_sync_service_->ProcessExtensionSyncData(extension_sync_data);
39 void ExtensionSyncBundle::Reset() {
40 sync_processor_.reset();
41 sync_error_factory_.reset();
42 synced_extensions_.clear();
43 pending_sync_data_.clear();
46 syncer::SyncChange ExtensionSyncBundle::CreateSyncChangeToDelete(
47 const Extension* extension) const {
48 extensions::ExtensionSyncData sync_data =
49 extension_sync_service_->GetExtensionSyncData(*extension);
50 return sync_data.GetSyncChange(syncer::SyncChange::ACTION_DELETE);
53 void ExtensionSyncBundle::ProcessDeletion(
54 std::string extension_id, const syncer::SyncChange& sync_change) {
55 RemoveExtension(extension_id);
56 sync_processor_->ProcessSyncChanges(FROM_HERE,
57 syncer::SyncChangeList(1, sync_change));
60 syncer::SyncChange ExtensionSyncBundle::CreateSyncChange(
61 const syncer::SyncData& sync_data) {
62 if (HasExtensionId(sync_data.GetTag())) {
63 return syncer::SyncChange(FROM_HERE,
64 syncer::SyncChange::ACTION_UPDATE,
65 sync_data);
66 } else {
67 AddExtension(sync_data.GetTag());
68 return syncer::SyncChange(FROM_HERE,
69 syncer::SyncChange::ACTION_ADD,
70 sync_data);
74 syncer::SyncDataList ExtensionSyncBundle::GetAllSyncData() const {
75 std::vector<ExtensionSyncData> extension_sync_data =
76 extension_sync_service_->GetExtensionSyncDataList();
77 syncer::SyncDataList result(extension_sync_data.size());
78 for (int i = 0; i < static_cast<int>(extension_sync_data.size()); ++i) {
79 result[i] = extension_sync_data[i].GetSyncData();
81 return result;
84 void ExtensionSyncBundle::ProcessSyncChange(
85 ExtensionSyncData extension_sync_data) {
86 if (extension_sync_data.uninstalled())
87 RemoveExtension(extension_sync_data.id());
88 else
89 AddExtension(extension_sync_data.id());
90 extension_sync_service_->ProcessExtensionSyncData(extension_sync_data);
93 void ExtensionSyncBundle::ProcessSyncChangeList(
94 syncer::SyncChangeList sync_change_list) {
95 sync_processor_->ProcessSyncChanges(FROM_HERE, sync_change_list);
98 bool ExtensionSyncBundle::HasExtensionId(
99 const std::string& id) const {
100 return synced_extensions_.find(id) != synced_extensions_.end();
103 bool ExtensionSyncBundle::HasPendingExtensionId(
104 const std::string& id) const {
105 return pending_sync_data_.find(id) != pending_sync_data_.end();
108 void ExtensionSyncBundle::AddPendingExtension(
109 const std::string& id,
110 const ExtensionSyncData& extension_sync_data) {
111 pending_sync_data_[id] = extension_sync_data;
114 bool ExtensionSyncBundle::IsSyncing() const {
115 return sync_processor_ != NULL;
118 void ExtensionSyncBundle::SyncChangeIfNeeded(const Extension& extension) {
119 ExtensionSyncData extension_sync_data =
120 extension_sync_service_->GetExtensionSyncData(extension);
122 syncer::SyncChangeList sync_change_list(1, extension_sync_data.GetSyncChange(
123 HasExtensionId(extension.id()) ?
124 syncer::SyncChange::ACTION_UPDATE : syncer::SyncChange::ACTION_ADD));
125 sync_processor_->ProcessSyncChanges(FROM_HERE, sync_change_list);
126 MarkPendingExtensionSynced(extension.id());
129 std::vector<ExtensionSyncData> ExtensionSyncBundle::GetPendingData() const {
130 std::vector<ExtensionSyncData> pending_extensions;
131 for (std::map<std::string, ExtensionSyncData>::const_iterator
132 i = pending_sync_data_.begin();
133 i != pending_sync_data_.end();
134 ++i) {
135 pending_extensions.push_back(i->second);
138 return pending_extensions;
141 void ExtensionSyncBundle::GetExtensionSyncDataListHelper(
142 const ExtensionSet& extensions,
143 std::vector<ExtensionSyncData>* sync_data_list) const {
144 for (ExtensionSet::const_iterator it = extensions.begin();
145 it != extensions.end(); ++it) {
146 const Extension& extension = *it->get();
147 // If we have pending extension data for this extension, then this
148 // version is out of date. We'll sync back the version we got from
149 // sync.
150 if (IsSyncing() && sync_helper::IsSyncableExtension(&extension) &&
151 !HasPendingExtensionId(extension.id())) {
152 sync_data_list->push_back(
153 extension_sync_service_->GetExtensionSyncData(extension));
158 void ExtensionSyncBundle::AddExtension(const std::string& id) {
159 synced_extensions_.insert(id);
162 void ExtensionSyncBundle::RemoveExtension(const std::string& id) {
163 synced_extensions_.erase(id);
166 void ExtensionSyncBundle::MarkPendingExtensionSynced(const std::string& id) {
167 pending_sync_data_.erase(id);
168 synced_extensions_.insert(id);
171 } // namespace extensions