OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 COMPONENTS_CRONET_ANDROID_URL_REQUEST_ADAPTER_H_ | |
6 #define COMPONENTS_CRONET_ANDROID_URL_REQUEST_ADAPTER_H_ | |
7 | |
8 #include <jni.h> | |
9 #include <stdint.h> | |
10 | |
11 #include <memory> | |
12 #include <string> | |
13 | |
14 #include "base/macros.h" | |
15 #include "base/memory/ref_counted.h" | |
16 #include "net/base/chunked_upload_data_stream.h" | |
17 #include "net/base/request_priority.h" | |
18 #include "net/http/http_request_headers.h" | |
19 #include "net/url_request/url_request.h" | |
20 | |
21 namespace net { | |
22 class HttpResponseHeaders; | |
23 class IOBufferWithSize; | |
24 class UploadDataStream; | |
25 struct RedirectInfo; | |
26 } // namespace net | |
27 | |
28 namespace cronet { | |
29 | |
30 class URLRequestContextAdapter; | |
31 | |
32 // An adapter from the JNI |UrlRequest| object and the Chromium |URLRequest| | |
33 // object. | |
34 class URLRequestAdapter : public net::URLRequest::Delegate { | |
35 public: | |
36 // The delegate which is called when the request finishes. | |
37 class URLRequestAdapterDelegate | |
38 : public base::RefCountedThreadSafe<URLRequestAdapterDelegate> { | |
39 public: | |
40 virtual void OnResponseStarted(URLRequestAdapter* request) = 0; | |
41 virtual void OnBytesRead(URLRequestAdapter* request, int bytes_read) = 0; | |
42 virtual void OnRequestFinished(URLRequestAdapter* request) = 0; | |
43 virtual int ReadFromUploadChannel(net::IOBuffer* buf, int buf_length) = 0; | |
44 | |
45 protected: | |
46 friend class base::RefCountedThreadSafe<URLRequestAdapterDelegate>; | |
47 virtual ~URLRequestAdapterDelegate() {} | |
48 }; | |
49 | |
50 URLRequestAdapter(URLRequestContextAdapter* context, | |
51 URLRequestAdapterDelegate* delegate, | |
52 GURL url, | |
53 net::RequestPriority priority); | |
54 ~URLRequestAdapter() override; | |
55 | |
56 // Sets the request method GET, POST etc | |
57 void SetMethod(const std::string& method); | |
58 | |
59 // Adds a header to the request | |
60 void AddHeader(const std::string& name, const std::string& value); | |
61 | |
62 // Sets the contents of the POST or PUT request | |
63 void SetUploadContent(const char* bytes, int bytes_len); | |
64 | |
65 // Sets the request to streaming upload. | |
66 void SetUploadChannel(JNIEnv* env, int64_t content_length); | |
67 | |
68 // Disables redirect. Note that redirect is enabled by default. | |
69 void DisableRedirects(); | |
70 | |
71 // Indicates that the request body will be streamed by calling AppendChunk() | |
72 // repeatedly. This must be called before Start(). | |
73 void EnableChunkedUpload(); | |
74 | |
75 // Appends a chunk to the POST body. | |
76 // This must be called after EnableChunkedUpload() and Start(). | |
77 void AppendChunk(const char* bytes, int bytes_len, bool is_last_chunk); | |
78 | |
79 // Starts the request. | |
80 void Start(); | |
81 | |
82 // Cancels the request. | |
83 void Cancel(); | |
84 | |
85 // Releases all resources for the request and deletes the object itself. | |
86 void Destroy(); | |
87 | |
88 // Returns the URL of the request. | |
89 GURL url() const { return url_; } | |
90 | |
91 // Returns the error code after the request is complete. | |
92 // Negative codes indicate system errors. | |
93 int error_code() const { return error_code_; } | |
94 | |
95 // Returns the HTTP status code. | |
96 int http_status_code() const { | |
97 return http_status_code_; | |
98 }; | |
99 | |
100 // Returns the HTTP status text of the normalized status line. | |
101 const std::string& http_status_text() const { | |
102 return http_status_text_; | |
103 } | |
104 | |
105 // Returns the value of the content-length response header. | |
106 int64_t content_length() const { return expected_size_; } | |
107 | |
108 // Returns the value of the content-type response header. | |
109 std::string content_type() const { return content_type_; } | |
110 | |
111 // Returns the value of the specified response header. | |
112 std::string GetHeader(const std::string& name) const; | |
113 | |
114 // Get all response headers, as a HttpResponseHeaders object. | |
115 net::HttpResponseHeaders* GetResponseHeaders() const; | |
116 | |
117 // Returns a pointer to the downloaded data. | |
118 unsigned char* Data() const; | |
119 | |
120 // Get NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo. | |
121 std::string GetNegotiatedProtocol() const; | |
122 | |
123 // Returns whether the response is serviced from cache. | |
124 bool GetWasCached() const; | |
125 | |
126 // net::URLRequest::Delegate implementation: | |
127 void OnResponseStarted(net::URLRequest* request, int net_error) override; | |
128 void OnReadCompleted(net::URLRequest* request, int bytes_read) override; | |
129 void OnReceivedRedirect(net::URLRequest* request, | |
130 const net::RedirectInfo& redirect_info, | |
131 bool* defer_redirect) override; | |
132 | |
133 bool OnNetworkThread() const; | |
134 | |
135 private: | |
136 static void OnDestroyRequest(URLRequestAdapter* self); | |
137 | |
138 void OnInitiateConnection(); | |
139 void OnCancelRequest(); | |
140 void OnRequestSucceeded(); | |
141 void OnRequestFailed(int net_error); | |
142 void OnRequestCompleted(); | |
143 void OnAppendChunk(const std::unique_ptr<char[]> bytes, | |
144 int bytes_len, | |
145 bool is_last_chunk); | |
146 | |
147 void Read(); | |
148 | |
149 // Handles synchronous or asynchronous read result, calls |delegate_| with | |
150 // bytes read and returns true unless request has succeeded or failed. | |
151 bool HandleReadResult(int result); | |
152 | |
153 URLRequestContextAdapter* context_; | |
154 scoped_refptr<URLRequestAdapterDelegate> delegate_; | |
155 GURL url_; | |
156 net::RequestPriority priority_; | |
157 std::string method_; | |
158 net::HttpRequestHeaders headers_; | |
159 std::unique_ptr<net::URLRequest> url_request_; | |
160 std::unique_ptr<net::UploadDataStream> upload_data_stream_; | |
161 std::unique_ptr<net::ChunkedUploadDataStream::Writer> chunked_upload_writer_; | |
162 scoped_refptr<net::IOBufferWithSize> read_buffer_; | |
163 int total_bytes_read_; | |
164 int error_code_; | |
165 int http_status_code_; | |
166 std::string http_status_text_; | |
167 std::string content_type_; | |
168 bool canceled_; | |
169 int64_t expected_size_; | |
170 bool chunked_upload_; | |
171 // Indicates whether redirect has been disabled. | |
172 bool disable_redirect_; | |
173 | |
174 DISALLOW_COPY_AND_ASSIGN(URLRequestAdapter); | |
175 }; | |
176 | |
177 } // namespace cronet | |
178 | |
179 #endif // COMPONENTS_CRONET_ANDROID_URL_REQUEST_ADAPTER_H_ | |
OLD | NEW |