blob: 9ef4850d9b27ad7b76a4d68625c790e3b6752df5 [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"
treibab0a39e2014-09-24 14:48:2815#include "base/observer_list.h"
[email protected]1ab137b2013-03-21 03:33:1816#include "base/prefs/pref_change_registrar.h"
[email protected]f2253ac2014-05-28 08:37:2117#include "base/scoped_observer.h"
[email protected]f9b294362013-06-10 20:22:3118#include "base/strings/string16.h"
treib4edbded2014-09-05 08:43:5519#include "chrome/browser/supervised_user/experimental/supervised_user_blacklist.h"
[email protected]cce15bb2014-06-17 13:43:5120#include "chrome/browser/supervised_user/supervised_user_url_filter.h"
21#include "chrome/browser/supervised_user/supervised_users.h"
[email protected]a243d644c2013-06-20 18:37:5522#include "chrome/browser/sync/profile_sync_service_observer.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"
[email protected]c14a6802014-07-11 21:51:1226
27#if defined(ENABLE_EXTENSIONS)
[email protected]301116c62013-11-26 10:37:4528#include "extensions/browser/management_policy.h"
[email protected]c14a6802014-07-11 21:51:1229#endif
[email protected]0850e842013-01-19 03:44:3130
[email protected]509ad1a92013-03-19 21:41:0631class Browser;
[email protected]acfcfbb2013-05-13 18:01:2732class GoogleServiceAuthError;
[email protected]4db65f952014-05-20 15:46:3033class PermissionRequestCreator;
[email protected]0850e842013-01-19 03:44:3134class Profile;
treibf136dfb2014-09-25 17:37:4735class SupervisedUserBlacklistDownloader;
[email protected]cce15bb2014-06-17 13:43:5136class SupervisedUserRegistrationUtility;
treibab0a39e2014-09-24 14:48:2837class SupervisedUserServiceObserver;
[email protected]cce15bb2014-06-17 13:43:5138class SupervisedUserSettingsService;
39class SupervisedUserSiteList;
40class SupervisedUserURLFilter;
bauerb4da36132014-12-26 19:53:1341class SupervisedUserWhitelistService;
[email protected]0850e842013-01-19 03:44:3142
treibdaece84f2014-09-05 12:58:1543namespace base {
44class FilePath;
45}
46
treib565a0192014-12-04 13:17:3147namespace content {
48class WebContents;
49}
50
[email protected]f2253ac2014-05-28 08:37:2151namespace extensions {
52class ExtensionRegistry;
53}
54
treib9e4fab902014-10-29 14:25:2655namespace net {
56class URLRequestContextGetter;
57}
58
[email protected]443e9312013-05-06 06:17:3459namespace user_prefs {
60class PrefRegistrySyncable;
61}
62
[email protected]cce15bb2014-06-17 13:43:5163// This class handles all the information related to a given supervised profile
[email protected]0850e842013-01-19 03:44:3164// (e.g. the installed content packs, the default URL filtering behavior, or
65// manual whitelist/blacklist overrides).
[email protected]cce15bb2014-06-17 13:43:5166class SupervisedUserService : public KeyedService,
[email protected]c14a6802014-07-11 21:51:1267#if defined(ENABLE_EXTENSIONS)
[email protected]cce15bb2014-06-17 13:43:5168 public extensions::ManagementPolicy::Provider,
[email protected]c14a6802014-07-11 21:51:1269#endif
[email protected]3a276ff2014-08-12 14:22:0970 public SyncTypePreferenceProvider,
[email protected]c14a6802014-07-11 21:51:1271 public ProfileSyncServiceObserver,
bauerbce9a1a82014-12-18 13:34:2472 public chrome::BrowserListObserver,
73 public SupervisedUserURLFilter::Observer {
[email protected]0850e842013-01-19 03:44:3174 public:
[email protected]438f7e12013-08-07 06:59:0075 typedef base::Callback<void(content::WebContents*)> NavigationBlockedCallback;
[email protected]514fcf22013-08-13 06:37:2476 typedef base::Callback<void(const GoogleServiceAuthError&)> AuthErrorCallback;
bauerb646019b12014-10-16 16:23:0977 typedef base::Callback<void(bool)> SuccessCallback;
[email protected]0850e842013-01-19 03:44:3178
[email protected]f085fdd52014-06-11 18:09:2079 class Delegate {
80 public:
81 virtual ~Delegate() {}
82 // Returns true to indicate that the delegate handled the (de)activation, or
[email protected]cce15bb2014-06-17 13:43:5183 // false to indicate that the SupervisedUserService itself should handle it.
[email protected]f085fdd52014-06-11 18:09:2084 virtual bool SetActive(bool active) = 0;
treibdaece84f2014-09-05 12:58:1585 // Returns the path to a blacklist file to load, or an empty path to
86 // indicate "none".
treib38983df2014-10-24 09:35:1287 virtual base::FilePath GetBlacklistPath() const;
treibf136dfb2014-09-25 17:37:4788 // Returns the URL from which to download a blacklist if no local one exists
89 // yet. The blacklist file will be stored at |GetBlacklistPath()|.
treib38983df2014-10-24 09:35:1290 virtual GURL GetBlacklistURL() const;
treib9e4fab902014-10-29 14:25:2691 // Returns the identifier ("cx") of the Custom Search Engine to use for the
92 // experimental "SafeSites" feature, or the empty string to disable the
93 // feature.
94 virtual std::string GetSafeSitesCx() const;
[email protected]f085fdd52014-06-11 18:09:2095 };
96
dchengc072fff2014-10-21 11:39:0597 ~SupervisedUserService() override;
[email protected]0850e842013-01-19 03:44:3198
[email protected]a243d644c2013-06-20 18:37:5599 // ProfileKeyedService override:
dchengc072fff2014-10-21 11:39:05100 void Shutdown() override;
[email protected]a243d644c2013-06-20 18:37:55101
[email protected]37ca3fe02013-07-05 15:32:44102 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
[email protected]0850e842013-01-19 03:44:31103
[email protected]f085fdd52014-06-11 18:09:20104 void SetDelegate(Delegate* delegate);
105
[email protected]0850e842013-01-19 03:44:31106 // Returns the URL filter for the IO thread, for filtering network requests
[email protected]cce15bb2014-06-17 13:43:51107 // (in SupervisedUserResourceThrottle).
108 scoped_refptr<const SupervisedUserURLFilter> GetURLFilterForIOThread();
[email protected]0850e842013-01-19 03:44:31109
110 // Returns the URL filter for the UI thread, for filtering navigations and
111 // classifying sites in the history view.
[email protected]cce15bb2014-06-17 13:43:51112 SupervisedUserURLFilter* GetURLFilterForUIThread();
[email protected]0850e842013-01-19 03:44:31113
bauerb4da36132014-12-26 19:53:13114 // Returns the whitelist service.
115 SupervisedUserWhitelistService* GetWhitelistService();
116
[email protected]0369d6ab2013-08-09 01:52:59117 // Whether the user can request access to blocked URLs.
118 bool AccessRequestsEnabled();
119
[email protected]e861bba2013-06-17 15:20:54120 // Adds an access request for the given URL. The requests are stored using
121 // a prefix followed by a URIEncoded version of the URL. Each entry contains
122 // a dictionary which currently has the timestamp of the request in it.
bauerb646019b12014-10-16 16:23:09123 void AddAccessRequest(const GURL& url, const SuccessCallback& callback);
[email protected]e861bba2013-06-17 15:20:54124
[email protected]a9c2d642013-05-31 14:37:14125 // Returns the email address of the custodian.
126 std::string GetCustodianEmailAddress() const;
127
[email protected]fae057a2013-06-21 22:46:08128 // Returns the name of the custodian, or the email address if the name is
129 // empty.
130 std::string GetCustodianName() const;
131
treib3e7ecf72014-10-24 16:44:07132 // Returns the email address of the second custodian, or the empty string
133 // if there is no second custodian.
134 std::string GetSecondCustodianEmailAddress() const;
135
136 // Returns the name of the second custodian, or the email address if the name
137 // is empty, or the empty string is there is no second custodian.
138 std::string GetSecondCustodianName() const;
139
[email protected]0850e842013-01-19 03:44:31140 // Initializes this object. This method does nothing if the profile is not
[email protected]cce15bb2014-06-17 13:43:51141 // supervised.
[email protected]0850e842013-01-19 03:44:31142 void Init();
143
[email protected]a243d644c2013-06-20 18:37:55144 // Initializes this profile for syncing, using the provided |refresh_token| to
145 // mint access tokens for Sync.
146 void InitSync(const std::string& refresh_token);
[email protected]acfcfbb2013-05-13 18:01:27147
[email protected]cce15bb2014-06-17 13:43:51148 // Convenience method that registers this supervised user using
[email protected]458d59442013-08-01 14:19:32149 // |registration_utility| and initializes sync with the returned token.
150 // The |callback| will be called when registration is complete,
[email protected]3a276ff2014-08-12 14:22:09151 // whether it succeeded or not -- unless registration was cancelled manually,
[email protected]458d59442013-08-01 14:19:32152 // in which case the callback will be ignored.
[email protected]cce15bb2014-06-17 13:43:51153 void RegisterAndInitSync(
154 SupervisedUserRegistrationUtility* registration_utility,
155 Profile* custodian_profile,
156 const std::string& supervised_user_id,
157 const AuthErrorCallback& callback);
[email protected]acfcfbb2013-05-13 18:01:27158
[email protected]438f7e12013-08-07 06:59:00159 void AddNavigationBlockedCallback(const NavigationBlockedCallback& callback);
160 void DidBlockNavigation(content::WebContents* web_contents);
161
treibab0a39e2014-09-24 14:48:28162 void AddObserver(SupervisedUserServiceObserver* observer);
163 void RemoveObserver(SupervisedUserServiceObserver* observer);
164
treib531fc7312014-12-09 12:49:20165 void AddPermissionRequestCreator(
166 scoped_ptr<PermissionRequestCreator> creator);
bauerb646019b12014-10-16 16:23:09167
[email protected]c14a6802014-07-11 21:51:12168#if defined(ENABLE_EXTENSIONS)
[email protected]ce019142013-02-12 18:08:10169 // extensions::ManagementPolicy::Provider implementation:
dchengc072fff2014-10-21 11:39:05170 std::string GetDebugPolicyProviderName() const override;
171 bool UserMayLoad(const extensions::Extension* extension,
172 base::string16* error) const override;
173 bool UserMayModifySettings(const extensions::Extension* extension,
174 base::string16* error) const override;
[email protected]c14a6802014-07-11 21:51:12175#endif
176
[email protected]3a276ff2014-08-12 14:22:09177 // SyncTypePreferenceProvider implementation:
dchengc072fff2014-10-21 11:39:05178 syncer::ModelTypeSet GetPreferredDataTypes() const override;
[email protected]3a276ff2014-08-12 14:22:09179
[email protected]c14a6802014-07-11 21:51:12180 // ProfileSyncServiceObserver implementation:
dchengc072fff2014-10-21 11:39:05181 void OnStateChanged() override;
[email protected]0850e842013-01-19 03:44:31182
[email protected]dfddd022013-07-10 17:29:48183 // chrome::BrowserListObserver implementation:
dchengc072fff2014-10-21 11:39:05184 void OnBrowserSetLastActive(Browser* browser) override;
[email protected]dfddd022013-07-10 17:29:48185
bauerbce9a1a82014-12-18 13:34:24186 // SupervisedUserURLFilter::Observer implementation:
187 void OnSiteListUpdated() override;
188
[email protected]0850e842013-01-19 03:44:31189 private:
[email protected]cce15bb2014-06-17 13:43:51190 friend class SupervisedUserServiceExtensionTestBase;
191 friend class SupervisedUserServiceFactory;
pvalenzuelac2ca4582014-10-14 19:24:57192 FRIEND_TEST_ALL_PREFIXES(SingleClientSupervisedUserSettingsSyncTest, Sanity);
[email protected]cce15bb2014-06-17 13:43:51193 FRIEND_TEST_ALL_PREFIXES(SupervisedUserServiceTest, ClearOmitOnRegistration);
fhorschig1f351f02014-09-30 12:30:22194 FRIEND_TEST_ALL_PREFIXES(SupervisedUserServiceTest,
195 ChangesIncludedSessionOnChangedSettings);
196 FRIEND_TEST_ALL_PREFIXES(SupervisedUserServiceTest,
197 ChangesSyncSessionStateOnChangedSettings);
bauerb95a50682015-01-07 17:04:15198
[email protected]cce15bb2014-06-17 13:43:51199 // A bridge from the UI thread to the SupervisedUserURLFilters, one of which
200 // lives on the IO thread. This class mediates access to them and makes sure
201 // they are kept in sync.
[email protected]0850e842013-01-19 03:44:31202 class URLFilterContext {
203 public:
204 URLFilterContext();
205 ~URLFilterContext();
206
[email protected]cce15bb2014-06-17 13:43:51207 SupervisedUserURLFilter* ui_url_filter() const;
208 SupervisedUserURLFilter* io_url_filter() const;
[email protected]0850e842013-01-19 03:44:31209
210 void SetDefaultFilteringBehavior(
[email protected]cce15bb2014-06-17 13:43:51211 SupervisedUserURLFilter::FilteringBehavior behavior);
bauerb95a50682015-01-07 17:04:15212 void LoadWhitelists(
213 const std::vector<scoped_refptr<SupervisedUserSiteList>>& site_lists);
bauerbce9a1a82014-12-18 13:34:24214 void LoadBlacklist(const base::FilePath& path,
215 const base::Closure& callback);
bauerb95a50682015-01-07 17:04:15216 void SetManualHosts(scoped_ptr<std::map<std::string, bool>> host_map);
217 void SetManualURLs(scoped_ptr<std::map<GURL, bool>> url_map);
[email protected]0850e842013-01-19 03:44:31218
treib9e4fab902014-10-29 14:25:26219 void InitAsyncURLChecker(net::URLRequestContextGetter* context,
treibe81fd0f2014-11-24 12:35:48220 const std::string& cx);
treib9e4fab902014-10-29 14:25:26221
[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
[email protected]0085863a2013-12-06 21:19:03245 void OnCustodianProfileDownloaded(const base::string16& full_name);
[email protected]200729f82013-06-22 07:05:55246
[email protected]cce15bb2014-06-17 13:43:51247 void OnSupervisedUserRegistered(const AuthErrorCallback& callback,
248 Profile* custodian_profile,
249 const GoogleServiceAuthError& auth_error,
250 const std::string& token);
[email protected]acfcfbb2013-05-13 18:01:27251
[email protected]a243d644c2013-06-20 18:37:55252 void SetupSync();
[email protected]6e08b9a62014-07-08 00:32:48253 void StartSetupSync();
254 void FinishSetupSyncWhenReady();
255 void FinishSetupSync();
[email protected]a243d644c2013-06-20 18:37:55256
[email protected]cce15bb2014-06-17 13:43:51257 bool ProfileIsSupervised() const;
[email protected]e000daf2013-07-31 16:50:58258
treib3fce4e92014-09-26 16:06:25259 void OnCustodianInfoChanged();
260
[email protected]c14a6802014-07-11 21:51:12261#if defined(ENABLE_EXTENSIONS)
[email protected]0850e842013-01-19 03:44:31262 // Internal implementation for ExtensionManagementPolicy::Delegate methods.
263 // If |error| is not NULL, it will be filled with an error message if the
264 // requested extension action (install, modify status, etc.) is not permitted.
[email protected]38b7fb92013-07-26 16:32:26265 bool ExtensionManagementPolicyImpl(const extensions::Extension* extension,
[email protected]0085863a2013-12-06 21:19:03266 base::string16* error) const;
[email protected]0850e842013-01-19 03:44:31267
[email protected]c14a6802014-07-11 21:51:12268 // Extensions helper to SetActive().
269 void SetExtensionsActive();
270#endif
271
[email protected]cce15bb2014-06-17 13:43:51272 SupervisedUserSettingsService* GetSettingsService();
[email protected]e861bba2013-06-17 15:20:54273
bauerbd3a36cc42014-10-01 13:05:49274 size_t FindEnabledPermissionRequestCreator(size_t start);
bauerb646019b12014-10-16 16:23:09275 void AddAccessRequestInternal(const GURL& url,
276 const SuccessCallback& callback,
277 size_t index);
278 void OnPermissionRequestIssued(const GURL& url,
279 const SuccessCallback& callback,
280 size_t index,
281 bool success);
bauerbd3a36cc42014-10-01 13:05:49282
[email protected]cce15bb2014-06-17 13:43:51283 void OnSupervisedUserIdChanged();
[email protected]f085fdd52014-06-11 18:09:20284
[email protected]0850e842013-01-19 03:44:31285 void OnDefaultFilteringBehaviorChanged();
286
bauerb95a50682015-01-07 17:04:15287 void OnSiteListsChanged(
288 const std::vector<scoped_refptr<SupervisedUserSiteList>>& site_lists);
[email protected]0850e842013-01-19 03:44:31289
treibf136dfb2014-09-25 17:37:47290 // Asynchronously downloads a static blacklist file from |url|, stores it at
291 // |path|, loads it, and applies it to the URL filters. If |url| is not valid
292 // (e.g. empty), directly tries to load from |path|.
293 void LoadBlacklist(const base::FilePath& path, const GURL& url);
294
treib4edbded2014-09-05 08:43:55295 // Asynchronously loads a static blacklist from a binary file at |path| and
296 // applies it to the URL filters.
treibf136dfb2014-09-25 17:37:47297 void LoadBlacklistFromFile(const base::FilePath& path);
298
299 void OnBlacklistDownloadDone(const base::FilePath& path, bool success);
treib4edbded2014-09-05 08:43:55300
bauerbce9a1a82014-12-18 13:34:24301 void OnBlacklistLoaded();
302
[email protected]5e022292013-02-06 16:42:17303 // Updates the manual overrides for hosts in the URL filters when the
304 // corresponding preference is changed.
305 void UpdateManualHosts();
[email protected]0850e842013-01-19 03:44:31306
[email protected]5e022292013-02-06 16:42:17307 // Updates the manual overrides for URLs in the URL filters when the
308 // corresponding preference is changed.
309 void UpdateManualURLs();
[email protected]0850e842013-01-19 03:44:31310
[email protected]0b4c6b22014-08-04 09:46:31311 // Returns the human readable name of the supervised user.
312 std::string GetSupervisedUserName() const;
313
fhorschig1f351f02014-09-30 12:30:22314 // Subscribes to the SupervisedUserPrefStore, refreshes
315 // |includes_sync_sessions_type_| and triggers reconfiguring the
316 // ProfileSyncService.
317 void OnHistoryRecordingStateChanged();
318
319 // Returns true if the syncer::SESSIONS type should be included in Sync.
320 bool IncludesSyncSessionsType() const;
321
322 // The option a custodian sets to either record or prevent recording the
323 // supervised user's history. Set by |FetchNewSessionSyncState()| and
324 // defaults to true.
325 bool includes_sync_sessions_type_;
326
[email protected]12b7af32014-03-13 05:28:20327 // Owns us via the KeyedService mechanism.
[email protected]0850e842013-01-19 03:44:31328 Profile* profile_;
329
[email protected]f085fdd52014-06-11 18:09:20330 bool active_;
331
332 Delegate* delegate_;
333
[email protected]0850e842013-01-19 03:44:31334 PrefChangeRegistrar pref_change_registrar_;
335
[email protected]a243d644c2013-06-20 18:37:55336 // True iff we're waiting for the Sync service to be initialized.
337 bool waiting_for_sync_initialization_;
[email protected]dfddd022013-07-10 17:29:48338 bool is_profile_active_;
[email protected]a243d644c2013-06-20 18:37:55339
[email protected]438f7e12013-08-07 06:59:00340 std::vector<NavigationBlockedCallback> navigation_blocked_callbacks_;
341
[email protected]3a276ff2014-08-12 14:22:09342 // True only when |Init()| method has been called.
343 bool did_init_;
344
[email protected]3bf45d02013-07-10 00:03:41345 // True only when |Shutdown()| method has been called.
346 bool did_shutdown_;
347
[email protected]0850e842013-01-19 03:44:31348 URLFilterContext url_filter_context_;
treibf136dfb2014-09-25 17:37:47349 scoped_ptr<SupervisedUserBlacklistDownloader> blacklist_downloader_;
[email protected]8052b242013-11-15 16:40:55350
bauerb4da36132014-12-26 19:53:13351 scoped_ptr<SupervisedUserWhitelistService> whitelist_service_;
352
[email protected]4db65f952014-05-20 15:46:30353 // Used to create permission requests.
bauerbd3a36cc42014-10-01 13:05:49354 ScopedVector<PermissionRequestCreator> permissions_creators_;
[email protected]4db65f952014-05-20 15:46:30355
treibab0a39e2014-09-24 14:48:28356 ObserverList<SupervisedUserServiceObserver> observer_list_;
357
[email protected]cce15bb2014-06-17 13:43:51358 base::WeakPtrFactory<SupervisedUserService> weak_ptr_factory_;
[email protected]0850e842013-01-19 03:44:31359};
360
[email protected]cce15bb2014-06-17 13:43:51361#endif // CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SERVICE_H_