blob: ab82377ff1c6ac79fa6a3d5687f56413a42c54ef [file] [log] [blame]
[email protected]f33c86752014-08-12 22:50:031// 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_PAIRING_PROTO_DECODER_H_
6#define COMPONENTS_PAIRING_PROTO_DECODER_H_
7
avif57136c12015-12-25 23:27:458#include <stdint.h>
9
[email protected]f33c86752014-08-12 22:50:0310#include <deque>
11
12#include "base/logging.h"
13#include "base/macros.h"
14#include "base/memory/ref_counted.h"
[email protected]f33c86752014-08-12 22:50:0315#include "components/pairing/message_buffer.h"
16
17namespace net {
18class IOBuffer;
19}
20
21namespace pairing_api {
zorkb7c25e12015-04-22 01:13:5222class AddNetwork;
[email protected]f33c86752014-08-12 22:50:0323class CompleteSetup;
24class ConfigureHost;
25class Error;
26class HostStatus;
27class PairDevices;
28} // namespace pairing_api
29
30namespace pairing_chromeos {
31
32// A ProtoDecoder collects data from a series of IOBuffers and decodes Proto
33// buffers from the data. The decoded messages are then forwarded to an
34// observer.
35class ProtoDecoder {
36 public:
37 typedef scoped_refptr<net::IOBuffer> IOBufferRefPtr;
38 class Observer {
39 public:
40 virtual ~Observer() {}
41
42 virtual void OnHostStatusMessage(
43 const pairing_api::HostStatus& message) = 0;
44 virtual void OnConfigureHostMessage(
45 const pairing_api::ConfigureHost& message) = 0;
46 virtual void OnPairDevicesMessage(
47 const pairing_api::PairDevices& message) = 0;
48 virtual void OnCompleteSetupMessage(
49 const pairing_api::CompleteSetup& message) = 0;
50 virtual void OnErrorMessage(
51 const pairing_api::Error& message) = 0;
zorkb7c25e12015-04-22 01:13:5252 virtual void OnAddNetworkMessage(
53 const pairing_api::AddNetwork& message) = 0;
[email protected]f33c86752014-08-12 22:50:0354
55 protected:
56 Observer() {}
57
58 private:
59 DISALLOW_COPY_AND_ASSIGN(Observer);
60 };
61
62 explicit ProtoDecoder(Observer* observer);
63 ~ProtoDecoder();
64
65 // Decodes the data from an io_buffer, and sends out events for any complete
66 // messages.
67 bool DecodeIOBuffer(int size, IOBufferRefPtr io_buffer);
68
69 // Convenience functions for serializing messages into an IOBuffer.
70 static IOBufferRefPtr SendHostStatus(const pairing_api::HostStatus& message,
71 int* size);
xdaica9c8152015-11-10 01:30:1572 static IOBufferRefPtr SendHostNetwork(const pairing_api::AddNetwork& message,
73 int* size);
[email protected]f33c86752014-08-12 22:50:0374 static IOBufferRefPtr SendConfigureHost(
75 const pairing_api::ConfigureHost& message, int* size);
76 static IOBufferRefPtr SendPairDevices(const pairing_api::PairDevices& message,
77 int* size);
78 static IOBufferRefPtr SendCompleteSetup(
79 const pairing_api::CompleteSetup& message, int* size);
80 static IOBufferRefPtr SendError(const pairing_api::Error& message, int* size);
81
82 private:
83 static IOBufferRefPtr SendMessage(uint8_t message_type,
84 const std::string& message,
85 int* size);
86
87 Observer* observer_;
88 MessageBuffer message_buffer_;
89 int next_message_type_;
90 int next_message_size_;
91
92 DISALLOW_COPY_AND_ASSIGN(ProtoDecoder);
93};
94
95} // namespace pairing_chromeos
96
97#endif // COMPONENTS_PAIRING_PROTO_DECODER_H_