Chromium Code Reviews| 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: { | |
|
xdai1
2017/05/23 00:07:50
the indent is off
| |
| 117 pairing_api::Reboot message; | |
| 118 message.ParseFromArray(&buffer[0], buffer.size()); | |
| 119 observer_->OnRebootMessage(message); | |
| 120 } break; | |
| 115 | 121 |
| 116 default: | 122 default: |
| 117 LOG(WARNING) << "Skipping unknown message type: " << next_message_type_; | 123 LOG(WARNING) << "Skipping unknown message type: " << next_message_type_; |
| 118 break; | 124 break; |
| 119 } | 125 } |
| 120 | 126 |
| 121 // Reset the message data. | 127 // Reset the message data. |
| 122 next_message_type_ = MESSAGE_NONE; | 128 next_message_type_ = MESSAGE_NONE; |
| 123 next_message_size_ = 0; | 129 next_message_size_ = 0; |
| 124 | 130 |
| 125 return true; | 131 return true; |
| 126 } | 132 } |
| 127 | 133 |
| 128 ProtoDecoder::IOBufferRefPtr ProtoDecoder::SendHostStatus( | 134 ProtoDecoder::IOBufferRefPtr ProtoDecoder::SendHostStatus( |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 173 | 179 |
| 174 ProtoDecoder::IOBufferRefPtr ProtoDecoder::SendError( | 180 ProtoDecoder::IOBufferRefPtr ProtoDecoder::SendError( |
| 175 const pairing_api::Error& message, int* size) { | 181 const pairing_api::Error& message, int* size) { |
| 176 std::string serialized_proto; | 182 std::string serialized_proto; |
| 177 if (!message.SerializeToString(&serialized_proto)) | 183 if (!message.SerializeToString(&serialized_proto)) |
| 178 NOTREACHED(); | 184 NOTREACHED(); |
| 179 | 185 |
| 180 return SendMessage(MESSAGE_ERROR, serialized_proto, size); | 186 return SendMessage(MESSAGE_ERROR, serialized_proto, size); |
| 181 } | 187 } |
| 182 | 188 |
| 189 ProtoDecoder::IOBufferRefPtr ProtoDecoder::SendRebootHost( | |
| 190 const pairing_api::Reboot& message, | |
| 191 int* size) { | |
| 192 std::string serialized_proto; | |
| 193 if (!message.SerializeToString(&serialized_proto)) | |
| 194 NOTREACHED(); | |
| 195 | |
| 196 return SendMessage(MESSAGE_REBOOT, serialized_proto, size); | |
| 197 } | |
| 198 | |
| 183 ProtoDecoder::IOBufferRefPtr ProtoDecoder::SendMessage( | 199 ProtoDecoder::IOBufferRefPtr ProtoDecoder::SendMessage( |
| 184 uint8_t message_type, | 200 uint8_t message_type, |
| 185 const std::string& message, | 201 const std::string& message, |
| 186 int* size) { | 202 int* size) { |
| 187 uint16_t message_size = message.size(); | 203 uint16_t message_size = message.size(); |
| 188 | 204 |
| 189 *size = sizeof(message_type) + sizeof(message_size) + message.size(); | 205 *size = sizeof(message_type) + sizeof(message_size) + message.size(); |
| 190 IOBufferRefPtr io_buffer(new net::IOBuffer(*size)); | 206 IOBufferRefPtr io_buffer(new net::IOBuffer(*size)); |
| 191 | 207 |
| 192 // Write the message type. | 208 // Write the message type. |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 204 memcpy(&io_buffer->data()[offset], &data, sizeof(data)); | 220 memcpy(&io_buffer->data()[offset], &data, sizeof(data)); |
| 205 offset += sizeof(data); | 221 offset += sizeof(data); |
| 206 | 222 |
| 207 // Write the actual message. | 223 // Write the actual message. |
| 208 memcpy(&io_buffer->data()[offset], message.data(), message.size()); | 224 memcpy(&io_buffer->data()[offset], message.data(), message.size()); |
| 209 | 225 |
| 210 return io_buffer; | 226 return io_buffer; |
| 211 } | 227 } |
| 212 | 228 |
| 213 } // namespace pairing_chromeos | 229 } // namespace pairing_chromeos |
| OLD | NEW |