1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118

content / public / test / test_navigation_throttle.cc [blame]

// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/342213636): Remove this and spanify to fix the errors.
#pragma allow_unsafe_buffers
#endif

#include "content/public/test/test_navigation_throttle.h"

#include "base/functional/bind.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/navigation_handle.h"

namespace content {

TestNavigationThrottle::TestNavigationThrottle(NavigationHandle* handle)
    : NavigationThrottle(handle) {}

TestNavigationThrottle::~TestNavigationThrottle() {}

NavigationThrottle::ThrottleCheckResult
TestNavigationThrottle::WillStartRequest() {
  return ProcessMethod(WILL_START_REQUEST);
}

NavigationThrottle::ThrottleCheckResult
TestNavigationThrottle::WillRedirectRequest() {
  return ProcessMethod(WILL_REDIRECT_REQUEST);
}

NavigationThrottle::ThrottleCheckResult
TestNavigationThrottle::WillFailRequest() {
  return ProcessMethod(WILL_FAIL_REQUEST);
}

NavigationThrottle::ThrottleCheckResult
TestNavigationThrottle::WillProcessResponse() {
  return ProcessMethod(WILL_PROCESS_RESPONSE);
}

NavigationThrottle::ThrottleCheckResult
TestNavigationThrottle::WillCommitWithoutUrlLoader() {
  return ProcessMethod(WILL_COMMIT_WITHOUT_URL_LOADER);
}

const char* TestNavigationThrottle::GetNameForLogging() {
  return "TestNavigationThrottle";
}

int TestNavigationThrottle::GetCallCount(ThrottleMethod method) {
  return method_properties_[method].call_count;
}

void TestNavigationThrottle::SetResponse(
    ThrottleMethod method,
    ResultSynchrony synchrony,
    NavigationThrottle::ThrottleCheckResult result) {
  CHECK_LT(method, NUM_THROTTLE_METHODS) << "Invalid throttle method";
  if (synchrony == ASYNCHRONOUS) {
    DCHECK(result.action() == NavigationThrottle::CANCEL_AND_IGNORE ||
           result.action() == NavigationThrottle::CANCEL ||
           result.action() == NavigationThrottle::BLOCK_REQUEST_AND_COLLAPSE)
        << "Invalid result for asynchronous response. Must have a valid action "
           "for CancelDeferredNavigation().";
  }
  method_properties_[method].synchrony = synchrony;
  method_properties_[method].result = result;
}

void TestNavigationThrottle::SetResponseForAllMethods(
    ResultSynchrony synchrony,
    NavigationThrottle::ThrottleCheckResult result) {
  for (size_t method = 0; method < NUM_THROTTLE_METHODS; method++) {
    SetResponse(static_cast<ThrottleMethod>(method), synchrony, result);
  }
}

void TestNavigationThrottle::SetCallback(ThrottleMethod method,
                                         base::RepeatingClosure callback) {
  method_properties_[method].callback = std::move(callback);
}

void TestNavigationThrottle::OnWillRespond() {}

NavigationThrottle::ThrottleCheckResult TestNavigationThrottle::ProcessMethod(
    ThrottleMethod method) {
  method_properties_[method].call_count++;
  if (!method_properties_[method].callback.is_null())
    method_properties_[method].callback.Run();

  NavigationThrottle::ThrottleCheckResult result =
      method_properties_[method].result;
  if (method_properties_[method].synchrony == ASYNCHRONOUS) {
    DCHECK_CURRENTLY_ON(BrowserThread::UI);
    GetUIThreadTaskRunner({})->PostTask(
        FROM_HERE,
        base::BindOnce(&TestNavigationThrottle::TestNavigationThrottle::
                           CancelAsynchronously,
                       weak_ptr_factory_.GetWeakPtr(), result));
    return NavigationThrottle::DEFER;
  }
  OnWillRespond();
  return result;
}

void TestNavigationThrottle::CancelAsynchronously(
    NavigationThrottle::ThrottleCheckResult result) {
  OnWillRespond();
  CancelDeferredNavigation(result);
}

TestNavigationThrottle::MethodProperties::MethodProperties() {}
TestNavigationThrottle::MethodProperties::~MethodProperties() {}

}  // namespace content