| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_PAIRING_PROTO_DECODER_H_ | 5 #ifndef COMPONENTS_PAIRING_PROTO_DECODER_H_ |
| 6 #define COMPONENTS_PAIRING_PROTO_DECODER_H_ | 6 #define COMPONENTS_PAIRING_PROTO_DECODER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <deque> | 10 #include <deque> |
| 11 | 11 |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "components/pairing/message_buffer.h" | 15 #include "components/pairing/message_buffer.h" |
| 16 | 16 |
| 17 namespace net { | 17 namespace net { |
| 18 class IOBuffer; | 18 class IOBuffer; |
| 19 } | 19 } |
| 20 | 20 |
| 21 namespace pairing_api { | 21 namespace pairing_api { |
| 22 class AddNetwork; | 22 class AddNetwork; |
| 23 class CompleteSetup; | 23 class CompleteSetup; |
| 24 class ConfigureHost; | 24 class ConfigureHost; |
| 25 class Error; | 25 class Error; |
| 26 class HostStatus; | 26 class HostStatus; |
| 27 class PairDevices; | 27 class PairDevices; |
| 28 class Reboot; |
| 28 } // namespace pairing_api | 29 } // namespace pairing_api |
| 29 | 30 |
| 30 namespace pairing_chromeos { | 31 namespace pairing_chromeos { |
| 31 | 32 |
| 32 // A ProtoDecoder collects data from a series of IOBuffers and decodes Proto | 33 // A ProtoDecoder collects data from a series of IOBuffers and decodes Proto |
| 33 // buffers from the data. The decoded messages are then forwarded to an | 34 // buffers from the data. The decoded messages are then forwarded to an |
| 34 // observer. | 35 // observer. |
| 35 class ProtoDecoder { | 36 class ProtoDecoder { |
| 36 public: | 37 public: |
| 37 typedef scoped_refptr<net::IOBuffer> IOBufferRefPtr; | 38 typedef scoped_refptr<net::IOBuffer> IOBufferRefPtr; |
| 38 class Observer { | 39 class Observer { |
| 39 public: | 40 public: |
| 40 virtual ~Observer() {} | 41 virtual ~Observer() {} |
| 41 | 42 |
| 42 virtual void OnHostStatusMessage( | 43 virtual void OnHostStatusMessage( |
| 43 const pairing_api::HostStatus& message) = 0; | 44 const pairing_api::HostStatus& message) = 0; |
| 44 virtual void OnConfigureHostMessage( | 45 virtual void OnConfigureHostMessage( |
| 45 const pairing_api::ConfigureHost& message) = 0; | 46 const pairing_api::ConfigureHost& message) = 0; |
| 46 virtual void OnPairDevicesMessage( | 47 virtual void OnPairDevicesMessage( |
| 47 const pairing_api::PairDevices& message) = 0; | 48 const pairing_api::PairDevices& message) = 0; |
| 48 virtual void OnCompleteSetupMessage( | 49 virtual void OnCompleteSetupMessage( |
| 49 const pairing_api::CompleteSetup& message) = 0; | 50 const pairing_api::CompleteSetup& message) = 0; |
| 50 virtual void OnErrorMessage( | 51 virtual void OnErrorMessage( |
| 51 const pairing_api::Error& message) = 0; | 52 const pairing_api::Error& message) = 0; |
| 52 virtual void OnAddNetworkMessage( | 53 virtual void OnAddNetworkMessage( |
| 53 const pairing_api::AddNetwork& message) = 0; | 54 const pairing_api::AddNetwork& message) = 0; |
| 55 virtual void OnRebootMessage(const pairing_api::Reboot& message) = 0; |
| 54 | 56 |
| 55 protected: | 57 protected: |
| 56 Observer() {} | 58 Observer() {} |
| 57 | 59 |
| 58 private: | 60 private: |
| 59 DISALLOW_COPY_AND_ASSIGN(Observer); | 61 DISALLOW_COPY_AND_ASSIGN(Observer); |
| 60 }; | 62 }; |
| 61 | 63 |
| 62 explicit ProtoDecoder(Observer* observer); | 64 explicit ProtoDecoder(Observer* observer); |
| 63 ~ProtoDecoder(); | 65 ~ProtoDecoder(); |
| 64 | 66 |
| 65 // Decodes the data from an io_buffer, and sends out events for any complete | 67 // Decodes the data from an io_buffer, and sends out events for any complete |
| 66 // messages. | 68 // messages. |
| 67 bool DecodeIOBuffer(int size, IOBufferRefPtr io_buffer); | 69 bool DecodeIOBuffer(int size, IOBufferRefPtr io_buffer); |
| 68 | 70 |
| 69 // Convenience functions for serializing messages into an IOBuffer. | 71 // Convenience functions for serializing messages into an IOBuffer. |
| 70 static IOBufferRefPtr SendHostStatus(const pairing_api::HostStatus& message, | 72 static IOBufferRefPtr SendHostStatus(const pairing_api::HostStatus& message, |
| 71 int* size); | 73 int* size); |
| 72 static IOBufferRefPtr SendHostNetwork(const pairing_api::AddNetwork& message, | 74 static IOBufferRefPtr SendHostNetwork(const pairing_api::AddNetwork& message, |
| 73 int* size); | 75 int* size); |
| 74 static IOBufferRefPtr SendConfigureHost( | 76 static IOBufferRefPtr SendConfigureHost( |
| 75 const pairing_api::ConfigureHost& message, int* size); | 77 const pairing_api::ConfigureHost& message, int* size); |
| 76 static IOBufferRefPtr SendPairDevices(const pairing_api::PairDevices& message, | 78 static IOBufferRefPtr SendPairDevices(const pairing_api::PairDevices& message, |
| 77 int* size); | 79 int* size); |
| 78 static IOBufferRefPtr SendCompleteSetup( | 80 static IOBufferRefPtr SendCompleteSetup( |
| 79 const pairing_api::CompleteSetup& message, int* size); | 81 const pairing_api::CompleteSetup& message, int* size); |
| 80 static IOBufferRefPtr SendError(const pairing_api::Error& message, int* size); | 82 static IOBufferRefPtr SendError(const pairing_api::Error& message, int* size); |
| 83 static IOBufferRefPtr SendRebootHost(const pairing_api::Reboot& message, |
| 84 int* size); |
| 81 | 85 |
| 82 private: | 86 private: |
| 83 static IOBufferRefPtr SendMessage(uint8_t message_type, | 87 static IOBufferRefPtr SendMessage(uint8_t message_type, |
| 84 const std::string& message, | 88 const std::string& message, |
| 85 int* size); | 89 int* size); |
| 86 | 90 |
| 87 Observer* observer_; | 91 Observer* observer_; |
| 88 MessageBuffer message_buffer_; | 92 MessageBuffer message_buffer_; |
| 89 int next_message_type_; | 93 int next_message_type_; |
| 90 int next_message_size_; | 94 int next_message_size_; |
| 91 | 95 |
| 92 DISALLOW_COPY_AND_ASSIGN(ProtoDecoder); | 96 DISALLOW_COPY_AND_ASSIGN(ProtoDecoder); |
| 93 }; | 97 }; |
| 94 | 98 |
| 95 } // namespace pairing_chromeos | 99 } // namespace pairing_chromeos |
| 96 | 100 |
| 97 #endif // COMPONENTS_PAIRING_PROTO_DECODER_H_ | 101 #endif // COMPONENTS_PAIRING_PROTO_DECODER_H_ |
| OLD | NEW |