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