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 "content/public/test/test_navigation_throttle.h"
6
7 #include "base/bind.h"
8 #include "base/optional.h"
9 #include "content/public/browser/browser_task_traits.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/navigation_handle.h"
12
13 namespace content {
14
TestNavigationThrottle(NavigationHandle * handle)15 TestNavigationThrottle::TestNavigationThrottle(NavigationHandle* handle)
16 : NavigationThrottle(handle) {}
17
~TestNavigationThrottle()18 TestNavigationThrottle::~TestNavigationThrottle() {}
19
20 NavigationThrottle::ThrottleCheckResult
WillStartRequest()21 TestNavigationThrottle::WillStartRequest() {
22 return ProcessMethod(WILL_START_REQUEST);
23 }
24
25 NavigationThrottle::ThrottleCheckResult
WillRedirectRequest()26 TestNavigationThrottle::WillRedirectRequest() {
27 return ProcessMethod(WILL_REDIRECT_REQUEST);
28 }
29
30 NavigationThrottle::ThrottleCheckResult
WillFailRequest()31 TestNavigationThrottle::WillFailRequest() {
32 return ProcessMethod(WILL_FAIL_REQUEST);
33 }
34
35 NavigationThrottle::ThrottleCheckResult
WillProcessResponse()36 TestNavigationThrottle::WillProcessResponse() {
37 return ProcessMethod(WILL_PROCESS_RESPONSE);
38 }
39
GetNameForLogging()40 const char* TestNavigationThrottle::GetNameForLogging() {
41 return "TestNavigationThrottle";
42 }
43
GetCallCount(ThrottleMethod method)44 int TestNavigationThrottle::GetCallCount(ThrottleMethod method) {
45 return method_properties_[method].call_count;
46 }
47
SetResponse(ThrottleMethod method,ResultSynchrony synchrony,NavigationThrottle::ThrottleCheckResult result)48 void TestNavigationThrottle::SetResponse(
49 ThrottleMethod method,
50 ResultSynchrony synchrony,
51 NavigationThrottle::ThrottleCheckResult result) {
52 CHECK_LT(method, NUM_THROTTLE_METHODS) << "Invalid throttle method";
53 if (synchrony == ASYNCHRONOUS) {
54 DCHECK(result.action() == NavigationThrottle::CANCEL_AND_IGNORE ||
55 result.action() == NavigationThrottle::CANCEL ||
56 result.action() == NavigationThrottle::BLOCK_REQUEST_AND_COLLAPSE)
57 << "Invalid result for asynchronous response. Must have a valid action "
58 "for CancelDeferredNavigation().";
59 }
60 method_properties_[method].synchrony = synchrony;
61 method_properties_[method].result = result;
62 }
63
SetResponseForAllMethods(ResultSynchrony synchrony,NavigationThrottle::ThrottleCheckResult result)64 void TestNavigationThrottle::SetResponseForAllMethods(
65 ResultSynchrony synchrony,
66 NavigationThrottle::ThrottleCheckResult result) {
67 for (size_t method = 0; method < NUM_THROTTLE_METHODS; method++) {
68 SetResponse(static_cast<ThrottleMethod>(method), synchrony, result);
69 }
70 }
71
SetCallback(ThrottleMethod method,base::RepeatingClosure callback)72 void TestNavigationThrottle::SetCallback(ThrottleMethod method,
73 base::RepeatingClosure callback) {
74 method_properties_[method].callback = std::move(callback);
75 }
76
OnWillRespond()77 void TestNavigationThrottle::OnWillRespond() {}
78
ProcessMethod(ThrottleMethod method)79 NavigationThrottle::ThrottleCheckResult TestNavigationThrottle::ProcessMethod(
80 ThrottleMethod method) {
81 method_properties_[method].call_count++;
82 if (!method_properties_[method].callback.is_null())
83 method_properties_[method].callback.Run();
84
85 NavigationThrottle::ThrottleCheckResult result =
86 method_properties_[method].result;
87 if (method_properties_[method].synchrony == ASYNCHRONOUS) {
88 DCHECK_CURRENTLY_ON(BrowserThread::UI);
89 GetUIThreadTaskRunner({})->PostTask(
90 FROM_HERE,
91 base::BindOnce(&TestNavigationThrottle::TestNavigationThrottle::
92 CancelAsynchronously,
93 weak_ptr_factory_.GetWeakPtr(), result));
94 return NavigationThrottle::DEFER;
95 }
96 OnWillRespond();
97 return result;
98 }
99
CancelAsynchronously(NavigationThrottle::ThrottleCheckResult result)100 void TestNavigationThrottle::CancelAsynchronously(
101 NavigationThrottle::ThrottleCheckResult result) {
102 OnWillRespond();
103 CancelDeferredNavigation(result);
104 }
105
MethodProperties()106 TestNavigationThrottle::MethodProperties::MethodProperties() {}
~MethodProperties()107 TestNavigationThrottle::MethodProperties::~MethodProperties() {}
108
109 } // namespace content
110