OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/supervised_user/supervised_user_service.h" | 5 #include "chrome/browser/supervised_user/supervised_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/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 SupervisedUserService::SupervisedUserService(Profile* profile) | 134 SupervisedUserService::SupervisedUserService(Profile* profile) |
135 : profile_(profile), | 135 : profile_(profile), |
136 active_(false), | 136 active_(false), |
137 delegate_(NULL), | 137 delegate_(NULL), |
138 #if defined(ENABLE_EXTENSIONS) | 138 #if defined(ENABLE_EXTENSIONS) |
139 extension_registry_observer_(this), | 139 extension_registry_observer_(this), |
140 #endif | 140 #endif |
141 waiting_for_sync_initialization_(false), | 141 waiting_for_sync_initialization_(false), |
142 is_profile_active_(false), | 142 is_profile_active_(false), |
143 elevated_for_testing_(false), | 143 elevated_for_testing_(false), |
| 144 did_init_(false), |
144 did_shutdown_(false), | 145 did_shutdown_(false), |
145 waiting_for_permissions_(false), | 146 waiting_for_permissions_(false), |
146 weak_ptr_factory_(this) { | 147 weak_ptr_factory_(this) { |
147 } | 148 } |
148 | 149 |
149 SupervisedUserService::~SupervisedUserService() { | 150 SupervisedUserService::~SupervisedUserService() { |
150 DCHECK(did_shutdown_); | 151 DCHECK(!did_init_ || did_shutdown_); |
151 } | 152 } |
152 | 153 |
153 void SupervisedUserService::Shutdown() { | 154 void SupervisedUserService::Shutdown() { |
| 155 if (!did_init_) |
| 156 return; |
| 157 DCHECK(!did_shutdown_); |
154 did_shutdown_ = true; | 158 did_shutdown_ = true; |
155 if (ProfileIsSupervised()) { | 159 if (ProfileIsSupervised()) { |
156 content::RecordAction(UserMetricsAction("ManagedUsers_QuitBrowser")); | 160 content::RecordAction(UserMetricsAction("ManagedUsers_QuitBrowser")); |
157 } | 161 } |
158 SetActive(false); | 162 SetActive(false); |
| 163 |
| 164 ProfileSyncService* sync_service = |
| 165 ProfileSyncServiceFactory::GetForProfile(profile_); |
| 166 // Can be null in tests. |
| 167 if (sync_service) |
| 168 sync_service->RemovePreferenceProvider(this); |
159 } | 169 } |
160 | 170 |
161 bool SupervisedUserService::ProfileIsSupervised() const { | 171 bool SupervisedUserService::ProfileIsSupervised() const { |
162 return profile_->IsSupervised(); | 172 return profile_->IsSupervised(); |
163 } | 173 } |
164 | 174 |
165 // static | 175 // static |
166 void SupervisedUserService::RegisterProfilePrefs( | 176 void SupervisedUserService::RegisterProfilePrefs( |
167 user_prefs::PrefRegistrySyncable* registry) { | 177 user_prefs::PrefRegistrySyncable* registry) { |
168 registry->RegisterDictionaryPref( | 178 registry->RegisterDictionaryPref( |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 content::BrowserContext* browser_context, | 338 content::BrowserContext* browser_context, |
329 const extensions::Extension* extension, | 339 const extensions::Extension* extension, |
330 extensions::UnloadedExtensionInfo::Reason reason) { | 340 extensions::UnloadedExtensionInfo::Reason reason) { |
331 if (!extensions::SupervisedUserInfo::GetContentPackSiteList(extension) | 341 if (!extensions::SupervisedUserInfo::GetContentPackSiteList(extension) |
332 .empty()) { | 342 .empty()) { |
333 UpdateSiteLists(); | 343 UpdateSiteLists(); |
334 } | 344 } |
335 } | 345 } |
336 #endif // defined(ENABLE_EXTENSIONS) | 346 #endif // defined(ENABLE_EXTENSIONS) |
337 | 347 |
| 348 syncer::ModelTypeSet SupervisedUserService::GetPreferredDataTypes() const { |
| 349 if (!ProfileIsSupervised()) |
| 350 return syncer::ModelTypeSet(); |
| 351 |
| 352 syncer::ModelTypeSet result; |
| 353 result.Put(syncer::SESSIONS); |
| 354 result.Put(syncer::EXTENSIONS); |
| 355 result.Put(syncer::EXTENSION_SETTINGS); |
| 356 result.Put(syncer::APPS); |
| 357 result.Put(syncer::APP_SETTINGS); |
| 358 result.Put(syncer::APP_NOTIFICATIONS); |
| 359 result.Put(syncer::APP_LIST); |
| 360 return result; |
| 361 } |
| 362 |
338 void SupervisedUserService::OnStateChanged() { | 363 void SupervisedUserService::OnStateChanged() { |
339 ProfileSyncService* service = | 364 ProfileSyncService* service = |
340 ProfileSyncServiceFactory::GetForProfile(profile_); | 365 ProfileSyncServiceFactory::GetForProfile(profile_); |
341 if (waiting_for_sync_initialization_ && service->sync_initialized()) { | 366 if (waiting_for_sync_initialization_ && service->sync_initialized()) { |
342 waiting_for_sync_initialization_ = false; | 367 waiting_for_sync_initialization_ = false; |
343 service->RemoveObserver(this); | 368 service->RemoveObserver(this); |
344 FinishSetupSync(); | 369 FinishSetupSync(); |
345 return; | 370 return; |
346 } | 371 } |
347 | 372 |
(...skipping 27 matching lines...) Expand all Loading... |
375 service->AddObserver(this); | 400 service->AddObserver(this); |
376 waiting_for_sync_initialization_ = true; | 401 waiting_for_sync_initialization_ = true; |
377 } | 402 } |
378 } | 403 } |
379 | 404 |
380 void SupervisedUserService::FinishSetupSync() { | 405 void SupervisedUserService::FinishSetupSync() { |
381 ProfileSyncService* service = | 406 ProfileSyncService* service = |
382 ProfileSyncServiceFactory::GetForProfile(profile_); | 407 ProfileSyncServiceFactory::GetForProfile(profile_); |
383 DCHECK(service->sync_initialized()); | 408 DCHECK(service->sync_initialized()); |
384 | 409 |
| 410 // Sync nothing (except types which are set via GetPreferredDataTypes). |
385 bool sync_everything = false; | 411 bool sync_everything = false; |
386 syncer::ModelTypeSet synced_datatypes; | 412 syncer::ModelTypeSet synced_datatypes; |
387 synced_datatypes.Put(syncer::SESSIONS); | |
388 synced_datatypes.Put(syncer::APPS); | |
389 synced_datatypes.Put(syncer::EXTENSIONS); | |
390 service->OnUserChoseDatatypes(sync_everything, synced_datatypes); | 413 service->OnUserChoseDatatypes(sync_everything, synced_datatypes); |
391 | 414 |
392 // Notify ProfileSyncService that we are done with configuration. | 415 // Notify ProfileSyncService that we are done with configuration. |
393 service->SetSetupInProgress(false); | 416 service->SetSetupInProgress(false); |
394 service->SetSyncSetupCompleted(); | 417 service->SetSyncSetupCompleted(); |
395 } | 418 } |
396 | 419 |
397 #if defined(ENABLE_EXTENSIONS) | 420 #if defined(ENABLE_EXTENSIONS) |
398 bool SupervisedUserService::ExtensionManagementPolicyImpl( | 421 bool SupervisedUserService::ExtensionManagementPolicyImpl( |
399 const extensions::Extension* extension, | 422 const extensions::Extension* extension, |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
560 | 583 |
561 ProfileOAuth2TokenService* token_service = | 584 ProfileOAuth2TokenService* token_service = |
562 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | 585 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); |
563 token_service->UpdateCredentials(supervised_users::kSupervisedUserPseudoEmail, | 586 token_service->UpdateCredentials(supervised_users::kSupervisedUserPseudoEmail, |
564 refresh_token); | 587 refresh_token); |
565 | 588 |
566 FinishSetupSyncWhenReady(); | 589 FinishSetupSyncWhenReady(); |
567 } | 590 } |
568 | 591 |
569 void SupervisedUserService::Init() { | 592 void SupervisedUserService::Init() { |
| 593 DCHECK(!did_init_); |
| 594 did_init_ = true; |
570 DCHECK(GetSettingsService()->IsReady()); | 595 DCHECK(GetSettingsService()->IsReady()); |
571 | 596 |
572 pref_change_registrar_.Init(profile_->GetPrefs()); | 597 pref_change_registrar_.Init(profile_->GetPrefs()); |
573 pref_change_registrar_.Add( | 598 pref_change_registrar_.Add( |
574 prefs::kSupervisedUserId, | 599 prefs::kSupervisedUserId, |
575 base::Bind(&SupervisedUserService::OnSupervisedUserIdChanged, | 600 base::Bind(&SupervisedUserService::OnSupervisedUserIdChanged, |
576 base::Unretained(this))); | 601 base::Unretained(this))); |
577 | 602 |
| 603 ProfileSyncService* sync_service = |
| 604 ProfileSyncServiceFactory::GetForProfile(profile_); |
| 605 // Can be null in tests. |
| 606 if (sync_service) |
| 607 sync_service->AddPreferenceProvider(this); |
| 608 |
578 SetActive(ProfileIsSupervised()); | 609 SetActive(ProfileIsSupervised()); |
579 } | 610 } |
580 | 611 |
581 void SupervisedUserService::SetActive(bool active) { | 612 void SupervisedUserService::SetActive(bool active) { |
582 if (active_ == active) | 613 if (active_ == active) |
583 return; | 614 return; |
584 active_ = active; | 615 active_ = active; |
585 | 616 |
586 if (!delegate_ || !delegate_->SetActive(active_)) { | 617 if (!delegate_ || !delegate_->SetActive(active_)) { |
587 if (active_) { | 618 if (active_) { |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
659 BrowserList::AddObserver(this); | 690 BrowserList::AddObserver(this); |
660 #endif | 691 #endif |
661 } else { | 692 } else { |
662 permissions_creator_.reset(); | 693 permissions_creator_.reset(); |
663 | 694 |
664 pref_change_registrar_.Remove( | 695 pref_change_registrar_.Remove( |
665 prefs::kDefaultSupervisedUserFilteringBehavior); | 696 prefs::kDefaultSupervisedUserFilteringBehavior); |
666 pref_change_registrar_.Remove(prefs::kSupervisedUserManualHosts); | 697 pref_change_registrar_.Remove(prefs::kSupervisedUserManualHosts); |
667 pref_change_registrar_.Remove(prefs::kSupervisedUserManualURLs); | 698 pref_change_registrar_.Remove(prefs::kSupervisedUserManualURLs); |
668 | 699 |
669 if (waiting_for_sync_initialization_) { | 700 if (waiting_for_sync_initialization_) |
670 ProfileSyncService* sync_service = | 701 ProfileSyncServiceFactory::GetForProfile(profile_)->RemoveObserver(this); |
671 ProfileSyncServiceFactory::GetForProfile(profile_); | |
672 sync_service->RemoveObserver(this); | |
673 } | |
674 | 702 |
675 #if !defined(OS_ANDROID) | 703 #if !defined(OS_ANDROID) |
676 // TODO(bauerb): Get rid of the platform-specific #ifdef here. | 704 // TODO(bauerb): Get rid of the platform-specific #ifdef here. |
677 // http://crbug.com/313377 | 705 // http://crbug.com/313377 |
678 BrowserList::RemoveObserver(this); | 706 BrowserList::RemoveObserver(this); |
679 #endif | 707 #endif |
680 } | 708 } |
681 } | 709 } |
682 | 710 |
683 void SupervisedUserService::RegisterAndInitSync( | 711 void SupervisedUserService::RegisterAndInitSync( |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
782 // The active user can be NULL in unit tests. | 810 // The active user can be NULL in unit tests. |
783 if (chromeos::UserManager::Get()->GetActiveUser()) { | 811 if (chromeos::UserManager::Get()->GetActiveUser()) { |
784 return UTF16ToUTF8(chromeos::UserManager::Get()->GetUserDisplayName( | 812 return UTF16ToUTF8(chromeos::UserManager::Get()->GetUserDisplayName( |
785 chromeos::UserManager::Get()->GetActiveUser()->GetUserID())); | 813 chromeos::UserManager::Get()->GetActiveUser()->GetUserID())); |
786 } | 814 } |
787 return std::string(); | 815 return std::string(); |
788 #else | 816 #else |
789 return profile_->GetPrefs()->GetString(prefs::kProfileName); | 817 return profile_->GetPrefs()->GetString(prefs::kProfileName); |
790 #endif | 818 #endif |
791 } | 819 } |
OLD | NEW |