Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / modules / plugin / sdk / samples / npthread / windows / plugin.cpp
blobbc77bc452e025e4c27e3ba95ff06ee499679645a
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
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/
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.
15 * The Original Code is mozilla.org code.
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.
22 * Contributor(s):
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.
36 * ***** END LICENSE BLOCK ***** */
38 #include "windows.h"
40 #include "npfunctions.h"
41 #include "npapi.h"
42 #include "plugin.h"
43 #include "plugload.h"
44 #include "dbg.h"
46 extern NPNetscapeFuncs NPNFuncs;
47 NPNetscapeFuncs wrapperNPNFuncs;
48 NPPluginFuncs pluginNPPFuncs;
50 typedef NPError (__stdcall * NP_GETENTRYPOINTS)(NPPluginFuncs *);
51 typedef NPError (__stdcall * NP_INITIALIZE)(NPNetscapeFuncs *);
52 typedef NPError (__stdcall * NP_SHUTDOWN)(void);
54 NP_SHUTDOWN plugin_NP_Shutdown = NULL;
55 HINSTANCE pluginLibrary = NULL;
57 nsPluginThread::nsPluginThread(DWORD aP1) : CThread(),
58 mP1(aP1),
59 mP2(0),
60 mP3(0),
61 mP4(0),
62 mP5(0),
63 mP6(0),
64 mP7(0)
66 dbgOut1("nsPluginThread::nsPluginThread");
68 open(this);
71 nsPluginThread::~nsPluginThread()
73 dbgOut1("nsPluginThread::~nsPluginThread");
75 close(this);
78 BOOL nsPluginThread::init()
80 dbgOut1("nsPluginThread::init");
82 // scan plugins dir for available plugins to see if we have anything
83 // for the given mimetype
84 pluginLibrary = LoadRealPlugin((NPMIMEType)mP1);
85 if(!pluginLibrary)
86 return FALSE;
88 NP_GETENTRYPOINTS plugin_NP_GetEntryPoints = (NP_GETENTRYPOINTS)GetProcAddress(pluginLibrary, "NP_GetEntryPoints");
89 if(!plugin_NP_GetEntryPoints)
90 return FALSE;
92 NP_INITIALIZE plugin_NP_Initialize = (NP_INITIALIZE)GetProcAddress(pluginLibrary, "NP_Initialize");
93 if(!plugin_NP_Initialize)
94 return FALSE;
96 plugin_NP_Shutdown = (NP_SHUTDOWN)GetProcAddress(pluginLibrary, "NP_Shutdown");
97 if(!plugin_NP_Shutdown)
98 return FALSE;
100 // fill callbacks structs
101 memset(&pluginNPPFuncs, 0, sizeof(NPPluginFuncs));
102 pluginNPPFuncs.size = sizeof(NPPluginFuncs);
104 plugin_NP_GetEntryPoints(&pluginNPPFuncs);
106 // inform the plugin about our entry point it should call
107 memset((void *)&wrapperNPNFuncs, 0, sizeof(wrapperNPNFuncs));
108 wrapperNPNFuncs.size = sizeof(wrapperNPNFuncs);
109 wrapperNPNFuncs.version = NPNFuncs.version;
110 wrapperNPNFuncs.geturlnotify = NPN_GetURLNotify;
111 wrapperNPNFuncs.geturl = NPN_GetURL;
112 wrapperNPNFuncs.posturlnotify = NPN_PostURLNotify;
113 wrapperNPNFuncs.posturl = NPN_PostURL;
114 wrapperNPNFuncs.requestread = NPN_RequestRead;
115 wrapperNPNFuncs.newstream = NPN_NewStream;
116 wrapperNPNFuncs.write = NPN_Write;
117 wrapperNPNFuncs.destroystream = NPN_DestroyStream;
118 wrapperNPNFuncs.status = NPN_Status;
119 wrapperNPNFuncs.uagent = NPN_UserAgent;
120 wrapperNPNFuncs.memalloc = NPN_MemAlloc;
121 wrapperNPNFuncs.memfree = NPN_MemFree;
122 wrapperNPNFuncs.memflush = NPN_MemFlush;
123 wrapperNPNFuncs.reloadplugins = NPN_ReloadPlugins;
124 wrapperNPNFuncs.getJavaEnv = NULL;
125 wrapperNPNFuncs.getJavaPeer = NULL;
126 wrapperNPNFuncs.getvalue = NPN_GetValue;
127 wrapperNPNFuncs.setvalue = NPN_SetValue;
128 wrapperNPNFuncs.invalidaterect = NPN_InvalidateRect;
129 wrapperNPNFuncs.invalidateregion = NPN_InvalidateRegion;
130 wrapperNPNFuncs.forceredraw = NPN_ForceRedraw;
132 plugin_NP_Initialize(&wrapperNPNFuncs);
134 return TRUE;
137 void nsPluginThread::shut()
139 dbgOut1("nsPluginThread::shut");
141 if (!plugin_NP_Shutdown)
142 plugin_NP_Shutdown();
144 if (!pluginLibrary)
145 UnloadRealPlugin(pluginLibrary);
148 // NPP_ call translator
149 DWORD nsPluginThread::callNPP(npapiAction aAction, DWORD aP1, DWORD aP2,
150 DWORD aP3, DWORD aP4, DWORD aP5,
151 DWORD aP6, DWORD aP7)
153 // don't enter untill thread is ready
154 while (isBusy()) {
155 Sleep(0);
158 mP1 = aP1;
159 mP2 = aP2;
160 mP3 = aP3;
161 mP4 = aP4;
162 mP5 = aP5;
163 mP6 = aP6;
164 mP7 = aP7;
166 doAction(aAction);
168 // don't return untill thread is ready
169 while (isBusy()) {
170 Sleep(0);
173 return NPERR_NO_ERROR;
175 void nsPluginThread::dispatch()
177 dbgOut2("nsPluginThread::dispatch: %s", FormatAction(mAction));
179 switch (mAction) {
180 case action_npp_new:
181 pluginNPPFuncs.newp((NPMIMEType)mP1, (NPP)mP2, (uint16)mP3, (int16)mP4, (char**)mP5, (char**)mP6, (NPSavedData*)mP7);
182 break;
183 case action_npp_destroy:
184 pluginNPPFuncs.destroy((NPP)mP1, (NPSavedData**)mP2);
185 break;
186 case action_npp_set_window:
187 pluginNPPFuncs.setwindow((NPP)mP1, (NPWindow*)mP2);
188 break;
189 case action_npp_new_stream:
190 pluginNPPFuncs.newstream((NPP)mP1, (NPMIMEType)mP2, (NPStream*)mP3, (NPBool)mP4, (uint16*)mP5);
191 break;
192 case action_npp_destroy_stream:
194 NPStream * stream = (NPStream *)mP2;
195 pluginNPPFuncs.destroystream((NPP)mP1, stream, (NPError)mP3);
196 break;
198 case action_npp_stream_as_file:
200 NPStream * stream = (NPStream *)mP2;
201 pluginNPPFuncs.asfile((NPP)mP1, stream, (char*)mP3);
202 break;
204 case action_npp_write_ready:
205 pluginNPPFuncs.writeready((NPP)mP1, (NPStream *)mP2);
206 break;
207 case action_npp_write:
208 pluginNPPFuncs.write((NPP)mP1, (NPStream *)mP2, (int32)mP3, (int32)mP4, (void *)mP5);
209 break;
210 case action_npp_print:
211 pluginNPPFuncs.print((NPP)mP1, (NPPrint*)mP2);
212 break;
213 case action_npp_handle_event:
214 pluginNPPFuncs.event((NPP)mP1, (void *)mP2);
215 break;
216 case action_npp_url_notify:
217 pluginNPPFuncs.urlnotify((NPP)mP1, (const char*)mP2, (NPReason)mP3, (void*)mP4);
218 break;
219 case action_npp_get_java_class:
220 // Deprecated action
221 break;
222 case action_npp_get_value:
223 pluginNPPFuncs.getvalue((NPP)mP1, (NPPVariable)mP2, (void *)mP3);
224 break;
225 case action_npp_set_value:
226 pluginNPPFuncs.setvalue((NPP)mP1, (NPNVariable)mP2, (void *)mP3);
227 break;
228 default:
229 dbgOut1("Unexpected action!");
230 break;