2012-05-08 09:20:35 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
let Cu = Components.utils;
|
|
|
|
let Ci = Components.interfaces;
|
|
|
|
let Cc = Components.classes;
|
2012-05-18 17:47:20 -07:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2012-05-08 09:20:35 -07:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
|
|
|
|
const NS_PREFBRANCH_PREFCHANGE_TOPIC_ID = "nsPref:changed";
|
|
|
|
const BROWSER_FRAMES_ENABLED_PREF = "dom.mozBrowserFramesEnabled";
|
|
|
|
|
|
|
|
function debug(msg) {
|
2012-05-08 16:17:10 -07:00
|
|
|
//dump("BrowserElementParent - " + msg + "\n");
|
2012-05-08 09:20:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-06-15 08:01:44 -07:00
|
|
|
* BrowserElementParent implements one half of <iframe mozbrowser>. (The other
|
|
|
|
* half is, unsurprisingly, BrowserElementChild.)
|
2012-05-08 09:20:35 -07:00
|
|
|
*
|
2012-06-15 08:01:44 -07:00
|
|
|
* BrowserElementParentFactory detects when we create a windows or docshell
|
|
|
|
* contained inside a <iframe mozbrowser> and creates a BrowserElementParent
|
|
|
|
* object for that window.
|
|
|
|
*
|
|
|
|
* BrowserElementParent injects script to listen for certain events in the
|
|
|
|
* child. We then listen to messages from the child script and take
|
|
|
|
* appropriate action here in the parent.
|
2012-05-08 09:20:35 -07:00
|
|
|
*/
|
|
|
|
|
2012-06-15 08:01:44 -07:00
|
|
|
function BrowserElementParentFactory() {
|
|
|
|
this._initialized = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
BrowserElementParentFactory.prototype = {
|
2012-05-08 09:20:35 -07:00
|
|
|
classID: Components.ID("{ddeafdac-cb39-47c4-9cb8-c9027ee36d26}"),
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
|
|
|
|
Ci.nsISupportsWeakReference]),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called on app startup, and also when the browser frames enabled pref is
|
|
|
|
* changed.
|
|
|
|
*/
|
|
|
|
_init: function() {
|
|
|
|
if (this._initialized) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the pref is disabled, do nothing except wait for the pref to change.
|
|
|
|
// (This is important for tests, if nothing else.)
|
|
|
|
if (!this._browserFramesPrefEnabled()) {
|
|
|
|
var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
|
|
|
|
prefs.addObserver(BROWSER_FRAMES_ENABLED_PREF, this, /* ownsWeak = */ true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-07 07:43:23 -07:00
|
|
|
debug("_init");
|
2012-05-08 09:20:35 -07:00
|
|
|
this._initialized = true;
|
|
|
|
|
2012-06-15 08:01:44 -07:00
|
|
|
// Maps frame elements to BrowserElementParent objects. We never look up
|
|
|
|
// anything in this map; the purpose is to keep the BrowserElementParent
|
|
|
|
// alive for as long as its frame element lives.
|
|
|
|
this._bepMap = new WeakMap();
|
2012-05-18 17:47:20 -07:00
|
|
|
|
2012-05-08 09:20:35 -07:00
|
|
|
var os = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
|
|
|
|
os.addObserver(this, 'remote-browser-frame-shown', /* ownsWeak = */ true);
|
|
|
|
os.addObserver(this, 'in-process-browser-frame-shown', /* ownsWeak = */ true);
|
|
|
|
},
|
|
|
|
|
|
|
|
_browserFramesPrefEnabled: function() {
|
|
|
|
var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
|
|
|
|
try {
|
|
|
|
return prefs.getBoolPref(BROWSER_FRAMES_ENABLED_PREF);
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-05-15 14:27:08 -07:00
|
|
|
_observeInProcessBrowserFrameShown: function(frameLoader) {
|
2012-05-08 09:20:35 -07:00
|
|
|
debug("In-process browser frame shown " + frameLoader);
|
2012-06-15 08:01:44 -07:00
|
|
|
this._createBrowserElementParent(frameLoader);
|
2012-05-08 09:20:35 -07:00
|
|
|
},
|
|
|
|
|
2012-05-15 14:27:08 -07:00
|
|
|
_observeRemoteBrowserFrameShown: function(frameLoader) {
|
2012-05-08 09:20:35 -07:00
|
|
|
debug("Remote browser frame shown " + frameLoader);
|
2012-06-15 08:01:44 -07:00
|
|
|
this._createBrowserElementParent(frameLoader);
|
2012-05-08 09:20:35 -07:00
|
|
|
},
|
|
|
|
|
2012-06-15 08:01:44 -07:00
|
|
|
_createBrowserElementParent: function(frameLoader) {
|
2012-05-08 09:20:35 -07:00
|
|
|
let frameElement = frameLoader.QueryInterface(Ci.nsIFrameLoader).ownerElement;
|
2012-06-15 08:01:44 -07:00
|
|
|
this._bepMap.set(frameElement, new BrowserElementParent(frameLoader));
|
|
|
|
},
|
2012-05-08 09:20:35 -07:00
|
|
|
|
2012-06-15 08:01:44 -07:00
|
|
|
observe: function(subject, topic, data) {
|
|
|
|
switch(topic) {
|
|
|
|
case 'app-startup':
|
|
|
|
this._init();
|
|
|
|
break;
|
|
|
|
case NS_PREFBRANCH_PREFCHANGE_TOPIC_ID:
|
|
|
|
if (data == BROWSER_FRAMES_ENABLED_PREF) {
|
|
|
|
this._init();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'remote-browser-frame-shown':
|
|
|
|
this._observeRemoteBrowserFrameShown(subject);
|
|
|
|
break;
|
|
|
|
case 'in-process-browser-frame-shown':
|
|
|
|
this._observeInProcessBrowserFrameShown(subject);
|
|
|
|
break;
|
|
|
|
case 'content-document-global-created':
|
|
|
|
this._observeContentGlobalCreated(subject);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
2012-05-08 09:20:35 -07:00
|
|
|
|
2012-06-15 08:01:44 -07:00
|
|
|
function BrowserElementParent(frameLoader) {
|
|
|
|
debug("Creating new BrowserElementParent object for " + frameLoader);
|
|
|
|
this._screenshotListeners = {};
|
|
|
|
this._screenshotReqCounter = 0;
|
|
|
|
|
|
|
|
this._frameElement = frameLoader.QueryInterface(Ci.nsIFrameLoader).ownerElement;
|
|
|
|
if (!this._frameElement) {
|
|
|
|
debug("No frame element?");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._mm = frameLoader.messageManager;
|
|
|
|
|
|
|
|
// Messages we receive are handed to functions which take a (data) argument,
|
|
|
|
// where |data| is the message manager's data object.
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
function addMessageListener(msg, handler) {
|
|
|
|
self._mm.addMessageListener('browser-element-api:' + msg, handler.bind(self));
|
|
|
|
}
|
|
|
|
|
|
|
|
addMessageListener("hello", this._recvHello);
|
|
|
|
addMessageListener("locationchange", this._fireEventFromMsg);
|
|
|
|
addMessageListener("loadstart", this._fireEventFromMsg);
|
|
|
|
addMessageListener("loadend", this._fireEventFromMsg);
|
|
|
|
addMessageListener("titlechange", this._fireEventFromMsg);
|
|
|
|
addMessageListener("iconchange", this._fireEventFromMsg);
|
|
|
|
addMessageListener("close", this._fireEventFromMsg);
|
2012-06-19 22:08:56 -07:00
|
|
|
addMessageListener("securitychange", this._fireEventFromMsg);
|
2012-06-15 08:01:44 -07:00
|
|
|
addMessageListener("get-mozapp-manifest-url", this._sendMozAppManifestURL);
|
|
|
|
addMessageListener("keyevent", this._fireKeyEvent);
|
|
|
|
addMessageListener("showmodalprompt", this._handleShowModalPrompt);
|
|
|
|
addMessageListener('got-screenshot', this._recvGotScreenshot);
|
|
|
|
|
|
|
|
function defineMethod(name, fn) {
|
|
|
|
XPCNativeWrapper.unwrap(self._frameElement)[name] = fn.bind(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define methods on the frame element.
|
|
|
|
defineMethod('getScreenshot', this._getScreenshot);
|
|
|
|
defineMethod('setVisible', this._setVisible);
|
|
|
|
|
2012-06-19 22:08:56 -07:00
|
|
|
this._mm.loadFrameScript("chrome://global/content/BrowserElementChild.js",
|
2012-06-15 08:01:44 -07:00
|
|
|
/* allowDelayedLoad = */ true);
|
|
|
|
}
|
2012-05-08 09:20:35 -07:00
|
|
|
|
2012-06-15 08:01:44 -07:00
|
|
|
BrowserElementParent.prototype = {
|
|
|
|
get _window() {
|
|
|
|
return this._frameElement.ownerDocument.defaultView;
|
|
|
|
},
|
2012-05-08 09:20:35 -07:00
|
|
|
|
2012-06-15 08:01:44 -07:00
|
|
|
_sendAsyncMsg: function(msg, data) {
|
|
|
|
this._frameElement.QueryInterface(Ci.nsIFrameLoaderOwner)
|
|
|
|
.frameLoader
|
|
|
|
.messageManager
|
|
|
|
.sendAsyncMessage('browser-element-api:' + msg, data);
|
2012-05-08 09:20:35 -07:00
|
|
|
},
|
|
|
|
|
2012-06-15 08:01:44 -07:00
|
|
|
_recvHello: function(data) {
|
|
|
|
debug("recvHello");
|
2012-05-08 09:20:35 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fire either a vanilla or a custom event, depending on the contents of
|
|
|
|
* |data|.
|
|
|
|
*/
|
2012-06-15 08:01:44 -07:00
|
|
|
_fireEventFromMsg: function(data) {
|
2012-06-07 07:43:23 -07:00
|
|
|
let name = data.name.substring('browser-element-api:'.length);
|
|
|
|
let detail = data.json;
|
|
|
|
|
|
|
|
debug('fireEventFromMsg: ' + name + ', ' + detail);
|
2012-06-15 08:01:44 -07:00
|
|
|
let evt = this._createEvent(name, detail,
|
2012-06-07 07:43:23 -07:00
|
|
|
/* cancelable = */ false);
|
2012-06-15 08:01:44 -07:00
|
|
|
this._frameElement.dispatchEvent(evt);
|
2012-06-07 07:43:23 -07:00
|
|
|
},
|
|
|
|
|
2012-06-15 08:01:44 -07:00
|
|
|
_handleShowModalPrompt: function(data) {
|
2012-06-07 07:43:23 -07:00
|
|
|
// Fire a showmodalprmopt event on the iframe. When this method is called,
|
|
|
|
// the child is spinning in a nested event loop waiting for an
|
|
|
|
// unblock-modal-prompt message.
|
|
|
|
//
|
|
|
|
// If the embedder calls preventDefault() on the showmodalprompt event,
|
|
|
|
// we'll block the child until event.detail.unblock() is called.
|
|
|
|
//
|
|
|
|
// Otherwise, if preventDefault() is not called, we'll send the
|
|
|
|
// unblock-modal-prompt message to the child as soon as the event is done
|
|
|
|
// dispatching.
|
|
|
|
|
2012-05-08 09:20:35 -07:00
|
|
|
let detail = data.json;
|
2012-06-07 07:43:23 -07:00
|
|
|
debug('handleShowPrompt ' + JSON.stringify(detail));
|
|
|
|
|
|
|
|
// Strip off the windowID property from the object we send along in the
|
|
|
|
// event.
|
|
|
|
let windowID = detail.windowID;
|
|
|
|
delete detail.windowID;
|
|
|
|
debug("Event will have detail: " + JSON.stringify(detail));
|
2012-06-15 08:01:44 -07:00
|
|
|
let evt = this._createEvent('showmodalprompt', detail,
|
2012-06-07 07:43:23 -07:00
|
|
|
/* cancelable = */ true);
|
|
|
|
|
2012-06-15 08:01:44 -07:00
|
|
|
let self = this;
|
2012-06-07 07:43:23 -07:00
|
|
|
let unblockMsgSent = false;
|
|
|
|
function sendUnblockMsg() {
|
|
|
|
if (unblockMsgSent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
unblockMsgSent = true;
|
|
|
|
|
|
|
|
// We don't need to sanitize evt.detail.returnValue (e.g. converting the
|
|
|
|
// return value of confirm() to a boolean); Gecko does that for us.
|
|
|
|
|
|
|
|
let data = { windowID: windowID,
|
|
|
|
returnValue: evt.detail.returnValue };
|
2012-06-15 08:01:44 -07:00
|
|
|
self._sendAsyncMsg('unblock-modal-prompt', data);
|
2012-06-07 07:43:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
XPCNativeWrapper.unwrap(evt.detail).unblock = function() {
|
|
|
|
sendUnblockMsg();
|
|
|
|
};
|
|
|
|
|
2012-06-15 08:01:44 -07:00
|
|
|
this._frameElement.dispatchEvent(evt);
|
2012-05-08 09:20:35 -07:00
|
|
|
|
2012-06-07 07:43:23 -07:00
|
|
|
if (!evt.defaultPrevented) {
|
|
|
|
// Unblock the inner frame immediately. Otherwise we'll unblock upon
|
|
|
|
// evt.detail.unblock().
|
|
|
|
sendUnblockMsg();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-06-15 08:01:44 -07:00
|
|
|
_createEvent: function(evtName, detail, cancelable) {
|
2012-05-08 09:20:35 -07:00
|
|
|
// This will have to change if we ever want to send a CustomEvent with null
|
|
|
|
// detail. For now, it's OK.
|
|
|
|
if (detail !== undefined && detail !== null) {
|
2012-06-15 08:01:44 -07:00
|
|
|
return new this._window.CustomEvent('mozbrowser' + evtName,
|
|
|
|
{ bubbles: true,
|
|
|
|
cancelable: cancelable,
|
|
|
|
detail: detail });
|
2012-05-08 09:20:35 -07:00
|
|
|
}
|
|
|
|
|
2012-06-15 08:01:44 -07:00
|
|
|
return new this._window.Event('mozbrowser' + evtName,
|
|
|
|
{ bubbles: true,
|
|
|
|
cancelable: cancelable });
|
2012-05-08 09:20:35 -07:00
|
|
|
},
|
|
|
|
|
2012-06-15 08:01:44 -07:00
|
|
|
_sendMozAppManifestURL: function(data) {
|
|
|
|
return this._frameElement.getAttribute('mozapp');
|
2012-05-11 13:03:51 -07:00
|
|
|
},
|
|
|
|
|
2012-05-18 17:47:20 -07:00
|
|
|
|
2012-06-15 08:01:44 -07:00
|
|
|
_getScreenshot: function() {
|
2012-05-18 17:47:20 -07:00
|
|
|
let id = 'req_' + this._screenshotReqCounter++;
|
2012-06-15 08:01:44 -07:00
|
|
|
let req = Services.DOMRequest.createRequest(this._window);
|
2012-05-18 17:47:20 -07:00
|
|
|
this._screenshotListeners[id] = req;
|
2012-06-15 08:01:44 -07:00
|
|
|
this._sendAsyncMsg('get-screenshot', {id: id});
|
2012-05-18 17:47:20 -07:00
|
|
|
return req;
|
|
|
|
},
|
|
|
|
|
2012-06-15 08:01:44 -07:00
|
|
|
_recvGotScreenshot: function(data) {
|
|
|
|
var req = this._screenshotListeners[data.json.id];
|
|
|
|
delete this._screenshotListeners[data.json.id];
|
|
|
|
Services.DOMRequest.fireSuccess(req, data.json.screenshot);
|
|
|
|
},
|
2012-05-27 05:39:04 -07:00
|
|
|
|
2012-06-15 08:01:44 -07:00
|
|
|
_setVisible: function(visible) {
|
|
|
|
this._sendAsyncMsg('set-visible', {visible: visible});
|
|
|
|
},
|
2012-05-23 07:15:31 -07:00
|
|
|
|
2012-06-15 08:01:44 -07:00
|
|
|
_fireKeyEvent: function(data) {
|
|
|
|
let evt = this._window.document.createEvent("KeyboardEvent");
|
|
|
|
evt.initKeyEvent(data.json.type, true, true, this._window,
|
2012-05-23 07:15:31 -07:00
|
|
|
false, false, false, false, // modifiers
|
|
|
|
data.json.keyCode,
|
|
|
|
data.json.charCode);
|
|
|
|
|
2012-06-15 08:01:44 -07:00
|
|
|
this._frameElement.dispatchEvent(evt);
|
2012-05-08 09:20:35 -07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2012-06-15 08:01:44 -07:00
|
|
|
var NSGetFactory = XPCOMUtils.generateNSGetFactory([BrowserElementParentFactory]);
|