Bug 708464 - Create click to play UI for fennec native. r=mfinkle

This commit is contained in:
Margaret Leibovic 2011-12-09 14:04:19 -08:00
parent 70c591e442
commit 45b9a679f1
3 changed files with 114 additions and 0 deletions

View File

@ -441,6 +441,8 @@ pref("plugin.disable", true);
pref("dom.ipc.plugins.enabled", true);
#endif
pref("plugins.click_to_play", true);
// process priority
// higher values give content process less CPU time
#if MOZ_PLATFORM_MAEMO == 5

View File

@ -978,6 +978,8 @@ function Tab(aURL, aParams) {
pageWidth: 1, pageHeight: 1, zoom: 1.0 };
this.viewportExcess = { x: 0, y: 0 };
this.userScrollPos = { x: 0, y: 0 };
this._pluginsToPlay = [];
this._pluginOverlayShowing = false;
}
Tab.prototype = {
@ -1022,6 +1024,8 @@ Tab.prototype = {
this.browser.addEventListener("DOMLinkAdded", this, true);
this.browser.addEventListener("DOMTitleChanged", this, true);
this.browser.addEventListener("scroll", this, true);
this.browser.addEventListener("PluginClickToPlay", this, true);
this.browser.addEventListener("pagehide", this, true);
Services.obs.addObserver(this, "http-on-modify-request", false);
this.browser.loadURI(aURL);
},
@ -1058,6 +1062,8 @@ Tab.prototype = {
this.browser.removeEventListener("DOMLinkAdded", this, true);
this.browser.removeEventListener("DOMTitleChanged", this, true);
this.browser.removeEventListener("scroll", this, true);
this.browser.removeEventListener("PluginClickToPlay", this, true);
this.browser.removeEventListener("pagehide", this, true);
BrowserApp.deck.removeChild(this.vbox);
Services.obs.removeObserver(this, "http-on-modify-request", false);
this.browser = null;
@ -1233,6 +1239,10 @@ Tab.prototype = {
}.bind(this), true);
}
// Show a plugin doorhanger if there are no clickable overlays showing
if (this._pluginsToPlay.length && !this._pluginOverlayShowing)
PluginHelper.showDoorHanger(this);
break;
}
@ -1302,6 +1312,35 @@ Tab.prototype = {
}
break;
}
case "PluginClickToPlay": {
let plugin = aEvent.target;
// Keep track of all the plugins on the current page
this._pluginsToPlay.push(plugin);
let overlay = plugin.ownerDocument.getAnonymousElementByAttribute(plugin, "class", "mainBox");
if (!overlay)
return;
// If the overlay is too small, hide the overlay and act like this
// is a hidden plugin object
if (PluginHelper.isTooSmall(plugin, overlay)) {
overlay.style.visibility = "hidden";
return;
}
// Play all the plugin objects when the user clicks on one
PluginHelper.addPluginClickCallback(plugin, "playAllPlugins", this);
this._pluginOverlayShowing = true;
break;
}
case "pagehide": {
// Reset plugin state when we leave the page
this._pluginsToPlay = [];
this._pluginOverlayShowing = false;
break;
}
}
},
@ -2849,3 +2888,71 @@ var ConsoleAPI = {
return aSourceURL;
}
};
var PluginHelper = {
showDoorHanger: function(aTab) {
let message = Strings.browser.GetStringFromName("clickToPlayFlash.message");
let buttons = [
{
label: Strings.browser.GetStringFromName("clickToPlayFlash.yes"),
callback: function() {
PluginHelper.playAllPlugins(aTab);
}
},
{
label: Strings.browser.GetStringFromName("clickToPlayFlash.no"),
callback: function() {
// Do nothing
}
}
]
NativeWindow.doorhanger.show(message, "ask-to-play-plugins", buttons, aTab.id);
},
playAllPlugins: function(aTab) {
let plugins = aTab._pluginsToPlay;
for (let i = 0; i < plugins.length; i++) {
let objLoadingContent = plugins[i].QueryInterface(Ci.nsIObjectLoadingContent);
objLoadingContent.playPlugin();
}
},
// Mostly copied from /browser/base/content/browser.js
addPluginClickCallback: function (plugin, callbackName /*callbackArgs...*/) {
// XXX just doing (callback)(arg) was giving a same-origin error. bug?
let self = this;
let callbackArgs = Array.prototype.slice.call(arguments).slice(2);
plugin.addEventListener("click", function(evt) {
if (!evt.isTrusted)
return;
evt.preventDefault();
if (callbackArgs.length == 0)
callbackArgs = [ evt ];
(self[callbackName]).apply(self, callbackArgs);
}, true);
plugin.addEventListener("keydown", function(evt) {
if (!evt.isTrusted)
return;
if (evt.keyCode == evt.DOM_VK_RETURN) {
evt.preventDefault();
if (callbackArgs.length == 0)
callbackArgs = [ evt ];
evt.preventDefault();
(self[callbackName]).apply(self, callbackArgs);
}
}, true);
},
// Copied from /browser/base/content/browser.js
isTooSmall : function (plugin, overlay) {
// Is the <object>'s size too small to hold what we want to show?
let pluginRect = plugin.getBoundingClientRect();
// XXX bug 446693. The text-shadow on the submitted-report text at
// the bottom causes scrollHeight to be larger than it should be.
let overflows = (overlay.scrollWidth > pluginRect.width) ||
(overlay.scrollHeight - 5 > pluginRect.height);
return overflows;
}
};

View File

@ -273,3 +273,8 @@ timer.start=%S: timer started
# This string is used to display the result of the console.timeEnd() call.
# %1$S=name of timer, %2$S=number of milliseconds
timer.end=%1$S: %2$Sms
# Click to play plugins
clickToPlayFlash.message=This page contains flash content. Would you like to play it?
clickToPlayFlash.yes=Yes
clickToPlayFlash.no=No