0d77959427c7469ff0d4d0005a9f2e6ae445cf8e
[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(ERROR_SUCCESS);
167 r = GetWindowLongPtrW(m_hDialog, GWLP_USERDATA);
168 if (r || GetLastError() != ERROR_SUCCESS)
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(StringID))
344 {
345 MessageBoxW(hMainWnd, szMsgText.GetString(), NULL, MB_OK | MB_ICONERROR);
346 }
347 }
348
349 struct DownloadParam
350 {
351 DownloadParam() : Dialog(NULL), AppInfo(), szCaption(NULL) {}
352 DownloadParam(HWND dlg, const ATL::CSimpleArray<PAPPLICATION_INFO> &info, LPCWSTR caption)
353 : Dialog(dlg), AppInfo(info), szCaption(caption)
354 {
355 }
356
357 HWND Dialog;
358 ATL::CSimpleArray<PAPPLICATION_INFO> AppInfo;
359 LPCWSTR szCaption;
360 };
361
362 // CDownloadManager
363 ATL::CSimpleArray<PAPPLICATION_INFO> CDownloadManager::AppsToInstallList;
364 CDowloadingAppsListView CDownloadManager::DownloadsListView;
365 INT CDownloadManager::iCurrentApp;
366
367 INT_PTR CALLBACK CDownloadManager::DownloadDlgProc(HWND Dlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
368 {
369 static WCHAR szCaption[MAX_PATH];
370
371 switch (uMsg)
372 {
373 case WM_INITDIALOG:
374 {
375 HICON hIconSm, hIconBg;
376
377 hIconBg = (HICON) GetClassLongW(hMainWnd, GCLP_HICON);
378 hIconSm = (HICON) GetClassLongW(hMainWnd, GCLP_HICONSM);
379
380 if (hIconBg && hIconSm)
381 {
382 SendMessageW(Dlg, WM_SETICON, ICON_BIG, (LPARAM) hIconBg);
383 SendMessageW(Dlg, WM_SETICON, ICON_SMALL, (LPARAM) hIconSm);
384 }
385
386 SetWindowLongW(Dlg, GWLP_USERDATA, 0);
387 HWND Item = GetDlgItem(Dlg, IDC_DOWNLOAD_PROGRESS);
388 if (Item)
389 {
390 // initialize the default values for our nifty progress bar
391 // and subclass it so that it learns to print a status text
392 SendMessageW(Item, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
393 SendMessageW(Item, PBM_SETPOS, 0, 0);
394
395 SetWindowSubclass(Item, DownloadProgressProc, 0, 0);
396 }
397
398 // Add a ListView
399 HWND hListView = DownloadsListView.Create(Dlg);
400 if (!hListView)
401 {
402 return FALSE;
403 }
404 DownloadsListView.LoadList(AppsToInstallList);
405
406 ShowWindow(Dlg, SW_SHOW);
407
408 // Get a dlg string for later use
409 GetWindowTextW(Dlg, szCaption, MAX_PATH);
410
411 // Start download process
412 DownloadParam *param = new DownloadParam(Dlg, AppsToInstallList, szCaption);
413 DWORD ThreadId;
414 HANDLE Thread = CreateThread(NULL, 0, ThreadFunc, (LPVOID) param, 0, &ThreadId);
415
416 if (!Thread)
417 {
418 return FALSE;
419 }
420
421 CloseHandle(Thread);
422 return TRUE;
423 }
424
425 case WM_COMMAND:
426 if (wParam == IDCANCEL)
427 {
428 SetWindowLongW(Dlg, GWLP_USERDATA, 1);
429 PostMessageW(Dlg, WM_CLOSE, 0, 0);
430 }
431 return FALSE;
432
433 case WM_CLOSE:
434 EndDialog(Dlg, 0);
435 //DestroyWindow(Dlg);
436 return TRUE;
437
438 default:
439 return FALSE;
440 }
441 }
442
443 LRESULT CALLBACK CDownloadManager::DownloadProgressProc(HWND hWnd,
444 UINT uMsg,
445 WPARAM wParam,
446 LPARAM lParam,
447 UINT_PTR uIdSubclass,
448 DWORD_PTR dwRefData)
449 {
450 static ATL::CStringW szProgressText;
451
452 switch (uMsg)
453 {
454 case WM_SETTEXT:
455 {
456 if (lParam)
457 {
458 szProgressText = (PCWSTR) lParam;
459 }
460 return TRUE;
461 }
462
463 case WM_ERASEBKGND:
464 case WM_PAINT:
465 {
466 PAINTSTRUCT ps;
467 HDC hDC = BeginPaint(hWnd, &ps), hdcMem;
468 HBITMAP hbmMem;
469 HANDLE hOld;
470 RECT myRect;
471 UINT win_width, win_height;
472
473 GetClientRect(hWnd, &myRect);
474
475 /* grab the progress bar rect size */
476 win_width = myRect.right - myRect.left;
477 win_height = myRect.bottom - myRect.top;
478
479 /* create an off-screen DC for double-buffering */
480 hdcMem = CreateCompatibleDC(hDC);
481 hbmMem = CreateCompatibleBitmap(hDC, win_width, win_height);
482
483 hOld = SelectObject(hdcMem, hbmMem);
484
485 /* call the original draw code and redirect it to our memory buffer */
486 DefSubclassProc(hWnd, uMsg, (WPARAM) hdcMem, lParam);
487
488 /* draw our nifty progress text over it */
489 SelectFont(hdcMem, GetStockFont(DEFAULT_GUI_FONT));
490 DrawShadowText(hdcMem, szProgressText.GetString(), szProgressText.GetLength(),
491 &myRect,
492 DT_CENTER | DT_VCENTER | DT_NOPREFIX | DT_SINGLELINE,
493 GetSysColor(COLOR_CAPTIONTEXT),
494 GetSysColor(COLOR_3DSHADOW),
495 1, 1);
496
497 /* transfer the off-screen DC to the screen */
498 BitBlt(hDC, 0, 0, win_width, win_height, hdcMem, 0, 0, SRCCOPY);
499
500 /* free the off-screen DC */
501 SelectObject(hdcMem, hOld);
502 DeleteObject(hbmMem);
503 DeleteDC(hdcMem);
504
505 EndPaint(hWnd, &ps);
506 return 0;
507 }
508
509 /* Raymond Chen says that we should safely unsubclass all the things!
510 (http://blogs.msdn.com/b/oldnewthing/archive/2003/11/11/55653.aspx) */
511
512 case WM_NCDESTROY:
513 {
514 szProgressText.Empty();
515 RemoveWindowSubclass(hWnd, DownloadProgressProc, uIdSubclass);
516 }
517 /* Fall-through */
518 default:
519 return DefSubclassProc(hWnd, uMsg, wParam, lParam);
520 }
521 }
522
523 DWORD WINAPI CDownloadManager::ThreadFunc(LPVOID param)
524 {
525 CComPtr<IBindStatusCallback> dl;
526 ATL::CStringW Path;
527 PWSTR p, q;
528
529 HWND hDlg = static_cast<DownloadParam*>(param)->Dialog;
530
531 ULONG dwContentLen, dwBytesWritten, dwBytesRead, dwStatus;
532 ULONG dwCurrentBytesRead = 0;
533 ULONG dwStatusLen = sizeof(dwStatus);
534
535 BOOL bCancelled = FALSE;
536 BOOL bTempfile = FALSE;
537 BOOL bCab = FALSE;
538
539 HINTERNET hOpen = NULL;
540 HINTERNET hFile = NULL;
541 HANDLE hOut = INVALID_HANDLE_VALUE;
542
543 unsigned char lpBuffer[4096];
544 LPCWSTR lpszAgent = L"RApps/1.0";
545 URL_COMPONENTS urlComponents;
546 size_t urlLength, filenameLength;
547
548 const INT iAppId = iCurrentApp;
549 const ATL::CSimpleArray<PAPPLICATION_INFO> InfoArray = static_cast<DownloadParam*>(param)->AppInfo;
550 LPCWSTR szCaption = static_cast<DownloadParam*>(param)->szCaption;
551 ATL::CStringW szNewCaption;
552
553 delete param;
554 if (InfoArray.GetSize() <= 0)
555 {
556 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD);
557 goto end;
558 }
559
560 for (INT iAppId = 0; iAppId < InfoArray.GetSize(); ++iAppId)
561 {
562 PAPPLICATION_INFO pCurrentInfo = AppsToInstallList[iAppId];
563 if (!pCurrentInfo)
564 {
565 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD);
566 continue;
567 }
568
569 // build the path for the download
570 p = wcsrchr(pCurrentInfo->szUrlDownload.GetString(), L'/');
571 q = wcsrchr(pCurrentInfo->szUrlDownload.GetString(), L'?');
572
573 // do we have a final slash separator?
574 if (!p)
575 goto end;
576
577 // prepare the tentative length of the filename, maybe we've to remove part of it later on
578 filenameLength = wcslen(p) * sizeof(WCHAR);
579
580 /* do we have query arguments in the target URL after the filename? account for them
581 (e.g. https://example.org/myfile.exe?no_adware_plz) */
582 if (q && q > p && (q - p) > 0)
583 filenameLength -= wcslen(q - 1) * sizeof(WCHAR);
584
585 // is this URL an update package for RAPPS? if so store it in a different place
586 if (pCurrentInfo->szUrlDownload == APPLICATION_DATABASE_URL)
587 {
588 bCab = TRUE;
589 if (!GetStorageDirectory(Path))
590 goto end;
591 }
592 else
593 {
594 Path = SettingsInfo.szDownloadDir;
595 }
596
597 // is the path valid? can we access it?
598 if (GetFileAttributesW(Path.GetString()) == INVALID_FILE_ATTRIBUTES)
599 {
600 if (!CreateDirectoryW(Path.GetString(), NULL))
601 goto end;
602 }
603
604 // append a \ to the provided file system path, and the filename portion from the URL after that
605 Path += L"\\";
606 Path += (LPWSTR) (p + 1);
607
608 if (!bCab && pCurrentInfo->szSHA1[0] && GetFileAttributesW(Path.GetString()) != INVALID_FILE_ATTRIBUTES)
609 {
610 // only open it in case of total correctness
611 if (VerifyInteg(pCurrentInfo->szSHA1, Path))
612 goto run;
613 }
614
615 // Reset progress bar
616 HWND Item = GetDlgItem(hDlg, IDC_DOWNLOAD_PROGRESS);
617 if (Item)
618 {
619 SendMessageW(Item, PBM_SETPOS, 0, 0);
620 }
621
622 // Change caption to show the currently downloaded app
623 if (!bCab)
624 {
625 szNewCaption.Format(szCaption, pCurrentInfo->szName.GetString());
626 }
627 else
628 {
629 szNewCaption.LoadStringW(IDS_DL_DIALOG_DB_DOWNLOAD_DISP);
630 }
631
632 SetWindowTextW(hDlg, szNewCaption.GetString());
633
634 // Add the download URL
635 SetDlgItemTextW(hDlg, IDC_DOWNLOAD_STATUS, pCurrentInfo->szUrlDownload.GetString());
636
637 DownloadsListView.SetDownloadStatus(iAppId, DOWNLOAD_STATUS::DLDownloading);
638
639 // download it
640 bTempfile = TRUE;
641 CDownloadDialog_Constructor(hDlg, &bCancelled, IID_PPV_ARG(IBindStatusCallback, &dl));
642
643 if (dl == NULL)
644 goto end;
645
646 /* FIXME: this should just be using the system-wide proxy settings */
647 switch (SettingsInfo.Proxy)
648 {
649 case 0: // preconfig
650 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
651 break;
652 case 1: // direct (no proxy)
653 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
654 break;
655 case 2: // use proxy
656 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PROXY, SettingsInfo.szProxyServer, SettingsInfo.szNoProxyFor, 0);
657 break;
658 default: // preconfig
659 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
660 break;
661 }
662
663 if (!hOpen)
664 goto end;
665
666 hFile = InternetOpenUrlW(hOpen, pCurrentInfo->szUrlDownload.GetString(), NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_KEEP_CONNECTION, 0);
667
668 if (!hFile)
669 {
670 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD2);
671 goto end;
672 }
673
674 if (!HttpQueryInfoW(hFile, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwStatusLen, NULL))
675 goto end;
676
677 if (dwStatus != HTTP_STATUS_OK)
678 {
679 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD);
680 goto end;
681 }
682
683 dwStatusLen = sizeof(dwStatus);
684
685 memset(&urlComponents, 0, sizeof(urlComponents));
686 urlComponents.dwStructSize = sizeof(urlComponents);
687
688 urlLength = pCurrentInfo->szUrlDownload.GetLength();
689 urlComponents.dwSchemeLength = urlLength + 1;
690 urlComponents.lpszScheme = (LPWSTR) malloc(urlComponents.dwSchemeLength * sizeof(WCHAR));
691 urlComponents.dwHostNameLength = urlLength + 1;
692 urlComponents.lpszHostName = (LPWSTR) malloc(urlComponents.dwHostNameLength * sizeof(WCHAR));
693
694 if (!InternetCrackUrlW(pCurrentInfo->szUrlDownload, urlLength + 1, ICU_DECODE | ICU_ESCAPE, &urlComponents))
695 goto end;
696
697 if (urlComponents.nScheme == INTERNET_SCHEME_HTTP || urlComponents.nScheme == INTERNET_SCHEME_HTTPS)
698 HttpQueryInfoW(hFile, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &dwContentLen, &dwStatus, 0);
699
700 if (urlComponents.nScheme == INTERNET_SCHEME_FTP)
701 dwContentLen = FtpGetFileSize(hFile, &dwStatus);
702
703 #ifdef USE_CERT_PINNING
704 // are we using HTTPS to download the RAPPS update package? check if the certificate is original
705 if ((urlComponents.nScheme == INTERNET_SCHEME_HTTPS) &&
706 (wcscmp(pCurrentInfo->szUrlDownload, APPLICATION_DATABASE_URL) == 0) &&
707 (!CertIsValid(hOpen, urlComponents.lpszHostName)))
708 {
709 MessageBox_LoadString(hMainWnd, IDS_CERT_DOES_NOT_MATCH);
710 goto end;
711 }
712 #endif
713
714 free(urlComponents.lpszScheme);
715 free(urlComponents.lpszHostName);
716
717 hOut = CreateFileW(Path.GetString(), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);
718
719 if (hOut == INVALID_HANDLE_VALUE)
720 goto end;
721
722 dwCurrentBytesRead = 0;
723 do
724 {
725 if (!InternetReadFile(hFile, lpBuffer, _countof(lpBuffer), &dwBytesRead))
726 {
727 MessageBox_LoadString(hMainWnd, IDS_INTERRUPTED_DOWNLOAD);
728 goto end;
729 }
730
731 if (!WriteFile(hOut, &lpBuffer[0], dwBytesRead, &dwBytesWritten, NULL))
732 {
733 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_WRITE);
734 goto end;
735 }
736
737 dwCurrentBytesRead += dwBytesRead;
738 dl->OnProgress(dwCurrentBytesRead, dwContentLen, 0, pCurrentInfo->szUrlDownload.GetString());
739 } while (dwBytesRead && !bCancelled);
740
741 CloseHandle(hOut);
742 hOut = INVALID_HANDLE_VALUE;
743
744 if (bCancelled)
745 goto end;
746
747 /* if this thing isn't a RAPPS update and it has a SHA-1 checksum
748 verify its integrity by using the native advapi32.A_SHA1 functions */
749 if (!bCab && pCurrentInfo->szSHA1[0] != 0)
750 {
751 ATL::CStringW szMsgText;
752
753 // change a few strings in the download dialog to reflect the verification process
754 if (!szMsgText.LoadStringW(IDS_INTEG_CHECK_TITLE))
755 goto end;
756
757 SetWindowTextW(hDlg, szMsgText.GetString());
758 SendMessageW(GetDlgItem(hDlg, IDC_DOWNLOAD_STATUS), WM_SETTEXT, 0, (LPARAM) Path.GetString());
759
760 // this may take a while, depending on the file size
761 if (!VerifyInteg(pCurrentInfo->szSHA1, Path.GetString()))
762 {
763 if (!szMsgText.LoadStringW(IDS_INTEG_CHECK_FAIL))
764 goto end;
765
766 MessageBoxW(hDlg, szMsgText.GetString(), NULL, MB_OK | MB_ICONERROR);
767 goto end;
768 }
769 }
770
771 run:
772 DownloadsListView.SetDownloadStatus(iAppId, DOWNLOAD_STATUS::DLWaitingToInstall);
773
774 // run it
775 if (!bCab)
776 {
777 SHELLEXECUTEINFOW shExInfo = {0};
778 shExInfo.cbSize = sizeof(shExInfo);
779 shExInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
780 shExInfo.lpVerb = L"open";
781 shExInfo.lpFile = Path.GetString();
782 shExInfo.lpParameters = L"";
783 shExInfo.nShow = SW_SHOW;
784
785 if (ShellExecuteExW(&shExInfo))
786 {
787 DownloadsListView.SetDownloadStatus(iAppId, DOWNLOAD_STATUS::DLInstalling);
788 //TODO: issue an install operation separately so that the apps could be downloaded in the background
789 WaitForSingleObject(shExInfo.hProcess, INFINITE);
790 CloseHandle(shExInfo.hProcess);
791 }
792 else
793 {
794 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_INSTALL);
795 }
796 }
797
798 end:
799 if (hOut != INVALID_HANDLE_VALUE)
800 CloseHandle(hOut);
801
802 InternetCloseHandle(hFile);
803 InternetCloseHandle(hOpen);
804
805 if (bTempfile)
806 {
807 if (bCancelled || (SettingsInfo.bDelInstaller && !bCab))
808 DeleteFileW(Path.GetString());
809 }
810
811 DownloadsListView.SetDownloadStatus(iAppId, DOWNLOAD_STATUS::DLFinished);
812 }
813
814 SendMessageW(hDlg, WM_CLOSE, 0, 0);
815 return 0;
816 }
817
818 BOOL CDownloadManager::DownloadListOfApplications(const ATL::CSimpleArray<PAPPLICATION_INFO>& AppsList, BOOL bIsModal)
819 {
820 if (AppsList.GetSize() == 0)
821 {
822 return FALSE;
823 }
824
825 // Initialize shared variables
826 AppsToInstallList = AppsList;
827
828 // Create a dialog and issue a download process
829 LaunchDownloadDialog(bIsModal);
830
831 return TRUE;
832 }
833
834 BOOL CDownloadManager::DownloadApplication(PAPPLICATION_INFO pAppInfo, BOOL bIsModal)
835 {
836 if (!pAppInfo)
837 {
838 return FALSE;
839 }
840
841 AppsToInstallList.RemoveAll();
842 AppsToInstallList.Add(pAppInfo);
843 LaunchDownloadDialog(bIsModal);
844
845 return TRUE;
846 }
847
848 VOID CDownloadManager::DownloadApplicationsDB(LPCWSTR lpUrl)
849 {
850 static APPLICATION_INFO IntInfo;
851 IntInfo.szUrlDownload = lpUrl;
852 IntInfo.szName.LoadStringW(IDS_DL_DIALOG_DB_DISP);
853 DownloadApplication(&IntInfo, TRUE);
854 }
855
856 //TODO: Reuse the dialog
857 VOID CDownloadManager::LaunchDownloadDialog(BOOL bIsModal)
858 {
859 if (bIsModal)
860 {
861 DialogBoxW(hInst,
862 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG),
863 hMainWnd,
864 DownloadDlgProc);
865 }
866 else
867 {
868 CreateDialogW(hInst,
869 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG),
870 hMainWnd,
871 DownloadDlgProc);
872 }
873 }
874 // CDownloadManager