1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
22 #include <sal/config.h>
28 #include <com/sun/star/bridge/XBridge.hpp>
29 #include <com/sun/star/lang/XComponent.hpp>
30 #include <com/sun/star/uno/Reference.hxx>
31 #include <cppuhelper/implbase.hxx>
32 #include <osl/conditn.hxx>
33 #include <osl/mutex.hxx>
34 #include <rtl/ref.hxx>
35 #include <rtl/ustring.hxx>
36 #include <sal/types.h>
37 #include <uno/environment.hxx>
38 #include <uno/mapping.hxx>
39 #include <uno/threadpool.h>
41 #include "outgoingrequest.hxx"
42 #include "outgoingrequests.hxx"
51 namespace com::sun::star
{
52 namespace bridge
{ class XInstanceProvider
; }
53 namespace connection
{ class XConnection
; }
54 namespace lang
{ class XEventListener
; }
57 class TypeDescription
;
58 class UnoInterfaceReference
;
62 namespace rtl
{ class ByteSequence
; }
67 public cppu::WeakImplHelper
<
68 com::sun::star::bridge::XBridge
, com::sun::star::lang::XComponent
>
72 rtl::Reference
< BridgeFactory
> const & factory
,
73 OUString
const & name
,
74 com::sun::star::uno::Reference
<
75 com::sun::star::connection::XConnection
> const & connection
,
76 com::sun::star::uno::Reference
<
77 com::sun::star::bridge::XInstanceProvider
> const & provider
);
81 // Internally waits for all incoming and outgoing remote calls to terminate,
82 // so must not be called from within such a call; when final is true, also
83 // joins all remaining threads (reader, writer, and worker threads from the
84 // thread pool), so must not be called with final set to true from such a
86 void terminate(bool final
);
88 const com::sun::star::uno::Reference
< com::sun::star::connection::XConnection
>&
89 getConnection() const { return connection_
;}
91 const com::sun::star::uno::Reference
< com::sun::star::bridge::XInstanceProvider
>&
92 getProvider() const { return provider_
;}
94 com::sun::star::uno::Mapping
& getCppToBinaryMapping() { return cppToBinaryMapping_
;}
96 BinaryAny
mapCppToBinaryAny(com::sun::star::uno::Any
const & cppAny
);
98 uno_ThreadPool
getThreadPool();
100 rtl::Reference
< Writer
> getWriter();
102 com::sun::star::uno::UnoInterfaceReference
registerIncomingInterface(
103 OUString
const & oid
,
104 com::sun::star::uno::TypeDescription
const & type
);
106 OUString
registerOutgoingInterface(
107 com::sun::star::uno::UnoInterfaceReference
const & object
,
108 com::sun::star::uno::TypeDescription
const & type
);
110 com::sun::star::uno::UnoInterfaceReference
findStub(
111 OUString
const & oid
,
112 com::sun::star::uno::TypeDescription
const & type
);
115 OUString
const & oid
,
116 com::sun::star::uno::TypeDescription
const & type
);
118 void resurrectProxy(Proxy
& proxy
);
120 void revokeProxy(Proxy
& proxy
);
122 void freeProxy(Proxy
& proxy
);
124 void incrementCalls(bool normalCall
) throw ();
126 void decrementCalls();
128 void incrementActiveCalls() throw ();
130 void decrementActiveCalls() throw ();
133 OUString
const & oid
,
134 com::sun::star::uno::TypeDescription
const & member
, bool setter
,
135 std::vector
< BinaryAny
> const & inArguments
, BinaryAny
* returnValue
,
136 std::vector
< BinaryAny
> * outArguments
);
138 // Only called from reader_ thread:
139 void sendRequestChangeRequest();
141 // Only called from reader_ thread:
142 void handleRequestChangeReply(
143 bool exception
, BinaryAny
const & returnValue
);
145 // Only called from reader_ thread:
146 void handleCommitChangeReply(bool exception
, BinaryAny
const & returnValue
);
148 // Only called from reader_ thread:
149 void handleRequestChangeRequest(
150 rtl::ByteSequence
const & tid
,
151 std::vector
< BinaryAny
> const & inArguments
);
153 // Only called from reader_ thread:
154 void handleCommitChangeRequest(
155 rtl::ByteSequence
const & tid
,
156 std::vector
< BinaryAny
> const & inArguments
);
158 OutgoingRequest
lastOutgoingRequest(rtl::ByteSequence
const & tid
);
160 bool isProtocolPropertiesRequest(
161 std::u16string_view oid
,
162 com::sun::star::uno::TypeDescription
const & type
) const;
164 void setCurrentContextMode();
166 bool isCurrentContextMode();
169 Bridge(const Bridge
&) = delete;
170 Bridge
& operator=(const Bridge
&) = delete;
172 virtual ~Bridge() override
;
174 virtual com::sun::star::uno::Reference
< com::sun::star::uno::XInterface
>
175 SAL_CALL
getInstance(OUString
const & sInstanceName
) override
;
177 virtual OUString SAL_CALL
getName() override
;
179 virtual OUString SAL_CALL
getDescription() override
;
181 virtual void SAL_CALL
dispose() override
;
183 virtual void SAL_CALL
addEventListener(
184 com::sun::star::uno::Reference
< com::sun::star::lang::XEventListener
>
185 const & xListener
) override
;
187 virtual void SAL_CALL
removeEventListener(
188 com::sun::star::uno::Reference
< com::sun::star::lang::XEventListener
>
189 const & aListener
) override
;
191 // Only called from reader_ thread:
192 void sendCommitChangeRequest();
194 // Only called from reader_ thread:
195 void sendProtPropRequest(
196 OutgoingRequest::Kind kind
,
197 std::vector
< BinaryAny
> const & inArguments
);
199 void makeReleaseCall(
200 OUString
const & oid
,
201 com::sun::star::uno::TypeDescription
const & type
);
204 rtl::ByteSequence
const & tid
, OUString
const & oid
,
205 com::sun::star::uno::TypeDescription
const & type
,
206 com::sun::star::uno::TypeDescription
const & member
,
207 std::vector
< BinaryAny
> const & inArguments
);
209 void throwException(bool exception
, BinaryAny
const & value
);
211 com::sun::star::uno::Any
mapBinaryToCppAny(BinaryAny
const & binaryAny
);
213 bool becameUnused() const;
215 void terminateWhenUnused(bool unused
);
217 // Must only be called with mutex_ locked:
218 void checkDisposed();
222 com::sun::star::uno::Reference
<
223 com::sun::star::lang::XEventListener
> >
228 typedef std::map
< com::sun::star::uno::TypeDescription
, SubStub
> Stub
;
230 typedef std::map
< OUString
, Stub
> Stubs
;
232 enum State
{ STATE_INITIAL
, STATE_STARTED
, STATE_TERMINATED
, STATE_FINAL
};
235 MODE_REQUESTED
, MODE_REPLY_MINUS1
, MODE_REPLY_0
, MODE_REPLY_1
,
236 MODE_WAIT
, MODE_NORMAL
, MODE_NORMAL_WAIT
};
238 rtl::Reference
< BridgeFactory
> factory_
;
240 com::sun::star::uno::Reference
< com::sun::star::connection::XConnection
>
242 com::sun::star::uno::Reference
< com::sun::star::bridge::XInstanceProvider
>
244 com::sun::star::uno::Environment binaryUno_
;
245 com::sun::star::uno::Mapping cppToBinaryMapping_
;
246 com::sun::star::uno::Mapping binaryToCppMapping_
;
247 rtl::ByteSequence protPropTid_
;
248 OUString protPropOid_
;
249 com::sun::star::uno::TypeDescription protPropType_
;
250 com::sun::star::uno::TypeDescription protPropRequest_
;
251 com::sun::star::uno::TypeDescription protPropCommit_
;
252 OutgoingRequests outgoingRequests_
;
253 osl::Condition passive_
;
254 // to guarantee that passive_ is eventually set (to avoid deadlock, see
255 // dispose), activeCalls_ only counts those calls for which it can be
256 // guaranteed that incrementActiveCalls is indeed followed by
257 // decrementActiveCalls, without an intervening exception
258 osl::Condition terminated_
;
262 Listeners listeners_
;
263 uno_ThreadPool threadPool_
;
264 rtl::Reference
< Writer
> writer_
;
265 rtl::Reference
< Reader
> reader_
;
266 bool currentContextMode_
;
268 std::size_t proxies_
;
271 std::size_t activeCalls_
;
273 // Only accessed from reader_ thread:
280 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */