Project: Eclipse_Platform_UI
Bug 256801 [Browser] Closing internal browser causing ClassCastException
Reported: 2008-11-27 11:05:09 | Status: resolved fixed

Build ID: 3.4 release Steps To Reproduce: I have created a menu item to launch the same web page using the web browser. The way that I launch the web browser is by calling: org.eclipse.ui.PlatformUI.getWorkbench().getBrowserSupport().createBrowser(id) to create the browser and then launch the url from there. I always pass in the same "id" when I call this method so that I can reuse the same browser everytime when I launch the same web page. The problem occurs when the user go through the following steps: 1) Launch the web page using the menu item(the default is to launch it in an embedded browser). Do NOT close it. 3) Go to Windows->Preferences, then General->Web Browser. Change the default to use external Web browser and then select Internet Explorer. Click OK. 4) Launch the web page using the menu item again. The external browser will be launched successfully. 5) Now close the internal web browser that was launched in step 3. An error dialog pops up and said "An error has occurred. See error log for more details." In the log, you'll see: java.lang.ClassCastException: org.eclipse.ui.internal.browser.ExternalBrowserInstance incompatible with java.util.HashMap at org.eclipse.ui.internal.browser.DefaultBrowserSupport.removeBrowser(Unknown Source) at org.eclipse.ui.internal.browser.InternalBrowserInstance$1.partClosed(Unknown Source) at org.eclipse.ui.internal.PartListenerList$3.run(Unknown Source) at org.eclipse.core.runtime.SafeRunner.run(Unknown Source) at org.eclipse.core.runtime.Platform.run(Unknown Source) at org.eclipse.ui.internal.PartListenerList.fireEvent(Unknown Source) at org.eclipse.ui.internal.PartListenerList.firePartClosed(Unknown Source) at org.eclipse.ui.internal.PartService.firePartClosed(Unknown Source) at org.eclipse.ui.internal.WorkbenchPagePartList.firePartClosed(Unknown Source) at org.eclipse.ui.internal.PartList.partClosed(Unknown Source) The eclipse framework should handle closing the internal browser properly after the user has changed the browser option when the same id is being used. More information: The problem only happens when the same id is being used for launching the embedded(internal) and external browser. The problem with that is as follows: Initially when an embedded browser is created, the following code in eclipse's DefaultBrowserSupport.class creates a HashMap and is put in 'browserIdMap' with the browserId as the key: if (webBrowser instanceof InternalBrowserInstance) { // we should only share internal browsers within one // workbench window. Each workbench window can have // a shared browser with the same id IWorkbenchWindow window = PlatformUI.getWorkbench() .getActiveWorkbenchWindow(); Integer key = getWindowKey(window); HashMap wmap = (HashMap) browserIdMap.get(browserId); if (wmap == null) { wmap = new HashMap(); browserIdMap.put(browserId, wmap); } wmap.put(key, webBrowser); } else { // external and system browsers are shared // for the entire workbench browserIdMap.put(browserId, webBrowser); } When an external browser is created, the same code replaces the entry (HashMap) in 'browserIdmap' with an actual web browser instance (ExternalBrowserInstance) because of the same browserId Then when the embedded browser is being closed, the following code tries to update 'browserIdMap': protected void removeBrowser(IWebBrowser browser) { String baseId = WebBrowserUtil.decodeId(browser.getId()); if (browser instanceof InternalBrowserInstance) { // Remove it from the window map and // also remove the window map itself if it is empty. Integer key = ((InternalBrowserInstance) browser).getWindowKey(); HashMap wmap = (HashMap) browserIdMap.get(baseId); if (wmap != null) { wmap.remove(key); if (wmap.isEmpty()) browserIdMap.remove(baseId); } } else browserIdMap.remove(baseId); } The internal browser is being closed, but the entry returned from 'browserIdMap' is actually the ExternalBrowserInstance, and therefore a ClassCastException is thrown.

Files changed in fix:
bundles/org.eclipse.ui.browser/src/org/eclipse/ui/internal/browser/DefaultBrowserSupport.java