blob: baa419f7f3d22674503d73757805949642959aac [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
[email protected]0d9a1da82013-03-14 21:52:078#include <set>
[email protected]0850e842013-01-19 03:44:319#include <vector>
10
[email protected]438f7e12013-08-07 06:59:0011#include "base/callback.h"
[email protected]076ebeda2014-06-06 21:47:2612#include "base/gtest_prod_util.h"
[email protected]a581ea22013-05-06 12:34:1913#include "base/memory/scoped_ptr.h"
[email protected]1ab137b2013-03-21 03:33:1814#include "base/prefs/pref_change_registrar.h"
[email protected]f2253ac2014-05-28 08:37:2115#include "base/scoped_observer.h"
[email protected]f9b294362013-06-10 20:22:3116#include "base/strings/string16.h"
treib4edbded2014-09-05 08:43:5517#include "chrome/browser/supervised_user/experimental/supervised_user_blacklist.h"
[email protected]cce15bb2014-06-17 13:43:5118#include "chrome/browser/supervised_user/supervised_user_url_filter.h"
19#include "chrome/browser/supervised_user/supervised_users.h"
[email protected]a243d644c2013-06-20 18:37:5520#include "chrome/browser/sync/profile_sync_service_observer.h"
[email protected]3a276ff2014-08-12 14:22:0921#include "chrome/browser/sync/sync_type_preference_provider.h"
[email protected]514fcf22013-08-13 06:37:2422#include "chrome/browser/ui/browser_list_observer.h"
[email protected]12b7af32014-03-13 05:28:2023#include "components/keyed_service/core/keyed_service.h"
[email protected]bfd8cf982013-03-04 15:24:0024#include "content/public/browser/web_contents.h"
[email protected]c14a6802014-07-11 21:51:1225
26#if defined(ENABLE_EXTENSIONS)
[email protected]f2253ac2014-05-28 08:37:2127#include "extensions/browser/extension_registry_observer.h"
[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;
[email protected]cce15bb2014-06-17 13:43:5135class SupervisedUserRegistrationUtility;
36class SupervisedUserSettingsService;
37class SupervisedUserSiteList;
38class SupervisedUserURLFilter;
[email protected]0850e842013-01-19 03:44:3139
treibdaece84f2014-09-05 12:58:1540namespace base {
41class FilePath;
42}
43
[email protected]f2253ac2014-05-28 08:37:2144namespace extensions {
45class ExtensionRegistry;
46}
47
[email protected]443e9312013-05-06 06:17:3448namespace user_prefs {
49class PrefRegistrySyncable;
50}
51
[email protected]cce15bb2014-06-17 13:43:5152// This class handles all the information related to a given supervised profile
[email protected]0850e842013-01-19 03:44:3153// (e.g. the installed content packs, the default URL filtering behavior, or
54// manual whitelist/blacklist overrides).
[email protected]cce15bb2014-06-17 13:43:5155class SupervisedUserService : public KeyedService,
[email protected]c14a6802014-07-11 21:51:1256#if defined(ENABLE_EXTENSIONS)
[email protected]cce15bb2014-06-17 13:43:5157 public extensions::ManagementPolicy::Provider,
[email protected]cce15bb2014-06-17 13:43:5158 public extensions::ExtensionRegistryObserver,
[email protected]c14a6802014-07-11 21:51:1259#endif
[email protected]3a276ff2014-08-12 14:22:0960 public SyncTypePreferenceProvider,
[email protected]c14a6802014-07-11 21:51:1261 public ProfileSyncServiceObserver,
[email protected]cce15bb2014-06-17 13:43:5162 public chrome::BrowserListObserver {
[email protected]0850e842013-01-19 03:44:3163 public:
[email protected]d2065e062013-12-12 23:49:5264 typedef std::vector<base::string16> CategoryList;
[email protected]438f7e12013-08-07 06:59:0065 typedef base::Callback<void(content::WebContents*)> NavigationBlockedCallback;
[email protected]514fcf22013-08-13 06:37:2466 typedef base::Callback<void(const GoogleServiceAuthError&)> AuthErrorCallback;
[email protected]0850e842013-01-19 03:44:3167
[email protected]5e022292013-02-06 16:42:1768 enum ManualBehavior {
69 MANUAL_NONE = 0,
70 MANUAL_ALLOW,
71 MANUAL_BLOCK
72 };
73
[email protected]f085fdd52014-06-11 18:09:2074 class Delegate {
75 public:
76 virtual ~Delegate() {}
77 // Returns true to indicate that the delegate handled the (de)activation, or
[email protected]cce15bb2014-06-17 13:43:5178 // false to indicate that the SupervisedUserService itself should handle it.
[email protected]f085fdd52014-06-11 18:09:2079 virtual bool SetActive(bool active) = 0;
treibdaece84f2014-09-05 12:58:1580 // Returns the path to a blacklist file to load, or an empty path to
81 // indicate "none".
82 virtual base::FilePath GetBlacklistPath() const = 0;
[email protected]f085fdd52014-06-11 18:09:2083 };
84
[email protected]cce15bb2014-06-17 13:43:5185 virtual ~SupervisedUserService();
[email protected]0850e842013-01-19 03:44:3186
[email protected]a243d644c2013-06-20 18:37:5587 // ProfileKeyedService override:
88 virtual void Shutdown() OVERRIDE;
89
[email protected]37ca3fe02013-07-05 15:32:4490 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
[email protected]0850e842013-01-19 03:44:3191
[email protected]f085fdd52014-06-11 18:09:2092 void SetDelegate(Delegate* delegate);
93
[email protected]0850e842013-01-19 03:44:3194 // Returns the URL filter for the IO thread, for filtering network requests
[email protected]cce15bb2014-06-17 13:43:5195 // (in SupervisedUserResourceThrottle).
96 scoped_refptr<const SupervisedUserURLFilter> GetURLFilterForIOThread();
[email protected]0850e842013-01-19 03:44:3197
98 // Returns the URL filter for the UI thread, for filtering navigations and
99 // classifying sites in the history view.
[email protected]cce15bb2014-06-17 13:43:51100 SupervisedUserURLFilter* GetURLFilterForUIThread();
[email protected]0850e842013-01-19 03:44:31101
102 // Returns the URL's category, obtained from the installed content packs.
103 int GetCategory(const GURL& url);
104
105 // Returns the list of all known human-readable category names, sorted by ID
106 // number. Called in the critical path of drawing the history UI, so needs to
107 // be fast.
108 void GetCategoryNames(CategoryList* list);
109
[email protected]0369d6ab2013-08-09 01:52:59110 // Whether the user can request access to blocked URLs.
111 bool AccessRequestsEnabled();
112
[email protected]4db65f952014-05-20 15:46:30113 void OnPermissionRequestIssued();
114
[email protected]e861bba2013-06-17 15:20:54115 // Adds an access request for the given URL. The requests are stored using
116 // a prefix followed by a URIEncoded version of the URL. Each entry contains
117 // a dictionary which currently has the timestamp of the request in it.
118 void AddAccessRequest(const GURL& url);
119
[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
[email protected]5e022292013-02-06 16:42:17127 // These methods allow querying and modifying the manual filtering behavior.
128 // The manual behavior is set by the user and overrides all other settings
129 // (whitelists or the default behavior).
[email protected]0850e842013-01-19 03:44:31130
[email protected]5e022292013-02-06 16:42:17131 // Returns the manual behavior for the given host.
132 ManualBehavior GetManualBehaviorForHost(const std::string& hostname);
[email protected]0850e842013-01-19 03:44:31133
[email protected]5e022292013-02-06 16:42:17134 // Returns the manual behavior for the given URL.
135 ManualBehavior GetManualBehaviorForURL(const GURL& url);
[email protected]0850e842013-01-19 03:44:31136
[email protected]43257902013-03-26 20:59:37137 // Returns all URLS on the given host that have exceptions.
138 void GetManualExceptionsForHost(const std::string& host,
139 std::vector<GURL>* urls);
140
[email protected]0850e842013-01-19 03:44:31141 // Initializes this object. This method does nothing if the profile is not
[email protected]cce15bb2014-06-17 13:43:51142 // supervised.
[email protected]0850e842013-01-19 03:44:31143 void Init();
144
[email protected]a243d644c2013-06-20 18:37:55145 // Initializes this profile for syncing, using the provided |refresh_token| to
146 // mint access tokens for Sync.
147 void InitSync(const std::string& refresh_token);
[email protected]acfcfbb2013-05-13 18:01:27148
[email protected]cce15bb2014-06-17 13:43:51149 // Convenience method that registers this supervised user using
[email protected]458d59442013-08-01 14:19:32150 // |registration_utility| and initializes sync with the returned token.
151 // The |callback| will be called when registration is complete,
[email protected]3a276ff2014-08-12 14:22:09152 // whether it succeeded or not -- unless registration was cancelled manually,
[email protected]458d59442013-08-01 14:19:32153 // in which case the callback will be ignored.
[email protected]cce15bb2014-06-17 13:43:51154 void RegisterAndInitSync(
155 SupervisedUserRegistrationUtility* registration_utility,
156 Profile* custodian_profile,
157 const std::string& supervised_user_id,
158 const AuthErrorCallback& callback);
[email protected]acfcfbb2013-05-13 18:01:27159
[email protected]4f02aac12013-05-20 05:19:06160 void set_elevated_for_testing(bool skip) {
161 elevated_for_testing_ = skip;
[email protected]849749d2013-05-06 17:30:45162 }
163
[email protected]438f7e12013-08-07 06:59:00164 void AddNavigationBlockedCallback(const NavigationBlockedCallback& callback);
165 void DidBlockNavigation(content::WebContents* web_contents);
166
[email protected]c14a6802014-07-11 21:51:12167#if defined(ENABLE_EXTENSIONS)
[email protected]ce019142013-02-12 18:08:10168 // extensions::ManagementPolicy::Provider implementation:
[email protected]0850e842013-01-19 03:44:31169 virtual std::string GetDebugPolicyProviderName() const OVERRIDE;
170 virtual bool UserMayLoad(const extensions::Extension* extension,
[email protected]0085863a2013-12-06 21:19:03171 base::string16* error) const OVERRIDE;
[email protected]0850e842013-01-19 03:44:31172 virtual bool UserMayModifySettings(const extensions::Extension* extension,
[email protected]0085863a2013-12-06 21:19:03173 base::string16* error) const OVERRIDE;
[email protected]0850e842013-01-19 03:44:31174
[email protected]f2253ac2014-05-28 08:37:21175 // extensions::ExtensionRegistryObserver implementation.
176 virtual void OnExtensionLoaded(
177 content::BrowserContext* browser_context,
178 const extensions::Extension* extension) OVERRIDE;
179 virtual void OnExtensionUnloaded(
180 content::BrowserContext* browser_context,
181 const extensions::Extension* extension,
182 extensions::UnloadedExtensionInfo::Reason reason) OVERRIDE;
[email protected]c14a6802014-07-11 21:51:12183#endif
184
[email protected]3a276ff2014-08-12 14:22:09185 // SyncTypePreferenceProvider implementation:
186 virtual syncer::ModelTypeSet GetPreferredDataTypes() const OVERRIDE;
187
[email protected]c14a6802014-07-11 21:51:12188 // ProfileSyncServiceObserver implementation:
189 virtual void OnStateChanged() OVERRIDE;
[email protected]0850e842013-01-19 03:44:31190
[email protected]dfddd022013-07-10 17:29:48191 // chrome::BrowserListObserver implementation:
192 virtual void OnBrowserSetLastActive(Browser* browser) OVERRIDE;
193
[email protected]0850e842013-01-19 03:44:31194 private:
[email protected]cce15bb2014-06-17 13:43:51195 friend class SupervisedUserServiceExtensionTestBase;
196 friend class SupervisedUserServiceFactory;
197 FRIEND_TEST_ALL_PREFIXES(SupervisedUserServiceTest, ClearOmitOnRegistration);
[email protected]0850e842013-01-19 03:44:31198
[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);
212 void LoadWhitelists(ScopedVector<SupervisedUserSiteList> site_lists);
treib4edbded2014-09-05 08:43:55213 void LoadBlacklist(const base::FilePath& path);
[email protected]5e022292013-02-06 16:42:17214 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
217 private:
treib4edbded2014-09-05 08:43:55218 void OnBlacklistLoaded();
219
[email protected]cce15bb2014-06-17 13:43:51220 // SupervisedUserURLFilter is refcounted because the IO thread filter is
221 // used both by ProfileImplIOData and OffTheRecordProfileIOData (to filter
[email protected]0850e842013-01-19 03:44:31222 // network requests), so they both keep a reference to it.
223 // Clients should not keep references to the UI thread filter, however
224 // (the filter will live as long as the profile lives, and afterwards it
225 // should not be used anymore either).
[email protected]cce15bb2014-06-17 13:43:51226 scoped_refptr<SupervisedUserURLFilter> ui_url_filter_;
227 scoped_refptr<SupervisedUserURLFilter> io_url_filter_;
[email protected]0850e842013-01-19 03:44:31228
treib4edbded2014-09-05 08:43:55229 SupervisedUserBlacklist blacklist_;
230
[email protected]0850e842013-01-19 03:44:31231 DISALLOW_COPY_AND_ASSIGN(URLFilterContext);
232 };
233
[email protected]cce15bb2014-06-17 13:43:51234 // Use |SupervisedUserServiceFactory::GetForProfile(..)| to get
[email protected]3bf45d02013-07-10 00:03:41235 // an instance of this service.
[email protected]cce15bb2014-06-17 13:43:51236 explicit SupervisedUserService(Profile* profile);
[email protected]3bf45d02013-07-10 00:03:41237
[email protected]f085fdd52014-06-11 18:09:20238 void SetActive(bool active);
239
[email protected]0085863a2013-12-06 21:19:03240 void OnCustodianProfileDownloaded(const base::string16& full_name);
[email protected]200729f82013-06-22 07:05:55241
[email protected]cce15bb2014-06-17 13:43:51242 void OnSupervisedUserRegistered(const AuthErrorCallback& callback,
243 Profile* custodian_profile,
244 const GoogleServiceAuthError& auth_error,
245 const std::string& token);
[email protected]acfcfbb2013-05-13 18:01:27246
[email protected]a243d644c2013-06-20 18:37:55247 void SetupSync();
[email protected]6e08b9a62014-07-08 00:32:48248 void StartSetupSync();
249 void FinishSetupSyncWhenReady();
250 void FinishSetupSync();
[email protected]a243d644c2013-06-20 18:37:55251
[email protected]cce15bb2014-06-17 13:43:51252 bool ProfileIsSupervised() const;
[email protected]e000daf2013-07-31 16:50:58253
[email protected]c14a6802014-07-11 21:51:12254#if defined(ENABLE_EXTENSIONS)
[email protected]0850e842013-01-19 03:44:31255 // Internal implementation for ExtensionManagementPolicy::Delegate methods.
256 // If |error| is not NULL, it will be filled with an error message if the
257 // requested extension action (install, modify status, etc.) is not permitted.
[email protected]38b7fb92013-07-26 16:32:26258 bool ExtensionManagementPolicyImpl(const extensions::Extension* extension,
[email protected]0085863a2013-12-06 21:19:03259 base::string16* error) const;
[email protected]0850e842013-01-19 03:44:31260
261 // Returns a list of all installed and enabled site lists in the current
[email protected]cce15bb2014-06-17 13:43:51262 // supervised profile.
263 ScopedVector<SupervisedUserSiteList> GetActiveSiteLists();
[email protected]0850e842013-01-19 03:44:31264
[email protected]c14a6802014-07-11 21:51:12265 // Extensions helper to SetActive().
266 void SetExtensionsActive();
267#endif
268
[email protected]cce15bb2014-06-17 13:43:51269 SupervisedUserSettingsService* GetSettingsService();
[email protected]e861bba2013-06-17 15:20:54270
[email protected]cce15bb2014-06-17 13:43:51271 void OnSupervisedUserIdChanged();
[email protected]f085fdd52014-06-11 18:09:20272
[email protected]0850e842013-01-19 03:44:31273 void OnDefaultFilteringBehaviorChanged();
274
275 void UpdateSiteLists();
276
treib4edbded2014-09-05 08:43:55277 // Asynchronously loads a static blacklist from a binary file at |path| and
278 // applies it to the URL filters.
279 void LoadBlacklist(const base::FilePath& path);
280
[email protected]5e022292013-02-06 16:42:17281 // Updates the manual overrides for hosts in the URL filters when the
282 // corresponding preference is changed.
283 void UpdateManualHosts();
[email protected]0850e842013-01-19 03:44:31284
[email protected]5e022292013-02-06 16:42:17285 // Updates the manual overrides for URLs in the URL filters when the
286 // corresponding preference is changed.
287 void UpdateManualURLs();
[email protected]0850e842013-01-19 03:44:31288
[email protected]0b4c6b22014-08-04 09:46:31289 // Returns the human readable name of the supervised user.
290 std::string GetSupervisedUserName() const;
291
[email protected]12b7af32014-03-13 05:28:20292 // Owns us via the KeyedService mechanism.
[email protected]0850e842013-01-19 03:44:31293 Profile* profile_;
294
[email protected]f085fdd52014-06-11 18:09:20295 bool active_;
296
297 Delegate* delegate_;
298
[email protected]c14a6802014-07-11 21:51:12299#if defined(ENABLE_EXTENSIONS)
[email protected]f2253ac2014-05-28 08:37:21300 ScopedObserver<extensions::ExtensionRegistry,
301 extensions::ExtensionRegistryObserver>
302 extension_registry_observer_;
[email protected]c14a6802014-07-11 21:51:12303#endif
[email protected]f2253ac2014-05-28 08:37:21304
[email protected]0850e842013-01-19 03:44:31305 PrefChangeRegistrar pref_change_registrar_;
306
[email protected]a243d644c2013-06-20 18:37:55307 // True iff we're waiting for the Sync service to be initialized.
308 bool waiting_for_sync_initialization_;
[email protected]dfddd022013-07-10 17:29:48309 bool is_profile_active_;
[email protected]a243d644c2013-06-20 18:37:55310
[email protected]438f7e12013-08-07 06:59:00311 std::vector<NavigationBlockedCallback> navigation_blocked_callbacks_;
312
[email protected]4f02aac12013-05-20 05:19:06313 // Sets a profile in elevated state for testing if set to true.
314 bool elevated_for_testing_;
[email protected]849749d2013-05-06 17:30:45315
[email protected]3a276ff2014-08-12 14:22:09316 // True only when |Init()| method has been called.
317 bool did_init_;
318
[email protected]3bf45d02013-07-10 00:03:41319 // True only when |Shutdown()| method has been called.
320 bool did_shutdown_;
321
[email protected]0850e842013-01-19 03:44:31322 URLFilterContext url_filter_context_;
[email protected]8052b242013-11-15 16:40:55323
[email protected]4db65f952014-05-20 15:46:30324 // Used to create permission requests.
325 scoped_ptr<PermissionRequestCreator> permissions_creator_;
326
327 // True iff we are waiting for a permission request to be issued.
328 bool waiting_for_permissions_;
329
[email protected]cce15bb2014-06-17 13:43:51330 base::WeakPtrFactory<SupervisedUserService> weak_ptr_factory_;
[email protected]0850e842013-01-19 03:44:31331};
332
[email protected]cce15bb2014-06-17 13:43:51333#endif // CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SERVICE_H_