-
Notifications
You must be signed in to change notification settings - Fork 15
/
frmBrowser.pas
281 lines (244 loc) · 8.67 KB
/
frmBrowser.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
unit frmBrowser;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, WebView2, Winapi.ActiveX, Vcl.Edge,
Vcl.OleCtrls, SHDocVw, SHDocVw_EWB, EwbCore, EmbeddedWB, IEDownload,
IEMultiDownload, UrlMon, Vcl.ExtCtrls, UWP.Downloader, Vcl.WinXPanels,
Vcl.ComCtrls, Winapi.Mshtmhst, Vcl.StdCtrls, Vcl.FileCtrl;
const
DOCHOSTUIFLAG_DPI_AWARE = $40000000;
IID_IDownloadManager: TGUID = '{988934A4-064B-11D3-BB80-00104B35E7F9}';
SID_IDownloadManager: TGUID = '{988934A4-064B-11D3-BB80-00104B35E7F9}';
type
//https://stackoverflow.com/a/13389595/537347
IDownloadManager = interface(IUnknown)
['{988934A4-064B-11D3-BB80-00104B35E7F9}']
function Download(pmk: IMoniker; pbc: IBindCtx; dwBindVerb: DWORD;
grfBINDF: DWORD; pBindInfo: PBindInfo; pszHeaders: PWideChar;
pszRedir: PWideChar; uiCP: UINT): HRESULT; stdcall;
end;
TBeforeFileDownloadEvent = procedure(Sender: TObject; const FileSource: WideString;
var Allowed: Boolean) of Object;
TWebBrowser = class(SHDocVw.TWebBrowser, IServiceProvider, IDownloadManager, IDocHostUIHandler)
private
FFileSource: WideString;
FOnBeforeFileDownload: TBeforeFileDownloadEvent;
function QueryService(const rsid, iid: TGUID; out Obj): HRESULT; stdcall;
function Download(pmk: IMoniker; pbc: IBindCtx; dwBindVerb: DWORD;
grfBINDF: DWORD; pBindInfo: PBindInfo; pszHeaders: PWideChar;
pszRedir: PWideChar; uiCP: UINT): HRESULT; stdcall;
// handling special chars
procedure CNChar(var Msg: TWMChar); message CN_CHAR;
// making webbrowser DPI aware https://stackoverflow.com/a/63810030
function GetHostInfo(var pInfo: TDocHostUIInfo): HRESULT; stdcall;
protected
procedure InvokeEvent(ADispID: TDispID; var AParams: TDispParams); override;
published
property OnBeforeFileDownload: TBeforeFileDownloadEvent read FOnBeforeFileDownload write FOnBeforeFileDownload;
end;
TfrmWeb = class(TForm)
WebBrowser1: TWebBrowser;
UWPDownloader1: TUWPDownloader;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
FileListBox1: TFileListBox;
procedure FormCreate(Sender: TObject);
procedure UWPDownloader1Downloaded(Sender: TObject; DownloadCode: Integer);
procedure WebBrowser1NewWindow2(ASender: TObject; var ppDisp: IDispatch;
var Cancel: WordBool);
procedure ListView1DblClick(Sender: TObject);
procedure VirtualExplorerListview1DblClick(Sender: TObject);
procedure FileListBox1DblClick(Sender: TObject);
procedure WebBrowser1DocumentComplete(ASender: TObject;
const pDisp: IDispatch; const URL: OleVariant);
private
{ Private declarations }
procedure BeforeFileDownload(Sender: TObject; const FileSource: WideString;
var Allowed: Boolean);
function getAPKversion: string;
public
{ Public declarations }
end;
var
frmWeb: TfrmWeb;
implementation
uses
System.Net.HttpClient, {IdHTTP,} helperFuncs,
frmApkInstaller, MSHTML;
{$R *.dfm}
procedure TfrmWeb.BeforeFileDownload(Sender: TObject;
const FileSource: WideString; var Allowed: Boolean);
function GetAttachmentFilename(CD: string): string;
var
fq, sq: Integer;
begin
Result := 'fileattachment';
if CD.Contains('filename') then // e.g. Content-Disposition returns: attachment; filename="filename.ext"
begin
fq := Pos('"', CD);
sq := Pos('"', CD, fq + 1);
if (fq > 0) and (sq > 0) and (sq > fq) then
begin
Result := Copy(CD, fq + 1, sq - fq - 1);
end;
end;
end;
var
FileTarget: string;
LUrl: string;
// Http: TIdHTTP;
Client: THTTPClient;
Response: IHTTPResponse;
begin
Allowed := False;
// ShowMessage(FileSource);
LUrl := Trim(FileSource);
if LUrl = '' then Exit;
Var domain := ExtractDomain(LUrl);
Client := THTTPClient.Create;
// Http := TIdHTTP.Create(nil);
try
try
Response := Client.Head(LUrl, nil);
except
end;
// Http.Head(LUrl);
if Assigned(Response) and (Response.StatusCode = 200) and (Response.ContentLength > 0) then
begin
var fName := GetAttachmentFilename(Response.HeaderValue['Content-Disposition']); //Http.Response.RawHeaders.Params['Content-Disposition', 'filename'];
var Prompt := Format('You chose to download: '#13#10'%s '#13#10'Size: %s '#13#10'From %s. '#13#10''#13#10'Continue?',
[
fName,
FormatFileSize(Response.ContentLength),//Http.Response.ContentLength),
domain
]);
if MessageDlg(Prompt,TMsgDlgType.mtConfirmation, mbYesNo, 0) = mrYes then
begin
UWPDownloader1.SavePath := ExtractFilePath(ParamStr(0)) + 'Downloads\' + fName;
UWPDownloader1.Detail := fName;
UWPDownloader1.URL := LUrl;
UWPDownloader1.DoStartDownload;
// PageControl1.SelectNextPage(True);
end;
end;
finally
Client.Free;
// Http.Free;
end;
end;
procedure TfrmWeb.FileListBox1DblClick(Sender: TObject);
begin
frmInstaller.FApkFile := FileListBox1.Items[FileListBox1.ItemIndex];
frmInstaller.Show;
frmInstaller.FApkInfo.DisplayName := '';
frmInstaller.FApkInfo.DisplayVersion := '';
frmInstaller.FApkInfo.PackageName := '';
frmInstaller.FApkInfo.Icon := '';
frmInstaller.FApkPermissions.Clear;
if LowerCase(FileListBox1.Items[FileListBox1.ItemIndex]).Contains('.xapk') then
frmInstaller.GetXAPKInfo
else
frmInstaller.GetAPKInfoWithAndroidAssetPackagingTool;
end;
procedure TfrmWeb.FormCreate(Sender: TObject);
begin
WebBrowser1.Silent := True;
WebBrowser1.OnBeforeFileDownload := BeforeFileDownload;
if DirectoryExists(ExtractFilePath(ParamStr(0))+'Downloads') then
begin
FileListBox1.Directory := ExtractFilePath(ParamStr(0))+'Downloads';
end;
end;
function TfrmWeb.getAPKversion: string;
begin
result := OleVariant((WebBrowser1.Document as IHTMLDocument2).parentWindow.document).page_config.info.version_name;
end;
procedure TfrmWeb.ListView1DblClick(Sender: TObject);
begin
// frmInstaller.FApkFile := ListView1.Items[ListView1.ItemIndex].;
frmInstaller.Show;
frmInstaller.FApkInfo.DisplayName := '';
frmInstaller.FApkInfo.DisplayVersion := '';
frmInstaller.FApkInfo.PackageName := '';
frmInstaller.FApkInfo.Icon := '';
frmInstaller.FApkPermissions.Clear;
frmInstaller.GetAPKInfoWithAndroidAssetPackagingTool;
end;
procedure TfrmWeb.UWPDownloader1Downloaded(Sender: TObject;
DownloadCode: Integer);
begin
ShowMessage('File Downloaded!');
end;
procedure TfrmWeb.VirtualExplorerListview1DblClick(Sender: TObject);
begin
end;
// prevent opening links in MSEdge
procedure TfrmWeb.WebBrowser1DocumentComplete(ASender: TObject;
const pDisp: IDispatch; const URL: OleVariant);
begin
if string(URL).Contains('apkpure.com/en/') then
Caption := 'Latest version: ' + getAPKversion;
end;
procedure TfrmWeb.WebBrowser1NewWindow2(ASender: TObject; var ppDisp: IDispatch;
var Cancel: WordBool);
begin
Cancel := True;
end;
{ TWebBrowser }
procedure TWebBrowser.CNChar(var Msg: TWMChar);
begin
Msg.Result := 0;
end;
function TWebBrowser.Download(pmk: IMoniker; pbc: IBindCtx; dwBindVerb,
grfBINDF: DWORD; pBindInfo: PBindInfo; pszHeaders, pszRedir: PWideChar;
uiCP: UINT): HRESULT;
var
Allowed: Boolean;
begin
Result := E_NOTIMPL;
if Assigned(FOnBeforeFileDownload) then
begin
Allowed := True;
if pszRedir <> '' then
FFileSource := pszRedir;
FOnBeforeFileDownload(Self, FFileSource, Allowed);
if not Allowed then
Result := S_OK;
end;
end;
function TWebBrowser.GetHostInfo(var pInfo: TDocHostUIInfo): HRESULT;
begin
// original code
pInfo.cbSize := SizeOf(pInfo);
pInfo.dwFlags := 0;
pInfo.dwFlags := pInfo.dwFlags or DOCHOSTUIFLAG_NO3DBORDER;
pInfo.dwFlags := pInfo.dwFlags or DOCHOSTUIFLAG_THEME;
pInfo.dwFlags := pInfo.dwFlags or DOCHOSTUIFLAG_DPI_AWARE; // NEW added flag
Result := S_OK;
// ResizeScrollBars; // will be called by subsequent routines anyway
end;
procedure TWebBrowser.InvokeEvent(ADispID: TDispID; var AParams: TDispParams);
begin
inherited;
/// DispID 250 is the BeforeNavigatte2 dispinterface and to the FFileSource here
/// is stored the URL parameter (for cases, when the IDownloaderManager::Download
/// won't redirect the URL and pass empty string to the pszRedir)
if ADispID = 250 then
FFileSource := OleVariant(AParams.rgvarg^[5]);
end;
function TWebBrowser.QueryService(const rsid, iid: TGUID; out Obj): HRESULT;
begin
Result := E_NOINTERFACE;
Pointer(Obj) := nil;
if Assigned(FOnBeforeFileDownload) and IsEqualCLSID(rsid, SID_IDownloadManager) and
IsEqualIID(iid, IID_IDownloadManager) then
begin
if Succeeded(QueryInterface(IID_IDownloadManager, Obj)) and
Assigned(Pointer(Obj))
then
Result := S_OK;
end;
end;
end.