Release 1.5.29.
[wine] / dlls / urlmon / axinstall.c
1 /*
2  * Copyright 2012 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 #define OEMRESOURCE
20
21 #include <assert.h>
22
23 #include "urlmon_main.h"
24 #include "resource.h"
25
26 #include "advpub.h"
27 #include "fdi.h"
28
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
32
33 static const WCHAR ctxW[] = {'c','t','x',0};
34 static const WCHAR cab_extW[] = {'.','c','a','b',0};
35 static const WCHAR infW[] = {'i','n','f',0};
36 static const WCHAR dllW[] = {'d','l','l',0};
37 static const WCHAR ocxW[] = {'o','c','x',0};
38
39 enum install_type {
40     INSTALL_UNKNOWN,
41     INSTALL_DLL,
42     INSTALL_INF
43 };
44
45 typedef struct {
46     IUri *uri;
47     IBindStatusCallback *callback;
48     BOOL release_on_stop;
49     BOOL cancel;
50     WCHAR *install_file;
51     const WCHAR *cache_file;
52     const WCHAR *tmp_dir;
53     const WCHAR *file_name;
54     enum install_type install_type;
55     HWND hwnd;
56     int counter;
57     INT_PTR timer;
58 } install_ctx_t;
59
60 static void release_install_ctx(install_ctx_t *ctx)
61 {
62     if(ctx->uri)
63         IUri_Release(ctx->uri);
64     if(ctx->callback)
65         IBindStatusCallback_Release(ctx->callback);
66     heap_free(ctx->install_file);
67     heap_free(ctx);
68 }
69
70 static inline BOOL file_exists(const WCHAR *file_name)
71 {
72     return GetFileAttributesW(file_name) != INVALID_FILE_ATTRIBUTES;
73 }
74
75 static HRESULT extract_cab_file(install_ctx_t *ctx)
76 {
77     size_t path_len, file_len;
78     WCHAR *ptr;
79     HRESULT hres;
80
81     hres = ExtractFilesW(ctx->cache_file, ctx->tmp_dir, 0, NULL, NULL, 0);
82     if(FAILED(hres)) {
83         WARN("ExtractFilesW failed: %08x\n", hres);
84         return hres;
85     }
86
87     path_len = strlenW(ctx->tmp_dir);
88     file_len = strlenW(ctx->file_name);
89     ctx->install_file = heap_alloc((path_len+file_len+2)*sizeof(WCHAR));
90     if(!ctx->install_file)
91         return E_OUTOFMEMORY;
92
93     memcpy(ctx->install_file, ctx->tmp_dir, path_len*sizeof(WCHAR));
94     ctx->install_file[path_len] = '\\';
95     memcpy(ctx->install_file+path_len+1, ctx->file_name, (file_len+1)*sizeof(WCHAR));
96
97     /* NOTE: Assume that file_name contains ".cab" extension */
98     ptr = ctx->install_file+path_len+1+file_len-3;
99
100     memcpy(ptr, infW, sizeof(infW));
101     if(file_exists(ctx->install_file)) {
102         ctx->install_type = INSTALL_INF;
103         return S_OK;
104     }
105
106     memcpy(ptr, dllW, sizeof(dllW));
107     if(file_exists(ctx->install_file)) {
108         ctx->install_type = INSTALL_DLL;
109         return S_OK;
110     }
111
112     memcpy(ptr, ocxW, sizeof(ocxW));
113     if(file_exists(ctx->install_file)) {
114         ctx->install_type = INSTALL_DLL;
115         return S_OK;
116     }
117
118     FIXME("No known install file\n");
119     return E_NOTIMPL;
120 }
121
122 static HRESULT setup_dll(install_ctx_t *ctx)
123 {
124     HMODULE module;
125     HRESULT hres;
126
127     HRESULT (WINAPI *reg_func)(void);
128
129     module = LoadLibraryW(ctx->install_file);
130     if(!module)
131         return E_FAIL;
132
133     reg_func = (void*)GetProcAddress(module, "DllRegisterServer");
134     if(reg_func) {
135         hres = reg_func();
136     }else {
137         WARN("no DllRegisterServer function\n");
138         hres = E_FAIL;
139     }
140
141     FreeLibrary(module);
142     return hres;
143 }
144
145 static void expand_command(install_ctx_t *ctx, const WCHAR *cmd, WCHAR *buf, size_t *size)
146 {
147     const WCHAR *ptr = cmd, *prev_ptr = cmd;
148     size_t len = 0, len2;
149
150     static const WCHAR expand_dirW[] = {'%','E','X','T','R','A','C','T','_','D','I','R','%'};
151
152     while((ptr = strchrW(ptr, '%'))) {
153         if(buf)
154             memcpy(buf+len, prev_ptr, ptr-prev_ptr);
155         len += ptr-prev_ptr;
156
157         if(!strncmpiW(ptr, expand_dirW, sizeof(expand_dirW)/sizeof(WCHAR))) {
158             len2 = strlenW(ctx->tmp_dir);
159             if(buf)
160                 memcpy(buf+len, ctx->tmp_dir, len2*sizeof(WCHAR));
161             len += len2;
162             ptr += sizeof(expand_dirW)/sizeof(WCHAR);
163         }else {
164             FIXME("Can't expand %s\n", debugstr_w(ptr));
165             if(buf)
166                 buf[len] = '%';
167             len++;
168             ptr++;
169         }
170
171         prev_ptr = ptr;
172     }
173
174     if(buf)
175         strcpyW(buf+len, prev_ptr);
176     *size = len + strlenW(prev_ptr) + 1;
177 }
178
179 static HRESULT process_hook_section(install_ctx_t *ctx, const WCHAR *sect_name)
180 {
181     WCHAR buf[2048], val[2*MAX_PATH];
182     const WCHAR *key;
183     DWORD len;
184     HRESULT hres;
185
186     static const WCHAR runW[] = {'r','u','n',0};
187
188     len = GetPrivateProfileStringW(sect_name, NULL, NULL, buf, sizeof(buf)/sizeof(*buf), ctx->install_file);
189     if(!len)
190         return S_OK;
191
192     for(key = buf; *key; key += strlenW(key)+1) {
193         if(!strcmpiW(key, runW)) {
194             WCHAR *cmd;
195             size_t size;
196
197             len = GetPrivateProfileStringW(sect_name, runW, NULL, val, sizeof(val)/sizeof(*val), ctx->install_file);
198
199             TRACE("Run %s\n", debugstr_w(val));
200
201             expand_command(ctx, val, NULL, &size);
202
203             cmd = heap_alloc(size*sizeof(WCHAR));
204             if(!cmd)
205                 heap_free(cmd);
206
207             expand_command(ctx, val, cmd, &size);
208             hres = RunSetupCommandW(ctx->hwnd, cmd, NULL, ctx->tmp_dir, NULL, NULL, 0, NULL);
209             heap_free(cmd);
210             if(FAILED(hres))
211                 return hres;
212         }else {
213             FIXME("Unsupported hook %s\n", debugstr_w(key));
214             return E_NOTIMPL;
215         }
216     }
217
218     return S_OK;
219 }
220
221 static HRESULT install_inf_file(install_ctx_t *ctx)
222 {
223     WCHAR buf[2048], sect_name[128];
224     BOOL default_install = TRUE;
225     const WCHAR *key;
226     DWORD len;
227     HRESULT hres;
228
229     static const WCHAR setup_hooksW[] = {'S','e','t','u','p',' ','H','o','o','k','s',0};
230     static const WCHAR add_codeW[] = {'A','d','d','.','C','o','d','e',0};
231
232     len = GetPrivateProfileStringW(setup_hooksW, NULL, NULL, buf, sizeof(buf)/sizeof(*buf), ctx->install_file);
233     if(len) {
234         default_install = FALSE;
235
236         for(key = buf; *key; key += strlenW(key)+1) {
237             TRACE("[Setup Hooks] key: %s\n", debugstr_w(key));
238
239             len = GetPrivateProfileStringW(setup_hooksW, key, NULL, sect_name, sizeof(sect_name)/sizeof(*sect_name),
240                     ctx->install_file);
241             if(!len) {
242                 WARN("Could not get key value\n");
243                 return E_FAIL;
244             }
245
246             hres = process_hook_section(ctx, sect_name);
247             if(FAILED(hres))
248                 return hres;
249         }
250     }
251
252     len = GetPrivateProfileStringW(add_codeW, NULL, NULL, buf, sizeof(buf)/sizeof(*buf), ctx->install_file);
253     if(len) {
254         FIXME("[Add.Code] section not supported\n");
255
256         /* Don't throw an error if we successfully ran setup hooks;
257            installation is likely to be complete enough */
258         if(default_install)
259             return E_NOTIMPL;
260     }
261
262     if(default_install) {
263         hres = RunSetupCommandW(ctx->hwnd, ctx->install_file, NULL, ctx->tmp_dir, NULL, NULL, RSC_FLAG_INF, NULL);
264         if(FAILED(hres)) {
265             WARN("RunSetupCommandW failed: %08x\n", hres);
266             return hres;
267         }
268     }
269
270     return S_OK;
271 }
272
273 static HRESULT install_cab_file(install_ctx_t *ctx)
274 {
275     WCHAR tmp_path[MAX_PATH], tmp_dir[MAX_PATH];
276     BOOL res = FALSE, leave_temp = FALSE;
277     DWORD i;
278     HRESULT hres;
279
280     GetTempPathW(sizeof(tmp_path)/sizeof(WCHAR), tmp_path);
281
282     for(i=0; !res && i < 100; i++) {
283         GetTempFileNameW(tmp_path, NULL, GetTickCount() + i*17037, tmp_dir);
284         res = CreateDirectoryW(tmp_dir, NULL);
285     }
286     if(!res)
287         return E_FAIL;
288
289     ctx->tmp_dir = tmp_dir;
290
291     TRACE("Using temporary directory %s\n", debugstr_w(tmp_dir));
292
293     hres = extract_cab_file(ctx);
294     if(SUCCEEDED(hres)) {
295         if(ctx->callback)
296             IBindStatusCallback_OnProgress(ctx->callback, 0, 0, BINDSTATUS_INSTALLINGCOMPONENTS, ctx->install_file);
297
298         switch(ctx->install_type) {
299         case INSTALL_INF:
300             hres = install_inf_file(ctx);
301             break;
302         case INSTALL_DLL:
303             FIXME("Installing DLL, registering in temporary location\n");
304             hres = setup_dll(ctx);
305             if(SUCCEEDED(hres))
306                 leave_temp = TRUE;
307             break;
308         default:
309             assert(0);
310         }
311     }
312
313     if(!leave_temp)
314         RemoveDirectoryW(ctx->tmp_dir);
315     return hres;
316 }
317
318 static void update_counter(install_ctx_t *ctx, HWND hwnd)
319 {
320     WCHAR text[100];
321
322     if(--ctx->counter <= 0) {
323         HWND button_hwnd;
324
325         KillTimer(hwnd, ctx->timer);
326         LoadStringW(urlmon_instance, IDS_AXINSTALL_INSTALL, text, sizeof(text)/sizeof(WCHAR));
327
328         button_hwnd = GetDlgItem(hwnd, ID_AXINSTALL_INSTALL_BTN);
329         EnableWindow(button_hwnd, TRUE);
330     }else {
331         WCHAR buf[100];
332         LoadStringW(urlmon_instance, IDS_AXINSTALL_INSTALLN, buf, sizeof(buf)/sizeof(WCHAR));
333         sprintfW(text, buf, ctx->counter);
334     }
335
336     SetDlgItemTextW(hwnd, ID_AXINSTALL_INSTALL_BTN, text);
337 }
338
339 static BOOL init_warning_dialog(HWND hwnd, install_ctx_t *ctx)
340 {
341     BSTR display_uri;
342     HRESULT hres;
343
344     if(!SetPropW(hwnd, ctxW, ctx))
345         return FALSE;
346
347     hres = IUri_GetDisplayUri(ctx->uri, &display_uri);
348     if(FAILED(hres))
349         return FALSE;
350
351     SetDlgItemTextW(hwnd, ID_AXINSTALL_LOCATION, display_uri);
352     SysFreeString(display_uri);
353
354     SendDlgItemMessageW(hwnd, ID_AXINSTALL_ICON, STM_SETICON,
355             (WPARAM)LoadIconW(0, (const WCHAR*)OIC_WARNING), 0);
356
357     ctx->counter = 4;
358     update_counter(ctx, hwnd);
359     ctx->timer = SetTimer(hwnd, 1, 1000, NULL);
360     return TRUE;
361 }
362
363 static INT_PTR WINAPI warning_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
364 {
365     switch(msg) {
366     case WM_INITDIALOG: {
367         if(!init_warning_dialog(hwnd, (install_ctx_t*)lparam))
368             EndDialog(hwnd, 0);
369         return TRUE;
370     }
371     case WM_COMMAND:
372         switch(wparam) {
373         case ID_AXINSTALL_INSTALL_BTN: {
374             install_ctx_t *ctx = GetPropW(hwnd, ctxW);
375             if(ctx)
376                 ctx->cancel = FALSE;
377             EndDialog(hwnd, 0);
378             return FALSE;
379         }
380         case IDCANCEL:
381             EndDialog(hwnd, 0);
382             return FALSE;
383         }
384     case WM_TIMER:
385         update_counter(GetPropW(hwnd, ctxW), hwnd);
386         return TRUE;
387     }
388
389     return FALSE;
390 }
391
392 static BOOL install_warning(install_ctx_t *ctx)
393 {
394     IWindowForBindingUI *window_iface;
395     HWND parent_hwnd = NULL;
396     HRESULT hres;
397
398     if(!ctx->callback) {
399         FIXME("no callback\n");
400         return FALSE;
401     }
402
403     hres = IBindStatusCallback_QueryInterface(ctx->callback, &IID_IWindowForBindingUI, (void**)&window_iface);
404     if(FAILED(hres))
405         return FALSE;
406
407     hres = IWindowForBindingUI_GetWindow(window_iface, &IID_ICodeInstall, &ctx->hwnd);
408     IWindowForBindingUI_Release(window_iface);
409     if(FAILED(hres))
410         return FALSE;
411
412     ctx->cancel = TRUE;
413     DialogBoxParamW(urlmon_instance, MAKEINTRESOURCEW(ID_AXINSTALL_WARNING_DLG), parent_hwnd, warning_proc, (LPARAM)ctx);
414     return !ctx->cancel;
415 }
416
417 static HRESULT install_file(install_ctx_t *ctx, const WCHAR *cache_file)
418 {
419     BSTR path;
420     HRESULT hres;
421
422     TRACE("%s\n", debugstr_w(cache_file));
423
424     ctx->cache_file = cache_file;
425
426     if(!install_warning(ctx)) {
427         TRACE("Installation cancelled\n");
428         return S_OK;
429     }
430
431     hres = IUri_GetPath(ctx->uri, &path);
432     if(SUCCEEDED(hres)) {
433         const WCHAR *ptr, *ptr2, *ext;
434
435         ptr = strrchrW(path, '/');
436         if(!ptr)
437             ptr = path;
438         else
439             ptr++;
440
441         ptr2 = strrchrW(ptr, '\\');
442         if(ptr2)
443             ptr = ptr2+1;
444
445         ctx->file_name = ptr;
446         ext = strrchrW(ptr, '.');
447         if(!ext)
448             ext = ptr;
449
450         if(!strcmpW(ext, cab_extW)) {
451             hres = install_cab_file(ctx);
452         }else {
453             FIXME("Unsupported extension %s\n", debugstr_w(ext));
454             hres = E_NOTIMPL;
455         }
456         SysFreeString(path);
457     }
458
459     return hres;
460 }
461
462 static void failure_msgbox(install_ctx_t *ctx, HRESULT hres)
463 {
464     WCHAR buf[1024], fmt[1024];
465
466     LoadStringW(urlmon_instance, IDS_AXINSTALL_FAILURE, fmt, sizeof(fmt)/sizeof(WCHAR));
467     sprintfW(buf, fmt, hres);
468     MessageBoxW(ctx->hwnd, buf, NULL, MB_OK);
469 }
470
471 static HRESULT distunit_on_stop(void *ctx, const WCHAR *cache_file, HRESULT hresult, const WCHAR *error_str)
472 {
473     install_ctx_t *install_ctx = ctx;
474
475     TRACE("(%p %s %08x %s)\n", ctx, debugstr_w(cache_file), hresult, debugstr_w(error_str));
476
477     if(hresult == S_OK) {
478         hresult = install_file(install_ctx, cache_file);
479         if(FAILED(hresult))
480             failure_msgbox(ctx, hresult);
481     }
482
483     if(install_ctx->callback)
484         IBindStatusCallback_OnStopBinding(install_ctx->callback, hresult, error_str);
485
486     if(install_ctx->release_on_stop)
487         release_install_ctx(install_ctx);
488     return S_OK;
489 }
490
491 /***********************************************************************
492  *           AsyncInstallDistributionUnit (URLMON.@)
493  */
494 HRESULT WINAPI AsyncInstallDistributionUnit(const WCHAR *szDistUnit, const WCHAR *szTYPE, const WCHAR *szExt,
495         DWORD dwFileVersionMS, DWORD dwFileVersionLS, const WCHAR *szURL, IBindCtx *pbc, void *pvReserved, DWORD flags)
496 {
497     install_ctx_t *ctx;
498     HRESULT hres;
499
500     TRACE("(%s %s %s %x %x %s %p %p %x)\n", debugstr_w(szDistUnit), debugstr_w(szTYPE), debugstr_w(szExt),
501           dwFileVersionMS, dwFileVersionLS, debugstr_w(szURL), pbc, pvReserved, flags);
502
503     if(szDistUnit || szTYPE || szExt)
504         FIXME("Unsupported arguments\n");
505
506     ctx = heap_alloc_zero(sizeof(*ctx));
507     if(!ctx)
508         return E_OUTOFMEMORY;
509
510     hres = CreateUri(szURL, 0, 0, &ctx->uri);
511     if(FAILED(hres)) {
512         heap_free(ctx);
513         return E_OUTOFMEMORY;
514     }
515
516     ctx->callback = bsc_from_bctx(pbc);
517
518     hres = download_to_cache(ctx->uri, distunit_on_stop, ctx, ctx->callback);
519     if(hres == MK_S_ASYNCHRONOUS)
520         ctx->release_on_stop = TRUE;
521     else
522         release_install_ctx(ctx);
523
524     return hres;
525 }