Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(280)

Side by Side Diff: Source/platform/LifecycleNotifier.h

Issue 916033002: Oilpan: Move LifecycleNotifier's hierarchy to Oilpan's heap Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/platform/LifecycleContextTest.cpp ('k') | Source/platform/LifecycleObserver.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 16 matching lines...) Expand all
27 #ifndef LifecycleNotifier_h 27 #ifndef LifecycleNotifier_h
28 #define LifecycleNotifier_h 28 #define LifecycleNotifier_h
29 29
30 #include "platform/LifecycleObserver.h" 30 #include "platform/LifecycleObserver.h"
31 #include "wtf/HashSet.h" 31 #include "wtf/HashSet.h"
32 #include "wtf/PassOwnPtr.h" 32 #include "wtf/PassOwnPtr.h"
33 #include "wtf/TemporaryChange.h" 33 #include "wtf/TemporaryChange.h"
34 34
35 namespace blink { 35 namespace blink {
36 36
37 // Using a virtual inheritance to resolve a diamond inheritance
38 // in ExecutionContext.
37 template<typename T> 39 template<typename T>
38 class LifecycleNotifier { 40 class LifecycleNotifier : public virtual WillBeGarbageCollectedMixin {
39 public: 41 public:
40 typedef LifecycleObserver<T> Observer; 42 typedef LifecycleObserver<T> Observer;
41 typedef T Context; 43 typedef T Context;
42 44
43 virtual ~LifecycleNotifier(); 45 virtual ~LifecycleNotifier();
44 virtual bool isContextThread() const { return true; } 46 virtual bool isContextThread() const { return true; }
45 47
46 // notifyContextDestroyed() should be explicitly dispatched from an 48 // notifyContextDestroyed() should be explicitly dispatched from an
47 // observed context to notify observers contextDestroyed(). 49 // observed context to notify observers contextDestroyed().
48 // At the point of contextDestroyed() is called, m_context is still 50 // At the point of contextDestroyed() is called, m_context is still
49 // valid and thus it is safe to use m_context during the notification. 51 // valid and thus it is safe to use m_context during the notification.
50 virtual void notifyContextDestroyed(); 52 virtual void notifyContextDestroyed();
51 53
52 // FIXME: this won't need to be virtual anymore. 54 // FIXME: this won't need to be virtual anymore.
53 virtual void addObserver(Observer*); 55 virtual void addObserver(Observer*);
54 virtual void removeObserver(Observer*); 56 virtual void removeObserver(Observer*);
55 57
56 virtual void trace(Visitor*) { } 58 virtual void trace(Visitor* visitor)
59 {
60 visitor->trace(m_context);
61 visitor->trace(m_observers);
62 }
57 63
58 bool isIteratingOverObservers() const { return m_iterating != IteratingNone; } 64 bool isIteratingOverObservers() const { return m_iterating != IteratingNone; }
59 65
60 protected: 66 protected:
61 explicit LifecycleNotifier(Context* context) 67 explicit LifecycleNotifier(Context* context)
62 : m_iterating(IteratingNone) 68 : m_iterating(IteratingNone)
63 , m_context(context) 69 , m_context(context)
64 , m_didCallContextDestroyed(false) 70 , m_didCallContextDestroyed(false)
65 { 71 {
66 } 72 }
67 73
68 Context* context() const { return m_context; } 74 Context* context() const { return m_context.get(); }
69 75
70 enum IterationType { 76 enum IterationType {
71 IteratingNone, 77 IteratingNone,
72 IteratingOverAll, 78 IteratingOverAll,
73 IteratingOverActiveDOMObjects, 79 IteratingOverActiveDOMObjects,
74 IteratingOverDocumentObservers, 80 IteratingOverDocumentObservers,
75 IteratingOverPageObservers, 81 IteratingOverPageObservers,
76 IteratingOverDOMWindowObservers 82 IteratingOverDOMWindowObservers
77 }; 83 };
78 84
79 IterationType m_iterating; 85 IterationType m_iterating;
80 86
81 private: 87 private:
82 typedef HashSet<Observer*> ObserverSet; 88 typedef WillBeHeapHashSet<RawPtrWillBeMember<Observer>> ObserverSet;
83 89
84 ObserverSet m_observers; 90 ObserverSet m_observers;
85 Context* m_context; 91 RawPtrWillBeWeakMember<Context> m_context;
86 bool m_didCallContextDestroyed; 92 bool m_didCallContextDestroyed;
87 }; 93 };
88 94
89 template<typename T> 95 template<typename T>
90 inline LifecycleNotifier<T>::~LifecycleNotifier() 96 inline LifecycleNotifier<T>::~LifecycleNotifier()
91 { 97 {
98 #if !ENABLE(OILPAN)
92 // FIXME: Enable the following ASSERT. Also see a FIXME in Document::detach( ). 99 // FIXME: Enable the following ASSERT. Also see a FIXME in Document::detach( ).
93 // ASSERT(!m_observers.size() || m_didCallContextDestroyed); 100 // ASSERT(!m_observers.size() || m_didCallContextDestroyed);
94 101
95 #if !ENABLE(OILPAN)
96 TemporaryChange<IterationType> scope(this->m_iterating, IteratingOverAll); 102 TemporaryChange<IterationType> scope(this->m_iterating, IteratingOverAll);
97 for (Observer* observer : m_observers) { 103 for (Observer* observer : m_observers) {
98 ASSERT(observer->lifecycleContext() == m_context); 104 ASSERT(observer->lifecycleContext() == m_context);
99 observer->clearLifecycleContext(); 105 observer->clearLifecycleContext();
100 } 106 }
101 #endif 107 #endif
102 } 108 }
103 109
104 template<typename T> 110 template<typename T>
105 inline void LifecycleNotifier<T>::notifyContextDestroyed() 111 inline void LifecycleNotifier<T>::notifyContextDestroyed()
106 { 112 {
107 // Don't notify contextDestroyed() twice. 113 // Don't notify contextDestroyed() twice.
108 if (m_didCallContextDestroyed) 114 if (m_didCallContextDestroyed)
109 return; 115 return;
110 116
111 TemporaryChange<IterationType> scope(this->m_iterating, IteratingOverAll); 117 TemporaryChange<IterationType> scope(this->m_iterating, IteratingOverAll);
118 #if ENABLE(OILPAN)
119 for (Observer* observer : m_observers) {
120 ASSERT(observer->lifecycleContext() == m_context);
121 observer->contextDestroyed();
122 }
123 #else
112 Vector<Observer*> snapshotOfObservers; 124 Vector<Observer*> snapshotOfObservers;
113 copyToVector(m_observers, snapshotOfObservers); 125 copyToVector(m_observers, snapshotOfObservers);
114 for (Observer* observer : snapshotOfObservers) { 126 for (Observer* observer : snapshotOfObservers) {
115 // FIXME: Oilpan: At the moment, it's possible that the Observer is 127 // FIXME: Oilpan: At the moment, it's possible that the Observer is
116 // destructed during the iteration. Once we enable Oilpan by default 128 // destructed during the iteration. Once we enable Oilpan by default
117 // for Observers, we can remove the hack by making m_observers 129 // for Observers, we can remove the hack by making m_observers
118 // a HeapHashSet<WeakMember<Observers>>. (i.e., we can just iterate 130 // a HeapHashSet<WeakMember<Observers>>. (i.e., we can just iterate
119 // m_observers without taking a snapshot). 131 // m_observers without taking a snapshot).
120 if (m_observers.contains(observer)) { 132 if (m_observers.contains(observer)) {
121 ASSERT(observer->lifecycleContext() == m_context); 133 ASSERT(observer->lifecycleContext() == m_context);
122 observer->contextDestroyed(); 134 observer->contextDestroyed();
123 } 135 }
124 } 136 }
137 #endif
125 m_didCallContextDestroyed = true; 138 m_didCallContextDestroyed = true;
126 } 139 }
127 140
128 template<typename T> 141 template<typename T>
129 inline void LifecycleNotifier<T>::addObserver(typename LifecycleNotifier<T>::Obs erver* observer) 142 inline void LifecycleNotifier<T>::addObserver(typename LifecycleNotifier<T>::Obs erver* observer)
130 { 143 {
131 RELEASE_ASSERT(m_iterating != IteratingOverAll); 144 RELEASE_ASSERT(m_iterating != IteratingOverAll);
132 m_observers.add(observer); 145 m_observers.add(observer);
133 } 146 }
134 147
135 template<typename T> 148 template<typename T>
136 inline void LifecycleNotifier<T>::removeObserver(typename LifecycleNotifier<T>:: Observer* observer) 149 inline void LifecycleNotifier<T>::removeObserver(typename LifecycleNotifier<T>:: Observer* observer)
137 { 150 {
151 #if ENABLE(OILPAN)
152 RELEASE_ASSERT(m_iterating != IteratingOverAll);
153 #endif
138 m_observers.remove(observer); 154 m_observers.remove(observer);
139 } 155 }
140 156
141 } // namespace blink 157 } // namespace blink
142 158
143 #endif // LifecycleNotifier_h 159 #endif // LifecycleNotifier_h
OLDNEW
« no previous file with comments | « Source/platform/LifecycleContextTest.cpp ('k') | Source/platform/LifecycleObserver.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698