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
{
27 } // namespace pairing_api
29 namespace pairing_chromeos
{
31 // A ProtoDecoder collects data from a series of IOBuffers and decodes Proto
32 // buffers from the data. The decoded messages are then forwarded to an
36 typedef scoped_refptr
<net::IOBuffer
> IOBufferRefPtr
;
39 virtual ~Observer() {}
41 virtual void OnHostStatusMessage(
42 const pairing_api::HostStatus
& message
) = 0;
43 virtual void OnConfigureHostMessage(
44 const pairing_api::ConfigureHost
& message
) = 0;
45 virtual void OnPairDevicesMessage(
46 const pairing_api::PairDevices
& message
) = 0;
47 virtual void OnCompleteSetupMessage(
48 const pairing_api::CompleteSetup
& message
) = 0;
49 virtual void OnErrorMessage(
50 const pairing_api::Error
& message
) = 0;
51 virtual void OnAddNetworkMessage(
52 const pairing_api::AddNetwork
& message
) = 0;
58 DISALLOW_COPY_AND_ASSIGN(Observer
);
61 explicit ProtoDecoder(Observer
* observer
);
64 // Decodes the data from an io_buffer, and sends out events for any complete
66 bool DecodeIOBuffer(int size
, IOBufferRefPtr io_buffer
);
68 // Convenience functions for serializing messages into an IOBuffer.
69 static IOBufferRefPtr
SendHostStatus(const pairing_api::HostStatus
& message
,
71 static IOBufferRefPtr
SendConfigureHost(
72 const pairing_api::ConfigureHost
& message
, int* size
);
73 static IOBufferRefPtr
SendPairDevices(const pairing_api::PairDevices
& message
,
75 static IOBufferRefPtr
SendCompleteSetup(
76 const pairing_api::CompleteSetup
& message
, int* size
);
77 static IOBufferRefPtr
SendError(const pairing_api::Error
& message
, int* size
);
80 static IOBufferRefPtr
SendMessage(uint8_t message_type
,
81 const std::string
& message
,
85 MessageBuffer message_buffer_
;
86 int next_message_type_
;
87 int next_message_size_
;
89 DISALLOW_COPY_AND_ASSIGN(ProtoDecoder
);
92 } // namespace pairing_chromeos
94 #endif // COMPONENTS_PAIRING_PROTO_DECODER_H_