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/shell/renderer/test_runner/gamepad_controller.h"
7 #include "content/shell/renderer/test_runner/TestInterfaces.h"
8 #include "content/shell/renderer/test_runner/WebTestDelegate.h"
9 #include "gin/arguments.h"
10 #include "gin/handle.h"
11 #include "gin/object_template_builder.h"
12 #include "gin/wrappable.h"
13 #include "third_party/WebKit/public/web/WebFrame.h"
14 #include "third_party/WebKit/public/web/WebKit.h"
15 #include "v8/include/v8.h"
17 using blink::WebFrame;
18 using blink::WebGamepad;
19 using blink::WebGamepads;
23 class GamepadControllerBindings
24 : public gin::Wrappable<GamepadControllerBindings> {
26 static gin::WrapperInfo kWrapperInfo;
28 static void Install(base::WeakPtr<GamepadController> controller,
29 blink::WebFrame* frame);
32 explicit GamepadControllerBindings(
33 base::WeakPtr<GamepadController> controller);
34 virtual ~GamepadControllerBindings();
37 virtual gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
38 v8::Isolate* isolate) OVERRIDE;
40 void Connect(int index);
41 void Disconnect(int index);
42 void SetId(int index, const std::string& src);
43 void SetButtonCount(int index, int buttons);
44 void SetButtonData(int index, int button, double data);
45 void SetAxisCount(int index, int axes);
46 void SetAxisData(int index, int axis, double data);
48 base::WeakPtr<GamepadController> controller_;
50 DISALLOW_COPY_AND_ASSIGN(GamepadControllerBindings);
53 gin::WrapperInfo GamepadControllerBindings::kWrapperInfo = {
54 gin::kEmbedderNativeGin};
57 void GamepadControllerBindings::Install(
58 base::WeakPtr<GamepadController> controller,
60 v8::Isolate* isolate = blink::mainThreadIsolate();
61 v8::HandleScope handle_scope(isolate);
62 v8::Handle<v8::Context> context = frame->mainWorldScriptContext();
63 if (context.IsEmpty())
66 v8::Context::Scope context_scope(context);
68 gin::Handle<GamepadControllerBindings> bindings =
69 gin::CreateHandle(isolate, new GamepadControllerBindings(controller));
70 v8::Handle<v8::Object> global = context->Global();
71 global->Set(gin::StringToV8(isolate, "gamepadController"), bindings.ToV8());
74 GamepadControllerBindings::GamepadControllerBindings(
75 base::WeakPtr<GamepadController> controller)
76 : controller_(controller) {}
78 GamepadControllerBindings::~GamepadControllerBindings() {}
80 gin::ObjectTemplateBuilder GamepadControllerBindings::GetObjectTemplateBuilder(
81 v8::Isolate* isolate) {
82 return gin::Wrappable<GamepadControllerBindings>::GetObjectTemplateBuilder(
84 .SetMethod("connect", &GamepadControllerBindings::Connect)
85 .SetMethod("disconnect", &GamepadControllerBindings::Disconnect)
86 .SetMethod("setId", &GamepadControllerBindings::SetId)
87 .SetMethod("setButtonCount", &GamepadControllerBindings::SetButtonCount)
88 .SetMethod("setButtonData", &GamepadControllerBindings::SetButtonData)
89 .SetMethod("setAxisCount", &GamepadControllerBindings::SetAxisCount)
90 .SetMethod("setAxisData", &GamepadControllerBindings::SetAxisData);
93 void GamepadControllerBindings::Connect(int index) {
95 controller_->Connect(index);
98 void GamepadControllerBindings::Disconnect(int index) {
100 controller_->Disconnect(index);
103 void GamepadControllerBindings::SetId(int index, const std::string& src) {
105 controller_->SetId(index, src);
108 void GamepadControllerBindings::SetButtonCount(int index, int buttons) {
110 controller_->SetButtonCount(index, buttons);
113 void GamepadControllerBindings::SetButtonData(int index,
117 controller_->SetButtonData(index, button, data);
120 void GamepadControllerBindings::SetAxisCount(int index, int axes) {
122 controller_->SetAxisCount(index, axes);
125 void GamepadControllerBindings::SetAxisData(int index, int axis, double data) {
127 controller_->SetAxisData(index, axis, data);
130 GamepadController::GamepadController() : delegate_(NULL), weak_factory_(this) {
134 GamepadController::~GamepadController() {}
136 void GamepadController::Reset() {
137 memset(&gamepads_, 0, sizeof(gamepads_));
140 void GamepadController::Install(WebFrame* frame) {
141 GamepadControllerBindings::Install(weak_factory_.GetWeakPtr(), frame);
144 void GamepadController::SetDelegate(WebTestRunner::WebTestDelegate* delegate) {
145 delegate_ = delegate;
148 void GamepadController::Connect(int index) {
149 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap))
151 gamepads_.items[index].connected = true;
152 gamepads_.length = 0;
153 for (unsigned i = 0; i < WebGamepads::itemsLengthCap; ++i) {
154 if (gamepads_.items[i].connected)
155 gamepads_.length = i + 1;
158 delegate_->setGamepadData(gamepads_);
161 void GamepadController::Disconnect(int index) {
162 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap))
164 gamepads_.items[index].connected = false;
165 gamepads_.length = 0;
166 for (unsigned i = 0; i < WebGamepads::itemsLengthCap; ++i) {
167 if (gamepads_.items[i].connected)
168 gamepads_.length = i + 1;
171 delegate_->setGamepadData(gamepads_);
174 void GamepadController::SetId(int index, const std::string& src) {
175 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap))
177 const char* p = src.c_str();
178 memset(gamepads_.items[index].id, 0, sizeof(gamepads_.items[index].id));
179 for (unsigned i = 0; *p && i < WebGamepad::idLengthCap - 1; ++i)
180 gamepads_.items[index].id[i] = *p++;
182 delegate_->setGamepadData(gamepads_);
185 void GamepadController::SetButtonCount(int index, int buttons) {
186 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap))
188 if (buttons < 0 || buttons >= static_cast<int>(WebGamepad::buttonsLengthCap))
190 gamepads_.items[index].buttonsLength = buttons;
192 delegate_->setGamepadData(gamepads_);
195 void GamepadController::SetButtonData(int index, int button, double data) {
196 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap))
198 if (button < 0 || button >= static_cast<int>(WebGamepad::buttonsLengthCap))
200 gamepads_.items[index].buttons[button] = data;
202 delegate_->setGamepadData(gamepads_);
205 void GamepadController::SetAxisCount(int index, int axes) {
206 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap))
208 if (axes < 0 || axes >= static_cast<int>(WebGamepad::axesLengthCap))
210 gamepads_.items[index].axesLength = axes;
212 delegate_->setGamepadData(gamepads_);
215 void GamepadController::SetAxisData(int index, int axis, double data) {
216 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap))
218 if (axis < 0 || axis >= static_cast<int>(WebGamepad::axesLengthCap))
220 gamepads_.items[index].axes[axis] = data;
222 delegate_->setGamepadData(gamepads_);
225 } // namespace content