Make default apps cache multiprofile friendly
[chromium-blink-merge.git] / chrome / browser / extensions / app_sync_bundle.cc
blob8038feddca21d5a9ddf8946ba316237b99b7a0a1
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_service.h"
9 #include "chrome/browser/extensions/extension_sorting.h"
10 #include "chrome/common/extensions/extension.h"
11 #include "chrome/common/extensions/extension_set.h"
12 #include "chrome/common/extensions/sync_helper.h"
13 #include "sync/api/sync_change_processor.h"
14 #include "sync/api/sync_error_factory.h"
16 namespace extensions {
18 AppSyncBundle::AppSyncBundle(ExtensionService* extension_service)
19 : extension_service_(extension_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_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_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_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::SyncChangeIfNeeded(const Extension& extension) {
85 AppSyncData app_sync_data = extension_service_->GetAppSyncData(extension);
87 syncer::SyncChangeList sync_change_list(1, app_sync_data.GetSyncChange(
88 HasExtensionId(extension.id()) ?
89 syncer::SyncChange::ACTION_UPDATE : syncer::SyncChange::ACTION_ADD));
90 sync_processor_->ProcessSyncChanges(FROM_HERE, sync_change_list);
91 MarkPendingAppSynced(extension.id());
94 void AppSyncBundle::ProcessSyncChange(AppSyncData app_sync_data) {
95 if (app_sync_data.uninstalled())
96 RemoveApp(app_sync_data.id());
97 else
98 AddApp(app_sync_data.id());
99 extension_service_->ProcessAppSyncData(app_sync_data);
102 void AppSyncBundle::ProcessSyncChangeList(
103 syncer::SyncChangeList sync_change_list) {
104 sync_processor_->ProcessSyncChanges(FROM_HERE, sync_change_list);
105 extension_service_->extension_prefs()->extension_sorting()->
106 FixNTPOrdinalCollisions();
109 bool AppSyncBundle::HasExtensionId(const std::string& id) const {
110 return synced_apps_.find(id) != synced_apps_.end();
113 bool AppSyncBundle::HasPendingExtensionId(const std::string& id) const {
114 return pending_sync_data_.find(id) != pending_sync_data_.end();
117 void AppSyncBundle::AddPendingApp(const std::string& id,
118 const AppSyncData& app_sync_data) {
119 pending_sync_data_[id] = app_sync_data;
122 bool AppSyncBundle::IsSyncing() const {
123 return sync_processor_ != NULL;
126 std::vector<AppSyncData> AppSyncBundle::GetPendingData() const {
127 std::vector<AppSyncData> pending_apps;
128 for (std::map<std::string, AppSyncData>::const_iterator
129 i = pending_sync_data_.begin();
130 i != pending_sync_data_.end();
131 ++i) {
132 pending_apps.push_back(i->second);
135 return pending_apps;
138 void AppSyncBundle::GetAppSyncDataListHelper(
139 const ExtensionSet& extensions,
140 std::vector<AppSyncData>* sync_data_list) const {
141 for (ExtensionSet::const_iterator it = extensions.begin();
142 it != extensions.end(); ++it) {
143 const Extension& extension = *it->get();
144 // If we have pending app data for this app, then this
145 // version is out of date. We'll sync back the version we got from
146 // sync.
147 if (IsSyncing() && sync_helper::IsSyncableApp(&extension) &&
148 !HasPendingExtensionId(extension.id())) {
149 sync_data_list->push_back(extension_service_->GetAppSyncData(extension));
154 void AppSyncBundle::AddApp(const std::string& id) {
155 synced_apps_.insert(id);
158 void AppSyncBundle::RemoveApp(const std::string& id) {
159 synced_apps_.erase(id);
163 void AppSyncBundle::MarkPendingAppSynced(const std::string& id) {
164 pending_sync_data_.erase(id);
165 synced_apps_.insert(id);
169 } // namespace extensions