| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/message_loop.h" | |
| 6 #include "chrome/browser/content_settings/content_settings_details.h" | |
| 7 #include "chrome/browser/content_settings/host_content_settings_map.h" | |
| 8 #include "chrome/browser/geolocation/geolocation_content_settings_map.h" | |
| 9 #include "chrome/browser/prefs/pref_service.h" | |
| 10 #include "chrome/browser/prefs/scoped_user_pref_update.h" | |
| 11 #include "chrome/common/chrome_notification_types.h" | |
| 12 #include "chrome/common/pref_names.h" | |
| 13 #include "chrome/test/base/testing_profile.h" | |
| 14 #include "content/browser/browser_thread.h" | |
| 15 #include "content/common/notification_registrar.h" | |
| 16 #include "content/common/notification_service.h" | |
| 17 #include "testing/gmock/include/gmock/gmock.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 using ::testing::_; | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 class MockGeolocationSettingsObserver : public NotificationObserver { | |
| 25 public: | |
| 26 MockGeolocationSettingsObserver(); | |
| 27 | |
| 28 virtual void Observe(int type, | |
| 29 const NotificationSource& source, | |
| 30 const NotificationDetails& details); | |
| 31 | |
| 32 MOCK_METHOD2(OnContentSettingsChanged, | |
| 33 void(GeolocationContentSettingsMap*, | |
| 34 ContentSettingsType)); | |
| 35 | |
| 36 private: | |
| 37 NotificationRegistrar registrar_; | |
| 38 }; | |
| 39 | |
| 40 MockGeolocationSettingsObserver::MockGeolocationSettingsObserver() { | |
| 41 registrar_.Add(this, chrome::NOTIFICATION_GEOLOCATION_SETTINGS_CHANGED, | |
| 42 NotificationService::AllSources()); | |
| 43 } | |
| 44 | |
| 45 void MockGeolocationSettingsObserver::Observe( | |
| 46 int type, | |
| 47 const NotificationSource& source, | |
| 48 const NotificationDetails& details) { | |
| 49 GeolocationContentSettingsMap* map = | |
| 50 Source<GeolocationContentSettingsMap>(source).ptr(); | |
| 51 ContentSettingsDetails* settings_details = | |
| 52 Details<ContentSettingsDetails>(details).ptr(); | |
| 53 OnContentSettingsChanged(map, | |
| 54 settings_details->type()); | |
| 55 // This checks that calling a Get function from an observer doesn't | |
| 56 // deadlock. | |
| 57 // TODO(bauerb): Move into expectation setup. | |
| 58 map->GetContentSetting(GURL("http://random-hostname.com/"), | |
| 59 GURL("http://foo.random-hostname.com/")); | |
| 60 } | |
| 61 | |
| 62 class GeolocationContentSettingsMapTests : public testing::Test { | |
| 63 public: | |
| 64 GeolocationContentSettingsMapTests() | |
| 65 : ui_thread_(BrowserThread::UI, &message_loop_) { | |
| 66 } | |
| 67 | |
| 68 protected: | |
| 69 MessageLoop message_loop_; | |
| 70 BrowserThread ui_thread_; | |
| 71 }; | |
| 72 | |
| 73 TEST_F(GeolocationContentSettingsMapTests, Embedder) { | |
| 74 TestingProfile profile; | |
| 75 GeolocationContentSettingsMap* map = | |
| 76 profile.GetGeolocationContentSettingsMap(); | |
| 77 GURL top_level("http://www.toplevel0.com/foo/bar"); | |
| 78 EXPECT_EQ(CONTENT_SETTING_ASK, map->GetContentSetting(top_level, top_level)); | |
| 79 // Now set the permission for requester_0. | |
| 80 map->SetContentSetting(top_level, top_level, CONTENT_SETTING_ALLOW); | |
| 81 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
| 82 map->GetContentSetting(top_level, top_level)); | |
| 83 | |
| 84 GURL requester_0("http://www.frame0.com/foo/bar"); | |
| 85 EXPECT_EQ(CONTENT_SETTING_ASK, | |
| 86 map->GetContentSetting(requester_0, top_level)); | |
| 87 // Now set the permission for only requester_1. | |
| 88 map->SetContentSetting(requester_0, top_level, CONTENT_SETTING_ALLOW); | |
| 89 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
| 90 map->GetContentSetting(top_level, top_level)); | |
| 91 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
| 92 map->GetContentSetting(requester_0, top_level)); | |
| 93 // Block only requester_1. | |
| 94 map->SetContentSetting(requester_0, top_level, CONTENT_SETTING_BLOCK); | |
| 95 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
| 96 map->GetContentSetting(requester_0, top_level)); | |
| 97 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
| 98 map->GetContentSetting(top_level, top_level)); | |
| 99 } | |
| 100 | |
| 101 TEST_F(GeolocationContentSettingsMapTests, MultipleEmbeddersAndOrigins) { | |
| 102 TestingProfile profile; | |
| 103 GeolocationContentSettingsMap* map = | |
| 104 profile.GetGeolocationContentSettingsMap(); | |
| 105 GURL requester_0("http://www.iframe0.com/foo/bar"); | |
| 106 GURL requester_1("http://www.iframe1.co.uk/bar/foo"); | |
| 107 GURL embedder_0("http://www.toplevel0.com/foo/bar"); | |
| 108 EXPECT_EQ(CONTENT_SETTING_ASK, | |
| 109 map->GetContentSetting(requester_0, embedder_0)); | |
| 110 EXPECT_EQ(CONTENT_SETTING_ASK, | |
| 111 map->GetContentSetting(requester_1, embedder_0)); | |
| 112 // Now set the permission for only one origin. | |
| 113 map->SetContentSetting(requester_0, embedder_0, CONTENT_SETTING_ALLOW); | |
| 114 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
| 115 map->GetContentSetting(requester_0, embedder_0)); | |
| 116 EXPECT_EQ(CONTENT_SETTING_ASK, | |
| 117 map->GetContentSetting(requester_1, embedder_0)); | |
| 118 // Set the permission for the other origin. | |
| 119 map->SetContentSetting(requester_1, embedder_0, CONTENT_SETTING_ALLOW); | |
| 120 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
| 121 map->GetContentSetting(requester_1, embedder_0)); | |
| 122 // Check they're not allowed on a different embedder. | |
| 123 GURL embedder_1("http://www.toplevel1.com/foo/bar"); | |
| 124 EXPECT_EQ(CONTENT_SETTING_ASK, | |
| 125 map->GetContentSetting(requester_0, embedder_1)); | |
| 126 EXPECT_EQ(CONTENT_SETTING_ASK, | |
| 127 map->GetContentSetting(requester_1, embedder_1)); | |
| 128 // Check all settings are valid. | |
| 129 GeolocationContentSettingsMap::AllOriginsSettings content_settings( | |
| 130 map->GetAllOriginsSettings()); | |
| 131 EXPECT_EQ(0U, content_settings.count(requester_0)); | |
| 132 EXPECT_EQ(1U, content_settings.count(requester_0.GetOrigin())); | |
| 133 GeolocationContentSettingsMap::OneOriginSettings one_origin_settings( | |
| 134 content_settings[requester_0.GetOrigin()]); | |
| 135 EXPECT_EQ(CONTENT_SETTING_ALLOW, one_origin_settings[embedder_0.GetOrigin()]); | |
| 136 | |
| 137 EXPECT_EQ(0U, content_settings.count(requester_1)); | |
| 138 EXPECT_EQ(1U, content_settings.count(requester_1.GetOrigin())); | |
| 139 one_origin_settings = content_settings[requester_1.GetOrigin()]; | |
| 140 EXPECT_EQ(CONTENT_SETTING_ALLOW, one_origin_settings[embedder_0.GetOrigin()]); | |
| 141 // Block requester_1 on the first embedder and add it to the second. | |
| 142 map->SetContentSetting(requester_1, embedder_0, CONTENT_SETTING_BLOCK); | |
| 143 map->SetContentSetting(requester_1, embedder_1, CONTENT_SETTING_ALLOW); | |
| 144 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
| 145 map->GetContentSetting(requester_1, embedder_0)); | |
| 146 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
| 147 map->GetContentSetting(requester_1, embedder_1)); | |
| 148 } | |
| 149 | |
| 150 TEST_F(GeolocationContentSettingsMapTests, SetContentSettingDefault) { | |
| 151 TestingProfile profile; | |
| 152 GeolocationContentSettingsMap* map = | |
| 153 profile.GetGeolocationContentSettingsMap(); | |
| 154 GURL top_level("http://www.toplevel0.com/foo/bar"); | |
| 155 map->SetContentSetting(top_level, top_level, CONTENT_SETTING_ALLOW); | |
| 156 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
| 157 map->GetContentSetting(top_level, top_level)); | |
| 158 // Set to CONTENT_SETTING_DEFAULT and check the actual value has changed. | |
| 159 map->SetContentSetting(top_level, top_level, CONTENT_SETTING_DEFAULT); | |
| 160 EXPECT_EQ(CONTENT_SETTING_ASK, map->GetContentSetting(top_level, top_level)); | |
| 161 } | |
| 162 | |
| 163 TEST_F(GeolocationContentSettingsMapTests, Reset) { | |
| 164 TestingProfile profile; | |
| 165 GeolocationContentSettingsMap* map = | |
| 166 profile.GetGeolocationContentSettingsMap(); | |
| 167 GURL requester_0("http://www.iframe0.com/foo/bar"); | |
| 168 GURL embedder_0("http://www.toplevel0.com/foo/bar"); | |
| 169 map->SetContentSetting(requester_0, embedder_0, CONTENT_SETTING_ALLOW); | |
| 170 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
| 171 map->GetContentSetting(requester_0, embedder_0)); | |
| 172 GeolocationContentSettingsMap::AllOriginsSettings content_settings( | |
| 173 map->GetAllOriginsSettings()); | |
| 174 EXPECT_EQ(1U, content_settings.size()); | |
| 175 | |
| 176 // Set to CONTENT_SETTING_DEFAULT and check the actual value has changed. | |
| 177 map->SetContentSetting(requester_0, embedder_0, CONTENT_SETTING_DEFAULT); | |
| 178 EXPECT_EQ(CONTENT_SETTING_ASK, | |
| 179 map->GetContentSetting(requester_0, embedder_0)); | |
| 180 content_settings = map->GetAllOriginsSettings(); | |
| 181 EXPECT_TRUE(content_settings.empty()); | |
| 182 } | |
| 183 | |
| 184 TEST_F(GeolocationContentSettingsMapTests, ClearsWhenSettingBackToDefault) { | |
| 185 TestingProfile profile; | |
| 186 GeolocationContentSettingsMap* map = | |
| 187 profile.GetGeolocationContentSettingsMap(); | |
| 188 GURL requester_0("http://www.iframe0.com/foo/bar"); | |
| 189 GURL requester_1("http://www.iframe1.com/foo/bar"); | |
| 190 GURL embedder_0("http://www.toplevel0.com/foo/bar"); | |
| 191 map->SetContentSetting(requester_0, embedder_0, CONTENT_SETTING_ALLOW); | |
| 192 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
| 193 map->GetContentSetting(requester_0, embedder_0)); | |
| 194 GeolocationContentSettingsMap::AllOriginsSettings content_settings( | |
| 195 map->GetAllOriginsSettings()); | |
| 196 EXPECT_EQ(1U, content_settings.size()); | |
| 197 | |
| 198 map->SetContentSetting(requester_1, embedder_0, CONTENT_SETTING_ALLOW); | |
| 199 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
| 200 map->GetContentSetting(requester_1, embedder_0)); | |
| 201 content_settings = map->GetAllOriginsSettings(); | |
| 202 EXPECT_EQ(2U, content_settings.size()); | |
| 203 EXPECT_EQ(1U, content_settings[requester_0.GetOrigin()].size()); | |
| 204 EXPECT_EQ(1U, content_settings[requester_1.GetOrigin()].size()); | |
| 205 | |
| 206 // Set to default. | |
| 207 map->SetContentSetting(requester_0, embedder_0, CONTENT_SETTING_DEFAULT); | |
| 208 content_settings = map->GetAllOriginsSettings(); | |
| 209 EXPECT_EQ(1U, content_settings.size()); | |
| 210 | |
| 211 map->SetContentSetting(requester_1, embedder_0, CONTENT_SETTING_DEFAULT); | |
| 212 content_settings = map->GetAllOriginsSettings(); | |
| 213 EXPECT_TRUE(content_settings.empty()); | |
| 214 } | |
| 215 | |
| 216 TEST_F(GeolocationContentSettingsMapTests, WildCardForEmptyEmbedder) { | |
| 217 TestingProfile profile; | |
| 218 GeolocationContentSettingsMap* map = | |
| 219 profile.GetGeolocationContentSettingsMap(); | |
| 220 GURL requester_0("http://www.iframe0.com/foo/bar"); | |
| 221 GURL embedder_0("http://www.toplevel0.com/foo/bar"); | |
| 222 GURL embedder_1("http://www.toplevel1.com/foo/bar"); | |
| 223 GURL empty_url; | |
| 224 map->SetContentSetting(requester_0, embedder_0, CONTENT_SETTING_BLOCK); | |
| 225 map->SetContentSetting(requester_0, empty_url, CONTENT_SETTING_ALLOW); | |
| 226 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
| 227 map->GetContentSetting(requester_0, embedder_0)); | |
| 228 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
| 229 map->GetContentSetting(requester_0, embedder_1)); | |
| 230 EXPECT_EQ(CONTENT_SETTING_ASK, | |
| 231 map->GetContentSetting(requester_0, requester_0)); | |
| 232 | |
| 233 // Change the wildcard behavior. | |
| 234 map->SetContentSetting(requester_0, embedder_0, CONTENT_SETTING_ALLOW); | |
| 235 map->SetContentSetting(requester_0, empty_url, CONTENT_SETTING_BLOCK); | |
| 236 profile.GetHostContentSettingsMap()->SetDefaultContentSetting( | |
| 237 CONTENT_SETTINGS_TYPE_GEOLOCATION, CONTENT_SETTING_ALLOW); | |
| 238 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
| 239 map->GetContentSetting(requester_0, embedder_0)); | |
| 240 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
| 241 map->GetContentSetting(requester_0, embedder_1)); | |
| 242 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
| 243 map->GetContentSetting(requester_0, requester_0)); | |
| 244 } | |
| 245 | |
| 246 TEST_F(GeolocationContentSettingsMapTests, IgnoreInvalidURLsInPrefs) { | |
| 247 TestingProfile profile; | |
| 248 { | |
| 249 DictionaryPrefUpdate update(profile.GetPrefs(), | |
| 250 prefs::kGeolocationContentSettings); | |
| 251 DictionaryValue* all_settings_dictionary = update.Get(); | |
| 252 // For simplicity, use the overloads that do path expansion. As '.' is the | |
| 253 // path separator, we can't have dotted hostnames (which is fine). | |
| 254 all_settings_dictionary->SetInteger("http://a/.http://b/", | |
| 255 CONTENT_SETTING_ALLOW); | |
| 256 all_settings_dictionary->SetInteger("bad_requester.http://b/", | |
| 257 CONTENT_SETTING_ALLOW); | |
| 258 all_settings_dictionary->SetInteger("http://a/.bad-embedder", | |
| 259 CONTENT_SETTING_ALLOW); | |
| 260 } | |
| 261 | |
| 262 GeolocationContentSettingsMap* map = | |
| 263 profile.GetGeolocationContentSettingsMap(); | |
| 264 EXPECT_EQ(CONTENT_SETTING_ASK, | |
| 265 profile.GetHostContentSettingsMap()->GetDefaultContentSetting( | |
| 266 CONTENT_SETTINGS_TYPE_GEOLOCATION)); | |
| 267 | |
| 268 // Check the valid entry was read OK. | |
| 269 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
| 270 map->GetContentSetting(GURL("http://a/"), GURL("http://b/"))); | |
| 271 // But everything else should be according to the default. | |
| 272 EXPECT_EQ(CONTENT_SETTING_ASK, | |
| 273 map->GetContentSetting(GURL("http://a/"), | |
| 274 GURL("http://bad-embedder"))); | |
| 275 EXPECT_EQ(CONTENT_SETTING_ASK, | |
| 276 map->GetContentSetting(GURL("http://a/"), | |
| 277 GURL("http://example.com"))); | |
| 278 EXPECT_EQ(CONTENT_SETTING_ASK, | |
| 279 map->GetContentSetting(GURL("http://bad_requester/"), | |
| 280 GURL("http://b/"))); | |
| 281 } | |
| 282 | |
| 283 TEST_F(GeolocationContentSettingsMapTests, Observe) { | |
| 284 TestingProfile profile; | |
| 285 GeolocationContentSettingsMap* map = | |
| 286 profile.GetGeolocationContentSettingsMap(); | |
| 287 MockGeolocationSettingsObserver observer; | |
| 288 | |
| 289 EXPECT_EQ(CONTENT_SETTING_ASK, | |
| 290 profile.GetHostContentSettingsMap()->GetDefaultContentSetting( | |
| 291 CONTENT_SETTINGS_TYPE_GEOLOCATION)); | |
| 292 | |
| 293 // Test if a GEOLOCATION_SETTINGS_CHANGED notification is sent after | |
| 294 // the geolocation default content setting was changed through calling the | |
| 295 // SetDefaultContentSetting method. | |
| 296 EXPECT_CALL( | |
| 297 observer, | |
| 298 OnContentSettingsChanged(map, CONTENT_SETTINGS_TYPE_DEFAULT)); | |
| 299 profile.GetHostContentSettingsMap()->SetDefaultContentSetting( | |
| 300 CONTENT_SETTINGS_TYPE_GEOLOCATION, CONTENT_SETTING_BLOCK); | |
| 301 ::testing::Mock::VerifyAndClearExpectations(&observer); | |
| 302 | |
| 303 // Test if a GEOLOCATION_SETTINGS_CHANGED notification is sent after the | |
| 304 // obsolete preference kGeolocationDefaultContentSetting was changed. | |
| 305 PrefService* prefs = profile.GetPrefs(); | |
| 306 EXPECT_CALL( | |
| 307 observer, | |
| 308 OnContentSettingsChanged(map, CONTENT_SETTINGS_TYPE_DEFAULT)); | |
| 309 prefs->SetInteger(prefs::kGeolocationDefaultContentSetting, | |
| 310 CONTENT_SETTING_ALLOW); | |
| 311 | |
| 312 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
| 313 profile.GetHostContentSettingsMap()->GetDefaultContentSetting( | |
| 314 CONTENT_SETTINGS_TYPE_GEOLOCATION)); | |
| 315 } | |
| 316 | |
| 317 } // namespace | |
| OLD | NEW |