Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / desktop / source / offacc / acceptor.cxx
blobb8612f668b2225027afa839cd3083236584a4f43
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 "acceptor.hxx"
23 #include <com/sun/star/bridge/BridgeFactory.hpp>
24 #include <com/sun/star/connection/Acceptor.hpp>
25 #include <com/sun/star/uno/XNamingService.hpp>
26 #include <cppuhelper/supportsservice.hxx>
27 #include <sal/log.hxx>
28 #include <comphelper/diagnose_ex.hxx>
29 #include <o3tl/string_view.hxx>
31 using namespace css::bridge;
32 using namespace css::connection;
33 using namespace css::lang;
34 using namespace css::uno;
36 namespace desktop
39 extern "C" {
41 static void offacc_workerfunc (void * acc)
43 osl_setThreadName("URP Acceptor");
45 static_cast<Acceptor*>(acc)->run();
50 Acceptor::Acceptor( const Reference< XComponentContext >& rxContext )
51 : m_thread(nullptr)
52 , m_rContext(rxContext)
53 , m_bInit(false)
54 , m_bDying(false)
56 m_rAcceptor = css::connection::Acceptor::create(m_rContext);
57 m_rBridgeFactory = BridgeFactory::create(m_rContext);
61 Acceptor::~Acceptor()
63 m_rAcceptor->stopAccepting();
64 oslThread t;
66 std::unique_lock g(m_aMutex);
67 t = m_thread;
69 //prevent locking if the thread is still waiting
70 m_bDying = true;
71 m_cEnable.set();
72 osl_joinWithThread(t);
73 osl_destroyThread(t);
75 // Make the final state of m_bridges visible to this thread (since
76 // m_thread is joined, the code that follows is the only one left
77 // accessing m_bridges):
78 std::unique_lock g(m_aMutex);
80 for (;;) {
81 css::uno::Reference< css::bridge::XBridge > b(m_bridges.remove());
82 if (!b.is()) {
83 break;
85 css::uno::Reference< css::lang::XComponent >(
86 b, css::uno::UNO_QUERY_THROW)->dispose();
90 void Acceptor::run()
92 SAL_INFO( "desktop.offacc", "Acceptor::run" );
93 for (;;)
95 try
97 // wait until we get enabled
98 SAL_INFO( "desktop.offacc",
99 "Acceptor::run waiting for office to come up");
100 m_cEnable.wait();
101 if (m_bDying) //see destructor
102 break;
103 SAL_INFO( "desktop.offacc",
104 "Acceptor::run now enabled and continuing");
106 // accept connection
107 Reference< XConnection > rConnection = m_rAcceptor->accept( m_aConnectString );
108 // if we return without a valid connection we must assume that the acceptor
109 // is destructed so we break out of the run method terminating the thread
110 if (! rConnection.is()) break;
111 OUString aDescription = rConnection->getDescription();
112 SAL_INFO( "desktop.offacc", "Acceptor::run connection " << aDescription );
114 // create instanceprovider for this connection
115 Reference< XInstanceProvider > rInstanceProvider(new AccInstanceProvider(m_rContext));
116 // create the bridge. The remote end will have a reference to this bridge
117 // thus preventing the bridge from being disposed. When the remote end releases
118 // the bridge, it will be destructed.
119 Reference< XBridge > rBridge = m_rBridgeFactory->createBridge(
120 "", m_aProtocol, rConnection, rInstanceProvider);
121 std::unique_lock g(m_aMutex);
122 m_bridges.add(rBridge);
123 } catch (const Exception&) {
124 TOOLS_WARN_EXCEPTION("desktop.offacc", "");
125 // connection failed...
126 // something went wrong during connection setup.
127 // just wait for a new connection to accept
132 // XInitialize
133 void Acceptor::initialize( const Sequence<Any>& aArguments )
135 // prevent multiple initialization
136 std::unique_lock aGuard( m_aMutex );
137 SAL_INFO( "desktop.offacc", "Acceptor::initialize()" );
139 bool bOk = false;
141 // arg count
142 int nArgs = aArguments.getLength();
144 // not yet initialized and accept-string
145 if (!m_bInit && nArgs > 0 && (aArguments[0] >>= m_aAcceptString))
147 SAL_INFO( "desktop.offacc", "Acceptor::initialize string=" << m_aAcceptString );
149 // get connect string and protocol from accept string
150 // "<connectString>;<protocol>"
151 sal_Int32 nIndex1 = m_aAcceptString.indexOf( ';' );
152 if (nIndex1 < 0)
153 throw IllegalArgumentException(
154 "Invalid accept-string format", m_rContext, 1);
155 m_aConnectString = o3tl::trim(m_aAcceptString.subView( 0 , nIndex1 ));
156 nIndex1++;
157 sal_Int32 nIndex2 = m_aAcceptString.indexOf( ';' , nIndex1 );
158 if (nIndex2 < 0) nIndex2 = m_aAcceptString.getLength();
159 m_aProtocol = m_aAcceptString.copy( nIndex1, nIndex2 - nIndex1 );
161 // start accepting in new thread...
162 m_thread = osl_createThread(offacc_workerfunc, this);
163 m_bInit = true;
164 bOk = true;
167 // do we want to enable accepting?
168 bool bEnable = false;
169 if (((nArgs == 1 && (aArguments[0] >>= bEnable)) ||
170 (nArgs == 2 && (aArguments[1] >>= bEnable))) &&
171 bEnable )
173 m_cEnable.set();
174 bOk = true;
177 if (!bOk)
179 throw IllegalArgumentException( "invalid initialization", m_rContext, 1);
183 // XServiceInfo
184 OUString Acceptor::getImplementationName()
186 return "com.sun.star.office.comp.Acceptor";
188 Sequence<OUString> Acceptor::getSupportedServiceNames()
190 return { "com.sun.star.office.Acceptor" };
193 sal_Bool Acceptor::supportsService(OUString const & ServiceName)
195 return cppu::supportsService(this, ServiceName);
199 // InstanceProvider
200 AccInstanceProvider::AccInstanceProvider(const Reference<XComponentContext>& rxContext)
201 : m_rContext(rxContext)
205 AccInstanceProvider::~AccInstanceProvider()
209 Reference<XInterface> AccInstanceProvider::getInstance (const OUString& aName )
212 Reference<XInterface> rInstance;
214 if ( aName == "StarOffice.ServiceManager" )
216 rInstance.set( m_rContext->getServiceManager() );
218 else if ( aName == "StarOffice.ComponentContext" )
220 rInstance = m_rContext;
222 else if ( aName == "StarOffice.NamingService" )
224 Reference< XNamingService > rNamingService(
225 m_rContext->getServiceManager()->createInstanceWithContext("com.sun.star.uno.NamingService", m_rContext),
226 UNO_QUERY );
227 if ( rNamingService.is() )
229 rNamingService->registerObject( "StarOffice.ServiceManager", m_rContext->getServiceManager() );
230 rNamingService->registerObject( "StarOffice.ComponentContext", m_rContext );
231 rInstance = rNamingService;
234 return rInstance;
239 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
240 desktop_Acceptor_get_implementation(
241 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
243 return cppu::acquire(new desktop::Acceptor(context));
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */