blob: 6e31d528550626d930a5f065e77390fbfeca60fb [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);
190#endif // BUILDFLAG(ENABLE_EXTENSIONS)
191
[email protected]0850e842013-01-19 03:44:31192 private:
[email protected]cce15bb2014-06-17 13:43:51193 friend class SupervisedUserServiceExtensionTestBase;
194 friend class SupervisedUserServiceFactory;
mamire9609642016-06-28 22:17:54195 FRIEND_TEST_ALL_PREFIXES(
196 SupervisedUserServiceExtensionTest,
197 ExtensionManagementPolicyProviderWithoutSUInitiatedInstalls);
198 FRIEND_TEST_ALL_PREFIXES(
199 SupervisedUserServiceExtensionTest,
200 ExtensionManagementPolicyProviderWithSUInitiatedInstalls);
bauerb95a50682015-01-07 17:04:15201
treib8ecc1eb52015-03-04 18:29:06202 using CreatePermissionRequestCallback =
Carlos IL6b784a62018-03-20 00:26:49203 base::RepeatingCallback<void(PermissionRequestCreator*, SuccessCallback)>;
treib8ecc1eb52015-03-04 18:29:06204
[email protected]cce15bb2014-06-17 13:43:51205 // Use |SupervisedUserServiceFactory::GetForProfile(..)| to get
[email protected]3bf45d02013-07-10 00:03:41206 // an instance of this service.
[email protected]cce15bb2014-06-17 13:43:51207 explicit SupervisedUserService(Profile* profile);
[email protected]3bf45d02013-07-10 00:03:41208
[email protected]f085fdd52014-06-11 18:09:20209 void SetActive(bool active);
210
[email protected]cce15bb2014-06-17 13:43:51211 bool ProfileIsSupervised() const;
[email protected]e000daf2013-07-31 16:50:58212
treib3fce4e92014-09-26 16:06:25213 void OnCustodianInfoChanged();
214
brettw00899e62016-11-12 02:10:17215#if BUILDFLAG(ENABLE_EXTENSIONS)
treibbb9a1962015-02-25 13:40:59216 // extensions::ManagementPolicy::Provider implementation:
217 std::string GetDebugPolicyProviderName() const override;
218 bool UserMayLoad(const extensions::Extension* extension,
219 base::string16* error) const override;
treib755c12d2015-03-30 08:46:25220 bool UserMayModifySettings(const extensions::Extension* extension,
221 base::string16* error) const override;
mamire9609642016-06-28 22:17:54222 bool MustRemainDisabled(const extensions::Extension* extension,
Minh X. Nguyen45479012017-08-18 21:35:36223 extensions::disable_reason::DisableReason* reason,
mamire9609642016-06-28 22:17:54224 base::string16* error) const override;
225
226 // extensions::ExtensionRegistryObserver overrides:
227 void OnExtensionInstalled(content::BrowserContext* browser_context,
228 const extensions::Extension* extension,
229 bool is_update) override;
[email protected]0850e842013-01-19 03:44:31230
mamir192d7882016-06-22 17:10:16231 // An extension can be in one of the following states:
232 //
mamire9609642016-06-28 22:17:54233 // REQUIRE_APPROVAL: if it is installed by the supervised user and
234 // hasn't been approved by the custodian yet.
mamir192d7882016-06-22 17:10:16235 // ALLOWED: Components, Themes, Default extensions ..etc
236 // are generally allowed. Extensions that have been approved by the
237 // custodian are also allowed.
238 // BLOCKED: if it is not ALLOWED or FORCED
239 // and supervised users initiated installs are disabled.
Toby Huangd7caf252019-10-30 01:11:16240 enum class ExtensionState { BLOCKED, ALLOWED, REQUIRE_APPROVAL };
mamir192d7882016-06-22 17:10:16241
mamire9609642016-06-28 22:17:54242 // Returns the state of an extension whether being FORCED, BLOCKED, ALLOWED or
243 // REQUIRE_APPROVAL from the Supervised User service's point of view.
mamir192d7882016-06-22 17:10:16244 ExtensionState GetExtensionState(
mamire9609642016-06-28 22:17:54245 const extensions::Extension& extension) const;
mamir192d7882016-06-22 17:10:16246
[email protected]c14a6802014-07-11 21:51:12247 // Extensions helper to SetActive().
248 void SetExtensionsActive();
mamire9609642016-06-28 22:17:54249
250 // Enables/Disables extensions upon change in approved version of the
251 // extension_id.
252 void ChangeExtensionStateIfNecessary(const std::string& extension_id);
Toby Huang6f0321f2019-11-01 18:03:49253#endif // BUILDFLAG(ENABLE_EXTENSIONS)
[email protected]c14a6802014-07-11 21:51:12254
[email protected]cce15bb2014-06-17 13:43:51255 SupervisedUserSettingsService* GetSettingsService();
[email protected]e861bba2013-06-17 15:20:54256
bauerbd3a36cc42014-10-01 13:05:49257 size_t FindEnabledPermissionRequestCreator(size_t start);
treib8ecc1eb52015-03-04 18:29:06258 void AddPermissionRequestInternal(
259 const CreatePermissionRequestCallback& create_request,
Carlos IL6b784a62018-03-20 00:26:49260 SuccessCallback callback,
treib8ecc1eb52015-03-04 18:29:06261 size_t index);
262 void OnPermissionRequestIssued(
263 const CreatePermissionRequestCallback& create_request,
Carlos IL6b784a62018-03-20 00:26:49264 SuccessCallback callback,
treib8ecc1eb52015-03-04 18:29:06265 size_t index,
266 bool success);
bauerbd3a36cc42014-10-01 13:05:49267
[email protected]cce15bb2014-06-17 13:43:51268 void OnSupervisedUserIdChanged();
[email protected]f085fdd52014-06-11 18:09:20269
[email protected]0850e842013-01-19 03:44:31270 void OnDefaultFilteringBehaviorChanged();
271
treib9cc1b112016-01-08 10:08:01272 void OnSafeSitesSettingChanged();
273
bauerb95a50682015-01-07 17:04:15274 void OnSiteListsChanged(
275 const std::vector<scoped_refptr<SupervisedUserSiteList>>& site_lists);
[email protected]0850e842013-01-19 03:44:31276
treib2fd187392015-04-16 17:19:38277 // Asynchronously loads a blacklist from a binary file at |path| and applies
278 // it to the URL filters. If no file exists at |path| yet, downloads a file
279 // from |url| and stores it at |path| first.
treibf136dfb2014-09-25 17:37:47280 void LoadBlacklist(const base::FilePath& path, const GURL& url);
281
treib2fd187392015-04-16 17:19:38282 void OnBlacklistFileChecked(const base::FilePath& path,
283 const GURL& url,
284 bool file_exists);
285
286 // Asynchronously loads a blacklist from a binary file at |path| and applies
287 // it to the URL filters.
treibf136dfb2014-09-25 17:37:47288 void LoadBlacklistFromFile(const base::FilePath& path);
289
treibf38cc252016-04-07 14:44:11290 void OnBlacklistDownloadDone(const base::FilePath& path,
291 FileDownloader::Result result);
treib4edbded2014-09-05 08:43:55292
bauerbce9a1a82014-12-18 13:34:24293 void OnBlacklistLoaded();
294
treib9cc1b112016-01-08 10:08:01295 void UpdateBlacklist();
296
[email protected]5e022292013-02-06 16:42:17297 // Updates the manual overrides for hosts in the URL filters when the
298 // corresponding preference is changed.
299 void UpdateManualHosts();
[email protected]0850e842013-01-19 03:44:31300
[email protected]5e022292013-02-06 16:42:17301 // Updates the manual overrides for URLs in the URL filters when the
302 // corresponding preference is changed.
303 void UpdateManualURLs();
[email protected]0850e842013-01-19 03:44:31304
[email protected]12b7af32014-03-13 05:28:20305 // Owns us via the KeyedService mechanism.
[email protected]0850e842013-01-19 03:44:31306 Profile* profile_;
307
[email protected]f085fdd52014-06-11 18:09:20308 bool active_;
309
310 Delegate* delegate_;
311
[email protected]0850e842013-01-19 03:44:31312 PrefChangeRegistrar pref_change_registrar_;
313
[email protected]dfddd022013-07-10 17:29:48314 bool is_profile_active_;
[email protected]a243d644c2013-06-20 18:37:55315
[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
mmenkedb2637ff2017-03-30 23:59:42322 SupervisedUserURLFilter url_filter_;
treib9cc1b112016-01-08 10:08:01323
mamire9609642016-06-28 22:17:54324 // Stores a map from extension_id -> approved version by the custodian.
325 // It is only relevant for SU-initiated installs.
326 std::map<std::string, base::Version> approved_extensions_map_;
327
treib9cc1b112016-01-08 10:08:01328 enum class BlacklistLoadState {
329 NOT_LOADED,
330 LOAD_STARTED,
331 LOADED
332 } blacklist_state_;
333
334 SupervisedUserBlacklist blacklist_;
dchengf624e472016-04-12 08:33:17335 std::unique_ptr<FileDownloader> blacklist_downloader_;
[email protected]8052b242013-11-15 16:40:55336
dchengf624e472016-04-12 08:33:17337 std::unique_ptr<SupervisedUserWhitelistService> whitelist_service_;
bauerb4da36132014-12-26 19:53:13338
atanasova9572aaf2016-02-26 18:08:26339 std::vector<scoped_refptr<SupervisedUserSiteList>> whitelists_;
340
[email protected]4db65f952014-05-20 15:46:30341 // Used to create permission requests.
leon.han4ea301f2017-03-28 03:36:31342 std::vector<std::unique_ptr<PermissionRequestCreator>> permissions_creators_;
[email protected]4db65f952014-05-20 15:46:30343
brettw00899e62016-11-12 02:10:17344#if BUILDFLAG(ENABLE_EXTENSIONS)
mamire9609642016-06-28 22:17:54345 ScopedObserver<extensions::ExtensionRegistry,
346 extensions::ExtensionRegistryObserver>
Evan Stade2fad9012019-10-09 18:47:50347 registry_observer_{this};
mamire9609642016-06-28 22:17:54348#endif
349
Trent Apteda250ec3ab2018-08-19 08:52:19350 base::ObserverList<SupervisedUserServiceObserver>::Unchecked observer_list_;
treibab0a39e2014-09-24 14:48:28351
Danan Sc1945912019-06-27 02:56:30352#if !defined(OS_ANDROID)
353 bool signout_required_after_supervision_enabled_ = false;
354#endif
355
Jeremy Roman495db682019-07-12 16:03:24356 base::WeakPtrFactory<SupervisedUserService> weak_ptr_factory_{this};
leon.han4ea301f2017-03-28 03:36:31357
358 DISALLOW_COPY_AND_ASSIGN(SupervisedUserService);
[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_