1 /* PROJECT: ReactOS Applications Manager
2 * LICENSE: GPL - See COPYING in the top level directory
3 * FILE: base/applications/rapps/loaddlg.c
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)
12 * Based on Wine dlls/shdocvw/shdocvw_main.c
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
34 static PAPPLICATION_INFO AppInfo
;
36 typedef struct _IBindStatusCallbackImpl
38 const IBindStatusCallbackVtbl
*vtbl
;
42 BOOL UrlHasBeenCopied
;
43 WCHAR ProgressText
[MAX_PATH
];
44 } IBindStatusCallbackImpl
;
48 dlQueryInterface(IBindStatusCallback
* This
, REFIID riid
, void** ppvObject
)
50 if (!ppvObject
) return E_POINTER
;
52 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IBindStatusCallback
))
54 IBindStatusCallback_AddRef(This
);
64 dlAddRef(IBindStatusCallback
* iface
)
66 IBindStatusCallbackImpl
*This
= (IBindStatusCallbackImpl
*) iface
;
67 return InterlockedIncrement(&This
->ref
);
72 dlRelease(IBindStatusCallback
* iface
)
74 IBindStatusCallbackImpl
*This
= (IBindStatusCallbackImpl
*) iface
;
75 DWORD ref
= InterlockedDecrement(&This
->ref
);
79 DestroyWindow(This
->hDialog
);
80 HeapFree(GetProcessHeap(), 0, This
);
88 dlOnStartBinding(IBindStatusCallback
* iface
, DWORD dwReserved
, IBinding
* pib
)
95 dlGetPriority(IBindStatusCallback
* iface
, LONG
* pnPriority
)
102 dlOnLowResource( IBindStatusCallback
* iface
, DWORD reserved
)
109 dlOnProgress(IBindStatusCallback
* iface
,
113 LPCWSTR szStatusText
)
115 IBindStatusCallbackImpl
*This
= (IBindStatusCallbackImpl
*) iface
;
119 Item
= GetDlgItem(This
->hDialog
, IDC_DOWNLOAD_PROGRESS
);
120 if (Item
&& ulProgressMax
)
122 WCHAR szProgress
[100];
123 WCHAR szProgressMax
[100];
124 UINT uiPercentage
= ((ULONGLONG
)ulProgress
* 100) / ulProgressMax
;
126 /* send the current progress to the progress bar */
127 SendMessageW(Item
, PBM_SETPOS
, uiPercentage
, 0);
129 /* format the bits and bytes into pretty and accessible units... */
130 StrFormatByteSizeW(ulProgress
, szProgress
, _countof(szProgress
));
131 StrFormatByteSizeW(ulProgressMax
, szProgressMax
, _countof(szProgressMax
));
133 /* ...and post all of it to our subclassed progress bar text subroutine */
134 StringCbPrintfW(This
->ProgressText
,
135 sizeof(This
->ProgressText
),
136 L
"%u%% \x2014 %ls / %ls",
140 SendMessageW(Item
, WM_SETTEXT
, 0, (LPARAM
)This
->ProgressText
);
143 Item
= GetDlgItem(This
->hDialog
, IDC_DOWNLOAD_STATUS
);
144 if (Item
&& szStatusText
&& wcslen(szStatusText
) > 0 && This
->UrlHasBeenCopied
== FALSE
)
146 DWORD len
= wcslen(szStatusText
) + 1;
147 PWSTR buf
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
151 /* beautify our url for display purposes */
152 InternetCanonicalizeUrl(szStatusText
, buf
, &len
, ICU_DECODE
| ICU_NO_ENCODE
);
156 /* just use the original */
157 buf
= (PWSTR
)szStatusText
;
160 /* paste it into our dialog and don't do it again in this instance */
161 SendMessageW(Item
, WM_SETTEXT
, 0, (LPARAM
)buf
);
162 This
->UrlHasBeenCopied
= TRUE
;
164 if (buf
!= szStatusText
)
166 HeapFree(GetProcessHeap(), 0, buf
);
171 r
= GetWindowLongPtrW(This
->hDialog
, GWLP_USERDATA
);
172 if (0 != r
|| 0 != GetLastError())
174 *This
->pbCancelled
= TRUE
;
183 dlOnStopBinding(IBindStatusCallback
* iface
, HRESULT hresult
, LPCWSTR szError
)
190 dlGetBindInfo(IBindStatusCallback
* iface
, DWORD
* grfBINDF
, BINDINFO
* pbindinfo
)
197 dlOnDataAvailable(IBindStatusCallback
* iface
, DWORD grfBSCF
,
198 DWORD dwSize
, FORMATETC
* pformatetc
, STGMEDIUM
* pstgmed
)
205 dlOnObjectAvailable(IBindStatusCallback
* iface
, REFIID riid
, IUnknown
* punk
)
210 static const IBindStatusCallbackVtbl dlVtbl
=
225 static IBindStatusCallback
*
226 CreateDl(HWND Dlg
, BOOL
*pbCancelled
)
228 IBindStatusCallbackImpl
*This
;
230 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IBindStatusCallbackImpl
));
234 This
->vtbl
= &dlVtbl
;
237 This
->pbCancelled
= pbCancelled
;
239 return (IBindStatusCallback
*) This
;
242 #ifdef USE_CERT_PINNING
243 static BOOL
CertIsValid(HINTERNET hInternet
, LPWSTR lpszHostName
)
247 DWORD certInfoLength
;
249 INTERNET_CERTIFICATE_INFOW certInfo
;
251 hConnect
= InternetConnectW(hInternet
, lpszHostName
, INTERNET_DEFAULT_HTTPS_PORT
, NULL
, NULL
, INTERNET_SERVICE_HTTP
, INTERNET_FLAG_SECURE
, 0);
254 hRequest
= HttpOpenRequestW(hConnect
, L
"HEAD", NULL
, NULL
, NULL
, NULL
, INTERNET_FLAG_SECURE
, 0);
255 if (hRequest
!= NULL
)
257 Ret
= HttpSendRequestW(hRequest
, L
"", 0, NULL
, 0);
260 certInfoLength
= sizeof(certInfo
);
261 Ret
= InternetQueryOptionW(hRequest
,
262 INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT
,
267 if (certInfo
.lpszEncryptionAlgName
)
268 LocalFree(certInfo
.lpszEncryptionAlgName
);
269 if (certInfo
.lpszIssuerInfo
)
271 if (strcmp((LPSTR
)certInfo
.lpszIssuerInfo
, CERT_ISSUER_INFO
) != 0)
273 LocalFree(certInfo
.lpszIssuerInfo
);
275 if (certInfo
.lpszProtocolName
)
276 LocalFree(certInfo
.lpszProtocolName
);
277 if (certInfo
.lpszSignatureAlgName
)
278 LocalFree(certInfo
.lpszSignatureAlgName
);
279 if (certInfo
.lpszSubjectInfo
)
281 if (strcmp((LPSTR
)certInfo
.lpszSubjectInfo
, CERT_SUBJECT_INFO
) != 0)
283 LocalFree(certInfo
.lpszSubjectInfo
);
287 InternetCloseHandle(hRequest
);
289 InternetCloseHandle(hConnect
);
297 ThreadFunc(LPVOID Context
)
299 IBindStatusCallback
*dl
= NULL
;
300 WCHAR path
[MAX_PATH
];
302 HWND Dlg
= (HWND
) Context
;
303 DWORD dwContentLen
, dwBytesWritten
, dwBytesRead
, dwStatus
;
304 DWORD dwCurrentBytesRead
= 0;
305 DWORD dwStatusLen
= sizeof(dwStatus
);
306 BOOL bCancelled
= FALSE
;
307 BOOL bTempfile
= FALSE
;
309 HINTERNET hOpen
= NULL
;
310 HINTERNET hFile
= NULL
;
311 HANDLE hOut
= INVALID_HANDLE_VALUE
;
312 unsigned char lpBuffer
[4096];
313 const LPWSTR lpszAgent
= L
"RApps/1.0";
314 URL_COMPONENTS urlComponents
;
315 size_t urlLength
, filenameLength
;
317 /* build the path for the download */
318 p
= wcsrchr(AppInfo
->szUrlDownload
, L
'/');
319 q
= wcsrchr(AppInfo
->szUrlDownload
, L
'?');
321 /* do we have a final slash separator? */
325 /* prepare the tentative length of the filename, maybe we've to remove part of it later on */
326 filenameLength
= wcslen(p
) * sizeof(WCHAR
);
328 /* do we have query arguments in the target URL after the filename? account for them
329 (e.g. https://example.org/myfile.exe?no_adware_plz) */
330 if (q
&& q
> p
&& (q
- p
) > 0)
331 filenameLength
-= wcslen(q
- 1) * sizeof(WCHAR
);
333 /* is this URL an update package for RAPPS? if so store it in a different place */
334 if (wcscmp(AppInfo
->szUrlDownload
, APPLICATION_DATABASE_URL
) == 0)
337 if (!GetStorageDirectory(path
, _countof(path
)))
342 if (FAILED(StringCbCopyW(path
, sizeof(path
), SettingsInfo
.szDownloadDir
)))
346 /* is the path valid? can we access it? */
347 if (GetFileAttributesW(path
) == INVALID_FILE_ATTRIBUTES
)
349 if (!CreateDirectoryW(path
, NULL
))
353 /* append a \ to the provided file system path, and the filename portion from the URL after that */
354 if (FAILED(StringCbCatW(path
, sizeof(path
), L
"\\")))
356 if (FAILED(StringCbCatNW(path
, sizeof(path
), p
+ 1, filenameLength
)))
359 /* is the file already there? let's avoid having to download it */
360 if (!bCab
&& AppInfo
->szSHA1
[0] != 0 && GetFileAttributesW(path
) != INVALID_FILE_ATTRIBUTES
)
362 /* only open it in case of total correctness */
363 if (VerifyInteg(AppInfo
->szSHA1
, path
))
367 /* create an async download context for it */
369 dl
= CreateDl(Context
, &bCancelled
);
374 /* FIXME: this should just be using the system-wide proxy settings */
375 switch(SettingsInfo
.Proxy
)
377 case 0: /* preconfig */
378 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, 0);
380 case 1: /* direct (no proxy) */
381 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_DIRECT
, NULL
, NULL
, 0);
383 case 2: /* use proxy */
384 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PROXY
, SettingsInfo
.szProxyServer
, SettingsInfo
.szNoProxyFor
, 0);
386 default: /* preconfig */
387 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, 0);
394 hFile
= InternetOpenUrlW(hOpen
, AppInfo
->szUrlDownload
, NULL
, 0, INTERNET_FLAG_PRAGMA_NOCACHE
| INTERNET_FLAG_KEEP_CONNECTION
, 0);
398 if (!HttpQueryInfoW(hFile
, HTTP_QUERY_STATUS_CODE
| HTTP_QUERY_FLAG_NUMBER
, &dwStatus
, &dwStatusLen
, NULL
))
401 if(dwStatus
!= HTTP_STATUS_OK
)
403 WCHAR szMsgText
[MAX_STR_LEN
];
405 if (!LoadStringW(hInst
, IDS_UNABLE_TO_DOWNLOAD
, szMsgText
, _countof(szMsgText
)))
408 MessageBoxW(hMainWnd
, szMsgText
, NULL
, MB_OK
| MB_ICONERROR
);
412 dwStatusLen
= sizeof(dwStatus
);
414 memset(&urlComponents
, 0, sizeof(urlComponents
));
415 urlComponents
.dwStructSize
= sizeof(urlComponents
);
417 if(FAILED(StringCbLengthW(AppInfo
->szUrlDownload
, sizeof(AppInfo
->szUrlDownload
), &urlLength
)))
420 urlLength
/= sizeof(WCHAR
);
421 urlComponents
.dwSchemeLength
= urlLength
+ 1;
422 urlComponents
.lpszScheme
= malloc(urlComponents
.dwSchemeLength
* sizeof(WCHAR
));
423 urlComponents
.dwHostNameLength
= urlLength
+ 1;
424 urlComponents
.lpszHostName
= malloc(urlComponents
.dwHostNameLength
* sizeof(WCHAR
));
426 if(!InternetCrackUrlW(AppInfo
->szUrlDownload
, urlLength
+1, ICU_DECODE
| ICU_ESCAPE
, &urlComponents
))
429 if(urlComponents
.nScheme
== INTERNET_SCHEME_HTTP
|| urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
)
430 HttpQueryInfo(hFile
, HTTP_QUERY_CONTENT_LENGTH
| HTTP_QUERY_FLAG_NUMBER
, &dwContentLen
, &dwStatus
, 0);
432 if(urlComponents
.nScheme
== INTERNET_SCHEME_FTP
)
433 dwContentLen
= FtpGetFileSize(hFile
, &dwStatus
);
435 #ifdef USE_CERT_PINNING
436 /* are we using HTTPS to download the RAPPS update package? check if the certificate is original */
437 if ((urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
) &&
438 (wcscmp(AppInfo
->szUrlDownload
, APPLICATION_DATABASE_URL
) == 0) &&
439 (!CertIsValid(hOpen
, urlComponents
.lpszHostName
)))
441 WCHAR szMsgText
[MAX_STR_LEN
];
443 if (!LoadStringW(hInst
, IDS_CERT_DOES_NOT_MATCH
, szMsgText
, _countof(szMsgText
)))
446 MessageBoxW(Dlg
, szMsgText
, NULL
, MB_OK
| MB_ICONERROR
);
451 free(urlComponents
.lpszScheme
);
452 free(urlComponents
.lpszHostName
);
454 hOut
= CreateFileW(path
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, CREATE_ALWAYS
, 0, NULL
);
456 if (hOut
== INVALID_HANDLE_VALUE
)
461 if (!InternetReadFile(hFile
, lpBuffer
, _countof(lpBuffer
), &dwBytesRead
))
463 WCHAR szMsgText
[MAX_STR_LEN
];
465 if (!LoadStringW(hInst
, IDS_INTERRUPTED_DOWNLOAD
, szMsgText
, _countof(szMsgText
)))
468 MessageBoxW(hMainWnd
, szMsgText
, NULL
, MB_OK
| MB_ICONERROR
);
471 if (!WriteFile(hOut
, &lpBuffer
[0], dwBytesRead
, &dwBytesWritten
, NULL
))
473 WCHAR szMsgText
[MAX_STR_LEN
];
475 if (!LoadStringW(hInst
, IDS_UNABLE_TO_WRITE
, szMsgText
, _countof(szMsgText
)))
478 MessageBoxW(hMainWnd
, szMsgText
, NULL
, MB_OK
| MB_ICONERROR
);
481 dwCurrentBytesRead
+= dwBytesRead
;
482 IBindStatusCallback_OnProgress(dl
, dwCurrentBytesRead
, dwContentLen
, 0, AppInfo
->szUrlDownload
);
484 while (dwBytesRead
&& !bCancelled
);
487 hOut
= INVALID_HANDLE_VALUE
;
492 /* if this thing isn't a RAPPS update and it has a SHA-1 checksum
493 verify its integrity by using the native advapi32.A_SHA1 functions */
494 if (!bCab
&& AppInfo
->szSHA1
[0] != 0)
496 WCHAR szMsgText
[MAX_STR_LEN
];
498 /* change a few strings in the download dialog to reflect the verification process */
499 LoadStringW(hInst
, IDS_INTEG_CHECK_TITLE
, szMsgText
, _countof(szMsgText
));
501 SetWindowText(Dlg
, szMsgText
);
502 SendMessageW(GetDlgItem(Dlg
, IDC_DOWNLOAD_STATUS
), WM_SETTEXT
, 0, (LPARAM
)path
);
504 /* this may take a while, depending on the file size */
505 if (!VerifyInteg(AppInfo
->szSHA1
, path
))
507 if (!LoadStringW(hInst
, IDS_INTEG_CHECK_FAIL
, szMsgText
, _countof(szMsgText
)))
510 MessageBoxW(Dlg
, szMsgText
, NULL
, MB_OK
| MB_ICONERROR
);
515 ShowWindow(Dlg
, SW_HIDE
);
520 ShellExecuteW( NULL
, L
"open", path
, NULL
, NULL
, SW_SHOWNORMAL
);
523 if (hOut
!= INVALID_HANDLE_VALUE
)
526 InternetCloseHandle(hFile
);
527 InternetCloseHandle(hOpen
);
530 IBindStatusCallback_Release(dl
);
534 if (bCancelled
|| (SettingsInfo
.bDelInstaller
&& !bCab
))
545 DownloadProgressProc(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
, UINT_PTR uIdSubclass
, DWORD_PTR dwRefData
)
547 static WCHAR szProgressText
[MAX_STR_LEN
] = {0};
555 StringCbCopyW(szProgressText
,
556 sizeof(szProgressText
),
566 HDC hDC
= BeginPaint(hWnd
, &ps
), hdcMem
;
570 UINT win_width
, win_height
;
572 GetClientRect(hWnd
, &myRect
);
574 /* grab the progress bar rect size */
575 win_width
= myRect
.right
- myRect
.left
;
576 win_height
= myRect
.bottom
- myRect
.top
;
578 /* create an off-screen DC for double-buffering */
579 hdcMem
= CreateCompatibleDC(hDC
);
580 hbmMem
= CreateCompatibleBitmap(hDC
, win_width
, win_height
);
582 hOld
= SelectObject(hdcMem
, hbmMem
);
584 /* call the original draw code and redirect it to our memory buffer */
585 DefSubclassProc(hWnd
, uMsg
, (WPARAM
)hdcMem
, lParam
);
587 /* draw our nifty progress text over it */
588 SelectFont(hdcMem
, GetStockFont(DEFAULT_GUI_FONT
));
589 DrawShadowText(hdcMem
, szProgressText
, wcslen(szProgressText
),
591 DT_CENTER
| DT_VCENTER
| DT_NOPREFIX
| DT_SINGLELINE
,
592 GetSysColor(COLOR_CAPTIONTEXT
),
593 GetSysColor(COLOR_3DSHADOW
),
596 /* transfer the off-screen DC to the screen */
597 BitBlt(hDC
, 0, 0, win_width
, win_height
, hdcMem
, 0, 0, SRCCOPY
);
599 /* free the off-screen DC */
600 SelectObject(hdcMem
, hOld
);
601 DeleteObject(hbmMem
);
608 /* Raymond Chen says that we should safely unsubclass all the things!
609 (http://blogs.msdn.com/b/oldnewthing/archive/2003/11/11/55653.aspx) */
612 ZeroMemory(szProgressText
, sizeof(szProgressText
));
613 RemoveWindowSubclass(hWnd
, DownloadProgressProc
, uIdSubclass
);
617 return DefSubclassProc(hWnd
, uMsg
, wParam
, lParam
);
623 DownloadDlgProc(HWND Dlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
633 HICON hIconSm
= NULL
, hIconBg
= NULL
;
635 hIconBg
= (HICON
)GetClassLongPtr(hMainWnd
, GCLP_HICON
);
636 hIconSm
= (HICON
)GetClassLongPtr(hMainWnd
, GCLP_HICONSM
);
638 if (hIconBg
&& hIconSm
)
640 SendMessageW(Dlg
, WM_SETICON
, ICON_BIG
, (LPARAM
) hIconBg
);
641 SendMessageW(Dlg
, WM_SETICON
, ICON_SMALL
, (LPARAM
) hIconSm
);
644 SetWindowLongPtrW(Dlg
, GWLP_USERDATA
, 0);
645 Item
= GetDlgItem(Dlg
, IDC_DOWNLOAD_PROGRESS
);
648 /* initialize the default values for our nifty progress bar
649 and subclass it so that it learns to print a status text */
650 SendMessageW(Item
, PBM_SETRANGE
, 0, MAKELPARAM(0, 100));
651 SendMessageW(Item
, PBM_SETPOS
, 0, 0);
653 SetWindowSubclass(Item
, DownloadProgressProc
, 0, 0);
656 /* add a neat placeholder until the download URL is retrieved */
657 Item
= GetDlgItem(Dlg
, IDC_DOWNLOAD_STATUS
);
658 SendMessageW(Item
, WM_SETTEXT
, 0, (LPARAM
)L
"\x2022 \x2022 \x2022");
660 Thread
= CreateThread(NULL
, 0, ThreadFunc
, Dlg
, 0, &ThreadId
);
668 if (wParam
== IDCANCEL
)
670 SetWindowLongPtrW(Dlg
, GWLP_USERDATA
, 1);
671 PostMessageW(Dlg
, WM_CLOSE
, 0, 0);
685 DownloadApplication(INT Index
)
687 if (!IS_AVAILABLE_ENUM(SelectedEnumType
))
690 AppInfo
= (PAPPLICATION_INFO
) ListViewGetlParam(Index
);
691 if (!AppInfo
) return FALSE
;
693 WriteLogMessage(EVENTLOG_SUCCESS
, MSG_SUCCESS_INSTALL
, AppInfo
->szName
);
696 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
),
704 DownloadApplicationsDB(LPWSTR lpUrl
)
706 APPLICATION_INFO IntInfo
;
708 ZeroMemory(&IntInfo
, sizeof(IntInfo
));
709 if (FAILED(StringCbCopyW(IntInfo
.szUrlDownload
,
710 sizeof(IntInfo
.szUrlDownload
),
719 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
),