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
20 #include "wine/port.h"
31 #define NONAMELESSUNION
50 #include "wine/debug.h"
51 #include "wine/library.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(appwizcpl
);
55 #define GECKO_VERSION "2.47"
58 #define ARCH_STRING "x86"
59 #define GECKO_SHA "3b8a361f5d63952d21caafd74e849a774994822fb96c5922b01d554f1677643a"
60 #elif defined(__x86_64__)
61 #define ARCH_STRING "x86_64"
62 #define GECKO_SHA "c565ea25e50ea953937d4ab01299e4306da4a556946327d253ea9b28357e4a7d"
64 #define ARCH_STRING ""
65 #define GECKO_SHA "???"
68 #define MONO_VERSION "4.7.0"
69 #define MONO_SHA "7698474dd9cb9eb80796b5812dff37386ba97b78b21ca23b20079ca5ad6ca5a1"
73 const char *file_name
;
74 const char *subdir_name
;
76 const char *url_default
;
77 const char *config_key
;
78 const char *url_config_key
;
79 const char *dir_config_key
;
80 LPCWSTR dialog_template
;
83 static const addon_info_t addons_info
[] = {
86 "wine_gecko-" GECKO_VERSION
"-" ARCH_STRING
".msi",
89 "http://source.winehq.org/winegecko.php",
90 "MSHTML", "GeckoUrl", "GeckoCabDir",
91 MAKEINTRESOURCEW(ID_DWL_GECKO_DIALOG
)
95 "wine-mono-" MONO_VERSION
".msi",
98 "http://source.winehq.org/winemono.php",
99 "Dotnet", "MonoUrl", "MonoCabDir",
100 MAKEINTRESOURCEW(ID_DWL_MONO_DIALOG
)
104 static const addon_info_t
*addon
;
106 static HWND install_dialog
= NULL
;
107 static LPWSTR url
= NULL
;
108 static IBinding
*dwl_binding
;
109 static WCHAR
*msi_file
;
111 static WCHAR
* (CDECL
*p_wine_get_dos_file_name
)(const char*);
112 static const WCHAR kernel32_dllW
[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
114 static BOOL
sha_check(const WCHAR
*file_name
)
116 const unsigned char *file_map
;
119 BCRYPT_HASH_HANDLE hash
= NULL
;
120 BCRYPT_ALG_HANDLE alg
= NULL
;
125 file
= CreateFileW(file_name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_READONLY
, NULL
);
126 if(file
== INVALID_HANDLE_VALUE
) {
127 WARN("Could not open file: %u\n", GetLastError());
131 size
= GetFileSize(file
, NULL
);
133 map
= CreateFileMappingW(file
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
138 file_map
= MapViewOfFile(map
, FILE_MAP_READ
, 0, 0, 0);
143 if(BCryptOpenAlgorithmProvider(&alg
, BCRYPT_SHA256_ALGORITHM
, MS_PRIMITIVE_PROVIDER
, 0))
145 if(BCryptCreateHash(alg
, &hash
, NULL
, 0, NULL
, 0, 0))
147 if(BCryptHashData(hash
, (UCHAR
*)file_map
, size
, 0))
149 if(BCryptFinishHash(hash
, sha
, sizeof(sha
), 0))
152 for(i
=0; i
< sizeof(sha
); i
++)
153 sprintf(buf
+ i
* 2, "%02x", sha
[i
]);
155 ret
= !strcmp(buf
, addon
->sha
);
157 WARN("Got %s, expected %s\n", buf
, addon
->sha
);
160 UnmapViewOfFile(file_map
);
161 if(hash
) BCryptDestroyHash(hash
);
162 if(alg
) BCryptCloseAlgorithmProvider(alg
, 0);
166 static void set_status(DWORD id
)
168 HWND status
= GetDlgItem(install_dialog
, ID_DWL_STATUS
);
171 LoadStringW(hInst
, id
, buf
, sizeof(buf
)/sizeof(WCHAR
));
172 SendMessageW(status
, WM_SETTEXT
, 0, (LPARAM
)buf
);
181 static enum install_res
install_file(const WCHAR
*file_name
)
185 res
= MsiInstallProductW(file_name
, NULL
);
186 if(res
!= ERROR_SUCCESS
) {
187 ERR("MsiInstallProduct failed: %u\n", res
);
188 return INSTALL_FAILED
;
194 static enum install_res
install_from_unix_file(const char *dir
, const char *subdir
, const char *file_name
)
196 LPWSTR dos_file_name
;
199 enum install_res ret
;
202 file_path
= heap_alloc(len
+strlen(subdir
)+strlen(file_name
)+3);
204 return INSTALL_FAILED
;
206 memcpy(file_path
, dir
, len
);
207 if(len
&& file_path
[len
-1] != '/' && file_path
[len
-1] != '\\')
208 file_path
[len
++] = '/';
210 strcpy(file_path
+len
, subdir
);
211 len
+= strlen(subdir
);
212 file_path
[len
++] = '/';
214 strcpy(file_path
+len
, file_name
);
216 fd
= open(file_path
, O_RDONLY
);
218 TRACE("%s not found\n", debugstr_a(file_path
));
219 heap_free(file_path
);
225 if(p_wine_get_dos_file_name
) { /* Wine UNIX mode */
226 dos_file_name
= p_wine_get_dos_file_name(file_path
);
228 ERR("Could not get dos file name of %s\n", debugstr_a(file_path
));
229 heap_free(file_path
);
230 return INSTALL_FAILED
;
232 } else { /* Windows mode */
234 WARN("Could not get wine_get_dos_file_name function, calling install_cab directly.\n");
235 res
= MultiByteToWideChar( CP_ACP
, 0, file_path
, -1, 0, 0);
236 dos_file_name
= heap_alloc (res
*sizeof(WCHAR
));
237 MultiByteToWideChar( CP_ACP
, 0, file_path
, -1, dos_file_name
, res
);
240 heap_free(file_path
);
242 ret
= install_file(dos_file_name
);
244 heap_free(dos_file_name
);
248 static HKEY
open_config_key(void)
253 static const WCHAR wine_keyW
[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e',0};
255 /* @@ Wine registry key: HKCU\Software\Wine\$config_key */
256 res
= RegOpenKeyW(HKEY_CURRENT_USER
, wine_keyW
, &hkey
);
257 if(res
!= ERROR_SUCCESS
)
260 res
= RegOpenKeyA(hkey
, addon
->config_key
, &ret
);
262 return res
== ERROR_SUCCESS
? ret
: NULL
;
265 static enum install_res
install_from_registered_dir(void)
269 DWORD res
, type
, size
= MAX_PATH
;
270 enum install_res ret
;
272 hkey
= open_config_key();
276 package_dir
= heap_alloc(size
);
277 res
= RegGetValueA(hkey
, NULL
, addon
->dir_config_key
, RRF_RT_ANY
, &type
, (PBYTE
)package_dir
, &size
);
278 if(res
== ERROR_MORE_DATA
) {
279 package_dir
= heap_realloc(package_dir
, size
);
280 res
= RegGetValueA(hkey
, NULL
, addon
->dir_config_key
, RRF_RT_ANY
, &type
, (PBYTE
)package_dir
, &size
);
283 if(res
== ERROR_FILE_NOT_FOUND
) {
284 heap_free(package_dir
);
286 } else if(res
!= ERROR_SUCCESS
|| (type
!= REG_SZ
&& type
!= REG_EXPAND_SZ
)) {
287 heap_free(package_dir
);
288 return INSTALL_FAILED
;
291 TRACE("Trying %s/%s\n", debugstr_a(package_dir
), debugstr_a(addon
->file_name
));
293 ret
= install_from_unix_file(package_dir
, "", addon
->file_name
);
295 heap_free(package_dir
);
299 static enum install_res
install_from_default_dir(void)
301 const char *data_dir
, *package_dir
;
302 char *dir_buf
= NULL
;
304 enum install_res ret
;
306 if((data_dir
= wine_get_data_dir())) {
307 package_dir
= data_dir
;
308 }else if((data_dir
= wine_get_build_dir())) {
309 len
= strlen(data_dir
);
310 dir_buf
= heap_alloc(len
+ sizeof("/../"));
311 memcpy(dir_buf
, data_dir
, len
);
312 strcpy(dir_buf
+len
, "/../");
313 package_dir
= dir_buf
;
318 ret
= install_from_unix_file(package_dir
, addon
->subdir_name
, addon
->file_name
);
321 if (ret
== INSTALL_NEXT
)
322 ret
= install_from_unix_file(INSTALL_DATADIR
"/wine/", addon
->subdir_name
, addon
->file_name
);
323 if (ret
== INSTALL_NEXT
&& strcmp(INSTALL_DATADIR
, "/usr/share"))
324 ret
= install_from_unix_file("/usr/share/wine/", addon
->subdir_name
, addon
->file_name
);
328 static WCHAR
*get_cache_file_name(BOOL ensure_exists
)
330 const char *home_dir
= NULL
, *xdg_cache_dir
;
331 size_t len
, size
= strlen(addon
->file_name
) + 7; /* strlen("/wine/"), '\0' */
332 char *cache_file_name
;
335 /* non-Wine (eg. Windows) cache is currently not supported */
336 if(!p_wine_get_dos_file_name
)
339 xdg_cache_dir
= getenv("XDG_CACHE_HOME");
340 if(xdg_cache_dir
&& *xdg_cache_dir
) {
341 size
+= strlen(xdg_cache_dir
);
343 home_dir
= getenv("HOME");
347 size
+= strlen(home_dir
) + 8; /* strlen("/.cache/") */
350 cache_file_name
= heap_alloc(size
);
354 if(xdg_cache_dir
&& *xdg_cache_dir
) {
355 len
= strlen(xdg_cache_dir
);
356 if(len
> 1 && xdg_cache_dir
[len
-1] == '/')
358 memcpy(cache_file_name
, xdg_cache_dir
, len
);
359 cache_file_name
[len
] = 0;
361 len
= strlen(home_dir
);
362 memcpy(cache_file_name
, home_dir
, len
);
363 strcpy(cache_file_name
+len
, "/.cache");
367 if(ensure_exists
&& mkdir(cache_file_name
, 0777) && errno
!= EEXIST
) {
368 WARN("%s does not exist and could not be created: %s\n", cache_file_name
, strerror(errno
));
369 heap_free(cache_file_name
);
373 strcpy(cache_file_name
+len
, "/wine");
376 if(ensure_exists
&& mkdir(cache_file_name
, 0777) && errno
!= EEXIST
) {
377 WARN("%s does not exist and could not be created: %s\n", cache_file_name
, strerror(errno
));
381 cache_file_name
[len
++] = '/';
382 strcpy(cache_file_name
+len
, addon
->file_name
);
383 ret
= p_wine_get_dos_file_name(cache_file_name
);
385 TRACE("%s -> %s\n", cache_file_name
, debugstr_w(ret
));
387 heap_free(cache_file_name
);
391 static enum install_res
install_from_cache(void)
393 WCHAR
*cache_file_name
;
394 enum install_res res
;
396 cache_file_name
= get_cache_file_name(FALSE
);
400 if(!sha_check(cache_file_name
)) {
401 WARN("could not validate checksum\n");
402 DeleteFileW(cache_file_name
);
403 heap_free(cache_file_name
);
407 res
= install_file(cache_file_name
);
408 heap_free(cache_file_name
);
412 static IInternetBindInfo InstallCallbackBindInfo
;
414 static HRESULT WINAPI
InstallCallback_QueryInterface(IBindStatusCallback
*iface
,
415 REFIID riid
, void **ppv
)
417 if(IsEqualGUID(&IID_IUnknown
, riid
) || IsEqualGUID(&IID_IBindStatusCallback
, riid
)) {
422 if(IsEqualGUID(&IID_IInternetBindInfo
, riid
)) {
423 TRACE("IID_IInternetBindInfo\n");
424 *ppv
= &InstallCallbackBindInfo
;
431 static ULONG WINAPI
InstallCallback_AddRef(IBindStatusCallback
*iface
)
436 static ULONG WINAPI
InstallCallback_Release(IBindStatusCallback
*iface
)
441 static HRESULT WINAPI
InstallCallback_OnStartBinding(IBindStatusCallback
*iface
,
442 DWORD dwReserved
, IBinding
*pib
)
444 set_status(IDS_DOWNLOADING
);
446 IBinding_AddRef(pib
);
452 static HRESULT WINAPI
InstallCallback_GetPriority(IBindStatusCallback
*iface
,
458 static HRESULT WINAPI
InstallCallback_OnLowResource(IBindStatusCallback
*iface
,
464 static HRESULT WINAPI
InstallCallback_OnProgress(IBindStatusCallback
*iface
, ULONG ulProgress
,
465 ULONG ulProgressMax
, ULONG ulStatusCode
, LPCWSTR szStatusText
)
467 HWND progress
= GetDlgItem(install_dialog
, ID_DWL_PROGRESS
);
470 SendMessageW(progress
, PBM_SETRANGE32
, 0, ulProgressMax
);
472 SendMessageW(progress
, PBM_SETPOS
, ulProgress
, 0);
477 static HRESULT WINAPI
InstallCallback_OnStopBinding(IBindStatusCallback
*iface
,
478 HRESULT hresult
, LPCWSTR szError
)
481 IBinding_Release(dwl_binding
);
485 if(FAILED(hresult
)) {
486 if(hresult
== E_ABORT
)
487 TRACE("Binding aborted\n");
489 ERR("Binding failed %08x\n", hresult
);
494 ERR("No MSI file\n");
498 set_status(IDS_INSTALLING
);
499 EnableWindow(GetDlgItem(install_dialog
, IDCANCEL
), 0);
501 if(sha_check(msi_file
)) {
502 WCHAR
*cache_file_name
;
504 install_file(msi_file
);
506 cache_file_name
= get_cache_file_name(TRUE
);
507 if(cache_file_name
) {
508 MoveFileW(msi_file
, cache_file_name
);
509 heap_free(cache_file_name
);
514 if(LoadStringW(hInst
, IDS_INVALID_SHA
, message
, sizeof(message
)/sizeof(WCHAR
)))
515 MessageBoxW(NULL
, message
, NULL
, MB_ICONERROR
);
518 DeleteFileW(msi_file
);
522 EndDialog(install_dialog
, 0);
526 static HRESULT WINAPI
InstallCallback_GetBindInfo(IBindStatusCallback
*iface
,
527 DWORD
* grfBINDF
, BINDINFO
* pbindinfo
)
531 *grfBINDF
= BINDF_ASYNCHRONOUS
;
535 static HRESULT WINAPI
InstallCallback_OnDataAvailable(IBindStatusCallback
*iface
, DWORD grfBSCF
,
536 DWORD dwSize
, FORMATETC
* pformatetc
, STGMEDIUM
* pstgmed
)
539 msi_file
= heap_strdupW(pstgmed
->u
.lpszFileName
);
540 TRACE("got file name %s\n", debugstr_w(msi_file
));
546 static HRESULT WINAPI
InstallCallback_OnObjectAvailable(IBindStatusCallback
*iface
,
547 REFIID riid
, IUnknown
* punk
)
553 static const IBindStatusCallbackVtbl InstallCallbackVtbl
= {
554 InstallCallback_QueryInterface
,
555 InstallCallback_AddRef
,
556 InstallCallback_Release
,
557 InstallCallback_OnStartBinding
,
558 InstallCallback_GetPriority
,
559 InstallCallback_OnLowResource
,
560 InstallCallback_OnProgress
,
561 InstallCallback_OnStopBinding
,
562 InstallCallback_GetBindInfo
,
563 InstallCallback_OnDataAvailable
,
564 InstallCallback_OnObjectAvailable
567 static IBindStatusCallback InstallCallback
= { &InstallCallbackVtbl
};
569 static HRESULT WINAPI
InstallCallbackBindInfo_QueryInterface(IInternetBindInfo
*iface
, REFIID riid
, void **ppv
)
571 return IBindStatusCallback_QueryInterface(&InstallCallback
, riid
, ppv
);
574 static ULONG WINAPI
InstallCallbackBindInfo_AddRef(IInternetBindInfo
*iface
)
579 static ULONG WINAPI
InstallCallbackBindInfo_Release(IInternetBindInfo
*iface
)
584 static HRESULT WINAPI
InstallCallbackBindInfo_GetBindInfo(IInternetBindInfo
*iface
, DWORD
*bindf
, BINDINFO
*bindinfo
)
590 static HRESULT WINAPI
InstallCallbackBindInfo_GetBindString(IInternetBindInfo
*iface
, ULONG string_type
,
591 WCHAR
**strs
, ULONG cnt
, ULONG
*fetched
)
593 static const WCHAR wine_addon_downloaderW
[] =
594 {'W','i','n','e',' ','A','d','d','o','n',' ','D','o','w','n','l','o','a','d','e','r',0};
596 switch(string_type
) {
597 case BINDSTRING_USER_AGENT
:
598 TRACE("BINDSTRING_USER_AGENT\n");
600 *strs
= CoTaskMemAlloc(sizeof(wine_addon_downloaderW
));
602 return E_OUTOFMEMORY
;
604 memcpy(*strs
, wine_addon_downloaderW
, sizeof(wine_addon_downloaderW
));
612 static const IInternetBindInfoVtbl InstallCallbackBindInfoVtbl
= {
613 InstallCallbackBindInfo_QueryInterface
,
614 InstallCallbackBindInfo_AddRef
,
615 InstallCallbackBindInfo_Release
,
616 InstallCallbackBindInfo_GetBindInfo
,
617 InstallCallbackBindInfo_GetBindString
620 static IInternetBindInfo InstallCallbackBindInfo
= { &InstallCallbackBindInfoVtbl
};
622 static void append_url_params( WCHAR
*url
)
624 static const WCHAR arch_formatW
[] = {'?','a','r','c','h','='};
625 static const WCHAR v_formatW
[] = {'&','v','='};
626 static const WCHAR winevW
[] = {'&','w','i','n','e','v','='};
627 DWORD size
= INTERNET_MAX_URL_LENGTH
* sizeof(WCHAR
);
628 DWORD len
= strlenW(url
);
630 memcpy(url
+len
, arch_formatW
, sizeof(arch_formatW
));
631 len
+= sizeof(arch_formatW
)/sizeof(WCHAR
);
632 len
+= MultiByteToWideChar(CP_ACP
, 0, ARCH_STRING
, sizeof(ARCH_STRING
),
633 url
+len
, size
/sizeof(WCHAR
)-len
)-1;
634 memcpy(url
+len
, v_formatW
, sizeof(v_formatW
));
635 len
+= sizeof(v_formatW
)/sizeof(WCHAR
);
636 len
+= MultiByteToWideChar(CP_ACP
, 0, addon
->version
, -1, url
+len
, size
/sizeof(WCHAR
)-len
)-1;
637 memcpy(url
+len
, winevW
, sizeof(winevW
));
638 len
+= sizeof(winevW
)/sizeof(WCHAR
);
639 MultiByteToWideChar(CP_ACP
, 0, PACKAGE_VERSION
, -1, url
+len
, size
/sizeof(WCHAR
)-len
);
642 static LPWSTR
get_url(void)
644 DWORD size
= INTERNET_MAX_URL_LENGTH
*sizeof(WCHAR
);
645 WCHAR
*url
, *config_key
;
650 static const WCHAR httpW
[] = {'h','t','t','p'};
652 url
= heap_alloc(size
);
653 returned_size
= size
;
655 hkey
= open_config_key();
658 config_key
= heap_strdupAtoW(addon
->url_config_key
);
659 res
= RegQueryValueExW(hkey
, config_key
, NULL
, &type
, (LPBYTE
)url
, &returned_size
);
660 heap_free(config_key
);
662 if(res
== ERROR_SUCCESS
&& type
== REG_SZ
) goto found
;
665 MultiByteToWideChar( CP_ACP
, 0, addon
->url_default
, -1, url
, size
/ sizeof(WCHAR
) );
668 if (returned_size
> sizeof(httpW
) && !memcmp(url
, httpW
, sizeof(httpW
))) append_url_params( url
);
670 TRACE("Got URL %s\n", debugstr_w(url
));
674 static BOOL
start_download(void)
681 hres
= CreateURLMoniker(NULL
, url
, &mon
);
685 hres
= CreateAsyncBindCtx(0, &InstallCallback
, NULL
, &bind_ctx
);
686 if(SUCCEEDED(hres
)) {
687 hres
= IMoniker_BindToStorage(mon
, bind_ctx
, NULL
, &IID_IUnknown
, (void**)&tmp
);
688 IBindCtx_Release(bind_ctx
);
690 IMoniker_Release(mon
);
695 IUnknown_Release(tmp
);
699 static void run_winebrowser(const WCHAR
*url
)
701 PROCESS_INFORMATION pi
;
708 static const WCHAR winebrowserW
[] = {'\\','w','i','n','e','b','r','o','w','s','e','r','.','e','x','e',0};
710 url_len
= strlenW(url
);
712 len
= GetSystemDirectoryW(app
, MAX_PATH
-sizeof(winebrowserW
)/sizeof(WCHAR
));
713 memcpy(app
+len
, winebrowserW
, sizeof(winebrowserW
));
714 len
+= sizeof(winebrowserW
)/sizeof(WCHAR
) -1;
716 args
= heap_alloc((len
+1+url_len
)*sizeof(WCHAR
));
720 memcpy(args
, app
, len
*sizeof(WCHAR
));
722 memcpy(args
+len
, url
, (url_len
+1) * sizeof(WCHAR
));
724 TRACE("starting %s\n", debugstr_w(args
));
726 memset(&si
, 0, sizeof(si
));
728 ret
= CreateProcessW(app
, args
, NULL
, NULL
, FALSE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
);
731 CloseHandle(pi
.hThread
);
732 CloseHandle(pi
.hProcess
);
736 static INT_PTR CALLBACK
installer_proc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
740 ShowWindow(GetDlgItem(hwnd
, ID_DWL_PROGRESS
), SW_HIDE
);
741 install_dialog
= hwnd
;
745 switch (((NMHDR
*)lParam
)->code
)
749 if (wParam
== ID_DWL_STATUS
)
750 run_winebrowser(((NMLINK
*)lParam
)->item
.szUrl
);
759 IBinding_Abort(dwl_binding
);
764 ShowWindow(GetDlgItem(hwnd
, ID_DWL_PROGRESS
), SW_SHOW
);
765 EnableWindow(GetDlgItem(hwnd
, ID_DWL_INSTALL
), 0);
766 if(!start_download())
767 EndDialog(install_dialog
, 0);
774 BOOL
install_addon(addon_t addon_type
)
779 addon
= addons_info
+addon_type
;
781 p_wine_get_dos_file_name
= (void*)GetProcAddress(GetModuleHandleW(kernel32_dllW
), "wine_get_dos_file_name");
784 * Try to find addon .msi file in following order:
785 * - directory stored in $dir_config_key value of HKCU/Software/Wine/$config_key key
786 * - $datadir/$addon_subdir/
787 * - $INSTALL_DATADIR/wine/$addon_subdir/
788 * - /usr/share/wine/$addon_subdir/
789 * - download from URL stored in $url_config_key value of HKCU/Software/Wine/$config_key key
791 if (install_from_registered_dir() == INSTALL_NEXT
792 && install_from_default_dir() == INSTALL_NEXT
793 && install_from_cache() == INSTALL_NEXT
794 && (url
= get_url()))
795 DialogBoxW(hInst
, addon
->dialog_template
, 0, installer_proc
);