2009-09-02 09:15:26 -07:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2009
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Fabrice Desré <fabrice.desre@gmail.com>, Original author
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2010-07-13 07:36:09 -07:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2009-09-02 09:15:26 -07:00
|
|
|
|
2011-01-04 07:35:05 -08:00
|
|
|
// Whitelist of methods we remote - to check against malicious data.
|
|
|
|
// For example, it would be dangerous to allow content to show auth prompts.
|
|
|
|
const REMOTABLE_METHODS = {
|
|
|
|
alert: { outParams: [] },
|
|
|
|
alertCheck: { outParams: [4] },
|
|
|
|
confirm: { outParams: [] },
|
|
|
|
prompt: { outParams: [3, 5] },
|
|
|
|
confirmEx: { outParams: [8] },
|
|
|
|
confirmCheck: { outParams: [4] },
|
|
|
|
select: { outParams: [5] }
|
|
|
|
};
|
|
|
|
|
2010-09-03 12:17:00 -07:00
|
|
|
var gPromptService = null;
|
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
function PromptService() {
|
2010-09-03 12:17:00 -07:00
|
|
|
// Depending on if we are in the parent or child, prepare to remote
|
|
|
|
// certain calls
|
|
|
|
var appInfo = Cc["@mozilla.org/xre/app-info;1"];
|
|
|
|
if (!appInfo || appInfo.getService(Ci.nsIXULRuntime).processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT) {
|
|
|
|
// Parent process
|
|
|
|
this.inContentProcess = false;
|
|
|
|
|
|
|
|
// Used for wakeups service. FIXME: clean up with bug 593407
|
|
|
|
this.wrappedJSObject = this;
|
|
|
|
|
|
|
|
// Setup listener for child messages. We don't need to call
|
|
|
|
// addMessageListener as the wakeup service will do that for us.
|
|
|
|
this.receiveMessage = function(aMessage) {
|
|
|
|
var json = aMessage.json;
|
|
|
|
switch (aMessage.name) {
|
|
|
|
case "Prompt:Call":
|
|
|
|
var method = aMessage.json.method;
|
2011-01-04 07:35:05 -08:00
|
|
|
if (!REMOTABLE_METHODS.hasOwnProperty(method))
|
2010-09-09 06:48:32 -07:00
|
|
|
throw "PromptServiceRemoter received an invalid method "+method;
|
2011-01-04 07:35:05 -08:00
|
|
|
|
2010-09-03 12:17:00 -07:00
|
|
|
var arguments = aMessage.json.arguments;
|
|
|
|
var ret = this[method].apply(this, arguments);
|
|
|
|
// Return multiple return values in objects of form { value: ... },
|
|
|
|
// and also with the actual return value at the end
|
|
|
|
arguments.push(ret);
|
|
|
|
return arguments;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
// Child process
|
|
|
|
this.inContentProcess = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
gPromptService = this;
|
2009-09-02 09:15:26 -07:00
|
|
|
}
|
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
PromptService.prototype = {
|
2009-09-02 09:15:26 -07:00
|
|
|
classID: Components.ID("{9a61149b-2276-4a0a-b79c-be994ad106cf}"),
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIPromptFactory, Ci.nsIPromptService, Ci.nsIPromptService2]),
|
|
|
|
|
|
|
|
/* ---------- nsIPromptFactory ---------- */
|
|
|
|
|
|
|
|
// XXX Copied from nsPrompter.js.
|
|
|
|
getPrompt: function getPrompt(domWin, iid) {
|
2011-01-04 07:35:05 -08:00
|
|
|
if (this.inContentProcess)
|
|
|
|
return ContentPrompt.QueryInterface(iid);
|
2010-06-25 13:29:47 -07:00
|
|
|
|
2009-09-02 09:15:26 -07:00
|
|
|
let doc = this.getDocument();
|
2011-01-04 07:35:05 -08:00
|
|
|
if (!doc) {
|
2010-06-25 13:29:47 -07:00
|
|
|
let fallback = this._getFallbackService();
|
|
|
|
return fallback.getPrompt(domWin, iid);
|
2009-11-12 21:09:38 -08:00
|
|
|
}
|
2011-01-04 07:35:05 -08:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
let p = new Prompt(domWin, doc);
|
|
|
|
p.QueryInterface(iid);
|
|
|
|
return p;
|
2009-09-02 09:15:26 -07:00
|
|
|
},
|
2010-06-25 13:29:47 -07:00
|
|
|
|
|
|
|
/* ---------- private memebers ---------- */
|
2010-09-03 12:17:00 -07:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
_getFallbackService: function _getFallbackService() {
|
|
|
|
return Components.classesByID["{7ad1b327-6dfa-46ec-9234-f2a620ea7e00}"]
|
|
|
|
.getService(Ci.nsIPromptService);
|
|
|
|
},
|
|
|
|
|
|
|
|
getDocument: function getDocument() {
|
2010-07-13 07:36:09 -07:00
|
|
|
let win = Services.wm.getMostRecentWindow("navigator:browser");
|
2010-06-25 13:29:47 -07:00
|
|
|
return win ? win.document : null;
|
|
|
|
},
|
|
|
|
|
|
|
|
// nsIPromptService and nsIPromptService2 methods proxy to our Prompt class
|
|
|
|
// if we can show in-document popups, or to the fallback service otherwise.
|
|
|
|
callProxy: function(aMethod, aArguments) {
|
2011-01-04 07:35:05 -08:00
|
|
|
let prompt;
|
2010-09-03 12:17:00 -07:00
|
|
|
if (this.inContentProcess) {
|
|
|
|
// Bring this tab to the front, so prompt appears on the right tab
|
|
|
|
var window = aArguments[0];
|
|
|
|
if (window && window.document) {
|
|
|
|
var event = window.document.createEvent("Events");
|
|
|
|
event.initEvent("DOMWillOpenModalDialog", true, false);
|
|
|
|
window.dispatchEvent(event);
|
|
|
|
}
|
2011-01-04 07:35:05 -08:00
|
|
|
prompt = ContentPrompt;
|
|
|
|
} else {
|
|
|
|
let doc = this.getDocument();
|
|
|
|
if (!doc) {
|
|
|
|
let fallback = this._getFallbackService();
|
|
|
|
return fallback[aMethod].apply(fallback, aArguments);
|
2010-09-03 12:17:00 -07:00
|
|
|
}
|
2011-01-04 07:35:05 -08:00
|
|
|
let domWin = aArguments[0];
|
|
|
|
prompt = new Prompt(domWin, doc);
|
2009-11-12 21:09:38 -08:00
|
|
|
}
|
2011-01-04 07:35:05 -08:00
|
|
|
return prompt[aMethod].apply(prompt, Array.prototype.slice.call(aArguments, 1));
|
2009-09-02 09:15:26 -07:00
|
|
|
},
|
2009-11-12 21:09:38 -08:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
/* ---------- nsIPromptService ---------- */
|
2009-11-12 21:09:38 -08:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
alert: function() {
|
2010-10-31 21:02:40 -07:00
|
|
|
return this.callProxy("alert", arguments);
|
2009-09-02 09:15:26 -07:00
|
|
|
},
|
2010-06-25 13:29:47 -07:00
|
|
|
alertCheck: function() {
|
|
|
|
return this.callProxy("alertCheck", arguments);
|
|
|
|
},
|
|
|
|
confirm: function() {
|
|
|
|
return this.callProxy("confirm", arguments);
|
|
|
|
},
|
|
|
|
confirmCheck: function() {
|
|
|
|
return this.callProxy("confirmCheck", arguments);
|
|
|
|
},
|
|
|
|
confirmEx: function() {
|
|
|
|
return this.callProxy("confirmEx", arguments);
|
|
|
|
},
|
|
|
|
prompt: function() {
|
|
|
|
return this.callProxy("prompt", arguments);
|
|
|
|
},
|
|
|
|
promptUsernameAndPassword: function() {
|
|
|
|
return this.callProxy("promptUsernameAndPassword", arguments);
|
|
|
|
},
|
|
|
|
promptPassword: function() {
|
|
|
|
return this.callProxy("promptPassword", arguments);
|
|
|
|
},
|
|
|
|
select: function() {
|
|
|
|
return this.callProxy("select", arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
/* ---------- nsIPromptService2 ---------- */
|
|
|
|
|
|
|
|
promptAuth: function() {
|
|
|
|
return this.callProxy("promptAuth", arguments);
|
|
|
|
},
|
|
|
|
asyncPromptAuth: function() {
|
|
|
|
return this.callProxy("asyncPromptAuth", arguments);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-01-04 07:35:05 -08:00
|
|
|
// Implementation of nsIPrompt that just forwards to the parent process.
|
|
|
|
let ContentPrompt = {
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIPrompt]),
|
|
|
|
|
|
|
|
sendMessage: function sendMessage(aMethod) {
|
|
|
|
let args = Array.prototype.slice.call(arguments);
|
|
|
|
args[0] = null; // No need to pass "window" argument to the prompt service.
|
|
|
|
|
|
|
|
// We send all prompts as sync, even alert (which has no important
|
|
|
|
// return value), as otherwise program flow will continue, and the
|
|
|
|
// script can theoretically show several alerts at once. In particular
|
|
|
|
// this can lead to a bug where you cannot click the earlier one, which
|
|
|
|
// is now hidden by a new one (and Fennec is helplessly frozen).
|
|
|
|
var json = { method: aMethod, arguments: args };
|
|
|
|
var response = this.messageManager.sendSyncMessage("Prompt:Call", json)[0];
|
|
|
|
|
|
|
|
// Args copying - for methods that have out values
|
|
|
|
REMOTABLE_METHODS[aMethod].outParams.forEach(function(i) {
|
|
|
|
args[i].value = response[i].value;
|
|
|
|
});
|
|
|
|
return response.pop(); // final return value was given at the end
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(ContentPrompt, "messageManager",
|
|
|
|
"@mozilla.org/childprocessmessagemanager;1", Ci.nsISyncMessageSender);
|
|
|
|
|
|
|
|
// Add remotable methods to ContentPrompt.
|
|
|
|
for (let [method, _] in Iterator(REMOTABLE_METHODS)) {
|
|
|
|
ContentPrompt[method] = ContentPrompt.sendMessage.bind(ContentPrompt, method);
|
|
|
|
}
|
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
function Prompt(aDomWin, aDocument) {
|
|
|
|
this._domWin = aDomWin;
|
|
|
|
this._doc = aDocument;
|
|
|
|
}
|
|
|
|
|
2010-11-22 18:31:40 -08:00
|
|
|
Prompt.prototype = {
|
2010-06-25 13:29:47 -07:00
|
|
|
_domWin: null,
|
|
|
|
_doc: null,
|
2009-11-12 21:09:38 -08:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIPrompt, Ci.nsIAuthPrompt, Ci.nsIAuthPrompt2]),
|
|
|
|
|
|
|
|
/* ---------- internal methods ---------- */
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
openDialog: function openDialog(aSrc, aParams) {
|
2010-07-13 07:36:09 -07:00
|
|
|
let browser = Services.wm.getMostRecentWindow("navigator:browser");
|
2010-06-25 13:29:47 -07:00
|
|
|
return browser.importDialog(this._domWin, aSrc, aParams);
|
|
|
|
},
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2011-04-11 16:35:00 -07:00
|
|
|
_setupPrompt: function setupPrompt(aDoc, aType, aTitle, aText, aCheck) {
|
|
|
|
aDoc.getElementById("prompt-" + aType + "-title").appendChild(aDoc.createTextNode(aTitle));
|
|
|
|
aDoc.getElementById("prompt-" + aType + "-message").appendChild(aDoc.createTextNode(aText));
|
|
|
|
|
|
|
|
if (aCheck && aCheck.msg) {
|
|
|
|
aDoc.getElementById("prompt-" + aType + "-checkbox").checked = aCheck.value;
|
|
|
|
this.setLabelForNode(aDoc.getElementById("prompt-" + aType + "-checkbox-label"), aCheck.msg);
|
|
|
|
aDoc.getElementById("prompt-" + aType + "-checkbox").removeAttribute("collapsed");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
commonPrompt: function commonPrompt(aTitle, aText, aValue, aCheckMsg, aCheckState, isPassword) {
|
2009-09-02 09:15:26 -07:00
|
|
|
var params = new Object();
|
|
|
|
params.result = false;
|
|
|
|
params.checkbox = aCheckState;
|
2010-06-25 13:29:47 -07:00
|
|
|
params.value = aValue;
|
2009-11-12 21:09:38 -08:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
let dialog = this.openDialog("chrome://browser/content/prompt/prompt.xul", params);
|
|
|
|
let doc = this._doc;
|
2011-04-11 16:35:00 -07:00
|
|
|
this._setupPrompt(doc, "prompt", aTitle, aText, {value: aCheckState.value, msg: aCheckMsg});
|
2010-06-25 13:29:47 -07:00
|
|
|
doc.getElementById("prompt-prompt-textbox").value = aValue.value;
|
|
|
|
if (isPassword)
|
|
|
|
doc.getElementById("prompt-prompt-textbox").type = "password";
|
2009-09-04 10:40:03 -07:00
|
|
|
|
2009-09-02 09:15:26 -07:00
|
|
|
dialog.waitForClose();
|
|
|
|
return params.result;
|
|
|
|
},
|
2010-06-25 13:29:47 -07:00
|
|
|
|
2009-09-02 09:15:26 -07:00
|
|
|
//
|
|
|
|
// Copied from chrome://global/content/commonDialog.js
|
|
|
|
//
|
2010-06-25 13:29:47 -07:00
|
|
|
setLabelForNode: function setLabelForNode(aNode, aLabel) {
|
2009-09-02 09:15:26 -07:00
|
|
|
// This is for labels which may contain embedded access keys.
|
|
|
|
// If we end in (&X) where X represents the access key, optionally preceded
|
|
|
|
// by spaces and/or followed by the ':' character, store the access key and
|
|
|
|
// remove the access key placeholder + leading spaces from the label.
|
|
|
|
// Otherwise a character preceded by one but not two &s is the access key.
|
|
|
|
// Store it and remove the &.
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2009-09-02 09:15:26 -07:00
|
|
|
// Note that if you change the following code, see the comment of
|
|
|
|
// nsTextBoxFrame::UpdateAccessTitle.
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2009-09-24 22:58:08 -07:00
|
|
|
if (!aLabel)
|
|
|
|
return;
|
|
|
|
|
2009-09-02 09:15:26 -07:00
|
|
|
var accessKey = null;
|
|
|
|
if (/ *\(\&([^&])\)(:)?$/.test(aLabel)) {
|
|
|
|
aLabel = RegExp.leftContext + RegExp.$2;
|
|
|
|
accessKey = RegExp.$1;
|
|
|
|
} else if (/^(.*[^&])?\&(([^&]).*$)/.test(aLabel)) {
|
|
|
|
aLabel = RegExp.$1 + RegExp.$2;
|
|
|
|
accessKey = RegExp.$3;
|
|
|
|
}
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2009-09-02 09:15:26 -07:00
|
|
|
// && is the magic sequence to embed an & in your label.
|
|
|
|
aLabel = aLabel.replace(/\&\&/g, "&");
|
2009-09-22 21:58:34 -07:00
|
|
|
if (aNode instanceof Ci.nsIDOMXULLabelElement) {
|
2009-09-02 09:15:26 -07:00
|
|
|
aNode.setAttribute("value", aLabel);
|
2009-09-22 21:58:34 -07:00
|
|
|
} else if (aNode instanceof Ci.nsIDOMXULDescriptionElement) {
|
2009-10-09 11:26:45 -07:00
|
|
|
let text = aNode.ownerDocument.createTextNode(aLabel);
|
2009-09-22 21:58:34 -07:00
|
|
|
aNode.appendChild(text);
|
2009-09-02 09:15:26 -07:00
|
|
|
} else { // Set text for other xul elements
|
|
|
|
aNode.setAttribute("label", aLabel);
|
|
|
|
}
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2009-09-02 09:15:26 -07:00
|
|
|
// XXXjag bug 325251
|
|
|
|
// Need to set this after aNode.setAttribute("value", aLabel);
|
|
|
|
if (accessKey)
|
|
|
|
aNode.setAttribute("accesskey", accessKey);
|
|
|
|
},
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
/*
|
|
|
|
* ---------- interface disambiguation ----------
|
|
|
|
*
|
|
|
|
* XXX Copied from nsPrompter.js.
|
|
|
|
*
|
|
|
|
* nsIPrompt and nsIAuthPrompt share 3 method names with slightly
|
|
|
|
* different arguments. All but prompt() have the same number of
|
|
|
|
* arguments, so look at the arg types to figure out how we're being
|
|
|
|
* called. :-(
|
|
|
|
*/
|
|
|
|
prompt: function prompt() {
|
2010-11-22 18:31:40 -08:00
|
|
|
if (gPromptService.inContentProcess)
|
|
|
|
return gPromptService.callProxy("prompt", [null].concat(Array.prototype.slice.call(arguments)));
|
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
// also, the nsIPrompt flavor has 5 args instead of 6.
|
|
|
|
if (typeof arguments[2] == "object")
|
|
|
|
return this.nsIPrompt_prompt.apply(this, arguments);
|
|
|
|
else
|
|
|
|
return this.nsIAuthPrompt_prompt.apply(this, arguments);
|
|
|
|
},
|
2009-09-02 09:15:26 -07:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
promptUsernameAndPassword: function promptUsernameAndPassword() {
|
|
|
|
// Both have 6 args, so use types.
|
|
|
|
if (typeof arguments[2] == "object")
|
|
|
|
return this.nsIPrompt_promptUsernameAndPassword.apply(this, arguments);
|
|
|
|
else
|
|
|
|
return this.nsIAuthPrompt_promptUsernameAndPassword.apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
promptPassword: function promptPassword() {
|
|
|
|
// Both have 5 args, so use types.
|
|
|
|
if (typeof arguments[2] == "object")
|
|
|
|
return this.nsIPrompt_promptPassword.apply(this, arguments);
|
|
|
|
else
|
|
|
|
return this.nsIAuthPrompt_promptPassword.apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
/* ---------- nsIPrompt ---------- */
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
alert: function alert(aTitle, aText) {
|
|
|
|
let dialog = this.openDialog("chrome://browser/content/prompt/alert.xul", null);
|
|
|
|
let doc = this._doc;
|
2011-04-11 16:35:00 -07:00
|
|
|
this._setupPrompt(doc, "alert", aTitle, aText);
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
dialog.waitForClose();
|
|
|
|
},
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
alertCheck: function alertCheck(aTitle, aText, aCheckMsg, aCheckState) {
|
|
|
|
let dialog = this.openDialog("chrome://browser/content/prompt/alert.xul", aCheckState);
|
|
|
|
let doc = this._doc;
|
2011-04-11 16:35:00 -07:00
|
|
|
this._setupPrompt(doc, "alert", aTitle, aText, {value: aCheckState.value, msg: aCheckMsg});
|
2010-06-25 13:29:47 -07:00
|
|
|
dialog.waitForClose();
|
|
|
|
},
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
confirm: function confirm(aTitle, aText) {
|
|
|
|
var params = new Object();
|
|
|
|
params.result = false;
|
|
|
|
|
|
|
|
let dialog = this.openDialog("chrome://browser/content/prompt/confirm.xul", params);
|
|
|
|
let doc = this._doc;
|
2011-04-11 16:35:00 -07:00
|
|
|
this._setupPrompt(doc, "confirm", aTitle, aText);
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
dialog.waitForClose();
|
|
|
|
return params.result;
|
|
|
|
},
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
confirmCheck: function confirmCheck(aTitle, aText, aCheckMsg, aCheckState) {
|
|
|
|
var params = new Object();
|
|
|
|
params.result = false;
|
|
|
|
params.checkbox = aCheckState;
|
|
|
|
|
|
|
|
let dialog = this.openDialog("chrome://browser/content/prompt/confirm.xul", params);
|
|
|
|
let doc = this._doc;
|
2011-04-11 16:35:00 -07:00
|
|
|
this._setupPrompt(doc, "prompt", aTitle, aText, {value: aCheckState.value, msg: aCheckMsg});
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
dialog.waitForClose();
|
|
|
|
return params.result;
|
|
|
|
},
|
2009-11-12 21:09:38 -08:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
confirmEx: function confirmEx(aTitle, aText, aButtonFlags, aButton0,
|
|
|
|
aButton1, aButton2, aCheckMsg, aCheckState) {
|
2010-11-12 01:17:00 -08:00
|
|
|
|
2009-09-02 09:15:26 -07:00
|
|
|
let numButtons = 0;
|
|
|
|
let titles = [aButton0, aButton1, aButton2];
|
2010-04-08 21:59:23 -07:00
|
|
|
|
|
|
|
let defaultButton = 0;
|
|
|
|
if (aButtonFlags & Ci.nsIPromptService.BUTTON_POS_1_DEFAULT)
|
|
|
|
defaultButton = 1;
|
|
|
|
if (aButtonFlags & Ci.nsIPromptService.BUTTON_POS_2_DEFAULT)
|
|
|
|
defaultButton = 2;
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-04-08 21:59:23 -07:00
|
|
|
var params = {
|
|
|
|
result: false,
|
|
|
|
checkbox: aCheckState,
|
|
|
|
defaultButton: defaultButton
|
|
|
|
}
|
2009-11-12 21:09:38 -08:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
let dialog = this.openDialog("chrome://browser/content/prompt/confirm.xul", params);
|
|
|
|
let doc = this._doc;
|
2011-04-11 16:35:00 -07:00
|
|
|
this._setupPrompt(doc, "confirm", aTitle, aText, {value: aCheckState.value, msg: aCheckMsg});
|
2010-05-05 06:21:35 -07:00
|
|
|
|
2010-04-29 22:00:27 -07:00
|
|
|
let bbox = doc.getElementById("prompt-confirm-buttons-box");
|
2010-05-05 06:21:35 -07:00
|
|
|
while (bbox.lastChild)
|
2009-09-02 09:15:26 -07:00
|
|
|
bbox.removeChild(bbox.lastChild);
|
2010-05-05 06:21:35 -07:00
|
|
|
|
2009-09-02 09:15:26 -07:00
|
|
|
for (let i = 0; i < 3; i++) {
|
|
|
|
let bTitle = null;
|
|
|
|
switch (aButtonFlags & 0xff) {
|
|
|
|
case Ci.nsIPromptService.BUTTON_TITLE_OK :
|
2010-06-25 13:29:47 -07:00
|
|
|
bTitle = PromptUtils.getLocaleString("OK");
|
2009-09-02 09:15:26 -07:00
|
|
|
break;
|
|
|
|
case Ci.nsIPromptService.BUTTON_TITLE_CANCEL :
|
2010-06-25 13:29:47 -07:00
|
|
|
bTitle = PromptUtils.getLocaleString("Cancel");
|
2009-09-02 09:15:26 -07:00
|
|
|
break;
|
|
|
|
case Ci.nsIPromptService.BUTTON_TITLE_YES :
|
2010-06-25 13:29:47 -07:00
|
|
|
bTitle = PromptUtils.getLocaleString("Yes");
|
2009-09-02 09:15:26 -07:00
|
|
|
break;
|
|
|
|
case Ci.nsIPromptService.BUTTON_TITLE_NO :
|
2010-06-25 13:29:47 -07:00
|
|
|
bTitle = PromptUtils.getLocaleString("No");
|
2009-09-02 09:15:26 -07:00
|
|
|
break;
|
|
|
|
case Ci.nsIPromptService.BUTTON_TITLE_SAVE :
|
2010-06-25 13:29:47 -07:00
|
|
|
bTitle = PromptUtils.getLocaleString("Save");
|
2009-09-02 09:15:26 -07:00
|
|
|
break;
|
|
|
|
case Ci.nsIPromptService.BUTTON_TITLE_DONT_SAVE :
|
2010-06-25 13:29:47 -07:00
|
|
|
bTitle = PromptUtils.getLocaleString("DontSave");
|
2009-09-02 09:15:26 -07:00
|
|
|
break;
|
|
|
|
case Ci.nsIPromptService.BUTTON_TITLE_REVERT :
|
2010-06-25 13:29:47 -07:00
|
|
|
bTitle = PromptUtils.getLocaleString("Revert");
|
2009-09-02 09:15:26 -07:00
|
|
|
break;
|
|
|
|
case Ci.nsIPromptService.BUTTON_TITLE_IS_STRING :
|
|
|
|
bTitle = titles[i];
|
|
|
|
break;
|
|
|
|
}
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2009-09-02 09:15:26 -07:00
|
|
|
if (bTitle) {
|
|
|
|
let button = doc.createElement("button");
|
2010-07-30 12:03:03 -07:00
|
|
|
button.className = "prompt-button";
|
2009-09-22 21:58:34 -07:00
|
|
|
this.setLabelForNode(button, bTitle);
|
2010-04-15 07:40:43 -07:00
|
|
|
if (i == defaultButton) {
|
2010-11-22 18:31:40 -08:00
|
|
|
button.setAttribute("command", "cmd_ok");
|
2010-04-15 07:40:43 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
button.setAttribute("oncommand",
|
|
|
|
"document.getElementById('prompt-confirm-dialog').PromptHelper.closeConfirm(" + i + ")");
|
|
|
|
}
|
2009-09-02 09:15:26 -07:00
|
|
|
bbox.appendChild(button);
|
|
|
|
}
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2009-09-02 09:15:26 -07:00
|
|
|
aButtonFlags >>= 8;
|
|
|
|
}
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2009-09-02 09:15:26 -07:00
|
|
|
dialog.waitForClose();
|
|
|
|
return params.result;
|
|
|
|
},
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
nsIPrompt_prompt: function nsIPrompt_prompt(aTitle, aText, aValue, aCheckMsg, aCheckState) {
|
|
|
|
return this.commonPrompt(aTitle, aText, aValue, aCheckMsg, aCheckState, false);
|
2009-09-02 09:15:26 -07:00
|
|
|
},
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
nsIPrompt_promptPassword: function nsIPrompt_promptPassword(
|
|
|
|
aTitle, aText, aPassword, aCheckMsg, aCheckState) {
|
|
|
|
return this.commonPrompt(aTitle, aText, aPassword, aCheckMsg, aCheckState, true);
|
2009-09-02 09:15:26 -07:00
|
|
|
},
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
nsIPrompt_promptUsernameAndPassword: function nsIPrompt_promptUsernameAndPassword(
|
|
|
|
aTitle, aText, aUsername, aPassword, aCheckMsg, aCheckState) {
|
2009-09-02 09:15:26 -07:00
|
|
|
var params = new Object();
|
|
|
|
params.result = false;
|
|
|
|
params.checkbox = aCheckState;
|
|
|
|
params.user = aUsername;
|
|
|
|
params.password = aPassword;
|
2009-11-12 21:09:38 -08:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
let dialog = this.openDialog("chrome://browser/content/prompt/promptPassword.xul", params);
|
|
|
|
let doc = this._doc;
|
2011-04-11 16:35:00 -07:00
|
|
|
this._setupPrompt(doc, "password", aTitle, aText, {value: aCheckState.value, msg: aCheckMsg});
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2009-09-02 09:15:26 -07:00
|
|
|
doc.getElementById("prompt-password-user").value = aUsername.value;
|
|
|
|
doc.getElementById("prompt-password-password").value = aPassword.value;
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2009-09-02 09:15:26 -07:00
|
|
|
dialog.waitForClose();
|
|
|
|
return params.result;
|
|
|
|
},
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
select: function select(aTitle, aText, aCount, aSelectList, aOutSelection) {
|
|
|
|
var params = new Object();
|
|
|
|
params.result = false;
|
|
|
|
params.selection = aOutSelection;
|
|
|
|
|
|
|
|
let dialog = this.openDialog("chrome://browser/content/prompt/select.xul", params);
|
|
|
|
let doc = this._doc;
|
2011-04-11 16:35:00 -07:00
|
|
|
this._setupPrompt(doc, "select", aTitle, aText);
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
let list = doc.getElementById("prompt-select-list");
|
|
|
|
for (let i = 0; i < aCount; i++)
|
|
|
|
list.appendItem(aSelectList[i], null, null);
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
// select the first one
|
|
|
|
list.selectedIndex = 0;
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
dialog.waitForClose();
|
|
|
|
return params.result;
|
|
|
|
},
|
|
|
|
|
|
|
|
/* ---------- nsIAuthPrompt ---------- */
|
|
|
|
|
|
|
|
nsIAuthPrompt_prompt : function (title, text, passwordRealm, savePassword, defaultText, result) {
|
2010-09-14 14:58:10 -07:00
|
|
|
// TODO: Port functions from nsLoginManagerPrompter.js to here
|
2010-06-25 13:29:47 -07:00
|
|
|
if (defaultText)
|
|
|
|
result.value = defaultText;
|
|
|
|
return this.nsIPrompt_prompt(title, text, result, null, {});
|
|
|
|
},
|
|
|
|
|
2010-09-14 14:58:13 -07:00
|
|
|
nsIAuthPrompt_promptUsernameAndPassword : function (aTitle, aText, aPasswordRealm, aSavePassword, aUser, aPass) {
|
|
|
|
return nsIAuthPrompt_loginPrompt(aTitle, aText, aPasswordRealm, aSavePassword, aUser, aPass);
|
2010-06-25 13:29:47 -07:00
|
|
|
},
|
|
|
|
|
2010-09-14 14:58:13 -07:00
|
|
|
nsIAuthPrompt_promptPassword : function (aTitle, aText, aPasswordRealm, aSavePassword, aPass) {
|
|
|
|
return nsIAuthPrompt_loginPrompt(aTitle, aText, aPasswordRealm, aSavePassword, null, aPass);
|
2010-06-25 13:29:47 -07:00
|
|
|
},
|
|
|
|
|
2010-09-14 14:58:13 -07:00
|
|
|
nsIAuthPrompt_loginPrompt: function(aTitle, aPasswordRealm, aSavePassword, aUser, aPass) {
|
|
|
|
let checkMsg = null;
|
|
|
|
let check = { value: false };
|
|
|
|
let [hostname, realm, aUser] = PromptUtils.getHostnameAndRealm(aPasswordRealm);
|
|
|
|
|
|
|
|
let canSave = PromptUtils.canSaveLogin(hostname, aSavePassword);
|
|
|
|
if (canSave) {
|
|
|
|
// Look for existing logins.
|
|
|
|
let foundLogins = PromptUtils.pwmgr.findLogins({}, hostname, null, realm);
|
|
|
|
[checkMsg, check] = PromptUtils.getUsernameAndPassword(foundLogins, aUser, aPass);
|
|
|
|
}
|
|
|
|
|
|
|
|
let ok = false;
|
|
|
|
if (aUser)
|
|
|
|
ok = this.nsIPrompt_promptUsernameAndPassword(aTitle, aText, aUser, aPass, checkMsg, check);
|
|
|
|
else
|
|
|
|
ok = this.nsIPrompt_promptPassword(aTitle, aText, aPass, checkMsg, check);
|
|
|
|
|
|
|
|
if (ok && canSave && check.value)
|
|
|
|
PromptUtils.savePassword(hostname, realm, aUser, aPass);
|
|
|
|
|
|
|
|
return ok; },
|
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
/* ---------- nsIAuthPrompt2 ---------- */
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-09-14 14:58:10 -07:00
|
|
|
promptAuth: function promptAuth(aChannel, aLevel, aAuthInfo) {
|
2010-09-14 14:58:13 -07:00
|
|
|
let checkMsg = null;
|
|
|
|
let check = { value: false };
|
2010-06-25 13:29:47 -07:00
|
|
|
let message = PromptUtils.makeDialogText(aChannel, aAuthInfo);
|
2010-09-14 14:58:13 -07:00
|
|
|
let [username, password] = PromptUtils.getAuthInfo(aAuthInfo);
|
|
|
|
let [hostname, httpRealm] = PromptUtils.getAuthTarget(aChannel, aAuthInfo);
|
|
|
|
let foundLogins = PromptUtils.pwmgr.findLogins({}, hostname, null, httpRealm);
|
|
|
|
|
|
|
|
let canSave = PromptUtils.canSaveLogin(hostname, null);
|
|
|
|
if (canSave)
|
|
|
|
[checkMsg, check] = PromptUtils.getUsernameAndPassword(foundLogins, username, password);
|
|
|
|
|
|
|
|
if (username.value && password.value) {
|
|
|
|
PromptUtils.setAuthInfo(aAuthInfo, username.value, password.value);
|
|
|
|
}
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-09-14 14:58:13 -07:00
|
|
|
let ok;
|
|
|
|
if (aAuthInfo.flags & Ci.nsIAuthInformation.ONLY_PASSWORD)
|
|
|
|
ok = this.nsIPrompt_promptPassword(null, message, password, checkMsg, check);
|
2010-06-25 13:29:47 -07:00
|
|
|
else
|
2010-09-14 14:58:13 -07:00
|
|
|
ok = this.nsIPrompt_promptUsernameAndPassword(null, message, username, password, checkMsg, check);
|
|
|
|
|
|
|
|
PromptUtils.setAuthInfo(aAuthInfo, username.value, password.value);
|
|
|
|
|
|
|
|
if (ok && canSave && check.value) {
|
|
|
|
PromptUtils.savePassword(foundLogins, username, password, hostname, httpRealm);
|
2009-09-04 10:40:03 -07:00
|
|
|
}
|
2010-09-14 14:58:13 -07:00
|
|
|
|
|
|
|
return ok;
|
2009-09-04 10:40:03 -07:00
|
|
|
},
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-09-14 14:58:10 -07:00
|
|
|
asyncPromptAuth: function asyncPromptAuth(aChannel, aCallback, aContext, aLevel, aAuthInfo) {
|
2010-06-25 13:29:47 -07:00
|
|
|
// bug 514196
|
|
|
|
throw Cr.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let PromptUtils = {
|
2010-09-14 14:58:13 -07:00
|
|
|
getLocaleString: function pu_getLocaleString(aKey, aService) {
|
|
|
|
if (aService == "passwdmgr")
|
2010-09-14 14:58:10 -07:00
|
|
|
return this.passwdBundle.GetStringFromName(aKey);
|
|
|
|
|
|
|
|
return this.bundle.GetStringFromName(aKey);
|
2010-06-25 13:29:47 -07:00
|
|
|
},
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-09-14 14:58:13 -07:00
|
|
|
get pwmgr() {
|
|
|
|
delete this.pwmgr;
|
|
|
|
return this.pwmgr = Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager);
|
|
|
|
},
|
|
|
|
|
|
|
|
getHostnameAndRealm: function pu_getHostnameAndRealm(aRealmString) {
|
|
|
|
let httpRealm = /^.+ \(.+\)$/;
|
|
|
|
if (httpRealm.test(aRealmString))
|
|
|
|
return [null, null, null];
|
|
|
|
|
|
|
|
let uri = Services.io.newURI(aRealmString, null, null);
|
|
|
|
let pathname = "";
|
|
|
|
|
|
|
|
if (uri.path != "/")
|
|
|
|
pathname = uri.path;
|
|
|
|
|
|
|
|
let formattedHostname = this._getFormattedHostname(uri);
|
|
|
|
return [formattedHostname, formattedHostname + pathname, uri.username];
|
|
|
|
},
|
|
|
|
|
|
|
|
canSaveLogin: function pu_canSaveLogin(aHostname, aSavePassword) {
|
|
|
|
let canSave = !this._inPrivateBrowsing && this.pwmgr.getLoginSavingEnabled(aHostname)
|
|
|
|
if (aSavePassword)
|
|
|
|
canSave = canSave && (aSavePassword == Ci.nsIAuthPrompt.SAVE_PASSWORD_PERMANENTLY)
|
|
|
|
return canSave;
|
|
|
|
},
|
|
|
|
|
|
|
|
getUsernameAndPassword: function pu_getUsernameAndPassword(aFoundLogins, aUser, aPass) {
|
|
|
|
let checkLabel = null;
|
|
|
|
let check = { value: false };
|
|
|
|
let selectedLogin;
|
|
|
|
|
|
|
|
checkLabel = this.getLocaleString("rememberPassword", "passwdmgr");
|
|
|
|
|
|
|
|
// XXX Like the original code, we can't deal with multiple
|
|
|
|
// account selection. (bug 227632)
|
|
|
|
if (aFoundLogins.length > 0) {
|
|
|
|
selectedLogin = aFoundLogins[0];
|
|
|
|
|
|
|
|
// If the caller provided a username, try to use it. If they
|
|
|
|
// provided only a password, this will try to find a password-only
|
|
|
|
// login (or return null if none exists).
|
|
|
|
if (aUser.value)
|
|
|
|
selectedLogin = this.findLogin(aFoundLogins, "username", aUser.value);
|
|
|
|
|
|
|
|
if (selectedLogin) {
|
|
|
|
check.value = true;
|
|
|
|
aUser.value = selectedLogin.username;
|
|
|
|
// If the caller provided a password, prefer it.
|
|
|
|
if (!aPass.value)
|
|
|
|
aPass.value = selectedLogin.password;
|
|
|
|
}
|
2009-09-04 10:40:03 -07:00
|
|
|
}
|
2010-09-14 14:58:13 -07:00
|
|
|
|
|
|
|
return [checkLabel, check];
|
|
|
|
},
|
|
|
|
|
|
|
|
findLogin: function pu_findLogin(aLogins, aName, aValue) {
|
|
|
|
for (let i = 0; i < aLogins.length; i++)
|
|
|
|
if (aLogins[i][aName] == aValue)
|
|
|
|
return aLogins[i];
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
savePassword: function pu_savePassword(aLogins, aUser, aPass, aHostname, aRealm) {
|
|
|
|
let selectedLogin = this.findLogin(aLogins, "username", aUser.value);
|
|
|
|
|
|
|
|
// If we didn't find an existing login, or if the username
|
|
|
|
// changed, save as a new login.
|
|
|
|
if (!selectedLogin) {
|
|
|
|
// add as new
|
|
|
|
var newLogin = Cc["@mozilla.org/login-manager/loginInfo;1"].createInstance(Ci.nsILoginInfo);
|
|
|
|
newLogin.init(aHostname, null, aRealm, aUser.value, aPass.value, "", "");
|
|
|
|
this.pwmgr.addLogin(newLogin);
|
|
|
|
} else if (aPass.value != selectedLogin.password) {
|
|
|
|
// update password
|
|
|
|
this.updateLogin(selectedLogin, aPass.value);
|
2009-09-04 10:40:03 -07:00
|
|
|
} else {
|
2010-09-14 14:58:13 -07:00
|
|
|
this.updateLogin(selectedLogin);
|
2009-09-04 10:40:03 -07:00
|
|
|
}
|
2010-09-14 14:58:13 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
updateLogin: function pu_updateLogin(aLogin, aPassword) {
|
|
|
|
let now = Date.now();
|
|
|
|
let propBag = Cc["@mozilla.org/hash-property-bag;1"].createInstance(Ci.nsIWritablePropertyBag);
|
|
|
|
if (aPassword) {
|
|
|
|
propBag.setProperty("password", aPassword);
|
|
|
|
// Explicitly set the password change time here (even though it would
|
|
|
|
// be changed automatically), to ensure that it's exactly the same
|
|
|
|
// value as timeLastUsed.
|
|
|
|
propBag.setProperty("timePasswordChanged", now);
|
2009-09-04 10:40:03 -07:00
|
|
|
}
|
2010-09-14 14:58:13 -07:00
|
|
|
propBag.setProperty("timeLastUsed", now);
|
|
|
|
propBag.setProperty("timesUsedIncrement", 1);
|
|
|
|
|
|
|
|
this.pwmgr.modifyLogin(aLogin, propBag);
|
|
|
|
},
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-09-14 14:58:13 -07:00
|
|
|
// JS port of http://mxr.mozilla.org/mozilla-central/source/embedding/components/windowwatcher/src/nsPrompt.cpp#388
|
|
|
|
makeDialogText: function pu_makeDialogText(aChannel, aAuthInfo) {
|
|
|
|
let isProxy = (aAuthInfo.flags & Ci.nsIAuthInformation.AUTH_PROXY);
|
|
|
|
let isPassOnly = (aAuthInfo.flags & Ci.nsIAuthInformation.ONLY_PASSWORD);
|
|
|
|
|
|
|
|
let username = aAuthInfo.username;
|
|
|
|
let [displayHost, realm] = this.getAuthTarget(aChannel, aAuthInfo);
|
|
|
|
|
|
|
|
// Suppress "the site says: $realm" when we synthesized a missing realm.
|
|
|
|
if (!aAuthInfo.realm && !isProxy)
|
|
|
|
realm = "";
|
|
|
|
|
|
|
|
// Trim obnoxiously long realms.
|
|
|
|
if (realm.length > 150) {
|
|
|
|
realm = realm.substring(0, 150);
|
|
|
|
// Append "..." (or localized equivalent).
|
|
|
|
realm += this.ellipsis;
|
|
|
|
}
|
|
|
|
|
|
|
|
let text;
|
|
|
|
if (isProxy)
|
|
|
|
text = this.bundle.formatStringFromName("EnterLoginForProxy", [realm, displayHost], 2);
|
|
|
|
else if (isPassOnly)
|
|
|
|
text = this.bundle.formatStringFromName("EnterPasswordFor", [username, displayHost], 2);
|
|
|
|
else if (!realm)
|
|
|
|
text = this.bundle.formatStringFromName("EnterUserPasswordFor", [displayHost], 1);
|
|
|
|
else
|
|
|
|
text = this.bundle.formatStringFromName("EnterLoginForRealm", [realm, displayHost], 2);
|
|
|
|
|
|
|
|
return text;
|
2009-09-04 10:40:03 -07:00
|
|
|
},
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
// JS port of http://mxr.mozilla.org/mozilla-central/source/embedding/components/windowwatcher/public/nsPromptUtils.h#89
|
2010-09-14 14:58:13 -07:00
|
|
|
getAuthHostPort: function pu_getAuthHostPort(aChannel, aAuthInfo) {
|
2010-06-25 13:29:47 -07:00
|
|
|
let uri = aChannel.URI;
|
|
|
|
let res = { host: null, port: -1 };
|
|
|
|
if (aAuthInfo.flags & aAuthInfo.AUTH_PROXY) {
|
|
|
|
let proxy = aChannel.QueryInterface(Ci.nsIProxiedChannel);
|
|
|
|
res.host = proxy.proxyInfo.host;
|
|
|
|
res.port = proxy.proxyInfo.port;
|
|
|
|
} else {
|
|
|
|
res.host = uri.host;
|
|
|
|
res.port = uri.port;
|
2009-09-04 10:40:03 -07:00
|
|
|
}
|
|
|
|
return res;
|
2010-09-14 14:58:13 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
getAuthTarget : function pu_getAuthTarget(aChannel, aAuthInfo) {
|
|
|
|
let hostname, realm;
|
|
|
|
// If our proxy is demanding authentication, don't use the
|
|
|
|
// channel's actual destination.
|
|
|
|
if (aAuthInfo.flags & Ci.nsIAuthInformation.AUTH_PROXY) {
|
|
|
|
if (!(aChannel instanceof Ci.nsIProxiedChannel))
|
|
|
|
throw "proxy auth needs nsIProxiedChannel";
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-09-14 14:58:13 -07:00
|
|
|
let info = aChannel.proxyInfo;
|
|
|
|
if (!info)
|
|
|
|
throw "proxy auth needs nsIProxyInfo";
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-09-14 14:58:13 -07:00
|
|
|
// Proxies don't have a scheme, but we'll use "moz-proxy://"
|
|
|
|
// so that it's more obvious what the login is for.
|
|
|
|
let idnService = Cc["@mozilla.org/network/idn-service;1"].getService(Ci.nsIIDNService);
|
|
|
|
hostname = "moz-proxy://" + idnService.convertUTF8toACE(info.host) + ":" + info.port;
|
|
|
|
realm = aAuthInfo.realm;
|
|
|
|
if (!realm)
|
|
|
|
realm = hostname;
|
2010-11-22 18:31:40 -08:00
|
|
|
|
2010-09-14 14:58:13 -07:00
|
|
|
return [hostname, realm];
|
|
|
|
}
|
|
|
|
hostname = this.getFormattedHostname(aChannel.URI);
|
|
|
|
|
|
|
|
// If a HTTP WWW-Authenticate header specified a realm, that value
|
|
|
|
// will be available here. If it wasn't set or wasn't HTTP, we'll use
|
|
|
|
// the formatted hostname instead.
|
|
|
|
realm = aAuthInfo.realm;
|
|
|
|
if (!realm)
|
|
|
|
realm = hostname;
|
|
|
|
|
|
|
|
return [hostname, realm];
|
|
|
|
},
|
|
|
|
|
|
|
|
getAuthInfo : function pu_getAuthInfo(aAuthInfo) {
|
|
|
|
let flags = aAuthInfo.flags;
|
|
|
|
let username = {value: ""};
|
|
|
|
let password = {value: ""};
|
|
|
|
|
|
|
|
if (flags & Ci.nsIAuthInformation.NEED_DOMAIN && aAuthInfo.domain)
|
|
|
|
username.value = aAuthInfo.domain + "\\" + aAuthInfo.username;
|
|
|
|
else
|
|
|
|
username.value = aAuthInfo.username;
|
|
|
|
|
|
|
|
password.value = aAuthInfo.password
|
|
|
|
|
|
|
|
return [username, password];
|
|
|
|
},
|
|
|
|
|
|
|
|
setAuthInfo : function (aAuthInfo, username, password) {
|
|
|
|
var flags = aAuthInfo.flags;
|
|
|
|
if (flags & Ci.nsIAuthInformation.NEED_DOMAIN) {
|
|
|
|
// Domain is separated from username by a backslash
|
|
|
|
var idx = username.indexOf("\\");
|
|
|
|
if (idx == -1) {
|
|
|
|
aAuthInfo.username = username;
|
|
|
|
} else {
|
|
|
|
aAuthInfo.domain = username.substring(0, idx);
|
|
|
|
aAuthInfo.username = username.substring(idx+1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
aAuthInfo.username = username;
|
|
|
|
}
|
|
|
|
aAuthInfo.password = password;
|
|
|
|
},
|
|
|
|
|
|
|
|
getFormattedHostname : function pu_getFormattedHostname(uri) {
|
|
|
|
let scheme = uri.scheme;
|
|
|
|
let hostname = scheme + "://" + uri.host;
|
|
|
|
|
|
|
|
// If the URI explicitly specified a port, only include it when
|
|
|
|
// it's not the default. (We never want "http://foo.com:80")
|
|
|
|
port = uri.port;
|
|
|
|
if (port != -1) {
|
|
|
|
let handler = Services.io.getProtocolHandler(scheme);
|
|
|
|
if (port != handler.defaultPort)
|
|
|
|
hostname += ":" + port;
|
|
|
|
}
|
|
|
|
return hostname;
|
|
|
|
},
|
2009-09-02 09:15:26 -07:00
|
|
|
};
|
|
|
|
|
2010-09-14 14:58:10 -07:00
|
|
|
XPCOMUtils.defineLazyGetter(PromptUtils, "passwdBundle", function () {
|
|
|
|
return Services.strings.createBundle("chrome://passwordmgr/locale/passwordmgr.properties");
|
|
|
|
});
|
|
|
|
|
2010-06-25 13:29:47 -07:00
|
|
|
XPCOMUtils.defineLazyGetter(PromptUtils, "bundle", function () {
|
2010-07-13 07:36:09 -07:00
|
|
|
return Services.strings.createBundle("chrome://global/locale/commonDialogs.properties");
|
2010-06-25 13:29:47 -07:00
|
|
|
});
|
|
|
|
|
2010-06-29 14:02:25 -07:00
|
|
|
const NSGetFactory = XPCOMUtils.generateNSGetFactory([PromptService]);
|