Fix UnlockEndpointIsDelayed again.
[chromium-blink-merge.git] / chrome / browser / supervised_user / supervised_user_service.h
blob97439d2a5f624881580154afe9bf175c5be48bda
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"
27 #if defined(ENABLE_EXTENSIONS)
28 #include "extensions/browser/management_policy.h"
29 #endif
31 class Browser;
32 class GoogleServiceAuthError;
33 class PermissionRequestCreator;
34 class Profile;
35 class SupervisedUserBlacklistDownloader;
36 class SupervisedUserRegistrationUtility;
37 class SupervisedUserServiceObserver;
38 class SupervisedUserSettingsService;
39 class SupervisedUserSiteList;
40 class SupervisedUserURLFilter;
41 class SupervisedUserWhitelistService;
43 namespace base {
44 class FilePath;
47 namespace content {
48 class WebContents;
51 namespace extensions {
52 class ExtensionRegistry;
55 namespace net {
56 class URLRequestContextGetter;
59 namespace user_prefs {
60 class PrefRegistrySyncable;
63 // This class handles all the information related to a given supervised profile
64 // (e.g. the installed content packs, the default URL filtering behavior, or
65 // manual whitelist/blacklist overrides).
66 class SupervisedUserService : public KeyedService,
67 #if defined(ENABLE_EXTENSIONS)
68 public extensions::ManagementPolicy::Provider,
69 #endif
70 public SyncTypePreferenceProvider,
71 public ProfileSyncServiceObserver,
72 public chrome::BrowserListObserver,
73 public SupervisedUserURLFilter::Observer {
74 public:
75 typedef base::Callback<void(content::WebContents*)> NavigationBlockedCallback;
76 typedef base::Callback<void(const GoogleServiceAuthError&)> AuthErrorCallback;
77 typedef base::Callback<void(bool)> SuccessCallback;
79 class Delegate {
80 public:
81 virtual ~Delegate() {}
82 // Returns true to indicate that the delegate handled the (de)activation, or
83 // false to indicate that the SupervisedUserService itself should handle it.
84 virtual bool SetActive(bool active) = 0;
85 // Returns the path to a blacklist file to load, or an empty path to
86 // indicate "none".
87 virtual base::FilePath GetBlacklistPath() const;
88 // Returns the URL from which to download a blacklist if no local one exists
89 // yet. The blacklist file will be stored at |GetBlacklistPath()|.
90 virtual GURL GetBlacklistURL() const;
91 // Returns the identifier ("cx") of the Custom Search Engine to use for the
92 // experimental "SafeSites" feature, or the empty string to disable the
93 // feature.
94 virtual std::string GetSafeSitesCx() const;
97 ~SupervisedUserService() override;
99 // ProfileKeyedService override:
100 void Shutdown() override;
102 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
104 void SetDelegate(Delegate* delegate);
106 // Returns the URL filter for the IO thread, for filtering network requests
107 // (in SupervisedUserResourceThrottle).
108 scoped_refptr<const SupervisedUserURLFilter> GetURLFilterForIOThread();
110 // Returns the URL filter for the UI thread, for filtering navigations and
111 // classifying sites in the history view.
112 SupervisedUserURLFilter* GetURLFilterForUIThread();
114 // Returns the whitelist service.
115 SupervisedUserWhitelistService* GetWhitelistService();
117 // Whether the user can request access to blocked URLs.
118 bool AccessRequestsEnabled();
120 // Adds an access request for the given URL. The requests are stored using
121 // a prefix followed by a URIEncoded version of the URL. Each entry contains
122 // a dictionary which currently has the timestamp of the request in it.
123 void AddAccessRequest(const GURL& url, 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 #if defined(ENABLE_EXTENSIONS)
169 // extensions::ManagementPolicy::Provider implementation:
170 std::string GetDebugPolicyProviderName() const override;
171 bool UserMayLoad(const extensions::Extension* extension,
172 base::string16* error) const override;
173 bool UserMayModifySettings(const extensions::Extension* extension,
174 base::string16* error) const override;
175 #endif
177 // SyncTypePreferenceProvider implementation:
178 syncer::ModelTypeSet GetPreferredDataTypes() const override;
180 // ProfileSyncServiceObserver implementation:
181 void OnStateChanged() override;
183 // chrome::BrowserListObserver implementation:
184 void OnBrowserSetLastActive(Browser* browser) override;
186 // SupervisedUserURLFilter::Observer implementation:
187 void OnSiteListUpdated() override;
189 private:
190 friend class SupervisedUserServiceExtensionTestBase;
191 friend class SupervisedUserServiceFactory;
192 FRIEND_TEST_ALL_PREFIXES(SingleClientSupervisedUserSettingsSyncTest, Sanity);
193 FRIEND_TEST_ALL_PREFIXES(SupervisedUserServiceTest, ClearOmitOnRegistration);
194 FRIEND_TEST_ALL_PREFIXES(SupervisedUserServiceTest,
195 ChangesIncludedSessionOnChangedSettings);
196 FRIEND_TEST_ALL_PREFIXES(SupervisedUserServiceTest,
197 ChangesSyncSessionStateOnChangedSettings);
199 // A bridge from the UI thread to the SupervisedUserURLFilters, one of which
200 // lives on the IO thread. This class mediates access to them and makes sure
201 // they are kept in sync.
202 class URLFilterContext {
203 public:
204 URLFilterContext();
205 ~URLFilterContext();
207 SupervisedUserURLFilter* ui_url_filter() const;
208 SupervisedUserURLFilter* io_url_filter() const;
210 void SetDefaultFilteringBehavior(
211 SupervisedUserURLFilter::FilteringBehavior behavior);
212 void LoadWhitelists(
213 const std::vector<scoped_refptr<SupervisedUserSiteList>>& site_lists);
214 void LoadBlacklist(const base::FilePath& path,
215 const base::Closure& callback);
216 void SetManualHosts(scoped_ptr<std::map<std::string, bool>> host_map);
217 void SetManualURLs(scoped_ptr<std::map<GURL, bool>> url_map);
219 void InitAsyncURLChecker(net::URLRequestContextGetter* context,
220 const std::string& cx);
222 void Clear();
224 private:
225 void OnBlacklistLoaded(const base::Closure& callback);
227 // SupervisedUserURLFilter is refcounted because the IO thread filter is
228 // used both by ProfileImplIOData and OffTheRecordProfileIOData (to filter
229 // network requests), so they both keep a reference to it.
230 // Clients should not keep references to the UI thread filter, however
231 // (the filter will live as long as the profile lives, and afterwards it
232 // should not be used anymore either).
233 scoped_refptr<SupervisedUserURLFilter> ui_url_filter_;
234 scoped_refptr<SupervisedUserURLFilter> io_url_filter_;
236 SupervisedUserBlacklist blacklist_;
238 DISALLOW_COPY_AND_ASSIGN(URLFilterContext);
241 // Use |SupervisedUserServiceFactory::GetForProfile(..)| to get
242 // an instance of this service.
243 explicit SupervisedUserService(Profile* profile);
245 void SetActive(bool active);
247 void OnCustodianProfileDownloaded(const base::string16& full_name);
249 void OnSupervisedUserRegistered(const AuthErrorCallback& callback,
250 Profile* custodian_profile,
251 const GoogleServiceAuthError& auth_error,
252 const std::string& token);
254 void SetupSync();
255 void StartSetupSync();
256 void FinishSetupSyncWhenReady();
257 void FinishSetupSync();
259 bool ProfileIsSupervised() const;
261 void OnCustodianInfoChanged();
263 #if defined(ENABLE_EXTENSIONS)
264 // Internal implementation for ExtensionManagementPolicy::Delegate methods.
265 // If |error| is not NULL, it will be filled with an error message if the
266 // requested extension action (install, modify status, etc.) is not permitted.
267 bool ExtensionManagementPolicyImpl(const extensions::Extension* extension,
268 base::string16* error) const;
270 // Extensions helper to SetActive().
271 void SetExtensionsActive();
272 #endif
274 SupervisedUserSettingsService* GetSettingsService();
276 size_t FindEnabledPermissionRequestCreator(size_t start);
277 void AddAccessRequestInternal(const GURL& url,
278 const SuccessCallback& callback,
279 size_t index);
280 void OnPermissionRequestIssued(const GURL& url,
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_