Commit a8177757 authored by xunjieli's avatar xunjieli Committed by Commit bot

This CL uses @NativeClassQualifiedName in

CronetUrlRequest.java to reduce JNI code. After the
refactoring, cronet_url_request.cc is merged with
cronet_url_request_adapter.cc, since the former mostly
contains JNI code that can be generated by
@NativeClassQualifiedName.
This CL also inlines CronetUrlRequestAdapterDelegate, since
we have only one file now.

BUG=458276

Review URL: https://codereview.chromium.org/923213002

Cr-Commit-Position: refs/heads/master@{#323976}
parent 1b4701c7
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
#include "components/cronet/android/chromium_url_request_context.h" #include "components/cronet/android/chromium_url_request_context.h"
#include "components/cronet/android/cronet_histogram_manager.h" #include "components/cronet/android/cronet_histogram_manager.h"
#include "components/cronet/android/cronet_upload_data_stream_delegate.h" #include "components/cronet/android/cronet_upload_data_stream_delegate.h"
#include "components/cronet/android/cronet_url_request.h" #include "components/cronet/android/cronet_url_request_adapter.h"
#include "components/cronet/android/cronet_url_request_context_adapter.h" #include "components/cronet/android/cronet_url_request_context_adapter.h"
#include "jni/CronetLibraryLoader_jni.h" #include "jni/CronetLibraryLoader_jni.h"
#include "net/android/net_jni_registrar.h" #include "net/android/net_jni_registrar.h"
...@@ -42,7 +42,7 @@ const base::android::RegistrationMethod kCronetRegisteredMethods[] = { ...@@ -42,7 +42,7 @@ const base::android::RegistrationMethod kCronetRegisteredMethods[] = {
{"CronetLibraryLoader", RegisterNativesImpl}, {"CronetLibraryLoader", RegisterNativesImpl},
{"CronetUploadDataStreamDelegate", {"CronetUploadDataStreamDelegate",
CronetUploadDataStreamDelegateRegisterJni}, CronetUploadDataStreamDelegateRegisterJni},
{"CronetUrlRequest", CronetUrlRequestRegisterJni}, {"CronetUrlRequestAdapter", CronetUrlRequestAdapterRegisterJni},
{"CronetUrlRequestContextAdapter", {"CronetUrlRequestContextAdapter",
CronetUrlRequestContextAdapterRegisterJni}, CronetUrlRequestContextAdapterRegisterJni},
{"NetAndroid", net::android::RegisterJni}, {"NetAndroid", net::android::RegisterJni},
......
This diff is collapsed.
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_H_
#define COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_H_
#include <jni.h>
namespace cronet {
bool CronetUrlRequestRegisterJni(JNIEnv* env);
} // namespace cronet
#endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_H_
...@@ -5,14 +5,17 @@ ...@@ -5,14 +5,17 @@
#ifndef COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ #ifndef COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_
#define COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ #define COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_
#include <jni.h>
#include <string> #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/callback.h"
#include "base/location.h" #include "base/location.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "net/base/request_priority.h" #include "net/base/request_priority.h"
#include "net/http/http_request_headers.h"
#include "net/url_request/url_request.h" #include "net/url_request/url_request.h"
#include "url/gurl.h" #include "url/gurl.h"
...@@ -22,6 +25,7 @@ class SingleThreadTaskRunner; ...@@ -22,6 +25,7 @@ class SingleThreadTaskRunner;
namespace net { namespace net {
class GrowableIOBuffer; class GrowableIOBuffer;
class HttpRequestHeaders;
class HttpResponseHeaders; class HttpResponseHeaders;
class UploadDataStream; class UploadDataStream;
} // namespace net } // namespace net
...@@ -30,43 +34,20 @@ namespace cronet { ...@@ -30,43 +34,20 @@ namespace cronet {
class CronetURLRequestContextAdapter; class CronetURLRequestContextAdapter;
// An adapter from the JNI CronetUrlRequest object to the Chromium URLRequest. bool CronetUrlRequestAdapterRegisterJni(JNIEnv* env);
// Created and configured from random thread. Start is posted to network
// thread and all callbacks into CronetURLRequestAdapterDelegate are done on // An adapter from Java CronetUrlRequest object to net::URLRequest.
// network thread. Each delegate callback is expected to initiate next step like // Created and configured from a Java thread. Start, ReadData, and Destroy are
// FollowDeferredRedirect, ReadData or Destroy. All methods except those needed // posted to network thread and all callbacks into the Java CronetUrlRequest are
// to set up a request must be called exclusively on the network thread. // 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 { class CronetURLRequestAdapter : public net::URLRequest::Delegate {
public: public:
// The delegate which is called when the request adapter completes a step. CronetURLRequestAdapter(CronetURLRequestContextAdapter* context,
// Always called on network thread. JNIEnv* env,
class CronetURLRequestAdapterDelegate { jobject jurl_request,
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, const GURL& url,
net::RequestPriority priority); net::RequestPriority priority);
~CronetURLRequestAdapter() override; ~CronetURLRequestAdapter() override;
...@@ -74,62 +55,62 @@ class CronetURLRequestAdapter : public net::URLRequest::Delegate { ...@@ -74,62 +55,62 @@ class CronetURLRequestAdapter : public net::URLRequest::Delegate {
// Methods called prior to Start are never called on network thread. // Methods called prior to Start are never called on network thread.
// Sets the request method GET, POST etc. // Sets the request method GET, POST etc.
void set_method(const std::string& method) { jboolean SetHttpMethod(JNIEnv* env, jobject jcaller, jstring jmethod);
initial_method_ = method;
}
// Adds a header to the request before it starts. // 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. // Adds a request body to the request before it starts.
void SetUpload(scoped_ptr<net::UploadDataStream> upload); 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. // Starts the request.
void Start(); void Start(JNIEnv* env, jobject jcaller);
// Follows redirect. // Follows redirect.
void FollowDeferredRedirect(); void FollowDeferredRedirect(JNIEnv* env, jobject jcaller);
// Reads more data. // Reads more data.
void ReadData(); void ReadData(JNIEnv* env, jobject jcaller);
// Releases all resources for the request and deletes the object itself. // 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 // 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 // Gets http status text from the response headers.
// if the last attempted request received no headers. base::android::ScopedJavaLocalRef<jstring> GetHttpStatusText(
const net::HttpResponseHeaders* GetResponseHeaders() const; JNIEnv* env,
jobject jcaller) const;
// Gets NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo. // 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. // 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 // 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. // 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 // net::URLRequest::Delegate implementations:
// effect.
void DisableCache();
// net::URLRequest::Delegate overrides.
void OnReceivedRedirect(net::URLRequest* request, void OnReceivedRedirect(net::URLRequest* request,
const net::RedirectInfo& redirect_info, const net::RedirectInfo& redirect_info,
bool* defer_redirect) override; bool* defer_redirect) override;
...@@ -137,12 +118,19 @@ class CronetURLRequestAdapter : public net::URLRequest::Delegate { ...@@ -137,12 +118,19 @@ class CronetURLRequestAdapter : public net::URLRequest::Delegate {
void OnReadCompleted(net::URLRequest* request, int bytes_read) override; void OnReadCompleted(net::URLRequest* request, int bytes_read) override;
private: private:
void StartOnNetworkThread();
void FollowDeferredRedirectOnNetworkThread();
void ReadDataOnNetworkThread();
void DestroyOnNetworkThread();
// Checks status of the request_adapter, return false if |is_success()| is // Checks status of the request_adapter, return false if |is_success()| is
// true, otherwise report error and cancel request_adapter. // true, otherwise report error and cancel request_adapter.
bool MaybeReportError(net::URLRequest* request) const; bool MaybeReportError(net::URLRequest* request) const;
CronetURLRequestContextAdapter* context_; CronetURLRequestContextAdapter* context_;
scoped_ptr<CronetURLRequestAdapterDelegate> delegate_;
// Java object that owns this CronetURLRequestContextAdapter.
base::android::ScopedJavaGlobalRef<jobject> owner_;
const GURL initial_url_; const GURL initial_url_;
const net::RequestPriority initial_priority_; const net::RequestPriority initial_priority_;
......
...@@ -9,6 +9,7 @@ import android.util.Pair; ...@@ -9,6 +9,7 @@ import android.util.Pair;
import org.chromium.base.CalledByNative; import org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace; import org.chromium.base.JNINamespace;
import org.chromium.base.NativeClassQualifiedName;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -99,7 +100,7 @@ final class CronetUrlRequest implements UrlRequest { ...@@ -99,7 +100,7 @@ final class CronetUrlRequest implements UrlRequest {
destroyRequestAdapter(); destroyRequestAdapter();
return; return;
} }
nativeReceiveData(mUrlRequestAdapter); nativeReadData(mUrlRequestAdapter);
} }
} catch (Exception e) { } catch (Exception e) {
synchronized (mUrlRequestAdapterLock) { synchronized (mUrlRequestAdapterLock) {
...@@ -291,7 +292,7 @@ final class CronetUrlRequest implements UrlRequest { ...@@ -291,7 +292,7 @@ final class CronetUrlRequest implements UrlRequest {
&& !header.second.isEmpty()) { && !header.second.isEmpty()) {
hasContentType = true; hasContentType = true;
} }
if (!nativeAddHeader(mUrlRequestAdapter, header.first, header.second)) { if (!nativeAddRequestHeader(mUrlRequestAdapter, header.first, header.second)) {
destroyRequestAdapter(); destroyRequestAdapter();
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Invalid header " + header.first + "=" + header.second); "Invalid header " + header.first + "=" + header.second);
...@@ -423,7 +424,7 @@ final class CronetUrlRequest implements UrlRequest { ...@@ -423,7 +424,7 @@ final class CronetUrlRequest implements UrlRequest {
if (mUrlRequestAdapter == 0) { if (mUrlRequestAdapter == 0) {
return; return;
} }
nativeDestroyRequestAdapter(mUrlRequestAdapter); nativeDestroy(mUrlRequestAdapter);
mRequestContext.onRequestDestroyed(this); mRequestContext.onRequestDestroyed(this);
mUrlRequestAdapter = 0; mUrlRequestAdapter = 0;
} }
...@@ -540,7 +541,7 @@ final class CronetUrlRequest implements UrlRequest { ...@@ -540,7 +541,7 @@ final class CronetUrlRequest implements UrlRequest {
if (isCanceled()) { if (isCanceled()) {
return; return;
} }
nativeReceiveData(mUrlRequestAdapter); nativeReadData(mUrlRequestAdapter);
} }
} catch (Exception e) { } catch (Exception e) {
onListenerException(e); onListenerException(e);
...@@ -657,30 +658,39 @@ final class CronetUrlRequest implements UrlRequest { ...@@ -657,30 +658,39 @@ final class CronetUrlRequest implements UrlRequest {
private native long nativeCreateRequestAdapter( private native long nativeCreateRequestAdapter(
long urlRequestContextAdapter, String url, int priority); long urlRequestContextAdapter, String url, int priority);
private native boolean nativeAddHeader(long urlRequestAdapter, String name, @NativeClassQualifiedName("CronetURLRequestAdapter")
String value); private native boolean nativeSetHttpMethod(long nativePtr, String method);
private native boolean nativeSetHttpMethod(long urlRequestAdapter, @NativeClassQualifiedName("CronetURLRequestAdapter")
String method); private native boolean nativeAddRequestHeader(long nativePtr, String name, String value);
private native void nativeStart(long urlRequestAdapter); @NativeClassQualifiedName("CronetURLRequestAdapter")
private native void nativeDisableCache(long nativePtr);
private native void nativeDestroyRequestAdapter(long urlRequestAdapter); @NativeClassQualifiedName("CronetURLRequestAdapter")
private native void nativeStart(long nativePtr);
private native void nativeFollowDeferredRedirect(long urlRequestAdapter); @NativeClassQualifiedName("CronetURLRequestAdapter")
private native void nativeFollowDeferredRedirect(long nativePtr);
private native void nativeReceiveData(long urlRequestAdapter); @NativeClassQualifiedName("CronetURLRequestAdapter")
private native void nativeReadData(long nativePtr);
private native void nativePopulateResponseHeaders(long urlRequestAdapter, @NativeClassQualifiedName("CronetURLRequestAdapter")
HeadersList headers); private native void nativeDestroy(long nativePtr);
private native String nativeGetNegotiatedProtocol(long urlRequestAdapter); @NativeClassQualifiedName("CronetURLRequestAdapter")
private native void nativePopulateResponseHeaders(long nativePtr, HeadersList headers);
private native String nativeGetHttpStatusText(long urlRequestAdapter); @NativeClassQualifiedName("CronetURLRequestAdapter")
private native String nativeGetHttpStatusText(long nativePtr);
private native boolean nativeGetWasCached(long urlRequestAdapter); @NativeClassQualifiedName("CronetURLRequestAdapter")
private native String nativeGetNegotiatedProtocol(long nativePtr);
private native long nativeGetTotalReceivedBytes(long urlRequestAdapter); @NativeClassQualifiedName("CronetURLRequestAdapter")
private native boolean nativeGetWasCached(long nativePtr);
private native void nativeDisableCache(long urlRequestAdapter); @NativeClassQualifiedName("CronetURLRequestAdapter")
private native long nativeGetTotalReceivedBytes(long nativePtr);
} }
...@@ -29,8 +29,6 @@ ...@@ -29,8 +29,6 @@
'android/cronet_upload_data_stream_adapter.h', 'android/cronet_upload_data_stream_adapter.h',
'android/cronet_upload_data_stream_delegate.cc', 'android/cronet_upload_data_stream_delegate.cc',
'android/cronet_upload_data_stream_delegate.h', 'android/cronet_upload_data_stream_delegate.h',
'android/cronet_url_request.cc',
'android/cronet_url_request.h',
'android/cronet_url_request_adapter.cc', 'android/cronet_url_request_adapter.cc',
'android/cronet_url_request_adapter.h', 'android/cronet_url_request_adapter.h',
'android/cronet_url_request_context_adapter.cc', 'android/cronet_url_request_context_adapter.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment