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