e47c28423d0cdf89de29b3ced61e9d6dcb0fa445
[reactos.git] / reactos / dll / win32 / mshtml / install.c
1 /*
2 * Copyright 2006-2007 Jacek Caban for CodeWeavers
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22 #include <fcntl.h>
23 #ifdef HAVE_UNISTD_H
24 # include <unistd.h>
25 #endif
26
27 #define COBJMACROS
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winuser.h"
34 #include "winreg.h"
35 #include "ole2.h"
36 #include "commctrl.h"
37 #include "advpub.h"
38 #include "wininet.h"
39 #include "shellapi.h"
40
41 #include "wine/debug.h"
42 #include "wine/unicode.h"
43 #include "wine/library.h"
44
45 #include "mshtml_private.h"
46 #include "resource.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
49
50 #ifdef __i386__
51 #define GECKO_ARCH "x86"
52 #else
53 #define GECKO_ARCH ""
54 #endif
55
56 #define GECKO_FILE_NAME "wine_gecko-" GECKO_VERSION "-" GECKO_ARCH ".cab"
57
58 static const WCHAR mshtml_keyW[] =
59 {'S','o','f','t','w','a','r','e',
60 '\\','W','i','n','e',
61 '\\','M','S','H','T','M','L',0};
62
63 static const CHAR mshtml_keyA[] =
64 {'S','o','f','t','w','a','r','e',
65 '\\','W','i','n','e',
66 '\\','M','S','H','T','M','L',0};
67
68 static HWND install_dialog = NULL;
69 static LPWSTR tmp_file_name = NULL;
70 static HANDLE tmp_file = INVALID_HANDLE_VALUE;
71 static LPWSTR url = NULL;
72
73 static void clean_up(void)
74 {
75 if(tmp_file != INVALID_HANDLE_VALUE)
76 CloseHandle(tmp_file);
77
78 if(tmp_file_name) {
79 DeleteFileW(tmp_file_name);
80 heap_free(tmp_file_name);
81 tmp_file_name = NULL;
82 }
83
84 if(tmp_file != INVALID_HANDLE_VALUE) {
85 CloseHandle(tmp_file);
86 tmp_file = INVALID_HANDLE_VALUE;
87 }
88
89 if(install_dialog)
90 EndDialog(install_dialog, 0);
91 }
92
93 static void set_status(DWORD id)
94 {
95 HWND status = GetDlgItem(install_dialog, ID_DWL_STATUS);
96 WCHAR buf[64];
97
98 LoadStringW(hInst, id, buf, sizeof(buf)/sizeof(WCHAR));
99 SendMessageW(status, WM_SETTEXT, 0, (LPARAM)buf);
100 }
101
102 static void set_registry(LPCSTR install_dir)
103 {
104 WCHAR mshtml_key[100];
105 LPWSTR gecko_path;
106 HKEY hkey;
107 DWORD res, len;
108
109 static const WCHAR wszGeckoPath[] = {'G','e','c','k','o','P','a','t','h',0};
110 static const WCHAR wszWineGecko[] = {'w','i','n','e','_','g','e','c','k','o',0};
111
112 memcpy(mshtml_key, mshtml_keyW, sizeof(mshtml_keyW));
113 mshtml_key[sizeof(mshtml_keyW)/sizeof(WCHAR)-1] = '\\';
114 MultiByteToWideChar(CP_ACP, 0, GECKO_VERSION, sizeof(GECKO_VERSION),
115 mshtml_key+sizeof(mshtml_keyW)/sizeof(WCHAR),
116 (sizeof(mshtml_key)-sizeof(mshtml_keyW))/sizeof(WCHAR));
117
118 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML\<version> */
119 res = RegCreateKeyW(HKEY_CURRENT_USER, mshtml_key, &hkey);
120 if(res != ERROR_SUCCESS) {
121 ERR("Faild to create MSHTML key: %d\n", res);
122 return;
123 }
124
125 len = MultiByteToWideChar(CP_ACP, 0, install_dir, -1, NULL, 0)-1;
126 gecko_path = heap_alloc((len+1)*sizeof(WCHAR)+sizeof(wszWineGecko));
127 MultiByteToWideChar(CP_ACP, 0, install_dir, -1, gecko_path, len+1);
128
129 if (len && gecko_path[len-1] != '\\')
130 gecko_path[len++] = '\\';
131
132 memcpy(gecko_path+len, wszWineGecko, sizeof(wszWineGecko));
133
134 res = RegSetValueExW(hkey, wszGeckoPath, 0, REG_SZ, (LPVOID)gecko_path,
135 len*sizeof(WCHAR)+sizeof(wszWineGecko));
136 heap_free(gecko_path);
137 RegCloseKey(hkey);
138 if(res != ERROR_SUCCESS)
139 ERR("Failed to set GeckoPath value: %08x\n", res);
140 }
141
142 static BOOL install_cab(LPCWSTR file_name)
143 {
144 HMODULE advpack;
145 char install_dir[MAX_PATH];
146 HRESULT (WINAPI *pExtractFilesA)(LPCSTR,LPCSTR,DWORD,LPCSTR,LPVOID,DWORD);
147 LPSTR file_name_a;
148 DWORD res;
149 HRESULT hres;
150
151 static const WCHAR wszAdvpack[] = {'a','d','v','p','a','c','k','.','d','l','l',0};
152
153 TRACE("(%s)\n", debugstr_w(file_name));
154
155 GetSystemDirectoryA(install_dir, sizeof(install_dir));
156 strcat(install_dir, "\\gecko\\");
157 res = CreateDirectoryA(install_dir, NULL);
158 if(!res && GetLastError() != ERROR_ALREADY_EXISTS) {
159 ERR("Could not create directory: %08u\n", GetLastError());
160 return FALSE;
161 }
162
163 strcat(install_dir, GECKO_VERSION);
164 res = CreateDirectoryA(install_dir, NULL);
165 if(!res && GetLastError() != ERROR_ALREADY_EXISTS) {
166 ERR("Could not create directory: %08u\n", GetLastError());
167 return FALSE;
168 }
169
170 advpack = LoadLibraryW(wszAdvpack);
171 pExtractFilesA = (void *)GetProcAddress(advpack, "ExtractFiles");
172
173 /* FIXME: Use unicode version (not yet implemented) */
174 file_name_a = heap_strdupWtoA(file_name);
175 hres = pExtractFilesA(file_name_a, install_dir, 0, NULL, NULL, 0);
176 FreeLibrary(advpack);
177 heap_free(file_name_a);
178 if(FAILED(hres)) {
179 ERR("Could not extract package: %08x\n", hres);
180 clean_up();
181 return FALSE;
182 }
183
184 set_registry(install_dir);
185 clean_up();
186
187 return TRUE;
188 }
189
190 static BOOL install_from_unix_file(const char *file_name)
191 {
192 LPWSTR dos_file_name;
193 int fd;
194 BOOL ret;
195
196 static WCHAR * (CDECL *wine_get_dos_file_name)(const char*);
197 static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
198
199 fd = open(file_name, O_RDONLY);
200 if(fd == -1) {
201 TRACE("%s not found\n", debugstr_a(file_name));
202 return FALSE;
203 }
204
205 close(fd);
206
207 if(!wine_get_dos_file_name)
208 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleW(kernel32W), "wine_get_dos_file_name");
209
210 if(wine_get_dos_file_name) { /* Wine UNIX mode */
211 dos_file_name = wine_get_dos_file_name(file_name);
212 if(!dos_file_name) {
213 ERR("Could not get dos file name of %s\n", debugstr_a(file_name));
214 return FALSE;
215 }
216 } else { /* Windows mode */
217 UINT res;
218 WARN("Could not get wine_get_dos_file_name function, calling install_cab directly.\n");
219 res = MultiByteToWideChar( CP_ACP, 0, file_name, -1, 0, 0);
220 dos_file_name = heap_alloc (res*sizeof(WCHAR));
221 MultiByteToWideChar( CP_ACP, 0, file_name, -1, dos_file_name, res);
222 }
223
224 ret = install_cab(dos_file_name);
225
226 heap_free(dos_file_name);
227 return ret;
228 }
229
230 static BOOL install_from_registered_dir(void)
231 {
232 char *file_name;
233 DWORD res, type, size = MAX_PATH;
234 BOOL ret;
235
236 file_name = heap_alloc(size+sizeof(GECKO_FILE_NAME));
237 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
238 res = RegGetValueA(HKEY_CURRENT_USER, mshtml_keyA, "GeckoCabDir", RRF_RT_ANY, &type, (PBYTE)file_name, &size);
239 if(res == ERROR_MORE_DATA) {
240 file_name = heap_realloc(file_name, size+sizeof(GECKO_FILE_NAME));
241 res = RegGetValueA(HKEY_CURRENT_USER, mshtml_keyA, "GeckoCabDir", RRF_RT_ANY, &type, (PBYTE)file_name, &size);
242 }
243
244 if(res != ERROR_SUCCESS || (type != REG_SZ && type != REG_EXPAND_SZ)) {
245 heap_free(file_name);
246 return FALSE;
247 }
248
249 strcat(file_name, GECKO_FILE_NAME);
250
251 TRACE("Trying %s\n", debugstr_a(file_name));
252
253 ret = install_from_unix_file(file_name);
254
255 heap_free(file_name);
256 return ret;
257 }
258
259 static BOOL install_from_default_dir(void)
260 {
261 const char *data_dir, *subdir;
262 char *file_name;
263 int len, len2;
264 BOOL ret;
265
266 if((data_dir = wine_get_data_dir()))
267 subdir = "/gecko/";
268 else if((data_dir = wine_get_build_dir()))
269 subdir = "/../gecko/";
270 else
271 return FALSE;
272
273 len = strlen(data_dir);
274 len2 = strlen(subdir);
275
276 file_name = heap_alloc(len+len2+sizeof(GECKO_FILE_NAME));
277 memcpy(file_name, data_dir, len);
278 memcpy(file_name+len, subdir, len2);
279 memcpy(file_name+len+len2, GECKO_FILE_NAME, sizeof(GECKO_FILE_NAME));
280
281 ret = install_from_unix_file(file_name);
282
283 heap_free(file_name);
284 return ret;
285 }
286
287 static HRESULT WINAPI InstallCallback_QueryInterface(IBindStatusCallback *iface,
288 REFIID riid, void **ppv)
289 {
290 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IBindStatusCallback, riid)) {
291 *ppv = iface;
292 return S_OK;
293 }
294
295 return E_INVALIDARG;
296 }
297
298 static ULONG WINAPI InstallCallback_AddRef(IBindStatusCallback *iface)
299 {
300 return 2;
301 }
302
303 static ULONG WINAPI InstallCallback_Release(IBindStatusCallback *iface)
304 {
305 return 1;
306 }
307
308 static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
309 DWORD dwReserved, IBinding *pib)
310 {
311 WCHAR tmp_dir[MAX_PATH];
312
313 set_status(IDS_DOWNLOADING);
314
315 GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);
316
317 tmp_file_name = heap_alloc(MAX_PATH*sizeof(WCHAR));
318 GetTempFileNameW(tmp_dir, NULL, 0, tmp_file_name);
319
320 TRACE("creating temp file %s\n", debugstr_w(tmp_file_name));
321
322 tmp_file = CreateFileW(tmp_file_name, GENERIC_WRITE, 0, NULL,
323 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
324
325 if(tmp_file == INVALID_HANDLE_VALUE) {
326 ERR("Could not create file: %d\n", GetLastError());
327 clean_up();
328 return E_FAIL;
329 }
330
331 return S_OK;
332 }
333
334 static HRESULT WINAPI InstallCallback_GetPriority(IBindStatusCallback *iface,
335 LONG *pnPriority)
336 {
337 return E_NOTIMPL;
338 }
339
340 static HRESULT WINAPI InstallCallback_OnLowResource(IBindStatusCallback *iface,
341 DWORD dwReserved)
342 {
343 return E_NOTIMPL;
344 }
345
346 static HRESULT WINAPI InstallCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
347 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
348 {
349 HWND progress = GetDlgItem(install_dialog, ID_DWL_PROGRESS);
350
351 if(ulProgressMax)
352 SendMessageW(progress, PBM_SETRANGE32, 0, ulProgressMax);
353 if(ulProgress)
354 SendMessageW(progress, PBM_SETPOS, ulProgress, 0);
355
356 return S_OK;
357 }
358
359 static HRESULT WINAPI InstallCallback_OnStopBinding(IBindStatusCallback *iface,
360 HRESULT hresult, LPCWSTR szError)
361 {
362 if(FAILED(hresult)) {
363 ERR("Binding failed %08x\n", hresult);
364 clean_up();
365 return S_OK;
366 }
367
368 CloseHandle(tmp_file);
369 tmp_file = INVALID_HANDLE_VALUE;
370
371 set_status(IDS_INSTALLING);
372
373 install_cab(tmp_file_name);
374
375 return S_OK;
376 }
377
378 static HRESULT WINAPI InstallCallback_GetBindInfo(IBindStatusCallback *iface,
379 DWORD* grfBINDF, BINDINFO* pbindinfo)
380 {
381 /* FIXME */
382 *grfBINDF = 0;
383 return S_OK;
384 }
385
386 static HRESULT WINAPI InstallCallback_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
387 DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
388 {
389 IStream *str = pstgmed->u.pstm;
390 BYTE buf[1024];
391 DWORD size;
392 HRESULT hres;
393
394 do {
395 DWORD written;
396
397 size = 0;
398 hres = IStream_Read(str, buf, sizeof(buf), &size);
399 if(size)
400 WriteFile(tmp_file, buf, size, &written, NULL);
401 }while(hres == S_OK);
402
403 return S_OK;
404 }
405
406 static HRESULT WINAPI InstallCallback_OnObjectAvailable(IBindStatusCallback *iface,
407 REFIID riid, IUnknown* punk)
408 {
409 ERR("\n");
410 return E_NOTIMPL;
411 }
412
413 static const IBindStatusCallbackVtbl InstallCallbackVtbl = {
414 InstallCallback_QueryInterface,
415 InstallCallback_AddRef,
416 InstallCallback_Release,
417 InstallCallback_OnStartBinding,
418 InstallCallback_GetPriority,
419 InstallCallback_OnLowResource,
420 InstallCallback_OnProgress,
421 InstallCallback_OnStopBinding,
422 InstallCallback_GetBindInfo,
423 InstallCallback_OnDataAvailable,
424 InstallCallback_OnObjectAvailable
425 };
426
427 static IBindStatusCallback InstallCallback = { &InstallCallbackVtbl };
428
429 static LPWSTR get_url(void)
430 {
431 HKEY hkey;
432 DWORD res, type;
433 DWORD size = INTERNET_MAX_URL_LENGTH*sizeof(WCHAR);
434 DWORD returned_size;
435 LPWSTR url;
436
437 static const WCHAR wszGeckoUrl[] = {'G','e','c','k','o','U','r','l',0};
438 static const WCHAR httpW[] = {'h','t','t','p'};
439 static const WCHAR arch_formatW[] = {'?','a','r','c','h','='};
440 static const WCHAR v_formatW[] = {'&','v','='};
441
442 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
443 res = RegOpenKeyW(HKEY_CURRENT_USER, mshtml_keyW, &hkey);
444 if(res != ERROR_SUCCESS)
445 return NULL;
446
447 url = heap_alloc(size);
448 returned_size = size;
449
450 res = RegQueryValueExW(hkey, wszGeckoUrl, NULL, &type, (LPBYTE)url, &returned_size);
451 RegCloseKey(hkey);
452 if(res != ERROR_SUCCESS || type != REG_SZ) {
453 heap_free(url);
454 return NULL;
455 }
456
457 if(returned_size > sizeof(httpW) && !memcmp(url, httpW, sizeof(httpW))) {
458 DWORD len;
459
460 len = strlenW(url);
461 memcpy(url+len, arch_formatW, sizeof(arch_formatW));
462 len += sizeof(arch_formatW)/sizeof(WCHAR);
463 len += MultiByteToWideChar(CP_ACP, 0, GECKO_ARCH, sizeof(GECKO_ARCH), url+len, size/sizeof(WCHAR)-len)-1;
464 memcpy(url+len, v_formatW, sizeof(v_formatW));
465 len += sizeof(v_formatW)/sizeof(WCHAR);
466 MultiByteToWideChar(CP_ACP, 0, GECKO_VERSION, -1, url+len, size/sizeof(WCHAR)-len);
467 }
468
469 TRACE("Got URL %s\n", debugstr_w(url));
470 return url;
471 }
472
473 static DWORD WINAPI download_proc(PVOID arg)
474 {
475 IMoniker *mon;
476 IBindCtx *bctx;
477 IStream *str = NULL;
478 HRESULT hres;
479
480 CreateURLMoniker(NULL, url, &mon);
481 heap_free(url);
482 url = NULL;
483
484 CreateAsyncBindCtx(0, &InstallCallback, 0, &bctx);
485
486 hres = IMoniker_BindToStorage(mon, bctx, NULL, &IID_IStream, (void**)&str);
487 IBindCtx_Release(bctx);
488 if(FAILED(hres)) {
489 ERR("BindToStorage failed: %08x\n", hres);
490 return 0;
491 }
492
493 if(str)
494 IStream_Release(str);
495
496 return 0;
497 }
498
499 static INT_PTR CALLBACK installer_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
500 {
501 switch(msg) {
502 case WM_INITDIALOG:
503 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_HIDE);
504 install_dialog = hwnd;
505 return TRUE;
506
507 case WM_COMMAND:
508 switch(wParam) {
509 case IDCANCEL:
510 EndDialog(hwnd, 0);
511 return FALSE;
512
513 case ID_DWL_INSTALL:
514 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_SHOW);
515 EnableWindow(GetDlgItem(hwnd, ID_DWL_INSTALL), 0);
516 EnableWindow(GetDlgItem(hwnd, IDCANCEL), 0); /* FIXME */
517 CreateThread(NULL, 0, download_proc, NULL, 0, NULL);
518 return FALSE;
519 }
520 }
521
522 return FALSE;
523 }
524
525 BOOL install_wine_gecko(BOOL silent)
526 {
527 HANDLE hsem;
528
529 if(!*GECKO_ARCH)
530 return FALSE;
531
532 SetLastError(ERROR_SUCCESS);
533 hsem = CreateSemaphoreA( NULL, 0, 1, "mshtml_install_semaphore");
534
535 if(GetLastError() == ERROR_ALREADY_EXISTS) {
536 WaitForSingleObject(hsem, INFINITE);
537 }else {
538 /*
539 * Try to find Gecko .cab file in following order:
540 * - directory stored in GeckoCabDir value of HKCU/Software/MSHTML key
541 * - $datadir/gecko
542 * - download from URL stored in GeckoUrl value of HKCU/Software/MSHTML key
543 */
544 if(!install_from_registered_dir()
545 && !install_from_default_dir()
546 && !silent && (url = get_url()))
547 DialogBoxW(hInst, MAKEINTRESOURCEW(ID_DWL_DIALOG), 0, installer_proc);
548 }
549
550 ReleaseSemaphore(hsem, 1, NULL);
551 CloseHandle(hsem);
552
553 return TRUE;
554 }