#ifndef CONTENT_PUBLIC_TEST_TEST_NAVIGATION_THROTTLE_H_
#define CONTENT_PUBLIC_TEST_TEST_NAVIGATION_THROTTLE_H_
#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "content/public/browser/navigation_throttle.h"
namespace content {
class NavigationHandle;
class TestNavigationThrottle : public NavigationThrottle {
public:
enum ThrottleMethod {
WILL_START_REQUEST,
WILL_REDIRECT_REQUEST,
WILL_FAIL_REQUEST,
WILL_PROCESS_RESPONSE,
WILL_COMMIT_WITHOUT_URL_LOADER,
NUM_THROTTLE_METHODS
};
enum ResultSynchrony {
SYNCHRONOUS,
ASYNCHRONOUS,
};
TestNavigationThrottle(NavigationHandle* handle);
TestNavigationThrottle(const TestNavigationThrottle&) = delete;
TestNavigationThrottle& operator=(const TestNavigationThrottle&) = delete;
~TestNavigationThrottle() override;
NavigationThrottle::ThrottleCheckResult WillStartRequest() override;
NavigationThrottle::ThrottleCheckResult WillRedirectRequest() override;
NavigationThrottle::ThrottleCheckResult WillFailRequest() override;
NavigationThrottle::ThrottleCheckResult WillProcessResponse() override;
NavigationThrottle::ThrottleCheckResult WillCommitWithoutUrlLoader() override;
const char* GetNameForLogging() override;
int GetCallCount(ThrottleMethod method);
void SetResponse(ThrottleMethod method,
ResultSynchrony synchrony,
NavigationThrottle::ThrottleCheckResult result);
void SetResponseForAllMethods(ResultSynchrony synchrony,
NavigationThrottle::ThrottleCheckResult result);
void SetCallback(ThrottleMethod method, base::RepeatingClosure callback);
protected:
void OnWillRespond();
private:
NavigationThrottle::ThrottleCheckResult ProcessMethod(ThrottleMethod method);
void CancelAsynchronously(NavigationThrottle::ThrottleCheckResult result);
struct MethodProperties {
public:
MethodProperties();
~MethodProperties();
ResultSynchrony synchrony = SYNCHRONOUS;
NavigationThrottle::ThrottleCheckResult result = {
NavigationThrottle::PROCEED};
base::RepeatingClosure callback;
int call_count = 0;
};
MethodProperties method_properties_[NUM_THROTTLE_METHODS];
base::WeakPtrFactory<TestNavigationThrottle> weak_ptr_factory_{this};
};
}
#endif