bump product version to 5.0.4.1
[LibreOffice.git] / extensions / source / plugin / win / sysplug.cxx
blobd34c8001229550c33ca29ec29e61780d383186c4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <prewin.h>
31 #include <postwin.h>
33 #include <sal/log.hxx>
35 #include <plugin/impl.hxx>
37 #if defined _MSC_VER
38 #pragma warning (push,1)
39 #pragma warning (disable:4005)
40 #endif
42 #include <string.h>
43 #include <tchar.h>
44 #include <objbase.h>
46 #if defined _MSC_VER
47 #pragma warning (pop)
48 #endif
50 #include <list>
51 #include <map>
52 #include <algorithm>
55 extern NPNetscapeFuncs aNPNFuncs;
57 #include <tools/debug.hxx>
60 #if OSL_DEBUG_LEVEL > 1
61 void TRACE( char const * s );
62 void TRACEN( char const * s, long n );
63 #else
64 #define TRACE(x)
65 #define TRACEN(x,n)
66 #endif
68 ::boost::shared_ptr<SysPlugData> CreateSysPlugData()
70 return ::boost::shared_ptr<SysPlugData>();
74 PluginComm_Impl::PluginComm_Impl( const OUString& /*rMIME*/, const OUString& rName, HWND /*hWnd*/ )
75 : PluginComm( OUStringToOString( rName, RTL_TEXTENCODING_MS_1252 ) )
77 // initialize plugin function table
78 memset( &_NPPfuncs, 0, sizeof( _NPPfuncs ) );
79 #ifdef UNICODE
80 _plDLL = ::LoadLibrary( rName.getStr() );
81 #else
82 OString aStr( OUStringToOString( rName, RTL_TEXTENCODING_MS_1252 ) );
83 _plDLL = ::LoadLibrary( aStr.getStr() );
84 #endif
85 DBG_ASSERT( _plDLL, "### loading plugin dll failed!" );
87 NPError (WINAPI * pEntry)( NPPluginFuncs* );
88 retrieveFunction( _T("NP_GetEntryPoints"), (void**)&pEntry );
90 _NPPfuncs.size = sizeof( _NPPfuncs );
91 _NPPfuncs.version = 0;
92 NPError nErr = (*pEntry)( &_NPPfuncs );
94 SAL_WARN_IF(
95 nErr != NPERR_NO_ERROR, "extensions.plugin",
96 "NP_GetEntryPoints() failed");
97 DBG_ASSERT( (_NPPfuncs.version >> 8) >= NP_VERSION_MAJOR,
98 "### version failure!" );
100 m_eCall = eNP_Initialize;
101 execute();
105 PluginComm_Impl::~PluginComm_Impl()
107 if (_plDLL)
109 // NPP_Shutdown();
111 NPError (WINAPI * pShutdown)();
112 if (retrieveFunction( _T("NP_Shutdown"), (void**)&pShutdown ))
114 NPError nErr = (*pShutdown)(); (void)nErr;
115 DBG_ASSERT( nErr == NPERR_NO_ERROR, "### NP_Shutdown() failed!" );
118 BOOL bRet = (BOOL)::FreeLibrary( _plDLL ); (void)bRet;
119 DBG_ASSERT( bRet, "### unloading plugin dll failed!" );
120 _plDLL = NULL;
125 BOOL PluginComm_Impl::retrieveFunction( TCHAR const * pName, void** ppFunc ) const
127 if( ! _plDLL )
128 return FALSE;
130 *ppFunc = (void*)::GetProcAddress( _plDLL, pName );
132 return (*ppFunc != NULL);
137 long PluginComm_Impl::doIt()
139 long nRet = 0;
140 switch( m_eCall )
142 case eNP_Initialize:
144 TRACE( "eNP_Initialize" );
145 NPError (WINAPI * pInit)( NPNetscapeFuncs* );
146 if ((_NPPfuncs.version >> 8) >= NP_VERSION_MAJOR &&
147 (retrieveFunction( _T("NP_Initialize"), (void**)&pInit ) ||
148 retrieveFunction( _T("NP_PluginInit"), (void**)&pInit )))
150 nRet = (*pInit)( &aNPNFuncs );
152 else
154 nRet = NPERR_GENERIC_ERROR;
156 DBG_ASSERT( nRet == NPERR_NO_ERROR, "### NP_Initialize() failed!" );
158 break;
159 case eNPP_Destroy:
160 TRACE( "eNPP_Destroy" );
161 nRet = (_NPPfuncs.destroy
162 ? (*_NPPfuncs.destroy)(
163 (NPP)m_aArgs[0],
164 (NPSavedData**)m_aArgs[1] )
165 : NPERR_GENERIC_ERROR);
166 break;
167 case eNPP_DestroyStream:
168 TRACE( "eNPP_DestroyStream" );
169 nRet = (_NPPfuncs.destroystream
170 ? (*_NPPfuncs.destroystream)(
171 (NPP)m_aArgs[0],
172 (NPStream*)m_aArgs[1],
173 (NPError)(sal_IntPtr)m_aArgs[2] )
174 : NPERR_GENERIC_ERROR);
175 break;
176 case eNPP_New:
177 TRACE( "eNPP_New" );
178 nRet = (_NPPfuncs.newp
179 ? (*_NPPfuncs.newp)(
180 (NPMIMEType)m_aArgs[0],
181 (NPP)m_aArgs[1],
182 (uint16_t)(sal_IntPtr)m_aArgs[2],
183 (int16_t)(sal_IntPtr)m_aArgs[3],
184 (char**)m_aArgs[4],
185 (char**)m_aArgs[5],
186 (NPSavedData*)m_aArgs[6] )
187 : NPERR_GENERIC_ERROR);
188 break;
189 case eNPP_NewStream:
190 TRACE( "eNPP_NewStream" );
191 nRet = (_NPPfuncs.newstream
192 ? (*_NPPfuncs.newstream)(
193 (NPP)m_aArgs[0],
194 (NPMIMEType)m_aArgs[1],
195 (NPStream*)m_aArgs[2],
196 (NPBool)(sal_IntPtr)m_aArgs[3],
197 (uint16_t*)m_aArgs[4] )
198 : NPERR_GENERIC_ERROR);
199 break;
200 case eNPP_Print:
201 TRACE( "eNPP_Print" );
202 if (_NPPfuncs.print)
203 (*_NPPfuncs.print)(
204 (NPP)m_aArgs[0],
205 (NPPrint*)m_aArgs[1] );
206 break;
207 case eNPP_SetWindow:
209 TRACE( "eNPP_SetWindow" );
210 nRet = (_NPPfuncs.setwindow
211 ? (*_NPPfuncs.setwindow)(
212 (NPP)m_aArgs[0],
213 (NPWindow*)m_aArgs[1] )
214 : NPERR_GENERIC_ERROR);
215 break;
217 case eNPP_StreamAsFile:
218 TRACE( "eNPP_StreamAsFile" );
219 if (_NPPfuncs.asfile)
220 (*_NPPfuncs.asfile)(
221 (NPP)m_aArgs[0],
222 (NPStream*)m_aArgs[1],
223 (char*)m_aArgs[2] );
224 break;
225 case eNPP_URLNotify:
226 TRACE( "eNPP_URLNotify" );
227 if (_NPPfuncs.urlnotify)
228 (*_NPPfuncs.urlnotify)(
229 (NPP)m_aArgs[0],
230 (char*)m_aArgs[1],
231 (NPReason)(sal_IntPtr)m_aArgs[2],
232 m_aArgs[3] );
233 break;
234 case eNPP_Write:
235 TRACEN( "eNPP_Write n=", (int32_t)m_aArgs[3] );
236 nRet = (_NPPfuncs.write
237 ? (*_NPPfuncs.write)(
238 (NPP)m_aArgs[0],
239 (NPStream*)m_aArgs[1],
240 (sal_IntPtr)m_aArgs[2],
241 (sal_IntPtr)m_aArgs[3],
242 m_aArgs[4] )
243 : 0);
244 break;
245 case eNPP_WriteReady:
246 TRACE( "eNPP_WriteReady" );
247 nRet = (_NPPfuncs.writeready
248 ? (*_NPPfuncs.writeready)(
249 (NPP)m_aArgs[0],
250 (NPStream*)m_aArgs[1] )
251 : 0);
252 break;
253 case eNPP_GetValue:
254 TRACE( "eNPP_GetValue" );
255 nRet = (_NPPfuncs.getvalue
256 ? (*_NPPfuncs.getvalue)(
257 (NPP)m_aArgs[0],
258 (NPPVariable)(sal_IntPtr)m_aArgs[1],
259 m_aArgs[2] )
260 : NPERR_GENERIC_ERROR);
261 break;
262 case eNPP_SetValue:
263 TRACE( "eNPP_SetValue" );
264 nRet = (_NPPfuncs.setvalue
265 ? (*_NPPfuncs.setvalue)(
266 (NPP)m_aArgs[0],
267 (NPNVariable)(sal_IntPtr)m_aArgs[1],
268 m_aArgs[2] )
269 : NPERR_GENERIC_ERROR);
270 break;
271 case eNPP_Shutdown:
273 TRACE( "eNPP_Shutdown" );
274 NPP_ShutdownUPP pFunc;
275 if (retrieveFunction( _T("NPP_Shutdown"), (void**)&pFunc ))
276 (*pFunc)();
278 break;
279 case eNPP_Initialize:
280 TRACE( "eNPP_Initialize" );
281 OSL_FAIL( "NPP_Initialize: not implemented!" );
282 break;
283 case eNPP_GetJavaClass:
284 TRACE( "eNPP_GetJavaClass" );
285 OSL_FAIL( "NPP_GetJavaClass: not implemented!" );
286 break;
288 return nRet;
292 NPError PluginComm_Impl::NPP_Destroy( NPP instance, NPSavedData** save )
294 DBG_ASSERT( _NPPfuncs.destroy, "### NPP_Destroy(): null pointer in NPP functions table!" );
295 m_eCall = eNPP_Destroy;
296 m_aArgs[0] = (void*)instance;
297 m_aArgs[1] = (void*)save;
298 return (NPError)execute();
302 NPError PluginComm_Impl::NPP_DestroyStream( NPP instance, NPStream* stream, NPError reason )
304 DBG_ASSERT( _NPPfuncs.destroystream, "### NPP_DestroyStream(): null pointer in NPP functions table!" );
305 m_eCall = eNPP_DestroyStream;
306 m_aArgs[0] = (void*)instance;
307 m_aArgs[1] = (void*)stream;
308 m_aArgs[2] = reinterpret_cast< void * >(static_cast< sal_IntPtr >(reason));
309 return (NPError)execute();
313 NPError PluginComm_Impl::NPP_New( NPMIMEType pluginType, NPP instance, uint16_t mode, int16_t argc,
314 char* argn[], char* argv[], NPSavedData *saved )
316 DBG_ASSERT( _NPPfuncs.newp, "### NPP_New(): null pointer in NPP functions table!" );
317 m_eCall = eNPP_New;
318 m_aArgs[0] = (void*)pluginType;
319 m_aArgs[1] = (void*)instance;
320 m_aArgs[2] = reinterpret_cast< void * >(static_cast< sal_uIntPtr >(mode));
321 m_aArgs[3] = reinterpret_cast< void * >(static_cast< sal_IntPtr >(argc));
322 m_aArgs[4] = (void*)argn;
323 m_aArgs[5] = (void*)argv;
324 m_aArgs[6] = (void*)saved;
325 return (NPError)execute();
329 NPError PluginComm_Impl::NPP_NewStream( NPP instance, NPMIMEType type, NPStream* stream,
330 NPBool seekable, uint16_t* stype )
332 DBG_ASSERT( _NPPfuncs.newstream, "### NPP_NewStream(): null pointer in NPP functions table!" );
333 m_eCall = eNPP_NewStream;
334 m_aArgs[0] = (void*)instance;
335 m_aArgs[1] = (void*)type;
336 m_aArgs[2] = (void*)stream;
337 m_aArgs[3] = reinterpret_cast< void * >(
338 static_cast< sal_uIntPtr >(seekable));
339 m_aArgs[4] = (void*)stype;
340 return (NPError)execute();
344 void PluginComm_Impl::NPP_Print( NPP instance, NPPrint* platformPrint )
346 DBG_ASSERT( _NPPfuncs.print, "### NPP_Print(): null pointer in NPP functions table!" );
347 m_eCall = eNPP_Print;
348 m_aArgs[0] = (void*)instance;
349 m_aArgs[1] = (void*)platformPrint;
350 execute();
354 NPError PluginComm_Impl::NPP_SetWindow( NPP instance, NPWindow* window )
356 DBG_ASSERT( _NPPfuncs.setwindow, "### NPP_SetWindow(): null pointer in NPP functions table!" );
357 m_eCall = eNPP_SetWindow;
358 m_aArgs[0] = (void*)instance;
359 m_aArgs[1] = (void*)window;
360 return (NPError)execute();
364 void PluginComm_Impl::NPP_StreamAsFile( NPP instance, NPStream* stream, const char* fname )
366 DBG_ASSERT( _NPPfuncs.asfile, "### NPP_StreamAsFile(): null pointer in NPP functions table!" );
367 m_eCall = eNPP_StreamAsFile;
368 m_aArgs[0] = (void*)instance;
369 m_aArgs[1] = (void*)stream;
370 m_aArgs[2] = (void*)fname;
371 execute();
375 void PluginComm_Impl::NPP_URLNotify( NPP instance, const char* url, NPReason reason, void* notifyData )
377 DBG_ASSERT( _NPPfuncs.urlnotify, "### NPP_URLNotify(): null pointer in NPP functions table!" );
378 m_eCall = eNPP_URLNotify;
379 m_aArgs[0] = (void*)instance;
380 m_aArgs[1] = (void*)url;
381 m_aArgs[2] = reinterpret_cast< void * >(static_cast< sal_IntPtr >(reason));
382 m_aArgs[3] = notifyData;
383 execute();
387 int32_t PluginComm_Impl::NPP_Write( NPP instance, NPStream* stream, int32_t offset, int32_t len, void* buffer )
389 DBG_ASSERT( _NPPfuncs.write, "### NPP_Write(): null pointer in NPP functions table!" );
390 m_eCall = eNPP_Write;
391 m_aArgs[0] = (void*)instance;
392 m_aArgs[1] = (void*)stream;
393 m_aArgs[2] = (void*)(sal_IntPtr)offset;
394 m_aArgs[3] = (void*)(sal_IntPtr)len;
395 m_aArgs[4] = buffer;
396 return (NPError)execute();
400 int32_t PluginComm_Impl::NPP_WriteReady( NPP instance, NPStream* stream )
402 DBG_ASSERT( _NPPfuncs.writeready, "### NPP_WriteReady(): null pointer in NPP functions table!" );
403 m_eCall = eNPP_WriteReady;
404 m_aArgs[0] = (void*)instance;
405 m_aArgs[1] = (void*)stream;
406 return execute();
410 NPError PluginComm_Impl::NPP_GetValue( NPP instance, NPPVariable variable, void *ret_value )
412 DBG_ASSERT( _NPPfuncs.getvalue, "### NPP_GetValue(): null pointer in NPP functions table!" );
413 m_eCall = eNPP_GetValue;
414 m_aArgs[0] = (void*)instance;
415 m_aArgs[1] = (void*)variable;
416 m_aArgs[2] = ret_value;
417 return (NPError)execute();
421 NPError PluginComm_Impl::NPP_SetValue( NPP instance, NPNVariable variable, void *set_value )
423 DBG_ASSERT( _NPPfuncs.setvalue, "### NPP_SetValue(): null pointer in NPP functions table!" );
424 m_eCall = eNPP_SetValue;
425 m_aArgs[0] = (void*)instance;
426 m_aArgs[1] = (void*)variable;
427 m_aArgs[2] = set_value;
428 return (NPError)execute();
432 void * PluginComm_Impl::NPP_GetJavaClass()
434 OSL_FAIL( "no java class available!" );
435 return 0;
439 NPError PluginComm_Impl::NPP_Initialize()
441 return NPERR_NO_ERROR;
445 void PluginComm_Impl::NPP_Shutdown()
447 m_eCall = eNPP_Shutdown;
448 execute();
451 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */