urlmon: Added registering name space test.
[wine] / dlls / urlmon / urlmon_main.c
1 /*
2  * UrlMon
3  *
4  * Copyright (c) 2000 Patrik Stridvall
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winreg.h"
28
29 #define NO_SHLWAPI_REG
30 #include "shlwapi.h"
31
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
34
35 #include "winuser.h"
36 #include "urlmon.h"
37 #include "urlmon_main.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
40
41 LONG URLMON_refCount = 0;
42
43 HINSTANCE URLMON_hInstance = 0;
44
45 /***********************************************************************
46  *              DllMain (URLMON.init)
47  */
48 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
49 {
50     TRACE("%p 0x%lx %p\n", hinstDLL, fdwReason, fImpLoad);
51
52     switch(fdwReason) {
53     case DLL_PROCESS_ATTACH:
54         DisableThreadLibraryCalls(hinstDLL);
55         URLMON_hInstance = hinstDLL;
56         break;
57
58     case DLL_PROCESS_DETACH:
59         URLMON_hInstance = 0;
60         break;
61     }
62     return TRUE;
63 }
64
65
66 /***********************************************************************
67  *              DllInstall (URLMON.@)
68  */
69 HRESULT WINAPI DllInstall(BOOL bInstall, LPCWSTR cmdline)
70 {
71   FIXME("(%s, %s): stub\n", bInstall?"TRUE":"FALSE",
72         debugstr_w(cmdline));
73
74   return S_OK;
75 }
76
77 /***********************************************************************
78  *              DllCanUnloadNow (URLMON.@)
79  */
80 HRESULT WINAPI DllCanUnloadNow(void)
81 {
82     return URLMON_refCount != 0 ? S_FALSE : S_OK;
83 }
84
85
86
87 /******************************************************************************
88  * Urlmon ClassFactory
89  */
90 typedef struct {
91     IClassFactory ITF_IClassFactory;
92
93     LONG ref;
94     HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, LPVOID *ppObj);
95 } IClassFactoryImpl;
96
97 struct object_creation_info
98 {
99     const CLSID *clsid;
100     HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, LPVOID *ppObj);
101 };
102  
103 static const struct object_creation_info object_creation[] =
104 {
105     { &CLSID_FileProtocol, FileProtocol_Construct },
106     { &CLSID_FtpProtocol, FtpProtocol_Construct },
107     { &CLSID_HttpProtocol, HttpProtocol_Construct },
108     { &CLSID_InternetSecurityManager, &SecManagerImpl_Construct },
109     { &CLSID_InternetZoneManager, ZoneMgrImpl_Construct }
110 };
111
112 static HRESULT WINAPI
113 CF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj)
114 {
115     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
116
117     if (IsEqualGUID(riid, &IID_IUnknown)
118         || IsEqualGUID(riid, &IID_IClassFactory))
119     {
120         IClassFactory_AddRef(iface);
121         *ppobj = This;
122         return S_OK;
123     }
124
125     WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
126     return E_NOINTERFACE;
127 }
128
129 static ULONG WINAPI CF_AddRef(LPCLASSFACTORY iface)
130 {
131     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
132     return InterlockedIncrement(&This->ref);
133 }
134
135 static ULONG WINAPI CF_Release(LPCLASSFACTORY iface)
136 {
137     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
138
139     ULONG ref = InterlockedDecrement(&This->ref);
140
141     if (ref == 0) {
142         HeapFree(GetProcessHeap(), 0, This);
143         URLMON_UnlockModule();
144     }
145
146     return ref;
147 }
148
149
150 static HRESULT WINAPI CF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pOuter,
151                                         REFIID riid, LPVOID *ppobj)
152 {
153     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
154     HRESULT hres;
155     LPUNKNOWN punk;
156     
157     TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
158
159     *ppobj = NULL;
160     if(SUCCEEDED(hres = This->pfnCreateInstance(pOuter, (LPVOID *) &punk))) {
161         hres = IUnknown_QueryInterface(punk, riid, ppobj);
162         IUnknown_Release(punk);
163     }
164     return hres;
165 }
166
167 static HRESULT WINAPI CF_LockServer(LPCLASSFACTORY iface,BOOL dolock)
168 {
169     TRACE("(%d)\n", dolock);
170
171     if (dolock)
172            URLMON_LockModule();
173     else
174            URLMON_UnlockModule();
175
176     return S_OK;
177 }
178
179 static const IClassFactoryVtbl CF_Vtbl =
180 {
181     CF_QueryInterface,
182     CF_AddRef,
183     CF_Release,
184     CF_CreateInstance,
185     CF_LockServer
186 };
187
188 /*******************************************************************************
189  * DllGetClassObject [URLMON.@]
190  * Retrieves class object from a DLL object
191  *
192  * NOTES
193  *    Docs say returns STDAPI
194  *
195  * PARAMS
196  *    rclsid [I] CLSID for the class object
197  *    riid   [I] Reference to identifier of interface for class object
198  *    ppv    [O] Address of variable to receive interface pointer for riid
199  *
200  * RETURNS
201  *    Success: S_OK
202  *    Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
203  *             E_UNEXPECTED
204  */
205
206 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
207 {
208     int i;
209     IClassFactoryImpl *factory;
210     
211     TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
212     
213     if ( !IsEqualGUID( &IID_IClassFactory, riid )
214          && ! IsEqualGUID( &IID_IUnknown, riid) )
215         return E_NOINTERFACE;
216
217     for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
218     {
219         if (IsEqualGUID(object_creation[i].clsid, rclsid))
220             break;
221     }
222
223     if (i == sizeof(object_creation)/sizeof(object_creation[0]))
224     {
225         FIXME("%s: no class found.\n", debugstr_guid(rclsid));
226         return CLASS_E_CLASSNOTAVAILABLE;
227     }
228
229     factory = HeapAlloc(GetProcessHeap(), 0, sizeof(*factory));
230     if (factory == NULL) return E_OUTOFMEMORY;
231
232     factory->ITF_IClassFactory.lpVtbl = &CF_Vtbl;
233     factory->ref = 1;
234     factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
235
236     *ppv = &(factory->ITF_IClassFactory);
237
238     URLMON_LockModule();
239
240     return S_OK;
241 }
242
243
244 /***********************************************************************
245  *              DllRegisterServerEx (URLMON.@)
246  */
247 HRESULT WINAPI DllRegisterServerEx(void)
248 {
249     FIXME("(void): stub\n");
250
251     return E_FAIL;
252 }
253
254 /**************************************************************************
255  *                 UrlMkSetSessionOption (URLMON.@)
256  */
257 HRESULT WINAPI UrlMkSetSessionOption(DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength,
258                                         DWORD Reserved)
259 {
260     FIXME("(%#lx, %p, %#lx): stub\n", dwOption, pBuffer, dwBufferLength);
261
262     return S_OK;
263 }
264
265 /**************************************************************************
266  *                 UrlMkGetSessionOption (URLMON.@)
267  */
268 HRESULT WINAPI UrlMkGetSessionOption(DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength,
269                                         DWORD* pdwBufferLength, DWORD dwReserved)
270 {
271     FIXME("(%#lx, %p, %#lx, %p): stub\n", dwOption, pBuffer, dwBufferLength, pdwBufferLength);
272
273     return S_OK;
274 }
275
276 static const CHAR Agent[] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)";
277
278 /**************************************************************************
279  *                 ObtainUserAgentString (URLMON.@)
280  */
281 HRESULT WINAPI ObtainUserAgentString(DWORD dwOption, LPSTR pcszUAOut, DWORD *cbSize)
282 {
283     FIXME("(%ld, %p, %p): stub\n", dwOption, pcszUAOut, cbSize);
284
285     if(dwOption) {
286       ERR("dwOption: %ld, must be zero\n", dwOption);
287     }
288
289     if (sizeof(Agent) < *cbSize)
290         *cbSize = sizeof(Agent);
291     lstrcpynA(pcszUAOut, Agent, *cbSize); 
292
293     return S_OK;
294 }
295
296 HRESULT WINAPI CoInternetCompareUrl(LPCWSTR pwzUrl1, LPCWSTR pwzUrl2, DWORD dwCompareFlags)
297 {
298     TRACE("(%s,%s,%08lx)\n", debugstr_w(pwzUrl1), debugstr_w(pwzUrl2), dwCompareFlags);
299     return UrlCompareW(pwzUrl1, pwzUrl2, dwCompareFlags)==0?S_OK:S_FALSE;
300 }
301
302 /**************************************************************************
303  *                 IsValidURL (URLMON.@)
304  * 
305  * Determines if a specified string is a valid URL.
306  *
307  * PARAMS
308  *  pBC        [I] ignored, must be NULL.
309  *  szURL      [I] string that represents the URL in question.
310  *  dwReserved [I] reserved and must be zero.
311  *
312  * RETURNS
313  *  Success: S_OK.
314  *  Failure: S_FALSE.
315  *  returns E_INVALIDARG if one or more of the args is invalid.
316  *
317  * TODO:
318  *  test functionality against windows to see what a valid URL is.
319  */
320 HRESULT WINAPI IsValidURL(LPBC pBC, LPCWSTR szURL, DWORD dwReserved)
321 {
322     FIXME("(%p, %s, %ld): stub\n", pBC, debugstr_w(szURL), dwReserved);
323     
324     if (pBC != NULL || dwReserved != 0)
325         return E_INVALIDARG;
326     
327     return S_OK;
328 }
329
330 /**************************************************************************
331  *                 FaultInIEFeature (URLMON.@)
332  *
333  *  Undocumented.  Appears to be used by native shdocvw.dll.
334  */
335 HRESULT WINAPI FaultInIEFeature( HWND hwnd, uCLSSPEC * pClassSpec,
336                                  QUERYCONTEXT *pQuery, DWORD flags )
337 {
338     FIXME("%p %p %p %08lx\n", hwnd, pClassSpec, pQuery, flags);
339     return E_NOTIMPL;
340 }
341
342 /**************************************************************************
343  *                 CoGetClassObjectFromURL (URLMON.@)
344  */
345 HRESULT WINAPI CoGetClassObjectFromURL( REFCLSID rclsid, LPCWSTR szCodeURL, DWORD dwFileVersionMS,
346                                         DWORD dwFileVersionLS, LPCWSTR szContentType,
347                                         LPBINDCTX pBindCtx, DWORD dwClsContext, LPVOID pvReserved,
348                                         REFIID riid, LPVOID *ppv )
349 {
350     FIXME("(%s %s %ld %ld %s %p %ld %p %s %p) Stub!\n", debugstr_guid(rclsid), debugstr_w(szCodeURL),
351         dwFileVersionMS, dwFileVersionLS, debugstr_w(szContentType), pBindCtx, dwClsContext, pvReserved,
352         debugstr_guid(riid), ppv);
353     return E_NOINTERFACE;
354 }
355
356 /***********************************************************************
357  *           ReleaseBindInfo (URLMON.@)
358  *
359  * Release the resources used by the specified BINDINFO structure.
360  *
361  * PARAMS
362  *  pbindinfo [I] BINDINFO to release.
363  *
364  * RETURNS
365  *  Nothing.
366  */
367 void WINAPI ReleaseBindInfo(BINDINFO* pbindinfo)
368 {
369     TRACE("(%p)\n", pbindinfo);
370
371     if(!pbindinfo)
372         return;
373
374     CoTaskMemFree(pbindinfo->szExtraInfo);
375
376     if(pbindinfo->pUnk)
377         IUnknown_Release(pbindinfo->pUnk);
378 }
379
380 /***********************************************************************
381  *           FindMimeFromData (URLMON.@)
382  *
383  * Determines the Multipurpose Internet Mail Extensions (MIME) type from the data provided.
384  */
385 HRESULT WINAPI FindMimeFromData(LPBC pBC, LPCWSTR pwzUrl, LPVOID pBuffer,
386         DWORD cbSize, LPCWSTR pwzMimeProposed, DWORD dwMimeFlags,
387         LPWSTR* ppwzMimeOut, DWORD dwReserved)
388 {
389     TRACE("(%p,%s,%p,%ld,%s,0x%lx,%p,0x%lx)\n", pBC, debugstr_w(pwzUrl), pBuffer, cbSize,
390             debugstr_w(pwzMimeProposed), dwMimeFlags, ppwzMimeOut, dwReserved);
391
392     if(dwMimeFlags)
393         WARN("dwMimeFlags=%08lx\n", dwMimeFlags);
394     if(dwReserved)
395         WARN("dwReserved=%ld\n", dwReserved);
396
397     /* pBC seams to not be used */
398
399     if(!ppwzMimeOut || (!pwzUrl && !pBuffer))
400         return E_INVALIDARG;
401
402     if(pwzMimeProposed && (!pwzUrl || !pBuffer || (pBuffer && !cbSize))) {
403         DWORD len;
404
405         if(!pwzMimeProposed)
406             return E_FAIL;
407
408         len = strlenW(pwzMimeProposed)+1;
409         *ppwzMimeOut = CoTaskMemAlloc(len*sizeof(WCHAR));
410         memcpy(*ppwzMimeOut, pwzMimeProposed, len*sizeof(WCHAR));
411         return S_OK;
412     }
413
414     if(pBuffer) {
415         UCHAR *ptr = pBuffer;
416         DWORD len;
417         LPCWSTR ret;
418
419         static const WCHAR wszAppOctetStream[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
420                 'o','c','t','e','t','-','s','t','r','e','a','m','\0'};
421         static const WCHAR wszTextPlain[] = {'t','e','x','t','/','p','l','a','i','n','\0'};
422
423         if(!cbSize)
424             return E_FAIL;
425
426         ret = wszTextPlain;
427         for(ptr = pBuffer; ptr < (UCHAR*)pBuffer+cbSize-1; ptr++) {
428             if(*ptr < 0x20 && *ptr != '\n' && *ptr != '\r' && *ptr != '\t') {
429                 ret = wszAppOctetStream;
430                 break;
431             }
432         }
433
434         len = strlenW(ret)+1;
435         *ppwzMimeOut = CoTaskMemAlloc(len*sizeof(WCHAR));
436         memcpy(*ppwzMimeOut, ret, len*sizeof(WCHAR));
437         return S_OK;
438     }
439
440     if(pwzUrl) {
441         HKEY hkey;
442         DWORD res, size;
443         LPCWSTR ptr;
444         WCHAR mime[64];
445
446         static const WCHAR wszContentType[] =
447                 {'C','o','n','t','e','n','t',' ','T','y','p','e','\0'};
448
449         ptr = strrchrW(pwzUrl, '.');
450         if(!ptr)
451             return E_FAIL;
452
453         res = RegOpenKeyW(HKEY_CLASSES_ROOT, ptr, &hkey);
454         if(res != ERROR_SUCCESS)
455             return E_FAIL;
456
457         size = sizeof(mime);
458         res = RegQueryValueExW(hkey, wszContentType, NULL, NULL, (LPBYTE)mime, &size);
459         RegCloseKey(hkey);
460         if(res != ERROR_SUCCESS)
461             return E_FAIL;
462
463         *ppwzMimeOut = CoTaskMemAlloc(size);
464         memcpy(*ppwzMimeOut, mime, size);
465         return S_OK;
466     }
467
468     return E_FAIL;
469 }