]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/browser/WebDownloadDelegate.java
Remove invalid SHA-256-Digests
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / browser / WebDownloadDelegate.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 java.io.*;
18
19 import org.eclipse.swt.*;
20 import org.eclipse.swt.internal.*;
21 import org.eclipse.swt.internal.ole.win32.*;
22 import org.eclipse.swt.internal.webkit.*;
23 import org.eclipse.swt.internal.win32.*;
24 import org.eclipse.swt.layout.*;
25 import org.eclipse.swt.widgets.*;
26
27 class WebDownloadDelegate {
28         COMObject iWebDownloadDelegate;
29
30         Browser browser;
31         int refCount = 0;
32         int status = -1;
33         long size, totalSize;
34         String url;
35
36         static final int DOWNLOAD_FINISHED = 0;
37         static final int DOWNLOAD_CANCELLED = 1;
38         static final int DOWNLOAD_ERROR = 3;
39
40 WebDownloadDelegate (Browser browser) {
41         createCOMInterfaces ();
42         this.browser = browser;
43 }
44
45 int AddRef () {
46         refCount++;
47         return refCount;
48 }
49
50 void createCOMInterfaces () {
51         iWebDownloadDelegate = new COMObject (new int[] {2, 0, 0, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4, 1, 1}) {
52                 @Override
53                 public long method0 (long[] args) {return QueryInterface (args[0], args[1]);}
54                 @Override
55                 public long method1 (long[] args) {return AddRef ();}
56                 @Override
57                 public long method2 (long[] args) {return Release ();}
58                 @Override
59                 public long method3 (long[] args) {return decideDestinationWithSuggestedFilename (args[0], args[1]);}
60                 @Override
61                 public long method4 (long[] args) {return COM.E_NOTIMPL;}
62                 @Override
63                 public long method5 (long[] args) {return COM.E_NOTIMPL;}
64                 @Override
65                 public long method6 (long[] args) {return didFailWithError (args[0], args[1]);}
66                 @Override
67                 public long method7 (long[] args) {return COM.E_NOTIMPL;}
68                 @Override
69                 public long method8 (long[] args) {return didReceiveDataOfLength (args[0], (int)args[1]);}
70                 @Override
71                 public long method9 (long[] args) {return didReceiveResponse (args[0], args[1]);}
72                 @Override
73                 public long method10 (long[] args) {return COM.E_NOTIMPL;}
74                 @Override
75                 public long method11 (long[] args) {return COM.E_NOTIMPL;}
76                 @Override
77                 public long method12 (long[] args) {return willSendRequest (args[0], args[1], args[2], args[3]);}
78                 @Override
79                 public long method13 (long[] args) {return didBegin (args[0]);}
80                 @Override
81                 public long method14 (long[] args) {return didFinish (args[0]);}
82         };
83 }
84
85 int decideDestinationWithSuggestedFilename (long download, long filename) {
86         String name = WebKit.extractBSTR (filename);
87         FileDialog dialog = new FileDialog (browser.getShell(), SWT.SAVE);
88         dialog.setText (SWT.getMessage ("SWT_FileDownload")); //$NON-NLS-1$
89         dialog.setFileName (name);
90         dialog.setOverwrite (true);
91         String path = dialog.open ();
92         IWebDownload iwebdownload = new IWebDownload (download);
93         iwebdownload.setDeletesFileUponFailure (0);
94         if (path == null) {
95                 /*
96                 * Bug in WebKit.  Failure to set a non-null destination on the IWebDownload results in
97                 * a crash, even when the download is being cancelled.
98                 */
99                 iwebdownload.setDestination (WebKit.createBSTR (""), 1); //$NON-NLS-1$
100                 iwebdownload.cancel();
101                 iwebdownload.Release();
102         } else {
103                 File file = new File (path);
104                 if (file.exists ()) file.delete ();
105                 iwebdownload.setDestination (WebKit.createBSTR (path), 1);
106                 openDownloadWindow (iwebdownload, path);
107         }
108         return COM.S_OK;
109 }
110
111 int didBegin (long download) {
112         new IWebDownload (download).AddRef ();
113         status = -1;
114         size = 0;
115         totalSize = 0;
116         url = null;
117         return COM.S_OK;
118 }
119
120 int didFailWithError (long download, long error) {
121         new IWebDownload (download).Release ();
122         status = DOWNLOAD_ERROR;
123         return COM.S_OK;
124 }
125
126 int didFinish (long download) {
127         new IWebDownload (download).Release ();
128         status = DOWNLOAD_FINISHED;
129         return COM.S_OK;
130 }
131
132 int didReceiveDataOfLength (long download, int length) {
133         size += length;
134         return COM.S_OK;
135 }
136
137 int didReceiveResponse (long download, long response) {
138         if (response != 0) {
139                 IWebURLResponse urlResponse = new IWebURLResponse (response);
140                 long[] size = new long[1];
141                 int hr = urlResponse.expectedContentLength (size);
142                 if (hr == COM.S_OK) totalSize = size[0];
143                 long[] result = new long[1];
144                 hr = urlResponse.URL (result);
145                 if (hr == COM.S_OK && result[0] != 0) {
146                         url = WebKit.extractBSTR (result[0]);
147                         COM.SysFreeString (result[0]);
148                 }
149         }
150         return COM.S_OK;
151 }
152
153 void disposeCOMInterfaces () {
154         if (iWebDownloadDelegate != null) {
155                 iWebDownloadDelegate.dispose ();
156                 iWebDownloadDelegate = null;
157         }
158 }
159
160 long getAddress () {
161         return iWebDownloadDelegate.getAddress ();
162 }
163
164 void openDownloadWindow (final IWebDownload download, String name) {
165         final Shell shell = new Shell ();
166         shell.setText (Compatibility.getMessage ("SWT_FileDownload"));  //$NON-NLS-1$
167         GridLayout gridLayout = new GridLayout ();
168         gridLayout.marginHeight = 15;
169         gridLayout.marginWidth = 15;
170         gridLayout.verticalSpacing = 20;
171         shell.setLayout (gridLayout);
172
173         Label nameLabel = new Label (shell, SWT.WRAP);
174         nameLabel.setText (Compatibility.getMessage ("SWT_Download_Location", new Object[] {name, url})); //$NON-NLS-1$
175         GridData data = new GridData ();
176         Monitor monitor = browser.getMonitor ();
177         int maxWidth = monitor.getBounds ().width / 2;
178         int width = nameLabel.computeSize (SWT.DEFAULT, SWT.DEFAULT).x;
179         data.widthHint = Math.min (width, maxWidth);
180         data.horizontalAlignment = GridData.FILL;
181         data.grabExcessHorizontalSpace = true;
182         nameLabel.setLayoutData (data);
183
184         final Label statusLabel = new Label (shell, SWT.NONE);
185         statusLabel.setText (Compatibility.getMessage ("SWT_Download_Started")); //$NON-NLS-1$
186         data = new GridData (GridData.FILL_BOTH);
187         statusLabel.setLayoutData (data);
188
189         final Button cancel = new Button (shell, SWT.PUSH);
190         cancel.setText (Compatibility.getMessage ("SWT_Cancel")); //$NON-NLS-1$
191         data = new GridData ();
192         data.horizontalAlignment = GridData.CENTER;
193         cancel.setLayoutData (data);
194         final Listener cancelListener = event -> {
195                 download.cancel ();
196                 status = DOWNLOAD_CANCELLED;
197                 download.Release ();
198         };
199         cancel.addListener (SWT.Selection, cancelListener);
200
201         final Display display = browser.getDisplay ();
202         final int INTERVAL = 500;
203         display.timerExec (INTERVAL, new Runnable () {
204                 @Override
205                 public void run () {
206                         if (shell.isDisposed () || status == DOWNLOAD_FINISHED || status == DOWNLOAD_CANCELLED) {
207                                 shell.dispose ();
208                                 return;
209                         }
210                         if (status == DOWNLOAD_ERROR) {
211                                 statusLabel.setText (Compatibility.getMessage ("SWT_Download_Error")); //$NON-NLS-1$
212                                 cancel.removeListener (SWT.Selection, cancelListener);
213                                 cancel.addListener (SWT.Selection, event -> shell.dispose ());
214                                 return;
215                         }
216                         long current = size / 1024L;
217                         long total = totalSize / 1024L;
218                         String message = Compatibility.getMessage ("SWT_Download_Status", new Object[] {current, total}); //$NON-NLS-1$
219                         statusLabel.setText (message);
220                         display.timerExec (INTERVAL, this);
221                 }
222         });
223         shell.pack ();
224         shell.open ();
225 }
226
227 int QueryInterface (long riid, long ppvObject) {
228         if (riid == 0 || ppvObject == 0) return COM.E_INVALIDARG;
229         GUID guid = new GUID ();
230         COM.MoveMemory (guid, riid, GUID.sizeof);
231
232         if (COM.IsEqualGUID (guid, COM.IIDIUnknown)) {
233                 OS.MoveMemory (ppvObject, new long[] {iWebDownloadDelegate.getAddress ()}, C.PTR_SIZEOF);
234                 new IUnknown (iWebDownloadDelegate.getAddress ()).AddRef ();
235                 return COM.S_OK;
236         }
237         if (COM.IsEqualGUID (guid, WebKit_win32.IID_IWebDownloadDelegate)) {
238                 OS.MoveMemory (ppvObject, new long[] {iWebDownloadDelegate.getAddress ()}, C.PTR_SIZEOF);
239                 new IUnknown (iWebDownloadDelegate.getAddress ()).AddRef ();
240                 return COM.S_OK;
241         }
242
243         OS.MoveMemory (ppvObject, new long[] {0}, C.PTR_SIZEOF);
244         return COM.E_NOINTERFACE;
245 }
246
247 int Release () {
248         refCount--;
249         if (refCount == 0) {
250                 disposeCOMInterfaces ();
251         }
252         return refCount;
253 }
254
255 int willSendRequest (long download, long request, long redirectResponse, long finalRequest) {
256         IWebMutableURLRequest req = new IWebMutableURLRequest (request);
257         req.AddRef ();
258         OS.MoveMemory (finalRequest, new long[] {request}, C.PTR_SIZEOF);
259         return COM.S_OK;
260 }
261
262 }