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) { |
| 37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 41 prefs_registrar_.Init(profile_->GetPrefs()); | |
| 42 prefs_registrar_.Add(prefs::kGeolocationDefaultContentSetting, this); | |
| 43 prefs_registrar_.Add(prefs::kGeolocationContentSettings, this); | |
| 44 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, | |
| 45 Source<Profile>(profile_)); | |
| 38 } | 46 } |
| 39 | 47 |
| 40 // static | 48 // static |
| 41 void GeolocationContentSettingsMap::RegisterUserPrefs(PrefService* prefs) { | 49 void GeolocationContentSettingsMap::RegisterUserPrefs(PrefService* prefs) { |
| 42 prefs->RegisterIntegerPref(prefs::kGeolocationDefaultContentSetting, | 50 prefs->RegisterIntegerPref(prefs::kGeolocationDefaultContentSetting, |
| 43 CONTENT_SETTING_ASK); | 51 CONTENT_SETTING_ASK); |
| 44 prefs->RegisterDictionaryPref(prefs::kGeolocationContentSettings); | 52 prefs->RegisterDictionaryPref(prefs::kGeolocationContentSettings); |
| 45 } | 53 } |
| 46 | 54 |
| 47 ContentSetting GeolocationContentSettingsMap::GetDefaultContentSetting() const { | 55 ContentSetting GeolocationContentSettingsMap::GetDefaultContentSetting() const { |
| 48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 49 const PrefService* prefs = profile_->GetPrefs(); | 57 const PrefService* prefs = profile_->GetPrefs(); |
|
jochen (gone - plz use gerrit)
2010/12/07 18:55:55
you should make sure that profile_ actually exists
markusheintz_
2010/12/08 14:41:39
Done.
| |
| 50 const ContentSetting default_content_setting = IntToContentSetting( | 58 const ContentSetting default_content_setting = IntToContentSetting( |
| 51 prefs->GetInteger(prefs::kGeolocationDefaultContentSetting)); | 59 prefs->GetInteger(prefs::kGeolocationDefaultContentSetting)); |
| 52 return default_content_setting == CONTENT_SETTING_DEFAULT ? | 60 return default_content_setting == CONTENT_SETTING_DEFAULT ? |
| 53 kDefaultSetting : default_content_setting; | 61 kDefaultSetting : default_content_setting; |
| 54 } | 62 } |
| 55 | 63 |
| 64 bool GeolocationContentSettingsMap::IsDefaultContentSettingManaged() const { | |
| 65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 66 return profile_->GetPrefs()->IsManagedPreference( | |
| 67 prefs::kGeolocationDefaultContentSetting); | |
| 68 } | |
| 69 | |
| 56 ContentSetting GeolocationContentSettingsMap::GetContentSetting( | 70 ContentSetting GeolocationContentSettingsMap::GetContentSetting( |
| 57 const GURL& requesting_url, | 71 const GURL& requesting_url, |
| 58 const GURL& embedding_url) const { | 72 const GURL& embedding_url) const { |
| 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 60 DCHECK(requesting_url.is_valid() && embedding_url.is_valid()); | 74 DCHECK(requesting_url.is_valid() && embedding_url.is_valid()); |
| 61 GURL requesting_origin(requesting_url.GetOrigin()); | 75 GURL requesting_origin(requesting_url.GetOrigin()); |
| 62 GURL embedding_origin(embedding_url.GetOrigin()); | 76 GURL embedding_origin(embedding_url.GetOrigin()); |
| 63 DCHECK(requesting_origin.is_valid() && embedding_origin.is_valid()); | 77 DCHECK(requesting_origin.is_valid() && embedding_origin.is_valid()); |
| 64 | 78 |
| 65 const DictionaryValue* all_settings_dictionary = | 79 const DictionaryValue* all_settings_dictionary = |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 160 } | 174 } |
| 161 | 175 |
| 162 void GeolocationContentSettingsMap::ResetToDefault() { | 176 void GeolocationContentSettingsMap::ResetToDefault() { |
| 163 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 164 | 178 |
| 165 PrefService* prefs = profile_->GetPrefs(); | 179 PrefService* prefs = profile_->GetPrefs(); |
| 166 prefs->ClearPref(prefs::kGeolocationDefaultContentSetting); | 180 prefs->ClearPref(prefs::kGeolocationDefaultContentSetting); |
| 167 prefs->ClearPref(prefs::kGeolocationContentSettings); | 181 prefs->ClearPref(prefs::kGeolocationContentSettings); |
| 168 } | 182 } |
| 169 | 183 |
| 184 void GeolocationContentSettingsMap::NotifyObservers( | |
| 185 const ContentSettingsDetails& details) { | |
| 186 NotificationService::current()->Notify( | |
| 187 NotificationType::CONTENT_SETTINGS_CHANGED, | |
| 188 Source<GeolocationContentSettingsMap>(this), | |
| 189 Details<const ContentSettingsDetails>(&details)); | |
| 190 } | |
| 191 | |
| 192 void GeolocationContentSettingsMap::Observe( | |
| 193 NotificationType type, | |
| 194 const NotificationSource& source, | |
| 195 const NotificationDetails& details) { | |
| 196 if (NotificationType::PREF_CHANGED == type) { | |
| 197 const std::string& name = *Details<std::string>(details).ptr(); | |
| 198 if (name == prefs::kGeolocationDefaultContentSetting) { | |
| 199 NotifyObservers(ContentSettingsDetails( | |
|
jochen (gone - plz use gerrit)
2010/12/07 18:55:55
Since this map always reads the settings out of th
markusheintz_
2010/12/08 14:41:39
If you don't mind I'd like to keep it separate.
| |
| 200 ContentSettingsPattern(), | |
| 201 CONTENT_SETTINGS_TYPE_DEFAULT, | |
| 202 "")); | |
| 203 } | |
| 204 } if (NotificationType::PROFILE_DESTROYED == type) { | |
| 205 UnregisterObservers(); | |
| 206 }else { | |
| 207 NOTREACHED(); | |
| 208 } | |
| 209 } | |
| 210 | |
| 211 void GeolocationContentSettingsMap::UnregisterObservers() { | |
| 212 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 213 if (!profile_) | |
| 214 return; | |
| 215 prefs_registrar_.RemoveAll(); | |
| 216 notification_registrar_.Remove(this, NotificationType::PROFILE_DESTROYED, | |
| 217 Source<Profile>(profile_)); | |
| 218 profile_ = NULL; | |
| 219 } | |
| 220 | |
| 170 GeolocationContentSettingsMap::~GeolocationContentSettingsMap() { | 221 GeolocationContentSettingsMap::~GeolocationContentSettingsMap() { |
| 171 } | 222 } |
| 172 | 223 |
| 173 // static | 224 // static |
| 174 void GeolocationContentSettingsMap::GetOneOriginSettingsFromDictionary( | 225 void GeolocationContentSettingsMap::GetOneOriginSettingsFromDictionary( |
| 175 const DictionaryValue* dictionary, | 226 const DictionaryValue* dictionary, |
| 176 OneOriginSettings* one_origin_settings) { | 227 OneOriginSettings* one_origin_settings) { |
| 177 for (DictionaryValue::key_iterator i(dictionary->begin_keys()); | 228 for (DictionaryValue::key_iterator i(dictionary->begin_keys()); |
| 178 i != dictionary->end_keys(); ++i) { | 229 i != dictionary->end_keys(); ++i) { |
| 179 const std::string& target(*i); | 230 const std::string& target(*i); |
| 180 int setting = kDefaultSetting; | 231 int setting = kDefaultSetting; |
| 181 bool found = dictionary->GetIntegerWithoutPathExpansion(target, &setting); | 232 bool found = dictionary->GetIntegerWithoutPathExpansion(target, &setting); |
| 182 DCHECK(found); | 233 DCHECK(found); |
| 183 GURL target_url(target); | 234 GURL target_url(target); |
| 184 // An empty URL has a special meaning (wildcard), so only accept invalid | 235 // An empty URL has a special meaning (wildcard), so only accept invalid |
| 185 // URLs if the original version was empty (avoids treating corrupted prefs | 236 // URLs if the original version was empty (avoids treating corrupted prefs |
| 186 // as the wildcard entry; see http://crbug.com/39685) | 237 // as the wildcard entry; see http://crbug.com/39685) |
| 187 if (target_url.is_valid() || target.empty()) | 238 if (target_url.is_valid() || target.empty()) |
| 188 (*one_origin_settings)[target_url] = IntToContentSetting(setting); | 239 (*one_origin_settings)[target_url] = IntToContentSetting(setting); |
| 189 } | 240 } |
| 190 } | 241 } |
| OLD | NEW |