1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 // MARKER(update_precomp.py): autogen include statement, do not remove
30 #include "precompiled_scripting.hxx"
34 #include <cppuhelper/implementationentry.hxx>
35 #include <sal/config.h>
36 #include <rtl/uri.hxx>
38 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
39 #include <com/sun/star/util/XMacroExpander.hpp>
40 #include <com/sun/star/lang/XComponent.hpp>
41 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
42 #include <com/sun/star/beans/XPropertySet.hpp>
43 #include <com/sun/star/frame/XModel.hpp>
44 #include <drafts/com/sun/star/script/framework/storage/XScriptInfoAccess.hpp>
46 #include "ScriptStorageManager.hxx"
47 #include <util/util.hxx>
48 #include <util/scriptingconstants.hxx>
50 using namespace ::rtl
;
51 using namespace ::com::sun::star
;
52 using namespace ::com::sun::star::uno
;
53 using namespace ::drafts::com::sun::star::script::framework
;
55 namespace scripting_impl
58 static OUString s_implName
=
59 ::rtl::OUString::createFromAscii(
60 "drafts.com.sun.star.script.framework.storage.ScriptStorageManager" );
61 static OUString s_serviceName
=
62 ::rtl::OUString::createFromAscii(
63 "drafts.com.sun.star.script.framework.storage.ScriptStorageManager" );
64 static Sequence
< OUString
> s_serviceNames
= Sequence
< OUString
>( &s_serviceName
, 1 );
66 //extern ::rtl_StandardModuleCount s_moduleCount = MODULE_COUNT_INIT;
67 //extern ::rtl_StandardModuleCount s_moduleCount;
70 //*************************************************************************
71 // ScriptStorageManager Constructor
72 ScriptStorageManager::ScriptStorageManager( const Reference
<
73 XComponentContext
> & xContext
) SAL_THROW ( ( RuntimeException
) )
74 : m_xContext( xContext
), m_count( 0 ), m_securityMgr( xContext
)
76 OSL_TRACE( "< ScriptStorageManager ctor called >\n" );
77 //s_moduleCount.modCnt.acquire( &s_moduleCount.modCnt );
79 validateXRef( m_xContext
,
80 "ScriptStorageManager::ScriptStorageManager : cannot get component context" );
82 m_xMgr
= m_xContext
->getServiceManager();
84 "ScriptStorageManager::ScriptStorageManager : cannot get service manager" );
88 // obtain the macro expander singleton to use in determining the
89 // location of the application script storage
90 Any aAny
= m_xContext
->getValueByName( OUString::createFromAscii(
91 "/singletons/com.sun.star.util.theMacroExpander" ) );
92 Reference
< util::XMacroExpander
> xME
;
93 if ( sal_False
== ( aAny
>>= xME
) )
95 throw RuntimeException(
96 OUSTR( "ScriptStorageManager::ScriptStorageManager: can't get XMacroExpander" ),
97 Reference
< XInterface
>() );
99 validateXRef( xME
, "ScriptStorageManager constructor: can't get MacroExpander" );
101 OUString base
= OUString::createFromAscii(
102 SAL_CONFIGFILE( "${$BRAND_BASE_DIR/program/bootstrap" ) );
104 setupAppStorage( xME
,
105 base
.concat( OUString::createFromAscii( "::BaseInstallation}/share" ) ),
107 setupAppStorage( xME
,
108 base
.concat( OUString::createFromAscii( "::UserInstallation}/user" ) ),
112 catch ( Exception
& e
)
114 throw RuntimeException( OUSTR( "ScriptStorageManager::ScriptStorageManager: " ).concat( e
.Message
), Reference
< XInterface
>() );
116 OSL_ASSERT( m_count
== 2 );
119 //*************************************************************************
120 // ScriptStorageManager setupAppStorage
122 ScriptStorageManager::setupAppStorage(
123 const Reference
< util::XMacroExpander
> & xME
,
124 const OUString
& storageStr
,
125 const OUString
& appStr
)
126 SAL_THROW ( ( RuntimeException
) )
130 Reference
< XInterface
> xInterface
=
131 m_xMgr
->createInstanceWithContext(
132 OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ), m_xContext
);
133 validateXRef( xInterface
,
134 "ScriptStorageManager constructor: can't get SimpleFileAccess XInterface" );
135 Reference
< ucb::XSimpleFileAccess
> xSFA( xInterface
, UNO_QUERY_THROW
);
137 setupAnyStorage( xSFA
, xME
->expandMacros( storageStr
), appStr
);
139 catch ( Exception
& e
)
141 throw RuntimeException(
142 OUSTR( "ScriptStorageManager::setupAppStorage: " ).concat( e
.Message
),
143 Reference
< XInterface
>() );
147 //*************************************************************************
148 // ScriptStorageManager setupAnyStorage
150 ScriptStorageManager::setupAnyStorage(
151 const Reference
< ucb::XSimpleFileAccess
> & xSFA
,
152 const OUString
& storageStr
,
153 const OUString
& origStringURI
)
154 SAL_THROW ( ( RuntimeException
) )
156 // Required for scope of fnc to protect all access read and writes to m_count
157 ::osl::Guard
< ::osl::Mutex
> aGuard( m_mutex
);
161 // create a ScriptingStorage using the SimpleFileAccess, the storageID
162 // (from the count), and the URL to the application's shared area
163 Sequence
< Any
> aArgs( 3 );
165 aArgs
[ 1 ] <<= m_count
;
166 aArgs
[ 2 ] <<= storageStr
;
168 OSL_TRACE( "creating storage for: %s\n",
169 ::rtl::OUStringToOString( storageStr
,
170 RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
172 Reference
< XInterface
> xInterface
=
173 m_xMgr
->createInstanceWithArgumentsAndContext(
174 OUString::createFromAscii(
175 "drafts.com.sun.star.script.framework.storage.ScriptStorage" ),
178 validateXRef( xInterface
, "ScriptStorageManager:: setupAnyStorage: Can't create ScriptStorage for share" );
180 // and place it in the hash_map. Increment the counter
181 m_ScriptStorageMap
[ m_count
++ ] = xInterface
;
182 sal_Int32 sid
= m_count
- 1;
184 // create hash of original uri i.e. file:///home/users/docs/mydoc.sxw
185 // and storage id (sid) this will allow us to retrieve
186 // the sid based on the url of the document.
187 m_StorageIdOrigURIHash
[ origStringURI
] = sid
;
188 OSL_TRACE( "\tcreated with ID=%d\n", m_count
- 1 );
191 catch ( Exception
& e
)
193 throw RuntimeException(
194 OUSTR( "ScriptStorageManager::setupAnyStorage: " ).concat(
195 e
.Message
), Reference
< XInterface
>() );
201 //*************************************************************************
202 // ScriptStorageManager Destructor
203 ScriptStorageManager::~ScriptStorageManager()
206 OSL_TRACE( "< ScriptStorageManager dtor called >\n" );
207 // s_moduleCount.modCnt.release( &s_moduleCount.modCnt );
210 //*************************************************************************
211 // This method assumes that the XSimpleFileAccess knows it's on root URL
212 // and can be used with relative URLs
214 ScriptStorageManager::createScriptStorage(
215 const Reference
< ucb::XSimpleFileAccess
>& xSFA
)
216 throw ( RuntimeException
)
218 OSL_TRACE( "** ==> ScriptStorageManager in createScriptingStorage\n" );
220 "ScriptStorageManager::createScriptStorage: XSimpleFileAccess is not valid" );
222 return setupAnyStorage( xSFA
, ::rtl::OUString::createFromAscii( "" ),
223 ::rtl::OUString::createFromAscii( "" ) );
226 //*************************************************************************
228 ScriptStorageManager::createScriptStorageWithURI(
229 const Reference
< ucb::XSimpleFileAccess
>& xSFA
, const OUString
& cStringURI
)
230 throw ( RuntimeException
)
232 OSL_TRACE( "** ==> ScriptStorageManager in createScriptingStorageWithURI\n" );
233 validateXRef( xSFA
, "ScriptStorageManager::createScriptStorage: XSimpleFileAccess is not valid" );
235 // related to issue 11866
236 // warning dialog gets launched when adding binding to script in doc
237 // workaround issue: no functionProvider created on doc open
238 // if NODIALOG tag, strip from stringURI, set boolean=true
239 bool displayDialog
= true;
240 ::rtl::OUString dialogTag
= ::rtl::OUString::createFromAscii( "NoDialog::" );
241 ::rtl::OUString stringURI
= cStringURI
;
242 if( stringURI
.indexOf( dialogTag
) == 0 )
244 OSL_TRACE( "ScriptStorageManager::createScriptStorage: will not display security dialogs" );
245 stringURI
= stringURI
.copy( dialogTag
.getLength() );
246 displayDialog
= false;
248 sal_Int32 returnedID
= getScriptStorageID(stringURI
);
251 // convert file:///... url to vnd... syntax
252 ::rtl::OUString
canonicalURI(
253 ::rtl::OUString::createFromAscii( "vnd.sun.star.pkg://" ) );
254 canonicalURI
= canonicalURI
.concat( ::rtl::Uri::encode( stringURI
,
255 rtl_UriCharClassUricNoSlash
, rtl_UriEncodeCheckEscapes
,
256 RTL_TEXTENCODING_ASCII_US
) );
258 if (returnedID
== -1)
260 OSL_TRACE("Creating new storage for %s",
261 ::rtl::OUStringToOString( stringURI
,
262 RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
263 returnedID
= setupAnyStorage( xSFA
, canonicalURI
, stringURI
);
267 OSL_TRACE("Using existing storage for %s",
268 ::rtl::OUStringToOString( stringURI
,
269 RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
272 // np - removed previous scripting framework security handling
273 // now handled by modification to existing calls in sfx for basic
275 /* if( displayDialog )
279 OSL_TRACE("Adding to security mgr for %s",
280 ::rtl::OUStringToOString( stringURI,
281 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
282 m_securityMgr.addScriptStorage( stringURI, returnedID );
284 catch ( RuntimeException & rte )
286 throw RuntimeException(
287 OUSTR( "ScriptStorageManager::createScriptStorageWithURI: " ).concat(
288 rte.Message ), Reference< XInterface >() );
293 OSL_TRACE("No need to security mgr for %s",
294 ::rtl::OUStringToOString( stringURI,
295 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
300 //*************************************************************************
301 Reference
< XInterface
> SAL_CALL
302 ScriptStorageManager::getScriptStorage( sal_Int32 scriptStorageID
)
303 throw( RuntimeException
)
305 OSL_TRACE( "** ==> ScriptStorageManager in getStorageInstance\n" );
306 OSL_TRACE( "** ==> request for id=%d",scriptStorageID
);
308 ScriptStorage_map::const_iterator itr
=
309 m_ScriptStorageMap
.find( scriptStorageID
);
311 if ( itr
== m_ScriptStorageMap
.end() )
313 throw RuntimeException(
314 OUSTR( "ScriptStorageManager::getScriptStorage: invalid storage ID" ),
315 Reference
< XInterface
>() );
317 validateXRef( itr
->second
,
318 "ScriptStorageManager::getScriptStorage: Cannot get ScriptStorage from ScriptStorageHash" );
322 //*******************************************************************
324 ScriptStorageManager::getScriptStorageID( const ::rtl::OUString
& origURI
)
325 throw (::com::sun::star::uno::RuntimeException
)
327 StorageId_hash::const_iterator it
= m_StorageIdOrigURIHash
.find( origURI
);
329 if ( it
== m_StorageIdOrigURIHash
.end() )
331 OUString message
= OUSTR( "ScriptStorageManager::getScriptStorageID(): Cannot find storage for " );
332 if ( origURI
.getLength() == 0 )
334 message
= message
.concat( OUSTR("Empty URI") );
338 message
= message
.concat( origURI
);
340 OSL_TRACE( ::rtl::OUStringToOString( message
,
341 RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
348 //*******************************************************************
350 ScriptStorageManager::removeScriptDocURIHashEntry( const OUString
& origURI
)
352 StorageId_hash::iterator it
= m_StorageIdOrigURIHash
.find( origURI
);
353 if ( it
== m_StorageIdOrigURIHash
.end() )
355 OSL_TRACE( "ScriptStorageManager::removeScriptDocURIHashEntry: no entry to remove" );
359 // remove entry for this doc url from orig uri map.
360 m_StorageIdOrigURIHash
.erase( it
);
363 //*******************************************************************
365 ScriptStorageManager::refreshScriptStorage( const OUString
& stringURI
)
366 throw( RuntimeException
)
368 OSL_TRACE( "** => ScriptStorageManager in refreshScriptStorage\n" );
369 OSL_TRACE( "** => refreshing URI: %s\n",
370 ::rtl::OUStringToOString(
371 stringURI
, RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
373 sal_Int32 storageID
= getScriptStorageID( stringURI
);
375 if ( storageID
== -1 )
377 OSL_TRACE( "** id was -1, no storage");
378 // Refreshing noexistent storage - just return
384 Reference
< storage::XScriptStorageRefresh
> xSSR(
385 getScriptStorage( storageID
), UNO_QUERY
);
389 catch ( RuntimeException
& e
)
391 throw RuntimeException(
392 OUSTR( "ScriptStorageManager::refreshScriptStorage: " ).concat(
393 e
.Message
), Reference
< XInterface
>() );
395 catch ( Exception
& e
)
397 throw RuntimeException(
398 OUSTR( "ScriptStorageManager::refreshScriptStorage: " ).concat(
399 e
.Message
), Reference
< XInterface
>() );
403 //*************************************************************************
405 ScriptStorageManager::checkPermission( const OUString
&
406 scriptStorageURI
, const OUString
& permissionRequest
)
407 throw ( RuntimeException
, lang::IllegalArgumentException
, css::security::AccessControlException
)
411 m_securityMgr
.checkPermission( scriptStorageURI
, permissionRequest
);
413 catch ( css::security::AccessControlException
& e
)
415 throw css::security::AccessControlException(
416 OUSTR( "ScriptStorageManager::checkPermission: AccessControlException: " ).concat(
417 e
.Message
), Reference
< XInterface
>(), e
.LackingPermission
);
419 catch ( lang::IllegalArgumentException
& e
)
421 throw lang::IllegalArgumentException(
422 OUSTR( "ScriptStorageManager::checkPermission: IllegalArgumentException: " ).concat(
423 e
.Message
), Reference
< XInterface
>(), e
.ArgumentPosition
);
425 catch ( RuntimeException
& e
)
427 throw RuntimeException(
428 OUSTR( "ScriptStorageManager::checkPermission: RuntimeException: " ).concat(
429 e
.Message
), Reference
< XInterface
>() );
433 //*************************************************************************
435 ScriptStorageManager::getImplementationName( )
436 throw( RuntimeException
)
441 //*************************************************************************
443 ScriptStorageManager::supportsService( const OUString
& serviceName
)
444 throw( RuntimeException
)
446 OUString
const * pNames
= s_serviceNames
.getConstArray();
447 for ( sal_Int32 nPos
= s_serviceNames
.getLength(); nPos
--; )
449 if ( serviceName
.equals( pNames
[ nPos
] ) )
457 //*************************************************************************
458 Sequence
< OUString
> SAL_CALL
459 ScriptStorageManager::getSupportedServiceNames( )
460 throw( RuntimeException
)
462 return s_serviceNames
;
465 //*************************************************************************
467 ScriptStorageManager::disposing( const ::com::sun::star::lang::EventObject
& Source
)
468 throw ( ::com::sun::star::uno::RuntimeException
)
470 OSL_TRACE( "ScriptStorageManager::disposing started" );
471 OSL_TRACE( "event object type=%s",
472 ::rtl::OUStringToOString( getCppuType( &Source
).getTypeName(),
473 RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
475 bool removeSecurityPermission
= true;
478 Reference
< XInterface
> xInterface
= Source
.Source
;
479 // no UNO_QUERY_THROW since we want a 2nd change to query if it's
480 // not a document being disposed
481 Reference
< frame::XModel
> xModel
= Reference
< frame::XModel
> ( xInterface
, UNO_QUERY
);
484 storageURI
= xModel
->getURL();
488 // UNO_QURY_THROW here since it's supposed to be either a doc
489 // or a XScriptInfo (in the case of a filesys script)
490 Reference
< storage::XScriptInfo
> xScriptInfo
= Reference
< storage::XScriptInfo
> ( xInterface
, UNO_QUERY_THROW
);
491 storageURI
= xScriptInfo
->getParcelURI().concat( xScriptInfo
->getFunctionName() );
492 // to save the user seeing the security dialogs every time they
493 // run the script we hang on to the permission for a given script
494 // for the lifetime of the Office
495 removeSecurityPermission
= false;
496 // possibly need to encode it??
498 if ( storageURI
.getLength() > 0 )
500 OSL_TRACE( "URI disposing is ... %s",
501 ::rtl::OUStringToOString( storageURI
,
502 RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
505 catch ( RuntimeException
& e
)
509 "ScriptStorageManager::disposing: can't get script context, reason = " );
510 message
= message
.concat( e
.Message
);
511 OSL_TRACE( ::rtl::OUStringToOString( message
,
512 RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
518 sal_Int32 scriptStorageID
= getScriptStorageID( storageURI
);
520 // no need to do anything if there's no doc storage
521 if( scriptStorageID
== -1 )
526 OSL_TRACE( "disposing storageID = %d", scriptStorageID
);
528 // attempt to get the storage from the hash to ensure that we have a
530 ScriptStorage_map::const_iterator itr
=
531 m_ScriptStorageMap
.find( scriptStorageID
);
533 if ( itr
== m_ScriptStorageMap
.end() )
535 OSL_TRACE( "Entry for storage id %d doesn't exist in map", scriptStorageID
);
539 // erase the entry from the hash
540 m_ScriptStorageMap
.erase( scriptStorageID
);
541 removeScriptDocURIHashEntry( storageURI
);
542 if ( removeSecurityPermission
)
544 m_securityMgr
.removePermissionSettings ( storageURI
);
549 namespace scripting_runtimemgr
551 //*************************************************************************
552 Reference
< XInterface
> SAL_CALL
554 const Reference
< XComponentContext
> & xCompC
)
556 return ( cppu::OWeakObject
* ) new ::scripting_impl::ScriptStorageManager( xCompC
);
559 //*************************************************************************
561 ssm_getSupportedServiceNames( )
564 return ::scripting_impl::s_serviceNames
;
567 //*************************************************************************
569 ssm_getImplementationName( )
572 return ::scripting_impl::s_implName
;
576 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */