From 059619bafc4b83cd3d46f2839ead32b231900a61 Mon Sep 17 00:00:00 2001 From: Jan Keromnes Date: Sat, 21 Mar 2015 05:51:00 -0400 Subject: [PATCH] Bug 1135018 - Move getjson from webide/ to shared/. r=jryans --HG-- rename : browser/devtools/webide/modules/remote-resources.js => browser/devtools/shared/getjson.js --- .../remote-resources.js => shared/getjson.js} | 25 ++++++------------- browser/devtools/shared/moz.build | 1 + browser/devtools/webide/content/newapp.js | 11 ++++---- browser/devtools/webide/content/webide.js | 7 +++--- browser/devtools/webide/modules/addons.js | 8 +++--- browser/devtools/webide/moz.build | 1 - 6 files changed, 23 insertions(+), 30 deletions(-) rename browser/devtools/{webide/modules/remote-resources.js => shared/getjson.js} (60%) diff --git a/browser/devtools/webide/modules/remote-resources.js b/browser/devtools/shared/getjson.js similarity index 60% rename from browser/devtools/webide/modules/remote-resources.js rename to browser/devtools/shared/getjson.js index 9a64a84fc321..e942b61d7a5b 100644 --- a/browser/devtools/webide/modules/remote-resources.js +++ b/browser/devtools/shared/getjson.js @@ -3,23 +3,22 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ const {Cu, CC} = require("chrome"); -const {Promise: promise} = Cu.import("resource://gre/modules/Promise.jsm", {}); +const promise = require("promise"); const {Services} = Cu.import("resource://gre/modules/Services.jsm"); const XMLHttpRequest = CC("@mozilla.org/xmlextras/xmlhttprequest;1"); -function getJSON(bypassCache, pref) { +// 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(pref + "_cache"); + 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 */} } - let deferred = promise.defer(); - let xhr = new XMLHttpRequest(); xhr.onload = () => { @@ -27,9 +26,9 @@ function getJSON(bypassCache, pref) { try { json = JSON.parse(xhr.responseText); } catch(e) { - return deferred.reject("Not valid JSON"); + return deferred.reject("Invalid JSON"); } - Services.prefs.setCharPref(pref + "_cache", xhr.responseText); + Services.prefs.setCharPref(prefName + "_cache", xhr.responseText); deferred.resolve(json); } @@ -37,18 +36,8 @@ function getJSON(bypassCache, pref) { deferred.reject("Network error"); } - xhr.open("get", Services.prefs.getCharPref(pref)); + xhr.open("get", Services.prefs.getCharPref(prefName)); xhr.send(); return deferred.promise; } - - - -exports.GetTemplatesJSON = function(bypassCache) { - return getJSON(bypassCache, "devtools.webide.templatesURL"); -} - -exports.GetAddonsJSON = function(bypassCache) { - return getJSON(bypassCache, "devtools.webide.addonsURL"); -} diff --git a/browser/devtools/shared/moz.build b/browser/devtools/shared/moz.build index 70ce730c7dae..31ff5d2e7e81 100644 --- a/browser/devtools/shared/moz.build +++ b/browser/devtools/shared/moz.build @@ -51,6 +51,7 @@ EXTRA_JS_MODULES.devtools.shared += [ 'devices.js', 'doorhanger.js', 'frame-script-utils.js', + 'getjson.js', 'inplace-editor.js', 'observable-object.js', 'options-view.js', diff --git a/browser/devtools/webide/content/newapp.js b/browser/devtools/webide/content/newapp.js index 31ab8802eeff..726c3048f43c 100644 --- a/browser/devtools/webide/content/newapp.js +++ b/browser/devtools/webide/content/newapp.js @@ -15,9 +15,10 @@ XPCOMUtils.defineLazyModuleGetter(this, "Downloads", "resource://gre/modules/Dow const {require} = Cu.import("resource://gre/modules/devtools/Loader.jsm", {}).devtools; const {FileUtils} = Cu.import("resource://gre/modules/FileUtils.jsm"); const {AppProjects} = require("devtools/app-manager/app-projects"); -const APP_CREATOR_LIST = "devtools.webide.templatesURL"; const {AppManager} = require("devtools/webide/app-manager"); -const {GetTemplatesJSON} = require("devtools/webide/remote-resources"); +const {getJSON} = require("devtools/shared/getjson"); + +const TEMPLATES_URL = "devtools.webide.templatesURL"; let gTemplateList = null; @@ -30,11 +31,11 @@ window.addEventListener("load", function onLoad() { window.removeEventListener("load", onLoad); let projectNameNode = document.querySelector("#project-name"); projectNameNode.addEventListener("input", canValidate, true); - getJSON(); + getTemplatesJSON(); }, true); -function getJSON() { - GetTemplatesJSON().then(list => { +function getTemplatesJSON() { + getJSON(TEMPLATES_URL).then(list => { if (!Array.isArray(list)) { throw new Error("JSON response not an array"); } diff --git a/browser/devtools/webide/content/webide.js b/browser/devtools/webide/content/webide.js index f31eafae0c58..cff6f8c929db 100644 --- a/browser/devtools/webide/content/webide.js +++ b/browser/devtools/webide/content/webide.js @@ -19,7 +19,7 @@ const {Promise: promise} = Cu.import("resource://gre/modules/Promise.jsm", {}); const ProjectEditor = require("projecteditor/projecteditor"); const {Devices} = Cu.import("resource://gre/modules/devtools/Devices.jsm"); const {GetAvailableAddons} = require("devtools/webide/addons"); -const {GetTemplatesJSON, GetAddonsJSON} = require("devtools/webide/remote-resources"); +const {getJSON} = require("devtools/shared/getjson"); const utils = require("devtools/webide/utils"); const Telemetry = require("devtools/shared/telemetry"); const {RuntimeScanners, WiFiScanner} = require("devtools/webide/runtimes"); @@ -34,8 +34,9 @@ const HELP_URL = "https://developer.mozilla.org/docs/Tools/WebIDE/Troubleshootin const MAX_ZOOM = 1.4; const MIN_ZOOM = 0.6; -// download template index early -GetTemplatesJSON(true); +// Download remote resources early +getJSON("devtools.webide.addonsURL", true); +getJSON("devtools.webide.templatesURL", true); // See bug 989619 console.log = console.log.bind(console); diff --git a/browser/devtools/webide/modules/addons.js b/browser/devtools/webide/modules/addons.js index d09a370caef1..9e9e2ce0f76c 100644 --- a/browser/devtools/webide/modules/addons.js +++ b/browser/devtools/webide/modules/addons.js @@ -5,9 +5,11 @@ const {Cu} = require("chrome"); const {Promise: promise} = Cu.import("resource://gre/modules/Promise.jsm"); const {AddonManager} = Cu.import("resource://gre/modules/AddonManager.jsm"); -const {EventEmitter} = Cu.import("resource://gre/modules/devtools/event-emitter.js"); const {Services} = Cu.import("resource://gre/modules/Services.jsm"); -const {GetAddonsJSON} = require("devtools/webide/remote-resources"); +const {getJSON} = require("devtools/shared/getjson"); +const EventEmitter = require("devtools/toolkit/event-emitter"); + +const ADDONS_URL = "devtools.webide.addonsURL"; let SIMULATOR_LINK = Services.prefs.getCharPref("devtools.webide.simulatorAddonsURL"); let ADB_LINK = Services.prefs.getCharPref("devtools.webide.adbAddonURL"); @@ -54,7 +56,7 @@ let GetAvailableAddons = exports.GetAvailableAddons = function() { simulators: [], adb: null } - GetAddonsJSON(true).then(json => { + getJSON(ADDONS_URL, true).then(json => { for (let stability in json) { for (let version of json[stability]) { addons.simulators.push(new SimulatorAddon(stability, version)); diff --git a/browser/devtools/webide/moz.build b/browser/devtools/webide/moz.build index 9f076ef463e6..a8ea7acbc7f5 100644 --- a/browser/devtools/webide/moz.build +++ b/browser/devtools/webide/moz.build @@ -25,7 +25,6 @@ EXTRA_JS_MODULES.devtools.webide += [ 'modules/build.js', 'modules/config-view.js', 'modules/project-list.js', - 'modules/remote-resources.js', 'modules/runtimes.js', 'modules/simulator-process.js', 'modules/simulators.js',