Supervised users: When an extension requires new permissions, send request to custodian
[chromium-blink-merge.git] / chrome / browser / supervised_user / supervised_user_service.h
blob6757352843470a601e3e1b0362ab515909caf806
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SERVICE_H_
6 #define CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SERVICE_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/callback.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/observer_list.h"
16 #include "base/prefs/pref_change_registrar.h"
17 #include "base/scoped_observer.h"
18 #include "base/strings/string16.h"
19 #include "chrome/browser/supervised_user/experimental/supervised_user_blacklist.h"
20 #include "chrome/browser/supervised_user/supervised_user_url_filter.h"
21 #include "chrome/browser/supervised_user/supervised_users.h"
22 #include "chrome/browser/sync/profile_sync_service_observer.h"
23 #include "chrome/browser/sync/sync_type_preference_provider.h"
24 #include "chrome/browser/ui/browser_list_observer.h"
25 #include "components/keyed_service/core/keyed_service.h"
26 #include "net/url_request/url_request_context_getter.h"
28 #if defined(ENABLE_EXTENSIONS)
29 #include "extensions/browser/management_policy.h"
30 #endif
32 class Browser;
33 class GoogleServiceAuthError;
34 class PermissionRequestCreator;
35 class Profile;
36 class SupervisedUserBlacklistDownloader;
37 class SupervisedUserRegistrationUtility;
38 class SupervisedUserServiceObserver;
39 class SupervisedUserSettingsService;
40 class SupervisedUserSiteList;
41 class SupervisedUserURLFilter;
42 class SupervisedUserWhitelistService;
44 namespace base {
45 class FilePath;
48 namespace content {
49 class WebContents;
52 namespace extensions {
53 class ExtensionRegistry;
56 namespace user_prefs {
57 class PrefRegistrySyncable;
60 // This class handles all the information related to a given supervised profile
61 // (e.g. the installed content packs, the default URL filtering behavior, or
62 // manual whitelist/blacklist overrides).
63 class SupervisedUserService : public KeyedService,
64 #if defined(ENABLE_EXTENSIONS)
65 public extensions::ManagementPolicy::Provider,
66 #endif
67 public SyncTypePreferenceProvider,
68 public ProfileSyncServiceObserver,
69 public chrome::BrowserListObserver,
70 public SupervisedUserURLFilter::Observer {
71 public:
72 using NavigationBlockedCallback = base::Callback<void(content::WebContents*)>;
73 using AuthErrorCallback = base::Callback<void(const GoogleServiceAuthError&)>;
74 using SuccessCallback = base::Callback<void(bool)>;
76 class Delegate {
77 public:
78 virtual ~Delegate() {}
79 // Returns true to indicate that the delegate handled the (de)activation, or
80 // false to indicate that the SupervisedUserService itself should handle it.
81 virtual bool SetActive(bool active) = 0;
82 // Returns the path to a blacklist file to load, or an empty path to
83 // indicate "none".
84 virtual base::FilePath GetBlacklistPath() const;
85 // Returns the URL from which to download a blacklist if no local one exists
86 // yet. The blacklist file will be stored at |GetBlacklistPath()|.
87 virtual GURL GetBlacklistURL() const;
88 // Returns the identifier ("cx") of the Custom Search Engine to use for the
89 // experimental "SafeSites" feature, or the empty string to disable the
90 // feature.
91 virtual std::string GetSafeSitesCx() const;
94 ~SupervisedUserService() override;
96 // ProfileKeyedService override:
97 void Shutdown() override;
99 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
101 void SetDelegate(Delegate* delegate);
103 // Returns the URL filter for the IO thread, for filtering network requests
104 // (in SupervisedUserResourceThrottle).
105 scoped_refptr<const SupervisedUserURLFilter> GetURLFilterForIOThread();
107 // Returns the URL filter for the UI thread, for filtering navigations and
108 // classifying sites in the history view.
109 SupervisedUserURLFilter* GetURLFilterForUIThread();
111 // Returns the whitelist service.
112 SupervisedUserWhitelistService* GetWhitelistService();
114 // Whether the user can request to get access to blocked URLs or to new
115 // extensions.
116 bool AccessRequestsEnabled();
118 // Adds an access request for the given URL.
119 void AddURLAccessRequest(const GURL& url, const SuccessCallback& callback);
121 // Adds an update request for the given WebStore item (App/Extension).
122 void AddExtensionUpdateRequest(const std::string& extension_id,
123 const SuccessCallback& callback);
125 // Returns the email address of the custodian.
126 std::string GetCustodianEmailAddress() const;
128 // Returns the name of the custodian, or the email address if the name is
129 // empty.
130 std::string GetCustodianName() const;
132 // Returns the email address of the second custodian, or the empty string
133 // if there is no second custodian.
134 std::string GetSecondCustodianEmailAddress() const;
136 // Returns the name of the second custodian, or the email address if the name
137 // is empty, or the empty string is there is no second custodian.
138 std::string GetSecondCustodianName() const;
140 // Initializes this object. This method does nothing if the profile is not
141 // supervised.
142 void Init();
144 // Initializes this profile for syncing, using the provided |refresh_token| to
145 // mint access tokens for Sync.
146 void InitSync(const std::string& refresh_token);
148 // Convenience method that registers this supervised user using
149 // |registration_utility| and initializes sync with the returned token.
150 // The |callback| will be called when registration is complete,
151 // whether it succeeded or not -- unless registration was cancelled manually,
152 // in which case the callback will be ignored.
153 void RegisterAndInitSync(
154 SupervisedUserRegistrationUtility* registration_utility,
155 Profile* custodian_profile,
156 const std::string& supervised_user_id,
157 const AuthErrorCallback& callback);
159 void AddNavigationBlockedCallback(const NavigationBlockedCallback& callback);
160 void DidBlockNavigation(content::WebContents* web_contents);
162 void AddObserver(SupervisedUserServiceObserver* observer);
163 void RemoveObserver(SupervisedUserServiceObserver* observer);
165 void AddPermissionRequestCreator(
166 scoped_ptr<PermissionRequestCreator> creator);
168 // SyncTypePreferenceProvider implementation:
169 syncer::ModelTypeSet GetPreferredDataTypes() const override;
171 // ProfileSyncServiceObserver implementation:
172 void OnStateChanged() override;
174 // chrome::BrowserListObserver implementation:
175 void OnBrowserSetLastActive(Browser* browser) override;
177 // SupervisedUserURLFilter::Observer implementation:
178 void OnSiteListUpdated() override;
180 private:
181 friend class SupervisedUserServiceExtensionTestBase;
182 friend class SupervisedUserServiceFactory;
183 FRIEND_TEST_ALL_PREFIXES(SingleClientSupervisedUserSettingsSyncTest, Sanity);
184 FRIEND_TEST_ALL_PREFIXES(SupervisedUserServiceTest, ClearOmitOnRegistration);
185 FRIEND_TEST_ALL_PREFIXES(SupervisedUserServiceTest,
186 ChangesIncludedSessionOnChangedSettings);
187 FRIEND_TEST_ALL_PREFIXES(SupervisedUserServiceTest,
188 ChangesSyncSessionStateOnChangedSettings);
189 FRIEND_TEST_ALL_PREFIXES(SupervisedUserServiceExtensionTest,
190 ExtensionManagementPolicyProvider);
192 using CreatePermissionRequestCallback =
193 base::Callback<void(PermissionRequestCreator*, const SuccessCallback&)>;
195 // A bridge from the UI thread to the SupervisedUserURLFilters, one of which
196 // lives on the IO thread. This class mediates access to them and makes sure
197 // they are kept in sync.
198 class URLFilterContext {
199 public:
200 URLFilterContext();
201 ~URLFilterContext();
203 SupervisedUserURLFilter* ui_url_filter() const;
204 SupervisedUserURLFilter* io_url_filter() const;
206 void SetDefaultFilteringBehavior(
207 SupervisedUserURLFilter::FilteringBehavior behavior);
208 void LoadWhitelists(
209 const std::vector<scoped_refptr<SupervisedUserSiteList>>& site_lists);
210 void LoadBlacklist(const base::FilePath& path,
211 const base::Closure& callback);
212 void SetManualHosts(scoped_ptr<std::map<std::string, bool>> host_map);
213 void SetManualURLs(scoped_ptr<std::map<GURL, bool>> url_map);
215 void InitAsyncURLChecker(
216 const scoped_refptr<net::URLRequestContextGetter>& context,
217 const std::string& cx);
219 void Clear();
221 private:
222 void OnBlacklistLoaded(const base::Closure& callback);
224 // SupervisedUserURLFilter is refcounted because the IO thread filter is
225 // used both by ProfileImplIOData and OffTheRecordProfileIOData (to filter
226 // network requests), so they both keep a reference to it.
227 // Clients should not keep references to the UI thread filter, however
228 // (the filter will live as long as the profile lives, and afterwards it
229 // should not be used anymore either).
230 scoped_refptr<SupervisedUserURLFilter> ui_url_filter_;
231 scoped_refptr<SupervisedUserURLFilter> io_url_filter_;
233 SupervisedUserBlacklist blacklist_;
235 DISALLOW_COPY_AND_ASSIGN(URLFilterContext);
238 // Use |SupervisedUserServiceFactory::GetForProfile(..)| to get
239 // an instance of this service.
240 explicit SupervisedUserService(Profile* profile);
242 void SetActive(bool active);
244 void OnCustodianProfileDownloaded(const base::string16& full_name);
246 void OnSupervisedUserRegistered(const AuthErrorCallback& callback,
247 Profile* custodian_profile,
248 const GoogleServiceAuthError& auth_error,
249 const std::string& token);
251 void SetupSync();
252 void StartSetupSync();
253 void FinishSetupSyncWhenReady();
254 void FinishSetupSync();
256 bool ProfileIsSupervised() const;
258 void OnCustodianInfoChanged();
260 #if defined(ENABLE_EXTENSIONS)
261 // extensions::ManagementPolicy::Provider implementation:
262 std::string GetDebugPolicyProviderName() const override;
263 bool UserMayLoad(const extensions::Extension* extension,
264 base::string16* error) const override;
265 bool MustRemainInstalled(const extensions::Extension* extension,
266 base::string16* error) const override;
268 // Extensions helper to SetActive().
269 void SetExtensionsActive();
270 #endif
272 SupervisedUserSettingsService* GetSettingsService();
274 size_t FindEnabledPermissionRequestCreator(size_t start);
275 void AddPermissionRequestInternal(
276 const CreatePermissionRequestCallback& create_request,
277 const SuccessCallback& callback,
278 size_t index);
279 void OnPermissionRequestIssued(
280 const CreatePermissionRequestCallback& create_request,
281 const SuccessCallback& callback,
282 size_t index,
283 bool success);
285 void OnSupervisedUserIdChanged();
287 void OnDefaultFilteringBehaviorChanged();
289 void OnSiteListsChanged(
290 const std::vector<scoped_refptr<SupervisedUserSiteList>>& site_lists);
292 // Asynchronously downloads a static blacklist file from |url|, stores it at
293 // |path|, loads it, and applies it to the URL filters. If |url| is not valid
294 // (e.g. empty), directly tries to load from |path|.
295 void LoadBlacklist(const base::FilePath& path, const GURL& url);
297 // Asynchronously loads a static blacklist from a binary file at |path| and
298 // applies it to the URL filters.
299 void LoadBlacklistFromFile(const base::FilePath& path);
301 void OnBlacklistDownloadDone(const base::FilePath& path, bool success);
303 void OnBlacklistLoaded();
305 // Updates the manual overrides for hosts in the URL filters when the
306 // corresponding preference is changed.
307 void UpdateManualHosts();
309 // Updates the manual overrides for URLs in the URL filters when the
310 // corresponding preference is changed.
311 void UpdateManualURLs();
313 // Returns the human readable name of the supervised user.
314 std::string GetSupervisedUserName() const;
316 // Subscribes to the SupervisedUserPrefStore, refreshes
317 // |includes_sync_sessions_type_| and triggers reconfiguring the
318 // ProfileSyncService.
319 void OnHistoryRecordingStateChanged();
321 // Returns true if the syncer::SESSIONS type should be included in Sync.
322 bool IncludesSyncSessionsType() const;
324 // The option a custodian sets to either record or prevent recording the
325 // supervised user's history. Set by |FetchNewSessionSyncState()| and
326 // defaults to true.
327 bool includes_sync_sessions_type_;
329 // Owns us via the KeyedService mechanism.
330 Profile* profile_;
332 bool active_;
334 Delegate* delegate_;
336 PrefChangeRegistrar pref_change_registrar_;
338 // True iff we're waiting for the Sync service to be initialized.
339 bool waiting_for_sync_initialization_;
340 bool is_profile_active_;
342 std::vector<NavigationBlockedCallback> navigation_blocked_callbacks_;
344 // True only when |Init()| method has been called.
345 bool did_init_;
347 // True only when |Shutdown()| method has been called.
348 bool did_shutdown_;
350 URLFilterContext url_filter_context_;
351 scoped_ptr<SupervisedUserBlacklistDownloader> blacklist_downloader_;
353 scoped_ptr<SupervisedUserWhitelistService> whitelist_service_;
355 // Used to create permission requests.
356 ScopedVector<PermissionRequestCreator> permissions_creators_;
358 ObserverList<SupervisedUserServiceObserver> observer_list_;
360 base::WeakPtrFactory<SupervisedUserService> weak_ptr_factory_;
363 #endif // CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SERVICE_H_