blob: 1c39ab04193dd2188cf1bd3e766aa2e4a9fba42c [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"
treib4edbded2014-09-05 08:43:5523#include "chrome/browser/supervised_user/experimental/supervised_user_blacklist.h"
[email protected]cce15bb2014-06-17 13:43:5124#include "chrome/browser/supervised_user/supervised_user_url_filter.h"
25#include "chrome/browser/supervised_user/supervised_users.h"
[email protected]514fcf22013-08-13 06:37:2426#include "chrome/browser/ui/browser_list_observer.h"
[email protected]12b7af32014-03-13 05:28:2027#include "components/keyed_service/core/keyed_service.h"
brettwb1fc1b82016-02-02 00:19:0828#include "components/prefs/pref_change_registrar.h"
maxbogue455a57e32016-08-14 00:08:3229#include "components/sync/driver/sync_type_preference_provider.h"
Scott Violetc8240b02018-03-08 22:03:5930#include "extensions/buildflags/buildflags.h"
[email protected]c14a6802014-07-11 21:51:1231
brettw00899e62016-11-12 02:10:1732#if BUILDFLAG(ENABLE_EXTENSIONS)
Toby Huang6f0321f2019-11-01 18:03:4933#include "components/sync/model/sync_change.h"
Evan Stade2fad9012019-10-09 18:47:5034#include "extensions/browser/extension_registry.h"
mamire9609642016-06-28 22:17:5435#include "extensions/browser/extension_registry_observer.h"
[email protected]301116c62013-11-26 10:37:4536#include "extensions/browser/management_policy.h"
[email protected]c14a6802014-07-11 21:51:1237#endif
[email protected]0850e842013-01-19 03:44:3138
[email protected]509ad1a92013-03-19 21:41:0639class Browser;
[email protected]4db65f952014-05-20 15:46:3040class PermissionRequestCreator;
[email protected]0850e842013-01-19 03:44:3141class Profile;
treibab0a39e2014-09-24 14:48:2842class SupervisedUserServiceObserver;
[email protected]cce15bb2014-06-17 13:43:5143class SupervisedUserSettingsService;
44class SupervisedUserSiteList;
45class SupervisedUserURLFilter;
bauerb4da36132014-12-26 19:53:1346class SupervisedUserWhitelistService;
[email protected]0850e842013-01-19 03:44:3147
treibdaece84f2014-09-05 12:58:1548namespace base {
49class FilePath;
treibf832a992015-03-24 18:09:2450class Version;
treibdaece84f2014-09-05 12:58:1551}
52
[email protected]443e9312013-05-06 06:17:3453namespace user_prefs {
54class PrefRegistrySyncable;
55}
56
[email protected]cce15bb2014-06-17 13:43:5157// This class handles all the information related to a given supervised profile
[email protected]0850e842013-01-19 03:44:3158// (e.g. the installed content packs, the default URL filtering behavior, or
59// manual whitelist/blacklist overrides).
[email protected]cce15bb2014-06-17 13:43:5160class SupervisedUserService : public KeyedService,
brettw00899e62016-11-12 02:10:1761#if BUILDFLAG(ENABLE_EXTENSIONS)
mamire9609642016-06-28 22:17:5462 public extensions::ExtensionRegistryObserver,
[email protected]cce15bb2014-06-17 13:43:5163 public extensions::ManagementPolicy::Provider,
[email protected]c14a6802014-07-11 21:51:1264#endif
maxbogue7e006db2016-10-03 19:48:2865 public syncer::SyncTypePreferenceProvider,
jam1c5a91492016-02-24 20:47:5366#if !defined(OS_ANDROID)
cm.sanchia0d6add2017-12-13 04:59:3367 public BrowserListObserver,
bauerb5f8cda92015-10-07 15:36:4468#endif
bauerbce9a1a82014-12-18 13:34:2469 public SupervisedUserURLFilter::Observer {
[email protected]0850e842013-01-19 03:44:3170 public:
Carlos IL6b784a62018-03-20 00:26:4971 using SuccessCallback = base::OnceCallback<void(bool)>;
[email protected]0850e842013-01-19 03:44:3172
[email protected]f085fdd52014-06-11 18:09:2073 class Delegate {
74 public:
75 virtual ~Delegate() {}
76 // Returns true to indicate that the delegate handled the (de)activation, or
[email protected]cce15bb2014-06-17 13:43:5177 // false to indicate that the SupervisedUserService itself should handle it.
[email protected]f085fdd52014-06-11 18:09:2078 virtual bool SetActive(bool active) = 0;
79 };
80
dchengc072fff2014-10-21 11:39:0581 ~SupervisedUserService() override;
[email protected]0850e842013-01-19 03:44:3182
[email protected]37ca3fe02013-07-05 15:32:4483 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
[email protected]0850e842013-01-19 03:44:3184
bauerb5f8cda92015-10-07 15:36:4485 // Initializes this object.
86 void Init();
87
[email protected]f085fdd52014-06-11 18:09:2088 void SetDelegate(Delegate* delegate);
89
mmenkedb2637ff2017-03-30 23:59:4290 // Returns the URL filter for filtering navigations and classifying sites in
91 // the history view. Both this method and the returned filter may only be used
92 // on the UI thread.
93 SupervisedUserURLFilter* GetURLFilter();
[email protected]0850e842013-01-19 03:44:3194
bauerb4da36132014-12-26 19:53:1395 // Returns the whitelist service.
96 SupervisedUserWhitelistService* GetWhitelistService();
97
atanasova9572aaf2016-02-26 18:08:2698 const std::vector<scoped_refptr<SupervisedUserSiteList>>& whitelists() const {
99 return whitelists_;
100 }
101
treib8ecc1eb52015-03-04 18:29:06102 // Whether the user can request to get access to blocked URLs or to new
103 // extensions.
[email protected]0369d6ab2013-08-09 01:52:59104 bool AccessRequestsEnabled();
105
treib8ecc1eb52015-03-04 18:29:06106 // Adds an access request for the given URL.
Carlos IL6b784a62018-03-20 00:26:49107 void AddURLAccessRequest(const GURL& url, SuccessCallback callback);
treib8ecc1eb52015-03-04 18:29:06108
mamire9609642016-06-28 22:17:54109 // Get the string used to identify an extension install or update request.
110 // Public for testing.
111 static std::string GetExtensionRequestId(const std::string& extension_id,
112 const base::Version& version);
treib40d3ad92015-10-20 18:15:42113
[email protected]a9c2d642013-05-31 14:37:14114 // Returns the email address of the custodian.
115 std::string GetCustodianEmailAddress() const;
116
Danan Sbef6ca22019-05-09 18:15:54117 // Returns the obfuscated GAIA id of the custodian.
118 std::string GetCustodianObfuscatedGaiaId() const;
119
[email protected]fae057a2013-06-21 22:46:08120 // Returns the name of the custodian, or the email address if the name is
121 // empty.
122 std::string GetCustodianName() const;
123
treib3e7ecf72014-10-24 16:44:07124 // Returns the email address of the second custodian, or the empty string
125 // if there is no second custodian.
126 std::string GetSecondCustodianEmailAddress() const;
127
Danan Sbef6ca22019-05-09 18:15:54128 // Returns the obfuscated GAIA id of the second custodian or the empty
129 // string if there is no second custodian.
130 std::string GetSecondCustodianObfuscatedGaiaId() const;
131
treib3e7ecf72014-10-24 16:44:07132 // Returns the name of the second custodian, or the email address if the name
Danan Sbef6ca22019-05-09 18:15:54133 // is empty, or the empty string if there is no second custodian.
treib3e7ecf72014-10-24 16:44:07134 std::string GetSecondCustodianName() const;
135
treib2170ea02015-10-13 14:55:12136 // Returns a message saying that extensions can only be modified by the
137 // custodian.
138 base::string16 GetExtensionsLockedMessage() const;
139
yilkal190d97c2019-09-06 23:50:03140 bool IsSupervisedUserIframeFilterEnabled() const;
141
jam1c5a91492016-02-24 20:47:53142#if !defined(OS_ANDROID)
[email protected]a243d644c2013-06-20 18:37:55143 // Initializes this profile for syncing, using the provided |refresh_token| to
144 // mint access tokens for Sync.
145 void InitSync(const std::string& refresh_token);
thestig1b76f1a2015-09-30 22:52:38146#endif
[email protected]acfcfbb2013-05-13 18:01:27147
treibab0a39e2014-09-24 14:48:28148 void AddObserver(SupervisedUserServiceObserver* observer);
149 void RemoveObserver(SupervisedUserServiceObserver* observer);
150
treib531fc7312014-12-09 12:49:20151 void AddPermissionRequestCreator(
dchengf624e472016-04-12 08:33:17152 std::unique_ptr<PermissionRequestCreator> creator);
bauerb646019b12014-10-16 16:23:09153
bauerb5f8cda92015-10-07 15:36:44154 // ProfileKeyedService override:
155 void Shutdown() override;
156
[email protected]3a276ff2014-08-12 14:22:09157 // SyncTypePreferenceProvider implementation:
Maksim Moskvitin35f598b2019-04-18 11:47:00158 syncer::UserSelectableTypeSet GetForcedTypes() const override;
Marc Treibb3bbf1d22019-03-27 15:45:39159 bool IsEncryptEverythingAllowed() const override;
[email protected]3a276ff2014-08-12 14:22:09160
jam1c5a91492016-02-24 20:47:53161#if !defined(OS_ANDROID)
cm.sanchia0d6add2017-12-13 04:59:33162 // BrowserListObserver implementation:
dchengc072fff2014-10-21 11:39:05163 void OnBrowserSetLastActive(Browser* browser) override;
jam1c5a91492016-02-24 20:47:53164#endif // !defined(OS_ANDROID)
[email protected]dfddd022013-07-10 17:29:48165
bauerbce9a1a82014-12-18 13:34:24166 // SupervisedUserURLFilter::Observer implementation:
167 void OnSiteListUpdated() override;
168
Danan Sc1945912019-06-27 02:56:30169#if !defined(OS_ANDROID)
170 bool signout_required_after_supervision_enabled() {
171 return signout_required_after_supervision_enabled_;
172 }
173 void set_signout_required_after_supervision_enabled() {
174 signout_required_after_supervision_enabled_ = true;
175 }
176#endif // !defined(OS_ANDROID)
177
yilkal921048bd2019-10-09 23:51:04178 void SetPrimaryPermissionCreatorForTest(
179 std::unique_ptr<PermissionRequestCreator> permission_creator);
180
Toby Huang6f0321f2019-11-01 18:03:49181#if BUILDFLAG(ENABLE_EXTENSIONS)
182 // Updates the map of approved extensions.
183 // If |type| is SyncChangeType::ADD, then add custodian approval for enabling
184 // the extension by adding the approved version to the map of approved
185 // extensions. If |type| is SyncChangeType::DELETE, then remove the extension
186 // from the map of approved extensions.
187 void UpdateApprovedExtensions(const std::string& extension_id,
188 const std::string& version,
189 syncer::SyncChange::SyncChangeType type);
Toby Huang114e9b02019-11-23 02:42:13190
191 bool GetSupervisedUserExtensionsMayRequestPermissionsPref() const;
192
193 void SetSupervisedUserExtensionsMayRequestPermissionsPrefForTesting(
194 bool enabled);
Toby Huang6f0321f2019-11-01 18:03:49195#endif // BUILDFLAG(ENABLE_EXTENSIONS)
196
[email protected]0850e842013-01-19 03:44:31197 private:
[email protected]cce15bb2014-06-17 13:43:51198 friend class SupervisedUserServiceExtensionTestBase;
199 friend class SupervisedUserServiceFactory;
mamire9609642016-06-28 22:17:54200 FRIEND_TEST_ALL_PREFIXES(
201 SupervisedUserServiceExtensionTest,
202 ExtensionManagementPolicyProviderWithoutSUInitiatedInstalls);
203 FRIEND_TEST_ALL_PREFIXES(
204 SupervisedUserServiceExtensionTest,
205 ExtensionManagementPolicyProviderWithSUInitiatedInstalls);
bauerb95a50682015-01-07 17:04:15206
treib8ecc1eb52015-03-04 18:29:06207 using CreatePermissionRequestCallback =
Carlos IL6b784a62018-03-20 00:26:49208 base::RepeatingCallback<void(PermissionRequestCreator*, SuccessCallback)>;
treib8ecc1eb52015-03-04 18:29:06209
[email protected]cce15bb2014-06-17 13:43:51210 // Use |SupervisedUserServiceFactory::GetForProfile(..)| to get
[email protected]3bf45d02013-07-10 00:03:41211 // an instance of this service.
[email protected]cce15bb2014-06-17 13:43:51212 explicit SupervisedUserService(Profile* profile);
[email protected]3bf45d02013-07-10 00:03:41213
[email protected]f085fdd52014-06-11 18:09:20214 void SetActive(bool active);
215
[email protected]cce15bb2014-06-17 13:43:51216 bool ProfileIsSupervised() const;
[email protected]e000daf2013-07-31 16:50:58217
treib3fce4e92014-09-26 16:06:25218 void OnCustodianInfoChanged();
219
brettw00899e62016-11-12 02:10:17220#if BUILDFLAG(ENABLE_EXTENSIONS)
treibbb9a1962015-02-25 13:40:59221 // extensions::ManagementPolicy::Provider implementation:
222 std::string GetDebugPolicyProviderName() const override;
223 bool UserMayLoad(const extensions::Extension* extension,
224 base::string16* error) const override;
treib755c12d2015-03-30 08:46:25225 bool UserMayModifySettings(const extensions::Extension* extension,
226 base::string16* error) const override;
mamire9609642016-06-28 22:17:54227 bool MustRemainDisabled(const extensions::Extension* extension,
Minh X. Nguyen45479012017-08-18 21:35:36228 extensions::disable_reason::DisableReason* reason,
mamire9609642016-06-28 22:17:54229 base::string16* error) const override;
230
231 // extensions::ExtensionRegistryObserver overrides:
232 void OnExtensionInstalled(content::BrowserContext* browser_context,
233 const extensions::Extension* extension,
234 bool is_update) override;
[email protected]0850e842013-01-19 03:44:31235
mamir192d7882016-06-22 17:10:16236 // An extension can be in one of the following states:
237 //
Toby Huang114e9b02019-11-23 02:42:13238 // BLOCKED: if kSupervisedUserExtensionsMayRequestPermissions is false and the
239 // child user is attempting to install a new extension or an existing
240 // extension is asking for additional permissions.
mamir192d7882016-06-22 17:10:16241 // ALLOWED: Components, Themes, Default extensions ..etc
242 // are generally allowed. Extensions that have been approved by the
243 // custodian are also allowed.
Toby Huang114e9b02019-11-23 02:42:13244 // REQUIRE_APPROVAL: if it is installed by the child user and
245 // hasn't been approved by the custodian yet.
Toby Huangd7caf252019-10-30 01:11:16246 enum class ExtensionState { BLOCKED, ALLOWED, REQUIRE_APPROVAL };
mamir192d7882016-06-22 17:10:16247
Toby Huang114e9b02019-11-23 02:42:13248 // Returns the state of an extension whether being BLOCKED, ALLOWED or
mamire9609642016-06-28 22:17:54249 // REQUIRE_APPROVAL from the Supervised User service's point of view.
mamir192d7882016-06-22 17:10:16250 ExtensionState GetExtensionState(
mamire9609642016-06-28 22:17:54251 const extensions::Extension& extension) const;
mamir192d7882016-06-22 17:10:16252
[email protected]c14a6802014-07-11 21:51:12253 // Extensions helper to SetActive().
254 void SetExtensionsActive();
mamire9609642016-06-28 22:17:54255
256 // Enables/Disables extensions upon change in approved version of the
Toby Huang114e9b02019-11-23 02:42:13257 // extension_id. This function is idempotent.
mamire9609642016-06-28 22:17:54258 void ChangeExtensionStateIfNecessary(const std::string& extension_id);
Toby Huang6f0321f2019-11-01 18:03:49259#endif // BUILDFLAG(ENABLE_EXTENSIONS)
[email protected]c14a6802014-07-11 21:51:12260
[email protected]cce15bb2014-06-17 13:43:51261 SupervisedUserSettingsService* GetSettingsService();
[email protected]e861bba2013-06-17 15:20:54262
bauerbd3a36cc42014-10-01 13:05:49263 size_t FindEnabledPermissionRequestCreator(size_t start);
treib8ecc1eb52015-03-04 18:29:06264 void AddPermissionRequestInternal(
265 const CreatePermissionRequestCallback& create_request,
Carlos IL6b784a62018-03-20 00:26:49266 SuccessCallback callback,
treib8ecc1eb52015-03-04 18:29:06267 size_t index);
268 void OnPermissionRequestIssued(
269 const CreatePermissionRequestCallback& create_request,
Carlos IL6b784a62018-03-20 00:26:49270 SuccessCallback callback,
treib8ecc1eb52015-03-04 18:29:06271 size_t index,
272 bool success);
bauerbd3a36cc42014-10-01 13:05:49273
[email protected]cce15bb2014-06-17 13:43:51274 void OnSupervisedUserIdChanged();
[email protected]f085fdd52014-06-11 18:09:20275
[email protected]0850e842013-01-19 03:44:31276 void OnDefaultFilteringBehaviorChanged();
277
treib9cc1b112016-01-08 10:08:01278 void OnSafeSitesSettingChanged();
279
bauerb95a50682015-01-07 17:04:15280 void OnSiteListsChanged(
281 const std::vector<scoped_refptr<SupervisedUserSiteList>>& site_lists);
[email protected]0850e842013-01-19 03:44:31282
treib2fd187392015-04-16 17:19:38283 // Asynchronously loads a blacklist from a binary file at |path| and applies
284 // it to the URL filters. If no file exists at |path| yet, downloads a file
285 // from |url| and stores it at |path| first.
treibf136dfb2014-09-25 17:37:47286 void LoadBlacklist(const base::FilePath& path, const GURL& url);
287
treib2fd187392015-04-16 17:19:38288 void OnBlacklistFileChecked(const base::FilePath& path,
289 const GURL& url,
290 bool file_exists);
291
292 // Asynchronously loads a blacklist from a binary file at |path| and applies
293 // it to the URL filters.
treibf136dfb2014-09-25 17:37:47294 void LoadBlacklistFromFile(const base::FilePath& path);
295
treibf38cc252016-04-07 14:44:11296 void OnBlacklistDownloadDone(const base::FilePath& path,
297 FileDownloader::Result result);
treib4edbded2014-09-05 08:43:55298
bauerbce9a1a82014-12-18 13:34:24299 void OnBlacklistLoaded();
300
treib9cc1b112016-01-08 10:08:01301 void UpdateBlacklist();
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]12b7af32014-03-13 05:28:20311 // Owns us via the KeyedService mechanism.
[email protected]0850e842013-01-19 03:44:31312 Profile* profile_;
313
[email protected]f085fdd52014-06-11 18:09:20314 bool active_;
315
316 Delegate* delegate_;
317
[email protected]0850e842013-01-19 03:44:31318 PrefChangeRegistrar pref_change_registrar_;
319
[email protected]dfddd022013-07-10 17:29:48320 bool is_profile_active_;
[email protected]a243d644c2013-06-20 18:37:55321
[email protected]3a276ff2014-08-12 14:22:09322 // True only when |Init()| method has been called.
323 bool did_init_;
324
[email protected]3bf45d02013-07-10 00:03:41325 // True only when |Shutdown()| method has been called.
326 bool did_shutdown_;
327
mmenkedb2637ff2017-03-30 23:59:42328 SupervisedUserURLFilter url_filter_;
treib9cc1b112016-01-08 10:08:01329
mamire9609642016-06-28 22:17:54330 // Stores a map from extension_id -> approved version by the custodian.
331 // It is only relevant for SU-initiated installs.
332 std::map<std::string, base::Version> approved_extensions_map_;
333
treib9cc1b112016-01-08 10:08:01334 enum class BlacklistLoadState {
335 NOT_LOADED,
336 LOAD_STARTED,
337 LOADED
338 } blacklist_state_;
339
340 SupervisedUserBlacklist blacklist_;
dchengf624e472016-04-12 08:33:17341 std::unique_ptr<FileDownloader> blacklist_downloader_;
[email protected]8052b242013-11-15 16:40:55342
dchengf624e472016-04-12 08:33:17343 std::unique_ptr<SupervisedUserWhitelistService> whitelist_service_;
bauerb4da36132014-12-26 19:53:13344
atanasova9572aaf2016-02-26 18:08:26345 std::vector<scoped_refptr<SupervisedUserSiteList>> whitelists_;
346
[email protected]4db65f952014-05-20 15:46:30347 // Used to create permission requests.
leon.han4ea301f2017-03-28 03:36:31348 std::vector<std::unique_ptr<PermissionRequestCreator>> permissions_creators_;
[email protected]4db65f952014-05-20 15:46:30349
brettw00899e62016-11-12 02:10:17350#if BUILDFLAG(ENABLE_EXTENSIONS)
mamire9609642016-06-28 22:17:54351 ScopedObserver<extensions::ExtensionRegistry,
352 extensions::ExtensionRegistryObserver>
Evan Stade2fad9012019-10-09 18:47:50353 registry_observer_{this};
mamire9609642016-06-28 22:17:54354#endif
355
Trent Apteda250ec3ab2018-08-19 08:52:19356 base::ObserverList<SupervisedUserServiceObserver>::Unchecked observer_list_;
treibab0a39e2014-09-24 14:48:28357
Danan Sc1945912019-06-27 02:56:30358#if !defined(OS_ANDROID)
359 bool signout_required_after_supervision_enabled_ = false;
360#endif
361
Jeremy Roman495db682019-07-12 16:03:24362 base::WeakPtrFactory<SupervisedUserService> weak_ptr_factory_{this};
leon.han4ea301f2017-03-28 03:36:31363
364 DISALLOW_COPY_AND_ASSIGN(SupervisedUserService);
[email protected]0850e842013-01-19 03:44:31365};
366
[email protected]cce15bb2014-06-17 13:43:51367#endif // CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SERVICE_H_