12c696243SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
32c696243SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
42c696243SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
52c696243SAndrew Rist  * distributed with this work for additional information
62c696243SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
72c696243SAndrew Rist  * to you under the Apache License, Version 2.0 (the
82c696243SAndrew Rist  * "License"); you may not use this file except in compliance
92c696243SAndrew Rist  * with the License.  You may obtain a copy of the License at
102c696243SAndrew Rist  *
112c696243SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
122c696243SAndrew Rist  *
132c696243SAndrew Rist  * Unless required by applicable law or agreed to in writing,
142c696243SAndrew Rist  * software distributed under the License is distributed on an
152c696243SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
162c696243SAndrew Rist  * KIND, either express or implied.  See the License for the
172c696243SAndrew Rist  * specific language governing permissions and limitations
182c696243SAndrew Rist  * under the License.
192c696243SAndrew Rist  *
202c696243SAndrew Rist  *************************************************************/
212c696243SAndrew Rist 
222c696243SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_scripting.hxx"
26cdf0e10cSrcweir #include "scripthandler.hxx"
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <osl/mutex.hxx>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <com/sun/star/frame/DispatchResultEvent.hpp>
31cdf0e10cSrcweir #include <com/sun/star/frame/DispatchResultState.hpp>
32cdf0e10cSrcweir #include <com/sun/star/frame/XController.hpp>
33cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <com/sun/star/document/XEmbeddedScripts.hpp>
36cdf0e10cSrcweir #include <com/sun/star/document/XScriptInvocationContext.hpp>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #include <com/sun/star/script/provider/XScriptProviderSupplier.hpp>
41cdf0e10cSrcweir #include <com/sun/star/script/provider/XScriptProviderFactory.hpp>
42cdf0e10cSrcweir #include <com/sun/star/script/provider/ScriptFrameworkErrorType.hpp>
43cdf0e10cSrcweir 
44af1b3ff9Smseidel #include <rtl/uri.hxx>
45cdf0e10cSrcweir #include <sfx2/objsh.hxx>
46cdf0e10cSrcweir #include <sfx2/frame.hxx>
47cdf0e10cSrcweir #include <sfx2/sfxdlg.hxx>
48cdf0e10cSrcweir #include <vcl/abstdlg.hxx>
49cdf0e10cSrcweir #include <tools/diagnose_ex.h>
50cdf0e10cSrcweir 
51cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
52cdf0e10cSrcweir #include <cppuhelper/exc_hlp.hxx>
53cdf0e10cSrcweir #include <util/util.hxx>
54cdf0e10cSrcweir #include <framework/documentundoguard.hxx>
55cdf0e10cSrcweir 
56cdf0e10cSrcweir #include "com/sun/star/uno/XComponentContext.hpp"
57cdf0e10cSrcweir #include "com/sun/star/uri/XUriReference.hpp"
58cdf0e10cSrcweir #include "com/sun/star/uri/XUriReferenceFactory.hpp"
59cdf0e10cSrcweir #include "com/sun/star/uri/XVndSunStarScriptUrl.hpp"
60cdf0e10cSrcweir #include "com/sun/star/beans/XPropertySet.hpp"
61cdf0e10cSrcweir 
62cdf0e10cSrcweir using namespace ::com::sun::star;
63cdf0e10cSrcweir using namespace ::com::sun::star::uno;
64cdf0e10cSrcweir using namespace ::com::sun::star::frame;
65cdf0e10cSrcweir using namespace ::com::sun::star::util;
66cdf0e10cSrcweir using namespace ::com::sun::star::beans;
67cdf0e10cSrcweir using namespace ::com::sun::star::lang;
68cdf0e10cSrcweir using namespace ::com::sun::star::script;
69cdf0e10cSrcweir using namespace ::com::sun::star::script::provider;
70cdf0e10cSrcweir using namespace ::com::sun::star::document;
71cdf0e10cSrcweir 
72cdf0e10cSrcweir namespace scripting_protocolhandler
73cdf0e10cSrcweir {
74cdf0e10cSrcweir 
75cdf0e10cSrcweir const sal_Char * const MYSERVICENAME = "com.sun.star.frame.ProtocolHandler";
76cdf0e10cSrcweir const sal_Char * const MYIMPLNAME = "com.sun.star.comp.ScriptProtocolHandler";
77cdf0e10cSrcweir const sal_Char * MYSCHEME = "vnd.sun.star.script";
78cdf0e10cSrcweir const sal_Int32 MYSCHEME_LEN = 20;
79cdf0e10cSrcweir 
initialize(const css::uno::Sequence<css::uno::Any> & aArguments)80cdf0e10cSrcweir void SAL_CALL ScriptProtocolHandler::initialize(
81cdf0e10cSrcweir     const css::uno::Sequence < css::uno::Any >& aArguments )
82cdf0e10cSrcweir     throw ( css::uno::Exception )
83cdf0e10cSrcweir {
84cdf0e10cSrcweir     if ( m_bInitialised )
85cdf0e10cSrcweir     {
86cdf0e10cSrcweir         return ;
87cdf0e10cSrcweir     }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir     // first argument contains a reference to the frame (may be empty or the desktop,
90cdf0e10cSrcweir     // but usually it's a "real" frame)
91cdf0e10cSrcweir     if ( aArguments.getLength() &&
92cdf0e10cSrcweir          sal_False == ( aArguments[ 0 ] >>= m_xFrame ) )
93cdf0e10cSrcweir     {
94cdf0e10cSrcweir         ::rtl::OUString temp = OUSTR( "ScriptProtocolHandler::initialize: could not extract reference to the frame" );
95cdf0e10cSrcweir         throw RuntimeException( temp, Reference< XInterface >() );
96cdf0e10cSrcweir     }
97cdf0e10cSrcweir 
98cdf0e10cSrcweir     ENSURE_OR_THROW( m_xFactory.is(), "ScriptProtocolHandler::initialize: No Service Manager available" );
99cdf0e10cSrcweir     m_bInitialised = true;
100cdf0e10cSrcweir }
101cdf0e10cSrcweir 
queryDispatch(const URL & aURL,const::rtl::OUString & sTargetFrameName,sal_Int32 nSearchFlags)102cdf0e10cSrcweir Reference< XDispatch > SAL_CALL ScriptProtocolHandler::queryDispatch(
103cdf0e10cSrcweir     const URL& aURL, const ::rtl::OUString& sTargetFrameName, sal_Int32 nSearchFlags )
104cdf0e10cSrcweir     throw( ::com::sun::star::uno::RuntimeException )
105cdf0e10cSrcweir {
106cdf0e10cSrcweir 	(void)sTargetFrameName;
107cdf0e10cSrcweir 	(void)nSearchFlags;
108cdf0e10cSrcweir 
109cdf0e10cSrcweir     Reference< XDispatch > xDispatcher;
110cdf0e10cSrcweir     // get scheme of url
111cdf0e10cSrcweir 
112cdf0e10cSrcweir     Reference< uri::XUriReferenceFactory > xFac (
113cdf0e10cSrcweir          m_xFactory->createInstance( rtl::OUString::createFromAscii(
114cdf0e10cSrcweir             "com.sun.star.uri.UriReferenceFactory") ) , UNO_QUERY );
115cdf0e10cSrcweir     if ( xFac.is() )
116cdf0e10cSrcweir     {
117cdf0e10cSrcweir         Reference<  uri::XUriReference > uriRef(
118cdf0e10cSrcweir             xFac->parse( aURL.Complete ), UNO_QUERY );
119cdf0e10cSrcweir         if ( uriRef.is() )
120cdf0e10cSrcweir         {
121cdf0e10cSrcweir             if ( uriRef->getScheme().equals( ::rtl::OUString::createFromAscii( ::scripting_protocolhandler::MYSCHEME ) ) )
122cdf0e10cSrcweir             {
123cdf0e10cSrcweir                 xDispatcher = this;
124cdf0e10cSrcweir             }
125cdf0e10cSrcweir         }
126cdf0e10cSrcweir     }
127cdf0e10cSrcweir 
128cdf0e10cSrcweir     return xDispatcher;
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
131cdf0e10cSrcweir Sequence< Reference< XDispatch > > SAL_CALL
queryDispatches(const Sequence<DispatchDescriptor> & seqDescriptor)132cdf0e10cSrcweir ScriptProtocolHandler::queryDispatches(
133cdf0e10cSrcweir const Sequence < DispatchDescriptor >& seqDescriptor )
134cdf0e10cSrcweir throw( RuntimeException )
135cdf0e10cSrcweir {
136cdf0e10cSrcweir     sal_Int32 nCount = seqDescriptor.getLength();
137cdf0e10cSrcweir     Sequence< Reference< XDispatch > > lDispatcher( nCount );
138cdf0e10cSrcweir     for ( sal_Int32 i = 0; i < nCount; ++i )
139cdf0e10cSrcweir     {
140cdf0e10cSrcweir         lDispatcher[ i ] = this->queryDispatch( seqDescriptor[ i ].FeatureURL,
141cdf0e10cSrcweir                                                 seqDescriptor[ i ].FrameName,
142cdf0e10cSrcweir                                                 seqDescriptor[ i ].SearchFlags );
143cdf0e10cSrcweir     }
144cdf0e10cSrcweir     return lDispatcher;
145cdf0e10cSrcweir }
146cdf0e10cSrcweir 
dispatchWithNotification(const URL & aURL,const Sequence<PropertyValue> & lArgs,const Reference<XDispatchResultListener> & xListener)147cdf0e10cSrcweir void SAL_CALL ScriptProtocolHandler::dispatchWithNotification(
148cdf0e10cSrcweir     const URL& aURL, const Sequence < PropertyValue >& lArgs,
149cdf0e10cSrcweir     const Reference< XDispatchResultListener >& xListener )
150cdf0e10cSrcweir     throw ( RuntimeException )
151cdf0e10cSrcweir {
152cdf0e10cSrcweir 
153cdf0e10cSrcweir     sal_Bool bSuccess = sal_False;
154cdf0e10cSrcweir     Any invokeResult;
155cdf0e10cSrcweir 	bool bCaughtException = sal_False;
156cdf0e10cSrcweir 	Any aException;
1575e03f7d3SArrigo Marchiori     Sequence< Any > inArgs( 0 );
158cdf0e10cSrcweir 
159cdf0e10cSrcweir     if ( m_bInitialised )
160cdf0e10cSrcweir     {
1615e03f7d3SArrigo Marchiori         ::rtl::OUString aReferer;
1625e03f7d3SArrigo Marchiori         if ( lArgs.getLength() > 0 )
1635e03f7d3SArrigo Marchiori         {
1645e03f7d3SArrigo Marchiori             int argCount = 0;
1655e03f7d3SArrigo Marchiori             for ( int index = 0; index < lArgs.getLength(); index++ )
1665e03f7d3SArrigo Marchiori             {
1675e03f7d3SArrigo Marchiori                 // The propertyval named "Referer"
1685e03f7d3SArrigo Marchiori                 // is not an argument to be passed to script
1695e03f7d3SArrigo Marchiori                 if ( lArgs[ index ].Name.compareToAscii("Referer") == 0 ) {
1705e03f7d3SArrigo Marchiori                     lArgs [ index ].Value >>= aReferer;
1715e03f7d3SArrigo Marchiori                 } else {
1725e03f7d3SArrigo Marchiori                     inArgs.realloc( ++argCount );
1735e03f7d3SArrigo Marchiori                     inArgs[ argCount - 1 ] = lArgs[ index ].Value;
1745e03f7d3SArrigo Marchiori                 }
1755e03f7d3SArrigo Marchiori             }
1765e03f7d3SArrigo Marchiori         }
177cdf0e10cSrcweir         try
178cdf0e10cSrcweir         {
179af1b3ff9Smseidel             ::rtl::OUString xStringUri = ::rtl::Uri::decode( aURL.Complete,
180af1b3ff9Smseidel                 rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
181af1b3ff9Smseidel             bool bIsDocumentScript = ( xStringUri.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "document" ) ) !=-1 );
1825e03f7d3SArrigo Marchiori             bool bRefererIsTrusted = ( aReferer.compareToAscii("private:", 8) == 0 );
183cdf0e10cSrcweir 
18457221d74SArrigo Marchiori             // obtain the component for our security check. We could check bIsDocumentScript but the "location" could be forged
18557221d74SArrigo Marchiori             if ( getScriptInvocation() ) {
18657221d74SArrigo Marchiori                     Reference< XEmbeddedScripts > xDocumentScripts;
187cdf0e10cSrcweir                     xDocumentScripts.set( m_xScriptInvocation->getScriptContainer(), UNO_SET_THROW );
188cdf0e10cSrcweir 
189cdf0e10cSrcweir                 OSL_ENSURE( xDocumentScripts.is(), "ScriptProtocolHandler::dispatchWithNotification: can't do the security check!" );
1905e03f7d3SArrigo Marchiori                 if ( !xDocumentScripts.is() ||
1915e03f7d3SArrigo Marchiori                      ( !bRefererIsTrusted && !xDocumentScripts->getAllowMacroExecution() ) )
192*b57309c1STsutomu Uchino                 {
193*b57309c1STsutomu Uchino                     if ( xListener.is() )
194*b57309c1STsutomu Uchino                     {
195*b57309c1STsutomu Uchino                         ::com::sun::star::frame::DispatchResultEvent aEvent(
196*b57309c1STsutomu Uchino                                 static_cast< ::cppu::OWeakObject* >( this ),
197*b57309c1STsutomu Uchino                                 ::com::sun::star::frame::DispatchResultState::FAILURE,
198*b57309c1STsutomu Uchino                                 invokeResult );
199*b57309c1STsutomu Uchino                         try
200*b57309c1STsutomu Uchino                         {
201*b57309c1STsutomu Uchino                             xListener->dispatchFinished( aEvent ) ;
202*b57309c1STsutomu Uchino                         }
203*b57309c1STsutomu Uchino                         catch(RuntimeException & e)
204*b57309c1STsutomu Uchino                         {
205*b57309c1STsutomu Uchino                             OSL_TRACE(
206*b57309c1STsutomu Uchino                                 "ScriptProtocolHandler::dispatchWithNotification: caught RuntimeException"
207*b57309c1STsutomu Uchino                                 "while dispatchFinished with failture of the execution %s",
208*b57309c1STsutomu Uchino                                 ::rtl::OUStringToOString( e.Message,
209*b57309c1STsutomu Uchino                                 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
210*b57309c1STsutomu Uchino                         }
211*b57309c1STsutomu Uchino                     }
212cdf0e10cSrcweir                     return;
213*b57309c1STsutomu Uchino                 }
214cdf0e10cSrcweir             }
215cdf0e10cSrcweir 
216cdf0e10cSrcweir             // Creates a ScriptProvider ( if one is not created allready )
217cdf0e10cSrcweir             createScriptProvider();
218cdf0e10cSrcweir 
219cdf0e10cSrcweir             Reference< provider::XScript > xFunc =
220cdf0e10cSrcweir                 m_xScriptProvider->getScript( aURL.Complete );
221cdf0e10cSrcweir             ENSURE_OR_THROW( xFunc.is(),
222cdf0e10cSrcweir                 "ScriptProtocolHandler::dispatchWithNotification: validate xFunc - unable to obtain XScript interface" );
223cdf0e10cSrcweir 
224cdf0e10cSrcweir 
225cdf0e10cSrcweir             Sequence< Any > outArgs( 0 );
226cdf0e10cSrcweir             Sequence< sal_Int16 > outIndex;
227cdf0e10cSrcweir 
228cdf0e10cSrcweir             // attempt to protect the document against the script tampering with its Undo Context
229cdf0e10cSrcweir             ::std::auto_ptr< ::framework::DocumentUndoGuard > pUndoGuard;
230cdf0e10cSrcweir             if ( bIsDocumentScript )
231cdf0e10cSrcweir                 pUndoGuard.reset( new ::framework::DocumentUndoGuard( m_xScriptInvocation ) );
232cdf0e10cSrcweir 
233cdf0e10cSrcweir             bSuccess = sal_False;
234cdf0e10cSrcweir             while ( !bSuccess )
235cdf0e10cSrcweir             {
236cdf0e10cSrcweir                 Any aFirstCaughtException;
237cdf0e10cSrcweir                 try
238cdf0e10cSrcweir                 {
239cdf0e10cSrcweir                     invokeResult = xFunc->invoke( inArgs, outIndex, outArgs );
240cdf0e10cSrcweir                     bSuccess = sal_True;
241cdf0e10cSrcweir                 }
242cdf0e10cSrcweir                 catch( const provider::ScriptFrameworkErrorException& se )
243cdf0e10cSrcweir                 {
244cdf0e10cSrcweir                     if  ( !aFirstCaughtException.hasValue() )
245cdf0e10cSrcweir                         aFirstCaughtException = ::cppu::getCaughtException();
246cdf0e10cSrcweir 
247cdf0e10cSrcweir                     if ( se.errorType != provider::ScriptFrameworkErrorType::NO_SUCH_SCRIPT )
248cdf0e10cSrcweir                         // the only condition which allows us to retry is if there is no method with the
249cdf0e10cSrcweir                         // given name/signature
250cdf0e10cSrcweir                         ::cppu::throwException( aFirstCaughtException );
251cdf0e10cSrcweir 
252cdf0e10cSrcweir                     if ( inArgs.getLength() == 0 )
253cdf0e10cSrcweir                         // no chance to retry if we can't strip more in-args
254cdf0e10cSrcweir                         ::cppu::throwException( aFirstCaughtException );
255cdf0e10cSrcweir 
256cdf0e10cSrcweir                     // strip one argument, then retry
257cdf0e10cSrcweir                     inArgs.realloc( inArgs.getLength() - 1 );
258cdf0e10cSrcweir                 }
259cdf0e10cSrcweir             }
260cdf0e10cSrcweir         }
261cdf0e10cSrcweir         // Office doesn't handle exceptions rethrown here very well, it cores,
262cdf0e10cSrcweir         // all we can is log them and then set fail for the dispatch event!
263cdf0e10cSrcweir         // (if there is a listener of course)
264cdf0e10cSrcweir         catch ( const Exception & e )
265cdf0e10cSrcweir         {
266cdf0e10cSrcweir             aException = ::cppu::getCaughtException();
267cdf0e10cSrcweir 
268cdf0e10cSrcweir             ::rtl::OUString reason = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ScriptProtocolHandler::dispatch: caught " ) );
269cdf0e10cSrcweir 
270cdf0e10cSrcweir             invokeResult <<= reason.concat( aException.getValueTypeName() ).concat( e.Message );
271cdf0e10cSrcweir 
272cdf0e10cSrcweir 			bCaughtException = sal_True;
273cdf0e10cSrcweir         }
274cdf0e10cSrcweir     }
275cdf0e10cSrcweir     else
276cdf0e10cSrcweir     {
277cdf0e10cSrcweir         ::rtl::OUString reason = ::rtl::OUString::createFromAscii(
278cdf0e10cSrcweir         "ScriptProtocolHandler::dispatchWithNotification failed, ScriptProtocolHandler not initialised"
279cdf0e10cSrcweir         );
280cdf0e10cSrcweir         invokeResult <<= reason;
281cdf0e10cSrcweir     }
282cdf0e10cSrcweir 
283cdf0e10cSrcweir 	if ( bCaughtException )
284cdf0e10cSrcweir 	{
285cdf0e10cSrcweir 		SfxAbstractDialogFactory* pFact = SfxAbstractDialogFactory::Create();
286cdf0e10cSrcweir 
287cdf0e10cSrcweir 		if ( pFact != NULL )
288cdf0e10cSrcweir 		{
289cdf0e10cSrcweir 			VclAbstractDialog* pDlg =
290cdf0e10cSrcweir 				pFact->CreateScriptErrorDialog( NULL, aException );
291cdf0e10cSrcweir 
292cdf0e10cSrcweir 			if ( pDlg != NULL )
293cdf0e10cSrcweir 			{
294cdf0e10cSrcweir 				pDlg->Execute();
295cdf0e10cSrcweir 				delete pDlg;
296cdf0e10cSrcweir 			}
297cdf0e10cSrcweir 		}
298cdf0e10cSrcweir    	}
299cdf0e10cSrcweir 
300cdf0e10cSrcweir     if ( xListener.is() )
301cdf0e10cSrcweir     {
302cdf0e10cSrcweir         // always call dispatchFinished(), because we didn't load a document but
303cdf0e10cSrcweir         // executed a macro instead!
304cdf0e10cSrcweir         ::com::sun::star::frame::DispatchResultEvent aEvent;
305cdf0e10cSrcweir 
306cdf0e10cSrcweir         aEvent.Source = static_cast< ::cppu::OWeakObject* >( this );
307cdf0e10cSrcweir         aEvent.Result = invokeResult;
308cdf0e10cSrcweir         if ( bSuccess )
309cdf0e10cSrcweir         {
310cdf0e10cSrcweir             aEvent.State = ::com::sun::star::frame::DispatchResultState::SUCCESS;
311cdf0e10cSrcweir         }
312cdf0e10cSrcweir         else
313cdf0e10cSrcweir         {
314cdf0e10cSrcweir             aEvent.State = ::com::sun::star::frame::DispatchResultState::FAILURE;
315cdf0e10cSrcweir         }
316cdf0e10cSrcweir 
317cdf0e10cSrcweir         try
318cdf0e10cSrcweir         {
319cdf0e10cSrcweir             xListener->dispatchFinished( aEvent ) ;
320cdf0e10cSrcweir         }
321cdf0e10cSrcweir         catch(RuntimeException & e)
322cdf0e10cSrcweir         {
323cdf0e10cSrcweir             OSL_TRACE(
324cdf0e10cSrcweir             "ScriptProtocolHandler::dispatchWithNotification: caught RuntimeException"
325cdf0e10cSrcweir             "while dispatchFinished %s",
326cdf0e10cSrcweir             ::rtl::OUStringToOString( e.Message,
327cdf0e10cSrcweir             RTL_TEXTENCODING_ASCII_US ).pData->buffer );
328cdf0e10cSrcweir         }
329cdf0e10cSrcweir     }
330cdf0e10cSrcweir }
331cdf0e10cSrcweir 
dispatch(const URL & aURL,const Sequence<PropertyValue> & lArgs)332cdf0e10cSrcweir void SAL_CALL ScriptProtocolHandler::dispatch(
333cdf0e10cSrcweir const URL& aURL, const Sequence< PropertyValue >& lArgs )
334cdf0e10cSrcweir throw ( RuntimeException )
335cdf0e10cSrcweir {
336cdf0e10cSrcweir     dispatchWithNotification( aURL, lArgs, Reference< XDispatchResultListener >() );
337cdf0e10cSrcweir }
338cdf0e10cSrcweir 
addStatusListener(const Reference<XStatusListener> & xControl,const URL & aURL)339cdf0e10cSrcweir void SAL_CALL ScriptProtocolHandler::addStatusListener(
340cdf0e10cSrcweir const Reference< XStatusListener >& xControl, const URL& aURL )
341cdf0e10cSrcweir throw ( RuntimeException )
342cdf0e10cSrcweir {
343cdf0e10cSrcweir 	(void)xControl;
344cdf0e10cSrcweir 	(void)aURL;
345cdf0e10cSrcweir 
346cdf0e10cSrcweir     // implement if status is supported
347cdf0e10cSrcweir }
348cdf0e10cSrcweir 
removeStatusListener(const Reference<XStatusListener> & xControl,const URL & aURL)349cdf0e10cSrcweir void SAL_CALL ScriptProtocolHandler::removeStatusListener(
350cdf0e10cSrcweir const Reference< XStatusListener >& xControl, const URL& aURL )
351cdf0e10cSrcweir throw ( RuntimeException )
352cdf0e10cSrcweir {
353cdf0e10cSrcweir 	(void)xControl;
354cdf0e10cSrcweir 	(void)aURL;
355cdf0e10cSrcweir }
356cdf0e10cSrcweir 
357cdf0e10cSrcweir bool
getScriptInvocation()358cdf0e10cSrcweir ScriptProtocolHandler::getScriptInvocation()
359cdf0e10cSrcweir {
360cdf0e10cSrcweir     if ( !m_xScriptInvocation.is() && m_xFrame.is() )
361cdf0e10cSrcweir     {
362cdf0e10cSrcweir         Reference< XController > xController = m_xFrame->getController();
363cdf0e10cSrcweir         if ( xController .is() )
364cdf0e10cSrcweir         {
365cdf0e10cSrcweir             // try to obtain an XScriptInvocationContext interface, preferred from the
366cdf0e10cSrcweir             // mode, then from the controller
367cdf0e10cSrcweir             if ( !m_xScriptInvocation.set( xController->getModel(), UNO_QUERY ) )
368cdf0e10cSrcweir                 m_xScriptInvocation.set( xController, UNO_QUERY );
369cdf0e10cSrcweir         }
370*b57309c1STsutomu Uchino         else
371*b57309c1STsutomu Uchino         {
372*b57309c1STsutomu Uchino             Reference< XFrame > xFrame( m_xFrame.get(), UNO_QUERY );
373*b57309c1STsutomu Uchino             if ( xFrame.is() )
374*b57309c1STsutomu Uchino             {
375*b57309c1STsutomu Uchino                 SfxFrame* pFrame = NULL;
376*b57309c1STsutomu Uchino                 for ( pFrame = SfxFrame::GetFirst(); pFrame; pFrame = SfxFrame::GetNext( *pFrame ) )
377*b57309c1STsutomu Uchino                 {
378*b57309c1STsutomu Uchino                     if ( pFrame->GetFrameInterface() == xFrame )
379*b57309c1STsutomu Uchino                         break;
380*b57309c1STsutomu Uchino                 }
381*b57309c1STsutomu Uchino                 SfxObjectShell* pDocShell = pFrame ? pFrame->GetCurrentDocument() : SfxObjectShell::Current();
382*b57309c1STsutomu Uchino                 if ( pDocShell )
383*b57309c1STsutomu Uchino                 {
384*b57309c1STsutomu Uchino                     Reference< XModel > xModel( pDocShell->GetModel() );
385*b57309c1STsutomu Uchino                     m_xScriptInvocation.set( xModel, UNO_QUERY );
386*b57309c1STsutomu Uchino                 }
387*b57309c1STsutomu Uchino             }
388*b57309c1STsutomu Uchino         }
389cdf0e10cSrcweir     }
390cdf0e10cSrcweir     return m_xScriptInvocation.is();
391cdf0e10cSrcweir }
392cdf0e10cSrcweir 
createScriptProvider()393cdf0e10cSrcweir void ScriptProtocolHandler::createScriptProvider()
394cdf0e10cSrcweir {
395cdf0e10cSrcweir     if ( m_xScriptProvider.is() )
396cdf0e10cSrcweir         return;
397cdf0e10cSrcweir 
398cdf0e10cSrcweir     try
399cdf0e10cSrcweir     {
400cdf0e10cSrcweir         // first, ask the component supporting the XScriptInvocationContext interface
401cdf0e10cSrcweir         // (if there is one) for a script provider
402cdf0e10cSrcweir         if ( getScriptInvocation() )
403cdf0e10cSrcweir         {
404cdf0e10cSrcweir             Reference< XScriptProviderSupplier > xSPS( m_xScriptInvocation, UNO_QUERY );
405cdf0e10cSrcweir             if ( xSPS.is() )
406cdf0e10cSrcweir                 m_xScriptProvider = xSPS->getScriptProvider();
407cdf0e10cSrcweir         }
408cdf0e10cSrcweir 
409cdf0e10cSrcweir         // second, ask the model in our frame
410cdf0e10cSrcweir         if ( !m_xScriptProvider.is() && m_xFrame.is() )
411cdf0e10cSrcweir         {
412cdf0e10cSrcweir             Reference< XController > xController = m_xFrame->getController();
413cdf0e10cSrcweir             if ( xController .is() )
414cdf0e10cSrcweir             {
415cdf0e10cSrcweir                 Reference< XScriptProviderSupplier > xSPS( xController->getModel(), UNO_QUERY );
416cdf0e10cSrcweir                 if ( xSPS.is() )
417cdf0e10cSrcweir                     m_xScriptProvider = xSPS->getScriptProvider();
418cdf0e10cSrcweir             }
419cdf0e10cSrcweir         }
420cdf0e10cSrcweir 
421cdf0e10cSrcweir 
422cdf0e10cSrcweir         // as a fallback, ask the controller
423cdf0e10cSrcweir         if ( !m_xScriptProvider.is() && m_xFrame.is() )
424cdf0e10cSrcweir         {
425cdf0e10cSrcweir             Reference< XScriptProviderSupplier > xSPS( m_xFrame->getController(), UNO_QUERY );
426cdf0e10cSrcweir             if ( xSPS.is() )
427cdf0e10cSrcweir                 m_xScriptProvider = xSPS->getScriptProvider();
428cdf0e10cSrcweir         }
429cdf0e10cSrcweir 
430cdf0e10cSrcweir         // if nothing of this is successful, use the master script provider
431cdf0e10cSrcweir         if ( !m_xScriptProvider.is() )
432cdf0e10cSrcweir         {
433cdf0e10cSrcweir             Reference< XPropertySet > xProps( m_xFactory, UNO_QUERY_THROW );
434cdf0e10cSrcweir 
435cdf0e10cSrcweir             ::rtl::OUString dc(
436cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ) );
437cdf0e10cSrcweir 
438cdf0e10cSrcweir             Reference< XComponentContext > xCtx(
439cdf0e10cSrcweir                 xProps->getPropertyValue( dc ), UNO_QUERY_THROW );
440cdf0e10cSrcweir 
441cdf0e10cSrcweir             ::rtl::OUString tmspf = ::rtl::OUString::createFromAscii(
442cdf0e10cSrcweir                 "/singletons/com.sun.star.script.provider.theMasterScriptProviderFactory");
443cdf0e10cSrcweir 
444cdf0e10cSrcweir             Reference< provider::XScriptProviderFactory > xFac(
445cdf0e10cSrcweir                 xCtx->getValueByName( tmspf ), UNO_QUERY_THROW );
446cdf0e10cSrcweir 
447cdf0e10cSrcweir             Any aContext;
448cdf0e10cSrcweir             if ( getScriptInvocation() )
449cdf0e10cSrcweir                 aContext = makeAny( m_xScriptInvocation );
450cdf0e10cSrcweir             m_xScriptProvider = Reference< provider::XScriptProvider > (
451cdf0e10cSrcweir                 xFac->createScriptProvider( aContext ), UNO_QUERY_THROW );
452cdf0e10cSrcweir         }
453cdf0e10cSrcweir     }
454cdf0e10cSrcweir     catch ( RuntimeException & e )
455cdf0e10cSrcweir     {
456cdf0e10cSrcweir         ::rtl::OUString temp = OUSTR( "ScriptProtocolHandler::createScriptProvider(),  " );
457cdf0e10cSrcweir         throw RuntimeException( temp.concat( e.Message ), Reference< XInterface >() );
458cdf0e10cSrcweir     }
459cdf0e10cSrcweir     catch ( Exception & e )
460cdf0e10cSrcweir     {
461cdf0e10cSrcweir         ::rtl::OUString temp = OUSTR( "ScriptProtocolHandler::createScriptProvider: " );
462cdf0e10cSrcweir         throw RuntimeException( temp.concat( e.Message ), Reference< XInterface >() );
463cdf0e10cSrcweir     }
464cdf0e10cSrcweir }
465cdf0e10cSrcweir 
ScriptProtocolHandler(Reference<css::lang::XMultiServiceFactory> const & rFact)466cdf0e10cSrcweir ScriptProtocolHandler::ScriptProtocolHandler(
467cdf0e10cSrcweir Reference< css::lang::XMultiServiceFactory > const& rFact ) :
468cdf0e10cSrcweir m_bInitialised( false ), m_xFactory( rFact )
469cdf0e10cSrcweir {
470cdf0e10cSrcweir }
471cdf0e10cSrcweir 
~ScriptProtocolHandler()472cdf0e10cSrcweir ScriptProtocolHandler::~ScriptProtocolHandler()
473cdf0e10cSrcweir {
474cdf0e10cSrcweir }
475cdf0e10cSrcweir 
476cdf0e10cSrcweir /* XServiceInfo */
getImplementationName()477cdf0e10cSrcweir ::rtl::OUString SAL_CALL ScriptProtocolHandler::getImplementationName( )
478cdf0e10cSrcweir throw( RuntimeException )
479cdf0e10cSrcweir {
480cdf0e10cSrcweir     return impl_getStaticImplementationName();
481cdf0e10cSrcweir }
482cdf0e10cSrcweir 
483cdf0e10cSrcweir /* XServiceInfo */
supportsService(const::rtl::OUString & sServiceName)484cdf0e10cSrcweir sal_Bool SAL_CALL ScriptProtocolHandler::supportsService(
485cdf0e10cSrcweir const ::rtl::OUString& sServiceName )
486cdf0e10cSrcweir throw( RuntimeException )
487cdf0e10cSrcweir {
488cdf0e10cSrcweir     Sequence< ::rtl::OUString > seqServiceNames = getSupportedServiceNames();
489cdf0e10cSrcweir     const ::rtl::OUString* pArray = seqServiceNames.getConstArray();
490cdf0e10cSrcweir     for ( sal_Int32 nCounter = 0; nCounter < seqServiceNames.getLength(); nCounter++ )
491cdf0e10cSrcweir     {
492cdf0e10cSrcweir         if ( pArray[ nCounter ] == sServiceName )
493cdf0e10cSrcweir         {
494cdf0e10cSrcweir             return sal_True ;
495cdf0e10cSrcweir         }
496cdf0e10cSrcweir     }
497cdf0e10cSrcweir 
498cdf0e10cSrcweir     return sal_False ;
499cdf0e10cSrcweir }
500cdf0e10cSrcweir 
501cdf0e10cSrcweir /* XServiceInfo */
getSupportedServiceNames()502cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL ScriptProtocolHandler::getSupportedServiceNames()
503cdf0e10cSrcweir throw( RuntimeException )
504cdf0e10cSrcweir {
505cdf0e10cSrcweir     return impl_getStaticSupportedServiceNames();
506cdf0e10cSrcweir }
507cdf0e10cSrcweir 
508cdf0e10cSrcweir /* Helper for XServiceInfo */
impl_getStaticSupportedServiceNames()509cdf0e10cSrcweir Sequence< ::rtl::OUString > ScriptProtocolHandler::impl_getStaticSupportedServiceNames()
510cdf0e10cSrcweir {
511cdf0e10cSrcweir     ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
512cdf0e10cSrcweir     Sequence< ::rtl::OUString > seqServiceNames( 1 );
513cdf0e10cSrcweir     seqServiceNames.getArray() [ 0 ] =
514cdf0e10cSrcweir         ::rtl::OUString::createFromAscii( ::scripting_protocolhandler::MYSERVICENAME );
515cdf0e10cSrcweir     return seqServiceNames ;
516cdf0e10cSrcweir }
517cdf0e10cSrcweir 
518cdf0e10cSrcweir /* Helper for XServiceInfo */
impl_getStaticImplementationName()519cdf0e10cSrcweir ::rtl::OUString ScriptProtocolHandler::impl_getStaticImplementationName()
520cdf0e10cSrcweir {
521cdf0e10cSrcweir     return ::rtl::OUString::createFromAscii( ::scripting_protocolhandler::MYIMPLNAME );
522cdf0e10cSrcweir }
523cdf0e10cSrcweir 
524cdf0e10cSrcweir /* Helper for registry */
impl_createInstance(const Reference<css::lang::XMultiServiceFactory> & xServiceManager)525cdf0e10cSrcweir Reference< XInterface > SAL_CALL ScriptProtocolHandler::impl_createInstance(
526cdf0e10cSrcweir const Reference< css::lang::XMultiServiceFactory >& xServiceManager )
527cdf0e10cSrcweir throw( RuntimeException )
528cdf0e10cSrcweir {
529cdf0e10cSrcweir     return Reference< XInterface > ( *new ScriptProtocolHandler( xServiceManager ) );
530cdf0e10cSrcweir }
531cdf0e10cSrcweir 
532cdf0e10cSrcweir /* Factory for registration */
impl_createFactory(const Reference<XMultiServiceFactory> & xServiceManager)533cdf0e10cSrcweir Reference< XSingleServiceFactory > ScriptProtocolHandler::impl_createFactory(
534cdf0e10cSrcweir const Reference< XMultiServiceFactory >& xServiceManager )
535cdf0e10cSrcweir {
536cdf0e10cSrcweir     Reference< XSingleServiceFactory > xReturn (
537cdf0e10cSrcweir         cppu::createSingleFactory( xServiceManager,
538cdf0e10cSrcweir             ScriptProtocolHandler::impl_getStaticImplementationName(),
539cdf0e10cSrcweir             ScriptProtocolHandler::impl_createInstance,
540cdf0e10cSrcweir             ScriptProtocolHandler::impl_getStaticSupportedServiceNames() )
541cdf0e10cSrcweir     );
542cdf0e10cSrcweir     return xReturn;
543cdf0e10cSrcweir }
544cdf0e10cSrcweir 
545cdf0e10cSrcweir } // namespace scripting_protocolhandler
546cdf0e10cSrcweir 
547cdf0e10cSrcweir /* exported functions for registration */
548cdf0e10cSrcweir extern "C"
549cdf0e10cSrcweir {
550cdf0e10cSrcweir 
551cdf0e10cSrcweir #undef css
552cdf0e10cSrcweir #define css ::com::sun::star
553cdf0e10cSrcweir 
component_getImplementationEnvironment(const sal_Char ** ppEnvironmentTypeName,uno_Environment ** ppEnvironment)554cdf0e10cSrcweir     void SAL_CALL component_getImplementationEnvironment(
555cdf0e10cSrcweir         const sal_Char** ppEnvironmentTypeName, uno_Environment** ppEnvironment )
556cdf0e10cSrcweir     {
557cdf0e10cSrcweir 		(void)ppEnvironment;
558cdf0e10cSrcweir 
559cdf0e10cSrcweir         *ppEnvironmentTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME ;
560cdf0e10cSrcweir     }
561cdf0e10cSrcweir 
component_getFactory(const sal_Char * pImplementationName,void * pServiceManager,void * pRegistryKey)562cdf0e10cSrcweir     void* SAL_CALL component_getFactory( const sal_Char * pImplementationName ,
563cdf0e10cSrcweir                                          void * pServiceManager ,
564cdf0e10cSrcweir                                          void * pRegistryKey )
565cdf0e10cSrcweir     {
566cdf0e10cSrcweir 		(void)pRegistryKey;
567cdf0e10cSrcweir 
568cdf0e10cSrcweir         // Set default return value for this operation - if it failed.
569cdf0e10cSrcweir         void * pReturn = NULL ;
570cdf0e10cSrcweir 
571cdf0e10cSrcweir         if (
572cdf0e10cSrcweir             ( pImplementationName != NULL ) &&
573cdf0e10cSrcweir             ( pServiceManager != NULL )
574cdf0e10cSrcweir         )
575cdf0e10cSrcweir         {
576cdf0e10cSrcweir             // Define variables which are used in following macros.
577cdf0e10cSrcweir             ::com::sun::star::uno::Reference<
578cdf0e10cSrcweir             ::com::sun::star::lang::XSingleServiceFactory > xFactory ;
579cdf0e10cSrcweir             ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >
580cdf0e10cSrcweir             xServiceManager( reinterpret_cast<
581cdf0e10cSrcweir             ::com::sun::star::lang::XMultiServiceFactory* >( pServiceManager ) ) ;
582cdf0e10cSrcweir 
583cdf0e10cSrcweir             if ( ::scripting_protocolhandler::ScriptProtocolHandler::impl_getStaticImplementationName().equals(
584cdf0e10cSrcweir                 ::rtl::OUString::createFromAscii( pImplementationName ) ) )
585cdf0e10cSrcweir             {
586cdf0e10cSrcweir                 xFactory = ::scripting_protocolhandler::ScriptProtocolHandler::impl_createFactory( xServiceManager );
587cdf0e10cSrcweir             }
588cdf0e10cSrcweir 
589cdf0e10cSrcweir             // Factory is valid - service was found.
590cdf0e10cSrcweir             if ( xFactory.is() )
591cdf0e10cSrcweir             {
592cdf0e10cSrcweir                 xFactory->acquire();
593cdf0e10cSrcweir                 pReturn = xFactory.get();
594cdf0e10cSrcweir             }
595cdf0e10cSrcweir         }
596cdf0e10cSrcweir 
597cdf0e10cSrcweir         // Return with result of this operation.
598cdf0e10cSrcweir         return pReturn ;
599cdf0e10cSrcweir     }
600cdf0e10cSrcweir } // extern "C"
601cdf0e10cSrcweir 
602cdf0e10cSrcweir 
603