1 // Copyright (c) 2014 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/test/test_navigation_url_loader.h"
6 
7 #include <utility>
8 
9 #include "content/browser/loader/navigation_url_loader_delegate.h"
10 #include "content/browser/navigation_subresource_loader_params.h"
11 #include "content/public/browser/global_request_id.h"
12 #include "content/public/browser/render_frame_host.h"
13 #include "content/public/browser/render_process_host.h"
14 #include "content/public/browser/ssl_status.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/common/navigation_policy.h"
17 #include "mojo/public/cpp/bindings/pending_remote.h"
18 #include "net/url_request/redirect_info.h"
19 #include "services/network/public/mojom/url_loader_factory.mojom.h"
20 #include "services/network/public/mojom/url_response_head.mojom.h"
21 
22 namespace content {
23 
TestNavigationURLLoader(std::unique_ptr<NavigationRequestInfo> request_info,NavigationURLLoaderDelegate * delegate,NavigationURLLoader::LoaderType loader_type)24 TestNavigationURLLoader::TestNavigationURLLoader(
25     std::unique_ptr<NavigationRequestInfo> request_info,
26     NavigationURLLoaderDelegate* delegate,
27     NavigationURLLoader::LoaderType loader_type)
28     : request_info_(std::move(request_info)),
29       delegate_(delegate),
30       redirect_count_(0),
31       loader_type_(loader_type) {}
32 
FollowRedirect(const std::vector<std::string> & removed_headers,const net::HttpRequestHeaders & modified_headers,const net::HttpRequestHeaders & modified_cors_exempt_headers,blink::PreviewsState new_previews_state)33 void TestNavigationURLLoader::FollowRedirect(
34     const std::vector<std::string>& removed_headers,
35     const net::HttpRequestHeaders& modified_headers,
36     const net::HttpRequestHeaders& modified_cors_exempt_headers,
37     blink::PreviewsState new_previews_state) {
38   DCHECK_EQ(loader_type_, NavigationURLLoader::LoaderType::kRegular);
39   redirect_count_++;
40 }
41 
SimulateServerRedirect(const GURL & redirect_url)42 void TestNavigationURLLoader::SimulateServerRedirect(const GURL& redirect_url) {
43   DCHECK_EQ(loader_type_, NavigationURLLoader::LoaderType::kRegular);
44   net::RedirectInfo redirect_info;
45   redirect_info.status_code = 302;
46   redirect_info.new_method = "GET";
47   redirect_info.new_url = redirect_url;
48   redirect_info.new_site_for_cookies =
49       net::SiteForCookies::FromUrl(redirect_url);
50   auto response_head = network::mojom::URLResponseHead::New();
51   CallOnRequestRedirected(redirect_info, std::move(response_head));
52 }
53 
SimulateError(int error_code)54 void TestNavigationURLLoader::SimulateError(int error_code) {
55   DCHECK_EQ(loader_type_, NavigationURLLoader::LoaderType::kRegular);
56   SimulateErrorWithStatus(network::URLLoaderCompletionStatus(error_code));
57 }
58 
SimulateErrorWithStatus(const network::URLLoaderCompletionStatus & status)59 void TestNavigationURLLoader::SimulateErrorWithStatus(
60     const network::URLLoaderCompletionStatus& status) {
61   DCHECK_EQ(loader_type_, NavigationURLLoader::LoaderType::kRegular);
62   delegate_->OnRequestFailed(status);
63 }
64 
CallOnRequestRedirected(const net::RedirectInfo & redirect_info,network::mojom::URLResponseHeadPtr response_head)65 void TestNavigationURLLoader::CallOnRequestRedirected(
66     const net::RedirectInfo& redirect_info,
67     network::mojom::URLResponseHeadPtr response_head) {
68   DCHECK_EQ(loader_type_, NavigationURLLoader::LoaderType::kRegular);
69   response_head->parsed_headers = network::mojom::ParsedHeaders::New();
70   delegate_->OnRequestRedirected(redirect_info, std::move(response_head));
71 }
72 
CallOnResponseStarted(network::mojom::URLResponseHeadPtr response_head)73 void TestNavigationURLLoader::CallOnResponseStarted(
74     network::mojom::URLResponseHeadPtr response_head) {
75   if (!response_head->parsed_headers)
76     response_head->parsed_headers = network::mojom::ParsedHeaders::New();
77   // Create a bidirectionnal communication pipe between a URLLoader and a
78   // URLLoaderClient. It will be closed at the end of this function. The sole
79   // purpose of this is not to violate some DCHECKs when the navigation commits.
80   mojo::PendingRemote<network::mojom::URLLoaderClient> url_loader_client_remote;
81   mojo::PendingRemote<network::mojom::URLLoader> url_loader_remote;
82   ignore_result(url_loader_remote.InitWithNewPipeAndPassReceiver());
83   auto url_loader_client_endpoints =
84       network::mojom::URLLoaderClientEndpoints::New(
85           std::move(url_loader_remote),
86           url_loader_client_remote.InitWithNewPipeAndPassReceiver());
87 
88   delegate_->OnResponseStarted(std::move(url_loader_client_endpoints),
89                                std::move(response_head),
90                                mojo::ScopedDataPipeConsumerHandle(),
91                                GlobalRequestID::MakeBrowserInitiated(), false,
92                                NavigationDownloadPolicy(), base::nullopt);
93 }
94 
~TestNavigationURLLoader()95 TestNavigationURLLoader::~TestNavigationURLLoader() {}
96 
97 }  // namespace content
98