Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: chrome/browser/content_settings/content_settings_policy_provider.cc

Issue 6542048: Add content_settings::PolicyProvider and a set of new policies to managed content settings. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: " Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/content_settings/content_settings_policy_provider.h" 5 #include "chrome/browser/content_settings/content_settings_policy_provider.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector>
8 9
9 #include "base/command_line.h" 10 #include "base/command_line.h"
10 #include "chrome/browser/browser_thread.h" 11 #include "chrome/browser/browser_thread.h"
11 #include "chrome/browser/content_settings/content_settings_details.h" 12 #include "chrome/browser/content_settings/content_settings_details.h"
12 #include "chrome/browser/content_settings/content_settings_pattern.h" 13 #include "chrome/browser/content_settings/content_settings_pattern.h"
13 #include "chrome/browser/prefs/pref_service.h" 14 #include "chrome/browser/prefs/pref_service.h"
14 #include "chrome/browser/prefs/scoped_pref_update.h" 15 #include "chrome/browser/prefs/scoped_pref_update.h"
15 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/common/notification_details.h" 17 #include "chrome/common/notification_details.h"
17 #include "chrome/common/notification_service.h" 18 #include "chrome/common/notification_service.h"
18 #include "chrome/common/notification_source.h" 19 #include "chrome/common/notification_source.h"
19 #include "chrome/common/pref_names.h" 20 #include "chrome/common/pref_names.h"
20 21
22 #include "webkit/plugins/npapi/plugin_group.h"
23 #include "webkit/plugins/npapi/plugin_list.h"
24
21 namespace { 25 namespace {
22 26
23 // Base pref path of the prefs that contain the managed default content 27 // Base pref path of the prefs that contain the managed default content
24 // settings values. 28 // settings values.
25 const std::string kManagedSettings = 29 const std::string kManagedSettings =
26 "profile.managed_default_content_settings"; 30 "profile.managed_default_content_settings";
27 31
28 // The preferences used to manage ContentSettingsTypes. 32 // The preferences used to manage ContentSettingsTypes.
29 const char* kPrefToManageType[CONTENT_SETTINGS_NUM_TYPES] = { 33 const char* kPrefToManageType[CONTENT_SETTINGS_NUM_TYPES] = {
30 prefs::kManagedDefaultCookiesSetting, 34 prefs::kManagedDefaultCookiesSetting,
31 prefs::kManagedDefaultImagesSetting, 35 prefs::kManagedDefaultImagesSetting,
32 prefs::kManagedDefaultJavaScriptSetting, 36 prefs::kManagedDefaultJavaScriptSetting,
33 prefs::kManagedDefaultPluginsSetting, 37 prefs::kManagedDefaultPluginsSetting,
34 prefs::kManagedDefaultPopupsSetting, 38 prefs::kManagedDefaultPopupsSetting,
35 NULL, // Not used for Geolocation 39 NULL, // Not used for Geolocation
36 NULL, // Not used for Notifications 40 NULL, // Not used for Notifications
37 }; 41 };
38 42
43 struct PrefsForManagedContentSettingsMapEntry {
44 const char* pref_name;
45 ContentSettingsType content_type;
46 ContentSetting setting;
47 };
48
49 const PrefsForManagedContentSettingsMapEntry
50 kPrefsForManagedContentSettingsMap[] = {
51 {
52 prefs::kManagedCookiesAllowedForUrls,
53 CONTENT_SETTINGS_TYPE_COOKIES,
54 CONTENT_SETTING_ALLOW
55 }, {
56 prefs::kManagedCookiesBlockedForUrls,
57 CONTENT_SETTINGS_TYPE_COOKIES,
58 CONTENT_SETTING_BLOCK
59 }, {
60 prefs::kManagedCookiesSessionOnlyForUrls,
61 CONTENT_SETTINGS_TYPE_COOKIES,
62 CONTENT_SETTING_SESSION_ONLY
63 }, {
64 prefs::kManagedImagesAllowedForUrls,
65 CONTENT_SETTINGS_TYPE_IMAGES,
66 CONTENT_SETTING_ALLOW
67 }, {
68 prefs::kManagedImagesBlockedForUrls,
69 CONTENT_SETTINGS_TYPE_IMAGES,
70 CONTENT_SETTING_BLOCK
71 }, {
72 prefs::kManagedJavaScriptAllowedForUrls,
73 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
74 CONTENT_SETTING_ALLOW
75 }, {
76 prefs::kManagedJavaScriptBlockedForUrls,
77 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
78 CONTENT_SETTING_BLOCK
79 }, {
80 prefs::kManagedPluginsAllowedForUrls,
81 CONTENT_SETTINGS_TYPE_PLUGINS,
82 CONTENT_SETTING_ALLOW
83 }, {
84 prefs::kManagedPluginsBlockedForUrls,
85 CONTENT_SETTINGS_TYPE_PLUGINS,
86 CONTENT_SETTING_BLOCK
87 }, {
88 prefs::kManagedPopupsAllowedForUrls,
89 CONTENT_SETTINGS_TYPE_POPUPS,
90 CONTENT_SETTING_ALLOW
91 }, {
92 prefs::kManagedPopupsBlockedForUrls,
93 CONTENT_SETTINGS_TYPE_POPUPS,
94 CONTENT_SETTING_BLOCK
95 }
96 };
97
98 const std::string NO_IDENTIFIER = "";
99
39 } // namespace 100 } // namespace
40 101
41 namespace content_settings { 102 namespace content_settings {
42 103
43 PolicyDefaultProvider::PolicyDefaultProvider(Profile* profile) 104 PolicyDefaultProvider::PolicyDefaultProvider(Profile* profile)
44 : profile_(profile), 105 : profile_(profile),
45 is_off_the_record_(profile_->IsOffTheRecord()) { 106 is_off_the_record_(profile_->IsOffTheRecord()) {
46 PrefService* prefs = profile->GetPrefs(); 107 PrefService* prefs = profile->GetPrefs();
47 108
48 // Read global defaults. 109 // Read global defaults.
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 return; 210 return;
150 NotificationService::current()->Notify( 211 NotificationService::current()->Notify(
151 NotificationType::CONTENT_SETTINGS_CHANGED, 212 NotificationType::CONTENT_SETTINGS_CHANGED,
152 Source<HostContentSettingsMap>(profile_->GetHostContentSettingsMap()), 213 Source<HostContentSettingsMap>(profile_->GetHostContentSettingsMap()),
153 Details<const ContentSettingsDetails>(&details)); 214 Details<const ContentSettingsDetails>(&details));
154 } 215 }
155 216
156 void PolicyDefaultProvider::ReadManagedDefaultSettings() { 217 void PolicyDefaultProvider::ReadManagedDefaultSettings() {
157 for (size_t type = 0; type < arraysize(kPrefToManageType); ++type) { 218 for (size_t type = 0; type < arraysize(kPrefToManageType); ++type) {
158 if (kPrefToManageType[type] == NULL) { 219 if (kPrefToManageType[type] == NULL) {
159 // TODO(markusheintz): Handle Geolocation and notification separately.
160 continue; 220 continue;
161 } 221 }
162 UpdateManagedDefaultSetting(ContentSettingsType(type)); 222 UpdateManagedDefaultSetting(ContentSettingsType(type));
163 } 223 }
164 } 224 }
165 225
166 void PolicyDefaultProvider::UpdateManagedDefaultSetting( 226 void PolicyDefaultProvider::UpdateManagedDefaultSetting(
167 ContentSettingsType type) { 227 ContentSettingsType type) {
168 // If a pref to manage a default-content-setting was not set (NOTICE: 228 // If a pref to manage a default-content-setting was not set (NOTICE:
169 // "HasPrefPath" returns false if no value was set for a registered pref) then 229 // "HasPrefPath" returns false if no value was set for a registered pref) then
(...skipping 18 matching lines...) Expand all
188 prefs->RegisterIntegerPref(prefs::kManagedDefaultImagesSetting, 248 prefs->RegisterIntegerPref(prefs::kManagedDefaultImagesSetting,
189 CONTENT_SETTING_DEFAULT); 249 CONTENT_SETTING_DEFAULT);
190 prefs->RegisterIntegerPref(prefs::kManagedDefaultJavaScriptSetting, 250 prefs->RegisterIntegerPref(prefs::kManagedDefaultJavaScriptSetting,
191 CONTENT_SETTING_DEFAULT); 251 CONTENT_SETTING_DEFAULT);
192 prefs->RegisterIntegerPref(prefs::kManagedDefaultPluginsSetting, 252 prefs->RegisterIntegerPref(prefs::kManagedDefaultPluginsSetting,
193 CONTENT_SETTING_DEFAULT); 253 CONTENT_SETTING_DEFAULT);
194 prefs->RegisterIntegerPref(prefs::kManagedDefaultPopupsSetting, 254 prefs->RegisterIntegerPref(prefs::kManagedDefaultPopupsSetting,
195 CONTENT_SETTING_DEFAULT); 255 CONTENT_SETTING_DEFAULT);
196 } 256 }
197 257
258 // ////////////////////////////////////////////////////////////////////////////
259 // PolicyProvider
260
261 // static
262 void PolicyProvider::RegisterUserPrefs(PrefService* prefs) {
263 prefs->RegisterListPref(prefs::kManagedCookiesAllowedForUrls);
264 prefs->RegisterListPref(prefs::kManagedCookiesBlockedForUrls);
265 prefs->RegisterListPref(prefs::kManagedCookiesSessionOnlyForUrls);
266 prefs->RegisterListPref(prefs::kManagedImagesAllowedForUrls);
267 prefs->RegisterListPref(prefs::kManagedImagesBlockedForUrls);
268 prefs->RegisterListPref(prefs::kManagedJavaScriptAllowedForUrls);
269 prefs->RegisterListPref(prefs::kManagedJavaScriptBlockedForUrls);
270 prefs->RegisterListPref(prefs::kManagedPluginsAllowedForUrls);
271 prefs->RegisterListPref(prefs::kManagedPluginsBlockedForUrls);
272 prefs->RegisterListPref(prefs::kManagedPopupsAllowedForUrls);
273 prefs->RegisterListPref(prefs::kManagedPopupsBlockedForUrls);
274 }
275
276 PolicyProvider::PolicyProvider(Profile* profile)
277 : BaseProvider(profile->IsOffTheRecord()),
278 profile_(profile) {
279 Init();
280 }
281
282 PolicyProvider::~PolicyProvider() {
283 UnregisterObservers();
284 }
285
286 void PolicyProvider::ReadManagedContentSettingsTypes(
287 ContentSettingsType content_type) {
288 PrefService* prefs = profile_->GetPrefs();
289 if (kPrefToManageType[content_type] == NULL) {
290 content_type_is_managed_[content_type] = false;
291 } else {
292 content_type_is_managed_[content_type] =
293 prefs->IsManagedPreference(kPrefToManageType[content_type]);
294 }
295 }
296
297 void PolicyProvider::Init() {
298 PrefService* prefs = profile_->GetPrefs();
299
300 ReadManagedContentSettings(false);
301 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i)
302 ReadManagedContentSettingsTypes(ContentSettingsType(i));
303
304 pref_change_registrar_.Init(prefs);
305 pref_change_registrar_.Add(prefs::kManagedCookiesBlockedForUrls, this);
306 pref_change_registrar_.Add(prefs::kManagedCookiesAllowedForUrls, this);
307 pref_change_registrar_.Add(prefs::kManagedCookiesSessionOnlyForUrls, this);
308 pref_change_registrar_.Add(prefs::kManagedImagesBlockedForUrls, this);
309 pref_change_registrar_.Add(prefs::kManagedImagesAllowedForUrls, this);
310 pref_change_registrar_.Add(prefs::kManagedJavaScriptBlockedForUrls, this);
311 pref_change_registrar_.Add(prefs::kManagedJavaScriptAllowedForUrls, this);
312 pref_change_registrar_.Add(prefs::kManagedPluginsBlockedForUrls, this);
313 pref_change_registrar_.Add(prefs::kManagedPluginsAllowedForUrls, this);
314 pref_change_registrar_.Add(prefs::kManagedPopupsBlockedForUrls, this);
315 pref_change_registrar_.Add(prefs::kManagedPopupsAllowedForUrls, this);
316
317 pref_change_registrar_.Add(prefs::kManagedDefaultCookiesSetting, this);
318 pref_change_registrar_.Add(prefs::kManagedDefaultImagesSetting, this);
319 pref_change_registrar_.Add(prefs::kManagedDefaultJavaScriptSetting, this);
320 pref_change_registrar_.Add(prefs::kManagedDefaultPluginsSetting, this);
321 pref_change_registrar_.Add(prefs::kManagedDefaultPopupsSetting, this);
322
323 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED,
324 Source<Profile>(profile_));
325 }
326
327 bool PolicyProvider::ContentSettingsTypeIsManaged(
328 ContentSettingsType content_type) {
329 return content_type_is_managed_[content_type];
330 }
331
332 void PolicyProvider::GetContentSettingsFromPreferences(
333 PrefService* prefs,
334 ContentSettingsRules* rules) {
335 for (size_t i = 0; i < arraysize(kPrefsForManagedContentSettingsMap); ++i) {
336 const char* pref_name = kPrefsForManagedContentSettingsMap[i].pref_name;
337 // Skip unset policies.
338 if (!prefs->HasPrefPath(pref_name))
339 continue;
340
341 const PrefService::Preference* pref = prefs->FindPreference(pref_name);
342 DCHECK(pref->IsManaged());
343 DCHECK_EQ(Value::TYPE_LIST, pref->GetType());
344
345 const ListValue* pattern_str_list =
346 static_cast<const ListValue*>(pref->GetValue());
347 for (size_t j = 0; j < pattern_str_list->GetSize(); ++j) {
348 std::string original_pattern_str;
349 pattern_str_list->GetString(j, &original_pattern_str);
350 ContentSettingsPattern pattern(original_pattern_str);
351 if (!pattern.IsValid()) {
352 // Ignore invalid patterns
353 continue;
354 LOG(WARNING) << "Ignoring invalid content settings pattern: "
355 << pattern.AsString();
356 }
357 rules->push_back(MakeTuple(
358 pattern,
359 pattern,
360 kPrefsForManagedContentSettingsMap[i].content_type,
361 NO_IDENTIFIER,
362 kPrefsForManagedContentSettingsMap[i].setting));
363 }
364 }
365 }
366
367 void PolicyProvider::ReadManagedContentSettings(bool overwrite) {
368 ContentSettingsRules rules;
369 PrefService* prefs = profile_->GetPrefs();
370 GetContentSettingsFromPreferences(prefs, &rules);
371 {
372 base::AutoLock auto_lock(lock());
373 HostContentSettings* content_settings_map = host_content_settings();
374 if (overwrite)
375 content_settings_map->clear();
376 for (ContentSettingsRules::iterator rule = rules.begin();
377 rule != rules.end();
378 ++rule) {
379 DispatchToMethod(this, &PolicyProvider::UpdateContentSettingsMap, *rule);
380 }
381 }
382 }
383
384 // Since the PolicyProvider is a read only content settings provider, all
385 // methodes of the ProviderInterface that set or delete any settings do nothing.
386 void PolicyProvider::SetContentSetting(
387 const ContentSettingsPattern& requesting_pattern,
388 const ContentSettingsPattern& embedding_pattern,
389 ContentSettingsType content_type,
390 const ResourceIdentifier& resource_identifier,
391 ContentSetting content_setting) {
392 }
393
394 ContentSetting PolicyProvider::GetContentSetting(
395 const GURL& requesting_url,
Bernhard Bauer 2011/02/25 16:08:01 Nit: this should be indented only four spaces.
markusheintz_ 2011/02/28 09:08:44 Done.
396 const GURL& embedding_url,
397 ContentSettingsType content_type,
398 const ResourceIdentifier& resource_identifier) const {
399 return BaseProvider::GetContentSetting(
400 requesting_url,
401 embedding_url,
402 content_type,
403 NO_IDENTIFIER);
404 }
405
406 void PolicyProvider::ClearAllContentSettingsRules(
407 ContentSettingsType content_type) {
408 }
409
410 void PolicyProvider::ResetToDefaults() {
411 }
412
413 void PolicyProvider::UnregisterObservers() {
414 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
415 if (!profile_)
416 return;
417 pref_change_registrar_.RemoveAll();
418 notification_registrar_.Remove(this, NotificationType::PROFILE_DESTROYED,
419 Source<Profile>(profile_));
420 profile_ = NULL;
421 }
422
423 void PolicyProvider::NotifyObservers(
424 const ContentSettingsDetails& details) {
425 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
426 if (profile_ == NULL)
427 return;
428 NotificationService::current()->Notify(
429 NotificationType::CONTENT_SETTINGS_CHANGED,
430 Source<HostContentSettingsMap>(profile_->GetHostContentSettingsMap()),
431 Details<const ContentSettingsDetails>(&details));
432 }
433
434 void PolicyProvider::Observe(NotificationType type,
435 const NotificationSource& source,
436 const NotificationDetails& details) {
437 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
438
439 if (type == NotificationType::PREF_CHANGED) {
440 DCHECK_EQ(profile_->GetPrefs(), Source<PrefService>(source).ptr());
441 std::string* name = Details<std::string>(details).ptr();
442 if (*name == prefs::kManagedCookiesAllowedForUrls ||
443 *name == prefs::kManagedCookiesBlockedForUrls ||
444 *name == prefs::kManagedCookiesSessionOnlyForUrls ||
445 *name == prefs::kManagedImagesAllowedForUrls ||
446 *name == prefs::kManagedImagesBlockedForUrls ||
447 *name == prefs::kManagedJavaScriptAllowedForUrls ||
448 *name == prefs::kManagedJavaScriptBlockedForUrls ||
449 *name == prefs::kManagedPluginsAllowedForUrls ||
450 *name == prefs::kManagedPluginsBlockedForUrls ||
451 *name == prefs::kManagedPopupsAllowedForUrls ||
452 *name == prefs::kManagedPopupsBlockedForUrls) {
453 ReadManagedContentSettings(true);
454 NotifyObservers(ContentSettingsDetails(
455 ContentSettingsPattern(), CONTENT_SETTINGS_TYPE_DEFAULT, ""));
456 // We do not want to sent a notification when managed default content
457 // settings change. The DefaultProvider will take care of that. WE are
Bernhard Bauer 2011/02/25 16:08:01 Nit: "We"
markusheintz_ 2011/02/28 09:08:44 Done.
458 // only a passive observer.
459 // TODO(markusheintz): NOTICE: This is still work in progress and part of a
460 // larger refactoring. The code will change and be much cleaner and
461 // clearer in the end.
462 } else if (*name == prefs::kManagedDefaultCookiesSetting) {
463 ReadManagedContentSettingsTypes(CONTENT_SETTINGS_TYPE_COOKIES);
464 } else if (*name == prefs::kManagedDefaultImagesSetting) {
465 ReadManagedContentSettingsTypes(CONTENT_SETTINGS_TYPE_IMAGES);
466 } else if (*name == prefs::kManagedDefaultJavaScriptSetting) {
467 ReadManagedContentSettingsTypes(CONTENT_SETTINGS_TYPE_JAVASCRIPT);
468 } else if (*name == prefs::kManagedDefaultPluginsSetting) {
469 ReadManagedContentSettingsTypes(CONTENT_SETTINGS_TYPE_PLUGINS);
470 } else if (*name == prefs::kManagedDefaultPopupsSetting) {
471 ReadManagedContentSettingsTypes(CONTENT_SETTINGS_TYPE_POPUPS);
472 }
473 } else if (type == NotificationType::PROFILE_DESTROYED) {
474 DCHECK_EQ(profile_, Source<Profile>(source).ptr());
475 UnregisterObservers();
476 } else {
477 NOTREACHED() << "Unexpected notification";
478 }
479 }
480
198 } // namespace content_settings 481 } // namespace content_settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698