Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / components / pairing / proto_decoder.h
blob826c4bb029d7ea83ff93bb03fbff7fcbce5d05c9
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_
8 #include <deque>
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"
16 namespace net {
17 class IOBuffer;
20 namespace pairing_api {
21 class AddNetwork;
22 class CompleteSetup;
23 class ConfigureHost;
24 class Error;
25 class HostStatus;
26 class PairDevices;
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
33 // observer.
34 class ProtoDecoder {
35 public:
36 typedef scoped_refptr<net::IOBuffer> IOBufferRefPtr;
37 class Observer {
38 public:
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;
54 protected:
55 Observer() {}
57 private:
58 DISALLOW_COPY_AND_ASSIGN(Observer);
61 explicit ProtoDecoder(Observer* observer);
62 ~ProtoDecoder();
64 // Decodes the data from an io_buffer, and sends out events for any complete
65 // messages.
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,
70 int* size);
71 static IOBufferRefPtr SendConfigureHost(
72 const pairing_api::ConfigureHost& message, int* size);
73 static IOBufferRefPtr SendPairDevices(const pairing_api::PairDevices& message,
74 int* size);
75 static IOBufferRefPtr SendCompleteSetup(
76 const pairing_api::CompleteSetup& message, int* size);
77 static IOBufferRefPtr SendError(const pairing_api::Error& message, int* size);
79 private:
80 static IOBufferRefPtr SendMessage(uint8_t message_type,
81 const std::string& message,
82 int* size);
84 Observer* observer_;
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_