25#include <com/sun/star/embed/EmbedStates.hpp>
26#include <com/sun/star/embed/EmbedVerbs.hpp>
27#include <com/sun/star/embed/UnreachableStateException.hpp>
28#include <com/sun/star/embed/XStorage.hpp>
29#include <com/sun/star/embed/ElementModes.hpp>
30#include <com/sun/star/embed/EmbedUpdateModes.hpp>
31#include <com/sun/star/embed/NeedsRunningStateException.hpp>
32#include <com/sun/star/embed/StateChangeInProgressException.hpp>
33#include <com/sun/star/embed/EmbedMisc.hpp>
34#include <com/sun/star/embed/XEmbedObjectCreator.hpp>
35#include <com/sun/star/io/TempFile.hpp>
36#include <com/sun/star/io/XSeekable.hpp>
37#include <com/sun/star/lang/DisposedException.hpp>
38#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
39#include <com/sun/star/beans/NamedValue.hpp>
40#include <com/sun/star/beans/XPropertySet.hpp>
41#include <com/sun/star/frame/XLoadable.hpp>
42#include <com/sun/star/document/XStorageBasedDocument.hpp>
43#include <com/sun/star/ucb/SimpleFileAccess.hpp>
44#include <com/sun/star/container/XNameAccess.hpp>
45#include <com/sun/star/container/XNameContainer.hpp>
46#include <com/sun/star/system/SystemShellExecute.hpp>
47#include <com/sun/star/system/SystemShellExecuteFlags.hpp>
69void OleEmbeddedObject::SwitchComponentToRunningState_Impl()
73 throw embed::UnreachableStateException();
79 catch(
const embed::UnreachableStateException& )
84 catch(
const embed::WrongStateException& )
92uno::Sequence< sal_Int32 > OleEmbeddedObject::GetReachableStatesList_Impl(
93 const uno::Sequence< embed::VerbDescriptor >& aVerbList )
95 uno::Sequence< sal_Int32 > aStates { embed::EmbedStates::LOADED, embed::EmbedStates::RUNNING };
96 for ( embed::VerbDescriptor
const & vd : aVerbList )
97 if ( vd.VerbID == embed::EmbedVerbs::MS_OLEVERB_OPEN )
100 aStates.getArray()[2] = embed::EmbedStates::ACTIVE;
108uno::Sequence< sal_Int32 > OleEmbeddedObject::GetIntermediateVerbsSequence_Impl( sal_Int32 nNewState )
110 SAL_WARN_IF(
m_nObjectState == embed::EmbedStates::LOADED,
"embeddedobj.ole",
"Loaded object is switched to running state without verbs using!" );
113 if (
m_nObjectState == embed::EmbedStates::RUNNING && nNewState == embed::EmbedStates::ACTIVE )
115 return { embed::EmbedVerbs::MS_OLEVERB_OPEN };
118 return uno::Sequence< sal_Int32 >();
131 if ( pStateChangeContainer !=
nullptr )
140 m_xWrappedObject->addStateChangeListener(
static_cast<embed::XStateChangeListener*
>(pIterator.
next()) );
142 catch(
const uno::RuntimeException& )
155 if ( pEventContainer !=
nullptr )
166 catch(
const uno::RuntimeException& )
179 if ( pCloseContainer !=
nullptr )
190 catch(
const uno::RuntimeException& )
205 uno::Reference< embed::XStorage > xResult;
207 for ( sal_Int32 nInd = 0; nInd < 32000 && !xResult.is(); nInd++ )
213 o_aStorageName =
aName;
219 o_aStorageName.clear();
220 throw uno::RuntimeException(
"Failed to create temporary storage for OLE embed object");
230 for ( sal_Int32 nInd = 0; nInd < 32000 && aResult.isEmpty(); nInd++ )
240 if ( aResult.isEmpty() )
241 throw uno::RuntimeException(
"Failed to rename temporary storage for OLE embed object");
249 bool bResult =
false;
251 OUString aStorageName;
252 OUString aTmpStreamName;
263 uno::Reference< io::XSeekable > xSeekable(
xStream, uno::UNO_QUERY_THROW );
264 xSeekable->seek( 0 );
271 uno::Reference< container::XNameAccess > xFilterFactory(
272 m_xContext->getServiceManager()->createInstanceWithContext(
"com.sun.star.document.FilterFactory",
m_xContext),
273 uno::UNO_QUERY_THROW );
275 OUString aDocServiceName;
277 uno::Sequence< beans::PropertyValue > aFilterData;
278 if ( aFilterAnyData >>= aFilterData )
280 for ( beans::PropertyValue
const & prop : std::as_const(aFilterData) )
281 if ( prop.Name ==
"DocumentService" )
282 prop.
Value >>= aDocServiceName;
285 if ( !aDocServiceName.isEmpty() )
289 beans::NamedValue(
"EmbeddedObject",
uno::Any(
true ))) };
291 uno::Reference< util::XCloseable > xDocument(
m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext( aDocServiceName,
aArguments,
m_xContext ), uno::UNO_QUERY_THROW );
292 uno::Reference< frame::XLoadable > xLoadable( xDocument, uno::UNO_QUERY_THROW );
293 uno::Reference< document::XStorageBasedDocument > xStorDoc( xDocument, uno::UNO_QUERY_THROW );
296 uno::Reference< frame::XModel >
xModel( xDocument, uno::UNO_QUERY_THROW );
298 "SetEmbedded",
true) };
302 uno::Sequence< beans::PropertyValue > aArgs{
310 xSeekable->seek( 0 );
311 xLoadable->load( aArgs );
315 xStorDoc->storeToStorage( xTmpStorage, uno::Sequence< beans::PropertyValue >() );
316 xDocument->close(
true );
317 uno::Reference< beans::XPropertySet > xStorProps( xTmpStorage, uno::UNO_QUERY_THROW );
319 xStorProps->getPropertyValue(
"MediaType") >>= aMediaType;
320 xTmpStorage->dispose();
324 OUString aEmbedFactory;
325 if ( !aMediaType.isEmpty() )
328 if ( aEmbedFactory.isEmpty() )
329 throw uno::RuntimeException(
"Failed to get OLE embedded object factory");
331 uno::Reference< uno::XInterface > xFact =
m_xContext->getServiceManager()->createInstanceWithContext( aEmbedFactory,
m_xContext );
333 uno::Reference< embed::XEmbedObjectCreator > xEmbCreator( xFact, uno::UNO_QUERY_THROW );
337 uno::Reference< lang::XComponent > xComp(
m_xObjectStream, uno::UNO_QUERY_THROW );
359 catch(
const uno::Exception& )
366 catch(
const uno::Exception& )
373 if ( !aTmpStreamName.isEmpty() && aTmpStreamName !=
m_aEntryName )
380 catch (
const uno::Exception&
ex )
385 }
catch(
const uno::Exception& ) {}
388 throw css::lang::WrappedTargetRuntimeException(
ex.Message,
398 catch(
const uno::Exception&
ex )
403 }
catch(
const uno::Exception& ) {}
405 throw css::lang::WrappedTargetRuntimeException(
ex.Message,
412 if ( !aStorageName.isEmpty() )
415 }
catch(
const uno::Exception& ) {
SAL_WARN(
"embeddedobj.ole",
"Can not remove temporary storage!" ); }
428 uno::Reference< container::XChild > xChild(
m_xWrappedObject, uno::UNO_QUERY );
443 if ( xWrappedObject.is() )
446 xWrappedObject->changeState( nNewState );
451 ::osl::ResettableMutexGuard aGuard(
m_aMutex );
454 throw lang::DisposedException();
457 throw embed::WrongStateException(
"The object has no persistence!",
470 throw embed::StateChangeInProgressException( OUString(),
471 uno::Reference< uno::XInterface >(),
482 StateChangeNotification_Impl(
true, nOldState, nNewState );
487 if ( nNewState == embed::EmbedStates::LOADED )
507 else if ( nNewState == embed::EmbedStates::RUNNING || nNewState == embed::EmbedStates::ACTIVE )
518 SwitchComponentToRunningState_Impl();
531 catch(
const uno::Exception& ) {}
541 if (
m_nObjectState == embed::EmbedStates::RUNNING && nNewState == embed::EmbedStates::ACTIVE )
556 catch( uno::Exception& ) {}
562 else if (
m_nObjectState == embed::EmbedStates::ACTIVE && nNewState == embed::EmbedStates::RUNNING )
572 throw embed::UnreachableStateException();
576 throw embed::UnreachableStateException();
578 catch( uno::Exception& )
588 throw embed::UnreachableStateException();
597 if ( xWrappedObject.is() )
600 return xWrappedObject->getReachableStates();
604 ::osl::MutexGuard aGuard(
m_aMutex );
606 throw lang::DisposedException();
609 throw embed::WrongStateException(
"The object has no persistence!",
618 throw embed::NeedsRunningStateException();
628 return uno::Sequence< sal_Int32 >();
637 if ( xWrappedObject.is() )
640 return xWrappedObject->getCurrentState();
644 ::osl::MutexGuard aGuard(
m_aMutex );
646 throw lang::DisposedException();
649 throw embed::WrongStateException(
"The object has no persistence!",
658 bool lcl_CopyStream(
const uno::Reference<io::XInputStream>& xIn,
const uno::Reference<io::XOutputStream>& xOut, sal_Int32 nMaxCopy = SAL_MAX_INT32)
663 const sal_Int32 nChunkSize = 4096;
664 uno::Sequence< sal_Int8 >
aData(nChunkSize);
665 sal_Int32 nTotalRead = 0;
669 if (nTotalRead +
aData.getLength() > nMaxCopy)
671 aData.realloc(nMaxCopy - nTotalRead);
673 nRead = xIn->readBytes(aData,
aData.getLength());
675 xOut->writeBytes(aData);
676 }
while (nRead == nChunkSize && nTotalRead <= nMaxCopy);
677 return nTotalRead != 0;
680 uno::Reference < io::XStream > lcl_GetExtractedStream( OUString& rUrl,
681 const css::uno::Reference< css::uno::XComponentContext >& xContext,
682 const css::uno::Reference< css::io::XStream >& xObjectStream )
684 uno::Reference <io::XTempFile> xNativeTempFile(
685 io::TempFile::create(xContext),
687 uno::Reference < io::XStream >
xStream(xNativeTempFile);
689 uno::Sequence< uno::Any > aArgs{
uno::Any(xObjectStream),
691 uno::Reference< container::XNameContainer > xNameContainer(
692 xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
693 "com.sun.star.embed.OLESimpleStorage",
694 aArgs, xContext ), uno::UNO_QUERY_THROW );
698 static const std::u16string_view aStreamNames[] =
705 u"PowerPoint Document"
708 bool bCopied =
false;
709 for (
size_t i = 0;
i < std::size(aStreamNames) && !bCopied; ++
i)
711 uno::Reference<io::XStream> xEmbeddedFile;
714 xNameContainer->getByName(OUString(aStreamNames[i])) >>= xEmbeddedFile;
716 catch (
const container::NoSuchElementException&)
720 bCopied = xEmbeddedFile.is() && lcl_CopyStream(xEmbeddedFile->getInputStream(),
xStream->getOutputStream());
725 uno::Reference< io::XStream > xOle10Native;
728 xNameContainer->getByName(
"\1Ole10Native") >>= xOle10Native;
730 catch (container::NoSuchElementException
const&)
734 if (xOle10Native.is())
736 const uno::Reference<io::XInputStream> xIn = xOle10Native->getInputStream();
739 uno::Sequence< sal_Int8 >
aData(1);
743 nRead = xIn->readBytes(aData, 1);
744 }
while (nRead == 1 && aData[0] != 0);
747 nRead = xIn->readBytes(aData, 1);
748 }
while (nRead == 1 && aData[0] != 0);
755 nRead = xIn->readBytes(aData, 1);
756 }
while (nRead == 1 && aData[0] != 0);
757 uno::Sequence< sal_Int8 > aLenData(4);
758 xIn->readBytes(aLenData, 4);
759 sal_uInt32 nLen =
static_cast<sal_uInt32
>(
760 (aLenData[0] & 0xFF) |
761 ((aLenData[1] & 0xFF) << 8) |
762 ((aLenData[2] & 0xFF) << 16) |
763 ((aLenData[3] & 0xFF) << 24));
765 bCopied = lcl_CopyStream(xIn,
xStream->getOutputStream(), nLen);
769 uno::Reference< io::XSeekable > xSeekableStor(xObjectStream, uno::UNO_QUERY);
770 if (xSeekableStor.is())
771 xSeekableStor->seek(0);
774 bCopied = lcl_CopyStream(xObjectStream->getInputStream(),
xStream->getOutputStream());
778 xNativeTempFile->setRemoveFile(
false);
779 rUrl = xNativeTempFile->getUri();
781 xNativeTempFile.clear();
783 uno::Reference < ucb::XSimpleFileAccess3 > xSimpleFileAccess(
784 ucb::SimpleFileAccess::create( xContext ) );
786 xSimpleFileAccess->setReadOnly(rUrl,
true);
790 xNativeTempFile->setRemoveFile(
true);
799 OUString lcl_ExtractObject(
const css::uno::Reference< css::uno::XComponentContext >& xContext,
800 const css::uno::Reference< css::io::XStream >& xObjectStream)
806 lcl_GetExtractedStream(sUrl, xContext, xObjectStream);
809 (void) xObjectStream;
814 uno::Reference < io::XStream > lcl_ExtractObjectStream(
const css::uno::Reference< css::uno::XComponentContext >& xContext,
815 const css::uno::Reference< css::io::XStream >& xObjectStream )
818 return lcl_GetExtractedStream( sUrl, xContext, xObjectStream );
827 if ( xWrappedObject.is() )
830 xWrappedObject->doVerb(embed::EmbedVerbs::MS_OLEVERB_OPEN);
835 ::osl::ResettableMutexGuard aGuard(
m_aMutex );
837 throw lang::DisposedException();
840 throw embed::WrongStateException(
"The object has no persistence!",
861 throw uno::RuntimeException(
"Null reference to OLE component");
879 catch( uno::Exception& )
897 throw embed::UnreachableStateException();
916 uno::Reference< io::XSeekable > xSeekable(
m_xObjectStream, uno::UNO_QUERY );
917 if ( xSeekable.is() )
918 xSeekable->seek( 0 );
922 catch( uno::RuntimeException& )
926 catch (uno::Exception
const&)
952 throw embed::UnreachableStateException();
954 uno::Reference< css::system::XSystemShellExecute > xSystemShellExecute(
955 css::system::SystemShellExecute::create(
m_xContext ) );
956 xSystemShellExecute->execute(
m_aTempDumpURL, OUString(), css::system::SystemShellExecuteFlags::URIS_ONLY);
968 if ( xWrappedObject.is() )
971 return xWrappedObject->getSupportedVerbs();
975 ::osl::MutexGuard aGuard(
m_aMutex );
977 throw lang::DisposedException();
980 throw embed::WrongStateException(
"The object has no persistence!",
1000 uno::Sequence<embed::VerbDescriptor> aRet(1);
1001 aRet.getArray()[0].VerbID = -9;
1008 const uno::Reference< embed::XEmbeddedClient >& xClient )
1011 uno::Reference< embed::XEmbeddedObject > xWrappedObject =
m_xWrappedObject;
1012 if ( xWrappedObject.is() )
1015 xWrappedObject->setClientSite( xClient );
1020 ::osl::MutexGuard aGuard(
m_aMutex );
1022 throw lang::DisposedException();
1027 throw embed::WrongStateException(
1028 "The client site can not be set currently!",
1039 uno::Reference< embed::XEmbeddedObject > xWrappedObject =
m_xWrappedObject;
1040 if ( xWrappedObject.is() )
1043 return xWrappedObject->getClientSite();
1047 ::osl::MutexGuard aGuard(
m_aMutex );
1049 throw lang::DisposedException();
1052 throw embed::WrongStateException(
"The object has no persistence!",
1062 uno::Reference< embed::XEmbeddedObject > xWrappedObject =
m_xWrappedObject;
1063 if ( xWrappedObject.is() )
1066 xWrappedObject->update();
1071 ::osl::MutexGuard aGuard(
m_aMutex );
1073 throw lang::DisposedException();
1076 throw embed::WrongStateException(
"The object has no persistence!",
1079 if (
m_nUpdateMode == embed::EmbedUpdateModes::EXPLICIT_UPDATE )
1094 uno::Reference< embed::XEmbeddedObject > xWrappedObject =
m_xWrappedObject;
1095 if ( xWrappedObject.is() )
1098 xWrappedObject->setUpdateMode( nMode );
1103 ::osl::MutexGuard aGuard(
m_aMutex );
1105 throw lang::DisposedException();
1108 throw embed::WrongStateException(
"The object has no persistence!",
1111 OSL_ENSURE( nMode == embed::EmbedUpdateModes::ALWAYS_UPDATE
1112 || nMode == embed::EmbedUpdateModes::EXPLICIT_UPDATE,
1113 "Unknown update mode!" );
1123 uno::Reference< embed::XEmbeddedObject > xWrappedObject =
m_xWrappedObject;
1124 if ( xWrappedObject.is() )
1127 return xWrappedObject->getStatus( nAspect );
1131 ::osl::MutexGuard aGuard(
m_aMutex );
1133 throw lang::DisposedException();
1136 throw embed::WrongStateException(
"The object must be in running state!",
1139 sal_Int64 nResult = 0;
1155 return ( nResult | embed::EmbedMisc::EMBED_NEEDSSIZEONLOAD );
1162 uno::Reference< embed::XEmbeddedObject > xWrappedObject =
m_xWrappedObject;
1163 if ( xWrappedObject.is() )
1166 xWrappedObject->setContainerName(
sName );
1171 ::osl::MutexGuard aGuard(
m_aMutex );
1173 throw lang::DisposedException();
css::uno::Reference< css::embed::XEmbeddedClient > m_xClientSite
OUString m_aContainerName
virtual void SAL_CALL doVerb(sal_Int32 nVerbID) override
virtual css::uno::Sequence< css::embed::VerbDescriptor > SAL_CALL getSupportedVerbs() override
rtl::Reference< OwnView_Impl > m_xOwnView
bool TryToConvertToOOo(const css::uno::Reference< css::io::XStream > &xStream)
css::uno::Reference< css::embed::XStorage > m_xParentStorage
virtual void SAL_CALL update() override
css::uno::Reference< css::uno::XInterface > m_xParent
virtual css::uno::Reference< css::embed::XEmbeddedClient > SAL_CALL getClientSite() override
std::unique_ptr<::comphelper::OMultiTypeInterfaceContainerHelper2 > m_pInterfaceContainer
sal_Int64 m_nStatusAspect
virtual void SAL_CALL close(sal_Bool DeliverOwnership) override
virtual sal_Int64 SAL_CALL getStatus(sal_Int64 nAspect) override
virtual sal_Int32 SAL_CALL getCurrentState() override
rtl::Reference< OleComponent > m_pOleComponent
OUString MoveToTemporarySubstream()
virtual css::uno::Sequence< sal_Int32 > SAL_CALL getReachableStates() override
css::uno::Reference< css::uno::XComponentContext > m_xContext
VerbExecutionController m_aVerbExecutionController
css::uno::Reference< css::io::XStream > m_xObjectStream
css::uno::Reference< css::embed::XStorage > CreateTemporarySubstorage(OUString &o_aStorageName)
css::awt::Size m_aSizeToSet
css::uno::Reference< css::embed::XEmbeddedObject > m_xWrappedObject
virtual void SAL_CALL setContainerName(const OUString &sName) override
virtual void SAL_CALL setUpdateMode(sal_Int32 nMode) override
virtual void SAL_CALL setClientSite(const css::uno::Reference< css::embed::XEmbeddedClient > &xClient) override
virtual void SAL_CALL changeState(sal_Int32 nNewState) override
static OUString GetFilterNameFromExtentionAndInStream(const css::uno::Reference< css::uno::XComponentContext > &xContext, std::u16string_view aNameWithExtention, const css::uno::Reference< css::io::XInputStream > &xInputStream)
OUString GetFactoryNameByMediaType(const OUString &aMediaType)
css::uno::XInterface * next()
bool hasMoreElements() const
#define TOOLS_WARN_EXCEPTION(area, stream)
Sequence< PropertyValue > aArguments
Sequence< sal_Int8 > aSeq
#define SAL_WARN_IF(condition, area, stream)
#define SAL_WARN(area, stream)
constexpr OUStringLiteral aData
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
Any SAL_CALL getCaughtException()
Reference< XModel > xModel