Index: components/cronet/android/cronet_bidirectional_stream_adapter.h |
diff --git a/components/cronet/android/cronet_bidirectional_stream_adapter.h b/components/cronet/android/cronet_bidirectional_stream_adapter.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..44833bc4f7a7387a3ddbcf827dbd61cd39786a7b |
--- /dev/null |
+++ b/components/cronet/android/cronet_bidirectional_stream_adapter.h |
@@ -0,0 +1,127 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef COMPONENTS_CRONET_ANDROID_CRONET_BIDIRECTIONAL_STREAM_ADAPTER_H_ |
+#define COMPONENTS_CRONET_ANDROID_CRONET_BIDIRECTIONAL_STREAM_ADAPTER_H_ |
+ |
+#include <jni.h> |
+#include <string> |
+ |
+#include "base/android/jni_android.h" |
+#include "base/android/jni_array.h" |
+#include "base/android/jni_string.h" |
+#include "base/android/scoped_java_ref.h" |
+#include "base/macros.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "net/base/request_priority.h" |
+#include "net/http/bidirectional_stream.h" |
+#include "url/gurl.h" |
+ |
+namespace net { |
+class BidirectionalStreamRequestInfo; |
+class SpdyHeaderBlock; |
+} // namespace net |
+ |
+using base::android::JavaParamRef; |
pauljensen
2016/01/19 16:03:39
This is not allowed in style guide: "Don't put an
mef
2016/01/20 15:37:40
Argh! I could argue that this class is NOT public
|
+ |
+namespace cronet { |
+ |
+class CronetURLRequestContextAdapter; |
+ |
+bool CronetBidirectionalStreamAdapterRegisterJni(JNIEnv* env); |
pauljensen
2016/01/19 16:03:39
nit: what about moving to a static member CronetBi
mef
2016/01/20 15:37:40
Done.
|
+ |
+// An adapter from Java BidirectionalStream object to net::BidirectionalStream. |
+// Created and configured from a Java thread. Start, ReadData, and Destroy are |
pauljensen
2016/01/19 16:03:39
This doesn't seem true. ReadData() is called on s
mef
2016/01/20 15:37:40
Done.
|
+// posted to network thread and all callbacks into the Java BidirectionalStream |
+// are done on the network thread. Java BidirectionalStream is expected to |
+// initiate the next step like ReadData or Destroy. Public methods can be called |
+// on any thread except Get* methods, which can only be called on the network |
pauljensen
2016/01/19 16:03:40
nit: I only see one Get* method; could just say "G
mef
2016/01/20 15:37:40
GetHeadersArray is now just a private helper metho
|
+// thread. |
+class CronetBidirectionalStreamAdapter |
+ : public net::BidirectionalStream::Delegate { |
+ public: |
+ CronetBidirectionalStreamAdapter(CronetURLRequestContextAdapter* context, |
+ JNIEnv* env, |
+ const JavaParamRef<jobject>& jbidi_stream); |
+ ~CronetBidirectionalStreamAdapter() override; |
+ |
+ // Validates method and headers, initializes and starts the request. If |
+ // |jend_of_stream| is true, then stream is half-closed after sending header |
+ // frame and no data is expected to be written. |
+ // Returns 0 if request is valid and started successfully, |
+ // Returns -1 if |jmethod| is not valid HTTP method name. |
+ // Returns position of invalid header value in |jheaders| if header name is |
+ // not valid. |
pauljensen
2016/01/19 16:03:39
isn't it position+1?
mef
2016/01/20 15:37:40
position of header value == position of header nam
|
+ jint Start(JNIEnv* env, |
+ const JavaParamRef<jobject>& jcaller, |
+ const JavaParamRef<jstring>& jurl, |
+ jint jpriority, |
+ const JavaParamRef<jstring>& jmethod, |
+ const JavaParamRef<jobjectArray>& jheaders, |
+ jboolean jend_of_stream); |
+ |
+ // Reads more data. |
+ jboolean ReadData(JNIEnv* env, |
+ const JavaParamRef<jobject>& jcaller, |
+ const JavaParamRef<jobject>& jbyte_buffer, |
+ jint jposition, |
+ jint jlimit); |
+ |
+ // Writes more data. |
+ jboolean WriteData(JNIEnv* env, |
+ const JavaParamRef<jobject>& jcaller, |
+ const JavaParamRef<jobject>& jbyte_buffer, |
+ jint jposition, |
+ jint jlimit, |
+ jboolean jend_of_stream); |
+ |
+ // Releases all resources for the request and deletes the object itself. |
+ // |jsend_on_canceled| indicates if Java onCanceled callback should be |
+ // issued to indicate that no more callbacks will be issued. |
+ void Destroy(JNIEnv* env, |
+ const JavaParamRef<jobject>& jcaller, |
+ jboolean jsend_on_canceled); |
+ |
+ // net::BidirectionalStream::Delegate implementations: |
+ void OnHeadersSent() override; |
+ void OnHeadersReceived(const net::SpdyHeaderBlock& response_headers) override; |
+ void OnDataRead(int bytes_read) override; |
+ void OnDataSent() override; |
+ void OnTrailersReceived(const net::SpdyHeaderBlock& trailers) override; |
+ void OnFailed(int error) override; |
+ |
+ private: |
+ class IOBufferWithByteBuffer; |
+ |
+ void StartOnNetworkThread( |
+ scoped_ptr<net::BidirectionalStreamRequestInfo> request_info); |
+ // Gets headers as Java array. |
+ base::android::ScopedJavaLocalRef<jobjectArray> GetHeadersArray( |
+ JNIEnv* env, |
+ const net::SpdyHeaderBlock& header_block); |
+ void ReadDataOnNetworkThread( |
+ scoped_refptr<IOBufferWithByteBuffer> read_buffer, |
+ int buffer_size); |
+ void WriteDataOnNetworkThread( |
+ scoped_refptr<IOBufferWithByteBuffer> read_buffer, |
+ int buffer_size, |
+ bool end_of_stream); |
+ void DestroyOnNetworkThread(bool send_on_canceled); |
+ |
+ CronetURLRequestContextAdapter* const context_; |
+ |
+ // Java object that owns this CronetBidirectionalStreamAdapter. |
+ base::android::ScopedJavaGlobalRef<jobject> owner_; |
+ |
+ scoped_refptr<IOBufferWithByteBuffer> read_buffer_; |
+ scoped_refptr<IOBufferWithByteBuffer> write_buffer_; |
+ scoped_ptr<net::BidirectionalStream> bidi_stream_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(CronetBidirectionalStreamAdapter); |
+}; |
+ |
+} // namespace cronet |
+ |
+#endif // COMPONENTS_CRONET_ANDROID_CRONET_BIDIRECTIONAL_STREAM_ADAPTER_H_ |