Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
4 "use strict";
6 var PermissionsHelper = {
7 _permissonTypes: ["password", "geolocation", "popup", "indexedDB",
8 "offline-app", "desktop-notification", "plugins", "native-intent"],
9 _permissionStrings: {
10 "password": {
11 label: "password.savePassword",
12 allowed: "password.save",
13 denied: "password.dontSave"
14 },
15 "geolocation": {
16 label: "geolocation.shareLocation",
17 allowed: "geolocation.allow",
18 denied: "geolocation.dontAllow"
19 },
20 "popup": {
21 label: "blockPopups.label",
22 allowed: "popup.show",
23 denied: "popup.dontShow"
24 },
25 "indexedDB": {
26 label: "offlineApps.storeOfflineData",
27 allowed: "offlineApps.allow",
28 denied: "offlineApps.dontAllow2"
29 },
30 "offline-app": {
31 label: "offlineApps.storeOfflineData",
32 allowed: "offlineApps.allow",
33 denied: "offlineApps.dontAllow2"
34 },
35 "desktop-notification": {
36 label: "desktopNotification.useNotifications",
37 allowed: "desktopNotification.allow",
38 denied: "desktopNotification.dontAllow"
39 },
40 "plugins": {
41 label: "clickToPlayPlugins.activatePlugins",
42 allowed: "clickToPlayPlugins.activate",
43 denied: "clickToPlayPlugins.dontActivate"
44 },
45 "native-intent": {
46 label: "helperapps.openWithList2",
47 allowed: "helperapps.always",
48 denied: "helperapps.never"
49 }
50 },
52 observe: function observe(aSubject, aTopic, aData) {
53 let uri = BrowserApp.selectedBrowser.currentURI;
55 switch (aTopic) {
56 case "Permissions:Get":
57 let permissions = [];
58 for (let i = 0; i < this._permissonTypes.length; i++) {
59 let type = this._permissonTypes[i];
60 let value = this.getPermission(uri, type);
62 // Only add the permission if it was set by the user
63 if (value == Services.perms.UNKNOWN_ACTION)
64 continue;
66 // Get the strings that correspond to the permission type
67 let typeStrings = this._permissionStrings[type];
68 let label = Strings.browser.GetStringFromName(typeStrings["label"]);
70 // Get the key to look up the appropriate string entity
71 let valueKey = value == Services.perms.ALLOW_ACTION ?
72 "allowed" : "denied";
73 let valueString = Strings.browser.GetStringFromName(typeStrings[valueKey]);
75 permissions.push({
76 type: type,
77 setting: label,
78 value: valueString
79 });
80 }
82 // Keep track of permissions, so we know which ones to clear
83 this._currentPermissions = permissions;
85 let host;
86 try {
87 host = uri.host;
88 } catch(e) {
89 host = uri.spec;
90 }
91 sendMessageToJava({
92 type: "Permissions:Data",
93 host: host,
94 permissions: permissions
95 });
96 break;
98 case "Permissions:Clear":
99 // An array of the indices of the permissions we want to clear
100 let permissionsToClear = JSON.parse(aData);
101 let privacyContext = BrowserApp.selectedBrowser.docShell
102 .QueryInterface(Ci.nsILoadContext);
104 for (let i = 0; i < permissionsToClear.length; i++) {
105 let indexToClear = permissionsToClear[i];
106 let permissionType = this._currentPermissions[indexToClear]["type"];
107 this.clearPermission(uri, permissionType, privacyContext);
108 }
109 break;
110 }
111 },
113 /**
114 * Gets the permission value stored for a specified permission type.
115 *
116 * @param aType
117 * The permission type string stored in permission manager.
118 * e.g. "geolocation", "indexedDB", "popup"
119 *
120 * @return A permission value defined in nsIPermissionManager.
121 */
122 getPermission: function getPermission(aURI, aType) {
123 // Password saving isn't a nsIPermissionManager permission type, so handle
124 // it seperately.
125 if (aType == "password") {
126 // By default, login saving is enabled, so if it is disabled, the
127 // user selected the never remember option
128 if (!Services.logins.getLoginSavingEnabled(aURI.prePath))
129 return Services.perms.DENY_ACTION;
131 // Check to see if the user ever actually saved a login
132 if (Services.logins.countLogins(aURI.prePath, "", ""))
133 return Services.perms.ALLOW_ACTION;
135 return Services.perms.UNKNOWN_ACTION;
136 }
138 // Geolocation consumers use testExactPermission
139 if (aType == "geolocation")
140 return Services.perms.testExactPermission(aURI, aType);
142 return Services.perms.testPermission(aURI, aType);
143 },
145 /**
146 * Clears a user-set permission value for the site given a permission type.
147 *
148 * @param aType
149 * The permission type string stored in permission manager.
150 * e.g. "geolocation", "indexedDB", "popup"
151 */
152 clearPermission: function clearPermission(aURI, aType, aContext) {
153 // Password saving isn't a nsIPermissionManager permission type, so handle
154 // it seperately.
155 if (aType == "password") {
156 // Get rid of exisiting stored logings
157 let logins = Services.logins.findLogins({}, aURI.prePath, "", "");
158 for (let i = 0; i < logins.length; i++) {
159 Services.logins.removeLogin(logins[i]);
160 }
161 // Re-set login saving to enabled
162 Services.logins.setLoginSavingEnabled(aURI.prePath, true);
163 } else {
164 Services.perms.remove(aURI.host, aType);
165 // Clear content prefs set in ContentPermissionPrompt.js
166 Cc["@mozilla.org/content-pref/service;1"]
167 .getService(Ci.nsIContentPrefService2)
168 .removeByDomainAndName(aURI.spec, aType + ".request.remember", aContext);
169 }
170 }
171 };