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 #ifndef CONTENT_PUBLIC_TEST_TEST_NAVIGATION_THROTTLE_H_ 6 #define CONTENT_PUBLIC_TEST_TEST_NAVIGATION_THROTTLE_H_ 7 8 #include "base/callback.h" 9 #include "base/macros.h" 10 #include "base/memory/weak_ptr.h" 11 #include "base/optional.h" 12 #include "content/public/browser/navigation_throttle.h" 13 14 namespace content { 15 16 class NavigationHandle; 17 18 // This class can be used to cancel navigations synchronously or asynchronously 19 // at specific times in the NavigationThrottle lifecycle. 20 // 21 // By default TestNavigationThrottle responds to every method synchronously with 22 // NavigationThrottle::PROCEED. 23 class TestNavigationThrottle : public NavigationThrottle { 24 public: 25 enum ThrottleMethod { 26 WILL_START_REQUEST, 27 WILL_REDIRECT_REQUEST, 28 WILL_FAIL_REQUEST, 29 WILL_PROCESS_RESPONSE, 30 NUM_THROTTLE_METHODS 31 }; 32 33 enum ResultSynchrony { 34 SYNCHRONOUS, 35 ASYNCHRONOUS, 36 }; 37 38 TestNavigationThrottle(NavigationHandle* handle); 39 ~TestNavigationThrottle() override; 40 41 // NavigationThrottle: 42 NavigationThrottle::ThrottleCheckResult WillStartRequest() override; 43 NavigationThrottle::ThrottleCheckResult WillRedirectRequest() override; 44 NavigationThrottle::ThrottleCheckResult WillFailRequest() override; 45 NavigationThrottle::ThrottleCheckResult WillProcessResponse() override; 46 const char* GetNameForLogging() override; 47 48 // Return how often the indicated |method| was called. 49 int GetCallCount(ThrottleMethod method); 50 51 // Sets the throttle to respond to the method indicated by |method| using 52 // |result|, with the given |synchrony|. This overrides any behaviour 53 // previously set for the same |method| using SetResult(). 54 // 55 // If |synchrony| is ASYNCHRONOUS, |result|'s action must be one that that is 56 // allowed for NavigationThrottle::CancelDeferredNavigation(): 57 // - NavigationThrottle::CANCEL, 58 // - NavigationThrottle::CANCEL_AND_IGNORE, or 59 // - NavigationThrottle::BLOCK_REQUEST_AND_COLLAPSE. 60 // 61 // At the moment, it is not possible to specify that the throttle should defer 62 // and then asynchronously call Resume(). 63 void SetResponse(ThrottleMethod method, 64 ResultSynchrony synchrony, 65 NavigationThrottle::ThrottleCheckResult result); 66 67 // Calls SetResponse with the given values for every method. 68 void SetResponseForAllMethods(ResultSynchrony synchrony, 69 NavigationThrottle::ThrottleCheckResult result); 70 71 // Callback to be called when the given method is called. 72 void SetCallback(ThrottleMethod method, base::RepeatingClosure callback); 73 74 protected: 75 // A method that subclasses can override to be called immediately before a 76 // throttle responds, either by returning synchronously, or by calling 77 // CancelDeferredNavigation() asynchronously. 78 // 79 // TODO(crbug.com/770292): Support setting a callback instead, and use that to 80 // get rid of the following classes: 81 // - ResourceLoadingCancellingThrottle in 82 // ads_page_load_metrics_observer_unittest.cc 83 // - DeletingNavigationThrottle in navigation_request_unittest.cc 84 void OnWillRespond(); 85 86 private: 87 NavigationThrottle::ThrottleCheckResult ProcessMethod(ThrottleMethod method); 88 void CancelAsynchronously(NavigationThrottle::ThrottleCheckResult result); 89 90 struct MethodProperties { 91 public: 92 MethodProperties(); 93 ~MethodProperties(); 94 95 ResultSynchrony synchrony = SYNCHRONOUS; 96 NavigationThrottle::ThrottleCheckResult result = { 97 NavigationThrottle::PROCEED}; 98 base::RepeatingClosure callback; 99 int call_count = 0; 100 }; 101 MethodProperties method_properties_[NUM_THROTTLE_METHODS]; 102 103 base::WeakPtrFactory<TestNavigationThrottle> weak_ptr_factory_{this}; 104 105 DISALLOW_COPY_AND_ASSIGN(TestNavigationThrottle); 106 }; 107 108 } // namespace content 109 110 #endif // CONTENT_PUBLIC_TEST_TEST_NAVIGATION_THROTTLE_H_ 111