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-09-11 23:12:12 -07:00
|
|
|
let Cr = Components.results;
|
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");
|
2013-01-30 14:22:54 -08:00
|
|
|
Cu.import("resource://gre/modules/BrowserElementParent.jsm");
|
2012-09-26 15:03:25 -07:00
|
|
|
|
2012-05-08 09:20:35 -07:00
|
|
|
const NS_PREFBRANCH_PREFCHANGE_TOPIC_ID = "nsPref:changed";
|
|
|
|
const BROWSER_FRAMES_ENABLED_PREF = "dom.mozBrowserFramesEnabled";
|
|
|
|
|
|
|
|
function debug(msg) {
|
2013-01-30 14:22:54 -08:00
|
|
|
//dump("BrowserElementParent.js - " + msg + "\n");
|
2012-08-14 20:54:33 -07:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*
|
2013-01-30 14:22:54 -08:00
|
|
|
* It creates a BrowserElementParent that injects script to listen for
|
|
|
|
* certain event.
|
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()) {
|
2013-01-30 14:22:54 -08:00
|
|
|
Services.prefs.addObserver(BROWSER_FRAMES_ENABLED_PREF, this, /* ownsWeak = */ true);
|
2012-05-08 09:20:35 -07:00
|
|
|
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
|
|
|
|
2013-01-30 14:22:54 -08:00
|
|
|
Services.obs.addObserver(this, 'remote-browser-frame-shown', /* ownsWeak = */ true);
|
|
|
|
Services.obs.addObserver(this, 'in-process-browser-or-app-frame-shown', /* ownsWeak = */ true);
|
2012-05-08 09:20:35 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
_browserFramesPrefEnabled: function() {
|
|
|
|
try {
|
2013-01-30 14:22:54 -08:00
|
|
|
return Services.prefs.getBoolPref(BROWSER_FRAMES_ENABLED_PREF);
|
2012-05-08 09:20:35 -07:00
|
|
|
}
|
|
|
|
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-07-30 19:09:31 -07:00
|
|
|
this._createBrowserElementParent(frameLoader, /* hasRemoteFrame = */ false);
|
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-07-30 19:09:31 -07:00
|
|
|
this._createBrowserElementParent(frameLoader, /* hasRemoteFrame = */ true);
|
2012-05-08 09:20:35 -07:00
|
|
|
},
|
|
|
|
|
2012-07-30 19:09:31 -07:00
|
|
|
_createBrowserElementParent: function(frameLoader, hasRemoteFrame) {
|
2012-05-08 09:20:35 -07:00
|
|
|
let frameElement = frameLoader.QueryInterface(Ci.nsIFrameLoader).ownerElement;
|
2013-01-30 14:22:54 -08:00
|
|
|
this._bepMap.set(frameElement, BrowserElementParentBuilder.create(frameLoader, hasRemoteFrame));
|
2012-06-15 08:01:44 -07:00
|
|
|
},
|
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;
|
2012-11-10 10:32:35 -08:00
|
|
|
case 'in-process-browser-or-app-frame-shown':
|
2012-06-15 08:01:44 -07:00
|
|
|
this._observeInProcessBrowserFrameShown(subject);
|
|
|
|
break;
|
|
|
|
case 'content-document-global-created':
|
|
|
|
this._observeContentGlobalCreated(subject);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
2012-05-08 09:20:35 -07:00
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([BrowserElementParentFactory]);
|