Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(99)

Side by Side Diff: content/common/gamepad_param_traits.cc

Issue 195873019: Gamepad API: add support for connection events (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add WebGamepad to the list of ok POD deps to fix presubmit Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 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.
4
5 #include "content/common/gamepad_param_traits.h"
6
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"
12
13 using blink::WebGamepad;
14
15 namespace {
16
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];
25 }
26 log->append(base::UTF16ToUTF8(utf16));
27 }
28
29 }
30
31 namespace IPC {
32
33 void ParamTraits<WebGamepad>::Write(
34 Message* m,
35 const WebGamepad& p) {
36 m->WriteBool(p.connected);
37 m->WriteData(reinterpret_cast<const char*>(p.id), sizeof(p.id));
38 m->WriteUInt64(p.timestamp);
39 m->WriteUInt32(p.axesLength);
40 m->WriteData(reinterpret_cast<const char*>(p.axes), sizeof(p.axes));
41 m->WriteUInt32(p.buttonsLength);
42 m->WriteData(reinterpret_cast<const char*>(p.buttons), sizeof(p.buttons));
43 m->WriteData(reinterpret_cast<const char*>(p.mapping), sizeof(p.mapping));
44 }
45
46 bool ParamTraits<WebGamepad>::Read(
47 const Message* m,
48 PickleIterator* iter,
49 WebGamepad* p) {
50 if (!m->ReadBool(iter, &p->connected)) return false;
51
52 int length;
53 const char* data;
54 if (!m->ReadData(iter, &data, &length) || length != sizeof(p->id))
55 return false;
56 memcpy(p->id, data, length);
57
58 COMPILE_ASSERT(sizeof(p->timestamp) == sizeof(uint64),
59 timestamp_cannot_encoded_as_uint64);
60 if (!m->ReadUInt64(iter, reinterpret_cast<uint64*>(&p->timestamp)))
61 return false;
62 if (!m->ReadUInt32(iter, &p->axesLength)) return false;
63 if (!m->ReadData(iter, &data, &length) || length != sizeof(p->axes))
64 return false;
65 memcpy(p->axes, data, length);
66 if (!m->ReadUInt32(iter, &p->buttonsLength)) return false;
67 if (!m->ReadData(iter, &data, &length) || length != sizeof(p->buttons))
68 return false;
69 memcpy(p->buttons, data, length);
70 if (!m->ReadData(iter, &data, &length) || length != sizeof(p->mapping))
71 return false;
72 memcpy(p->mapping, data, length);
73
74 return true;
75 }
76
77 void ParamTraits<WebGamepad>::Log(
78 const WebGamepad& p,
79 std::string* l) {
80 l->append("WebGamepad(");
81 LogParam(p.connected, l);
82 LogWebUCharString(p.id, WebGamepad::idLengthCap, l);
83 l->append(",");
84 LogWebUCharString(p.mapping, WebGamepad::mappingLengthCap, l);
85 l->append(",");
86 LogParam(p.timestamp, l);
87 l->append(",");
88 LogParam(p.axesLength, l);
89 l->append(", [");
90 for (size_t i = 0; i < arraysize(p.axes); ++i) {
91 l->append(base::StringPrintf("%f%s", p.axes[i],
92 i < (arraysize(p.axes) - 1) ? ", " : "], "));
93 }
94 LogParam(p.buttonsLength, l);
95 l->append(", [");
96 for (size_t i = 0; i < arraysize(p.buttons); ++i) {
97 l->append(base::StringPrintf("(%u, %f)%s",
98 p.buttons[i].pressed, p.buttons[i].value,
99 i < (arraysize(p.buttons) - 1) ? ", " : "], "));
100 }
101 l->append(")");
102 }
103
104 } // namespace IPC
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698