winemenubuilder: Create/release semaphore only around call to wineshelllink.
[wine/testsucceed.git] / dlls / mshtml / install.c
blob845598e489a7c8edcb7affc2e9c21ce33c84204b
1 /*
2 * Copyright 2006 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
19 #include "config.h"
21 #include <stdarg.h>
23 #define COBJMACROS
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "winreg.h"
31 #include "ole2.h"
32 #include "commctrl.h"
33 #include "advpub.h"
34 #include "wininet.h"
35 #include "shellapi.h"
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
40 #include "mshtml_private.h"
41 #include "resource.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
45 static HWND install_dialog = NULL;
46 static LPWSTR tmp_file_name = NULL;
47 static HANDLE tmp_file = INVALID_HANDLE_VALUE;
48 static LPWSTR url = NULL;
50 static void clean_up(void)
52 if(tmp_file != INVALID_HANDLE_VALUE)
53 CloseHandle(tmp_file);
55 if(tmp_file_name) {
56 DeleteFileW(tmp_file_name);
57 mshtml_free(tmp_file_name);
58 tmp_file_name = NULL;
61 if(tmp_file != INVALID_HANDLE_VALUE) {
62 CloseHandle(tmp_file);
63 tmp_file = INVALID_HANDLE_VALUE;
66 if(install_dialog)
67 EndDialog(install_dialog, 0);
70 static void set_status(DWORD id)
72 HWND status = GetDlgItem(install_dialog, ID_DWL_STATUS);
73 WCHAR buf[64];
75 LoadStringW(hInst, id, buf, sizeof(buf)/sizeof(WCHAR));
76 SendMessageW(status, WM_SETTEXT, 0, (LPARAM)buf);
79 static void set_registry(LPCSTR install_dir)
81 WCHAR mshtml_key[100];
82 LPWSTR gecko_path;
83 HKEY hkey;
84 DWORD res, len, size;
86 static const WCHAR wszMshtmlKey[] = {
87 'S','o','f','t','w','a','r','e','\\','W','i','n','e',
88 '\\','M','S','H','T','M','L','\\'};
89 static const WCHAR wszGeckoPath[] = {'G','e','c','k','o','P','a','t','h',0};
90 static const WCHAR wszWineGecko[] = {'w','i','n','e','_','g','e','c','k','o',0};
92 memcpy(mshtml_key, wszMshtmlKey, sizeof(wszMshtmlKey));
93 MultiByteToWideChar(CP_ACP, 0, GECKO_VERSION, sizeof(GECKO_VERSION),
94 mshtml_key+sizeof(wszMshtmlKey)/sizeof(WCHAR),
95 (sizeof(mshtml_key)-sizeof(wszMshtmlKey))/sizeof(WCHAR));
97 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML\<version> */
98 res = RegCreateKeyW(HKEY_CURRENT_USER, mshtml_key, &hkey);
99 if(res != ERROR_SUCCESS) {
100 ERR("Faild to create MSHTML key: %d\n", res);
101 return;
104 len = MultiByteToWideChar(CP_ACP, 0, install_dir, -1, NULL, 0)-1;
105 gecko_path = mshtml_alloc((len+1)*sizeof(WCHAR)+sizeof(wszWineGecko));
106 MultiByteToWideChar(CP_ACP, 0, install_dir, -1, gecko_path, (len+1)*sizeof(WCHAR));
108 if (len && gecko_path[len-1] != '\\')
109 gecko_path[len++] = '\\';
111 memcpy(gecko_path+len, wszWineGecko, sizeof(wszWineGecko));
113 size = len*sizeof(WCHAR)+sizeof(wszWineGecko);
114 res = RegSetValueExW(hkey, wszGeckoPath, 0, REG_SZ, (LPVOID)gecko_path,
115 len*sizeof(WCHAR)+sizeof(wszWineGecko));
116 mshtml_free(gecko_path);
117 RegCloseKey(hkey);
118 if(res != ERROR_SUCCESS)
119 ERR("Failed to set GeckoPath value: %08x\n", res);
122 static HRESULT WINAPI InstallCallback_QueryInterface(IBindStatusCallback *iface,
123 REFIID riid, void **ppv)
125 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IBindStatusCallback, riid)) {
126 *ppv = iface;
127 return S_OK;
130 return E_INVALIDARG;
133 static ULONG WINAPI InstallCallback_AddRef(IBindStatusCallback *iface)
135 return 2;
138 static ULONG WINAPI InstallCallback_Release(IBindStatusCallback *iface)
140 return 1;
143 static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
144 DWORD dwReserved, IBinding *pib)
146 WCHAR tmp_dir[MAX_PATH];
148 set_status(IDS_DOWNLOADING);
150 GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);
152 tmp_file_name = mshtml_alloc(MAX_PATH*sizeof(WCHAR));
153 GetTempFileNameW(tmp_dir, NULL, 0, tmp_file_name);
155 TRACE("creating temp file %s\n", debugstr_w(tmp_file_name));
157 tmp_file = CreateFileW(tmp_file_name, GENERIC_WRITE, 0, NULL,
158 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
160 if(tmp_file == INVALID_HANDLE_VALUE) {
161 ERR("Could not create file: %d\n", GetLastError());
162 clean_up();
163 return E_FAIL;
166 return S_OK;
169 static HRESULT WINAPI InstallCallback_GetPriority(IBindStatusCallback *iface,
170 LONG *pnPriority)
172 return E_NOTIMPL;
175 static HRESULT WINAPI InstallCallback_OnLowResource(IBindStatusCallback *iface,
176 DWORD dwReserved)
178 return E_NOTIMPL;
181 static HRESULT WINAPI InstallCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
182 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
184 HWND progress = GetDlgItem(install_dialog, ID_DWL_PROGRESS);
186 if(ulProgressMax)
187 SendMessageW(progress, PBM_SETRANGE32, 0, ulProgressMax);
188 if(ulProgress)
189 SendMessageW(progress, PBM_SETPOS, ulProgress, 0);
191 return S_OK;
194 static HRESULT WINAPI InstallCallback_OnStopBinding(IBindStatusCallback *iface,
195 HRESULT hresult, LPCWSTR szError)
197 LPSTR file_name;
198 DWORD len;
199 HMODULE advpack;
200 char install_dir[MAX_PATH];
201 typeof(ExtractFilesA) *pExtractFilesA;
202 BOOL res;
203 HRESULT hres;
205 static const WCHAR wszAdvpack[] = {'a','d','v','p','a','c','k','.','d','l','l',0};
207 if(FAILED(hresult)) {
208 ERR("Binding failed %08x\n", hresult);
209 clean_up();
210 return S_OK;
213 CloseHandle(tmp_file);
214 tmp_file = INVALID_HANDLE_VALUE;
216 set_status(IDS_INSTALLING);
218 GetWindowsDirectoryA(install_dir, sizeof(install_dir));
219 strcat(install_dir, "\\gecko\\");
220 res = CreateDirectoryA(install_dir, NULL);
221 if(!res && GetLastError() != ERROR_ALREADY_EXISTS) {
222 ERR("Could not create directory: %08u\n", GetLastError());
223 return S_OK;
226 strcat(install_dir, GECKO_VERSION);
227 res = CreateDirectoryA(install_dir, NULL);
228 if(!res && GetLastError() != ERROR_ALREADY_EXISTS) {
229 ERR("Could not create directory: %08u\n", GetLastError());
230 return S_OK;
233 advpack = LoadLibraryW(wszAdvpack);
234 pExtractFilesA = (typeof(ExtractFilesA)*)GetProcAddress(advpack, "ExtractFiles");
236 len = WideCharToMultiByte(CP_ACP, 0, tmp_file_name, -1, NULL, 0, NULL, NULL);
237 file_name = mshtml_alloc(len);
238 WideCharToMultiByte(CP_ACP, 0, tmp_file_name, -1, file_name, -1, NULL, NULL);
240 /* FIXME: Use unicode version (not yet implemented) */
241 hres = pExtractFilesA(file_name, install_dir, 0, NULL, NULL, 0);
242 FreeLibrary(advpack);
243 mshtml_free(file_name);
244 if(FAILED(hres)) {
245 ERR("Could not extract package: %08x\n", hres);
246 clean_up();
249 set_registry(install_dir);
250 clean_up();
252 return S_OK;
255 static HRESULT WINAPI InstallCallback_GetBindInfo(IBindStatusCallback *iface,
256 DWORD* grfBINDF, BINDINFO* pbindinfo)
258 /* FIXME */
259 *grfBINDF = 0;
260 return S_OK;
263 static HRESULT WINAPI InstallCallback_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
264 DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
266 IStream *str = pstgmed->u.pstm;
267 BYTE buf[1024];
268 DWORD size;
269 HRESULT hres;
271 do {
272 size = 0;
273 hres = IStream_Read(str, buf, sizeof(buf), &size);
274 if(size)
275 WriteFile(tmp_file, buf, size, NULL, NULL);
276 }while(hres == S_OK);
278 return S_OK;
281 static HRESULT WINAPI InstallCallback_OnObjectAvailable(IBindStatusCallback *iface,
282 REFIID riid, IUnknown* punk)
284 ERR("\n");
285 return E_NOTIMPL;
288 static const IBindStatusCallbackVtbl InstallCallbackVtbl = {
289 InstallCallback_QueryInterface,
290 InstallCallback_AddRef,
291 InstallCallback_Release,
292 InstallCallback_OnStartBinding,
293 InstallCallback_GetPriority,
294 InstallCallback_OnLowResource,
295 InstallCallback_OnProgress,
296 InstallCallback_OnStopBinding,
297 InstallCallback_GetBindInfo,
298 InstallCallback_OnDataAvailable,
299 InstallCallback_OnObjectAvailable
302 static IBindStatusCallback InstallCallback = { &InstallCallbackVtbl };
304 static LPWSTR get_url(void)
306 HKEY hkey;
307 DWORD res, type;
308 DWORD size = INTERNET_MAX_URL_LENGTH*sizeof(WCHAR);
309 LPWSTR url;
311 static const WCHAR wszMshtmlKey[] = {
312 'S','o','f','t','w','a','r','e','\\','W','i','n','e',
313 '\\','M','S','H','T','M','L',0};
314 static const WCHAR wszGeckoUrl[] = {'G','e','c','k','o','U','r','l',0};
315 static const WCHAR httpW[] = {'h','t','t','p'};
316 static const WCHAR v_formatW[] = {'?','v','=',0};
318 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
319 res = RegOpenKeyW(HKEY_CURRENT_USER, wszMshtmlKey, &hkey);
320 if(res != ERROR_SUCCESS)
321 return NULL;
323 url = mshtml_alloc(size);
325 res = RegQueryValueExW(hkey, wszGeckoUrl, NULL, &type, (LPBYTE)url, &size);
326 RegCloseKey(hkey);
327 if(res != ERROR_SUCCESS || type != REG_SZ) {
328 mshtml_free(url);
329 return NULL;
332 if(size > sizeof(httpW) && !memcmp(url, httpW, sizeof(httpW))) {
333 strcatW(url, v_formatW);
334 MultiByteToWideChar(CP_ACP, 0, GECKO_VERSION, -1, url+strlenW(url), -1);
337 TRACE("Got URL %s\n", debugstr_w(url));
338 return url;
341 static DWORD WINAPI download_proc(PVOID arg)
343 IMoniker *mon;
344 IBindCtx *bctx;
345 IStream *str = NULL;
346 HRESULT hres;
348 CreateURLMoniker(NULL, url, &mon);
349 mshtml_free(url);
350 url = NULL;
352 CreateAsyncBindCtx(0, &InstallCallback, 0, &bctx);
354 hres = IMoniker_BindToStorage(mon, bctx, NULL, &IID_IStream, (void**)&str);
355 IBindCtx_Release(bctx);
356 if(FAILED(hres)) {
357 ERR("BindToStorage failed: %08x\n", hres);
358 return 0;
361 if(str)
362 IStream_Release(str);
364 return 0;
367 static INT_PTR CALLBACK installer_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
369 switch(msg) {
370 case WM_INITDIALOG:
371 install_dialog = hwnd;
372 return TRUE;
374 case WM_COMMAND:
375 switch(wParam) {
376 case IDCANCEL:
377 EndDialog(hwnd, 0);
378 return FALSE;
380 case ID_DWL_INSTALL:
381 EnableWindow(GetDlgItem(hwnd, ID_DWL_INSTALL), 0);
382 EnableWindow(GetDlgItem(hwnd, IDCANCEL), 0); /* FIXME */
383 CreateThread(NULL, 0, download_proc, NULL, 0, NULL);
384 return FALSE;
388 return FALSE;
391 BOOL install_wine_gecko(void)
393 HANDLE hsem;
395 SetLastError(ERROR_SUCCESS);
396 hsem = CreateSemaphoreA( NULL, 0, 1, "mshtml_install_semaphore");
398 if(GetLastError() == ERROR_ALREADY_EXISTS) {
399 WaitForSingleObject(hsem, INFINITE);
400 }else {
401 if((url = get_url()))
402 DialogBoxW(hInst, MAKEINTRESOURCEW(ID_DWL_DIALOG), 0, installer_proc);
405 ReleaseSemaphore(hsem, 1, NULL);
406 CloseHandle(hsem);
408 return TRUE;