Index: components/cronet/android/cronet_url_request_adapter.h |
diff --git a/components/cronet/android/cronet_url_request_adapter.h b/components/cronet/android/cronet_url_request_adapter.h |
index 00726110b416e19e540385c16889de7b8c13bb7b..956598dd115293665cb00bae4a55ab70686563e6 100644 |
--- a/components/cronet/android/cronet_url_request_adapter.h |
+++ b/components/cronet/android/cronet_url_request_adapter.h |
@@ -5,14 +5,17 @@ |
#ifndef COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ |
#define COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ |
+#include <jni.h> |
#include <string> |
+#include "base/android/jni_android.h" |
+#include "base/android/jni_string.h" |
+#include "base/android/scoped_java_ref.h" |
#include "base/callback.h" |
#include "base/location.h" |
#include "base/macros.h" |
#include "base/memory/scoped_ptr.h" |
#include "net/base/request_priority.h" |
-#include "net/http/http_request_headers.h" |
#include "net/url_request/url_request.h" |
#include "url/gurl.h" |
@@ -22,6 +25,7 @@ class SingleThreadTaskRunner; |
namespace net { |
class GrowableIOBuffer; |
+class HttpRequestHeaders; |
class HttpResponseHeaders; |
class UploadDataStream; |
} // namespace net |
@@ -30,106 +34,83 @@ namespace cronet { |
class CronetURLRequestContextAdapter; |
-// An adapter from the JNI CronetUrlRequest object to the Chromium URLRequest. |
-// Created and configured from random thread. Start is posted to network |
-// thread and all callbacks into CronetURLRequestAdapterDelegate are done on |
-// network thread. Each delegate callback is expected to initiate next step like |
-// FollowDeferredRedirect, ReadData or Destroy. All methods except those needed |
-// to set up a request must be called exclusively on the network thread. |
+bool CronetUrlRequestAdapterRegisterJni(JNIEnv* env); |
+ |
+// An adapter from Java CronetUrlRequest object to net::URLRequest. |
+// Created and configured from a Java thread. Start, ReadData, and Destroy are |
+// posted to network thread and all callbacks into the Java CronetUrlRequest are |
+// done on the network thread. Java CronetUrlRequest is expected to initiate the |
+// next step like FollowDeferredRedirect, ReadData or Destroy. Public methods |
+// can be called on any thread except PopulateResponseHeaders and Get* methods, |
+// which can only be called on the network thread. |
class CronetURLRequestAdapter : public net::URLRequest::Delegate { |
public: |
- // The delegate which is called when the request adapter completes a step. |
- // Always called on network thread. |
- class CronetURLRequestAdapterDelegate { |
- public: |
- // Called on redirect. Consumer should either destroy the request, or |
- // call FollowDeferredRedirect. |
- virtual void OnRedirect(const GURL& newLocation, int http_status_code) = 0; |
- // Called when response has started and headers have been received. Consumer |
- // should either destroy the request, or call ReadData. |
- virtual void OnResponseStarted(int http_status_code) = 0; |
- // Called when response bytes were read. Consumer should consume data and |
- // either destroy the request, or call ReadData. The request may only be |
- // destroyed after the embedder is done with |bytes|, as deleting the |
- // request frees the buffer. |
- virtual void OnBytesRead(unsigned char* bytes, |
- int bytes_read) = 0; |
- // Called when response has finished successfully. Consumer should destroy |
- // the request. |
- virtual void OnRequestFinished() = 0; |
- // Called when response has finished with error. Consumer should destroy |
- // the request. |
- virtual void OnError(int net_error) = 0; |
- |
- virtual ~CronetURLRequestAdapterDelegate() {} |
- }; |
- |
- CronetURLRequestAdapter( |
- CronetURLRequestContextAdapter* context, |
- scoped_ptr<CronetURLRequestAdapterDelegate> delegate, |
- const GURL& url, |
- net::RequestPriority priority); |
+ CronetURLRequestAdapter(CronetURLRequestContextAdapter* context, |
+ JNIEnv* env, |
+ jobject jurl_request, |
+ const GURL& url, |
+ net::RequestPriority priority); |
~CronetURLRequestAdapter() override; |
// Methods called prior to Start are never called on network thread. |
// Sets the request method GET, POST etc. |
- void set_method(const std::string& method) { |
- initial_method_ = method; |
- } |
+ jboolean SetHttpMethod(JNIEnv* env, jobject jcaller, jstring jmethod); |
// Adds a header to the request before it starts. |
- void AddRequestHeader(const std::string& name, const std::string& value); |
+ jboolean AddRequestHeader(JNIEnv* env, |
+ jobject jcaller, |
+ jstring jname, |
+ jstring jvalue); |
+ |
+ // Bypasses cache. If context is not set up to use cache, this call has no |
+ // effect. |
+ void DisableCache(JNIEnv* env, jobject jcaller); |
// Adds a request body to the request before it starts. |
void SetUpload(scoped_ptr<net::UploadDataStream> upload); |
- // Methods called on any thread. |
- |
- // Posts tasks to network thread. |
- void PostTaskToNetworkThread(const tracked_objects::Location& from_here, |
- const base::Closure& task); |
- |
- // Returns true if called on network thread. |
- bool IsOnNetworkThread() const; |
- |
- // Methods called only on network thread. |
- |
// Starts the request. |
- void Start(); |
+ void Start(JNIEnv* env, jobject jcaller); |
// Follows redirect. |
- void FollowDeferredRedirect(); |
+ void FollowDeferredRedirect(JNIEnv* env, jobject jcaller); |
// Reads more data. |
- void ReadData(); |
+ void ReadData(JNIEnv* env, jobject jcaller); |
// Releases all resources for the request and deletes the object itself. |
- void Destroy(); |
+ void Destroy(JNIEnv* env, jobject jcaller); |
+ |
+ // Populate response headers on network thread. |
+ void PopulateResponseHeaders(JNIEnv* env, |
+ jobject jcaller, |
+ jobject jheaders_list); |
// When called during a OnRedirect or OnResponseStarted callback, these |
- // methods return the corresponding response information. |
+ // methods return the corresponding response information. These methods |
+ // can only be called on the network thread. |
- // Gets all response headers, as a HttpResponseHeaders object. Returns NULL |
- // if the last attempted request received no headers. |
- const net::HttpResponseHeaders* GetResponseHeaders() const; |
+ // Gets http status text from the response headers. |
+ base::android::ScopedJavaLocalRef<jstring> GetHttpStatusText( |
+ JNIEnv* env, |
+ jobject jcaller) const; |
// Gets NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo. |
- const std::string& GetNegotiatedProtocol() const; |
+ base::android::ScopedJavaLocalRef<jstring> GetNegotiatedProtocol( |
+ JNIEnv* env, |
+ jobject jcaller) const; |
// Returns true if response is coming from the cache. |
- bool GetWasCached() const; |
+ jboolean GetWasCached(JNIEnv* env, jobject jcaller) const; |
// Gets the total amount of body data received from network after |
- // SSL/SPDY/QUIC decoding and proxy handling. Basically the size of the body |
+ // SSL/SPDY/QUIC decoding and proxy handling. Basically the size of the body |
// prior to decompression. |
- int64 GetTotalReceivedBytes() const; |
+ int64 GetTotalReceivedBytes(JNIEnv* env, jobject jcaller) const; |
- // Bypasses cache. If context is not set up to use cache, this call has no |
- // effect. |
- void DisableCache(); |
+ // net::URLRequest::Delegate implementations: |
- // net::URLRequest::Delegate overrides. |
void OnReceivedRedirect(net::URLRequest* request, |
const net::RedirectInfo& redirect_info, |
bool* defer_redirect) override; |
@@ -137,12 +118,19 @@ class CronetURLRequestAdapter : public net::URLRequest::Delegate { |
void OnReadCompleted(net::URLRequest* request, int bytes_read) override; |
private: |
+ void StartOnNetworkThread(); |
+ void FollowDeferredRedirectOnNetworkThread(); |
+ void ReadDataOnNetworkThread(); |
+ void DestroyOnNetworkThread(); |
+ |
// Checks status of the request_adapter, return false if |is_success()| is |
// true, otherwise report error and cancel request_adapter. |
bool MaybeReportError(net::URLRequest* request) const; |
CronetURLRequestContextAdapter* context_; |
- scoped_ptr<CronetURLRequestAdapterDelegate> delegate_; |
+ |
+ // Java object that owns this CronetURLRequestContextAdapter. |
+ base::android::ScopedJavaGlobalRef<jobject> owner_; |
const GURL initial_url_; |
const net::RequestPriority initial_priority_; |