blob: 59926bb419646336ad9e47ca4e5be0878f5fa2e3 [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
John Abd-El-Malek6b56ef712017-10-21 22:52:465#ifndef CONTENT_RENDERER_LOADER_URL_LOADER_CLIENT_IMPL_H_
6#define CONTENT_RENDERER_LOADER_URL_LOADER_CLIENT_IMPL_H_
yhiranof6c76cf2016-12-22 11:42:187
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"
mmenke680c2142017-07-05 19:08:5614#include "content/public/common/url_loader.mojom.h"
arthursonzogni3a4ca9f2017-12-07 17:58:3415#include "mojo/public/cpp/bindings/binding.h"
yhiranof6c76cf2016-12-22 11:42:1816#include "mojo/public/cpp/system/data_pipe.h"
17
18namespace base {
19class SingleThreadTaskRunner;
20} // namespace base
21
yhiranof6c76cf2016-12-22 11:42:1822namespace net {
23struct RedirectInfo;
24} // namespace net
25
Takashi Toyoshima8f988532017-11-13 07:32:3726namespace network {
John Abd-El-Malek46248032018-01-17 19:11:2327struct ResourceResponseHead;
Takashi Toyoshimaaa278662017-11-20 11:11:2628struct URLLoaderCompletionStatus;
Takashi Toyoshima8f988532017-11-13 07:32:3729} // namespace network
30
yhiranof6c76cf2016-12-22 11:42:1831namespace content {
32class ResourceDispatcher;
33class URLResponseBodyConsumer;
yhiranof6c76cf2016-12-22 11:42:1834
35class 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
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
arthursonzogni3a4ca9f2017-12-07 17:58:3452 // 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
yhiranof6c76cf2016-12-22 11:42:1860 // mojom::URLLoaderClient implementation
John Abd-El-Malek46248032018-01-17 19:11:2361 void OnReceiveResponse(const network::ResourceResponseHead& response_head,
jam33d897e2017-04-14 21:28:4662 const base::Optional<net::SSLInfo>& ssl_info,
yhirano6f57b502017-03-01 03:44:2063 mojom::DownloadedTempFilePtr downloaded_file) override;
John Abd-El-Malek46248032018-01-17 19:11:2364 void OnReceiveRedirect(
65 const net::RedirectInfo& redirect_info,
66 const network::ResourceResponseHead& response_head) override;
yhiranof6c76cf2016-12-22 11:42:1867 void OnDataDownloaded(int64_t data_len, int64_t encoded_data_len) override;
tzikbedcbce2017-01-24 15:08:0568 void OnUploadProgress(int64_t current_position,
69 int64_t total_size,
tzikc2007562017-04-25 07:48:2470 OnUploadProgressCallback ack_callback) override;
yhirano2a525f92017-01-18 08:16:4671 void OnReceiveCachedMetadata(const std::vector<uint8_t>& data) override;
yhiranof6c76cf2016-12-22 11:42:1872 void OnTransferSizeUpdated(int32_t transfer_size_diff) override;
73 void OnStartLoadingResponseBody(
74 mojo::ScopedDataPipeConsumerHandle body) override;
Takashi Toyoshimaaa278662017-11-20 11:11:2675 void OnComplete(const network::URLLoaderCompletionStatus& status) override;
yhiranof6c76cf2016-12-22 11:42:1876
yhirano6a0e9c72016-12-27 08:31:0477 private:
Yutaka Hirano3dad2832017-12-14 06:48:1678 class DeferredMessage;
79 class DeferredOnReceiveResponse;
80 class DeferredOnReceiveRedirect;
81 class DeferredOnDataDownloaded;
82 class DeferredOnUploadProgress;
83 class DeferredOnReceiveCachedMetadata;
84 class DeferredOnComplete;
85
yhiranoe45a888f2017-04-14 01:24:2086 bool NeedsStoringMessage() const;
Yutaka Hirano3dad2832017-12-14 06:48:1687 void StoreAndDispatch(std::unique_ptr<DeferredMessage> message);
arthursonzogni3a4ca9f2017-12-07 17:58:3488 void OnConnectionClosed();
yhiranof6c76cf2016-12-22 11:42:1889
yhiranof6c76cf2016-12-22 11:42:1890 scoped_refptr<URLResponseBodyConsumer> body_consumer_;
yhirano6f57b502017-03-01 03:44:2091 mojom::DownloadedTempFilePtr downloaded_file_;
Yutaka Hirano3dad2832017-12-14 06:48:1692 std::vector<std::unique_ptr<DeferredMessage>> deferred_messages_;
yhiranof6c76cf2016-12-22 11:42:1893 const int request_id_;
94 bool has_received_response_ = false;
arthursonzogni3a4ca9f2017-12-07 17:58:3495 bool has_received_complete_ = false;
yhirano6a0e9c72016-12-27 08:31:0496 bool is_deferred_ = false;
97 int32_t accumulated_transfer_size_diff_during_deferred_ = 0;
yhiranof6c76cf2016-12-22 11:42:1898 ResourceDispatcher* const resource_dispatcher_;
99 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
arthursonzogni3a4ca9f2017-12-07 17:58:34100
101 // Used in NavigationMojoResponse and NetworkService.
102 mojom::URLLoaderPtr url_loader_;
103 mojo::Binding<mojom::URLLoaderClient> url_loader_client_binding_;
104
yhirano6a0e9c72016-12-27 08:31:04105 base::WeakPtrFactory<URLLoaderClientImpl> weak_factory_;
yhiranof6c76cf2016-12-22 11:42:18106};
107
108} // namespace content
109
John Abd-El-Malek6b56ef712017-10-21 22:52:46110#endif // CONTENT_RENDERER_LOADER_URL_LOADER_CLIENT_IMPL_H_