OLD | NEW |
(Empty) | |
| 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 "config.h" |
| 6 #include "modules/screen_orientation/ScreenOrientationDispatcher.h" |
| 7 |
| 8 #include "modules/screen_orientation/ScreenOrientationController.h" |
| 9 #include "public/platform/Platform.h" |
| 10 #include "wtf/TemporaryChange.h" |
| 11 |
| 12 namespace WebCore { |
| 13 |
| 14 ScreenOrientationDispatcher& ScreenOrientationDispatcher::instance() |
| 15 { |
| 16 DEFINE_STATIC_LOCAL(ScreenOrientationDispatcher, screenOrientationDispatcher
, ()); |
| 17 return screenOrientationDispatcher; |
| 18 } |
| 19 |
| 20 ScreenOrientationDispatcher::ScreenOrientationDispatcher() |
| 21 : m_needsPurge(false) |
| 22 , m_isDispatching(false) |
| 23 { |
| 24 } |
| 25 |
| 26 void ScreenOrientationDispatcher::addController(ScreenOrientationController* con
troller) |
| 27 { |
| 28 bool wasEmpty = m_controllers.isEmpty(); |
| 29 if (!m_controllers.contains(controller)) |
| 30 m_controllers.append(controller); |
| 31 if (wasEmpty) |
| 32 startListening(); |
| 33 } |
| 34 |
| 35 void ScreenOrientationDispatcher::removeController(ScreenOrientationController*
controller) |
| 36 { |
| 37 // Do not actually remove the controllers from the vector, instead zero them
out. |
| 38 // The zeros are removed in these two cases: |
| 39 // 1. either immediately if we are not dispatching any events, |
| 40 // 2. or after didChangeScreenOrientation has dispatched all events. |
| 41 // This is to correctly handle the re-entrancy case when a controller is des
troyed |
| 42 // while in the didChangeScreenOrientation() method. |
| 43 size_t index = m_controllers.find(controller); |
| 44 if (index == kNotFound) |
| 45 return; |
| 46 |
| 47 m_controllers[index] = 0; |
| 48 m_needsPurge = true; |
| 49 |
| 50 if (!m_isDispatching) |
| 51 purgeControllers(); |
| 52 } |
| 53 |
| 54 void ScreenOrientationDispatcher::purgeControllers() |
| 55 { |
| 56 ASSERT(m_needsPurge); |
| 57 |
| 58 size_t i = 0; |
| 59 while (i < m_controllers.size()) { |
| 60 if (!m_controllers[i]) { |
| 61 m_controllers[i] = m_controllers.last(); |
| 62 m_controllers.removeLast(); |
| 63 } else { |
| 64 ++i; |
| 65 } |
| 66 } |
| 67 |
| 68 m_needsPurge = false; |
| 69 |
| 70 if (m_controllers.isEmpty()) |
| 71 stopListening(); |
| 72 } |
| 73 |
| 74 void ScreenOrientationDispatcher::didChangeScreenOrientation(blink::WebScreenOri
entation orientation) |
| 75 { |
| 76 { |
| 77 TemporaryChange<bool> changeIsDispatching(m_isDispatching, true); |
| 78 // Don't fire controllers removed or added during event dispatch. |
| 79 size_t size = m_controllers.size(); |
| 80 for (size_t i = 0; i < size; ++i) { |
| 81 if (m_controllers[i]) |
| 82 static_cast<ScreenOrientationController*>(m_controllers[i])->did
ChangeScreenOrientation(orientation); |
| 83 } |
| 84 } |
| 85 |
| 86 if (m_needsPurge) |
| 87 purgeControllers(); |
| 88 } |
| 89 |
| 90 void ScreenOrientationDispatcher::startListening() |
| 91 { |
| 92 blink::Platform::current()->setScreenOrientationListener(this); |
| 93 } |
| 94 |
| 95 void ScreenOrientationDispatcher::stopListening() |
| 96 { |
| 97 blink::Platform::current()->setScreenOrientationListener(0); |
| 98 } |
| 99 |
| 100 } // namespace WebCore |
OLD | NEW |