| 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 #include "components/pairing/proto_decoder.h" | 5 #include "components/pairing/proto_decoder.h" |
| 6 | 6 |
| 7 #include "components/pairing/pairing_api.pb.h" | 7 #include "components/pairing/pairing_api.pb.h" |
| 8 #include "net/base/io_buffer.h" | 8 #include "net/base/io_buffer.h" |
| 9 | 9 |
| 10 namespace { | 10 namespace { |
| 11 enum { | 11 enum { |
| 12 MESSAGE_NONE, | 12 MESSAGE_NONE, |
| 13 MESSAGE_HOST_STATUS, | 13 MESSAGE_HOST_STATUS, |
| 14 MESSAGE_CONFIGURE_HOST, | 14 MESSAGE_CONFIGURE_HOST, |
| 15 MESSAGE_PAIR_DEVICES, | 15 MESSAGE_PAIR_DEVICES, |
| 16 MESSAGE_COMPLETE_SETUP, | 16 MESSAGE_COMPLETE_SETUP, |
| 17 MESSAGE_ERROR, | 17 MESSAGE_ERROR, |
| 18 MESSAGE_ADD_NETWORK, | 18 MESSAGE_ADD_NETWORK, |
| 19 MESSAGE_REBOOT, |
| 19 NUM_MESSAGES, | 20 NUM_MESSAGES, |
| 20 }; | 21 }; |
| 21 } | 22 } |
| 22 | 23 |
| 23 namespace pairing_chromeos { | 24 namespace pairing_chromeos { |
| 24 | 25 |
| 25 ProtoDecoder::ProtoDecoder(Observer* observer) | 26 ProtoDecoder::ProtoDecoder(Observer* observer) |
| 26 : observer_(observer), | 27 : observer_(observer), |
| 27 next_message_type_(MESSAGE_NONE), | 28 next_message_type_(MESSAGE_NONE), |
| 28 next_message_size_(0) { | 29 next_message_size_(0) { |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 message.ParseFromArray(&buffer[0], buffer.size()); | 106 message.ParseFromArray(&buffer[0], buffer.size()); |
| 106 observer_->OnErrorMessage(message); | 107 observer_->OnErrorMessage(message); |
| 107 } | 108 } |
| 108 break; | 109 break; |
| 109 case MESSAGE_ADD_NETWORK: { | 110 case MESSAGE_ADD_NETWORK: { |
| 110 pairing_api::AddNetwork message; | 111 pairing_api::AddNetwork message; |
| 111 message.ParseFromArray(&buffer[0], buffer.size()); | 112 message.ParseFromArray(&buffer[0], buffer.size()); |
| 112 observer_->OnAddNetworkMessage(message); | 113 observer_->OnAddNetworkMessage(message); |
| 113 } | 114 } |
| 114 break; | 115 break; |
| 116 case MESSAGE_REBOOT: { |
| 117 pairing_api::Reboot message; |
| 118 message.ParseFromArray(&buffer[0], buffer.size()); |
| 119 observer_->OnRebootMessage(message); |
| 120 } |
| 121 break; |
| 115 | 122 |
| 116 default: | 123 default: |
| 117 LOG(WARNING) << "Skipping unknown message type: " << next_message_type_; | 124 LOG(WARNING) << "Skipping unknown message type: " << next_message_type_; |
| 118 break; | 125 break; |
| 119 } | 126 } |
| 120 | 127 |
| 121 // Reset the message data. | 128 // Reset the message data. |
| 122 next_message_type_ = MESSAGE_NONE; | 129 next_message_type_ = MESSAGE_NONE; |
| 123 next_message_size_ = 0; | 130 next_message_size_ = 0; |
| 124 | 131 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 | 180 |
| 174 ProtoDecoder::IOBufferRefPtr ProtoDecoder::SendError( | 181 ProtoDecoder::IOBufferRefPtr ProtoDecoder::SendError( |
| 175 const pairing_api::Error& message, int* size) { | 182 const pairing_api::Error& message, int* size) { |
| 176 std::string serialized_proto; | 183 std::string serialized_proto; |
| 177 if (!message.SerializeToString(&serialized_proto)) | 184 if (!message.SerializeToString(&serialized_proto)) |
| 178 NOTREACHED(); | 185 NOTREACHED(); |
| 179 | 186 |
| 180 return SendMessage(MESSAGE_ERROR, serialized_proto, size); | 187 return SendMessage(MESSAGE_ERROR, serialized_proto, size); |
| 181 } | 188 } |
| 182 | 189 |
| 190 ProtoDecoder::IOBufferRefPtr ProtoDecoder::SendRebootHost( |
| 191 const pairing_api::Reboot& message, |
| 192 int* size) { |
| 193 std::string serialized_proto; |
| 194 if (!message.SerializeToString(&serialized_proto)) |
| 195 NOTREACHED(); |
| 196 |
| 197 return SendMessage(MESSAGE_REBOOT, serialized_proto, size); |
| 198 } |
| 199 |
| 183 ProtoDecoder::IOBufferRefPtr ProtoDecoder::SendMessage( | 200 ProtoDecoder::IOBufferRefPtr ProtoDecoder::SendMessage( |
| 184 uint8_t message_type, | 201 uint8_t message_type, |
| 185 const std::string& message, | 202 const std::string& message, |
| 186 int* size) { | 203 int* size) { |
| 187 uint16_t message_size = message.size(); | 204 uint16_t message_size = message.size(); |
| 188 | 205 |
| 189 *size = sizeof(message_type) + sizeof(message_size) + message.size(); | 206 *size = sizeof(message_type) + sizeof(message_size) + message.size(); |
| 190 IOBufferRefPtr io_buffer(new net::IOBuffer(*size)); | 207 IOBufferRefPtr io_buffer(new net::IOBuffer(*size)); |
| 191 | 208 |
| 192 // Write the message type. | 209 // Write the message type. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 204 memcpy(&io_buffer->data()[offset], &data, sizeof(data)); | 221 memcpy(&io_buffer->data()[offset], &data, sizeof(data)); |
| 205 offset += sizeof(data); | 222 offset += sizeof(data); |
| 206 | 223 |
| 207 // Write the actual message. | 224 // Write the actual message. |
| 208 memcpy(&io_buffer->data()[offset], message.data(), message.size()); | 225 memcpy(&io_buffer->data()[offset], message.data(), message.size()); |
| 209 | 226 |
| 210 return io_buffer; | 227 return io_buffer; |
| 211 } | 228 } |
| 212 | 229 |
| 213 } // namespace pairing_chromeos | 230 } // namespace pairing_chromeos |
| OLD | NEW |