blob: ef4142defe6564a7a1628f88cb95b6a74a9a1cc0 [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#ifndef CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SERVICE_H_
6#define CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SERVICE_H_
[email protected]0850e842013-01-19 03:44:317
treib950c6e82014-09-23 13:07:298#include <map>
treib950c6e82014-09-23 13:07:299#include <string>
[email protected]0850e842013-01-19 03:44:3110#include <vector>
11
[email protected]438f7e12013-08-07 06:59:0012#include "base/callback.h"
[email protected]076ebeda2014-06-06 21:47:2613#include "base/gtest_prod_util.h"
[email protected]a581ea22013-05-06 12:34:1914#include "base/memory/scoped_ptr.h"
thestig7790a352015-08-22 00:05:1115#include "base/memory/scoped_vector.h"
treibab0a39e2014-09-24 14:48:2816#include "base/observer_list.h"
[email protected]1ab137b2013-03-21 03:33:1817#include "base/prefs/pref_change_registrar.h"
[email protected]f2253ac2014-05-28 08:37:2118#include "base/scoped_observer.h"
[email protected]f9b294362013-06-10 20:22:3119#include "base/strings/string16.h"
treib4edbded2014-09-05 08:43:5520#include "chrome/browser/supervised_user/experimental/supervised_user_blacklist.h"
[email protected]cce15bb2014-06-17 13:43:5121#include "chrome/browser/supervised_user/supervised_user_url_filter.h"
22#include "chrome/browser/supervised_user/supervised_users.h"
[email protected]3a276ff2014-08-12 14:22:0923#include "chrome/browser/sync/sync_type_preference_provider.h"
[email protected]514fcf22013-08-13 06:37:2424#include "chrome/browser/ui/browser_list_observer.h"
[email protected]12b7af32014-03-13 05:28:2025#include "components/keyed_service/core/keyed_service.h"
droger507e7e2f2015-04-03 18:56:2326#include "components/sync_driver/sync_service_observer.h"
maxbogue8e4fb452015-02-11 20:26:0927#include "net/url_request/url_request_context_getter.h"
[email protected]c14a6802014-07-11 21:51:1228
29#if defined(ENABLE_EXTENSIONS)
[email protected]301116c62013-11-26 10:37:4530#include "extensions/browser/management_policy.h"
[email protected]c14a6802014-07-11 21:51:1231#endif
[email protected]0850e842013-01-19 03:44:3132
[email protected]509ad1a92013-03-19 21:41:0633class Browser;
treibcffa6502015-08-06 09:12:2734class FileDownloader;
[email protected]acfcfbb2013-05-13 18:01:2735class GoogleServiceAuthError;
[email protected]4db65f952014-05-20 15:46:3036class PermissionRequestCreator;
[email protected]0850e842013-01-19 03:44:3137class Profile;
[email protected]cce15bb2014-06-17 13:43:5138class SupervisedUserRegistrationUtility;
treibab0a39e2014-09-24 14:48:2839class SupervisedUserServiceObserver;
[email protected]cce15bb2014-06-17 13:43:5140class SupervisedUserSettingsService;
41class SupervisedUserSiteList;
42class SupervisedUserURLFilter;
bauerb4da36132014-12-26 19:53:1343class SupervisedUserWhitelistService;
[email protected]0850e842013-01-19 03:44:3144
treibdaece84f2014-09-05 12:58:1545namespace base {
46class FilePath;
treibf832a992015-03-24 18:09:2447class Version;
treibdaece84f2014-09-05 12:58:1548}
49
treib565a0192014-12-04 13:17:3150namespace content {
51class WebContents;
52}
53
[email protected]f2253ac2014-05-28 08:37:2154namespace extensions {
55class ExtensionRegistry;
56}
57
[email protected]443e9312013-05-06 06:17:3458namespace user_prefs {
59class PrefRegistrySyncable;
60}
61
[email protected]cce15bb2014-06-17 13:43:5162// This class handles all the information related to a given supervised profile
[email protected]0850e842013-01-19 03:44:3163// (e.g. the installed content packs, the default URL filtering behavior, or
64// manual whitelist/blacklist overrides).
[email protected]cce15bb2014-06-17 13:43:5165class SupervisedUserService : public KeyedService,
[email protected]c14a6802014-07-11 21:51:1266#if defined(ENABLE_EXTENSIONS)
[email protected]cce15bb2014-06-17 13:43:5167 public extensions::ManagementPolicy::Provider,
[email protected]c14a6802014-07-11 21:51:1268#endif
[email protected]3a276ff2014-08-12 14:22:0969 public SyncTypePreferenceProvider,
droger507e7e2f2015-04-03 18:56:2370 public sync_driver::SyncServiceObserver,
bauerbce9a1a82014-12-18 13:34:2471 public chrome::BrowserListObserver,
72 public SupervisedUserURLFilter::Observer {
[email protected]0850e842013-01-19 03:44:3173 public:
treib8ecc1eb52015-03-04 18:29:0674 using NavigationBlockedCallback = base::Callback<void(content::WebContents*)>;
75 using AuthErrorCallback = base::Callback<void(const GoogleServiceAuthError&)>;
76 using SuccessCallback = base::Callback<void(bool)>;
[email protected]0850e842013-01-19 03:44:3177
[email protected]f085fdd52014-06-11 18:09:2078 class Delegate {
79 public:
80 virtual ~Delegate() {}
81 // Returns true to indicate that the delegate handled the (de)activation, or
[email protected]cce15bb2014-06-17 13:43:5182 // false to indicate that the SupervisedUserService itself should handle it.
[email protected]f085fdd52014-06-11 18:09:2083 virtual bool SetActive(bool active) = 0;
[email protected]f085fdd52014-06-11 18:09:2084 };
85
dchengc072fff2014-10-21 11:39:0586 ~SupervisedUserService() override;
[email protected]0850e842013-01-19 03:44:3187
[email protected]a243d644c2013-06-20 18:37:5588 // ProfileKeyedService override:
dchengc072fff2014-10-21 11:39:0589 void Shutdown() override;
[email protected]a243d644c2013-06-20 18:37:5590
[email protected]37ca3fe02013-07-05 15:32:4491 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
[email protected]0850e842013-01-19 03:44:3192
[email protected]f085fdd52014-06-11 18:09:2093 void SetDelegate(Delegate* delegate);
94
[email protected]0850e842013-01-19 03:44:3195 // Returns the URL filter for the IO thread, for filtering network requests
[email protected]cce15bb2014-06-17 13:43:5196 // (in SupervisedUserResourceThrottle).
97 scoped_refptr<const SupervisedUserURLFilter> GetURLFilterForIOThread();
[email protected]0850e842013-01-19 03:44:3198
99 // Returns the URL filter for the UI thread, for filtering navigations and
100 // classifying sites in the history view.
[email protected]cce15bb2014-06-17 13:43:51101 SupervisedUserURLFilter* GetURLFilterForUIThread();
[email protected]0850e842013-01-19 03:44:31102
bauerb4da36132014-12-26 19:53:13103 // Returns the whitelist service.
104 SupervisedUserWhitelistService* GetWhitelistService();
105
treib8ecc1eb52015-03-04 18:29:06106 // Whether the user can request to get access to blocked URLs or to new
107 // extensions.
[email protected]0369d6ab2013-08-09 01:52:59108 bool AccessRequestsEnabled();
109
treib8ecc1eb52015-03-04 18:29:06110 // Adds an access request for the given URL.
111 void AddURLAccessRequest(const GURL& url, const SuccessCallback& callback);
112
113 // Adds an update request for the given WebStore item (App/Extension).
114 void AddExtensionUpdateRequest(const std::string& extension_id,
treibf832a992015-03-24 18:09:24115 const base::Version& version,
treib8ecc1eb52015-03-04 18:29:06116 const SuccessCallback& callback);
[email protected]e861bba2013-06-17 15:20:54117
[email protected]a9c2d642013-05-31 14:37:14118 // Returns the email address of the custodian.
119 std::string GetCustodianEmailAddress() const;
120
[email protected]fae057a2013-06-21 22:46:08121 // Returns the name of the custodian, or the email address if the name is
122 // empty.
123 std::string GetCustodianName() const;
124
treib3e7ecf72014-10-24 16:44:07125 // Returns the email address of the second custodian, or the empty string
126 // if there is no second custodian.
127 std::string GetSecondCustodianEmailAddress() const;
128
129 // Returns the name of the second custodian, or the email address if the name
130 // is empty, or the empty string is there is no second custodian.
131 std::string GetSecondCustodianName() const;
132
treib9e30e302015-04-15 08:12:16133 // Initializes this object.
[email protected]0850e842013-01-19 03:44:31134 void Init();
135
[email protected]a243d644c2013-06-20 18:37:55136 // Initializes this profile for syncing, using the provided |refresh_token| to
137 // mint access tokens for Sync.
138 void InitSync(const std::string& refresh_token);
[email protected]acfcfbb2013-05-13 18:01:27139
thestig1b76f1a2015-09-30 22:52:38140#if !defined(OS_ANDROID) && !defined(OS_IOS)
[email protected]cce15bb2014-06-17 13:43:51141 // Convenience method that registers this supervised user using
[email protected]458d59442013-08-01 14:19:32142 // |registration_utility| and initializes sync with the returned token.
143 // The |callback| will be called when registration is complete,
[email protected]3a276ff2014-08-12 14:22:09144 // whether it succeeded or not -- unless registration was cancelled manually,
[email protected]458d59442013-08-01 14:19:32145 // in which case the callback will be ignored.
[email protected]cce15bb2014-06-17 13:43:51146 void RegisterAndInitSync(
147 SupervisedUserRegistrationUtility* registration_utility,
148 Profile* custodian_profile,
149 const std::string& supervised_user_id,
150 const AuthErrorCallback& callback);
thestig1b76f1a2015-09-30 22:52:38151#endif
[email protected]acfcfbb2013-05-13 18:01:27152
[email protected]438f7e12013-08-07 06:59:00153 void AddNavigationBlockedCallback(const NavigationBlockedCallback& callback);
154 void DidBlockNavigation(content::WebContents* web_contents);
155
treibab0a39e2014-09-24 14:48:28156 void AddObserver(SupervisedUserServiceObserver* observer);
157 void RemoveObserver(SupervisedUserServiceObserver* observer);
158
treib531fc7312014-12-09 12:49:20159 void AddPermissionRequestCreator(
160 scoped_ptr<PermissionRequestCreator> creator);
bauerb646019b12014-10-16 16:23:09161
[email protected]3a276ff2014-08-12 14:22:09162 // SyncTypePreferenceProvider implementation:
dchengc072fff2014-10-21 11:39:05163 syncer::ModelTypeSet GetPreferredDataTypes() const override;
[email protected]3a276ff2014-08-12 14:22:09164
droger507e7e2f2015-04-03 18:56:23165 // sync_driver::SyncServiceObserver implementation:
dchengc072fff2014-10-21 11:39:05166 void OnStateChanged() override;
[email protected]0850e842013-01-19 03:44:31167
[email protected]dfddd022013-07-10 17:29:48168 // chrome::BrowserListObserver implementation:
dchengc072fff2014-10-21 11:39:05169 void OnBrowserSetLastActive(Browser* browser) override;
[email protected]dfddd022013-07-10 17:29:48170
bauerbce9a1a82014-12-18 13:34:24171 // SupervisedUserURLFilter::Observer implementation:
172 void OnSiteListUpdated() override;
173
[email protected]0850e842013-01-19 03:44:31174 private:
[email protected]cce15bb2014-06-17 13:43:51175 friend class SupervisedUserServiceExtensionTestBase;
176 friend class SupervisedUserServiceFactory;
pvalenzuelac2ca4582014-10-14 19:24:57177 FRIEND_TEST_ALL_PREFIXES(SingleClientSupervisedUserSettingsSyncTest, Sanity);
[email protected]cce15bb2014-06-17 13:43:51178 FRIEND_TEST_ALL_PREFIXES(SupervisedUserServiceTest, ClearOmitOnRegistration);
fhorschig1f351f02014-09-30 12:30:22179 FRIEND_TEST_ALL_PREFIXES(SupervisedUserServiceTest,
180 ChangesIncludedSessionOnChangedSettings);
181 FRIEND_TEST_ALL_PREFIXES(SupervisedUserServiceTest,
182 ChangesSyncSessionStateOnChangedSettings);
treibbb9a1962015-02-25 13:40:59183 FRIEND_TEST_ALL_PREFIXES(SupervisedUserServiceExtensionTest,
184 ExtensionManagementPolicyProvider);
bauerb95a50682015-01-07 17:04:15185
treib8ecc1eb52015-03-04 18:29:06186 using CreatePermissionRequestCallback =
187 base::Callback<void(PermissionRequestCreator*, const SuccessCallback&)>;
188
[email protected]cce15bb2014-06-17 13:43:51189 // A bridge from the UI thread to the SupervisedUserURLFilters, one of which
190 // lives on the IO thread. This class mediates access to them and makes sure
191 // they are kept in sync.
[email protected]0850e842013-01-19 03:44:31192 class URLFilterContext {
193 public:
194 URLFilterContext();
195 ~URLFilterContext();
196
[email protected]cce15bb2014-06-17 13:43:51197 SupervisedUserURLFilter* ui_url_filter() const;
198 SupervisedUserURLFilter* io_url_filter() const;
[email protected]0850e842013-01-19 03:44:31199
200 void SetDefaultFilteringBehavior(
[email protected]cce15bb2014-06-17 13:43:51201 SupervisedUserURLFilter::FilteringBehavior behavior);
bauerb95a50682015-01-07 17:04:15202 void LoadWhitelists(
203 const std::vector<scoped_refptr<SupervisedUserSiteList>>& site_lists);
bauerbce9a1a82014-12-18 13:34:24204 void LoadBlacklist(const base::FilePath& path,
205 const base::Closure& callback);
bauerb95a50682015-01-07 17:04:15206 void SetManualHosts(scoped_ptr<std::map<std::string, bool>> host_map);
207 void SetManualURLs(scoped_ptr<std::map<GURL, bool>> url_map);
[email protected]0850e842013-01-19 03:44:31208
maxbogue8e4fb452015-02-11 20:26:09209 void InitAsyncURLChecker(
treibd3f8b7a2015-04-10 11:41:33210 const scoped_refptr<net::URLRequestContextGetter>& context);
treib9e4fab902014-10-29 14:25:26211
treib22c3a042015-01-15 21:30:13212 void Clear();
213
[email protected]0850e842013-01-19 03:44:31214 private:
bauerbce9a1a82014-12-18 13:34:24215 void OnBlacklistLoaded(const base::Closure& callback);
treib4edbded2014-09-05 08:43:55216
[email protected]cce15bb2014-06-17 13:43:51217 // SupervisedUserURLFilter is refcounted because the IO thread filter is
218 // used both by ProfileImplIOData and OffTheRecordProfileIOData (to filter
[email protected]0850e842013-01-19 03:44:31219 // network requests), so they both keep a reference to it.
220 // Clients should not keep references to the UI thread filter, however
221 // (the filter will live as long as the profile lives, and afterwards it
222 // should not be used anymore either).
[email protected]cce15bb2014-06-17 13:43:51223 scoped_refptr<SupervisedUserURLFilter> ui_url_filter_;
224 scoped_refptr<SupervisedUserURLFilter> io_url_filter_;
[email protected]0850e842013-01-19 03:44:31225
treib4edbded2014-09-05 08:43:55226 SupervisedUserBlacklist blacklist_;
227
[email protected]0850e842013-01-19 03:44:31228 DISALLOW_COPY_AND_ASSIGN(URLFilterContext);
229 };
230
[email protected]cce15bb2014-06-17 13:43:51231 // Use |SupervisedUserServiceFactory::GetForProfile(..)| to get
[email protected]3bf45d02013-07-10 00:03:41232 // an instance of this service.
[email protected]cce15bb2014-06-17 13:43:51233 explicit SupervisedUserService(Profile* profile);
[email protected]3bf45d02013-07-10 00:03:41234
[email protected]f085fdd52014-06-11 18:09:20235 void SetActive(bool active);
236
[email protected]0085863a2013-12-06 21:19:03237 void OnCustodianProfileDownloaded(const base::string16& full_name);
[email protected]200729f82013-06-22 07:05:55238
[email protected]cce15bb2014-06-17 13:43:51239 void OnSupervisedUserRegistered(const AuthErrorCallback& callback,
240 Profile* custodian_profile,
241 const GoogleServiceAuthError& auth_error,
242 const std::string& token);
[email protected]acfcfbb2013-05-13 18:01:27243
[email protected]a243d644c2013-06-20 18:37:55244 void SetupSync();
[email protected]6e08b9a62014-07-08 00:32:48245 void StartSetupSync();
246 void FinishSetupSyncWhenReady();
247 void FinishSetupSync();
[email protected]a243d644c2013-06-20 18:37:55248
[email protected]cce15bb2014-06-17 13:43:51249 bool ProfileIsSupervised() const;
[email protected]e000daf2013-07-31 16:50:58250
treib3fce4e92014-09-26 16:06:25251 void OnCustodianInfoChanged();
252
[email protected]c14a6802014-07-11 21:51:12253#if defined(ENABLE_EXTENSIONS)
treibbb9a1962015-02-25 13:40:59254 // extensions::ManagementPolicy::Provider implementation:
255 std::string GetDebugPolicyProviderName() const override;
256 bool UserMayLoad(const extensions::Extension* extension,
257 base::string16* error) const override;
treib755c12d2015-03-30 08:46:25258 bool UserMayModifySettings(const extensions::Extension* extension,
259 base::string16* error) const override;
treibbb9a1962015-02-25 13:40:59260 bool MustRemainInstalled(const extensions::Extension* extension,
261 base::string16* error) const override;
[email protected]0850e842013-01-19 03:44:31262
[email protected]c14a6802014-07-11 21:51:12263 // Extensions helper to SetActive().
264 void SetExtensionsActive();
265#endif
266
[email protected]cce15bb2014-06-17 13:43:51267 SupervisedUserSettingsService* GetSettingsService();
[email protected]e861bba2013-06-17 15:20:54268
bauerbd3a36cc42014-10-01 13:05:49269 size_t FindEnabledPermissionRequestCreator(size_t start);
treib8ecc1eb52015-03-04 18:29:06270 void AddPermissionRequestInternal(
271 const CreatePermissionRequestCallback& create_request,
272 const SuccessCallback& callback,
273 size_t index);
274 void OnPermissionRequestIssued(
275 const CreatePermissionRequestCallback& create_request,
276 const SuccessCallback& callback,
277 size_t index,
278 bool success);
bauerbd3a36cc42014-10-01 13:05:49279
[email protected]cce15bb2014-06-17 13:43:51280 void OnSupervisedUserIdChanged();
[email protected]f085fdd52014-06-11 18:09:20281
[email protected]0850e842013-01-19 03:44:31282 void OnDefaultFilteringBehaviorChanged();
283
bauerb95a50682015-01-07 17:04:15284 void OnSiteListsChanged(
285 const std::vector<scoped_refptr<SupervisedUserSiteList>>& site_lists);
[email protected]0850e842013-01-19 03:44:31286
treib2fd187392015-04-16 17:19:38287 // Asynchronously loads a blacklist from a binary file at |path| and applies
288 // it to the URL filters. If no file exists at |path| yet, downloads a file
289 // from |url| and stores it at |path| first.
treibf136dfb2014-09-25 17:37:47290 void LoadBlacklist(const base::FilePath& path, const GURL& url);
291
treib2fd187392015-04-16 17:19:38292 void OnBlacklistFileChecked(const base::FilePath& path,
293 const GURL& url,
294 bool file_exists);
295
296 // Asynchronously loads a blacklist from a binary file at |path| and applies
297 // it to the URL filters.
treibf136dfb2014-09-25 17:37:47298 void LoadBlacklistFromFile(const base::FilePath& path);
299
300 void OnBlacklistDownloadDone(const base::FilePath& path, bool success);
treib4edbded2014-09-05 08:43:55301
bauerbce9a1a82014-12-18 13:34:24302 void OnBlacklistLoaded();
303
[email protected]5e022292013-02-06 16:42:17304 // Updates the manual overrides for hosts in the URL filters when the
305 // corresponding preference is changed.
306 void UpdateManualHosts();
[email protected]0850e842013-01-19 03:44:31307
[email protected]5e022292013-02-06 16:42:17308 // Updates the manual overrides for URLs in the URL filters when the
309 // corresponding preference is changed.
310 void UpdateManualURLs();
[email protected]0850e842013-01-19 03:44:31311
[email protected]0b4c6b22014-08-04 09:46:31312 // Returns the human readable name of the supervised user.
313 std::string GetSupervisedUserName() const;
314
fhorschig1f351f02014-09-30 12:30:22315 // Subscribes to the SupervisedUserPrefStore, refreshes
316 // |includes_sync_sessions_type_| and triggers reconfiguring the
317 // ProfileSyncService.
318 void OnHistoryRecordingStateChanged();
319
320 // Returns true if the syncer::SESSIONS type should be included in Sync.
321 bool IncludesSyncSessionsType() const;
322
323 // The option a custodian sets to either record or prevent recording the
324 // supervised user's history. Set by |FetchNewSessionSyncState()| and
325 // defaults to true.
326 bool includes_sync_sessions_type_;
327
[email protected]12b7af32014-03-13 05:28:20328 // Owns us via the KeyedService mechanism.
[email protected]0850e842013-01-19 03:44:31329 Profile* profile_;
330
[email protected]f085fdd52014-06-11 18:09:20331 bool active_;
332
333 Delegate* delegate_;
334
[email protected]0850e842013-01-19 03:44:31335 PrefChangeRegistrar pref_change_registrar_;
336
[email protected]a243d644c2013-06-20 18:37:55337 // True iff we're waiting for the Sync service to be initialized.
338 bool waiting_for_sync_initialization_;
[email protected]dfddd022013-07-10 17:29:48339 bool is_profile_active_;
[email protected]a243d644c2013-06-20 18:37:55340
[email protected]438f7e12013-08-07 06:59:00341 std::vector<NavigationBlockedCallback> navigation_blocked_callbacks_;
342
[email protected]3a276ff2014-08-12 14:22:09343 // True only when |Init()| method has been called.
344 bool did_init_;
345
[email protected]3bf45d02013-07-10 00:03:41346 // True only when |Shutdown()| method has been called.
347 bool did_shutdown_;
348
[email protected]0850e842013-01-19 03:44:31349 URLFilterContext url_filter_context_;
treibcffa6502015-08-06 09:12:27350 scoped_ptr<FileDownloader> blacklist_downloader_;
[email protected]8052b242013-11-15 16:40:55351
bauerb4da36132014-12-26 19:53:13352 scoped_ptr<SupervisedUserWhitelistService> whitelist_service_;
353
[email protected]4db65f952014-05-20 15:46:30354 // Used to create permission requests.
bauerbd3a36cc42014-10-01 13:05:49355 ScopedVector<PermissionRequestCreator> permissions_creators_;
[email protected]4db65f952014-05-20 15:46:30356
brettwd195c952015-06-02 17:31:12357 base::ObserverList<SupervisedUserServiceObserver> observer_list_;
treibab0a39e2014-09-24 14:48:28358
[email protected]cce15bb2014-06-17 13:43:51359 base::WeakPtrFactory<SupervisedUserService> weak_ptr_factory_;
[email protected]0850e842013-01-19 03:44:31360};
361
[email protected]cce15bb2014-06-17 13:43:51362#endif // CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SERVICE_H_