Back to index

lightning-sunbird  0.9+nobinonly
components.cs
Go to the documentation of this file.
00001 using System;
00002 using Mozilla.XPCOM;
00003 using System.Reflection;
00004 using System.Reflection.Emit;
00005 using System.Runtime.InteropServices;
00006 
00007 namespace Mozilla.XPCOM {
00008 
00009 public class Components
00010 {
00011     [DllImport("xpcom-dotnet.so")]
00012     static extern int StartXPCOM(out IntPtr srvmgr);
00013 
00014     static IntPtr serviceManager = IntPtr.Zero;
00015 
00016     public static IntPtr ServiceManager {
00017         get {
00018             return serviceManager;
00019         }
00020     }
00021 
00022     static void RegisterAppDomainHooks()
00023     {
00024         AppDomain.CurrentDomain.TypeResolve += 
00025             new ResolveEventHandler(TypeResolve);
00026         AppDomain.CurrentDomain.AssemblyResolve +=
00027             new ResolveEventHandler(AssemblyResolve);
00028         Console.WriteLine("XPCOM AppDomain hooks registered.");
00029     }
00030 
00031     static Assembly AssemblyResolve(object sender, ResolveEventArgs args)
00032     {
00033         Console.WriteLine("Resolving: {0}", args.Name);
00034         return null;
00035     }
00036 
00037     public static AssemblyBuilder ProxyAssembly {
00038         get {
00039             return ProxyGenerator.ProxyAssembly;
00040         }
00041     }
00042 
00043     static Assembly TypeResolve(object sender, ResolveEventArgs args)
00044     {
00045         if (args.Name.StartsWith("Mozilla.XPCOM.Proxies.")) {
00046             ProxyGenerator gen = new ProxyGenerator(args.Name);
00047             return gen.Generate();
00048         }
00049         
00050 #if NOTYET
00051         if (args.Name.StartsWith("Mozilla.XPCOM.Interfaces.")) {
00052             InterfaceGenerator gen = new InterfaceGenerator(args.Name);
00053             return gen.Generate();
00054         }
00055 #endif
00056 
00057         return null;
00058     }
00059 
00060     public static void Init()
00061     {
00062         if (serviceManager != IntPtr.Zero)
00063             return;
00064 
00065         int res = StartXPCOM(out serviceManager);
00066         if (res != 0) {
00067             throw new Exception(String.Format("StartXPCOM failed: {0:X2}",
00068                                               res));
00069         }
00070 
00071         RegisterAppDomainHooks();
00072     }
00073 
00074     [DllImport("test.so", EntryPoint="GetImpl")]
00075     public static extern IntPtr GetTestImpl();
00076 
00077     public static object CreateInstance(string contractId, Type iface)
00078     {
00079         String typeName = iface.FullName;
00080         String proxyName = typeName.Replace(".Interfaces.", ".Proxies.");
00081         Console.WriteLine("Need proxy class {0} for {1}", proxyName, typeName);
00082         Type proxyType = System.Type.GetType(proxyName);
00083         if (contractId == "@off.net/test-component;1") {
00084             ConstructorInfo ctor = 
00085                 proxyType.GetConstructor(BindingFlags.NonPublic |
00086                                          BindingFlags.Instance,
00087                                          null, new Type[1] { typeof(IntPtr) },
00088                                          null);
00089             return ctor.Invoke(new object[] { GetTestImpl() });
00090         }
00091         return null;
00092     }
00093 }
00094 
00095 }