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/scoped_ptr.h"
18 #include "net/base/request_priority.h"
19 #include "net/url_request/url_request.h"
23 class SingleThreadTaskRunner
;
27 class GrowableIOBuffer
;
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
);
77 void FollowDeferredRedirect(JNIEnv
* env
, jobject jcaller
);
80 void ReadData(JNIEnv
* env
, jobject jcaller
);
82 // Releases all resources for the request and deletes the object itself.
83 void Destroy(JNIEnv
* env
, jobject jcaller
);
85 // Populate response headers on network thread.
86 void PopulateResponseHeaders(JNIEnv
* env
,
88 jobject jheaders_list
);
90 // When called during a OnRedirect or OnResponseStarted callback, these
91 // methods return the corresponding response information. These methods
92 // can only be called on the network thread.
94 // Gets http status text from the response headers.
95 base::android::ScopedJavaLocalRef
<jstring
> GetHttpStatusText(
97 jobject jcaller
) const;
99 // Gets NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo.
100 base::android::ScopedJavaLocalRef
<jstring
> GetNegotiatedProtocol(
102 jobject jcaller
) const;
104 // Returns true if response is coming from the cache.
105 jboolean
GetWasCached(JNIEnv
* env
, jobject jcaller
) const;
107 // Gets the total amount of body data received from network after
108 // SSL/SPDY/QUIC decoding and proxy handling. Basically the size of the body
109 // prior to decompression.
110 int64
GetTotalReceivedBytes(JNIEnv
* env
, jobject jcaller
) const;
112 // net::URLRequest::Delegate implementations:
114 void OnReceivedRedirect(net::URLRequest
* request
,
115 const net::RedirectInfo
& redirect_info
,
116 bool* defer_redirect
) override
;
117 void OnResponseStarted(net::URLRequest
* request
) override
;
118 void OnReadCompleted(net::URLRequest
* request
, int bytes_read
) override
;
121 void StartOnNetworkThread();
122 void FollowDeferredRedirectOnNetworkThread();
123 void ReadDataOnNetworkThread();
124 void DestroyOnNetworkThread();
126 // Checks status of the request_adapter, return false if |is_success()| is
127 // true, otherwise report error and cancel request_adapter.
128 bool MaybeReportError(net::URLRequest
* request
) const;
130 CronetURLRequestContextAdapter
* context_
;
132 // Java object that owns this CronetURLRequestContextAdapter.
133 base::android::ScopedJavaGlobalRef
<jobject
> owner_
;
135 const GURL initial_url_
;
136 const net::RequestPriority initial_priority_
;
137 std::string initial_method_
;
139 net::HttpRequestHeaders initial_request_headers_
;
140 scoped_ptr
<net::UploadDataStream
> upload_
;
142 scoped_refptr
<net::IOBufferWithSize
> read_buffer_
;
143 scoped_ptr
<net::URLRequest
> url_request_
;
145 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestAdapter
);
148 } // namespace cronet
150 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_