browser/modules/SharedFrame.jsm

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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
michael@0 3 * file, 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 this.EXPORTED_SYMBOLS = [ "SharedFrame" ];
michael@0 8
michael@0 9 const Ci = Components.interfaces;
michael@0 10 const Cu = Components.utils;
michael@0 11
michael@0 12 /**
michael@0 13 * The purpose of this module is to create and group various iframe
michael@0 14 * elements that are meant to all display the same content and only
michael@0 15 * one at a time. This makes it possible to have the content loaded
michael@0 16 * only once, while the other iframes can be kept as placeholders to
michael@0 17 * quickly move the content to them through the swapFrameLoaders function
michael@0 18 * when another one of the placeholder is meant to be displayed.
michael@0 19 * */
michael@0 20
michael@0 21 let Frames = new Map();
michael@0 22
michael@0 23 /**
michael@0 24 * The Frames map is the main data structure that holds information
michael@0 25 * about the groups being tracked. Each entry's key is the group name,
michael@0 26 * and the object holds information about what is the URL being displayed
michael@0 27 * on that group, and what is the active element on the group (the frame that
michael@0 28 * holds the loaded content).
michael@0 29 * The reference to the activeFrame is a weak reference, which allows the
michael@0 30 * frame to go away at any time, and when that happens the module considers that
michael@0 31 * there are no active elements in that group. The group can be reactivated
michael@0 32 * by changing the URL, calling preload again or adding a new element.
michael@0 33 *
michael@0 34 *
michael@0 35 * Frames = {
michael@0 36 * "messages-panel": {
michael@0 37 * url: string,
michael@0 38 * activeFrame: weakref
michael@0 39 * }
michael@0 40 * }
michael@0 41 *
michael@0 42 * Each object on the map is called a _SharedFrameGroup, which is an internal
michael@0 43 * class of this module which does not automatically keep track of its state. This
michael@0 44 * object should not be used externally, and all control should be handled by the
michael@0 45 * module's functions.
michael@0 46 */
michael@0 47
michael@0 48 function UNLOADED_URL(aStr) "data:text/html;charset=utf-8,<!-- Unloaded frame " + aStr + " -->";
michael@0 49
michael@0 50
michael@0 51 this.SharedFrame = {
michael@0 52 /**
michael@0 53 * Creates an iframe element and track it as part of the specified group
michael@0 54 * The module must create the iframe itself because it needs to do some special
michael@0 55 * handling for the element's src attribute.
michael@0 56 *
michael@0 57 * @param aGroupName the name of the group to which this frame belongs
michael@0 58 * @param aParent the parent element to which the frame will be appended to
michael@0 59 * @param aFrameAttributes an object with a list of attributes to set in the iframe
michael@0 60 * before appending it to the DOM. The "src" attribute has
michael@0 61 * special meaning here and if it's not blank it specifies
michael@0 62 * the URL that will be initially assigned to this group
michael@0 63 * @param aPreload optional, tells if the URL specified in the src attribute
michael@0 64 * should be preloaded in the frame being created, in case
michael@0 65 * it's not yet preloaded in any other frame of the group.
michael@0 66 * This parameter has no meaning if src is blank.
michael@0 67 */
michael@0 68 createFrame: function (aGroupName, aParent, aFrameAttributes, aPreload = true) {
michael@0 69 let frame = aParent.ownerDocument.createElement("iframe");
michael@0 70
michael@0 71 for (let [key, val] of Iterator(aFrameAttributes)) {
michael@0 72 frame.setAttribute(key, val);
michael@0 73 }
michael@0 74
michael@0 75 let src = aFrameAttributes.src;
michael@0 76 if (!src) {
michael@0 77 aPreload = false;
michael@0 78 }
michael@0 79
michael@0 80 let group = Frames.get(aGroupName);
michael@0 81
michael@0 82 if (group) {
michael@0 83 // If this group has already been created
michael@0 84
michael@0 85 if (aPreload && !group.isAlive) {
michael@0 86 // If aPreload is set and the group is not already loaded, load it.
michael@0 87 // This can happen if:
michael@0 88 // - aPreload was not used while creating the previous frames of this group, or
michael@0 89 // - the previously active frame went dead in the meantime
michael@0 90 group.url = src;
michael@0 91 this.preload(aGroupName, frame);
michael@0 92 } else {
michael@0 93 // If aPreload is not set, or the group is already loaded in a different frame,
michael@0 94 // there's not much that we need to do here: just create this frame as an
michael@0 95 // inactivate placeholder
michael@0 96 frame.setAttribute("src", UNLOADED_URL(aGroupName));
michael@0 97 }
michael@0 98
michael@0 99 } else {
michael@0 100 // This is the first time we hear about this group, so let's start tracking it,
michael@0 101 // and also preload it if the src attribute was set and aPreload = true
michael@0 102 group = new _SharedFrameGroup(src);
michael@0 103 Frames.set(aGroupName, group);
michael@0 104
michael@0 105 if (aPreload) {
michael@0 106 this.preload(aGroupName, frame);
michael@0 107 } else {
michael@0 108 frame.setAttribute("src", UNLOADED_URL(aGroupName));
michael@0 109 }
michael@0 110 }
michael@0 111
michael@0 112 aParent.appendChild(frame);
michael@0 113 return frame;
michael@0 114
michael@0 115 },
michael@0 116
michael@0 117 /**
michael@0 118 * Function that moves the loaded content from one active frame to
michael@0 119 * another one that is currently a placeholder. If there's no active
michael@0 120 * frame in the group, the content is loaded/reloaded.
michael@0 121 *
michael@0 122 * @param aGroupName the name of the group
michael@0 123 * @param aTargetFrame the frame element to which the content should
michael@0 124 * be moved to.
michael@0 125 */
michael@0 126 setOwner: function (aGroupName, aTargetFrame) {
michael@0 127 let group = Frames.get(aGroupName);
michael@0 128 let frame = group.activeFrame;
michael@0 129
michael@0 130 if (frame == aTargetFrame) {
michael@0 131 // nothing to do here
michael@0 132 return;
michael@0 133 }
michael@0 134
michael@0 135 if (group.isAlive) {
michael@0 136 // Move document ownership to the desired frame, and make it the active one
michael@0 137 frame.QueryInterface(Ci.nsIFrameLoaderOwner).swapFrameLoaders(aTargetFrame);
michael@0 138 group.activeFrame = aTargetFrame;
michael@0 139 } else {
michael@0 140 // Previous owner was dead, reload the document at the new owner and make it the active one
michael@0 141 aTargetFrame.setAttribute("src", group.url);
michael@0 142 group.activeFrame = aTargetFrame;
michael@0 143 }
michael@0 144 },
michael@0 145
michael@0 146 /**
michael@0 147 * Updates the current URL in use by this group, and loads it into the active frame.
michael@0 148 *
michael@0 149 * @param aGroupName the name of the group
michael@0 150 * @param aURL the new url
michael@0 151 */
michael@0 152 updateURL: function (aGroupName, aURL) {
michael@0 153 let group = Frames.get(aGroupName);
michael@0 154 group.url = aURL;
michael@0 155
michael@0 156 if (group.isAlive) {
michael@0 157 group.activeFrame.setAttribute("src", aURL);
michael@0 158 }
michael@0 159 },
michael@0 160
michael@0 161 /**
michael@0 162 * Loads the group's url into a target frame, if the group doesn't have a currently
michael@0 163 * active frame.
michael@0 164 *
michael@0 165 * @param aGroupName the name of the group
michael@0 166 * @param aTargetFrame the frame element which should be made active and
michael@0 167 * have the group's content loaded to
michael@0 168 */
michael@0 169 preload: function (aGroupName, aTargetFrame) {
michael@0 170 let group = Frames.get(aGroupName);
michael@0 171 if (!group.isAlive) {
michael@0 172 aTargetFrame.setAttribute("src", group.url);
michael@0 173 group.activeFrame = aTargetFrame;
michael@0 174 }
michael@0 175 },
michael@0 176
michael@0 177 /**
michael@0 178 * Tells if a group currently have an active element.
michael@0 179 *
michael@0 180 * @param aGroupName the name of the group
michael@0 181 */
michael@0 182 isGroupAlive: function (aGroupName) {
michael@0 183 return Frames.get(aGroupName).isAlive;
michael@0 184 },
michael@0 185
michael@0 186 /**
michael@0 187 * Forgets about this group. This function doesn't need to be used
michael@0 188 * unless the group's name needs to be reused.
michael@0 189 *
michael@0 190 * @param aGroupName the name of the group
michael@0 191 */
michael@0 192 forgetGroup: function (aGroupName) {
michael@0 193 Frames.delete(aGroupName);
michael@0 194 }
michael@0 195 }
michael@0 196
michael@0 197
michael@0 198 function _SharedFrameGroup(aURL) {
michael@0 199 this.url = aURL;
michael@0 200 this._activeFrame = null;
michael@0 201 }
michael@0 202
michael@0 203 _SharedFrameGroup.prototype = {
michael@0 204 get isAlive() {
michael@0 205 let frame = this.activeFrame;
michael@0 206 return !!(frame &&
michael@0 207 frame.contentDocument &&
michael@0 208 frame.contentDocument.location);
michael@0 209 },
michael@0 210
michael@0 211 get activeFrame() {
michael@0 212 return this._activeFrame &&
michael@0 213 this._activeFrame.get();
michael@0 214 },
michael@0 215
michael@0 216 set activeFrame(aActiveFrame) {
michael@0 217 this._activeFrame = aActiveFrame
michael@0 218 ? Cu.getWeakReference(aActiveFrame)
michael@0 219 : null;
michael@0 220 }
michael@0 221 }

mercurial