private IntPtr GetJSContext() { if (_cx != IntPtr.Zero) { _cx = GlobalJSContextHolder.GetJSContextForDomWindow(StyleSheet._DomStyleSheet .GetOwnerNodeAttribute().GetOwnerDocumentAttribute().GetDefaultViewAttribute()); } return(_cx); }
/// <summary> /// Initializes XPCOM using the specified directory. /// </summary> /// <param name="binDirectory">The directory which contains xul.dll.</param> public static void Initialize(string binDirectory) { if (_IsInitialized) { return; } if (BeforeInitalization != null) { BeforeInitalization(); } Interlocked.Exchange(ref _XpcomThreadId, Thread.CurrentThread.ManagedThreadId); if (IsWindows) { Kernel32.SetDllDirectory(binDirectory); } string folder = binDirectory ?? Environment.CurrentDirectory; string xpcomPath = Path.Combine(folder, IsLinux ? "libxul.so" : "xul.dll"); try { // works on windows // but some classes can be not exist on mono (may be) // so make it in another function(stack allocation is making on function start) ReadXulrunnerVersion(xpcomPath); } catch (Exception) { } if (binDirectory != null) { Environment.SetEnvironmentVariable("PATH", Environment.GetEnvironmentVariable("PATH") + ";" + binDirectory, EnvironmentVariableTarget.Process); } object mreAppDir = null; if (binDirectory != null) { using (nsAString str = new nsAString(Path.GetFullPath(binDirectory))) if (NS_NewLocalFile(str, true, out mreAppDir) != 0) { throw new Exception("Failed on NS_NewLocalFile"); } } // temporarily change the current directory so NS_InitEmbedding can find all the DLLs it needs String oldCurrent = Environment.CurrentDirectory; Environment.CurrentDirectory = folder; nsIServiceManager serviceManager; //int res = NS_InitXPCOM2(out serviceManagerPtr, mreAppDir, new DirectoryServiceProvider()); //Note: the error box that this can generate can't be prevented with try/catch, and res is 0 even if it fails //REVIEW: how else can we determine what happened and give a more useful answer, to help new GeckoFX users, //Telling them that probably the version of firefox or xulrunner didn't match this library version? // calling NS_InitXPCOM2 invokes AddRef to the returned nsIServerManager, but as this gets assigned to the __ComObject serviceManager // Release will be called by the when the GC runs __ComObject finaliser. int res = NS_InitXPCOM2(out serviceManager, mreAppDir, null); // change back Environment.CurrentDirectory = oldCurrent; if (res != 0) { throw new Exception("Failed on NS_InitXPCOM2"); } ServiceManager = (nsIServiceManager)serviceManager; // get some global objects we will need later NS_GetComponentManager(out ComponentManager); NS_GetComponentRegistrar(out ComponentRegistrar); if (IsMono) { _comGC = new COMGC(); } // RegisterProvider is necessary to get link styles etc. nsIDirectoryService directoryService = GetService <nsIDirectoryService>("@mozilla.org/file/directory_service;1"); if (directoryService != null) { directoryService.RegisterProvider(new ProfileProvider()); } _IsInitialized = true; GlobalJSContextHolder.Initialize(); // On Linux calling CreateWindowlessBrowser too early crashes with passing null to gdk_window_enable_synchronized_configure() // the gdkwidgets window is null. if (!Xpcom.IsLinux) { InitChromeContext(); } PromptFactoryFactory.Init(); if (AfterInitalization != null) { AfterInitalization(); } }