Bug 1543122 - Allow passing a static list of preference values to UAWidgets upon construction. r=bgrins
authorMike Conley <mconley@mozilla.com>
Mon, 15 Apr 2019 01:06:54 +0000
changeset 469455 3619626a7662df350d6136343ad86c3799e3ab0d
parent 469454 815543d81a1d2ab79ed4e27612dba769e4b697a8
child 469456 22f9a15468c4017eeae659c4f00eea7053a490f7
push id112792
push userncsoregi@mozilla.com
push dateMon, 15 Apr 2019 09:49:11 +0000
treeherdermozilla-inbound@a57f27d3ccd0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbgrins
bugs1543122
milestone68.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 1543122 - Allow passing a static list of preference values to UAWidgets upon construction. r=bgrins Differential Revision: https://phabricator.services.mozilla.com/D26775
toolkit/actors/UAWidgetsChild.jsm
--- a/toolkit/actors/UAWidgetsChild.jsm
+++ b/toolkit/actors/UAWidgetsChild.jsm
@@ -9,16 +9,17 @@ var EXPORTED_SYMBOLS = ["UAWidgetsChild"
 const {ActorChild} = ChromeUtils.import("resource://gre/modules/ActorChild.jsm");
 const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
 
 class UAWidgetsChild extends ActorChild {
   constructor(dispatcher) {
     super(dispatcher);
 
     this.widgets = new WeakMap();
+    this.prefsCache = new Map();
   }
 
   handleEvent(aEvent) {
     switch (aEvent.type) {
       case "UAWidgetSetupOrChange":
         this.setupOrNotifyWidget(aEvent.target);
         break;
       case "UAWidgetTeardown":
@@ -44,16 +45,17 @@ class UAWidgetsChild extends ActorChild 
         Cu.reportError(ex);
       }
     }
   }
 
   setupWidget(aElement) {
     let uri;
     let widgetName;
+    let prefKeys = [];
     switch (aElement.localName) {
       case "video":
       case "audio":
         uri = "chrome://global/content/elements/videocontrols.js";
         widgetName = "VideoControlsWidget";
         break;
       case "input":
         uri = "chrome://global/content/elements/datetimebox.js";
@@ -84,17 +86,19 @@ class UAWidgetsChild extends ActorChild 
     let isSystemPrincipal = aElement.nodePrincipal.isSystemPrincipal;
     let sandbox = isSystemPrincipal ?
       Object.create(null) : Cu.getUAWidgetScope(aElement.nodePrincipal);
 
     if (!sandbox[widgetName]) {
       Services.scriptloader.loadSubScript(uri, sandbox);
     }
 
-    let widget = new sandbox[widgetName](shadowRoot);
+    let prefs = Cu.cloneInto(this.getPrefsForUAWidget(widgetName, prefKeys), sandbox);
+
+    let widget = new sandbox[widgetName](shadowRoot, prefs);
     if (!isSystemPrincipal) {
       widget = widget.wrappedJSObject;
     }
     this.widgets.set(aElement, widget);
     try {
       widget.onsetup();
     } catch (ex) {
       Cu.reportError(ex);
@@ -110,9 +114,37 @@ class UAWidgetsChild extends ActorChild 
       try {
         widget.destructor();
       } catch (ex) {
         Cu.reportError(ex);
       }
     }
     this.widgets.delete(aElement);
   }
+
+  getPrefsForUAWidget(aWidgetName, aPrefKeys) {
+    let result = this.prefsCache.get(aWidgetName);
+    if (result) {
+      return result;
+    }
+
+    result = {};
+    for (let key of aPrefKeys) {
+      switch (Services.prefs.getPrefType(key)) {
+        case Ci.nsIPrefBranch.PREF_BOOL: {
+          result[key] = Services.prefs.getBoolPref(key);
+          break;
+        }
+        case Ci.nsIPrefBranch.PREF_INT: {
+          result[key] = Services.prefs.getIntPref(key);
+          break;
+        }
+        case Ci.nsIPrefBranch.PREF_STRING: {
+          result[key] = Services.prefs.getStringPref(key);
+          break;
+        }
+      }
+    }
+
+    this.prefsCache.set(aWidgetName, result);
+    return result;
+  }
 }