Bug 1482054 - Create module to manage network locations;r=daisuke
authorJulian Descottes <jdescottes@mozilla.com>
Fri, 10 Aug 2018 09:38:55 +0200
changeset 828754 2d6272c35ae86e92d8aa5a8fe04db77e5f16291c
parent 828753 ec43c1941881c67e62b45ac5773b4e2929794bef
child 828755 50bbb02dc5c9eb264ec6221c67c4c19a9f5b67df
push id118704
push userbmo:rcaliman@mozilla.com
push dateMon, 13 Aug 2018 16:58:07 +0000
reviewersdaisuke
bugs1482054
milestone63.0a1
Bug 1482054 - Create module to manage network locations;r=daisuke This patch introduces a new preference: devtools.aboutdebugging.network-locations - this preference is a string pref, that should be a stringified JavaScript array. - the default value is "[]". - the module supports getting, adding, removing and observing network locations. Differential Revision: https://phabricator.services.mozilla.com/D3096
devtools/client/aboutdebugging-new/src/modules/moz.build
devtools/client/aboutdebugging-new/src/modules/network-locations.js
devtools/client/aboutdebugging-new/src/moz.build
devtools/client/preferences/devtools-client.js
new file mode 100644
--- /dev/null
+++ b/devtools/client/aboutdebugging-new/src/modules/moz.build
@@ -0,0 +1,7 @@
+# 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/.
+
+DevToolsModules(
+    'network-locations.js',
+)
new file mode 100644
--- /dev/null
+++ b/devtools/client/aboutdebugging-new/src/modules/network-locations.js
@@ -0,0 +1,62 @@
+/* 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 Services = require("Services");
+
+const NETWORK_LOCATIONS_PREF = "devtools.aboutdebugging.network-locations";
+
+/**
+ * This module provides a collection of helper methods to read and update network
+ * locations monitored by about-debugging.
+ */
+
+function addNetworkLocationsObserver(listener) {
+  Services.prefs.addObserver(NETWORK_LOCATIONS_PREF, listener);
+}
+exports.addNetworkLocationsObserver = addNetworkLocationsObserver;
+
+/**
+ * Read the current preference value for aboutdebugging network locations.
+ * Will throw if the value cannot be parsed or is not an array.
+ */
+function _parsePreferenceAsArray() {
+  const pref = Services.prefs.getStringPref(NETWORK_LOCATIONS_PREF, "[]");
+  const parsedValue = JSON.parse(pref);
+  if (!Array.isArray(parsedValue)) {
+    throw new Error("Expected array value in " + NETWORK_LOCATIONS_PREF);
+  }
+  return parsedValue;
+}
+
+function getNetworkLocations() {
+  try {
+    return _parsePreferenceAsArray();
+  } catch (e) {
+    Services.prefs.clearUserPref(NETWORK_LOCATIONS_PREF);
+    return [];
+  }
+}
+exports.getNetworkLocations = getNetworkLocations;
+
+function addNetworkLocation(location) {
+  const locations = getNetworkLocations();
+  const locationsSet = new Set(locations);
+  locationsSet.add(location);
+
+  Services.prefs.setStringPref(NETWORK_LOCATIONS_PREF,
+    JSON.stringify([...locationsSet]));
+}
+exports.addNetworkLocation = addNetworkLocation;
+
+function removeNetworkLocation(location) {
+  const locations = getNetworkLocations();
+  const locationsSet = new Set(locations);
+  locationsSet.delete(location);
+
+  Services.prefs.setStringPref(NETWORK_LOCATIONS_PREF,
+    JSON.stringify([...locationsSet]));
+}
+exports.removeNetworkLocation = removeNetworkLocation;
--- a/devtools/client/aboutdebugging-new/src/moz.build
+++ b/devtools/client/aboutdebugging-new/src/moz.build
@@ -1,15 +1,16 @@
 # 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/.
 
 DIRS += [
     'actions',
     'components',
     'middleware',
+    'modules',
     'reducers',
 ]
 
 DevToolsModules(
     'constants.js',
     'create-store.js',
 )
--- a/devtools/client/preferences/devtools-client.js
+++ b/devtools/client/preferences/devtools-client.js
@@ -314,16 +314,17 @@ pref("devtools.editor.autocomplete", tru
 pref("devtools.responsive.reloadConditions.touchSimulation", false);
 // Whether to reload when user agent is changed
 pref("devtools.responsive.reloadConditions.userAgent", false);
 // Whether to show the notification about reloading to apply emulation
 pref("devtools.responsive.reloadNotification.enabled", true);
 
 // Enable new about:debugging.
 pref("devtools.aboutdebugging.new-enabled", false);
+pref("devtools.aboutdebugging.network-locations", "[]");
 
 // about:debugging: only show system add-ons in local builds by default.
 #ifdef MOZILLA_OFFICIAL
   pref("devtools.aboutdebugging.showSystemAddons", false);
 #else
   pref("devtools.aboutdebugging.showSystemAddons", true);
 #endif