Update ooo320-m1
[ooovba.git] / extensions / source / plugin / unx / nppapi.cxx
blobbfe9d4edc1fae534dfe0f4841a10e8c7e144ee78
1 /*************************************************************************
3 Source Code Control System - Header
5 $Header: /zpool/svn/migration/cvs_rep_09_09_08/code/extensions/source/plugin/unx/nppapi.cxx,v 1.7 2008-01-14 14:53:38 ihi Exp $
7 *************************************************************************/
9 // MARKER(update_precomp.py): autogen include statement, do not remove
10 #include "precompiled_extensions.hxx"
12 #if STLPORT_VERSION>=321
13 #include <cstdarg>
14 #endif
16 #include <plugin/impl.hxx>
17 #include <vcl/svapp.hxx>
19 std::vector<PluginConnector*> PluginConnector::allConnectors;
21 PluginConnector::PluginConnector( int nSocket ) :
22 Mediator( nSocket )
24 allConnectors.push_back( this );
25 SetNewMessageHdl( LINK( this, PluginConnector, NewMessageHdl ) );
28 PluginConnector::~PluginConnector()
30 NAMESPACE_VOS(OGuard) aGuard( m_aUserEventMutex );
31 for( std::vector< PluginConnector* >::iterator it = allConnectors.begin();
32 it != allConnectors.end(); ++it )
34 if( *it == this )
36 allConnectors.erase( it );
37 break;
42 IMPL_LINK( PluginConnector, NewMessageHdl, Mediator*, /*pMediator*/ )
44 NAMESPACE_VOS(OGuard) aGuard( m_aUserEventMutex );
45 bool bFound = false;
46 for( std::vector< PluginConnector* >::iterator it = allConnectors.begin();
47 it != allConnectors.end() && bFound == false; ++it )
49 if( *it == this )
50 bFound = true;
52 if( ! bFound )
53 return 0;
54 Application::PostUserEvent( LINK( this, PluginConnector, WorkOnNewMessageHdl ) );
55 return 0;
58 IMPL_LINK( PluginConnector, WorkOnNewMessageHdl, Mediator*, /*pMediator*/ )
60 bool bFound = false;
61 for( std::vector< PluginConnector* >::iterator it = allConnectors.begin();
62 it != allConnectors.end() && bFound == false; ++it )
64 if( *it == this )
65 bFound = true;
67 if( ! bFound )
68 return 0;
71 NAMESPACE_VOS(OGuard) aGuard( m_aUserEventMutex );
72 m_aUserEventIDs.pop_front();
76 MediatorMessage* pMessage;
77 CommandAtoms nCommand;
78 while( (pMessage = GetNextMessage( FALSE )) )
80 nCommand = (CommandAtoms)pMessage->GetUINT32();
81 medDebug( 1, "%s\n", GetCommandName( nCommand ) );
82 switch( nCommand )
84 case eNPN_GetURL:
86 UINT32 nInstance = pMessage->GetUINT32();
87 NPP instance = m_aInstances[ nInstance ]->instance;
88 char* pUrl = pMessage->GetString();
89 char* pWindow = pMessage->GetString();
90 NPError aRet = NPN_GetURL( instance, pUrl, pWindow );
91 Respond( pMessage->m_nID,
92 (char*)(&aRet), sizeof( NPError ), NULL );
93 delete [] pUrl;
94 delete [] pWindow;
96 break;
97 case eNPN_GetURLNotify:
99 UINT32 nInstance = pMessage->GetUINT32();
100 NPP instance = m_aInstances[ nInstance ]->instance;
101 char* pUrl = pMessage->GetString();
102 char* pWindow = pMessage->GetString();
103 void** pNotifyData = (void**)pMessage->GetBytes();
104 NPError aRet = NPN_GetURLNotify( instance, pUrl, pWindow,
105 *pNotifyData );
106 Respond( pMessage->m_nID,
107 (char*)(&aRet), sizeof( NPError ), NULL );
108 delete [] pUrl;
109 delete [] pWindow;
110 delete [] pNotifyData;
112 break;
113 case eNPN_DestroyStream:
115 UINT32 nInstance = pMessage->GetUINT32();
116 NPP instance = m_aInstances[ nInstance ]->instance;
117 UINT32 nFileID = pMessage->GetUINT32();
118 char* pUrl = pMessage->GetString();
119 NPError* pReason = (NPError*)pMessage->GetBytes();
120 NPError aRet = NPERR_FILE_NOT_FOUND;
121 if( nFileID < static_cast<UINT32>(m_aNPWrapStreams.size()) )
123 if( ! strcmp( m_aNPWrapStreams[ nFileID ]->url, pUrl ) )
125 aRet =
126 NPN_DestroyStream( instance, m_aNPWrapStreams[ nFileID ],
127 *pReason );
128 m_aNPWrapStreams.erase( m_aNPWrapStreams.begin() + nFileID );
130 else
131 medDebug( 1, "StreamID %d has incoherent urls %s and %s\n",
132 nFileID, pUrl, m_aNPWrapStreams[ nFileID ]->url );
134 else
135 medDebug( 1, "Nonexistent StreamID %d\n", nFileID );
137 Respond( pMessage->m_nID,
138 (char*)(&aRet), sizeof( NPError ), NULL );
140 delete [] pUrl;
141 delete [] pReason;
143 break;
144 case eNPN_NewStream:
146 UINT32 nInstance = pMessage->GetUINT32();
147 NPP instance = m_aInstances[ nInstance ]->instance;
148 NPMIMEType pType = pMessage->GetString();
149 char* pTarget = pMessage->GetString();
151 NPStream* pStream = NULL;
153 NPError aRet = NPN_NewStream( instance, pType, pTarget, &pStream );
155 if( aRet != NPERR_NO_ERROR )
157 UINT32 nDummy = 0;
158 Respond( pMessage->m_nID,
159 (char*)&aRet, sizeof( aRet ),
160 "", 0,
161 &nDummy, sizeof(UINT32),
162 &nDummy, sizeof(UINT32),
163 NULL );
165 else
167 m_aNPWrapStreams.push_back( pStream );
169 ULONG nLen = strlen( pStream->url );
170 Respond( pMessage->m_nID,
171 (char*)&aRet, sizeof( aRet ),
172 pStream->url, nLen,
173 &pStream->end, sizeof(UINT32),
174 &pStream->lastmodified, sizeof(UINT32),
175 NULL );
178 delete [] pTarget;
179 delete [] pType;
181 break;
182 case eNPN_PostURLNotify:
184 UINT32 nInstance = pMessage->GetUINT32();
185 NPP instance = m_aInstances[ nInstance ]->instance;
186 char* pUrl = pMessage->GetString();
187 char* pTarget = pMessage->GetString();
188 UINT32 nLen = pMessage->GetUINT32();
189 char* pBuf = (char*)pMessage->GetBytes();
190 NPBool* pFile = (NPBool*)pMessage->GetBytes();
191 void** pNData = (void**)pMessage->GetBytes();
192 NPError aRet =
193 NPN_PostURLNotify( instance, pUrl, pTarget, nLen, pBuf, *pFile, *pNData );
194 Respond( pMessage->m_nID, (char*)&aRet, sizeof( aRet ), NULL );
195 delete [] pUrl;
196 delete [] pTarget;
197 delete [] pBuf;
198 delete [] pFile;
199 delete [] pNData;
201 break;
202 case eNPN_PostURL:
204 UINT32 nInstance = pMessage->GetUINT32();
205 NPP instance = m_aInstances[ nInstance ]->instance;
206 char* pUrl = pMessage->GetString();
207 char* pWindow = pMessage->GetString();
208 UINT32 nLen = pMessage->GetUINT32();
209 char* pBuf = (char*)pMessage->GetBytes();
210 NPBool* pFile = (NPBool*)pMessage->GetBytes();
211 NPError aRet =
212 NPN_PostURL( instance, pUrl, pWindow, nLen, pBuf, *pFile );
213 Respond( pMessage->m_nID, (char*)&aRet, sizeof( aRet ), NULL );
214 delete [] pUrl;
215 delete [] pWindow;
216 delete [] pBuf;
217 delete [] pFile;
219 break;
220 case eNPN_RequestRead:
222 UINT32 nFileID = pMessage->GetUINT32();
223 NPStream* pStream = m_aNPWrapStreams[ nFileID ];
224 UINT32 nRanges = pMessage->GetUINT32();
225 UINT32* pArray = (UINT32*)pMessage->GetBytes();
226 // build ranges table
227 NPByteRange* pFirst = new NPByteRange;
228 NPByteRange* pRun = pFirst;
229 for( UINT32 n = 0; n < nRanges; n++ )
231 pRun->offset = pArray[ 2*n ];
232 pRun->length = pArray[ 2*n+1 ];
233 pRun->next = n < nRanges-1 ? new NPByteRange : NULL;
234 pRun = pRun->next;
236 NPError aRet = NPN_RequestRead( pStream, pFirst );
237 Respond( pMessage->m_nID, (char*)&aRet, sizeof( aRet ), NULL );
238 while( pFirst )
240 pRun = pFirst->next;
241 delete pFirst;
242 pFirst = pRun;
244 delete [] pArray;
246 break;
247 case eNPN_Status:
249 UINT32 nInstance = pMessage->GetUINT32();
250 NPP instance = m_aInstances[ nInstance ]->instance;
251 char* pString = pMessage->GetString();
252 NPN_Status( instance, pString );
253 delete [] pString;
255 break;
256 case eNPN_Version:
258 int major, minor, net_major, net_minor;
259 NPN_Version( &major, &minor, &net_major, &net_minor );
260 Respond( pMessage->m_nID,
261 (char*)&major, sizeof( int ),
262 &minor, sizeof( int ),
263 &net_major, sizeof( int ),
264 &net_minor, sizeof( int ),
265 NULL );
267 break;
268 case eNPN_Write:
270 UINT32 nInstance = pMessage->GetUINT32();
271 NPP instance = m_aInstances[ nInstance ]->instance;
272 UINT32 nFileID = pMessage->GetUINT32();
273 NPStream* pStream = m_aNPWrapStreams[ nFileID ];
274 INT32 nLen = pMessage->GetUINT32();
275 void* pBuffer = pMessage->GetBytes();
276 INT32 nRet = NPN_Write( instance, pStream, nLen, pBuffer );
277 Respond( pMessage->m_nID,
278 (char*)&nRet, sizeof( nRet ),
279 NULL );
280 delete [] (char*)pBuffer;
281 delete instance;
283 break;
284 case eNPN_UserAgent:
286 UINT32 nInstance = pMessage->GetUINT32();
287 NPP instance = m_aInstances[ nInstance ]->instance;
288 const char* pAnswer = NPN_UserAgent( instance );
289 Respond( pMessage->m_nID,
290 (char*)pAnswer, strlen( pAnswer ),
291 NULL );
293 break;
294 default:
295 medDebug( 1, "caught unknown NPN request %d\n", nCommand );
298 delete pMessage;
300 return 0;
303 #define GET_INSTANCE() \
304 UINT32 nInstance; \
305 nInstance = GetNPPID( instance );
307 #define GET_INSTANCE_RET( err ) \
308 GET_INSTANCE() \
309 if( nInstance == PluginConnector::UnknownNPPID ) \
310 return err
313 #define POST_INSTANCE() (char*)&nInstance, sizeof( nInstance )
315 NPError UnxPluginComm::NPP_Destroy( NPP instance, NPSavedData** save )
317 NPError aRet = NPERR_GENERIC_ERROR;
318 GET_INSTANCE_RET( aRet );
319 MediatorMessage* pMes =
320 Transact( eNPP_Destroy,
321 POST_INSTANCE(),
322 NULL );
323 if( ! pMes )
324 return NPERR_GENERIC_ERROR;
325 delete pMes;
327 pMes = Transact( eNPP_DestroyPhase2,
328 POST_INSTANCE(),
329 NULL );
330 if( ! pMes )
331 return NPERR_GENERIC_ERROR;
333 aRet = GetNPError( pMes );
334 ULONG nSaveBytes;
335 void* pSaveData = pMes->GetBytes( nSaveBytes );
336 if( nSaveBytes == 4 && *(UINT32*)pSaveData == 0 )
337 *save = NULL;
338 else
340 *save = new NPSavedData;
341 (*save)->len = nSaveBytes;
342 (*save)->buf = pSaveData;
344 delete pMes;
346 return aRet;
349 NPError UnxPluginComm::NPP_DestroyStream( NPP instance, NPStream* stream, NPError reason )
351 NPError aRet = NPERR_GENERIC_ERROR;
352 GET_INSTANCE_RET( aRet );
353 UINT32 nFileID = GetStreamID( stream );
354 if( nFileID == PluginConnector::UnknownStreamID )
355 return NPERR_GENERIC_ERROR;
357 MediatorMessage* pMes =
358 Transact( eNPP_DestroyStream,
359 POST_INSTANCE(),
360 &nFileID, sizeof( nFileID ),
361 &reason, sizeof( reason ),
362 NULL );
363 m_aNPWrapStreams.erase( m_aNPWrapStreams.begin() + nFileID );
364 if( ! pMes )
365 return NPERR_GENERIC_ERROR;
367 aRet = GetNPError( pMes );
368 delete pMes;
369 return aRet;
372 void* UnxPluginComm::NPP_GetJavaClass()
374 return NULL;
377 NPError UnxPluginComm::NPP_Initialize()
379 MediatorMessage* pMes =
380 Transact( eNPP_Initialize,
381 NULL );
382 if( ! pMes )
383 return NPERR_GENERIC_ERROR;
385 NPError aRet = GetNPError( pMes );
386 delete pMes;
387 return aRet;
390 NPError UnxPluginComm::NPP_New( NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc,
391 char* argn[], char* argv[], NPSavedData *saved )
393 m_aInstances.push_back(
394 new ConnectorInstance( instance, pluginType, 0,
395 NULL, 0, NULL, 0,
396 saved ? (char*)saved->buf : NULL,
397 saved ? saved->len : 0 ) );
399 char *pArgnBuf, *pArgvBuf;
400 size_t nArgnLen = 0, nArgvLen = 0;
401 int i;
402 for( i = 0; i < argc; i++ )
404 nArgnLen += strlen( argn[i] ) +1;
405 nArgvLen += strlen( argv[i] ) +1;
407 pArgnBuf = new char[ nArgnLen ];
408 pArgvBuf = new char[ nArgvLen ];
409 char* pRunArgn = pArgnBuf;
410 char* pRunArgv = pArgvBuf;
411 for( i = 0; i < argc; i++ )
413 strcpy( pRunArgn, argn[i] );
414 strcpy( pRunArgv, argv[i] );
415 pRunArgn += strlen( argn[i] ) +1;
416 pRunArgv += strlen( argv[i] ) +1;
419 MediatorMessage* pMes;
420 if( saved )
421 pMes =
422 Transact( eNPP_New,
423 pluginType, strlen( pluginType ),
424 &mode, sizeof( mode ),
425 &argc, sizeof( argc ),
426 pArgnBuf, nArgnLen,
427 pArgvBuf, nArgvLen,
428 saved->buf, static_cast<size_t>(saved->len),
429 NULL );
430 else
431 pMes =
432 Transact( eNPP_New,
433 pluginType, strlen( pluginType ),
434 &mode, sizeof( mode ),
435 &argc, sizeof( argc ),
436 pArgnBuf, nArgnLen,
437 pArgvBuf, nArgvLen,
438 "0000", size_t(4),
439 NULL );
440 delete [] pArgnBuf;
441 delete [] pArgvBuf;
442 if( ! pMes )
443 return NPERR_GENERIC_ERROR;
445 NPError aRet = GetNPError( pMes );
446 delete pMes;
448 return aRet;
451 NPError UnxPluginComm::NPP_NewStream( NPP instance, NPMIMEType type, NPStream* stream,
452 NPBool seekable, uint16* stype )
454 NPError aRet = NPERR_GENERIC_ERROR;
455 GET_INSTANCE_RET( aRet );
457 m_aNPWrapStreams.push_back( stream );
458 MediatorMessage* pMes =
459 Transact( eNPP_NewStream,
460 POST_INSTANCE(),
461 type, strlen( type ),
462 stream->url, strlen( stream->url ),
463 &stream->end, sizeof( stream->end ),
464 &stream->lastmodified, sizeof( stream->lastmodified ),
465 &seekable, sizeof( seekable ),
466 NULL );
468 if( ! pMes )
469 return NPERR_GENERIC_ERROR;
471 aRet = GetNPError( pMes );
472 uint16* pSType = (uint16*)pMes->GetBytes();
473 *stype = *pSType;
475 delete [] pSType;
476 delete pMes;
477 return aRet;
480 void UnxPluginComm::NPP_Print( NPP /*instance*/, NPPrint* /*platformPrint*/ )
484 NPError UnxPluginComm::NPP_SetWindow( NPP instance, NPWindow* window )
486 NPError aRet = NPERR_GENERIC_ERROR;
487 GET_INSTANCE_RET( aRet );
489 MediatorMessage* pMes =
490 Transact( eNPP_SetWindow,
491 POST_INSTANCE(),
492 window, sizeof( NPWindow ),
493 NULL );
494 if( ! pMes )
495 return NPERR_GENERIC_ERROR;
497 aRet = GetNPError( pMes );
498 delete pMes;
499 return aRet;
502 void UnxPluginComm::NPP_Shutdown()
504 Send( eNPP_Shutdown, NULL );
507 void UnxPluginComm::NPP_StreamAsFile( NPP instance, NPStream* stream, const char* fname )
509 GET_INSTANCE();
510 UINT32 nFileID = GetStreamID( stream );
511 if( nFileID == PluginConnector::UnknownStreamID )
512 return;
514 Send( eNPP_StreamAsFile,
515 POST_INSTANCE(),
516 &nFileID, sizeof( nFileID ),
517 fname, strlen( fname ),
518 NULL );
521 void UnxPluginComm::NPP_URLNotify( NPP instance, const char* url, NPReason reason, void* notifyData )
523 GET_INSTANCE();
525 Send( eNPP_URLNotify,
526 POST_INSTANCE(),
527 url, strlen( url ),
528 &reason, sizeof( reason ),
529 &notifyData, sizeof( void* ),
530 NULL );
533 int32 UnxPluginComm::NPP_Write( NPP instance, NPStream* stream, int32 offset, int32 len, void* buffer )
535 GET_INSTANCE_RET( -1 );
536 UINT32 nFileID = GetStreamID( stream );
537 if( nFileID == PluginConnector::UnknownStreamID )
538 return -1;
540 MediatorMessage* pMes =
541 Transact( eNPP_Write,
542 POST_INSTANCE(),
543 &nFileID, sizeof( nFileID ),
544 &offset, sizeof( offset ),
545 buffer, static_cast<size_t>(len),
546 NULL );
547 if( ! pMes )
548 return 0;
550 int32 aRet = pMes->GetUINT32();
551 delete pMes;
553 return aRet;
556 int32 UnxPluginComm::NPP_WriteReady( NPP instance, NPStream* stream )
558 GET_INSTANCE_RET( -1 );
559 UINT32 nFileID = GetStreamID( stream );
560 if( nFileID == PluginConnector::UnknownStreamID )
561 return -1;
563 MediatorMessage* pMes =
564 Transact( eNPP_WriteReady,
565 POST_INSTANCE(),
566 &nFileID, sizeof( nFileID ),
567 NULL );
569 if( ! pMes )
570 return 0;
572 int32 aRet = pMes->GetUINT32();
573 delete pMes;
575 return aRet;
578 char* UnxPluginComm::NPP_GetMIMEDescription()
580 static char* pDesc = NULL;
581 MediatorMessage* pMes =
582 Transact( eNPP_GetMIMEDescription,
583 NULL );
584 if( ! pMes )
585 return (char*)"";
587 if( pDesc )
588 delete [] pDesc;
589 pDesc = pMes->GetString();
590 delete pMes;
591 return pDesc;
594 NPError UnxPluginComm::NPP_GetValue( NPP /*instance*/, NPPVariable /*variable*/, void* /*value*/ )
596 return 0;
599 NPError UnxPluginComm::NPP_SetValue( NPP /*instance*/, NPNVariable /*variable*/, void* /*value*/ )
601 return 0;