Tell shdocvw where to get the Mozilla control
[reactos.git] / reactos / lib / 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 LONG SHDOCVW_refCount = 0;
51
52 static const WCHAR szMozDlPath[] = {
53 'S','o','f','t','w','a','r','e','\\','R','e','a','c','t','O','S','\\',
54 's','h','d','o','c','v','w',0
55 };
56
57 DEFINE_GUID( CLSID_MozillaBrowser, 0x1339B54C,0x3453,0x11D2,0x93,0xB9,0x00,0x00,0x00,0x00,0x00,0x00);
58
59 typedef HRESULT (WINAPI *fnGetClassObject)(REFCLSID rclsid, REFIID iid, LPVOID *ppv);
60 typedef HRESULT (WINAPI *fnCanUnloadNow)(void);
61
62 HINSTANCE shdocvw_hinstance = 0;
63 static HMODULE SHDOCVW_hshell32 = 0;
64 static HMODULE hMozCtl = (HMODULE)~0UL;
65
66
67 /* convert a guid to a wide character string */
68 static void SHDOCVW_guid2wstr( const GUID *guid, LPWSTR wstr )
69 {
70 char str[40];
71
72 sprintf(str, "{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
73 guid->Data1, guid->Data2, guid->Data3,
74 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
75 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
76 MultiByteToWideChar( CP_ACP, 0, str, -1, wstr, 40 );
77 }
78
79 static BOOL SHDOCVW_GetMozctlPath( LPWSTR szPath, DWORD sz )
80 {
81 DWORD r, type;
82 BOOL ret = FALSE;
83 HKEY hkey;
84 static const WCHAR szPre[] = {
85 'S','o','f','t','w','a','r','e','\\',
86 'C','l','a','s','s','e','s','\\',
87 'C','L','S','I','D','\\',0 };
88 static const WCHAR szPost[] = {
89 '\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2',0 };
90 WCHAR szRegPath[(sizeof(szPre)+sizeof(szPost))/sizeof(WCHAR)+40];
91
92 strcpyW( szRegPath, szPre );
93 SHDOCVW_guid2wstr( &CLSID_MozillaBrowser, &szRegPath[strlenW(szRegPath)] );
94 strcatW( szRegPath, szPost );
95
96 TRACE("key = %s\n", debugstr_w( szRegPath ) );
97
98 r = RegOpenKeyW( HKEY_LOCAL_MACHINE, szRegPath, &hkey );
99 if( r != ERROR_SUCCESS )
100 return FALSE;
101
102 r = RegQueryValueExW( hkey, NULL, NULL, &type, (LPBYTE)szPath, &sz );
103 ret = ( r == ERROR_SUCCESS ) && ( type == REG_SZ );
104 RegCloseKey( hkey );
105
106 return ret;
107 }
108
109 /*************************************************************************
110 * SHDOCVW DllMain
111 */
112 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD fdwReason, LPVOID fImpLoad)
113 {
114 TRACE("%p 0x%lx %p\n", hinst, fdwReason, fImpLoad);
115 switch (fdwReason)
116 {
117 case DLL_PROCESS_ATTACH:
118 shdocvw_hinstance = hinst;
119 break;
120 case DLL_PROCESS_DETACH:
121 if (SHDOCVW_hshell32) FreeLibrary(SHDOCVW_hshell32);
122 if (hMozCtl && hMozCtl != (HMODULE)~0UL) FreeLibrary(hMozCtl);
123 break;
124 }
125 return TRUE;
126 }
127
128 /*************************************************************************
129 * DllCanUnloadNow (SHDOCVW.@)
130 */
131 HRESULT WINAPI DllCanUnloadNow(void)
132 {
133 HRESULT moz_can_unload = S_OK;
134 fnCanUnloadNow pCanUnloadNow;
135
136 if (hMozCtl)
137 {
138 pCanUnloadNow = (fnCanUnloadNow)
139 GetProcAddress(hMozCtl, "DllCanUnloadNow");
140 if (pCanUnloadNow)
141 moz_can_unload = pCanUnloadNow();
142 }
143
144 if (moz_can_unload == S_OK && SHDOCVW_refCount == 0)
145 return S_OK;
146
147 return S_FALSE;
148 }
149
150 /*************************************************************************
151 * SHDOCVW_TryDownloadMozillaControl
152 */
153 typedef struct _IBindStatusCallbackImpl {
154 const IBindStatusCallbackVtbl *vtbl;
155 LONG ref;
156 HWND hDialog;
157 BOOL *pbCancelled;
158 } IBindStatusCallbackImpl;
159
160 static HRESULT WINAPI
161 dlQueryInterface( IBindStatusCallback* This, REFIID riid, void** ppvObject )
162 {
163 if (ppvObject == NULL) return E_POINTER;
164
165 if( IsEqualIID(riid, &IID_IUnknown) ||
166 IsEqualIID(riid, &IID_IBindStatusCallback))
167 {
168 IBindStatusCallback_AddRef( This );
169 *ppvObject = This;
170 return S_OK;
171 }
172 return E_NOINTERFACE;
173 }
174
175 static ULONG WINAPI dlAddRef( IBindStatusCallback* iface )
176 {
177 IBindStatusCallbackImpl *This = (IBindStatusCallbackImpl *) iface;
178
179 SHDOCVW_LockModule();
180
181 return InterlockedIncrement( &This->ref );
182 }
183
184 static ULONG WINAPI dlRelease( IBindStatusCallback* iface )
185 {
186 IBindStatusCallbackImpl *This = (IBindStatusCallbackImpl *) iface;
187 DWORD ref = InterlockedDecrement( &This->ref );
188
189 if( !ref )
190 {
191 DestroyWindow( This->hDialog );
192 HeapFree( GetProcessHeap(), 0, This );
193 }
194
195 SHDOCVW_UnlockModule();
196
197 return ref;
198 }
199
200 static HRESULT WINAPI
201 dlOnStartBinding( IBindStatusCallback* iface, DWORD dwReserved, IBinding* pib)
202 {
203 ERR("\n");
204 return S_OK;
205 }
206
207 static HRESULT WINAPI
208 dlGetPriority( IBindStatusCallback* iface, LONG* pnPriority)
209 {
210 ERR("\n");
211 return S_OK;
212 }
213
214 static HRESULT WINAPI
215 dlOnLowResource( IBindStatusCallback* iface, DWORD reserved)
216 {
217 ERR("\n");
218 return S_OK;
219 }
220
221 static HRESULT WINAPI
222 dlOnProgress( IBindStatusCallback* iface, ULONG ulProgress,
223 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
224 {
225 IBindStatusCallbackImpl *This = (IBindStatusCallbackImpl *) iface;
226 HWND hItem;
227 LONG r;
228
229 hItem = GetDlgItem( This->hDialog, 1000 );
230 if( hItem && ulProgressMax )
231 SendMessageW(hItem,PBM_SETPOS,(ulProgress*100)/ulProgressMax,0);
232
233 hItem = GetDlgItem(This->hDialog, 104);
234 if( hItem )
235 SendMessageW(hItem,WM_SETTEXT, 0, (LPARAM) szStatusText);
236
237 SetLastError(0);
238 r = GetWindowLongPtrW( This->hDialog, GWLP_USERDATA );
239 if( r || GetLastError() )
240 {
241 *This->pbCancelled = TRUE;
242 ERR("Cancelled\n");
243 return E_ABORT;
244 }
245
246 return S_OK;
247 }
248
249 static HRESULT WINAPI
250 dlOnStopBinding( IBindStatusCallback* iface, HRESULT hresult, LPCWSTR szError)
251 {
252 ERR("\n");
253 return S_OK;
254 }
255
256 static HRESULT WINAPI
257 dlGetBindInfo( IBindStatusCallback* iface, DWORD* grfBINDF, BINDINFO* pbindinfo)
258 {
259 ERR("\n");
260 return S_OK;
261 }
262
263 static HRESULT WINAPI
264 dlOnDataAvailable( IBindStatusCallback* iface, DWORD grfBSCF,
265 DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
266 {
267 ERR("\n");
268 return S_OK;
269 }
270
271 static HRESULT WINAPI
272 dlOnObjectAvailable( IBindStatusCallback* iface, REFIID riid, IUnknown* punk)
273 {
274 ERR("\n");
275 return S_OK;
276 }
277
278 static const IBindStatusCallbackVtbl dlVtbl =
279 {
280 dlQueryInterface,
281 dlAddRef,
282 dlRelease,
283 dlOnStartBinding,
284 dlGetPriority,
285 dlOnLowResource,
286 dlOnProgress,
287 dlOnStopBinding,
288 dlGetBindInfo,
289 dlOnDataAvailable,
290 dlOnObjectAvailable
291 };
292
293 static IBindStatusCallback* create_dl(HWND dlg, BOOL *pbCancelled)
294 {
295 IBindStatusCallbackImpl *This;
296
297 This = HeapAlloc( GetProcessHeap(), 0, sizeof *This );
298 This->vtbl = &dlVtbl;
299 This->ref = 1;
300 This->hDialog = dlg;
301 This->pbCancelled = pbCancelled;
302
303 return (IBindStatusCallback*) This;
304 }
305
306 static DWORD WINAPI ThreadFunc( LPVOID info )
307 {
308 IBindStatusCallback *dl;
309 static const WCHAR szUrlVal[] = {'M','o','z','i','l','l','a','U','r','l',0};
310 static const WCHAR szFileProtocol[] = {'f','i','l','e',':','/','/','/',0};
311 WCHAR path[MAX_PATH], szUrl[MAX_PATH];
312 LPWSTR p;
313 STARTUPINFOW si;
314 PROCESS_INFORMATION pi;
315 HWND hDlg = info;
316 DWORD r, sz, type;
317 HKEY hkey;
318 BOOL bCancelled = FALSE;
319 BOOL bTempfile = FALSE;
320
321 /* find the name of the thing to download */
322 szUrl[0] = 0;
323 /* @@ Wine registry key: HKCU\Software\Wine\shdocvw */
324 r = RegOpenKeyW( HKEY_LOCAL_MACHINE, szMozDlPath, &hkey );
325 if( r == ERROR_SUCCESS )
326 {
327 sz = MAX_PATH;
328 r = RegQueryValueExW( hkey, szUrlVal, NULL, &type, (LPBYTE)szUrl, &sz );
329 RegCloseKey( hkey );
330 }
331 if( r != ERROR_SUCCESS )
332 goto end;
333
334 if( !strncmpW(szUrl, szFileProtocol, strlenW(szFileProtocol)) )
335 lstrcpynW( path, szUrl+strlenW(szFileProtocol), MAX_PATH );
336 else
337 {
338 /* built the path for the download */
339 p = strrchrW( szUrl, '/' );
340 if (!p)
341 goto end;
342 if (!GetTempPathW( MAX_PATH, path ))
343 goto end;
344 strcatW( path, p+1 );
345
346 /* download it */
347 bTempfile = TRUE;
348 dl = create_dl(info, &bCancelled);
349 r = URLDownloadToFileW( NULL, szUrl, path, 0, dl );
350 if( dl )
351 IBindStatusCallback_Release( dl );
352 if( (r != S_OK) || bCancelled )
353 goto end;
354 }
355
356 /* run it */
357 memset( &si, 0, sizeof si );
358 si.cb = sizeof si;
359 r = CreateProcessW( path, NULL, NULL, NULL, 0, 0, NULL, NULL, &si, &pi );
360 if( !r )
361 goto end;
362 WaitForSingleObject( pi.hProcess, INFINITE );
363 CloseHandle( pi.hProcess );
364 CloseHandle( pi.hThread );
365
366 end:
367 if( bTempfile )
368 DeleteFileW( path );
369 EndDialog( hDlg, 0 );
370 return 0;
371 }
372
373 static INT_PTR CALLBACK
374 dlProc ( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
375 {
376 HANDLE hThread;
377 DWORD ThreadId;
378 HWND hItem;
379
380 switch (uMsg)
381 {
382 case WM_INITDIALOG:
383 SetWindowLongPtrW( hwndDlg, GWLP_USERDATA, 0 );
384 hItem = GetDlgItem(hwndDlg, 1000);
385 if( hItem )
386 {
387 SendMessageW(hItem,PBM_SETRANGE,0,MAKELPARAM(0,100));
388 SendMessageW(hItem,PBM_SETPOS,0,0);
389 }
390 hThread = CreateThread(NULL,0,ThreadFunc,hwndDlg,0,&ThreadId);
391 if (!hThread)
392 return FALSE;
393 return TRUE;
394 case WM_COMMAND:
395 if( wParam == IDCANCEL )
396 SetWindowLongPtrW( hwndDlg, GWLP_USERDATA, 1 );
397 return FALSE;
398 default:
399 return FALSE;
400 }
401 }
402
403 static BOOL SHDOCVW_TryDownloadMozillaControl(void)
404 {
405 DWORD r;
406 WCHAR buf[0x100];
407 static const WCHAR szTitle[] = { 'R','e','a','c','t','O','S',0 };
408 HANDLE hsem;
409
410 SetLastError( ERROR_SUCCESS );
411 hsem = CreateSemaphoreA( NULL, 0, 1, "mozctl_install_semaphore");
412 if( GetLastError() != ERROR_ALREADY_EXISTS )
413 {
414 LoadStringW( shdocvw_hinstance, 1001, buf, sizeof buf/sizeof(WCHAR) );
415 r = MessageBoxW(NULL, buf, szTitle, MB_YESNO | MB_ICONQUESTION);
416 if( r != IDYES )
417 return FALSE;
418
419 DialogBoxW(shdocvw_hinstance, MAKEINTRESOURCEW(100), 0, dlProc);
420 }
421 else
422 WaitForSingleObject( hsem, INFINITE );
423 ReleaseSemaphore( hsem, 1, NULL );
424 CloseHandle( hsem );
425
426 return TRUE;
427 }
428
429 static BOOL SHDOCVW_TryLoadMozillaControl(void)
430 {
431 WCHAR szPath[MAX_PATH];
432 BOOL bTried = FALSE;
433
434 if( hMozCtl != (HMODULE)~0UL )
435 return hMozCtl ? TRUE : FALSE;
436
437 while( 1 )
438 {
439 if( SHDOCVW_GetMozctlPath( szPath, sizeof szPath ) )
440 {
441 hMozCtl = LoadLibraryExW(szPath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
442 if( hMozCtl )
443 return TRUE;
444 }
445 if( bTried )
446 {
447 MESSAGE("You need to install the Mozilla ActiveX control to\n");
448 MESSAGE("use ReactOS's builtin CLSID_WebBrowser from SHDOCVW.DLL\n");
449 return FALSE;
450 }
451 SHDOCVW_TryDownloadMozillaControl();
452 bTried = TRUE;
453 }
454 }
455
456 /*************************************************************************
457 * DllGetClassObject (SHDOCVW.@)
458 */
459 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
460 {
461 TRACE("\n");
462
463 if( IsEqualGUID( &CLSID_WebBrowser, rclsid ) &&
464 SHDOCVW_TryLoadMozillaControl() )
465 {
466 HRESULT r;
467 fnGetClassObject pGetClassObject;
468
469 TRACE("WebBrowser class %s\n", debugstr_guid(rclsid) );
470
471 pGetClassObject = (fnGetClassObject)
472 GetProcAddress( hMozCtl, "DllGetClassObject" );
473
474 if( !pGetClassObject )
475 return CLASS_E_CLASSNOTAVAILABLE;
476 r = pGetClassObject( &CLSID_MozillaBrowser, riid, ppv );
477
478 TRACE("r = %08lx *ppv = %p\n", r, *ppv );
479
480 return r;
481 }
482
483 if (IsEqualCLSID(&CLSID_WebBrowser, rclsid) &&
484 IsEqualIID(&IID_IClassFactory, riid))
485 {
486 /* Pass back our shdocvw class factory */
487 *ppv = (LPVOID)&SHDOCVW_ClassFactory;
488 IClassFactory_AddRef((IClassFactory*)&SHDOCVW_ClassFactory);
489
490 return S_OK;
491 }
492
493 /* As a last resort, figure if the CLSID belongs to a 'Shell Instance Object' */
494 return SHDOCVW_GetShellInstanceObjectClassObject(rclsid, riid, ppv);
495 }
496
497 /***********************************************************************
498 * DllGetVersion (SHDOCVW.@)
499 */
500 HRESULT WINAPI DllGetVersion(DLLVERSIONINFO *info)
501 {
502 if (info->cbSize != sizeof(DLLVERSIONINFO)) FIXME("support DLLVERSIONINFO2\n");
503
504 /* this is what IE6 on Windows 98 reports */
505 info->dwMajorVersion = 6;
506 info->dwMinorVersion = 0;
507 info->dwBuildNumber = 2600;
508 info->dwPlatformID = DLLVER_PLATFORM_WINDOWS;
509
510 return NOERROR;
511 }
512
513 /*************************************************************************
514 * DllInstall (SHDOCVW.@)
515 */
516 HRESULT WINAPI DllInstall(BOOL bInstall, LPCWSTR cmdline)
517 {
518 FIXME("(%s, %s): stub!\n", bInstall ? "TRUE":"FALSE", debugstr_w(cmdline));
519
520 return S_OK;
521 }
522
523 /*************************************************************************
524 * SHDOCVW_LoadShell32
525 *
526 * makes sure the handle to shell32 is valid
527 */
528 BOOL SHDOCVW_LoadShell32(void)
529 {
530 if (SHDOCVW_hshell32)
531 return TRUE;
532 return ((SHDOCVW_hshell32 = LoadLibraryA("shell32.dll")) != NULL);
533 }
534
535 /***********************************************************************
536 * @ (SHDOCVW.110)
537 *
538 * Called by Win98 explorer.exe main binary, definitely has 0
539 * parameters.
540 */
541 DWORD WINAPI WinList_Init(void)
542 {
543 FIXME("(), stub!\n");
544 return 0x0deadfeed;
545 }
546
547 /***********************************************************************
548 * @ (SHDOCVW.118)
549 *
550 * Called by Win98 explorer.exe main binary, definitely has only one
551 * parameter.
552 */
553 static BOOL (WINAPI *pShellDDEInit)(BOOL start) = NULL;
554
555 BOOL WINAPI ShellDDEInit(BOOL start)
556 {
557 TRACE("(%d)\n", start);
558
559 if (!pShellDDEInit)
560 {
561 if (!SHDOCVW_LoadShell32())
562 return FALSE;
563 pShellDDEInit = GetProcAddress(SHDOCVW_hshell32, (LPCSTR)188);
564 }
565
566 if (pShellDDEInit)
567 return pShellDDEInit(start);
568 else
569 return FALSE;
570 }
571
572 /***********************************************************************
573 * @ (SHDOCVW.125)
574 *
575 * Called by Win98 explorer.exe main binary, definitely has 0
576 * parameters.
577 */
578 DWORD WINAPI RunInstallUninstallStubs(void)
579 {
580 FIXME("(), stub!\n");
581 return 0x0deadbee;
582 }
583
584 /***********************************************************************
585 * SetQueryNetSessionCount (SHDOCVW.@)
586 */
587 DWORD WINAPI SetQueryNetSessionCount(DWORD arg)
588 {
589 FIXME("(%lu), stub!\n", arg);
590 return 0;
591 }