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.
5 #ifndef COMPONENTS_PAIRING_PROTO_DECODER_H_
6 #define COMPONENTS_PAIRING_PROTO_DECODER_H_
10 #include "base/logging.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "components/pairing/message_buffer.h"
20 namespace pairing_api
{
26 } // namespace pairing_api
28 namespace pairing_chromeos
{
30 // A ProtoDecoder collects data from a series of IOBuffers and decodes Proto
31 // buffers from the data. The decoded messages are then forwarded to an
35 typedef scoped_refptr
<net::IOBuffer
> IOBufferRefPtr
;
38 virtual ~Observer() {}
40 virtual void OnHostStatusMessage(
41 const pairing_api::HostStatus
& message
) = 0;
42 virtual void OnConfigureHostMessage(
43 const pairing_api::ConfigureHost
& message
) = 0;
44 virtual void OnPairDevicesMessage(
45 const pairing_api::PairDevices
& message
) = 0;
46 virtual void OnCompleteSetupMessage(
47 const pairing_api::CompleteSetup
& message
) = 0;
48 virtual void OnErrorMessage(
49 const pairing_api::Error
& message
) = 0;
55 DISALLOW_COPY_AND_ASSIGN(Observer
);
58 explicit ProtoDecoder(Observer
* observer
);
61 // Decodes the data from an io_buffer, and sends out events for any complete
63 bool DecodeIOBuffer(int size
, IOBufferRefPtr io_buffer
);
65 // Convenience functions for serializing messages into an IOBuffer.
66 static IOBufferRefPtr
SendHostStatus(const pairing_api::HostStatus
& message
,
68 static IOBufferRefPtr
SendConfigureHost(
69 const pairing_api::ConfigureHost
& message
, int* size
);
70 static IOBufferRefPtr
SendPairDevices(const pairing_api::PairDevices
& message
,
72 static IOBufferRefPtr
SendCompleteSetup(
73 const pairing_api::CompleteSetup
& message
, int* size
);
74 static IOBufferRefPtr
SendError(const pairing_api::Error
& message
, int* size
);
77 static IOBufferRefPtr
SendMessage(uint8_t message_type
,
78 const std::string
& message
,
82 MessageBuffer message_buffer_
;
83 int next_message_type_
;
84 int next_message_size_
;
86 DISALLOW_COPY_AND_ASSIGN(ProtoDecoder
);
89 } // namespace pairing_chromeos
91 #endif // COMPONENTS_PAIRING_PROTO_DECODER_H_