LibreOffice Module scripting (master) 1
ActiveMSPList.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
21#include <util/MiscUtils.hxx>
22
23#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
24
25#include "ActiveMSPList.hxx"
26
28
29using namespace com::sun::star;
30using namespace com::sun::star::uno;
31using namespace com::sun::star::script;
32using namespace ::sf_misc;
33
35{
36
38{
39 userDirString = "user";
40 shareDirString = "share";
41 bundledDirString = "bundled";
42}
43
45{
46}
47
50{
51 Sequence< Any > args( &context, 1 );
52
54 m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
55 "com.sun.star.script.provider.MasterScriptProvider", args, m_xContext ), UNO_QUERY );
56 return msp;
57}
58
60{
61public:
63 {
64 pList->createNonDocMSPs();
65 }
66};
67
68namespace
69{
70 //thread-safe method to ensure createNonDocMSPs is called once
71 void ensureNonDocMSPs(ActiveMSPList *pList)
72 {
73 static NonDocMSPCreator theCreator(pList);
74 }
75}
76
79{
81 OUString sContext;
82 if ( aContext >>= sContext )
83 {
84 msp = getMSPFromStringContext( sContext );
85 return msp;
86 }
87
88 Reference< frame::XModel > xModel( aContext, UNO_QUERY );
89
90 Reference< document::XScriptInvocationContext > xScriptContext( aContext, UNO_QUERY );
91 if ( xScriptContext.is() )
92 {
93 try
94 {
95 // the component supports executing scripts embedded in a - possibly foreign document.
96 // Check whether this other document it's the component itself.
97 if ( !xModel.is() || ( xModel != xScriptContext->getScriptContainer() ) )
98 {
99 msp = getMSPFromInvocationContext( xScriptContext );
100 return msp;
101 }
102 }
103 catch( const lang::IllegalArgumentException& )
104 {
106 }
107 }
108
109 if ( xModel.is() )
110 {
111 sContext = MiscUtils::xModelToTdocUrl( xModel, m_xContext );
112 msp = getMSPFromStringContext( sContext );
113 return msp;
114 }
115
116 ensureNonDocMSPs(this);
117 return m_hMsps[ shareDirString ];
118}
119
122{
124
126 if ( xContext.is() )
127 xScripts.set( xContext->getScriptContainer() );
128 if ( !xScripts.is() )
129 {
130 throw lang::IllegalArgumentException(
131 "Failed to create MasterScriptProvider for ScriptInvocationContext: "
132 "Component supporting XEmbeddScripts interface not found.",
133 nullptr, 1 );
134 }
135
136 ::osl::MutexGuard guard( m_mutex );
137
138 Reference< XInterface > xNormalized( xContext, UNO_QUERY );
139 ScriptComponent_map::const_iterator pos = m_mScriptComponents.find( xNormalized );
140 if ( pos == m_mScriptComponents.end() )
141 {
142 // TODO
143 msp = createNewMSP( uno::Any( xContext ) );
144 addActiveMSP( xNormalized, msp );
145 }
146 else
147 {
148 msp = pos->second;
149 }
150
151 return msp;
152}
153
155 ActiveMSPList::getMSPFromStringContext( const OUString& context )
156{
158 try
159 {
160 if ( context.startsWith( "vnd.sun.star.tdoc" ) )
161 {
162 Reference< frame::XModel > xModel( MiscUtils::tDocUrlToModel( context ) );
163
164 Reference< document::XEmbeddedScripts > xScripts( xModel, UNO_QUERY );
165 Reference< document::XScriptInvocationContext > xScriptsContext( xModel, UNO_QUERY );
166 if ( !xScripts.is() && !xScriptsContext.is() )
167 {
168 throw lang::IllegalArgumentException(
169 "Failed to create MasterScriptProvider for '"
170 + context +
171 "': Either XEmbeddScripts or XScriptInvocationContext need to be supported by the document.",
172 nullptr, 1 );
173 }
174
175 ::osl::MutexGuard guard( m_mutex );
176 Reference< XInterface > xNormalized( xModel, UNO_QUERY );
177 ScriptComponent_map::const_iterator pos = m_mScriptComponents.find( xNormalized );
178 if ( pos == m_mScriptComponents.end() )
179 {
180 msp = createNewMSP( context );
181 addActiveMSP( xNormalized, msp );
182 }
183 else
184 {
185 msp = pos->second;
186 }
187 }
188 else
189 {
190 ::osl::MutexGuard guard( m_mutex );
191 Msp_hash::iterator h_itEnd = m_hMsps.end();
192 Msp_hash::const_iterator itr = m_hMsps.find( context );
193 if ( itr == h_itEnd )
194 {
195 msp = createNewMSP( context );
196 m_hMsps[ context ] = msp;
197 }
198 else
199 {
200 msp = m_hMsps[ context ];
201 }
202 }
203 }
204 catch( const lang::IllegalArgumentException& )
205 {
206 // allowed to leave
207 }
208 catch( const RuntimeException& )
209 {
210 // allowed to leave
211 }
212 catch( const Exception& )
213 {
214 css::uno::Any anyEx = cppu::getCaughtException();
215 throw lang::WrappedTargetRuntimeException(
216 "Failed to create MasterScriptProvider for context '"
217 + context + "'.",
218 *this, anyEx );
219 }
220 return msp;
221}
222
223void
226{
227 ::osl::MutexGuard guard( m_mutex );
228 Reference< XInterface > xNormalized( xComponent, UNO_QUERY );
229 ScriptComponent_map::const_iterator pos = m_mScriptComponents.find( xNormalized );
230 if ( pos != m_mScriptComponents.end() )
231 return;
232
233 m_mScriptComponents[ xNormalized ] = msp;
234
235 // add self as listener for component disposal
236 // should probably throw from this method!!, reexamine
237 try
238 {
239 Reference< lang::XComponent > xBroadcaster( xComponent, UNO_QUERY_THROW );
240 xBroadcaster->addEventListener( this );
241 }
242 catch ( const Exception& )
243 {
244 DBG_UNHANDLED_EXCEPTION("scripting");
245 }
246}
247
248
249void SAL_CALL ActiveMSPList::disposing( const css::lang::EventObject& Source )
250
251{
252 try
253 {
254 Reference< XInterface > xNormalized( Source.Source, UNO_QUERY );
255 if ( xNormalized.is() )
256 {
257 ::osl::MutexGuard guard( m_mutex );
258 ScriptComponent_map::iterator pos = m_mScriptComponents.find( xNormalized );
259 if ( pos != m_mScriptComponents.end() )
260 m_mScriptComponents.erase( pos );
261 }
262 }
263 catch ( const Exception& )
264 {
265 // if we get an exception here, there is not much we can do about
266 // it can't throw as it will screw up the model that is calling dispose
267 DBG_UNHANDLED_EXCEPTION("scripting");
268 }
269}
270
271void
273{
274 // do creation of user and share MSPs here
275 OUString serviceName("com.sun.star.script.provider.MasterScriptProvider");
276
278 Reference< provider::XScriptProvider > userMsp( m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext( serviceName, args, m_xContext ), UNO_QUERY );
279 // should check if provider reference is valid
280 m_hMsps[ userDirString ] = userMsp;
281
282 args = { Any(shareDirString) };
283 Reference< provider::XScriptProvider > shareMsp( m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext( serviceName, args, m_xContext ), UNO_QUERY );
284 // should check if provider reference is valid
285 m_hMsps[ shareDirString ] = shareMsp;
286
288 Reference< provider::XScriptProvider > bundledMsp( m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext( serviceName, args, m_xContext ), UNO_QUERY );
289 // should check if provider reference is valid
290 m_hMsps[ bundledDirString ] = bundledMsp;
291}
292
293} // namespace func_provider
294
295/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XComponentContext > m_xContext
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
virtual ~ActiveMSPList() override
css::uno::Reference< css::script::provider::XScriptProvider > getMSPFromStringContext(const OUString &context)
ActiveMSPList(const css::uno::Reference< css::uno::XComponentContext > &xContext)
css::uno::Reference< css::script::provider::XScriptProvider > getMSPFromAnyContext(const css::uno::Any &context)
css::uno::Reference< css::script::provider::XScriptProvider > getMSPFromInvocationContext(const css::uno::Reference< css::document::XScriptInvocationContext > &context)
css::uno::Reference< css::script::provider::XScriptProvider > createNewMSP(const css::uno::Any &context)
css::uno::Reference< css::uno::XComponentContext > m_xContext
ScriptComponent_map m_mScriptComponents
void addActiveMSP(const css::uno::Reference< css::uno::XInterface > &xComponent, const css::uno::Reference< css::script::provider::XScriptProvider > &msp)
NonDocMSPCreator(ActiveMSPList *pList)
#define DBG_UNHANDLED_EXCEPTION(...)
@ Exception
Any SAL_CALL getCaughtException()
args
Reference< XModel > xModel
size_t pos