2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 * Copyright (C) 2013 Google Inc. All Rights Reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "core/dom/ContextLifecycleNotifier.h"
31 #include "core/dom/ActiveDOMObject.h"
32 #include "wtf/TemporaryChange.h"
36 void ContextLifecycleNotifier::notifyResumingActiveDOMObjects()
38 TemporaryChange
<IterationType
> scope(m_iterating
, IteratingOverAll
);
39 Vector
<ContextLifecycleObserver
*> snapshotOfObservers
;
40 copyToVector(m_observers
, snapshotOfObservers
);
41 for (ContextLifecycleObserver
* observer
: snapshotOfObservers
) {
42 // FIXME: Oilpan: At the moment, it's possible that a ActiveDOMObject
43 // observer is destructed while iterating. Once we enable Oilpan by default
44 // for all LifecycleObserver<T>s, we can remove the hack by making m_observers
45 // a HeapHashSet<WeakMember<LifecycleObserver<T>>>.
46 // (i.e., we can just iterate m_observers without taking a snapshot).
47 // For more details, see https://codereview.chromium.org/247253002/.
48 if (m_observers
.contains(observer
)) {
49 if (observer
->observerType() != ContextLifecycleObserver::ActiveDOMObjectType
)
51 ActiveDOMObject
* activeDOMObject
= static_cast<ActiveDOMObject
*>(observer
);
52 ASSERT(activeDOMObject
->executionContext() == context());
53 ASSERT(activeDOMObject
->suspendIfNeededCalled());
54 activeDOMObject
->resume();
59 void ContextLifecycleNotifier::notifySuspendingActiveDOMObjects()
61 TemporaryChange
<IterationType
> scope(m_iterating
, IteratingOverAll
);
62 Vector
<ContextLifecycleObserver
*> snapshotOfObservers
;
63 copyToVector(m_observers
, snapshotOfObservers
);
64 for (ContextLifecycleObserver
* observer
: snapshotOfObservers
) {
65 // It's possible that the ActiveDOMObject is already destructed.
67 if (m_observers
.contains(observer
)) {
68 if (observer
->observerType() != ContextLifecycleObserver::ActiveDOMObjectType
)
70 ActiveDOMObject
* activeDOMObject
= static_cast<ActiveDOMObject
*>(observer
);
71 ASSERT(activeDOMObject
->executionContext() == context());
72 ASSERT(activeDOMObject
->suspendIfNeededCalled());
73 activeDOMObject
->suspend();
78 void ContextLifecycleNotifier::notifyStoppingActiveDOMObjects()
80 TemporaryChange
<IterationType
> scope(m_iterating
, IteratingOverAll
);
81 Vector
<ContextLifecycleObserver
*> snapshotOfObservers
;
82 copyToVector(m_observers
, snapshotOfObservers
);
83 for (ContextLifecycleObserver
* observer
: snapshotOfObservers
) {
84 // It's possible that the ActiveDOMObject is already destructed.
86 if (m_observers
.contains(observer
)) {
87 if (observer
->observerType() != ContextLifecycleObserver::ActiveDOMObjectType
)
89 ActiveDOMObject
* activeDOMObject
= static_cast<ActiveDOMObject
*>(observer
);
90 ASSERT(activeDOMObject
->executionContext() == context());
91 ASSERT(activeDOMObject
->suspendIfNeededCalled());
92 activeDOMObject
->stop();
97 unsigned ContextLifecycleNotifier::activeDOMObjectCount() const
99 unsigned activeDOMObjects
= 0;
100 for (ContextLifecycleObserver
* observer
: m_observers
) {
101 if (observer
->observerType() != ContextLifecycleObserver::ActiveDOMObjectType
)
105 return activeDOMObjects
;
108 bool ContextLifecycleNotifier::hasPendingActivity() const
110 for (ContextLifecycleObserver
* observer
: m_observers
) {
111 if (observer
->observerType() != ContextLifecycleObserver::ActiveDOMObjectType
)
113 ActiveDOMObject
* activeDOMObject
= static_cast<ActiveDOMObject
*>(observer
);
114 if (activeDOMObject
->hasPendingActivity())
121 bool ContextLifecycleNotifier::contains(ActiveDOMObject
* object
) const
123 for (ContextLifecycleObserver
* observer
: m_observers
) {
124 if (observer
->observerType() != ContextLifecycleObserver::ActiveDOMObjectType
)
126 ActiveDOMObject
* activeDOMObject
= static_cast<ActiveDOMObject
*>(observer
);
127 if (activeDOMObject
== object
)