Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/geolocation/geolocation_content_settings_map.h" | |
| 6 | |
| 7 #include "base/utf_string_conversions.h" | |
| 8 #include "chrome/browser/chrome_thread.h" | |
| 9 #include "chrome/browser/pref_service.h" | |
| 10 #include "chrome/browser/profile.h" | |
| 11 #include "chrome/common/notification_service.h" | |
| 12 #include "chrome/common/notification_type.h" | |
| 13 #include "chrome/common/pref_names.h" | |
| 14 #include "chrome/common/url_constants.h" | |
| 15 #include "net/base/dns_util.h" | |
| 16 #include "net/base/static_cookie_policy.h" | |
| 17 | |
| 18 // static | |
| 19 const ContentSetting | |
| 20 GeolocationContentSettingsMap::kDefaultSetting = CONTENT_SETTING_ASK; | |
| 21 | |
| 22 GeolocationContentSettingsMap::GeolocationContentSettingsMap(Profile* profile) | |
| 23 : profile_(profile) { | |
| 24 PrefService* prefs = profile_->GetPrefs(); | |
| 25 | |
| 26 // Read global defaults. | |
| 27 int default_content_settting = | |
|
Peter Kasting
2010/03/18 20:59:20
Nit: 3 ts
| |
| 28 prefs->GetInteger(prefs::kGeolocationDefaultContentSetting); | |
| 29 default_content_setting_ = ConvertToContentSetting(default_content_settting); | |
| 30 if (default_content_setting_ == CONTENT_SETTING_DEFAULT) | |
| 31 default_content_setting_ = kDefaultSetting; | |
| 32 | |
| 33 // Read exceptions. | |
| 34 const DictionaryValue* all_settings_dictionary = | |
| 35 prefs->GetDictionary(prefs::kGeolocationContentSettings); | |
| 36 // Careful: The returned value could be NULL if the pref has never been set. | |
| 37 if (all_settings_dictionary != NULL) { | |
| 38 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); | |
| 39 i != all_settings_dictionary->end_keys(); ++i) { | |
| 40 const std::wstring& wide_origin(*i); | |
| 41 DictionaryValue* requesting_origin_settings_dictionary = NULL; | |
| 42 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( | |
| 43 wide_origin, &requesting_origin_settings_dictionary); | |
| 44 DCHECK(found); | |
| 45 EmbedderSettings* embedder_settings = | |
| 46 &requesting_origin_settings_[GURL(WideToUTF8(wide_origin))]; | |
| 47 GetEmbedderSettingsFromDictionary(requesting_origin_settings_dictionary, | |
| 48 embedder_settings); | |
| 49 } | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 // static | |
| 54 void GeolocationContentSettingsMap::RegisterUserPrefs(PrefService* prefs) { | |
| 55 prefs->RegisterIntegerPref(prefs::kGeolocationDefaultContentSetting, | |
| 56 CONTENT_SETTING_ASK); | |
| 57 prefs->RegisterDictionaryPref(prefs::kGeolocationContentSettings); | |
| 58 } | |
| 59 | |
| 60 ContentSetting GeolocationContentSettingsMap::GetDefaultContentSetting() const { | |
| 61 AutoLock auto_lock(lock_); | |
| 62 return default_content_setting_; | |
| 63 } | |
| 64 | |
| 65 ContentSetting GeolocationContentSettingsMap::GetContentSetting( | |
| 66 const GURL& embedder, const GURL& requesting_origin) const { | |
|
Peter Kasting
2010/03/18 20:59:20
Nit: We should check that these are both valid.
| |
| 67 AutoLock auto_lock(lock_); | |
| 68 RequestingOriginSettings::const_iterator i( | |
| 69 requesting_origin_settings_.find(requesting_origin.GetOrigin())); | |
| 70 if (i != requesting_origin_settings_.end()) { | |
| 71 EmbedderSettings::const_iterator j(i->second.find(embedder.GetOrigin())); | |
| 72 if (j != i->second.end()) | |
| 73 return j->second; | |
| 74 EmbedderSettings::const_iterator any_embedder(i->second.find(GURL())); | |
|
Peter Kasting
2010/03/18 20:59:20
We should only do this if embedder != requesting_o
| |
| 75 if (any_embedder != i->second.end()) | |
| 76 return any_embedder->second; | |
| 77 } | |
| 78 return default_content_setting_; | |
| 79 } | |
| 80 | |
| 81 const GeolocationContentSettingsMap::RequestingOriginSettings& | |
| 82 GeolocationContentSettingsMap::requesting_origin_settings() const { | |
| 83 return requesting_origin_settings_; | |
| 84 } | |
| 85 | |
| 86 void GeolocationContentSettingsMap::SetDefaultContentSetting( | |
| 87 ContentSetting setting) { | |
| 88 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | |
| 89 { | |
| 90 AutoLock auto_lock(lock_); | |
| 91 default_content_setting_ = setting == CONTENT_SETTING_DEFAULT ? | |
|
Peter Kasting
2010/03/18 20:59:20
Nit: Enclosing the test in parens seems slightly m
| |
| 92 kDefaultSetting : setting; | |
| 93 } | |
| 94 profile_->GetPrefs()->SetInteger(prefs::kGeolocationDefaultContentSetting, | |
| 95 default_content_setting_); | |
| 96 } | |
| 97 | |
| 98 void GeolocationContentSettingsMap::SetContentSetting( | |
| 99 const GURL& embedder, const GURL& requesting_origin, | |
| 100 ContentSetting setting) { | |
| 101 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | |
| 102 DCHECK(embedder.is_valid() || embedder.is_empty()); | |
| 103 DCHECK(requesting_origin.is_valid()); | |
| 104 | |
| 105 GURL embedder_internal = embedder.GetOrigin(); | |
| 106 GURL requesting_origin_internal = requesting_origin.GetOrigin(); | |
| 107 std::wstring wide_requesting_origin(UTF8ToWide(requesting_origin.spec())); | |
| 108 DictionaryValue* all_settings_dictionary = | |
| 109 profile_->GetPrefs()->GetMutableDictionary( | |
| 110 prefs::kGeolocationContentSettings); | |
| 111 std::wstring embedder_dictionary_path(UTF8ToWide(embedder_internal.spec())); | |
| 112 | |
| 113 AutoLock auto_lock(lock_); | |
| 114 DictionaryValue* requesting_origin_settings_dictionary; | |
| 115 all_settings_dictionary->GetDictionaryWithoutPathExpansion( | |
| 116 wide_requesting_origin, &requesting_origin_settings_dictionary); | |
| 117 if (setting == CONTENT_SETTING_DEFAULT) { | |
| 118 if (!requesting_origin_settings_.count(requesting_origin_internal) || | |
| 119 !requesting_origin_settings_[requesting_origin_internal].count( | |
| 120 embedder_internal)) | |
|
Peter Kasting
2010/03/18 20:59:20
Nit: I'd indent this 4
| |
| 121 return; | |
| 122 if (requesting_origin_settings_[requesting_origin_internal].size() == 1U) { | |
|
Peter Kasting
2010/03/18 20:59:20
Nit: No need for U here.
| |
| 123 all_settings_dictionary->RemoveWithoutPathExpansion( | |
| 124 wide_requesting_origin, NULL); | |
| 125 requesting_origin_settings_.erase(requesting_origin_internal); | |
| 126 return; | |
| 127 } | |
| 128 requesting_origin_settings_dictionary->RemoveWithoutPathExpansion( | |
| 129 embedder_dictionary_path, NULL); | |
| 130 requesting_origin_settings_[requesting_origin_internal].erase( | |
| 131 embedder_internal); | |
| 132 return; | |
| 133 } | |
| 134 | |
| 135 if (!requesting_origin_settings_.count(requesting_origin_internal)) { | |
| 136 requesting_origin_settings_dictionary = new DictionaryValue; | |
| 137 all_settings_dictionary->SetWithoutPathExpansion( | |
| 138 wide_requesting_origin, requesting_origin_settings_dictionary); | |
| 139 } | |
| 140 requesting_origin_settings_[requesting_origin_internal][embedder_internal] = | |
| 141 setting; | |
| 142 DCHECK(requesting_origin_settings_dictionary); | |
| 143 requesting_origin_settings_dictionary->SetWithoutPathExpansion( | |
| 144 embedder_dictionary_path, Value::CreateIntegerValue(setting)); | |
| 145 } | |
| 146 | |
| 147 void GeolocationContentSettingsMap::ResetToDefault() { | |
|
Peter Kasting
2010/03/18 20:59:20
We also need a function to reset the settings for
| |
| 148 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | |
| 149 | |
| 150 { | |
| 151 AutoLock auto_lock(lock_); | |
| 152 default_content_setting_ = kDefaultSetting; | |
| 153 requesting_origin_settings_.clear(); | |
| 154 } | |
| 155 | |
| 156 PrefService* prefs = profile_->GetPrefs(); | |
| 157 prefs->ClearPref(prefs::kGeolocationDefaultContentSetting); | |
| 158 prefs->ClearPref(prefs::kGeolocationContentSettings); | |
| 159 } | |
| 160 | |
| 161 GeolocationContentSettingsMap::~GeolocationContentSettingsMap() { | |
| 162 } | |
| 163 | |
| 164 // static | |
| 165 void GeolocationContentSettingsMap::GetEmbedderSettingsFromDictionary( | |
| 166 const DictionaryValue* dictionary, EmbedderSettings* embedder_settings) { | |
| 167 for (DictionaryValue::key_iterator i(dictionary->begin_keys()); | |
| 168 i != dictionary->end_keys(); ++i) { | |
| 169 const std::wstring& target(*i); | |
| 170 int setting = kDefaultSetting; | |
| 171 bool found = dictionary->GetIntegerWithoutPathExpansion(target, &setting); | |
| 172 DCHECK(found); | |
| 173 (*embedder_settings)[GURL(WideToUTF8(target))] = | |
| 174 ConvertToContentSetting(setting); | |
| 175 } | |
| 176 } | |
| 177 | |
| 178 // static | |
| 179 ContentSetting GeolocationContentSettingsMap::ConvertToContentSetting( | |
| 180 int content_setting) { | |
| 181 DCHECK(content_setting >= CONTENT_SETTING_DEFAULT && | |
| 182 content_setting < CONTENT_SETTING_NUM_SETTINGS); | |
| 183 return static_cast<ContentSetting>(content_setting); | |
| 184 } | |
| OLD | NEW |