Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / chrome / browser / supervised_user / supervised_user_service.h
blob7bbdfedefab7adc3b972c779ed10fd6ed1ed7fbe
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 <set>
9 #include <vector>
11 #include "base/callback.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/prefs/pref_change_registrar.h"
15 #include "base/scoped_observer.h"
16 #include "base/strings/string16.h"
17 #include "chrome/browser/supervised_user/supervised_user_url_filter.h"
18 #include "chrome/browser/supervised_user/supervised_users.h"
19 #include "chrome/browser/sync/profile_sync_service_observer.h"
20 #include "chrome/browser/sync/sync_type_preference_provider.h"
21 #include "chrome/browser/ui/browser_list_observer.h"
22 #include "components/keyed_service/core/keyed_service.h"
23 #include "content/public/browser/web_contents.h"
25 #if defined(ENABLE_EXTENSIONS)
26 #include "extensions/browser/extension_registry_observer.h"
27 #include "extensions/browser/management_policy.h"
28 #endif
30 class Browser;
31 class GoogleServiceAuthError;
32 class PermissionRequestCreator;
33 class Profile;
34 class SupervisedUserRegistrationUtility;
35 class SupervisedUserSettingsService;
36 class SupervisedUserSiteList;
37 class SupervisedUserURLFilter;
39 namespace extensions {
40 class ExtensionRegistry;
43 namespace user_prefs {
44 class PrefRegistrySyncable;
47 // This class handles all the information related to a given supervised profile
48 // (e.g. the installed content packs, the default URL filtering behavior, or
49 // manual whitelist/blacklist overrides).
50 class SupervisedUserService : public KeyedService,
51 #if defined(ENABLE_EXTENSIONS)
52 public extensions::ManagementPolicy::Provider,
53 public extensions::ExtensionRegistryObserver,
54 #endif
55 public SyncTypePreferenceProvider,
56 public ProfileSyncServiceObserver,
57 public chrome::BrowserListObserver {
58 public:
59 typedef std::vector<base::string16> CategoryList;
60 typedef base::Callback<void(content::WebContents*)> NavigationBlockedCallback;
61 typedef base::Callback<void(const GoogleServiceAuthError&)> AuthErrorCallback;
63 enum ManualBehavior {
64 MANUAL_NONE = 0,
65 MANUAL_ALLOW,
66 MANUAL_BLOCK
69 class Delegate {
70 public:
71 virtual ~Delegate() {}
72 // Returns true to indicate that the delegate handled the (de)activation, or
73 // false to indicate that the SupervisedUserService itself should handle it.
74 virtual bool SetActive(bool active) = 0;
77 virtual ~SupervisedUserService();
79 // ProfileKeyedService override:
80 virtual void Shutdown() OVERRIDE;
82 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
84 void SetDelegate(Delegate* delegate);
86 // Returns the URL filter for the IO thread, for filtering network requests
87 // (in SupervisedUserResourceThrottle).
88 scoped_refptr<const SupervisedUserURLFilter> GetURLFilterForIOThread();
90 // Returns the URL filter for the UI thread, for filtering navigations and
91 // classifying sites in the history view.
92 SupervisedUserURLFilter* GetURLFilterForUIThread();
94 // Returns the URL's category, obtained from the installed content packs.
95 int GetCategory(const GURL& url);
97 // Returns the list of all known human-readable category names, sorted by ID
98 // number. Called in the critical path of drawing the history UI, so needs to
99 // be fast.
100 void GetCategoryNames(CategoryList* list);
102 // Whether the user can request access to blocked URLs.
103 bool AccessRequestsEnabled();
105 void OnPermissionRequestIssued();
107 // Adds an access request for the given URL. The requests are stored using
108 // a prefix followed by a URIEncoded version of the URL. Each entry contains
109 // a dictionary which currently has the timestamp of the request in it.
110 void AddAccessRequest(const GURL& url);
112 // Returns the email address of the custodian.
113 std::string GetCustodianEmailAddress() const;
115 // Returns the name of the custodian, or the email address if the name is
116 // empty.
117 std::string GetCustodianName() const;
119 // These methods allow querying and modifying the manual filtering behavior.
120 // The manual behavior is set by the user and overrides all other settings
121 // (whitelists or the default behavior).
123 // Returns the manual behavior for the given host.
124 ManualBehavior GetManualBehaviorForHost(const std::string& hostname);
126 // Returns the manual behavior for the given URL.
127 ManualBehavior GetManualBehaviorForURL(const GURL& url);
129 // Returns all URLS on the given host that have exceptions.
130 void GetManualExceptionsForHost(const std::string& host,
131 std::vector<GURL>* urls);
133 // Initializes this object. This method does nothing if the profile is not
134 // supervised.
135 void Init();
137 // Initializes this profile for syncing, using the provided |refresh_token| to
138 // mint access tokens for Sync.
139 void InitSync(const std::string& refresh_token);
141 // Convenience method that registers this supervised user using
142 // |registration_utility| and initializes sync with the returned token.
143 // The |callback| will be called when registration is complete,
144 // whether it succeeded or not -- unless registration was cancelled manually,
145 // in which case the callback will be ignored.
146 void RegisterAndInitSync(
147 SupervisedUserRegistrationUtility* registration_utility,
148 Profile* custodian_profile,
149 const std::string& supervised_user_id,
150 const AuthErrorCallback& callback);
152 void set_elevated_for_testing(bool skip) {
153 elevated_for_testing_ = skip;
156 void AddNavigationBlockedCallback(const NavigationBlockedCallback& callback);
157 void DidBlockNavigation(content::WebContents* web_contents);
159 #if defined(ENABLE_EXTENSIONS)
160 // extensions::ManagementPolicy::Provider implementation:
161 virtual std::string GetDebugPolicyProviderName() const OVERRIDE;
162 virtual bool UserMayLoad(const extensions::Extension* extension,
163 base::string16* error) const OVERRIDE;
164 virtual bool UserMayModifySettings(const extensions::Extension* extension,
165 base::string16* error) const OVERRIDE;
167 // extensions::ExtensionRegistryObserver implementation.
168 virtual void OnExtensionLoaded(
169 content::BrowserContext* browser_context,
170 const extensions::Extension* extension) OVERRIDE;
171 virtual void OnExtensionUnloaded(
172 content::BrowserContext* browser_context,
173 const extensions::Extension* extension,
174 extensions::UnloadedExtensionInfo::Reason reason) OVERRIDE;
175 #endif
177 // SyncTypePreferenceProvider implementation:
178 virtual syncer::ModelTypeSet GetPreferredDataTypes() const OVERRIDE;
180 // ProfileSyncServiceObserver implementation:
181 virtual void OnStateChanged() OVERRIDE;
183 // chrome::BrowserListObserver implementation:
184 virtual void OnBrowserSetLastActive(Browser* browser) OVERRIDE;
186 private:
187 friend class SupervisedUserServiceExtensionTestBase;
188 friend class SupervisedUserServiceFactory;
189 FRIEND_TEST_ALL_PREFIXES(SupervisedUserServiceTest, ClearOmitOnRegistration);
191 // A bridge from the UI thread to the SupervisedUserURLFilters, one of which
192 // lives on the IO thread. This class mediates access to them and makes sure
193 // they are kept in sync.
194 class URLFilterContext {
195 public:
196 URLFilterContext();
197 ~URLFilterContext();
199 SupervisedUserURLFilter* ui_url_filter() const;
200 SupervisedUserURLFilter* io_url_filter() const;
202 void SetDefaultFilteringBehavior(
203 SupervisedUserURLFilter::FilteringBehavior behavior);
204 void LoadWhitelists(ScopedVector<SupervisedUserSiteList> site_lists);
205 void SetManualHosts(scoped_ptr<std::map<std::string, bool> > host_map);
206 void SetManualURLs(scoped_ptr<std::map<GURL, bool> > url_map);
208 private:
209 // SupervisedUserURLFilter is refcounted because the IO thread filter is
210 // used both by ProfileImplIOData and OffTheRecordProfileIOData (to filter
211 // network requests), so they both keep a reference to it.
212 // Clients should not keep references to the UI thread filter, however
213 // (the filter will live as long as the profile lives, and afterwards it
214 // should not be used anymore either).
215 scoped_refptr<SupervisedUserURLFilter> ui_url_filter_;
216 scoped_refptr<SupervisedUserURLFilter> io_url_filter_;
218 DISALLOW_COPY_AND_ASSIGN(URLFilterContext);
221 // Use |SupervisedUserServiceFactory::GetForProfile(..)| to get
222 // an instance of this service.
223 explicit SupervisedUserService(Profile* profile);
225 void SetActive(bool active);
227 void OnCustodianProfileDownloaded(const base::string16& full_name);
229 void OnSupervisedUserRegistered(const AuthErrorCallback& callback,
230 Profile* custodian_profile,
231 const GoogleServiceAuthError& auth_error,
232 const std::string& token);
234 void SetupSync();
235 void StartSetupSync();
236 void FinishSetupSyncWhenReady();
237 void FinishSetupSync();
239 bool ProfileIsSupervised() const;
241 #if defined(ENABLE_EXTENSIONS)
242 // Internal implementation for ExtensionManagementPolicy::Delegate methods.
243 // If |error| is not NULL, it will be filled with an error message if the
244 // requested extension action (install, modify status, etc.) is not permitted.
245 bool ExtensionManagementPolicyImpl(const extensions::Extension* extension,
246 base::string16* error) const;
248 // Returns a list of all installed and enabled site lists in the current
249 // supervised profile.
250 ScopedVector<SupervisedUserSiteList> GetActiveSiteLists();
252 // Extensions helper to SetActive().
253 void SetExtensionsActive();
254 #endif
256 SupervisedUserSettingsService* GetSettingsService();
258 void OnSupervisedUserIdChanged();
260 void OnDefaultFilteringBehaviorChanged();
262 void UpdateSiteLists();
264 // Updates the manual overrides for hosts in the URL filters when the
265 // corresponding preference is changed.
266 void UpdateManualHosts();
268 // Updates the manual overrides for URLs in the URL filters when the
269 // corresponding preference is changed.
270 void UpdateManualURLs();
272 // Returns the human readable name of the supervised user.
273 std::string GetSupervisedUserName() const;
275 // Owns us via the KeyedService mechanism.
276 Profile* profile_;
278 bool active_;
280 Delegate* delegate_;
282 #if defined(ENABLE_EXTENSIONS)
283 ScopedObserver<extensions::ExtensionRegistry,
284 extensions::ExtensionRegistryObserver>
285 extension_registry_observer_;
286 #endif
288 PrefChangeRegistrar pref_change_registrar_;
290 // True iff we're waiting for the Sync service to be initialized.
291 bool waiting_for_sync_initialization_;
292 bool is_profile_active_;
294 std::vector<NavigationBlockedCallback> navigation_blocked_callbacks_;
296 // Sets a profile in elevated state for testing if set to true.
297 bool elevated_for_testing_;
299 // True only when |Init()| method has been called.
300 bool did_init_;
302 // True only when |Shutdown()| method has been called.
303 bool did_shutdown_;
305 URLFilterContext url_filter_context_;
307 // Used to create permission requests.
308 scoped_ptr<PermissionRequestCreator> permissions_creator_;
310 // True iff we are waiting for a permission request to be issued.
311 bool waiting_for_permissions_;
313 base::WeakPtrFactory<SupervisedUserService> weak_ptr_factory_;
316 #endif // CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_SERVICE_H_