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