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" |
mmenke | 680c214 | 2017-07-05 19:08:56 | [diff] [blame] | 14 | #include "content/public/common/url_loader.mojom.h" |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame] | 15 | #include "mojo/public/cpp/bindings/binding.h" |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 16 | #include "mojo/public/cpp/system/data_pipe.h" |
| 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 { |
Takashi Toyoshima | aa27866 | 2017-11-20 11:11:26 | [diff] [blame] | 27 | struct URLLoaderCompletionStatus; |
Takashi Toyoshima | 8f98853 | 2017-11-13 07:32:37 | [diff] [blame] | 28 | } // namespace network |
| 29 | |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 30 | namespace content { |
| 31 | class ResourceDispatcher; |
| 32 | class URLResponseBodyConsumer; |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 33 | struct ResourceResponseHead; |
| 34 | |
| 35 | class CONTENT_EXPORT URLLoaderClientImpl final : public mojom::URLLoaderClient { |
| 36 | public: |
| 37 | URLLoaderClientImpl(int request_id, |
| 38 | ResourceDispatcher* resource_dispatcher, |
| 39 | scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
| 40 | ~URLLoaderClientImpl() override; |
| 41 | |
yhirano | 6a0e9c7 | 2016-12-27 08:31:04 | [diff] [blame] | 42 | // Sets |is_deferred_|. From now, the received messages are not dispatched |
| 43 | // to clients until UnsetDefersLoading is called. |
| 44 | void SetDefersLoading(); |
| 45 | |
| 46 | // Unsets |is_deferred_|. |
| 47 | void UnsetDefersLoading(); |
| 48 | |
| 49 | // Disaptches the messages received after SetDefersLoading is called. |
| 50 | void FlushDeferredMessages(); |
| 51 | |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame] | 52 | // Binds this instance to the given URLLoaderClient endpoints so that it can |
| 53 | // start getting the mojo calls from the given loader. This is used only for |
| 54 | // the main resource loading when NavigationMojoResponse and/or NetworkService |
| 55 | // is enabled. Otherwise (in regular subresource loading cases) |this| is not |
| 56 | // bound to a client request, but used via ThrottlingURLLoader to get client |
| 57 | // upcalls from the loader. |
| 58 | void Bind(mojom::URLLoaderClientEndpointsPtr url_loader_client_endpoints); |
| 59 | |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 60 | // mojom::URLLoaderClient implementation |
yhirano | 6f57b50 | 2017-03-01 03:44:20 | [diff] [blame] | 61 | void OnReceiveResponse(const ResourceResponseHead& response_head, |
jam | 33d897e | 2017-04-14 21:28:46 | [diff] [blame] | 62 | const base::Optional<net::SSLInfo>& ssl_info, |
yhirano | 6f57b50 | 2017-03-01 03:44:20 | [diff] [blame] | 63 | mojom::DownloadedTempFilePtr downloaded_file) override; |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 64 | void OnReceiveRedirect(const net::RedirectInfo& redirect_info, |
| 65 | const ResourceResponseHead& response_head) override; |
| 66 | void OnDataDownloaded(int64_t data_len, int64_t encoded_data_len) override; |
tzik | bedcbce | 2017-01-24 15:08:05 | [diff] [blame] | 67 | void OnUploadProgress(int64_t current_position, |
| 68 | int64_t total_size, |
tzik | c200756 | 2017-04-25 07:48:24 | [diff] [blame] | 69 | OnUploadProgressCallback ack_callback) override; |
yhirano | 2a525f9 | 2017-01-18 08:16:46 | [diff] [blame] | 70 | void OnReceiveCachedMetadata(const std::vector<uint8_t>& data) override; |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 71 | void OnTransferSizeUpdated(int32_t transfer_size_diff) override; |
| 72 | void OnStartLoadingResponseBody( |
| 73 | mojo::ScopedDataPipeConsumerHandle body) override; |
Takashi Toyoshima | aa27866 | 2017-11-20 11:11:26 | [diff] [blame] | 74 | void OnComplete(const network::URLLoaderCompletionStatus& status) override; |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 75 | |
yhirano | 6a0e9c7 | 2016-12-27 08:31:04 | [diff] [blame] | 76 | private: |
Yutaka Hirano | 3dad283 | 2017-12-14 06:48:16 | [diff] [blame] | 77 | class DeferredMessage; |
| 78 | class DeferredOnReceiveResponse; |
| 79 | class DeferredOnReceiveRedirect; |
| 80 | class DeferredOnDataDownloaded; |
| 81 | class DeferredOnUploadProgress; |
| 82 | class DeferredOnReceiveCachedMetadata; |
| 83 | class DeferredOnComplete; |
| 84 | |
yhirano | e45a888f | 2017-04-14 01:24:20 | [diff] [blame] | 85 | bool NeedsStoringMessage() const; |
Yutaka Hirano | 3dad283 | 2017-12-14 06:48:16 | [diff] [blame] | 86 | void StoreAndDispatch(std::unique_ptr<DeferredMessage> message); |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame] | 87 | void OnConnectionClosed(); |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 88 | |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 89 | scoped_refptr<URLResponseBodyConsumer> body_consumer_; |
yhirano | 6f57b50 | 2017-03-01 03:44:20 | [diff] [blame] | 90 | mojom::DownloadedTempFilePtr downloaded_file_; |
Yutaka Hirano | 3dad283 | 2017-12-14 06:48:16 | [diff] [blame] | 91 | std::vector<std::unique_ptr<DeferredMessage>> deferred_messages_; |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 92 | const int request_id_; |
| 93 | bool has_received_response_ = false; |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame] | 94 | bool has_received_complete_ = false; |
yhirano | 6a0e9c7 | 2016-12-27 08:31:04 | [diff] [blame] | 95 | bool is_deferred_ = false; |
| 96 | int32_t accumulated_transfer_size_diff_during_deferred_ = 0; |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 97 | ResourceDispatcher* const resource_dispatcher_; |
| 98 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame] | 99 | |
| 100 | // Used in NavigationMojoResponse and NetworkService. |
| 101 | mojom::URLLoaderPtr url_loader_; |
| 102 | mojo::Binding<mojom::URLLoaderClient> url_loader_client_binding_; |
| 103 | |
yhirano | 6a0e9c7 | 2016-12-27 08:31:04 | [diff] [blame] | 104 | base::WeakPtrFactory<URLLoaderClientImpl> weak_factory_; |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | } // namespace content |
| 108 | |
John Abd-El-Malek | 6b56ef71 | 2017-10-21 22:52:46 | [diff] [blame] | 109 | #endif // CONTENT_RENDERER_LOADER_URL_LOADER_CLIENT_IMPL_H_ |