blob: ada64af8cb5e5928565af62125c49ccc7ecc6295 [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"
16#include "mojo/public/cpp/bindings/associated_binding.h"
17#include "mojo/public/cpp/system/data_pipe.h"
18
19namespace base {
20class SingleThreadTaskRunner;
21} // namespace base
22
23namespace mojo {
24class AssociatedGroup;
25} // namespace mojo
26
27namespace net {
28struct RedirectInfo;
29} // namespace net
30
31namespace content {
32class ResourceDispatcher;
33class URLResponseBodyConsumer;
34struct ResourceRequestCompletionStatus;
35struct ResourceResponseHead;
36
37class 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
yhirano6a0e9c72016-12-27 08:31:0447 // 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
yhiranof6c76cf2016-12-22 11:42:1857 // 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
yhirano6a0e9c72016-12-27 08:31:0468 private:
yhiranof6c76cf2016-12-22 11:42:1869 void Dispatch(const IPC::Message& message);
70
71 mojo::AssociatedBinding<mojom::URLLoaderClient> binding_;
72 scoped_refptr<URLResponseBodyConsumer> body_consumer_;
73 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_