1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * plugin-glue.cpp: MoonLight browser plugin.
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.
16 #include "moonlight.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;
25 NPP_New (NPMIMEType pluginType
, NPP instance
, uint16_t mode
, int16_t argc
, char *argn
[], char *argv
[], NPSavedData
*saved
)
28 return NPERR_INVALID_INSTANCE_ERROR
;
30 PluginInstance
*plugin
= new PluginInstance (instance
, mode
);
32 return NPERR_OUT_OF_MEMORY_ERROR
;
36 plugin
->Initialize (argc
, argn
, argv
);
37 instance
->pdata
= plugin
;
39 return NPERR_NO_ERROR
;
43 NPP_Destroy (NPP instance
, NPSavedData
**save
)
46 return NPERR_INVALID_INSTANCE_ERROR
;
48 PluginInstance
*plugin
= (PluginInstance
*) instance
->pdata
;
49 Deployment::SetCurrent (plugin
->GetDeployment ());
52 instance
->pdata
= NULL
;
57 return NPERR_NO_ERROR
;
61 NPP_SetWindow (NPP instance
, NPWindow
*window
)
64 return NPERR_INVALID_INSTANCE_ERROR
;
66 PluginInstance
*plugin
= (PluginInstance
*) instance
->pdata
;
68 return plugin
->SetWindow (window
);
72 NPP_NewStream (NPP instance
, NPMIMEType type
, NPStream
*stream
, NPBool seekable
, uint16_t *stype
)
75 return NPERR_INVALID_INSTANCE_ERROR
;
77 PluginInstance
*plugin
= (PluginInstance
*) instance
->pdata
;
79 return plugin
->NewStream (type
, stream
, seekable
, stype
);
83 NPP_DestroyStream (NPP instance
, NPStream
*stream
, NPError reason
)
86 return NPERR_INVALID_INSTANCE_ERROR
;
88 PluginInstance
*plugin
= (PluginInstance
*) instance
->pdata
;
90 return plugin
->DestroyStream (stream
, reason
);
94 NPP_StreamAsFile (NPP instance
, NPStream
*stream
, const char *fname
)
99 PluginInstance
*plugin
= (PluginInstance
*) instance
->pdata
;
101 plugin
->StreamAsFile (stream
, fname
);
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
);
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
);
127 NPP_Print (NPP instance
, NPPrint
*platformPrint
)
129 if (instance
== NULL
)
132 PluginInstance
*plugin
= (PluginInstance
*) instance
->pdata
;
134 plugin
->Print (platformPrint
);
138 NPP_URLNotify (NPP instance
, const char *url
, NPReason reason
, void *notifyData
)
140 if (instance
== NULL
)
143 PluginInstance
*plugin
= (PluginInstance
*) instance
->pdata
;
145 plugin
->UrlNotify (url
, reason
, notifyData
);
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
);
160 NPP_GetValue (NPP instance
, NPPVariable variable
, void *result
)
162 NPError err
= NPERR_NO_ERROR
;
165 case NPPVpluginNeedsXEmbed
:
166 *((NPBool
*)result
) = true;
168 case NPPVpluginNameString
:
169 *((char **)result
) = (char *) PLUGIN_NAME
;
171 case NPPVpluginDescriptionString
:
172 *((char **)result
) = (char *) PLUGIN_DESCRIPTION
;
175 if (instance
== NULL
)
176 return NPERR_INVALID_INSTANCE_ERROR
;
178 PluginInstance
*plugin
= (PluginInstance
*) instance
->pdata
;
179 err
= plugin
->GetValue (variable
, result
);
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
);
197 NPP_GetMIMEDescription (void)
199 return (char *) (MIME_TYPES_HANDLED
);
202 static bool runtime_initialized
= false;
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
;
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
);
246 // printf ("shutdown_moonlight (): proceeding with shutdown, there are no more deployments nor plugins left.\n");
248 downloader_destroy ();
249 plugin_destroy_classes ();
251 runtime_initialized
= false;
259 shutdown_moonlight (NULL
);