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