cURL: follow redirects
[LibreOffice.git] / binaryurp / source / bridge.cxx
blobd47b83410708aa4e3c15f28f790809664ee162cb
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 #include "sal/config.h"
22 #include <algorithm>
23 #include <cassert>
24 #include <cstddef>
25 #include <limits>
26 #include <memory>
27 #include <vector>
29 #include "com/sun/star/bridge/InvalidProtocolChangeException.hpp"
30 #include "com/sun/star/bridge/XBridge.hpp"
31 #include "com/sun/star/bridge/XInstanceProvider.hpp"
32 #include "com/sun/star/bridge/XProtocolProperties.hpp"
33 #include "com/sun/star/connection/XConnection.hpp"
34 #include "com/sun/star/io/IOException.hpp"
35 #include "com/sun/star/lang/DisposedException.hpp"
36 #include "com/sun/star/lang/EventObject.hpp"
37 #include "com/sun/star/lang/XEventListener.hpp"
38 #include "com/sun/star/uno/Reference.hxx"
39 #include "com/sun/star/uno/RuntimeException.hpp"
40 #include "com/sun/star/uno/Sequence.hxx"
41 #include "com/sun/star/uno/XInterface.hpp"
42 #include "cppuhelper/exc_hlp.hxx"
43 #include "cppuhelper/weak.hxx"
44 #include "osl/mutex.hxx"
45 #include "osl/thread.hxx"
46 #include "rtl/byteseq.hxx"
47 #include "rtl/random.h"
48 #include "rtl/ref.hxx"
49 #include "rtl/ustrbuf.hxx"
50 #include "rtl/ustring.h"
51 #include "rtl/ustring.hxx"
52 #include "sal/log.hxx"
53 #include "sal/types.h"
54 #include "typelib/typeclass.h"
55 #include "typelib/typedescription.h"
56 #include "typelib/typedescription.hxx"
57 #include "uno/dispatcher.hxx"
58 #include "uno/environment.hxx"
59 #include "uno/lbnames.h"
61 #include "binaryany.hxx"
62 #include "bridge.hxx"
63 #include "bridgefactory.hxx"
64 #include "incomingreply.hxx"
65 #include "lessoperators.hxx"
66 #include "outgoingrequest.hxx"
67 #include "outgoingrequests.hxx"
68 #include "proxy.hxx"
69 #include "reader.hxx"
71 namespace binaryurp {
73 namespace {
75 sal_Int32 random() {
76 sal_Int32 n;
77 rtlRandomPool pool = rtl_random_createPool();
78 rtl_random_getBytes(pool, &n, sizeof n);
79 rtl_random_destroyPool(pool);
80 return n;
83 OUString toString(css::uno::TypeDescription const & type) {
84 typelib_TypeDescription * d = type.get();
85 assert(d != nullptr && d->pTypeName != nullptr);
86 return OUString(d->pTypeName);
89 extern "C" void SAL_CALL freeProxyCallback(
90 SAL_UNUSED_PARAMETER uno_ExtEnvironment *, void * pProxy)
92 assert(pProxy != nullptr);
93 static_cast< Proxy * >(pProxy)->do_free();
96 bool isThread(salhelper::Thread * thread) {
97 assert(thread != nullptr);
98 return osl::Thread::getCurrentIdentifier() == thread->getIdentifier();
101 class AttachThread {
102 public:
103 explicit AttachThread(uno_ThreadPool threadPool);
105 ~AttachThread();
107 const rtl::ByteSequence& getTid() throw () { return tid_;}
109 private:
110 AttachThread(const AttachThread&) = delete;
111 AttachThread& operator=(const AttachThread&) = delete;
113 uno_ThreadPool threadPool_;
114 rtl::ByteSequence tid_;
117 AttachThread::AttachThread(uno_ThreadPool threadPool): threadPool_(threadPool) {
118 sal_Sequence * s = nullptr;
119 uno_getIdOfCurrentThread(&s);
120 tid_ = rtl::ByteSequence(s, rtl::BYTESEQ_NOACQUIRE);
121 uno_threadpool_attach(threadPool_);
124 AttachThread::~AttachThread() {
125 uno_threadpool_detach(threadPool_);
126 uno_releaseIdFromCurrentThread();
130 class PopOutgoingRequest {
131 public:
132 PopOutgoingRequest(
133 OutgoingRequests & requests, rtl::ByteSequence const & tid,
134 OutgoingRequest const & request);
136 ~PopOutgoingRequest();
138 void clear();
140 private:
141 PopOutgoingRequest(const PopOutgoingRequest&) = delete;
142 PopOutgoingRequest& operator=(const PopOutgoingRequest&) = delete;
144 OutgoingRequests & requests_;
145 rtl::ByteSequence tid_;
146 bool cleared_;
149 PopOutgoingRequest::PopOutgoingRequest(
150 OutgoingRequests & requests, rtl::ByteSequence const & tid,
151 OutgoingRequest const & request):
152 requests_(requests), tid_(tid), cleared_(false)
154 requests_.push(tid_, request);
157 PopOutgoingRequest::~PopOutgoingRequest() {
158 if (!cleared_) {
159 requests_.pop(tid_);
163 void PopOutgoingRequest::clear() {
164 cleared_ = true;
169 struct Bridge::SubStub {
170 com::sun::star::uno::UnoInterfaceReference object;
172 sal_uInt32 references;
175 Bridge::Bridge(
176 rtl::Reference< BridgeFactory > const & factory, OUString const & name,
177 css::uno::Reference< css::connection::XConnection > const & connection,
178 css::uno::Reference< css::bridge::XInstanceProvider > const & provider):
179 factory_(factory), name_(name), connection_(connection),
180 provider_(provider),
181 binaryUno_(UNO_LB_UNO),
182 cppToBinaryMapping_(CPPU_CURRENT_LANGUAGE_BINDING_NAME, UNO_LB_UNO),
183 binaryToCppMapping_(UNO_LB_UNO, CPPU_CURRENT_LANGUAGE_BINDING_NAME),
184 protPropTid_(
185 reinterpret_cast< sal_Int8 const * >(".UrpProtocolPropertiesTid"),
186 RTL_CONSTASCII_LENGTH(".UrpProtocolPropertiesTid")),
187 protPropOid_("UrpProtocolProperties"),
188 protPropType_(
189 cppu::UnoType<
190 css::uno::Reference< css::bridge::XProtocolProperties > >::get()),
191 protPropRequest_("com.sun.star.bridge.XProtocolProperties::requestChange"),
192 protPropCommit_("com.sun.star.bridge.XProtocolProperties::commitChange"),
193 state_(STATE_INITIAL), threadPool_(nullptr), currentContextMode_(false),
194 proxies_(0), calls_(0), normalCall_(false), activeCalls_(0),
195 mode_(MODE_REQUESTED)
197 assert(factory.is() && connection.is());
198 if (!binaryUno_.is()) {
199 throw css::uno::RuntimeException("URP: no binary UNO environment");
201 if (!(cppToBinaryMapping_.is() && binaryToCppMapping_.is())) {
202 throw css::uno::RuntimeException("URP: no C++ UNO mapping");
204 passive_.set();
207 void Bridge::start() {
208 rtl::Reference< Reader > r(new Reader(this));
209 rtl::Reference< Writer > w(new Writer(this));
211 osl::MutexGuard g(mutex_);
212 assert(
213 state_ == STATE_INITIAL && threadPool_ == nullptr && !writer_.is() &&
214 !reader_.is());
215 threadPool_ = uno_threadpool_create();
216 assert(threadPool_ != nullptr);
217 reader_ = r;
218 writer_ = w;
219 state_ = STATE_STARTED;
221 // It is important to call reader_->launch() last here; both
222 // Writer::execute and Reader::execute can call Bridge::terminate, but
223 // Writer::execute is initially blocked in unblocked_.wait() until
224 // Reader::execute has called bridge_->sendRequestChangeRequest(), so
225 // effectively only reader_->launch() can lead to an early call to
226 // Bridge::terminate
227 w->launch();
228 r->launch();
231 void Bridge::terminate(bool final) {
232 uno_ThreadPool tp;
233 // Make sure function-local variables (Stubs s, etc.) are destroyed before
234 // the final uno_threadpool_destroy/threadPool_ = 0:
236 rtl::Reference< Reader > r;
237 rtl::Reference< Writer > w;
238 bool joinW;
239 Listeners ls;
241 osl::ClearableMutexGuard g(mutex_);
242 switch (state_) {
243 case STATE_INITIAL: // via ~Bridge -> dispose -> terminate
244 case STATE_FINAL:
245 return;
246 case STATE_STARTED:
247 break;
248 case STATE_TERMINATED:
249 if (final) {
250 g.clear();
251 terminated_.wait();
253 osl::MutexGuard g2(mutex_);
254 tp = threadPool_;
255 threadPool_ = nullptr;
256 if (reader_.is()) {
257 if (!isThread(reader_.get())) {
258 r = reader_;
260 reader_.clear();
262 if (writer_.is()) {
263 if (!isThread(writer_.get())) {
264 w = writer_;
266 writer_.clear();
268 state_ = STATE_FINAL;
270 assert(!(r.is() && w.is()));
271 if (r.is()) {
272 r->join();
273 } else if (w.is()) {
274 w->join();
276 if (tp != nullptr) {
277 uno_threadpool_destroy(tp);
280 return;
282 tp = threadPool_;
283 assert(!(final && isThread(reader_.get())));
284 if (!isThread(reader_.get())) {
285 std::swap(reader_, r);
287 w = writer_;
288 joinW = !isThread(writer_.get());
289 assert(!final || joinW);
290 if (joinW) {
291 writer_.clear();
293 ls.swap(listeners_);
294 state_ = final ? STATE_FINAL : STATE_TERMINATED;
296 try {
297 connection_->close();
298 } catch (const css::io::IOException & e) {
299 SAL_INFO("binaryurp", "caught IO exception '" << e.Message << '\'');
301 assert(w.is());
302 w->stop();
303 if (r.is()) {
304 r->join();
306 if (joinW) {
307 w->join();
309 assert(tp != nullptr);
310 uno_threadpool_dispose(tp);
311 Stubs s;
313 osl::MutexGuard g(mutex_);
314 s.swap(stubs_);
316 for (Stubs::iterator i(s.begin()); i != s.end(); ++i) {
317 for (Stub::iterator j(i->second.begin()); j != i->second.end(); ++j)
319 SAL_INFO(
320 "binaryurp",
321 "stub '" << i->first << "', '" << toString(j->first)
322 << "' still mapped at Bridge::terminate");
323 binaryUno_.get()->pExtEnv->revokeInterface(
324 binaryUno_.get()->pExtEnv, j->second.object.get());
327 factory_->removeBridge(this);
328 for (Listeners::iterator i(ls.begin()); i != ls.end(); ++i) {
329 try {
330 (*i)->disposing(
331 css::lang::EventObject(
332 static_cast< cppu::OWeakObject * >(this)));
333 } catch (const css::uno::RuntimeException & e) {
334 SAL_WARN(
335 "binaryurp",
336 "caught runtime exception '" << e.Message << '\'');
340 if (final) {
341 uno_threadpool_destroy(tp);
344 osl::MutexGuard g(mutex_);
345 if (final) {
346 threadPool_ = nullptr;
349 terminated_.set();
353 BinaryAny Bridge::mapCppToBinaryAny(css::uno::Any const & cppAny) {
354 css::uno::Any in(cppAny);
355 BinaryAny out;
356 out.~BinaryAny();
357 uno_copyAndConvertData(
358 &out.get(), &in,
359 css::uno::TypeDescription(cppu::UnoType< css::uno::Any >::get()).get(),
360 cppToBinaryMapping_.get());
361 return out;
364 uno_ThreadPool Bridge::getThreadPool() {
365 osl::MutexGuard g(mutex_);
366 checkDisposed();
367 assert(threadPool_ != nullptr);
368 return threadPool_;
371 rtl::Reference< Writer > Bridge::getWriter() {
372 osl::MutexGuard g(mutex_);
373 checkDisposed();
374 assert(writer_.is());
375 return writer_;
378 css::uno::UnoInterfaceReference Bridge::registerIncomingInterface(
379 OUString const & oid, css::uno::TypeDescription const & type)
381 assert(type.is());
382 if (oid.isEmpty()) {
383 return css::uno::UnoInterfaceReference();
385 css::uno::UnoInterfaceReference obj(findStub(oid, type));
386 if (!obj.is()) {
387 binaryUno_.get()->pExtEnv->getRegisteredInterface(
388 binaryUno_.get()->pExtEnv,
389 reinterpret_cast< void ** >(&obj.m_pUnoI), oid.pData,
390 reinterpret_cast< typelib_InterfaceTypeDescription * >(type.get()));
391 if (obj.is()) {
392 makeReleaseCall(oid, type);
393 } else {
394 obj.set(new Proxy(this, oid, type), SAL_NO_ACQUIRE);
396 osl::MutexGuard g(mutex_);
397 assert(proxies_ < std::numeric_limits< std::size_t >::max());
398 ++proxies_;
400 binaryUno_.get()->pExtEnv->registerProxyInterface(
401 binaryUno_.get()->pExtEnv,
402 reinterpret_cast< void ** >(&obj.m_pUnoI), &freeProxyCallback,
403 oid.pData,
404 reinterpret_cast< typelib_InterfaceTypeDescription * >(
405 type.get()));
408 return obj;
411 OUString Bridge::registerOutgoingInterface(
412 css::uno::UnoInterfaceReference const & object,
413 css::uno::TypeDescription const & type)
415 assert(type.is());
416 if (!object.is()) {
417 return OUString();
419 OUString oid;
420 if (!Proxy::isProxy(this, object, &oid)) {
421 binaryUno_.get()->pExtEnv->getObjectIdentifier(
422 binaryUno_.get()->pExtEnv, &oid.pData, object.get());
423 osl::MutexGuard g(mutex_);
424 Stubs::iterator i(stubs_.find(oid));
425 Stub newStub;
426 Stub * stub = i == stubs_.end() ? &newStub : &i->second;
427 Stub::iterator j(stub->find(type));
428 //TODO: Release sub-stub if it is not successfully sent to remote side
429 // (otherwise, stub will leak until terminate()):
430 if (j == stub->end()) {
431 j = stub->insert(Stub::value_type(type, SubStub())).first;
432 if (stub == &newStub) {
433 i = stubs_.insert(Stubs::value_type(oid, Stub())).first;
434 std::swap(i->second, newStub);
435 j = i->second.find(type);
436 assert(j != i->second.end());
438 j->second.object = object;
439 j->second.references = 1;
440 binaryUno_.get()->pExtEnv->registerInterface(
441 binaryUno_.get()->pExtEnv,
442 reinterpret_cast< void ** >(&j->second.object.m_pUnoI),
443 oid.pData,
444 reinterpret_cast< typelib_InterfaceTypeDescription * >(
445 type.get()));
446 } else {
447 assert(stub != &newStub);
448 if (j->second.references == SAL_MAX_UINT32) {
449 throw css::uno::RuntimeException(
450 "URP: stub reference count overflow");
452 ++j->second.references;
455 return oid;
458 css::uno::UnoInterfaceReference Bridge::findStub(
459 OUString const & oid, css::uno::TypeDescription const & type)
461 assert(!oid.isEmpty() && type.is());
462 osl::MutexGuard g(mutex_);
463 Stubs::iterator i(stubs_.find(oid));
464 if (i != stubs_.end()) {
465 Stub::iterator j(i->second.find(type));
466 if (j != i->second.end()) {
467 return j->second.object;
469 for (j = i->second.begin(); j != i->second.end(); ++j) {
470 if (typelib_typedescription_isAssignableFrom(
471 type.get(), j->first.get()))
473 return j->second.object;
477 return css::uno::UnoInterfaceReference();
480 void Bridge::releaseStub(
481 OUString const & oid, css::uno::TypeDescription const & type)
483 assert(!oid.isEmpty() && type.is());
484 css::uno::UnoInterfaceReference obj;
485 bool unused;
487 osl::MutexGuard g(mutex_);
488 Stubs::iterator i(stubs_.find(oid));
489 if (i == stubs_.end()) {
490 throw css::uno::RuntimeException("URP: release unknown stub");
492 Stub::iterator j(i->second.find(type));
493 if (j == i->second.end()) {
494 throw css::uno::RuntimeException("URP: release unknown stub");
496 assert(j->second.references > 0);
497 --j->second.references;
498 if (j->second.references == 0) {
499 obj = j->second.object;
500 i->second.erase(j);
501 if (i->second.empty()) {
502 stubs_.erase(i);
505 unused = becameUnused();
507 if (obj.is()) {
508 binaryUno_.get()->pExtEnv->revokeInterface(
509 binaryUno_.get()->pExtEnv, obj.get());
511 terminateWhenUnused(unused);
514 void Bridge::resurrectProxy(Proxy & proxy) {
515 uno_Interface * p = &proxy;
516 binaryUno_.get()->pExtEnv->registerProxyInterface(
517 binaryUno_.get()->pExtEnv,
518 reinterpret_cast< void ** >(&p), &freeProxyCallback,
519 proxy.getOid().pData,
520 reinterpret_cast< typelib_InterfaceTypeDescription * >(
521 proxy.getType().get()));
522 assert(p == &proxy);
525 void Bridge::revokeProxy(Proxy & proxy) {
526 binaryUno_.get()->pExtEnv->revokeInterface(
527 binaryUno_.get()->pExtEnv, &proxy);
530 void Bridge::freeProxy(Proxy & proxy) {
531 try {
532 makeReleaseCall(proxy.getOid(), proxy.getType());
533 } catch (const css::uno::RuntimeException & e) {
534 SAL_INFO(
535 "binaryurp", "caught runtime exception '" << e.Message << '\'');
536 } catch (const std::exception & e) {
537 SAL_WARN("binaryurp", "caught C++ exception '" << e.what() << '\'');
539 bool unused;
541 osl::MutexGuard g(mutex_);
542 assert(proxies_ > 0);
543 --proxies_;
544 unused = becameUnused();
546 terminateWhenUnused(unused);
549 void Bridge::incrementCalls(bool normalCall) throw () {
550 osl::MutexGuard g(mutex_);
551 assert(calls_ < std::numeric_limits< std::size_t >::max());
552 ++calls_;
553 normalCall_ |= normalCall;
556 void Bridge::decrementCalls() {
557 bool unused;
559 osl::MutexGuard g(mutex_);
560 assert(calls_ > 0);
561 --calls_;
562 unused = becameUnused();
564 terminateWhenUnused(unused);
567 void Bridge::incrementActiveCalls() throw () {
568 osl::MutexGuard g(mutex_);
569 assert(
570 activeCalls_ <= calls_ &&
571 activeCalls_ < std::numeric_limits< std::size_t >::max());
572 ++activeCalls_;
573 passive_.reset();
576 void Bridge::decrementActiveCalls() throw () {
577 osl::MutexGuard g(mutex_);
578 assert(activeCalls_ <= calls_ && activeCalls_ > 0);
579 --activeCalls_;
580 if (activeCalls_ == 0) {
581 passive_.set();
585 bool Bridge::makeCall(
586 OUString const & oid, css::uno::TypeDescription const & member,
587 bool setter, std::vector< BinaryAny > const & inArguments,
588 BinaryAny * returnValue, std::vector< BinaryAny > * outArguments)
590 std::unique_ptr< IncomingReply > resp;
592 uno_ThreadPool tp = getThreadPool();
593 AttachThread att(tp);
594 PopOutgoingRequest pop(
595 outgoingRequests_, att.getTid(),
596 OutgoingRequest(OutgoingRequest::KIND_NORMAL, member, setter));
597 sendRequest(
598 att.getTid(), oid, css::uno::TypeDescription(), member,
599 inArguments);
600 pop.clear();
601 incrementCalls(true);
602 incrementActiveCalls();
603 void * job;
604 uno_threadpool_enter(tp, &job);
605 resp.reset(static_cast< IncomingReply * >(job));
606 decrementActiveCalls();
607 decrementCalls();
609 if (resp.get() == nullptr) {
610 throw css::lang::DisposedException(
611 "Binary URP bridge disposed during call",
612 static_cast< cppu::OWeakObject * >(this));
614 *returnValue = resp->returnValue;
615 if (!resp->exception) {
616 *outArguments = resp->outArguments;
618 return resp->exception;
621 void Bridge::sendRequestChangeRequest() {
622 assert(mode_ == MODE_REQUESTED);
623 random_ = random();
624 std::vector< BinaryAny > a;
625 a.push_back(
626 BinaryAny(
627 css::uno::TypeDescription(cppu::UnoType< sal_Int32 >::get()),
628 &random_));
629 sendProtPropRequest(OutgoingRequest::KIND_REQUEST_CHANGE, a);
632 void Bridge::handleRequestChangeReply(
633 bool exception, BinaryAny const & returnValue)
635 try {
636 throwException(exception, returnValue);
637 } catch (css::uno::RuntimeException & e) {
638 // Before OOo 2.2, Java URP would throw a RuntimeException when
639 // receiving a requestChange message (see i#35277 "Java URP: Support
640 // Manipulation of Protocol Properties"):
641 if (mode_ != MODE_REQUESTED) {
642 throw;
644 SAL_WARN(
645 "binaryurp",
646 "requestChange caught RuntimeException \'" << e.Message
647 << "' in state 'requested'");
648 mode_ = MODE_NORMAL;
649 getWriter()->unblock();
650 decrementCalls();
651 return;
653 sal_Int32 n = *static_cast< sal_Int32 * >(
654 returnValue.getValue(
655 css::uno::TypeDescription(cppu::UnoType< sal_Int32 >::get())));
656 sal_Int32 exp = 0;
657 switch (mode_) {
658 case MODE_REQUESTED:
659 case MODE_REPLY_1:
660 exp = 1;
661 break;
662 case MODE_REPLY_MINUS1:
663 exp = -1;
664 mode_ = MODE_REQUESTED;
665 break;
666 case MODE_REPLY_0:
667 exp = 0;
668 mode_ = MODE_WAIT;
669 break;
670 default:
671 assert(false); // this cannot happen
672 break;
674 if (n != exp) {
675 throw css::uno::RuntimeException(
676 "URP: requestChange reply with unexpected return value received",
677 static_cast< cppu::OWeakObject * >(this));
679 decrementCalls();
680 switch (exp) {
681 case -1:
682 sendRequestChangeRequest();
683 break;
684 case 0:
685 break;
686 case 1:
687 sendCommitChangeRequest();
688 break;
689 default:
690 assert(false); // this cannot happen
691 break;
695 void Bridge::handleCommitChangeReply(
696 bool exception, BinaryAny const & returnValue)
698 bool bCcMode = true;
699 try {
700 throwException(exception, returnValue);
701 } catch (const css::bridge::InvalidProtocolChangeException &) {
702 bCcMode = false;
704 if (bCcMode) {
705 setCurrentContextMode();
707 assert(mode_ == MODE_REQUESTED || mode_ == MODE_REPLY_1);
708 mode_ = MODE_NORMAL;
709 getWriter()->unblock();
710 decrementCalls();
713 void Bridge::handleRequestChangeRequest(
714 rtl::ByteSequence const & tid, std::vector< BinaryAny > const & inArguments)
716 assert(inArguments.size() == 1);
717 switch (mode_) {
718 case MODE_REQUESTED:
720 sal_Int32 n2 = *static_cast< sal_Int32 * >(
721 inArguments[0].getValue(
722 css::uno::TypeDescription(
723 cppu::UnoType< sal_Int32 >::get())));
724 sal_Int32 ret;
725 if (n2 > random_) {
726 ret = 1;
727 mode_ = MODE_REPLY_0;
728 } else if (n2 == random_) {
729 ret = -1;
730 mode_ = MODE_REPLY_MINUS1;
731 } else {
732 ret = 0;
733 mode_ = MODE_REPLY_1;
735 getWriter()->sendDirectReply(
736 tid, protPropRequest_, false,
737 BinaryAny(
738 css::uno::TypeDescription(
739 cppu::UnoType< sal_Int32 >::get()),
740 &ret),
741 std::vector< BinaryAny >());
742 break;
744 case MODE_NORMAL:
746 mode_ = MODE_NORMAL_WAIT;
747 sal_Int32 ret = 1;
748 getWriter()->queueReply(
749 tid, protPropRequest_, false, false,
750 BinaryAny(
751 css::uno::TypeDescription(
752 cppu::UnoType< sal_Int32 >::get()),
753 &ret),
754 std::vector< BinaryAny >(), false);
755 break;
757 default:
758 throw css::uno::RuntimeException(
759 "URP: unexpected requestChange request received",
760 static_cast< cppu::OWeakObject * >(this));
764 void Bridge::handleCommitChangeRequest(
765 rtl::ByteSequence const & tid, std::vector< BinaryAny > const & inArguments)
767 bool bCcMode = false;
768 bool bExc = false;
769 BinaryAny ret;
770 assert(inArguments.size() == 1);
771 css::uno::Sequence< css::bridge::ProtocolProperty > s;
772 bool ok = (mapBinaryToCppAny(inArguments[0]) >>= s);
773 assert(ok);
774 (void) ok; // avoid warnings
775 for (sal_Int32 i = 0; i != s.getLength(); ++i) {
776 if (s[i].Name == "CurrentContext") {
777 bCcMode = true;
778 } else {
779 bCcMode = false;
780 bExc = true;
781 ret = mapCppToBinaryAny(
782 css::uno::makeAny(
783 css::bridge::InvalidProtocolChangeException(
784 "InvalidProtocolChangeException",
785 css::uno::Reference< css::uno::XInterface >(), s[i],
786 1)));
787 break;
790 switch (mode_) {
791 case MODE_WAIT:
792 getWriter()->sendDirectReply(
793 tid, protPropCommit_, bExc, ret, std::vector< BinaryAny >());
794 if (bCcMode) {
795 setCurrentContextMode();
796 mode_ = MODE_NORMAL;
797 getWriter()->unblock();
798 } else {
799 mode_ = MODE_REQUESTED;
800 sendRequestChangeRequest();
802 break;
803 case MODE_NORMAL_WAIT:
804 getWriter()->queueReply(
805 tid, protPropCommit_, false, false, ret, std::vector< BinaryAny >(),
806 bCcMode);
807 mode_ = MODE_NORMAL;
808 break;
809 default:
810 throw css::uno::RuntimeException(
811 "URP: unexpected commitChange request received",
812 static_cast< cppu::OWeakObject * >(this));
816 OutgoingRequest Bridge::lastOutgoingRequest(rtl::ByteSequence const & tid) {
817 OutgoingRequest req(outgoingRequests_.top(tid));
818 outgoingRequests_.pop(tid);
819 return req;
822 bool Bridge::isProtocolPropertiesRequest(
823 OUString const & oid, css::uno::TypeDescription const & type) const
825 return oid == protPropOid_ && type.equals(protPropType_);
828 void Bridge::setCurrentContextMode() {
829 osl::MutexGuard g(mutex_);
830 currentContextMode_ = true;
833 bool Bridge::isCurrentContextMode() {
834 osl::MutexGuard g(mutex_);
835 return currentContextMode_;
838 Bridge::~Bridge() {
839 #if OSL_DEBUG_LEVEL > 0
841 osl::MutexGuard g(mutex_);
842 SAL_WARN_IF(
843 state_ == STATE_STARTED || state_ == STATE_TERMINATED, "binaryurp",
844 "undisposed bridge, potential deadlock ahead");
846 #endif
847 dispose();
850 css::uno::Reference< css::uno::XInterface > Bridge::getInstance(
851 OUString const & sInstanceName) throw (css::uno::RuntimeException, std::exception)
853 if (sInstanceName.isEmpty()) {
854 throw css::uno::RuntimeException(
855 "XBridge::getInstance sInstanceName must be non-empty",
856 static_cast< cppu::OWeakObject * >(this));
858 for (sal_Int32 i = 0; i != sInstanceName.getLength(); ++i) {
859 if (sInstanceName[i] > 0x7F) {
860 throw css::uno::RuntimeException(
861 "XBridge::getInstance sInstanceName contains non-ASCII"
862 " character");
865 css::uno::TypeDescription ifc(cppu::UnoType<css::uno::XInterface>::get());
866 typelib_TypeDescription * p = ifc.get();
867 std::vector< BinaryAny > inArgs;
868 inArgs.push_back(
869 BinaryAny(
870 css::uno::TypeDescription(cppu::UnoType< css::uno::Type >::get()),
871 &p));
872 BinaryAny ret;
873 std::vector< BinaryAny> outArgs;
874 bool bExc = makeCall(
875 sInstanceName,
876 css::uno::TypeDescription(
877 "com.sun.star.uno.XInterface::queryInterface"),
878 false, inArgs, &ret, &outArgs);
879 throwException(bExc, ret);
880 return css::uno::Reference< css::uno::XInterface >(
881 static_cast< css::uno::XInterface * >(
882 binaryToCppMapping_.mapInterface(
883 *static_cast< uno_Interface ** >(ret.getValue(ifc)),
884 ifc.get())),
885 SAL_NO_ACQUIRE);
888 OUString Bridge::getName() throw (css::uno::RuntimeException, std::exception) {
889 return name_;
892 OUString Bridge::getDescription() throw (css::uno::RuntimeException, std::exception) {
893 OUStringBuffer b(name_);
894 b.append(':');
895 b.append(connection_->getDescription());
896 return b.makeStringAndClear();
899 void Bridge::dispose() throw (css::uno::RuntimeException, std::exception) {
900 // For terminate(true) not to deadlock, an external protocol must ensure
901 // that dispose is not called from a thread pool worker thread (that dispose
902 // is never called from the reader or writer thread is already ensured
903 // internally):
904 terminate(true);
905 // OOo expects dispose to not return while there are still remote calls in
906 // progress; an external protocol must ensure that dispose is not called
907 // from within an incoming or outgoing remote call, as passive_.wait() would
908 // otherwise deadlock:
909 passive_.wait();
912 void Bridge::addEventListener(
913 css::uno::Reference< css::lang::XEventListener > const & xListener)
914 throw (css::uno::RuntimeException, std::exception)
916 assert(xListener.is());
918 osl::MutexGuard g(mutex_);
919 assert(state_ != STATE_INITIAL);
920 if (state_ == STATE_STARTED) {
921 listeners_.push_back(xListener);
922 return;
925 xListener->disposing(
926 css::lang::EventObject(static_cast< cppu::OWeakObject * >(this)));
929 void Bridge::removeEventListener(
930 css::uno::Reference< css::lang::XEventListener > const & aListener)
931 throw (css::uno::RuntimeException, std::exception)
933 osl::MutexGuard g(mutex_);
934 Listeners::iterator i(
935 std::find(listeners_.begin(), listeners_.end(), aListener));
936 if (i != listeners_.end()) {
937 listeners_.erase(i);
941 void Bridge::sendCommitChangeRequest() {
942 assert(mode_ == MODE_REQUESTED || mode_ == MODE_REPLY_1);
943 css::uno::Sequence< css::bridge::ProtocolProperty > s(1);
944 s[0].Name = "CurrentContext";
945 std::vector< BinaryAny > a;
946 a.push_back(mapCppToBinaryAny(css::uno::makeAny(s)));
947 sendProtPropRequest(OutgoingRequest::KIND_COMMIT_CHANGE, a);
950 void Bridge::sendProtPropRequest(
951 OutgoingRequest::Kind kind, std::vector< BinaryAny > const & inArguments)
953 assert(
954 kind == OutgoingRequest::KIND_REQUEST_CHANGE ||
955 kind == OutgoingRequest::KIND_COMMIT_CHANGE);
956 incrementCalls(false);
957 css::uno::TypeDescription member(
958 kind == OutgoingRequest::KIND_REQUEST_CHANGE
959 ? protPropRequest_ : protPropCommit_);
960 PopOutgoingRequest pop(
961 outgoingRequests_, protPropTid_, OutgoingRequest(kind, member, false));
962 getWriter()->sendDirectRequest(
963 protPropTid_, protPropOid_, protPropType_, member, inArguments);
964 pop.clear();
967 void Bridge::makeReleaseCall(
968 OUString const & oid, css::uno::TypeDescription const & type)
970 AttachThread att(getThreadPool());
971 sendRequest(
972 att.getTid(), oid, type,
973 css::uno::TypeDescription("com.sun.star.uno.XInterface::release"),
974 std::vector< BinaryAny >());
977 void Bridge::sendRequest(
978 rtl::ByteSequence const & tid, OUString const & oid,
979 css::uno::TypeDescription const & type,
980 css::uno::TypeDescription const & member,
981 std::vector< BinaryAny > const & inArguments)
983 getWriter()->queueRequest(tid, oid, type, member, inArguments);
986 void Bridge::throwException(bool exception, BinaryAny const & value) {
987 if (exception) {
988 cppu::throwException(mapBinaryToCppAny(value));
992 css::uno::Any Bridge::mapBinaryToCppAny(BinaryAny const & binaryAny) {
993 BinaryAny in(binaryAny);
994 css::uno::Any out;
995 out.~Any();
996 uno_copyAndConvertData(
997 &out, &in.get(),
998 css::uno::TypeDescription(cppu::UnoType< css::uno::Any >::get()).get(),
999 binaryToCppMapping_.get());
1000 return out;
1003 bool Bridge::becameUnused() const {
1004 return stubs_.empty() && proxies_ == 0 && calls_ == 0 && normalCall_;
1007 void Bridge::terminateWhenUnused(bool unused) {
1008 if (unused) {
1009 // That the current thread considers the bridge unused implies that it
1010 // is not within an incoming or outgoing remote call (so calling
1011 // terminate cannot lead to deadlock):
1012 terminate(false);
1016 void Bridge::checkDisposed() {
1017 assert(state_ != STATE_INITIAL);
1018 if (state_ != STATE_STARTED) {
1019 throw css::lang::DisposedException(
1020 "Binary URP bridge already disposed",
1021 static_cast< cppu::OWeakObject * >(this));
1027 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */