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

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: Self review. 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"
xunjieli 2016/01/22 14:52:22 not used?
mef 2016/01/22 17:36:06 Done.
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 namespace cronet {
28
29 class CronetURLRequestContextAdapter;
30
31 // An adapter from Java BidirectionalStream object to net::BidirectionalStream.
32 // Created and configured from a Java thread. Start, ReadData, WriteData and
33 // Destroy can be called on any thread (including network thread), and post
34 // calls to corresponding {Start|ReadData|WriteData|Destroy}OnNetworkThread to
35 // the network thread. All callbacks into the Java BidirectionalStream are done
36 // on the network thread. Java BidirectionalStream is expected to initiate the
37 // next step like ReadData or Destroy. Public methods can be called on any
38 // thread.
39 class CronetBidirectionalStreamAdapter
40 : public net::BidirectionalStream::Delegate {
41 public:
42 static bool RegisterJni(JNIEnv* env);
43
44 CronetBidirectionalStreamAdapter(
45 CronetURLRequestContextAdapter* context,
46 JNIEnv* env,
47 const base::android::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 base::android::JavaParamRef<jobject>& jcaller,
59 const base::android::JavaParamRef<jstring>& jurl,
60 jint jpriority,
61 const base::android::JavaParamRef<jstring>& jmethod,
62 const base::android::JavaParamRef<jobjectArray>& jheaders,
63 jboolean jend_of_stream);
64
65 // Reads more data into |jbyte_buffer| starting at |jposition| and not
66 // exceeding |jlimit|. Arguments are preserved to ensure that |jbyte_buffer|
67 // is not modified by the application during read.
68 jboolean ReadData(JNIEnv* env,
69 const base::android::JavaParamRef<jobject>& jcaller,
70 const base::android::JavaParamRef<jobject>& jbyte_buffer,
71 jint jposition,
72 jint jlimit);
73
74 // Writes more data from |jbyte_buffer| starting at |jposition| and ending at
75 // |jlimit|-1. Arguments are preserved to ensure that |jbyte_buffer|
76 // is not modified by the application during write. The |jend_of_stream| is
77 // passed to remote to indicate end of stream.
78 jboolean WriteData(JNIEnv* env,
79 const base::android::JavaParamRef<jobject>& jcaller,
80 const base::android::JavaParamRef<jobject>& jbyte_buffer,
81 jint jposition,
82 jint jlimit,
83 jboolean jend_of_stream);
84
85 // Releases all resources for the request and deletes the object itself.
86 // |jsend_on_canceled| indicates if Java onCanceled callback should be
87 // issued to indicate that no more callbacks will be issued.
88 void Destroy(JNIEnv* env,
89 const base::android::JavaParamRef<jobject>& jcaller,
90 jboolean jsend_on_canceled);
91
92 private:
93 class IOBufferWithByteBuffer;
94
95 // net::BidirectionalStream::Delegate implementations:
96 void OnHeadersSent() override;
97 void OnHeadersReceived(const net::SpdyHeaderBlock& response_headers) override;
98 void OnDataRead(int bytes_read) override;
99 void OnDataSent() override;
100 void OnTrailersReceived(const net::SpdyHeaderBlock& trailers) override;
101 void OnFailed(int error) override;
102
103 void StartOnNetworkThread(
104 scoped_ptr<net::BidirectionalStreamRequestInfo> request_info);
105 void ReadDataOnNetworkThread(
106 scoped_refptr<IOBufferWithByteBuffer> read_buffer,
107 int buffer_size);
108 void WriteDataOnNetworkThread(
109 scoped_refptr<IOBufferWithByteBuffer> read_buffer,
110 int buffer_size,
111 bool end_of_stream);
112 void DestroyOnNetworkThread(bool send_on_canceled);
113 // Gets headers as Java array.
114 base::android::ScopedJavaLocalRef<jobjectArray> GetHeadersArray(
115 JNIEnv* env,
116 const net::SpdyHeaderBlock& header_block);
117
118 CronetURLRequestContextAdapter* const context_;
119
120 // Java object that owns this CronetBidirectionalStreamAdapter.
121 base::android::ScopedJavaGlobalRef<jobject> owner_;
122
123 scoped_refptr<IOBufferWithByteBuffer> read_buffer_;
124 scoped_refptr<IOBufferWithByteBuffer> write_buffer_;
125 scoped_ptr<net::BidirectionalStream> bidi_stream_;
126
127 DISALLOW_COPY_AND_ASSIGN(CronetBidirectionalStreamAdapter);
128 };
129
130 } // namespace cronet
131
132 #endif // COMPONENTS_CRONET_ANDROID_CRONET_BIDIRECTIONAL_STREAM_ADAPTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698