Reland "Non-SFI mode: Switch to newlib. (patchset #4 id:60001 of https://codereview...
[chromium-blink-merge.git] / components / cronet / android / cronet_url_request_adapter.h
blob4075e57437d0a79241ceca3ccef585ec57f02f0a
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 UploadDataStream;
31 } // namespace net
33 namespace cronet {
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 {
47 public:
48 CronetURLRequestAdapter(CronetURLRequestContextAdapter* context,
49 JNIEnv* env,
50 jobject jurl_request,
51 const GURL& url,
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,
62 jobject jcaller,
63 jstring jname,
64 jstring jvalue);
66 // Bypasses cache. If context is not set up to use cache, this call has no
67 // effect.
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);
76 // Follows redirect.
77 void FollowDeferredRedirect(JNIEnv* env, jobject jcaller);
79 // Reads more data.
80 jboolean ReadData(JNIEnv* env, jobject jcaller,
81 jobject jbyte_buffer,
82 jint jposition,
83 jint jcapacity);
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,
90 jobject jcaller,
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(
99 JNIEnv* env,
100 jobject jcaller) const;
102 // Gets NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo.
103 base::android::ScopedJavaLocalRef<jstring> GetNegotiatedProtocol(
104 JNIEnv* env,
105 jobject jcaller) const;
107 // Returns the host and port of the proxy server, if one was used.
108 base::android::ScopedJavaLocalRef<jstring> GetProxyServer(
109 JNIEnv* env,
110 jobject jcaller) const;
112 // Returns true if response is coming from the cache.
113 jboolean GetWasCached(JNIEnv* env, jobject jcaller) const;
115 // net::URLRequest::Delegate implementations:
117 void OnReceivedRedirect(net::URLRequest* request,
118 const net::RedirectInfo& redirect_info,
119 bool* defer_redirect) override;
120 void OnResponseStarted(net::URLRequest* request) override;
121 void OnReadCompleted(net::URLRequest* request, int bytes_read) override;
123 private:
124 class IOBufferWithByteBuffer;
126 void StartOnNetworkThread();
127 void FollowDeferredRedirectOnNetworkThread();
128 void ReadDataOnNetworkThread(
129 scoped_refptr<IOBufferWithByteBuffer> read_buffer,
130 int buffer_size);
131 void DestroyOnNetworkThread();
133 // Checks status of the request_adapter, return false if |is_success()| is
134 // true, otherwise report error and cancel request_adapter.
135 bool MaybeReportError(net::URLRequest* request) const;
137 CronetURLRequestContextAdapter* context_;
139 // Java object that owns this CronetURLRequestContextAdapter.
140 base::android::ScopedJavaGlobalRef<jobject> owner_;
142 const GURL initial_url_;
143 const net::RequestPriority initial_priority_;
144 std::string initial_method_;
145 int load_flags_;
146 net::HttpRequestHeaders initial_request_headers_;
147 scoped_ptr<net::UploadDataStream> upload_;
149 scoped_refptr<IOBufferWithByteBuffer> read_buffer_;
150 scoped_ptr<net::URLRequest> url_request_;
152 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestAdapter);
155 } // namespace cronet
157 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_