• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef COMPONENTS_PAIRING_PROTO_DECODER_H_
6 #define COMPONENTS_PAIRING_PROTO_DECODER_H_
7 
8 #include <deque>
9 
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"
15 
16 namespace net {
17 class IOBuffer;
18 }
19 
20 namespace pairing_api {
21 class CompleteSetup;
22 class ConfigureHost;
23 class Error;
24 class HostStatus;
25 class PairDevices;
26 }  // namespace pairing_api
27 
28 namespace pairing_chromeos {
29 
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
32 // observer.
33 class ProtoDecoder {
34  public:
35   typedef scoped_refptr<net::IOBuffer> IOBufferRefPtr;
36   class Observer {
37    public:
~Observer()38     virtual ~Observer() {}
39 
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;
50 
51    protected:
Observer()52     Observer() {}
53 
54    private:
55     DISALLOW_COPY_AND_ASSIGN(Observer);
56   };
57 
58   explicit ProtoDecoder(Observer* observer);
59   ~ProtoDecoder();
60 
61   // Decodes the data from an io_buffer, and sends out events for any complete
62   // messages.
63   bool DecodeIOBuffer(int size, IOBufferRefPtr io_buffer);
64 
65   // Convenience functions for serializing messages into an IOBuffer.
66   static IOBufferRefPtr SendHostStatus(const pairing_api::HostStatus& message,
67                                        int* size);
68   static IOBufferRefPtr SendConfigureHost(
69       const pairing_api::ConfigureHost& message, int* size);
70   static IOBufferRefPtr SendPairDevices(const pairing_api::PairDevices& message,
71                                         int* size);
72   static IOBufferRefPtr SendCompleteSetup(
73       const pairing_api::CompleteSetup& message, int* size);
74   static IOBufferRefPtr SendError(const pairing_api::Error& message, int* size);
75 
76  private:
77   static IOBufferRefPtr SendMessage(uint8_t message_type,
78                                     const std::string& message,
79                                     int* size);
80 
81   Observer* observer_;
82   MessageBuffer message_buffer_;
83   int next_message_type_;
84   int next_message_size_;
85 
86   DISALLOW_COPY_AND_ASSIGN(ProtoDecoder);
87 };
88 
89 }  // namespace pairing_chromeos
90 
91 #endif  // COMPONENTS_PAIRING_PROTO_DECODER_H_
92