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

Side by Side Diff: Source/modules/gamepad/NavigatorGamepad.cpp

Issue 256593010: Gracefully support Navigator Gamepad methods in a detached state. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Have document supplements keep a reference to their document Created 6 years, 7 months 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
« no previous file with comments | « Source/modules/gamepad/NavigatorGamepad.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011, Google Inc. All rights reserved. 2 * Copyright (C) 2011, Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met: 5 * modification, are permitted provided that the following conditions are met:
6 * 6 *
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 if (!document.frame() || !document.frame()->domWindow()) 77 if (!document.frame() || !document.frame()->domWindow())
78 return 0; 78 return 0;
79 Navigator& navigator = document.frame()->domWindow()->navigator(); 79 Navigator& navigator = document.frame()->domWindow()->navigator();
80 return &from(navigator); 80 return &from(navigator);
81 } 81 }
82 82
83 NavigatorGamepad& NavigatorGamepad::from(Navigator& navigator) 83 NavigatorGamepad& NavigatorGamepad::from(Navigator& navigator)
84 { 84 {
85 NavigatorGamepad* supplement = static_cast<NavigatorGamepad*>(WillBeHeapSupp lement<Navigator>::from(navigator, supplementName())); 85 NavigatorGamepad* supplement = static_cast<NavigatorGamepad*>(WillBeHeapSupp lement<Navigator>::from(navigator, supplementName()));
86 if (!supplement) { 86 if (!supplement) {
87 supplement = new NavigatorGamepad(*navigator.frame()->document()); 87 supplement = new NavigatorGamepad(navigator.frame());
88 provideTo(navigator, supplementName(), adoptPtrWillBeNoop(supplement)); 88 provideTo(navigator, supplementName(), adoptPtrWillBeNoop(supplement));
89 } 89 }
90 return *supplement; 90 return *supplement;
91 } 91 }
92 92
93 WebKitGamepadList* NavigatorGamepad::webkitGetGamepads(Navigator& navigator) 93 WebKitGamepadList* NavigatorGamepad::webkitGetGamepads(Navigator& navigator)
94 { 94 {
95 return NavigatorGamepad::from(navigator).webkitGamepads(); 95 return NavigatorGamepad::from(navigator).webkitGamepads();
96 } 96 }
97 97
98 GamepadList* NavigatorGamepad::getGamepads(Navigator& navigator) 98 GamepadList* NavigatorGamepad::getGamepads(Navigator& navigator)
99 { 99 {
100 return NavigatorGamepad::from(navigator).gamepads(); 100 return NavigatorGamepad::from(navigator).gamepads();
101 } 101 }
102 102
103 WebKitGamepadList* NavigatorGamepad::webkitGamepads() 103 WebKitGamepadList* NavigatorGamepad::webkitGamepads()
104 { 104 {
105 startUpdating();
106 if (!m_webkitGamepads) 105 if (!m_webkitGamepads)
107 m_webkitGamepads = WebKitGamepadList::create(); 106 m_webkitGamepads = WebKitGamepadList::create();
108 sampleGamepads<WebKitGamepad>(m_webkitGamepads.get()); 107 if (window()) {
108 startUpdating();
109 sampleGamepads<WebKitGamepad>(m_webkitGamepads.get());
110 }
109 return m_webkitGamepads.get(); 111 return m_webkitGamepads.get();
110 } 112 }
111 113
112 GamepadList* NavigatorGamepad::gamepads() 114 GamepadList* NavigatorGamepad::gamepads()
113 { 115 {
114 startUpdating();
115 if (!m_gamepads) 116 if (!m_gamepads)
116 m_gamepads = GamepadList::create(); 117 m_gamepads = GamepadList::create();
117 sampleGamepads<Gamepad>(m_gamepads.get()); 118 if (window()) {
119 startUpdating();
120 sampleGamepads<Gamepad>(m_gamepads.get());
121 }
118 return m_gamepads.get(); 122 return m_gamepads.get();
119 } 123 }
120 124
121 void NavigatorGamepad::trace(Visitor* visitor) 125 void NavigatorGamepad::trace(Visitor* visitor)
122 { 126 {
123 visitor->trace(m_gamepads); 127 visitor->trace(m_gamepads);
124 visitor->trace(m_webkitGamepads); 128 visitor->trace(m_webkitGamepads);
125 } 129 }
126 130
127 void NavigatorGamepad::didConnectOrDisconnectGamepad(unsigned index, const blink ::WebGamepad& webGamepad, bool connected) 131 void NavigatorGamepad::didConnectOrDisconnectGamepad(unsigned index, const blink ::WebGamepad& webGamepad, bool connected)
(...skipping 18 matching lines...) Expand all
146 if (!gamepad) 150 if (!gamepad)
147 gamepad = Gamepad::create(); 151 gamepad = Gamepad::create();
148 sampleGamepad(index, *gamepad, webGamepad); 152 sampleGamepad(index, *gamepad, webGamepad);
149 m_gamepads->set(index, gamepad); 153 m_gamepads->set(index, gamepad);
150 154
151 const AtomicString& eventName = connected ? EventTypeNames::gamepadconnected : EventTypeNames::gamepaddisconnected; 155 const AtomicString& eventName = connected ? EventTypeNames::gamepadconnected : EventTypeNames::gamepaddisconnected;
152 RefPtrWillBeRawPtr<GamepadEvent> event = GamepadEvent::create(eventName, fal se, true, gamepad); 156 RefPtrWillBeRawPtr<GamepadEvent> event = GamepadEvent::create(eventName, fal se, true, gamepad);
153 window()->dispatchEvent(event); 157 window()->dispatchEvent(event);
154 } 158 }
155 159
156 NavigatorGamepad::NavigatorGamepad(Document& document) 160 NavigatorGamepad::NavigatorGamepad(LocalFrame* frame)
157 : DOMWindowProperty(document.frame()) 161 : DOMWindowProperty(frame)
158 , DeviceSensorEventController(document) 162 , DeviceSensorEventController(frame ? frame->page() : 0)
159 , DOMWindowLifecycleObserver(document.frame()->domWindow()) 163 , DOMWindowLifecycleObserver(frame ? frame->domWindow() : 0)
160 { 164 {
161 } 165 }
162 166
163 NavigatorGamepad::~NavigatorGamepad() 167 NavigatorGamepad::~NavigatorGamepad()
164 { 168 {
165 } 169 }
166 170
167 const char* NavigatorGamepad::supplementName() 171 const char* NavigatorGamepad::supplementName()
168 { 172 {
169 return "NavigatorGamepad"; 173 return "NavigatorGamepad";
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 return nullptr; 208 return nullptr;
205 } 209 }
206 210
207 bool NavigatorGamepad::isNullEvent(Event*) 211 bool NavigatorGamepad::isNullEvent(Event*)
208 { 212 {
209 // This is called only when hasLastData() is true. 213 // This is called only when hasLastData() is true.
210 ASSERT_NOT_REACHED(); 214 ASSERT_NOT_REACHED();
211 return false; 215 return false;
212 } 216 }
213 217
218 Document* NavigatorGamepad::document()
219 {
220 return window() ? window()->document() : 0;
221 }
222
223 static bool isGamepadEvent(const AtomicString& eventType)
224 {
225 return eventType == EventTypeNames::gamepadconnected || eventType == EventTy peNames::gamepaddisconnected;
226 }
227
214 void NavigatorGamepad::didAddEventListener(DOMWindow*, const AtomicString& event Type) 228 void NavigatorGamepad::didAddEventListener(DOMWindow*, const AtomicString& event Type)
215 { 229 {
216 if (RuntimeEnabledFeatures::gamepadEnabled() && (eventType == EventTypeNames ::gamepadconnected || eventType == EventTypeNames::gamepaddisconnected)) { 230 if (RuntimeEnabledFeatures::gamepadEnabled() && isGamepadEvent(eventType)) {
217 if (page() && page()->visibilityState() == PageVisibilityStateVisible) 231 if (page() && page()->visibilityState() == PageVisibilityStateVisible)
218 startUpdating(); 232 startUpdating();
219 m_hasEventListener = true; 233 m_hasEventListener = true;
220 } 234 }
221 } 235 }
222 236
223 void NavigatorGamepad::didRemoveEventListener(DOMWindow*, const AtomicString& ev entType) 237 void NavigatorGamepad::didRemoveEventListener(DOMWindow*, const AtomicString& ev entType)
224 { 238 {
225 if (eventType == EventTypeNames::gamepadconnected || eventType == EventTypeN ames::gamepaddisconnected) 239 if (isGamepadEvent(eventType))
226 m_hasEventListener = false; 240 m_hasEventListener = false;
227 } 241 }
228 242
229 void NavigatorGamepad::didRemoveAllEventListeners(DOMWindow*) 243 void NavigatorGamepad::didRemoveAllEventListeners(DOMWindow*)
230 { 244 {
231 m_hasEventListener = false; 245 m_hasEventListener = false;
232 } 246 }
233 247
234 } // namespace WebCore 248 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/modules/gamepad/NavigatorGamepad.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698