Translation updates
[torbutton.git] / components / external-app-blocker.js
blob6a53fc01eaf95fae8440634a71b4556e6425816b
1 // Bug 1506 Android P1/TBB P5: This code provides users with notification
2 // in the event of external app launch. We want it to exist in the desktop
3 // port, but it is probably useless for Android.
5 /*************************************************************************
6 * External App Handler.
7 * Handles displaying confirmation dialogs for external apps and protocols
8 * due to Firefox Bug https://bugzilla.mozilla.org/show_bug.cgi?id=440892
10 * An instance of this module is created each time the browser starts to
11 * download a file and when an external application may be invoked to
12 * handle an URL (e.g., when the user clicks on a mailto: URL).
13 *************************************************************************/
15 const { XPCOMUtils } = ChromeUtils.import(
16 "resource://gre/modules/XPCOMUtils.jsm"
18 const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
19 const { PromptUtils } = ChromeUtils.import(
20 "resource://gre/modules/SharedPromptUtils.jsm"
23 XPCOMUtils.defineLazyModuleGetters(this, {
24 ComponentUtils: "resource://gre/modules/ComponentUtils.jsm",
25 });
27 let { torbutton_get_property_string } = ChromeUtils.import(
28 "resource://torbutton/modules/utils.js"
31 // Module specific constants
32 const kMODULE_NAME = "Torbutton External App Handler";
33 const kCONTRACT_ID = "@torproject.org/torbutton-extAppBlocker;1";
34 const kMODULE_CID = Components.ID("3da0269f-fc29-4e9e-a678-c3b1cafcf13f");
36 const kInterfaces = [Ci.nsIObserver, Ci.nsIClassInfo];
38 function ExternalAppBlocker() {
39 this.logger = Cc["@torproject.org/torbutton-logger;1"].getService(
40 Ci.nsISupports
41 ).wrappedJSObject;
42 this.logger.log(3, "Component Load 0: New ExternalAppBlocker.");
45 ExternalAppBlocker.prototype = {
46 _helperAppLauncher: undefined,
48 QueryInterface: ChromeUtils.generateQI([
49 Ci.nsIObserver,
50 Ci.nsIHelperAppWarningDialog,
51 ]),
53 // make this an nsIClassInfo object
54 flags: Ci.nsIClassInfo.DOM_OBJECT,
55 classDescription: kMODULE_NAME,
56 contractID: kCONTRACT_ID,
57 classID: kMODULE_CID,
59 // method of nsIClassInfo
60 getInterfaces(count) {
61 count.value = kInterfaces.length;
62 return kInterfaces;
65 // method of nsIClassInfo
66 getHelperForLanguage(count) {
67 return null;
70 // method of nsIHelperAppWarningDialog
71 maybeShow(aLauncher, aWindowContext) {
72 // Hold a reference to the object that called this component. This is
73 // important not just because we need to later invoke the
74 // continueRequest() or cancelRequest() callback on aLauncher, but also
75 // so that the launcher object (which is a reference counted object) is
76 // not released too soon.
77 this._helperAppLauncher = aLauncher;
79 if (!Services.prefs.getBoolPref("extensions.torbutton.launch_warning")) {
80 this._helperAppLauncher.continueRequest();
81 return;
84 this._showPrompt(aWindowContext);
88 * The _showPrompt() implementation uses some XUL and JS that is part of the
89 * browser's confirmEx() implementation. Specifically, _showPrompt() depends
90 * on chrome://global/content/commonDialog.xhtml as well as some of the code
91 * in resource://gre/modules/SharedPromptUtils.jsm.
93 _showPrompt(aWindowContext) {
94 let parentWin;
95 try {
96 parentWin = aWindowContext.getInterface(Ci.nsIDOMWindow);
97 } catch (e) {
98 parentWin = Services.wm.getMostRecentWindow("navigator:browser");
101 let title = torbutton_get_property_string("torbutton.popup.external.title");
102 let app = torbutton_get_property_string("torbutton.popup.external.app");
103 let note = torbutton_get_property_string("torbutton.popup.external.note");
104 let suggest = torbutton_get_property_string(
105 "torbutton.popup.external.suggest"
107 let launch = torbutton_get_property_string("torbutton.popup.launch");
108 let cancel = torbutton_get_property_string("torbutton.popup.cancel");
109 let dontask = torbutton_get_property_string("torbutton.popup.dontask");
111 let args = {
112 promptType: "confirmEx",
113 title,
114 text: app + note + suggest + " ",
115 checkLabel: dontask,
116 checked: false,
117 ok: false,
118 button0Label: launch,
119 button1Label: cancel,
120 defaultButtonNum: 1, // Cancel
121 buttonNumClicked: 1, // Cancel
122 enableDelay: true,
125 let propBag = PromptUtils.objectToPropBag(args);
126 let uri = "chrome://global/content/commonDialog.xhtml";
127 let promptWin = Services.ww.openWindow(
128 parentWin,
129 uri,
130 "_blank",
131 "centerscreen,chrome,titlebar",
132 propBag
134 promptWin.addEventListener("load", aEvent => {
135 promptWin.addEventListener("unload", aEvent => {
136 PromptUtils.propBagToObject(propBag, args);
138 if (0 == args.buttonNumClicked) {
139 // Save the checkbox value and tell the browser's external helper app
140 // module about the user's choice.
141 if (args.checked) {
142 Services.prefs.setBoolPref(
143 "extensions.torbutton.launch_warning",
144 false
148 this._helperAppLauncher.continueRequest();
149 } else {
150 this._helperAppLauncher.cancelRequest(Cr.NS_BINDING_ABORTED);
157 // Assign factory to global object.
158 const NSGetFactory = XPCOMUtils.generateNSGetFactory
159 ? XPCOMUtils.generateNSGetFactory([ExternalAppBlocker])
160 : ComponentUtils.generateNSGetFactory([ExternalAppBlocker]);