msvcp90: Added numpunct<wchar> implementation.
[wine] / dlls / 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 {
27     LPWSTR protocol;
28     IClassFactory *cf;
29     CLSID clsid;
30     BOOL urlmon;
31
32     struct list entry;
33 } name_space;
34
35 typedef struct {
36     IClassFactory *cf;
37     CLSID clsid;
38     LPWSTR mime;
39
40     struct list entry;
41 } mime_filter;
42
43 static struct list name_space_list = LIST_INIT(name_space_list);
44 static struct list mime_filter_list = LIST_INIT(mime_filter_list);
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     LIST_FOR_EACH_ENTRY(iter, &name_space_list, name_space, entry) {
67         if(!strcmpiW(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 MK_E_SYNTAX;
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 MK_E_SYNTAX;
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     hres = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IClassFactory, (void**)ret);
119     return SUCCEEDED(hres) ? S_OK : MK_E_SYNTAX;
120 }
121
122 HRESULT register_namespace(IClassFactory *cf, REFIID clsid, LPCWSTR protocol, BOOL urlmon_protocol)
123 {
124     name_space *new_name_space;
125
126     new_name_space = heap_alloc(sizeof(name_space));
127
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);
134
135     EnterCriticalSection(&session_cs);
136
137     list_add_head(&name_space_list, &new_name_space->entry);
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;
147
148     EnterCriticalSection(&session_cs);
149
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);
153
154             LeaveCriticalSection(&session_cs);
155
156             if(!iter->urlmon)
157                 IClassFactory_Release(iter->cf);
158             heap_free(iter->protocol);
159             heap_free(iter);
160             return S_OK;
161         }
162     }
163
164     LeaveCriticalSection(&session_cs);
165     return S_OK;
166 }
167
168 BOOL is_registered_protocol(LPCWSTR url)
169 {
170     DWORD schema_len;
171     WCHAR schema[64];
172     HRESULT hres;
173
174     hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, schema, sizeof(schema)/sizeof(schema[0]),
175             &schema_len, 0);
176     if(FAILED(hres))
177         return FALSE;
178
179     return get_protocol_cf(schema, schema_len, NULL, NULL) == S_OK;
180 }
181
182 IInternetProtocolInfo *get_protocol_info(LPCWSTR url)
183 {
184     IInternetProtocolInfo *ret = NULL;
185     IClassFactory *cf;
186     name_space *ns;
187     WCHAR schema[64];
188     DWORD schema_len;
189     HRESULT hres;
190
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;
195
196     EnterCriticalSection(&session_cs);
197
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);
203     }
204
205     LeaveCriticalSection(&session_cs);
206
207     if(ns && SUCCEEDED(hres))
208         return ret;
209
210     hres = get_protocol_cf(schema, schema_len, NULL, &cf);
211     if(FAILED(hres))
212         return NULL;
213
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);
218
219     return ret;
220 }
221
222 HRESULT get_protocol_handler(IUri *uri, CLSID *clsid, BOOL *urlmon_protocol, IClassFactory **ret)
223 {
224     name_space *ns;
225     BSTR scheme;
226     HRESULT hres;
227
228     *ret = NULL;
229
230     /* FIXME: Avoid GetSchemeName call for known schemes */
231     hres = IUri_GetSchemeName(uri, &scheme);
232     if(FAILED(hres))
233         return hres;
234
235     EnterCriticalSection(&session_cs);
236
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;
245     }
246
247     LeaveCriticalSection(&session_cs);
248
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);
255     }
256
257     SysFreeString(scheme);
258     return hres;
259 }
260
261 IInternetProtocol *get_mime_filter(LPCWSTR mime)
262 {
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};
266
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;
275
276     EnterCriticalSection(&session_cs);
277
278     LIST_FOR_EACH_ENTRY(iter, &mime_filter_list, mime_filter, entry) {
279         if(!strcmpW(iter->mime, mime)) {
280             cf = iter->cf;
281             break;
282         }
283     }
284
285     LeaveCriticalSection(&session_cs);
286
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;
292         }
293
294         return ret;
295     }
296
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;
301     }
302
303     res = RegOpenKeyW(hlist, mime, &hfilter);
304     CloseHandle(hlist);
305     if(res != ERROR_SUCCESS)
306         return NULL;
307
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;
314     }
315
316     hres = CLSIDFromString(clsidw, &clsid);
317     if(FAILED(hres)) {
318         WARN("CLSIDFromString failed for %s (%x)\n", debugstr_w(mime), hres);
319         return NULL;
320     }
321
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;
326     }
327
328     return ret;
329 }
330
331 static HRESULT WINAPI InternetSession_QueryInterface(IInternetSession *iface,
332         REFIID riid, void **ppv)
333 {
334     TRACE("(%s %p)\n", debugstr_guid(riid), ppv);
335
336     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetSession, riid)) {
337         *ppv = iface;
338         IInternetSession_AddRef(iface);
339         return S_OK;
340     }
341
342     *ppv = NULL;
343     return E_NOINTERFACE;
344 }
345
346 static ULONG WINAPI InternetSession_AddRef(IInternetSession *iface)
347 {
348     TRACE("()\n");
349     URLMON_LockModule();
350     return 2;
351 }
352
353 static ULONG WINAPI InternetSession_Release(IInternetSession *iface)
354 {
355     TRACE("()\n");
356     URLMON_UnlockModule();
357     return 1;
358 }
359
360 static HRESULT WINAPI InternetSession_RegisterNameSpace(IInternetSession *iface,
361         IClassFactory *pCF, REFCLSID rclsid, LPCWSTR pwzProtocol, ULONG cPatterns,
362         const LPCWSTR *ppwzPatterns, DWORD dwReserved)
363 {
364     TRACE("(%p %s %s %d %p %d)\n", pCF, debugstr_guid(rclsid), debugstr_w(pwzProtocol),
365           cPatterns, ppwzPatterns, dwReserved);
366
367     if(cPatterns || ppwzPatterns)
368         FIXME("patterns not supported\n");
369     if(dwReserved)
370         WARN("dwReserved = %d\n", dwReserved);
371
372     if(!pCF || !pwzProtocol)
373         return E_INVALIDARG;
374
375     return register_namespace(pCF, rclsid, pwzProtocol, FALSE);
376 }
377
378 static HRESULT WINAPI InternetSession_UnregisterNameSpace(IInternetSession *iface,
379         IClassFactory *pCF, LPCWSTR pszProtocol)
380 {
381     TRACE("(%p %s)\n", pCF, debugstr_w(pszProtocol));
382
383     if(!pCF || !pszProtocol)
384         return E_INVALIDARG;
385
386     return unregister_namespace(pCF, pszProtocol);
387 }
388
389 static HRESULT WINAPI InternetSession_RegisterMimeFilter(IInternetSession *iface,
390         IClassFactory *pCF, REFCLSID rclsid, LPCWSTR pwzType)
391 {
392     mime_filter *filter;
393
394     TRACE("(%p %s %s)\n", pCF, debugstr_guid(rclsid), debugstr_w(pwzType));
395
396     filter = heap_alloc(sizeof(mime_filter));
397
398     IClassFactory_AddRef(pCF);
399     filter->cf = pCF;
400     filter->clsid = *rclsid;
401     filter->mime = heap_strdupW(pwzType);
402
403     EnterCriticalSection(&session_cs);
404
405     list_add_head(&mime_filter_list, &filter->entry);
406
407     LeaveCriticalSection(&session_cs);
408
409     return S_OK;
410 }
411
412 static HRESULT WINAPI InternetSession_UnregisterMimeFilter(IInternetSession *iface,
413         IClassFactory *pCF, LPCWSTR pwzType)
414 {
415     mime_filter *iter;
416
417     TRACE("(%p %s)\n", pCF, debugstr_w(pwzType));
418
419     EnterCriticalSection(&session_cs);
420
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);
424
425             LeaveCriticalSection(&session_cs);
426
427             IClassFactory_Release(iter->cf);
428             heap_free(iter->mime);
429             heap_free(iter);
430             return S_OK;
431         }
432     }
433
434     LeaveCriticalSection(&session_cs);
435     return S_OK;
436 }
437
438 static HRESULT WINAPI InternetSession_CreateBinding(IInternetSession *iface,
439         LPBC pBC, LPCWSTR szUrl, IUnknown *pUnkOuter, IUnknown **ppUnk,
440         IInternetProtocol **ppOInetProt, DWORD dwOption)
441 {
442     BindProtocol *protocol;
443     HRESULT hres;
444
445     TRACE("(%p %s %p %p %p %08x)\n", pBC, debugstr_w(szUrl), pUnkOuter, ppUnk,
446             ppOInetProt, dwOption);
447
448     if(pBC || pUnkOuter || ppUnk || dwOption)
449         FIXME("Unsupported arguments\n");
450
451     hres = create_binding_protocol(FALSE, &protocol);
452     if(FAILED(hres))
453         return hres;
454
455     *ppOInetProt = (IInternetProtocol*)&protocol->IInternetProtocolEx_iface;
456     return S_OK;
457 }
458
459 static HRESULT WINAPI InternetSession_SetSessionOption(IInternetSession *iface,
460         DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength, DWORD dwReserved)
461 {
462     FIXME("(%08x %p %d %d)\n", dwOption, pBuffer, dwBufferLength, dwReserved);
463     return E_NOTIMPL;
464 }
465
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
476 };
477
478 static IInternetSession InternetSession = { &InternetSessionVtbl };
479
480 /***********************************************************************
481  *           CoInternetGetSession (URLMON.@)
482  *
483  * Create a new internet session and return an IInternetSession interface
484  * representing it.
485  *
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.
490  *
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.
495  */
496 HRESULT WINAPI CoInternetGetSession(DWORD dwSessionMode, IInternetSession **ppIInternetSession,
497         DWORD dwReserved)
498 {
499     TRACE("(%d %p %d)\n", dwSessionMode, ppIInternetSession, dwReserved);
500
501     if(dwSessionMode)
502         ERR("dwSessionMode=%d\n", dwSessionMode);
503     if(dwReserved)
504         ERR("dwReserved=%d\n", dwReserved);
505
506     IInternetSession_AddRef(&InternetSession);
507     *ppIInternetSession = &InternetSession;
508     return S_OK;
509 }
510
511 /**************************************************************************
512  *                 UrlMkGetSessionOption (URLMON.@)
513  */
514 static BOOL get_url_encoding(HKEY root, DWORD *encoding)
515 {
516     DWORD size = sizeof(DWORD), res, type;
517     HKEY hkey;
518
519     static const WCHAR wszUrlEncoding[] = {'U','r','l','E','n','c','o','d','i','n','g',0};
520
521     res = RegOpenKeyW(root, internet_settings_keyW, &hkey);
522     if(res != ERROR_SUCCESS)
523         return FALSE;
524
525     res = RegQueryValueExW(hkey, wszUrlEncoding, NULL, &type, (LPBYTE)encoding, &size);
526     RegCloseKey(hkey);
527
528     return res == ERROR_SUCCESS;
529 }
530
531 static LPWSTR user_agent;
532
533 static void ensure_useragent(void)
534 {
535     DWORD size = sizeof(DWORD), res, type;
536     HKEY hkey;
537
538     static const WCHAR user_agentW[] = {'U','s','e','r',' ','A','g','e','n','t',0};
539
540     if(user_agent)
541         return;
542
543     res = RegOpenKeyW(HKEY_CURRENT_USER, internet_settings_keyW, &hkey);
544     if(res != ERROR_SUCCESS)
545         return;
546
547     res = RegQueryValueExW(hkey, user_agentW, NULL, &type, NULL, &size);
548     if(res == ERROR_SUCCESS && type == REG_SZ) {
549         user_agent = heap_alloc(size);
550         res = RegQueryValueExW(hkey, user_agentW, NULL, &type, (LPBYTE)user_agent, &size);
551         if(res != ERROR_SUCCESS) {
552             heap_free(user_agent);
553             user_agent = NULL;
554         }
555     }else {
556         WARN("Could not find User Agent value: %u\n", res);
557     }
558
559     RegCloseKey(hkey);
560 }
561
562 LPWSTR get_useragent(void)
563 {
564     LPWSTR ret;
565
566     ensure_useragent();
567
568     EnterCriticalSection(&session_cs);
569     ret = heap_strdupW(user_agent);
570     LeaveCriticalSection(&session_cs);
571
572     return ret;
573 }
574
575 HRESULT WINAPI UrlMkGetSessionOption(DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength,
576                                      DWORD* pdwBufferLength, DWORD dwReserved)
577 {
578     TRACE("(%x, %p, %d, %p)\n", dwOption, pBuffer, dwBufferLength, pdwBufferLength);
579
580     if(dwReserved)
581         WARN("dwReserved = %d\n", dwReserved);
582
583     switch(dwOption) {
584     case URLMON_OPTION_USERAGENT: {
585         HRESULT hres = E_OUTOFMEMORY;
586         DWORD size;
587
588         if(!pdwBufferLength)
589             return E_INVALIDARG;
590
591         EnterCriticalSection(&session_cs);
592
593         ensure_useragent();
594         if(user_agent) {
595             size = WideCharToMultiByte(CP_ACP, 0, user_agent, -1, NULL, 0, NULL, NULL);
596             *pdwBufferLength = size;
597             if(size <= dwBufferLength) {
598                 if(pBuffer)
599                     WideCharToMultiByte(CP_ACP, 0, user_agent, -1, pBuffer, size, NULL, NULL);
600                 else
601                     hres = E_INVALIDARG;
602             }
603         }
604
605         LeaveCriticalSection(&session_cs);
606
607         /* Tests prove that we have to return E_OUTOFMEMORY on success. */
608         return hres;
609     }
610     case URLMON_OPTION_URL_ENCODING: {
611         DWORD encoding = 0;
612
613         if(!pBuffer || dwBufferLength < sizeof(DWORD) || !pdwBufferLength)
614             return E_INVALIDARG;
615
616         if(!get_url_encoding(HKEY_CURRENT_USER, &encoding))
617             get_url_encoding(HKEY_LOCAL_MACHINE, &encoding);
618
619         *pdwBufferLength = sizeof(DWORD);
620         *(DWORD*)pBuffer = encoding ? URL_ENCODING_DISABLE_UTF8 : URL_ENCODING_ENABLE_UTF8;
621         return S_OK;
622     }
623     default:
624         FIXME("unsupported option %x\n", dwOption);
625     }
626
627     return E_INVALIDARG;
628 }
629
630 /**************************************************************************
631  *                 UrlMkSetSessionOption (URLMON.@)
632  */
633 HRESULT WINAPI UrlMkSetSessionOption(DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength,
634         DWORD Reserved)
635 {
636     TRACE("(%x %p %x)\n", dwOption, pBuffer, dwBufferLength);
637
638     switch(dwOption) {
639     case URLMON_OPTION_USERAGENT: {
640         LPWSTR new_user_agent;
641         char *buf = pBuffer;
642         DWORD len, size;
643
644         if(!pBuffer || !dwBufferLength)
645             return E_INVALIDARG;
646
647         for(len=0; len<dwBufferLength && buf[len]; len++);
648
649         TRACE("Setting user agent %s\n", debugstr_an(buf, len));
650
651         size = MultiByteToWideChar(CP_ACP, 0, buf, len, NULL, 0);
652         new_user_agent = heap_alloc((size+1)*sizeof(WCHAR));
653         if(!new_user_agent)
654             return E_OUTOFMEMORY;
655         MultiByteToWideChar(CP_ACP, 0, buf, len, new_user_agent, size);
656         new_user_agent[size] = 0;
657
658         EnterCriticalSection(&session_cs);
659
660         heap_free(user_agent);
661         user_agent = new_user_agent;
662
663         LeaveCriticalSection(&session_cs);
664         break;
665     }
666     default:
667         FIXME("Unknown option %x\n", dwOption);
668         return E_INVALIDARG;
669     }
670
671     return S_OK;
672 }
673
674 /**************************************************************************
675  *                 ObtainUserAgentString (URLMON.@)
676  */
677 HRESULT WINAPI ObtainUserAgentString(DWORD dwOption, LPSTR pcszUAOut, DWORD *cbSize)
678 {
679     DWORD size;
680     HRESULT hres = E_FAIL;
681
682     TRACE("(%d %p %p)\n", dwOption, pcszUAOut, cbSize);
683
684     if(!pcszUAOut || !cbSize)
685         return E_INVALIDARG;
686
687     EnterCriticalSection(&session_cs);
688
689     ensure_useragent();
690     if(user_agent) {
691         size = WideCharToMultiByte(CP_ACP, 0, user_agent, -1, NULL, 0, NULL, NULL);
692
693         if(size <= *cbSize) {
694             WideCharToMultiByte(CP_ACP, 0, user_agent, -1, pcszUAOut, *cbSize, NULL, NULL);
695             hres = S_OK;
696         }else {
697             hres = E_OUTOFMEMORY;
698         }
699
700         *cbSize = size;
701     }
702
703     LeaveCriticalSection(&session_cs);
704     return hres;
705 }
706
707 void free_session(void)
708 {
709     name_space *ns_iter, *ns_last;
710     mime_filter *mf_iter, *mf_last;
711
712     LIST_FOR_EACH_ENTRY_SAFE(ns_iter, ns_last, &name_space_list, name_space, entry) {
713             if(!ns_iter->urlmon)
714                 IClassFactory_Release(ns_iter->cf);
715             heap_free(ns_iter->protocol);
716             heap_free(ns_iter);
717     }
718
719     LIST_FOR_EACH_ENTRY_SAFE(mf_iter, mf_last, &mime_filter_list, mime_filter, entry) {
720             IClassFactory_Release(mf_iter->cf);
721             heap_free(mf_iter->mime);
722             heap_free(mf_iter);
723     }
724
725     heap_free(user_agent);
726 }