LCOV - code coverage report
Current view: top level - extensions/source/plugin/base - context.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 1 115 0.9 %
Date: 2014-11-03 Functions: 2 20 10.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             :  *
       4             :  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       5             :  *
       6             :  * Copyright 2000, 2010 Oracle and/or its affiliates.
       7             :  *
       8             :  * OpenOffice.org - a multi-platform office productivity suite
       9             :  *
      10             :  * This file is part of OpenOffice.org.
      11             :  *
      12             :  * OpenOffice.org is free software: you can redistribute it and/or modify
      13             :  * it under the terms of the GNU Lesser General Public License version 3
      14             :  * only, as published by the Free Software Foundation.
      15             :  *
      16             :  * OpenOffice.org is distributed in the hope that it will be useful,
      17             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19             :  * GNU Lesser General Public License version 3 for more details
      20             :  * (a copy is included in the LICENSE file that accompanied this code).
      21             :  *
      22             :  * You should have received a copy of the GNU Lesser General Public License
      23             :  * version 3 along with OpenOffice.org.  If not, see
      24             :  * <http://www.openoffice.org/license.html>
      25             :  * for a copy of the LGPLv3 License.
      26             :  *
      27             :  ************************************************************************/
      28             : 
      29             : #ifdef AIX
      30             : #define _LINUX_SOURCE_COMPAT
      31             : #include <sys/timer.h>
      32             : #undef _LINUX_SOURCE_COMPAT
      33             : #endif
      34             : 
      35             : #ifdef WNT
      36             : #include <prewin.h>
      37             : #include <postwin.h>
      38             : #endif
      39             : 
      40             : #include <cstdarg>
      41             : #include <cstdio>
      42             : 
      43             : #include <plugin/impl.hxx>
      44             : 
      45             : #include <sal/log.hxx>
      46             : #include <com/sun/star/frame/FrameSearchFlag.hpp>
      47             : #include <com/sun/star/frame/XComponentLoader.hpp>
      48             : #include <com/sun/star/uno/XComponentContext.hpp>
      49             : #include <com/sun/star/frame/Desktop.hpp>
      50             : #include <tools/urlobj.hxx>
      51             : #include <osl/file.hxx>
      52             : 
      53             : #include <cppuhelper/implbase1.hxx>
      54             : 
      55             : using namespace com::sun::star::io;
      56             : using namespace com::sun::star::frame;
      57             : 
      58             : namespace ext_plug {
      59             : 
      60             : class FileSink : public ::cppu::WeakAggImplHelper1< ::com::sun::star::io::XOutputStream >
      61             : {
      62             : private:
      63             :     Reference< ::com::sun::star::uno::XComponentContext >   m_xContext;
      64             :     FILE*                   fp;
      65             :     Reference< ::com::sun::star::plugin::XPlugin >          m_xPlugin;
      66             :     OUString                 m_aTarget;
      67             :     OUString                 m_aFileName;
      68             : 
      69             : public:
      70             :     FileSink( const Reference< ::com::sun::star::uno::XComponentContext > &,
      71             :               const Reference< ::com::sun::star::plugin::XPlugin > & plugin,
      72             :               const OUString& target,
      73             :               const Reference< ::com::sun::star::io::XActiveDataSource > & source );
      74             :     virtual ~FileSink();
      75             : 
      76             :     // ::com::sun::star::io::XOutputStream
      77             :     virtual void SAL_CALL writeBytes( const Sequence<sal_Int8>& ) throw(std::exception) SAL_OVERRIDE;
      78             :     virtual void SAL_CALL flush() throw(std::exception) SAL_OVERRIDE;
      79             :     virtual void SAL_CALL closeOutput() throw (RuntimeException, std::exception) SAL_OVERRIDE;
      80             : };
      81             : 
      82             : }
      83             : using namespace ext_plug;
      84             : 
      85             : class XPluginContext_Impl : public ::cppu::WeakAggImplHelper1< ::com::sun::star::plugin::XPluginContext >
      86             : {
      87             :     Reference< ::com::sun::star::uno::XComponentContext >   m_xContext;
      88             :     rtl_TextEncoding                                        m_aEncoding;
      89             : public:
      90             : 
      91             :     XPluginContext_Impl( const Reference< ::com::sun::star::uno::XComponentContext >  & );
      92             :     virtual ~XPluginContext_Impl();
      93             : 
      94             : 
      95             :     virtual OUString SAL_CALL getValue(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, ::com::sun::star::plugin::PluginVariable variable) throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception ) SAL_OVERRIDE;
      96             :     virtual void SAL_CALL getURLNotify(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const OUString& url, const OUString& target, const Reference< ::com::sun::star::lang::XEventListener > & listener) throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception ) SAL_OVERRIDE;
      97             :     virtual void SAL_CALL getURL(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const OUString& url, const OUString& target) throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception ) SAL_OVERRIDE;
      98             :     virtual void SAL_CALL postURLNotify(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const OUString& url, const OUString& target, const Sequence< sal_Int8 >& buf, sal_Bool file, const Reference< ::com::sun::star::lang::XEventListener > & listener) throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception ) SAL_OVERRIDE;
      99             :     virtual void SAL_CALL postURL(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const OUString& url, const OUString& target, const Sequence< sal_Int8 >& buf, sal_Bool file) throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception ) SAL_OVERRIDE;
     100             :     virtual void SAL_CALL newStream(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const OUString& mimetype, const OUString& target, const Reference< ::com::sun::star::io::XActiveDataSource > & source) throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception ) SAL_OVERRIDE;
     101             :     virtual void SAL_CALL displayStatusText(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const OUString& message) throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception ) SAL_OVERRIDE;
     102             :     virtual OUString SAL_CALL getUserAgent(const Reference< ::com::sun::star::plugin::XPlugin > & plugin) throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception ) SAL_OVERRIDE;
     103             : };
     104             : 
     105           0 : Reference< ::com::sun::star::plugin::XPluginContext >  XPluginManager_Impl::createPluginContext() throw(std::exception)
     106             : {
     107           0 :     return new XPluginContext_Impl( m_xContext );
     108             : }
     109             : 
     110           0 : XPluginContext_Impl::XPluginContext_Impl( const Reference< ::com::sun::star::uno::XComponentContext >  & rxContext )
     111             :     : m_xContext( rxContext ),
     112           0 :       m_aEncoding( osl_getThreadTextEncoding() )
     113             : {
     114           0 : }
     115             : 
     116           0 : XPluginContext_Impl::~XPluginContext_Impl()
     117             : {
     118           0 : }
     119             : 
     120           0 : OUString XPluginContext_Impl::getValue( const Reference< ::com::sun::star::plugin::XPlugin > & /*plugin*/, ::com::sun::star::plugin::PluginVariable /*variable*/ )
     121             :     throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception )
     122             : {
     123           0 :     return OUString();
     124             : }
     125             : 
     126             : 
     127           0 : void XPluginContext_Impl::getURL(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const OUString& url, const OUString& target) throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception )
     128             : {
     129           0 :     Reference< XDesktop2 > xDesktop = Desktop::create(m_xContext);
     130             : 
     131           0 :     if(  target.isEmpty() )
     132             :     {
     133           0 :         INetURLObject aURL;
     134           0 :         aURL.SetSmartProtocol( INET_PROT_FILE );
     135           0 :         aURL.SetSmartURL( url );
     136             : 
     137           0 :         OUString aUrl = aURL.GetMainURL(INetURLObject::DECODE_TO_IURI);
     138             :         // the mimetype cannot be specified
     139           0 :         plugin->provideNewStream( OUString(),
     140             :                                   Reference< XActiveDataSource >(),
     141             :                                   aUrl,
     142           0 :                                   0, 0, aUrl.startsWith("file:") );
     143           0 :         return;
     144             :     }
     145             : 
     146           0 :     XPlugin_Impl* pPlugin = XPluginManager_Impl::getPluginImplementation( plugin );
     147             : 
     148           0 :     if( pPlugin )
     149             :     {
     150             :         try
     151             :         {
     152           0 :             ::com::sun::star::beans::PropertyValue aValue;
     153           0 :             aValue.Name     = "Referer";
     154           0 :             aValue.Value <<= pPlugin->getRefererURL();
     155             : 
     156           0 :             Sequence< ::com::sun::star::beans::PropertyValue > aArgs( &aValue, 1 );
     157             :             Reference< ::com::sun::star::lang::XComponent >  xComp =
     158           0 :                 xDesktop->loadComponentFromURL(
     159             :                                               url,
     160             :                                               target,
     161             :                                               ::com::sun::star::frame::FrameSearchFlag::PARENT          |
     162             :                                               ::com::sun::star::frame::FrameSearchFlag::SELF            |
     163             :                                               ::com::sun::star::frame::FrameSearchFlag::CHILDREN        |
     164             :                                               ::com::sun::star::frame::FrameSearchFlag::SIBLINGS        |
     165             :                                               ::com::sun::star::frame::FrameSearchFlag::TASKS           |
     166             :                                               ::com::sun::star::frame::FrameSearchFlag::CREATE,
     167             :                                               aArgs
     168           0 :                                               );
     169             :         }
     170           0 :         catch(...)
     171             :         {
     172           0 :             throw ::com::sun::star::plugin::PluginException();
     173             :         }
     174           0 :     }
     175             : }
     176             : 
     177           0 : void XPluginContext_Impl::getURLNotify(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const OUString& url, const OUString& target, const Reference< ::com::sun::star::lang::XEventListener > & listener )
     178             :     throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception )
     179             : {
     180           0 :     getURL( plugin, url, target );
     181           0 :     if( listener.is() )
     182           0 :         listener->disposing( ::com::sun::star::lang::EventObject() );
     183           0 : }
     184             : 
     185           0 : OUString XPluginContext_Impl::getUserAgent(const Reference< ::com::sun::star::plugin::XPlugin > & /*plugin*/)
     186             :     throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception )
     187             : {
     188           0 :     return OUString("Mozilla 3.0");
     189             : }
     190             : 
     191           0 : void XPluginContext_Impl::displayStatusText(const Reference< ::com::sun::star::plugin::XPlugin > & /*plugin*/, const OUString& /*message*/)
     192             :     throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception )
     193             : {
     194           0 : }
     195             : 
     196           0 : void XPluginContext_Impl::postURL(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const OUString& url, const OUString& target, const Sequence< sal_Int8 >& buf, sal_Bool file)
     197             :     throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception )
     198             : {
     199           0 :     Sequence< sal_Int8 > aBuf;
     200             : 
     201           0 :     if( file )
     202             :     {
     203           0 :         OUString aFileName( (char*)buf.getConstArray(), strlen((char*)buf.getConstArray()), m_aEncoding );
     204           0 :         INetURLObject aFilePath( aFileName );
     205           0 :         aFileName = aFilePath.PathToFileName();
     206           0 :         SvFileStream aStream( aFileName, STREAM_READ );
     207           0 :         if( aStream.IsOpen() )
     208             :         {
     209           0 :             sal_Int64 const nBytes = aStream.remainingSize();
     210           0 :             aBuf = Sequence<sal_Int8>( nBytes );
     211           0 :             aStream.Read( aBuf.getArray(), nBytes );
     212           0 :             aStream.Close();
     213           0 :             osl::FileBase::getFileURLFromSystemPath( aFileName, aFileName );
     214           0 :             osl::File::remove( aFileName );
     215           0 :         }
     216             :     }
     217             : 
     218           0 :     Reference< XDesktop2 > xDesktop = Desktop::create(m_xContext);
     219             : 
     220           0 :     XPlugin_Impl* pPlugin = XPluginManager_Impl::getPluginImplementation( plugin );
     221           0 :     if( pPlugin )
     222             :     {
     223             :         try
     224             :         {
     225           0 :             ::com::sun::star::beans::PropertyValue aValues[2];
     226           0 :             aValues[0].Name = "Referer";
     227           0 :             aValues[0].Value <<= pPlugin->getRefererURL();
     228             : 
     229           0 :             aValues[1].Name = "PostString";
     230           0 :             aValues[1].Value <<= OStringToOUString( (char*)( file ? aBuf : buf ).getConstArray(), m_aEncoding );
     231           0 :             Sequence< ::com::sun::star::beans::PropertyValue > aArgs( aValues, 2 );
     232             :             Reference< ::com::sun::star::lang::XComponent >  xComp =
     233           0 :                 xDesktop->loadComponentFromURL(
     234             :                                               url,
     235             :                                               target,
     236             :                                               ::com::sun::star::frame::FrameSearchFlag::PARENT          |
     237             :                                               ::com::sun::star::frame::FrameSearchFlag::SELF            |
     238             :                                               ::com::sun::star::frame::FrameSearchFlag::CHILDREN        |
     239             :                                               ::com::sun::star::frame::FrameSearchFlag::SIBLINGS        |
     240             :                                               ::com::sun::star::frame::FrameSearchFlag::TASKS           |
     241             :                                               ::com::sun::star::frame::FrameSearchFlag::CREATE,
     242             :                                               aArgs
     243           0 :                                               );
     244             :         }
     245           0 :         catch( ... )
     246             :         {
     247           0 :             throw ::com::sun::star::plugin::PluginException();
     248             :         }
     249           0 :     }
     250           0 : }
     251             : 
     252           0 : void XPluginContext_Impl::postURLNotify(const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const OUString& url, const OUString& target, const Sequence< sal_Int8 >& buf, sal_Bool file, const Reference< ::com::sun::star::lang::XEventListener > & listener )
     253             :     throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception )
     254             : {
     255           0 :     postURL( plugin, url, target, buf, file );
     256           0 :     if( listener.is() )
     257           0 :         listener->disposing( ::com::sun::star::lang::EventObject() );
     258           0 : }
     259             : 
     260           0 : void XPluginContext_Impl::newStream( const Reference< ::com::sun::star::plugin::XPlugin > & plugin, const OUString&, const OUString& target, const Reference< ::com::sun::star::io::XActiveDataSource > & source )
     261             :     throw( ::com::sun::star::plugin::PluginException, RuntimeException, std::exception )
     262             : {
     263           0 :     FileSink*  pNewSink = new FileSink( m_xContext, plugin, target, source );
     264           0 :     pNewSink->acquire();
     265           0 : }
     266             : 
     267             : 
     268             : 
     269           0 : FileSink::FileSink( const Reference< ::com::sun::star::uno::XComponentContext >  & rxContext, const Reference< ::com::sun::star::plugin::XPlugin > & plugin,
     270             :                     const OUString& target, const Reference< ::com::sun::star::io::XActiveDataSource > & source ) :
     271             :         m_xContext( rxContext ),
     272             :         m_xPlugin( plugin ),
     273           0 :         m_aTarget( target )
     274             : {
     275           0 :     osl::FileBase::createTempFile( 0, 0, &m_aFileName );
     276           0 :     OString aFile = OUStringToOString( m_aFileName, osl_getThreadTextEncoding() );
     277           0 :     fp = fopen( aFile.getStr() , "wb" );
     278             : 
     279           0 :     Reference< ::com::sun::star::io::XActiveDataControl >  xControl( source, UNO_QUERY );
     280             : 
     281           0 :     source->setOutputStream( Reference< ::com::sun::star::io::XOutputStream > ( this ) );
     282           0 :     if( xControl.is() )
     283           0 :         xControl->start();
     284           0 : }
     285             : 
     286           0 : FileSink::~FileSink()
     287             : {
     288           0 :     osl::File::remove( m_aFileName );
     289           0 : }
     290             : 
     291           0 : void FileSink::closeOutput() throw (RuntimeException, std::exception)
     292             : {
     293           0 :     if( fp )
     294           0 :         fclose( fp );
     295             : 
     296           0 :     Reference< XDesktop2 > xDesktop = Desktop::create(m_xContext);
     297           0 :     XPlugin_Impl* pPlugin = XPluginManager_Impl::getPluginImplementation( m_xPlugin );
     298             : 
     299           0 :     if( pPlugin )
     300             :     {
     301             :         try
     302             :         {
     303           0 :             ::com::sun::star::beans::PropertyValue aValue;
     304           0 :             aValue.Name = "Referer";
     305           0 :             aValue.Value <<= pPlugin->getRefererURL();
     306             : 
     307           0 :             Sequence< ::com::sun::star::beans::PropertyValue > aArgs( &aValue, 1 );
     308             :             Reference< ::com::sun::star::lang::XComponent >  xComp =
     309           0 :                 xDesktop->loadComponentFromURL(
     310             :                                               m_aFileName,
     311             :                                               m_aTarget,
     312             :                                               ::com::sun::star::frame::FrameSearchFlag::PARENT          |
     313             :                                               ::com::sun::star::frame::FrameSearchFlag::SELF            |
     314             :                                               ::com::sun::star::frame::FrameSearchFlag::CHILDREN        |
     315             :                                               ::com::sun::star::frame::FrameSearchFlag::SIBLINGS        |
     316             :                                               ::com::sun::star::frame::FrameSearchFlag::TASKS           |
     317             :                                               ::com::sun::star::frame::FrameSearchFlag::CREATE,
     318             :                                               aArgs
     319           0 :                                               );
     320             :         }
     321           0 :         catch( ... )
     322             :         {
     323             :         }
     324             :     }
     325           0 :     release();
     326           0 : }
     327             : 
     328           0 : void FileSink::writeBytes( const Sequence<sal_Int8>& Buffer ) throw(std::exception)
     329             : {
     330           0 :     if( fp )
     331             :     {
     332           0 :         size_t nItems = Buffer.getLength();
     333           0 :         bool bSuccess = (fwrite(Buffer.getConstArray(), 1, nItems, fp) == nItems);
     334             :         SAL_WARN_IF( !bSuccess, "extensions.plugin", "short write");
     335             :     }
     336           0 : }
     337             : 
     338           0 : void FileSink::flush() throw(std::exception)
     339             : {
     340           0 :     if( fp )
     341           0 :         fflush( fp );
     342           6 : }
     343             : 
     344             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10