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)); |
57 // If the profile is destroyed (and set to NULL) return CONTENT_SETTING_BLOCK. | |
58 if (!profile_) | |
59 return CONTENT_SETTING_BLOCK; | |
49 const PrefService* prefs = profile_->GetPrefs(); | 60 const PrefService* prefs = profile_->GetPrefs(); |
50 const ContentSetting default_content_setting = IntToContentSetting( | 61 const ContentSetting default_content_setting = IntToContentSetting( |
51 prefs->GetInteger(prefs::kGeolocationDefaultContentSetting)); | 62 prefs->GetInteger(prefs::kGeolocationDefaultContentSetting)); |
52 return default_content_setting == CONTENT_SETTING_DEFAULT ? | 63 return default_content_setting == CONTENT_SETTING_DEFAULT ? |
53 kDefaultSetting : default_content_setting; | 64 kDefaultSetting : default_content_setting; |
54 } | 65 } |
55 | 66 |
67 bool GeolocationContentSettingsMap::IsDefaultContentSettingManaged() const { | |
68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
69 // If the profile is destroyed (and set to NULL) return true. | |
70 if (!profile_) | |
71 return true; | |
72 return profile_->GetPrefs()->IsManagedPreference( | |
73 prefs::kGeolocationDefaultContentSetting); | |
74 } | |
75 | |
56 ContentSetting GeolocationContentSettingsMap::GetContentSetting( | 76 ContentSetting GeolocationContentSettingsMap::GetContentSetting( |
57 const GURL& requesting_url, | 77 const GURL& requesting_url, |
58 const GURL& embedding_url) const { | 78 const GURL& embedding_url) const { |
59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
60 DCHECK(requesting_url.is_valid() && embedding_url.is_valid()); | 80 DCHECK(requesting_url.is_valid() && embedding_url.is_valid()); |
61 GURL requesting_origin(requesting_url.GetOrigin()); | 81 GURL requesting_origin(requesting_url.GetOrigin()); |
62 GURL embedding_origin(embedding_url.GetOrigin()); | 82 GURL embedding_origin(embedding_url.GetOrigin()); |
63 DCHECK(requesting_origin.is_valid() && embedding_origin.is_valid()); | 83 DCHECK(requesting_origin.is_valid() && embedding_origin.is_valid()); |
64 | 84 // If the profile is destroyed (and set to NULL) return true. |
jochen (gone - plz use gerrit)
2010/12/09 14:36:28
true -> CONTENT_SETTING_BLOCK
markusheintz_
2010/12/15 12:15:58
Done.
| |
85 if (!profile_) | |
86 return CONTENT_SETTING_BLOCK; | |
65 const DictionaryValue* all_settings_dictionary = | 87 const DictionaryValue* all_settings_dictionary = |
66 profile_->GetPrefs()->GetDictionary(prefs::kGeolocationContentSettings); | 88 profile_->GetPrefs()->GetDictionary(prefs::kGeolocationContentSettings); |
67 // Careful: The returned value could be NULL if the pref has never been set. | 89 // Careful: The returned value could be NULL if the pref has never been set. |
68 if (all_settings_dictionary != NULL) { | 90 if (all_settings_dictionary != NULL) { |
69 DictionaryValue* requesting_origin_settings; | 91 DictionaryValue* requesting_origin_settings; |
70 if (all_settings_dictionary->GetDictionaryWithoutPathExpansion( | 92 if (all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
71 requesting_origin.spec(), &requesting_origin_settings)) { | 93 requesting_origin.spec(), &requesting_origin_settings)) { |
72 int setting; | 94 int setting; |
73 if (requesting_origin_settings->GetIntegerWithoutPathExpansion( | 95 if (requesting_origin_settings->GetIntegerWithoutPathExpansion( |
74 embedding_origin.spec(), &setting)) | 96 embedding_origin.spec(), &setting)) |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
107 requesting_origin_settings_dictionary, | 129 requesting_origin_settings_dictionary, |
108 &content_settings[origin_as_url]); | 130 &content_settings[origin_as_url]); |
109 } | 131 } |
110 } | 132 } |
111 return content_settings; | 133 return content_settings; |
112 } | 134 } |
113 | 135 |
114 void GeolocationContentSettingsMap::SetDefaultContentSetting( | 136 void GeolocationContentSettingsMap::SetDefaultContentSetting( |
115 ContentSetting setting) { | 137 ContentSetting setting) { |
116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
139 if (!profile_) | |
140 return; | |
117 profile_->GetPrefs()->SetInteger(prefs::kGeolocationDefaultContentSetting, | 141 profile_->GetPrefs()->SetInteger(prefs::kGeolocationDefaultContentSetting, |
118 setting == CONTENT_SETTING_DEFAULT ? | 142 setting == CONTENT_SETTING_DEFAULT ? |
119 kDefaultSetting : setting); | 143 kDefaultSetting : setting); |
120 } | 144 } |
121 | 145 |
122 void GeolocationContentSettingsMap::SetContentSetting( | 146 void GeolocationContentSettingsMap::SetContentSetting( |
123 const GURL& requesting_url, | 147 const GURL& requesting_url, |
124 const GURL& embedding_url, | 148 const GURL& embedding_url, |
125 ContentSetting setting) { | 149 ContentSetting setting) { |
126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
127 DCHECK(requesting_url.is_valid()); | 151 DCHECK(requesting_url.is_valid()); |
128 DCHECK(embedding_url.is_valid() || embedding_url.is_empty()); | 152 DCHECK(embedding_url.is_valid() || embedding_url.is_empty()); |
129 GURL requesting_origin(requesting_url.GetOrigin()); | 153 GURL requesting_origin(requesting_url.GetOrigin()); |
130 GURL embedding_origin(embedding_url.GetOrigin()); | 154 GURL embedding_origin(embedding_url.GetOrigin()); |
131 DCHECK(requesting_origin.is_valid()); | 155 DCHECK(requesting_origin.is_valid()); |
132 DCHECK(embedding_origin.is_valid() || embedding_url.is_empty()); | 156 DCHECK(embedding_origin.is_valid() || embedding_url.is_empty()); |
157 if (!profile_) | |
158 return; | |
133 PrefService* prefs = profile_->GetPrefs(); | 159 PrefService* prefs = profile_->GetPrefs(); |
134 DictionaryValue* all_settings_dictionary = prefs->GetMutableDictionary( | 160 DictionaryValue* all_settings_dictionary = prefs->GetMutableDictionary( |
135 prefs::kGeolocationContentSettings); | 161 prefs::kGeolocationContentSettings); |
136 DCHECK(all_settings_dictionary); | 162 DCHECK(all_settings_dictionary); |
137 | 163 |
138 ScopedPrefUpdate update(prefs, prefs::kGeolocationContentSettings); | 164 ScopedPrefUpdate update(prefs, prefs::kGeolocationContentSettings); |
139 DictionaryValue* requesting_origin_settings_dictionary = NULL; | 165 DictionaryValue* requesting_origin_settings_dictionary = NULL; |
140 all_settings_dictionary->GetDictionaryWithoutPathExpansion( | 166 all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
141 requesting_origin.spec(), &requesting_origin_settings_dictionary); | 167 requesting_origin.spec(), &requesting_origin_settings_dictionary); |
142 if (setting == CONTENT_SETTING_DEFAULT) { | 168 if (setting == CONTENT_SETTING_DEFAULT) { |
143 if (requesting_origin_settings_dictionary) { | 169 if (requesting_origin_settings_dictionary) { |
144 requesting_origin_settings_dictionary->RemoveWithoutPathExpansion( | 170 requesting_origin_settings_dictionary->RemoveWithoutPathExpansion( |
145 embedding_origin.spec(), NULL); | 171 embedding_origin.spec(), NULL); |
146 if (requesting_origin_settings_dictionary->empty()) | 172 if (requesting_origin_settings_dictionary->empty()) |
147 all_settings_dictionary->RemoveWithoutPathExpansion( | 173 all_settings_dictionary->RemoveWithoutPathExpansion( |
148 requesting_origin.spec(), NULL); | 174 requesting_origin.spec(), NULL); |
149 } | 175 } |
150 } else { | 176 } else { |
151 if (!requesting_origin_settings_dictionary) { | 177 if (!requesting_origin_settings_dictionary) { |
152 requesting_origin_settings_dictionary = new DictionaryValue; | 178 requesting_origin_settings_dictionary = new DictionaryValue; |
153 all_settings_dictionary->SetWithoutPathExpansion( | 179 all_settings_dictionary->SetWithoutPathExpansion( |
154 requesting_origin.spec(), requesting_origin_settings_dictionary); | 180 requesting_origin.spec(), requesting_origin_settings_dictionary); |
155 } | 181 } |
156 DCHECK(requesting_origin_settings_dictionary); | 182 DCHECK(requesting_origin_settings_dictionary); |
157 requesting_origin_settings_dictionary->SetWithoutPathExpansion( | 183 requesting_origin_settings_dictionary->SetWithoutPathExpansion( |
jochen (gone - plz use gerrit)
2010/12/09 14:36:28
no indent
markusheintz_
2010/12/15 12:15:58
Done.
| |
158 embedding_origin.spec(), Value::CreateIntegerValue(setting)); | 184 embedding_origin.spec(), Value::CreateIntegerValue(setting)); |
159 } | 185 } |
160 } | 186 } |
161 | 187 |
162 void GeolocationContentSettingsMap::ResetToDefault() { | 188 void GeolocationContentSettingsMap::ResetToDefault() { |
163 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
164 | 190 if (!profile_) |
191 return; | |
165 PrefService* prefs = profile_->GetPrefs(); | 192 PrefService* prefs = profile_->GetPrefs(); |
166 prefs->ClearPref(prefs::kGeolocationDefaultContentSetting); | 193 prefs->ClearPref(prefs::kGeolocationDefaultContentSetting); |
167 prefs->ClearPref(prefs::kGeolocationContentSettings); | 194 prefs->ClearPref(prefs::kGeolocationContentSettings); |
168 } | 195 } |
169 | 196 |
197 void GeolocationContentSettingsMap::NotifyObservers( | |
198 const ContentSettingsDetails& details) { | |
199 NotificationService::current()->Notify( | |
200 NotificationType::CONTENT_SETTINGS_CHANGED, | |
jochen (gone - plz use gerrit)
2010/12/09 14:36:28
hum, now that I see this (and ran into a similar b
markusheintz_
2010/12/15 12:15:58
Done.
| |
201 Source<GeolocationContentSettingsMap>(this), | |
202 Details<const ContentSettingsDetails>(&details)); | |
203 } | |
204 | |
205 void GeolocationContentSettingsMap::Observe( | |
206 NotificationType type, | |
207 const NotificationSource& source, | |
208 const NotificationDetails& details) { | |
209 if (NotificationType::PREF_CHANGED == type) { | |
jochen (gone - plz use gerrit)
2010/12/09 14:36:28
type == NotificationType::PREF_CHANGED (see coding
markusheintz_
2010/12/15 12:15:58
Done.
| |
210 const std::string& name = *Details<std::string>(details).ptr(); | |
211 if (name == prefs::kGeolocationDefaultContentSetting) { | |
212 NotifyObservers(ContentSettingsDetails( | |
213 ContentSettingsPattern(), | |
214 CONTENT_SETTINGS_TYPE_DEFAULT, | |
215 "")); | |
216 } | |
217 } else if (NotificationType::PROFILE_DESTROYED == type) { | |
218 UnregisterObservers(); | |
219 } else { | |
220 NOTREACHED(); | |
221 } | |
222 } | |
223 | |
224 void GeolocationContentSettingsMap::UnregisterObservers() { | |
225 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
226 if (!profile_) | |
227 return; | |
228 prefs_registrar_.RemoveAll(); | |
229 notification_registrar_.Remove(this, NotificationType::PROFILE_DESTROYED, | |
230 Source<Profile>(profile_)); | |
231 profile_ = NULL; | |
232 } | |
233 | |
170 GeolocationContentSettingsMap::~GeolocationContentSettingsMap() { | 234 GeolocationContentSettingsMap::~GeolocationContentSettingsMap() { |
235 UnregisterObservers(); | |
171 } | 236 } |
172 | 237 |
173 // static | 238 // static |
174 void GeolocationContentSettingsMap::GetOneOriginSettingsFromDictionary( | 239 void GeolocationContentSettingsMap::GetOneOriginSettingsFromDictionary( |
175 const DictionaryValue* dictionary, | 240 const DictionaryValue* dictionary, |
176 OneOriginSettings* one_origin_settings) { | 241 OneOriginSettings* one_origin_settings) { |
177 for (DictionaryValue::key_iterator i(dictionary->begin_keys()); | 242 for (DictionaryValue::key_iterator i(dictionary->begin_keys()); |
178 i != dictionary->end_keys(); ++i) { | 243 i != dictionary->end_keys(); ++i) { |
179 const std::string& target(*i); | 244 const std::string& target(*i); |
180 int setting = kDefaultSetting; | 245 int setting = kDefaultSetting; |
181 bool found = dictionary->GetIntegerWithoutPathExpansion(target, &setting); | 246 bool found = dictionary->GetIntegerWithoutPathExpansion(target, &setting); |
182 DCHECK(found); | 247 DCHECK(found); |
183 GURL target_url(target); | 248 GURL target_url(target); |
184 // An empty URL has a special meaning (wildcard), so only accept invalid | 249 // An empty URL has a special meaning (wildcard), so only accept invalid |
185 // URLs if the original version was empty (avoids treating corrupted prefs | 250 // URLs if the original version was empty (avoids treating corrupted prefs |
186 // as the wildcard entry; see http://crbug.com/39685) | 251 // as the wildcard entry; see http://crbug.com/39685) |
187 if (target_url.is_valid() || target.empty()) | 252 if (target_url.is_valid() || target.empty()) |
188 (*one_origin_settings)[target_url] = IntToContentSetting(setting); | 253 (*one_origin_settings)[target_url] = IntToContentSetting(setting); |
189 } | 254 } |
190 } | 255 } |
OLD | NEW |