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 #if ENABLE(OILPAN) | |
17 DEFINE_STATIC_LOCAL(Persistent<ScreenOrientationDispatcher>, screenOrientati
onDispatcher, (new ScreenOrientationDispatcher())); | |
18 return *screenOrientationDispatcher; | |
19 #else | |
20 DEFINE_STATIC_LOCAL(ScreenOrientationDispatcher, screenOrientationDispatcher
, ()); | |
21 return screenOrientationDispatcher; | |
22 #endif | |
23 } | |
24 | |
25 ScreenOrientationDispatcher::ScreenOrientationDispatcher() | |
26 #if !ENABLE(OILPAN) | |
27 : m_needsPurge(false) | |
28 , m_isDispatching(false) | |
29 #endif | |
30 { | |
31 } | |
32 | |
33 void ScreenOrientationDispatcher::addController(ScreenOrientationController* con
troller) | |
34 { | |
35 bool wasEmpty = m_controllers.isEmpty(); | |
36 #if ENABLE(OILPAN) | |
37 m_controllers.add(controller); | |
38 #else | |
39 if (!m_controllers.contains(controller)) | |
40 m_controllers.append(controller); | |
41 #endif | |
42 if (wasEmpty) | |
43 startListening(); | |
44 } | |
45 | |
46 #if !ENABLE(OILPAN) | |
47 void ScreenOrientationDispatcher::removeController(ScreenOrientationController*
controller) | |
48 { | |
49 // Do not actually remove the controllers from the vector, instead zero them
out. | |
50 // The zeros are removed in these two cases: | |
51 // 1. either immediately if we are not dispatching any events, | |
52 // 2. or after didChangeScreenOrientation has dispatched all events. | |
53 // This is to correctly handle the re-entrancy case when a controller is des
troyed | |
54 // while in the didChangeScreenOrientation() method. | |
55 size_t index = m_controllers.find(controller); | |
56 if (index == kNotFound) | |
57 return; | |
58 | |
59 m_controllers[index] = 0; | |
60 m_needsPurge = true; | |
61 | |
62 if (!m_isDispatching) | |
63 purgeControllers(); | |
64 } | |
65 | |
66 void ScreenOrientationDispatcher::purgeControllers() | |
67 { | |
68 ASSERT(m_needsPurge); | |
69 | |
70 size_t i = 0; | |
71 while (i < m_controllers.size()) { | |
72 if (!m_controllers[i]) { | |
73 m_controllers[i] = m_controllers.last(); | |
74 m_controllers.removeLast(); | |
75 } else { | |
76 ++i; | |
77 } | |
78 } | |
79 | |
80 m_needsPurge = false; | |
81 | |
82 if (m_controllers.isEmpty()) | |
83 stopListening(); | |
84 } | |
85 #endif | |
86 | |
87 void ScreenOrientationDispatcher::didChangeScreenOrientation(blink::WebScreenOri
entationType orientation) | |
88 { | |
89 #if ENABLE(OILPAN) | |
90 if (m_controllers.isEmpty()) { | |
91 stopListening(); | |
92 return; | |
93 } | |
94 // The on-stack iterator will make m_controllers strong while iterating, | |
95 // therefore no controllers can be removed during iteration. | |
96 for (HeapHashSet<WeakMember<ScreenOrientationController> >::iterator it = m_
controllers.begin(); it != m_controllers.end(); ++it) { | |
97 (*it)->didChangeScreenOrientation(orientation); | |
98 } | |
99 #else | |
100 { | |
101 TemporaryChange<bool> changeIsDispatching(m_isDispatching, true); | |
102 // Don't fire controllers removed or added during event dispatch. | |
103 size_t size = m_controllers.size(); | |
104 for (size_t i = 0; i < size; ++i) { | |
105 if (m_controllers[i]) | |
106 static_cast<ScreenOrientationController*>(m_controllers[i])->did
ChangeScreenOrientation(orientation); | |
107 } | |
108 } | |
109 | |
110 if (m_needsPurge) | |
111 purgeControllers(); | |
112 #endif | |
113 } | |
114 | |
115 void ScreenOrientationDispatcher::startListening() | |
116 { | |
117 blink::Platform::current()->setScreenOrientationListener(this); | |
118 } | |
119 | |
120 void ScreenOrientationDispatcher::stopListening() | |
121 { | |
122 blink::Platform::current()->setScreenOrientationListener(0); | |
123 } | |
124 | |
125 #if ENABLE(OILPAN) | |
126 void ScreenOrientationDispatcher::trace(Visitor* visitor) | |
127 { | |
128 // Weak processing will remove controllers once they are dead. | |
129 visitor->trace(m_controllers); | |
130 } | |
131 #endif | |
132 | |
133 } // namespace WebCore | |
OLD | NEW |