LCOV - code coverage report
Current view: top level - desktop/source/app - check_ext_deps.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 75 157 47.8 %
Date: 2014-11-03 Functions: 14 17 82.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <config_folders.h>
      21             : #include <config_features.h>
      22             : 
      23             : #include <osl/file.hxx>
      24             : #include <osl/mutex.hxx>
      25             : 
      26             : #include <rtl/bootstrap.hxx>
      27             : #include <rtl/ustring.hxx>
      28             : #include <cppuhelper/compbase3.hxx>
      29             : 
      30             : #include <vcl/wrkwin.hxx>
      31             : #include <vcl/timer.hxx>
      32             : 
      33             : #include <unotools/configmgr.hxx>
      34             : #include <toolkit/helper/vclunohelper.hxx>
      35             : 
      36             : #include <comphelper/processfactory.hxx>
      37             : #include <comphelper/sequence.hxx>
      38             : #include <cppuhelper/bootstrap.hxx>
      39             : #include <com/sun/star/ucb/XCommandEnvironment.hpp>
      40             : #include <com/sun/star/beans/XPropertySet.hpp>
      41             : #include <com/sun/star/beans/NamedValue.hpp>
      42             : #include <com/sun/star/configuration/theDefaultProvider.hpp>
      43             : #include <com/sun/star/deployment/XPackage.hpp>
      44             : #include <com/sun/star/deployment/ExtensionManager.hpp>
      45             : #include <com/sun/star/deployment/LicenseException.hpp>
      46             : #include <com/sun/star/deployment/ui/LicenseDialog.hpp>
      47             : #include <com/sun/star/task/OfficeRestartManager.hpp>
      48             : #include <com/sun/star/task/XJob.hpp>
      49             : #include <com/sun/star/task/XInteractionApprove.hpp>
      50             : #include <com/sun/star/task/XInteractionAbort.hpp>
      51             : #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
      52             : #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
      53             : #include <com/sun/star/util/XChangesBatch.hpp>
      54             : 
      55             : #include "app.hxx"
      56             : 
      57             : #include "../deployment/inc/dp_misc.h"
      58             : 
      59             : using namespace desktop;
      60             : using namespace com::sun::star;
      61             : using namespace com::sun::star::lang;
      62             : using namespace com::sun::star::task;
      63             : using namespace com::sun::star::uno;
      64             : 
      65             : namespace
      66             : {
      67             : //For use with XExtensionManager.synchronize
      68             : class SilentCommandEnv
      69             :     : public ::cppu::WeakImplHelper3< ucb::XCommandEnvironment,
      70             :                                       task::XInteractionHandler,
      71             :                                       ucb::XProgressHandler >
      72             : {
      73             :     uno::Reference<uno::XComponentContext> mxContext;
      74             :     Desktop    *mpDesktop;
      75             :     sal_Int32   mnLevel;
      76             :     sal_Int32   mnProgress;
      77             : 
      78             : public:
      79             :     SilentCommandEnv(
      80             :         uno::Reference<uno::XComponentContext> const & xContext,
      81             :         Desktop* pDesktop );
      82             :     virtual ~SilentCommandEnv();
      83             : 
      84             :     // XCommandEnvironment
      85             :     virtual uno::Reference<task::XInteractionHandler > SAL_CALL
      86             :     getInteractionHandler() throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
      87             :     virtual uno::Reference<ucb::XProgressHandler >
      88             :     SAL_CALL getProgressHandler() throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
      89             : 
      90             :     // XInteractionHandler
      91             :     virtual void SAL_CALL handle(
      92             :         uno::Reference<task::XInteractionRequest > const & xRequest )
      93             :         throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
      94             : 
      95             :     // XProgressHandler
      96             :     virtual void SAL_CALL push( uno::Any const & Status )
      97             :         throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
      98             :     virtual void SAL_CALL update( uno::Any const & Status )
      99             :         throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
     100             :     virtual void SAL_CALL pop() throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
     101             : };
     102             : 
     103             : 
     104         160 : SilentCommandEnv::SilentCommandEnv(
     105             :     uno::Reference<uno::XComponentContext> const & xContext,
     106             :     Desktop* pDesktop ):
     107             :     mxContext( xContext ),
     108             :     mpDesktop( pDesktop ),
     109             :     mnLevel( 0 ),
     110         160 :     mnProgress( 25 )
     111         160 : {}
     112             : 
     113             : 
     114         480 : SilentCommandEnv::~SilentCommandEnv()
     115             : {
     116         160 :     mpDesktop->SetSplashScreenText( OUString() );
     117         320 : }
     118             : 
     119             : 
     120         128 : Reference<task::XInteractionHandler> SilentCommandEnv::getInteractionHandler()
     121             :     throw (uno::RuntimeException, std::exception)
     122             : {
     123         128 :     return this;
     124             : }
     125             : 
     126             : 
     127         256 : Reference<ucb::XProgressHandler> SilentCommandEnv::getProgressHandler()
     128             :     throw (uno::RuntimeException, std::exception)
     129             : {
     130         256 :     return this;
     131             : }
     132             : 
     133             : 
     134             : // XInteractionHandler
     135           0 : void SilentCommandEnv::handle( Reference< task::XInteractionRequest> const & xRequest )
     136             :     throw (uno::RuntimeException, std::exception)
     137             : {
     138           0 :     deployment::LicenseException licExc;
     139             : 
     140           0 :     uno::Any request( xRequest->getRequest() );
     141           0 :     bool bApprove = true;
     142             : 
     143           0 :     if ( request >>= licExc )
     144             :     {
     145             :         uno::Reference< ui::dialogs::XExecutableDialog > xDialog(
     146             :             deployment::ui::LicenseDialog::create(
     147             :             mxContext, VCLUnoHelper::GetInterface( NULL ),
     148           0 :             licExc.ExtensionName, licExc.Text ) );
     149           0 :         sal_Int16 res = xDialog->execute();
     150           0 :         if ( res == ui::dialogs::ExecutableDialogResults::CANCEL )
     151           0 :             bApprove = false;
     152           0 :         else if ( res == ui::dialogs::ExecutableDialogResults::OK )
     153           0 :             bApprove = true;
     154             :         else
     155             :         {
     156             :             OSL_ASSERT(false);
     157           0 :         }
     158             :     }
     159             : 
     160             :     // We approve everything here
     161           0 :     uno::Sequence< Reference< task::XInteractionContinuation > > conts( xRequest->getContinuations() );
     162           0 :     Reference< task::XInteractionContinuation > const * pConts = conts.getConstArray();
     163           0 :     sal_Int32 len = conts.getLength();
     164             : 
     165           0 :     for ( sal_Int32 pos = 0; pos < len; ++pos )
     166             :     {
     167           0 :         if ( bApprove )
     168             :         {
     169           0 :             uno::Reference< task::XInteractionApprove > xInteractionApprove( pConts[ pos ], uno::UNO_QUERY );
     170           0 :             if ( xInteractionApprove.is() )
     171           0 :                 xInteractionApprove->select();
     172             :         }
     173             :         else
     174             :         {
     175           0 :             uno::Reference< task::XInteractionAbort > xInteractionAbort( pConts[ pos ], uno::UNO_QUERY );
     176           0 :             if ( xInteractionAbort.is() )
     177           0 :                 xInteractionAbort->select();
     178             :         }
     179           0 :     }
     180           0 : }
     181             : 
     182             : 
     183             : // XProgressHandler
     184         192 : void SilentCommandEnv::push( uno::Any const & rStatus )
     185             :     throw (uno::RuntimeException, std::exception)
     186             : {
     187         192 :     OUString sText;
     188         192 :     mnLevel += 1;
     189             : 
     190         192 :     if ( rStatus.hasValue() && ( rStatus >>= sText) )
     191             :     {
     192         192 :         if ( mnLevel <= 3 )
     193         192 :             mpDesktop->SetSplashScreenText( sText );
     194             :         else
     195           0 :             mpDesktop->SetSplashScreenProgress( ++mnProgress );
     196         192 :     }
     197         192 : }
     198             : 
     199             : 
     200         128 : void SilentCommandEnv::update( uno::Any const & rStatus )
     201             :     throw (uno::RuntimeException, std::exception)
     202             : {
     203         128 :     OUString sText;
     204         128 :     if ( rStatus.hasValue() && ( rStatus >>= sText) )
     205             :     {
     206         128 :         mpDesktop->SetSplashScreenText( sText );
     207         128 :     }
     208         128 : }
     209             : 
     210             : 
     211         192 : void SilentCommandEnv::pop() throw (uno::RuntimeException, std::exception)
     212             : {
     213         192 :     mnLevel -= 1;
     214         192 : }
     215             : 
     216             : } // end namespace
     217             : 
     218             : 
     219             : static const char aAccessSrvc[] = "com.sun.star.configuration.ConfigurationUpdateAccess";
     220             : 
     221           0 : static sal_Int16 impl_showExtensionDialog( uno::Reference< uno::XComponentContext > &xContext )
     222             : {
     223           0 :     OUString sServiceName = "com.sun.star.deployment.ui.UpdateRequiredDialog";
     224           0 :     uno::Reference< uno::XInterface > xService;
     225           0 :     sal_Int16 nRet = 0;
     226             : 
     227           0 :     uno::Reference< lang::XMultiComponentFactory > xServiceManager( xContext->getServiceManager() );
     228           0 :     if( !xServiceManager.is() )
     229             :         throw uno::RuntimeException(
     230           0 :             "impl_showExtensionDialog(): unable to obtain service manager from component context", uno::Reference< uno::XInterface > () );
     231             : 
     232           0 :     xService = xServiceManager->createInstanceWithContext( sServiceName, xContext );
     233           0 :     uno::Reference< ui::dialogs::XExecutableDialog > xExecuteable( xService, uno::UNO_QUERY );
     234           0 :     if ( xExecuteable.is() )
     235           0 :         nRet = xExecuteable->execute();
     236             : 
     237           0 :     return nRet;
     238             : }
     239             : 
     240             : 
     241             : // Check dependencies of all packages
     242             : 
     243          64 : static bool impl_checkDependencies( const uno::Reference< uno::XComponentContext > &xContext )
     244             : {
     245          64 :     uno::Sequence< uno::Sequence< uno::Reference< deployment::XPackage > > > xAllPackages;
     246         128 :     uno::Reference< deployment::XExtensionManager > xExtensionManager = deployment::ExtensionManager::get( xContext );
     247             : 
     248          64 :     if ( !xExtensionManager.is() )
     249             :     {
     250             :        SAL_WARN( "desktop.app", "Could not get the Extension Manager!" );
     251           0 :         return true;
     252             :     }
     253             : 
     254             :     try {
     255         192 :         xAllPackages = xExtensionManager->getAllExtensions( uno::Reference< task::XAbortChannel >(),
     256         128 :                                                             uno::Reference< ucb::XCommandEnvironment >() );
     257             :     }
     258           0 :     catch ( const deployment::DeploymentException & ) { return true; }
     259           0 :     catch ( const ucb::CommandFailedException & ) { return true; }
     260           0 :     catch ( const ucb::CommandAbortedException & ) { return true; }
     261           0 :     catch ( const lang::IllegalArgumentException & e ) {
     262           0 :         throw uno::RuntimeException( e.Message, e.Context );
     263             :     }
     264             : 
     265          64 :     sal_Int32 nMax = 2;
     266             : #ifdef DEBUG
     267             :     nMax = 3;
     268             : #endif
     269             : 
     270          64 :     for ( sal_Int32 i = 0; i < xAllPackages.getLength(); ++i )
     271             :     {
     272           0 :         uno::Sequence< uno::Reference< deployment::XPackage > > xPackageList = xAllPackages[i];
     273             : 
     274           0 :         for ( sal_Int32 j = 0; (j<nMax) && (j < xPackageList.getLength()); ++j )
     275             :         {
     276           0 :             uno::Reference< deployment::XPackage > xPackage = xPackageList[j];
     277           0 :             if ( xPackage.is() )
     278             :             {
     279           0 :                 bool bRegistered = false;
     280             :                 try {
     281           0 :                     beans::Optional< beans::Ambiguous< sal_Bool > > option( xPackage->isRegistered( uno::Reference< task::XAbortChannel >(),
     282           0 :                                                                                                     uno::Reference< ucb::XCommandEnvironment >() ) );
     283           0 :                     if ( option.IsPresent )
     284             :                     {
     285           0 :                         ::beans::Ambiguous< sal_Bool > const & reg = option.Value;
     286           0 :                         if ( reg.IsAmbiguous )
     287           0 :                             bRegistered = false;
     288             :                         else
     289           0 :                             bRegistered = reg.Value ? true : false;
     290             :                     }
     291             :                     else
     292           0 :                         bRegistered = false;
     293             :                 }
     294           0 :                 catch ( const uno::RuntimeException & ) { throw; }
     295           0 :                 catch (const uno::Exception & exc) {
     296             :                     (void) exc;
     297             :                    SAL_WARN( "desktop.app", "" <<  exc.Message );
     298             :                 }
     299             : 
     300           0 :                 if ( bRegistered )
     301             :                 {
     302           0 :                     bool bDependenciesValid = false;
     303             :                     try {
     304           0 :                         bDependenciesValid = xPackage->checkDependencies( uno::Reference< ucb::XCommandEnvironment >() );
     305             :                     }
     306           0 :                     catch ( const deployment::DeploymentException & ) {}
     307           0 :                     if ( ! bDependenciesValid )
     308             :                     {
     309           0 :                         return false;
     310             :                     }
     311             :                 }
     312             :             }
     313           0 :         }
     314           0 :     }
     315         128 :     return true;
     316             : }
     317             : 
     318             : 
     319             : // resets the 'check needed' flag (needed, if aborted)
     320             : 
     321           0 : static void impl_setNeedsCompatCheck()
     322             : {
     323             :     try {
     324             :         Reference< XMultiServiceFactory > theConfigProvider(
     325             :             configuration::theDefaultProvider::get(
     326           0 :                 comphelper::getProcessComponentContext() ) );
     327             : 
     328           0 :         Sequence< Any > theArgs(1);
     329             :         beans::NamedValue v( OUString("nodepath"),
     330           0 :                       makeAny( OUString("org.openoffice.Setup/Office") ) );
     331           0 :         theArgs[0] <<= v;
     332             :         Reference< beans::XPropertySet > pset = Reference< beans::XPropertySet >(
     333           0 :             theConfigProvider->createInstanceWithArguments( OUString(aAccessSrvc), theArgs ), UNO_QUERY_THROW );
     334             : 
     335           0 :         Any value = makeAny( OUString("never") );
     336             : 
     337           0 :         pset->setPropertyValue("LastCompatibilityCheckID", value );
     338           0 :         Reference< util::XChangesBatch >( pset, UNO_QUERY_THROW )->commitChanges();
     339             :     }
     340           0 :     catch (const Exception&) {}
     341           0 : }
     342             : 
     343             : 
     344             : // to check if we need checking the dependencies of the extensions again, we compare
     345             : // the build id of the office with the one of the last check
     346             : 
     347         160 : static bool impl_needsCompatCheck()
     348             : {
     349         160 :     bool bNeedsCheck = false;
     350         160 :     OUString aLastCheckBuildID;
     351         320 :     OUString aCurrentBuildID( "${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE("version") ":buildid}" );
     352         160 :     rtl::Bootstrap::expandMacros( aCurrentBuildID );
     353             : 
     354             :     try {
     355             :         Reference< XMultiServiceFactory > theConfigProvider(
     356             :             configuration::theDefaultProvider::get(
     357         160 :                 comphelper::getProcessComponentContext() ) );
     358             : 
     359         320 :         Sequence< Any > theArgs(1);
     360             :         beans::NamedValue v( OUString("nodepath"),
     361         320 :                       makeAny( OUString("org.openoffice.Setup/Office") ) );
     362         160 :         theArgs[0] <<= v;
     363             :         Reference< beans::XPropertySet > pset = Reference< beans::XPropertySet >(
     364         320 :             theConfigProvider->createInstanceWithArguments( OUString(aAccessSrvc), theArgs ), UNO_QUERY_THROW );
     365             : 
     366         320 :         Any result = pset->getPropertyValue("LastCompatibilityCheckID");
     367             : 
     368         160 :         result >>= aLastCheckBuildID;
     369         160 :         if ( aLastCheckBuildID != aCurrentBuildID )
     370             :         {
     371          64 :             bNeedsCheck = true;
     372          64 :             result <<= aCurrentBuildID;
     373          64 :             pset->setPropertyValue("LastCompatibilityCheckID", result );
     374          64 :             Reference< util::XChangesBatch >( pset, UNO_QUERY_THROW )->commitChanges();
     375         160 :         }
     376             : #ifdef DEBUG
     377             :         bNeedsCheck = true;
     378             : #endif
     379             :     }
     380           0 :     catch (const com::sun::star::uno::Exception&) {}
     381             : 
     382         320 :     return bNeedsCheck;
     383             : }
     384             : 
     385             : 
     386             : // Do we need to check the dependencies of the extensions?
     387             : // When there are unresolved issues, we can't continue with startup
     388         160 : bool Desktop::CheckExtensionDependencies()
     389             : {
     390         160 :     if (!impl_needsCompatCheck())
     391             :     {
     392          96 :         return false;
     393             :     }
     394             : 
     395             :     uno::Reference< uno::XComponentContext > xContext(
     396          64 :         comphelper::getProcessComponentContext());
     397             : 
     398          64 :     bool bDependenciesValid = impl_checkDependencies( xContext );
     399             : 
     400          64 :     short nRet = 0;
     401             : 
     402          64 :     if ( !bDependenciesValid )
     403           0 :         nRet = impl_showExtensionDialog( xContext );
     404             : 
     405          64 :     if ( nRet == -1 )
     406             :     {
     407           0 :         impl_setNeedsCompatCheck();
     408           0 :         return true;
     409             :     }
     410             :     else
     411          64 :         return false;
     412             : }
     413             : 
     414         160 : void Desktop::SynchronizeExtensionRepositories()
     415             : {
     416             :     SAL_INFO( "desktop.app", "desktop (jl) ::Desktop::SynchronizeExtensionRepositories");
     417             :     uno::Reference< uno::XComponentContext > context(
     418         160 :         comphelper::getProcessComponentContext());
     419             :     uno::Reference< ucb::XCommandEnvironment > silent(
     420         320 :         new SilentCommandEnv(context, this));
     421         160 :     if (m_bCleanedExtensionCache) {
     422         128 :         deployment::ExtensionManager::get(context)->reinstallDeployedExtensions(
     423          64 :             true, "user", Reference<task::XAbortChannel>(), silent);
     424             : #if !HAVE_FEATURE_MACOSX_SANDBOX
     425         128 :         task::OfficeRestartManager::get(context)->requestRestart(
     426          64 :             silent->getInteractionHandler());
     427             : #endif
     428             :     } else {
     429             :         // reinstallDeployedExtensions above already calls syncRepositories
     430             :         // internally:
     431          96 :         dp_misc::syncRepositories(false, silent);
     432         160 :     }
     433         640 : }
     434             : 
     435             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10