Author: rolf
Date: 2007-09-21 18:50:51 -0400 (Fri, 21 Sep 2007)
New Revision: 86183

Modified:
   trunk/moon/plugin/ChangeLog
   trunk/moon/plugin/moonlight.cs
   trunk/moon/plugin/plugin.cpp
   trunk/moon/plugin/plugin.h
Log:
* plugin.cpp, plugin.h: Add a plugin_unload_callback to PluginInstance,
  and call it in the dtor.
* moonlight.cs: Added UnloadDomain: unloads the domain that was loaded
  for a specific plugin, and set the corresponding native callback.
  Keep a hash table of all created domains so that we know which
  domain to unload, Delete some dead code (DisposeXamlLoader,
  ~Loader, Dispose)

Modified: trunk/moon/plugin/ChangeLog
===================================================================
--- trunk/moon/plugin/ChangeLog 2007-09-21 22:14:09 UTC (rev 86182)
+++ trunk/moon/plugin/ChangeLog 2007-09-21 22:50:51 UTC (rev 86183)
@@ -1,3 +1,12 @@
+2007-09-22  Rolf Bjarne Kvinge <[EMAIL PROTECTED]> 
+
+       * plugin.cpp, plugin.h: Add a plugin_unload_callback to PluginInstance, 
and
+         call it in the dtor.
+       * moonlight.cs: Added UnloadDomain: unloads the domain that was loaded 
for a
+         specific plugin, and set the corresponding native callback. Keep a 
hash
+         table of all created domains so that we know which domain to unload,
+         Delete some dead code (DisposeXamlLoader, ~Loader, Dispose)
+
 2007-09-21  Rolf Bjarne Kvinge <[EMAIL PROTECTED]> 
 
        * plugin.cpp: If an assembly already has been downloaded once, don't 
try to

Modified: trunk/moon/plugin/moonlight.cs
===================================================================
--- trunk/moon/plugin/moonlight.cs      2007-09-21 22:14:09 UTC (rev 86182)
+++ trunk/moon/plugin/moonlight.cs      2007-09-21 22:50:51 UTC (rev 86183)
@@ -39,10 +39,15 @@
 using System.ComponentModel;
 
 namespace Moonlight {
-
        public class Loader {
+               [DllImport ("moonplugin")]
+               static extern void plugin_set_unload_callback (IntPtr plugin, 
plugin_unload puc);
+               delegate void plugin_unload (IntPtr plugin);
+               
+               static Hashtable domains = new Hashtable ();
+               
                Mono.Xaml.XamlLoader rl;
-               AppDomain domain;
+               plugin_unload unload_callback;
                
                // [DONE] 1. Load XAML file 
                // 2. Make sure XAML file exposes a few new properites:
@@ -64,19 +69,28 @@
                        }
                }
                
-               public static void DisposeXamlLoader (Loader loader)
+               private AppDomain GetDomain (IntPtr plugin)
                {
-                       try {
-                               loader.Dispose ();
-                       } catch (Exception ex) {
-                               Console.Error.WriteLine ("Loader::Dispose: 
Unable to dispose: {0}.", ex.Message);
+                       AppDomain domain;
+                       if (domains.Contains (plugin)) {
+                               domain = (AppDomain) domains [plugin];
+                       } else {
+                               domain = Helper.CreateDomain (plugin);
+                               domains [plugin] = domain;
+                               if (plugin != IntPtr.Zero) {
+                                       // Set the plugin unload callback so 
that we get a chance to unload the domain when 
+                                       // the plugin unloads.
+                                       unload_callback = new plugin_unload 
(UnloadDomain);
+                                       plugin_set_unload_callback (plugin, 
unload_callback);
+                               }
                        }
+                       return domain;
                }
-               
+                               
                public Loader (IntPtr native_loader, IntPtr plugin, IntPtr 
surface, string filename, string contents)
                {
-                       domain = Helper.CreateDomain (plugin);
-
+                       AppDomain domain = GetDomain (plugin);
+                       
                        rl = (Mono.Xaml.XamlLoader) 
Helper.CreateInstanceAndUnwrap (
                                domain, typeof 
(DependencyObject).Assembly.FullName, 
                                "Mono.Xaml.ManagedXamlLoader");
@@ -84,20 +98,19 @@
                        rl.Setup (native_loader, plugin, surface, filename, 
contents);
                }
                
-               public void Dispose ()
+               public static void UnloadDomain (IntPtr plugin)
                {
-                       if (domain != null) {
-                               Helper.UnloadDomain (domain);
-                               domain = null;
+                       try {
+                               AppDomain domain = (AppDomain) domains [plugin];
+                               if (domain != null) {
+                                       Console.WriteLine 
("Moonlight.Loader::UnloadDomain ({0}): {1}.", plugin, domain.FriendlyName);
+                                       domains.Remove (plugin);
+                                       Helper.UnloadDomain (domain);
+                               }
+                       } catch (Exception ex) {
+                               Console.Error.WriteLine 
("Moonlight.Loader::UnloadDomain ({0}): Unable to unload domain: {1}.", plugin, 
ex.Message);
                        }
-                       rl = null;
-                       GC.SuppressFinalize (this);
                }
-               
-               ~Loader ()
-               {
-                       Dispose ();
-               }
        }
 
        class Moonlight {

Modified: trunk/moon/plugin/plugin.cpp
===================================================================
--- trunk/moon/plugin/plugin.cpp        2007-09-21 22:14:09 UTC (rev 86182)
+++ trunk/moon/plugin/plugin.cpp        2007-09-21 22:50:51 UTC (rev 86183)
@@ -105,6 +105,17 @@
 
 GSList *plugin_instances = NULL;
 
+void
+plugin_set_unload_callback (PluginInstance* plugin, plugin_unload_callback* 
puc)
+{
+       if (!plugin) {
+               printf ("Trying to set plugin unload callback on a null 
plugin.\n");
+               return;
+       }
+       
+       plugin->SetUnloadCallback (puc);
+}
+
 PluginInstance::PluginInstance (NPP instance, uint16 mode)
 {
        this->mode = mode;
@@ -128,6 +139,7 @@
        
        this->vm_missing_file = NULL;
        this->xaml_loader = NULL;
+       this->plugin_unload = NULL;
 
        this->timers = NULL;
 
@@ -155,7 +167,7 @@
        // The code below was an attempt at fixing this, but we are still 
getting spurious errors
        // we might have another source of problems
        //
-       fprintf (stderr, "Destroying the plugin: %p\n", surface);
+       fprintf (stderr, "Destroying the surface: %p, plugin: %p\n", surface, 
this);
        if (surface != NULL){
                //gdk_error_trap_push ();
                surface->unref ();
@@ -171,8 +183,17 @@
 
        delete xaml_loader;
        xaml_loader = NULL;
+       
+       if (plugin_unload)
+               plugin_unload (this);
 }
 
+void
+PluginInstance::SetUnloadCallback (plugin_unload_callback* puc)
+{
+       plugin_unload = puc;
+}
+
 void 
 PluginInstance::Initialize (int argc, char* const argn[], char* const argv[])
 {

Modified: trunk/moon/plugin/plugin.h
===================================================================
--- trunk/moon/plugin/plugin.h  2007-09-21 22:14:09 UTC (rev 86182)
+++ trunk/moon/plugin/plugin.h  2007-09-21 22:50:51 UTC (rev 86183)
@@ -17,7 +17,10 @@
 
 class MoonlightScriptControlObject;
 class PluginXamlLoader;
+class PluginInstance;
 
+typedef void plugin_unload_callback (PluginInstance* plugin);
+
 class PluginInstance
 {
  private:
@@ -41,6 +44,10 @@
        // The XAML loader, contains a handle to a MonoObject *
        //
        PluginXamlLoader* xaml_loader;
+       // 
+       // A (managed) callback to call when the plugin is unloaded.
+       //
+       plugin_unload_callback* plugin_unload;
 
        // The name of the file that we are missing, and we requested to be 
loaded
        char *vm_missing_file;
@@ -54,6 +61,7 @@
  public:       
        PluginInstance (NPP instance, uint16 mode);
        ~PluginInstance ();
+       void SetUnloadCallback (plugin_unload_callback* puc);
        void Initialize (int argc, char* const argn[], char* const argv[]);
        void Finalize ();
 
@@ -181,6 +189,7 @@
 
 void     plugin_html_timer_timeout_stop (PluginInstance *instance, uint32_t 
source_id);
 uint32_t plugin_html_timer_timeout_add (PluginInstance *instance, int32_t 
interval, GSourceFunc callback, gpointer data);
+void     plugin_set_unload_callback (PluginInstance* instance, 
plugin_unload_callback* puc);
 
 G_END_DECLS
 

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to