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 | |
| 5 | #ifndef CONTENT_CHILD_URL_LOADER_CLIENT_IMPL_H_ |
| 6 | #define CONTENT_CHILD_URL_LOADER_CLIENT_IMPL_H_ |
| 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" |
| 14 | #include "content/common/url_loader.mojom.h" |
| 15 | #include "ipc/ipc_message.h" |
yhirano | 6f57b50 | 2017-03-01 03:44:20 | [diff] [blame] | 16 | #include "mojo/public/cpp/bindings/binding.h" |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 17 | #include "mojo/public/cpp/system/data_pipe.h" |
| 18 | |
| 19 | namespace base { |
| 20 | class SingleThreadTaskRunner; |
| 21 | } // namespace base |
| 22 | |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 23 | namespace net { |
| 24 | struct RedirectInfo; |
| 25 | } // namespace net |
| 26 | |
| 27 | namespace content { |
| 28 | class ResourceDispatcher; |
| 29 | class URLResponseBodyConsumer; |
| 30 | struct ResourceRequestCompletionStatus; |
| 31 | struct ResourceResponseHead; |
| 32 | |
| 33 | class CONTENT_EXPORT URLLoaderClientImpl final : public mojom::URLLoaderClient { |
| 34 | public: |
| 35 | URLLoaderClientImpl(int request_id, |
| 36 | ResourceDispatcher* resource_dispatcher, |
| 37 | scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
| 38 | ~URLLoaderClientImpl() override; |
| 39 | |
yhirano | 6f57b50 | 2017-03-01 03:44:20 | [diff] [blame] | 40 | void Bind(mojom::URLLoaderClientPtr* client_ptr); |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 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 | |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 52 | // mojom::URLLoaderClient implementation |
yhirano | 6f57b50 | 2017-03-01 03:44:20 | [diff] [blame] | 53 | void OnReceiveResponse(const ResourceResponseHead& response_head, |
| 54 | mojom::DownloadedTempFilePtr downloaded_file) override; |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 55 | void OnReceiveRedirect(const net::RedirectInfo& redirect_info, |
| 56 | const ResourceResponseHead& response_head) override; |
| 57 | void OnDataDownloaded(int64_t data_len, int64_t encoded_data_len) override; |
tzik | bedcbce | 2017-01-24 15:08:05 | [diff] [blame] | 58 | void OnUploadProgress(int64_t current_position, |
| 59 | int64_t total_size, |
| 60 | const base::Closure& ack_callback) override; |
yhirano | 2a525f9 | 2017-01-18 08:16:46 | [diff] [blame] | 61 | void OnReceiveCachedMetadata(const std::vector<uint8_t>& data) override; |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 62 | void OnTransferSizeUpdated(int32_t transfer_size_diff) override; |
| 63 | void OnStartLoadingResponseBody( |
| 64 | mojo::ScopedDataPipeConsumerHandle body) override; |
| 65 | void OnComplete(const ResourceRequestCompletionStatus& status) override; |
| 66 | |
yhirano | 6a0e9c7 | 2016-12-27 08:31:04 | [diff] [blame] | 67 | private: |
yhirano | e45a888f | 2017-04-14 01:24:20 | [diff] [blame^] | 68 | bool NeedsStoringMessage() const; |
| 69 | void StoreAndDispatch(const IPC::Message& message); |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 70 | |
yhirano | 6f57b50 | 2017-03-01 03:44:20 | [diff] [blame] | 71 | mojo::Binding<mojom::URLLoaderClient> binding_; |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 72 | scoped_refptr<URLResponseBodyConsumer> body_consumer_; |
yhirano | 6f57b50 | 2017-03-01 03:44:20 | [diff] [blame] | 73 | mojom::DownloadedTempFilePtr downloaded_file_; |
yhirano | 6a0e9c7 | 2016-12-27 08:31:04 | [diff] [blame] | 74 | std::vector<IPC::Message> deferred_messages_; |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 75 | const int request_id_; |
| 76 | bool has_received_response_ = false; |
yhirano | 6a0e9c7 | 2016-12-27 08:31:04 | [diff] [blame] | 77 | bool is_deferred_ = false; |
| 78 | int32_t accumulated_transfer_size_diff_during_deferred_ = 0; |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 79 | ResourceDispatcher* const resource_dispatcher_; |
| 80 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
yhirano | 6a0e9c7 | 2016-12-27 08:31:04 | [diff] [blame] | 81 | base::WeakPtrFactory<URLLoaderClientImpl> weak_factory_; |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | } // namespace content |
| 85 | |
| 86 | #endif // CONTENT_CHILD_URL_LOADER_CLIENT_IMPL_H_ |