Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / app_sync_bundle.cc
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.
4
5 #include "chrome/browser/extensions/app_sync_bundle.h"
6
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_data.h"
15 #include "sync/api/sync_error_factory.h"
16
17 namespace extensions {
18
19 AppSyncBundle::AppSyncBundle(ExtensionSyncService* extension_sync_service)
20     : extension_sync_service_(extension_sync_service) {}
21
22 AppSyncBundle::~AppSyncBundle() {}
23
24 void AppSyncBundle::SetupSync(
25     syncer::SyncChangeProcessor* sync_change_processor,
26     syncer::SyncErrorFactory* sync_error_factory,
27     const syncer::SyncDataList& initial_sync_data) {
28   sync_processor_.reset(sync_change_processor);
29   sync_error_factory_.reset(sync_error_factory);
30
31   for (syncer::SyncDataList::const_iterator i = initial_sync_data.begin();
32        i != initial_sync_data.end();
33        ++i) {
34     AppSyncData app_sync_data(*i);
35     AddApp(app_sync_data.id());
36     extension_sync_service_->ProcessAppSyncData(app_sync_data);
37   }
38 }
39
40 void AppSyncBundle::Reset() {
41   sync_processor_.reset();
42   sync_error_factory_.reset();
43   synced_apps_.clear();
44   pending_sync_data_.clear();
45 }
46
47 syncer::SyncChange AppSyncBundle::CreateSyncChangeToDelete(
48     const Extension* extension)
49     const {
50   AppSyncData sync_data = extension_sync_service_->GetAppSyncData(*extension);
51   return sync_data.GetSyncChange(syncer::SyncChange::ACTION_DELETE);
52 }
53
54 void AppSyncBundle::ProcessDeletion(const std::string& extension_id,
55                                     const syncer::SyncChange& sync_change) {
56   RemoveApp(extension_id);
57   sync_processor_->ProcessSyncChanges(FROM_HERE,
58                                       syncer::SyncChangeList(1, sync_change));
59 }
60
61 syncer::SyncChange AppSyncBundle::CreateSyncChange(
62     const syncer::SyncData& sync_data) {
63   const syncer::SyncDataLocal sync_data_local(sync_data);
64   if (HasExtensionId(sync_data_local.GetTag())) {
65     return syncer::SyncChange(FROM_HERE,
66                               syncer::SyncChange::ACTION_UPDATE,
67                               sync_data);
68   } else {
69     AddApp(sync_data_local.GetTag());
70     return syncer::SyncChange(FROM_HERE,
71                               syncer::SyncChange::ACTION_ADD,
72                               sync_data);
73   }
74 }
75
76 syncer::SyncDataList AppSyncBundle::GetAllSyncData() const {
77   std::vector<AppSyncData> app_sync_data =
78       extension_sync_service_->GetAppSyncDataList();
79   syncer::SyncDataList result(app_sync_data.size());
80   for (int i = 0; i < static_cast<int>(app_sync_data.size()); ++i) {
81     result[i] = app_sync_data[i].GetSyncData();
82   }
83   return result;
84 }
85
86 void AppSyncBundle::ProcessSyncChange(AppSyncData app_sync_data) {
87   if (app_sync_data.uninstalled())
88     RemoveApp(app_sync_data.id());
89   else
90     AddApp(app_sync_data.id());
91   extension_sync_service_->ProcessAppSyncData(app_sync_data);
92 }
93
94 void AppSyncBundle::ProcessSyncChangeList(
95     syncer::SyncChangeList sync_change_list) {
96   sync_processor_->ProcessSyncChanges(FROM_HERE, sync_change_list);
97   extension_sync_service_->extension_prefs().app_sorting()->
98       FixNTPOrdinalCollisions();
99 }
100
101 bool AppSyncBundle::HasExtensionId(const std::string& id) const {
102   return synced_apps_.find(id) != synced_apps_.end();
103 }
104
105 bool AppSyncBundle::HasPendingExtensionId(const std::string& id) const {
106   return pending_sync_data_.find(id) != pending_sync_data_.end();
107 }
108
109 void AppSyncBundle::AddPendingApp(const std::string& id,
110                                   const AppSyncData& app_sync_data) {
111   pending_sync_data_[id] = app_sync_data;
112 }
113
114 bool AppSyncBundle::IsSyncing() const {
115   return sync_processor_ != NULL;
116 }
117
118 void AppSyncBundle::SyncChangeIfNeeded(const Extension& extension) {
119   AppSyncData app_sync_data = extension_sync_service_->GetAppSyncData(
120       extension);
121
122   syncer::SyncChangeList sync_change_list(1, app_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   MarkPendingAppSynced(extension.id());
127 }
128
129 std::vector<AppSyncData> AppSyncBundle::GetPendingData() const {
130   std::vector<AppSyncData> pending_apps;
131   for (std::map<std::string, AppSyncData>::const_iterator
132            i = pending_sync_data_.begin();
133        i != pending_sync_data_.end();
134        ++i) {
135     pending_apps.push_back(i->second);
136   }
137
138   return pending_apps;
139 }
140
141 void AppSyncBundle::GetAppSyncDataListHelper(
142     const ExtensionSet& extensions,
143     std::vector<AppSyncData>* 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 app data for this app, then this
148     // version is out of date.  We'll sync back the version we got from
149     // sync.
150     if (IsSyncing() && sync_helper::IsSyncableApp(&extension) &&
151         !HasPendingExtensionId(extension.id())) {
152       sync_data_list->push_back(extension_sync_service_->GetAppSyncData(
153           extension));
154     }
155   }
156 }
157
158 void AppSyncBundle::AddApp(const std::string& id) {
159   synced_apps_.insert(id);
160 }
161
162 void AppSyncBundle::RemoveApp(const std::string& id) {
163   synced_apps_.erase(id);
164 }
165
166
167 void AppSyncBundle::MarkPendingAppSynced(const std::string& id) {
168   pending_sync_data_.erase(id);
169   synced_apps_.insert(id);
170 }
171
172
173 }  // namespace extensions