Update ooo320-m1
[ooovba.git] / extensions / source / plugin / win / sysplug.cxx
blob5ae81decb3c220a0893b3240e527a549f92bddad
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: sysplug.cxx,v $
10 * $Revision: 1.9 $
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"
34 #include <plugin/impl.hxx>
36 #pragma warning (push,1)
37 #pragma warning (disable:4005)
39 #include <tools/prewin.h>
41 #include <windows.h>
42 #include <string.h>
43 #include <tchar.h>
44 #include <winreg.h>
45 #include <winbase.h>
46 #include <objbase.h>
48 #include <tools/postwin.h>
50 #pragma warning (pop)
52 #include <list>
53 #include <map>
54 #include <algorithm>
57 extern NPNetscapeFuncs aNPNFuncs;
59 #include <tools/debug.hxx>
61 using namespace rtl;
63 #if OSL_DEBUG_LEVEL > 1
64 void TRACE( char const * s );
65 void TRACEN( char const * s, long n );
66 #else
67 #define TRACE(x)
68 #define TRACEN(x,n)
69 #endif
72 //--------------------------------------------------------------------------------------------------
73 PluginComm_Impl::PluginComm_Impl( const OUString& /*rMIME*/, const OUString& rName, HWND /*hWnd*/ )
74 : PluginComm( OUStringToOString( rName, RTL_TEXTENCODING_MS_1252 ) )
76 // initialize plugin function table
77 memset( &_NPPfuncs, 0, sizeof( _NPPfuncs ) );
78 #ifdef UNICODE
79 _plDLL = ::LoadLibrary( rName.getStr() );
80 #else
81 OString aStr( OUStringToOString( rName, RTL_TEXTENCODING_MS_1252 ) );
82 _plDLL = ::LoadLibrary( aStr.getStr() );
83 #endif
84 DBG_ASSERT( _plDLL, "### loading plugin dll failed!" );
86 NPError nErr = NPERR_NO_ERROR;
87 NPError (WINAPI * pEntry)( NPPluginFuncs* );
88 retrieveFunction( _T("NP_GetEntryPoints"), (void**)&pEntry );
90 _NPPfuncs.size = sizeof( _NPPfuncs );
91 _NPPfuncs.version = 0;
92 nErr = (*pEntry)( &_NPPfuncs );
94 DBG_ASSERT( nErr == NPERR_NO_ERROR, "### NP_GetEntryPoints() failed!" );
95 DBG_ASSERT( (_NPPfuncs.version >> 8) >= NP_VERSION_MAJOR,
96 "### version failure!" );
98 m_eCall = eNP_Initialize;
99 execute();
102 //--------------------------------------------------------------------------------------------------
103 PluginComm_Impl::~PluginComm_Impl()
105 if (_plDLL)
107 // NPP_Shutdown();
109 NPError (WINAPI * pShutdown)();
110 if (retrieveFunction( _T("NP_Shutdown"), (void**)&pShutdown ))
112 NPError nErr = (*pShutdown)(); (void)nErr;
113 DBG_ASSERT( nErr == NPERR_NO_ERROR, "### NP_Shutdown() failed!" );
116 BOOL bRet = (BOOL)::FreeLibrary( _plDLL ); (void)bRet;
117 DBG_ASSERT( bRet, "### unloading plugin dll failed!" );
118 _plDLL = NULL;
122 //--------------------------------------------------------------------------------------------------
123 BOOL PluginComm_Impl::retrieveFunction( TCHAR* pName, void** ppFunc ) const
125 if( ! _plDLL )
126 return FALSE;
128 *ppFunc = (void*)::GetProcAddress( _plDLL, pName );
130 return (*ppFunc != NULL);
133 //--------------------------------------------------------------------------------------------------
135 long PluginComm_Impl::doIt()
137 long nRet = 0;
138 switch( m_eCall )
140 case eNP_Initialize:
142 TRACE( "eNP_Initialize" );
143 NPError (WINAPI * pInit)( NPNetscapeFuncs* );
144 if ((_NPPfuncs.version >> 8) >= NP_VERSION_MAJOR &&
145 (retrieveFunction( _T("NP_Initialize"), (void**)&pInit ) ||
146 retrieveFunction( _T("NP_PluginInit"), (void**)&pInit )))
148 nRet = (*pInit)( &aNPNFuncs );
150 else
152 nRet = NPERR_GENERIC_ERROR;
154 DBG_ASSERT( nRet == NPERR_NO_ERROR, "### NP_Initialize() failed!" );
156 break;
157 case eNPP_Destroy:
158 TRACE( "eNPP_Destroy" );
159 nRet = (_NPPfuncs.destroy
160 ? (*_NPPfuncs.destroy)(
161 (NPP)m_aArgs[0],
162 (NPSavedData**)m_aArgs[1] )
163 : NPERR_GENERIC_ERROR);
164 break;
165 case eNPP_DestroyStream:
166 TRACE( "eNPP_DestroyStream" );
167 nRet = (_NPPfuncs.destroystream
168 ? (*_NPPfuncs.destroystream)(
169 (NPP)m_aArgs[0],
170 (NPStream*)m_aArgs[1],
171 (NPError)(sal_IntPtr)m_aArgs[2] )
172 : NPERR_GENERIC_ERROR);
173 break;
174 case eNPP_New:
175 TRACE( "eNPP_New" );
176 nRet = (_NPPfuncs.newp
177 ? (*_NPPfuncs.newp)(
178 (NPMIMEType)m_aArgs[0],
179 (NPP)m_aArgs[1],
180 (uint16)(sal_IntPtr)m_aArgs[2],
181 (int16)(sal_IntPtr)m_aArgs[3],
182 (char**)m_aArgs[4],
183 (char**)m_aArgs[5],
184 (NPSavedData*)m_aArgs[6] )
185 : NPERR_GENERIC_ERROR);
186 break;
187 case eNPP_NewStream:
188 TRACE( "eNPP_NewStream" );
189 nRet = (_NPPfuncs.newstream
190 ? (*_NPPfuncs.newstream)(
191 (NPP)m_aArgs[0],
192 (NPMIMEType)m_aArgs[1],
193 (NPStream*)m_aArgs[2],
194 (NPBool)(sal_IntPtr)m_aArgs[3],
195 (uint16*)m_aArgs[4] )
196 : NPERR_GENERIC_ERROR);
197 break;
198 case eNPP_Print:
199 TRACE( "eNPP_Print" );
200 if (_NPPfuncs.print)
201 (*_NPPfuncs.print)(
202 (NPP)m_aArgs[0],
203 (NPPrint*)m_aArgs[1] );
204 break;
205 case eNPP_SetWindow:
207 TRACE( "eNPP_SetWindow" );
208 nRet = (_NPPfuncs.setwindow
209 ? (*_NPPfuncs.setwindow)(
210 (NPP)m_aArgs[0],
211 (NPWindow*)m_aArgs[1] )
212 : NPERR_GENERIC_ERROR);
213 break;
215 case eNPP_StreamAsFile:
216 TRACE( "eNPP_StreamAsFile" );
217 if (_NPPfuncs.asfile)
218 (*_NPPfuncs.asfile)(
219 (NPP)m_aArgs[0],
220 (NPStream*)m_aArgs[1],
221 (char*)m_aArgs[2] );
222 break;
223 case eNPP_URLNotify:
224 TRACE( "eNPP_URLNotify" );
225 if (_NPPfuncs.urlnotify)
226 (*_NPPfuncs.urlnotify)(
227 (NPP)m_aArgs[0],
228 (char*)m_aArgs[1],
229 (NPReason)(sal_IntPtr)m_aArgs[2],
230 m_aArgs[3] );
231 break;
232 case eNPP_Write:
233 TRACEN( "eNPP_Write n=", (int32)m_aArgs[3] );
234 nRet = (_NPPfuncs.write
235 ? (*_NPPfuncs.write)(
236 (NPP)m_aArgs[0],
237 (NPStream*)m_aArgs[1],
238 (int32)m_aArgs[2],
239 (int32)m_aArgs[3],
240 m_aArgs[4] )
241 : 0);
242 break;
243 case eNPP_WriteReady:
244 TRACE( "eNPP_WriteReady" );
245 nRet = (_NPPfuncs.writeready
246 ? (*_NPPfuncs.writeready)(
247 (NPP)m_aArgs[0],
248 (NPStream*)m_aArgs[1] )
249 : 0);
250 break;
251 case eNPP_GetValue:
252 TRACE( "eNPP_GetValue" );
253 nRet = (_NPPfuncs.getvalue
254 ? (*_NPPfuncs.getvalue)(
255 (NPP)m_aArgs[0],
256 (NPPVariable)(int)m_aArgs[1],
257 m_aArgs[2] )
258 : NPERR_GENERIC_ERROR);
259 break;
260 case eNPP_SetValue:
261 TRACE( "eNPP_SetValue" );
262 nRet = (_NPPfuncs.setvalue
263 ? (*_NPPfuncs.setvalue)(
264 (NPP)m_aArgs[0],
265 (NPNVariable)(int)m_aArgs[1],
266 m_aArgs[2] )
267 : NPERR_GENERIC_ERROR);
268 break;
269 case eNPP_Shutdown:
271 TRACE( "eNPP_Shutdown" );
272 NPP_ShutdownUPP pFunc;
273 if (retrieveFunction( _T("NPP_Shutdown"), (void**)&pFunc ))
274 (*pFunc)();
276 break;
277 case eNPP_Initialize:
278 TRACE( "eNPP_Initialize" );
279 OSL_ENSURE( false, "NPP_Initialize: not implemented!" );
280 break;
281 case eNPP_GetJavaClass:
282 TRACE( "eNPP_GetJavaClass" );
283 OSL_ENSURE( false, "NPP_GetJavaClass: not implemented!" );
284 break;
286 return nRet;
289 //--------------------------------------------------------------------------------------------------
290 NPError PluginComm_Impl::NPP_Destroy( NPP instance, NPSavedData** save )
292 DBG_ASSERT( _NPPfuncs.destroy, "### NPP_Destroy(): null pointer in NPP functions table!" );
293 m_eCall = eNPP_Destroy;
294 m_aArgs[0] = (void*)instance;
295 m_aArgs[1] = (void*)save;
296 return (NPError)execute();
299 //--------------------------------------------------------------------------------------------------
300 NPError PluginComm_Impl::NPP_DestroyStream( NPP instance, NPStream* stream, NPError reason )
302 DBG_ASSERT( _NPPfuncs.destroystream, "### NPP_DestroyStream(): null pointer in NPP functions table!" );
303 m_eCall = eNPP_DestroyStream;
304 m_aArgs[0] = (void*)instance;
305 m_aArgs[1] = (void*)stream;
306 m_aArgs[2] = (void*)reason;
307 return (NPError)execute();
310 //--------------------------------------------------------------------------------------------------
311 NPError PluginComm_Impl::NPP_New( NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc,
312 char* argn[], char* argv[], NPSavedData *saved )
314 DBG_ASSERT( _NPPfuncs.newp, "### NPP_New(): null pointer in NPP functions table!" );
315 m_eCall = eNPP_New;
316 m_aArgs[0] = (void*)pluginType;
317 m_aArgs[1] = (void*)instance;
318 m_aArgs[2] = (void*)mode;
319 m_aArgs[3] = (void*)argc;
320 m_aArgs[4] = (void*)argn;
321 m_aArgs[5] = (void*)argv;
322 m_aArgs[6] = (void*)saved;
323 return (NPError)execute();
326 //--------------------------------------------------------------------------------------------------
327 NPError PluginComm_Impl::NPP_NewStream( NPP instance, NPMIMEType type, NPStream* stream,
328 NPBool seekable, uint16* stype )
330 DBG_ASSERT( _NPPfuncs.newstream, "### NPP_NewStream(): null pointer in NPP functions table!" );
331 m_eCall = eNPP_NewStream;
332 m_aArgs[0] = (void*)instance;
333 m_aArgs[1] = (void*)type;
334 m_aArgs[2] = (void*)stream;
335 m_aArgs[3] = (void*)seekable;
336 m_aArgs[4] = (void*)stype;
337 return (NPError)execute();
340 //--------------------------------------------------------------------------------------------------
341 void PluginComm_Impl::NPP_Print( NPP instance, NPPrint* platformPrint )
343 DBG_ASSERT( _NPPfuncs.print, "### NPP_Print(): null pointer in NPP functions table!" );
344 m_eCall = eNPP_Print;
345 m_aArgs[0] = (void*)instance;
346 m_aArgs[1] = (void*)platformPrint;
347 execute();
350 //--------------------------------------------------------------------------------------------------
351 NPError PluginComm_Impl::NPP_SetWindow( NPP instance, NPWindow* window )
353 DBG_ASSERT( _NPPfuncs.setwindow, "### NPP_SetWindow(): null pointer in NPP functions table!" );
354 m_eCall = eNPP_SetWindow;
355 m_aArgs[0] = (void*)instance;
356 m_aArgs[1] = (void*)window;
357 return (NPError)execute();
360 //--------------------------------------------------------------------------------------------------
361 void PluginComm_Impl::NPP_StreamAsFile( NPP instance, NPStream* stream, const char* fname )
363 DBG_ASSERT( _NPPfuncs.asfile, "### NPP_StreamAsFile(): null pointer in NPP functions table!" );
364 m_eCall = eNPP_StreamAsFile;
365 m_aArgs[0] = (void*)instance;
366 m_aArgs[1] = (void*)stream;
367 m_aArgs[2] = (void*)fname;
368 execute();
371 //--------------------------------------------------------------------------------------------------
372 void PluginComm_Impl::NPP_URLNotify( NPP instance, const char* url, NPReason reason, void* notifyData )
374 DBG_ASSERT( _NPPfuncs.urlnotify, "### NPP_URLNotify(): null pointer in NPP functions table!" );
375 m_eCall = eNPP_URLNotify;
376 m_aArgs[0] = (void*)instance;
377 m_aArgs[1] = (void*)url;
378 m_aArgs[2] = (void*)reason;
379 m_aArgs[3] = notifyData;
380 execute();
383 //--------------------------------------------------------------------------------------------------
384 int32 PluginComm_Impl::NPP_Write( NPP instance, NPStream* stream, int32 offset, int32 len, void* buffer )
386 DBG_ASSERT( _NPPfuncs.write, "### NPP_Write(): null pointer in NPP functions table!" );
387 m_eCall = eNPP_Write;
388 m_aArgs[0] = (void*)instance;
389 m_aArgs[1] = (void*)stream;
390 m_aArgs[2] = (void*)offset;
391 m_aArgs[3] = (void*)len;
392 m_aArgs[4] = buffer;
393 return (NPError)execute();
396 //--------------------------------------------------------------------------------------------------
397 int32 PluginComm_Impl::NPP_WriteReady( NPP instance, NPStream* stream )
399 DBG_ASSERT( _NPPfuncs.writeready, "### NPP_WriteReady(): null pointer in NPP functions table!" );
400 m_eCall = eNPP_WriteReady;
401 m_aArgs[0] = (void*)instance;
402 m_aArgs[1] = (void*)stream;
403 return execute();
406 //--------------------------------------------------------------------------------------------------
407 NPError PluginComm_Impl::NPP_GetValue( NPP instance, NPPVariable variable, void *ret_value )
409 DBG_ASSERT( _NPPfuncs.getvalue, "### NPP_GetValue(): null pointer in NPP functions table!" );
410 m_eCall = eNPP_GetValue;
411 m_aArgs[0] = (void*)instance;
412 m_aArgs[1] = (void*)variable;
413 m_aArgs[2] = ret_value;
414 return (NPError)execute();
417 //--------------------------------------------------------------------------------------------------
418 NPError PluginComm_Impl::NPP_SetValue( NPP instance, NPNVariable variable, void *set_value )
420 DBG_ASSERT( _NPPfuncs.setvalue, "### NPP_SetValue(): null pointer in NPP functions table!" );
421 m_eCall = eNPP_SetValue;
422 m_aArgs[0] = (void*)instance;
423 m_aArgs[1] = (void*)variable;
424 m_aArgs[2] = set_value;
425 return (NPError)execute();
428 //--------------------------------------------------------------------------------------------------
429 void * PluginComm_Impl::NPP_GetJavaClass()
431 DBG_ERROR( "no java class available!" );
432 return 0;
435 //--------------------------------------------------------------------------------------------------
436 NPError PluginComm_Impl::NPP_Initialize()
438 return NPERR_NO_ERROR;
441 //--------------------------------------------------------------------------------------------------
442 void PluginComm_Impl::NPP_Shutdown()
444 m_eCall = eNPP_Shutdown;
445 execute();