]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/browser/WebPolicyDelegate.java
Remove invalid SHA-256-Digests
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / browser / WebPolicyDelegate.java
1 /*******************************************************************************
2  * Copyright (c) 2010, 2017 IBM Corporation and others.
3  *
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/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.browser;
15
16
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.*;
23
24 class WebPolicyDelegate {
25         COMObject iWebPolicyDelegate;
26         int refCount = 0;
27
28         Browser browser;
29
30 WebPolicyDelegate (Browser browser) {
31         createCOMInterfaces ();
32         this.browser = browser;
33 }
34
35 int AddRef () {
36         refCount++;
37         return refCount;
38 }
39
40 void createCOMInterfaces () {
41         iWebPolicyDelegate = new COMObject (new int[] {2, 0, 0, 5, 5, 5, 3}) {
42                 @Override
43                 public long method0 (long[] args) {return QueryInterface (args[0], args[1]);}
44                 @Override
45                 public long method1 (long[] args) {return AddRef ();}
46                 @Override
47                 public long method2 (long[] args) {return Release ();}
48                 @Override
49                 public long method3 (long[] args) {return decidePolicyForNavigationAction (args[0], args[1], args[2], args[3], args[4]);}
50                 @Override
51                 public long method4 (long[] args) {return decidePolicyForNewWindowAction (args[0], args[1], args[2], args[3], args[4]);}
52                 @Override
53                 public long method5 (long[] args) {return decidePolicyForMIMEType (args[0], args[1], args[2], args[3], args[4]);}
54                 @Override
55                 public long method6 (long[] args) {return unableToImplementPolicyWithError (args[0], args[1], args[2]);}
56         };
57 }
58
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) {
65                 pdListener.use ();
66         } else {
67                 pdListener.download ();
68         }
69         return COM.S_OK;
70 }
71
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) {
77                 return COM.S_OK;
78         }
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) {
84                 /*
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().
88                  */
89                 pdListener.use ();
90                 return COM.S_OK;
91         }
92         if (url.length () == 0) {
93                 pdListener.ignore ();
94                 return COM.S_OK;
95         }
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 */
98                 pdListener.ignore ();
99                 return COM.S_OK;
100         }
101         /*
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.
104          */
105         if (url.equals (WebKit.URI_FILEROOT)) {
106                 url = WebKit.ABOUT_BLANK;
107         } else {
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);
111                 }
112         }
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);
122                 }
123         }
124         if (newEvent.doit) {
125                 if (webKit.jsEnabled != webKit.jsEnabledOnNextPage) {
126                         webKit.jsEnabled = webKit.jsEnabledOnNextPage;
127                         IWebView view = new IWebView (webView);
128                         result[0] = 0;
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 ();
135                         }
136                 }
137                 pdListener.use ();
138                 webKit.lastNavigateURL = url;
139         } else {
140                 pdListener.ignore ();
141         }
142         return COM.S_OK;
143 }
144
145 int decidePolicyForNewWindowAction (long webView, long actionInformation, long request, long frameName, long listener) {
146         IWebPolicyDecisionListener pdListener = new IWebPolicyDecisionListener (listener);
147         pdListener.use();
148         return COM.S_OK;
149 }
150
151 protected void disposeCOMInterfaces () {
152         if (iWebPolicyDelegate != null) {
153                 iWebPolicyDelegate.dispose ();
154                 iWebPolicyDelegate = null;
155         }
156 }
157
158 long getAddress () {
159         return iWebPolicyDelegate.getAddress ();
160 }
161
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);
166
167         if (COM.IsEqualGUID (guid, COM.IIDIUnknown)) {
168                 OS.MoveMemory (ppvObject, new long[] {iWebPolicyDelegate.getAddress ()}, C.PTR_SIZEOF);
169                 new IUnknown (iWebPolicyDelegate.getAddress ()).AddRef ();
170                 return COM.S_OK;
171         }
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 ();
175                 return COM.S_OK;
176         }
177
178         OS.MoveMemory (ppvObject, new long[] {0}, C.PTR_SIZEOF);
179         return COM.E_NOINTERFACE;
180 }
181
182 int Release () {
183         refCount--;
184         if (refCount == 0) {
185                 disposeCOMInterfaces ();
186         }
187         return refCount;
188 }
189
190 int unableToImplementPolicyWithError (long webView, long error, long frame) {
191         if (browser.isDisposed ()) return COM.S_OK;
192
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]);
200         }
201         result[0] = 0;
202         hr = iweberror.localizedDescription (result);
203         if (hr != COM.S_OK || result[0] == 0) {
204                 return COM.S_OK;
205         }
206         String description = WebKit.extractBSTR (result[0]);
207         COM.SysFreeString (result[0]);
208
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);
213         messageBox.open ();
214         return COM.S_OK;
215 }
216
217 }