12722ceddSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
32722ceddSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
42722ceddSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
52722ceddSAndrew Rist  * distributed with this work for additional information
62722ceddSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
72722ceddSAndrew Rist  * to you under the Apache License, Version 2.0 (the
82722ceddSAndrew Rist  * "License"); you may not use this file except in compliance
92722ceddSAndrew Rist  * with the License.  You may obtain a copy of the License at
102722ceddSAndrew Rist  *
112722ceddSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
122722ceddSAndrew Rist  *
132722ceddSAndrew Rist  * Unless required by applicable law or agreed to in writing,
142722ceddSAndrew Rist  * software distributed under the License is distributed on an
152722ceddSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
162722ceddSAndrew Rist  * KIND, either express or implied.  See the License for the
172722ceddSAndrew Rist  * specific language governing permissions and limitations
182722ceddSAndrew Rist  * under the License.
192722ceddSAndrew Rist  *
202722ceddSAndrew Rist  *************************************************************/
212722ceddSAndrew Rist 
222722ceddSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_desktop.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "comphelper/servicedecl.hxx"
30cdf0e10cSrcweir #include "cppuhelper/exc_hlp.hxx"
31cdf0e10cSrcweir #include "rtl/bootstrap.hxx"
32cdf0e10cSrcweir #include "com/sun/star/deployment/ExtensionManager.hpp"
33cdf0e10cSrcweir #include "com/sun/star/deployment/XExtensionManager.hpp"
34cdf0e10cSrcweir #include "com/sun/star/deployment/thePackageManagerFactory.hpp"
35cdf0e10cSrcweir #include "com/sun/star/deployment/XPackageManager.hpp"
36cdf0e10cSrcweir #include "com/sun/star/deployment/XPackageManagerFactory.hpp"
37cdf0e10cSrcweir #include "com/sun/star/deployment/XPackage.hpp"
38cdf0e10cSrcweir #include "com/sun/star/deployment/InstallException.hpp"
39cdf0e10cSrcweir #include "com/sun/star/deployment/VersionException.hpp"
40cdf0e10cSrcweir #include "com/sun/star/deployment/LicenseException.hpp"
41cdf0e10cSrcweir #include "com/sun/star/lang/XServiceInfo.hpp"
42cdf0e10cSrcweir #include "com/sun/star/registry/XRegistryKey.hpp"
43cdf0e10cSrcweir #include "com/sun/star/beans/Optional.hpp"
44cdf0e10cSrcweir #include "com/sun/star/task/XInteractionApprove.hpp"
45cdf0e10cSrcweir #include "com/sun/star/beans/Ambiguous.hpp"
46cdf0e10cSrcweir #include "com/sun/star/uno/XComponentContext.hpp"
47cdf0e10cSrcweir #include "com/sun/star/io/XInputStream.hpp"
48cdf0e10cSrcweir #include "com/sun/star/util/XModifyBroadcaster.hpp"
49cdf0e10cSrcweir #include "comphelper/sequence.hxx"
50cdf0e10cSrcweir #include "xmlscript/xml_helper.hxx"
51cdf0e10cSrcweir #include "osl/diagnose.h"
52cdf0e10cSrcweir #include "dp_interact.h"
53cdf0e10cSrcweir #include "dp_resource.h"
54cdf0e10cSrcweir #include "dp_ucb.h"
55cdf0e10cSrcweir #include "dp_identifier.hxx"
56cdf0e10cSrcweir #include "dp_descriptioninfoset.hxx"
57cdf0e10cSrcweir #include "dp_extensionmanager.hxx"
58cdf0e10cSrcweir #include "dp_commandenvironments.hxx"
59cdf0e10cSrcweir #include "dp_properties.hxx"
60cdf0e10cSrcweir #include "boost/bind.hpp"
61cdf0e10cSrcweir 
62cdf0e10cSrcweir #include <list>
63cdf0e10cSrcweir #include <hash_map>
64cdf0e10cSrcweir #include <algorithm>
65cdf0e10cSrcweir 
66cdf0e10cSrcweir namespace deploy = com::sun::star::deployment;
67cdf0e10cSrcweir namespace lang  = com::sun::star::lang;
68cdf0e10cSrcweir namespace registry = com::sun::star::registry;
69cdf0e10cSrcweir namespace task = com::sun::star::task;
70cdf0e10cSrcweir namespace ucb = com::sun::star::ucb;
71cdf0e10cSrcweir namespace uno = com::sun::star::uno;
72cdf0e10cSrcweir namespace beans = com::sun::star::beans;
73cdf0e10cSrcweir namespace util = com::sun::star::util;
74cdf0e10cSrcweir namespace css = com::sun::star;
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 
77cdf0e10cSrcweir //#define OUSTR(s) rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(s))
78cdf0e10cSrcweir 
79cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
80cdf0e10cSrcweir using ::rtl::OUString;
81cdf0e10cSrcweir 
82cdf0e10cSrcweir namespace {
83cdf0e10cSrcweir 
84cdf0e10cSrcweir struct CompIdentifiers
85cdf0e10cSrcweir {
operator ()__anon30963b050111::CompIdentifiers86cdf0e10cSrcweir     bool operator() (::std::vector<Reference<deploy::XPackage> > const & a,
87cdf0e10cSrcweir                      ::std::vector<Reference<deploy::XPackage> > const & b)
88cdf0e10cSrcweir         {
89cdf0e10cSrcweir 
90cdf0e10cSrcweir             if (getName(a).compareTo(getName(b)) < 0)
91cdf0e10cSrcweir                 return true;
92cdf0e10cSrcweir             return false;
93cdf0e10cSrcweir         }
94cdf0e10cSrcweir 
95cdf0e10cSrcweir     OUString getName(::std::vector<Reference<deploy::XPackage> > const & a);
96cdf0e10cSrcweir };
97cdf0e10cSrcweir 
getName(::std::vector<Reference<deploy::XPackage>> const & a)98cdf0e10cSrcweir OUString CompIdentifiers::getName(::std::vector<Reference<deploy::XPackage> > const & a)
99cdf0e10cSrcweir {
100cdf0e10cSrcweir     OSL_ASSERT(a.size() == 3);
101cdf0e10cSrcweir     //get the first non-null reference
102cdf0e10cSrcweir     Reference<deploy::XPackage>  extension;
103cdf0e10cSrcweir     ::std::vector<Reference<deploy::XPackage> >::const_iterator it = a.begin();
104cdf0e10cSrcweir     for (; it != a.end(); it++)
105cdf0e10cSrcweir     {
106cdf0e10cSrcweir         if (it->is())
107cdf0e10cSrcweir         {
108cdf0e10cSrcweir             extension = *it;
109cdf0e10cSrcweir             break;
110cdf0e10cSrcweir         }
111cdf0e10cSrcweir     }
112cdf0e10cSrcweir     OSL_ASSERT(extension.is());
113cdf0e10cSrcweir     return extension->getDisplayName();
114cdf0e10cSrcweir }
115cdf0e10cSrcweir 
writeLastModified(OUString & url,Reference<ucb::XCommandEnvironment> const & xCmdEnv)116cdf0e10cSrcweir void writeLastModified(OUString & url, Reference<ucb::XCommandEnvironment> const & xCmdEnv)
117cdf0e10cSrcweir {
118cdf0e10cSrcweir     //Write the lastmodified file
119cdf0e10cSrcweir     try {
120cdf0e10cSrcweir         ::rtl::Bootstrap::expandMacros(url);
121cdf0e10cSrcweir         ::ucbhelper::Content ucbStamp(url, xCmdEnv );
122cdf0e10cSrcweir         dp_misc::erase_path( url, xCmdEnv );
123cdf0e10cSrcweir         ::rtl::OString stamp("1" );
124cdf0e10cSrcweir         Reference<css::io::XInputStream> xData(
125cdf0e10cSrcweir             ::xmlscript::createInputStream(
126cdf0e10cSrcweir                 ::rtl::ByteSequence(
127cdf0e10cSrcweir                     reinterpret_cast<sal_Int8 const *>(stamp.getStr()),
128cdf0e10cSrcweir                     stamp.getLength() ) ) );
129cdf0e10cSrcweir         ucbStamp.writeStream( xData, true /* replace existing */ );
130cdf0e10cSrcweir     }
131cdf0e10cSrcweir     catch(...)
132cdf0e10cSrcweir     {
133cdf0e10cSrcweir         uno::Any exc(::cppu::getCaughtException());
134cdf0e10cSrcweir         throw deploy::DeploymentException(
135cdf0e10cSrcweir             OUSTR("Failed to update") + url, 0, exc);
136cdf0e10cSrcweir     }
137cdf0e10cSrcweir }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir class ExtensionRemoveGuard
140cdf0e10cSrcweir {
141cdf0e10cSrcweir     css::uno::Reference<css::deployment::XPackage> m_extension;
142cdf0e10cSrcweir     css::uno::Reference<css::deployment::XPackageManager> m_xPackageManager;
143cdf0e10cSrcweir 
144cdf0e10cSrcweir public:
ExtensionRemoveGuard()1457bc7b19fSMichael Stahl     ExtensionRemoveGuard(){};
ExtensionRemoveGuard(css::uno::Reference<css::deployment::XPackage> const & extension,css::uno::Reference<css::deployment::XPackageManager> const & xPackageManager)146cdf0e10cSrcweir     ExtensionRemoveGuard(
147cdf0e10cSrcweir         css::uno::Reference<css::deployment::XPackage> const & extension,
148cdf0e10cSrcweir         css::uno::Reference<css::deployment::XPackageManager> const & xPackageManager):
149cdf0e10cSrcweir         m_extension(extension), m_xPackageManager(xPackageManager) {}
150cdf0e10cSrcweir     ~ExtensionRemoveGuard();
151cdf0e10cSrcweir 
set(css::uno::Reference<css::deployment::XPackage> const & extension,css::uno::Reference<css::deployment::XPackageManager> const & xPackageManager)1527bc7b19fSMichael Stahl     void set(css::uno::Reference<css::deployment::XPackage> const & extension,
1537bc7b19fSMichael Stahl              css::uno::Reference<css::deployment::XPackageManager> const & xPackageManager) {
154cdf0e10cSrcweir         m_extension = extension;
1557bc7b19fSMichael Stahl         m_xPackageManager = xPackageManager;
156cdf0e10cSrcweir     }
157cdf0e10cSrcweir };
158cdf0e10cSrcweir 
~ExtensionRemoveGuard()159cdf0e10cSrcweir ExtensionRemoveGuard::~ExtensionRemoveGuard()
160cdf0e10cSrcweir {
161cdf0e10cSrcweir     try {
1627bc7b19fSMichael Stahl         OSL_ASSERT(!(m_extension.is() && !m_xPackageManager.is()));
163cdf0e10cSrcweir         if (m_xPackageManager.is() && m_extension.is())
164cdf0e10cSrcweir             m_xPackageManager->removePackage(
165cdf0e10cSrcweir                 dp_misc::getIdentifier(m_extension), ::rtl::OUString(),
166cdf0e10cSrcweir                 css::uno::Reference<css::task::XAbortChannel>(),
167cdf0e10cSrcweir                 css::uno::Reference<css::ucb::XCommandEnvironment>());
168cdf0e10cSrcweir     } catch (...) {
169cdf0e10cSrcweir         OSL_ASSERT(0);
170cdf0e10cSrcweir     }
171cdf0e10cSrcweir }
172cdf0e10cSrcweir 
173cdf0e10cSrcweir } //end namespace
174cdf0e10cSrcweir 
175cdf0e10cSrcweir namespace dp_manager {
176cdf0e10cSrcweir 
177cdf0e10cSrcweir 
178cdf0e10cSrcweir 
179cdf0e10cSrcweir //------------------------------------------------------------------------------
180cdf0e10cSrcweir 
181cdf0e10cSrcweir //ToDo: bundled extension
ExtensionManager(Reference<uno::XComponentContext> const & xContext)182cdf0e10cSrcweir ExtensionManager::ExtensionManager( Reference< uno::XComponentContext > const& xContext) :
183cdf0e10cSrcweir     ::cppu::WeakComponentImplHelper1< css::deployment::XExtensionManager >(getMutex()),
184cdf0e10cSrcweir     m_xContext( xContext )
185cdf0e10cSrcweir {
186cdf0e10cSrcweir     m_xPackageManagerFactory = deploy::thePackageManagerFactory::get(m_xContext);
187cdf0e10cSrcweir     OSL_ASSERT(m_xPackageManagerFactory.is());
188cdf0e10cSrcweir 
189cdf0e10cSrcweir     m_repositoryNames.push_back(OUSTR("user"));
190cdf0e10cSrcweir     m_repositoryNames.push_back(OUSTR("shared"));
191cdf0e10cSrcweir     m_repositoryNames.push_back(OUSTR("bundled"));
192cdf0e10cSrcweir }
193cdf0e10cSrcweir 
194cdf0e10cSrcweir //------------------------------------------------------------------------------
195cdf0e10cSrcweir 
~ExtensionManager()196cdf0e10cSrcweir ExtensionManager::~ExtensionManager()
197cdf0e10cSrcweir {
198cdf0e10cSrcweir }
199cdf0e10cSrcweir 
getUserRepository()200cdf0e10cSrcweir Reference<deploy::XPackageManager> ExtensionManager::getUserRepository()
201cdf0e10cSrcweir {
202cdf0e10cSrcweir     return m_xPackageManagerFactory->getPackageManager(OUSTR("user"));
203cdf0e10cSrcweir }
getSharedRepository()204cdf0e10cSrcweir Reference<deploy::XPackageManager>  ExtensionManager::getSharedRepository()
205cdf0e10cSrcweir {
206cdf0e10cSrcweir     return m_xPackageManagerFactory->getPackageManager(OUSTR("shared"));
207cdf0e10cSrcweir }
getBundledRepository()208cdf0e10cSrcweir Reference<deploy::XPackageManager>  ExtensionManager::getBundledRepository()
209cdf0e10cSrcweir {
210cdf0e10cSrcweir     return m_xPackageManagerFactory->getPackageManager(OUSTR("bundled"));
211cdf0e10cSrcweir }
getTmpRepository()212cdf0e10cSrcweir Reference<deploy::XPackageManager>  ExtensionManager::getTmpRepository()
213cdf0e10cSrcweir {
214cdf0e10cSrcweir     return m_xPackageManagerFactory->getPackageManager(OUSTR("tmp"));
215cdf0e10cSrcweir }
getBakRepository()2167bc7b19fSMichael Stahl Reference<deploy::XPackageManager>  ExtensionManager::getBakRepository()
2177bc7b19fSMichael Stahl {
2187bc7b19fSMichael Stahl     return m_xPackageManagerFactory->getPackageManager(OUSTR("bak"));
2197bc7b19fSMichael Stahl }
220cdf0e10cSrcweir 
createAbortChannel()221cdf0e10cSrcweir Reference<task::XAbortChannel> ExtensionManager::createAbortChannel()
222cdf0e10cSrcweir     throw (uno::RuntimeException)
223cdf0e10cSrcweir {
224cdf0e10cSrcweir     return new dp_misc::AbortChannel;
225cdf0e10cSrcweir }
226cdf0e10cSrcweir 
227cdf0e10cSrcweir css::uno::Reference<css::deployment::XPackageManager>
getPackageManager(::rtl::OUString const & repository)228cdf0e10cSrcweir ExtensionManager::getPackageManager(::rtl::OUString const & repository)
229cdf0e10cSrcweir     throw (css::lang::IllegalArgumentException)
230cdf0e10cSrcweir {
231cdf0e10cSrcweir     Reference<deploy::XPackageManager> xPackageManager;
232cdf0e10cSrcweir     if (repository.equals(OUSTR("user")))
233cdf0e10cSrcweir         xPackageManager = getUserRepository();
234cdf0e10cSrcweir     else if (repository.equals(OUSTR("shared")))
235cdf0e10cSrcweir         xPackageManager = getSharedRepository();
236cdf0e10cSrcweir     else if (repository.equals(OUSTR("bundled")))
237cdf0e10cSrcweir         xPackageManager = getBundledRepository();
2388402cd44SMichael Stahl     else if (repository.equals(OUSTR("tmp")))
2398402cd44SMichael Stahl         xPackageManager = getTmpRepository();
2408402cd44SMichael Stahl     else if (repository.equals(OUSTR("bak")))
2418402cd44SMichael Stahl         xPackageManager = getBakRepository();
242c6dedb65SAndre Fischer     else if (repository.equals(OUSTR("bundled_prereg")))
243c6dedb65SAndre Fischer 		xPackageManager = m_xPackageManagerFactory->getPackageManager(repository);
244cdf0e10cSrcweir     else
245cdf0e10cSrcweir         throw lang::IllegalArgumentException(
246cdf0e10cSrcweir             OUSTR("No valid repository name provided."),
247c6dedb65SAndre Fischer 			static_cast<cppu::OWeakObject*>(this), 0);
248cdf0e10cSrcweir     return xPackageManager;
249cdf0e10cSrcweir }
250cdf0e10cSrcweir 
251cdf0e10cSrcweir 
252cdf0e10cSrcweir /*
253cdf0e10cSrcweir   Enters the XPackage objects into a map. They must be all from the
254cdf0e10cSrcweir   same repository. The value type of the map is a vector, where each vector
255cdf0e10cSrcweir   represents an extension with a particular identifier. The first member
256cdf0e10cSrcweir   is represents the user extension, the second the shared extension and the
257cdf0e10cSrcweir   third the bundled extension.
258cdf0e10cSrcweir  */
addExtensionsToMap(id2extensions & mapExt,uno::Sequence<Reference<deploy::XPackage>> const & seqExt,OUString const & repository)259cdf0e10cSrcweir void ExtensionManager::addExtensionsToMap(
260cdf0e10cSrcweir     id2extensions & mapExt,
261cdf0e10cSrcweir     uno::Sequence<Reference<deploy::XPackage> > const & seqExt,
262cdf0e10cSrcweir     OUString const & repository)
263cdf0e10cSrcweir {
264cdf0e10cSrcweir     //Determine the index in the vector where these extensions are to be
265cdf0e10cSrcweir     //added.
266cdf0e10cSrcweir     ::std::list<OUString>::const_iterator citNames =
267cdf0e10cSrcweir         m_repositoryNames.begin();
268cdf0e10cSrcweir     int index = 0;
269cdf0e10cSrcweir     for (;citNames != m_repositoryNames.end(); citNames++, index++)
270cdf0e10cSrcweir     {
271cdf0e10cSrcweir         if (citNames->equals(repository))
272cdf0e10cSrcweir             break;
273cdf0e10cSrcweir     }
274cdf0e10cSrcweir 
275cdf0e10cSrcweir     for (int i = 0; i < seqExt.getLength(); i++)
276cdf0e10cSrcweir     {
277cdf0e10cSrcweir         Reference<deploy::XPackage> const & xExtension = seqExt[i];
278cdf0e10cSrcweir         OUString id = dp_misc::getIdentifier(xExtension);
279cdf0e10cSrcweir         id2extensions::iterator ivec =  mapExt.find(id);
280cdf0e10cSrcweir         if (ivec == mapExt.end())
281cdf0e10cSrcweir         {
282cdf0e10cSrcweir             ::std::vector<Reference<deploy::XPackage> > vec(3);
283cdf0e10cSrcweir             vec[index] = xExtension;
284cdf0e10cSrcweir             mapExt[id] = vec;
285cdf0e10cSrcweir         }
286cdf0e10cSrcweir         else
287cdf0e10cSrcweir         {
288cdf0e10cSrcweir             ivec->second[index] = xExtension;
289cdf0e10cSrcweir         }
290cdf0e10cSrcweir     }
291cdf0e10cSrcweir }
292cdf0e10cSrcweir 
293cdf0e10cSrcweir /*
294cdf0e10cSrcweir    returns a list containing extensions with the same identifier from
295cdf0e10cSrcweir    all repositories (user, shared, bundled) If one repository does not
296cdf0e10cSrcweir    have this extension, then the list contains an empty Referenc. The list
297cdf0e10cSrcweir    is ordered according to the priority of the repostories:
298cdf0e10cSrcweir    1. user
299cdf0e10cSrcweir    2. shared
300cdf0e10cSrcweir    3. bundled
301cdf0e10cSrcweir 
302cdf0e10cSrcweir    The number of elements is always three, unless the number of repository
303cdf0e10cSrcweir    changes.
304cdf0e10cSrcweir  */
305cdf0e10cSrcweir ::std::list<Reference<deploy::XPackage> >
getExtensionsWithSameId(OUString const & identifier,OUString const & fileName,Reference<ucb::XCommandEnvironment> const &)306cdf0e10cSrcweir     ExtensionManager::getExtensionsWithSameId(
307cdf0e10cSrcweir         OUString const & identifier, OUString const & fileName,
308cdf0e10cSrcweir         Reference< ucb::XCommandEnvironment> const & /*xCmdEnv*/)
309cdf0e10cSrcweir 
310cdf0e10cSrcweir {
311cdf0e10cSrcweir     ::std::list<Reference<deploy::XPackage> > extensionList;
312cdf0e10cSrcweir     try
313cdf0e10cSrcweir     {   //will throw an exception if the extension does not exist
314cdf0e10cSrcweir         extensionList.push_back(getUserRepository()->getDeployedPackage(
315cdf0e10cSrcweir             identifier, fileName, Reference<ucb::XCommandEnvironment>()));
316*44416357SOliver-Rainer Wittmann     }
317*44416357SOliver-Rainer Wittmann     catch(lang::IllegalArgumentException &)
318*44416357SOliver-Rainer Wittmann     {
319*44416357SOliver-Rainer Wittmann         extensionList.push_back(Reference<deploy::XPackage>());
320*44416357SOliver-Rainer Wittmann     }
321*44416357SOliver-Rainer Wittmann     // catch deploy::DeploymentException and ucb::CommandFailedException to avoid unhandled exceptions causing crashes
322*44416357SOliver-Rainer Wittmann     catch( deploy::DeploymentException & )
323*44416357SOliver-Rainer Wittmann     {
324*44416357SOliver-Rainer Wittmann         extensionList.push_back(Reference<deploy::XPackage>());
325*44416357SOliver-Rainer Wittmann     }
326*44416357SOliver-Rainer Wittmann     catch( ucb::CommandFailedException & )
327cdf0e10cSrcweir     {
328cdf0e10cSrcweir         extensionList.push_back(Reference<deploy::XPackage>());
329cdf0e10cSrcweir     }
330*44416357SOliver-Rainer Wittmann 
331cdf0e10cSrcweir     try
332cdf0e10cSrcweir     {
333cdf0e10cSrcweir         extensionList.push_back(getSharedRepository()->getDeployedPackage(
334cdf0e10cSrcweir             identifier, fileName, Reference<ucb::XCommandEnvironment>()));
335*44416357SOliver-Rainer Wittmann     }
336*44416357SOliver-Rainer Wittmann     catch (lang::IllegalArgumentException &)
337*44416357SOliver-Rainer Wittmann     {
338*44416357SOliver-Rainer Wittmann         extensionList.push_back(Reference<deploy::XPackage>());
339*44416357SOliver-Rainer Wittmann     }
340*44416357SOliver-Rainer Wittmann     // catch deploy::DeploymentException and ucb::CommandFailedException to avoid unhandled exceptions causing crashes
341*44416357SOliver-Rainer Wittmann     catch( deploy::DeploymentException & )
342cdf0e10cSrcweir     {
343cdf0e10cSrcweir         extensionList.push_back(Reference<deploy::XPackage>());
344cdf0e10cSrcweir     }
345*44416357SOliver-Rainer Wittmann     catch( ucb::CommandFailedException & )
346*44416357SOliver-Rainer Wittmann     {
347*44416357SOliver-Rainer Wittmann         extensionList.push_back(Reference<deploy::XPackage>());
348*44416357SOliver-Rainer Wittmann     }
349*44416357SOliver-Rainer Wittmann 
350cdf0e10cSrcweir     try
351cdf0e10cSrcweir     {
352cdf0e10cSrcweir        extensionList.push_back(getBundledRepository()->getDeployedPackage(
353cdf0e10cSrcweir            identifier, fileName, Reference<ucb::XCommandEnvironment>()));
354*44416357SOliver-Rainer Wittmann     }
355*44416357SOliver-Rainer Wittmann     catch (lang::IllegalArgumentException &)
356cdf0e10cSrcweir     {
357cdf0e10cSrcweir         extensionList.push_back(Reference<deploy::XPackage>());
358cdf0e10cSrcweir     }
359*44416357SOliver-Rainer Wittmann     // catch deploy::DeploymentException and ucb::CommandFailedException to avoid unhandled exceptions causing crashes
360*44416357SOliver-Rainer Wittmann     catch( deploy::DeploymentException & )
361*44416357SOliver-Rainer Wittmann     {
362*44416357SOliver-Rainer Wittmann         extensionList.push_back(Reference<deploy::XPackage>());
363*44416357SOliver-Rainer Wittmann     }
364*44416357SOliver-Rainer Wittmann     catch( ucb::CommandFailedException & )
365*44416357SOliver-Rainer Wittmann     {
366*44416357SOliver-Rainer Wittmann         extensionList.push_back(Reference<deploy::XPackage>());
367*44416357SOliver-Rainer Wittmann     }
368*44416357SOliver-Rainer Wittmann 
369cdf0e10cSrcweir     OSL_ASSERT(extensionList.size() == 3);
370cdf0e10cSrcweir     return extensionList;
371cdf0e10cSrcweir }
372cdf0e10cSrcweir 
373cdf0e10cSrcweir uno::Sequence<Reference<deploy::XPackage> >
getExtensionsWithSameIdentifier(OUString const & identifier,OUString const & fileName,Reference<ucb::XCommandEnvironment> const & xCmdEnv)374cdf0e10cSrcweir ExtensionManager::getExtensionsWithSameIdentifier(
375cdf0e10cSrcweir         OUString const & identifier,
376cdf0e10cSrcweir         OUString const & fileName,
377cdf0e10cSrcweir         Reference< ucb::XCommandEnvironment> const & xCmdEnv )
378cdf0e10cSrcweir         throw (
379cdf0e10cSrcweir             deploy::DeploymentException,
380cdf0e10cSrcweir             ucb::CommandFailedException,
381cdf0e10cSrcweir             lang::IllegalArgumentException,
382cdf0e10cSrcweir             uno::RuntimeException)
383cdf0e10cSrcweir {
384cdf0e10cSrcweir     try
385cdf0e10cSrcweir     {
386cdf0e10cSrcweir         ::std::list<Reference<deploy::XPackage> > listExtensions =
387cdf0e10cSrcweir             getExtensionsWithSameId(
388cdf0e10cSrcweir                 identifier, fileName, xCmdEnv);
389cdf0e10cSrcweir         sal_Bool bHasExtension = false;
390cdf0e10cSrcweir 
391cdf0e10cSrcweir         //throw an IllegalArgumentException if there is no extension at all.
392cdf0e10cSrcweir         typedef  ::std::list<Reference<deploy::XPackage> >::const_iterator CIT;
393cdf0e10cSrcweir         for (CIT i = listExtensions.begin(); i != listExtensions.end(); i++)
394cdf0e10cSrcweir             bHasExtension |= i->is();
395cdf0e10cSrcweir         if (!bHasExtension)
396cdf0e10cSrcweir             throw lang::IllegalArgumentException(
397cdf0e10cSrcweir                 OUSTR("Could not find extension: ") + identifier + OUSTR(", ") + fileName,
398cdf0e10cSrcweir                 static_cast<cppu::OWeakObject*>(this), -1);
399cdf0e10cSrcweir 
400cdf0e10cSrcweir         return comphelper::containerToSequence<
401cdf0e10cSrcweir             Reference<deploy::XPackage>,
402cdf0e10cSrcweir             ::std::list<Reference<deploy::XPackage> >
403cdf0e10cSrcweir             > (listExtensions);
404cdf0e10cSrcweir     }
405cdf0e10cSrcweir     catch (deploy::DeploymentException & )
406cdf0e10cSrcweir     {
407cdf0e10cSrcweir         throw;
408cdf0e10cSrcweir     }
409cdf0e10cSrcweir     catch ( ucb::CommandFailedException & )
410cdf0e10cSrcweir     {
411cdf0e10cSrcweir         throw;
412cdf0e10cSrcweir     }
413cdf0e10cSrcweir     catch (lang::IllegalArgumentException &)
414cdf0e10cSrcweir     {
415cdf0e10cSrcweir         throw;
416cdf0e10cSrcweir     }
417cdf0e10cSrcweir     catch (...)
418cdf0e10cSrcweir     {
419cdf0e10cSrcweir         uno::Any exc = ::cppu::getCaughtException();
420cdf0e10cSrcweir         throw deploy::DeploymentException(
421cdf0e10cSrcweir             OUSTR("Extension Manager: exception during getExtensionsWithSameIdentifier"),
422cdf0e10cSrcweir             static_cast<OWeakObject*>(this), exc);
423cdf0e10cSrcweir     }
424cdf0e10cSrcweir }
425cdf0e10cSrcweir 
426cdf0e10cSrcweir 
427cdf0e10cSrcweir 
isUserDisabled(OUString const & identifier,OUString const & fileName)428cdf0e10cSrcweir bool ExtensionManager::isUserDisabled(
429cdf0e10cSrcweir     OUString const & identifier, OUString const & fileName)
430cdf0e10cSrcweir {
431cdf0e10cSrcweir 	::std::list<Reference<deploy::XPackage> > listExtensions;
432cdf0e10cSrcweir 
433cdf0e10cSrcweir 	try {
434cdf0e10cSrcweir 		listExtensions = getExtensionsWithSameId(identifier, fileName);
435cdf0e10cSrcweir 	} catch (lang::IllegalArgumentException & ) {
436cdf0e10cSrcweir 	}
437cdf0e10cSrcweir     OSL_ASSERT(listExtensions.size() == 3);
438cdf0e10cSrcweir 
439cdf0e10cSrcweir     return isUserDisabled( ::comphelper::containerToSequence<
440cdf0e10cSrcweir                            Reference<deploy::XPackage>,
441cdf0e10cSrcweir                            ::std::list<Reference<deploy::XPackage> >
442cdf0e10cSrcweir                            > (listExtensions));
443cdf0e10cSrcweir }
444cdf0e10cSrcweir 
isUserDisabled(uno::Sequence<Reference<deploy::XPackage>> const & seqExtSameId)445cdf0e10cSrcweir bool ExtensionManager::isUserDisabled(
446cdf0e10cSrcweir     uno::Sequence<Reference<deploy::XPackage> > const & seqExtSameId)
447cdf0e10cSrcweir {
448cdf0e10cSrcweir     OSL_ASSERT(seqExtSameId.getLength() == 3);
449cdf0e10cSrcweir     Reference<deploy::XPackage> const & userExtension = seqExtSameId[0];
450cdf0e10cSrcweir     if (userExtension.is())
451cdf0e10cSrcweir     {
452cdf0e10cSrcweir         beans::Optional<beans::Ambiguous<sal_Bool> > reg =
453cdf0e10cSrcweir             userExtension->isRegistered(Reference<task::XAbortChannel>(),
454cdf0e10cSrcweir                                         Reference<ucb::XCommandEnvironment>());
455cdf0e10cSrcweir         //If the value is ambiguous is than we assume that the extension
456cdf0e10cSrcweir         //is enabled, but something went wrong during enabling. We do not
457cdf0e10cSrcweir         //automatically disable user extensions.
458cdf0e10cSrcweir         if (reg.IsPresent &&
459cdf0e10cSrcweir             ! reg.Value.IsAmbiguous && ! reg.Value.Value)
460cdf0e10cSrcweir             return true;
461cdf0e10cSrcweir     }
462cdf0e10cSrcweir     return false;
463cdf0e10cSrcweir }
464cdf0e10cSrcweir 
465cdf0e10cSrcweir /*
466cdf0e10cSrcweir     This method determines the active extension (XPackage.registerPackage) with a
467cdf0e10cSrcweir     particular identifier.
468cdf0e10cSrcweir 
469cdf0e10cSrcweir     The parameter bUserDisabled determines if the user extension is disabled.
470cdf0e10cSrcweir 
471cdf0e10cSrcweir     When the user repository contains an extension with the given identifier and
472cdf0e10cSrcweir     it is not disabled by the user, then it is always registered.  Otherwise an
473cdf0e10cSrcweir     extension is only registered when there is no registered extension in one of
474cdf0e10cSrcweir     the repositories with a higher priority. That is, if the extension is from
475cdf0e10cSrcweir     the shared repository and an active extension with the same identifer is in
476cdf0e10cSrcweir     the user repository, then the extension is not registered. Similarly a
477cdf0e10cSrcweir     bundled extension is not registered if there is an active extension with the
478cdf0e10cSrcweir     same identifier in the shared or user repository.
479cdf0e10cSrcweir */
activateExtension(OUString const & identifier,OUString const & fileName,bool bUserDisabled,bool bStartup,Reference<task::XAbortChannel> const & xAbortChannel,Reference<ucb::XCommandEnvironment> const & xCmdEnv)480cdf0e10cSrcweir void ExtensionManager::activateExtension(
481cdf0e10cSrcweir     OUString const & identifier, OUString const & fileName,
482cdf0e10cSrcweir     bool bUserDisabled,
483cdf0e10cSrcweir     bool bStartup,
484cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
485cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv )
486cdf0e10cSrcweir {
487cdf0e10cSrcweir     ::std::list<Reference<deploy::XPackage> > listExtensions;
488cdf0e10cSrcweir 	try {
489cdf0e10cSrcweir         listExtensions = getExtensionsWithSameId(identifier, fileName);
490cdf0e10cSrcweir 	} catch (lang::IllegalArgumentException &) {
491cdf0e10cSrcweir 	}
492cdf0e10cSrcweir     OSL_ASSERT(listExtensions.size() == 3);
493cdf0e10cSrcweir 
494cdf0e10cSrcweir     activateExtension(
495cdf0e10cSrcweir         ::comphelper::containerToSequence<
496cdf0e10cSrcweir         Reference<deploy::XPackage>,
497cdf0e10cSrcweir         ::std::list<Reference<deploy::XPackage> >
498cdf0e10cSrcweir         > (listExtensions),
499cdf0e10cSrcweir         bUserDisabled, bStartup, xAbortChannel, xCmdEnv);
500cdf0e10cSrcweir 
501cdf0e10cSrcweir     fireModified();
502cdf0e10cSrcweir }
503cdf0e10cSrcweir 
activateExtension(uno::Sequence<Reference<deploy::XPackage>> const & seqExt,bool bUserDisabled,bool bStartup,Reference<task::XAbortChannel> const & xAbortChannel,Reference<ucb::XCommandEnvironment> const & xCmdEnv)504cdf0e10cSrcweir void ExtensionManager::activateExtension(
505cdf0e10cSrcweir     uno::Sequence<Reference<deploy::XPackage> > const & seqExt,
506cdf0e10cSrcweir     bool bUserDisabled,
507cdf0e10cSrcweir     bool bStartup,
508cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
509cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv )
510cdf0e10cSrcweir {
511cdf0e10cSrcweir     bool bActive = false;
512cdf0e10cSrcweir     sal_Int32 len = seqExt.getLength();
513cdf0e10cSrcweir     for (sal_Int32 i = 0; i < len; i++)
514cdf0e10cSrcweir     {
515cdf0e10cSrcweir         Reference<deploy::XPackage> const & aExt =  seqExt[i];
516cdf0e10cSrcweir         if (aExt.is())
517cdf0e10cSrcweir         {
518cdf0e10cSrcweir             //get the registration value of the current iteration
519cdf0e10cSrcweir             beans::Optional<beans::Ambiguous<sal_Bool> > optReg =
520cdf0e10cSrcweir                 aExt->isRegistered(xAbortChannel, xCmdEnv);
521cdf0e10cSrcweir             //If nothing can be registered then break
522cdf0e10cSrcweir             if (!optReg.IsPresent)
523cdf0e10cSrcweir                 break;
524cdf0e10cSrcweir 
525cdf0e10cSrcweir             //Check if this is a disabled user extension,
526cdf0e10cSrcweir             if (i == 0 && bUserDisabled)
527cdf0e10cSrcweir             {
528cdf0e10cSrcweir                    aExt->revokePackage(xAbortChannel, xCmdEnv);
529cdf0e10cSrcweir                    continue;
530cdf0e10cSrcweir             }
531cdf0e10cSrcweir 
532cdf0e10cSrcweir             //If we have already determined an active extension then we must
533cdf0e10cSrcweir             //make sure to unregister all extensions with the same id in
534cdf0e10cSrcweir             //repositories with a lower priority
535cdf0e10cSrcweir             if (bActive)
536cdf0e10cSrcweir             {
537cdf0e10cSrcweir                 aExt->revokePackage(xAbortChannel, xCmdEnv);
538cdf0e10cSrcweir             }
539cdf0e10cSrcweir             else
540cdf0e10cSrcweir             {
541cdf0e10cSrcweir                 //This is the first extension in the ordered list, which becomes
542cdf0e10cSrcweir                 //the active extension
543cdf0e10cSrcweir                 bActive = true;
544cdf0e10cSrcweir                 //Register if not already done.
545cdf0e10cSrcweir                 //reregister if the value is ambiguous, which indicates that
546cdf0e10cSrcweir                 //something went wrong during last registration.
547cdf0e10cSrcweir                 aExt->registerPackage(bStartup, xAbortChannel, xCmdEnv);
548cdf0e10cSrcweir             }
549cdf0e10cSrcweir         }
550cdf0e10cSrcweir     }
551cdf0e10cSrcweir }
552cdf0e10cSrcweir 
backupExtension(OUString const & identifier,OUString const & fileName,Reference<deploy::XPackageManager> const & xPackageManager,Reference<ucb::XCommandEnvironment> const & xCmdEnv)553cdf0e10cSrcweir Reference<deploy::XPackage> ExtensionManager::backupExtension(
554cdf0e10cSrcweir     OUString const & identifier, OUString const & fileName,
555cdf0e10cSrcweir     Reference<deploy::XPackageManager> const & xPackageManager,
556cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv )
557cdf0e10cSrcweir {
558cdf0e10cSrcweir     Reference<deploy::XPackage> xBackup;
559cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> tmpCmdEnv(
560cdf0e10cSrcweir         new TmpRepositoryCommandEnv(xCmdEnv->getInteractionHandler()));
561cdf0e10cSrcweir     Reference<deploy::XPackage> xOldExtension;
562cdf0e10cSrcweir     xOldExtension = xPackageManager->getDeployedPackage(
563cdf0e10cSrcweir             identifier, fileName, tmpCmdEnv);
564cdf0e10cSrcweir 
565cdf0e10cSrcweir     if (xOldExtension.is())
566cdf0e10cSrcweir     {
567cdf0e10cSrcweir         xBackup = getTmpRepository()->addPackage(
568cdf0e10cSrcweir             xOldExtension->getURL(), uno::Sequence<beans::NamedValue>(),
569cdf0e10cSrcweir             OUString(), Reference<task::XAbortChannel>(), tmpCmdEnv);
570cdf0e10cSrcweir 
571cdf0e10cSrcweir         OSL_ENSURE(xBackup.is(), "Failed to backup extension");
572cdf0e10cSrcweir     }
573cdf0e10cSrcweir     return xBackup;
574cdf0e10cSrcweir }
575cdf0e10cSrcweir 
576cdf0e10cSrcweir //The supported package types are actually determined by the registry. However
577cdf0e10cSrcweir //creating a registry
578cdf0e10cSrcweir //(desktop/source/deployment/registry/dp_registry.cxx:PackageRegistryImpl) will
579cdf0e10cSrcweir //create all the backends, so that the registry can obtain from them the package
580cdf0e10cSrcweir //types. Creating the registry will also set up the registry folder containing
581cdf0e10cSrcweir //all the subfolders for the respective backends.
582cdf0e10cSrcweir //Because all repositories support the same backends, we can just delegate this
583cdf0e10cSrcweir //call to one of the repositories.
584cdf0e10cSrcweir uno::Sequence< Reference<deploy::XPackageTypeInfo> >
getSupportedPackageTypes()585cdf0e10cSrcweir ExtensionManager::getSupportedPackageTypes()
586cdf0e10cSrcweir     throw (uno::RuntimeException)
587cdf0e10cSrcweir {
588cdf0e10cSrcweir     return getUserRepository()->getSupportedPackageTypes();
589cdf0e10cSrcweir }
590cdf0e10cSrcweir //Do some necessary checks and user interaction. This function does not
591cdf0e10cSrcweir //aquire the extension manager mutex and that mutex must not be aquired
592cdf0e10cSrcweir //when this function is called. doChecksForAddExtension does  synchronous
593cdf0e10cSrcweir //user interactions which may require aquiring the solar mutex.
594cdf0e10cSrcweir //Returns true if the extension can be installed.
doChecksForAddExtension(Reference<deploy::XPackageManager> const & xPackageMgr,uno::Sequence<beans::NamedValue> const & properties,css::uno::Reference<css::deployment::XPackage> const & xTmpExtension,Reference<task::XAbortChannel> const & xAbortChannel,Reference<ucb::XCommandEnvironment> const & xCmdEnv,Reference<deploy::XPackage> & out_existingExtension)595cdf0e10cSrcweir bool ExtensionManager::doChecksForAddExtension(
596cdf0e10cSrcweir     Reference<deploy::XPackageManager> const & xPackageMgr,
597cdf0e10cSrcweir     uno::Sequence<beans::NamedValue> const & properties,
598cdf0e10cSrcweir     css::uno::Reference<css::deployment::XPackage> const & xTmpExtension,
599cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
600cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv,
601cdf0e10cSrcweir     Reference<deploy::XPackage> & out_existingExtension )
602cdf0e10cSrcweir     throw (deploy::DeploymentException,
603cdf0e10cSrcweir            ucb::CommandFailedException,
604cdf0e10cSrcweir            ucb::CommandAbortedException,
605cdf0e10cSrcweir            lang::IllegalArgumentException,
606cdf0e10cSrcweir            uno::RuntimeException)
607cdf0e10cSrcweir {
608cdf0e10cSrcweir     try
609cdf0e10cSrcweir     {
610cdf0e10cSrcweir         Reference<deploy::XPackage> xOldExtension;
611cdf0e10cSrcweir         const OUString sIdentifier = dp_misc::getIdentifier(xTmpExtension);
612cdf0e10cSrcweir         const OUString sFileName = xTmpExtension->getName();
613cdf0e10cSrcweir         const OUString sDisplayName = xTmpExtension->getDisplayName();
614cdf0e10cSrcweir         const OUString sVersion = xTmpExtension->getVersion();
615cdf0e10cSrcweir 
616cdf0e10cSrcweir         try
617cdf0e10cSrcweir         {
618cdf0e10cSrcweir             xOldExtension = xPackageMgr->getDeployedPackage(
619cdf0e10cSrcweir                 sIdentifier, sFileName, xCmdEnv);
620cdf0e10cSrcweir             out_existingExtension = xOldExtension;
621cdf0e10cSrcweir         }
622cdf0e10cSrcweir         catch (lang::IllegalArgumentException &)
623cdf0e10cSrcweir         {
624cdf0e10cSrcweir         }
625cdf0e10cSrcweir         bool bCanInstall = false;
626cdf0e10cSrcweir 
627cdf0e10cSrcweir         //This part is not guarded against other threads removing, adding, disabling ...
628cdf0e10cSrcweir         //etc. the same extension.
629cdf0e10cSrcweir         //checkInstall is safe because it notifies the user if the extension is not yet
630cdf0e10cSrcweir         //installed in the same repository. Because addExtension has its own guard
631cdf0e10cSrcweir         //(m_addMutex), another thread cannot add the extension in the meantime.
632cdf0e10cSrcweir         //checkUpdate is called if the same extension exists in the same
633cdf0e10cSrcweir         //repository. The user is asked if they want to replace it.  Another
634cdf0e10cSrcweir         //thread
635cdf0e10cSrcweir         //could already remove the extension. So asking the user was not
636cdf0e10cSrcweir         //necessary. No harm is done. The other thread may also ask the user
637cdf0e10cSrcweir         //if he wants to remove the extension. This depends on the
638cdf0e10cSrcweir         //XCommandEnvironment which it passes to removeExtension.
639cdf0e10cSrcweir         if (xOldExtension.is())
640cdf0e10cSrcweir         {
641cdf0e10cSrcweir             //throws a CommandFailedException if the user cancels
642cdf0e10cSrcweir             //the action.
643cdf0e10cSrcweir             checkUpdate(sVersion, sDisplayName,xOldExtension, xCmdEnv);
644cdf0e10cSrcweir         }
645cdf0e10cSrcweir         else
646cdf0e10cSrcweir         {
647cdf0e10cSrcweir             //throws a CommandFailedException if the user cancels
648cdf0e10cSrcweir             //the action.
649cdf0e10cSrcweir             checkInstall(sDisplayName, xCmdEnv);
650cdf0e10cSrcweir         }
651cdf0e10cSrcweir         //Prevent showing the license if requested.
652cdf0e10cSrcweir         Reference<ucb::XCommandEnvironment> _xCmdEnv(xCmdEnv);
653cdf0e10cSrcweir         ExtensionProperties props(OUString(), properties, Reference<ucb::XCommandEnvironment>());
654cdf0e10cSrcweir 
655cdf0e10cSrcweir         dp_misc::DescriptionInfoset info(dp_misc::getDescriptionInfoset(xTmpExtension->getURL()));
656cdf0e10cSrcweir         const ::boost::optional<dp_misc::SimpleLicenseAttributes> licenseAttributes =
657cdf0e10cSrcweir             info.getSimpleLicenseAttributes();
658cdf0e10cSrcweir 
659cdf0e10cSrcweir         if (licenseAttributes && licenseAttributes->suppressIfRequired
660cdf0e10cSrcweir             && props.isSuppressedLicense())
661cdf0e10cSrcweir             _xCmdEnv = Reference<ucb::XCommandEnvironment>(
662cdf0e10cSrcweir                 new NoLicenseCommandEnv(xCmdEnv->getInteractionHandler()));
663cdf0e10cSrcweir 
664cdf0e10cSrcweir         bCanInstall = xTmpExtension->checkPrerequisites(
665cdf0e10cSrcweir             xAbortChannel, _xCmdEnv, xOldExtension.is() || props.isExtensionUpdate()) == 0 ? true : false;
666cdf0e10cSrcweir 
667cdf0e10cSrcweir         return bCanInstall;
668cdf0e10cSrcweir     }
669cdf0e10cSrcweir     catch (deploy::DeploymentException& ) {
670cdf0e10cSrcweir         throw;
671cdf0e10cSrcweir     } catch (ucb::CommandFailedException & ) {
672cdf0e10cSrcweir         throw;
673cdf0e10cSrcweir     } catch (ucb::CommandAbortedException & ) {
674cdf0e10cSrcweir         throw;
675cdf0e10cSrcweir     } catch (lang::IllegalArgumentException &) {
676cdf0e10cSrcweir         throw;
677cdf0e10cSrcweir     } catch (uno::RuntimeException &) {
678cdf0e10cSrcweir         throw;
679cdf0e10cSrcweir     } catch (uno::Exception &) {
680cdf0e10cSrcweir         uno::Any excOccurred = ::cppu::getCaughtException();
681cdf0e10cSrcweir         deploy::DeploymentException exc(
682cdf0e10cSrcweir             OUSTR("Extension Manager: exception in doChecksForAddExtension"),
683cdf0e10cSrcweir             static_cast<OWeakObject*>(this), excOccurred);
684cdf0e10cSrcweir         throw exc;
685cdf0e10cSrcweir     } catch (...) {
686cdf0e10cSrcweir         throw uno::RuntimeException(
687cdf0e10cSrcweir             OUSTR("Extension Manager: unexpected exception in doChecksForAddExtension"),
688cdf0e10cSrcweir             static_cast<OWeakObject*>(this));
689cdf0e10cSrcweir     }
690cdf0e10cSrcweir }
691cdf0e10cSrcweir 
692cdf0e10cSrcweir // Only add to shared and user repository
addExtension(OUString const & url,uno::Sequence<beans::NamedValue> const & properties,OUString const & repository,Reference<task::XAbortChannel> const & xAbortChannel,Reference<ucb::XCommandEnvironment> const & xCmdEnv)693cdf0e10cSrcweir Reference<deploy::XPackage> ExtensionManager::addExtension(
694cdf0e10cSrcweir     OUString const & url, uno::Sequence<beans::NamedValue> const & properties,
695cdf0e10cSrcweir     OUString const & repository,
696cdf0e10cSrcweir         Reference<task::XAbortChannel> const & xAbortChannel,
697cdf0e10cSrcweir         Reference<ucb::XCommandEnvironment> const & xCmdEnv )
698cdf0e10cSrcweir         throw (deploy::DeploymentException,
699cdf0e10cSrcweir                ucb::CommandFailedException,
700cdf0e10cSrcweir                ucb::CommandAbortedException,
701cdf0e10cSrcweir                lang::IllegalArgumentException,
702cdf0e10cSrcweir                uno::RuntimeException)
703cdf0e10cSrcweir {
704cdf0e10cSrcweir     Reference<deploy::XPackage> xNewExtension;
705cdf0e10cSrcweir     //Determine the repository to use
706cdf0e10cSrcweir     Reference<deploy::XPackageManager> xPackageManager;
707cdf0e10cSrcweir     if (repository.equals(OUSTR("user")))
708cdf0e10cSrcweir         xPackageManager = getUserRepository();
709cdf0e10cSrcweir     else if (repository.equals(OUSTR("shared")))
710cdf0e10cSrcweir         xPackageManager = getSharedRepository();
711cdf0e10cSrcweir     else
712cdf0e10cSrcweir         throw lang::IllegalArgumentException(
713cdf0e10cSrcweir             OUSTR("No valid repository name provided."),
714cdf0e10cSrcweir             static_cast<cppu::OWeakObject*>(this), 0);
715cdf0e10cSrcweir     //We must make sure that the xTmpExtension is not create twice, because this
716cdf0e10cSrcweir     //would remove the first one.
717cdf0e10cSrcweir     ::osl::MutexGuard addGuard(m_addMutex);
718cdf0e10cSrcweir 
719cdf0e10cSrcweir     Reference<deploy::XPackage> xTmpExtension =
720cdf0e10cSrcweir         getTempExtension(url, xAbortChannel, xCmdEnv);
721cdf0e10cSrcweir     //Make sure the extension is removed from the tmp repository in case
722cdf0e10cSrcweir     //of an exception
723cdf0e10cSrcweir     ExtensionRemoveGuard tmpExtensionRemoveGuard(xTmpExtension, getTmpRepository());
7247bc7b19fSMichael Stahl     ExtensionRemoveGuard bakExtensionRemoveGuard;
725cdf0e10cSrcweir     const OUString sIdentifier = dp_misc::getIdentifier(xTmpExtension);
726cdf0e10cSrcweir     const OUString sFileName = xTmpExtension->getName();
727cdf0e10cSrcweir     Reference<deploy::XPackage> xOldExtension;
728cdf0e10cSrcweir     Reference<deploy::XPackage> xExtensionBackup;
729cdf0e10cSrcweir 
730cdf0e10cSrcweir     uno::Any excOccurred2;
731cdf0e10cSrcweir     bool bUserDisabled = false;
732cdf0e10cSrcweir     bool bCanInstall = doChecksForAddExtension(
733cdf0e10cSrcweir         xPackageManager,
734cdf0e10cSrcweir         properties,
735cdf0e10cSrcweir         xTmpExtension,
736cdf0e10cSrcweir         xAbortChannel,
737cdf0e10cSrcweir         xCmdEnv,
738cdf0e10cSrcweir         xOldExtension );
739cdf0e10cSrcweir 
740cdf0e10cSrcweir     {
741cdf0e10cSrcweir         // In this garded section (getMutex) we must not use the argument xCmdEnv
742cdf0e10cSrcweir         // because it may bring up dialogs (XInteractionHandler::handle) this
743cdf0e10cSrcweir         //may potententially deadlock. See issue
744cdf0e10cSrcweir         //http://qa.openoffice.org/issues/show_bug.cgi?id=114933
745cdf0e10cSrcweir         //By not providing xCmdEnv the underlying APIs will throw an exception if
746cdf0e10cSrcweir         //the XInteractionRequest cannot be handled
747cdf0e10cSrcweir         ::osl::MutexGuard guard(getMutex());
748cdf0e10cSrcweir 
749cdf0e10cSrcweir         if (bCanInstall)
750cdf0e10cSrcweir         {
751cdf0e10cSrcweir             try
752cdf0e10cSrcweir             {
753cdf0e10cSrcweir                 bUserDisabled = isUserDisabled(sIdentifier, sFileName);
754cdf0e10cSrcweir                 if (xOldExtension.is())
755cdf0e10cSrcweir                 {
756cdf0e10cSrcweir                     try
757cdf0e10cSrcweir                     {
758cdf0e10cSrcweir                         xOldExtension->revokePackage(
759cdf0e10cSrcweir                             xAbortChannel, Reference<ucb::XCommandEnvironment>());
760cdf0e10cSrcweir                         //save the old user extension in case the user aborts
7617bc7b19fSMichael Stahl                         xExtensionBackup = getBakRepository()->importExtension(
762cdf0e10cSrcweir                             xOldExtension, Reference<task::XAbortChannel>(),
763cdf0e10cSrcweir                             Reference<ucb::XCommandEnvironment>());
7647bc7b19fSMichael Stahl                         bakExtensionRemoveGuard.set(xExtensionBackup, getBakRepository());
765cdf0e10cSrcweir                     }
766cdf0e10cSrcweir                     catch (lang::DisposedException &)
767cdf0e10cSrcweir                     {
768cdf0e10cSrcweir                         //Another thread might have removed the extension meanwhile
769cdf0e10cSrcweir                     }
770cdf0e10cSrcweir                 }
771cdf0e10cSrcweir                 //check again dependencies but prevent user interaction,
772cdf0e10cSrcweir                 //We can disregard the license, because the user must have already
773cdf0e10cSrcweir                 //accepted it, whe we called checkPrerequisites the first time
774cdf0e10cSrcweir                 SilentCheckPrerequisitesCommandEnv * pSilentCommandEnv =
775cdf0e10cSrcweir                     new SilentCheckPrerequisitesCommandEnv();
776cdf0e10cSrcweir                 Reference<ucb::XCommandEnvironment> silentCommandEnv(pSilentCommandEnv);
777cdf0e10cSrcweir 
778cdf0e10cSrcweir                 sal_Int32 failedPrereq = xTmpExtension->checkPrerequisites(
779cdf0e10cSrcweir                     xAbortChannel, silentCommandEnv, true);
780cdf0e10cSrcweir                 if (failedPrereq == 0)
781cdf0e10cSrcweir                 {
782cdf0e10cSrcweir                     xNewExtension = xPackageManager->addPackage(
783cdf0e10cSrcweir                         url, properties, OUString(), xAbortChannel,
784cdf0e10cSrcweir                         Reference<ucb::XCommandEnvironment>());
785cdf0e10cSrcweir                     //If we add a user extension and there is already one which was
786cdf0e10cSrcweir                     //disabled by a user, then the newly installed one is enabled. If we
787cdf0e10cSrcweir                     //add to another repository then the user extension remains
788cdf0e10cSrcweir                     //disabled.
789cdf0e10cSrcweir                     bool bUserDisabled2 = bUserDisabled;
790cdf0e10cSrcweir                     if (repository.equals(OUSTR("user")))
791cdf0e10cSrcweir                         bUserDisabled2 = false;
792cdf0e10cSrcweir 
793cdf0e10cSrcweir                     ::rtl::OUString const name(xNewExtension->getName());
794cdf0e10cSrcweir                     activateExtension(
795cdf0e10cSrcweir                         dp_misc::getIdentifier(xNewExtension),
796cdf0e10cSrcweir                         name, bUserDisabled2, false, xAbortChannel,
797cdf0e10cSrcweir                         Reference<ucb::XCommandEnvironment>());
798cdf0e10cSrcweir                 }
799cdf0e10cSrcweir                 else
800cdf0e10cSrcweir                 {
801cdf0e10cSrcweir                     if (pSilentCommandEnv->m_Exception.hasValue())
802cdf0e10cSrcweir                         ::cppu::throwException(pSilentCommandEnv->m_Exception);
803cdf0e10cSrcweir                     else if ( pSilentCommandEnv->m_UnknownException.hasValue())
804cdf0e10cSrcweir                         ::cppu::throwException(pSilentCommandEnv->m_UnknownException);
805cdf0e10cSrcweir                     else
806cdf0e10cSrcweir                         throw deploy::DeploymentException (
807cdf0e10cSrcweir                             OUSTR("Extension Manager: exception during addExtension, ckeckPrerequisites failed"),
808cdf0e10cSrcweir                             static_cast<OWeakObject*>(this), uno::Any());
809cdf0e10cSrcweir                 }
810cdf0e10cSrcweir             }
811cdf0e10cSrcweir             catch (deploy::DeploymentException& ) {
812cdf0e10cSrcweir                 excOccurred2 = ::cppu::getCaughtException();
813cdf0e10cSrcweir             } catch (ucb::CommandFailedException & ) {
814cdf0e10cSrcweir                 excOccurred2 = ::cppu::getCaughtException();
815cdf0e10cSrcweir             } catch (ucb::CommandAbortedException & ) {
816cdf0e10cSrcweir                 excOccurred2 = ::cppu::getCaughtException();
817cdf0e10cSrcweir             } catch (lang::IllegalArgumentException &) {
818cdf0e10cSrcweir                 excOccurred2 = ::cppu::getCaughtException();
819cdf0e10cSrcweir             } catch (uno::RuntimeException &) {
820cdf0e10cSrcweir                 excOccurred2 = ::cppu::getCaughtException();
821cdf0e10cSrcweir             } catch (...) {
822cdf0e10cSrcweir                 excOccurred2 = ::cppu::getCaughtException();
823cdf0e10cSrcweir                 deploy::DeploymentException exc(
824cdf0e10cSrcweir                     OUSTR("Extension Manager: exception during addExtension, url: ")
825cdf0e10cSrcweir                     + url, static_cast<OWeakObject*>(this), excOccurred2);
826cdf0e10cSrcweir                 excOccurred2 <<= exc;
827cdf0e10cSrcweir             }
828cdf0e10cSrcweir         }
829cdf0e10cSrcweir 
830cdf0e10cSrcweir         if (excOccurred2.hasValue())
831cdf0e10cSrcweir         {
832cdf0e10cSrcweir             //It does not matter what exception is thrown. We try to
833cdf0e10cSrcweir             //recover the original status.
834cdf0e10cSrcweir             //If the user aborted installation then a ucb::CommandAbortedException
835cdf0e10cSrcweir             //is thrown.
836cdf0e10cSrcweir             //Use a private AbortChannel so the user cannot interrupt.
837cdf0e10cSrcweir             try
838cdf0e10cSrcweir             {
839cdf0e10cSrcweir                 if (xExtensionBackup.is())
840cdf0e10cSrcweir                 {
841cdf0e10cSrcweir                     Reference<deploy::XPackage> xRestored =
842cdf0e10cSrcweir                         xPackageManager->importExtension(
843cdf0e10cSrcweir                             xExtensionBackup, Reference<task::XAbortChannel>(),
844cdf0e10cSrcweir                             Reference<ucb::XCommandEnvironment>());
845cdf0e10cSrcweir                 }
846cdf0e10cSrcweir                 activateExtension(
847cdf0e10cSrcweir                     sIdentifier, sFileName, bUserDisabled, false,
848cdf0e10cSrcweir                     Reference<task::XAbortChannel>(), Reference<ucb::XCommandEnvironment>());
849cdf0e10cSrcweir             }
850cdf0e10cSrcweir             catch (...)
851cdf0e10cSrcweir             {
852cdf0e10cSrcweir             }
853cdf0e10cSrcweir             ::cppu::throwException(excOccurred2);
854cdf0e10cSrcweir         }
855cdf0e10cSrcweir     } // leaving the garded section (getMutex())
856cdf0e10cSrcweir 
857cdf0e10cSrcweir     try
858cdf0e10cSrcweir     {
859cdf0e10cSrcweir         fireModified();
860cdf0e10cSrcweir 
861cdf0e10cSrcweir     }catch (deploy::DeploymentException& ) {
862cdf0e10cSrcweir         throw;
863cdf0e10cSrcweir     } catch (ucb::CommandFailedException & ) {
864cdf0e10cSrcweir         throw;
865cdf0e10cSrcweir     } catch (ucb::CommandAbortedException & ) {
866cdf0e10cSrcweir         throw;
867cdf0e10cSrcweir     } catch (lang::IllegalArgumentException &) {
868cdf0e10cSrcweir         throw;
869cdf0e10cSrcweir     } catch (uno::RuntimeException &) {
870cdf0e10cSrcweir         throw;
871cdf0e10cSrcweir     } catch (uno::Exception &) {
872cdf0e10cSrcweir         uno::Any excOccurred = ::cppu::getCaughtException();
873cdf0e10cSrcweir         deploy::DeploymentException exc(
874cdf0e10cSrcweir             OUSTR("Extension Manager: exception in doChecksForAddExtension"),
875cdf0e10cSrcweir             static_cast<OWeakObject*>(this), excOccurred);
876cdf0e10cSrcweir         throw exc;
877cdf0e10cSrcweir     } catch (...) {
878cdf0e10cSrcweir         throw uno::RuntimeException(
879cdf0e10cSrcweir             OUSTR("Extension Manager: unexpected exception in doChecksForAddExtension"),
880cdf0e10cSrcweir             static_cast<OWeakObject*>(this));
881cdf0e10cSrcweir     }
882cdf0e10cSrcweir 
883cdf0e10cSrcweir     return xNewExtension;
884cdf0e10cSrcweir }
885cdf0e10cSrcweir 
removeExtension(OUString const & identifier,OUString const & fileName,OUString const & repository,Reference<task::XAbortChannel> const & xAbortChannel,Reference<ucb::XCommandEnvironment> const & xCmdEnv)886cdf0e10cSrcweir void ExtensionManager::removeExtension(
887cdf0e10cSrcweir     OUString const & identifier, OUString const & fileName,
888cdf0e10cSrcweir     OUString const & repository,
889cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
890cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv )
891cdf0e10cSrcweir     throw (deploy::DeploymentException,
892cdf0e10cSrcweir            ucb::CommandFailedException,
893cdf0e10cSrcweir            ucb::CommandAbortedException,
894cdf0e10cSrcweir            lang::IllegalArgumentException,
895cdf0e10cSrcweir            uno::RuntimeException)
896cdf0e10cSrcweir {
897cdf0e10cSrcweir     uno::Any excOccurred1;
898cdf0e10cSrcweir     Reference<deploy::XPackage> xExtensionBackup;
899cdf0e10cSrcweir     Reference<deploy::XPackageManager> xPackageManager;
900cdf0e10cSrcweir     bool bUserDisabled = false;
901cdf0e10cSrcweir     ::osl::MutexGuard guard(getMutex());
902cdf0e10cSrcweir     try
903cdf0e10cSrcweir     {
904cdf0e10cSrcweir //Determine the repository to use
905cdf0e10cSrcweir         if (repository.equals(OUSTR("user")))
906cdf0e10cSrcweir             xPackageManager = getUserRepository();
907cdf0e10cSrcweir         else if (repository.equals(OUSTR("shared")))
908cdf0e10cSrcweir             xPackageManager = getSharedRepository();
909cdf0e10cSrcweir         else
910cdf0e10cSrcweir             throw lang::IllegalArgumentException(
911cdf0e10cSrcweir                 OUSTR("No valid repository name provided."),
912cdf0e10cSrcweir                 static_cast<cppu::OWeakObject*>(this), 0);
913cdf0e10cSrcweir 
914cdf0e10cSrcweir         bUserDisabled = isUserDisabled(identifier, fileName);
915cdf0e10cSrcweir         //Backup the extension, in case the user cancels the action
916cdf0e10cSrcweir         xExtensionBackup = backupExtension(
917cdf0e10cSrcweir             identifier, fileName, xPackageManager, xCmdEnv);
918cdf0e10cSrcweir 
919cdf0e10cSrcweir         //revoke the extension if it is active
920cdf0e10cSrcweir         Reference<deploy::XPackage> xOldExtension =
921cdf0e10cSrcweir             xPackageManager->getDeployedPackage(
922cdf0e10cSrcweir                 identifier, fileName, xCmdEnv);
923cdf0e10cSrcweir         xOldExtension->revokePackage(xAbortChannel, xCmdEnv);
924cdf0e10cSrcweir 
925cdf0e10cSrcweir         xPackageManager->removePackage(
926cdf0e10cSrcweir             identifier, fileName, xAbortChannel, xCmdEnv);
927cdf0e10cSrcweir         activateExtension(identifier, fileName, bUserDisabled, false,
928cdf0e10cSrcweir                           xAbortChannel, xCmdEnv);
929cdf0e10cSrcweir         fireModified();
930cdf0e10cSrcweir     }
931cdf0e10cSrcweir     catch (deploy::DeploymentException& ) {
932cdf0e10cSrcweir         excOccurred1 = ::cppu::getCaughtException();
933cdf0e10cSrcweir     } catch (ucb::CommandFailedException & ) {
934cdf0e10cSrcweir         excOccurred1 = ::cppu::getCaughtException();
935cdf0e10cSrcweir     } catch (ucb::CommandAbortedException & ) {
936cdf0e10cSrcweir         excOccurred1 = ::cppu::getCaughtException();
937cdf0e10cSrcweir     } catch (lang::IllegalArgumentException &) {
938cdf0e10cSrcweir         excOccurred1 = ::cppu::getCaughtException();
939cdf0e10cSrcweir     } catch (uno::RuntimeException &) {
940cdf0e10cSrcweir         excOccurred1 = ::cppu::getCaughtException();
941cdf0e10cSrcweir     } catch (...) {
942cdf0e10cSrcweir         excOccurred1 = ::cppu::getCaughtException();
943cdf0e10cSrcweir         deploy::DeploymentException exc(
944cdf0e10cSrcweir             OUSTR("Extension Manager: exception during removeEtension"),
945cdf0e10cSrcweir             static_cast<OWeakObject*>(this), excOccurred1);
946cdf0e10cSrcweir         excOccurred1 <<= exc;
947cdf0e10cSrcweir     }
948cdf0e10cSrcweir 
949cdf0e10cSrcweir     if (excOccurred1.hasValue())
950cdf0e10cSrcweir     {
951cdf0e10cSrcweir         //User aborted installation, restore the previous situation.
952cdf0e10cSrcweir         //Use a private AbortChannel so the user cannot interrupt.
953cdf0e10cSrcweir         try
954cdf0e10cSrcweir         {
955cdf0e10cSrcweir             Reference<ucb::XCommandEnvironment> tmpCmdEnv(
956cdf0e10cSrcweir                 new TmpRepositoryCommandEnv(xCmdEnv->getInteractionHandler()));
957cdf0e10cSrcweir             if (xExtensionBackup.is())
958cdf0e10cSrcweir             {
959cdf0e10cSrcweir                 Reference<deploy::XPackage> xRestored =
960cdf0e10cSrcweir                     xPackageManager->importExtension(
961cdf0e10cSrcweir                         xExtensionBackup, Reference<task::XAbortChannel>(),
962cdf0e10cSrcweir                         tmpCmdEnv);
963cdf0e10cSrcweir                 activateExtension(
964cdf0e10cSrcweir                     identifier, fileName, bUserDisabled, false,
965cdf0e10cSrcweir                     Reference<task::XAbortChannel>(),
966cdf0e10cSrcweir                     tmpCmdEnv);
967cdf0e10cSrcweir 
968cdf0e10cSrcweir                 getTmpRepository()->removePackage(
969cdf0e10cSrcweir                     dp_misc::getIdentifier(xExtensionBackup),
970cdf0e10cSrcweir                     xExtensionBackup->getName(), xAbortChannel, xCmdEnv);
971cdf0e10cSrcweir                 fireModified();
972cdf0e10cSrcweir             }
973cdf0e10cSrcweir         }
974cdf0e10cSrcweir         catch (...)
975cdf0e10cSrcweir         {
976cdf0e10cSrcweir         }
977cdf0e10cSrcweir         ::cppu::throwException(excOccurred1);
978cdf0e10cSrcweir     }
979cdf0e10cSrcweir 
980cdf0e10cSrcweir     if (xExtensionBackup.is())
981cdf0e10cSrcweir         getTmpRepository()->removePackage(
982cdf0e10cSrcweir             dp_misc::getIdentifier(xExtensionBackup),
983cdf0e10cSrcweir             xExtensionBackup->getName(), xAbortChannel, xCmdEnv);
984cdf0e10cSrcweir }
985cdf0e10cSrcweir 
986cdf0e10cSrcweir // Only enable extensions from shared and user repository
enableExtension(Reference<deploy::XPackage> const & extension,Reference<task::XAbortChannel> const & xAbortChannel,Reference<ucb::XCommandEnvironment> const & xCmdEnv)987cdf0e10cSrcweir void ExtensionManager::enableExtension(
988cdf0e10cSrcweir     Reference<deploy::XPackage> const & extension,
989cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
990cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv)
991cdf0e10cSrcweir     throw (deploy::DeploymentException,
992cdf0e10cSrcweir         ucb::CommandFailedException,
993cdf0e10cSrcweir         ucb::CommandAbortedException,
994cdf0e10cSrcweir         lang::IllegalArgumentException,
995cdf0e10cSrcweir         uno::RuntimeException)
996cdf0e10cSrcweir {
997cdf0e10cSrcweir     ::osl::MutexGuard guard(getMutex());
998cdf0e10cSrcweir     bool bUserDisabled = false;
999cdf0e10cSrcweir     uno::Any excOccurred;
1000cdf0e10cSrcweir     try
1001cdf0e10cSrcweir     {
1002cdf0e10cSrcweir         if (!extension.is())
1003cdf0e10cSrcweir             return;
1004cdf0e10cSrcweir         OUString repository = extension->getRepositoryName();
1005cdf0e10cSrcweir         if (!repository.equals(OUSTR("user")))
1006cdf0e10cSrcweir             throw lang::IllegalArgumentException(
1007cdf0e10cSrcweir                 OUSTR("No valid repository name provided."),
1008cdf0e10cSrcweir                 static_cast<cppu::OWeakObject*>(this), 0);
1009cdf0e10cSrcweir 
1010cdf0e10cSrcweir         bUserDisabled = isUserDisabled(dp_misc::getIdentifier(extension),
1011cdf0e10cSrcweir                                        extension->getName());
1012cdf0e10cSrcweir 
1013cdf0e10cSrcweir         activateExtension(dp_misc::getIdentifier(extension),
1014cdf0e10cSrcweir                           extension->getName(), false, false,
1015cdf0e10cSrcweir                           xAbortChannel, xCmdEnv);
1016cdf0e10cSrcweir     }
1017cdf0e10cSrcweir     catch (deploy::DeploymentException& ) {
1018cdf0e10cSrcweir         excOccurred = ::cppu::getCaughtException();
1019cdf0e10cSrcweir     } catch (ucb::CommandFailedException & ) {
1020cdf0e10cSrcweir         excOccurred = ::cppu::getCaughtException();
1021cdf0e10cSrcweir     } catch (ucb::CommandAbortedException & ) {
1022cdf0e10cSrcweir         excOccurred = ::cppu::getCaughtException();
1023cdf0e10cSrcweir     } catch (lang::IllegalArgumentException &) {
1024cdf0e10cSrcweir         excOccurred = ::cppu::getCaughtException();
1025cdf0e10cSrcweir     } catch (uno::RuntimeException &) {
1026cdf0e10cSrcweir         excOccurred = ::cppu::getCaughtException();
1027cdf0e10cSrcweir     } catch (...) {
1028cdf0e10cSrcweir         excOccurred = ::cppu::getCaughtException();
1029cdf0e10cSrcweir         deploy::DeploymentException exc(
1030cdf0e10cSrcweir             OUSTR("Extension Manager: exception during enableExtension"),
1031cdf0e10cSrcweir             static_cast<OWeakObject*>(this), excOccurred);
1032cdf0e10cSrcweir         excOccurred <<= exc;
1033cdf0e10cSrcweir     }
1034cdf0e10cSrcweir 
1035cdf0e10cSrcweir     if (excOccurred.hasValue())
1036cdf0e10cSrcweir     {
1037cdf0e10cSrcweir         try
1038cdf0e10cSrcweir         {
1039cdf0e10cSrcweir             activateExtension(dp_misc::getIdentifier(extension),
1040cdf0e10cSrcweir                               extension->getName(), bUserDisabled, false,
1041cdf0e10cSrcweir                               xAbortChannel, xCmdEnv);
1042cdf0e10cSrcweir         }
1043cdf0e10cSrcweir         catch (...)
1044cdf0e10cSrcweir         {
1045cdf0e10cSrcweir         }
1046cdf0e10cSrcweir         ::cppu::throwException(excOccurred);
1047cdf0e10cSrcweir     }
1048cdf0e10cSrcweir }
1049cdf0e10cSrcweir 
1050cdf0e10cSrcweir /**
1051cdf0e10cSrcweir  */
checkPrerequisitesAndEnable(Reference<deploy::XPackage> const & extension,Reference<task::XAbortChannel> const & xAbortChannel,Reference<ucb::XCommandEnvironment> const & xCmdEnv)1052cdf0e10cSrcweir sal_Int32 ExtensionManager::checkPrerequisitesAndEnable(
1053cdf0e10cSrcweir     Reference<deploy::XPackage> const & extension,
1054cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
1055cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv)
1056cdf0e10cSrcweir     throw (deploy::DeploymentException,
1057cdf0e10cSrcweir         ucb::CommandFailedException,
1058cdf0e10cSrcweir         ucb::CommandAbortedException,
1059cdf0e10cSrcweir         lang::IllegalArgumentException,
1060cdf0e10cSrcweir         uno::RuntimeException)
1061cdf0e10cSrcweir {
1062cdf0e10cSrcweir     try
1063cdf0e10cSrcweir     {
1064cdf0e10cSrcweir         if (!extension.is())
1065cdf0e10cSrcweir             return 0;
1066cdf0e10cSrcweir         ::osl::MutexGuard guard(getMutex());
1067cdf0e10cSrcweir         sal_Int32 ret = 0;
1068cdf0e10cSrcweir         Reference<deploy::XPackageManager> mgr =
1069cdf0e10cSrcweir             getPackageManager(extension->getRepositoryName());
1070cdf0e10cSrcweir         ret = mgr->checkPrerequisites(extension, xAbortChannel, xCmdEnv);
1071cdf0e10cSrcweir         if (ret)
1072cdf0e10cSrcweir         {
1073cdf0e10cSrcweir             //There are some unfulfilled prerequisites, try to revoke
1074cdf0e10cSrcweir             extension->revokePackage(xAbortChannel, xCmdEnv);
1075cdf0e10cSrcweir         }
1076cdf0e10cSrcweir         const OUString id(dp_misc::getIdentifier(extension));
1077cdf0e10cSrcweir         activateExtension(id, extension->getName(),
1078cdf0e10cSrcweir                           isUserDisabled(id, extension->getName()), false,
1079cdf0e10cSrcweir                           xAbortChannel, xCmdEnv);
1080cdf0e10cSrcweir         return ret;
1081cdf0e10cSrcweir     }
1082cdf0e10cSrcweir     catch (deploy::DeploymentException& ) {
1083cdf0e10cSrcweir         throw;
1084cdf0e10cSrcweir     } catch (ucb::CommandFailedException & ) {
1085cdf0e10cSrcweir         throw;
1086cdf0e10cSrcweir     } catch (ucb::CommandAbortedException & ) {
1087cdf0e10cSrcweir         throw;
1088cdf0e10cSrcweir     } catch (lang::IllegalArgumentException &) {
1089cdf0e10cSrcweir         throw;
1090cdf0e10cSrcweir     } catch (uno::RuntimeException &) {
1091cdf0e10cSrcweir         throw;
1092cdf0e10cSrcweir     } catch (...) {
1093cdf0e10cSrcweir         uno::Any excOccurred = ::cppu::getCaughtException();
1094cdf0e10cSrcweir         deploy::DeploymentException exc(
1095cdf0e10cSrcweir             OUSTR("Extension Manager: exception during disableExtension"),
1096cdf0e10cSrcweir             static_cast<OWeakObject*>(this), excOccurred);
1097cdf0e10cSrcweir         throw exc;
1098cdf0e10cSrcweir     }
1099cdf0e10cSrcweir }
1100cdf0e10cSrcweir 
1101cdf0e10cSrcweir 
disableExtension(Reference<deploy::XPackage> const & extension,Reference<task::XAbortChannel> const & xAbortChannel,Reference<ucb::XCommandEnvironment> const & xCmdEnv)1102cdf0e10cSrcweir void ExtensionManager::disableExtension(
1103cdf0e10cSrcweir     Reference<deploy::XPackage> const & extension,
1104cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
1105cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv )
1106cdf0e10cSrcweir     throw (deploy::DeploymentException,
1107cdf0e10cSrcweir            ucb::CommandFailedException,
1108cdf0e10cSrcweir            ucb::CommandAbortedException,
1109cdf0e10cSrcweir            lang::IllegalArgumentException,
1110cdf0e10cSrcweir            uno::RuntimeException)
1111cdf0e10cSrcweir {
1112cdf0e10cSrcweir     ::osl::MutexGuard guard(getMutex());
1113cdf0e10cSrcweir     uno::Any excOccurred;
1114cdf0e10cSrcweir     bool bUserDisabled = false;
1115cdf0e10cSrcweir     try
1116cdf0e10cSrcweir     {
1117cdf0e10cSrcweir         if (!extension.is())
1118cdf0e10cSrcweir             return;
1119cdf0e10cSrcweir         const OUString repository( extension->getRepositoryName());
1120cdf0e10cSrcweir         if (!repository.equals(OUSTR("user")))
1121cdf0e10cSrcweir             throw lang::IllegalArgumentException(
1122cdf0e10cSrcweir                 OUSTR("No valid repository name provided."),
1123cdf0e10cSrcweir                 static_cast<cppu::OWeakObject*>(this), 0);
1124cdf0e10cSrcweir 
1125cdf0e10cSrcweir         const OUString id(dp_misc::getIdentifier(extension));
1126cdf0e10cSrcweir         bUserDisabled = isUserDisabled(id, extension->getName());
1127cdf0e10cSrcweir 
1128cdf0e10cSrcweir         activateExtension(id, extension->getName(), true, false,
1129cdf0e10cSrcweir                           xAbortChannel, xCmdEnv);
1130cdf0e10cSrcweir     }
1131cdf0e10cSrcweir     catch (deploy::DeploymentException& ) {
1132cdf0e10cSrcweir         excOccurred = ::cppu::getCaughtException();
1133cdf0e10cSrcweir     } catch (ucb::CommandFailedException & ) {
1134cdf0e10cSrcweir         excOccurred = ::cppu::getCaughtException();
1135cdf0e10cSrcweir     } catch (ucb::CommandAbortedException & ) {
1136cdf0e10cSrcweir         excOccurred = ::cppu::getCaughtException();
1137cdf0e10cSrcweir     } catch (lang::IllegalArgumentException &) {
1138cdf0e10cSrcweir         excOccurred = ::cppu::getCaughtException();
1139cdf0e10cSrcweir     } catch (uno::RuntimeException &) {
1140cdf0e10cSrcweir         excOccurred = ::cppu::getCaughtException();
1141cdf0e10cSrcweir     } catch (...) {
1142cdf0e10cSrcweir         excOccurred = ::cppu::getCaughtException();
1143cdf0e10cSrcweir         deploy::DeploymentException exc(
1144cdf0e10cSrcweir             OUSTR("Extension Manager: exception during disableExtension"),
1145cdf0e10cSrcweir             static_cast<OWeakObject*>(this), excOccurred);
1146cdf0e10cSrcweir         excOccurred <<= exc;
1147cdf0e10cSrcweir     }
1148cdf0e10cSrcweir 
1149cdf0e10cSrcweir     if (excOccurred.hasValue())
1150cdf0e10cSrcweir     {
1151cdf0e10cSrcweir         try
1152cdf0e10cSrcweir         {
1153cdf0e10cSrcweir             activateExtension(dp_misc::getIdentifier(extension),
1154cdf0e10cSrcweir                               extension->getName(), bUserDisabled, false,
1155cdf0e10cSrcweir                               xAbortChannel, xCmdEnv);
1156cdf0e10cSrcweir         }
1157cdf0e10cSrcweir         catch (...)
1158cdf0e10cSrcweir         {
1159cdf0e10cSrcweir         }
1160cdf0e10cSrcweir         ::cppu::throwException(excOccurred);
1161cdf0e10cSrcweir     }
1162cdf0e10cSrcweir }
1163cdf0e10cSrcweir 
1164cdf0e10cSrcweir uno::Sequence< Reference<deploy::XPackage> >
getDeployedExtensions(OUString const & repository,Reference<task::XAbortChannel> const & xAbort,Reference<ucb::XCommandEnvironment> const & xCmdEnv)1165cdf0e10cSrcweir     ExtensionManager::getDeployedExtensions(
1166cdf0e10cSrcweir     OUString const & repository,
1167cdf0e10cSrcweir     Reference<task::XAbortChannel> const &xAbort,
1168cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv )
1169cdf0e10cSrcweir     throw (deploy::DeploymentException,
1170cdf0e10cSrcweir         ucb::CommandFailedException,
1171cdf0e10cSrcweir         ucb::CommandAbortedException,
1172cdf0e10cSrcweir         lang::IllegalArgumentException,
1173cdf0e10cSrcweir         uno::RuntimeException)
1174cdf0e10cSrcweir {
1175cdf0e10cSrcweir     return getPackageManager(repository)->getDeployedPackages(
1176cdf0e10cSrcweir         xAbort, xCmdEnv);
1177cdf0e10cSrcweir }
1178cdf0e10cSrcweir 
1179cdf0e10cSrcweir Reference<deploy::XPackage>
getDeployedExtension(OUString const & repository,OUString const & identifier,OUString const & filename,Reference<ucb::XCommandEnvironment> const & xCmdEnv)1180cdf0e10cSrcweir     ExtensionManager::getDeployedExtension(
1181cdf0e10cSrcweir     OUString const & repository,
1182cdf0e10cSrcweir     OUString const & identifier,
1183cdf0e10cSrcweir     OUString const & filename,
1184cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv )
1185cdf0e10cSrcweir     throw (deploy::DeploymentException,
1186cdf0e10cSrcweir         ucb::CommandFailedException,
1187cdf0e10cSrcweir         lang::IllegalArgumentException,
1188cdf0e10cSrcweir         uno::RuntimeException)
1189cdf0e10cSrcweir {
1190cdf0e10cSrcweir     return getPackageManager(repository)->getDeployedPackage(
1191cdf0e10cSrcweir         identifier, filename, xCmdEnv);
1192cdf0e10cSrcweir }
1193cdf0e10cSrcweir 
1194cdf0e10cSrcweir uno::Sequence< uno::Sequence<Reference<deploy::XPackage> > >
getAllExtensions(Reference<task::XAbortChannel> const & xAbort,Reference<ucb::XCommandEnvironment> const & xCmdEnv)1195cdf0e10cSrcweir     ExtensionManager::getAllExtensions(
1196cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbort,
1197cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv )
1198cdf0e10cSrcweir     throw (deploy::DeploymentException,
1199cdf0e10cSrcweir         ucb::CommandFailedException,
1200cdf0e10cSrcweir         ucb::CommandAbortedException,
1201cdf0e10cSrcweir         lang::IllegalArgumentException,
1202cdf0e10cSrcweir         uno::RuntimeException)
1203cdf0e10cSrcweir {
1204cdf0e10cSrcweir     try
1205cdf0e10cSrcweir     {
1206cdf0e10cSrcweir         id2extensions mapExt;
1207cdf0e10cSrcweir 
1208cdf0e10cSrcweir         uno::Sequence<Reference<deploy::XPackage> > userExt =
1209cdf0e10cSrcweir             getUserRepository()->getDeployedPackages(xAbort, xCmdEnv);
1210cdf0e10cSrcweir         addExtensionsToMap(mapExt, userExt, OUSTR("user"));
1211cdf0e10cSrcweir         uno::Sequence<Reference<deploy::XPackage> > sharedExt =
1212cdf0e10cSrcweir             getSharedRepository()->getDeployedPackages(xAbort, xCmdEnv);
1213cdf0e10cSrcweir         addExtensionsToMap(mapExt, sharedExt, OUSTR("shared"));
1214cdf0e10cSrcweir         uno::Sequence<Reference<deploy::XPackage> > bundledExt =
1215cdf0e10cSrcweir             getBundledRepository()->getDeployedPackages(xAbort, xCmdEnv);
1216cdf0e10cSrcweir         addExtensionsToMap(mapExt, bundledExt, OUSTR("bundled"));
1217cdf0e10cSrcweir 
1218b3439cf1SAndre Fischer         // Create the tmp repository to trigger its clean up (deletion
1219b3439cf1SAndre Fischer         // of old temporary data.)
1220b3439cf1SAndre Fischer         getTmpRepository();
1221b3439cf1SAndre Fischer 
1222cdf0e10cSrcweir         //copy the values of the map to a vector for sorting
1223cdf0e10cSrcweir         ::std::vector< ::std::vector<Reference<deploy::XPackage> > >
1224cdf0e10cSrcweir               vecExtensions;
1225cdf0e10cSrcweir         id2extensions::const_iterator mapIt = mapExt.begin();
1226cdf0e10cSrcweir         for (;mapIt != mapExt.end(); mapIt++)
1227cdf0e10cSrcweir             vecExtensions.push_back(mapIt->second);
1228cdf0e10cSrcweir 
1229cdf0e10cSrcweir         //sort the element according to the identifier
1230cdf0e10cSrcweir         ::std::sort(vecExtensions.begin(), vecExtensions.end(), CompIdentifiers());
1231cdf0e10cSrcweir 
1232cdf0e10cSrcweir         ::std::vector< ::std::vector<Reference<deploy::XPackage> > >::const_iterator
1233cdf0e10cSrcweir               citVecVec = vecExtensions.begin();
1234cdf0e10cSrcweir         sal_Int32 j = 0;
1235cdf0e10cSrcweir         uno::Sequence< uno::Sequence<Reference<deploy::XPackage> > > seqSeq(vecExtensions.size());
1236cdf0e10cSrcweir         for (;citVecVec != vecExtensions.end(); citVecVec++, j++)
1237cdf0e10cSrcweir         {
1238cdf0e10cSrcweir             seqSeq[j] = comphelper::containerToSequence(*citVecVec);
1239cdf0e10cSrcweir         }
1240cdf0e10cSrcweir         return seqSeq;
1241cdf0e10cSrcweir 
1242cdf0e10cSrcweir     } catch (deploy::DeploymentException& ) {
1243cdf0e10cSrcweir         throw;
1244cdf0e10cSrcweir     } catch (ucb::CommandFailedException & ) {
1245cdf0e10cSrcweir         throw;
1246cdf0e10cSrcweir     } catch (ucb::CommandAbortedException & ) {
1247cdf0e10cSrcweir         throw;
1248cdf0e10cSrcweir     } catch (lang::IllegalArgumentException &) {
1249cdf0e10cSrcweir         throw;
1250cdf0e10cSrcweir     } catch (uno::RuntimeException &) {
1251cdf0e10cSrcweir         throw;
1252cdf0e10cSrcweir     } catch (...) {
1253cdf0e10cSrcweir         uno::Any exc = ::cppu::getCaughtException();
1254cdf0e10cSrcweir         throw deploy::DeploymentException(
1255cdf0e10cSrcweir             OUSTR("Extension Manager: exception during enableExtension"),
1256cdf0e10cSrcweir             static_cast<OWeakObject*>(this), exc);
1257cdf0e10cSrcweir    }
1258cdf0e10cSrcweir }
1259cdf0e10cSrcweir 
1260cdf0e10cSrcweir //only to be called from unopkg!!!
reinstallDeployedExtensions(OUString const & repository,Reference<task::XAbortChannel> const & xAbortChannel,Reference<ucb::XCommandEnvironment> const & xCmdEnv)1261cdf0e10cSrcweir void ExtensionManager::reinstallDeployedExtensions(
1262cdf0e10cSrcweir     OUString const & repository,
1263cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
1264cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv )
1265cdf0e10cSrcweir     throw (deploy::DeploymentException,
1266cdf0e10cSrcweir         ucb::CommandFailedException, ucb::CommandAbortedException,
1267cdf0e10cSrcweir         lang::IllegalArgumentException, uno::RuntimeException)
1268cdf0e10cSrcweir {
1269cdf0e10cSrcweir     try
1270cdf0e10cSrcweir     {
1271cdf0e10cSrcweir         Reference<deploy::XPackageManager>
1272cdf0e10cSrcweir             xPackageManager = getPackageManager(repository);
1273cdf0e10cSrcweir 
1274cdf0e10cSrcweir         ::osl::MutexGuard guard(getMutex());
1275cdf0e10cSrcweir         xPackageManager->reinstallDeployedPackages(xAbortChannel, xCmdEnv);
1276cdf0e10cSrcweir         //We must sync here, otherwise we will get exceptions when extensions
1277cdf0e10cSrcweir         //are removed.
1278cdf0e10cSrcweir         dp_misc::syncRepositories(xCmdEnv);
1279cdf0e10cSrcweir         const uno::Sequence< Reference<deploy::XPackage> > extensions(
1280cdf0e10cSrcweir             xPackageManager->getDeployedPackages(xAbortChannel, xCmdEnv));
1281cdf0e10cSrcweir 
1282cdf0e10cSrcweir         for ( sal_Int32 pos = 0; pos < extensions.getLength(); ++pos )
1283cdf0e10cSrcweir         {
1284cdf0e10cSrcweir             try
1285cdf0e10cSrcweir             {
1286cdf0e10cSrcweir                 const OUString id =  dp_misc::getIdentifier(extensions[ pos ]);
1287cdf0e10cSrcweir                 const OUString fileName = extensions[ pos ]->getName();
1288cdf0e10cSrcweir                 OSL_ASSERT(id.getLength());
1289cdf0e10cSrcweir                 activateExtension(id, fileName, false, true, xAbortChannel, xCmdEnv );
1290cdf0e10cSrcweir             }
1291cdf0e10cSrcweir             catch (lang::DisposedException &)
1292cdf0e10cSrcweir             {
1293cdf0e10cSrcweir             }
1294cdf0e10cSrcweir         }
1295cdf0e10cSrcweir     } catch (deploy::DeploymentException& ) {
1296cdf0e10cSrcweir         throw;
1297cdf0e10cSrcweir     } catch (ucb::CommandFailedException & ) {
1298cdf0e10cSrcweir         throw;
1299cdf0e10cSrcweir     } catch (ucb::CommandAbortedException & ) {
1300cdf0e10cSrcweir         throw;
1301cdf0e10cSrcweir     } catch (lang::IllegalArgumentException &) {
1302cdf0e10cSrcweir         throw;
1303cdf0e10cSrcweir     } catch (uno::RuntimeException &) {
1304cdf0e10cSrcweir         throw;
1305cdf0e10cSrcweir     } catch (...) {
1306cdf0e10cSrcweir         uno::Any exc = ::cppu::getCaughtException();
1307cdf0e10cSrcweir         throw deploy::DeploymentException(
1308cdf0e10cSrcweir             OUSTR("Extension Manager: exception during enableExtension"),
1309cdf0e10cSrcweir             static_cast<OWeakObject*>(this), exc);
1310cdf0e10cSrcweir     }
1311cdf0e10cSrcweir }
1312cdf0e10cSrcweir 
1313cdf0e10cSrcweir /** Works on the bundled repository. That is using the variables
1314cdf0e10cSrcweir     BUNDLED_EXTENSIONS and BUNDLED_EXTENSIONS_USER.
1315cdf0e10cSrcweir  */
synchronizeBundledPrereg(Reference<task::XAbortChannel> const & xAbortChannel,Reference<ucb::XCommandEnvironment> const & xCmdEnv)1316cdf0e10cSrcweir void ExtensionManager::synchronizeBundledPrereg(
1317cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
1318cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv )
1319cdf0e10cSrcweir     throw (deploy::DeploymentException,
1320cdf0e10cSrcweir            uno::RuntimeException)
1321cdf0e10cSrcweir {
1322cdf0e10cSrcweir     try
1323cdf0e10cSrcweir     {
1324cdf0e10cSrcweir         String sSynchronizingBundled(StrSyncRepository::get());
1325cdf0e10cSrcweir         sSynchronizingBundled.SearchAndReplaceAllAscii( "%NAME", OUSTR("bundled"));
1326cdf0e10cSrcweir         dp_misc::ProgressLevel progressBundled(xCmdEnv, sSynchronizingBundled);
1327cdf0e10cSrcweir 
1328cdf0e10cSrcweir         Reference<deploy::XPackageManagerFactory> xPackageManagerFactory(
1329cdf0e10cSrcweir             deploy::thePackageManagerFactory::get(m_xContext));
1330cdf0e10cSrcweir 
1331cdf0e10cSrcweir         Reference<deploy::XPackageManager> xMgr =
1332cdf0e10cSrcweir             xPackageManagerFactory->getPackageManager(OUSTR("bundled_prereg"));
1333cdf0e10cSrcweir         xMgr->synchronize(xAbortChannel, xCmdEnv);
1334cdf0e10cSrcweir         progressBundled.update(OUSTR("\n\n"));
1335cdf0e10cSrcweir 
1336cdf0e10cSrcweir         uno::Sequence<Reference<deploy::XPackage> > extensions = xMgr->getDeployedPackages(
1337cdf0e10cSrcweir             xAbortChannel, xCmdEnv);
1338cdf0e10cSrcweir         try
1339cdf0e10cSrcweir         {
1340cdf0e10cSrcweir             for (sal_Int32 i = 0; i < extensions.getLength(); i++)
1341cdf0e10cSrcweir             {
1342cdf0e10cSrcweir                 extensions[i]->registerPackage(true, xAbortChannel, xCmdEnv);
1343cdf0e10cSrcweir             }
1344cdf0e10cSrcweir         }
1345cdf0e10cSrcweir         catch (...)
1346cdf0e10cSrcweir         {
1347cdf0e10cSrcweir             OSL_ASSERT(0);
1348cdf0e10cSrcweir         }
1349cdf0e10cSrcweir         OUString lastSyncBundled(RTL_CONSTASCII_USTRINGPARAM(
1350cdf0e10cSrcweir                                      "$BUNDLED_EXTENSIONS_PREREG/lastsynchronized"));
1351cdf0e10cSrcweir         writeLastModified(lastSyncBundled, xCmdEnv);
1352cdf0e10cSrcweir 
1353cdf0e10cSrcweir     } catch (deploy::DeploymentException& ) {
1354cdf0e10cSrcweir         throw;
1355cdf0e10cSrcweir     } catch (ucb::CommandFailedException & ) {
1356cdf0e10cSrcweir         throw;
1357cdf0e10cSrcweir     } catch (ucb::CommandAbortedException & ) {
1358cdf0e10cSrcweir         throw;
1359cdf0e10cSrcweir     } catch (lang::IllegalArgumentException &) {
1360cdf0e10cSrcweir         throw;
1361cdf0e10cSrcweir     } catch (uno::RuntimeException &) {
1362cdf0e10cSrcweir         throw;
1363cdf0e10cSrcweir     } catch (...) {
1364cdf0e10cSrcweir         uno::Any exc = ::cppu::getCaughtException();
1365cdf0e10cSrcweir         throw deploy::DeploymentException(
1366cdf0e10cSrcweir             OUSTR("Extension Manager: exception in synchronize"),
1367cdf0e10cSrcweir             static_cast<OWeakObject*>(this), exc);
1368cdf0e10cSrcweir     }
1369cdf0e10cSrcweir }
1370cdf0e10cSrcweir 
synchronize(Reference<task::XAbortChannel> const & xAbortChannel,Reference<ucb::XCommandEnvironment> const & xCmdEnv)1371cdf0e10cSrcweir sal_Bool ExtensionManager::synchronize(
1372cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
1373cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv )
1374cdf0e10cSrcweir     throw (deploy::DeploymentException,
1375cdf0e10cSrcweir            ucb::CommandFailedException,
1376cdf0e10cSrcweir            ucb::CommandAbortedException,
1377cdf0e10cSrcweir            lang::IllegalArgumentException,
1378cdf0e10cSrcweir            uno::RuntimeException)
1379cdf0e10cSrcweir {
1380cdf0e10cSrcweir     try
1381cdf0e10cSrcweir     {
1382cdf0e10cSrcweir         sal_Bool bModified = sal_False;
1383cdf0e10cSrcweir 
1384cdf0e10cSrcweir         ::osl::MutexGuard guard(getMutex());
1385cdf0e10cSrcweir         String sSynchronizingShared(StrSyncRepository::get());
1386cdf0e10cSrcweir         sSynchronizingShared.SearchAndReplaceAllAscii( "%NAME", OUSTR("shared"));
1387cdf0e10cSrcweir         dp_misc::ProgressLevel progressShared(xCmdEnv, sSynchronizingShared);
1388cdf0e10cSrcweir         bModified = getSharedRepository()->synchronize(xAbortChannel, xCmdEnv);
1389cdf0e10cSrcweir         progressShared.update(OUSTR("\n\n"));
1390cdf0e10cSrcweir 
1391cdf0e10cSrcweir         String sSynchronizingBundled(StrSyncRepository::get());
1392cdf0e10cSrcweir         sSynchronizingBundled.SearchAndReplaceAllAscii( "%NAME", OUSTR("bundled"));
1393cdf0e10cSrcweir         dp_misc::ProgressLevel progressBundled(xCmdEnv, sSynchronizingBundled);
1394cdf0e10cSrcweir         bModified |= getBundledRepository()->synchronize(xAbortChannel, xCmdEnv);
1395cdf0e10cSrcweir         progressBundled.update(OUSTR("\n\n"));
1396cdf0e10cSrcweir 
1397cdf0e10cSrcweir         //Always determine the active extension. This is necessary for the
1398cdf0e10cSrcweir         //first-start optimization. The setup creates the registration data for the
1399cdf0e10cSrcweir         //bundled extensions (brand_layer/share/prereg/bundled), which is copied to the user
1400cdf0e10cSrcweir         //installation (user_installation/extension/bundled) when a user starts OOo
1401cdf0e10cSrcweir         //for the first time after running setup. All bundled extensions are registered
1402cdf0e10cSrcweir         //at that moment. However, extensions with the same identifier can be in the
1403cdf0e10cSrcweir         //shared or user repository, in which case the respective bundled extensions must
1404cdf0e10cSrcweir         //be revoked.
1405cdf0e10cSrcweir         try
1406cdf0e10cSrcweir         {
1407cdf0e10cSrcweir             const uno::Sequence<uno::Sequence<Reference<deploy::XPackage> > >
1408cdf0e10cSrcweir                 seqSeqExt = getAllExtensions(xAbortChannel, xCmdEnv);
1409cdf0e10cSrcweir             for (sal_Int32 i = 0; i < seqSeqExt.getLength(); i++)
1410cdf0e10cSrcweir             {
1411cdf0e10cSrcweir                 uno::Sequence<Reference<deploy::XPackage> > const & seqExt =
1412cdf0e10cSrcweir                     seqSeqExt[i];
1413cdf0e10cSrcweir                 activateExtension(seqExt, isUserDisabled(seqExt), true,
1414cdf0e10cSrcweir                                   xAbortChannel, xCmdEnv);
1415cdf0e10cSrcweir             }
1416cdf0e10cSrcweir         }
1417cdf0e10cSrcweir         catch (...)
1418cdf0e10cSrcweir         {
1419cdf0e10cSrcweir             //We catch the exception, so we can write the lastmodified file
1420cdf0e10cSrcweir             //so we will no repeat this everytime OOo starts.
1421cdf0e10cSrcweir             OSL_ENSURE(0, "Extensions Manager: synchronize");
1422cdf0e10cSrcweir         }
1423cdf0e10cSrcweir         OUString lastSyncBundled(RTL_CONSTASCII_USTRINGPARAM(
1424cdf0e10cSrcweir                                      "$BUNDLED_EXTENSIONS_USER/lastsynchronized"));
1425cdf0e10cSrcweir         writeLastModified(lastSyncBundled, xCmdEnv);
1426cdf0e10cSrcweir         OUString lastSyncShared(RTL_CONSTASCII_USTRINGPARAM(
1427cdf0e10cSrcweir                                     "$SHARED_EXTENSIONS_USER/lastsynchronized"));
1428cdf0e10cSrcweir         writeLastModified(lastSyncShared, xCmdEnv);
1429cdf0e10cSrcweir         return bModified;
1430cdf0e10cSrcweir     } catch (deploy::DeploymentException& ) {
1431cdf0e10cSrcweir         throw;
1432cdf0e10cSrcweir     } catch (ucb::CommandFailedException & ) {
1433cdf0e10cSrcweir         throw;
1434cdf0e10cSrcweir     } catch (ucb::CommandAbortedException & ) {
1435cdf0e10cSrcweir         throw;
1436cdf0e10cSrcweir     } catch (lang::IllegalArgumentException &) {
1437cdf0e10cSrcweir         throw;
1438cdf0e10cSrcweir     } catch (uno::RuntimeException &) {
1439cdf0e10cSrcweir         throw;
1440cdf0e10cSrcweir     } catch (...) {
1441cdf0e10cSrcweir         uno::Any exc = ::cppu::getCaughtException();
1442cdf0e10cSrcweir         throw deploy::DeploymentException(
1443cdf0e10cSrcweir             OUSTR("Extension Manager: exception in synchronize"),
1444cdf0e10cSrcweir             static_cast<OWeakObject*>(this), exc);
1445cdf0e10cSrcweir     }
1446cdf0e10cSrcweir }
1447cdf0e10cSrcweir 
1448cdf0e10cSrcweir // Notify the user when a new extension is to be installed. This is only the
1449cdf0e10cSrcweir // case when one uses the system integration to install an extension (double
1450cdf0e10cSrcweir // clicking on .oxt file etc.)). The function must only be called if there is no
1451cdf0e10cSrcweir // extension with the same identifier already deployed. Then the checkUpdate
1452cdf0e10cSrcweir // function will inform the user that the extension is about to be installed In
1453cdf0e10cSrcweir // case the user cancels the installation a CommandFailed exception is
1454cdf0e10cSrcweir // thrown.
checkInstall(OUString const & displayName,Reference<ucb::XCommandEnvironment> const & cmdEnv)1455cdf0e10cSrcweir void ExtensionManager::checkInstall(
1456cdf0e10cSrcweir 	OUString const & displayName,
1457cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & cmdEnv)
1458cdf0e10cSrcweir {
1459cdf0e10cSrcweir         uno::Any request(
1460cdf0e10cSrcweir 			deploy::InstallException(
1461cdf0e10cSrcweir 				OUSTR("Extension ") + displayName +
1462cdf0e10cSrcweir                 OUSTR(" is about to be installed."),
1463cdf0e10cSrcweir 				static_cast<OWeakObject *>(this), displayName));
1464cdf0e10cSrcweir 	    bool approve = false, abort = false;
1465cdf0e10cSrcweir         if (! dp_misc::interactContinuation(
1466cdf0e10cSrcweir                 request, task::XInteractionApprove::static_type(),
1467cdf0e10cSrcweir                 cmdEnv, &approve, &abort ))
1468cdf0e10cSrcweir 		{
1469cdf0e10cSrcweir             OSL_ASSERT( !approve && !abort );
1470cdf0e10cSrcweir             throw deploy::DeploymentException(
1471cdf0e10cSrcweir                 dp_misc::getResourceString(RID_STR_ERROR_WHILE_ADDING) + displayName,
1472cdf0e10cSrcweir                 static_cast<OWeakObject *>(this), request );
1473cdf0e10cSrcweir         }
1474cdf0e10cSrcweir         if (abort || !approve)
1475cdf0e10cSrcweir             throw ucb::CommandFailedException(
1476cdf0e10cSrcweir                 dp_misc::getResourceString(RID_STR_ERROR_WHILE_ADDING) + displayName,
1477cdf0e10cSrcweir                 static_cast<OWeakObject *>(this), request );
1478cdf0e10cSrcweir }
1479cdf0e10cSrcweir 
1480cdf0e10cSrcweir /* The function will make the user interaction in case there is an extension
1481cdf0e10cSrcweir installed with the same id. This function may only be called if there is already
1482cdf0e10cSrcweir an extension.
1483cdf0e10cSrcweir */
checkUpdate(OUString const & newVersion,OUString const & newDisplayName,Reference<deploy::XPackage> const & oldExtension,Reference<ucb::XCommandEnvironment> const & xCmdEnv)1484cdf0e10cSrcweir void ExtensionManager::checkUpdate(
1485cdf0e10cSrcweir     OUString const & newVersion,
1486cdf0e10cSrcweir     OUString const & newDisplayName,
1487cdf0e10cSrcweir     Reference<deploy::XPackage> const & oldExtension,
1488cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & xCmdEnv )
1489cdf0e10cSrcweir {
1490cdf0e10cSrcweir     // package already deployed, interact --force:
1491cdf0e10cSrcweir     uno::Any request(
1492cdf0e10cSrcweir         (deploy::VersionException(
1493cdf0e10cSrcweir             dp_misc::getResourceString(
1494cdf0e10cSrcweir                 RID_STR_PACKAGE_ALREADY_ADDED ) + newDisplayName,
1495cdf0e10cSrcweir             static_cast<OWeakObject *>(this), newVersion, newDisplayName,
1496cdf0e10cSrcweir             oldExtension ) ) );
1497cdf0e10cSrcweir     bool replace = false, abort = false;
1498cdf0e10cSrcweir     if (! dp_misc::interactContinuation(
1499cdf0e10cSrcweir             request, task::XInteractionApprove::static_type(),
1500cdf0e10cSrcweir             xCmdEnv, &replace, &abort )) {
1501cdf0e10cSrcweir         OSL_ASSERT( !replace && !abort );
1502cdf0e10cSrcweir         throw deploy::DeploymentException(
1503cdf0e10cSrcweir             dp_misc::getResourceString(
1504cdf0e10cSrcweir                 RID_STR_ERROR_WHILE_ADDING) + newDisplayName,
1505cdf0e10cSrcweir             static_cast<OWeakObject *>(this), request );
1506cdf0e10cSrcweir     }
1507cdf0e10cSrcweir     if (abort || !replace)
1508cdf0e10cSrcweir         throw ucb::CommandFailedException(
1509cdf0e10cSrcweir             dp_misc::getResourceString(
1510cdf0e10cSrcweir                 RID_STR_PACKAGE_ALREADY_ADDED) + newDisplayName,
1511cdf0e10cSrcweir             static_cast<OWeakObject *>(this), request );
1512cdf0e10cSrcweir }
1513cdf0e10cSrcweir 
getTempExtension(OUString const & url,Reference<task::XAbortChannel> const & xAbortChannel,Reference<ucb::XCommandEnvironment> const &)1514cdf0e10cSrcweir Reference<deploy::XPackage> ExtensionManager::getTempExtension(
1515cdf0e10cSrcweir     OUString const & url,
1516cdf0e10cSrcweir     Reference<task::XAbortChannel> const & xAbortChannel,
1517cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> const & /*xCmdEnv*/)
1518cdf0e10cSrcweir 
1519cdf0e10cSrcweir {
1520cdf0e10cSrcweir     Reference<ucb::XCommandEnvironment> tmpCmdEnvA(new TmpRepositoryCommandEnv());
1521cdf0e10cSrcweir     Reference<deploy::XPackage> xTmpPackage = getTmpRepository()->addPackage(
1522cdf0e10cSrcweir         url, uno::Sequence<beans::NamedValue>(),OUString(), xAbortChannel, tmpCmdEnvA);
1523cdf0e10cSrcweir     if (!xTmpPackage.is())
1524cdf0e10cSrcweir     {
1525cdf0e10cSrcweir         throw deploy::DeploymentException(
1526cdf0e10cSrcweir             OUSTR("Extension Manager: Failed to create temporary XPackage for url: ") + url,
1527cdf0e10cSrcweir             static_cast<OWeakObject*>(this), uno::Any());
1528cdf0e10cSrcweir 
1529cdf0e10cSrcweir     }
1530cdf0e10cSrcweir     return xTmpPackage;
1531cdf0e10cSrcweir }
1532cdf0e10cSrcweir 
1533cdf0e10cSrcweir uno::Sequence<Reference<deploy::XPackage> > SAL_CALL
getExtensionsWithUnacceptedLicenses(OUString const & repository,Reference<ucb::XCommandEnvironment> const & xCmdEnv)1534cdf0e10cSrcweir ExtensionManager::getExtensionsWithUnacceptedLicenses(
1535cdf0e10cSrcweir         OUString const & repository,
1536cdf0e10cSrcweir         Reference<ucb::XCommandEnvironment> const & xCmdEnv)
1537cdf0e10cSrcweir         throw (deploy::DeploymentException,
1538cdf0e10cSrcweir                uno::RuntimeException)
1539cdf0e10cSrcweir {
1540cdf0e10cSrcweir     Reference<deploy::XPackageManager>
1541cdf0e10cSrcweir         xPackageManager = getPackageManager(repository);
1542cdf0e10cSrcweir     ::osl::MutexGuard guard(getMutex());
1543cdf0e10cSrcweir     return xPackageManager->getExtensionsWithUnacceptedLicenses(xCmdEnv);
1544cdf0e10cSrcweir }
1545cdf0e10cSrcweir 
isReadOnlyRepository(::rtl::OUString const & repository)1546cdf0e10cSrcweir sal_Bool ExtensionManager::isReadOnlyRepository(::rtl::OUString const & repository)
1547cdf0e10cSrcweir         throw (uno::RuntimeException)
1548cdf0e10cSrcweir {
1549cdf0e10cSrcweir     return getPackageManager(repository)->isReadOnly();
1550cdf0e10cSrcweir }
1551cdf0e10cSrcweir //------------------------------------------------------------------------------
1552cdf0e10cSrcweir //------------------------------------------------------------------------------
1553cdf0e10cSrcweir //------------------------------------------------------------------------------
1554cdf0e10cSrcweir 
1555cdf0e10cSrcweir namespace sdecl = comphelper::service_decl;
1556cdf0e10cSrcweir sdecl::class_<ExtensionManager> servicePIP;
1557cdf0e10cSrcweir extern sdecl::ServiceDecl const serviceDecl(
1558cdf0e10cSrcweir     servicePIP,
1559cdf0e10cSrcweir     // a private one:
1560cdf0e10cSrcweir     "com.sun.star.comp.deployment.ExtensionManager",
1561cdf0e10cSrcweir     "com.sun.star.comp.deployment.ExtensionManager");
1562cdf0e10cSrcweir 
1563cdf0e10cSrcweir //------------------------------------------------------------------------------
singleton_entries(uno::Reference<registry::XRegistryKey> const & xRegistryKey)1564cdf0e10cSrcweir bool singleton_entries(
1565cdf0e10cSrcweir     uno::Reference< registry::XRegistryKey > const & xRegistryKey )
1566cdf0e10cSrcweir {
1567cdf0e10cSrcweir     try {
1568cdf0e10cSrcweir         uno::Reference< registry::XRegistryKey > xKey(
1569cdf0e10cSrcweir             xRegistryKey->createKey(
1570cdf0e10cSrcweir                 serviceDecl.getImplementationName() +
1571cdf0e10cSrcweir                 // xxx todo: use future generated function to get singleton name
1572cdf0e10cSrcweir                 OUSTR("/UNO/SINGLETONS/"
1573cdf0e10cSrcweir                       "com.sun.star.deployment.ExtensionManager") ) );
1574cdf0e10cSrcweir         xKey->setStringValue( serviceDecl.getSupportedServiceNames()[0] );
1575cdf0e10cSrcweir         return true;
1576cdf0e10cSrcweir     }
1577cdf0e10cSrcweir     catch (registry::InvalidRegistryException & exc) {
1578cdf0e10cSrcweir         (void) exc; // avoid warnings
1579cdf0e10cSrcweir         OSL_ENSURE( 0, ::rtl::OUStringToOString(
1580cdf0e10cSrcweir                         exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
1581cdf0e10cSrcweir         return false;
1582cdf0e10cSrcweir     }
1583cdf0e10cSrcweir }
1584cdf0e10cSrcweir 
1585cdf0e10cSrcweir // XModifyBroadcaster
1586cdf0e10cSrcweir //______________________________________________________________________________
addModifyListener(Reference<util::XModifyListener> const & xListener)1587cdf0e10cSrcweir void ExtensionManager::addModifyListener(
1588cdf0e10cSrcweir     Reference<util::XModifyListener> const & xListener )
1589cdf0e10cSrcweir     throw (uno::RuntimeException)
1590cdf0e10cSrcweir {
1591cdf0e10cSrcweir      check();
1592cdf0e10cSrcweir      rBHelper.addListener( ::getCppuType( &xListener ), xListener );
1593cdf0e10cSrcweir }
1594cdf0e10cSrcweir 
1595cdf0e10cSrcweir //______________________________________________________________________________
removeModifyListener(Reference<util::XModifyListener> const & xListener)1596cdf0e10cSrcweir void ExtensionManager::removeModifyListener(
1597cdf0e10cSrcweir     Reference<util::XModifyListener> const & xListener )
1598cdf0e10cSrcweir     throw (uno::RuntimeException)
1599cdf0e10cSrcweir {
1600cdf0e10cSrcweir     check();
1601cdf0e10cSrcweir     rBHelper.removeListener( ::getCppuType( &xListener ), xListener );
1602cdf0e10cSrcweir }
1603cdf0e10cSrcweir 
check()1604cdf0e10cSrcweir void ExtensionManager::check()
1605cdf0e10cSrcweir {
1606cdf0e10cSrcweir     ::osl::MutexGuard guard( getMutex() );
1607cdf0e10cSrcweir     if (rBHelper.bInDispose || rBHelper.bDisposed) {
1608cdf0e10cSrcweir         throw lang::DisposedException(
1609cdf0e10cSrcweir             OUSTR("ExtensionManager instance has already been disposed!"),
1610cdf0e10cSrcweir             static_cast<OWeakObject *>(this) );
1611cdf0e10cSrcweir     }
1612cdf0e10cSrcweir }
1613cdf0e10cSrcweir 
fireModified()1614cdf0e10cSrcweir void ExtensionManager::fireModified()
1615cdf0e10cSrcweir {
1616cdf0e10cSrcweir     ::cppu::OInterfaceContainerHelper * pContainer = rBHelper.getContainer(
1617cdf0e10cSrcweir         util::XModifyListener::static_type() );
1618cdf0e10cSrcweir     if (pContainer != 0) {
1619cdf0e10cSrcweir         pContainer->forEach<util::XModifyListener>(
1620cdf0e10cSrcweir             boost::bind(&util::XModifyListener::modified, _1,
1621cdf0e10cSrcweir                         lang::EventObject(static_cast<OWeakObject *>(this))) );
1622cdf0e10cSrcweir     }
1623cdf0e10cSrcweir }
1624cdf0e10cSrcweir 
1625cdf0e10cSrcweir } // namespace dp_manager
1626cdf0e10cSrcweir 
1627cdf0e10cSrcweir 
1628