Convert raw pointers to scoped_ptr in net module.
[chromium-blink-merge.git] / content / shell / renderer / test_runner / gamepad_controller.cc
blobc3ecd407b4c66bf30092eb496f6ec329af868920
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/web_test_delegate.h"
8 #include "gin/arguments.h"
9 #include "gin/handle.h"
10 #include "gin/object_template_builder.h"
11 #include "gin/wrappable.h"
12 #include "third_party/WebKit/public/platform/WebGamepadListener.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;
21 namespace content {
23 class GamepadControllerBindings
24 : public gin::Wrappable<GamepadControllerBindings> {
25 public:
26 static gin::WrapperInfo kWrapperInfo;
28 static void Install(base::WeakPtr<GamepadController> controller,
29 blink::WebFrame* frame);
31 private:
32 explicit GamepadControllerBindings(
33 base::WeakPtr<GamepadController> controller);
34 ~GamepadControllerBindings() override;
36 // gin::Wrappable.
37 gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
38 v8::Isolate* isolate) override;
40 void Connect(int index);
41 void DispatchConnected(int index);
42 void Disconnect(int index);
43 void SetId(int index, const std::string& src);
44 void SetButtonCount(int index, int buttons);
45 void SetButtonData(int index, int button, double data);
46 void SetAxisCount(int index, int axes);
47 void SetAxisData(int index, int axis, double data);
49 base::WeakPtr<GamepadController> controller_;
51 DISALLOW_COPY_AND_ASSIGN(GamepadControllerBindings);
54 gin::WrapperInfo GamepadControllerBindings::kWrapperInfo = {
55 gin::kEmbedderNativeGin};
57 // static
58 void GamepadControllerBindings::Install(
59 base::WeakPtr<GamepadController> controller,
60 WebFrame* frame) {
61 v8::Isolate* isolate = blink::mainThreadIsolate();
62 v8::HandleScope handle_scope(isolate);
63 v8::Local<v8::Context> context = frame->mainWorldScriptContext();
64 if (context.IsEmpty())
65 return;
67 v8::Context::Scope context_scope(context);
69 gin::Handle<GamepadControllerBindings> bindings =
70 gin::CreateHandle(isolate, new GamepadControllerBindings(controller));
71 if (bindings.IsEmpty())
72 return;
73 v8::Local<v8::Object> global = context->Global();
74 global->Set(gin::StringToV8(isolate, "gamepadController"), bindings.ToV8());
77 GamepadControllerBindings::GamepadControllerBindings(
78 base::WeakPtr<GamepadController> controller)
79 : controller_(controller) {}
81 GamepadControllerBindings::~GamepadControllerBindings() {}
83 gin::ObjectTemplateBuilder GamepadControllerBindings::GetObjectTemplateBuilder(
84 v8::Isolate* isolate) {
85 return gin::Wrappable<GamepadControllerBindings>::GetObjectTemplateBuilder(
86 isolate)
87 .SetMethod("connect", &GamepadControllerBindings::Connect)
88 .SetMethod("dispatchConnected", &GamepadControllerBindings::DispatchConnected)
89 .SetMethod("disconnect", &GamepadControllerBindings::Disconnect)
90 .SetMethod("setId", &GamepadControllerBindings::SetId)
91 .SetMethod("setButtonCount", &GamepadControllerBindings::SetButtonCount)
92 .SetMethod("setButtonData", &GamepadControllerBindings::SetButtonData)
93 .SetMethod("setAxisCount", &GamepadControllerBindings::SetAxisCount)
94 .SetMethod("setAxisData", &GamepadControllerBindings::SetAxisData);
97 void GamepadControllerBindings::Connect(int index) {
98 if (controller_)
99 controller_->Connect(index);
102 void GamepadControllerBindings::DispatchConnected(int index) {
103 if (controller_)
104 controller_->DispatchConnected(index);
107 void GamepadControllerBindings::Disconnect(int index) {
108 if (controller_)
109 controller_->Disconnect(index);
112 void GamepadControllerBindings::SetId(int index, const std::string& src) {
113 if (controller_)
114 controller_->SetId(index, src);
117 void GamepadControllerBindings::SetButtonCount(int index, int buttons) {
118 if (controller_)
119 controller_->SetButtonCount(index, buttons);
122 void GamepadControllerBindings::SetButtonData(int index,
123 int button,
124 double data) {
125 if (controller_)
126 controller_->SetButtonData(index, button, data);
129 void GamepadControllerBindings::SetAxisCount(int index, int axes) {
130 if (controller_)
131 controller_->SetAxisCount(index, axes);
134 void GamepadControllerBindings::SetAxisData(int index, int axis, double data) {
135 if (controller_)
136 controller_->SetAxisData(index, axis, data);
139 // static
140 base::WeakPtr<GamepadController> GamepadController::Create(
141 WebTestDelegate* delegate) {
142 CHECK(delegate);
144 GamepadController* controller = new GamepadController();
145 delegate->SetGamepadProvider(controller);
146 return controller->weak_factory_.GetWeakPtr();
149 GamepadController::GamepadController()
150 : listener_(nullptr), weak_factory_(this) {
151 Reset();
154 GamepadController::~GamepadController() {
157 void GamepadController::Reset() {
158 memset(&gamepads_, 0, sizeof(gamepads_));
161 void GamepadController::Install(WebFrame* frame) {
162 GamepadControllerBindings::Install(weak_factory_.GetWeakPtr(), frame);
165 void GamepadController::SampleGamepads(blink::WebGamepads& gamepads) {
166 memcpy(&gamepads, &gamepads_, sizeof(blink::WebGamepads));
169 void GamepadController::SetListener(blink::WebGamepadListener* listener) {
170 listener_ = listener;
173 void GamepadController::Connect(int index) {
174 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap))
175 return;
176 gamepads_.items[index].connected = true;
177 gamepads_.length = 0;
178 for (unsigned i = 0; i < WebGamepads::itemsLengthCap; ++i) {
179 if (gamepads_.items[i].connected)
180 gamepads_.length = i + 1;
184 void GamepadController::DispatchConnected(int index) {
185 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)
186 || !gamepads_.items[index].connected)
187 return;
188 const WebGamepad& pad = gamepads_.items[index];
189 if (listener_)
190 listener_->didConnectGamepad(index, pad);
193 void GamepadController::Disconnect(int index) {
194 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap))
195 return;
196 WebGamepad& pad = gamepads_.items[index];
197 pad.connected = false;
198 gamepads_.length = 0;
199 for (unsigned i = 0; i < WebGamepads::itemsLengthCap; ++i) {
200 if (gamepads_.items[i].connected)
201 gamepads_.length = i + 1;
203 if (listener_)
204 listener_->didDisconnectGamepad(index, pad);
207 void GamepadController::SetId(int index, const std::string& src) {
208 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap))
209 return;
210 const char* p = src.c_str();
211 memset(gamepads_.items[index].id, 0, sizeof(gamepads_.items[index].id));
212 for (unsigned i = 0; *p && i < WebGamepad::idLengthCap - 1; ++i)
213 gamepads_.items[index].id[i] = *p++;
216 void GamepadController::SetButtonCount(int index, int buttons) {
217 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap))
218 return;
219 if (buttons < 0 || buttons >= static_cast<int>(WebGamepad::buttonsLengthCap))
220 return;
221 gamepads_.items[index].buttonsLength = buttons;
224 void GamepadController::SetButtonData(int index, int button, double data) {
225 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap))
226 return;
227 if (button < 0 || button >= static_cast<int>(WebGamepad::buttonsLengthCap))
228 return;
229 gamepads_.items[index].buttons[button].value = data;
230 gamepads_.items[index].buttons[button].pressed = data > 0.1f;
233 void GamepadController::SetAxisCount(int index, int axes) {
234 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap))
235 return;
236 if (axes < 0 || axes >= static_cast<int>(WebGamepad::axesLengthCap))
237 return;
238 gamepads_.items[index].axesLength = axes;
241 void GamepadController::SetAxisData(int index, int axis, double data) {
242 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap))
243 return;
244 if (axis < 0 || axis >= static_cast<int>(WebGamepad::axesLengthCap))
245 return;
246 gamepads_.items[index].axes[axis] = data;
249 } // namespace content