| 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_service.h" | 5 #include "extensions/browser/api/declarative/rules_registry_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "chrome/test/base/testing_profile.h" | 9 #include "chrome/test/base/testing_profile.h" |
| 10 #include "content/public/test/test_browser_thread.h" | 10 #include "content/public/test/test_browser_thread.h" |
| 11 #include "extensions/browser/api/declarative/test_rules_registry.h" | 11 #include "extensions/browser/api/declarative/test_rules_registry.h" |
| 12 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h" | 12 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h" |
| 13 #include "extensions/browser/guest_view/web_view/web_view_guest.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 const char kExtensionId[] = "foo"; | 17 const char kExtensionId[] = "foo"; |
| 17 | 18 |
| 18 void InsertRule(scoped_refptr<extensions::RulesRegistry> registry, | 19 void InsertRule(scoped_refptr<extensions::RulesRegistry> registry, |
| 19 const std::string& id) { | 20 const std::string& id) { |
| 20 std::vector<linked_ptr<extensions::RulesRegistry::Rule> > add_rules; | 21 std::vector<linked_ptr<extensions::RulesRegistry::Rule> > add_rules; |
| 21 add_rules.push_back(make_linked_ptr(new extensions::RulesRegistry::Rule)); | 22 add_rules.push_back(make_linked_ptr(new extensions::RulesRegistry::Rule)); |
| 22 add_rules[0]->id.reset(new std::string(id)); | 23 add_rules[0]->id.reset(new std::string(id)); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 48 message_loop_.RunUntilIdle(); | 49 message_loop_.RunUntilIdle(); |
| 49 } | 50 } |
| 50 | 51 |
| 51 protected: | 52 protected: |
| 52 base::MessageLoop message_loop_; | 53 base::MessageLoop message_loop_; |
| 53 content::TestBrowserThread ui_; | 54 content::TestBrowserThread ui_; |
| 54 content::TestBrowserThread io_; | 55 content::TestBrowserThread io_; |
| 55 }; | 56 }; |
| 56 | 57 |
| 57 TEST_F(RulesRegistryServiceTest, TestConstructionAndMultiThreading) { | 58 TEST_F(RulesRegistryServiceTest, TestConstructionAndMultiThreading) { |
| 58 const RulesRegistry::WebViewKey key(0, 0); | 59 RulesRegistryService registry_service(NULL); |
| 60 |
| 61 int key = RulesRegistryService::kDefultRulesRegistryID; |
| 59 TestRulesRegistry* ui_registry = | 62 TestRulesRegistry* ui_registry = |
| 60 new TestRulesRegistry(content::BrowserThread::UI, "ui", key); | 63 new TestRulesRegistry(content::BrowserThread::UI, "ui", key); |
| 61 | 64 |
| 62 TestRulesRegistry* io_registry = | 65 TestRulesRegistry* io_registry = |
| 63 new TestRulesRegistry(content::BrowserThread::IO, "io", key); | 66 new TestRulesRegistry(content::BrowserThread::IO, "io", key); |
| 64 | 67 |
| 65 // Test registration. | 68 // Test registration. |
| 66 | 69 |
| 67 RulesRegistryService registry_service(NULL); | |
| 68 registry_service.RegisterRulesRegistry(make_scoped_refptr(ui_registry)); | 70 registry_service.RegisterRulesRegistry(make_scoped_refptr(ui_registry)); |
| 69 registry_service.RegisterRulesRegistry(make_scoped_refptr(io_registry)); | 71 registry_service.RegisterRulesRegistry(make_scoped_refptr(io_registry)); |
| 70 | 72 |
| 71 EXPECT_TRUE(registry_service.GetRulesRegistry(key, "ui").get()); | 73 EXPECT_TRUE(registry_service.GetRulesRegistry(key, false, "ui").get()); |
| 72 EXPECT_TRUE(registry_service.GetRulesRegistry(key, "io").get()); | 74 EXPECT_TRUE(registry_service.GetRulesRegistry(key, false, "io").get()); |
| 73 EXPECT_FALSE(registry_service.GetRulesRegistry(key, "foo").get()); | 75 EXPECT_FALSE(registry_service.GetRulesRegistry(key, false, "foo").get()); |
| 74 | 76 |
| 75 content::BrowserThread::PostTask( | 77 content::BrowserThread::PostTask( |
| 76 content::BrowserThread::UI, FROM_HERE, | 78 content::BrowserThread::UI, FROM_HERE, |
| 77 base::Bind(&InsertRule, registry_service.GetRulesRegistry(key, "ui"), | 79 base::Bind(&InsertRule, |
| 80 registry_service.GetRulesRegistry(key, false, "ui"), |
| 78 "ui_task")); | 81 "ui_task")); |
| 79 | 82 |
| 80 content::BrowserThread::PostTask( | 83 content::BrowserThread::PostTask( |
| 81 content::BrowserThread::IO, FROM_HERE, | 84 content::BrowserThread::IO, FROM_HERE, |
| 82 base::Bind(&InsertRule, registry_service.GetRulesRegistry(key, "io"), | 85 base::Bind(&InsertRule, |
| 83 "io_task")); | 86 registry_service.GetRulesRegistry(key, false, "io"), |
| 87 "io_task")); |
| 84 | 88 |
| 85 content::BrowserThread::PostTask( | 89 content::BrowserThread::PostTask( |
| 86 content::BrowserThread::UI, FROM_HERE, | 90 content::BrowserThread::UI, FROM_HERE, |
| 87 base::Bind(&VerifyNumberOfRules, | 91 base::Bind(&VerifyNumberOfRules, |
| 88 registry_service.GetRulesRegistry(key, "ui"), 1)); | 92 registry_service.GetRulesRegistry(key, false, "ui"), 1)); |
| 89 | 93 |
| 90 content::BrowserThread::PostTask( | 94 content::BrowserThread::PostTask( |
| 91 content::BrowserThread::IO, FROM_HERE, | 95 content::BrowserThread::IO, FROM_HERE, |
| 92 base::Bind(&VerifyNumberOfRules, | 96 base::Bind(&VerifyNumberOfRules, |
| 93 registry_service.GetRulesRegistry(key, "io"), 1)); | 97 registry_service.GetRulesRegistry(key, false, "io"), 1)); |
| 94 | 98 |
| 95 message_loop_.RunUntilIdle(); | 99 message_loop_.RunUntilIdle(); |
| 96 | 100 |
| 97 // Test extension uninstalling. | 101 // Test extension uninstalling. |
| 98 | 102 |
| 99 registry_service.SimulateExtensionUninstalled(kExtensionId); | 103 registry_service.SimulateExtensionUninstalled(kExtensionId); |
| 100 | 104 |
| 101 content::BrowserThread::PostTask( | 105 content::BrowserThread::PostTask( |
| 102 content::BrowserThread::UI, FROM_HERE, | 106 content::BrowserThread::UI, FROM_HERE, |
| 103 base::Bind(&VerifyNumberOfRules, | 107 base::Bind(&VerifyNumberOfRules, |
| 104 registry_service.GetRulesRegistry(key, "ui"), 0)); | 108 registry_service.GetRulesRegistry(key, false, "ui"), 0)); |
| 105 | 109 |
| 106 content::BrowserThread::PostTask( | 110 content::BrowserThread::PostTask( |
| 107 content::BrowserThread::IO, FROM_HERE, | 111 content::BrowserThread::IO, FROM_HERE, |
| 108 base::Bind(&VerifyNumberOfRules, | 112 base::Bind(&VerifyNumberOfRules, |
| 109 registry_service.GetRulesRegistry(key, "io"), 0)); | 113 registry_service.GetRulesRegistry(key, false, "io"), 0)); |
| 110 | 114 |
| 111 message_loop_.RunUntilIdle(); | 115 message_loop_.RunUntilIdle(); |
| 112 } | 116 } |
| 113 | 117 |
| 114 // This test verifies that removing rules registries by process ID works as | 118 // This test verifies that removing rules registries by process ID works as |
| 115 // intended. This test ensures that removing registries associated with one | 119 // intended. This test ensures that removing registries associated with one |
| 116 // Webview embedder process does not remove registries associated with the | 120 // Webview embedder process does not remove registries associated with the |
| 117 // other. | 121 // other. |
| 118 TEST_F(RulesRegistryServiceTest, TestWebViewKey) { | 122 TEST_F(RulesRegistryServiceTest, TestWebViewRulesRegistryIDsAreGone) { |
| 119 const int kEmbedderProcessID1 = 1; | 123 const int kEmbedderProcessID1 = 1; |
| 120 const int kEmbedderProcessID2 = 2; | 124 const int kEmbedderProcessID2 = 2; |
| 121 const int kWebViewInstanceID = 1; | 125 const int kWebViewInstanceID = 1; |
| 122 | 126 |
| 123 const RulesRegistry::WebViewKey key1(kEmbedderProcessID1, kWebViewInstanceID); | 127 RulesRegistryService registry_service(NULL); |
| 124 const RulesRegistry::WebViewKey key2(kEmbedderProcessID2, kWebViewInstanceID); | 128 int rules_registry_id1 = 0; |
| 129 WebViewGuest::GenerateRulesRegistryID(kEmbedderProcessID1, kWebViewInstanceID, |
| 130 ®istry_service, &rules_registry_id1); |
| 131 int rules_registry_id2 = 0; |
| 132 WebViewGuest::GenerateRulesRegistryID(kEmbedderProcessID2, kWebViewInstanceID, |
| 133 ®istry_service, &rules_registry_id2); |
| 125 | 134 |
| 126 TestRulesRegistry* ui_registry_key1 = | 135 TestRulesRegistry* ui_registry_key1 = new TestRulesRegistry( |
| 127 new TestRulesRegistry(content::BrowserThread::UI, "ui", key1); | 136 content::BrowserThread::UI, "ui", rules_registry_id1); |
| 128 TestRulesRegistry* ui_registry_key2 = | 137 TestRulesRegistry* ui_registry_key2 = new TestRulesRegistry( |
| 129 new TestRulesRegistry(content::BrowserThread::UI, "ui", key2); | 138 content::BrowserThread::UI, "ui", rules_registry_id2); |
| 130 | 139 |
| 131 RulesRegistryService registry_service(NULL); | |
| 132 registry_service.RegisterRulesRegistry(make_scoped_refptr(ui_registry_key1)); | 140 registry_service.RegisterRulesRegistry(make_scoped_refptr(ui_registry_key1)); |
| 133 registry_service.RegisterRulesRegistry(make_scoped_refptr(ui_registry_key2)); | 141 registry_service.RegisterRulesRegistry(make_scoped_refptr(ui_registry_key2)); |
| 134 | 142 |
| 135 content::BrowserThread::PostTask( | 143 content::BrowserThread::PostTask( |
| 136 content::BrowserThread::UI, FROM_HERE, | 144 content::BrowserThread::UI, FROM_HERE, |
| 137 base::Bind(&InsertRule, registry_service.GetRulesRegistry(key1, "ui"), | 145 base::Bind(&InsertRule, registry_service.GetRulesRegistry( |
| 146 rules_registry_id1, true, "ui"), |
| 138 "ui_task")); | 147 "ui_task")); |
| 139 content::BrowserThread::PostTask( | 148 content::BrowserThread::PostTask( |
| 140 content::BrowserThread::UI, FROM_HERE, | 149 content::BrowserThread::UI, FROM_HERE, |
| 141 base::Bind(&InsertRule, registry_service.GetRulesRegistry(key2, "ui"), | 150 base::Bind(&InsertRule, registry_service.GetRulesRegistry( |
| 151 rules_registry_id2, true, "ui"), |
| 142 "ui_task")); | 152 "ui_task")); |
| 143 message_loop_.RunUntilIdle(); | 153 message_loop_.RunUntilIdle(); |
| 144 | 154 |
| 145 registry_service.RemoveWebViewRulesRegistries(kEmbedderProcessID1); | 155 registry_service.RemoveWebViewRulesRegistries(kEmbedderProcessID1); |
| 146 EXPECT_FALSE(registry_service.GetRulesRegistry(key1, "ui").get()); | 156 EXPECT_FALSE( |
| 147 EXPECT_TRUE(registry_service.GetRulesRegistry(key2, "ui").get()); | 157 registry_service.GetRulesRegistry(rules_registry_id1, true, "ui").get()); |
| 158 EXPECT_TRUE( |
| 159 registry_service.GetRulesRegistry(rules_registry_id2, true, "ui").get()); |
| 148 } | 160 } |
| 149 | 161 |
| 150 TEST_F(RulesRegistryServiceTest, TestWebViewWebRequestRegistryHasNoCache) { | 162 TEST_F(RulesRegistryServiceTest, TestWebViewWebRequestRegistryHasNoCache) { |
| 151 const int kEmbedderProcessID = 1; | 163 const int kEmbedderProcessID = 1; |
| 152 const int kWebViewInstanceID = 1; | 164 const int kWebViewInstanceID = 1; |
| 153 const RulesRegistry::WebViewKey key(kEmbedderProcessID, kWebViewInstanceID); | |
| 154 TestingProfile profile; | 165 TestingProfile profile; |
| 155 RulesRegistryService registry_service(&profile); | 166 RulesRegistryService registry_service(&profile); |
| 167 int rules_registry_id = 0; |
| 168 WebViewGuest::GenerateRulesRegistryID(kEmbedderProcessID, kWebViewInstanceID, |
| 169 ®istry_service, &rules_registry_id); |
| 170 |
| 156 RulesRegistry* registry = | 171 RulesRegistry* registry = |
| 157 registry_service.GetRulesRegistry( | 172 registry_service.GetRulesRegistry( |
| 158 key, | 173 rules_registry_id, true, |
| 159 declarative_webrequest_constants::kOnRequest).get(); | 174 declarative_webrequest_constants::kOnRequest).get(); |
| 160 EXPECT_TRUE(registry); | 175 EXPECT_TRUE(registry); |
| 161 EXPECT_FALSE(registry->rules_cache_delegate_for_testing()); | 176 EXPECT_FALSE(registry->rules_cache_delegate_for_testing()); |
| 162 } | 177 } |
| 163 | 178 |
| 164 } // namespace extensions | 179 } // namespace extensions |
| OLD | NEW |