2 * Copyright 2006-2010 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
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
48 #include "wine/debug.h"
49 #include "wine/unicode.h"
50 #include "wine/library.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(appwizcpl
);
54 #define GECKO_VERSION "1.1.0"
57 #define ARCH_STRING "x86"
58 #define GECKO_SHA "1b6c637207b6f032ae8a52841db9659433482714"
59 #elif defined(__x86_64__)
60 #define ARCH_STRING "x86_64"
61 #define GECKO_SHA "55b4b60cd2a48631d6236fb411c3a94d806d9906"
63 #define ARCH_STRING ""
64 #define GECKO_SHA "???"
67 #define GECKO_FILE_NAME "wine_gecko-" GECKO_VERSION "-" ARCH_STRING ".cab"
69 static const WCHAR mshtml_keyW
[] =
70 {'S','o','f','t','w','a','r','e',
72 '\\','M','S','H','T','M','L',0};
74 static HWND install_dialog
= NULL
;
75 static LPWSTR url
= NULL
;
77 static inline char *heap_strdupWtoA(LPCWSTR str
)
82 DWORD size
= WideCharToMultiByte(CP_ACP
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
83 ret
= heap_alloc(size
);
84 WideCharToMultiByte(CP_ACP
, 0, str
, -1, ret
, size
, NULL
, NULL
);
90 /* SHA definitions are copied from advapi32. They aren't available in headers. */
99 void WINAPI
A_SHAInit(PSHA_CTX
);
100 void WINAPI
A_SHAUpdate(PSHA_CTX
,const unsigned char*,UINT
);
101 void WINAPI
A_SHAFinal(PSHA_CTX
,PULONG
);
103 static BOOL
sha_check(const WCHAR
*file_name
)
105 const unsigned char *file_map
;
108 char buf
[2*sizeof(sha
)+1];
112 file
= CreateFileW(file_name
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_READONLY
, NULL
);
113 if(file
== INVALID_HANDLE_VALUE
)
116 size
= GetFileSize(file
, NULL
);
118 map
= CreateFileMappingW(file
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
123 file_map
= MapViewOfFile(map
, FILE_MAP_READ
, 0, 0, 0);
129 A_SHAUpdate(&ctx
, file_map
, size
);
130 A_SHAFinal(&ctx
, sha
);
132 UnmapViewOfFile(file_map
);
134 for(i
=0; i
< sizeof(sha
); i
++)
135 sprintf(buf
+ i
*2, "%02x", *((unsigned char*)sha
+i
));
137 if(strcmp(buf
, GECKO_SHA
)) {
140 WARN("Got %s, expected %s\n", buf
, GECKO_SHA
);
142 if(LoadStringW(hInst
, IDS_INVALID_SHA
, message
, sizeof(message
)/sizeof(WCHAR
)))
143 MessageBoxW(NULL
, message
, NULL
, MB_ICONERROR
);
151 static void set_status(DWORD id
)
153 HWND status
= GetDlgItem(install_dialog
, ID_DWL_STATUS
);
156 LoadStringW(hInst
, id
, buf
, sizeof(buf
)/sizeof(WCHAR
));
157 SendMessageW(status
, WM_SETTEXT
, 0, (LPARAM
)buf
);
160 static void set_registry(const WCHAR
*install_dir
)
162 WCHAR mshtml_key
[100];
167 static const WCHAR wszGeckoPath
[] = {'G','e','c','k','o','P','a','t','h',0};
168 static const WCHAR wszWineGecko
[] = {'w','i','n','e','_','g','e','c','k','o',0};
170 memcpy(mshtml_key
, mshtml_keyW
, sizeof(mshtml_keyW
));
171 mshtml_key
[sizeof(mshtml_keyW
)/sizeof(WCHAR
)-1] = '\\';
172 MultiByteToWideChar(CP_ACP
, 0, GECKO_VERSION
, sizeof(GECKO_VERSION
),
173 mshtml_key
+sizeof(mshtml_keyW
)/sizeof(WCHAR
),
174 (sizeof(mshtml_key
)-sizeof(mshtml_keyW
))/sizeof(WCHAR
));
176 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML\<version> */
177 res
= RegCreateKeyW(HKEY_CURRENT_USER
, mshtml_key
, &hkey
);
178 if(res
!= ERROR_SUCCESS
) {
179 ERR("Faild to create MSHTML key: %d\n", res
);
183 len
= strlenW(install_dir
);
184 gecko_path
= heap_alloc((len
+1)*sizeof(WCHAR
)+sizeof(wszWineGecko
));
185 memcpy(gecko_path
, install_dir
, len
*sizeof(WCHAR
));
187 if (len
&& gecko_path
[len
-1] != '\\')
188 gecko_path
[len
++] = '\\';
190 memcpy(gecko_path
+len
, wszWineGecko
, sizeof(wszWineGecko
));
192 res
= RegSetValueExW(hkey
, wszGeckoPath
, 0, REG_SZ
, (LPVOID
)gecko_path
,
193 len
*sizeof(WCHAR
)+sizeof(wszWineGecko
));
194 heap_free(gecko_path
);
196 if(res
!= ERROR_SUCCESS
)
197 ERR("Failed to set GeckoPath value: %08x\n", res
);
200 static BOOL
install_cab(LPCWSTR file_name
)
202 char *install_dir_a
, *file_name_a
;
203 WCHAR install_dir
[MAX_PATH
];
207 static const WCHAR gecko_subdirW
[] = {'\\','g','e','c','k','o','\\',0};
209 TRACE("(%s)\n", debugstr_w(file_name
));
211 GetSystemDirectoryW(install_dir
, sizeof(install_dir
)/sizeof(WCHAR
));
212 strcatW(install_dir
, gecko_subdirW
);
213 res
= CreateDirectoryW(install_dir
, NULL
);
214 if(!res
&& GetLastError() != ERROR_ALREADY_EXISTS
) {
215 ERR("Could not create directory: %08u\n", GetLastError());
219 len
= strlenW(install_dir
);
220 MultiByteToWideChar(CP_ACP
, 0, GECKO_VERSION
, -1, install_dir
+len
, sizeof(install_dir
)/sizeof(WCHAR
)-len
);
221 res
= CreateDirectoryW(install_dir
, NULL
);
222 if(!res
&& GetLastError() != ERROR_ALREADY_EXISTS
) {
223 ERR("Could not create directory: %08u\n", GetLastError());
228 /* FIXME: Use ExtractFilesW once it's implemented */
229 file_name_a
= heap_strdupWtoA(file_name
);
230 install_dir_a
= heap_strdupWtoA(install_dir
);
231 if(file_name_a
&& install_dir_a
)
232 hres
= ExtractFilesA(file_name_a
, install_dir_a
, 0, NULL
, NULL
, 0);
234 hres
= E_OUTOFMEMORY
;
235 heap_free(file_name_a
);
236 heap_free(install_dir_a
);
238 ERR("Could not extract package: %08x\n", hres
);
242 set_registry(install_dir
);
246 static BOOL
install_msi_file(const WCHAR
*file_name
)
250 res
= MsiInstallProductW(file_name
, NULL
);
251 if(res
!= ERROR_SUCCESS
) {
252 ERR("MsiInstallProduct failed: %u\n", res
);
259 static BOOL
install_file(const WCHAR
*file_name
)
266 static const BYTE msi_magic
[] = {0xd0,0xcf,0x11,0xe0};
268 file
= CreateFileW(file_name
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_READONLY
, NULL
);
269 if(file
== INVALID_HANDLE_VALUE
)
272 res
= ReadFile(file
, magic
, sizeof(magic
), &size
, NULL
);
274 if(!res
|| size
!= sizeof(magic
))
275 return INET_E_DOWNLOAD_FAILURE
;
277 if(!memcmp(magic
, "MSCF", sizeof(magic
)))
278 return install_cab(file_name
);
279 else if(!memcmp(magic
, msi_magic
, sizeof(magic
)))
280 return install_msi_file(file_name
);
282 ERR("Unknown file magic\n");
286 static BOOL
install_from_unix_file(const char *file_name
)
288 LPWSTR dos_file_name
;
292 static WCHAR
* (CDECL
*wine_get_dos_file_name
)(const char*);
293 static const WCHAR kernel32W
[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
295 fd
= open(file_name
, O_RDONLY
);
297 TRACE("%s not found\n", debugstr_a(file_name
));
303 if(!wine_get_dos_file_name
)
304 wine_get_dos_file_name
= (void*)GetProcAddress(GetModuleHandleW(kernel32W
), "wine_get_dos_file_name");
306 if(wine_get_dos_file_name
) { /* Wine UNIX mode */
307 dos_file_name
= wine_get_dos_file_name(file_name
);
309 ERR("Could not get dos file name of %s\n", debugstr_a(file_name
));
312 } else { /* Windows mode */
314 WARN("Could not get wine_get_dos_file_name function, calling install_cab directly.\n");
315 res
= MultiByteToWideChar( CP_ACP
, 0, file_name
, -1, 0, 0);
316 dos_file_name
= heap_alloc (res
*sizeof(WCHAR
));
317 MultiByteToWideChar( CP_ACP
, 0, file_name
, -1, dos_file_name
, res
);
320 ret
= install_file(dos_file_name
);
322 heap_free(dos_file_name
);
326 static BOOL
install_from_registered_dir(void)
330 DWORD res
, type
, size
= MAX_PATH
;
333 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
334 res
= RegOpenKeyW(HKEY_CURRENT_USER
, mshtml_keyW
, &hkey
);
335 if(res
!= ERROR_SUCCESS
)
338 file_name
= heap_alloc(size
+sizeof(GECKO_FILE_NAME
));
339 res
= RegGetValueA(hkey
, NULL
, "GeckoCabDir", RRF_RT_ANY
, &type
, (PBYTE
)file_name
, &size
);
340 if(res
== ERROR_MORE_DATA
) {
341 file_name
= heap_realloc(file_name
, size
+sizeof(GECKO_FILE_NAME
));
342 res
= RegGetValueA(hkey
, NULL
, "GeckoCabDir", RRF_RT_ANY
, &type
, (PBYTE
)file_name
, &size
);
345 if(res
!= ERROR_SUCCESS
|| (type
!= REG_SZ
&& type
!= REG_EXPAND_SZ
)) {
346 heap_free(file_name
);
350 strcat(file_name
, GECKO_FILE_NAME
);
352 TRACE("Trying %s\n", debugstr_a(file_name
));
354 ret
= install_from_unix_file(file_name
);
356 heap_free(file_name
);
360 static BOOL
install_from_default_dir(void)
362 const char *data_dir
, *subdir
;
367 if((data_dir
= wine_get_data_dir()))
369 else if((data_dir
= wine_get_build_dir()))
370 subdir
= "/../gecko/";
374 len
= strlen(data_dir
);
375 len2
= strlen(subdir
);
377 file_name
= heap_alloc(len
+len2
+sizeof(GECKO_FILE_NAME
));
378 memcpy(file_name
, data_dir
, len
);
379 memcpy(file_name
+len
, subdir
, len2
);
380 memcpy(file_name
+len
+len2
, GECKO_FILE_NAME
, sizeof(GECKO_FILE_NAME
));
382 ret
= install_from_unix_file(file_name
);
384 heap_free(file_name
);
387 ret
= install_from_unix_file(INSTALL_DATADIR
"/wine/gecko/" GECKO_FILE_NAME
);
388 if (!ret
&& strcmp(INSTALL_DATADIR
, "/usr/share"))
389 ret
= install_from_unix_file("/usr/share/wine/gecko/" GECKO_FILE_NAME
);
393 static HRESULT WINAPI
InstallCallback_QueryInterface(IBindStatusCallback
*iface
,
394 REFIID riid
, void **ppv
)
396 if(IsEqualGUID(&IID_IUnknown
, riid
) || IsEqualGUID(&IID_IBindStatusCallback
, riid
)) {
404 static ULONG WINAPI
InstallCallback_AddRef(IBindStatusCallback
*iface
)
409 static ULONG WINAPI
InstallCallback_Release(IBindStatusCallback
*iface
)
414 static HRESULT WINAPI
InstallCallback_OnStartBinding(IBindStatusCallback
*iface
,
415 DWORD dwReserved
, IBinding
*pib
)
417 set_status(IDS_DOWNLOADING
);
421 static HRESULT WINAPI
InstallCallback_GetPriority(IBindStatusCallback
*iface
,
427 static HRESULT WINAPI
InstallCallback_OnLowResource(IBindStatusCallback
*iface
,
433 static HRESULT WINAPI
InstallCallback_OnProgress(IBindStatusCallback
*iface
, ULONG ulProgress
,
434 ULONG ulProgressMax
, ULONG ulStatusCode
, LPCWSTR szStatusText
)
436 HWND progress
= GetDlgItem(install_dialog
, ID_DWL_PROGRESS
);
439 SendMessageW(progress
, PBM_SETRANGE32
, 0, ulProgressMax
);
441 SendMessageW(progress
, PBM_SETPOS
, ulProgress
, 0);
446 static HRESULT WINAPI
InstallCallback_OnStopBinding(IBindStatusCallback
*iface
,
447 HRESULT hresult
, LPCWSTR szError
)
449 if(FAILED(hresult
)) {
450 ERR("Binding failed %08x\n", hresult
);
454 set_status(IDS_INSTALLING
);
458 static HRESULT WINAPI
InstallCallback_GetBindInfo(IBindStatusCallback
*iface
,
459 DWORD
* grfBINDF
, BINDINFO
* pbindinfo
)
466 static HRESULT WINAPI
InstallCallback_OnDataAvailable(IBindStatusCallback
*iface
, DWORD grfBSCF
,
467 DWORD dwSize
, FORMATETC
* pformatetc
, STGMEDIUM
* pstgmed
)
473 static HRESULT WINAPI
InstallCallback_OnObjectAvailable(IBindStatusCallback
*iface
,
474 REFIID riid
, IUnknown
* punk
)
480 static const IBindStatusCallbackVtbl InstallCallbackVtbl
= {
481 InstallCallback_QueryInterface
,
482 InstallCallback_AddRef
,
483 InstallCallback_Release
,
484 InstallCallback_OnStartBinding
,
485 InstallCallback_GetPriority
,
486 InstallCallback_OnLowResource
,
487 InstallCallback_OnProgress
,
488 InstallCallback_OnStopBinding
,
489 InstallCallback_GetBindInfo
,
490 InstallCallback_OnDataAvailable
,
491 InstallCallback_OnObjectAvailable
494 static IBindStatusCallback InstallCallback
= { &InstallCallbackVtbl
};
496 static LPWSTR
get_url(void)
500 DWORD size
= INTERNET_MAX_URL_LENGTH
*sizeof(WCHAR
);
504 static const WCHAR wszGeckoUrl
[] = {'G','e','c','k','o','U','r','l',0};
505 static const WCHAR httpW
[] = {'h','t','t','p'};
506 static const WCHAR arch_formatW
[] = {'?','a','r','c','h','='};
507 static const WCHAR v_formatW
[] = {'&','v','='};
509 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
510 res
= RegOpenKeyW(HKEY_CURRENT_USER
, mshtml_keyW
, &hkey
);
511 if(res
!= ERROR_SUCCESS
)
514 url
= heap_alloc(size
);
515 returned_size
= size
;
517 res
= RegQueryValueExW(hkey
, wszGeckoUrl
, NULL
, &type
, (LPBYTE
)url
, &returned_size
);
519 if(res
!= ERROR_SUCCESS
|| type
!= REG_SZ
) {
524 if(returned_size
> sizeof(httpW
) && !memcmp(url
, httpW
, sizeof(httpW
))) {
528 memcpy(url
+len
, arch_formatW
, sizeof(arch_formatW
));
529 len
+= sizeof(arch_formatW
)/sizeof(WCHAR
);
530 len
+= MultiByteToWideChar(CP_ACP
, 0, ARCH_STRING
, sizeof(ARCH_STRING
), url
+len
, size
/sizeof(WCHAR
)-len
)-1;
531 memcpy(url
+len
, v_formatW
, sizeof(v_formatW
));
532 len
+= sizeof(v_formatW
)/sizeof(WCHAR
);
533 MultiByteToWideChar(CP_ACP
, 0, GECKO_VERSION
, -1, url
+len
, size
/sizeof(WCHAR
)-len
);
536 TRACE("Got URL %s\n", debugstr_w(url
));
540 static DWORD WINAPI
download_proc(PVOID arg
)
542 WCHAR tmp_dir
[MAX_PATH
], tmp_file
[MAX_PATH
];
545 GetTempPathW(sizeof(tmp_dir
)/sizeof(WCHAR
), tmp_dir
);
546 GetTempFileNameW(tmp_dir
, NULL
, 0, tmp_file
);
548 TRACE("using temp file %s\n", debugstr_w(tmp_file
));
550 hres
= URLDownloadToFileW(NULL
, url
, tmp_file
, 0, &InstallCallback
);
552 ERR("URLDownloadToFile failed: %08x\n", hres
);
556 if(sha_check(tmp_file
))
557 install_file(tmp_file
);
558 DeleteFileW(tmp_file
);
559 EndDialog(install_dialog
, 0);
563 static INT_PTR CALLBACK
installer_proc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
567 ShowWindow(GetDlgItem(hwnd
, ID_DWL_PROGRESS
), SW_HIDE
);
568 install_dialog
= hwnd
;
578 ShowWindow(GetDlgItem(hwnd
, ID_DWL_PROGRESS
), SW_SHOW
);
579 EnableWindow(GetDlgItem(hwnd
, ID_DWL_INSTALL
), 0);
580 EnableWindow(GetDlgItem(hwnd
, IDCANCEL
), 0); /* FIXME */
581 CreateThread(NULL
, 0, download_proc
, NULL
, 0, NULL
);
589 BOOL
install_wine_gecko(void)
595 * Try to find Gecko .cab file in following order:
596 * - directory stored in GeckoCabDir value of HKCU/Wine/Software/MSHTML key
598 * - $INSTALL_DATADIR/wine/gecko/
599 * - /usr/share/wine/gecko/
600 * - download from URL stored in GeckoUrl value of HKCU/Wine/Software/MSHTML key
602 if(!install_from_registered_dir()
603 && !install_from_default_dir()
604 && (url
= get_url()))
605 DialogBoxW(hInst
, MAKEINTRESOURCEW(ID_DWL_DIALOG
), 0, installer_proc
);