shell32/tests: Fixed potential overflow.
[wine/gsoc_dplay.git] / dlls / mshtml / install.c
blob07f83e7d9ae184f0805090098620ff685e446f78
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"
35 #include "wine/debug.h"
36 #include "wine/unicode.h"
38 #include "mshtml_private.h"
39 #include "resource.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
43 static HWND install_dialog = NULL;
44 static LPWSTR tmp_file_name = NULL;
45 static HANDLE tmp_file = INVALID_HANDLE_VALUE;
46 static LPWSTR url = NULL;
48 static void clean_up(void)
50 if(tmp_file != INVALID_HANDLE_VALUE)
51 CloseHandle(tmp_file);
53 if(tmp_file_name) {
54 DeleteFileW(tmp_file_name);
55 mshtml_free(tmp_file_name);
56 tmp_file_name = NULL;
59 if(tmp_file != INVALID_HANDLE_VALUE) {
60 CloseHandle(tmp_file);
61 tmp_file = INVALID_HANDLE_VALUE;
64 if(install_dialog)
65 EndDialog(install_dialog, 0);
68 static void set_status(DWORD id)
70 HWND status = GetDlgItem(install_dialog, ID_DWL_STATUS);
71 WCHAR buf[64];
73 LoadStringW(hInst, id, buf, sizeof(buf)/sizeof(WCHAR));
74 SendMessageW(status, WM_SETTEXT, 0, (LPARAM)buf);
77 static void set_registry(LPCSTR install_dir)
79 LPWSTR gecko_path;
80 HKEY hkey;
81 DWORD res, len, size;
83 static const WCHAR wszMshtmlKey[] = {
84 'S','o','f','t','w','a','r','e','\\','W','i','n','e',
85 '\\','M','S','H','T','M','L',0};
86 static const WCHAR wszGeckoPath[] = {'G','e','c','k','o','P','a','t','h',0};
87 static const WCHAR wszWineGecko[] = {'w','i','n','e','_','g','e','c','k','o',0};
89 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
90 res = RegOpenKeyW(HKEY_CURRENT_USER, wszMshtmlKey, &hkey);
91 if(res != ERROR_SUCCESS) {
92 ERR("Faild to open MSHTML key: %d\n", res);
93 return;
96 len = MultiByteToWideChar(CP_ACP, 0, install_dir, -1, NULL, 0)-1;
97 gecko_path = mshtml_alloc((len+1)*sizeof(WCHAR)+sizeof(wszWineGecko));
98 MultiByteToWideChar(CP_ACP, 0, install_dir, -1, gecko_path, (len+1)*sizeof(WCHAR));
100 if (len && gecko_path[len-1] != '\\')
101 gecko_path[len++] = '\\';
103 memcpy(gecko_path+len, wszWineGecko, sizeof(wszWineGecko));
105 size = len*sizeof(WCHAR)+sizeof(wszWineGecko);
106 res = RegSetValueExW(hkey, wszGeckoPath, 0, REG_SZ, (LPVOID)gecko_path,
107 len*sizeof(WCHAR)+sizeof(wszWineGecko));
108 mshtml_free(gecko_path);
109 RegCloseKey(hkey);
110 if(res != ERROR_SUCCESS)
111 ERR("Failed to set GeckoPath value: %08x\n", res);
114 static HRESULT WINAPI InstallCallback_QueryInterface(IBindStatusCallback *iface,
115 REFIID riid, void **ppv)
117 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IBindStatusCallback, riid)) {
118 *ppv = iface;
119 return S_OK;
122 return E_INVALIDARG;
125 static ULONG WINAPI InstallCallback_AddRef(IBindStatusCallback *iface)
127 return 2;
130 static ULONG WINAPI InstallCallback_Release(IBindStatusCallback *iface)
132 return 1;
135 static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
136 DWORD dwReserved, IBinding *pib)
138 WCHAR tmp_dir[MAX_PATH];
140 set_status(IDS_DOWNLOADING);
142 GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);
144 tmp_file_name = mshtml_alloc(MAX_PATH*sizeof(WCHAR));
145 GetTempFileNameW(tmp_dir, NULL, 0, tmp_file_name);
147 TRACE("creating temp file %s\n", debugstr_w(tmp_file_name));
149 tmp_file = CreateFileW(tmp_file_name, GENERIC_WRITE, 0, NULL,
150 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
152 if(tmp_file == INVALID_HANDLE_VALUE) {
153 ERR("Could not create file: %d\n", GetLastError());
154 clean_up();
155 return E_FAIL;
158 return S_OK;
161 static HRESULT WINAPI InstallCallback_GetPriority(IBindStatusCallback *iface,
162 LONG *pnPriority)
164 return E_NOTIMPL;
167 static HRESULT WINAPI InstallCallback_OnLowResource(IBindStatusCallback *iface,
168 DWORD dwReserved)
170 return E_NOTIMPL;
173 static HRESULT WINAPI InstallCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
174 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
176 HWND progress = GetDlgItem(install_dialog, ID_DWL_PROGRESS);
178 if(ulProgressMax)
179 SendMessageW(progress, PBM_SETRANGE32, 0, ulProgressMax);
180 if(ulProgress)
181 SendMessageW(progress, PBM_SETPOS, ulProgress, 0);
183 return S_OK;
186 static HRESULT WINAPI InstallCallback_OnStopBinding(IBindStatusCallback *iface,
187 HRESULT hresult, LPCWSTR szError)
189 LPSTR file_name;
190 DWORD len;
191 HMODULE advpack;
192 char program_files[MAX_PATH];
193 typeof(ExtractFilesA) *pExtractFilesA;
194 HRESULT hres;
196 static const WCHAR wszAdvpack[] = {'a','d','v','p','a','c','k','.','d','l','l',0};
198 if(FAILED(hresult)) {
199 ERR("Binding failed %08x\n", hresult);
200 clean_up();
201 return S_OK;
204 CloseHandle(tmp_file);
205 tmp_file = INVALID_HANDLE_VALUE;
207 set_status(IDS_INSTALLING);
209 advpack = LoadLibraryW(wszAdvpack);
210 pExtractFilesA = (typeof(ExtractFilesA)*)GetProcAddress(advpack, "ExtractFiles");
212 len = WideCharToMultiByte(CP_ACP, 0, tmp_file_name, -1, NULL, 0, NULL, NULL);
213 file_name = mshtml_alloc(len);
214 WideCharToMultiByte(CP_ACP, 0, tmp_file_name, -1, file_name, -1, NULL, NULL);
216 GetEnvironmentVariableA("ProgramFiles", program_files, sizeof(program_files));
218 /* FIXME: Use unicode version (not yet implemented) */
219 hres = pExtractFilesA(file_name, program_files, 0, NULL, NULL, 0);
220 FreeLibrary(advpack);
221 mshtml_free(file_name);
222 if(FAILED(hres)) {
223 ERR("Could not extract package: %08x\n", hres);
224 clean_up();
227 set_registry(program_files);
228 clean_up();
230 return S_OK;
233 static HRESULT WINAPI InstallCallback_GetBindInfo(IBindStatusCallback *iface,
234 DWORD* grfBINDF, BINDINFO* pbindinfo)
236 /* FIXME */
237 *grfBINDF = 0;
238 return S_OK;
241 static HRESULT WINAPI InstallCallback_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
242 DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
244 IStream *str = pstgmed->u.pstm;
245 BYTE buf[1024];
246 DWORD size;
247 HRESULT hres;
249 do {
250 size = 0;
251 hres = IStream_Read(str, buf, sizeof(buf), &size);
252 if(size)
253 WriteFile(tmp_file, buf, size, NULL, NULL);
254 }while(hres == S_OK);
256 return S_OK;
259 static HRESULT WINAPI InstallCallback_OnObjectAvailable(IBindStatusCallback *iface,
260 REFIID riid, IUnknown* punk)
262 ERR("\n");
263 return E_NOTIMPL;
266 static const IBindStatusCallbackVtbl InstallCallbackVtbl = {
267 InstallCallback_QueryInterface,
268 InstallCallback_AddRef,
269 InstallCallback_Release,
270 InstallCallback_OnStartBinding,
271 InstallCallback_GetPriority,
272 InstallCallback_OnLowResource,
273 InstallCallback_OnProgress,
274 InstallCallback_OnStopBinding,
275 InstallCallback_GetBindInfo,
276 InstallCallback_OnDataAvailable,
277 InstallCallback_OnObjectAvailable
280 static IBindStatusCallback InstallCallback = { &InstallCallbackVtbl };
282 static LPWSTR get_url(void)
284 HKEY hkey;
285 DWORD res, type;
286 DWORD size = 512*sizeof(WCHAR);
287 LPWSTR url;
289 static const WCHAR wszMshtmlKey[] = {
290 'S','o','f','t','w','a','r','e','\\','W','i','n','e',
291 '\\','M','S','H','T','M','L',0};
292 static const WCHAR wszGeckoUrl[] = {'G','e','c','k','o','U','r','l',0};
294 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
295 res = RegOpenKeyW(HKEY_CURRENT_USER, wszMshtmlKey, &hkey);
296 if(res != ERROR_SUCCESS)
297 return NULL;
299 url = mshtml_alloc(size);
301 res = RegQueryValueExW(hkey, wszGeckoUrl, NULL, &type, (LPBYTE)url, &size);
302 RegCloseKey(hkey);
303 if(res != ERROR_SUCCESS || type != REG_SZ) {
304 mshtml_free(url);
305 return NULL;
308 return url;
311 static DWORD WINAPI download_proc(PVOID arg)
313 IMoniker *mon;
314 IBindCtx *bctx;
315 IStream *str = NULL;
316 HRESULT hres;
318 CreateURLMoniker(NULL, url, &mon);
319 mshtml_free(url);
320 url = NULL;
322 CreateAsyncBindCtx(0, &InstallCallback, 0, &bctx);
324 hres = IMoniker_BindToStorage(mon, bctx, NULL, &IID_IStream, (void**)&str);
325 IBindCtx_Release(bctx);
326 if(FAILED(hres)) {
327 ERR("BindToStorage failed: %08x\n", hres);
328 return 0;
331 if(str)
332 IStream_Release(str);
334 return 0;
337 static INT_PTR CALLBACK installer_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
339 switch(msg) {
340 case WM_INITDIALOG:
341 install_dialog = hwnd;
342 return TRUE;
344 case WM_COMMAND:
345 switch(wParam) {
346 case IDCANCEL:
347 EndDialog(hwnd, 0);
348 return FALSE;
350 case ID_DWL_INSTALL:
351 EnableWindow(GetDlgItem(hwnd, ID_DWL_INSTALL), 0);
352 EnableWindow(GetDlgItem(hwnd, IDCANCEL), 0); /* FIXME */
353 CreateThread(NULL, 0, download_proc, NULL, 0, NULL);
354 return FALSE;
358 return FALSE;
361 BOOL install_wine_gecko(void)
363 HANDLE hsem;
365 SetLastError(ERROR_SUCCESS);
366 hsem = CreateSemaphoreA( NULL, 0, 1, "mshtml_install_semaphore");
368 if(GetLastError() == ERROR_ALREADY_EXISTS) {
369 WaitForSingleObject(hsem, INFINITE);
370 }else {
371 if((url = get_url()))
372 DialogBoxW(hInst, MAKEINTRESOURCEW(ID_DWL_DIALOG), 0, installer_proc);
375 ReleaseSemaphore(hsem, 1, NULL);
376 CloseHandle(hsem);
378 return TRUE;