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

Side by Side Diff: components/cronet/android/cronet_bidirectional_stream_adapter.h

Issue 1412243012: Initial implementation of CronetBidirectionalStream. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Destroy the native adapter instead of cancel if can't post task to executor. Created 4 years, 11 months 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 unified diff | Download patch
OLDNEW
(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;
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
28
29 namespace cronet {
30
31 class CronetURLRequestContextAdapter;
32
33 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.
34
35 // An adapter from Java BidirectionalStream object to net::BidirectionalStream.
36 // 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.
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
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
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.
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
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 // net::BidirectionalStream::Delegate implementations:
88 void OnHeadersSent() override;
89 void OnHeadersReceived(const net::SpdyHeaderBlock& response_headers) override;
90 void OnDataRead(int bytes_read) override;
91 void OnDataSent() override;
92 void OnTrailersReceived(const net::SpdyHeaderBlock& trailers) override;
93 void OnFailed(int error) override;
94
95 private:
96 class IOBufferWithByteBuffer;
97
98 void StartOnNetworkThread(
99 scoped_ptr<net::BidirectionalStreamRequestInfo> request_info);
100 // Gets headers as Java array.
101 base::android::ScopedJavaLocalRef<jobjectArray> GetHeadersArray(
102 JNIEnv* env,
103 const net::SpdyHeaderBlock& header_block);
104 void ReadDataOnNetworkThread(
105 scoped_refptr<IOBufferWithByteBuffer> read_buffer,
106 int buffer_size);
107 void WriteDataOnNetworkThread(
108 scoped_refptr<IOBufferWithByteBuffer> read_buffer,
109 int buffer_size,
110 bool end_of_stream);
111 void DestroyOnNetworkThread(bool send_on_canceled);
112
113 CronetURLRequestContextAdapter* const context_;
114
115 // Java object that owns this CronetBidirectionalStreamAdapter.
116 base::android::ScopedJavaGlobalRef<jobject> owner_;
117
118 scoped_refptr<IOBufferWithByteBuffer> read_buffer_;
119 scoped_refptr<IOBufferWithByteBuffer> write_buffer_;
120 scoped_ptr<net::BidirectionalStream> bidi_stream_;
121
122 DISALLOW_COPY_AND_ASSIGN(CronetBidirectionalStreamAdapter);
123 };
124
125 } // namespace cronet
126
127 #endif // COMPONENTS_CRONET_ANDROID_CRONET_BIDIRECTIONAL_STREAM_ADAPTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698