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

Side by Side Diff: components/grpc_support/bidirectional_stream.h

Issue 2487863003: Revert of Revert "Revert of Moving gRPC support interfaces out of cronet and into a new component. (patchset … (Closed)
Patch Set: Created 4 years, 1 month 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
« no previous file with comments | « components/grpc_support/README.md ('k') | components/grpc_support/bidirectional_stream.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 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_GRPC_SUPPORT_BIDIRECTIONAL_STREAM_H_
6 #define COMPONENTS_GRPC_SUPPORT_BIDIRECTIONAL_STREAM_H_
7
8 #include <memory>
9 #include <vector>
10
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/synchronization/lock.h"
15 #include "net/http/bidirectional_stream.h"
16 #include "net/url_request/url_request_context_getter.h"
17
18 namespace net {
19 class HttpRequestHeaders;
20 class WrappedIOBuffer;
21 } // namespace net
22
23 namespace grpc_support {
24
25 // An adapter to net::BidirectionalStream.
26 // Created and configured from any thread. Start, ReadData, WriteData and
27 // Destroy can be called on any thread (including network thread), and post
28 // calls to corresponding {Start|ReadData|WriteData|Destroy}OnNetworkThread to
29 // the network thread. The object is always deleted on network thread. All
30 // callbacks into the Delegate are done on the network thread.
31 // The app is expected to initiate the next step like ReadData or Destroy.
32 // Public methods can be called on any thread.
33 class BidirectionalStream : public net::BidirectionalStream::Delegate {
34 public:
35 class Delegate {
36 public:
37 virtual void OnStreamReady() = 0;
38
39 virtual void OnHeadersReceived(const net::SpdyHeaderBlock& response_headers,
40 const char* negotiated_protocol) = 0;
41
42 virtual void OnDataRead(char* data, int size) = 0;
43
44 virtual void OnDataSent(const char* data) = 0;
45
46 virtual void OnTrailersReceived(const net::SpdyHeaderBlock& trailers) = 0;
47
48 virtual void OnSucceeded() = 0;
49
50 virtual void OnFailed(int error) = 0;
51
52 virtual void OnCanceled() = 0;
53 };
54
55 BidirectionalStream(net::URLRequestContextGetter* request_context_getter,
56 Delegate* delegate);
57 ~BidirectionalStream() override;
58
59 // Disables automatic flushing of each buffer passed to WriteData().
60 void disable_auto_flush(bool disable_auto_flush) {
61 disable_auto_flush_ = disable_auto_flush;
62 }
63
64 // Delays sending request headers until first call to Flush().
65 void delay_headers_until_flush(bool delay_headers_until_flush) {
66 delay_headers_until_flush_ = delay_headers_until_flush;
67 }
68
69 // Validates method and headers, initializes and starts the request. If
70 // |end_of_stream| is true, then stream is half-closed after sending header
71 // frame and no data is expected to be written.
72 // Returns 0 if request is valid and started successfully,
73 // Returns -1 if |method| is not valid HTTP method name.
74 // Returns position of invalid header value in |headers| if header name is
75 // not valid.
76 int Start(const char* url,
77 int priority,
78 const char* method,
79 const net::HttpRequestHeaders& headers,
80 bool end_of_stream);
81
82 // Reads more data into |buffer| up to |capacity| bytes.
83 bool ReadData(char* buffer, int capacity);
84
85 // Writes |count| bytes of data from |buffer|. The |end_of_stream| is
86 // passed to remote to indicate end of stream.
87 bool WriteData(const char* buffer, int count, bool end_of_stream);
88
89 // Sends buffers passed to WriteData().
90 void Flush();
91
92 // Cancels the request. The OnCanceled callback is invoked when request is
93 // caneceled, and not other callbacks are invoked afterwards..
94 void Cancel();
95
96 // Releases all resources for the request and deletes the object itself.
97 void Destroy();
98
99 private:
100 // States of BidirectionalStream are tracked in |read_state_| and
101 // |write_state_|.
102 // The write state is separated as it changes independently of the read state.
103 // There is one initial state: NOT_STARTED. There is one normal final state:
104 // SUCCESS, reached after READING_DONE and WRITING_DONE. There are two
105 // exceptional final states: CANCELED and ERROR, which can be reached from
106 // any other non-final state.
107 enum State {
108 // Initial state, stream not started.
109 NOT_STARTED,
110 // Stream started, request headers are being sent.
111 STARTED,
112 // Waiting for ReadData() to be called.
113 WAITING_FOR_READ,
114 // Reading from the remote, OnDataRead callback will be invoked when done.
115 READING,
116 // There is no more data to read and stream is half-closed by the remote
117 // side.
118 READING_DONE,
119 // Stream is canceled.
120 CANCELED,
121 // Error has occured, stream is closed.
122 ERR,
123 // Reading and writing are done, and the stream is closed successfully.
124 SUCCESS,
125 // Waiting for Flush() to be called.
126 WAITING_FOR_FLUSH,
127 // Writing to the remote, callback will be invoked when done.
128 WRITING,
129 // There is no more data to write and stream is half-closed by the local
130 // side.
131 WRITING_DONE,
132 };
133
134 // Container to hold buffers and sizes of the pending data to be written.
135 class WriteBuffers {
136 public:
137 WriteBuffers();
138 ~WriteBuffers();
139
140 // Clears Write Buffers list.
141 void Clear();
142
143 // Appends |buffer| of |buffer_size| length to the end of buffer list.
144 void AppendBuffer(const scoped_refptr<net::IOBuffer>& buffer,
145 int buffer_size);
146
147 void MoveTo(WriteBuffers* target);
148
149 // Returns true of Write Buffers list is empty.
150 bool Empty() const;
151
152 const std::vector<scoped_refptr<net::IOBuffer>>& buffers() const {
153 return write_buffer_list;
154 }
155
156 const std::vector<int>& lengths() const { return write_buffer_len_list; }
157
158 private:
159 // Every IOBuffer in |write_buffer_list| points to the memory owned by the
160 // application.
161 std::vector<scoped_refptr<net::IOBuffer>> write_buffer_list;
162 // A list of the length of each IOBuffer in |write_buffer_list|.
163 std::vector<int> write_buffer_len_list;
164
165 DISALLOW_COPY_AND_ASSIGN(WriteBuffers);
166 };
167
168 // net::BidirectionalStream::Delegate implementations:
169 void OnStreamReady(bool request_headers_sent) override;
170 void OnHeadersReceived(const net::SpdyHeaderBlock& response_headers) override;
171 void OnDataRead(int bytes_read) override;
172 void OnDataSent() override;
173 void OnTrailersReceived(const net::SpdyHeaderBlock& trailers) override;
174 void OnFailed(int error) override;
175 // Helper method to derive OnSucceeded.
176 void MaybeOnSucceded();
177
178 void StartOnNetworkThread(
179 std::unique_ptr<net::BidirectionalStreamRequestInfo> request_info);
180 void ReadDataOnNetworkThread(scoped_refptr<net::WrappedIOBuffer> read_buffer,
181 int buffer_size);
182 void WriteDataOnNetworkThread(scoped_refptr<net::WrappedIOBuffer> read_buffer,
183 int buffer_size,
184 bool end_of_stream);
185 void FlushOnNetworkThread();
186 void SendFlushingWriteData();
187 void CancelOnNetworkThread();
188 void DestroyOnNetworkThread();
189
190 bool IsOnNetworkThread();
191 void PostToNetworkThread(const tracked_objects::Location& from_here,
192 const base::Closure& task);
193
194 // Read state is tracking reading flow. Only accessed on network thread.
195 // | <--- READING <--- |
196 // | |
197 // | |
198 // NOT_STARTED -> STARTED --> WAITING_FOR_READ -> READING_DONE -> SUCCESS
199 State read_state_;
200
201 // Write state is tracking writing flow. Only accessed on network thread.
202 // | <--- WRITING <--- |
203 // | |
204 // | |
205 // NOT_STARTED -> STARTED --> WAITING_FOR_FLUSH -> WRITING_DONE -> SUCCESS
206 State write_state_;
207
208 bool write_end_of_stream_;
209 bool request_headers_sent_;
210
211 bool disable_auto_flush_;
212 bool delay_headers_until_flush_;
213
214 net::URLRequestContextGetter* const request_context_getter_;
215
216 scoped_refptr<net::WrappedIOBuffer> read_buffer_;
217
218 // Write data that is pending the flush.
219 std::unique_ptr<WriteBuffers> pending_write_data_;
220 // Write data that is flushed, but not sending yet.
221 std::unique_ptr<WriteBuffers> flushing_write_data_;
222 // Write data that is sending.
223 std::unique_ptr<WriteBuffers> sending_write_data_;
224
225 std::unique_ptr<net::BidirectionalStream> bidi_stream_;
226 Delegate* delegate_;
227
228 base::WeakPtr<BidirectionalStream> weak_this_;
229 base::WeakPtrFactory<BidirectionalStream> weak_factory_;
230
231 DISALLOW_COPY_AND_ASSIGN(BidirectionalStream);
232 };
233
234 } // namespace grpc_support
235
236 #endif // COMPONENTS_GRPC_SUPPORT_BIDIRECTIONAL_STREAM_H_
OLDNEW
« no previous file with comments | « components/grpc_support/README.md ('k') | components/grpc_support/bidirectional_stream.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698