Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(849)

Unified Diff: components/cronet/android/cronet_bidirectional_stream.h

Issue 1412243012: Initial implementation of CronetBidirectionalStream. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/cronet/android/cronet_bidirectional_stream.h
diff --git a/components/cronet/android/cronet_bidirectional_stream.h b/components/cronet/android/cronet_bidirectional_stream.h
new file mode 100644
index 0000000000000000000000000000000000000000..2f6a592fcc7d976b53967ab6caec07be10dd8c07
--- /dev/null
+++ b/components/cronet/android/cronet_bidirectional_stream.h
@@ -0,0 +1,137 @@
+// 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_H_
+#define COMPONENTS_CRONET_ANDROID_CRONET_BIDIRECTIONAL_STREAM_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/callback.h"
xunjieli 2015/12/18 19:11:11 nit: not used?
mef 2015/12/29 20:36:54 Done.
+#include "base/location.h"
xunjieli 2015/12/18 19:11:11 nit: not used?
mef 2015/12/29 20:36:54 Done.
+#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
+
+namespace cronet {
+
+class CronetURLRequestContextAdapter;
+
+bool CronetBidirectionalStreamRegisterJni(JNIEnv* env);
+
+// An adapter from Java BidirectionalStream object to net::BidirectionalStream.
+// Created and configured from a Java thread. Start, ReadData, and Destroy are
+// 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
+// thread.
+class CronetBidirectionalStream : public net::BidirectionalStream::Delegate {
xunjieli 2015/12/18 19:49:25 I think this should be named CronetBidirectionalSt
mef 2015/12/29 20:36:54 Acknowledged. Sounds like a good idea, will do in
+ public:
+ CronetBidirectionalStream(CronetURLRequestContextAdapter* context,
+ JNIEnv* env,
+ jobject jbidi_stream);
+ ~CronetBidirectionalStream() override;
+
+ // Starts the request.
+ jint Start(JNIEnv* env,
+ jobject jcaller,
+ jstring jurl,
+ jstring jmethod,
+ jobjectArray jheaders,
+ jboolean jend_of_stream);
+
+ // Reads more data.
+ jboolean ReadData(JNIEnv* env,
+ jobject jcaller,
+ jobject jbyte_buffer,
+ jint jposition,
+ jint jcapacity);
+
+ // Writes more data.
+ jboolean WriteData(JNIEnv* env,
+ jobject jcaller,
+ jobject jbyte_buffer,
+ jint jposition,
+ jint jcapacity,
+ 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 when no more callbacks will be issued.
xunjieli 2015/12/18 19:49:25 nit: s/indicate when/indicate that
mef 2015/12/29 20:36:54 Done.
+ void Destroy(JNIEnv* env, jobject jcaller, jboolean jsend_on_canceled);
+
+ // Gets NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo.
+ // Can only be called on the network thread.
+ base::android::ScopedJavaLocalRef<jstring> GetNegotiatedProtocol(
+ JNIEnv* env,
+ jobject jcaller) const;
+
+ // net::BidirectionalStream::Delegate implementations:
+
+ // Called when the request headers have been sent.
xunjieli 2015/12/18 19:49:25 nit: override methods do not need documentation. T
mef 2015/12/29 20:36:54 Done.
+ void OnHeadersSent() override;
+
+ // Called when response headers are received.
+ void OnHeadersReceived(const net::SpdyHeaderBlock& response_headers) override;
+
+ // Called when read is completed asynchronously. |bytes_read| specifies how
+ // much data is available.
+ void OnDataRead(int bytes_read) override;
+
+ // Called when the entire buffer passed through SendData is sent.
+ void OnDataSent() override;
+
+ // Called when trailers are received.
+ void OnTrailersReceived(const net::SpdyHeaderBlock& trailers) override;
+
+ // Called when the stream is closed with error. No other delegate functions
+ // will be called after this.
+ 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* context_;
+
+ // Java object that owns this CronetBidirectionalStream.
+ 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(CronetBidirectionalStream);
+};
+
+} // namespace cronet
+
+#endif // COMPONENTS_CRONET_ANDROID_CRONET_BIDIRECTIONAL_STREAM_H_

Powered by Google App Engine
This is Rietveld 408576698