Chromium Code Reviews| Index: content/common/gamepad_connection_event_message_params.cc |
| diff --git a/content/common/gamepad_connection_event_message_params.cc b/content/common/gamepad_connection_event_message_params.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fbaf45165a32d207b136ad44141ef8686fd571bd |
| --- /dev/null |
| +++ b/content/common/gamepad_connection_event_message_params.cc |
| @@ -0,0 +1,68 @@ |
| +// Copyright 2014 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 "content/common/gamepad_connection_event_message_params.h" |
| + |
| +#include "base/logging.h" |
| + |
| +namespace content { |
| + |
| +namespace { |
| + |
| +template <size_t N> |
| +size_t StringLength(const blink::WebUChar web_string[N]) { |
|
bajones
2014/03/27 18:56:49
Suprised that we don't have a common way to do thi
|
| + size_t i = 0; |
| + while (web_string[i] != 0) ++i; |
| + DCHECK(i < N); |
| + return i; |
| +} |
| + |
| +} |
| + |
| +GamepadConnectionEventMessageParams::GamepadConnectionEventMessageParams() |
| + : index(-1) { |
| +} |
| + |
| +GamepadConnectionEventMessageParams::~GamepadConnectionEventMessageParams() { |
| +} |
| + |
| +GamepadConnectionEventMessageParams::GamepadConnectionEventMessageParams( |
| + int index, const blink::WebGamepad& gamepad) |
| + : index(index), |
| + timestamp(gamepad.timestamp), |
| + axes_length(gamepad.axesLength), |
| + buttons_length(gamepad.buttonsLength), |
| + connected(gamepad.connected) { |
| + size_t length = StringLength<blink::WebGamepad::idLengthCap>(gamepad.id); |
| + const blink::WebUChar* characters = &gamepad.id[0]; |
| + id_characters = std::vector<blink::WebUChar>(characters, |
| + characters + length); |
| + |
| + length = StringLength<blink::WebGamepad::mappingLengthCap>(gamepad.mapping); |
| + characters = &gamepad.mapping[0]; |
| + mapping_characters = std::vector<blink::WebUChar>(characters, |
| + characters + length); |
| +} |
| + |
| +void GamepadConnectionEventMessageParams::GetWebGamepad( |
| + blink::WebGamepad* gamepad) const { |
| + DCHECK(index >= 0); |
| + DCHECK(id_characters.size() < (blink::WebGamepad::idLengthCap - 1)); |
| + |
| + size_t num_bytes = id_characters.size() * sizeof(blink::WebUChar); |
| + memcpy(&gamepad->id[0], &id_characters[0], num_bytes); |
| + gamepad->id[id_characters.size()] = 0; |
| + |
| + DCHECK(mapping_characters.size() < (blink::WebGamepad::mappingLengthCap - 1)); |
| + num_bytes = mapping_characters.size() * sizeof(blink::WebUChar); |
| + memcpy(&gamepad->mapping[0], &mapping_characters[0], num_bytes); |
| + gamepad->mapping[mapping_characters.size()] = 0; |
| + |
| + gamepad->timestamp = timestamp; |
| + gamepad->axesLength = axes_length; |
| + gamepad->buttonsLength = buttons_length; |
| + gamepad->connected = connected; |
| +} |
| + |
| +} |