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
);
77 void FollowDeferredRedirect(JNIEnv
* env
, jobject jcaller
);
80 jboolean
ReadData(JNIEnv
* env
, jobject jcaller
,
85 // Releases all resources for the request and deletes the object itself.
86 void Destroy(JNIEnv
* env
, jobject jcaller
);
88 // Populate response headers on network thread.
89 void PopulateResponseHeaders(JNIEnv
* env
,
91 jobject jheaders_list
);
93 // When called during a OnRedirect or OnResponseStarted callback, these
94 // methods return the corresponding response information. These methods
95 // can only be called on the network thread.
97 // Gets http status text from the response headers.
98 base::android::ScopedJavaLocalRef
<jstring
> GetHttpStatusText(
100 jobject jcaller
) const;
102 // Gets NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo.
103 base::android::ScopedJavaLocalRef
<jstring
> GetNegotiatedProtocol(
105 jobject jcaller
) const;
107 // Returns true if response is coming from the cache.
108 jboolean
GetWasCached(JNIEnv
* env
, jobject jcaller
) const;
110 // net::URLRequest::Delegate implementations:
112 void OnReceivedRedirect(net::URLRequest
* request
,
113 const net::RedirectInfo
& redirect_info
,
114 bool* defer_redirect
) override
;
115 void OnResponseStarted(net::URLRequest
* request
) override
;
116 void OnReadCompleted(net::URLRequest
* request
, int bytes_read
) override
;
119 class IOBufferWithByteBuffer
;
121 void StartOnNetworkThread();
122 void FollowDeferredRedirectOnNetworkThread();
123 void ReadDataOnNetworkThread(
124 scoped_refptr
<IOBufferWithByteBuffer
> read_buffer
,
126 void DestroyOnNetworkThread();
128 // Checks status of the request_adapter, return false if |is_success()| is
129 // true, otherwise report error and cancel request_adapter.
130 bool MaybeReportError(net::URLRequest
* request
) const;
132 CronetURLRequestContextAdapter
* context_
;
134 // Java object that owns this CronetURLRequestContextAdapter.
135 base::android::ScopedJavaGlobalRef
<jobject
> owner_
;
137 const GURL initial_url_
;
138 const net::RequestPriority initial_priority_
;
139 std::string initial_method_
;
141 net::HttpRequestHeaders initial_request_headers_
;
142 scoped_ptr
<net::UploadDataStream
> upload_
;
144 scoped_refptr
<IOBufferWithByteBuffer
> read_buffer_
;
145 scoped_ptr
<net::URLRequest
> url_request_
;
147 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestAdapter
);
150 } // namespace cronet
152 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_