From 60b5a85bdc2f05d8d8e541dfc375e100025a5aed Mon Sep 17 00:00:00 2001 From: Evan Tseng Date: Fri, 12 Jun 2015 18:35:13 +0800 Subject: [PATCH] Bug 1165134 - Add new MozChromeEvent/MozContentEvent to allow System app to control its own audio channels. r=baku --HG-- extra : rebase_source : 3476900358289e38f3f7b11ec975b488967ee879 --- b2g/chrome/content/shell.js | 86 +++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/b2g/chrome/content/shell.js b/b2g/chrome/content/shell.js index abb0f45fd13c..931a041e1a86 100644 --- a/b2g/chrome/content/shell.js +++ b/b2g/chrome/content/shell.js @@ -328,6 +328,18 @@ var shell = { .sessionHistory = Cc["@mozilla.org/browser/shistory;1"] .createInstance(Ci.nsISHistory); + this.allowedAudioChannels = new Map(); + let audioChannels = systemAppFrame.allowedAudioChannels; + audioChannels && audioChannels.forEach(function(audioChannel) { + this.allowedAudioChannels.set(audioChannel.name, audioChannel); + audioChannel.addEventListener('activestatechanged', this); + // Set all audio channels as unmuted by default + // because some audio in System app will be played + // before AudioChannelService[1] is Gaia is loaded. + // [1]: https://github.com/mozilla-b2g/gaia/blob/master/apps/system/js/audio_channel_service.js + audioChannel.setMuted(false); + }.bind(this)); + // On firefox mulet, shell.html is loaded in a tab // and we have to listen on the chrome event handler // to catch key events @@ -561,6 +573,18 @@ var shell = { case 'unload': this.stop(); break; + case 'activestatechanged': + var channel = evt.target; + // TODO: We should get the `isActive` state from evt.isActive. + // Then we don't need to do `channel.isActive()` here. + channel.isActive().onsuccess = function(evt) { + this.sendChromeEvent({ + type: 'system-audiochannel-state-changed', + name: channel.name, + isActive: evt.target.result + }); + }.bind(this); + break; } }, @@ -739,6 +763,11 @@ var CustomEventManager = { case 'inputregistry-remove': KeyboardHelper.handleEvent(detail); break; + case 'system-audiochannel-list': + case 'system-audiochannel-mute': + case 'system-audiochannel-volume': + SystemAppMozBrowserHelper.handleEvent(detail); + break; case 'do-command': DoCommandHelper.handleEvent(detail.cmd); break; @@ -867,6 +896,63 @@ let KeyboardHelper = { } }; +let SystemAppMozBrowserHelper = { + handleEvent: function systemAppMozBrowser_handleEvent(detail) { + let request; + let name; + switch (detail.type) { + case 'system-audiochannel-list': + let audioChannels = []; + shell.allowedAudioChannels.forEach(function(value, name) { + audioChannels.push(name); + }); + SystemAppProxy._sendCustomEvent('mozSystemWindowChromeEvent', { + type: 'system-audiochannel-list', + audioChannels: audioChannels + }); + break; + case 'system-audiochannel-mute': + name = detail.name; + let isMuted = detail.isMuted; + request = shell.allowedAudioChannels.get(name).setMuted(isMuted); + request.onsuccess = function() { + SystemAppProxy._sendCustomEvent('mozSystemWindowChromeEvent', { + type: 'system-audiochannel-mute-onsuccess', + name: name, + isMuted: isMuted + }); + }; + request.onerror = function() { + SystemAppProxy._sendCustomEvent('mozSystemWindowChromeEvent', { + type: 'system-audiochannel-mute-onerror', + name: name, + isMuted: isMuted + }); + }; + break; + case 'system-audiochannel-volume': + name = detail.name; + let volume = detail.volume; + request = shell.allowedAudioChannels.get(name).setVolume(volume); + request.onsuccess = function() { + sSystemAppProxy._sendCustomEvent('mozSystemWindowChromeEvent', { + type: 'system-audiochannel-volume-onsuccess', + name: name, + volume: volume + }); + }; + request.onerror = function() { + SystemAppProxy._sendCustomEvent('mozSystemWindowChromeEvent', { + type: 'system-audiochannel-volume-onerror', + name: name, + volume: volume + }); + }; + break; + } + } +}; + // This is the backend for Gaia's screenshot feature. Gaia requests a // screenshot by sending a mozContentEvent with detail.type set to // 'take-screenshot'. Then we take a screenshot and send a