Speech refactoring: Reimplemented SpeechRecognitionManagerImpl as a FSM. (CL1.7)
[chromium-blink-merge.git] / net / spdy / spdy_frame_builder.h
blobc5467e98af1c4866dc8b18442b4bc99283dabb01
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_SPDY_FRAME_BUILDER_H_
6 #define NET_SPDY_SPDY_FRAME_BUILDER_H_
7 #pragma once
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/string_piece.h"
13 #include "base/sys_byteorder.h"
14 #include "net/base/net_export.h"
15 #include "net/spdy/spdy_protocol.h"
17 namespace net {
19 // This class provides facilities for basic binary value packing and unpacking
20 // into Spdy frames.
22 // The SpdyFrameBuilder supports appending primitive values (int, string, etc)
23 // to a frame instance. The SpdyFrameBuilder grows its internal memory buffer
24 // dynamically to hold the sequence of primitive values. The internal memory
25 // buffer is exposed as the "data" of the SpdyFrameBuilder.
26 class NET_EXPORT_PRIVATE SpdyFrameBuilder {
27 public:
28 ~SpdyFrameBuilder();
30 // Initializes a SpdyFrameBuilder with a buffer of given size,
31 // populate with a SPDY control frame header based on
32 // |type|, |flags|, and |spdy_version|.
33 SpdyFrameBuilder(SpdyControlType type, SpdyControlFlags flags,
34 int spdy_version, size_t size);
36 // Initiailizes a SpdyFrameBuilder with a buffer of given size,
37 // populated with a SPDY data frame header based on
38 // |stream_id| and |flags|.
39 SpdyFrameBuilder(SpdyStreamId stream_id, SpdyDataFlags flags, size_t size);
41 // Returns the size of the SpdyFrameBuilder's data.
42 size_t length() const { return length_; }
44 // Takes the buffer from the SpdyFrameBuilder.
45 SpdyFrame* take() {
46 SpdyFrame* rv = new SpdyFrame(buffer_, true);
47 buffer_ = NULL;
48 capacity_ = 0;
49 length_ = 0;
50 return rv;
53 // Methods for adding to the payload. These values are appended to the end
54 // of the SpdyFrameBuilder payload. Note - binary integers are converted from
55 // host to network form.
56 bool WriteUInt8(uint8 value) {
57 return WriteBytes(&value, sizeof(value));
59 bool WriteUInt16(uint16 value) {
60 value = htons(value);
61 return WriteBytes(&value, sizeof(value));
63 bool WriteUInt32(uint32 value) {
64 value = htonl(value);
65 return WriteBytes(&value, sizeof(value));
67 // TODO(hkhalil) Rename to WriteStringPiece16().
68 bool WriteString(const std::string& value);
69 bool WriteStringPiece32(const base::StringPiece& value);
70 bool WriteBytes(const void* data, uint32 data_len);
72 // Write an integer to a particular offset in the data buffer.
73 bool WriteUInt32ToOffset(int offset, uint32 value) {
74 value = htonl(value);
75 return WriteBytesToOffset(offset, &value, sizeof(value));
78 // Write to a particular offset in the data buffer.
79 bool WriteBytesToOffset(int offset, const void* data, uint32 data_len) {
80 if (offset + data_len > length_)
81 return false;
82 char *ptr = buffer_ + offset;
83 memcpy(ptr, data, data_len);
84 return true;
87 // Returns true if the given iterator could point to data with the given
88 // length. If there is no room for the given data before the end of the
89 // payload, returns false.
90 bool IteratorHasRoomFor(const void* iter, int len) const {
91 const char* end_of_region = reinterpret_cast<const char*>(iter) + len;
92 if (len < 0 ||
93 iter < buffer_ ||
94 iter > end_of_payload() ||
95 iter > end_of_region ||
96 end_of_region > end_of_payload())
97 return false;
99 // Watch out for overflow in pointer calculation, which wraps.
100 return (iter <= end_of_region) && (end_of_region <= end_of_payload());
103 protected:
104 size_t capacity() const {
105 return capacity_;
108 const char* end_of_payload() const { return buffer_ + length_; }
110 // Completes the write operation by padding the data with NULL bytes until it
111 // is padded. Should be paired with BeginWrite, but it does not necessarily
112 // have to be called after the data is written.
113 void EndWrite(char* dest, int length);
115 // Moves the iterator by the given number of bytes.
116 static void UpdateIter(void** iter, int bytes) {
117 *iter = static_cast<char*>(*iter) + bytes;
120 private:
121 // Returns the location that the data should be written at, or NULL if there
122 // is not enough room. Call EndWrite with the returned offset and the given
123 // length to pad out for the next write.
124 char* BeginWrite(size_t length);
126 char* buffer_;
127 size_t capacity_; // Allocation size of payload (or -1 if buffer is const).
128 size_t length_; // current length of the buffer
131 } // namespace net
133 #endif // NET_SPDY_SPDY_FRAME_BUILDER_H_