Bug 1190685 - [webext] Implements webNavigation.getFrame/getAllFrames API methods. r=kmag

This commit is contained in:
Luca Greco 2016-02-08 18:30:48 +01:00
parent ce45b02f5e
commit fca2a13a13
6 changed files with 218 additions and 47 deletions

View File

@ -34,6 +34,9 @@ XPCOMUtils.defineLazyModuleGetter(this, "PromiseUtils",
XPCOMUtils.defineLazyModuleGetter(this, "MessageChannel",
"resource://gre/modules/MessageChannel.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "WebNavigationFrames",
"resource://gre/modules/WebNavigationFrames.jsm");
Cu.import("resource://gre/modules/ExtensionUtils.jsm");
var {
runSafeSyncWithoutClone,
@ -668,6 +671,8 @@ class ExtensionGlobal {
MessageChannel.addListener(global, "Extension:Capture", this);
MessageChannel.addListener(global, "Extension:Execute", this);
MessageChannel.addListener(global, "WebNavigation:GetFrame", this);
MessageChannel.addListener(global, "WebNavigation:GetAllFrames", this);
this.broker = new MessageBroker([global]);
@ -695,12 +700,23 @@ class ExtensionGlobal {
receiveMessage({ target, messageName, recipient, data }) {
switch (messageName) {
case "Extension:Capture":
return this.handleExtensionCapture(data.width, data.height, data.options);
case "Extension:Execute":
return this.handleExtensionExecute(target, recipient, data.options);
case "WebNavigation:GetFrame":
return this.handleWebNavigationGetFrame(data.options);
case "WebNavigation:GetAllFrames":
return this.handleWebNavigationGetAllFrames();
}
}
handleExtensionCapture(width, height, options) {
let win = this.global.content;
const XHTML_NS = "http://www.w3.org/1999/xhtml";
let canvas = win.document.createElementNS(XHTML_NS, "canvas");
canvas.width = data.width;
canvas.height = data.height;
canvas.width = width;
canvas.height = height;
canvas.mozOpaque = true;
let ctx = canvas.getContext("2d");
@ -708,23 +724,27 @@ class ExtensionGlobal {
// We need to scale the image to the visible size of the browser,
// in order for the result to appear as the user sees it when
// settings like full zoom come into play.
ctx.scale(canvas.width / win.innerWidth,
canvas.height / win.innerHeight);
ctx.scale(canvas.width / win.innerWidth, canvas.height / win.innerHeight);
ctx.drawWindow(win, win.scrollX, win.scrollY, win.innerWidth, win.innerHeight, "#fff");
return canvas.toDataURL(`image/${data.options.format}`,
data.options.quality / 100);
return canvas.toDataURL(`image/${options.format}`, options.quality / 100);
}
case "Extension:Execute":
handleExtensionExecute(target, recipient, options) {
let deferred = PromiseUtils.defer();
let script = new Script(data.options, deferred);
let script = new Script(options, deferred);
let { extensionId } = recipient;
DocumentManager.executeScript(target, extensionId, script);
return deferred.promise;
}
handleWebNavigationGetFrame({ frameId }) {
return WebNavigationFrames.getFrame(this.global.docShell, frameId);
}
handleWebNavigationGetAllFrames() {
return WebNavigationFrames.getAllFrames(this.global.docShell);
}
}

View File

@ -60,6 +60,16 @@ function WebNavigationEventManager(context, eventName) {
WebNavigationEventManager.prototype = Object.create(SingletonEventManager.prototype);
function convertGetFrameResult(tabId, data) {
return {
errorOccurred: data.errorOccurred,
url: data.url,
tabId,
frameId: ExtensionManagement.getFrameId(data.windowId),
parentFrameId: ExtensionManagement.getParentFrameId(data.parentWindowId, data.windowId),
};
}
extensions.registerSchemaAPI("webNavigation", "webNavigation", (extension, context) => {
return {
webNavigation: {
@ -70,6 +80,36 @@ extensions.registerSchemaAPI("webNavigation", "webNavigation", (extension, conte
onErrorOccurred: new WebNavigationEventManager(context, "onErrorOccurred").api(),
onReferenceFragmentUpdated: new WebNavigationEventManager(context, "onReferenceFragmentUpdated").api(),
onCreatedNavigationTarget: ignoreEvent(context, "webNavigation.onCreatedNavigationTarget"),
getAllFrames(details) {
let tab = TabManager.getTab(details.tabId);
if (!tab) {
return Promise.reject({ message: `No tab found with tabId: ${details.tabId}`});
}
let { innerWindowID, messageManager } = tab.linkedBrowser;
let recipient = { innerWindowID };
return context.sendMessage(messageManager, "WebNavigation:GetAllFrames", {}, recipient)
.then((results) => results.map(convertGetFrameResult.bind(null, details.tabId)));
},
getFrame(details) {
let tab = TabManager.getTab(details.tabId);
if (!tab) {
return Promise.reject({ message: `No tab found with tabId: ${details.tabId}`});
}
let recipient = {
innerWindowID: tab.linkedBrowser.innerWindowID,
};
let mm = tab.linkedBrowser.messageManager;
return context.sendMessage(mm, "WebNavigation:GetFrame", { options: details }, recipient)
.then((result) => {
return result ?
convertGetFrameResult(details.tabId, result) :
Promise.reject({ message: `No frame found with frameId: ${details.frameId}`});
});
},
},
};
});

View File

@ -36,7 +36,6 @@
"functions": [
{
"name": "getFrame",
"unsupported": true,
"type": "function",
"description": "Retrieves information about the given frame. A frame refers to an <iframe> or a <frame> of a web page and is identified by a tab ID and a frame ID.",
"async": "callback",
@ -47,7 +46,7 @@
"description": "Information about the frame to retrieve information about.",
"properties": {
"tabId": { "type": "integer", "minimum": 0, "description": "The ID of the tab in which the frame is." },
"processId": {"unsupported": true, "type": "integer", "description": "The ID of the process runs the renderer for this tab."},
"processId": {"optional": true, "type": "integer", "description": "The ID of the process runs the renderer for this tab."},
"frameId": { "type": "integer", "minimum": 0, "description": "The ID of the frame in the given tab." }
}
},
@ -62,6 +61,7 @@
"description": "Information about the requested frame, null if the specified frame ID and/or tab ID are invalid.",
"properties": {
"errorOccurred": {
"unsupported": true,
"type": "boolean",
"description": "True if the last navigation in this frame was interrupted by an error, i.e. the onErrorOccurred event fired."
},
@ -81,7 +81,6 @@
},
{
"name": "getAllFrames",
"unsupported": true,
"type": "function",
"description": "Retrieves information about all frames of a given tab.",
"async": "callback",
@ -107,6 +106,7 @@
"type": "object",
"properties": {
"errorOccurred": {
"unsupported": true,
"type": "boolean",
"description": "True if the last navigation in this frame was interrupted by an error, i.e. the onErrorOccurred event fired."
},

View File

@ -6,22 +6,15 @@ var Ci = Components.interfaces;
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
function getWindowId(window) {
return window.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils)
.outerWindowID;
}
function getParentWindowId(window) {
return getWindowId(window.parent);
}
XPCOMUtils.defineLazyModuleGetter(this, "WebNavigationFrames",
"resource://gre/modules/WebNavigationFrames.jsm");
function loadListener(event) {
let document = event.target;
let window = document.defaultView;
let url = document.documentURI;
let windowId = getWindowId(window);
let parentWindowId = getParentWindowId(window);
let windowId = WebNavigationFrames.getWindowId(window);
let parentWindowId = WebNavigationFrames.getParentWindowId(window);
sendAsyncMessage("Extension:DOMContentLoaded", {windowId, parentWindowId, url});
}
@ -51,7 +44,7 @@ var WebProgressListener = {
let data = {
requestURL: request.QueryInterface(Ci.nsIChannel).URI.spec,
windowId: webProgress.DOMWindowID,
parentWindowId: getParentWindowId(webProgress.DOMWindow),
parentWindowId: WebNavigationFrames.getParentWindowId(webProgress.DOMWindow),
status,
stateFlags,
};
@ -66,7 +59,7 @@ var WebProgressListener = {
let data = {
location: request.QueryInterface(Ci.nsIChannel).URI.spec,
windowId: webProgress.DOMWindowID,
parentWindowId: getParentWindowId(webProgress.DOMWindow),
parentWindowId: WebNavigationFrames.getParentWindowId(webProgress.DOMWindow),
flags: 0,
};
sendAsyncMessage("Extension:LocationChange", data);
@ -78,7 +71,7 @@ var WebProgressListener = {
let data = {
location: locationURI ? locationURI.spec : "",
windowId: webProgress.DOMWindowID,
parentWindowId: getParentWindowId(webProgress.DOMWindow),
parentWindowId: WebNavigationFrames.getParentWindowId(webProgress.DOMWindow),
flags,
};
sendAsyncMessage("Extension:LocationChange", data);
@ -91,10 +84,13 @@ var disabled = false;
WebProgressListener.init();
addEventListener("unload", () => {
if (!disabled) {
disabled = true;
WebProgressListener.uninit();
}
});
addMessageListener("Extension:DisableWebNavigation", () => {
if (!disabled) {
disabled = true;
WebProgressListener.uninit();
}
});

View File

@ -0,0 +1,114 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const EXPORTED_SYMBOLS = ["WebNavigationFrames"];
var Ci = Components.interfaces;
/* exported WebNavigationFrames */
function getWindowId(window) {
return window.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils)
.outerWindowID;
}
function getParentWindowId(window) {
return getWindowId(window.parent);
}
/**
* Retrieve the DOMWindow associated to the docShell passed as parameter.
*
* @param {nsIDocShell} docShell - the docShell that we want to get the DOMWindow from.
* @return {nsIDOMWindow} - the DOMWindow associated to the docShell.
*/
function docShellToWindow(docShell) {
return docShell.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindow);
}
/**
* The FrameDetail object which represents a frame in WebExtensions APIs.
*
* @typedef {Object} FrameDetail
* @inner
* @property {number} windowId - Represents the numeric id which identify the frame in its tab.
* @property {number} parentWindowId - Represents the numeric id which identify the parent frame.
* @property {string} url - Represents the current location URL loaded in the frame.
* @property {boolean} errorOccurred - Indicates whether an error is occurred during the last load
* happened on this frame (NOT YET SUPPORTED).
*/
/**
* Convert a docShell object into its internal FrameDetail representation.
*
* @param {nsIDocShell} docShell - the docShell object to be converted into a FrameDetail JSON object.
* @return {FrameDetail} the FrameDetail JSON object which represents the docShell.
*/
function convertDocShellToFrameDetail(docShell) {
let window = docShellToWindow(docShell);
return {
windowId: getWindowId(window),
parentWindowId: getParentWindowId(window),
url: window.location.href,
};
}
/**
* A generator function which iterates over a docShell tree, given a root docShell.
*
* @param {nsIDocShell} docShell - the root docShell object
* @return {Iterator<DocShell>} the FrameDetail JSON object which represents the docShell.
*/
function* iterateDocShellTree(docShell) {
let docShellsEnum = docShell.getDocShellEnumerator(
Ci.nsIDocShellTreeItem.typeContent,
Ci.nsIDocShell.ENUMERATE_FORWARDS
);
while (docShellsEnum.hasMoreElements()) {
yield docShellsEnum.getNext();
}
return null;
}
/**
* Search for a frame starting from the passed root docShell and
* convert it to its related frame detail representation.
*
* @param {number} windowId - the windowId of the frame to retrieve
* @param {nsIDocShell} docShell - the root docShell object
* @return {FrameDetail} the FrameDetail JSON object which represents the docShell.
*/
function findFrame(windowId, rootDocShell) {
for (let docShell of iterateDocShellTree(rootDocShell)) {
if (windowId == getWindowId(docShellToWindow(docShell))) {
return convertDocShellToFrameDetail(docShell);
}
}
return null;
}
var WebNavigationFrames = {
getFrame(docShell, frameId) {
if (frameId == 0) {
return convertDocShellToFrameDetail(docShell);
}
return findFrame(frameId, docShell);
},
getAllFrames(docShell) {
return Array.from(iterateDocShellTree(docShell), convertDocShellToFrameDetail);
},
getWindowId,
getParentWindowId,
};

View File

@ -19,6 +19,7 @@ EXTRA_JS_MODULES += [
'addons/MatchPattern.jsm',
'addons/WebNavigation.jsm',
'addons/WebNavigationContent.js',
'addons/WebNavigationFrames.jsm',
'addons/WebRequest.jsm',
'addons/WebRequestCommon.jsm',
'addons/WebRequestContent.js',