Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(687)

Side by Side Diff: chrome/browser/sync/credential_cache_service_win.cc

Issue 10656033: [sync] Automatic bootstrapping of Sync on Win 8 from cached credentials (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fully decouple PSS and CCS. Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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/sync/credential_cache_service_win.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/base64.h"
10 #include "base/compiler_specific.h"
11 #include "base/file_util.h"
12 #include "base/values.h"
13 #include "base/win/windows_version.h"
14 #include "chrome/browser/prefs/pref_service.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/browser/signin/signin_manager.h"
18 #include "chrome/browser/signin/token_service.h"
19 #include "chrome/browser/signin/token_service_factory.h"
20 #include "chrome/browser/sync/glue/chrome_encryptor.h"
21 #include "chrome/browser/sync/profile_sync_service.h"
22 #include "chrome/browser/sync/profile_sync_service_factory.h"
23 #include "chrome/browser/sync/sync_prefs.h"
24 #include "chrome/common/chrome_constants.h"
25 #include "chrome/common/chrome_notification_types.h"
26 #include "chrome/common/chrome_paths_internal.h"
27 #include "chrome/common/net/gaia/gaia_auth_consumer.h"
28 #include "chrome/common/net/gaia/gaia_constants.h"
29 #include "chrome/common/pref_names.h"
30 #include "content/public/browser/browser_thread.h"
31 #include "content/public/browser/notification_details.h"
32 #include "content/public/browser/notification_source.h"
33 #include "sync/internal_api/public/base/model_type.h"
34
35 namespace syncer {
36
37 using content::BrowserThread;
38
39 CredentialCacheService::CredentialCacheService(Profile* profile)
40 : profile_(profile),
41 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
42 // |profile_| is null for unit tests.
43 if (profile_) {
44 if (ShouldInitializeLocalCredentialCacheWriter())
45 InitializeLocalCredentialCacheWriter();
46 if (ShouldLookForCachedCredentialsInAlternateProfile())
47 LookForCachedCredentialsInAlternateProfile();
48 }
49 }
50
51 CredentialCacheService::~CredentialCacheService() {
52 Shutdown();
53 }
54
55 void CredentialCacheService::Shutdown() {
56 if (local_store_.get()) {
57 local_store_.release();
58 }
59
60 if (alternate_store_.get()) {
61 alternate_store_->RemoveObserver(this);
62 alternate_store_.release();
63 }
64 }
65
66 void CredentialCacheService::OnInitializationCompleted(bool succeeded) {
67 DCHECK(succeeded);
68 // When the alternate credential store becomes available, begin consuming its
69 // cached credentials.
70 if (alternate_store_.get() && alternate_store_->IsInitializationComplete()) {
71 ReadCachedCredentialsFromAlternateProfile();
72 }
73 }
74
75 void CredentialCacheService::OnPrefValueChanged(const std::string& key) {
76 // Nothing to do here, since credentials are cached silently.
77 }
78
79 void CredentialCacheService::Observe(
80 int type,
81 const content::NotificationSource& source,
82 const content::NotificationDetails& details) {
83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
84 DCHECK(local_store_.get());
85 switch (type) {
86 case chrome::NOTIFICATION_PREF_CHANGED: {
87 const std::string pref_name =
88 *(content::Details<const std::string>(details).ptr());
89 if (pref_name == prefs::kGoogleServicesUsername ||
90 pref_name == prefs::kSyncEncryptionBootstrapToken) {
91 PackAndUpdateStringPref(
92 pref_name,
93 profile_->GetPrefs()->GetString(pref_name.c_str()));
94 } else {
95 UpdateBooleanPref(pref_name,
96 profile_->GetPrefs()->GetBoolean(pref_name.c_str()));
97 }
98 break;
99 }
100
101 case chrome::NOTIFICATION_TOKEN_SERVICE_CREDENTIALS_UPDATED: {
102 const TokenService::CredentialsUpdatedDetails& token_details =
103 *(content::Details<const TokenService::CredentialsUpdatedDetails>(
104 details).ptr());
105 PackAndUpdateStringPref(GaiaConstants::kGaiaLsid, token_details.lsid());
106 PackAndUpdateStringPref(GaiaConstants::kGaiaSid, token_details.sid());
107 break;
108 }
109
110 case chrome::NOTIFICATION_TOKENS_CLEARED: {
111 PackAndUpdateStringPref(GaiaConstants::kGaiaLsid, std::string());
112 PackAndUpdateStringPref(GaiaConstants::kGaiaSid, std::string());
113 break;
114 }
115
116 default: {
117 NOTREACHED();
118 break;
119 }
120 }
121 }
122
123 bool CredentialCacheService::HasPref(scoped_refptr<JsonPrefStore> store,
124 const std::string& pref_name) {
125 return (store->GetValue(pref_name, NULL) == PrefStore::READ_OK);
126 }
127
128 // static
129 base::StringValue* CredentialCacheService::PackCredential(
130 const std::string& credential) {
131 // Do nothing for empty credentials.
132 if (credential.empty())
133 return base::Value::CreateStringValue("");
134
135 browser_sync::ChromeEncryptor encryptor;
136 std::string encrypted;
137 if (!encryptor.EncryptString(credential, &encrypted)) {
138 NOTREACHED();
139 return base::Value::CreateStringValue(std::string());
140 }
141
142 std::string encoded;
143 if (!base::Base64Encode(encrypted, &encoded)) {
144 NOTREACHED();
145 return base::Value::CreateStringValue(std::string());
146 }
147
148 return base::Value::CreateStringValue(encoded);
149 }
150
151 // static
152 std::string CredentialCacheService::UnpackCredential(
153 const base::Value& packed) {
154 std::string encoded;
155 if (!packed.GetAsString(&encoded)) {
156 NOTREACHED();
157 return std::string();
158 }
159
160 // Do nothing for empty credentials.
161 if (encoded.empty())
162 return std::string();
163
164 std::string encrypted;
165 if (!base::Base64Decode(encoded, &encrypted)) {
166 NOTREACHED();
167 return std::string();
168 }
169
170 browser_sync::ChromeEncryptor encryptor;
171 std::string unencrypted;
172 if (!encryptor.DecryptString(encrypted, &unencrypted)) {
173 NOTREACHED();
174 return std::string();
175 }
176
177 return unencrypted;
178 }
179
180 void CredentialCacheService::PackAndUpdateStringPref(
181 const std::string& pref_name,
182 const std::string& new_value) {
183 DCHECK(local_store_.get());
184 if (!HasUserSignedOut()) {
185 local_store_->SetValueSilently(pref_name, PackCredential(new_value));
186 } else {
187 // Write a blank value since we cache credentials only for first-time
188 // sign-ins.
189 local_store_->SetValueSilently(pref_name, PackCredential(std::string()));
190 }
191 }
192
193 void CredentialCacheService::UpdateBooleanPref(const std::string& pref_name,
194 bool new_value) {
195 DCHECK(local_store_.get());
196 if (!HasUserSignedOut()) {
197 local_store_->SetValueSilently(pref_name,
198 base::Value::CreateBooleanValue(new_value));
199 } else {
200 // Write a default value of false since we cache credentials only for
201 // first-time sign-ins.
202 local_store_->SetValueSilently(pref_name,
203 base::Value::CreateBooleanValue(false));
204 }
205 }
206
207 std::string CredentialCacheService::GetAndUnpackStringPref(
208 scoped_refptr<JsonPrefStore> store,
209 const std::string& pref_name) {
210 const base::Value* pref_value = NULL;
211 store->GetValue(pref_name, &pref_value);
212 return UnpackCredential(*pref_value);
213 }
214
215 bool CredentialCacheService::GetBooleanPref(
216 scoped_refptr<JsonPrefStore> store,
217 const std::string& pref_name) {
218 const base::Value* pref_value = NULL;
219 store->GetValue(pref_name, &pref_value);
220 bool pref;
221 pref_value->GetAsBoolean(&pref);
222 return pref;
223 }
224
225 bool CredentialCacheService::IsDefaultProfile() const {
226 DCHECK(profile_);
227 FilePath default_user_data_dir;
228 chrome::GetDefaultUserDataDirectory(&default_user_data_dir);
229 return profile_->GetPath() ==
230 ProfileManager::GetDefaultProfileDir(default_user_data_dir);
231 }
232
233 FilePath CredentialCacheService::GetCredentialPathInCurrentProfile() const {
234 // The sync credential path in the default Desktop profile is
235 // "%Appdata%\Local\Google\Chrome\User Data\Default\Sync Credentials", while
236 // the sync credential path in the default Metro profile is
237 // "%Appdata%\Local\Google\Chrome\Metro\User Data\Default\Sync Credentials".
238 DCHECK(profile_);
239 return profile_->GetPath().Append(chrome::kSyncCredentialsFilename);
240 }
241
242 FilePath CredentialCacheService::GetCredentialPathInAlternateProfile() const {
243 DCHECK(profile_);
244 FilePath alternate_user_data_dir;
245 chrome::GetAlternateUserDataDirectory(&alternate_user_data_dir);
246 FilePath alternate_default_profile_dir =
247 ProfileManager::GetDefaultProfileDir(alternate_user_data_dir);
248 return alternate_default_profile_dir.Append(chrome::kSyncCredentialsFilename);
249 }
250
251 bool CredentialCacheService::ShouldInitializeLocalCredentialCacheWriter()
252 const {
253 // The local credential cache writer must be initialized iff:
254 // 1) Chrome is running on Windows 8.
255 // 2) We are running in the default profile.
256 DCHECK(profile_);
257 return base::win::GetVersion() >= base::win::VERSION_WIN8 &&
258 IsDefaultProfile();
Andrew T Wilson (Slow) 2012/07/24 20:55:29 So, this is fine, but another way to do it would b
Raghu Simha 2012/07/24 22:17:35 Done. (Unnecessary methods removed).
259 }
260
261 bool CredentialCacheService::ShouldLookForCachedCredentialsInAlternateProfile()
262 const {
263 // We must look for credentials in the alternate profile iff, in addition to
264 // the conditions in ShouldInitializeLocalCredentialCacheWriter(), the
265 // following are true:
266 // 1) Sync is not disabled by policy.
267 // 2) Sync startup is not suppressed.
268 // 3) No user is currently signed in to sync.
269 DCHECK(profile_);
270 PrefService* prefs = profile_->GetPrefs();
271 DCHECK(prefs);
272 return ShouldInitializeLocalCredentialCacheWriter() &&
273 !prefs->GetBoolean(prefs::kSyncManaged) &&
274 !prefs->GetBoolean(prefs::kSyncSuppressStart) &&
275 prefs->GetString(prefs::kGoogleServicesUsername).empty();
276 }
277
278 void CredentialCacheService::InitializeLocalCredentialCacheWriter() {
279 DCHECK(IsDefaultProfile());
280 local_store_ = new JsonPrefStore(
281 GetCredentialPathInCurrentProfile(),
282 content::BrowserThread::GetMessageLoopProxyForThread(
283 content::BrowserThread::FILE));
284 local_store_->ReadPrefsAsync(NULL);
285
286 // Register for notifications for updates to the sync credentials, which are
287 // stored in the PrefStore.
288 pref_registrar_.Init(profile_->GetPrefs());
289 pref_registrar_.Add(prefs::kSyncEncryptionBootstrapToken, this);
290 pref_registrar_.Add(prefs::kGoogleServicesUsername, this);
291 pref_registrar_.Add(prefs::kSyncKeepEverythingSynced, this);
292 for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) {
293 if (i == NIGORI) // The NIGORI preference is not persisted.
294 continue;
295 pref_registrar_.Add(
296 browser_sync::SyncPrefs::GetPrefNameForDataType(ModelTypeFromInt(i)),
297 this);
298 }
299
300 // Register for notifications for updates to lsid and sid, which are stored in
301 // the TokenService.
302 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_);
303 registrar_.Add(this,
304 chrome::NOTIFICATION_TOKEN_SERVICE_CREDENTIALS_UPDATED,
305 content::Source<TokenService>(token_service));
306 registrar_.Add(this,
307 chrome::NOTIFICATION_TOKENS_CLEARED,
308 content::Source<TokenService>(token_service));
309 }
310
311 void CredentialCacheService::InitializeAlternateCredentialCacheReader(
312 bool* should_initialize) {
313 // If |should_initialize| is false, there was no credential cache in the
314 // alternate profile directory, and there is nothing to do.
315 // TODO(rsimha): Add a polling mechanism that periodically examines the
316 // credential file in the alternate profile directory so we can respond to the
317 // user signing in and signing out.
318 DCHECK(should_initialize);
319 if (!*should_initialize)
320 return;
321 alternate_store_ = new JsonPrefStore(
322 GetCredentialPathInAlternateProfile(),
323 content::BrowserThread::GetMessageLoopProxyForThread(
324 content::BrowserThread::FILE));
325 alternate_store_->AddObserver(this);
326 alternate_store_->ReadPrefsAsync(NULL);
327 }
328
329 bool CredentialCacheService::HasUserSignedOut() {
330 DCHECK(local_store_.get());
331 // If HasPref() is false, the user never signed in, since there are no
332 // previously cached credentials. If the kGoogleServicesUsername pref is
333 // empty, it means that the user signed in and subsequently signed out.
334 return HasPref(local_store_, prefs::kGoogleServicesUsername) &&
335 GetAndUnpackStringPref(local_store_,
336 prefs::kGoogleServicesUsername).empty();
337 }
338
339 namespace {
340
341 // Determines if credentials should be read from the alternate profile based
342 // on the existence of the local and alternate credential files. Returns
343 // true via |result| if there is a credential cache file in the alternate
344 // profile, but there isn't one in the local profile. Returns false otherwise.
345 void ShouldReadFromAlternateCache(
346 const FilePath& credential_path_in_current_profile,
347 const FilePath& credential_path_in_alternate_profile,
348 bool* result) {
349 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
350 DCHECK(result);
351 *result = !file_util::PathExists(credential_path_in_current_profile) &&
352 file_util::PathExists(credential_path_in_alternate_profile);
353 }
354
355 } // namespace
356
357 void CredentialCacheService::LookForCachedCredentialsInAlternateProfile() {
358 DCHECK(IsDefaultProfile());
359 bool* should_initialize = new bool(false);
360 content::BrowserThread::PostTaskAndReply(
361 content::BrowserThread::FILE,
362 FROM_HERE,
363 base::Bind(&ShouldReadFromAlternateCache,
364 GetCredentialPathInCurrentProfile(),
365 GetCredentialPathInAlternateProfile(),
366 should_initialize),
367 base::Bind(
368 &CredentialCacheService::InitializeAlternateCredentialCacheReader,
369 weak_factory_.GetWeakPtr(),
370 base::Owned(should_initialize)));
371 }
372
373 void CredentialCacheService::ReadCachedCredentialsFromAlternateProfile() {
374 DCHECK(alternate_store_.get());
375 if (!HasPref(alternate_store_, prefs::kGoogleServicesUsername) ||
376 !HasPref(alternate_store_, GaiaConstants::kGaiaLsid) ||
377 !HasPref(alternate_store_, GaiaConstants::kGaiaSid) ||
378 !HasPref(alternate_store_, prefs::kSyncEncryptionBootstrapToken) ||
379 !HasPref(alternate_store_, prefs::kSyncKeepEverythingSynced)) {
380 VLOG(1) << "Could not find cached credentials.";
381 return;
382 }
383
384 std::string google_services_username =
385 GetAndUnpackStringPref(alternate_store_, prefs::kGoogleServicesUsername);
386 std::string lsid =
387 GetAndUnpackStringPref(alternate_store_, GaiaConstants::kGaiaLsid);
388 std::string sid =
389 GetAndUnpackStringPref(alternate_store_, GaiaConstants::kGaiaSid);
390 std::string encryption_bootstrap_token =
391 GetAndUnpackStringPref(alternate_store_,
392 prefs::kSyncEncryptionBootstrapToken);
393 bool keep_everything_synced =
394 GetBooleanPref(alternate_store_, prefs::kSyncKeepEverythingSynced);
395
396 if (google_services_username.empty() ||
397 lsid.empty() ||
398 sid.empty() ||
399 encryption_bootstrap_token.empty()) {
400 VLOG(1) << "Found empty cached credentials.";
401 return;
402 }
403
404 bool datatype_prefs[MODEL_TYPE_COUNT] = { false };
405 for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) {
406 if (i == NIGORI) // The NIGORI preference is not persisted.
407 continue;
408 std::string datatype_pref_name =
409 browser_sync::SyncPrefs::GetPrefNameForDataType(ModelTypeFromInt(i));
410 if (!HasPref(alternate_store_, datatype_pref_name)) {
411 VLOG(1) << "Could not find cached datatype prefs.";
412 return;
413 }
414 datatype_prefs[i] = GetBooleanPref(alternate_store_, datatype_pref_name);
415 }
416
417 ApplyCachedCredentials(google_services_username,
418 lsid,
419 sid,
420 encryption_bootstrap_token,
421 keep_everything_synced,
422 datatype_prefs);
423 }
424
425 void CredentialCacheService::ApplyCachedCredentials(
426 const std::string& google_services_username,
427 const std::string& lsid,
428 const std::string& sid,
429 const std::string& encryption_bootstrap_token,
430 bool keep_everything_synced,
431 const bool datatype_prefs[]) {
432 // Update the google username in the SigninManager and PrefStore.
433 ProfileSyncService* service =
434 ProfileSyncServiceFactory::GetForProfile(profile_);
435 service->signin()->SetAuthenticatedUsername(google_services_username);
436 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
437 google_services_username);
438
439 // Update the sync preferences using a SyncPrefs object.
440 browser_sync::SyncPrefs sync_prefs(profile_->GetPrefs());
441 sync_prefs.SetStartSuppressed(false);
442 sync_prefs.SetSyncSetupCompleted();
443 sync_prefs.SetEncryptionBootstrapToken(encryption_bootstrap_token);
444 sync_prefs.SetKeepEverythingSynced(keep_everything_synced);
445 syncer::ModelTypeSet registered_types;
446 syncer::ModelTypeSet preferred_types;
447 for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) {
448 if (i == NIGORI) // The NIGORI preference is not persisted.
449 continue;
450 registered_types.Put(ModelTypeFromInt(i));
451 if (datatype_prefs[i])
452 preferred_types.Put(ModelTypeFromInt(i));
453 }
454 sync_prefs.SetPreferredDataTypes(registered_types, preferred_types);
455
456 // Update the lsid and sid in the TokenService and mint new tokens for all
457 // Chrome services.
458 GaiaAuthConsumer::ClientLoginResult login_result;
459 login_result.lsid = lsid;
460 login_result.sid = sid;
461 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_);
462 token_service->UpdateCredentials(login_result);
463 DCHECK(token_service->AreCredentialsValid());
464 token_service->StartFetchingTokens();
465 }
466
467 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698