Bug 1254392 - Change getJSON behaviour and move caches to local storage, r=jryans

This commit is contained in:
Benoit Chabod 2016-06-01 06:15:00 +02:00
Родитель cd94cb207b
Коммит 4117da6f9f
6 изменённых файлов: 64 добавлений и 24 удалений

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

@ -47,11 +47,11 @@ function AddDevice(device, type = "phones") {
exports.AddDevice = AddDevice;
// Get the complete devices catalog.
function GetDevices(bypassCache = false) {
function GetDevices() {
let deferred = promise.defer();
// Fetch common devices from Mozilla's CDN.
getJSON(DEVICES_URL, bypassCache).then(devices => {
getJSON(DEVICES_URL).then(devices => {
for (let type in localDevices) {
if (!devices[type]) {
devices.TYPES.push(type);

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

@ -8,36 +8,61 @@ const {CC} = require("chrome");
const promise = require("promise");
const Services = require("Services");
loader.lazyRequireGetter(this, "asyncStorage", "devtools/shared/async-storage");
const XMLHttpRequest = CC("@mozilla.org/xmlextras/xmlhttprequest;1");
// Downloads and caches a JSON file from a URL given by the pref.
exports.getJSON = function (prefName, bypassCache) {
if (!bypassCache) {
try {
let str = Services.prefs.getCharPref(prefName + "_cache");
let json = JSON.parse(str);
return promise.resolve(json);
} catch (e) {
// no pref or invalid json. Let's continue
}
}
/**
* Downloads and caches a JSON file from an URL given by a pref.
*
* @param {String} prefName
* The preference for the target URL
*
* @return {Promise}
* - Resolved with the JSON object in case of successful request
* or cache hit
* - Rejected with an error message in case of failure
*/
exports.getJSON = function (prefName) {
let deferred = promise.defer();
let xhr = new XMLHttpRequest();
// We used to store cached data in preferences, but now we use asyncStorage
// Migration step: if it still exists, move this now useless preference in its
// new location and clear it
if (Services.prefs.prefHasUserValue(prefName + "_cache")) {
let json = Services.prefs.getCharPref(prefName + "_cache");
asyncStorage.setItem(prefName + "_cache", json).catch(function (e) {
// Could not move the cache, let's log the error but continue
console.error(e);
});
Services.prefs.clearUserPref(prefName + "_cache");
}
function readFromStorage(networkError) {
asyncStorage.getItem(prefName + "_cache").then(function (json) {
deferred.resolve(json);
}).catch(function (e) {
deferred.reject("JSON not available, CDN error: " + networkError +
", storage error: " + e);
});
}
xhr.onload = () => {
let json;
try {
json = JSON.parse(xhr.responseText);
let json = JSON.parse(xhr.responseText);
asyncStorage.setItem(prefName + "_cache", json).catch(function (e) {
// Could not update cache, let's log the error but continue
console.error(e);
});
deferred.resolve(json);
} catch (e) {
return deferred.reject("Invalid JSON");
readFromStorage(e);
}
Services.prefs.setCharPref(prefName + "_cache", xhr.responseText);
return deferred.resolve(json);
};
xhr.onerror = (e) => {
deferred.reject("Network error");
readFromStorage(e);
};
xhr.open("get", Services.prefs.getCharPref(prefName));

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

@ -114,6 +114,7 @@ var SimulatorEditor = {
// Update the form fields.
this._form.name.value = simulator.name;
this.updateVersionSelector();
this.updateProfileSelector();
this.updateDeviceSelector();
@ -123,6 +124,11 @@ var SimulatorEditor = {
let tvSimMenu = document.querySelector("#tv_simulator_menu");
tvSimMenu.style.visibility = (this._simulator.type === "television") ?
"visible" : "hidden";
// Trigger any listener waiting for this update
let change = document.createEvent("HTMLEvents");
change.initEvent("change", true, true);
this._form.dispatchEvent(change);
});
},

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

@ -47,9 +47,9 @@ const MS_PER_DAY = 86400000;
});
// Download remote resources early
getJSON("devtools.webide.addonsURL", true);
getJSON("devtools.webide.templatesURL", true);
getJSON("devtools.devices.url", true);
getJSON("devtools.webide.addonsURL");
getJSON("devtools.webide.templatesURL");
getJSON("devtools.devices.url");
// See bug 989619
console.log = console.log.bind(console);

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

@ -56,7 +56,7 @@ var GetAvailableAddons = exports.GetAvailableAddons = function () {
simulators: [],
adb: null
};
getJSON(ADDONS_URL, true).then(json => {
getJSON(ADDONS_URL).then(json => {
for (let stability in json) {
for (let version of json[stability]) {
addons.simulators.push(new SimulatorAddon(stability, version));

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

@ -148,6 +148,13 @@
let doc = simulatorPanel.contentWindow.document;
let form = doc.querySelector("#simulator-editor");
let formReady = new Promise((resolve, reject) => {
form.addEventListener("change", () => {
resolve();
});
});
let change = doc.createEvent("HTMLEvents");
change.initEvent("change", true, true);
@ -160,6 +167,8 @@
let MockFilePicker = SpecialPowers.MockFilePicker;
MockFilePicker.init(simulatorPanel.contentWindow);
yield formReady;
// Test `name`.
is(form.name.value, find(".runtime-panel-item-simulator").textContent, "Original simulator name");