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

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

Issue 316813002: Start managed_mode refactor. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remove DCHECKs Created 6 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 | Annotate | Revision Log
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/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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 #include "google_apis/gaia/google_service_auth_error.h" 49 #include "google_apis/gaia/google_service_auth_error.h"
50 #include "grit/generated_resources.h" 50 #include "grit/generated_resources.h"
51 #include "net/base/escape.h" 51 #include "net/base/escape.h"
52 #include "ui/base/l10n/l10n_util.h" 52 #include "ui/base/l10n/l10n_util.h"
53 53
54 #if defined(OS_CHROMEOS) 54 #if defined(OS_CHROMEOS)
55 #include "chrome/browser/chromeos/login/users/supervised_user_manager.h" 55 #include "chrome/browser/chromeos/login/users/supervised_user_manager.h"
56 #include "chrome/browser/chromeos/login/users/user_manager.h" 56 #include "chrome/browser/chromeos/login/users/user_manager.h"
57 #endif 57 #endif
58 58
59 #if defined(ENABLE_THEMES)
60 #include "chrome/browser/themes/theme_service.h"
61 #include "chrome/browser/themes/theme_service_factory.h"
62 #endif
63
59 using base::DictionaryValue; 64 using base::DictionaryValue;
60 using base::UserMetricsAction; 65 using base::UserMetricsAction;
61 using content::BrowserThread; 66 using content::BrowserThread;
62 67
63 ManagedUserService::URLFilterContext::URLFilterContext() 68 ManagedUserService::URLFilterContext::URLFilterContext()
64 : ui_url_filter_(new ManagedModeURLFilter), 69 : ui_url_filter_(new ManagedModeURLFilter),
65 io_url_filter_(new ManagedModeURLFilter) {} 70 io_url_filter_(new ManagedModeURLFilter) {}
66 ManagedUserService::URLFilterContext::~URLFilterContext() {} 71 ManagedUserService::URLFilterContext::~URLFilterContext() {}
67 72
68 ManagedModeURLFilter* 73 ManagedModeURLFilter*
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 ui_url_filter_->SetManualURLs(url_map.get()); 123 ui_url_filter_->SetManualURLs(url_map.get());
119 BrowserThread::PostTask( 124 BrowserThread::PostTask(
120 BrowserThread::IO, 125 BrowserThread::IO,
121 FROM_HERE, 126 FROM_HERE,
122 base::Bind(&ManagedModeURLFilter::SetManualURLs, 127 base::Bind(&ManagedModeURLFilter::SetManualURLs,
123 io_url_filter_, base::Owned(url_map.release()))); 128 io_url_filter_, base::Owned(url_map.release())));
124 } 129 }
125 130
126 ManagedUserService::ManagedUserService(Profile* profile) 131 ManagedUserService::ManagedUserService(Profile* profile)
127 : profile_(profile), 132 : profile_(profile),
133 active_(false),
134 delegate_(NULL),
128 extension_registry_observer_(this), 135 extension_registry_observer_(this),
129 waiting_for_sync_initialization_(false), 136 waiting_for_sync_initialization_(false),
130 is_profile_active_(false), 137 is_profile_active_(false),
131 elevated_for_testing_(false), 138 elevated_for_testing_(false),
132 did_shutdown_(false), 139 did_shutdown_(false),
133 waiting_for_permissions_(false), 140 waiting_for_permissions_(false),
134 weak_ptr_factory_(this) { 141 weak_ptr_factory_(this) {
135 } 142 }
136 143
137 ManagedUserService::~ManagedUserService() { 144 ManagedUserService::~ManagedUserService() {
138 DCHECK(did_shutdown_); 145 DCHECK(did_shutdown_);
139 } 146 }
140 147
141 void ManagedUserService::Shutdown() { 148 void ManagedUserService::Shutdown() {
142 did_shutdown_ = true; 149 did_shutdown_ = true;
143 if (ProfileIsManaged()) { 150 if (ProfileIsManaged()) {
144 content::RecordAction(UserMetricsAction("ManagedUsers_QuitBrowser")); 151 content::RecordAction(UserMetricsAction("ManagedUsers_QuitBrowser"));
145 #if !defined(OS_ANDROID)
146 // TODO(bauerb): Get rid of the platform-specific #ifdef here.
147 // http://crbug.com/313377
148 BrowserList::RemoveObserver(this);
149 #endif
150 } 152 }
151 153 SetActive(false);
152 if (!waiting_for_sync_initialization_)
153 return;
154
155 ProfileSyncService* sync_service =
156 ProfileSyncServiceFactory::GetForProfile(profile_);
157 sync_service->RemoveObserver(this);
158 } 154 }
159 155
160 bool ManagedUserService::ProfileIsManaged() const { 156 bool ManagedUserService::ProfileIsManaged() const {
161 return profile_->IsManaged(); 157 return profile_->IsManaged();
162 } 158 }
163 159
164 // static 160 // static
165 void ManagedUserService::RegisterProfilePrefs( 161 void ManagedUserService::RegisterProfilePrefs(
166 user_prefs::PrefRegistrySyncable* registry) { 162 user_prefs::PrefRegistrySyncable* registry) {
167 registry->RegisterDictionaryPref( 163 registry->RegisterDictionaryPref(
(...skipping 26 matching lines...) Expand all
194 if (!is_managed) 190 if (!is_managed)
195 return; 191 return;
196 192
197 std::string managed_user_id = prefs->GetString(prefs::kManagedUserId); 193 std::string managed_user_id = prefs->GetString(prefs::kManagedUserId);
198 if (!managed_user_id.empty()) 194 if (!managed_user_id.empty())
199 return; 195 return;
200 196
201 prefs->SetString(prefs::kManagedUserId, "Dummy ID"); 197 prefs->SetString(prefs::kManagedUserId, "Dummy ID");
202 } 198 }
203 199
200 void ManagedUserService::SetDelegate(Delegate* delegate) {
201 if (delegate_ == delegate)
202 return;
203 // If the delegate changed, deactivate first to give the old delegate a chance
204 // to clean up.
205 SetActive(false);
206 delegate_ = delegate;
207 }
208
204 scoped_refptr<const ManagedModeURLFilter> 209 scoped_refptr<const ManagedModeURLFilter>
205 ManagedUserService::GetURLFilterForIOThread() { 210 ManagedUserService::GetURLFilterForIOThread() {
206 return url_filter_context_.io_url_filter(); 211 return url_filter_context_.io_url_filter();
207 } 212 }
208 213
209 ManagedModeURLFilter* ManagedUserService::GetURLFilterForUIThread() { 214 ManagedModeURLFilter* ManagedUserService::GetURLFilterForUIThread() {
210 return url_filter_context_.ui_url_filter(); 215 return url_filter_context_.ui_url_filter();
211 } 216 }
212 217
213 // Items not on any list must return -1 (CATEGORY_NOT_ON_LIST in history.js). 218 // Items not on any list must return -1 (CATEGORY_NOT_ON_LIST in history.js).
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 } 407 }
403 } 408 }
404 409
405 return site_lists.Pass(); 410 return site_lists.Pass();
406 } 411 }
407 412
408 ManagedUserSettingsService* ManagedUserService::GetSettingsService() { 413 ManagedUserSettingsService* ManagedUserService::GetSettingsService() {
409 return ManagedUserSettingsServiceFactory::GetForProfile(profile_); 414 return ManagedUserSettingsServiceFactory::GetForProfile(profile_);
410 } 415 }
411 416
417 void ManagedUserService::OnManagedUserIdChanged() {
418 std::string managed_user_id =
419 profile_->GetPrefs()->GetString(prefs::kManagedUserId);
420 SetActive(!managed_user_id.empty());
421 }
422
412 void ManagedUserService::OnDefaultFilteringBehaviorChanged() { 423 void ManagedUserService::OnDefaultFilteringBehaviorChanged() {
413 DCHECK(ProfileIsManaged()); 424 DCHECK(ProfileIsManaged());
414 425
415 int behavior_value = profile_->GetPrefs()->GetInteger( 426 int behavior_value = profile_->GetPrefs()->GetInteger(
416 prefs::kDefaultManagedModeFilteringBehavior); 427 prefs::kDefaultManagedModeFilteringBehavior);
417 ManagedModeURLFilter::FilteringBehavior behavior = 428 ManagedModeURLFilter::FilteringBehavior behavior =
418 ManagedModeURLFilter::BehaviorFromInt(behavior_value); 429 ManagedModeURLFilter::BehaviorFromInt(behavior_value);
419 url_filter_context_.SetDefaultFilteringBehavior(behavior); 430 url_filter_context_.SetDefaultFilteringBehavior(behavior);
420 } 431 }
421 432
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
506 // Continue in SetupSync() once the Sync backend has been initialized. 517 // Continue in SetupSync() once the Sync backend has been initialized.
507 if (service->sync_initialized()) { 518 if (service->sync_initialized()) {
508 SetupSync(); 519 SetupSync();
509 } else { 520 } else {
510 ProfileSyncServiceFactory::GetForProfile(profile_)->AddObserver(this); 521 ProfileSyncServiceFactory::GetForProfile(profile_)->AddObserver(this);
511 waiting_for_sync_initialization_ = true; 522 waiting_for_sync_initialization_ = true;
512 } 523 }
513 } 524 }
514 525
515 void ManagedUserService::Init() { 526 void ManagedUserService::Init() {
516 ManagedUserSettingsService* settings_service = GetSettingsService(); 527 DCHECK(GetSettingsService()->IsReady());
517 DCHECK(settings_service->IsReady()); 528
518 if (!ProfileIsManaged()) { 529 pref_change_registrar_.Init(profile_->GetPrefs());
519 settings_service->Clear(); 530 pref_change_registrar_.Add(
531 prefs::kManagedUserId,
532 base::Bind(&ManagedUserService::OnManagedUserIdChanged,
533 base::Unretained(this)));
534
535 SetActive(ProfileIsManaged());
536 }
537
538 void ManagedUserService::SetActive(bool active) {
539 if (active_ == active)
520 return; 540 return;
541 active_ = active;
542
543 if (!delegate_ || !delegate_->SetActive(active_)) {
544 if (active_) {
545 SupervisedUserPrefMappingServiceFactory::GetForBrowserContext(profile_)
546 ->Init();
547
548 CommandLine* command_line = CommandLine::ForCurrentProcess();
549 if (command_line->HasSwitch(switches::kManagedUserSyncToken)) {
550 InitSync(
551 command_line->GetSwitchValueASCII(switches::kManagedUserSyncToken));
552 }
553
554 ProfileOAuth2TokenService* token_service =
555 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
556 token_service->LoadCredentials(managed_users::kManagedUserPseudoEmail);
557 }
521 } 558 }
522 559
523 settings_service->Activate(); 560 // Now activate/deactivate anything not handled by the delegate yet.
524 561
525 SupervisedUserPrefMappingServiceFactory::GetForBrowserContext(profile_) 562 #if defined(ENABLE_THEMES)
526 ->Init(); 563 // Re-set the default theme to turn the SU theme on/off.
564 ThemeService* theme_service = ThemeServiceFactory::GetForProfile(profile_);
565 if (theme_service->UsingDefaultTheme() || theme_service->UsingSystemTheme()) {
566 ThemeServiceFactory::GetForProfile(profile_)->UseDefaultTheme();
567 }
568 #endif
527 569
528 CommandLine* command_line = CommandLine::ForCurrentProcess(); 570 ManagedUserSettingsService* settings_service = GetSettingsService();
529 if (command_line->HasSwitch(switches::kManagedUserSyncToken)) { 571 settings_service->SetActive(active_);
530 InitSync(
531 command_line->GetSwitchValueASCII(switches::kManagedUserSyncToken));
532 }
533
534 ProfileOAuth2TokenService* token_service =
535 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
536 token_service->LoadCredentials(managed_users::kManagedUserPseudoEmail);
537 if (CommandLine::ForCurrentProcess()->HasSwitch(
538 switches::kPermissionRequestApiUrl)) {
539 permissions_creator_ =
540 PermissionRequestCreatorApiary::CreateWithProfile(profile_);
541 } else {
542 PrefService* pref_service = profile_->GetPrefs();
543 permissions_creator_.reset(new PermissionRequestCreatorSync(
544 settings_service,
545 ManagedUserSharedSettingsServiceFactory::GetForBrowserContext(profile_),
546 pref_service->GetString(prefs::kProfileName),
547 pref_service->GetString(prefs::kManagedUserId)));
548 }
549 572
550 extensions::ExtensionSystem* extension_system = 573 extensions::ExtensionSystem* extension_system =
551 extensions::ExtensionSystem::Get(profile_); 574 extensions::ExtensionSystem::Get(profile_);
552 extensions::ManagementPolicy* management_policy = 575 extensions::ManagementPolicy* management_policy =
553 extension_system->management_policy(); 576 extension_system->management_policy();
554 if (management_policy)
555 extension_system->management_policy()->RegisterProvider(this);
556 577
557 extension_registry_observer_.Add( 578 if (active_) {
558 extensions::ExtensionRegistry::Get(profile_)); 579 if (CommandLine::ForCurrentProcess()->HasSwitch(
580 switches::kPermissionRequestApiUrl)) {
581 permissions_creator_ =
582 PermissionRequestCreatorApiary::CreateWithProfile(profile_);
583 } else {
584 PrefService* pref_service = profile_->GetPrefs();
585 permissions_creator_.reset(new PermissionRequestCreatorSync(
586 settings_service,
587 ManagedUserSharedSettingsServiceFactory::GetForBrowserContext(
588 profile_),
589 pref_service->GetString(prefs::kProfileName),
590 pref_service->GetString(prefs::kManagedUserId)));
591 }
559 592
560 pref_change_registrar_.Init(profile_->GetPrefs()); 593 if (management_policy)
561 pref_change_registrar_.Add( 594 management_policy->RegisterProvider(this);
562 prefs::kDefaultManagedModeFilteringBehavior, 595
563 base::Bind(&ManagedUserService::OnDefaultFilteringBehaviorChanged, 596 extension_registry_observer_.Add(
564 base::Unretained(this))); 597 extensions::ExtensionRegistry::Get(profile_));
565 pref_change_registrar_.Add(prefs::kManagedModeManualHosts, 598
566 base::Bind(&ManagedUserService::UpdateManualHosts, 599 pref_change_registrar_.Add(
567 base::Unretained(this))); 600 prefs::kDefaultManagedModeFilteringBehavior,
568 pref_change_registrar_.Add(prefs::kManagedModeManualURLs, 601 base::Bind(&ManagedUserService::OnDefaultFilteringBehaviorChanged,
569 base::Bind(&ManagedUserService::UpdateManualURLs, 602 base::Unretained(this)));
570 base::Unretained(this))); 603 pref_change_registrar_.Add(prefs::kManagedModeManualHosts,
604 base::Bind(&ManagedUserService::UpdateManualHosts,
605 base::Unretained(this)));
606 pref_change_registrar_.Add(prefs::kManagedModeManualURLs,
607 base::Bind(&ManagedUserService::UpdateManualURLs,
608 base::Unretained(this)));
609
610 // Initialize the filter.
611 OnDefaultFilteringBehaviorChanged();
612 UpdateSiteLists();
613 UpdateManualHosts();
614 UpdateManualURLs();
571 615
572 #if !defined(OS_ANDROID) 616 #if !defined(OS_ANDROID)
573 // TODO(bauerb): Get rid of the platform-specific #ifdef here. 617 // TODO(bauerb): Get rid of the platform-specific #ifdef here.
574 // http://crbug.com/313377 618 // http://crbug.com/313377
575 BrowserList::AddObserver(this); 619 BrowserList::AddObserver(this);
576 #endif 620 #endif
621 } else {
622 permissions_creator_.reset();
577 623
578 // Initialize the filter. 624 if (management_policy)
579 OnDefaultFilteringBehaviorChanged(); 625 management_policy->UnregisterProvider(this);
580 UpdateSiteLists(); 626
581 UpdateManualHosts(); 627 extension_registry_observer_.RemoveAll();
582 UpdateManualURLs(); 628
629 pref_change_registrar_.Remove(prefs::kDefaultManagedModeFilteringBehavior);
630 pref_change_registrar_.Remove(prefs::kManagedModeManualHosts);
631 pref_change_registrar_.Remove(prefs::kManagedModeManualURLs);
632
633 if (waiting_for_sync_initialization_) {
634 ProfileSyncService* sync_service =
635 ProfileSyncServiceFactory::GetForProfile(profile_);
636 sync_service->RemoveObserver(this);
637 }
638
639 #if !defined(OS_ANDROID)
640 // TODO(bauerb): Get rid of the platform-specific #ifdef here.
641 // http://crbug.com/313377
642 BrowserList::RemoveObserver(this);
643 #endif
644 }
583 } 645 }
584 646
585 void ManagedUserService::RegisterAndInitSync( 647 void ManagedUserService::RegisterAndInitSync(
586 ManagedUserRegistrationUtility* registration_utility, 648 ManagedUserRegistrationUtility* registration_utility,
587 Profile* custodian_profile, 649 Profile* custodian_profile,
588 const std::string& managed_user_id, 650 const std::string& managed_user_id,
589 const AuthErrorCallback& callback) { 651 const AuthErrorCallback& callback) {
590 DCHECK(ProfileIsManaged()); 652 DCHECK(ProfileIsManaged());
591 DCHECK(!custodian_profile->IsManaged()); 653 DCHECK(!custodian_profile->IsManaged());
592 654
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 733
672 void ManagedUserService::OnBrowserSetLastActive(Browser* browser) { 734 void ManagedUserService::OnBrowserSetLastActive(Browser* browser) {
673 bool profile_became_active = profile_->IsSameProfile(browser->profile()); 735 bool profile_became_active = profile_->IsSameProfile(browser->profile());
674 if (!is_profile_active_ && profile_became_active) 736 if (!is_profile_active_ && profile_became_active)
675 content::RecordAction(UserMetricsAction("ManagedUsers_OpenProfile")); 737 content::RecordAction(UserMetricsAction("ManagedUsers_OpenProfile"));
676 else if (is_profile_active_ && !profile_became_active) 738 else if (is_profile_active_ && !profile_became_active)
677 content::RecordAction(UserMetricsAction("ManagedUsers_SwitchProfile")); 739 content::RecordAction(UserMetricsAction("ManagedUsers_SwitchProfile"));
678 740
679 is_profile_active_ = profile_became_active; 741 is_profile_active_ = profile_became_active;
680 } 742 }
OLDNEW
« no previous file with comments | « chrome/browser/managed_mode/managed_user_service.h ('k') | chrome/browser/managed_mode/managed_user_settings_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698