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