2007-07-30 15:59:41 -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.
|
|
|
|
*
|
|
|
|
* The Original Code is the Mozilla Community QA Extension
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is the Mozilla Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2006
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Zach Lipton <zach@zachlipton.com>
|
|
|
|
*
|
|
|
|
* 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 ***** */
|
|
|
|
|
|
|
|
|
2007-07-18 13:11:14 -07:00
|
|
|
var qaPref = {
|
|
|
|
litmus : null,
|
|
|
|
prefBase : "qa.extension",
|
|
|
|
|
|
|
|
setPref : function(aName, aValue, aType) {
|
2007-09-10 15:01:06 -07:00
|
|
|
|
2007-07-18 13:11:14 -07:00
|
|
|
var pref = Components.classes["@mozilla.org/preferences-service;1"]
|
2007-09-10 15:01:06 -07:00
|
|
|
.getService(Components.interfaces.nsIPrefBranch);
|
2007-07-18 13:11:14 -07:00
|
|
|
try{
|
|
|
|
if(aType == "bool")
|
|
|
|
pref.setBoolPref(aName, aValue);
|
2007-09-10 15:01:06 -07:00
|
|
|
else if(aType == "int")
|
2007-07-18 13:11:14 -07:00
|
|
|
pref.setIntPref(aName, aValue);
|
|
|
|
else if(aType == "char")
|
|
|
|
pref.setCharPref(aName, aValue);
|
|
|
|
}
|
|
|
|
catch(e){ };
|
|
|
|
},
|
2007-09-10 15:01:06 -07:00
|
|
|
|
2007-07-18 13:11:14 -07:00
|
|
|
getPref : function(aName, aType) {
|
2007-09-10 15:01:06 -07:00
|
|
|
|
2007-07-18 13:11:14 -07:00
|
|
|
var pref = Components.classes["@mozilla.org/preferences-service;1"]
|
2007-09-10 15:01:06 -07:00
|
|
|
.getService(Components.interfaces.nsIPrefBranch);
|
2007-07-18 13:11:14 -07:00
|
|
|
try{
|
|
|
|
var result;
|
|
|
|
if(aType == "bool")
|
|
|
|
result = pref.getBoolPref(aName);
|
2007-09-10 15:01:06 -07:00
|
|
|
else if(aType == "int")
|
2007-07-18 13:11:14 -07:00
|
|
|
result = pref.getIntPref(aName);
|
2007-09-10 15:01:06 -07:00
|
|
|
else if(aType == "char")
|
2007-07-18 13:11:14 -07:00
|
|
|
result = pref.getCharPref(aName);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
catch(e){
|
|
|
|
if(aType == "bool"){
|
|
|
|
return false;
|
2007-09-10 15:01:06 -07:00
|
|
|
}
|
2007-07-18 13:11:14 -07:00
|
|
|
else if(aType == "int"){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if(aType == "char"){
|
2007-09-10 15:01:06 -07:00
|
|
|
return null;
|
2007-07-18 13:11:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2007-09-10 15:01:06 -07:00
|
|
|
}
|
2007-07-18 13:11:14 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// public API:
|
2007-09-10 15:01:06 -07:00
|
|
|
// qaPref.litmus.getUsername()
|
|
|
|
// qaPref.litmus.getPassword()
|
|
|
|
// qaPref.litmus.setPassword()
|
2007-07-18 13:11:14 -07:00
|
|
|
// everything else is abstracted away to deal with fx2 vs fx3 changes
|
|
|
|
|
|
|
|
var CC_passwordManager = Components.classes["@mozilla.org/passwordmanager;1"];
|
|
|
|
var CC_loginManager = Components.classes["@mozilla.org/login-manager;1"];
|
|
|
|
|
2007-09-10 15:01:06 -07:00
|
|
|
if (CC_passwordManager != null) { // old-fashioned password manager
|
|
|
|
qaPref.litmus = {
|
|
|
|
passwordName : function() {return "mozqa-extension-litmus-"+litmus.baseURL},
|
|
|
|
|
|
|
|
manager : function() {
|
|
|
|
return Components.classes["@mozilla.org/passwordmanager;1"].
|
|
|
|
getService(Components.interfaces.nsIPasswordManager);
|
|
|
|
},
|
|
|
|
getPasswordObj : function() {
|
|
|
|
var m = this.manager();
|
|
|
|
var e = m.enumerator;
|
|
|
|
|
|
|
|
// iterate through the PasswordManager service to find
|
|
|
|
// our password:
|
|
|
|
var password;
|
|
|
|
while(e.hasMoreElements()) {
|
|
|
|
try {
|
|
|
|
var pass = e.getNext().
|
|
|
|
QueryInterface(Components.interfaces.nsIPassword);
|
|
|
|
if (pass.host == this.passwordName()) {
|
|
|
|
password=pass; // found it
|
|
|
|
return password;
|
|
|
|
}
|
|
|
|
} catch (ex) { return false; }
|
|
|
|
}
|
|
|
|
if (!password) { return false; }
|
|
|
|
return password;
|
|
|
|
},
|
|
|
|
getUsername : function() {
|
|
|
|
var password = this.getPasswordObj();
|
|
|
|
try {
|
|
|
|
return password.user;
|
|
|
|
} catch (ex) { return false; }
|
|
|
|
},
|
|
|
|
getPassword : function() {
|
|
|
|
var password=this.getPasswordObj();
|
|
|
|
try {
|
|
|
|
return password.password;
|
|
|
|
} catch (ex) { return false; }
|
|
|
|
},
|
|
|
|
setPassword : function(username, password) {
|
|
|
|
var m = this.manager();
|
|
|
|
// check to see whether we already have a password stored
|
|
|
|
// for the current baseURL. If we do, remove it before adding
|
|
|
|
// any new passwords:
|
|
|
|
var p;
|
2008-02-07 15:14:32 -08:00
|
|
|
while (p == this.getPasswordObj()) {
|
2007-09-10 15:01:06 -07:00
|
|
|
m.removeUser(p.host, p.user);
|
|
|
|
}
|
|
|
|
m.addUser(this.passwordName(), username, password);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} else if (CC_loginManager != null) { // use the new login manager interface
|
|
|
|
qaPref.litmus = {
|
|
|
|
manager : function() {
|
|
|
|
return Components.classes["@mozilla.org/login-manager;1"]
|
|
|
|
.getService(Components.interfaces.nsILoginManager);
|
|
|
|
},
|
|
|
|
getUsername : function() {
|
|
|
|
var password = this.getPasswordObj();
|
|
|
|
try {
|
|
|
|
return password.username;
|
|
|
|
} catch (ex) { return false; }
|
|
|
|
},
|
|
|
|
getPassword : function() {
|
|
|
|
var password=this.getPasswordObj();
|
|
|
|
try {
|
|
|
|
return password.password;
|
|
|
|
} catch (ex) { return false; }
|
|
|
|
},
|
|
|
|
setPassword : function(username, password) {
|
|
|
|
// if we already have a password stored, remove it first:
|
|
|
|
var login = this.getPasswordObj();
|
|
|
|
if (login != false) {
|
|
|
|
this.manager().removeLogin(login);
|
|
|
|
}
|
|
|
|
|
|
|
|
var nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1",
|
2007-07-18 13:11:14 -07:00
|
|
|
Components.interfaces.nsILoginInfo,
|
|
|
|
"init");
|
2008-01-23 16:42:43 -08:00
|
|
|
|
|
|
|
// The LoginManager does not like "null" for username/password fields,
|
|
|
|
// So we send in the field names from the XUL.
|
2008-05-16 09:59:37 -07:00
|
|
|
var newLogin = new nsLoginInfo('chrome://qa', null, litmus.baseURL,
|
2008-01-23 16:42:43 -08:00
|
|
|
username, password, "username", "password");
|
|
|
|
try {
|
|
|
|
this.manager().addLogin(newLogin);
|
|
|
|
} catch (err) {
|
|
|
|
alert("ERROR: " + err);
|
|
|
|
}
|
2007-09-10 15:01:06 -07:00
|
|
|
},
|
|
|
|
getPasswordObj: function() {
|
|
|
|
try {
|
|
|
|
var logins = this.manager().findLogins({}, 'chrome://qa',
|
2008-05-16 09:59:37 -07:00
|
|
|
null, litmus.baseURL);
|
2007-09-10 15:01:06 -07:00
|
|
|
if (logins.length > 0 && logins[0] != null)
|
|
|
|
return logins[0];
|
|
|
|
return false;
|
|
|
|
} catch(ex) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|