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 {
31 struct name_space *next;
34 typedef struct mime_filter {
39 struct mime_filter *next;
42 static name_space *name_space_list = NULL;
43 static mime_filter *mime_filter_list = NULL;
45 static name_space *find_name_space(LPCWSTR protocol)
49 for(iter = name_space_list; iter; iter = iter->next) {
50 if(!strcmpW(iter->protocol, protocol))
57 static HRESULT get_protocol_cf(LPCWSTR schema, DWORD schema_len, CLSID *pclsid, IClassFactory **ret)
61 DWORD res, type, size;
66 static const WCHAR wszProtocolsKey[] =
67 {'P','R','O','T','O','C','O','L','S','\\','H','a','n','d','l','e','r','\\'};
68 static const WCHAR wszCLSID[] = {'C','L','S','I','D',0};
70 wszKey = heap_alloc(sizeof(wszProtocolsKey)+(schema_len+1)*sizeof(WCHAR));
71 memcpy(wszKey, wszProtocolsKey, sizeof(wszProtocolsKey));
72 memcpy(wszKey + sizeof(wszProtocolsKey)/sizeof(WCHAR), schema, (schema_len+1)*sizeof(WCHAR));
74 res = RegOpenKeyW(HKEY_CLASSES_ROOT, wszKey, &hkey);
76 if(res != ERROR_SUCCESS) {
77 TRACE("Could not open protocol handler key\n");
81 size = sizeof(str_clsid);
82 res = RegQueryValueExW(hkey, wszCLSID, NULL, &type, (LPBYTE)str_clsid, &size);
84 if(res != ERROR_SUCCESS || type != REG_SZ) {
85 WARN("Could not get protocol CLSID res=%d\n", res);
89 hres = CLSIDFromString(str_clsid, &clsid);
91 WARN("CLSIDFromString failed: %08x\n", hres);
98 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IClassFactory, (void**)ret);
101 IInternetProtocolInfo *get_protocol_info(LPCWSTR url)
103 IInternetProtocolInfo *ret = NULL;
110 hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, schema, sizeof(schema)/sizeof(schema[0]),
112 if(FAILED(hres) || !schema_len)
115 ns = find_name_space(schema);
117 hres = IClassFactory_QueryInterface(ns->cf, &IID_IInternetProtocolInfo, (void**)&ret);
121 hres = IClassFactory_CreateInstance(ns->cf, NULL, &IID_IInternetProtocolInfo, (void**)&ret);
126 hres = get_protocol_cf(schema, schema_len, NULL, &cf);
130 hres = IClassFactory_QueryInterface(cf, &IID_IInternetProtocolInfo, (void**)&ret);
132 IClassFactory_CreateInstance(cf, NULL, &IID_IInternetProtocolInfo, (void**)&ret);
133 IClassFactory_Release(cf);
138 HRESULT get_protocol_handler(LPCWSTR url, CLSID *clsid, IClassFactory **ret)
145 hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, schema, sizeof(schema)/sizeof(schema[0]),
147 if(FAILED(hres) || !schema_len)
148 return schema_len ? hres : E_FAIL;
150 ns = find_name_space(schema);
153 IClassFactory_AddRef(*ret);
159 return get_protocol_cf(schema, schema_len, clsid, ret);
162 static HRESULT WINAPI InternetSession_QueryInterface(IInternetSession *iface,
163 REFIID riid, void **ppv)
165 TRACE("(%s %p)\n", debugstr_guid(riid), ppv);
167 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetSession, riid)) {
169 IInternetSession_AddRef(iface);
174 return E_NOINTERFACE;
177 static ULONG WINAPI InternetSession_AddRef(IInternetSession *iface)
184 static ULONG WINAPI InternetSession_Release(IInternetSession *iface)
187 URLMON_UnlockModule();
191 static HRESULT WINAPI InternetSession_RegisterNameSpace(IInternetSession *iface,
192 IClassFactory *pCF, REFCLSID rclsid, LPCWSTR pwzProtocol, ULONG cPatterns,
193 const LPCWSTR *ppwzPatterns, DWORD dwReserved)
195 name_space *new_name_space;
197 TRACE("(%p %s %s %d %p %d)\n", pCF, debugstr_guid(rclsid), debugstr_w(pwzProtocol),
198 cPatterns, ppwzPatterns, dwReserved);
200 if(cPatterns || ppwzPatterns)
201 FIXME("patterns not supported\n");
203 WARN("dwReserved = %d\n", dwReserved);
205 if(!pCF || !pwzProtocol)
208 new_name_space = heap_alloc(sizeof(name_space));
210 IClassFactory_AddRef(pCF);
211 new_name_space->cf = pCF;
212 new_name_space->clsid = *rclsid;
213 new_name_space->protocol = heap_strdupW(pwzProtocol);
215 new_name_space->next = name_space_list;
216 name_space_list = new_name_space;
220 static HRESULT WINAPI InternetSession_UnregisterNameSpace(IInternetSession *iface,
221 IClassFactory *pCF, LPCWSTR pszProtocol)
223 name_space *iter, *last = NULL;
225 TRACE("(%p %s)\n", pCF, debugstr_w(pszProtocol));
227 if(!pCF || !pszProtocol)
230 for(iter = name_space_list; iter; iter = iter->next) {
231 if(iter->cf == pCF && !strcmpW(iter->protocol, pszProtocol))
240 last->next = iter->next;
242 name_space_list = iter->next;
244 IClassFactory_Release(iter->cf);
245 heap_free(iter->protocol);
251 static HRESULT WINAPI InternetSession_RegisterMimeFilter(IInternetSession *iface,
252 IClassFactory *pCF, REFCLSID rclsid, LPCWSTR pwzType)
256 TRACE("(%p %s %s)\n", pCF, debugstr_guid(rclsid), debugstr_w(pwzType));
258 filter = heap_alloc(sizeof(mime_filter));
260 IClassFactory_AddRef(pCF);
262 filter->clsid = *rclsid;
263 filter->mime = heap_strdupW(pwzType);
265 filter->next = mime_filter_list;
266 mime_filter_list = filter;
271 static HRESULT WINAPI InternetSession_UnregisterMimeFilter(IInternetSession *iface,
272 IClassFactory *pCF, LPCWSTR pwzType)
274 mime_filter *iter, *prev = NULL;
276 TRACE("(%p %s)\n", pCF, debugstr_w(pwzType));
278 for(iter = mime_filter_list; iter; iter = iter->next) {
279 if(iter->cf == pCF && !strcmpW(iter->mime, pwzType))
288 prev->next = iter->next;
290 mime_filter_list = iter->next;
292 IClassFactory_Release(iter->cf);
293 heap_free(iter->mime);
299 static HRESULT WINAPI InternetSession_CreateBinding(IInternetSession *iface,
300 LPBC pBC, LPCWSTR szUrl, IUnknown *pUnkOuter, IUnknown **ppUnk,
301 IInternetProtocol **ppOInetProt, DWORD dwOption)
303 TRACE("(%p %s %p %p %p %08x)\n", pBC, debugstr_w(szUrl), pUnkOuter, ppUnk,
304 ppOInetProt, dwOption);
306 if(pBC || pUnkOuter || ppUnk || dwOption)
307 FIXME("Unsupported arguments\n");
309 return create_binding_protocol(szUrl, FALSE, ppOInetProt);
312 static HRESULT WINAPI InternetSession_SetSessionOption(IInternetSession *iface,
313 DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength, DWORD dwReserved)
315 FIXME("(%08x %p %d %d)\n", dwOption, pBuffer, dwBufferLength, dwReserved);
319 static const IInternetSessionVtbl InternetSessionVtbl = {
320 InternetSession_QueryInterface,
321 InternetSession_AddRef,
322 InternetSession_Release,
323 InternetSession_RegisterNameSpace,
324 InternetSession_UnregisterNameSpace,
325 InternetSession_RegisterMimeFilter,
326 InternetSession_UnregisterMimeFilter,
327 InternetSession_CreateBinding,
328 InternetSession_SetSessionOption
331 static IInternetSession InternetSession = { &InternetSessionVtbl };
333 /***********************************************************************
334 * CoInternetGetSession (URLMON.@)
336 * Create a new internet session and return an IInternetSession interface
340 * dwSessionMode [I] Mode for the internet session
341 * ppIInternetSession [O] Destination for creates IInternetSession object
342 * dwReserved [I] Reserved, must be 0.
345 * Success: S_OK. ppIInternetSession contains the IInternetSession interface.
346 * Failure: E_INVALIDARG, if any argument is invalid, or
347 * E_OUTOFMEMORY if memory allocation fails.
349 HRESULT WINAPI CoInternetGetSession(DWORD dwSessionMode, IInternetSession **ppIInternetSession,
352 TRACE("(%d %p %d)\n", dwSessionMode, ppIInternetSession, dwReserved);
355 ERR("dwSessionMode=%d\n", dwSessionMode);
357 ERR("dwReserved=%d\n", dwReserved);
359 IInternetSession_AddRef(&InternetSession);
360 *ppIInternetSession = &InternetSession;
364 /**************************************************************************
365 * UrlMkGetSessionOption (URLMON.@)
367 static BOOL get_url_encoding(HKEY root, DWORD *encoding)
369 DWORD size = sizeof(DWORD), res, type;
372 static const WCHAR wszKeyName[] =
373 {'S','O','F','T','W','A','R','E',
374 '\\','M','i','c','r','o','s','o','f','t',
375 '\\','W','i','n','d','o','w','s',
376 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
377 '\\','I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0};
378 static const WCHAR wszUrlEncoding[] = {'U','r','l','E','n','c','o','d','i','n','g',0};
380 res = RegOpenKeyW(root, wszKeyName, &hkey);
381 if(res != ERROR_SUCCESS)
384 res = RegQueryValueExW(hkey, wszUrlEncoding, NULL, &type, (LPBYTE)encoding, &size);
387 return res == ERROR_SUCCESS;
390 HRESULT WINAPI UrlMkGetSessionOption(DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength,
391 DWORD* pdwBufferLength, DWORD dwReserved)
393 TRACE("(%x, %p, %d, %p)\n", dwOption, pBuffer, dwBufferLength, pdwBufferLength);
396 WARN("dwReserved = %d\n", dwReserved);
399 case URLMON_OPTION_URL_ENCODING: {
402 if(!pBuffer || dwBufferLength < sizeof(DWORD) || !pdwBufferLength)
405 if(!get_url_encoding(HKEY_CURRENT_USER, &encoding))
406 get_url_encoding(HKEY_LOCAL_MACHINE, &encoding);
408 *pdwBufferLength = sizeof(DWORD);
409 *(DWORD*)pBuffer = encoding ? URL_ENCODING_DISABLE_UTF8 : URL_ENCODING_ENABLE_UTF8;
413 FIXME("unsupported option %x\n", dwOption);