2009-10-05 Chris Toshok <toshok@ximian.com>
[moon.git] / plugin / plugin.h
blobebcb5c6883d6c283b3afc6a3dac33772e21b628e
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * plugin.h: 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 #ifndef MOON_PLUGIN
15 #define MOON_PLUGIN
17 #include "moonlight.h"
19 #if PLUGIN_SL_2_0
20 #include <mono/jit/jit.h>
21 #include <mono/metadata/appdomain.h>
22 #include <mono/metadata/assembly.h>
23 #include <mono/metadata/debug-helpers.h>
24 G_BEGIN_DECLS
25 /* because this header sucks */
26 #include <mono/metadata/mono-debug.h>
27 G_END_DECLS
28 #include <mono/metadata/mono-config.h>
29 #endif
31 class MoonlightScriptControlObject;
32 class PluginXamlLoader;
33 class PluginInstance;
34 class BrowserBridge;
35 #if PLUGIN_SL_2_0
36 class Xap;
37 #endif
39 char *NPN_strdup (const char *val);
41 class PluginInstance
43 public:
44 PluginInstance (NPMIMEType pluginType, NPP instance, guint16 mode);
45 ~PluginInstance ();
47 void Initialize (int argc, char *const argn[], char *const argv[]);
48 void Finalize ();
50 // Mozilla plugin related methods
51 NPError GetValue (NPPVariable variable, void *result);
52 NPError SetValue (NPNVariable variable, void *value);
53 NPError SetWindow (NPWindow *window);
54 NPError NewStream (NPMIMEType type, NPStream *stream, NPBool seekable, guint16 *stype);
55 NPError DestroyStream (NPStream *stream, NPError reason);
56 void StreamAsFile (NPStream *stream, const char *fname);
57 gint32 WriteReady (NPStream *stream);
58 gint32 Write (NPStream *stream, gint32 offset, gint32 len, void *buffer);
59 void UrlNotify (const char *url, NPReason reason, void *notifyData);
60 void LoadSplash ();
61 void FlushSplash ();
62 void Print (NPPrint *platformPrint);
63 int16_t EventHandle (void *event);
64 /* @GenerateCBinding,GeneratePInvoke */
65 void ReportException (char *msg, char *details, /* @MarshalAs=string[] */ char **stack_trace, int num_frames);
66 /* @GenerateCBinding,GeneratePInvoke */
67 void *Evaluate (const char *code);
69 NPObject *GetHost ();
71 /* @GenerateCBinding,GeneratePInvoke */
72 void *GetBrowserHost () { return GetHost (); } // same as GetHost, just without bleeding NPObjects into the cbindings
74 void AddWrappedObject (EventObject *obj, NPObject *wrapper);
75 void RemoveWrappedObject (EventObject *obj);
76 NPObject *LookupWrappedObject (EventObject *obj);
78 void AddCleanupPointer (gpointer p);
79 void RemoveCleanupPointer (gpointer p);
81 // [Obselete (this is obsolete in SL b2)]
82 guint32 TimeoutAdd (gint32 interval, GSourceFunc callback, gpointer data);
83 // [Obselete (this is obsolete in SL b2)]
84 void TimeoutStop (guint32 source_id);
86 void Properties ();
88 // Property getters and setters
89 /* @GenerateCBinding,GeneratePInvoke */
90 const char *GetInitParams () { return this->initParams; }
91 void SetInitParams (const char *value);
92 /* @GenerateCBinding,GeneratePInvoke */
93 const char *GetSource () { return this->source; }
94 /* @GenerateCBinding,GeneratePInvoke */
95 const char *GetSourceOriginal () { return this->source_original; }
96 /* @GenerateCBinding,GeneratePInvoke */
97 const char *GetSourceLocation () { return this->source_location; }
98 /* @GenerateCBinding,GeneratePInvoke */
99 const char *GetSourceLocationOriginal () { return this->source_location_original; }
100 char *GetId () { return this->id; }
102 void SetSource (const char *value);
104 char *GetBackground ();
105 bool SetBackground (const char *value);
106 bool GetEnableFramerateCounter ();
107 void SetEnableFramerateCounter (bool value);
108 bool GetEnableRedrawRegions ();
109 void SetEnableRedrawRegions (bool value);
110 /* @GenerateCBinding,GeneratePInvoke */
111 bool GetEnableHtmlAccess ();
112 /* @GenerateCBinding,GeneratePInvoke */
113 bool GetAllowHtmlPopupWindow ();
114 /* @GenerateCBinding,GeneratePInvoke */
115 bool GetWindowless ();
116 bool IsLoaded ();
118 void SetMaxFrameRate (int value);
119 int GetMaxFrameRate ();
120 Deployment *GetDeployment ();
122 BrowserBridge *GetBridge () { return bridge; }
124 MoonlightScriptControlObject *GetRootObject ();
125 NPP GetInstance ();
126 NPWindow *GetWindow ();
127 /* @GenerateCBinding,GeneratePInvoke */
128 Surface *GetSurface () { return surface; }
130 /* @GenerateCBinding,GeneratePInvoke */
131 gint32 GetActualHeight ();
132 /* @GenerateCBinding,GeneratePInvoke */
133 gint32 GetActualWidth ();
135 bool IsCrossDomainApplication () { return cross_domain_app; }
137 static gboolean plugin_button_press_callback (GtkWidget *widget, GdkEventButton *event, gpointer user_data);
139 static Downloader *CreateDownloader (PluginInstance *instance);
141 #if DEBUG
142 struct moon_source : List::Node {
143 char *uri;
144 char *filename;
145 virtual ~moon_source ()
147 g_free (uri);
148 g_free (filename);
151 void AddSource (const char *uri, const char *filename);
152 List *GetSources ();
153 #endif
155 #if PLUGIN_SL_2_0
156 static bool MonoIsLoaded ();
157 static bool DeploymentInit ();
159 bool InitializePluginAppDomain ();
160 bool CreatePluginDeployment ();
162 gpointer ManagedCreateXamlLoaderForFile (XamlLoader* loader, const char *resourceBase, const char *file);
163 gpointer ManagedCreateXamlLoaderForString (XamlLoader* loader, const char *resourceBase, const char *str);
164 void ManagedLoaderDestroy (gpointer loader_object);
165 #endif
167 private:
168 #if DEBUG
169 List *moon_sources;
170 #endif
172 // Gtk controls
173 GtkWidget *container; // plugin container object
174 Surface *surface; // plugin surface object
175 MoonWindow *moon_window;
176 GdkDisplay *display;
178 GSList *timers;
180 guint16 mode; // NP_EMBED, NP_FULL, or NP_BACKGROUND
181 NPWindow *window; // Mozilla window object
182 NPP instance; // Mozilla instance object
183 NPObject *rootobject; // Mozilla jscript object wrapper
184 guint32 xembed_supported; // XEmbed Extension supported
186 GHashTable *wrapped_objects; // wrapped object cache
188 GSList *cleanup_pointers;
190 // Property fields
191 char *initParams;
192 char *source;
193 char *source_original;
194 char *source_location;
195 char *source_location_original;
196 guint source_idle;
197 char *onLoad;
198 char *background;
199 char *onError;
200 char *onResize;
201 char *id;
202 char *splashscreensource;
203 char *onSourceDownloadProgressChanged;
204 char *onSourceDownloadComplete;
206 char *culture;
207 char *uiCulture;
209 int source_size;
211 bool windowless;
212 bool cross_domain_app;
213 bool default_enable_html_access;
214 bool enable_html_access;
215 bool allow_html_popup_window;
216 bool enable_framerate_counter;
217 bool loading_splash;
218 bool is_splash;
219 int maxFrameRate;
221 BrowserBridge *bridge;
223 GtkWidget *properties_fps_label;
224 GtkWidget *properties_cache_label;
227 // The XAML loader, contains a handle to a MonoObject *
229 PluginXamlLoader *xaml_loader;
230 Deployment *deployment;
231 #if PLUGIN_SL_2_0
232 MonoAssembly *system_windows_assembly;
234 static bool mono_is_loaded;
236 // Methods
237 MonoMethod *moon_load_xaml;
238 MonoMethod *moon_initialize_deployment_xap;
239 MonoMethod *moon_initialize_deployment_xaml;
240 MonoMethod *moon_destroy_application;
242 MonoClass *moon_exception;
243 MonoProperty *moon_exception_message;
244 MonoProperty *moon_exception_error_code;
246 void LoadXAP (const char*url, const char *fname);
247 void DestroyApplication ();
249 MonoMethod *MonoGetMethodFromName (MonoClass *klass, const char *name, int narg);
250 MonoProperty *MonoGetPropertyFromName (MonoClass *klass, const char *name);
252 ErrorEventArgs* ManagedExceptionToErrorEventArgs (MonoObject *exc);
254 bool ManagedInitializeDeployment (const char *file);
255 void ManagedDestroyApplication ();
257 gpointer ManagedCreateXamlLoader (XamlLoader* native_loader, const char *resourceBase, const char *file, const char *str);
258 #endif
260 // The name of the file that we are missing, and we requested to be loaded
261 char *vm_missing_file;
263 // Private methods
264 void CreateWindow ();
265 void UpdateSource ();
266 void UpdateSourceByReference (const char *value);
267 void LoadXAML ();
268 void SetPageURL ();
269 char* GetPageLocation ();
270 void CrossDomainApplicationCheck (const char *source);
272 void TryLoadBridge (const char *prefix);
274 static gboolean IdleUpdateSourceByReference (gpointer data);
276 static void ReportFPS (Surface *surface, int nframes, float nsecs, void *user_data);
277 static void ReportCache (Surface *surface, long bytes, void *user_data);
278 static void properties_dialog_response (GtkWidget *dialog, int response, PluginInstance *plugin);
280 static void network_error_tickcall (EventObject *data);
281 static void splashscreen_error_tickcall (EventObject *data);
284 extern GSList *plugin_instances;
286 #define NPID(x) NPN_GetStringIdentifier (x)
288 #define STRDUP_FROM_VARIANT(v) (g_strndup ((char *) NPVARIANT_TO_STRING (v).utf8characters, NPVARIANT_TO_STRING (v).utf8length))
289 #define STRLEN_FROM_VARIANT(v) ((size_t) NPVARIANT_TO_STRING (v).utf8length)
291 #define STREAM_NOTIFY(x) ((StreamNotify*) x)
293 #define STREAM_NOTIFY_DATA(x) ((StreamNotify*) x)->pdata
295 #define IS_NOTIFY_SOURCE(x) \
296 (!x ? true : (((StreamNotify*) x)->type == StreamNotify::SOURCE))
298 #define IS_NOTIFY_SPLASHSOURCE(x) \
299 (!x ? true : (((StreamNotify*) x)->type == StreamNotify::SPLASHSOURCE))
301 #define IS_NOTIFY_DOWNLOADER(x) \
302 (!x ? StreamNotify::NONE : (((StreamNotify*) x)->type == StreamNotify::DOWNLOADER))
304 #define IS_NOTIFY_REQUEST(x) \
305 (!x ? StreamNotify::NONE : (((StreamNotify*) x)->type == StreamNotify::REQUEST))
307 class StreamNotify
309 public:
310 enum StreamNotifyFlags {
311 NONE = 0,
312 SOURCE = 1,
313 SPLASHSOURCE = 2,
314 DOWNLOADER = 3,
315 REQUEST = 4
318 StreamNotifyFlags type;
319 void *pdata;
321 StreamNotify () : type (NONE), pdata (NULL) {};
322 StreamNotify (void *data) : type (NONE), pdata (data) {};
323 StreamNotify (StreamNotifyFlags type) : type (type), pdata (NULL) {};
324 StreamNotify (StreamNotifyFlags type, void *data) : type (type), pdata (data) {};
325 StreamNotify (StreamNotifyFlags type, DependencyObject *dob) : type (type), pdata (dob)
327 if (dob)
328 dob->ref ();
331 ~StreamNotify ()
333 if (type == DOWNLOADER && pdata)
334 ((DependencyObject *) pdata)->unref ();
338 class PluginXamlLoader : public XamlLoader
340 PluginXamlLoader (const char *resourceBase, const char *filename, const char *str, PluginInstance *plugin, Surface *surface);
341 bool InitializeLoader ();
342 PluginInstance *plugin;
343 bool initialized;
344 bool xaml_is_managed;
346 #if PLUGIN_SL_2_0
347 gpointer managed_loader;
348 Xap *xap;
349 #endif
350 public:
351 virtual ~PluginXamlLoader ();
352 const char *TryLoad (int *error);
354 bool SetProperty (void *parser, Value *top_level, const char *xmlns, Value *target, void *target_data, Value *target_parent, const char *prop_xmlns, const char *name, Value* value, void* value_data, int flags = 0);
356 static PluginXamlLoader *FromFilename (const char *resourceBase, const char *filename, PluginInstance *plugin, Surface *surface)
358 return new PluginXamlLoader (resourceBase, filename, NULL, plugin, surface);
361 static PluginXamlLoader *FromStr (const char *resourceBase, const char *str, PluginInstance *plugin, Surface *surface)
363 return new PluginXamlLoader (resourceBase, NULL, str, plugin, surface);
366 bool IsManaged () { return xaml_is_managed; }
368 virtual bool LoadVM ();
371 G_BEGIN_DECLS
373 const char *get_plugin_dir (void);
375 char *plugin_instance_get_id (PluginInstance *instance);
377 void plugin_instance_get_browser_runtime_settings (bool *debug, bool *html_access,
378 bool *httpnet_access, bool *script_access);
380 void *plugin_instance_load_url (PluginInstance *instance, char *url, gint32 *length);
382 PluginXamlLoader *plugin_xaml_loader_from_str (const char *str, const char *resourceBase, PluginInstance *plugin, Surface *surface);
384 G_END_DECLS
386 #endif /* MOON_PLUGIN */