wineconsole: Free registry key name (valgrind).
[wine.git] / dlls / urlmon / session.c
blobbb41c3106570f6e7fb8cdd98d431fc342b41b902
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 "urlmon_main.h"
20 #include "winreg.h"
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
26 typedef struct {
27 LPWSTR protocol;
28 IClassFactory *cf;
29 CLSID clsid;
30 BOOL urlmon;
32 struct list entry;
33 } name_space;
35 typedef struct {
36 IClassFactory *cf;
37 CLSID clsid;
38 LPWSTR mime;
40 struct list entry;
41 } mime_filter;
43 static struct list name_space_list = LIST_INIT(name_space_list);
44 static struct list mime_filter_list = LIST_INIT(mime_filter_list);
46 static CRITICAL_SECTION session_cs;
47 static CRITICAL_SECTION_DEBUG session_cs_dbg =
49 0, 0, &session_cs,
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 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};
62 static name_space *find_name_space(LPCWSTR protocol)
64 name_space *iter;
66 LIST_FOR_EACH_ENTRY(iter, &name_space_list, name_space, entry) {
67 if(!strcmpiW(iter->protocol, protocol))
68 return iter;
71 return NULL;
74 static HRESULT get_protocol_cf(LPCWSTR schema, DWORD schema_len, CLSID *pclsid, IClassFactory **ret)
76 WCHAR str_clsid[64];
77 HKEY hkey = NULL;
78 DWORD res, type, size;
79 CLSID clsid;
80 LPWSTR wszKey;
81 HRESULT hres;
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};
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));
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 MK_E_SYNTAX;
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 MK_E_SYNTAX;
106 hres = CLSIDFromString(str_clsid, &clsid);
107 if(FAILED(hres)) {
108 WARN("CLSIDFromString failed: %08x\n", hres);
109 return hres;
112 if(pclsid)
113 *pclsid = clsid;
115 if(!ret)
116 return S_OK;
118 hres = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IClassFactory, (void**)ret);
119 return SUCCEEDED(hres) ? S_OK : MK_E_SYNTAX;
122 HRESULT register_namespace(IClassFactory *cf, REFIID clsid, LPCWSTR protocol, BOOL urlmon_protocol)
124 name_space *new_name_space;
126 new_name_space = heap_alloc(sizeof(name_space));
128 if(!urlmon_protocol)
129 IClassFactory_AddRef(cf);
130 new_name_space->cf = cf;
131 new_name_space->clsid = *clsid;
132 new_name_space->urlmon = urlmon_protocol;
133 new_name_space->protocol = heap_strdupW(protocol);
135 EnterCriticalSection(&session_cs);
137 list_add_head(&name_space_list, &new_name_space->entry);
139 LeaveCriticalSection(&session_cs);
141 return S_OK;
144 static HRESULT unregister_namespace(IClassFactory *cf, LPCWSTR protocol)
146 name_space *iter;
148 EnterCriticalSection(&session_cs);
150 LIST_FOR_EACH_ENTRY(iter, &name_space_list, name_space, entry) {
151 if(iter->cf == cf && !strcmpiW(iter->protocol, protocol)) {
152 list_remove(&iter->entry);
154 LeaveCriticalSection(&session_cs);
156 if(!iter->urlmon)
157 IClassFactory_Release(iter->cf);
158 heap_free(iter->protocol);
159 heap_free(iter);
160 return S_OK;
164 LeaveCriticalSection(&session_cs);
165 return S_OK;
168 BOOL is_registered_protocol(LPCWSTR url)
170 DWORD schema_len;
171 WCHAR schema[64];
172 HRESULT hres;
174 hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, schema, sizeof(schema)/sizeof(schema[0]),
175 &schema_len, 0);
176 if(FAILED(hres))
177 return FALSE;
179 return get_protocol_cf(schema, schema_len, NULL, NULL) == S_OK;
182 IInternetProtocolInfo *get_protocol_info(LPCWSTR url)
184 IInternetProtocolInfo *ret = NULL;
185 IClassFactory *cf;
186 name_space *ns;
187 WCHAR schema[64];
188 DWORD schema_len;
189 HRESULT hres;
191 hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, schema, sizeof(schema)/sizeof(schema[0]),
192 &schema_len, 0);
193 if(FAILED(hres) || !schema_len)
194 return NULL;
196 EnterCriticalSection(&session_cs);
198 ns = find_name_space(schema);
199 if(ns && !ns->urlmon) {
200 hres = IClassFactory_QueryInterface(ns->cf, &IID_IInternetProtocolInfo, (void**)&ret);
201 if(FAILED(hres))
202 hres = IClassFactory_CreateInstance(ns->cf, NULL, &IID_IInternetProtocolInfo, (void**)&ret);
205 LeaveCriticalSection(&session_cs);
207 if(ns && SUCCEEDED(hres))
208 return ret;
210 hres = get_protocol_cf(schema, schema_len, NULL, &cf);
211 if(FAILED(hres))
212 return NULL;
214 hres = IClassFactory_QueryInterface(cf, &IID_IInternetProtocolInfo, (void**)&ret);
215 if(FAILED(hres))
216 IClassFactory_CreateInstance(cf, NULL, &IID_IInternetProtocolInfo, (void**)&ret);
217 IClassFactory_Release(cf);
219 return ret;
222 HRESULT get_protocol_handler(IUri *uri, CLSID *clsid, BOOL *urlmon_protocol, IClassFactory **ret)
224 name_space *ns;
225 BSTR scheme;
226 HRESULT hres;
228 *ret = NULL;
230 /* FIXME: Avoid GetSchemeName call for known schemes */
231 hres = IUri_GetSchemeName(uri, &scheme);
232 if(FAILED(hres))
233 return hres;
235 EnterCriticalSection(&session_cs);
237 ns = find_name_space(scheme);
238 if(ns) {
239 *ret = ns->cf;
240 IClassFactory_AddRef(*ret);
241 if(clsid)
242 *clsid = ns->clsid;
243 if(urlmon_protocol)
244 *urlmon_protocol = ns->urlmon;
247 LeaveCriticalSection(&session_cs);
249 if(*ret) {
250 hres = S_OK;
251 }else {
252 if(urlmon_protocol)
253 *urlmon_protocol = FALSE;
254 hres = get_protocol_cf(scheme, SysStringLen(scheme), clsid, ret);
257 SysFreeString(scheme);
258 return hres;
261 IInternetProtocol *get_mime_filter(LPCWSTR mime)
263 static const WCHAR filtersW[] = {'P','r','o','t','o','c','o','l','s',
264 '\\','F','i','l','t','e','r',0 };
265 static const WCHAR CLSIDW[] = {'C','L','S','I','D',0};
267 IClassFactory *cf = NULL;
268 IInternetProtocol *ret;
269 mime_filter *iter;
270 HKEY hlist, hfilter;
271 WCHAR clsidw[64];
272 CLSID clsid;
273 DWORD res, type, size;
274 HRESULT hres;
276 EnterCriticalSection(&session_cs);
278 LIST_FOR_EACH_ENTRY(iter, &mime_filter_list, mime_filter, entry) {
279 if(!strcmpW(iter->mime, mime)) {
280 cf = iter->cf;
281 break;
285 LeaveCriticalSection(&session_cs);
287 if(cf) {
288 hres = IClassFactory_CreateInstance(cf, NULL, &IID_IInternetProtocol, (void**)&ret);
289 if(FAILED(hres)) {
290 WARN("CreateInstance failed: %08x\n", hres);
291 return NULL;
294 return ret;
297 res = RegOpenKeyW(HKEY_CLASSES_ROOT, filtersW, &hlist);
298 if(res != ERROR_SUCCESS) {
299 TRACE("Could not open MIME filters key\n");
300 return NULL;
303 res = RegOpenKeyW(hlist, mime, &hfilter);
304 CloseHandle(hlist);
305 if(res != ERROR_SUCCESS)
306 return NULL;
308 size = sizeof(clsidw);
309 res = RegQueryValueExW(hfilter, CLSIDW, NULL, &type, (LPBYTE)clsidw, &size);
310 CloseHandle(hfilter);
311 if(res!=ERROR_SUCCESS || type!=REG_SZ) {
312 WARN("Could not get filter CLSID for %s\n", debugstr_w(mime));
313 return NULL;
316 hres = CLSIDFromString(clsidw, &clsid);
317 if(FAILED(hres)) {
318 WARN("CLSIDFromString failed for %s (%x)\n", debugstr_w(mime), hres);
319 return NULL;
322 hres = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER, &IID_IInternetProtocol, (void**)&ret);
323 if(FAILED(hres)) {
324 WARN("CoCreateInstance failed: %08x\n", hres);
325 return NULL;
328 return ret;
331 static HRESULT WINAPI InternetSession_QueryInterface(IInternetSession *iface,
332 REFIID riid, void **ppv)
334 TRACE("(%s %p)\n", debugstr_guid(riid), ppv);
336 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetSession, riid)) {
337 *ppv = iface;
338 IInternetSession_AddRef(iface);
339 return S_OK;
342 *ppv = NULL;
343 return E_NOINTERFACE;
346 static ULONG WINAPI InternetSession_AddRef(IInternetSession *iface)
348 TRACE("()\n");
349 URLMON_LockModule();
350 return 2;
353 static ULONG WINAPI InternetSession_Release(IInternetSession *iface)
355 TRACE("()\n");
356 URLMON_UnlockModule();
357 return 1;
360 static HRESULT WINAPI InternetSession_RegisterNameSpace(IInternetSession *iface,
361 IClassFactory *pCF, REFCLSID rclsid, LPCWSTR pwzProtocol, ULONG cPatterns,
362 const LPCWSTR *ppwzPatterns, DWORD dwReserved)
364 TRACE("(%p %s %s %d %p %d)\n", pCF, debugstr_guid(rclsid), debugstr_w(pwzProtocol),
365 cPatterns, ppwzPatterns, dwReserved);
367 if(cPatterns || ppwzPatterns)
368 FIXME("patterns not supported\n");
369 if(dwReserved)
370 WARN("dwReserved = %d\n", dwReserved);
372 if(!pCF || !pwzProtocol)
373 return E_INVALIDARG;
375 return register_namespace(pCF, rclsid, pwzProtocol, FALSE);
378 static HRESULT WINAPI InternetSession_UnregisterNameSpace(IInternetSession *iface,
379 IClassFactory *pCF, LPCWSTR pszProtocol)
381 TRACE("(%p %s)\n", pCF, debugstr_w(pszProtocol));
383 if(!pCF || !pszProtocol)
384 return E_INVALIDARG;
386 return unregister_namespace(pCF, pszProtocol);
389 static HRESULT WINAPI InternetSession_RegisterMimeFilter(IInternetSession *iface,
390 IClassFactory *pCF, REFCLSID rclsid, LPCWSTR pwzType)
392 mime_filter *filter;
394 TRACE("(%p %s %s)\n", pCF, debugstr_guid(rclsid), debugstr_w(pwzType));
396 filter = heap_alloc(sizeof(mime_filter));
398 IClassFactory_AddRef(pCF);
399 filter->cf = pCF;
400 filter->clsid = *rclsid;
401 filter->mime = heap_strdupW(pwzType);
403 EnterCriticalSection(&session_cs);
405 list_add_head(&mime_filter_list, &filter->entry);
407 LeaveCriticalSection(&session_cs);
409 return S_OK;
412 static HRESULT WINAPI InternetSession_UnregisterMimeFilter(IInternetSession *iface,
413 IClassFactory *pCF, LPCWSTR pwzType)
415 mime_filter *iter;
417 TRACE("(%p %s)\n", pCF, debugstr_w(pwzType));
419 EnterCriticalSection(&session_cs);
421 LIST_FOR_EACH_ENTRY(iter, &mime_filter_list, mime_filter, entry) {
422 if(iter->cf == pCF && !strcmpW(iter->mime, pwzType)) {
423 list_remove(&iter->entry);
425 LeaveCriticalSection(&session_cs);
427 IClassFactory_Release(iter->cf);
428 heap_free(iter->mime);
429 heap_free(iter);
430 return S_OK;
434 LeaveCriticalSection(&session_cs);
435 return S_OK;
438 static HRESULT WINAPI InternetSession_CreateBinding(IInternetSession *iface,
439 LPBC pBC, LPCWSTR szUrl, IUnknown *pUnkOuter, IUnknown **ppUnk,
440 IInternetProtocol **ppOInetProt, DWORD dwOption)
442 BindProtocol *protocol;
443 HRESULT hres;
445 TRACE("(%p %s %p %p %p %08x)\n", pBC, debugstr_w(szUrl), pUnkOuter, ppUnk,
446 ppOInetProt, dwOption);
448 if(pBC || pUnkOuter || ppUnk || dwOption)
449 FIXME("Unsupported arguments\n");
451 hres = create_binding_protocol(FALSE, &protocol);
452 if(FAILED(hres))
453 return hres;
455 *ppOInetProt = (IInternetProtocol*)&protocol->IInternetProtocolEx_iface;
456 return S_OK;
459 static HRESULT WINAPI InternetSession_SetSessionOption(IInternetSession *iface,
460 DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength, DWORD dwReserved)
462 FIXME("(%08x %p %d %d)\n", dwOption, pBuffer, dwBufferLength, dwReserved);
463 return E_NOTIMPL;
466 static const IInternetSessionVtbl InternetSessionVtbl = {
467 InternetSession_QueryInterface,
468 InternetSession_AddRef,
469 InternetSession_Release,
470 InternetSession_RegisterNameSpace,
471 InternetSession_UnregisterNameSpace,
472 InternetSession_RegisterMimeFilter,
473 InternetSession_UnregisterMimeFilter,
474 InternetSession_CreateBinding,
475 InternetSession_SetSessionOption
478 static IInternetSession InternetSession = { &InternetSessionVtbl };
480 /***********************************************************************
481 * CoInternetGetSession (URLMON.@)
483 * Create a new internet session and return an IInternetSession interface
484 * representing it.
486 * PARAMS
487 * dwSessionMode [I] Mode for the internet session
488 * ppIInternetSession [O] Destination for creates IInternetSession object
489 * dwReserved [I] Reserved, must be 0.
491 * RETURNS
492 * Success: S_OK. ppIInternetSession contains the IInternetSession interface.
493 * Failure: E_INVALIDARG, if any argument is invalid, or
494 * E_OUTOFMEMORY if memory allocation fails.
496 HRESULT WINAPI CoInternetGetSession(DWORD dwSessionMode, IInternetSession **ppIInternetSession,
497 DWORD dwReserved)
499 TRACE("(%d %p %d)\n", dwSessionMode, ppIInternetSession, dwReserved);
501 if(dwSessionMode)
502 ERR("dwSessionMode=%d\n", dwSessionMode);
503 if(dwReserved)
504 ERR("dwReserved=%d\n", dwReserved);
506 IInternetSession_AddRef(&InternetSession);
507 *ppIInternetSession = &InternetSession;
508 return S_OK;
511 /**************************************************************************
512 * UrlMkGetSessionOption (URLMON.@)
514 static BOOL get_url_encoding(HKEY root, DWORD *encoding)
516 DWORD size = sizeof(DWORD), res, type;
517 HKEY hkey;
519 static const WCHAR wszUrlEncoding[] = {'U','r','l','E','n','c','o','d','i','n','g',0};
521 res = RegOpenKeyW(root, internet_settings_keyW, &hkey);
522 if(res != ERROR_SUCCESS)
523 return FALSE;
525 res = RegQueryValueExW(hkey, wszUrlEncoding, NULL, &type, (LPBYTE)encoding, &size);
526 RegCloseKey(hkey);
528 return res == ERROR_SUCCESS;
531 static LPWSTR user_agent;
533 static void ensure_useragent(void)
535 OSVERSIONINFOW info = {sizeof(info)};
536 const WCHAR *os_type, *is_nt;
537 WCHAR buf[512], *ret, *tmp;
538 DWORD res, idx=0;
539 size_t len, size;
540 BOOL is_wow;
541 HKEY key;
543 static const WCHAR formatW[] =
544 {'M','o','z','i','l','l','a','/','4','.','0',
545 ' ','(','c','o','m','p','a','t','i','b','l','e',';',
546 ' ','M','S','I','E',' ','8','.','0',';',
547 ' ','W','i','n','d','o','w','s',' ','%','s','%','d','.','%','d',';',
548 ' ','%','s',';',' ','T','r','i','d','e','n','t','/','5','.','0',0};
549 static const WCHAR post_platform_keyW[] =
550 {'S','O','F','T','W','A','R','E',
551 '\\','M','i','c','r','o','s','o','f','t',
552 '\\','W','i','n','d','o','w','s',
553 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
554 '\\','I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',
555 '\\','5','.','0','\\','U','s','e','r',' ','A','g','e','n','t',
556 '\\','P','o','s','t',' ','P','l','a','t','f','o','r','m',0};
557 static const WCHAR ntW[] = {'N','T',' ',0};
558 static const WCHAR win32W[] = {'W','i','n','3','2',0};
559 static const WCHAR win64W[] = {'W','i','n','6','4',0};
560 static const WCHAR wow64W[] = {'W','O','W','6','4',0};
561 static const WCHAR emptyW[] = {0};
563 if(user_agent)
564 return;
566 GetVersionExW(&info);
567 is_nt = info.dwPlatformId == VER_PLATFORM_WIN32_NT ? ntW : emptyW;
569 if(sizeof(void*) == 8)
570 os_type = win64W;
571 else if(IsWow64Process(GetCurrentProcess(), &is_wow) && is_wow)
572 os_type = wow64W;
573 else
574 os_type = win32W;
576 sprintfW(buf, formatW, is_nt, info.dwMajorVersion, info.dwMinorVersion, os_type);
577 len = strlenW(buf);
579 size = len+40;
580 ret = heap_alloc(size * sizeof(WCHAR));
581 if(!ret)
582 return;
584 memcpy(ret, buf, len*sizeof(WCHAR));
586 res = RegOpenKeyW(HKEY_LOCAL_MACHINE, post_platform_keyW, &key);
587 if(res == ERROR_SUCCESS) {
588 DWORD value_len;
590 while(1) {
591 value_len = sizeof(buf)/sizeof(WCHAR);
592 res = RegEnumValueW(key, idx, buf, &value_len, NULL, NULL, NULL, NULL);
593 if(res != ERROR_SUCCESS)
594 break;
595 idx++;
597 if(len + value_len + 2 /* strlen("; ") */ + 1 /* trailing ')' */ >= size) {
598 tmp = heap_realloc(ret, (size*2+value_len)*sizeof(WCHAR));
599 if(!tmp)
600 break;
601 ret = tmp;
602 size = size*2+value_len;
605 ret[len++] = ';';
606 ret[len++] = ' ';
607 memcpy(ret+len, buf, value_len*sizeof(WCHAR));
608 len += value_len;
611 RegCloseKey(key);
614 ret[len++] = ')';
615 ret[len++] = 0;
617 user_agent = ret;
618 TRACE("Using user agent %s\n", debugstr_w(user_agent));
621 LPWSTR get_useragent(void)
623 LPWSTR ret;
625 ensure_useragent();
627 EnterCriticalSection(&session_cs);
628 ret = heap_strdupW(user_agent);
629 LeaveCriticalSection(&session_cs);
631 return ret;
634 HRESULT WINAPI UrlMkGetSessionOption(DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength,
635 DWORD* pdwBufferLength, DWORD dwReserved)
637 TRACE("(%x, %p, %d, %p)\n", dwOption, pBuffer, dwBufferLength, pdwBufferLength);
639 if(dwReserved)
640 WARN("dwReserved = %d\n", dwReserved);
642 switch(dwOption) {
643 case URLMON_OPTION_USERAGENT: {
644 HRESULT hres = E_OUTOFMEMORY;
645 DWORD size;
647 if(!pdwBufferLength)
648 return E_INVALIDARG;
650 EnterCriticalSection(&session_cs);
652 ensure_useragent();
653 if(user_agent) {
654 size = WideCharToMultiByte(CP_ACP, 0, user_agent, -1, NULL, 0, NULL, NULL);
655 *pdwBufferLength = size;
656 if(size <= dwBufferLength) {
657 if(pBuffer)
658 WideCharToMultiByte(CP_ACP, 0, user_agent, -1, pBuffer, size, NULL, NULL);
659 else
660 hres = E_INVALIDARG;
664 LeaveCriticalSection(&session_cs);
666 /* Tests prove that we have to return E_OUTOFMEMORY on success. */
667 return hres;
669 case URLMON_OPTION_URL_ENCODING: {
670 DWORD encoding = 0;
672 if(!pBuffer || dwBufferLength < sizeof(DWORD) || !pdwBufferLength)
673 return E_INVALIDARG;
675 if(!get_url_encoding(HKEY_CURRENT_USER, &encoding))
676 get_url_encoding(HKEY_LOCAL_MACHINE, &encoding);
678 *pdwBufferLength = sizeof(DWORD);
679 *(DWORD*)pBuffer = encoding ? URL_ENCODING_DISABLE_UTF8 : URL_ENCODING_ENABLE_UTF8;
680 return S_OK;
682 default:
683 FIXME("unsupported option %x\n", dwOption);
686 return E_INVALIDARG;
689 /**************************************************************************
690 * UrlMkSetSessionOption (URLMON.@)
692 HRESULT WINAPI UrlMkSetSessionOption(DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength,
693 DWORD Reserved)
695 TRACE("(%x %p %x)\n", dwOption, pBuffer, dwBufferLength);
697 switch(dwOption) {
698 case URLMON_OPTION_USERAGENT: {
699 LPWSTR new_user_agent;
700 char *buf = pBuffer;
701 DWORD len, size;
703 if(!pBuffer || !dwBufferLength)
704 return E_INVALIDARG;
706 for(len=0; len<dwBufferLength && buf[len]; len++);
708 TRACE("Setting user agent %s\n", debugstr_an(buf, len));
710 size = MultiByteToWideChar(CP_ACP, 0, buf, len, NULL, 0);
711 new_user_agent = heap_alloc((size+1)*sizeof(WCHAR));
712 if(!new_user_agent)
713 return E_OUTOFMEMORY;
714 MultiByteToWideChar(CP_ACP, 0, buf, len, new_user_agent, size);
715 new_user_agent[size] = 0;
717 EnterCriticalSection(&session_cs);
719 heap_free(user_agent);
720 user_agent = new_user_agent;
722 LeaveCriticalSection(&session_cs);
723 break;
725 default:
726 FIXME("Unknown option %x\n", dwOption);
727 return E_INVALIDARG;
730 return S_OK;
733 /**************************************************************************
734 * ObtainUserAgentString (URLMON.@)
736 HRESULT WINAPI ObtainUserAgentString(DWORD dwOption, LPSTR pcszUAOut, DWORD *cbSize)
738 DWORD size;
739 HRESULT hres = E_FAIL;
741 TRACE("(%d %p %p)\n", dwOption, pcszUAOut, cbSize);
743 if(!pcszUAOut || !cbSize)
744 return E_INVALIDARG;
746 EnterCriticalSection(&session_cs);
748 ensure_useragent();
749 if(user_agent) {
750 size = WideCharToMultiByte(CP_ACP, 0, user_agent, -1, NULL, 0, NULL, NULL);
752 if(size <= *cbSize) {
753 WideCharToMultiByte(CP_ACP, 0, user_agent, -1, pcszUAOut, *cbSize, NULL, NULL);
754 hres = S_OK;
755 }else {
756 hres = E_OUTOFMEMORY;
759 *cbSize = size;
762 LeaveCriticalSection(&session_cs);
763 return hres;
766 void free_session(void)
768 name_space *ns_iter, *ns_last;
769 mime_filter *mf_iter, *mf_last;
771 LIST_FOR_EACH_ENTRY_SAFE(ns_iter, ns_last, &name_space_list, name_space, entry) {
772 if(!ns_iter->urlmon)
773 IClassFactory_Release(ns_iter->cf);
774 heap_free(ns_iter->protocol);
775 heap_free(ns_iter);
778 LIST_FOR_EACH_ENTRY_SAFE(mf_iter, mf_last, &mime_filter_list, mime_filter, entry) {
779 IClassFactory_Release(mf_iter->cf);
780 heap_free(mf_iter->mime);
781 heap_free(mf_iter);
784 heap_free(user_agent);