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 |
| 68 // Initialize the ruleset dealer. |
| 69 std::vector<proto::UrlRule> rules; |
| 70 rules.push_back(testing::CreateWhitelistRuleForDocument( |
| 71 "whitelist.com", proto::ACTIVATION_TYPE_DOCUMENT, {"parent.com"})); |
| 72 ASSERT_NO_FATAL_FAILURE(test_ruleset_creator_.CreateRulesetWithRules( |
| 73 rules, &test_ruleset_pair_)); |
| 74 dealer_handle_ = |
| 75 base::MakeUnique<VerifiedRulesetDealer::Handle>(blocking_task_runner_); |
| 76 dealer_handle_->SetRulesetFile( |
| 77 testing::TestRuleset::Open(test_ruleset_pair_.indexed)); |
| 78 |
| 79 throttle_manager_ = |
| 80 base::MakeUnique<ContentSubresourceFilterThrottleManager>( |
| 81 this, dealer_handle_.get(), |
| 82 RenderViewHostTestHarness::web_contents()); |
| 83 } |
| 84 |
| 85 void TearDown() override { |
| 86 throttle_manager_.reset(); |
| 87 dealer_handle_.reset(); |
| 88 RunUntilIdle(); |
| 89 content::RenderViewHostTestHarness::TearDown(); |
| 90 } |
| 91 |
| 92 void RunUntilIdle() { |
| 93 blocking_task_runner_->RunUntilIdle(); |
| 94 base::RunLoop().RunUntilIdle(); |
| 95 } |
| 96 |
| 97 // content::WebContentsObserver |
| 98 void DidStartNavigation( |
| 99 content::NavigationHandle* navigation_handle) override { |
| 100 // Inject the proper throttles at this time. |
| 101 std::vector<std::unique_ptr<content::NavigationThrottle>> throttles; |
| 102 throttles.push_back(base::MakeUnique<MockPageStateActivationThrottle>( |
| 103 navigation_handle, throttle_manager_.get())); |
| 104 throttle_manager_->MaybeInsertNavigationThrottles(navigation_handle, |
| 105 &throttles); |
| 106 for (auto& it : throttles) { |
| 107 navigation_handle->RegisterThrottleForTesting(std::move(it)); |
| 108 } |
| 109 } |
| 110 |
| 111 // ContentSubresourceFilterThrottleManager::Delegate: |
| 112 void OnFirstSubresourceLoadDisallowed() override {} |
| 113 |
| 114 bool ShouldVetoActivation( |
| 115 content::NavigationHandle* navigation_handle) override { |
| 116 return false; |
| 117 } |
| 118 |
| 119 private: |
| 120 scoped_refptr<base::TestSimpleTaskRunner> blocking_task_runner_; |
| 121 |
| 122 testing::TestRulesetCreator test_ruleset_creator_; |
| 123 testing::TestRulesetPair test_ruleset_pair_; |
| 124 |
| 125 std::unique_ptr<VerifiedRulesetDealer::Handle> dealer_handle_; |
| 126 |
| 127 std::unique_ptr<ContentSubresourceFilterThrottleManager> throttle_manager_; |
| 128 |
| 129 DISALLOW_COPY_AND_ASSIGN(ContentSubresourceFilterThrottleManagerTest); |
| 130 }; |
| 131 |
| 132 } // namespace subresource_filter |
OLD | NEW |