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