1 // Copyright 2016 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_TEST_TEST_NAVIGATION_URL_LOADER_DELEGATE_H_
6 #define CONTENT_TEST_TEST_NAVIGATION_URL_LOADER_DELEGATE_H_
7 
8 #include <memory>
9 
10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/optional.h"
13 #include "base/time/time.h"
14 #include "content/browser/loader/navigation_url_loader_delegate.h"
15 #include "net/url_request/redirect_info.h"
16 #include "services/network/public/mojom/url_response_head.mojom-forward.h"
17 
18 namespace base {
19 class RunLoop;
20 }
21 
22 namespace content {
23 
24 // Test implementation of NavigationURLLoaderDelegate to monitor navigation
25 // progress in the network stack.
26 class TestNavigationURLLoaderDelegate : public NavigationURLLoaderDelegate {
27  public:
28   TestNavigationURLLoaderDelegate();
29   ~TestNavigationURLLoaderDelegate() override;
30 
redirect_info()31   const net::RedirectInfo& redirect_info() const { return redirect_info_; }
redirect_response()32   network::mojom::URLResponseHead* redirect_response() const {
33     return redirect_response_.get();
34   }
response()35   network::mojom::URLResponseHead* response() const {
36     return response_head_.get();
37   }
net_error()38   int net_error() const { return net_error_; }
ssl_info()39   const net::SSLInfo& ssl_info() const { return ssl_info_; }
on_request_handled_counter()40   int on_request_handled_counter() const { return on_request_handled_counter_; }
is_download()41   bool is_download() const { return is_download_; }
has_url_loader_client_endpoints()42   bool has_url_loader_client_endpoints() {
43     return !!url_loader_client_endpoints_;
44   }
45 
46   // Waits for various navigation events.
47   // Note: if the event already happened, the functions will hang.
48   // TODO(clamy): Make the functions not hang if they are called after the
49   // event happened.
50   void WaitForRequestRedirected();
51   void WaitForResponseStarted();
52   void WaitForRequestFailed();
53   void WaitForRequestStarted();
54 
55   void ReleaseURLLoaderClientEndpoints();
56 
57   // NavigationURLLoaderDelegate implementation.
58   void OnRequestRedirected(
59       const net::RedirectInfo& redirect_info,
60       network::mojom::URLResponseHeadPtr response) override;
61   void OnResponseStarted(
62       network::mojom::URLLoaderClientEndpointsPtr url_loader_client_endpoints,
63       network::mojom::URLResponseHeadPtr response_head,
64       mojo::ScopedDataPipeConsumerHandle response_body,
65       const GlobalRequestID& request_id,
66       bool is_download,
67       NavigationDownloadPolicy download_policy,
68       base::Optional<SubresourceLoaderParams> subresource_loader_params)
69       override;
70   void OnRequestFailed(
71       const network::URLLoaderCompletionStatus& status) override;
72   void OnRequestStarted(base::TimeTicks timestamp) override;
73 
74  private:
75   net::RedirectInfo redirect_info_;
76   network::mojom::URLResponseHeadPtr redirect_response_;
77   network::mojom::URLLoaderClientEndpointsPtr url_loader_client_endpoints_;
78   network::mojom::URLResponseHeadPtr response_head_;
79   mojo::ScopedDataPipeConsumerHandle response_body_;
80   int net_error_;
81   net::SSLInfo ssl_info_;
82   int on_request_handled_counter_;
83   bool is_download_;
84 
85   std::unique_ptr<base::RunLoop> request_redirected_;
86   std::unique_ptr<base::RunLoop> response_started_;
87   std::unique_ptr<base::RunLoop> request_failed_;
88   std::unique_ptr<base::RunLoop> request_started_;
89 
90   DISALLOW_COPY_AND_ASSIGN(TestNavigationURLLoaderDelegate);
91 };
92 
93 }  // namespace content
94 
95 #endif  // CONTENT_TEST_TEST_NAVIGATION_URL_LOADER_DELEGATE_H
96