Bug 867089 - Validate the playbackRate before using it. r=ehsan
[gecko.git] / webapprt / ContentPermission.js
blob3a726393f61dd426baa497020c4aef50250f618b
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 Cu.import("resource://gre/modules/Services.jsm");
10 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
11 Cu.import("resource://webapprt/modules/WebappRT.jsm");
13 function ContentPermission() {}
15 ContentPermission.prototype = {
16   classID: Components.ID("{07ef5b2e-88fb-47bd-8cec-d3b0bef11ac4}"),
17   QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionPrompt]),
19   prompt: function(request) {
20     // Only handle geolocation requests for now
21     if (request.type != "geolocation") {
22       return;
23     }
25     // Reuse any remembered permission preferences
26     let result = Services.perms.testExactPermissionFromPrincipal(request.principal, "geo");
27     if (result == Ci.nsIPermissionManager.ALLOW_ACTION) {
28       request.allow();
29       return;
30     }
31     else if (result == Ci.nsIPermissionManager.DENY_ACTION) {
32       request.cancel();
33       return;
34     }
36     function getChromeWindow(aWindow) {
37       var chromeWin = aWindow
38         .QueryInterface(Ci.nsIInterfaceRequestor)
39         .getInterface(Ci.nsIWebNavigation)
40         .QueryInterface(Ci.nsIDocShellTreeItem)
41         .rootTreeItem
42         .QueryInterface(Ci.nsIInterfaceRequestor)
43         .getInterface(Ci.nsIDOMWindow)
44         .QueryInterface(Ci.nsIDOMChromeWindow);
45       return chromeWin;
46     }
48     // Display a prompt at the top level
49     let {name} = WebappRT.config.app.manifest;
50     let requestingWindow = request.window.top;
51     let chromeWin = getChromeWindow(requestingWindow);
52     let bundle = Services.strings.createBundle("chrome://webapprt/locale/webapp.properties");
54     // Construct a prompt with share/don't and remember checkbox
55     let remember = {value: false};
56     let choice = Services.prompt.confirmEx(
57       chromeWin,
58       bundle.formatStringFromName("geolocation.title", [name], 1),
59       bundle.GetStringFromName("geolocation.description"),
60       // Set both buttons to strings with the cancel button being default
61       Ci.nsIPromptService.BUTTON_POS_1_DEFAULT |
62         Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_0 |
63         Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_1,
64       bundle.GetStringFromName("geolocation.sharelocation"),
65       bundle.GetStringFromName("geolocation.dontshare"),
66       null,
67       bundle.GetStringFromName("geolocation.remember"),
68       remember);
70     // Persist the choice if the user wants to remember
71     if (remember.value) {
72       let action = Ci.nsIPermissionManager.ALLOW_ACTION;
73       if (choice != 0) {
74         action = Ci.nsIPermissionManager.DENY_ACTION;
75       }
76       Services.perms.addFromPrincipal(request.principal, "geo", action);
77     }
79     // Trigger the selected choice
80     if (choice == 0) {
81       request.allow();
82     }
83     else {
84       request.cancel();
85     }
86   }
89 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ContentPermission]);