Chromium Code Reviews| Index: chrome/browser/geolocation/geolocation_content_settings_map.cc |
| diff --git a/chrome/browser/geolocation/geolocation_content_settings_map.cc b/chrome/browser/geolocation/geolocation_content_settings_map.cc |
| index 2eb2f747760b08cf4b410c373beaa1468a82dc73..13e00ba4b2344a73a8d090ad5772cdce21e1635b 100644 |
| --- a/chrome/browser/geolocation/geolocation_content_settings_map.cc |
| +++ b/chrome/browser/geolocation/geolocation_content_settings_map.cc |
| @@ -18,10 +18,13 @@ |
| #include "base/string_piece.h" |
| #include "base/utf_string_conversions.h" |
| #include "chrome/browser/browser_thread.h" |
| +#include "chrome/browser/content_settings/content_settings_details.h" |
| +#include "chrome/browser/content_settings/content_settings_pattern.h" |
| #include "chrome/browser/prefs/pref_service.h" |
| #include "chrome/browser/prefs/scoped_pref_update.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/common/notification_service.h" |
| +#include "chrome/common/notification_source.h" |
| #include "chrome/common/notification_type.h" |
| #include "chrome/common/pref_names.h" |
| #include "chrome/common/url_constants.h" |
| @@ -33,8 +36,14 @@ const ContentSetting |
| GeolocationContentSettingsMap::kDefaultSetting = CONTENT_SETTING_ASK; |
| GeolocationContentSettingsMap::GeolocationContentSettingsMap(Profile* profile) |
| - : profile_(profile) { |
| + : profile_(profile), |
| + updating_preferences_(false) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + prefs_registrar_.Init(profile_->GetPrefs()); |
| + prefs_registrar_.Add(prefs::kGeolocationDefaultContentSetting, this); |
| + prefs_registrar_.Add(prefs::kGeolocationContentSettings, this); |
| + notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, |
| + Source<Profile>(profile_)); |
| } |
| // static |
| @@ -46,6 +55,9 @@ void GeolocationContentSettingsMap::RegisterUserPrefs(PrefService* prefs) { |
| ContentSetting GeolocationContentSettingsMap::GetDefaultContentSetting() const { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + // If the profile is destroyed (and set to NULL) return CONTENT_SETTING_BLOCK. |
| + if (!profile_) |
| + return CONTENT_SETTING_BLOCK; |
| const PrefService* prefs = profile_->GetPrefs(); |
| const ContentSetting default_content_setting = IntToContentSetting( |
| prefs->GetInteger(prefs::kGeolocationDefaultContentSetting)); |
| @@ -53,6 +65,15 @@ ContentSetting GeolocationContentSettingsMap::GetDefaultContentSetting() const { |
| kDefaultSetting : default_content_setting; |
| } |
| +bool GeolocationContentSettingsMap::IsDefaultContentSettingManaged() const { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + // If the profile is destroyed (and set to NULL) return true. |
| + if (!profile_) |
| + return true; |
| + return profile_->GetPrefs()->IsManagedPreference( |
| + prefs::kGeolocationDefaultContentSetting); |
| +} |
| + |
| ContentSetting GeolocationContentSettingsMap::GetContentSetting( |
| const GURL& requesting_url, |
| const GURL& embedding_url) const { |
| @@ -61,7 +82,9 @@ ContentSetting GeolocationContentSettingsMap::GetContentSetting( |
| GURL requesting_origin(requesting_url.GetOrigin()); |
| GURL embedding_origin(embedding_url.GetOrigin()); |
| DCHECK(requesting_origin.is_valid() && embedding_origin.is_valid()); |
| - |
| + // If the profile is destroyed (and set to NULL) return true. |
| + if (!profile_) |
| + return CONTENT_SETTING_BLOCK; |
| const DictionaryValue* all_settings_dictionary = |
| profile_->GetPrefs()->GetDictionary(prefs::kGeolocationContentSettings); |
| // Careful: The returned value could be NULL if the pref has never been set. |
| @@ -114,9 +137,13 @@ GeolocationContentSettingsMap::AllOriginsSettings |
| void GeolocationContentSettingsMap::SetDefaultContentSetting( |
| ContentSetting setting) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + if (!profile_) |
| + return; |
| + updating_preferences_ = true; |
| profile_->GetPrefs()->SetInteger(prefs::kGeolocationDefaultContentSetting, |
| setting == CONTENT_SETTING_DEFAULT ? |
| kDefaultSetting : setting); |
| + updating_preferences_ = false; |
| } |
| void GeolocationContentSettingsMap::SetContentSetting( |
| @@ -130,44 +157,94 @@ void GeolocationContentSettingsMap::SetContentSetting( |
| GURL embedding_origin(embedding_url.GetOrigin()); |
| DCHECK(requesting_origin.is_valid()); |
| DCHECK(embedding_origin.is_valid() || embedding_url.is_empty()); |
| + if (!profile_) |
| + return; |
| PrefService* prefs = profile_->GetPrefs(); |
| DictionaryValue* all_settings_dictionary = prefs->GetMutableDictionary( |
| prefs::kGeolocationContentSettings); |
| DCHECK(all_settings_dictionary); |
| - ScopedPrefUpdate update(prefs, prefs::kGeolocationContentSettings); |
| - DictionaryValue* requesting_origin_settings_dictionary = NULL; |
| - all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
| - requesting_origin.spec(), &requesting_origin_settings_dictionary); |
| - if (setting == CONTENT_SETTING_DEFAULT) { |
| - if (requesting_origin_settings_dictionary) { |
| - requesting_origin_settings_dictionary->RemoveWithoutPathExpansion( |
| - embedding_origin.spec(), NULL); |
| - if (requesting_origin_settings_dictionary->empty()) |
| - all_settings_dictionary->RemoveWithoutPathExpansion( |
| - requesting_origin.spec(), NULL); |
| + updating_preferences_ = true; |
| + { |
| + ScopedPrefUpdate update(prefs, prefs::kGeolocationContentSettings); |
| + DictionaryValue* requesting_origin_settings_dictionary = NULL; |
| + all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
| + requesting_origin.spec(), &requesting_origin_settings_dictionary); |
| + if (setting == CONTENT_SETTING_DEFAULT) { |
| + if (requesting_origin_settings_dictionary) { |
| + requesting_origin_settings_dictionary->RemoveWithoutPathExpansion( |
| + embedding_origin.spec(), NULL); |
| + if (requesting_origin_settings_dictionary->empty()) |
| + all_settings_dictionary->RemoveWithoutPathExpansion( |
| + requesting_origin.spec(), NULL); |
| + } |
| + } else { |
| + if (!requesting_origin_settings_dictionary) { |
| + requesting_origin_settings_dictionary = new DictionaryValue; |
| + all_settings_dictionary->SetWithoutPathExpansion( |
| + requesting_origin.spec(), requesting_origin_settings_dictionary); |
| + } |
| + DCHECK(requesting_origin_settings_dictionary); |
| + requesting_origin_settings_dictionary->SetWithoutPathExpansion( |
| + embedding_origin.spec(), Value::CreateIntegerValue(setting)); |
| } |
| - } else { |
| - if (!requesting_origin_settings_dictionary) { |
| - requesting_origin_settings_dictionary = new DictionaryValue; |
| - all_settings_dictionary->SetWithoutPathExpansion( |
| - requesting_origin.spec(), requesting_origin_settings_dictionary); |
| - } |
| - DCHECK(requesting_origin_settings_dictionary); |
| - requesting_origin_settings_dictionary->SetWithoutPathExpansion( |
| - embedding_origin.spec(), Value::CreateIntegerValue(setting)); |
| } |
| + updating_preferences_ = false; |
| } |
| void GeolocationContentSettingsMap::ResetToDefault() { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| - |
| + if (!profile_) |
| + return; |
| PrefService* prefs = profile_->GetPrefs(); |
| + updating_preferences_ = true; |
| prefs->ClearPref(prefs::kGeolocationDefaultContentSetting); |
| prefs->ClearPref(prefs::kGeolocationContentSettings); |
| + updating_preferences_ = false; |
| +} |
| + |
| +void GeolocationContentSettingsMap::NotifyObservers( |
|
jochen (gone - plz use gerrit)
2010/12/08 14:57:43
please add a unit test that the notifications are
markusheintz_
2010/12/09 14:27:01
Done.
|
| + const ContentSettingsDetails& details) { |
| + NotificationService::current()->Notify( |
| + NotificationType::CONTENT_SETTINGS_CHANGED, |
| + Source<GeolocationContentSettingsMap>(this), |
| + Details<const ContentSettingsDetails>(&details)); |
| +} |
| + |
| +void GeolocationContentSettingsMap::Observe( |
| + NotificationType type, |
| + const NotificationSource& source, |
| + const NotificationDetails& details) { |
| + if (NotificationType::PREF_CHANGED == type) { |
| + if (updating_preferences_) |
| + return; |
| + |
| + const std::string& name = *Details<std::string>(details).ptr(); |
| + if (name == prefs::kGeolocationDefaultContentSetting) { |
| + NotifyObservers(ContentSettingsDetails( |
| + ContentSettingsPattern(), |
| + CONTENT_SETTINGS_TYPE_DEFAULT, |
| + "")); |
| + } |
| + } else if (NotificationType::PROFILE_DESTROYED == type) { |
| + UnregisterObservers(); |
| + } else { |
| + NOTREACHED(); |
| + } |
| +} |
| + |
| +void GeolocationContentSettingsMap::UnregisterObservers() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + if (!profile_) |
| + return; |
| + prefs_registrar_.RemoveAll(); |
| + notification_registrar_.Remove(this, NotificationType::PROFILE_DESTROYED, |
| + Source<Profile>(profile_)); |
| + profile_ = NULL; |
| } |
| GeolocationContentSettingsMap::~GeolocationContentSettingsMap() { |
| + UnregisterObservers(); |
| } |
| // static |