bump product version to 5.0.4.1
[LibreOffice.git] / extensions / source / plugin / unx / plugcon.cxx
blobfe0bd5d1251134ec086e18999dc8df65d98c7a6e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #ifdef AIX
31 #define _LINUX_SOURCE_COMPAT
32 #include <sys/timer.h>
33 #undef _LINUX_SOURCE_COMPAT
34 #endif
36 #include <sal/log.hxx>
38 #include <plugin/unx/plugcon.hxx>
40 #include <cstdarg>
41 #include <vector>
43 sal_uInt32 PluginConnector::GetStreamID( NPStream* pStream )
45 size_t nLen = m_aNPWrapStreams.size();
46 for( size_t i = 0; i < nLen; i++ )
47 if( m_aNPWrapStreams[ i ] == pStream )
48 return static_cast<sal_uInt32>(i);
49 SAL_WARN("extensions.plugin", "NPStream has no ID");
50 return UnknownStreamID;
53 sal_uInt32 PluginConnector::GetNPPID( NPP instance )
55 size_t nLen = m_aInstances.size();
56 for( size_t i=0; i <nLen; i++ )
57 if( m_aInstances[ i ]->instance == instance )
58 return static_cast<sal_uInt32>(i);
59 SAL_WARN("extensions.plugin", "NPP has no ID");
61 return UnknownNPPID;
64 struct PtrStruct
66 char* pData;
67 sal_uLong nBytes;
69 PtrStruct( char* i_pData, sal_uLong i_nBytes )
70 : pData( i_pData ), nBytes( i_nBytes ) {}
73 sal_uLong PluginConnector::FillBuffer( char*& rpBuffer,
74 const char* pFunction,
75 sal_uLong nFunctionLen,
76 va_list ap )
78 std::vector< PtrStruct > aList;
79 aList.reserve( 5 );
81 sal_uLong nDataSize = nFunctionLen + sizeof( sal_uLong );
82 char* pNext;
84 do {
85 pNext = va_arg( ap, char* );
86 if( pNext )
88 aList.push_back( PtrStruct( pNext, va_arg( ap, sal_uLong ) ) );
89 nDataSize += aList.back().nBytes + sizeof(sal_uLong);
91 } while( pNext );
93 rpBuffer = new char[ nDataSize ];
94 char* pRun = rpBuffer;
95 memcpy( pRun, &nFunctionLen, sizeof( nFunctionLen ) );
96 pRun += sizeof( nFunctionLen );
97 memcpy( pRun, pFunction, nFunctionLen );
98 pRun += nFunctionLen;
100 for( std::vector<PtrStruct>::const_iterator it = aList.begin(); it != aList.end(); ++it )
102 memcpy( pRun, &it->nBytes, sizeof( sal_uLong ) );
103 pRun += sizeof( sal_uLong );
104 memcpy( pRun, it->pData, it->nBytes );
105 pRun += it->nBytes;
107 return nDataSize;
110 MediatorMessage* PluginConnector::Transact( const char* pFunction,
111 sal_uLong nFunctionLen, ... )
113 va_list ap;
114 char* pBuffer;
116 va_start( ap, nFunctionLen );
117 sal_uLong nSize = FillBuffer( pBuffer, pFunction, nFunctionLen, ap );
118 va_end( ap );
119 MediatorMessage* pRet = TransactMessage( nSize, pBuffer );
120 delete[] pBuffer;
121 return pRet;
124 MediatorMessage* PluginConnector::Transact( sal_uInt32 nFunction, ... )
126 va_list ap;
127 char* pBuffer;
129 va_start( ap, nFunction );
130 sal_uLong nSize = FillBuffer( pBuffer, reinterpret_cast<char*>(&nFunction), sizeof( nFunction ), ap );
131 va_end( ap );
132 MediatorMessage* pRet = TransactMessage( nSize, pBuffer );
133 delete[] pBuffer;
134 return pRet;
137 sal_uLong PluginConnector::Send( sal_uInt32 nFunction, ... )
139 va_list ap;
140 char* pBuffer;
142 va_start( ap, nFunction );
143 sal_uLong nSize = FillBuffer( pBuffer, reinterpret_cast<char*>(&nFunction), sizeof( nFunction ), ap );
144 va_end( ap );
145 sal_uLong nRet = SendMessage( nSize, pBuffer );
146 delete[] pBuffer;
147 return nRet;
150 void PluginConnector::Respond( sal_uLong nID,
151 char* pFunction,
152 sal_uLong nFunctionLen, ... )
154 va_list ap;
155 char* pBuffer;
157 va_start( ap, nFunctionLen );
158 sal_uLong nSize = FillBuffer( pBuffer, pFunction, nFunctionLen, ap );
159 va_end( ap );
160 SendMessage( nSize, pBuffer, nID | ( 1 << 24 ) );
161 delete[] pBuffer;
164 MediatorMessage* PluginConnector::WaitForAnswer( sal_uLong nMessageID )
166 if( ! m_bValid )
167 return NULL;
169 nMessageID &= 0x00ffffff;
170 while( m_pListener )
173 osl::MutexGuard aGuard( m_aQueueMutex );
174 for( size_t i = 0; i < m_aMessageQueue.size(); i++ )
176 MediatorMessage* pMessage = m_aMessageQueue[ i ];
177 sal_uLong nID = pMessage->m_nID;
178 if( ( nID & 0xff000000 ) &&
179 ( ( nID & 0x00ffffff ) == nMessageID ) )
181 m_aMessageQueue.erase( m_aMessageQueue.begin() + i );
182 return pMessage;
186 if( ! m_aMessageQueue.empty() )
187 CallWorkHandler();
188 WaitForMessage( 2000 );
190 return NULL;
193 ConnectorInstance::ConnectorInstance( NPP inst, char* type,
194 int args, char* pargnbuf, sal_uLong nargnbytes,
195 char* pargvbuf, sal_uLong nargvbytes,
196 char* savedata, sal_uLong savebytes ) :
197 instance( inst ),
198 pShell( NULL ),
199 pWidget( NULL ),
200 pForm( NULL ),
201 pGtkWindow( NULL ),
202 pGtkWidget( NULL ),
203 bShouldUseXEmbed( false ),
204 nArg( args ),
205 pArgnBuf( pargnbuf ),
206 pArgvBuf( pargvbuf )
208 memset( &window, 0, sizeof(window) );
209 pMimeType = new char[ strlen( type ) + 1 ];
210 strcpy( pMimeType, type );
211 aData.len = savebytes;
212 aData.buf = savedata;
213 argn = new char*[ nArg ];
214 argv = new char*[ nArg ];
215 int i;
216 char* pRun = pArgnBuf;
217 for( i = 0; i < nArg; i++ )
219 argn[i] = pRun;
220 while( *pRun != 0 && (sal_uLong)(pRun - pArgnBuf) < nargnbytes )
221 pRun++;
222 if( (sal_uLong)(pRun - pArgnBuf) < nargnbytes )
223 pRun++;
225 pRun = pArgvBuf;
226 for( i = 0; i < nArg; i++ )
228 argv[i] = pRun;
229 while( *pRun != 0 && (sal_uLong)(pRun - pArgvBuf) < nargvbytes )
230 pRun++;
231 if( (sal_uLong)(pRun - pArgvBuf) < nargvbytes )
232 pRun++;
236 ConnectorInstance::~ConnectorInstance()
238 delete [] pMimeType;
239 delete [] argn;
240 delete [] argv;
241 delete [] pArgnBuf;
242 delete [] pArgvBuf;
243 delete [] static_cast<char*>(aData.buf);
246 const char* GetCommandName( CommandAtoms eCommand )
248 switch( eCommand )
250 case eNPN_GetURL: return "NPN_GetURL";
251 case eNPN_GetURLNotify: return "NPN_GetURLNotify";
252 case eNPN_DestroyStream: return "NPN_DestroyStream";
253 case eNPN_NewStream: return "NPN_NewStream";
254 case eNPN_PostURLNotify: return "NPN_PostURLNotify";
255 case eNPN_PostURL: return "NPN_PostURL";
256 case eNPN_RequestRead: return "NPN_RequestRead";
257 case eNPN_Status: return "NPN_Status";
258 case eNPN_Version: return "NPN_Version";
259 case eNPN_Write: return "NPN_Write";
260 case eNPN_UserAgent: return "NPN_UserAgent";
262 case eNPP_DestroyStream: return "NPP_DestroyStream";
263 case eNPP_Destroy: return "NPP_Destroy";
264 case eNPP_DestroyPhase2: return "NPP_DestroyPhase2";
265 case eNPP_NewStream: return "NPP_NewStream";
266 case eNPP_New: return "NPP_New";
267 case eNPP_SetWindow: return "NPP_SetWindow";
268 case eNPP_StreamAsFile: return "NPP_StreamAsFile";
269 case eNPP_URLNotify: return "NPP_URLNotify";
270 case eNPP_WriteReady: return "NPP_WriteReady";
271 case eNPP_Write: return "NPP_Write";
272 case eNPP_GetMIMEDescription: return "NPP_GetMIMEDescription";
273 case eNPP_Initialize: return "NPP_Initialize";
274 case eNPP_Shutdown: return "NPP_Shutdown";
276 case eMaxCommand: return "eMaxCommand";
277 default: return "unknown command";
279 return NULL;
282 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */