Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / common / gamepad_param_traits.cc
blobcbc7e6aa088a3efe733411186c58fc3ee48a9f90
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
3 // found in the LICENSE file.
5 #include "content/common/gamepad_param_traits.h"
7 #include "base/pickle.h"
8 #include "base/strings/string16.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "ipc/ipc_message_utils.h"
11 #include "third_party/WebKit/public/platform/WebGamepad.h"
13 using blink::WebGamepad;
15 namespace {
17 void LogWebUCharString(
18 const blink::WebUChar web_string[],
19 const size_t array_size,
20 std::string* log) {
21 base::string16 utf16;
22 utf16.reserve(array_size);
23 for (size_t i = 0; i < array_size && web_string[i]; ++i) {
24 utf16[i] = web_string[i];
26 log->append(base::UTF16ToUTF8(utf16));
31 namespace IPC {
33 void ParamTraits<WebGamepad>::Write(
34 Message* m,
35 const WebGamepad& p) {
36 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(WebGamepad));
39 bool ParamTraits<WebGamepad>::Read(const Message* m,
40 base::PickleIterator* iter,
41 WebGamepad* p) {
42 int length;
43 const char* data;
44 if (!iter->ReadData(&data, &length) || length != sizeof(WebGamepad))
45 return false;
46 memcpy(p, data, sizeof(WebGamepad));
48 return true;
51 void ParamTraits<WebGamepad>::Log(
52 const WebGamepad& p,
53 std::string* l) {
54 l->append("WebGamepad(");
55 LogParam(p.connected, l);
56 LogWebUCharString(p.id, WebGamepad::idLengthCap, l);
57 l->append(",");
58 LogWebUCharString(p.mapping, WebGamepad::mappingLengthCap, l);
59 l->append(",");
60 LogParam(p.timestamp, l);
61 l->append(",");
62 LogParam(p.axesLength, l);
63 l->append(", [");
64 for (size_t i = 0; i < arraysize(p.axes); ++i) {
65 l->append(base::StringPrintf("%f%s", p.axes[i],
66 i < (arraysize(p.axes) - 1) ? ", " : "], "));
68 LogParam(p.buttonsLength, l);
69 l->append(", [");
70 for (size_t i = 0; i < arraysize(p.buttons); ++i) {
71 l->append(base::StringPrintf("(%u, %f)%s",
72 p.buttons[i].pressed, p.buttons[i].value,
73 i < (arraysize(p.buttons) - 1) ? ", " : "], "));
75 l->append(")");
78 } // namespace IPC