| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "extensions/browser/api/declarative/rules_registry.h" | 5 #include "extensions/browser/api/declarative/rules_registry.h" |
| 6 | 6 |
| 7 // Here we test the TestRulesRegistry which is the simplest possible | 7 // Here we test the TestRulesRegistry which is the simplest possible |
| 8 // implementation of RulesRegistryWithCache as a proxy for | 8 // implementation of RulesRegistryWithCache as a proxy for |
| 9 // RulesRegistryWithCache. | 9 // RulesRegistryWithCache. |
| 10 | 10 |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "chrome/browser/extensions/extension_service.h" | 13 #include "chrome/browser/extensions/extension_service.h" |
| 14 #include "chrome/browser/extensions/test_extension_environment.h" | 14 #include "chrome/browser/extensions/test_extension_environment.h" |
| 15 #include "chrome/browser/extensions/test_extension_system.h" | 15 #include "chrome/browser/extensions/test_extension_system.h" |
| 16 #include "chrome/common/extensions/extension_test_util.h" | 16 #include "chrome/common/extensions/extension_test_util.h" |
| 17 #include "chrome/common/extensions/features/feature_channel.h" | 17 #include "chrome/common/extensions/features/feature_channel.h" |
| 18 #include "chrome/test/base/testing_profile.h" | 18 #include "chrome/test/base/testing_profile.h" |
| 19 #include "content/public/test/test_browser_thread_bundle.h" | 19 #include "content/public/test/test_browser_thread_bundle.h" |
| 20 #include "extensions/browser/api/declarative/rules_cache_delegate.h" | 20 #include "extensions/browser/api/declarative/rules_cache_delegate.h" |
| 21 #include "extensions/browser/api/declarative/rules_registry_service.h" |
| 21 #include "extensions/browser/api/declarative/test_rules_registry.h" | 22 #include "extensions/browser/api/declarative/test_rules_registry.h" |
| 22 #include "extensions/browser/extension_prefs.h" | 23 #include "extensions/browser/extension_prefs.h" |
| 23 #include "extensions/browser/extension_registry.h" | 24 #include "extensions/browser/extension_registry.h" |
| 24 #include "extensions/browser/value_store/testing_value_store.h" | 25 #include "extensions/browser/value_store/testing_value_store.h" |
| 25 #include "extensions/common/extension.h" | 26 #include "extensions/common/extension.h" |
| 26 #include "extensions/common/manifest_constants.h" | 27 #include "extensions/common/manifest_constants.h" |
| 27 #include "extensions/common/permissions/permissions_data.h" | 28 #include "extensions/common/permissions/permissions_data.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
| 29 | 30 |
| 30 using extension_test_util::LoadManifestUnchecked; | 31 using extension_test_util::LoadManifestUnchecked; |
| 31 | 32 |
| 32 namespace { | 33 namespace { |
| 33 const char kRuleId[] = "rule"; | 34 const char kRuleId[] = "rule"; |
| 34 const char kRule2Id[] = "rule2"; | 35 const char kRule2Id[] = "rule2"; |
| 35 } | 36 } |
| 36 | 37 |
| 37 namespace extensions { | 38 namespace extensions { |
| 39 const int kRulesRegistryID = RulesRegistryService::kDefaultRulesRegistryID; |
| 38 | 40 |
| 39 class RulesRegistryWithCacheTest : public testing::Test { | 41 class RulesRegistryWithCacheTest : public testing::Test { |
| 40 public: | 42 public: |
| 41 RulesRegistryWithCacheTest() | 43 RulesRegistryWithCacheTest() |
| 42 : cache_delegate_(/*log_storage_init_delay=*/false), | 44 : cache_delegate_(/*log_storage_init_delay=*/false), |
| 43 registry_(new TestRulesRegistry(profile(), | 45 registry_(new TestRulesRegistry(profile(), |
| 44 /*event_name=*/"", | 46 /*event_name=*/"", |
| 45 content::BrowserThread::UI, | 47 content::BrowserThread::UI, |
| 46 &cache_delegate_, | 48 &cache_delegate_, |
| 47 RulesRegistry::WebViewKey(0, 0))) {} | 49 kRulesRegistryID)) {} |
| 48 | 50 |
| 49 void SetUp() override { | 51 void SetUp() override { |
| 50 env_.GetExtensionPrefs(); // Force creation before adding extensions. | 52 env_.GetExtensionPrefs(); // Force creation before adding extensions. |
| 51 // Note that env_.MakeExtension below also forces the creation of | 53 // Note that env_.MakeExtension below also forces the creation of |
| 52 // ExtensionService. | 54 // ExtensionService. |
| 53 | 55 |
| 54 base::DictionaryValue manifest_extra; | 56 base::DictionaryValue manifest_extra; |
| 55 std::string key; | 57 std::string key; |
| 56 CHECK(Extension::ProducePEM("test extension 1", &key)); | 58 CHECK(Extension::ProducePEM("test extension 1", &key)); |
| 57 manifest_extra.SetString(manifest_keys::kPublicKey, key); | 59 manifest_extra.SetString(manifest_keys::kPublicKey, key); |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 TEST_F(RulesRegistryWithCacheTest, DeclarativeRulesStored) { | 228 TEST_F(RulesRegistryWithCacheTest, DeclarativeRulesStored) { |
| 227 ExtensionPrefs* extension_prefs = env_.GetExtensionPrefs(); | 229 ExtensionPrefs* extension_prefs = env_.GetExtensionPrefs(); |
| 228 // The value store is first created during GetExtensionService. | 230 // The value store is first created during GetExtensionService. |
| 229 TestingValueStore* store = env_.GetExtensionSystem()->value_store(); | 231 TestingValueStore* store = env_.GetExtensionSystem()->value_store(); |
| 230 | 232 |
| 231 const std::string event_name("testEvent"); | 233 const std::string event_name("testEvent"); |
| 232 const std::string rules_stored_key( | 234 const std::string rules_stored_key( |
| 233 RulesCacheDelegate::GetRulesStoredKey( | 235 RulesCacheDelegate::GetRulesStoredKey( |
| 234 event_name, profile()->IsOffTheRecord())); | 236 event_name, profile()->IsOffTheRecord())); |
| 235 scoped_ptr<RulesCacheDelegate> cache_delegate(new RulesCacheDelegate(false)); | 237 scoped_ptr<RulesCacheDelegate> cache_delegate(new RulesCacheDelegate(false)); |
| 236 scoped_refptr<RulesRegistry> registry(new TestRulesRegistry( | 238 scoped_refptr<RulesRegistry> registry( |
| 237 profile(), event_name, content::BrowserThread::UI, | 239 new TestRulesRegistry(profile(), event_name, content::BrowserThread::UI, |
| 238 cache_delegate.get(), | 240 cache_delegate.get(), kRulesRegistryID)); |
| 239 RulesRegistry::WebViewKey(0, 0))); | |
| 240 | 241 |
| 241 // 1. Test the handling of preferences. | 242 // 1. Test the handling of preferences. |
| 242 // Default value is always true. | 243 // Default value is always true. |
| 243 EXPECT_TRUE(cache_delegate->GetDeclarativeRulesStored(extension1_->id())); | 244 EXPECT_TRUE(cache_delegate->GetDeclarativeRulesStored(extension1_->id())); |
| 244 | 245 |
| 245 extension_prefs->UpdateExtensionPref( | 246 extension_prefs->UpdateExtensionPref( |
| 246 extension1_->id(), rules_stored_key, new base::FundamentalValue(false)); | 247 extension1_->id(), rules_stored_key, new base::FundamentalValue(false)); |
| 247 EXPECT_FALSE(cache_delegate->GetDeclarativeRulesStored(extension1_->id())); | 248 EXPECT_FALSE(cache_delegate->GetDeclarativeRulesStored(extension1_->id())); |
| 248 | 249 |
| 249 extension_prefs->UpdateExtensionPref( | 250 extension_prefs->UpdateExtensionPref( |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 | 297 |
| 297 const std::string event_name1("testEvent1"); | 298 const std::string event_name1("testEvent1"); |
| 298 const std::string event_name2("testEvent2"); | 299 const std::string event_name2("testEvent2"); |
| 299 const std::string rules_stored_key1( | 300 const std::string rules_stored_key1( |
| 300 RulesCacheDelegate::GetRulesStoredKey( | 301 RulesCacheDelegate::GetRulesStoredKey( |
| 301 event_name1, profile()->IsOffTheRecord())); | 302 event_name1, profile()->IsOffTheRecord())); |
| 302 const std::string rules_stored_key2( | 303 const std::string rules_stored_key2( |
| 303 RulesCacheDelegate::GetRulesStoredKey( | 304 RulesCacheDelegate::GetRulesStoredKey( |
| 304 event_name2, profile()->IsOffTheRecord())); | 305 event_name2, profile()->IsOffTheRecord())); |
| 305 scoped_ptr<RulesCacheDelegate> cache_delegate1(new RulesCacheDelegate(false)); | 306 scoped_ptr<RulesCacheDelegate> cache_delegate1(new RulesCacheDelegate(false)); |
| 306 scoped_refptr<RulesRegistry> registry1(new TestRulesRegistry( | 307 scoped_refptr<RulesRegistry> registry1( |
| 307 profile(), event_name1, content::BrowserThread::UI, | 308 new TestRulesRegistry(profile(), event_name1, content::BrowserThread::UI, |
| 308 cache_delegate1.get(), | 309 cache_delegate1.get(), kRulesRegistryID)); |
| 309 RulesRegistry::WebViewKey(0, 0))); | |
| 310 | 310 |
| 311 scoped_ptr<RulesCacheDelegate> cache_delegate2(new RulesCacheDelegate(false)); | 311 scoped_ptr<RulesCacheDelegate> cache_delegate2(new RulesCacheDelegate(false)); |
| 312 scoped_refptr<RulesRegistry> registry2(new TestRulesRegistry( | 312 scoped_refptr<RulesRegistry> registry2( |
| 313 profile(), event_name2, content::BrowserThread::UI, | 313 new TestRulesRegistry(profile(), event_name2, content::BrowserThread::UI, |
| 314 cache_delegate2.get(), | 314 cache_delegate2.get(), kRulesRegistryID)); |
| 315 RulesRegistry::WebViewKey(0, 0))); | |
| 316 | 315 |
| 317 // Checkt the correct default values. | 316 // Checkt the correct default values. |
| 318 EXPECT_TRUE(cache_delegate1->GetDeclarativeRulesStored(extension1_->id())); | 317 EXPECT_TRUE(cache_delegate1->GetDeclarativeRulesStored(extension1_->id())); |
| 319 EXPECT_TRUE(cache_delegate2->GetDeclarativeRulesStored(extension1_->id())); | 318 EXPECT_TRUE(cache_delegate2->GetDeclarativeRulesStored(extension1_->id())); |
| 320 | 319 |
| 321 // Update the flag for the first registry. | 320 // Update the flag for the first registry. |
| 322 extension_prefs->UpdateExtensionPref( | 321 extension_prefs->UpdateExtensionPref( |
| 323 extension1_->id(), rules_stored_key1, new base::FundamentalValue(false)); | 322 extension1_->id(), rules_stored_key1, new base::FundamentalValue(false)); |
| 324 EXPECT_FALSE(cache_delegate1->GetDeclarativeRulesStored(extension1_->id())); | 323 EXPECT_FALSE(cache_delegate1->GetDeclarativeRulesStored(extension1_->id())); |
| 325 EXPECT_TRUE(cache_delegate2->GetDeclarativeRulesStored(extension1_->id())); | 324 EXPECT_TRUE(cache_delegate2->GetDeclarativeRulesStored(extension1_->id())); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 348 extension_service->AddExtension(extension.get()); | 347 extension_service->AddExtension(extension.get()); |
| 349 EXPECT_TRUE(extensions::ExtensionRegistry::Get(env_.profile()) | 348 EXPECT_TRUE(extensions::ExtensionRegistry::Get(env_.profile()) |
| 350 ->enabled_extensions() | 349 ->enabled_extensions() |
| 351 .Contains(extension->id())); | 350 .Contains(extension->id())); |
| 352 EXPECT_TRUE(extension->permissions_data()->HasAPIPermission( | 351 EXPECT_TRUE(extension->permissions_data()->HasAPIPermission( |
| 353 APIPermission::kDeclarativeWebRequest)); | 352 APIPermission::kDeclarativeWebRequest)); |
| 354 env_.GetExtensionSystem()->SetReady(); | 353 env_.GetExtensionSystem()->SetReady(); |
| 355 | 354 |
| 356 // 2. First run, adding a rule for the extension. | 355 // 2. First run, adding a rule for the extension. |
| 357 scoped_ptr<RulesCacheDelegate> cache_delegate(new RulesCacheDelegate(false)); | 356 scoped_ptr<RulesCacheDelegate> cache_delegate(new RulesCacheDelegate(false)); |
| 358 scoped_refptr<TestRulesRegistry> registry(new TestRulesRegistry( | 357 scoped_refptr<TestRulesRegistry> registry( |
| 359 profile(), | 358 new TestRulesRegistry(profile(), "testEvent", content::BrowserThread::UI, |
| 360 "testEvent", | 359 cache_delegate.get(), kRulesRegistryID)); |
| 361 content::BrowserThread::UI, | |
| 362 cache_delegate.get(), | |
| 363 RulesRegistry::WebViewKey(0, 0))); | |
| 364 | 360 |
| 365 AddRule(extension1_->id(), kRuleId, registry.get()); | 361 AddRule(extension1_->id(), kRuleId, registry.get()); |
| 366 base::RunLoop().RunUntilIdle(); // Posted tasks store the added rule. | 362 base::RunLoop().RunUntilIdle(); // Posted tasks store the added rule. |
| 367 EXPECT_EQ(1, GetNumberOfRules(extension1_->id(), registry.get())); | 363 EXPECT_EQ(1, GetNumberOfRules(extension1_->id(), registry.get())); |
| 368 | 364 |
| 369 // 3. Restart the TestRulesRegistry and see the rule still there. | 365 // 3. Restart the TestRulesRegistry and see the rule still there. |
| 370 cache_delegate.reset(new RulesCacheDelegate(false)); | 366 cache_delegate.reset(new RulesCacheDelegate(false)); |
| 371 registry = new TestRulesRegistry( | 367 registry = |
| 372 profile(), | 368 new TestRulesRegistry(profile(), "testEvent", content::BrowserThread::UI, |
| 373 "testEvent", | 369 cache_delegate.get(), kRulesRegistryID); |
| 374 content::BrowserThread::UI, | |
| 375 cache_delegate.get(), | |
| 376 RulesRegistry::WebViewKey(0, 0)); | |
| 377 | 370 |
| 378 base::RunLoop().RunUntilIdle(); // Posted tasks retrieve the stored rule. | 371 base::RunLoop().RunUntilIdle(); // Posted tasks retrieve the stored rule. |
| 379 EXPECT_EQ(1, GetNumberOfRules(extension1_->id(), registry.get())); | 372 EXPECT_EQ(1, GetNumberOfRules(extension1_->id(), registry.get())); |
| 380 } | 373 } |
| 381 | 374 |
| 382 TEST_F(RulesRegistryWithCacheTest, ConcurrentStoringOfRules) { | 375 TEST_F(RulesRegistryWithCacheTest, ConcurrentStoringOfRules) { |
| 383 // When an extension updates its rules, the new set of rules is stored to disk | 376 // When an extension updates its rules, the new set of rules is stored to disk |
| 384 // with some delay. While it is acceptable for a quick series of updates for a | 377 // with some delay. While it is acceptable for a quick series of updates for a |
| 385 // single extension to only write the last one, we should never forget to | 378 // single extension to only write the last one, we should never forget to |
| 386 // write a rules update for extension A, just because it is immediately | 379 // write a rules update for extension A, just because it is immediately |
| 387 // followed by a rules update for extension B. | 380 // followed by a rules update for extension B. |
| 388 extensions::TestExtensionSystem* system = env_.GetExtensionSystem(); | 381 extensions::TestExtensionSystem* system = env_.GetExtensionSystem(); |
| 389 TestingValueStore* store = system->value_store(); | 382 TestingValueStore* store = system->value_store(); |
| 390 | 383 |
| 391 int write_count = store->write_count(); | 384 int write_count = store->write_count(); |
| 392 EXPECT_EQ("", AddRule(extension1_->id(), kRuleId)); | 385 EXPECT_EQ("", AddRule(extension1_->id(), kRuleId)); |
| 393 EXPECT_EQ("", AddRule(extension2_->id(), kRule2Id)); | 386 EXPECT_EQ("", AddRule(extension2_->id(), kRule2Id)); |
| 394 env_.GetExtensionSystem()->SetReady(); | 387 env_.GetExtensionSystem()->SetReady(); |
| 395 base::RunLoop().RunUntilIdle(); | 388 base::RunLoop().RunUntilIdle(); |
| 396 EXPECT_EQ(write_count + 2, store->write_count()); | 389 EXPECT_EQ(write_count + 2, store->write_count()); |
| 397 } | 390 } |
| 398 | 391 |
| 399 } // namespace extensions | 392 } // namespace extensions |
| OLD | NEW |