OLD | NEW |
| (Empty) |
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_BROWSER_LOADER_TEST_URL_LOADER_CLIENT_H_ | |
6 #define CONTENT_BROWSER_LOADER_TEST_URL_LOADER_CLIENT_H_ | |
7 | |
8 #include <stdint.h> | |
9 #include <vector> | |
10 | |
11 #include "base/callback.h" | |
12 #include "base/macros.h" | |
13 #include "content/public/common/resource_request_completion_status.h" | |
14 #include "content/public/common/resource_response.h" | |
15 #include "content/public/common/url_loader.mojom.h" | |
16 #include "content/public/common/url_loader_factory.mojom.h" | |
17 #include "mojo/public/c/system/data_pipe.h" | |
18 #include "mojo/public/cpp/bindings/binding.h" | |
19 #include "net/url_request/redirect_info.h" | |
20 | |
21 namespace content { | |
22 | |
23 // A TestURLLoaderClient records URLLoaderClient function calls. It also calls | |
24 // the closure set via set_quit_closure if set, in order to make it possible to | |
25 // create a base::RunLoop, set its quit closure to this client and then run the | |
26 // RunLoop. | |
27 class TestURLLoaderClient final : public mojom::URLLoaderClient { | |
28 public: | |
29 TestURLLoaderClient(); | |
30 ~TestURLLoaderClient() override; | |
31 | |
32 void OnReceiveResponse(const ResourceResponseHead& response_head, | |
33 const base::Optional<net::SSLInfo>& ssl_info, | |
34 mojom::DownloadedTempFilePtr downloaded_file) override; | |
35 void OnReceiveRedirect(const net::RedirectInfo& redirect_info, | |
36 const ResourceResponseHead& response_head) override; | |
37 void OnDataDownloaded(int64_t data_length, int64_t encoded_length) override; | |
38 void OnReceiveCachedMetadata(const std::vector<uint8_t>& data) override; | |
39 void OnTransferSizeUpdated(int32_t transfer_size_diff) override; | |
40 void OnUploadProgress(int64_t current_position, | |
41 int64_t total_size, | |
42 OnUploadProgressCallback ack_callback) override; | |
43 void OnStartLoadingResponseBody( | |
44 mojo::ScopedDataPipeConsumerHandle body) override; | |
45 void OnComplete(const ResourceRequestCompletionStatus& status) override; | |
46 | |
47 bool has_received_response() const { return has_received_response_; } | |
48 bool has_received_redirect() const { return has_received_redirect_; } | |
49 bool has_data_downloaded() const { return has_data_downloaded_; } | |
50 bool has_received_upload_progress() const { | |
51 return has_received_upload_progress_; | |
52 } | |
53 bool has_received_cached_metadata() const { | |
54 return has_received_cached_metadata_; | |
55 } | |
56 bool has_received_completion() const { return has_received_completion_; } | |
57 const ResourceResponseHead& response_head() const { return response_head_; } | |
58 const base::Optional<net::SSLInfo>& ssl_info() const { return ssl_info_; } | |
59 const net::RedirectInfo& redirect_info() const { return redirect_info_; } | |
60 const std::string& cached_metadata() const { | |
61 return cached_metadata_; | |
62 } | |
63 mojo::DataPipeConsumerHandle response_body() { return response_body_.get(); } | |
64 mojo::ScopedDataPipeConsumerHandle response_body_release() { | |
65 return std::move(response_body_); | |
66 } | |
67 const ResourceRequestCompletionStatus& completion_status() const { | |
68 return completion_status_; | |
69 } | |
70 int64_t download_data_length() const { return download_data_length_; } | |
71 int64_t encoded_download_data_length() const { | |
72 return encoded_download_data_length_; | |
73 } | |
74 int64_t body_transfer_size() const { return body_transfer_size_; } | |
75 int64_t current_upload_position() const { return current_upload_position_; } | |
76 int64_t total_upload_size() const { return total_upload_size_; } | |
77 | |
78 void reset_has_received_upload_progress() { | |
79 has_received_upload_progress_ = false; | |
80 } | |
81 | |
82 void ClearHasReceivedRedirect(); | |
83 // Creates an InterfacePtr, binds it to |*this| and returns it. | |
84 mojom::URLLoaderClientPtr CreateInterfacePtr(); | |
85 | |
86 void Unbind(); | |
87 | |
88 void RunUntilResponseReceived(); | |
89 void RunUntilRedirectReceived(); | |
90 void RunUntilDataDownloaded(); | |
91 void RunUntilCachedMetadataReceived(); | |
92 void RunUntilResponseBodyArrived(); | |
93 void RunUntilComplete(); | |
94 void RunUntilConnectionError(); | |
95 | |
96 private: | |
97 void OnConnectionError(); | |
98 | |
99 mojo::Binding<mojom::URLLoaderClient> binding_; | |
100 ResourceResponseHead response_head_; | |
101 base::Optional<net::SSLInfo> ssl_info_; | |
102 net::RedirectInfo redirect_info_; | |
103 std::string cached_metadata_; | |
104 mojo::ScopedDataPipeConsumerHandle response_body_; | |
105 ResourceRequestCompletionStatus completion_status_; | |
106 bool has_received_response_ = false; | |
107 bool has_received_redirect_ = false; | |
108 bool has_data_downloaded_ = false; | |
109 bool has_received_upload_progress_ = false; | |
110 bool has_received_cached_metadata_ = false; | |
111 bool has_received_completion_ = false; | |
112 bool has_received_connection_error_ = false; | |
113 | |
114 base::Closure quit_closure_for_on_receive_response_; | |
115 base::Closure quit_closure_for_on_receive_redirect_; | |
116 base::Closure quit_closure_for_on_data_downloaded_; | |
117 base::Closure quit_closure_for_on_receive_cached_metadata_; | |
118 base::Closure quit_closure_for_on_start_loading_response_body_; | |
119 base::Closure quit_closure_for_on_complete_; | |
120 base::Closure quit_closure_for_on_connection_error_; | |
121 | |
122 mojom::URLLoaderFactoryPtr url_loader_factory_; | |
123 int64_t download_data_length_ = 0; | |
124 int64_t encoded_download_data_length_ = 0; | |
125 int64_t body_transfer_size_ = 0; | |
126 int64_t current_upload_position_ = 0; | |
127 int64_t total_upload_size_ = 0; | |
128 | |
129 DISALLOW_COPY_AND_ASSIGN(TestURLLoaderClient); | |
130 }; | |
131 | |
132 } // namespace content | |
133 | |
134 #endif // CONTENT_BROWSER_LOADER_TEST_URL_LOADER_CLIENT_H_ | |
OLD | NEW |