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