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.
5 #ifndef COMPONENTS_CRONET_ANDROID_URL_REQUEST_ADAPTER_H_
6 #define COMPONENTS_CRONET_ANDROID_URL_REQUEST_ADAPTER_H_
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "net/base/request_priority.h"
16 #include "net/http/http_request_headers.h"
17 #include "net/url_request/url_request.h"
20 class IOBufferWithSize
;
21 class HttpResponseHeaders
;
22 class UploadDataStream
;
28 class URLRequestContextAdapter
;
30 // An adapter from the JNI |UrlRequest| object and the Chromium |URLRequest|
32 class URLRequestAdapter
: public net::URLRequest::Delegate
{
34 // The delegate which is called when the request finishes.
35 class URLRequestAdapterDelegate
36 : public base::RefCountedThreadSafe
<URLRequestAdapterDelegate
> {
38 virtual void OnResponseStarted(URLRequestAdapter
* request
) = 0;
39 virtual void OnBytesRead(URLRequestAdapter
* request
, int bytes_read
) = 0;
40 virtual void OnRequestFinished(URLRequestAdapter
* request
) = 0;
41 virtual int ReadFromUploadChannel(net::IOBuffer
* buf
, int buf_length
) = 0;
44 friend class base::RefCountedThreadSafe
<URLRequestAdapterDelegate
>;
45 virtual ~URLRequestAdapterDelegate() {}
48 URLRequestAdapter(URLRequestContextAdapter
* context
,
49 URLRequestAdapterDelegate
* delegate
,
51 net::RequestPriority priority
);
52 ~URLRequestAdapter() override
;
54 // Sets the request method GET, POST etc
55 void SetMethod(const std::string
& method
);
57 // Adds a header to the request
58 void AddHeader(const std::string
& name
, const std::string
& value
);
60 // Sets the contents of the POST or PUT request
61 void SetUploadContent(const char* bytes
, int bytes_len
);
63 // Sets the request to streaming upload.
64 void SetUploadChannel(JNIEnv
* env
, int64 content_length
);
66 // Disables redirect. Note that redirect is enabled by default.
67 void DisableRedirects();
69 // Indicates that the request body will be streamed by calling AppendChunk()
70 // repeatedly. This must be called before Start().
71 void EnableChunkedUpload();
73 // Appends a chunk to the POST body.
74 // This must be called after EnableChunkedUpload() and Start().
75 void AppendChunk(const char* bytes
, int bytes_len
, bool is_last_chunk
);
77 // Starts the request.
80 // Cancels the request.
83 // Releases all resources for the request and deletes the object itself.
86 // Returns the URL of the request.
87 GURL
url() const { return url_
; }
89 // Returns the error code after the request is complete.
90 // Negative codes indicate system errors.
91 int error_code() const { return error_code_
; }
93 // Returns the HTTP status code.
94 int http_status_code() const {
95 return http_status_code_
;
98 // Returns the HTTP status text of the normalized status line.
99 const std::string
& http_status_text() const {
100 return http_status_text_
;
103 // Returns the value of the content-length response header.
104 int64
content_length() const { return expected_size_
; }
106 // Returns the value of the content-type response header.
107 std::string
content_type() const { return content_type_
; }
109 // Returns the value of the specified response header.
110 std::string
GetHeader(const std::string
& name
) const;
112 // Get all response headers, as a HttpResponseHeaders object.
113 net::HttpResponseHeaders
* GetResponseHeaders() const;
115 // Returns a pointer to the downloaded data.
116 unsigned char* Data() const;
118 // Get NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo.
119 std::string
GetNegotiatedProtocol() const;
121 // Returns whether the response is serviced from cache.
122 bool GetWasCached() const;
124 // net::URLRequest::Delegate implementation:
125 void OnResponseStarted(net::URLRequest
* request
) override
;
126 void OnReadCompleted(net::URLRequest
* request
, int bytes_read
) override
;
127 void OnReceivedRedirect(net::URLRequest
* request
,
128 const net::RedirectInfo
& redirect_info
,
129 bool* defer_redirect
) override
;
131 bool OnNetworkThread() const;
134 static void OnDestroyRequest(URLRequestAdapter
* self
);
136 void OnInitiateConnection();
137 void OnCancelRequest();
138 void OnRequestSucceeded();
139 void OnRequestFailed();
140 void OnRequestCompleted();
141 void OnAppendChunk(const scoped_ptr
<char[]> bytes
, int bytes_len
,
146 // Handles synchronous or asynchronous read result, calls |delegate_| with
147 // bytes read and returns true unless request has succeeded or failed.
148 bool HandleReadResult(int bytes_read
);
150 URLRequestContextAdapter
* context_
;
151 scoped_refptr
<URLRequestAdapterDelegate
> delegate_
;
153 net::RequestPriority priority_
;
155 net::HttpRequestHeaders headers_
;
156 scoped_ptr
<net::URLRequest
> url_request_
;
157 scoped_ptr
<net::UploadDataStream
> upload_data_stream_
;
158 scoped_refptr
<net::IOBufferWithSize
> read_buffer_
;
159 int total_bytes_read_
;
161 int http_status_code_
;
162 std::string http_status_text_
;
163 std::string content_type_
;
165 int64 expected_size_
;
166 bool chunked_upload_
;
167 // Indicates whether redirect has been disabled.
168 bool disable_redirect_
;
170 DISALLOW_COPY_AND_ASSIGN(URLRequestAdapter
);
173 } // namespace cronet
175 #endif // COMPONENTS_CRONET_ANDROID_URL_REQUEST_ADAPTER_H_