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

Side by Side Diff: device/gamepad/public/interfaces/gamepad_struct_traits.cc

Issue 2492183002: Add struct_traits and typemap for blink::WebGamepad (Closed)
Patch Set: avoid pass unnecessary bytes of webgamepad. Created 4 years, 1 month 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 2016 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 "device/gamepad/public/interfaces/gamepad_struct_traits.h"
6
7 namespace mojo {
8
9 // static
10 void StructTraits<
11 device::mojom::GamepadQuaternionDataView,
12 blink::WebGamepadQuaternion>::SetToNull(blink::WebGamepadQuaternion* out) {
13 memset(out, 0, sizeof(blink::WebGamepadQuaternion));
14 out->notNull = false;
15 }
16
17 // static
18 bool StructTraits<device::mojom::GamepadQuaternionDataView,
19 blink::WebGamepadQuaternion>::
20 Read(device::mojom::GamepadQuaternionDataView data,
21 blink::WebGamepadQuaternion* out) {
22 memset(out, 0, sizeof(blink::WebGamepadQuaternion));
yzshen1 2016/11/16 19:20:18 why is this needed?
ke.he 2016/11/17 05:51:48 Done.
23 out->notNull = true;
24 out->x = data.x();
25 out->y = data.y();
26 out->z = data.z();
27 out->w = data.w();
28 return true;
29 }
30
31 // static
32 void StructTraits<device::mojom::GamepadVectorDataView,
33 blink::WebGamepadVector>::SetToNull(blink::WebGamepadVector*
34 out) {
35 memset(out, 0, sizeof(blink::WebGamepadVector));
36 out->notNull = false;
37 }
38
39 // static
40 bool StructTraits<
41 device::mojom::GamepadVectorDataView,
42 blink::WebGamepadVector>::Read(device::mojom::GamepadVectorDataView data,
43 blink::WebGamepadVector* out) {
44 memset(out, 0, sizeof(blink::WebGamepadVector));
yzshen1 2016/11/16 19:20:18 ditto
ke.he 2016/11/17 05:51:48 Done.
45 out->notNull = true;
46 out->x = data.x();
47 out->y = data.y();
48 out->z = data.z();
49 return true;
50 }
51
52 // static
53 bool StructTraits<
54 device::mojom::GamepadButtonDataView,
55 blink::WebGamepadButton>::Read(device::mojom::GamepadButtonDataView data,
56 blink::WebGamepadButton* out) {
57 memset(out, 0, sizeof(blink::WebGamepadButton));
yzshen1 2016/11/16 19:20:18 ditto
ke.he 2016/11/17 05:51:48 Done.
58 out->pressed = data.pressed();
59 out->touched = data.touched();
60 out->value = data.value();
61 return true;
62 }
63
64 // static
65 void StructTraits<device::mojom::GamepadPoseDataView,
66 blink::WebGamepadPose>::SetToNull(blink::WebGamepadPose*
67 out) {
68 memset(out, 0, sizeof(blink::WebGamepadPose));
69 out->notNull = false;
70 }
71
72 // static
73 bool StructTraits<device::mojom::GamepadPoseDataView, blink::WebGamepadPose>::
74 Read(device::mojom::GamepadPoseDataView data, blink::WebGamepadPose* out) {
75 memset(out, 0, sizeof(blink::WebGamepadPose));
yzshen1 2016/11/16 19:20:18 ditto
ke.he 2016/11/17 05:51:49 Done.
76 out->notNull = true;
77 if (!data.ReadOrientation(&out->orientation)) {
78 return false;
79 }
80 out->hasOrientation = out->orientation.notNull;
81
82 if (!data.ReadPosition(&out->position)) {
83 return false;
84 }
85 out->hasPosition = out->position.notNull;
86
87 if (!data.ReadAngularVelocity(&out->angularVelocity)) {
88 return false;
89 }
90 if (!data.ReadLinearVelocity(&out->linearVelocity)) {
91 return false;
92 }
93 if (!data.ReadAngularAcceleration(&out->angularAcceleration)) {
94 return false;
95 }
96 if (!data.ReadLinearAcceleration(&out->linearAcceleration)) {
97 return false;
98 }
99 return true;
100 }
101
102 // static
103 device::mojom::GamepadHand
104 EnumTraits<device::mojom::GamepadHand, blink::WebGamepadHand>::ToMojom(
105 blink::WebGamepadHand input) {
106 switch (input) {
107 case blink::WebGamepadHand::GamepadHandNone:
108 return device::mojom::GamepadHand::GamepadHandNone;
109 case blink::WebGamepadHand::GamepadHandLeft:
110 return device::mojom::GamepadHand::GamepadHandLeft;
111 case blink::WebGamepadHand::GamepadHandRight:
112 return device::mojom::GamepadHand::GamepadHandRight;
113 }
114
115 NOTREACHED();
116 return device::mojom::GamepadHand::GamepadHandNone;
117 }
118
119 // static
120 bool EnumTraits<device::mojom::GamepadHand, blink::WebGamepadHand>::FromMojom(
121 device::mojom::GamepadHand input,
122 blink::WebGamepadHand* output) {
123 switch (input) {
124 case device::mojom::GamepadHand::GamepadHandNone:
125 *output = blink::WebGamepadHand::GamepadHandNone;
126 return true;
127 case device::mojom::GamepadHand::GamepadHandLeft:
128 *output = blink::WebGamepadHand::GamepadHandLeft;
129 return true;
130 case device::mojom::GamepadHand::GamepadHandRight:
131 *output = blink::WebGamepadHand::GamepadHandRight;
132 return true;
133 }
134
135 NOTREACHED();
136 return false;
137 }
138
139 // static
140 ConstCArray<uint16_t>
141 StructTraits<device::mojom::GamepadDataView, blink::WebGamepad>::id(
142 const blink::WebGamepad& r) {
143 size_t id_length = 0;
144 while (r.id[id_length] != 0 && id_length < blink::WebGamepad::idLengthCap) {
yzshen1 2016/11/16 19:20:18 You need to swap the position of the two condition
ke.he 2016/11/17 05:51:48 blundell@, I think it is correct way to check the
blundell 2016/11/17 14:55:41 I'm not a domain expert on Gamepad either, but thi
145 id_length++;
146 }
147 return {id_length, reinterpret_cast<const uint16_t*>(&r.id[0])};
148 }
149
150 // static
151 ConstCArray<uint16_t>
152 StructTraits<device::mojom::GamepadDataView, blink::WebGamepad>::mapping(
153 const blink::WebGamepad& r) {
154 size_t mapping_length = 0;
155 while (r.mapping[mapping_length] != 0 &&
yzshen1 2016/11/16 19:20:18 ditto
ke.he 2016/11/17 05:51:48 Done.
156 mapping_length < blink::WebGamepad::mappingLengthCap) {
157 mapping_length++;
158 }
159 return {mapping_length, reinterpret_cast<const uint16_t*>(&r.mapping[0])};
160 }
161
162 // static
163 bool StructTraits<device::mojom::GamepadDataView, blink::WebGamepad>::Read(
164 device::mojom::GamepadDataView data,
165 blink::WebGamepad* out) {
166 memset(out, 0, sizeof(blink::WebGamepad));
yzshen1 2016/11/16 19:20:18 Does it make sense to only memset those necessary
ke.he 2016/11/17 05:51:48 Yes, it make sense to only memset necessary fields
167 out->connected = data.connected();
168
169 CArray<uint16_t> id = {0, blink::WebGamepad::idLengthCap,
170 reinterpret_cast<uint16_t*>(&out->id[0])};
171 if (!data.ReadId(&id)) {
172 return false;
173 }
174
175 out->timestamp = data.timestamp();
176
177 CArray<double> axes = {0, blink::WebGamepad::axesLengthCap, &out->axes[0]};
178 if (!data.ReadAxes(&axes)) {
179 return false;
180 }
181 // static_cast is safe when "data.ReadAxes(&axes)" above returns true.
182 out->axesLength = static_cast<unsigned>(axes.size);
183
184 CArray<blink::WebGamepadButton> buttons = {
185 0, blink::WebGamepad::buttonsLengthCap, &out->buttons[0]};
186 if (!data.ReadButtons(&buttons)) {
187 return false;
188 }
189 // static_cast is safe when "data.ReadButtons(&buttons)" above returns true.
190 out->buttonsLength = static_cast<unsigned>(buttons.size);
191
192 CArray<uint16_t> mapping = {0, blink::WebGamepad::mappingLengthCap,
193 reinterpret_cast<uint16_t*>(&out->mapping[0])};
194
195 if (!data.ReadMapping(&mapping)) {
196 return false;
197 }
198
199 if (!data.ReadPose(&out->pose)) {
200 return false;
201 }
202
203 blink::WebGamepadHand hand;
204 if (!data.ReadHand(&hand)) {
205 return false;
206 }
207 out->hand = hand;
208
209 out->displayId = data.display_id();
210
211 return true;
212 }
213
214 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698