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
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
41 #include "wine/library.h"
43 #include "mshtml_private.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
48 #define GECKO_FILE_NAME "wine_gecko-" GECKO_VERSION ".cab"
50 static const WCHAR mshtml_keyW[] =
51 {'S','o','f','t','w','a','r','e',
53 '\\','M','S','H','T','M','L',0};
55 static HWND install_dialog = NULL;
56 static LPWSTR tmp_file_name = NULL;
57 static HANDLE tmp_file = INVALID_HANDLE_VALUE;
58 static LPWSTR url = NULL;
60 static void clean_up(void)
62 if(tmp_file != INVALID_HANDLE_VALUE)
63 CloseHandle(tmp_file);
66 DeleteFileW(tmp_file_name);
67 mshtml_free(tmp_file_name);
71 if(tmp_file != INVALID_HANDLE_VALUE) {
72 CloseHandle(tmp_file);
73 tmp_file = INVALID_HANDLE_VALUE;
77 EndDialog(install_dialog, 0);
80 static void set_status(DWORD id)
82 HWND status = GetDlgItem(install_dialog, ID_DWL_STATUS);
85 LoadStringW(hInst, id, buf, sizeof(buf)/sizeof(WCHAR));
86 SendMessageW(status, WM_SETTEXT, 0, (LPARAM)buf);
89 static void set_registry(LPCSTR install_dir)
91 WCHAR mshtml_key[100];
96 static const WCHAR wszGeckoPath[] = {'G','e','c','k','o','P','a','t','h',0};
97 static const WCHAR wszWineGecko[] = {'w','i','n','e','_','g','e','c','k','o',0};
99 memcpy(mshtml_key, mshtml_keyW, sizeof(mshtml_keyW));
100 mshtml_key[sizeof(mshtml_keyW)/sizeof(WCHAR)-1] = '\\';
101 MultiByteToWideChar(CP_ACP, 0, GECKO_VERSION, sizeof(GECKO_VERSION),
102 mshtml_key+sizeof(mshtml_keyW)/sizeof(WCHAR),
103 (sizeof(mshtml_key)-sizeof(mshtml_keyW))/sizeof(WCHAR));
105 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML\<version> */
106 res = RegCreateKeyW(HKEY_CURRENT_USER, mshtml_key, &hkey);
107 if(res != ERROR_SUCCESS) {
108 ERR("Faild to create MSHTML key: %d\n", res);
112 len = MultiByteToWideChar(CP_ACP, 0, install_dir, -1, NULL, 0)-1;
113 gecko_path = mshtml_alloc((len+1)*sizeof(WCHAR)+sizeof(wszWineGecko));
114 MultiByteToWideChar(CP_ACP, 0, install_dir, -1, gecko_path, (len+1)*sizeof(WCHAR));
116 if (len && gecko_path[len-1] != '\\')
117 gecko_path[len++] = '\\';
119 memcpy(gecko_path+len, wszWineGecko, sizeof(wszWineGecko));
121 size = len*sizeof(WCHAR)+sizeof(wszWineGecko);
122 res = RegSetValueExW(hkey, wszGeckoPath, 0, REG_SZ, (LPVOID)gecko_path,
123 len*sizeof(WCHAR)+sizeof(wszWineGecko));
124 mshtml_free(gecko_path);
126 if(res != ERROR_SUCCESS)
127 ERR("Failed to set GeckoPath value: %08x\n", res);
130 static BOOL install_cab(LPCWSTR file_name)
133 char install_dir[MAX_PATH];
134 typeof(ExtractFilesA) *pExtractFilesA;
139 static const WCHAR wszAdvpack[] = {'a','d','v','p','a','c','k','.','d','l','l',0};
141 TRACE("(%s)\n", debugstr_w(file_name));
143 GetWindowsDirectoryA(install_dir, sizeof(install_dir));
144 strcat(install_dir, "\\gecko\\");
145 res = CreateDirectoryA(install_dir, NULL);
146 if(!res && GetLastError() != ERROR_ALREADY_EXISTS) {
147 ERR("Could not create directory: %08u\n", GetLastError());
151 strcat(install_dir, GECKO_VERSION);
152 res = CreateDirectoryA(install_dir, NULL);
153 if(!res && GetLastError() != ERROR_ALREADY_EXISTS) {
154 ERR("Could not create directory: %08u\n", GetLastError());
158 advpack = LoadLibraryW(wszAdvpack);
159 pExtractFilesA = (typeof(ExtractFilesA)*)GetProcAddress(advpack, "ExtractFiles");
161 len = WideCharToMultiByte(CP_ACP, 0, file_name, -1, NULL, 0, NULL, NULL);
162 file_name_a = mshtml_alloc(len);
163 WideCharToMultiByte(CP_ACP, 0, file_name, -1, file_name_a, -1, NULL, NULL);
165 /* FIXME: Use unicode version (not yet implemented) */
166 hres = pExtractFilesA(file_name_a, install_dir, 0, NULL, NULL, 0);
167 FreeLibrary(advpack);
168 mshtml_free(file_name_a);
170 ERR("Could not extract package: %08x\n", hres);
175 set_registry(install_dir);
181 static BOOL install_from_unix_file(const char *file_name)
183 LPWSTR dos_file_name;
187 static WCHAR *(*wine_get_dos_file_name)(const char*);
188 static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
190 fd = open(file_name, O_RDONLY);
192 TRACE("%s not found\n", debugstr_a(file_name));
198 if(!wine_get_dos_file_name) {
199 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleW(kernel32W), "wine_get_dos_file_name");
200 if(!wine_get_dos_file_name) {
201 ERR("Could not get wine_get_dos_file_name function.\n");
206 dos_file_name = wine_get_dos_file_name(file_name);
208 ERR("Could not get dos file name of %s\n", debugstr_a(file_name));
212 ret = install_cab(dos_file_name);
214 mshtml_free(dos_file_name);
218 static BOOL install_from_registered_dir(void)
222 DWORD res, type, size = MAX_PATH;
225 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
226 res = RegOpenKeyW(HKEY_CURRENT_USER, mshtml_keyW, &hkey);
227 if(res != ERROR_SUCCESS)
230 file_name = mshtml_alloc(size+sizeof(GECKO_FILE_NAME));
231 res = RegQueryValueExA(hkey, "GeckoCabDir", NULL, &type, (PBYTE)file_name, &size);
232 if(res == ERROR_MORE_DATA) {
233 file_name = mshtml_realloc(file_name, size+sizeof(GECKO_FILE_NAME));
234 res = RegQueryValueExA(hkey, "GeckoCabDir", NULL, &type, (PBYTE)file_name, &size);
237 if(res != ERROR_SUCCESS || type != REG_SZ)
240 strcat(file_name, GECKO_FILE_NAME);
242 TRACE("Trying %s\n", debugstr_a(file_name));
244 ret = install_from_unix_file(file_name);
246 mshtml_free(file_name);
250 static BOOL install_from_default_dir(void)
252 const char *data_dir;
257 static const char gecko_dir[] = "/gecko/";
259 data_dir = wine_get_data_dir();
260 if(!data_dir) return FALSE;
262 len = strlen(data_dir);
264 file_name = mshtml_alloc(len+sizeof(gecko_dir)+sizeof(GECKO_FILE_NAME));
265 memcpy(file_name, data_dir, len);
266 memcpy(file_name+len, gecko_dir, sizeof(gecko_dir));
267 memcpy(file_name+len+sizeof(gecko_dir)-1, GECKO_FILE_NAME, sizeof(GECKO_FILE_NAME));
269 ret = install_from_unix_file(file_name);
271 mshtml_free(file_name);
275 static HRESULT WINAPI InstallCallback_QueryInterface(IBindStatusCallback *iface,
276 REFIID riid, void **ppv)
278 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IBindStatusCallback, riid)) {
286 static ULONG WINAPI InstallCallback_AddRef(IBindStatusCallback *iface)
291 static ULONG WINAPI InstallCallback_Release(IBindStatusCallback *iface)
296 static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
297 DWORD dwReserved, IBinding *pib)
299 WCHAR tmp_dir[MAX_PATH];
301 set_status(IDS_DOWNLOADING);
303 GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);
305 tmp_file_name = mshtml_alloc(MAX_PATH*sizeof(WCHAR));
306 GetTempFileNameW(tmp_dir, NULL, 0, tmp_file_name);
308 TRACE("creating temp file %s\n", debugstr_w(tmp_file_name));
310 tmp_file = CreateFileW(tmp_file_name, GENERIC_WRITE, 0, NULL,
311 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
313 if(tmp_file == INVALID_HANDLE_VALUE) {
314 ERR("Could not create file: %d\n", GetLastError());
322 static HRESULT WINAPI InstallCallback_GetPriority(IBindStatusCallback *iface,
328 static HRESULT WINAPI InstallCallback_OnLowResource(IBindStatusCallback *iface,
334 static HRESULT WINAPI InstallCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
335 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
337 HWND progress = GetDlgItem(install_dialog, ID_DWL_PROGRESS);
340 SendMessageW(progress, PBM_SETRANGE32, 0, ulProgressMax);
342 SendMessageW(progress, PBM_SETPOS, ulProgress, 0);
347 static HRESULT WINAPI InstallCallback_OnStopBinding(IBindStatusCallback *iface,
348 HRESULT hresult, LPCWSTR szError)
350 if(FAILED(hresult)) {
351 ERR("Binding failed %08x\n", hresult);
356 CloseHandle(tmp_file);
357 tmp_file = INVALID_HANDLE_VALUE;
359 set_status(IDS_INSTALLING);
361 install_cab(tmp_file_name);
366 static HRESULT WINAPI InstallCallback_GetBindInfo(IBindStatusCallback *iface,
367 DWORD* grfBINDF, BINDINFO* pbindinfo)
374 static HRESULT WINAPI InstallCallback_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
375 DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
377 IStream *str = pstgmed->u.pstm;
384 hres = IStream_Read(str, buf, sizeof(buf), &size);
386 WriteFile(tmp_file, buf, size, NULL, NULL);
387 }while(hres == S_OK);
392 static HRESULT WINAPI InstallCallback_OnObjectAvailable(IBindStatusCallback *iface,
393 REFIID riid, IUnknown* punk)
399 static const IBindStatusCallbackVtbl InstallCallbackVtbl = {
400 InstallCallback_QueryInterface,
401 InstallCallback_AddRef,
402 InstallCallback_Release,
403 InstallCallback_OnStartBinding,
404 InstallCallback_GetPriority,
405 InstallCallback_OnLowResource,
406 InstallCallback_OnProgress,
407 InstallCallback_OnStopBinding,
408 InstallCallback_GetBindInfo,
409 InstallCallback_OnDataAvailable,
410 InstallCallback_OnObjectAvailable
413 static IBindStatusCallback InstallCallback = { &InstallCallbackVtbl };
415 static LPWSTR get_url(void)
419 DWORD size = INTERNET_MAX_URL_LENGTH*sizeof(WCHAR);
422 static const WCHAR wszGeckoUrl[] = {'G','e','c','k','o','U','r','l',0};
423 static const WCHAR httpW[] = {'h','t','t','p'};
424 static const WCHAR v_formatW[] = {'?','v','=',0};
426 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
427 res = RegOpenKeyW(HKEY_CURRENT_USER, mshtml_keyW, &hkey);
428 if(res != ERROR_SUCCESS)
431 url = mshtml_alloc(size);
433 res = RegQueryValueExW(hkey, wszGeckoUrl, NULL, &type, (LPBYTE)url, &size);
435 if(res != ERROR_SUCCESS || type != REG_SZ) {
440 if(size > sizeof(httpW) && !memcmp(url, httpW, sizeof(httpW))) {
441 strcatW(url, v_formatW);
442 MultiByteToWideChar(CP_ACP, 0, GECKO_VERSION, -1, url+strlenW(url), -1);
445 TRACE("Got URL %s\n", debugstr_w(url));
449 static DWORD WINAPI download_proc(PVOID arg)
456 CreateURLMoniker(NULL, url, &mon);
460 CreateAsyncBindCtx(0, &InstallCallback, 0, &bctx);
462 hres = IMoniker_BindToStorage(mon, bctx, NULL, &IID_IStream, (void**)&str);
463 IBindCtx_Release(bctx);
465 ERR("BindToStorage failed: %08x\n", hres);
470 IStream_Release(str);
475 static INT_PTR CALLBACK installer_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
479 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_HIDE);
480 install_dialog = hwnd;
490 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_SHOW);
491 EnableWindow(GetDlgItem(hwnd, ID_DWL_INSTALL), 0);
492 EnableWindow(GetDlgItem(hwnd, IDCANCEL), 0); /* FIXME */
493 CreateThread(NULL, 0, download_proc, NULL, 0, NULL);
501 BOOL install_wine_gecko(void)
505 SetLastError(ERROR_SUCCESS);
506 hsem = CreateSemaphoreA( NULL, 0, 1, "mshtml_install_semaphore");
508 if(GetLastError() == ERROR_ALREADY_EXISTS) {
509 WaitForSingleObject(hsem, INFINITE);
512 * Try to find Gecko .cab file in following order:
513 * - directory stored in GeckoCabDir value of HKCU/Software/MSHTML key
515 * - download from URL stored in GeckoUrl value of HKCU/Software/MSHTML key
517 if(!install_from_registered_dir()
518 && !install_from_default_dir()
519 && (url = get_url()))
520 DialogBoxW(hInst, MAKEINTRESOURCEW(ID_DWL_DIALOG), 0, installer_proc);
523 ReleaseSemaphore(hsem, 1, NULL);