Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | const {utils: Cu, interfaces: Ci} = Components; |
michael@0 | 8 | |
michael@0 | 9 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 10 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 11 | |
michael@0 | 12 | const NS_PREFBRANCH_PREFCHANGE_TOPIC_ID = "nsPref:changed"; |
michael@0 | 13 | const BROWSER_FRAMES_ENABLED_PREF = "dom.mozBrowserFramesEnabled"; |
michael@0 | 14 | |
michael@0 | 15 | XPCOMUtils.defineLazyModuleGetter(this, "BrowserElementParentBuilder", |
michael@0 | 16 | "resource://gre/modules/BrowserElementParent.jsm", |
michael@0 | 17 | "BrowserElementParentBuilder"); |
michael@0 | 18 | |
michael@0 | 19 | function debug(msg) { |
michael@0 | 20 | //dump("BrowserElementParent.js - " + msg + "\n"); |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | /** |
michael@0 | 24 | * BrowserElementParent implements one half of <iframe mozbrowser>. (The other |
michael@0 | 25 | * half is, unsurprisingly, BrowserElementChild.) |
michael@0 | 26 | * |
michael@0 | 27 | * BrowserElementParentFactory detects when we create a windows or docshell |
michael@0 | 28 | * contained inside a <iframe mozbrowser> and creates a BrowserElementParent |
michael@0 | 29 | * object for that window. |
michael@0 | 30 | * |
michael@0 | 31 | * It creates a BrowserElementParent that injects script to listen for |
michael@0 | 32 | * certain event. |
michael@0 | 33 | */ |
michael@0 | 34 | |
michael@0 | 35 | function BrowserElementParentFactory() { |
michael@0 | 36 | this._initialized = false; |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | BrowserElementParentFactory.prototype = { |
michael@0 | 40 | classID: Components.ID("{ddeafdac-cb39-47c4-9cb8-c9027ee36d26}"), |
michael@0 | 41 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, |
michael@0 | 42 | Ci.nsISupportsWeakReference]), |
michael@0 | 43 | |
michael@0 | 44 | /** |
michael@0 | 45 | * Called on app startup, and also when the browser frames enabled pref is |
michael@0 | 46 | * changed. |
michael@0 | 47 | */ |
michael@0 | 48 | _init: function() { |
michael@0 | 49 | if (this._initialized) { |
michael@0 | 50 | return; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | // If the pref is disabled, do nothing except wait for the pref to change. |
michael@0 | 54 | // (This is important for tests, if nothing else.) |
michael@0 | 55 | if (!this._browserFramesPrefEnabled()) { |
michael@0 | 56 | Services.prefs.addObserver(BROWSER_FRAMES_ENABLED_PREF, this, /* ownsWeak = */ true); |
michael@0 | 57 | return; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | debug("_init"); |
michael@0 | 61 | this._initialized = true; |
michael@0 | 62 | |
michael@0 | 63 | // Maps frame elements to BrowserElementParent objects. We never look up |
michael@0 | 64 | // anything in this map; the purpose is to keep the BrowserElementParent |
michael@0 | 65 | // alive for as long as its frame element lives. |
michael@0 | 66 | this._bepMap = new WeakMap(); |
michael@0 | 67 | |
michael@0 | 68 | Services.obs.addObserver(this, 'remote-browser-pending', /* ownsWeak = */ true); |
michael@0 | 69 | Services.obs.addObserver(this, 'inprocess-browser-shown', /* ownsWeak = */ true); |
michael@0 | 70 | }, |
michael@0 | 71 | |
michael@0 | 72 | _browserFramesPrefEnabled: function() { |
michael@0 | 73 | try { |
michael@0 | 74 | return Services.prefs.getBoolPref(BROWSER_FRAMES_ENABLED_PREF); |
michael@0 | 75 | } |
michael@0 | 76 | catch(e) { |
michael@0 | 77 | return false; |
michael@0 | 78 | } |
michael@0 | 79 | }, |
michael@0 | 80 | |
michael@0 | 81 | _observeInProcessBrowserFrameShown: function(frameLoader) { |
michael@0 | 82 | // Ignore notifications that aren't from a BrowserOrApp |
michael@0 | 83 | if (!frameLoader.QueryInterface(Ci.nsIFrameLoader).ownerIsBrowserOrAppFrame) { |
michael@0 | 84 | return; |
michael@0 | 85 | } |
michael@0 | 86 | debug("In-process browser frame shown " + frameLoader); |
michael@0 | 87 | this._createBrowserElementParent(frameLoader, |
michael@0 | 88 | /* hasRemoteFrame = */ false, |
michael@0 | 89 | /* pending frame */ false); |
michael@0 | 90 | }, |
michael@0 | 91 | |
michael@0 | 92 | _observeRemoteBrowserFramePending: function(frameLoader) { |
michael@0 | 93 | // Ignore notifications that aren't from a BrowserOrApp |
michael@0 | 94 | if (!frameLoader.QueryInterface(Ci.nsIFrameLoader).ownerIsBrowserOrAppFrame) { |
michael@0 | 95 | return; |
michael@0 | 96 | } |
michael@0 | 97 | debug("Remote browser frame shown " + frameLoader); |
michael@0 | 98 | this._createBrowserElementParent(frameLoader, |
michael@0 | 99 | /* hasRemoteFrame = */ true, |
michael@0 | 100 | /* pending frame */ true); |
michael@0 | 101 | }, |
michael@0 | 102 | |
michael@0 | 103 | _createBrowserElementParent: function(frameLoader, hasRemoteFrame, isPendingFrame) { |
michael@0 | 104 | let frameElement = frameLoader.QueryInterface(Ci.nsIFrameLoader).ownerElement; |
michael@0 | 105 | this._bepMap.set(frameElement, BrowserElementParentBuilder.create( |
michael@0 | 106 | frameLoader, hasRemoteFrame, isPendingFrame)); |
michael@0 | 107 | }, |
michael@0 | 108 | |
michael@0 | 109 | observe: function(subject, topic, data) { |
michael@0 | 110 | switch(topic) { |
michael@0 | 111 | case 'app-startup': |
michael@0 | 112 | this._init(); |
michael@0 | 113 | break; |
michael@0 | 114 | case NS_PREFBRANCH_PREFCHANGE_TOPIC_ID: |
michael@0 | 115 | if (data == BROWSER_FRAMES_ENABLED_PREF) { |
michael@0 | 116 | this._init(); |
michael@0 | 117 | } |
michael@0 | 118 | break; |
michael@0 | 119 | case 'remote-browser-pending': |
michael@0 | 120 | this._observeRemoteBrowserFramePending(subject); |
michael@0 | 121 | break; |
michael@0 | 122 | case 'inprocess-browser-shown': |
michael@0 | 123 | this._observeInProcessBrowserFrameShown(subject); |
michael@0 | 124 | break; |
michael@0 | 125 | } |
michael@0 | 126 | }, |
michael@0 | 127 | }; |
michael@0 | 128 | |
michael@0 | 129 | this.NSGetFactory = XPCOMUtils.generateNSGetFactory([BrowserElementParentFactory]); |