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>
29 #include <com/sun/star/bridge/XBridge.hpp>
30 #include <com/sun/star/lang/XComponent.hpp>
31 #include <com/sun/star/uno/Reference.hxx>
32 #include <cppuhelper/implbase.hxx>
33 #include <osl/conditn.hxx>
34 #include <osl/mutex.hxx>
35 #include <rtl/ref.hxx>
36 #include <rtl/ustring.hxx>
37 #include <sal/types.h>
38 #include <uno/environment.hxx>
39 #include <uno/mapping.hxx>
40 #include <uno/threadpool.h>
42 #include "outgoingrequest.hxx"
43 #include "outgoingrequests.hxx"
52 namespace com::sun::star
{
53 namespace bridge
{ class XInstanceProvider
; }
54 namespace connection
{ class XConnection
; }
55 namespace lang
{ class XEventListener
; }
58 class TypeDescription
;
59 class UnoInterfaceReference
;
63 namespace rtl
{ class ByteSequence
; }
68 public cppu::WeakImplHelper
<
69 com::sun::star::bridge::XBridge
, com::sun::star::lang::XComponent
>
73 rtl::Reference
< BridgeFactory
> const & factory
,
75 com::sun::star::uno::Reference
<
76 com::sun::star::connection::XConnection
> const & connection
,
77 com::sun::star::uno::Reference
<
78 com::sun::star::bridge::XInstanceProvider
> provider
);
82 // Internally waits for all incoming and outgoing remote calls to terminate,
83 // so must not be called from within such a call; when final is true, also
84 // joins all remaining threads (reader, writer, and worker threads from the
85 // thread pool), so must not be called with final set to true from such a
87 void terminate(bool final
);
89 const com::sun::star::uno::Reference
< com::sun::star::connection::XConnection
>&
90 getConnection() const { return connection_
;}
92 const com::sun::star::uno::Reference
< com::sun::star::bridge::XInstanceProvider
>&
93 getProvider() const { return provider_
;}
95 com::sun::star::uno::Mapping
& getCppToBinaryMapping() { return cppToBinaryMapping_
;}
97 BinaryAny
mapCppToBinaryAny(com::sun::star::uno::Any
const & cppAny
);
99 uno_ThreadPool
getThreadPool();
101 rtl::Reference
< Writer
> getWriter();
103 com::sun::star::uno::UnoInterfaceReference
registerIncomingInterface(
104 OUString
const & oid
,
105 com::sun::star::uno::TypeDescription
const & type
);
107 OUString
registerOutgoingInterface(
108 com::sun::star::uno::UnoInterfaceReference
const & object
,
109 com::sun::star::uno::TypeDescription
const & type
);
111 com::sun::star::uno::UnoInterfaceReference
findStub(
112 OUString
const & oid
,
113 com::sun::star::uno::TypeDescription
const & type
);
116 OUString
const & oid
,
117 com::sun::star::uno::TypeDescription
const & type
);
119 void resurrectProxy(Proxy
& proxy
);
121 void revokeProxy(Proxy
& proxy
);
123 void freeProxy(Proxy
& proxy
);
125 void incrementCalls(bool normalCall
) noexcept
;
127 void decrementCalls();
129 void incrementActiveCalls() noexcept
;
131 void decrementActiveCalls() noexcept
;
134 OUString
const & oid
,
135 com::sun::star::uno::TypeDescription
const & member
, bool setter
,
136 std::vector
< BinaryAny
>&& inArguments
, BinaryAny
* returnValue
,
137 std::vector
< BinaryAny
> * outArguments
);
139 // Only called from reader_ thread:
140 void sendRequestChangeRequest();
142 // Only called from reader_ thread:
143 void handleRequestChangeReply(
144 bool exception
, BinaryAny
const & returnValue
);
146 // Only called from reader_ thread:
147 void handleCommitChangeReply(bool exception
, BinaryAny
const & returnValue
);
149 // Only called from reader_ thread:
150 void handleRequestChangeRequest(
151 rtl::ByteSequence
const & tid
,
152 std::vector
< BinaryAny
> const & inArguments
);
154 // Only called from reader_ thread:
155 void handleCommitChangeRequest(
156 rtl::ByteSequence
const & tid
,
157 std::vector
< BinaryAny
> const & inArguments
);
159 OutgoingRequest
lastOutgoingRequest(rtl::ByteSequence
const & tid
);
161 bool isProtocolPropertiesRequest(
162 std::u16string_view oid
,
163 com::sun::star::uno::TypeDescription
const & type
) const;
165 void setCurrentContextMode();
167 bool isCurrentContextMode();
170 Bridge(const Bridge
&) = delete;
171 Bridge
& operator=(const Bridge
&) = delete;
173 virtual ~Bridge() override
;
175 virtual com::sun::star::uno::Reference
< com::sun::star::uno::XInterface
>
176 SAL_CALL
getInstance(OUString
const & sInstanceName
) override
;
178 virtual OUString SAL_CALL
getName() override
;
180 virtual OUString SAL_CALL
getDescription() override
;
182 virtual void SAL_CALL
dispose() override
;
184 virtual void SAL_CALL
addEventListener(
185 com::sun::star::uno::Reference
< com::sun::star::lang::XEventListener
>
186 const & xListener
) override
;
188 virtual void SAL_CALL
removeEventListener(
189 com::sun::star::uno::Reference
< com::sun::star::lang::XEventListener
>
190 const & aListener
) override
;
192 // Only called from reader_ thread:
193 void sendCommitChangeRequest();
195 // Only called from reader_ thread:
196 void sendProtPropRequest(
197 OutgoingRequest::Kind kind
,
198 std::vector
< BinaryAny
> const & inArguments
);
200 void makeReleaseCall(
201 OUString
const & oid
,
202 com::sun::star::uno::TypeDescription
const & type
);
205 rtl::ByteSequence
const & tid
, OUString
const & oid
,
206 com::sun::star::uno::TypeDescription
const & type
,
207 com::sun::star::uno::TypeDescription
const & member
,
208 std::vector
< BinaryAny
>&& inArguments
);
210 void throwException(bool exception
, BinaryAny
const & value
);
212 com::sun::star::uno::Any
mapBinaryToCppAny(BinaryAny
const & binaryAny
);
214 bool becameUnused() const;
216 void terminateWhenUnused(bool unused
);
218 // Must only be called with mutex_ locked:
219 void checkDisposed();
223 com::sun::star::uno::Reference
<
224 com::sun::star::lang::XEventListener
> >
229 typedef std::map
< com::sun::star::uno::TypeDescription
, SubStub
> Stub
;
231 typedef std::map
< OUString
, Stub
> Stubs
;
233 enum State
{ STATE_INITIAL
, STATE_STARTED
, STATE_TERMINATED
, STATE_FINAL
};
236 MODE_REQUESTED
, MODE_REPLY_MINUS1
, MODE_REPLY_0
, MODE_REPLY_1
,
237 MODE_WAIT
, MODE_NORMAL
, MODE_NORMAL_WAIT
};
239 rtl::Reference
< BridgeFactory
> factory_
;
241 com::sun::star::uno::Reference
< com::sun::star::connection::XConnection
>
243 com::sun::star::uno::Reference
< com::sun::star::bridge::XInstanceProvider
>
245 com::sun::star::uno::Environment binaryUno_
;
246 com::sun::star::uno::Mapping cppToBinaryMapping_
;
247 com::sun::star::uno::Mapping binaryToCppMapping_
;
248 rtl::ByteSequence protPropTid_
;
249 OUString protPropOid_
;
250 com::sun::star::uno::TypeDescription protPropType_
;
251 com::sun::star::uno::TypeDescription protPropRequest_
;
252 com::sun::star::uno::TypeDescription protPropCommit_
;
253 OutgoingRequests outgoingRequests_
;
254 osl::Condition passive_
;
255 // to guarantee that passive_ is eventually set (to avoid deadlock, see
256 // dispose), activeCalls_ only counts those calls for which it can be
257 // guaranteed that incrementActiveCalls is indeed followed by
258 // decrementActiveCalls, without an intervening exception
259 osl::Condition terminated_
;
263 Listeners listeners_
;
264 uno_ThreadPool threadPool_
;
265 rtl::Reference
< Writer
> writer_
;
266 rtl::Reference
< Reader
> reader_
;
267 bool currentContextMode_
;
269 std::size_t proxies_
;
272 std::size_t activeCalls_
;
274 // Only accessed from reader_ thread:
281 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */