| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef ScopedWindowFocusAllowedIndicator_h | 5 #ifndef ScopedWindowFocusAllowedIndicator_h |
| 6 #define ScopedWindowFocusAllowedIndicator_h | 6 #define ScopedWindowFocusAllowedIndicator_h |
| 7 | 7 |
| 8 #include "core/dom/ContextLifecycleObserver.h" | |
| 9 #include "core/dom/ExecutionContext.h" | 8 #include "core/dom/ExecutionContext.h" |
| 10 #include "wtf/Noncopyable.h" | 9 #include "wtf/Noncopyable.h" |
| 11 | 10 |
| 12 namespace blink { | 11 namespace blink { |
| 13 | 12 |
| 14 class ScopedWindowFocusAllowedIndicator final { | 13 class ScopedWindowFocusAllowedIndicator final { |
| 15 USING_FAST_MALLOC(ScopedWindowFocusAllowedIndicator); | 14 USING_FAST_MALLOC(ScopedWindowFocusAllowedIndicator); |
| 16 WTF_MAKE_NONCOPYABLE(ScopedWindowFocusAllowedIndicator); | 15 WTF_MAKE_NONCOPYABLE(ScopedWindowFocusAllowedIndicator); |
| 17 | 16 |
| 18 public: | 17 public: |
| 19 explicit ScopedWindowFocusAllowedIndicator(ExecutionContext* executionContext) | 18 explicit ScopedWindowFocusAllowedIndicator(ExecutionContext* executionContext) |
| 20 : m_observer(new Observer(executionContext)) {} | 19 : m_executionContext(executionContext) { |
| 21 ~ScopedWindowFocusAllowedIndicator() { m_observer->dispose(); } | 20 executionContext->allowWindowInteraction(); |
| 21 } |
| 22 ~ScopedWindowFocusAllowedIndicator() { |
| 23 m_executionContext->consumeWindowInteraction(); |
| 24 } |
| 22 | 25 |
| 23 private: | 26 private: |
| 24 class Observer final : public GarbageCollected<Observer>, | 27 // This doesn't create a cycle because ScopedWindowFocusAllowedIndicator |
| 25 public ContextLifecycleObserver { | 28 // is used only on a machine stack. |
| 26 USING_GARBAGE_COLLECTED_MIXIN(Observer); | 29 Persistent<ExecutionContext> m_executionContext; |
| 27 | |
| 28 public: | |
| 29 explicit Observer(ExecutionContext* executionContext) | |
| 30 : ContextLifecycleObserver(executionContext) { | |
| 31 if (executionContext) | |
| 32 executionContext->allowWindowInteraction(); | |
| 33 } | |
| 34 | |
| 35 void dispose() { | |
| 36 if (getExecutionContext()) | |
| 37 getExecutionContext()->consumeWindowInteraction(); | |
| 38 } | |
| 39 }; | |
| 40 | |
| 41 // In Oilpan, destructors are not allowed to touch other on-heap objects. | |
| 42 // The Observer indirection is needed to keep | |
| 43 // ScopedWindowFocusAllowedIndicator off-heap and thus allows its destructor | |
| 44 // to call getExecutionContext()->consumeWindowInteraction(). | |
| 45 Persistent<Observer> m_observer; | |
| 46 }; | 30 }; |
| 47 | 31 |
| 48 } // namespace blink | 32 } // namespace blink |
| 49 | 33 |
| 50 #endif // ScopedWindowFocusAllowedIndicator_h | 34 #endif // ScopedWindowFocusAllowedIndicator_h |
| OLD | NEW |