org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/preview/PreviewPage.java
changeset 55 bb6160d0b6f2
parent 54 5a2cfa9bc743
child 56 22f918ed49f7
equal deleted inserted replaced
54:5a2cfa9bc743 55:bb6160d0b6f2
     1 package org.symbian.tools.wrttools.previewer.preview;
       
     2 
       
     3 import java.io.File;
       
     4 import java.io.IOException;
       
     5 import java.net.URI;
       
     6 import java.net.URISyntaxException;
       
     7 import java.net.URL;
       
     8 import java.util.Collection;
       
     9 
       
    10 import org.eclipse.core.net.proxy.IProxyData;
       
    11 import org.eclipse.core.net.proxy.IProxyService;
       
    12 import org.eclipse.core.resources.IFile;
       
    13 import org.eclipse.core.resources.IProject;
       
    14 import org.eclipse.core.runtime.FileLocator;
       
    15 import org.eclipse.core.runtime.Platform;
       
    16 import org.eclipse.jface.action.Action;
       
    17 import org.eclipse.jface.action.IAction;
       
    18 import org.eclipse.jface.action.IToolBarManager;
       
    19 import org.eclipse.jface.viewers.ISelection;
       
    20 import org.eclipse.jface.viewers.ISelectionChangedListener;
       
    21 import org.eclipse.jface.viewers.ISelectionProvider;
       
    22 import org.eclipse.jface.viewers.StructuredSelection;
       
    23 import org.eclipse.swt.SWT;
       
    24 import org.eclipse.swt.browser.Browser;
       
    25 import org.eclipse.swt.widgets.Composite;
       
    26 import org.eclipse.swt.widgets.Control;
       
    27 import org.eclipse.ui.actions.ActionFactory;
       
    28 import org.eclipse.ui.part.IPageBookViewPage;
       
    29 import org.eclipse.ui.part.IPageSite;
       
    30 import org.eclipse.ui.part.Page;
       
    31 import org.mozilla.interfaces.nsIPrefBranch;
       
    32 import org.mozilla.interfaces.nsIServiceManager;
       
    33 import org.mozilla.xpcom.Mozilla;
       
    34 import org.osgi.framework.Bundle;
       
    35 import org.symbian.tools.wrttools.previewer.PreviewerPlugin;
       
    36 import org.symbian.tools.wrttools.previewer.Images;
       
    37 import org.symbian.tools.wrttools.previewer.http.WebAppInterface;
       
    38 import org.symbian.tools.wrttools.previewer.http.WebappManager;
       
    39 
       
    40 public class PreviewPage extends Page implements IPageBookViewPage, ISelectionProvider {
       
    41 	private static final String XUL_RUNNER_PATH_PARAMETER = "org.eclipse.swt.browser.XULRunnerPath";
       
    42 	private nsIPrefBranch mozillaPrefs;
       
    43 
       
    44 	private final IAction refreshAction = new Action("Refresh") {
       
    45 		public void run() {
       
    46 			refresh();
       
    47 		};
       
    48 	};
       
    49 	private final IAction toggleRefresh = new Action("Toggle Refresh", IAction.AS_RADIO_BUTTON) {
       
    50 		public void run() {
       
    51 			toggleRefresh();
       
    52 		};
       
    53 	};
       
    54 
       
    55 	private final IProject project;
       
    56 	private Browser browser;
       
    57 	private boolean toggleState = true;
       
    58 	private final PreviewView previewView;
       
    59 	private boolean needsRefresh = false;
       
    60 
       
    61 	public PreviewPage(IProject project, PreviewView previewView) {
       
    62 		this.project = project;
       
    63 		this.previewView = previewView;
       
    64 	}
       
    65 
       
    66 	protected void toggleRefresh() {
       
    67 		toggleState = !toggleState;
       
    68 		toggleRefresh.setChecked(toggleState);
       
    69 		previewView.setProjectAutorefresh(project, toggleState);
       
    70 		toggleRefresh.setToolTipText(getToggleActionTooltip());
       
    71 		if (toggleState && needsRefresh) {
       
    72 			refresh();
       
    73 		}
       
    74 	}
       
    75 
       
    76 	private synchronized void initMozilla() {
       
    77 		if (System.getProperty(XUL_RUNNER_PATH_PARAMETER) == null) {
       
    78 			Bundle bundle = Platform.getBundle("org.mozilla.xulrunner"); //$NON-NLS-1$
       
    79 			if (bundle != null) {
       
    80 				URL resourceUrl = bundle.getResource("xulrunner"); //$NON-NLS-1$
       
    81 				if (resourceUrl != null) {
       
    82 					try {
       
    83 						URL fileUrl = FileLocator.toFileURL(resourceUrl);
       
    84 						File file = new File(fileUrl.toURI());
       
    85 						System.setProperty(XUL_RUNNER_PATH_PARAMETER, file
       
    86 								.getAbsolutePath()); //$NON-NLS-1$
       
    87 					} catch (IOException e) {
       
    88 						// log the exception
       
    89 					} catch (URISyntaxException e) {
       
    90 						// log the exception
       
    91 					}
       
    92 				}
       
    93 			}
       
    94 		}
       
    95 	}
       
    96 
       
    97 	
       
    98 	@Override
       
    99 	public void createControl(Composite parent) {
       
   100 		initMozilla();
       
   101 		browser = new Browser(parent, SWT.MOZILLA);
       
   102 		bypassSameOriginPolicy();
       
   103 		applyProxySettings();
       
   104 		browser.setUrl(getURI().toASCIIString());
       
   105 	}
       
   106 
       
   107 	private URI getURI() {
       
   108 		return PreviewerPlugin.getDefault().getHttpPreviewer().previewProject(project);
       
   109 	}
       
   110 
       
   111 	@Override
       
   112 	public Control getControl() {
       
   113 		return browser;
       
   114 	}
       
   115 
       
   116 	@Override
       
   117 	public void setFocus() {
       
   118 		browser.setFocus();
       
   119 	}
       
   120 	
       
   121 	private boolean refreshScheduled = false;
       
   122 
       
   123 	public synchronized void process(Collection<IFile> files) {
       
   124 		if (!refreshScheduled && needsRefresh(files)) {
       
   125 			asyncExec(new Runnable() {
       
   126 				@Override
       
   127 				public void run() {
       
   128 					if (toggleState) {
       
   129 						refresh();
       
   130 					} else {
       
   131 						needsRefresh = true;
       
   132 						refreshAction.setImageDescriptor(PreviewerPlugin.getImageDescriptor(Images.RED_SYNC));
       
   133 						refreshAction.setToolTipText("Refresh the preview browser (there are updated files)");
       
   134 					}
       
   135 				}
       
   136 			});
       
   137 			refreshScheduled = true;
       
   138 		}
       
   139 	}
       
   140 
       
   141 	private void asyncExec(Runnable runnable) {
       
   142 		getControl().getDisplay().asyncExec(runnable);
       
   143 	}
       
   144 
       
   145 	private boolean needsRefresh(Collection<IFile> files) {
       
   146 		for (IFile iFile : files) {
       
   147 			if (iFile.getProject().equals(project)) {
       
   148 				return true;
       
   149 			}
       
   150 		}
       
   151 		return false;
       
   152 	}
       
   153 
       
   154 	protected synchronized void refresh() {
       
   155 		try {
       
   156 			final Control focusControl = browser.getDisplay().getFocusControl();
       
   157 			browser.refresh();
       
   158 			refreshAction.setImageDescriptor(PreviewerPlugin
       
   159 					.getImageDescriptor(Images.GREEN_SYNC));
       
   160 			if (focusControl != null) {
       
   161 				asyncExec(new Runnable() {
       
   162 					@Override
       
   163 					public void run() {
       
   164 						focusControl.setFocus();
       
   165 					}
       
   166 				});
       
   167 			}
       
   168 			refreshAction.setToolTipText("Refresh the preview browser");
       
   169 			needsRefresh = false;
       
   170 		} finally {
       
   171 			refreshScheduled = false;
       
   172 		}
       
   173 	}
       
   174 
       
   175 	@Override
       
   176 	public void init(IPageSite pageSite) {
       
   177 		super.init(pageSite);
       
   178 		IToolBarManager toolBar = pageSite.getActionBars().getToolBarManager();
       
   179 		refreshAction.setImageDescriptor(PreviewerPlugin.getImageDescriptor(Images.GREEN_SYNC));
       
   180 		refreshAction.setToolTipText("Refresh the preview browser");
       
   181 		toolBar.add(refreshAction);
       
   182 		
       
   183 		toggleState = previewView.getProjectAutorefresh(project);
       
   184 		
       
   185 		toggleRefresh.setImageDescriptor(PreviewerPlugin.getImageDescriptor(Images.YELLOW_SYNC));
       
   186 		toggleRefresh.setToolTipText(getToggleActionTooltip());
       
   187 		toggleRefresh.setChecked(toggleState);
       
   188 		toolBar.add(toggleRefresh);
       
   189 		
       
   190 		pageSite.getActionBars().setGlobalActionHandler(ActionFactory.REFRESH.getId(), refreshAction);
       
   191 		getSite().setSelectionProvider(this);
       
   192 	}
       
   193 
       
   194 	private String getToggleActionTooltip() {
       
   195 		return toggleState ? "Disable preview autorefresh" : "Enable preview autorefresh";
       
   196 	}
       
   197 
       
   198 	@Override
       
   199 	public void addSelectionChangedListener(ISelectionChangedListener listener) {
       
   200 		// Do nothing
       
   201 	}
       
   202 
       
   203 	@Override
       
   204 	public ISelection getSelection() {
       
   205 		return new StructuredSelection(project);
       
   206 	}
       
   207 
       
   208 	@Override
       
   209 	public void removeSelectionChangedListener(
       
   210 			ISelectionChangedListener listener) {
       
   211 		// Do nothing
       
   212 	}
       
   213 
       
   214 	@Override
       
   215 	public void setSelection(ISelection selection) {
       
   216 		// Do nothing
       
   217 	}
       
   218 	
       
   219 	private void applyProxySettings() {
       
   220 		IProxyService px = PreviewerPlugin.getDefault().getProxyService();
       
   221 		if(px != null){			 
       
   222 			boolean proxyEnabled = px.isProxiesEnabled();
       
   223 
       
   224 			boolean systemProxy = px.isSystemProxiesEnabled();
       
   225 			if( proxyEnabled && !systemProxy){
       
   226 				IProxyData pd = px.getProxyData(IProxyData.HTTP_PROXY_TYPE);
       
   227 				if (pd !=null &&mozillaPrefs != null) {				
       
   228 					String host= pd.getHost();
       
   229 					int port = pd.getPort();
       
   230 					if(host !=null && port != -1){
       
   231 						mozillaPrefs.setIntPref("network.proxy.type", 1);
       
   232 						mozillaPrefs.setCharPref("network.proxy.http", host);
       
   233 						mozillaPrefs.setIntPref("network.proxy.http_port", port);
       
   234 					}
       
   235 				}
       
   236 			 }
       
   237 		}
       
   238 		else{
       
   239 			 Exception e= new Exception();
       
   240 			 PreviewerPlugin.log("Proxy service returned null", e);
       
   241 		 }
       
   242 	}
       
   243 	
       
   244 	private void bypassSameOriginPolicy() {
       
   245 		WebAppInterface.getInstance();
       
   246 		try{
       
   247 			nsIServiceManager servMgr = null;
       
   248 			try {
       
   249 				servMgr = Mozilla.getInstance().getServiceManager();
       
   250 				if (servMgr == null) return;
       
   251 			} catch (Exception x) {
       
   252 				// known to throw NullPointException on Mac OS when you're not using 
       
   253 				// Mozilla. We don't want to pollute the error log with this
       
   254 				return;
       
   255 			}
       
   256 			
       
   257 			mozillaPrefs = (nsIPrefBranch) servMgr.getServiceByContractID(
       
   258 											"@mozilla.org/preferences-service;1", 
       
   259 											nsIPrefBranch.NS_IPREFBRANCH_IID );		
       
   260 
       
   261 			mozillaPrefs.setBoolPref("signed.applets.codebase_principal_support", 1);
       
   262 
       
   263 			mozillaPrefs.setCharPref("capability.policy.default.XMLDocument.getElementsByTagName", "allAccess");
       
   264 			mozillaPrefs.setCharPref("capability.policy.default.CDATASection.nodeValue", "allAccess");
       
   265 
       
   266 			mozillaPrefs.setCharPref("capability.policy.default.HTMLCollection.length", "allAccess");
       
   267 			mozillaPrefs.setCharPref("capability.policy.default.HTMLCollection.item", "allAccess");
       
   268 
       
   269 			mozillaPrefs.setCharPref("capability.policy.default.*.nodeValue", "allAccess");
       
   270 			mozillaPrefs.setCharPref("capability.policy.default.*.nodeType", "allAccess");
       
   271 			mozillaPrefs.setCharPref("capability.policy.default.*.nodeName", "allAccess");
       
   272 			mozillaPrefs.setCharPref("capability.policy.default.*.nextSibling", "allAccess");
       
   273 			mozillaPrefs.setCharPref("capability.policy.default.*.previousSibling", "allAccess");
       
   274 			mozillaPrefs.setCharPref("capability.policy.default.*.attributes", "allAccess");
       
   275 			mozillaPrefs.setCharPref("capability.policy.default.*.childNodes", "allAccess");
       
   276 			mozillaPrefs.setCharPref("capability.policy.default.*.firstChild", "allAccess");
       
   277 			mozillaPrefs.setCharPref("capability.policy.default.*.getAttribute", "allAccess");
       
   278 			mozillaPrefs.setCharPref("capability.policy.default.*.getElementsByTagName", "allAccess");
       
   279 			mozillaPrefs.setCharPref("capability.policy.default.*.lastChild", "allAccess");
       
   280 			mozillaPrefs.setCharPref("capability.policy.default.*.parentNode", "allAccess");
       
   281 			mozillaPrefs.setCharPref("capability.policy.default.*.tagName", "allAccess");
       
   282 
       
   283 			mozillaPrefs.setCharPref("capability.policy.default.XMLDocument.documentElement", "allAccess");
       
   284 			mozillaPrefs.setCharPref("capability.policy.default.XMLDocument.getElementsByTagName", "allAccess");
       
   285 			mozillaPrefs.setCharPref("capability.policy.default.XMLHttpRequest.channel", "allAccess");
       
   286 			mozillaPrefs.setCharPref("capability.policy.default.XMLHttpRequest.open", "allAccess");
       
   287 			mozillaPrefs.setCharPref("capability.policy.default.XMLHttpRequest.responseText", "allAccess");
       
   288 			mozillaPrefs.setCharPref("capability.policy.default.XMLHttpRequest.responseXML", "allAccess");
       
   289 			mozillaPrefs.setCharPref("capability.policy.default.XMLHttpRequest.send", "allAccess");
       
   290 			mozillaPrefs.setCharPref("capability.policy.default.XMLHttpRequest.setRequestHeader", "allAccess");
       
   291 			/* to over-ride the internet security dialog when preview browser tries to access local hard drive */
       
   292 			mozillaPrefs.setCharPref("capability.principal.codebase.p0.granted", "UniversalXPConnect  UniversalBrowserRead");
       
   293 			String location = "http://127.0.0.1:" + WebappManager.getPort();
       
   294 			mozillaPrefs.setCharPref("capability.principal.codebase.p0.id", location);
       
   295 			mozillaPrefs.setBoolPref("security.fileuri.strict_origin_policy", 0);
       
   296 		} catch (Exception e) {
       
   297 			PreviewerPlugin.log("Error getting preferences", e);
       
   298 		}
       
   299 	}
       
   300 
       
   301 }