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>
52 #ifdef USE_CERT_PINNING
53 #define CERT_ISSUER_INFO_OLD "US\r\nLet's Encrypt\r\nLet's Encrypt Authority X3"
54 #define CERT_ISSUER_INFO_NEW "US\r\nLet's Encrypt\r\nR3"
55 #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
76 ATL::CStringW
LoadStatusString(DownloadStatus StatusParam
)
78 ATL::CStringW szString
;
79 szString
.LoadStringW(StatusParam
);
86 DownloadInfo(const CAvailableApplicationInfo
& AppInfo
)
87 : DLType(DLTYPE_APPLICATION
)
88 , szUrl(AppInfo
.m_szUrlDownload
)
89 , szName(AppInfo
.m_szName
)
90 , szSHA1(AppInfo
.m_szSHA1
)
91 , SizeInBytes(AppInfo
.m_SizeBytes
)
104 DownloadParam() : Dialog(NULL
), AppInfo(), szCaption(NULL
) {}
105 DownloadParam(HWND dlg
, const ATL::CSimpleArray
<DownloadInfo
> &info
, LPCWSTR caption
)
106 : Dialog(dlg
), AppInfo(info
), szCaption(caption
)
111 ATL::CSimpleArray
<DownloadInfo
> AppInfo
;
116 class CDownloaderProgress
117 : public CWindowImpl
<CDownloaderProgress
, CWindow
, CControlWinTraits
>
119 ATL::CStringW m_szProgressText
;
122 CDownloaderProgress()
126 VOID
SetMarquee(BOOL Enable
)
129 ModifyStyle(0, PBS_MARQUEE
, 0);
131 ModifyStyle(PBS_MARQUEE
, 0, 0);
133 SendMessage(PBM_SETMARQUEE
, Enable
, 0);
136 VOID
SetProgress(ULONG ulProgress
, ULONG ulProgressMax
)
138 WCHAR szProgress
[100];
140 /* format the bits and bytes into pretty and accessible units... */
141 StrFormatByteSizeW(ulProgress
, szProgress
, _countof(szProgress
));
143 /* use our subclassed progress bar text subroutine */
144 ATL::CStringW ProgressText
;
148 /* total size is known */
149 WCHAR szProgressMax
[100];
150 UINT uiPercentage
= ((ULONGLONG
) ulProgress
* 100) / ulProgressMax
;
152 /* send the current progress to the progress bar */
153 if (!IsWindow()) return;
154 SendMessage(PBM_SETPOS
, uiPercentage
, 0);
156 /* format total download size */
157 StrFormatByteSizeW(ulProgressMax
, szProgressMax
, _countof(szProgressMax
));
159 /* generate the text on progress bar */
160 ProgressText
.Format(L
"%u%% \x2014 %ls / %ls",
167 /* send the current progress to the progress bar */
168 if (!IsWindow()) return;
169 SendMessage(PBM_SETPOS
, 0, 0);
171 /* total size is not known, display only current size */
172 ProgressText
.Format(L
"%ls...", szProgress
);
175 /* and finally display it */
176 if (!IsWindow()) return;
177 SendMessage(WM_SETTEXT
, 0, (LPARAM
) ProgressText
.GetString());
180 LRESULT
OnPaint(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
183 HDC hDC
= BeginPaint(&ps
), hdcMem
;
187 UINT win_width
, win_height
;
189 GetClientRect(&myRect
);
191 /* grab the progress bar rect size */
192 win_width
= myRect
.right
- myRect
.left
;
193 win_height
= myRect
.bottom
- myRect
.top
;
195 /* create an off-screen DC for double-buffering */
196 hdcMem
= CreateCompatibleDC(hDC
);
197 hbmMem
= CreateCompatibleBitmap(hDC
, win_width
, win_height
);
199 hOld
= SelectObject(hdcMem
, hbmMem
);
201 /* call the original draw code and redirect it to our memory buffer */
202 DefWindowProc(uMsg
, (WPARAM
) hdcMem
, lParam
);
204 /* draw our nifty progress text over it */
205 SelectFont(hdcMem
, GetStockFont(DEFAULT_GUI_FONT
));
206 DrawShadowText(hdcMem
, m_szProgressText
.GetString(), m_szProgressText
.GetLength(),
208 DT_CENTER
| DT_VCENTER
| DT_NOPREFIX
| DT_SINGLELINE
,
209 GetSysColor(COLOR_CAPTIONTEXT
),
210 GetSysColor(COLOR_3DSHADOW
),
213 /* transfer the off-screen DC to the screen */
214 BitBlt(hDC
, 0, 0, win_width
, win_height
, hdcMem
, 0, 0, SRCCOPY
);
216 /* free the off-screen DC */
217 SelectObject(hdcMem
, hOld
);
218 DeleteObject(hbmMem
);
225 LRESULT
OnSetText(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
229 m_szProgressText
= (PCWSTR
) lParam
;
234 BEGIN_MSG_MAP(CDownloaderProgress
)
235 MESSAGE_HANDLER(WM_ERASEBKGND
, OnPaint
)
236 MESSAGE_HANDLER(WM_PAINT
, OnPaint
)
237 MESSAGE_HANDLER(WM_SETTEXT
, OnSetText
)
241 class CDowloadingAppsListView
245 HWND
Create(HWND hwndParent
)
247 RECT r
= {10, 150, 320, 350};
248 const DWORD style
= WS_CHILD
| WS_VISIBLE
| LVS_REPORT
| LVS_SINGLESEL
249 | LVS_SHOWSELALWAYS
| LVS_NOSORTHEADER
| LVS_NOCOLUMNHEADER
;
251 HWND hwnd
= CListView::Create(hwndParent
, r
, NULL
, style
, WS_EX_CLIENTEDGE
);
253 AddColumn(0, 150, LVCFMT_LEFT
);
254 AddColumn(1, 120, LVCFMT_LEFT
);
259 VOID
LoadList(ATL::CSimpleArray
<DownloadInfo
> arrInfo
)
261 for (INT i
= 0; i
< arrInfo
.GetSize(); ++i
)
263 AddRow(i
, arrInfo
[i
].szName
.GetString(), DLSTATUS_WAITING
);
267 VOID
SetDownloadStatus(INT ItemIndex
, DownloadStatus Status
)
269 ATL::CStringW szBuffer
= LoadStatusString(Status
);
270 SetItemText(ItemIndex
, 1, szBuffer
.GetString());
273 BOOL
AddItem(INT ItemIndex
, LPWSTR lpText
)
277 ZeroMemory(&Item
, sizeof(Item
));
279 Item
.mask
= LVIF_TEXT
| LVIF_STATE
;
280 Item
.pszText
= lpText
;
281 Item
.iItem
= ItemIndex
;
283 return InsertItem(&Item
);
286 VOID
AddRow(INT RowIndex
, LPCWSTR szAppName
, const DownloadStatus Status
)
288 ATL::CStringW szStatus
= LoadStatusString(Status
);
290 const_cast<LPWSTR
>(szAppName
));
291 SetDownloadStatus(RowIndex
, Status
);
294 BOOL
AddColumn(INT Index
, INT Width
, INT Format
)
297 ZeroMemory(&Column
, sizeof(Column
));
299 Column
.mask
= LVCF_FMT
| LVCF_WIDTH
| LVCF_SUBITEM
;
300 Column
.iSubItem
= Index
;
304 return (InsertColumn(Index
, &Column
) == -1) ? FALSE
: TRUE
;
308 #ifdef USE_CERT_PINNING
309 static BOOL
CertGetSubjectAndIssuer(HINTERNET hFile
, CLocalPtr
<char>& subjectInfo
, CLocalPtr
<char>& issuerInfo
)
311 DWORD certInfoLength
;
312 INTERNET_CERTIFICATE_INFOA certInfo
;
315 size
= sizeof(flags
);
316 if (!InternetQueryOptionA(hFile
, INTERNET_OPTION_SECURITY_FLAGS
, &flags
, &size
))
321 if (!flags
& SECURITY_FLAG_SECURE
)
326 /* Despite what the header indicates, the implementation of INTERNET_CERTIFICATE_INFO is not Unicode-aware. */
327 certInfoLength
= sizeof(certInfo
);
328 if (!InternetQueryOptionA(hFile
,
329 INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT
,
336 subjectInfo
.Attach(certInfo
.lpszSubjectInfo
);
337 issuerInfo
.Attach(certInfo
.lpszIssuerInfo
);
339 if (certInfo
.lpszProtocolName
)
340 LocalFree(certInfo
.lpszProtocolName
);
341 if (certInfo
.lpszSignatureAlgName
)
342 LocalFree(certInfo
.lpszSignatureAlgName
);
343 if (certInfo
.lpszEncryptionAlgName
)
344 LocalFree(certInfo
.lpszEncryptionAlgName
);
346 return certInfo
.lpszSubjectInfo
&& certInfo
.lpszIssuerInfo
;
350 inline VOID
MessageBox_LoadString(HWND hMainWnd
, INT StringID
)
352 ATL::CStringW szMsgText
;
353 if (szMsgText
.LoadStringW(StringID
))
355 MessageBoxW(hMainWnd
, szMsgText
.GetString(), NULL
, MB_OK
| MB_ICONERROR
);
359 // Download dialog (loaddlg.cpp)
360 class CDownloadManager
362 static ATL::CSimpleArray
<DownloadInfo
> AppsDownloadList
;
363 static CDowloadingAppsListView DownloadsListView
;
364 static CDownloaderProgress ProgressBar
;
365 static BOOL bCancelled
;
367 static VOID
UpdateProgress(HWND hDlg
, ULONG ulProgress
, ULONG ulProgressMax
, ULONG ulStatusCode
, LPCWSTR szStatusText
);
369 static VOID
Add(DownloadInfo info
);
370 static VOID
Download(const DownloadInfo
& DLInfo
, BOOL bIsModal
= FALSE
);
371 static INT_PTR CALLBACK
DownloadDlgProc(HWND Dlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
372 static unsigned int WINAPI
ThreadFunc(LPVOID Context
);
373 static VOID
LaunchDownloadDialog(BOOL
);
378 ATL::CSimpleArray
<DownloadInfo
> CDownloadManager::AppsDownloadList
;
379 CDowloadingAppsListView
CDownloadManager::DownloadsListView
;
380 CDownloaderProgress
CDownloadManager::ProgressBar
;
381 BOOL
CDownloadManager::bCancelled
= FALSE
;
382 BOOL
CDownloadManager::bModal
= FALSE
;
384 VOID
CDownloadManager::Add(DownloadInfo info
)
386 AppsDownloadList
.Add(info
);
389 VOID
CDownloadManager::Download(const DownloadInfo
&DLInfo
, BOOL bIsModal
)
391 AppsDownloadList
.RemoveAll();
392 AppsDownloadList
.Add(DLInfo
);
393 LaunchDownloadDialog(bIsModal
);
396 INT_PTR CALLBACK
CDownloadManager::DownloadDlgProc(HWND Dlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
398 static WCHAR szCaption
[MAX_PATH
];
404 HICON hIconSm
, hIconBg
;
405 ATL::CStringW szTempCaption
;
409 hIconBg
= (HICON
) GetClassLongPtrW(hMainWnd
, GCLP_HICON
);
410 hIconSm
= (HICON
) GetClassLongPtrW(hMainWnd
, GCLP_HICONSM
);
412 if (hIconBg
&& hIconSm
)
414 SendMessageW(Dlg
, WM_SETICON
, ICON_BIG
, (LPARAM
) hIconBg
);
415 SendMessageW(Dlg
, WM_SETICON
, ICON_SMALL
, (LPARAM
) hIconSm
);
418 SetWindowLongPtrW(Dlg
, GWLP_USERDATA
, 0);
419 HWND Item
= GetDlgItem(Dlg
, IDC_DOWNLOAD_PROGRESS
);
422 // initialize the default values for our nifty progress bar
423 // and subclass it so that it learns to print a status text
424 ProgressBar
.SubclassWindow(Item
);
425 ProgressBar
.SendMessage(PBM_SETRANGE
, 0, MAKELPARAM(0, 100));
426 ProgressBar
.SendMessage(PBM_SETPOS
, 0, 0);
430 HWND hListView
= DownloadsListView
.Create(Dlg
);
435 DownloadsListView
.LoadList(AppsDownloadList
);
437 // Get a dlg string for later use
438 GetWindowTextW(Dlg
, szCaption
, _countof(szCaption
));
440 // Hide a placeholder from displaying
441 szTempCaption
= szCaption
;
442 szTempCaption
.Replace(L
"%ls", L
"");
443 SetWindowText(Dlg
, szTempCaption
.GetString());
445 ShowWindow(Dlg
, SW_SHOW
);
447 // Start download process
448 DownloadParam
*param
= new DownloadParam(Dlg
, AppsDownloadList
, szCaption
);
449 unsigned int ThreadId
;
450 HANDLE Thread
= (HANDLE
)_beginthreadex(NULL
, 0, ThreadFunc
, (void *) param
, 0, &ThreadId
);
458 AppsDownloadList
.RemoveAll();
463 if (wParam
== IDCANCEL
)
466 PostMessageW(Dlg
, WM_CLOSE
, 0, 0);
471 if (CDownloadManager::bModal
)
477 ::DestroyWindow(Dlg
);
486 BOOL UrlHasBeenCopied
;
488 VOID
CDownloadManager::UpdateProgress(
493 LPCWSTR szStatusText
)
497 if (!IsWindow(hDlg
)) return;
498 ProgressBar
.SetProgress(ulProgress
, ulProgressMax
);
500 if (!IsWindow(hDlg
)) return;
501 Item
= GetDlgItem(hDlg
, IDC_DOWNLOAD_STATUS
);
502 if (Item
&& szStatusText
&& wcslen(szStatusText
) > 0 && UrlHasBeenCopied
== FALSE
)
504 SIZE_T len
= wcslen(szStatusText
) + 1;
508 /* beautify our url for display purposes */
509 if (!InternetCanonicalizeUrlW(szStatusText
, buf
.GetBuffer(len
), &dummyLen
, ICU_DECODE
| ICU_NO_ENCODE
))
511 /* just use the original */
520 /* paste it into our dialog and don't do it again in this instance */
521 SendMessageW(Item
, WM_SETTEXT
, 0, (LPARAM
) buf
.GetString());
522 UrlHasBeenCopied
= TRUE
;
531 CLocalPtr
<WCHAR
> lpMsg
;
533 if (!FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER
|
534 FORMAT_MESSAGE_IGNORE_INSERTS
|
535 (bInetError
? FORMAT_MESSAGE_FROM_HMODULE
: FORMAT_MESSAGE_FROM_SYSTEM
),
536 (bInetError
? GetModuleHandleW(L
"wininet.dll") : NULL
),
542 DPRINT1("FormatMessageW unexpected failure (err %d)\n", GetLastError());
546 MessageBoxW(hWndOwner
, lpMsg
, NULL
, MB_OK
| MB_ICONERROR
);
550 unsigned int WINAPI
CDownloadManager::ThreadFunc(LPVOID param
)
555 HWND hDlg
= static_cast<DownloadParam
*>(param
)->Dialog
;
559 ULONG dwContentLen
, dwBytesWritten
, dwBytesRead
, dwStatus
;
560 ULONG dwCurrentBytesRead
= 0;
561 ULONG dwStatusLen
= sizeof(dwStatus
);
563 BOOL bTempfile
= FALSE
;
565 HINTERNET hOpen
= NULL
;
566 HINTERNET hFile
= NULL
;
567 HANDLE hOut
= INVALID_HANDLE_VALUE
;
569 unsigned char lpBuffer
[4096];
570 LPCWSTR lpszAgent
= L
"RApps/1.1";
571 URL_COMPONENTSW urlComponents
;
572 size_t urlLength
, filenameLength
;
574 const ATL::CSimpleArray
<DownloadInfo
> &InfoArray
= static_cast<DownloadParam
*>(param
)->AppInfo
;
575 LPCWSTR szCaption
= static_cast<DownloadParam
*>(param
)->szCaption
;
576 ATL::CStringW szNewCaption
;
578 const DWORD dwUrlConnectFlags
= INTERNET_FLAG_DONT_CACHE
| INTERNET_FLAG_PRAGMA_NOCACHE
| INTERNET_FLAG_KEEP_CONNECTION
;
580 if (InfoArray
.GetSize() <= 0)
582 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD
);
586 for (iAppId
= 0; iAppId
< InfoArray
.GetSize(); ++iAppId
)
588 // Reset progress bar
589 if (!IsWindow(hDlg
)) break;
590 Item
= GetDlgItem(hDlg
, IDC_DOWNLOAD_PROGRESS
);
593 ProgressBar
.SetMarquee(FALSE
);
594 ProgressBar
.SendMessage(WM_SETTEXT
, 0, (LPARAM
) L
"");
595 ProgressBar
.SendMessage(PBM_SETPOS
, 0, 0);
598 // is this URL an update package for RAPPS? if so store it in a different place
599 if (InfoArray
[iAppId
].DLType
!= DLTYPE_APPLICATION
)
601 if (!GetStorageDirectory(Path
))
603 ShowLastError(hMainWnd
, FALSE
, GetLastError());
609 Path
= SettingsInfo
.szDownloadDir
;
612 // Change caption to show the currently downloaded app
613 switch(InfoArray
[iAppId
].DLType
)
615 case DLTYPE_APPLICATION
:
616 szNewCaption
.Format(szCaption
, InfoArray
[iAppId
].szName
.GetString());
618 case DLTYPE_DBUPDATE
:
619 szNewCaption
.LoadStringW(IDS_DL_DIALOG_DB_DOWNLOAD_DISP
);
621 case DLTYPE_DBUPDATE_UNOFFICIAL
:
622 szNewCaption
.LoadStringW(IDS_DL_DIALOG_DB_UNOFFICIAL_DOWNLOAD_DISP
);
626 if (!IsWindow(hDlg
)) goto end
;
627 SetWindowTextW(hDlg
, szNewCaption
.GetString());
629 // build the path for the download
630 p
= wcsrchr(InfoArray
[iAppId
].szUrl
.GetString(), L
'/');
631 q
= wcsrchr(InfoArray
[iAppId
].szUrl
.GetString(), L
'?');
633 // do we have a final slash separator?
636 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_PATH
);
640 // prepare the tentative length of the filename, maybe we've to remove part of it later on
641 filenameLength
= wcslen(p
) * sizeof(WCHAR
);
643 /* do we have query arguments in the target URL after the filename? account for them
644 (e.g. https://example.org/myfile.exe?no_adware_plz) */
645 if (q
&& q
> p
&& (q
- p
) > 0)
646 filenameLength
-= wcslen(q
- 1) * sizeof(WCHAR
);
648 // is the path valid? can we access it?
649 if (GetFileAttributesW(Path
.GetString()) == INVALID_FILE_ATTRIBUTES
)
651 if (!CreateDirectoryW(Path
.GetString(), NULL
))
653 ShowLastError(hMainWnd
, FALSE
, GetLastError());
658 // append a \ to the provided file system path, and the filename portion from the URL after that
660 PathAddBackslashW(Path
.GetBuffer(MAX_PATH
));
661 switch (InfoArray
[iAppId
].DLType
)
663 case DLTYPE_DBUPDATE
:
664 case DLTYPE_DBUPDATE_UNOFFICIAL
:
665 PathAppendW(Path
.GetBuffer(), APPLICATION_DATABASE_NAME
);
667 case DLTYPE_APPLICATION
:
668 PathAppendW(Path
.GetBuffer(), (LPWSTR
)(p
+ 1)); // use the filename retrieved from URL
671 Path
.ReleaseBuffer();
673 if ((InfoArray
[iAppId
].DLType
== DLTYPE_APPLICATION
) && InfoArray
[iAppId
].szSHA1
[0] && GetFileAttributesW(Path
.GetString()) != INVALID_FILE_ATTRIBUTES
)
675 // only open it in case of total correctness
676 if (VerifyInteg(InfoArray
[iAppId
].szSHA1
.GetString(), Path
))
680 // Add the download URL
681 if (!IsWindow(hDlg
)) goto end
;
682 SetDlgItemTextW(hDlg
, IDC_DOWNLOAD_STATUS
, InfoArray
[iAppId
].szUrl
.GetString());
684 DownloadsListView
.SetDownloadStatus(iAppId
, DLSTATUS_DOWNLOADING
);
687 UrlHasBeenCopied
= FALSE
;
690 /* FIXME: this should just be using the system-wide proxy settings */
691 switch (SettingsInfo
.Proxy
)
695 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, 0);
697 case 1: // direct (no proxy)
698 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_DIRECT
, NULL
, NULL
, 0);
701 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PROXY
, SettingsInfo
.szProxyServer
, SettingsInfo
.szNoProxyFor
, 0);
707 ShowLastError(hMainWnd
, TRUE
, GetLastError());
711 dwStatusLen
= sizeof(dwStatus
);
713 memset(&urlComponents
, 0, sizeof(urlComponents
));
714 urlComponents
.dwStructSize
= sizeof(urlComponents
);
716 urlLength
= InfoArray
[iAppId
].szUrl
.GetLength();
717 urlComponents
.dwSchemeLength
= urlLength
+ 1;
718 urlComponents
.lpszScheme
= (LPWSTR
) malloc(urlComponents
.dwSchemeLength
* sizeof(WCHAR
));
720 if (!InternetCrackUrlW(InfoArray
[iAppId
].szUrl
, urlLength
+ 1, ICU_DECODE
| ICU_ESCAPE
, &urlComponents
))
722 ShowLastError(hMainWnd
, TRUE
, GetLastError());
728 if (urlComponents
.nScheme
== INTERNET_SCHEME_HTTP
||
729 urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
)
731 hFile
= InternetOpenUrlW(hOpen
, InfoArray
[iAppId
].szUrl
.GetString(), NULL
, 0,
736 if (!ShowLastError(hMainWnd
, TRUE
, GetLastError()))
738 /* Workaround for CORE-17377 */
739 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD2
);
745 if (!HttpQueryInfoW(hFile
, HTTP_QUERY_STATUS_CODE
| HTTP_QUERY_FLAG_NUMBER
, &dwStatus
, &dwStatusLen
, NULL
))
747 ShowLastError(hMainWnd
, TRUE
, GetLastError());
751 if (dwStatus
!= HTTP_STATUS_OK
)
753 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD
);
757 // query content length
758 HttpQueryInfoW(hFile
, HTTP_QUERY_CONTENT_LENGTH
| HTTP_QUERY_FLAG_NUMBER
, &dwContentLen
, &dwStatusLen
, NULL
);
760 else if (urlComponents
.nScheme
== INTERNET_SCHEME_FTP
)
762 // force passive mode on FTP
763 hFile
= InternetOpenUrlW(hOpen
, InfoArray
[iAppId
].szUrl
, NULL
, 0,
764 dwUrlConnectFlags
| INTERNET_FLAG_PASSIVE
,
768 if (!ShowLastError(hMainWnd
, TRUE
, GetLastError()))
770 /* Workaround for CORE-17377 */
771 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD2
);
776 dwContentLen
= FtpGetFileSize(hFile
, &dwStatus
);
778 else if (urlComponents
.nScheme
== INTERNET_SCHEME_FILE
)
780 // Add support for the file scheme so testing locally is simpler
781 WCHAR LocalFilePath
[MAX_PATH
];
782 DWORD cchPath
= _countof(LocalFilePath
);
783 // Ideally we would use PathCreateFromUrlAlloc here, but that is not exported (yet)
784 HRESULT hr
= PathCreateFromUrlW(InfoArray
[iAppId
].szUrl
, LocalFilePath
, &cchPath
, 0);
787 if (CopyFileW(LocalFilePath
, Path
, FALSE
))
793 ShowLastError(hMainWnd
, FALSE
, GetLastError());
799 ShowLastError(hMainWnd
, FALSE
, hr
);
806 // Someone was nice enough to add this, let's use it
807 if (InfoArray
[iAppId
].SizeInBytes
)
809 dwContentLen
= InfoArray
[iAppId
].SizeInBytes
;
813 // content-length is not known, enable marquee mode
814 ProgressBar
.SetMarquee(TRUE
);
818 free(urlComponents
.lpszScheme
);
820 #ifdef USE_CERT_PINNING
821 // are we using HTTPS to download the RAPPS update package? check if the certificate is original
822 if ((urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
) &&
823 (InfoArray
[iAppId
].DLType
== DLTYPE_DBUPDATE
))
825 CLocalPtr
<char> subjectName
, issuerName
;
827 bool bAskQuestion
= false;
828 if (!CertGetSubjectAndIssuer(hFile
, subjectName
, issuerName
))
830 szMsgText
.LoadStringW(IDS_UNABLE_TO_QUERY_CERT
);
835 if (strcmp(subjectName
, CERT_SUBJECT_INFO
) ||
836 (strcmp(issuerName
, CERT_ISSUER_INFO_OLD
) &&
837 strcmp(issuerName
, CERT_ISSUER_INFO_NEW
)))
839 szMsgText
.Format(IDS_MISMATCH_CERT_INFO
, (char*)subjectName
, (const char*)issuerName
);
846 if (MessageBoxA(hMainWnd
, szMsgText
.GetString(), NULL
, MB_YESNO
| MB_ICONERROR
) != IDYES
)
854 hOut
= CreateFileW(Path
.GetString(), GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, CREATE_ALWAYS
, 0, NULL
);
856 if (hOut
== INVALID_HANDLE_VALUE
)
858 ShowLastError(hMainWnd
, FALSE
, GetLastError());
862 dwCurrentBytesRead
= 0;
865 if (!InternetReadFile(hFile
, lpBuffer
, _countof(lpBuffer
), &dwBytesRead
))
867 ShowLastError(hMainWnd
, TRUE
, GetLastError());
871 if (!WriteFile(hOut
, &lpBuffer
[0], dwBytesRead
, &dwBytesWritten
, NULL
))
873 ShowLastError(hMainWnd
, FALSE
, GetLastError());
877 dwCurrentBytesRead
+= dwBytesRead
;
878 if (!IsWindow(hDlg
)) goto end
;
879 UpdateProgress(hDlg
, dwCurrentBytesRead
, dwContentLen
, 0, InfoArray
[iAppId
].szUrl
.GetString());
880 } while (dwBytesRead
&& !bCancelled
);
883 hOut
= INVALID_HANDLE_VALUE
;
887 DPRINT1("Operation cancelled\n");
893 // set progress bar to 100%
894 ProgressBar
.SetMarquee(FALSE
);
896 dwContentLen
= dwCurrentBytesRead
;
897 if (!IsWindow(hDlg
)) goto end
;
898 UpdateProgress(hDlg
, dwCurrentBytesRead
, dwContentLen
, 0, InfoArray
[iAppId
].szUrl
.GetString());
901 /* if this thing isn't a RAPPS update and it has a SHA-1 checksum
902 verify its integrity by using the native advapi32.A_SHA1 functions */
903 if ((InfoArray
[iAppId
].DLType
== DLTYPE_APPLICATION
) && InfoArray
[iAppId
].szSHA1
[0] != 0)
905 ATL::CStringW szMsgText
;
907 // change a few strings in the download dialog to reflect the verification process
908 if (!szMsgText
.LoadStringW(IDS_INTEG_CHECK_TITLE
))
910 DPRINT1("Unable to load string\n");
914 if (!IsWindow(hDlg
)) goto end
;
915 SetWindowTextW(hDlg
, szMsgText
.GetString());
916 SendMessageW(GetDlgItem(hDlg
, IDC_DOWNLOAD_STATUS
), WM_SETTEXT
, 0, (LPARAM
) Path
.GetString());
918 // this may take a while, depending on the file size
919 if (!VerifyInteg(InfoArray
[iAppId
].szSHA1
.GetString(), Path
.GetString()))
921 if (!szMsgText
.LoadStringW(IDS_INTEG_CHECK_FAIL
))
923 DPRINT1("Unable to load string\n");
927 if (!IsWindow(hDlg
)) goto end
;
928 MessageBoxW(hDlg
, szMsgText
.GetString(), NULL
, MB_OK
| MB_ICONERROR
);
934 DownloadsListView
.SetDownloadStatus(iAppId
, DLSTATUS_WAITING_INSTALL
);
937 if (InfoArray
[iAppId
].DLType
== DLTYPE_APPLICATION
)
939 SHELLEXECUTEINFOW shExInfo
= {0};
940 shExInfo
.cbSize
= sizeof(shExInfo
);
941 shExInfo
.fMask
= SEE_MASK_NOCLOSEPROCESS
;
942 shExInfo
.lpVerb
= L
"open";
943 shExInfo
.lpFile
= Path
.GetString();
944 shExInfo
.lpParameters
= L
"";
945 shExInfo
.nShow
= SW_SHOW
;
947 /* FIXME: Do we want to log installer status? */
948 WriteLogMessage(EVENTLOG_SUCCESS
, MSG_SUCCESS_INSTALL
, InfoArray
[iAppId
].szName
);
950 if (ShellExecuteExW(&shExInfo
))
952 //reflect installation progress in the titlebar
953 //TODO: make a separate string with a placeholder to include app name?
954 ATL::CStringW szMsgText
= LoadStatusString(DLSTATUS_INSTALLING
);
955 if (!IsWindow(hDlg
)) goto end
;
956 SetWindowTextW(hDlg
, szMsgText
.GetString());
958 DownloadsListView
.SetDownloadStatus(iAppId
, DLSTATUS_INSTALLING
);
960 //TODO: issue an install operation separately so that the apps could be downloaded in the background
961 WaitForSingleObject(shExInfo
.hProcess
, INFINITE
);
962 CloseHandle(shExInfo
.hProcess
);
966 ShowLastError(hMainWnd
, FALSE
, GetLastError());
971 if (hOut
!= INVALID_HANDLE_VALUE
)
975 InternetCloseHandle(hFile
);
976 InternetCloseHandle(hOpen
);
980 if (bCancelled
|| (SettingsInfo
.bDelInstaller
&& (InfoArray
[iAppId
].DLType
== DLTYPE_APPLICATION
)))
981 DeleteFileW(Path
.GetString());
984 if (!IsWindow(hDlg
)) return 0;
985 DownloadsListView
.SetDownloadStatus(iAppId
, DLSTATUS_FINISHED
);
988 delete static_cast<DownloadParam
*>(param
);
989 if (!IsWindow(hDlg
)) return 0;
990 SendMessageW(hDlg
, WM_CLOSE
, 0, 0);
994 //TODO: Reuse the dialog
995 VOID
CDownloadManager::LaunchDownloadDialog(BOOL bIsModal
)
997 CDownloadManager::bModal
= bIsModal
;
1001 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
),
1007 CreateDialogW(hInst
,
1008 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
),
1016 BOOL
DownloadListOfApplications(const ATL::CSimpleArray
<CAvailableApplicationInfo
>& AppsList
, BOOL bIsModal
)
1018 if (AppsList
.GetSize() == 0)
1021 // Initialize shared variables
1022 for (INT i
= 0; i
< AppsList
.GetSize(); ++i
)
1024 CDownloadManager::Add(AppsList
[i
]); // implicit conversion to DownloadInfo
1027 // Create a dialog and issue a download process
1028 CDownloadManager::LaunchDownloadDialog(bIsModal
);
1033 BOOL
DownloadApplication(CAvailableApplicationInfo
* pAppInfo
, BOOL bIsModal
)
1038 CDownloadManager::Download(*pAppInfo
, bIsModal
);
1042 VOID
DownloadApplicationsDB(LPCWSTR lpUrl
, BOOL IsOfficial
)
1044 static DownloadInfo DatabaseDLInfo
;
1045 DatabaseDLInfo
.szUrl
= lpUrl
;
1046 DatabaseDLInfo
.szName
.LoadStringW(IDS_DL_DIALOG_DB_DISP
);
1047 DatabaseDLInfo
.DLType
= IsOfficial
? DLTYPE_DBUPDATE
: DLTYPE_DBUPDATE_UNOFFICIAL
;
1048 CDownloadManager::Download(DatabaseDLInfo
, TRUE
);