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