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

Generated by: LCOV version 1.11