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_CRONET_URL_REQUEST_ADAPTER_H_
6 #define COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_
11 #include "base/android/jni_android.h"
12 #include "base/android/jni_string.h"
13 #include "base/android/scoped_java_ref.h"
14 #include "base/callback.h"
15 #include "base/location.h"
16 #include "base/macros.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "net/base/request_priority.h"
20 #include "net/url_request/url_request.h"
24 class SingleThreadTaskRunner
;
28 class HttpRequestHeaders
;
29 class HttpResponseHeaders
;
31 class UploadDataStream
;
36 class CronetURLRequestContextAdapter
;
38 bool CronetUrlRequestAdapterRegisterJni(JNIEnv
* env
);
40 // An adapter from Java CronetUrlRequest object to net::URLRequest.
41 // Created and configured from a Java thread. Start, ReadData, and Destroy are
42 // posted to network thread and all callbacks into the Java CronetUrlRequest are
43 // done on the network thread. Java CronetUrlRequest is expected to initiate the
44 // next step like FollowDeferredRedirect, ReadData or Destroy. Public methods
45 // can be called on any thread except PopulateResponseHeaders and Get* methods,
46 // which can only be called on the network thread.
47 class CronetURLRequestAdapter
: public net::URLRequest::Delegate
{
49 CronetURLRequestAdapter(CronetURLRequestContextAdapter
* context
,
53 net::RequestPriority priority
);
54 ~CronetURLRequestAdapter() override
;
56 // Methods called prior to Start are never called on network thread.
58 // Sets the request method GET, POST etc.
59 jboolean
SetHttpMethod(JNIEnv
* env
, jobject jcaller
, jstring jmethod
);
61 // Adds a header to the request before it starts.
62 jboolean
AddRequestHeader(JNIEnv
* env
,
67 // Bypasses cache. If context is not set up to use cache, this call has no
69 void DisableCache(JNIEnv
* env
, jobject jcaller
);
71 // Adds a request body to the request before it starts.
72 void SetUpload(scoped_ptr
<net::UploadDataStream
> upload
);
74 // Starts the request.
75 void Start(JNIEnv
* env
, jobject jcaller
);
77 void GetStatus(JNIEnv
* env
, jobject jcaller
, jobject jstatus_listener
) const;
80 void FollowDeferredRedirect(JNIEnv
* env
, jobject jcaller
);
83 jboolean
ReadData(JNIEnv
* env
, jobject jcaller
,
88 // Releases all resources for the request and deletes the object itself.
89 void Destroy(JNIEnv
* env
, jobject jcaller
);
91 // Populate response headers on network thread.
92 void PopulateResponseHeaders(JNIEnv
* env
,
94 jobject jheaders_list
);
96 // When called during a OnRedirect or OnResponseStarted callback, these
97 // methods return the corresponding response information. These methods
98 // can only be called on the network thread.
100 // Gets http status text from the response headers.
101 base::android::ScopedJavaLocalRef
<jstring
> GetHttpStatusText(
103 jobject jcaller
) const;
105 // Gets NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo.
106 base::android::ScopedJavaLocalRef
<jstring
> GetNegotiatedProtocol(
108 jobject jcaller
) const;
110 // Returns the host and port of the proxy server, if one was used.
111 base::android::ScopedJavaLocalRef
<jstring
> GetProxyServer(
113 jobject jcaller
) const;
115 // Returns true if response is coming from the cache.
116 jboolean
GetWasCached(JNIEnv
* env
, jobject jcaller
) const;
118 // net::URLRequest::Delegate implementations:
120 void OnReceivedRedirect(net::URLRequest
* request
,
121 const net::RedirectInfo
& redirect_info
,
122 bool* defer_redirect
) override
;
123 void OnSSLCertificateError(net::URLRequest
* request
,
124 const net::SSLInfo
& ssl_info
,
125 bool fatal
) override
;
126 void OnResponseStarted(net::URLRequest
* request
) override
;
127 void OnReadCompleted(net::URLRequest
* request
, int bytes_read
) override
;
130 class IOBufferWithByteBuffer
;
132 void StartOnNetworkThread();
133 void GetStatusOnNetworkThread(
134 const base::android::ScopedJavaGlobalRef
<jobject
>& jstatus_listener_ref
)
136 void FollowDeferredRedirectOnNetworkThread();
137 void ReadDataOnNetworkThread(
138 scoped_refptr
<IOBufferWithByteBuffer
> read_buffer
,
140 void DestroyOnNetworkThread();
142 // Checks status of the request_adapter, return false if |is_success()| is
143 // true, otherwise report error and cancel request_adapter.
144 bool MaybeReportError(net::URLRequest
* request
) const;
146 CronetURLRequestContextAdapter
* context_
;
148 // Java object that owns this CronetURLRequestContextAdapter.
149 base::android::ScopedJavaGlobalRef
<jobject
> owner_
;
151 const GURL initial_url_
;
152 const net::RequestPriority initial_priority_
;
153 std::string initial_method_
;
155 net::HttpRequestHeaders initial_request_headers_
;
156 scoped_ptr
<net::UploadDataStream
> upload_
;
158 scoped_refptr
<IOBufferWithByteBuffer
> read_buffer_
;
159 scoped_ptr
<net::URLRequest
> url_request_
;
161 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestAdapter
);
164 } // namespace cronet
166 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_