Bug 1429091 - Use global message manager in AsyncMessageChannel. r=maja_zf

Whenever we make proxied IPC calls to the content frame's message manager,
we do so over the global message manager.  AsyncMessageChannel takes a
closure that returns the current message manager from GeckoDriver#mm,
but this is unnecessary because it always holds a global message channel.

By not having to pass in a closure returning a message manager
to AsyncMessageChannel we losen the tight coupling a little bit.
Future patches will further reduce the tight coupling of browserFn.

MozReview-Commit-ID: EU0pkxA7lab

--HG--
extra : rebase_source : f6d6735e2d5bacdfbf20bde9a835f3f83846b2d6
This commit is contained in:
Andreas Tolfsen 2018-01-09 15:53:46 +00:00
parent d0c7964c6a
commit a48dd7b64b
3 changed files with 16 additions and 21 deletions

View File

@ -150,8 +150,8 @@ this.GeckoDriver = function(appId, server) {
this.capabilities = new session.Capabilities(); this.capabilities = new session.Capabilities();
this.mm = globalMessageManager; this.mm = globalMessageManager;
this.listener = proxy.toListener(() => this.mm, this.sendAsync.bind(this), this.listener = proxy.toListener(
() => this.curBrowser); this.sendAsync.bind(this), () => this.curBrowser);
// points to an alert instance if a modal dialog is present // points to an alert instance if a modal dialog is present
this.dialog = null; this.dialog = null;

View File

@ -627,9 +627,7 @@ function deregister() {
* an empty dictionary. * an empty dictionary.
*/ */
function sendToServer(uuid, data = undefined) { function sendToServer(uuid, data = undefined) {
let channel = new proxy.AsyncMessageChannel( let channel = new proxy.AsyncMessageChannel(sendAsyncMessage.bind(this));
() => this,
sendAsyncMessage.bind(this));
channel.reply(uuid, data); channel.reply(uuid, data);
} }

View File

@ -21,8 +21,11 @@ this.EXPORTED_SYMBOLS = ["proxy"];
XPCOMUtils.defineLazyServiceGetter( XPCOMUtils.defineLazyServiceGetter(
this, "uuidgen", "@mozilla.org/uuid-generator;1", "nsIUUIDGenerator"); this, "uuidgen", "@mozilla.org/uuid-generator;1", "nsIUUIDGenerator");
XPCOMUtils.defineLazyServiceGetter(
this, "globalMessageManager", "@mozilla.org/globalmessagemanager;1",
"nsIMessageBroadcaster");
const logger = Log.repository.getLogger("Marionette"); const log = Log.repository.getLogger("Marionette");
// Proxy handler that traps requests to get a property. Will prioritise // Proxy handler that traps requests to get a property. Will prioritise
// properties that exist on the object's own prototype. // properties that exist on the object's own prototype.
@ -50,14 +53,13 @@ this.proxy = {};
* passed literally. The latter specialisation is temporary to achieve * passed literally. The latter specialisation is temporary to achieve
* backwards compatibility with listener.js. * backwards compatibility with listener.js.
* *
* @param {function(): (nsIMessageSender|nsIMessageBroadcaster)} mmFn
* Closure function returning the current message manager.
* @param {function(string, Object, number)} sendAsyncFn * @param {function(string, Object, number)} sendAsyncFn
* Callback for sending async messages. * Callback for sending async messages.
* @param {function(): browser.Context} browserFn
* Closure that returns the current browsing context.
*/ */
proxy.toListener = function(mmFn, sendAsyncFn, browserFn) { proxy.toListener = function(sendAsyncFn, browserFn) {
let sender = new proxy.AsyncMessageChannel( let sender = new proxy.AsyncMessageChannel(sendAsyncFn, browserFn);
mmFn, sendAsyncFn, browserFn);
return new Proxy(sender, ownPriorityGetterTrap); return new Proxy(sender, ownPriorityGetterTrap);
}; };
@ -71,8 +73,7 @@ proxy.toListener = function(mmFn, sendAsyncFn, browserFn) {
* <code>.reply(...)</code>. * <code>.reply(...)</code>.
*/ */
proxy.AsyncMessageChannel = class { proxy.AsyncMessageChannel = class {
constructor(mmFn, sendAsyncFn, browserFn) { constructor(sendAsyncFn, browserFn) {
this.mmFn_ = mmFn;
this.sendAsync = sendAsyncFn; this.sendAsync = sendAsyncFn;
this.browserFn_ = browserFn; this.browserFn_ = browserFn;
@ -88,10 +89,6 @@ proxy.AsyncMessageChannel = class {
return this.browserFn_(); return this.browserFn_();
} }
get mm() {
return this.mmFn_();
}
/** /**
* Send a message across the channel. The name of the function to * Send a message across the channel. The name of the function to
* call must be registered as a message listener. * call must be registered as a message listener.
@ -150,7 +147,7 @@ proxy.AsyncMessageChannel = class {
// The currently selected tab or window has been closed. No clean-up // The currently selected tab or window has been closed. No clean-up
// is necessary to do because all loaded listeners are gone. // is necessary to do because all loaded listeners are gone.
this.closeHandler = ({type, target}) => { this.closeHandler = ({type, target}) => {
logger.debug(`Received DOM event ${type} for ${target}`); log.debug(`Received DOM event ${type} for ${target}`);
switch (type) { switch (type) {
case "TabClose": case "TabClose":
@ -165,7 +162,7 @@ proxy.AsyncMessageChannel = class {
// the active command has to be aborted. Therefore remove all handlers, // the active command has to be aborted. Therefore remove all handlers,
// and cancel any ongoing requests in the listener. // and cancel any ongoing requests in the listener.
this.dialogueObserver_ = (subject, topic) => { this.dialogueObserver_ = (subject, topic) => {
logger.debug(`Received observer notification ${topic}`); log.debug(`Received observer notification ${topic}`);
this.removeAllListeners_(); this.removeAllListeners_();
// TODO(ato): It's not ideal to have listener specific behaviour here: // TODO(ato): It's not ideal to have listener specific behaviour here:
@ -294,7 +291,7 @@ proxy.AsyncMessageChannel = class {
callback(msg); callback(msg);
}; };
this.mm.addMessageListener(path, autoRemover); globalMessageManager.addMessageListener(path, autoRemover);
this.listeners_.set(path, autoRemover); this.listeners_.set(path, autoRemover);
} }
@ -304,7 +301,7 @@ proxy.AsyncMessageChannel = class {
} }
let l = this.listeners_.get(path); let l = this.listeners_.get(path);
this.mm.removeMessageListener(path, l[1]); globalMessageManager.removeMessageListener(path, l[1]);
return this.listeners_.delete(path); return this.listeners_.delete(path);
} }