Update ooo320-m1
[ooovba.git] / extensions / source / plugin / unx / plugcon.cxx
blob919d1676762bc90a114e77e36ddab4112142ceb2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: plugcon.cxx,v $
10 * $Revision: 1.8.90.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
33 #include <plugin/unx/plugcon.hxx>
35 #include <cstdarg>
36 #include <vector>
38 UINT32 PluginConnector::GetStreamID( NPStream* pStream )
40 size_t nLen = m_aNPWrapStreams.size();
41 for( size_t i = 0; i < nLen; i++ )
42 if( m_aNPWrapStreams[ i ] == pStream )
43 return static_cast<UINT32>(i);
44 medDebug( 1, "Error: NPStream has no ID\n" );
45 return UnknownStreamID;
48 UINT32 PluginConnector::GetNPPID( NPP instance )
50 size_t nLen = m_aInstances.size();
51 for( size_t i=0; i <nLen; i++ )
52 if( m_aInstances[ i ]->instance == instance )
53 return static_cast<UINT32>(i);
54 medDebug( 1, "Error: NPP has no ID\n" );
56 return UnknownNPPID;
59 ConnectorInstance* PluginConnector::getInstance( NPP instance )
61 size_t nLen = m_aInstances.size();
62 for( size_t i=0; i <nLen; i++ )
64 ConnectorInstance* pInst = m_aInstances[i];
65 if( pInst->instance == instance )
66 return pInst;
68 return NULL;
71 ConnectorInstance* PluginConnector::getInstanceById( UINT32 nInstanceID )
73 return nInstanceID < static_cast<UINT32>(m_aInstances.size()) ? m_aInstances[ nInstanceID ] : NULL;
76 struct PtrStruct
78 char* pData;
79 ULONG nBytes;
81 PtrStruct( char* i_pData, ULONG i_nBytes )
82 : pData( i_pData ), nBytes( i_nBytes ) {}
85 ULONG PluginConnector::FillBuffer( char*& rpBuffer,
86 const char* pFunction,
87 ULONG nFunctionLen,
88 va_list ap )
90 std::vector< PtrStruct > aList;
91 aList.reserve( 5 );
93 ULONG nDataSize = nFunctionLen + sizeof( ULONG );
94 char* pNext;
96 do {
97 pNext = va_arg( ap, char* );
98 if( pNext )
100 aList.push_back( PtrStruct( pNext, va_arg( ap, ULONG ) ) );
101 nDataSize += aList.back().nBytes + sizeof(ULONG);
103 } while( pNext );
105 rpBuffer = new char[ nDataSize ];
106 char* pRun = rpBuffer;
107 memcpy( pRun, &nFunctionLen, sizeof( nFunctionLen ) );
108 pRun += sizeof( nFunctionLen );
109 memcpy( pRun, pFunction, nFunctionLen );
110 pRun += nFunctionLen;
112 for( std::vector<PtrStruct>::const_iterator it = aList.begin(); it != aList.end(); ++it )
114 memcpy( pRun, &it->nBytes, sizeof( ULONG ) );
115 pRun += sizeof( ULONG );
116 memcpy( pRun, it->pData, it->nBytes );
117 pRun += it->nBytes;
119 return nDataSize;
122 MediatorMessage* PluginConnector::Transact( const char* pFunction,
123 ULONG nFunctionLen, ... )
125 va_list ap;
126 char* pBuffer;
128 va_start( ap, nFunctionLen );
129 ULONG nSize = FillBuffer( pBuffer, pFunction, nFunctionLen, ap );
130 va_end( ap );
131 return TransactMessage( nSize, pBuffer );
134 MediatorMessage* PluginConnector::Transact( UINT32 nFunction, ... )
136 va_list ap;
137 char* pBuffer;
139 va_start( ap, nFunction );
140 ULONG nSize = FillBuffer( pBuffer, (char*)&nFunction, sizeof( nFunction ), ap );
141 va_end( ap );
142 return TransactMessage( nSize, pBuffer );
145 ULONG PluginConnector::Send( UINT32 nFunction, ... )
147 va_list ap;
148 char* pBuffer;
150 va_start( ap, nFunction );
151 ULONG nSize = FillBuffer( pBuffer, (char*)&nFunction, sizeof( nFunction ), ap );
152 va_end( ap );
153 return SendMessage( nSize, pBuffer );
156 void PluginConnector::Respond( ULONG nID,
157 char* pFunction,
158 ULONG nFunctionLen, ... )
160 va_list ap;
161 char* pBuffer;
163 va_start( ap, nFunctionLen );
164 ULONG nSize = FillBuffer( pBuffer, pFunction, nFunctionLen, ap );
165 va_end( ap );
166 SendMessage( nSize, pBuffer, nID | ( 1 << 24 ) );
169 MediatorMessage* PluginConnector::WaitForAnswer( ULONG nMessageID )
171 if( ! m_bValid )
172 return NULL;
174 nMessageID &= 0x00ffffff;
175 while( m_pListener )
178 NAMESPACE_VOS(OGuard) aGuard( m_aQueueMutex );
179 for( size_t i = 0; i < m_aMessageQueue.size(); i++ )
181 MediatorMessage* pMessage = m_aMessageQueue[ i ];
182 ULONG nID = pMessage->m_nID;
183 if( ( nID & 0xff000000 ) &&
184 ( ( nID & 0x00ffffff ) == nMessageID ) )
186 m_aMessageQueue.erase( m_aMessageQueue.begin() + i );
187 return pMessage;
191 if( ! m_aMessageQueue.empty() )
192 CallWorkHandler();
193 WaitForMessage( 2000 );
195 return NULL;
198 ConnectorInstance::ConnectorInstance( NPP inst, char* type,
199 int args, char* pargnbuf, ULONG nargnbytes,
200 char* pargvbuf, ULONG nargvbytes,
201 char* savedata, ULONG savebytes ) :
202 instance( inst ),
203 pShell( NULL ),
204 pWidget( NULL ),
205 pForm( NULL ),
206 pGtkWindow( NULL ),
207 pGtkWidget( NULL ),
208 bShouldUseXEmbed( false ),
209 nArg( args ),
210 pArgnBuf( pargnbuf ),
211 pArgvBuf( pargvbuf )
213 memset( &window, 0, sizeof(window) );
214 pMimeType = new char[ strlen( type ) + 1 ];
215 strcpy( pMimeType, type );
216 aData.len = savebytes;
217 aData.buf = savedata;
218 argn = new char*[ nArg ];
219 argv = new char*[ nArg ];
220 int i;
221 char* pRun = pArgnBuf;
222 for( i = 0; i < nArg; i++ )
224 argn[i] = pRun;
225 while( *pRun != 0 && (ULONG)(pRun - pArgnBuf) < nargnbytes )
226 pRun++;
227 if( (ULONG)(pRun - pArgnBuf) < nargnbytes )
228 pRun++;
230 pRun = pArgvBuf;
231 for( i = 0; i < nArg; i++ )
233 argv[i] = pRun;
234 while( *pRun != 0 && (ULONG)(pRun - pArgvBuf) < nargvbytes )
235 pRun++;
236 if( (ULONG)(pRun - pArgvBuf) < nargvbytes )
237 pRun++;
241 ConnectorInstance::~ConnectorInstance()
243 delete [] pMimeType;
244 delete [] argn;
245 delete [] argv;
246 delete [] pArgnBuf;
247 delete [] pArgvBuf;
248 delete [] (char*)aData.buf;
251 const char* GetCommandName( CommandAtoms eCommand )
253 switch( eCommand )
255 case eNPN_GetURL: return "NPN_GetURL";
256 case eNPN_GetURLNotify: return "NPN_GetURLNotify";
257 case eNPN_DestroyStream: return "NPN_DestroyStream";
258 case eNPN_NewStream: return "NPN_NewStream";
259 case eNPN_PostURLNotify: return "NPN_PostURLNotify";
260 case eNPN_PostURL: return "NPN_PostURL";
261 case eNPN_RequestRead: return "NPN_RequestRead";
262 case eNPN_Status: return "NPN_Status";
263 case eNPN_Version: return "NPN_Version";
264 case eNPN_Write: return "NPN_Write";
265 case eNPN_UserAgent: return "NPN_UserAgent";
267 case eNPP_DestroyStream: return "NPP_DestroyStream";
268 case eNPP_Destroy: return "NPP_Destroy";
269 case eNPP_DestroyPhase2: return "NPP_DestroyPhase2";
270 case eNPP_NewStream: return "NPP_NewStream";
271 case eNPP_New: return "NPP_New";
272 case eNPP_SetWindow: return "NPP_SetWindow";
273 case eNPP_StreamAsFile: return "NPP_StreamAsFile";
274 case eNPP_URLNotify: return "NPP_URLNotify";
275 case eNPP_WriteReady: return "NPP_WriteReady";
276 case eNPP_Write: return "NPP_Write";
277 case eNPP_GetMIMEDescription: return "NPP_GetMIMEDescription";
278 case eNPP_Initialize: return "NPP_Initialize";
279 case eNPP_Shutdown: return "NPP_Shutdown";
281 case eMaxCommand: return "eMaxCommand";
282 default: return "unknown command";
284 return NULL;