Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / sync / glue / sync_backend_registrar.cc
blob8628c813c13ec4b5244028839ba977ee0e0e3c4a
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/sync/glue/sync_backend_registrar.h"
7 #include <cstddef>
9 #include "base/compiler_specific.h"
10 #include "base/logging.h"
11 #include "base/message_loop/message_loop.h"
12 #include "chrome/browser/history/history_service_factory.h"
13 #include "chrome/browser/password_manager/password_store_factory.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/sync/glue/browser_thread_model_worker.h"
16 #include "chrome/browser/sync/glue/history_model_worker.h"
17 #include "chrome/browser/sync/glue/ui_model_worker.h"
18 #include "components/password_manager/core/browser/password_store.h"
19 #include "components/password_manager/sync/browser/password_model_worker.h"
20 #include "components/sync_driver/change_processor.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "sync/internal_api/public/engine/passive_model_worker.h"
23 #include "sync/internal_api/public/user_share.h"
25 using content::BrowserThread;
27 namespace browser_sync {
29 namespace {
31 // Returns true if the current thread is the native thread for the
32 // given group (or if it is undeterminable).
33 bool IsOnThreadForGroup(syncer::ModelType type, syncer::ModelSafeGroup group) {
34 switch (group) {
35 case syncer::GROUP_PASSIVE:
36 return IsControlType(type);
37 case syncer::GROUP_UI:
38 return BrowserThread::CurrentlyOn(BrowserThread::UI);
39 case syncer::GROUP_DB:
40 return BrowserThread::CurrentlyOn(BrowserThread::DB);
41 case syncer::GROUP_FILE:
42 return BrowserThread::CurrentlyOn(BrowserThread::FILE);
43 case syncer::GROUP_HISTORY:
44 // TODO(sync): How to check we're on the right thread?
45 return type == syncer::TYPED_URLS;
46 case syncer::GROUP_PASSWORD:
47 // TODO(sync): How to check we're on the right thread?
48 return type == syncer::PASSWORDS;
49 case syncer::MODEL_SAFE_GROUP_COUNT:
50 default:
51 return false;
55 } // namespace
57 SyncBackendRegistrar::SyncBackendRegistrar(
58 const std::string& name,
59 Profile* profile,
60 scoped_ptr<base::Thread> sync_thread) :
61 name_(name),
62 profile_(profile) {
63 DCHECK_CURRENTLY_ON(BrowserThread::UI);
64 CHECK(profile_);
66 sync_thread_ = sync_thread.Pass();
67 if (!sync_thread_) {
68 sync_thread_.reset(new base::Thread("Chrome_SyncThread"));
69 base::Thread::Options options;
70 options.timer_slack = base::TIMER_SLACK_MAXIMUM;
71 CHECK(sync_thread_->StartWithOptions(options));
74 workers_[syncer::GROUP_DB] = new DatabaseModelWorker(this);
75 workers_[syncer::GROUP_DB]->RegisterForLoopDestruction();
77 workers_[syncer::GROUP_FILE] = new FileModelWorker(this);
78 workers_[syncer::GROUP_FILE]->RegisterForLoopDestruction();
80 workers_[syncer::GROUP_UI] = new UIModelWorker(this);
81 workers_[syncer::GROUP_UI]->RegisterForLoopDestruction();
83 // GROUP_PASSIVE worker does work on sync_loop_. But sync_loop_ is not
84 // stopped until all workers have stopped. To break the cycle, use UI loop
85 // instead.
86 workers_[syncer::GROUP_PASSIVE] =
87 new syncer::PassiveModelWorker(sync_thread_->message_loop(), this);
88 workers_[syncer::GROUP_PASSIVE]->RegisterForLoopDestruction();
90 history::HistoryService* history_service =
91 HistoryServiceFactory::GetForProfile(profile,
92 ServiceAccessType::IMPLICIT_ACCESS);
93 if (history_service) {
94 workers_[syncer::GROUP_HISTORY] =
95 new HistoryModelWorker(history_service->AsWeakPtr(), this);
96 workers_[syncer::GROUP_HISTORY]->RegisterForLoopDestruction();
100 scoped_refptr<password_manager::PasswordStore> password_store =
101 PasswordStoreFactory::GetForProfile(profile,
102 ServiceAccessType::IMPLICIT_ACCESS);
103 if (password_store.get()) {
104 workers_[syncer::GROUP_PASSWORD] =
105 new PasswordModelWorker(password_store, this);
106 workers_[syncer::GROUP_PASSWORD]->RegisterForLoopDestruction();
110 void SyncBackendRegistrar::SetInitialTypes(syncer::ModelTypeSet initial_types) {
111 base::AutoLock lock(lock_);
113 // This function should be called only once, shortly after construction. The
114 // routing info at that point is expected to be empty.
115 DCHECK(routing_info_.empty());
117 // Set our initial state to reflect the current status of the sync directory.
118 // This will ensure that our calculations in ConfigureDataTypes() will always
119 // return correct results.
120 for (syncer::ModelTypeSet::Iterator it = initial_types.First();
121 it.Good(); it.Inc()) {
122 routing_info_[it.Get()] = syncer::GROUP_PASSIVE;
125 if (!workers_.count(syncer::GROUP_HISTORY)) {
126 LOG_IF(WARNING, initial_types.Has(syncer::TYPED_URLS))
127 << "History store disabled, cannot sync Omnibox History";
128 routing_info_.erase(syncer::TYPED_URLS);
131 if (!workers_.count(syncer::GROUP_PASSWORD)) {
132 LOG_IF(WARNING, initial_types.Has(syncer::PASSWORDS))
133 << "Password store not initialized, cannot sync passwords";
134 routing_info_.erase(syncer::PASSWORDS);
137 last_configured_types_ = syncer::GetRoutingInfoTypes(routing_info_);
140 bool SyncBackendRegistrar::IsNigoriEnabled() const {
141 DCHECK_CURRENTLY_ON(BrowserThread::UI);
142 base::AutoLock lock(lock_);
143 return routing_info_.find(syncer::NIGORI) != routing_info_.end();
146 syncer::ModelTypeSet SyncBackendRegistrar::ConfigureDataTypes(
147 syncer::ModelTypeSet types_to_add,
148 syncer::ModelTypeSet types_to_remove) {
149 DCHECK(Intersection(types_to_add, types_to_remove).Empty());
150 syncer::ModelTypeSet filtered_types_to_add = types_to_add;
151 if (workers_.count(syncer::GROUP_HISTORY) == 0) {
152 LOG(WARNING) << "No history worker -- removing TYPED_URLS";
153 filtered_types_to_add.Remove(syncer::TYPED_URLS);
155 if (workers_.count(syncer::GROUP_PASSWORD) == 0) {
156 LOG(WARNING) << "No password worker -- removing PASSWORDS";
157 filtered_types_to_add.Remove(syncer::PASSWORDS);
160 base::AutoLock lock(lock_);
161 syncer::ModelTypeSet newly_added_types;
162 for (syncer::ModelTypeSet::Iterator it =
163 filtered_types_to_add.First();
164 it.Good(); it.Inc()) {
165 // Add a newly specified data type as syncer::GROUP_PASSIVE into the
166 // routing_info, if it does not already exist.
167 if (routing_info_.count(it.Get()) == 0) {
168 routing_info_[it.Get()] = syncer::GROUP_PASSIVE;
169 newly_added_types.Put(it.Get());
172 for (syncer::ModelTypeSet::Iterator it = types_to_remove.First();
173 it.Good(); it.Inc()) {
174 routing_info_.erase(it.Get());
177 // TODO(akalin): Use SVLOG/SLOG if we add any more logging.
178 DVLOG(1) << name_ << ": Adding types "
179 << syncer::ModelTypeSetToString(types_to_add)
180 << " (with newly-added types "
181 << syncer::ModelTypeSetToString(newly_added_types)
182 << ") and removing types "
183 << syncer::ModelTypeSetToString(types_to_remove)
184 << " to get new routing info "
185 <<syncer::ModelSafeRoutingInfoToString(routing_info_);
186 last_configured_types_ = syncer::GetRoutingInfoTypes(routing_info_);
188 return newly_added_types;
191 syncer::ModelTypeSet SyncBackendRegistrar::GetLastConfiguredTypes() const {
192 return last_configured_types_;
195 void SyncBackendRegistrar::RequestWorkerStopOnUIThread() {
196 DCHECK_CURRENTLY_ON(BrowserThread::UI);
197 base::AutoLock lock(lock_);
198 for (WorkerMap::const_iterator it = workers_.begin();
199 it != workers_.end(); ++it) {
200 it->second->RequestStop();
204 void SyncBackendRegistrar::ActivateDataType(
205 syncer::ModelType type,
206 syncer::ModelSafeGroup group,
207 sync_driver::ChangeProcessor* change_processor,
208 syncer::UserShare* user_share) {
209 DVLOG(1) << "Activate: " << syncer::ModelTypeToString(type);
211 base::AutoLock lock(lock_);
212 // Ensure that the given data type is in the PASSIVE group.
213 syncer::ModelSafeRoutingInfo::iterator i = routing_info_.find(type);
214 DCHECK(i != routing_info_.end());
215 DCHECK_EQ(i->second, syncer::GROUP_PASSIVE);
216 routing_info_[type] = group;
218 // Add the data type's change processor to the list of change
219 // processors so it can receive updates.
220 DCHECK_EQ(processors_.count(type), 0U);
221 processors_[type] = change_processor;
223 // Start the change processor.
224 change_processor->Start(user_share);
225 DCHECK(GetProcessorUnsafe(type));
228 void SyncBackendRegistrar::DeactivateDataType(syncer::ModelType type) {
229 DVLOG(1) << "Deactivate: " << syncer::ModelTypeToString(type);
231 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || IsControlType(type));
232 base::AutoLock lock(lock_);
234 routing_info_.erase(type);
235 ignore_result(processors_.erase(type));
236 DCHECK(!GetProcessorUnsafe(type));
239 bool SyncBackendRegistrar::IsTypeActivatedForTest(
240 syncer::ModelType type) const {
241 return GetProcessor(type) != NULL;
244 void SyncBackendRegistrar::OnChangesApplied(
245 syncer::ModelType model_type,
246 int64 model_version,
247 const syncer::BaseTransaction* trans,
248 const syncer::ImmutableChangeRecordList& changes) {
249 sync_driver::ChangeProcessor* processor = GetProcessor(model_type);
250 if (!processor)
251 return;
253 processor->ApplyChangesFromSyncModel(trans, model_version, changes);
256 void SyncBackendRegistrar::OnChangesComplete(syncer::ModelType model_type) {
257 sync_driver::ChangeProcessor* processor = GetProcessor(model_type);
258 if (!processor)
259 return;
261 // This call just notifies the processor that it can commit; it
262 // already buffered any changes it plans to makes so needs no
263 // further information.
264 processor->CommitChangesFromSyncModel();
267 void SyncBackendRegistrar::GetWorkers(
268 std::vector<scoped_refptr<syncer::ModelSafeWorker> >* out) {
269 base::AutoLock lock(lock_);
270 out->clear();
271 for (WorkerMap::const_iterator it = workers_.begin();
272 it != workers_.end(); ++it) {
273 out->push_back(it->second.get());
277 void SyncBackendRegistrar::GetModelSafeRoutingInfo(
278 syncer::ModelSafeRoutingInfo* out) {
279 base::AutoLock lock(lock_);
280 syncer::ModelSafeRoutingInfo copy(routing_info_);
281 out->swap(copy);
284 sync_driver::ChangeProcessor* SyncBackendRegistrar::GetProcessor(
285 syncer::ModelType type) const {
286 base::AutoLock lock(lock_);
287 sync_driver::ChangeProcessor* processor = GetProcessorUnsafe(type);
288 if (!processor)
289 return NULL;
291 // We can only check if |processor| exists, as otherwise the type is
292 // mapped to syncer::GROUP_PASSIVE.
293 CHECK(IsCurrentThreadSafeForModel(type));
294 return processor;
297 sync_driver::ChangeProcessor* SyncBackendRegistrar::GetProcessorUnsafe(
298 syncer::ModelType type) const {
299 lock_.AssertAcquired();
300 std::map<syncer::ModelType, sync_driver::ChangeProcessor*>::const_iterator
301 it = processors_.find(type);
303 // Until model association happens for a datatype, it will not
304 // appear in the processors list. During this time, it is OK to
305 // drop changes on the floor (since model association has not
306 // happened yet). When the data type is activated, model
307 // association takes place then the change processor is added to the
308 // |processors_| list.
309 if (it == processors_.end())
310 return NULL;
312 return it->second;
315 bool SyncBackendRegistrar::IsCurrentThreadSafeForModel(
316 syncer::ModelType model_type) const {
317 lock_.AssertAcquired();
318 return IsOnThreadForGroup(model_type,
319 GetGroupForModelType(model_type, routing_info_));
322 SyncBackendRegistrar::~SyncBackendRegistrar() {
323 DCHECK(workers_.empty());
326 void SyncBackendRegistrar::OnWorkerLoopDestroyed(syncer::ModelSafeGroup group) {
327 RemoveWorker(group);
330 void SyncBackendRegistrar::OnWorkerUnregistrationDone(
331 syncer::ModelSafeGroup group) {
332 RemoveWorker(group);
335 void SyncBackendRegistrar::RemoveWorker(syncer::ModelSafeGroup group) {
336 DVLOG(1) << "Remove " << ModelSafeGroupToString(group) << " worker.";
338 bool last_worker = false;
340 base::AutoLock al(lock_);
341 WorkerMap::iterator it = workers_.find(group);
342 CHECK(it != workers_.end());
343 stopped_workers_.push_back(it->second);
344 workers_.erase(it);
345 last_worker = workers_.empty();
348 if (last_worker) {
349 // Self-destruction after last worker.
350 DVLOG(1) << "Destroy registrar on loop of "
351 << ModelSafeGroupToString(group);
352 delete this;
356 scoped_ptr<base::Thread> SyncBackendRegistrar::ReleaseSyncThread() {
357 return sync_thread_.Pass();
360 void SyncBackendRegistrar::Shutdown() {
361 // All data types should have been deactivated by now.
362 DCHECK(processors_.empty());
364 // Unregister worker from observing loop destruction.
365 base::AutoLock al(lock_);
366 for (WorkerMap::iterator it = workers_.begin();
367 it != workers_.end(); ++it) {
368 it->second->UnregisterForLoopDestruction(
369 base::Bind(&SyncBackendRegistrar::OnWorkerUnregistrationDone,
370 base::Unretained(this)));
374 base::Thread* SyncBackendRegistrar::sync_thread() {
375 return sync_thread_.get();
378 } // namespace browser_sync