[email protected] | f33c8675 | 2014-08-12 22:50:03 | [diff] [blame] | 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 | |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame^] | 8 | #include <stdint.h> |
| 9 | |
[email protected] | f33c8675 | 2014-08-12 22:50:03 | [diff] [blame] | 10 | #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 | |
| 18 | namespace net { |
| 19 | class IOBuffer; |
| 20 | } |
| 21 | |
| 22 | namespace pairing_api { |
zork | b7c25e1 | 2015-04-22 01:13:52 | [diff] [blame] | 23 | class AddNetwork; |
[email protected] | f33c8675 | 2014-08-12 22:50:03 | [diff] [blame] | 24 | class CompleteSetup; |
| 25 | class ConfigureHost; |
| 26 | class Error; |
| 27 | class HostStatus; |
| 28 | class PairDevices; |
| 29 | } // namespace pairing_api |
| 30 | |
| 31 | namespace 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. |
| 36 | class 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; |
zork | b7c25e1 | 2015-04-22 01:13:52 | [diff] [blame] | 53 | virtual void OnAddNetworkMessage( |
| 54 | const pairing_api::AddNetwork& message) = 0; |
[email protected] | f33c8675 | 2014-08-12 22:50:03 | [diff] [blame] | 55 | |
| 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); |
xdai | ca9c815 | 2015-11-10 01:30:15 | [diff] [blame] | 73 | static IOBufferRefPtr SendHostNetwork(const pairing_api::AddNetwork& message, |
| 74 | int* size); |
[email protected] | f33c8675 | 2014-08-12 22:50:03 | [diff] [blame] | 75 | 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_ |