| 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 15 matching lines...) Expand all Loading... |
| 26 | 26 |
| 27 #ifndef ContextLifecycleObserver_h | 27 #ifndef ContextLifecycleObserver_h |
| 28 #define ContextLifecycleObserver_h | 28 #define ContextLifecycleObserver_h |
| 29 | 29 |
| 30 #include "core/CoreExport.h" | 30 #include "core/CoreExport.h" |
| 31 #include "core/dom/ExecutionContext.h" | 31 #include "core/dom/ExecutionContext.h" |
| 32 #include "platform/LifecycleObserver.h" | 32 #include "platform/LifecycleObserver.h" |
| 33 | 33 |
| 34 namespace blink { | 34 namespace blink { |
| 35 | 35 |
| 36 class LocalDOMWindow; |
| 36 class LocalFrame; | 37 class LocalFrame; |
| 37 | 38 |
| 38 // ContextClient and ContextLifecycleObserver are helpers to associate a | 39 // ContextClient and ContextLifecycleObserver are helpers to associate an |
| 39 // class with an ExecutionContext. ContextLifecycleObserver provides an | 40 // object with an ExecutionContext. |
| 40 // additional contextDestroyed() hook to execute cleanup code when a | |
| 41 // context is destroyed. Prefer the simpler ContextClient when possible. | |
| 42 // | 41 // |
| 43 // getExecutionContext() returns null after the observing context is detached. | 42 // - getExecutionContext() returns null after the context is detached. |
| 44 // frame() returns null after the observing context is detached or if the | 43 // - frame() is a syntax sugar for getExecutionContext()->frame(). It returns |
| 45 // context doesn't have a frame (i.e., if the context is not a Document). | 44 // null after the context is detached or the context is not a Document. |
| 46 | |
| 47 class CORE_EXPORT ContextClient : public GarbageCollectedMixin { | 45 class CORE_EXPORT ContextClient : public GarbageCollectedMixin { |
| 48 public: | 46 public: |
| 49 ExecutionContext* getExecutionContext() const; | 47 ExecutionContext* getExecutionContext() const; |
| 50 LocalFrame* frame() const; | 48 LocalFrame* frame() const; |
| 51 | 49 |
| 52 DECLARE_VIRTUAL_TRACE(); | 50 DECLARE_VIRTUAL_TRACE(); |
| 53 | 51 |
| 54 protected: | 52 protected: |
| 55 explicit ContextClient(ExecutionContext* executionContext) | 53 explicit ContextClient(ExecutionContext*); |
| 56 : m_executionContext(executionContext) {} | |
| 57 explicit ContextClient(LocalFrame*); | 54 explicit ContextClient(LocalFrame*); |
| 58 | 55 |
| 59 private: | 56 private: |
| 60 WeakMember<ExecutionContext> m_executionContext; | 57 WeakMember<ExecutionContext> m_executionContext; |
| 61 }; | 58 }; |
| 62 | 59 |
| 60 // ContextLifecycleObserver provides an additional contextDestroyed() hook |
| 61 // to execute cleanup code when a context is destroyed. Prefer the simpler |
| 62 // ContextClient when possible. |
| 63 class CORE_EXPORT ContextLifecycleObserver | 63 class CORE_EXPORT ContextLifecycleObserver |
| 64 : public LifecycleObserver<ExecutionContext, ContextLifecycleObserver> { | 64 : public LifecycleObserver<ExecutionContext, ContextLifecycleObserver> { |
| 65 public: | 65 public: |
| 66 ExecutionContext* getExecutionContext() const { return lifecycleContext(); } | 66 ExecutionContext* getExecutionContext() const { return lifecycleContext(); } |
| 67 LocalFrame* frame() const; | 67 LocalFrame* frame() const; |
| 68 | 68 |
| 69 enum Type { | 69 enum Type { |
| 70 GenericType, | 70 GenericType, |
| 71 SuspendableObjectType, | 71 SuspendableObjectType, |
| 72 }; | 72 }; |
| 73 | 73 |
| 74 Type observerType() const { return m_observerType; } | 74 Type observerType() const { return m_observerType; } |
| 75 | 75 |
| 76 protected: | 76 protected: |
| 77 explicit ContextLifecycleObserver(ExecutionContext* executionContext, | 77 explicit ContextLifecycleObserver(ExecutionContext* executionContext, |
| 78 Type type = GenericType) | 78 Type type = GenericType) |
| 79 : LifecycleObserver(executionContext), m_observerType(type) {} | 79 : LifecycleObserver(executionContext), m_observerType(type) {} |
| 80 | 80 |
| 81 private: | 81 private: |
| 82 Type m_observerType; | 82 Type m_observerType; |
| 83 }; | 83 }; |
| 84 | 84 |
| 85 // DOMWindowClient is a helper to associate an object with a LocalDOMWindow. |
| 86 // |
| 87 // - domWindow() returns null after the window is detached. |
| 88 // - frame() is a syntax sugar for domWindow()->frame(). It returns |
| 89 // null after the window is detached. |
| 90 // |
| 91 // If the object is a per-ExecutionContext thing, use ContextClient/ |
| 92 // ContextLifecycleObserver. If the object is a per-DOMWindow thing, use |
| 93 // DOMWindowClient. Basically, DOMWindowClient is expected to be used (only) |
| 94 // for objects directly held by LocalDOMWindow. Other objects should use |
| 95 // ContextClient/ContextLifecycleObserver. |
| 96 // |
| 97 // There is a subtle difference between the timing when the context gets |
| 98 // detached and the timing when the window gets detached. In common cases, |
| 99 // these two happen at the same timing. The only exception is a case where |
| 100 // a frame navigates from an initial empty document to another same-origin |
| 101 // document. In this case, a Document is recreated but a DOMWindow is reused. |
| 102 // Hence, in the navigated document ContextClient::getExecutionContext() |
| 103 // returns null while DOMWindowClient::domWindow() keeps returning the window. |
| 104 class CORE_EXPORT DOMWindowClient : public GarbageCollectedMixin { |
| 105 public: |
| 106 LocalDOMWindow* domWindow() const; |
| 107 LocalFrame* frame() const; |
| 108 |
| 109 DECLARE_VIRTUAL_TRACE(); |
| 110 |
| 111 protected: |
| 112 explicit DOMWindowClient(LocalDOMWindow*); |
| 113 explicit DOMWindowClient(LocalFrame*); |
| 114 |
| 115 private: |
| 116 WeakMember<LocalDOMWindow> m_domWindow; |
| 117 }; |
| 118 |
| 85 } // namespace blink | 119 } // namespace blink |
| 86 | 120 |
| 87 #endif // ContextLifecycleObserver_h | 121 #endif // ContextLifecycleObserver_h |
| OLD | NEW |