blob: d9b83f2a4cac4212386c37772bc520cac41bdf81 [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
avi664c07b2015-12-26 02:18:318#include <stddef.h>
9
treib950c6e82014-09-23 13:07:2910#include <map>
dchengf624e472016-04-12 08:33:1711#include <memory>
treib950c6e82014-09-23 13:07:2912#include <string>
[email protected]0850e842013-01-19 03:44:3113#include <vector>
14
[email protected]438f7e12013-08-07 06:59:0015#include "base/callback.h"
[email protected]076ebeda2014-06-06 21:47:2616#include "base/gtest_prod_util.h"
avi664c07b2015-12-26 02:18:3117#include "base/macros.h"
treibab0a39e2014-09-24 14:48:2818#include "base/observer_list.h"
[email protected]f2253ac2014-05-28 08:37:2119#include "base/scoped_observer.h"
[email protected]f9b294362013-06-10 20:22:3120#include "base/strings/string16.h"
avi664c07b2015-12-26 02:18:3121#include "build/build_config.h"
treibf38cc252016-04-07 14:44:1122#include "chrome/browser/net/file_downloader.h"
atanasovaac676032016-04-05 16:31:0523#include "chrome/browser/supervised_user/experimental/safe_search_url_reporter.h"
treib4edbded2014-09-05 08:43:5524#include "chrome/browser/supervised_user/experimental/supervised_user_blacklist.h"
[email protected]cce15bb2014-06-17 13:43:5125#include "chrome/browser/supervised_user/supervised_user_url_filter.h"
26#include "chrome/browser/supervised_user/supervised_users.h"
[email protected]514fcf22013-08-13 06:37:2427#include "chrome/browser/ui/browser_list_observer.h"
[email protected]12b7af32014-03-13 05:28:2028#include "components/keyed_service/core/keyed_service.h"
brettwb1fc1b82016-02-02 00:19:0829#include "components/prefs/pref_change_registrar.h"
maxbogue455a57e32016-08-14 00:08:3230#include "components/sync/driver/sync_type_preference_provider.h"
Scott Violetc8240b02018-03-08 22:03:5931#include "extensions/buildflags/buildflags.h"
[email protected]c14a6802014-07-11 21:51:1232
brettw00899e62016-11-12 02:10:1733#if BUILDFLAG(ENABLE_EXTENSIONS)
mamire9609642016-06-28 22:17:5434#include "extensions/browser/extension_registry_observer.h"
[email protected]301116c62013-11-26 10:37:4535#include "extensions/browser/management_policy.h"
[email protected]c14a6802014-07-11 21:51:1236#endif
[email protected]0850e842013-01-19 03:44:3137
[email protected]509ad1a92013-03-19 21:41:0638class Browser;
[email protected]4db65f952014-05-20 15:46:3039class PermissionRequestCreator;
[email protected]0850e842013-01-19 03:44:3140class Profile;
treibab0a39e2014-09-24 14:48:2841class SupervisedUserServiceObserver;
[email protected]cce15bb2014-06-17 13:43:5142class SupervisedUserSettingsService;
43class SupervisedUserSiteList;
44class SupervisedUserURLFilter;
bauerb4da36132014-12-26 19:53:1345class SupervisedUserWhitelistService;
[email protected]0850e842013-01-19 03:44:3146
treibdaece84f2014-09-05 12:58:1547namespace base {
48class FilePath;
treibf832a992015-03-24 18:09:2449class Version;
treibdaece84f2014-09-05 12:58:1550}
51
[email protected]f2253ac2014-05-28 08:37:2152namespace extensions {
53class ExtensionRegistry;
54}
55
[email protected]443e9312013-05-06 06:17:3456namespace user_prefs {
57class PrefRegistrySyncable;
58}
59
[email protected]cce15bb2014-06-17 13:43:5160// This class handles all the information related to a given supervised profile
[email protected]0850e842013-01-19 03:44:3161// (e.g. the installed content packs, the default URL filtering behavior, or
62// manual whitelist/blacklist overrides).
[email protected]cce15bb2014-06-17 13:43:5163class SupervisedUserService : public KeyedService,
brettw00899e62016-11-12 02:10:1764#if BUILDFLAG(ENABLE_EXTENSIONS)
mamire9609642016-06-28 22:17:5465 public extensions::ExtensionRegistryObserver,
[email protected]cce15bb2014-06-17 13:43:5166 public extensions::ManagementPolicy::Provider,
[email protected]c14a6802014-07-11 21:51:1267#endif
maxbogue7e006db2016-10-03 19:48:2868 public syncer::SyncTypePreferenceProvider,
jam1c5a91492016-02-24 20:47:5369#if !defined(OS_ANDROID)
cm.sanchia0d6add2017-12-13 04:59:3370 public BrowserListObserver,
bauerb5f8cda92015-10-07 15:36:4471#endif
bauerbce9a1a82014-12-18 13:34:2472 public SupervisedUserURLFilter::Observer {
[email protected]0850e842013-01-19 03:44:3173 public:
Carlos IL6b784a62018-03-20 00:26:4974 using SuccessCallback = base::OnceCallback<void(bool)>;
[email protected]0850e842013-01-19 03:44:3175
[email protected]f085fdd52014-06-11 18:09:2076 class Delegate {
77 public:
78 virtual ~Delegate() {}
79 // Returns true to indicate that the delegate handled the (de)activation, or
[email protected]cce15bb2014-06-17 13:43:5180 // false to indicate that the SupervisedUserService itself should handle it.
[email protected]f085fdd52014-06-11 18:09:2081 virtual bool SetActive(bool active) = 0;
82 };
83
dchengc072fff2014-10-21 11:39:0584 ~SupervisedUserService() override;
[email protected]0850e842013-01-19 03:44:3185
[email protected]37ca3fe02013-07-05 15:32:4486 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
[email protected]0850e842013-01-19 03:44:3187
bauerb5f8cda92015-10-07 15:36:4488 // Initializes this object.
89 void Init();
90
[email protected]f085fdd52014-06-11 18:09:2091 void SetDelegate(Delegate* delegate);
92
mmenkedb2637ff2017-03-30 23:59:4293 // Returns the URL filter for filtering navigations and classifying sites in
94 // the history view. Both this method and the returned filter may only be used
95 // on the UI thread.
96 SupervisedUserURLFilter* GetURLFilter();
[email protected]0850e842013-01-19 03:44:3197
bauerb4da36132014-12-26 19:53:1398 // Returns the whitelist service.
99 SupervisedUserWhitelistService* GetWhitelistService();
100
atanasova9572aaf2016-02-26 18:08:26101 const std::vector<scoped_refptr<SupervisedUserSiteList>>& whitelists() const {
102 return whitelists_;
103 }
104
treib8ecc1eb52015-03-04 18:29:06105 // Whether the user can request to get access to blocked URLs or to new
106 // extensions.
[email protected]0369d6ab2013-08-09 01:52:59107 bool AccessRequestsEnabled();
108
treib8ecc1eb52015-03-04 18:29:06109 // Adds an access request for the given URL.
Carlos IL6b784a62018-03-20 00:26:49110 void AddURLAccessRequest(const GURL& url, SuccessCallback callback);
treib8ecc1eb52015-03-04 18:29:06111
atanasovaac676032016-04-05 16:31:05112 // Reports |url| to the SafeSearch API, because the user thinks this is an
113 // inappropriate URL.
Carlos IL6b784a62018-03-20 00:26:49114 void ReportURL(const GURL& url, SuccessCallback callback);
atanasovaac676032016-04-05 16:31:05115
mamire9609642016-06-28 22:17:54116 // Adds an install request for the given WebStore item (App/Extension).
117 void AddExtensionInstallRequest(const std::string& extension_id,
118 const base::Version& version,
Carlos IL6b784a62018-03-20 00:26:49119 SuccessCallback callback);
mamire9609642016-06-28 22:17:54120
121 // Same as above, but without a callback, just logging errors on failure.
122 void AddExtensionInstallRequest(const std::string& extension_id,
123 const base::Version& version);
124
treib8ecc1eb52015-03-04 18:29:06125 // Adds an update request for the given WebStore item (App/Extension).
126 void AddExtensionUpdateRequest(const std::string& extension_id,
treibf832a992015-03-24 18:09:24127 const base::Version& version,
Carlos IL6b784a62018-03-20 00:26:49128 SuccessCallback callback);
[email protected]e861bba2013-06-17 15:20:54129
treib40d3ad92015-10-20 18:15:42130 // Same as above, but without a callback, just logging errors on failure.
131 void AddExtensionUpdateRequest(const std::string& extension_id,
132 const base::Version& version);
133
mamire9609642016-06-28 22:17:54134 // Get the string used to identify an extension install or update request.
135 // Public for testing.
136 static std::string GetExtensionRequestId(const std::string& extension_id,
137 const base::Version& version);
treib40d3ad92015-10-20 18:15:42138
[email protected]a9c2d642013-05-31 14:37:14139 // Returns the email address of the custodian.
140 std::string GetCustodianEmailAddress() const;
141
[email protected]fae057a2013-06-21 22:46:08142 // Returns the name of the custodian, or the email address if the name is
143 // empty.
144 std::string GetCustodianName() const;
145
treib3e7ecf72014-10-24 16:44:07146 // Returns the email address of the second custodian, or the empty string
147 // if there is no second custodian.
148 std::string GetSecondCustodianEmailAddress() const;
149
150 // Returns the name of the second custodian, or the email address if the name
151 // is empty, or the empty string is there is no second custodian.
152 std::string GetSecondCustodianName() const;
153
treib2170ea02015-10-13 14:55:12154 // Returns a message saying that extensions can only be modified by the
155 // custodian.
156 base::string16 GetExtensionsLockedMessage() const;
157
jam1c5a91492016-02-24 20:47:53158#if !defined(OS_ANDROID)
[email protected]a243d644c2013-06-20 18:37:55159 // Initializes this profile for syncing, using the provided |refresh_token| to
160 // mint access tokens for Sync.
161 void InitSync(const std::string& refresh_token);
thestig1b76f1a2015-09-30 22:52:38162#endif
[email protected]acfcfbb2013-05-13 18:01:27163
treibab0a39e2014-09-24 14:48:28164 void AddObserver(SupervisedUserServiceObserver* observer);
165 void RemoveObserver(SupervisedUserServiceObserver* observer);
166
treib531fc7312014-12-09 12:49:20167 void AddPermissionRequestCreator(
dchengf624e472016-04-12 08:33:17168 std::unique_ptr<PermissionRequestCreator> creator);
bauerb646019b12014-10-16 16:23:09169
dchengf624e472016-04-12 08:33:17170 void SetSafeSearchURLReporter(
171 std::unique_ptr<SafeSearchURLReporter> reporter);
atanasovaac676032016-04-05 16:31:05172
bauerb5f8cda92015-10-07 15:36:44173 // ProfileKeyedService override:
174 void Shutdown() override;
175
[email protected]3a276ff2014-08-12 14:22:09176 // SyncTypePreferenceProvider implementation:
Maksim Moskvitin35f598b2019-04-18 11:47:00177 syncer::UserSelectableTypeSet GetForcedTypes() const override;
Marc Treibb3bbf1d22019-03-27 15:45:39178 bool IsEncryptEverythingAllowed() const override;
[email protected]3a276ff2014-08-12 14:22:09179
jam1c5a91492016-02-24 20:47:53180#if !defined(OS_ANDROID)
cm.sanchia0d6add2017-12-13 04:59:33181 // BrowserListObserver implementation:
dchengc072fff2014-10-21 11:39:05182 void OnBrowserSetLastActive(Browser* browser) override;
jam1c5a91492016-02-24 20:47:53183#endif // !defined(OS_ANDROID)
[email protected]dfddd022013-07-10 17:29:48184
bauerbce9a1a82014-12-18 13:34:24185 // SupervisedUserURLFilter::Observer implementation:
186 void OnSiteListUpdated() override;
187
[email protected]0850e842013-01-19 03:44:31188 private:
[email protected]cce15bb2014-06-17 13:43:51189 friend class SupervisedUserServiceExtensionTestBase;
190 friend class SupervisedUserServiceFactory;
mamire9609642016-06-28 22:17:54191 FRIEND_TEST_ALL_PREFIXES(
192 SupervisedUserServiceExtensionTest,
193 ExtensionManagementPolicyProviderWithoutSUInitiatedInstalls);
194 FRIEND_TEST_ALL_PREFIXES(
195 SupervisedUserServiceExtensionTest,
196 ExtensionManagementPolicyProviderWithSUInitiatedInstalls);
bauerb95a50682015-01-07 17:04:15197
treib8ecc1eb52015-03-04 18:29:06198 using CreatePermissionRequestCallback =
Carlos IL6b784a62018-03-20 00:26:49199 base::RepeatingCallback<void(PermissionRequestCreator*, SuccessCallback)>;
treib8ecc1eb52015-03-04 18:29:06200
[email protected]cce15bb2014-06-17 13:43:51201 // Use |SupervisedUserServiceFactory::GetForProfile(..)| to get
[email protected]3bf45d02013-07-10 00:03:41202 // an instance of this service.
[email protected]cce15bb2014-06-17 13:43:51203 explicit SupervisedUserService(Profile* profile);
[email protected]3bf45d02013-07-10 00:03:41204
[email protected]f085fdd52014-06-11 18:09:20205 void SetActive(bool active);
206
[email protected]cce15bb2014-06-17 13:43:51207 bool ProfileIsSupervised() const;
[email protected]e000daf2013-07-31 16:50:58208
treib3fce4e92014-09-26 16:06:25209 void OnCustodianInfoChanged();
210
brettw00899e62016-11-12 02:10:17211#if BUILDFLAG(ENABLE_EXTENSIONS)
treibbb9a1962015-02-25 13:40:59212 // extensions::ManagementPolicy::Provider implementation:
213 std::string GetDebugPolicyProviderName() const override;
214 bool UserMayLoad(const extensions::Extension* extension,
215 base::string16* error) const override;
treib755c12d2015-03-30 08:46:25216 bool UserMayModifySettings(const extensions::Extension* extension,
217 base::string16* error) const override;
treibbb9a1962015-02-25 13:40:59218 bool MustRemainInstalled(const extensions::Extension* extension,
219 base::string16* error) const override;
mamire9609642016-06-28 22:17:54220 bool MustRemainDisabled(const extensions::Extension* extension,
Minh X. Nguyen45479012017-08-18 21:35:36221 extensions::disable_reason::DisableReason* reason,
mamire9609642016-06-28 22:17:54222 base::string16* error) const override;
223
224 // extensions::ExtensionRegistryObserver overrides:
225 void OnExtensionInstalled(content::BrowserContext* browser_context,
226 const extensions::Extension* extension,
227 bool is_update) override;
[email protected]0850e842013-01-19 03:44:31228
mamir192d7882016-06-22 17:10:16229 // An extension can be in one of the following states:
230 //
231 // FORCED: if it is installed by the custodian.
mamire9609642016-06-28 22:17:54232 // REQUIRE_APPROVAL: if it is installed by the supervised user and
233 // hasn't been approved by the custodian yet.
mamir192d7882016-06-22 17:10:16234 // ALLOWED: Components, Themes, Default extensions ..etc
235 // are generally allowed. Extensions that have been approved by the
236 // custodian are also allowed.
237 // BLOCKED: if it is not ALLOWED or FORCED
238 // and supervised users initiated installs are disabled.
mamire9609642016-06-28 22:17:54239 enum class ExtensionState { FORCED, BLOCKED, ALLOWED, REQUIRE_APPROVAL };
mamir192d7882016-06-22 17:10:16240
mamire9609642016-06-28 22:17:54241 // Returns the state of an extension whether being FORCED, BLOCKED, ALLOWED or
242 // REQUIRE_APPROVAL from the Supervised User service's point of view.
mamir192d7882016-06-22 17:10:16243 ExtensionState GetExtensionState(
mamire9609642016-06-28 22:17:54244 const extensions::Extension& extension) const;
mamir192d7882016-06-22 17:10:16245
[email protected]c14a6802014-07-11 21:51:12246 // Extensions helper to SetActive().
247 void SetExtensionsActive();
mamire9609642016-06-28 22:17:54248
249 // Enables/Disables extensions upon change in approved version of the
250 // extension_id.
251 void ChangeExtensionStateIfNecessary(const std::string& extension_id);
252
253 // Updates the map of approved extensions when the corresponding preference
254 // is changed.
255 void UpdateApprovedExtensions();
[email protected]c14a6802014-07-11 21:51:12256#endif
257
[email protected]cce15bb2014-06-17 13:43:51258 SupervisedUserSettingsService* GetSettingsService();
[email protected]e861bba2013-06-17 15:20:54259
bauerbd3a36cc42014-10-01 13:05:49260 size_t FindEnabledPermissionRequestCreator(size_t start);
treib8ecc1eb52015-03-04 18:29:06261 void AddPermissionRequestInternal(
262 const CreatePermissionRequestCallback& create_request,
Carlos IL6b784a62018-03-20 00:26:49263 SuccessCallback callback,
treib8ecc1eb52015-03-04 18:29:06264 size_t index);
265 void OnPermissionRequestIssued(
266 const CreatePermissionRequestCallback& create_request,
Carlos IL6b784a62018-03-20 00:26:49267 SuccessCallback callback,
treib8ecc1eb52015-03-04 18:29:06268 size_t index,
269 bool success);
bauerbd3a36cc42014-10-01 13:05:49270
[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
treib9cc1b112016-01-08 10:08:01275 void OnSafeSitesSettingChanged();
276
bauerb95a50682015-01-07 17:04:15277 void OnSiteListsChanged(
278 const std::vector<scoped_refptr<SupervisedUserSiteList>>& site_lists);
[email protected]0850e842013-01-19 03:44:31279
treib2fd187392015-04-16 17:19:38280 // Asynchronously loads a blacklist from a binary file at |path| and applies
281 // it to the URL filters. If no file exists at |path| yet, downloads a file
282 // from |url| and stores it at |path| first.
treibf136dfb2014-09-25 17:37:47283 void LoadBlacklist(const base::FilePath& path, const GURL& url);
284
treib2fd187392015-04-16 17:19:38285 void OnBlacklistFileChecked(const base::FilePath& path,
286 const GURL& url,
287 bool file_exists);
288
289 // Asynchronously loads a blacklist from a binary file at |path| and applies
290 // it to the URL filters.
treibf136dfb2014-09-25 17:37:47291 void LoadBlacklistFromFile(const base::FilePath& path);
292
treibf38cc252016-04-07 14:44:11293 void OnBlacklistDownloadDone(const base::FilePath& path,
294 FileDownloader::Result result);
treib4edbded2014-09-05 08:43:55295
bauerbce9a1a82014-12-18 13:34:24296 void OnBlacklistLoaded();
297
treib9cc1b112016-01-08 10:08:01298 void UpdateBlacklist();
299
[email protected]5e022292013-02-06 16:42:17300 // Updates the manual overrides for hosts in the URL filters when the
301 // corresponding preference is changed.
302 void UpdateManualHosts();
[email protected]0850e842013-01-19 03:44:31303
[email protected]5e022292013-02-06 16:42:17304 // Updates the manual overrides for URLs in the URL filters when the
305 // corresponding preference is changed.
306 void UpdateManualURLs();
[email protected]0850e842013-01-19 03:44:31307
[email protected]12b7af32014-03-13 05:28:20308 // Owns us via the KeyedService mechanism.
[email protected]0850e842013-01-19 03:44:31309 Profile* profile_;
310
[email protected]f085fdd52014-06-11 18:09:20311 bool active_;
312
313 Delegate* delegate_;
314
[email protected]0850e842013-01-19 03:44:31315 PrefChangeRegistrar pref_change_registrar_;
316
[email protected]dfddd022013-07-10 17:29:48317 bool is_profile_active_;
[email protected]a243d644c2013-06-20 18:37:55318
[email protected]3a276ff2014-08-12 14:22:09319 // True only when |Init()| method has been called.
320 bool did_init_;
321
[email protected]3bf45d02013-07-10 00:03:41322 // True only when |Shutdown()| method has been called.
323 bool did_shutdown_;
324
mmenkedb2637ff2017-03-30 23:59:42325 SupervisedUserURLFilter url_filter_;
treib9cc1b112016-01-08 10:08:01326
mamire9609642016-06-28 22:17:54327 // Stores a map from extension_id -> approved version by the custodian.
328 // It is only relevant for SU-initiated installs.
329 std::map<std::string, base::Version> approved_extensions_map_;
330
treib9cc1b112016-01-08 10:08:01331 enum class BlacklistLoadState {
332 NOT_LOADED,
333 LOAD_STARTED,
334 LOADED
335 } blacklist_state_;
336
337 SupervisedUserBlacklist blacklist_;
dchengf624e472016-04-12 08:33:17338 std::unique_ptr<FileDownloader> blacklist_downloader_;
[email protected]8052b242013-11-15 16:40:55339
dchengf624e472016-04-12 08:33:17340 std::unique_ptr<SupervisedUserWhitelistService> whitelist_service_;
bauerb4da36132014-12-26 19:53:13341
atanasova9572aaf2016-02-26 18:08:26342 std::vector<scoped_refptr<SupervisedUserSiteList>> whitelists_;
343
[email protected]4db65f952014-05-20 15:46:30344 // Used to create permission requests.
leon.han4ea301f2017-03-28 03:36:31345 std::vector<std::unique_ptr<PermissionRequestCreator>> permissions_creators_;
[email protected]4db65f952014-05-20 15:46:30346
atanasovaac676032016-04-05 16:31:05347 // Used to report inappropriate URLs to SafeSarch API.
dchengf624e472016-04-12 08:33:17348 std::unique_ptr<SafeSearchURLReporter> url_reporter_;
atanasovaac676032016-04-05 16:31:05349
brettw00899e62016-11-12 02:10:17350#if BUILDFLAG(ENABLE_EXTENSIONS)
mamire9609642016-06-28 22:17:54351 ScopedObserver<extensions::ExtensionRegistry,
352 extensions::ExtensionRegistryObserver>
353 registry_observer_;
354#endif
355
Trent Apteda250ec3ab2018-08-19 08:52:19356 base::ObserverList<SupervisedUserServiceObserver>::Unchecked observer_list_;
treibab0a39e2014-09-24 14:48:28357
[email protected]cce15bb2014-06-17 13:43:51358 base::WeakPtrFactory<SupervisedUserService> weak_ptr_factory_;
leon.han4ea301f2017-03-28 03:36:31359
360 DISALLOW_COPY_AND_ASSIGN(SupervisedUserService);
[email protected]0850e842013-01-19 03:44:31361};
362
[email protected]cce15bb2014-06-17 13:43:51363#endif // CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SERVICE_H_