blob: 7bbdfedefab7adc3b972c779ed10fd6ed1ed7fbe [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"
[email protected]cce15bb2014-06-17 13:43:5117#include "chrome/browser/supervised_user/supervised_user_url_filter.h"
18#include "chrome/browser/supervised_user/supervised_users.h"
[email protected]a243d644c2013-06-20 18:37:5519#include "chrome/browser/sync/profile_sync_service_observer.h"
[email protected]3a276ff2014-08-12 14:22:0920#include "chrome/browser/sync/sync_type_preference_provider.h"
[email protected]514fcf22013-08-13 06:37:2421#include "chrome/browser/ui/browser_list_observer.h"
[email protected]12b7af32014-03-13 05:28:2022#include "components/keyed_service/core/keyed_service.h"
[email protected]bfd8cf982013-03-04 15:24:0023#include "content/public/browser/web_contents.h"
[email protected]c14a6802014-07-11 21:51:1224
25#if defined(ENABLE_EXTENSIONS)
[email protected]f2253ac2014-05-28 08:37:2126#include "extensions/browser/extension_registry_observer.h"
[email protected]301116c62013-11-26 10:37:4527#include "extensions/browser/management_policy.h"
[email protected]c14a6802014-07-11 21:51:1228#endif
[email protected]0850e842013-01-19 03:44:3129
[email protected]509ad1a92013-03-19 21:41:0630class Browser;
[email protected]acfcfbb2013-05-13 18:01:2731class GoogleServiceAuthError;
[email protected]4db65f952014-05-20 15:46:3032class PermissionRequestCreator;
[email protected]0850e842013-01-19 03:44:3133class Profile;
[email protected]cce15bb2014-06-17 13:43:5134class SupervisedUserRegistrationUtility;
35class SupervisedUserSettingsService;
36class SupervisedUserSiteList;
37class SupervisedUserURLFilter;
[email protected]0850e842013-01-19 03:44:3138
[email protected]f2253ac2014-05-28 08:37:2139namespace extensions {
40class ExtensionRegistry;
41}
42
[email protected]443e9312013-05-06 06:17:3443namespace user_prefs {
44class PrefRegistrySyncable;
45}
46
[email protected]cce15bb2014-06-17 13:43:5147// This class handles all the information related to a given supervised profile
[email protected]0850e842013-01-19 03:44:3148// (e.g. the installed content packs, the default URL filtering behavior, or
49// manual whitelist/blacklist overrides).
[email protected]cce15bb2014-06-17 13:43:5150class SupervisedUserService : public KeyedService,
[email protected]c14a6802014-07-11 21:51:1251#if defined(ENABLE_EXTENSIONS)
[email protected]cce15bb2014-06-17 13:43:5152 public extensions::ManagementPolicy::Provider,
[email protected]cce15bb2014-06-17 13:43:5153 public extensions::ExtensionRegistryObserver,
[email protected]c14a6802014-07-11 21:51:1254#endif
[email protected]3a276ff2014-08-12 14:22:0955 public SyncTypePreferenceProvider,
[email protected]c14a6802014-07-11 21:51:1256 public ProfileSyncServiceObserver,
[email protected]cce15bb2014-06-17 13:43:5157 public chrome::BrowserListObserver {
[email protected]0850e842013-01-19 03:44:3158 public:
[email protected]d2065e062013-12-12 23:49:5259 typedef std::vector<base::string16> CategoryList;
[email protected]438f7e12013-08-07 06:59:0060 typedef base::Callback<void(content::WebContents*)> NavigationBlockedCallback;
[email protected]514fcf22013-08-13 06:37:2461 typedef base::Callback<void(const GoogleServiceAuthError&)> AuthErrorCallback;
[email protected]0850e842013-01-19 03:44:3162
[email protected]5e022292013-02-06 16:42:1763 enum ManualBehavior {
64 MANUAL_NONE = 0,
65 MANUAL_ALLOW,
66 MANUAL_BLOCK
67 };
68
[email protected]f085fdd52014-06-11 18:09:2069 class Delegate {
70 public:
71 virtual ~Delegate() {}
72 // Returns true to indicate that the delegate handled the (de)activation, or
[email protected]cce15bb2014-06-17 13:43:5173 // false to indicate that the SupervisedUserService itself should handle it.
[email protected]f085fdd52014-06-11 18:09:2074 virtual bool SetActive(bool active) = 0;
75 };
76
[email protected]cce15bb2014-06-17 13:43:5177 virtual ~SupervisedUserService();
[email protected]0850e842013-01-19 03:44:3178
[email protected]a243d644c2013-06-20 18:37:5579 // ProfileKeyedService override:
80 virtual void Shutdown() OVERRIDE;
81
[email protected]37ca3fe02013-07-05 15:32:4482 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
[email protected]0850e842013-01-19 03:44:3183
[email protected]f085fdd52014-06-11 18:09:2084 void SetDelegate(Delegate* delegate);
85
[email protected]0850e842013-01-19 03:44:3186 // Returns the URL filter for the IO thread, for filtering network requests
[email protected]cce15bb2014-06-17 13:43:5187 // (in SupervisedUserResourceThrottle).
88 scoped_refptr<const SupervisedUserURLFilter> GetURLFilterForIOThread();
[email protected]0850e842013-01-19 03:44:3189
90 // Returns the URL filter for the UI thread, for filtering navigations and
91 // classifying sites in the history view.
[email protected]cce15bb2014-06-17 13:43:5192 SupervisedUserURLFilter* GetURLFilterForUIThread();
[email protected]0850e842013-01-19 03:44:3193
94 // Returns the URL's category, obtained from the installed content packs.
95 int GetCategory(const GURL& url);
96
97 // Returns the list of all known human-readable category names, sorted by ID
98 // number. Called in the critical path of drawing the history UI, so needs to
99 // be fast.
100 void GetCategoryNames(CategoryList* list);
101
[email protected]0369d6ab2013-08-09 01:52:59102 // Whether the user can request access to blocked URLs.
103 bool AccessRequestsEnabled();
104
[email protected]4db65f952014-05-20 15:46:30105 void OnPermissionRequestIssued();
106
[email protected]e861bba2013-06-17 15:20:54107 // Adds an access request for the given URL. The requests are stored using
108 // a prefix followed by a URIEncoded version of the URL. Each entry contains
109 // a dictionary which currently has the timestamp of the request in it.
110 void AddAccessRequest(const GURL& url);
111
[email protected]a9c2d642013-05-31 14:37:14112 // Returns the email address of the custodian.
113 std::string GetCustodianEmailAddress() const;
114
[email protected]fae057a2013-06-21 22:46:08115 // Returns the name of the custodian, or the email address if the name is
116 // empty.
117 std::string GetCustodianName() const;
118
[email protected]5e022292013-02-06 16:42:17119 // These methods allow querying and modifying the manual filtering behavior.
120 // The manual behavior is set by the user and overrides all other settings
121 // (whitelists or the default behavior).
[email protected]0850e842013-01-19 03:44:31122
[email protected]5e022292013-02-06 16:42:17123 // Returns the manual behavior for the given host.
124 ManualBehavior GetManualBehaviorForHost(const std::string& hostname);
[email protected]0850e842013-01-19 03:44:31125
[email protected]5e022292013-02-06 16:42:17126 // Returns the manual behavior for the given URL.
127 ManualBehavior GetManualBehaviorForURL(const GURL& url);
[email protected]0850e842013-01-19 03:44:31128
[email protected]43257902013-03-26 20:59:37129 // Returns all URLS on the given host that have exceptions.
130 void GetManualExceptionsForHost(const std::string& host,
131 std::vector<GURL>* urls);
132
[email protected]0850e842013-01-19 03:44:31133 // Initializes this object. This method does nothing if the profile is not
[email protected]cce15bb2014-06-17 13:43:51134 // supervised.
[email protected]0850e842013-01-19 03:44:31135 void Init();
136
[email protected]a243d644c2013-06-20 18:37:55137 // Initializes this profile for syncing, using the provided |refresh_token| to
138 // mint access tokens for Sync.
139 void InitSync(const std::string& refresh_token);
[email protected]acfcfbb2013-05-13 18:01:27140
[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);
[email protected]acfcfbb2013-05-13 18:01:27151
[email protected]4f02aac12013-05-20 05:19:06152 void set_elevated_for_testing(bool skip) {
153 elevated_for_testing_ = skip;
[email protected]849749d2013-05-06 17:30:45154 }
155
[email protected]438f7e12013-08-07 06:59:00156 void AddNavigationBlockedCallback(const NavigationBlockedCallback& callback);
157 void DidBlockNavigation(content::WebContents* web_contents);
158
[email protected]c14a6802014-07-11 21:51:12159#if defined(ENABLE_EXTENSIONS)
[email protected]ce019142013-02-12 18:08:10160 // extensions::ManagementPolicy::Provider implementation:
[email protected]0850e842013-01-19 03:44:31161 virtual std::string GetDebugPolicyProviderName() const OVERRIDE;
162 virtual bool UserMayLoad(const extensions::Extension* extension,
[email protected]0085863a2013-12-06 21:19:03163 base::string16* error) const OVERRIDE;
[email protected]0850e842013-01-19 03:44:31164 virtual bool UserMayModifySettings(const extensions::Extension* extension,
[email protected]0085863a2013-12-06 21:19:03165 base::string16* error) const OVERRIDE;
[email protected]0850e842013-01-19 03:44:31166
[email protected]f2253ac2014-05-28 08:37:21167 // extensions::ExtensionRegistryObserver implementation.
168 virtual void OnExtensionLoaded(
169 content::BrowserContext* browser_context,
170 const extensions::Extension* extension) OVERRIDE;
171 virtual void OnExtensionUnloaded(
172 content::BrowserContext* browser_context,
173 const extensions::Extension* extension,
174 extensions::UnloadedExtensionInfo::Reason reason) OVERRIDE;
[email protected]c14a6802014-07-11 21:51:12175#endif
176
[email protected]3a276ff2014-08-12 14:22:09177 // SyncTypePreferenceProvider implementation:
178 virtual syncer::ModelTypeSet GetPreferredDataTypes() const OVERRIDE;
179
[email protected]c14a6802014-07-11 21:51:12180 // ProfileSyncServiceObserver implementation:
181 virtual void OnStateChanged() OVERRIDE;
[email protected]0850e842013-01-19 03:44:31182
[email protected]dfddd022013-07-10 17:29:48183 // chrome::BrowserListObserver implementation:
184 virtual void OnBrowserSetLastActive(Browser* browser) OVERRIDE;
185
[email protected]0850e842013-01-19 03:44:31186 private:
[email protected]cce15bb2014-06-17 13:43:51187 friend class SupervisedUserServiceExtensionTestBase;
188 friend class SupervisedUserServiceFactory;
189 FRIEND_TEST_ALL_PREFIXES(SupervisedUserServiceTest, ClearOmitOnRegistration);
[email protected]0850e842013-01-19 03:44:31190
[email protected]cce15bb2014-06-17 13:43:51191 // A bridge from the UI thread to the SupervisedUserURLFilters, one of which
192 // lives on the IO thread. This class mediates access to them and makes sure
193 // they are kept in sync.
[email protected]0850e842013-01-19 03:44:31194 class URLFilterContext {
195 public:
196 URLFilterContext();
197 ~URLFilterContext();
198
[email protected]cce15bb2014-06-17 13:43:51199 SupervisedUserURLFilter* ui_url_filter() const;
200 SupervisedUserURLFilter* io_url_filter() const;
[email protected]0850e842013-01-19 03:44:31201
202 void SetDefaultFilteringBehavior(
[email protected]cce15bb2014-06-17 13:43:51203 SupervisedUserURLFilter::FilteringBehavior behavior);
204 void LoadWhitelists(ScopedVector<SupervisedUserSiteList> site_lists);
[email protected]5e022292013-02-06 16:42:17205 void SetManualHosts(scoped_ptr<std::map<std::string, bool> > host_map);
206 void SetManualURLs(scoped_ptr<std::map<GURL, bool> > url_map);
[email protected]0850e842013-01-19 03:44:31207
208 private:
[email protected]cce15bb2014-06-17 13:43:51209 // SupervisedUserURLFilter is refcounted because the IO thread filter is
210 // used both by ProfileImplIOData and OffTheRecordProfileIOData (to filter
[email protected]0850e842013-01-19 03:44:31211 // network requests), so they both keep a reference to it.
212 // Clients should not keep references to the UI thread filter, however
213 // (the filter will live as long as the profile lives, and afterwards it
214 // should not be used anymore either).
[email protected]cce15bb2014-06-17 13:43:51215 scoped_refptr<SupervisedUserURLFilter> ui_url_filter_;
216 scoped_refptr<SupervisedUserURLFilter> io_url_filter_;
[email protected]0850e842013-01-19 03:44:31217
218 DISALLOW_COPY_AND_ASSIGN(URLFilterContext);
219 };
220
[email protected]cce15bb2014-06-17 13:43:51221 // Use |SupervisedUserServiceFactory::GetForProfile(..)| to get
[email protected]3bf45d02013-07-10 00:03:41222 // an instance of this service.
[email protected]cce15bb2014-06-17 13:43:51223 explicit SupervisedUserService(Profile* profile);
[email protected]3bf45d02013-07-10 00:03:41224
[email protected]f085fdd52014-06-11 18:09:20225 void SetActive(bool active);
226
[email protected]0085863a2013-12-06 21:19:03227 void OnCustodianProfileDownloaded(const base::string16& full_name);
[email protected]200729f82013-06-22 07:05:55228
[email protected]cce15bb2014-06-17 13:43:51229 void OnSupervisedUserRegistered(const AuthErrorCallback& callback,
230 Profile* custodian_profile,
231 const GoogleServiceAuthError& auth_error,
232 const std::string& token);
[email protected]acfcfbb2013-05-13 18:01:27233
[email protected]a243d644c2013-06-20 18:37:55234 void SetupSync();
[email protected]6e08b9a62014-07-08 00:32:48235 void StartSetupSync();
236 void FinishSetupSyncWhenReady();
237 void FinishSetupSync();
[email protected]a243d644c2013-06-20 18:37:55238
[email protected]cce15bb2014-06-17 13:43:51239 bool ProfileIsSupervised() const;
[email protected]e000daf2013-07-31 16:50:58240
[email protected]c14a6802014-07-11 21:51:12241#if defined(ENABLE_EXTENSIONS)
[email protected]0850e842013-01-19 03:44:31242 // Internal implementation for ExtensionManagementPolicy::Delegate methods.
243 // If |error| is not NULL, it will be filled with an error message if the
244 // requested extension action (install, modify status, etc.) is not permitted.
[email protected]38b7fb92013-07-26 16:32:26245 bool ExtensionManagementPolicyImpl(const extensions::Extension* extension,
[email protected]0085863a2013-12-06 21:19:03246 base::string16* error) const;
[email protected]0850e842013-01-19 03:44:31247
248 // Returns a list of all installed and enabled site lists in the current
[email protected]cce15bb2014-06-17 13:43:51249 // supervised profile.
250 ScopedVector<SupervisedUserSiteList> GetActiveSiteLists();
[email protected]0850e842013-01-19 03:44:31251
[email protected]c14a6802014-07-11 21:51:12252 // Extensions helper to SetActive().
253 void SetExtensionsActive();
254#endif
255
[email protected]cce15bb2014-06-17 13:43:51256 SupervisedUserSettingsService* GetSettingsService();
[email protected]e861bba2013-06-17 15:20:54257
[email protected]cce15bb2014-06-17 13:43:51258 void OnSupervisedUserIdChanged();
[email protected]f085fdd52014-06-11 18:09:20259
[email protected]0850e842013-01-19 03:44:31260 void OnDefaultFilteringBehaviorChanged();
261
262 void UpdateSiteLists();
263
[email protected]5e022292013-02-06 16:42:17264 // Updates the manual overrides for hosts in the URL filters when the
265 // corresponding preference is changed.
266 void UpdateManualHosts();
[email protected]0850e842013-01-19 03:44:31267
[email protected]5e022292013-02-06 16:42:17268 // Updates the manual overrides for URLs in the URL filters when the
269 // corresponding preference is changed.
270 void UpdateManualURLs();
[email protected]0850e842013-01-19 03:44:31271
[email protected]0b4c6b22014-08-04 09:46:31272 // Returns the human readable name of the supervised user.
273 std::string GetSupervisedUserName() const;
274
[email protected]12b7af32014-03-13 05:28:20275 // Owns us via the KeyedService mechanism.
[email protected]0850e842013-01-19 03:44:31276 Profile* profile_;
277
[email protected]f085fdd52014-06-11 18:09:20278 bool active_;
279
280 Delegate* delegate_;
281
[email protected]c14a6802014-07-11 21:51:12282#if defined(ENABLE_EXTENSIONS)
[email protected]f2253ac2014-05-28 08:37:21283 ScopedObserver<extensions::ExtensionRegistry,
284 extensions::ExtensionRegistryObserver>
285 extension_registry_observer_;
[email protected]c14a6802014-07-11 21:51:12286#endif
[email protected]f2253ac2014-05-28 08:37:21287
[email protected]0850e842013-01-19 03:44:31288 PrefChangeRegistrar pref_change_registrar_;
289
[email protected]a243d644c2013-06-20 18:37:55290 // True iff we're waiting for the Sync service to be initialized.
291 bool waiting_for_sync_initialization_;
[email protected]dfddd022013-07-10 17:29:48292 bool is_profile_active_;
[email protected]a243d644c2013-06-20 18:37:55293
[email protected]438f7e12013-08-07 06:59:00294 std::vector<NavigationBlockedCallback> navigation_blocked_callbacks_;
295
[email protected]4f02aac12013-05-20 05:19:06296 // Sets a profile in elevated state for testing if set to true.
297 bool elevated_for_testing_;
[email protected]849749d2013-05-06 17:30:45298
[email protected]3a276ff2014-08-12 14:22:09299 // True only when |Init()| method has been called.
300 bool did_init_;
301
[email protected]3bf45d02013-07-10 00:03:41302 // True only when |Shutdown()| method has been called.
303 bool did_shutdown_;
304
[email protected]0850e842013-01-19 03:44:31305 URLFilterContext url_filter_context_;
[email protected]8052b242013-11-15 16:40:55306
[email protected]4db65f952014-05-20 15:46:30307 // Used to create permission requests.
308 scoped_ptr<PermissionRequestCreator> permissions_creator_;
309
310 // True iff we are waiting for a permission request to be issued.
311 bool waiting_for_permissions_;
312
[email protected]cce15bb2014-06-17 13:43:51313 base::WeakPtrFactory<SupervisedUserService> weak_ptr_factory_;
[email protected]0850e842013-01-19 03:44:31314};
315
[email protected]cce15bb2014-06-17 13:43:51316#endif // CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SERVICE_H_