Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(176)

Side by Side Diff: chrome/browser/geolocation/geolocation_content_settings_map.cc

Issue 1525018: Simplify the geolocation content settings map: it's only ever used from the U... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #include "chrome/browser/geolocation/geolocation_content_settings_map.h" 5 #include "chrome/browser/geolocation/geolocation_content_settings_map.h"
6 6
7 #include "base/utf_string_conversions.h" 7 #include "base/utf_string_conversions.h"
8 #include "chrome/browser/chrome_thread.h" 8 #include "chrome/browser/chrome_thread.h"
9 #include "chrome/browser/pref_service.h" 9 #include "chrome/browser/pref_service.h"
10 #include "chrome/browser/profile.h" 10 #include "chrome/browser/profile.h"
11 #include "chrome/browser/scoped_pref_update.h" 11 #include "chrome/browser/scoped_pref_update.h"
12 #include "chrome/common/notification_service.h" 12 #include "chrome/common/notification_service.h"
13 #include "chrome/common/notification_type.h" 13 #include "chrome/common/notification_type.h"
14 #include "chrome/common/pref_names.h" 14 #include "chrome/common/pref_names.h"
15 #include "chrome/common/url_constants.h" 15 #include "chrome/common/url_constants.h"
16 #include "net/base/dns_util.h" 16 #include "net/base/dns_util.h"
17 #include "net/base/static_cookie_policy.h" 17 #include "net/base/static_cookie_policy.h"
18 18
19 // static 19 // static
20 const ContentSetting 20 const ContentSetting
21 GeolocationContentSettingsMap::kDefaultSetting = CONTENT_SETTING_ASK; 21 GeolocationContentSettingsMap::kDefaultSetting = CONTENT_SETTING_ASK;
22 22
23 GeolocationContentSettingsMap::GeolocationContentSettingsMap(Profile* profile) 23 GeolocationContentSettingsMap::GeolocationContentSettingsMap(Profile* profile)
24 : profile_(profile), updating_preferences_(false) { 24 : profile_(profile) {
25 PrefService* prefs = profile_->GetPrefs(); 25 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
26
27 // Read global defaults.
28 default_content_setting_ = IntToContentSetting(
29 prefs->GetInteger(prefs::kGeolocationDefaultContentSetting));
30 if (default_content_setting_ == CONTENT_SETTING_DEFAULT)
31 default_content_setting_ = kDefaultSetting;
32
33 // Read exceptions from the preference service.
34 ReadExceptions();
35
36 prefs->AddPrefObserver(prefs::kGeolocationContentSettings, this);
37 } 26 }
38 27
39 // static 28 // static
40 void GeolocationContentSettingsMap::RegisterUserPrefs(PrefService* prefs) { 29 void GeolocationContentSettingsMap::RegisterUserPrefs(PrefService* prefs) {
41 prefs->RegisterIntegerPref(prefs::kGeolocationDefaultContentSetting, 30 prefs->RegisterIntegerPref(prefs::kGeolocationDefaultContentSetting,
42 CONTENT_SETTING_ASK); 31 CONTENT_SETTING_ASK);
43 prefs->RegisterDictionaryPref(prefs::kGeolocationContentSettings); 32 prefs->RegisterDictionaryPref(prefs::kGeolocationContentSettings);
44 } 33 }
45 34
46 // static 35 // static
47 std::string GeolocationContentSettingsMap::OriginToString(const GURL& origin) { 36 std::string GeolocationContentSettingsMap::OriginToString(const GURL& origin) {
48 std::string port_component((origin.IntPort() != url_parse::PORT_UNSPECIFIED) ? 37 std::string port_component((origin.IntPort() != url_parse::PORT_UNSPECIFIED) ?
49 ":" + origin.port() : ""); 38 ":" + origin.port() : "");
50 std::string scheme_component(!origin.SchemeIs(chrome::kHttpScheme) ? 39 std::string scheme_component(!origin.SchemeIs(chrome::kHttpScheme) ?
51 origin.scheme() + chrome::kStandardSchemeSeparator : ""); 40 origin.scheme() + chrome::kStandardSchemeSeparator : "");
52 return scheme_component + origin.host() + port_component; 41 return scheme_component + origin.host() + port_component;
53 } 42 }
54 43
55 ContentSetting GeolocationContentSettingsMap::GetDefaultContentSetting() const { 44 ContentSetting GeolocationContentSettingsMap::GetDefaultContentSetting() const {
56 AutoLock auto_lock(lock_); 45 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
57 return default_content_setting_; 46 const PrefService* prefs = profile_->GetPrefs();
47 const ContentSetting default_content_setting = IntToContentSetting(
48 prefs->GetInteger(prefs::kGeolocationDefaultContentSetting));
49 return default_content_setting == CONTENT_SETTING_DEFAULT ?
50 kDefaultSetting : default_content_setting;
58 } 51 }
59 52
60 ContentSetting GeolocationContentSettingsMap::GetContentSetting( 53 ContentSetting GeolocationContentSettingsMap::GetContentSetting(
61 const GURL& requesting_url, 54 const GURL& requesting_url,
62 const GURL& embedding_url) const { 55 const GURL& embedding_url) const {
56 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
63 DCHECK(requesting_url.is_valid() && embedding_url.is_valid()); 57 DCHECK(requesting_url.is_valid() && embedding_url.is_valid());
64 GURL requesting_origin(requesting_url.GetOrigin()); 58 GURL requesting_origin(requesting_url.GetOrigin());
65 GURL embedding_origin(embedding_url.GetOrigin()); 59 GURL embedding_origin(embedding_url.GetOrigin());
66 DCHECK(requesting_origin.is_valid() && embedding_origin.is_valid()); 60 DCHECK(requesting_origin.is_valid() && embedding_origin.is_valid());
67 AutoLock auto_lock(lock_); 61
68 AllOriginsSettings::const_iterator i(content_settings_.find( 62 const DictionaryValue* all_settings_dictionary =
69 requesting_origin)); 63 profile_->GetPrefs()->GetDictionary(prefs::kGeolocationContentSettings);
70 if (i != content_settings_.end()) { 64 // Careful: The returned value could be NULL if the pref has never been set.
71 OneOriginSettings::const_iterator j(i->second.find(embedding_origin)); 65 if (all_settings_dictionary != NULL) {
72 if (j != i->second.end()) 66 DictionaryValue* requesting_origin_settings;
73 return j->second; 67 if (all_settings_dictionary->GetDictionaryWithoutPathExpansion(
74 if (requesting_origin != embedding_origin) { 68 UTF8ToWide(requesting_origin.spec()), &requesting_origin_settings)) {
75 OneOriginSettings::const_iterator any_embedder(i->second.find(GURL())); 69 int setting;
76 if (any_embedder != i->second.end()) 70 if (requesting_origin_settings->GetIntegerWithoutPathExpansion(
77 return any_embedder->second; 71 UTF8ToWide(embedding_origin.spec()), &setting))
72 return IntToContentSetting(setting);
73 // Check for any-embedder setting
74 if (requesting_origin != embedding_origin &&
75 requesting_origin_settings->GetIntegerWithoutPathExpansion(
76 L"", &setting))
77 return IntToContentSetting(setting);
78 } 78 }
79 } 79 }
80 return default_content_setting_; 80 return GetDefaultContentSetting();
81 } 81 }
82 82
83 GeolocationContentSettingsMap::AllOriginsSettings 83 GeolocationContentSettingsMap::AllOriginsSettings
84 GeolocationContentSettingsMap::GetAllOriginsSettings() const { 84 GeolocationContentSettingsMap::GetAllOriginsSettings() const {
85 AutoLock auto_lock(lock_); 85 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
86 return content_settings_; 86 AllOriginsSettings content_settings;
87 const DictionaryValue* all_settings_dictionary =
88 profile_->GetPrefs()->GetDictionary(prefs::kGeolocationContentSettings);
89 // Careful: The returned value could be NULL if the pref has never been set.
90 if (all_settings_dictionary != NULL) {
91 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys());
92 i != all_settings_dictionary->end_keys(); ++i) {
93 const std::wstring& wide_origin(*i);
94 GURL origin_as_url(WideToUTF8(wide_origin));
95 if (!origin_as_url.is_valid())
96 continue;
97 DictionaryValue* requesting_origin_settings_dictionary = NULL;
98 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion(
99 wide_origin, &requesting_origin_settings_dictionary);
100 DCHECK(found);
101 if (!requesting_origin_settings_dictionary)
102 continue;
103 GetOneOriginSettingsFromDictionary(
104 requesting_origin_settings_dictionary,
105 &content_settings[origin_as_url]);
106 }
107 }
108 return content_settings;
87 } 109 }
88 110
89 void GeolocationContentSettingsMap::SetDefaultContentSetting( 111 void GeolocationContentSettingsMap::SetDefaultContentSetting(
90 ContentSetting setting) { 112 ContentSetting setting) {
91 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); 113 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
92 {
93 AutoLock auto_lock(lock_);
94 default_content_setting_ =
95 (setting == CONTENT_SETTING_DEFAULT) ? kDefaultSetting : setting;
96 }
97 profile_->GetPrefs()->SetInteger(prefs::kGeolocationDefaultContentSetting, 114 profile_->GetPrefs()->SetInteger(prefs::kGeolocationDefaultContentSetting,
98 default_content_setting_); 115 setting == CONTENT_SETTING_DEFAULT ?
116 kDefaultSetting : setting);
99 } 117 }
100 118
101 void GeolocationContentSettingsMap::SetContentSetting( 119 void GeolocationContentSettingsMap::SetContentSetting(
102 const GURL& requesting_url, 120 const GURL& requesting_url,
103 const GURL& embedding_url, 121 const GURL& embedding_url,
104 ContentSetting setting) { 122 ContentSetting setting) {
105 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); 123 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
106 DCHECK(requesting_url.is_valid()); 124 DCHECK(requesting_url.is_valid());
107 DCHECK(embedding_url.is_valid() || embedding_url.is_empty()); 125 DCHECK(embedding_url.is_valid() || embedding_url.is_empty());
108 GURL requesting_origin(requesting_url.GetOrigin()); 126 GURL requesting_origin(requesting_url.GetOrigin());
109 GURL embedding_origin(embedding_url.GetOrigin()); 127 GURL embedding_origin(embedding_url.GetOrigin());
110 DCHECK(requesting_origin.is_valid() && 128 DCHECK(requesting_origin.is_valid());
111 (embedding_origin.is_valid() || embedding_url.is_empty())); 129 DCHECK(embedding_origin.is_valid() || embedding_url.is_empty());
112 std::wstring wide_requesting_origin(UTF8ToWide(requesting_origin.spec())); 130 std::wstring wide_requesting_origin(UTF8ToWide(requesting_origin.spec()));
113 std::wstring wide_embedding_origin(UTF8ToWide(embedding_origin.spec())); 131 std::wstring wide_embedding_origin(UTF8ToWide(embedding_origin.spec()));
114 DictionaryValue* all_settings_dictionary = 132 PrefService* prefs = profile_->GetPrefs();
115 profile_->GetPrefs()->GetMutableDictionary( 133 DictionaryValue* all_settings_dictionary = prefs->GetMutableDictionary(
116 prefs::kGeolocationContentSettings); 134 prefs::kGeolocationContentSettings);
135 DCHECK(all_settings_dictionary);
117 136
118 updating_preferences_ = true; 137 ScopedPrefUpdate update(prefs, prefs::kGeolocationContentSettings);
119 { 138 DictionaryValue* requesting_origin_settings_dictionary = NULL;
120 ScopedPrefUpdate update(profile_->GetPrefs(), 139 all_settings_dictionary->GetDictionaryWithoutPathExpansion(
121 prefs::kGeolocationContentSettings); 140 wide_requesting_origin, &requesting_origin_settings_dictionary);
122 AutoLock auto_lock(lock_); 141 if (setting == CONTENT_SETTING_DEFAULT) {
123 DictionaryValue* requesting_origin_settings_dictionary; 142 if (requesting_origin_settings_dictionary) {
124 all_settings_dictionary->GetDictionaryWithoutPathExpansion( 143 requesting_origin_settings_dictionary->RemoveWithoutPathExpansion(
125 wide_requesting_origin, &requesting_origin_settings_dictionary); 144 wide_embedding_origin, NULL);
126 if (setting == CONTENT_SETTING_DEFAULT) { 145 if (requesting_origin_settings_dictionary->empty())
127 if (content_settings_.count(requesting_origin) && 146 all_settings_dictionary->RemoveWithoutPathExpansion(
128 content_settings_[requesting_origin].count(embedding_origin)) { 147 wide_requesting_origin, NULL);
129 if (content_settings_[requesting_origin].size() == 1) {
130 all_settings_dictionary->RemoveWithoutPathExpansion(
131 wide_requesting_origin, NULL);
132 content_settings_.erase(requesting_origin);
133 } else {
134 requesting_origin_settings_dictionary->RemoveWithoutPathExpansion(
135 wide_embedding_origin, NULL);
136 content_settings_[requesting_origin].erase(embedding_origin);
137 }
138 }
139 } else {
140 if (!content_settings_.count(requesting_origin)) {
141 requesting_origin_settings_dictionary = new DictionaryValue;
142 all_settings_dictionary->SetWithoutPathExpansion(
143 wide_requesting_origin, requesting_origin_settings_dictionary);
144 }
145 content_settings_[requesting_origin][embedding_origin] = setting;
146 DCHECK(requesting_origin_settings_dictionary);
147 requesting_origin_settings_dictionary->SetWithoutPathExpansion(
148 wide_embedding_origin, Value::CreateIntegerValue(setting));
149 } 148 }
149 } else {
150 if (!requesting_origin_settings_dictionary) {
151 requesting_origin_settings_dictionary = new DictionaryValue;
152 all_settings_dictionary->SetWithoutPathExpansion(
153 wide_requesting_origin, requesting_origin_settings_dictionary);
154 }
155 DCHECK(requesting_origin_settings_dictionary);
156 requesting_origin_settings_dictionary->SetWithoutPathExpansion(
157 wide_embedding_origin, Value::CreateIntegerValue(setting));
150 } 158 }
151 updating_preferences_ = false;
152 }
153
154 void GeolocationContentSettingsMap::ClearOneRequestingOrigin(
155 const GURL& requesting_origin) {
156 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
157 DCHECK(requesting_origin.is_valid());
158
159 {
160 AutoLock auto_lock(lock_);
161 AllOriginsSettings::iterator i(content_settings_.find(requesting_origin));
162 if (i == content_settings_.end())
163 return;
164 content_settings_.erase(i);
165 }
166
167 PrefService* prefs = profile_->GetPrefs();
168 DictionaryValue* all_settings_dictionary =
169 prefs->GetMutableDictionary(prefs::kGeolocationContentSettings);
170 updating_preferences_ = true;
171 {
172 ScopedPrefUpdate update(prefs, prefs::kGeolocationContentSettings);
173 all_settings_dictionary->RemoveWithoutPathExpansion(
174 UTF8ToWide(requesting_origin.spec()), NULL);
175 }
176 updating_preferences_ = false;
177 } 159 }
178 160
179 void GeolocationContentSettingsMap::ResetToDefault() { 161 void GeolocationContentSettingsMap::ResetToDefault() {
180 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); 162 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
181 163
182 {
183 AutoLock auto_lock(lock_);
184 default_content_setting_ = kDefaultSetting;
185 content_settings_.clear();
186 }
187
188 PrefService* prefs = profile_->GetPrefs(); 164 PrefService* prefs = profile_->GetPrefs();
189 updating_preferences_ = true; 165 prefs->ClearPref(prefs::kGeolocationDefaultContentSetting);
190 { 166 prefs->ClearPref(prefs::kGeolocationContentSettings);
191 prefs->ClearPref(prefs::kGeolocationDefaultContentSetting);
192 prefs->ClearPref(prefs::kGeolocationContentSettings);
193 }
194 updating_preferences_ = false;
195 }
196
197 void GeolocationContentSettingsMap::Observe(
198 NotificationType type,
199 const NotificationSource& source,
200 const NotificationDetails& details) {
201 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
202 DCHECK(NotificationType::PREF_CHANGED == type);
203 DCHECK_EQ(profile_->GetPrefs(), Source<PrefService>(source).ptr());
204 if (updating_preferences_)
205 return;
206
207 std::wstring* name = Details<std::wstring>(details).ptr();
208 if (prefs::kGeolocationContentSettings == *name) {
209 ReadExceptions();
210 } else {
211 NOTREACHED() << "Unexpected preference observed.";
212 }
213 } 167 }
214 168
215 GeolocationContentSettingsMap::~GeolocationContentSettingsMap() { 169 GeolocationContentSettingsMap::~GeolocationContentSettingsMap() {
216 profile_->GetPrefs()->RemovePrefObserver(prefs::kGeolocationContentSettings,
217 this);
218 }
219
220 void GeolocationContentSettingsMap::ReadExceptions() {
221 PrefService* prefs = profile_->GetPrefs();
222 const DictionaryValue* all_settings_dictionary =
223 prefs->GetDictionary(prefs::kGeolocationContentSettings);
224 content_settings_.clear();
225 // Careful: The returned value could be NULL if the pref has never been set.
226 if (all_settings_dictionary != NULL) {
227 for (DictionaryValue::key_iterator i(
228 all_settings_dictionary->begin_keys());
229 i != all_settings_dictionary->end_keys(); ++i) {
230 const std::wstring& wide_origin(*i);
231 DictionaryValue* requesting_origin_settings_dictionary = NULL;
232 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion(
233 wide_origin, &requesting_origin_settings_dictionary);
234 DCHECK(found);
235 GURL origin_as_url(WideToUTF8(wide_origin));
236 if (!origin_as_url.is_valid())
237 continue;
238 OneOriginSettings* requesting_origin_settings =
239 &content_settings_[origin_as_url];
240 GetOneOriginSettingsFromDictionary(
241 requesting_origin_settings_dictionary,
242 requesting_origin_settings);
243 }
244 }
245 } 170 }
246 171
247 // static 172 // static
248 void GeolocationContentSettingsMap::GetOneOriginSettingsFromDictionary( 173 void GeolocationContentSettingsMap::GetOneOriginSettingsFromDictionary(
249 const DictionaryValue* dictionary, 174 const DictionaryValue* dictionary,
250 OneOriginSettings* one_origin_settings) { 175 OneOriginSettings* one_origin_settings) {
251 for (DictionaryValue::key_iterator i(dictionary->begin_keys()); 176 for (DictionaryValue::key_iterator i(dictionary->begin_keys());
252 i != dictionary->end_keys(); ++i) { 177 i != dictionary->end_keys(); ++i) {
253 const std::wstring& target(*i); 178 const std::wstring& target(*i);
254 int setting = kDefaultSetting; 179 int setting = kDefaultSetting;
255 bool found = dictionary->GetIntegerWithoutPathExpansion(target, &setting); 180 bool found = dictionary->GetIntegerWithoutPathExpansion(target, &setting);
256 DCHECK(found); 181 DCHECK(found);
257 GURL target_url(WideToUTF8(target)); 182 GURL target_url(WideToUTF8(target));
258 // An empty URL has a special meaning (wildcard), so only accept invalid 183 // An empty URL has a special meaning (wildcard), so only accept invalid
259 // URLs if the original version was empty (avoids treating corrupted prefs 184 // URLs if the original version was empty (avoids treating corrupted prefs
260 // as the wildcard entry; see http://crbug.com/39685) 185 // as the wildcard entry; see http://crbug.com/39685)
261 if (target_url.is_valid() || target.empty()) 186 if (target_url.is_valid() || target.empty())
262 (*one_origin_settings)[target_url] = IntToContentSetting(setting); 187 (*one_origin_settings)[target_url] = IntToContentSetting(setting);
263 } 188 }
264 } 189 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698