Bug 1135018 - Move getjson from webide/ to shared/. r=jryans
authorJan Keromnes <janx@linux.com>
Sat, 21 Mar 2015 05:51:00 -0400
changeset 265599 3ec9f77647f6116c1720bb5f9367b36e7c61eab2
parent 265598 d462c69631569305ef3046275dc1d232cf04107f
child 265600 c521d136b0f3d0c1e401ed502de5a2be9756f2cb
push id830
push userraliiev@mozilla.com
push dateFri, 19 Jun 2015 19:24:37 +0000
treeherdermozilla-release@932614382a68 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjryans
bugs1135018
milestone39.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1135018 - Move getjson from webide/ to shared/. r=jryans
browser/devtools/shared/getjson.js
browser/devtools/shared/moz.build
browser/devtools/webide/content/newapp.js
browser/devtools/webide/content/webide.js
browser/devtools/webide/modules/addons.js
browser/devtools/webide/modules/remote-resources.js
browser/devtools/webide/moz.build
rename from browser/devtools/webide/modules/remote-resources.js
rename to browser/devtools/shared/getjson.js
--- a/browser/devtools/webide/modules/remote-resources.js
+++ b/browser/devtools/shared/getjson.js
@@ -1,54 +1,43 @@
 /* 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/. */
 
 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 = () => {
     let json;
     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);
   }
 
   xhr.onerror = (e) => {
     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");
-}
--- a/browser/devtools/shared/moz.build
+++ b/browser/devtools/shared/moz.build
@@ -46,16 +46,17 @@ EXTRA_JS_MODULES.devtools.shared.timelin
 ]
 
 EXTRA_JS_MODULES.devtools.shared += [
     'autocomplete-popup.js',
     'd3.js',
     'devices.js',
     'doorhanger.js',
     'frame-script-utils.js',
+    'getjson.js',
     'inplace-editor.js',
     'observable-object.js',
     'options-view.js',
     'telemetry.js',
     'theme-switching.js',
     'theme.js',
     'undo.js',
 ]
--- a/browser/devtools/webide/content/newapp.js
+++ b/browser/devtools/webide/content/newapp.js
@@ -10,36 +10,37 @@ Cu.import("resource://gre/modules/Servic
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 
 XPCOMUtils.defineLazyModuleGetter(this, "ZipUtils", "resource://gre/modules/ZipUtils.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "Downloads", "resource://gre/modules/Downloads.jsm");
 
 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;
 
 // See bug 989619
 console.log = console.log.bind(console);
 console.warn = console.warn.bind(console);
 console.error = console.error.bind(console);
 
 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");
     }
     if (list.length == 0) {
       throw new Error("JSON response is an empty array");
     }
     gTemplateList = list;
     let templatelistNode = document.querySelector("#templatelist");
--- a/browser/devtools/webide/content/webide.js
+++ b/browser/devtools/webide/content/webide.js
@@ -14,33 +14,34 @@ const {require} = devtools;
 const {Services} = Cu.import("resource://gre/modules/Services.jsm");
 const {AppProjects} = require("devtools/app-manager/app-projects");
 const {Connection} = require("devtools/client/connection-manager");
 const {AppManager} = require("devtools/webide/app-manager");
 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");
 const {showDoorhanger} = require("devtools/shared/doorhanger");
 const ProjectList = require("devtools/webide/project-list");
 
 const Strings = Services.strings.createBundle("chrome://browser/locale/devtools/webide.properties");
 
 const HTML = "http://www.w3.org/1999/xhtml";
 const HELP_URL = "https://developer.mozilla.org/docs/Tools/WebIDE/Troubleshooting";
 
 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);
 console.warn = console.warn.bind(console);
 console.error = console.error.bind(console);
 
 window.addEventListener("load", function onLoad() {
   window.removeEventListener("load", onLoad);
--- a/browser/devtools/webide/modules/addons.js
+++ b/browser/devtools/webide/modules/addons.js
@@ -1,18 +1,20 @@
 /* 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/. */
 
 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");
 let ADAPTERS_LINK = Services.prefs.getCharPref("devtools.webide.adaptersAddonURL");
 let SIMULATOR_ADDON_ID = Services.prefs.getCharPref("devtools.webide.simulatorAddonID");
 let ADB_ADDON_ID = Services.prefs.getCharPref("devtools.webide.adbAddonID");
 let ADAPTERS_ADDON_ID = Services.prefs.getCharPref("devtools.webide.adaptersAddonID");
 
@@ -49,17 +51,17 @@ let GetAvailableAddons_promise = null;
 let GetAvailableAddons = exports.GetAvailableAddons = function() {
   if (!GetAvailableAddons_promise) {
     let deferred = promise.defer();
     GetAvailableAddons_promise = deferred.promise;
     let addons = {
       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));
         }
       }
       addons.adb = new ADBAddon();
       addons.adapters = new AdaptersAddon();
       deferred.resolve(addons);
--- a/browser/devtools/webide/moz.build
+++ b/browser/devtools/webide/moz.build
@@ -20,17 +20,16 @@ MOCHITEST_CHROME_MANIFESTS += [
 ]
 
 EXTRA_JS_MODULES.devtools.webide += [
     'modules/addons.js',
     'modules/app-manager.js',
     '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',
     'modules/tab-store.js',
     'modules/utils.js'
 ]
 
 JS_PREFERENCE_FILES += [