Sync with trunk (r48042), except win32k/ntuser/cursoricon.c
[reactos.git] / dll / win32 / urlmon / session.c
1 /*
2 * Copyright 2005-2006 Jacek Caban for CodeWeavers
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include "urlmon_main.h"
20 #include "winreg.h"
21
22 #include "wine/debug.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
25
26 typedef struct name_space {
27 LPWSTR protocol;
28 IClassFactory *cf;
29 CLSID clsid;
30 BOOL urlmon;
31
32 struct name_space *next;
33 } name_space;
34
35 typedef struct mime_filter {
36 IClassFactory *cf;
37 CLSID clsid;
38 LPWSTR mime;
39
40 struct mime_filter *next;
41 } mime_filter;
42
43 static name_space *name_space_list = NULL;
44 static mime_filter *mime_filter_list = NULL;
45
46 static CRITICAL_SECTION session_cs;
47 static CRITICAL_SECTION_DEBUG session_cs_dbg =
48 {
49 0, 0, &session_cs,
50 { &session_cs_dbg.ProcessLocksList, &session_cs_dbg.ProcessLocksList },
51 0, 0, { (DWORD_PTR)(__FILE__ ": session") }
52 };
53 static CRITICAL_SECTION session_cs = { &session_cs_dbg, -1, 0, 0, 0, 0 };
54
55 static const WCHAR internet_settings_keyW[] =
56 {'S','O','F','T','W','A','R','E',
57 '\\','M','i','c','r','o','s','o','f','t',
58 '\\','W','i','n','d','o','w','s',
59 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
60 '\\','I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0};
61
62 static name_space *find_name_space(LPCWSTR protocol)
63 {
64 name_space *iter;
65
66 for(iter = name_space_list; iter; iter = iter->next) {
67 if(!strcmpW(iter->protocol, protocol))
68 return iter;
69 }
70
71 return NULL;
72 }
73
74 static HRESULT get_protocol_cf(LPCWSTR schema, DWORD schema_len, CLSID *pclsid, IClassFactory **ret)
75 {
76 WCHAR str_clsid[64];
77 HKEY hkey = NULL;
78 DWORD res, type, size;
79 CLSID clsid;
80 LPWSTR wszKey;
81 HRESULT hres;
82
83 static const WCHAR wszProtocolsKey[] =
84 {'P','R','O','T','O','C','O','L','S','\\','H','a','n','d','l','e','r','\\'};
85 static const WCHAR wszCLSID[] = {'C','L','S','I','D',0};
86
87 wszKey = heap_alloc(sizeof(wszProtocolsKey)+(schema_len+1)*sizeof(WCHAR));
88 memcpy(wszKey, wszProtocolsKey, sizeof(wszProtocolsKey));
89 memcpy(wszKey + sizeof(wszProtocolsKey)/sizeof(WCHAR), schema, (schema_len+1)*sizeof(WCHAR));
90
91 res = RegOpenKeyW(HKEY_CLASSES_ROOT, wszKey, &hkey);
92 heap_free(wszKey);
93 if(res != ERROR_SUCCESS) {
94 TRACE("Could not open protocol handler key\n");
95 return E_FAIL;
96 }
97
98 size = sizeof(str_clsid);
99 res = RegQueryValueExW(hkey, wszCLSID, NULL, &type, (LPBYTE)str_clsid, &size);
100 RegCloseKey(hkey);
101 if(res != ERROR_SUCCESS || type != REG_SZ) {
102 WARN("Could not get protocol CLSID res=%d\n", res);
103 return E_FAIL;
104 }
105
106 hres = CLSIDFromString(str_clsid, &clsid);
107 if(FAILED(hres)) {
108 WARN("CLSIDFromString failed: %08x\n", hres);
109 return hres;
110 }
111
112 if(pclsid)
113 *pclsid = clsid;
114
115 if(!ret)
116 return S_OK;
117
118 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IClassFactory, (void**)ret);
119 }
120
121 static HRESULT register_namespace(IClassFactory *cf, REFIID clsid, LPCWSTR protocol, BOOL urlmon_protocol)
122 {
123 name_space *new_name_space;
124
125 new_name_space = heap_alloc(sizeof(name_space));
126
127 if(!urlmon_protocol)
128 IClassFactory_AddRef(cf);
129 new_name_space->cf = cf;
130 new_name_space->clsid = *clsid;
131 new_name_space->urlmon = urlmon_protocol;
132 new_name_space->protocol = heap_strdupW(protocol);
133
134 EnterCriticalSection(&session_cs);
135
136 new_name_space->next = name_space_list;
137 name_space_list = new_name_space;
138
139 LeaveCriticalSection(&session_cs);
140
141 return S_OK;
142 }
143
144 static HRESULT unregister_namespace(IClassFactory *cf, LPCWSTR protocol)
145 {
146 name_space *iter, *last = NULL;
147
148 EnterCriticalSection(&session_cs);
149
150 for(iter = name_space_list; iter; iter = iter->next) {
151 if(iter->cf == cf && !strcmpW(iter->protocol, protocol))
152 break;
153 last = iter;
154 }
155
156 if(iter) {
157 if(last)
158 last->next = iter->next;
159 else
160 name_space_list = iter->next;
161 }
162
163 LeaveCriticalSection(&session_cs);
164
165 if(iter) {
166 if(!iter->urlmon)
167 IClassFactory_Release(iter->cf);
168 heap_free(iter->protocol);
169 heap_free(iter);
170 }
171
172 return S_OK;
173 }
174
175
176 void register_urlmon_namespace(IClassFactory *cf, REFIID clsid, LPCWSTR protocol, BOOL do_register)
177 {
178 if(do_register)
179 register_namespace(cf, clsid, protocol, TRUE);
180 else
181 unregister_namespace(cf, protocol);
182 }
183
184 BOOL is_registered_protocol(LPCWSTR url)
185 {
186 DWORD schema_len;
187 WCHAR schema[64];
188 HRESULT hres;
189
190 hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, schema, sizeof(schema)/sizeof(schema[0]),
191 &schema_len, 0);
192 if(FAILED(hres))
193 return FALSE;
194
195 return get_protocol_cf(schema, schema_len, NULL, NULL) == S_OK;
196 }
197
198 IInternetProtocolInfo *get_protocol_info(LPCWSTR url)
199 {
200 IInternetProtocolInfo *ret = NULL;
201 IClassFactory *cf;
202 name_space *ns;
203 WCHAR schema[64];
204 DWORD schema_len;
205 HRESULT hres;
206
207 hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, schema, sizeof(schema)/sizeof(schema[0]),
208 &schema_len, 0);
209 if(FAILED(hres) || !schema_len)
210 return NULL;
211
212 EnterCriticalSection(&session_cs);
213
214 ns = find_name_space(schema);
215 if(ns && !ns->urlmon) {
216 hres = IClassFactory_QueryInterface(ns->cf, &IID_IInternetProtocolInfo, (void**)&ret);
217 if(FAILED(hres))
218 hres = IClassFactory_CreateInstance(ns->cf, NULL, &IID_IInternetProtocolInfo, (void**)&ret);
219 }
220
221 LeaveCriticalSection(&session_cs);
222
223 if(ns && SUCCEEDED(hres))
224 return ret;
225
226 hres = get_protocol_cf(schema, schema_len, NULL, &cf);
227 if(FAILED(hres))
228 return NULL;
229
230 hres = IClassFactory_QueryInterface(cf, &IID_IInternetProtocolInfo, (void**)&ret);
231 if(FAILED(hres))
232 IClassFactory_CreateInstance(cf, NULL, &IID_IInternetProtocolInfo, (void**)&ret);
233 IClassFactory_Release(cf);
234
235 return ret;
236 }
237
238 HRESULT get_protocol_handler(LPCWSTR url, CLSID *clsid, BOOL *urlmon_protocol, IClassFactory **ret)
239 {
240 name_space *ns;
241 WCHAR schema[64];
242 DWORD schema_len;
243 HRESULT hres;
244
245 *ret = NULL;
246
247 hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, schema, sizeof(schema)/sizeof(schema[0]),
248 &schema_len, 0);
249 if(FAILED(hres) || !schema_len)
250 return schema_len ? hres : E_FAIL;
251
252 EnterCriticalSection(&session_cs);
253
254 ns = find_name_space(schema);
255 if(ns) {
256 *ret = ns->cf;
257 IClassFactory_AddRef(*ret);
258 if(clsid)
259 *clsid = ns->clsid;
260 if(urlmon_protocol)
261 *urlmon_protocol = ns->urlmon;
262 }
263
264 LeaveCriticalSection(&session_cs);
265
266 if(*ret)
267 return S_OK;
268
269 if(urlmon_protocol)
270 *urlmon_protocol = FALSE;
271 return get_protocol_cf(schema, schema_len, clsid, ret);
272 }
273
274 IInternetProtocol *get_mime_filter(LPCWSTR mime)
275 {
276 IClassFactory *cf = NULL;
277 IInternetProtocol *ret;
278 mime_filter *iter;
279 HRESULT hres;
280
281 EnterCriticalSection(&session_cs);
282
283 for(iter = mime_filter_list; iter; iter = iter->next) {
284 if(!strcmpW(iter->mime, mime)) {
285 cf = iter->cf;
286 break;
287 }
288 }
289
290 LeaveCriticalSection(&session_cs);
291
292 if(!cf)
293 return NULL;
294
295 hres = IClassFactory_CreateInstance(cf, NULL, &IID_IInternetProtocol, (void**)&ret);
296 if(FAILED(hres)) {
297 WARN("CreateInstance failed: %08x\n", hres);
298 return NULL;
299 }
300
301 return ret;
302 }
303
304 static HRESULT WINAPI InternetSession_QueryInterface(IInternetSession *iface,
305 REFIID riid, void **ppv)
306 {
307 TRACE("(%s %p)\n", debugstr_guid(riid), ppv);
308
309 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetSession, riid)) {
310 *ppv = iface;
311 IInternetSession_AddRef(iface);
312 return S_OK;
313 }
314
315 *ppv = NULL;
316 return E_NOINTERFACE;
317 }
318
319 static ULONG WINAPI InternetSession_AddRef(IInternetSession *iface)
320 {
321 TRACE("()\n");
322 URLMON_LockModule();
323 return 2;
324 }
325
326 static ULONG WINAPI InternetSession_Release(IInternetSession *iface)
327 {
328 TRACE("()\n");
329 URLMON_UnlockModule();
330 return 1;
331 }
332
333 static HRESULT WINAPI InternetSession_RegisterNameSpace(IInternetSession *iface,
334 IClassFactory *pCF, REFCLSID rclsid, LPCWSTR pwzProtocol, ULONG cPatterns,
335 const LPCWSTR *ppwzPatterns, DWORD dwReserved)
336 {
337 TRACE("(%p %s %s %d %p %d)\n", pCF, debugstr_guid(rclsid), debugstr_w(pwzProtocol),
338 cPatterns, ppwzPatterns, dwReserved);
339
340 if(cPatterns || ppwzPatterns)
341 FIXME("patterns not supported\n");
342 if(dwReserved)
343 WARN("dwReserved = %d\n", dwReserved);
344
345 if(!pCF || !pwzProtocol)
346 return E_INVALIDARG;
347
348 return register_namespace(pCF, rclsid, pwzProtocol, FALSE);
349 }
350
351 static HRESULT WINAPI InternetSession_UnregisterNameSpace(IInternetSession *iface,
352 IClassFactory *pCF, LPCWSTR pszProtocol)
353 {
354 TRACE("(%p %s)\n", pCF, debugstr_w(pszProtocol));
355
356 if(!pCF || !pszProtocol)
357 return E_INVALIDARG;
358
359 return unregister_namespace(pCF, pszProtocol);
360 }
361
362 static HRESULT WINAPI InternetSession_RegisterMimeFilter(IInternetSession *iface,
363 IClassFactory *pCF, REFCLSID rclsid, LPCWSTR pwzType)
364 {
365 mime_filter *filter;
366
367 TRACE("(%p %s %s)\n", pCF, debugstr_guid(rclsid), debugstr_w(pwzType));
368
369 filter = heap_alloc(sizeof(mime_filter));
370
371 IClassFactory_AddRef(pCF);
372 filter->cf = pCF;
373 filter->clsid = *rclsid;
374 filter->mime = heap_strdupW(pwzType);
375
376 EnterCriticalSection(&session_cs);
377
378 filter->next = mime_filter_list;
379 mime_filter_list = filter;
380
381 LeaveCriticalSection(&session_cs);
382
383 return S_OK;
384 }
385
386 static HRESULT WINAPI InternetSession_UnregisterMimeFilter(IInternetSession *iface,
387 IClassFactory *pCF, LPCWSTR pwzType)
388 {
389 mime_filter *iter, *prev = NULL;
390
391 TRACE("(%p %s)\n", pCF, debugstr_w(pwzType));
392
393 EnterCriticalSection(&session_cs);
394
395 for(iter = mime_filter_list; iter; iter = iter->next) {
396 if(iter->cf == pCF && !strcmpW(iter->mime, pwzType))
397 break;
398 prev = iter;
399 }
400
401 if(iter) {
402 if(prev)
403 prev->next = iter->next;
404 else
405 mime_filter_list = iter->next;
406 }
407
408 LeaveCriticalSection(&session_cs);
409
410 if(iter) {
411 IClassFactory_Release(iter->cf);
412 heap_free(iter->mime);
413 heap_free(iter);
414 }
415
416 return S_OK;
417 }
418
419 static HRESULT WINAPI InternetSession_CreateBinding(IInternetSession *iface,
420 LPBC pBC, LPCWSTR szUrl, IUnknown *pUnkOuter, IUnknown **ppUnk,
421 IInternetProtocol **ppOInetProt, DWORD dwOption)
422 {
423 TRACE("(%p %s %p %p %p %08x)\n", pBC, debugstr_w(szUrl), pUnkOuter, ppUnk,
424 ppOInetProt, dwOption);
425
426 if(pBC || pUnkOuter || ppUnk || dwOption)
427 FIXME("Unsupported arguments\n");
428
429 return create_binding_protocol(szUrl, FALSE, ppOInetProt);
430 }
431
432 static HRESULT WINAPI InternetSession_SetSessionOption(IInternetSession *iface,
433 DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength, DWORD dwReserved)
434 {
435 FIXME("(%08x %p %d %d)\n", dwOption, pBuffer, dwBufferLength, dwReserved);
436 return E_NOTIMPL;
437 }
438
439 static const IInternetSessionVtbl InternetSessionVtbl = {
440 InternetSession_QueryInterface,
441 InternetSession_AddRef,
442 InternetSession_Release,
443 InternetSession_RegisterNameSpace,
444 InternetSession_UnregisterNameSpace,
445 InternetSession_RegisterMimeFilter,
446 InternetSession_UnregisterMimeFilter,
447 InternetSession_CreateBinding,
448 InternetSession_SetSessionOption
449 };
450
451 static IInternetSession InternetSession = { &InternetSessionVtbl };
452
453 /***********************************************************************
454 * CoInternetGetSession (URLMON.@)
455 *
456 * Create a new internet session and return an IInternetSession interface
457 * representing it.
458 *
459 * PARAMS
460 * dwSessionMode [I] Mode for the internet session
461 * ppIInternetSession [O] Destination for creates IInternetSession object
462 * dwReserved [I] Reserved, must be 0.
463 *
464 * RETURNS
465 * Success: S_OK. ppIInternetSession contains the IInternetSession interface.
466 * Failure: E_INVALIDARG, if any argument is invalid, or
467 * E_OUTOFMEMORY if memory allocation fails.
468 */
469 HRESULT WINAPI CoInternetGetSession(DWORD dwSessionMode, IInternetSession **ppIInternetSession,
470 DWORD dwReserved)
471 {
472 TRACE("(%d %p %d)\n", dwSessionMode, ppIInternetSession, dwReserved);
473
474 if(dwSessionMode)
475 ERR("dwSessionMode=%d\n", dwSessionMode);
476 if(dwReserved)
477 ERR("dwReserved=%d\n", dwReserved);
478
479 IInternetSession_AddRef(&InternetSession);
480 *ppIInternetSession = &InternetSession;
481 return S_OK;
482 }
483
484 /**************************************************************************
485 * UrlMkGetSessionOption (URLMON.@)
486 */
487 static BOOL get_url_encoding(HKEY root, DWORD *encoding)
488 {
489 DWORD size = sizeof(DWORD), res, type;
490 HKEY hkey;
491
492 static const WCHAR wszUrlEncoding[] = {'U','r','l','E','n','c','o','d','i','n','g',0};
493
494 res = RegOpenKeyW(root, internet_settings_keyW, &hkey);
495 if(res != ERROR_SUCCESS)
496 return FALSE;
497
498 res = RegQueryValueExW(hkey, wszUrlEncoding, NULL, &type, (LPBYTE)encoding, &size);
499 RegCloseKey(hkey);
500
501 return res == ERROR_SUCCESS;
502 }
503
504 static LPWSTR user_agent;
505
506 static void ensure_useragent(void)
507 {
508 DWORD size = sizeof(DWORD), res, type;
509 HKEY hkey;
510
511 static const WCHAR user_agentW[] = {'U','s','e','r',' ','A','g','e','n','t',0};
512
513 if(user_agent)
514 return;
515
516 res = RegOpenKeyW(HKEY_CURRENT_USER, internet_settings_keyW, &hkey);
517 if(res != ERROR_SUCCESS)
518 return;
519
520 res = RegQueryValueExW(hkey, user_agentW, NULL, &type, NULL, &size);
521 if(res == ERROR_SUCCESS && type == REG_SZ) {
522 user_agent = heap_alloc(size);
523 res = RegQueryValueExW(hkey, user_agentW, NULL, &type, (LPBYTE)user_agent, &size);
524 if(res != ERROR_SUCCESS) {
525 heap_free(user_agent);
526 user_agent = NULL;
527 }
528 }else {
529 WARN("Could not find User Agent value: %u\n", res);
530 }
531
532 RegCloseKey(hkey);
533 }
534
535 LPWSTR get_useragent(void)
536 {
537 LPWSTR ret;
538
539 ensure_useragent();
540
541 EnterCriticalSection(&session_cs);
542 ret = heap_strdupW(user_agent);
543 LeaveCriticalSection(&session_cs);
544
545 return ret;
546 }
547
548 HRESULT WINAPI UrlMkGetSessionOption(DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength,
549 DWORD* pdwBufferLength, DWORD dwReserved)
550 {
551 TRACE("(%x, %p, %d, %p)\n", dwOption, pBuffer, dwBufferLength, pdwBufferLength);
552
553 if(dwReserved)
554 WARN("dwReserved = %d\n", dwReserved);
555
556 switch(dwOption) {
557 case URLMON_OPTION_USERAGENT: {
558 HRESULT hres = E_OUTOFMEMORY;
559 DWORD size;
560
561 if(!pdwBufferLength)
562 return E_INVALIDARG;
563
564 EnterCriticalSection(&session_cs);
565
566 ensure_useragent();
567 if(user_agent) {
568 size = WideCharToMultiByte(CP_ACP, 0, user_agent, -1, NULL, 0, NULL, NULL);
569 *pdwBufferLength = size;
570 if(size <= dwBufferLength) {
571 if(pBuffer)
572 WideCharToMultiByte(CP_ACP, 0, user_agent, -1, pBuffer, size, NULL, NULL);
573 else
574 hres = E_INVALIDARG;
575 }
576 }
577
578 LeaveCriticalSection(&session_cs);
579
580 /* Tests prove that we have to return E_OUTOFMEMORY on success. */
581 return hres;
582 }
583 case URLMON_OPTION_URL_ENCODING: {
584 DWORD encoding = 0;
585
586 if(!pBuffer || dwBufferLength < sizeof(DWORD) || !pdwBufferLength)
587 return E_INVALIDARG;
588
589 if(!get_url_encoding(HKEY_CURRENT_USER, &encoding))
590 get_url_encoding(HKEY_LOCAL_MACHINE, &encoding);
591
592 *pdwBufferLength = sizeof(DWORD);
593 *(DWORD*)pBuffer = encoding ? URL_ENCODING_DISABLE_UTF8 : URL_ENCODING_ENABLE_UTF8;
594 return S_OK;
595 }
596 default:
597 FIXME("unsupported option %x\n", dwOption);
598 }
599
600 return E_INVALIDARG;
601 }
602
603 /**************************************************************************
604 * UrlMkSetSessionOption (URLMON.@)
605 */
606 HRESULT WINAPI UrlMkSetSessionOption(DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength,
607 DWORD Reserved)
608 {
609 TRACE("(%x %p %x)\n", dwOption, pBuffer, dwBufferLength);
610
611 switch(dwOption) {
612 case URLMON_OPTION_USERAGENT: {
613 LPWSTR new_user_agent;
614 char *buf = pBuffer;
615 DWORD len, size;
616
617 if(!pBuffer || !dwBufferLength)
618 return E_INVALIDARG;
619
620 for(len=0; len<dwBufferLength && buf[len]; len++);
621
622 TRACE("Setting user agent %s\n", debugstr_an(buf, len));
623
624 size = MultiByteToWideChar(CP_ACP, 0, buf, len, NULL, 0);
625 new_user_agent = heap_alloc((size+1)*sizeof(WCHAR));
626 if(!new_user_agent)
627 return E_OUTOFMEMORY;
628 MultiByteToWideChar(CP_ACP, 0, buf, len, new_user_agent, size);
629 new_user_agent[size] = 0;
630
631 EnterCriticalSection(&session_cs);
632
633 heap_free(user_agent);
634 user_agent = new_user_agent;
635
636 LeaveCriticalSection(&session_cs);
637 break;
638 }
639 default:
640 FIXME("Unknown option %x\n", dwOption);
641 return E_INVALIDARG;
642 }
643
644 return S_OK;
645 }
646
647 /**************************************************************************
648 * ObtainUserAgentString (URLMON.@)
649 */
650 HRESULT WINAPI ObtainUserAgentString(DWORD dwOption, LPSTR pcszUAOut, DWORD *cbSize)
651 {
652 DWORD size;
653 HRESULT hres = E_FAIL;
654
655 TRACE("(%d %p %p)\n", dwOption, pcszUAOut, cbSize);
656
657 if(!pcszUAOut || !cbSize)
658 return E_INVALIDARG;
659
660 EnterCriticalSection(&session_cs);
661
662 ensure_useragent();
663 if(user_agent) {
664 size = WideCharToMultiByte(CP_ACP, 0, user_agent, -1, NULL, 0, NULL, NULL);
665
666 if(size <= *cbSize) {
667 WideCharToMultiByte(CP_ACP, 0, user_agent, -1, pcszUAOut, *cbSize, NULL, NULL);
668 hres = S_OK;
669 }else {
670 hres = E_OUTOFMEMORY;
671 }
672
673 *cbSize = size;
674 }
675
676 LeaveCriticalSection(&session_cs);
677 return hres;
678 }
679
680 void free_session(void)
681 {
682 heap_free(user_agent);
683 }