Roll src/third_party/WebKit aa8346d:dbb8a38 (svn 202629:202630)
[chromium-blink-merge.git] / components / cronet / android / cronet_url_request_adapter.h
blobd7e611a4113431386589223e32963fb2920aef11
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_
8 #include <jni.h>
9 #include <string>
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"
21 #include "url/gurl.h"
23 namespace base {
24 class SingleThreadTaskRunner;
25 } // namespace base
27 namespace net {
28 class HttpRequestHeaders;
29 class HttpResponseHeaders;
30 class SSLInfo;
31 class UploadDataStream;
32 } // namespace net
34 namespace cronet {
36 class CronetURLRequestContextAdapter;
38 bool CronetUrlRequestAdapterRegisterJni(JNIEnv* env);
40 // An adapter from Java CronetUrlRequest object to net::URLRequest.
41 // Created and configured from a Java thread. Start, ReadData, and Destroy are
42 // posted to network thread and all callbacks into the Java CronetUrlRequest are
43 // done on the network thread. Java CronetUrlRequest is expected to initiate the
44 // next step like FollowDeferredRedirect, ReadData or Destroy. Public methods
45 // can be called on any thread except PopulateResponseHeaders and Get* methods,
46 // which can only be called on the network thread.
47 class CronetURLRequestAdapter : public net::URLRequest::Delegate {
48 public:
49 CronetURLRequestAdapter(CronetURLRequestContextAdapter* context,
50 JNIEnv* env,
51 jobject jurl_request,
52 const GURL& url,
53 net::RequestPriority priority);
54 ~CronetURLRequestAdapter() override;
56 // Methods called prior to Start are never called on network thread.
58 // Sets the request method GET, POST etc.
59 jboolean SetHttpMethod(JNIEnv* env, jobject jcaller, jstring jmethod);
61 // Adds a header to the request before it starts.
62 jboolean AddRequestHeader(JNIEnv* env,
63 jobject jcaller,
64 jstring jname,
65 jstring jvalue);
67 // Bypasses cache. If context is not set up to use cache, this call has no
68 // effect.
69 void DisableCache(JNIEnv* env, jobject jcaller);
71 // Adds a request body to the request before it starts.
72 void SetUpload(scoped_ptr<net::UploadDataStream> upload);
74 // Starts the request.
75 void Start(JNIEnv* env, jobject jcaller);
77 void GetStatus(JNIEnv* env, jobject jcaller, jobject jstatus_listener) const;
79 // Follows redirect.
80 void FollowDeferredRedirect(JNIEnv* env, jobject jcaller);
82 // Reads more data.
83 jboolean ReadData(JNIEnv* env, jobject jcaller,
84 jobject jbyte_buffer,
85 jint jposition,
86 jint jcapacity);
88 // Releases all resources for the request and deletes the object itself.
89 void Destroy(JNIEnv* env, jobject jcaller);
91 // Populate response headers on network thread.
92 void PopulateResponseHeaders(JNIEnv* env,
93 jobject jcaller,
94 jobject jheaders_list);
96 // When called during a OnRedirect or OnResponseStarted callback, these
97 // methods return the corresponding response information. These methods
98 // can only be called on the network thread.
100 // Gets http status text from the response headers.
101 base::android::ScopedJavaLocalRef<jstring> GetHttpStatusText(
102 JNIEnv* env,
103 jobject jcaller) const;
105 // Gets NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo.
106 base::android::ScopedJavaLocalRef<jstring> GetNegotiatedProtocol(
107 JNIEnv* env,
108 jobject jcaller) const;
110 // Returns the host and port of the proxy server, if one was used.
111 base::android::ScopedJavaLocalRef<jstring> GetProxyServer(
112 JNIEnv* env,
113 jobject jcaller) const;
115 // Returns true if response is coming from the cache.
116 jboolean GetWasCached(JNIEnv* env, jobject jcaller) const;
118 // net::URLRequest::Delegate implementations:
120 void OnReceivedRedirect(net::URLRequest* request,
121 const net::RedirectInfo& redirect_info,
122 bool* defer_redirect) override;
123 void OnSSLCertificateError(net::URLRequest* request,
124 const net::SSLInfo& ssl_info,
125 bool fatal) override;
126 void OnResponseStarted(net::URLRequest* request) override;
127 void OnReadCompleted(net::URLRequest* request, int bytes_read) override;
129 private:
130 class IOBufferWithByteBuffer;
132 void StartOnNetworkThread();
133 void GetStatusOnNetworkThread(
134 const base::android::ScopedJavaGlobalRef<jobject>& jstatus_listener_ref)
135 const;
136 void FollowDeferredRedirectOnNetworkThread();
137 void ReadDataOnNetworkThread(
138 scoped_refptr<IOBufferWithByteBuffer> read_buffer,
139 int buffer_size);
140 void DestroyOnNetworkThread();
142 // Checks status of the request_adapter, return false if |is_success()| is
143 // true, otherwise report error and cancel request_adapter.
144 bool MaybeReportError(net::URLRequest* request) const;
146 CronetURLRequestContextAdapter* context_;
148 // Java object that owns this CronetURLRequestContextAdapter.
149 base::android::ScopedJavaGlobalRef<jobject> owner_;
151 const GURL initial_url_;
152 const net::RequestPriority initial_priority_;
153 std::string initial_method_;
154 int load_flags_;
155 net::HttpRequestHeaders initial_request_headers_;
156 scoped_ptr<net::UploadDataStream> upload_;
158 scoped_refptr<IOBufferWithByteBuffer> read_buffer_;
159 scoped_ptr<net::URLRequest> url_request_;
161 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestAdapter);
164 } // namespace cronet
166 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_