Add a webstorePrivate API to show a permission prompt for delegated bundle installs
[chromium-blink-merge.git] / chrome / browser / extensions / extension_sync_bundle.cc
blob61ec1173532d5242c54099ae788d496e87670568
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/browser/extensions/extension_util.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/common/extensions/sync_helper.h"
12 #include "extensions/common/extension.h"
13 #include "extensions/common/extension_set.h"
14 #include "sync/api/sync_change_processor.h"
15 #include "sync/api/sync_error_factory.h"
17 namespace extensions {
19 ExtensionSyncBundle::ExtensionSyncBundle(
20 ExtensionSyncService* extension_sync_service)
21 : extension_sync_service_(extension_sync_service) {}
23 ExtensionSyncBundle::~ExtensionSyncBundle() {}
25 void ExtensionSyncBundle::SetupSync(
26 syncer::SyncChangeProcessor* sync_processor,
27 syncer::SyncErrorFactory* sync_error_factory,
28 const syncer::SyncDataList& initial_sync_data) {
29 sync_processor_.reset(sync_processor);
30 sync_error_factory_.reset(sync_error_factory);
32 for (syncer::SyncDataList::const_iterator i = initial_sync_data.begin();
33 i != initial_sync_data.end();
34 ++i) {
35 scoped_ptr<ExtensionSyncData> extension_sync_data(
36 ExtensionSyncData::CreateFromSyncData(*i));
37 if (extension_sync_data.get()) {
38 AddExtension(extension_sync_data->id());
39 extension_sync_service_->ProcessExtensionSyncData(*extension_sync_data);
44 void ExtensionSyncBundle::Reset() {
45 sync_processor_.reset();
46 sync_error_factory_.reset();
47 synced_extensions_.clear();
48 pending_sync_data_.clear();
51 syncer::SyncChange ExtensionSyncBundle::CreateSyncChangeToDelete(
52 const Extension* extension) const {
53 extensions::ExtensionSyncData sync_data =
54 extension_sync_service_->GetExtensionSyncData(*extension);
55 return sync_data.GetSyncChange(syncer::SyncChange::ACTION_DELETE);
58 void ExtensionSyncBundle::ProcessDeletion(
59 std::string extension_id, const syncer::SyncChange& sync_change) {
60 RemoveExtension(extension_id);
61 sync_processor_->ProcessSyncChanges(FROM_HERE,
62 syncer::SyncChangeList(1, sync_change));
65 syncer::SyncChange ExtensionSyncBundle::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,
71 sync_data);
72 } else {
73 AddExtension(sync_data_local.GetTag());
74 return syncer::SyncChange(FROM_HERE,
75 syncer::SyncChange::ACTION_ADD,
76 sync_data);
80 syncer::SyncDataList ExtensionSyncBundle::GetAllSyncData() const {
81 std::vector<ExtensionSyncData> extension_sync_data =
82 extension_sync_service_->GetExtensionSyncDataList();
83 syncer::SyncDataList result(extension_sync_data.size());
84 for (int i = 0; i < static_cast<int>(extension_sync_data.size()); ++i) {
85 result[i] = extension_sync_data[i].GetSyncData();
87 return result;
90 void ExtensionSyncBundle::ProcessSyncChange(
91 ExtensionSyncData extension_sync_data) {
92 if (extension_sync_data.uninstalled())
93 RemoveExtension(extension_sync_data.id());
94 else
95 AddExtension(extension_sync_data.id());
96 extension_sync_service_->ProcessExtensionSyncData(extension_sync_data);
99 void ExtensionSyncBundle::ProcessSyncChangeList(
100 syncer::SyncChangeList sync_change_list) {
101 sync_processor_->ProcessSyncChanges(FROM_HERE, sync_change_list);
104 bool ExtensionSyncBundle::HasExtensionId(
105 const std::string& id) const {
106 return synced_extensions_.find(id) != synced_extensions_.end();
109 bool ExtensionSyncBundle::HasPendingExtensionId(
110 const std::string& id) const {
111 return pending_sync_data_.find(id) != pending_sync_data_.end();
114 void ExtensionSyncBundle::AddPendingExtension(
115 const std::string& id,
116 const ExtensionSyncData& extension_sync_data) {
117 pending_sync_data_[id] = extension_sync_data;
120 bool ExtensionSyncBundle::IsSyncing() const {
121 return sync_processor_ != NULL;
124 void ExtensionSyncBundle::SyncChangeIfNeeded(const Extension& extension) {
125 ExtensionSyncData extension_sync_data =
126 extension_sync_service_->GetExtensionSyncData(extension);
128 syncer::SyncChangeList sync_change_list(1, extension_sync_data.GetSyncChange(
129 HasExtensionId(extension.id()) ?
130 syncer::SyncChange::ACTION_UPDATE : syncer::SyncChange::ACTION_ADD));
131 sync_processor_->ProcessSyncChanges(FROM_HERE, sync_change_list);
132 MarkPendingExtensionSynced(extension.id());
135 std::vector<ExtensionSyncData> ExtensionSyncBundle::GetPendingData() const {
136 std::vector<ExtensionSyncData> pending_extensions;
137 for (std::map<std::string, ExtensionSyncData>::const_iterator
138 i = pending_sync_data_.begin();
139 i != pending_sync_data_.end();
140 ++i) {
141 pending_extensions.push_back(i->second);
144 return pending_extensions;
147 void ExtensionSyncBundle::GetExtensionSyncDataListHelper(
148 const ExtensionSet& extensions,
149 std::vector<ExtensionSyncData>* sync_data_list) const {
150 Profile* profile = extension_sync_service_->profile();
152 for (ExtensionSet::const_iterator it = extensions.begin();
153 it != extensions.end(); ++it) {
154 const Extension& extension = *it->get();
155 // If we have pending extension data for this extension, then this
156 // version is out of date. We'll sync back the version we got from
157 // sync.
158 if (IsSyncing() && util::ShouldSyncExtension(&extension, profile) &&
159 !HasPendingExtensionId(extension.id())) {
160 sync_data_list->push_back(
161 extension_sync_service_->GetExtensionSyncData(extension));
166 void ExtensionSyncBundle::AddExtension(const std::string& id) {
167 synced_extensions_.insert(id);
170 void ExtensionSyncBundle::RemoveExtension(const std::string& id) {
171 synced_extensions_.erase(id);
174 void ExtensionSyncBundle::MarkPendingExtensionSynced(const std::string& id) {
175 pending_sync_data_.erase(id);
176 synced_extensions_.insert(id);
179 } // namespace extensions