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
.GetString());
117 Item
= GetDlgItem(m_hDialog
, IDC_DOWNLOAD_STATUS
);
118 if (Item
&& szStatusText
&& wcslen(szStatusText
) > 0 && m_UrlHasBeenCopied
== FALSE
)
120 DWORD len
= wcslen(szStatusText
) + 1;
123 /* beautify our url for display purposes */
124 if (!InternetCanonicalizeUrlW(szStatusText
, buf
.GetBuffer(len
), &len
, ICU_DECODE
| ICU_NO_ENCODE
))
126 /* just use the original */
135 /* paste it into our dialog and don't do it again in this instance */
136 SendMessageW(Item
, WM_SETTEXT
, 0, (LPARAM
) buf
.GetString());
137 m_UrlHasBeenCopied
= TRUE
;
141 r
= GetWindowLongPtrW(m_hDialog
, GWLP_USERDATA
);
142 if (0 != r
|| 0 != GetLastError())
144 *m_pbCancelled
= TRUE
;
151 virtual HRESULT STDMETHODCALLTYPE
OnStopBinding(
158 virtual HRESULT STDMETHODCALLTYPE
GetBindInfo(
165 virtual HRESULT STDMETHODCALLTYPE
OnDataAvailable(
168 FORMATETC
*pformatetc
,
174 virtual HRESULT STDMETHODCALLTYPE
OnObjectAvailable(
181 BEGIN_COM_MAP(CDownloadDialog
)
182 COM_INTERFACE_ENTRY_IID(IID_IBindStatusCallback
, IBindStatusCallback
)
187 HRESULT WINAPI
CDownloadDialog_Constructor(HWND Dlg
, BOOL
*pbCancelled
, REFIID riid
, LPVOID
*ppv
)
189 return ShellObjectCreatorInit
<CDownloadDialog
>(Dlg
, pbCancelled
, riid
, ppv
);
192 #ifdef USE_CERT_PINNING
193 static BOOL
CertIsValid(HINTERNET hInternet
, LPWSTR lpszHostName
)
197 DWORD certInfoLength
;
199 INTERNET_CERTIFICATE_INFOW certInfo
;
201 hConnect
= InternetConnectW(hInternet
, lpszHostName
, INTERNET_DEFAULT_HTTPS_PORT
, NULL
, NULL
, INTERNET_SERVICE_HTTP
, INTERNET_FLAG_SECURE
, 0);
204 hRequest
= HttpOpenRequestW(hConnect
, L
"HEAD", NULL
, NULL
, NULL
, NULL
, INTERNET_FLAG_SECURE
, 0);
205 if (hRequest
!= NULL
)
207 Ret
= HttpSendRequestW(hRequest
, L
"", 0, NULL
, 0);
210 certInfoLength
= sizeof(certInfo
);
211 Ret
= InternetQueryOptionW(hRequest
,
212 INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT
,
217 if (certInfo
.lpszEncryptionAlgName
)
218 LocalFree(certInfo
.lpszEncryptionAlgName
);
219 if (certInfo
.lpszIssuerInfo
)
221 if (strcmp((LPSTR
) certInfo
.lpszIssuerInfo
, CERT_ISSUER_INFO
) != 0)
223 LocalFree(certInfo
.lpszIssuerInfo
);
225 if (certInfo
.lpszProtocolName
)
226 LocalFree(certInfo
.lpszProtocolName
);
227 if (certInfo
.lpszSignatureAlgName
)
228 LocalFree(certInfo
.lpszSignatureAlgName
);
229 if (certInfo
.lpszSubjectInfo
)
231 if (strcmp((LPSTR
) certInfo
.lpszSubjectInfo
, CERT_SUBJECT_INFO
) != 0)
233 LocalFree(certInfo
.lpszSubjectInfo
);
237 InternetCloseHandle(hRequest
);
239 InternetCloseHandle(hConnect
);
246 MessageBox_LoadString(HWND hMainWnd
, INT StringID
)
248 ATL::CString szMsgText
;
249 if (szMsgText
.LoadStringW(hInst
, StringID
))
250 MessageBoxW(hMainWnd
, szMsgText
.GetString(), NULL
, MB_OK
| MB_ICONERROR
);
255 ThreadFunc(LPVOID Context
)
257 CComPtr
<IBindStatusCallback
> dl
;
260 HWND Dlg
= (HWND
) Context
;
261 ULONG dwContentLen
, dwBytesWritten
, dwBytesRead
, dwStatus
;
262 ULONG dwCurrentBytesRead
= 0;
263 ULONG dwStatusLen
= sizeof(dwStatus
);
264 BOOL bCancelled
= FALSE
;
265 BOOL bTempfile
= FALSE
;
267 HINTERNET hOpen
= NULL
;
268 HINTERNET hFile
= NULL
;
269 HANDLE hOut
= INVALID_HANDLE_VALUE
;
270 unsigned char lpBuffer
[4096];
271 LPCWSTR lpszAgent
= L
"RApps/1.0";
272 URL_COMPONENTS urlComponents
;
273 size_t urlLength
, filenameLength
;
275 /* build the path for the download */
276 p
= wcsrchr(AppInfo
->szUrlDownload
, L
'/');
277 q
= wcsrchr(AppInfo
->szUrlDownload
, L
'?');
279 /* do we have a final slash separator? */
283 /* prepare the tentative length of the filename, maybe we've to remove part of it later on */
284 filenameLength
= wcslen(p
) * sizeof(WCHAR
);
286 /* do we have query arguments in the target URL after the filename? account for them
287 (e.g. https://example.org/myfile.exe?no_adware_plz) */
288 if (q
&& q
> p
&& (q
- p
) > 0)
289 filenameLength
-= wcslen(q
- 1) * sizeof(WCHAR
);
291 /* is this URL an update package for RAPPS? if so store it in a different place */
292 if (AppInfo
->szUrlDownload
== APPLICATION_DATABASE_URL
)
295 if (!GetStorageDirectory(Path
))
300 Path
= SettingsInfo
.szDownloadDir
;
303 /* is the path valid? can we access it? */
304 if (GetFileAttributesW(Path
.GetString()) == INVALID_FILE_ATTRIBUTES
)
306 if (!CreateDirectoryW(Path
.GetString(), NULL
))
310 /* append a \ to the provided file system path, and the filename portion from the URL after that */
311 Path
.Format(L
"\\%ls", (p
+ 1));
313 if (!bCab
&& AppInfo
->szSHA1
[0] && GetFileAttributesW(Path
.GetString()) != INVALID_FILE_ATTRIBUTES
)
315 /* only open it in case of total correctness */
316 if (VerifyInteg(AppInfo
->szSHA1
, Path
))
322 CDownloadDialog_Constructor(Dlg
, &bCancelled
, IID_PPV_ARG(IBindStatusCallback
, &dl
));
327 /* FIXME: this should just be using the system-wide proxy settings */
328 switch (SettingsInfo
.Proxy
)
330 case 0: /* preconfig */
331 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, 0);
333 case 1: /* direct (no proxy) */
334 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_DIRECT
, NULL
, NULL
, 0);
336 case 2: /* use proxy */
337 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PROXY
, SettingsInfo
.szProxyServer
, SettingsInfo
.szNoProxyFor
, 0);
339 default: /* preconfig */
340 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, 0);
347 hFile
= InternetOpenUrlW(hOpen
, AppInfo
->szUrlDownload
, NULL
, 0, INTERNET_FLAG_PRAGMA_NOCACHE
| INTERNET_FLAG_KEEP_CONNECTION
, 0);
350 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD2
);
354 if (!HttpQueryInfoW(hFile
, HTTP_QUERY_STATUS_CODE
| HTTP_QUERY_FLAG_NUMBER
, &dwStatus
, &dwStatusLen
, NULL
))
357 if (dwStatus
!= HTTP_STATUS_OK
)
359 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD
);
363 dwStatusLen
= sizeof(dwStatus
);
365 memset(&urlComponents
, 0, sizeof(urlComponents
));
366 urlComponents
.dwStructSize
= sizeof(urlComponents
);
368 urlLength
= AppInfo
->szUrlDownload
.GetLength();
369 urlComponents
.dwSchemeLength
= urlLength
+ 1;
370 urlComponents
.lpszScheme
= (LPWSTR
) malloc(urlComponents
.dwSchemeLength
* sizeof(WCHAR
));
371 urlComponents
.dwHostNameLength
= urlLength
+ 1;
372 urlComponents
.lpszHostName
= (LPWSTR
) malloc(urlComponents
.dwHostNameLength
* sizeof(WCHAR
));
374 if (!InternetCrackUrlW(AppInfo
->szUrlDownload
, urlLength
+ 1, ICU_DECODE
| ICU_ESCAPE
, &urlComponents
))
377 if (urlComponents
.nScheme
== INTERNET_SCHEME_HTTP
|| urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
)
378 HttpQueryInfo(hFile
, HTTP_QUERY_CONTENT_LENGTH
| HTTP_QUERY_FLAG_NUMBER
, &dwContentLen
, &dwStatus
, 0);
380 if (urlComponents
.nScheme
== INTERNET_SCHEME_FTP
)
381 dwContentLen
= FtpGetFileSize(hFile
, &dwStatus
);
383 #ifdef USE_CERT_PINNING
384 /* are we using HTTPS to download the RAPPS update package? check if the certificate is original */
385 if ((urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
) &&
386 (wcscmp(AppInfo
->szUrlDownload
, APPLICATION_DATABASE_URL
) == 0) &&
387 (!CertIsValid(hOpen
, urlComponents
.lpszHostName
)))
389 MessageBox_LoadString(hMainWnd
, IDS_CERT_DOES_NOT_MATCH
);
394 free(urlComponents
.lpszScheme
);
395 free(urlComponents
.lpszHostName
);
397 hOut
= CreateFileW(Path
.GetString(), GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, CREATE_ALWAYS
, 0, NULL
);
399 if (hOut
== INVALID_HANDLE_VALUE
)
404 if (!InternetReadFile(hFile
, lpBuffer
, _countof(lpBuffer
), &dwBytesRead
))
406 MessageBox_LoadString(hMainWnd
, IDS_INTERRUPTED_DOWNLOAD
);
410 if (!WriteFile(hOut
, &lpBuffer
[0], dwBytesRead
, &dwBytesWritten
, NULL
))
412 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_WRITE
);
416 dwCurrentBytesRead
+= dwBytesRead
;
417 dl
->OnProgress(dwCurrentBytesRead
, dwContentLen
, 0, AppInfo
->szUrlDownload
);
418 } while (dwBytesRead
&& !bCancelled
);
421 hOut
= INVALID_HANDLE_VALUE
;
426 /* if this thing isn't a RAPPS update and it has a SHA-1 checksum
427 verify its integrity by using the native advapi32.A_SHA1 functions */
428 if (!bCab
&& AppInfo
->szSHA1
[0] != 0)
430 ATL::CStringW szMsgText
;
432 /* change a few strings in the download dialog to reflect the verification process */
433 if (!szMsgText
.LoadStringW(hInst
, IDS_INTEG_CHECK_TITLE
))
436 SetWindowText(Dlg
, szMsgText
.GetString());
437 SendMessageW(GetDlgItem(Dlg
, IDC_DOWNLOAD_STATUS
), WM_SETTEXT
, 0, (LPARAM
) Path
.GetString());
439 /* this may take a while, depending on the file size */
440 if (!VerifyInteg(AppInfo
->szSHA1
, Path
.GetString()))
442 if (!szMsgText
.LoadStringW(hInst
, IDS_INTEG_CHECK_FAIL
))
445 MessageBoxW(Dlg
, szMsgText
.GetString(), NULL
, MB_OK
| MB_ICONERROR
);
450 ShowWindow(Dlg
, SW_HIDE
);
455 ShellExecuteW(NULL
, L
"open", Path
.GetString(), NULL
, NULL
, SW_SHOWNORMAL
);
458 if (hOut
!= INVALID_HANDLE_VALUE
)
461 InternetCloseHandle(hFile
);
462 InternetCloseHandle(hOpen
);
466 if (bCancelled
|| (SettingsInfo
.bDelInstaller
&& !bCab
))
467 DeleteFileW(Path
.GetString());
477 DownloadProgressProc(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
, UINT_PTR uIdSubclass
, DWORD_PTR dwRefData
)
479 static ATL::CStringW szProgressText
;
487 szProgressText
= (PCWSTR
) lParam
;
496 HDC hDC
= BeginPaint(hWnd
, &ps
), hdcMem
;
500 UINT win_width
, win_height
;
502 GetClientRect(hWnd
, &myRect
);
504 /* grab the progress bar rect size */
505 win_width
= myRect
.right
- myRect
.left
;
506 win_height
= myRect
.bottom
- myRect
.top
;
508 /* create an off-screen DC for double-buffering */
509 hdcMem
= CreateCompatibleDC(hDC
);
510 hbmMem
= CreateCompatibleBitmap(hDC
, win_width
, win_height
);
512 hOld
= SelectObject(hdcMem
, hbmMem
);
514 /* call the original draw code and redirect it to our memory buffer */
515 DefSubclassProc(hWnd
, uMsg
, (WPARAM
) hdcMem
, lParam
);
517 /* draw our nifty progress text over it */
518 SelectFont(hdcMem
, GetStockFont(DEFAULT_GUI_FONT
));
519 DrawShadowText(hdcMem
, szProgressText
.GetString(), szProgressText
.GetLength(),
521 DT_CENTER
| DT_VCENTER
| DT_NOPREFIX
| DT_SINGLELINE
,
522 GetSysColor(COLOR_CAPTIONTEXT
),
523 GetSysColor(COLOR_3DSHADOW
),
526 /* transfer the off-screen DC to the screen */
527 BitBlt(hDC
, 0, 0, win_width
, win_height
, hdcMem
, 0, 0, SRCCOPY
);
529 /* free the off-screen DC */
530 SelectObject(hdcMem
, hOld
);
531 DeleteObject(hbmMem
);
538 /* Raymond Chen says that we should safely unsubclass all the things!
539 (http://blogs.msdn.com/b/oldnewthing/archive/2003/11/11/55653.aspx) */
542 szProgressText
.Empty();
543 RemoveWindowSubclass(hWnd
, DownloadProgressProc
, uIdSubclass
);
547 return DefSubclassProc(hWnd
, uMsg
, wParam
, lParam
);
553 DownloadDlgProc(HWND Dlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
563 HICON hIconSm
= NULL
, hIconBg
= NULL
;
565 hIconBg
= (HICON
) GetClassLongPtr(hMainWnd
, GCLP_HICON
);
566 hIconSm
= (HICON
) GetClassLongPtr(hMainWnd
, GCLP_HICONSM
);
568 if (hIconBg
&& hIconSm
)
570 SendMessageW(Dlg
, WM_SETICON
, ICON_BIG
, (LPARAM
) hIconBg
);
571 SendMessageW(Dlg
, WM_SETICON
, ICON_SMALL
, (LPARAM
) hIconSm
);
574 SetWindowLongPtrW(Dlg
, GWLP_USERDATA
, 0);
575 Item
= GetDlgItem(Dlg
, IDC_DOWNLOAD_PROGRESS
);
578 /* initialize the default values for our nifty progress bar
579 and subclass it so that it learns to print a status text */
580 SendMessageW(Item
, PBM_SETRANGE
, 0, MAKELPARAM(0, 100));
581 SendMessageW(Item
, PBM_SETPOS
, 0, 0);
583 SetWindowSubclass(Item
, DownloadProgressProc
, 0, 0);
586 /* add a neat placeholder until the download URL is retrieved */
587 Item
= GetDlgItem(Dlg
, IDC_DOWNLOAD_STATUS
);
588 SendMessageW(Item
, WM_SETTEXT
, 0, (LPARAM
) L
"\x2022 \x2022 \x2022");
590 Thread
= CreateThread(NULL
, 0, ThreadFunc
, Dlg
, 0, &ThreadId
);
597 if (wParam
== IDCANCEL
)
599 SetWindowLongPtrW(Dlg
, GWLP_USERDATA
, 1);
600 PostMessageW(Dlg
, WM_CLOSE
, 0, 0);
614 DownloadApplication(INT Index
)
616 if (!IS_AVAILABLE_ENUM(SelectedEnumType
))
619 AppInfo
= (PAPPLICATION_INFO
) ListViewGetlParam(Index
);
623 WriteLogMessage(EVENTLOG_SUCCESS
, MSG_SUCCESS_INSTALL
, AppInfo
->szName
.GetString());
626 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
),
634 DownloadApplicationsDB(LPCWSTR lpUrl
)
636 APPLICATION_INFO IntInfo
;
637 IntInfo
.szUrlDownload
= lpUrl
;
642 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
),