ntdll: Set initial module refcount to 1 to avoid unloading during imports fixup.
[wine] / dlls / mshtml / install.c
1 /*
2  * Copyright 2006 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
23 #define COBJMACROS
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
26
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
35 #include "wine/debug.h"
36 #include "wine/unicode.h"
37
38 #include "mshtml_private.h"
39 #include "resource.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
42
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;
47
48 static void clean_up(void)
49 {
50     if(tmp_file != INVALID_HANDLE_VALUE)
51         CloseHandle(tmp_file);
52
53     if(tmp_file_name) {
54         DeleteFileW(tmp_file_name);
55         mshtml_free(tmp_file_name);
56         tmp_file_name = NULL;
57     }
58
59     if(tmp_file != INVALID_HANDLE_VALUE) {
60         CloseHandle(tmp_file);
61         tmp_file = INVALID_HANDLE_VALUE;
62     }
63
64     if(install_dialog)
65         EndDialog(install_dialog, 0);
66 }
67
68 static void set_status(DWORD id)
69 {
70     HWND status = GetDlgItem(install_dialog, ID_DWL_STATUS);
71     WCHAR buf[64];
72
73     LoadStringW(hInst, id, buf, sizeof(buf)/sizeof(WCHAR));
74     SendMessageW(status, WM_SETTEXT, 0, (LPARAM)buf);
75 }
76
77 static void set_registry(LPCSTR install_dir)
78 {
79     LPWSTR gecko_path;
80     HKEY hkey;
81     DWORD res, len, size;
82
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};
88
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: %ld\n", res);
93         return;
94     }
95
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));
99
100     if (len && gecko_path[len-1] != '\\')
101         gecko_path[len++] = '\\';
102
103     memcpy(gecko_path+len, wszWineGecko, sizeof(wszWineGecko));
104
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: %08lx\n", res);
112 }
113
114 static HRESULT WINAPI InstallCallback_QueryInterface(IBindStatusCallback *iface,
115                                                      REFIID riid, void **ppv)
116 {
117     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IBindStatusCallback, riid)) {
118         *ppv = iface;
119         return S_OK;
120     }
121
122     return E_INVALIDARG;
123 }
124
125 static ULONG WINAPI InstallCallback_AddRef(IBindStatusCallback *iface)
126 {
127     return 2;
128 }
129
130 static ULONG WINAPI InstallCallback_Release(IBindStatusCallback *iface)
131 {
132     return 1;
133 }
134
135 static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
136         DWORD dwReserved, IBinding *pib)
137 {
138     WCHAR tmp_dir[MAX_PATH];
139
140     set_status(IDS_DOWNLOADING);
141
142     GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);
143
144     tmp_file_name = mshtml_alloc(MAX_PATH*sizeof(WCHAR));
145     GetTempFileNameW(tmp_dir, NULL, 0, tmp_file_name);
146
147     TRACE("creating temp file %s\n", debugstr_w(tmp_file_name));
148
149     tmp_file = CreateFileW(tmp_file_name, GENERIC_WRITE, 0, NULL, 
150                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
151
152     if(tmp_file == INVALID_HANDLE_VALUE) {
153         ERR("Could not create file: %ld\n", GetLastError());
154         clean_up();
155         return E_FAIL;
156     }
157
158     return S_OK;
159 }
160
161 static HRESULT WINAPI InstallCallback_GetPriority(IBindStatusCallback *iface,
162         LONG *pnPriority)
163 {
164     return E_NOTIMPL;
165 }
166
167 static HRESULT WINAPI InstallCallback_OnLowResource(IBindStatusCallback *iface,
168        DWORD dwReserved)
169 {
170     return E_NOTIMPL;
171 }
172
173 static HRESULT WINAPI InstallCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
174         ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
175 {
176     HWND progress = GetDlgItem(install_dialog, ID_DWL_PROGRESS);
177
178     if(ulProgressMax)
179         SendMessageW(progress, PBM_SETRANGE32, 0, ulProgressMax);
180     if(ulProgress)
181         SendMessageW(progress, PBM_SETPOS, ulProgress, 0);
182
183     return S_OK;
184 }
185
186 static HRESULT WINAPI InstallCallback_OnStopBinding(IBindStatusCallback *iface,
187         HRESULT hresult, LPCWSTR szError)
188 {
189     LPSTR file_name;
190     DWORD len;
191     HMODULE advpack;
192     char program_files[MAX_PATH];
193     typeof(ExtractFilesA) *pExtractFilesA;
194     HRESULT hres;
195
196     static const WCHAR wszAdvpack[] = {'a','d','v','p','a','c','k','.','d','l','l',0};
197
198     if(FAILED(hresult)) {
199         ERR("Binding failed %08lx\n", hresult);
200         clean_up();
201         return S_OK;
202     }
203
204     CloseHandle(tmp_file);
205     tmp_file = INVALID_HANDLE_VALUE;
206
207     set_status(IDS_INSTALLING);
208
209     advpack = LoadLibraryW(wszAdvpack);
210     pExtractFilesA = (typeof(ExtractFilesA)*)GetProcAddress(advpack, "ExtractFiles");
211
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);
215
216     GetEnvironmentVariableA("ProgramFiles", program_files, sizeof(program_files));
217
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: %08lx\n", hres);
224         clean_up();
225     }
226
227     set_registry(program_files);
228     clean_up();
229
230     return S_OK;
231 }
232
233 static HRESULT WINAPI InstallCallback_GetBindInfo(IBindStatusCallback *iface,
234         DWORD* grfBINDF, BINDINFO* pbindinfo)
235 {
236     /* FIXME */
237     *grfBINDF = 0;
238     return S_OK;
239 }
240
241 static HRESULT WINAPI InstallCallback_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
242         DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
243 {
244     IStream *str = pstgmed->u.pstm;
245     BYTE buf[1024];
246     DWORD size;
247     HRESULT hres;
248
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);
255
256     return S_OK;
257 }
258
259 static HRESULT WINAPI InstallCallback_OnObjectAvailable(IBindStatusCallback *iface,
260         REFIID riid, IUnknown* punk)
261 {
262     ERR("\n");
263     return E_NOTIMPL;
264 }
265
266 static 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
278 };
279
280 static IBindStatusCallback InstallCallback = { &InstallCallbackVtbl };
281
282 static LPWSTR get_url(void)
283 {
284     HKEY hkey;
285     DWORD res, type;
286     DWORD size = 512*sizeof(WCHAR);
287     LPWSTR url;
288
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};
293
294     /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
295     res = RegOpenKeyW(HKEY_CURRENT_USER, wszMshtmlKey, &hkey);
296     if(res != ERROR_SUCCESS)
297         return NULL;
298
299     url = mshtml_alloc(size);
300
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;
306     }
307
308     return url;
309 }
310
311 static DWORD WINAPI download_proc(PVOID arg)
312 {
313     IMoniker *mon;
314     IBindCtx *bctx;
315     IStream *str = NULL;
316     HRESULT hres;
317
318     CreateURLMoniker(NULL, url, &mon);
319     mshtml_free(url);
320     url = NULL;
321
322     CreateAsyncBindCtx(0, &InstallCallback, 0, &bctx);
323
324     hres = IMoniker_BindToStorage(mon, bctx, NULL, &IID_IStream, (void**)&str);
325     IBindCtx_Release(bctx);
326     if(FAILED(hres)) {
327         ERR("BindToStorage failed: %08lx\n", hres);
328         return 0;
329     }
330
331     if(str)
332         IStream_Release(str);
333
334     return 0;
335 }
336
337 static INT_PTR CALLBACK installer_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
338 {
339     switch(msg) {
340     case WM_INITDIALOG:
341         install_dialog = hwnd;
342         return TRUE;
343
344     case WM_COMMAND:
345         switch(wParam) {
346         case IDCANCEL:
347             EndDialog(hwnd, 0);
348             return FALSE;
349
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;
355         }
356     }
357
358     return FALSE;
359 }
360
361 void install_wine_gecko(void)
362 {
363     HANDLE hsem;
364
365     SetLastError(ERROR_SUCCESS);
366     hsem = CreateSemaphoreA( NULL, 0, 1, "mshtml_install_semaphore");
367
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);
373     }
374
375     ReleaseSemaphore(hsem, 1, NULL);
376     CloseHandle(hsem);
377 }