OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. | 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. |
3 * Copyright (C) 2013 Google Inc. All Rights Reserved. | 3 * Copyright (C) 2013 Google Inc. All Rights Reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
7 * are met: | 7 * are met: |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. 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 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 17 matching lines...) Expand all Loading... |
28 #include "config.h" | 28 #include "config.h" |
29 #include "core/dom/ContextLifecycleNotifier.h" | 29 #include "core/dom/ContextLifecycleNotifier.h" |
30 | 30 |
31 #include "core/dom/ActiveDOMObject.h" | 31 #include "core/dom/ActiveDOMObject.h" |
32 #include "wtf/TemporaryChange.h" | 32 #include "wtf/TemporaryChange.h" |
33 | 33 |
34 namespace blink { | 34 namespace blink { |
35 | 35 |
36 ContextLifecycleNotifier::ContextLifecycleNotifier(ExecutionContext* context) | 36 ContextLifecycleNotifier::ContextLifecycleNotifier(ExecutionContext* context) |
37 : LifecycleNotifier<ExecutionContext, ContextLifecycleObserver>(context) | 37 : LifecycleNotifier<ExecutionContext, ContextLifecycleObserver>(context) |
| 38 , m_activeDOMObjectCount(0) |
38 { | 39 { |
39 } | 40 } |
40 | 41 |
41 void ContextLifecycleNotifier::addObserver(ContextLifecycleObserver* observer) | 42 void ContextLifecycleNotifier::addObserver(ContextLifecycleObserver* observer) |
42 { | 43 { |
43 LifecycleNotifier<ExecutionContext, ContextLifecycleObserver>::addObserver(o
bserver); | 44 LifecycleNotifier<ExecutionContext, ContextLifecycleObserver>::addObserver(o
bserver); |
44 if (observer->observerType() == ContextLifecycleObserver::ActiveDOMObjectTyp
e) { | 45 if (observer->observerType() == ContextLifecycleObserver::ActiveDOMObjectTyp
e) { |
45 RELEASE_ASSERT(m_iterating != IteratingOverActiveDOMObjects); | 46 RELEASE_ASSERT(m_iterating != IteratingOverActiveDOMObjects); |
46 m_activeDOMObjects.add(static_cast<ActiveDOMObject*>(observer)); | 47 m_activeDOMObjectCount++; |
47 } | 48 } |
48 } | 49 } |
49 | 50 |
50 void ContextLifecycleNotifier::removeObserver(ContextLifecycleObserver* observer
) | 51 void ContextLifecycleNotifier::removeObserver(ContextLifecycleObserver* observer
) |
51 { | 52 { |
52 LifecycleNotifier<ExecutionContext, ContextLifecycleObserver>::removeObserve
r(observer); | 53 LifecycleNotifier<ExecutionContext, ContextLifecycleObserver>::removeObserve
r(observer); |
53 if (observer->observerType() == ContextLifecycleObserver::ActiveDOMObjectTyp
e) | 54 if (observer->observerType() == ContextLifecycleObserver::ActiveDOMObjectTyp
e) { |
54 m_activeDOMObjects.remove(static_cast<ActiveDOMObject*>(observer)); | 55 RELEASE_ASSERT(m_activeDOMObjectCount > 0); |
| 56 m_activeDOMObjectCount--; |
| 57 } |
55 } | 58 } |
56 | 59 |
57 void ContextLifecycleNotifier::notifyResumingActiveDOMObjects() | 60 void ContextLifecycleNotifier::notifyResumingActiveDOMObjects() |
58 { | 61 { |
59 TemporaryChange<IterationType> scope(m_iterating, IteratingOverActiveDOMObje
cts); | 62 TemporaryChange<IterationType> scope(m_iterating, IteratingOverActiveDOMObje
cts); |
60 Vector<ActiveDOMObject*> snapshotOfActiveDOMObjects; | 63 Vector<ContextLifecycleObserver*> snapshotOfObservers; |
61 copyToVector(m_activeDOMObjects, snapshotOfActiveDOMObjects); | 64 copyToVector(m_observers, snapshotOfObservers); |
62 for (ActiveDOMObject* obj : snapshotOfActiveDOMObjects) { | 65 for (ContextLifecycleObserver* observer : snapshotOfObservers) { |
63 // FIXME: Oilpan: At the moment, it's possible that the ActiveDOMObject | 66 if (observer->observerType() != ContextLifecycleObserver::ActiveDOMObjec
tType) |
64 // is destructed during the iteration. Once we enable Oilpan by default | 67 continue; |
65 // for ActiveDOMObjects, we can remove the hack by making | 68 // FIXME: Oilpan: At the moment, it's possible that a ActiveDOMObject |
66 // m_activeDOMObjects a HeapHashSet<WeakMember<ActiveDOMObject>>. | 69 // observer is destructed while iterating. Once we enable Oilpan by defa
ult |
67 // (i.e., we can just iterate m_activeDOMObjects without taking | 70 // for all LifecycleObserver<T>s, we can remove the hack by making m_obs
ervers |
68 // a snapshot). | 71 // a HeapHashSet<WeakMember<LifecycleObserver<T>>>. |
| 72 // (i.e., we can just iterate m_observers without taking a snapshot). |
69 // For more details, see https://codereview.chromium.org/247253002/. | 73 // For more details, see https://codereview.chromium.org/247253002/. |
70 if (m_activeDOMObjects.contains(obj)) { | 74 if (m_observers.contains(observer)) { |
71 ASSERT(obj->executionContext() == context()); | 75 ActiveDOMObject* activeDOMObject = static_cast<ActiveDOMObject*>(obs
erver); |
72 ASSERT(obj->suspendIfNeededCalled()); | 76 ASSERT(activeDOMObject->executionContext() == context()); |
73 obj->resume(); | 77 ASSERT(activeDOMObject->suspendIfNeededCalled()); |
| 78 activeDOMObject->resume(); |
74 } | 79 } |
75 } | 80 } |
76 } | 81 } |
77 | 82 |
78 void ContextLifecycleNotifier::notifySuspendingActiveDOMObjects() | 83 void ContextLifecycleNotifier::notifySuspendingActiveDOMObjects() |
79 { | 84 { |
80 TemporaryChange<IterationType> scope(m_iterating, IteratingOverActiveDOMObje
cts); | 85 TemporaryChange<IterationType> scope(m_iterating, IteratingOverActiveDOMObje
cts); |
81 Vector<ActiveDOMObject*> snapshotOfActiveDOMObjects; | 86 Vector<ContextLifecycleObserver*> snapshotOfObservers; |
82 copyToVector(m_activeDOMObjects, snapshotOfActiveDOMObjects); | 87 copyToVector(m_observers, snapshotOfObservers); |
83 for (ActiveDOMObject* obj : snapshotOfActiveDOMObjects) { | 88 for (ContextLifecycleObserver* observer : snapshotOfObservers) { |
| 89 if (observer->observerType() != ContextLifecycleObserver::ActiveDOMObjec
tType) |
| 90 continue; |
84 // It's possible that the ActiveDOMObject is already destructed. | 91 // It's possible that the ActiveDOMObject is already destructed. |
85 // See a FIXME above. | 92 // See a FIXME above. |
86 if (m_activeDOMObjects.contains(obj)) { | 93 if (m_observers.contains(observer)) { |
87 ASSERT(obj->executionContext() == context()); | 94 ActiveDOMObject* activeDOMObject = static_cast<ActiveDOMObject*>(obs
erver); |
88 ASSERT(obj->suspendIfNeededCalled()); | 95 ASSERT(activeDOMObject->executionContext() == context()); |
89 obj->suspend(); | 96 ASSERT(activeDOMObject->suspendIfNeededCalled()); |
| 97 activeDOMObject->suspend(); |
90 } | 98 } |
91 } | 99 } |
92 } | 100 } |
93 | 101 |
94 void ContextLifecycleNotifier::notifyStoppingActiveDOMObjects() | 102 void ContextLifecycleNotifier::notifyStoppingActiveDOMObjects() |
95 { | 103 { |
96 TemporaryChange<IterationType> scope(m_iterating, IteratingOverActiveDOMObje
cts); | 104 TemporaryChange<IterationType> scope(m_iterating, IteratingOverActiveDOMObje
cts); |
97 Vector<ActiveDOMObject*> snapshotOfActiveDOMObjects; | 105 Vector<ContextLifecycleObserver*> snapshotOfObservers; |
98 copyToVector(m_activeDOMObjects, snapshotOfActiveDOMObjects); | 106 copyToVector(m_observers, snapshotOfObservers); |
99 for (ActiveDOMObject* obj : snapshotOfActiveDOMObjects) { | 107 for (ContextLifecycleObserver* observer : snapshotOfObservers) { |
| 108 if (observer->observerType() != ContextLifecycleObserver::ActiveDOMObjec
tType) |
| 109 continue; |
100 // It's possible that the ActiveDOMObject is already destructed. | 110 // It's possible that the ActiveDOMObject is already destructed. |
101 // See a FIXME above. | 111 // See a FIXME above. |
102 if (m_activeDOMObjects.contains(obj)) { | 112 if (m_observers.contains(observer)) { |
103 ASSERT(obj->executionContext() == context()); | 113 ActiveDOMObject* activeDOMObject = static_cast<ActiveDOMObject*>(obs
erver); |
104 ASSERT(obj->suspendIfNeededCalled()); | 114 ASSERT(activeDOMObject->executionContext() == context()); |
105 obj->stop(); | 115 ASSERT(activeDOMObject->suspendIfNeededCalled()); |
| 116 activeDOMObject->stop(); |
106 } | 117 } |
107 } | 118 } |
108 } | 119 } |
109 | 120 |
110 bool ContextLifecycleNotifier::hasPendingActivity() const | 121 bool ContextLifecycleNotifier::hasPendingActivity() const |
111 { | 122 { |
112 for (ActiveDOMObject* obj : m_activeDOMObjects) { | 123 for (ContextLifecycleObserver* observer : m_observers) { |
113 if (obj->hasPendingActivity()) | 124 if (observer->observerType() != ContextLifecycleObserver::ActiveDOMObjec
tType) |
| 125 continue; |
| 126 ActiveDOMObject* activeDOMObject = static_cast<ActiveDOMObject*>(observe
r); |
| 127 if (activeDOMObject->hasPendingActivity()) |
114 return true; | 128 return true; |
115 } | 129 } |
116 return false; | 130 return false; |
117 } | 131 } |
118 | 132 |
| 133 #if ENABLE(ASSERT) |
| 134 bool ContextLifecycleNotifier::contains(ActiveDOMObject* object) const |
| 135 { |
| 136 for (ContextLifecycleObserver* observer : m_observers) { |
| 137 if (observer->observerType() != ContextLifecycleObserver::ActiveDOMObjec
tType) |
| 138 continue; |
| 139 ActiveDOMObject* activeDOMObject = static_cast<ActiveDOMObject*>(observe
r); |
| 140 if (activeDOMObject == object) |
| 141 return true; |
| 142 } |
| 143 return false; |
| 144 } |
| 145 #endif |
| 146 |
119 } // namespace blink | 147 } // namespace blink |
OLD | NEW |