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

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

Issue 2045703004: Simplify contextDestroyed() notifications. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: eagerly clear observers Created 4 years, 6 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
« no previous file with comments | « no previous file | no next file » | 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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 DEFINE_INLINE_VIRTUAL_TRACE() 51 DEFINE_INLINE_VIRTUAL_TRACE()
52 { 52 {
53 visitor->trace(m_observers); 53 visitor->trace(m_observers);
54 } 54 }
55 55
56 bool isIteratingOverObservers() const { return m_iterating != IteratingNone; } 56 bool isIteratingOverObservers() const { return m_iterating != IteratingNone; }
57 57
58 protected: 58 protected:
59 LifecycleNotifier() 59 LifecycleNotifier()
60 : m_iterating(IteratingNone) 60 : m_iterating(IteratingNone)
61 , m_didCallContextDestroyed(false)
62 { 61 {
63 } 62 }
64 63
65 enum IterationType { 64 enum IterationType {
66 IteratingNone, 65 IteratingNone,
67 IteratingOverAll, 66 IteratingOverAll,
68 }; 67 };
69 68
70 IterationType m_iterating; 69 IterationType m_iterating;
71 70
72 protected: 71 protected:
73 using ObserverSet = HeapHashSet<WeakMember<Observer>>; 72 using ObserverSet = HeapHashSet<WeakMember<Observer>>;
74 73
75 ObserverSet m_observers; 74 ObserverSet m_observers;
76 75
77 #if DCHECK_IS_ON() 76 #if DCHECK_IS_ON()
78 T* context() { return static_cast<T*>(this); } 77 T* context() { return static_cast<T*>(this); }
79 #endif 78 #endif
80
81 private:
82 bool m_didCallContextDestroyed;
83 }; 79 };
84 80
85 template<typename T, typename Observer> 81 template<typename T, typename Observer>
86 inline LifecycleNotifier<T, Observer>::~LifecycleNotifier() 82 inline LifecycleNotifier<T, Observer>::~LifecycleNotifier()
87 { 83 {
88 // FIXME: Enable the following ASSERT. Also see a FIXME in Document::detach( ). 84 // FIXME: Enable the following ASSERT. Also see a FIXME in Document::detach( ).
89 // ASSERT(!m_observers.size() || m_didCallContextDestroyed); 85 // ASSERT(!m_observers.size());
90 } 86 }
91 87
92 template<typename T, typename Observer> 88 template<typename T, typename Observer>
93 inline void LifecycleNotifier<T, Observer>::notifyContextDestroyed() 89 inline void LifecycleNotifier<T, Observer>::notifyContextDestroyed()
94 { 90 {
95 // Don't notify contextDestroyed() twice.
96 if (m_didCallContextDestroyed)
97 return;
98
99 TemporaryChange<IterationType> scope(m_iterating, IteratingOverAll); 91 TemporaryChange<IterationType> scope(m_iterating, IteratingOverAll);
100 Vector<UntracedMember<Observer>> snapshotOfObservers; 92 ObserverSet observers;
101 copyToVector(m_observers, snapshotOfObservers); 93 m_observers.swap(observers);
102 for (Observer* observer : snapshotOfObservers) { 94 for (Observer* observer : observers) {
103 if (!m_observers.contains(observer))
104 continue;
105
106 ASSERT(observer->lifecycleContext() == context()); 95 ASSERT(observer->lifecycleContext() == context());
107 observer->contextDestroyed(); 96 observer->contextDestroyed();
108 } 97 }
109
110 m_didCallContextDestroyed = true;
111 } 98 }
112 99
113 template<typename T, typename Observer> 100 template<typename T, typename Observer>
114 inline void LifecycleNotifier<T, Observer>::addObserver(Observer* observer) 101 inline void LifecycleNotifier<T, Observer>::addObserver(Observer* observer)
115 { 102 {
116 RELEASE_ASSERT(m_iterating != IteratingOverAll); 103 RELEASE_ASSERT(m_iterating != IteratingOverAll);
117 m_observers.add(observer); 104 m_observers.add(observer);
118 } 105 }
119 106
120 template<typename T, typename Observer> 107 template<typename T, typename Observer>
121 inline void LifecycleNotifier<T, Observer>::removeObserver(Observer* observer) 108 inline void LifecycleNotifier<T, Observer>::removeObserver(Observer* observer)
122 { 109 {
123 m_observers.remove(observer); 110 m_observers.remove(observer);
124 } 111 }
125 112
126 } // namespace blink 113 } // namespace blink
127 114
128 #endif // LifecycleNotifier_h 115 #endif // LifecycleNotifier_h
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698