Ejemplo n.º 1
0
        public static void InitChromeContext()
        {
            if (Xpcom.ChromeContext != null)
            {
                return;
            }

            Xpcom.ChromeContext = new ChromeContext();
        }
Ejemplo n.º 2
0
        public static void Shutdown()
        {
            GeckoPreferences.Shutdown();
            WindowWatcher.Shutdown();
            WindowMediator.Shutdown();

            PromptFactoryFactory.Shutdown();
            Xpcom.DisposeObject(ref _comGC);

            if (Xpcom.ChromeContext != null)
            {
                Xpcom.ChromeContext.Dispose();
                Xpcom.ChromeContext = null;
            }

            if (ComponentRegistrar != null)
            {
                Marshal.ReleaseComObject(ComponentRegistrar);
            }

            if (ComponentManager != null)
            {
                Marshal.ReleaseComObject(ComponentManager);
            }

            if (ServiceManager != null)
            {
                var s = GetService <nsIObserverService>("@mozilla.org/observer-service;1");
                s.NotifyObservers(null, "profile-change-net-teardown", "shutdown-persist");
                s.NotifyObservers(null, "profile-change-teardown", "shutdown-persist");
                s.NotifyObservers(null, "profile-before-change", "shutdown-persist");
                s.NotifyObservers(null, "profile-before-change2", "shutdown-persist");
                Marshal.ReleaseComObject(s);

                // NS_ShutdownXPCOM calls Release on the ServiceManager COM objects.
                // However since the ServiceManager is a __ComObject its finaliser will also call release.
                var ptr = Marshal.GetIUnknownForObject(ServiceManager);
                NS_ShutdownXPCOM(ServiceManager);
                Marshal.ReleaseComObject(ServiceManager);
            }

            _IsInitialized = false;
        }
Ejemplo n.º 3
0
        public static void Shutdown()
        {
            GeckoPreferences.Shutdown();
            WindowWatcher.Shutdown();
            WindowMediator.Shutdown();

            PromptFactoryFactory.Shutdown();
            Xpcom.DisposeObject(ref _comGC);

            if (Xpcom.ChromeContext != null)
            {
                Xpcom.ChromeContext.Dispose();
                Xpcom.ChromeContext = null;
            }

            if (ComponentRegistrar != null)
            {
                Marshal.ReleaseComObject(ComponentRegistrar);
            }

            if (ComponentManager != null)
            {
                Marshal.ReleaseComObject(ComponentManager);
            }

            if (ServiceManager != null)
            {
                // NS_ShutdownXPCOM calls Release on the ServiceManager COM objects.
                // However since the ServiceManager is a __ComObject its finaliser will also call release.
                var ptr = Marshal.GetIUnknownForObject(ServiceManager);
                NS_ShutdownXPCOM(ServiceManager);
                Marshal.ReleaseComObject(ServiceManager);
            }

            _IsInitialized = false;
        }
Ejemplo n.º 4
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 ProfileProvider());
            }

            _IsInitialized = true;
            GlobalJSContextHolder.Initialize();

            Xpcom.ChromeContext = new ChromeContext();

            PromptFactoryFactory.Init();

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