OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 "components/subresource_filter/content/browser/content_subresource_filt
er_throttle_manager.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/callback_forward.h" |
| 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/run_loop.h" |
| 13 #include "base/test/test_simple_task_runner.h" |
| 14 #include "components/subresource_filter/content/browser/async_document_subresour
ce_filter.h" |
| 15 #include "components/subresource_filter/content/browser/async_document_subresour
ce_filter_test_utils.h" |
| 16 #include "components/subresource_filter/core/common/activation_level.h" |
| 17 #include "components/subresource_filter/core/common/activation_state.h" |
| 18 #include "components/subresource_filter/core/common/proto/rules.pb.h" |
| 19 #include "components/subresource_filter/core/common/test_ruleset_creator.h" |
| 20 #include "components/subresource_filter/core/common/test_ruleset_utils.h" |
| 21 #include "content/public/browser/navigation_handle.h" |
| 22 #include "content/public/browser/navigation_throttle.h" |
| 23 #include "content/public/common/referrer.h" |
| 24 #include "content/public/test/test_renderer_host.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 |
| 27 namespace subresource_filter { |
| 28 |
| 29 class MockPageStateActivationThrottle : public content::NavigationThrottle { |
| 30 public: |
| 31 MockPageStateActivationThrottle( |
| 32 content::NavigationHandle* navigation_handle, |
| 33 ContentSubresourceFilterThrottleManager* throttle_manager) |
| 34 : content::NavigationThrottle(navigation_handle), |
| 35 throttle_manager_(throttle_manager) {} |
| 36 ~MockPageStateActivationThrottle() override {} |
| 37 |
| 38 // content::NavigationThrottle: |
| 39 content::NavigationThrottle::ThrottleCheckResult WillProcessResponse() |
| 40 override { |
| 41 if (navigation_handle()->GetURL() == url_to_activate_) { |
| 42 throttle_manager_->OnPageStateActivationComputed( |
| 43 navigation_handle(), ActivationState(ActivationLevel::ENABLED)); |
| 44 } |
| 45 return content::NavigationThrottle::PROCEED; |
| 46 } |
| 47 |
| 48 private: |
| 49 const GURL url_to_activate_; |
| 50 ContentSubresourceFilterThrottleManager* throttle_manager_; |
| 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(MockPageStateActivationThrottle); |
| 53 }; |
| 54 |
| 55 class ContentSubresourceFilterThrottleManagerTest |
| 56 : public content::RenderViewHostTestHarness, |
| 57 public content::WebContentsObserver, |
| 58 public ContentSubresourceFilterThrottleManager::Delegate { |
| 59 public: |
| 60 ContentSubresourceFilterThrottleManagerTest() |
| 61 : ContentSubresourceFilterThrottleManager::Delegate(), |
| 62 blocking_task_runner_(new base::TestSimpleTaskRunner) {} |
| 63 ~ContentSubresourceFilterThrottleManagerTest() override {} |
| 64 |
| 65 void SetUp() override { |
| 66 content::RenderViewHostTestHarness::SetUp(); |
| 67 InitializeRulesetDealer(); |
| 68 } |
| 69 |
| 70 void TearDown() override { |
| 71 dealer_handle_.reset(); |
| 72 RunUntilIdle(); |
| 73 content::RenderViewHostTestHarness::TearDown(); |
| 74 } |
| 75 |
| 76 void RunUntilIdle() { |
| 77 blocking_task_runner_->RunUntilIdle(); |
| 78 base::RunLoop().RunUntilIdle(); |
| 79 } |
| 80 |
| 81 void CreateTestNavigationForSubframe( |
| 82 const GURL& first_url, |
| 83 const ActivationState& parent_activation_state) { |
| 84 subframe_ = content::RenderFrameHostTester::For(main_rfh()) |
| 85 ->AppendChild("subframe"); |
| 86 } |
| 87 |
| 88 // content::WebContentsObserver |
| 89 void DidStartNavigation( |
| 90 content::NavigationHandle* navigation_handle) override { |
| 91 navigation_handle->RegisterThrottleForTesting( |
| 92 base::MakeUnique<MockPageStateActivationThrottle>( |
| 93 navigation_handle, throttle_manager_.get())); |
| 94 std::unique_ptr<content::NavigationThrottle> filtering_throttle = |
| 95 throttle_manager_->MaybeCreateSubframeNavigationFilteringThrottle( |
| 96 navigation_handle); |
| 97 std::unique_ptr<content::NavigationThrottle> activation_throttle = |
| 98 throttle_manager_->MaybeCreateActivationStateComputingThrottle( |
| 99 navigation_handle); |
| 100 if (filtering_throttle) |
| 101 navigation_handle->RegisterThrottleForTesting( |
| 102 std::move(filtering_throttle)); |
| 103 if (activation_throttle) |
| 104 navigation_handle->RegisterThrottleForTesting( |
| 105 std::move(activation_throttle)); |
| 106 } |
| 107 |
| 108 // ContentSubresourceFilterThrottleManager::Delegate: |
| 109 VerifiedRulesetDealer::Handle* GetRulesetDealerHandle() override { |
| 110 return dealer_handle_.get(); |
| 111 } |
| 112 |
| 113 void OnFirstSubresourceLoadDisallowed() override {} |
| 114 |
| 115 void OnActivationStateComputedForCommittingLoad( |
| 116 content::NavigationHandle* navigation_handle, |
| 117 const ActivationState& activation_state) override {} |
| 118 |
| 119 bool ShouldVetoActivation( |
| 120 content::NavigationHandle* navigation_handle) override { |
| 121 return false; |
| 122 } |
| 123 |
| 124 void InitializeRulesetDealer() { |
| 125 std::vector<proto::UrlRule> rules; |
| 126 rules.push_back(testing::CreateWhitelistRuleForDocument( |
| 127 "whitelist.com", proto::ACTIVATION_TYPE_DOCUMENT, {"parent.com"})); |
| 128 ASSERT_NO_FATAL_FAILURE(test_ruleset_creator_.CreateRulesetWithRules( |
| 129 rules, &test_ruleset_pair_)); |
| 130 dealer_handle_ = |
| 131 base::MakeUnique<VerifiedRulesetDealer::Handle>(blocking_task_runner_); |
| 132 dealer_handle_->SetRulesetFile( |
| 133 testing::TestRuleset::Open(test_ruleset_pair_.indexed)); |
| 134 } |
| 135 |
| 136 private: |
| 137 scoped_refptr<base::TestSimpleTaskRunner> blocking_task_runner_; |
| 138 |
| 139 testing::TestRulesetCreator test_ruleset_creator_; |
| 140 testing::TestRulesetPair test_ruleset_pair_; |
| 141 |
| 142 std::unique_ptr<VerifiedRulesetDealer::Handle> dealer_handle_; |
| 143 |
| 144 std::unique_ptr<ContentSubresourceFilterThrottleManager> throttle_manager_; |
| 145 |
| 146 content::RenderFrameHost* subframe_ = nullptr; |
| 147 |
| 148 DISALLOW_COPY_AND_ASSIGN(ContentSubresourceFilterThrottleManagerTest); |
| 149 }; |
| 150 |
| 151 } // namespace subresource_filter |
OLD | NEW |