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