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

Side by Side Diff: remoting/protocol/protobuf_message_parser.h

Issue 841773005: Cleanup channel dispatchers (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 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
« no previous file with comments | « remoting/protocol/message_reader_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 REMOTING_PROTOCOL_PROTOBUF_MESSAGE_PARSER_H_
6 #define REMOTING_PROTOCOL_PROTOBUF_MESSAGE_PARSER_H_
7
8 #include "base/bind.h"
9 #include "base/callback.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "remoting/base/compound_buffer.h"
12 #include "remoting/protocol/message_reader.h"
13
14 namespace remoting {
15 namespace protocol {
16
17 // Version of MessageReader for protocol buffer messages, that parses
18 // each incoming message.
19 template <class T>
20 class ProtobufMessageParser {
21 public:
22 // The callback that is called when a new message is received. |done_task|
23 // must be called by the callback when it's done processing the |message|.
24 typedef typename base::Callback<void(scoped_ptr<T> message,
25 const base::Closure& done_task)>
26 MessageReceivedCallback;
27
28 // |message_reader| must outlive ProtobufMessageParser.
29 ProtobufMessageParser(const MessageReceivedCallback& callback,
30 MessageReader* message_reader)
31 : message_reader_(message_reader),
32 message_received_callback_(callback) {
33 message_reader->SetMessageReceivedCallback(base::Bind(
34 &ProtobufMessageParser<T>::OnNewData, base::Unretained(this)));
35 }
36 ~ProtobufMessageParser() {
37 message_reader_->SetMessageReceivedCallback(
38 MessageReader::MessageReceivedCallback());
39 }
40
41 private:
42 void OnNewData(scoped_ptr<CompoundBuffer> buffer,
43 const base::Closure& done_task) {
44 scoped_ptr<T> message(new T());
45 CompoundBufferInputStream stream(buffer.get());
46 bool ret = message->ParseFromZeroCopyStream(&stream);
47 if (!ret) {
48 LOG(WARNING) << "Received message that is not a valid protocol buffer.";
49 } else {
50 DCHECK_EQ(stream.position(), buffer->total_bytes());
51 message_received_callback_.Run(message.Pass(), done_task);
52 }
53 }
54
55 MessageReader* message_reader_;
56 MessageReceivedCallback message_received_callback_;
57 };
58
59 } // namespace protocol
60 } // namespace remoting
61
62 #endif // REMOTING_PROTOCOL_PROTOBUF_MESSAGE_PARSER_H_
OLDNEW
« no previous file with comments | « remoting/protocol/message_reader_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698