1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "oo3extensionmigration.hxx"
22 #include <sal/log.hxx>
23 #include <osl/file.hxx>
24 #include <comphelper/diagnose_ex.hxx>
25 #include <unotools/bootstrap.hxx>
26 #include <unotools/textsearch.hxx>
27 #include <comphelper/sequence.hxx>
28 #include <cppuhelper/supportsservice.hxx>
29 #include <rtl/ref.hxx>
31 #include <com/sun/star/task/XInteractionApprove.hpp>
32 #include <com/sun/star/ucb/CommandAbortedException.hpp>
33 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
34 #include <com/sun/star/ucb/SimpleFileAccess.hpp>
35 #include <com/sun/star/xml/xpath/XPathAPI.hpp>
36 #include <com/sun/star/xml/xpath/XPathException.hpp>
37 #include <com/sun/star/xml/dom/DOMException.hpp>
38 #include <com/sun/star/xml/dom/DocumentBuilder.hpp>
39 #include <com/sun/star/beans/NamedValue.hpp>
40 #include <com/sun/star/deployment/ExtensionManager.hpp>
41 #include <com/sun/star/deployment/XExtensionManager.hpp>
43 using namespace ::com::sun::star
;
44 using namespace ::com::sun::star::uno
;
52 OO3ExtensionMigration::OO3ExtensionMigration(Reference
< XComponentContext
> const & ctx
) :
58 OO3ExtensionMigration::~OO3ExtensionMigration()
62 void OO3ExtensionMigration::scanUserExtensions( const OUString
& sSourceDir
, TStringVector
& aMigrateExtensions
)
64 osl::Directory
aScanRootDir( sSourceDir
);
65 osl::FileStatus
fs(osl_FileStatus_Mask_Type
| osl_FileStatus_Mask_FileURL
);
66 osl::FileBase::RC nRetCode
= aScanRootDir
.open();
67 if ( nRetCode
!= osl::Directory::E_None
)
70 sal_uInt32
nHint( 0 );
71 osl::DirectoryItem aItem
;
72 while ( aScanRootDir
.getNextItem( aItem
, nHint
) == osl::Directory::E_None
)
74 if (( aItem
.getFileStatus(fs
) == osl::FileBase::E_None
) &&
75 ( fs
.getFileType() == osl::FileStatus::Directory
))
77 //Check next folder as the "real" extension folder is below a temp folder!
78 OUString sExtensionFolderURL
= fs
.getFileURL();
80 osl::Directory
aExtensionRootDir( sExtensionFolderURL
);
82 nRetCode
= aExtensionRootDir
.open();
83 if ( nRetCode
== osl::Directory::E_None
)
85 osl::DirectoryItem aExtDirItem
;
86 while ( aExtensionRootDir
.getNextItem( aExtDirItem
, nHint
) == osl::Directory::E_None
)
88 bool bFileStatus
= aExtDirItem
.getFileStatus(fs
) == osl::FileBase::E_None
;
89 bool bIsDir
= fs
.getFileType() == osl::FileStatus::Directory
;
91 if ( bFileStatus
&& bIsDir
)
93 sExtensionFolderURL
= fs
.getFileURL();
94 ScanResult eResult
= scanExtensionFolder( sExtensionFolderURL
);
95 if ( eResult
== SCANRESULT_MIGRATE_EXTENSION
)
96 aMigrateExtensions
.push_back( sExtensionFolderURL
);
105 OO3ExtensionMigration::ScanResult
OO3ExtensionMigration::scanExtensionFolder( const OUString
& sExtFolder
)
107 ScanResult aResult
= SCANRESULT_NOTFOUND
;
108 osl::Directory
aDir(sExtFolder
);
111 if (aDir
.open() == osl::FileBase::E_None
)
113 // work through directory contents...
114 osl::DirectoryItem item
;
115 osl::FileStatus
fs(osl_FileStatus_Mask_Type
| osl_FileStatus_Mask_FileURL
);
116 TStringVector aDirectories
;
117 while ((aDir
.getNextItem(item
) == osl::FileBase::E_None
) &&
118 ( aResult
== SCANRESULT_NOTFOUND
))
120 if (item
.getFileStatus(fs
) == osl::FileBase::E_None
)
122 if (fs
.getFileType() == osl::FileStatus::Directory
)
123 aDirectories
.push_back( fs
.getFileURL() );
126 OUString aDirEntryURL
= fs
.getFileURL();
127 if ( aDirEntryURL
.indexOf( "/description.xml" ) > 0 )
128 aResult
= scanDescriptionXml( aDirEntryURL
) ? SCANRESULT_MIGRATE_EXTENSION
: SCANRESULT_DONTMIGRATE_EXTENSION
;
133 for (auto const& directory
: aDirectories
)
135 aResult
= scanExtensionFolder(directory
);
136 if (aResult
!= SCANRESULT_NOTFOUND
)
143 bool OO3ExtensionMigration::scanDescriptionXml( const OUString
& sDescriptionXmlURL
)
145 if ( !m_xDocBuilder
.is() )
147 m_xDocBuilder
.set( xml::dom::DocumentBuilder::create(m_ctx
) );
150 if ( !m_xSimpleFileAccess
.is() )
152 m_xSimpleFileAccess
= ucb::SimpleFileAccess::create(m_ctx
);
155 OUString aExtIdentifier
;
158 uno::Reference
< io::XInputStream
> xIn
=
159 m_xSimpleFileAccess
->openFileRead( sDescriptionXmlURL
);
163 uno::Reference
< xml::dom::XDocument
> xDoc
= m_xDocBuilder
->parse( xIn
);
166 uno::Reference
< xml::dom::XElement
> xRoot
= xDoc
->getDocumentElement();
167 if ( xRoot
.is() && xRoot
->getTagName() == "description" )
169 uno::Reference
< xml::xpath::XXPathAPI
> xPath
= xml::xpath::XPathAPI::create(m_ctx
);
171 xPath
->registerNS("desc", xRoot
->getNamespaceURI());
172 xPath
->registerNS("xlink", "http://www.w3.org/1999/xlink");
176 uno::Reference
< xml::dom::XNode
> xNode(
177 xPath
->selectSingleNode(
178 xRoot
, "desc:identifier/@value" ));
180 aExtIdentifier
= xNode
->getNodeValue();
182 catch ( const xml::xpath::XPathException
& )
185 catch ( const xml::dom::DOMException
& )
192 if ( !aExtIdentifier
.isEmpty() )
194 // scan extension identifier and try to match with our black list entries
195 for (const OUString
& i
: m_aDenyList
)
197 utl::SearchParam
param(i
, utl::SearchParam::SearchType::Regexp
);
198 utl::TextSearch
ts(param
, LANGUAGE_DONTKNOW
);
201 sal_Int32 end
= aExtIdentifier
.getLength();
202 if (ts
.SearchForward(aExtIdentifier
, &start
, &end
))
207 catch ( const ucb::CommandAbortedException
& )
210 catch ( const uno::RuntimeException
& )
214 if ( aExtIdentifier
.isEmpty() )
217 // Try to use the folder name to match our black list
218 // as some extensions don't provide an identifier in the
220 for (const OUString
& i
: m_aDenyList
)
222 utl::SearchParam
param(i
, utl::SearchParam::SearchType::Regexp
);
223 utl::TextSearch
ts(param
, LANGUAGE_DONTKNOW
);
226 sal_Int32 end
= sDescriptionXmlURL
.getLength();
227 if (ts
.SearchForward(sDescriptionXmlURL
, &start
, &end
))
235 void OO3ExtensionMigration::migrateExtension( const OUString
& sSourceDir
)
237 css::uno::Reference
< css::deployment::XExtensionManager
> extMgr(
238 deployment::ExtensionManager::get( m_ctx
) );
241 rtl::Reference
<TmpRepositoryCommandEnv
> pCmdEnv
= new TmpRepositoryCommandEnv();
243 uno::Reference
< task::XAbortChannel
> xAbortChannel
;
244 extMgr
->addExtension(
245 sSourceDir
, uno::Sequence
<beans::NamedValue
>(), "user",
246 xAbortChannel
, pCmdEnv
);
248 catch ( css::uno::Exception
& )
250 TOOLS_WARN_EXCEPTION(
252 "Ignoring UNO Exception while migrating extension from <" << sSourceDir
<< ">");
260 OUString
OO3ExtensionMigration::getImplementationName()
262 return "com.sun.star.comp.desktop.migration.OOo3Extensions";
266 sal_Bool
OO3ExtensionMigration::supportsService(OUString
const & ServiceName
)
268 return cppu::supportsService(this, ServiceName
);
272 Sequence
< OUString
> OO3ExtensionMigration::getSupportedServiceNames()
274 return { "com.sun.star.migration.Extensions" };
281 void OO3ExtensionMigration::initialize( const Sequence
< Any
>& aArguments
)
283 ::osl::MutexGuard
aGuard( m_aMutex
);
285 const Any
* pIter
= aArguments
.getConstArray();
286 const Any
* pEnd
= pIter
+ aArguments
.getLength();
287 for ( ; pIter
!= pEnd
; ++pIter
)
289 beans::NamedValue aValue
;
291 if ( aValue
.Name
== "UserData" )
293 if ( !(aValue
.Value
>>= m_sSourceDir
) )
295 OSL_FAIL( "ExtensionMigration::initialize: argument UserData has wrong type!" );
298 else if ( aValue
.Name
== "ExtensionDenyList" )
300 Sequence
< OUString
> aDenyList
;
301 if ( (aValue
.Value
>>= aDenyList
) && aDenyList
.hasElements())
303 m_aDenyList
.resize( aDenyList
.getLength() );
304 ::comphelper::sequenceToArray
< OUString
>( m_aDenyList
.data(), aDenyList
);
310 Any
OO3ExtensionMigration::execute( const Sequence
< beans::NamedValue
>& )
312 ::osl::MutexGuard
aGuard( m_aMutex
);
314 ::utl::Bootstrap::PathStatus aStatus
= ::utl::Bootstrap::locateUserInstallation( m_sTargetDir
);
315 if ( aStatus
== ::utl::Bootstrap::PATH_EXISTS
)
317 // copy all extensions
318 OUString sSourceDir
= m_sSourceDir
+
319 "/user/uno_packages/cache/uno_packages";
320 TStringVector aExtensionToMigrate
;
321 scanUserExtensions( sSourceDir
, aExtensionToMigrate
);
322 for (auto const& extensionToMigrate
: aExtensionToMigrate
)
324 migrateExtension(extensionToMigrate
);
332 // TmpRepositoryCommandEnv
335 TmpRepositoryCommandEnv::TmpRepositoryCommandEnv()
339 TmpRepositoryCommandEnv::~TmpRepositoryCommandEnv()
342 // XCommandEnvironment
344 uno::Reference
< task::XInteractionHandler
> TmpRepositoryCommandEnv::getInteractionHandler()
350 uno::Reference
< ucb::XProgressHandler
> TmpRepositoryCommandEnv::getProgressHandler()
355 // XInteractionHandler
356 void TmpRepositoryCommandEnv::handle(
357 uno::Reference
< task::XInteractionRequest
> const & xRequest
)
359 OSL_ASSERT( xRequest
->getRequest().getValueTypeClass() == uno::TypeClass_EXCEPTION
);
364 uno::Sequence
< Reference
< task::XInteractionContinuation
> > conts(
365 xRequest
->getContinuations() );
366 Reference
< task::XInteractionContinuation
> const * pConts
=
367 conts
.getConstArray();
368 sal_Int32 len
= conts
.getLength();
369 for ( sal_Int32 pos
= 0; pos
< len
; ++pos
)
372 uno::Reference
< task::XInteractionApprove
> xInteractionApprove(
373 pConts
[ pos
], uno::UNO_QUERY
);
374 if (xInteractionApprove
.is()) {
375 xInteractionApprove
->select();
376 // don't query again for ongoing continuations:
384 void TmpRepositoryCommandEnv::push( uno::Any
const & /*Status*/ )
389 void TmpRepositoryCommandEnv::update( uno::Any
const & /*Status */)
393 void TmpRepositoryCommandEnv::pop()
398 } // namespace migration
401 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
402 desktop_OO3ExtensionMigration_get_implementation(
403 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const&)
405 return cppu::acquire(new migration::OO3ExtensionMigration(context
));
409 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */