diff dwt/browser/Mozilla.d @ 45:d8635bb48c7c

Merge with SWT 3.5
author Jacob Carlborg <doob@me.com>
date Mon, 01 Dec 2008 17:07:00 +0100
parents db5a898b2119
children 5583f8eeee6c
line wrap: on
line diff
--- a/dwt/browser/Mozilla.d	Tue Oct 21 15:20:04 2008 +0200
+++ b/dwt/browser/Mozilla.d	Mon Dec 01 17:07:00 2008 +0100
@@ -1,5 +1,5 @@
-/*******************************************************************************
- * Copyright (c) 2003, 2007 IBM Corporation and others.
+/*******************************************************************************
+ * Copyright (c) 2003, 2008 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -29,9 +29,7 @@
 import dwt.internal.Compatibility;
 import dwt.internal.LONG;
 import dwt.internal.Library;
-import dwt.internal.mozilla.GREVersionRange;
 import dwt.internal.mozilla.XPCOM;
-import dwt.internal.mozilla.XPCOMInit;
 import dwt.internal.mozilla.XPCOMObject;
 import dwt.internal.mozilla.nsEmbedString;
 import dwt.internal.mozilla.nsIAppShell;
@@ -83,6 +81,8 @@
 import dwt.internal.mozilla.nsIWebProgress;
 import dwt.internal.mozilla.nsIWebProgressListener;
 import dwt.internal.mozilla.nsIWindowWatcher;
+import dwt.internal.mozilla.init.GREVersionRange;
+import dwt.internal.mozilla.init.XPCOMInit;
 import dwt.layout.FillLayout;
 import dwt.widgets.Composite;
 import dwt.widgets.Display;
@@ -223,7 +223,7 @@
 
 public void create (Composite parent, int style) {
     delegate = new MozillaDelegate (browser);
-    Display display = parent.getDisplay ();
+    final Display display = parent.getDisplay ();
 
     int /*long*/[] result = new int /*long*/[1];
     if (!Initialized) {
@@ -332,10 +332,37 @@
                     byte[] path = MozillaDelegate.wcsToMbcs (null, mozillaPath, true);
                     rc = XPCOMInit.XPCOMGlueStartup (path);
                     if (rc !is XPCOM.NS_OK) {
-                        IsXULRunner = false;    /* failed */
                         mozillaPath = mozillaPath.substring (0, mozillaPath.lastIndexOf cast(SEPARATOR_OS));
                         if (Device.DEBUG) System.out.println ("cannot use detected XULRunner: " + mozillaPath); //$NON-NLS-1$
-                    } else {
+
+                        /* attempt to XPCOMGlueStartup the GRE pointed at by MOZILLA_FIVE_HOME */
+                        int /*long*/ ptr = C.getenv (MozillaDelegate.wcsToMbcs (null, XPCOM.MOZILLA_FIVE_HOME, true));
+                        if (ptr is 0) {
+                            IsXULRunner = false;
+                        } else {
+                            length = C.strlen (ptr);
+                            byte[] buffer = new byte[length];
+                            C.memmove (buffer, ptr, length);
+                            mozillaPath = new String (MozillaDelegate.mbcsToWcs (null, buffer));
+                            /*
+                             * Attempting to XPCOMGlueStartup a mozilla-based GRE !is xulrunner can
+                             * crash, so don't attempt unless the GRE appears to be xulrunner.
+                             */
+                            if (mozillaPath.indexOf("xulrunner") is -1) { //$NON-NLS-1$
+                                IsXULRunner = false;    
+                            } else {
+                                mozillaPath += SEPARATOR_OS + delegate.getLibraryName ();
+                                path = MozillaDelegate.wcsToMbcs (null, mozillaPath, true);
+                                rc = XPCOMInit.XPCOMGlueStartup (path);
+                                if (rc !is XPCOM.NS_OK) {
+                                    IsXULRunner = false;
+                                    mozillaPath = mozillaPath.substring (0, mozillaPath.lastIndexOf (SEPARATOR_OS));
+                                    if (Device.DEBUG) System.out.println ("failed to start as XULRunner: " + mozillaPath); //$NON-NLS-1$
+                                }
+                            }
+                        } 
+                    }
+                    if (IsXULRunner) {
                         XPCOMInitWasGlued = true;
                     }
                 }
@@ -940,17 +967,6 @@
         }
         factory.Release ();
         
-        HelperAppLauncherDialogFactory dialogFactory = new HelperAppLauncherDialogFactory ();
-        dialogFactory.AddRef ();
-        aContractID = MozillaDelegate.wcsToMbcs (null, XPCOM.NS_HELPERAPPLAUNCHERDIALOG_CONTRACTID, true);
-        aClassName = MozillaDelegate.wcsToMbcs (null, "Helper App Launcher Dialog", true); //$NON-NLS-1$
-        rc = componentRegistrar.RegisterFactory (XPCOM.NS_HELPERAPPLAUNCHERDIALOG_CID, aClassName, aContractID, dialogFactory.getAddress ());
-        if (rc !is XPCOM.NS_OK) {
-            browser.dispose ();
-            error (rc);
-        }
-        dialogFactory.Release ();
-
         /*
         * This Download factory will be used if the GRE version is < 1.8.
         * If the GRE version is 1.8.x then the Download factory that is registered later for
@@ -1046,7 +1062,17 @@
                 serviceManager.Release ();
 
                 if (XPCOMWasGlued) {
-                    XPCOM.XPCOMGlueShutdown ();
+                    /*
+                    * XULRunner 1.9 can crash on Windows if XPCOMGlueShutdown is invoked here,
+                    * presumably because one or more of its unloaded symbols are referenced when
+                    * this callback returns.  The workaround is to delay invoking XPCOMGlueShutdown
+                    * so that its symbols are still available once this callback returns.
+                    */
+                    display.asyncExec (new Runnable () {
+                        public void run () {
+                            XPCOM.XPCOMGlueShutdown ();
+                        }
+                    });
                     XPCOMWasGlued = false;
                 }
                 if (XPCOMInitWasGlued) {
@@ -1134,6 +1160,29 @@
     if (!PerformedVersionCheck) {
         PerformedVersionCheck = true;
 
+        rc = componentManager.QueryInterface (nsIComponentRegistrar.NS_ICOMPONENTREGISTRAR_IID, result);
+        if (rc !is XPCOM.NS_OK) {
+            browser.dispose ();
+            error (rc);
+        }
+        if (result[0] is 0) {
+            browser.dispose ();
+            error (XPCOM.NS_NOINTERFACE);
+        }
+        nsIComponentRegistrar componentRegistrar = new nsIComponentRegistrar (result[0]);
+        result[0] = 0;
+
+        HelperAppLauncherDialogFactory dialogFactory = new HelperAppLauncherDialogFactory ();
+        dialogFactory.AddRef ();
+        byte[] aContractID = MozillaDelegate.wcsToMbcs (null, XPCOM.NS_HELPERAPPLAUNCHERDIALOG_CONTRACTID, true);
+        byte[] aClassName = MozillaDelegate.wcsToMbcs (null, "Helper App Launcher Dialog", true); //$NON-NLS-1$
+        rc = componentRegistrar.RegisterFactory (XPCOM.NS_HELPERAPPLAUNCHERDIALOG_CID, aClassName, aContractID, dialogFactory.getAddress ());
+        if (rc !is XPCOM.NS_OK) {
+            browser.dispose ();
+            error (rc);
+        }
+        dialogFactory.Release ();
+
         /*
         * Check for the availability of the pre-1.8 implementation of nsIDocShell
         * to determine if the GRE's version is < 1.8.
@@ -1171,28 +1220,17 @@
             if (rc is XPCOM.NS_OK && result[0] !is 0) { /* 1.8 */
                 new nsISupports (result[0]).Release ();
                 result[0] = 0;
-                rc = componentManager.QueryInterface (nsIComponentRegistrar.NS_ICOMPONENTREGISTRAR_IID, result);
-                if (rc !is XPCOM.NS_OK) {
-                    browser.dispose ();
-                    error (rc);
-                }
-                if (result[0] is 0) {
-                    browser.dispose ();
-                    error (XPCOM.NS_NOINTERFACE);
-                }
 
-                nsIComponentRegistrar componentRegistrar = new nsIComponentRegistrar (result[0]);
                 DownloadFactory_1_8 downloadFactory_1_8 = new DownloadFactory_1_8 ();
                 downloadFactory_1_8.AddRef ();
-                byte[] aContractID = MozillaDelegate.wcsToMbcs (null, XPCOM.NS_TRANSFER_CONTRACTID, true);
-                byte[] aClassName = MozillaDelegate.wcsToMbcs (null, "Transfer", true); //$NON-NLS-1$
+                aContractID = MozillaDelegate.wcsToMbcs (null, XPCOM.NS_TRANSFER_CONTRACTID, true);
+                aClassName = MozillaDelegate.wcsToMbcs (null, "Transfer", true); //$NON-NLS-1$
                 rc = componentRegistrar.RegisterFactory (XPCOM.NS_DOWNLOAD_CID, aClassName, aContractID, downloadFactory_1_8.getAddress ());
                 if (rc !is XPCOM.NS_OK) {
                     browser.dispose ();
                     error (rc);
                 }
                 downloadFactory_1_8.Release ();
-                componentRegistrar.Release ();
             } else { /* >= 1.9 */
                 /*
                  * Bug in XULRunner 1.9.  Mozilla no longer clears its background before initial content has
@@ -1217,10 +1255,13 @@
                 ABOUT_BLANK.getChars (0, ABOUT_BLANK.length (), uri, 0);
                 rc = webNavigation.LoadURI (uri, nsIWebNavigation.LOAD_FLAGS_NONE, 0, 0, 0);
                 webNavigation.Release ();
+
+                dialogFactory.isPre_1_9 = false;
             }
         }
         result[0] = 0;
         interfaceRequestor.Release ();
+        componentRegistrar.Release ();
     }
     componentManager.Release ();
 
@@ -1496,6 +1537,51 @@
     return MozillaDelegate.findBrowser (handle);
 }
 
+static Browser findBrowser (nsIDOMWindow aDOMWindow) {
+    int /*long*/[] result = new int /*long*/[1];
+    int rc = XPCOM.NS_GetServiceManager (result);
+    if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
+    if (result[0] is 0) Mozilla.error (XPCOM.NS_NOINTERFACE);
+
+    nsIServiceManager serviceManager = new nsIServiceManager (result[0]);
+    result[0] = 0;
+    byte[] aContractID = MozillaDelegate.wcsToMbcs (null, XPCOM.NS_WINDOWWATCHER_CONTRACTID, true);
+    rc = serviceManager.GetServiceByContractID (aContractID, nsIWindowWatcher.NS_IWINDOWWATCHER_IID, result);
+    if (rc !is XPCOM.NS_OK) Mozilla.error(rc);
+    if (result[0] is 0) Mozilla.error (XPCOM.NS_NOINTERFACE);       
+    serviceManager.Release ();
+
+    nsIWindowWatcher windowWatcher = new nsIWindowWatcher (result[0]);
+    result[0] = 0;
+    /* the chrome will only be answered for the top-level nsIDOMWindow */
+    rc = aDOMWindow.GetTop (result);
+    if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
+    if (result[0] is 0) Mozilla.error (XPCOM.NS_NOINTERFACE);
+    int /*long*/ topDOMWindow = result[0];
+    result[0] = 0;
+    rc = windowWatcher.GetChromeForWindow (topDOMWindow, result);
+    if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
+    if (result[0] is 0) Mozilla.error (XPCOM.NS_NOINTERFACE);
+    new nsISupports (topDOMWindow).Release ();
+    windowWatcher.Release ();   
+
+    nsIWebBrowserChrome webBrowserChrome = new nsIWebBrowserChrome (result[0]);
+    result[0] = 0;
+    rc = webBrowserChrome.QueryInterface (nsIEmbeddingSiteWindow.NS_IEMBEDDINGSITEWINDOW_IID, result);
+    if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
+    if (result[0] is 0) Mozilla.error (XPCOM.NS_NOINTERFACE);       
+    webBrowserChrome.Release ();
+
+    nsIEmbeddingSiteWindow embeddingSiteWindow = new nsIEmbeddingSiteWindow (result[0]);
+    result[0] = 0;
+    rc = embeddingSiteWindow.GetSiteWindow (result);
+    if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
+    if (result[0] is 0) Mozilla.error (XPCOM.NS_NOINTERFACE);       
+    embeddingSiteWindow.Release ();
+
+    return findBrowser (result[0]); 
+}
+
 public bool forward () {
     if (awaitingNavigate) return false;
 
@@ -1511,6 +1597,10 @@
     return rc is XPCOM.NS_OK;
 }
 
+public String getBrowserType () {
+    return "mozilla"; //$NON-NLS-1$
+}
+
 public String getText () {
     if (awaitingNavigate) return ""; //$NON-NLS-1$
 
@@ -2445,7 +2535,7 @@
 
 int SetWebBrowser (int /*long*/ aWebBrowser) {
     if (webBrowser !is null) webBrowser.Release ();
-    webBrowser = aWebBrowser !is 0 ? new nsIWebBrowser (aWebBrowser) : null;                 
+    webBrowser = aWebBrowser !is 0 ? new nsIWebBrowser (aWebBrowser) : null;                
     return XPCOM.NS_OK;
 }
    
@@ -2803,7 +2893,7 @@
             /* First try to use the nsIWebNavigationInfo if it's available (>= mozilla 1.8) */
             byte[] aContractID = MozillaDelegate.wcsToMbcs (null, XPCOM.NS_WEBNAVIGATIONINFO_CONTRACTID, true);
             rc = serviceManager.GetServiceByContractID (aContractID, nsIWebNavigationInfo.NS_IWEBNAVIGATIONINFO_IID, result);
-            if (rc is 0) {
+            if (rc is XPCOM.NS_OK) {
                 byte[] bytes = MozillaDelegate.wcsToMbcs (null, contentType, true);
                 int /*long*/ typePtr = XPCOM.nsEmbedCString_new (bytes, bytes.length);
                 nsIWebNavigationInfo info = new nsIWebNavigationInfo (result[0]);