Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 } | |
| 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 /** | |
| 85 * States of BidirectionalStream are tracked in read_state_ and write_state_. | |
|
xunjieli
2016/04/08 14:43:15
nit: add pipes? |read_state_| and |write_state_|
mef
2016/04/08 16:43:03
Done.
| |
| 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 */ | |
|
xunjieli
2016/04/08 14:43:15
All the comments should start with double slash? i
mef
2016/04/08 16:43:03
Done.
| |
| 92 enum State { | |
| 93 /* Initial state, stream not started. */ | |
| 94 NOT_STARTED, | |
| 95 /* Stream started, request headers are being sent. */ | |
| 96 STARTED, | |
| 97 /* Waiting for ReadData() to be called. */ | |
| 98 WAITING_FOR_READ, | |
| 99 /* Reading from the remote, OnDataRead callback will be invoked when done. | |
| 100 */ | |
| 101 READING, | |
| 102 /* There is no more data to read and stream is half-closed by the remote | |
| 103 side. */ | |
| 104 READING_DONE, | |
| 105 /* Stream is canceled. */ | |
| 106 CANCELED, | |
| 107 /* Error has occured, stream is closed. */ | |
| 108 ERROR, | |
| 109 /* Reading and writing are done, and the stream is closed successfully. */ | |
| 110 SUCCESS, | |
| 111 /* Waiting for WriteData() to be called. */ | |
| 112 WAITING_FOR_WRITE, | |
| 113 /* Writing to the remote, callback will be invoked when done. */ | |
| 114 WRITING, | |
| 115 /* There is no more data to write and stream is half-closed by the local | |
| 116 side. */ | |
| 117 WRITING_DONE, | |
| 118 }; | |
| 119 | |
| 120 // net::BidirectionalStream::Delegate implementations: | |
| 121 void OnHeadersSent() override; | |
| 122 void OnHeadersReceived(const net::SpdyHeaderBlock& response_headers) override; | |
| 123 void OnDataRead(int bytes_read) override; | |
| 124 void OnDataSent() override; | |
| 125 void OnTrailersReceived(const net::SpdyHeaderBlock& trailers) override; | |
| 126 void OnFailed(int error) override; | |
| 127 // Helper method to derive OnSucceeded. | |
| 128 void MaybeOnSucceded(); | |
| 129 | |
| 130 void StartOnNetworkThread( | |
| 131 scoped_ptr<net::BidirectionalStreamRequestInfo> request_info); | |
| 132 void ReadDataOnNetworkThread(scoped_refptr<net::WrappedIOBuffer> read_buffer, | |
| 133 int buffer_size); | |
| 134 void WriteDataOnNetworkThread(scoped_refptr<net::WrappedIOBuffer> read_buffer, | |
| 135 int buffer_size, | |
| 136 bool end_of_stream); | |
| 137 void CancelOnNetworkThread(); | |
| 138 void DestroyOnNetworkThread(); | |
| 139 | |
| 140 /** | |
| 141 * Read state is tracking reading flow. Only accessed on network thread. | |
| 142 * / <--- READING <--- \ | |
| 143 * | | | |
| 144 * \ / | |
| 145 * NOT_STARTED -> STARTED --> WAITING_FOR_READ -> READING_DONE -> SUCCESS | |
| 146 */ | |
| 147 State read_state_; | |
| 148 | |
| 149 /** | |
| 150 * Write state is tracking writing flow. Only accessed on network thread. | |
| 151 * / <--- WRITING <--- \ | |
| 152 * | | | |
| 153 * \ / | |
| 154 * NOT_STARTED -> STARTED --> WAITING_FOR_WRITE -> WRITING_DONE -> SUCCESS | |
| 155 */ | |
| 156 State write_state_; | |
| 157 | |
| 158 bool write_end_of_stream_; | |
| 159 | |
| 160 CronetEnvironment* const environment_; | |
| 161 | |
| 162 scoped_refptr<net::WrappedIOBuffer> read_buffer_; | |
| 163 scoped_refptr<net::WrappedIOBuffer> write_buffer_; | |
| 164 scoped_ptr<net::BidirectionalStream> bidi_stream_; | |
| 165 Delegate* delegate_; | |
| 166 | |
| 167 DISALLOW_COPY_AND_ASSIGN(CronetBidirectionalStream); | |
| 168 }; | |
| 169 | |
| 170 } // namespace cronet | |
| 171 | |
| 172 #endif // COMPONENTS_CRONET_IOS_CRONET_BIDIRECTIONAL_STREAM_H_ | |
| OLD | NEW |