wininet: Support the Cache-Control max-age directive for setting url cache entry...
[wine/testsucceed.git] / dlls / appwiz.cpl / addons.c
blobc55806fb69a7b5ac7cb42c6936189fb9d3c1cd86
1 /*
2 * Copyright 2006-2010 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
21 #include <stdarg.h>
22 #include <fcntl.h>
23 #include <stdio.h>
24 #ifdef HAVE_UNISTD_H
25 # include <unistd.h>
26 #endif
28 #define COBJMACROS
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winuser.h"
35 #include "cpl.h"
36 #include "winreg.h"
37 #include "ole2.h"
38 #include "commctrl.h"
39 #include "advpub.h"
40 #include "wininet.h"
41 #include "shellapi.h"
42 #include "urlmon.h"
43 #include "msi.h"
45 #include "appwiz.h"
46 #include "res.h"
48 #include "wine/debug.h"
49 #include "wine/unicode.h"
50 #include "wine/library.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(appwizcpl);
54 #define GECKO_VERSION "1.1.0"
56 #ifdef __i386__
57 #define ARCH_STRING "x86"
58 #define GECKO_SHA "1b6c637207b6f032ae8a52841db9659433482714"
59 #elif defined(__x86_64__)
60 #define ARCH_STRING "x86_64"
61 #define GECKO_SHA "55b4b60cd2a48631d6236fb411c3a94d806d9906"
62 #else
63 #define ARCH_STRING ""
64 #define GECKO_SHA "???"
65 #endif
67 #define GECKO_FILE_NAME "wine_gecko-" GECKO_VERSION "-" ARCH_STRING ".cab"
69 static const WCHAR mshtml_keyW[] =
70 {'S','o','f','t','w','a','r','e',
71 '\\','W','i','n','e',
72 '\\','M','S','H','T','M','L',0};
74 static HWND install_dialog = NULL;
75 static LPWSTR url = NULL;
77 static inline char *heap_strdupWtoA(LPCWSTR str)
79 char *ret = NULL;
81 if(str) {
82 DWORD size = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
83 ret = heap_alloc(size);
84 WideCharToMultiByte(CP_ACP, 0, str, -1, ret, size, NULL, NULL);
87 return ret;
90 /* SHA definitions are copied from advapi32. They aren't available in headers. */
92 typedef struct {
93 ULONG Unknown[6];
94 ULONG State[5];
95 ULONG Count[2];
96 UCHAR Buffer[64];
97 } SHA_CTX, *PSHA_CTX;
99 void WINAPI A_SHAInit(PSHA_CTX);
100 void WINAPI A_SHAUpdate(PSHA_CTX,const unsigned char*,UINT);
101 void WINAPI A_SHAFinal(PSHA_CTX,PULONG);
103 static BOOL sha_check(const WCHAR *file_name)
105 const unsigned char *file_map;
106 HANDLE file, map;
107 ULONG sha[5];
108 char buf[2*sizeof(sha)+1];
109 SHA_CTX ctx;
110 DWORD size, i;
112 file = CreateFileW(file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
113 if(file == INVALID_HANDLE_VALUE)
114 return FALSE;
116 size = GetFileSize(file, NULL);
118 map = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
119 CloseHandle(file);
120 if(!map)
121 return FALSE;
123 file_map = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
124 CloseHandle(map);
125 if(!file_map)
126 return FALSE;
128 A_SHAInit(&ctx);
129 A_SHAUpdate(&ctx, file_map, size);
130 A_SHAFinal(&ctx, sha);
132 UnmapViewOfFile(file_map);
134 for(i=0; i < sizeof(sha); i++)
135 sprintf(buf + i*2, "%02x", *((unsigned char*)sha+i));
137 if(strcmp(buf, GECKO_SHA)) {
138 WCHAR message[256];
140 WARN("Got %s, expected %s\n", buf, GECKO_SHA);
142 if(LoadStringW(hInst, IDS_INVALID_SHA, message, sizeof(message)/sizeof(WCHAR)))
143 MessageBoxW(NULL, message, NULL, MB_ICONERROR);
145 return FALSE;
148 return TRUE;
151 static void set_status(DWORD id)
153 HWND status = GetDlgItem(install_dialog, ID_DWL_STATUS);
154 WCHAR buf[64];
156 LoadStringW(hInst, id, buf, sizeof(buf)/sizeof(WCHAR));
157 SendMessageW(status, WM_SETTEXT, 0, (LPARAM)buf);
160 static void set_registry(const WCHAR *install_dir)
162 WCHAR mshtml_key[100];
163 LPWSTR gecko_path;
164 HKEY hkey;
165 DWORD res, len;
167 static const WCHAR wszGeckoPath[] = {'G','e','c','k','o','P','a','t','h',0};
168 static const WCHAR wszWineGecko[] = {'w','i','n','e','_','g','e','c','k','o',0};
170 memcpy(mshtml_key, mshtml_keyW, sizeof(mshtml_keyW));
171 mshtml_key[sizeof(mshtml_keyW)/sizeof(WCHAR)-1] = '\\';
172 MultiByteToWideChar(CP_ACP, 0, GECKO_VERSION, sizeof(GECKO_VERSION),
173 mshtml_key+sizeof(mshtml_keyW)/sizeof(WCHAR),
174 (sizeof(mshtml_key)-sizeof(mshtml_keyW))/sizeof(WCHAR));
176 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML\<version> */
177 res = RegCreateKeyW(HKEY_CURRENT_USER, mshtml_key, &hkey);
178 if(res != ERROR_SUCCESS) {
179 ERR("Faild to create MSHTML key: %d\n", res);
180 return;
183 len = strlenW(install_dir);
184 gecko_path = heap_alloc((len+1)*sizeof(WCHAR)+sizeof(wszWineGecko));
185 memcpy(gecko_path, install_dir, len*sizeof(WCHAR));
187 if (len && gecko_path[len-1] != '\\')
188 gecko_path[len++] = '\\';
190 memcpy(gecko_path+len, wszWineGecko, sizeof(wszWineGecko));
192 res = RegSetValueExW(hkey, wszGeckoPath, 0, REG_SZ, (LPVOID)gecko_path,
193 len*sizeof(WCHAR)+sizeof(wszWineGecko));
194 heap_free(gecko_path);
195 RegCloseKey(hkey);
196 if(res != ERROR_SUCCESS)
197 ERR("Failed to set GeckoPath value: %08x\n", res);
200 static BOOL install_cab(LPCWSTR file_name)
202 char *install_dir_a, *file_name_a;
203 WCHAR install_dir[MAX_PATH];
204 DWORD res, len;
205 HRESULT hres;
207 static const WCHAR gecko_subdirW[] = {'\\','g','e','c','k','o','\\',0};
209 TRACE("(%s)\n", debugstr_w(file_name));
211 GetSystemDirectoryW(install_dir, sizeof(install_dir)/sizeof(WCHAR));
212 strcatW(install_dir, gecko_subdirW);
213 res = CreateDirectoryW(install_dir, NULL);
214 if(!res && GetLastError() != ERROR_ALREADY_EXISTS) {
215 ERR("Could not create directory: %08u\n", GetLastError());
216 return FALSE;
219 len = strlenW(install_dir);
220 MultiByteToWideChar(CP_ACP, 0, GECKO_VERSION, -1, install_dir+len, sizeof(install_dir)/sizeof(WCHAR)-len);
221 res = CreateDirectoryW(install_dir, NULL);
222 if(!res && GetLastError() != ERROR_ALREADY_EXISTS) {
223 ERR("Could not create directory: %08u\n", GetLastError());
224 return FALSE;
228 /* FIXME: Use ExtractFilesW once it's implemented */
229 file_name_a = heap_strdupWtoA(file_name);
230 install_dir_a = heap_strdupWtoA(install_dir);
231 if(file_name_a && install_dir_a)
232 hres = ExtractFilesA(file_name_a, install_dir_a, 0, NULL, NULL, 0);
233 else
234 hres = E_OUTOFMEMORY;
235 heap_free(file_name_a);
236 heap_free(install_dir_a);
237 if(FAILED(hres)) {
238 ERR("Could not extract package: %08x\n", hres);
239 return FALSE;
242 set_registry(install_dir);
243 return TRUE;
246 static BOOL install_msi_file(const WCHAR *file_name)
248 ULONG res;
250 res = MsiInstallProductW(file_name, NULL);
251 if(res != ERROR_SUCCESS) {
252 ERR("MsiInstallProduct failed: %u\n", res);
253 return FALSE;
256 return TRUE;
259 static BOOL install_file(const WCHAR *file_name)
261 BYTE magic[4];
262 HANDLE file;
263 DWORD size;
264 BOOL res;
266 static const BYTE msi_magic[] = {0xd0,0xcf,0x11,0xe0};
268 file = CreateFileW(file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
269 if(file == INVALID_HANDLE_VALUE)
270 return FALSE;
272 res = ReadFile(file, magic, sizeof(magic), &size, NULL);
273 CloseHandle(file);
274 if(!res || size != sizeof(magic))
275 return INET_E_DOWNLOAD_FAILURE;
277 if(!memcmp(magic, "MSCF", sizeof(magic)))
278 return install_cab(file_name);
279 else if(!memcmp(magic, msi_magic, sizeof(magic)))
280 return install_msi_file(file_name);
282 ERR("Unknown file magic\n");
283 return FALSE;
286 static BOOL install_from_unix_file(const char *file_name)
288 LPWSTR dos_file_name;
289 int fd;
290 BOOL ret;
292 static WCHAR * (CDECL *wine_get_dos_file_name)(const char*);
293 static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
295 fd = open(file_name, O_RDONLY);
296 if(fd == -1) {
297 TRACE("%s not found\n", debugstr_a(file_name));
298 return FALSE;
301 close(fd);
303 if(!wine_get_dos_file_name)
304 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleW(kernel32W), "wine_get_dos_file_name");
306 if(wine_get_dos_file_name) { /* Wine UNIX mode */
307 dos_file_name = wine_get_dos_file_name(file_name);
308 if(!dos_file_name) {
309 ERR("Could not get dos file name of %s\n", debugstr_a(file_name));
310 return FALSE;
312 } else { /* Windows mode */
313 UINT res;
314 WARN("Could not get wine_get_dos_file_name function, calling install_cab directly.\n");
315 res = MultiByteToWideChar( CP_ACP, 0, file_name, -1, 0, 0);
316 dos_file_name = heap_alloc (res*sizeof(WCHAR));
317 MultiByteToWideChar( CP_ACP, 0, file_name, -1, dos_file_name, res);
320 ret = install_file(dos_file_name);
322 heap_free(dos_file_name);
323 return ret;
326 static BOOL install_from_registered_dir(void)
328 char *file_name;
329 HKEY hkey;
330 DWORD res, type, size = MAX_PATH;
331 BOOL ret;
333 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
334 res = RegOpenKeyW(HKEY_CURRENT_USER, mshtml_keyW, &hkey);
335 if(res != ERROR_SUCCESS)
336 return FALSE;
338 file_name = heap_alloc(size+sizeof(GECKO_FILE_NAME));
339 res = RegGetValueA(hkey, NULL, "GeckoCabDir", RRF_RT_ANY, &type, (PBYTE)file_name, &size);
340 if(res == ERROR_MORE_DATA) {
341 file_name = heap_realloc(file_name, size+sizeof(GECKO_FILE_NAME));
342 res = RegGetValueA(hkey, NULL, "GeckoCabDir", RRF_RT_ANY, &type, (PBYTE)file_name, &size);
344 RegCloseKey(hkey);
345 if(res != ERROR_SUCCESS || (type != REG_SZ && type != REG_EXPAND_SZ)) {
346 heap_free(file_name);
347 return FALSE;
350 strcat(file_name, GECKO_FILE_NAME);
352 TRACE("Trying %s\n", debugstr_a(file_name));
354 ret = install_from_unix_file(file_name);
356 heap_free(file_name);
357 return ret;
360 static BOOL install_from_default_dir(void)
362 const char *data_dir, *subdir;
363 char *file_name;
364 int len, len2;
365 BOOL ret;
367 if((data_dir = wine_get_data_dir()))
368 subdir = "/gecko/";
369 else if((data_dir = wine_get_build_dir()))
370 subdir = "/../gecko/";
371 else
372 return FALSE;
374 len = strlen(data_dir);
375 len2 = strlen(subdir);
377 file_name = heap_alloc(len+len2+sizeof(GECKO_FILE_NAME));
378 memcpy(file_name, data_dir, len);
379 memcpy(file_name+len, subdir, len2);
380 memcpy(file_name+len+len2, GECKO_FILE_NAME, sizeof(GECKO_FILE_NAME));
382 ret = install_from_unix_file(file_name);
384 heap_free(file_name);
386 if (!ret)
387 ret = install_from_unix_file(INSTALL_DATADIR "/wine/gecko/" GECKO_FILE_NAME);
388 if (!ret && strcmp(INSTALL_DATADIR, "/usr/share"))
389 ret = install_from_unix_file("/usr/share/wine/gecko/" GECKO_FILE_NAME);
390 return ret;
393 static HRESULT WINAPI InstallCallback_QueryInterface(IBindStatusCallback *iface,
394 REFIID riid, void **ppv)
396 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IBindStatusCallback, riid)) {
397 *ppv = iface;
398 return S_OK;
401 return E_INVALIDARG;
404 static ULONG WINAPI InstallCallback_AddRef(IBindStatusCallback *iface)
406 return 2;
409 static ULONG WINAPI InstallCallback_Release(IBindStatusCallback *iface)
411 return 1;
414 static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
415 DWORD dwReserved, IBinding *pib)
417 set_status(IDS_DOWNLOADING);
418 return S_OK;
421 static HRESULT WINAPI InstallCallback_GetPriority(IBindStatusCallback *iface,
422 LONG *pnPriority)
424 return E_NOTIMPL;
427 static HRESULT WINAPI InstallCallback_OnLowResource(IBindStatusCallback *iface,
428 DWORD dwReserved)
430 return E_NOTIMPL;
433 static HRESULT WINAPI InstallCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
434 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
436 HWND progress = GetDlgItem(install_dialog, ID_DWL_PROGRESS);
438 if(ulProgressMax)
439 SendMessageW(progress, PBM_SETRANGE32, 0, ulProgressMax);
440 if(ulProgress)
441 SendMessageW(progress, PBM_SETPOS, ulProgress, 0);
443 return S_OK;
446 static HRESULT WINAPI InstallCallback_OnStopBinding(IBindStatusCallback *iface,
447 HRESULT hresult, LPCWSTR szError)
449 if(FAILED(hresult)) {
450 ERR("Binding failed %08x\n", hresult);
451 return S_OK;
454 set_status(IDS_INSTALLING);
455 return S_OK;
458 static HRESULT WINAPI InstallCallback_GetBindInfo(IBindStatusCallback *iface,
459 DWORD* grfBINDF, BINDINFO* pbindinfo)
461 /* FIXME */
462 *grfBINDF = 0;
463 return S_OK;
466 static HRESULT WINAPI InstallCallback_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
467 DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
469 ERR("\n");
470 return E_NOTIMPL;
473 static HRESULT WINAPI InstallCallback_OnObjectAvailable(IBindStatusCallback *iface,
474 REFIID riid, IUnknown* punk)
476 ERR("\n");
477 return E_NOTIMPL;
480 static const IBindStatusCallbackVtbl InstallCallbackVtbl = {
481 InstallCallback_QueryInterface,
482 InstallCallback_AddRef,
483 InstallCallback_Release,
484 InstallCallback_OnStartBinding,
485 InstallCallback_GetPriority,
486 InstallCallback_OnLowResource,
487 InstallCallback_OnProgress,
488 InstallCallback_OnStopBinding,
489 InstallCallback_GetBindInfo,
490 InstallCallback_OnDataAvailable,
491 InstallCallback_OnObjectAvailable
494 static IBindStatusCallback InstallCallback = { &InstallCallbackVtbl };
496 static LPWSTR get_url(void)
498 HKEY hkey;
499 DWORD res, type;
500 DWORD size = INTERNET_MAX_URL_LENGTH*sizeof(WCHAR);
501 DWORD returned_size;
502 LPWSTR url;
504 static const WCHAR wszGeckoUrl[] = {'G','e','c','k','o','U','r','l',0};
505 static const WCHAR httpW[] = {'h','t','t','p'};
506 static const WCHAR arch_formatW[] = {'?','a','r','c','h','='};
507 static const WCHAR v_formatW[] = {'&','v','='};
509 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
510 res = RegOpenKeyW(HKEY_CURRENT_USER, mshtml_keyW, &hkey);
511 if(res != ERROR_SUCCESS)
512 return NULL;
514 url = heap_alloc(size);
515 returned_size = size;
517 res = RegQueryValueExW(hkey, wszGeckoUrl, NULL, &type, (LPBYTE)url, &returned_size);
518 RegCloseKey(hkey);
519 if(res != ERROR_SUCCESS || type != REG_SZ) {
520 heap_free(url);
521 return NULL;
524 if(returned_size > sizeof(httpW) && !memcmp(url, httpW, sizeof(httpW))) {
525 DWORD len;
527 len = strlenW(url);
528 memcpy(url+len, arch_formatW, sizeof(arch_formatW));
529 len += sizeof(arch_formatW)/sizeof(WCHAR);
530 len += MultiByteToWideChar(CP_ACP, 0, ARCH_STRING, sizeof(ARCH_STRING), url+len, size/sizeof(WCHAR)-len)-1;
531 memcpy(url+len, v_formatW, sizeof(v_formatW));
532 len += sizeof(v_formatW)/sizeof(WCHAR);
533 MultiByteToWideChar(CP_ACP, 0, GECKO_VERSION, -1, url+len, size/sizeof(WCHAR)-len);
536 TRACE("Got URL %s\n", debugstr_w(url));
537 return url;
540 static DWORD WINAPI download_proc(PVOID arg)
542 WCHAR tmp_dir[MAX_PATH], tmp_file[MAX_PATH];
543 HRESULT hres;
545 GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);
546 GetTempFileNameW(tmp_dir, NULL, 0, tmp_file);
548 TRACE("using temp file %s\n", debugstr_w(tmp_file));
550 hres = URLDownloadToFileW(NULL, url, tmp_file, 0, &InstallCallback);
551 if(FAILED(hres)) {
552 ERR("URLDownloadToFile failed: %08x\n", hres);
553 return 0;
556 if(sha_check(tmp_file))
557 install_file(tmp_file);
558 DeleteFileW(tmp_file);
559 EndDialog(install_dialog, 0);
560 return 0;
563 static INT_PTR CALLBACK installer_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
565 switch(msg) {
566 case WM_INITDIALOG:
567 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_HIDE);
568 install_dialog = hwnd;
569 return TRUE;
571 case WM_COMMAND:
572 switch(wParam) {
573 case IDCANCEL:
574 EndDialog(hwnd, 0);
575 return FALSE;
577 case ID_DWL_INSTALL:
578 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_SHOW);
579 EnableWindow(GetDlgItem(hwnd, ID_DWL_INSTALL), 0);
580 EnableWindow(GetDlgItem(hwnd, IDCANCEL), 0); /* FIXME */
581 CreateThread(NULL, 0, download_proc, NULL, 0, NULL);
582 return FALSE;
586 return FALSE;
589 BOOL install_wine_gecko(void)
591 if(!*ARCH_STRING)
592 return FALSE;
595 * Try to find Gecko .cab file in following order:
596 * - directory stored in GeckoCabDir value of HKCU/Wine/Software/MSHTML key
597 * - $datadir/gecko/
598 * - $INSTALL_DATADIR/wine/gecko/
599 * - /usr/share/wine/gecko/
600 * - download from URL stored in GeckoUrl value of HKCU/Wine/Software/MSHTML key
602 if(!install_from_registered_dir()
603 && !install_from_default_dir()
604 && (url = get_url()))
605 DialogBoxW(hInst, MAKEINTRESOURCEW(ID_DWL_DIALOG), 0, installer_proc);
607 heap_free(url);
608 url = NULL;
609 return TRUE;