2 * PROJECT: ReactOS Applications Manager
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
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 (sanchaez@reactos.org)
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
32 #include <shlobj_undoc.h>
33 #include <shlguid_undoc.h>
39 #include <shellutils.h>
43 #include <ui/rosctrls.h>
45 #include <shlwapi_undoc.h>
53 #ifdef USE_CERT_PINNING
54 #define CERT_ISSUER_INFO_OLD "US\r\nLet's Encrypt\r\nLet's Encrypt Authority X3"
55 #define CERT_ISSUER_INFO_NEW "US\r\nLet's Encrypt\r\nR3"
56 #define CERT_SUBJECT_INFO "rapps.reactos.org"
63 DLTYPE_DBUPDATE_UNOFFICIAL
68 DLSTATUS_WAITING
= IDS_STATUS_WAITING
,
69 DLSTATUS_DOWNLOADING
= IDS_STATUS_DOWNLOADING
,
70 DLSTATUS_WAITING_INSTALL
= IDS_STATUS_DOWNLOADED
,
71 DLSTATUS_INSTALLING
= IDS_STATUS_INSTALLING
,
72 DLSTATUS_INSTALLED
= IDS_STATUS_INSTALLED
,
73 DLSTATUS_FINISHED
= IDS_STATUS_FINISHED
77 LoadStatusString(DownloadStatus StatusParam
)
80 szString
.LoadStringW(StatusParam
);
84 #define FILENAME_VALID_CHAR ( \
85 PATH_CHAR_CLASS_LETTER | \
86 PATH_CHAR_CLASS_DOT | \
87 PATH_CHAR_CLASS_SEMICOLON | \
88 PATH_CHAR_CLASS_COMMA | \
89 PATH_CHAR_CLASS_SPACE | \
90 PATH_CHAR_CLASS_OTHER_VALID)
93 UrlUnescapeAndMakeFileNameValid(CStringW
& str
)
95 WCHAR szPath
[MAX_PATH
];
96 DWORD cchPath
= _countof(szPath
);
97 UrlUnescapeW(const_cast<LPWSTR
>((LPCWSTR
)str
), szPath
, &cchPath
, 0);
99 for (PWCHAR pch
= szPath
; *pch
; ++pch
)
101 if (!PathIsValidCharW(*pch
, FILENAME_VALID_CHAR
))
113 DownloadInfo(const CAppInfo
&AppInfo
) : DLType(DLTYPE_APPLICATION
)
115 AppInfo
.GetDownloadInfo(szUrl
, szSHA1
, SizeInBytes
);
116 szName
= AppInfo
.szDisplayName
;
128 DownloadParam() : Dialog(NULL
), AppInfo(), szCaption(NULL
)
131 DownloadParam(HWND dlg
, const ATL::CSimpleArray
<DownloadInfo
> &info
, LPCWSTR caption
)
132 : Dialog(dlg
), AppInfo(info
), szCaption(caption
)
137 ATL::CSimpleArray
<DownloadInfo
> AppInfo
;
141 class CDownloaderProgress
: public CWindowImpl
<CDownloaderProgress
, CWindow
, CControlWinTraits
>
143 CStringW m_szProgressText
;
146 CDownloaderProgress()
151 SetMarquee(BOOL Enable
)
154 ModifyStyle(0, PBS_MARQUEE
, 0);
156 ModifyStyle(PBS_MARQUEE
, 0, 0);
158 SendMessage(PBM_SETMARQUEE
, Enable
, 0);
162 SetProgress(ULONG ulProgress
, ULONG ulProgressMax
)
164 WCHAR szProgress
[100];
166 /* format the bits and bytes into pretty and accessible units... */
167 StrFormatByteSizeW(ulProgress
, szProgress
, _countof(szProgress
));
169 /* use our subclassed progress bar text subroutine */
170 CStringW ProgressText
;
174 /* total size is known */
175 WCHAR szProgressMax
[100];
176 UINT uiPercentage
= ((ULONGLONG
)ulProgress
* 100) / ulProgressMax
;
178 /* send the current progress to the progress bar */
181 SendMessage(PBM_SETPOS
, uiPercentage
, 0);
183 /* format total download size */
184 StrFormatByteSizeW(ulProgressMax
, szProgressMax
, _countof(szProgressMax
));
186 /* generate the text on progress bar */
187 ProgressText
.Format(L
"%u%% \x2014 %ls / %ls", uiPercentage
, szProgress
, szProgressMax
);
191 /* send the current progress to the progress bar */
194 SendMessage(PBM_SETPOS
, 0, 0);
196 /* total size is not known, display only current size */
197 ProgressText
.Format(L
"%ls...", szProgress
);
200 /* and finally display it */
203 SetWindowText(ProgressText
.GetString());
207 OnEraseBkgnd(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
213 OnPaint(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
216 HDC hDC
= BeginPaint(&ps
), hdcMem
;
220 UINT win_width
, win_height
;
222 GetClientRect(&myRect
);
224 /* grab the progress bar rect size */
225 win_width
= myRect
.right
- myRect
.left
;
226 win_height
= myRect
.bottom
- myRect
.top
;
228 /* create an off-screen DC for double-buffering */
229 hdcMem
= CreateCompatibleDC(hDC
);
230 hbmMem
= CreateCompatibleBitmap(hDC
, win_width
, win_height
);
232 hOld
= SelectObject(hdcMem
, hbmMem
);
234 /* call the original draw code and redirect it to our memory buffer */
235 DefWindowProc(uMsg
, (WPARAM
)hdcMem
, lParam
);
237 /* draw our nifty progress text over it */
238 SelectFont(hdcMem
, GetStockFont(DEFAULT_GUI_FONT
));
240 hdcMem
, m_szProgressText
.GetString(), m_szProgressText
.GetLength(), &myRect
,
241 DT_CENTER
| DT_VCENTER
| DT_NOPREFIX
| DT_SINGLELINE
, GetSysColor(COLOR_CAPTIONTEXT
),
242 GetSysColor(COLOR_3DDKSHADOW
), 1, 1);
244 /* transfer the off-screen DC to the screen */
245 BitBlt(hDC
, 0, 0, win_width
, win_height
, hdcMem
, 0, 0, SRCCOPY
);
247 /* free the off-screen DC */
248 SelectObject(hdcMem
, hOld
);
249 DeleteObject(hbmMem
);
257 OnSetText(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
259 PCWSTR pszText
= (PCWSTR
)lParam
;
262 if (m_szProgressText
!= pszText
)
264 m_szProgressText
= pszText
;
265 InvalidateRect(NULL
, TRUE
);
270 if (!m_szProgressText
.IsEmpty())
272 m_szProgressText
.Empty();
273 InvalidateRect(NULL
, TRUE
);
279 BEGIN_MSG_MAP(CDownloaderProgress
)
280 MESSAGE_HANDLER(WM_ERASEBKGND
, OnEraseBkgnd
)
281 MESSAGE_HANDLER(WM_PAINT
, OnPaint
)
282 MESSAGE_HANDLER(WM_SETTEXT
, OnSetText
)
286 class CDowloadingAppsListView
: public CListView
290 Create(HWND hwndParent
)
293 ::GetClientRect(hwndParent
, &r
);
294 r
.top
= (2 * r
.top
+ 1 * r
.bottom
) / 3; /* The vertical position at ratio 1 : 2 */
296 ::InflateRect(&r
, -MARGIN
, -MARGIN
);
298 const DWORD style
= WS_CHILD
| WS_VISIBLE
| LVS_REPORT
| LVS_SINGLESEL
| LVS_SHOWSELALWAYS
| LVS_NOSORTHEADER
|
301 HWND hwnd
= CListView::Create(hwndParent
, r
, NULL
, style
, WS_EX_CLIENTEDGE
);
303 AddColumn(0, 150, LVCFMT_LEFT
);
304 AddColumn(1, 120, LVCFMT_LEFT
);
310 LoadList(ATL::CSimpleArray
<DownloadInfo
> arrInfo
)
312 for (INT i
= 0; i
< arrInfo
.GetSize(); ++i
)
314 AddRow(i
, arrInfo
[i
].szName
.GetString(), DLSTATUS_WAITING
);
319 SetDownloadStatus(INT ItemIndex
, DownloadStatus Status
)
321 CStringW szBuffer
= LoadStatusString(Status
);
322 SetItemText(ItemIndex
, 1, szBuffer
.GetString());
326 AddItem(INT ItemIndex
, LPWSTR lpText
)
330 ZeroMemory(&Item
, sizeof(Item
));
332 Item
.mask
= LVIF_TEXT
| LVIF_STATE
;
333 Item
.pszText
= lpText
;
334 Item
.iItem
= ItemIndex
;
336 return InsertItem(&Item
);
340 AddRow(INT RowIndex
, LPCWSTR szAppName
, const DownloadStatus Status
)
342 CStringW szStatus
= LoadStatusString(Status
);
343 AddItem(RowIndex
, const_cast<LPWSTR
>(szAppName
));
344 SetDownloadStatus(RowIndex
, Status
);
348 AddColumn(INT Index
, INT Width
, INT Format
)
351 ZeroMemory(&Column
, sizeof(Column
));
353 Column
.mask
= LVCF_FMT
| LVCF_WIDTH
| LVCF_SUBITEM
;
354 Column
.iSubItem
= Index
;
358 return (InsertColumn(Index
, &Column
) == -1) ? FALSE
: TRUE
;
362 #ifdef USE_CERT_PINNING
364 CertGetSubjectAndIssuer(HINTERNET hFile
, CLocalPtr
<char> &subjectInfo
, CLocalPtr
<char> &issuerInfo
)
366 DWORD certInfoLength
;
367 INTERNET_CERTIFICATE_INFOA certInfo
;
370 size
= sizeof(flags
);
371 if (!InternetQueryOptionA(hFile
, INTERNET_OPTION_SECURITY_FLAGS
, &flags
, &size
))
376 if (!flags
& SECURITY_FLAG_SECURE
)
381 /* Despite what the header indicates, the implementation of INTERNET_CERTIFICATE_INFO is not Unicode-aware. */
382 certInfoLength
= sizeof(certInfo
);
383 if (!InternetQueryOptionA(hFile
, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT
, &certInfo
, &certInfoLength
))
388 subjectInfo
.Attach(certInfo
.lpszSubjectInfo
);
389 issuerInfo
.Attach(certInfo
.lpszIssuerInfo
);
391 if (certInfo
.lpszProtocolName
)
392 LocalFree(certInfo
.lpszProtocolName
);
393 if (certInfo
.lpszSignatureAlgName
)
394 LocalFree(certInfo
.lpszSignatureAlgName
);
395 if (certInfo
.lpszEncryptionAlgName
)
396 LocalFree(certInfo
.lpszEncryptionAlgName
);
398 return certInfo
.lpszSubjectInfo
&& certInfo
.lpszIssuerInfo
;
403 MessageBox_LoadString(HWND hMainWnd
, INT StringID
)
406 if (szMsgText
.LoadStringW(StringID
))
408 MessageBoxW(hMainWnd
, szMsgText
.GetString(), NULL
, MB_OK
| MB_ICONERROR
);
412 // Download dialog (loaddlg.cpp)
413 class CDownloadManager
415 static ATL::CSimpleArray
<DownloadInfo
> AppsDownloadList
;
416 static CDowloadingAppsListView DownloadsListView
;
417 static CDownloaderProgress ProgressBar
;
418 static BOOL bCancelled
;
421 UpdateProgress(HWND hDlg
, ULONG ulProgress
, ULONG ulProgressMax
, ULONG ulStatusCode
, LPCWSTR szStatusText
);
425 Add(DownloadInfo info
);
427 Download(const DownloadInfo
&DLInfo
, BOOL bIsModal
= FALSE
);
428 static INT_PTR CALLBACK
429 DownloadDlgProc(HWND Dlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
430 static unsigned int WINAPI
431 ThreadFunc(LPVOID Context
);
432 static VOID
LaunchDownloadDialog(BOOL
);
436 ATL::CSimpleArray
<DownloadInfo
> CDownloadManager::AppsDownloadList
;
437 CDowloadingAppsListView
CDownloadManager::DownloadsListView
;
438 CDownloaderProgress
CDownloadManager::ProgressBar
;
439 BOOL
CDownloadManager::bCancelled
= FALSE
;
440 BOOL
CDownloadManager::bModal
= FALSE
;
443 CDownloadManager::Add(DownloadInfo info
)
445 AppsDownloadList
.Add(info
);
449 CDownloadManager::Download(const DownloadInfo
&DLInfo
, BOOL bIsModal
)
451 AppsDownloadList
.RemoveAll();
452 AppsDownloadList
.Add(DLInfo
);
453 LaunchDownloadDialog(bIsModal
);
457 CDownloadManager::DownloadDlgProc(HWND Dlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
459 static WCHAR szCaption
[MAX_PATH
];
465 HICON hIconSm
, hIconBg
;
466 CStringW szTempCaption
;
472 hIconBg
= (HICON
)GetClassLongPtrW(hMainWnd
, GCLP_HICON
);
473 hIconSm
= (HICON
)GetClassLongPtrW(hMainWnd
, GCLP_HICONSM
);
475 if (!hMainWnd
|| (!hIconBg
|| !hIconSm
))
477 /* Load the default icon */
478 hIconBg
= hIconSm
= LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_MAIN
));
481 if (hIconBg
&& hIconSm
)
483 SendMessageW(Dlg
, WM_SETICON
, ICON_BIG
, (LPARAM
)hIconBg
);
484 SendMessageW(Dlg
, WM_SETICON
, ICON_SMALL
, (LPARAM
)hIconSm
);
487 HWND Item
= GetDlgItem(Dlg
, IDC_DOWNLOAD_PROGRESS
);
490 // initialize the default values for our nifty progress bar
491 // and subclass it so that it learns to print a status text
492 ProgressBar
.SubclassWindow(Item
);
493 ProgressBar
.SendMessage(PBM_SETRANGE
, 0, MAKELPARAM(0, 100));
494 ProgressBar
.SendMessage(PBM_SETPOS
, 0, 0);
495 if (AppsDownloadList
.GetSize() > 0)
496 ProgressBar
.SetProgress(0, AppsDownloadList
[0].SizeInBytes
);
500 HWND hListView
= DownloadsListView
.Create(Dlg
);
505 DownloadsListView
.LoadList(AppsDownloadList
);
507 // Get a dlg string for later use
508 GetWindowTextW(Dlg
, szCaption
, _countof(szCaption
));
510 // Hide a placeholder from displaying
511 szTempCaption
= szCaption
;
512 szTempCaption
.Replace(L
"%ls", L
"");
513 SetWindowText(Dlg
, szTempCaption
.GetString());
515 ShowWindow(Dlg
, SW_SHOW
);
517 // Start download process
518 DownloadParam
*param
= new DownloadParam(Dlg
, AppsDownloadList
, szCaption
);
519 unsigned int ThreadId
;
520 HANDLE Thread
= (HANDLE
)_beginthreadex(NULL
, 0, ThreadFunc
, (void *)param
, 0, &ThreadId
);
527 AppsDownloadList
.RemoveAll();
532 if (wParam
== IDCANCEL
)
535 PostMessageW(Dlg
, WM_CLOSE
, 0, 0);
541 ProgressBar
.UnsubclassWindow(TRUE
);
542 if (CDownloadManager::bModal
)
548 ::DestroyWindow(Dlg
);
557 BOOL UrlHasBeenCopied
;
560 CDownloadManager::UpdateProgress(
565 LPCWSTR szStatusText
)
571 ProgressBar
.SetProgress(ulProgress
, ulProgressMax
);
575 Item
= GetDlgItem(hDlg
, IDC_DOWNLOAD_STATUS
);
576 if (Item
&& szStatusText
&& wcslen(szStatusText
) > 0 && UrlHasBeenCopied
== FALSE
)
578 SIZE_T len
= wcslen(szStatusText
) + 1;
582 /* beautify our url for display purposes */
583 if (!InternetCanonicalizeUrlW(szStatusText
, buf
.GetBuffer(len
), &dummyLen
, ICU_DECODE
| ICU_NO_ENCODE
))
585 /* just use the original */
594 /* paste it into our dialog and don't do it again in this instance */
595 ::SetWindowText(Item
, buf
.GetString());
596 UrlHasBeenCopied
= TRUE
;
601 ShowLastError(HWND hWndOwner
, BOOL bInetError
, DWORD dwLastError
)
603 CLocalPtr
<WCHAR
> lpMsg
;
606 FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_IGNORE_INSERTS
|
607 (bInetError
? FORMAT_MESSAGE_FROM_HMODULE
: FORMAT_MESSAGE_FROM_SYSTEM
),
608 (bInetError
? GetModuleHandleW(L
"wininet.dll") : NULL
), dwLastError
, LANG_USER_DEFAULT
, (LPWSTR
)&lpMsg
, 0,
611 DPRINT1("FormatMessageW unexpected failure (err %d)\n", GetLastError());
615 MessageBoxW(hWndOwner
, lpMsg
, NULL
, MB_OK
| MB_ICONERROR
);
620 CDownloadManager::ThreadFunc(LPVOID param
)
625 HWND hDlg
= static_cast<DownloadParam
*>(param
)->Dialog
;
629 ULONG dwContentLen
, dwBytesWritten
, dwBytesRead
, dwStatus
;
630 ULONG dwCurrentBytesRead
= 0;
631 ULONG dwStatusLen
= sizeof(dwStatus
);
633 BOOL bTempfile
= FALSE
;
635 HINTERNET hOpen
= NULL
;
636 HINTERNET hFile
= NULL
;
637 HANDLE hOut
= INVALID_HANDLE_VALUE
;
639 unsigned char lpBuffer
[4096];
640 LPCWSTR lpszAgent
= L
"RApps/1.1";
641 URL_COMPONENTSW urlComponents
;
642 size_t urlLength
, filenameLength
;
644 const ATL::CSimpleArray
<DownloadInfo
> &InfoArray
= static_cast<DownloadParam
*>(param
)->AppInfo
;
645 LPCWSTR szCaption
= static_cast<DownloadParam
*>(param
)->szCaption
;
646 CStringW szNewCaption
;
648 const DWORD dwUrlConnectFlags
=
649 INTERNET_FLAG_DONT_CACHE
| INTERNET_FLAG_PRAGMA_NOCACHE
| INTERNET_FLAG_KEEP_CONNECTION
;
651 if (InfoArray
.GetSize() <= 0)
653 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD
);
657 for (iAppId
= 0; iAppId
< InfoArray
.GetSize(); ++iAppId
)
659 // Reset progress bar
662 Item
= GetDlgItem(hDlg
, IDC_DOWNLOAD_PROGRESS
);
665 ProgressBar
.SetMarquee(FALSE
);
666 ProgressBar
.SendMessage(PBM_SETPOS
, 0, 0);
667 ProgressBar
.SetProgress(0, InfoArray
[iAppId
].SizeInBytes
);
670 // is this URL an update package for RAPPS? if so store it in a different place
671 if (InfoArray
[iAppId
].DLType
!= DLTYPE_APPLICATION
)
673 if (!GetStorageDirectory(Path
))
675 ShowLastError(hMainWnd
, FALSE
, GetLastError());
681 Path
= SettingsInfo
.szDownloadDir
;
684 // Change caption to show the currently downloaded app
685 switch (InfoArray
[iAppId
].DLType
)
687 case DLTYPE_APPLICATION
:
688 szNewCaption
.Format(szCaption
, InfoArray
[iAppId
].szName
.GetString());
690 case DLTYPE_DBUPDATE
:
691 szNewCaption
.LoadStringW(IDS_DL_DIALOG_DB_DOWNLOAD_DISP
);
693 case DLTYPE_DBUPDATE_UNOFFICIAL
:
694 szNewCaption
.LoadStringW(IDS_DL_DIALOG_DB_UNOFFICIAL_DOWNLOAD_DISP
);
700 SetWindowTextW(hDlg
, szNewCaption
.GetString());
702 // build the path for the download
703 p
= wcsrchr(InfoArray
[iAppId
].szUrl
.GetString(), L
'/');
704 q
= wcsrchr(InfoArray
[iAppId
].szUrl
.GetString(), L
'?');
706 // do we have a final slash separator?
709 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_PATH
);
713 // prepare the tentative length of the filename, maybe we've to remove part of it later on
714 filenameLength
= wcslen(p
) * sizeof(WCHAR
);
716 /* do we have query arguments in the target URL after the filename? account for them
717 (e.g. https://example.org/myfile.exe?no_adware_plz) */
718 if (q
&& q
> p
&& (q
- p
) > 0)
719 filenameLength
-= wcslen(q
- 1) * sizeof(WCHAR
);
721 // is the path valid? can we access it?
722 if (GetFileAttributesW(Path
) == INVALID_FILE_ATTRIBUTES
)
724 if (!CreateDirectoryW(Path
, NULL
))
726 ShowLastError(hMainWnd
, FALSE
, GetLastError());
731 switch (InfoArray
[iAppId
].DLType
)
733 case DLTYPE_DBUPDATE
:
734 case DLTYPE_DBUPDATE_UNOFFICIAL
:
735 Path
+= APPLICATION_DATABASE_NAME
;
737 case DLTYPE_APPLICATION
:
739 CStringW str
= p
+ 1; // use the filename retrieved from URL
740 UrlUnescapeAndMakeFileNameValid(str
);
746 if ((InfoArray
[iAppId
].DLType
== DLTYPE_APPLICATION
) && InfoArray
[iAppId
].szSHA1
[0] &&
747 GetFileAttributesW(Path
) != INVALID_FILE_ATTRIBUTES
)
749 // only open it in case of total correctness
750 if (VerifyInteg(InfoArray
[iAppId
].szSHA1
.GetString(), Path
))
754 // Add the download URL
757 SetDlgItemTextW(hDlg
, IDC_DOWNLOAD_STATUS
, InfoArray
[iAppId
].szUrl
.GetString());
759 DownloadsListView
.SetDownloadStatus(iAppId
, DLSTATUS_DOWNLOADING
);
762 UrlHasBeenCopied
= FALSE
;
765 /* FIXME: this should just be using the system-wide proxy settings */
766 switch (SettingsInfo
.Proxy
)
770 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, 0);
772 case 1: // direct (no proxy)
773 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_DIRECT
, NULL
, NULL
, 0);
776 hOpen
= InternetOpenW(
777 lpszAgent
, INTERNET_OPEN_TYPE_PROXY
, SettingsInfo
.szProxyServer
, SettingsInfo
.szNoProxyFor
, 0);
783 ShowLastError(hMainWnd
, TRUE
, GetLastError());
787 dwStatusLen
= sizeof(dwStatus
);
789 memset(&urlComponents
, 0, sizeof(urlComponents
));
790 urlComponents
.dwStructSize
= sizeof(urlComponents
);
792 urlLength
= InfoArray
[iAppId
].szUrl
.GetLength();
793 urlComponents
.dwSchemeLength
= urlLength
+ 1;
794 urlComponents
.lpszScheme
= (LPWSTR
)malloc(urlComponents
.dwSchemeLength
* sizeof(WCHAR
));
796 if (!InternetCrackUrlW(InfoArray
[iAppId
].szUrl
, urlLength
+ 1, ICU_DECODE
| ICU_ESCAPE
, &urlComponents
))
798 ShowLastError(hMainWnd
, TRUE
, GetLastError());
804 if (urlComponents
.nScheme
== INTERNET_SCHEME_HTTP
|| urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
)
806 hFile
= InternetOpenUrlW(hOpen
, InfoArray
[iAppId
].szUrl
.GetString(), NULL
, 0, dwUrlConnectFlags
, 0);
809 if (!ShowLastError(hMainWnd
, TRUE
, GetLastError()))
811 /* Workaround for CORE-17377 */
812 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD2
);
818 if (!HttpQueryInfoW(hFile
, HTTP_QUERY_STATUS_CODE
| HTTP_QUERY_FLAG_NUMBER
, &dwStatus
, &dwStatusLen
, NULL
))
820 ShowLastError(hMainWnd
, TRUE
, GetLastError());
824 if (dwStatus
!= HTTP_STATUS_OK
)
826 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD
);
830 // query content length
832 hFile
, HTTP_QUERY_CONTENT_LENGTH
| HTTP_QUERY_FLAG_NUMBER
, &dwContentLen
, &dwStatusLen
, NULL
);
834 else if (urlComponents
.nScheme
== INTERNET_SCHEME_FTP
)
836 // force passive mode on FTP
838 InternetOpenUrlW(hOpen
, InfoArray
[iAppId
].szUrl
, NULL
, 0, dwUrlConnectFlags
| INTERNET_FLAG_PASSIVE
, 0);
841 if (!ShowLastError(hMainWnd
, TRUE
, GetLastError()))
843 /* Workaround for CORE-17377 */
844 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD2
);
849 dwContentLen
= FtpGetFileSize(hFile
, &dwStatus
);
851 else if (urlComponents
.nScheme
== INTERNET_SCHEME_FILE
)
853 // Add support for the file scheme so testing locally is simpler
854 WCHAR LocalFilePath
[MAX_PATH
];
855 DWORD cchPath
= _countof(LocalFilePath
);
856 // Ideally we would use PathCreateFromUrlAlloc here, but that is not exported (yet)
857 HRESULT hr
= PathCreateFromUrlW(InfoArray
[iAppId
].szUrl
, LocalFilePath
, &cchPath
, 0);
860 if (CopyFileW(LocalFilePath
, Path
, FALSE
))
866 ShowLastError(hMainWnd
, FALSE
, GetLastError());
872 ShowLastError(hMainWnd
, FALSE
, hr
);
879 // Someone was nice enough to add this, let's use it
880 if (InfoArray
[iAppId
].SizeInBytes
)
882 dwContentLen
= InfoArray
[iAppId
].SizeInBytes
;
886 // content-length is not known, enable marquee mode
887 ProgressBar
.SetMarquee(TRUE
);
891 free(urlComponents
.lpszScheme
);
893 #ifdef USE_CERT_PINNING
894 // are we using HTTPS to download the RAPPS update package? check if the certificate is original
895 if ((urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
) && (InfoArray
[iAppId
].DLType
== DLTYPE_DBUPDATE
))
897 CLocalPtr
<char> subjectName
, issuerName
;
899 bool bAskQuestion
= false;
900 if (!CertGetSubjectAndIssuer(hFile
, subjectName
, issuerName
))
902 szMsgText
.LoadStringW(IDS_UNABLE_TO_QUERY_CERT
);
907 if (strcmp(subjectName
, CERT_SUBJECT_INFO
) ||
908 (strcmp(issuerName
, CERT_ISSUER_INFO_OLD
) && strcmp(issuerName
, CERT_ISSUER_INFO_NEW
)))
910 szMsgText
.Format(IDS_MISMATCH_CERT_INFO
, (char *)subjectName
, (const char *)issuerName
);
917 if (MessageBoxA(hMainWnd
, szMsgText
.GetString(), NULL
, MB_YESNO
| MB_ICONERROR
) != IDYES
)
925 hOut
= CreateFileW(Path
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, CREATE_ALWAYS
, 0, NULL
);
927 if (hOut
== INVALID_HANDLE_VALUE
)
929 ShowLastError(hMainWnd
, FALSE
, GetLastError());
933 dwCurrentBytesRead
= 0;
936 if (!InternetReadFile(hFile
, lpBuffer
, _countof(lpBuffer
), &dwBytesRead
))
938 ShowLastError(hMainWnd
, TRUE
, GetLastError());
942 if (!WriteFile(hOut
, &lpBuffer
[0], dwBytesRead
, &dwBytesWritten
, NULL
))
944 ShowLastError(hMainWnd
, FALSE
, GetLastError());
948 dwCurrentBytesRead
+= dwBytesRead
;
951 UpdateProgress(hDlg
, dwCurrentBytesRead
, dwContentLen
, 0, InfoArray
[iAppId
].szUrl
.GetString());
952 } while (dwBytesRead
&& !bCancelled
);
955 hOut
= INVALID_HANDLE_VALUE
;
959 DPRINT1("Operation cancelled\n");
965 // set progress bar to 100%
966 ProgressBar
.SetMarquee(FALSE
);
968 dwContentLen
= dwCurrentBytesRead
;
971 UpdateProgress(hDlg
, dwCurrentBytesRead
, dwContentLen
, 0, InfoArray
[iAppId
].szUrl
.GetString());
974 /* if this thing isn't a RAPPS update and it has a SHA-1 checksum
975 verify its integrity by using the native advapi32.A_SHA1 functions */
976 if ((InfoArray
[iAppId
].DLType
== DLTYPE_APPLICATION
) && InfoArray
[iAppId
].szSHA1
[0] != 0)
980 // change a few strings in the download dialog to reflect the verification process
981 if (!szMsgText
.LoadStringW(IDS_INTEG_CHECK_TITLE
))
983 DPRINT1("Unable to load string\n");
989 SetWindowTextW(hDlg
, szMsgText
.GetString());
990 ::SetDlgItemTextW(hDlg
, IDC_DOWNLOAD_STATUS
, Path
);
992 // this may take a while, depending on the file size
993 if (!VerifyInteg(InfoArray
[iAppId
].szSHA1
.GetString(), Path
))
995 if (!szMsgText
.LoadStringW(IDS_INTEG_CHECK_FAIL
))
997 DPRINT1("Unable to load string\n");
1001 if (!IsWindow(hDlg
))
1003 MessageBoxW(hDlg
, szMsgText
.GetString(), NULL
, MB_OK
| MB_ICONERROR
);
1009 DownloadsListView
.SetDownloadStatus(iAppId
, DLSTATUS_WAITING_INSTALL
);
1012 if (InfoArray
[iAppId
].DLType
== DLTYPE_APPLICATION
)
1014 SHELLEXECUTEINFOW shExInfo
= {0};
1015 shExInfo
.cbSize
= sizeof(shExInfo
);
1016 shExInfo
.fMask
= SEE_MASK_NOCLOSEPROCESS
;
1017 shExInfo
.lpVerb
= L
"open";
1018 shExInfo
.lpFile
= Path
;
1019 shExInfo
.lpParameters
= L
"";
1020 shExInfo
.nShow
= SW_SHOW
;
1022 /* FIXME: Do we want to log installer status? */
1023 WriteLogMessage(EVENTLOG_SUCCESS
, MSG_SUCCESS_INSTALL
, InfoArray
[iAppId
].szName
);
1025 if (ShellExecuteExW(&shExInfo
))
1027 // reflect installation progress in the titlebar
1028 // TODO: make a separate string with a placeholder to include app name?
1029 CStringW szMsgText
= LoadStatusString(DLSTATUS_INSTALLING
);
1030 if (!IsWindow(hDlg
))
1032 SetWindowTextW(hDlg
, szMsgText
.GetString());
1034 DownloadsListView
.SetDownloadStatus(iAppId
, DLSTATUS_INSTALLING
);
1036 // TODO: issue an install operation separately so that the apps could be downloaded in the background
1037 WaitForSingleObject(shExInfo
.hProcess
, INFINITE
);
1038 CloseHandle(shExInfo
.hProcess
);
1042 ShowLastError(hMainWnd
, FALSE
, GetLastError());
1047 if (hOut
!= INVALID_HANDLE_VALUE
)
1051 InternetCloseHandle(hFile
);
1052 InternetCloseHandle(hOpen
);
1056 if (bCancelled
|| (SettingsInfo
.bDelInstaller
&& (InfoArray
[iAppId
].DLType
== DLTYPE_APPLICATION
)))
1060 if (!IsWindow(hDlg
))
1062 DownloadsListView
.SetDownloadStatus(iAppId
, DLSTATUS_FINISHED
);
1065 delete static_cast<DownloadParam
*>(param
);
1066 if (!IsWindow(hDlg
))
1068 SendMessageW(hDlg
, WM_CLOSE
, 0, 0);
1072 // TODO: Reuse the dialog
1074 CDownloadManager::LaunchDownloadDialog(BOOL bIsModal
)
1076 CDownloadManager::bModal
= bIsModal
;
1079 DialogBoxW(hInst
, MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
), hMainWnd
, DownloadDlgProc
);
1083 CreateDialogW(hInst
, MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
), hMainWnd
, DownloadDlgProc
);
1089 DownloadListOfApplications(const CAtlList
<CAppInfo
*> &AppsList
, BOOL bIsModal
)
1091 if (AppsList
.IsEmpty())
1094 POSITION CurrentListPosition
= AppsList
.GetHeadPosition();
1095 while (CurrentListPosition
)
1097 const CAppInfo
*Info
= AppsList
.GetNext(CurrentListPosition
);
1098 CDownloadManager::Add(DownloadInfo(*Info
));
1101 // Create a dialog and issue a download process
1102 CDownloadManager::LaunchDownloadDialog(bIsModal
);
1108 DownloadApplication(CAppInfo
*pAppInfo
)
1113 CDownloadManager::Download(*pAppInfo
, FALSE
);
1118 DownloadApplicationsDB(LPCWSTR lpUrl
, BOOL IsOfficial
)
1120 static DownloadInfo DatabaseDLInfo
;
1121 DatabaseDLInfo
.szUrl
= lpUrl
;
1122 DatabaseDLInfo
.szName
.LoadStringW(IDS_DL_DIALOG_DB_DISP
);
1123 DatabaseDLInfo
.DLType
= IsOfficial
? DLTYPE_DBUPDATE
: DLTYPE_DBUPDATE_UNOFFICIAL
;
1124 CDownloadManager::Download(DatabaseDLInfo
, TRUE
);