Bug 801355 - Race condition setting up permissions at startup [r=gwagner]
authorFabrice Desré <fabrice@mozilla.com>
Tue, 23 Oct 2012 13:11:02 -0700
changeset 111178 fb3f0836b8346bf55b652b0d60cddde387435285
parent 111177 38ae29192358f0351ef994d066d03b41e793081c
child 111179 4b997a0e6b5fbe7fb76fc642b7776c6752139f15
push id16916
push userfdesre@mozilla.com
push dateTue, 23 Oct 2012 20:11:14 +0000
treeherdermozilla-inbound@fb3f0836b834 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgwagner
bugs801355
milestone19.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 801355 - Race condition setting up permissions at startup [r=gwagner]
b2g/chrome/content/shell.js
dom/apps/src/PermissionsInstaller.jsm
dom/permission/PermissionSettings.js
dom/permission/PermissionSettings.jsm
--- a/b2g/chrome/content/shell.js
+++ b/b2g/chrome/content/shell.js
@@ -14,17 +14,16 @@ Cu.import('resource://gre/modules/Servic
 Cu.import('resource://gre/modules/ContactService.jsm');
 Cu.import('resource://gre/modules/SettingsChangeNotifier.jsm');
 #ifdef MOZ_B2G_FM
 Cu.import('resource://gre/modules/DOMFMRadioParent.jsm');
 #endif
 Cu.import('resource://gre/modules/AlarmService.jsm');
 Cu.import('resource://gre/modules/ActivitiesService.jsm');
 Cu.import('resource://gre/modules/PermissionPromptHelper.jsm');
-Cu.import('resource://gre/modules/PermissionSettings.jsm');
 Cu.import('resource://gre/modules/ObjectWrapper.jsm');
 Cu.import('resource://gre/modules/accessibility/AccessFu.jsm');
 Cu.import('resource://gre/modules/Payment.jsm');
 Cu.import("resource://gre/modules/AppsUtils.jsm");
 Cu.import('resource://gre/modules/UserAgentOverrides.jsm');
 #ifdef MOZ_B2G_RIL
 Cu.import('resource://gre/modules/NetworkStatsService.jsm');
 #endif
--- a/dom/apps/src/PermissionsInstaller.jsm
+++ b/dom/apps/src/PermissionsInstaller.jsm
@@ -4,42 +4,33 @@
 
 "use strict";
 
 const Ci = Components.interfaces;
 const Cu = Components.utils;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/AppsUtils.jsm");
+Cu.import("resource://gre/modules/PermissionSettings.jsm");
 
 var EXPORTED_SYMBOLS = ["PermissionsInstaller"];
 
 const UNKNOWN_ACTION = Ci.nsIPermissionManager.UNKNOWN_ACTION;
 const ALLOW_ACTION = Ci.nsIPermissionManager.ALLOW_ACTION;
 const DENY_ACTION = Ci.nsIPermissionManager.DENY_ACTION;
 const PROMPT_ACTION = Ci.nsIPermissionManager.PROMPT_ACTION;
 
 // Permission access flags
 const READONLY = "readonly";
 const CREATEONLY = "createonly";
 const READCREATE = "readcreate";
 const READWRITE = "readwrite";
 
 const PERM_TO_STRING = ["unknown", "allow", "deny", "prompt"];
 
-XPCOMUtils.defineLazyServiceGetter(this,
-                                   "PermSettings",
-                                   "@mozilla.org/permissionSettings;1",
-                                   "nsIDOMPermissionSettings");
-
-XPCOMUtils.defineLazyServiceGetter(this,
-                                   "permissionManager",
-                                   "@mozilla.org/permissionmanager;1",
-                                   "nsIPermissionManager");
-
 function debug(aMsg) {
   //dump("-*-*- PermissionsInstaller.jsm : " + aMsg + "\n");
 }
 
 /**
  * Converts ['read', 'write'] to ['contacts-read', 'contacts-write'], etc...
  * @param string aPermName
  * @param Array aSuffixes
@@ -339,17 +330,17 @@ let PermissionsInstaller = {
                                            newManifest.permissions[perm].access);
             newPerms = newPerms.concat(_perms);
           }
 
           for (let idx in AllPossiblePermissions) {
             let index = newPerms.indexOf(AllPossiblePermissions[idx]);
             if (index == -1) {
               // See if the permission was installed previously
-              let _perm = PermSettings.get(AllPossiblePermissions[idx],
+              let _perm = PermissionSettingsModule.getPermission(AllPossiblePermissions[idx],
                                            aApp.manifestURL,
                                            aApp.origin,
                                            false);
               if (_perm == "unknown" || _perm == "deny") {
                 // All 'deny' permissions should be preserved
                 continue;
               }
               // Remove the deprecated permission
@@ -410,19 +401,31 @@ let PermissionsInstaller = {
    *        The permission value.
    * @param object aApp
    *        The just-installed app configuration.
             The properties used are manifestURL, origin and manifest.
    * @returns void
    **/
   _setPermission: function setPermission(aPerm, aValue, aApp) {
     if (aPerm != "storage") {
-      PermSettings.set(aPerm, aValue, aApp.manifestURL, aApp.origin, false);
+      PermissionSettingsModule.addPermission({
+        type: aPerm,
+        origin: aApp.origin,
+        manifestURL: aApp.manifestURL,
+        value: aValue,
+        browserFlag: false
+      });
       return;
     }
 
     ["indexedDB-unlimited", "offline-app", "pin-app"].forEach(
       function(aName) {
-        PermSettings.set(aName, aValue, aApp.manifestURL, aApp.origin, false);
+        PermissionSettingsModule.addPermission({
+          type: aName,
+          origin: aApp.origin,
+          manifestURL: aApp.manifestURL,
+          value: aValue,
+          browserFlag: false
+        });
       }
     );
   }
 }
--- a/dom/permission/PermissionSettings.js
+++ b/dom/permission/PermissionSettings.js
@@ -1,16 +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/. */
 
 "use strict";
 
 function debug(aMsg) {
-  // dump("-*- PermissionSettings.js: " + aMsg + "\n");
+  //dump("-*- PermissionSettings.js: " + aMsg + "\n");
 }
 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 const Cu = Components.utils;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
@@ -23,19 +23,30 @@ const PERMISSIONSETTINGS_CONTRACTID = "@
 const PERMISSIONSETTINGS_CID        = Components.ID("{18390770-02ab-11e2-a21f-0800200c9a66}");
 const nsIDOMPermissionSettings      = Ci.nsIDOMPermissionSettings;
 
 function PermissionSettings()
 {
   debug("Constructor");
 }
 
-var permissionManager = Cc["@mozilla.org/permissionmanager;1"].getService(Ci.nsIPermissionManager);
-var secMan = Cc["@mozilla.org/scriptsecuritymanager;1"].getService(Ci.nsIScriptSecurityManager);
-var appsService = Cc["@mozilla.org/AppsService;1"].getService(Ci.nsIAppsService);
+XPCOMUtils.defineLazyServiceGetter(this,
+                                   "permissionManager",
+                                   "@mozilla.org/permissionmanager;1",
+                                   "nsIPermissionManager");
+
+XPCOMUtils.defineLazyServiceGetter(this,
+                                   "secMan",
+                                   "@mozilla.org/scriptsecuritymanager;1",
+                                   "nsIScriptSecurityManager");
+
+XPCOMUtils.defineLazyServiceGetter(this,
+                                   "appsService",
+                                   "@mozilla.org/AppsService;1",
+                                   "nsIAppsService");
 
 PermissionSettings.prototype = {
   get: function get(aPermission, aManifestURL, aOrigin, aBrowserFlag) {
     debug("Get called with: " + aPermission + ", " + aManifestURL + ", " + aOrigin + ", " + aBrowserFlag);
     let uri = Services.io.newURI(aOrigin, null, null);
     let appID = appsService.getAppLocalIdByManifestURL(aManifestURL);
     let principal = secMan.getAppCodebasePrincipal(uri, appID, aBrowserFlag);
     let result = permissionManager.testExactPermissionFromPrincipal(principal, aPermission);
--- a/dom/permission/PermissionSettings.jsm
+++ b/dom/permission/PermissionSettings.jsm
@@ -1,36 +1,45 @@
 /* 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";
 
-let DEBUG = 0;
-if (DEBUG)
-  debug = function (s) { dump("-*- PermissionSettings Module: " + s + "\n"); }
-else
-  debug = function (s) {}
+function debug(s) {
+  //dump("-*- PermissionSettings Module: " + s + "\n");
+}
 
 const Cu = Components.utils;
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 
 let EXPORTED_SYMBOLS = ["PermissionSettingsModule"];
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 
 XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
                                    "@mozilla.org/parentprocessmessagemanager;1",
                                    "nsIMessageListenerManager");
 
-var permissionManager = Cc["@mozilla.org/permissionmanager;1"].getService(Ci.nsIPermissionManager);
-var secMan = Cc["@mozilla.org/scriptsecuritymanager;1"].getService(Ci.nsIScriptSecurityManager);
-var appsService = Cc["@mozilla.org/AppsService;1"].getService(Ci.nsIAppsService);
+XPCOMUtils.defineLazyServiceGetter(this,
+                                   "permissionManager",
+                                   "@mozilla.org/permissionmanager;1",
+                                   "nsIPermissionManager");
+
+XPCOMUtils.defineLazyServiceGetter(this,
+                                   "secMan",
+                                   "@mozilla.org/scriptsecuritymanager;1",
+                                   "nsIScriptSecurityManager");
+
+XPCOMUtils.defineLazyServiceGetter(this,
+                                   "appsService",
+                                   "@mozilla.org/AppsService;1",
+                                   "nsIAppsService");
 
 let PermissionSettingsModule = {
   init: function() {
     debug("Init");
     ppmm.addMessageListener("PermissionSettings:AddPermission", this);
     Services.obs.addObserver(this, "profile-before-change", false);
   },
 
@@ -57,16 +66,39 @@ let PermissionSettingsModule = {
       default:
         dump("Unsupported PermisionSettings Action: " + aData.value +"\n");
         action = Ci.nsIPermissionManager.UNKNOWN_ACTION;
     }
     debug("add: " + aData.origin + " " + appID + " " + action);
     permissionManager.addFromPrincipal(principal, aData.type, action);
   },
 
+  getPermission: function getPermission(aPermission, aManifestURL, aOrigin, aBrowserFlag) {
+    debug("getPermission: " + aPermission + ", " + aManifestURL + ", " + aOrigin);
+    let uri = Services.io.newURI(aOrigin, null, null);
+    let appID = appsService.getAppLocalIdByManifestURL(aManifestURL);
+    let principal = secMan.getAppCodebasePrincipal(uri, appID, aBrowserFlag);
+    let result = permissionManager.testExactPermissionFromPrincipal(principal, aPermission);
+
+    switch (result)
+    {
+      case Ci.nsIPermissionManager.UNKNOWN_ACTION:
+        return "unknown";
+      case Ci.nsIPermissionManager.ALLOW_ACTION:
+        return "allow";
+      case Ci.nsIPermissionManager.DENY_ACTION:
+        return "deny";
+      case Ci.nsIPermissionManager.PROMPT_ACTION:
+        return "prompt";
+      default:
+        dump("Unsupported PermissionSettings Action!\n");
+        return "unknown";
+    }
+  },
+
   observe: function(aSubject, aTopic, aData) {
     ppmm.removeMessageListener("PermissionSettings:AddPermission", this);
     Services.obs.removeObserver(this, "profile-before-change");
     ppmm = null;
   },
 
   receiveMessage: function(aMessage) {
     debug("PermissionSettings::receiveMessage " + aMessage.name);