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 accesible 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
) * sizeof(WCHAR
);
147 PWSTR buf
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
151 /* beautify our url for display purposes */
152 InternetCanonicalizeUrl(szStatusText
, buf
, &len
, ICU_DECODE
| ICU_NO_ENCODE
);
154 /* paste it into our dialog, free the temp buffer
155 and don't do it again in this instance */
156 SendMessageW(Item
, WM_SETTEXT
, 0, (LPARAM
)buf
);
157 HeapFree(GetProcessHeap(), 0, buf
);
161 /* our computer is old and rusty and does not have enough ram for this,
162 use the ugly version and call it a day */
163 SendMessageW(Item
, WM_SETTEXT
, 0, (LPARAM
)szStatusText
);
166 This
->UrlHasBeenCopied
= TRUE
;
170 r
= GetWindowLongPtrW(This
->hDialog
, GWLP_USERDATA
);
171 if (0 != r
|| 0 != GetLastError())
173 *This
->pbCancelled
= TRUE
;
182 dlOnStopBinding(IBindStatusCallback
* iface
, HRESULT hresult
, LPCWSTR szError
)
189 dlGetBindInfo(IBindStatusCallback
* iface
, DWORD
* grfBINDF
, BINDINFO
* pbindinfo
)
196 dlOnDataAvailable(IBindStatusCallback
* iface
, DWORD grfBSCF
,
197 DWORD dwSize
, FORMATETC
* pformatetc
, STGMEDIUM
* pstgmed
)
204 dlOnObjectAvailable(IBindStatusCallback
* iface
, REFIID riid
, IUnknown
* punk
)
209 static const IBindStatusCallbackVtbl dlVtbl
=
224 static IBindStatusCallback
*
225 CreateDl(HWND Dlg
, BOOL
*pbCancelled
)
227 IBindStatusCallbackImpl
*This
;
229 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IBindStatusCallbackImpl
));
233 This
->vtbl
= &dlVtbl
;
236 This
->pbCancelled
= pbCancelled
;
238 return (IBindStatusCallback
*) This
;
241 #ifdef USE_CERT_PINNING
242 static BOOL
CertIsValid(HINTERNET hInternet
, LPWSTR lpszHostName
)
246 DWORD certInfoLength
;
248 INTERNET_CERTIFICATE_INFOW certInfo
;
250 hConnect
= InternetConnectW(hInternet
, lpszHostName
, INTERNET_DEFAULT_HTTPS_PORT
, NULL
, NULL
, INTERNET_SERVICE_HTTP
, INTERNET_FLAG_SECURE
, 0);
253 hRequest
= HttpOpenRequestW(hConnect
, L
"HEAD", NULL
, NULL
, NULL
, NULL
, INTERNET_FLAG_SECURE
, 0);
254 if (hRequest
!= NULL
)
256 Ret
= HttpSendRequestW(hRequest
, L
"", 0, NULL
, 0);
259 certInfoLength
= sizeof(INTERNET_CERTIFICATE_INFOW
);
260 Ret
= InternetQueryOptionW(hRequest
,
261 INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT
,
266 if (certInfo
.lpszEncryptionAlgName
)
267 LocalFree(certInfo
.lpszEncryptionAlgName
);
268 if (certInfo
.lpszIssuerInfo
)
270 if (strcmp((LPSTR
)certInfo
.lpszIssuerInfo
, CERT_ISSUER_INFO
) != 0)
272 LocalFree(certInfo
.lpszIssuerInfo
);
274 if (certInfo
.lpszProtocolName
)
275 LocalFree(certInfo
.lpszProtocolName
);
276 if (certInfo
.lpszSignatureAlgName
)
277 LocalFree(certInfo
.lpszSignatureAlgName
);
278 if (certInfo
.lpszSubjectInfo
)
280 if (strcmp((LPSTR
)certInfo
.lpszSubjectInfo
, CERT_SUBJECT_INFO
) != 0)
282 LocalFree(certInfo
.lpszSubjectInfo
);
286 InternetCloseHandle(hRequest
);
295 ThreadFunc(LPVOID Context
)
297 IBindStatusCallback
*dl
= NULL
;
298 WCHAR path
[MAX_PATH
];
300 HWND Dlg
= (HWND
) Context
;
301 DWORD dwContentLen
, dwBytesWritten
, dwBytesRead
, dwStatus
;
302 DWORD dwCurrentBytesRead
= 0;
303 DWORD dwStatusLen
= sizeof(dwStatus
);
304 BOOL bCancelled
= FALSE
;
305 BOOL bTempfile
= FALSE
;
307 HINTERNET hOpen
= NULL
;
308 HINTERNET hFile
= NULL
;
309 HANDLE hOut
= INVALID_HANDLE_VALUE
;
310 unsigned char lpBuffer
[4096];
311 const LPWSTR lpszAgent
= L
"RApps/1.0";
312 URL_COMPONENTS urlComponents
;
313 size_t urlLength
, filenameLength
;
315 /* build the path for the download */
316 p
= wcsrchr(AppInfo
->szUrlDownload
, L
'/');
317 q
= wcsrchr(AppInfo
->szUrlDownload
, L
'?');
319 /* do we have a final slash separator? */
323 /* prepare the tentative length of the filename, maybe we've to remove part of it later on */
324 filenameLength
= wcslen(p
) * sizeof(WCHAR
);
326 /* do we have query arguments in the target URL after the filename? account for them
327 (e.g. https://example.org/myfile.exe?no_adware_plz) */
328 if (q
&& q
> p
&& (q
- p
) > 0)
329 filenameLength
-= wcslen(q
- 1) * sizeof(WCHAR
);
331 /* is this URL an update package for RAPPS? if so store it in a different place */
332 if (wcscmp(AppInfo
->szUrlDownload
, APPLICATION_DATABASE_URL
) == 0)
335 if (!GetStorageDirectory(path
, _countof(path
)))
340 if (FAILED(StringCbCopyW(path
, sizeof(path
), SettingsInfo
.szDownloadDir
)))
344 /* is the path valid? can we access it? */
345 if (GetFileAttributesW(path
) == INVALID_FILE_ATTRIBUTES
)
347 if (!CreateDirectoryW(path
, NULL
))
351 /* append a \ to the provided file system path, and the filename portion from the URL after that */
352 if (FAILED(StringCbCatW(path
, sizeof(path
), L
"\\")))
354 if (FAILED(StringCbCatNW(path
, sizeof(path
), p
+ 1, filenameLength
)))
357 /* create an async download context for it */
359 dl
= CreateDl(Context
, &bCancelled
);
364 /* FIXME: this should just be using the system-wide proxy settings */
365 switch(SettingsInfo
.Proxy
)
367 case 0: /* preconfig */
368 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, 0);
370 case 1: /* direct (no proxy) */
371 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_DIRECT
, NULL
, NULL
, 0);
373 case 2: /* use proxy */
374 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PROXY
, SettingsInfo
.szProxyServer
, SettingsInfo
.szNoProxyFor
, 0);
376 default: /* preconfig */
377 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, 0);
384 hFile
= InternetOpenUrlW(hOpen
, AppInfo
->szUrlDownload
, NULL
, 0, INTERNET_FLAG_PRAGMA_NOCACHE
| INTERNET_FLAG_KEEP_CONNECTION
, 0);
388 if (!HttpQueryInfoW(hFile
, HTTP_QUERY_STATUS_CODE
| HTTP_QUERY_FLAG_NUMBER
, &dwStatus
, &dwStatusLen
, NULL
))
391 if(dwStatus
!= HTTP_STATUS_OK
)
393 WCHAR szMsgText
[MAX_STR_LEN
];
395 if (!LoadStringW(hInst
, IDS_UNABLE_TO_DOWNLOAD
, szMsgText
, sizeof(szMsgText
) / sizeof(WCHAR
)))
398 MessageBoxW(hMainWnd
, szMsgText
, NULL
, MB_OK
| MB_ICONERROR
);
402 dwStatusLen
= sizeof(dwStatus
);
404 memset(&urlComponents
, 0, sizeof(urlComponents
));
405 urlComponents
.dwStructSize
= sizeof(urlComponents
);
407 if(FAILED(StringCbLengthW(AppInfo
->szUrlDownload
, sizeof(AppInfo
->szUrlDownload
), &urlLength
)))
410 urlComponents
.dwSchemeLength
= urlLength
*sizeof(WCHAR
);
411 urlComponents
.lpszScheme
= malloc(urlComponents
.dwSchemeLength
);
412 urlComponents
.dwHostNameLength
= urlLength
*sizeof(WCHAR
);
413 urlComponents
.lpszHostName
= malloc(urlComponents
.dwHostNameLength
);
415 if(!InternetCrackUrlW(AppInfo
->szUrlDownload
, urlLength
+1, ICU_DECODE
| ICU_ESCAPE
, &urlComponents
))
418 if(urlComponents
.nScheme
== INTERNET_SCHEME_HTTP
|| urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
)
419 HttpQueryInfo(hFile
, HTTP_QUERY_CONTENT_LENGTH
| HTTP_QUERY_FLAG_NUMBER
, &dwContentLen
, &dwStatus
, 0);
421 if(urlComponents
.nScheme
== INTERNET_SCHEME_FTP
)
422 dwContentLen
= FtpGetFileSize(hFile
, &dwStatus
);
424 #ifdef USE_CERT_PINNING
425 /* are we using HTTPS to download the RAPPS update package? check if the certificate is original */
426 if ((urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
) &&
427 (wcscmp(AppInfo
->szUrlDownload
, APPLICATION_DATABASE_URL
) == 0) &&
428 (!CertIsValid(hOpen
, urlComponents
.lpszHostName
)))
430 WCHAR szMsgText
[MAX_STR_LEN
];
432 if (!LoadStringW(hInst
, IDS_CERT_DOES_NOT_MATCH
, szMsgText
, sizeof(szMsgText
) / sizeof(WCHAR
)))
435 MessageBoxW(Dlg
, szMsgText
, NULL
, MB_OK
| MB_ICONERROR
);
440 free(urlComponents
.lpszScheme
);
441 free(urlComponents
.lpszHostName
);
443 hOut
= CreateFileW(path
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, CREATE_ALWAYS
, 0, NULL
);
445 if (hOut
== INVALID_HANDLE_VALUE
)
450 if (!InternetReadFile(hFile
, lpBuffer
, _countof(lpBuffer
), &dwBytesRead
)) goto end
;
451 if (!WriteFile(hOut
, &lpBuffer
[0], dwBytesRead
, &dwBytesWritten
, NULL
)) goto end
;
452 dwCurrentBytesRead
+= dwBytesRead
;
453 IBindStatusCallback_OnProgress(dl
, dwCurrentBytesRead
, dwContentLen
, 0, AppInfo
->szUrlDownload
);
455 while (dwBytesRead
&& !bCancelled
);
458 hOut
= INVALID_HANDLE_VALUE
;
463 ShowWindow(Dlg
, SW_HIDE
);
467 ShellExecuteW( NULL
, L
"open", path
, NULL
, NULL
, SW_SHOWNORMAL
);
470 if (hOut
!= INVALID_HANDLE_VALUE
)
473 InternetCloseHandle(hFile
);
474 InternetCloseHandle(hOpen
);
477 IBindStatusCallback_Release(dl
);
481 if (bCancelled
|| (SettingsInfo
.bDelInstaller
&& !bCab
))
492 DownloadProgressProc(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
, UINT_PTR uIdSubclass
, DWORD_PTR dwRefData
)
494 static WCHAR szProgressText
[MAX_STR_LEN
] = {0};
502 StringCbCopyW(szProgressText
,
503 sizeof(szProgressText
),
512 HDC hDC
= BeginPaint(hWnd
, &ps
), hdcMem
;
516 UINT win_width
, win_height
;
518 GetClientRect(hWnd
, &myRect
);
520 /* grab the progress bar rect size */
521 win_width
= myRect
.right
- myRect
.left
;
522 win_height
= myRect
.bottom
- myRect
.top
;
524 /* create an off-screen DC for double-buffering */
525 hdcMem
= CreateCompatibleDC(hDC
);
526 hbmMem
= CreateCompatibleBitmap(hDC
, win_width
, win_height
);
528 hOld
= SelectObject(hdcMem
, hbmMem
);
530 /* call the original draw code and redirect it to our memory buffer */
531 DefSubclassProc(hWnd
, uMsg
, (WPARAM
)hdcMem
, lParam
);
533 /* draw our nifty progress text over it */
534 SelectFont(hdcMem
, GetStockFont(DEFAULT_GUI_FONT
));
535 DrawShadowText(hdcMem
, szProgressText
, wcslen(szProgressText
),
537 DT_CENTER
| DT_VCENTER
| DT_NOPREFIX
| DT_SINGLELINE
,
538 GetSysColor(COLOR_CAPTIONTEXT
),
539 GetSysColor(COLOR_3DSHADOW
),
542 /* transfer the off-screen DC to the screen */
543 BitBlt(hDC
, 0, 0, win_width
, win_height
, hdcMem
, 0, 0, SRCCOPY
);
545 /* free the off-screen DC */
546 SelectObject(hdcMem
, hOld
);
547 DeleteObject(hbmMem
);
554 /* Raymond Chen says that we should safely unsubclass all the things!
555 (http://blogs.msdn.com/b/oldnewthing/archive/2003/11/11/55653.aspx) */
558 ZeroMemory(szProgressText
, sizeof(szProgressText
));
559 RemoveWindowSubclass(hWnd
, DownloadProgressProc
, uIdSubclass
);
563 return DefSubclassProc(hWnd
, uMsg
, wParam
, lParam
);
569 DownloadDlgProc(HWND Dlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
579 HICON hIconSm
= NULL
, hIconBg
= NULL
;
581 hIconBg
= (HICON
)GetClassLongPtr(hMainWnd
, GCLP_HICON
);
582 hIconSm
= (HICON
)GetClassLongPtr(hMainWnd
, GCLP_HICONSM
);
584 if (hIconBg
&& hIconSm
)
586 SendMessageW(Dlg
, WM_SETICON
, ICON_BIG
, (LPARAM
) hIconBg
);
587 SendMessageW(Dlg
, WM_SETICON
, ICON_SMALL
, (LPARAM
) hIconSm
);
590 SetWindowLongPtrW(Dlg
, GWLP_USERDATA
, 0);
591 Item
= GetDlgItem(Dlg
, IDC_DOWNLOAD_PROGRESS
);
594 /* initialize the default values for our nifty progress bar
595 and subclass it so that it learns to print a status text */
596 SendMessageW(Item
, PBM_SETRANGE
, 0, MAKELPARAM(0, 100));
597 SendMessageW(Item
, PBM_SETPOS
, 0, 0);
599 SetWindowSubclass(Item
, DownloadProgressProc
, 0, 0);
602 /* add a neat placeholder until the download URL is retrieved */
603 Item
= GetDlgItem(Dlg
, IDC_DOWNLOAD_STATUS
);
604 SendMessageW(Item
, WM_SETTEXT
, 0, (LPARAM
)L
"\x2022 \x2022 \x2022");
606 Thread
= CreateThread(NULL
, 0, ThreadFunc
, Dlg
, 0, &ThreadId
);
614 if (wParam
== IDCANCEL
)
616 SetWindowLongPtrW(Dlg
, GWLP_USERDATA
, 1);
617 PostMessageW(Dlg
, WM_CLOSE
, 0, 0);
631 DownloadApplication(INT Index
)
633 if (!IS_AVAILABLE_ENUM(SelectedEnumType
))
636 AppInfo
= (PAPPLICATION_INFO
) ListViewGetlParam(Index
);
637 if (!AppInfo
) return FALSE
;
639 WriteLogMessage(EVENTLOG_SUCCESS
, MSG_SUCCESS_INSTALL
, AppInfo
->szName
);
642 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
),
650 DownloadApplicationsDB(LPWSTR lpUrl
)
652 APPLICATION_INFO IntInfo
;
654 ZeroMemory(&IntInfo
, sizeof(APPLICATION_INFO
));
655 if (FAILED(StringCbCopyW(IntInfo
.szUrlDownload
,
656 sizeof(IntInfo
.szUrlDownload
),
665 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
),