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

Side by Side Diff: chrome/browser/managed_mode/managed_user_service.cc

Issue 15780020: Setup Sync to use OAuth token for managed users. (Closed) Base URL: http://git.chromium.org/chromium/src.git@issue226464a
Patch Set: . Created 7 years, 6 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
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/managed_mode/managed_user_service.h" 5 #include "chrome/browser/managed_mode/managed_user_service.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/memory/ref_counted.h" 8 #include "base/memory/ref_counted.h"
9 #include "base/metrics/field_trial.h" 9 #include "base/metrics/field_trial.h"
10 #include "base/prefs/pref_service.h" 10 #include "base/prefs/pref_service.h"
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 BrowserThread::PostTask( 132 BrowserThread::PostTask(
133 BrowserThread::IO, 133 BrowserThread::IO,
134 FROM_HERE, 134 FROM_HERE,
135 base::Bind(&ManagedModeURLFilter::SetManualURLs, 135 base::Bind(&ManagedModeURLFilter::SetManualURLs,
136 io_url_filter_, base::Owned(url_map.release()))); 136 io_url_filter_, base::Owned(url_map.release())));
137 } 137 }
138 138
139 ManagedUserService::ManagedUserService(Profile* profile) 139 ManagedUserService::ManagedUserService(Profile* profile)
140 : weak_ptr_factory_(this), 140 : weak_ptr_factory_(this),
141 profile_(profile), 141 profile_(profile),
142 elevated_for_testing_(false) {} 142 waiting_for_sync_initialization_(false),
143 elevated_for_testing_(false) {
144 }
143 145
144 ManagedUserService::~ManagedUserService() {} 146 ManagedUserService::~ManagedUserService() {}
145 147
148 void ManagedUserService::Shutdown() {
149 ProfileSyncService* sync_service =
150 ProfileSyncServiceFactory::GetForProfile(profile_);
151 if (sync_service)
152 sync_service->RemoveObserver(this);
153 }
154
146 bool ManagedUserService::ProfileIsManaged() const { 155 bool ManagedUserService::ProfileIsManaged() const {
147 return ProfileIsManaged(profile_); 156 return ProfileIsManaged(profile_);
148 } 157 }
149 158
150 // static 159 // static
151 bool ManagedUserService::ProfileIsManaged(Profile* profile) { 160 bool ManagedUserService::ProfileIsManaged(Profile* profile) {
152 return profile->GetPrefs()->GetBoolean(prefs::kProfileIsManaged); 161 return profile->GetPrefs()->GetBoolean(prefs::kProfileIsManaged);
153 } 162 }
154 163
155 // static 164 // static
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 } 271 }
263 272
264 bool ManagedUserService::UserMayModifySettings( 273 bool ManagedUserService::UserMayModifySettings(
265 const extensions::Extension* extension, 274 const extensions::Extension* extension,
266 string16* error) const { 275 string16* error) const {
267 // |extension| can be NULL in unit tests. 276 // |extension| can be NULL in unit tests.
268 return ExtensionManagementPolicyImpl( 277 return ExtensionManagementPolicyImpl(
269 extension ? extension->id() : std::string(), error); 278 extension ? extension->id() : std::string(), error);
270 } 279 }
271 280
281 void ManagedUserService::OnStateChanged() {
282 ProfileSyncService* service =
283 ProfileSyncServiceFactory::GetForProfile(profile_);
284 if (waiting_for_sync_initialization_ && service->sync_initialized()) {
285 SetupSync();
286 service->RemoveObserver(this);
287 waiting_for_sync_initialization_ = false;
288 return;
289 }
290
291 DLOG_IF(ERROR, service->GetAuthError().state() ==
292 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)
293 << "Credentials rejected";
294 }
295
272 void ManagedUserService::Observe(int type, 296 void ManagedUserService::Observe(int type,
273 const content::NotificationSource& source, 297 const content::NotificationSource& source,
274 const content::NotificationDetails& details) { 298 const content::NotificationDetails& details) {
275 switch (type) { 299 switch (type) {
276 case chrome::NOTIFICATION_EXTENSION_LOADED: { 300 case chrome::NOTIFICATION_EXTENSION_LOADED: {
277 const extensions::Extension* extension = 301 const extensions::Extension* extension =
278 content::Details<extensions::Extension>(details).ptr(); 302 content::Details<extensions::Extension>(details).ptr();
279 if (!extensions::ManagedModeInfo::GetContentPackSiteList( 303 if (!extensions::ManagedModeInfo::GetContentPackSiteList(
280 extension).empty()) { 304 extension).empty()) {
281 UpdateSiteLists(); 305 UpdateSiteLists();
282 } 306 }
283 break; 307 break;
284 } 308 }
285 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { 309 case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
286 const extensions::UnloadedExtensionInfo* extension_info = 310 const extensions::UnloadedExtensionInfo* extension_info =
287 content::Details<extensions::UnloadedExtensionInfo>(details).ptr(); 311 content::Details<extensions::UnloadedExtensionInfo>(details).ptr();
288 if (!extensions::ManagedModeInfo::GetContentPackSiteList( 312 if (!extensions::ManagedModeInfo::GetContentPackSiteList(
289 extension_info->extension).empty()) { 313 extension_info->extension).empty()) {
290 UpdateSiteLists(); 314 UpdateSiteLists();
291 } 315 }
292 break; 316 break;
293 } 317 }
294 default: 318 default:
295 NOTREACHED(); 319 NOTREACHED();
296 } 320 }
297 } 321 }
298 322
323 void ManagedUserService::SetupSync() {
324 ProfileSyncService* service =
325 ProfileSyncServiceFactory::GetForProfile(profile_);
326 DCHECK(service->sync_initialized());
327
328 bool sync_everything = false;
329 syncer::ModelTypeSet synced_datatypes;
330 synced_datatypes.Put(syncer::MANAGED_USER_SETTINGS);
331 service->OnUserChoseDatatypes(sync_everything, synced_datatypes);
332
333 // Notify ProfileSyncService that we are done with configuration.
334 service->SetSetupInProgress(false);
335 service->SetSyncSetupCompleted();
336 }
337
299 bool ManagedUserService::ExtensionManagementPolicyImpl( 338 bool ManagedUserService::ExtensionManagementPolicyImpl(
300 const std::string& extension_id, 339 const std::string& extension_id,
301 string16* error) const { 340 string16* error) const {
302 if (!ProfileIsManaged()) 341 if (!ProfileIsManaged())
303 return true; 342 return true;
304 343
305 if (elevated_for_testing_) 344 if (elevated_for_testing_)
306 return true; 345 return true;
307 346
308 if (error) 347 if (error)
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 urls->push_back(url); 445 urls->push_back(url);
407 } 446 }
408 } 447 }
409 448
410 void ManagedUserService::InitForTesting() { 449 void ManagedUserService::InitForTesting() {
411 DCHECK(!profile_->GetPrefs()->GetBoolean(prefs::kProfileIsManaged)); 450 DCHECK(!profile_->GetPrefs()->GetBoolean(prefs::kProfileIsManaged));
412 profile_->GetPrefs()->SetBoolean(prefs::kProfileIsManaged, true); 451 profile_->GetPrefs()->SetBoolean(prefs::kProfileIsManaged, true);
413 Init(); 452 Init();
414 } 453 }
415 454
416 void ManagedUserService::InitSync(const std::string& sync_token) { 455 void ManagedUserService::InitSync(const std::string& refresh_token) {
417 ProfileSyncService* service = 456 ProfileSyncService* service =
418 ProfileSyncServiceFactory::GetForProfile(profile_); 457 ProfileSyncServiceFactory::GetForProfile(profile_);
419 DCHECK(!service->sync_initialized());
420 // Tell the sync service that setup is in progress so we don't start syncing 458 // Tell the sync service that setup is in progress so we don't start syncing
421 // until we've finished configuration. 459 // until we've finished configuration.
422 service->SetSetupInProgress(true); 460 service->SetSetupInProgress(true);
423 461
424 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); 462 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_);
425 token_service->AddAuthTokenManually(GaiaConstants::kSyncService, sync_token); 463 token_service->UpdateCredentialsWithOAuth2(
464 GaiaAuthConsumer::ClientOAuthResult(refresh_token, std::string(), 0));
426 465
427 bool sync_everything = false; 466 // Continue in SetupSync() once the Sync backend has been initialized.
428 syncer::ModelTypeSet synced_datatypes; 467 if (service->sync_initialized()) {
429 synced_datatypes.Put(syncer::MANAGED_USER_SETTINGS); 468 SetupSync();
430 service->OnUserChoseDatatypes(sync_everything, synced_datatypes); 469 } else {
431 470 ProfileSyncServiceFactory::GetForProfile(profile_)->AddObserver(this);
432 // Notify ProfileSyncService that we are done with configuration. 471 waiting_for_sync_initialization_ = true;
433 service->SetSetupInProgress(false); 472 }
434 service->SetSyncSetupCompleted();
435 } 473 }
436 474
437 // static 475 // static
438 const char* ManagedUserService::GetManagedUserPseudoEmail() { 476 const char* ManagedUserService::GetManagedUserPseudoEmail() {
439 return kManagedUserPseudoEmail; 477 return kManagedUserPseudoEmail;
440 } 478 }
441 479
442 void ManagedUserService::Init() { 480 void ManagedUserService::Init() {
443 ManagedModePolicyProvider* policy_provider = GetPolicyProvider(); 481 ManagedModePolicyProvider* policy_provider = GetPolicyProvider();
444 if (!ProfileIsManaged()) { 482 if (!ProfileIsManaged()) {
445 if (policy_provider) 483 if (policy_provider)
446 policy_provider->Clear(); 484 policy_provider->Clear();
447 485
448 return; 486 return;
449 } 487 }
450 488
451 CommandLine* command_line = CommandLine::ForCurrentProcess(); 489 CommandLine* command_line = CommandLine::ForCurrentProcess();
452 if (command_line->HasSwitch(switches::kManagedUserSyncToken)) { 490 if (command_line->HasSwitch(switches::kManagedUserSyncToken)) {
453 InitSync( 491 InitSync(
454 command_line->GetSwitchValueASCII(switches::kManagedUserSyncToken)); 492 command_line->GetSwitchValueASCII(switches::kManagedUserSyncToken));
455 } 493 }
456 494
495 // TokenService only loads tokens automatically if we're signed in, so we have
496 // to nudge it ourselves.
497 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_);
498 token_service->LoadTokensFromDB();
499
457 extensions::ExtensionSystem* extension_system = 500 extensions::ExtensionSystem* extension_system =
458 extensions::ExtensionSystem::Get(profile_); 501 extensions::ExtensionSystem::Get(profile_);
459 extensions::ManagementPolicy* management_policy = 502 extensions::ManagementPolicy* management_policy =
460 extension_system->management_policy(); 503 extension_system->management_policy();
461 if (management_policy) 504 if (management_policy)
462 extension_system->management_policy()->RegisterProvider(this); 505 extension_system->management_policy()->RegisterProvider(this);
463 506
464 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, 507 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
465 content::Source<Profile>(profile_)); 508 content::Source<Profile>(profile_));
466 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, 509 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 profile_->GetPrefs()->GetDictionary(prefs::kManagedModeManualURLs); 584 profile_->GetPrefs()->GetDictionary(prefs::kManagedModeManualURLs);
542 scoped_ptr<std::map<GURL, bool> > url_map(new std::map<GURL, bool>()); 585 scoped_ptr<std::map<GURL, bool> > url_map(new std::map<GURL, bool>());
543 for (DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { 586 for (DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) {
544 bool allow = false; 587 bool allow = false;
545 bool result = it.value().GetAsBoolean(&allow); 588 bool result = it.value().GetAsBoolean(&allow);
546 DCHECK(result); 589 DCHECK(result);
547 (*url_map)[GURL(it.key())] = allow; 590 (*url_map)[GURL(it.key())] = allow;
548 } 591 }
549 url_filter_context_.SetManualURLs(url_map.Pass()); 592 url_filter_context_.SetManualURLs(url_map.Pass());
550 } 593 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698