Example #1
0
        /// <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 DirectoryServiceProvider());
            }

            _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();
            }

            XULAppInfoFactory.Init();
            PromptFactoryFactory.Init();

            if (AfterInitalization != null)
            {
                AfterInitalization();
            }
        }
Example #2
0
        /// <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;
            }

            Debug.WriteLineIf(Thread.CurrentThread.GetApartmentState() != ApartmentState.STA,
                              "Warning: Main Entry point missing [STAThread] attribute.");
            Debug.Assert(Thread.CurrentThread.GetApartmentState() == ApartmentState.STA,
                         "Main Entry point missing [STAThread] attribute.");

            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", string.Format("{0}{1}{2}",
                                                                         Environment.GetEnvironmentVariable("PATH"), Path.PathSeparator, 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 DirectoryServiceProvider());
            }

            _IsInitialized = true;

            // 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();
            }

            XULAppInfoFactory.Init();
            OnProfileStartup();
            PromptFactoryFactory.Init();

            if (AfterInitalization != null)
            {
                AfterInitalization();
            }

            if (Xpcom.IsLinux)
            {
                // Firefox enable offmainthreadcomposition on Linux around May 2015.
                // (see https://mozillagfx.wordpress.com/2015/05/19/off-main-thread-compositing-on-linux/ )
                // However with geckofx on Linux we end up with two Compositors.
                // So we end up with a ClientLayerManager with an uninitalzied mTransactionIdAllocator
                // which causes segfault on Paint.
                GeckoPreferences.User["layers.offmainthreadcomposition.enabled"] = false;
            }
        }