1 /*******************************************************************************
2 * Copyright (c) 2010, 2017 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.browser;
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.internal.*;
19 import org.eclipse.swt.internal.ole.win32.*;
20 import org.eclipse.swt.internal.webkit.*;
21 import org.eclipse.swt.internal.win32.*;
22 import org.eclipse.swt.widgets.*;
24 class WebPolicyDelegate {
25 COMObject iWebPolicyDelegate;
30 WebPolicyDelegate (Browser browser) {
31 createCOMInterfaces ();
32 this.browser = browser;
40 void createCOMInterfaces () {
41 iWebPolicyDelegate = new COMObject (new int[] {2, 0, 0, 5, 5, 5, 3}) {
43 public long method0 (long[] args) {return QueryInterface (args[0], args[1]);}
45 public long method1 (long[] args) {return AddRef ();}
47 public long method2 (long[] args) {return Release ();}
49 public long method3 (long[] args) {return decidePolicyForNavigationAction (args[0], args[1], args[2], args[3], args[4]);}
51 public long method4 (long[] args) {return decidePolicyForNewWindowAction (args[0], args[1], args[2], args[3], args[4]);}
53 public long method5 (long[] args) {return decidePolicyForMIMEType (args[0], args[1], args[2], args[3], args[4]);}
55 public long method6 (long[] args) {return unableToImplementPolicyWithError (args[0], args[1], args[2]);}
59 int decidePolicyForMIMEType (long webView, long type, long request, long frame, long listener) {
60 IWebView iwebView = new IWebView (webView);
61 int[] canShow = new int[1];
62 iwebView.canShowMIMEType (type, canShow);
63 IWebPolicyDecisionListener pdListener = new IWebPolicyDecisionListener (listener);
64 if (canShow[0] != 0) {
67 pdListener.download ();
72 int decidePolicyForNavigationAction (long webView, long actionInformation, long request, long frame, long listener) {
73 IWebURLRequest iwebUrlRequest = new IWebURLRequest (request);
74 long[] result = new long[1];
75 int hr = iwebUrlRequest.URL (result);
76 if (hr != COM.S_OK || result[0] == 0) {
79 String url = WebKit.extractBSTR (result[0]);
80 COM.SysFreeString (result[0]);
81 IWebPolicyDecisionListener pdListener = new IWebPolicyDecisionListener (listener);
82 WebKit webKit = (WebKit)browser.webBrowser;
83 if (webKit.loadingText) {
85 * WebKit is auto-navigating to about:blank in response to a loadHTMLString()
86 * invocation. This navigate should always proceed without sending an event
87 * since it is preceded by an explicit navigate to about:blank in setText().
92 if (url.length () == 0) {
96 if (url.startsWith (WebKit.PROTOCOL_FILE) && webKit.getUrl ().startsWith (WebKit.ABOUT_BLANK) && webKit.untrustedText) {
97 /* indicates an attempt to access the local file system from untrusted content */
102 * If the URI indicates that the page is being rendered from memory
103 * (via setText()) then set it to about:blank to be consistent with IE.
105 if (url.equals (WebKit.URI_FILEROOT)) {
106 url = WebKit.ABOUT_BLANK;
108 int length = WebKit.URI_FILEROOT.length ();
109 if (url.startsWith (WebKit.URI_FILEROOT) && url.charAt (length) == '#') {
110 url = WebKit.ABOUT_BLANK + url.substring (length);
113 LocationEvent newEvent = new LocationEvent (browser);
114 newEvent.display = browser.getDisplay ();
115 newEvent.widget = browser;
116 newEvent.location = url;
117 newEvent.doit = true;
118 LocationListener[] locationListeners = webKit.locationListeners;
119 if (locationListeners != null) {
120 for (int i = 0; i < locationListeners.length; i++) {
121 locationListeners[i].changing (newEvent);
125 if (webKit.jsEnabled != webKit.jsEnabledOnNextPage) {
126 webKit.jsEnabled = webKit.jsEnabledOnNextPage;
127 IWebView view = new IWebView (webView);
129 hr = view.preferences (result);
130 if (hr == COM.S_OK && result[0] != 0) {
131 IWebPreferences preferences = new IWebPreferences (result[0]);
132 hr = preferences.setJavaScriptEnabled (webKit.jsEnabled ? 1 : 0);
133 view.setPreferences (preferences.getAddress());
134 preferences.Release ();
138 webKit.lastNavigateURL = url;
140 pdListener.ignore ();
145 int decidePolicyForNewWindowAction (long webView, long actionInformation, long request, long frameName, long listener) {
146 IWebPolicyDecisionListener pdListener = new IWebPolicyDecisionListener (listener);
151 protected void disposeCOMInterfaces () {
152 if (iWebPolicyDelegate != null) {
153 iWebPolicyDelegate.dispose ();
154 iWebPolicyDelegate = null;
159 return iWebPolicyDelegate.getAddress ();
162 int QueryInterface (long riid, long ppvObject) {
163 if (riid == 0 || ppvObject == 0) return COM.E_INVALIDARG;
164 GUID guid = new GUID ();
165 COM.MoveMemory (guid, riid, GUID.sizeof);
167 if (COM.IsEqualGUID (guid, COM.IIDIUnknown)) {
168 OS.MoveMemory (ppvObject, new long[] {iWebPolicyDelegate.getAddress ()}, C.PTR_SIZEOF);
169 new IUnknown (iWebPolicyDelegate.getAddress ()).AddRef ();
172 if (COM.IsEqualGUID (guid, WebKit_win32.IID_IWebPolicyDelegate)) {
173 OS.MoveMemory (ppvObject, new long[] {iWebPolicyDelegate.getAddress ()}, C.PTR_SIZEOF);
174 new IUnknown (iWebPolicyDelegate.getAddress ()).AddRef ();
178 OS.MoveMemory (ppvObject, new long[] {0}, C.PTR_SIZEOF);
179 return COM.E_NOINTERFACE;
185 disposeCOMInterfaces ();
190 int unableToImplementPolicyWithError (long webView, long error, long frame) {
191 if (browser.isDisposed ()) return COM.S_OK;
193 IWebError iweberror = new IWebError (error);
194 String failingURL = null;
195 long[] result = new long[1];
196 int hr = iweberror.failingURL (result);
197 if (hr == COM.S_OK && result[0] != 0) {
198 failingURL = WebKit.extractBSTR (result[0]);
199 COM.SysFreeString (result[0]);
202 hr = iweberror.localizedDescription (result);
203 if (hr != COM.S_OK || result[0] == 0) {
206 String description = WebKit.extractBSTR (result[0]);
207 COM.SysFreeString (result[0]);
209 String message = failingURL != null ? failingURL + "\n\n" : ""; //$NON-NLS-1$ //$NON-NLS-2$
210 message += Compatibility.getMessage ("SWT_Page_Load_Failed", new Object[] {description}); //$NON-NLS-1$
211 MessageBox messageBox = new MessageBox (browser.getShell (), SWT.OK | SWT.ICON_ERROR);
212 messageBox.setMessage (message);