OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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. | |
4 | |
5 #ifndef COMPONENTS_CRONET_ANDROID_CRONET_BIDIRECTIONAL_STREAM_H_ | |
6 #define COMPONENTS_CRONET_ANDROID_CRONET_BIDIRECTIONAL_STREAM_H_ | |
7 | |
8 #include <jni.h> | |
9 #include <string> | |
10 | |
11 #include "base/android/jni_android.h" | |
12 #include "base/android/jni_array.h" | |
13 #include "base/android/jni_string.h" | |
14 #include "base/android/scoped_java_ref.h" | |
15 #include "base/callback.h" | |
16 #include "base/location.h" | |
17 #include "base/macros.h" | |
18 #include "base/memory/ref_counted.h" | |
19 #include "base/memory/scoped_ptr.h" | |
20 #include "net/base/request_priority.h" | |
21 #include "net/http/bidirectional_stream.h" | |
22 #include "url/gurl.h" | |
23 | |
24 namespace base { | |
25 class SingleThreadTaskRunner; | |
xunjieli
2015/12/08 17:59:55
I don't see this being used.
mef
2015/12/11 21:28:25
Done.
| |
26 } // namespace base | |
27 | |
28 namespace net { | |
29 enum Error; | |
xunjieli
2015/12/08 17:59:55
I believe the style guide recommends against forwa
mef
2015/12/11 21:28:25
Done.
| |
30 class HttpRequestHeaders; | |
31 class HttpResponseHeaders; | |
32 class SSLInfo; | |
33 class SpdyHeaderBlock; | |
34 class UploadDataStream; | |
xunjieli
2015/12/08 17:59:55
UploadDataStream is not used.
mef
2015/12/11 21:28:25
Done.
| |
35 } // namespace net | |
36 | |
37 namespace cronet { | |
38 | |
39 class CronetURLRequestContextAdapter; | |
40 | |
41 bool CronetBidirectionalStreamRegisterJni(JNIEnv* env); | |
42 | |
43 // An adapter from Java BidirectionalStream object to net::BidirectionalStream. | |
44 // Created and configured from a Java thread. Start, ReadData, and Destroy are | |
45 // posted to network thread and all callbacks into the Java BidirectionalStream | |
46 // are done on the network thread. Java BidirectionalStream is expected to | |
47 // initiate the next step like ReadData or Destroy. Public methods can be called | |
48 // on any thread except PopulateResponseHeaders and Get* methods, which can only | |
xunjieli
2015/12/08 17:59:55
nit: outdated comment. There isn't a PopulateRespo
mef
2015/12/11 21:28:25
Done.
| |
49 // be called on the network thread. | |
50 class CronetBidirectionalStream : public net::BidirectionalStream::Delegate { | |
51 public: | |
52 CronetBidirectionalStream(CronetURLRequestContextAdapter* context, | |
53 JNIEnv* env, | |
54 jobject jbidi_stream); | |
55 ~CronetBidirectionalStream() override; | |
56 | |
57 // Starts the request. | |
58 jint Start(JNIEnv* env, | |
59 jobject jcaller, | |
60 jstring jurl, | |
61 jstring jmethod, | |
62 jobjectArray jheaders); | |
63 | |
64 // Reads more data. | |
65 jboolean ReadData(JNIEnv* env, | |
66 jobject jcaller, | |
67 jobject jbyte_buffer, | |
68 jint jposition, | |
69 jint jcapacity); | |
70 | |
71 // Writes more data. | |
72 jboolean WriteData(JNIEnv* env, | |
73 jobject jcaller, | |
74 jobject jbyte_buffer, | |
75 jint jposition, | |
76 jint jcapacity, | |
77 jboolean jend_of_stream); | |
78 | |
79 // Releases all resources for the request and deletes the object itself. | |
80 // |jsend_on_canceled| indicates if Java onCanceled callback should be | |
81 // issued to indicate when no more callbacks will be issued. | |
82 void Destroy(JNIEnv* env, jobject jcaller, jboolean jsend_on_canceled); | |
83 | |
84 // Gets NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo. | |
85 // Can only be called on the network thread. | |
86 base::android::ScopedJavaLocalRef<jstring> GetNegotiatedProtocol( | |
87 JNIEnv* env, | |
88 jobject jcaller) const; | |
89 | |
90 // net::BidirectionalStream::Delegate implementations: | |
91 | |
92 // Called when the request headers have been sent. | |
93 void OnRequestHeadersSent() override; | |
94 | |
95 // Called when response headers are received. | |
96 void OnHeaders(const net::SpdyHeaderBlock& response_headers) override; | |
97 | |
98 // Called when read is completed asynchronously. |bytes_read| specifies how | |
99 // much data is available. | |
100 void OnReadCompleted(int bytes_read) override; | |
101 | |
102 // Called when the entire buffer passed through SendData is sent. | |
103 void OnDataSent() override; | |
104 | |
105 // Called when trailers are received. | |
106 void OnTrailers(const net::SpdyHeaderBlock& trailers) override; | |
107 | |
108 // Called when the stream is closed with error. No other delegate functions | |
109 // will be called after this. | |
110 void OnFailed(int error) override; | |
111 | |
112 private: | |
113 class IOBufferWithByteBuffer; | |
114 | |
115 void StartOnNetworkThread( | |
116 scoped_ptr<net::BidirectionalStream::RequestInfo> request_info); | |
117 // Gets headers as Java array. | |
118 base::android::ScopedJavaLocalRef<jobjectArray> GetHeadersArray( | |
119 JNIEnv* env, | |
120 const net::SpdyHeaderBlock& header_block); | |
121 void ReadDataOnNetworkThread( | |
122 scoped_refptr<IOBufferWithByteBuffer> read_buffer, | |
123 int buffer_size); | |
124 void WriteDataOnNetworkThread( | |
125 scoped_refptr<IOBufferWithByteBuffer> read_buffer, | |
126 int buffer_size, | |
127 bool end_of_stream); | |
128 void DestroyOnNetworkThread(bool send_on_canceled); | |
129 | |
130 CronetURLRequestContextAdapter* context_; | |
131 | |
132 // Java object that owns this CronetURLRequestContextAdapter. | |
xunjieli
2015/12/08 17:59:55
nit: s/CronetURLRequestContextAdapter/CrnetBidirec
mef
2015/12/11 21:28:25
Done.
| |
133 base::android::ScopedJavaGlobalRef<jobject> owner_; | |
134 | |
135 scoped_refptr<IOBufferWithByteBuffer> read_buffer_; | |
136 scoped_refptr<IOBufferWithByteBuffer> write_buffer_; | |
137 scoped_ptr<net::BidirectionalStream> bidi_stream_; | |
138 | |
139 DISALLOW_COPY_AND_ASSIGN(CronetBidirectionalStream); | |
140 }; | |
141 | |
142 } // namespace cronet | |
143 | |
144 #endif // COMPONENTS_CRONET_ANDROID_CRONET_BIDIRECTIONAL_STREAM_H_ | |
OLD | NEW |