| OLD | NEW |
| 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::kManagedCookiesSessionOnlyForUrls, |
| 57 CONTENT_SETTINGS_TYPE_COOKIES, |
| 58 CONTENT_SETTING_SESSION_ONLY |
| 59 }, { |
| 60 prefs::kManagedCookiesBlockedForUrls, |
| 61 CONTENT_SETTINGS_TYPE_COOKIES, |
| 62 CONTENT_SETTING_BLOCK |
| 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 Loading... |
| 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 Loading... |
| 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 LOG(INFO) << "Skiping unset preference: " << pref_name; |
| 340 continue; |
| 341 } |
| 342 |
| 343 const PrefService::Preference* pref = prefs->FindPreference(pref_name); |
| 344 DCHECK(pref->IsManaged()); |
| 345 DCHECK_EQ(Value::TYPE_LIST, pref->GetType()); |
| 346 |
| 347 const ListValue* pattern_str_list = |
| 348 static_cast<const ListValue*>(pref->GetValue()); |
| 349 for (size_t j = 0; j < pattern_str_list->GetSize(); ++j) { |
| 350 std::string original_pattern_str; |
| 351 pattern_str_list->GetString(j, &original_pattern_str); |
| 352 ContentSettingsPattern pattern(original_pattern_str); |
| 353 if (!pattern.IsValid()) { |
| 354 // Ignore invalid patterns |
| 355 continue; |
| 356 LOG(WARNING) << "Ignoring invalid content settings pattern: " |
| 357 << pattern.AsString(); |
| 358 } |
| 359 rules->push_back(MakeTuple( |
| 360 pattern, |
| 361 pattern, |
| 362 kPrefsForManagedContentSettingsMap[i].content_type, |
| 363 NO_IDENTIFIER, |
| 364 kPrefsForManagedContentSettingsMap[i].setting)); |
| 365 } |
| 366 } |
| 367 } |
| 368 |
| 369 void PolicyProvider::ReadManagedContentSettings(bool overwrite) { |
| 370 ContentSettingsRules rules; |
| 371 PrefService* prefs = profile_->GetPrefs(); |
| 372 GetContentSettingsFromPreferences(prefs, &rules); |
| 373 { |
| 374 base::AutoLock auto_lock(lock()); |
| 375 HostContentSettings* content_settings_map = host_content_settings(); |
| 376 if (overwrite) |
| 377 content_settings_map->clear(); |
| 378 for (ContentSettingsRules::iterator rule = rules.begin(); |
| 379 rule != rules.end(); |
| 380 ++rule) { |
| 381 DispatchToMethod(this, &PolicyProvider::UpdateContentSettingsMap, *rule); |
| 382 } |
| 383 } |
| 384 } |
| 385 |
| 386 // Since the PolicyProvider is a read only content settings provider, all |
| 387 // methodes of the ProviderInterface that set or delete any settings do nothing. |
| 388 void PolicyProvider::SetContentSetting( |
| 389 const ContentSettingsPattern& requesting_pattern, |
| 390 const ContentSettingsPattern& embedding_pattern, |
| 391 ContentSettingsType content_type, |
| 392 const ResourceIdentifier& resource_identifier, |
| 393 ContentSetting content_setting) { |
| 394 } |
| 395 |
| 396 ContentSetting PolicyProvider::GetContentSetting( |
| 397 const GURL& requesting_url, |
| 398 const GURL& embedding_url, |
| 399 ContentSettingsType content_type, |
| 400 const ResourceIdentifier& resource_identifier) const { |
| 401 return BaseProvider::GetContentSetting( |
| 402 requesting_url, |
| 403 embedding_url, |
| 404 content_type, |
| 405 NO_IDENTIFIER); |
| 406 } |
| 407 |
| 408 void PolicyProvider::ClearAllContentSettingsRules( |
| 409 ContentSettingsType content_type) { |
| 410 } |
| 411 |
| 412 void PolicyProvider::ResetToDefaults() { |
| 413 } |
| 414 |
| 415 void PolicyProvider::UnregisterObservers() { |
| 416 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 417 if (!profile_) |
| 418 return; |
| 419 pref_change_registrar_.RemoveAll(); |
| 420 notification_registrar_.Remove(this, NotificationType::PROFILE_DESTROYED, |
| 421 Source<Profile>(profile_)); |
| 422 profile_ = NULL; |
| 423 } |
| 424 |
| 425 void PolicyProvider::NotifyObservers( |
| 426 const ContentSettingsDetails& details) { |
| 427 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 428 if (profile_ == NULL) |
| 429 return; |
| 430 NotificationService::current()->Notify( |
| 431 NotificationType::CONTENT_SETTINGS_CHANGED, |
| 432 Source<HostContentSettingsMap>(profile_->GetHostContentSettingsMap()), |
| 433 Details<const ContentSettingsDetails>(&details)); |
| 434 } |
| 435 |
| 436 void PolicyProvider::Observe(NotificationType type, |
| 437 const NotificationSource& source, |
| 438 const NotificationDetails& details) { |
| 439 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 440 |
| 441 if (type == NotificationType::PREF_CHANGED) { |
| 442 DCHECK_EQ(profile_->GetPrefs(), Source<PrefService>(source).ptr()); |
| 443 std::string* name = Details<std::string>(details).ptr(); |
| 444 if (*name == prefs::kManagedCookiesAllowedForUrls || |
| 445 *name == prefs::kManagedCookiesBlockedForUrls || |
| 446 *name == prefs::kManagedCookiesSessionOnlyForUrls || |
| 447 *name == prefs::kManagedImagesAllowedForUrls || |
| 448 *name == prefs::kManagedImagesBlockedForUrls || |
| 449 *name == prefs::kManagedJavaScriptAllowedForUrls || |
| 450 *name == prefs::kManagedJavaScriptBlockedForUrls || |
| 451 *name == prefs::kManagedPluginsAllowedForUrls || |
| 452 *name == prefs::kManagedPluginsBlockedForUrls || |
| 453 *name == prefs::kManagedPopupsAllowedForUrls || |
| 454 *name == prefs::kManagedPopupsBlockedForUrls) { |
| 455 ReadManagedContentSettings(true); |
| 456 NotifyObservers(ContentSettingsDetails( |
| 457 ContentSettingsPattern(), CONTENT_SETTINGS_TYPE_DEFAULT, "")); |
| 458 // We do not want to sent a notification when managed default content |
| 459 // settings change. The DefaultProvider will take care of that. We are |
| 460 // only a passive observer. |
| 461 // TODO(markusheintz): NOTICE: This is still work in progress and part of |
| 462 // a larger refactoring. The code will change and be much cleaner and |
| 463 // clearer in the end. |
| 464 } else if (*name == prefs::kManagedDefaultCookiesSetting) { |
| 465 ReadManagedContentSettingsTypes(CONTENT_SETTINGS_TYPE_COOKIES); |
| 466 } else if (*name == prefs::kManagedDefaultImagesSetting) { |
| 467 ReadManagedContentSettingsTypes(CONTENT_SETTINGS_TYPE_IMAGES); |
| 468 } else if (*name == prefs::kManagedDefaultJavaScriptSetting) { |
| 469 ReadManagedContentSettingsTypes(CONTENT_SETTINGS_TYPE_JAVASCRIPT); |
| 470 } else if (*name == prefs::kManagedDefaultPluginsSetting) { |
| 471 ReadManagedContentSettingsTypes(CONTENT_SETTINGS_TYPE_PLUGINS); |
| 472 } else if (*name == prefs::kManagedDefaultPopupsSetting) { |
| 473 ReadManagedContentSettingsTypes(CONTENT_SETTINGS_TYPE_POPUPS); |
| 474 } |
| 475 } else if (type == NotificationType::PROFILE_DESTROYED) { |
| 476 DCHECK_EQ(profile_, Source<Profile>(source).ptr()); |
| 477 UnregisterObservers(); |
| 478 } else { |
| 479 NOTREACHED() << "Unexpected notification"; |
| 480 } |
| 481 } |
| 482 |
| 198 } // namespace content_settings | 483 } // namespace content_settings |
| OLD | NEW |