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
3 // found in the LICENSE file.
5 #include "chrome/browser/supervised_user/supervised_user_service.h"
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/component_updater/supervised_user_whitelist_installer.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/profiles/profile_info_cache.h"
16 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
18 #include "chrome/browser/signin/signin_manager_factory.h"
19 #include "chrome/browser/supervised_user/experimental/supervised_user_blacklist_downloader.h"
20 #include "chrome/browser/supervised_user/legacy/custodian_profile_downloader_service.h"
21 #include "chrome/browser/supervised_user/legacy/custodian_profile_downloader_service_factory.h"
22 #include "chrome/browser/supervised_user/legacy/permission_request_creator_sync.h"
23 #include "chrome/browser/supervised_user/legacy/supervised_user_pref_mapping_service.h"
24 #include "chrome/browser/supervised_user/legacy/supervised_user_pref_mapping_service_factory.h"
25 #include "chrome/browser/supervised_user/legacy/supervised_user_registration_utility.h"
26 #include "chrome/browser/supervised_user/legacy/supervised_user_shared_settings_service_factory.h"
27 #include "chrome/browser/supervised_user/supervised_user_constants.h"
28 #include "chrome/browser/supervised_user/supervised_user_service_observer.h"
29 #include "chrome/browser/supervised_user/supervised_user_settings_service.h"
30 #include "chrome/browser/supervised_user/supervised_user_settings_service_factory.h"
31 #include "chrome/browser/supervised_user/supervised_user_site_list.h"
32 #include "chrome/browser/supervised_user/supervised_user_whitelist_service.h"
33 #include "chrome/browser/sync/profile_sync_service.h"
34 #include "chrome/browser/sync/profile_sync_service_factory.h"
35 #include "chrome/browser/ui/browser.h"
36 #include "chrome/browser/ui/browser_list.h"
37 #include "chrome/common/chrome_switches.h"
38 #include "chrome/common/pref_names.h"
39 #include "chrome/grit/generated_resources.h"
40 #include "components/pref_registry/pref_registry_syncable.h"
41 #include "components/signin/core/browser/profile_oauth2_token_service.h"
42 #include "components/signin/core/browser/signin_manager.h"
43 #include "components/signin/core/browser/signin_manager_base.h"
44 #include "content/public/browser/browser_thread.h"
45 #include "content/public/browser/user_metrics.h"
46 #include "ui/base/l10n/l10n_util.h"
48 #if defined(OS_CHROMEOS)
49 #include "chrome/browser/chromeos/login/users/chrome_user_manager.h"
50 #include "chrome/browser/chromeos/login/users/supervised_user_manager.h"
51 #include "components/user_manager/user_manager.h"
54 #if defined(ENABLE_EXTENSIONS)
55 #include "extensions/browser/extension_registry.h"
56 #include "extensions/browser/extension_system.h"
59 #if defined(ENABLE_THEMES)
60 #include "chrome/browser/themes/theme_service.h"
61 #include "chrome/browser/themes/theme_service_factory.h"
64 using base::DictionaryValue
;
65 using base::UserMetricsAction
;
66 using content::BrowserThread
;
70 const char* const kCustodianInfoPrefs
[] = {
71 prefs::kSupervisedUserCustodianName
,
72 prefs::kSupervisedUserCustodianEmail
,
73 prefs::kSupervisedUserCustodianProfileImageURL
,
74 prefs::kSupervisedUserCustodianProfileURL
,
75 prefs::kSupervisedUserSecondCustodianName
,
76 prefs::kSupervisedUserSecondCustodianEmail
,
77 prefs::kSupervisedUserSecondCustodianProfileImageURL
,
78 prefs::kSupervisedUserSecondCustodianProfileURL
,
81 void CreateURLAccessRequest(
83 PermissionRequestCreator
* creator
,
84 const SupervisedUserService::SuccessCallback
& callback
) {
85 creator
->CreateURLAccessRequest(url
, callback
);
88 void CreateExtensionUpdateRequest(
89 const std::string
& extension_id
,
90 PermissionRequestCreator
* creator
,
91 const SupervisedUserService::SuccessCallback
& callback
) {
92 creator
->CreateExtensionUpdateRequest(extension_id
, callback
);
95 #if defined(ENABLE_EXTENSIONS)
102 ExtensionState
GetExtensionState(const extensions::Extension
* extension
) {
103 if (extension
->is_theme())
104 return EXTENSION_ALLOWED
;
106 bool was_installed_by_default
= extension
->was_installed_by_default();
107 bool was_installed_by_custodian
= extension
->was_installed_by_custodian();
108 #if defined(OS_CHROMEOS)
109 // On Chrome OS all external sources are controlled by us so it means that
110 // they are "default". Method was_installed_by_default returns false because
111 // extensions creation flags are ignored in case of default extensions with
112 // update URL(the flags aren't passed to OnExternalExtensionUpdateUrlFound).
113 // TODO(dpolukhin): remove this Chrome OS specific code as soon as creation
114 // flags are not ignored.
115 was_installed_by_default
=
116 extensions::Manifest::IsExternalLocation(extension
->location());
118 if (extensions::Manifest::IsComponentLocation(extension
->location()) ||
119 was_installed_by_default
||
120 was_installed_by_custodian
) {
121 // Enforce default extensions as well as custodian-installed extensions
122 // (if we'd allow the supervised user to uninstall them, there'd be no way
123 // to get them back).
124 return EXTENSION_FORCED
;
127 return EXTENSION_BLOCKED
;
133 base::FilePath
SupervisedUserService::Delegate::GetBlacklistPath() const {
134 return base::FilePath();
137 GURL
SupervisedUserService::Delegate::GetBlacklistURL() const {
141 std::string
SupervisedUserService::Delegate::GetSafeSitesCx() const {
142 return std::string();
145 SupervisedUserService::URLFilterContext::URLFilterContext()
146 : ui_url_filter_(new SupervisedUserURLFilter
),
147 io_url_filter_(new SupervisedUserURLFilter
) {}
148 SupervisedUserService::URLFilterContext::~URLFilterContext() {}
150 SupervisedUserURLFilter
*
151 SupervisedUserService::URLFilterContext::ui_url_filter() const {
152 return ui_url_filter_
.get();
155 SupervisedUserURLFilter
*
156 SupervisedUserService::URLFilterContext::io_url_filter() const {
157 return io_url_filter_
.get();
160 void SupervisedUserService::URLFilterContext::SetDefaultFilteringBehavior(
161 SupervisedUserURLFilter::FilteringBehavior behavior
) {
162 ui_url_filter_
->SetDefaultFilteringBehavior(behavior
);
163 BrowserThread::PostTask(
166 base::Bind(&SupervisedUserURLFilter::SetDefaultFilteringBehavior
,
167 io_url_filter_
.get(), behavior
));
170 void SupervisedUserService::URLFilterContext::LoadWhitelists(
171 const std::vector
<scoped_refptr
<SupervisedUserSiteList
> >& site_lists
) {
172 ui_url_filter_
->LoadWhitelists(site_lists
);
173 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
174 base::Bind(&SupervisedUserURLFilter::LoadWhitelists
,
175 io_url_filter_
, site_lists
));
178 void SupervisedUserService::URLFilterContext::LoadBlacklist(
179 const base::FilePath
& path
,
180 const base::Closure
& callback
) {
181 // For now, support loading only once. If we want to support re-load, we'll
182 // have to clear the blacklist pointer in the url filters first.
183 DCHECK_EQ(0u, blacklist_
.GetEntryCount());
184 blacklist_
.ReadFromFile(
186 base::Bind(&SupervisedUserService::URLFilterContext::OnBlacklistLoaded
,
187 base::Unretained(this), callback
));
190 void SupervisedUserService::URLFilterContext::SetManualHosts(
191 scoped_ptr
<std::map
<std::string
, bool> > host_map
) {
192 ui_url_filter_
->SetManualHosts(host_map
.get());
193 BrowserThread::PostTask(
196 base::Bind(&SupervisedUserURLFilter::SetManualHosts
,
197 io_url_filter_
, base::Owned(host_map
.release())));
200 void SupervisedUserService::URLFilterContext::SetManualURLs(
201 scoped_ptr
<std::map
<GURL
, bool> > url_map
) {
202 ui_url_filter_
->SetManualURLs(url_map
.get());
203 BrowserThread::PostTask(
206 base::Bind(&SupervisedUserURLFilter::SetManualURLs
,
207 io_url_filter_
, base::Owned(url_map
.release())));
210 void SupervisedUserService::URLFilterContext::Clear() {
211 ui_url_filter_
->Clear();
212 BrowserThread::PostTask(
215 base::Bind(&SupervisedUserURLFilter::Clear
,
219 void SupervisedUserService::URLFilterContext::OnBlacklistLoaded(
220 const base::Closure
& callback
) {
221 ui_url_filter_
->SetBlacklist(&blacklist_
);
222 BrowserThread::PostTask(
225 base::Bind(&SupervisedUserURLFilter::SetBlacklist
,
231 void SupervisedUserService::URLFilterContext::InitAsyncURLChecker(
232 const scoped_refptr
<net::URLRequestContextGetter
>& context
,
233 const std::string
& cx
) {
234 ui_url_filter_
->InitAsyncURLChecker(context
.get(), cx
);
235 BrowserThread::PostTask(
238 base::Bind(&SupervisedUserURLFilter::InitAsyncURLChecker
,
239 io_url_filter_
, context
, cx
));
242 SupervisedUserService::SupervisedUserService(Profile
* profile
)
243 : includes_sync_sessions_type_(true),
247 waiting_for_sync_initialization_(false),
248 is_profile_active_(false),
250 did_shutdown_(false),
251 weak_ptr_factory_(this) {
252 url_filter_context_
.ui_url_filter()->AddObserver(this);
255 SupervisedUserService::~SupervisedUserService() {
256 DCHECK(!did_init_
|| did_shutdown_
);
257 url_filter_context_
.ui_url_filter()->RemoveObserver(this);
260 void SupervisedUserService::Shutdown() {
263 DCHECK(!did_shutdown_
);
264 did_shutdown_
= true;
265 if (ProfileIsSupervised()) {
266 content::RecordAction(UserMetricsAction("ManagedUsers_QuitBrowser"));
270 ProfileSyncService
* sync_service
=
271 ProfileSyncServiceFactory::GetForProfile(profile_
);
272 // Can be null in tests.
274 sync_service
->RemovePreferenceProvider(this);
277 bool SupervisedUserService::ProfileIsSupervised() const {
278 return profile_
->IsSupervised();
281 void SupervisedUserService::OnCustodianInfoChanged() {
283 SupervisedUserServiceObserver
, observer_list_
, OnCustodianInfoChanged());
287 void SupervisedUserService::RegisterProfilePrefs(
288 user_prefs::PrefRegistrySyncable
* registry
) {
289 registry
->RegisterDictionaryPref(
290 prefs::kSupervisedUserManualHosts
,
291 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
292 registry
->RegisterDictionaryPref(
293 prefs::kSupervisedUserManualURLs
,
294 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
295 registry
->RegisterIntegerPref(
296 prefs::kDefaultSupervisedUserFilteringBehavior
,
297 SupervisedUserURLFilter::ALLOW
,
298 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
299 registry
->RegisterBooleanPref(prefs::kSupervisedUserCreationAllowed
, true,
300 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
301 for (const char* pref
: kCustodianInfoPrefs
) {
302 registry
->RegisterStringPref(pref
, std::string(),
303 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
307 void SupervisedUserService::SetDelegate(Delegate
* delegate
) {
309 // Changing delegates isn't allowed.
312 // If the delegate is removed, deactivate first to give the old delegate a
313 // chance to clean up.
316 delegate_
= delegate
;
319 scoped_refptr
<const SupervisedUserURLFilter
>
320 SupervisedUserService::GetURLFilterForIOThread() {
321 return url_filter_context_
.io_url_filter();
324 SupervisedUserURLFilter
* SupervisedUserService::GetURLFilterForUIThread() {
325 return url_filter_context_
.ui_url_filter();
328 SupervisedUserWhitelistService
* SupervisedUserService::GetWhitelistService() {
329 return whitelist_service_
.get();
332 std::string
SupervisedUserService::GetCustodianEmailAddress() const {
333 std::string custodian_email
= profile_
->GetPrefs()->GetString(
334 prefs::kSupervisedUserCustodianEmail
);
335 #if defined(OS_CHROMEOS)
336 if (custodian_email
.empty()) {
337 custodian_email
= chromeos::ChromeUserManager::Get()
338 ->GetSupervisedUserManager()
339 ->GetManagerDisplayEmail(
340 user_manager::UserManager::Get()->GetActiveUser()->email());
343 return custodian_email
;
346 std::string
SupervisedUserService::GetCustodianName() const {
347 std::string name
= profile_
->GetPrefs()->GetString(
348 prefs::kSupervisedUserCustodianName
);
349 #if defined(OS_CHROMEOS)
351 name
= base::UTF16ToUTF8(chromeos::ChromeUserManager::Get()
352 ->GetSupervisedUserManager()
353 ->GetManagerDisplayName(
354 user_manager::UserManager::Get()->GetActiveUser()->email()));
357 return name
.empty() ? GetCustodianEmailAddress() : name
;
360 std::string
SupervisedUserService::GetSecondCustodianEmailAddress() const {
361 return profile_
->GetPrefs()->GetString(
362 prefs::kSupervisedUserSecondCustodianEmail
);
365 std::string
SupervisedUserService::GetSecondCustodianName() const {
366 std::string name
= profile_
->GetPrefs()->GetString(
367 prefs::kSupervisedUserSecondCustodianName
);
368 return name
.empty() ? GetSecondCustodianEmailAddress() : name
;
371 void SupervisedUserService::AddNavigationBlockedCallback(
372 const NavigationBlockedCallback
& callback
) {
373 navigation_blocked_callbacks_
.push_back(callback
);
376 void SupervisedUserService::DidBlockNavigation(
377 content::WebContents
* web_contents
) {
378 for (const auto& callback
: navigation_blocked_callbacks_
)
379 callback
.Run(web_contents
);
382 void SupervisedUserService::AddObserver(
383 SupervisedUserServiceObserver
* observer
) {
384 observer_list_
.AddObserver(observer
);
387 void SupervisedUserService::RemoveObserver(
388 SupervisedUserServiceObserver
* observer
) {
389 observer_list_
.RemoveObserver(observer
);
392 void SupervisedUserService::AddPermissionRequestCreator(
393 scoped_ptr
<PermissionRequestCreator
> creator
) {
394 permissions_creators_
.push_back(creator
.release());
397 syncer::ModelTypeSet
SupervisedUserService::GetPreferredDataTypes() const {
398 if (!ProfileIsSupervised())
399 return syncer::ModelTypeSet();
401 syncer::ModelTypeSet result
;
402 if (IncludesSyncSessionsType())
403 result
.Put(syncer::SESSIONS
);
404 result
.Put(syncer::EXTENSIONS
);
405 result
.Put(syncer::EXTENSION_SETTINGS
);
406 result
.Put(syncer::APPS
);
407 result
.Put(syncer::APP_SETTINGS
);
408 result
.Put(syncer::APP_NOTIFICATIONS
);
409 result
.Put(syncer::APP_LIST
);
413 void SupervisedUserService::OnHistoryRecordingStateChanged() {
414 bool record_history
=
415 profile_
->GetPrefs()->GetBoolean(prefs::kRecordHistory
);
416 includes_sync_sessions_type_
= record_history
;
417 ProfileSyncServiceFactory::GetForProfile(profile_
)
418 ->ReconfigureDatatypeManager();
421 bool SupervisedUserService::IncludesSyncSessionsType() const {
422 return includes_sync_sessions_type_
;
425 void SupervisedUserService::OnStateChanged() {
426 ProfileSyncService
* service
=
427 ProfileSyncServiceFactory::GetForProfile(profile_
);
428 if (waiting_for_sync_initialization_
&& service
->backend_initialized() &&
429 service
->backend_mode() == ProfileSyncService::SYNC
) {
430 waiting_for_sync_initialization_
= false;
431 service
->RemoveObserver(this);
436 DLOG_IF(ERROR
, service
->GetAuthError().state() ==
437 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
)
438 << "Credentials rejected";
441 void SupervisedUserService::SetupSync() {
443 FinishSetupSyncWhenReady();
446 void SupervisedUserService::StartSetupSync() {
447 // Tell the sync service that setup is in progress so we don't start syncing
448 // until we've finished configuration.
449 ProfileSyncServiceFactory::GetForProfile(profile_
)->SetSetupInProgress(true);
452 void SupervisedUserService::FinishSetupSyncWhenReady() {
453 // If we're already waiting for the Sync backend, there's nothing to do here.
454 if (waiting_for_sync_initialization_
)
457 // Continue in FinishSetupSync() once the Sync backend has been initialized.
458 ProfileSyncService
* service
=
459 ProfileSyncServiceFactory::GetForProfile(profile_
);
460 if (service
->backend_initialized() &&
461 service
->backend_mode() == ProfileSyncService::SYNC
) {
464 service
->AddObserver(this);
465 waiting_for_sync_initialization_
= true;
469 void SupervisedUserService::FinishSetupSync() {
470 ProfileSyncService
* service
=
471 ProfileSyncServiceFactory::GetForProfile(profile_
);
472 DCHECK(service
->backend_initialized());
473 DCHECK(service
->backend_mode() == ProfileSyncService::SYNC
);
475 // Sync nothing (except types which are set via GetPreferredDataTypes).
476 bool sync_everything
= false;
477 syncer::ModelTypeSet synced_datatypes
;
478 service
->OnUserChoseDatatypes(sync_everything
, synced_datatypes
);
480 // Notify ProfileSyncService that we are done with configuration.
481 service
->SetSetupInProgress(false);
482 service
->SetSyncSetupCompleted();
485 #if defined(ENABLE_EXTENSIONS)
486 std::string
SupervisedUserService::GetDebugPolicyProviderName() const {
487 // Save the string space in official builds.
490 return std::string();
492 return "Supervised User Service";
496 bool SupervisedUserService::UserMayLoad(const extensions::Extension
* extension
,
497 base::string16
* error
) const {
498 DCHECK(ProfileIsSupervised());
499 ExtensionState result
= GetExtensionState(extension
);
500 bool may_load
= (result
!= EXTENSION_BLOCKED
);
501 if (!may_load
&& error
)
502 *error
= l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOCKED_SUPERVISED_USER
);
506 // Note: Having MustRemainInstalled always say "true" for custodian-installed
507 // extensions does NOT prevent remote uninstalls (which is a bit unexpected, but
508 // exactly what we want).
509 bool SupervisedUserService::MustRemainInstalled(
510 const extensions::Extension
* extension
,
511 base::string16
* error
) const {
512 DCHECK(ProfileIsSupervised());
513 ExtensionState result
= GetExtensionState(extension
);
514 bool may_not_uninstall
= (result
== EXTENSION_FORCED
);
515 if (may_not_uninstall
&& error
)
516 *error
= l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOCKED_SUPERVISED_USER
);
517 return may_not_uninstall
;
520 void SupervisedUserService::SetExtensionsActive() {
521 extensions::ExtensionSystem
* extension_system
=
522 extensions::ExtensionSystem::Get(profile_
);
523 extensions::ManagementPolicy
* management_policy
=
524 extension_system
->management_policy();
526 if (management_policy
) {
528 management_policy
->RegisterProvider(this);
530 management_policy
->UnregisterProvider(this);
533 #endif // defined(ENABLE_EXTENSIONS)
535 SupervisedUserSettingsService
* SupervisedUserService::GetSettingsService() {
536 return SupervisedUserSettingsServiceFactory::GetForProfile(profile_
);
539 size_t SupervisedUserService::FindEnabledPermissionRequestCreator(
541 for (size_t i
= start
; i
< permissions_creators_
.size(); ++i
) {
542 if (permissions_creators_
[i
]->IsEnabled())
545 return permissions_creators_
.size();
548 void SupervisedUserService::AddPermissionRequestInternal(
549 const CreatePermissionRequestCallback
& create_request
,
550 const SuccessCallback
& callback
,
552 // Find a permission request creator that is enabled.
553 size_t next_index
= FindEnabledPermissionRequestCreator(index
);
554 if (next_index
>= permissions_creators_
.size()) {
560 permissions_creators_
[next_index
],
561 base::Bind(&SupervisedUserService::OnPermissionRequestIssued
,
562 weak_ptr_factory_
.GetWeakPtr(), create_request
,
563 callback
, next_index
));
566 void SupervisedUserService::OnPermissionRequestIssued(
567 const CreatePermissionRequestCallback
& create_request
,
568 const SuccessCallback
& callback
,
576 AddPermissionRequestInternal(create_request
, callback
, index
+ 1);
579 void SupervisedUserService::OnSupervisedUserIdChanged() {
580 SetActive(ProfileIsSupervised());
583 void SupervisedUserService::OnDefaultFilteringBehaviorChanged() {
584 DCHECK(ProfileIsSupervised());
586 int behavior_value
= profile_
->GetPrefs()->GetInteger(
587 prefs::kDefaultSupervisedUserFilteringBehavior
);
588 SupervisedUserURLFilter::FilteringBehavior behavior
=
589 SupervisedUserURLFilter::BehaviorFromInt(behavior_value
);
590 url_filter_context_
.SetDefaultFilteringBehavior(behavior
);
593 SupervisedUserServiceObserver
, observer_list_
, OnURLFilterChanged());
596 void SupervisedUserService::OnSiteListsChanged(
597 const std::vector
<scoped_refptr
<SupervisedUserSiteList
> >& site_lists
) {
598 url_filter_context_
.LoadWhitelists(site_lists
);
601 void SupervisedUserService::OnSiteListUpdated() {
603 SupervisedUserServiceObserver
, observer_list_
, OnURLFilterChanged());
606 void SupervisedUserService::LoadBlacklist(const base::FilePath
& path
,
608 if (!url
.is_valid()) {
609 LoadBlacklistFromFile(path
);
613 DCHECK(!blacklist_downloader_
);
614 blacklist_downloader_
.reset(new SupervisedUserBlacklistDownloader(
617 profile_
->GetRequestContext(),
618 base::Bind(&SupervisedUserService::OnBlacklistDownloadDone
,
619 base::Unretained(this), path
)));
622 void SupervisedUserService::LoadBlacklistFromFile(const base::FilePath
& path
) {
623 // This object is guaranteed to outlive the URLFilterContext, so we can bind a
624 // raw pointer to it in the callback.
625 url_filter_context_
.LoadBlacklist(
626 path
, base::Bind(&SupervisedUserService::OnBlacklistLoaded
,
627 base::Unretained(this)));
630 void SupervisedUserService::OnBlacklistDownloadDone(const base::FilePath
& path
,
633 LoadBlacklistFromFile(path
);
635 LOG(WARNING
) << "Blacklist download failed";
637 blacklist_downloader_
.reset();
640 void SupervisedUserService::OnBlacklistLoaded() {
642 SupervisedUserServiceObserver
, observer_list_
, OnURLFilterChanged());
645 bool SupervisedUserService::AccessRequestsEnabled() {
646 return FindEnabledPermissionRequestCreator(0) < permissions_creators_
.size();
649 void SupervisedUserService::AddURLAccessRequest(
651 const SuccessCallback
& callback
) {
652 AddPermissionRequestInternal(
653 base::Bind(CreateURLAccessRequest
,
654 SupervisedUserURLFilter::Normalize(url
)),
658 void SupervisedUserService::AddExtensionUpdateRequest(
659 const std::string
& extension_id
,
660 const SuccessCallback
& callback
) {
661 AddPermissionRequestInternal(
662 base::Bind(CreateExtensionUpdateRequest
, extension_id
),
666 void SupervisedUserService::InitSync(const std::string
& refresh_token
) {
669 ProfileOAuth2TokenService
* token_service
=
670 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_
);
671 token_service
->UpdateCredentials(supervised_users::kSupervisedUserPseudoEmail
,
674 FinishSetupSyncWhenReady();
677 void SupervisedUserService::Init() {
680 DCHECK(GetSettingsService()->IsReady());
682 pref_change_registrar_
.Init(profile_
->GetPrefs());
683 pref_change_registrar_
.Add(
684 prefs::kSupervisedUserId
,
685 base::Bind(&SupervisedUserService::OnSupervisedUserIdChanged
,
686 base::Unretained(this)));
687 pref_change_registrar_
.Add(
688 prefs::kRecordHistory
,
689 base::Bind(&SupervisedUserService::OnHistoryRecordingStateChanged
,
690 base::Unretained(this)));
692 ProfileSyncService
* sync_service
=
693 ProfileSyncServiceFactory::GetForProfile(profile_
);
694 // Can be null in tests.
696 sync_service
->AddPreferenceProvider(this);
698 std::string client_id
= component_updater::SupervisedUserWhitelistInstaller::
699 ClientIdForProfilePath(profile_
->GetPath());
700 whitelist_service_
.reset(new SupervisedUserWhitelistService(
701 profile_
->GetPrefs(),
702 g_browser_process
->supervised_user_whitelist_installer(), client_id
));
703 whitelist_service_
->AddSiteListsChangedCallback(
704 base::Bind(&SupervisedUserService::OnSiteListsChanged
,
705 weak_ptr_factory_
.GetWeakPtr()));
707 SetActive(ProfileIsSupervised());
710 void SupervisedUserService::SetActive(bool active
) {
711 if (active_
== active
)
715 if (!delegate_
|| !delegate_
->SetActive(active_
)) {
717 SupervisedUserPrefMappingServiceFactory::GetForBrowserContext(profile_
)
720 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
721 if (command_line
->HasSwitch(switches::kSupervisedUserSyncToken
)) {
723 command_line
->GetSwitchValueASCII(
724 switches::kSupervisedUserSyncToken
));
727 ProfileOAuth2TokenService
* token_service
=
728 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_
);
729 token_service
->LoadCredentials(
730 supervised_users::kSupervisedUserPseudoEmail
);
732 permissions_creators_
.push_back(new PermissionRequestCreatorSync(
733 GetSettingsService(),
734 SupervisedUserSharedSettingsServiceFactory::GetForBrowserContext(
736 ProfileSyncServiceFactory::GetForProfile(profile_
),
737 GetSupervisedUserName(),
738 profile_
->GetPrefs()->GetString(prefs::kSupervisedUserId
)));
744 // Now activate/deactivate anything not handled by the delegate yet.
746 #if defined(ENABLE_THEMES)
747 // Re-set the default theme to turn the SU theme on/off.
748 ThemeService
* theme_service
= ThemeServiceFactory::GetForProfile(profile_
);
749 if (theme_service
->UsingDefaultTheme() || theme_service
->UsingSystemTheme()) {
750 ThemeServiceFactory::GetForProfile(profile_
)->UseDefaultTheme();
754 ProfileSyncService
* sync_service
=
755 ProfileSyncServiceFactory::GetForProfile(profile_
);
756 sync_service
->SetEncryptEverythingAllowed(!active_
);
758 GetSettingsService()->SetActive(active_
);
760 #if defined(ENABLE_EXTENSIONS)
761 SetExtensionsActive();
765 pref_change_registrar_
.Add(
766 prefs::kDefaultSupervisedUserFilteringBehavior
,
767 base::Bind(&SupervisedUserService::OnDefaultFilteringBehaviorChanged
,
768 base::Unretained(this)));
769 pref_change_registrar_
.Add(prefs::kSupervisedUserManualHosts
,
770 base::Bind(&SupervisedUserService::UpdateManualHosts
,
771 base::Unretained(this)));
772 pref_change_registrar_
.Add(prefs::kSupervisedUserManualURLs
,
773 base::Bind(&SupervisedUserService::UpdateManualURLs
,
774 base::Unretained(this)));
775 for (const char* pref
: kCustodianInfoPrefs
) {
776 pref_change_registrar_
.Add(pref
,
777 base::Bind(&SupervisedUserService::OnCustodianInfoChanged
,
778 base::Unretained(this)));
781 // Initialize the filter.
782 OnDefaultFilteringBehaviorChanged();
783 whitelist_service_
->Init();
786 bool use_blacklist
= base::CommandLine::ForCurrentProcess()->HasSwitch(
787 switches::kEnableSupervisedUserBlacklist
);
788 if (delegate_
&& use_blacklist
) {
789 base::FilePath blacklist_path
= delegate_
->GetBlacklistPath();
790 if (!blacklist_path
.empty())
791 LoadBlacklist(blacklist_path
, delegate_
->GetBlacklistURL());
793 bool use_safesites
= base::CommandLine::ForCurrentProcess()->HasSwitch(
794 switches::kEnableSupervisedUserSafeSites
);
795 if (delegate_
&& use_safesites
) {
796 const std::string
& cx
= delegate_
->GetSafeSitesCx();
798 url_filter_context_
.InitAsyncURLChecker(
799 profile_
->GetRequestContext(), cx
);
803 #if !defined(OS_ANDROID)
804 // TODO(bauerb): Get rid of the platform-specific #ifdef here.
805 // http://crbug.com/313377
806 BrowserList::AddObserver(this);
809 permissions_creators_
.clear();
811 pref_change_registrar_
.Remove(
812 prefs::kDefaultSupervisedUserFilteringBehavior
);
813 pref_change_registrar_
.Remove(prefs::kSupervisedUserManualHosts
);
814 pref_change_registrar_
.Remove(prefs::kSupervisedUserManualURLs
);
815 for (const char* pref
: kCustodianInfoPrefs
) {
816 pref_change_registrar_
.Remove(pref
);
819 url_filter_context_
.Clear();
821 SupervisedUserServiceObserver
, observer_list_
, OnURLFilterChanged());
823 if (waiting_for_sync_initialization_
)
824 ProfileSyncServiceFactory::GetForProfile(profile_
)->RemoveObserver(this);
826 #if !defined(OS_ANDROID)
827 // TODO(bauerb): Get rid of the platform-specific #ifdef here.
828 // http://crbug.com/313377
829 BrowserList::RemoveObserver(this);
834 void SupervisedUserService::RegisterAndInitSync(
835 SupervisedUserRegistrationUtility
* registration_utility
,
836 Profile
* custodian_profile
,
837 const std::string
& supervised_user_id
,
838 const AuthErrorCallback
& callback
) {
839 DCHECK(ProfileIsSupervised());
840 DCHECK(!custodian_profile
->IsSupervised());
842 base::string16 name
= base::UTF8ToUTF16(
843 profile_
->GetPrefs()->GetString(prefs::kProfileName
));
844 int avatar_index
= profile_
->GetPrefs()->GetInteger(
845 prefs::kProfileAvatarIndex
);
846 SupervisedUserRegistrationInfo
info(name
, avatar_index
);
847 registration_utility
->Register(
850 base::Bind(&SupervisedUserService::OnSupervisedUserRegistered
,
851 weak_ptr_factory_
.GetWeakPtr(), callback
, custodian_profile
));
853 // Fetch the custodian's profile information, to store the name.
854 // TODO(pamg): If --google-profile-info (flag: switches::kGoogleProfileInfo)
855 // is ever enabled, take the name from the ProfileInfoCache instead.
856 CustodianProfileDownloaderService
* profile_downloader_service
=
857 CustodianProfileDownloaderServiceFactory::GetForProfile(
859 profile_downloader_service
->DownloadProfile(
860 base::Bind(&SupervisedUserService::OnCustodianProfileDownloaded
,
861 weak_ptr_factory_
.GetWeakPtr()));
864 void SupervisedUserService::OnCustodianProfileDownloaded(
865 const base::string16
& full_name
) {
866 profile_
->GetPrefs()->SetString(prefs::kSupervisedUserCustodianName
,
867 base::UTF16ToUTF8(full_name
));
870 void SupervisedUserService::OnSupervisedUserRegistered(
871 const AuthErrorCallback
& callback
,
872 Profile
* custodian_profile
,
873 const GoogleServiceAuthError
& auth_error
,
874 const std::string
& token
) {
875 if (auth_error
.state() == GoogleServiceAuthError::NONE
) {
877 SigninManagerBase
* signin
=
878 SigninManagerFactory::GetForProfile(custodian_profile
);
879 profile_
->GetPrefs()->SetString(prefs::kSupervisedUserCustodianEmail
,
880 signin
->GetAuthenticatedUsername());
882 // The supervised user profile is now ready for use.
883 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
884 ProfileInfoCache
& cache
= profile_manager
->GetProfileInfoCache();
885 size_t index
= cache
.GetIndexOfProfileWithPath(profile_
->GetPath());
886 cache
.SetIsOmittedProfileAtIndex(index
, false);
888 DCHECK_EQ(std::string(), token
);
891 callback
.Run(auth_error
);
894 void SupervisedUserService::UpdateManualHosts() {
895 const base::DictionaryValue
* dict
=
896 profile_
->GetPrefs()->GetDictionary(prefs::kSupervisedUserManualHosts
);
897 scoped_ptr
<std::map
<std::string
, bool> > host_map(
898 new std::map
<std::string
, bool>());
899 for (base::DictionaryValue::Iterator
it(*dict
); !it
.IsAtEnd(); it
.Advance()) {
901 bool result
= it
.value().GetAsBoolean(&allow
);
903 (*host_map
)[it
.key()] = allow
;
905 url_filter_context_
.SetManualHosts(host_map
.Pass());
908 SupervisedUserServiceObserver
, observer_list_
, OnURLFilterChanged());
911 void SupervisedUserService::UpdateManualURLs() {
912 const base::DictionaryValue
* dict
=
913 profile_
->GetPrefs()->GetDictionary(prefs::kSupervisedUserManualURLs
);
914 scoped_ptr
<std::map
<GURL
, bool> > url_map(new std::map
<GURL
, bool>());
915 for (base::DictionaryValue::Iterator
it(*dict
); !it
.IsAtEnd(); it
.Advance()) {
917 bool result
= it
.value().GetAsBoolean(&allow
);
919 (*url_map
)[GURL(it
.key())] = allow
;
921 url_filter_context_
.SetManualURLs(url_map
.Pass());
924 SupervisedUserServiceObserver
, observer_list_
, OnURLFilterChanged());
927 void SupervisedUserService::OnBrowserSetLastActive(Browser
* browser
) {
928 bool profile_became_active
= profile_
->IsSameProfile(browser
->profile());
929 if (!is_profile_active_
&& profile_became_active
)
930 content::RecordAction(UserMetricsAction("ManagedUsers_OpenProfile"));
931 else if (is_profile_active_
&& !profile_became_active
)
932 content::RecordAction(UserMetricsAction("ManagedUsers_SwitchProfile"));
934 is_profile_active_
= profile_became_active
;
937 std::string
SupervisedUserService::GetSupervisedUserName() const {
938 #if defined(OS_CHROMEOS)
939 // The active user can be NULL in unit tests.
940 if (user_manager::UserManager::Get()->GetActiveUser()) {
941 return UTF16ToUTF8(user_manager::UserManager::Get()->GetUserDisplayName(
942 user_manager::UserManager::Get()->GetActiveUser()->GetUserID()));
944 return std::string();
946 return profile_
->GetPrefs()->GetString(prefs::kProfileName
);