Add ICU message format support
[chromium-blink-merge.git] / extensions / browser / api / cast_channel / cast_framer.h
blob81c9f90792bced1449ab2ff932d1671f622c4ff8
1 // Copyright 2014 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 EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_FRAMER_H_
6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_FRAMER_H_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "extensions/common/api/cast_channel.h"
12 #include "net/base/io_buffer.h"
14 namespace extensions {
15 namespace api {
16 namespace cast_channel {
17 class CastMessage;
19 // Class for constructing and parsing CastMessage packet data.
20 class MessageFramer {
21 public:
22 // |input_buffer|: The input buffer used by all socket read operations that
23 // feed data into the framer.
24 explicit MessageFramer(scoped_refptr<net::GrowableIOBuffer> input_buffer);
25 ~MessageFramer();
27 // The number of bytes required from |input_buffer| to complete the
28 // CastMessage being read.
29 // Returns zero if |error_| is true (framer is in an invalid state.)
30 size_t BytesRequested();
32 // Serializes |message_proto| into |message_data|.
33 // Returns true if the message was serialized successfully, false otherwise.
34 static bool Serialize(const CastMessage& message_proto,
35 std::string* message_data);
37 // Reads bytes from |input_buffer_| and returns a new CastMessage if one
38 // is fully read.
40 // |num_bytes| The number of bytes received by a read operation.
41 // Value must be <= BytesRequested().
42 // |message_length| Size of the deserialized message object, in bytes. For
43 // logging purposes. Set to zero if no message was parsed.
44 // |error| The result of the ingest operation. Set to CHANNEL_ERROR_NONE
45 // if no error occurred.
46 // Returns A pointer to a parsed CastMessage if a message was received
47 // in its entirety, nullptr otherwise.
48 scoped_ptr<CastMessage> Ingest(size_t num_bytes,
49 size_t* message_length,
50 ChannelError* error);
52 // Message header struct. If fields are added, be sure to update
53 // header_size(). Public to allow use of *_size() methods in unit tests.
54 struct MessageHeader {
55 MessageHeader();
56 // Sets the message size.
57 void SetMessageSize(size_t message_size);
58 // Prepends this header to |str|.
59 void PrependToString(std::string* str);
60 // Reads |header| from the bytes specified by |data|.
61 static void Deserialize(char* data, MessageHeader* header);
62 // Size (in bytes) of the message header.
63 static size_t header_size();
64 // Maximum size (in bytes) of a message payload on the wire (does not
65 // include header).
66 static size_t max_message_size();
67 std::string ToString();
68 // The size of the following protocol message in bytes, in host byte order.
69 size_t message_size;
72 private:
73 enum MessageElement { HEADER, BODY };
75 // Prepares the framer for ingesting a new message.
76 void Reset();
78 // The element of the message that will be read on the next call to Ingest().
79 MessageElement current_element_;
81 // Total size of the message, in bytes (head + body).
82 size_t message_bytes_received_;
84 // Size of the body alone, in bytes.
85 size_t body_size_;
87 // Input buffer which carries message data read from the socket.
88 // Caller is responsible for writing into this buffer.
89 scoped_refptr<net::GrowableIOBuffer> input_buffer_;
91 // Disables Ingest functionality is the parser receives invalid data.
92 bool error_;
94 DISALLOW_COPY_AND_ASSIGN(MessageFramer);
96 } // namespace cast_channel
97 } // namespace api
98 } // namespace extensions
99 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_FRAMER_H_