urlmon: Check if protocol object implements IInternetProtocolInfo interface.
[wine.git] / dlls / urlmon / session.c
blob6e809968054b283698866266ba648c52480658cb
1 /*
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 <stdarg.h>
21 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "winreg.h"
27 #include "ole2.h"
28 #include "urlmon.h"
29 #include "urlmon_main.h"
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
36 typedef struct name_space {
37 LPWSTR protocol;
38 IClassFactory *cf;
39 CLSID clsid;
41 struct name_space *next;
42 } name_space;
44 static name_space *name_space_list = NULL;
46 static name_space *find_name_space(LPCWSTR protocol)
48 name_space *iter;
50 for(iter = name_space_list; iter; iter = iter->next) {
51 if(!strcmpW(iter->protocol, protocol))
52 return iter;
55 return NULL;
58 static HRESULT get_protocol_cf(LPCWSTR schema, DWORD schema_len, CLSID *pclsid, IClassFactory **ret)
60 WCHAR str_clsid[64];
61 HKEY hkey = NULL;
62 DWORD res, type, size;
63 CLSID clsid;
64 LPWSTR wszKey;
65 HRESULT hres;
67 static const WCHAR wszProtocolsKey[] =
68 {'P','R','O','T','O','C','O','L','S','\\','H','a','n','d','l','e','r','\\'};
69 static const WCHAR wszCLSID[] = {'C','L','S','I','D',0};
71 wszKey = HeapAlloc(GetProcessHeap(), 0, sizeof(wszProtocolsKey)+(schema_len+1)*sizeof(WCHAR));
72 memcpy(wszKey, wszProtocolsKey, sizeof(wszProtocolsKey));
73 memcpy(wszKey + sizeof(wszProtocolsKey)/sizeof(WCHAR), schema, (schema_len+1)*sizeof(WCHAR));
75 res = RegOpenKeyW(HKEY_CLASSES_ROOT, wszKey, &hkey);
76 HeapFree(GetProcessHeap(), 0, wszKey);
77 if(res != ERROR_SUCCESS) {
78 TRACE("Could not open protocol handler key\n");
79 return E_FAIL;
82 size = sizeof(str_clsid);
83 res = RegQueryValueExW(hkey, wszCLSID, NULL, &type, (LPBYTE)str_clsid, &size);
84 RegCloseKey(hkey);
85 if(res != ERROR_SUCCESS || type != REG_SZ) {
86 WARN("Could not get protocol CLSID res=%d\n", res);
87 return E_FAIL;
90 hres = CLSIDFromString(str_clsid, &clsid);
91 if(FAILED(hres)) {
92 WARN("CLSIDFromString failed: %08x\n", hres);
93 return hres;
96 if(pclsid)
97 *pclsid = clsid;
99 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IClassFactory, (void**)ret);
102 IInternetProtocolInfo *get_protocol_info(LPCWSTR url)
104 IInternetProtocolInfo *ret = NULL;
105 IClassFactory *cf;
106 name_space *ns;
107 WCHAR schema[64];
108 DWORD schema_len;
109 HRESULT hres;
111 hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, schema, sizeof(schema)/sizeof(schema[0]),
112 &schema_len, 0);
113 if(FAILED(hres) || !schema_len)
114 return NULL;
116 ns = find_name_space(schema);
117 if(ns) {
118 hres = IClassFactory_QueryInterface(ns->cf, &IID_IInternetProtocolInfo, (void**)&ret);
119 if(SUCCEEDED(hres))
120 return ret;
122 hres = IClassFactory_CreateInstance(ns->cf, NULL, &IID_IInternetProtocolInfo, (void**)&ret);
123 if(SUCCEEDED(hres))
124 return ret;
127 hres = get_protocol_cf(schema, schema_len, NULL, &cf);
128 if(FAILED(hres))
129 return NULL;
131 hres = IClassFactory_QueryInterface(cf, &IID_IInternetProtocolInfo, (void**)&ret);
132 if(FAILED(hres))
133 IClassFactory_CreateInstance(cf, NULL, &IID_IInternetProtocolInfo, (void**)&ret);
134 IClassFactory_Release(cf);
136 return ret;
139 HRESULT get_protocol_handler(LPCWSTR url, CLSID *clsid, IClassFactory **ret)
141 name_space *ns;
142 WCHAR schema[64];
143 DWORD schema_len;
144 HRESULT hres;
146 hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, schema, sizeof(schema)/sizeof(schema[0]),
147 &schema_len, 0);
148 if(FAILED(hres) || !schema_len)
149 return schema_len ? hres : E_FAIL;
151 ns = find_name_space(schema);
152 if(ns) {
153 *ret = ns->cf;
154 if(clsid)
155 *clsid = ns->clsid;
156 return S_OK;
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)) {
168 *ppv = iface;
169 IInternetSession_AddRef(iface);
170 return S_OK;
173 *ppv = NULL;
174 return E_NOINTERFACE;
177 static ULONG WINAPI InternetSession_AddRef(IInternetSession *iface)
179 TRACE("()\n");
180 URLMON_LockModule();
181 return 2;
184 static ULONG WINAPI InternetSession_Release(IInternetSession *iface)
186 TRACE("()\n");
187 URLMON_UnlockModule();
188 return 1;
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;
196 int size;
198 TRACE("(%p %s %s %d %p %d)\n", pCF, debugstr_guid(rclsid), debugstr_w(pwzProtocol),
199 cPatterns, ppwzPatterns, dwReserved);
201 if(cPatterns || ppwzPatterns)
202 FIXME("patterns not supported\n");
203 if(dwReserved)
204 WARN("dwReserved = %d\n", dwReserved);
206 if(!pCF || !pwzProtocol)
207 return E_INVALIDARG;
209 new_name_space = HeapAlloc(GetProcessHeap(), 0, sizeof(name_space));
211 size = (strlenW(pwzProtocol)+1)*sizeof(WCHAR);
212 new_name_space->protocol = HeapAlloc(GetProcessHeap(), 0, size);
213 memcpy(new_name_space->protocol, pwzProtocol, size);
215 IClassFactory_AddRef(pCF);
216 new_name_space->cf = pCF;
217 new_name_space->clsid = *rclsid;
219 new_name_space->next = name_space_list;
220 name_space_list = new_name_space;
221 return S_OK;
224 static HRESULT WINAPI InternetSession_UnregisterNameSpace(IInternetSession *iface,
225 IClassFactory *pCF, LPCWSTR pszProtocol)
227 name_space *iter, *last = NULL;
229 TRACE("(%p %s)\n", pCF, debugstr_w(pszProtocol));
231 if(!pCF || !pszProtocol)
232 return E_INVALIDARG;
234 for(iter = name_space_list; iter; iter = iter->next) {
235 if(iter->cf == pCF && !strcmpW(iter->protocol, pszProtocol))
236 break;
237 last = iter;
240 if(!iter)
241 return S_OK;
243 if(last)
244 last->next = iter->next;
245 else
246 name_space_list = iter->next;
248 IClassFactory_Release(iter->cf);
249 HeapFree(GetProcessHeap(), 0, iter->protocol);
250 HeapFree(GetProcessHeap(), 0, iter);
252 return S_OK;
255 static HRESULT WINAPI InternetSession_RegisterMimeFilter(IInternetSession *iface,
256 IClassFactory *pCF, REFCLSID rclsid, LPCWSTR pwzType)
258 FIXME("(%p %s %s)\n", pCF, debugstr_guid(rclsid), debugstr_w(pwzType));
259 return E_NOTIMPL;
262 static HRESULT WINAPI InternetSession_UnregisterMimeFilter(IInternetSession *iface,
263 IClassFactory *pCF, LPCWSTR pwzType)
265 FIXME("(%p %s)\n", pCF, debugstr_w(pwzType));
266 return E_NOTIMPL;
269 static HRESULT WINAPI InternetSession_CreateBinding(IInternetSession *iface,
270 LPBC pBC, LPCWSTR szUrl, IUnknown *pUnkOuter, IUnknown **ppUnk,
271 IInternetProtocol **ppOInetProt, DWORD dwOption)
273 TRACE("(%p %s %p %p %p %08x)\n", pBC, debugstr_w(szUrl), pUnkOuter, ppUnk,
274 ppOInetProt, dwOption);
276 if(pBC || pUnkOuter || ppUnk || dwOption)
277 FIXME("Unsupported arguments\n");
279 return create_binding_protocol(szUrl, ppOInetProt);
282 static HRESULT WINAPI InternetSession_SetSessionOption(IInternetSession *iface,
283 DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength, DWORD dwReserved)
285 FIXME("(%08x %p %d %d)\n", dwOption, pBuffer, dwBufferLength, dwReserved);
286 return E_NOTIMPL;
289 static const IInternetSessionVtbl InternetSessionVtbl = {
290 InternetSession_QueryInterface,
291 InternetSession_AddRef,
292 InternetSession_Release,
293 InternetSession_RegisterNameSpace,
294 InternetSession_UnregisterNameSpace,
295 InternetSession_RegisterMimeFilter,
296 InternetSession_UnregisterMimeFilter,
297 InternetSession_CreateBinding,
298 InternetSession_SetSessionOption
301 static IInternetSession InternetSession = { &InternetSessionVtbl };
303 /***********************************************************************
304 * CoInternetGetSession (URLMON.@)
306 * Create a new internet session and return an IInternetSession interface
307 * representing it.
309 * PARAMS
310 * dwSessionMode [I] Mode for the internet session
311 * ppIInternetSession [O] Destination for creates IInternetSession object
312 * dwReserved [I] Reserved, must be 0.
314 * RETURNS
315 * Success: S_OK. ppIInternetSession contains the IInternetSession interface.
316 * Failure: E_INVALIDARG, if any argument is invalid, or
317 * E_OUTOFMEMORY if memory allocation fails.
319 HRESULT WINAPI CoInternetGetSession(DWORD dwSessionMode, IInternetSession **ppIInternetSession,
320 DWORD dwReserved)
322 TRACE("(%d %p %d)\n", dwSessionMode, ppIInternetSession, dwReserved);
324 if(dwSessionMode)
325 ERR("dwSessionMode=%d\n", dwSessionMode);
326 if(dwReserved)
327 ERR("dwReserved=%d\n", dwReserved);
329 IInternetSession_AddRef(&InternetSession);
330 *ppIInternetSession = &InternetSession;
331 return S_OK;
334 /**************************************************************************
335 * UrlMkGetSessionOption (URLMON.@)
337 static BOOL get_url_encoding(HKEY root, DWORD *encoding)
339 DWORD size = sizeof(DWORD), res, type;
340 HKEY hkey;
342 static const WCHAR wszKeyName[] =
343 {'S','O','F','T','W','A','R','E',
344 '\\','M','i','c','r','o','s','o','f','t',
345 '\\','W','i','n','d','o','w','s',
346 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
347 '\\','I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0};
348 static const WCHAR wszUrlEncoding[] = {'U','r','l','E','n','c','o','d','i','n','g',0};
350 res = RegOpenKeyW(root, wszKeyName, &hkey);
351 if(res != ERROR_SUCCESS)
352 return FALSE;
354 res = RegQueryValueExW(hkey, wszUrlEncoding, NULL, &type, (LPBYTE)encoding, &size);
355 RegCloseKey(hkey);
357 return res == ERROR_SUCCESS;
360 HRESULT WINAPI UrlMkGetSessionOption(DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength,
361 DWORD* pdwBufferLength, DWORD dwReserved)
363 TRACE("(%x, %p, %d, %p)\n", dwOption, pBuffer, dwBufferLength, pdwBufferLength);
365 if(dwReserved)
366 WARN("dwReserved = %d\n", dwReserved);
368 switch(dwOption) {
369 case URLMON_OPTION_URL_ENCODING: {
370 DWORD encoding = 0;
372 if(!pBuffer || dwBufferLength < sizeof(DWORD) || !pdwBufferLength)
373 return E_INVALIDARG;
375 if(!get_url_encoding(HKEY_CURRENT_USER, &encoding))
376 get_url_encoding(HKEY_LOCAL_MACHINE, &encoding);
378 *pdwBufferLength = sizeof(DWORD);
379 *(DWORD*)pBuffer = encoding ? URL_ENCODING_DISABLE_UTF8 : URL_ENCODING_ENABLE_UTF8;
380 return S_OK;
382 default:
383 FIXME("unsupported option %x\n", dwOption);
386 return E_INVALIDARG;