Removed excessive statement (break after return or goto, not useful
[wine] / dlls / shdocvw / shdocvw_main.c
1 /*
2  * SHDOCVW - Internet Explorer Web Control
3  *
4  * Copyright 2001 John R. Sheets (for CodeWeavers)
5  * Copyright 2004 Mike McCormack (for CodeWeavers)
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #define COBJMACROS
29 #define COM_NO_WINDOWS_H
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winreg.h"
34 #include "winuser.h"
35 #include "winnls.h"
36 #include "ole2.h"
37 #include "shlwapi.h"
38
39 #include "shdocvw.h"
40 #include "uuids.h"
41 #include "urlmon.h"
42
43 #include "wine/unicode.h"
44 #include "wine/debug.h"
45
46 #include "initguid.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(shdocvw);
49
50 static const WCHAR szMozDlPath[] = {
51     'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
52     's','h','d','o','c','v','w',0
53 };
54
55 DEFINE_GUID( CLSID_MozillaBrowser, 0x1339B54C,0x3453,0x11D2,0x93,0xB9,0x00,0x00,0x00,0x00,0x00,0x00);
56
57 typedef HRESULT (WINAPI *fnGetClassObject)(REFCLSID rclsid, REFIID iid, LPVOID *ppv);
58
59 HINSTANCE shdocvw_hinstance = 0;
60 static HMODULE SHDOCVW_hshell32 = 0;
61 static HMODULE hMozCtl = (HMODULE)~0UL;
62
63
64 /* convert a guid to a wide character string */
65 static void SHDOCVW_guid2wstr( const GUID *guid, LPWSTR wstr )
66 {
67     char str[40];
68
69     sprintf(str, "{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
70            guid->Data1, guid->Data2, guid->Data3,
71            guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
72            guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
73     MultiByteToWideChar( CP_ACP, 0, str, -1, wstr, 40 );
74 }
75
76 static BOOL SHDOCVW_GetMozctlPath( LPWSTR szPath, DWORD sz )
77 {
78     DWORD r, type;
79     BOOL ret = FALSE;
80     HKEY hkey;
81     static const WCHAR szPre[] = {
82         'S','o','f','t','w','a','r','e','\\',
83         'C','l','a','s','s','e','s','\\',
84         'C','L','S','I','D','\\',0 };
85     static const WCHAR szPost[] = {
86         '\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2',0 };
87     WCHAR szRegPath[(sizeof(szPre)+sizeof(szPost))/sizeof(WCHAR)+40];
88
89     strcpyW( szRegPath, szPre );
90     SHDOCVW_guid2wstr( &CLSID_MozillaBrowser, &szRegPath[strlenW(szRegPath)] );
91     strcatW( szRegPath, szPost );
92
93     TRACE("key = %s\n", debugstr_w( szRegPath ) );
94
95     r = RegOpenKeyW( HKEY_LOCAL_MACHINE, szRegPath, &hkey );
96     if( r != ERROR_SUCCESS )
97         return FALSE;
98
99     r = RegQueryValueExW( hkey, NULL, NULL, &type, (LPBYTE)szPath, &sz );
100     ret = ( r == ERROR_SUCCESS ) && ( type == REG_SZ );
101     RegCloseKey( hkey );
102
103     return ret;
104 }
105
106 /*************************************************************************
107  * SHDOCVW DllMain
108  */
109 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD fdwReason, LPVOID fImpLoad)
110 {
111         TRACE("%p 0x%lx %p\n", hinst, fdwReason, fImpLoad);
112         switch (fdwReason)
113         {
114           case DLL_PROCESS_ATTACH:
115             shdocvw_hinstance = hinst;
116             break;
117           case DLL_PROCESS_DETACH:
118             if (SHDOCVW_hshell32) FreeLibrary(SHDOCVW_hshell32);
119             if (hMozCtl && hMozCtl != (HMODULE)~0UL) FreeLibrary(hMozCtl);
120             break;
121         }
122         return TRUE;
123 }
124
125 /*************************************************************************
126  *              DllCanUnloadNow (SHDOCVW.@)
127  */
128 HRESULT WINAPI SHDOCVW_DllCanUnloadNow(void)
129 {
130     FIXME("(void): stub\n");
131
132     return S_FALSE;
133 }
134
135 /*************************************************************************
136  *              SHDOCVW_TryDownloadMozillaControl
137  */
138 typedef struct _IBindStatusCallbackImpl {
139     IBindStatusCallbackVtbl *vtbl;
140     DWORD ref;
141     HWND hDialog;
142     BOOL *pbCancelled;
143 } IBindStatusCallbackImpl;
144
145 static HRESULT WINAPI
146 dlQueryInterface( IBindStatusCallback* This, REFIID riid, void** ppvObject )
147 {
148     if( IsEqualIID(riid, &IID_IUnknown) ||
149         IsEqualIID(riid, &IID_IBindStatusCallback))
150     {
151         IBindStatusCallback_AddRef( This );
152         *ppvObject = This;
153         return S_OK;
154     }
155     return E_NOINTERFACE;
156 }
157
158 static ULONG WINAPI dlAddRef( IBindStatusCallback* iface )
159 {
160     IBindStatusCallbackImpl *This = (IBindStatusCallbackImpl *) iface;
161     return InterlockedIncrement( &This->ref );
162 }
163
164 static ULONG WINAPI dlRelease( IBindStatusCallback* iface )
165 {
166     IBindStatusCallbackImpl *This = (IBindStatusCallbackImpl *) iface;
167     DWORD ref = InterlockedDecrement( &This->ref );
168     if( !ref )
169     {
170         DestroyWindow( This->hDialog );
171         HeapFree( GetProcessHeap(), 0, This );
172     }
173     return ref;
174 }
175
176 static HRESULT WINAPI
177 dlOnStartBinding( IBindStatusCallback* iface, DWORD dwReserved, IBinding* pib)
178 {
179     ERR("\n");
180     return S_OK;
181 }
182
183 static HRESULT WINAPI
184 dlGetPriority( IBindStatusCallback* iface, LONG* pnPriority)
185 {
186     ERR("\n");
187     return S_OK;
188 }
189
190 static HRESULT WINAPI
191 dlOnLowResource( IBindStatusCallback* iface, DWORD reserved)
192 {
193     ERR("\n");
194     return S_OK;
195 }
196
197 static HRESULT WINAPI
198 dlOnProgress( IBindStatusCallback* iface, ULONG ulProgress,
199               ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
200 {
201     IBindStatusCallbackImpl *This = (IBindStatusCallbackImpl *) iface;
202     HWND hItem;
203     LONG r;
204
205     hItem = GetDlgItem( This->hDialog, 1000 );
206     if( hItem && ulProgressMax )
207         SendMessageW(hItem,PBM_SETPOS,(ulProgress*100)/ulProgressMax,0);
208
209     hItem = GetDlgItem(This->hDialog, 104);
210     if( hItem )
211         SendMessageW(hItem,WM_SETTEXT, 0, (LPARAM) szStatusText);
212
213     SetLastError(0);
214     r = GetWindowLongPtrW( This->hDialog, GWLP_USERDATA );
215     if( r || GetLastError() )
216     {
217         *This->pbCancelled = TRUE;
218         ERR("Cancelled\n");
219         return E_ABORT;
220     }
221
222     return S_OK;
223 }
224
225 static HRESULT WINAPI
226 dlOnStopBinding( IBindStatusCallback* iface, HRESULT hresult, LPCWSTR szError)
227 {
228     ERR("\n");
229     return S_OK;
230 }
231
232 static HRESULT WINAPI
233 dlGetBindInfo( IBindStatusCallback* iface, DWORD* grfBINDF, BINDINFO* pbindinfo)
234 {
235     ERR("\n");
236     return S_OK;
237 }
238
239 static HRESULT WINAPI
240 dlOnDataAvailable( IBindStatusCallback* iface, DWORD grfBSCF,
241                    DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
242 {
243     ERR("\n");
244     return S_OK;
245 }
246
247 static HRESULT WINAPI
248 dlOnObjectAvailable( IBindStatusCallback* iface, REFIID riid, IUnknown* punk)
249 {
250     ERR("\n");
251     return S_OK;
252 }
253
254 struct IBindStatusCallbackVtbl dlVtbl =
255 {
256     dlQueryInterface,
257     dlAddRef,
258     dlRelease,
259     dlOnStartBinding,
260     dlGetPriority,
261     dlOnLowResource,
262     dlOnProgress,
263     dlOnStopBinding,
264     dlGetBindInfo,
265     dlOnDataAvailable,
266     dlOnObjectAvailable
267 };
268
269 static IBindStatusCallback* create_dl(HWND dlg, BOOL *pbCancelled)
270 {
271     IBindStatusCallbackImpl *This;
272
273     This = HeapAlloc( GetProcessHeap(), 0, sizeof *This );
274     This->vtbl = &dlVtbl;
275     This->ref = 1;
276     This->hDialog = dlg;
277     This->pbCancelled = pbCancelled;
278
279     return (IBindStatusCallback*) This;
280 }
281
282 static DWORD WINAPI ThreadFunc( LPVOID info )
283 {
284     IBindStatusCallback *dl;
285     static const WCHAR szUrlVal[] = {'M','o','z','i','l','l','a','U','r','l',0};
286     WCHAR path[MAX_PATH], szUrl[MAX_PATH];
287     LPWSTR p;
288     STARTUPINFOW si;
289     PROCESS_INFORMATION pi;
290     HWND hDlg = info;
291     DWORD r, sz, type;
292     HKEY hkey;
293     BOOL bCancelled = FALSE;
294
295     /* find the name of the thing to download */
296     szUrl[0] = 0;
297     r = RegOpenKeyW( HKEY_LOCAL_MACHINE, szMozDlPath, &hkey );
298     if( r == ERROR_SUCCESS )
299     {
300         sz = MAX_PATH;
301         r = RegQueryValueExW( hkey, szUrlVal, NULL, &type, (LPBYTE)szUrl, &sz );
302         RegCloseKey( hkey );
303     }
304     if( r != ERROR_SUCCESS )
305         goto end;
306
307     /* built the path for the download */
308     p = strrchrW( szUrl, '/' );
309     if (!p)
310         goto end;
311     if (!GetTempPathW( MAX_PATH, path ))
312         goto end;
313     strcatW( path, p+1 );
314
315     /* download it */
316     dl = create_dl(info, &bCancelled);
317     r = URLDownloadToFileW( NULL, szUrl, path, 0, dl );
318     if( dl )
319         IBindStatusCallback_Release( dl );
320     if( (r != S_OK) || bCancelled )
321         goto end;
322
323     /* run it */
324     memset( &si, 0, sizeof si );
325     si.cb = sizeof si;
326     r = CreateProcessW( path, NULL, NULL, NULL, 0, 0, NULL, NULL, &si, &pi );
327     if( !r )
328         goto end;
329     WaitForSingleObject( pi.hProcess, INFINITE );
330
331 end:
332     EndDialog( hDlg, 0 );
333     return 0;
334 }
335
336 static INT_PTR CALLBACK
337 dlProc ( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
338 {
339     HANDLE hThread;
340     DWORD ThreadId;
341     HWND hItem;
342
343     switch (uMsg)
344     {
345     case WM_INITDIALOG:
346         SetWindowLongPtrW( hwndDlg, GWLP_USERDATA, 0 );
347         hItem = GetDlgItem(hwndDlg, 1000);
348         if( hItem )
349         {
350             SendMessageW(hItem,PBM_SETRANGE,0,MAKELPARAM(0,100));
351             SendMessageW(hItem,PBM_SETPOS,0,0);
352         }
353         hThread = CreateThread(NULL,0,ThreadFunc,hwndDlg,0,&ThreadId);
354         if (!hThread)
355             return FALSE;
356         return TRUE;
357     case WM_COMMAND:
358         if( wParam == IDCANCEL )
359             SetWindowLongPtrW( hwndDlg, GWLP_USERDATA, 1 );
360         return FALSE;
361     default:
362         return FALSE;
363     }
364 }
365
366 static BOOL SHDOCVW_TryDownloadMozillaControl()
367 {
368     DWORD r;
369     WCHAR buf[0x100];
370     static const WCHAR szWine[] = { 'W','i','n','e',0 };
371     HANDLE hsem;
372
373     SetLastError( ERROR_SUCCESS );
374     hsem = CreateSemaphoreA( NULL, 0, 1, "mozctl_install_semaphore");
375     if( GetLastError() != ERROR_ALREADY_EXISTS )
376     {
377         LoadStringW( shdocvw_hinstance, 1001, buf, sizeof buf/sizeof(WCHAR) );
378         r = MessageBoxW(NULL, buf, szWine, MB_YESNO | MB_ICONQUESTION);
379         if( r != IDYES )
380             return FALSE;
381
382         DialogBoxW(shdocvw_hinstance, MAKEINTRESOURCEW(100), 0, dlProc);
383     }
384     else
385         WaitForSingleObject( hsem, INFINITE );
386     ReleaseSemaphore( hsem, 1, NULL );
387     CloseHandle( hsem );
388     
389     return TRUE;
390 }
391  
392 static BOOL SHDOCVW_TryLoadMozillaControl()
393 {
394     WCHAR szPath[MAX_PATH];
395     BOOL bTried = FALSE;
396
397     if( hMozCtl != (HMODULE)~0UL )
398         return hMozCtl ? TRUE : FALSE;
399
400     while( 1 )
401     {
402         if( SHDOCVW_GetMozctlPath( szPath, sizeof szPath ) )
403         {
404             hMozCtl = LoadLibraryExW(szPath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
405             if( hMozCtl )
406                 return TRUE;
407         }
408         if( bTried )
409         {
410             MESSAGE("You need to install the Mozilla ActiveX control to\n");
411             MESSAGE("use Wine's builtin CLSID_WebBrowser from SHDOCVW.DLL\n");
412             return FALSE;
413         }
414         SHDOCVW_TryDownloadMozillaControl();
415         bTried = TRUE;
416     }
417 }
418
419 /*************************************************************************
420  *              DllGetClassObject (SHDOCVW.@)
421  */
422 HRESULT WINAPI SHDOCVW_DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
423 {
424     TRACE("\n");
425
426     if( IsEqualGUID( &CLSID_WebBrowser, rclsid ) &&
427         SHDOCVW_TryLoadMozillaControl() )
428     {
429         HRESULT r;
430         fnGetClassObject pGetClassObject;
431
432         TRACE("WebBrowser class %s\n", debugstr_guid(rclsid) );
433
434         pGetClassObject = (fnGetClassObject)
435             GetProcAddress( hMozCtl, "DllGetClassObject" );
436
437         if( !pGetClassObject )
438             return CLASS_E_CLASSNOTAVAILABLE;
439         r = pGetClassObject( &CLSID_MozillaBrowser, riid, ppv );
440
441         TRACE("r = %08lx  *ppv = %p\n", r, *ppv );
442
443         return r;
444     }
445
446     if (IsEqualGUID(&IID_IClassFactory, riid))
447     {
448         /* Pass back our shdocvw class factory */
449         *ppv = (LPVOID)&SHDOCVW_ClassFactory;
450         IClassFactory_AddRef((IClassFactory*)&SHDOCVW_ClassFactory);
451
452         return S_OK;
453     }
454
455     return CLASS_E_CLASSNOTAVAILABLE;
456 }
457
458 /***********************************************************************
459  *              DllGetVersion (SHDOCVW.@)
460  */
461 HRESULT WINAPI SHDOCVW_DllGetVersion(DLLVERSIONINFO *info)
462 {
463     if (info->cbSize != sizeof(DLLVERSIONINFO)) FIXME("support DLLVERSIONINFO2\n");
464
465     /* this is what IE6 on Windows 98 reports */
466     info->dwMajorVersion = 6;
467     info->dwMinorVersion = 0;
468     info->dwBuildNumber = 2600;
469     info->dwPlatformID = DLLVER_PLATFORM_WINDOWS;
470
471     return NOERROR;
472 }
473
474 /*************************************************************************
475  *              DllInstall (SHDOCVW.@)
476  */
477 HRESULT WINAPI SHDOCVW_DllInstall(BOOL bInstall, LPCWSTR cmdline)
478 {
479    FIXME("(%s, %s): stub!\n", bInstall ? "TRUE":"FALSE", debugstr_w(cmdline));
480
481    return S_OK;
482 }
483
484 /*************************************************************************
485  * SHDOCVW_LoadShell32
486  *
487  * makes sure the handle to shell32 is valid
488  */
489  BOOL SHDOCVW_LoadShell32(void)
490 {
491      if (SHDOCVW_hshell32)
492        return TRUE;
493      return ((SHDOCVW_hshell32 = LoadLibraryA("shell32.dll")) != NULL);
494 }
495
496 /***********************************************************************
497  *              @ (SHDOCVW.110)
498  *
499  * Called by Win98 explorer.exe main binary, definitely has 0
500  * parameters.
501  */
502 DWORD WINAPI WinList_Init(void)
503 {
504     FIXME("(), stub!\n");
505     return 0x0deadfeed;
506 }
507
508 /***********************************************************************
509  *              @ (SHDOCVW.118)
510  *
511  * Called by Win98 explorer.exe main binary, definitely has only one
512  * parameter.
513  */
514 static BOOL (WINAPI *pShellDDEInit)(BOOL start) = NULL;
515
516 BOOL WINAPI ShellDDEInit(BOOL start)
517 {
518     TRACE("(%d)\n", start);
519
520     if (!pShellDDEInit)
521     {
522       if (!SHDOCVW_LoadShell32())
523         return FALSE;
524       pShellDDEInit = GetProcAddress(SHDOCVW_hshell32, (LPCSTR)188);
525     }
526
527     if (pShellDDEInit)
528       return pShellDDEInit(start);
529     else
530       return FALSE;
531 }
532
533 /***********************************************************************
534  *              @ (SHDOCVW.125)
535  *
536  * Called by Win98 explorer.exe main binary, definitely has 0
537  * parameters.
538  */
539 DWORD WINAPI RunInstallUninstallStubs(void)
540 {
541     FIXME("(), stub!\n");
542     return 0x0deadbee;
543 }
544
545 /***********************************************************************
546  *              SetQueryNetSessionCount (SHDOCVW.@)
547  */
548 DWORD WINAPI SetQueryNetSessionCount(DWORD arg)
549 {
550     FIXME("(%lu), stub!\n", arg);
551     return 0;
552 }