blob: 6a3c19ce8a5aecf43adbffa6d056afab9b031b9e [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>
Matt Falkenhagen164f9b842020-04-17 11:39:0810
yhiranof6c76cf2016-12-22 11:42:1811#include "base/callback_forward.h"
Matt Falkenhagen164f9b842020-04-17 11:39:0812#include "base/memory/scoped_refptr.h"
yhirano6a0e9c72016-12-27 08:31:0413#include "base/memory/weak_ptr.h"
Matt Falkenhagen164f9b842020-04-17 11:39:0814#include "base/time/time.h"
yhiranof6c76cf2016-12-22 11:42:1815#include "content/common/content_export.h"
Julie Jeongeun Kim028b2c92019-11-08 02:51:1616#include "mojo/public/cpp/bindings/receiver.h"
Julie Jeongeun Kim917c197902019-11-26 03:36:1417#include "mojo/public/cpp/bindings/remote.h"
yhiranof6c76cf2016-12-22 11:42:1818#include "mojo/public/cpp/system/data_pipe.h"
Ken Rockot54311e62018-02-10 19:01:5219#include "services/network/public/mojom/url_loader.mojom.h"
yhiranof6c76cf2016-12-22 11:42:1820
21namespace base {
22class SingleThreadTaskRunner;
23} // namespace base
24
yhiranof6c76cf2016-12-22 11:42:1825namespace net {
26struct RedirectInfo;
27} // namespace net
28
Takashi Toyoshima8f988532017-11-13 07:32:3729namespace network {
Takashi Toyoshimaaa278662017-11-20 11:11:2630struct URLLoaderCompletionStatus;
Takashi Toyoshima8f988532017-11-13 07:32:3731} // namespace network
32
yhiranof6c76cf2016-12-22 11:42:1833namespace content {
34class ResourceDispatcher;
yhiranof6c76cf2016-12-22 11:42:1835
John Abd-El-Malekb165dc52018-01-18 17:12:1836class CONTENT_EXPORT URLLoaderClientImpl final
37 : public network::mojom::URLLoaderClient {
yhiranof6c76cf2016-12-22 11:42:1838 public:
39 URLLoaderClientImpl(int request_id,
40 ResourceDispatcher* resource_dispatcher,
Clark DuVallb2680c22018-08-10 15:27:2741 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
Chris Mumford90b1a0d2018-09-26 20:10:4342 bool bypass_redirect_checks,
43 const GURL& request_url);
yhiranof6c76cf2016-12-22 11:42:1844 ~URLLoaderClientImpl() override;
45
yhirano6a0e9c72016-12-27 08:31:0446 // Sets |is_deferred_|. From now, the received messages are not dispatched
47 // to clients until UnsetDefersLoading is called.
48 void SetDefersLoading();
49
50 // Unsets |is_deferred_|.
51 void UnsetDefersLoading();
52
Marijn Kruisselbrink090b1a4f2018-03-30 19:54:1253 // Dispatches the messages received after SetDefersLoading is called.
yhirano6a0e9c72016-12-27 08:31:0454 void FlushDeferredMessages();
55
arthursonzogni3a4ca9f2017-12-07 17:58:3456 // Binds this instance to the given URLLoaderClient endpoints so that it can
57 // start getting the mojo calls from the given loader. This is used only for
arthursonzogni1645d4d2018-04-24 20:28:5058 // the main resource loading. Otherwise (in regular subresource loading cases)
59 // |this| is not bound to a client request, but used via ThrottlingURLLoader
60 // to get client upcalls from the loader.
John Abd-El-Malekb165dc52018-01-18 17:12:1861 void Bind(
62 network::mojom::URLLoaderClientEndpointsPtr url_loader_client_endpoints);
arthursonzogni3a4ca9f2017-12-07 17:58:3463
John Abd-El-Malekb165dc52018-01-18 17:12:1864 // network::mojom::URLLoaderClient implementation
65 void OnReceiveResponse(
Lucas Furukawa Gadani81e294b2019-08-29 16:26:3266 network::mojom::URLResponseHeadPtr response_head) override;
John Abd-El-Malek46248032018-01-17 19:11:2367 void OnReceiveRedirect(
68 const net::RedirectInfo& redirect_info,
Lucas Furukawa Gadani81e294b2019-08-29 16:26:3269 network::mojom::URLResponseHeadPtr response_head) override;
tzikbedcbce2017-01-24 15:08:0570 void OnUploadProgress(int64_t current_position,
71 int64_t total_size,
tzikc2007562017-04-25 07:48:2472 OnUploadProgressCallback ack_callback) override;
Ben Kelly33b29c2d2019-05-08 01:41:1073 void OnReceiveCachedMetadata(mojo_base::BigBuffer data) override;
yhiranof6c76cf2016-12-22 11:42:1874 void OnTransferSizeUpdated(int32_t transfer_size_diff) override;
75 void OnStartLoadingResponseBody(
76 mojo::ScopedDataPipeConsumerHandle body) override;
Takashi Toyoshimaaa278662017-11-20 11:11:2677 void OnComplete(const network::URLLoaderCompletionStatus& status) override;
yhiranof6c76cf2016-12-22 11:42:1878
yhirano6a0e9c72016-12-27 08:31:0479 private:
Yutaka Hirano3dad2832017-12-14 06:48:1680 class DeferredMessage;
81 class DeferredOnReceiveResponse;
82 class DeferredOnReceiveRedirect;
Yutaka Hirano3dad2832017-12-14 06:48:1683 class DeferredOnUploadProgress;
84 class DeferredOnReceiveCachedMetadata;
Makoto Shimazu494ae7b52019-01-10 09:35:3285 class DeferredOnStartLoadingResponseBody;
Yutaka Hirano3dad2832017-12-14 06:48:1686 class DeferredOnComplete;
87
yhiranoe45a888f2017-04-14 01:24:2088 bool NeedsStoringMessage() const;
Yutaka Hirano3dad2832017-12-14 06:48:1689 void StoreAndDispatch(std::unique_ptr<DeferredMessage> message);
arthursonzogni3a4ca9f2017-12-07 17:58:3490 void OnConnectionClosed();
yhiranof6c76cf2016-12-22 11:42:1891
Yutaka Hirano3dad2832017-12-14 06:48:1692 std::vector<std::unique_ptr<DeferredMessage>> deferred_messages_;
yhiranof6c76cf2016-12-22 11:42:1893 const int request_id_;
Arthur Sonzognia727d4b2018-12-10 09:10:1894 bool has_received_response_head_ = false;
95 bool has_received_response_body_ = false;
arthursonzogni3a4ca9f2017-12-07 17:58:3496 bool has_received_complete_ = false;
yhirano6a0e9c72016-12-27 08:31:0497 bool is_deferred_ = false;
98 int32_t accumulated_transfer_size_diff_during_deferred_ = 0;
yhiranof6c76cf2016-12-22 11:42:1899 ResourceDispatcher* const resource_dispatcher_;
100 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
Clark DuVallb2680c22018-08-10 15:27:27101 bool bypass_redirect_checks_ = false;
Chris Mumford90b1a0d2018-09-26 20:10:43102 GURL last_loaded_url_;
arthursonzogni3a4ca9f2017-12-07 17:58:34103
Matt Falkenhagen164f9b842020-04-17 11:39:08104 // For UMA.
105 base::TimeTicks on_receive_response_time_;
106
Julie Jeongeun Kim917c197902019-11-26 03:36:14107 mojo::Remote<network::mojom::URLLoader> url_loader_;
Julie Jeongeun Kim028b2c92019-11-08 02:51:16108 mojo::Receiver<network::mojom::URLLoaderClient> url_loader_client_receiver_{
109 this};
arthursonzogni3a4ca9f2017-12-07 17:58:34110
Jeremy Roman3bca4bf2019-07-11 03:41:25111 base::WeakPtrFactory<URLLoaderClientImpl> weak_factory_{this};
yhiranof6c76cf2016-12-22 11:42:18112};
113
114} // namespace content
115
John Abd-El-Malek6b56ef712017-10-21 22:52:46116#endif // CONTENT_RENDERER_LOADER_URL_LOADER_CLIENT_IMPL_H_