blob: 76c4288a97c1b46f832eaf287e15b51873a69699 [file] [log] [blame]
[email protected]cce15bb2014-06-17 13:43:511// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]0850e842013-01-19 03:44:312// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]cce15bb2014-06-17 13:43:515#include "chrome/browser/supervised_user/supervised_user_service.h"
[email protected]0850e842013-01-19 03:44:316
[email protected]a19df3e2013-05-21 00:03:037#include "base/command_line.h"
treibdaece84f2014-09-05 12:58:158#include "base/files/file_path.h"
[email protected]0850e842013-01-19 03:44:319#include "base/memory/ref_counted.h"
treibd3f8b7a2015-04-10 11:41:3310#include "base/path_service.h"
[email protected]3853a4c2013-02-11 17:15:5711#include "base/prefs/pref_service.h"
[email protected]112158af2013-06-07 23:46:1812#include "base/strings/utf_string_conversions.h"
treibf832a992015-03-24 18:09:2413#include "base/version.h"
[email protected]5ddfade2014-02-03 10:24:5314#include "chrome/browser/browser_process.h"
bauerb4da36132014-12-26 19:53:1315#include "chrome/browser/component_updater/supervised_user_whitelist_installer.h"
[email protected]0850e842013-01-19 03:44:3116#include "chrome/browser/profiles/profile.h"
[email protected]5ddfade2014-02-03 10:24:5317#include "chrome/browser/profiles/profile_info_cache.h"
18#include "chrome/browser/profiles/profile_manager.h"
[email protected]aab946912013-11-06 22:18:5119#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
[email protected]f252df2e2013-06-06 23:47:1920#include "chrome/browser/signin/signin_manager_factory.h"
treibf136dfb2014-09-25 17:37:4721#include "chrome/browser/supervised_user/experimental/supervised_user_blacklist_downloader.h"
treibe2082a0e2015-04-08 10:15:3022#include "chrome/browser/supervised_user/experimental/supervised_user_filtering_switches.h"
treibb473df22015-01-08 18:08:2623#include "chrome/browser/supervised_user/legacy/custodian_profile_downloader_service.h"
24#include "chrome/browser/supervised_user/legacy/custodian_profile_downloader_service_factory.h"
25#include "chrome/browser/supervised_user/legacy/permission_request_creator_sync.h"
26#include "chrome/browser/supervised_user/legacy/supervised_user_pref_mapping_service.h"
27#include "chrome/browser/supervised_user/legacy/supervised_user_pref_mapping_service_factory.h"
28#include "chrome/browser/supervised_user/legacy/supervised_user_registration_utility.h"
29#include "chrome/browser/supervised_user/legacy/supervised_user_shared_settings_service_factory.h"
[email protected]cce15bb2014-06-17 13:43:5130#include "chrome/browser/supervised_user/supervised_user_constants.h"
treibab0a39e2014-09-24 14:48:2831#include "chrome/browser/supervised_user/supervised_user_service_observer.h"
[email protected]cce15bb2014-06-17 13:43:5132#include "chrome/browser/supervised_user/supervised_user_settings_service.h"
33#include "chrome/browser/supervised_user/supervised_user_settings_service_factory.h"
[email protected]cce15bb2014-06-17 13:43:5134#include "chrome/browser/supervised_user/supervised_user_site_list.h"
bauerb4da36132014-12-26 19:53:1335#include "chrome/browser/supervised_user/supervised_user_whitelist_service.h"
[email protected]a19df3e2013-05-21 00:03:0336#include "chrome/browser/sync/profile_sync_service.h"
37#include "chrome/browser/sync/profile_sync_service_factory.h"
[email protected]509ad1a92013-03-19 21:41:0638#include "chrome/browser/ui/browser.h"
[email protected]dfddd022013-07-10 17:29:4839#include "chrome/browser/ui/browser_list.h"
treibd3f8b7a2015-04-10 11:41:3340#include "chrome/common/chrome_paths.h"
[email protected]a19df3e2013-05-21 00:03:0341#include "chrome/common/chrome_switches.h"
[email protected]0850e842013-01-19 03:44:3142#include "chrome/common/pref_names.h"
[email protected]af39f002014-08-22 10:18:1843#include "chrome/grit/generated_resources.h"
[email protected]f0c8c4992014-05-15 17:37:2644#include "components/pref_registry/pref_registry_syncable.h"
[email protected]7274ef02014-03-24 22:43:4045#include "components/signin/core/browser/profile_oauth2_token_service.h"
[email protected]7fbd3b12014-04-01 11:19:1646#include "components/signin/core/browser/signin_manager.h"
[email protected]63c7f0c2014-03-25 21:12:5947#include "components/signin/core/browser/signin_manager_base.h"
[email protected]0850e842013-01-19 03:44:3148#include "content/public/browser/browser_thread.h"
[email protected]2056c3b2014-04-07 18:08:5049#include "content/public/browser/user_metrics.h"
[email protected]0850e842013-01-19 03:44:3150#include "ui/base/l10n/l10n_util.h"
51
[email protected]3aacc9c2013-08-08 11:19:3152#if defined(OS_CHROMEOS)
[email protected]4d390782014-08-15 09:22:5853#include "chrome/browser/chromeos/login/users/chrome_user_manager.h"
[email protected]83d82d42014-05-16 02:04:4254#include "chrome/browser/chromeos/login/users/supervised_user_manager.h"
[email protected]4d390782014-08-15 09:22:5855#include "components/user_manager/user_manager.h"
[email protected]3aacc9c2013-08-08 11:19:3156#endif
57
[email protected]c14a6802014-07-11 21:51:1258#if defined(ENABLE_EXTENSIONS)
treib9e30e302015-04-15 08:12:1659#include "chrome/browser/extensions/extension_service.h"
[email protected]c14a6802014-07-11 21:51:1260#include "extensions/browser/extension_system.h"
[email protected]c14a6802014-07-11 21:51:1261#endif
62
[email protected]f085fdd52014-06-11 18:09:2063#if defined(ENABLE_THEMES)
64#include "chrome/browser/themes/theme_service.h"
65#include "chrome/browser/themes/theme_service_factory.h"
66#endif
67
[email protected]a581ea22013-05-06 12:34:1968using base::DictionaryValue;
[email protected]2056c3b2014-04-07 18:08:5069using base::UserMetricsAction;
[email protected]0850e842013-01-19 03:44:3170using content::BrowserThread;
71
treib22c3a042015-01-15 21:30:1372namespace {
73
treibd3f8b7a2015-04-10 11:41:3374// The URL from which to download a host blacklist if no local one exists yet.
75const char kBlacklistURL[] =
76 "https://www.gstatic.com/chrome/supervised_user/blacklist-20141001-1k.bin";
77// The filename under which we'll store the blacklist (in the user data dir).
78const char kBlacklistFilename[] = "su-blacklist.bin";
79
treib22c3a042015-01-15 21:30:1380const char* const kCustodianInfoPrefs[] = {
81 prefs::kSupervisedUserCustodianName,
82 prefs::kSupervisedUserCustodianEmail,
83 prefs::kSupervisedUserCustodianProfileImageURL,
84 prefs::kSupervisedUserCustodianProfileURL,
85 prefs::kSupervisedUserSecondCustodianName,
86 prefs::kSupervisedUserSecondCustodianEmail,
87 prefs::kSupervisedUserSecondCustodianProfileImageURL,
88 prefs::kSupervisedUserSecondCustodianProfileURL,
89};
90
treib8ecc1eb52015-03-04 18:29:0691void CreateURLAccessRequest(
92 const GURL& url,
93 PermissionRequestCreator* creator,
94 const SupervisedUserService::SuccessCallback& callback) {
95 creator->CreateURLAccessRequest(url, callback);
96}
97
98void CreateExtensionUpdateRequest(
treibf832a992015-03-24 18:09:2499 const std::string& id,
treib8ecc1eb52015-03-04 18:29:06100 PermissionRequestCreator* creator,
101 const SupervisedUserService::SuccessCallback& callback) {
treibf832a992015-03-24 18:09:24102 creator->CreateExtensionUpdateRequest(id, callback);
treib8ecc1eb52015-03-04 18:29:06103}
104
treibd3f8b7a2015-04-10 11:41:33105base::FilePath GetBlacklistPath() {
106 base::FilePath blacklist_dir;
107 PathService::Get(chrome::DIR_USER_DATA, &blacklist_dir);
108 return blacklist_dir.AppendASCII(kBlacklistFilename);
109}
110
treibbb9a1962015-02-25 13:40:59111#if defined(ENABLE_EXTENSIONS)
112enum ExtensionState {
113 EXTENSION_FORCED,
114 EXTENSION_BLOCKED,
115 EXTENSION_ALLOWED
116};
117
118ExtensionState GetExtensionState(const extensions::Extension* extension) {
treibbb9a1962015-02-25 13:40:59119 bool was_installed_by_default = extension->was_installed_by_default();
treibbb9a1962015-02-25 13:40:59120#if defined(OS_CHROMEOS)
121 // On Chrome OS all external sources are controlled by us so it means that
122 // they are "default". Method was_installed_by_default returns false because
123 // extensions creation flags are ignored in case of default extensions with
124 // update URL(the flags aren't passed to OnExternalExtensionUpdateUrlFound).
125 // TODO(dpolukhin): remove this Chrome OS specific code as soon as creation
126 // flags are not ignored.
127 was_installed_by_default =
128 extensions::Manifest::IsExternalLocation(extension->location());
129#endif
treib2fc09342015-04-09 15:53:58130 // Note: Component extensions are protected from modification/uninstallation
131 // anyway, so there's no need to enforce them again for supervised users.
treibbb9a1962015-02-25 13:40:59132 if (extensions::Manifest::IsComponentLocation(extension->location()) ||
treib2fc09342015-04-09 15:53:58133 extension->is_theme() ||
134 was_installed_by_default) {
135 return EXTENSION_ALLOWED;
treibbb9a1962015-02-25 13:40:59136 }
137
treib2fc09342015-04-09 15:53:58138 if (extension->was_installed_by_custodian())
139 return EXTENSION_FORCED;
140
treibbb9a1962015-02-25 13:40:59141 return EXTENSION_BLOCKED;
142}
143#endif
144
treib22c3a042015-01-15 21:30:13145} // namespace
146
[email protected]cce15bb2014-06-17 13:43:51147SupervisedUserService::URLFilterContext::URLFilterContext()
148 : ui_url_filter_(new SupervisedUserURLFilter),
149 io_url_filter_(new SupervisedUserURLFilter) {}
150SupervisedUserService::URLFilterContext::~URLFilterContext() {}
[email protected]0850e842013-01-19 03:44:31151
[email protected]cce15bb2014-06-17 13:43:51152SupervisedUserURLFilter*
153SupervisedUserService::URLFilterContext::ui_url_filter() const {
[email protected]0850e842013-01-19 03:44:31154 return ui_url_filter_.get();
155}
156
[email protected]cce15bb2014-06-17 13:43:51157SupervisedUserURLFilter*
158SupervisedUserService::URLFilterContext::io_url_filter() const {
[email protected]0850e842013-01-19 03:44:31159 return io_url_filter_.get();
160}
161
[email protected]cce15bb2014-06-17 13:43:51162void SupervisedUserService::URLFilterContext::SetDefaultFilteringBehavior(
163 SupervisedUserURLFilter::FilteringBehavior behavior) {
[email protected]0850e842013-01-19 03:44:31164 ui_url_filter_->SetDefaultFilteringBehavior(behavior);
165 BrowserThread::PostTask(
166 BrowserThread::IO,
167 FROM_HERE,
[email protected]cce15bb2014-06-17 13:43:51168 base::Bind(&SupervisedUserURLFilter::SetDefaultFilteringBehavior,
[email protected]5e022292013-02-06 16:42:17169 io_url_filter_.get(), behavior));
[email protected]0850e842013-01-19 03:44:31170}
171
[email protected]cce15bb2014-06-17 13:43:51172void SupervisedUserService::URLFilterContext::LoadWhitelists(
bauerb95a50682015-01-07 17:04:15173 const std::vector<scoped_refptr<SupervisedUserSiteList> >& site_lists) {
174 ui_url_filter_->LoadWhitelists(site_lists);
175 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
176 base::Bind(&SupervisedUserURLFilter::LoadWhitelists,
177 io_url_filter_, site_lists));
[email protected]0850e842013-01-19 03:44:31178}
179
treib4edbded2014-09-05 08:43:55180void SupervisedUserService::URLFilterContext::LoadBlacklist(
bauerbce9a1a82014-12-18 13:34:24181 const base::FilePath& path,
182 const base::Closure& callback) {
treib4edbded2014-09-05 08:43:55183 // For now, support loading only once. If we want to support re-load, we'll
184 // have to clear the blacklist pointer in the url filters first.
185 DCHECK_EQ(0u, blacklist_.GetEntryCount());
186 blacklist_.ReadFromFile(
187 path,
188 base::Bind(&SupervisedUserService::URLFilterContext::OnBlacklistLoaded,
bauerbce9a1a82014-12-18 13:34:24189 base::Unretained(this), callback));
treib4edbded2014-09-05 08:43:55190}
191
[email protected]cce15bb2014-06-17 13:43:51192void SupervisedUserService::URLFilterContext::SetManualHosts(
[email protected]5e022292013-02-06 16:42:17193 scoped_ptr<std::map<std::string, bool> > host_map) {
194 ui_url_filter_->SetManualHosts(host_map.get());
[email protected]0850e842013-01-19 03:44:31195 BrowserThread::PostTask(
196 BrowserThread::IO,
197 FROM_HERE,
[email protected]cce15bb2014-06-17 13:43:51198 base::Bind(&SupervisedUserURLFilter::SetManualHosts,
[email protected]5e022292013-02-06 16:42:17199 io_url_filter_, base::Owned(host_map.release())));
[email protected]0850e842013-01-19 03:44:31200}
201
[email protected]cce15bb2014-06-17 13:43:51202void SupervisedUserService::URLFilterContext::SetManualURLs(
[email protected]5e022292013-02-06 16:42:17203 scoped_ptr<std::map<GURL, bool> > url_map) {
204 ui_url_filter_->SetManualURLs(url_map.get());
[email protected]0850e842013-01-19 03:44:31205 BrowserThread::PostTask(
206 BrowserThread::IO,
207 FROM_HERE,
[email protected]cce15bb2014-06-17 13:43:51208 base::Bind(&SupervisedUserURLFilter::SetManualURLs,
[email protected]5e022292013-02-06 16:42:17209 io_url_filter_, base::Owned(url_map.release())));
[email protected]0850e842013-01-19 03:44:31210}
211
treib22c3a042015-01-15 21:30:13212void SupervisedUserService::URLFilterContext::Clear() {
213 ui_url_filter_->Clear();
214 BrowserThread::PostTask(
215 BrowserThread::IO,
216 FROM_HERE,
217 base::Bind(&SupervisedUserURLFilter::Clear,
218 io_url_filter_));
219}
220
bauerbce9a1a82014-12-18 13:34:24221void SupervisedUserService::URLFilterContext::OnBlacklistLoaded(
222 const base::Closure& callback) {
treib4edbded2014-09-05 08:43:55223 ui_url_filter_->SetBlacklist(&blacklist_);
224 BrowserThread::PostTask(
225 BrowserThread::IO,
226 FROM_HERE,
227 base::Bind(&SupervisedUserURLFilter::SetBlacklist,
228 io_url_filter_,
229 &blacklist_));
bauerbce9a1a82014-12-18 13:34:24230 callback.Run();
treib4edbded2014-09-05 08:43:55231}
232
treib9e4fab902014-10-29 14:25:26233void SupervisedUserService::URLFilterContext::InitAsyncURLChecker(
treibd3f8b7a2015-04-10 11:41:33234 const scoped_refptr<net::URLRequestContextGetter>& context) {
235 ui_url_filter_->InitAsyncURLChecker(context.get());
treib9e4fab902014-10-29 14:25:26236 BrowserThread::PostTask(
237 BrowserThread::IO,
238 FROM_HERE,
239 base::Bind(&SupervisedUserURLFilter::InitAsyncURLChecker,
treibd3f8b7a2015-04-10 11:41:33240 io_url_filter_, context));
treib9e4fab902014-10-29 14:25:26241}
242
[email protected]cce15bb2014-06-17 13:43:51243SupervisedUserService::SupervisedUserService(Profile* profile)
fhorschig1f351f02014-09-30 12:30:22244 : includes_sync_sessions_type_(true),
245 profile_(profile),
[email protected]f085fdd52014-06-11 18:09:20246 active_(false),
247 delegate_(NULL),
[email protected]a243d644c2013-06-20 18:37:55248 waiting_for_sync_initialization_(false),
[email protected]dfddd022013-07-10 17:29:48249 is_profile_active_(false),
[email protected]3a276ff2014-08-12 14:22:09250 did_init_(false),
[email protected]8052b242013-11-15 16:40:55251 did_shutdown_(false),
252 weak_ptr_factory_(this) {
bauerbce9a1a82014-12-18 13:34:24253 url_filter_context_.ui_url_filter()->AddObserver(this);
[email protected]a243d644c2013-06-20 18:37:55254}
[email protected]0850e842013-01-19 03:44:31255
[email protected]cce15bb2014-06-17 13:43:51256SupervisedUserService::~SupervisedUserService() {
[email protected]3a276ff2014-08-12 14:22:09257 DCHECK(!did_init_ || did_shutdown_);
bauerbce9a1a82014-12-18 13:34:24258 url_filter_context_.ui_url_filter()->RemoveObserver(this);
[email protected]3bf45d02013-07-10 00:03:41259}
[email protected]0850e842013-01-19 03:44:31260
[email protected]cce15bb2014-06-17 13:43:51261void SupervisedUserService::Shutdown() {
[email protected]3a276ff2014-08-12 14:22:09262 if (!did_init_)
263 return;
264 DCHECK(!did_shutdown_);
[email protected]3bf45d02013-07-10 00:03:41265 did_shutdown_ = true;
[email protected]cce15bb2014-06-17 13:43:51266 if (ProfileIsSupervised()) {
[email protected]2056c3b2014-04-07 18:08:50267 content::RecordAction(UserMetricsAction("ManagedUsers_QuitBrowser"));
[email protected]dfddd022013-07-10 17:29:48268 }
[email protected]f085fdd52014-06-11 18:09:20269 SetActive(false);
[email protected]3a276ff2014-08-12 14:22:09270
271 ProfileSyncService* sync_service =
272 ProfileSyncServiceFactory::GetForProfile(profile_);
273 // Can be null in tests.
274 if (sync_service)
275 sync_service->RemovePreferenceProvider(this);
[email protected]a243d644c2013-06-20 18:37:55276}
277
[email protected]cce15bb2014-06-17 13:43:51278bool SupervisedUserService::ProfileIsSupervised() const {
[email protected]d20d0432014-06-12 17:14:05279 return profile_->IsSupervised();
[email protected]0850e842013-01-19 03:44:31280}
281
treib3fce4e92014-09-26 16:06:25282void SupervisedUserService::OnCustodianInfoChanged() {
283 FOR_EACH_OBSERVER(
284 SupervisedUserServiceObserver, observer_list_, OnCustodianInfoChanged());
285}
286
[email protected]0850e842013-01-19 03:44:31287// static
[email protected]cce15bb2014-06-17 13:43:51288void SupervisedUserService::RegisterProfilePrefs(
[email protected]443e9312013-05-06 06:17:34289 user_prefs::PrefRegistrySyncable* registry) {
290 registry->RegisterDictionaryPref(
[email protected]d20d0432014-06-12 17:14:05291 prefs::kSupervisedUserManualHosts,
[email protected]443e9312013-05-06 06:17:34292 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
293 registry->RegisterDictionaryPref(
[email protected]d20d0432014-06-12 17:14:05294 prefs::kSupervisedUserManualURLs,
[email protected]443e9312013-05-06 06:17:34295 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
296 registry->RegisterIntegerPref(
[email protected]d20d0432014-06-12 17:14:05297 prefs::kDefaultSupervisedUserFilteringBehavior,
[email protected]cce15bb2014-06-17 13:43:51298 SupervisedUserURLFilter::ALLOW,
[email protected]443e9312013-05-06 06:17:34299 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
[email protected]d20d0432014-06-12 17:14:05300 registry->RegisterBooleanPref(prefs::kSupervisedUserCreationAllowed, true,
[email protected]a7d65a82013-07-02 17:01:05301 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
treib22c3a042015-01-15 21:30:13302 for (const char* pref : kCustodianInfoPrefs) {
303 registry->RegisterStringPref(pref, std::string(),
304 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
305 }
[email protected]0850e842013-01-19 03:44:31306}
307
[email protected]cce15bb2014-06-17 13:43:51308void SupervisedUserService::SetDelegate(Delegate* delegate) {
[email protected]49607ff2014-08-12 16:27:01309 if (delegate) {
310 // Changing delegates isn't allowed.
311 DCHECK(!delegate_);
312 } else {
313 // If the delegate is removed, deactivate first to give the old delegate a
314 // chance to clean up.
315 SetActive(false);
316 }
[email protected]f085fdd52014-06-11 18:09:20317 delegate_ = delegate;
318}
319
[email protected]cce15bb2014-06-17 13:43:51320scoped_refptr<const SupervisedUserURLFilter>
321SupervisedUserService::GetURLFilterForIOThread() {
[email protected]0850e842013-01-19 03:44:31322 return url_filter_context_.io_url_filter();
323}
324
[email protected]cce15bb2014-06-17 13:43:51325SupervisedUserURLFilter* SupervisedUserService::GetURLFilterForUIThread() {
[email protected]0850e842013-01-19 03:44:31326 return url_filter_context_.ui_url_filter();
327}
328
bauerb4da36132014-12-26 19:53:13329SupervisedUserWhitelistService* SupervisedUserService::GetWhitelistService() {
330 return whitelist_service_.get();
331}
332
[email protected]cce15bb2014-06-17 13:43:51333std::string SupervisedUserService::GetCustodianEmailAddress() const {
merkulova270ffc012014-10-13 13:58:04334 std::string custodian_email = profile_->GetPrefs()->GetString(
335 prefs::kSupervisedUserCustodianEmail);
[email protected]3aacc9c2013-08-08 11:19:31336#if defined(OS_CHROMEOS)
merkulova270ffc012014-10-13 13:58:04337 if (custodian_email.empty()) {
338 custodian_email = chromeos::ChromeUserManager::Get()
339 ->GetSupervisedUserManager()
340 ->GetManagerDisplayEmail(
341 user_manager::UserManager::Get()->GetActiveUser()->email());
342 }
[email protected]3aacc9c2013-08-08 11:19:31343#endif
merkulova270ffc012014-10-13 13:58:04344 return custodian_email;
[email protected]fae057a2013-06-21 22:46:08345}
346
[email protected]cce15bb2014-06-17 13:43:51347std::string SupervisedUserService::GetCustodianName() const {
[email protected]fae057a2013-06-21 22:46:08348 std::string name = profile_->GetPrefs()->GetString(
[email protected]d20d0432014-06-12 17:14:05349 prefs::kSupervisedUserCustodianName);
merkulova270ffc012014-10-13 13:58:04350#if defined(OS_CHROMEOS)
351 if (name.empty()) {
352 name = base::UTF16ToUTF8(chromeos::ChromeUserManager::Get()
353 ->GetSupervisedUserManager()
354 ->GetManagerDisplayName(
355 user_manager::UserManager::Get()->GetActiveUser()->email()));
356 }
[email protected]3aacc9c2013-08-08 11:19:31357#endif
merkulova270ffc012014-10-13 13:58:04358 return name.empty() ? GetCustodianEmailAddress() : name;
[email protected]a9c2d642013-05-31 14:37:14359}
360
treib3e7ecf72014-10-24 16:44:07361std::string SupervisedUserService::GetSecondCustodianEmailAddress() const {
362 return profile_->GetPrefs()->GetString(
363 prefs::kSupervisedUserSecondCustodianEmail);
364}
365
366std::string SupervisedUserService::GetSecondCustodianName() const {
367 std::string name = profile_->GetPrefs()->GetString(
368 prefs::kSupervisedUserSecondCustodianName);
369 return name.empty() ? GetSecondCustodianEmailAddress() : name;
370}
371
[email protected]cce15bb2014-06-17 13:43:51372void SupervisedUserService::AddNavigationBlockedCallback(
[email protected]438f7e12013-08-07 06:59:00373 const NavigationBlockedCallback& callback) {
374 navigation_blocked_callbacks_.push_back(callback);
375}
376
[email protected]cce15bb2014-06-17 13:43:51377void SupervisedUserService::DidBlockNavigation(
[email protected]438f7e12013-08-07 06:59:00378 content::WebContents* web_contents) {
treib5844aca2014-09-30 14:08:38379 for (const auto& callback : navigation_blocked_callbacks_)
380 callback.Run(web_contents);
[email protected]438f7e12013-08-07 06:59:00381}
382
treibab0a39e2014-09-24 14:48:28383void SupervisedUserService::AddObserver(
384 SupervisedUserServiceObserver* observer) {
385 observer_list_.AddObserver(observer);
386}
387
388void SupervisedUserService::RemoveObserver(
389 SupervisedUserServiceObserver* observer) {
390 observer_list_.RemoveObserver(observer);
391}
392
treib531fc7312014-12-09 12:49:20393void SupervisedUserService::AddPermissionRequestCreator(
394 scoped_ptr<PermissionRequestCreator> creator) {
395 permissions_creators_.push_back(creator.release());
bauerb646019b12014-10-16 16:23:09396}
397
[email protected]3a276ff2014-08-12 14:22:09398syncer::ModelTypeSet SupervisedUserService::GetPreferredDataTypes() const {
399 if (!ProfileIsSupervised())
400 return syncer::ModelTypeSet();
401
402 syncer::ModelTypeSet result;
fhorschig1f351f02014-09-30 12:30:22403 if (IncludesSyncSessionsType())
404 result.Put(syncer::SESSIONS);
[email protected]3a276ff2014-08-12 14:22:09405 result.Put(syncer::EXTENSIONS);
406 result.Put(syncer::EXTENSION_SETTINGS);
407 result.Put(syncer::APPS);
408 result.Put(syncer::APP_SETTINGS);
409 result.Put(syncer::APP_NOTIFICATIONS);
410 result.Put(syncer::APP_LIST);
411 return result;
412}
413
fhorschig1f351f02014-09-30 12:30:22414void SupervisedUserService::OnHistoryRecordingStateChanged() {
treib6f9598012014-11-11 12:21:24415 bool record_history =
fhorschig1f351f02014-09-30 12:30:22416 profile_->GetPrefs()->GetBoolean(prefs::kRecordHistory);
treib6f9598012014-11-11 12:21:24417 includes_sync_sessions_type_ = record_history;
fhorschig1f351f02014-09-30 12:30:22418 ProfileSyncServiceFactory::GetForProfile(profile_)
419 ->ReconfigureDatatypeManager();
420}
421
422bool SupervisedUserService::IncludesSyncSessionsType() const {
423 return includes_sync_sessions_type_;
424}
425
[email protected]c14a6802014-07-11 21:51:12426void SupervisedUserService::OnStateChanged() {
427 ProfileSyncService* service =
428 ProfileSyncServiceFactory::GetForProfile(profile_);
zea09acc932014-10-14 21:06:20429 if (waiting_for_sync_initialization_ && service->backend_initialized() &&
430 service->backend_mode() == ProfileSyncService::SYNC) {
[email protected]c14a6802014-07-11 21:51:12431 waiting_for_sync_initialization_ = false;
432 service->RemoveObserver(this);
433 FinishSetupSync();
434 return;
435 }
436
437 DLOG_IF(ERROR, service->GetAuthError().state() ==
438 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)
439 << "Credentials rejected";
440}
[email protected]0850e842013-01-19 03:44:31441
[email protected]cce15bb2014-06-17 13:43:51442void SupervisedUserService::SetupSync() {
[email protected]6e08b9a62014-07-08 00:32:48443 StartSetupSync();
444 FinishSetupSyncWhenReady();
445}
446
447void SupervisedUserService::StartSetupSync() {
448 // Tell the sync service that setup is in progress so we don't start syncing
449 // until we've finished configuration.
450 ProfileSyncServiceFactory::GetForProfile(profile_)->SetSetupInProgress(true);
451}
452
453void SupervisedUserService::FinishSetupSyncWhenReady() {
454 // If we're already waiting for the Sync backend, there's nothing to do here.
455 if (waiting_for_sync_initialization_)
456 return;
457
458 // Continue in FinishSetupSync() once the Sync backend has been initialized.
459 ProfileSyncService* service =
460 ProfileSyncServiceFactory::GetForProfile(profile_);
zea09acc932014-10-14 21:06:20461 if (service->backend_initialized() &&
462 service->backend_mode() == ProfileSyncService::SYNC) {
[email protected]6e08b9a62014-07-08 00:32:48463 FinishSetupSync();
464 } else {
465 service->AddObserver(this);
466 waiting_for_sync_initialization_ = true;
467 }
468}
469
470void SupervisedUserService::FinishSetupSync() {
[email protected]a243d644c2013-06-20 18:37:55471 ProfileSyncService* service =
472 ProfileSyncServiceFactory::GetForProfile(profile_);
zea09acc932014-10-14 21:06:20473 DCHECK(service->backend_initialized());
474 DCHECK(service->backend_mode() == ProfileSyncService::SYNC);
[email protected]a243d644c2013-06-20 18:37:55475
[email protected]3a276ff2014-08-12 14:22:09476 // Sync nothing (except types which are set via GetPreferredDataTypes).
[email protected]a243d644c2013-06-20 18:37:55477 bool sync_everything = false;
478 syncer::ModelTypeSet synced_datatypes;
[email protected]a243d644c2013-06-20 18:37:55479 service->OnUserChoseDatatypes(sync_everything, synced_datatypes);
480
481 // Notify ProfileSyncService that we are done with configuration.
482 service->SetSetupInProgress(false);
483 service->SetSyncSetupCompleted();
484}
485
[email protected]c14a6802014-07-11 21:51:12486#if defined(ENABLE_EXTENSIONS)
treibbb9a1962015-02-25 13:40:59487std::string SupervisedUserService::GetDebugPolicyProviderName() const {
488 // Save the string space in official builds.
489#ifdef NDEBUG
490 NOTREACHED();
491 return std::string();
492#else
493 return "Supervised User Service";
494#endif
495}
496
497bool SupervisedUserService::UserMayLoad(const extensions::Extension* extension,
498 base::string16* error) const {
499 DCHECK(ProfileIsSupervised());
500 ExtensionState result = GetExtensionState(extension);
501 bool may_load = (result != EXTENSION_BLOCKED);
502 if (!may_load && error)
503 *error = l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOCKED_SUPERVISED_USER);
504 return may_load;
505}
506
treib755c12d2015-03-30 08:46:25507bool SupervisedUserService::UserMayModifySettings(
508 const extensions::Extension* extension,
509 base::string16* error) const {
510 DCHECK(ProfileIsSupervised());
511 ExtensionState result = GetExtensionState(extension);
512 bool may_modify = (result == EXTENSION_ALLOWED);
513 if (!may_modify && error)
514 *error = l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOCKED_SUPERVISED_USER);
515 return may_modify;
516}
517
treibbb9a1962015-02-25 13:40:59518// Note: Having MustRemainInstalled always say "true" for custodian-installed
519// extensions does NOT prevent remote uninstalls (which is a bit unexpected, but
520// exactly what we want).
521bool SupervisedUserService::MustRemainInstalled(
[email protected]38b7fb92013-07-26 16:32:26522 const extensions::Extension* extension,
[email protected]0085863a2013-12-06 21:19:03523 base::string16* error) const {
treibbb9a1962015-02-25 13:40:59524 DCHECK(ProfileIsSupervised());
525 ExtensionState result = GetExtensionState(extension);
526 bool may_not_uninstall = (result == EXTENSION_FORCED);
527 if (may_not_uninstall && error)
[email protected]a7340d612014-06-25 22:09:15528 *error = l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOCKED_SUPERVISED_USER);
treibbb9a1962015-02-25 13:40:59529 return may_not_uninstall;
[email protected]0850e842013-01-19 03:44:31530}
531
[email protected]c14a6802014-07-11 21:51:12532void SupervisedUserService::SetExtensionsActive() {
533 extensions::ExtensionSystem* extension_system =
534 extensions::ExtensionSystem::Get(profile_);
535 extensions::ManagementPolicy* management_policy =
536 extension_system->management_policy();
537
bauerb16ccc5e12015-01-05 10:58:31538 if (management_policy) {
539 if (active_)
[email protected]c14a6802014-07-11 21:51:12540 management_policy->RegisterProvider(this);
bauerb16ccc5e12015-01-05 10:58:31541 else
[email protected]c14a6802014-07-11 21:51:12542 management_policy->UnregisterProvider(this);
treib9e30e302015-04-15 08:12:16543
544 // Re-check the policy to make sure any new settings get applied.
545 extension_system->extension_service()->CheckManagementPolicy();
[email protected]c14a6802014-07-11 21:51:12546 }
547}
548#endif // defined(ENABLE_EXTENSIONS)
549
[email protected]cce15bb2014-06-17 13:43:51550SupervisedUserSettingsService* SupervisedUserService::GetSettingsService() {
551 return SupervisedUserSettingsServiceFactory::GetForProfile(profile_);
[email protected]e861bba2013-06-17 15:20:54552}
553
bauerbd3a36cc42014-10-01 13:05:49554size_t SupervisedUserService::FindEnabledPermissionRequestCreator(
555 size_t start) {
556 for (size_t i = start; i < permissions_creators_.size(); ++i) {
557 if (permissions_creators_[i]->IsEnabled())
558 return i;
559 }
560 return permissions_creators_.size();
561}
562
treib8ecc1eb52015-03-04 18:29:06563void SupervisedUserService::AddPermissionRequestInternal(
564 const CreatePermissionRequestCallback& create_request,
bauerb646019b12014-10-16 16:23:09565 const SuccessCallback& callback,
566 size_t index) {
bauerbd3a36cc42014-10-01 13:05:49567 // Find a permission request creator that is enabled.
568 size_t next_index = FindEnabledPermissionRequestCreator(index);
bauerb646019b12014-10-16 16:23:09569 if (next_index >= permissions_creators_.size()) {
570 callback.Run(false);
bauerbd3a36cc42014-10-01 13:05:49571 return;
bauerb646019b12014-10-16 16:23:09572 }
bauerbd3a36cc42014-10-01 13:05:49573
treib8ecc1eb52015-03-04 18:29:06574 create_request.Run(
575 permissions_creators_[next_index],
bauerbd3a36cc42014-10-01 13:05:49576 base::Bind(&SupervisedUserService::OnPermissionRequestIssued,
treib8ecc1eb52015-03-04 18:29:06577 weak_ptr_factory_.GetWeakPtr(), create_request,
578 callback, next_index));
bauerbd3a36cc42014-10-01 13:05:49579}
580
bauerb646019b12014-10-16 16:23:09581void SupervisedUserService::OnPermissionRequestIssued(
treib8ecc1eb52015-03-04 18:29:06582 const CreatePermissionRequestCallback& create_request,
bauerb646019b12014-10-16 16:23:09583 const SuccessCallback& callback,
584 size_t index,
585 bool success) {
586 if (success) {
587 callback.Run(true);
bauerbd3a36cc42014-10-01 13:05:49588 return;
bauerb646019b12014-10-16 16:23:09589 }
bauerbd3a36cc42014-10-01 13:05:49590
treib8ecc1eb52015-03-04 18:29:06591 AddPermissionRequestInternal(create_request, callback, index + 1);
bauerbd3a36cc42014-10-01 13:05:49592}
593
[email protected]cce15bb2014-06-17 13:43:51594void SupervisedUserService::OnSupervisedUserIdChanged() {
treibf813dfc2014-12-08 11:26:33595 SetActive(ProfileIsSupervised());
[email protected]f085fdd52014-06-11 18:09:20596}
597
[email protected]cce15bb2014-06-17 13:43:51598void SupervisedUserService::OnDefaultFilteringBehaviorChanged() {
599 DCHECK(ProfileIsSupervised());
[email protected]0850e842013-01-19 03:44:31600
601 int behavior_value = profile_->GetPrefs()->GetInteger(
[email protected]d20d0432014-06-12 17:14:05602 prefs::kDefaultSupervisedUserFilteringBehavior);
[email protected]cce15bb2014-06-17 13:43:51603 SupervisedUserURLFilter::FilteringBehavior behavior =
604 SupervisedUserURLFilter::BehaviorFromInt(behavior_value);
[email protected]0850e842013-01-19 03:44:31605 url_filter_context_.SetDefaultFilteringBehavior(behavior);
treibab0a39e2014-09-24 14:48:28606
607 FOR_EACH_OBSERVER(
608 SupervisedUserServiceObserver, observer_list_, OnURLFilterChanged());
[email protected]0850e842013-01-19 03:44:31609}
610
bauerb16ccc5e12015-01-05 10:58:31611void SupervisedUserService::OnSiteListsChanged(
bauerb95a50682015-01-07 17:04:15612 const std::vector<scoped_refptr<SupervisedUserSiteList> >& site_lists) {
613 url_filter_context_.LoadWhitelists(site_lists);
bauerbce9a1a82014-12-18 13:34:24614}
treibab0a39e2014-09-24 14:48:28615
bauerbce9a1a82014-12-18 13:34:24616void SupervisedUserService::OnSiteListUpdated() {
treibab0a39e2014-09-24 14:48:28617 FOR_EACH_OBSERVER(
618 SupervisedUserServiceObserver, observer_list_, OnURLFilterChanged());
[email protected]0850e842013-01-19 03:44:31619}
620
treibf136dfb2014-09-25 17:37:47621void SupervisedUserService::LoadBlacklist(const base::FilePath& path,
622 const GURL& url) {
treibd3f8b7a2015-04-10 11:41:33623 // TODO(treib): Don't re-download the blacklist if the local file exists!
treibf136dfb2014-09-25 17:37:47624 if (!url.is_valid()) {
625 LoadBlacklistFromFile(path);
626 return;
627 }
628
bauerbce9a1a82014-12-18 13:34:24629 DCHECK(!blacklist_downloader_);
treibf136dfb2014-09-25 17:37:47630 blacklist_downloader_.reset(new SupervisedUserBlacklistDownloader(
631 url,
632 path,
633 profile_->GetRequestContext(),
634 base::Bind(&SupervisedUserService::OnBlacklistDownloadDone,
635 base::Unretained(this), path)));
636}
637
638void SupervisedUserService::LoadBlacklistFromFile(const base::FilePath& path) {
bauerbce9a1a82014-12-18 13:34:24639 // This object is guaranteed to outlive the URLFilterContext, so we can bind a
640 // raw pointer to it in the callback.
641 url_filter_context_.LoadBlacklist(
642 path, base::Bind(&SupervisedUserService::OnBlacklistLoaded,
643 base::Unretained(this)));
treib4edbded2014-09-05 08:43:55644}
645
treibf136dfb2014-09-25 17:37:47646void SupervisedUserService::OnBlacklistDownloadDone(const base::FilePath& path,
647 bool success) {
648 if (success) {
649 LoadBlacklistFromFile(path);
650 } else {
651 LOG(WARNING) << "Blacklist download failed";
652 }
653 blacklist_downloader_.reset();
654}
655
bauerbce9a1a82014-12-18 13:34:24656void SupervisedUserService::OnBlacklistLoaded() {
657 FOR_EACH_OBSERVER(
658 SupervisedUserServiceObserver, observer_list_, OnURLFilterChanged());
659}
660
[email protected]cce15bb2014-06-17 13:43:51661bool SupervisedUserService::AccessRequestsEnabled() {
bauerbd3a36cc42014-10-01 13:05:49662 return FindEnabledPermissionRequestCreator(0) < permissions_creators_.size();
[email protected]4db65f952014-05-20 15:46:30663}
664
treib8ecc1eb52015-03-04 18:29:06665void SupervisedUserService::AddURLAccessRequest(
666 const GURL& url,
667 const SuccessCallback& callback) {
668 AddPermissionRequestInternal(
669 base::Bind(CreateURLAccessRequest,
670 SupervisedUserURLFilter::Normalize(url)),
671 callback, 0);
672}
673
674void SupervisedUserService::AddExtensionUpdateRequest(
675 const std::string& extension_id,
treibf832a992015-03-24 18:09:24676 const base::Version& version,
treib8ecc1eb52015-03-04 18:29:06677 const SuccessCallback& callback) {
treibf832a992015-03-24 18:09:24678 std::string id = extension_id + ":" + version.GetString();
treib8ecc1eb52015-03-04 18:29:06679 AddPermissionRequestInternal(
treibf832a992015-03-24 18:09:24680 base::Bind(CreateExtensionUpdateRequest, id),
treib8ecc1eb52015-03-04 18:29:06681 callback, 0);
[email protected]e861bba2013-06-17 15:20:54682}
683
[email protected]cce15bb2014-06-17 13:43:51684void SupervisedUserService::InitSync(const std::string& refresh_token) {
[email protected]6e08b9a62014-07-08 00:32:48685 StartSetupSync();
[email protected]a19df3e2013-05-21 00:03:03686
[email protected]aab946912013-11-06 22:18:51687 ProfileOAuth2TokenService* token_service =
688 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
[email protected]cce15bb2014-06-17 13:43:51689 token_service->UpdateCredentials(supervised_users::kSupervisedUserPseudoEmail,
[email protected]aab946912013-11-06 22:18:51690 refresh_token);
[email protected]a19df3e2013-05-21 00:03:03691
[email protected]6e08b9a62014-07-08 00:32:48692 FinishSetupSyncWhenReady();
[email protected]a19df3e2013-05-21 00:03:03693}
694
[email protected]cce15bb2014-06-17 13:43:51695void SupervisedUserService::Init() {
[email protected]3a276ff2014-08-12 14:22:09696 DCHECK(!did_init_);
697 did_init_ = true;
[email protected]f085fdd52014-06-11 18:09:20698 DCHECK(GetSettingsService()->IsReady());
699
700 pref_change_registrar_.Init(profile_->GetPrefs());
701 pref_change_registrar_.Add(
[email protected]d20d0432014-06-12 17:14:05702 prefs::kSupervisedUserId,
[email protected]cce15bb2014-06-17 13:43:51703 base::Bind(&SupervisedUserService::OnSupervisedUserIdChanged,
[email protected]f085fdd52014-06-11 18:09:20704 base::Unretained(this)));
fhorschig1f351f02014-09-30 12:30:22705 pref_change_registrar_.Add(
706 prefs::kRecordHistory,
707 base::Bind(&SupervisedUserService::OnHistoryRecordingStateChanged,
708 base::Unretained(this)));
[email protected]f085fdd52014-06-11 18:09:20709
[email protected]3a276ff2014-08-12 14:22:09710 ProfileSyncService* sync_service =
711 ProfileSyncServiceFactory::GetForProfile(profile_);
712 // Can be null in tests.
713 if (sync_service)
714 sync_service->AddPreferenceProvider(this);
715
bauerb22951c32015-02-12 13:18:17716 std::string client_id = component_updater::SupervisedUserWhitelistInstaller::
717 ClientIdForProfilePath(profile_->GetPath());
bauerb4da36132014-12-26 19:53:13718 whitelist_service_.reset(new SupervisedUserWhitelistService(
719 profile_->GetPrefs(),
bauerb22951c32015-02-12 13:18:17720 g_browser_process->supervised_user_whitelist_installer(), client_id));
bauerb16ccc5e12015-01-05 10:58:31721 whitelist_service_->AddSiteListsChangedCallback(
722 base::Bind(&SupervisedUserService::OnSiteListsChanged,
723 weak_ptr_factory_.GetWeakPtr()));
bauerb4da36132014-12-26 19:53:13724
[email protected]cce15bb2014-06-17 13:43:51725 SetActive(ProfileIsSupervised());
[email protected]f085fdd52014-06-11 18:09:20726}
727
[email protected]cce15bb2014-06-17 13:43:51728void SupervisedUserService::SetActive(bool active) {
[email protected]f085fdd52014-06-11 18:09:20729 if (active_ == active)
[email protected]0850e842013-01-19 03:44:31730 return;
[email protected]f085fdd52014-06-11 18:09:20731 active_ = active;
732
733 if (!delegate_ || !delegate_->SetActive(active_)) {
734 if (active_) {
735 SupervisedUserPrefMappingServiceFactory::GetForBrowserContext(profile_)
736 ->Init();
737
avi556c05022014-12-22 23:31:43738 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
[email protected]d20d0432014-06-12 17:14:05739 if (command_line->HasSwitch(switches::kSupervisedUserSyncToken)) {
[email protected]f085fdd52014-06-11 18:09:20740 InitSync(
[email protected]d20d0432014-06-12 17:14:05741 command_line->GetSwitchValueASCII(
742 switches::kSupervisedUserSyncToken));
[email protected]f085fdd52014-06-11 18:09:20743 }
744
745 ProfileOAuth2TokenService* token_service =
746 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
[email protected]cce15bb2014-06-17 13:43:51747 token_service->LoadCredentials(
748 supervised_users::kSupervisedUserPseudoEmail);
[email protected]6e08b9a62014-07-08 00:32:48749
treib531fc7312014-12-09 12:49:20750 permissions_creators_.push_back(new PermissionRequestCreatorSync(
751 GetSettingsService(),
752 SupervisedUserSharedSettingsServiceFactory::GetForBrowserContext(
753 profile_),
754 ProfileSyncServiceFactory::GetForProfile(profile_),
755 GetSupervisedUserName(),
756 profile_->GetPrefs()->GetString(prefs::kSupervisedUserId)));
757
[email protected]6e08b9a62014-07-08 00:32:48758 SetupSync();
[email protected]f085fdd52014-06-11 18:09:20759 }
[email protected]e861bba2013-06-17 15:20:54760 }
[email protected]0850e842013-01-19 03:44:31761
[email protected]f085fdd52014-06-11 18:09:20762 // Now activate/deactivate anything not handled by the delegate yet.
[email protected]e1480482013-09-11 11:49:58763
[email protected]f085fdd52014-06-11 18:09:20764#if defined(ENABLE_THEMES)
765 // Re-set the default theme to turn the SU theme on/off.
766 ThemeService* theme_service = ThemeServiceFactory::GetForProfile(profile_);
treib9e30e302015-04-15 08:12:16767 if (theme_service->UsingDefaultTheme() || theme_service->UsingSystemTheme())
768 theme_service->UseDefaultTheme();
[email protected]f085fdd52014-06-11 18:09:20769#endif
[email protected]a19df3e2013-05-21 00:03:03770
bauerbf03588b92014-10-27 13:40:15771 ProfileSyncService* sync_service =
772 ProfileSyncServiceFactory::GetForProfile(profile_);
773 sync_service->SetEncryptEverythingAllowed(!active_);
774
bauerbd3a36cc42014-10-01 13:05:49775 GetSettingsService()->SetActive(active_);
[email protected]a243d644c2013-06-20 18:37:55776
[email protected]c14a6802014-07-11 21:51:12777#if defined(ENABLE_EXTENSIONS)
778 SetExtensionsActive();
779#endif
[email protected]0850e842013-01-19 03:44:31780
[email protected]f085fdd52014-06-11 18:09:20781 if (active_) {
[email protected]f085fdd52014-06-11 18:09:20782 pref_change_registrar_.Add(
[email protected]d20d0432014-06-12 17:14:05783 prefs::kDefaultSupervisedUserFilteringBehavior,
[email protected]cce15bb2014-06-17 13:43:51784 base::Bind(&SupervisedUserService::OnDefaultFilteringBehaviorChanged,
[email protected]f085fdd52014-06-11 18:09:20785 base::Unretained(this)));
[email protected]d20d0432014-06-12 17:14:05786 pref_change_registrar_.Add(prefs::kSupervisedUserManualHosts,
[email protected]cce15bb2014-06-17 13:43:51787 base::Bind(&SupervisedUserService::UpdateManualHosts,
[email protected]f085fdd52014-06-11 18:09:20788 base::Unretained(this)));
[email protected]d20d0432014-06-12 17:14:05789 pref_change_registrar_.Add(prefs::kSupervisedUserManualURLs,
[email protected]cce15bb2014-06-17 13:43:51790 base::Bind(&SupervisedUserService::UpdateManualURLs,
[email protected]f085fdd52014-06-11 18:09:20791 base::Unretained(this)));
treib22c3a042015-01-15 21:30:13792 for (const char* pref : kCustodianInfoPrefs) {
793 pref_change_registrar_.Add(pref,
794 base::Bind(&SupervisedUserService::OnCustodianInfoChanged,
795 base::Unretained(this)));
796 }
[email protected]f085fdd52014-06-11 18:09:20797
798 // Initialize the filter.
799 OnDefaultFilteringBehaviorChanged();
bauerb4da36132014-12-26 19:53:13800 whitelist_service_->Init();
[email protected]f085fdd52014-06-11 18:09:20801 UpdateManualHosts();
802 UpdateManualURLs();
treib9e30e302015-04-15 08:12:16803 if (profile_->IsChild() && delegate_ &&
treibe2082a0e2015-04-08 10:15:30804 supervised_users::IsSafeSitesBlacklistEnabled()) {
treibd3f8b7a2015-04-10 11:41:33805 LoadBlacklist(GetBlacklistPath(), GURL(kBlacklistURL));
treibdaece84f2014-09-05 12:58:15806 }
treib9e30e302015-04-15 08:12:16807 if (profile_->IsChild() && delegate_ &&
treibe2082a0e2015-04-08 10:15:30808 supervised_users::IsSafeSitesOnlineCheckEnabled()) {
treibd3f8b7a2015-04-10 11:41:33809 url_filter_context_.InitAsyncURLChecker(profile_->GetRequestContext());
treib9e4fab902014-10-29 14:25:26810 }
[email protected]0850e842013-01-19 03:44:31811
[email protected]975677d2013-11-14 16:15:34812#if !defined(OS_ANDROID)
[email protected]f085fdd52014-06-11 18:09:20813 // TODO(bauerb): Get rid of the platform-specific #ifdef here.
814 // http://crbug.com/313377
815 BrowserList::AddObserver(this);
[email protected]975677d2013-11-14 16:15:34816#endif
[email protected]f085fdd52014-06-11 18:09:20817 } else {
bauerbd3a36cc42014-10-01 13:05:49818 permissions_creators_.clear();
[email protected]dfddd022013-07-10 17:29:48819
[email protected]d20d0432014-06-12 17:14:05820 pref_change_registrar_.Remove(
821 prefs::kDefaultSupervisedUserFilteringBehavior);
822 pref_change_registrar_.Remove(prefs::kSupervisedUserManualHosts);
823 pref_change_registrar_.Remove(prefs::kSupervisedUserManualURLs);
treib22c3a042015-01-15 21:30:13824 for (const char* pref : kCustodianInfoPrefs) {
825 pref_change_registrar_.Remove(pref);
826 }
827
828 url_filter_context_.Clear();
829 FOR_EACH_OBSERVER(
830 SupervisedUserServiceObserver, observer_list_, OnURLFilterChanged());
[email protected]f085fdd52014-06-11 18:09:20831
[email protected]3a276ff2014-08-12 14:22:09832 if (waiting_for_sync_initialization_)
833 ProfileSyncServiceFactory::GetForProfile(profile_)->RemoveObserver(this);
[email protected]f085fdd52014-06-11 18:09:20834
835#if !defined(OS_ANDROID)
836 // TODO(bauerb): Get rid of the platform-specific #ifdef here.
837 // http://crbug.com/313377
838 BrowserList::RemoveObserver(this);
839#endif
840 }
[email protected]0850e842013-01-19 03:44:31841}
842
[email protected]cce15bb2014-06-17 13:43:51843void SupervisedUserService::RegisterAndInitSync(
844 SupervisedUserRegistrationUtility* registration_utility,
[email protected]f252df2e2013-06-06 23:47:19845 Profile* custodian_profile,
[email protected]cce15bb2014-06-17 13:43:51846 const std::string& supervised_user_id,
[email protected]514fcf22013-08-13 06:37:24847 const AuthErrorCallback& callback) {
[email protected]cce15bb2014-06-17 13:43:51848 DCHECK(ProfileIsSupervised());
[email protected]d20d0432014-06-12 17:14:05849 DCHECK(!custodian_profile->IsSupervised());
[email protected]200729f82013-06-22 07:05:55850
[email protected]6778fed2013-12-24 20:09:37851 base::string16 name = base::UTF8ToUTF16(
[email protected]4e2b6f32013-06-04 16:38:34852 profile_->GetPrefs()->GetString(prefs::kProfileName));
[email protected]b6c626e2013-09-09 13:51:08853 int avatar_index = profile_->GetPrefs()->GetInteger(
854 prefs::kProfileAvatarIndex);
[email protected]cce15bb2014-06-17 13:43:51855 SupervisedUserRegistrationInfo info(name, avatar_index);
[email protected]458d59442013-08-01 14:19:32856 registration_utility->Register(
[email protected]cce15bb2014-06-17 13:43:51857 supervised_user_id,
[email protected]a96e6fd2013-06-17 14:36:58858 info,
[email protected]cce15bb2014-06-17 13:43:51859 base::Bind(&SupervisedUserService::OnSupervisedUserRegistered,
[email protected]f252df2e2013-06-06 23:47:19860 weak_ptr_factory_.GetWeakPtr(), callback, custodian_profile));
[email protected]200729f82013-06-22 07:05:55861
862 // Fetch the custodian's profile information, to store the name.
[email protected]115d5728c2013-10-23 18:06:14863 // TODO(pamg): If --google-profile-info (flag: switches::kGoogleProfileInfo)
[email protected]200729f82013-06-22 07:05:55864 // is ever enabled, take the name from the ProfileInfoCache instead.
[email protected]458d59442013-08-01 14:19:32865 CustodianProfileDownloaderService* profile_downloader_service =
866 CustodianProfileDownloaderServiceFactory::GetForProfile(
867 custodian_profile);
868 profile_downloader_service->DownloadProfile(
[email protected]cce15bb2014-06-17 13:43:51869 base::Bind(&SupervisedUserService::OnCustodianProfileDownloaded,
[email protected]458d59442013-08-01 14:19:32870 weak_ptr_factory_.GetWeakPtr()));
[email protected]200729f82013-06-22 07:05:55871}
872
[email protected]cce15bb2014-06-17 13:43:51873void SupervisedUserService::OnCustodianProfileDownloaded(
[email protected]0085863a2013-12-06 21:19:03874 const base::string16& full_name) {
[email protected]d20d0432014-06-12 17:14:05875 profile_->GetPrefs()->SetString(prefs::kSupervisedUserCustodianName,
[email protected]6778fed2013-12-24 20:09:37876 base::UTF16ToUTF8(full_name));
[email protected]acfcfbb2013-05-13 18:01:27877}
878
[email protected]cce15bb2014-06-17 13:43:51879void SupervisedUserService::OnSupervisedUserRegistered(
[email protected]514fcf22013-08-13 06:37:24880 const AuthErrorCallback& callback,
[email protected]f252df2e2013-06-06 23:47:19881 Profile* custodian_profile,
[email protected]acfcfbb2013-05-13 18:01:27882 const GoogleServiceAuthError& auth_error,
883 const std::string& token) {
[email protected]514fcf22013-08-13 06:37:24884 if (auth_error.state() == GoogleServiceAuthError::NONE) {
885 InitSync(token);
886 SigninManagerBase* signin =
887 SigninManagerFactory::GetForProfile(custodian_profile);
[email protected]d20d0432014-06-12 17:14:05888 profile_->GetPrefs()->SetString(prefs::kSupervisedUserCustodianEmail,
[email protected]514fcf22013-08-13 06:37:24889 signin->GetAuthenticatedUsername());
[email protected]5ddfade2014-02-03 10:24:53890
[email protected]cce15bb2014-06-17 13:43:51891 // The supervised user profile is now ready for use.
[email protected]5ddfade2014-02-03 10:24:53892 ProfileManager* profile_manager = g_browser_process->profile_manager();
893 ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
894 size_t index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
895 cache.SetIsOmittedProfileAtIndex(index, false);
[email protected]514fcf22013-08-13 06:37:24896 } else {
[email protected]acfcfbb2013-05-13 18:01:27897 DCHECK_EQ(std::string(), token);
[email protected]acfcfbb2013-05-13 18:01:27898 }
899
[email protected]514fcf22013-08-13 06:37:24900 callback.Run(auth_error);
[email protected]acfcfbb2013-05-13 18:01:27901}
902
[email protected]cce15bb2014-06-17 13:43:51903void SupervisedUserService::UpdateManualHosts() {
[email protected]cb1078de2013-12-23 20:04:22904 const base::DictionaryValue* dict =
[email protected]d20d0432014-06-12 17:14:05905 profile_->GetPrefs()->GetDictionary(prefs::kSupervisedUserManualHosts);
[email protected]5e022292013-02-06 16:42:17906 scoped_ptr<std::map<std::string, bool> > host_map(
907 new std::map<std::string, bool>());
[email protected]cb1078de2013-12-23 20:04:22908 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) {
[email protected]5e022292013-02-06 16:42:17909 bool allow = false;
910 bool result = it.value().GetAsBoolean(&allow);
911 DCHECK(result);
912 (*host_map)[it.key()] = allow;
913 }
914 url_filter_context_.SetManualHosts(host_map.Pass());
treibab0a39e2014-09-24 14:48:28915
916 FOR_EACH_OBSERVER(
917 SupervisedUserServiceObserver, observer_list_, OnURLFilterChanged());
[email protected]0850e842013-01-19 03:44:31918}
919
[email protected]cce15bb2014-06-17 13:43:51920void SupervisedUserService::UpdateManualURLs() {
[email protected]cb1078de2013-12-23 20:04:22921 const base::DictionaryValue* dict =
[email protected]d20d0432014-06-12 17:14:05922 profile_->GetPrefs()->GetDictionary(prefs::kSupervisedUserManualURLs);
[email protected]5e022292013-02-06 16:42:17923 scoped_ptr<std::map<GURL, bool> > url_map(new std::map<GURL, bool>());
[email protected]cb1078de2013-12-23 20:04:22924 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) {
[email protected]5e022292013-02-06 16:42:17925 bool allow = false;
926 bool result = it.value().GetAsBoolean(&allow);
927 DCHECK(result);
928 (*url_map)[GURL(it.key())] = allow;
929 }
930 url_filter_context_.SetManualURLs(url_map.Pass());
treibab0a39e2014-09-24 14:48:28931
932 FOR_EACH_OBSERVER(
933 SupervisedUserServiceObserver, observer_list_, OnURLFilterChanged());
[email protected]0850e842013-01-19 03:44:31934}
[email protected]dfddd022013-07-10 17:29:48935
[email protected]cce15bb2014-06-17 13:43:51936void SupervisedUserService::OnBrowserSetLastActive(Browser* browser) {
[email protected]dfddd022013-07-10 17:29:48937 bool profile_became_active = profile_->IsSameProfile(browser->profile());
938 if (!is_profile_active_ && profile_became_active)
[email protected]2056c3b2014-04-07 18:08:50939 content::RecordAction(UserMetricsAction("ManagedUsers_OpenProfile"));
[email protected]dfddd022013-07-10 17:29:48940 else if (is_profile_active_ && !profile_became_active)
[email protected]2056c3b2014-04-07 18:08:50941 content::RecordAction(UserMetricsAction("ManagedUsers_SwitchProfile"));
[email protected]dfddd022013-07-10 17:29:48942
943 is_profile_active_ = profile_became_active;
944}
[email protected]0b4c6b22014-08-04 09:46:31945
946std::string SupervisedUserService::GetSupervisedUserName() const {
947#if defined(OS_CHROMEOS)
948 // The active user can be NULL in unit tests.
[email protected]4d390782014-08-15 09:22:58949 if (user_manager::UserManager::Get()->GetActiveUser()) {
950 return UTF16ToUTF8(user_manager::UserManager::Get()->GetUserDisplayName(
951 user_manager::UserManager::Get()->GetActiveUser()->GetUserID()));
[email protected]0b4c6b22014-08-04 09:46:31952 }
953 return std::string();
954#else
955 return profile_->GetPrefs()->GetString(prefs::kProfileName);
956#endif
957}