| 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 "chrome/browser/extensions/api/declarative_content/chrome_content_rules
_registry.h" | 5 #include "chrome/browser/extensions/api/declarative_content/chrome_content_rules
_registry.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/test/values_test_util.h" | 10 #include "base/test/values_test_util.h" |
| 11 #include "chrome/browser/extensions/api/declarative_content/content_predicate.h" | 11 #include "chrome/browser/extensions/api/declarative_content/content_predicate.h" |
| 12 #include "chrome/browser/extensions/api/declarative_content/content_predicate_ev
aluator.h" | 12 #include "chrome/browser/extensions/api/declarative_content/content_predicate_ev
aluator.h" |
| 13 #include "chrome/browser/extensions/test_extension_environment.h" | 13 #include "chrome/browser/extensions/test_extension_environment.h" |
| 14 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
| 15 #include "content/public/browser/navigation_details.h" | 15 #include "content/public/browser/navigation_handle.h" |
| 16 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
| 17 #include "content/public/common/frame_navigate_params.h" | 17 #include "content/public/common/frame_navigate_params.h" |
| 18 #include "extensions/common/extension.h" | 18 #include "extensions/common/extension.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 namespace extensions { | 21 namespace extensions { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 class TestPredicateEvaluator; | 25 class TestPredicateEvaluator; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 const std::vector<const void*>& predicate_groups) override { | 70 const std::vector<const void*>& predicate_groups) override { |
| 71 RequestEvaluationIfSpecified(); | 71 RequestEvaluationIfSpecified(); |
| 72 } | 72 } |
| 73 | 73 |
| 74 void TrackForWebContents(content::WebContents* contents) override { | 74 void TrackForWebContents(content::WebContents* contents) override { |
| 75 RequestEvaluationIfSpecified(); | 75 RequestEvaluationIfSpecified(); |
| 76 } | 76 } |
| 77 | 77 |
| 78 void OnWebContentsNavigation( | 78 void OnWebContentsNavigation( |
| 79 content::WebContents* contents, | 79 content::WebContents* contents, |
| 80 const content::LoadCommittedDetails& details, | 80 content::NavigationHandle* navigation_handle) override { |
| 81 const content::FrameNavigateParams& params) override { | |
| 82 RequestEvaluationIfSpecified(); | 81 RequestEvaluationIfSpecified(); |
| 83 } | 82 } |
| 84 | 83 |
| 85 bool EvaluatePredicate(const ContentPredicate* predicate, | 84 bool EvaluatePredicate(const ContentPredicate* predicate, |
| 86 content::WebContents* tab) const override { | 85 content::WebContents* tab) const override { |
| 87 bool result = next_evaluation_result_; | 86 bool result = next_evaluation_result_; |
| 88 next_evaluation_result_ = false; | 87 next_evaluation_result_ = false; |
| 89 return result; | 88 return result; |
| 90 } | 89 } |
| 91 | 90 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 TestPredicateEvaluator* evaluator = nullptr; | 143 TestPredicateEvaluator* evaluator = nullptr; |
| 145 scoped_refptr<ChromeContentRulesRegistry> registry( | 144 scoped_refptr<ChromeContentRulesRegistry> registry( |
| 146 new ChromeContentRulesRegistry(env()->profile(), nullptr, | 145 new ChromeContentRulesRegistry(env()->profile(), nullptr, |
| 147 base::Bind(&CreateTestEvaluator, | 146 base::Bind(&CreateTestEvaluator, |
| 148 &evaluator))); | 147 &evaluator))); |
| 149 | 148 |
| 150 EXPECT_EQ(0u, registry->GetActiveRulesCountForTesting()); | 149 EXPECT_EQ(0u, registry->GetActiveRulesCountForTesting()); |
| 151 | 150 |
| 152 std::unique_ptr<content::WebContents> tab = env()->MakeTab(); | 151 std::unique_ptr<content::WebContents> tab = env()->MakeTab(); |
| 153 registry->MonitorWebContentsForRuleEvaluation(tab.get()); | 152 registry->MonitorWebContentsForRuleEvaluation(tab.get()); |
| 154 registry->DidNavigateMainFrame(tab.get(), content::LoadCommittedDetails(), | 153 std::unique_ptr<content::NavigationHandle> navigation_handle = |
| 155 content::FrameNavigateParams()); | 154 content::NavigationHandle::CreateNavigationHandleForTesting( |
| 155 GURL(), tab->GetMainFrame(), true); |
| 156 |
| 157 registry->DidFinishNavigation(tab.get(), navigation_handle.get()); |
| 156 EXPECT_EQ(0u, registry->GetActiveRulesCountForTesting()); | 158 EXPECT_EQ(0u, registry->GetActiveRulesCountForTesting()); |
| 157 | 159 |
| 158 // Add a rule. | 160 // Add a rule. |
| 159 linked_ptr<api::events::Rule> rule(new api::events::Rule); | 161 linked_ptr<api::events::Rule> rule(new api::events::Rule); |
| 160 api::events::Rule::Populate( | 162 api::events::Rule::Populate( |
| 161 *base::test::ParseJson( | 163 *base::test::ParseJson( |
| 162 "{\n" | 164 "{\n" |
| 163 " \"id\": \"rule1\",\n" | 165 " \"id\": \"rule1\",\n" |
| 164 " \"priority\": 100,\n" | 166 " \"priority\": 100,\n" |
| 165 " \"conditions\": [\n" | 167 " \"conditions\": [\n" |
| 166 " {\n" | 168 " {\n" |
| 167 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n" | 169 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n" |
| 168 " \"test_predicate\": []\n" | 170 " \"test_predicate\": []\n" |
| 169 " }],\n" | 171 " }],\n" |
| 170 " \"actions\": [\n" | 172 " \"actions\": [\n" |
| 171 " { \"instanceType\": \"declarativeContent.ShowPageAction\" }\n" | 173 " { \"instanceType\": \"declarativeContent.ShowPageAction\" }\n" |
| 172 " ]\n" | 174 " ]\n" |
| 173 "}"), | 175 "}"), |
| 174 rule.get()); | 176 rule.get()); |
| 175 std::vector<linked_ptr<api::events::Rule>> rules; | 177 std::vector<linked_ptr<api::events::Rule>> rules; |
| 176 rules.push_back(rule); | 178 rules.push_back(rule); |
| 177 | 179 |
| 178 const Extension* extension = env()->MakeExtension(*base::test::ParseJson( | 180 const Extension* extension = env()->MakeExtension(*base::test::ParseJson( |
| 179 "{\"page_action\": {}}")); | 181 "{\"page_action\": {}}")); |
| 180 registry->AddRulesImpl(extension->id(), rules); | 182 registry->AddRulesImpl(extension->id(), rules); |
| 181 | 183 |
| 182 registry->DidNavigateMainFrame(tab.get(), content::LoadCommittedDetails(), | 184 registry->DidFinishNavigation(tab.get(), navigation_handle.get()); |
| 183 content::FrameNavigateParams()); | |
| 184 EXPECT_EQ(0u, registry->GetActiveRulesCountForTesting()); | 185 EXPECT_EQ(0u, registry->GetActiveRulesCountForTesting()); |
| 185 | 186 |
| 186 evaluator->RequestImmediateEvaluation(tab.get(), true); | 187 evaluator->RequestImmediateEvaluation(tab.get(), true); |
| 187 EXPECT_EQ(1u, registry->GetActiveRulesCountForTesting()); | 188 EXPECT_EQ(1u, registry->GetActiveRulesCountForTesting()); |
| 188 | 189 |
| 189 // Closing the tab should erase its entry from active_rules_. | 190 // Closing the tab should erase its entry from active_rules_. |
| 191 navigation_handle.reset(); |
| 190 tab.reset(); | 192 tab.reset(); |
| 191 EXPECT_EQ(0u, registry->GetActiveRulesCountForTesting()); | 193 EXPECT_EQ(0u, registry->GetActiveRulesCountForTesting()); |
| 192 | 194 |
| 193 tab = env()->MakeTab(); | 195 tab = env()->MakeTab(); |
| 196 navigation_handle = |
| 197 content::NavigationHandle::CreateNavigationHandleForTesting( |
| 198 GURL(), tab->GetMainFrame(), true); |
| 194 registry->MonitorWebContentsForRuleEvaluation(tab.get()); | 199 registry->MonitorWebContentsForRuleEvaluation(tab.get()); |
| 195 evaluator->RequestImmediateEvaluation(tab.get(), true); | 200 evaluator->RequestImmediateEvaluation(tab.get(), true); |
| 196 EXPECT_EQ(1u, registry->GetActiveRulesCountForTesting()); | 201 EXPECT_EQ(1u, registry->GetActiveRulesCountForTesting()); |
| 197 | 202 |
| 198 evaluator->RequestEvaluationOnNextOperation(tab.get(), false); | 203 evaluator->RequestEvaluationOnNextOperation(tab.get(), false); |
| 199 registry->DidNavigateMainFrame(tab.get(), content::LoadCommittedDetails(), | 204 registry->DidFinishNavigation(tab.get(), navigation_handle.get()); |
| 200 content::FrameNavigateParams()); | |
| 201 EXPECT_EQ(0u, registry->GetActiveRulesCountForTesting()); | 205 EXPECT_EQ(0u, registry->GetActiveRulesCountForTesting()); |
| 202 } | 206 } |
| 203 | 207 |
| 204 } // namespace extensions | 208 } // namespace extensions |
| OLD | NEW |