summaryrefslogtreecommitdiffstats
path: root/components/pairing/proto_decoder.h
blob: e36eb7fce06132e49f05e5a3da38ec27a35e66f1 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// 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 COMPONENTS_PAIRING_PROTO_DECODER_H_
#define COMPONENTS_PAIRING_PROTO_DECODER_H_

#include <deque>

#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "components/pairing/message_buffer.h"

namespace net {
class IOBuffer;
}

namespace pairing_api {
class CompleteSetup;
class ConfigureHost;
class Error;
class HostStatus;
class PairDevices;
}  // namespace pairing_api

namespace pairing_chromeos {

// A ProtoDecoder collects data from a series of IOBuffers and decodes Proto
// buffers from the data.  The decoded messages are then forwarded to an
// observer.
class ProtoDecoder {
 public:
  typedef scoped_refptr<net::IOBuffer> IOBufferRefPtr;
  class Observer {
   public:
    virtual ~Observer() {}

    virtual void OnHostStatusMessage(
        const pairing_api::HostStatus& message) = 0;
    virtual void OnConfigureHostMessage(
        const pairing_api::ConfigureHost& message) = 0;
    virtual void OnPairDevicesMessage(
        const pairing_api::PairDevices& message) = 0;
    virtual void OnCompleteSetupMessage(
        const pairing_api::CompleteSetup& message) = 0;
    virtual void OnErrorMessage(
        const pairing_api::Error& message) = 0;

   protected:
    Observer() {}

   private:
    DISALLOW_COPY_AND_ASSIGN(Observer);
  };

  explicit ProtoDecoder(Observer* observer);
  ~ProtoDecoder();

  // Decodes the data from an io_buffer, and sends out events for any complete
  // messages.
  bool DecodeIOBuffer(int size, IOBufferRefPtr io_buffer);

  // Convenience functions for serializing messages into an IOBuffer.
  static IOBufferRefPtr SendHostStatus(const pairing_api::HostStatus& message,
                                       int* size);
  static IOBufferRefPtr SendConfigureHost(
      const pairing_api::ConfigureHost& message, int* size);
  static IOBufferRefPtr SendPairDevices(const pairing_api::PairDevices& message,
                                        int* size);
  static IOBufferRefPtr SendCompleteSetup(
      const pairing_api::CompleteSetup& message, int* size);
  static IOBufferRefPtr SendError(const pairing_api::Error& message, int* size);

 private:
  static IOBufferRefPtr SendMessage(uint8_t message_type,
                                    const std::string& message,
                                    int* size);

  Observer* observer_;
  MessageBuffer message_buffer_;
  int next_message_type_;
  int next_message_size_;

  DISALLOW_COPY_AND_ASSIGN(ProtoDecoder);
};

}  // namespace pairing_chromeos

#endif  // COMPONENTS_PAIRING_PROTO_DECODER_H_