blob: 80ff3b716ec95a313d63f470c4da647c87f35b88 [file] [log] [blame]
yhiranof6c76cf2016-12-22 11:42:181// 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>
yhirano6a0e9c72016-12-27 08:31:049#include <vector>
yhiranof6c76cf2016-12-22 11:42:1810#include "base/callback_forward.h"
11#include "base/memory/ref_counted.h"
yhirano6a0e9c72016-12-27 08:31:0412#include "base/memory/weak_ptr.h"
yhiranof6c76cf2016-12-22 11:42:1813#include "content/common/content_export.h"
14#include "content/common/url_loader.mojom.h"
15#include "ipc/ipc_message.h"
yhirano6f57b502017-03-01 03:44:2016#include "mojo/public/cpp/bindings/binding.h"
yhiranof6c76cf2016-12-22 11:42:1817#include "mojo/public/cpp/system/data_pipe.h"
18
19namespace base {
20class SingleThreadTaskRunner;
21} // namespace base
22
yhiranof6c76cf2016-12-22 11:42:1823namespace net {
24struct RedirectInfo;
25} // namespace net
26
27namespace content {
28class ResourceDispatcher;
29class URLResponseBodyConsumer;
30struct ResourceRequestCompletionStatus;
31struct ResourceResponseHead;
32
33class 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
yhirano6f57b502017-03-01 03:44:2040 void Bind(mojom::URLLoaderClientPtr* client_ptr);
yhiranof6c76cf2016-12-22 11:42:1841
yhirano6a0e9c72016-12-27 08:31:0442 // 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
yhiranof6c76cf2016-12-22 11:42:1852 // mojom::URLLoaderClient implementation
yhirano6f57b502017-03-01 03:44:2053 void OnReceiveResponse(const ResourceResponseHead& response_head,
54 mojom::DownloadedTempFilePtr downloaded_file) override;
yhiranof6c76cf2016-12-22 11:42:1855 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;
tzikbedcbce2017-01-24 15:08:0558 void OnUploadProgress(int64_t current_position,
59 int64_t total_size,
60 const base::Closure& ack_callback) override;
yhirano2a525f92017-01-18 08:16:4661 void OnReceiveCachedMetadata(const std::vector<uint8_t>& data) override;
yhiranof6c76cf2016-12-22 11:42:1862 void OnTransferSizeUpdated(int32_t transfer_size_diff) override;
63 void OnStartLoadingResponseBody(
64 mojo::ScopedDataPipeConsumerHandle body) override;
65 void OnComplete(const ResourceRequestCompletionStatus& status) override;
66
yhirano6a0e9c72016-12-27 08:31:0467 private:
yhiranoe45a888f2017-04-14 01:24:2068 bool NeedsStoringMessage() const;
69 void StoreAndDispatch(const IPC::Message& message);
yhiranof6c76cf2016-12-22 11:42:1870
yhirano6f57b502017-03-01 03:44:2071 mojo::Binding<mojom::URLLoaderClient> binding_;
yhiranof6c76cf2016-12-22 11:42:1872 scoped_refptr<URLResponseBodyConsumer> body_consumer_;
yhirano6f57b502017-03-01 03:44:2073 mojom::DownloadedTempFilePtr downloaded_file_;
yhirano6a0e9c72016-12-27 08:31:0474 std::vector<IPC::Message> deferred_messages_;
yhiranof6c76cf2016-12-22 11:42:1875 const int request_id_;
76 bool has_received_response_ = false;
yhirano6a0e9c72016-12-27 08:31:0477 bool is_deferred_ = false;
78 int32_t accumulated_transfer_size_diff_during_deferred_ = 0;
yhiranof6c76cf2016-12-22 11:42:1879 ResourceDispatcher* const resource_dispatcher_;
80 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
yhirano6a0e9c72016-12-27 08:31:0481 base::WeakPtrFactory<URLLoaderClientImpl> weak_factory_;
yhiranof6c76cf2016-12-22 11:42:1882};
83
84} // namespace content
85
86#endif // CONTENT_CHILD_URL_LOADER_CLIENT_IMPL_H_