Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / net / spdy / buffered_spdy_framer.h
blobb95501d66a025ddbee1a6281c32bcc15daf611b7
1 // Copyright (c) 2012 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.
5 #ifndef NET_SPDY_BUFFERED_SPDY_FRAMER_H_
6 #define NET_SPDY_BUFFERED_SPDY_FRAMER_H_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "net/base/net_export.h"
14 #include "net/socket/next_proto.h"
15 #include "net/spdy/spdy_framer.h"
16 #include "net/spdy/spdy_header_block.h"
17 #include "net/spdy/spdy_protocol.h"
19 namespace net {
21 // Returns the SPDY major version corresponding to the given NextProto
22 // value, which must represent a SPDY-like protocol.
23 NET_EXPORT_PRIVATE SpdyMajorVersion NextProtoToSpdyMajorVersion(
24 NextProto next_proto);
26 class NET_EXPORT_PRIVATE BufferedSpdyFramerVisitorInterface {
27 public:
28 BufferedSpdyFramerVisitorInterface() {}
30 // Called if an error is detected in the SpdyFrame protocol.
31 virtual void OnError(SpdyFramer::SpdyError error_code) = 0;
33 // Called if an error is detected in a SPDY stream.
34 virtual void OnStreamError(SpdyStreamId stream_id,
35 const std::string& description) = 0;
37 // Called after all the header data for SYN_STREAM control frame is received.
38 virtual void OnSynStream(SpdyStreamId stream_id,
39 SpdyStreamId associated_stream_id,
40 SpdyPriority priority,
41 bool fin,
42 bool unidirectional,
43 const SpdyHeaderBlock& headers) = 0;
45 // Called after all the header data for SYN_REPLY control frame is received.
46 virtual void OnSynReply(SpdyStreamId stream_id,
47 bool fin,
48 const SpdyHeaderBlock& headers) = 0;
50 // Called after all the header data for HEADERS control frame is received.
51 virtual void OnHeaders(SpdyStreamId stream_id,
52 bool has_priority,
53 SpdyPriority priority,
54 SpdyStreamId parent_stream_id,
55 bool exclusive,
56 bool fin,
57 const SpdyHeaderBlock& headers) = 0;
59 // Called when a data frame header is received.
60 virtual void OnDataFrameHeader(SpdyStreamId stream_id,
61 size_t length,
62 bool fin) = 0;
64 // Called when data is received.
65 // |stream_id| The stream receiving data.
66 // |data| A buffer containing the data received.
67 // |len| The length of the data buffer (at most 2^24 - 1 for SPDY/3,
68 // but 2^16 - 1 - 8 for SPDY/4).
69 // When the other side has finished sending data on this stream,
70 // this method will be called with a zero-length buffer.
71 virtual void OnStreamFrameData(SpdyStreamId stream_id,
72 const char* data,
73 size_t len,
74 bool fin) = 0;
76 // Called when padding is received (padding length field or padding octets).
77 // |stream_id| The stream receiving data.
78 // |len| The number of padding octets.
79 virtual void OnStreamPadding(SpdyStreamId stream_id, size_t len) = 0;
81 // Called when a SETTINGS frame is received.
82 // |clear_persisted| True if the respective flag is set on the SETTINGS frame.
83 virtual void OnSettings(bool clear_persisted) = 0;
85 // Called when an individual setting within a SETTINGS frame has been parsed
86 // and validated.
87 virtual void OnSetting(SpdySettingsIds id, uint8 flags, uint32 value) = 0;
89 // Called when a SETTINGS frame is received with the ACK flag set.
90 virtual void OnSettingsAck() {}
92 // Called at the completion of parsing SETTINGS id and value tuples.
93 virtual void OnSettingsEnd() {}
95 // Called when a PING frame has been parsed.
96 virtual void OnPing(SpdyPingId unique_id, bool is_ack) = 0;
98 // Called when a RST_STREAM frame has been parsed.
99 virtual void OnRstStream(SpdyStreamId stream_id,
100 SpdyRstStreamStatus status) = 0;
102 // Called when a GOAWAY frame has been parsed.
103 virtual void OnGoAway(SpdyStreamId last_accepted_stream_id,
104 SpdyGoAwayStatus status) = 0;
106 // Called when a WINDOW_UPDATE frame has been parsed.
107 virtual void OnWindowUpdate(SpdyStreamId stream_id,
108 int delta_window_size) = 0;
110 // Called when a PUSH_PROMISE frame has been parsed.
111 virtual void OnPushPromise(SpdyStreamId stream_id,
112 SpdyStreamId promised_stream_id,
113 const SpdyHeaderBlock& headers) = 0;
115 // Called when a frame type we don't recognize is received.
116 // Return true if this appears to be a valid extension frame, false otherwise.
117 // We distinguish between extension frames and nonsense by checking
118 // whether the stream id is valid.
119 virtual bool OnUnknownFrame(SpdyStreamId stream_id, int frame_type) = 0;
121 protected:
122 virtual ~BufferedSpdyFramerVisitorInterface() {}
124 private:
125 DISALLOW_COPY_AND_ASSIGN(BufferedSpdyFramerVisitorInterface);
128 class NET_EXPORT_PRIVATE BufferedSpdyFramer
129 : public SpdyFramerVisitorInterface {
130 public:
131 BufferedSpdyFramer(SpdyMajorVersion version,
132 bool enable_compression);
133 ~BufferedSpdyFramer() override;
135 // Sets callbacks to be called from the buffered spdy framer. A visitor must
136 // be set, or else the framer will likely crash. It is acceptable for the
137 // visitor to do nothing. If this is called multiple times, only the last
138 // visitor will be used.
139 void set_visitor(BufferedSpdyFramerVisitorInterface* visitor);
141 // Set debug callbacks to be called from the framer. The debug visitor is
142 // completely optional and need not be set in order for normal operation.
143 // If this is called multiple times, only the last visitor will be used.
144 void set_debug_visitor(SpdyFramerDebugVisitorInterface* debug_visitor);
146 // SpdyFramerVisitorInterface
147 void OnError(SpdyFramer* spdy_framer) override;
148 void OnSynStream(SpdyStreamId stream_id,
149 SpdyStreamId associated_stream_id,
150 SpdyPriority priority,
151 bool fin,
152 bool unidirectional) override;
153 void OnSynReply(SpdyStreamId stream_id, bool fin) override;
154 void OnHeaders(SpdyStreamId stream_id,
155 bool has_priority,
156 SpdyPriority priority,
157 SpdyStreamId parent_stream_id,
158 bool exclusive,
159 bool fin,
160 bool end) override;
161 bool OnControlFrameHeaderData(SpdyStreamId stream_id,
162 const char* header_data,
163 size_t len) override;
164 void OnStreamFrameData(SpdyStreamId stream_id,
165 const char* data,
166 size_t len,
167 bool fin) override;
168 void OnStreamPadding(SpdyStreamId stream_id, size_t len) override;
169 void OnSettings(bool clear_persisted) override;
170 void OnSetting(SpdySettingsIds id, uint8 flags, uint32 value) override;
171 void OnSettingsAck() override;
172 void OnSettingsEnd() override;
173 void OnPing(SpdyPingId unique_id, bool is_ack) override;
174 void OnRstStream(SpdyStreamId stream_id, SpdyRstStreamStatus status) override;
175 void OnGoAway(SpdyStreamId last_accepted_stream_id,
176 SpdyGoAwayStatus status) override;
177 void OnWindowUpdate(SpdyStreamId stream_id, int delta_window_size) override;
178 void OnPushPromise(SpdyStreamId stream_id,
179 SpdyStreamId promised_stream_id,
180 bool end) override;
181 void OnDataFrameHeader(SpdyStreamId stream_id,
182 size_t length,
183 bool fin) override;
184 void OnContinuation(SpdyStreamId stream_id, bool end) override;
185 bool OnUnknownFrame(SpdyStreamId stream_id, int frame_type) override;
187 // SpdyFramer methods.
188 size_t ProcessInput(const char* data, size_t len);
189 SpdyMajorVersion protocol_version();
190 void Reset();
191 SpdyFramer::SpdyError error_code() const;
192 SpdyFramer::SpdyState state() const;
193 bool MessageFullyRead();
194 bool HasError();
195 SpdyFrame* CreateSynStream(SpdyStreamId stream_id,
196 SpdyStreamId associated_stream_id,
197 SpdyPriority priority,
198 SpdyControlFlags flags,
199 const SpdyHeaderBlock* headers);
200 SpdyFrame* CreateSynReply(SpdyStreamId stream_id,
201 SpdyControlFlags flags,
202 const SpdyHeaderBlock* headers);
203 SpdyFrame* CreateRstStream(SpdyStreamId stream_id,
204 SpdyRstStreamStatus status) const;
205 SpdyFrame* CreateSettings(const SettingsMap& values) const;
206 SpdyFrame* CreatePingFrame(SpdyPingId unique_id, bool is_ack) const;
207 SpdyFrame* CreateGoAway(
208 SpdyStreamId last_accepted_stream_id,
209 SpdyGoAwayStatus status) const;
210 SpdyFrame* CreateHeaders(SpdyStreamId stream_id,
211 SpdyControlFlags flags,
212 SpdyPriority priority,
213 const SpdyHeaderBlock* headers);
214 SpdyFrame* CreateWindowUpdate(
215 SpdyStreamId stream_id,
216 uint32 delta_window_size) const;
217 SpdyFrame* CreateDataFrame(SpdyStreamId stream_id,
218 const char* data,
219 uint32 len,
220 SpdyDataFlags flags);
221 SpdyFrame* CreatePushPromise(SpdyStreamId stream_id,
222 SpdyStreamId promised_stream_id,
223 const SpdyHeaderBlock* headers);
225 // Serialize a frame of unknown type.
226 SpdySerializedFrame* SerializeFrame(const SpdyFrameIR& frame) {
227 return spdy_framer_.SerializeFrame(frame);
230 SpdyPriority GetHighestPriority() const;
232 size_t GetDataFrameMinimumSize() const {
233 return spdy_framer_.GetDataFrameMinimumSize();
236 size_t GetControlFrameHeaderSize() const {
237 return spdy_framer_.GetControlFrameHeaderSize();
240 size_t GetSynStreamMinimumSize() const {
241 return spdy_framer_.GetSynStreamMinimumSize();
244 size_t GetFrameMinimumSize() const {
245 return spdy_framer_.GetFrameMinimumSize();
248 size_t GetFrameMaximumSize() const {
249 return spdy_framer_.GetFrameMaximumSize();
252 size_t GetDataFrameMaximumPayload() const {
253 return spdy_framer_.GetDataFrameMaximumPayload();
256 int frames_received() const { return frames_received_; }
258 private:
259 // The size of the header_buffer_.
260 enum { kHeaderBufferSize = 32 * 1024 };
262 void InitHeaderStreaming(SpdyStreamId stream_id);
264 SpdyFramer spdy_framer_;
265 BufferedSpdyFramerVisitorInterface* visitor_;
267 // Header block streaming state:
268 char header_buffer_[kHeaderBufferSize];
269 size_t header_buffer_used_;
270 bool header_buffer_valid_;
271 SpdyStreamId header_stream_id_;
272 int frames_received_;
274 // Collection of fields from control frames that we need to
275 // buffer up from the spdy framer.
276 struct ControlFrameFields {
277 SpdyFrameType type;
278 SpdyStreamId stream_id;
279 SpdyStreamId associated_stream_id;
280 SpdyStreamId promised_stream_id;
281 bool has_priority;
282 SpdyPriority priority;
283 SpdyStreamId parent_stream_id;
284 bool exclusive;
285 uint8 credential_slot;
286 bool fin;
287 bool unidirectional;
289 scoped_ptr<ControlFrameFields> control_frame_fields_;
291 DISALLOW_COPY_AND_ASSIGN(BufferedSpdyFramer);
294 } // namespace net
296 #endif // NET_SPDY_BUFFERED_SPDY_FRAMER_H_