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
16 #include <plugin/impl.hxx>
17 #include <vcl/svapp.hxx>
19 std::vector
<PluginConnector
*> PluginConnector::allConnectors
;
21 PluginConnector::PluginConnector( int 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
)
36 allConnectors
.erase( it
);
42 IMPL_LINK( PluginConnector
, NewMessageHdl
, Mediator
*, /*pMediator*/ )
44 NAMESPACE_VOS(OGuard
) aGuard( m_aUserEventMutex
);
46 for( std::vector
< PluginConnector
* >::iterator it
= allConnectors
.begin();
47 it
!= allConnectors
.end() && bFound
== false; ++it
)
54 Application::PostUserEvent( LINK( this, PluginConnector
, WorkOnNewMessageHdl
) );
58 IMPL_LINK( PluginConnector
, WorkOnNewMessageHdl
, Mediator
*, /*pMediator*/ )
61 for( std::vector
< PluginConnector
* >::iterator it
= allConnectors
.begin();
62 it
!= allConnectors
.end() && bFound
== false; ++it
)
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
) );
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
);
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
,
106 Respond( pMessage
->m_nID
,
107 (char*)(&aRet
), sizeof( NPError
), NULL
);
110 delete [] pNotifyData
;
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
) )
126 NPN_DestroyStream( instance
, m_aNPWrapStreams
[ nFileID
],
128 m_aNPWrapStreams
.erase( m_aNPWrapStreams
.begin() + nFileID
);
131 medDebug( 1, "StreamID %d has incoherent urls %s and %s\n",
132 nFileID
, pUrl
, m_aNPWrapStreams
[ nFileID
]->url
);
135 medDebug( 1, "Nonexistent StreamID %d\n", nFileID
);
137 Respond( pMessage
->m_nID
,
138 (char*)(&aRet
), sizeof( NPError
), NULL
);
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
)
158 Respond( pMessage
->m_nID
,
159 (char*)&aRet
, sizeof( aRet
),
161 &nDummy
, sizeof(UINT32
),
162 &nDummy
, sizeof(UINT32
),
167 m_aNPWrapStreams
.push_back( pStream
);
169 ULONG nLen
= strlen( pStream
->url
);
170 Respond( pMessage
->m_nID
,
171 (char*)&aRet
, sizeof( aRet
),
173 &pStream
->end
, sizeof(UINT32
),
174 &pStream
->lastmodified
, sizeof(UINT32
),
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();
193 NPN_PostURLNotify( instance
, pUrl
, pTarget
, nLen
, pBuf
, *pFile
, *pNData
);
194 Respond( pMessage
->m_nID
, (char*)&aRet
, sizeof( aRet
), NULL
);
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();
212 NPN_PostURL( instance
, pUrl
, pWindow
, nLen
, pBuf
, *pFile
);
213 Respond( pMessage
->m_nID
, (char*)&aRet
, sizeof( aRet
), NULL
);
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
;
236 NPError aRet
= NPN_RequestRead( pStream
, pFirst
);
237 Respond( pMessage
->m_nID
, (char*)&aRet
, sizeof( aRet
), NULL
);
249 UINT32 nInstance
= pMessage
->GetUINT32();
250 NPP instance
= m_aInstances
[ nInstance
]->instance
;
251 char* pString
= pMessage
->GetString();
252 NPN_Status( instance
, pString
);
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 ),
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
),
280 delete [] (char*)pBuffer
;
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
),
295 medDebug( 1, "caught unknown NPN request %d\n", nCommand
);
303 #define GET_INSTANCE() \
305 nInstance = GetNPPID( instance );
307 #define GET_INSTANCE_RET( err ) \
309 if( nInstance == PluginConnector::UnknownNPPID ) \
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
,
324 return NPERR_GENERIC_ERROR
;
327 pMes
= Transact( eNPP_DestroyPhase2
,
331 return NPERR_GENERIC_ERROR
;
333 aRet
= GetNPError( pMes
);
335 void* pSaveData
= pMes
->GetBytes( nSaveBytes
);
336 if( nSaveBytes
== 4 && *(UINT32
*)pSaveData
== 0 )
340 *save
= new NPSavedData
;
341 (*save
)->len
= nSaveBytes
;
342 (*save
)->buf
= pSaveData
;
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
,
360 &nFileID
, sizeof( nFileID
),
361 &reason
, sizeof( reason
),
363 m_aNPWrapStreams
.erase( m_aNPWrapStreams
.begin() + nFileID
);
365 return NPERR_GENERIC_ERROR
;
367 aRet
= GetNPError( pMes
);
372 void* UnxPluginComm::NPP_GetJavaClass()
377 NPError
UnxPluginComm::NPP_Initialize()
379 MediatorMessage
* pMes
=
380 Transact( eNPP_Initialize
,
383 return NPERR_GENERIC_ERROR
;
385 NPError aRet
= GetNPError( pMes
);
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,
396 saved
? (char*)saved
->buf
: NULL
,
397 saved
? saved
->len
: 0 ) );
399 char *pArgnBuf
, *pArgvBuf
;
400 size_t nArgnLen
= 0, nArgvLen
= 0;
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
;
423 pluginType
, strlen( pluginType
),
424 &mode
, sizeof( mode
),
425 &argc
, sizeof( argc
),
428 saved
->buf
, static_cast<size_t>(saved
->len
),
433 pluginType
, strlen( pluginType
),
434 &mode
, sizeof( mode
),
435 &argc
, sizeof( argc
),
443 return NPERR_GENERIC_ERROR
;
445 NPError aRet
= GetNPError( pMes
);
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
,
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
),
469 return NPERR_GENERIC_ERROR
;
471 aRet
= GetNPError( pMes
);
472 uint16
* pSType
= (uint16
*)pMes
->GetBytes();
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
,
492 window
, sizeof( NPWindow
),
495 return NPERR_GENERIC_ERROR
;
497 aRet
= GetNPError( pMes
);
502 void UnxPluginComm::NPP_Shutdown()
504 Send( eNPP_Shutdown
, NULL
);
507 void UnxPluginComm::NPP_StreamAsFile( NPP instance
, NPStream
* stream
, const char* fname
)
510 UINT32 nFileID
= GetStreamID( stream
);
511 if( nFileID
== PluginConnector::UnknownStreamID
)
514 Send( eNPP_StreamAsFile
,
516 &nFileID
, sizeof( nFileID
),
517 fname
, strlen( fname
),
521 void UnxPluginComm::NPP_URLNotify( NPP instance
, const char* url
, NPReason reason
, void* notifyData
)
525 Send( eNPP_URLNotify
,
528 &reason
, sizeof( reason
),
529 ¬ifyData
, sizeof( void* ),
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
)
540 MediatorMessage
* pMes
=
541 Transact( eNPP_Write
,
543 &nFileID
, sizeof( nFileID
),
544 &offset
, sizeof( offset
),
545 buffer
, static_cast<size_t>(len
),
550 int32 aRet
= pMes
->GetUINT32();
556 int32
UnxPluginComm::NPP_WriteReady( NPP instance
, NPStream
* stream
)
558 GET_INSTANCE_RET( -1 );
559 UINT32 nFileID
= GetStreamID( stream
);
560 if( nFileID
== PluginConnector::UnknownStreamID
)
563 MediatorMessage
* pMes
=
564 Transact( eNPP_WriteReady
,
566 &nFileID
, sizeof( nFileID
),
572 int32 aRet
= pMes
->GetUINT32();
578 char* UnxPluginComm::NPP_GetMIMEDescription()
580 static char* pDesc
= NULL
;
581 MediatorMessage
* pMes
=
582 Transact( eNPP_GetMIMEDescription
,
589 pDesc
= pMes
->GetString();
594 NPError
UnxPluginComm::NPP_GetValue( NPP
/*instance*/, NPPVariable
/*variable*/, void* /*value*/ )
599 NPError
UnxPluginComm::NPP_SetValue( NPP
/*instance*/, NPNVariable
/*variable*/, void* /*value*/ )