blob: 2fa72c99f9fa0ab5ee958dc22360cd74bc0e8238 [file] [log] [blame]
Hongchan Choi7dd0b3e2019-05-13 21:19:031// Copyright 2019 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5/**
6 * @implements {SDK.SDKModelObserver<!WebAudio.WebAudioModel>}
7 */
8WebAudio.WebAudioView = class extends UI.ThrottledWidget {
9 constructor() {
10 super(true, 1000);
11 this.element.classList.add('web-audio-drawer');
12 this.registerRequiredCSS('web_audio/webAudio.css');
13
14 // Creates the toolbar.
15 const toolbarContainer = this.contentElement.createChild(
16 'div', 'web-audio-toolbar-container vbox');
17 this._contextSelector = new WebAudio.AudioContextSelector(ls`BaseAudioContexts`);
18 const toolbar = new UI.Toolbar('web-audio-toolbar', toolbarContainer);
19 toolbar.appendToolbarItem(UI.Toolbar.createActionButtonForId('components.collect-garbage'));
20 toolbar.appendSeparator();
21 toolbar.appendToolbarItem(this._contextSelector.toolbarItem());
22
23 // Creates the detail view.
24 this._detailViewContainer = this.contentElement.createChild('div', 'vbox flex-auto');
25
26 // Creates the landing page.
27 this._landingPage = new UI.VBox();
28 this._landingPage.contentElement.classList.add('web-audio-landing-page', 'fill');
29 this._landingPage.contentElement.appendChild(UI.html`
30 <div>
31 <p>${ls`Open a page that uses Web Audio API to start monitoring.`}</p>
32 </div>
33 `);
34 this._landingPage.show(this._detailViewContainer);
35
36 // Creates the summary bar.
37 this._summaryBarContainer = this.contentElement.createChild('div', 'web-audio-summary-container');
38
39 this._contextSelector.addEventListener(WebAudio.AudioContextSelector.Events.ContextSelected, event => {
40 const context =
41 /** @type {!Protocol.WebAudio.BaseAudioContext} */ (event.data);
42 this._updateDetailView(context);
43 this.doUpdate();
44 });
45
46 SDK.targetManager.observeModels(WebAudio.WebAudioModel, this);
47 }
48
49 /**
50 * @override
51 */
52 wasShown() {
53 for (const model of SDK.targetManager.models(WebAudio.WebAudioModel))
54 this._addEventListeners(model);
55 }
56
57 /**
58 * @override
59 */
60 willHide() {
61 for (const model of SDK.targetManager.models(WebAudio.WebAudioModel))
62 this._removeEventListeners(model);
63 }
64
65 /**
66 * @override
67 * @param {!WebAudio.WebAudioModel} webAudioModel
68 */
69 modelAdded(webAudioModel) {
70 if (this.isShowing())
71 this._addEventListeners(webAudioModel);
72 }
73
74 /**
75 * @override
76 * @param {!WebAudio.WebAudioModel} webAudioModel
77 */
78 modelRemoved(webAudioModel) {
79 this._removeEventListeners(webAudioModel);
80 }
81
82 /**
83 * @override
84 * @return {!Promise<?>}
85 */
86 async doUpdate() {
87 await this._pollRealtimeData();
88 this.update();
89 }
90
91 /**
92 * @param {!WebAudio.WebAudioModel} webAudioModel
93 */
94 _addEventListeners(webAudioModel) {
95 webAudioModel.ensureEnabled();
96 webAudioModel.addEventListener(WebAudio.WebAudioModel.Events.ContextCreated, this._contextCreated, this);
97 webAudioModel.addEventListener(WebAudio.WebAudioModel.Events.ContextDestroyed, this._contextDestroyed, this);
98 webAudioModel.addEventListener(WebAudio.WebAudioModel.Events.ContextChanged, this._contextChanged, this);
Hongchan Choi2aae57a2019-05-23 20:42:4199 webAudioModel.addEventListener(WebAudio.WebAudioModel.Events.ModelReset, this._reset, this);
Hongchan Choi7dd0b3e2019-05-13 21:19:03100 }
101
102 /**
103 * @param {!WebAudio.WebAudioModel} webAudioModel
104 */
105 _removeEventListeners(webAudioModel) {
106 webAudioModel.removeEventListener(WebAudio.WebAudioModel.Events.ContextCreated, this._contextCreated, this);
107 webAudioModel.removeEventListener(WebAudio.WebAudioModel.Events.ContextDestroyed, this._contextDestroyed, this);
108 webAudioModel.removeEventListener(WebAudio.WebAudioModel.Events.ContextChanged, this._contextChanged, this);
Hongchan Choi2aae57a2019-05-23 20:42:41109 webAudioModel.removeEventListener(WebAudio.WebAudioModel.Events.ModelReset, this._reset, this);
Hongchan Choi7dd0b3e2019-05-13 21:19:03110 }
111
112 /**
113 * @param {!Common.Event} event
114 */
115 _contextCreated(event) {
116 this._contextSelector.contextCreated(event);
117 }
118
119 /**
120 * @param {!Common.Event} event
121 */
122 _contextDestroyed(event) {
123 this._contextSelector.contextDestroyed(event);
124 }
125
126 /**
127 * @param {!Common.Event} event
128 */
129 _contextChanged(event) {
130 this._contextSelector.contextChanged(event);
131 }
132
133 _reset() {
134 if (this._landingPage.isShowing())
135 this._landingPage.detach();
136 this._contextSelector.reset();
137 this._detailViewContainer.removeChildren();
138 this._landingPage.show(this._detailViewContainer);
139 }
140
141 /**
142 * @param {!Protocol.WebAudio.BaseAudioContext} context
143 */
144 _updateDetailView(context) {
145 if (this._landingPage.isShowing())
146 this._landingPage.detach();
147 const detailBuilder = new WebAudio.ContextDetailBuilder(context);
148 this._detailViewContainer.removeChildren();
149 this._detailViewContainer.appendChild(detailBuilder.getFragment());
150 }
151
152 /**
153 * @param {!Protocol.WebAudio.ContextId} contextId
154 * @param {!Protocol.WebAudio.ContextRealtimeData} contextRealtimeData
155 */
156 _updateSummaryBar(contextId, contextRealtimeData) {
157 const summaryBuilder =
158 new WebAudio.AudioContextSummaryBuilder(contextId, contextRealtimeData);
159 this._summaryBarContainer.removeChildren();
160 this._summaryBarContainer.appendChild(summaryBuilder.getFragment());
161 }
162
163 _clearSummaryBar() {
164 this._summaryBarContainer.removeChildren();
165 }
166
167 async _pollRealtimeData() {
168 const context = this._contextSelector.selectedContext();
169 if (!context) {
170 this._clearSummaryBar();
171 return;
172 }
173
174 for (const model of SDK.targetManager.models(WebAudio.WebAudioModel)) {
175 // Display summary only for real-time context.
176 if (context.contextType === 'realtime') {
177 const realtimeData = await model.requestRealtimeData(context.contextId);
Hongchan Choi17a74252019-06-05 18:27:58178 if (realtimeData)
Hongchan Choi7dd0b3e2019-05-13 21:19:03179 this._updateSummaryBar(context.contextId, realtimeData);
180 } else {
181 this._clearSummaryBar();
182 }
183 }
184 }
185};