| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "modules/compositorworker/WindowAnimationWorklet.h" | 5 #include "modules/compositorworker/WindowAnimationWorklet.h" |
| 6 | 6 |
| 7 #include "core/dom/Document.h" |
| 7 #include "core/frame/LocalDOMWindow.h" | 8 #include "core/frame/LocalDOMWindow.h" |
| 8 #include "core/frame/LocalFrame.h" | 9 #include "core/frame/LocalFrame.h" |
| 9 #include "modules/compositorworker/AnimationWorklet.h" | 10 #include "modules/compositorworker/AnimationWorklet.h" |
| 10 | 11 |
| 11 namespace blink { | 12 namespace blink { |
| 12 | 13 |
| 13 WindowAnimationWorklet::WindowAnimationWorklet(LocalDOMWindow& window) | 14 WindowAnimationWorklet::WindowAnimationWorklet(LocalDOMWindow& window) |
| 14 : DOMWindowProperty(window.frame()) {} | 15 : ContextLifecycleObserver(window.frame()->document()) {} |
| 15 | 16 |
| 16 const char* WindowAnimationWorklet::supplementName() { | 17 const char* WindowAnimationWorklet::supplementName() { |
| 17 return "WindowAnimationWorklet"; | 18 return "WindowAnimationWorklet"; |
| 18 } | 19 } |
| 19 | 20 |
| 20 // static | 21 // static |
| 21 WindowAnimationWorklet& WindowAnimationWorklet::from(LocalDOMWindow& window) { | 22 WindowAnimationWorklet& WindowAnimationWorklet::from(LocalDOMWindow& window) { |
| 22 WindowAnimationWorklet* supplement = static_cast<WindowAnimationWorklet*>( | 23 WindowAnimationWorklet* supplement = static_cast<WindowAnimationWorklet*>( |
| 23 Supplement<LocalDOMWindow>::from(window, supplementName())); | 24 Supplement<LocalDOMWindow>::from(window, supplementName())); |
| 24 if (!supplement) { | 25 if (!supplement) { |
| 25 supplement = new WindowAnimationWorklet(window); | 26 supplement = new WindowAnimationWorklet(window); |
| 26 provideTo(window, supplementName(), supplement); | 27 provideTo(window, supplementName(), supplement); |
| 27 } | 28 } |
| 28 return *supplement; | 29 return *supplement; |
| 29 } | 30 } |
| 30 | 31 |
| 31 // static | 32 // static |
| 32 Worklet* WindowAnimationWorklet::animationWorklet(DOMWindow& window) { | 33 Worklet* WindowAnimationWorklet::animationWorklet(DOMWindow& window) { |
| 33 return from(toLocalDOMWindow(window)).animationWorklet(); | 34 return from(toLocalDOMWindow(window)) |
| 35 .animationWorklet(toLocalDOMWindow(window)); |
| 34 } | 36 } |
| 35 | 37 |
| 36 AnimationWorklet* WindowAnimationWorklet::animationWorklet() { | 38 AnimationWorklet* WindowAnimationWorklet::animationWorklet( |
| 37 if (!m_animationWorklet && frame()) | 39 LocalDOMWindow& window) { |
| 38 m_animationWorklet = AnimationWorklet::create(frame()); | 40 if (!m_animationWorklet && getExecutionContext()) { |
| 41 DCHECK(window.frame()); |
| 42 m_animationWorklet = AnimationWorklet::create(window.frame()); |
| 43 } |
| 39 return m_animationWorklet.get(); | 44 return m_animationWorklet.get(); |
| 40 } | 45 } |
| 41 | 46 |
| 42 void WindowAnimationWorklet::frameDestroyed() { | 47 // Break the following cycle when the context gets detached. |
| 43 m_animationWorklet.clear(); | 48 // Otherwise, the worklet object will leak. |
| 44 DOMWindowProperty::frameDestroyed(); | 49 // |
| 50 // window => window.animationWorklet |
| 51 // => WindowAnimationWorklet |
| 52 // => AnimationWorklet <--- break this reference |
| 53 // => ThreadedWorkletMessagingProxy |
| 54 // => Document |
| 55 // => ... => window |
| 56 void WindowAnimationWorklet::contextDestroyed() { |
| 57 m_animationWorklet = nullptr; |
| 45 } | 58 } |
| 46 | 59 |
| 47 DEFINE_TRACE(WindowAnimationWorklet) { | 60 DEFINE_TRACE(WindowAnimationWorklet) { |
| 48 visitor->trace(m_animationWorklet); | 61 visitor->trace(m_animationWorklet); |
| 49 Supplement<LocalDOMWindow>::trace(visitor); | 62 Supplement<LocalDOMWindow>::trace(visitor); |
| 50 DOMWindowProperty::trace(visitor); | 63 ContextLifecycleObserver::trace(visitor); |
| 51 } | 64 } |
| 52 | 65 |
| 53 } // namespace blink | 66 } // namespace blink |
| OLD | NEW |