blob: 7120eee9d75d1b9bcc75c771395216439882d823 [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
8#include <vector>
9
10#include "base/prefs/public/pref_change_registrar.h"
11#include "base/string16.h"
12#include "chrome/browser/extensions/management_policy.h"
13#include "chrome/browser/managed_mode/managed_mode_url_filter.h"
14#include "chrome/browser/profiles/profile_keyed_service.h"
15#include "content/public/browser/notification_observer.h"
16#include "content/public/browser/notification_registrar.h"
17
18class ManagedModeURLFilter;
19class ManagedModeSiteList;
20class PrefServiceSyncable;
21class Profile;
22
23// This class handles all the information related to a given managed profile
24// (e.g. the installed content packs, the default URL filtering behavior, or
25// manual whitelist/blacklist overrides).
26class ManagedUserService : public ProfileKeyedService,
27 public extensions::ManagementPolicy::Provider,
28 public content::NotificationObserver {
29 public:
30 typedef std::vector<string16> CategoryList;
31
[email protected]5e022292013-02-06 16:42:1732 enum ManualBehavior {
33 MANUAL_NONE = 0,
34 MANUAL_ALLOW,
35 MANUAL_BLOCK
36 };
37
[email protected]0850e842013-01-19 03:44:3138 explicit ManagedUserService(Profile* profile);
39 virtual ~ManagedUserService();
40
41 bool ProfileIsManaged() const;
42
43 static void RegisterUserPrefs(PrefServiceSyncable* prefs);
44
45 // Returns the URL filter for the IO thread, for filtering network requests
46 // (in ManagedModeResourceThrottle).
47 scoped_refptr<const ManagedModeURLFilter> GetURLFilterForIOThread();
48
49 // Returns the URL filter for the UI thread, for filtering navigations and
50 // classifying sites in the history view.
51 ManagedModeURLFilter* GetURLFilterForUIThread();
52
53 // Returns the URL's category, obtained from the installed content packs.
54 int GetCategory(const GURL& url);
55
56 // Returns the list of all known human-readable category names, sorted by ID
57 // number. Called in the critical path of drawing the history UI, so needs to
58 // be fast.
59 void GetCategoryNames(CategoryList* list);
60
[email protected]5e022292013-02-06 16:42:1761 // These methods allow querying and modifying the manual filtering behavior.
62 // The manual behavior is set by the user and overrides all other settings
63 // (whitelists or the default behavior).
[email protected]0850e842013-01-19 03:44:3164
[email protected]5e022292013-02-06 16:42:1765 // Returns the manual behavior for the given host.
66 ManualBehavior GetManualBehaviorForHost(const std::string& hostname);
[email protected]0850e842013-01-19 03:44:3167
[email protected]5e022292013-02-06 16:42:1768 // Sets the manual behavior for the given host.
69 void SetManualBehaviorForHosts(const std::vector<std::string>& hostnames,
70 ManualBehavior behavior);
[email protected]0850e842013-01-19 03:44:3171
[email protected]5e022292013-02-06 16:42:1772 // Returns the manual behavior for the given URL.
73 ManualBehavior GetManualBehaviorForURL(const GURL& url);
[email protected]0850e842013-01-19 03:44:3174
[email protected]5e022292013-02-06 16:42:1775 // Sets the manual behavior for the given URL.
76 void SetManualBehaviorForURLs(const std::vector<GURL>& url,
77 ManualBehavior behavior);
[email protected]0850e842013-01-19 03:44:3178
79 void SetElevatedForTesting(bool is_elevated);
80
81 // Initializes this object. This method does nothing if the profile is not
[email protected]eb7dbdc2013-01-24 20:23:5582 // managed. This method only needs to be called if the profile is set to be
83 // managed after the ManagedUserService has been created (which happens when
84 // creating a new profile).
[email protected]0850e842013-01-19 03:44:3185 void Init();
86
87 // ExtensionManagementPolicy::Provider implementation:
88 virtual std::string GetDebugPolicyProviderName() const OVERRIDE;
89 virtual bool UserMayLoad(const extensions::Extension* extension,
90 string16* error) const OVERRIDE;
91 virtual bool UserMayModifySettings(const extensions::Extension* extension,
92 string16* error) const OVERRIDE;
93
94 // content::NotificationObserver implementation:
95 virtual void Observe(int type,
96 const content::NotificationSource& source,
97 const content::NotificationDetails& details) OVERRIDE;
98
99 private:
100 friend class ManagedUserServiceExtensionTest;
101
102 // A bridge from ManagedMode (which lives on the UI thread) to the
103 // ManagedModeURLFilters, one of which lives on the IO thread. This class
104 // mediates access to them and makes sure they are kept in sync.
105 class URLFilterContext {
106 public:
107 URLFilterContext();
108 ~URLFilterContext();
109
110 ManagedModeURLFilter* ui_url_filter() const;
111 ManagedModeURLFilter* io_url_filter() const;
112
113 void SetDefaultFilteringBehavior(
114 ManagedModeURLFilter::FilteringBehavior behavior);
115 void LoadWhitelists(ScopedVector<ManagedModeSiteList> site_lists);
[email protected]5e022292013-02-06 16:42:17116 void SetManualHosts(scoped_ptr<std::map<std::string, bool> > host_map);
117 void SetManualURLs(scoped_ptr<std::map<GURL, bool> > url_map);
[email protected]0850e842013-01-19 03:44:31118
119 private:
120 // ManagedModeURLFilter is refcounted because the IO thread filter is used
121 // both by ProfileImplIOData and OffTheRecordProfileIOData (to filter
122 // network requests), so they both keep a reference to it.
123 // Clients should not keep references to the UI thread filter, however
124 // (the filter will live as long as the profile lives, and afterwards it
125 // should not be used anymore either).
126 scoped_refptr<ManagedModeURLFilter> ui_url_filter_;
127 scoped_refptr<ManagedModeURLFilter> io_url_filter_;
128
129 DISALLOW_COPY_AND_ASSIGN(URLFilterContext);
130 };
131
132 // Internal implementation for ExtensionManagementPolicy::Delegate methods.
133 // If |error| is not NULL, it will be filled with an error message if the
134 // requested extension action (install, modify status, etc.) is not permitted.
135 bool ExtensionManagementPolicyImpl(string16* error) const;
136
137 // Returns a list of all installed and enabled site lists in the current
138 // managed profile.
139 ScopedVector<ManagedModeSiteList> GetActiveSiteLists();
140
141 void OnDefaultFilteringBehaviorChanged();
142
143 void UpdateSiteLists();
144
[email protected]5e022292013-02-06 16:42:17145 // Updates the manual overrides for hosts in the URL filters when the
146 // corresponding preference is changed.
147 void UpdateManualHosts();
[email protected]0850e842013-01-19 03:44:31148
[email protected]5e022292013-02-06 16:42:17149 // Updates the manual overrides for URLs in the URL filters when the
150 // corresponding preference is changed.
151 void UpdateManualURLs();
[email protected]0850e842013-01-19 03:44:31152
153 // Owns us via the ProfileKeyedService mechanism.
154 Profile* profile_;
155
156 // If ManagedUserService is in an elevated state, a custodian user has
157 // authorized making changes (to install additional content packs, for
158 // example).
159 bool is_elevated_;
160
161 content::NotificationRegistrar registrar_;
162 PrefChangeRegistrar pref_change_registrar_;
163
164 URLFilterContext url_filter_context_;
165};
166
167#endif // CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_SERVICE_H_