summaryrefslogtreecommitdiffstats
path: root/remoting/protocol/protobuf_message_parser.h
blob: 68840b1fc6d3722a9ca4a556a39b5c89262353a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef REMOTING_PROTOCOL_PROTOBUF_MESSAGE_PARSER_H_
#define REMOTING_PROTOCOL_PROTOBUF_MESSAGE_PARSER_H_

#include "base/bind.h"
#include "base/callback.h"
#include "base/memory/scoped_ptr.h"
#include "remoting/base/compound_buffer.h"
#include "remoting/protocol/message_reader.h"

namespace remoting {
namespace protocol {

// Version of MessageReader for protocol buffer messages, that parses
// each incoming message.
template <class T>
class ProtobufMessageParser {
 public:
  // The callback that is called when a new message is received. |done_task|
  // must be called by the callback when it's done processing the |message|.
  typedef typename base::Callback<void(scoped_ptr<T> message,
                                       const base::Closure& done_task)>
      MessageReceivedCallback;

  // |message_reader| must outlive ProtobufMessageParser.
  ProtobufMessageParser(const MessageReceivedCallback& callback,
                        MessageReader* message_reader)
      : message_reader_(message_reader),
        message_received_callback_(callback) {
    message_reader->SetMessageReceivedCallback(base::Bind(
        &ProtobufMessageParser<T>::OnNewData, base::Unretained(this)));
  }
  ~ProtobufMessageParser() {
    message_reader_->SetMessageReceivedCallback(
        MessageReader::MessageReceivedCallback());
  }

 private:
  void OnNewData(scoped_ptr<CompoundBuffer> buffer,
                 const base::Closure& done_task) {
    scoped_ptr<T> message(new T());
    CompoundBufferInputStream stream(buffer.get());
    bool ret = message->ParseFromZeroCopyStream(&stream);
    if (!ret) {
      LOG(WARNING) << "Received message that is not a valid protocol buffer.";
    } else {
      DCHECK_EQ(stream.position(), buffer->total_bytes());
      message_received_callback_.Run(message.Pass(), done_task);
    }
  }

  MessageReader* message_reader_;
  MessageReceivedCallback message_received_callback_;
};

}  // namespace protocol
}  // namespace remoting

#endif  // REMOTING_PROTOCOL_PROTOBUF_MESSAGE_PARSER_H_