Chromium Code Reviews| 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_ADAPTER_H_ | |
| 6 #define COMPONENTS_CRONET_ANDROID_CRONET_BIDIRECTIONAL_STREAM_ADAPTER_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/macros.h" | |
| 16 #include "base/memory/ref_counted.h" | |
| 17 #include "base/memory/scoped_ptr.h" | |
| 18 #include "net/base/request_priority.h" | |
| 19 #include "net/http/bidirectional_stream.h" | |
| 20 #include "url/gurl.h" | |
| 21 | |
| 22 namespace net { | |
| 23 class BidirectionalStreamRequestInfo; | |
| 24 class SpdyHeaderBlock; | |
| 25 } // namespace net | |
| 26 | |
| 27 using base::android::JavaParamRef; | |
| 28 | |
| 29 namespace cronet { | |
| 30 | |
| 31 class CronetURLRequestContextAdapter; | |
| 32 | |
| 33 bool CronetBidirectionalStreamAdapterRegisterJni(JNIEnv* env); | |
| 34 | |
| 35 // An adapter from Java BidirectionalStream object to net::BidirectionalStream. | |
| 36 // Created and configured from a Java thread. Start, ReadData, and Destroy are | |
| 37 // posted to network thread and all callbacks into the Java BidirectionalStream | |
| 38 // are done on the network thread. Java BidirectionalStream is expected to | |
| 39 // initiate the next step like ReadData or Destroy. Public methods can be called | |
| 40 // on any thread except Get* methods, which can only be called on the network | |
| 41 // thread. | |
| 42 class CronetBidirectionalStreamAdapter | |
| 43 : public net::BidirectionalStream::Delegate { | |
| 44 public: | |
| 45 CronetBidirectionalStreamAdapter(CronetURLRequestContextAdapter* context, | |
| 46 JNIEnv* env, | |
| 47 const JavaParamRef<jobject>& jbidi_stream); | |
| 48 ~CronetBidirectionalStreamAdapter() override; | |
| 49 | |
| 50 // Validates method and headers, initializes and starts the request. If | |
| 51 // |jend_of_stream| is true, then stream is half-closed after sending header | |
| 52 // frame and no data is expected to be written. | |
| 53 // Returns 0 if request is valid and started successfully, | |
| 54 // Returns -1 if |jmethod| is not valid HTTP method name. | |
| 55 // Returns position of invalid header value in |jheaders| if header name is | |
| 56 // not valid. | |
| 57 jint Start(JNIEnv* env, | |
| 58 const JavaParamRef<jobject>& jcaller, | |
| 59 const JavaParamRef<jstring>& jurl, | |
| 60 jint jpriority, | |
| 61 const JavaParamRef<jstring>& jmethod, | |
| 62 const JavaParamRef<jobjectArray>& jheaders, | |
| 63 jboolean jend_of_stream); | |
| 64 | |
| 65 // Reads more data. | |
| 66 jboolean ReadData(JNIEnv* env, | |
| 67 const JavaParamRef<jobject>& jcaller, | |
| 68 const JavaParamRef<jobject>& jbyte_buffer, | |
| 69 jint jposition, | |
| 70 jint jlimit); | |
| 71 | |
| 72 // Writes more data. | |
| 73 jboolean WriteData(JNIEnv* env, | |
| 74 const JavaParamRef<jobject>& jcaller, | |
| 75 const JavaParamRef<jobject>& jbyte_buffer, | |
| 76 jint jposition, | |
| 77 jint jlimit, | |
| 78 jboolean jend_of_stream); | |
| 79 | |
| 80 // Releases all resources for the request and deletes the object itself. | |
| 81 // |jsend_on_canceled| indicates if Java onCanceled callback should be | |
| 82 // issued to indicate that no more callbacks will be issued. | |
| 83 void Destroy(JNIEnv* env, | |
| 84 const JavaParamRef<jobject>& jcaller, | |
| 85 jboolean jsend_on_canceled); | |
| 86 | |
| 87 // Gets NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo. | |
| 88 // Can only be called on the network thread. | |
| 89 base::android::ScopedJavaLocalRef<jstring> GetNegotiatedProtocol( | |
| 90 JNIEnv* env, | |
| 91 const JavaParamRef<jobject>& jcaller) const; | |
| 92 | |
| 93 // net::BidirectionalStream::Delegate implementations: | |
| 94 void OnHeadersSent() override; | |
| 95 void OnHeadersReceived(const net::SpdyHeaderBlock& response_headers) override; | |
| 96 void OnDataRead(int bytes_read) override; | |
| 97 void OnDataSent() override; | |
| 98 void OnTrailersReceived(const net::SpdyHeaderBlock& trailers) override; | |
| 99 void OnFailed(int error) override; | |
| 100 | |
| 101 private: | |
| 102 class IOBufferWithByteBuffer; | |
| 103 | |
| 104 void StartOnNetworkThread( | |
| 105 scoped_ptr<net::BidirectionalStreamRequestInfo> request_info); | |
| 106 // Gets headers as Java array. | |
| 107 base::android::ScopedJavaLocalRef<jobjectArray> GetHeadersArray( | |
| 108 JNIEnv* env, | |
| 109 const net::SpdyHeaderBlock& header_block); | |
| 110 void ReadDataOnNetworkThread( | |
| 111 scoped_refptr<IOBufferWithByteBuffer> read_buffer, | |
| 112 int buffer_size); | |
| 113 void WriteDataOnNetworkThread( | |
| 114 scoped_refptr<IOBufferWithByteBuffer> read_buffer, | |
| 115 int buffer_size, | |
| 116 bool end_of_stream); | |
| 117 void DestroyOnNetworkThread(bool send_on_canceled); | |
| 118 | |
| 119 CronetURLRequestContextAdapter* context_; | |
|
pauljensen
2016/01/11 20:05:45
nit: CronetURLRequestContextAdapter* const context
mef
2016/01/11 23:22:50
Done.
| |
| 120 | |
| 121 // Java object that owns this CronetBidirectionalStreamAdapter. | |
| 122 base::android::ScopedJavaGlobalRef<jobject> owner_; | |
| 123 | |
| 124 scoped_refptr<IOBufferWithByteBuffer> read_buffer_; | |
| 125 scoped_refptr<IOBufferWithByteBuffer> write_buffer_; | |
| 126 scoped_ptr<net::BidirectionalStream> bidi_stream_; | |
| 127 | |
| 128 DISALLOW_COPY_AND_ASSIGN(CronetBidirectionalStreamAdapter); | |
| 129 }; | |
| 130 | |
| 131 } // namespace cronet | |
| 132 | |
| 133 #endif // COMPONENTS_CRONET_ANDROID_CRONET_BIDIRECTIONAL_STREAM_ADAPTER_H_ | |
| OLD | NEW |