yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 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 | |
John Abd-El-Malek | 6b56ef71 | 2017-10-21 22:52:46 | [diff] [blame] | 5 | #ifndef CONTENT_RENDERER_LOADER_URL_LOADER_CLIENT_IMPL_H_ |
| 6 | #define CONTENT_RENDERER_LOADER_URL_LOADER_CLIENT_IMPL_H_ |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 7 | |
| 8 | #include <stdint.h> |
yhirano | 6a0e9c7 | 2016-12-27 08:31:04 | [diff] [blame] | 9 | #include <vector> |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 10 | #include "base/callback_forward.h" |
| 11 | #include "base/memory/ref_counted.h" |
yhirano | 6a0e9c7 | 2016-12-27 08:31:04 | [diff] [blame] | 12 | #include "base/memory/weak_ptr.h" |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 13 | #include "content/common/content_export.h" |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame] | 14 | #include "mojo/public/cpp/bindings/binding.h" |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 15 | #include "mojo/public/cpp/system/data_pipe.h" |
Ken Rockot | 54311e6 | 2018-02-10 19:01:52 | [diff] [blame] | 16 | #include "services/network/public/mojom/url_loader.mojom.h" |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 17 | |
| 18 | namespace base { |
| 19 | class SingleThreadTaskRunner; |
| 20 | } // namespace base |
| 21 | |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 22 | namespace net { |
| 23 | struct RedirectInfo; |
| 24 | } // namespace net |
| 25 | |
Takashi Toyoshima | 8f98853 | 2017-11-13 07:32:37 | [diff] [blame] | 26 | namespace network { |
John Abd-El-Malek | 4624803 | 2018-01-17 19:11:23 | [diff] [blame] | 27 | struct ResourceResponseHead; |
Takashi Toyoshima | aa27866 | 2017-11-20 11:11:26 | [diff] [blame] | 28 | struct URLLoaderCompletionStatus; |
Takashi Toyoshima | 8f98853 | 2017-11-13 07:32:37 | [diff] [blame] | 29 | } // namespace network |
| 30 | |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 31 | namespace content { |
| 32 | class ResourceDispatcher; |
| 33 | class URLResponseBodyConsumer; |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 34 | |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 35 | class CONTENT_EXPORT URLLoaderClientImpl final |
| 36 | : public network::mojom::URLLoaderClient { |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 37 | public: |
| 38 | URLLoaderClientImpl(int request_id, |
| 39 | ResourceDispatcher* resource_dispatcher, |
Clark DuVall | b2680c2 | 2018-08-10 15:27:27 | [diff] [blame] | 40 | scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
Chris Mumford | 90b1a0d | 2018-09-26 20:10:43 | [diff] [blame] | 41 | bool bypass_redirect_checks, |
| 42 | const GURL& request_url); |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 43 | ~URLLoaderClientImpl() override; |
| 44 | |
yhirano | 6a0e9c7 | 2016-12-27 08:31:04 | [diff] [blame] | 45 | // 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 Kruisselbrink | 090b1a4f | 2018-03-30 19:54:12 | [diff] [blame] | 52 | // Dispatches the messages received after SetDefersLoading is called. |
yhirano | 6a0e9c7 | 2016-12-27 08:31:04 | [diff] [blame] | 53 | void FlushDeferredMessages(); |
| 54 | |
Marijn Kruisselbrink | 090b1a4f | 2018-03-30 19:54:12 | [diff] [blame] | 55 | // 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 Orlovich | 5031c5d | 2018-10-26 12:41:44 | [diff] [blame] | 58 | // from the body to the dispatcher. |
Marijn Kruisselbrink | 090b1a4f | 2018-03-30 19:54:12 | [diff] [blame] | 59 | void SetPassResponsePipeToDispatcher(bool pass_pipe) { |
Maks Orlovich | 5031c5d | 2018-10-26 12:41:44 | [diff] [blame] | 60 | pass_response_pipe_to_dispatcher_ = pass_pipe; |
Marijn Kruisselbrink | 090b1a4f | 2018-03-30 19:54:12 | [diff] [blame] | 61 | } |
| 62 | |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame] | 63 | // 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 |
arthursonzogni | 1645d4d | 2018-04-24 20:28:50 | [diff] [blame] | 65 | // 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-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 68 | void Bind( |
| 69 | network::mojom::URLLoaderClientEndpointsPtr url_loader_client_endpoints); |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame] | 70 | |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 71 | // network::mojom::URLLoaderClient implementation |
| 72 | void OnReceiveResponse( |
Marijn Kruisselbrink | 9ebd7ba | 2018-06-11 23:18:04 | [diff] [blame] | 73 | const network::ResourceResponseHead& response_head) override; |
John Abd-El-Malek | 4624803 | 2018-01-17 19:11:23 | [diff] [blame] | 74 | void OnReceiveRedirect( |
| 75 | const net::RedirectInfo& redirect_info, |
| 76 | const network::ResourceResponseHead& response_head) override; |
tzik | bedcbce | 2017-01-24 15:08:05 | [diff] [blame] | 77 | void OnUploadProgress(int64_t current_position, |
| 78 | int64_t total_size, |
tzik | c200756 | 2017-04-25 07:48:24 | [diff] [blame] | 79 | OnUploadProgressCallback ack_callback) override; |
yhirano | 2a525f9 | 2017-01-18 08:16:46 | [diff] [blame] | 80 | void OnReceiveCachedMetadata(const std::vector<uint8_t>& data) override; |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 81 | void OnTransferSizeUpdated(int32_t transfer_size_diff) override; |
| 82 | void OnStartLoadingResponseBody( |
| 83 | mojo::ScopedDataPipeConsumerHandle body) override; |
Takashi Toyoshima | aa27866 | 2017-11-20 11:11:26 | [diff] [blame] | 84 | void OnComplete(const network::URLLoaderCompletionStatus& status) override; |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 85 | |
yhirano | 6a0e9c7 | 2016-12-27 08:31:04 | [diff] [blame] | 86 | private: |
Yutaka Hirano | 3dad283 | 2017-12-14 06:48:16 | [diff] [blame] | 87 | class DeferredMessage; |
| 88 | class DeferredOnReceiveResponse; |
| 89 | class DeferredOnReceiveRedirect; |
Yutaka Hirano | 3dad283 | 2017-12-14 06:48:16 | [diff] [blame] | 90 | class DeferredOnUploadProgress; |
| 91 | class DeferredOnReceiveCachedMetadata; |
Makoto Shimazu | 494ae7b5 | 2019-01-10 09:35:32 | [diff] [blame^] | 92 | class DeferredOnStartLoadingResponseBody; |
Yutaka Hirano | 3dad283 | 2017-12-14 06:48:16 | [diff] [blame] | 93 | class DeferredOnComplete; |
| 94 | |
yhirano | e45a888f | 2017-04-14 01:24:20 | [diff] [blame] | 95 | bool NeedsStoringMessage() const; |
Yutaka Hirano | 3dad283 | 2017-12-14 06:48:16 | [diff] [blame] | 96 | void StoreAndDispatch(std::unique_ptr<DeferredMessage> message); |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame] | 97 | void OnConnectionClosed(); |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 98 | |
Makoto Shimazu | 494ae7b5 | 2019-01-10 09:35:32 | [diff] [blame^] | 99 | // 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. |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 103 | scoped_refptr<URLResponseBodyConsumer> body_consumer_; |
Makoto Shimazu | 494ae7b5 | 2019-01-10 09:35:32 | [diff] [blame^] | 104 | |
Yutaka Hirano | 3dad283 | 2017-12-14 06:48:16 | [diff] [blame] | 105 | std::vector<std::unique_ptr<DeferredMessage>> deferred_messages_; |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 106 | const int request_id_; |
Arthur Sonzogni | a727d4b | 2018-12-10 09:10:18 | [diff] [blame] | 107 | bool has_received_response_head_ = false; |
| 108 | bool has_received_response_body_ = false; |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame] | 109 | bool has_received_complete_ = false; |
yhirano | 6a0e9c7 | 2016-12-27 08:31:04 | [diff] [blame] | 110 | bool is_deferred_ = false; |
Marijn Kruisselbrink | 090b1a4f | 2018-03-30 19:54:12 | [diff] [blame] | 111 | bool pass_response_pipe_to_dispatcher_ = false; |
yhirano | 6a0e9c7 | 2016-12-27 08:31:04 | [diff] [blame] | 112 | int32_t accumulated_transfer_size_diff_during_deferred_ = 0; |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 113 | ResourceDispatcher* const resource_dispatcher_; |
| 114 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
Clark DuVall | b2680c2 | 2018-08-10 15:27:27 | [diff] [blame] | 115 | bool bypass_redirect_checks_ = false; |
Chris Mumford | 90b1a0d | 2018-09-26 20:10:43 | [diff] [blame] | 116 | GURL last_loaded_url_; |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame] | 117 | |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 118 | network::mojom::URLLoaderPtr url_loader_; |
| 119 | mojo::Binding<network::mojom::URLLoaderClient> url_loader_client_binding_; |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame] | 120 | |
yhirano | 6a0e9c7 | 2016-12-27 08:31:04 | [diff] [blame] | 121 | base::WeakPtrFactory<URLLoaderClientImpl> weak_factory_; |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | } // namespace content |
| 125 | |
John Abd-El-Malek | 6b56ef71 | 2017-10-21 22:52:46 | [diff] [blame] | 126 | #endif // CONTENT_RENDERER_LOADER_URL_LOADER_CLIENT_IMPL_H_ |