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
  119
  120

content / test / test_navigation_url_loader.cc [blame]

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

#include "content/test/test_navigation_url_loader.h"

#include <tuple>
#include <utility>

#include "content/browser/loader/navigation_early_hints_manager.h"
#include "content/browser/loader/navigation_url_loader_delegate.h"
#include "content/browser/navigation_subresource_loader_params.h"
#include "content/public/browser/global_request_id.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/ssl_status.h"
#include "content/public/browser/web_contents.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "net/url_request/redirect_info.h"
#include "services/network/public/mojom/url_loader_factory.mojom.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "third_party/blink/public/common/navigation/navigation_policy.h"

namespace content {

TestNavigationURLLoader::TestNavigationURLLoader(
    std::unique_ptr<NavigationRequestInfo> request_info,
    NavigationURLLoaderDelegate* delegate,
    NavigationURLLoader::LoaderType loader_type)
    : request_info_(std::move(request_info)),
      delegate_(delegate),
      redirect_count_(0),
      loader_type_(loader_type) {}

void TestNavigationURLLoader::Start() {
  // Do nothing.
}

void TestNavigationURLLoader::FollowRedirect(
    const std::vector<std::string>& removed_headers,
    const net::HttpRequestHeaders& modified_headers,
    const net::HttpRequestHeaders& modified_cors_exempt_headers) {
  DCHECK_EQ(loader_type_, NavigationURLLoader::LoaderType::kRegular);
  redirect_count_++;
}

bool TestNavigationURLLoader::SetNavigationTimeout(base::TimeDelta timeout) {
  // Do nothing. `false` here means no timeout was started.
  return false;
}

void TestNavigationURLLoader::CancelNavigationTimeout() {}

void TestNavigationURLLoader::SimulateServerRedirect(const GURL& redirect_url) {
  DCHECK_EQ(loader_type_, NavigationURLLoader::LoaderType::kRegular);
  net::RedirectInfo redirect_info;
  redirect_info.status_code = 302;
  redirect_info.new_method = "GET";
  redirect_info.new_url = redirect_url;
  redirect_info.new_site_for_cookies =
      net::SiteForCookies::FromUrl(redirect_url);
  auto response_head = network::mojom::URLResponseHead::New();
  CallOnRequestRedirected(redirect_info, std::move(response_head));
}

void TestNavigationURLLoader::SimulateError(int error_code) {
  DCHECK_EQ(loader_type_, NavigationURLLoader::LoaderType::kRegular);
  SimulateErrorWithStatus(network::URLLoaderCompletionStatus(error_code));
}

void TestNavigationURLLoader::SimulateErrorWithStatus(
    const network::URLLoaderCompletionStatus& status) {
  DCHECK_EQ(loader_type_, NavigationURLLoader::LoaderType::kRegular);
  delegate_->OnRequestFailed(status);
}

void TestNavigationURLLoader::SimulateEarlyHintsPreloadLinkHeaderReceived() {
  was_resource_hints_received_ = true;
}

void TestNavigationURLLoader::CallOnRequestRedirected(
    const net::RedirectInfo& redirect_info,
    network::mojom::URLResponseHeadPtr response_head) {
  DCHECK_EQ(loader_type_, NavigationURLLoader::LoaderType::kRegular);
  response_head->parsed_headers = network::mojom::ParsedHeaders::New();
  delegate_->OnRequestRedirected(
      redirect_info, request_info_->isolation_info.network_anonymization_key(),
      std::move(response_head));
}

void TestNavigationURLLoader::CallOnResponseStarted(
    network::mojom::URLResponseHeadPtr response_head,
    mojo::ScopedDataPipeConsumerHandle response_body,
    std::optional<mojo_base::BigBuffer> cached_metadata) {
  if (!response_head->parsed_headers)
    response_head->parsed_headers = network::mojom::ParsedHeaders::New();
  // Create a bidirectionnal communication pipe between a URLLoader and a
  // URLLoaderClient. It will be closed at the end of this function. The sole
  // purpose of this is not to violate some DCHECKs when the navigation commits.
  mojo::PendingRemote<network::mojom::URLLoaderClient> url_loader_client_remote;
  mojo::PendingRemote<network::mojom::URLLoader> url_loader_remote;
  std::ignore = url_loader_remote.InitWithNewPipeAndPassReceiver();
  auto url_loader_client_endpoints =
      network::mojom::URLLoaderClientEndpoints::New(
          std::move(url_loader_remote),
          url_loader_client_remote.InitWithNewPipeAndPassReceiver());

  NavigationURLLoaderDelegate::EarlyHints early_hints;
  early_hints.was_resource_hints_received = was_resource_hints_received_;

  delegate_->OnResponseStarted(
      std::move(url_loader_client_endpoints), std::move(response_head),
      std::move(response_body), GlobalRequestID::MakeBrowserInitiated(), false,
      request_info_->isolation_info.network_anonymization_key(),
      SubresourceLoaderParams(), std::move(early_hints));
}

TestNavigationURLLoader::~TestNavigationURLLoader() {}

}  // namespace content