1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: MNSMozabProxy.cxx,v $
10 * $Revision: 1.10.66.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include "MNSMozabProxy.hxx"
35 #include "resource/mozab_res.hrc"
36 #include "MDatabaseMetaDataHelper.hxx"
38 #include <osl/mutex.hxx>
39 #include <osl/conditn.hxx>
41 #include "pre_include_mozilla.h"
42 #include <nsIProxyObjectManager.h>
43 // More Mozilla includes for LDAP Connection Test
45 #include "nsILDAPURL.h"
46 #include "nsILDAPMessage.h"
47 #include "nsILDAPMessageListener.h"
48 #include "nsILDAPErrors.h"
49 #include "nsILDAPConnection.h"
50 #include "nsILDAPOperation.h"
51 #include "post_include_mozilla.h"
53 #include <MQueryHelper.hxx>
54 #include <com/sun/star/uno/Reference.hxx>
55 #include <unotools/processfactory.hxx>
56 #include "com/sun/star/mozilla/XProxyRunner.hpp"
58 using namespace com::sun::star::uno
;
59 using namespace com::sun::star::lang
;
60 using namespace com::sun::star::mozilla
;
63 #define TYPEASSERT(value,type) if (value != type) return !NS_OK;
65 using namespace connectivity::mozab
;
67 /* Implementation file */
69 static ::osl::Mutex m_aThreadMutex
;
70 extern nsresult
NewAddressBook(const ::rtl::OUString
* aName
);
73 MNSMozabProxy::MNSMozabProxy()
76 #if OSL_DEBUG_LEVEL > 0
77 m_oThreadID
= osl_getThreadIdentifier(NULL
);
82 MNSMozabProxy::~MNSMozabProxy()
86 sal_Int32
MNSMozabProxy::StartProxy(RunArgs
* args
,::com::sun::star::mozilla::MozillaProductType aProduct
,const ::rtl::OUString
&aProfile
)
88 OSL_TRACE( "IN : MNSMozabProxy::StartProxy() \n" );
89 ::osl::MutexGuard
aGuard(m_aThreadMutex
);
95 Reference
<XMultiServiceFactory
> xFactory
= ::comphelper::getProcessServiceFactory();
96 OSL_ENSURE( xFactory
.is(), "can't get service factory" );
97 ::com::sun::star::uno::Reference
<XInterface
> xInstance
= xFactory
->createInstance(::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.mozilla.MozillaBootstrap")) );
98 OSL_ENSURE( xInstance
.is(), "failed to create instance" );
99 xRunner
= ::com::sun::star::uno::Reference
< ::com::sun::star::mozilla::XProxyRunner
>(xInstance
,UNO_QUERY
);
101 const ::com::sun::star::uno::Reference
< ::com::sun::star::mozilla::XCodeProxy
> aCode(this);
102 return xRunner
->Run(aCode
);
105 extern nsresult
getTableStringsProxied(const sal_Char
* sAbURI
, sal_Int32
*nDirectoryType
,MNameMapper
*nmap
,
106 ::std::vector
< ::rtl::OUString
>* _rStrings
,
107 ::std::vector
< ::rtl::OUString
>* _rTypes
,
108 sal_Int32
* pErrorId
);
110 ::com::sun::star::mozilla::MozillaProductType SAL_CALL
MNSMozabProxy::getProductType( ) throw (::com::sun::star::uno::RuntimeException
)
114 ::rtl::OUString SAL_CALL
MNSMozabProxy::getProfileName( ) throw (::com::sun::star::uno::RuntimeException
)
119 sal_Int32 SAL_CALL
MNSMozabProxy::run( ) throw (::com::sun::star::uno::RuntimeException
)
121 #if OSL_DEBUG_LEVEL > 0
122 OSL_TRACE( "IN : MNSMozabProxy::Run() Caller thread :%4d \n" , m_oThreadID
);
124 OSL_TRACE( "IN : MNSMozabProxy::Run() \n" );
126 nsresult rv
= NS_ERROR_INVALID_ARG
;
128 return NS_ERROR_INVALID_ARG
;
129 switch(m_Args
->funcIndex
)
131 case ProxiedFunc::FUNC_TESTLDAP_INIT_LDAP
:
132 case ProxiedFunc::FUNC_TESTLDAP_IS_LDAP_CONNECTED
:
133 case ProxiedFunc::FUNC_TESTLDAP_RELEASE_RESOURCE
:
134 rv
= testLDAPConnection();
136 case ProxiedFunc::FUNC_GET_TABLE_STRINGS
:
137 rv
= getTableStringsProxied((const sal_Char
*)m_Args
->arg1
,
138 (sal_Int32
*)m_Args
->arg2
,
139 (MNameMapper
*)m_Args
->arg3
,
140 (::std::vector
< ::rtl::OUString
>*)m_Args
->arg4
,
141 (::std::vector
< ::rtl::OUString
>*)m_Args
->arg5
,
142 (sal_Int32
*)m_Args
->arg6
);
144 case ProxiedFunc::FUNC_EXECUTE_QUERY
:
145 if (m_Args
->arg1
&& m_Args
->arg2
)
147 rv
= ((MQuery
*)m_Args
->arg1
)->executeQueryProxied((OConnection
*)m_Args
->arg2
);
150 case ProxiedFunc::FUNC_QUERYHELPER_CREATE_NEW_CARD
:
151 case ProxiedFunc::FUNC_QUERYHELPER_DELETE_CARD
:
152 case ProxiedFunc::FUNC_QUERYHELPER_COMMIT_CARD
:
153 case ProxiedFunc::FUNC_QUERYHELPER_RESYNC_CARD
:
156 rv
= QueryHelperStub();
159 case ProxiedFunc::FUNC_NEW_ADDRESS_BOOK
:
162 rv
= NewAddressBook((const ::rtl::OUString
*)m_Args
->arg1
);
166 return NS_ERROR_INVALID_ARG
;
171 nsresult
MNSMozabProxy::QueryHelperStub()
173 nsresult rv
= NS_ERROR_INVALID_ARG
;
174 MQueryHelper
* mHelper
=(MQueryHelper
*) m_Args
->arg1
;
175 switch(m_Args
->funcIndex
)
177 case ProxiedFunc::FUNC_QUERYHELPER_CREATE_NEW_CARD
:
178 if (m_Args
->arg2
) //m_Args->arg2 used to return cord number
180 *((sal_Int32
*)m_Args
->arg2
) = mHelper
->createNewCard();
184 case ProxiedFunc::FUNC_QUERYHELPER_DELETE_CARD
:
185 if (m_Args
->arg2
&& m_Args
->arg3
) //m_Args->arg2 used to get the cord number
187 rv
= mHelper
->deleteCard(*((sal_Int32
*)m_Args
->arg2
),(nsIAbDirectory
*)m_Args
->arg3
);
190 case ProxiedFunc::FUNC_QUERYHELPER_COMMIT_CARD
:
191 if (m_Args
->arg2
&& m_Args
->arg3
) //m_Args->arg2 used to get the cord number
193 rv
= mHelper
->commitCard(*((sal_Int32
*)m_Args
->arg2
),(nsIAbDirectory
*)m_Args
->arg3
);
196 case ProxiedFunc::FUNC_QUERYHELPER_RESYNC_CARD
:
197 if (m_Args
->arg2
) //m_Args->arg2 used to get the cord number
199 rv
= mHelper
->resyncRow(*((sal_Int32
*)m_Args
->arg2
));
207 //-------------------------------------------------------------------
209 #define NS_LDAPCONNECTION_CONTRACTID "@mozilla.org/network/ldap-connection;1"
210 #define NS_LDAPOPERATION_CONTRACTID "@mozilla.org/network/ldap-operation;1"
211 #define NS_LDAPMESSAGE_CONTRACTID "@mozilla.org/network/ldap-message;1"
212 #define NS_LDAPURL_CONTRACTID "@mozilla.org/network/ldap-url;1"
214 namespace connectivity
{
216 class MLDAPMessageListener
: public nsILDAPMessageListener
219 NS_DECL_NSILDAPMESSAGELISTENER
221 MLDAPMessageListener();
222 virtual ~MLDAPMessageListener();
224 sal_Bool
initialized() const;
225 sal_Bool
goodConnection() const { return initialized() && m_GoodConnection
; }
229 ::osl::Mutex m_aMutex
;
230 ::osl::Condition m_aCondition
;
232 sal_Bool m_IsComplete
;
233 sal_Bool m_GoodConnection
;
235 void setConnectionStatus( sal_Bool _good
);
240 NS_IMPL_THREADSAFE_ISUPPORTS1(MLDAPMessageListener
, nsILDAPMessageListener
)
242 MLDAPMessageListener::MLDAPMessageListener()
244 , m_IsComplete( sal_False
)
245 , m_GoodConnection( sal_False
)
247 m_aCondition
.reset();
250 MLDAPMessageListener::~MLDAPMessageListener()
254 sal_Bool
MLDAPMessageListener::initialized() const
256 return const_cast< MLDAPMessageListener
* >( this )->m_aCondition
.check();
259 void MLDAPMessageListener::setConnectionStatus( sal_Bool _good
)
261 ::osl::MutexGuard
aGuard( m_aMutex
);
262 m_IsComplete
= sal_True
;
263 m_GoodConnection
= _good
;
268 NS_IMETHODIMP
MLDAPMessageListener::OnLDAPInit(nsILDAPConnection
* /*aConn*/, nsresult aStatus
)
270 setConnectionStatus( NS_SUCCEEDED( aStatus
) ? sal_True
: sal_False
);
274 NS_IMETHODIMP
MLDAPMessageListener::OnLDAPMessage( nsILDAPMessage
* aMessage
)
279 rv
= aMessage
->GetType(&messageType
);
280 NS_ENSURE_SUCCESS(rv
, rv
);
284 case nsILDAPMessage::RES_BIND
:
285 rv
= aMessage
->GetErrorCode(&errCode
);
286 // if the login failed
287 if (errCode
!= (PRInt32
)nsILDAPErrors::SUCCESS
) {
288 setConnectionStatus( sal_False
);
291 setConnectionStatus( sal_True
);
293 case nsILDAPMessage::RES_SEARCH_RESULT
:
294 setConnectionStatus( sal_True
);
303 //-------------------------------------------------------------------
306 MNSMozabProxy::testLDAPConnection( )
308 nsresult rv
=NS_ERROR_INVALID_ARG
;
309 switch(m_Args
->funcIndex
)
311 case ProxiedFunc::FUNC_TESTLDAP_INIT_LDAP
:
312 if (m_Args
->arg1
&& m_Args
->arg4
)
314 rv
= InitLDAP((sal_Char
*)m_Args
->arg1
,(sal_Unicode
*)m_Args
->arg2
,(sal_Unicode
*)m_Args
->arg3
,(sal_Bool
*)m_Args
->arg4
);
317 case ProxiedFunc::FUNC_TESTLDAP_IS_LDAP_CONNECTED
:
320 const MLDAPMessageListener
* pListener( static_cast< const MLDAPMessageListener
* >( m_Args
->arg5
) );
321 if ( pListener
->initialized() )
322 rv
= pListener
->goodConnection() ? 0 : (nsresult
)PR_NOT_CONNECTED_ERROR
;
324 rv
= (nsresult
)PR_CONNECT_TIMEOUT_ERROR
;
327 case ProxiedFunc::FUNC_TESTLDAP_RELEASE_RESOURCE
:
330 ((MLDAPMessageListener
*)m_Args
->arg5
)->Release();
331 delete (MLDAPMessageListener
*)m_Args
->arg5
;
337 return NS_ERROR_INVALID_ARG
;
342 MNSMozabProxy::InitLDAP(sal_Char
* sUri
, sal_Unicode
* sBindDN
, sal_Unicode
* pPasswd
,sal_Bool
* nUseSSL
)
344 sal_Bool useSSL
= *nUseSSL
;
347 nsCOMPtr
<nsILDAPURL
> url
;
348 url
= do_CreateInstance(NS_LDAPURL_CONTRACTID
, &rv
);
350 return NS_ERROR_INVALID_ARG
;
352 rv
= url
->SetSpec( nsDependentCString(sUri
) );
353 NS_ENSURE_SUCCESS(rv
, rv
);
356 rv
= url
->GetAsciiHost(host
);
357 NS_ENSURE_SUCCESS(rv
, rv
);
360 rv
= url
->GetPort(&port
);
361 NS_ENSURE_SUCCESS(rv
, rv
);
365 NS_ENSURE_SUCCESS(rv
, rv
);
369 // Get the ldap connection
370 nsCOMPtr
<nsILDAPConnection
> ldapConnection
;
371 ldapConnection
= do_CreateInstance(NS_LDAPCONNECTION_CONTRACTID
, &rv
);
372 NS_ENSURE_SUCCESS(rv
, rv
);
374 MLDAPMessageListener
* messageListener
=
375 new MLDAPMessageListener ( );
376 if (messageListener
== NULL
)
377 return NS_ERROR_INVALID_ARG
;
379 messageListener
->AddRef();
381 nsCAutoString nsBind
;
382 // PRUnichar != sal_Unicode in mingw
383 nsBind
.AssignWithConversion(reinterpret_cast_mingw_only
<const PRUnichar
*>(sBindDN
));
385 // Now lets initialize the LDAP connection properly.
386 rv
= ldapConnection
->Init(host
.get(), port
, useSSL
, nsBind
,
387 messageListener
,NULL
,nsILDAPConnection::VERSION3
);
388 // Initiate the LDAP operation
389 nsCOMPtr
<nsILDAPOperation
> ldapOperation
=
390 do_CreateInstance(NS_LDAPOPERATION_CONTRACTID
, &rv
);
392 rv
= ldapOperation
->Init(ldapConnection
, messageListener
, nsnull
);
394 return NS_ERROR_UNEXPECTED
; // this should never happen
396 if ( pPasswd
&& *pPasswd
)
398 nsCAutoString nsPassword
;
399 // PRUnichar != sal_Unicode in mingw
400 nsPassword
.AssignWithConversion(reinterpret_cast_mingw_only
<const PRUnichar
*>(pPasswd
));
401 rv
= ldapOperation
->SimpleBind(nsPassword
);
404 if (NS_SUCCEEDED(rv
))
405 m_Args
->arg5
= messageListener
;