Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / modules / plugin / sdk / samples / common / npp_gate.cpp
blob9168c3680ecfb8e7de8ca3aae3d7cd9a2ffb45e2
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 // Implementation of plugin entry points (NPP_*)
40 #include "pluginbase.h"
42 // here the plugin creates a plugin instance object which
43 // will be associated with this newly created NPP instance and
44 // will do all the necessary job
45 NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16_t mode, int16_t argc, char* argn[], char* argv[], NPSavedData* saved)
47 if (!instance)
48 return NPERR_INVALID_INSTANCE_ERROR;
50 // create a new plugin instance object
51 // initialization will be done when the associated window is ready
52 nsPluginCreateData ds;
54 ds.instance = instance;
55 ds.type = pluginType;
56 ds.mode = mode;
57 ds.argc = argc;
58 ds.argn = argn;
59 ds.argv = argv;
60 ds.saved = saved;
62 nsPluginInstanceBase * plugin = NS_NewPluginInstance(&ds);
63 if (!plugin)
64 return NPERR_OUT_OF_MEMORY_ERROR;
66 // associate the plugin instance object with NPP instance
67 instance->pdata = (void *)plugin;
68 return NPERR_NO_ERROR;
71 // here is the place to clean up and destroy the nsPluginInstance object
72 NPError NPP_Destroy (NPP instance, NPSavedData** save)
74 if (!instance)
75 return NPERR_INVALID_INSTANCE_ERROR;
77 nsPluginInstanceBase * plugin = (nsPluginInstanceBase *)instance->pdata;
78 if (plugin) {
79 plugin->shut();
80 NS_DestroyPluginInstance(plugin);
82 return NPERR_NO_ERROR;
85 // during this call we know when the plugin window is ready or
86 // is about to be destroyed so we can do some gui specific
87 // initialization and shutdown
88 NPError NPP_SetWindow (NPP instance, NPWindow* pNPWindow)
90 if (!instance)
91 return NPERR_INVALID_INSTANCE_ERROR;
93 if (!pNPWindow)
94 return NPERR_GENERIC_ERROR;
96 nsPluginInstanceBase * plugin = (nsPluginInstanceBase *)instance->pdata;
98 if (!plugin)
99 return NPERR_GENERIC_ERROR;
101 // window just created
102 if (!plugin->isInitialized() && pNPWindow->window) {
103 if (!plugin->init(pNPWindow)) {
104 NS_DestroyPluginInstance(plugin);
105 return NPERR_MODULE_LOAD_FAILED_ERROR;
109 // window goes away
110 if (!pNPWindow->window && plugin->isInitialized())
111 return plugin->SetWindow(pNPWindow);
113 // window resized?
114 if (plugin->isInitialized() && pNPWindow->window)
115 return plugin->SetWindow(pNPWindow);
117 // this should not happen, nothing to do
118 if (!pNPWindow->window && !plugin->isInitialized())
119 return plugin->SetWindow(pNPWindow);
121 return NPERR_NO_ERROR;
124 NPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16_t* stype)
126 if (!instance)
127 return NPERR_INVALID_INSTANCE_ERROR;
129 nsPluginInstanceBase * plugin = (nsPluginInstanceBase *)instance->pdata;
130 if (!plugin)
131 return NPERR_GENERIC_ERROR;
133 return plugin->NewStream(type, stream, seekable, stype);
136 int32_t NPP_WriteReady (NPP instance, NPStream *stream)
138 if (!instance)
139 return 0x0fffffff;
141 nsPluginInstanceBase * plugin = (nsPluginInstanceBase *)instance->pdata;
142 if (!plugin)
143 return 0x0fffffff;
145 return plugin->WriteReady(stream);
148 int32_t NPP_Write (NPP instance, NPStream *stream, int32_t offset, int32_t len, void *buffer)
150 if (!instance)
151 return len;
153 nsPluginInstanceBase * plugin = (nsPluginInstanceBase *)instance->pdata;
154 if (!plugin)
155 return len;
157 return plugin->Write(stream, offset, len, buffer);
160 NPError NPP_DestroyStream (NPP instance, NPStream *stream, NPError reason)
162 if (!instance)
163 return NPERR_INVALID_INSTANCE_ERROR;
165 nsPluginInstanceBase * plugin = (nsPluginInstanceBase *)instance->pdata;
166 if (!plugin)
167 return NPERR_GENERIC_ERROR;
169 return plugin->DestroyStream(stream, reason);
172 void NPP_StreamAsFile (NPP instance, NPStream* stream, const char* fname)
174 if (!instance)
175 return;
177 nsPluginInstanceBase * plugin = (nsPluginInstanceBase *)instance->pdata;
178 if (!plugin)
179 return;
181 plugin->StreamAsFile(stream, fname);
184 void NPP_Print (NPP instance, NPPrint* printInfo)
186 if (!instance)
187 return;
189 nsPluginInstanceBase * plugin = (nsPluginInstanceBase *)instance->pdata;
190 if (!plugin)
191 return;
193 plugin->Print(printInfo);
196 void NPP_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData)
198 if (!instance)
199 return;
201 nsPluginInstanceBase * plugin = (nsPluginInstanceBase *)instance->pdata;
202 if (!plugin)
203 return;
205 plugin->URLNotify(url, reason, notifyData);
208 NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value)
210 if (!instance)
211 return NPERR_INVALID_INSTANCE_ERROR;
213 nsPluginInstanceBase * plugin = (nsPluginInstanceBase *)instance->pdata;
214 if (!plugin)
215 return NPERR_GENERIC_ERROR;
217 return plugin->GetValue(variable, value);
220 NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value)
222 if (!instance)
223 return NPERR_INVALID_INSTANCE_ERROR;
225 nsPluginInstanceBase * plugin = (nsPluginInstanceBase *)instance->pdata;
226 if (!plugin)
227 return NPERR_GENERIC_ERROR;
229 return plugin->SetValue(variable, value);
232 int16_t NPP_HandleEvent(NPP instance, void* event)
234 if (!instance)
235 return 0;
237 nsPluginInstanceBase * plugin = (nsPluginInstanceBase *)instance->pdata;
238 if (!plugin)
239 return 0;
241 return plugin->HandleEvent(event);