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 class CDownloadDialog
:
43 public CComObjectRootEx
<CComMultiThreadModelNoCS
>,
44 public IBindStatusCallback
48 BOOL m_UrlHasBeenCopied
;
53 DestroyWindow(m_hDialog
);
56 HRESULT
Initialize(HWND Dlg
, BOOL
*pbCancelled
)
59 m_pbCancelled
= pbCancelled
;
60 m_UrlHasBeenCopied
= FALSE
;
64 virtual HRESULT STDMETHODCALLTYPE
OnStartBinding(
71 virtual HRESULT STDMETHODCALLTYPE
GetPriority(
77 virtual HRESULT STDMETHODCALLTYPE
OnLowResource(
83 virtual HRESULT STDMETHODCALLTYPE
OnProgress(
92 Item
= GetDlgItem(m_hDialog
, IDC_DOWNLOAD_PROGRESS
);
93 if (Item
&& ulProgressMax
)
95 WCHAR szProgress
[100];
96 WCHAR szProgressMax
[100];
97 UINT uiPercentage
= ((ULONGLONG
) ulProgress
* 100) / ulProgressMax
;
99 /* send the current progress to the progress bar */
100 SendMessageW(Item
, PBM_SETPOS
, uiPercentage
, 0);
102 /* format the bits and bytes into pretty and accessible units... */
103 StrFormatByteSizeW(ulProgress
, szProgress
, _countof(szProgress
));
104 StrFormatByteSizeW(ulProgressMax
, szProgressMax
, _countof(szProgressMax
));
106 /* ...and post all of it to our subclassed progress bar text subroutine */
107 ATL::CStringW m_ProgressText
;
108 m_ProgressText
.Format(L
"%u%% \x2014 %ls / %ls",
112 SendMessageW(Item
, WM_SETTEXT
, 0, (LPARAM
) m_ProgressText
.GetString());
115 Item
= GetDlgItem(m_hDialog
, IDC_DOWNLOAD_STATUS
);
116 if (Item
&& szStatusText
&& wcslen(szStatusText
) > 0 && m_UrlHasBeenCopied
== FALSE
)
118 DWORD len
= wcslen(szStatusText
) + 1;
121 /* beautify our url for display purposes */
122 if (!InternetCanonicalizeUrlW(szStatusText
, buf
.GetBuffer(len
), &len
, ICU_DECODE
| ICU_NO_ENCODE
))
124 /* just use the original */
133 /* paste it into our dialog and don't do it again in this instance */
134 SendMessageW(Item
, WM_SETTEXT
, 0, (LPARAM
) buf
.GetString());
135 m_UrlHasBeenCopied
= TRUE
;
139 r
= GetWindowLongPtrW(m_hDialog
, GWLP_USERDATA
);
140 if (0 != r
|| 0 != GetLastError())
142 *m_pbCancelled
= TRUE
;
149 virtual HRESULT STDMETHODCALLTYPE
OnStopBinding(
156 virtual HRESULT STDMETHODCALLTYPE
GetBindInfo(
163 virtual HRESULT STDMETHODCALLTYPE
OnDataAvailable(
166 FORMATETC
*pformatetc
,
172 virtual HRESULT STDMETHODCALLTYPE
OnObjectAvailable(
179 BEGIN_COM_MAP(CDownloadDialog
)
180 COM_INTERFACE_ENTRY_IID(IID_IBindStatusCallback
, IBindStatusCallback
)
185 HRESULT WINAPI
CDownloadDialog_Constructor(HWND Dlg
, BOOL
*pbCancelled
, REFIID riid
, LPVOID
*ppv
)
187 return ShellObjectCreatorInit
<CDownloadDialog
>(Dlg
, pbCancelled
, riid
, ppv
);
190 #ifdef USE_CERT_PINNING
191 static BOOL
CertIsValid(HINTERNET hInternet
, LPWSTR lpszHostName
)
195 DWORD certInfoLength
;
197 INTERNET_CERTIFICATE_INFOW certInfo
;
199 hConnect
= InternetConnectW(hInternet
, lpszHostName
, INTERNET_DEFAULT_HTTPS_PORT
, NULL
, NULL
, INTERNET_SERVICE_HTTP
, INTERNET_FLAG_SECURE
, 0);
202 hRequest
= HttpOpenRequestW(hConnect
, L
"HEAD", NULL
, NULL
, NULL
, NULL
, INTERNET_FLAG_SECURE
, 0);
203 if (hRequest
!= NULL
)
205 Ret
= HttpSendRequestW(hRequest
, L
"", 0, NULL
, 0);
208 certInfoLength
= sizeof(certInfo
);
209 Ret
= InternetQueryOptionW(hRequest
,
210 INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT
,
215 if (certInfo
.lpszEncryptionAlgName
)
216 LocalFree(certInfo
.lpszEncryptionAlgName
);
217 if (certInfo
.lpszIssuerInfo
)
219 if (strcmp((LPSTR
) certInfo
.lpszIssuerInfo
, CERT_ISSUER_INFO
) != 0)
221 LocalFree(certInfo
.lpszIssuerInfo
);
223 if (certInfo
.lpszProtocolName
)
224 LocalFree(certInfo
.lpszProtocolName
);
225 if (certInfo
.lpszSignatureAlgName
)
226 LocalFree(certInfo
.lpszSignatureAlgName
);
227 if (certInfo
.lpszSubjectInfo
)
229 if (strcmp((LPSTR
) certInfo
.lpszSubjectInfo
, CERT_SUBJECT_INFO
) != 0)
231 LocalFree(certInfo
.lpszSubjectInfo
);
235 InternetCloseHandle(hRequest
);
237 InternetCloseHandle(hConnect
);
244 MessageBox_LoadString(HWND hMainWnd
, INT StringID
)
246 ATL::CString szMsgText
;
247 if (szMsgText
.LoadStringW(hInst
, StringID
))
248 MessageBoxW(hMainWnd
, szMsgText
.GetString(), NULL
, MB_OK
| MB_ICONERROR
);
253 PAPPLICATION_INFO
DownloadManager::AppInfo
= NULL
;
255 INT_PTR CALLBACK
DownloadManager::DownloadDlgProc(HWND Dlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
265 HICON hIconSm
, hIconBg
;
266 WCHAR szCaption
[MAX_PATH
];
267 ATL::CStringW szNewCaption
;
269 hIconBg
= (HICON
) GetClassLongW(hMainWnd
, GCLP_HICON
);
270 hIconSm
= (HICON
) GetClassLongW(hMainWnd
, GCLP_HICONSM
);
272 if (hIconBg
&& hIconSm
)
274 SendMessageW(Dlg
, WM_SETICON
, ICON_BIG
, (LPARAM
) hIconBg
);
275 SendMessageW(Dlg
, WM_SETICON
, ICON_SMALL
, (LPARAM
) hIconSm
);
278 // Change caption to show the currently downloaded app
279 GetWindowTextW(Dlg
, szCaption
, MAX_PATH
);
280 szNewCaption
.Format(szCaption
, AppInfo
->szName
.GetString());
281 SetWindowTextW(Dlg
, szNewCaption
.GetString());
283 SetWindowLongW(Dlg
, GWLP_USERDATA
, 0);
284 Item
= GetDlgItem(Dlg
, IDC_DOWNLOAD_PROGRESS
);
287 // initialize the default values for our nifty progress bar
288 // and subclass it so that it learns to print a status text
289 SendMessageW(Item
, PBM_SETRANGE
, 0, MAKELPARAM(0, 100));
290 SendMessageW(Item
, PBM_SETPOS
, 0, 0);
292 SetWindowSubclass(Item
, DownloadProgressProc
, 0, 0);
295 // add a neat placeholder until the download URL is retrieved
296 SetDlgItemTextW(Dlg
, IDC_DOWNLOAD_STATUS
, L
"\x2022 \x2022 \x2022");
298 Thread
= CreateThread(NULL
, 0, ThreadFunc
, Dlg
, 0, &ThreadId
);
305 if (wParam
== IDCANCEL
)
307 SetWindowLongPtrW(Dlg
, GWLP_USERDATA
, 1);
308 PostMessageW(Dlg
, WM_CLOSE
, 0, 0);
321 LRESULT CALLBACK
DownloadManager::DownloadProgressProc(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
, UINT_PTR uIdSubclass
, DWORD_PTR dwRefData
)
323 static ATL::CStringW szProgressText
;
331 szProgressText
= (PCWSTR
) lParam
;
340 HDC hDC
= BeginPaint(hWnd
, &ps
), hdcMem
;
344 UINT win_width
, win_height
;
346 GetClientRect(hWnd
, &myRect
);
348 /* grab the progress bar rect size */
349 win_width
= myRect
.right
- myRect
.left
;
350 win_height
= myRect
.bottom
- myRect
.top
;
352 /* create an off-screen DC for double-buffering */
353 hdcMem
= CreateCompatibleDC(hDC
);
354 hbmMem
= CreateCompatibleBitmap(hDC
, win_width
, win_height
);
356 hOld
= SelectObject(hdcMem
, hbmMem
);
358 /* call the original draw code and redirect it to our memory buffer */
359 DefSubclassProc(hWnd
, uMsg
, (WPARAM
) hdcMem
, lParam
);
361 /* draw our nifty progress text over it */
362 SelectFont(hdcMem
, GetStockFont(DEFAULT_GUI_FONT
));
363 DrawShadowText(hdcMem
, szProgressText
.GetString(), szProgressText
.GetLength(),
365 DT_CENTER
| DT_VCENTER
| DT_NOPREFIX
| DT_SINGLELINE
,
366 GetSysColor(COLOR_CAPTIONTEXT
),
367 GetSysColor(COLOR_3DSHADOW
),
370 /* transfer the off-screen DC to the screen */
371 BitBlt(hDC
, 0, 0, win_width
, win_height
, hdcMem
, 0, 0, SRCCOPY
);
373 /* free the off-screen DC */
374 SelectObject(hdcMem
, hOld
);
375 DeleteObject(hbmMem
);
382 /* Raymond Chen says that we should safely unsubclass all the things!
383 (http://blogs.msdn.com/b/oldnewthing/archive/2003/11/11/55653.aspx) */
386 szProgressText
.Empty();
387 RemoveWindowSubclass(hWnd
, DownloadProgressProc
, uIdSubclass
);
391 return DefSubclassProc(hWnd
, uMsg
, wParam
, lParam
);
395 DWORD WINAPI
DownloadManager::ThreadFunc(LPVOID Context
)
397 CComPtr
<IBindStatusCallback
> dl
;
400 HWND Dlg
= (HWND
) Context
;
401 ULONG dwContentLen
, dwBytesWritten
, dwBytesRead
, dwStatus
;
402 ULONG dwCurrentBytesRead
= 0;
403 ULONG dwStatusLen
= sizeof(dwStatus
);
404 BOOL bCancelled
= FALSE
;
405 BOOL bTempfile
= FALSE
;
407 HINTERNET hOpen
= NULL
;
408 HINTERNET hFile
= NULL
;
409 HANDLE hOut
= INVALID_HANDLE_VALUE
;
410 unsigned char lpBuffer
[4096];
411 LPCWSTR lpszAgent
= L
"RApps/1.0";
412 URL_COMPONENTS urlComponents
;
413 size_t urlLength
, filenameLength
;
417 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD
);
421 /* build the path for the download */
422 p
= wcsrchr(AppInfo
->szUrlDownload
.GetString(), L
'/');
423 q
= wcsrchr(AppInfo
->szUrlDownload
.GetString(), L
'?');
425 /* do we have a final slash separator? */
429 /* prepare the tentative length of the filename, maybe we've to remove part of it later on */
430 filenameLength
= wcslen(p
) * sizeof(WCHAR
);
432 /* do we have query arguments in the target URL after the filename? account for them
433 (e.g. https://example.org/myfile.exe?no_adware_plz) */
434 if (q
&& q
> p
&& (q
- p
) > 0)
435 filenameLength
-= wcslen(q
- 1) * sizeof(WCHAR
);
437 /* is this URL an update package for RAPPS? if so store it in a different place */
438 if (AppInfo
->szUrlDownload
== APPLICATION_DATABASE_URL
)
441 if (!GetStorageDirectory(Path
))
446 Path
= SettingsInfo
.szDownloadDir
;
449 /* is the path valid? can we access it? */
450 if (GetFileAttributesW(Path
.GetString()) == INVALID_FILE_ATTRIBUTES
)
452 if (!CreateDirectoryW(Path
.GetString(), NULL
))
456 /* append a \ to the provided file system path, and the filename portion from the URL after that */
457 Path
.Format(L
"\\%ls", (p
+ 1));
459 if (!bCab
&& AppInfo
->szSHA1
[0] && GetFileAttributesW(Path
.GetString()) != INVALID_FILE_ATTRIBUTES
)
461 /* only open it in case of total correctness */
462 if (VerifyInteg(AppInfo
->szSHA1
, Path
))
468 CDownloadDialog_Constructor(Dlg
, &bCancelled
, IID_PPV_ARG(IBindStatusCallback
, &dl
));
473 /* FIXME: this should just be using the system-wide proxy settings */
474 switch (SettingsInfo
.Proxy
)
476 case 0: /* preconfig */
477 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, 0);
479 case 1: /* direct (no proxy) */
480 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_DIRECT
, NULL
, NULL
, 0);
482 case 2: /* use proxy */
483 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PROXY
, SettingsInfo
.szProxyServer
, SettingsInfo
.szNoProxyFor
, 0);
485 default: /* preconfig */
486 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, 0);
493 hFile
= InternetOpenUrlW(hOpen
, AppInfo
->szUrlDownload
, NULL
, 0, INTERNET_FLAG_PRAGMA_NOCACHE
| INTERNET_FLAG_KEEP_CONNECTION
, 0);
496 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD2
);
500 if (!HttpQueryInfoW(hFile
, HTTP_QUERY_STATUS_CODE
| HTTP_QUERY_FLAG_NUMBER
, &dwStatus
, &dwStatusLen
, NULL
))
503 if (dwStatus
!= HTTP_STATUS_OK
)
505 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD
);
509 dwStatusLen
= sizeof(dwStatus
);
511 memset(&urlComponents
, 0, sizeof(urlComponents
));
512 urlComponents
.dwStructSize
= sizeof(urlComponents
);
514 urlLength
= AppInfo
->szUrlDownload
.GetLength();
515 urlComponents
.dwSchemeLength
= urlLength
+ 1;
516 urlComponents
.lpszScheme
= (LPWSTR
) malloc(urlComponents
.dwSchemeLength
* sizeof(WCHAR
));
517 urlComponents
.dwHostNameLength
= urlLength
+ 1;
518 urlComponents
.lpszHostName
= (LPWSTR
) malloc(urlComponents
.dwHostNameLength
* sizeof(WCHAR
));
520 if (!InternetCrackUrlW(AppInfo
->szUrlDownload
, urlLength
+ 1, ICU_DECODE
| ICU_ESCAPE
, &urlComponents
))
523 if (urlComponents
.nScheme
== INTERNET_SCHEME_HTTP
|| urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
)
524 HttpQueryInfo(hFile
, HTTP_QUERY_CONTENT_LENGTH
| HTTP_QUERY_FLAG_NUMBER
, &dwContentLen
, &dwStatus
, 0);
526 if (urlComponents
.nScheme
== INTERNET_SCHEME_FTP
)
527 dwContentLen
= FtpGetFileSize(hFile
, &dwStatus
);
529 #ifdef USE_CERT_PINNING
530 /* are we using HTTPS to download the RAPPS update package? check if the certificate is original */
531 if ((urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
) &&
532 (wcscmp(AppInfo
->szUrlDownload
, APPLICATION_DATABASE_URL
) == 0) &&
533 (!CertIsValid(hOpen
, urlComponents
.lpszHostName
)))
535 MessageBox_LoadString(hMainWnd
, IDS_CERT_DOES_NOT_MATCH
);
540 free(urlComponents
.lpszScheme
);
541 free(urlComponents
.lpszHostName
);
543 hOut
= CreateFileW(Path
.GetString(), GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, CREATE_ALWAYS
, 0, NULL
);
545 if (hOut
== INVALID_HANDLE_VALUE
)
550 if (!InternetReadFile(hFile
, lpBuffer
, _countof(lpBuffer
), &dwBytesRead
))
552 MessageBox_LoadString(hMainWnd
, IDS_INTERRUPTED_DOWNLOAD
);
556 if (!WriteFile(hOut
, &lpBuffer
[0], dwBytesRead
, &dwBytesWritten
, NULL
))
558 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_WRITE
);
562 dwCurrentBytesRead
+= dwBytesRead
;
563 dl
->OnProgress(dwCurrentBytesRead
, dwContentLen
, 0, AppInfo
->szUrlDownload
);
564 } while (dwBytesRead
&& !bCancelled
);
567 hOut
= INVALID_HANDLE_VALUE
;
572 /* if this thing isn't a RAPPS update and it has a SHA-1 checksum
573 verify its integrity by using the native advapi32.A_SHA1 functions */
574 if (!bCab
&& AppInfo
->szSHA1
[0] != 0)
576 ATL::CStringW szMsgText
;
578 /* change a few strings in the download dialog to reflect the verification process */
579 if (!szMsgText
.LoadStringW(hInst
, IDS_INTEG_CHECK_TITLE
))
582 SetWindowText(Dlg
, szMsgText
.GetString());
583 SendMessageW(GetDlgItem(Dlg
, IDC_DOWNLOAD_STATUS
), WM_SETTEXT
, 0, (LPARAM
) Path
.GetString());
585 /* this may take a while, depending on the file size */
586 if (!VerifyInteg(AppInfo
->szSHA1
, Path
.GetString()))
588 if (!szMsgText
.LoadStringW(hInst
, IDS_INTEG_CHECK_FAIL
))
591 MessageBoxW(Dlg
, szMsgText
.GetString(), NULL
, MB_OK
| MB_ICONERROR
);
596 ShowWindow(Dlg
, SW_HIDE
);
601 ShellExecuteW(NULL
, L
"open", Path
.GetString(), NULL
, NULL
, SW_SHOWNORMAL
);
604 if (hOut
!= INVALID_HANDLE_VALUE
)
607 InternetCloseHandle(hFile
);
608 InternetCloseHandle(hOpen
);
612 if (bCancelled
|| (SettingsInfo
.bDelInstaller
&& !bCab
))
613 DeleteFileW(Path
.GetString());
621 //TODO: Maybe launch this (similar function) in a seperate thread, so the list could be updated
622 BOOL
DownloadManager::DownloadListOfApplications(const ATL::CSimpleArray
<PAPPLICATION_INFO
>& AppsList
)
626 for (INT i
= 0; i
< AppsList
.GetSize(); ++i
)
628 bResult
= DownloadApplication(AppsList
[i
]) && bResult
;
633 BOOL
DownloadManager::DownloadApplication(PAPPLICATION_INFO pAppInfo
)
640 // Create a dialog and issue a download process
642 LaunchDownloadDialog();
644 WriteLogMessage(EVENTLOG_SUCCESS
, MSG_SUCCESS_INSTALL
, AppInfo
->szName
.GetString());
649 VOID
DownloadManager::DownloadApplicationsDB(LPCWSTR lpUrl
)
651 APPLICATION_INFO IntInfo
;
652 IntInfo
.szUrlDownload
= lpUrl
;
656 LaunchDownloadDialog();
659 //TODO: Reuse the dialog
660 VOID
DownloadManager::LaunchDownloadDialog()
663 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
),