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

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

Issue 1858483002: Cronet for iOS with C API for GRPC support. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@small
Patch Set: Address comments, bundle libboringssl.a into libcronet.a Created 4 years, 8 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
« no previous file with comments | « components/cronet/ios/Cronet.mm ('k') | components/cronet/ios/cronet_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_CRONET_IOS_CRONET_BIDIRECTIONAL_STREAM_H_
6 #define COMPONENTS_CRONET_IOS_CRONET_BIDIRECTIONAL_STREAM_H_
7
8 #include "base/macros.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/synchronization/lock.h"
12 #include "net/http/bidirectional_stream.h"
13
14 namespace net {
15 class HttpRequestHeaders;
16 class WrappedIOBuffer;
17 } // namespace net
18
19 namespace cronet {
20
21 class CronetEnvironment;
22
23 // An adapter to net::BidirectionalStream.
24 // Created and configured from any thread. Start, ReadData, WriteData and
25 // Destroy can be called on any thread (including network thread), and post
26 // calls to corresponding {Start|ReadData|WriteData|Destroy}OnNetworkThread to
27 // the network thread. The object is always deleted on network thread. All
28 // callbacks into the Delegate are done on the network thread.
29 // The app is expected to initiate the next step like ReadData or Destroy.
30 // Public methods can be called on any thread.
31 class CronetBidirectionalStream : public net::BidirectionalStream::Delegate {
32 public:
33 class Delegate {
34 public:
35 virtual void OnHeadersSent() = 0;
36
37 virtual void OnHeadersReceived(const net::SpdyHeaderBlock& response_headers,
38 const char* negotiated_protocol) = 0;
39
40 virtual void OnDataRead(char* data, int size) = 0;
41
42 virtual void OnDataSent(const char* data) = 0;
43
44 virtual void OnTrailersReceived(const net::SpdyHeaderBlock& trailers) = 0;
45
46 virtual void OnSucceeded() = 0;
47
48 virtual void OnFailed(int error) = 0;
49
50 virtual void OnCanceled() = 0;
51 };
52
53 CronetBidirectionalStream(CronetEnvironment* environment, Delegate* delegate);
54 ~CronetBidirectionalStream() override;
55
56 // Validates method and headers, initializes and starts the request. If
57 // |end_of_stream| is true, then stream is half-closed after sending header
58 // frame and no data is expected to be written.
59 // Returns 0 if request is valid and started successfully,
60 // Returns -1 if |method| is not valid HTTP method name.
61 // Returns position of invalid header value in |headers| if header name is
62 // not valid.
63 int Start(const char* url,
64 int priority,
65 const char* method,
66 const net::HttpRequestHeaders& headers,
67 bool end_of_stream);
68
69 // Reads more data into |buffer| up to |capacity| bytes.
70 bool ReadData(char* buffer, int capacity);
71
72 // Writes |count| bytes of data from |buffer|. The |end_of_stream| is
73 // passed to remote to indicate end of stream.
74 bool WriteData(const char* buffer, int count, bool end_of_stream);
75
76 // Cancels the request. The OnCanceled callback is invoked when request is
77 // caneceled, and not other callbacks are invoked afterwards..
78 void Cancel();
79
80 // Releases all resources for the request and deletes the object itself.
81 void Destroy();
82
83 private:
84 // States of BidirectionalStream are tracked in |read_state_| and
85 // |write_state_|.
86 // The write state is separated as it changes independently of the read state.
87 // There is one initial state: NOT_STARTED. There is one normal final state:
88 // SUCCESS, reached after READING_DONE and WRITING_DONE. There are two
89 // exceptional final states: CANCELED and ERROR, which can be reached from
90 // any other non-final state.
91 enum State {
92 // Initial state, stream not started.
93 NOT_STARTED,
94 // Stream started, request headers are being sent.
95 STARTED,
96 // Waiting for ReadData() to be called.
97 WAITING_FOR_READ,
98 // Reading from the remote, OnDataRead callback will be invoked when done.
99 READING,
100 // There is no more data to read and stream is half-closed by the remote
101 // side.
102 READING_DONE,
103 // Stream is canceled.
104 CANCELED,
105 // Error has occured, stream is closed.
106 ERROR,
107 // Reading and writing are done, and the stream is closed successfully.
108 SUCCESS,
109 // Waiting for WriteData() to be called.
110 WAITING_FOR_WRITE,
111 // Writing to the remote, callback will be invoked when done.
112 WRITING,
113 // There is no more data to write and stream is half-closed by the local
114 // side.
115 WRITING_DONE,
116 };
117
118 // net::BidirectionalStream::Delegate implementations:
119 void OnHeadersSent() override;
120 void OnHeadersReceived(const net::SpdyHeaderBlock& response_headers) override;
121 void OnDataRead(int bytes_read) override;
122 void OnDataSent() override;
123 void OnTrailersReceived(const net::SpdyHeaderBlock& trailers) override;
124 void OnFailed(int error) override;
125 // Helper method to derive OnSucceeded.
126 void MaybeOnSucceded();
127
128 void StartOnNetworkThread(
129 scoped_ptr<net::BidirectionalStreamRequestInfo> request_info);
130 void ReadDataOnNetworkThread(scoped_refptr<net::WrappedIOBuffer> read_buffer,
131 int buffer_size);
132 void WriteDataOnNetworkThread(scoped_refptr<net::WrappedIOBuffer> read_buffer,
133 int buffer_size,
134 bool end_of_stream);
135 void CancelOnNetworkThread();
136 void DestroyOnNetworkThread();
137
138 // Read state is tracking reading flow. Only accessed on network thread.
139 // / <--- READING <--- \
140 // | |
141 // \ /
142 // NOT_STARTED -> STARTED --> WAITING_FOR_READ -> READING_DONE -> SUCCESS
143 State read_state_;
144
145 // Write state is tracking writing flow. Only accessed on network thread.
146 // / <--- WRITING <--- \
147 // | |
148 // \ /
149 // NOT_STARTED -> STARTED --> WAITING_FOR_WRITE -> WRITING_DONE -> SUCCESS
150 State write_state_;
151
152 bool write_end_of_stream_;
153
154 CronetEnvironment* const environment_;
155
156 scoped_refptr<net::WrappedIOBuffer> read_buffer_;
157 scoped_refptr<net::WrappedIOBuffer> write_buffer_;
158 scoped_ptr<net::BidirectionalStream> bidi_stream_;
159 Delegate* delegate_;
160
161 DISALLOW_COPY_AND_ASSIGN(CronetBidirectionalStream);
162 };
163
164 } // namespace cronet
165
166 #endif // COMPONENTS_CRONET_IOS_CRONET_BIDIRECTIONAL_STREAM_H_
OLDNEW
« no previous file with comments | « components/cronet/ios/Cronet.mm ('k') | components/cronet/ios/cronet_bidirectional_stream.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698