Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / binaryurp / source / bridge.hxx
bloba258f331639e0f3ac8377a6491cd21d6dfe3eb24
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #ifndef INCLUDED_BINARYURP_SOURCE_BRIDGE_HXX
21 #define INCLUDED_BINARYURP_SOURCE_BRIDGE_HXX
23 #include <sal/config.h>
25 #include <cstddef>
26 #include <map>
27 #include <vector>
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 <com/sun/star/uno/RuntimeException.hpp>
33 #include <cppuhelper/implbase.hxx>
34 #include <osl/conditn.hxx>
35 #include <osl/mutex.hxx>
36 #include <rtl/ref.hxx>
37 #include <rtl/ustring.hxx>
38 #include <sal/types.h>
39 #include <uno/environment.hxx>
40 #include <uno/mapping.hxx>
41 #include <uno/threadpool.h>
43 #include "outgoingrequest.hxx"
44 #include "outgoingrequests.hxx"
45 #include "writer.hxx"
47 namespace binaryurp {
48 class BinaryAny;
49 class BridgeFactory;
50 class Proxy;
51 class Reader;
53 namespace com { namespace sun { namespace star {
54 namespace bridge { class XInstanceProvider; }
55 namespace connection { class XConnection; }
56 namespace lang { class XEventListener; }
57 namespace uno {
58 class Any;
59 class TypeDescription;
60 class UnoInterfaceReference;
61 class XInterface;
63 } } }
64 namespace rtl { class ByteSequence; }
66 namespace binaryurp {
68 class Bridge:
69 public cppu::WeakImplHelper<
70 com::sun::star::bridge::XBridge, com::sun::star::lang::XComponent >
72 public:
73 Bridge(
74 rtl::Reference< BridgeFactory > const & factory,
75 OUString const & name,
76 com::sun::star::uno::Reference<
77 com::sun::star::connection::XConnection > const & connection,
78 com::sun::star::uno::Reference<
79 com::sun::star::bridge::XInstanceProvider > const & provider);
81 void start();
83 // Internally waits for all incoming and outgoing remote calls to terminate,
84 // so must not be called from within such a call; when final is true, also
85 // joins all remaining threads (reader, writer, and worker threads from the
86 // thread pool), so must not be called with final set to true from such a
87 // thread:
88 void terminate(bool final);
90 const com::sun::star::uno::Reference< com::sun::star::connection::XConnection >&
91 getConnection() const { return connection_;}
93 const com::sun::star::uno::Reference< com::sun::star::bridge::XInstanceProvider >&
94 getProvider() const { return provider_;}
96 com::sun::star::uno::Mapping & getCppToBinaryMapping() { return cppToBinaryMapping_;}
98 BinaryAny mapCppToBinaryAny(com::sun::star::uno::Any const & cppAny);
100 uno_ThreadPool getThreadPool();
102 rtl::Reference< Writer > getWriter();
104 com::sun::star::uno::UnoInterfaceReference registerIncomingInterface(
105 OUString const & oid,
106 com::sun::star::uno::TypeDescription const & type);
108 OUString registerOutgoingInterface(
109 com::sun::star::uno::UnoInterfaceReference const & object,
110 com::sun::star::uno::TypeDescription const & type);
112 com::sun::star::uno::UnoInterfaceReference findStub(
113 OUString const & oid,
114 com::sun::star::uno::TypeDescription const & type);
116 void releaseStub(
117 OUString const & oid,
118 com::sun::star::uno::TypeDescription const & type);
120 void resurrectProxy(Proxy & proxy);
122 void revokeProxy(Proxy & proxy);
124 void freeProxy(Proxy & proxy);
126 void incrementCalls(bool normalCall) throw ();
128 void decrementCalls();
130 void incrementActiveCalls() throw ();
132 void decrementActiveCalls() throw ();
134 bool makeCall(
135 OUString const & oid,
136 com::sun::star::uno::TypeDescription const & member, bool setter,
137 std::vector< BinaryAny > const & inArguments, BinaryAny * returnValue,
138 std::vector< BinaryAny > * outArguments);
140 // Only called from reader_ thread:
141 void sendRequestChangeRequest();
143 // Only called from reader_ thread:
144 void handleRequestChangeReply(
145 bool exception, BinaryAny const & returnValue);
147 // Only called from reader_ thread:
148 void handleCommitChangeReply(bool exception, BinaryAny const & returnValue);
150 // Only called from reader_ thread:
151 void handleRequestChangeRequest(
152 rtl::ByteSequence const & tid,
153 std::vector< BinaryAny > const & inArguments);
155 // Only called from reader_ thread:
156 void handleCommitChangeRequest(
157 rtl::ByteSequence const & tid,
158 std::vector< BinaryAny > const & inArguments);
160 OutgoingRequest lastOutgoingRequest(rtl::ByteSequence const & tid);
162 bool isProtocolPropertiesRequest(
163 OUString const & oid,
164 com::sun::star::uno::TypeDescription const & type) const;
166 void setCurrentContextMode();
168 bool isCurrentContextMode();
170 private:
171 Bridge(const Bridge&) = delete;
172 Bridge& operator=(const Bridge&) = delete;
174 virtual ~Bridge() override;
176 virtual com::sun::star::uno::Reference< com::sun::star::uno::XInterface >
177 SAL_CALL getInstance(OUString const & sInstanceName) override;
179 virtual OUString SAL_CALL getName() override;
181 virtual OUString SAL_CALL getDescription() override;
183 virtual void SAL_CALL dispose() override;
185 virtual void SAL_CALL addEventListener(
186 com::sun::star::uno::Reference< com::sun::star::lang::XEventListener >
187 const & xListener) override;
189 virtual void SAL_CALL removeEventListener(
190 com::sun::star::uno::Reference< com::sun::star::lang::XEventListener >
191 const & aListener) override;
193 // Only called from reader_ thread:
194 void sendCommitChangeRequest();
196 // Only called from reader_ thread:
197 void sendProtPropRequest(
198 OutgoingRequest::Kind kind,
199 std::vector< BinaryAny > const & inArguments);
201 void makeReleaseCall(
202 OUString const & oid,
203 com::sun::star::uno::TypeDescription const & type);
205 void sendRequest(
206 rtl::ByteSequence const & tid, OUString const & oid,
207 com::sun::star::uno::TypeDescription const & type,
208 com::sun::star::uno::TypeDescription const & member,
209 std::vector< BinaryAny > const & inArguments);
211 void throwException(bool exception, BinaryAny const & value);
213 com::sun::star::uno::Any mapBinaryToCppAny(BinaryAny const & binaryAny);
215 bool becameUnused() const;
217 void terminateWhenUnused(bool unused);
219 // Must only be called with mutex_ locked:
220 void checkDisposed();
222 typedef
223 std::vector<
224 com::sun::star::uno::Reference<
225 com::sun::star::lang::XEventListener > >
226 Listeners;
228 struct SubStub;
230 typedef std::map< com::sun::star::uno::TypeDescription, SubStub > Stub;
232 typedef std::map< OUString, Stub > Stubs;
234 enum State { STATE_INITIAL, STATE_STARTED, STATE_TERMINATED, STATE_FINAL };
236 enum Mode {
237 MODE_REQUESTED, MODE_REPLY_MINUS1, MODE_REPLY_0, MODE_REPLY_1,
238 MODE_WAIT, MODE_NORMAL, MODE_NORMAL_WAIT };
240 rtl::Reference< BridgeFactory > factory_;
241 OUString name_;
242 com::sun::star::uno::Reference< com::sun::star::connection::XConnection >
243 connection_;
244 com::sun::star::uno::Reference< com::sun::star::bridge::XInstanceProvider >
245 provider_;
246 com::sun::star::uno::Environment binaryUno_;
247 com::sun::star::uno::Mapping cppToBinaryMapping_;
248 com::sun::star::uno::Mapping binaryToCppMapping_;
249 rtl::ByteSequence protPropTid_;
250 OUString protPropOid_;
251 com::sun::star::uno::TypeDescription protPropType_;
252 com::sun::star::uno::TypeDescription protPropRequest_;
253 com::sun::star::uno::TypeDescription protPropCommit_;
254 OutgoingRequests outgoingRequests_;
255 osl::Condition passive_;
256 // to guarantee that passive_ is eventually set (to avoid deadlock, see
257 // dispose), activeCalls_ only counts those calls for which it can be
258 // guaranteed that incrementActiveCalls is indeed followed by
259 // decrementActiveCalls, without an intervening exception
260 osl::Condition terminated_;
262 osl::Mutex mutex_;
263 State state_;
264 Listeners listeners_;
265 uno_ThreadPool threadPool_;
266 rtl::Reference< Writer > writer_;
267 rtl::Reference< Reader > reader_;
268 bool currentContextMode_;
269 Stubs stubs_;
270 std::size_t proxies_;
271 std::size_t calls_;
272 bool normalCall_;
273 std::size_t activeCalls_;
275 // Only accessed from reader_ thread:
276 Mode mode_;
277 sal_Int32 random_;
282 #endif
284 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */