1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_ucbhelper.hxx"
26 
27 /**************************************************************************
28 								TODO
29  **************************************************************************
30 
31  *************************************************************************/
32 #include <osl/diagnose.h>
33 #include <osl/mutex.hxx>
34 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
35 #include <com/sun/star/lang/XComponent.hpp>
36 #include <com/sun/star/ucb/XContentIdentifierFactory.hpp>
37 #include <com/sun/star/ucb/XContentProvider.hpp>
38 #include <com/sun/star/ucb/XContentProviderManager.hpp>
39 #include <com/sun/star/ucb/XCommandProcessor.hpp>
40 #include <ucbhelper/contentbroker.hxx>
41 
42 using namespace com::sun::star::lang;
43 using namespace com::sun::star::ucb;
44 using namespace com::sun::star::uno;
45 using namespace rtl;
46 
47 namespace
48 {
49     osl::Mutex globalContentBrokerMutex;
getGlobalContentBrokerMutex()50     osl::Mutex & getGlobalContentBrokerMutex() { return globalContentBrokerMutex; }
51 
52 } // namespace
53 
54 namespace ucbhelper
55 {
56 
57 //=========================================================================
58 //=========================================================================
59 //
60 // class ContentBroker_Impl.
61 //
62 //=========================================================================
63 //=========================================================================
64 
65 class ContentBroker_Impl
66 {
67     Reference< XMultiServiceFactory >       m_xSMgr;
68     Reference< XContentIdentifierFactory >  m_xIdFac;
69     Reference< XContentProvider >           m_xProvider;
70     Reference< XContentProviderManager >    m_xProviderMgr;
71     Reference< XCommandProcessor >          m_xCommandProc;
72     osl::Mutex                              m_aMutex;
73     Sequence< Any >                         m_aArguments;
74     ContentProviderDataList                 m_aProvData;
75     bool                                    m_bInitDone;
76 
77 public:
ContentBroker_Impl(const Reference<XMultiServiceFactory> & rSMgr,const Sequence<Any> & rArguments)78 	ContentBroker_Impl( const Reference< XMultiServiceFactory >& rSMgr,
79 						const Sequence< Any >& rArguments )
80     : m_xSMgr( rSMgr ), m_aArguments( rArguments ), m_bInitDone( sal_False )
81 	{}
82 
ContentBroker_Impl(const Reference<XMultiServiceFactory> & rSMgr,const ContentProviderDataList & rData)83 	ContentBroker_Impl( const Reference< XMultiServiceFactory >& rSMgr,
84                         const ContentProviderDataList & rData )
85     : m_xSMgr( rSMgr ), m_aProvData( rData ), m_bInitDone( sal_False )
86 	{}
87 
88 	~ContentBroker_Impl();
89 
90     bool initialize();
91 
getServiceManager() const92 	const Reference< XMultiServiceFactory >& getServiceManager() const
93 	{ return m_xSMgr; }
94 
getIdFactory() const95 	const Reference< XContentIdentifierFactory >& getIdFactory() const
96     { return m_xIdFac; }
97 
getProvider() const98 	const Reference< XContentProvider >& getProvider() const
99     { return m_xProvider; }
100 
getProviderManager() const101 	const Reference< XContentProviderManager >& getProviderManager() const
102     { return m_xProviderMgr; }
103 
getCommandProcessor() const104     const Reference< XCommandProcessor >& getCommandProcessor() const
105     { return m_xCommandProc; }
106 };
107 
108 //=========================================================================
109 //=========================================================================
110 //
111 // ContentBroker Implementation.
112 //
113 //=========================================================================
114 //=========================================================================
115 
116 // static member!
117 ContentBroker* ContentBroker::m_pTheBroker = 0;
118 
119 //=========================================================================
ContentBroker(const Reference<XMultiServiceFactory> & rSMgr,const Sequence<Any> & rArguments)120 ContentBroker::ContentBroker( const Reference< XMultiServiceFactory >& rSMgr,
121 							  const Sequence< Any >& rArguments )
122 {
123 	m_pImpl = new ContentBroker_Impl( rSMgr, rArguments );
124 }
125 
126 //=========================================================================
ContentBroker(const Reference<XMultiServiceFactory> & rSMgr,const ContentProviderDataList & rData)127 ContentBroker::ContentBroker( const Reference< XMultiServiceFactory >& rSMgr,
128                               const ContentProviderDataList & rData )
129 {
130     m_pImpl = new ContentBroker_Impl( rSMgr, rData );
131 }
132 
133 //=========================================================================
~ContentBroker()134 ContentBroker::~ContentBroker()
135 {
136 	delete m_pImpl;
137 }
138 
139 //=========================================================================
getServiceManager() const140 Reference< XMultiServiceFactory > ContentBroker::getServiceManager() const
141 {
142 	return m_pImpl->getServiceManager();
143 }
144 
145 //=========================================================================
146 Reference< XContentIdentifierFactory >
getContentIdentifierFactoryInterface() const147 				ContentBroker::getContentIdentifierFactoryInterface() const
148 {
149 	return m_pImpl->getIdFactory();
150 }
151 
152 //=========================================================================
153 Reference< XContentProvider >
getContentProviderInterface() const154 				ContentBroker::getContentProviderInterface() const
155 {
156 	return m_pImpl->getProvider();
157 }
158 
159 //=========================================================================
160 Reference< XContentProviderManager >
getContentProviderManagerInterface() const161                 ContentBroker::getContentProviderManagerInterface() const
162 {
163 	return m_pImpl->getProviderManager();
164 }
165 
166 //=========================================================================
167 Reference< XCommandProcessor >
getCommandProcessorInterface() const168                 ContentBroker::getCommandProcessorInterface() const
169 {
170     return m_pImpl->getCommandProcessor();
171 }
172 
173 //=========================================================================
174 // static
initialize(const Reference<XMultiServiceFactory> & rSMgr,const Sequence<Any> & rArguments)175 sal_Bool ContentBroker::initialize(
176                         const Reference< XMultiServiceFactory >& rSMgr,
177                         const Sequence< Any >& rArguments )
178 {
179     OSL_ENSURE( !m_pTheBroker,
180                 "ContentBroker::initialize - already initialized!" );
181 
182     if ( !m_pTheBroker )
183     {
184         osl::Guard< osl::Mutex > aGuard( getGlobalContentBrokerMutex() );
185 
186         if ( !m_pTheBroker )
187         {
188             ContentBroker * pBroker = new ContentBroker( rSMgr, rArguments );
189 
190             // Force init to be able to detect UCB init trouble immediately.
191             if ( pBroker->m_pImpl->initialize() )
192                 m_pTheBroker = pBroker;
193             else
194                 delete pBroker;
195         }
196     }
197 
198     return m_pTheBroker != 0;
199 }
200 
201 //=========================================================================
202 // static
initialize(const Reference<XMultiServiceFactory> & rSMgr,const ContentProviderDataList & rData)203 sal_Bool ContentBroker::initialize(
204                         const Reference< XMultiServiceFactory >& rSMgr,
205                         const ContentProviderDataList & rData )
206 {
207     OSL_ENSURE( !m_pTheBroker,
208                 "ContentBroker::initialize - already initialized!" );
209 
210     if ( !m_pTheBroker )
211     {
212         osl::Guard< osl::Mutex > aGuard( getGlobalContentBrokerMutex() );
213 
214         if ( !m_pTheBroker )
215         {
216             ContentBroker * pBroker = new ContentBroker( rSMgr, rData );
217 
218             // Force init to be able to detect UCB init trouble immediately.
219             if ( pBroker->m_pImpl->initialize() )
220                 m_pTheBroker = pBroker;
221             else
222                 delete pBroker;
223         }
224     }
225 
226     return m_pTheBroker != 0;
227 }
228 
229 //=========================================================================
230 // static
deinitialize()231 void ContentBroker::deinitialize()
232 {
233     osl::MutexGuard aGuard( getGlobalContentBrokerMutex() );
234 
235 	delete m_pTheBroker;
236 	m_pTheBroker = 0;
237 }
238 
239 //=========================================================================
240 // static
get()241 ContentBroker* ContentBroker::get()
242 {
243 	return m_pTheBroker;
244 }
245 
246 //=========================================================================
247 //=========================================================================
248 //
249 // ContentBroker_Impl Implementation.
250 //
251 //=========================================================================
252 //=========================================================================
253 
~ContentBroker_Impl()254 ContentBroker_Impl::~ContentBroker_Impl()
255 {
256 	Reference< XComponent > xComponent(	m_xProvider, UNO_QUERY );
257     if ( xComponent.is() )
258 	{
259 		m_xIdFac 	   = 0;
260 		m_xProvider    = 0;
261 		m_xProviderMgr = 0;
262 
263 		xComponent->dispose();
264 	}
265 }
266 
267 //=========================================================================
initialize()268 bool ContentBroker_Impl::initialize()
269 {
270 	if ( !m_bInitDone )
271 	{
272         osl::MutexGuard aGuard( m_aMutex );
273 
274         if ( !m_bInitDone )
275         {
276             Reference< XInterface > xIfc;
277 
278             if ( m_aProvData.size() > 0 )
279             {
280                 try
281                 {
282                     xIfc = m_xSMgr->createInstance(
283                             OUString::createFromAscii(
284                                 "com.sun.star.ucb.UniversalContentBroker" ) );
285                 }
286                 catch ( Exception const & )
287                 {
288                 }
289 
290                 if ( xIfc.is() )
291                 {
292                     m_xProviderMgr
293                         = Reference< XContentProviderManager >( xIfc, UNO_QUERY );
294 
295                     if ( m_xProviderMgr.is() )
296                     {
297                         ContentProviderDataList::const_iterator aEnd(m_aProvData.end());
298                         for (ContentProviderDataList::const_iterator aIt(m_aProvData.begin());
299 		                    aIt != aEnd; ++aIt)
300                     	{
301                             registerAtUcb(m_xProviderMgr,
302                                       m_xSMgr,
303                                       aIt->ServiceName,
304                                       aIt->Arguments,
305                                       aIt->URLTemplate,
306                                       0);
307 	                    }
308 
309                     }
310                 }
311             }
312             else
313             {
314                 try
315                 {
316                     xIfc = m_xSMgr->createInstanceWithArguments(
317                             OUString::createFromAscii(
318                                 "com.sun.star.ucb.UniversalContentBroker" ),
319                             m_aArguments );
320                 }
321                 catch ( Exception const & )
322                 {
323                 }
324             }
325 
326             OSL_ENSURE( xIfc.is(), "Error creating UCB service!" );
327 
328             if ( !xIfc.is() )
329                 return false;
330 
331 
332             m_xIdFac
333                 = Reference< XContentIdentifierFactory >( xIfc, UNO_QUERY );
334 
335             OSL_ENSURE( m_xIdFac.is(),
336                 "UCB without required interface XContentIdentifierFactory!" );
337 
338             if ( !m_xIdFac.is() )
339                 return false;
340 
341             m_xProvider = Reference< XContentProvider >( xIfc, UNO_QUERY );
342 
343             OSL_ENSURE( m_xProvider.is(),
344                 "UCB without required interface XContentProvider!" );
345 
346             if ( !m_xProvider.is() )
347                 return false;
348 
349             if ( !m_xProviderMgr.is() )
350                 m_xProviderMgr
351                     = Reference< XContentProviderManager >( xIfc, UNO_QUERY );
352 
353             OSL_ENSURE( m_xProviderMgr.is(),
354                 "UCB without required interface XContentProviderManager!" );
355 
356             if ( !m_xProviderMgr.is() )
357                 return false;
358 
359             m_xCommandProc = Reference< XCommandProcessor >( xIfc, UNO_QUERY );
360 
361             OSL_ENSURE( m_xCommandProc.is(),
362                 "UCB without required interface XCommandProcessor!" );
363 
364             if ( !m_xCommandProc.is() )
365                 return false;
366 
367             // Everything okay.
368             m_bInitDone = sal_True;
369         }
370 	}
371 
372     return true;
373 }
374 
375 } /* namespace ucbhelper */
376 
377