Backout a933e3c0ec0f (bug 749477) for causing a new random failure in m-oth

This commit is contained in:
Marco Bonardo 2012-09-12 21:26:38 +02:00
parent 1886002408
commit db05fdca4d
6 changed files with 137 additions and 115 deletions

View File

@ -112,38 +112,28 @@ const DEFAULT_SNIPPETS_URLS = [
const SNIPPETS_UPDATE_INTERVAL_MS = 86400000; // 1 Day. const SNIPPETS_UPDATE_INTERVAL_MS = 86400000; // 1 Day.
let gObserver = new MutationObserver(function (mutations) { let gSearchEngine;
for (let mutation of mutations) {
if (mutation.attributeName == "searchEngineURL") {
gObserver.disconnect();
setupSearchEngine();
loadSnippets();
return;
}
}
});
window.addEventListener("load", function () { document.addEventListener("DOMContentLoaded", function init() {
// Delay search engine setup, cause browser.js::BrowserOnAboutPageLoad runs setupSearchEngine();
// later and may use asynchronous getters. loadSnippets();
window.gObserver.observe(document.documentElement, { attributes: true });
fitToWidth();
}); });
window.addEventListener("load", fitToWidth);
window.addEventListener("resize", fitToWidth); window.addEventListener("resize", fitToWidth);
function onSearchSubmit(aEvent) function onSearchSubmit(aEvent)
{ {
let searchTerms = document.getElementById("searchText").value; let searchTerms = document.getElementById("searchText").value;
let searchURL = document.documentElement.getAttribute("searchEngineURL"); if (gSearchEngine && searchTerms.length > 0) {
if (searchURL && searchTerms.length > 0) {
const SEARCH_TOKENS = { const SEARCH_TOKENS = {
"_searchTerms_": encodeURIComponent(searchTerms) "_searchTerms_": encodeURIComponent(searchTerms)
} }
let url = gSearchEngine.searchUrl;
for (let key in SEARCH_TOKENS) { for (let key in SEARCH_TOKENS) {
searchURL = searchURL.replace(key, SEARCH_TOKENS[key]); url = url.replace(key, SEARCH_TOKENS[key]);
} }
window.location.href = searchURL; window.location.href = url;
} }
aEvent.preventDefault(); aEvent.preventDefault();
@ -152,24 +142,27 @@ function onSearchSubmit(aEvent)
function setupSearchEngine() function setupSearchEngine()
{ {
let searchEngineName = document.documentElement.getAttribute("searchEngineName"); gSearchEngine = JSON.parse(localStorage["search-engine"]);
let searchEngineInfo = SEARCH_ENGINES[searchEngineName];
if (!searchEngineInfo) { if (!gSearchEngine)
return; return;
// Look for extended information, like logo and links.
let searchEngineInfo = SEARCH_ENGINES[gSearchEngine.name];
if (searchEngineInfo) {
for (let prop in searchEngineInfo)
gSearchEngine[prop] = searchEngineInfo[prop];
} }
// Enqueue additional params if required by the engine definition. // Enqueue additional params if required by the engine definition.
if (searchEngineInfo.params) { if (gSearchEngine.params)
let searchEngineURL = document.documentElement.getAttribute("searchEngineURL"); gSearchEngine.searchUrl += "&" + gSearchEngine.params;
searchEngineURL += "&" + searchEngineInfo.params;
document.documentElement.setAttribute("searchEngineURL", searchEngineURL);
}
// Add search engine logo. // Add search engine logo.
if (searchEngineInfo.image) { if (gSearchEngine.image) {
let logoElt = document.getElementById("searchEngineLogo"); let logoElt = document.getElementById("searchEngineLogo");
logoElt.src = searchEngineInfo.image; logoElt.src = gSearchEngine.image;
logoElt.alt = searchEngineInfo.name; logoElt.alt = gSearchEngine.name;
} }
// The "autofocus" attribute doesn't focus the form element // The "autofocus" attribute doesn't focus the form element
@ -187,7 +180,7 @@ function loadSnippets()
{ {
// Check last snippets update. // Check last snippets update.
let lastUpdate = localStorage["snippets-last-update"]; let lastUpdate = localStorage["snippets-last-update"];
let updateURL = document.documentElement.getAttribute("snippetsURL"); let updateURL = localStorage["snippets-update-url"];
if (updateURL && (!lastUpdate || if (updateURL && (!lastUpdate ||
Date.now() - lastUpdate > SNIPPETS_UPDATE_INTERVAL_MS)) { Date.now() - lastUpdate > SNIPPETS_UPDATE_INTERVAL_MS)) {
// Try to update from network. // Try to update from network.

View File

@ -90,9 +90,6 @@ __defineSetter__("PluralForm", function (val) {
XPCOMUtils.defineLazyModuleGetter(this, "TelemetryStopwatch", XPCOMUtils.defineLazyModuleGetter(this, "TelemetryStopwatch",
"resource://gre/modules/TelemetryStopwatch.jsm"); "resource://gre/modules/TelemetryStopwatch.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "AboutHomeUtils",
"resource:///modules/AboutHomeUtils.jsm");
#ifdef MOZ_SERVICES_SYNC #ifdef MOZ_SERVICES_SYNC
XPCOMUtils.defineLazyGetter(this, "Weave", function() { XPCOMUtils.defineLazyGetter(this, "Weave", function() {
let tmp = {}; let tmp = {};
@ -2498,19 +2495,10 @@ function BrowserOnAboutPageLoad(document) {
// the hidden attribute set on the apps button in aboutHome.xhtml // the hidden attribute set on the apps button in aboutHome.xhtml
if (getBoolPref("browser.aboutHome.apps", false)) if (getBoolPref("browser.aboutHome.apps", false))
document.getElementById("apps").removeAttribute("hidden"); document.getElementById("apps").removeAttribute("hidden");
let ss = Components.classes["@mozilla.org/browser/sessionstore;1"]. let ss = Components.classes["@mozilla.org/browser/sessionstore;1"].
getService(Components.interfaces.nsISessionStore); getService(Components.interfaces.nsISessionStore);
if (!ss.canRestoreLastSession) if (!ss.canRestoreLastSession)
document.getElementById("launcher").removeAttribute("session"); document.getElementById("launcher").removeAttribute("session");
// Inject search engine and snippets URL.
let docElt = document.documentElement;
docElt.setAttribute("snippetsURL", AboutHomeUtils.snippetsURL);
docElt.setAttribute("searchEngineName",
AboutHomeUtils.defaultSearchEngine.name);
docElt.setAttribute("searchEngineURL",
AboutHomeUtils.defaultSearchEngine.searchURL);
} }
} }
@ -4490,14 +4478,9 @@ var TabsProgressListener = {
// We can't look for this during onLocationChange since at that point the // We can't look for this during onLocationChange since at that point the
// document URI is not yet the about:-uri of the error page. // document URI is not yet the about:-uri of the error page.
let document = aWebProgress.DOMWindow.document;
if (aStateFlags & Ci.nsIWebProgressListener.STATE_STOP && if (aStateFlags & Ci.nsIWebProgressListener.STATE_STOP &&
Components.isSuccessCode(aStatus) && Components.isSuccessCode(aStatus) &&
/^about:/.test(document.documentURI) && /^about:/.test(aWebProgress.DOMWindow.document.documentURI)) {
!document.documentElement.hasAttribute("hasBrowserHandlers")) {
// STATE_STOP may be received twice for documents, thus store an
// attribute to ensure handling it just once.
document.documentElement.setAttribute("hasBrowserHandlers", "true");
aBrowser.addEventListener("click", BrowserOnClick, true); aBrowser.addEventListener("click", BrowserOnClick, true);
aBrowser.addEventListener("pagehide", function onPageHide(event) { aBrowser.addEventListener("pagehide", function onPageHide(event) {
if (event.target.defaultView.frameElement) if (event.target.defaultView.frameElement)
@ -4507,7 +4490,7 @@ var TabsProgressListener = {
}, true); }, true);
// We also want to make changes to page UI for unprivileged about pages. // We also want to make changes to page UI for unprivileged about pages.
BrowserOnAboutPageLoad(document); BrowserOnAboutPageLoad(aWebProgress.DOMWindow.document);
} }
}, },

View File

@ -15,17 +15,64 @@ registerCleanupFunction(function() {
let gTests = [ let gTests = [
{ {
desc: "Check that clearing cookies does not clear storage", desc: "Check that rejecting cookies does not prevent page from working",
setup: function () setup: function ()
{ {
Cc["@mozilla.org/dom/storagemanager;1"] Services.prefs.setIntPref("network.cookies.cookieBehavior", 2);
.getService(Ci.nsIObserver)
.observe(null, "cookie-changed", "cleared");
}, },
run: function () run: function ()
{ {
let storage = getStorage(); let storage = getStorage();
isnot(storage.getItem("snippets-last-update"), null); isnot(storage.getItem("search-engine"), null);
try {
Services.prefs.clearUserPref("network.cookies.cookieBehavior");
} catch (ex) {}
executeSoon(runNextTest);
}
},
{
desc: "Check that asking for cookies does not prevent page from working",
setup: function ()
{
Services.prefs.setIntPref("network.cookie.lifetimePolicy", 1);
},
run: function ()
{
let storage = getStorage();
isnot(storage.getItem("search-engine"), null);
try {
Services.prefs.clearUserPref("network.cookie.lifetimePolicy");
} catch (ex) {}
executeSoon(runNextTest);
}
},
{
desc: "Check that clearing cookies does not prevent page from working",
setup: function ()
{
Components.classes["@mozilla.org/dom/storagemanager;1"].
getService(Components.interfaces.nsIObserver).
observe(null, "cookie-changed", "cleared");
},
run: function ()
{
let storage = getStorage();
isnot(storage.getItem("search-engine"), null);
executeSoon(runNextTest);
}
},
{
desc: "Check normal status is working",
setup: function ()
{
},
run: function ()
{
let storage = getStorage();
isnot(storage.getItem("search-engine"), null);
executeSoon(runNextTest); executeSoon(runNextTest);
} }
}, },
@ -73,6 +120,14 @@ function test()
{ {
waitForExplicitFinish(); waitForExplicitFinish();
// browser-chrome test harness inits browser specifying an hardcoded page
// and this causes nsIBrowserHandler.defaultArgs to not be evaluated since
// there is a predefined argument.
// About:home localStorage is populated with overridden homepage, that is
// setup in the defaultArgs getter.
// Thus to populate about:home we need to get defaultArgs manually.
Cc["@mozilla.org/browser/clh;1"].getService(Ci.nsIBrowserHandler).defaultArgs;
// Ensure that by default we don't try to check for remote snippets since that // Ensure that by default we don't try to check for remote snippets since that
// could be tricky due to network bustages or slowness. // could be tricky due to network bustages or slowness.
let storage = getStorage(); let storage = getStorage();
@ -93,20 +148,10 @@ function runNextTest()
info(test.desc); info(test.desc);
test.setup(); test.setup();
let tab = gBrowser.selectedTab = gBrowser.addTab("about:home"); let tab = gBrowser.selectedTab = gBrowser.addTab("about:home");
tab.linkedBrowser.addEventListener("load", function load(event) { tab.linkedBrowser.addEventListener("load", function (event) {
tab.linkedBrowser.removeEventListener("load", load, true); tab.linkedBrowser.removeEventListener("load", arguments.callee, true);
// Some part of the page is populated on load, so enqueue on it.
let observer = new MutationObserver(function (mutations) { executeSoon(test.run);
for (let mutation of mutations) {
if (mutation.attributeName == "searchEngineURL") {
observer.disconnect();
executeSoon(test.run);
return;
}
}
});
let docElt = tab.linkedBrowser.contentDocument.documentElement;
observer.observe(docElt, { attributes: true });
}, true); }, true);
} }
else { else {

View File

@ -577,6 +577,10 @@ nsBrowserContentHandler.prototype = {
} catch (ex) {} } catch (ex) {}
let override = needHomepageOverride(prefb); let override = needHomepageOverride(prefb);
if (override != OVERRIDE_NONE) { if (override != OVERRIDE_NONE) {
// Setup the default search engine to about:home page.
AboutHomeUtils.loadDefaultSearchEngine();
AboutHomeUtils.loadSnippetsURL();
switch (override) { switch (override) {
case OVERRIDE_NEW_PROFILE: case OVERRIDE_NEW_PROFILE:
// New profile. // New profile.
@ -599,6 +603,13 @@ nsBrowserContentHandler.prototype = {
break; break;
} }
} }
else {
// No need to override homepage, but update snippets url if the pref has
// been manually changed.
if (Services.prefs.prefHasUserValue(AboutHomeUtils.SNIPPETS_URL_PREF)) {
AboutHomeUtils.loadSnippetsURL();
}
}
} catch (ex) {} } catch (ex) {}
// formatURLPref might return "about:blank" if getting the pref fails // formatURLPref might return "about:blank" if getting the pref fails
@ -824,5 +835,41 @@ nsDefaultCommandLineHandler.prototype = {
helpInfo : "", helpInfo : "",
}; };
let AboutHomeUtils = {
SNIPPETS_URL_PREF: "browser.aboutHomeSnippets.updateUrl",
get _storage() {
let aboutHomeURI = Services.io.newURI("moz-safe-about:home", null, null);
let principal = Components.classes["@mozilla.org/scriptsecuritymanager;1"].
getService(Components.interfaces.nsIScriptSecurityManager).
getNoAppCodebasePrincipal(aboutHomeURI);
let dsm = Components.classes["@mozilla.org/dom/storagemanager;1"].
getService(Components.interfaces.nsIDOMStorageManager);
return dsm.getLocalStorageForPrincipal(principal, "");
},
loadDefaultSearchEngine: function AHU_loadDefaultSearchEngine()
{
let defaultEngine = Services.search.originalDefaultEngine;
let submission = defaultEngine.getSubmission("_searchTerms_");
if (submission.postData)
throw new Error("Home page does not support POST search engines.");
let engine = {
name: defaultEngine.name
, searchUrl: submission.uri.spec
}
this._storage.setItem("search-engine", JSON.stringify(engine));
},
loadSnippetsURL: function AHU_loadSnippetsURL()
{
const STARTPAGE_VERSION = 3;
let updateURL = Services.prefs
.getCharPref(this.SNIPPETS_URL_PREF)
.replace("%STARTPAGE_VERSION%", STARTPAGE_VERSION);
updateURL = Services.urlFormatter.formatURL(updateURL);
this._storage.setItem("snippets-update-url", updateURL);
},
};
var components = [nsBrowserContentHandler, nsDefaultCommandLineHandler]; var components = [nsBrowserContentHandler, nsDefaultCommandLineHandler];
var NSGetFactory = XPCOMUtils.generateNSGetFactory(components); var NSGetFactory = XPCOMUtils.generateNSGetFactory(components);

View File

@ -1,45 +0,0 @@
/* 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";
const EXPORTED_SYMBOLS = [ "AboutHomeUtils" ];
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
Components.utils.import("resource://gre/modules/Services.jsm");
// Url to fetch snippets, in the urlFormatter service format.
const SNIPPETS_URL_PREF = "browser.aboutHomeSnippets.updateUrl";
// Should be bumped up if the snippets content format changes.
const STARTPAGE_VERSION = 3;
let AboutHomeUtils = new Object();
/**
* Returns an object containing the name and searchURL of the original default
* search engine.
*/
XPCOMUtils.defineLazyGetter(AboutHomeUtils, "defaultSearchEngine", function() {
let defaultEngine = Services.search.originalDefaultEngine;
let submission = defaultEngine.getSubmission("_searchTerms_");
if (submission.postData) {
throw new Error("Home page does not support POST search engines.");
}
return Object.freeze({
name: defaultEngine.name,
searchURL: submission.uri.spec
});
});
/**
* Returns the URL to fetch snippets from, in the urlFormatter service format.
*/
XPCOMUtils.defineLazyGetter(AboutHomeUtils, "snippetsURL", function() {
let updateURL = Services.prefs
.getCharPref(SNIPPETS_URL_PREF)
.replace("%STARTPAGE_VERSION%", STARTPAGE_VERSION);
return Services.urlFormatter.formatURL(updateURL);
});

View File

@ -14,7 +14,6 @@ include $(topsrcdir)/config/config.mk
TEST_DIRS += test TEST_DIRS += test
EXTRA_JS_MODULES = \ EXTRA_JS_MODULES = \
AboutHomeUtils.jsm \
BrowserNewTabPreloader.jsm \ BrowserNewTabPreloader.jsm \
openLocationLastURL.jsm \ openLocationLastURL.jsm \
NetworkPrioritizer.jsm \ NetworkPrioritizer.jsm \