blob: d8cec432f32cb0714cd96256fde1ca2a9f184f9d [file] [log] [blame]
yhiranof6c76cf2016-12-22 11:42:181// 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
John Abd-El-Malek6b56ef712017-10-21 22:52:465#ifndef CONTENT_RENDERER_LOADER_URL_LOADER_CLIENT_IMPL_H_
6#define CONTENT_RENDERER_LOADER_URL_LOADER_CLIENT_IMPL_H_
yhiranof6c76cf2016-12-22 11:42:187
8#include <stdint.h>
yhirano6a0e9c72016-12-27 08:31:049#include <vector>
yhiranof6c76cf2016-12-22 11:42:1810#include "base/callback_forward.h"
11#include "base/memory/ref_counted.h"
yhirano6a0e9c72016-12-27 08:31:0412#include "base/memory/weak_ptr.h"
yhiranof6c76cf2016-12-22 11:42:1813#include "content/common/content_export.h"
arthursonzogni3a4ca9f2017-12-07 17:58:3414#include "mojo/public/cpp/bindings/binding.h"
yhiranof6c76cf2016-12-22 11:42:1815#include "mojo/public/cpp/system/data_pipe.h"
Ken Rockot54311e62018-02-10 19:01:5216#include "services/network/public/mojom/url_loader.mojom.h"
yhiranof6c76cf2016-12-22 11:42:1817
18namespace base {
19class SingleThreadTaskRunner;
20} // namespace base
21
yhiranof6c76cf2016-12-22 11:42:1822namespace net {
23struct RedirectInfo;
24} // namespace net
25
Takashi Toyoshima8f988532017-11-13 07:32:3726namespace network {
John Abd-El-Malek46248032018-01-17 19:11:2327struct ResourceResponseHead;
Takashi Toyoshimaaa278662017-11-20 11:11:2628struct URLLoaderCompletionStatus;
Takashi Toyoshima8f988532017-11-13 07:32:3729} // namespace network
30
yhiranof6c76cf2016-12-22 11:42:1831namespace content {
32class ResourceDispatcher;
33class URLResponseBodyConsumer;
yhiranof6c76cf2016-12-22 11:42:1834
John Abd-El-Malekb165dc52018-01-18 17:12:1835class CONTENT_EXPORT URLLoaderClientImpl final
36 : public network::mojom::URLLoaderClient {
yhiranof6c76cf2016-12-22 11:42:1837 public:
38 URLLoaderClientImpl(int request_id,
39 ResourceDispatcher* resource_dispatcher,
Clark DuVallb2680c22018-08-10 15:27:2740 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
Chris Mumford90b1a0d2018-09-26 20:10:4341 bool bypass_redirect_checks,
42 const GURL& request_url);
yhiranof6c76cf2016-12-22 11:42:1843 ~URLLoaderClientImpl() override;
44
yhirano6a0e9c72016-12-27 08:31:0445 // Sets |is_deferred_|. From now, the received messages are not dispatched
46 // to clients until UnsetDefersLoading is called.
47 void SetDefersLoading();
48
49 // Unsets |is_deferred_|.
50 void UnsetDefersLoading();
51
Marijn Kruisselbrink090b1a4f2018-03-30 19:54:1252 // Dispatches the messages received after SetDefersLoading is called.
yhirano6a0e9c72016-12-27 08:31:0453 void FlushDeferredMessages();
54
Marijn Kruisselbrink090b1a4f2018-03-30 19:54:1255 // If set to true, this causes the raw datapipe containing the response body
56 // to be passed on to the ResourceDispatcher. Otherwise a
57 // URLResponseBodyConsumer is created that passes individual chunks of data
Maks Orlovich5031c5d2018-10-26 12:41:4458 // from the body to the dispatcher.
Marijn Kruisselbrink090b1a4f2018-03-30 19:54:1259 void SetPassResponsePipeToDispatcher(bool pass_pipe) {
Maks Orlovich5031c5d2018-10-26 12:41:4460 pass_response_pipe_to_dispatcher_ = pass_pipe;
Marijn Kruisselbrink090b1a4f2018-03-30 19:54:1261 }
62
arthursonzogni3a4ca9f2017-12-07 17:58:3463 // Binds this instance to the given URLLoaderClient endpoints so that it can
64 // start getting the mojo calls from the given loader. This is used only for
arthursonzogni1645d4d2018-04-24 20:28:5065 // the main resource loading. Otherwise (in regular subresource loading cases)
66 // |this| is not bound to a client request, but used via ThrottlingURLLoader
67 // to get client upcalls from the loader.
John Abd-El-Malekb165dc52018-01-18 17:12:1868 void Bind(
69 network::mojom::URLLoaderClientEndpointsPtr url_loader_client_endpoints);
arthursonzogni3a4ca9f2017-12-07 17:58:3470
John Abd-El-Malekb165dc52018-01-18 17:12:1871 // network::mojom::URLLoaderClient implementation
72 void OnReceiveResponse(
Marijn Kruisselbrink9ebd7ba2018-06-11 23:18:0473 const network::ResourceResponseHead& response_head) override;
John Abd-El-Malek46248032018-01-17 19:11:2374 void OnReceiveRedirect(
75 const net::RedirectInfo& redirect_info,
76 const network::ResourceResponseHead& response_head) override;
tzikbedcbce2017-01-24 15:08:0577 void OnUploadProgress(int64_t current_position,
78 int64_t total_size,
tzikc2007562017-04-25 07:48:2479 OnUploadProgressCallback ack_callback) override;
yhirano2a525f92017-01-18 08:16:4680 void OnReceiveCachedMetadata(const std::vector<uint8_t>& data) override;
yhiranof6c76cf2016-12-22 11:42:1881 void OnTransferSizeUpdated(int32_t transfer_size_diff) override;
82 void OnStartLoadingResponseBody(
83 mojo::ScopedDataPipeConsumerHandle body) override;
Takashi Toyoshimaaa278662017-11-20 11:11:2684 void OnComplete(const network::URLLoaderCompletionStatus& status) override;
yhiranof6c76cf2016-12-22 11:42:1885
yhirano6a0e9c72016-12-27 08:31:0486 private:
Yutaka Hirano3dad2832017-12-14 06:48:1687 class DeferredMessage;
88 class DeferredOnReceiveResponse;
89 class DeferredOnReceiveRedirect;
Yutaka Hirano3dad2832017-12-14 06:48:1690 class DeferredOnUploadProgress;
91 class DeferredOnReceiveCachedMetadata;
Makoto Shimazu494ae7b52019-01-10 09:35:3292 class DeferredOnStartLoadingResponseBody;
Yutaka Hirano3dad2832017-12-14 06:48:1693 class DeferredOnComplete;
94
yhiranoe45a888f2017-04-14 01:24:2095 bool NeedsStoringMessage() const;
Yutaka Hirano3dad2832017-12-14 06:48:1696 void StoreAndDispatch(std::unique_ptr<DeferredMessage> message);
arthursonzogni3a4ca9f2017-12-07 17:58:3497 void OnConnectionClosed();
yhiranof6c76cf2016-12-22 11:42:1898
Makoto Shimazu494ae7b52019-01-10 09:35:3299 // Non-ResourceLoadViaDataPipe:
100 // Used for reading the response body from the data pipe passed on
101 // OnStartLoadingResponseBody() and passing the data to corresponding
102 // RequestPeer.
yhiranof6c76cf2016-12-22 11:42:18103 scoped_refptr<URLResponseBodyConsumer> body_consumer_;
Makoto Shimazu494ae7b52019-01-10 09:35:32104
Yutaka Hirano3dad2832017-12-14 06:48:16105 std::vector<std::unique_ptr<DeferredMessage>> deferred_messages_;
yhiranof6c76cf2016-12-22 11:42:18106 const int request_id_;
Arthur Sonzognia727d4b2018-12-10 09:10:18107 bool has_received_response_head_ = false;
108 bool has_received_response_body_ = false;
arthursonzogni3a4ca9f2017-12-07 17:58:34109 bool has_received_complete_ = false;
yhirano6a0e9c72016-12-27 08:31:04110 bool is_deferred_ = false;
Marijn Kruisselbrink090b1a4f2018-03-30 19:54:12111 bool pass_response_pipe_to_dispatcher_ = false;
yhirano6a0e9c72016-12-27 08:31:04112 int32_t accumulated_transfer_size_diff_during_deferred_ = 0;
yhiranof6c76cf2016-12-22 11:42:18113 ResourceDispatcher* const resource_dispatcher_;
114 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
Clark DuVallb2680c22018-08-10 15:27:27115 bool bypass_redirect_checks_ = false;
Chris Mumford90b1a0d2018-09-26 20:10:43116 GURL last_loaded_url_;
arthursonzogni3a4ca9f2017-12-07 17:58:34117
John Abd-El-Malekb165dc52018-01-18 17:12:18118 network::mojom::URLLoaderPtr url_loader_;
119 mojo::Binding<network::mojom::URLLoaderClient> url_loader_client_binding_;
arthursonzogni3a4ca9f2017-12-07 17:58:34120
yhirano6a0e9c72016-12-27 08:31:04121 base::WeakPtrFactory<URLLoaderClientImpl> weak_factory_;
yhiranof6c76cf2016-12-22 11:42:18122};
123
124} // namespace content
125
John Abd-El-Malek6b56ef712017-10-21 22:52:46126#endif // CONTENT_RENDERER_LOADER_URL_LOADER_CLIENT_IMPL_H_