Revert of Refactor the avatar button/icon class (patchset #14 id:320001 of https...
[chromium-blink-merge.git] / components / cronet / android / cronet_url_request_adapter.h
blob301c0b57b1017946251d68376362405d24648afd
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 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;
118 private:
119 class IOBufferWithByteBuffer;
121 void StartOnNetworkThread();
122 void FollowDeferredRedirectOnNetworkThread();
123 void ReadDataOnNetworkThread(
124 scoped_refptr<IOBufferWithByteBuffer> read_buffer,
125 int buffer_size);
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_;
140 int load_flags_;
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_