dcb883efb48e6523bcf7c3bbe8f34a7f763756e4
[reactos.git] / reactos / base / applications / rapps / loaddlg.cpp
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)
11 */
12
13 /*
14 * Based on Wine dlls/shdocvw/shdocvw_main.c
15 *
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.
20 *
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.
25 *
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
29 */
30 #include "defines.h"
31
32 #include <shlobj_undoc.h>
33 #include <shlguid_undoc.h>
34
35 #include <atlbase.h>
36 #include <atlcom.h>
37 #include <atlwin.h>
38 #include <wininet.h>
39 #include <shellutils.h>
40
41 #include <rosctrls.h>
42 #include <windowsx.h>
43
44 #include "rosui.h"
45 #include "dialogs.h"
46 #include "misc.h"
47
48 #ifdef USE_CERT_PINNING
49 #define CERT_ISSUER_INFO "BE\r\nGlobalSign nv-sa\r\nGlobalSign Domain Validation CA - SHA256 - G2"
50 #define CERT_SUBJECT_INFO "Domain Control Validated\r\n*.reactos.org"
51 #endif
52
53 typedef enum
54 {
55 DLWaiting = IDS_STATUS_WAITING,
56 DLDownloading = IDS_STATUS_DOWNLOADING,
57 DLWaitingToInstall = IDS_STATUS_DOWNLOADED,
58 DLInstalling = IDS_STATUS_INSTALLING,
59 DLInstalled = IDS_STATUS_INSTALLED,
60 DLFinished = IDS_STATUS_FINISHED
61 } DOWNLOAD_STATUS;
62
63 ATL::CStringW LoadStatusString(DOWNLOAD_STATUS StatusParam)
64 {
65 ATL::CStringW szString;
66 szString.LoadStringW(StatusParam);
67 return szString;
68 }
69
70 class CDownloadDialog :
71 public CComObjectRootEx<CComMultiThreadModelNoCS>,
72 public IBindStatusCallback
73 {
74 HWND m_hDialog;
75 PBOOL m_pbCancelled;
76 BOOL m_UrlHasBeenCopied;
77
78 public:
79 ~CDownloadDialog()
80 {
81 //DestroyWindow(m_hDialog);
82 }
83
84 HRESULT Initialize(HWND Dlg, BOOL *pbCancelled)
85 {
86 m_hDialog = Dlg;
87 m_pbCancelled = pbCancelled;
88 m_UrlHasBeenCopied = FALSE;
89 return S_OK;
90 }
91
92 virtual HRESULT STDMETHODCALLTYPE OnStartBinding(
93 DWORD dwReserved,
94 IBinding *pib)
95 {
96 return S_OK;
97 }
98
99 virtual HRESULT STDMETHODCALLTYPE GetPriority(
100 LONG *pnPriority)
101 {
102 return S_OK;
103 }
104
105 virtual HRESULT STDMETHODCALLTYPE OnLowResource(
106 DWORD reserved)
107 {
108 return S_OK;
109 }
110
111 virtual HRESULT STDMETHODCALLTYPE OnProgress(
112 ULONG ulProgress,
113 ULONG ulProgressMax,
114 ULONG ulStatusCode,
115 LPCWSTR szStatusText)
116 {
117 HWND Item;
118 LONG r;
119
120 Item = GetDlgItem(m_hDialog, IDC_DOWNLOAD_PROGRESS);
121 if (Item && ulProgressMax)
122 {
123 WCHAR szProgress[100];
124 WCHAR szProgressMax[100];
125 UINT uiPercentage = ((ULONGLONG) ulProgress * 100) / ulProgressMax;
126
127 /* send the current progress to the progress bar */
128 SendMessageW(Item, PBM_SETPOS, uiPercentage, 0);
129
130 /* format the bits and bytes into pretty and accessible units... */
131 StrFormatByteSizeW(ulProgress, szProgress, _countof(szProgress));
132 StrFormatByteSizeW(ulProgressMax, szProgressMax, _countof(szProgressMax));
133
134 /* ...and post all of it to our subclassed progress bar text subroutine */
135 ATL::CStringW m_ProgressText;
136 m_ProgressText.Format(L"%u%% \x2014 %ls / %ls",
137 uiPercentage,
138 szProgress,
139 szProgressMax);
140 SendMessageW(Item, WM_SETTEXT, 0, (LPARAM) m_ProgressText.GetString());
141 }
142
143 Item = GetDlgItem(m_hDialog, IDC_DOWNLOAD_STATUS);
144 if (Item && szStatusText && wcslen(szStatusText) > 0 && m_UrlHasBeenCopied == FALSE)
145 {
146 DWORD len = wcslen(szStatusText) + 1;
147 ATL::CStringW buf;
148
149 /* beautify our url for display purposes */
150 if (!InternetCanonicalizeUrlW(szStatusText, buf.GetBuffer(len), &len, ICU_DECODE | ICU_NO_ENCODE))
151 {
152 /* just use the original */
153 buf.ReleaseBuffer();
154 buf = szStatusText;
155 }
156 else
157 {
158 buf.ReleaseBuffer();
159 }
160
161 /* paste it into our dialog and don't do it again in this instance */
162 SendMessageW(Item, WM_SETTEXT, 0, (LPARAM) buf.GetString());
163 m_UrlHasBeenCopied = TRUE;
164 }
165
166 SetLastError(0);
167 r = GetWindowLongPtrW(m_hDialog, GWLP_USERDATA);
168 if (0 != r || 0 != GetLastError())
169 {
170 *m_pbCancelled = TRUE;
171 return E_ABORT;
172 }
173
174 return S_OK;
175 }
176
177 virtual HRESULT STDMETHODCALLTYPE OnStopBinding(
178 HRESULT hresult,
179 LPCWSTR szError)
180 {
181 return S_OK;
182 }
183
184 virtual HRESULT STDMETHODCALLTYPE GetBindInfo(
185 DWORD *grfBINDF,
186 BINDINFO *pbindinfo)
187 {
188 return S_OK;
189 }
190
191 virtual HRESULT STDMETHODCALLTYPE OnDataAvailable(
192 DWORD grfBSCF,
193 DWORD dwSize,
194 FORMATETC *pformatetc,
195 STGMEDIUM *pstgmed)
196 {
197 return S_OK;
198 }
199
200 virtual HRESULT STDMETHODCALLTYPE OnObjectAvailable(
201 REFIID riid,
202 IUnknown *punk)
203 {
204 return S_OK;
205 }
206
207 BEGIN_COM_MAP(CDownloadDialog)
208 COM_INTERFACE_ENTRY_IID(IID_IBindStatusCallback, IBindStatusCallback)
209 END_COM_MAP()
210 };
211
212 class CDowloadingAppsListView
213 : public CUiWindow<CListView>
214 {
215 public:
216 HWND Create(HWND hwndParent)
217 {
218 RECT r = {10, 150, 320, 350};
219 const DWORD style = WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_SINGLESEL
220 | LVS_SHOWSELALWAYS | LVS_NOSORTHEADER | LVS_NOCOLUMNHEADER;
221
222 HWND hwnd = CListView::Create(hwndParent, r, NULL, style, WS_EX_CLIENTEDGE);
223
224 AddColumn(0, 150, LVCFMT_LEFT);
225 AddColumn(1, 120, LVCFMT_LEFT);
226
227 return hwnd;
228 }
229
230 VOID LoadList(ATL::CSimpleArray<PAPPLICATION_INFO> arrInfo)
231 {
232 for (INT i = 0; i < arrInfo.GetSize(); ++i)
233 {
234 PAPPLICATION_INFO AppInfo = arrInfo[i];
235 AddRow(i, AppInfo->szName.GetString(), DOWNLOAD_STATUS::DLWaiting);
236 }
237 }
238
239 VOID SetDownloadStatus(INT ItemIndex, DOWNLOAD_STATUS Status)
240 {
241 HWND hListView = GetWindow();
242 ATL::CStringW szBuffer = LoadStatusString(Status);
243 ListView_SetItemText(hListView, ItemIndex, 1, const_cast<LPWSTR>(szBuffer.GetString()));
244 }
245
246 BOOL AddItem(INT ItemIndex, LPWSTR lpText)
247 {
248 LVITEMW Item;
249
250 ZeroMemory(&Item, sizeof(Item));
251
252 Item.mask = LVIF_TEXT | LVIF_STATE;
253 Item.pszText = lpText;
254 Item.iItem = ItemIndex;
255
256 return InsertItem(&Item);
257 }
258
259 VOID AddRow(INT RowIndex, LPCWSTR szAppName, const DOWNLOAD_STATUS Status)
260 {
261 ATL::CStringW szStatus = LoadStatusString(Status);
262 AddItem(RowIndex,
263 const_cast<LPWSTR>(szAppName));
264 SetDownloadStatus(RowIndex, Status);
265 }
266
267 BOOL AddColumn(INT Index, INT Width, INT Format)
268 {
269 LVCOLUMNW Column;
270 ZeroMemory(&Column, sizeof(Column));
271
272 Column.mask = LVCF_FMT | LVCF_WIDTH | LVCF_SUBITEM;
273 Column.iSubItem = Index;
274 Column.cx = Width;
275 Column.fmt = Format;
276
277 return (InsertColumn(Index, &Column) == -1) ? FALSE : TRUE;
278 }
279 };
280
281 extern "C"
282 HRESULT WINAPI CDownloadDialog_Constructor(HWND Dlg, BOOL *pbCancelled, REFIID riid, LPVOID *ppv)
283 {
284 return ShellObjectCreatorInit<CDownloadDialog>(Dlg, pbCancelled, riid, ppv);
285 }
286
287 #ifdef USE_CERT_PINNING
288 static BOOL CertIsValid(HINTERNET hInternet, LPWSTR lpszHostName)
289 {
290 HINTERNET hConnect;
291 HINTERNET hRequest;
292 DWORD certInfoLength;
293 BOOL Ret = FALSE;
294 INTERNET_CERTIFICATE_INFOW certInfo;
295
296 hConnect = InternetConnectW(hInternet, lpszHostName, INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0);
297 if (hConnect)
298 {
299 hRequest = HttpOpenRequestW(hConnect, L"HEAD", NULL, NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0);
300 if (hRequest != NULL)
301 {
302 Ret = HttpSendRequestW(hRequest, L"", 0, NULL, 0);
303 if (Ret)
304 {
305 certInfoLength = sizeof(certInfo);
306 Ret = InternetQueryOptionW(hRequest,
307 INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
308 &certInfo,
309 &certInfoLength);
310 if (Ret)
311 {
312 if (certInfo.lpszEncryptionAlgName)
313 LocalFree(certInfo.lpszEncryptionAlgName);
314 if (certInfo.lpszIssuerInfo)
315 {
316 if (strcmp((LPSTR) certInfo.lpszIssuerInfo, CERT_ISSUER_INFO) != 0)
317 Ret = FALSE;
318 LocalFree(certInfo.lpszIssuerInfo);
319 }
320 if (certInfo.lpszProtocolName)
321 LocalFree(certInfo.lpszProtocolName);
322 if (certInfo.lpszSignatureAlgName)
323 LocalFree(certInfo.lpszSignatureAlgName);
324 if (certInfo.lpszSubjectInfo)
325 {
326 if (strcmp((LPSTR) certInfo.lpszSubjectInfo, CERT_SUBJECT_INFO) != 0)
327 Ret = FALSE;
328 LocalFree(certInfo.lpszSubjectInfo);
329 }
330 }
331 }
332 InternetCloseHandle(hRequest);
333 }
334 InternetCloseHandle(hConnect);
335 }
336 return Ret;
337 }
338 #endif
339
340 inline VOID MessageBox_LoadString(HWND hMainWnd, INT StringID)
341 {
342 ATL::CString szMsgText;
343 if (szMsgText.LoadStringW(hInst, StringID))
344 {
345 MessageBoxW(hMainWnd, szMsgText.GetString(), NULL, MB_OK | MB_ICONERROR);
346 }
347 }
348
349 // CDownloadManager
350 PAPPLICATION_INFO CDownloadManager::AppInfo;
351 ATL::CSimpleArray<PAPPLICATION_INFO> CDownloadManager::AppsToInstallList;
352 CDowloadingAppsListView CDownloadManager::DownloadsListView;
353 INT CDownloadManager::iCurrentApp;
354
355 #define DL_START_NEW WM_APP + 1
356
357 INT_PTR CALLBACK CDownloadManager::DownloadDlgProc(HWND Dlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
358 {
359 HANDLE Thread;
360 DWORD ThreadId;
361 HWND Item;
362 static WCHAR szCaption[MAX_PATH];
363
364 switch (uMsg)
365 {
366 case WM_INITDIALOG:
367 {
368 HICON hIconSm, hIconBg;
369
370 hIconBg = (HICON) GetClassLongW(hMainWnd, GCLP_HICON);
371 hIconSm = (HICON) GetClassLongW(hMainWnd, GCLP_HICONSM);
372
373 if (hIconBg && hIconSm)
374 {
375 SendMessageW(Dlg, WM_SETICON, ICON_BIG, (LPARAM) hIconBg);
376 SendMessageW(Dlg, WM_SETICON, ICON_SMALL, (LPARAM) hIconSm);
377 }
378
379 SetWindowLongW(Dlg, GWLP_USERDATA, 0);
380 Item = GetDlgItem(Dlg, IDC_DOWNLOAD_PROGRESS);
381 if (Item)
382 {
383 // initialize the default values for our nifty progress bar
384 // and subclass it so that it learns to print a status text
385 SendMessageW(Item, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
386 SendMessageW(Item, PBM_SETPOS, 0, 0);
387
388 SetWindowSubclass(Item, DownloadProgressProc, 0, 0);
389 }
390 // Get a dlg string for later use
391 GetWindowTextW(Dlg, szCaption, MAX_PATH);
392
393 // Add a ListView
394 HWND hListView = DownloadsListView.Create(Dlg);
395 if (!hListView)
396 {
397 return FALSE;
398 }
399 DownloadsListView.LoadList(AppsToInstallList);
400 iCurrentApp = -1;
401
402 ShowWindow(Dlg, SW_SHOW);
403
404 // Start new download
405 SendMessageW(Dlg, DL_START_NEW, 0, 0);
406
407 return TRUE;
408 }
409 case WM_COMMAND:
410 if (wParam == IDCANCEL)
411 {
412 SetWindowLongW(Dlg, GWLP_USERDATA, 1);
413 PostMessageW(Dlg, WM_CLOSE, 0, 0);
414 }
415 return FALSE;
416
417 case DL_START_NEW:
418
419 ++iCurrentApp;
420 // If some downloads left we issue it
421 if (iCurrentApp < AppsToInstallList.GetSize())
422 {
423 AppInfo = AppsToInstallList[iCurrentApp];
424 if (!AppInfo)
425 {
426 return FALSE;
427 }
428
429 ATL::CStringW szNewCaption;
430
431 // Reset progress bar
432 Item = GetDlgItem(Dlg, IDC_DOWNLOAD_PROGRESS);
433 if (Item)
434 {
435 SendMessageW(Item, PBM_SETPOS, 0, 0);
436 }
437
438 // Change caption to show the currently downloaded app
439 szNewCaption.Format(szCaption, AppInfo->szName.GetString());
440 SetWindowTextW(Dlg, szNewCaption.GetString());
441
442 // Add a neat placeholder until the download URL is retrieved
443 SetDlgItemTextW(Dlg, IDC_DOWNLOAD_STATUS, L"\x2022 \x2022 \x2022");
444 Thread = CreateThread(NULL, 0, ThreadFunc, Dlg, 0, &ThreadId);
445
446 if (!Thread)
447 {
448 return FALSE;
449 }
450
451 CloseHandle(Thread);
452 return TRUE;
453 }
454 // No downloads left, closing (fallthrough)
455
456 case WM_CLOSE:
457 EndDialog(Dlg, 0);
458 DestroyWindow(Dlg);
459 return TRUE;
460
461 default:
462 return FALSE;
463 }
464 }
465
466 LRESULT CALLBACK CDownloadManager::DownloadProgressProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
467 {
468 static ATL::CStringW szProgressText;
469
470 switch (uMsg)
471 {
472 case WM_SETTEXT:
473 {
474 if (lParam)
475 {
476 szProgressText = (PCWSTR) lParam;
477 }
478 return TRUE;
479 }
480
481 case WM_ERASEBKGND:
482 case WM_PAINT:
483 {
484 PAINTSTRUCT ps;
485 HDC hDC = BeginPaint(hWnd, &ps), hdcMem;
486 HBITMAP hbmMem;
487 HANDLE hOld;
488 RECT myRect;
489 UINT win_width, win_height;
490
491 GetClientRect(hWnd, &myRect);
492
493 /* grab the progress bar rect size */
494 win_width = myRect.right - myRect.left;
495 win_height = myRect.bottom - myRect.top;
496
497 /* create an off-screen DC for double-buffering */
498 hdcMem = CreateCompatibleDC(hDC);
499 hbmMem = CreateCompatibleBitmap(hDC, win_width, win_height);
500
501 hOld = SelectObject(hdcMem, hbmMem);
502
503 /* call the original draw code and redirect it to our memory buffer */
504 DefSubclassProc(hWnd, uMsg, (WPARAM) hdcMem, lParam);
505
506 /* draw our nifty progress text over it */
507 SelectFont(hdcMem, GetStockFont(DEFAULT_GUI_FONT));
508 DrawShadowText(hdcMem, szProgressText.GetString(), szProgressText.GetLength(),
509 &myRect,
510 DT_CENTER | DT_VCENTER | DT_NOPREFIX | DT_SINGLELINE,
511 GetSysColor(COLOR_CAPTIONTEXT),
512 GetSysColor(COLOR_3DSHADOW),
513 1, 1);
514
515 /* transfer the off-screen DC to the screen */
516 BitBlt(hDC, 0, 0, win_width, win_height, hdcMem, 0, 0, SRCCOPY);
517
518 /* free the off-screen DC */
519 SelectObject(hdcMem, hOld);
520 DeleteObject(hbmMem);
521 DeleteDC(hdcMem);
522
523 EndPaint(hWnd, &ps);
524 return 0;
525 }
526
527 /* Raymond Chen says that we should safely unsubclass all the things!
528 (http://blogs.msdn.com/b/oldnewthing/archive/2003/11/11/55653.aspx) */
529
530 case WM_NCDESTROY:
531 {
532 szProgressText.Empty();
533 RemoveWindowSubclass(hWnd, DownloadProgressProc, uIdSubclass);
534 }
535 /* Fall-through */
536 default:
537 return DefSubclassProc(hWnd, uMsg, wParam, lParam);
538 }
539 }
540
541 DWORD WINAPI CDownloadManager::ThreadFunc(LPVOID Context)
542 {
543 CComPtr<IBindStatusCallback> dl;
544 ATL::CStringW Path;
545 PWSTR p, q;
546 HWND Dlg = (HWND) Context;
547
548 ULONG dwContentLen, dwBytesWritten, dwBytesRead, dwStatus;
549 ULONG dwCurrentBytesRead = 0;
550 ULONG dwStatusLen = sizeof(dwStatus);
551
552 BOOL bCancelled = FALSE;
553 BOOL bTempfile = FALSE;
554 BOOL bCab = FALSE;
555
556 HINTERNET hOpen = NULL;
557 HINTERNET hFile = NULL;
558 HANDLE hOut = INVALID_HANDLE_VALUE;
559
560 unsigned char lpBuffer[4096];
561 LPCWSTR lpszAgent = L"RApps/1.0";
562 URL_COMPONENTS urlComponents;
563 size_t urlLength, filenameLength;
564
565 const INT iAppId = iCurrentApp;
566 const PAPPLICATION_INFO pCurrentInfo = AppInfo;
567 if (!AppInfo)
568 {
569 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD);
570 goto end;
571 }
572
573 DownloadsListView.SetDownloadStatus(iAppId, DOWNLOAD_STATUS::DLDownloading);
574
575 // build the path for the download
576 p = wcsrchr(pCurrentInfo->szUrlDownload.GetString(), L'/');
577 q = wcsrchr(pCurrentInfo->szUrlDownload.GetString(), L'?');
578
579 // do we have a final slash separator?
580 if (!p)
581 goto end;
582
583 // prepare the tentative length of the filename, maybe we've to remove part of it later on
584 filenameLength = wcslen(p) * sizeof(WCHAR);
585
586 /* do we have query arguments in the target URL after the filename? account for them
587 (e.g. https://example.org/myfile.exe?no_adware_plz) */
588 if (q && q > p && (q - p) > 0)
589 filenameLength -= wcslen(q - 1) * sizeof(WCHAR);
590
591 // is this URL an update package for RAPPS? if so store it in a different place
592 if (pCurrentInfo->szUrlDownload == APPLICATION_DATABASE_URL)
593 {
594 bCab = TRUE;
595 if (!GetStorageDirectory(Path))
596 goto end;
597 }
598 else
599 {
600 Path = SettingsInfo.szDownloadDir;
601 }
602
603 // is the path valid? can we access it?
604 if (GetFileAttributesW(Path.GetString()) == INVALID_FILE_ATTRIBUTES)
605 {
606 if (!CreateDirectoryW(Path.GetString(), NULL))
607 goto end;
608 }
609
610 // append a \ to the provided file system path, and the filename portion from the URL after that
611 Path += L"\\";
612 Path += (LPWSTR) (p + 1);
613
614 if (!bCab && pCurrentInfo->szSHA1[0] && GetFileAttributesW(Path.GetString()) != INVALID_FILE_ATTRIBUTES)
615 {
616 // only open it in case of total correctness
617 if (VerifyInteg(pCurrentInfo->szSHA1, Path))
618 goto run;
619 }
620
621 // download it
622 bTempfile = TRUE;
623 CDownloadDialog_Constructor(Dlg, &bCancelled, IID_PPV_ARG(IBindStatusCallback, &dl));
624
625 if (dl == NULL)
626 goto end;
627
628 /* FIXME: this should just be using the system-wide proxy settings */
629 switch (SettingsInfo.Proxy)
630 {
631 case 0: // preconfig
632 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
633 break;
634 case 1: // direct (no proxy)
635 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
636 break;
637 case 2: // use proxy
638 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PROXY, SettingsInfo.szProxyServer, SettingsInfo.szNoProxyFor, 0);
639 break;
640 default: // preconfig
641 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
642 break;
643 }
644
645 if (!hOpen)
646 goto end;
647
648 hFile = InternetOpenUrlW(hOpen, pCurrentInfo->szUrlDownload, NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_KEEP_CONNECTION, 0);
649 if (!hFile)
650 {
651 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD2);
652 goto end;
653 }
654
655 if (!HttpQueryInfoW(hFile, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwStatusLen, NULL))
656 goto end;
657
658 if (dwStatus != HTTP_STATUS_OK)
659 {
660 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD);
661 goto end;
662 }
663
664 dwStatusLen = sizeof(dwStatus);
665
666 memset(&urlComponents, 0, sizeof(urlComponents));
667 urlComponents.dwStructSize = sizeof(urlComponents);
668
669 urlLength = pCurrentInfo->szUrlDownload.GetLength();
670 urlComponents.dwSchemeLength = urlLength + 1;
671 urlComponents.lpszScheme = (LPWSTR) malloc(urlComponents.dwSchemeLength * sizeof(WCHAR));
672 urlComponents.dwHostNameLength = urlLength + 1;
673 urlComponents.lpszHostName = (LPWSTR) malloc(urlComponents.dwHostNameLength * sizeof(WCHAR));
674
675 if (!InternetCrackUrlW(pCurrentInfo->szUrlDownload, urlLength + 1, ICU_DECODE | ICU_ESCAPE, &urlComponents))
676 goto end;
677
678 if (urlComponents.nScheme == INTERNET_SCHEME_HTTP || urlComponents.nScheme == INTERNET_SCHEME_HTTPS)
679 HttpQueryInfoW(hFile, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &dwContentLen, &dwStatus, 0);
680
681 if (urlComponents.nScheme == INTERNET_SCHEME_FTP)
682 dwContentLen = FtpGetFileSize(hFile, &dwStatus);
683
684 #ifdef USE_CERT_PINNING
685 // are we using HTTPS to download the RAPPS update package? check if the certificate is original
686 if ((urlComponents.nScheme == INTERNET_SCHEME_HTTPS) &&
687 (wcscmp(pCurrentInfo->szUrlDownload, APPLICATION_DATABASE_URL) == 0) &&
688 (!CertIsValid(hOpen, urlComponents.lpszHostName)))
689 {
690 MessageBox_LoadString(hMainWnd, IDS_CERT_DOES_NOT_MATCH);
691 goto end;
692 }
693 #endif
694
695 free(urlComponents.lpszScheme);
696 free(urlComponents.lpszHostName);
697
698 hOut = CreateFileW(Path.GetString(), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);
699
700 if (hOut == INVALID_HANDLE_VALUE)
701 goto end;
702
703 do
704 {
705 if (!InternetReadFile(hFile, lpBuffer, _countof(lpBuffer), &dwBytesRead))
706 {
707 MessageBox_LoadString(hMainWnd, IDS_INTERRUPTED_DOWNLOAD);
708 goto end;
709 }
710
711 if (!WriteFile(hOut, &lpBuffer[0], dwBytesRead, &dwBytesWritten, NULL))
712 {
713 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_WRITE);
714 goto end;
715 }
716
717 dwCurrentBytesRead += dwBytesRead;
718 dl->OnProgress(dwCurrentBytesRead, dwContentLen, 0, pCurrentInfo->szUrlDownload);
719 } while (dwBytesRead && !bCancelled);
720
721 CloseHandle(hOut);
722 hOut = INVALID_HANDLE_VALUE;
723
724 if (bCancelled)
725 goto end;
726
727 /* if this thing isn't a RAPPS update and it has a SHA-1 checksum
728 verify its integrity by using the native advapi32.A_SHA1 functions */
729 if (!bCab && pCurrentInfo->szSHA1[0] != 0)
730 {
731 ATL::CStringW szMsgText;
732
733 // change a few strings in the download dialog to reflect the verification process
734 if (!szMsgText.LoadStringW(hInst, IDS_INTEG_CHECK_TITLE))
735 goto end;
736
737 SetWindowTextW(Dlg, szMsgText.GetString());
738 SendMessageW(GetDlgItem(Dlg, IDC_DOWNLOAD_STATUS), WM_SETTEXT, 0, (LPARAM) Path.GetString());
739
740 // this may take a while, depending on the file size
741 if (!VerifyInteg(pCurrentInfo->szSHA1, Path.GetString()))
742 {
743 if (!szMsgText.LoadStringW(hInst, IDS_INTEG_CHECK_FAIL))
744 goto end;
745
746 MessageBoxW(Dlg, szMsgText.GetString(), NULL, MB_OK | MB_ICONERROR);
747 goto end;
748 }
749 }
750
751
752 run:
753 DownloadsListView.SetDownloadStatus(iAppId, DOWNLOAD_STATUS::DLWaitingToInstall);
754
755 // run it
756 if (!bCab)
757 {
758 SHELLEXECUTEINFOW shExInfo = {0};
759 shExInfo.cbSize = sizeof(shExInfo);
760 shExInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
761 shExInfo.lpVerb = L"open";
762 shExInfo.lpFile = Path.GetString();
763 shExInfo.lpParameters = L"";
764 shExInfo.nShow = SW_SHOW;
765
766 if (ShellExecuteExW(&shExInfo))
767 {
768 DownloadsListView.SetDownloadStatus(iAppId, DOWNLOAD_STATUS::DLInstalling);
769 //TODO: issue an install operation separately so that the apps could be downloaded in the background
770 WaitForSingleObject(shExInfo.hProcess, INFINITE);
771 CloseHandle(shExInfo.hProcess);
772 }
773 else
774 {
775 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_INSTALL);
776 }
777 }
778
779 end:
780 if (hOut != INVALID_HANDLE_VALUE)
781 CloseHandle(hOut);
782
783 InternetCloseHandle(hFile);
784 InternetCloseHandle(hOpen);
785
786 if (bTempfile)
787 {
788 if (bCancelled || (SettingsInfo.bDelInstaller && !bCab))
789 DeleteFileW(Path.GetString());
790 }
791 DownloadsListView.SetDownloadStatus(iAppId, DOWNLOAD_STATUS::DLFinished);
792 SendMessageW(Dlg, DL_START_NEW, 0, 0);
793 return 0;
794 }
795
796 BOOL CDownloadManager::DownloadListOfApplications(const ATL::CSimpleArray<PAPPLICATION_INFO>& AppsList, BOOL modal)
797 {
798 if (AppsList.GetSize() == 0)
799 {
800 return FALSE;
801 }
802
803 // Initialize shared variables
804 AppsToInstallList = AppsList;
805
806 // Create a dialog and issue a download process
807 LaunchDownloadDialog(modal);
808
809 return TRUE;
810 }
811
812 BOOL CDownloadManager::DownloadApplication(PAPPLICATION_INFO pAppInfo, BOOL modal)
813 {
814 if (!pAppInfo)
815 {
816 return FALSE;
817 }
818
819 AppsToInstallList.RemoveAll();
820 AppsToInstallList.Add(pAppInfo);
821 LaunchDownloadDialog(modal);
822
823 return TRUE;
824 }
825
826 VOID CDownloadManager::DownloadApplicationsDB(LPCWSTR lpUrl)
827 {
828 APPLICATION_INFO IntInfo;
829 IntInfo.szUrlDownload = lpUrl;
830
831 DownloadApplication(&IntInfo);
832 }
833
834 //TODO: Reuse the dialog
835 VOID CDownloadManager::LaunchDownloadDialog(BOOL modal)
836 {
837 if (modal)
838 {
839 DialogBoxW(hInst,
840 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG),
841 hMainWnd,
842 DownloadDlgProc);
843 }
844 else
845 {
846 CreateDialogW(hInst,
847 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG),
848 hMainWnd,
849 DownloadDlgProc);
850 }
851
852 }
853 // CDownloadManager