NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / extensions / app_sync_bundle.cc
blob061d5842fa04b1d883d0a7f0e02fffa197716732
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/app_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/browser/app_sorting.h"
11 #include "extensions/common/extension.h"
12 #include "extensions/common/extension_set.h"
13 #include "sync/api/sync_change_processor.h"
14 #include "sync/api/sync_error_factory.h"
16 namespace extensions {
18 AppSyncBundle::AppSyncBundle(ExtensionSyncService* extension_sync_service)
19 : extension_sync_service_(extension_sync_service) {}
21 AppSyncBundle::~AppSyncBundle() {}
23 void AppSyncBundle::SetupSync(
24 syncer::SyncChangeProcessor* sync_change_processor,
25 syncer::SyncErrorFactory* sync_error_factory,
26 const syncer::SyncDataList& initial_sync_data) {
27 sync_processor_.reset(sync_change_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 AppSyncData app_sync_data(*i);
34 AddApp(app_sync_data.id());
35 extension_sync_service_->ProcessAppSyncData(app_sync_data);
39 void AppSyncBundle::Reset() {
40 sync_processor_.reset();
41 sync_error_factory_.reset();
42 synced_apps_.clear();
43 pending_sync_data_.clear();
46 syncer::SyncChange AppSyncBundle::CreateSyncChangeToDelete(
47 const Extension* extension)
48 const {
49 AppSyncData sync_data = extension_sync_service_->GetAppSyncData(*extension);
50 return sync_data.GetSyncChange(syncer::SyncChange::ACTION_DELETE);
53 void AppSyncBundle::ProcessDeletion(std::string extension_id,
54 const syncer::SyncChange& sync_change) {
55 RemoveApp(extension_id);
56 sync_processor_->ProcessSyncChanges(FROM_HERE,
57 syncer::SyncChangeList(1, sync_change));
60 syncer::SyncChange AppSyncBundle::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 AddApp(sync_data.GetTag());
68 return syncer::SyncChange(FROM_HERE,
69 syncer::SyncChange::ACTION_ADD,
70 sync_data);
74 syncer::SyncDataList AppSyncBundle::GetAllSyncData() const {
75 std::vector<AppSyncData> app_sync_data =
76 extension_sync_service_->GetAppSyncDataList();
77 syncer::SyncDataList result(app_sync_data.size());
78 for (int i = 0; i < static_cast<int>(app_sync_data.size()); ++i) {
79 result[i] = app_sync_data[i].GetSyncData();
81 return result;
84 void AppSyncBundle::ProcessSyncChange(AppSyncData app_sync_data) {
85 if (app_sync_data.uninstalled())
86 RemoveApp(app_sync_data.id());
87 else
88 AddApp(app_sync_data.id());
89 extension_sync_service_->ProcessAppSyncData(app_sync_data);
92 void AppSyncBundle::ProcessSyncChangeList(
93 syncer::SyncChangeList sync_change_list) {
94 sync_processor_->ProcessSyncChanges(FROM_HERE, sync_change_list);
95 extension_sync_service_->extension_prefs().app_sorting()->
96 FixNTPOrdinalCollisions();
99 bool AppSyncBundle::HasExtensionId(const std::string& id) const {
100 return synced_apps_.find(id) != synced_apps_.end();
103 bool AppSyncBundle::HasPendingExtensionId(const std::string& id) const {
104 return pending_sync_data_.find(id) != pending_sync_data_.end();
107 void AppSyncBundle::AddPendingApp(const std::string& id,
108 const AppSyncData& app_sync_data) {
109 pending_sync_data_[id] = app_sync_data;
112 bool AppSyncBundle::IsSyncing() const {
113 return sync_processor_ != NULL;
116 void AppSyncBundle::SyncChangeIfNeeded(const Extension& extension) {
117 AppSyncData app_sync_data = extension_sync_service_->GetAppSyncData(
118 extension);
120 syncer::SyncChangeList sync_change_list(1, app_sync_data.GetSyncChange(
121 HasExtensionId(extension.id()) ?
122 syncer::SyncChange::ACTION_UPDATE : syncer::SyncChange::ACTION_ADD));
123 sync_processor_->ProcessSyncChanges(FROM_HERE, sync_change_list);
124 MarkPendingAppSynced(extension.id());
127 std::vector<AppSyncData> AppSyncBundle::GetPendingData() const {
128 std::vector<AppSyncData> pending_apps;
129 for (std::map<std::string, AppSyncData>::const_iterator
130 i = pending_sync_data_.begin();
131 i != pending_sync_data_.end();
132 ++i) {
133 pending_apps.push_back(i->second);
136 return pending_apps;
139 void AppSyncBundle::GetAppSyncDataListHelper(
140 const ExtensionSet& extensions,
141 std::vector<AppSyncData>* sync_data_list) const {
142 for (ExtensionSet::const_iterator it = extensions.begin();
143 it != extensions.end(); ++it) {
144 const Extension& extension = *it->get();
145 // If we have pending app data for this app, then this
146 // version is out of date. We'll sync back the version we got from
147 // sync.
148 if (IsSyncing() && sync_helper::IsSyncableApp(&extension) &&
149 !HasPendingExtensionId(extension.id())) {
150 sync_data_list->push_back(extension_sync_service_->GetAppSyncData(
151 extension));
156 void AppSyncBundle::AddApp(const std::string& id) {
157 synced_apps_.insert(id);
160 void AppSyncBundle::RemoveApp(const std::string& id) {
161 synced_apps_.erase(id);
165 void AppSyncBundle::MarkPendingAppSynced(const std::string& id) {
166 pending_sync_data_.erase(id);
167 synced_apps_.insert(id);
171 } // namespace extensions