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