Bug 1052276 - Move toast code to a jsm module. r=margaret

This commit is contained in:
Wes Johnston 2014-08-14 13:48:46 -07:00
parent 6af5329b62
commit 6ea49644cf
4 changed files with 67 additions and 51 deletions

View File

@ -610,8 +610,7 @@ public abstract class GeckoApp
if (button != null) {
final String label = button.optString("label", "");
final String icon = button.optString("icon", "");
final String id = button.optString("id", "");
showButtonToast(msg, duration, label, icon, id);
showButtonToast(msg, duration, label, icon, callback);
} else {
showNormalToast(msg, duration);
}
@ -790,21 +789,21 @@ public abstract class GeckoApp
void showButtonToast(final String message, final String duration,
final String buttonText, final String buttonIcon,
final String buttonId) {
final EventCallback callback) {
BitmapUtils.getDrawable(GeckoApp.this, buttonIcon, new BitmapUtils.BitmapLoader() {
@Override
public void onBitmapFound(final Drawable d) {
final int toastDuration = duration.equals("long") ? ButtonToast.LENGTH_LONG : ButtonToast.LENGTH_SHORT;
getButtonToast().show(false, message, toastDuration ,buttonText, d, new ButtonToast.ToastListener() {
getButtonToast().show(false, message, toastDuration, buttonText, d, new ButtonToast.ToastListener() {
@Override
public void onButtonClicked() {
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Toast:Click", buttonId));
callback.sendSuccess(null);
}
@Override
public void onToastHidden(ButtonToast.ReasonHidden reason) {
if (reason == ButtonToast.ReasonHidden.TIMEOUT) {
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Toast:Hidden", buttonId));
callback.sendCancel();
}
}
});

View File

@ -1844,16 +1844,12 @@ var NativeWindow = {
init: function() {
Services.obs.addObserver(this, "Menu:Clicked", false);
Services.obs.addObserver(this, "Doorhanger:Reply", false);
Services.obs.addObserver(this, "Toast:Click", false);
Services.obs.addObserver(this, "Toast:Hidden", false);
this.contextmenus.init();
},
uninit: function() {
Services.obs.removeObserver(this, "Menu:Clicked");
Services.obs.removeObserver(this, "Doorhanger:Reply");
Services.obs.removeObserver(this, "Toast:Click", false);
Services.obs.removeObserver(this, "Toast:Hidden", false);
this.contextmenus.uninit();
},
@ -1872,38 +1868,6 @@ var NativeWindow = {
});
},
toast: {
_callbacks: {},
show: function(aMessage, aDuration, aOptions) {
let msg = {
type: "Toast:Show",
message: aMessage,
duration: aDuration
};
if (aOptions && aOptions.button) {
msg.button = {
id: uuidgen.generateUUID().toString(),
};
// null is badly handled by the receiver, so try to avoid including nulls.
if (aOptions.button.label) {
msg.button.label = aOptions.button.label;
}
if (aOptions.button.icon) {
// If the caller specified a button, make sure we convert any chrome urls
// to jar:jar urls so that the frontend can show them
msg.button.icon = resolveGeckoURI(aOptions.button.icon);
};
this._callbacks[msg.button.id] = aOptions.button.callback;
}
sendMessageToJava(msg);
}
},
menu: {
_callbacks: [],
_menuId: 1,
@ -2005,14 +1969,6 @@ var NativeWindow = {
if (aTopic == "Menu:Clicked") {
if (this.menu._callbacks[aData])
this.menu._callbacks[aData]();
} else if (aTopic == "Toast:Click") {
if (this.toast._callbacks[aData]) {
this.toast._callbacks[aData]();
delete this.toast._callbacks[aData];
}
} else if (aTopic == "Toast:Hidden") {
if (this.toast._callbacks[aData])
delete this.toast._callbacks[aData];
} else if (aTopic == "Doorhanger:Reply") {
let data = JSON.parse(aData);
let reply_id = data["callback"];
@ -2717,7 +2673,8 @@ XPCOMUtils.defineLazyModuleGetter(this, "PageActions",
// These alias to the old, deprecated NativeWindow interfaces
[
["pageactions", "resource://gre/modules/PageActions.jsm", "PageActions"]
["pageactions", "resource://gre/modules/PageActions.jsm", "PageActions"],
["toast", "resource://gre/modules/Toast.jsm", "Toast"],
].forEach(item => {
let [name, script, exprt] = item;

View File

@ -0,0 +1,59 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/Messaging.jsm");
this.EXPORTED_SYMBOLS = ["Toast"];
// Copied from browser.js
// TODO: We should move this method to a common importable location
function resolveGeckoURI(uri) {
if (!uri)
throw "Can't resolve an empty uri";
if (uri.startsWith("chrome://")) {
let registry = Cc['@mozilla.org/chrome/chrome-registry;1'].getService(Ci["nsIChromeRegistry"]);
return registry.convertChromeURL(Services.io.newURI(uri, null, null)).spec;
} else if (uri.startsWith("resource://")) {
let handler = Services.io.getProtocolHandler("resource").QueryInterface(Ci.nsIResProtocolHandler);
return handler.resolveURI(Services.io.newURI(uri, null, null));
}
return uri;
}
var Toast = {
show: function(message, duration, options) {
let msg = {
type: "Toast:Show",
message: message,
duration: duration
};
let callback;
if (options && options.button) {
msg.button = { };
// null is badly handled by the receiver, so try to avoid including nulls.
if (options.button.label) {
msg.button.label = options.button.label;
}
if (options.button.icon) {
// If the caller specified a button, make sure we convert any chrome urls
// to jar:jar urls so that the frontend can show them
msg.button.icon = resolveGeckoURI(options.button.icon);
};
callback = options.button.callback;
}
sendMessageToJava(msg, callback);
}
}

View File

@ -27,6 +27,7 @@ EXTRA_JS_MODULES += [
'SimpleServiceDiscovery.jsm',
'SSLExceptions.jsm',
'TabMirror.jsm',
'Toast.jsm',
'WebappManagerWorker.js',
]