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