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