Chromium Code Reviews| 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 // Implementation of the geolocation content settings map. Styled on | 5 // Implementation of the geolocation content settings map. Styled on |
| 6 // HostContentSettingsMap however unlike that class, this one does not hold | 6 // HostContentSettingsMap however unlike that class, this one does not hold |
| 7 // an additional in-memory copy of the settings as it does not need to support | 7 // an additional in-memory copy of the settings as it does not need to support |
| 8 // thread safe synchronous access to the settings; all geolocation permissions | 8 // thread safe synchronous access to the settings; all geolocation permissions |
| 9 // are read and written in the UI thread. (If in future this is no longer the | 9 // are read and written in the UI thread. (If in future this is no longer the |
| 10 // case, refer to http://codereview.chromium.org/1525018 for a previous version | 10 // case, refer to http://codereview.chromium.org/1525018 for a previous version |
| 11 // with caching. Note that as we must observe the prefs store for settings | 11 // with caching. Note that as we must observe the prefs store for settings |
| 12 // changes, e.g. coming from the sync engine, the simplest design would be to | 12 // changes, e.g. coming from the sync engine, the simplest design would be to |
| 13 // always write-through changes straight to the prefs store, and rely on the | 13 // always write-through changes straight to the prefs store, and rely on the |
| 14 // notification observer to subsequently update any cached copy). | 14 // notification observer to subsequently update any cached copy). |
| 15 | 15 |
| 16 #include "chrome/browser/geolocation/geolocation_content_settings_map.h" | 16 #include "chrome/browser/geolocation/geolocation_content_settings_map.h" |
| 17 | 17 |
| 18 #include "base/string_piece.h" | 18 #include "base/string_piece.h" |
| 19 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
| 20 #include "chrome/browser/browser_thread.h" | 20 #include "chrome/browser/browser_thread.h" |
| 21 #include "chrome/browser/content_settings/content_settings_details.h" | |
| 22 #include "chrome/browser/content_settings/content_settings_pattern.h" | |
| 21 #include "chrome/browser/prefs/pref_service.h" | 23 #include "chrome/browser/prefs/pref_service.h" |
| 22 #include "chrome/browser/prefs/scoped_pref_update.h" | 24 #include "chrome/browser/prefs/scoped_pref_update.h" |
| 23 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
| 24 #include "chrome/common/notification_service.h" | 26 #include "chrome/common/notification_service.h" |
| 27 #include "chrome/common/notification_source.h" | |
| 25 #include "chrome/common/notification_type.h" | 28 #include "chrome/common/notification_type.h" |
| 26 #include "chrome/common/pref_names.h" | 29 #include "chrome/common/pref_names.h" |
| 27 #include "chrome/common/url_constants.h" | 30 #include "chrome/common/url_constants.h" |
| 28 #include "net/base/dns_util.h" | 31 #include "net/base/dns_util.h" |
| 29 #include "net/base/static_cookie_policy.h" | 32 #include "net/base/static_cookie_policy.h" |
| 30 | 33 |
| 31 // static | 34 // static |
| 32 const ContentSetting | 35 const ContentSetting |
| 33 GeolocationContentSettingsMap::kDefaultSetting = CONTENT_SETTING_ASK; | 36 GeolocationContentSettingsMap::kDefaultSetting = CONTENT_SETTING_ASK; |
| 34 | 37 |
| 35 GeolocationContentSettingsMap::GeolocationContentSettingsMap(Profile* profile) | 38 GeolocationContentSettingsMap::GeolocationContentSettingsMap(Profile* profile) |
| 36 : profile_(profile) { | 39 : profile_(profile), |
| 40 updating_preferences_(false) { | |
| 37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 42 prefs_registrar_.Init(profile_->GetPrefs()); | |
| 43 prefs_registrar_.Add(prefs::kGeolocationDefaultContentSetting, this); | |
| 44 prefs_registrar_.Add(prefs::kGeolocationContentSettings, this); | |
| 45 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, | |
| 46 Source<Profile>(profile_)); | |
| 38 } | 47 } |
| 39 | 48 |
| 40 // static | 49 // static |
| 41 void GeolocationContentSettingsMap::RegisterUserPrefs(PrefService* prefs) { | 50 void GeolocationContentSettingsMap::RegisterUserPrefs(PrefService* prefs) { |
| 42 prefs->RegisterIntegerPref(prefs::kGeolocationDefaultContentSetting, | 51 prefs->RegisterIntegerPref(prefs::kGeolocationDefaultContentSetting, |
| 43 CONTENT_SETTING_ASK); | 52 CONTENT_SETTING_ASK); |
| 44 prefs->RegisterDictionaryPref(prefs::kGeolocationContentSettings); | 53 prefs->RegisterDictionaryPref(prefs::kGeolocationContentSettings); |
| 45 } | 54 } |
| 46 | 55 |
| 47 ContentSetting GeolocationContentSettingsMap::GetDefaultContentSetting() const { | 56 ContentSetting GeolocationContentSettingsMap::GetDefaultContentSetting() const { |
| 48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 58 // If the profile is destroyed (and set to NULL) return CONTENT_SETTING_BLOCK. | |
| 59 if (!profile_) | |
| 60 return CONTENT_SETTING_BLOCK; | |
| 49 const PrefService* prefs = profile_->GetPrefs(); | 61 const PrefService* prefs = profile_->GetPrefs(); |
| 50 const ContentSetting default_content_setting = IntToContentSetting( | 62 const ContentSetting default_content_setting = IntToContentSetting( |
| 51 prefs->GetInteger(prefs::kGeolocationDefaultContentSetting)); | 63 prefs->GetInteger(prefs::kGeolocationDefaultContentSetting)); |
| 52 return default_content_setting == CONTENT_SETTING_DEFAULT ? | 64 return default_content_setting == CONTENT_SETTING_DEFAULT ? |
| 53 kDefaultSetting : default_content_setting; | 65 kDefaultSetting : default_content_setting; |
| 54 } | 66 } |
| 55 | 67 |
| 68 bool GeolocationContentSettingsMap::IsDefaultContentSettingManaged() const { | |
| 69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 70 // If the profile is destroyed (and set to NULL) return true. | |
| 71 if (!profile_) | |
| 72 return true; | |
| 73 return profile_->GetPrefs()->IsManagedPreference( | |
| 74 prefs::kGeolocationDefaultContentSetting); | |
| 75 } | |
| 76 | |
| 56 ContentSetting GeolocationContentSettingsMap::GetContentSetting( | 77 ContentSetting GeolocationContentSettingsMap::GetContentSetting( |
| 57 const GURL& requesting_url, | 78 const GURL& requesting_url, |
| 58 const GURL& embedding_url) const { | 79 const GURL& embedding_url) const { |
| 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 60 DCHECK(requesting_url.is_valid() && embedding_url.is_valid()); | 81 DCHECK(requesting_url.is_valid() && embedding_url.is_valid()); |
| 61 GURL requesting_origin(requesting_url.GetOrigin()); | 82 GURL requesting_origin(requesting_url.GetOrigin()); |
| 62 GURL embedding_origin(embedding_url.GetOrigin()); | 83 GURL embedding_origin(embedding_url.GetOrigin()); |
| 63 DCHECK(requesting_origin.is_valid() && embedding_origin.is_valid()); | 84 DCHECK(requesting_origin.is_valid() && embedding_origin.is_valid()); |
| 64 | 85 // If the profile is destroyed (and set to NULL) return true. |
| 86 if (!profile_) | |
| 87 return CONTENT_SETTING_BLOCK; | |
| 65 const DictionaryValue* all_settings_dictionary = | 88 const DictionaryValue* all_settings_dictionary = |
| 66 profile_->GetPrefs()->GetDictionary(prefs::kGeolocationContentSettings); | 89 profile_->GetPrefs()->GetDictionary(prefs::kGeolocationContentSettings); |
| 67 // Careful: The returned value could be NULL if the pref has never been set. | 90 // Careful: The returned value could be NULL if the pref has never been set. |
| 68 if (all_settings_dictionary != NULL) { | 91 if (all_settings_dictionary != NULL) { |
| 69 DictionaryValue* requesting_origin_settings; | 92 DictionaryValue* requesting_origin_settings; |
| 70 if (all_settings_dictionary->GetDictionaryWithoutPathExpansion( | 93 if (all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
| 71 requesting_origin.spec(), &requesting_origin_settings)) { | 94 requesting_origin.spec(), &requesting_origin_settings)) { |
| 72 int setting; | 95 int setting; |
| 73 if (requesting_origin_settings->GetIntegerWithoutPathExpansion( | 96 if (requesting_origin_settings->GetIntegerWithoutPathExpansion( |
| 74 embedding_origin.spec(), &setting)) | 97 embedding_origin.spec(), &setting)) |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 107 requesting_origin_settings_dictionary, | 130 requesting_origin_settings_dictionary, |
| 108 &content_settings[origin_as_url]); | 131 &content_settings[origin_as_url]); |
| 109 } | 132 } |
| 110 } | 133 } |
| 111 return content_settings; | 134 return content_settings; |
| 112 } | 135 } |
| 113 | 136 |
| 114 void GeolocationContentSettingsMap::SetDefaultContentSetting( | 137 void GeolocationContentSettingsMap::SetDefaultContentSetting( |
| 115 ContentSetting setting) { | 138 ContentSetting setting) { |
| 116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 140 if (!profile_) | |
| 141 return; | |
| 142 updating_preferences_ = true; | |
| 117 profile_->GetPrefs()->SetInteger(prefs::kGeolocationDefaultContentSetting, | 143 profile_->GetPrefs()->SetInteger(prefs::kGeolocationDefaultContentSetting, |
| 118 setting == CONTENT_SETTING_DEFAULT ? | 144 setting == CONTENT_SETTING_DEFAULT ? |
| 119 kDefaultSetting : setting); | 145 kDefaultSetting : setting); |
| 146 updating_preferences_ = false; | |
| 120 } | 147 } |
| 121 | 148 |
| 122 void GeolocationContentSettingsMap::SetContentSetting( | 149 void GeolocationContentSettingsMap::SetContentSetting( |
| 123 const GURL& requesting_url, | 150 const GURL& requesting_url, |
| 124 const GURL& embedding_url, | 151 const GURL& embedding_url, |
| 125 ContentSetting setting) { | 152 ContentSetting setting) { |
| 126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 127 DCHECK(requesting_url.is_valid()); | 154 DCHECK(requesting_url.is_valid()); |
| 128 DCHECK(embedding_url.is_valid() || embedding_url.is_empty()); | 155 DCHECK(embedding_url.is_valid() || embedding_url.is_empty()); |
| 129 GURL requesting_origin(requesting_url.GetOrigin()); | 156 GURL requesting_origin(requesting_url.GetOrigin()); |
| 130 GURL embedding_origin(embedding_url.GetOrigin()); | 157 GURL embedding_origin(embedding_url.GetOrigin()); |
| 131 DCHECK(requesting_origin.is_valid()); | 158 DCHECK(requesting_origin.is_valid()); |
| 132 DCHECK(embedding_origin.is_valid() || embedding_url.is_empty()); | 159 DCHECK(embedding_origin.is_valid() || embedding_url.is_empty()); |
| 160 if (!profile_) | |
| 161 return; | |
| 133 PrefService* prefs = profile_->GetPrefs(); | 162 PrefService* prefs = profile_->GetPrefs(); |
| 134 DictionaryValue* all_settings_dictionary = prefs->GetMutableDictionary( | 163 DictionaryValue* all_settings_dictionary = prefs->GetMutableDictionary( |
| 135 prefs::kGeolocationContentSettings); | 164 prefs::kGeolocationContentSettings); |
| 136 DCHECK(all_settings_dictionary); | 165 DCHECK(all_settings_dictionary); |
| 137 | 166 |
| 138 ScopedPrefUpdate update(prefs, prefs::kGeolocationContentSettings); | 167 updating_preferences_ = true; |
| 139 DictionaryValue* requesting_origin_settings_dictionary = NULL; | 168 { |
| 140 all_settings_dictionary->GetDictionaryWithoutPathExpansion( | 169 ScopedPrefUpdate update(prefs, prefs::kGeolocationContentSettings); |
| 141 requesting_origin.spec(), &requesting_origin_settings_dictionary); | 170 DictionaryValue* requesting_origin_settings_dictionary = NULL; |
| 142 if (setting == CONTENT_SETTING_DEFAULT) { | 171 all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
| 143 if (requesting_origin_settings_dictionary) { | 172 requesting_origin.spec(), &requesting_origin_settings_dictionary); |
| 144 requesting_origin_settings_dictionary->RemoveWithoutPathExpansion( | 173 if (setting == CONTENT_SETTING_DEFAULT) { |
| 145 embedding_origin.spec(), NULL); | 174 if (requesting_origin_settings_dictionary) { |
| 146 if (requesting_origin_settings_dictionary->empty()) | 175 requesting_origin_settings_dictionary->RemoveWithoutPathExpansion( |
| 147 all_settings_dictionary->RemoveWithoutPathExpansion( | 176 embedding_origin.spec(), NULL); |
| 148 requesting_origin.spec(), NULL); | 177 if (requesting_origin_settings_dictionary->empty()) |
| 178 all_settings_dictionary->RemoveWithoutPathExpansion( | |
| 179 requesting_origin.spec(), NULL); | |
| 180 } | |
| 181 } else { | |
| 182 if (!requesting_origin_settings_dictionary) { | |
| 183 requesting_origin_settings_dictionary = new DictionaryValue; | |
| 184 all_settings_dictionary->SetWithoutPathExpansion( | |
| 185 requesting_origin.spec(), requesting_origin_settings_dictionary); | |
| 186 } | |
| 187 DCHECK(requesting_origin_settings_dictionary); | |
| 188 requesting_origin_settings_dictionary->SetWithoutPathExpansion( | |
| 189 embedding_origin.spec(), Value::CreateIntegerValue(setting)); | |
| 149 } | 190 } |
| 150 } else { | |
| 151 if (!requesting_origin_settings_dictionary) { | |
| 152 requesting_origin_settings_dictionary = new DictionaryValue; | |
| 153 all_settings_dictionary->SetWithoutPathExpansion( | |
| 154 requesting_origin.spec(), requesting_origin_settings_dictionary); | |
| 155 } | |
| 156 DCHECK(requesting_origin_settings_dictionary); | |
| 157 requesting_origin_settings_dictionary->SetWithoutPathExpansion( | |
| 158 embedding_origin.spec(), Value::CreateIntegerValue(setting)); | |
| 159 } | 191 } |
| 192 updating_preferences_ = false; | |
| 160 } | 193 } |
| 161 | 194 |
| 162 void GeolocationContentSettingsMap::ResetToDefault() { | 195 void GeolocationContentSettingsMap::ResetToDefault() { |
| 163 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 164 | 197 if (!profile_) |
| 198 return; | |
| 165 PrefService* prefs = profile_->GetPrefs(); | 199 PrefService* prefs = profile_->GetPrefs(); |
| 200 updating_preferences_ = true; | |
| 166 prefs->ClearPref(prefs::kGeolocationDefaultContentSetting); | 201 prefs->ClearPref(prefs::kGeolocationDefaultContentSetting); |
| 167 prefs->ClearPref(prefs::kGeolocationContentSettings); | 202 prefs->ClearPref(prefs::kGeolocationContentSettings); |
| 203 updating_preferences_ = false; | |
| 204 } | |
| 205 | |
| 206 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.
| |
| 207 const ContentSettingsDetails& details) { | |
| 208 NotificationService::current()->Notify( | |
| 209 NotificationType::CONTENT_SETTINGS_CHANGED, | |
| 210 Source<GeolocationContentSettingsMap>(this), | |
| 211 Details<const ContentSettingsDetails>(&details)); | |
| 212 } | |
| 213 | |
| 214 void GeolocationContentSettingsMap::Observe( | |
| 215 NotificationType type, | |
| 216 const NotificationSource& source, | |
| 217 const NotificationDetails& details) { | |
| 218 if (NotificationType::PREF_CHANGED == type) { | |
| 219 if (updating_preferences_) | |
| 220 return; | |
| 221 | |
| 222 const std::string& name = *Details<std::string>(details).ptr(); | |
| 223 if (name == prefs::kGeolocationDefaultContentSetting) { | |
| 224 NotifyObservers(ContentSettingsDetails( | |
| 225 ContentSettingsPattern(), | |
| 226 CONTENT_SETTINGS_TYPE_DEFAULT, | |
| 227 "")); | |
| 228 } | |
| 229 } else if (NotificationType::PROFILE_DESTROYED == type) { | |
| 230 UnregisterObservers(); | |
| 231 } else { | |
| 232 NOTREACHED(); | |
| 233 } | |
| 234 } | |
| 235 | |
| 236 void GeolocationContentSettingsMap::UnregisterObservers() { | |
| 237 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 238 if (!profile_) | |
| 239 return; | |
| 240 prefs_registrar_.RemoveAll(); | |
| 241 notification_registrar_.Remove(this, NotificationType::PROFILE_DESTROYED, | |
| 242 Source<Profile>(profile_)); | |
| 243 profile_ = NULL; | |
| 168 } | 244 } |
| 169 | 245 |
| 170 GeolocationContentSettingsMap::~GeolocationContentSettingsMap() { | 246 GeolocationContentSettingsMap::~GeolocationContentSettingsMap() { |
| 247 UnregisterObservers(); | |
| 171 } | 248 } |
| 172 | 249 |
| 173 // static | 250 // static |
| 174 void GeolocationContentSettingsMap::GetOneOriginSettingsFromDictionary( | 251 void GeolocationContentSettingsMap::GetOneOriginSettingsFromDictionary( |
| 175 const DictionaryValue* dictionary, | 252 const DictionaryValue* dictionary, |
| 176 OneOriginSettings* one_origin_settings) { | 253 OneOriginSettings* one_origin_settings) { |
| 177 for (DictionaryValue::key_iterator i(dictionary->begin_keys()); | 254 for (DictionaryValue::key_iterator i(dictionary->begin_keys()); |
| 178 i != dictionary->end_keys(); ++i) { | 255 i != dictionary->end_keys(); ++i) { |
| 179 const std::string& target(*i); | 256 const std::string& target(*i); |
| 180 int setting = kDefaultSetting; | 257 int setting = kDefaultSetting; |
| 181 bool found = dictionary->GetIntegerWithoutPathExpansion(target, &setting); | 258 bool found = dictionary->GetIntegerWithoutPathExpansion(target, &setting); |
| 182 DCHECK(found); | 259 DCHECK(found); |
| 183 GURL target_url(target); | 260 GURL target_url(target); |
| 184 // An empty URL has a special meaning (wildcard), so only accept invalid | 261 // An empty URL has a special meaning (wildcard), so only accept invalid |
| 185 // URLs if the original version was empty (avoids treating corrupted prefs | 262 // URLs if the original version was empty (avoids treating corrupted prefs |
| 186 // as the wildcard entry; see http://crbug.com/39685) | 263 // as the wildcard entry; see http://crbug.com/39685) |
| 187 if (target_url.is_valid() || target.empty()) | 264 if (target_url.is_valid() || target.empty()) |
| 188 (*one_origin_settings)[target_url] = IntToContentSetting(setting); | 265 (*one_origin_settings)[target_url] = IntToContentSetting(setting); |
| 189 } | 266 } |
| 190 } | 267 } |
| OLD | NEW |