Bug 1101825 - Create UITour event framework. r=MattN

This commit is contained in:
Blair McBride 2014-12-05 14:50:45 +13:00
parent 1e8cb75575
commit 13136e7c82
5 changed files with 127 additions and 10 deletions

View File

@ -12,6 +12,7 @@ let UITourListener = {
return;
}
addMessageListener("UITour:SendPageCallback", this);
addMessageListener("UITour:SendPageNotification", this);
sendAsyncMessage("UITour:onPageEvent", {detail: event.detail, type: event.type});
},
@ -68,14 +69,18 @@ let UITourListener = {
receiveMessage: function(aMessage) {
switch (aMessage.name) {
case "UITour:SendPageCallback":
this.sendPageCallback(aMessage.data);
this.sendPageEvent("Response", aMessage.data);
break;
}
case "UITour:SendPageNotification":
this.sendPageEvent("Notification", aMessage.data);
break;
}
},
sendPageCallback: function (detail) {
sendPageEvent: function (type, detail) {
let doc = content.document;
let event = new doc.defaultView.CustomEvent("mozUITourResponse", {
let eventName = "mozUITour" + type;
let event = new doc.defaultView.CustomEvent(eventName, {
bubbles: true,
detail: Cu.cloneInto(detail, doc.defaultView)
});

View File

@ -608,6 +608,12 @@ this.UITour = {
}).then(null, Cu.reportError);
break;
}
case "ping": {
if (typeof data.callbackID == "string")
this.sendPageCallback(messageManager, data.callbackID);
break;
}
}
if (!window.gMultiProcessBrowser) { // Non-e10s. See bug 1089000.
@ -1622,7 +1628,29 @@ this.UITour = {
reject("Search engine not available");
});
});
}
},
notify(eventName, params) {
let winEnum = Services.wm.getEnumerator("navigator:browser");
while (winEnum.hasMoreElements()) {
let window = winEnum.getNext();
if (window.closed)
continue;
debugger;
let originTabs = this.originTabs.get(window);
if (!originTabs)
continue;
for (let tab of originTabs) {
let messageManager = tab.linkedBrowser.messageManager;
let detail = {
event: eventName,
params: params,
};
messageManager.sendAsyncMessage("UITour:SendPageNotification", detail);
}
}
},
};
this.UITour.init();

View File

@ -33,6 +33,8 @@ skip-if = e10s # Bug 941428 - UITour.jsm not e10s friendly.
[browser_UITour_modalDialog.js]
run-if = os == "mac" # modal dialog disabling only working on OS X
skip-if = e10s # Bug 941428 - UITour.jsm not e10s friendly
[browser_UITour_observe.js]
skip-if = e10s # Bug 941428 - UITour.jsm not e10s friendly.
[browser_UITour_panel_close_annotation.js]
skip-if = true # Disabled due to frequent failures, bugs 1026310 and 1032137
[browser_UITour_registerPageID.js]

View File

@ -0,0 +1,51 @@
"use strict";
let gTestTab;
let gContentAPI;
let gContentWindow;
Components.utils.import("resource:///modules/UITour.jsm");
function test() {
requestLongerTimeout(2);
UITourTest();
}
let tests = [
function test_no_params(done) {
function listener(event, params) {
is(event, "test-event-1", "Correct event name");
is(params, null, "No param object");
gContentAPI.observe(null);
done();
}
gContentAPI.observe(listener, () => {
UITour.notify("test-event-1");
});
},
function test_param_string(done) {
function listener(event, params) {
is(event, "test-event-2", "Correct event name");
is(params, "a param", "Correct param string");
gContentAPI.observe(null);
done();
}
gContentAPI.observe(listener, () => {
UITour.notify("test-event-2", "a param");
});
},
function test_param_object(done) {
function listener(event, params) {
is(event, "test-event-3", "Correct event name");
is(JSON.stringify(params), JSON.stringify({key: "something"}), "Correct param object");
gContentAPI.observe(null);
done();
}
gContentAPI.observe(listener, () => {
UITour.notify("test-event-3", {key: "something"});
});
},
];

View File

@ -43,23 +43,54 @@ if (typeof Mozilla == 'undefined') {
var id = _generateCallbackID();
function listener(event) {
if (typeof event.detail != "object")
if (typeof event.detail != 'object')
return;
if (event.detail.callbackID != id)
return;
document.removeEventListener("mozUITourResponse", listener);
document.removeEventListener('mozUITourResponse', listener);
callback(event.detail.data);
}
document.addEventListener("mozUITourResponse", listener);
document.addEventListener('mozUITourResponse', listener);
return id;
}
var notificationListener = null;
function _notificationListener(event) {
if (typeof event.detail != 'object')
return;
if (typeof notificationListener != 'function')
return;
notificationListener(event.detail.event, event.detail.params);
}
Mozilla.UITour.DEFAULT_THEME_CYCLE_DELAY = 10 * 1000;
Mozilla.UITour.CONFIGNAME_SYNC = "sync";
Mozilla.UITour.CONFIGNAME_AVAILABLETARGETS = "availableTargets";
Mozilla.UITour.CONFIGNAME_SYNC = 'sync';
Mozilla.UITour.CONFIGNAME_AVAILABLETARGETS = 'availableTargets';
Mozilla.UITour.ping = function(callback) {
var data = {};
if (callback) {
data.callbackID = _waitForCallback(callback);
}
_sendEvent('ping', data);
};
Mozilla.UITour.observe = function(listener, callback) {
notificationListener = listener;
if (listener) {
document.addEventListener('mozUITourNotification',
_notificationListener);
Mozilla.UITour.ping(callback);
} else {
document.removeEventListener('mozUITourNotification',
_notificationListener);
}
};
Mozilla.UITour.registerPageID = function(pageID) {
_sendEvent('registerPageID', {