1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is mozilla.org code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38#include "xp.h"
39
40#include "epmanager.h"
41#include "plugload.h"
42#include "logger.h"
43
44extern NPNetscapeFuncs NPNFuncs;
45extern Logger * logger;
46
47NPNetscapeFuncs fakeNPNFuncs;
48
49extern NPPEntryPointManager * epManager;
50
51jref NPP_GetJavaClass (void)
52{
53  if(logger)
54    logger->logCall(action_npp_get_java_class);
55
56  if(logger)
57    logger->logReturn(action_npp_get_java_class);
58  return NULL;
59}
60
61NPError NPP_New(NPMIMEType pluginType,
62                NPP instance,
63                uint16 mode,
64                int16 argc,
65                char* argn[],
66                char* argv[],
67                NPSavedData* saved)
68{
69  if(epManager == NULL)
70    return NPERR_GENERIC_ERROR;
71
72  if(instance == NULL)
73    return NPERR_INVALID_INSTANCE_ERROR;
74
75  if(logger)
76    logger->logCall(action_npp_new, (DWORD)pluginType, (DWORD)instance, (DWORD)mode, (DWORD)argc, (DWORD)argn, (DWORD)argv, (DWORD)saved);
77
78/* now action begins */
79
80  if(NULL == epManager->findEntryPointsForPlugin(pluginType))
81  {
82    // if it is first time in, we don't have it yet
83    // scan plugins dir for available plugins to see if we have anything
84    // for the given mimetype
85    XP_HLIB hLib = LoadRealPlugin(pluginType);
86    if(!hLib)
87    {
88      // what do we do if we don't?
89      return NPERR_GENERIC_ERROR;
90    }
91
92    NP_GETENTRYPOINTS real_NP_GetEntryPoints = (NP_GETENTRYPOINTS)XP_GetSymbol(hLib, "NP_GetEntryPoints");
93    if(!real_NP_GetEntryPoints)
94      return NPERR_GENERIC_ERROR;
95
96    NP_INITIALIZE real_NP_Initialize = (NP_INITIALIZE)XP_GetSymbol(hLib, "NP_Initialize");
97    if(!real_NP_Initialize)
98      return NPERR_GENERIC_ERROR;
99
100    NP_SHUTDOWN real_NP_Shutdown = (NP_SHUTDOWN)XP_GetSymbol(hLib, "NP_Shutdown");
101    if(!real_NP_Shutdown)
102      return NPERR_GENERIC_ERROR;
103
104    // fill callbacks structs
105    NPPluginFuncs realNPPFuncs;
106    memset(&realNPPFuncs, 0, sizeof(NPPluginFuncs));
107    realNPPFuncs.size = sizeof(NPPluginFuncs);
108
109    real_NP_GetEntryPoints(&realNPPFuncs);
110
111    if(logger)
112      logger->logSPY_NP_GetEntryPoints(&realNPPFuncs);
113
114    // store the table with the entry point manager
115    epManager->createEntryPointsForPlugin(pluginType, &realNPPFuncs, real_NP_Shutdown, hLib);
116
117    // inform the plugin about our entry point it should call
118    memset((void *)&fakeNPNFuncs, 0, sizeof(fakeNPNFuncs));
119
120    fakeNPNFuncs.size             = sizeof(fakeNPNFuncs);
121    fakeNPNFuncs.version          = NPNFuncs.version;
122    fakeNPNFuncs.geturlnotify     = NPN_GetURLNotify;
123    fakeNPNFuncs.geturl           = NPN_GetURL;
124    fakeNPNFuncs.posturlnotify    = NPN_PostURLNotify;
125    fakeNPNFuncs.posturl          = NPN_PostURL;
126    fakeNPNFuncs.requestread      = NPN_RequestRead;
127    fakeNPNFuncs.newstream        = NPN_NewStream;
128    fakeNPNFuncs.write            = NPN_Write;
129    fakeNPNFuncs.destroystream    = NPN_DestroyStream;
130    fakeNPNFuncs.status           = NPN_Status;
131    fakeNPNFuncs.uagent           = NPN_UserAgent;
132    fakeNPNFuncs.memalloc         = NPN_MemAlloc;
133    fakeNPNFuncs.memfree          = NPN_MemFree;
134    fakeNPNFuncs.memflush         = NPN_MemFlush;
135    fakeNPNFuncs.reloadplugins    = NPN_ReloadPlugins;
136    fakeNPNFuncs.getJavaEnv       = NPN_GetJavaEnv;
137    fakeNPNFuncs.getJavaPeer      = NPN_GetJavaPeer;
138    fakeNPNFuncs.getvalue         = NPN_GetValue;
139    fakeNPNFuncs.setvalue         = NPN_SetValue;
140    fakeNPNFuncs.invalidaterect   = NPN_InvalidateRect;
141    fakeNPNFuncs.invalidateregion = NPN_InvalidateRegion;
142    fakeNPNFuncs.forceredraw      = NPN_ForceRedraw;
143    fakeNPNFuncs.getstringidentifier    = NPN_GetStringIdentifier;
144    fakeNPNFuncs.getstringidentifiers   = NPN_GetStringIdentifiers;
145    fakeNPNFuncs.getintidentifier       = NPN_GetIntIdentifier;
146    fakeNPNFuncs.identifierisstring     = NPN_IdentifierIsString;
147    fakeNPNFuncs.utf8fromidentifier     = NPN_UTF8FromIdentifier;
148    fakeNPNFuncs.intfromidentifier      = NPN_IntFromIdentifier;
149    fakeNPNFuncs.createobject           = NPN_CreateObject;
150    fakeNPNFuncs.retainobject           = NPN_RetainObject;
151    fakeNPNFuncs.releaseobject          = NPN_ReleaseObject;
152    fakeNPNFuncs.invoke                 = NPN_Invoke;
153    fakeNPNFuncs.invokeDefault          = NPN_InvokeDefault;
154    fakeNPNFuncs.evaluate               = NPN_Evaluate;
155    fakeNPNFuncs.getproperty            = NPN_GetProperty;
156    fakeNPNFuncs.setproperty            = NPN_SetProperty;
157    fakeNPNFuncs.removeproperty         = NPN_RemoveProperty;
158    fakeNPNFuncs.hasproperty            = NPN_HasProperty;
159    fakeNPNFuncs.hasmethod              = NPN_HasMethod;
160    fakeNPNFuncs.releasevariantvalue    = NPN_ReleaseVariantValue;
161    fakeNPNFuncs.setexception           = NPN_SetException;
162    fakeNPNFuncs.pushpopupsenabledstate = NPN_PushPopupsEnabledState;
163    fakeNPNFuncs.poppopupsenabledstate  = NPN_PopPopupsEnabledState;
164    fakeNPNFuncs.enumerate              = NPN_Enumerate;
165
166    if(logger)
167      logger->logSPY_NP_Initialize();
168
169    real_NP_Initialize(&fakeNPNFuncs);
170  }
171
172  NPError rv = epManager->callNPP_New(pluginType, instance, mode, argc, argn, argv, saved);
173
174  if(logger)
175    logger->logReturn(action_npp_new);
176
177  return rv;
178}
179
180NPError NPP_Destroy (NPP instance, NPSavedData** save)
181{
182  if(epManager == NULL)
183    return NPERR_GENERIC_ERROR;
184
185  if(instance == NULL)
186    return NPERR_INVALID_INSTANCE_ERROR;
187
188  BOOL last = FALSE;
189
190  if(logger)
191    logger->logCall(action_npp_destroy, (DWORD)instance, (DWORD)save);
192
193  NPError rv = epManager->callNPP_Destroy(instance, save, &last);
194
195  if(logger)
196    logger->logReturn(action_npp_destroy, rv);
197
198  if(last && logger->bSPALID)
199  {
200    // this will log it
201    epManager->callNP_Shutdown(instance);
202
203    XP_HLIB hLib = NULL;
204
205    epManager->removeEntryPointsForPlugin(instance, &hLib);
206
207    UnloadRealPlugin(hLib);
208  }
209  return rv;
210}
211
212NPError NPP_SetWindow (NPP instance, NPWindow* pNPWindow)
213{
214  if(epManager == NULL)
215    return NPERR_GENERIC_ERROR;
216
217  if(instance == NULL)
218    return NPERR_INVALID_INSTANCE_ERROR;
219
220  if(logger)
221    logger->logCall(action_npp_set_window, (DWORD)instance, (DWORD)pNPWindow);
222
223  NPError rv = epManager->callNPP_SetWindow(instance, pNPWindow);
224
225  if(logger)
226    logger->logReturn(action_npp_set_window, rv);
227
228  return rv;
229}
230
231NPError NPP_NewStream(NPP instance,
232                      NPMIMEType type,
233                      NPStream* stream,
234                      NPBool seekable,
235                      uint16* stype)
236{
237  if(epManager == NULL)
238    return NPERR_GENERIC_ERROR;
239
240  if(instance == NULL)
241    return NPERR_INVALID_INSTANCE_ERROR;
242
243  if(logger)
244    logger->logCall(action_npp_new_stream, (DWORD)instance, (DWORD)type, (DWORD)stream, (DWORD)seekable, (DWORD)stype);
245
246  NPError rv = epManager->callNPP_NewStream(instance, type, stream, seekable, stype);
247
248  if(logger)
249    logger->logReturn(action_npp_new_stream, rv);
250
251  return rv;
252}
253
254int32 NPP_WriteReady (NPP instance, NPStream *stream)
255{
256  if(epManager == NULL)
257    return NPERR_GENERIC_ERROR;
258
259  if(instance == NULL)
260    return NPERR_INVALID_INSTANCE_ERROR;
261
262  if(logger)
263    logger->logCall(action_npp_write_ready, (DWORD)instance, (DWORD)stream);
264
265  int32 rv = epManager->callNPP_WriteReady(instance, stream);
266
267  if(logger)
268    logger->logReturn(action_npp_write_ready, rv);
269
270  return rv;
271}
272
273int32 NPP_Write (NPP instance, NPStream *stream, int32 offset, int32 len, void *buffer)
274{
275  if(epManager == NULL)
276    return NPERR_GENERIC_ERROR;
277
278  if(instance == NULL)
279    return NPERR_INVALID_INSTANCE_ERROR;
280
281  if(logger)
282    logger->logCall(action_npp_write, (DWORD)instance, (DWORD)stream, (DWORD)offset, (DWORD)len, (DWORD)buffer);
283
284  int32 rv = epManager->callNPP_Write(instance, stream, offset, len, buffer);
285
286  if(logger)
287    logger->logReturn(action_npp_write, rv);
288
289  return rv;
290}
291
292NPError NPP_DestroyStream (NPP instance, NPStream *stream, NPError reason)
293{
294  if(epManager == NULL)
295    return NPERR_GENERIC_ERROR;
296
297  if(instance == NULL)
298    return NPERR_INVALID_INSTANCE_ERROR;
299
300  if(logger)
301    logger->logCall(action_npp_destroy_stream, (DWORD)instance, (DWORD)stream, (DWORD)reason);
302
303  NPError rv = epManager->callNPP_DestroyStream(instance, stream, reason);
304
305  if(logger)
306    logger->logReturn(action_npp_destroy_stream, rv);
307
308  return rv;
309}
310
311void NPP_StreamAsFile (NPP instance, NPStream* stream, const char* fname)
312{
313  if(epManager == NULL)
314    return;
315
316  if(instance == NULL)
317    return;
318
319  if(logger)
320    logger->logCall(action_npp_stream_as_file, (DWORD)instance, (DWORD)stream, (DWORD)fname);
321
322  epManager->callNPP_StreamAsFile(instance, stream, fname);
323}
324
325void NPP_Print (NPP instance, NPPrint* printInfo)
326{
327  if(epManager == NULL)
328    return;
329
330  if(logger)
331    logger->logCall(action_npp_print, (DWORD)instance, (DWORD)printInfo);
332
333  epManager->callNPP_Print(instance, printInfo);
334}
335
336void NPP_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData)
337{
338  if(epManager == NULL)
339    return;
340
341  if(instance == NULL)
342    return;
343
344  if(logger)
345    logger->logCall(action_npp_url_notify, (DWORD)instance, (DWORD)url, (DWORD)reason, (DWORD)notifyData);
346
347  epManager->callNPP_URLNotify(instance, url, reason, notifyData);
348}
349
350NPError	NPP_GetValue(NPP instance, NPPVariable variable, void *value)
351{
352  if(epManager == NULL)
353    return NPERR_GENERIC_ERROR;
354
355  if(instance == NULL)
356    return NPERR_INVALID_INSTANCE_ERROR;
357
358  if(logger)
359    logger->logCall(action_npp_get_value, (DWORD)instance, (DWORD)variable, (DWORD)value);
360
361  NPError rv = epManager->callNPP_GetValue(instance, variable, value);
362
363  if(logger)
364    logger->logReturn(action_npp_get_value, rv);
365
366  return rv;
367}
368
369NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value)
370{
371  if(epManager == NULL)
372    return NPERR_GENERIC_ERROR;
373
374  if(instance == NULL)
375    return NPERR_INVALID_INSTANCE_ERROR;
376
377  if(logger)
378    logger->logCall(action_npp_set_value, (DWORD)instance, (DWORD)variable, (DWORD)value);
379
380  NPError rv = epManager->callNPP_SetValue(instance, variable, value);
381
382  if(logger)
383    logger->logReturn(action_npp_set_value, rv);
384
385  return rv;
386}
387
388int16	NPP_HandleEvent(NPP instance, void* event)
389{
390  if(epManager == NULL)
391    return 0;
392
393  if(instance == NULL)
394    return 0;
395
396  if(logger)
397    logger->logCall(action_npp_handle_event, (DWORD)instance, (DWORD)event);
398
399  int16 rv = epManager->callNPP_HandleEvent(instance, event);
400
401  if(logger)
402    logger->logReturn(action_npp_handle_event, rv);
403
404  return rv;
405}
406