Bug 2406 - Preview window sometimes is completely blank.
authorEugene Ostroukhov <eugeneo@symbian.org>
Mon, 29 Mar 2010 15:44:29 -0700
changeset 288 351bff9a265b
parent 287 acfa94f7e231
child 289 75070492d7b2
Bug 2406 - Preview window sometimes is completely blank.
org.symbian.tools.wrttools.previewer/icons/obj16/clearpreferences.gif
org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/Images.java
org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/PreviewerPlugin.java
org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/preview/AbstractPreviewPage.java
org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/preview/MozillaPreviewPage.java
Binary file org.symbian.tools.wrttools.previewer/icons/obj16/clearpreferences.gif has changed
--- a/org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/Images.java	Mon Mar 29 13:59:08 2010 -0700
+++ b/org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/Images.java	Mon Mar 29 15:44:29 2010 -0700
@@ -4,4 +4,5 @@
 	String RED_SYNC = "/icons/obj16/refresh_red.gif";
 	String YELLOW_SYNC = "/icons/obj16/refresh.gif";
 	String GREEN_SYNC = "/icons/obj16/refresh_green.gif";
+    String CLEAR_PREFS = "/icons/obj16/clearpreferences.gif";
 }
--- a/org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/PreviewerPlugin.java	Mon Mar 29 13:59:08 2010 -0700
+++ b/org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/PreviewerPlugin.java	Mon Mar 29 15:44:29 2010 -0700
@@ -79,6 +79,7 @@
 		addImage(reg, Images.GREEN_SYNC);
 		addImage(reg, Images.RED_SYNC);
 		addImage(reg, Images.YELLOW_SYNC);
+        addImage(reg, Images.CLEAR_PREFS);
 	}
 	
 	private void addImage(ImageRegistry reg, String path) {
--- a/org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/preview/AbstractPreviewPage.java	Mon Mar 29 13:59:08 2010 -0700
+++ b/org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/preview/AbstractPreviewPage.java	Mon Mar 29 15:44:29 2010 -0700
@@ -33,7 +33,7 @@
 		};
 	};
 
-	private final IProject project;
+    protected final IProject project;
 	private Browser browser;
 	private boolean toggleState = true;
 	private final PreviewView previewView;
@@ -137,6 +137,9 @@
 	public void init(IPageSite pageSite) {
 		super.init(pageSite);
 		IToolBarManager toolBar = pageSite.getActionBars().getToolBarManager();
+
+        contributeToToolbar(toolBar);
+
 		refreshAction.setImageDescriptor(PreviewerPlugin.getImageDescriptor(Images.GREEN_SYNC));
 		refreshAction.setToolTipText("Refresh the preview browser");
 		toolBar.add(refreshAction);
@@ -152,7 +155,11 @@
 		getSite().setSelectionProvider(this);
 	}
 
-	private String getToggleActionTooltip() {
+    protected void contributeToToolbar(IToolBarManager toolBar) {
+        // Do nothing here
+    }
+
+    private String getToggleActionTooltip() {
 		return toggleState ? "Disable preview autorefresh" : "Enable preview autorefresh";
 	}
 
--- a/org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/preview/MozillaPreviewPage.java	Mon Mar 29 13:59:08 2010 -0700
+++ b/org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/preview/MozillaPreviewPage.java	Mon Mar 29 15:44:29 2010 -0700
@@ -9,149 +9,212 @@
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.runtime.FileLocator;
 import org.eclipse.core.runtime.Platform;
+import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.IToolBarManager;
+import org.eclipse.jface.action.Separator;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.browser.Browser;
 import org.eclipse.swt.widgets.Composite;
+import org.mozilla.interfaces.nsICookie;
+import org.mozilla.interfaces.nsICookieManager;
 import org.mozilla.interfaces.nsIIOService;
 import org.mozilla.interfaces.nsIIOService2;
 import org.mozilla.interfaces.nsIPrefBranch;
 import org.mozilla.interfaces.nsIServiceManager;
+import org.mozilla.interfaces.nsISimpleEnumerator;
 import org.mozilla.xpcom.Mozilla;
 import org.osgi.framework.Bundle;
+import org.symbian.tools.wrttools.previewer.Images;
 import org.symbian.tools.wrttools.previewer.PreviewerPlugin;
 import org.symbian.tools.wrttools.previewer.http.WebAppInterface;
 import org.symbian.tools.wrttools.previewer.http.WebappManager;
 
 public class MozillaPreviewPage extends AbstractPreviewPage {
-	public static final String XUL_RUNNER_BUNDLE = "org.mozilla.xulrunner";
-	
-	private static final String XUL_RUNNER_PATH_PARAMETER = "org.eclipse.swt.browser.XULRunnerPath";
-	private nsIPrefBranch mozillaPrefs;
+    public class ClearPreferencesAction extends Action {
+        public ClearPreferencesAction() {
+            setText("Clear");
+            setImageDescriptor(PreviewerPlugin.getImageDescriptor(Images.CLEAR_PREFS));
+            setDescription("Forget all WRT application preference values");
+            setToolTipText(getDescription());
+        }
+
+        @Override
+        public void run() {
+            clearPreferences();
+        }
+    }
+
+    public static final String XUL_RUNNER_BUNDLE = "org.mozilla.xulrunner";
 
-	public MozillaPreviewPage(IProject project, PreviewView previewView) {
-		super(project, previewView);
-	}
+    private static final String XUL_RUNNER_PATH_PARAMETER = "org.eclipse.swt.browser.XULRunnerPath";
+    private nsIPrefBranch mozillaPrefs;
+
+    public MozillaPreviewPage(IProject project, PreviewView previewView) {
+        super(project, previewView);
+    }
 
+    public void clearPreferences() {
+        nsIServiceManager servMgr = null;
+        try {
+            servMgr = Mozilla.getInstance().getServiceManager();
+            if (servMgr != null) {
+
+                nsICookieManager cookieManager = (nsICookieManager) servMgr.getServiceByContractID(
+                        "@mozilla.org/cookiemanager;1", nsICookieManager.NS_ICOOKIEMANAGER_IID);
+                nsISimpleEnumerator enumerator = cookieManager.getEnumerator();
 
-	private synchronized void initMozilla() {
-		if (System.getProperty(XUL_RUNNER_PATH_PARAMETER) == null) {
-			Bundle bundle = Platform.getBundle(XUL_RUNNER_BUNDLE); //$NON-NLS-1$
-			if (bundle != null) {
-				URL resourceUrl = bundle.getResource("xulrunner"); //$NON-NLS-1$
-				if (resourceUrl != null) {
-					try {
-						URL fileUrl = FileLocator.toFileURL(resourceUrl);
-						File file = new File(fileUrl.getPath());
-						System.setProperty(XUL_RUNNER_PATH_PARAMETER, file
-								.getAbsolutePath()); //$NON-NLS-1$
-					} catch (IOException e) {
-						PreviewerPlugin.log(e);
-					}
-				}
-			}
-		}
-	}
+                boolean needsRefresh = false;
+                while (enumerator.hasMoreElements()) {
+                    nsICookie cookie = (nsICookie) enumerator.getNext().queryInterface(nsICookie.NS_ICOOKIE_IID);
+                    if (isRelevantPreference(cookie.getName())) {
+                        cookieManager.remove(cookie.getHost(), cookie.getName(), cookie.getPath(), false);
+                        needsRefresh = true;
+                    }
+                }
+                if (needsRefresh) {
+                    refresh(true);
+                }
+            }
+        } catch (Exception x) {
+            PreviewerPlugin.log(x);
+            // known to throw NullPointException on Mac OS when you're not using 
+            // Mozilla. We don't want to pollute the error log with this
+            return;
+        }
+    }
 
-	@Override
-	protected Browser createBrowser(Composite parent) {
-		initMozilla();
-		final Browser browser = new Browser(parent, SWT.MOZILLA);
-		bypassSameOriginPolicy();
-		applyProxySettings();
-		return browser;
-	}
-	
-	private void applyProxySettings() {
-		IProxyService px = PreviewerPlugin.getDefault().getProxyService();
-		if(px != null){			 
-			boolean proxyEnabled = px.isProxiesEnabled();
+    private boolean isRelevantPreference(String path) {
+        if (path != null && path.matches("Nokia_WRT#/preview/.*/preview-frame.html#.*")) {
+            String key = path.substring(path.lastIndexOf("#") + 1);
+            return !"_WRT_VERSION".equals(key) && !"NOKIA_EMULATOR_DEVICE_MODE".equals(key)
+                    && !"NOKIA_EMULATOR_DEVICE".equals(key);
+        }
+        return false;
+    }
+
+    private synchronized void initMozilla() {
+        if (System.getProperty(XUL_RUNNER_PATH_PARAMETER) == null) {
+            Bundle bundle = Platform.getBundle(XUL_RUNNER_BUNDLE);
+            if (bundle != null) {
+                URL resourceUrl = bundle.getResource("xulrunner"); //$NON-NLS-1$
+                if (resourceUrl != null) {
+                    try {
+                        URL fileUrl = FileLocator.toFileURL(resourceUrl);
+                        File file = new File(fileUrl.getPath());
+                        System.setProperty(XUL_RUNNER_PATH_PARAMETER, file.getAbsolutePath());
+                    } catch (IOException e) {
+                        PreviewerPlugin.log(e);
+                    }
+                }
+            }
+        }
+    }
+
+    @Override
+    protected Browser createBrowser(Composite parent) {
+        initMozilla();
+        final Browser browser = new Browser(parent, SWT.MOZILLA);
+        bypassSameOriginPolicy();
+        applyProxySettings();
+        return browser;
+    }
 
-			boolean systemProxy = px.isSystemProxiesEnabled();
-			if( proxyEnabled && !systemProxy){
-				IProxyData pd = px.getProxyData(IProxyData.HTTP_PROXY_TYPE);
-				if (pd !=null &&mozillaPrefs != null) {				
-					String host= pd.getHost();
-					int port = pd.getPort();
-					if(host !=null && port != -1){
-						mozillaPrefs.setIntPref("network.proxy.type", 1);
-						mozillaPrefs.setCharPref("network.proxy.http", host);
-						mozillaPrefs.setIntPref("network.proxy.http_port", port);
-					}
-				}
-			 }
-		}
-		else{
-			 Exception e= new Exception();
-			 PreviewerPlugin.log("Proxy service returned null", e);
-		 }
-	}
-	
-	private void bypassSameOriginPolicy() {
-		WebAppInterface.getInstance();
-		try{
-			nsIServiceManager servMgr = null;
-			try {
-				servMgr = Mozilla.getInstance().getServiceManager();
-				if (servMgr == null) return;
-			} catch (Exception x) {
-				PreviewerPlugin.log(x);
-				// known to throw NullPointException on Mac OS when you're not using 
-				// Mozilla. We don't want to pollute the error log with this
-				return;
-			}
+    private void applyProxySettings() {
+        IProxyService px = PreviewerPlugin.getDefault().getProxyService();
+        if (px != null) {
+            boolean proxyEnabled = px.isProxiesEnabled();
+
+            boolean systemProxy = px.isSystemProxiesEnabled();
+            if (proxyEnabled && !systemProxy) {
+                IProxyData pd = px.getProxyData(IProxyData.HTTP_PROXY_TYPE);
+                if (pd != null && mozillaPrefs != null) {
+                    String host = pd.getHost();
+                    int port = pd.getPort();
+                    if (host != null && port != -1) {
+                        mozillaPrefs.setIntPref("network.proxy.type", 1);
+                        mozillaPrefs.setCharPref("network.proxy.http", host);
+                        mozillaPrefs.setIntPref("network.proxy.http_port", port);
+                    }
+                }
+            }
+        } else {
+            Exception e = new Exception();
+            PreviewerPlugin.log("Proxy service returned null", e);
+        }
+    }
 
-			
-			mozillaPrefs = (nsIPrefBranch) servMgr.getServiceByContractID(
-											"@mozilla.org/preferences-service;1", 
-											nsIPrefBranch.NS_IPREFBRANCH_IID );		
+    private void bypassSameOriginPolicy() {
+        WebAppInterface.getInstance();
+        try {
+            nsIServiceManager servMgr = null;
+            try {
+                servMgr = Mozilla.getInstance().getServiceManager();
+                if (servMgr == null) {
+                    return;
+                }
+            } catch (Exception x) {
+                PreviewerPlugin.log(x);
+                // known to throw NullPointException on Mac OS when you're not using 
+                // Mozilla. We don't want to pollute the error log with this
+                return;
+            }
 
-			mozillaPrefs.setBoolPref("signed.applets.codebase_principal_support", 1);
+            mozillaPrefs = (nsIPrefBranch) servMgr.getServiceByContractID("@mozilla.org/preferences-service;1",
+                    nsIPrefBranch.NS_IPREFBRANCH_IID);
 
-			mozillaPrefs.setCharPref("capability.policy.default.XMLDocument.getElementsByTagName", "allAccess");
-			mozillaPrefs.setCharPref("capability.policy.default.CDATASection.nodeValue", "allAccess");
+            mozillaPrefs.setBoolPref("signed.applets.codebase_principal_support", 1);
+
+            mozillaPrefs.setCharPref("capability.policy.default.XMLDocument.getElementsByTagName", "allAccess");
+            mozillaPrefs.setCharPref("capability.policy.default.CDATASection.nodeValue", "allAccess");
 
-			mozillaPrefs.setCharPref("capability.policy.default.HTMLCollection.length", "allAccess");
-			mozillaPrefs.setCharPref("capability.policy.default.HTMLCollection.item", "allAccess");
+            mozillaPrefs.setCharPref("capability.policy.default.HTMLCollection.length", "allAccess");
+            mozillaPrefs.setCharPref("capability.policy.default.HTMLCollection.item", "allAccess");
 
-			mozillaPrefs.setCharPref("capability.policy.default.*.nodeValue", "allAccess");
-			mozillaPrefs.setCharPref("capability.policy.default.*.nodeType", "allAccess");
-			mozillaPrefs.setCharPref("capability.policy.default.*.nodeName", "allAccess");
-			mozillaPrefs.setCharPref("capability.policy.default.*.nextSibling", "allAccess");
-			mozillaPrefs.setCharPref("capability.policy.default.*.previousSibling", "allAccess");
-			mozillaPrefs.setCharPref("capability.policy.default.*.attributes", "allAccess");
-			mozillaPrefs.setCharPref("capability.policy.default.*.childNodes", "allAccess");
-			mozillaPrefs.setCharPref("capability.policy.default.*.firstChild", "allAccess");
-			mozillaPrefs.setCharPref("capability.policy.default.*.getAttribute", "allAccess");
-			mozillaPrefs.setCharPref("capability.policy.default.*.getElementsByTagName", "allAccess");
-			mozillaPrefs.setCharPref("capability.policy.default.*.lastChild", "allAccess");
-			mozillaPrefs.setCharPref("capability.policy.default.*.parentNode", "allAccess");
-			mozillaPrefs.setCharPref("capability.policy.default.*.tagName", "allAccess");
+            mozillaPrefs.setCharPref("capability.policy.default.*.nodeValue", "allAccess");
+            mozillaPrefs.setCharPref("capability.policy.default.*.nodeType", "allAccess");
+            mozillaPrefs.setCharPref("capability.policy.default.*.nodeName", "allAccess");
+            mozillaPrefs.setCharPref("capability.policy.default.*.nextSibling", "allAccess");
+            mozillaPrefs.setCharPref("capability.policy.default.*.previousSibling", "allAccess");
+            mozillaPrefs.setCharPref("capability.policy.default.*.attributes", "allAccess");
+            mozillaPrefs.setCharPref("capability.policy.default.*.childNodes", "allAccess");
+            mozillaPrefs.setCharPref("capability.policy.default.*.firstChild", "allAccess");
+            mozillaPrefs.setCharPref("capability.policy.default.*.getAttribute", "allAccess");
+            mozillaPrefs.setCharPref("capability.policy.default.*.getElementsByTagName", "allAccess");
+            mozillaPrefs.setCharPref("capability.policy.default.*.lastChild", "allAccess");
+            mozillaPrefs.setCharPref("capability.policy.default.*.parentNode", "allAccess");
+            mozillaPrefs.setCharPref("capability.policy.default.*.tagName", "allAccess");
 
-			mozillaPrefs.setCharPref("capability.policy.default.XMLDocument.documentElement", "allAccess");
-			mozillaPrefs.setCharPref("capability.policy.default.XMLDocument.getElementsByTagName", "allAccess");
-			mozillaPrefs.setCharPref("capability.policy.default.XMLHttpRequest.channel", "allAccess");
-			mozillaPrefs.setCharPref("capability.policy.default.XMLHttpRequest.open", "allAccess");
-			mozillaPrefs.setCharPref("capability.policy.default.XMLHttpRequest.responseText", "allAccess");
-			mozillaPrefs.setCharPref("capability.policy.default.XMLHttpRequest.responseXML", "allAccess");
-			mozillaPrefs.setCharPref("capability.policy.default.XMLHttpRequest.send", "allAccess");
-			mozillaPrefs.setCharPref("capability.policy.default.XMLHttpRequest.setRequestHeader", "allAccess");
-			/* to over-ride the internet security dialog when preview browser tries to access local hard drive */
-			mozillaPrefs.setCharPref("capability.principal.codebase.p0.granted", "UniversalXPConnect  UniversalBrowserRead");
-			String location = "http://" + WebappManager.getHost() + ":" + WebappManager.getPort();
-			mozillaPrefs.setCharPref("capability.principal.codebase.p0.id", location);
-			mozillaPrefs.setBoolPref("security.fileuri.strict_origin_policy", 0);
-			// start JavaXPCOM section
-			nsIIOService ioService =
-				(nsIIOService)servMgr.getServiceByContractID("@mozilla.org/network/io-service;1", nsIIOService.NS_IIOSERVICE_IID);
-			nsIIOService2 ioService2 =
-				(nsIIOService2)ioService.queryInterface(nsIIOService2.NS_IIOSERVICE2_IID);
-			ioService2.setManageOfflineStatus(false);
-			ioService.setOffline(false);
-			// end JavaXPCOM section
-		} catch (Exception e) {
-			PreviewerPlugin.log("Error getting preferences", e);
-		}
-	}
+            mozillaPrefs.setCharPref("capability.policy.default.XMLDocument.documentElement", "allAccess");
+            mozillaPrefs.setCharPref("capability.policy.default.XMLDocument.getElementsByTagName", "allAccess");
+            mozillaPrefs.setCharPref("capability.policy.default.XMLHttpRequest.channel", "allAccess");
+            mozillaPrefs.setCharPref("capability.policy.default.XMLHttpRequest.open", "allAccess");
+            mozillaPrefs.setCharPref("capability.policy.default.XMLHttpRequest.responseText", "allAccess");
+            mozillaPrefs.setCharPref("capability.policy.default.XMLHttpRequest.responseXML", "allAccess");
+            mozillaPrefs.setCharPref("capability.policy.default.XMLHttpRequest.send", "allAccess");
+            mozillaPrefs.setCharPref("capability.policy.default.XMLHttpRequest.setRequestHeader", "allAccess");
+            /* to over-ride the internet security dialog when preview browser tries to access local hard drive */
+            mozillaPrefs.setCharPref("capability.principal.codebase.p0.granted",
+                    "UniversalXPConnect  UniversalBrowserRead");
+            String location = "http://" + WebappManager.getHost() + ":" + WebappManager.getPort();
+            mozillaPrefs.setCharPref("capability.principal.codebase.p0.id", location);
+            mozillaPrefs.setBoolPref("security.fileuri.strict_origin_policy", 0);
+            // start JavaXPCOM section
+            nsIIOService ioService = (nsIIOService) servMgr.getServiceByContractID("@mozilla.org/network/io-service;1",
+                    nsIIOService.NS_IIOSERVICE_IID);
+            nsIIOService2 ioService2 = (nsIIOService2) ioService.queryInterface(nsIIOService2.NS_IIOSERVICE2_IID);
+            ioService2.setManageOfflineStatus(false);
+            ioService.setOffline(false);
 
+            // end JavaXPCOM section
+        } catch (Exception e) {
+            PreviewerPlugin.log("Error getting preferences", e);
+        }
+    }
+
+    @Override
+    protected void contributeToToolbar(IToolBarManager toolBar) {
+        toolBar.add(new ClearPreferencesAction());
+        toolBar.add(new Separator());
+    }
 
 }