Chromium Code Reviews| 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 14 matching lines...) Expand all Loading... | |
| 25 * | 25 * |
| 26 */ | 26 */ |
| 27 | 27 |
| 28 #include "core/dom/ContextLifecycleNotifier.h" | 28 #include "core/dom/ContextLifecycleNotifier.h" |
| 29 | 29 |
| 30 #include "core/dom/ActiveDOMObject.h" | 30 #include "core/dom/ActiveDOMObject.h" |
| 31 #include "wtf/TemporaryChange.h" | 31 #include "wtf/TemporaryChange.h" |
| 32 | 32 |
| 33 namespace blink { | 33 namespace blink { |
| 34 | 34 |
| 35 void ContextLifecycleNotifier::eachActiveDOMObject(void (*f)(ActiveDOMObject*)) | |
| 36 { | |
| 37 // This ensures that no ActiveDOMObjects are *added* during iteration. | |
| 38 // It does not stop ActiveDOMObjects from being removed. | |
| 39 TemporaryChange<IterationType> scope(m_iterating, IteratingOverAll); | |
| 40 | |
| 41 // Take a snapshot of the observers. Observers may be removed | |
| 42 // during iteration, hence we need a snapshot for a stable iterator. | |
| 43 HeapVector<Member<ContextLifecycleObserver>> snapshotOfObservers; | |
|
sof
2016/06/24 05:57:59
Could you explain why this is needed? We don't wan
| |
| 44 snapshotOfObservers.reserveInitialCapacity(m_observers.size()); | |
| 45 copyToVector(m_observers, snapshotOfObservers); | |
| 46 | |
| 47 for (ContextLifecycleObserver* observer : snapshotOfObservers) { | |
| 48 if (!m_observers.contains(observer)) | |
| 49 continue; | |
| 50 | |
| 51 if (observer->observerType() != ContextLifecycleObserver::ActiveDOMObjec tType) | |
| 52 continue; | |
| 53 | |
| 54 ActiveDOMObject* activeDOMObject = static_cast<ActiveDOMObject*>(observe r); | |
| 55 #if DCHECK_IS_ON() | |
| 56 DCHECK_EQ(activeDOMObject->getExecutionContext(), context()); | |
| 57 DCHECK(activeDOMObject->suspendIfNeededCalled()); | |
| 58 #endif | |
| 59 f(activeDOMObject); | |
| 60 } | |
| 61 } | |
| 62 | |
| 35 void ContextLifecycleNotifier::notifyResumingActiveDOMObjects() | 63 void ContextLifecycleNotifier::notifyResumingActiveDOMObjects() |
| 36 { | 64 { |
| 37 TemporaryChange<IterationType> scope(m_iterating, IteratingOverAll); | 65 eachActiveDOMObject([](ActiveDOMObject* object) { |
| 38 Vector<UntracedMember<ContextLifecycleObserver>> snapshotOfObservers; | 66 object->resume(); |
| 39 copyToVector(m_observers, snapshotOfObservers); | 67 }); |
| 40 for (ContextLifecycleObserver* observer : snapshotOfObservers) { | |
| 41 // FIXME: Oilpan: At the moment, it's possible that a ActiveDOMObject | |
| 42 // observer is destructed while iterating. Once we enable Oilpan by defa ult | |
| 43 // for all LifecycleObserver<T>s, we can remove the hack by making m_obs ervers | |
| 44 // a HeapHashSet<WeakMember<LifecycleObserver<T>>>. | |
| 45 // (i.e., we can just iterate m_observers without taking a snapshot). | |
| 46 // For more details, see https://codereview.chromium.org/247253002/. | |
| 47 if (m_observers.contains(observer)) { | |
| 48 if (observer->observerType() != ContextLifecycleObserver::ActiveDOMO bjectType) | |
| 49 continue; | |
| 50 ActiveDOMObject* activeDOMObject = static_cast<ActiveDOMObject*>(obs erver); | |
| 51 #if DCHECK_IS_ON() | |
| 52 DCHECK_EQ(activeDOMObject->getExecutionContext(), context()); | |
| 53 DCHECK(activeDOMObject->suspendIfNeededCalled()); | |
| 54 #endif | |
| 55 activeDOMObject->resume(); | |
| 56 } | |
| 57 } | |
| 58 } | 68 } |
| 59 | 69 |
| 60 void ContextLifecycleNotifier::notifySuspendingActiveDOMObjects() | 70 void ContextLifecycleNotifier::notifySuspendingActiveDOMObjects() |
| 61 { | 71 { |
| 62 TemporaryChange<IterationType> scope(m_iterating, IteratingOverAll); | 72 eachActiveDOMObject([](ActiveDOMObject* object) { |
| 63 Vector<UntracedMember<ContextLifecycleObserver>> snapshotOfObservers; | 73 object->suspend(); |
| 64 copyToVector(m_observers, snapshotOfObservers); | 74 }); |
| 65 for (ContextLifecycleObserver* observer : snapshotOfObservers) { | |
| 66 // It's possible that the ActiveDOMObject is already destructed. | |
| 67 // See a FIXME above. | |
| 68 if (m_observers.contains(observer)) { | |
| 69 if (observer->observerType() != ContextLifecycleObserver::ActiveDOMO bjectType) | |
| 70 continue; | |
| 71 ActiveDOMObject* activeDOMObject = static_cast<ActiveDOMObject*>(obs erver); | |
| 72 #if DCHECK_IS_ON() | |
| 73 DCHECK_EQ(activeDOMObject->getExecutionContext(), context()); | |
| 74 DCHECK(activeDOMObject->suspendIfNeededCalled()); | |
| 75 #endif | |
| 76 activeDOMObject->suspend(); | |
| 77 } | |
| 78 } | |
| 79 } | 75 } |
| 80 | 76 |
| 81 void ContextLifecycleNotifier::notifyStoppingActiveDOMObjects() | 77 void ContextLifecycleNotifier::notifyStoppingActiveDOMObjects() |
| 82 { | 78 { |
| 83 TemporaryChange<IterationType> scope(m_iterating, IteratingOverAll); | 79 eachActiveDOMObject([](ActiveDOMObject* object) { |
| 84 Vector<UntracedMember<ContextLifecycleObserver>> snapshotOfObservers; | 80 object->stop(); |
| 85 copyToVector(m_observers, snapshotOfObservers); | 81 }); |
| 86 for (ContextLifecycleObserver* observer : snapshotOfObservers) { | |
| 87 // It's possible that the ActiveDOMObject is already destructed. | |
| 88 // See a FIXME above. | |
| 89 if (m_observers.contains(observer)) { | |
| 90 if (observer->observerType() != ContextLifecycleObserver::ActiveDOMO bjectType) | |
| 91 continue; | |
| 92 ActiveDOMObject* activeDOMObject = static_cast<ActiveDOMObject*>(obs erver); | |
| 93 #if DCHECK_IS_ON() | |
| 94 DCHECK_EQ(activeDOMObject->getExecutionContext(), context()); | |
| 95 DCHECK(activeDOMObject->suspendIfNeededCalled()); | |
| 96 #endif | |
| 97 activeDOMObject->stop(); | |
| 98 } | |
| 99 } | |
| 100 } | 82 } |
| 101 | 83 |
| 102 unsigned ContextLifecycleNotifier::activeDOMObjectCount() const | 84 unsigned ContextLifecycleNotifier::activeDOMObjectCount() const |
| 103 { | 85 { |
| 104 unsigned activeDOMObjects = 0; | 86 unsigned activeDOMObjects = 0; |
| 105 for (ContextLifecycleObserver* observer : m_observers) { | 87 for (ContextLifecycleObserver* observer : m_observers) { |
| 106 if (observer->observerType() != ContextLifecycleObserver::ActiveDOMObjec tType) | 88 if (observer->observerType() != ContextLifecycleObserver::ActiveDOMObjec tType) |
| 107 continue; | 89 continue; |
| 108 activeDOMObjects++; | 90 activeDOMObjects++; |
| 109 } | 91 } |
| 110 return activeDOMObjects; | 92 return activeDOMObjects; |
| 111 } | 93 } |
| 112 | 94 |
| 113 #if DCHECK_IS_ON() | 95 #if DCHECK_IS_ON() |
| 114 bool ContextLifecycleNotifier::contains(ActiveDOMObject* object) const | 96 bool ContextLifecycleNotifier::contains(ActiveDOMObject* object) const |
| 115 { | 97 { |
| 116 for (ContextLifecycleObserver* observer : m_observers) { | 98 for (ContextLifecycleObserver* observer : m_observers) { |
| 117 if (observer->observerType() != ContextLifecycleObserver::ActiveDOMObjec tType) | 99 if (observer->observerType() != ContextLifecycleObserver::ActiveDOMObjec tType) |
| 118 continue; | 100 continue; |
| 119 ActiveDOMObject* activeDOMObject = static_cast<ActiveDOMObject*>(observe r); | 101 ActiveDOMObject* activeDOMObject = static_cast<ActiveDOMObject*>(observe r); |
| 120 if (activeDOMObject == object) | 102 if (activeDOMObject == object) |
| 121 return true; | 103 return true; |
| 122 } | 104 } |
| 123 return false; | 105 return false; |
| 124 } | 106 } |
| 125 #endif | 107 #endif |
| 126 | 108 |
| 127 } // namespace blink | 109 } // namespace blink |
| OLD | NEW |