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

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

Issue 2146343002: Revert of Remove DOMWindowProperty::willDetachGlobalObjectFromFrame Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 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
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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 sampleGamepads<Gamepad>(m_gamepads.get()); 99 sampleGamepads<Gamepad>(m_gamepads.get());
100 return m_gamepads.get(); 100 return m_gamepads.get();
101 } 101 }
102 102
103 DEFINE_TRACE(NavigatorGamepad) 103 DEFINE_TRACE(NavigatorGamepad)
104 { 104 {
105 visitor->trace(m_gamepads); 105 visitor->trace(m_gamepads);
106 visitor->trace(m_pendingEvents); 106 visitor->trace(m_pendingEvents);
107 visitor->trace(m_dispatchOneEventRunner); 107 visitor->trace(m_dispatchOneEventRunner);
108 Supplement<Navigator>::trace(visitor); 108 Supplement<Navigator>::trace(visitor);
109 ContextLifecycleObserver::trace(visitor); 109 DOMWindowProperty::trace(visitor);
110 PlatformEventController::trace(visitor); 110 PlatformEventController::trace(visitor);
111 } 111 }
112 112
113 bool NavigatorGamepad::startUpdatingIfAttached() 113 bool NavigatorGamepad::startUpdatingIfAttached()
114 { 114 {
115 Document* document = static_cast<Document*>(getExecutionContext());
116 // The frame must be attached to start updating. 115 // The frame must be attached to start updating.
117 if (document && document->frame()) { 116 if (frame() && frame()->host()) {
118 startUpdating(); 117 startUpdating();
119 return true; 118 return true;
120 } 119 }
121 return false; 120 return false;
122 } 121 }
123 122
124 void NavigatorGamepad::didUpdateData() 123 void NavigatorGamepad::didUpdateData()
125 { 124 {
126 // We should stop listening once we detached. 125 // We should stop listening once we detached.
127 Document* document = static_cast<Document*>(getExecutionContext()); 126 ASSERT(frame());
128 DCHECK(document->frame()); 127 ASSERT(frame()->domWindow());
129 DCHECK(document->frame()->domWindow());
130 128
131 // We register to the dispatcher before sampling gamepads so we need to chec k if we actually have an event listener. 129 // We register to the dispatcher before sampling gamepads so we need to chec k if we actually have an event listener.
132 if (!m_hasEventListener) 130 if (!m_hasEventListener)
133 return; 131 return;
134 132
133 Document* document = frame()->domWindow()->document();
135 if (document->activeDOMObjectsAreStopped() || document->activeDOMObjectsAreS uspended()) 134 if (document->activeDOMObjectsAreStopped() || document->activeDOMObjectsAreS uspended())
136 return; 135 return;
137 136
138 const GamepadDispatcher::ConnectionChange& change = GamepadDispatcher::insta nce().latestConnectionChange(); 137 const GamepadDispatcher::ConnectionChange& change = GamepadDispatcher::insta nce().latestConnectionChange();
139 138
140 if (!m_gamepads) 139 if (!m_gamepads)
141 m_gamepads = GamepadList::create(); 140 m_gamepads = GamepadList::create();
142 141
143 Gamepad* gamepad = m_gamepads->item(change.index); 142 Gamepad* gamepad = m_gamepads->item(change.index);
144 if (!gamepad) 143 if (!gamepad)
145 gamepad = Gamepad::create(); 144 gamepad = Gamepad::create();
146 sampleGamepad(change.index, *gamepad, change.pad); 145 sampleGamepad(change.index, *gamepad, change.pad);
147 m_gamepads->set(change.index, gamepad); 146 m_gamepads->set(change.index, gamepad);
148 147
149 m_pendingEvents.append(gamepad); 148 m_pendingEvents.append(gamepad);
150 m_dispatchOneEventRunner->runAsync(); 149 m_dispatchOneEventRunner->runAsync();
151 } 150 }
152 151
153 void NavigatorGamepad::dispatchOneEvent() 152 void NavigatorGamepad::dispatchOneEvent()
154 { 153 {
155 Document* document = static_cast<Document*>(getExecutionContext()); 154 ASSERT(frame());
156 DCHECK(document->frame()); 155 ASSERT(frame()->domWindow());
157 DCHECK(document->frame()->domWindow()); 156 ASSERT(!m_pendingEvents.isEmpty());
158 DCHECK(!m_pendingEvents.isEmpty());
159 157
160 Gamepad* gamepad = m_pendingEvents.takeFirst(); 158 Gamepad* gamepad = m_pendingEvents.takeFirst();
161 const AtomicString& eventName = gamepad->connected() ? EventTypeNames::gamep adconnected : EventTypeNames::gamepaddisconnected; 159 const AtomicString& eventName = gamepad->connected() ? EventTypeNames::gamep adconnected : EventTypeNames::gamepaddisconnected;
162 document->frame()->domWindow()->dispatchEvent(GamepadEvent::create(eventName , false, true, gamepad)); 160 frame()->domWindow()->dispatchEvent(GamepadEvent::create(eventName, false, t rue, gamepad));
163 161
164 if (!m_pendingEvents.isEmpty()) 162 if (!m_pendingEvents.isEmpty())
165 m_dispatchOneEventRunner->runAsync(); 163 m_dispatchOneEventRunner->runAsync();
166 } 164 }
167 165
168 NavigatorGamepad::NavigatorGamepad(LocalFrame* frame) 166 NavigatorGamepad::NavigatorGamepad(LocalFrame* frame)
169 : ContextLifecycleObserver(frame->document()) 167 : DOMWindowProperty(frame)
170 , PlatformEventController(frame ? frame->page() : 0) 168 , PlatformEventController(frame ? frame->page() : 0)
171 , m_dispatchOneEventRunner(AsyncMethodRunner<NavigatorGamepad>::create(this, &NavigatorGamepad::dispatchOneEvent)) 169 , m_dispatchOneEventRunner(AsyncMethodRunner<NavigatorGamepad>::create(this, &NavigatorGamepad::dispatchOneEvent))
172 { 170 {
173 if (frame) 171 if (frame)
174 frame->localDOMWindow()->registerEventListenerObserver(this); 172 frame->localDOMWindow()->registerEventListenerObserver(this);
175 } 173 }
176 174
177 NavigatorGamepad::~NavigatorGamepad() 175 NavigatorGamepad::~NavigatorGamepad()
178 { 176 {
179 } 177 }
180 178
181 const char* NavigatorGamepad::supplementName() 179 const char* NavigatorGamepad::supplementName()
182 { 180 {
183 return "NavigatorGamepad"; 181 return "NavigatorGamepad";
184 } 182 }
185 183
186 void NavigatorGamepad::contextDestroyed() 184 void NavigatorGamepad::willDestroyGlobalObjectInFrame()
187 { 185 {
188 stopUpdating(); 186 stopUpdating();
187 DOMWindowProperty::willDestroyGlobalObjectInFrame();
188 }
189
190 void NavigatorGamepad::willDetachGlobalObjectFromFrame()
191 {
192 stopUpdating();
193 DOMWindowProperty::willDetachGlobalObjectFromFrame();
189 } 194 }
190 195
191 void NavigatorGamepad::registerWithDispatcher() 196 void NavigatorGamepad::registerWithDispatcher()
192 { 197 {
193 GamepadDispatcher::instance().addController(this); 198 GamepadDispatcher::instance().addController(this);
194 m_dispatchOneEventRunner->resume(); 199 m_dispatchOneEventRunner->resume();
195 } 200 }
196 201
197 void NavigatorGamepad::unregisterWithDispatcher() 202 void NavigatorGamepad::unregisterWithDispatcher()
198 { 203 {
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 stopUpdating(); 256 stopUpdating();
252 257
253 if (!visible || !m_hasEventListener) 258 if (!visible || !m_hasEventListener)
254 return; 259 return;
255 260
256 // Tell the page what has changed. m_gamepads contains the state before we b ecame hidden. 261 // Tell the page what has changed. m_gamepads contains the state before we b ecame hidden.
257 // We create a new snapshot and compare them. 262 // We create a new snapshot and compare them.
258 GamepadList* oldGamepads = m_gamepads.release(); 263 GamepadList* oldGamepads = m_gamepads.release();
259 gamepads(); 264 gamepads();
260 GamepadList* newGamepads = m_gamepads.get(); 265 GamepadList* newGamepads = m_gamepads.get();
261 DCHECK(newGamepads); 266 ASSERT(newGamepads);
262 267
263 for (unsigned i = 0; i < WebGamepads::itemsLengthCap; ++i) { 268 for (unsigned i = 0; i < WebGamepads::itemsLengthCap; ++i) {
264 Gamepad* oldGamepad = oldGamepads ? oldGamepads->item(i) : 0; 269 Gamepad* oldGamepad = oldGamepads ? oldGamepads->item(i) : 0;
265 Gamepad* newGamepad = newGamepads->item(i); 270 Gamepad* newGamepad = newGamepads->item(i);
266 bool oldWasConnected = oldGamepad && oldGamepad->connected(); 271 bool oldWasConnected = oldGamepad && oldGamepad->connected();
267 bool newIsConnected = newGamepad && newGamepad->connected(); 272 bool newIsConnected = newGamepad && newGamepad->connected();
268 bool connectedGamepadChanged = oldWasConnected && newIsConnected && oldG amepad->id() != newGamepad->id(); 273 bool connectedGamepadChanged = oldWasConnected && newIsConnected && oldG amepad->id() != newGamepad->id();
269 if (connectedGamepadChanged || (oldWasConnected && !newIsConnected)) { 274 if (connectedGamepadChanged || (oldWasConnected && !newIsConnected)) {
270 oldGamepad->setConnected(false); 275 oldGamepad->setConnected(false);
271 m_pendingEvents.append(oldGamepad); 276 m_pendingEvents.append(oldGamepad);
272 } 277 }
273 if (connectedGamepadChanged || (!oldWasConnected && newIsConnected)) { 278 if (connectedGamepadChanged || (!oldWasConnected && newIsConnected)) {
274 m_pendingEvents.append(newGamepad); 279 m_pendingEvents.append(newGamepad);
275 } 280 }
276 } 281 }
277 282
278 if (!m_pendingEvents.isEmpty()) 283 if (!m_pendingEvents.isEmpty())
279 m_dispatchOneEventRunner->runAsync(); 284 m_dispatchOneEventRunner->runAsync();
280 } 285 }
281 286
282 } // namespace blink 287 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698