Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / supervised_user / supervised_user_service.h
blobbca5978ed50cb3e5a921990ece6b34cac065ec8d
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/memory/scoped_vector.h"
16 #include "base/observer_list.h"
17 #include "base/prefs/pref_change_registrar.h"
18 #include "base/scoped_observer.h"
19 #include "base/strings/string16.h"
20 #include "chrome/browser/supervised_user/experimental/supervised_user_blacklist.h"
21 #include "chrome/browser/supervised_user/supervised_user_url_filter.h"
22 #include "chrome/browser/supervised_user/supervised_users.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 "components/sync_driver/sync_service_observer.h"
27 #include "net/url_request/url_request_context_getter.h"
29 #if defined(ENABLE_EXTENSIONS)
30 #include "extensions/browser/management_policy.h"
31 #endif
33 class Browser;
34 class FileDownloader;
35 class GoogleServiceAuthError;
36 class PermissionRequestCreator;
37 class Profile;
38 class SupervisedUserRegistrationUtility;
39 class SupervisedUserServiceObserver;
40 class SupervisedUserSettingsService;
41 class SupervisedUserSiteList;
42 class SupervisedUserURLFilter;
43 class SupervisedUserWhitelistService;
45 namespace base {
46 class FilePath;
47 class Version;
50 namespace content {
51 class WebContents;
54 namespace extensions {
55 class ExtensionRegistry;
58 namespace user_prefs {
59 class PrefRegistrySyncable;
62 // This class handles all the information related to a given supervised profile
63 // (e.g. the installed content packs, the default URL filtering behavior, or
64 // manual whitelist/blacklist overrides).
65 class SupervisedUserService : public KeyedService,
66 #if defined(ENABLE_EXTENSIONS)
67 public extensions::ManagementPolicy::Provider,
68 #endif
69 public SyncTypePreferenceProvider,
70 public sync_driver::SyncServiceObserver,
71 public chrome::BrowserListObserver,
72 public SupervisedUserURLFilter::Observer {
73 public:
74 using NavigationBlockedCallback = base::Callback<void(content::WebContents*)>;
75 using AuthErrorCallback = base::Callback<void(const GoogleServiceAuthError&)>;
76 using SuccessCallback = base::Callback<void(bool)>;
78 class Delegate {
79 public:
80 virtual ~Delegate() {}
81 // Returns true to indicate that the delegate handled the (de)activation, or
82 // false to indicate that the SupervisedUserService itself should handle it.
83 virtual bool SetActive(bool active) = 0;
86 ~SupervisedUserService() override;
88 // ProfileKeyedService override:
89 void Shutdown() override;
91 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
93 void SetDelegate(Delegate* delegate);
95 // Returns the URL filter for the IO thread, for filtering network requests
96 // (in SupervisedUserResourceThrottle).
97 scoped_refptr<const SupervisedUserURLFilter> GetURLFilterForIOThread();
99 // Returns the URL filter for the UI thread, for filtering navigations and
100 // classifying sites in the history view.
101 SupervisedUserURLFilter* GetURLFilterForUIThread();
103 // Returns the whitelist service.
104 SupervisedUserWhitelistService* GetWhitelistService();
106 // Whether the user can request to get access to blocked URLs or to new
107 // extensions.
108 bool AccessRequestsEnabled();
110 // Adds an access request for the given URL.
111 void AddURLAccessRequest(const GURL& url, const SuccessCallback& callback);
113 // Adds an update request for the given WebStore item (App/Extension).
114 void AddExtensionUpdateRequest(const std::string& extension_id,
115 const base::Version& version,
116 const SuccessCallback& callback);
118 // Returns the email address of the custodian.
119 std::string GetCustodianEmailAddress() const;
121 // Returns the name of the custodian, or the email address if the name is
122 // empty.
123 std::string GetCustodianName() const;
125 // Returns the email address of the second custodian, or the empty string
126 // if there is no second custodian.
127 std::string GetSecondCustodianEmailAddress() const;
129 // Returns the name of the second custodian, or the email address if the name
130 // is empty, or the empty string is there is no second custodian.
131 std::string GetSecondCustodianName() const;
133 // Initializes this object.
134 void Init();
136 // Initializes this profile for syncing, using the provided |refresh_token| to
137 // mint access tokens for Sync.
138 void InitSync(const std::string& refresh_token);
140 // Convenience method that registers this supervised user using
141 // |registration_utility| and initializes sync with the returned token.
142 // The |callback| will be called when registration is complete,
143 // whether it succeeded or not -- unless registration was cancelled manually,
144 // in which case the callback will be ignored.
145 void RegisterAndInitSync(
146 SupervisedUserRegistrationUtility* registration_utility,
147 Profile* custodian_profile,
148 const std::string& supervised_user_id,
149 const AuthErrorCallback& callback);
151 void AddNavigationBlockedCallback(const NavigationBlockedCallback& callback);
152 void DidBlockNavigation(content::WebContents* web_contents);
154 void AddObserver(SupervisedUserServiceObserver* observer);
155 void RemoveObserver(SupervisedUserServiceObserver* observer);
157 void AddPermissionRequestCreator(
158 scoped_ptr<PermissionRequestCreator> creator);
160 // SyncTypePreferenceProvider implementation:
161 syncer::ModelTypeSet GetPreferredDataTypes() const override;
163 // sync_driver::SyncServiceObserver implementation:
164 void OnStateChanged() override;
166 // chrome::BrowserListObserver implementation:
167 void OnBrowserSetLastActive(Browser* browser) override;
169 // SupervisedUserURLFilter::Observer implementation:
170 void OnSiteListUpdated() override;
172 private:
173 friend class SupervisedUserServiceExtensionTestBase;
174 friend class SupervisedUserServiceFactory;
175 FRIEND_TEST_ALL_PREFIXES(SingleClientSupervisedUserSettingsSyncTest, Sanity);
176 FRIEND_TEST_ALL_PREFIXES(SupervisedUserServiceTest, ClearOmitOnRegistration);
177 FRIEND_TEST_ALL_PREFIXES(SupervisedUserServiceTest,
178 ChangesIncludedSessionOnChangedSettings);
179 FRIEND_TEST_ALL_PREFIXES(SupervisedUserServiceTest,
180 ChangesSyncSessionStateOnChangedSettings);
181 FRIEND_TEST_ALL_PREFIXES(SupervisedUserServiceExtensionTest,
182 ExtensionManagementPolicyProvider);
184 using CreatePermissionRequestCallback =
185 base::Callback<void(PermissionRequestCreator*, const SuccessCallback&)>;
187 // A bridge from the UI thread to the SupervisedUserURLFilters, one of which
188 // lives on the IO thread. This class mediates access to them and makes sure
189 // they are kept in sync.
190 class URLFilterContext {
191 public:
192 URLFilterContext();
193 ~URLFilterContext();
195 SupervisedUserURLFilter* ui_url_filter() const;
196 SupervisedUserURLFilter* io_url_filter() const;
198 void SetDefaultFilteringBehavior(
199 SupervisedUserURLFilter::FilteringBehavior behavior);
200 void LoadWhitelists(
201 const std::vector<scoped_refptr<SupervisedUserSiteList>>& site_lists);
202 void LoadBlacklist(const base::FilePath& path,
203 const base::Closure& callback);
204 void SetManualHosts(scoped_ptr<std::map<std::string, bool>> host_map);
205 void SetManualURLs(scoped_ptr<std::map<GURL, bool>> url_map);
207 void InitAsyncURLChecker(
208 const scoped_refptr<net::URLRequestContextGetter>& context);
210 void Clear();
212 private:
213 void OnBlacklistLoaded(const base::Closure& callback);
215 // SupervisedUserURLFilter is refcounted because the IO thread filter is
216 // used both by ProfileImplIOData and OffTheRecordProfileIOData (to filter
217 // network requests), so they both keep a reference to it.
218 // Clients should not keep references to the UI thread filter, however
219 // (the filter will live as long as the profile lives, and afterwards it
220 // should not be used anymore either).
221 scoped_refptr<SupervisedUserURLFilter> ui_url_filter_;
222 scoped_refptr<SupervisedUserURLFilter> io_url_filter_;
224 SupervisedUserBlacklist blacklist_;
226 DISALLOW_COPY_AND_ASSIGN(URLFilterContext);
229 // Use |SupervisedUserServiceFactory::GetForProfile(..)| to get
230 // an instance of this service.
231 explicit SupervisedUserService(Profile* profile);
233 void SetActive(bool active);
235 void OnCustodianProfileDownloaded(const base::string16& full_name);
237 void OnSupervisedUserRegistered(const AuthErrorCallback& callback,
238 Profile* custodian_profile,
239 const GoogleServiceAuthError& auth_error,
240 const std::string& token);
242 void SetupSync();
243 void StartSetupSync();
244 void FinishSetupSyncWhenReady();
245 void FinishSetupSync();
247 bool ProfileIsSupervised() const;
249 void OnCustodianInfoChanged();
251 #if defined(ENABLE_EXTENSIONS)
252 // extensions::ManagementPolicy::Provider implementation:
253 std::string GetDebugPolicyProviderName() const override;
254 bool UserMayLoad(const extensions::Extension* extension,
255 base::string16* error) const override;
256 bool UserMayModifySettings(const extensions::Extension* extension,
257 base::string16* error) const override;
258 bool MustRemainInstalled(const extensions::Extension* extension,
259 base::string16* error) const override;
261 // Extensions helper to SetActive().
262 void SetExtensionsActive();
263 #endif
265 SupervisedUserSettingsService* GetSettingsService();
267 size_t FindEnabledPermissionRequestCreator(size_t start);
268 void AddPermissionRequestInternal(
269 const CreatePermissionRequestCallback& create_request,
270 const SuccessCallback& callback,
271 size_t index);
272 void OnPermissionRequestIssued(
273 const CreatePermissionRequestCallback& create_request,
274 const SuccessCallback& callback,
275 size_t index,
276 bool success);
278 void OnSupervisedUserIdChanged();
280 void OnDefaultFilteringBehaviorChanged();
282 void OnSiteListsChanged(
283 const std::vector<scoped_refptr<SupervisedUserSiteList>>& site_lists);
285 // Asynchronously loads a blacklist from a binary file at |path| and applies
286 // it to the URL filters. If no file exists at |path| yet, downloads a file
287 // from |url| and stores it at |path| first.
288 void LoadBlacklist(const base::FilePath& path, const GURL& url);
290 void OnBlacklistFileChecked(const base::FilePath& path,
291 const GURL& url,
292 bool file_exists);
294 // Asynchronously loads a blacklist from a binary file at |path| and applies
295 // it to the URL filters.
296 void LoadBlacklistFromFile(const base::FilePath& path);
298 void OnBlacklistDownloadDone(const base::FilePath& path, bool success);
300 void OnBlacklistLoaded();
302 // Updates the manual overrides for hosts in the URL filters when the
303 // corresponding preference is changed.
304 void UpdateManualHosts();
306 // Updates the manual overrides for URLs in the URL filters when the
307 // corresponding preference is changed.
308 void UpdateManualURLs();
310 // Returns the human readable name of the supervised user.
311 std::string GetSupervisedUserName() const;
313 // Subscribes to the SupervisedUserPrefStore, refreshes
314 // |includes_sync_sessions_type_| and triggers reconfiguring the
315 // ProfileSyncService.
316 void OnHistoryRecordingStateChanged();
318 // Returns true if the syncer::SESSIONS type should be included in Sync.
319 bool IncludesSyncSessionsType() const;
321 // The option a custodian sets to either record or prevent recording the
322 // supervised user's history. Set by |FetchNewSessionSyncState()| and
323 // defaults to true.
324 bool includes_sync_sessions_type_;
326 // Owns us via the KeyedService mechanism.
327 Profile* profile_;
329 bool active_;
331 Delegate* delegate_;
333 PrefChangeRegistrar pref_change_registrar_;
335 // True iff we're waiting for the Sync service to be initialized.
336 bool waiting_for_sync_initialization_;
337 bool is_profile_active_;
339 std::vector<NavigationBlockedCallback> navigation_blocked_callbacks_;
341 // True only when |Init()| method has been called.
342 bool did_init_;
344 // True only when |Shutdown()| method has been called.
345 bool did_shutdown_;
347 URLFilterContext url_filter_context_;
348 scoped_ptr<FileDownloader> blacklist_downloader_;
350 scoped_ptr<SupervisedUserWhitelistService> whitelist_service_;
352 // Used to create permission requests.
353 ScopedVector<PermissionRequestCreator> permissions_creators_;
355 base::ObserverList<SupervisedUserServiceObserver> observer_list_;
357 base::WeakPtrFactory<SupervisedUserService> weak_ptr_factory_;
360 #endif // CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SERVICE_H_