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
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
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.3"
57 #define ARCH_STRING "x86"
58 #define GECKO_SHA "acc6a5bc15ebb3574e00f8ef4f23912239658b41"
59 #elif defined(__x86_64__)
60 #define ARCH_STRING "x86_64"
61 #define GECKO_SHA "5bcf29c48677dffa7a9112d481f7f5474cd255d4"
63 #define ARCH_STRING ""
64 #define GECKO_SHA "???"
67 #define GECKO_FILE_NAME "wine_gecko-" GECKO_VERSION "-" ARCH_STRING ".msi"
69 static const WCHAR mshtml_keyW[] =
70 {'S','o','f','t','w','a','r','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)
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);
90 /* SHA definitions are copied from advapi32. They aren't available in headers. */
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;
108 char buf[2*sizeof(sha)+1];
112 file = CreateFileW(file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
113 if(file == INVALID_HANDLE_VALUE)
116 size = GetFileSize(file, NULL);
118 map = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
123 file_map = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
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)) {
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);
151 static void set_status(DWORD id)
153 HWND status = GetDlgItem(install_dialog, ID_DWL_STATUS);
156 LoadStringW(hInst, id, buf, sizeof(buf)/sizeof(WCHAR));
157 SendMessageW(status, WM_SETTEXT, 0, (LPARAM)buf);
160 static BOOL install_file(const WCHAR *file_name)
164 res = MsiInstallProductW(file_name, NULL);
165 if(res != ERROR_SUCCESS) {
166 ERR("MsiInstallProduct failed: %u\n", res);
173 static BOOL install_from_unix_file(const char *file_name)
175 LPWSTR dos_file_name;
179 static WCHAR * (CDECL *wine_get_dos_file_name)(const char*);
180 static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
182 fd = open(file_name, O_RDONLY);
184 TRACE("%s not found\n", debugstr_a(file_name));
190 if(!wine_get_dos_file_name)
191 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleW(kernel32W), "wine_get_dos_file_name");
193 if(wine_get_dos_file_name) { /* Wine UNIX mode */
194 dos_file_name = wine_get_dos_file_name(file_name);
196 ERR("Could not get dos file name of %s\n", debugstr_a(file_name));
199 } else { /* Windows mode */
201 WARN("Could not get wine_get_dos_file_name function, calling install_cab directly.\n");
202 res = MultiByteToWideChar( CP_ACP, 0, file_name, -1, 0, 0);
203 dos_file_name = heap_alloc (res*sizeof(WCHAR));
204 MultiByteToWideChar( CP_ACP, 0, file_name, -1, dos_file_name, res);
207 ret = install_file(dos_file_name);
209 heap_free(dos_file_name);
213 static BOOL install_from_registered_dir(void)
217 DWORD res, type, size = MAX_PATH;
220 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
221 res = RegOpenKeyW(HKEY_CURRENT_USER, mshtml_keyW, &hkey);
222 if(res != ERROR_SUCCESS)
225 file_name = heap_alloc(size+sizeof(GECKO_FILE_NAME));
226 res = RegGetValueA(hkey, NULL, "GeckoCabDir", RRF_RT_ANY, &type, (PBYTE)file_name, &size);
227 if(res == ERROR_MORE_DATA) {
228 file_name = heap_realloc(file_name, size+sizeof(GECKO_FILE_NAME));
229 res = RegGetValueA(hkey, NULL, "GeckoCabDir", RRF_RT_ANY, &type, (PBYTE)file_name, &size);
232 if(res != ERROR_SUCCESS || (type != REG_SZ && type != REG_EXPAND_SZ)) {
233 heap_free(file_name);
237 strcat(file_name, GECKO_FILE_NAME);
239 TRACE("Trying %s\n", debugstr_a(file_name));
241 ret = install_from_unix_file(file_name);
243 heap_free(file_name);
247 static BOOL install_from_default_dir(void)
249 const char *data_dir, *subdir;
254 if((data_dir = wine_get_data_dir()))
256 else if((data_dir = wine_get_build_dir()))
257 subdir = "/../gecko/";
261 len = strlen(data_dir);
262 len2 = strlen(subdir);
264 file_name = heap_alloc(len+len2+sizeof(GECKO_FILE_NAME));
265 memcpy(file_name, data_dir, len);
266 memcpy(file_name+len, subdir, len2);
267 memcpy(file_name+len+len2, GECKO_FILE_NAME, sizeof(GECKO_FILE_NAME));
269 ret = install_from_unix_file(file_name);
271 heap_free(file_name);
274 ret = install_from_unix_file(INSTALL_DATADIR "/wine/gecko/" GECKO_FILE_NAME);
275 if (!ret && strcmp(INSTALL_DATADIR, "/usr/share"))
276 ret = install_from_unix_file("/usr/share/wine/gecko/" GECKO_FILE_NAME);
280 static HRESULT WINAPI InstallCallback_QueryInterface(IBindStatusCallback *iface,
281 REFIID riid, void **ppv)
283 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IBindStatusCallback, riid)) {
291 static ULONG WINAPI InstallCallback_AddRef(IBindStatusCallback *iface)
296 static ULONG WINAPI InstallCallback_Release(IBindStatusCallback *iface)
301 static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
302 DWORD dwReserved, IBinding *pib)
304 set_status(IDS_DOWNLOADING);
308 static HRESULT WINAPI InstallCallback_GetPriority(IBindStatusCallback *iface,
314 static HRESULT WINAPI InstallCallback_OnLowResource(IBindStatusCallback *iface,
320 static HRESULT WINAPI InstallCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
321 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
323 HWND progress = GetDlgItem(install_dialog, ID_DWL_PROGRESS);
326 SendMessageW(progress, PBM_SETRANGE32, 0, ulProgressMax);
328 SendMessageW(progress, PBM_SETPOS, ulProgress, 0);
333 static HRESULT WINAPI InstallCallback_OnStopBinding(IBindStatusCallback *iface,
334 HRESULT hresult, LPCWSTR szError)
336 if(FAILED(hresult)) {
337 ERR("Binding failed %08x\n", hresult);
341 set_status(IDS_INSTALLING);
345 static HRESULT WINAPI InstallCallback_GetBindInfo(IBindStatusCallback *iface,
346 DWORD* grfBINDF, BINDINFO* pbindinfo)
353 static HRESULT WINAPI InstallCallback_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
354 DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
360 static HRESULT WINAPI InstallCallback_OnObjectAvailable(IBindStatusCallback *iface,
361 REFIID riid, IUnknown* punk)
367 static const IBindStatusCallbackVtbl InstallCallbackVtbl = {
368 InstallCallback_QueryInterface,
369 InstallCallback_AddRef,
370 InstallCallback_Release,
371 InstallCallback_OnStartBinding,
372 InstallCallback_GetPriority,
373 InstallCallback_OnLowResource,
374 InstallCallback_OnProgress,
375 InstallCallback_OnStopBinding,
376 InstallCallback_GetBindInfo,
377 InstallCallback_OnDataAvailable,
378 InstallCallback_OnObjectAvailable
381 static IBindStatusCallback InstallCallback = { &InstallCallbackVtbl };
383 static LPWSTR get_url(void)
387 DWORD size = INTERNET_MAX_URL_LENGTH*sizeof(WCHAR);
391 static const WCHAR wszGeckoUrl[] = {'G','e','c','k','o','U','r','l',0};
392 static const WCHAR httpW[] = {'h','t','t','p'};
393 static const WCHAR arch_formatW[] = {'?','a','r','c','h','='};
394 static const WCHAR v_formatW[] = {'&','v','='};
396 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
397 res = RegOpenKeyW(HKEY_CURRENT_USER, mshtml_keyW, &hkey);
398 if(res != ERROR_SUCCESS)
401 url = heap_alloc(size);
402 returned_size = size;
404 res = RegQueryValueExW(hkey, wszGeckoUrl, NULL, &type, (LPBYTE)url, &returned_size);
406 if(res != ERROR_SUCCESS || type != REG_SZ) {
411 if(returned_size > sizeof(httpW) && !memcmp(url, httpW, sizeof(httpW))) {
415 memcpy(url+len, arch_formatW, sizeof(arch_formatW));
416 len += sizeof(arch_formatW)/sizeof(WCHAR);
417 len += MultiByteToWideChar(CP_ACP, 0, ARCH_STRING, sizeof(ARCH_STRING), url+len, size/sizeof(WCHAR)-len)-1;
418 memcpy(url+len, v_formatW, sizeof(v_formatW));
419 len += sizeof(v_formatW)/sizeof(WCHAR);
420 MultiByteToWideChar(CP_ACP, 0, GECKO_VERSION, -1, url+len, size/sizeof(WCHAR)-len);
423 TRACE("Got URL %s\n", debugstr_w(url));
427 static DWORD WINAPI download_proc(PVOID arg)
429 WCHAR tmp_dir[MAX_PATH], tmp_file[MAX_PATH];
432 GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);
433 GetTempFileNameW(tmp_dir, NULL, 0, tmp_file);
435 TRACE("using temp file %s\n", debugstr_w(tmp_file));
437 hres = URLDownloadToFileW(NULL, url, tmp_file, 0, &InstallCallback);
439 ERR("URLDownloadToFile failed: %08x\n", hres);
443 if(sha_check(tmp_file))
444 install_file(tmp_file);
445 DeleteFileW(tmp_file);
446 EndDialog(install_dialog, 0);
450 static INT_PTR CALLBACK installer_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
454 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_HIDE);
455 install_dialog = hwnd;
465 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_SHOW);
466 EnableWindow(GetDlgItem(hwnd, ID_DWL_INSTALL), 0);
467 EnableWindow(GetDlgItem(hwnd, IDCANCEL), 0); /* FIXME */
468 CreateThread(NULL, 0, download_proc, NULL, 0, NULL);
476 BOOL install_wine_gecko(void)
482 * Try to find Gecko .cab file in following order:
483 * - directory stored in GeckoCabDir value of HKCU/Wine/Software/MSHTML key
485 * - $INSTALL_DATADIR/wine/gecko/
486 * - /usr/share/wine/gecko/
487 * - download from URL stored in GeckoUrl value of HKCU/Wine/Software/MSHTML key
489 if(!install_from_registered_dir()
490 && !install_from_default_dir()
491 && (url = get_url()))
492 DialogBoxW(hInst, MAKEINTRESOURCEW(ID_DWL_DIALOG), 0, installer_proc);