OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ | 5 #ifndef COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ |
6 #define COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ | 6 #define COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ |
7 | 7 |
8 #include <jni.h> | |
8 #include <string> | 9 #include <string> |
9 | 10 |
11 #include "base/android/jni_android.h" | |
12 #include "base/android/jni_string.h" | |
13 #include "base/android/scoped_java_ref.h" | |
10 #include "base/callback.h" | 14 #include "base/callback.h" |
11 #include "base/location.h" | 15 #include "base/location.h" |
12 #include "base/macros.h" | 16 #include "base/macros.h" |
13 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
14 #include "net/base/request_priority.h" | 18 #include "net/base/request_priority.h" |
15 #include "net/http/http_request_headers.h" | |
16 #include "net/url_request/url_request.h" | 19 #include "net/url_request/url_request.h" |
17 #include "url/gurl.h" | 20 #include "url/gurl.h" |
18 | 21 |
19 namespace base { | 22 namespace base { |
20 class SingleThreadTaskRunner; | 23 class SingleThreadTaskRunner; |
21 } // namespace base | 24 } // namespace base |
22 | 25 |
23 namespace net { | 26 namespace net { |
24 class GrowableIOBuffer; | 27 class GrowableIOBuffer; |
28 class HttpRequestHeaders; | |
25 class HttpResponseHeaders; | 29 class HttpResponseHeaders; |
26 class UploadDataStream; | 30 class UploadDataStream; |
27 } // namespace net | 31 } // namespace net |
28 | 32 |
29 namespace cronet { | 33 namespace cronet { |
30 | 34 |
31 class CronetURLRequestContextAdapter; | 35 class CronetURLRequestContextAdapter; |
32 | 36 |
33 // An adapter from the JNI CronetUrlRequest object to the Chromium URLRequest. | 37 bool CronetUrlRequestAdapterRegisterJni(JNIEnv* env); |
34 // Created and configured from random thread. Start is posted to network | 38 |
35 // thread and all callbacks into CronetURLRequestAdapterDelegate are done on | 39 // An adapter from Java CronetUrlRequest object to net::URLRequest. |
36 // network thread. Each delegate callback is expected to initiate next step like | 40 // Created and configured from a Java thread. Start, ReadData, and Destroy are |
37 // FollowDeferredRedirect, ReadData or Destroy. All methods except those needed | 41 // posted to network thread and all callbacks into the Java CronetUrlRequest are |
38 // to set up a request must be called exclusively on the network thread. | 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. | |
39 class CronetURLRequestAdapter : public net::URLRequest::Delegate { | 46 class CronetURLRequestAdapter : public net::URLRequest::Delegate { |
40 public: | 47 public: |
41 // The delegate which is called when the request adapter completes a step. | 48 CronetURLRequestAdapter(CronetURLRequestContextAdapter* context, |
42 // Always called on network thread. | 49 JNIEnv* env, |
43 class CronetURLRequestAdapterDelegate { | 50 jobject jurl_request, |
44 public: | 51 const GURL& url, |
45 // Called on redirect. Consumer should either destroy the request, or | 52 net::RequestPriority priority); |
46 // call FollowDeferredRedirect. | |
47 virtual void OnRedirect(const GURL& newLocation, int http_status_code) = 0; | |
48 // Called when response has started and headers have been received. Consumer | |
49 // should either destroy the request, or call ReadData. | |
50 virtual void OnResponseStarted(int http_status_code) = 0; | |
51 // Called when response bytes were read. Consumer should consume data and | |
52 // either destroy the request, or call ReadData. The request may only be | |
53 // destroyed after the embedder is done with |bytes|, as deleting the | |
54 // request frees the buffer. | |
55 virtual void OnBytesRead(unsigned char* bytes, | |
56 int bytes_read) = 0; | |
57 // Called when response has finished successfully. Consumer should destroy | |
58 // the request. | |
59 virtual void OnRequestFinished() = 0; | |
60 // Called when response has finished with error. Consumer should destroy | |
61 // the request. | |
62 virtual void OnError(int net_error) = 0; | |
63 | |
64 virtual ~CronetURLRequestAdapterDelegate() {} | |
65 }; | |
66 | |
67 CronetURLRequestAdapter( | |
68 CronetURLRequestContextAdapter* context, | |
69 scoped_ptr<CronetURLRequestAdapterDelegate> delegate, | |
70 const GURL& url, | |
71 net::RequestPriority priority); | |
72 ~CronetURLRequestAdapter() override; | 53 ~CronetURLRequestAdapter() override; |
73 | 54 |
74 // Methods called prior to Start are never called on network thread. | 55 // Methods called prior to Start are never called on network thread. |
75 | 56 |
76 // Sets the request method GET, POST etc. | 57 // Sets the request method GET, POST etc. |
77 void set_method(const std::string& method) { | 58 jboolean SetHttpMethod(JNIEnv* env, jobject jcaller, jstring jmethod); |
78 initial_method_ = method; | |
79 } | |
80 | 59 |
81 // Adds a header to the request before it starts. | 60 // Adds a header to the request before it starts. |
82 void AddRequestHeader(const std::string& name, const std::string& value); | 61 jboolean AddRequestHeader(JNIEnv* env, |
62 jobject jcaller, | |
63 jstring jname, | |
64 jstring jvalue); | |
65 | |
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); | |
83 | 69 |
84 // Adds a request body to the request before it starts. | 70 // Adds a request body to the request before it starts. |
85 void SetUpload(scoped_ptr<net::UploadDataStream> upload); | 71 void SetUpload(scoped_ptr<net::UploadDataStream> upload); |
86 | 72 |
87 // Methods called on any thread. | |
88 | |
89 // Posts tasks to network thread. | |
90 void PostTaskToNetworkThread(const tracked_objects::Location& from_here, | |
91 const base::Closure& task); | |
92 | |
93 // Returns true if called on network thread. | |
94 bool IsOnNetworkThread() const; | |
95 | |
96 // Methods called only on network thread. | |
97 | |
98 // Starts the request. | 73 // Starts the request. |
99 void Start(); | 74 void Start(JNIEnv* env, jobject jcaller); |
100 | 75 |
101 // Follows redirect. | 76 // Follows redirect. |
102 void FollowDeferredRedirect(); | 77 void FollowDeferredRedirect(JNIEnv* env, jobject jcaller); |
103 | 78 |
104 // Reads more data. | 79 // Reads more data. |
105 void ReadData(); | 80 void ReadData(JNIEnv* env, jobject jcaller); |
106 | 81 |
107 // Releases all resources for the request and deletes the object itself. | 82 // Releases all resources for the request and deletes the object itself. |
108 void Destroy(); | 83 void Destroy(JNIEnv* env, jobject jcaller); |
84 | |
85 // Populate response headers on network thread. | |
86 void PopulateResponseHeaders(JNIEnv* env, | |
87 jobject jcaller, | |
88 jobject jheaders_list); | |
109 | 89 |
110 // When called during a OnRedirect or OnResponseStarted callback, these | 90 // When called during a OnRedirect or OnResponseStarted callback, these |
111 // methods return the corresponding response information. | 91 // methods return the corresponding response information. These methods |
92 // can only be called on the network thread. | |
112 | 93 |
113 // Gets all response headers, as a HttpResponseHeaders object. Returns NULL | 94 base::android::ScopedJavaLocalRef<jstring> GetHttpStatusText( |
mef
2015/04/06 20:41:30
add comment.
xunjieli
2015/04/06 21:28:20
Done.
| |
114 // if the last attempted request received no headers. | 95 JNIEnv* env, |
115 const net::HttpResponseHeaders* GetResponseHeaders() const; | 96 jobject jcaller) const; |
116 | 97 |
117 // Gets NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo. | 98 // Gets NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo. |
118 const std::string& GetNegotiatedProtocol() const; | 99 base::android::ScopedJavaLocalRef<jstring> GetNegotiatedProtocol( |
100 JNIEnv* env, | |
101 jobject jcaller) const; | |
119 | 102 |
120 // Returns true if response is coming from the cache. | 103 // Returns true if response is coming from the cache. |
121 bool GetWasCached() const; | 104 jboolean GetWasCached(JNIEnv* env, jobject jcaller) const; |
122 | 105 |
123 // Gets the total amount of body data received from network after | 106 // Gets the total amount of body data received from network after |
124 // SSL/SPDY/QUIC decoding and proxy handling. Basically the size of the body | 107 // SSL/SPDY/QUIC decoding and proxy handling. Basically the size of the body |
125 // prior to decompression. | 108 // prior to decompression. |
126 int64 GetTotalReceivedBytes() const; | 109 int64 GetTotalReceivedBytes(JNIEnv* env, jobject jcaller) const; |
127 | 110 |
128 // Bypasses cache. If context is not set up to use cache, this call has no | 111 // net::URLRequest::Delegate implementations: |
129 // effect. | |
130 void DisableCache(); | |
131 | 112 |
132 // net::URLRequest::Delegate overrides. | |
133 void OnReceivedRedirect(net::URLRequest* request, | 113 void OnReceivedRedirect(net::URLRequest* request, |
134 const net::RedirectInfo& redirect_info, | 114 const net::RedirectInfo& redirect_info, |
135 bool* defer_redirect) override; | 115 bool* defer_redirect) override; |
136 void OnResponseStarted(net::URLRequest* request) override; | 116 void OnResponseStarted(net::URLRequest* request) override; |
137 void OnReadCompleted(net::URLRequest* request, int bytes_read) override; | 117 void OnReadCompleted(net::URLRequest* request, int bytes_read) override; |
138 | 118 |
139 private: | 119 private: |
120 void StartOnNetworkThread(); | |
121 void FollowDeferredRedirectOnNetworkThread(); | |
122 void ReadDataOnNetworkThread(); | |
123 void DestroyOnNetworkThread(); | |
124 | |
140 // Checks status of the request_adapter, return false if |is_success()| is | 125 // Checks status of the request_adapter, return false if |is_success()| is |
141 // true, otherwise report error and cancel request_adapter. | 126 // true, otherwise report error and cancel request_adapter. |
142 bool MaybeReportError(net::URLRequest* request) const; | 127 bool MaybeReportError(net::URLRequest* request) const; |
143 | 128 |
144 CronetURLRequestContextAdapter* context_; | 129 CronetURLRequestContextAdapter* context_; |
145 scoped_ptr<CronetURLRequestAdapterDelegate> delegate_; | 130 |
131 // Java object that owns the CronetURLRequestContextAdapter. | |
mef
2015/04/06 20:41:31
owns this CronetURLRequestAdapter
xunjieli
2015/04/06 21:28:20
Done.
| |
132 base::android::ScopedJavaGlobalRef<jobject> owner_; | |
146 | 133 |
147 const GURL initial_url_; | 134 const GURL initial_url_; |
148 const net::RequestPriority initial_priority_; | 135 const net::RequestPriority initial_priority_; |
149 std::string initial_method_; | 136 std::string initial_method_; |
150 int load_flags_; | 137 int load_flags_; |
151 net::HttpRequestHeaders initial_request_headers_; | 138 net::HttpRequestHeaders initial_request_headers_; |
152 scoped_ptr<net::UploadDataStream> upload_; | 139 scoped_ptr<net::UploadDataStream> upload_; |
153 | 140 |
154 scoped_refptr<net::IOBufferWithSize> read_buffer_; | 141 scoped_refptr<net::IOBufferWithSize> read_buffer_; |
155 scoped_ptr<net::URLRequest> url_request_; | 142 scoped_ptr<net::URLRequest> url_request_; |
156 | 143 |
157 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestAdapter); | 144 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestAdapter); |
158 }; | 145 }; |
159 | 146 |
160 } // namespace cronet | 147 } // namespace cronet |
161 | 148 |
162 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ | 149 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ |
OLD | NEW |