| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All Rights Reserved. | 2 * Copyright (C) 2013 Google 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 |
| 11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
| 12 * | 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 */ | 24 */ |
| 25 | 25 |
| 26 #ifndef DocumentLifecycleObserver_h | 26 #ifndef DocumentLifecycleObserver_h |
| 27 #define DocumentLifecycleObserver_h | 27 #define DocumentLifecycleObserver_h |
| 28 | 28 |
| 29 #include "core/dom/ContextDestructionObserver.h" | 29 #include "core/dom/ContextDestructionObserver.h" |
| 30 #include "core/dom/ContextLifecycleNotifier.h" | |
| 31 #include "wtf/Assertions.h" | 30 #include "wtf/Assertions.h" |
| 32 #include "wtf/PassOwnPtr.h" | 31 #include "wtf/PassOwnPtr.h" |
| 33 #include "wtf/TemporaryChange.h" | 32 #include "wtf/Vector.h" |
| 34 | 33 |
| 35 namespace WebCore { | 34 namespace WebCore { |
| 36 | 35 |
| 37 class Document; | 36 class Document; |
| 38 | 37 |
| 39 class DocumentLifecycleObserver : public ContextDestructionObserver { | 38 class DocumentLifecycleObserver : public ContextDestructionObserver { |
| 40 public: | 39 public: |
| 41 explicit DocumentLifecycleObserver(Document*); | 40 explicit DocumentLifecycleObserver(Document*); |
| 42 virtual ~DocumentLifecycleObserver(); | 41 virtual ~DocumentLifecycleObserver() { } |
| 43 virtual void documentWasDetached() { } | 42 virtual void documentWasDetached() { } |
| 44 virtual void documentWasDisposed() { } | 43 virtual void documentWasDisposed() { } |
| 45 }; | 44 }; |
| 46 | 45 |
| 47 class DocumentLifecycleNotifier : public ContextLifecycleNotifier { | 46 class DocumentLifecycleNotifier { |
| 48 public: | 47 public: |
| 49 static PassOwnPtr<DocumentLifecycleNotifier> create(ScriptExecutionContext*)
; | 48 static PassOwnPtr<DocumentLifecycleNotifier> create(); |
| 50 | 49 |
| 51 void notifyDocumentWasDetached(); | 50 void notifyDocumentWasDetached(); |
| 52 void notifyDocumentWasDisposed(); | 51 void notifyDocumentWasDisposed(); |
| 53 | 52 |
| 54 virtual void addObserver(ContextDestructionObserver*, ContextDestructionObse
rver::Type as) OVERRIDE; | 53 void addObserver(DocumentLifecycleObserver*); |
| 55 virtual void removeObserver(ContextDestructionObserver*, ContextDestructionO
bserver::Type as) OVERRIDE; | |
| 56 | 54 |
| 57 private: | 55 private: |
| 58 explicit DocumentLifecycleNotifier(ScriptExecutionContext*); | 56 #if ASSERT_DISABLED |
| 57 DocumentLifecycleNotifier() { } |
| 58 void startIteration() { } |
| 59 void endIteration() { } |
| 60 #else |
| 61 DocumentLifecycleNotifier() : m_iterating(false) { } |
| 59 void startIteration() { m_iterating = true; } | 62 void startIteration() { m_iterating = true; } |
| 60 void endIteration() { m_iterating = false; } | 63 void endIteration() { m_iterating = false; } |
| 64 bool m_iterating; |
| 65 #endif |
| 61 | 66 |
| 62 typedef HashSet<DocumentLifecycleObserver*> DocumentObserverSet; | 67 Vector<DocumentLifecycleObserver*> m_observers; // Use Vector instead of Has
hSet for faster iteration |
| 63 DocumentObserverSet m_documentObservers; | |
| 64 }; | 68 }; |
| 65 | 69 |
| 66 inline PassOwnPtr<DocumentLifecycleNotifier> DocumentLifecycleNotifier::create(S
criptExecutionContext* context) | |
| 67 { | |
| 68 return adoptPtr(new DocumentLifecycleNotifier(context)); | |
| 69 } | |
| 70 | |
| 71 inline void DocumentLifecycleNotifier::notifyDocumentWasDetached() | 70 inline void DocumentLifecycleNotifier::notifyDocumentWasDetached() |
| 72 { | 71 { |
| 73 TemporaryChange<bool> scope(this->m_iterating, true); | 72 startIteration(); |
| 74 for (DocumentObserverSet::iterator i = m_documentObservers.begin(); i != m_d
ocumentObservers.end(); ++i) | 73 for (size_t i = 0; i < m_observers.size(); ++i) |
| 75 (*i)->documentWasDetached(); | 74 m_observers[i]->documentWasDetached(); |
| 75 endIteration(); |
| 76 } | 76 } |
| 77 | 77 |
| 78 inline void DocumentLifecycleNotifier::notifyDocumentWasDisposed() | 78 inline void DocumentLifecycleNotifier::notifyDocumentWasDisposed() |
| 79 { | 79 { |
| 80 TemporaryChange<bool> scope(this->m_iterating, true); | 80 startIteration(); |
| 81 for (DocumentObserverSet::iterator i = m_documentObservers.begin(); i != m_d
ocumentObservers.end(); ++i) | 81 for (size_t i = 0; i < m_observers.size(); ++i) |
| 82 (*i)->documentWasDisposed(); | 82 m_observers[i]->documentWasDisposed(); |
| 83 endIteration(); |
| 83 } | 84 } |
| 84 | 85 |
| 85 } // namespace WebCore | 86 } // namespace WebCore |
| 86 | 87 |
| 87 #endif // DocumentLifecycleObserver_h | 88 #endif // DocumentLifecycleObserver_h |
| OLD | NEW |