Chromium Code Reviews| Index: remoting/host/security_key/security_key_message.cc |
| diff --git a/remoting/host/security_key/security_key_message.cc b/remoting/host/security_key/security_key_message.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9c02821eb81091f9efda6d09f3643454eb174109 |
| --- /dev/null |
| +++ b/remoting/host/security_key/security_key_message.cc |
| @@ -0,0 +1,85 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "remoting/host/security_key/security_key_message.h" |
| + |
| +#include "base/logging.h" |
| + |
| +namespace { |
| + |
| +// Limit remote security key messages to 256KB. |
| +const uint32_t kMaxSecurityKeyMessageByteCount = 256 * 1024; |
| + |
| +const int kRemoteSecurityKeyMessageControlCodeByteCount = 1; |
| + |
| +} // namespace |
| + |
| +namespace remoting { |
| + |
| +SecurityKeyMessage::SecurityKeyMessage() {} |
| + |
| +SecurityKeyMessage::~SecurityKeyMessage() {} |
| + |
| +bool SecurityKeyMessage::IsValidMessageSize(uint32_t message_size) { |
| + return message_size > 0 && message_size <= kMaxSecurityKeyMessageByteCount; |
| +} |
| + |
| +RemoteSecurityKeyMessageType SecurityKeyMessage::MessageTypeFromValue( |
| + int value) { |
| + // Note: The static_cast from enum value to int should be safe since the enum |
| + // type is an unsigned 8bit value. |
| + switch (value) { |
|
Sergey Ulanov
2016/03/30 21:21:50
I think you can also format this code as follows:
joedow
2016/03/30 21:46:10
Done.
|
| + case static_cast<int>(RemoteSecurityKeyMessageType::CONNECT): |
| + return RemoteSecurityKeyMessageType::CONNECT; |
| + |
| + case static_cast<int>(RemoteSecurityKeyMessageType::CONNECT_RESPONSE): |
| + return RemoteSecurityKeyMessageType::CONNECT_RESPONSE; |
| + |
| + case static_cast<int>(RemoteSecurityKeyMessageType::CONNECT_ERROR): |
| + return RemoteSecurityKeyMessageType::CONNECT_ERROR; |
| + |
| + case static_cast<int>(RemoteSecurityKeyMessageType::REQUEST): |
| + return RemoteSecurityKeyMessageType::REQUEST; |
| + |
| + case static_cast<int>(RemoteSecurityKeyMessageType::REQUEST_RESPONSE): |
| + return RemoteSecurityKeyMessageType::REQUEST_RESPONSE; |
| + |
| + case static_cast<int>(RemoteSecurityKeyMessageType::REQUEST_ERROR): |
| + return RemoteSecurityKeyMessageType::REQUEST_ERROR; |
| + |
| + case static_cast<int>(RemoteSecurityKeyMessageType::UNKNOWN_COMMAND): |
| + return RemoteSecurityKeyMessageType::UNKNOWN_COMMAND; |
| + |
| + case static_cast<int>(RemoteSecurityKeyMessageType::UNKNOWN_ERROR): |
| + return RemoteSecurityKeyMessageType::UNKNOWN_ERROR; |
| + |
| + case static_cast<int>(RemoteSecurityKeyMessageType::INVALID): |
| + return RemoteSecurityKeyMessageType::INVALID; |
| + |
| + default: |
| + LOG(ERROR) << "Unknown message type passed in: " << value; |
| + return RemoteSecurityKeyMessageType::INVALID; |
| + } |
| +} |
| + |
| +bool SecurityKeyMessage::ParseMessage(const std::string& message_data) { |
| + if (!IsValidMessageSize(message_data.size())) { |
| + return false; |
| + } |
| + |
| + // The first char of the message is the message type. |
| + type_ = MessageTypeFromValue(message_data[0]); |
| + if (type_ == RemoteSecurityKeyMessageType::INVALID) { |
| + return false; |
| + } |
| + |
| + payload_.clear(); |
| + if (message_data.size() > kRemoteSecurityKeyMessageControlCodeByteCount) { |
| + payload_ = message_data.substr(1); |
| + } |
| + |
| + return true; |
| +} |
| + |
| +} // namespace remoting |