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" |
| 16 | #include "mojo/public/cpp/bindings/associated_binding.h" |
| 17 | #include "mojo/public/cpp/system/data_pipe.h" |
| 18 | |
| 19 | namespace base { |
| 20 | class SingleThreadTaskRunner; |
| 21 | } // namespace base |
| 22 | |
| 23 | namespace mojo { |
| 24 | class AssociatedGroup; |
| 25 | } // namespace mojo |
| 26 | |
| 27 | namespace net { |
| 28 | struct RedirectInfo; |
| 29 | } // namespace net |
| 30 | |
| 31 | namespace content { |
| 32 | class ResourceDispatcher; |
| 33 | class URLResponseBodyConsumer; |
| 34 | struct ResourceRequestCompletionStatus; |
| 35 | struct ResourceResponseHead; |
| 36 | |
| 37 | class CONTENT_EXPORT URLLoaderClientImpl final : public mojom::URLLoaderClient { |
| 38 | public: |
| 39 | URLLoaderClientImpl(int request_id, |
| 40 | ResourceDispatcher* resource_dispatcher, |
| 41 | scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
| 42 | ~URLLoaderClientImpl() override; |
| 43 | |
| 44 | void Bind(mojom::URLLoaderClientAssociatedPtrInfo* client_ptr_info, |
| 45 | mojo::AssociatedGroup* associated_group); |
| 46 | |
yhirano | 6a0e9c7 | 2016-12-27 08:31:04 | [diff] [blame^] | 47 | // Sets |is_deferred_|. From now, the received messages are not dispatched |
| 48 | // to clients until UnsetDefersLoading is called. |
| 49 | void SetDefersLoading(); |
| 50 | |
| 51 | // Unsets |is_deferred_|. |
| 52 | void UnsetDefersLoading(); |
| 53 | |
| 54 | // Disaptches the messages received after SetDefersLoading is called. |
| 55 | void FlushDeferredMessages(); |
| 56 | |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 57 | // mojom::URLLoaderClient implementation |
| 58 | void OnReceiveResponse(const ResourceResponseHead& response_head, |
| 59 | mojom::DownloadedTempFilePtr downloaded_file) override; |
| 60 | void OnReceiveRedirect(const net::RedirectInfo& redirect_info, |
| 61 | const ResourceResponseHead& response_head) override; |
| 62 | void OnDataDownloaded(int64_t data_len, int64_t encoded_data_len) override; |
| 63 | void OnTransferSizeUpdated(int32_t transfer_size_diff) override; |
| 64 | void OnStartLoadingResponseBody( |
| 65 | mojo::ScopedDataPipeConsumerHandle body) override; |
| 66 | void OnComplete(const ResourceRequestCompletionStatus& status) override; |
| 67 | |
yhirano | 6a0e9c7 | 2016-12-27 08:31:04 | [diff] [blame^] | 68 | private: |
yhirano | f6c76cf | 2016-12-22 11:42:18 | [diff] [blame] | 69 | void Dispatch(const IPC::Message& message); |
| 70 | |
| 71 | mojo::AssociatedBinding<mojom::URLLoaderClient> binding_; |
| 72 | scoped_refptr<URLResponseBodyConsumer> body_consumer_; |
| 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_ |