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

Side by Side Diff: components/cronet/ios/cronet_bidirectional_stream.h

Issue 2273403003: Moving gRPC support interfaces out of cronet and into a new component. (Closed)
Patch Set: Add cronet_c_for_graph back to sources. Might fix GN, probably won't compile Created 4 years, 3 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 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_CRONET_IOS_CRONET_BIDIRECTIONAL_STREAM_H_
6 #define COMPONENTS_CRONET_IOS_CRONET_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/synchronization/lock.h"
14 #include "net/http/bidirectional_stream.h"
15
16 namespace net {
17 class HttpRequestHeaders;
18 class WrappedIOBuffer;
19 } // namespace net
20
21 namespace cronet {
22
23 class CronetEnvironment;
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 CronetBidirectionalStream : 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 CronetBidirectionalStream(CronetEnvironment* environment, Delegate* delegate);
56 ~CronetBidirectionalStream() override;
57
58 // Disables automatic flushing of each buffer passed to WriteData().
59 void disable_auto_flush(bool disable_auto_flush) {
60 disable_auto_flush_ = disable_auto_flush;
61 }
62
63 // Delays sending request headers until first call to Flush().
64 void delay_headers_until_flush(bool delay_headers_until_flush) {
65 delay_headers_until_flush_ = delay_headers_until_flush;
66 }
67
68 // Validates method and headers, initializes and starts the request. If
69 // |end_of_stream| is true, then stream is half-closed after sending header
70 // frame and no data is expected to be written.
71 // Returns 0 if request is valid and started successfully,
72 // Returns -1 if |method| is not valid HTTP method name.
73 // Returns position of invalid header value in |headers| if header name is
74 // not valid.
75 int Start(const char* url,
76 int priority,
77 const char* method,
78 const net::HttpRequestHeaders& headers,
79 bool end_of_stream);
80
81 // Reads more data into |buffer| up to |capacity| bytes.
82 bool ReadData(char* buffer, int capacity);
83
84 // Writes |count| bytes of data from |buffer|. The |end_of_stream| is
85 // passed to remote to indicate end of stream.
86 bool WriteData(const char* buffer, int count, bool end_of_stream);
87
88 // Sends buffers passed to WriteData().
89 void Flush();
90
91 // Cancels the request. The OnCanceled callback is invoked when request is
92 // caneceled, and not other callbacks are invoked afterwards..
93 void Cancel();
94
95 // Releases all resources for the request and deletes the object itself.
96 void Destroy();
97
98 private:
99 // States of BidirectionalStream are tracked in |read_state_| and
100 // |write_state_|.
101 // The write state is separated as it changes independently of the read state.
102 // There is one initial state: NOT_STARTED. There is one normal final state:
103 // SUCCESS, reached after READING_DONE and WRITING_DONE. There are two
104 // exceptional final states: CANCELED and ERROR, which can be reached from
105 // any other non-final state.
106 enum State {
107 // Initial state, stream not started.
108 NOT_STARTED,
109 // Stream started, request headers are being sent.
110 STARTED,
111 // Waiting for ReadData() to be called.
112 WAITING_FOR_READ,
113 // Reading from the remote, OnDataRead callback will be invoked when done.
114 READING,
115 // There is no more data to read and stream is half-closed by the remote
116 // side.
117 READING_DONE,
118 // Stream is canceled.
119 CANCELED,
120 // Error has occured, stream is closed.
121 ERROR,
122 // Reading and writing are done, and the stream is closed successfully.
123 SUCCESS,
124 // Waiting for Flush() to be called.
125 WAITING_FOR_FLUSH,
126 // Writing to the remote, callback will be invoked when done.
127 WRITING,
128 // There is no more data to write and stream is half-closed by the local
129 // side.
130 WRITING_DONE,
131 };
132
133 // Container to hold buffers and sizes of the pending data to be written.
134 class WriteBuffers {
135 public:
136 WriteBuffers();
137 ~WriteBuffers();
138
139 // Clears Write Buffers list.
140 void Clear();
141
142 // Appends |buffer| of |buffer_size| length to the end of buffer list.
143 void AppendBuffer(const scoped_refptr<net::IOBuffer>& buffer,
144 int buffer_size);
145
146 void MoveTo(WriteBuffers* target);
147
148 // Returns true of Write Buffers list is empty.
149 bool Empty() const;
150
151 const std::vector<scoped_refptr<net::IOBuffer>>& buffers() const {
152 return write_buffer_list;
153 }
154
155 const std::vector<int>& lengths() const { return write_buffer_len_list; }
156
157 private:
158 // Every IOBuffer in |write_buffer_list| points to the memory owned by the
159 // application.
160 std::vector<scoped_refptr<net::IOBuffer>> write_buffer_list;
161 // A list of the length of each IOBuffer in |write_buffer_list|.
162 std::vector<int> write_buffer_len_list;
163
164 DISALLOW_COPY_AND_ASSIGN(WriteBuffers);
165 };
166
167 // net::BidirectionalStream::Delegate implementations:
168 void OnStreamReady(bool request_headers_sent) override;
169 void OnHeadersReceived(const net::SpdyHeaderBlock& response_headers) override;
170 void OnDataRead(int bytes_read) override;
171 void OnDataSent() override;
172 void OnTrailersReceived(const net::SpdyHeaderBlock& trailers) override;
173 void OnFailed(int error) override;
174 // Helper method to derive OnSucceeded.
175 void MaybeOnSucceded();
176
177 void StartOnNetworkThread(
178 std::unique_ptr<net::BidirectionalStreamRequestInfo> request_info);
179 void ReadDataOnNetworkThread(scoped_refptr<net::WrappedIOBuffer> read_buffer,
180 int buffer_size);
181 void WriteDataOnNetworkThread(scoped_refptr<net::WrappedIOBuffer> read_buffer,
182 int buffer_size,
183 bool end_of_stream);
184 void FlushOnNetworkThread();
185 void SendFlushingWriteData();
186 void CancelOnNetworkThread();
187 void DestroyOnNetworkThread();
188
189 // Read state is tracking reading flow. Only accessed on network thread.
190 // / <--- READING <--- \
191 // | |
192 // \ /
193 // NOT_STARTED -> STARTED --> WAITING_FOR_READ -> READING_DONE -> SUCCESS
194 State read_state_;
195
196 // Write state is tracking writing flow. Only accessed on network thread.
197 // / <--- WRITING <--- \
198 // | |
199 // \ /
200 // NOT_STARTED -> STARTED --> WAITING_FOR_FLUSH -> WRITING_DONE -> SUCCESS
201 State write_state_;
202
203 bool write_end_of_stream_;
204 bool request_headers_sent_;
205
206 bool disable_auto_flush_;
207 bool delay_headers_until_flush_;
208
209 CronetEnvironment* const environment_;
210
211 scoped_refptr<net::WrappedIOBuffer> read_buffer_;
212
213 // Write data that is pending the flush.
214 std::unique_ptr<WriteBuffers> pending_write_data_;
215 // Write data that is flushed, but not sending yet.
216 std::unique_ptr<WriteBuffers> flushing_write_data_;
217 // Write data that is sending.
218 std::unique_ptr<WriteBuffers> sending_write_data_;
219
220 std::unique_ptr<net::BidirectionalStream> bidi_stream_;
221 Delegate* delegate_;
222
223 DISALLOW_COPY_AND_ASSIGN(CronetBidirectionalStream);
224 };
225
226 } // namespace cronet
227
228 #endif // COMPONENTS_CRONET_IOS_CRONET_BIDIRECTIONAL_STREAM_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698