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, |
| 40 | scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
| 41 | ~URLLoaderClientImpl() override; |
| 42 | |
yhirano | 6a0e9c7 | 2016-12-27 08:31:04 | [diff] [blame] | 43 | // 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 | |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame] | 53 | // 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-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 59 | void Bind( |
| 60 | network::mojom::URLLoaderClientEndpointsPtr url_loader_client_endpoints); |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame] | 61 | |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 62 | // 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-Malek | 4624803 | 2018-01-17 19:11:23 | [diff] [blame] | 67 | void OnReceiveRedirect( |
| 68 | const net::RedirectInfo& redirect_info, |
| 69 | const network::ResourceResponseHead& response_head) override; |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 70 | void OnDataDownloaded(int64_t data_len, int64_t encoded_data_len) override; |
tzik | bedcbce | 2017-01-24 15:08:05 | [diff] [blame] | 71 | void OnUploadProgress(int64_t current_position, |
| 72 | int64_t total_size, |
tzik | c200756 | 2017-04-25 07:48:24 | [diff] [blame] | 73 | OnUploadProgressCallback ack_callback) override; |
yhirano | 2a525f9 | 2017-01-18 08:16:46 | [diff] [blame] | 74 | void OnReceiveCachedMetadata(const std::vector<uint8_t>& data) override; |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 75 | void OnTransferSizeUpdated(int32_t transfer_size_diff) override; |
| 76 | void OnStartLoadingResponseBody( |
| 77 | mojo::ScopedDataPipeConsumerHandle body) override; |
Takashi Toyoshima | aa27866 | 2017-11-20 11:11:26 | [diff] [blame] | 78 | void OnComplete(const network::URLLoaderCompletionStatus& status) override; |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 79 | |
yhirano | 6a0e9c7 | 2016-12-27 08:31:04 | [diff] [blame] | 80 | private: |
Yutaka Hirano | 3dad283 | 2017-12-14 06:48:16 | [diff] [blame] | 81 | class DeferredMessage; |
| 82 | class DeferredOnReceiveResponse; |
| 83 | class DeferredOnReceiveRedirect; |
| 84 | class DeferredOnDataDownloaded; |
| 85 | class DeferredOnUploadProgress; |
| 86 | class DeferredOnReceiveCachedMetadata; |
| 87 | class DeferredOnComplete; |
| 88 | |
yhirano | e45a888f | 2017-04-14 01:24:20 | [diff] [blame] | 89 | bool NeedsStoringMessage() const; |
Yutaka Hirano | 3dad283 | 2017-12-14 06:48:16 | [diff] [blame] | 90 | void StoreAndDispatch(std::unique_ptr<DeferredMessage> message); |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame] | 91 | void OnConnectionClosed(); |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 92 | |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 93 | scoped_refptr<URLResponseBodyConsumer> body_consumer_; |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 94 | network::mojom::DownloadedTempFilePtr downloaded_file_; |
Yutaka Hirano | 3dad283 | 2017-12-14 06:48:16 | [diff] [blame] | 95 | std::vector<std::unique_ptr<DeferredMessage>> deferred_messages_; |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 96 | const int request_id_; |
| 97 | bool has_received_response_ = false; |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame] | 98 | bool has_received_complete_ = false; |
yhirano | 6a0e9c7 | 2016-12-27 08:31:04 | [diff] [blame] | 99 | bool is_deferred_ = false; |
| 100 | int32_t accumulated_transfer_size_diff_during_deferred_ = 0; |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 101 | ResourceDispatcher* const resource_dispatcher_; |
| 102 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame] | 103 | |
| 104 | // Used in NavigationMojoResponse and NetworkService. |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 105 | network::mojom::URLLoaderPtr url_loader_; |
| 106 | mojo::Binding<network::mojom::URLLoaderClient> url_loader_client_binding_; |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame] | 107 | |
yhirano | 6a0e9c7 | 2016-12-27 08:31:04 | [diff] [blame] | 108 | base::WeakPtrFactory<URLLoaderClientImpl> weak_factory_; |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | } // namespace content |
| 112 | |
John Abd-El-Malek | 6b56ef71 | 2017-10-21 22:52:46 | [diff] [blame] | 113 | #endif // CONTENT_RENDERER_LOADER_URL_LOADER_CLIENT_IMPL_H_ |