OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are |
| 6 * met: |
| 7 * |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
| 30 |
| 31 #include "config.h" |
| 32 #include "DeviceMotionDispatcher.h" |
| 33 |
| 34 #include "core/platform/NotImplemented.h" |
| 35 #include "modules/device_orientation/DeviceMotionController.h" |
| 36 #include "modules/device_orientation/DeviceMotionData.h" |
| 37 #include <public/Platform.h> |
| 38 #include <public/WebDeviceMotionData.h> |
| 39 |
| 40 namespace WebCore { |
| 41 |
| 42 DeviceMotionDispatcher& DeviceMotionDispatcher::instance() |
| 43 { |
| 44 DEFINE_STATIC_LOCAL(DeviceMotionDispatcher, deviceMotionDispatcher, ()); |
| 45 return deviceMotionDispatcher; |
| 46 } |
| 47 |
| 48 DeviceMotionDispatcher::DeviceMotionDispatcher() |
| 49 { |
| 50 } |
| 51 |
| 52 DeviceMotionDispatcher::~DeviceMotionDispatcher() |
| 53 { |
| 54 } |
| 55 |
| 56 void DeviceMotionDispatcher::addController(DeviceMotionController* controller) |
| 57 { |
| 58 bool wasEmpty = m_controllers.isEmpty(); |
| 59 if (!m_controllers.contains(controller)) |
| 60 m_controllers.append(controller); |
| 61 if (wasEmpty) |
| 62 startListening(); |
| 63 } |
| 64 |
| 65 void DeviceMotionDispatcher::removeController(DeviceMotionController* controller
) |
| 66 { |
| 67 // Do not actually remove controller from the vector, instead zero them out. |
| 68 // The zeros are removed after didChangeDeviceMotion has dispatched all even
ts. |
| 69 // This is to prevent re-entrancy case when a controller is destroyed while
in the |
| 70 // didChangeDeviceMotion method. |
| 71 size_t index = m_controllers.find(controller); |
| 72 if (index != notFound) |
| 73 m_controllers[index] = 0; |
| 74 } |
| 75 |
| 76 void DeviceMotionDispatcher::startListening() |
| 77 { |
| 78 WebKit::Platform::current()->setDeviceMotionListener(this); |
| 79 } |
| 80 |
| 81 void DeviceMotionDispatcher::stopListening() |
| 82 { |
| 83 WebKit::Platform::current()->setDeviceMotionListener(0); |
| 84 } |
| 85 |
| 86 void DeviceMotionDispatcher::didChangeDeviceMotion(const WebKit::WebDeviceMotion
Data& motion) |
| 87 { |
| 88 m_lastDeviceMotionData = DeviceMotionData::create(motion); |
| 89 bool needsPurge = false; |
| 90 for (size_t i = 0; i < m_controllers.size(); ++i) { |
| 91 if (m_controllers[i]) |
| 92 m_controllers[i]->didChangeDeviceMotion(m_lastDeviceMotionData.get()
); |
| 93 else |
| 94 needsPurge = true; |
| 95 } |
| 96 |
| 97 if (needsPurge) |
| 98 purgeControllers(); |
| 99 } |
| 100 |
| 101 DeviceMotionData* DeviceMotionDispatcher::latestDeviceMotionData() |
| 102 { |
| 103 return m_lastDeviceMotionData.get(); |
| 104 } |
| 105 |
| 106 void DeviceMotionDispatcher::purgeControllers() |
| 107 { |
| 108 size_t i = 0; |
| 109 while (i < m_controllers.size()) { |
| 110 if (!m_controllers[i]) { |
| 111 m_controllers[i] = m_controllers.last(); |
| 112 m_controllers.removeLast(); |
| 113 } else |
| 114 ++i; |
| 115 } |
| 116 |
| 117 if (m_controllers.isEmpty()) |
| 118 stopListening(); |
| 119 } |
| 120 |
| 121 } // namespace WebCore |
OLD | NEW |