1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2011 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #ifndef INCLUDED_BINARYURP_SOURCE_BRIDGE_HXX 29*cdf0e10cSrcweir #define INCLUDED_BINARYURP_SOURCE_BRIDGE_HXX 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include "sal/config.h" 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir #include <cstddef> 34*cdf0e10cSrcweir #include <list> 35*cdf0e10cSrcweir #include <map> 36*cdf0e10cSrcweir #include <vector> 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir #include "boost/noncopyable.hpp" 39*cdf0e10cSrcweir #include "com/sun/star/bridge/XBridge.hpp" 40*cdf0e10cSrcweir #include "com/sun/star/lang/XComponent.hpp" 41*cdf0e10cSrcweir #include "com/sun/star/uno/Reference.hxx" 42*cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp" 43*cdf0e10cSrcweir #include "cppuhelper/implbase2.hxx" 44*cdf0e10cSrcweir #include "osl/conditn.hxx" 45*cdf0e10cSrcweir #include "osl/mutex.hxx" 46*cdf0e10cSrcweir #include "rtl/ref.hxx" 47*cdf0e10cSrcweir #include "rtl/ustring.hxx" 48*cdf0e10cSrcweir #include "sal/types.h" 49*cdf0e10cSrcweir #include "uno/environment.hxx" 50*cdf0e10cSrcweir #include "uno/mapping.hxx" 51*cdf0e10cSrcweir #include "uno/threadpool.h" 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir #include "outgoingrequest.hxx" 54*cdf0e10cSrcweir #include "outgoingrequests.hxx" 55*cdf0e10cSrcweir #include "writer.hxx" 56*cdf0e10cSrcweir 57*cdf0e10cSrcweir namespace binaryurp { 58*cdf0e10cSrcweir class BinaryAny; 59*cdf0e10cSrcweir class BridgeFactory; 60*cdf0e10cSrcweir class Proxy; 61*cdf0e10cSrcweir class Reader; 62*cdf0e10cSrcweir } 63*cdf0e10cSrcweir namespace com { namespace sun { namespace star { 64*cdf0e10cSrcweir namespace bridge { class XInstanceProvider; } 65*cdf0e10cSrcweir namespace connection { class XConnection; } 66*cdf0e10cSrcweir namespace lang { class XEventListener; } 67*cdf0e10cSrcweir namespace uno { 68*cdf0e10cSrcweir class Any; 69*cdf0e10cSrcweir class TypeDescription; 70*cdf0e10cSrcweir class UnoInterfaceReference; 71*cdf0e10cSrcweir class XInterface; 72*cdf0e10cSrcweir } 73*cdf0e10cSrcweir } } } 74*cdf0e10cSrcweir namespace rtl { class ByteSequence; } 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir namespace binaryurp { 77*cdf0e10cSrcweir 78*cdf0e10cSrcweir class Bridge: 79*cdf0e10cSrcweir public cppu::WeakImplHelper2< 80*cdf0e10cSrcweir com::sun::star::bridge::XBridge, com::sun::star::lang::XComponent >, 81*cdf0e10cSrcweir private boost::noncopyable 82*cdf0e10cSrcweir { 83*cdf0e10cSrcweir public: 84*cdf0e10cSrcweir Bridge( 85*cdf0e10cSrcweir rtl::Reference< BridgeFactory > const & factory, 86*cdf0e10cSrcweir rtl::OUString const & name, 87*cdf0e10cSrcweir com::sun::star::uno::Reference< 88*cdf0e10cSrcweir com::sun::star::connection::XConnection > const & connection, 89*cdf0e10cSrcweir com::sun::star::uno::Reference< 90*cdf0e10cSrcweir com::sun::star::bridge::XInstanceProvider > const & provider); 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir void start(); 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir // Internally waits for all incoming and outgoing remote calls to terminate, 95*cdf0e10cSrcweir // so must not be called from within such a call: 96*cdf0e10cSrcweir void terminate(); 97*cdf0e10cSrcweir 98*cdf0e10cSrcweir com::sun::star::uno::Reference< com::sun::star::connection::XConnection > 99*cdf0e10cSrcweir getConnection() const; 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir com::sun::star::uno::Reference< com::sun::star::bridge::XInstanceProvider > 102*cdf0e10cSrcweir getProvider() const; 103*cdf0e10cSrcweir 104*cdf0e10cSrcweir com::sun::star::uno::Mapping & getCppToBinaryMapping(); 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir BinaryAny mapCppToBinaryAny(com::sun::star::uno::Any const & cppAny); 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir uno_ThreadPool getThreadPool() const; 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir rtl::Reference< Writer > getWriter(); 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir com::sun::star::uno::UnoInterfaceReference registerIncomingInterface( 113*cdf0e10cSrcweir rtl::OUString const & oid, 114*cdf0e10cSrcweir com::sun::star::uno::TypeDescription const & type); 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir rtl::OUString registerOutgoingInterface( 117*cdf0e10cSrcweir com::sun::star::uno::UnoInterfaceReference const & object, 118*cdf0e10cSrcweir com::sun::star::uno::TypeDescription const & type); 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir com::sun::star::uno::UnoInterfaceReference findStub( 121*cdf0e10cSrcweir rtl::OUString const & oid, 122*cdf0e10cSrcweir com::sun::star::uno::TypeDescription const & type); 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir void releaseStub( 125*cdf0e10cSrcweir rtl::OUString const & oid, 126*cdf0e10cSrcweir com::sun::star::uno::TypeDescription const & type); 127*cdf0e10cSrcweir 128*cdf0e10cSrcweir void resurrectProxy(Proxy & proxy); 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir void revokeProxy(Proxy & proxy); 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir void freeProxy(Proxy & proxy); 133*cdf0e10cSrcweir 134*cdf0e10cSrcweir void incrementCalls(bool normalCall) throw (); 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir void decrementCalls(); 137*cdf0e10cSrcweir 138*cdf0e10cSrcweir void incrementActiveCalls() throw (); 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir void decrementActiveCalls() throw (); 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir bool makeCall( 143*cdf0e10cSrcweir rtl::OUString const & oid, 144*cdf0e10cSrcweir com::sun::star::uno::TypeDescription const & member, bool setter, 145*cdf0e10cSrcweir std::vector< BinaryAny > const & inArguments, BinaryAny * returnValue, 146*cdf0e10cSrcweir std::vector< BinaryAny > * outArguments); 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir // Only called from reader_ thread: 149*cdf0e10cSrcweir void sendRequestChangeRequest(); 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir // Only called from reader_ thread: 152*cdf0e10cSrcweir void handleRequestChangeReply( 153*cdf0e10cSrcweir bool exception, BinaryAny const & returnValue); 154*cdf0e10cSrcweir 155*cdf0e10cSrcweir // Only called from reader_ thread: 156*cdf0e10cSrcweir void handleCommitChangeReply(bool exception, BinaryAny const & returnValue); 157*cdf0e10cSrcweir 158*cdf0e10cSrcweir // Only called from reader_ thread: 159*cdf0e10cSrcweir void handleRequestChangeRequest( 160*cdf0e10cSrcweir rtl::ByteSequence const & tid, 161*cdf0e10cSrcweir std::vector< BinaryAny > const & inArguments); 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir // Only called from reader_ thread: 164*cdf0e10cSrcweir void handleCommitChangeRequest( 165*cdf0e10cSrcweir rtl::ByteSequence const & tid, 166*cdf0e10cSrcweir std::vector< BinaryAny > const & inArguments); 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir OutgoingRequest lastOutgoingRequest(rtl::ByteSequence const & tid); 169*cdf0e10cSrcweir 170*cdf0e10cSrcweir bool isProtocolPropertiesRequest( 171*cdf0e10cSrcweir rtl::OUString const & oid, 172*cdf0e10cSrcweir com::sun::star::uno::TypeDescription const & type) const; 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir void setCurrentContextMode(); 175*cdf0e10cSrcweir 176*cdf0e10cSrcweir bool isCurrentContextMode(); 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir private: 179*cdf0e10cSrcweir virtual ~Bridge(); 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir virtual com::sun::star::uno::Reference< com::sun::star::uno::XInterface > 182*cdf0e10cSrcweir SAL_CALL getInstance(rtl::OUString const & sInstanceName) 183*cdf0e10cSrcweir throw (com::sun::star::uno::RuntimeException); 184*cdf0e10cSrcweir 185*cdf0e10cSrcweir virtual rtl::OUString SAL_CALL getName() 186*cdf0e10cSrcweir throw (com::sun::star::uno::RuntimeException); 187*cdf0e10cSrcweir 188*cdf0e10cSrcweir virtual rtl::OUString SAL_CALL getDescription() 189*cdf0e10cSrcweir throw (com::sun::star::uno::RuntimeException); 190*cdf0e10cSrcweir 191*cdf0e10cSrcweir virtual void SAL_CALL dispose() 192*cdf0e10cSrcweir throw (com::sun::star::uno::RuntimeException); 193*cdf0e10cSrcweir 194*cdf0e10cSrcweir virtual void SAL_CALL addEventListener( 195*cdf0e10cSrcweir com::sun::star::uno::Reference< com::sun::star::lang::XEventListener > 196*cdf0e10cSrcweir const & xListener) 197*cdf0e10cSrcweir throw (com::sun::star::uno::RuntimeException); 198*cdf0e10cSrcweir 199*cdf0e10cSrcweir virtual void SAL_CALL removeEventListener( 200*cdf0e10cSrcweir com::sun::star::uno::Reference< com::sun::star::lang::XEventListener > 201*cdf0e10cSrcweir const & aListener) 202*cdf0e10cSrcweir throw (com::sun::star::uno::RuntimeException); 203*cdf0e10cSrcweir 204*cdf0e10cSrcweir // Only called from reader_ thread: 205*cdf0e10cSrcweir void sendCommitChangeRequest(); 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir // Only called from reader_ thread: 208*cdf0e10cSrcweir void sendProtPropRequest( 209*cdf0e10cSrcweir OutgoingRequest::Kind kind, 210*cdf0e10cSrcweir std::vector< BinaryAny > const & inArguments); 211*cdf0e10cSrcweir 212*cdf0e10cSrcweir void makeReleaseCall( 213*cdf0e10cSrcweir rtl::OUString const & oid, 214*cdf0e10cSrcweir com::sun::star::uno::TypeDescription const & type); 215*cdf0e10cSrcweir 216*cdf0e10cSrcweir void sendRequest( 217*cdf0e10cSrcweir rtl::ByteSequence const & tid, rtl::OUString const & oid, 218*cdf0e10cSrcweir com::sun::star::uno::TypeDescription const & type, 219*cdf0e10cSrcweir com::sun::star::uno::TypeDescription const & member, 220*cdf0e10cSrcweir std::vector< BinaryAny > const & inArguments); 221*cdf0e10cSrcweir 222*cdf0e10cSrcweir void throwException(bool exception, BinaryAny const & value); 223*cdf0e10cSrcweir 224*cdf0e10cSrcweir com::sun::star::uno::Any mapBinaryToCppAny(BinaryAny const & binaryAny); 225*cdf0e10cSrcweir 226*cdf0e10cSrcweir bool becameUnused() const; 227*cdf0e10cSrcweir 228*cdf0e10cSrcweir void terminateWhenUnused(bool unused); 229*cdf0e10cSrcweir 230*cdf0e10cSrcweir typedef 231*cdf0e10cSrcweir std::list< 232*cdf0e10cSrcweir com::sun::star::uno::Reference< 233*cdf0e10cSrcweir com::sun::star::lang::XEventListener > > 234*cdf0e10cSrcweir Listeners; 235*cdf0e10cSrcweir 236*cdf0e10cSrcweir struct SubStub; 237*cdf0e10cSrcweir 238*cdf0e10cSrcweir typedef std::map< com::sun::star::uno::TypeDescription, SubStub > Stub; 239*cdf0e10cSrcweir 240*cdf0e10cSrcweir typedef std::map< rtl::OUString, Stub > Stubs; 241*cdf0e10cSrcweir 242*cdf0e10cSrcweir enum Mode { 243*cdf0e10cSrcweir MODE_REQUESTED, MODE_REPLY_MINUS1, MODE_REPLY_0, MODE_REPLY_1, 244*cdf0e10cSrcweir MODE_WAIT, MODE_NORMAL, MODE_NORMAL_WAIT }; 245*cdf0e10cSrcweir 246*cdf0e10cSrcweir rtl::Reference< BridgeFactory > factory_; 247*cdf0e10cSrcweir rtl::OUString name_; 248*cdf0e10cSrcweir com::sun::star::uno::Reference< com::sun::star::connection::XConnection > 249*cdf0e10cSrcweir connection_; 250*cdf0e10cSrcweir com::sun::star::uno::Reference< com::sun::star::bridge::XInstanceProvider > 251*cdf0e10cSrcweir provider_; 252*cdf0e10cSrcweir com::sun::star::uno::Environment binaryUno_; 253*cdf0e10cSrcweir com::sun::star::uno::Mapping cppToBinaryMapping_; 254*cdf0e10cSrcweir com::sun::star::uno::Mapping binaryToCppMapping_; 255*cdf0e10cSrcweir rtl::ByteSequence protPropTid_; 256*cdf0e10cSrcweir rtl::OUString protPropOid_; 257*cdf0e10cSrcweir com::sun::star::uno::TypeDescription protPropType_; 258*cdf0e10cSrcweir com::sun::star::uno::TypeDescription protPropRequest_; 259*cdf0e10cSrcweir com::sun::star::uno::TypeDescription protPropCommit_; 260*cdf0e10cSrcweir uno_ThreadPool threadPool_; 261*cdf0e10cSrcweir OutgoingRequests outgoingRequests_; 262*cdf0e10cSrcweir 263*cdf0e10cSrcweir osl::Mutex mutex_; 264*cdf0e10cSrcweir Listeners listeners_; 265*cdf0e10cSrcweir rtl::Reference< Writer > writer_; 266*cdf0e10cSrcweir rtl::Reference< Reader > reader_; 267*cdf0e10cSrcweir bool currentContextMode_; 268*cdf0e10cSrcweir Stubs stubs_; 269*cdf0e10cSrcweir std::size_t proxies_; 270*cdf0e10cSrcweir std::size_t calls_; 271*cdf0e10cSrcweir bool normalCall_; 272*cdf0e10cSrcweir std::size_t activeCalls_; 273*cdf0e10cSrcweir osl::Condition passive_; 274*cdf0e10cSrcweir // to guarantee that passive_ is eventually set (to avoid deadlock, see 275*cdf0e10cSrcweir // dispose), activeCalls_ only counts those calls for which it can be 276*cdf0e10cSrcweir // guaranteed that incrementActiveCalls is indeed followed by 277*cdf0e10cSrcweir // decrementActiveCalls, without an intervening exception 278*cdf0e10cSrcweir bool terminated_; 279*cdf0e10cSrcweir 280*cdf0e10cSrcweir // Only accessed from reader_ thread: 281*cdf0e10cSrcweir Mode mode_; 282*cdf0e10cSrcweir sal_Int32 random_; 283*cdf0e10cSrcweir }; 284*cdf0e10cSrcweir 285*cdf0e10cSrcweir } 286*cdf0e10cSrcweir 287*cdf0e10cSrcweir #endif 288