2 * Copyright 2006-2007 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
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
41 #include "wine/debug.h"
42 #include "wine/unicode.h"
43 #include "wine/library.h"
45 #include "mshtml_private.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
51 #define GECKO_ARCH "x86"
52 #elif defined(__x86_64__)
53 #define GECKO_ARCH "x86_64"
58 #define GECKO_FILE_NAME "wine_gecko-" GECKO_VERSION "-" GECKO_ARCH ".cab"
60 static const WCHAR mshtml_keyW
[] =
61 {'S','o','f','t','w','a','r','e',
63 '\\','M','S','H','T','M','L',0};
65 static HWND install_dialog
= NULL
;
66 static LPWSTR url
= NULL
;
68 static void set_status(DWORD id
)
70 HWND status
= GetDlgItem(install_dialog
, ID_DWL_STATUS
);
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 WCHAR mshtml_key
[100];
84 static const WCHAR wszGeckoPath
[] = {'G','e','c','k','o','P','a','t','h',0};
85 static const WCHAR wszWineGecko
[] = {'w','i','n','e','_','g','e','c','k','o',0};
87 memcpy(mshtml_key
, mshtml_keyW
, sizeof(mshtml_keyW
));
88 mshtml_key
[sizeof(mshtml_keyW
)/sizeof(WCHAR
)-1] = '\\';
89 MultiByteToWideChar(CP_ACP
, 0, GECKO_VERSION
, sizeof(GECKO_VERSION
),
90 mshtml_key
+sizeof(mshtml_keyW
)/sizeof(WCHAR
),
91 (sizeof(mshtml_key
)-sizeof(mshtml_keyW
))/sizeof(WCHAR
));
93 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML\<version> */
94 res
= RegCreateKeyW(HKEY_CURRENT_USER
, mshtml_key
, &hkey
);
95 if(res
!= ERROR_SUCCESS
) {
96 ERR("Faild to create MSHTML key: %d\n", res
);
100 len
= MultiByteToWideChar(CP_ACP
, 0, install_dir
, -1, NULL
, 0)-1;
101 gecko_path
= heap_alloc((len
+1)*sizeof(WCHAR
)+sizeof(wszWineGecko
));
102 MultiByteToWideChar(CP_ACP
, 0, install_dir
, -1, gecko_path
, len
+1);
104 if (len
&& gecko_path
[len
-1] != '\\')
105 gecko_path
[len
++] = '\\';
107 memcpy(gecko_path
+len
, wszWineGecko
, sizeof(wszWineGecko
));
109 res
= RegSetValueExW(hkey
, wszGeckoPath
, 0, REG_SZ
, (LPVOID
)gecko_path
,
110 len
*sizeof(WCHAR
)+sizeof(wszWineGecko
));
111 heap_free(gecko_path
);
113 if(res
!= ERROR_SUCCESS
)
114 ERR("Failed to set GeckoPath value: %08x\n", res
);
117 static BOOL
install_cab(LPCWSTR file_name
)
120 char install_dir
[MAX_PATH
];
121 HRESULT (WINAPI
*pExtractFilesA
)(LPCSTR
,LPCSTR
,DWORD
,LPCSTR
,LPVOID
,DWORD
);
126 static const WCHAR wszAdvpack
[] = {'a','d','v','p','a','c','k','.','d','l','l',0};
128 TRACE("(%s)\n", debugstr_w(file_name
));
130 GetSystemDirectoryA(install_dir
, sizeof(install_dir
));
131 strcat(install_dir
, "\\gecko\\");
132 res
= CreateDirectoryA(install_dir
, NULL
);
133 if(!res
&& GetLastError() != ERROR_ALREADY_EXISTS
) {
134 ERR("Could not create directory: %08u\n", GetLastError());
138 strcat(install_dir
, GECKO_VERSION
);
139 res
= CreateDirectoryA(install_dir
, NULL
);
140 if(!res
&& GetLastError() != ERROR_ALREADY_EXISTS
) {
141 ERR("Could not create directory: %08u\n", GetLastError());
145 advpack
= LoadLibraryW(wszAdvpack
);
146 pExtractFilesA
= (void *)GetProcAddress(advpack
, "ExtractFiles");
148 /* FIXME: Use unicode version (not yet implemented) */
149 file_name_a
= heap_strdupWtoA(file_name
);
150 hres
= pExtractFilesA(file_name_a
, install_dir
, 0, NULL
, NULL
, 0);
151 FreeLibrary(advpack
);
152 heap_free(file_name_a
);
154 ERR("Could not extract package: %08x\n", hres
);
158 set_registry(install_dir
);
162 static BOOL
install_from_unix_file(const char *file_name
)
164 LPWSTR dos_file_name
;
168 static WCHAR
* (CDECL
*wine_get_dos_file_name
)(const char*);
169 static const WCHAR kernel32W
[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
171 fd
= open(file_name
, O_RDONLY
);
173 TRACE("%s not found\n", debugstr_a(file_name
));
179 if(!wine_get_dos_file_name
)
180 wine_get_dos_file_name
= (void*)GetProcAddress(GetModuleHandleW(kernel32W
), "wine_get_dos_file_name");
182 if(wine_get_dos_file_name
) { /* Wine UNIX mode */
183 dos_file_name
= wine_get_dos_file_name(file_name
);
185 ERR("Could not get dos file name of %s\n", debugstr_a(file_name
));
188 } else { /* Windows mode */
190 WARN("Could not get wine_get_dos_file_name function, calling install_cab directly.\n");
191 res
= MultiByteToWideChar( CP_ACP
, 0, file_name
, -1, 0, 0);
192 dos_file_name
= heap_alloc (res
*sizeof(WCHAR
));
193 MultiByteToWideChar( CP_ACP
, 0, file_name
, -1, dos_file_name
, res
);
196 ret
= install_cab(dos_file_name
);
198 heap_free(dos_file_name
);
202 static BOOL
install_from_registered_dir(void)
206 DWORD res
, type
, size
= MAX_PATH
;
209 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
210 res
= RegOpenKeyW(HKEY_CURRENT_USER
, mshtml_keyW
, &hkey
);
211 if(res
!= ERROR_SUCCESS
)
214 file_name
= heap_alloc(size
+sizeof(GECKO_FILE_NAME
));
215 res
= RegGetValueA(hkey
, NULL
, "GeckoCabDir", RRF_RT_ANY
, &type
, (PBYTE
)file_name
, &size
);
216 if(res
== ERROR_MORE_DATA
) {
217 file_name
= heap_realloc(file_name
, size
+sizeof(GECKO_FILE_NAME
));
218 res
= RegGetValueA(hkey
, NULL
, "GeckoCabDir", RRF_RT_ANY
, &type
, (PBYTE
)file_name
, &size
);
221 if(res
!= ERROR_SUCCESS
|| (type
!= REG_SZ
&& type
!= REG_EXPAND_SZ
)) {
222 heap_free(file_name
);
226 strcat(file_name
, GECKO_FILE_NAME
);
228 TRACE("Trying %s\n", debugstr_a(file_name
));
230 ret
= install_from_unix_file(file_name
);
232 heap_free(file_name
);
236 static BOOL
install_from_default_dir(void)
238 const char *data_dir
, *subdir
;
243 if((data_dir
= wine_get_data_dir()))
245 else if((data_dir
= wine_get_build_dir()))
246 subdir
= "/../gecko/";
250 len
= strlen(data_dir
);
251 len2
= strlen(subdir
);
253 file_name
= heap_alloc(len
+len2
+sizeof(GECKO_FILE_NAME
));
254 memcpy(file_name
, data_dir
, len
);
255 memcpy(file_name
+len
, subdir
, len2
);
256 memcpy(file_name
+len
+len2
, GECKO_FILE_NAME
, sizeof(GECKO_FILE_NAME
));
258 ret
= install_from_unix_file(file_name
);
260 heap_free(file_name
);
263 ret
= install_from_unix_file( GECKO_DATADIR
"/wine/gecko/" GECKO_FILE_NAME
);
264 if (!ret
&& strcmp( GECKO_DATADIR
, "/usr/share" ))
265 ret
= install_from_unix_file("/usr/share/wine/gecko/" GECKO_FILE_NAME
);
269 static HRESULT WINAPI
InstallCallback_QueryInterface(IBindStatusCallback
*iface
,
270 REFIID riid
, void **ppv
)
272 if(IsEqualGUID(&IID_IUnknown
, riid
) || IsEqualGUID(&IID_IBindStatusCallback
, riid
)) {
280 static ULONG WINAPI
InstallCallback_AddRef(IBindStatusCallback
*iface
)
285 static ULONG WINAPI
InstallCallback_Release(IBindStatusCallback
*iface
)
290 static HRESULT WINAPI
InstallCallback_OnStartBinding(IBindStatusCallback
*iface
,
291 DWORD dwReserved
, IBinding
*pib
)
293 set_status(IDS_DOWNLOADING
);
297 static HRESULT WINAPI
InstallCallback_GetPriority(IBindStatusCallback
*iface
,
303 static HRESULT WINAPI
InstallCallback_OnLowResource(IBindStatusCallback
*iface
,
309 static HRESULT WINAPI
InstallCallback_OnProgress(IBindStatusCallback
*iface
, ULONG ulProgress
,
310 ULONG ulProgressMax
, ULONG ulStatusCode
, LPCWSTR szStatusText
)
312 HWND progress
= GetDlgItem(install_dialog
, ID_DWL_PROGRESS
);
315 SendMessageW(progress
, PBM_SETRANGE32
, 0, ulProgressMax
);
317 SendMessageW(progress
, PBM_SETPOS
, ulProgress
, 0);
322 static HRESULT WINAPI
InstallCallback_OnStopBinding(IBindStatusCallback
*iface
,
323 HRESULT hresult
, LPCWSTR szError
)
325 if(FAILED(hresult
)) {
326 ERR("Binding failed %08x\n", hresult
);
330 set_status(IDS_INSTALLING
);
334 static HRESULT WINAPI
InstallCallback_GetBindInfo(IBindStatusCallback
*iface
,
335 DWORD
* grfBINDF
, BINDINFO
* pbindinfo
)
342 static HRESULT WINAPI
InstallCallback_OnDataAvailable(IBindStatusCallback
*iface
, DWORD grfBSCF
,
343 DWORD dwSize
, FORMATETC
* pformatetc
, STGMEDIUM
* pstgmed
)
349 static HRESULT WINAPI
InstallCallback_OnObjectAvailable(IBindStatusCallback
*iface
,
350 REFIID riid
, IUnknown
* punk
)
356 static const IBindStatusCallbackVtbl InstallCallbackVtbl
= {
357 InstallCallback_QueryInterface
,
358 InstallCallback_AddRef
,
359 InstallCallback_Release
,
360 InstallCallback_OnStartBinding
,
361 InstallCallback_GetPriority
,
362 InstallCallback_OnLowResource
,
363 InstallCallback_OnProgress
,
364 InstallCallback_OnStopBinding
,
365 InstallCallback_GetBindInfo
,
366 InstallCallback_OnDataAvailable
,
367 InstallCallback_OnObjectAvailable
370 static IBindStatusCallback InstallCallback
= { &InstallCallbackVtbl
};
372 static LPWSTR
get_url(void)
376 DWORD size
= INTERNET_MAX_URL_LENGTH
*sizeof(WCHAR
);
380 static const WCHAR wszGeckoUrl
[] = {'G','e','c','k','o','U','r','l',0};
381 static const WCHAR httpW
[] = {'h','t','t','p'};
382 static const WCHAR arch_formatW
[] = {'?','a','r','c','h','='};
383 static const WCHAR v_formatW
[] = {'&','v','='};
385 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
386 res
= RegOpenKeyW(HKEY_CURRENT_USER
, mshtml_keyW
, &hkey
);
387 if(res
!= ERROR_SUCCESS
)
390 url
= heap_alloc(size
);
391 returned_size
= size
;
393 res
= RegQueryValueExW(hkey
, wszGeckoUrl
, NULL
, &type
, (LPBYTE
)url
, &returned_size
);
395 if(res
!= ERROR_SUCCESS
|| type
!= REG_SZ
) {
400 if(returned_size
> sizeof(httpW
) && !memcmp(url
, httpW
, sizeof(httpW
))) {
404 memcpy(url
+len
, arch_formatW
, sizeof(arch_formatW
));
405 len
+= sizeof(arch_formatW
)/sizeof(WCHAR
);
406 len
+= MultiByteToWideChar(CP_ACP
, 0, GECKO_ARCH
, sizeof(GECKO_ARCH
), url
+len
, size
/sizeof(WCHAR
)-len
)-1;
407 memcpy(url
+len
, v_formatW
, sizeof(v_formatW
));
408 len
+= sizeof(v_formatW
)/sizeof(WCHAR
);
409 MultiByteToWideChar(CP_ACP
, 0, GECKO_VERSION
, -1, url
+len
, size
/sizeof(WCHAR
)-len
);
412 TRACE("Got URL %s\n", debugstr_w(url
));
416 static DWORD WINAPI
download_proc(PVOID arg
)
418 WCHAR tmp_dir
[MAX_PATH
], tmp_file
[MAX_PATH
];
421 GetTempPathW(sizeof(tmp_dir
)/sizeof(WCHAR
), tmp_dir
);
422 GetTempFileNameW(tmp_dir
, NULL
, 0, tmp_file
);
424 TRACE("using temp file %s\n", debugstr_w(tmp_file
));
426 hres
= URLDownloadToFileW(NULL
, url
, tmp_file
, 0, &InstallCallback
);
428 ERR("URLDownloadToFile failed: %08x\n", hres
);
432 install_cab(tmp_file
);
433 DeleteFileW(tmp_file
);
434 EndDialog(install_dialog
, 0);
438 static INT_PTR CALLBACK
installer_proc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
442 ShowWindow(GetDlgItem(hwnd
, ID_DWL_PROGRESS
), SW_HIDE
);
443 install_dialog
= hwnd
;
453 ShowWindow(GetDlgItem(hwnd
, ID_DWL_PROGRESS
), SW_SHOW
);
454 EnableWindow(GetDlgItem(hwnd
, ID_DWL_INSTALL
), 0);
455 EnableWindow(GetDlgItem(hwnd
, IDCANCEL
), 0); /* FIXME */
456 CreateThread(NULL
, 0, download_proc
, NULL
, 0, NULL
);
464 BOOL
install_wine_gecko(BOOL silent
)
471 SetLastError(ERROR_SUCCESS
);
472 hsem
= CreateSemaphoreA( NULL
, 0, 1, "mshtml_install_semaphore");
474 if(GetLastError() == ERROR_ALREADY_EXISTS
) {
475 WaitForSingleObject(hsem
, INFINITE
);
478 * Try to find Gecko .cab file in following order:
479 * - directory stored in GeckoCabDir value of HKCU/Wine/Software/MSHTML key
481 * - $GECKO_DATADIR/wine/gecko/
482 * - /usr/share/wine/gecko/
483 * - download from URL stored in GeckoUrl value of HKCU/Wine/Software/MSHTML key
485 if(!install_from_registered_dir()
486 && !install_from_default_dir()
487 && !silent
&& (url
= get_url()))
488 DialogBoxW(hInst
, MAKEINTRESOURCEW(ID_DWL_DIALOG
), 0, installer_proc
);
493 ReleaseSemaphore(hsem
, 1, NULL
);