2 * Copyright 2006-2007 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
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
41 #include "wine/debug.h"
42 #include "wine/unicode.h"
43 #include "wine/library.h"
45 #include "mshtml_private.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
50 #define GECKO_FILE_NAME "wine_gecko-" GECKO_VERSION "-x86.cab"
52 static const WCHAR mshtml_keyW[] =
53 {'S','o','f','t','w','a','r','e',
55 '\\','M','S','H','T','M','L',0};
57 static HWND install_dialog = NULL;
58 static LPWSTR tmp_file_name = NULL;
59 static HANDLE tmp_file = INVALID_HANDLE_VALUE;
60 static LPWSTR url = NULL;
62 static void clean_up(void)
64 if(tmp_file != INVALID_HANDLE_VALUE)
65 CloseHandle(tmp_file);
68 DeleteFileW(tmp_file_name);
69 heap_free(tmp_file_name);
73 if(tmp_file != INVALID_HANDLE_VALUE) {
74 CloseHandle(tmp_file);
75 tmp_file = INVALID_HANDLE_VALUE;
79 EndDialog(install_dialog, 0);
82 static void set_status(DWORD id)
84 HWND status = GetDlgItem(install_dialog, ID_DWL_STATUS);
87 LoadStringW(hInst, id, buf, sizeof(buf)/sizeof(WCHAR));
88 SendMessageW(status, WM_SETTEXT, 0, (LPARAM)buf);
91 static void set_registry(LPCSTR install_dir)
93 WCHAR mshtml_key[100];
98 static const WCHAR wszGeckoPath[] = {'G','e','c','k','o','P','a','t','h',0};
99 static const WCHAR wszWineGecko[] = {'w','i','n','e','_','g','e','c','k','o',0};
101 memcpy(mshtml_key, mshtml_keyW, sizeof(mshtml_keyW));
102 mshtml_key[sizeof(mshtml_keyW)/sizeof(WCHAR)-1] = '\\';
103 MultiByteToWideChar(CP_ACP, 0, GECKO_VERSION, sizeof(GECKO_VERSION),
104 mshtml_key+sizeof(mshtml_keyW)/sizeof(WCHAR),
105 (sizeof(mshtml_key)-sizeof(mshtml_keyW))/sizeof(WCHAR));
107 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML\<version> */
108 res = RegCreateKeyW(HKEY_CURRENT_USER, mshtml_key, &hkey);
109 if(res != ERROR_SUCCESS) {
110 ERR("Faild to create MSHTML key: %d\n", res);
114 len = MultiByteToWideChar(CP_ACP, 0, install_dir, -1, NULL, 0)-1;
115 gecko_path = heap_alloc((len+1)*sizeof(WCHAR)+sizeof(wszWineGecko));
116 MultiByteToWideChar(CP_ACP, 0, install_dir, -1, gecko_path, len+1);
118 if (len && gecko_path[len-1] != '\\')
119 gecko_path[len++] = '\\';
121 memcpy(gecko_path+len, wszWineGecko, sizeof(wszWineGecko));
123 res = RegSetValueExW(hkey, wszGeckoPath, 0, REG_SZ, (LPVOID)gecko_path,
124 len*sizeof(WCHAR)+sizeof(wszWineGecko));
125 heap_free(gecko_path);
127 if(res != ERROR_SUCCESS)
128 ERR("Failed to set GeckoPath value: %08x\n", res);
131 static BOOL install_cab(LPCWSTR file_name)
134 char install_dir[MAX_PATH];
135 HRESULT (WINAPI *pExtractFilesA)(LPCSTR,LPCSTR,DWORD,LPCSTR,LPVOID,DWORD);
140 static const WCHAR wszAdvpack[] = {'a','d','v','p','a','c','k','.','d','l','l',0};
142 TRACE("(%s)\n", debugstr_w(file_name));
144 GetSystemDirectoryA(install_dir, sizeof(install_dir));
145 strcat(install_dir, "\\gecko\\");
146 res = CreateDirectoryA(install_dir, NULL);
147 if(!res && GetLastError() != ERROR_ALREADY_EXISTS) {
148 ERR("Could not create directory: %08u\n", GetLastError());
152 strcat(install_dir, GECKO_VERSION);
153 res = CreateDirectoryA(install_dir, NULL);
154 if(!res && GetLastError() != ERROR_ALREADY_EXISTS) {
155 ERR("Could not create directory: %08u\n", GetLastError());
159 advpack = LoadLibraryW(wszAdvpack);
160 pExtractFilesA = (void *)GetProcAddress(advpack, "ExtractFiles");
162 /* FIXME: Use unicode version (not yet implemented) */
163 file_name_a = heap_strdupWtoA(file_name);
164 hres = pExtractFilesA(file_name_a, install_dir, 0, NULL, NULL, 0);
165 FreeLibrary(advpack);
166 heap_free(file_name_a);
168 ERR("Could not extract package: %08x\n", hres);
173 set_registry(install_dir);
179 static BOOL install_from_unix_file(const char *file_name)
181 LPWSTR dos_file_name;
185 static WCHAR *(*wine_get_dos_file_name)(const char*);
186 static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
188 fd = open(file_name, O_RDONLY);
190 TRACE("%s not found\n", debugstr_a(file_name));
196 if(!wine_get_dos_file_name)
197 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleW(kernel32W), "wine_get_dos_file_name");
199 if(wine_get_dos_file_name) { /* Wine UNIX mode */
200 dos_file_name = wine_get_dos_file_name(file_name);
202 ERR("Could not get dos file name of %s\n", debugstr_a(file_name));
205 } else { /* Windows mode */
207 WARN("Could not get wine_get_dos_file_name function, calling install_cab directly.\n");
208 res = MultiByteToWideChar( CP_ACP, 0, file_name, -1, 0, 0);
209 dos_file_name = heap_alloc (res*sizeof(WCHAR));
210 MultiByteToWideChar( CP_ACP, 0, file_name, -1, dos_file_name, res);
213 ret = install_cab(dos_file_name);
215 heap_free(dos_file_name);
219 static BOOL install_from_registered_dir(void)
223 DWORD res, type, size = MAX_PATH;
226 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
227 res = RegOpenKeyW(HKEY_CURRENT_USER, mshtml_keyW, &hkey);
228 if(res != ERROR_SUCCESS)
231 file_name = heap_alloc(size+sizeof(GECKO_FILE_NAME));
232 res = RegQueryValueExA(hkey, "GeckoCabDir", NULL, &type, (PBYTE)file_name, &size);
233 if(res == ERROR_MORE_DATA) {
234 file_name = heap_realloc(file_name, size+sizeof(GECKO_FILE_NAME));
235 res = RegQueryValueExA(hkey, "GeckoCabDir", NULL, &type, (PBYTE)file_name, &size);
238 if(res != ERROR_SUCCESS || type != REG_SZ) {
239 heap_free(file_name);
243 strcat(file_name, GECKO_FILE_NAME);
245 TRACE("Trying %s\n", debugstr_a(file_name));
247 ret = install_from_unix_file(file_name);
249 heap_free(file_name);
253 static BOOL install_from_default_dir(void)
255 const char *data_dir, *subdir;
260 if((data_dir = wine_get_data_dir()))
262 else if((data_dir = wine_get_build_dir()))
263 subdir = "/../gecko/";
267 len = strlen(data_dir);
268 len2 = strlen(subdir);
270 file_name = heap_alloc(len+len2+sizeof(GECKO_FILE_NAME));
271 memcpy(file_name, data_dir, len);
272 memcpy(file_name+len, subdir, len2);
273 memcpy(file_name+len+len2, GECKO_FILE_NAME, sizeof(GECKO_FILE_NAME));
275 ret = install_from_unix_file(file_name);
277 heap_free(file_name);
281 static HRESULT WINAPI InstallCallback_QueryInterface(IBindStatusCallback *iface,
282 REFIID riid, void **ppv)
284 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IBindStatusCallback, riid)) {
292 static ULONG WINAPI InstallCallback_AddRef(IBindStatusCallback *iface)
297 static ULONG WINAPI InstallCallback_Release(IBindStatusCallback *iface)
302 static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
303 DWORD dwReserved, IBinding *pib)
305 WCHAR tmp_dir[MAX_PATH];
307 set_status(IDS_DOWNLOADING);
309 GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);
311 tmp_file_name = heap_alloc(MAX_PATH*sizeof(WCHAR));
312 GetTempFileNameW(tmp_dir, NULL, 0, tmp_file_name);
314 TRACE("creating temp file %s\n", debugstr_w(tmp_file_name));
316 tmp_file = CreateFileW(tmp_file_name, GENERIC_WRITE, 0, NULL,
317 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
319 if(tmp_file == INVALID_HANDLE_VALUE) {
320 ERR("Could not create file: %d\n", GetLastError());
328 static HRESULT WINAPI InstallCallback_GetPriority(IBindStatusCallback *iface,
334 static HRESULT WINAPI InstallCallback_OnLowResource(IBindStatusCallback *iface,
340 static HRESULT WINAPI InstallCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
341 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
343 HWND progress = GetDlgItem(install_dialog, ID_DWL_PROGRESS);
346 SendMessageW(progress, PBM_SETRANGE32, 0, ulProgressMax);
348 SendMessageW(progress, PBM_SETPOS, ulProgress, 0);
353 static HRESULT WINAPI InstallCallback_OnStopBinding(IBindStatusCallback *iface,
354 HRESULT hresult, LPCWSTR szError)
356 if(FAILED(hresult)) {
357 ERR("Binding failed %08x\n", hresult);
362 CloseHandle(tmp_file);
363 tmp_file = INVALID_HANDLE_VALUE;
365 set_status(IDS_INSTALLING);
367 install_cab(tmp_file_name);
372 static HRESULT WINAPI InstallCallback_GetBindInfo(IBindStatusCallback *iface,
373 DWORD* grfBINDF, BINDINFO* pbindinfo)
380 static HRESULT WINAPI InstallCallback_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
381 DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
383 IStream *str = pstgmed->u.pstm;
392 hres = IStream_Read(str, buf, sizeof(buf), &size);
394 WriteFile(tmp_file, buf, size, &written, NULL);
395 }while(hres == S_OK);
400 static HRESULT WINAPI InstallCallback_OnObjectAvailable(IBindStatusCallback *iface,
401 REFIID riid, IUnknown* punk)
407 static const IBindStatusCallbackVtbl InstallCallbackVtbl = {
408 InstallCallback_QueryInterface,
409 InstallCallback_AddRef,
410 InstallCallback_Release,
411 InstallCallback_OnStartBinding,
412 InstallCallback_GetPriority,
413 InstallCallback_OnLowResource,
414 InstallCallback_OnProgress,
415 InstallCallback_OnStopBinding,
416 InstallCallback_GetBindInfo,
417 InstallCallback_OnDataAvailable,
418 InstallCallback_OnObjectAvailable
421 static IBindStatusCallback InstallCallback = { &InstallCallbackVtbl };
423 static LPWSTR get_url(void)
427 DWORD size = INTERNET_MAX_URL_LENGTH*sizeof(WCHAR);
431 static const WCHAR wszGeckoUrl[] = {'G','e','c','k','o','U','r','l',0};
432 static const WCHAR httpW[] = {'h','t','t','p'};
433 static const WCHAR v_formatW[] = {'?','a','r','c','h','=','x','8','6','&','v','=',0};
435 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
436 res = RegOpenKeyW(HKEY_CURRENT_USER, mshtml_keyW, &hkey);
437 if(res != ERROR_SUCCESS)
440 url = heap_alloc(size);
441 returned_size = size;
443 res = RegQueryValueExW(hkey, wszGeckoUrl, NULL, &type, (LPBYTE)url, &returned_size);
445 if(res != ERROR_SUCCESS || type != REG_SZ) {
450 if(returned_size > sizeof(httpW) && !memcmp(url, httpW, sizeof(httpW))) {
451 strcatW(url, v_formatW);
452 MultiByteToWideChar(CP_ACP, 0, GECKO_VERSION, -1, url+strlenW(url), size/sizeof(WCHAR)-strlenW(url));
455 TRACE("Got URL %s\n", debugstr_w(url));
459 static DWORD WINAPI download_proc(PVOID arg)
466 CreateURLMoniker(NULL, url, &mon);
470 CreateAsyncBindCtx(0, &InstallCallback, 0, &bctx);
472 hres = IMoniker_BindToStorage(mon, bctx, NULL, &IID_IStream, (void**)&str);
473 IBindCtx_Release(bctx);
475 ERR("BindToStorage failed: %08x\n", hres);
480 IStream_Release(str);
485 static INT_PTR CALLBACK installer_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
489 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_HIDE);
490 install_dialog = hwnd;
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 CreateThread(NULL, 0, download_proc, NULL, 0, NULL);
511 BOOL install_wine_gecko(BOOL silent)
515 SetLastError(ERROR_SUCCESS);
516 hsem = CreateSemaphoreA( NULL, 0, 1, "mshtml_install_semaphore");
518 if(GetLastError() == ERROR_ALREADY_EXISTS) {
519 WaitForSingleObject(hsem, INFINITE);
522 * Try to find Gecko .cab file in following order:
523 * - directory stored in GeckoCabDir value of HKCU/Software/MSHTML key
525 * - download from URL stored in GeckoUrl value of HKCU/Software/MSHTML key
527 if(!install_from_registered_dir()
528 && !install_from_default_dir()
529 && !silent && (url = get_url()))
530 DialogBoxW(hInst, MAKEINTRESOURCEW(ID_DWL_DIALOG), 0, installer_proc);
533 ReleaseSemaphore(hsem, 1, NULL);