2 * Copyright 2005-2006 Jacek Caban for CodeWeavers
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.
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.
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
19 #include "urlmon_main.h"
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(urlmon
);
26 typedef struct name_space
{
32 struct name_space
*next
;
35 typedef struct mime_filter
{
40 struct mime_filter
*next
;
43 static name_space
*name_space_list
= NULL
;
44 static mime_filter
*mime_filter_list
= NULL
;
46 static CRITICAL_SECTION session_cs
;
47 static CRITICAL_SECTION_DEBUG session_cs_dbg
=
50 { &session_cs_dbg
.ProcessLocksList
, &session_cs_dbg
.ProcessLocksList
},
51 0, 0, { (DWORD_PTR
)(__FILE__
": session") }
53 static CRITICAL_SECTION session_cs
= { &session_cs_dbg
, -1, 0, 0, 0, 0 };
55 static name_space
*find_name_space(LPCWSTR protocol
)
59 for(iter
= name_space_list
; iter
; iter
= iter
->next
) {
60 if(!strcmpW(iter
->protocol
, protocol
))
67 static HRESULT
get_protocol_cf(LPCWSTR schema
, DWORD schema_len
, CLSID
*pclsid
, IClassFactory
**ret
)
71 DWORD res
, type
, size
;
76 static const WCHAR wszProtocolsKey
[] =
77 {'P','R','O','T','O','C','O','L','S','\\','H','a','n','d','l','e','r','\\'};
78 static const WCHAR wszCLSID
[] = {'C','L','S','I','D',0};
80 wszKey
= heap_alloc(sizeof(wszProtocolsKey
)+(schema_len
+1)*sizeof(WCHAR
));
81 memcpy(wszKey
, wszProtocolsKey
, sizeof(wszProtocolsKey
));
82 memcpy(wszKey
+ sizeof(wszProtocolsKey
)/sizeof(WCHAR
), schema
, (schema_len
+1)*sizeof(WCHAR
));
84 res
= RegOpenKeyW(HKEY_CLASSES_ROOT
, wszKey
, &hkey
);
86 if(res
!= ERROR_SUCCESS
) {
87 TRACE("Could not open protocol handler key\n");
91 size
= sizeof(str_clsid
);
92 res
= RegQueryValueExW(hkey
, wszCLSID
, NULL
, &type
, (LPBYTE
)str_clsid
, &size
);
94 if(res
!= ERROR_SUCCESS
|| type
!= REG_SZ
) {
95 WARN("Could not get protocol CLSID res=%d\n", res
);
99 hres
= CLSIDFromString(str_clsid
, &clsid
);
101 WARN("CLSIDFromString failed: %08x\n", hres
);
111 return CoGetClassObject(&clsid
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IClassFactory
, (void**)ret
);
114 static HRESULT
register_namespace(IClassFactory
*cf
, REFIID clsid
, LPCWSTR protocol
, BOOL urlmon_protocol
)
116 name_space
*new_name_space
;
118 new_name_space
= heap_alloc(sizeof(name_space
));
121 IClassFactory_AddRef(cf
);
122 new_name_space
->cf
= cf
;
123 new_name_space
->clsid
= *clsid
;
124 new_name_space
->urlmon
= urlmon_protocol
;
125 new_name_space
->protocol
= heap_strdupW(protocol
);
127 EnterCriticalSection(&session_cs
);
129 new_name_space
->next
= name_space_list
;
130 name_space_list
= new_name_space
;
132 LeaveCriticalSection(&session_cs
);
137 static HRESULT
unregister_namespace(IClassFactory
*cf
, LPCWSTR protocol
)
139 name_space
*iter
, *last
= NULL
;
141 EnterCriticalSection(&session_cs
);
143 for(iter
= name_space_list
; iter
; iter
= iter
->next
) {
144 if(iter
->cf
== cf
&& !strcmpW(iter
->protocol
, protocol
))
151 last
->next
= iter
->next
;
153 name_space_list
= iter
->next
;
156 LeaveCriticalSection(&session_cs
);
160 IClassFactory_Release(iter
->cf
);
161 heap_free(iter
->protocol
);
169 void register_urlmon_namespace(IClassFactory
*cf
, REFIID clsid
, LPCWSTR protocol
, BOOL do_register
)
172 register_namespace(cf
, clsid
, protocol
, TRUE
);
174 unregister_namespace(cf
, protocol
);
177 BOOL
is_registered_protocol(LPCWSTR url
)
183 hres
= CoInternetParseUrl(url
, PARSE_SCHEMA
, 0, schema
, sizeof(schema
)/sizeof(schema
[0]),
188 return get_protocol_cf(schema
, schema_len
, NULL
, NULL
) == S_OK
;
191 IInternetProtocolInfo
*get_protocol_info(LPCWSTR url
)
193 IInternetProtocolInfo
*ret
= NULL
;
200 hres
= CoInternetParseUrl(url
, PARSE_SCHEMA
, 0, schema
, sizeof(schema
)/sizeof(schema
[0]),
202 if(FAILED(hres
) || !schema_len
)
205 EnterCriticalSection(&session_cs
);
207 ns
= find_name_space(schema
);
208 if(ns
&& !ns
->urlmon
) {
209 hres
= IClassFactory_QueryInterface(ns
->cf
, &IID_IInternetProtocolInfo
, (void**)&ret
);
211 hres
= IClassFactory_CreateInstance(ns
->cf
, NULL
, &IID_IInternetProtocolInfo
, (void**)&ret
);
214 LeaveCriticalSection(&session_cs
);
216 if(ns
&& SUCCEEDED(hres
))
219 hres
= get_protocol_cf(schema
, schema_len
, NULL
, &cf
);
223 hres
= IClassFactory_QueryInterface(cf
, &IID_IInternetProtocolInfo
, (void**)&ret
);
225 IClassFactory_CreateInstance(cf
, NULL
, &IID_IInternetProtocolInfo
, (void**)&ret
);
226 IClassFactory_Release(cf
);
231 HRESULT
get_protocol_handler(LPCWSTR url
, CLSID
*clsid
, IClassFactory
**ret
)
240 hres
= CoInternetParseUrl(url
, PARSE_SCHEMA
, 0, schema
, sizeof(schema
)/sizeof(schema
[0]),
242 if(FAILED(hres
) || !schema_len
)
243 return schema_len
? hres
: E_FAIL
;
245 EnterCriticalSection(&session_cs
);
247 ns
= find_name_space(schema
);
250 IClassFactory_AddRef(*ret
);
255 LeaveCriticalSection(&session_cs
);
260 return get_protocol_cf(schema
, schema_len
, clsid
, ret
);
263 static HRESULT WINAPI
InternetSession_QueryInterface(IInternetSession
*iface
,
264 REFIID riid
, void **ppv
)
266 TRACE("(%s %p)\n", debugstr_guid(riid
), ppv
);
268 if(IsEqualGUID(&IID_IUnknown
, riid
) || IsEqualGUID(&IID_IInternetSession
, riid
)) {
270 IInternetSession_AddRef(iface
);
275 return E_NOINTERFACE
;
278 static ULONG WINAPI
InternetSession_AddRef(IInternetSession
*iface
)
285 static ULONG WINAPI
InternetSession_Release(IInternetSession
*iface
)
288 URLMON_UnlockModule();
292 static HRESULT WINAPI
InternetSession_RegisterNameSpace(IInternetSession
*iface
,
293 IClassFactory
*pCF
, REFCLSID rclsid
, LPCWSTR pwzProtocol
, ULONG cPatterns
,
294 const LPCWSTR
*ppwzPatterns
, DWORD dwReserved
)
296 TRACE("(%p %s %s %d %p %d)\n", pCF
, debugstr_guid(rclsid
), debugstr_w(pwzProtocol
),
297 cPatterns
, ppwzPatterns
, dwReserved
);
299 if(cPatterns
|| ppwzPatterns
)
300 FIXME("patterns not supported\n");
302 WARN("dwReserved = %d\n", dwReserved
);
304 if(!pCF
|| !pwzProtocol
)
307 return register_namespace(pCF
, rclsid
, pwzProtocol
, FALSE
);
310 static HRESULT WINAPI
InternetSession_UnregisterNameSpace(IInternetSession
*iface
,
311 IClassFactory
*pCF
, LPCWSTR pszProtocol
)
313 TRACE("(%p %s)\n", pCF
, debugstr_w(pszProtocol
));
315 if(!pCF
|| !pszProtocol
)
318 return unregister_namespace(pCF
, pszProtocol
);
321 static HRESULT WINAPI
InternetSession_RegisterMimeFilter(IInternetSession
*iface
,
322 IClassFactory
*pCF
, REFCLSID rclsid
, LPCWSTR pwzType
)
326 TRACE("(%p %s %s)\n", pCF
, debugstr_guid(rclsid
), debugstr_w(pwzType
));
328 filter
= heap_alloc(sizeof(mime_filter
));
330 IClassFactory_AddRef(pCF
);
332 filter
->clsid
= *rclsid
;
333 filter
->mime
= heap_strdupW(pwzType
);
335 EnterCriticalSection(&session_cs
);
337 filter
->next
= mime_filter_list
;
338 mime_filter_list
= filter
;
340 LeaveCriticalSection(&session_cs
);
345 static HRESULT WINAPI
InternetSession_UnregisterMimeFilter(IInternetSession
*iface
,
346 IClassFactory
*pCF
, LPCWSTR pwzType
)
348 mime_filter
*iter
, *prev
= NULL
;
350 TRACE("(%p %s)\n", pCF
, debugstr_w(pwzType
));
352 EnterCriticalSection(&session_cs
);
354 for(iter
= mime_filter_list
; iter
; iter
= iter
->next
) {
355 if(iter
->cf
== pCF
&& !strcmpW(iter
->mime
, pwzType
))
362 prev
->next
= iter
->next
;
364 mime_filter_list
= iter
->next
;
367 LeaveCriticalSection(&session_cs
);
370 IClassFactory_Release(iter
->cf
);
371 heap_free(iter
->mime
);
378 static HRESULT WINAPI
InternetSession_CreateBinding(IInternetSession
*iface
,
379 LPBC pBC
, LPCWSTR szUrl
, IUnknown
*pUnkOuter
, IUnknown
**ppUnk
,
380 IInternetProtocol
**ppOInetProt
, DWORD dwOption
)
382 TRACE("(%p %s %p %p %p %08x)\n", pBC
, debugstr_w(szUrl
), pUnkOuter
, ppUnk
,
383 ppOInetProt
, dwOption
);
385 if(pBC
|| pUnkOuter
|| ppUnk
|| dwOption
)
386 FIXME("Unsupported arguments\n");
388 return create_binding_protocol(szUrl
, FALSE
, ppOInetProt
);
391 static HRESULT WINAPI
InternetSession_SetSessionOption(IInternetSession
*iface
,
392 DWORD dwOption
, LPVOID pBuffer
, DWORD dwBufferLength
, DWORD dwReserved
)
394 FIXME("(%08x %p %d %d)\n", dwOption
, pBuffer
, dwBufferLength
, dwReserved
);
398 static const IInternetSessionVtbl InternetSessionVtbl
= {
399 InternetSession_QueryInterface
,
400 InternetSession_AddRef
,
401 InternetSession_Release
,
402 InternetSession_RegisterNameSpace
,
403 InternetSession_UnregisterNameSpace
,
404 InternetSession_RegisterMimeFilter
,
405 InternetSession_UnregisterMimeFilter
,
406 InternetSession_CreateBinding
,
407 InternetSession_SetSessionOption
410 static IInternetSession InternetSession
= { &InternetSessionVtbl
};
412 /***********************************************************************
413 * CoInternetGetSession (URLMON.@)
415 * Create a new internet session and return an IInternetSession interface
419 * dwSessionMode [I] Mode for the internet session
420 * ppIInternetSession [O] Destination for creates IInternetSession object
421 * dwReserved [I] Reserved, must be 0.
424 * Success: S_OK. ppIInternetSession contains the IInternetSession interface.
425 * Failure: E_INVALIDARG, if any argument is invalid, or
426 * E_OUTOFMEMORY if memory allocation fails.
428 HRESULT WINAPI
CoInternetGetSession(DWORD dwSessionMode
, IInternetSession
**ppIInternetSession
,
431 TRACE("(%d %p %d)\n", dwSessionMode
, ppIInternetSession
, dwReserved
);
434 ERR("dwSessionMode=%d\n", dwSessionMode
);
436 ERR("dwReserved=%d\n", dwReserved
);
438 IInternetSession_AddRef(&InternetSession
);
439 *ppIInternetSession
= &InternetSession
;
443 /**************************************************************************
444 * UrlMkGetSessionOption (URLMON.@)
446 static BOOL
get_url_encoding(HKEY root
, DWORD
*encoding
)
448 DWORD size
= sizeof(DWORD
), res
, type
;
451 static const WCHAR wszKeyName
[] =
452 {'S','O','F','T','W','A','R','E',
453 '\\','M','i','c','r','o','s','o','f','t',
454 '\\','W','i','n','d','o','w','s',
455 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
456 '\\','I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0};
457 static const WCHAR wszUrlEncoding
[] = {'U','r','l','E','n','c','o','d','i','n','g',0};
459 res
= RegOpenKeyW(root
, wszKeyName
, &hkey
);
460 if(res
!= ERROR_SUCCESS
)
463 res
= RegQueryValueExW(hkey
, wszUrlEncoding
, NULL
, &type
, (LPBYTE
)encoding
, &size
);
466 return res
== ERROR_SUCCESS
;
469 HRESULT WINAPI
UrlMkGetSessionOption(DWORD dwOption
, LPVOID pBuffer
, DWORD dwBufferLength
,
470 DWORD
* pdwBufferLength
, DWORD dwReserved
)
472 TRACE("(%x, %p, %d, %p)\n", dwOption
, pBuffer
, dwBufferLength
, pdwBufferLength
);
475 WARN("dwReserved = %d\n", dwReserved
);
478 case URLMON_OPTION_URL_ENCODING
: {
481 if(!pBuffer
|| dwBufferLength
< sizeof(DWORD
) || !pdwBufferLength
)
484 if(!get_url_encoding(HKEY_CURRENT_USER
, &encoding
))
485 get_url_encoding(HKEY_LOCAL_MACHINE
, &encoding
);
487 *pdwBufferLength
= sizeof(DWORD
);
488 *(DWORD
*)pBuffer
= encoding
? URL_ENCODING_DISABLE_UTF8
: URL_ENCODING_ENABLE_UTF8
;
492 FIXME("unsupported option %x\n", dwOption
);