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