OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "components/test_runner/gamepad_controller.h" | |
6 | |
7 #include <string.h> | |
8 | |
9 #include "base/macros.h" | |
10 #include "components/test_runner/web_test_delegate.h" | |
11 #include "gin/arguments.h" | |
12 #include "gin/handle.h" | |
13 #include "gin/object_template_builder.h" | |
14 #include "gin/wrappable.h" | |
15 #include "third_party/WebKit/public/platform/WebGamepadListener.h" | |
16 #include "third_party/WebKit/public/web/WebFrame.h" | |
17 #include "third_party/WebKit/public/web/WebKit.h" | |
18 #include "v8/include/v8.h" | |
19 | |
20 using blink::WebFrame; | |
21 using blink::WebGamepad; | |
22 using blink::WebGamepads; | |
23 | |
24 namespace test_runner { | |
25 | |
26 class GamepadControllerBindings | |
27 : public gin::Wrappable<GamepadControllerBindings> { | |
28 public: | |
29 static gin::WrapperInfo kWrapperInfo; | |
30 | |
31 static void Install(base::WeakPtr<GamepadController> controller, | |
32 blink::WebFrame* frame); | |
33 | |
34 private: | |
35 explicit GamepadControllerBindings( | |
36 base::WeakPtr<GamepadController> controller); | |
37 ~GamepadControllerBindings() override; | |
38 | |
39 // gin::Wrappable. | |
40 gin::ObjectTemplateBuilder GetObjectTemplateBuilder( | |
41 v8::Isolate* isolate) override; | |
42 | |
43 void Connect(int index); | |
44 void DispatchConnected(int index); | |
45 void Disconnect(int index); | |
46 void SetId(int index, const std::string& src); | |
47 void SetButtonCount(int index, int buttons); | |
48 void SetButtonData(int index, int button, double data); | |
49 void SetAxisCount(int index, int axes); | |
50 void SetAxisData(int index, int axis, double data); | |
51 | |
52 base::WeakPtr<GamepadController> controller_; | |
53 | |
54 DISALLOW_COPY_AND_ASSIGN(GamepadControllerBindings); | |
55 }; | |
56 | |
57 gin::WrapperInfo GamepadControllerBindings::kWrapperInfo = { | |
58 gin::kEmbedderNativeGin}; | |
59 | |
60 // static | |
61 void GamepadControllerBindings::Install( | |
62 base::WeakPtr<GamepadController> controller, | |
63 WebFrame* frame) { | |
64 v8::Isolate* isolate = blink::mainThreadIsolate(); | |
65 v8::HandleScope handle_scope(isolate); | |
66 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); | |
67 if (context.IsEmpty()) | |
68 return; | |
69 | |
70 v8::Context::Scope context_scope(context); | |
71 | |
72 gin::Handle<GamepadControllerBindings> bindings = | |
73 gin::CreateHandle(isolate, new GamepadControllerBindings(controller)); | |
74 if (bindings.IsEmpty()) | |
75 return; | |
76 v8::Local<v8::Object> global = context->Global(); | |
77 global->Set(gin::StringToV8(isolate, "gamepadController"), bindings.ToV8()); | |
78 } | |
79 | |
80 GamepadControllerBindings::GamepadControllerBindings( | |
81 base::WeakPtr<GamepadController> controller) | |
82 : controller_(controller) {} | |
83 | |
84 GamepadControllerBindings::~GamepadControllerBindings() {} | |
85 | |
86 gin::ObjectTemplateBuilder GamepadControllerBindings::GetObjectTemplateBuilder( | |
87 v8::Isolate* isolate) { | |
88 return gin::Wrappable<GamepadControllerBindings>::GetObjectTemplateBuilder( | |
89 isolate) | |
90 .SetMethod("connect", &GamepadControllerBindings::Connect) | |
91 .SetMethod("dispatchConnected", | |
92 &GamepadControllerBindings::DispatchConnected) | |
93 .SetMethod("disconnect", &GamepadControllerBindings::Disconnect) | |
94 .SetMethod("setId", &GamepadControllerBindings::SetId) | |
95 .SetMethod("setButtonCount", &GamepadControllerBindings::SetButtonCount) | |
96 .SetMethod("setButtonData", &GamepadControllerBindings::SetButtonData) | |
97 .SetMethod("setAxisCount", &GamepadControllerBindings::SetAxisCount) | |
98 .SetMethod("setAxisData", &GamepadControllerBindings::SetAxisData); | |
99 } | |
100 | |
101 void GamepadControllerBindings::Connect(int index) { | |
102 if (controller_) | |
103 controller_->Connect(index); | |
104 } | |
105 | |
106 void GamepadControllerBindings::DispatchConnected(int index) { | |
107 if (controller_) | |
108 controller_->DispatchConnected(index); | |
109 } | |
110 | |
111 void GamepadControllerBindings::Disconnect(int index) { | |
112 if (controller_) | |
113 controller_->Disconnect(index); | |
114 } | |
115 | |
116 void GamepadControllerBindings::SetId(int index, const std::string& src) { | |
117 if (controller_) | |
118 controller_->SetId(index, src); | |
119 } | |
120 | |
121 void GamepadControllerBindings::SetButtonCount(int index, int buttons) { | |
122 if (controller_) | |
123 controller_->SetButtonCount(index, buttons); | |
124 } | |
125 | |
126 void GamepadControllerBindings::SetButtonData(int index, | |
127 int button, | |
128 double data) { | |
129 if (controller_) | |
130 controller_->SetButtonData(index, button, data); | |
131 } | |
132 | |
133 void GamepadControllerBindings::SetAxisCount(int index, int axes) { | |
134 if (controller_) | |
135 controller_->SetAxisCount(index, axes); | |
136 } | |
137 | |
138 void GamepadControllerBindings::SetAxisData(int index, int axis, double data) { | |
139 if (controller_) | |
140 controller_->SetAxisData(index, axis, data); | |
141 } | |
142 | |
143 // static | |
144 base::WeakPtr<GamepadController> GamepadController::Create( | |
145 WebTestDelegate* delegate) { | |
146 CHECK(delegate); | |
147 | |
148 GamepadController* controller = new GamepadController(); | |
149 delegate->SetGamepadProvider(controller); | |
150 return controller->weak_factory_.GetWeakPtr(); | |
151 } | |
152 | |
153 GamepadController::GamepadController() | |
154 : listener_(nullptr), weak_factory_(this) { | |
155 Reset(); | |
156 } | |
157 | |
158 GamepadController::~GamepadController() { | |
159 } | |
160 | |
161 void GamepadController::Reset() { | |
162 memset(&gamepads_, 0, sizeof(gamepads_)); | |
163 } | |
164 | |
165 void GamepadController::Install(WebFrame* frame) { | |
166 GamepadControllerBindings::Install(weak_factory_.GetWeakPtr(), frame); | |
167 } | |
168 | |
169 void GamepadController::SampleGamepads(blink::WebGamepads& gamepads) { | |
170 memcpy(&gamepads, &gamepads_, sizeof(blink::WebGamepads)); | |
171 } | |
172 | |
173 void GamepadController::SetListener(blink::WebGamepadListener* listener) { | |
174 listener_ = listener; | |
175 } | |
176 | |
177 void GamepadController::Connect(int index) { | |
178 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) | |
179 return; | |
180 gamepads_.items[index].connected = true; | |
181 } | |
182 | |
183 void GamepadController::DispatchConnected(int index) { | |
184 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap) | |
185 || !gamepads_.items[index].connected) | |
186 return; | |
187 const WebGamepad& pad = gamepads_.items[index]; | |
188 if (listener_) | |
189 listener_->didConnectGamepad(index, pad); | |
190 } | |
191 | |
192 void GamepadController::Disconnect(int index) { | |
193 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) | |
194 return; | |
195 WebGamepad& pad = gamepads_.items[index]; | |
196 pad.connected = false; | |
197 if (listener_) | |
198 listener_->didDisconnectGamepad(index, pad); | |
199 } | |
200 | |
201 void GamepadController::SetId(int index, const std::string& src) { | |
202 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) | |
203 return; | |
204 const char* p = src.c_str(); | |
205 memset(gamepads_.items[index].id, 0, sizeof(gamepads_.items[index].id)); | |
206 for (unsigned i = 0; *p && i < WebGamepad::idLengthCap - 1; ++i) | |
207 gamepads_.items[index].id[i] = *p++; | |
208 } | |
209 | |
210 void GamepadController::SetButtonCount(int index, int buttons) { | |
211 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) | |
212 return; | |
213 if (buttons < 0 || buttons >= static_cast<int>(WebGamepad::buttonsLengthCap)) | |
214 return; | |
215 gamepads_.items[index].buttonsLength = buttons; | |
216 } | |
217 | |
218 void GamepadController::SetButtonData(int index, int button, double data) { | |
219 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) | |
220 return; | |
221 if (button < 0 || button >= static_cast<int>(WebGamepad::buttonsLengthCap)) | |
222 return; | |
223 gamepads_.items[index].buttons[button].value = data; | |
224 gamepads_.items[index].buttons[button].pressed = data > 0.1f; | |
225 } | |
226 | |
227 void GamepadController::SetAxisCount(int index, int axes) { | |
228 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) | |
229 return; | |
230 if (axes < 0 || axes >= static_cast<int>(WebGamepad::axesLengthCap)) | |
231 return; | |
232 gamepads_.items[index].axesLength = axes; | |
233 } | |
234 | |
235 void GamepadController::SetAxisData(int index, int axis, double data) { | |
236 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) | |
237 return; | |
238 if (axis < 0 || axis >= static_cast<int>(WebGamepad::axesLengthCap)) | |
239 return; | |
240 gamepads_.items[index].axes[axis] = data; | |
241 } | |
242 | |
243 } // namespace test_runner | |
OLD | NEW |