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 /* vim: set ft=javascript : */
8 var Cm = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
10 var EXPORTED_SYMBOLS = ["BrowserElementPromptService"];
13 // dump("BrowserElementPromptService - " + msg + "\n");
16 function BrowserElementPrompt(win, browserElementChild) {
18 this._browserElementChild = browserElementChild;
21 BrowserElementPrompt.prototype = {
22 QueryInterface: ChromeUtils.generateQI(["nsIPrompt"]),
25 this._browserElementChild.showModalPrompt(this._win, {
29 returnValue: undefined,
33 alertCheck(title, text, checkMsg, checkState) {
34 // Treat this like a normal alert() call, ignoring the checkState. The
35 // front-end can do its own suppression of the alert() if it wants.
36 this.alert(title, text);
39 confirm(title, text) {
40 return this._browserElementChild.showModalPrompt(this._win, {
41 promptType: "confirm",
44 returnValue: undefined,
48 confirmCheck(title, text, checkMsg, checkState) {
49 return this.confirm(title, text);
52 // Each button is described by an object with the following schema
54 // string messageType, // 'builtin' or 'custom'
55 // string message, // 'ok', 'cancel', 'yes', 'no', 'save', 'dontsave',
56 // // 'revert' or a string from caller if messageType was 'custom'.
59 // Expected result from embedder:
61 // int button, // Index of the button that user pressed.
62 // boolean checked, // True if the check box is checked.
74 let buttonProperties = this._buildConfirmExButtonProperties(
80 let defaultReturnValue = { selectedButton: buttonProperties.defaultButton };
82 defaultReturnValue.checked = checkState.value;
84 let ret = this._browserElementChild.showModalPrompt(this._win, {
85 promptType: "custom-prompt",
88 defaultButton: buttonProperties.defaultButton,
89 buttons: buttonProperties.buttons,
90 showCheckbox: !!checkMsg,
91 checkboxMessage: checkMsg,
92 checkboxCheckedByDefault: !!checkState.value,
93 returnValue: defaultReturnValue,
96 checkState.value = ret.checked;
98 return buttonProperties.indexToButtonNumberMap[ret.selectedButton];
101 prompt(title, text, value, checkMsg, checkState) {
102 let rv = this._browserElementChild.showModalPrompt(this._win, {
103 promptType: "prompt",
106 initialValue: value.value,
112 // nsIPrompt::Prompt returns true if the user pressed "OK" at the prompt,
113 // and false if the user pressed "Cancel".
115 // BrowserElementChild returns null for "Cancel" and returns the string the
116 // user entered otherwise.
120 promptUsernameAndPassword(title, text, username, password) {
121 throw Components.Exception("", Cr.NS_ERROR_NOT_IMPLEMENTED);
124 promptPassword(title, text, password) {
125 throw Components.Exception("", Cr.NS_ERROR_NOT_IMPLEMENTED);
128 select(title, text, aSelectList, aOutSelection) {
129 throw Components.Exception("", Cr.NS_ERROR_NOT_IMPLEMENTED);
132 _buildConfirmExButtonProperties(
141 // This map is for translating array index to the button number that
142 // is recognized by Gecko. This shouldn't be exposed to embedder.
143 indexToButtonNumberMap: [],
146 let defaultButton = 0; // Default to Button 0.
147 if (buttonFlags & Ci.nsIPrompt.BUTTON_POS_1_DEFAULT) {
149 } else if (buttonFlags & Ci.nsIPrompt.BUTTON_POS_2_DEFAULT) {
153 // Properties of each button.
154 let buttonPositions = [
155 Ci.nsIPrompt.BUTTON_POS_0,
156 Ci.nsIPrompt.BUTTON_POS_1,
157 Ci.nsIPrompt.BUTTON_POS_2,
160 function buildButton(buttonTitle, buttonNumber) {
162 let buttonPosition = buttonPositions[buttonNumber];
163 let mask = 0xff * buttonPosition; // 8 bit mask
164 let titleType = (buttonFlags & mask) / buttonPosition;
166 ret.messageType = "builtin";
168 case Ci.nsIPrompt.BUTTON_TITLE_OK:
171 case Ci.nsIPrompt.BUTTON_TITLE_CANCEL:
172 ret.message = "cancel";
174 case Ci.nsIPrompt.BUTTON_TITLE_YES:
177 case Ci.nsIPrompt.BUTTON_TITLE_NO:
180 case Ci.nsIPrompt.BUTTON_TITLE_SAVE:
181 ret.message = "save";
183 case Ci.nsIPrompt.BUTTON_TITLE_DONT_SAVE:
184 ret.message = "dontsave";
186 case Ci.nsIPrompt.BUTTON_TITLE_REVERT:
187 ret.message = "revert";
189 case Ci.nsIPrompt.BUTTON_TITLE_IS_STRING:
190 ret.message = buttonTitle;
191 ret.messageType = "custom";
194 // This button is not shown.
198 // If this is the default button, set r.defaultButton to
199 // the index of this button in the array. This value is going to be
200 // exposed to the embedder.
201 if (defaultButton === buttonNumber) {
202 r.defaultButton = r.buttons.length;
205 r.indexToButtonNumberMap.push(buttonNumber);
208 buildButton(button0Title, 0);
209 buildButton(button1Title, 1);
210 buildButton(button2Title, 2);
212 // If defaultButton is still -1 here, it means the default button won't
214 if (r.defaultButton === -1) {
215 throw new Components.Exception(
216 "Default button won't be shown",
225 function BrowserElementAuthPrompt() {}
227 BrowserElementAuthPrompt.prototype = {
228 QueryInterface: ChromeUtils.generateQI(["nsIAuthPrompt2"]),
230 promptAuth: function promptAuth(channel, level, authInfo) {
231 throw Components.Exception("", Cr.NS_ERROR_NOT_IMPLEMENTED);
234 asyncPromptAuth: function asyncPromptAuth(
241 debug("asyncPromptAuth");
243 // The cases that we don't support now.
245 authInfo.flags & Ci.nsIAuthInformation.AUTH_PROXY &&
246 authInfo.flags & Ci.nsIAuthInformation.ONLY_PASSWORD
248 throw Components.Exception("", Cr.NS_ERROR_FAILURE);
251 let frame = this._getFrameFromChannel(channel);
253 debug("Cannot get frame, asyncPromptAuth fail");
254 throw Components.Exception("", Cr.NS_ERROR_FAILURE);
257 let browserElementParent =
258 BrowserElementPromptService.getBrowserElementParentForFrame(frame);
260 if (!browserElementParent) {
261 debug("Failed to load browser element parent.");
262 throw Components.Exception("", Cr.NS_ERROR_FAILURE);
266 QueryInterface: ChromeUtils.generateQI(["nsICancelable"]),
270 this.callback.onAuthCancelled(this.context, false);
271 this.callback = null;
276 let [hostname, httpRealm] = this._getAuthTarget(channel, authInfo);
277 let hashKey = level + "|" + hostname + "|" + httpRealm;
278 let asyncPrompt = this._asyncPrompts[hashKey];
280 asyncPrompt.consumers.push(consumer);
285 consumers: [consumer],
290 browserElementParent,
293 this._asyncPrompts[hashKey] = asyncPrompt;
294 this._doAsyncPrompt();
298 // Utilities for nsIAuthPrompt2 ----------------
301 _asyncPromptInProgress: new WeakMap(),
303 // Find the key of a prompt whose browser element parent does not have
304 // async prompt in progress.
306 for (let key in this._asyncPrompts) {
307 let prompt = this._asyncPrompts[key];
308 if (!this._asyncPromptInProgress.get(prompt.browserElementParent)) {
314 // Didn't find an available prompt, so just return.
319 let prompt = this._asyncPrompts[hashKey];
321 this._asyncPromptInProgress.set(prompt.browserElementParent, true);
322 prompt.inProgress = true;
325 let callback = function (ok, username, password) {
327 "Async auth callback is called, ok = " + ok + ", username = " + username
330 // Here we got the username and password provided by embedder, or
331 // ok = false if the prompt was cancelled by embedder.
332 delete self._asyncPrompts[hashKey];
333 prompt.inProgress = false;
334 self._asyncPromptInProgress.delete(prompt.browserElementParent);
336 // Fill authentication information with username and password provided
338 let flags = prompt.authInfo.flags;
340 if (flags & Ci.nsIAuthInformation.NEED_DOMAIN) {
341 // Domain is separated from username by a backslash
342 let idx = username.indexOf("\\");
344 prompt.authInfo.username = username;
346 prompt.authInfo.domain = username.substring(0, idx);
347 prompt.authInfo.username = username.substring(idx + 1);
350 prompt.authInfo.username = username;
355 prompt.authInfo.password = password;
358 for (let consumer of prompt.consumers) {
359 if (!consumer.callback) {
360 // Not having a callback means that consumer didn't provide it
361 // or canceled the notification.
367 debug("Ok, calling onAuthAvailable to finish auth");
368 consumer.callback.onAuthAvailable(
373 debug("Cancelled, calling onAuthCancelled to finish auth.");
374 consumer.callback.onAuthCancelled(consumer.context, true);
377 /* Throw away exceptions caused by callback */
381 // Process the next prompt, if one is pending.
382 self._doAsyncPrompt();
387 // Call promptAuth of browserElementParent, to show the prompt.
388 prompt.browserElementParent.promptAuth(
389 self._createAuthDetail(prompt.channel, prompt.authInfo),
395 Services.tm.dispatchToMainThread(runnable);
398 _getFrameFromChannel(channel) {
399 let loadContext = channel.notificationCallbacks.getInterface(
402 return loadContext.topFrameElement;
405 _createAuthDetail(channel, authInfo) {
406 let [hostname, httpRealm] = this._getAuthTarget(channel, authInfo);
409 path: channel.URI.pathQueryRef,
411 username: authInfo.username,
412 isProxy: !!(authInfo.flags & Ci.nsIAuthInformation.AUTH_PROXY),
413 isOnlyPassword: !!(authInfo.flags & Ci.nsIAuthInformation.ONLY_PASSWORD),
417 // The code is taken from nsLoginManagerPrompter.js, with slight
418 // modification for parameter name consistency here.
419 _getAuthTarget(channel, authInfo) {
422 // If our proxy is demanding authentication, don't use the
423 // channel's actual destination.
424 if (authInfo.flags & Ci.nsIAuthInformation.AUTH_PROXY) {
425 if (!(channel instanceof Ci.nsIProxiedChannel)) {
426 throw new Error("proxy auth needs nsIProxiedChannel");
429 let info = channel.proxyInfo;
431 throw new Error("proxy auth needs nsIProxyInfo");
434 // Proxies don't have a scheme, but we'll use "moz-proxy://"
435 // so that it's more obvious what the login is for.
436 var idnService = Cc["@mozilla.org/network/idn-service;1"].getService(
441 idnService.convertUTF8toACE(info.host) +
444 realm = authInfo.realm;
449 return [hostname, realm];
452 hostname = this._getFormattedHostname(channel.URI);
454 // If a HTTP WWW-Authenticate header specified a realm, that value
455 // will be available here. If it wasn't set or wasn't HTTP, we'll use
456 // the formatted hostname instead.
457 realm = authInfo.realm;
462 return [hostname, realm];
466 * Strip out things like userPass and path for display.
468 _getFormattedHostname(uri) {
469 return uri.scheme + "://" + uri.hostPort;
473 function AuthPromptWrapper(oldImpl, browserElementImpl) {
474 this._oldImpl = oldImpl;
475 this._browserElementImpl = browserElementImpl;
478 AuthPromptWrapper.prototype = {
479 QueryInterface: ChromeUtils.generateQI(["nsIAuthPrompt2"]),
480 promptAuth(channel, level, authInfo) {
481 if (this._canGetParentElement(channel)) {
482 return this._browserElementImpl.promptAuth(channel, level, authInfo);
484 return this._oldImpl.promptAuth(channel, level, authInfo);
487 asyncPromptAuth(channel, callback, context, level, authInfo) {
488 if (this._canGetParentElement(channel)) {
489 return this._browserElementImpl.asyncPromptAuth(
497 return this._oldImpl.asyncPromptAuth(
506 _canGetParentElement(channel) {
508 let context = channel.notificationCallbacks.getInterface(
511 let frame = context.topFrameElement;
516 if (!BrowserElementPromptService.getBrowserElementParentForFrame(frame)) {
527 function BrowserElementPromptFactory(toWrap) {
528 this._wrapped = toWrap;
531 BrowserElementPromptFactory.prototype = {
532 classID: Components.ID("{24f3d0cf-e417-4b85-9017-c9ecf8bb1299}"),
533 QueryInterface: ChromeUtils.generateQI(["nsIPromptFactory"]),
535 _mayUseNativePrompt() {
537 return Services.prefs.getBoolPref("browser.prompt.allowNative");
539 // This properity is default to true.
544 _getNativePromptIfAllowed(win, iid, err) {
545 if (this._mayUseNativePrompt()) {
546 return this._wrapped.getPrompt(win, iid);
549 // Not allowed, throw an exception.
553 getPrompt(win, iid) {
554 // It is possible for some object to get a prompt without passing
555 // valid reference of window, like nsNSSComponent. In such case, we
556 // should just fall back to the native prompt service
558 return this._getNativePromptIfAllowed(win, iid, Cr.NS_ERROR_INVALID_ARG);
562 iid.number != Ci.nsIPrompt.number &&
563 iid.number != Ci.nsIAuthPrompt2.number
566 "We don't recognize the requested IID (" +
577 return this._getNativePromptIfAllowed(win, iid, Cr.NS_ERROR_INVALID_ARG);
580 // Try to find a BrowserElementChild for the window.
581 let browserElementChild =
582 BrowserElementPromptService.getBrowserElementChildForWindow(win);
584 if (iid.number === Ci.nsIAuthPrompt2.number) {
585 debug("Caller requests an instance of nsIAuthPrompt2.");
587 if (browserElementChild) {
588 // If we are able to get a BrowserElementChild, it means that
589 // the auth prompt is for a mozbrowser. Therefore we don't need to
591 return new BrowserElementAuthPrompt().QueryInterface(iid);
594 // Because nsIAuthPrompt2 is called in parent process. If caller
595 // wants nsIAuthPrompt2 and we cannot get BrowserElementchild,
596 // it doesn't mean that we should fallback. It is possible that we can
597 // get the BrowserElementParent from nsIChannel that passed to
598 // functions of nsIAuthPrompt2.
599 if (this._mayUseNativePrompt()) {
600 return new AuthPromptWrapper(
601 this._wrapped.getPrompt(win, iid),
602 new BrowserElementAuthPrompt().QueryInterface(iid)
603 ).QueryInterface(iid);
605 // Falling back is not allowed, so we don't need wrap the
606 // BrowserElementPrompt.
607 return new BrowserElementAuthPrompt().QueryInterface(iid);
610 if (!browserElementChild) {
612 "We can't find a browserElementChild for " + win + ", " + win.location
614 return this._getNativePromptIfAllowed(win, iid, Cr.NS_ERROR_FAILURE);
617 debug("Returning wrapped getPrompt for " + win);
618 return new BrowserElementPrompt(win, browserElementChild).QueryInterface(
624 var BrowserElementPromptService = {
625 QueryInterface: ChromeUtils.generateQI([
627 "nsISupportsWeakReference",
633 if (this._initialized) {
637 this._initialized = true;
638 this._browserElementParentMap = new WeakMap();
640 Services.obs.addObserver(
642 "outer-window-destroyed",
643 /* ownsWeak = */ true
646 // Wrap the existing @mozilla.org/prompter;1 implementation.
647 var contractID = "@mozilla.org/prompter;1";
648 var oldCID = Cm.contractIDToCID(contractID);
649 var newCID = BrowserElementPromptFactory.prototype.classID;
650 var oldFactory = Cm.getClassObject(Cc[contractID], Ci.nsIFactory);
652 if (oldCID == newCID) {
653 debug("WARNING: Wrapped prompt factory is already installed!");
657 var oldInstance = oldFactory.createInstance(null, Ci.nsIPromptFactory);
658 var newInstance = new BrowserElementPromptFactory(oldInstance);
661 createInstance(iid) {
662 return newInstance.QueryInterface(iid);
667 "BrowserElementPromptService's prompter;1 wrapper",
672 debug("Done installing new prompt factory.");
675 _getOuterWindowID(win) {
676 return win.docShell.outerWindowID;
679 _browserElementChildMap: {},
680 mapWindowToBrowserElementChild(win, browserElementChild) {
681 this._browserElementChildMap[this._getOuterWindowID(win)] =
684 unmapWindowToBrowserElementChild(win) {
685 delete this._browserElementChildMap[this._getOuterWindowID(win)];
688 getBrowserElementChildForWindow(win) {
689 // We only have a mapping for <iframe mozbrowser>s, not their inner
690 // <iframes>, so we look up win.top below. window.top (when called from
691 // script) respects <iframe mozbrowser> boundaries.
692 return this._browserElementChildMap[this._getOuterWindowID(win.top)];
695 mapFrameToBrowserElementParent(frame, browserElementParent) {
696 this._browserElementParentMap.set(frame, browserElementParent);
699 getBrowserElementParentForFrame(frame) {
700 return this._browserElementParentMap.get(frame);
703 _observeOuterWindowDestroyed(outerWindowID) {
704 let id = outerWindowID.QueryInterface(Ci.nsISupportsPRUint64).data;
705 debug("observeOuterWindowDestroyed " + id);
706 delete this._browserElementChildMap[outerWindowID.data];
709 observe(subject, topic, data) {
711 case "outer-window-destroyed":
712 this._observeOuterWindowDestroyed(subject);
715 debug("Observed unexpected topic " + topic);
720 BrowserElementPromptService._init();