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