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

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

Issue 968633002: Simplify lifecycle notifiers and observers. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 5 years, 9 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
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 14 matching lines...) Expand all
25 */ 25 */
26 26
27 #ifndef LifecycleObserver_h 27 #ifndef LifecycleObserver_h
28 #define LifecycleObserver_h 28 #define LifecycleObserver_h
29 29
30 #include "platform/heap/Handle.h" 30 #include "platform/heap/Handle.h"
31 #include "wtf/Assertions.h" 31 #include "wtf/Assertions.h"
32 32
33 namespace blink { 33 namespace blink {
34 34
35 template<typename T> 35 template<typename T, typename Observer, typename Notifier>
36 class LifecycleObserver : public WillBeGarbageCollectedMixin { 36 class LifecycleObserver : public WillBeGarbageCollectedMixin {
37 // FIXME: Oilpan: Remove the pre-finalizer by moving LifecycleNotifier 37 // FIXME: Oilpan: Remove the pre-finalizer by moving LifecycleNotifier
38 // to Oilpan's heap and making LifecycleNotifier::m_observers 38 // to Oilpan's heap and making LifecycleNotifier::m_observers
39 // a hash set of weak members. 39 // a hash set of weak members.
40 WILL_BE_USING_PRE_FINALIZER(LifecycleObserver, dispose); 40 WILL_BE_USING_PRE_FINALIZER(LifecycleObserver, dispose);
41 public: 41 public:
42 typedef T Context; 42 using Context = T;
43 43
44 enum Type { 44 explicit LifecycleObserver(Context* context)
45 ActiveDOMObjectType,
46 DocumentLifecycleObserverType,
47 GenericType,
48 PageLifecycleObserverType,
49 DOMWindowLifecycleObserverType
50 };
51
52 explicit LifecycleObserver(Context* context, Type type = GenericType)
53 : m_lifecycleContext(nullptr) 45 : m_lifecycleContext(nullptr)
54 , m_observerType(type)
55 { 46 {
56 #if ENABLE(OILPAN) 47 #if ENABLE(OILPAN)
57 ThreadState::current()->registerPreFinalizer(*this); 48 ThreadState::current()->registerPreFinalizer(*this);
58 #endif 49 #endif
59 setContext(context);
60 } 50 }
51
61 virtual ~LifecycleObserver() 52 virtual ~LifecycleObserver()
62 { 53 {
63 #if !ENABLE(OILPAN) 54 #if !ENABLE(OILPAN)
64 dispose(); 55 dispose();
65 #endif 56 #endif
66 } 57 }
67 58
68 DEFINE_INLINE_VIRTUAL_TRACE() 59 DEFINE_INLINE_VIRTUAL_TRACE()
69 { 60 {
70 visitor->trace(m_lifecycleContext); 61 visitor->trace(m_lifecycleContext);
71 } 62 }
72 virtual void contextDestroyed() { } 63 virtual void contextDestroyed() { }
73 void dispose() 64 void dispose()
74 { 65 {
75 setContext(nullptr); 66 setContext(nullptr);
76 } 67 }
77 68
78 Context* lifecycleContext() const { return m_lifecycleContext; } 69 Context* lifecycleContext() const { return m_lifecycleContext; }
79 void clearLifecycleContext() { m_lifecycleContext = nullptr; } 70 void clearLifecycleContext() { m_lifecycleContext = nullptr; }
80 Type observerType() const { return m_observerType; }
81 71
82 protected: 72 protected:
83 void setContext(Context*); 73 void setContext(Context*);
84 74
75 private:
85 RawPtrWillBeWeakMember<Context> m_lifecycleContext; 76 RawPtrWillBeWeakMember<Context> m_lifecycleContext;
86 Type m_observerType;
87 }; 77 };
88 78
89 // 79 template<typename T, typename Observer, typename Notifier>
90 // These functions should be specialized for each LifecycleObserver instances. 80 inline void LifecycleObserver<T, Observer, Notifier>::setContext(typename Lifecy cleObserver<T, Observer, Notifier>::Context* context)
91 //
92 template<typename T> void observeContext(T*, LifecycleObserver<T>*) { ASSERT_NOT _REACHED(); }
93 template<typename T> void unobserveContext(T*, LifecycleObserver<T>*) { ASSERT_N OT_REACHED(); }
94
95
96 template<typename T>
97 inline void LifecycleObserver<T>::setContext(typename LifecycleObserver<T>::Cont ext* context)
98 { 81 {
99 if (m_lifecycleContext) 82 if (m_lifecycleContext)
100 unobserveContext(m_lifecycleContext.get(), this); 83 static_cast<Notifier*>(m_lifecycleContext.get())->removeObserver(static_ cast<Observer*>(this));
101 84
102 m_lifecycleContext = context; 85 m_lifecycleContext = context;
103 86
104 if (m_lifecycleContext) 87 if (m_lifecycleContext)
105 observeContext(m_lifecycleContext.get(), this); 88 static_cast<Notifier*>(m_lifecycleContext.get())->addObserver(static_cas t<Observer*>(this));
106 } 89 }
107 90
108 } // namespace blink 91 } // namespace blink
109 92
110 #endif // LifecycleObserver_h 93 #endif // LifecycleObserver_h
OLDNEW
« Source/platform/LifecycleNotifier.h ('K') | « Source/platform/LifecycleNotifier.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698