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"
31 #if STLPORT_VERSION>=321
35 #include <plugin/impl.hxx>
36 #include <vcl/svapp.hxx>
38 std::vector
<PluginConnector
*> PluginConnector::allConnectors
;
40 PluginConnector::PluginConnector( int nSocket
) :
43 allConnectors
.push_back( this );
44 SetNewMessageHdl( LINK( this, PluginConnector
, NewMessageHdl
) );
47 PluginConnector::~PluginConnector()
49 vos::OGuard
aGuard( m_aUserEventMutex
);
50 for( std::vector
< PluginConnector
* >::iterator it
= allConnectors
.begin();
51 it
!= allConnectors
.end(); ++it
)
55 allConnectors
.erase( it
);
61 IMPL_LINK( PluginConnector
, NewMessageHdl
, Mediator
*, /*pMediator*/ )
63 vos::OGuard
aGuard( m_aUserEventMutex
);
65 for( std::vector
< PluginConnector
* >::iterator it
= allConnectors
.begin();
66 it
!= allConnectors
.end() && bFound
== false; ++it
)
73 Application::PostUserEvent( LINK( this, PluginConnector
, WorkOnNewMessageHdl
) );
77 IMPL_LINK( PluginConnector
, WorkOnNewMessageHdl
, Mediator
*, /*pMediator*/ )
80 for( std::vector
< PluginConnector
* >::iterator it
= allConnectors
.begin();
81 it
!= allConnectors
.end() && bFound
== false; ++it
)
90 vos::OGuard aGuard( m_aUserEventMutex );
91 m_aUserEventIDs.pop_front();
95 MediatorMessage
* pMessage
;
96 CommandAtoms nCommand
;
97 while( (pMessage
= GetNextMessage( sal_False
)) )
99 nCommand
= (CommandAtoms
)pMessage
->GetUINT32();
100 medDebug( 1, "%s\n", GetCommandName( nCommand
) );
105 sal_uInt32 nInstance
= pMessage
->GetUINT32();
106 NPP instance
= m_aInstances
[ nInstance
]->instance
;
107 char* pUrl
= pMessage
->GetString();
108 char* pWindow
= pMessage
->GetString();
109 NPError aRet
= NPN_GetURL( instance
, pUrl
, pWindow
);
110 Respond( pMessage
->m_nID
,
111 (char*)(&aRet
), sizeof( NPError
), NULL
);
116 case eNPN_GetURLNotify
:
118 sal_uInt32 nInstance
= pMessage
->GetUINT32();
119 NPP instance
= m_aInstances
[ nInstance
]->instance
;
120 char* pUrl
= pMessage
->GetString();
121 char* pWindow
= pMessage
->GetString();
122 void** pNotifyData
= (void**)pMessage
->GetBytes();
123 NPError aRet
= NPN_GetURLNotify( instance
, pUrl
, pWindow
,
125 Respond( pMessage
->m_nID
,
126 (char*)(&aRet
), sizeof( NPError
), NULL
);
129 delete [] pNotifyData
;
132 case eNPN_DestroyStream
:
134 sal_uInt32 nInstance
= pMessage
->GetUINT32();
135 NPP instance
= m_aInstances
[ nInstance
]->instance
;
136 sal_uInt32 nFileID
= pMessage
->GetUINT32();
137 char* pUrl
= pMessage
->GetString();
138 NPError
* pReason
= (NPError
*)pMessage
->GetBytes();
139 NPError aRet
= NPERR_FILE_NOT_FOUND
;
140 if( nFileID
< static_cast<sal_uInt32
>(m_aNPWrapStreams
.size()) )
142 if( ! strcmp( m_aNPWrapStreams
[ nFileID
]->url
, pUrl
) )
145 NPN_DestroyStream( instance
, m_aNPWrapStreams
[ nFileID
],
147 m_aNPWrapStreams
.erase( m_aNPWrapStreams
.begin() + nFileID
);
150 medDebug( 1, "StreamID %d has incoherent urls %s and %s\n",
151 nFileID
, pUrl
, m_aNPWrapStreams
[ nFileID
]->url
);
154 medDebug( 1, "Nonexistent StreamID %d\n", nFileID
);
156 Respond( pMessage
->m_nID
,
157 (char*)(&aRet
), sizeof( NPError
), NULL
);
165 sal_uInt32 nInstance
= pMessage
->GetUINT32();
166 NPP instance
= m_aInstances
[ nInstance
]->instance
;
167 NPMIMEType pType
= pMessage
->GetString();
168 char* pTarget
= pMessage
->GetString();
170 NPStream
* pStream
= NULL
;
172 NPError aRet
= NPN_NewStream( instance
, pType
, pTarget
, &pStream
);
174 if( aRet
!= NPERR_NO_ERROR
)
176 sal_uInt32 nDummy
= 0;
177 Respond( pMessage
->m_nID
,
178 (char*)&aRet
, sizeof( aRet
),
180 &nDummy
, sizeof(sal_uInt32
),
181 &nDummy
, sizeof(sal_uInt32
),
186 m_aNPWrapStreams
.push_back( pStream
);
188 sal_uLong nLen
= strlen( pStream
->url
);
189 Respond( pMessage
->m_nID
,
190 (char*)&aRet
, sizeof( aRet
),
192 &pStream
->end
, sizeof(sal_uInt32
),
193 &pStream
->lastmodified
, sizeof(sal_uInt32
),
201 case eNPN_PostURLNotify
:
203 sal_uInt32 nInstance
= pMessage
->GetUINT32();
204 NPP instance
= m_aInstances
[ nInstance
]->instance
;
205 char* pUrl
= pMessage
->GetString();
206 char* pTarget
= pMessage
->GetString();
207 sal_uInt32 nLen
= pMessage
->GetUINT32();
208 char* pBuf
= (char*)pMessage
->GetBytes();
209 NPBool
* pFile
= (NPBool
*)pMessage
->GetBytes();
210 void** pNData
= (void**)pMessage
->GetBytes();
212 NPN_PostURLNotify( instance
, pUrl
, pTarget
, nLen
, pBuf
, *pFile
, *pNData
);
213 Respond( pMessage
->m_nID
, (char*)&aRet
, sizeof( aRet
), NULL
);
223 sal_uInt32 nInstance
= pMessage
->GetUINT32();
224 NPP instance
= m_aInstances
[ nInstance
]->instance
;
225 char* pUrl
= pMessage
->GetString();
226 char* pWindow
= pMessage
->GetString();
227 sal_uInt32 nLen
= pMessage
->GetUINT32();
228 char* pBuf
= (char*)pMessage
->GetBytes();
229 NPBool
* pFile
= (NPBool
*)pMessage
->GetBytes();
231 NPN_PostURL( instance
, pUrl
, pWindow
, nLen
, pBuf
, *pFile
);
232 Respond( pMessage
->m_nID
, (char*)&aRet
, sizeof( aRet
), NULL
);
239 case eNPN_RequestRead
:
241 sal_uInt32 nFileID
= pMessage
->GetUINT32();
242 NPStream
* pStream
= m_aNPWrapStreams
[ nFileID
];
243 sal_uInt32 nRanges
= pMessage
->GetUINT32();
244 sal_uInt32
* pArray
= (sal_uInt32
*)pMessage
->GetBytes();
245 // build ranges table
246 NPByteRange
* pFirst
= new NPByteRange
;
247 NPByteRange
* pRun
= pFirst
;
248 for( sal_uInt32 n
= 0; n
< nRanges
; n
++ )
250 pRun
->offset
= pArray
[ 2*n
];
251 pRun
->length
= pArray
[ 2*n
+1 ];
252 pRun
->next
= n
< nRanges
-1 ? new NPByteRange
: NULL
;
255 NPError aRet
= NPN_RequestRead( pStream
, pFirst
);
256 Respond( pMessage
->m_nID
, (char*)&aRet
, sizeof( aRet
), NULL
);
268 sal_uInt32 nInstance
= pMessage
->GetUINT32();
269 NPP instance
= m_aInstances
[ nInstance
]->instance
;
270 char* pString
= pMessage
->GetString();
271 NPN_Status( instance
, pString
);
277 int major
, minor
, net_major
, net_minor
;
278 NPN_Version( &major
, &minor
, &net_major
, &net_minor
);
279 Respond( pMessage
->m_nID
,
280 (char*)&major
, sizeof( int ),
281 &minor
, sizeof( int ),
282 &net_major
, sizeof( int ),
283 &net_minor
, sizeof( int ),
289 sal_uInt32 nInstance
= pMessage
->GetUINT32();
290 NPP instance
= m_aInstances
[ nInstance
]->instance
;
291 sal_uInt32 nFileID
= pMessage
->GetUINT32();
292 NPStream
* pStream
= m_aNPWrapStreams
[ nFileID
];
293 sal_Int32 nLen
= pMessage
->GetUINT32();
294 void* pBuffer
= pMessage
->GetBytes();
295 sal_Int32 nRet
= NPN_Write( instance
, pStream
, nLen
, pBuffer
);
296 Respond( pMessage
->m_nID
,
297 (char*)&nRet
, sizeof( nRet
),
299 delete [] (char*)pBuffer
;
305 sal_uInt32 nInstance
= pMessage
->GetUINT32();
306 NPP instance
= m_aInstances
[ nInstance
]->instance
;
307 const char* pAnswer
= NPN_UserAgent( instance
);
308 Respond( pMessage
->m_nID
,
309 (char*)pAnswer
, strlen( pAnswer
),
314 medDebug( 1, "caught unknown NPN request %d\n", nCommand
);
322 #define GET_INSTANCE() \
323 sal_uInt32 nInstance; \
324 nInstance = GetNPPID( instance );
326 #define GET_INSTANCE_RET( err ) \
328 if( nInstance == PluginConnector::UnknownNPPID ) \
332 #define POST_INSTANCE() (char*)&nInstance, sizeof( nInstance )
334 NPError
UnxPluginComm::NPP_Destroy( NPP instance
, NPSavedData
** save
)
336 NPError aRet
= NPERR_GENERIC_ERROR
;
337 GET_INSTANCE_RET( aRet
);
338 MediatorMessage
* pMes
=
339 Transact( eNPP_Destroy
,
343 return NPERR_GENERIC_ERROR
;
346 pMes
= Transact( eNPP_DestroyPhase2
,
350 return NPERR_GENERIC_ERROR
;
352 aRet
= GetNPError( pMes
);
353 sal_uLong nSaveBytes
;
354 void* pSaveData
= pMes
->GetBytes( nSaveBytes
);
355 if( nSaveBytes
== 4 && *(sal_uInt32
*)pSaveData
== 0 )
359 *save
= new NPSavedData
;
360 (*save
)->len
= nSaveBytes
;
361 (*save
)->buf
= pSaveData
;
368 NPError
UnxPluginComm::NPP_DestroyStream( NPP instance
, NPStream
* stream
, NPError reason
)
370 NPError aRet
= NPERR_GENERIC_ERROR
;
371 GET_INSTANCE_RET( aRet
);
372 sal_uInt32 nFileID
= GetStreamID( stream
);
373 if( nFileID
== PluginConnector::UnknownStreamID
)
374 return NPERR_GENERIC_ERROR
;
376 MediatorMessage
* pMes
=
377 Transact( eNPP_DestroyStream
,
379 &nFileID
, sizeof( nFileID
),
380 &reason
, sizeof( reason
),
382 m_aNPWrapStreams
.erase( m_aNPWrapStreams
.begin() + nFileID
);
384 return NPERR_GENERIC_ERROR
;
386 aRet
= GetNPError( pMes
);
391 void* UnxPluginComm::NPP_GetJavaClass()
396 NPError
UnxPluginComm::NPP_Initialize()
398 MediatorMessage
* pMes
=
399 Transact( eNPP_Initialize
,
402 return NPERR_GENERIC_ERROR
;
404 NPError aRet
= GetNPError( pMes
);
409 NPError
UnxPluginComm::NPP_New( NPMIMEType pluginType
, NPP instance
, uint16 mode
, int16 argc
,
410 char* argn
[], char* argv
[], NPSavedData
*saved
)
412 m_aInstances
.push_back(
413 new ConnectorInstance( instance
, pluginType
, 0,
415 saved
? (char*)saved
->buf
: NULL
,
416 saved
? saved
->len
: 0 ) );
418 char *pArgnBuf
, *pArgvBuf
;
419 size_t nArgnLen
= 0, nArgvLen
= 0;
421 for( i
= 0; i
< argc
; i
++ )
423 nArgnLen
+= strlen( argn
[i
] ) +1;
424 nArgvLen
+= strlen( argv
[i
] ) +1;
426 pArgnBuf
= new char[ nArgnLen
];
427 pArgvBuf
= new char[ nArgvLen
];
428 char* pRunArgn
= pArgnBuf
;
429 char* pRunArgv
= pArgvBuf
;
430 for( i
= 0; i
< argc
; i
++ )
432 strcpy( pRunArgn
, argn
[i
] );
433 strcpy( pRunArgv
, argv
[i
] );
434 pRunArgn
+= strlen( argn
[i
] ) +1;
435 pRunArgv
+= strlen( argv
[i
] ) +1;
438 MediatorMessage
* pMes
;
442 pluginType
, strlen( pluginType
),
443 &mode
, sizeof( mode
),
444 &argc
, sizeof( argc
),
447 saved
->buf
, static_cast<size_t>(saved
->len
),
452 pluginType
, strlen( pluginType
),
453 &mode
, sizeof( mode
),
454 &argc
, sizeof( argc
),
462 return NPERR_GENERIC_ERROR
;
464 NPError aRet
= GetNPError( pMes
);
470 NPError
UnxPluginComm::NPP_NewStream( NPP instance
, NPMIMEType type
, NPStream
* stream
,
471 NPBool seekable
, uint16
* stype
)
473 NPError aRet
= NPERR_GENERIC_ERROR
;
474 GET_INSTANCE_RET( aRet
);
476 m_aNPWrapStreams
.push_back( stream
);
477 MediatorMessage
* pMes
=
478 Transact( eNPP_NewStream
,
480 type
, strlen( type
),
481 stream
->url
, strlen( stream
->url
),
482 &stream
->end
, sizeof( stream
->end
),
483 &stream
->lastmodified
, sizeof( stream
->lastmodified
),
484 &seekable
, sizeof( seekable
),
488 return NPERR_GENERIC_ERROR
;
490 aRet
= GetNPError( pMes
);
491 uint16
* pSType
= (uint16
*)pMes
->GetBytes();
499 void UnxPluginComm::NPP_Print( NPP
/*instance*/, NPPrint
* /*platformPrint*/ )
503 NPError
UnxPluginComm::NPP_SetWindow( NPP instance
, NPWindow
* window
)
505 NPError aRet
= NPERR_GENERIC_ERROR
;
506 GET_INSTANCE_RET( aRet
);
508 MediatorMessage
* pMes
=
509 Transact( eNPP_SetWindow
,
511 window
, sizeof( NPWindow
),
514 return NPERR_GENERIC_ERROR
;
516 aRet
= GetNPError( pMes
);
521 void UnxPluginComm::NPP_Shutdown()
523 Send( eNPP_Shutdown
, NULL
);
526 void UnxPluginComm::NPP_StreamAsFile( NPP instance
, NPStream
* stream
, const char* fname
)
529 sal_uInt32 nFileID
= GetStreamID( stream
);
530 if( nFileID
== PluginConnector::UnknownStreamID
)
533 Send( eNPP_StreamAsFile
,
535 &nFileID
, sizeof( nFileID
),
536 fname
, strlen( fname
),
540 void UnxPluginComm::NPP_URLNotify( NPP instance
, const char* url
, NPReason reason
, void* notifyData
)
544 Send( eNPP_URLNotify
,
547 &reason
, sizeof( reason
),
548 ¬ifyData
, sizeof( void* ),
552 int32
UnxPluginComm::NPP_Write( NPP instance
, NPStream
* stream
, int32 offset
, int32 len
, void* buffer
)
554 GET_INSTANCE_RET( -1 );
555 sal_uInt32 nFileID
= GetStreamID( stream
);
556 if( nFileID
== PluginConnector::UnknownStreamID
)
559 MediatorMessage
* pMes
=
560 Transact( eNPP_Write
,
562 &nFileID
, sizeof( nFileID
),
563 &offset
, sizeof( offset
),
564 buffer
, static_cast<size_t>(len
),
569 int32 aRet
= pMes
->GetUINT32();
575 int32
UnxPluginComm::NPP_WriteReady( NPP instance
, NPStream
* stream
)
577 GET_INSTANCE_RET( -1 );
578 sal_uInt32 nFileID
= GetStreamID( stream
);
579 if( nFileID
== PluginConnector::UnknownStreamID
)
582 MediatorMessage
* pMes
=
583 Transact( eNPP_WriteReady
,
585 &nFileID
, sizeof( nFileID
),
591 int32 aRet
= pMes
->GetUINT32();
597 char* UnxPluginComm::NPP_GetMIMEDescription()
599 static char* pDesc
= NULL
;
600 MediatorMessage
* pMes
=
601 Transact( eNPP_GetMIMEDescription
,
608 pDesc
= pMes
->GetString();
613 NPError
UnxPluginComm::NPP_GetValue( NPP
/*instance*/, NPPVariable
/*variable*/, void* /*value*/ )
618 NPError
UnxPluginComm::NPP_SetValue( NPP
/*instance*/, NPNVariable
/*variable*/, void* /*value*/ )