Index: content/common/gamepad_param_traits.cc |
diff --git a/content/common/gamepad_param_traits.cc b/content/common/gamepad_param_traits.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..89e2bc2dc275c0fb842213194eb8518b297472f2 |
--- /dev/null |
+++ b/content/common/gamepad_param_traits.cc |
@@ -0,0 +1,104 @@ |
+// Copyright (c) 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_param_traits.h" |
+ |
+#include "base/pickle.h" |
+#include "base/strings/string16.h" |
+#include "base/strings/utf_string_conversions.h" |
+#include "ipc/ipc_message_utils.h" |
+#include "third_party/WebKit/public/platform/WebGamepad.h" |
+ |
+using blink::WebGamepad; |
+ |
+namespace { |
+ |
+void LogWebUCharString( |
+ const blink::WebUChar web_string[], |
+ const size_t array_size, |
+ std::string* log) { |
+ base::string16 utf16; |
+ utf16.reserve(array_size); |
+ for (size_t i = 0; i < array_size && web_string[i]; ++i) { |
+ utf16[i] = web_string[i]; |
+ } |
+ log->append(base::UTF16ToUTF8(utf16)); |
+} |
+ |
+} |
+ |
+namespace IPC { |
+ |
+void ParamTraits<WebGamepad>::Write( |
+ Message* m, |
+ const WebGamepad& p) { |
+ m->WriteBool(p.connected); |
+ m->WriteData(reinterpret_cast<const char*>(p.id), sizeof(p.id)); |
+ m->WriteUInt64(p.timestamp); |
+ m->WriteUInt32(p.axesLength); |
+ m->WriteData(reinterpret_cast<const char*>(p.axes), sizeof(p.axes)); |
+ m->WriteUInt32(p.buttonsLength); |
+ m->WriteData(reinterpret_cast<const char*>(p.buttons), sizeof(p.buttons)); |
+ m->WriteData(reinterpret_cast<const char*>(p.mapping), sizeof(p.mapping)); |
+} |
+ |
+bool ParamTraits<WebGamepad>::Read( |
+ const Message* m, |
+ PickleIterator* iter, |
+ WebGamepad* p) { |
+ if (!m->ReadBool(iter, &p->connected)) return false; |
+ |
+ int length; |
+ const char* data; |
+ if (!m->ReadData(iter, &data, &length) || length != sizeof(p->id)) |
+ return false; |
+ memcpy(p->id, data, length); |
+ |
+ COMPILE_ASSERT(sizeof(p->timestamp) == sizeof(uint64), |
+ timestamp_cannot_encoded_as_uint64); |
+ if (!m->ReadUInt64(iter, reinterpret_cast<uint64*>(&p->timestamp))) |
+ return false; |
+ if (!m->ReadUInt32(iter, &p->axesLength)) return false; |
+ if (!m->ReadData(iter, &data, &length) || length != sizeof(p->axes)) |
+ return false; |
+ memcpy(p->axes, data, length); |
+ if (!m->ReadUInt32(iter, &p->buttonsLength)) return false; |
+ if (!m->ReadData(iter, &data, &length) || length != sizeof(p->buttons)) |
+ return false; |
+ memcpy(p->buttons, data, length); |
+ if (!m->ReadData(iter, &data, &length) || length != sizeof(p->mapping)) |
+ return false; |
+ memcpy(p->mapping, data, length); |
+ |
+ return true; |
+} |
+ |
+void ParamTraits<WebGamepad>::Log( |
+ const WebGamepad& p, |
+ std::string* l) { |
+ l->append("WebGamepad("); |
+ LogParam(p.connected, l); |
+ LogWebUCharString(p.id, WebGamepad::idLengthCap, l); |
+ l->append(","); |
+ LogWebUCharString(p.mapping, WebGamepad::mappingLengthCap, l); |
+ l->append(","); |
+ LogParam(p.timestamp, l); |
+ l->append(","); |
+ LogParam(p.axesLength, l); |
+ l->append(", ["); |
+ for (size_t i = 0; i < arraysize(p.axes); ++i) { |
+ l->append(base::StringPrintf("%f%s", p.axes[i], |
+ i < (arraysize(p.axes) - 1) ? ", " : "], ")); |
+ } |
+ LogParam(p.buttonsLength, l); |
+ l->append(", ["); |
+ for (size_t i = 0; i < arraysize(p.buttons); ++i) { |
+ l->append(base::StringPrintf("(%u, %f)%s", |
+ p.buttons[i].pressed, p.buttons[i].value, |
+ i < (arraysize(p.buttons) - 1) ? ", " : "], ")); |
+ } |
+ l->append(")"); |
+} |
+ |
+} // namespace IPC |