OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
Pam (message me for reviews)
2013/01/14 14:12:42
2013
Bernhard Bauer
2013/01/15 14:24:44
Done.
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/managed_mode/managed_user_service.h" | |
6 | |
7 #include "base/memory/ref_counted.h" | |
8 #include "base/sequenced_task_runner.h" | |
9 #include "chrome/browser/extensions/extension_service.h" | |
10 #include "chrome/browser/extensions/extension_system.h" | |
11 #include "chrome/browser/managed_mode/managed_mode_site_list.h" | |
12 #include "chrome/browser/prefs/pref_service.h" | |
13 #include "chrome/browser/prefs/scoped_user_pref_update.h" | |
14 #include "chrome/browser/profiles/profile.h" | |
15 #include "chrome/common/chrome_notification_types.h" | |
16 #include "chrome/common/extensions/extension_set.h" | |
17 #include "chrome/common/pref_names.h" | |
18 #include "content/public/browser/browser_thread.h" | |
19 #include "grit/generated_resources.h" | |
20 #include "ui/base/l10n/l10n_util.h" | |
21 | |
22 using content::BrowserThread; | |
23 | |
24 ManagedUserService::URLFilterContext::URLFilterContext() | |
25 : ui_url_filter_(new ManagedModeURLFilter), | |
26 io_url_filter_(new ManagedModeURLFilter) {} | |
27 ManagedUserService::URLFilterContext::~URLFilterContext() {} | |
28 | |
29 ManagedModeURLFilter* | |
30 ManagedUserService::URLFilterContext::ui_url_filter() const { | |
31 return ui_url_filter_.get(); | |
32 } | |
33 | |
34 ManagedModeURLFilter* | |
35 ManagedUserService::URLFilterContext::io_url_filter() const { | |
36 return io_url_filter_.get(); | |
37 } | |
38 | |
39 void ManagedUserService::URLFilterContext::SetDefaultFilteringBehavior( | |
40 ManagedModeURLFilter::FilteringBehavior behavior) { | |
41 ui_url_filter_->SetDefaultFilteringBehavior(behavior); | |
Pam (message me for reviews)
2013/01/14 14:12:42
Is it worth making sure that we're on the UI threa
Bernhard Bauer
2013/01/15 14:24:44
The filter will check that itself.
| |
42 BrowserThread::PostTask( | |
43 BrowserThread::IO, | |
44 FROM_HERE, | |
45 base::Bind(&ManagedModeURLFilter::SetDefaultFilteringBehavior, | |
46 io_url_filter_.get(), | |
47 behavior)); | |
48 } | |
49 | |
50 void ManagedUserService::URLFilterContext::LoadWhitelists( | |
51 ScopedVector<ManagedModeSiteList> site_lists) { | |
52 // ManagedModeURLFilter::LoadWhitelists takes ownership of |site_lists|, | |
53 // so we make an additional copy of it. | |
54 /// TODO(bauerb): This is kinda ugly. | |
55 ScopedVector<ManagedModeSiteList> site_lists_copy; | |
Pam (message me for reviews)
2013/01/14 14:12:42
You make this copy, then don't do anything with it
Bernhard Bauer
2013/01/15 14:24:44
Oops. Fixed.
| |
56 for (ScopedVector<ManagedModeSiteList>::iterator it = site_lists.begin(); | |
57 it != site_lists.end(); ++it) { | |
58 site_lists_copy.push_back((*it)->Clone()); | |
59 } | |
60 ui_url_filter_->LoadWhitelists(site_lists.Pass(), | |
61 base::Bind(&base::DoNothing)); | |
62 BrowserThread::PostTask( | |
63 BrowserThread::IO, | |
64 FROM_HERE, | |
65 base::Bind(&ManagedModeURLFilter::LoadWhitelists, | |
66 io_url_filter_, | |
67 base::Passed(&site_lists), | |
68 base::Bind(&base::DoNothing))); | |
69 } | |
70 | |
71 void ManagedUserService::URLFilterContext::SetManualLists( | |
72 scoped_ptr<ListValue> whitelist, | |
73 scoped_ptr<ListValue> blacklist) { | |
74 ui_url_filter_->SetManualLists(whitelist.get(), blacklist.get()); | |
75 BrowserThread::PostTask( | |
76 BrowserThread::IO, | |
77 FROM_HERE, | |
78 base::Bind(&ManagedModeURLFilter::SetManualLists, | |
79 io_url_filter_, | |
80 base::Owned(whitelist.release()), | |
81 base::Owned(blacklist.release()))); | |
82 } | |
83 | |
84 void ManagedUserService::URLFilterContext::AddURLPatternToManualList( | |
85 const bool is_whitelist, | |
86 const std::string& url) { | |
87 ui_url_filter_->AddURLPatternToManualList(is_whitelist, url); | |
88 BrowserThread::PostTask( | |
89 BrowserThread::IO, | |
90 FROM_HERE, | |
91 base::Bind(&ManagedModeURLFilter::AddURLPatternToManualList, | |
92 io_url_filter_, | |
93 is_whitelist, | |
94 url)); | |
95 } | |
96 | |
97 ManagedUserService::ManagedUserService(Profile* profile) : profile_(profile) { | |
98 if (!ProfileIsManaged()) | |
99 return; | |
100 | |
101 extensions::ExtensionSystem* extension_system = | |
102 extensions::ExtensionSystem::Get(profile); | |
103 extensions::ManagementPolicy* management_policy = | |
104 extension_system->management_policy(); | |
105 if (management_policy) | |
106 extension_system->management_policy()->RegisterProvider(this); | |
107 | |
108 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | |
109 content::Source<Profile>(profile_)); | |
110 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | |
111 content::Source<Profile>(profile_)); | |
112 pref_change_registrar_.Init(profile_->GetPrefs()); | |
113 pref_change_registrar_.Add( | |
114 prefs::kDefaultManagedModeFilteringBehavior, | |
115 base::Bind( | |
116 &ManagedUserService::OnDefaultFilteringBehaviorChanged, | |
117 base::Unretained(this))); | |
118 } | |
119 | |
120 ManagedUserService::~ManagedUserService() { | |
121 } | |
122 | |
123 bool ManagedUserService::ProfileIsManaged() const { | |
124 return profile_->GetPrefs()->GetBoolean(prefs::kProfileIsManaged); | |
Pam (message me for reviews)
2013/01/14 14:12:42
What sets kProfileIsManaged? Will it be possible t
Bernhard Bauer
2013/01/15 14:24:44
It's set by the profile creation mechanism when th
Pam (message me for reviews)
2013/01/15 14:47:53
I'm not convinced that kProfileIsManaged is necess
Bernhard Bauer
2013/01/15 14:59:55
* Can install extensions/content packs only after
| |
125 } | |
126 | |
127 // static | |
128 void ManagedUserService::RegisterUserPrefs(PrefServiceSyncable* prefs) { | |
129 prefs->RegisterIntegerPref(prefs::kDefaultManagedModeFilteringBehavior, | |
130 2, | |
Pam (message me for reviews)
2013/01/14 14:12:42
Ew. Please at least attach a comment saying what t
Bernhard Bauer
2013/01/15 14:24:44
Done.
| |
131 PrefServiceSyncable::UNSYNCABLE_PREF); | |
132 } | |
133 | |
134 scoped_refptr<const ManagedModeURLFilter> | |
135 ManagedUserService::GetURLFilterForIOThread() { | |
136 return url_filter_context_.io_url_filter(); | |
137 } | |
138 | |
139 const ManagedModeURLFilter* ManagedUserService::GetURLFilterForUIThread() { | |
140 return url_filter_context_.ui_url_filter(); | |
141 } | |
142 | |
143 // Items not on any list must return -1 (CATEGORY_NOT_ON_LIST in history.js). | |
144 // Items on a list, but with no category, must return 0 (CATEGORY_OTHER). | |
145 #define CATEGORY_NOT_ON_LIST -1; | |
146 #define CATEGORY_OTHER 0; | |
147 | |
148 int ManagedUserService::GetCategory(const GURL& url) { | |
149 std::vector<ManagedModeSiteList::Site*> sites; | |
150 GetURLFilterForUIThread()->GetSites(url, &sites); | |
151 if (sites.empty()) | |
152 return CATEGORY_NOT_ON_LIST; | |
153 | |
154 return (*sites.begin())->category_id; | |
155 } | |
156 | |
157 // static | |
158 void ManagedUserService::GetCategoryNames(CategoryList* list) { | |
159 ManagedModeSiteList::GetCategoryNames(list); | |
160 }; | |
161 | |
162 void ManagedUserService::AddToManualList(bool is_whitelist, | |
163 const base::ListValue& list) { | |
164 | |
Pam (message me for reviews)
2013/01/14 14:12:42
Extra blank line
Bernhard Bauer
2013/01/15 14:24:44
Removed.
| |
165 ListPrefUpdate pref_update(profile_->GetPrefs(), | |
166 is_whitelist ? prefs::kManagedModeWhitelist : | |
167 prefs::kManagedModeBlacklist); | |
168 ListValue* pref_list = pref_update.Get(); | |
169 | |
170 for (size_t i = 0; i < list.GetSize(); ++i) { | |
171 std::string url_pattern; | |
172 list.GetString(i, &url_pattern); | |
173 | |
174 if (!IsInManualList(is_whitelist, url_pattern)) { | |
175 pref_list->AppendString(url_pattern); | |
176 AddURLPatternToManualList(is_whitelist, url_pattern); | |
177 } | |
178 } | |
179 } | |
180 | |
181 void ManagedUserService::RemoveFromManualList(bool is_whitelist, | |
182 const base::ListValue& list) { | |
183 ListPrefUpdate pref_update(profile_->GetPrefs(), | |
184 is_whitelist ? prefs::kManagedModeWhitelist : | |
185 prefs::kManagedModeBlacklist); | |
186 ListValue* pref_list = pref_update.Get(); | |
187 | |
188 for (size_t i = 0; i < list.GetSize(); ++i) { | |
189 std::string pattern; | |
190 size_t out_index; | |
191 list.GetString(i, &pattern); | |
192 StringValue value_to_remove(pattern); | |
193 | |
194 pref_list->Remove(value_to_remove, &out_index); | |
195 } | |
196 } | |
197 | |
198 bool ManagedUserService::IsInManualList(bool is_whitelist, | |
199 const std::string& url_pattern) { | |
200 StringValue pattern(url_pattern); | |
201 const ListValue* list = profile_->GetPrefs()->GetList( | |
202 is_whitelist ? prefs::kManagedModeWhitelist : | |
203 prefs::kManagedModeBlacklist); | |
204 return list->Find(pattern) != list->end(); | |
205 } | |
206 | |
207 // static | |
208 scoped_ptr<base::ListValue> ManagedUserService::GetBlacklist() { | |
209 return make_scoped_ptr( | |
210 profile_->GetPrefs()->GetList(prefs::kManagedModeBlacklist)->DeepCopy()); | |
211 } | |
212 | |
213 std::string ManagedUserService::GetDebugPolicyProviderName() const { | |
214 // Save the string space in official builds. | |
215 #ifdef NDEBUG | |
216 NOTREACHED(); | |
217 return std::string(); | |
218 #else | |
219 return "Managed Mode"; | |
Pam (message me for reviews)
2013/01/14 14:12:42
"Managed User Service"
Bernhard Bauer
2013/01/15 14:24:44
Done.
| |
220 #endif | |
221 } | |
222 | |
223 bool ManagedUserService::UserMayLoad(const extensions::Extension* extension, | |
224 string16* error) const { | |
225 string16 tmp_error; | |
226 if (ExtensionManagementPolicyImpl(&tmp_error)) | |
227 return true; | |
228 | |
229 // If the extension is already loaded, we allow it, otherwise we'd unload | |
230 // all existing extensions. | |
231 ExtensionService* extension_service = | |
232 extensions::ExtensionSystem::Get(profile_)->extension_service(); | |
233 | |
234 // |extension_service| can be NULL in a unit test. | |
235 if (extension_service && | |
236 extension_service->GetInstalledExtension(extension->id())) | |
237 return true; | |
238 | |
239 if (error) | |
240 *error = tmp_error; | |
241 return false; | |
242 } | |
243 | |
244 bool ManagedUserService::UserMayModifySettings( | |
245 const extensions::Extension* extension, | |
246 string16* error) const { | |
247 return ExtensionManagementPolicyImpl(error); | |
248 } | |
249 | |
250 void ManagedUserService::Observe(int type, | |
251 const content::NotificationSource& source, | |
252 const content::NotificationDetails& details) { | |
253 switch (type) { | |
254 case chrome::NOTIFICATION_EXTENSION_LOADED: | |
255 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { | |
256 if (!profile_) | |
257 break; | |
258 | |
259 const extensions::Extension* extension = | |
260 content::Details<const extensions::Extension>(details).ptr(); | |
261 if (!extension->GetContentPackSiteList().empty()) | |
262 UpdateSiteLists(); | |
263 | |
264 break; | |
265 } | |
266 default: | |
267 NOTREACHED(); | |
268 } | |
269 } | |
270 | |
271 bool ManagedUserService::ExtensionManagementPolicyImpl(string16* error) const { | |
272 if (!ProfileIsManaged()) | |
273 return true; | |
274 | |
275 if (error) | |
276 *error = l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOCKED_MANAGED_MODE); | |
277 return false; | |
278 } | |
279 | |
280 ScopedVector<ManagedModeSiteList> ManagedUserService::GetActiveSiteLists() { | |
281 ExtensionService* extension_service = | |
282 extensions::ExtensionSystem::Get(profile_)->extension_service(); | |
283 const ExtensionSet* extensions = extension_service->extensions(); | |
284 ScopedVector<ManagedModeSiteList> site_lists; | |
285 for (ExtensionSet::const_iterator it = extensions->begin(); | |
286 it != extensions->end(); ++it) { | |
287 const extensions::Extension* extension = *it; | |
288 if (!extension_service->IsExtensionEnabled(extension->id())) | |
289 continue; | |
290 | |
291 ExtensionResource site_list = extension->GetContentPackSiteList(); | |
292 if (!site_list.empty()) | |
293 site_lists.push_back(new ManagedModeSiteList(extension->id(), site_list)); | |
294 } | |
295 | |
296 return site_lists.Pass(); | |
297 } | |
298 | |
299 void ManagedUserService::OnDefaultFilteringBehaviorChanged() { | |
300 DCHECK(ProfileIsManaged()); | |
301 | |
302 int behavior_value = profile_->GetPrefs()->GetInteger( | |
303 prefs::kDefaultManagedModeFilteringBehavior); | |
304 ManagedModeURLFilter::FilteringBehavior behavior = | |
305 ManagedModeURLFilter::BehaviorFromInt(behavior_value); | |
306 url_filter_context_.SetDefaultFilteringBehavior(behavior); | |
307 } | |
308 | |
309 void ManagedUserService::UpdateSiteLists() { | |
310 url_filter_context_.LoadWhitelists(GetActiveSiteLists()); | |
311 } | |
312 | |
313 void ManagedUserService::UpdateManualLists() { | |
314 url_filter_context_.SetManualLists(GetWhitelist(), GetBlacklist()); | |
315 } | |
316 | |
317 scoped_ptr<base::ListValue> ManagedUserService::GetWhitelist() { | |
318 return make_scoped_ptr( | |
319 profile_->GetPrefs()->GetList(prefs::kManagedModeWhitelist)->DeepCopy()); | |
320 } | |
321 | |
322 void ManagedUserService::AddURLPatternToManualList( | |
323 bool is_whitelist, | |
324 const std::string& url_pattern) { | |
325 url_filter_context_.AddURLPatternToManualList(true, url_pattern); | |
326 } | |
OLD | NEW |