2012-03-22 08:19:57 -07:00
|
|
|
/* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
2012-05-11 13:06:53 -07:00
|
|
|
let {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
|
|
|
|
|
|
|
|
let uuidGen = Cc["@mozilla.org/uuid-generator;1"]
|
|
|
|
.getService(Ci.nsIUUIDGenerator);
|
|
|
|
|
|
|
|
let loader = Cc["@mozilla.org/moz/jssubscript-loader;1"]
|
|
|
|
.getService(Ci.mozIJSSubScriptLoader);
|
2012-03-22 08:19:57 -07:00
|
|
|
|
|
|
|
loader.loadSubScript("chrome://marionette/content/marionette-simpletest.js");
|
|
|
|
loader.loadSubScript("chrome://marionette/content/marionette-log-obj.js");
|
2012-06-10 16:44:50 -07:00
|
|
|
loader.loadSubScript("chrome://marionette/content/marionette-perf.js");
|
2012-05-11 13:06:53 -07:00
|
|
|
Cu.import("chrome://marionette/content/marionette-elements.js");
|
2012-06-04 10:50:06 -07:00
|
|
|
Cu.import("resource://gre/modules/FileUtils.jsm");
|
2013-01-23 11:26:33 -08:00
|
|
|
Cu.import("resource://gre/modules/NetUtil.jsm");
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2012-04-10 17:28:08 -07:00
|
|
|
let utils = {};
|
|
|
|
utils.window = content;
|
2012-05-11 13:06:53 -07:00
|
|
|
// Load Event/ChromeUtils for use with JS scripts:
|
|
|
|
loader.loadSubScript("chrome://marionette/content/EventUtils.js", utils);
|
2012-04-10 17:28:08 -07:00
|
|
|
loader.loadSubScript("chrome://marionette/content/ChromeUtils.js", utils);
|
|
|
|
loader.loadSubScript("chrome://marionette/content/atoms.js", utils);
|
2012-08-05 12:49:42 -07:00
|
|
|
loader.loadSubScript("chrome://marionette/content/marionette-sendkeys.js", utils);
|
2012-05-18 13:30:13 -07:00
|
|
|
|
|
|
|
loader.loadSubScript("chrome://specialpowers/content/specialpowersAPI.js");
|
|
|
|
loader.loadSubScript("chrome://specialpowers/content/specialpowers.js");
|
|
|
|
|
2012-03-22 08:19:57 -07:00
|
|
|
let marionetteLogObj = new MarionetteLogObj();
|
2012-06-10 16:44:50 -07:00
|
|
|
let marionettePerf = new MarionettePerfData();
|
2012-03-22 08:19:57 -07:00
|
|
|
|
|
|
|
let isB2G = false;
|
|
|
|
|
2012-10-31 15:36:57 -07:00
|
|
|
let marionetteTestName;
|
2012-05-11 13:06:53 -07:00
|
|
|
let winUtil = content.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils);
|
2012-03-22 08:19:57 -07:00
|
|
|
let listenerId = null; //unique ID of this listener
|
|
|
|
let activeFrame = null;
|
2012-05-02 07:21:05 -07:00
|
|
|
let curWindow = content;
|
2012-03-22 08:19:57 -07:00
|
|
|
let elementManager = new ElementManager([]);
|
2012-12-14 08:11:02 -08:00
|
|
|
let importedScripts = null;
|
2012-03-22 08:19:57 -07:00
|
|
|
|
2012-05-09 12:05:39 -07:00
|
|
|
// The sandbox we execute test scripts in. Gets lazily created in
|
|
|
|
// createExecuteContentSandbox().
|
|
|
|
let sandbox;
|
|
|
|
|
2013-02-12 10:10:03 -08:00
|
|
|
// the unload handler
|
|
|
|
let onunload;
|
|
|
|
|
2012-05-09 12:05:39 -07:00
|
|
|
// Flag to indicate whether an async script is currently running or not.
|
|
|
|
let asyncTestRunning = false;
|
|
|
|
let asyncTestCommandId;
|
|
|
|
let asyncTestTimeoutId;
|
2012-12-18 06:21:28 -08:00
|
|
|
let originalOnError;
|
2012-11-14 10:35:44 -08:00
|
|
|
//timer for doc changes
|
|
|
|
let checkTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
2013-01-22 11:27:44 -08:00
|
|
|
// Send move events about this often
|
|
|
|
let EVENT_INTERVAL = 30; // milliseconds
|
|
|
|
// The current array of all pending touches
|
|
|
|
let touches = [];
|
|
|
|
// For assigning unique ids to all touches
|
|
|
|
let nextTouchId = 1000;
|
2013-03-04 17:09:58 -08:00
|
|
|
let touchIds = {};
|
2013-03-18 13:42:46 -07:00
|
|
|
// last touch for each fingerId
|
|
|
|
let multiLast = {};
|
2013-03-04 17:09:58 -08:00
|
|
|
// last touch for single finger
|
|
|
|
let lastTouch = null;
|
2013-04-10 16:55:43 -07:00
|
|
|
// last touch type
|
|
|
|
let isTouchStart = false;
|
|
|
|
// whether to send mouse event
|
|
|
|
let mouseEvent = true;
|
2012-03-22 08:19:57 -07:00
|
|
|
/**
|
|
|
|
* Called when listener is first started up.
|
|
|
|
* The listener sends its unique window ID and its current URI to the actor.
|
|
|
|
* If the actor returns an ID, we start the listeners. Otherwise, nothing happens.
|
|
|
|
*/
|
|
|
|
function registerSelf() {
|
2012-11-26 18:19:04 -08:00
|
|
|
let msg = {value: winUtil.outerWindowID, href: content.location.href};
|
|
|
|
let register = sendSyncMessage("Marionette:register", msg);
|
|
|
|
|
2012-03-22 08:19:57 -07:00
|
|
|
if (register[0]) {
|
2012-12-14 08:11:02 -08:00
|
|
|
listenerId = register[0].id;
|
|
|
|
importedScripts = FileUtils.File(register[0].importedScripts);
|
2012-03-22 08:19:57 -07:00
|
|
|
startListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-11 13:06:53 -07:00
|
|
|
/**
|
|
|
|
* Add a message listener that's tied to our listenerId.
|
|
|
|
*/
|
|
|
|
function addMessageListenerId(messageName, handler) {
|
|
|
|
addMessageListener(messageName + listenerId, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a message listener that's tied to our listenerId.
|
|
|
|
*/
|
|
|
|
function removeMessageListenerId(messageName, handler) {
|
|
|
|
removeMessageListener(messageName + listenerId, handler);
|
|
|
|
}
|
|
|
|
|
2012-03-22 08:19:57 -07:00
|
|
|
/**
|
|
|
|
* Start all message listeners
|
|
|
|
*/
|
|
|
|
function startListeners() {
|
2012-05-11 13:06:53 -07:00
|
|
|
addMessageListenerId("Marionette:newSession", newSession);
|
|
|
|
addMessageListenerId("Marionette:executeScript", executeScript);
|
|
|
|
addMessageListenerId("Marionette:executeAsyncScript", executeAsyncScript);
|
|
|
|
addMessageListenerId("Marionette:executeJSScript", executeJSScript);
|
2013-01-22 11:27:44 -08:00
|
|
|
addMessageListenerId("Marionette:singleTap", singleTap);
|
|
|
|
addMessageListenerId("Marionette:doubleTap", doubleTap);
|
2013-02-06 11:58:14 -08:00
|
|
|
addMessageListenerId("Marionette:press", press);
|
|
|
|
addMessageListenerId("Marionette:release", release);
|
2013-03-19 14:59:48 -07:00
|
|
|
addMessageListenerId("Marionette:cancelTouch", cancelTouch);
|
2013-03-04 17:09:58 -08:00
|
|
|
addMessageListenerId("Marionette:actionChain", actionChain);
|
2013-03-18 13:42:46 -07:00
|
|
|
addMessageListenerId("Marionette:multiAction", multiAction);
|
2013-04-10 16:55:43 -07:00
|
|
|
addMessageListenerId("Marionette:sendMouseEvent", sendMouseEvent);
|
2012-05-11 13:06:53 -07:00
|
|
|
addMessageListenerId("Marionette:setSearchTimeout", setSearchTimeout);
|
|
|
|
addMessageListenerId("Marionette:goUrl", goUrl);
|
|
|
|
addMessageListenerId("Marionette:getUrl", getUrl);
|
2012-07-10 13:30:21 -07:00
|
|
|
addMessageListenerId("Marionette:getTitle", getTitle);
|
2012-08-09 12:31:12 -07:00
|
|
|
addMessageListenerId("Marionette:getPageSource", getPageSource);
|
2012-05-11 13:06:53 -07:00
|
|
|
addMessageListenerId("Marionette:goBack", goBack);
|
|
|
|
addMessageListenerId("Marionette:goForward", goForward);
|
|
|
|
addMessageListenerId("Marionette:refresh", refresh);
|
|
|
|
addMessageListenerId("Marionette:findElementContent", findElementContent);
|
|
|
|
addMessageListenerId("Marionette:findElementsContent", findElementsContent);
|
2013-01-14 06:57:54 -08:00
|
|
|
addMessageListenerId("Marionette:getActiveElement", getActiveElement);
|
2012-05-11 13:06:53 -07:00
|
|
|
addMessageListenerId("Marionette:clickElement", clickElement);
|
|
|
|
addMessageListenerId("Marionette:getElementAttribute", getElementAttribute);
|
|
|
|
addMessageListenerId("Marionette:getElementText", getElementText);
|
2012-08-08 13:21:50 -07:00
|
|
|
addMessageListenerId("Marionette:getElementTagName", getElementTagName);
|
2012-05-11 13:06:53 -07:00
|
|
|
addMessageListenerId("Marionette:isElementDisplayed", isElementDisplayed);
|
2012-10-19 13:59:15 -07:00
|
|
|
addMessageListenerId("Marionette:getElementSize", getElementSize);
|
2012-05-11 13:06:53 -07:00
|
|
|
addMessageListenerId("Marionette:isElementEnabled", isElementEnabled);
|
|
|
|
addMessageListenerId("Marionette:isElementSelected", isElementSelected);
|
|
|
|
addMessageListenerId("Marionette:sendKeysToElement", sendKeysToElement);
|
2012-10-19 13:35:30 -07:00
|
|
|
addMessageListenerId("Marionette:getElementPosition", getElementPosition);
|
2012-05-11 13:06:53 -07:00
|
|
|
addMessageListenerId("Marionette:clearElement", clearElement);
|
|
|
|
addMessageListenerId("Marionette:switchToFrame", switchToFrame);
|
|
|
|
addMessageListenerId("Marionette:deleteSession", deleteSession);
|
|
|
|
addMessageListenerId("Marionette:sleepSession", sleepSession);
|
2012-05-18 13:30:43 -07:00
|
|
|
addMessageListenerId("Marionette:emulatorCmdResult", emulatorCmdResult);
|
2012-06-04 10:50:06 -07:00
|
|
|
addMessageListenerId("Marionette:importScript", importScript);
|
2012-08-23 15:07:16 -07:00
|
|
|
addMessageListenerId("Marionette:getAppCacheStatus", getAppCacheStatus);
|
2012-10-31 15:36:57 -07:00
|
|
|
addMessageListenerId("Marionette:setTestName", setTestName);
|
2012-11-21 17:08:11 -08:00
|
|
|
addMessageListenerId("Marionette:setState", setState);
|
2012-11-30 15:25:26 -08:00
|
|
|
addMessageListenerId("Marionette:screenShot", screenShot);
|
2012-11-22 07:53:44 -08:00
|
|
|
addMessageListenerId("Marionette:addCookie", addCookie);
|
|
|
|
addMessageListenerId("Marionette:getAllCookies", getAllCookies);
|
|
|
|
addMessageListenerId("Marionette:deleteAllCookies", deleteAllCookies);
|
|
|
|
addMessageListenerId("Marionette:deleteCookie", deleteCookie);
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when we start a new session. It registers the
|
|
|
|
* current environment, and resets all values
|
|
|
|
*/
|
|
|
|
function newSession(msg) {
|
|
|
|
isB2G = msg.json.B2G;
|
|
|
|
resetValues();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Puts the current session to sleep, so all listeners are removed except
|
|
|
|
* for the 'restart' listener. This is used to keep the content listener
|
|
|
|
* alive for reuse in B2G instead of reloading it each time.
|
|
|
|
*/
|
|
|
|
function sleepSession(msg) {
|
|
|
|
deleteSession();
|
|
|
|
addMessageListener("Marionette:restart", restart);
|
|
|
|
}
|
|
|
|
|
2012-11-21 17:08:11 -08:00
|
|
|
/**
|
|
|
|
* Sets script-wide state variables; used after frame switching.
|
|
|
|
*/
|
|
|
|
function setState(msg) {
|
|
|
|
marionetteTimeout = msg.json.scriptTimeout;
|
|
|
|
try {
|
|
|
|
elementManager.setSearchTimeout(msg.json.searchTimeout);
|
|
|
|
}
|
|
|
|
catch (e) {
|
2012-11-26 18:19:04 -08:00
|
|
|
sendError(e.message, e.code, e.stack, msg.json.command_id);
|
2012-11-21 17:08:11 -08:00
|
|
|
return;
|
|
|
|
}
|
2012-11-26 18:19:04 -08:00
|
|
|
sendOk(msg.json.command_id);
|
2012-11-21 17:08:11 -08:00
|
|
|
}
|
|
|
|
|
2012-03-22 08:19:57 -07:00
|
|
|
/**
|
|
|
|
* Restarts all our listeners after this listener was put to sleep
|
|
|
|
*/
|
2012-11-26 18:19:04 -08:00
|
|
|
function restart(msg) {
|
2012-03-22 08:19:57 -07:00
|
|
|
removeMessageListener("Marionette:restart", restart);
|
|
|
|
registerSelf();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all listeners
|
|
|
|
*/
|
|
|
|
function deleteSession(msg) {
|
2012-05-11 13:06:53 -07:00
|
|
|
removeMessageListenerId("Marionette:newSession", newSession);
|
|
|
|
removeMessageListenerId("Marionette:executeScript", executeScript);
|
|
|
|
removeMessageListenerId("Marionette:executeAsyncScript", executeAsyncScript);
|
|
|
|
removeMessageListenerId("Marionette:executeJSScript", executeJSScript);
|
2013-01-22 11:27:44 -08:00
|
|
|
removeMessageListenerId("Marionette:singleTap", singleTap);
|
|
|
|
removeMessageListenerId("Marionette:doubleTap", doubleTap);
|
2013-02-06 11:58:14 -08:00
|
|
|
removeMessageListenerId("Marionette:press", press);
|
|
|
|
removeMessageListenerId("Marionette:release", release);
|
2013-03-19 14:59:48 -07:00
|
|
|
removeMessageListenerId("Marionette:cancelTouch", cancelTouch);
|
2013-03-04 17:09:58 -08:00
|
|
|
removeMessageListenerId("Marionette:actionChain", actionChain);
|
2013-03-18 13:42:46 -07:00
|
|
|
removeMessageListenerId("Marionette:multiAction", multiAction);
|
2013-04-10 16:55:43 -07:00
|
|
|
removeMessageListenerId("Marionette:sendMouseEvent", sendMouseEvent);
|
2012-05-11 13:06:53 -07:00
|
|
|
removeMessageListenerId("Marionette:setSearchTimeout", setSearchTimeout);
|
|
|
|
removeMessageListenerId("Marionette:goUrl", goUrl);
|
2012-07-10 13:30:21 -07:00
|
|
|
removeMessageListenerId("Marionette:getTitle", getTitle);
|
2012-08-09 12:31:12 -07:00
|
|
|
removeMessageListenerId("Marionette:getPageSource", getPageSource);
|
2012-05-11 13:06:53 -07:00
|
|
|
removeMessageListenerId("Marionette:getUrl", getUrl);
|
|
|
|
removeMessageListenerId("Marionette:goBack", goBack);
|
|
|
|
removeMessageListenerId("Marionette:goForward", goForward);
|
|
|
|
removeMessageListenerId("Marionette:refresh", refresh);
|
|
|
|
removeMessageListenerId("Marionette:findElementContent", findElementContent);
|
|
|
|
removeMessageListenerId("Marionette:findElementsContent", findElementsContent);
|
2013-01-14 06:57:54 -08:00
|
|
|
removeMessageListenerId("Marionette:getActiveElement", getActiveElement);
|
2012-05-11 13:06:53 -07:00
|
|
|
removeMessageListenerId("Marionette:clickElement", clickElement);
|
|
|
|
removeMessageListenerId("Marionette:getElementAttribute", getElementAttribute);
|
2012-08-08 13:21:50 -07:00
|
|
|
removeMessageListenerId("Marionette:getElementTagName", getElementTagName);
|
2012-05-11 13:06:53 -07:00
|
|
|
removeMessageListenerId("Marionette:isElementDisplayed", isElementDisplayed);
|
2012-10-19 13:59:15 -07:00
|
|
|
removeMessageListenerId("Marionette:getElementSize", getElementSize);
|
2012-05-11 13:06:53 -07:00
|
|
|
removeMessageListenerId("Marionette:isElementEnabled", isElementEnabled);
|
|
|
|
removeMessageListenerId("Marionette:isElementSelected", isElementSelected);
|
|
|
|
removeMessageListenerId("Marionette:sendKeysToElement", sendKeysToElement);
|
2012-10-19 13:35:30 -07:00
|
|
|
removeMessageListenerId("Marionette:getElementPosition", getElementPosition);
|
2012-05-11 13:06:53 -07:00
|
|
|
removeMessageListenerId("Marionette:clearElement", clearElement);
|
|
|
|
removeMessageListenerId("Marionette:switchToFrame", switchToFrame);
|
|
|
|
removeMessageListenerId("Marionette:deleteSession", deleteSession);
|
|
|
|
removeMessageListenerId("Marionette:sleepSession", sleepSession);
|
2012-05-18 13:30:43 -07:00
|
|
|
removeMessageListenerId("Marionette:emulatorCmdResult", emulatorCmdResult);
|
2012-06-04 10:50:06 -07:00
|
|
|
removeMessageListenerId("Marionette:importScript", importScript);
|
2012-08-23 15:07:16 -07:00
|
|
|
removeMessageListenerId("Marionette:getAppCacheStatus", getAppCacheStatus);
|
2012-10-31 15:36:57 -07:00
|
|
|
removeMessageListenerId("Marionette:setTestName", setTestName);
|
2012-11-21 17:08:11 -08:00
|
|
|
removeMessageListenerId("Marionette:setState", setState);
|
2012-11-30 15:25:26 -08:00
|
|
|
removeMessageListenerId("Marionette:screenShot", screenShot);
|
2012-11-22 07:53:44 -08:00
|
|
|
removeMessageListenerId("Marionette:addCookie", addCookie);
|
|
|
|
removeMessageListenerId("Marionette:getAllCookies", getAllCookies);
|
|
|
|
removeMessageListenerId("Marionette:deleteAllCookies", deleteAllCookies);
|
|
|
|
removeMessageListenerId("Marionette:deleteCookie", deleteCookie);
|
2012-03-22 08:19:57 -07:00
|
|
|
this.elementManager.reset();
|
2012-11-13 13:51:58 -08:00
|
|
|
// reset frame to the top-most frame
|
|
|
|
curWindow = content;
|
|
|
|
curWindow.focus();
|
2013-02-06 11:58:14 -08:00
|
|
|
touches = [];
|
2013-03-04 17:09:58 -08:00
|
|
|
touchIds = {};
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic method to send a message to the server
|
|
|
|
*/
|
|
|
|
function sendToServer(msg, value, command_id) {
|
|
|
|
if (command_id) {
|
|
|
|
value.command_id = command_id;
|
|
|
|
}
|
|
|
|
sendAsyncMessage(msg, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send response back to server
|
|
|
|
*/
|
|
|
|
function sendResponse(value, command_id) {
|
|
|
|
sendToServer("Marionette:done", value, command_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send ack back to server
|
|
|
|
*/
|
|
|
|
function sendOk(command_id) {
|
|
|
|
sendToServer("Marionette:ok", {}, command_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send log message to server
|
|
|
|
*/
|
|
|
|
function sendLog(msg) {
|
|
|
|
sendToServer("Marionette:log", { message: msg });
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send error message to server
|
|
|
|
*/
|
|
|
|
function sendError(message, status, trace, command_id) {
|
|
|
|
let error_msg = { message: message, status: status, stacktrace: trace };
|
|
|
|
sendToServer("Marionette:error", error_msg, command_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear test values after completion of test
|
|
|
|
*/
|
|
|
|
function resetValues() {
|
2012-05-09 12:05:39 -07:00
|
|
|
sandbox = null;
|
2012-11-13 13:51:58 -08:00
|
|
|
curWindow = content;
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Marionette Methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a content sandbox that can be used by the execute_foo functions.
|
|
|
|
*/
|
2013-01-23 11:26:33 -08:00
|
|
|
function createExecuteContentSandbox(aWindow, timeout) {
|
2013-02-14 01:40:15 -08:00
|
|
|
let sandbox = new Cu.Sandbox(aWindow, {sandboxPrototype: aWindow});
|
2012-05-09 12:05:39 -07:00
|
|
|
sandbox.global = sandbox;
|
2012-03-22 08:19:57 -07:00
|
|
|
sandbox.window = aWindow;
|
|
|
|
sandbox.document = sandbox.window.document;
|
|
|
|
sandbox.navigator = sandbox.window.navigator;
|
2012-04-10 17:28:08 -07:00
|
|
|
sandbox.testUtils = utils;
|
2013-03-25 11:09:16 -07:00
|
|
|
sandbox.asyncTestCommandId = asyncTestCommandId;
|
2012-03-22 08:19:57 -07:00
|
|
|
|
2012-10-31 23:51:30 -07:00
|
|
|
let marionette = new Marionette(this, aWindow, "content",
|
|
|
|
marionetteLogObj, marionettePerf,
|
2012-11-28 13:31:23 -08:00
|
|
|
timeout, marionetteTestName);
|
2012-05-09 12:05:39 -07:00
|
|
|
sandbox.marionette = marionette;
|
2012-03-22 08:19:57 -07:00
|
|
|
marionette.exports.forEach(function(fn) {
|
2012-09-14 14:34:38 -07:00
|
|
|
try {
|
|
|
|
sandbox[fn] = marionette[fn].bind(marionette);
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
sandbox[fn] = marionette[fn];
|
|
|
|
}
|
2012-03-22 08:19:57 -07:00
|
|
|
});
|
|
|
|
|
2013-01-23 11:26:33 -08:00
|
|
|
XPCOMUtils.defineLazyGetter(sandbox, 'SpecialPowers', function() {
|
|
|
|
return new SpecialPowers(aWindow);
|
|
|
|
});
|
2012-05-18 13:30:13 -07:00
|
|
|
|
2013-03-25 11:09:16 -07:00
|
|
|
sandbox.asyncComplete = function sandbox_asyncComplete(value, status, stack, commandId) {
|
|
|
|
if (commandId == asyncTestCommandId) {
|
|
|
|
curWindow.removeEventListener("unload", onunload, false);
|
|
|
|
curWindow.clearTimeout(asyncTestTimeoutId);
|
2012-05-09 12:05:39 -07:00
|
|
|
|
2013-03-25 11:09:16 -07:00
|
|
|
sendSyncMessage("Marionette:shareData", {log: elementManager.wrapValue(marionetteLogObj.getLogs()),
|
|
|
|
perf: elementManager.wrapValue(marionettePerf.getPerfData())});
|
|
|
|
marionetteLogObj.clearLogs();
|
|
|
|
marionettePerf.clearPerfData();
|
2012-11-27 13:58:58 -08:00
|
|
|
|
2013-03-25 11:09:16 -07:00
|
|
|
if (status == 0){
|
|
|
|
if (Object.keys(_emu_cbs).length) {
|
|
|
|
_emu_cbs = {};
|
|
|
|
sendError("Emulator callback still pending when finish() called",
|
|
|
|
500, null, commandId);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sendResponse({value: elementManager.wrapValue(value), status: status},
|
|
|
|
commandId);
|
|
|
|
}
|
2012-11-27 13:58:58 -08:00
|
|
|
}
|
|
|
|
else {
|
2013-03-25 11:09:16 -07:00
|
|
|
sendError(value, status, stack, commandId);
|
2012-11-27 13:58:58 -08:00
|
|
|
}
|
2012-05-09 12:05:39 -07:00
|
|
|
|
2013-03-25 11:09:16 -07:00
|
|
|
asyncTestRunning = false;
|
|
|
|
asyncTestTimeoutId = undefined;
|
|
|
|
asyncTestCommandId = undefined;
|
|
|
|
}
|
2012-05-09 12:05:39 -07:00
|
|
|
};
|
|
|
|
sandbox.finish = function sandbox_finish() {
|
|
|
|
if (asyncTestRunning) {
|
2013-03-25 11:09:16 -07:00
|
|
|
sandbox.asyncComplete(marionette.generate_results(), 0, null, sandbox.asyncTestCommandId);
|
2012-05-09 12:05:39 -07:00
|
|
|
} else {
|
|
|
|
return marionette.generate_results();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
sandbox.marionetteScriptFinished = function sandbox_marionetteScriptFinished(value) {
|
2013-03-25 11:09:16 -07:00
|
|
|
return sandbox.asyncComplete(value, 0, null, sandbox.asyncTestCommandId);
|
2012-05-09 12:05:39 -07:00
|
|
|
};
|
|
|
|
|
2012-03-22 08:19:57 -07:00
|
|
|
return sandbox;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the given script either as a function body (executeScript)
|
|
|
|
* or directly (for 'mochitest' like JS Marionette tests)
|
|
|
|
*/
|
|
|
|
function executeScript(msg, directInject) {
|
2013-02-12 10:10:03 -08:00
|
|
|
asyncTestCommandId = msg.json.command_id;
|
2012-03-22 08:19:57 -07:00
|
|
|
let script = msg.json.value;
|
|
|
|
|
2012-05-09 12:05:39 -07:00
|
|
|
if (msg.json.newSandbox || !sandbox) {
|
2013-01-15 15:42:06 -08:00
|
|
|
sandbox = createExecuteContentSandbox(curWindow,
|
2013-01-23 11:26:33 -08:00
|
|
|
msg.json.timeout);
|
2012-05-09 12:05:39 -07:00
|
|
|
if (!sandbox) {
|
2013-02-12 10:10:03 -08:00
|
|
|
sendError("Could not create sandbox!", 500, null, asyncTestCommandId);
|
2012-05-09 12:05:39 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-03-25 11:09:16 -07:00
|
|
|
else {
|
|
|
|
sandbox.asyncTestCommandId = asyncTestCommandId;
|
|
|
|
}
|
2012-03-22 08:19:57 -07:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (directInject) {
|
2012-06-04 10:50:06 -07:00
|
|
|
if (importedScripts.exists()) {
|
|
|
|
let stream = Components.classes["@mozilla.org/network/file-input-stream;1"].
|
|
|
|
createInstance(Components.interfaces.nsIFileInputStream);
|
|
|
|
stream.init(importedScripts, -1, 0, 0);
|
|
|
|
let data = NetUtil.readInputStreamToString(stream, stream.available());
|
|
|
|
script = data + script;
|
|
|
|
}
|
2012-03-22 08:19:57 -07:00
|
|
|
let res = Cu.evalInSandbox(script, sandbox, "1.8");
|
2012-06-10 16:44:50 -07:00
|
|
|
sendSyncMessage("Marionette:shareData", {log: elementManager.wrapValue(marionetteLogObj.getLogs()),
|
|
|
|
perf: elementManager.wrapValue(marionettePerf.getPerfData())});
|
2012-03-22 08:19:57 -07:00
|
|
|
marionetteLogObj.clearLogs();
|
2012-06-10 16:44:50 -07:00
|
|
|
marionettePerf.clearPerfData();
|
2012-03-22 08:19:57 -07:00
|
|
|
if (res == undefined || res.passed == undefined) {
|
2012-11-26 18:19:04 -08:00
|
|
|
sendError("Marionette.finish() not called", 17, null, asyncTestCommandId);
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
else {
|
2012-11-26 18:19:04 -08:00
|
|
|
sendResponse({value: elementManager.wrapValue(res)}, asyncTestCommandId);
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2012-05-09 12:05:39 -07:00
|
|
|
try {
|
|
|
|
sandbox.__marionetteParams = elementManager.convertWrappedArguments(
|
|
|
|
msg.json.args, curWindow);
|
|
|
|
}
|
|
|
|
catch(e) {
|
2012-11-26 18:19:04 -08:00
|
|
|
sendError(e.message, e.code, e.stack, asyncTestCommandId);
|
2012-05-09 12:05:39 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-22 08:19:57 -07:00
|
|
|
let scriptSrc = "let __marionetteFunc = function(){" + script + "};" +
|
|
|
|
"__marionetteFunc.apply(null, __marionetteParams);";
|
2012-06-04 10:50:06 -07:00
|
|
|
if (importedScripts.exists()) {
|
|
|
|
let stream = Components.classes["@mozilla.org/network/file-input-stream;1"].
|
|
|
|
createInstance(Components.interfaces.nsIFileInputStream);
|
|
|
|
stream.init(importedScripts, -1, 0, 0);
|
|
|
|
let data = NetUtil.readInputStreamToString(stream, stream.available());
|
|
|
|
scriptSrc = data + scriptSrc;
|
|
|
|
}
|
2012-03-22 08:19:57 -07:00
|
|
|
let res = Cu.evalInSandbox(scriptSrc, sandbox, "1.8");
|
2012-06-10 16:44:50 -07:00
|
|
|
sendSyncMessage("Marionette:shareData", {log: elementManager.wrapValue(marionetteLogObj.getLogs()),
|
|
|
|
perf: elementManager.wrapValue(marionettePerf.getPerfData())});
|
2012-03-22 08:19:57 -07:00
|
|
|
marionetteLogObj.clearLogs();
|
2012-06-10 16:44:50 -07:00
|
|
|
marionettePerf.clearPerfData();
|
2012-11-26 18:19:04 -08:00
|
|
|
sendResponse({value: elementManager.wrapValue(res)}, asyncTestCommandId);
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
// 17 = JavascriptException
|
2012-11-26 18:19:04 -08:00
|
|
|
sendError(e.name + ': ' + e.message, 17, e.stack, asyncTestCommandId);
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-31 15:36:57 -07:00
|
|
|
/**
|
|
|
|
* Sets the test name, used in logging messages.
|
|
|
|
*/
|
|
|
|
function setTestName(msg) {
|
|
|
|
marionetteTestName = msg.json.value;
|
2012-11-26 18:19:04 -08:00
|
|
|
sendOk(msg.json.command_id);
|
2012-10-31 15:36:57 -07:00
|
|
|
}
|
|
|
|
|
2012-03-22 08:19:57 -07:00
|
|
|
/**
|
|
|
|
* Execute async script
|
|
|
|
*/
|
|
|
|
function executeAsyncScript(msg) {
|
|
|
|
executeWithCallback(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute pure JS test. Handles both async and sync cases.
|
|
|
|
*/
|
|
|
|
function executeJSScript(msg) {
|
2012-11-28 13:31:23 -08:00
|
|
|
if (msg.json.async) {
|
|
|
|
executeWithCallback(msg, msg.json.async);
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
executeScript(msg, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is used by executeAsync and executeJSScript to execute a script
|
|
|
|
* in a sandbox.
|
|
|
|
*
|
|
|
|
* For executeJSScript, it will return a message only when the finish() method is called.
|
|
|
|
* For executeAsync, it will return a response when marionetteScriptFinished/arguments[arguments.length-1]
|
|
|
|
* method is called, or if it times out.
|
|
|
|
*/
|
2012-11-30 07:42:43 -08:00
|
|
|
function executeWithCallback(msg, useFinish) {
|
2012-03-22 08:19:57 -07:00
|
|
|
let script = msg.json.value;
|
2013-02-12 10:10:03 -08:00
|
|
|
asyncTestCommandId = msg.json.command_id;
|
|
|
|
|
|
|
|
onunload = function() {
|
|
|
|
sendError("unload was called", 17, null, asyncTestCommandId);
|
|
|
|
};
|
|
|
|
curWindow.addEventListener("unload", onunload, false);
|
2012-03-22 08:19:57 -07:00
|
|
|
|
2012-05-10 16:07:07 -07:00
|
|
|
if (msg.json.newSandbox || !sandbox) {
|
2013-01-15 15:42:06 -08:00
|
|
|
sandbox = createExecuteContentSandbox(curWindow,
|
2013-01-23 11:26:33 -08:00
|
|
|
msg.json.timeout);
|
2012-05-10 16:07:07 -07:00
|
|
|
if (!sandbox) {
|
2013-02-12 10:10:03 -08:00
|
|
|
sendError("Could not create sandbox!", 17, null, asyncTestCommandId);
|
2012-05-10 16:07:07 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-03-25 11:09:16 -07:00
|
|
|
else {
|
|
|
|
sandbox.asyncTestCommandId = asyncTestCommandId;
|
|
|
|
}
|
2012-11-26 18:19:04 -08:00
|
|
|
sandbox.tag = script;
|
2012-05-10 16:07:07 -07:00
|
|
|
|
2012-03-22 08:19:57 -07:00
|
|
|
// Error code 28 is scriptTimeout, but spec says execute_async should return 21 (Timeout),
|
|
|
|
// see http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/execute_async.
|
|
|
|
// However Selenium code returns 28, see
|
|
|
|
// http://code.google.com/p/selenium/source/browse/trunk/javascript/firefox-driver/js/evaluate.js.
|
|
|
|
// We'll stay compatible with the Selenium code.
|
2012-05-09 12:05:39 -07:00
|
|
|
asyncTestTimeoutId = curWindow.setTimeout(function() {
|
2013-02-12 10:10:03 -08:00
|
|
|
sandbox.asyncComplete('timed out', 28, null, asyncTestCommandId);
|
2012-11-28 13:31:23 -08:00
|
|
|
}, msg.json.timeout);
|
2012-05-10 16:07:07 -07:00
|
|
|
|
2012-12-18 06:21:28 -08:00
|
|
|
originalOnError = curWindow.onerror;
|
|
|
|
curWindow.onerror = function errHandler(errMsg, url, line) {
|
2013-04-05 15:25:46 -07:00
|
|
|
sandbox.asyncComplete(errMsg, 17, "@" + url + ", line " + line, asyncTestCommandId);
|
2012-12-18 06:21:28 -08:00
|
|
|
curWindow.onerror = originalOnError;
|
|
|
|
};
|
2012-03-22 08:19:57 -07:00
|
|
|
|
|
|
|
let scriptSrc;
|
2012-11-30 07:42:43 -08:00
|
|
|
if (useFinish) {
|
2012-11-28 13:31:23 -08:00
|
|
|
if (msg.json.timeout == null || msg.json.timeout == 0) {
|
2012-11-26 18:19:04 -08:00
|
|
|
sendError("Please set a timeout", 21, null, asyncTestCommandId);
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
scriptSrc = script;
|
|
|
|
}
|
|
|
|
else {
|
2012-05-09 12:05:39 -07:00
|
|
|
try {
|
|
|
|
sandbox.__marionetteParams = elementManager.convertWrappedArguments(
|
|
|
|
msg.json.args, curWindow);
|
|
|
|
}
|
|
|
|
catch(e) {
|
2012-11-26 18:19:04 -08:00
|
|
|
sendError(e.message, e.code, e.stack, asyncTestCommandId);
|
2012-05-09 12:05:39 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
scriptSrc = "__marionetteParams.push(marionetteScriptFinished);" +
|
2012-03-22 08:19:57 -07:00
|
|
|
"let __marionetteFunc = function() { " + script + "};" +
|
|
|
|
"__marionetteFunc.apply(null, __marionetteParams); ";
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2012-05-09 12:05:39 -07:00
|
|
|
asyncTestRunning = true;
|
2012-06-04 10:50:06 -07:00
|
|
|
if (importedScripts.exists()) {
|
2012-08-31 13:59:48 -07:00
|
|
|
let stream = Cc["@mozilla.org/network/file-input-stream;1"].
|
|
|
|
createInstance(Ci.nsIFileInputStream);
|
2012-06-04 10:50:06 -07:00
|
|
|
stream.init(importedScripts, -1, 0, 0);
|
|
|
|
let data = NetUtil.readInputStreamToString(stream, stream.available());
|
|
|
|
scriptSrc = data + scriptSrc;
|
|
|
|
}
|
2012-05-09 12:05:39 -07:00
|
|
|
Cu.evalInSandbox(scriptSrc, sandbox, "1.8");
|
2012-03-22 08:19:57 -07:00
|
|
|
} catch (e) {
|
|
|
|
// 17 = JavascriptException
|
2013-02-12 10:10:03 -08:00
|
|
|
sandbox.asyncComplete(e.name + ': ' + e.message, 17,
|
|
|
|
e.stack, asyncTestCommandId);
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-10 16:55:43 -07:00
|
|
|
/**
|
|
|
|
* This function sets a value for mouseEvent to decide if we want to send mouse events
|
|
|
|
*/
|
|
|
|
function sendMouseEvent(msg) {
|
|
|
|
let command_id = msg.json.command_id;
|
|
|
|
if (typeof msg.json.value === 'boolean') {
|
|
|
|
mouseEvent = msg.json.value;
|
|
|
|
sendOk(msg.json.command_id);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sendError("Json value is not boolean as expected", 500, null, command_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-22 11:27:44 -08:00
|
|
|
/**
|
|
|
|
* This function creates a touch event given a touch type and a touch
|
|
|
|
*/
|
|
|
|
function emitTouchEvent(type, touch) {
|
|
|
|
// Using domWindowUtils
|
2013-03-04 17:09:58 -08:00
|
|
|
let domWindowUtils = curWindow.QueryInterface(Components.interfaces.nsIInterfaceRequestor).getInterface(Components.interfaces.nsIDOMWindowUtils);
|
2013-01-22 11:27:44 -08:00
|
|
|
domWindowUtils.sendTouchEvent(type, [touch.identifier], [touch.screenX], [touch.screenY], [touch.radiusX], [touch.radiusY], [touch.rotationAngle], [touch.force], 1, 0);
|
|
|
|
}
|
|
|
|
|
2013-04-10 16:55:43 -07:00
|
|
|
/**
|
|
|
|
* This function emit mouse event
|
|
|
|
* @param: doc is the current document
|
|
|
|
* type is the type of event to dispatch
|
|
|
|
* detail is the number of clicks, button notes the mouse button
|
|
|
|
* elClientX and elClientY are the coordinates of the mouse relative to the viewport
|
|
|
|
*/
|
|
|
|
function emitMouseEvent(doc, type, detail, button, elClientX, elClientY) {
|
|
|
|
var win = doc.defaultView;
|
|
|
|
// Figure out the element the mouse would be over at (x, y)
|
|
|
|
var target = doc.elementFromPoint(elClientX, elClientY);
|
|
|
|
utils.synthesizeMouse(target, win.mozInnerScreenX, win.mozInnerScreenY, {type: type, button: button, clickCount: detail}, win);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function create a mouse and emit mouse events
|
|
|
|
* @param 'xt' and 'yt' are functions of t that specify the mouse coordinates at time t
|
|
|
|
*/
|
|
|
|
function mouse(doc, duration, xt, yt, then, detail, button) {
|
|
|
|
detail = detail || 1;
|
|
|
|
button = button || 0;
|
|
|
|
var x = xt;
|
|
|
|
if (typeof xt !== 'function') {
|
|
|
|
x = function(t) {
|
|
|
|
return xt[0] + t / duration * (xt[1] - xt[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var y = yt;
|
|
|
|
if (typeof yt !== 'function') {
|
|
|
|
y = function(t) {
|
|
|
|
return yt[0] + t / duration * (yt[1] - yt[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// viewport coordinates
|
|
|
|
var clientX = Math.round(x(0)), clientY = Math.round(y(0));
|
|
|
|
// Remember the coordinates
|
|
|
|
var lastX = clientX, lastY = clientY;
|
|
|
|
emitMouseEvent(doc, 'mousedown', detail, button, clientX, clientY);
|
|
|
|
// now send a sequence of mousemove events followed by mouse up
|
|
|
|
let startTime = Date.now();
|
|
|
|
checkTimer.initWithCallback(nextEvent, EVENT_INTERVAL, Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
function nextEvent() {
|
|
|
|
// figure out if we've sent all the mousemove events
|
|
|
|
var time = Date.now();
|
|
|
|
var dt = time - startTime;
|
|
|
|
let last = dt + EVENT_INTERVAL / 2 > duration;
|
|
|
|
// New coordinates of the touch
|
|
|
|
clientX = Math.round(x(dt));
|
|
|
|
clientY = Math.round(y(dt));
|
|
|
|
// If we moved, send a move event
|
|
|
|
if (clientX !== lastX || clientY !== lastY) { // If we moved
|
|
|
|
lastX = clientX;
|
|
|
|
lastY = clientY;
|
|
|
|
emitMouseEvent(doc, 'mousemove', detail, button, clientX, clientY);
|
|
|
|
}
|
|
|
|
// If this was the last move, send a mouse up and call the callback
|
|
|
|
// Otherwise, schedule the next move event
|
|
|
|
if (last) {
|
|
|
|
emitMouseEvent(doc, 'mouseup', detail, button, lastX, lastY);
|
|
|
|
if (then) {
|
|
|
|
checkTimer.initWithCallback(then, 0, Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
checkTimer.initWithCallback(nextEvent, EVENT_INTERVAL, Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-22 11:27:44 -08:00
|
|
|
/**
|
|
|
|
* This function creates a touch and emit touch events
|
|
|
|
* @param 'xt' and 'yt' are two-element array [from, to] and then is a callback that will be invoked after touchend event is sent
|
|
|
|
*/
|
|
|
|
function touch(target, duration, xt, yt, then) {
|
2013-03-04 17:09:58 -08:00
|
|
|
let doc = target.ownerDocument;
|
|
|
|
let win = doc.defaultView;
|
|
|
|
let touchId = nextTouchId++;
|
|
|
|
let x = xt;
|
2013-01-22 11:27:44 -08:00
|
|
|
if (typeof xt !== 'function') {
|
|
|
|
x = function(t) { return xt[0] + t / duration * (xt[1] - xt[0]); };
|
|
|
|
}
|
2013-03-04 17:09:58 -08:00
|
|
|
let y = yt;
|
2013-01-22 11:27:44 -08:00
|
|
|
if (typeof yt !== 'function') {
|
|
|
|
y = function(t) { return yt[0] + t / duration * (yt[1] - yt[0]); };
|
|
|
|
}
|
|
|
|
// viewport coordinates
|
2013-03-04 17:09:58 -08:00
|
|
|
let clientX = Math.round(x(0)), clientY = Math.round(y(0));
|
2013-01-22 11:27:44 -08:00
|
|
|
// document coordinates
|
2013-03-04 17:09:58 -08:00
|
|
|
let pageX = clientX + win.pageXOffset,
|
2013-01-22 11:27:44 -08:00
|
|
|
pageY = clientY + win.pageYOffset;
|
|
|
|
// screen coordinates
|
2013-03-04 17:09:58 -08:00
|
|
|
let screenX = clientX + win.mozInnerScreenX,
|
2013-01-22 11:27:44 -08:00
|
|
|
screenY = clientY + win.mozInnerScreenY;
|
|
|
|
// Remember the coordinates
|
2013-03-04 17:09:58 -08:00
|
|
|
let lastX = clientX, lastY = clientY;
|
2013-01-22 11:27:44 -08:00
|
|
|
// Create the touch object
|
2013-03-04 17:09:58 -08:00
|
|
|
let touch = doc.createTouch(win, target, touchId,
|
2013-01-22 11:27:44 -08:00
|
|
|
pageX, pageY,
|
|
|
|
screenX, screenY,
|
|
|
|
clientX, clientY);
|
|
|
|
// Add this new touch to the list of touches
|
|
|
|
touches.push(touch);
|
|
|
|
// Send the start event
|
|
|
|
emitTouchEvent('touchstart', touch);
|
2013-03-04 17:09:58 -08:00
|
|
|
let startTime = Date.now();
|
2013-01-22 11:27:44 -08:00
|
|
|
checkTimer.initWithCallback(nextEvent, EVENT_INTERVAL, Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
function nextEvent() {
|
|
|
|
// Figure out if this is the last of the touchmove events
|
2013-03-04 17:09:58 -08:00
|
|
|
let time = Date.now();
|
|
|
|
let dt = time - startTime;
|
|
|
|
let last = dt + EVENT_INTERVAL / 2 > duration;
|
2013-01-22 11:27:44 -08:00
|
|
|
// Find our touch object in the touches[] array.
|
|
|
|
// Note that its index may have changed since we pushed it
|
2013-03-04 17:09:58 -08:00
|
|
|
let touchIndex = touches.indexOf(touch);
|
2013-01-22 11:27:44 -08:00
|
|
|
// If this is the last move event, make sure we move all the way
|
|
|
|
if (last)
|
|
|
|
dt = duration;
|
|
|
|
// New coordinates of the touch
|
|
|
|
clientX = Math.round(x(dt));
|
|
|
|
clientY = Math.round(y(dt));
|
|
|
|
// If we've moved, send a move event
|
|
|
|
if (clientX !== lastX || clientY !== lastY) { // If we moved
|
|
|
|
lastX = clientX;
|
|
|
|
lastY = clientY;
|
|
|
|
pageX = clientX + win.pageXOffset;
|
|
|
|
pageY = clientY + win.pageYOffset;
|
|
|
|
screenX = clientX + win.mozInnerScreenX;
|
|
|
|
screenY = clientY + win.mozInnerScreenY;
|
|
|
|
// Since we moved, we've got to create a new Touch object
|
|
|
|
// with the new coordinates
|
|
|
|
touch = doc.createTouch(win, target, touchId,
|
|
|
|
pageX, pageY,
|
|
|
|
screenX, screenY,
|
|
|
|
clientX, clientY);
|
|
|
|
// Replace the old touch object with the new one
|
|
|
|
touches[touchIndex] = touch;
|
|
|
|
// And send the touchmove event
|
|
|
|
emitTouchEvent('touchmove', touch);
|
|
|
|
}
|
|
|
|
// If that was the last move, send the touchend event
|
|
|
|
// and call the callback
|
|
|
|
if (last) {
|
|
|
|
touches.splice(touchIndex, 1);
|
|
|
|
emitTouchEvent('touchend', touch);
|
|
|
|
if (then)
|
|
|
|
checkTimer.initWithCallback(then, 0, Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
// Otherwise, schedule the next event
|
|
|
|
else {
|
|
|
|
checkTimer.initWithCallback(nextEvent, EVENT_INTERVAL, Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function generates the coordinates of the element
|
|
|
|
* @param 'x0', 'y0', 'x1', and 'y1' are the relative to the viewport.
|
|
|
|
* If they are not specified, then the center of the target is used.
|
|
|
|
*/
|
|
|
|
function coordinates(target, x0, y0, x1, y1) {
|
2013-03-04 17:09:58 -08:00
|
|
|
let coords = {};
|
|
|
|
let box = target.getBoundingClientRect();
|
|
|
|
let tx0 = typeof x0;
|
|
|
|
let ty0 = typeof y0;
|
|
|
|
let tx1 = typeof x1;
|
|
|
|
let ty1 = typeof y1;
|
2013-01-22 11:27:44 -08:00
|
|
|
function percent(s, x) {
|
|
|
|
s = s.trim();
|
2013-03-04 17:09:58 -08:00
|
|
|
let f = parseFloat(s);
|
2013-01-22 11:27:44 -08:00
|
|
|
if (s[s.length - 1] === '%')
|
|
|
|
f = f * x / 100;
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
function relative(s, x) {
|
2013-03-04 17:09:58 -08:00
|
|
|
let factor;
|
2013-01-22 11:27:44 -08:00
|
|
|
if (s[0] === '+')
|
|
|
|
factor = 1;
|
|
|
|
else
|
|
|
|
factor = -1;
|
|
|
|
return factor * percent(s.substring(1), x);
|
|
|
|
}
|
|
|
|
if (tx0 === 'number')
|
|
|
|
coords.x0 = box.left + x0;
|
|
|
|
else if (tx0 === 'string')
|
|
|
|
coords.x0 = box.left + percent(x0, box.width);
|
|
|
|
//check tx1 point
|
|
|
|
if (tx1 === 'number')
|
|
|
|
coords.x1 = box.left + x1;
|
|
|
|
else if (tx1 === 'string') {
|
|
|
|
x1 = x1.trim();
|
|
|
|
if (x1[0] === '+' || x1[0] === '-')
|
|
|
|
coords.x1 = coords.x0 + relative(x1, box.width);
|
|
|
|
else
|
|
|
|
coords.x1 = box.left + percent(x1, box.width);
|
|
|
|
}
|
|
|
|
// check ty0
|
|
|
|
if (ty0 === 'number')
|
|
|
|
coords.y0 = box.top + y0;
|
|
|
|
else if (ty0 === 'string')
|
|
|
|
coords.y0 = box.top + percent(y0, box.height);
|
|
|
|
//check ty1
|
|
|
|
if (ty1 === 'number')
|
|
|
|
coords.y1 = box.top + y1;
|
|
|
|
else if (ty1 === 'string') {
|
|
|
|
y1 = y1.trim();
|
|
|
|
if (y1[0] === '+' || y1[0] === '-')
|
|
|
|
coords.y1 = coords.y0 + relative(y1, box.height);
|
|
|
|
else
|
|
|
|
coords.y1 = box.top + percent(y1, box.height);
|
|
|
|
}
|
|
|
|
return coords;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function returns if the element is in viewport
|
|
|
|
*/
|
|
|
|
function elementInViewport(el) {
|
2013-04-17 00:42:10 -07:00
|
|
|
let rect = el.getBoundingClientRect();
|
|
|
|
return (rect.top >= curWindow.pageYOffset &&
|
|
|
|
rect.left >= curWindow.pageXOffset &&
|
|
|
|
rect.bottom <= (curWindow.pageYOffset + curWindow.innerHeight) &&
|
|
|
|
rect.right <= (curWindow.pageXOffset + curWindow.innerWidth)
|
2013-01-22 11:27:44 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function throws the visibility of the element error
|
|
|
|
*/
|
|
|
|
function checkVisible(el, command_id) {
|
2013-03-01 13:29:10 -08:00
|
|
|
//check if the element is visible
|
|
|
|
let visible = utils.isElementDisplayed(el);
|
|
|
|
if (!visible) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-11 13:52:42 -07:00
|
|
|
if (el.tagName.toLowerCase() === 'body') {
|
|
|
|
return true;
|
|
|
|
}
|
2013-03-01 13:29:10 -08:00
|
|
|
if (!elementInViewport(el)) {
|
2013-01-22 11:27:44 -08:00
|
|
|
//check if scroll function exist. If so, call it.
|
|
|
|
if (el.scrollIntoView) {
|
|
|
|
el.scrollIntoView(true);
|
2013-03-01 13:29:10 -08:00
|
|
|
if (!elementInViewport(el)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-22 11:27:44 -08:00
|
|
|
}
|
2013-03-01 13:29:10 -08:00
|
|
|
else {
|
2013-01-22 11:27:44 -08:00
|
|
|
return false;
|
|
|
|
}
|
2013-03-01 13:29:10 -08:00
|
|
|
}
|
|
|
|
return true;
|
2013-01-22 11:27:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function that perform a single tap
|
|
|
|
*/
|
|
|
|
function singleTap(msg) {
|
|
|
|
let command_id = msg.json.command_id;
|
|
|
|
let el;
|
|
|
|
try {
|
|
|
|
el = elementManager.getKnownElement(msg.json.value, curWindow);
|
2013-02-01 13:29:10 -08:00
|
|
|
let x = msg.json.corx;
|
|
|
|
let y = msg.json.cory;
|
2013-01-22 11:27:44 -08:00
|
|
|
if (!checkVisible(el, command_id)) {
|
|
|
|
sendError("Element is not currently visible and may not be manipulated", 11, null, command_id);
|
|
|
|
return;
|
|
|
|
}
|
2013-02-01 13:29:10 -08:00
|
|
|
if (x == null) {
|
|
|
|
x = '50%';
|
|
|
|
}
|
|
|
|
if (y == null) {
|
|
|
|
y = '50%';
|
|
|
|
}
|
2013-01-22 11:27:44 -08:00
|
|
|
let c = coordinates(el, x, y);
|
2013-04-10 16:55:43 -07:00
|
|
|
if (mouseEvent) {
|
|
|
|
touch(el, 25, [c.x0, c.x0], [c.y0, c.y0], function() {
|
|
|
|
mousetap(el, 25, c.x0, c.y0, 1, 0, null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
touch(el, 25, [c.x0, c.x0], [c.y0, c.y0], null);
|
|
|
|
}
|
2013-01-22 11:27:44 -08:00
|
|
|
sendOk(msg.json.command_id);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
sendError(e.message, e.code, e.stack, msg.json.command_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-10 16:55:43 -07:00
|
|
|
/**
|
|
|
|
* Function that perform a mouse tap
|
|
|
|
*/
|
|
|
|
function mousetap(target, duration, x, y, detail, button, then) {
|
|
|
|
var doc = target.ownerDocument;
|
|
|
|
detail = detail || 1;
|
|
|
|
button = button || 0;
|
|
|
|
emitMouseEvent(doc, 'mousemove', detail, button, x, y);
|
|
|
|
mouse(doc, duration, [x, x], [y, y], then, detail, button);
|
|
|
|
}
|
|
|
|
|
2013-01-22 11:27:44 -08:00
|
|
|
/**
|
|
|
|
* Function that performs a double tap
|
|
|
|
*/
|
|
|
|
function doubleTap(msg) {
|
|
|
|
let command_id = msg.json.command_id;
|
|
|
|
let el;
|
|
|
|
try {
|
|
|
|
el = elementManager.getKnownElement(msg.json.value, curWindow);
|
2013-02-01 13:29:10 -08:00
|
|
|
let x = msg.json.corx;
|
|
|
|
let y = msg.json.cory;
|
2013-01-22 11:27:44 -08:00
|
|
|
if (!checkVisible(el, command_id)) {
|
|
|
|
sendError("Element is not currently visible and may not be manipulated", 11, null, command_id);
|
|
|
|
return;
|
|
|
|
}
|
2013-02-01 13:29:10 -08:00
|
|
|
if (x == null){
|
|
|
|
x = '50%';
|
|
|
|
}
|
|
|
|
if (y == null){
|
|
|
|
y = '50%';
|
|
|
|
}
|
2013-01-22 11:27:44 -08:00
|
|
|
let c = coordinates(el, x, y);
|
2013-04-10 16:55:43 -07:00
|
|
|
if (mouseEvent) {
|
|
|
|
touch(el, 25, [c.x0, c.x0], [c.y0, c.y0], function() {
|
|
|
|
// When the first tap is done, start a timer for interval ms
|
|
|
|
checkTimer.initWithCallback(function() {
|
|
|
|
//After interval ms, send the first mouse tap
|
|
|
|
mousetap(el, 25, c.x0, c.y0, 1, 0, function() {
|
|
|
|
// when the first tap is done, start a timer for interval ms
|
|
|
|
checkTimer.initWithCallback(function() {
|
|
|
|
//After interval ms, send the second tap
|
|
|
|
touch(el, 25, [c.x0, c.x0], [c.y0, c.y0], function() {
|
|
|
|
// when the second tap is done, start a timer for interval ms
|
|
|
|
checkTimer.initWithCallback(function() {
|
|
|
|
//After interval ms, send the mousetap
|
|
|
|
mousetap(el, 25, c.x0, c.y0, 2, 0, null);
|
|
|
|
}, 50, Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
});
|
|
|
|
}, 50, Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
});
|
|
|
|
}, 50, Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
touch(el, 25, [c.x0, c.x0], [c.y0, c.y0], function() {
|
|
|
|
// When the first tap is done, start a timer for interval ms
|
|
|
|
checkTimer.initWithCallback(function() {
|
|
|
|
//After interval ms, send the second tap
|
|
|
|
touch(el, 25, [c.x0, c.x0], [c.y0, c.y0], null);
|
|
|
|
}, 50, Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
});
|
|
|
|
}
|
2013-01-22 11:27:44 -08:00
|
|
|
sendOk(msg.json.command_id);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
sendError(e.message, e.code, e.stack, msg.json.command_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-06 11:58:14 -08:00
|
|
|
/**
|
|
|
|
* Function to create a touch based on the element
|
2013-03-04 17:09:58 -08:00
|
|
|
* corx and cory are related to the el, id is the touchId
|
2013-02-06 11:58:14 -08:00
|
|
|
*/
|
|
|
|
function createATouch(el, corx, cory, id) {
|
2013-03-04 17:09:58 -08:00
|
|
|
let doc = el.ownerDocument;
|
|
|
|
let win = doc.defaultView;
|
2013-02-06 11:58:14 -08:00
|
|
|
if (corx == null) {
|
|
|
|
corx = '50%';
|
|
|
|
}
|
|
|
|
if (cory == null){
|
|
|
|
cory = '50%';
|
|
|
|
}
|
2013-03-04 17:09:58 -08:00
|
|
|
// corx and cory are relative to the el target. They must be within the same viewport
|
|
|
|
// c are the coordinates relative to the current viewport
|
|
|
|
let c = coordinates(el, corx, cory);
|
|
|
|
let clientX = Math.round(c.x0),
|
2013-02-06 11:58:14 -08:00
|
|
|
clientY = Math.round(c.y0);
|
2013-03-04 17:09:58 -08:00
|
|
|
let pageX = clientX + win.pageXOffset,
|
2013-02-06 11:58:14 -08:00
|
|
|
pageY = clientY + win.pageYOffset;
|
2013-03-04 17:09:58 -08:00
|
|
|
let screenX = clientX + win.mozInnerScreenX,
|
2013-02-06 11:58:14 -08:00
|
|
|
screenY = clientY + win.mozInnerScreenY;
|
2013-03-04 17:09:58 -08:00
|
|
|
let atouch = doc.createTouch(win, el, id, pageX, pageY, screenX, screenY, clientX, clientY);
|
2013-02-06 11:58:14 -08:00
|
|
|
return atouch;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to start a touch event
|
2013-03-04 17:09:58 -08:00
|
|
|
* corx and cory are relative to the element
|
2013-02-06 11:58:14 -08:00
|
|
|
*/
|
|
|
|
function press(msg) {
|
|
|
|
let command_id = msg.json.command_id;
|
|
|
|
let el;
|
|
|
|
try {
|
|
|
|
el = elementManager.getKnownElement(msg.json.value, curWindow);
|
|
|
|
let corx = msg.json.corx;
|
|
|
|
let cory = msg.json.cory;
|
|
|
|
if (!checkVisible(el, command_id)) {
|
|
|
|
sendError("Element is not currently visible and may not be manipulated", 11, null, command_id);
|
|
|
|
return;
|
|
|
|
}
|
2013-03-04 17:09:58 -08:00
|
|
|
let touchId = nextTouchId++;
|
|
|
|
let touch = createATouch(el, corx, cory, touchId);
|
2013-02-06 11:58:14 -08:00
|
|
|
emitTouchEvent('touchstart', touch);
|
2013-03-04 17:09:58 -08:00
|
|
|
touchIds[touchId] = touch;
|
2013-02-06 11:58:14 -08:00
|
|
|
sendResponse({value: touch.identifier}, command_id);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
sendError(e.message, e.code, e.stack, msg.json.command_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to end a touch event
|
|
|
|
*/
|
|
|
|
function release(msg) {
|
|
|
|
let command_id = msg.json.command_id;
|
|
|
|
let el;
|
|
|
|
try {
|
|
|
|
let id = msg.json.touchId;
|
2013-03-04 17:09:58 -08:00
|
|
|
if (id in touchIds) {
|
|
|
|
let startTouch = touchIds[id];
|
|
|
|
el = startTouch.target;
|
2013-02-06 11:58:14 -08:00
|
|
|
let corx = msg.json.corx;
|
|
|
|
let cory = msg.json.cory;
|
2013-04-10 16:55:43 -07:00
|
|
|
var isTap = true;
|
2013-02-06 11:58:14 -08:00
|
|
|
if (!checkVisible(el, command_id)) {
|
|
|
|
sendError("Element is not currently visible and may not be manipulated", 11, null, command_id);
|
|
|
|
return;
|
|
|
|
}
|
2013-03-04 17:09:58 -08:00
|
|
|
let touch = createATouch(el, corx, cory, id);
|
|
|
|
if (touch.clientX != startTouch.clientX ||
|
|
|
|
touch.clientY != startTouch.clientY) {
|
|
|
|
emitTouchEvent('touchmove', touch);
|
2013-04-10 16:55:43 -07:00
|
|
|
isTap = false;
|
2013-03-04 17:09:58 -08:00
|
|
|
}
|
2013-02-06 11:58:14 -08:00
|
|
|
emitTouchEvent('touchend', touch);
|
2013-04-10 16:55:43 -07:00
|
|
|
if (isTap && mouseEvent) {
|
|
|
|
mousetap(el, 25, touch.clientX, touch.clientY, 1, 0, null);
|
|
|
|
}
|
2013-03-04 17:09:58 -08:00
|
|
|
delete touchIds[id];
|
2013-02-06 11:58:14 -08:00
|
|
|
sendOk(msg.json.command_id);
|
|
|
|
}
|
|
|
|
else {
|
2013-03-19 14:59:48 -07:00
|
|
|
sendError("Element has not been pressed: no such element", 7, null, command_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
sendError(e.message, e.code, e.stack, msg.json.command_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to cancel a touch event
|
|
|
|
*/
|
|
|
|
function cancelTouch(msg) {
|
|
|
|
let command_id = msg.json.command_id;
|
|
|
|
try {
|
|
|
|
let id = msg.json.touchId;
|
|
|
|
if (id in touchIds) {
|
|
|
|
let startTouch = touchIds[id];
|
|
|
|
emitTouchEvent('touchcancel', startTouch);
|
|
|
|
delete touchIds[id];
|
|
|
|
sendOk(msg.json.command_id);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sendError("Element not previously interacted with", 7, null, command_id);
|
2013-02-06 11:58:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
sendError(e.message, e.code, e.stack, msg.json.command_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-04 17:09:58 -08:00
|
|
|
/**
|
|
|
|
* Function to emit touch events for each finger. e.g. finger=[['press', id], ['wait', 5], ['release']]
|
|
|
|
* touchId represents the finger id, i keeps track of the current action of the finger
|
|
|
|
*/
|
|
|
|
function actions(finger, touchId, command_id, i){
|
|
|
|
if (typeof i === "undefined") {
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
if (i == finger.length) {
|
2013-03-22 11:29:02 -07:00
|
|
|
sendResponse({value: touchId}, command_id);
|
2013-03-04 17:09:58 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let pack = finger[i];
|
|
|
|
let command = pack[0];
|
|
|
|
// el has the id
|
|
|
|
let el;
|
|
|
|
let corx;
|
|
|
|
let cory;
|
|
|
|
let touch;
|
2013-04-10 16:55:43 -07:00
|
|
|
let contextmenu = false;
|
2013-03-04 17:09:58 -08:00
|
|
|
i++;
|
|
|
|
switch(command) {
|
|
|
|
case 'press':
|
2013-04-15 13:19:22 -07:00
|
|
|
if (lastTouch != null) {
|
|
|
|
touch = lastTouch;
|
|
|
|
emitTouchEvent('touchcancel', touch);
|
|
|
|
lastTouch = null;
|
|
|
|
sendError("Invalid Command: long_press cannot follow an active touch event", 500, null, command_id);
|
|
|
|
return;
|
|
|
|
}
|
2013-03-04 17:09:58 -08:00
|
|
|
el = elementManager.getKnownElement(pack[1], curWindow);
|
|
|
|
corx = pack[2];
|
|
|
|
cory = pack[3];
|
|
|
|
// after this block, the element will be scrolled into view
|
|
|
|
if (!checkVisible(el, command_id)) {
|
|
|
|
sendError("Element is not currently visible and may not be manipulated", 11, null, command_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
touch = createATouch(el, corx, cory, touchId);
|
|
|
|
lastTouch = touch;
|
2013-04-10 16:55:43 -07:00
|
|
|
isTouchStart = true;
|
2013-03-04 17:09:58 -08:00
|
|
|
emitTouchEvent('touchstart', touch);
|
|
|
|
actions(finger,touchId, command_id, i);
|
|
|
|
break;
|
|
|
|
case 'release':
|
2013-03-22 11:29:02 -07:00
|
|
|
if (lastTouch == null) {
|
|
|
|
sendError("Element has not been pressed: no such element", 7, null, command_id);
|
|
|
|
return;
|
|
|
|
}
|
2013-03-04 17:09:58 -08:00
|
|
|
touch = lastTouch;
|
2013-03-19 14:59:48 -07:00
|
|
|
lastTouch = null;
|
2013-03-04 17:09:58 -08:00
|
|
|
emitTouchEvent('touchend', touch);
|
2013-04-10 16:55:43 -07:00
|
|
|
if (isTouchStart && mouseEvent) {
|
|
|
|
mousetap(touch.target, 25, touch.clientX, touch.clientY, 1, 0, null);
|
|
|
|
}
|
|
|
|
isTouchStart = false;
|
2013-03-04 17:09:58 -08:00
|
|
|
actions(finger, touchId, command_id, i);
|
|
|
|
break;
|
|
|
|
case 'move':
|
2013-03-22 11:29:02 -07:00
|
|
|
if (lastTouch == null) {
|
|
|
|
sendError("Element has not been pressed: no such element", 7, null, command_id);
|
|
|
|
return;
|
|
|
|
}
|
2013-03-04 17:09:58 -08:00
|
|
|
el = elementManager.getKnownElement(pack[1], curWindow);
|
|
|
|
let boxTarget = el.getBoundingClientRect();
|
|
|
|
let startElement = lastTouch.target;
|
|
|
|
let boxStart = startElement.getBoundingClientRect();
|
|
|
|
corx = boxTarget.left - boxStart.left + boxTarget.width * 0.5;
|
|
|
|
cory = boxTarget.top - boxStart.top + boxTarget.height * 0.5;
|
|
|
|
touch = createATouch(startElement, corx, cory, touchId);
|
|
|
|
lastTouch = touch;
|
2013-04-10 16:55:43 -07:00
|
|
|
isTouchStart = false;
|
2013-03-04 17:09:58 -08:00
|
|
|
emitTouchEvent('touchmove', touch);
|
|
|
|
actions(finger, touchId, command_id, i);
|
|
|
|
break;
|
|
|
|
case 'moveByOffset':
|
2013-03-22 11:29:02 -07:00
|
|
|
if (lastTouch == null) {
|
|
|
|
sendError("Element has not been pressed: no such element", 7, null, command_id);
|
|
|
|
return;
|
|
|
|
}
|
2013-03-04 17:09:58 -08:00
|
|
|
el = lastTouch.target;
|
|
|
|
let doc = el.ownerDocument;
|
|
|
|
let win = doc.defaultView;
|
|
|
|
let clientX = lastTouch.clientX + pack[1],
|
|
|
|
clientY = lastTouch.clientY + pack[2];
|
|
|
|
let pageX = clientX + win.pageXOffset,
|
|
|
|
pageY = clientY + win.pageYOffset;
|
|
|
|
let screenX = clientX + win.mozInnerScreenX,
|
|
|
|
screenY = clientY + win.mozInnerScreenY;
|
|
|
|
touch = doc.createTouch(win, el, touchId, pageX, pageY, screenX, screenY, clientX, clientY);
|
|
|
|
lastTouch = touch;
|
2013-04-10 16:55:43 -07:00
|
|
|
isTouchStart = false;
|
2013-03-04 17:09:58 -08:00
|
|
|
emitTouchEvent('touchmove', touch);
|
|
|
|
actions(finger, touchId, command_id, i);
|
|
|
|
break;
|
|
|
|
case 'wait':
|
|
|
|
if (pack[1] != null ) {
|
|
|
|
let time = pack[1]*1000;
|
2013-04-10 16:55:43 -07:00
|
|
|
// standard waiting time to fire contextmenu
|
|
|
|
let standard = Services.prefs.getIntPref("ui.click_hold_context_menus.delay");
|
|
|
|
if (time >= standard && isTouchStart && !contextmenu) {
|
|
|
|
finger.splice(i, 0, ['longPress'], ['wait', (time-standard)/1000]);
|
|
|
|
time = standard;
|
|
|
|
}
|
2013-03-04 17:09:58 -08:00
|
|
|
checkTimer.initWithCallback(function(){actions(finger, touchId, command_id, i);}, time, Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
actions(finger, touchId, command_id, i);
|
|
|
|
}
|
|
|
|
break;
|
2013-03-19 14:59:48 -07:00
|
|
|
case 'cancel':
|
|
|
|
touch = lastTouch;
|
|
|
|
emitTouchEvent('touchcancel', touch);
|
|
|
|
lastTouch = null;
|
2013-04-10 16:55:43 -07:00
|
|
|
isTouchStart = false;
|
2013-03-19 14:59:48 -07:00
|
|
|
actions(finger, touchId, command_id, i);
|
|
|
|
break;
|
2013-04-01 15:21:06 -07:00
|
|
|
case 'longPress':
|
2013-04-10 16:55:43 -07:00
|
|
|
isTouchStart = false;
|
|
|
|
contextmenu = true;
|
2013-04-01 15:21:06 -07:00
|
|
|
let event = curWindow.document.createEvent('HTMLEvents');
|
|
|
|
event.initEvent('contextmenu',
|
|
|
|
true,
|
|
|
|
true);
|
|
|
|
lastTouch.target.dispatchEvent(event);
|
|
|
|
actions(finger, touchId, command_id, i);
|
|
|
|
break;
|
2013-03-04 17:09:58 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to start action chain on one finger
|
|
|
|
*/
|
|
|
|
function actionChain(msg) {
|
|
|
|
let command_id = msg.json.command_id;
|
2013-03-22 11:29:02 -07:00
|
|
|
let args = msg.json.chain;
|
|
|
|
let touchId = msg.json.nextId;
|
2013-03-04 17:09:58 -08:00
|
|
|
try {
|
|
|
|
let commandArray = elementManager.convertWrappedArguments(args, curWindow);
|
|
|
|
// loop the action array [ ['press', id], ['move', id], ['release', id] ]
|
2013-03-22 11:29:02 -07:00
|
|
|
if (touchId == null) {
|
|
|
|
touchId = nextTouchId++;
|
|
|
|
}
|
2013-03-04 17:09:58 -08:00
|
|
|
actions(commandArray, touchId, command_id);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
sendError(e.message, e.code, e.stack, msg.json.command_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-18 13:42:46 -07:00
|
|
|
/**
|
|
|
|
* Function to emit touch events which allow multi touch on the screen
|
|
|
|
* @param type represents the type of event, touch represents the current touch,touches are all pending touches
|
|
|
|
*/
|
|
|
|
function emitMultiEvents(type, touch, touches) {
|
|
|
|
let target = touch.target;
|
|
|
|
let doc = target.ownerDocument;
|
|
|
|
let win = doc.defaultView;
|
|
|
|
// touches that are in the same document
|
|
|
|
let documentTouches = doc.createTouchList(touches.filter(function(t) {
|
|
|
|
return t.target.ownerDocument === doc;
|
|
|
|
}));
|
|
|
|
// touches on the same target
|
|
|
|
let targetTouches = doc.createTouchList(touches.filter(function(t) {
|
|
|
|
return t.target === target;
|
|
|
|
}));
|
|
|
|
// Create changed touches
|
|
|
|
let changedTouches = doc.createTouchList(touch);
|
|
|
|
// Create the event object
|
|
|
|
let event = curWindow.document.createEvent('TouchEvent');
|
|
|
|
event.initTouchEvent(type,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
win,
|
|
|
|
0,
|
|
|
|
false, false, false, false,
|
|
|
|
documentTouches,
|
|
|
|
targetTouches,
|
|
|
|
changedTouches);
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to dispatch one set of actions
|
|
|
|
* @param touches represents all pending touches, batchIndex represents the batch we are dispatching right now
|
|
|
|
*/
|
|
|
|
function setDispatch(batches, touches, command_id, batchIndex) {
|
|
|
|
if (typeof batchIndex === "undefined") {
|
|
|
|
batchIndex = 0;
|
|
|
|
}
|
|
|
|
// check if all the sets have been fired
|
|
|
|
if (batchIndex >= batches.length) {
|
|
|
|
multiLast = {};
|
|
|
|
sendOk(command_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// a set of actions need to be done
|
|
|
|
let batch = batches[batchIndex];
|
|
|
|
// each action for some finger
|
|
|
|
let pack;
|
|
|
|
// the touch id for the finger (pack)
|
|
|
|
let touchId;
|
|
|
|
// command for the finger
|
|
|
|
let command;
|
|
|
|
// touch that will be created for the finger
|
|
|
|
let el;
|
|
|
|
let corx;
|
|
|
|
let cory;
|
|
|
|
let touch;
|
|
|
|
let lastTouch;
|
|
|
|
let touchIndex;
|
|
|
|
let waitTime = 0;
|
|
|
|
let maxTime = 0;
|
|
|
|
batchIndex++;
|
|
|
|
// loop through the batch
|
|
|
|
for (let i = 0; i < batch.length; i++) {
|
|
|
|
pack = batch[i];
|
|
|
|
touchId = pack[0];
|
|
|
|
command = pack[1];
|
|
|
|
switch (command) {
|
|
|
|
case 'press':
|
|
|
|
el = elementManager.getKnownElement(pack[2], curWindow);
|
|
|
|
// after this block, the element will be scrolled into view
|
|
|
|
if (!checkVisible(el, command_id)) {
|
|
|
|
sendError("Element is not currently visible and may not be manipulated", 11, null, command_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
corx = pack[3];
|
|
|
|
cory = pack[4];
|
|
|
|
touch = createATouch(el, corx, cory, touchId);
|
|
|
|
multiLast[touchId] = touch;
|
|
|
|
touches.push(touch);
|
|
|
|
emitMultiEvents('touchstart', touch, touches);
|
|
|
|
break;
|
|
|
|
case 'release':
|
|
|
|
touch = multiLast[touchId];
|
|
|
|
// the index of the previous touch for the finger may change in the touches array
|
|
|
|
touchIndex = touches.indexOf(touch);
|
|
|
|
touches.splice(touchIndex, 1);
|
|
|
|
emitMultiEvents('touchend', touch, touches);
|
|
|
|
break;
|
|
|
|
case 'move':
|
|
|
|
el = elementManager.getKnownElement(pack[2], curWindow);
|
|
|
|
lastTouch = multiLast[touchId];
|
|
|
|
let boxTarget = el.getBoundingClientRect();
|
|
|
|
let startTarget = lastTouch.target;
|
|
|
|
let boxStart = startTarget.getBoundingClientRect();
|
|
|
|
// note here corx and cory are relative to the target, not the viewport
|
|
|
|
// we always want to touch the center of the element if the element is specified
|
|
|
|
corx = boxTarget.left - boxStart.left + boxTarget.width * 0.5;
|
|
|
|
cory = boxTarget.top - boxStart.top + boxTarget.height * 0.5;
|
|
|
|
touch = createATouch(startTarget, corx, cory, touchId);
|
|
|
|
touchIndex = touches.indexOf(lastTouch);
|
|
|
|
touches[touchIndex] = touch;
|
|
|
|
multiLast[touchId] = touch;
|
|
|
|
emitMultiEvents('touchmove', touch, touches);
|
|
|
|
break;
|
|
|
|
case 'moveByOffset':
|
|
|
|
el = multiLast[touchId].target;
|
|
|
|
lastTouch = multiLast[touchId];
|
|
|
|
touchIndex = touches.indexOf(lastTouch);
|
|
|
|
let doc = el.ownerDocument;
|
|
|
|
let win = doc.defaultView;
|
|
|
|
// since x and y are relative to the last touch, therefore, it's relative to the position of the last touch
|
|
|
|
let clientX = lastTouch.clientX + pack[2],
|
|
|
|
clientY = lastTouch.clientY + pack[3];
|
|
|
|
let pageX = clientX + win.pageXOffset,
|
|
|
|
pageY = clientY + win.pageYOffset;
|
|
|
|
let screenX = clientX + win.mozInnerScreenX,
|
|
|
|
screenY = clientY + win.mozInnerScreenY;
|
|
|
|
touch = doc.createTouch(win, el, touchId, pageX, pageY, screenX, screenY, clientX, clientY);
|
|
|
|
touches[touchIndex] = touch;
|
|
|
|
multiLast[touchId] = touch;
|
|
|
|
emitMultiEvents('touchmove', touch, touches);
|
|
|
|
break;
|
|
|
|
case 'wait':
|
|
|
|
if (pack[2] != undefined ) {
|
|
|
|
waitTime = pack[2]*1000;
|
|
|
|
if (waitTime > maxTime) {
|
|
|
|
maxTime = waitTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}//end of switch block
|
|
|
|
}//end of for loop
|
|
|
|
if (maxTime != 0) {
|
|
|
|
checkTimer.initWithCallback(function(){setDispatch(batches, touches, command_id, batchIndex);}, maxTime, Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setDispatch(batches, touches, command_id, batchIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to start multi-action
|
|
|
|
*/
|
|
|
|
function multiAction(msg) {
|
|
|
|
let command_id = msg.json.command_id;
|
|
|
|
let args = msg.json.value;
|
|
|
|
// maxlen is the longest action chain for one finger
|
|
|
|
let maxlen = msg.json.maxlen;
|
|
|
|
try {
|
|
|
|
// unwrap the original nested array
|
|
|
|
let commandArray = elementManager.convertWrappedArguments(args, curWindow);
|
|
|
|
let concurrentEvent = [];
|
|
|
|
let temp;
|
|
|
|
for (let i = 0; i < maxlen; i++) {
|
|
|
|
let row = [];
|
|
|
|
for (let j = 0; j < commandArray.length; j++) {
|
|
|
|
if (commandArray[j][i] != undefined) {
|
|
|
|
// add finger id to the front of each action, i.e. [finger_id, action, element]
|
|
|
|
temp = commandArray[j][i];
|
|
|
|
temp.unshift(j);
|
|
|
|
row.push(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
concurrentEvent.push(row);
|
|
|
|
}
|
|
|
|
// now concurrent event is made of sets where each set contain a list of actions that need to be fired.
|
|
|
|
// note: each action belongs to a different finger
|
|
|
|
// pendingTouches keeps track of current touches that's on the screen
|
|
|
|
let pendingTouches = [];
|
|
|
|
setDispatch(concurrentEvent, pendingTouches, command_id);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
sendError(e.message, e.code, e.stack, msg.json.command_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-22 08:19:57 -07:00
|
|
|
/**
|
|
|
|
* Function to set the timeout period for element searching
|
|
|
|
*/
|
|
|
|
function setSearchTimeout(msg) {
|
|
|
|
try {
|
|
|
|
elementManager.setSearchTimeout(msg.json.value);
|
|
|
|
}
|
|
|
|
catch (e) {
|
2012-11-26 18:19:04 -08:00
|
|
|
sendError(e.message, e.code, e.stack, msg.json.command_id);
|
2012-03-22 08:19:57 -07:00
|
|
|
return;
|
|
|
|
}
|
2012-11-26 18:19:04 -08:00
|
|
|
sendOk(msg.json.command_id);
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Navigate to URI. Handles the case where we navigate within an iframe.
|
|
|
|
* All other navigation is handled by the server (in chrome space).
|
|
|
|
*/
|
|
|
|
function goUrl(msg) {
|
2012-11-26 18:19:04 -08:00
|
|
|
let command_id = msg.json.command_id;
|
2013-01-11 11:33:58 -08:00
|
|
|
// Prevent DOMContentLoaded events from frames from invoking this code,
|
|
|
|
// unless the event is coming from the frame associated with the current
|
|
|
|
// window (i.e., someone has used switch_to_frame).
|
|
|
|
let onDOMContentLoaded = function onDOMContentLoaded(event){
|
|
|
|
if (msg.json.pageTimeout != null){
|
|
|
|
checkTimer.cancel();
|
|
|
|
}
|
|
|
|
if (!event.originalTarget.defaultView.frameElement ||
|
|
|
|
event.originalTarget.defaultView.frameElement == curWindow.frameElement) {
|
2012-08-21 14:00:44 -07:00
|
|
|
removeEventListener("DOMContentLoaded", onDOMContentLoaded, false);
|
|
|
|
let errorRegex = /about:.+(error)|(blocked)\?/;
|
|
|
|
if (curWindow.document.readyState == "interactive" && errorRegex.exec(curWindow.document.baseURI)) {
|
2012-11-26 18:19:04 -08:00
|
|
|
sendError("Error loading page", 13, null, command_id);
|
2012-08-21 14:00:44 -07:00
|
|
|
return;
|
|
|
|
}
|
2012-11-26 18:19:04 -08:00
|
|
|
sendOk(command_id);
|
2012-08-21 14:00:44 -07:00
|
|
|
}
|
2013-01-11 11:33:58 -08:00
|
|
|
};
|
|
|
|
function timerFunc(){
|
|
|
|
sendError("Error loading page", 13, null, command_id);
|
|
|
|
removeEventListener("DOMContentLoaded", onDOMContentLoaded, false);
|
|
|
|
}
|
|
|
|
if (msg.json.pageTimeout != null){
|
|
|
|
checkTimer.initWithCallback(timerFunc, msg.json.pageTimeout, Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
addEventListener("DOMContentLoaded", onDOMContentLoaded, false);
|
2012-08-21 14:00:44 -07:00
|
|
|
curWindow.location = msg.json.value;
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current URI
|
|
|
|
*/
|
|
|
|
function getUrl(msg) {
|
2012-11-26 18:19:04 -08:00
|
|
|
sendResponse({value: curWindow.location.href}, msg.json.command_id);
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
|
2012-07-10 13:30:21 -07:00
|
|
|
/**
|
|
|
|
* Get the current Title of the window
|
|
|
|
*/
|
|
|
|
function getTitle(msg) {
|
2012-11-26 18:19:04 -08:00
|
|
|
sendResponse({value: curWindow.top.document.title}, msg.json.command_id);
|
2012-07-10 13:30:21 -07:00
|
|
|
}
|
|
|
|
|
2012-08-09 12:31:12 -07:00
|
|
|
/**
|
|
|
|
* Get the current page source
|
|
|
|
*/
|
|
|
|
function getPageSource(msg) {
|
|
|
|
var XMLSerializer = curWindow.XMLSerializer;
|
|
|
|
var pageSource = new XMLSerializer().serializeToString(curWindow.document);
|
2012-11-26 18:19:04 -08:00
|
|
|
sendResponse({value: pageSource}, msg.json.command_id);
|
2012-08-09 12:31:12 -07:00
|
|
|
}
|
|
|
|
|
2012-03-22 08:19:57 -07:00
|
|
|
/**
|
|
|
|
* Go back in history
|
|
|
|
*/
|
|
|
|
function goBack(msg) {
|
2012-05-02 07:21:05 -07:00
|
|
|
curWindow.history.back();
|
2012-11-26 18:19:04 -08:00
|
|
|
sendOk(msg.json.command_id);
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Go forward in history
|
|
|
|
*/
|
|
|
|
function goForward(msg) {
|
2012-05-02 07:21:05 -07:00
|
|
|
curWindow.history.forward();
|
2012-11-26 18:19:04 -08:00
|
|
|
sendOk(msg.json.command_id);
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Refresh the page
|
|
|
|
*/
|
|
|
|
function refresh(msg) {
|
2012-11-26 18:19:04 -08:00
|
|
|
let command_id = msg.json.command_id;
|
2012-05-02 07:21:05 -07:00
|
|
|
curWindow.location.reload(true);
|
2012-11-26 18:19:04 -08:00
|
|
|
let listen = function() {
|
|
|
|
removeEventListener("DOMContentLoaded", arguments.callee, false);
|
|
|
|
sendOk(command_id);
|
|
|
|
};
|
2012-03-22 08:19:57 -07:00
|
|
|
addEventListener("DOMContentLoaded", listen, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find an element in the document using requested search strategy
|
|
|
|
*/
|
|
|
|
function findElementContent(msg) {
|
2012-11-26 18:19:04 -08:00
|
|
|
let command_id = msg.json.command_id;
|
2012-03-22 08:19:57 -07:00
|
|
|
try {
|
2012-11-26 18:19:04 -08:00
|
|
|
let on_success = function(id, cmd_id) { sendResponse({value:id}, cmd_id); };
|
2012-08-27 13:50:34 -07:00
|
|
|
let on_error = sendError;
|
2012-11-26 18:19:04 -08:00
|
|
|
elementManager.find(curWindow, msg.json, on_success, on_error, false, command_id);
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
catch (e) {
|
2012-11-26 18:19:04 -08:00
|
|
|
sendError(e.message, e.code, e.stack, command_id);
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find elements in the document using requested search strategy
|
|
|
|
*/
|
|
|
|
function findElementsContent(msg) {
|
2012-11-26 18:19:04 -08:00
|
|
|
let command_id = msg.json.command_id;
|
2012-03-22 08:19:57 -07:00
|
|
|
try {
|
2012-11-26 18:19:04 -08:00
|
|
|
let on_success = function(id, cmd_id) { sendResponse({value:id}, cmd_id); };
|
2012-08-27 13:50:34 -07:00
|
|
|
let on_error = sendError;
|
2012-11-26 18:19:04 -08:00
|
|
|
elementManager.find(curWindow, msg.json, on_success, on_error, true, command_id);
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
catch (e) {
|
2012-11-26 18:19:04 -08:00
|
|
|
sendError(e.message, e.code, e.stack, command_id);
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-14 06:57:54 -08:00
|
|
|
/**
|
|
|
|
* Find and return the active element on the page
|
|
|
|
*/
|
|
|
|
function getActiveElement(msg) {
|
|
|
|
let command_id = msg.json.command_id;
|
|
|
|
var element = curWindow.document.activeElement;
|
|
|
|
var id = elementManager.addToKnownElements(element);
|
|
|
|
sendResponse({value: id}, command_id);
|
|
|
|
}
|
|
|
|
|
2012-03-22 08:19:57 -07:00
|
|
|
/**
|
|
|
|
* Send click event to element
|
|
|
|
*/
|
|
|
|
function clickElement(msg) {
|
2012-11-26 18:19:04 -08:00
|
|
|
let command_id = msg.json.command_id;
|
2012-03-22 08:19:57 -07:00
|
|
|
let el;
|
|
|
|
try {
|
2012-05-02 07:21:05 -07:00
|
|
|
el = elementManager.getKnownElement(msg.json.element, curWindow);
|
2013-04-04 07:59:36 -07:00
|
|
|
if (checkVisible(el, command_id)) {
|
2013-04-02 05:35:27 -07:00
|
|
|
if (utils.isElementEnabled(el)) {
|
|
|
|
utils.synthesizeMouseAtCenter(el, {}, el.ownerDocument.defaultView)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sendError("Element is not Enabled", 12, null, command_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sendError("Element is not visible", 11, null, command_id)
|
|
|
|
}
|
2012-11-26 18:19:04 -08:00
|
|
|
sendOk(command_id);
|
2012-04-10 17:28:08 -07:00
|
|
|
}
|
|
|
|
catch (e) {
|
2012-11-26 18:19:04 -08:00
|
|
|
sendError(e.message, e.code, e.stack, command_id);
|
2012-04-10 17:28:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a given attribute of an element
|
|
|
|
*/
|
2012-05-01 12:37:34 -07:00
|
|
|
function getElementAttribute(msg) {
|
2012-11-26 18:19:04 -08:00
|
|
|
let command_id = msg.json.command_id;
|
2012-04-10 17:28:08 -07:00
|
|
|
try {
|
2012-05-02 07:21:05 -07:00
|
|
|
let el = elementManager.getKnownElement(msg.json.element, curWindow);
|
2012-11-26 18:19:04 -08:00
|
|
|
sendResponse({value: utils.getElementAttribute(el, msg.json.name)},
|
|
|
|
command_id);
|
2012-04-10 17:28:08 -07:00
|
|
|
}
|
|
|
|
catch (e) {
|
2012-11-26 18:19:04 -08:00
|
|
|
sendError(e.message, e.code, e.stack, command_id);
|
2012-04-10 17:28:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the text of this element. This includes text from child elements.
|
|
|
|
*/
|
|
|
|
function getElementText(msg) {
|
2012-11-26 18:19:04 -08:00
|
|
|
let command_id = msg.json.command_id;
|
2012-04-10 17:28:08 -07:00
|
|
|
try {
|
2012-05-02 07:21:05 -07:00
|
|
|
let el = elementManager.getKnownElement(msg.json.element, curWindow);
|
2012-11-26 18:19:04 -08:00
|
|
|
sendResponse({value: utils.getElementText(el)}, command_id);
|
2012-04-10 17:28:08 -07:00
|
|
|
}
|
|
|
|
catch (e) {
|
2012-11-26 18:19:04 -08:00
|
|
|
sendError(e.message, e.code, e.stack, command_id);
|
2012-04-10 17:28:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-08 13:21:50 -07:00
|
|
|
/**
|
|
|
|
* Get the tag name of an element.
|
|
|
|
*/
|
|
|
|
function getElementTagName(msg) {
|
2012-11-26 18:19:04 -08:00
|
|
|
let command_id = msg.json.command_id;
|
2012-08-08 13:21:50 -07:00
|
|
|
try {
|
|
|
|
let el = elementManager.getKnownElement(msg.json.element, curWindow);
|
2012-11-26 18:19:04 -08:00
|
|
|
sendResponse({value: el.tagName.toLowerCase()}, command_id);
|
2012-08-08 13:21:50 -07:00
|
|
|
}
|
|
|
|
catch (e) {
|
2012-11-26 18:19:04 -08:00
|
|
|
sendError(e.message, e.code, e.stack, command_id);
|
2012-08-08 13:21:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-10 17:28:08 -07:00
|
|
|
/**
|
|
|
|
* Check if element is displayed
|
|
|
|
*/
|
|
|
|
function isElementDisplayed(msg) {
|
2012-11-26 18:19:04 -08:00
|
|
|
let command_id = msg.json.command_id;
|
2012-04-10 17:28:08 -07:00
|
|
|
try {
|
2012-05-02 07:21:05 -07:00
|
|
|
let el = elementManager.getKnownElement(msg.json.element, curWindow);
|
2012-11-26 18:19:04 -08:00
|
|
|
sendResponse({value: utils.isElementDisplayed(el)}, command_id);
|
2012-04-10 17:28:08 -07:00
|
|
|
}
|
|
|
|
catch (e) {
|
2012-11-26 18:19:04 -08:00
|
|
|
sendError(e.message, e.code, e.stack, command_id);
|
2012-04-10 17:28:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-19 13:59:15 -07:00
|
|
|
/**
|
|
|
|
* Get the size of the element and return it
|
|
|
|
*/
|
|
|
|
function getElementSize(msg){
|
2012-11-26 18:19:04 -08:00
|
|
|
let command_id = msg.json.command_id;
|
2012-10-19 13:59:15 -07:00
|
|
|
try {
|
|
|
|
let el = elementManager.getKnownElement(msg.json.element, curWindow);
|
|
|
|
let clientRect = el.getBoundingClientRect();
|
2012-11-26 18:19:04 -08:00
|
|
|
sendResponse({value: {width: clientRect.width, height: clientRect.height}},
|
|
|
|
command_id);
|
2012-10-19 13:59:15 -07:00
|
|
|
}
|
|
|
|
catch (e) {
|
2012-11-26 18:19:04 -08:00
|
|
|
sendError(e.message, e.code, e.stack, command_id);
|
2012-10-19 13:59:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-10 17:28:08 -07:00
|
|
|
/**
|
|
|
|
* Check if element is enabled
|
|
|
|
*/
|
|
|
|
function isElementEnabled(msg) {
|
2012-11-26 18:19:04 -08:00
|
|
|
let command_id = msg.json.command_id;
|
2012-04-10 17:28:08 -07:00
|
|
|
try {
|
2012-05-02 07:21:05 -07:00
|
|
|
let el = elementManager.getKnownElement(msg.json.element, curWindow);
|
2012-11-26 18:19:04 -08:00
|
|
|
sendResponse({value: utils.isElementEnabled(el)}, command_id);
|
2012-04-10 17:28:08 -07:00
|
|
|
}
|
|
|
|
catch (e) {
|
2012-11-26 18:19:04 -08:00
|
|
|
sendError(e.message, e.code, e.stack, command_id);
|
2012-04-10 17:28:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if element is selected
|
|
|
|
*/
|
|
|
|
function isElementSelected(msg) {
|
2012-11-26 18:19:04 -08:00
|
|
|
let command_id = msg.json.command_id;
|
2012-04-10 17:28:08 -07:00
|
|
|
try {
|
2012-05-02 07:21:05 -07:00
|
|
|
let el = elementManager.getKnownElement(msg.json.element, curWindow);
|
2012-11-26 18:19:04 -08:00
|
|
|
sendResponse({value: utils.isElementSelected(el)}, command_id);
|
2012-04-10 17:28:08 -07:00
|
|
|
}
|
|
|
|
catch (e) {
|
2012-11-26 18:19:04 -08:00
|
|
|
sendError(e.message, e.code, e.stack, command_id);
|
2012-04-10 17:28:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send keys to element
|
|
|
|
*/
|
|
|
|
function sendKeysToElement(msg) {
|
2012-11-26 18:19:04 -08:00
|
|
|
let command_id = msg.json.command_id;
|
2012-04-10 17:28:08 -07:00
|
|
|
try {
|
2012-05-02 07:21:05 -07:00
|
|
|
let el = elementManager.getKnownElement(msg.json.element, curWindow);
|
2013-04-11 13:52:42 -07:00
|
|
|
if (checkVisible(el, command_id)) {
|
|
|
|
utils.type(curWindow.document, el, msg.json.value.join(""), true);
|
|
|
|
sendOk(command_id);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sendError("Element is not visible", 11, null, command_id)
|
|
|
|
}
|
2012-04-10 17:28:08 -07:00
|
|
|
}
|
|
|
|
catch (e) {
|
2012-11-26 18:19:04 -08:00
|
|
|
sendError(e.message, e.code, e.stack, command_id);
|
2012-04-10 17:28:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-19 13:35:30 -07:00
|
|
|
/**
|
|
|
|
* Get the position of an element
|
|
|
|
*/
|
|
|
|
function getElementPosition(msg) {
|
2012-11-26 18:19:04 -08:00
|
|
|
let command_id = msg.json.command_id;
|
2012-10-19 13:35:30 -07:00
|
|
|
try{
|
|
|
|
let el = elementManager.getKnownElement(msg.json.element, curWindow);
|
2013-04-03 05:14:38 -07:00
|
|
|
let rect = el.getBoundingClientRect();
|
2012-10-19 13:35:30 -07:00
|
|
|
|
|
|
|
let location = {};
|
2013-04-03 05:14:38 -07:00
|
|
|
location.x = rect.left;
|
|
|
|
location.y = rect.top;
|
2012-10-19 13:35:30 -07:00
|
|
|
|
2012-11-26 18:19:04 -08:00
|
|
|
sendResponse({value: location}, command_id);
|
2012-10-19 13:35:30 -07:00
|
|
|
}
|
|
|
|
catch (e) {
|
2012-11-26 18:19:04 -08:00
|
|
|
sendError(e.message, e.code, e.stack, command_id);
|
2012-10-19 13:35:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-10 17:28:08 -07:00
|
|
|
/**
|
|
|
|
* Clear the text of an element
|
|
|
|
*/
|
|
|
|
function clearElement(msg) {
|
2012-11-26 18:19:04 -08:00
|
|
|
let command_id = msg.json.command_id;
|
2012-04-10 17:28:08 -07:00
|
|
|
try {
|
2012-05-02 07:21:05 -07:00
|
|
|
let el = elementManager.getKnownElement(msg.json.element, curWindow);
|
2012-04-10 17:28:08 -07:00
|
|
|
utils.clearElement(el);
|
2012-11-26 18:19:04 -08:00
|
|
|
sendOk(command_id);
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
catch (e) {
|
2012-11-26 18:19:04 -08:00
|
|
|
sendError(e.message, e.code, e.stack, command_id);
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Switch to frame given either the server-assigned element id,
|
|
|
|
* its index in window.frames, or the iframe's name or id.
|
|
|
|
*/
|
|
|
|
function switchToFrame(msg) {
|
2012-11-26 18:19:04 -08:00
|
|
|
let command_id = msg.json.command_id;
|
2012-11-14 10:35:44 -08:00
|
|
|
function checkLoad() {
|
|
|
|
let errorRegex = /about:.+(error)|(blocked)\?/;
|
|
|
|
if (curWindow.document.readyState == "complete") {
|
2012-11-26 18:19:04 -08:00
|
|
|
sendOk(command_id);
|
2012-11-14 10:35:44 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (curWindow.document.readyState == "interactive" && errorRegex.exec(curWindow.document.baseURI)) {
|
2012-11-26 18:19:04 -08:00
|
|
|
sendError("Error loading page", 13, null, command_id);
|
2012-11-14 10:35:44 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
checkTimer.initWithCallback(checkLoad, 100, Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
}
|
2012-03-22 08:19:57 -07:00
|
|
|
let foundFrame = null;
|
2013-01-16 07:00:00 -08:00
|
|
|
let frames = []; //curWindow.document.getElementsByTagName("iframe");
|
|
|
|
let parWindow = null; //curWindow.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
// Check of the curWindow reference is dead
|
|
|
|
try {
|
|
|
|
frames = curWindow.document.getElementsByTagName("iframe");
|
|
|
|
//Until Bug 761935 lands, we won't have multiple nested OOP iframes. We will only have one.
|
|
|
|
//parWindow will refer to the iframe above the nested OOP frame.
|
|
|
|
parWindow = curWindow.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils).outerWindowID;
|
|
|
|
} catch (e) {
|
|
|
|
// We probably have a dead compartment so accessing it is going to make Firefox
|
|
|
|
// very upset. Let's now try redirect everything to the top frame even if the
|
|
|
|
// user has given us a frame since search doesnt look up.
|
|
|
|
msg.json.value = null;
|
|
|
|
msg.json.element = null;
|
|
|
|
}
|
2012-03-22 08:19:57 -07:00
|
|
|
if ((msg.json.value == null) && (msg.json.element == null)) {
|
2012-05-02 07:21:05 -07:00
|
|
|
curWindow = content;
|
2012-12-17 01:45:28 -08:00
|
|
|
if(msg.json.focus == true) {
|
|
|
|
curWindow.focus();
|
|
|
|
}
|
2013-04-05 10:15:19 -07:00
|
|
|
sandbox = null;
|
2012-11-14 10:35:44 -08:00
|
|
|
checkTimer.initWithCallback(checkLoad, 100, Ci.nsITimer.TYPE_ONE_SHOT);
|
2012-03-22 08:19:57 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (msg.json.element != undefined) {
|
|
|
|
if (elementManager.seenItems[msg.json.element] != undefined) {
|
2013-02-04 11:40:51 -08:00
|
|
|
let wantedFrame;
|
|
|
|
try {
|
|
|
|
wantedFrame = elementManager.getKnownElement(msg.json.element, curWindow); //HTMLIFrameElement
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
sendError(e.message, e.code, e.stack, command_id);
|
|
|
|
}
|
2012-10-19 11:46:58 -07:00
|
|
|
for (let i = 0; i < frames.length; i++) {
|
2013-02-04 11:40:51 -08:00
|
|
|
// use XPCNativeWrapper to compare elements; see bug 834266
|
|
|
|
if (XPCNativeWrapper(frames[i]) == XPCNativeWrapper(wantedFrame)) {
|
2012-10-19 11:46:58 -07:00
|
|
|
curWindow = frames[i];
|
2012-11-06 10:54:09 -08:00
|
|
|
foundFrame = i;
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-06 10:54:09 -08:00
|
|
|
if (foundFrame == null) {
|
|
|
|
switch(typeof(msg.json.value)) {
|
|
|
|
case "string" :
|
|
|
|
let foundById = null;
|
|
|
|
for (let i = 0; i < frames.length; i++) {
|
|
|
|
//give precedence to name
|
|
|
|
let frame = frames[i];
|
|
|
|
let name = utils.getElementAttribute(frame, 'name');
|
|
|
|
let id = utils.getElementAttribute(frame, 'id');
|
|
|
|
if (name == msg.json.value) {
|
|
|
|
foundFrame = i;
|
|
|
|
break;
|
|
|
|
} else if ((foundById == null) && (id == msg.json.value)) {
|
|
|
|
foundById = i;
|
|
|
|
}
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
2012-11-06 10:54:09 -08:00
|
|
|
if ((foundFrame == null) && (foundById != null)) {
|
|
|
|
foundFrame = foundById;
|
|
|
|
curWindow = frames[foundFrame];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "number":
|
|
|
|
if (frames[msg.json.value] != undefined) {
|
|
|
|
foundFrame = msg.json.value;
|
|
|
|
curWindow = frames[foundFrame];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
2012-05-09 12:05:39 -07:00
|
|
|
if (foundFrame == null) {
|
2012-11-26 18:19:04 -08:00
|
|
|
sendError("Unable to locate frame: " + msg.json.value, 8, null, command_id);
|
2012-05-09 12:05:39 -07:00
|
|
|
return;
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
2012-05-09 12:05:39 -07:00
|
|
|
|
|
|
|
sandbox = null;
|
2012-09-28 16:16:22 -07:00
|
|
|
|
|
|
|
if (curWindow.contentWindow == null) {
|
|
|
|
// The frame we want to switch to is a remote frame; notify our parent to handle
|
|
|
|
// the switch.
|
|
|
|
curWindow = content;
|
2012-11-26 18:19:04 -08:00
|
|
|
sendToServer('Marionette:switchToFrame', {frame: foundFrame,
|
|
|
|
win: parWindow,
|
|
|
|
command_id: command_id});
|
2012-09-28 16:16:22 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
curWindow = curWindow.contentWindow;
|
2012-12-17 01:45:28 -08:00
|
|
|
if(msg.json.focus == true) {
|
|
|
|
curWindow.focus();
|
|
|
|
}
|
2012-11-14 10:35:44 -08:00
|
|
|
checkTimer.initWithCallback(checkLoad, 100, Ci.nsITimer.TYPE_ONE_SHOT);
|
2012-09-28 16:16:22 -07:00
|
|
|
}
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
2012-11-22 07:53:44 -08:00
|
|
|
/**
|
|
|
|
* Add a cookie to the document
|
|
|
|
*/
|
|
|
|
function addCookie(msg) {
|
|
|
|
cookie = msg.json.cookie;
|
|
|
|
|
|
|
|
if (!cookie.expiry) {
|
|
|
|
var date = new Date();
|
|
|
|
var thePresent = new Date(Date.now());
|
|
|
|
date.setYear(thePresent.getFullYear() + 20);
|
|
|
|
cookie.expiry = date.getTime() / 1000; // Stored in seconds.
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cookie.domain) {
|
|
|
|
var location = curWindow.document.location;
|
|
|
|
cookie.domain = location.hostname;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var currLocation = curWindow.location;
|
|
|
|
var currDomain = currLocation.host;
|
|
|
|
if (currDomain.indexOf(cookie.domain) == -1) {
|
|
|
|
sendError("You may only set cookies for the current domain", 24, null, msg.json.command_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The cookie's domain may include a port. Which is bad. Remove it
|
|
|
|
// We'll catch ip6 addresses by mistake. Since no-one uses those
|
|
|
|
// this will be okay for now. See Bug 814416
|
|
|
|
if (cookie.domain.match(/:\d+$/)) {
|
|
|
|
cookie.domain = cookie.domain.replace(/:\d+$/, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
var document = curWindow.document;
|
|
|
|
if (!document || !document.contentType.match(/html/i)) {
|
|
|
|
sendError('You may only set cookies on html documents', 25, null, msg.json.command_id);
|
|
|
|
}
|
|
|
|
var cookieManager = Cc['@mozilla.org/cookiemanager;1'].
|
|
|
|
getService(Ci.nsICookieManager2);
|
|
|
|
cookieManager.add(cookie.domain, cookie.path, cookie.name, cookie.value,
|
|
|
|
cookie.secure, false, false, cookie.expiry);
|
|
|
|
sendOk(msg.json.command_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get All the cookies for a location
|
|
|
|
*/
|
|
|
|
function getAllCookies(msg) {
|
|
|
|
var toReturn = [];
|
|
|
|
var cookies = getVisibleCookies(curWindow.location);
|
|
|
|
for (var i = 0; i < cookies.length; i++) {
|
|
|
|
var cookie = cookies[i];
|
|
|
|
var expires = cookie.expires;
|
|
|
|
if (expires == 0) { // Session cookie, don't return an expiry.
|
|
|
|
expires = null;
|
|
|
|
} else if (expires == 1) { // Date before epoch time, cap to epoch.
|
|
|
|
expires = 0;
|
|
|
|
}
|
|
|
|
toReturn.push({
|
|
|
|
'name': cookie.name,
|
|
|
|
'value': cookie.value,
|
|
|
|
'path': cookie.path,
|
|
|
|
'domain': cookie.host,
|
|
|
|
'secure': cookie.isSecure,
|
|
|
|
'expiry': expires
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
sendResponse({value: toReturn}, msg.json.command_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a cookie by name
|
|
|
|
*/
|
|
|
|
function deleteCookie(msg) {
|
|
|
|
var toDelete = msg.json.name;
|
|
|
|
var cookieManager = Cc['@mozilla.org/cookiemanager;1'].
|
|
|
|
getService(Ci.nsICookieManager);
|
|
|
|
|
|
|
|
var cookies = getVisibleCookies(curWindow.location);
|
|
|
|
for (var i = 0; i < cookies.length; i++) {
|
|
|
|
var cookie = cookies[i];
|
|
|
|
if (cookie.name == toDelete) {
|
|
|
|
cookieManager.remove(cookie.host, cookie.name, cookie.path, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sendOk(msg.json.command_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all the visibile cookies on a page
|
|
|
|
*/
|
|
|
|
function deleteAllCookies(msg) {
|
|
|
|
let cookieManager = Cc['@mozilla.org/cookiemanager;1'].
|
|
|
|
getService(Ci.nsICookieManager);
|
|
|
|
let cookies = getVisibleCookies(curWindow.location);
|
|
|
|
for (let i = 0; i < cookies.length; i++) {
|
|
|
|
let cookie = cookies[i];
|
|
|
|
cookieManager.remove(cookie.host, cookie.name, cookie.path, false);
|
|
|
|
}
|
|
|
|
sendOk(msg.json.command_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all the visible cookies from a location
|
|
|
|
*/
|
|
|
|
function getVisibleCookies(location) {
|
|
|
|
let results = [];
|
|
|
|
let currentPath = location.pathname;
|
|
|
|
if (!currentPath) currentPath = '/';
|
|
|
|
let isForCurrentPath = function(aPath) {
|
|
|
|
return currentPath.indexOf(aPath) != -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
let cookieManager = Cc['@mozilla.org/cookiemanager;1'].
|
|
|
|
getService(Ci.nsICookieManager);
|
|
|
|
let enumerator = cookieManager.enumerator;
|
|
|
|
while (enumerator.hasMoreElements()) {
|
|
|
|
let cookie = enumerator.getNext().QueryInterface(Ci['nsICookie']);
|
|
|
|
|
|
|
|
// Take the hostname and progressively shorten
|
|
|
|
let hostname = location.hostname;
|
|
|
|
do {
|
|
|
|
if ((cookie.host == '.' + hostname || cookie.host == hostname)
|
|
|
|
&& isForCurrentPath(cookie.path)) {
|
|
|
|
results.push(cookie);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
hostname = hostname.replace(/^.*?\./, '');
|
|
|
|
} while (hostname.indexOf('.') != -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
2012-03-22 08:19:57 -07:00
|
|
|
|
2012-11-26 18:19:04 -08:00
|
|
|
function getAppCacheStatus(msg) {
|
|
|
|
sendResponse({ value: curWindow.applicationCache.status },
|
|
|
|
msg.json.command_id);
|
2012-08-23 15:07:16 -07:00
|
|
|
}
|
|
|
|
|
2012-05-23 17:23:36 -07:00
|
|
|
// emulator callbacks
|
2012-05-18 13:30:43 -07:00
|
|
|
let _emu_cb_id = 0;
|
|
|
|
let _emu_cbs = {};
|
2012-05-23 17:23:36 -07:00
|
|
|
|
2012-05-18 13:30:43 -07:00
|
|
|
function runEmulatorCmd(cmd, callback) {
|
2012-05-25 10:37:21 -07:00
|
|
|
if (callback) {
|
|
|
|
_emu_cbs[_emu_cb_id] = callback;
|
2012-05-18 13:30:43 -07:00
|
|
|
}
|
|
|
|
sendAsyncMessage("Marionette:runEmulatorCmd", {emulator_cmd: cmd, id: _emu_cb_id});
|
|
|
|
_emu_cb_id += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function emulatorCmdResult(msg) {
|
|
|
|
let message = msg.json;
|
2012-05-25 10:37:21 -07:00
|
|
|
if (!sandbox) {
|
2012-05-23 17:23:36 -07:00
|
|
|
return;
|
|
|
|
}
|
2012-05-18 13:30:43 -07:00
|
|
|
let cb = _emu_cbs[message.id];
|
|
|
|
delete _emu_cbs[message.id];
|
2012-05-25 10:37:21 -07:00
|
|
|
if (!cb) {
|
|
|
|
return;
|
|
|
|
}
|
2012-05-18 13:30:43 -07:00
|
|
|
try {
|
|
|
|
cb(message.result);
|
|
|
|
}
|
|
|
|
catch(e) {
|
2013-02-12 10:10:03 -08:00
|
|
|
sendError(e.message, e.code, e.stack, -1);
|
2012-05-18 13:30:43 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-04 10:50:06 -07:00
|
|
|
function importScript(msg) {
|
2012-11-26 18:19:04 -08:00
|
|
|
let command_id = msg.json.command_id;
|
2012-06-04 10:50:06 -07:00
|
|
|
let file;
|
|
|
|
if (importedScripts.exists()) {
|
2012-11-26 18:19:04 -08:00
|
|
|
file = FileUtils.openFileOutputStream(importedScripts,
|
|
|
|
FileUtils.MODE_APPEND | FileUtils.MODE_WRONLY);
|
2012-06-04 10:50:06 -07:00
|
|
|
}
|
|
|
|
else {
|
2012-10-24 08:36:43 -07:00
|
|
|
//Note: The permission bits here don't actually get set (bug 804563)
|
2012-11-26 18:19:04 -08:00
|
|
|
importedScripts.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE,
|
|
|
|
parseInt("0666", 8));
|
|
|
|
file = FileUtils.openFileOutputStream(importedScripts,
|
|
|
|
FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE);
|
2012-10-24 08:36:43 -07:00
|
|
|
importedScripts.permissions = parseInt("0666", 8); //actually set permissions
|
2012-06-04 10:50:06 -07:00
|
|
|
}
|
|
|
|
file.write(msg.json.script, msg.json.script.length);
|
|
|
|
file.close();
|
2012-11-26 18:19:04 -08:00
|
|
|
sendOk(command_id);
|
2012-06-04 10:50:06 -07:00
|
|
|
}
|
|
|
|
|
2012-11-30 15:25:26 -08:00
|
|
|
/**
|
|
|
|
* Saves a screenshot and returns a Base64 string
|
|
|
|
*/
|
|
|
|
function screenShot(msg) {
|
|
|
|
let node = null;
|
|
|
|
if (msg.json.element) {
|
|
|
|
try {
|
|
|
|
node = elementManager.getKnownElement(msg.json.element, curWindow)
|
|
|
|
}
|
|
|
|
catch (e) {
|
2013-02-12 10:10:03 -08:00
|
|
|
sendResponse(e.message, e.code, e.stack, msg.json.command_id);
|
2012-11-30 15:25:26 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
node = curWindow;
|
|
|
|
}
|
|
|
|
let highlights = msg.json.highlights;
|
|
|
|
|
|
|
|
var document = curWindow.document;
|
|
|
|
var rect, win, width, height, left, top, needsOffset;
|
|
|
|
// node can be either a window or an arbitrary DOM node
|
|
|
|
if (node == curWindow) {
|
|
|
|
// node is a window
|
|
|
|
win = node;
|
|
|
|
width = win.innerWidth;
|
|
|
|
height = win.innerHeight;
|
|
|
|
top = 0;
|
|
|
|
left = 0;
|
|
|
|
// offset needed for highlights to take 'outerHeight' of window into account
|
|
|
|
needsOffset = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// node is an arbitrary DOM node
|
|
|
|
win = node.ownerDocument.defaultView;
|
|
|
|
rect = node.getBoundingClientRect();
|
|
|
|
width = rect.width;
|
|
|
|
height = rect.height;
|
|
|
|
top = rect.top;
|
|
|
|
left = rect.left;
|
|
|
|
// offset for highlights not needed as they will be relative to this node
|
|
|
|
needsOffset = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var canvas = document.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
|
|
|
|
canvas.width = width;
|
|
|
|
canvas.height = height;
|
|
|
|
var ctx = canvas.getContext("2d");
|
|
|
|
// Draws the DOM contents of the window to the canvas
|
|
|
|
ctx.drawWindow(win, left, top, width, height, 'rgb(255,255,255)');
|
|
|
|
|
|
|
|
// This section is for drawing a red rectangle around each element passed in via the highlights array
|
|
|
|
if (highlights) {
|
|
|
|
ctx.lineWidth = "2";
|
|
|
|
ctx.strokeStyle = "red";
|
|
|
|
ctx.save();
|
|
|
|
|
|
|
|
for (var i = 0; i < highlights.length; ++i) {
|
|
|
|
var elem = highlights[i];
|
|
|
|
rect = elem.getBoundingClientRect();
|
|
|
|
|
|
|
|
var offsetY = 0, offsetX = 0;
|
|
|
|
if (needsOffset) {
|
|
|
|
var offset = getChromeOffset(elem);
|
|
|
|
offsetX = offset.x;
|
|
|
|
offsetY = offset.y;
|
|
|
|
} else {
|
|
|
|
// Don't need to offset the window chrome, just make relative to containing node
|
|
|
|
offsetY = -top;
|
|
|
|
offsetX = -left;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the rectangle
|
|
|
|
ctx.strokeRect(rect.left + offsetX, rect.top + offsetY, rect.width, rect.height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the Base64 String back to the client bindings and they can manage
|
|
|
|
// saving the file to disk if it is required
|
2013-02-12 10:10:03 -08:00
|
|
|
sendResponse({value:canvas.toDataURL("image/png","")}, msg.json.command_id);
|
2012-11-30 15:25:26 -08:00
|
|
|
}
|
|
|
|
|
2012-03-22 08:19:57 -07:00
|
|
|
//call register self when we get loaded
|
|
|
|
registerSelf();
|
2012-05-23 17:23:36 -07:00
|
|
|
|