Bug 811247 - Create only one instance of each social ambient panel instead of one per window. r=jaws,markh

This commit is contained in:
Felipe Gomes 2012-12-06 23:38:46 -08:00
Родитель b2a17d3c4b
Коммит 2567b32f2f
7 изменённых файлов: 493 добавлений и 17 удалений

Просмотреть файл

@ -6,6 +6,9 @@
const PANEL_MIN_HEIGHT = 100;
const PANEL_MIN_WIDTH = 330;
XPCOMUtils.defineLazyModuleGetter(this, "SharedFrame",
"resource:///modules/SharedFrame.jsm");
let SocialUI = {
// Called on delayed startup to initialize UI
init: function SocialUI_init() {
@ -678,8 +681,11 @@ var SocialToolbar = {
if (!SocialUI.haveLoggedInUser() || !socialEnabled) {
let parent = document.getElementById("social-notification-panel");
while (parent.hasChildNodes())
parent.removeChild(parent.firstChild);
while (parent.hasChildNodes()) {
let frame = parent.firstChild;
SharedFrame.forgetGroup(frame.id);
parent.removeChild(frame);
}
while (tbi.lastChild != tbi.firstChild)
tbi.removeChild(tbi.lastChild);
@ -757,7 +763,6 @@ var SocialToolbar = {
str);
}
let notificationFrames = document.createDocumentFragment();
let iconContainers = document.createDocumentFragment();
let createdFrames = [];
@ -767,22 +772,32 @@ var SocialToolbar = {
let notificationFrameId = "social-status-" + icon.name;
let notificationFrame = document.getElementById(notificationFrameId);
if (!notificationFrame) {
notificationFrame = document.createElement("iframe");
notificationFrame.setAttribute("type", "content");
notificationFrame.setAttribute("class", "social-panel-frame");
notificationFrame.setAttribute("id", notificationFrameId);
notificationFrame.setAttribute("mozbrowser", "true");
// work around bug 793057 - by making the panel roughly the final size
// we are more likely to have the anchor in the correct position.
notificationFrame.style.width = PANEL_MIN_WIDTH + "px";
notificationFrame = SharedFrame.createFrame(
notificationFrameId, /* frame name */
panel, /* parent */
{
"type": "content",
"mozbrowser": "true",
"class": "social-panel-frame",
"id": notificationFrameId,
// work around bug 793057 - by making the panel roughly the final size
// we are more likely to have the anchor in the correct position.
"style": "width: " + PANEL_MIN_WIDTH + "px;",
"origin": provider.origin,
"src": icon.contentPanel
}
);
createdFrames.push(notificationFrame);
notificationFrames.appendChild(notificationFrame);
} else {
notificationFrame.setAttribute("origin", provider.origin);
SharedFrame.updateURL(notificationFrameId, icon.contentPanel);
}
notificationFrame.setAttribute("origin", provider.origin);
if (notificationFrame.getAttribute("src") != icon.contentPanel)
notificationFrame.setAttribute("src", icon.contentPanel);
let iconId = "social-notification-icon-" + icon.name;
let imageId = iconId + "-image";
@ -833,7 +848,6 @@ var SocialToolbar = {
image.style.listStyleImage = "url(" + icon.iconURL + ")";
}
panel.appendChild(notificationFrames);
iconBox.appendChild(iconContainers);
for (let frame of createdFrames) {
@ -856,6 +870,9 @@ var SocialToolbar = {
let notificationFrameId = aToolbarButtonBox.getAttribute("notificationFrameId");
let notificationFrame = document.getElementById(notificationFrameId);
let wasAlive = SharedFrame.isGroupAlive(notificationFrameId);
SharedFrame.setOwner(notificationFrameId, notificationFrame);
// Clear dimensions on all browsers so the panel size will
// only use the selected browser.
let frameIter = panel.firstElementChild;
@ -884,7 +901,7 @@ var SocialToolbar = {
aToolbarButtonBox.setAttribute("open", "true");
notificationFrame.docShell.isActive = true;
notificationFrame.docShell.isAppTab = true;
if (notificationFrame.contentDocument.readyState == "complete") {
if (notificationFrame.contentDocument.readyState == "complete" && wasAlive) {
dynamicResizer.start(panel, notificationFrame);
dispatchPanelEvent("socialFrameShow");
} else {

Просмотреть файл

@ -26,6 +26,7 @@ EXTRA_JS_MODULES = \
webappsUI.jsm \
webrtcUI.jsm \
KeywordURLResetPrompter.jsm \
SharedFrame.jsm \
$(NULL)
EXTRA_PP_JS_MODULES = RecentWindow.jsm

Просмотреть файл

@ -0,0 +1,221 @@
/* 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";
this.EXPORTED_SYMBOLS = [ "SharedFrame" ];
const Ci = Components.interfaces;
const Cu = Components.utils;
/**
* The purpose of this module is to create and group various iframe
* elements that are meant to all display the same content and only
* one at a time. This makes it possible to have the content loaded
* only once, while the other iframes can be kept as placeholders to
* quickly move the content to them through the swapFrameLoaders function
* when another one of the placeholder is meant to be displayed.
* */
let Frames = new Map();
/**
* The Frames map is the main data structure that holds information
* about the groups being tracked. Each entry's key is the group name,
* and the object holds information about what is the URL being displayed
* on that group, and what is the active element on the group (the frame that
* holds the loaded content).
* The reference to the activeFrame is a weak reference, which allows the
* frame to go away at any time, and when that happens the module considers that
* there are no active elements in that group. The group can be reactivated
* by changing the URL, calling preload again or adding a new element.
*
*
* Frames = {
* "messages-panel": {
* url: string,
* activeFrame: weakref
* }
* }
*
* Each object on the map is called a _SharedFrameGroup, which is an internal
* class of this module which does not automatically keep track of its state. This
* object should not be used externally, and all control should be handled by the
* module's functions.
*/
function UNLOADED_URL(aStr) "data:text/html;charset=utf-8,<!-- Unloaded frame " + aStr + " -->";
this.SharedFrame = {
/**
* Creates an iframe element and track it as part of the specified group
* The module must create the iframe itself because it needs to do some special
* handling for the element's src attribute.
*
* @param aGroupName the name of the group to which this frame belongs
* @param aParent the parent element to which the frame will be appended to
* @param aFrameAttributes an object with a list of attributes to set in the iframe
* before appending it to the DOM. The "src" attribute has
* special meaning here and if it's not blank it specifies
* the URL that will be initially assigned to this group
* @param aPreload optional, tells if the URL specified in the src attribute
* should be preloaded in the frame being created, in case
* it's not yet preloaded in any other frame of the group.
* This parameter has no meaning if src is blank.
*/
createFrame: function (aGroupName, aParent, aFrameAttributes, aPreload = true) {
let frame = aParent.ownerDocument.createElement("iframe");
for (let [key, val] of Iterator(aFrameAttributes)) {
frame.setAttribute(key, val);
}
let src = aFrameAttributes.src;
if (!src) {
aPreload = false;
}
let group = Frames.get(aGroupName);
if (group) {
// If this group has already been created
if (aPreload && !group.isAlive) {
// If aPreload is set and the group is not already loaded, load it.
// This can happen if:
// - aPreload was not used while creating the previous frames of this group, or
// - the previously active frame went dead in the meantime
group.url = src;
this.preload(aGroupName, frame);
} else {
// If aPreload is not set, or the group is already loaded in a different frame,
// there's not much that we need to do here: just create this frame as an
// inactivate placeholder
frame.setAttribute("src", UNLOADED_URL(aGroupName));
}
} else {
// This is the first time we hear about this group, so let's start tracking it,
// and also preload it if the src attribute was set and aPreload = true
group = new _SharedFrameGroup(src);
Frames.set(aGroupName, group);
if (aPreload) {
this.preload(aGroupName, frame);
} else {
frame.setAttribute("src", UNLOADED_URL(aGroupName));
}
}
aParent.appendChild(frame);
return frame;
},
/**
* Function that moves the loaded content from one active frame to
* another one that is currently a placeholder. If there's no active
* frame in the group, the content is loaded/reloaded.
*
* @param aGroupName the name of the group
* @param aTargetFrame the frame element to which the content should
* be moved to.
*/
setOwner: function (aGroupName, aTargetFrame) {
let group = Frames.get(aGroupName);
let frame = group.activeFrame;
if (frame == aTargetFrame) {
// nothing to do here
return;
}
if (group.isAlive) {
// Move document ownership to the desired frame, and make it the active one
frame.QueryInterface(Ci.nsIFrameLoaderOwner).swapFrameLoaders(aTargetFrame);
group.activeFrame = aTargetFrame;
} else {
// Previous owner was dead, reload the document at the new owner and make it the active one
aTargetFrame.setAttribute("src", group.url);
group.activeFrame = aTargetFrame;
}
},
/**
* Updates the current URL in use by this group, and loads it into the active frame.
*
* @param aGroupName the name of the group
* @param aURL the new url
*/
updateURL: function (aGroupName, aURL) {
let group = Frames.get(aGroupName);
group.url = aURL;
if (group.isAlive) {
group.activeFrame.setAttribute("src", aURL);
}
},
/**
* Loads the group's url into a target frame, if the group doesn't have a currently
* active frame.
*
* @param aGroupName the name of the group
* @param aTargetFrame the frame element which should be made active and
* have the group's content loaded to
*/
preload: function (aGroupName, aTargetFrame) {
let group = Frames.get(aGroupName);
if (!group.isAlive) {
aTargetFrame.setAttribute("src", group.url);
group.activeFrame = aTargetFrame;
}
},
/**
* Tells if a group currently have an active element.
*
* @param aGroupName the name of the group
*/
isGroupAlive: function (aGroupName) {
return Frames.get(aGroupName).isAlive;
},
/**
* Forgets about this group. This function doesn't need to be used
* unless the group's name needs to be reused.
*
* @param aGroupName the name of the group
*/
forgetGroup: function (aGroupName) {
Frames.delete(aGroupName);
}
}
function _SharedFrameGroup(aURL) {
this.url = aURL;
this._activeFrame = null;
}
_SharedFrameGroup.prototype = {
get isAlive() {
let frame = this.activeFrame;
return !!(frame &&
frame.contentDocument &&
frame.contentDocument.location);
},
get activeFrame() {
return this._activeFrame &&
this._activeFrame.get();
},
set activeFrame(aActiveFrame) {
this._activeFrame = aActiveFrame
? Cu.getWeakReference(aActiveFrame)
: null;
}
}

Просмотреть файл

@ -8,6 +8,10 @@ srcdir = @srcdir@
VPATH = @srcdir@
relativesrcdir = @relativesrcdir@
DIRS = \
chrome \
$(NULL)
include $(DEPTH)/config/autoconf.mk
XPCSHELL_TESTS = unit

Просмотреть файл

@ -0,0 +1,18 @@
# 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/.
DEPTH = @DEPTH@
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
relativesrcdir = @relativesrcdir@
include $(DEPTH)/config/autoconf.mk
MOCHITEST_CHROME_FILES = \
test_sharedframe.xul \
sharedframe.xul \
$(NULL)
include $(topsrcdir)/config/rules.mk

Просмотреть файл

@ -0,0 +1,174 @@
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
type="text/css"?>
<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<window title="Test SharedFrame - Bug 811247"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
onload="runTest();">
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
<script type="application/javascript">
<![CDATA[
function is(a,b,c) opener.wrappedJSObject.is(a,b,c);
function ok(a,b) opener.wrappedJSObject.ok(a,b);
function done() opener.wrappedJSObject.done();
Components.utils.import("resource:///modules/SharedFrame.jsm");
ok(SharedFrame, "SharedFrame module exists");
let box, gGen;
function runTest() {
box = document.getElementById("frames-container");
gGen = test_module();
gGen.next();
}
function test_module() {
// note: no 'src' attribute means aPreload = false;
let frame1 = SharedFrame.createFrame("group1", box, {id: "group1-frame1", type: "content"});
let frame2 = SharedFrame.createFrame("group1", box, {id: "group1-frame2", type: "content"});
let frame3 = SharedFrame.createFrame("group1", box, {id: "group1-frame3", type: "content"});
// Check proper attribute assignment
is(frame1.id, "group1-frame1", "correct id");
is(frame2.id, "group1-frame2", "correct id");
is(frame3.id, "group1-frame3", "correct id");
is(frame1.getAttribute("type"), "content", "correct type");
is(frame2.getAttribute("type"), "content", "correct type");
is(frame3.getAttribute("type"), "content", "correct type");
//--------------------------
yield waitForLoad([frame1, frame2, frame3]);
// Check for unloaded in the src URL
ok(/Unloaded/.test(frame1.contentDocument.location), "frame 1 is unloaded");
ok(/Unloaded/.test(frame2.contentDocument.location), "frame 2 is unloaded");
ok(/Unloaded/.test(frame3.contentDocument.location), "frame 3 is unloaded");
// Check that there is no frame alive in the group
ok(!SharedFrame.isGroupAlive("group1"), "group 1 is not alive");
// Set the URL and load the group
SharedFrame.updateURL("group1", "http://www.example.com");
SharedFrame.preload("group1", frame1);
//--------------------------
yield waitForLoad([frame1]);
// Check that frame 1 was properly loaded and the group is alive
ok(SharedFrame.isGroupAlive("group1"), "group 1 is now alive");
ok(!/Unloaded/.test(frame1.contentDocument.location), "frame 1 is now loaded");
ok(/Unloaded/.test(frame2.contentDocument.location), "frame 2 is unloaded");
ok(/Unloaded/.test(frame3.contentDocument.location), "frame 3 is unloaded");
// Move content to frame 2
SharedFrame.setOwner("group1", frame2);
ok(/Unloaded/.test(frame1.contentDocument.location), "frame 1 is unloaded");
ok(!/Unloaded/.test(frame2.contentDocument.location), "content was transfered to frame 2");
ok(/Unloaded/.test(frame3.contentDocument.location), "frame 3 is unloaded");
// Update URL and check that new content got loaded
SharedFrame.updateURL("group1", "http://www.example.com/new");
//--------------------------
yield waitForLoad([frame2]);
ok(/new$/.test(frame2.contentDocument.location), "new url loaded");
// Now remove the loaded content and check if the group is properly reported as unloaded
box.removeChild(frame2);
ok(!SharedFrame.isGroupAlive("group1"), "group 1 is not alive");
// And see if setOwnering will reload the group
SharedFrame.setOwner("group1", frame3);
//--------------------------
yield waitForLoad([frame3]);
ok(SharedFrame.isGroupAlive("group1"), "group 1 is alive");
ok(/new$/.test(frame3.contentDocument.location), "content was transfered to frame 3");
// Create a second group to verify it doesn't interact with the first one. Also test
// that preloading works
let frame4 = SharedFrame.createFrame("group2", box, {src: "http://www.example.com/group2", type: "content"});
let frame5 = SharedFrame.createFrame("group2", box, {src: "http://www.example.com/group2", type: "content"});
//--------------------------
yield waitForLoad([frame4, frame5]);
ok(SharedFrame.isGroupAlive("group2"), "group 2 was preloaded due to the src attribute");
// Check for unloaded in the src URL
ok(/group2$/.test(frame4.contentDocument.location), "frame 4 is loaded");
ok(/Unloaded/.test(frame5.contentDocument.location), "frame 5 is unloaded");
SharedFrame.setOwner("group2", frame5);
ok(/Unloaded/.test(frame4.contentDocument.location), "frame 4 is unloaded");
ok(/group2$/.test(frame5.contentDocument.location), "frame 5 is loaded");
SharedFrame.updateURL("group2", "http://www.example.com/new2");
//--------------------------
yield waitForLoad([frame5]);
ok(/new2$/.test(frame5.contentDocument.location), "frame 5 changed");
ok(/Unloaded/.test(frame1.contentDocument.location), "frame 1 still has its previous value");
ok(/new$/.test(frame3.contentDocument.location), "frame 3 still has its previous value");
//And now check that aPreload parameter works
let frame7 = SharedFrame.createFrame("group3", box, {src: "http://www.example.com/group3", type: "content"}, false);
//--------------------------
yield waitForLoad([frame7]);
ok(!SharedFrame.isGroupAlive("group3"), "aPreload = false works");
ok(/Unloaded/.test(frame7.contentDocument.location), "frame 7 is unloaded");
let frame8 = SharedFrame.createFrame("group3", box, {src: "http://www.example.com/group3", type: "content"});
//--------------------------
yield waitForLoad([frame8]);
ok(SharedFrame.isGroupAlive("group3"), "aPreload defauls to true");
ok(/group3/.test(frame8.contentDocument.location), "aPreload + src loads/reloads group");
done();
}
function waitForLoad(frames) {
let count = frames.length;
for (let frame of frames) {
let f = frame;
f.addEventListener("DOMContentLoaded", function frameloaded(event) {
f.removeEventListener("DOMContentLoaded", frameloaded, false);
if (--count == 0) {
try { gGen.next() } catch (ex if ex instanceof StopIteration) { }
}
}, false);
}
}
]]>
</script>
<box id="frames-container"/>
<body xmlns="http://www.w3.org/1999/xhtml">
<p id="display"></p>
<div id="content" style="display:none;"></div>
<pre id="test"></pre>
</body>
<label id="test-result"/>
</window>

Просмотреть файл

@ -0,0 +1,41 @@
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
type="text/css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=811247
-->
<window title="Mozilla Bug 811247"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<!-- test results are displayed in the html:body -->
<body xmlns="http://www.w3.org/1999/xhtml">
<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=811247"
target="_blank">Mozilla Bug 811247</a>
</body>
<script type="application/javascript"><![CDATA[
/* Test for Bug 811247
*
* The test must run on a separate window becase .swapFrameLoaders currently won't swap
* iframes that are inside frames with history enabled, which is the case of the test running
* in the content area of a regular browser window; so we need a blank xul window for that
*/
SimpleTest.waitForExplicitFinish();
let testWin;
function done() {
testWin.close();
SimpleTest.finish();
}
addLoadEvent(function() {
testWin = window.open("sharedframe.xul", "", "chrome,dialog,width=400,height=400");
});
]]></script>
</window>