Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(597)

Side by Side Diff: extensions/browser/api/cast_channel/cast_framer.h

Issue 555283002: Create new class "CastTransport", which encapsulates the message read and write event loops. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added unit tests to CL Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_FRAMER_H_
6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_FRAMER_H_
7
8 #include <string>
9
10 #include "base/basictypes.h"
11 #include "extensions/common/api/cast_channel.h"
12 #include "net/base/io_buffer.h"
13
14 namespace extensions {
15 namespace core_api {
16 namespace cast_channel {
17 class CastMessage;
18
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();
26
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();
31
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);
36
37 // Reads bytes from |input_buffer_| and returns a new CastMessage if one
38 // is fully read.
39 //
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, NULL otherwise.
48 scoped_ptr<CastMessage> Ingest(size_t num_bytes,
49 size_t* message_length,
50 ChannelError* error);
51
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;
70 };
71
72 private:
73 enum MessageElement { HEADER, BODY };
74
75 // Prepares the framer for ingesting a new message.
76 void Reset();
77
78 // The element of the message that will be read on the next call to Ingest().
79 MessageElement current_element_;
80
81 // Total size of the message, in bytes (head + body).
82 size_t message_bytes_received_;
83
84 // Size of the body alone, in bytes.
85 size_t body_size_;
86
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_;
90
91 // Disables Ingest functionality is the parser receives invalid data.
92 bool error_;
93
94 DISALLOW_COPY_AND_ASSIGN(MessageFramer);
95 };
96 } // namespace cast_channel
97 } // namespace core_api
98 } // namespace extensions
99 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_FRAMER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698