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