urlmon: Don't create stgmed_obj for binding to object.
[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 name_space {
27     LPWSTR protocol;
28     IClassFactory *cf;
29     CLSID clsid;
30
31     struct name_space *next;
32 } name_space;
33
34 typedef struct mime_filter {
35     IClassFactory *cf;
36     CLSID clsid;
37     LPWSTR mime;
38
39     struct mime_filter *next;
40 } mime_filter;
41
42 static name_space *name_space_list = NULL;
43 static mime_filter *mime_filter_list = NULL;
44
45 static name_space *find_name_space(LPCWSTR protocol)
46 {
47     name_space *iter;
48
49     for(iter = name_space_list; iter; iter = iter->next) {
50         if(!strcmpW(iter->protocol, protocol))
51             return iter;
52     }
53
54     return NULL;
55 }
56
57 static HRESULT get_protocol_cf(LPCWSTR schema, DWORD schema_len, CLSID *pclsid, IClassFactory **ret)
58 {
59     WCHAR str_clsid[64];
60     HKEY hkey = NULL;
61     DWORD res, type, size;
62     CLSID clsid;
63     LPWSTR wszKey;
64     HRESULT hres;
65
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};
69
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));
73
74     res = RegOpenKeyW(HKEY_CLASSES_ROOT, wszKey, &hkey);
75     heap_free(wszKey);
76     if(res != ERROR_SUCCESS) {
77         TRACE("Could not open protocol handler key\n");
78         return E_FAIL;
79     }
80     
81     size = sizeof(str_clsid);
82     res = RegQueryValueExW(hkey, wszCLSID, NULL, &type, (LPBYTE)str_clsid, &size);
83     RegCloseKey(hkey);
84     if(res != ERROR_SUCCESS || type != REG_SZ) {
85         WARN("Could not get protocol CLSID res=%d\n", res);
86         return E_FAIL;
87     }
88
89     hres = CLSIDFromString(str_clsid, &clsid);
90     if(FAILED(hres)) {
91         WARN("CLSIDFromString failed: %08x\n", hres);
92         return hres;
93     }
94
95     if(pclsid)
96         *pclsid = clsid;
97
98     if(!ret)
99         return S_OK;
100
101     return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IClassFactory, (void**)ret);
102 }
103
104 BOOL is_registered_protocol(LPCWSTR url)
105 {
106     DWORD schema_len;
107     WCHAR schema[64];
108     HRESULT hres;
109
110     hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, schema, sizeof(schema)/sizeof(schema[0]),
111             &schema_len, 0);
112     if(FAILED(hres))
113         return FALSE;
114
115     return get_protocol_cf(schema, schema_len, NULL, NULL) == S_OK;
116 }
117
118 IInternetProtocolInfo *get_protocol_info(LPCWSTR url)
119 {
120     IInternetProtocolInfo *ret = NULL;
121     IClassFactory *cf;
122     name_space *ns;
123     WCHAR schema[64];
124     DWORD schema_len;
125     HRESULT hres;
126
127     hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, schema, sizeof(schema)/sizeof(schema[0]),
128             &schema_len, 0);
129     if(FAILED(hres) || !schema_len)
130         return NULL;
131
132     ns = find_name_space(schema);
133     if(ns) {
134         hres = IClassFactory_QueryInterface(ns->cf, &IID_IInternetProtocolInfo, (void**)&ret);
135         if(SUCCEEDED(hres))
136             return ret;
137
138         hres = IClassFactory_CreateInstance(ns->cf, NULL, &IID_IInternetProtocolInfo, (void**)&ret);
139         if(SUCCEEDED(hres))
140             return ret;
141     }
142
143     hres = get_protocol_cf(schema, schema_len, NULL, &cf);
144     if(FAILED(hres))
145         return NULL;
146
147     hres = IClassFactory_QueryInterface(cf, &IID_IInternetProtocolInfo, (void**)&ret);
148     if(FAILED(hres))
149         IClassFactory_CreateInstance(cf, NULL, &IID_IInternetProtocolInfo, (void**)&ret);
150     IClassFactory_Release(cf);
151
152     return ret;
153 }
154
155 HRESULT get_protocol_handler(LPCWSTR url, CLSID *clsid, IClassFactory **ret)
156 {
157     name_space *ns;
158     WCHAR schema[64];
159     DWORD schema_len;
160     HRESULT hres;
161
162     hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, schema, sizeof(schema)/sizeof(schema[0]),
163             &schema_len, 0);
164     if(FAILED(hres) || !schema_len)
165         return schema_len ? hres : E_FAIL;
166
167     ns = find_name_space(schema);
168     if(ns) {
169         *ret = ns->cf;
170         IClassFactory_AddRef(*ret);
171         if(clsid)
172             *clsid = ns->clsid;
173         return S_OK;
174     }
175
176     return get_protocol_cf(schema, schema_len, clsid, ret);
177 }
178
179 static HRESULT WINAPI InternetSession_QueryInterface(IInternetSession *iface,
180         REFIID riid, void **ppv)
181 {
182     TRACE("(%s %p)\n", debugstr_guid(riid), ppv);
183
184     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetSession, riid)) {
185         *ppv = iface;
186         IInternetSession_AddRef(iface);
187         return S_OK;
188     }
189
190     *ppv = NULL;
191     return E_NOINTERFACE;
192 }
193
194 static ULONG WINAPI InternetSession_AddRef(IInternetSession *iface)
195 {
196     TRACE("()\n");
197     URLMON_LockModule();
198     return 2;
199 }
200
201 static ULONG WINAPI InternetSession_Release(IInternetSession *iface)
202 {
203     TRACE("()\n");
204     URLMON_UnlockModule();
205     return 1;
206 }
207
208 static HRESULT WINAPI InternetSession_RegisterNameSpace(IInternetSession *iface,
209         IClassFactory *pCF, REFCLSID rclsid, LPCWSTR pwzProtocol, ULONG cPatterns,
210         const LPCWSTR *ppwzPatterns, DWORD dwReserved)
211 {
212     name_space *new_name_space;
213
214     TRACE("(%p %s %s %d %p %d)\n", pCF, debugstr_guid(rclsid), debugstr_w(pwzProtocol),
215           cPatterns, ppwzPatterns, dwReserved);
216
217     if(cPatterns || ppwzPatterns)
218         FIXME("patterns not supported\n");
219     if(dwReserved)
220         WARN("dwReserved = %d\n", dwReserved);
221
222     if(!pCF || !pwzProtocol)
223         return E_INVALIDARG;
224
225     new_name_space = heap_alloc(sizeof(name_space));
226
227     IClassFactory_AddRef(pCF);
228     new_name_space->cf = pCF;
229     new_name_space->clsid = *rclsid;
230     new_name_space->protocol = heap_strdupW(pwzProtocol);
231
232     new_name_space->next = name_space_list;
233     name_space_list = new_name_space;
234     return S_OK;
235 }
236
237 static HRESULT WINAPI InternetSession_UnregisterNameSpace(IInternetSession *iface,
238         IClassFactory *pCF, LPCWSTR pszProtocol)
239 {
240     name_space *iter, *last = NULL;
241
242     TRACE("(%p %s)\n", pCF, debugstr_w(pszProtocol));
243
244     if(!pCF || !pszProtocol)
245         return E_INVALIDARG;
246
247     for(iter = name_space_list; iter; iter = iter->next) {
248         if(iter->cf == pCF && !strcmpW(iter->protocol, pszProtocol))
249             break;
250         last = iter;
251     }
252
253     if(!iter)
254         return S_OK;
255
256     if(last)
257         last->next = iter->next;
258     else
259         name_space_list = iter->next;
260
261     IClassFactory_Release(iter->cf);
262     heap_free(iter->protocol);
263     heap_free(iter);
264
265     return S_OK;
266 }
267
268 static HRESULT WINAPI InternetSession_RegisterMimeFilter(IInternetSession *iface,
269         IClassFactory *pCF, REFCLSID rclsid, LPCWSTR pwzType)
270 {
271     mime_filter *filter;
272
273     TRACE("(%p %s %s)\n", pCF, debugstr_guid(rclsid), debugstr_w(pwzType));
274
275     filter = heap_alloc(sizeof(mime_filter));
276
277     IClassFactory_AddRef(pCF);
278     filter->cf = pCF;
279     filter->clsid = *rclsid;
280     filter->mime = heap_strdupW(pwzType);
281
282     filter->next = mime_filter_list;
283     mime_filter_list = filter;
284
285     return S_OK;
286 }
287
288 static HRESULT WINAPI InternetSession_UnregisterMimeFilter(IInternetSession *iface,
289         IClassFactory *pCF, LPCWSTR pwzType)
290 {
291     mime_filter *iter, *prev = NULL;
292
293     TRACE("(%p %s)\n", pCF, debugstr_w(pwzType));
294
295     for(iter = mime_filter_list; iter; iter = iter->next) {
296         if(iter->cf == pCF && !strcmpW(iter->mime, pwzType))
297             break;
298         prev = iter;
299     }
300
301     if(!iter)
302         return S_OK;
303
304     if(prev)
305         prev->next = iter->next;
306     else
307         mime_filter_list = iter->next;
308
309     IClassFactory_Release(iter->cf);
310     heap_free(iter->mime);
311     heap_free(iter);
312
313     return S_OK;
314 }
315
316 static HRESULT WINAPI InternetSession_CreateBinding(IInternetSession *iface,
317         LPBC pBC, LPCWSTR szUrl, IUnknown *pUnkOuter, IUnknown **ppUnk,
318         IInternetProtocol **ppOInetProt, DWORD dwOption)
319 {
320     TRACE("(%p %s %p %p %p %08x)\n", pBC, debugstr_w(szUrl), pUnkOuter, ppUnk,
321             ppOInetProt, dwOption);
322
323     if(pBC || pUnkOuter || ppUnk || dwOption)
324         FIXME("Unsupported arguments\n");
325
326     return create_binding_protocol(szUrl, FALSE, ppOInetProt);
327 }
328
329 static HRESULT WINAPI InternetSession_SetSessionOption(IInternetSession *iface,
330         DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength, DWORD dwReserved)
331 {
332     FIXME("(%08x %p %d %d)\n", dwOption, pBuffer, dwBufferLength, dwReserved);
333     return E_NOTIMPL;
334 }
335
336 static const IInternetSessionVtbl InternetSessionVtbl = {
337     InternetSession_QueryInterface,
338     InternetSession_AddRef,
339     InternetSession_Release,
340     InternetSession_RegisterNameSpace,
341     InternetSession_UnregisterNameSpace,
342     InternetSession_RegisterMimeFilter,
343     InternetSession_UnregisterMimeFilter,
344     InternetSession_CreateBinding,
345     InternetSession_SetSessionOption
346 };
347
348 static IInternetSession InternetSession = { &InternetSessionVtbl };
349
350 /***********************************************************************
351  *           CoInternetGetSession (URLMON.@)
352  *
353  * Create a new internet session and return an IInternetSession interface
354  * representing it.
355  *
356  * PARAMS
357  *    dwSessionMode      [I] Mode for the internet session
358  *    ppIInternetSession [O] Destination for creates IInternetSession object
359  *    dwReserved         [I] Reserved, must be 0.
360  *
361  * RETURNS
362  *    Success: S_OK. ppIInternetSession contains the IInternetSession interface.
363  *    Failure: E_INVALIDARG, if any argument is invalid, or
364  *             E_OUTOFMEMORY if memory allocation fails.
365  */
366 HRESULT WINAPI CoInternetGetSession(DWORD dwSessionMode, IInternetSession **ppIInternetSession,
367         DWORD dwReserved)
368 {
369     TRACE("(%d %p %d)\n", dwSessionMode, ppIInternetSession, dwReserved);
370
371     if(dwSessionMode)
372         ERR("dwSessionMode=%d\n", dwSessionMode);
373     if(dwReserved)
374         ERR("dwReserved=%d\n", dwReserved);
375
376     IInternetSession_AddRef(&InternetSession);
377     *ppIInternetSession = &InternetSession;
378     return S_OK;
379 }
380
381 /**************************************************************************
382  *                 UrlMkGetSessionOption (URLMON.@)
383  */
384 static BOOL get_url_encoding(HKEY root, DWORD *encoding)
385 {
386     DWORD size = sizeof(DWORD), res, type;
387     HKEY hkey;
388
389     static const WCHAR wszKeyName[] = 
390         {'S','O','F','T','W','A','R','E',
391          '\\','M','i','c','r','o','s','o','f','t',
392          '\\','W','i','n','d','o','w','s',
393          '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
394          '\\','I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0};
395     static const WCHAR wszUrlEncoding[] = {'U','r','l','E','n','c','o','d','i','n','g',0};
396
397     res = RegOpenKeyW(root, wszKeyName, &hkey);
398     if(res != ERROR_SUCCESS)
399         return FALSE;
400
401     res = RegQueryValueExW(hkey, wszUrlEncoding, NULL, &type, (LPBYTE)encoding, &size);
402     RegCloseKey(hkey);
403
404     return res == ERROR_SUCCESS;
405 }
406
407 HRESULT WINAPI UrlMkGetSessionOption(DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength,
408                                      DWORD* pdwBufferLength, DWORD dwReserved)
409 {
410     TRACE("(%x, %p, %d, %p)\n", dwOption, pBuffer, dwBufferLength, pdwBufferLength);
411
412     if(dwReserved)
413         WARN("dwReserved = %d\n", dwReserved);
414
415     switch(dwOption) {
416     case URLMON_OPTION_URL_ENCODING: {
417         DWORD encoding = 0;
418
419         if(!pBuffer || dwBufferLength < sizeof(DWORD) || !pdwBufferLength)
420             return E_INVALIDARG;
421
422         if(!get_url_encoding(HKEY_CURRENT_USER, &encoding))
423             get_url_encoding(HKEY_LOCAL_MACHINE, &encoding);
424
425         *pdwBufferLength = sizeof(DWORD);
426         *(DWORD*)pBuffer = encoding ? URL_ENCODING_DISABLE_UTF8 : URL_ENCODING_ENABLE_UTF8;
427         return S_OK;
428     }
429     default:
430         FIXME("unsupported option %x\n", dwOption);
431     }
432
433     return E_INVALIDARG;
434 }