LibreOffice Module package (master) 1
xfactory.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
20#include <sal/config.h>
21#include <sal/log.hxx>
22
23#include <com/sun/star/ucb/SimpleFileAccess.hpp>
24#include <com/sun/star/embed/ElementModes.hpp>
25#include <com/sun/star/embed/StorageFormats.hpp>
26#include <com/sun/star/beans/PropertyValue.hpp>
27#include <com/sun/star/io/IOException.hpp>
28#include <com/sun/star/io/TempFile.hpp>
29#include <com/sun/star/io/XSeekable.hpp>
30
34#include <cppuhelper/weak.hxx>
35#include <osl/diagnose.h>
36#include <unotools/tempfile.hxx>
37
38#include "xfactory.hxx"
39#include "xstorage.hxx"
40
41using namespace ::com::sun::star;
42
43#if OSL_DEBUG_LEVEL > 0
44#define THROW_WHERE SAL_WHERE
45#else
46#define THROW_WHERE ""
47#endif
48
49static bool CheckPackageSignature_Impl( const uno::Reference< io::XInputStream >& xInputStream,
50 const uno::Reference< io::XSeekable >& xSeekable )
51{
52 if ( !xInputStream.is() || !xSeekable.is() )
53 throw uno::RuntimeException();
54
55 if ( xSeekable->getLength() )
56 {
57 uno::Sequence< sal_Int8 > aData( 4 );
58 xSeekable->seek( 0 );
59 sal_Int32 nRead = xInputStream->readBytes( aData, 4 );
60 xSeekable->seek( 0 );
61
62 // TODO/LATER: should the disk spanned files be supported?
63 // 0x50, 0x4b, 0x07, 0x08
64 return ( nRead == 4 && aData[0] == 0x50 && aData[1] == 0x4b && aData[2] == 0x03 && aData[3] == 0x04 );
65 }
66 else
67 return true; // allow to create a storage based on empty stream
68}
69
70
71
72uno::Reference< uno::XInterface > SAL_CALL OStorageFactory::createInstance()
73{
74 // TODO: reimplement TempStream service to support XStream interface
75 uno::Reference < io::XStream > xTempStream(new utl::TempFileFastService);
76
77 return cppu::getXWeak(new OStorage(xTempStream, embed::ElementModes::READWRITE,
78 uno::Sequence<beans::PropertyValue>(), m_xContext,
79 embed::StorageFormats::PACKAGE));
80}
81
82uno::Reference< uno::XInterface > SAL_CALL OStorageFactory::createInstanceWithArguments(
83 const uno::Sequence< uno::Any >& aArguments )
84{
85 // The request for storage can be done with up to three arguments
86
87 // The first argument specifies a source for the storage
88 // it can be URL, XStream, XInputStream.
89 // The second value is a mode the storage should be open in.
90 // And the third value is a media descriptor.
91
92 sal_Int32 nArgNum = aArguments.getLength();
93 OSL_ENSURE( nArgNum < 4, "Wrong parameter number" );
94
95 if ( !nArgNum )
96 return createInstance();
97
98 // first try to retrieve storage open mode if any
99 // by default the storage will be open in readonly mode
100 sal_Int32 nStorageMode = embed::ElementModes::READ;
101 if ( nArgNum >= 2 )
102 {
103 if( !( aArguments[1] >>= nStorageMode ) )
104 {
105 OSL_FAIL( "Wrong second argument!" );
106 throw lang::IllegalArgumentException(); // TODO:
107 }
108 // it's always possible to read written storage in this implementation
109 nStorageMode |= embed::ElementModes::READ;
110 }
111
112 if ( ( nStorageMode & embed::ElementModes::TRUNCATE ) == embed::ElementModes::TRUNCATE
113 && ( nStorageMode & embed::ElementModes::WRITE ) != embed::ElementModes::WRITE )
114 throw lang::IllegalArgumentException(); // TODO:
115
116 // retrieve storage source stream
117 OUString aURL;
118 uno::Reference< io::XStream > xStream;
119 uno::Reference< io::XInputStream > xInputStream;
120
121 if ( aArguments[0] >>= aURL )
122 {
123 if ( aURL.isEmpty() )
124 {
125 OSL_FAIL( "Empty URL is provided!" );
126 throw lang::IllegalArgumentException(); // TODO:
127 }
128
129 if ( aURL.startsWithIgnoreAsciiCase("vnd.sun.star.pkg:") )
130 {
131 OSL_FAIL( "Packages URL's are not valid for storages!" ); // ???
132 throw lang::IllegalArgumentException(); // TODO:
133 }
134
135 uno::Reference < ucb::XSimpleFileAccess3 > xTempAccess(
136 ucb::SimpleFileAccess::create(
137 m_xContext ) );
138
139 if ( nStorageMode & embed::ElementModes::WRITE )
140 xStream = xTempAccess->openFileReadWrite( aURL );
141 else
142 xInputStream = xTempAccess->openFileRead( aURL );
143 }
144 else if ( !( aArguments[0] >>= xStream ) && !( aArguments[0] >>= xInputStream ) )
145 {
146 OSL_FAIL( "Wrong first argument!" );
147 throw uno::Exception("wrong first arg", nullptr); // TODO: Illegal argument
148 }
149
150 // retrieve mediadescriptor and set storage properties
151 uno::Sequence< beans::PropertyValue > aDescr;
152 uno::Sequence< beans::PropertyValue > aPropsToSet;
153
154 sal_Int32 nStorageType = embed::StorageFormats::PACKAGE;
155
156 if ( nArgNum >= 3 )
157 {
158 if( aArguments[2] >>= aDescr )
159 {
160 if ( !aURL.isEmpty() )
161 {
162 aPropsToSet = { comphelper::makePropertyValue("URL", aURL) };
163 }
164
165 sal_Int32 nNumArgs = 1;
166 for ( const auto& rProp : std::as_const(aDescr) )
167 {
168 if ( rProp.Name == "InteractionHandler"
169 || rProp.Name == "Password"
170 || rProp.Name == "RepairPackage"
171 || rProp.Name == "StatusIndicator" )
172 {
173 aPropsToSet.realloc( ++nNumArgs );
174 auto pPropsToSet = aPropsToSet.getArray();
175 pPropsToSet[nNumArgs-1].Name = rProp.Name;
176 pPropsToSet[nNumArgs-1].Value = rProp.Value;
177 }
178 else if ( rProp.Name == "StorageFormat" )
179 {
180 OUString aFormatName;
181 sal_Int32 nFormatID = 0;
182 if ( rProp.Value >>= aFormatName )
183 {
184 if ( aFormatName == PACKAGE_STORAGE_FORMAT_STRING )
185 nStorageType = embed::StorageFormats::PACKAGE;
186 else if ( aFormatName == ZIP_STORAGE_FORMAT_STRING )
187 nStorageType = embed::StorageFormats::ZIP;
188 else if ( aFormatName == OFOPXML_STORAGE_FORMAT_STRING )
189 nStorageType = embed::StorageFormats::OFOPXML;
190 else
191 throw lang::IllegalArgumentException( THROW_WHERE, uno::Reference< uno::XInterface >(), 1 );
192 }
193 else if ( rProp.Value >>= nFormatID )
194 {
195 if ( nFormatID != embed::StorageFormats::PACKAGE
196 && nFormatID != embed::StorageFormats::ZIP
197 && nFormatID != embed::StorageFormats::OFOPXML )
198 throw lang::IllegalArgumentException( THROW_WHERE, uno::Reference< uno::XInterface >(), 1 );
199
200 nStorageType = nFormatID;
201 }
202 else
203 throw lang::IllegalArgumentException( THROW_WHERE, uno::Reference< uno::XInterface >(), 1 );
204 }
205 else if (rProp.Name == "NoFileSync")
206 {
207 // Forward NoFileSync to the storage.
208 aPropsToSet.realloc(++nNumArgs);
209 auto pPropsToSet = aPropsToSet.getArray();
210 pPropsToSet[nNumArgs - 1].Name = rProp.Name;
211 pPropsToSet[nNumArgs - 1].Value = rProp.Value;
212 }
213 else
214 OSL_FAIL( "Unacceptable property, will be ignored!" );
215 }
216 }
217 else
218 {
219 OSL_FAIL( "Wrong third argument!" );
220 throw uno::Exception("wrong 3rd arg", nullptr); // TODO: Illegal argument
221 }
222
223 }
224
225 // create storage based on source
226 if ( xInputStream.is() )
227 {
228 // if xInputStream is set the storage should be open from it
229 if ( nStorageMode & embed::ElementModes::WRITE )
230 throw uno::Exception("storagemode==write", nullptr); // TODO: access denied
231
232 uno::Reference< io::XSeekable > xSeekable( xInputStream, uno::UNO_QUERY );
233 if ( !xSeekable.is() )
234 {
235 // TODO: wrap stream to let it be seekable
236 OSL_FAIL( "Nonseekable streams are not supported for now!" );
237 }
238
239 if ( !CheckPackageSignature_Impl( xInputStream, xSeekable ) )
240 throw io::IOException("package signature check failed, probably not a package file", nullptr); // TODO: this is not a package file
241
242 return cppu::getXWeak(
243 new OStorage(xInputStream, nStorageMode, aPropsToSet, m_xContext, nStorageType));
244 }
245 else if ( xStream.is() )
246 {
247 if ( ( ( nStorageMode & embed::ElementModes::WRITE ) && !xStream->getOutputStream().is() )
248 || !xStream->getInputStream().is() )
249 throw uno::Exception("access denied", nullptr); // TODO: access denied
250
251 uno::Reference< io::XSeekable > xSeekable( xStream, uno::UNO_QUERY );
252 if ( !xSeekable.is() )
253 {
254 // TODO: wrap stream to let it be seekable
255 OSL_FAIL( "Nonseekable streams are not supported for now!" );
256 }
257
258 if ( !CheckPackageSignature_Impl( xStream->getInputStream(), xSeekable ) )
259 throw io::IOException(); // TODO: this is not a package file
260
261 return cppu::getXWeak(
262 new OStorage(xStream, nStorageMode, aPropsToSet, m_xContext, nStorageType));
263 }
264
265 throw uno::Exception("no input stream or regular stream", nullptr); // general error during creation
266}
267
269{
270 return "com.sun.star.comp.embed.StorageFactory";
271}
272
273sal_Bool SAL_CALL OStorageFactory::supportsService( const OUString& ServiceName )
274{
276}
277
278uno::Sequence< OUString > SAL_CALL OStorageFactory::getSupportedServiceNames()
279{
280 return { "com.sun.star.embed.StorageFactory",
281 "com.sun.star.comp.embed.StorageFactory" };
282}
283
284
285extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
287 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
288{
289 return cppu::acquire(new OStorageFactory(context));
290}
291
292/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XInputStream > xStream
virtual OUString SAL_CALL getImplementationName() override
Definition: xfactory.cxx:268
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithArguments(const css::uno::Sequence< css::uno::Any > &aArguments) override
Definition: xfactory.cxx:82
css::uno::Reference< css::uno::XComponentContext > m_xContext
Definition: xfactory.hxx:33
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: xfactory.cxx:273
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstance() override
Definition: xfactory.cxx:72
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: xfactory.cxx:278
URL aURL
Sequence< PropertyValue > aArguments
constexpr OUStringLiteral aData
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
constexpr OUStringLiteral ZIP_STORAGE_FORMAT_STRING
constexpr OUStringLiteral OFOPXML_STORAGE_FORMAT_STRING
constexpr OUStringLiteral PACKAGE_STORAGE_FORMAT_STRING
unsigned char sal_Bool
#define THROW_WHERE
Definition: xfactory.cxx:44
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * package_OStorageFactory_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
Definition: xfactory.cxx:286
static bool CheckPackageSignature_Impl(const uno::Reference< io::XInputStream > &xInputStream, const uno::Reference< io::XSeekable > &xSeekable)
Definition: xfactory.cxx:49