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.5"
57 #define ARCH_STRING "x86"
58 #define GECKO_SHA "07b2bc74d03c885bb39124a7641715314cd3ae71"
59 #elif defined(__x86_64__)
60 #define ARCH_STRING "x86_64"
61 #define GECKO_SHA "80a3b36c30bb79a11889879392fdc1fcda9ca165"
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 /* SHA definitions are copied from advapi32. They aren't available in headers. */
86 void WINAPI A_SHAInit(PSHA_CTX);
87 void WINAPI A_SHAUpdate(PSHA_CTX,const unsigned char*,UINT);
88 void WINAPI A_SHAFinal(PSHA_CTX,PULONG);
90 static BOOL sha_check(const WCHAR *file_name)
92 const unsigned char *file_map;
95 char buf[2*sizeof(sha)+1];
99 file = CreateFileW(file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
100 if(file == INVALID_HANDLE_VALUE)
103 size = GetFileSize(file, NULL);
105 map = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
110 file_map = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
116 A_SHAUpdate(&ctx, file_map, size);
117 A_SHAFinal(&ctx, sha);
119 UnmapViewOfFile(file_map);
121 for(i=0; i < sizeof(sha); i++)
122 sprintf(buf + i*2, "%02x", *((unsigned char*)sha+i));
124 if(strcmp(buf, GECKO_SHA)) {
127 WARN("Got %s, expected %s\n", buf, GECKO_SHA);
129 if(LoadStringW(hInst, IDS_INVALID_SHA, message, sizeof(message)/sizeof(WCHAR)))
130 MessageBoxW(NULL, message, NULL, MB_ICONERROR);
138 static void set_status(DWORD id)
140 HWND status = GetDlgItem(install_dialog, ID_DWL_STATUS);
143 LoadStringW(hInst, id, buf, sizeof(buf)/sizeof(WCHAR));
144 SendMessageW(status, WM_SETTEXT, 0, (LPARAM)buf);
147 static BOOL install_file(const WCHAR *file_name)
151 res = MsiInstallProductW(file_name, NULL);
152 if(res != ERROR_SUCCESS) {
153 ERR("MsiInstallProduct failed: %u\n", res);
160 static BOOL install_from_unix_file(const char *file_name)
162 LPWSTR dos_file_name;
166 static WCHAR * (CDECL *wine_get_dos_file_name)(const char*);
167 static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
169 fd = open(file_name, O_RDONLY);
171 TRACE("%s not found\n", debugstr_a(file_name));
177 if(!wine_get_dos_file_name)
178 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleW(kernel32W), "wine_get_dos_file_name");
180 if(wine_get_dos_file_name) { /* Wine UNIX mode */
181 dos_file_name = wine_get_dos_file_name(file_name);
183 ERR("Could not get dos file name of %s\n", debugstr_a(file_name));
186 } else { /* Windows mode */
188 WARN("Could not get wine_get_dos_file_name function, calling install_cab directly.\n");
189 res = MultiByteToWideChar( CP_ACP, 0, file_name, -1, 0, 0);
190 dos_file_name = heap_alloc (res*sizeof(WCHAR));
191 MultiByteToWideChar( CP_ACP, 0, file_name, -1, dos_file_name, res);
194 ret = install_file(dos_file_name);
196 heap_free(dos_file_name);
200 static BOOL install_from_registered_dir(void)
204 DWORD res, type, size = MAX_PATH;
207 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
208 res = RegOpenKeyW(HKEY_CURRENT_USER, mshtml_keyW, &hkey);
209 if(res != ERROR_SUCCESS)
212 file_name = heap_alloc(size+sizeof(GECKO_FILE_NAME));
213 res = RegGetValueA(hkey, NULL, "GeckoCabDir", RRF_RT_ANY, &type, (PBYTE)file_name, &size);
214 if(res == ERROR_MORE_DATA) {
215 file_name = heap_realloc(file_name, size+sizeof(GECKO_FILE_NAME));
216 res = RegGetValueA(hkey, NULL, "GeckoCabDir", RRF_RT_ANY, &type, (PBYTE)file_name, &size);
219 if(res != ERROR_SUCCESS || (type != REG_SZ && type != REG_EXPAND_SZ)) {
220 heap_free(file_name);
224 strcat(file_name, GECKO_FILE_NAME);
226 TRACE("Trying %s\n", debugstr_a(file_name));
228 ret = install_from_unix_file(file_name);
230 heap_free(file_name);
234 static BOOL install_from_default_dir(void)
236 const char *data_dir, *subdir;
241 if((data_dir = wine_get_data_dir()))
243 else if((data_dir = wine_get_build_dir()))
244 subdir = "/../gecko/";
248 len = strlen(data_dir);
249 len2 = strlen(subdir);
251 file_name = heap_alloc(len+len2+sizeof(GECKO_FILE_NAME));
252 memcpy(file_name, data_dir, len);
253 memcpy(file_name+len, subdir, len2);
254 memcpy(file_name+len+len2, GECKO_FILE_NAME, sizeof(GECKO_FILE_NAME));
256 ret = install_from_unix_file(file_name);
258 heap_free(file_name);
261 ret = install_from_unix_file(INSTALL_DATADIR "/wine/gecko/" GECKO_FILE_NAME);
262 if (!ret && strcmp(INSTALL_DATADIR, "/usr/share"))
263 ret = install_from_unix_file("/usr/share/wine/gecko/" GECKO_FILE_NAME);
267 static HRESULT WINAPI InstallCallback_QueryInterface(IBindStatusCallback *iface,
268 REFIID riid, void **ppv)
270 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IBindStatusCallback, riid)) {
278 static ULONG WINAPI InstallCallback_AddRef(IBindStatusCallback *iface)
283 static ULONG WINAPI InstallCallback_Release(IBindStatusCallback *iface)
288 static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
289 DWORD dwReserved, IBinding *pib)
291 set_status(IDS_DOWNLOADING);
295 static HRESULT WINAPI InstallCallback_GetPriority(IBindStatusCallback *iface,
301 static HRESULT WINAPI InstallCallback_OnLowResource(IBindStatusCallback *iface,
307 static HRESULT WINAPI InstallCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
308 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
310 HWND progress = GetDlgItem(install_dialog, ID_DWL_PROGRESS);
313 SendMessageW(progress, PBM_SETRANGE32, 0, ulProgressMax);
315 SendMessageW(progress, PBM_SETPOS, ulProgress, 0);
320 static HRESULT WINAPI InstallCallback_OnStopBinding(IBindStatusCallback *iface,
321 HRESULT hresult, LPCWSTR szError)
323 if(FAILED(hresult)) {
324 ERR("Binding failed %08x\n", hresult);
328 set_status(IDS_INSTALLING);
332 static HRESULT WINAPI InstallCallback_GetBindInfo(IBindStatusCallback *iface,
333 DWORD* grfBINDF, BINDINFO* pbindinfo)
340 static HRESULT WINAPI InstallCallback_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
341 DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
347 static HRESULT WINAPI InstallCallback_OnObjectAvailable(IBindStatusCallback *iface,
348 REFIID riid, IUnknown* punk)
354 static const IBindStatusCallbackVtbl InstallCallbackVtbl = {
355 InstallCallback_QueryInterface,
356 InstallCallback_AddRef,
357 InstallCallback_Release,
358 InstallCallback_OnStartBinding,
359 InstallCallback_GetPriority,
360 InstallCallback_OnLowResource,
361 InstallCallback_OnProgress,
362 InstallCallback_OnStopBinding,
363 InstallCallback_GetBindInfo,
364 InstallCallback_OnDataAvailable,
365 InstallCallback_OnObjectAvailable
368 static IBindStatusCallback InstallCallback = { &InstallCallbackVtbl };
370 static LPWSTR get_url(void)
374 DWORD size = INTERNET_MAX_URL_LENGTH*sizeof(WCHAR);
378 static const WCHAR wszGeckoUrl[] = {'G','e','c','k','o','U','r','l',0};
379 static const WCHAR httpW[] = {'h','t','t','p'};
380 static const WCHAR arch_formatW[] = {'?','a','r','c','h','='};
381 static const WCHAR v_formatW[] = {'&','v','='};
383 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
384 res = RegOpenKeyW(HKEY_CURRENT_USER, mshtml_keyW, &hkey);
385 if(res != ERROR_SUCCESS)
388 url = heap_alloc(size);
389 returned_size = size;
391 res = RegQueryValueExW(hkey, wszGeckoUrl, NULL, &type, (LPBYTE)url, &returned_size);
393 if(res != ERROR_SUCCESS || type != REG_SZ) {
398 if(returned_size > sizeof(httpW) && !memcmp(url, httpW, sizeof(httpW))) {
402 memcpy(url+len, arch_formatW, sizeof(arch_formatW));
403 len += sizeof(arch_formatW)/sizeof(WCHAR);
404 len += MultiByteToWideChar(CP_ACP, 0, ARCH_STRING, sizeof(ARCH_STRING), url+len, size/sizeof(WCHAR)-len)-1;
405 memcpy(url+len, v_formatW, sizeof(v_formatW));
406 len += sizeof(v_formatW)/sizeof(WCHAR);
407 MultiByteToWideChar(CP_ACP, 0, GECKO_VERSION, -1, url+len, size/sizeof(WCHAR)-len);
410 TRACE("Got URL %s\n", debugstr_w(url));
414 static DWORD WINAPI download_proc(PVOID arg)
416 WCHAR tmp_dir[MAX_PATH], tmp_file[MAX_PATH];
419 GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);
420 GetTempFileNameW(tmp_dir, NULL, 0, tmp_file);
422 TRACE("using temp file %s\n", debugstr_w(tmp_file));
424 hres = URLDownloadToFileW(NULL, url, tmp_file, 0, &InstallCallback);
426 ERR("URLDownloadToFile failed: %08x\n", hres);
430 if(sha_check(tmp_file))
431 install_file(tmp_file);
432 DeleteFileW(tmp_file);
433 EndDialog(install_dialog, 0);
437 static void run_winebrowser(const WCHAR *url)
439 PROCESS_INFORMATION pi;
446 static const WCHAR winebrowserW[] = {'\\','w','i','n','e','b','r','o','w','s','e','r','.','e','x','e',0};
448 url_len = strlenW(url);
450 len = GetSystemDirectoryW(app, MAX_PATH-sizeof(winebrowserW)/sizeof(WCHAR));
451 memcpy(app+len, winebrowserW, sizeof(winebrowserW));
452 len += sizeof(winebrowserW)/sizeof(WCHAR) -1;
454 args = heap_alloc((len+1+url_len)*sizeof(WCHAR));
458 memcpy(args, app, len*sizeof(WCHAR));
460 memcpy(args+len, url, (url_len+1) * sizeof(WCHAR));
462 TRACE("starting %s\n", debugstr_w(args));
464 memset(&si, 0, sizeof(si));
466 ret = CreateProcessW(app, args, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
469 CloseHandle(pi.hThread);
470 CloseHandle(pi.hProcess);
474 static INT_PTR CALLBACK installer_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
478 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_HIDE);
479 install_dialog = hwnd;
483 switch (((NMHDR *)lParam)->code)
487 if (wParam == ID_DWL_STATUS)
488 run_winebrowser(((NMLINK*)lParam)->item.szUrl);
500 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_SHOW);
501 EnableWindow(GetDlgItem(hwnd, ID_DWL_INSTALL), 0);
502 EnableWindow(GetDlgItem(hwnd, IDCANCEL), 0); /* FIXME */
503 CloseHandle( CreateThread(NULL, 0, download_proc, NULL, 0, NULL));
511 BOOL install_wine_gecko(void)
517 * Try to find Gecko .cab file in following order:
518 * - directory stored in GeckoCabDir value of HKCU/Wine/Software/MSHTML key
520 * - $INSTALL_DATADIR/wine/gecko/
521 * - /usr/share/wine/gecko/
522 * - download from URL stored in GeckoUrl value of HKCU/Wine/Software/MSHTML key
524 if(!install_from_registered_dir()
525 && !install_from_default_dir()
526 && (url = get_url()))
527 DialogBoxW(hInst, MAKEINTRESOURCEW(ID_DWL_DIALOG), 0, installer_proc);