Bug 823547 - Store the snippets version along with the snippets.

r=adw
This commit is contained in:
Marco Bonardo 2013-02-27 18:57:50 +01:00
Родитель 5987d821e9
Коммит 531ed4f366
4 изменённых файлов: 56 добавлений и 3 удалений

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

@ -134,6 +134,7 @@ function ensureSnippetsMapThen(aCallback)
// Populate the cache from the persistent storage. // Populate the cache from the persistent storage.
let cache = new Map(); let cache = new Map();
for (let key of [ "snippets-last-update", for (let key of [ "snippets-last-update",
"snippets-cached-version",
"snippets" ]) { "snippets" ]) {
cache.set(key, localStorage[key]); cache.set(key, localStorage[key]);
} }
@ -229,6 +230,14 @@ function loadSnippets()
if (!gSnippetsMap) if (!gSnippetsMap)
throw new Error("Snippets map has not properly been initialized"); throw new Error("Snippets map has not properly been initialized");
// Check cached snippets version.
let cachedVersion = gSnippetsMap.get("snippets-cached-version") || 0;
let currentVersion = document.documentElement.getAttribute("snippetsVersion");
if (cachedVersion < currentVersion) {
// The cached snippets are old and unsupported, restart from scratch.
gSnippetsMap.clear();
}
// Check last snippets update. // Check last snippets update.
let lastUpdate = gSnippetsMap.get("snippets-last-update"); let lastUpdate = gSnippetsMap.get("snippets-last-update");
let updateURL = document.documentElement.getAttribute("snippetsURL"); let updateURL = document.documentElement.getAttribute("snippetsURL");
@ -253,6 +262,7 @@ function loadSnippets()
{ {
if (xhr.status == 200) { if (xhr.status == 200) {
gSnippetsMap.set("snippets", xhr.responseText); gSnippetsMap.set("snippets", xhr.responseText);
gSnippetsMap.set("snippets-cached-version", currentVersion);
} }
showSnippets(); showSnippets();
}; };

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

@ -2630,6 +2630,7 @@ function BrowserOnAboutPageLoad(doc) {
// Inject search engine and snippets URL. // Inject search engine and snippets URL.
let docElt = doc.documentElement; let docElt = doc.documentElement;
docElt.setAttribute("snippetsURL", AboutHomeUtils.snippetsURL); docElt.setAttribute("snippetsURL", AboutHomeUtils.snippetsURL);
docElt.setAttribute("snippetsVersion", AboutHomeUtils.snippetsVersion);
docElt.setAttribute("searchEngineName", docElt.setAttribute("searchEngineName",
AboutHomeUtils.defaultSearchEngine.name); AboutHomeUtils.defaultSearchEngine.name);
docElt.setAttribute("searchEngineURL", docElt.setAttribute("searchEngineURL",

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

@ -6,6 +6,8 @@ XPCOMUtils.defineLazyModuleGetter(this, "Promise",
"resource://gre/modules/commonjs/sdk/core/promise.js"); "resource://gre/modules/commonjs/sdk/core/promise.js");
XPCOMUtils.defineLazyModuleGetter(this, "Task", XPCOMUtils.defineLazyModuleGetter(this, "Task",
"resource://gre/modules/Task.jsm"); "resource://gre/modules/Task.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "AboutHomeUtils",
"resource:///modules/AboutHomeUtils.jsm");
registerCleanupFunction(function() { registerCleanupFunction(function() {
// Ensure we don't pollute prefs for next tests. // Ensure we don't pollute prefs for next tests.
@ -42,7 +44,7 @@ let gTests = [
let snippetsElt = doc.getElementById("snippets"); let snippetsElt = doc.getElementById("snippets");
ok(snippetsElt, "Found snippets element") ok(snippetsElt, "Found snippets element")
is(snippetsElt.getElementsByTagName("span").length, 1, is(snippetsElt.getElementsByTagName("span").length, 1,
"A default snippet is visible."); "A default snippet is present.");
} }
}, },
@ -60,7 +62,7 @@ let gTests = [
let snippetsElt = doc.getElementById("snippets"); let snippetsElt = doc.getElementById("snippets");
ok(snippetsElt, "Found snippets element"); ok(snippetsElt, "Found snippets element");
is(snippetsElt.getElementsByTagName("span").length, 1, is(snippetsElt.getElementsByTagName("span").length, 1,
"A default snippet is visible."); "A default snippet is present.");
aSnippetsMap.delete("snippets"); aSnippetsMap.delete("snippets");
} }
@ -160,6 +162,43 @@ let gTests = [
} }
}, },
{
desc: "Check snippets map is cleared if cached version is old",
setup: function (aSnippetsMap)
{
aSnippetsMap.set("snippets", "test");
aSnippetsMap.set("snippets-cached-version", 0);
},
run: function (aSnippetsMap)
{
ok(!aSnippetsMap.has("snippets"), "snippets have been properly cleared");
ok(!aSnippetsMap.has("snippets-cached-version"),
"cached-version has been properly cleared");
}
},
{
desc: "Check cached snippets are shown if cached version is current",
setup: function (aSnippetsMap)
{
aSnippetsMap.set("snippets", "test");
},
run: function (aSnippetsMap)
{
let doc = gBrowser.selectedTab.linkedBrowser.contentDocument;
let snippetsElt = doc.getElementById("snippets");
ok(snippetsElt, "Found snippets element");
is(snippetsElt.innerHTML, "test", "Cached snippet is present.");
is(aSnippetsMap.get("snippets"), "test", "snippets still cached");
is(aSnippetsMap.get("snippets-cached-version"),
AboutHomeUtils.snippetsVersion,
"cached-version is correct");
ok(aSnippetsMap.has("snippets-last-update"), "last-update still exists");
}
},
]; ];
function test() function test()
@ -228,6 +267,7 @@ function promiseSetupSnippetsMap(aTab, aSetupFn)
cw.ensureSnippetsMapThen(function (aSnippetsMap) { cw.ensureSnippetsMapThen(function (aSnippetsMap) {
// Don't try to update. // Don't try to update.
aSnippetsMap.set("snippets-last-update", Date.now()); aSnippetsMap.set("snippets-last-update", Date.now());
aSnippetsMap.set("snippets-cached-version", AboutHomeUtils.snippetsVersion);
// Clear snippets. // Clear snippets.
aSnippetsMap.delete("snippets"); aSnippetsMap.delete("snippets");
aSetupFn(aSnippetsMap); aSetupFn(aSnippetsMap);

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

@ -15,7 +15,9 @@ const SNIPPETS_URL_PREF = "browser.aboutHomeSnippets.updateUrl";
// Should be bumped up if the snippets content format changes. // Should be bumped up if the snippets content format changes.
const STARTPAGE_VERSION = 4; const STARTPAGE_VERSION = 4;
this.AboutHomeUtils = new Object(); this.AboutHomeUtils = {
get snippetsVersion() STARTPAGE_VERSION
};
/** /**
* Returns an object containing the name and searchURL of the original default * Returns an object containing the name and searchURL of the original default