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
;
30 class UploadDataStream
;
35 class CronetURLRequestContextAdapter
;
37 bool CronetUrlRequestAdapterRegisterJni(JNIEnv
* env
);
39 // An adapter from Java CronetUrlRequest object to net::URLRequest.
40 // Created and configured from a Java thread. Start, ReadData, and Destroy are
41 // posted to network thread and all callbacks into the Java CronetUrlRequest are
42 // done on the network thread. Java CronetUrlRequest is expected to initiate the
43 // next step like FollowDeferredRedirect, ReadData or Destroy. Public methods
44 // can be called on any thread except PopulateResponseHeaders and Get* methods,
45 // which can only be called on the network thread.
46 class CronetURLRequestAdapter
: public net::URLRequest::Delegate
{
48 CronetURLRequestAdapter(CronetURLRequestContextAdapter
* context
,
52 net::RequestPriority priority
);
53 ~CronetURLRequestAdapter() override
;
55 // Methods called prior to Start are never called on network thread.
57 // Sets the request method GET, POST etc.
58 jboolean
SetHttpMethod(JNIEnv
* env
, jobject jcaller
, jstring jmethod
);
60 // Adds a header to the request before it starts.
61 jboolean
AddRequestHeader(JNIEnv
* env
,
66 // Bypasses cache. If context is not set up to use cache, this call has no
68 void DisableCache(JNIEnv
* env
, jobject jcaller
);
70 // Adds a request body to the request before it starts.
71 void SetUpload(scoped_ptr
<net::UploadDataStream
> upload
);
73 // Starts the request.
74 void Start(JNIEnv
* env
, jobject jcaller
);
76 void GetStatus(JNIEnv
* env
, jobject jcaller
, jobject jstatus_listener
) const;
79 void FollowDeferredRedirect(JNIEnv
* env
, jobject jcaller
);
82 jboolean
ReadData(JNIEnv
* env
, jobject jcaller
,
87 // Releases all resources for the request and deletes the object itself.
88 void Destroy(JNIEnv
* env
, jobject jcaller
);
90 // Populate response headers on network thread.
91 void PopulateResponseHeaders(JNIEnv
* env
,
93 jobject jheaders_list
);
95 // When called during a OnRedirect or OnResponseStarted callback, these
96 // methods return the corresponding response information. These methods
97 // can only be called on the network thread.
99 // Gets http status text from the response headers.
100 base::android::ScopedJavaLocalRef
<jstring
> GetHttpStatusText(
102 jobject jcaller
) const;
104 // Gets NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo.
105 base::android::ScopedJavaLocalRef
<jstring
> GetNegotiatedProtocol(
107 jobject jcaller
) const;
109 // Returns the host and port of the proxy server, if one was used.
110 base::android::ScopedJavaLocalRef
<jstring
> GetProxyServer(
112 jobject jcaller
) const;
114 // Returns true if response is coming from the cache.
115 jboolean
GetWasCached(JNIEnv
* env
, jobject jcaller
) const;
117 // net::URLRequest::Delegate implementations:
119 void OnReceivedRedirect(net::URLRequest
* request
,
120 const net::RedirectInfo
& redirect_info
,
121 bool* defer_redirect
) override
;
122 void OnResponseStarted(net::URLRequest
* request
) override
;
123 void OnReadCompleted(net::URLRequest
* request
, int bytes_read
) override
;
126 class IOBufferWithByteBuffer
;
128 void StartOnNetworkThread();
129 void GetStatusOnNetworkThread(
130 const base::android::ScopedJavaGlobalRef
<jobject
>& jstatus_listener_ref
)
132 void FollowDeferredRedirectOnNetworkThread();
133 void ReadDataOnNetworkThread(
134 scoped_refptr
<IOBufferWithByteBuffer
> read_buffer
,
136 void DestroyOnNetworkThread();
138 // Checks status of the request_adapter, return false if |is_success()| is
139 // true, otherwise report error and cancel request_adapter.
140 bool MaybeReportError(net::URLRequest
* request
) const;
142 CronetURLRequestContextAdapter
* context_
;
144 // Java object that owns this CronetURLRequestContextAdapter.
145 base::android::ScopedJavaGlobalRef
<jobject
> owner_
;
147 const GURL initial_url_
;
148 const net::RequestPriority initial_priority_
;
149 std::string initial_method_
;
151 net::HttpRequestHeaders initial_request_headers_
;
152 scoped_ptr
<net::UploadDataStream
> upload_
;
154 scoped_refptr
<IOBufferWithByteBuffer
> read_buffer_
;
155 scoped_ptr
<net::URLRequest
> url_request_
;
157 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestAdapter
);
160 } // namespace cronet
162 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_