OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2014 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 | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
11 * copyright notice, this list of conditions and the following disclaimer | 11 * copyright notice, this list of conditions and the following disclaimer |
12 * in the documentation and/or other materials provided with the | 12 * in the documentation and/or other materials provided with the |
13 * distribution. | 13 * distribution. |
14 * * Neither the name of Google Inc. nor the names of its | 14 * * Neither the name of Google Inc. nor the names of its |
15 * contributors may be used to endorse or promote products derived from | 15 * contributors may be used to endorse or promote products derived from |
16 * this software without specific prior written permission. | 16 * this software without specific prior written permission. |
17 * | 17 * |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
32 #include "core/frame/DeviceSensorEventDispatcher.h" | 32 #include "core/frame/DeviceEventDispatcherBase.h" |
| 33 |
| 34 #include "core/frame/DeviceEventControllerBase.h" |
| 35 #include "wtf/TemporaryChange.h" |
33 | 36 |
34 namespace WebCore { | 37 namespace WebCore { |
35 | 38 |
36 DeviceSensorEventDispatcher::DeviceSensorEventDispatcher() | 39 DeviceEventDispatcherBase::DeviceEventDispatcherBase() |
37 : m_needsPurge(false) | 40 : m_needsPurge(false) |
38 , m_isDispatching(false) | 41 , m_isDispatching(false) |
39 { | 42 { |
40 } | 43 } |
41 | 44 |
42 DeviceSensorEventDispatcher::~DeviceSensorEventDispatcher() | 45 DeviceEventDispatcherBase::~DeviceEventDispatcherBase() |
43 { | 46 { |
44 } | 47 } |
45 | 48 |
46 void DeviceSensorEventDispatcher::addController(DeviceSensorEventController* con
troller) | 49 void DeviceEventDispatcherBase::addController(DeviceEventControllerBase* control
ler) |
47 { | 50 { |
48 bool wasEmpty = m_controllers.isEmpty(); | 51 bool wasEmpty = m_controllers.isEmpty(); |
49 if (!m_controllers.contains(controller)) | 52 if (!m_controllers.contains(controller)) |
50 m_controllers.append(controller); | 53 m_controllers.append(controller); |
51 if (wasEmpty) | 54 if (wasEmpty) |
52 startListening(); | 55 startListening(); |
53 } | 56 } |
54 | 57 |
55 void DeviceSensorEventDispatcher::removeController(DeviceSensorEventController*
controller) | 58 void DeviceEventDispatcherBase::removeController(DeviceEventControllerBase* cont
roller) |
56 { | 59 { |
57 // Do not actually remove the controller from the vector, instead zero them
out. | 60 // Do not actually remove the controller from the vector, instead zero them
out. |
58 // The zeros are removed in these two cases: | 61 // The zeros are removed in these two cases: |
59 // 1. either immediately if we are not dispatching any events, | 62 // 1. either immediately if we are not dispatching any events, |
60 // 2. or after events to all controllers have dispatched | 63 // 2. or after events to all controllers have dispatched (see notifyControll
ers()). |
61 // (see e.g. DeviceOrientationDispatcher::didChangeDeviceOrientation). | |
62 // This is to correctly handle the re-entrancy case when a controller is des
troyed | 64 // This is to correctly handle the re-entrancy case when a controller is des
troyed |
63 // while the events are still being dispatched. | 65 // while the events are still being dispatched. |
64 size_t index = m_controllers.find(controller); | 66 size_t index = m_controllers.find(controller); |
65 if (index == kNotFound) | 67 if (index == kNotFound) |
66 return; | 68 return; |
67 | 69 |
68 m_controllers[index] = 0; | 70 m_controllers[index] = 0; |
69 m_needsPurge = true; | 71 m_needsPurge = true; |
70 | 72 |
71 if (!m_isDispatching) | 73 if (!m_isDispatching) |
72 purgeControllers(); | 74 purgeControllers(); |
73 } | 75 } |
74 | 76 |
75 void DeviceSensorEventDispatcher::purgeControllers() | 77 void DeviceEventDispatcherBase::purgeControllers() |
76 { | 78 { |
77 ASSERT(m_needsPurge); | 79 ASSERT(m_needsPurge); |
78 | 80 |
79 size_t i = 0; | 81 size_t i = 0; |
80 while (i < m_controllers.size()) { | 82 while (i < m_controllers.size()) { |
81 if (!m_controllers[i]) { | 83 if (!m_controllers[i]) { |
82 m_controllers[i] = m_controllers.last(); | 84 m_controllers[i] = m_controllers.last(); |
83 m_controllers.removeLast(); | 85 m_controllers.removeLast(); |
84 } else { | 86 } else { |
85 ++i; | 87 ++i; |
86 } | 88 } |
87 } | 89 } |
88 | 90 |
89 m_needsPurge = false; | 91 m_needsPurge = false; |
90 | 92 |
91 if (m_controllers.isEmpty()) | 93 if (m_controllers.isEmpty()) |
92 stopListening(); | 94 stopListening(); |
93 } | 95 } |
94 | 96 |
| 97 void DeviceEventDispatcherBase::notifyControllers() |
| 98 { |
| 99 { |
| 100 TemporaryChange<bool> changeIsDispatching(m_isDispatching, true); |
| 101 // Don't notify controllers removed or added during event dispatch. |
| 102 size_t size = m_controllers.size(); |
| 103 for (size_t i = 0; i < size; ++i) { |
| 104 if (m_controllers[i]) |
| 105 m_controllers[i]->didUpdateData(); |
| 106 } |
| 107 } |
| 108 |
| 109 if (m_needsPurge) |
| 110 purgeControllers(); |
| 111 } |
| 112 |
95 } // namespace WebCore | 113 } // namespace WebCore |
OLD | NEW |