1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/content_settings/core/browser/content_settings_pref_provider.h"
#include "base/auto_reset.h"
#include "base/command_line.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/prefs/default_pref_store.h"
#include "base/prefs/overlay_user_pref_store.h"
#include "base/prefs/pref_change_registrar.h"
#include "base/prefs/pref_service.h"
#include "base/prefs/scoped_user_pref_update.h"
#include "base/prefs/testing_pref_store.h"
#include "base/test/simple_test_clock.h"
#include "base/threading/platform_thread.h"
#include "base/values.h"
#include "chrome/browser/content_settings/content_settings_mock_observer.h"
#include "chrome/browser/prefs/browser_prefs.h"
#include "chrome/browser/prefs/pref_service_mock_factory.h"
#include "chrome/browser/prefs/pref_service_syncable.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/testing_pref_service_syncable.h"
#include "chrome/test/base/testing_profile.h"
#include "components/content_settings/core/browser/content_settings_utils.h"
#include "components/content_settings/core/test/content_settings_test_utils.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
using ::testing::_;
namespace content_settings {
class DeadlockCheckerThread : public base::PlatformThread::Delegate {
public:
explicit DeadlockCheckerThread(PrefProvider* provider)
: provider_(provider) {}
void ThreadMain() override {
bool got_lock = provider_->lock_.Try();
EXPECT_TRUE(got_lock);
if (got_lock)
provider_->lock_.Release();
}
private:
PrefProvider* provider_;
DISALLOW_COPY_AND_ASSIGN(DeadlockCheckerThread);
};
// A helper for observing an preference changes and testing whether
// |PrefProvider| holds a lock when the preferences change.
class DeadlockCheckerObserver {
public:
// |DeadlockCheckerObserver| doesn't take the ownership of |prefs| or
// ||provider|.
DeadlockCheckerObserver(PrefService* prefs, PrefProvider* provider)
: provider_(provider),
notification_received_(false) {
pref_change_registrar_.Init(prefs);
pref_change_registrar_.Add(
prefs::kContentSettingsPatternPairs,
base::Bind(
&DeadlockCheckerObserver::OnContentSettingsPatternPairsChanged,
base::Unretained(this)));
}
virtual ~DeadlockCheckerObserver() {}
bool notification_received() const {
return notification_received_;
}
private:
void OnContentSettingsPatternPairsChanged() {
// Check whether |provider_| holds its lock. For this, we need a
// separate thread.
DeadlockCheckerThread thread(provider_);
base::PlatformThreadHandle handle;
ASSERT_TRUE(base::PlatformThread::Create(0, &thread, &handle));
base::PlatformThread::Join(handle);
notification_received_ = true;
}
PrefProvider* provider_;
PrefChangeRegistrar pref_change_registrar_;
bool notification_received_;
DISALLOW_COPY_AND_ASSIGN(DeadlockCheckerObserver);
};
TEST(PrefProviderTest, Observer) {
TestingProfile profile;
PrefProvider pref_content_settings_provider(profile.GetPrefs(), false);
ContentSettingsPattern pattern =
ContentSettingsPattern::FromString("[*.]example.com");
content_settings::MockObserver mock_observer;
EXPECT_CALL(mock_observer,
OnContentSettingChanged(pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_IMAGES,
""));
pref_content_settings_provider.AddObserver(&mock_observer);
pref_content_settings_provider.SetWebsiteSetting(
pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
new base::FundamentalValue(CONTENT_SETTING_ALLOW));
pref_content_settings_provider.ShutdownOnUIThread();
}
// Test for regression in which the PrefProvider modified the user pref store
// of the OTR unintentionally: http://crbug.com/74466.
TEST(PrefProviderTest, Incognito) {
PersistentPrefStore* user_prefs = new TestingPrefStore();
OverlayUserPrefStore* otr_user_prefs =
new OverlayUserPrefStore(user_prefs);
PrefServiceMockFactory factory;
factory.set_user_prefs(make_scoped_refptr(user_prefs));
scoped_refptr<user_prefs::PrefRegistrySyncable> registry(
new user_prefs::PrefRegistrySyncable);
PrefServiceSyncable* regular_prefs =
factory.CreateSyncable(registry.get()).release();
chrome::RegisterUserProfilePrefs(registry.get());
PrefServiceMockFactory otr_factory;
otr_factory.set_user_prefs(make_scoped_refptr(otr_user_prefs));
scoped_refptr<user_prefs::PrefRegistrySyncable> otr_registry(
new user_prefs::PrefRegistrySyncable);
PrefServiceSyncable* otr_prefs =
otr_factory.CreateSyncable(otr_registry.get()).release();
chrome::RegisterUserProfilePrefs(otr_registry.get());
TestingProfile::Builder profile_builder;
profile_builder.SetPrefService(make_scoped_ptr(regular_prefs));
scoped_ptr<TestingProfile> profile = profile_builder.Build();
TestingProfile::Builder otr_profile_builder;
otr_profile_builder.SetPrefService(make_scoped_ptr(otr_prefs));
otr_profile_builder.BuildIncognito(profile.get());
PrefProvider pref_content_settings_provider(regular_prefs, false);
PrefProvider pref_content_settings_provider_incognito(otr_prefs, true);
ContentSettingsPattern pattern =
ContentSettingsPattern::FromString("[*.]example.com");
pref_content_settings_provider.SetWebsiteSetting(
pattern,
pattern,
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
new base::FundamentalValue(CONTENT_SETTING_ALLOW));
GURL host("http://example.com/");
// The value should of course be visible in the regular PrefProvider.
EXPECT_EQ(CONTENT_SETTING_ALLOW,
GetContentSetting(&pref_content_settings_provider,
host,
host,
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
false));
// And also in the OTR version.
EXPECT_EQ(CONTENT_SETTING_ALLOW,
GetContentSetting(&pref_content_settings_provider_incognito,
host,
host,
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
false));
// But the value should not be overridden in the OTR user prefs accidentally.
EXPECT_FALSE(otr_user_prefs->IsSetInOverlay(
prefs::kContentSettingsPatternPairs));
pref_content_settings_provider.ShutdownOnUIThread();
pref_content_settings_provider_incognito.ShutdownOnUIThread();
}
TEST(PrefProviderTest, GetContentSettingsValue) {
TestingProfile testing_profile;
PrefProvider provider(testing_profile.GetPrefs(), false);
GURL primary_url("http://example.com/");
ContentSettingsPattern primary_pattern =
ContentSettingsPattern::FromString("[*.]example.com");
EXPECT_EQ(CONTENT_SETTING_DEFAULT,
GetContentSetting(&provider,
primary_url,
primary_url,
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
false));
EXPECT_EQ(NULL,
GetContentSettingValue(&provider,
primary_url,
primary_url,
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
false));
provider.SetWebsiteSetting(primary_pattern,
primary_pattern,
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
new base::FundamentalValue(CONTENT_SETTING_BLOCK));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
GetContentSetting(&provider,
primary_url,
primary_url,
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
false));
scoped_ptr<base::Value> value_ptr(
GetContentSettingValue(&provider,
primary_url,
primary_url,
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
false));
int int_value = -1;
value_ptr->GetAsInteger(&int_value);
EXPECT_EQ(CONTENT_SETTING_BLOCK, IntToContentSetting(int_value));
provider.SetWebsiteSetting(primary_pattern,
primary_pattern,
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
NULL);
EXPECT_EQ(NULL,
GetContentSettingValue(&provider,
primary_url,
primary_url,
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
false));
provider.ShutdownOnUIThread();
}
TEST(PrefProviderTest, Patterns) {
TestingProfile testing_profile;
PrefProvider pref_content_settings_provider(testing_profile.GetPrefs(),
false);
GURL host1("http://example.com/");
GURL host2("http://www.example.com/");
GURL host3("http://example.org/");
GURL host4("file:///tmp/test.html");
ContentSettingsPattern pattern1 =
ContentSettingsPattern::FromString("[*.]example.com");
ContentSettingsPattern pattern2 =
ContentSettingsPattern::FromString("example.org");
ContentSettingsPattern pattern3 =
ContentSettingsPattern::FromString("file:///tmp/test.html");
EXPECT_EQ(CONTENT_SETTING_DEFAULT,
GetContentSetting(&pref_content_settings_provider,
host1,
host1,
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
false));
pref_content_settings_provider.SetWebsiteSetting(
pattern1,
pattern1,
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
new base::FundamentalValue(CONTENT_SETTING_BLOCK));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
GetContentSetting(&pref_content_settings_provider,
host1,
host1,
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
false));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
GetContentSetting(&pref_content_settings_provider,
host2,
host2,
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
false));
EXPECT_EQ(CONTENT_SETTING_DEFAULT,
GetContentSetting(&pref_content_settings_provider,
host3,
host3,
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
false));
pref_content_settings_provider.SetWebsiteSetting(
pattern2,
pattern2,
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
new base::FundamentalValue(CONTENT_SETTING_BLOCK));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
GetContentSetting(&pref_content_settings_provider,
host3,
host3,
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
false));
EXPECT_EQ(CONTENT_SETTING_DEFAULT,
GetContentSetting(&pref_content_settings_provider,
host4,
host4,
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
false));
pref_content_settings_provider.SetWebsiteSetting(
pattern3,
pattern3,
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
new base::FundamentalValue(CONTENT_SETTING_BLOCK));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
GetContentSetting(&pref_content_settings_provider,
host4,
host4,
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
false));
pref_content_settings_provider.ShutdownOnUIThread();
}
TEST(PrefProviderTest, ResourceIdentifier) {
TestingProfile testing_profile;
PrefProvider pref_content_settings_provider(testing_profile.GetPrefs(),
false);
GURL host("http://example.com/");
ContentSettingsPattern pattern =
ContentSettingsPattern::FromString("[*.]example.com");
std::string resource1("someplugin");
std::string resource2("otherplugin");
EXPECT_EQ(CONTENT_SETTING_DEFAULT,
GetContentSetting(
&pref_content_settings_provider,
host, host, CONTENT_SETTINGS_TYPE_PLUGINS,
resource1, false));
pref_content_settings_provider.SetWebsiteSetting(
pattern,
pattern,
CONTENT_SETTINGS_TYPE_PLUGINS,
resource1,
new base::FundamentalValue(CONTENT_SETTING_BLOCK));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
GetContentSetting(
&pref_content_settings_provider,
host, host, CONTENT_SETTINGS_TYPE_PLUGINS,
resource1, false));
EXPECT_EQ(CONTENT_SETTING_DEFAULT,
GetContentSetting(
&pref_content_settings_provider,
host, host, CONTENT_SETTINGS_TYPE_PLUGINS,
resource2, false));
pref_content_settings_provider.ShutdownOnUIThread();
}
TEST(PrefProviderTest, AutoSubmitCertificateContentSetting) {
TestingProfile profile;
TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
GURL primary_url("https://www.example.com");
GURL secondary_url("https://www.sample.com");
PrefProvider provider(prefs, false);
EXPECT_EQ(CONTENT_SETTING_DEFAULT,
GetContentSetting(
&provider,
primary_url,
primary_url,
CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
std::string(),
false));
provider.SetWebsiteSetting(ContentSettingsPattern::FromURL(primary_url),
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
std::string(),
new base::FundamentalValue(CONTENT_SETTING_ALLOW));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
GetContentSetting(
&provider,
primary_url,
secondary_url,
CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
std::string(),
false));
provider.ShutdownOnUIThread();
}
// http://crosbug.com/17760
TEST(PrefProviderTest, Deadlock) {
TestingPrefServiceSyncable prefs;
PrefProvider::RegisterProfilePrefs(prefs.registry());
// Chain of events: a preference changes, |PrefProvider| notices it, and reads
// and writes the preference. When the preference is written, a notification
// is sent, and this used to happen when |PrefProvider| was still holding its
// lock.
PrefProvider provider(&prefs, false);
DeadlockCheckerObserver observer(&prefs, &provider);
{
DictionaryPrefUpdate update(&prefs,
prefs::kContentSettingsPatternPairs);
base::DictionaryValue* mutable_settings = update.Get();
mutable_settings->SetWithoutPathExpansion("www.example.com,*",
new base::DictionaryValue());
}
EXPECT_TRUE(observer.notification_received());
provider.ShutdownOnUIThread();
}
TEST(PrefProviderTest, LastUsage) {
TestingProfile testing_profile;
PrefProvider pref_content_settings_provider(testing_profile.GetPrefs(),
false);
base::SimpleTestClock* test_clock = new base::SimpleTestClock;
test_clock->SetNow(base::Time::Now());
pref_content_settings_provider.SetClockForTesting(
scoped_ptr<base::Clock>(test_clock));
GURL host("http://example.com/");
ContentSettingsPattern pattern =
ContentSettingsPattern::FromString("[*.]example.com");
base::Time no_usage = pref_content_settings_provider.GetLastUsage(
pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION);
EXPECT_EQ(no_usage.ToDoubleT(), 0);
pref_content_settings_provider.UpdateLastUsage(
pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION);
base::Time first = pref_content_settings_provider.GetLastUsage(
pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION);
test_clock->Advance(base::TimeDelta::FromSeconds(10));
pref_content_settings_provider.UpdateLastUsage(
pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION);
base::Time second = pref_content_settings_provider.GetLastUsage(
pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION);
base::TimeDelta delta = second - first;
EXPECT_EQ(delta.InSeconds(), 10);
pref_content_settings_provider.ShutdownOnUIThread();
}
} // namespace content_settings
|