| OLD | NEW |
| (Empty) |
| 1 // 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 | |
| 8 #include <set> | |
| 9 #include <vector> | |
| 10 | |
| 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/managed_mode/managed_mode_url_filter.h" | |
| 18 #include "chrome/browser/managed_mode/managed_users.h" | |
| 19 #include "chrome/browser/sync/profile_sync_service_observer.h" | |
| 20 #include "chrome/browser/ui/browser_list_observer.h" | |
| 21 #include "components/keyed_service/core/keyed_service.h" | |
| 22 #include "content/public/browser/web_contents.h" | |
| 23 #include "extensions/browser/extension_registry_observer.h" | |
| 24 #include "extensions/browser/management_policy.h" | |
| 25 | |
| 26 class Browser; | |
| 27 class GoogleServiceAuthError; | |
| 28 class ManagedModeURLFilter; | |
| 29 class ManagedModeSiteList; | |
| 30 class ManagedUserRegistrationUtility; | |
| 31 class ManagedUserSettingsService; | |
| 32 class PermissionRequestCreator; | |
| 33 class Profile; | |
| 34 | |
| 35 namespace extensions { | |
| 36 class ExtensionRegistry; | |
| 37 } | |
| 38 | |
| 39 namespace user_prefs { | |
| 40 class PrefRegistrySyncable; | |
| 41 } | |
| 42 | |
| 43 // This class handles all the information related to a given managed profile | |
| 44 // (e.g. the installed content packs, the default URL filtering behavior, or | |
| 45 // manual whitelist/blacklist overrides). | |
| 46 class ManagedUserService : public KeyedService, | |
| 47 public extensions::ManagementPolicy::Provider, | |
| 48 public ProfileSyncServiceObserver, | |
| 49 public extensions::ExtensionRegistryObserver, | |
| 50 public chrome::BrowserListObserver { | |
| 51 public: | |
| 52 typedef std::vector<base::string16> CategoryList; | |
| 53 typedef base::Callback<void(content::WebContents*)> NavigationBlockedCallback; | |
| 54 typedef base::Callback<void(const GoogleServiceAuthError&)> AuthErrorCallback; | |
| 55 | |
| 56 enum ManualBehavior { | |
| 57 MANUAL_NONE = 0, | |
| 58 MANUAL_ALLOW, | |
| 59 MANUAL_BLOCK | |
| 60 }; | |
| 61 | |
| 62 class Delegate { | |
| 63 public: | |
| 64 virtual ~Delegate() {} | |
| 65 // Returns true to indicate that the delegate handled the (de)activation, or | |
| 66 // false to indicate that the ManagedUserService itself should handle it. | |
| 67 virtual bool SetActive(bool active) = 0; | |
| 68 }; | |
| 69 | |
| 70 virtual ~ManagedUserService(); | |
| 71 | |
| 72 // ProfileKeyedService override: | |
| 73 virtual void Shutdown() OVERRIDE; | |
| 74 | |
| 75 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | |
| 76 | |
| 77 static void MigrateUserPrefs(PrefService* prefs); | |
| 78 | |
| 79 void SetDelegate(Delegate* delegate); | |
| 80 | |
| 81 // Returns the URL filter for the IO thread, for filtering network requests | |
| 82 // (in ManagedModeResourceThrottle). | |
| 83 scoped_refptr<const ManagedModeURLFilter> GetURLFilterForIOThread(); | |
| 84 | |
| 85 // Returns the URL filter for the UI thread, for filtering navigations and | |
| 86 // classifying sites in the history view. | |
| 87 ManagedModeURLFilter* GetURLFilterForUIThread(); | |
| 88 | |
| 89 // Returns the URL's category, obtained from the installed content packs. | |
| 90 int GetCategory(const GURL& url); | |
| 91 | |
| 92 // Returns the list of all known human-readable category names, sorted by ID | |
| 93 // number. Called in the critical path of drawing the history UI, so needs to | |
| 94 // be fast. | |
| 95 void GetCategoryNames(CategoryList* list); | |
| 96 | |
| 97 // Whether the user can request access to blocked URLs. | |
| 98 bool AccessRequestsEnabled(); | |
| 99 | |
| 100 void OnPermissionRequestIssued(); | |
| 101 | |
| 102 // Adds an access request for the given URL. The requests are stored using | |
| 103 // a prefix followed by a URIEncoded version of the URL. Each entry contains | |
| 104 // a dictionary which currently has the timestamp of the request in it. | |
| 105 void AddAccessRequest(const GURL& url); | |
| 106 | |
| 107 // Returns the email address of the custodian. | |
| 108 std::string GetCustodianEmailAddress() const; | |
| 109 | |
| 110 // Returns the name of the custodian, or the email address if the name is | |
| 111 // empty. | |
| 112 std::string GetCustodianName() const; | |
| 113 | |
| 114 // These methods allow querying and modifying the manual filtering behavior. | |
| 115 // The manual behavior is set by the user and overrides all other settings | |
| 116 // (whitelists or the default behavior). | |
| 117 | |
| 118 // Returns the manual behavior for the given host. | |
| 119 ManualBehavior GetManualBehaviorForHost(const std::string& hostname); | |
| 120 | |
| 121 // Returns the manual behavior for the given URL. | |
| 122 ManualBehavior GetManualBehaviorForURL(const GURL& url); | |
| 123 | |
| 124 // Returns all URLS on the given host that have exceptions. | |
| 125 void GetManualExceptionsForHost(const std::string& host, | |
| 126 std::vector<GURL>* urls); | |
| 127 | |
| 128 // Initializes this object. This method does nothing if the profile is not | |
| 129 // managed. | |
| 130 void Init(); | |
| 131 | |
| 132 // Initializes this profile for syncing, using the provided |refresh_token| to | |
| 133 // mint access tokens for Sync. | |
| 134 void InitSync(const std::string& refresh_token); | |
| 135 | |
| 136 // Convenience method that registers this managed user using | |
| 137 // |registration_utility| and initializes sync with the returned token. | |
| 138 // The |callback| will be called when registration is complete, | |
| 139 // whether it suceeded or not -- unless registration was cancelled manually, | |
| 140 // in which case the callback will be ignored. | |
| 141 void RegisterAndInitSync(ManagedUserRegistrationUtility* registration_utility, | |
| 142 Profile* custodian_profile, | |
| 143 const std::string& managed_user_id, | |
| 144 const AuthErrorCallback& callback); | |
| 145 | |
| 146 void set_elevated_for_testing(bool skip) { | |
| 147 elevated_for_testing_ = skip; | |
| 148 } | |
| 149 | |
| 150 void AddNavigationBlockedCallback(const NavigationBlockedCallback& callback); | |
| 151 void DidBlockNavigation(content::WebContents* web_contents); | |
| 152 | |
| 153 // extensions::ManagementPolicy::Provider implementation: | |
| 154 virtual std::string GetDebugPolicyProviderName() const OVERRIDE; | |
| 155 virtual bool UserMayLoad(const extensions::Extension* extension, | |
| 156 base::string16* error) const OVERRIDE; | |
| 157 virtual bool UserMayModifySettings(const extensions::Extension* extension, | |
| 158 base::string16* error) const OVERRIDE; | |
| 159 | |
| 160 // ProfileSyncServiceObserver implementation: | |
| 161 virtual void OnStateChanged() OVERRIDE; | |
| 162 | |
| 163 // extensions::ExtensionRegistryObserver implementation. | |
| 164 virtual void OnExtensionLoaded( | |
| 165 content::BrowserContext* browser_context, | |
| 166 const extensions::Extension* extension) OVERRIDE; | |
| 167 virtual void OnExtensionUnloaded( | |
| 168 content::BrowserContext* browser_context, | |
| 169 const extensions::Extension* extension, | |
| 170 extensions::UnloadedExtensionInfo::Reason reason) OVERRIDE; | |
| 171 | |
| 172 // chrome::BrowserListObserver implementation: | |
| 173 virtual void OnBrowserSetLastActive(Browser* browser) OVERRIDE; | |
| 174 | |
| 175 private: | |
| 176 friend class ManagedUserServiceExtensionTestBase; | |
| 177 friend class ManagedUserServiceFactory; | |
| 178 FRIEND_TEST_ALL_PREFIXES(ManagedUserServiceTest, ClearOmitOnRegistration); | |
| 179 | |
| 180 // A bridge from ManagedMode (which lives on the UI thread) to the | |
| 181 // ManagedModeURLFilters, one of which lives on the IO thread. This class | |
| 182 // mediates access to them and makes sure they are kept in sync. | |
| 183 class URLFilterContext { | |
| 184 public: | |
| 185 URLFilterContext(); | |
| 186 ~URLFilterContext(); | |
| 187 | |
| 188 ManagedModeURLFilter* ui_url_filter() const; | |
| 189 ManagedModeURLFilter* io_url_filter() const; | |
| 190 | |
| 191 void SetDefaultFilteringBehavior( | |
| 192 ManagedModeURLFilter::FilteringBehavior behavior); | |
| 193 void LoadWhitelists(ScopedVector<ManagedModeSiteList> site_lists); | |
| 194 void SetManualHosts(scoped_ptr<std::map<std::string, bool> > host_map); | |
| 195 void SetManualURLs(scoped_ptr<std::map<GURL, bool> > url_map); | |
| 196 | |
| 197 private: | |
| 198 // ManagedModeURLFilter is refcounted because the IO thread filter is used | |
| 199 // both by ProfileImplIOData and OffTheRecordProfileIOData (to filter | |
| 200 // network requests), so they both keep a reference to it. | |
| 201 // Clients should not keep references to the UI thread filter, however | |
| 202 // (the filter will live as long as the profile lives, and afterwards it | |
| 203 // should not be used anymore either). | |
| 204 scoped_refptr<ManagedModeURLFilter> ui_url_filter_; | |
| 205 scoped_refptr<ManagedModeURLFilter> io_url_filter_; | |
| 206 | |
| 207 DISALLOW_COPY_AND_ASSIGN(URLFilterContext); | |
| 208 }; | |
| 209 | |
| 210 // Use |ManagedUserServiceFactory::GetForProfile(..)| to get | |
| 211 // an instance of this service. | |
| 212 explicit ManagedUserService(Profile* profile); | |
| 213 | |
| 214 void SetActive(bool active); | |
| 215 | |
| 216 void OnCustodianProfileDownloaded(const base::string16& full_name); | |
| 217 | |
| 218 void OnManagedUserRegistered(const AuthErrorCallback& callback, | |
| 219 Profile* custodian_profile, | |
| 220 const GoogleServiceAuthError& auth_error, | |
| 221 const std::string& token); | |
| 222 | |
| 223 void SetupSync(); | |
| 224 | |
| 225 bool ProfileIsManaged() const; | |
| 226 | |
| 227 // Internal implementation for ExtensionManagementPolicy::Delegate methods. | |
| 228 // If |error| is not NULL, it will be filled with an error message if the | |
| 229 // requested extension action (install, modify status, etc.) is not permitted. | |
| 230 bool ExtensionManagementPolicyImpl(const extensions::Extension* extension, | |
| 231 base::string16* error) const; | |
| 232 | |
| 233 // Returns a list of all installed and enabled site lists in the current | |
| 234 // managed profile. | |
| 235 ScopedVector<ManagedModeSiteList> GetActiveSiteLists(); | |
| 236 | |
| 237 ManagedUserSettingsService* GetSettingsService(); | |
| 238 | |
| 239 void OnManagedUserIdChanged(); | |
| 240 | |
| 241 void OnDefaultFilteringBehaviorChanged(); | |
| 242 | |
| 243 void UpdateSiteLists(); | |
| 244 | |
| 245 // Updates the manual overrides for hosts in the URL filters when the | |
| 246 // corresponding preference is changed. | |
| 247 void UpdateManualHosts(); | |
| 248 | |
| 249 // Updates the manual overrides for URLs in the URL filters when the | |
| 250 // corresponding preference is changed. | |
| 251 void UpdateManualURLs(); | |
| 252 | |
| 253 // Owns us via the KeyedService mechanism. | |
| 254 Profile* profile_; | |
| 255 | |
| 256 bool active_; | |
| 257 | |
| 258 Delegate* delegate_; | |
| 259 | |
| 260 ScopedObserver<extensions::ExtensionRegistry, | |
| 261 extensions::ExtensionRegistryObserver> | |
| 262 extension_registry_observer_; | |
| 263 | |
| 264 PrefChangeRegistrar pref_change_registrar_; | |
| 265 | |
| 266 // True iff we're waiting for the Sync service to be initialized. | |
| 267 bool waiting_for_sync_initialization_; | |
| 268 bool is_profile_active_; | |
| 269 | |
| 270 std::vector<NavigationBlockedCallback> navigation_blocked_callbacks_; | |
| 271 | |
| 272 // Sets a profile in elevated state for testing if set to true. | |
| 273 bool elevated_for_testing_; | |
| 274 | |
| 275 // True only when |Shutdown()| method has been called. | |
| 276 bool did_shutdown_; | |
| 277 | |
| 278 URLFilterContext url_filter_context_; | |
| 279 | |
| 280 // Used to create permission requests. | |
| 281 scoped_ptr<PermissionRequestCreator> permissions_creator_; | |
| 282 | |
| 283 // True iff we are waiting for a permission request to be issued. | |
| 284 bool waiting_for_permissions_; | |
| 285 | |
| 286 base::WeakPtrFactory<ManagedUserService> weak_ptr_factory_; | |
| 287 }; | |
| 288 | |
| 289 #endif // CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_SERVICE_H_ | |
| OLD | NEW |