- Added more tests.
[wine] / dlls / mshtml / main.c
1 /*
2  *    MSHTML Class Factory
3  *
4  * Copyright 2002 Lionel Ulmer
5  * Copyright 2003 Mike McCormack
6  * Copyright 2005 Jacek Caban
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "config.h"
24
25 #include <stdarg.h>
26 #include <stdio.h>
27
28 #define COBJMACROS
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winuser.h"
33 #include "winnls.h"
34 #include "winreg.h"
35 #include "ole2.h"
36 #include "docobj.h"
37 #include "advpub.h"
38
39 #include "mshtml.h"
40 #include "mshtmhst.h"
41
42 #include "wine/unicode.h"
43 #include "wine/debug.h"
44
45 #include "initguid.h"
46 #include "mshtml_private.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
49
50 DEFINE_GUID( CLSID_MozillaBrowser, 0x1339B54C,0x3453,0x11D2,0x93,0xB9,0x00,0x00,0x00,0x00,0x00,0x00);
51
52 typedef HRESULT (WINAPI *fnGetClassObject)(REFCLSID rclsid, REFIID iid, LPVOID *ppv);
53 typedef BOOL (WINAPI *fnCanUnloadNow)();
54
55 static HMODULE hMozCtl;
56
57 HINSTANCE hInst;
58
59 /* convert a guid to a wide character string */
60 static void MSHTML_guid2wstr( const GUID *guid, LPWSTR wstr )
61 {
62     char str[40];
63
64     sprintf(str, "{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
65            guid->Data1, guid->Data2, guid->Data3,
66            guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
67            guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
68     MultiByteToWideChar( CP_ACP, 0, str, -1, wstr, 40 );
69 }
70
71 static BOOL MSHTML_GetMozctlPath( LPWSTR szPath, DWORD sz )
72 {
73     DWORD r, type;
74     BOOL ret = FALSE;
75     HKEY hkey;
76     static const WCHAR szPre[] = {
77         'S','o','f','t','w','a','r','e','\\',
78         'C','l','a','s','s','e','s','\\',
79         'C','L','S','I','D','\\',0 };
80     static const WCHAR szPost[] = {
81         '\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2',0 };
82     WCHAR szRegPath[(sizeof(szPre)+sizeof(szPost))/sizeof(WCHAR)+40];
83
84     strcpyW( szRegPath, szPre );
85     MSHTML_guid2wstr( &CLSID_MozillaBrowser, &szRegPath[strlenW(szRegPath)] );
86     strcatW( szRegPath, szPost );
87
88     TRACE("key = %s\n", debugstr_w( szRegPath ) );
89
90     r = RegOpenKeyW( HKEY_LOCAL_MACHINE, szRegPath, &hkey );
91     if( r != ERROR_SUCCESS )
92         return FALSE;
93
94     r = RegQueryValueExW( hkey, NULL, NULL, &type, (LPBYTE)szPath, &sz );
95     ret = ( r == ERROR_SUCCESS ) && ( type == REG_SZ );
96     RegCloseKey( hkey );
97
98     return ret;
99 }
100
101 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
102 {
103     WCHAR szPath[MAX_PATH];
104
105     switch(fdwReason) {
106         case DLL_PROCESS_ATTACH:
107             if(MSHTML_GetMozctlPath(szPath, sizeof szPath)) {
108                 hMozCtl = LoadLibraryExW(szPath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
109                 if(!hMozCtl)
110                     ERR("Can't load the Mozilla ActiveX control\n");
111             }else {
112                 TRACE("Not found Mozilla ActiveX Control. HTML rendering will be disabled.\n");
113             }
114             hInst = hInstDLL;
115             break;
116         case DLL_PROCESS_DETACH:
117             if(hMozCtl)
118                 FreeLibrary( hMozCtl );
119             break;
120     }
121     return TRUE;
122 }
123
124 /***********************************************************
125  *    ClassFactory implementation
126  */
127 typedef HRESULT (*CreateInstanceFunc)(IUnknown*,REFIID,void**);
128 typedef struct {
129     const IClassFactoryVtbl *lpVtbl;
130     LONG ref;
131     CreateInstanceFunc fnCreateInstance;
132 } ClassFactory;
133
134 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFGUID riid, void **ppvObject)
135 {
136     if(IsEqualGUID(&IID_IClassFactory, riid) || IsEqualGUID(&IID_IUnknown, riid)) {
137         IClassFactory_AddRef(iface);
138         *ppvObject = iface;
139         return S_OK;
140     }
141
142     WARN("not supported iid %s\n", debugstr_guid(riid));
143     *ppvObject = NULL;
144     return E_NOINTERFACE;
145 }
146
147 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
148 {
149     ClassFactory *This = (ClassFactory*)iface;
150     ULONG ref = InterlockedIncrement(&This->ref);
151     TRACE("(%p) ref = %lu\n", This, ref);
152     return ref;
153 }
154
155 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
156 {
157     ClassFactory *This = (ClassFactory*)iface;
158     ULONG ref = InterlockedDecrement(&This->ref);
159
160     TRACE("(%p) ref = %lu\n", This, ref);
161
162     if(!ref)
163         HeapFree(GetProcessHeap(), 0, This);
164
165     return ref;
166 }
167
168 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
169         REFIID riid, void **ppvObject)
170 {
171     ClassFactory *This = (ClassFactory*)iface;
172     return This->fnCreateInstance(pUnkOuter, riid, ppvObject);
173 }
174
175 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
176 {
177     FIXME("(%p)->(%x) stub\n", iface, dolock);
178     return S_OK;
179 }
180
181 static const IClassFactoryVtbl HTMLClassFactoryVtbl = {
182     ClassFactory_QueryInterface,
183     ClassFactory_AddRef,
184     ClassFactory_Release,
185     ClassFactory_CreateInstance,
186     ClassFactory_LockServer
187 };
188
189 static HRESULT ClassFactory_Create(REFIID riid, void **ppv, CreateInstanceFunc fnCreateInstance)
190 {
191     ClassFactory *ret = HeapAlloc(GetProcessHeap(), 0, sizeof(ClassFactory));
192     HRESULT hres;
193
194     ret->lpVtbl = &HTMLClassFactoryVtbl;
195     ret->ref = 0;
196     ret->fnCreateInstance = fnCreateInstance;
197
198     hres = IClassFactory_QueryInterface((IClassFactory*)ret, riid, ppv);
199     if(FAILED(hres)) {
200         HeapFree(GetProcessHeap(), 0, ret);
201         *ppv = NULL;
202     }
203     return hres;
204 }
205
206 HRESULT WINAPI MSHTML_DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
207 {
208     HRESULT hres;
209     fnGetClassObject pGetClassObject;
210
211     if(hMozCtl && IsEqualGUID(&CLSID_HTMLDocument, rclsid)) {
212         pGetClassObject = (fnGetClassObject) GetProcAddress(hMozCtl, "DllGetClassObject");
213         if(pGetClassObject) {
214             hres = pGetClassObject(&CLSID_MozillaBrowser, riid, ppv);
215             if(SUCCEEDED(hres)) {
216                 TRACE("returning Mozilla ActiveX Control hres = %08lx  *ppv = %p\n", hres, *ppv);
217                 return hres;
218             }
219         }
220     }
221
222     if(IsEqualGUID(&CLSID_HTMLDocument, rclsid)) {
223         TRACE("(CLSID_HTMLDocument %s %p)\n", debugstr_guid(riid), ppv);
224         return ClassFactory_Create(riid, ppv, HTMLDocument_Create);
225     }else if(IsEqualGUID(&CLSID_AboutProtocol, rclsid)) {
226         TRACE("(CLSID_AboutProtocol %s %p)\n", debugstr_guid(riid), ppv);
227         return ProtocolFactory_Create(rclsid, riid, ppv);
228     }else if(IsEqualGUID(&CLSID_JSProtocol, rclsid)) {
229         TRACE("(CLSID_JSProtocol %s %p)\n", debugstr_guid(riid), ppv);
230         return ProtocolFactory_Create(rclsid, riid, ppv);
231     }else if(IsEqualGUID(&CLSID_MailtoProtocol, rclsid)) {
232         TRACE("(CLSID_MailtoProtocol %s %p)\n", debugstr_guid(riid), ppv);
233         return ProtocolFactory_Create(rclsid, riid, ppv);
234     }else if(IsEqualGUID(&CLSID_ResProtocol, rclsid)) {
235         TRACE("(CLSID_ResProtocol %s %p)\n", debugstr_guid(riid), ppv);
236         return ProtocolFactory_Create(rclsid, riid, ppv);
237     }else if(IsEqualGUID(&CLSID_SysimageProtocol, rclsid)) {
238         TRACE("(CLSID_SysimageProtocol %s %p)\n", debugstr_guid(riid), ppv);
239         return ProtocolFactory_Create(rclsid, riid, ppv);
240     }
241
242     FIXME("Unknown class %s\n", debugstr_guid(rclsid));
243     return CLASS_E_CLASSNOTAVAILABLE;
244 }
245
246 HRESULT WINAPI MSHTML_DllCanUnloadNow(void)
247 {
248     fnCanUnloadNow pCanUnloadNow = NULL;
249     HRESULT hres;
250
251     TRACE("()\n");
252
253     if(hMozCtl)
254         pCanUnloadNow = (fnCanUnloadNow) GetProcAddress(hMozCtl, "DllCanUnloadNow");
255     if(!pCanUnloadNow)
256         return S_FALSE;
257
258     hres = pCanUnloadNow();
259
260     TRACE("hres = %08lx\n", hres);
261
262     return hres;
263 }
264
265 /***********************************************************************
266  *          RunHTMLApplication (MSHTML.@)
267  *
268  * Appears to have the same prototype as WinMain.
269  */
270 HRESULT WINAPI RunHTMLApplication( HINSTANCE hinst, HINSTANCE hPrevInst,
271                                LPSTR szCmdLine, INT nCmdShow )
272 {
273     FIXME("%p %p %s %d\n", hinst, hPrevInst, debugstr_a(szCmdLine), nCmdShow );
274     return 0;
275 }
276
277 /***********************************************************************
278  *          DllInstall (MSHTML.@)
279  */
280 HRESULT WINAPI MSHTML_DllInstall(BOOL bInstall, LPCWSTR cmdline)
281 {
282     FIXME("stub %d %s: returning S_OK\n", bInstall, debugstr_w(cmdline));
283     return S_OK;
284 }
285
286 DEFINE_GUID(CLSID_CAnchorBrowsePropertyPage, 0x3050F3BB, 0x98B5, 0x11CF, 0xBB,0x82, 0x00,0xAA,0x00,0xBD,0xCE,0x0B);
287 DEFINE_GUID(CLSID_CBackgroundPropertyPage, 0x3050F232, 0x98B5, 0x11CF, 0xBB,0x82, 0x00,0xAA,0x00,0xBD,0xCE,0x0B);
288 DEFINE_GUID(CLSID_CCDAnchorPropertyPage, 0x3050F1FC, 0x98B5, 0x11CF, 0xBB,0x82, 0x00,0xAA,0x00,0xBD,0xCE,0x0B);
289 DEFINE_GUID(CLSID_CCDGenericPropertyPage, 0x3050F17F, 0x98B5, 0x11CF, 0xBB,0x82, 0x00,0xAA,0x00,0xBD,0xCE,0x0B);
290 DEFINE_GUID(CLSID_CDocBrowsePropertyPage, 0x3050F3B4, 0x98B5, 0x11CF, 0xBB,0x82, 0x00,0xAA,0x00,0xBD,0xCE,0x0B);
291 DEFINE_GUID(CLSID_CDwnBindInfo, 0x3050F3C2, 0x98B5, 0x11CF, 0xBB,0x82, 0x00,0xAA,0x00,0xBD,0xCE,0x0B);
292 DEFINE_GUID(CLSID_CHiFiUses, 0x5AAF51B3, 0xB1F0, 0x11D1, 0xB6,0xAB, 0x00,0xA0,0xC9,0x08,0x33,0xE9);
293 DEFINE_GUID(CLSID_CHtmlComponentConstructor, 0x3050F4F8, 0x98B5, 0x11CF, 0xBB,0x82, 0x00,0xAA,0x00,0xBD,0xCE,0x0B);
294 DEFINE_GUID(CLSID_CImageBrowsePropertyPage, 0x3050F3B3, 0x98B5, 0x11CF, 0xBB,0x82, 0x00,0xAA,0x00,0xBD,0xCE,0x0B);
295 DEFINE_GUID(CLSID_CInlineStylePropertyPage, 0x3050F296, 0x98B5, 0x11CF, 0xBB,0x82, 0x00,0xAA,0x00,0xBD,0xCE,0x0B);
296 DEFINE_GUID(CLSID_CPeerHandler, 0x5AAF51B2, 0xB1F0, 0x11D1, 0xB6,0xAB, 0x00,0xA0,0xC9,0x08,0x33,0xE9);
297 DEFINE_GUID(CLSID_CRecalcEngine, 0x3050F499, 0x98B5, 0x11CF, 0xBB,0x82, 0x00,0xAA,0x00,0xBD,0xCE,0x0B);
298 DEFINE_GUID(CLSID_CSvrOMUses, 0x3050F4F0, 0x98B5, 0x11CF, 0xBB,0x82, 0x00,0xAA,0x00,0xBD,0xCE,0x0B);
299 DEFINE_GUID(CLSID_CrSource, 0x65014010, 0x9F62, 0x11D1, 0xA6,0x51, 0x00,0x60,0x08,0x11,0xD5,0xCE);
300 DEFINE_GUID(CLSID_ExternalFrameworkSite, 0x3050F163, 0x98B5, 0x11CF, 0xBB,0x82, 0x00,0xAA,0x00,0xBD,0xCE,0x0B);
301 DEFINE_GUID(CLSID_HTADocument, 0x3050F5C8, 0x98B5, 0x11CF, 0xBB,0x82, 0x00,0xAA,0x00,0xBD,0xCE,0x0B);
302 DEFINE_GUID(CLSID_HTMLLoadOptions, 0x18845040, 0x0FA5, 0x11D1, 0xBA,0x19, 0x00,0xC0,0x4F,0xD9,0x12,0xD0);
303 DEFINE_GUID(CLSID_HTMLPluginDocument, 0x25336921, 0x03F9, 0x11CF, 0x8F,0xD0, 0x00,0xAA,0x00,0x68,0x6F,0x13);
304 DEFINE_GUID(CLSID_HTMLPopup, 0x3050F667, 0x98B5, 0x11CF, 0xBB,0x82, 0x00,0xAA,0x00,0xBD,0xCE,0x0B);
305 DEFINE_GUID(CLSID_HTMLPopupDoc, 0x3050F67D, 0x98B5, 0x11CF, 0xBB,0x82, 0x00,0xAA,0x00,0xBD,0xCE,0x0B);
306 DEFINE_GUID(CLSID_HTMLServerDoc, 0x3050F4E7, 0x98B5, 0x11CF, 0xBB,0x82, 0x00,0xAA,0x00,0xBD,0xCE,0x0B);
307 DEFINE_GUID(CLSID_HTMLWindowProxy, 0x3050F391, 0x98B5, 0x11CF, 0xBB,0x82, 0x00,0xAA,0x00,0xBD,0xCE,0x0B);
308 DEFINE_GUID(CLSID_IImageDecodeFilter, 0x607FD4E8, 0x0A03, 0x11D1, 0xAB,0x1D, 0x00,0xC0,0x4F,0xC9,0xB3,0x04);
309 DEFINE_GUID(CLSID_IImgCtx, 0x3050F3D6, 0x98B5, 0x11CF, 0xBB,0x82, 0x00,0xAA,0x00,0xBD,0xCE,0x0B);
310 DEFINE_GUID(CLSID_IntDitherer, 0x05F6FE1A, 0xECEF, 0x11D0, 0xAA,0xE7, 0x00,0xC0,0x4F,0xC9,0xB3,0x04);
311 DEFINE_GUID(CLSID_MHTMLDocument, 0x3050F3D9, 0x98B5, 0x11CF, 0xBB,0x82, 0x00,0xAA,0x00,0xBD,0xCE,0x0B);
312 DEFINE_GUID(CLSID_Scriptlet, 0xAE24FDAE, 0x03C6, 0x11D1, 0x8B,0x76, 0x00,0x80,0xC7,0x44,0xF3,0x89);
313 DEFINE_GUID(CLSID_TridentAPI, 0x429AF92C, 0xA51F, 0x11D2, 0x86,0x1E, 0x00,0xC0,0x4F,0xA3,0x5C,0x89);
314
315 #define INF_SET_ID(id) \
316     pse[i].pszName = #id; \
317     clsids[i++] = &id;
318
319 #define INF_SET_CLSID(clsid) INF_SET_ID(CLSID_ ## clsid)
320
321 static HRESULT register_server(BOOL do_register)
322 {
323     HRESULT hres;
324     HMODULE hAdvpack;
325     typeof(RegInstall) *pRegInstall;
326     STRTABLE strtable;
327     STRENTRY pse[35];
328     static CLSID const *clsids[35];
329     int i = 0;
330     
331     static const WCHAR wszAdvpack[] = {'a','d','v','p','a','c','k','.','d','l','l',0};
332
333     TRACE("(%x)\n", do_register);
334     
335     INF_SET_CLSID(AboutProtocol);
336     INF_SET_CLSID(CAnchorBrowsePropertyPage);
337     INF_SET_CLSID(CBackgroundPropertyPage);
338     INF_SET_CLSID(CCDAnchorPropertyPage);
339     INF_SET_CLSID(CCDGenericPropertyPage);
340     INF_SET_CLSID(CDocBrowsePropertyPage);
341     INF_SET_CLSID(CDwnBindInfo);
342     INF_SET_CLSID(CHiFiUses);
343     INF_SET_CLSID(CHtmlComponentConstructor);
344     INF_SET_CLSID(CImageBrowsePropertyPage);
345     INF_SET_CLSID(CInlineStylePropertyPage);
346     INF_SET_CLSID(CPeerHandler);
347     INF_SET_CLSID(CRecalcEngine);
348     INF_SET_CLSID(CSvrOMUses);
349     INF_SET_CLSID(CrSource);
350     INF_SET_CLSID(ExternalFrameworkSite);
351     INF_SET_CLSID(HTADocument);
352     INF_SET_CLSID(HTMLDocument);
353     INF_SET_CLSID(HTMLLoadOptions);
354     INF_SET_CLSID(HTMLPluginDocument);
355     INF_SET_CLSID(HTMLPopup);
356     INF_SET_CLSID(HTMLPopupDoc);
357     INF_SET_CLSID(HTMLServerDoc);
358     INF_SET_CLSID(HTMLWindowProxy);
359     INF_SET_CLSID(IImageDecodeFilter);
360     INF_SET_CLSID(IImgCtx);
361     INF_SET_CLSID(IntDitherer);
362     INF_SET_CLSID(JSProtocol);
363     INF_SET_CLSID(MHTMLDocument);
364     INF_SET_CLSID(MailtoProtocol);
365     INF_SET_CLSID(ResProtocol);
366     INF_SET_CLSID(Scriptlet);
367     INF_SET_CLSID(SysimageProtocol);
368     INF_SET_CLSID(TridentAPI);
369     INF_SET_ID(LIBID_MSHTML);
370
371     for(i=0; i < sizeof(pse)/sizeof(pse[0]); i++) {
372         pse[i].pszValue = HeapAlloc(GetProcessHeap(), 0, 39);
373         sprintf(pse[i].pszValue, "{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
374                 clsids[i]->Data1, clsids[i]->Data2, clsids[i]->Data3, clsids[i]->Data4[0],
375                 clsids[i]->Data4[1], clsids[i]->Data4[2], clsids[i]->Data4[3], clsids[i]->Data4[4],
376                 clsids[i]->Data4[5], clsids[i]->Data4[6], clsids[i]->Data4[7]);
377     }
378
379     strtable.cEntries = sizeof(pse)/sizeof(pse[0]);
380     strtable.pse = pse;
381
382     hAdvpack = LoadLibraryW(wszAdvpack);
383     pRegInstall = (typeof(RegInstall)*)GetProcAddress(hAdvpack, "RegInstall");
384
385     hres = pRegInstall(hInst, do_register ? "RegisterDll" : "UnregisterDll", &strtable);
386
387     for(i=0; i < sizeof(pse)/sizeof(pse[0]); i++)
388         HeapFree(GetProcessHeap(), 0, pse[i].pszValue);
389
390     return hres;
391 }
392
393 #undef INF_SET_CLSID
394
395 /***********************************************************************
396  *          DllRegisterServer (MSHTML.@)
397  */
398 HRESULT WINAPI MSHTML_DllRegisterServer(void)
399 {
400     return register_server(TRUE);
401 }
402
403 /***********************************************************************
404  *          DllUnregisterServer (MSHTML.@)
405  */
406 HRESULT WINAPI MSHTML_DllUnregisterServer(void)
407 {
408     return register_server(FALSE);
409 }