merged tag ooo/OOO330_m14
[LibreOffice.git] / extensions / source / plugin / unx / plugcon.cxx
blob74d8fa8bd2da959ba139c93672f78068ef20e754
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_extensions.hxx"
30 #include <plugin/unx/plugcon.hxx>
32 #include <cstdarg>
33 #include <vector>
35 UINT32 PluginConnector::GetStreamID( NPStream* pStream )
37 size_t nLen = m_aNPWrapStreams.size();
38 for( size_t i = 0; i < nLen; i++ )
39 if( m_aNPWrapStreams[ i ] == pStream )
40 return static_cast<UINT32>(i);
41 medDebug( 1, "Error: NPStream has no ID\n" );
42 return UnknownStreamID;
45 UINT32 PluginConnector::GetNPPID( NPP instance )
47 size_t nLen = m_aInstances.size();
48 for( size_t i=0; i <nLen; i++ )
49 if( m_aInstances[ i ]->instance == instance )
50 return static_cast<UINT32>(i);
51 medDebug( 1, "Error: NPP has no ID\n" );
53 return UnknownNPPID;
56 ConnectorInstance* PluginConnector::getInstance( NPP instance )
58 size_t nLen = m_aInstances.size();
59 for( size_t i=0; i <nLen; i++ )
61 ConnectorInstance* pInst = m_aInstances[i];
62 if( pInst->instance == instance )
63 return pInst;
65 return NULL;
68 ConnectorInstance* PluginConnector::getInstanceById( UINT32 nInstanceID )
70 return nInstanceID < static_cast<UINT32>(m_aInstances.size()) ? m_aInstances[ nInstanceID ] : NULL;
73 struct PtrStruct
75 char* pData;
76 ULONG nBytes;
78 PtrStruct( char* i_pData, ULONG i_nBytes )
79 : pData( i_pData ), nBytes( i_nBytes ) {}
82 ULONG PluginConnector::FillBuffer( char*& rpBuffer,
83 const char* pFunction,
84 ULONG nFunctionLen,
85 va_list ap )
87 std::vector< PtrStruct > aList;
88 aList.reserve( 5 );
90 ULONG nDataSize = nFunctionLen + sizeof( ULONG );
91 char* pNext;
93 do {
94 pNext = va_arg( ap, char* );
95 if( pNext )
97 aList.push_back( PtrStruct( pNext, va_arg( ap, ULONG ) ) );
98 nDataSize += aList.back().nBytes + sizeof(ULONG);
100 } while( pNext );
102 rpBuffer = new char[ nDataSize ];
103 char* pRun = rpBuffer;
104 memcpy( pRun, &nFunctionLen, sizeof( nFunctionLen ) );
105 pRun += sizeof( nFunctionLen );
106 memcpy( pRun, pFunction, nFunctionLen );
107 pRun += nFunctionLen;
109 for( std::vector<PtrStruct>::const_iterator it = aList.begin(); it != aList.end(); ++it )
111 memcpy( pRun, &it->nBytes, sizeof( ULONG ) );
112 pRun += sizeof( ULONG );
113 memcpy( pRun, it->pData, it->nBytes );
114 pRun += it->nBytes;
116 return nDataSize;
119 MediatorMessage* PluginConnector::Transact( const char* pFunction,
120 ULONG nFunctionLen, ... )
122 va_list ap;
123 char* pBuffer;
125 va_start( ap, nFunctionLen );
126 ULONG nSize = FillBuffer( pBuffer, pFunction, nFunctionLen, ap );
127 va_end( ap );
128 return TransactMessage( nSize, pBuffer );
131 MediatorMessage* PluginConnector::Transact( UINT32 nFunction, ... )
133 va_list ap;
134 char* pBuffer;
136 va_start( ap, nFunction );
137 ULONG nSize = FillBuffer( pBuffer, (char*)&nFunction, sizeof( nFunction ), ap );
138 va_end( ap );
139 return TransactMessage( nSize, pBuffer );
142 ULONG PluginConnector::Send( UINT32 nFunction, ... )
144 va_list ap;
145 char* pBuffer;
147 va_start( ap, nFunction );
148 ULONG nSize = FillBuffer( pBuffer, (char*)&nFunction, sizeof( nFunction ), ap );
149 va_end( ap );
150 return SendMessage( nSize, pBuffer );
153 void PluginConnector::Respond( ULONG nID,
154 char* pFunction,
155 ULONG nFunctionLen, ... )
157 va_list ap;
158 char* pBuffer;
160 va_start( ap, nFunctionLen );
161 ULONG nSize = FillBuffer( pBuffer, pFunction, nFunctionLen, ap );
162 va_end( ap );
163 SendMessage( nSize, pBuffer, nID | ( 1 << 24 ) );
166 MediatorMessage* PluginConnector::WaitForAnswer( ULONG nMessageID )
168 if( ! m_bValid )
169 return NULL;
171 nMessageID &= 0x00ffffff;
172 while( m_pListener )
175 NAMESPACE_VOS(OGuard) aGuard( m_aQueueMutex );
176 for( size_t i = 0; i < m_aMessageQueue.size(); i++ )
178 MediatorMessage* pMessage = m_aMessageQueue[ i ];
179 ULONG nID = pMessage->m_nID;
180 if( ( nID & 0xff000000 ) &&
181 ( ( nID & 0x00ffffff ) == nMessageID ) )
183 m_aMessageQueue.erase( m_aMessageQueue.begin() + i );
184 return pMessage;
188 if( ! m_aMessageQueue.empty() )
189 CallWorkHandler();
190 WaitForMessage( 2000 );
192 return NULL;
195 ConnectorInstance::ConnectorInstance( NPP inst, char* type,
196 int args, char* pargnbuf, ULONG nargnbytes,
197 char* pargvbuf, ULONG nargvbytes,
198 char* savedata, ULONG savebytes ) :
199 instance( inst ),
200 pShell( NULL ),
201 pWidget( NULL ),
202 pForm( NULL ),
203 pGtkWindow( NULL ),
204 pGtkWidget( NULL ),
205 bShouldUseXEmbed( false ),
206 nArg( args ),
207 pArgnBuf( pargnbuf ),
208 pArgvBuf( pargvbuf )
210 memset( &window, 0, sizeof(window) );
211 pMimeType = new char[ strlen( type ) + 1 ];
212 strcpy( pMimeType, type );
213 aData.len = savebytes;
214 aData.buf = savedata;
215 argn = new char*[ nArg ];
216 argv = new char*[ nArg ];
217 int i;
218 char* pRun = pArgnBuf;
219 for( i = 0; i < nArg; i++ )
221 argn[i] = pRun;
222 while( *pRun != 0 && (ULONG)(pRun - pArgnBuf) < nargnbytes )
223 pRun++;
224 if( (ULONG)(pRun - pArgnBuf) < nargnbytes )
225 pRun++;
227 pRun = pArgvBuf;
228 for( i = 0; i < nArg; i++ )
230 argv[i] = pRun;
231 while( *pRun != 0 && (ULONG)(pRun - pArgvBuf) < nargvbytes )
232 pRun++;
233 if( (ULONG)(pRun - pArgvBuf) < nargvbytes )
234 pRun++;
238 ConnectorInstance::~ConnectorInstance()
240 delete [] pMimeType;
241 delete [] argn;
242 delete [] argv;
243 delete [] pArgnBuf;
244 delete [] pArgvBuf;
245 delete [] (char*)aData.buf;
248 const char* GetCommandName( CommandAtoms eCommand )
250 switch( eCommand )
252 case eNPN_GetURL: return "NPN_GetURL";
253 case eNPN_GetURLNotify: return "NPN_GetURLNotify";
254 case eNPN_DestroyStream: return "NPN_DestroyStream";
255 case eNPN_NewStream: return "NPN_NewStream";
256 case eNPN_PostURLNotify: return "NPN_PostURLNotify";
257 case eNPN_PostURL: return "NPN_PostURL";
258 case eNPN_RequestRead: return "NPN_RequestRead";
259 case eNPN_Status: return "NPN_Status";
260 case eNPN_Version: return "NPN_Version";
261 case eNPN_Write: return "NPN_Write";
262 case eNPN_UserAgent: return "NPN_UserAgent";
264 case eNPP_DestroyStream: return "NPP_DestroyStream";
265 case eNPP_Destroy: return "NPP_Destroy";
266 case eNPP_DestroyPhase2: return "NPP_DestroyPhase2";
267 case eNPP_NewStream: return "NPP_NewStream";
268 case eNPP_New: return "NPP_New";
269 case eNPP_SetWindow: return "NPP_SetWindow";
270 case eNPP_StreamAsFile: return "NPP_StreamAsFile";
271 case eNPP_URLNotify: return "NPP_URLNotify";
272 case eNPP_WriteReady: return "NPP_WriteReady";
273 case eNPP_Write: return "NPP_Write";
274 case eNPP_GetMIMEDescription: return "NPP_GetMIMEDescription";
275 case eNPP_Initialize: return "NPP_Initialize";
276 case eNPP_Shutdown: return "NPP_Shutdown";
278 case eMaxCommand: return "eMaxCommand";
279 default: return "unknown command";
281 return NULL;