Bug 948389 - Replace mozilla-banner.gif with a plain blue image in 405577-1.html...
[gecko.git] / webapprt / ContentPermission.js
blobc0cf06993b995ba6ea41cd799fac7f187cd73505
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/. */
5 const Cc = Components.classes;
6 const Ci = Components.interfaces;
7 const Cu = Components.utils;
9 const UNKNOWN_FAIL = ["geolocation", "desktop-notification"];
11 Cu.import("resource://gre/modules/Services.jsm");
12 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
13 Cu.import("resource://webapprt/modules/WebappRT.jsm");
15 function ContentPermission() {}
17 ContentPermission.prototype = {
18   classID: Components.ID("{07ef5b2e-88fb-47bd-8cec-d3b0bef11ac4}"),
19   QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionPrompt]),
21   _getChromeWindow: function(aWindow) { 
22     return aWindow
23       .QueryInterface(Ci.nsIInterfaceRequestor)
24       .getInterface(Ci.nsIWebNavigation)
25       .QueryInterface(Ci.nsIDocShellTreeItem)
26       .rootTreeItem
27       .QueryInterface(Ci.nsIInterfaceRequestor)
28       .getInterface(Ci.nsIDOMWindow)
29       .QueryInterface(Ci.nsIDOMChromeWindow);
30   },
32   prompt: function(request) {
33     // Reuse any remembered permission preferences
34     let result =
35       Services.perms.testExactPermissionFromPrincipal(request.principal,
36                                                       request.type);
38     // We used to use the name "geo" for the geolocation permission, now we're
39     // using "geolocation".  We need to check both to support existing
40     // installations.
41     if ((result == Ci.nsIPermissionManager.UNKNOWN_ACTION ||
42          result == Ci.nsIPermissionManager.PROMPT_ACTION) &&
43         request.type == "geolocation") {
44       let geoResult = Services.perms.testExactPermission(request.principal.URI,
45                                                          "geo");
46       // We override the result only if the "geo" permission was allowed or
47       // denied.
48       if (geoResult == Ci.nsIPermissionManager.ALLOW_ACTION ||
49           geoResult == Ci.nsIPermissionManager.DENY_ACTION) {
50         result = geoResult;
51       }
52     }
54     if (result == Ci.nsIPermissionManager.ALLOW_ACTION) {
55       request.allow();
56       return;
57     } else if (result == Ci.nsIPermissionManager.DENY_ACTION ||
58                (result == Ci.nsIPermissionManager.UNKNOWN_ACTION &&
59                 UNKNOWN_FAIL.indexOf(request.type) >= 0)) {
60       request.cancel();
61       return;
62     }
64     // Display a prompt at the top level
65     let {name} = WebappRT.localeManifest;
66     let requestingWindow = request.window.top;
67     let chromeWin = this._getChromeWindow(requestingWindow);
68     let bundle = Services.strings.createBundle("chrome://webapprt/locale/webapp.properties");
70     // Construct a prompt with share/don't and remember checkbox
71     let remember = {value: false};
72     let choice = Services.prompt.confirmEx(
73       chromeWin,
74       bundle.formatStringFromName(request.type + ".title", [name], 1),
75       bundle.GetStringFromName(request.type + ".description"),
76       // Set both buttons to strings with the cancel button being default
77       Ci.nsIPromptService.BUTTON_POS_1_DEFAULT |
78         Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_0 |
79         Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_1,
80       bundle.GetStringFromName(request.type + ".allow"),
81       bundle.GetStringFromName(request.type + ".deny"),
82       null,
83       bundle.GetStringFromName(request.type + ".remember"),
84       remember);
86     let action = Ci.nsIPermissionManager.ALLOW_ACTION;
87     if (choice != 0) {
88       action = Ci.nsIPermissionManager.DENY_ACTION;
89     }
91     if (remember.value) {
92       // Persist the choice if the user wants to remember
93       Services.perms.addFromPrincipal(request.principal, request.type, action);
94     } else {
95       // Otherwise allow the permission for the current session
96       Services.perms.addFromPrincipal(request.principal, request.type, action,
97                                       Ci.nsIPermissionManager.EXPIRE_SESSION);
98     }
100     // Trigger the selected choice
101     if (choice == 0) {
102       request.allow();
103     }
104     else {
105       request.cancel();
106     }
107   }
110 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ContentPermission]);