xref: /trunk/main/scripting/source/protocolhandler/scripthandler.cxx (revision fb9ad7aa17549019ccdd2762a97d104449abf45d)
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
10cdf0e10cSrcweir  *
112c696243SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
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.
19cdf0e10cSrcweir  *
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 
44cdf0e10cSrcweir #include <sfx2/objsh.hxx>
45cdf0e10cSrcweir #include <sfx2/frame.hxx>
46cdf0e10cSrcweir #include <sfx2/sfxdlg.hxx>
47cdf0e10cSrcweir #include <vcl/abstdlg.hxx>
48cdf0e10cSrcweir #include <tools/diagnose_ex.h>
49cdf0e10cSrcweir 
50cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
51cdf0e10cSrcweir #include <cppuhelper/exc_hlp.hxx>
5202463bafSDamjan Jovanovic #include <cppuhelper/implementationentry.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"
5802463bafSDamjan Jovanovic #include "com/sun/star/uri/UriReferenceFactory.hpp"
59cdf0e10cSrcweir #include "com/sun/star/uri/XUriReferenceFactory.hpp"
60cdf0e10cSrcweir #include "com/sun/star/uri/XVndSunStarScriptUrl.hpp"
6115daf392SDamjan Jovanovic #include <com/sun/star/uri/XVndSunStarScriptUrlReference.hpp>
62cdf0e10cSrcweir #include "com/sun/star/beans/XPropertySet.hpp"
63cdf0e10cSrcweir 
64cdf0e10cSrcweir using namespace ::com::sun::star;
65cdf0e10cSrcweir using namespace ::com::sun::star::uno;
66cdf0e10cSrcweir using namespace ::com::sun::star::frame;
67cdf0e10cSrcweir using namespace ::com::sun::star::util;
68cdf0e10cSrcweir using namespace ::com::sun::star::beans;
69cdf0e10cSrcweir using namespace ::com::sun::star::lang;
70cdf0e10cSrcweir using namespace ::com::sun::star::script;
71cdf0e10cSrcweir using namespace ::com::sun::star::script::provider;
72cdf0e10cSrcweir using namespace ::com::sun::star::document;
73cdf0e10cSrcweir 
74cdf0e10cSrcweir namespace scripting_protocolhandler
75cdf0e10cSrcweir {
76cdf0e10cSrcweir 
77cdf0e10cSrcweir const sal_Char * const MYSERVICENAME = "com.sun.star.frame.ProtocolHandler";
78cdf0e10cSrcweir const sal_Char * const MYIMPLNAME = "com.sun.star.comp.ScriptProtocolHandler";
79cdf0e10cSrcweir const sal_Char * MYSCHEME = "vnd.sun.star.script";
80cdf0e10cSrcweir const sal_Int32 MYSCHEME_LEN = 20;
81cdf0e10cSrcweir 
8202463bafSDamjan Jovanovic rtl_StandardModuleCount g_moduleCount = MODULE_COUNT_INIT;
8302463bafSDamjan Jovanovic 
84cdf0e10cSrcweir void SAL_CALL ScriptProtocolHandler::initialize(
85cdf0e10cSrcweir     const css::uno::Sequence < css::uno::Any >& aArguments )
86cdf0e10cSrcweir     throw ( css::uno::Exception )
87cdf0e10cSrcweir {
88cdf0e10cSrcweir     if ( m_bInitialised )
89cdf0e10cSrcweir     {
90cdf0e10cSrcweir         return ;
91cdf0e10cSrcweir     }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     // first argument contains a reference to the frame (may be empty or the desktop,
94cdf0e10cSrcweir     // but usually it's a "real" frame)
95cdf0e10cSrcweir     if ( aArguments.getLength() &&
96cdf0e10cSrcweir          sal_False == ( aArguments[ 0 ] >>= m_xFrame ) )
97cdf0e10cSrcweir     {
98cdf0e10cSrcweir         ::rtl::OUString temp = OUSTR( "ScriptProtocolHandler::initialize: could not extract reference to the frame" );
99cdf0e10cSrcweir         throw RuntimeException( temp, Reference< XInterface >() );
100cdf0e10cSrcweir     }
101cdf0e10cSrcweir 
10202463bafSDamjan Jovanovic     ENSURE_OR_THROW( m_xCtx.is(), "ScriptProtocolHandler::initialize: No Component Context available" );
103cdf0e10cSrcweir     m_bInitialised = true;
104cdf0e10cSrcweir }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir Reference< XDispatch > SAL_CALL ScriptProtocolHandler::queryDispatch(
107cdf0e10cSrcweir     const URL& aURL, const ::rtl::OUString& sTargetFrameName, sal_Int32 nSearchFlags )
108cdf0e10cSrcweir     throw( ::com::sun::star::uno::RuntimeException )
109cdf0e10cSrcweir {
110cdf0e10cSrcweir     (void)sTargetFrameName;
111cdf0e10cSrcweir     (void)nSearchFlags;
112cdf0e10cSrcweir 
113cdf0e10cSrcweir     Reference< XDispatch > xDispatcher;
114cdf0e10cSrcweir     // get scheme of url
115cdf0e10cSrcweir 
11602463bafSDamjan Jovanovic     Reference< uri::XUriReferenceFactory > xFac ( uri::UriReferenceFactory::create( m_xCtx ) );
117cdf0e10cSrcweir     if ( xFac.is() )
118cdf0e10cSrcweir     {
119cdf0e10cSrcweir         Reference<  uri::XUriReference > uriRef(
120cdf0e10cSrcweir             xFac->parse( aURL.Complete ), UNO_QUERY );
121cdf0e10cSrcweir         if ( uriRef.is() )
122cdf0e10cSrcweir         {
123cdf0e10cSrcweir             if ( uriRef->getScheme().equals( ::rtl::OUString::createFromAscii( ::scripting_protocolhandler::MYSCHEME ) ) )
124cdf0e10cSrcweir             {
125cdf0e10cSrcweir                 xDispatcher = this;
126cdf0e10cSrcweir             }
127cdf0e10cSrcweir         }
128cdf0e10cSrcweir     }
129cdf0e10cSrcweir 
130cdf0e10cSrcweir     return xDispatcher;
131cdf0e10cSrcweir }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir Sequence< Reference< XDispatch > > SAL_CALL
134cdf0e10cSrcweir ScriptProtocolHandler::queryDispatches(
135cdf0e10cSrcweir const Sequence < DispatchDescriptor >& seqDescriptor )
136cdf0e10cSrcweir throw( RuntimeException )
137cdf0e10cSrcweir {
138cdf0e10cSrcweir     sal_Int32 nCount = seqDescriptor.getLength();
139cdf0e10cSrcweir     Sequence< Reference< XDispatch > > lDispatcher( nCount );
140cdf0e10cSrcweir     for ( sal_Int32 i = 0; i < nCount; ++i )
141cdf0e10cSrcweir     {
142cdf0e10cSrcweir         lDispatcher[ i ] = this->queryDispatch( seqDescriptor[ i ].FeatureURL,
143cdf0e10cSrcweir                                                 seqDescriptor[ i ].FrameName,
144cdf0e10cSrcweir                                                 seqDescriptor[ i ].SearchFlags );
145cdf0e10cSrcweir     }
146cdf0e10cSrcweir     return lDispatcher;
147cdf0e10cSrcweir }
148cdf0e10cSrcweir 
149cdf0e10cSrcweir void SAL_CALL ScriptProtocolHandler::dispatchWithNotification(
150cdf0e10cSrcweir     const URL& aURL, const Sequence < PropertyValue >& lArgs,
151cdf0e10cSrcweir     const Reference< XDispatchResultListener >& xListener )
152cdf0e10cSrcweir     throw ( RuntimeException )
153cdf0e10cSrcweir {
154cdf0e10cSrcweir 
155cdf0e10cSrcweir     sal_Bool bSuccess = sal_False;
156cdf0e10cSrcweir     Any invokeResult;
157cdf0e10cSrcweir     bool bCaughtException = sal_False;
158cdf0e10cSrcweir     Any aException;
159cdf0e10cSrcweir 
160cdf0e10cSrcweir     if ( m_bInitialised )
161cdf0e10cSrcweir     {
162cdf0e10cSrcweir         try
163cdf0e10cSrcweir         {
16415daf392SDamjan Jovanovic             Reference< uri::XUriReferenceFactory > xFac( uri::UriReferenceFactory::create( m_xCtx ) );
16515daf392SDamjan Jovanovic             Reference< uri::XVndSunStarScriptUrlReference > xScriptUri( xFac->parse( aURL.Complete ), UNO_QUERY_THROW );
16615daf392SDamjan Jovanovic             ::rtl::OUString sLocation = xScriptUri->getParameter( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "location" ) ) );
16715daf392SDamjan Jovanovic             bool bIsDocumentScript = ( sLocation == ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "document" ) ) );
168cdf0e10cSrcweir 
169*fb9ad7aaSArrigo Marchiori             // obtain the component for our security check. We could check bIsDocumentScript but the "location" could be forged
170*fb9ad7aaSArrigo Marchiori             if ( getScriptInvocation() ) {
171cdf0e10cSrcweir                     Reference< XEmbeddedScripts > xDocumentScripts;
172cdf0e10cSrcweir                     xDocumentScripts.set( m_xScriptInvocation->getScriptContainer(), UNO_SET_THROW );
173cdf0e10cSrcweir 
174cdf0e10cSrcweir                 OSL_ENSURE( xDocumentScripts.is(), "ScriptProtocolHandler::dispatchWithNotification: can't do the security check!" );
175cdf0e10cSrcweir                 if ( !xDocumentScripts.is() || !xDocumentScripts->getAllowMacroExecution() )
176718aea48STsutomu Uchino                 {
177718aea48STsutomu Uchino                     if ( xListener.is() )
178718aea48STsutomu Uchino                     {
179718aea48STsutomu Uchino                         ::com::sun::star::frame::DispatchResultEvent aEvent(
180718aea48STsutomu Uchino                                 static_cast< ::cppu::OWeakObject* >( this ),
181718aea48STsutomu Uchino                                 ::com::sun::star::frame::DispatchResultState::FAILURE,
182718aea48STsutomu Uchino                                 invokeResult );
183718aea48STsutomu Uchino                         try
184718aea48STsutomu Uchino                         {
185718aea48STsutomu Uchino                             xListener->dispatchFinished( aEvent ) ;
186718aea48STsutomu Uchino                         }
187718aea48STsutomu Uchino                         catch(RuntimeException & e)
188718aea48STsutomu Uchino                         {
189718aea48STsutomu Uchino                             OSL_TRACE(
190718aea48STsutomu Uchino                                 "ScriptProtocolHandler::dispatchWithNotification: caught RuntimeException"
191718aea48STsutomu Uchino                                 "while dispatchFinished with failture of the execution %s",
192718aea48STsutomu Uchino                                 ::rtl::OUStringToOString( e.Message,
193718aea48STsutomu Uchino                                 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
194718aea48STsutomu Uchino                         }
195718aea48STsutomu Uchino                     }
196cdf0e10cSrcweir                     return;
197cdf0e10cSrcweir                 }
198718aea48STsutomu Uchino             }
199cdf0e10cSrcweir 
20086e1cf34SPedro Giffuni             // Creates a ScriptProvider ( if one is not created already )
201cdf0e10cSrcweir             createScriptProvider();
202cdf0e10cSrcweir 
203cdf0e10cSrcweir             Reference< provider::XScript > xFunc =
204cdf0e10cSrcweir                 m_xScriptProvider->getScript( aURL.Complete );
205cdf0e10cSrcweir             ENSURE_OR_THROW( xFunc.is(),
206cdf0e10cSrcweir                 "ScriptProtocolHandler::dispatchWithNotification: validate xFunc - unable to obtain XScript interface" );
207cdf0e10cSrcweir 
208cdf0e10cSrcweir 
209cdf0e10cSrcweir             Sequence< Any > inArgs( 0 );
210cdf0e10cSrcweir             Sequence< Any > outArgs( 0 );
211cdf0e10cSrcweir             Sequence< sal_Int16 > outIndex;
212cdf0e10cSrcweir 
213cdf0e10cSrcweir             if ( lArgs.getLength() > 0 )
214cdf0e10cSrcweir             {
215cdf0e10cSrcweir                int argCount = 0;
216cdf0e10cSrcweir                for ( int index = 0; index < lArgs.getLength(); index++ )
217cdf0e10cSrcweir                {
218cdf0e10cSrcweir                    // Sometimes we get a propertyval with name = "Referer"
219cdf0e10cSrcweir                    // this is not an argument to be passed to script, so
220cdf0e10cSrcweir                    // ignore.
221cdf0e10cSrcweir                    if ( lArgs[ index ].Name.compareToAscii("Referer") != 0  ||
222cdf0e10cSrcweir                         lArgs[ index ].Name.getLength() == 0 )
223cdf0e10cSrcweir                    {
224cdf0e10cSrcweir                        inArgs.realloc( ++argCount );
225cdf0e10cSrcweir                        inArgs[ argCount - 1 ] = lArgs[ index ].Value;
226cdf0e10cSrcweir                    }
227cdf0e10cSrcweir                }
228cdf0e10cSrcweir             }
229cdf0e10cSrcweir 
230cdf0e10cSrcweir             // attempt to protect the document against the script tampering with its Undo Context
231cdf0e10cSrcweir             ::std::auto_ptr< ::framework::DocumentUndoGuard > pUndoGuard;
232cdf0e10cSrcweir             if ( bIsDocumentScript )
233cdf0e10cSrcweir                 pUndoGuard.reset( new ::framework::DocumentUndoGuard( m_xScriptInvocation ) );
234cdf0e10cSrcweir 
235cdf0e10cSrcweir             bSuccess = sal_False;
236cdf0e10cSrcweir             while ( !bSuccess )
237cdf0e10cSrcweir             {
238cdf0e10cSrcweir                 Any aFirstCaughtException;
239cdf0e10cSrcweir                 try
240cdf0e10cSrcweir                 {
241cdf0e10cSrcweir                     invokeResult = xFunc->invoke( inArgs, outIndex, outArgs );
242cdf0e10cSrcweir                     bSuccess = sal_True;
243cdf0e10cSrcweir                 }
244cdf0e10cSrcweir                 catch( const provider::ScriptFrameworkErrorException& se )
245cdf0e10cSrcweir                 {
246cdf0e10cSrcweir                     if  ( !aFirstCaughtException.hasValue() )
247cdf0e10cSrcweir                         aFirstCaughtException = ::cppu::getCaughtException();
248cdf0e10cSrcweir 
249cdf0e10cSrcweir                     if ( se.errorType != provider::ScriptFrameworkErrorType::NO_SUCH_SCRIPT )
250cdf0e10cSrcweir                         // the only condition which allows us to retry is if there is no method with the
251cdf0e10cSrcweir                         // given name/signature
252cdf0e10cSrcweir                         ::cppu::throwException( aFirstCaughtException );
253cdf0e10cSrcweir 
254cdf0e10cSrcweir                     if ( inArgs.getLength() == 0 )
255cdf0e10cSrcweir                         // no chance to retry if we can't strip more in-args
256cdf0e10cSrcweir                         ::cppu::throwException( aFirstCaughtException );
257cdf0e10cSrcweir 
258cdf0e10cSrcweir                     // strip one argument, then retry
259cdf0e10cSrcweir                     inArgs.realloc( inArgs.getLength() - 1 );
260cdf0e10cSrcweir                 }
261cdf0e10cSrcweir             }
262cdf0e10cSrcweir         }
263cdf0e10cSrcweir         // Office doesn't handle exceptions rethrown here very well, it cores,
264cdf0e10cSrcweir         // all we can is log them and then set fail for the dispatch event!
265cdf0e10cSrcweir         // (if there is a listener of course)
266cdf0e10cSrcweir         catch ( const Exception & e )
267cdf0e10cSrcweir         {
268cdf0e10cSrcweir             aException = ::cppu::getCaughtException();
269cdf0e10cSrcweir 
270cdf0e10cSrcweir             ::rtl::OUString reason = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ScriptProtocolHandler::dispatch: caught " ) );
271cdf0e10cSrcweir 
272cdf0e10cSrcweir             invokeResult <<= reason.concat( aException.getValueTypeName() ).concat( e.Message );
273cdf0e10cSrcweir 
274cdf0e10cSrcweir             bCaughtException = sal_True;
275cdf0e10cSrcweir         }
276cdf0e10cSrcweir     }
277cdf0e10cSrcweir     else
278cdf0e10cSrcweir     {
279cdf0e10cSrcweir         ::rtl::OUString reason = ::rtl::OUString::createFromAscii(
280cdf0e10cSrcweir         "ScriptProtocolHandler::dispatchWithNotification failed, ScriptProtocolHandler not initialised"
281cdf0e10cSrcweir         );
282cdf0e10cSrcweir         invokeResult <<= reason;
283cdf0e10cSrcweir     }
284cdf0e10cSrcweir 
285cdf0e10cSrcweir     if ( bCaughtException )
286cdf0e10cSrcweir     {
287cdf0e10cSrcweir         SfxAbstractDialogFactory* pFact = SfxAbstractDialogFactory::Create();
288cdf0e10cSrcweir 
289cdf0e10cSrcweir         if ( pFact != NULL )
290cdf0e10cSrcweir         {
291cdf0e10cSrcweir             VclAbstractDialog* pDlg =
292cdf0e10cSrcweir                 pFact->CreateScriptErrorDialog( NULL, aException );
293cdf0e10cSrcweir 
294cdf0e10cSrcweir             if ( pDlg != NULL )
295cdf0e10cSrcweir             {
296cdf0e10cSrcweir                 pDlg->Execute();
297cdf0e10cSrcweir                 delete pDlg;
298cdf0e10cSrcweir             }
299cdf0e10cSrcweir         }
300cdf0e10cSrcweir     }
301cdf0e10cSrcweir 
302cdf0e10cSrcweir     if ( xListener.is() )
303cdf0e10cSrcweir     {
304cdf0e10cSrcweir         // always call dispatchFinished(), because we didn't load a document but
305cdf0e10cSrcweir         // executed a macro instead!
306cdf0e10cSrcweir         ::com::sun::star::frame::DispatchResultEvent aEvent;
307cdf0e10cSrcweir 
308cdf0e10cSrcweir         aEvent.Source = static_cast< ::cppu::OWeakObject* >( this );
309cdf0e10cSrcweir         aEvent.Result = invokeResult;
310cdf0e10cSrcweir         if ( bSuccess )
311cdf0e10cSrcweir         {
312cdf0e10cSrcweir             aEvent.State = ::com::sun::star::frame::DispatchResultState::SUCCESS;
313cdf0e10cSrcweir         }
314cdf0e10cSrcweir         else
315cdf0e10cSrcweir         {
316cdf0e10cSrcweir             aEvent.State = ::com::sun::star::frame::DispatchResultState::FAILURE;
317cdf0e10cSrcweir         }
318cdf0e10cSrcweir 
319cdf0e10cSrcweir         try
320cdf0e10cSrcweir         {
321cdf0e10cSrcweir             xListener->dispatchFinished( aEvent ) ;
322cdf0e10cSrcweir         }
323cdf0e10cSrcweir         catch(RuntimeException & e)
324cdf0e10cSrcweir         {
325cdf0e10cSrcweir             OSL_TRACE(
326cdf0e10cSrcweir             "ScriptProtocolHandler::dispatchWithNotification: caught RuntimeException"
327cdf0e10cSrcweir             "while dispatchFinished %s",
328cdf0e10cSrcweir             ::rtl::OUStringToOString( e.Message,
329cdf0e10cSrcweir             RTL_TEXTENCODING_ASCII_US ).pData->buffer );
330cdf0e10cSrcweir         }
331cdf0e10cSrcweir     }
332cdf0e10cSrcweir }
333cdf0e10cSrcweir 
334cdf0e10cSrcweir void SAL_CALL ScriptProtocolHandler::dispatch(
335cdf0e10cSrcweir const URL& aURL, const Sequence< PropertyValue >& lArgs )
336cdf0e10cSrcweir throw ( RuntimeException )
337cdf0e10cSrcweir {
338cdf0e10cSrcweir     dispatchWithNotification( aURL, lArgs, Reference< XDispatchResultListener >() );
339cdf0e10cSrcweir }
340cdf0e10cSrcweir 
341cdf0e10cSrcweir void SAL_CALL ScriptProtocolHandler::addStatusListener(
342cdf0e10cSrcweir const Reference< XStatusListener >& xControl, const URL& aURL )
343cdf0e10cSrcweir throw ( RuntimeException )
344cdf0e10cSrcweir {
345cdf0e10cSrcweir     (void)xControl;
346cdf0e10cSrcweir     (void)aURL;
347cdf0e10cSrcweir 
348cdf0e10cSrcweir     // implement if status is supported
349cdf0e10cSrcweir }
350cdf0e10cSrcweir 
351cdf0e10cSrcweir void SAL_CALL ScriptProtocolHandler::removeStatusListener(
352cdf0e10cSrcweir const Reference< XStatusListener >& xControl, const URL& aURL )
353cdf0e10cSrcweir throw ( RuntimeException )
354cdf0e10cSrcweir {
355cdf0e10cSrcweir     (void)xControl;
356cdf0e10cSrcweir     (void)aURL;
357cdf0e10cSrcweir }
358cdf0e10cSrcweir 
359cdf0e10cSrcweir bool
360cdf0e10cSrcweir ScriptProtocolHandler::getScriptInvocation()
361cdf0e10cSrcweir {
362cdf0e10cSrcweir     if ( !m_xScriptInvocation.is() && m_xFrame.is() )
363cdf0e10cSrcweir     {
364cdf0e10cSrcweir         Reference< XController > xController = m_xFrame->getController();
365cdf0e10cSrcweir         if ( xController .is() )
366cdf0e10cSrcweir         {
367cdf0e10cSrcweir             // try to obtain an XScriptInvocationContext interface, preferred from the
368cdf0e10cSrcweir             // mode, then from the controller
369cdf0e10cSrcweir             if ( !m_xScriptInvocation.set( xController->getModel(), UNO_QUERY ) )
370cdf0e10cSrcweir                 m_xScriptInvocation.set( xController, UNO_QUERY );
371cdf0e10cSrcweir         }
372718aea48STsutomu Uchino         else
373718aea48STsutomu Uchino         {
374718aea48STsutomu Uchino             Reference< XFrame > xFrame( m_xFrame.get(), UNO_QUERY );
375718aea48STsutomu Uchino             if ( xFrame.is() )
376718aea48STsutomu Uchino             {
377718aea48STsutomu Uchino                 SfxFrame* pFrame = NULL;
378718aea48STsutomu Uchino                 for ( pFrame = SfxFrame::GetFirst(); pFrame; pFrame = SfxFrame::GetNext( *pFrame ) )
379718aea48STsutomu Uchino                 {
380718aea48STsutomu Uchino                     if ( pFrame->GetFrameInterface() == xFrame )
381718aea48STsutomu Uchino                         break;
382718aea48STsutomu Uchino                 }
383718aea48STsutomu Uchino                 SfxObjectShell* pDocShell = pFrame ? pFrame->GetCurrentDocument() : SfxObjectShell::Current();
384718aea48STsutomu Uchino                 if ( pDocShell )
385718aea48STsutomu Uchino                 {
386718aea48STsutomu Uchino                     Reference< XModel > xModel( pDocShell->GetModel() );
387718aea48STsutomu Uchino                     m_xScriptInvocation.set( xModel, UNO_QUERY );
388718aea48STsutomu Uchino                 }
389718aea48STsutomu Uchino             }
390718aea48STsutomu Uchino         }
391cdf0e10cSrcweir     }
392cdf0e10cSrcweir     return m_xScriptInvocation.is();
393cdf0e10cSrcweir }
394cdf0e10cSrcweir 
395cdf0e10cSrcweir void ScriptProtocolHandler::createScriptProvider()
396cdf0e10cSrcweir {
397cdf0e10cSrcweir     if ( m_xScriptProvider.is() )
398cdf0e10cSrcweir         return;
399cdf0e10cSrcweir 
400cdf0e10cSrcweir     try
401cdf0e10cSrcweir     {
402cdf0e10cSrcweir         // first, ask the component supporting the XScriptInvocationContext interface
403cdf0e10cSrcweir         // (if there is one) for a script provider
404cdf0e10cSrcweir         if ( getScriptInvocation() )
405cdf0e10cSrcweir         {
406cdf0e10cSrcweir             Reference< XScriptProviderSupplier > xSPS( m_xScriptInvocation, UNO_QUERY );
407cdf0e10cSrcweir             if ( xSPS.is() )
408cdf0e10cSrcweir                 m_xScriptProvider = xSPS->getScriptProvider();
409cdf0e10cSrcweir         }
410cdf0e10cSrcweir 
411cdf0e10cSrcweir         // second, ask the model in our frame
412cdf0e10cSrcweir         if ( !m_xScriptProvider.is() && m_xFrame.is() )
413cdf0e10cSrcweir         {
414cdf0e10cSrcweir             Reference< XController > xController = m_xFrame->getController();
415cdf0e10cSrcweir             if ( xController .is() )
416cdf0e10cSrcweir             {
417cdf0e10cSrcweir                 Reference< XScriptProviderSupplier > xSPS( xController->getModel(), UNO_QUERY );
418cdf0e10cSrcweir                 if ( xSPS.is() )
419cdf0e10cSrcweir                     m_xScriptProvider = xSPS->getScriptProvider();
420cdf0e10cSrcweir             }
421cdf0e10cSrcweir         }
422cdf0e10cSrcweir 
423cdf0e10cSrcweir 
424cdf0e10cSrcweir         // as a fallback, ask the controller
425cdf0e10cSrcweir         if ( !m_xScriptProvider.is() && m_xFrame.is() )
426cdf0e10cSrcweir         {
427cdf0e10cSrcweir             Reference< XScriptProviderSupplier > xSPS( m_xFrame->getController(), UNO_QUERY );
428cdf0e10cSrcweir             if ( xSPS.is() )
429cdf0e10cSrcweir                 m_xScriptProvider = xSPS->getScriptProvider();
430cdf0e10cSrcweir         }
431cdf0e10cSrcweir 
432cdf0e10cSrcweir         // if nothing of this is successful, use the master script provider
433cdf0e10cSrcweir         if ( !m_xScriptProvider.is() )
434cdf0e10cSrcweir         {
435cdf0e10cSrcweir             ::rtl::OUString tmspf = ::rtl::OUString::createFromAscii(
436cdf0e10cSrcweir                 "/singletons/com.sun.star.script.provider.theMasterScriptProviderFactory");
437cdf0e10cSrcweir 
438cdf0e10cSrcweir             Reference< provider::XScriptProviderFactory > xFac(
43902463bafSDamjan Jovanovic                 m_xCtx->getValueByName( tmspf ), UNO_QUERY_THROW );
440cdf0e10cSrcweir 
441cdf0e10cSrcweir             Any aContext;
442cdf0e10cSrcweir             if ( getScriptInvocation() )
443cdf0e10cSrcweir                 aContext = makeAny( m_xScriptInvocation );
444cdf0e10cSrcweir             m_xScriptProvider = Reference< provider::XScriptProvider > (
445cdf0e10cSrcweir                 xFac->createScriptProvider( aContext ), UNO_QUERY_THROW );
446cdf0e10cSrcweir         }
447cdf0e10cSrcweir     }
448cdf0e10cSrcweir     catch ( RuntimeException & e )
449cdf0e10cSrcweir     {
450cdf0e10cSrcweir         ::rtl::OUString temp = OUSTR( "ScriptProtocolHandler::createScriptProvider(),  " );
451cdf0e10cSrcweir         throw RuntimeException( temp.concat( e.Message ), Reference< XInterface >() );
452cdf0e10cSrcweir     }
453cdf0e10cSrcweir     catch ( Exception & e )
454cdf0e10cSrcweir     {
455cdf0e10cSrcweir         ::rtl::OUString temp = OUSTR( "ScriptProtocolHandler::createScriptProvider: " );
456cdf0e10cSrcweir         throw RuntimeException( temp.concat( e.Message ), Reference< XInterface >() );
457cdf0e10cSrcweir     }
458cdf0e10cSrcweir }
459cdf0e10cSrcweir 
460cdf0e10cSrcweir ScriptProtocolHandler::ScriptProtocolHandler(
46102463bafSDamjan Jovanovic Reference< css::uno::XComponentContext > const& xCtx ) :
46202463bafSDamjan Jovanovic m_bInitialised( false ), m_xCtx( xCtx )
463cdf0e10cSrcweir {
464cdf0e10cSrcweir }
465cdf0e10cSrcweir 
466cdf0e10cSrcweir ScriptProtocolHandler::~ScriptProtocolHandler()
467cdf0e10cSrcweir {
468cdf0e10cSrcweir }
469cdf0e10cSrcweir 
470cdf0e10cSrcweir /* XServiceInfo */
471cdf0e10cSrcweir ::rtl::OUString SAL_CALL ScriptProtocolHandler::getImplementationName( )
472cdf0e10cSrcweir throw( RuntimeException )
473cdf0e10cSrcweir {
474cdf0e10cSrcweir     return impl_getStaticImplementationName();
475cdf0e10cSrcweir }
476cdf0e10cSrcweir 
477cdf0e10cSrcweir /* XServiceInfo */
478cdf0e10cSrcweir sal_Bool SAL_CALL ScriptProtocolHandler::supportsService(
479cdf0e10cSrcweir const ::rtl::OUString& sServiceName )
480cdf0e10cSrcweir throw( RuntimeException )
481cdf0e10cSrcweir {
482cdf0e10cSrcweir     Sequence< ::rtl::OUString > seqServiceNames = getSupportedServiceNames();
483cdf0e10cSrcweir     const ::rtl::OUString* pArray = seqServiceNames.getConstArray();
484cdf0e10cSrcweir     for ( sal_Int32 nCounter = 0; nCounter < seqServiceNames.getLength(); nCounter++ )
485cdf0e10cSrcweir     {
486cdf0e10cSrcweir         if ( pArray[ nCounter ] == sServiceName )
487cdf0e10cSrcweir         {
488cdf0e10cSrcweir             return sal_True ;
489cdf0e10cSrcweir         }
490cdf0e10cSrcweir     }
491cdf0e10cSrcweir 
492cdf0e10cSrcweir     return sal_False ;
493cdf0e10cSrcweir }
494cdf0e10cSrcweir 
495cdf0e10cSrcweir /* XServiceInfo */
496cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL ScriptProtocolHandler::getSupportedServiceNames()
497cdf0e10cSrcweir throw( RuntimeException )
498cdf0e10cSrcweir {
499cdf0e10cSrcweir     return impl_getStaticSupportedServiceNames();
500cdf0e10cSrcweir }
501cdf0e10cSrcweir 
502cdf0e10cSrcweir /* Helper for XServiceInfo */
503cdf0e10cSrcweir Sequence< ::rtl::OUString > ScriptProtocolHandler::impl_getStaticSupportedServiceNames()
504cdf0e10cSrcweir {
505cdf0e10cSrcweir     ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
506cdf0e10cSrcweir     Sequence< ::rtl::OUString > seqServiceNames( 1 );
507cdf0e10cSrcweir     seqServiceNames.getArray() [ 0 ] =
508cdf0e10cSrcweir         ::rtl::OUString::createFromAscii( ::scripting_protocolhandler::MYSERVICENAME );
509cdf0e10cSrcweir     return seqServiceNames ;
510cdf0e10cSrcweir }
511cdf0e10cSrcweir 
512cdf0e10cSrcweir /* Helper for XServiceInfo */
513cdf0e10cSrcweir ::rtl::OUString ScriptProtocolHandler::impl_getStaticImplementationName()
514cdf0e10cSrcweir {
515cdf0e10cSrcweir     return ::rtl::OUString::createFromAscii( ::scripting_protocolhandler::MYIMPLNAME );
516cdf0e10cSrcweir }
517cdf0e10cSrcweir 
518cdf0e10cSrcweir /* Helper for registry */
519cdf0e10cSrcweir Reference< XInterface > SAL_CALL ScriptProtocolHandler::impl_createInstance(
52002463bafSDamjan Jovanovic const Reference< css::uno::XComponentContext > & xCtx)
521cdf0e10cSrcweir throw( RuntimeException )
522cdf0e10cSrcweir {
52302463bafSDamjan Jovanovic     return Reference < XInterface >( *new ScriptProtocolHandler( xCtx ) );
524cdf0e10cSrcweir }
525cdf0e10cSrcweir 
52602463bafSDamjan Jovanovic static struct ::cppu::ImplementationEntry g_entries[] =
527cdf0e10cSrcweir {
52802463bafSDamjan Jovanovic     {
529cdf0e10cSrcweir         ScriptProtocolHandler::impl_createInstance,
53002463bafSDamjan Jovanovic         ScriptProtocolHandler::impl_getStaticImplementationName,
53102463bafSDamjan Jovanovic         ScriptProtocolHandler::impl_getStaticSupportedServiceNames,
53202463bafSDamjan Jovanovic         ::cppu::createSingleComponentFactory,
53302463bafSDamjan Jovanovic         &g_moduleCount.modCnt,
53402463bafSDamjan Jovanovic         0
53502463bafSDamjan Jovanovic     },
53602463bafSDamjan Jovanovic     { 0, 0, 0, 0, 0, 0 }
53702463bafSDamjan Jovanovic };
538cdf0e10cSrcweir 
539cdf0e10cSrcweir } // namespace scripting_protocolhandler
540cdf0e10cSrcweir 
541cdf0e10cSrcweir /* exported functions for registration */
542cdf0e10cSrcweir extern "C"
543cdf0e10cSrcweir {
544cdf0e10cSrcweir 
545cdf0e10cSrcweir #undef css
546cdf0e10cSrcweir #define css ::com::sun::star
547cdf0e10cSrcweir 
548cdf0e10cSrcweir     void SAL_CALL component_getImplementationEnvironment(
549cdf0e10cSrcweir         const sal_Char** ppEnvironmentTypeName, uno_Environment** ppEnvironment )
550cdf0e10cSrcweir     {
551cdf0e10cSrcweir         (void)ppEnvironment;
552cdf0e10cSrcweir 
553cdf0e10cSrcweir         *ppEnvironmentTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME ;
554cdf0e10cSrcweir     }
555cdf0e10cSrcweir 
556cdf0e10cSrcweir     void* SAL_CALL component_getFactory( const sal_Char * pImplementationName ,
557cdf0e10cSrcweir                                          void * pServiceManager ,
558cdf0e10cSrcweir                                          void * pRegistryKey )
559cdf0e10cSrcweir     {
56002463bafSDamjan Jovanovic         return component_getFactoryHelper( pImplementationName, pServiceManager, pRegistryKey, scripting_protocolhandler::g_entries );
561cdf0e10cSrcweir     }
562cdf0e10cSrcweir } // extern "C"
563cdf0e10cSrcweir 
564cdf0e10cSrcweir 
565