Bug 396745 nsILDAPConnection::Init should take an nsILDAPURL as a parameter instead of three others. r/sr=bienvenu NPOTDFFB

git-svn-id: svn://10.0.0.236/trunk@236314 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
bugzilla%standard8.plus.com 2007-09-19 20:32:00 +00:00
parent d328734de5
commit 8ec99cdb42
8 changed files with 110 additions and 214 deletions

View File

@ -41,12 +41,13 @@
interface nsILDAPOperation;
interface nsILDAPMessageListener;
interface nsILDAPURL;
%{C++
#define NS_LDAPCONNECTION_CONTRACTID "@mozilla.org/network/ldap-connection;1"
%}
[scriptable, uuid(337ad2fe-1dd2-11b2-89f8-aae1221ec86c)]
[scriptable, uuid(360c1ff7-15e3-4ffe-b4b8-0eda72ebc096)]
interface nsILDAPConnection : nsISupports
{
/**
@ -70,10 +71,8 @@ interface nsILDAPConnection : nsISupports
* Set up the connection. Note that init() must be called on a thread
* that already has an nsIEventQueue.
*
* @param aHost server name for ldap_init()
* @param aPort server port number for ldap_init()
* -1 == default port (389)
* @param aSSL use SSL on this connection?
* @param aUrl A URL for the ldap server. The host, port and
* ssl connection type will be extracted from this
* @param aBindName DN to bind as
* @param aMessageListener Callback for DNS resolution completion
* @param aClosure private parameter (anything caller desires)
@ -86,8 +85,8 @@ interface nsILDAPConnection : nsISupports
* @exception NS_ERROR_FAILURE
* @exception NS_ERROR_UNEXPECTED internal error
*/
void init(in string aHost, in long aPort, in boolean aSSL,
in AUTF8String aBindName,
void init(in nsILDAPURL aUrl,
in AUTF8String aBindName,
in nsILDAPMessageListener aMessageListener,
in nsISupports aClosure, in unsigned long aVersion);

View File

@ -1,4 +1,4 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
@ -59,6 +59,7 @@
#include "nsLDAPOperation.h"
#include "nsILDAPErrors.h"
#include "nsIClassInfoImpl.h"
#include "nsILDAPURL.h"
const char kConsoleServiceContractId[] = "@mozilla.org/consoleservice;1";
const char kDNSServiceContractId[] = "@mozilla.org/network/dns-service;1";
@ -142,109 +143,106 @@ nsLDAPConnection::Release(void)
return count;
}
NS_IMETHODIMP
nsLDAPConnection::Init(const char *aHost, PRInt32 aPort, PRBool aSSL,
const nsACString& aBindName,
nsLDAPConnection::Init(nsILDAPURL *aUrl, const nsACString &aBindName,
nsILDAPMessageListener *aMessageListener,
nsISupports *aClosure, PRUint32 aVersion)
{
nsCOMPtr<nsIDNSListener> selfProxy;
nsresult rv;
NS_ENSURE_ARG_POINTER(aUrl);
NS_ENSURE_ARG_POINTER(aMessageListener);
if ( !aHost || !aMessageListener) {
return NS_ERROR_ILLEGAL_VALUE;
// Save various items that we'll use later
mBindName.Assign(aBindName);
mClosure = aClosure;
mInitListener = aMessageListener;
// Make sure we haven't called Init earlier, i.e. there's a DNS
// request pending.
NS_ASSERTION(!mDNSRequest, "nsLDAPConnection::Init() "
"Connection was already initialized\n");
// Check and save the version number
if (aVersion != nsILDAPConnection::VERSION2 &&
aVersion != nsILDAPConnection::VERSION3) {
NS_ERROR("nsLDAPConnection::Init(): illegal version");
return NS_ERROR_ILLEGAL_VALUE;
}
mVersion = aVersion;
nsresult rv;
// Get the port number, SSL flag for use later, once the DNS server(s)
// has resolved the host part.
rv = aUrl->GetPort(&mPort);
NS_ENSURE_SUCCESS(rv, rv);
PRUint32 options;
rv = aUrl->GetOptions(&options);
NS_ENSURE_SUCCESS(rv, rv);
mSSL = options & nsILDAPURL::OPT_SECURE;
// allocate a hashtable to keep track of pending operations.
// 10 buckets seems like a reasonable size, and we do want it to
// be threadsafe
mPendingOperations = new nsSupportsHashtable(10, PR_TRUE);
if (!mPendingOperations) {
NS_ERROR("failure initializing mPendingOperations hashtable");
return NS_ERROR_OUT_OF_MEMORY;
}
nsCOMPtr<nsIThread> curThread = do_GetCurrentThread();
if (!curThread) {
NS_ERROR("nsLDAPConnection::Init(): couldn't get current thread");
return NS_ERROR_FAILURE;
}
// Do the pre-resolve of the hostname, using the DNS service. This
// will also initialize the LDAP connection properly, once we have
// the IPs resolved for the hostname. This includes creating the
// new thread for this connection.
//
// XXX - What return codes can we expect from the DNS service?
//
nsCOMPtr<nsIDNSService>
pDNSService(do_GetService(kDNSServiceContractId, &rv));
if (NS_FAILED(rv)) {
NS_ERROR("nsLDAPConnection::Init(): couldn't create the DNS Service object");
return NS_ERROR_FAILURE;
}
rv = aUrl->GetAsciiHost(mDNSHost);
NS_ENSURE_SUCCESS(rv, rv);
// if the caller has passed in a space-delimited set of hosts, as the
// ldap c-sdk allows, strip off the trailing hosts for now.
// Soon, we'd like to make multiple hosts work, but now make
// at least the first one work.
mDNSHost.CompressWhitespace(PR_TRUE, PR_TRUE);
PRInt32 spacePos = mDNSHost.FindChar(' ');
// trim off trailing host(s)
if (spacePos != kNotFound)
mDNSHost.Truncate(spacePos);
rv = pDNSService->AsyncResolve(mDNSHost, 0, this, curThread,
getter_AddRefs(mDNSRequest));
if (NS_FAILED(rv)) {
switch (rv) {
case NS_ERROR_OUT_OF_MEMORY:
case NS_ERROR_UNKNOWN_HOST:
case NS_ERROR_FAILURE:
case NS_ERROR_OFFLINE:
break;
default:
rv = NS_ERROR_UNEXPECTED;
}
// Make sure we haven't called Init earlier, i.e. there's a DNS
// request pending.
//
NS_ASSERTION(!mDNSRequest, "nsLDAPConnection::Init() "
"Connection was already initialized\n");
mBindName.Assign(aBindName);
mClosure = aClosure;
// Save the port number, SSL flag, and protocol version for later
// use, once the DNS server(s) has resolved the host part.
//
mPort = aPort;
mSSL = aSSL;
if (aVersion != nsILDAPConnection::VERSION2 &&
aVersion != nsILDAPConnection::VERSION3) {
NS_ERROR("nsLDAPConnection::Init(): illegal version");
return NS_ERROR_ILLEGAL_VALUE;
}
mVersion = aVersion;
// Save the Init listener reference, we need it when the async
// DNS resolver has finished.
//
mInitListener = aMessageListener;
// allocate a hashtable to keep track of pending operations.
// 10 buckets seems like a reasonable size, and we do want it to
// be threadsafe
//
mPendingOperations = new nsSupportsHashtable(10, PR_TRUE);
if ( !mPendingOperations) {
NS_ERROR("failure initializing mPendingOperations hashtable");
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsIThread> curThread = do_GetCurrentThread();
if (!curThread) {
NS_ERROR("nsLDAPConnection::Init(): couldn't "
"get current thread");
return NS_ERROR_FAILURE;
}
// Do the pre-resolve of the hostname, using the DNS service. This
// will also initialize the LDAP connection properly, once we have
// the IPs resolved for the hostname. This includes creating the
// new thread for this connection.
//
// XXX - What return codes can we expect from the DNS service?
//
nsCOMPtr<nsIDNSService>
pDNSService(do_GetService(kDNSServiceContractId, &rv));
if (NS_FAILED(rv)) {
NS_ERROR("nsLDAPConnection::Init(): couldn't "
"create the DNS Service object");
return NS_ERROR_FAILURE;
}
mDNSHost = aHost;
// if the caller has passed in a space-delimited set of hosts, as the
// ldap c-sdk allows, strip off the trailing hosts for now.
// Soon, we'd like to make multiple hosts work, but now make
// at least the first one work.
mDNSHost.CompressWhitespace(PR_TRUE, PR_TRUE);
PRInt32 spacePos = mDNSHost.FindChar(' ');
// trim off trailing host(s)
if (spacePos != kNotFound)
mDNSHost.Truncate(spacePos);
rv = pDNSService->AsyncResolve(mDNSHost, 0, this, curThread,
getter_AddRefs(mDNSRequest));
if (NS_FAILED(rv)) {
switch (rv) {
case NS_ERROR_OUT_OF_MEMORY:
case NS_ERROR_UNKNOWN_HOST:
case NS_ERROR_FAILURE:
case NS_ERROR_OFFLINE:
break;
default:
rv = NS_ERROR_UNEXPECTED;
}
mDNSHost.Truncate();
}
return rv;
mDNSHost.Truncate();
}
return rv;
}
// this might get exposed to clients, so we've broken it

View File

@ -710,11 +710,8 @@ nsLDAPService::EstablishConnection(nsLDAPServiceEntry *aEntry,
nsCOMPtr<nsILDAPURL> url;
nsCOMPtr<nsILDAPConnection> conn, conn2;
nsCOMPtr<nsILDAPMessage> message;
nsCAutoString host;
nsCAutoString binddn;
nsCAutoString password;
PRInt32 port;
PRUint32 options;
PRUint32 protocolVersion;
nsresult rv;
@ -744,19 +741,6 @@ nsLDAPService::EstablishConnection(nsLDAPServiceEntry *aEntry,
if (NS_FAILED(rv)) {
return NS_ERROR_FAILURE;
}
rv = url->GetAsciiHost(host);
if (NS_FAILED(rv)) {
return NS_ERROR_FAILURE;
}
rv = url->GetPort(&port);
if (NS_FAILED(rv)) {
return NS_ERROR_FAILURE;
}
rv = url->GetOptions(&options);
if (NS_FAILED(rv)) {
return NS_ERROR_FAILURE;
}
// Create a new connection for this server.
//
conn = do_CreateInstance(kLDAPConnectionCID, &rv);
@ -766,9 +750,7 @@ nsLDAPService::EstablishConnection(nsLDAPServiceEntry *aEntry,
return NS_ERROR_FAILURE;
}
rv = conn->Init(host.get(), port,
(options & nsILDAPURL::OPT_SECURE) ? PR_TRUE : PR_FALSE,
binddn, this, nsnull, protocolVersion);
rv = conn->Init(url, binddn, this, nsnull, protocolVersion);
if (NS_FAILED(rv)) {
switch (rv) {
// Only pass along errors we are aware of

View File

@ -380,31 +380,6 @@ nsresult nsLDAPSyncQuery::InitConnection()
return NS_ERROR_NOT_INITIALIZED;
}
// host to connect to
//
nsCAutoString host;
rv = mServerURL->GetAsciiHost(host);
if (NS_FAILED(rv)) {
FinishLDAPQuery();
return NS_ERROR_FAILURE;
}
// on which port
//
PRInt32 port;
rv = mServerURL->GetPort(&port);
if (NS_FAILED(rv)) {
FinishLDAPQuery();
return NS_ERROR_FAILURE;
}
PRUint32 options;
rv = mServerURL->GetOptions(&options);
if (NS_FAILED(rv)) {
FinishLDAPQuery();
return NS_ERROR_FAILURE;
}
// get a proxy object so the callback happens on the main thread
//
rv = NS_GetProxyForObject(NS_PROXY_TO_CURRENT_THREAD,
@ -419,9 +394,7 @@ nsresult nsLDAPSyncQuery::InitConnection()
return NS_ERROR_FAILURE;
}
rv = mConnection->Init(host.get(), port,
(options & nsILDAPURL::OPT_SECURE)
? PR_TRUE : PR_FALSE, EmptyCString(), selfProxy,
rv = mConnection->Init(mServerURL, EmptyCString(), selfProxy,
nsnull, mProtocolVersion);
if (NS_FAILED(rv)) {
FinishLDAPQuery();

View File

@ -592,7 +592,7 @@ NS_IMETHODIMP nsAbLDAPDirectoryQuery::DoQuery(nsIAbDirectory *aDirectory,
// Now lets initialize the LDAP connection properly. We'll kick
// off the bind operation in the callback function, |OnLDAPInit()|.
rv = mConnection->Init(host.get(), port, options, mCurrentLogin,
rv = mConnection->Init(mDirectoryUrl, mCurrentLogin,
mListener, nsnull, mCurrentProtocolVersion);
NS_ENSURE_SUCCESS(rv, rv);

View File

@ -116,25 +116,7 @@ nsresult nsAbLDAPReplicationQuery::ConnectToLDAPServer()
if (!mInitialized || !mURL)
return NS_ERROR_NOT_INITIALIZED;
nsCAutoString host;
nsresult rv = mURL->GetHost(host);
if (NS_FAILED(rv))
return rv;
if (host.IsEmpty())
return NS_ERROR_UNEXPECTED;
PRInt32 port;
rv = mURL->GetPort(&port);
if (NS_FAILED(rv))
return rv;
if (!port)
return NS_ERROR_UNEXPECTED;
PRUint32 options;
rv = mURL->GetOptions(&options);
if (NS_FAILED(rv))
return NS_ERROR_UNEXPECTED;
nsresult rv;
nsCOMPtr<nsILDAPMessageListener> mDp = do_QueryInterface(mDataProcessor,
&rv);
if (NS_FAILED(rv))
@ -162,9 +144,7 @@ nsresult nsAbLDAPReplicationQuery::ConnectToLDAPServer()
NS_ENSURE_SUCCESS(rv, rv);
// initialize the LDAP connection
return mConnection->Init(host.get(), port,
(options & nsILDAPURL::OPT_SECURE) ? PR_TRUE : PR_FALSE,
mLogin, listener, nsnull, protocolVersion);
return mConnection->Init(mURL, mLogin, listener, nsnull, protocolVersion);
}
NS_IMETHODIMP nsAbLDAPReplicationQuery::Init(nsIAbLDAPDirectory *aDirectory,

View File

@ -1139,36 +1139,6 @@ nsLDAPAutoCompleteSession::InitConnection()
return NS_ERROR_NOT_INITIALIZED;
}
// host to connect to
//
nsCAutoString host;
rv = mServerURL->GetAsciiHost(host);
if (NS_FAILED(rv)) {
FinishAutoCompleteLookup(nsIAutoCompleteStatus::failureItems, rv,
UNBOUND);
return NS_ERROR_FAILURE;
}
// on which port
//
PRInt32 port;
rv = mServerURL->GetPort(&port);
if (NS_FAILED(rv)) {
FinishAutoCompleteLookup(nsIAutoCompleteStatus::failureItems, rv,
UNBOUND);
return NS_ERROR_FAILURE;
}
// which options
//
PRUint32 options;
rv = mServerURL->GetOptions(&options);
if (NS_FAILED(rv)) {
FinishAutoCompleteLookup(nsIAutoCompleteStatus::failureItems, rv,
UNBOUND);
return NS_ERROR_FAILURE;
}
// get a proxy object so the callback happens on the main thread
//
rv = NS_GetProxyForObject(NS_PROXY_TO_MAIN_THREAD,
@ -1188,9 +1158,7 @@ nsLDAPAutoCompleteSession::InitConnection()
// lookup to occur, and we'll finish the binding of the connection
// in the OnLDAPInit() listener function.
//
rv = mConnection->Init(host.get(), port,
(options & nsILDAPURL::OPT_SECURE) ? PR_TRUE
: PR_FALSE, mLogin, selfProxy, nsnull, mVersion);
rv = mConnection->Init(mServerURL, mLogin, selfProxy, nsnull, mVersion);
if (NS_FAILED(rv)) {
switch (rv) {

View File

@ -95,13 +95,9 @@ function search()
gLdapConnection = Components.classes["@mozilla.org/network/ldap-connection;1"]
.createInstance().QueryInterface(Components.interfaces.nsILDAPConnection);
gLdapConnection.init(
gLdapServerURL.asciiHost,
gLdapServerURL.port,
gLdapServerURL.options & gLdapServerURL.OPT_SECURE,
gLogin,
gLdapConnection.init(gLdapServerURL, gLogin,
getProxyOnUIThread(new boundListener(),
Components.interfaces.nsILDAPMessageListener),
Components.interfaces.nsILDAPMessageListener),
null, Components.interfaces.nsILDAPConnection.VERSION3);
} catch (ex) {