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
|
// Copyright 2013 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/managed_mode/managed_user_registration_service.h"
#include "base/base64.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/prefs/pref_service.h"
#include "base/rand_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/managed_mode/managed_user_refresh_token_fetcher.h"
#include "chrome/browser/managed_mode/managed_user_service.h"
#include "chrome/browser/managed_mode/managed_user_service_factory.h"
#include "chrome/browser/prefs/scoped_user_pref_update.h"
#include "chrome/browser/sync/glue/device_info.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "components/user_prefs/pref_registry_syncable.h"
#include "google_apis/gaia/gaia_urls.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "sync/api/sync_change.h"
#include "sync/api/sync_error_factory.h"
#include "sync/protocol/sync.pb.h"
using base::DictionaryValue;
using syncer::MANAGED_USERS;
using syncer::ModelType;
using syncer::SyncChange;
using syncer::SyncChangeList;
using syncer::SyncChangeProcessor;
using syncer::SyncData;
using syncer::SyncDataList;
using syncer::SyncError;
using syncer::SyncErrorFactory;
using syncer::SyncMergeResult;
using sync_pb::ManagedUserSpecifics;
using user_prefs::PrefRegistrySyncable;
// How long to wait before canceling user registration. If this is changed, the
// histogram limits in the BrowserOptionsHandler should also be updated.
static const int kRegistrationTimeoutMS = 30 * 1000;
namespace {
const char kAcknowledged[] = "acknowledged";
const char kName[] = "name";
SyncData CreateLocalSyncData(const std::string& id,
const std::string& name,
bool acknowledged) {
::sync_pb::EntitySpecifics specifics;
specifics.mutable_managed_user()->set_id(id);
specifics.mutable_managed_user()->set_name(name);
if (acknowledged)
specifics.mutable_managed_user()->set_acknowledged(true);
return SyncData::CreateLocalData(id, name, specifics);
}
} // namespace
ManagedUserRegistrationService::ManagedUserRegistrationService(
PrefService* prefs,
scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher)
: weak_ptr_factory_(this),
prefs_(prefs),
token_fetcher_(token_fetcher.Pass()),
pending_managed_user_acknowledged_(false) {
pref_change_registrar_.Init(prefs);
pref_change_registrar_.Add(
prefs::kGoogleServicesLastUsername,
base::Bind(&ManagedUserRegistrationService::OnLastSignedInUsernameChange,
base::Unretained(this)));
}
ManagedUserRegistrationService::~ManagedUserRegistrationService() {
DCHECK(pending_managed_user_id_.empty());
DCHECK(callback_.is_null());
}
// static
void ManagedUserRegistrationService::RegisterUserPrefs(
PrefRegistrySyncable* registry) {
registry->RegisterDictionaryPref(prefs::kManagedUsers,
PrefRegistrySyncable::UNSYNCABLE_PREF);
}
void ManagedUserRegistrationService::Register(
const string16& name,
const RegistrationCallback& callback) {
DCHECK(pending_managed_user_id_.empty());
DCHECK(!registration_timer_.IsRunning());
callback_ = callback;
if (!CommandLine::ForCurrentProcess()->HasSwitch(
switches::kNoManagedUserRegistrationTimeout)) {
registration_timer_.Start(
FROM_HERE,
base::TimeDelta::FromMilliseconds(kRegistrationTimeoutMS),
base::Bind(
&ManagedUserRegistrationService::CancelPendingRegistrationImpl,
weak_ptr_factory_.GetWeakPtr(),
GoogleServiceAuthError(GoogleServiceAuthError::CONNECTION_FAILED)));
}
DictionaryPrefUpdate update(prefs_, prefs::kManagedUsers);
DictionaryValue* dict = update.Get();
DictionaryValue* value = new DictionaryValue;
value->SetString(kName, name);
std::string id_raw = base::RandBytesAsString(8);
bool success = base::Base64Encode(id_raw, &pending_managed_user_id_);
DCHECK(success);
DCHECK(!dict->HasKey(pending_managed_user_id_));
dict->SetWithoutPathExpansion(pending_managed_user_id_, value);
if (sync_processor_) {
// If we're already syncing, create a new change and upload it.
SyncChangeList change_list;
change_list.push_back(SyncChange(
FROM_HERE,
SyncChange::ACTION_ADD,
CreateLocalSyncData(
pending_managed_user_id_, base::UTF16ToUTF8(name), false)));
SyncError error =
sync_processor_->ProcessSyncChanges(FROM_HERE, change_list);
DCHECK(!error.IsSet()) << error.ToString();
}
browser_sync::DeviceInfo::CreateLocalDeviceInfo(
base::Bind(&ManagedUserRegistrationService::FetchToken,
weak_ptr_factory_.GetWeakPtr(), name));
}
void ManagedUserRegistrationService::CancelPendingRegistration() {
CancelPendingRegistrationImpl(
GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED));
}
void ManagedUserRegistrationService::Shutdown() {
CancelPendingRegistration();
}
SyncMergeResult ManagedUserRegistrationService::MergeDataAndStartSyncing(
ModelType type,
const SyncDataList& initial_sync_data,
scoped_ptr<SyncChangeProcessor> sync_processor,
scoped_ptr<SyncErrorFactory> error_handler) {
DCHECK_EQ(MANAGED_USERS, type);
sync_processor_ = sync_processor.Pass();
error_handler_ = error_handler.Pass();
SyncChangeList change_list;
SyncMergeResult result(MANAGED_USERS);
DictionaryPrefUpdate update(prefs_, prefs::kManagedUsers);
DictionaryValue* dict = update.Get();
result.set_num_items_before_association(dict->size());
std::set<std::string> seen_ids;
int num_items_added = 0;
int num_items_modified = 0;
for (SyncDataList::const_iterator it = initial_sync_data.begin();
it != initial_sync_data.end(); ++it) {
DCHECK_EQ(MANAGED_USERS, it->GetDataType());
const ManagedUserSpecifics& managed_user =
it->GetSpecifics().managed_user();
DictionaryValue* value = new DictionaryValue();
value->SetString(kName, managed_user.name());
DCHECK(managed_user.acknowledged());
value->SetBoolean(kAcknowledged, managed_user.acknowledged());
if (dict->HasKey(managed_user.id()))
num_items_modified++;
else
num_items_added++;
dict->SetWithoutPathExpansion(managed_user.id(), value);
seen_ids.insert(managed_user.id());
}
for (DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) {
if (seen_ids.find(it.key()) != seen_ids.end())
continue;
const DictionaryValue* dict = NULL;
bool success = it.value().GetAsDictionary(&dict);
DCHECK(success);
bool acknowledged = false;
dict->GetBoolean(kAcknowledged, &acknowledged);
std::string name;
dict->GetString(kName, &name);
DCHECK(!name.empty());
change_list.push_back(
SyncChange(FROM_HERE, SyncChange::ACTION_ADD,
CreateLocalSyncData(it.key(), name, acknowledged)));
}
result.set_error(sync_processor_->ProcessSyncChanges(FROM_HERE, change_list));
result.set_num_items_modified(num_items_modified);
result.set_num_items_added(num_items_added);
result.set_num_items_after_association(dict->size());
return result;
}
void ManagedUserRegistrationService::StopSyncing(ModelType type) {
DCHECK_EQ(MANAGED_USERS, type);
// Canceling a pending registration might result in changes in the Sync data,
// so we do it before resetting the |sync_processor|.
CancelPendingRegistration();
sync_processor_.reset();
error_handler_.reset();
}
SyncDataList ManagedUserRegistrationService::GetAllSyncData(
ModelType type) const {
SyncDataList data;
DictionaryPrefUpdate update(prefs_, prefs::kManagedUsers);
DictionaryValue* dict = update.Get();
for (DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) {
const DictionaryValue* dict = NULL;
bool success = it.value().GetAsDictionary(&dict);
DCHECK(success);
std::string name;
dict->GetString(kName, &name);
bool acknowledged = false;
dict->GetBoolean(kAcknowledged, &acknowledged);
data.push_back(CreateLocalSyncData(it.key(), name, acknowledged));
}
return data;
}
SyncError ManagedUserRegistrationService::ProcessSyncChanges(
const tracked_objects::Location& from_here,
const SyncChangeList& change_list) {
SyncError error;
DictionaryPrefUpdate update(prefs_, prefs::kManagedUsers);
DictionaryValue* dict = update.Get();
for (SyncChangeList::const_iterator it = change_list.begin();
it != change_list.end(); ++it) {
SyncData data = it->sync_data();
DCHECK_EQ(MANAGED_USERS, data.GetDataType());
const ManagedUserSpecifics& managed_user =
data.GetSpecifics().managed_user();
switch (it->change_type()) {
case SyncChange::ACTION_ADD:
case SyncChange::ACTION_UPDATE: {
// Every item we get from the server should be acknowledged.
DCHECK(managed_user.acknowledged());
const DictionaryValue* old_value = NULL;
dict->GetDictionaryWithoutPathExpansion(managed_user.id(), &old_value);
// For an update action, the managed user should already exist, for an
// add action, it should not.
DCHECK_EQ(
old_value ? SyncChange::ACTION_UPDATE : SyncChange::ACTION_ADD,
it->change_type());
// If the managed user switched from unacknowledged to acknowledged,
// we might need to continue with a registration.
if (!old_value->HasKey(kAcknowledged))
OnManagedUserAcknowledged(managed_user.id());
DictionaryValue* value = new DictionaryValue;
value->SetString(kName, managed_user.name());
value->SetBoolean(kAcknowledged, managed_user.acknowledged());
dict->SetWithoutPathExpansion(managed_user.id(), value);
break;
}
case SyncChange::ACTION_DELETE: {
DCHECK(dict->HasKey(managed_user.id())) << managed_user.id();
dict->RemoveWithoutPathExpansion(managed_user.id(), NULL);
break;
}
case SyncChange::ACTION_INVALID: {
NOTREACHED();
break;
}
}
}
return error;
}
void ManagedUserRegistrationService::OnLastSignedInUsernameChange() {
DCHECK(!sync_processor_);
// If the last signed in user changes, we clear all data, to avoid managed
// users from one custodian appearing in another one's profile.
prefs_->ClearPref(prefs::kManagedUsers);
}
void ManagedUserRegistrationService::OnManagedUserAcknowledged(
const std::string& managed_user_id) {
// |pending_managed_user_id_| might be empty if we get a late acknowledgement
// for a previous registration that was canceled.
if (pending_managed_user_id_.empty())
return;
DCHECK_EQ(pending_managed_user_id_, managed_user_id);
DCHECK(!pending_managed_user_acknowledged_);
pending_managed_user_acknowledged_ = true;
DispatchCallbackIfReady();
}
void ManagedUserRegistrationService::FetchToken(
const string16& name,
const browser_sync::DeviceInfo& device_info) {
token_fetcher_->Start(
pending_managed_user_id_, name, device_info.client_name(),
base::Bind(&ManagedUserRegistrationService::OnReceivedToken,
weak_ptr_factory_.GetWeakPtr()));
}
void ManagedUserRegistrationService::OnReceivedToken(
const GoogleServiceAuthError& error,
const std::string& token) {
if (error.state() != GoogleServiceAuthError::NONE) {
DispatchCallback(error);
return;
}
DCHECK(!token.empty());
pending_managed_user_token_ = token;
DispatchCallbackIfReady();
}
void ManagedUserRegistrationService::DispatchCallbackIfReady() {
if (!pending_managed_user_acknowledged_ ||
pending_managed_user_token_.empty()) {
return;
}
GoogleServiceAuthError error(GoogleServiceAuthError::NONE);
DispatchCallback(error);
}
void ManagedUserRegistrationService::CancelPendingRegistrationImpl(
const GoogleServiceAuthError& error) {
pending_managed_user_token_.clear();
DispatchCallback(error);
}
void ManagedUserRegistrationService::DispatchCallback(
const GoogleServiceAuthError& error) {
registration_timer_.Stop();
if (!callback_.is_null()) {
callback_.Run(error, pending_managed_user_token_);
callback_.Reset();
DCHECK(!pending_managed_user_id_.empty());
if (pending_managed_user_token_.empty()) {
// Remove the pending managed user if we weren't successful.
DCHECK_NE(GoogleServiceAuthError::NONE, error.state());
DictionaryPrefUpdate update(prefs_, prefs::kManagedUsers);
bool success =
update->RemoveWithoutPathExpansion(pending_managed_user_id_, NULL);
DCHECK(success);
if (sync_processor_) {
SyncChangeList change_list;
change_list.push_back(
SyncChange(FROM_HERE, SyncChange::ACTION_DELETE,
SyncData::CreateLocalDelete(pending_managed_user_id_,
MANAGED_USERS)));
SyncError sync_error =
sync_processor_->ProcessSyncChanges(FROM_HERE, change_list);
DCHECK(!sync_error.IsSet());
}
}
}
pending_managed_user_token_.clear();
pending_managed_user_id_.clear();
pending_managed_user_acknowledged_ = false;
}
|