mobile/android/chrome/content/PermissionsHelper.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/chrome/content/PermissionsHelper.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,171 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +"use strict";
     1.8 +
     1.9 +var PermissionsHelper = {
    1.10 +  _permissonTypes: ["password", "geolocation", "popup", "indexedDB",
    1.11 +                    "offline-app", "desktop-notification", "plugins", "native-intent"],
    1.12 +  _permissionStrings: {
    1.13 +    "password": {
    1.14 +      label: "password.savePassword",
    1.15 +      allowed: "password.save",
    1.16 +      denied: "password.dontSave"
    1.17 +    },
    1.18 +    "geolocation": {
    1.19 +      label: "geolocation.shareLocation",
    1.20 +      allowed: "geolocation.allow",
    1.21 +      denied: "geolocation.dontAllow"
    1.22 +    },
    1.23 +    "popup": {
    1.24 +      label: "blockPopups.label",
    1.25 +      allowed: "popup.show",
    1.26 +      denied: "popup.dontShow"
    1.27 +    },
    1.28 +    "indexedDB": {
    1.29 +      label: "offlineApps.storeOfflineData",
    1.30 +      allowed: "offlineApps.allow",
    1.31 +      denied: "offlineApps.dontAllow2"
    1.32 +    },
    1.33 +    "offline-app": {
    1.34 +      label: "offlineApps.storeOfflineData",
    1.35 +      allowed: "offlineApps.allow",
    1.36 +      denied: "offlineApps.dontAllow2"
    1.37 +    },
    1.38 +    "desktop-notification": {
    1.39 +      label: "desktopNotification.useNotifications",
    1.40 +      allowed: "desktopNotification.allow",
    1.41 +      denied: "desktopNotification.dontAllow"
    1.42 +    },
    1.43 +    "plugins": {
    1.44 +      label: "clickToPlayPlugins.activatePlugins",
    1.45 +      allowed: "clickToPlayPlugins.activate",
    1.46 +      denied: "clickToPlayPlugins.dontActivate"
    1.47 +    },
    1.48 +    "native-intent": {
    1.49 +      label: "helperapps.openWithList2",
    1.50 +      allowed: "helperapps.always",
    1.51 +      denied: "helperapps.never"
    1.52 +    }
    1.53 +  },
    1.54 +
    1.55 +  observe: function observe(aSubject, aTopic, aData) {
    1.56 +    let uri = BrowserApp.selectedBrowser.currentURI;
    1.57 +
    1.58 +    switch (aTopic) {
    1.59 +      case "Permissions:Get":
    1.60 +        let permissions = [];
    1.61 +        for (let i = 0; i < this._permissonTypes.length; i++) {
    1.62 +          let type = this._permissonTypes[i];
    1.63 +          let value = this.getPermission(uri, type);
    1.64 +
    1.65 +          // Only add the permission if it was set by the user
    1.66 +          if (value == Services.perms.UNKNOWN_ACTION)
    1.67 +            continue;
    1.68 +
    1.69 +          // Get the strings that correspond to the permission type
    1.70 +          let typeStrings = this._permissionStrings[type];
    1.71 +          let label = Strings.browser.GetStringFromName(typeStrings["label"]);
    1.72 +
    1.73 +          // Get the key to look up the appropriate string entity
    1.74 +          let valueKey = value == Services.perms.ALLOW_ACTION ?
    1.75 +                         "allowed" : "denied";
    1.76 +          let valueString = Strings.browser.GetStringFromName(typeStrings[valueKey]);
    1.77 +
    1.78 +          permissions.push({
    1.79 +            type: type,
    1.80 +            setting: label,
    1.81 +            value: valueString
    1.82 +          });
    1.83 +        }
    1.84 +
    1.85 +        // Keep track of permissions, so we know which ones to clear
    1.86 +        this._currentPermissions = permissions;
    1.87 +
    1.88 +        let host;
    1.89 +        try {
    1.90 +          host = uri.host;
    1.91 +        } catch(e) {
    1.92 +          host = uri.spec;
    1.93 +        }
    1.94 +        sendMessageToJava({
    1.95 +          type: "Permissions:Data",
    1.96 +          host: host,
    1.97 +          permissions: permissions
    1.98 +        });
    1.99 +        break;
   1.100 + 
   1.101 +      case "Permissions:Clear":
   1.102 +        // An array of the indices of the permissions we want to clear
   1.103 +        let permissionsToClear = JSON.parse(aData);
   1.104 +        let privacyContext = BrowserApp.selectedBrowser.docShell
   1.105 +                               .QueryInterface(Ci.nsILoadContext);
   1.106 +
   1.107 +        for (let i = 0; i < permissionsToClear.length; i++) {
   1.108 +          let indexToClear = permissionsToClear[i];
   1.109 +          let permissionType = this._currentPermissions[indexToClear]["type"];
   1.110 +          this.clearPermission(uri, permissionType, privacyContext);
   1.111 +        }
   1.112 +        break;
   1.113 +    }
   1.114 +  },
   1.115 +
   1.116 +  /**
   1.117 +   * Gets the permission value stored for a specified permission type.
   1.118 +   *
   1.119 +   * @param aType
   1.120 +   *        The permission type string stored in permission manager.
   1.121 +   *        e.g. "geolocation", "indexedDB", "popup"
   1.122 +   *
   1.123 +   * @return A permission value defined in nsIPermissionManager.
   1.124 +   */
   1.125 +  getPermission: function getPermission(aURI, aType) {
   1.126 +    // Password saving isn't a nsIPermissionManager permission type, so handle
   1.127 +    // it seperately.
   1.128 +    if (aType == "password") {
   1.129 +      // By default, login saving is enabled, so if it is disabled, the
   1.130 +      // user selected the never remember option
   1.131 +      if (!Services.logins.getLoginSavingEnabled(aURI.prePath))
   1.132 +        return Services.perms.DENY_ACTION;
   1.133 +
   1.134 +      // Check to see if the user ever actually saved a login
   1.135 +      if (Services.logins.countLogins(aURI.prePath, "", ""))
   1.136 +        return Services.perms.ALLOW_ACTION;
   1.137 +
   1.138 +      return Services.perms.UNKNOWN_ACTION;
   1.139 +    }
   1.140 +
   1.141 +    // Geolocation consumers use testExactPermission
   1.142 +    if (aType == "geolocation")
   1.143 +      return Services.perms.testExactPermission(aURI, aType);
   1.144 +
   1.145 +    return Services.perms.testPermission(aURI, aType);
   1.146 +  },
   1.147 +
   1.148 +  /**
   1.149 +   * Clears a user-set permission value for the site given a permission type.
   1.150 +   *
   1.151 +   * @param aType
   1.152 +   *        The permission type string stored in permission manager.
   1.153 +   *        e.g. "geolocation", "indexedDB", "popup"
   1.154 +   */
   1.155 +  clearPermission: function clearPermission(aURI, aType, aContext) {
   1.156 +    // Password saving isn't a nsIPermissionManager permission type, so handle
   1.157 +    // it seperately.
   1.158 +    if (aType == "password") {
   1.159 +      // Get rid of exisiting stored logings
   1.160 +      let logins = Services.logins.findLogins({}, aURI.prePath, "", "");
   1.161 +      for (let i = 0; i < logins.length; i++) {
   1.162 +        Services.logins.removeLogin(logins[i]);
   1.163 +      }
   1.164 +      // Re-set login saving to enabled
   1.165 +      Services.logins.setLoginSavingEnabled(aURI.prePath, true);
   1.166 +    } else {
   1.167 +      Services.perms.remove(aURI.host, aType);
   1.168 +      // Clear content prefs set in ContentPermissionPrompt.js
   1.169 +      Cc["@mozilla.org/content-pref/service;1"]
   1.170 +        .getService(Ci.nsIContentPrefService2)
   1.171 +        .removeByDomainAndName(aURI.spec, aType + ".request.remember", aContext);
   1.172 +    }
   1.173 +  }
   1.174 +};

mercurial