| OLD | NEW |
| 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 19 matching lines...) Expand all Loading... |
| 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, typename Observer, typename Notifier> | 35 template<typename T, typename Observer, typename Notifier> |
| 36 class LifecycleObserver : public WillBeGarbageCollectedMixin { | 36 class LifecycleObserver : public WillBeGarbageCollectedMixin { |
| 37 public: | 37 public: |
| 38 using Context = T; | 38 using Context = T; |
| 39 | 39 |
| 40 LifecycleObserver() | |
| 41 : m_lifecycleContext(nullptr) | |
| 42 { | |
| 43 } | |
| 44 | |
| 45 #if !ENABLE(OILPAN) | 40 #if !ENABLE(OILPAN) |
| 46 virtual ~LifecycleObserver() | 41 virtual ~LifecycleObserver() |
| 47 { | 42 { |
| 48 dispose(); | 43 dispose(); |
| 49 } | 44 } |
| 50 #endif | 45 #endif |
| 51 | 46 |
| 52 DEFINE_INLINE_VIRTUAL_TRACE() | 47 DEFINE_INLINE_VIRTUAL_TRACE() |
| 53 { | 48 { |
| 54 visitor->trace(m_lifecycleContext); | 49 visitor->trace(m_lifecycleContext); |
| 55 } | 50 } |
| 56 virtual void contextDestroyed() { } | 51 virtual void contextDestroyed() { } |
| 57 | 52 |
| 58 void dispose() | 53 void dispose() |
| 59 { | 54 { |
| 60 setContext(nullptr); | 55 setContext(nullptr); |
| 61 } | 56 } |
| 62 | 57 |
| 63 Context* lifecycleContext() const { return m_lifecycleContext; } | 58 Context* lifecycleContext() const { return m_lifecycleContext; } |
| 64 void clearLifecycleContext() { m_lifecycleContext = nullptr; } | 59 void clearLifecycleContext() { m_lifecycleContext = nullptr; } |
| 65 | 60 |
| 66 protected: | 61 protected: |
| 62 explicit LifecycleObserver(Context* context) |
| 63 : m_lifecycleContext(nullptr) |
| 64 { |
| 65 setContext(context); |
| 66 } |
| 67 |
| 67 void setContext(Context*); | 68 void setContext(Context*); |
| 68 | 69 |
| 69 private: | 70 private: |
| 70 RawPtrWillBeWeakMember<Context> m_lifecycleContext; | 71 RawPtrWillBeWeakMember<Context> m_lifecycleContext; |
| 71 }; | 72 }; |
| 72 | 73 |
| 73 template<typename T, typename Observer, typename Notifier> | 74 template<typename T, typename Observer, typename Notifier> |
| 74 inline void LifecycleObserver<T, Observer, Notifier>::setContext(typename Lifecy
cleObserver<T, Observer, Notifier>::Context* context) | 75 inline void LifecycleObserver<T, Observer, Notifier>::setContext(typename Lifecy
cleObserver<T, Observer, Notifier>::Context* context) |
| 75 { | 76 { |
| 76 if (m_lifecycleContext) | 77 if (m_lifecycleContext) |
| 77 static_cast<Notifier*>(m_lifecycleContext.get())->removeObserver(static_
cast<Observer*>(this)); | 78 static_cast<Notifier*>(m_lifecycleContext.get())->removeObserver(static_
cast<Observer*>(this)); |
| 78 | 79 |
| 79 m_lifecycleContext = context; | 80 m_lifecycleContext = context; |
| 80 | 81 |
| 81 if (m_lifecycleContext) | 82 if (m_lifecycleContext) |
| 82 static_cast<Notifier*>(m_lifecycleContext.get())->addObserver(static_cas
t<Observer*>(this)); | 83 static_cast<Notifier*>(m_lifecycleContext.get())->addObserver(static_cas
t<Observer*>(this)); |
| 83 } | 84 } |
| 84 | 85 |
| 85 } // namespace blink | 86 } // namespace blink |
| 86 | 87 |
| 87 #endif // LifecycleObserver_h | 88 #endif // LifecycleObserver_h |
| OLD | NEW |