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
|
// Copyright (c) 2011 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 <algorithm>
#include <cstddef>
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/glue/browser_thread_model_worker.h"
#include "chrome/browser/sync/glue/change_processor.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 "content/public/browser/browser_thread.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(ModelSafeGroup group) {
switch (group) {
case GROUP_PASSIVE:
return false;
case GROUP_UI:
return BrowserThread::CurrentlyOn(BrowserThread::UI);
case GROUP_DB:
return BrowserThread::CurrentlyOn(BrowserThread::DB);
case GROUP_FILE:
return BrowserThread::CurrentlyOn(BrowserThread::FILE);
case GROUP_HISTORY:
// TODO(ncarter): How to determine this?
return true;
case GROUP_PASSWORD:
// TODO(ncarter): How to determine this?
return true;
case MODEL_SAFE_GROUP_COUNT:
default:
return false;
}
}
} // namespace
SyncBackendRegistrar::SyncBackendRegistrar(
const syncable::ModelTypeSet& initial_types,
const std::string& name, Profile* profile,
MessageLoop* sync_loop) :
name_(name),
profile_(profile),
sync_loop_(sync_loop),
ui_worker_(new UIModelWorker()),
stopped_on_ui_thread_(false) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
CHECK(profile_);
DCHECK(sync_loop_);
workers_[GROUP_DB] = new DatabaseModelWorker();
workers_[GROUP_FILE] = new FileModelWorker();
workers_[GROUP_UI] = ui_worker_;
workers_[GROUP_PASSIVE] = new ModelSafeWorker();
// Any datatypes that we want the syncer to pull down must be in the
// routing_info map. We set them to group passive, meaning that
// updates will be applied to sync, but not dispatched to the native
// models.
for (syncable::ModelTypeSet::const_iterator it = initial_types.begin();
it != initial_types.end(); ++it) {
routing_info_[*it] = GROUP_PASSIVE;
}
HistoryService* history_service = profile->GetHistoryService(
Profile::IMPLICIT_ACCESS);
if (history_service) {
workers_[GROUP_HISTORY] = new HistoryModelWorker(history_service);
} else {
LOG_IF(WARNING, initial_types.count(syncable::TYPED_URLS) > 0)
<< "History store disabled, cannot sync Omnibox History";
routing_info_.erase(syncable::TYPED_URLS);
}
PasswordStore* password_store =
profile->GetPasswordStore(Profile::IMPLICIT_ACCESS);
if (password_store) {
workers_[GROUP_PASSWORD] = new PasswordModelWorker(password_store);
} else {
LOG_IF(WARNING, initial_types.count(syncable::PASSWORDS) > 0)
<< "Password store not initialized, cannot sync passwords";
routing_info_.erase(syncable::PASSWORDS);
}
}
SyncBackendRegistrar::~SyncBackendRegistrar() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(stopped_on_ui_thread_);
}
bool SyncBackendRegistrar::IsNigoriEnabled() const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
base::AutoLock lock(lock_);
return routing_info_.find(syncable::NIGORI) != routing_info_.end();
}
syncable::ModelTypeSet SyncBackendRegistrar::ConfigureDataTypes(
const syncable::ModelTypeSet& types_to_add,
const syncable::ModelTypeSet& types_to_remove) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (DCHECK_IS_ON()) {
syncable::ModelTypeSet intersection;
std::set_intersection(types_to_add.begin(), types_to_add.end(),
types_to_remove.begin(), types_to_remove.end(),
std::inserter(intersection, intersection.end()));
DCHECK(intersection.empty());
}
syncable::ModelTypeSet filtered_types_to_add = types_to_add;
if (workers_.count(GROUP_HISTORY) == 0) {
LOG(WARNING) << "No history worker -- removing TYPED_URLS";
filtered_types_to_add.erase(syncable::TYPED_URLS);
}
if (workers_.count(GROUP_PASSWORD) == 0) {
LOG(WARNING) << "No password worker -- removing PASSWORDS";
filtered_types_to_add.erase(syncable::PASSWORDS);
}
base::AutoLock lock(lock_);
syncable::ModelTypeSet newly_added_types;
for (syncable::ModelTypeSet::const_iterator it =
filtered_types_to_add.begin();
it != filtered_types_to_add.end(); ++it) {
// Add a newly specified data type as GROUP_PASSIVE into the
// routing_info, if it does not already exist.
if (routing_info_.count(*it) == 0) {
routing_info_[*it] = GROUP_PASSIVE;
newly_added_types.insert(*it);
}
}
for (syncable::ModelTypeSet::const_iterator it = types_to_remove.begin();
it != types_to_remove.end(); ++it) {
routing_info_.erase(*it);
}
// TODO(akalin): Use SVLOG/SLOG if we add any more logging.
VLOG(1) << name_ << ": Adding types "
<< syncable::ModelTypeSetToString(types_to_add)
<< " (with newly-added types "
<< syncable::ModelTypeSetToString(newly_added_types)
<< ") and removing types "
<< syncable::ModelTypeSetToString(types_to_remove)
<< " to get new routing info "
<< ModelSafeRoutingInfoToString(routing_info_);
return newly_added_types;
}
void SyncBackendRegistrar::StopOnUIThread() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!stopped_on_ui_thread_);
ui_worker_->Stop();
stopped_on_ui_thread_ = true;
}
void SyncBackendRegistrar::OnSyncerShutdownComplete() {
DCHECK_EQ(MessageLoop::current(), sync_loop_);
ui_worker_->OnSyncerShutdownComplete();
}
void SyncBackendRegistrar::ActivateDataType(
syncable::ModelType type,
ModelSafeGroup group,
ChangeProcessor* change_processor,
sync_api::UserShare* user_share) {
CHECK(IsOnThreadForGroup(group));
base::AutoLock lock(lock_);
// Ensure that the given data type is in the PASSIVE group.
ModelSafeRoutingInfo::iterator i = routing_info_.find(type);
DCHECK(i != routing_info_.end());
DCHECK_EQ(i->second, GROUP_PASSIVE);
routing_info_[type] = group;
CHECK(IsCurrentThreadSafeForModel(type));
// 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(profile_, user_share);
DCHECK(GetProcessorUnsafe(type));
}
void SyncBackendRegistrar::DeactivateDataType(syncable::ModelType type) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
base::AutoLock lock(lock_);
ChangeProcessor* change_processor = GetProcessorUnsafe(type);
if (change_processor)
change_processor->Stop();
routing_info_.erase(type);
ignore_result(processors_.erase(type));
DCHECK(!GetProcessorUnsafe(type));
}
bool SyncBackendRegistrar::IsTypeActivatedForTest(
syncable::ModelType type) const {
return GetProcessor(type) != NULL;
}
void SyncBackendRegistrar::OnChangesApplied(
syncable::ModelType model_type,
const sync_api::BaseTransaction* trans,
const sync_api::ImmutableChangeRecordList& changes) {
ChangeProcessor* processor = GetProcessor(model_type);
if (!processor)
return;
processor->ApplyChangesFromSyncModel(trans, changes);
}
void SyncBackendRegistrar::OnChangesComplete(
syncable::ModelType model_type) {
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<ModelSafeWorker*>* out) {
base::AutoLock lock(lock_);
out->clear();
for (WorkerMap::const_iterator it = workers_.begin();
it != workers_.end(); ++it) {
out->push_back(it->second);
}
}
void SyncBackendRegistrar::GetModelSafeRoutingInfo(
ModelSafeRoutingInfo* out) {
base::AutoLock lock(lock_);
ModelSafeRoutingInfo copy(routing_info_);
out->swap(copy);
}
ChangeProcessor* SyncBackendRegistrar::GetProcessor(
syncable::ModelType type) const {
base::AutoLock lock(lock_);
ChangeProcessor* processor = GetProcessorUnsafe(type);
if (!processor)
return NULL;
// We can only check if |processor| exists, as otherwise the type is
// mapped to GROUP_PASSIVE.
CHECK(IsCurrentThreadSafeForModel(type));
CHECK(processor->IsRunning());
return processor;
}
ChangeProcessor* SyncBackendRegistrar::GetProcessorUnsafe(
syncable::ModelType type) const {
lock_.AssertAcquired();
std::map<syncable::ModelType, 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(
syncable::ModelType model_type) const {
lock_.AssertAcquired();
return IsOnThreadForGroup(GetGroupForModelType(model_type, routing_info_));
}
} // namespace browser_sync
|