Bug 984464 - Create mozIApplication objects directly with the mozIApplication constructor (without mutating the prototype). r=fabrice
authorMarco Castelluccio <mar.castelluccio@studenti.unina.it>
Mon, 21 Apr 2014 11:30:15 -0400
changeset 179400 aaf8037b9e0f2ffdec6ae5ecbbe49be06cdc7ca0
parent 179399 d525f195556dedaaa0572e84995047def5461cbf
child 179401 dcc3ce7fcd23ffb3c9d281edd91a7c7939191284
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersfabrice
bugs984464
milestone31.0a1
Bug 984464 - Create mozIApplication objects directly with the mozIApplication constructor (without mutating the prototype). r=fabrice
dom/apps/src/AppsServiceChild.jsm
dom/apps/src/AppsUtils.jsm
--- a/dom/apps/src/AppsServiceChild.jsm
+++ b/dom/apps/src/AppsServiceChild.jsm
@@ -91,17 +91,17 @@ this.DOMApplicationRegistry = {
   getAppByLocalId: function getAppByLocalId(aLocalId) {
     debug("getAppByLocalId " + aLocalId);
     let app = this.localIdIndex[aLocalId];
     if (!app) {
       debug("Ouch, No app!");
       return null;
     }
 
-    return AppsUtils.cloneAsMozIApplication(app);
+    return new mozIApplication(app);
   },
 
   getManifestURLByLocalId: function getManifestURLByLocalId(aLocalId) {
     debug("getManifestURLByLocalId " + aLocalId);
     return AppsUtils.getManifestURLByLocalId(this.webapps, aLocalId);
   },
 
   getCoreAppsBasePath: function getCoreAppsBasePath() {
--- a/dom/apps/src/AppsUtils.jsm
+++ b/dom/apps/src/AppsUtils.jsm
@@ -21,30 +21,31 @@ XPCOMUtils.defineLazyModuleGetter(this, 
 XPCOMUtils.defineLazyModuleGetter(this, "WebappOSUtils",
   "resource://gre/modules/WebappOSUtils.jsm");
 
 XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
   "resource://gre/modules/NetUtil.jsm");
 
 // Shared code for AppsServiceChild.jsm, Webapps.jsm and Webapps.js
 
-this.EXPORTED_SYMBOLS = ["AppsUtils", "ManifestHelper", "isAbsoluteURI"];
+this.EXPORTED_SYMBOLS = ["AppsUtils", "ManifestHelper", "isAbsoluteURI", "mozIApplication"];
 
 function debug(s) {
   //dump("-*- AppsUtils.jsm: " + s + "\n");
 }
 
 this.isAbsoluteURI = function(aURI) {
   let foo = Services.io.newURI("http://foo", null, null);
   let bar = Services.io.newURI("http://bar", null, null);
   return Services.io.newURI(aURI, null, foo).prePath != foo.prePath ||
          Services.io.newURI(aURI, null, bar).prePath != bar.prePath;
 }
 
-function mozIApplication() {
+this.mozIApplication = function(aApp) {
+  _setAppProperties(this, aApp);
 }
 
 mozIApplication.prototype = {
   hasPermission: function(aPermission) {
     let uri = Services.io.newURI(this.origin, null, null);
     let secMan = Cc["@mozilla.org/scriptsecuritymanager;1"]
                    .getService(Ci.nsIScriptSecurityManager);
     // This helper checks an URI inside |aApp|'s origin and part of |aApp| has a
@@ -60,72 +61,70 @@ mozIApplication.prototype = {
   QueryInterface: function(aIID) {
     if (aIID.equals(Ci.mozIApplication) ||
         aIID.equals(Ci.nsISupports))
       return this;
     throw Cr.NS_ERROR_NO_INTERFACE;
   }
 }
 
+function _setAppProperties(aObj, aApp) {
+  aObj.name = aApp.name;
+  aObj.csp = aApp.csp;
+  aObj.installOrigin = aApp.installOrigin;
+  aObj.origin = aApp.origin;
+#ifdef MOZ_ANDROID_SYNTHAPKS
+  aObj.apkPackageName = aApp.apkPackageName;
+#endif
+  aObj.receipts = aApp.receipts ? JSON.parse(JSON.stringify(aApp.receipts)) : null;
+  aObj.installTime = aApp.installTime;
+  aObj.manifestURL = aApp.manifestURL;
+  aObj.appStatus = aApp.appStatus;
+  aObj.removable = aApp.removable;
+  aObj.id = aApp.id;
+  aObj.localId = aApp.localId;
+  aObj.basePath = aApp.basePath;
+  aObj.progress = aApp.progress || 0.0;
+  aObj.installState = aApp.installState || "installed";
+  aObj.downloadAvailable = aApp.downloadAvailable;
+  aObj.downloading = aApp.downloading;
+  aObj.readyToApplyDownload = aApp.readyToApplyDownload;
+  aObj.downloadSize = aApp.downloadSize || 0;
+  aObj.lastUpdateCheck = aApp.lastUpdateCheck;
+  aObj.updateTime = aApp.updateTime;
+  aObj.etag = aApp.etag;
+  aObj.packageEtag = aApp.packageEtag;
+  aObj.manifestHash = aApp.manifestHash;
+  aObj.packageHash = aApp.packageHash;
+  aObj.staged = aApp.staged;
+  aObj.installerAppId = aApp.installerAppId || Ci.nsIScriptSecurityManager.NO_APP_ID;
+  aObj.installerIsBrowser = !!aApp.installerIsBrowser;
+  aObj.storeId = aApp.storeId || "";
+  aObj.storeVersion = aApp.storeVersion || 0;
+  aObj.role = aApp.role || "";
+  aObj.redirects = aApp.redirects;
+}
+
 this.AppsUtils = {
   // Clones a app, without the manifest.
-  cloneAppObject: function cloneAppObject(aApp) {
-    return {
-      name: aApp.name,
-      csp: aApp.csp,
-      installOrigin: aApp.installOrigin,
-      origin: aApp.origin,
-#ifdef MOZ_ANDROID_SYNTHAPKS
-      apkPackageName: aApp.apkPackageName,
-#endif
-      receipts: aApp.receipts ? JSON.parse(JSON.stringify(aApp.receipts)) : null,
-      installTime: aApp.installTime,
-      manifestURL: aApp.manifestURL,
-      appStatus: aApp.appStatus,
-      removable: aApp.removable,
-      id: aApp.id,
-      localId: aApp.localId,
-      basePath: aApp.basePath,
-      progress: aApp.progress || 0.0,
-      installState: aApp.installState || "installed",
-      downloadAvailable: aApp.downloadAvailable,
-      downloading: aApp.downloading,
-      readyToApplyDownload: aApp.readyToApplyDownload,
-      downloadSize: aApp.downloadSize || 0,
-      lastUpdateCheck: aApp.lastUpdateCheck,
-      updateTime: aApp.updateTime,
-      etag: aApp.etag,
-      packageEtag: aApp.packageEtag,
-      manifestHash: aApp.manifestHash,
-      packageHash: aApp.packageHash,
-      staged: aApp.staged,
-      installerAppId: aApp.installerAppId || Ci.nsIScriptSecurityManager.NO_APP_ID,
-      installerIsBrowser: !!aApp.installerIsBrowser,
-      storeId: aApp.storeId || "",
-      storeVersion: aApp.storeVersion || 0,
-      role: aApp.role || "",
-      redirects: aApp.redirects
-    };
-  },
-
-  cloneAsMozIApplication: function cloneAsMozIApplication(aApp) {
-    let res = this.cloneAppObject(aApp);
-    res.__proto__ = mozIApplication.prototype;
-    return res;
+  cloneAppObject: function(aApp) {
+    let obj = {};
+    _setAppProperties(obj, aApp);
+    return obj;
   },
 
   getAppByManifestURL: function getAppByManifestURL(aApps, aManifestURL) {
     debug("getAppByManifestURL " + aManifestURL);
     // This could be O(1) if |webapps| was a dictionary indexed on manifestURL
     // which should be the unique app identifier.
     // It's currently O(n).
     for (let id in aApps) {
       let app = aApps[id];
       if (app.manifestURL == aManifestURL) {
-        return this.cloneAsMozIApplication(app);
+        return new mozIApplication(app);
       }
     }
 
     return null;
   },
 
   getAppLocalIdByManifestURL: function getAppLocalIdByManifestURL(aApps, aManifestURL) {
     debug("getAppLocalIdByManifestURL " + aManifestURL);
@@ -161,17 +160,17 @@ this.AppsUtils = {
     return "";
   },
 
   getAppByLocalId: function getAppByLocalId(aApps, aLocalId) {
     debug("getAppByLocalId " + aLocalId);
     for (let id in aApps) {
       let app = aApps[id];
       if (app.localId == aLocalId) {
-        return this.cloneAsMozIApplication(app);
+        return new mozIApplication(app);
       }
     }
 
     return null;
   },
 
   getManifestURLByLocalId: function getManifestURLByLocalId(aApps, aLocalId) {
     debug("getManifestURLByLocalId " + aLocalId);