2016-02-11 19:05:41 +03:00
|
|
|
/* 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/. */
|
|
|
|
|
2016-02-03 21:52:37 +03:00
|
|
|
"use strict";
|
2016-02-11 19:05:41 +03:00
|
|
|
|
2016-02-03 22:41:03 +03:00
|
|
|
const {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components;
|
2016-02-11 19:05:41 +03:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
|
2016-02-03 21:52:37 +03:00
|
|
|
this.EXPORTED_SYMBOLS = ["frame"];
|
|
|
|
|
|
|
|
this.frame = {};
|
|
|
|
|
|
|
|
const FRAME_SCRIPT = "chrome://marionette/content/listener.js";
|
2016-02-11 19:05:41 +03:00
|
|
|
|
2016-02-03 21:52:37 +03:00
|
|
|
// list of OOP frames that has the frame script loaded
|
2016-02-11 19:05:41 +03:00
|
|
|
var remoteFrames = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An object representing a frame that Marionette has loaded a
|
|
|
|
* frame script in.
|
|
|
|
*/
|
2016-02-03 21:52:37 +03:00
|
|
|
frame.RemoteFrame = function(windowId, frameId) {
|
|
|
|
// outerWindowId relative to main process
|
|
|
|
this.windowId = windowId;
|
|
|
|
// actual frame relative to the windowId's frames list
|
|
|
|
this.frameId = frameId;
|
|
|
|
// assigned frame ID, used for messaging
|
|
|
|
this.targetFrameId = this.frameId;
|
|
|
|
// list of OOP frames that has the frame script loaded
|
|
|
|
this.remoteFrames = [];
|
2016-02-11 19:05:41 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-02-03 21:52:37 +03:00
|
|
|
* The FrameManager will maintain the list of Out Of Process (OOP)
|
|
|
|
* frames and will handle frame switching between them.
|
2016-02-11 19:05:41 +03:00
|
|
|
*
|
2016-02-03 21:52:37 +03:00
|
|
|
* It handles explicit frame switching (switchToFrame), and implicit
|
|
|
|
* frame switching, which occurs when a modal dialog is triggered in B2G.
|
2016-02-03 22:41:03 +03:00
|
|
|
*
|
|
|
|
* @param {GeckoDriver} driver
|
|
|
|
* Reference to the driver instance.
|
2016-02-11 19:05:41 +03:00
|
|
|
*/
|
2016-02-03 22:41:03 +03:00
|
|
|
frame.Manager = class {
|
|
|
|
constructor(driver) {
|
|
|
|
// messageManager maintains the messageManager
|
|
|
|
// for the current process' chrome frame or the global message manager
|
2016-02-11 19:05:41 +03:00
|
|
|
|
2016-02-03 22:41:03 +03:00
|
|
|
// holds a member of the remoteFrames (for an OOP frame)
|
|
|
|
// or null (for the main process)
|
|
|
|
this.currentRemoteFrame = null;
|
|
|
|
// frame we'll need to restore once interrupt is gone
|
|
|
|
this.previousRemoteFrame = null;
|
|
|
|
// set to true when we have been interrupted by a modal
|
|
|
|
this.handledModal = false;
|
|
|
|
this.driver = driver;
|
|
|
|
}
|
2016-02-11 19:05:41 +03:00
|
|
|
|
|
|
|
/**
|
2016-02-03 21:52:37 +03:00
|
|
|
* Receives all messages from content messageManager.
|
2016-02-11 19:05:41 +03:00
|
|
|
*/
|
2016-02-03 22:41:03 +03:00
|
|
|
receiveMessage(message) {
|
2016-02-11 19:05:41 +03:00
|
|
|
switch (message.name) {
|
|
|
|
case "MarionetteFrame:getInterruptedState":
|
2016-02-03 22:41:03 +03:00
|
|
|
// this will return true if the calling frame was interrupted by a modal dialog
|
2016-02-11 19:05:41 +03:00
|
|
|
if (this.previousRemoteFrame) {
|
2016-02-03 22:41:03 +03:00
|
|
|
// get the frame window of the interrupted frame
|
|
|
|
let interruptedFrame = Services.wm.getOuterWindowWithId(
|
|
|
|
this.previousRemoteFrame.windowId);
|
|
|
|
|
|
|
|
if (this.previousRemoteFrame.frameId !== null) {
|
|
|
|
// find OOP frame
|
|
|
|
let iframes = interruptedFrame.document.getElementsByTagName("iframe");
|
|
|
|
interruptedFrame = iframes[this.previousRemoteFrame.frameId];
|
2016-02-11 19:05:41 +03:00
|
|
|
}
|
2016-02-03 22:41:03 +03:00
|
|
|
|
|
|
|
// check if the interrupted frame is the same as the calling frame
|
2016-02-11 19:05:41 +03:00
|
|
|
if (interruptedFrame.src == message.target.src) {
|
|
|
|
return {value: this.handledModal};
|
|
|
|
}
|
2016-02-03 22:41:03 +03:00
|
|
|
|
|
|
|
// we get here if previousRemoteFrame and currentRemoteFrame are null,
|
|
|
|
// i.e. if we're in a non-OOP process, or we haven't switched into an OOP frame,
|
|
|
|
// in which case, handledModal can't be set to true
|
|
|
|
} else if (this.currentRemoteFrame === null) {
|
2016-02-11 19:05:41 +03:00
|
|
|
return {value: this.handledModal};
|
|
|
|
}
|
|
|
|
return {value: false};
|
2016-02-03 21:52:37 +03:00
|
|
|
|
2016-02-03 22:41:03 +03:00
|
|
|
// handleModal is called when we need to switch frames to the main
|
|
|
|
// process due to a modal dialog interrupt
|
2016-02-11 19:05:41 +03:00
|
|
|
case "MarionetteFrame:handleModal":
|
2016-02-03 22:41:03 +03:00
|
|
|
// If previousRemoteFrame was set, that means we switched into a
|
|
|
|
// remote frame. If this is the case, then we want to switch back
|
|
|
|
// into the system frame. If it isn't the case, then we're in a
|
|
|
|
// non-OOP environment, so we don't need to handle remote frames.
|
2016-02-11 19:05:41 +03:00
|
|
|
let isLocal = true;
|
2016-02-03 22:41:03 +03:00
|
|
|
if (this.currentRemoteFrame !== null) {
|
2016-02-11 19:05:41 +03:00
|
|
|
isLocal = false;
|
2016-02-03 22:41:03 +03:00
|
|
|
this.removeMessageManagerListeners(
|
|
|
|
this.currentRemoteFrame.messageManager.get());
|
|
|
|
|
|
|
|
// store the previous frame so we can switch back to it when
|
|
|
|
// the modal is dismissed
|
2016-02-11 19:05:41 +03:00
|
|
|
this.previousRemoteFrame = this.currentRemoteFrame;
|
2016-02-03 22:41:03 +03:00
|
|
|
|
|
|
|
// by setting currentRemoteFrame to null,
|
|
|
|
// it signifies we're in the main process
|
2016-02-11 19:05:41 +03:00
|
|
|
this.currentRemoteFrame = null;
|
2016-02-03 22:41:03 +03:00
|
|
|
this.driver.messageManager = Cc["@mozilla.org/globalmessagemanager;1"]
|
|
|
|
.getService(Ci.nsIMessageBroadcaster);
|
2016-02-11 19:05:41 +03:00
|
|
|
}
|
2016-02-03 22:41:03 +03:00
|
|
|
|
2016-02-11 19:05:41 +03:00
|
|
|
this.handledModal = true;
|
2016-02-03 22:41:03 +03:00
|
|
|
this.driver.sendOk(this.driver.command_id);
|
2016-02-11 19:05:41 +03:00
|
|
|
return {value: isLocal};
|
2016-02-03 21:52:37 +03:00
|
|
|
|
2016-02-11 19:05:41 +03:00
|
|
|
case "MarionetteFrame:getCurrentFrameId":
|
2016-02-03 22:41:03 +03:00
|
|
|
if (this.currentRemoteFrame !== null) {
|
2016-02-11 19:05:41 +03:00
|
|
|
return this.currentRemoteFrame.frameId;
|
|
|
|
}
|
|
|
|
}
|
2016-02-03 22:41:03 +03:00
|
|
|
}
|
2016-02-11 19:05:41 +03:00
|
|
|
|
2016-02-03 22:41:03 +03:00
|
|
|
getOopFrame(winId, frameId) {
|
2016-02-11 19:05:41 +03:00
|
|
|
// get original frame window
|
|
|
|
let outerWin = Services.wm.getOuterWindowWithId(winId);
|
|
|
|
// find the OOP frame
|
|
|
|
let f = outerWin.document.getElementsByTagName("iframe")[frameId];
|
|
|
|
return f;
|
2016-02-03 22:41:03 +03:00
|
|
|
}
|
2016-02-11 19:05:41 +03:00
|
|
|
|
2016-02-03 22:41:03 +03:00
|
|
|
getFrameMM(winId, frameId) {
|
2016-02-11 19:05:41 +03:00
|
|
|
let oopFrame = this.getOopFrame(winId, frameId);
|
|
|
|
let mm = oopFrame.QueryInterface(Ci.nsIFrameLoaderOwner)
|
|
|
|
.frameLoader.messageManager;
|
|
|
|
return mm;
|
2016-02-03 22:41:03 +03:00
|
|
|
}
|
2016-02-11 19:05:41 +03:00
|
|
|
|
|
|
|
/**
|
2016-02-03 21:52:37 +03:00
|
|
|
* Switch to OOP frame. We're handling this here so we can maintain
|
|
|
|
* a list of remote frames.
|
2016-02-11 19:05:41 +03:00
|
|
|
*/
|
2016-02-03 22:41:03 +03:00
|
|
|
switchToFrame(winId, frameId) {
|
2016-02-11 19:05:41 +03:00
|
|
|
let oopFrame = this.getOopFrame(winId, frameId);
|
|
|
|
let mm = this.getFrameMM(winId, frameId);
|
|
|
|
|
2016-02-03 22:41:03 +03:00
|
|
|
// see if this frame already has our frame script loaded in it;
|
|
|
|
// if so, just wake it up
|
2016-02-11 19:05:41 +03:00
|
|
|
for (let i = 0; i < remoteFrames.length; i++) {
|
2016-02-03 22:41:03 +03:00
|
|
|
let f = remoteFrames[i];
|
|
|
|
let fmm = f.messageManager.get();
|
2016-02-11 19:05:41 +03:00
|
|
|
try {
|
2016-02-03 22:41:03 +03:00
|
|
|
fmm.sendAsyncMessage("aliveCheck", {});
|
2016-02-11 19:05:41 +03:00
|
|
|
} catch (e) {
|
2016-02-03 22:41:03 +03:00
|
|
|
if (e.result == Cr.NS_ERROR_NOT_INITIALIZED) {
|
2016-02-11 19:05:41 +03:00
|
|
|
remoteFrames.splice(i--, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2016-02-03 22:41:03 +03:00
|
|
|
|
|
|
|
if (fmm == mm) {
|
|
|
|
this.currentRemoteFrame = f;
|
2016-02-11 19:05:41 +03:00
|
|
|
this.addMessageManagerListeners(mm);
|
|
|
|
|
|
|
|
mm.sendAsyncMessage("Marionette:restart");
|
|
|
|
return oopFrame.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-03 22:41:03 +03:00
|
|
|
// if we get here, then we need to load the frame script in this frame,
|
2016-02-11 19:05:41 +03:00
|
|
|
// and set the frame's ChromeMessageSender as the active message manager
|
2016-02-03 22:41:03 +03:00
|
|
|
// the driver will listen to.
|
2016-02-11 19:05:41 +03:00
|
|
|
this.addMessageManagerListeners(mm);
|
2016-02-03 22:41:03 +03:00
|
|
|
let f = new frame.RemoteFrame(winId, frameId);
|
|
|
|
f.messageManager = Cu.getWeakReference(mm);
|
|
|
|
remoteFrames.push(f);
|
|
|
|
this.currentRemoteFrame = f;
|
2016-02-11 19:05:41 +03:00
|
|
|
|
|
|
|
mm.loadFrameScript(FRAME_SCRIPT, true, true);
|
|
|
|
|
|
|
|
return oopFrame.id;
|
2016-02-03 22:41:03 +03:00
|
|
|
}
|
2016-02-11 19:05:41 +03:00
|
|
|
|
|
|
|
/*
|
2016-02-03 22:41:03 +03:00
|
|
|
* This function handles switching back to the frame that was
|
|
|
|
* interrupted by the modal dialog. It gets called by the interrupted
|
|
|
|
* frame once the dialog is dismissed and the frame resumes its process.
|
2016-02-11 19:05:41 +03:00
|
|
|
*/
|
2016-02-03 22:41:03 +03:00
|
|
|
switchToModalOrigin() {
|
|
|
|
// only handle this if we indeed switched out of the modal's
|
|
|
|
// originating frame
|
|
|
|
if (this.previousRemoteFrame !== null) {
|
2016-02-11 19:05:41 +03:00
|
|
|
this.currentRemoteFrame = this.previousRemoteFrame;
|
2016-02-03 22:41:03 +03:00
|
|
|
let mm = this.currentRemoteFrame.messageManager.get();
|
|
|
|
this.addMessageManagerListeners(mm);
|
2016-02-11 19:05:41 +03:00
|
|
|
}
|
|
|
|
this.handledModal = false;
|
2016-02-03 22:41:03 +03:00
|
|
|
}
|
2016-02-11 19:05:41 +03:00
|
|
|
|
|
|
|
/**
|
2016-02-03 22:41:03 +03:00
|
|
|
* Adds message listeners to the driver, listening for
|
|
|
|
* messages from content frame scripts. It also adds a
|
|
|
|
* MarionetteFrame:getInterruptedState message listener to the
|
|
|
|
* FrameManager, so the frame manager's state can be checked by the frame.
|
2016-02-11 19:05:41 +03:00
|
|
|
*
|
|
|
|
* @param {nsIMessageListenerManager} mm
|
|
|
|
* The message manager object, typically
|
|
|
|
* ChromeMessageBroadcaster or ChromeMessageSender.
|
|
|
|
*/
|
2016-02-03 22:41:03 +03:00
|
|
|
addMessageManagerListeners(mm) {
|
|
|
|
mm.addWeakMessageListener("Marionette:ok", this.driver);
|
|
|
|
mm.addWeakMessageListener("Marionette:done", this.driver);
|
|
|
|
mm.addWeakMessageListener("Marionette:error", this.driver);
|
|
|
|
mm.addWeakMessageListener("Marionette:emitTouchEvent", this.driver);
|
|
|
|
mm.addWeakMessageListener("Marionette:log", this.driver);
|
|
|
|
mm.addWeakMessageListener("Marionette:shareData", this.driver);
|
|
|
|
mm.addWeakMessageListener("Marionette:switchToModalOrigin", this.driver);
|
|
|
|
mm.addWeakMessageListener("Marionette:switchedToFrame", this.driver);
|
|
|
|
mm.addWeakMessageListener("Marionette:getVisibleCookies", this.driver);
|
2016-02-29 21:52:30 +03:00
|
|
|
mm.addWeakMessageListener("Marionette:getImportedScripts", this.driver.importedScripts);
|
2016-02-03 22:41:03 +03:00
|
|
|
mm.addWeakMessageListener("Marionette:register", this.driver);
|
|
|
|
mm.addWeakMessageListener("Marionette:listenersAttached", this.driver);
|
|
|
|
mm.addWeakMessageListener("Marionette:getFiles", this.driver);
|
2016-02-11 19:05:41 +03:00
|
|
|
mm.addWeakMessageListener("MarionetteFrame:handleModal", this);
|
|
|
|
mm.addWeakMessageListener("MarionetteFrame:getCurrentFrameId", this);
|
|
|
|
mm.addWeakMessageListener("MarionetteFrame:getInterruptedState", this);
|
2016-02-03 22:41:03 +03:00
|
|
|
}
|
2016-02-11 19:05:41 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes listeners for messages from content frame scripts.
|
2016-02-03 22:41:03 +03:00
|
|
|
* We do not remove the MarionetteFrame:getInterruptedState or
|
|
|
|
* the Marionette:switchToModalOrigin message listener, because we
|
|
|
|
* want to allow all known frames to contact the frame manager so
|
|
|
|
* that it can check if it was interrupted, and if so, it will call
|
|
|
|
* switchToModalOrigin when its process gets resumed.
|
2016-02-11 19:05:41 +03:00
|
|
|
*
|
|
|
|
* @param {nsIMessageListenerManager} mm
|
|
|
|
* The message manager object, typically
|
|
|
|
* ChromeMessageBroadcaster or ChromeMessageSender.
|
|
|
|
*/
|
2016-02-03 22:41:03 +03:00
|
|
|
removeMessageManagerListeners(mm) {
|
|
|
|
mm.removeWeakMessageListener("Marionette:ok", this.driver);
|
|
|
|
mm.removeWeakMessageListener("Marionette:done", this.driver);
|
|
|
|
mm.removeWeakMessageListener("Marionette:error", this.driver);
|
|
|
|
mm.removeWeakMessageListener("Marionette:log", this.driver);
|
|
|
|
mm.removeWeakMessageListener("Marionette:shareData", this.driver);
|
|
|
|
mm.removeWeakMessageListener("Marionette:switchedToFrame", this.driver);
|
|
|
|
mm.removeWeakMessageListener("Marionette:getVisibleCookies", this.driver);
|
2016-02-29 21:52:30 +03:00
|
|
|
mm.removeWeakMessageListener("Marionette:getImportedScripts", this.driver.importedScripts);
|
2016-02-03 22:41:03 +03:00
|
|
|
mm.removeWeakMessageListener("Marionette:listenersAttached", this.driver);
|
|
|
|
mm.removeWeakMessageListener("Marionette:register", this.driver);
|
|
|
|
mm.removeWeakMessageListener("Marionette:getFiles", this.driver);
|
2016-02-11 19:05:41 +03:00
|
|
|
mm.removeWeakMessageListener("MarionetteFrame:handleModal", this);
|
|
|
|
mm.removeWeakMessageListener("MarionetteFrame:getCurrentFrameId", this);
|
|
|
|
}
|
|
|
|
};
|
2016-02-03 22:41:03 +03:00
|
|
|
|
|
|
|
frame.Manager.prototype.QueryInterface = XPCOMUtils.generateQI(
|
|
|
|
[Ci.nsIMessageListener, Ci.nsISupportsWeakReference]);
|