app_list: Re-enable people search.
[chromium-blink-merge.git] / chrome / browser / extensions / extension_sync_bundle.cc
blob2333ffa6b94f430147f535194a32077f68168b2f
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 ExtensionSyncData extension_sync_data(*i);
36 AddExtension(extension_sync_data.id());
37 extension_sync_service_->ProcessExtensionSyncData(extension_sync_data);
41 void ExtensionSyncBundle::Reset() {
42 sync_processor_.reset();
43 sync_error_factory_.reset();
44 synced_extensions_.clear();
45 pending_sync_data_.clear();
48 syncer::SyncChange ExtensionSyncBundle::CreateSyncChangeToDelete(
49 const Extension* extension) const {
50 extensions::ExtensionSyncData sync_data =
51 extension_sync_service_->GetExtensionSyncData(*extension);
52 return sync_data.GetSyncChange(syncer::SyncChange::ACTION_DELETE);
55 void ExtensionSyncBundle::ProcessDeletion(
56 std::string extension_id, const syncer::SyncChange& sync_change) {
57 RemoveExtension(extension_id);
58 sync_processor_->ProcessSyncChanges(FROM_HERE,
59 syncer::SyncChangeList(1, sync_change));
62 syncer::SyncChange ExtensionSyncBundle::CreateSyncChange(
63 const syncer::SyncData& sync_data) {
64 const syncer::SyncDataLocal sync_data_local(sync_data);
65 if (HasExtensionId(sync_data_local.GetTag())) {
66 return syncer::SyncChange(FROM_HERE,
67 syncer::SyncChange::ACTION_UPDATE,
68 sync_data);
69 } else {
70 AddExtension(sync_data_local.GetTag());
71 return syncer::SyncChange(FROM_HERE,
72 syncer::SyncChange::ACTION_ADD,
73 sync_data);
77 syncer::SyncDataList ExtensionSyncBundle::GetAllSyncData() const {
78 std::vector<ExtensionSyncData> extension_sync_data =
79 extension_sync_service_->GetExtensionSyncDataList();
80 syncer::SyncDataList result(extension_sync_data.size());
81 for (int i = 0; i < static_cast<int>(extension_sync_data.size()); ++i) {
82 result[i] = extension_sync_data[i].GetSyncData();
84 return result;
87 void ExtensionSyncBundle::ProcessSyncChange(
88 ExtensionSyncData extension_sync_data) {
89 if (extension_sync_data.uninstalled())
90 RemoveExtension(extension_sync_data.id());
91 else
92 AddExtension(extension_sync_data.id());
93 extension_sync_service_->ProcessExtensionSyncData(extension_sync_data);
96 void ExtensionSyncBundle::ProcessSyncChangeList(
97 syncer::SyncChangeList sync_change_list) {
98 sync_processor_->ProcessSyncChanges(FROM_HERE, sync_change_list);
101 bool ExtensionSyncBundle::HasExtensionId(
102 const std::string& id) const {
103 return synced_extensions_.find(id) != synced_extensions_.end();
106 bool ExtensionSyncBundle::HasPendingExtensionId(
107 const std::string& id) const {
108 return pending_sync_data_.find(id) != pending_sync_data_.end();
111 void ExtensionSyncBundle::AddPendingExtension(
112 const std::string& id,
113 const ExtensionSyncData& extension_sync_data) {
114 pending_sync_data_[id] = extension_sync_data;
117 bool ExtensionSyncBundle::IsSyncing() const {
118 return sync_processor_ != NULL;
121 void ExtensionSyncBundle::SyncChangeIfNeeded(const Extension& extension) {
122 ExtensionSyncData extension_sync_data =
123 extension_sync_service_->GetExtensionSyncData(extension);
125 syncer::SyncChangeList sync_change_list(1, extension_sync_data.GetSyncChange(
126 HasExtensionId(extension.id()) ?
127 syncer::SyncChange::ACTION_UPDATE : syncer::SyncChange::ACTION_ADD));
128 sync_processor_->ProcessSyncChanges(FROM_HERE, sync_change_list);
129 MarkPendingExtensionSynced(extension.id());
132 std::vector<ExtensionSyncData> ExtensionSyncBundle::GetPendingData() const {
133 std::vector<ExtensionSyncData> pending_extensions;
134 for (std::map<std::string, ExtensionSyncData>::const_iterator
135 i = pending_sync_data_.begin();
136 i != pending_sync_data_.end();
137 ++i) {
138 pending_extensions.push_back(i->second);
141 return pending_extensions;
144 void ExtensionSyncBundle::GetExtensionSyncDataListHelper(
145 const ExtensionSet& extensions,
146 std::vector<ExtensionSyncData>* sync_data_list) const {
147 Profile* profile = extension_sync_service_->profile();
149 for (ExtensionSet::const_iterator it = extensions.begin();
150 it != extensions.end(); ++it) {
151 const Extension& extension = *it->get();
152 // If we have pending extension data for this extension, then this
153 // version is out of date. We'll sync back the version we got from
154 // sync.
155 if (IsSyncing() && util::ShouldSyncExtension(&extension, profile) &&
156 !HasPendingExtensionId(extension.id())) {
157 sync_data_list->push_back(
158 extension_sync_service_->GetExtensionSyncData(extension));
163 void ExtensionSyncBundle::AddExtension(const std::string& id) {
164 synced_extensions_.insert(id);
167 void ExtensionSyncBundle::RemoveExtension(const std::string& id) {
168 synced_extensions_.erase(id);
171 void ExtensionSyncBundle::MarkPendingExtensionSynced(const std::string& id) {
172 pending_sync_data_.erase(id);
173 synced_extensions_.insert(id);
176 } // namespace extensions