2009-11-13 Jeffrey Stedfast <fejj@novell.com>
[moon.git] / plugin / plugin-glue.cpp
blob26d3de5671257e227adaf961e523e993ef718494
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * plugin-glue.cpp: MoonLight browser plugin.
5 * Contact:
6 * Moonlight List (moonlight-list@lists.ximian.com)
8 * Copyright 2007 Novell, Inc. (http://www.novell.com)
10 * See the LICENSE file included with the distribution for details.
14 #include <config.h>
16 #include "moonlight.h"
17 #include "plugin.h"
18 #include "plugin-class.h"
19 #include "plugin-downloader.h"
21 /* the number of plugin instances running in the browser */
22 static int browser_plugins = 0;
24 NPError
25 NPP_New (NPMIMEType pluginType, NPP instance, uint16_t mode, int16_t argc, char *argn[], char *argv[], NPSavedData *saved)
27 if (!instance)
28 return NPERR_INVALID_INSTANCE_ERROR;
30 PluginInstance *plugin = new PluginInstance (instance, mode);
31 if (plugin == NULL)
32 return NPERR_OUT_OF_MEMORY_ERROR;
34 browser_plugins++;
36 plugin->Initialize (argc, argn, argv);
37 instance->pdata = plugin;
39 return NPERR_NO_ERROR;
42 NPError
43 NPP_Destroy (NPP instance, NPSavedData **save)
45 if (instance == NULL)
46 return NPERR_INVALID_INSTANCE_ERROR;
48 PluginInstance *plugin = (PluginInstance *) instance->pdata;
49 Deployment::SetCurrent (plugin->GetDeployment ());
51 plugin->Shutdown ();
52 instance->pdata = NULL;
53 plugin->unref ();
55 browser_plugins--;
57 return NPERR_NO_ERROR;
60 NPError
61 NPP_SetWindow (NPP instance, NPWindow *window)
63 if (instance == NULL)
64 return NPERR_INVALID_INSTANCE_ERROR;
66 PluginInstance *plugin = (PluginInstance *) instance->pdata;
68 return plugin->SetWindow (window);
71 NPError
72 NPP_NewStream (NPP instance, NPMIMEType type, NPStream *stream, NPBool seekable, uint16_t *stype)
74 if (instance == NULL)
75 return NPERR_INVALID_INSTANCE_ERROR;
77 PluginInstance *plugin = (PluginInstance *) instance->pdata;
79 return plugin->NewStream (type, stream, seekable, stype);
82 NPError
83 NPP_DestroyStream (NPP instance, NPStream *stream, NPError reason)
85 if (instance == NULL)
86 return NPERR_INVALID_INSTANCE_ERROR;
88 PluginInstance *plugin = (PluginInstance *) instance->pdata;
90 return plugin->DestroyStream (stream, reason);
93 void
94 NPP_StreamAsFile (NPP instance, NPStream *stream, const char *fname)
96 if (instance == NULL)
97 return;
99 PluginInstance *plugin = (PluginInstance *) instance->pdata;
101 plugin->StreamAsFile (stream, fname);
104 int32_t
105 NPP_WriteReady (NPP instance, NPStream *stream)
107 if (instance == NULL)
108 return NPERR_INVALID_INSTANCE_ERROR;
110 PluginInstance *plugin = (PluginInstance *) instance->pdata;
112 return plugin->WriteReady (stream);
115 int32_t
116 NPP_Write (NPP instance, NPStream *stream, int32_t offset, int32_t len, void *buffer)
118 if (instance == NULL)
119 return NPERR_INVALID_INSTANCE_ERROR;
121 PluginInstance *plugin = (PluginInstance *) instance->pdata;
123 return plugin->Write (stream, offset, len, buffer);
126 void
127 NPP_Print (NPP instance, NPPrint *platformPrint)
129 if (instance == NULL)
130 return;
132 PluginInstance *plugin = (PluginInstance *) instance->pdata;
134 plugin->Print (platformPrint);
137 void
138 NPP_URLNotify (NPP instance, const char *url, NPReason reason, void *notifyData)
140 if (instance == NULL)
141 return;
143 PluginInstance *plugin = (PluginInstance *) instance->pdata;
145 plugin->UrlNotify (url, reason, notifyData);
149 int16_t
150 NPP_HandleEvent (NPP instance, void *event)
152 if (instance == NULL)
153 return NPERR_INVALID_INSTANCE_ERROR;
155 PluginInstance *plugin = (PluginInstance *) instance->pdata;
156 return plugin->EventHandle (event);
159 NPError
160 NPP_GetValue (NPP instance, NPPVariable variable, void *result)
162 NPError err = NPERR_NO_ERROR;
164 switch (variable) {
165 case NPPVpluginNeedsXEmbed:
166 *((NPBool *)result) = true;
167 break;
168 case NPPVpluginNameString:
169 *((char **)result) = (char *) PLUGIN_NAME;
170 break;
171 case NPPVpluginDescriptionString:
172 *((char **)result) = (char *) PLUGIN_DESCRIPTION;
173 break;
174 default:
175 if (instance == NULL)
176 return NPERR_INVALID_INSTANCE_ERROR;
178 PluginInstance *plugin = (PluginInstance *) instance->pdata;
179 err = plugin->GetValue (variable, result);
180 break;
183 return err;
186 NPError
187 NPP_SetValue (NPP instance, NPNVariable variable, void *value)
189 if (instance == NULL)
190 return NPERR_INVALID_INSTANCE_ERROR;
192 PluginInstance *plugin = (PluginInstance *) instance->pdata;
193 return plugin->SetValue (variable, value);
196 char *
197 NPP_GetMIMEDescription (void)
199 return (char *) (MIME_TYPES_HANDLED);
202 static bool runtime_initialized = false;
204 NPError
205 NPP_Initialize (void)
207 NPNToolkitType toolkit = (NPNToolkitType)0;
209 NPN_GetValue (NULL, NPNVToolkit, &toolkit);
210 if (toolkit != (NPNToolkitType)NPNVGtk2) {
211 g_warning ("we don't have the toolkit we need");
212 return NPERR_INCOMPATIBLE_VERSION_ERROR;
214 // We dont need to initialize mono vm and gtk more than one time.
215 if (!g_thread_supported ()) {
216 g_warning ("host has not initialized threads");
217 //g_thread_init (NULL);
220 downloader_initialize ();
222 if (!runtime_initialized) {
223 runtime_initialized = true;
224 runtime_init_browser (get_plugin_dir ());
227 plugin_init_classes ();
229 return NPERR_NO_ERROR;
232 static gboolean
233 shutdown_moonlight (gpointer data)
235 /* check if we still should be shutting down */
236 if (browser_plugins != 0)
237 return false; /* another plugin instance has been created after shutting down the last one earlier */
239 /* check if all deployments and all plugins have been freed. */
240 if (Deployment::GetDeploymentCount () != 0 || PluginInstance::GetPluginCount () != 0) {
241 // printf ("shutdown_moonlight (): there are %i deployments and %i plugins left, postponing shutdown a bit.\n", Deployment::GetDeploymentCount (), PluginInstance::GetPluginCount ());
242 g_timeout_add_full (100, G_PRIORITY_DEFAULT_IDLE, shutdown_moonlight, NULL, NULL);
243 return FALSE;
246 // printf ("shutdown_moonlight (): proceeding with shutdown, there are no more deployments nor plugins left.\n");
248 downloader_destroy ();
249 plugin_destroy_classes ();
250 runtime_shutdown ();
251 runtime_initialized = false;
253 return FALSE;
256 void
257 NPP_Shutdown (void)
259 shutdown_moonlight (NULL);