public void JavaScriptToCSharpCallBack()
        {
            // Note: Firefox 17 removed enablePrivilege #546848 - refactored test so that javascript to create "@mozillazine.org/example/priority;1" is now executated by AutoJsContext

            // Register a C# COM Object

            const string          ComponentManagerCID = "91775d60-d5dc-11d2-92fb-00e09805570f";
            nsIComponentRegistrar mgr = (nsIComponentRegistrar)Xpcom.GetObjectForIUnknown((IntPtr)Xpcom.GetService(new Guid(ComponentManagerCID)));
            Guid aClass = new Guid("a7139c0e-962c-44b6-bec3-aaaaaaaaaaab");

            mgr.RegisterFactory(ref aClass, "Example C sharp com component", "@geckofx/mysharpclass;1", new MyCSharpComClassFactory());

            // In order to use Components.classes etc we need to enable certan privileges.
            GeckoPreferences.User["capability.principal.codebase.p0.granted"]     = "UniversalXPConnect";
            GeckoPreferences.User["capability.principal.codebase.p0.id"]          = "file://";
            GeckoPreferences.User["capability.principal.codebase.p0.subjectName"] = "";
            GeckoPreferences.User["security.fileuri.strict_origin_policy"]        = false;

#if PORT
            browser.JavascriptError += (x, w) => Console.WriteLine(w.Message);
#endif

            string inithtml = "<html><body></body></html>";

            string initialjavascript =
                "var myClassInstance = Components.classes['@geckofx/mysharpclass;1'].createInstance(Components.interfaces.nsICommandHandler); myClassInstance.exec('hello', 'world');";

            // Create temp file to load
            var tempfilename = Path.GetTempFileName();
            tempfilename += ".html";
            using (TextWriter tw = new StreamWriter(tempfilename))
            {
                tw.WriteLine(inithtml);
                tw.Close();
            }

            browser.Navigate(tempfilename);
            browser.NavigateFinishedNotifier.BlockUntilNavigationFinished();
            File.Delete(tempfilename);

            using (var context = new AutoJSContext(GlobalJSContextHolder.BackstageJSContext))
            {
                string result  = String.Empty;
                bool   success = context.EvaluateScript(initialjavascript, out result);
                Console.WriteLine("success = {1} result = {0}", result, success);
            }

            // Test the results
            Assert.AreEqual(MyCSharpComClass._execCount, 1);
            Assert.AreEqual(MyCSharpComClass._aCommand, "hello");
            Assert.AreEqual(MyCSharpComClass._aParameters, "world");
        }
Beispiel #2
0
 /// <summary>
 /// Registers a factory to be used to instantiate a particular class identified by ClassID, and creates an association of class name and ContractID with the class.
 /// </summary>
 /// <param name="classID">The ClassID of the class being registered.</param>
 /// <param name="className">The name of the class being registered. This value is intended as a human-readable name for the class and need not be globally unique.</param>
 /// <param name="contractID">The ContractID of the class being registered.</param>
 /// <param name="factory">The nsIFactory instance of the class being registered.</param>
 public static void RegisterFactory(Guid classID, string className, string contractID, nsIFactory factory)
 {
     ComponentRegistrar.RegisterFactory(ref classID, className, contractID, factory);
 }