Bug 849616 - Use ObjectWrapper.jsm instead of __exposedProps__ in Settings API. r=gwagner
authorReuben Morais <reuben.morais@gmail.com>
Mon, 11 Mar 2013 22:31:19 -0700
changeset 124774 8cbb58a451d9db220e0bbb6b086b68fc0d0ba9e6
parent 124773 adc0079eeb545d0874df991463a3165047a6baee
child 124775 3a2a6239763e3c4f7ae4be61b5d86506dca03717
push id24433
push useremorley@mozilla.com
push dateThu, 14 Mar 2013 12:21:10 +0000
treeherdermozilla-central@96af92fa87fd [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgwagner
bugs849616
milestone22.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 849616 - Use ObjectWrapper.jsm instead of __exposedProps__ in Settings API. r=gwagner
dom/settings/SettingsManager.js
dom/settings/tests/test_settings_basics.html
--- a/dom/settings/SettingsManager.js
+++ b/dom/settings/SettingsManager.js
@@ -12,16 +12,17 @@ function debug(s) {
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 const Cu = Components.utils;
 
 Cu.import("resource://gre/modules/SettingsQueue.jsm");
 Cu.import("resource://gre/modules/SettingsDB.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
+Cu.import("resource://gre/modules/ObjectWrapper.jsm")
 
 XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
                                    "@mozilla.org/childprocessmessagemanager;1",
                                    "nsIMessageSender");
 
 const nsIClassInfo            = Ci.nsIClassInfo;
 const SETTINGSLOCK_CONTRACTID = "@mozilla.org/settingsLock;1";
 const SETTINGSLOCK_CID        = Components.ID("{60c9357c-3ae0-4222-8f55-da01428470d5}");
@@ -37,16 +38,20 @@ function SettingsLock(aSettingsManager)
 }
 
 SettingsLock.prototype = {
 
   get closed() {
     return !this._open;
   },
 
+  _wrap: function _wrap(obj) {
+    return ObjectWrapper.wrap(obj, this._settingsManager._window);
+  },
+
   process: function process() {
     let lock = this;
     lock._open = false;
     let store = lock._transaction.objectStore(SETTINGSSTORE_NAME);
 
     while (!lock._requests.isEmpty()) {
       let info = lock._requests.dequeue();
       if (DEBUG) debug("info: " + info.intent);
@@ -122,45 +127,35 @@ SettingsLock.prototype = {
             };
           }
           break;
         case "get":
           let getReq = (info.name === "*") ? store.mozGetAll()
                                            : store.mozGetAll(info.name);
 
           getReq.onsuccess = function(event) {
-            if (DEBUG) debug("Request for '" + info.name + "' successful. " + 
+            if (DEBUG) debug("Request for '" + info.name + "' successful. " +
                   "Record count: " + event.target.result.length);
 
             if (event.target.result.length == 0) {
               if (DEBUG) debug("MOZSETTINGS-GET-WARNING: " + info.name + " is not in the database.\n");
             }
 
-            let results = {
-              __exposedProps__: {
-              }
-            };
+            let results = {};
 
             for (var i in event.target.result) {
               let result = event.target.result[i];
               var name = result.settingName;
               if (DEBUG) debug("VAL: " + result.userValue +", " + result.defaultValue + "\n");
               var value = result.userValue !== undefined ? result.userValue : result.defaultValue;
-              results[name] = value;
-              results.__exposedProps__[name] = "r";
-              // If the value itself is an object, expose the properties.
-              if (typeof value == "object" && value != null) {
-                var exposed = {};
-                Object.keys(value).forEach(function(key) { exposed[key] = 'r'; });
-                results[name].__exposedProps__ = exposed;
-              }
+              results[name] = this._wrap(value);
             }
 
             this._open = true;
-            Services.DOMRequest.fireSuccess(request, results);
+            Services.DOMRequest.fireSuccess(request, this._wrap(results));
             this._open = false;
           }.bind(lock);
 
           getReq.onerror = function() {
             Services.DOMRequest.fireError(request, 0)
           };
           break;
       }
@@ -267,16 +262,20 @@ function SettingsManager()
   this._settingsDB = new SettingsDB();
   this._settingsDB.init(myGlobal);
 }
 
 SettingsManager.prototype = {
   _onsettingchange: null,
   _callbacks: null,
 
+  _wrap: function _wrap(obj) {
+    return ObjectWrapper.wrap(obj, this._window);
+  },
+
   nextTick: function nextTick(aCallback, thisObj) {
     if (thisObj)
       aCallback = aCallback.bind(thisObj);
 
     Services.tm.currentThread.dispatch(aCallback, Ci.nsIThread.DISPATCH_NORMAL);
   },
 
   set onsettingchange(aCallback) {
@@ -321,25 +320,24 @@ SettingsManager.prototype = {
               settingName: msg.key,
               settingValue: msg.value
             });
             this._onsettingchange.handleEvent(event);
           }
           if (this._callbacks && this._callbacks[msg.key]) {
             if (DEBUG) debug("observe callback called! " + msg.key + " " + this._callbacks[msg.key].length);
             this._callbacks[msg.key].forEach(function(cb) {
-              cb({settingName: msg.key, settingValue: msg.value,
-                  __exposedProps__: {settingName: 'r', settingValue: 'r'}});
-            });
+              cb(this._wrap({settingName: msg.key, settingValue: msg.value}));
+            }.bind(this));
           }
         } else {
           if (DEBUG) debug("no observers stored!");
         }
         break;
-      default: 
+      default:
         if (DEBUG) debug("Wrong message: " + aMessage.name);
     }
   },
 
   addObserver: function addObserver(aName, aCallback) {
     if (DEBUG) debug("addObserver " + aName);
     if (!this._callbacks) {
       cpmm.sendAsyncMessage("Settings:RegisterForMessages");
--- a/dom/settings/tests/test_settings_basics.html
+++ b/dom/settings/tests/test_settings_basics.html
@@ -303,17 +303,17 @@ var steps = [
     };
     req.onerror = onFailure;
 
     req2 = lock.get("screen.brightness");
     req2.onsuccess = function () {
       is(req2.result["screen.brightness"], 0.7, "same Value");
     }
     req2.onerror = onFailure;
-    
+
     var lock2 = mozSettings.createLock();
     req5 = lock2.get("screen.brightness");
     req5.onsuccess = function () {
       is(req5.result["screen.brightness"], 1.7, "same Value");
       next();
     }
     req5.onerror = onFailure;
   },
@@ -460,17 +460,17 @@ var steps = [
       var testObj = {};
       testObj["wifi.enabled" + i] = false;
       req5.onsuccess = function () {
         check(this.request.result, this.testObj);
         ok(true, "Get2 Done");
       }.bind({testObj: testObj, request: req5});
       req5.onerror = onFailure;
     }
-    
+
     var lock5 = mozSettings.createLock();
     req6 = lock5.clear();
     req6.onsuccess = function () {
       ok(true, "Deleted the database");
       next();
     };
     req6.onerror = onFailure;
   },
@@ -490,37 +490,37 @@ var steps = [
     req2.onsuccess = function () {
       ok(true, "Set Done");
     };
     req2.onerror = onFailure;
   },
   function () {
     ok(true, "Test locking result");
     var lock = mozSettings.createLock();
-    
+
     req = lock.get("wifi.enabled");
     req.onsuccess = function() {
       check(req.result, wifiEnabled);
       ok(true, "Test2 locking result done");
     }
     req.onerror = onFailure;
-    
+
     req2 = lock.clear();
     req2.onsuccess = function () {
       ok(true, "Deleted the database");
     };
     req2.onerror = onFailure;
 
     req3 = lock.set(wifi);
     req3.onsuccess = function () {
       ok(true, "set done");
       next();
     }
     req3.onerror = onFailure;
-    
+
   },
   function () {
     ok(true, "Get all settings");
     var lock = mozSettings.createLock();
     req = lock.get("*");
     req.onsuccess = function () {
       is(Object.keys(req.result).length, 1, "length 1");
       check(wifi, req.result);
@@ -729,16 +729,42 @@ var steps = [
     var lock = mozSettings.createLock();
     req = lock.clear();
     req.onsuccess = function () {
       ok(true, "Deleted the database");
       next();
     };
     req.onerror = onFailure;
   },
+  function() {
+    ok(true, "Set object value");
+    var lock = mozSettings.createLock();
+    req = lock.set({"setting-obj": {foo: {bar: 23}}});
+    req.onsuccess = function() {
+      req2 = lock.get("setting-obj");
+      req2.onsuccess = function(event) {
+        var result = event.target.result["setting-obj"];
+        ok(result, "Got valid result");
+        ok(typeof result == "object", "Result is object");
+        ok("foo" in result && "bar" in result.foo, "Result has properties");
+        ok(result.foo.bar == 23, "Result properties are set");
+        next();
+      };
+    };
+  },
+  function() {
+    ok(true, "Clear DB");
+    var lock = mozSettings.createLock();
+    req = lock.clear();
+    req.onsuccess = function () {
+      ok(true, "Deleted the database");
+      next();
+    };
+    req.onerror = onFailure;
+  },
   function () {
     ok(true, "all done!\n");
     SimpleTest.finish();
   }
 ];
 
 function next() {
   ok(true, "Begin!");