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

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: Log the exception. Created 4 years, 10 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/http/bidirectional_stream.h"
19
20 namespace net {
21 class BidirectionalStreamRequestInfo;
22 class SpdyHeaderBlock;
23 } // namespace net
24
25 namespace cronet {
26
27 class CronetURLRequestContextAdapter;
28
29 // An adapter from Java BidirectionalStream object to net::BidirectionalStream.
30 // Created and configured from a Java thread. Start, ReadData, WriteData and
31 // Destroy can be called on any thread (including network thread), and post
32 // calls to corresponding {Start|ReadData|WriteData|Destroy}OnNetworkThread to
33 // the network thread. The object is always deleted on network thread. All
34 // callbacks into the Java BidirectionalStream are done on the network thread.
35 // Java BidirectionalStream is expected to initiate the next step like ReadData
36 // or Destroy. Public methods can be called on any thread.
37 class CronetBidirectionalStreamAdapter
38 : public net::BidirectionalStream::Delegate {
39 public:
40 static bool RegisterJni(JNIEnv* env);
41
42 CronetBidirectionalStreamAdapter(
43 CronetURLRequestContextAdapter* context,
44 JNIEnv* env,
45 const base::android::JavaParamRef<jobject>& jbidi_stream);
46 ~CronetBidirectionalStreamAdapter() override;
47
48 // Validates method and headers, initializes and starts the request. If
49 // |jend_of_stream| is true, then stream is half-closed after sending header
50 // frame and no data is expected to be written.
51 // Returns 0 if request is valid and started successfully,
52 // Returns -1 if |jmethod| is not valid HTTP method name.
53 // Returns position of invalid header value in |jheaders| if header name is
54 // not valid.
55 jint Start(JNIEnv* env,
56 const base::android::JavaParamRef<jobject>& jcaller,
57 const base::android::JavaParamRef<jstring>& jurl,
58 jint jpriority,
59 const base::android::JavaParamRef<jstring>& jmethod,
60 const base::android::JavaParamRef<jobjectArray>& jheaders,
61 jboolean jend_of_stream);
62
63 // Reads more data into |jbyte_buffer| starting at |jposition| and not
64 // exceeding |jlimit|. Arguments are preserved to ensure that |jbyte_buffer|
65 // is not modified by the application during read.
66 jboolean ReadData(JNIEnv* env,
67 const base::android::JavaParamRef<jobject>& jcaller,
68 const base::android::JavaParamRef<jobject>& jbyte_buffer,
69 jint jposition,
70 jint jlimit);
71
72 // Writes more data from |jbyte_buffer| starting at |jposition| and ending at
73 // |jlimit|-1. Arguments are preserved to ensure that |jbyte_buffer|
74 // is not modified by the application during write. The |jend_of_stream| is
75 // passed to remote to indicate end of stream.
76 jboolean WriteData(JNIEnv* env,
77 const base::android::JavaParamRef<jobject>& jcaller,
78 const base::android::JavaParamRef<jobject>& jbyte_buffer,
79 jint jposition,
80 jint jlimit,
81 jboolean jend_of_stream);
82
83 // Releases all resources for the request and deletes the object itself.
84 // |jsend_on_canceled| indicates if Java onCanceled callback should be
85 // issued to indicate that no more callbacks will be issued.
86 void Destroy(JNIEnv* env,
87 const base::android::JavaParamRef<jobject>& jcaller,
88 jboolean jsend_on_canceled);
89
90 private:
91 class IOBufferWithByteBuffer;
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 void StartOnNetworkThread(
102 scoped_ptr<net::BidirectionalStreamRequestInfo> request_info);
103 void ReadDataOnNetworkThread(
104 scoped_refptr<IOBufferWithByteBuffer> read_buffer,
105 int buffer_size);
106 void WriteDataOnNetworkThread(
107 scoped_refptr<IOBufferWithByteBuffer> read_buffer,
108 int buffer_size,
109 bool end_of_stream);
110 void DestroyOnNetworkThread(bool send_on_canceled);
111 // Gets headers as a Java array.
112 base::android::ScopedJavaLocalRef<jobjectArray> GetHeadersArray(
113 JNIEnv* env,
114 const net::SpdyHeaderBlock& header_block);
115
116 CronetURLRequestContextAdapter* const context_;
117
118 // Java object that owns this CronetBidirectionalStreamAdapter.
119 base::android::ScopedJavaGlobalRef<jobject> owner_;
120
121 scoped_refptr<IOBufferWithByteBuffer> read_buffer_;
122 scoped_refptr<IOBufferWithByteBuffer> write_buffer_;
123 scoped_ptr<net::BidirectionalStream> bidi_stream_;
124
125 DISALLOW_COPY_AND_ASSIGN(CronetBidirectionalStreamAdapter);
126 };
127
128 } // namespace cronet
129
130 #endif // COMPONENTS_CRONET_ANDROID_CRONET_BIDIRECTIONAL_STREAM_ADAPTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698