blob: 3bcc321b4520da89805a012cfdb9b8a4ee9e1fef [file] [log] [blame]
[email protected]0850e842013-01-19 03:44:311// Copyright (c) 2013 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.
4
5#ifndef CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_SERVICE_H_
6#define CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_SERVICE_H_
7
[email protected]0d9a1da82013-03-14 21:52:078#include <set>
[email protected]0850e842013-01-19 03:44:319#include <vector>
10
[email protected]a581ea22013-05-06 12:34:1911#include "base/memory/scoped_ptr.h"
[email protected]1ab137b2013-03-21 03:33:1812#include "base/prefs/pref_change_registrar.h"
[email protected]f9b294362013-06-10 20:22:3113#include "base/strings/string16.h"
[email protected]0850e842013-01-19 03:44:3114#include "chrome/browser/extensions/management_policy.h"
15#include "chrome/browser/managed_mode/managed_mode_url_filter.h"
[email protected]4e2b6f32013-06-04 16:38:3416#include "chrome/browser/profiles/profile_manager.h"
[email protected]a243d644c2013-06-20 18:37:5517#include "chrome/browser/sync/profile_sync_service_observer.h"
[email protected]0dd6f2032013-05-20 23:33:4018#include "components/browser_context_keyed_service/browser_context_keyed_service.h"
[email protected]0850e842013-01-19 03:44:3119#include "content/public/browser/notification_observer.h"
20#include "content/public/browser/notification_registrar.h"
[email protected]bfd8cf982013-03-04 15:24:0021#include "content/public/browser/web_contents.h"
[email protected]0850e842013-01-19 03:44:3122
[email protected]509ad1a92013-03-19 21:41:0623class Browser;
[email protected]acfcfbb2013-05-13 18:01:2724class GoogleServiceAuthError;
[email protected]0850e842013-01-19 03:44:3125class ManagedModeURLFilter;
26class ManagedModeSiteList;
[email protected]acfcfbb2013-05-13 18:01:2727class ManagedUserRegistrationService;
[email protected]0850e842013-01-19 03:44:3128class Profile;
29
[email protected]e861bba2013-06-17 15:20:5430namespace policy {
31class ManagedModePolicyProvider;
32}
33
[email protected]443e9312013-05-06 06:17:3434namespace user_prefs {
35class PrefRegistrySyncable;
36}
37
[email protected]0850e842013-01-19 03:44:3138// This class handles all the information related to a given managed profile
39// (e.g. the installed content packs, the default URL filtering behavior, or
40// manual whitelist/blacklist overrides).
[email protected]f1484c52013-05-22 23:25:4441class ManagedUserService : public BrowserContextKeyedService,
[email protected]0850e842013-01-19 03:44:3142 public extensions::ManagementPolicy::Provider,
[email protected]a243d644c2013-06-20 18:37:5543 public ProfileSyncServiceObserver,
[email protected]0850e842013-01-19 03:44:3144 public content::NotificationObserver {
45 public:
46 typedef std::vector<string16> CategoryList;
47
[email protected]5e022292013-02-06 16:42:1748 enum ManualBehavior {
49 MANUAL_NONE = 0,
50 MANUAL_ALLOW,
51 MANUAL_BLOCK
52 };
53
[email protected]0850e842013-01-19 03:44:3154 explicit ManagedUserService(Profile* profile);
55 virtual ~ManagedUserService();
56
[email protected]a243d644c2013-06-20 18:37:5557 // ProfileKeyedService override:
58 virtual void Shutdown() OVERRIDE;
59
[email protected]0850e842013-01-19 03:44:3160 bool ProfileIsManaged() const;
[email protected]ce764fc2013-03-25 12:01:4461
[email protected]a19df3e2013-05-21 00:03:0362 // Checks whether the given profile is managed without constructing a
63 // ManagedUserService (which could lead to cyclic dependencies).
64 static bool ProfileIsManaged(Profile* profile);
65
[email protected]37ca3fe02013-07-05 15:32:4466 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
[email protected]0850e842013-01-19 03:44:3167
[email protected]a7d65a82013-07-02 17:01:0568 // Returns true if managed users are enabled by either Finch or the command
69 // line flag.
70 // TODO(pamg, sergiu): Remove this once the feature is fully launched.
[email protected]e34a7972013-05-24 13:47:0271 static bool AreManagedUsersEnabled();
72
[email protected]0850e842013-01-19 03:44:3173 // Returns the URL filter for the IO thread, for filtering network requests
74 // (in ManagedModeResourceThrottle).
75 scoped_refptr<const ManagedModeURLFilter> GetURLFilterForIOThread();
76
77 // Returns the URL filter for the UI thread, for filtering navigations and
78 // classifying sites in the history view.
79 ManagedModeURLFilter* GetURLFilterForUIThread();
80
81 // Returns the URL's category, obtained from the installed content packs.
82 int GetCategory(const GURL& url);
83
84 // Returns the list of all known human-readable category names, sorted by ID
85 // number. Called in the critical path of drawing the history UI, so needs to
86 // be fast.
87 void GetCategoryNames(CategoryList* list);
88
[email protected]e861bba2013-06-17 15:20:5489 // Adds an access request for the given URL. The requests are stored using
90 // a prefix followed by a URIEncoded version of the URL. Each entry contains
91 // a dictionary which currently has the timestamp of the request in it.
92 void AddAccessRequest(const GURL& url);
93
[email protected]a9c2d642013-05-31 14:37:1494 // Returns the email address of the custodian.
95 std::string GetCustodianEmailAddress() const;
96
[email protected]fae057a2013-06-21 22:46:0897 // Returns the name of the custodian, or the email address if the name is
98 // empty.
99 std::string GetCustodianName() const;
100
[email protected]5e022292013-02-06 16:42:17101 // These methods allow querying and modifying the manual filtering behavior.
102 // The manual behavior is set by the user and overrides all other settings
103 // (whitelists or the default behavior).
[email protected]0850e842013-01-19 03:44:31104
[email protected]5e022292013-02-06 16:42:17105 // Returns the manual behavior for the given host.
106 ManualBehavior GetManualBehaviorForHost(const std::string& hostname);
[email protected]0850e842013-01-19 03:44:31107
[email protected]5e022292013-02-06 16:42:17108 // Returns the manual behavior for the given URL.
109 ManualBehavior GetManualBehaviorForURL(const GURL& url);
[email protected]0850e842013-01-19 03:44:31110
[email protected]43257902013-03-26 20:59:37111 // Returns all URLS on the given host that have exceptions.
112 void GetManualExceptionsForHost(const std::string& host,
113 std::vector<GURL>* urls);
114
[email protected]0850e842013-01-19 03:44:31115 // Initializes this object. This method does nothing if the profile is not
[email protected]ce019142013-02-12 18:08:10116 // managed.
[email protected]0850e842013-01-19 03:44:31117 void Init();
118
[email protected]a581ea22013-05-06 12:34:19119 // Marks the profile as managed and initializes it.
120 void InitForTesting();
121
[email protected]a243d644c2013-06-20 18:37:55122 // Initializes this profile for syncing, using the provided |refresh_token| to
123 // mint access tokens for Sync.
124 void InitSync(const std::string& refresh_token);
[email protected]acfcfbb2013-05-13 18:01:27125
126 // Convenience method that registers this managed user with
127 // |registration_service| and initializes sync with the returned token.
128 // Note that |registration_service| should belong to the custodian's profile,
[email protected]54d016d22013-06-10 08:40:23129 // not this one. The |callback| will be called when registration is complete,
130 // whether it suceeded or not -- unless registration was cancelled in the
131 // ManagedUserRegistrationService manually, in which case the callback will
132 // be ignored.
[email protected]f252df2e2013-06-06 23:47:19133 void RegisterAndInitSync(Profile* custodian_profile,
134 const ProfileManager::CreateCallback& callback);
[email protected]acfcfbb2013-05-13 18:01:27135
[email protected]a19df3e2013-05-21 00:03:03136 // Returns a pseudo-email address for systems that expect well-formed email
137 // addresses (like Sync), even though we're not signed in.
138 static const char* GetManagedUserPseudoEmail();
139
[email protected]4f02aac12013-05-20 05:19:06140 void set_elevated_for_testing(bool skip) {
141 elevated_for_testing_ = skip;
[email protected]849749d2013-05-06 17:30:45142 }
143
[email protected]ce019142013-02-12 18:08:10144 // extensions::ManagementPolicy::Provider implementation:
[email protected]0850e842013-01-19 03:44:31145 virtual std::string GetDebugPolicyProviderName() const OVERRIDE;
146 virtual bool UserMayLoad(const extensions::Extension* extension,
147 string16* error) const OVERRIDE;
148 virtual bool UserMayModifySettings(const extensions::Extension* extension,
149 string16* error) const OVERRIDE;
150
[email protected]a243d644c2013-06-20 18:37:55151 // ProfileSyncServiceObserver implementation:
152 virtual void OnStateChanged() OVERRIDE;
153
[email protected]0850e842013-01-19 03:44:31154 // content::NotificationObserver implementation:
155 virtual void Observe(int type,
156 const content::NotificationSource& source,
157 const content::NotificationDetails& details) OVERRIDE;
158
159 private:
160 friend class ManagedUserServiceExtensionTest;
161
162 // A bridge from ManagedMode (which lives on the UI thread) to the
163 // ManagedModeURLFilters, one of which lives on the IO thread. This class
164 // mediates access to them and makes sure they are kept in sync.
165 class URLFilterContext {
166 public:
167 URLFilterContext();
168 ~URLFilterContext();
169
170 ManagedModeURLFilter* ui_url_filter() const;
171 ManagedModeURLFilter* io_url_filter() const;
172
173 void SetDefaultFilteringBehavior(
174 ManagedModeURLFilter::FilteringBehavior behavior);
175 void LoadWhitelists(ScopedVector<ManagedModeSiteList> site_lists);
[email protected]5e022292013-02-06 16:42:17176 void SetManualHosts(scoped_ptr<std::map<std::string, bool> > host_map);
177 void SetManualURLs(scoped_ptr<std::map<GURL, bool> > url_map);
[email protected]0850e842013-01-19 03:44:31178
179 private:
180 // ManagedModeURLFilter is refcounted because the IO thread filter is used
181 // both by ProfileImplIOData and OffTheRecordProfileIOData (to filter
182 // network requests), so they both keep a reference to it.
183 // Clients should not keep references to the UI thread filter, however
184 // (the filter will live as long as the profile lives, and afterwards it
185 // should not be used anymore either).
186 scoped_refptr<ManagedModeURLFilter> ui_url_filter_;
187 scoped_refptr<ManagedModeURLFilter> io_url_filter_;
188
189 DISALLOW_COPY_AND_ASSIGN(URLFilterContext);
190 };
191
[email protected]200729f82013-06-22 07:05:55192 void OnCustodianProfileDownloaded(const string16& full_name);
193
[email protected]4e2b6f32013-06-04 16:38:34194 void OnManagedUserRegistered(const ProfileManager::CreateCallback& callback,
[email protected]f252df2e2013-06-06 23:47:19195 Profile* custodian_profile,
[email protected]4e2b6f32013-06-04 16:38:34196 const GoogleServiceAuthError& auth_error,
[email protected]acfcfbb2013-05-13 18:01:27197 const std::string& token);
198
[email protected]a243d644c2013-06-20 18:37:55199 void SetupSync();
200
[email protected]0850e842013-01-19 03:44:31201 // Internal implementation for ExtensionManagementPolicy::Delegate methods.
202 // If |error| is not NULL, it will be filled with an error message if the
203 // requested extension action (install, modify status, etc.) is not permitted.
[email protected]0d9a1da82013-03-14 21:52:07204 bool ExtensionManagementPolicyImpl(const std::string& extension_id,
205 string16* error) const;
[email protected]0850e842013-01-19 03:44:31206
207 // Returns a list of all installed and enabled site lists in the current
208 // managed profile.
209 ScopedVector<ManagedModeSiteList> GetActiveSiteLists();
210
[email protected]e861bba2013-06-17 15:20:54211 policy::ManagedModePolicyProvider* GetPolicyProvider();
212
[email protected]0850e842013-01-19 03:44:31213 void OnDefaultFilteringBehaviorChanged();
214
215 void UpdateSiteLists();
216
[email protected]5e022292013-02-06 16:42:17217 // Updates the manual overrides for hosts in the URL filters when the
218 // corresponding preference is changed.
219 void UpdateManualHosts();
[email protected]0850e842013-01-19 03:44:31220
[email protected]5e022292013-02-06 16:42:17221 // Updates the manual overrides for URLs in the URL filters when the
222 // corresponding preference is changed.
223 void UpdateManualURLs();
[email protected]0850e842013-01-19 03:44:31224
[email protected]acfcfbb2013-05-13 18:01:27225 base::WeakPtrFactory<ManagedUserService> weak_ptr_factory_;
226
[email protected]f1484c52013-05-22 23:25:44227 // Owns us via the BrowserContextKeyedService mechanism.
[email protected]0850e842013-01-19 03:44:31228 Profile* profile_;
229
[email protected]0850e842013-01-19 03:44:31230 content::NotificationRegistrar registrar_;
231 PrefChangeRegistrar pref_change_registrar_;
232
[email protected]a243d644c2013-06-20 18:37:55233 // True iff we're waiting for the Sync service to be initialized.
234 bool waiting_for_sync_initialization_;
235
[email protected]4f02aac12013-05-20 05:19:06236 // Sets a profile in elevated state for testing if set to true.
237 bool elevated_for_testing_;
[email protected]849749d2013-05-06 17:30:45238
[email protected]0850e842013-01-19 03:44:31239 URLFilterContext url_filter_context_;
240};
241
242#endif // CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_SERVICE_H_