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 |
37 bool CronetUrlRequestAdapterRegisterJni(JNIEnv* env); | |
38 | |
33 // An adapter from the JNI CronetUrlRequest object to the Chromium URLRequest. | 39 // An adapter from the JNI CronetUrlRequest object to the Chromium URLRequest. |
34 // Created and configured from random thread. Start is posted to network | 40 // Created and configured from random thread. Start is posted to network |
35 // thread and all callbacks into CronetURLRequestAdapterDelegate are done on | 41 // thread and all callbacks into CronetURLRequestAdapterDelegate are done on |
36 // network thread. Each delegate callback is expected to initiate next step like | 42 // network thread. Each delegate callback is expected to initiate next step like |
37 // FollowDeferredRedirect, ReadData or Destroy. All methods except those needed | 43 // FollowDeferredRedirect, ReadData or Destroy. All methods except those needed |
38 // to set up a request must be called exclusively on the network thread. | 44 // to set up a request must be called exclusively on the network thread. |
mmenke
2015/04/02 21:07:05
Update docs.
xunjieli
2015/04/02 21:50:13
Done.
| |
39 class CronetURLRequestAdapter : public net::URLRequest::Delegate { | 45 class CronetURLRequestAdapter : public net::URLRequest::Delegate { |
40 public: | 46 public: |
41 // The delegate which is called when the request adapter completes a step. | 47 CronetURLRequestAdapter(CronetURLRequestContextAdapter* context, |
42 // Always called on network thread. | 48 JNIEnv* env, |
43 class CronetURLRequestAdapterDelegate { | 49 jobject jurl_request, |
44 public: | 50 const GURL& url, |
45 // Called on redirect. Consumer should either destroy the request, or | 51 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; | 52 ~CronetURLRequestAdapter() override; |
73 | 53 |
74 // Methods called prior to Start are never called on network thread. | 54 // Methods called prior to Start are never called on network thread. |
75 | 55 |
76 // Sets the request method GET, POST etc. | 56 // Sets the request method GET, POST etc. |
77 void set_method(const std::string& method) { | 57 jboolean SetHttpMethod(JNIEnv* env, jobject jcaller, jstring jmethod); |
78 initial_method_ = method; | |
79 } | |
80 | 58 |
81 // Adds a header to the request before it starts. | 59 // Adds a header to the request before it starts. |
82 void AddRequestHeader(const std::string& name, const std::string& value); | 60 jboolean AddRequestHeader(JNIEnv* env, |
61 jobject jcaller, | |
62 jstring jname, | |
63 jstring jvalue); | |
83 | 64 |
84 // Adds a request body to the request before it starts. | 65 // Adds a request body to the request before it starts. |
85 void SetUpload(scoped_ptr<net::UploadDataStream> upload); | 66 void SetUpload(scoped_ptr<net::UploadDataStream> upload); |
86 | 67 |
87 // Methods called on any thread. | 68 // Starts the request. |
69 void Start(JNIEnv* env, jobject jcaller); | |
88 | 70 |
89 // Posts tasks to network thread. | 71 // Reads more data. |
90 void PostTaskToNetworkThread(const tracked_objects::Location& from_here, | 72 void ReadData(JNIEnv* env, jobject jcaller); |
91 const base::Closure& task); | |
92 | 73 |
93 // Returns true if called on network thread. | 74 // Releases all resources for the request and deletes the object itself. |
94 bool IsOnNetworkThread() const; | 75 void Destroy(JNIEnv* env, jobject jcaller); |
95 | 76 |
96 // Methods called only on network thread. | 77 // Populate response headers on network thread. |
97 | 78 void PopulateResponseHeaders(JNIEnv* env, |
98 // Starts the request. | 79 jobject jcaller, |
99 void Start(); | 80 jobject jheaders_list); |
100 | 81 |
101 // Follows redirect. | 82 // Follows redirect. |
102 void FollowDeferredRedirect(); | 83 void FollowDeferredRedirect(JNIEnv* env, jobject jcaller); |
mmenke
2015/04/02 21:07:05
Suggest putting this between Start and ReadData, t
xunjieli
2015/04/02 21:50:13
Done.
| |
103 | 84 |
104 // Reads more data. | 85 // Bypasses cache. If context is not set up to use cache, this call has no |
105 void ReadData(); | 86 // effect. |
106 | 87 void DisableCache(JNIEnv* env, jobject jcaller); |
mmenke
2015/04/02 21:07:05
This should go up with the other ones that must be
xunjieli
2015/04/02 21:50:13
Done. Good rule for ordering. Thanks!
| |
107 // Releases all resources for the request and deletes the object itself. | |
108 void Destroy(); | |
109 | 88 |
110 // When called during a OnRedirect or OnResponseStarted callback, these | 89 // When called during a OnRedirect or OnResponseStarted callback, these |
111 // methods return the corresponding response information. | 90 // methods return the corresponding response information. |
112 | 91 |
113 // Gets all response headers, as a HttpResponseHeaders object. Returns NULL | 92 base::android::ScopedJavaLocalRef<jstring> GetHttpStatusText( |
114 // if the last attempted request received no headers. | 93 JNIEnv* env, |
115 const net::HttpResponseHeaders* GetResponseHeaders() const; | 94 jobject jcaller) const; |
116 | 95 |
117 // Gets NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo. | 96 // Gets NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo. |
118 const std::string& GetNegotiatedProtocol() const; | 97 base::android::ScopedJavaLocalRef<jstring> GetNegotiatedProtocol( |
98 JNIEnv* env, | |
99 jobject jcaller) const; | |
119 | 100 |
120 // Returns true if response is coming from the cache. | 101 // Returns true if response is coming from the cache. |
121 bool GetWasCached() const; | 102 jboolean GetWasCached(JNIEnv* env, jobject jcaller) const; |
122 | 103 |
123 // Gets the total amount of body data received from network after | 104 // 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 | 105 // SSL/SPDY/QUIC decoding and proxy handling. Basically the size of the body |
125 // prior to decompression. | 106 // prior to decompression. |
126 int64 GetTotalReceivedBytes() const; | 107 int64 GetTotalReceivedBytes(JNIEnv* env, jobject jcaller) const; |
127 | 108 |
128 // Bypasses cache. If context is not set up to use cache, this call has no | 109 // net::URLRequest::Delegate implementations: |
129 // effect. | |
130 void DisableCache(); | |
131 | 110 |
132 // net::URLRequest::Delegate overrides. | |
133 void OnReceivedRedirect(net::URLRequest* request, | 111 void OnReceivedRedirect(net::URLRequest* request, |
134 const net::RedirectInfo& redirect_info, | 112 const net::RedirectInfo& redirect_info, |
135 bool* defer_redirect) override; | 113 bool* defer_redirect) override; |
136 void OnResponseStarted(net::URLRequest* request) override; | 114 void OnResponseStarted(net::URLRequest* request) override; |
137 void OnReadCompleted(net::URLRequest* request, int bytes_read) override; | 115 void OnReadCompleted(net::URLRequest* request, int bytes_read) override; |
138 | 116 |
139 private: | 117 private: |
118 void StartOnNetworkThread(); | |
119 void ReadDataOnNetworkThread(); | |
120 void DestroyOnNetworkThread(); | |
121 | |
140 // Checks status of the request_adapter, return false if |is_success()| is | 122 // Checks status of the request_adapter, return false if |is_success()| is |
141 // true, otherwise report error and cancel request_adapter. | 123 // true, otherwise report error and cancel request_adapter. |
142 bool MaybeReportError(net::URLRequest* request) const; | 124 bool MaybeReportError(net::URLRequest* request) const; |
143 | 125 |
144 CronetURLRequestContextAdapter* context_; | 126 CronetURLRequestContextAdapter* context_; |
145 scoped_ptr<CronetURLRequestAdapterDelegate> delegate_; | 127 |
128 // Java object that owns the CronetURLRequestContextAdapter. | |
129 base::android::ScopedJavaGlobalRef<jobject> owner_; | |
146 | 130 |
147 const GURL initial_url_; | 131 const GURL initial_url_; |
148 const net::RequestPriority initial_priority_; | 132 const net::RequestPriority initial_priority_; |
149 std::string initial_method_; | 133 std::string initial_method_; |
150 int load_flags_; | 134 int load_flags_; |
151 net::HttpRequestHeaders initial_request_headers_; | 135 net::HttpRequestHeaders initial_request_headers_; |
152 scoped_ptr<net::UploadDataStream> upload_; | 136 scoped_ptr<net::UploadDataStream> upload_; |
153 | 137 |
154 scoped_refptr<net::IOBufferWithSize> read_buffer_; | 138 scoped_refptr<net::IOBufferWithSize> read_buffer_; |
155 scoped_ptr<net::URLRequest> url_request_; | 139 scoped_ptr<net::URLRequest> url_request_; |
156 | 140 |
157 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestAdapter); | 141 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestAdapter); |
158 }; | 142 }; |
159 | 143 |
160 } // namespace cronet | 144 } // namespace cronet |
161 | 145 |
162 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ | 146 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ |
OLD | NEW |