blob: d42044af045ef3bd7c14d896bc4c6dbd50628e2f [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,
40 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
41 ~URLLoaderClientImpl() override;
42
yhirano6a0e9c72016-12-27 08:31:0443 // Sets |is_deferred_|. From now, the received messages are not dispatched
44 // to clients until UnsetDefersLoading is called.
45 void SetDefersLoading();
46
47 // Unsets |is_deferred_|.
48 void UnsetDefersLoading();
49
50 // Disaptches the messages received after SetDefersLoading is called.
51 void FlushDeferredMessages();
52
arthursonzogni3a4ca9f2017-12-07 17:58:3453 // Binds this instance to the given URLLoaderClient endpoints so that it can
54 // start getting the mojo calls from the given loader. This is used only for
55 // the main resource loading when NavigationMojoResponse and/or NetworkService
56 // is enabled. Otherwise (in regular subresource loading cases) |this| is not
57 // bound to a client request, but used via ThrottlingURLLoader to get client
58 // upcalls from the loader.
John Abd-El-Malekb165dc52018-01-18 17:12:1859 void Bind(
60 network::mojom::URLLoaderClientEndpointsPtr url_loader_client_endpoints);
arthursonzogni3a4ca9f2017-12-07 17:58:3461
John Abd-El-Malekb165dc52018-01-18 17:12:1862 // network::mojom::URLLoaderClient implementation
63 void OnReceiveResponse(
64 const network::ResourceResponseHead& response_head,
65 const base::Optional<net::SSLInfo>& ssl_info,
66 network::mojom::DownloadedTempFilePtr downloaded_file) override;
John Abd-El-Malek46248032018-01-17 19:11:2367 void OnReceiveRedirect(
68 const net::RedirectInfo& redirect_info,
69 const network::ResourceResponseHead& response_head) override;
yhiranof6c76cf2016-12-22 11:42:1870 void OnDataDownloaded(int64_t data_len, int64_t encoded_data_len) override;
tzikbedcbce2017-01-24 15:08:0571 void OnUploadProgress(int64_t current_position,
72 int64_t total_size,
tzikc2007562017-04-25 07:48:2473 OnUploadProgressCallback ack_callback) override;
yhirano2a525f92017-01-18 08:16:4674 void OnReceiveCachedMetadata(const std::vector<uint8_t>& data) override;
yhiranof6c76cf2016-12-22 11:42:1875 void OnTransferSizeUpdated(int32_t transfer_size_diff) override;
76 void OnStartLoadingResponseBody(
77 mojo::ScopedDataPipeConsumerHandle body) override;
Takashi Toyoshimaaa278662017-11-20 11:11:2678 void OnComplete(const network::URLLoaderCompletionStatus& status) override;
yhiranof6c76cf2016-12-22 11:42:1879
yhirano6a0e9c72016-12-27 08:31:0480 private:
Yutaka Hirano3dad2832017-12-14 06:48:1681 class DeferredMessage;
82 class DeferredOnReceiveResponse;
83 class DeferredOnReceiveRedirect;
84 class DeferredOnDataDownloaded;
85 class DeferredOnUploadProgress;
86 class DeferredOnReceiveCachedMetadata;
87 class DeferredOnComplete;
88
yhiranoe45a888f2017-04-14 01:24:2089 bool NeedsStoringMessage() const;
Yutaka Hirano3dad2832017-12-14 06:48:1690 void StoreAndDispatch(std::unique_ptr<DeferredMessage> message);
arthursonzogni3a4ca9f2017-12-07 17:58:3491 void OnConnectionClosed();
yhiranof6c76cf2016-12-22 11:42:1892
yhiranof6c76cf2016-12-22 11:42:1893 scoped_refptr<URLResponseBodyConsumer> body_consumer_;
John Abd-El-Malekb165dc52018-01-18 17:12:1894 network::mojom::DownloadedTempFilePtr downloaded_file_;
Yutaka Hirano3dad2832017-12-14 06:48:1695 std::vector<std::unique_ptr<DeferredMessage>> deferred_messages_;
yhiranof6c76cf2016-12-22 11:42:1896 const int request_id_;
97 bool has_received_response_ = false;
arthursonzogni3a4ca9f2017-12-07 17:58:3498 bool has_received_complete_ = false;
yhirano6a0e9c72016-12-27 08:31:0499 bool is_deferred_ = false;
100 int32_t accumulated_transfer_size_diff_during_deferred_ = 0;
yhiranof6c76cf2016-12-22 11:42:18101 ResourceDispatcher* const resource_dispatcher_;
102 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
arthursonzogni3a4ca9f2017-12-07 17:58:34103
104 // Used in NavigationMojoResponse and NetworkService.
John Abd-El-Malekb165dc52018-01-18 17:12:18105 network::mojom::URLLoaderPtr url_loader_;
106 mojo::Binding<network::mojom::URLLoaderClient> url_loader_client_binding_;
arthursonzogni3a4ca9f2017-12-07 17:58:34107
yhirano6a0e9c72016-12-27 08:31:04108 base::WeakPtrFactory<URLLoaderClientImpl> weak_factory_;
yhiranof6c76cf2016-12-22 11:42:18109};
110
111} // namespace content
112
John Abd-El-Malek6b56ef712017-10-21 22:52:46113#endif // CONTENT_RENDERER_LOADER_URL_LOADER_CLIENT_IMPL_H_