1 /* PROJECT: ReactOS Applications Manager
2 * LICENSE: GPL - See COPYING in the top level directory
3 * FILE: base/applications/rapps/loaddlg.cpp
4 * PURPOSE: Displaying a download dialog
5 * COPYRIGHT: Copyright 2001 John R. Sheets (for CodeWeavers)
6 * Copyright 2004 Mike McCormack (for CodeWeavers)
7 * Copyright 2005 Ge van Geldorp (gvg@reactos.org)
8 * Copyright 2009 Dmitry Chapyshev (dmitry@reactos.org)
9 * Copyright 2015 Ismael Ferreras Morezuelas (swyterzone+ros@gmail.com)
10 * Copyright 2017 Alexander Shaposhnikov (chaez.san@gmail.com)
14 * Based on Wine dlls/shdocvw/shdocvw_main.c
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License as published by the Free Software Foundation; either
19 * version 2.1 of the License, or (at your option) any later version.
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this library; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33 #include <shlobj_undoc.h>
34 #include <shlguid_undoc.h>
39 #include <shellutils.h>
42 static PAPPLICATION_INFO AppInfo
;
44 class CDownloadDialog
:
45 public CComObjectRootEx
<CComMultiThreadModelNoCS
>,
46 public IBindStatusCallback
50 BOOL m_UrlHasBeenCopied
;
55 DestroyWindow(m_hDialog
);
58 HRESULT
Initialize(HWND Dlg
, BOOL
*pbCancelled
)
61 m_pbCancelled
= pbCancelled
;
62 m_UrlHasBeenCopied
= FALSE
;
66 virtual HRESULT STDMETHODCALLTYPE
OnStartBinding(
73 virtual HRESULT STDMETHODCALLTYPE
GetPriority(
79 virtual HRESULT STDMETHODCALLTYPE
OnLowResource(
85 virtual HRESULT STDMETHODCALLTYPE
OnProgress(
94 Item
= GetDlgItem(m_hDialog
, IDC_DOWNLOAD_PROGRESS
);
95 if (Item
&& ulProgressMax
)
97 WCHAR szProgress
[100];
98 WCHAR szProgressMax
[100];
99 UINT uiPercentage
= ((ULONGLONG
) ulProgress
* 100) / ulProgressMax
;
101 /* send the current progress to the progress bar */
102 SendMessageW(Item
, PBM_SETPOS
, uiPercentage
, 0);
104 /* format the bits and bytes into pretty and accessible units... */
105 StrFormatByteSizeW(ulProgress
, szProgress
, _countof(szProgress
));
106 StrFormatByteSizeW(ulProgressMax
, szProgressMax
, _countof(szProgressMax
));
108 /* ...and post all of it to our subclassed progress bar text subroutine */
109 ATL::CStringW m_ProgressText
;
110 m_ProgressText
.Format(L
"%u%% \x2014 %ls / %ls",
114 SendMessageW(Item
, WM_SETTEXT
, 0, (LPARAM
) m_ProgressText
.GetBuffer());
115 m_ProgressText
.ReleaseBuffer();
118 Item
= GetDlgItem(m_hDialog
, IDC_DOWNLOAD_STATUS
);
119 if (Item
&& szStatusText
&& wcslen(szStatusText
) > 0 && m_UrlHasBeenCopied
== FALSE
)
121 DWORD len
= wcslen(szStatusText
) + 1;
124 /* beautify our url for display purposes */
125 if (!InternetCanonicalizeUrlW(szStatusText
, buf
.GetBuffer(len
), &len
, ICU_DECODE
| ICU_NO_ENCODE
))
127 /* just use the original */
136 /* paste it into our dialog and don't do it again in this instance */
137 SendMessageW(Item
, WM_SETTEXT
, 0, (LPARAM
) buf
.GetBuffer());
139 m_UrlHasBeenCopied
= TRUE
;
143 r
= GetWindowLongPtrW(m_hDialog
, GWLP_USERDATA
);
144 if (0 != r
|| 0 != GetLastError())
146 *m_pbCancelled
= TRUE
;
153 virtual HRESULT STDMETHODCALLTYPE
OnStopBinding(
160 virtual HRESULT STDMETHODCALLTYPE
GetBindInfo(
167 virtual HRESULT STDMETHODCALLTYPE
OnDataAvailable(
170 FORMATETC
*pformatetc
,
176 virtual HRESULT STDMETHODCALLTYPE
OnObjectAvailable(
183 BEGIN_COM_MAP(CDownloadDialog
)
184 COM_INTERFACE_ENTRY_IID(IID_IBindStatusCallback
, IBindStatusCallback
)
189 HRESULT WINAPI
CDownloadDialog_Constructor(HWND Dlg
, BOOL
*pbCancelled
, REFIID riid
, LPVOID
*ppv
)
191 return ShellObjectCreatorInit
<CDownloadDialog
>(Dlg
, pbCancelled
, riid
, ppv
);
194 #ifdef USE_CERT_PINNING
195 static BOOL
CertIsValid(HINTERNET hInternet
, LPWSTR lpszHostName
)
199 DWORD certInfoLength
;
201 INTERNET_CERTIFICATE_INFOW certInfo
;
203 hConnect
= InternetConnectW(hInternet
, lpszHostName
, INTERNET_DEFAULT_HTTPS_PORT
, NULL
, NULL
, INTERNET_SERVICE_HTTP
, INTERNET_FLAG_SECURE
, 0);
206 hRequest
= HttpOpenRequestW(hConnect
, L
"HEAD", NULL
, NULL
, NULL
, NULL
, INTERNET_FLAG_SECURE
, 0);
207 if (hRequest
!= NULL
)
209 Ret
= HttpSendRequestW(hRequest
, L
"", 0, NULL
, 0);
212 certInfoLength
= sizeof(certInfo
);
213 Ret
= InternetQueryOptionW(hRequest
,
214 INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT
,
219 if (certInfo
.lpszEncryptionAlgName
)
220 LocalFree(certInfo
.lpszEncryptionAlgName
);
221 if (certInfo
.lpszIssuerInfo
)
223 if (strcmp((LPSTR
) certInfo
.lpszIssuerInfo
, CERT_ISSUER_INFO
) != 0)
225 LocalFree(certInfo
.lpszIssuerInfo
);
227 if (certInfo
.lpszProtocolName
)
228 LocalFree(certInfo
.lpszProtocolName
);
229 if (certInfo
.lpszSignatureAlgName
)
230 LocalFree(certInfo
.lpszSignatureAlgName
);
231 if (certInfo
.lpszSubjectInfo
)
233 if (strcmp((LPSTR
) certInfo
.lpszSubjectInfo
, CERT_SUBJECT_INFO
) != 0)
235 LocalFree(certInfo
.lpszSubjectInfo
);
239 InternetCloseHandle(hRequest
);
241 InternetCloseHandle(hConnect
);
248 MessageBox_LoadString(HWND hMainWnd
, INT StringID
)
250 ATL::CString szMsgText
;
251 if (szMsgText
.LoadStringW(hInst
, StringID
))
252 MessageBoxW(hMainWnd
, szMsgText
.GetString(), NULL
, MB_OK
| MB_ICONERROR
);
257 ThreadFunc(LPVOID Context
)
259 CComPtr
<IBindStatusCallback
> dl
;
262 HWND Dlg
= (HWND
) Context
;
263 ULONG dwContentLen
, dwBytesWritten
, dwBytesRead
, dwStatus
;
264 ULONG dwCurrentBytesRead
= 0;
265 ULONG dwStatusLen
= sizeof(dwStatus
);
266 BOOL bCancelled
= FALSE
;
267 BOOL bTempfile
= FALSE
;
269 HINTERNET hOpen
= NULL
;
270 HINTERNET hFile
= NULL
;
271 HANDLE hOut
= INVALID_HANDLE_VALUE
;
272 unsigned char lpBuffer
[4096];
273 LPCWSTR lpszAgent
= L
"RApps/1.0";
274 URL_COMPONENTS urlComponents
;
275 size_t urlLength
, filenameLength
;
277 /* build the path for the download */
278 p
= wcsrchr(AppInfo
->szUrlDownload
, L
'/');
279 q
= wcsrchr(AppInfo
->szUrlDownload
, L
'?');
281 /* do we have a final slash separator? */
285 /* prepare the tentative length of the filename, maybe we've to remove part of it later on */
286 filenameLength
= wcslen(p
) * sizeof(WCHAR
);
288 /* do we have query arguments in the target URL after the filename? account for them
289 (e.g. https://example.org/myfile.exe?no_adware_plz) */
290 if (q
&& q
> p
&& (q
- p
) > 0)
291 filenameLength
-= wcslen(q
- 1) * sizeof(WCHAR
);
293 /* is this URL an update package for RAPPS? if so store it in a different place */
294 if (AppInfo
->szUrlDownload
== APPLICATION_DATABASE_URL
)
297 if (!GetStorageDirectory(Path
))
302 Path
= SettingsInfo
.szDownloadDir
;
305 /* is the path valid? can we access it? */
306 if (GetFileAttributesW(Path
.GetString()) == INVALID_FILE_ATTRIBUTES
)
308 if (!CreateDirectoryW(Path
.GetString(), NULL
))
312 /* append a \ to the provided file system path, and the filename portion from the URL after that */
313 Path
.Format(L
"\\%ls", (p
+ 1));
315 if (!bCab
&& AppInfo
->szSHA1
[0] && GetFileAttributesW(Path
.GetString()) != INVALID_FILE_ATTRIBUTES
)
317 /* only open it in case of total correctness */
318 if (VerifyInteg(AppInfo
->szSHA1
, Path
))
324 CDownloadDialog_Constructor(Dlg
, &bCancelled
, IID_PPV_ARG(IBindStatusCallback
, &dl
));
329 /* FIXME: this should just be using the system-wide proxy settings */
330 switch (SettingsInfo
.Proxy
)
332 case 0: /* preconfig */
333 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, 0);
335 case 1: /* direct (no proxy) */
336 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_DIRECT
, NULL
, NULL
, 0);
338 case 2: /* use proxy */
339 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PROXY
, SettingsInfo
.szProxyServer
, SettingsInfo
.szNoProxyFor
, 0);
341 default: /* preconfig */
342 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, 0);
349 hFile
= InternetOpenUrlW(hOpen
, AppInfo
->szUrlDownload
, NULL
, 0, INTERNET_FLAG_PRAGMA_NOCACHE
| INTERNET_FLAG_KEEP_CONNECTION
, 0);
352 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD2
);
356 if (!HttpQueryInfoW(hFile
, HTTP_QUERY_STATUS_CODE
| HTTP_QUERY_FLAG_NUMBER
, &dwStatus
, &dwStatusLen
, NULL
))
359 if (dwStatus
!= HTTP_STATUS_OK
)
361 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD
);
365 dwStatusLen
= sizeof(dwStatus
);
367 memset(&urlComponents
, 0, sizeof(urlComponents
));
368 urlComponents
.dwStructSize
= sizeof(urlComponents
);
370 urlLength
= AppInfo
->szUrlDownload
.GetLength();
371 urlComponents
.dwSchemeLength
= urlLength
+ 1;
372 urlComponents
.lpszScheme
= (LPWSTR
) malloc(urlComponents
.dwSchemeLength
* sizeof(WCHAR
));
373 urlComponents
.dwHostNameLength
= urlLength
+ 1;
374 urlComponents
.lpszHostName
= (LPWSTR
) malloc(urlComponents
.dwHostNameLength
* sizeof(WCHAR
));
376 if (!InternetCrackUrlW(AppInfo
->szUrlDownload
, urlLength
+ 1, ICU_DECODE
| ICU_ESCAPE
, &urlComponents
))
379 if (urlComponents
.nScheme
== INTERNET_SCHEME_HTTP
|| urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
)
380 HttpQueryInfo(hFile
, HTTP_QUERY_CONTENT_LENGTH
| HTTP_QUERY_FLAG_NUMBER
, &dwContentLen
, &dwStatus
, 0);
382 if (urlComponents
.nScheme
== INTERNET_SCHEME_FTP
)
383 dwContentLen
= FtpGetFileSize(hFile
, &dwStatus
);
385 #ifdef USE_CERT_PINNING
386 /* are we using HTTPS to download the RAPPS update package? check if the certificate is original */
387 if ((urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
) &&
388 (wcscmp(AppInfo
->szUrlDownload
, APPLICATION_DATABASE_URL
) == 0) &&
389 (!CertIsValid(hOpen
, urlComponents
.lpszHostName
)))
391 MessageBox_LoadString(hMainWnd
, IDS_CERT_DOES_NOT_MATCH
);
396 free(urlComponents
.lpszScheme
);
397 free(urlComponents
.lpszHostName
);
399 hOut
= CreateFileW(Path
.GetString(), GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, CREATE_ALWAYS
, 0, NULL
);
401 if (hOut
== INVALID_HANDLE_VALUE
)
406 if (!InternetReadFile(hFile
, lpBuffer
, _countof(lpBuffer
), &dwBytesRead
))
408 MessageBox_LoadString(hMainWnd
, IDS_INTERRUPTED_DOWNLOAD
);
412 if (!WriteFile(hOut
, &lpBuffer
[0], dwBytesRead
, &dwBytesWritten
, NULL
))
414 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_WRITE
);
418 dwCurrentBytesRead
+= dwBytesRead
;
419 dl
->OnProgress(dwCurrentBytesRead
, dwContentLen
, 0, AppInfo
->szUrlDownload
);
420 } while (dwBytesRead
&& !bCancelled
);
423 hOut
= INVALID_HANDLE_VALUE
;
428 /* if this thing isn't a RAPPS update and it has a SHA-1 checksum
429 verify its integrity by using the native advapi32.A_SHA1 functions */
430 if (!bCab
&& AppInfo
->szSHA1
[0] != 0)
432 ATL::CStringW szMsgText
;
434 /* change a few strings in the download dialog to reflect the verification process */
435 if (!szMsgText
.LoadStringW(hInst
, IDS_INTEG_CHECK_TITLE
))
438 SetWindowText(Dlg
, szMsgText
.GetString());
439 SendMessageW(GetDlgItem(Dlg
, IDC_DOWNLOAD_STATUS
), WM_SETTEXT
, 0, (LPARAM
) Path
.GetBuffer());
440 Path
.ReleaseBuffer();
442 /* this may take a while, depending on the file size */
443 if (!VerifyInteg(AppInfo
->szSHA1
, Path
.GetString()))
445 if (!szMsgText
.LoadStringW(hInst
, IDS_INTEG_CHECK_FAIL
))
448 MessageBoxW(Dlg
, szMsgText
.GetString(), NULL
, MB_OK
| MB_ICONERROR
);
453 ShowWindow(Dlg
, SW_HIDE
);
458 ShellExecuteW(NULL
, L
"open", Path
.GetString(), NULL
, NULL
, SW_SHOWNORMAL
);
461 if (hOut
!= INVALID_HANDLE_VALUE
)
464 InternetCloseHandle(hFile
);
465 InternetCloseHandle(hOpen
);
469 if (bCancelled
|| (SettingsInfo
.bDelInstaller
&& !bCab
))
470 DeleteFileW(Path
.GetString());
480 DownloadProgressProc(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
, UINT_PTR uIdSubclass
, DWORD_PTR dwRefData
)
482 static ATL::CStringW szProgressText
;
490 szProgressText
= (PCWSTR
) lParam
;
499 HDC hDC
= BeginPaint(hWnd
, &ps
), hdcMem
;
503 UINT win_width
, win_height
;
505 GetClientRect(hWnd
, &myRect
);
507 /* grab the progress bar rect size */
508 win_width
= myRect
.right
- myRect
.left
;
509 win_height
= myRect
.bottom
- myRect
.top
;
511 /* create an off-screen DC for double-buffering */
512 hdcMem
= CreateCompatibleDC(hDC
);
513 hbmMem
= CreateCompatibleBitmap(hDC
, win_width
, win_height
);
515 hOld
= SelectObject(hdcMem
, hbmMem
);
517 /* call the original draw code and redirect it to our memory buffer */
518 DefSubclassProc(hWnd
, uMsg
, (WPARAM
) hdcMem
, lParam
);
520 /* draw our nifty progress text over it */
521 SelectFont(hdcMem
, GetStockFont(DEFAULT_GUI_FONT
));
522 DrawShadowText(hdcMem
, szProgressText
.GetString(), szProgressText
.GetLength(),
524 DT_CENTER
| DT_VCENTER
| DT_NOPREFIX
| DT_SINGLELINE
,
525 GetSysColor(COLOR_CAPTIONTEXT
),
526 GetSysColor(COLOR_3DSHADOW
),
529 /* transfer the off-screen DC to the screen */
530 BitBlt(hDC
, 0, 0, win_width
, win_height
, hdcMem
, 0, 0, SRCCOPY
);
532 /* free the off-screen DC */
533 SelectObject(hdcMem
, hOld
);
534 DeleteObject(hbmMem
);
541 /* Raymond Chen says that we should safely unsubclass all the things!
542 (http://blogs.msdn.com/b/oldnewthing/archive/2003/11/11/55653.aspx) */
545 szProgressText
.Empty();
546 RemoveWindowSubclass(hWnd
, DownloadProgressProc
, uIdSubclass
);
550 return DefSubclassProc(hWnd
, uMsg
, wParam
, lParam
);
556 DownloadDlgProc(HWND Dlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
566 HICON hIconSm
= NULL
, hIconBg
= NULL
;
568 hIconBg
= (HICON
) GetClassLongPtr(hMainWnd
, GCLP_HICON
);
569 hIconSm
= (HICON
) GetClassLongPtr(hMainWnd
, GCLP_HICONSM
);
571 if (hIconBg
&& hIconSm
)
573 SendMessageW(Dlg
, WM_SETICON
, ICON_BIG
, (LPARAM
) hIconBg
);
574 SendMessageW(Dlg
, WM_SETICON
, ICON_SMALL
, (LPARAM
) hIconSm
);
577 SetWindowLongPtrW(Dlg
, GWLP_USERDATA
, 0);
578 Item
= GetDlgItem(Dlg
, IDC_DOWNLOAD_PROGRESS
);
581 /* initialize the default values for our nifty progress bar
582 and subclass it so that it learns to print a status text */
583 SendMessageW(Item
, PBM_SETRANGE
, 0, MAKELPARAM(0, 100));
584 SendMessageW(Item
, PBM_SETPOS
, 0, 0);
586 SetWindowSubclass(Item
, DownloadProgressProc
, 0, 0);
589 /* add a neat placeholder until the download URL is retrieved */
590 Item
= GetDlgItem(Dlg
, IDC_DOWNLOAD_STATUS
);
591 SendMessageW(Item
, WM_SETTEXT
, 0, (LPARAM
) L
"\x2022 \x2022 \x2022");
593 Thread
= CreateThread(NULL
, 0, ThreadFunc
, Dlg
, 0, &ThreadId
);
600 if (wParam
== IDCANCEL
)
602 SetWindowLongPtrW(Dlg
, GWLP_USERDATA
, 1);
603 PostMessageW(Dlg
, WM_CLOSE
, 0, 0);
617 DownloadApplication(INT Index
)
619 if (!IS_AVAILABLE_ENUM(SelectedEnumType
))
622 AppInfo
= (PAPPLICATION_INFO
) ListViewGetlParam(Index
);
626 WriteLogMessage(EVENTLOG_SUCCESS
, MSG_SUCCESS_INSTALL
, AppInfo
->szName
.GetString());
629 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
),
637 DownloadApplicationsDB(LPCWSTR lpUrl
)
639 APPLICATION_INFO IntInfo
;
640 IntInfo
.szUrlDownload
= lpUrl
;
645 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
),