|
1 // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 const Ci = Components.interfaces; |
|
6 const Cc = Components.classes; |
|
7 const Cr = Components.results; |
|
8 const Cu = Components.utils; |
|
9 |
|
10 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
11 Cu.import("resource://gre/modules/Services.jsm"); |
|
12 |
|
13 function PromptService() { |
|
14 |
|
15 } |
|
16 |
|
17 PromptService.prototype = { |
|
18 classID: Components.ID("{9a61149b-2276-4a0a-b79c-be994ad106cf}"), |
|
19 |
|
20 QueryInterface: XPCOMUtils.generateQI([Ci.nsIPromptFactory, Ci.nsIPromptService, Ci.nsIPromptService2]), |
|
21 |
|
22 /* ---------- nsIPromptFactory ---------- */ |
|
23 |
|
24 getPrompt: function getPrompt(domWin, iid) { |
|
25 if (!domWin) { |
|
26 let chromeWin = Services.wm.getMostRecentWindow("navigator:browser"); |
|
27 if (!chromeWin) { |
|
28 let msg = "PromptService.js: Attempted create a prompt but no DOM Window specified and failed to find one"; |
|
29 Cu.reportError(msg); |
|
30 throw(msg); |
|
31 } |
|
32 domWin = chromeWin.getBrowser().contentWindow; |
|
33 } |
|
34 |
|
35 let factory = |
|
36 Components.classesByID["{1c978d25-b37f-43a8-a2d6-0c7a239ead87}"] |
|
37 .createInstance(Ci.nsIPromptFactory); |
|
38 |
|
39 let prompt = factory.getPrompt(domWin, iid); |
|
40 try { |
|
41 let bag = prompt.QueryInterface(Ci.nsIWritablePropertyBag2); |
|
42 bag.setPropertyAsBool("allowTabModal", true); |
|
43 } catch(e) { |
|
44 } |
|
45 return prompt; |
|
46 }, |
|
47 |
|
48 /* ---------- private memebers ---------- */ |
|
49 |
|
50 callProxy: function(aName, aArgs) { |
|
51 let domWin = aArgs[0]; |
|
52 let prompt = this.getPrompt(domWin, Ci.nsIPrompt); |
|
53 if (aName == 'promptAuth' || aName == 'promptAuthAsync') { |
|
54 let adapterFactory = |
|
55 Components.classesByID["{6e134924-6c3a-4d86-81ac-69432dd971dc}"] |
|
56 .createInstance(Ci.nsIAuthPromptAdapterFactory); |
|
57 prompt = adapterFactory.createAdapter(prompt); |
|
58 } |
|
59 |
|
60 return prompt[aName].apply(prompt, Array.prototype.slice.call(aArgs, 1)); |
|
61 }, |
|
62 |
|
63 /* ---------- nsIPromptService ---------- */ |
|
64 |
|
65 alert: function() { |
|
66 return this.callProxy("alert", arguments); |
|
67 }, |
|
68 alertCheck: function() { |
|
69 return this.callProxy("alertCheck", arguments); |
|
70 }, |
|
71 confirm: function() { |
|
72 return this.callProxy("confirm", arguments); |
|
73 }, |
|
74 confirmCheck: function() { |
|
75 return this.callProxy("confirmCheck", arguments); |
|
76 }, |
|
77 confirmEx: function() { |
|
78 return this.callProxy("confirmEx", arguments); |
|
79 }, |
|
80 prompt: function() { |
|
81 return this.callProxy("prompt", arguments); |
|
82 }, |
|
83 promptUsernameAndPassword: function() { |
|
84 return this.callProxy("promptUsernameAndPassword", arguments); |
|
85 }, |
|
86 promptPassword: function() { |
|
87 return this.callProxy("promptPassword", arguments); |
|
88 }, |
|
89 select: function() { |
|
90 return this.callProxy("select", arguments); |
|
91 }, |
|
92 |
|
93 /* ---------- nsIPromptService2 ---------- */ |
|
94 |
|
95 promptAuth: function() { |
|
96 return this.callProxy("promptAuth", arguments); |
|
97 }, |
|
98 asyncPromptAuth: function() { |
|
99 return this.callProxy("asyncPromptAuth", arguments); |
|
100 } |
|
101 }; |
|
102 |
|
103 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([PromptService]); |