Bump version to 4.3-4
[LibreOffice.git] / binaryurp / source / bridge.hxx
blobf22eb5aad5f5509ffb94d54dc27c66099ced88f5
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 <list>
27 #include <map>
28 #include <vector>
30 #include "boost/noncopyable.hpp"
31 #include "com/sun/star/bridge/XBridge.hpp"
32 #include "com/sun/star/lang/XComponent.hpp"
33 #include "com/sun/star/uno/Reference.hxx"
34 #include "com/sun/star/uno/RuntimeException.hpp"
35 #include "cppuhelper/implbase2.hxx"
36 #include "osl/conditn.hxx"
37 #include "osl/mutex.hxx"
38 #include "rtl/ref.hxx"
39 #include "rtl/ustring.hxx"
40 #include "sal/types.h"
41 #include "uno/environment.hxx"
42 #include "uno/mapping.hxx"
43 #include "uno/threadpool.h"
45 #include "outgoingrequest.hxx"
46 #include "outgoingrequests.hxx"
47 #include "writer.hxx"
49 namespace binaryurp {
50 class BinaryAny;
51 class BridgeFactory;
52 class Proxy;
53 class Reader;
55 namespace com { namespace sun { namespace star {
56 namespace bridge { class XInstanceProvider; }
57 namespace connection { class XConnection; }
58 namespace lang { class XEventListener; }
59 namespace uno {
60 class Any;
61 class TypeDescription;
62 class UnoInterfaceReference;
63 class XInterface;
65 } } }
66 namespace rtl { class ByteSequence; }
68 namespace binaryurp {
70 class Bridge:
71 public cppu::WeakImplHelper2<
72 com::sun::star::bridge::XBridge, com::sun::star::lang::XComponent >,
73 private boost::noncopyable
75 public:
76 Bridge(
77 rtl::Reference< BridgeFactory > const & factory,
78 OUString const & name,
79 com::sun::star::uno::Reference<
80 com::sun::star::connection::XConnection > const & connection,
81 com::sun::star::uno::Reference<
82 com::sun::star::bridge::XInstanceProvider > const & provider);
84 void start();
86 // Internally waits for all incoming and outgoing remote calls to terminate,
87 // so must not be called from within such a call; when final is true, also
88 // joins all remaining threads (reader, writer, and worker threads from the
89 // thread pool), so must not be called with final set to true from such a
90 // thread:
91 void terminate(bool final);
93 com::sun::star::uno::Reference< com::sun::star::connection::XConnection >
94 getConnection() const;
96 com::sun::star::uno::Reference< com::sun::star::bridge::XInstanceProvider >
97 getProvider() const;
99 com::sun::star::uno::Mapping & getCppToBinaryMapping();
101 BinaryAny mapCppToBinaryAny(com::sun::star::uno::Any const & cppAny);
103 uno_ThreadPool getThreadPool();
105 rtl::Reference< Writer > getWriter();
107 com::sun::star::uno::UnoInterfaceReference registerIncomingInterface(
108 OUString const & oid,
109 com::sun::star::uno::TypeDescription const & type);
111 OUString registerOutgoingInterface(
112 com::sun::star::uno::UnoInterfaceReference const & object,
113 com::sun::star::uno::TypeDescription const & type);
115 com::sun::star::uno::UnoInterfaceReference findStub(
116 OUString const & oid,
117 com::sun::star::uno::TypeDescription const & type);
119 void releaseStub(
120 OUString const & oid,
121 com::sun::star::uno::TypeDescription const & type);
123 void resurrectProxy(Proxy & proxy);
125 void revokeProxy(Proxy & proxy);
127 void freeProxy(Proxy & proxy);
129 void incrementCalls(bool normalCall) throw ();
131 void decrementCalls();
133 void incrementActiveCalls() throw ();
135 void decrementActiveCalls() throw ();
137 bool makeCall(
138 OUString const & oid,
139 com::sun::star::uno::TypeDescription const & member, bool setter,
140 std::vector< BinaryAny > const & inArguments, BinaryAny * returnValue,
141 std::vector< BinaryAny > * outArguments);
143 // Only called from reader_ thread:
144 void sendRequestChangeRequest();
146 // Only called from reader_ thread:
147 void handleRequestChangeReply(
148 bool exception, BinaryAny const & returnValue);
150 // Only called from reader_ thread:
151 void handleCommitChangeReply(bool exception, BinaryAny const & returnValue);
153 // Only called from reader_ thread:
154 void handleRequestChangeRequest(
155 rtl::ByteSequence const & tid,
156 std::vector< BinaryAny > const & inArguments);
158 // Only called from reader_ thread:
159 void handleCommitChangeRequest(
160 rtl::ByteSequence const & tid,
161 std::vector< BinaryAny > const & inArguments);
163 OutgoingRequest lastOutgoingRequest(rtl::ByteSequence const & tid);
165 bool isProtocolPropertiesRequest(
166 OUString const & oid,
167 com::sun::star::uno::TypeDescription const & type) const;
169 void setCurrentContextMode();
171 bool isCurrentContextMode();
173 private:
174 virtual ~Bridge();
176 virtual com::sun::star::uno::Reference< com::sun::star::uno::XInterface >
177 SAL_CALL getInstance(OUString const & sInstanceName)
178 throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
180 virtual OUString SAL_CALL getName()
181 throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
183 virtual OUString SAL_CALL getDescription()
184 throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
186 virtual void SAL_CALL dispose()
187 throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
189 virtual void SAL_CALL addEventListener(
190 com::sun::star::uno::Reference< com::sun::star::lang::XEventListener >
191 const & xListener)
192 throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
194 virtual void SAL_CALL removeEventListener(
195 com::sun::star::uno::Reference< com::sun::star::lang::XEventListener >
196 const & aListener)
197 throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
199 // Only called from reader_ thread:
200 void sendCommitChangeRequest();
202 // Only called from reader_ thread:
203 void sendProtPropRequest(
204 OutgoingRequest::Kind kind,
205 std::vector< BinaryAny > const & inArguments);
207 void makeReleaseCall(
208 OUString const & oid,
209 com::sun::star::uno::TypeDescription const & type);
211 void sendRequest(
212 rtl::ByteSequence const & tid, OUString const & oid,
213 com::sun::star::uno::TypeDescription const & type,
214 com::sun::star::uno::TypeDescription const & member,
215 std::vector< BinaryAny > const & inArguments);
217 void throwException(bool exception, BinaryAny const & value);
219 com::sun::star::uno::Any mapBinaryToCppAny(BinaryAny const & binaryAny);
221 bool becameUnused() const;
223 void terminateWhenUnused(bool unused);
225 // Must only be called with mutex_ locked:
226 void checkDisposed();
228 typedef
229 std::list<
230 com::sun::star::uno::Reference<
231 com::sun::star::lang::XEventListener > >
232 Listeners;
234 struct SubStub;
236 typedef std::map< com::sun::star::uno::TypeDescription, SubStub > Stub;
238 typedef std::map< OUString, Stub > Stubs;
240 enum State { STATE_INITIAL, STATE_STARTED, STATE_TERMINATED, STATE_FINAL };
242 enum Mode {
243 MODE_REQUESTED, MODE_REPLY_MINUS1, MODE_REPLY_0, MODE_REPLY_1,
244 MODE_WAIT, MODE_NORMAL, MODE_NORMAL_WAIT };
246 rtl::Reference< BridgeFactory > factory_;
247 OUString name_;
248 com::sun::star::uno::Reference< com::sun::star::connection::XConnection >
249 connection_;
250 com::sun::star::uno::Reference< com::sun::star::bridge::XInstanceProvider >
251 provider_;
252 com::sun::star::uno::Environment binaryUno_;
253 com::sun::star::uno::Mapping cppToBinaryMapping_;
254 com::sun::star::uno::Mapping binaryToCppMapping_;
255 rtl::ByteSequence protPropTid_;
256 OUString protPropOid_;
257 com::sun::star::uno::TypeDescription protPropType_;
258 com::sun::star::uno::TypeDescription protPropRequest_;
259 com::sun::star::uno::TypeDescription protPropCommit_;
260 OutgoingRequests outgoingRequests_;
261 osl::Condition passive_;
262 // to guarantee that passive_ is eventually set (to avoid deadlock, see
263 // dispose), activeCalls_ only counts those calls for which it can be
264 // guaranteed that incrementActiveCalls is indeed followed by
265 // decrementActiveCalls, without an intervening exception
266 osl::Condition terminated_;
268 osl::Mutex mutex_;
269 State state_;
270 Listeners listeners_;
271 uno_ThreadPool threadPool_;
272 rtl::Reference< Writer > writer_;
273 rtl::Reference< Reader > reader_;
274 bool currentContextMode_;
275 Stubs stubs_;
276 std::size_t proxies_;
277 std::size_t calls_;
278 bool normalCall_;
279 std::size_t activeCalls_;
281 // Only accessed from reader_ thread:
282 Mode mode_;
283 sal_Int32 random_;
288 #endif
290 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */