blob: 32f115d3ee05f4cbb074e451775ff5fe11324e18 [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,
bauerb5f8cda92015-10-07 15:36:4470#if !defined(OS_ANDROID) && !defined(OS_IOS)
droger507e7e2f2015-04-03 18:56:2371 public sync_driver::SyncServiceObserver,
bauerbce9a1a82014-12-18 13:34:2472 public chrome::BrowserListObserver,
bauerb5f8cda92015-10-07 15:36:4473#endif
bauerbce9a1a82014-12-18 13:34:2474 public SupervisedUserURLFilter::Observer {
[email protected]0850e842013-01-19 03:44:3175 public:
treib8ecc1eb52015-03-04 18:29:0676 using NavigationBlockedCallback = base::Callback<void(content::WebContents*)>;
77 using AuthErrorCallback = base::Callback<void(const GoogleServiceAuthError&)>;
78 using SuccessCallback = base::Callback<void(bool)>;
[email protected]0850e842013-01-19 03:44:3179
[email protected]f085fdd52014-06-11 18:09:2080 class Delegate {
81 public:
82 virtual ~Delegate() {}
83 // Returns true to indicate that the delegate handled the (de)activation, or
[email protected]cce15bb2014-06-17 13:43:5184 // false to indicate that the SupervisedUserService itself should handle it.
[email protected]f085fdd52014-06-11 18:09:2085 virtual bool SetActive(bool active) = 0;
[email protected]f085fdd52014-06-11 18:09:2086 };
87
dchengc072fff2014-10-21 11:39:0588 ~SupervisedUserService() override;
[email protected]0850e842013-01-19 03:44:3189
[email protected]37ca3fe02013-07-05 15:32:4490 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
[email protected]0850e842013-01-19 03:44:3191
bauerb5f8cda92015-10-07 15:36:4492 // Initializes this object.
93 void Init();
94
[email protected]f085fdd52014-06-11 18:09:2095 void SetDelegate(Delegate* delegate);
96
[email protected]0850e842013-01-19 03:44:3197 // Returns the URL filter for the IO thread, for filtering network requests
[email protected]cce15bb2014-06-17 13:43:5198 // (in SupervisedUserResourceThrottle).
99 scoped_refptr<const SupervisedUserURLFilter> GetURLFilterForIOThread();
[email protected]0850e842013-01-19 03:44:31100
101 // Returns the URL filter for the UI thread, for filtering navigations and
102 // classifying sites in the history view.
[email protected]cce15bb2014-06-17 13:43:51103 SupervisedUserURLFilter* GetURLFilterForUIThread();
[email protected]0850e842013-01-19 03:44:31104
bauerb4da36132014-12-26 19:53:13105 // Returns the whitelist service.
106 SupervisedUserWhitelistService* GetWhitelistService();
107
treib8ecc1eb52015-03-04 18:29:06108 // Whether the user can request to get access to blocked URLs or to new
109 // extensions.
[email protected]0369d6ab2013-08-09 01:52:59110 bool AccessRequestsEnabled();
111
treib8ecc1eb52015-03-04 18:29:06112 // Adds an access request for the given URL.
113 void AddURLAccessRequest(const GURL& url, const SuccessCallback& callback);
114
115 // Adds an update request for the given WebStore item (App/Extension).
116 void AddExtensionUpdateRequest(const std::string& extension_id,
treibf832a992015-03-24 18:09:24117 const base::Version& version,
treib8ecc1eb52015-03-04 18:29:06118 const SuccessCallback& callback);
[email protected]e861bba2013-06-17 15:20:54119
[email protected]a9c2d642013-05-31 14:37:14120 // Returns the email address of the custodian.
121 std::string GetCustodianEmailAddress() const;
122
[email protected]fae057a2013-06-21 22:46:08123 // Returns the name of the custodian, or the email address if the name is
124 // empty.
125 std::string GetCustodianName() const;
126
treib3e7ecf72014-10-24 16:44:07127 // Returns the email address of the second custodian, or the empty string
128 // if there is no second custodian.
129 std::string GetSecondCustodianEmailAddress() const;
130
131 // Returns the name of the second custodian, or the email address if the name
132 // is empty, or the empty string is there is no second custodian.
133 std::string GetSecondCustodianName() const;
134
treib2170ea02015-10-13 14:55:12135 // Returns a message saying that extensions can only be modified by the
136 // custodian.
137 base::string16 GetExtensionsLockedMessage() const;
138
bauerb5f8cda92015-10-07 15:36:44139#if !defined(OS_ANDROID) && !defined(OS_IOS)
[email protected]a243d644c2013-06-20 18:37:55140 // Initializes this profile for syncing, using the provided |refresh_token| to
141 // mint access tokens for Sync.
142 void InitSync(const std::string& refresh_token);
[email protected]acfcfbb2013-05-13 18:01:27143
[email protected]cce15bb2014-06-17 13:43:51144 // Convenience method that registers this supervised user using
[email protected]458d59442013-08-01 14:19:32145 // |registration_utility| and initializes sync with the returned token.
146 // The |callback| will be called when registration is complete,
[email protected]3a276ff2014-08-12 14:22:09147 // whether it succeeded or not -- unless registration was cancelled manually,
[email protected]458d59442013-08-01 14:19:32148 // in which case the callback will be ignored.
[email protected]cce15bb2014-06-17 13:43:51149 void RegisterAndInitSync(
150 SupervisedUserRegistrationUtility* registration_utility,
151 Profile* custodian_profile,
152 const std::string& supervised_user_id,
153 const AuthErrorCallback& callback);
thestig1b76f1a2015-09-30 22:52:38154#endif
[email protected]acfcfbb2013-05-13 18:01:27155
[email protected]438f7e12013-08-07 06:59:00156 void AddNavigationBlockedCallback(const NavigationBlockedCallback& callback);
157 void DidBlockNavigation(content::WebContents* web_contents);
158
treibab0a39e2014-09-24 14:48:28159 void AddObserver(SupervisedUserServiceObserver* observer);
160 void RemoveObserver(SupervisedUserServiceObserver* observer);
161
treib531fc7312014-12-09 12:49:20162 void AddPermissionRequestCreator(
163 scoped_ptr<PermissionRequestCreator> creator);
bauerb646019b12014-10-16 16:23:09164
bauerb5f8cda92015-10-07 15:36:44165 // ProfileKeyedService override:
166 void Shutdown() override;
167
[email protected]3a276ff2014-08-12 14:22:09168 // SyncTypePreferenceProvider implementation:
dchengc072fff2014-10-21 11:39:05169 syncer::ModelTypeSet GetPreferredDataTypes() const override;
[email protected]3a276ff2014-08-12 14:22:09170
bauerb5f8cda92015-10-07 15:36:44171#if !defined(OS_ANDROID) && !defined(OS_IOS)
droger507e7e2f2015-04-03 18:56:23172 // sync_driver::SyncServiceObserver implementation:
dchengc072fff2014-10-21 11:39:05173 void OnStateChanged() override;
[email protected]0850e842013-01-19 03:44:31174
[email protected]dfddd022013-07-10 17:29:48175 // chrome::BrowserListObserver implementation:
dchengc072fff2014-10-21 11:39:05176 void OnBrowserSetLastActive(Browser* browser) override;
bauerb5f8cda92015-10-07 15:36:44177#endif // !defined(OS_ANDROID) && !defined(OS_IOS)
[email protected]dfddd022013-07-10 17:29:48178
bauerbce9a1a82014-12-18 13:34:24179 // SupervisedUserURLFilter::Observer implementation:
180 void OnSiteListUpdated() override;
181
[email protected]0850e842013-01-19 03:44:31182 private:
[email protected]cce15bb2014-06-17 13:43:51183 friend class SupervisedUserServiceExtensionTestBase;
184 friend class SupervisedUserServiceFactory;
pvalenzuelac2ca4582014-10-14 19:24:57185 FRIEND_TEST_ALL_PREFIXES(SingleClientSupervisedUserSettingsSyncTest, Sanity);
[email protected]cce15bb2014-06-17 13:43:51186 FRIEND_TEST_ALL_PREFIXES(SupervisedUserServiceTest, ClearOmitOnRegistration);
fhorschig1f351f02014-09-30 12:30:22187 FRIEND_TEST_ALL_PREFIXES(SupervisedUserServiceTest,
188 ChangesIncludedSessionOnChangedSettings);
189 FRIEND_TEST_ALL_PREFIXES(SupervisedUserServiceTest,
190 ChangesSyncSessionStateOnChangedSettings);
treibbb9a1962015-02-25 13:40:59191 FRIEND_TEST_ALL_PREFIXES(SupervisedUserServiceExtensionTest,
192 ExtensionManagementPolicyProvider);
bauerb95a50682015-01-07 17:04:15193
treib8ecc1eb52015-03-04 18:29:06194 using CreatePermissionRequestCallback =
195 base::Callback<void(PermissionRequestCreator*, const SuccessCallback&)>;
196
[email protected]cce15bb2014-06-17 13:43:51197 // A bridge from the UI thread to the SupervisedUserURLFilters, one of which
198 // lives on the IO thread. This class mediates access to them and makes sure
199 // they are kept in sync.
[email protected]0850e842013-01-19 03:44:31200 class URLFilterContext {
201 public:
202 URLFilterContext();
203 ~URLFilterContext();
204
[email protected]cce15bb2014-06-17 13:43:51205 SupervisedUserURLFilter* ui_url_filter() const;
206 SupervisedUserURLFilter* io_url_filter() const;
[email protected]0850e842013-01-19 03:44:31207
208 void SetDefaultFilteringBehavior(
[email protected]cce15bb2014-06-17 13:43:51209 SupervisedUserURLFilter::FilteringBehavior behavior);
bauerb95a50682015-01-07 17:04:15210 void LoadWhitelists(
211 const std::vector<scoped_refptr<SupervisedUserSiteList>>& site_lists);
bauerbce9a1a82014-12-18 13:34:24212 void LoadBlacklist(const base::FilePath& path,
213 const base::Closure& callback);
bauerb95a50682015-01-07 17:04:15214 void SetManualHosts(scoped_ptr<std::map<std::string, bool>> host_map);
215 void SetManualURLs(scoped_ptr<std::map<GURL, bool>> url_map);
[email protected]0850e842013-01-19 03:44:31216
maxbogue8e4fb452015-02-11 20:26:09217 void InitAsyncURLChecker(
treibd3f8b7a2015-04-10 11:41:33218 const scoped_refptr<net::URLRequestContextGetter>& context);
treib9e4fab902014-10-29 14:25:26219
treib22c3a042015-01-15 21:30:13220 void Clear();
221
[email protected]0850e842013-01-19 03:44:31222 private:
bauerbce9a1a82014-12-18 13:34:24223 void OnBlacklistLoaded(const base::Closure& callback);
treib4edbded2014-09-05 08:43:55224
[email protected]cce15bb2014-06-17 13:43:51225 // SupervisedUserURLFilter is refcounted because the IO thread filter is
226 // used both by ProfileImplIOData and OffTheRecordProfileIOData (to filter
[email protected]0850e842013-01-19 03:44:31227 // network requests), so they both keep a reference to it.
228 // Clients should not keep references to the UI thread filter, however
229 // (the filter will live as long as the profile lives, and afterwards it
230 // should not be used anymore either).
[email protected]cce15bb2014-06-17 13:43:51231 scoped_refptr<SupervisedUserURLFilter> ui_url_filter_;
232 scoped_refptr<SupervisedUserURLFilter> io_url_filter_;
[email protected]0850e842013-01-19 03:44:31233
treib4edbded2014-09-05 08:43:55234 SupervisedUserBlacklist blacklist_;
235
[email protected]0850e842013-01-19 03:44:31236 DISALLOW_COPY_AND_ASSIGN(URLFilterContext);
237 };
238
[email protected]cce15bb2014-06-17 13:43:51239 // Use |SupervisedUserServiceFactory::GetForProfile(..)| to get
[email protected]3bf45d02013-07-10 00:03:41240 // an instance of this service.
[email protected]cce15bb2014-06-17 13:43:51241 explicit SupervisedUserService(Profile* profile);
[email protected]3bf45d02013-07-10 00:03:41242
[email protected]f085fdd52014-06-11 18:09:20243 void SetActive(bool active);
244
bauerb5f8cda92015-10-07 15:36:44245#if !defined(OS_ANDROID) && !defined(OS_IOS)
[email protected]0085863a2013-12-06 21:19:03246 void OnCustodianProfileDownloaded(const base::string16& full_name);
[email protected]200729f82013-06-22 07:05:55247
[email protected]cce15bb2014-06-17 13:43:51248 void OnSupervisedUserRegistered(const AuthErrorCallback& callback,
249 Profile* custodian_profile,
250 const GoogleServiceAuthError& auth_error,
251 const std::string& token);
[email protected]acfcfbb2013-05-13 18:01:27252
[email protected]a243d644c2013-06-20 18:37:55253 void SetupSync();
[email protected]6e08b9a62014-07-08 00:32:48254 void StartSetupSync();
255 void FinishSetupSyncWhenReady();
256 void FinishSetupSync();
bauerb5f8cda92015-10-07 15:36:44257#endif
[email protected]a243d644c2013-06-20 18:37:55258
[email protected]cce15bb2014-06-17 13:43:51259 bool ProfileIsSupervised() const;
[email protected]e000daf2013-07-31 16:50:58260
treib3fce4e92014-09-26 16:06:25261 void OnCustodianInfoChanged();
262
[email protected]c14a6802014-07-11 21:51:12263#if defined(ENABLE_EXTENSIONS)
treibbb9a1962015-02-25 13:40:59264 // extensions::ManagementPolicy::Provider implementation:
265 std::string GetDebugPolicyProviderName() const override;
266 bool UserMayLoad(const extensions::Extension* extension,
267 base::string16* error) const override;
treib755c12d2015-03-30 08:46:25268 bool UserMayModifySettings(const extensions::Extension* extension,
269 base::string16* error) const override;
treibbb9a1962015-02-25 13:40:59270 bool MustRemainInstalled(const extensions::Extension* extension,
271 base::string16* error) const override;
[email protected]0850e842013-01-19 03:44:31272
[email protected]c14a6802014-07-11 21:51:12273 // Extensions helper to SetActive().
274 void SetExtensionsActive();
275#endif
276
[email protected]cce15bb2014-06-17 13:43:51277 SupervisedUserSettingsService* GetSettingsService();
[email protected]e861bba2013-06-17 15:20:54278
bauerbd3a36cc42014-10-01 13:05:49279 size_t FindEnabledPermissionRequestCreator(size_t start);
treib8ecc1eb52015-03-04 18:29:06280 void AddPermissionRequestInternal(
281 const CreatePermissionRequestCallback& create_request,
282 const SuccessCallback& callback,
283 size_t index);
284 void OnPermissionRequestIssued(
285 const CreatePermissionRequestCallback& create_request,
286 const SuccessCallback& callback,
287 size_t index,
288 bool success);
bauerbd3a36cc42014-10-01 13:05:49289
[email protected]cce15bb2014-06-17 13:43:51290 void OnSupervisedUserIdChanged();
[email protected]f085fdd52014-06-11 18:09:20291
[email protected]0850e842013-01-19 03:44:31292 void OnDefaultFilteringBehaviorChanged();
293
bauerb95a50682015-01-07 17:04:15294 void OnSiteListsChanged(
295 const std::vector<scoped_refptr<SupervisedUserSiteList>>& site_lists);
[email protected]0850e842013-01-19 03:44:31296
treib2fd187392015-04-16 17:19:38297 // Asynchronously loads a blacklist from a binary file at |path| and applies
298 // it to the URL filters. If no file exists at |path| yet, downloads a file
299 // from |url| and stores it at |path| first.
treibf136dfb2014-09-25 17:37:47300 void LoadBlacklist(const base::FilePath& path, const GURL& url);
301
treib2fd187392015-04-16 17:19:38302 void OnBlacklistFileChecked(const base::FilePath& path,
303 const GURL& url,
304 bool file_exists);
305
306 // Asynchronously loads a blacklist from a binary file at |path| and applies
307 // it to the URL filters.
treibf136dfb2014-09-25 17:37:47308 void LoadBlacklistFromFile(const base::FilePath& path);
309
310 void OnBlacklistDownloadDone(const base::FilePath& path, bool success);
treib4edbded2014-09-05 08:43:55311
bauerbce9a1a82014-12-18 13:34:24312 void OnBlacklistLoaded();
313
[email protected]5e022292013-02-06 16:42:17314 // Updates the manual overrides for hosts in the URL filters when the
315 // corresponding preference is changed.
316 void UpdateManualHosts();
[email protected]0850e842013-01-19 03:44:31317
[email protected]5e022292013-02-06 16:42:17318 // Updates the manual overrides for URLs in the URL filters when the
319 // corresponding preference is changed.
320 void UpdateManualURLs();
[email protected]0850e842013-01-19 03:44:31321
[email protected]0b4c6b22014-08-04 09:46:31322 // Returns the human readable name of the supervised user.
323 std::string GetSupervisedUserName() const;
324
fhorschig1f351f02014-09-30 12:30:22325 // Subscribes to the SupervisedUserPrefStore, refreshes
326 // |includes_sync_sessions_type_| and triggers reconfiguring the
327 // ProfileSyncService.
328 void OnHistoryRecordingStateChanged();
329
330 // Returns true if the syncer::SESSIONS type should be included in Sync.
331 bool IncludesSyncSessionsType() const;
332
333 // The option a custodian sets to either record or prevent recording the
334 // supervised user's history. Set by |FetchNewSessionSyncState()| and
335 // defaults to true.
336 bool includes_sync_sessions_type_;
337
[email protected]12b7af32014-03-13 05:28:20338 // Owns us via the KeyedService mechanism.
[email protected]0850e842013-01-19 03:44:31339 Profile* profile_;
340
[email protected]f085fdd52014-06-11 18:09:20341 bool active_;
342
343 Delegate* delegate_;
344
[email protected]0850e842013-01-19 03:44:31345 PrefChangeRegistrar pref_change_registrar_;
346
[email protected]a243d644c2013-06-20 18:37:55347 // True iff we're waiting for the Sync service to be initialized.
348 bool waiting_for_sync_initialization_;
[email protected]dfddd022013-07-10 17:29:48349 bool is_profile_active_;
[email protected]a243d644c2013-06-20 18:37:55350
[email protected]438f7e12013-08-07 06:59:00351 std::vector<NavigationBlockedCallback> navigation_blocked_callbacks_;
352
[email protected]3a276ff2014-08-12 14:22:09353 // True only when |Init()| method has been called.
354 bool did_init_;
355
[email protected]3bf45d02013-07-10 00:03:41356 // True only when |Shutdown()| method has been called.
357 bool did_shutdown_;
358
[email protected]0850e842013-01-19 03:44:31359 URLFilterContext url_filter_context_;
treibcffa6502015-08-06 09:12:27360 scoped_ptr<FileDownloader> blacklist_downloader_;
[email protected]8052b242013-11-15 16:40:55361
bauerb4da36132014-12-26 19:53:13362 scoped_ptr<SupervisedUserWhitelistService> whitelist_service_;
363
[email protected]4db65f952014-05-20 15:46:30364 // Used to create permission requests.
bauerbd3a36cc42014-10-01 13:05:49365 ScopedVector<PermissionRequestCreator> permissions_creators_;
[email protected]4db65f952014-05-20 15:46:30366
brettwd195c952015-06-02 17:31:12367 base::ObserverList<SupervisedUserServiceObserver> observer_list_;
treibab0a39e2014-09-24 14:48:28368
[email protected]cce15bb2014-06-17 13:43:51369 base::WeakPtrFactory<SupervisedUserService> weak_ptr_factory_;
[email protected]0850e842013-01-19 03:44:31370};
371
[email protected]cce15bb2014-06-17 13:43:51372#endif // CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SERVICE_H_