예제 #1
0
        public static void Shutdown()
        {
            if (!_IsInitialized)
            {
                return;
            }

            _IsInitialized = false;

            ComponentManager   = null;
            ComponentRegistrar = null;

            NS_ShutdownXPCOM(Marshal.GetIUnknownForObject(ServiceManager));
            ServiceManager = null;

            XULAppInfo.Shutdown();
            GeckoWebBrowser.WindowCreator.Shutdown();
        }
예제 #2
0
        /// <summary>
        /// Initializes XPCOM using the specified GeckoAppInfo.
        /// </summary>
        public static void Initialize(GeckoAppInfo geckoInfo)
        {
            if (_IsInitialized)
            {
                return;
            }

            string folder       = geckoInfo.GeckoPath;
            string binDirectory = (folder != Application.StartupPath) ? folder : null;
            string xpcomPath    = Path.Combine(folder, "xpcom.dll");

            if (Debugger.IsAttached)
            {
                // make sure this DLL is there
                if (!File.Exists(xpcomPath))
                {
                    if (MessageBox.Show(String.Format("Couldn't find XULRunner in '{0}'. Call Xpcom.Initialize() in your application startup code and specify the directory where XULRunner is installed.{1}{1}If you do not have XULRunner installed, click Yes to open the download page.  Otherwise, click No, and update your application startup code.",
                                                      folder,
                                                      Environment.NewLine),
                                        "XULRunner Not Found", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes)
                    {
                        Process.Start("http://releases.mozilla.org/pub/mozilla.org/xulrunner/releases/");
                    }

                    Environment.Exit(0);
                }
            }

            if (binDirectory != null)
            {
                Environment.SetEnvironmentVariable("path",
                                                   String.Format("{0};{1}", Environment.GetEnvironmentVariable("path"), binDirectory), EnvironmentVariableTarget.Process);
            }

            object mreAppDir = null;

            if (binDirectory != null)
            {
                using (nsACString str = new nsACString(Path.GetFullPath(binDirectory)))
                    if (NS_NewNativeLocalFile(str, true, out mreAppDir) != 0)
                    {
                        throw new Exception("Failed on NS_NewNativeLocalFile");
                    }
            }

            // temporarily change the current directory so NS_InitEmbedding can find all the DLLs it needs
            String oldCurrent = Environment.CurrentDirectory;

            Environment.CurrentDirectory = folder;

            IntPtr serviceManagerPtr;
            //int res = NS_InitXPCOM2(out serviceManagerPtr, mreAppDir, new DirectoryServiceProvider());
            int res = NS_InitXPCOM2(out serviceManagerPtr, mreAppDir, null);

            // change back
            Environment.CurrentDirectory = oldCurrent;

            if (res != 0)
            {
                throw new Exception("Failed on NS_InitXPCOM2");
            }

            // ok, we initialized xpcom, most like we will work, so we need gecko version from now on
            geckoAppInfo = geckoInfo;

            ServiceManager = (nsIServiceManager)Marshal.GetObjectForIUnknown(serviceManagerPtr);

            // get some global objects we will need later
            NS_GetComponentManager(out ComponentManager);
            NS_GetComponentRegistrar(out ComponentRegistrar);

            // a bug in Mozilla 1.8 (https://bugzilla.mozilla.org/show_bug.cgi?id=309877) causes the PSM to
            // crash when loading a site over HTTPS.  in order to work around this bug, we must register an nsIDirectoryServiceProvider
            // which will provide the location of a profile
            nsIDirectoryService directoryService = GetService <nsIDirectoryService>("@mozilla.org/file/directory_service;1");

            directoryService.RegisterProvider(new ProfileProvider());

            XULAppInfo.Initialize();

            _IsInitialized = true;
        }