shlwapi: Add some tests for AssocQueryStringW.
[wine] / dlls / urlmon / bindctx.c
1 /*
2  * Copyright 2007 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 <stdio.h>
20
21 #include "urlmon_main.h"
22 #include "wine/debug.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
25
26 static WCHAR BSCBHolder[] = { '_','B','S','C','B','_','H','o','l','d','e','r','_',0 };
27
28 extern IID IID_IBindStatusCallbackHolder;
29
30 typedef struct {
31     const IBindStatusCallbackVtbl  *lpBindStatusCallbackVtbl;
32     const IServiceProviderVtbl     *lpServiceProviderVtbl;
33     const IHttpNegotiate2Vtbl      *lpHttpNegotiate2Vtbl;
34     const IAuthenticateVtbl        *lpAuthenticateVtbl;
35
36     LONG ref;
37
38     IBindStatusCallback *callback;
39     IServiceProvider *serv_prov;
40
41     IHttpNegotiate *http_negotiate;
42     BOOL init_http_negotiate;
43     IHttpNegotiate2 *http_negotiate2;
44     BOOL init_http_negotiate2;
45     IAuthenticate *authenticate;
46     BOOL init_authenticate;
47 } BindStatusCallback;
48
49 #define STATUSCLB(x)     ((IBindStatusCallback*)  &(x)->lpBindStatusCallbackVtbl)
50 #define SERVPROV(x)      ((IServiceProvider*)     &(x)->lpServiceProviderVtbl)
51 #define HTTPNEG2(x)      ((IHttpNegotiate2*)      &(x)->lpHttpNegotiate2Vtbl)
52 #define AUTHENTICATE(x)  ((IAuthenticate*)        &(x)->lpAuthenticateVtbl)
53
54 #define STATUSCLB_THIS(iface) DEFINE_THIS(BindStatusCallback, BindStatusCallback, iface)
55
56 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
57         REFIID riid, void **ppv)
58 {
59     BindStatusCallback *This = STATUSCLB_THIS(iface);
60
61     *ppv = NULL;
62
63     if(IsEqualGUID(&IID_IUnknown, riid)) {
64         TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
65         *ppv = STATUSCLB(This);
66     }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
67         TRACE("(%p)->(IID_IBindStatusCallback, %p)\n", This, ppv);
68         *ppv = STATUSCLB(This);
69     }else if(IsEqualGUID(&IID_IBindStatusCallbackHolder, riid)) {
70         TRACE("(%p)->(IID_IBindStatusCallbackHolder, %p)\n", This, ppv);
71         *ppv = This;
72     }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
73         TRACE("(%p)->(IID_IServiceProvider, %p)\n", This, ppv);
74         *ppv = SERVPROV(This);
75     }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
76         TRACE("(%p)->(IID_IHttpNegotiate, %p)\n", This, ppv);
77         *ppv = HTTPNEG2(This);
78     }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
79         TRACE("(%p)->(IID_IHttpNegotiate2, %p)\n", This, ppv);
80         *ppv = HTTPNEG2(This);
81     }else if(IsEqualGUID(&IID_IAuthenticate, riid)) {
82         TRACE("(%p)->(IID_IAuthenticate, %p)\n", This, ppv);
83         *ppv = AUTHENTICATE(This);
84     }
85
86     if(*ppv) {
87         IBindStatusCallback_AddRef((IUnknown*)*ppv);
88         return S_OK;
89     }
90
91     TRACE("Unsupported riid = %s\n", debugstr_guid(riid));
92     return E_NOINTERFACE;
93 }
94
95 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
96 {
97     BindStatusCallback *This = STATUSCLB_THIS(iface);
98     LONG ref = InterlockedIncrement(&This->ref);
99
100     TRACE("(%p) ref = %d\n", This, ref);
101
102     return ref;
103 }
104
105 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
106 {
107     BindStatusCallback *This = STATUSCLB_THIS(iface);
108     LONG ref = InterlockedDecrement(&This->ref);
109
110     TRACE("(%p) ref = %d\n", This, ref);
111
112     if(!ref) {
113         if(This->serv_prov)
114             IServiceProvider_Release(This->serv_prov);
115         if(This->http_negotiate)
116             IHttpNegotiate_Release(This->http_negotiate);
117         if(This->http_negotiate2)
118             IHttpNegotiate2_Release(This->http_negotiate2);
119         if(This->authenticate)
120             IAuthenticate_Release(This->authenticate);
121         IBindStatusCallback_Release(This->callback);
122         heap_free(This);
123     }
124
125     return ref;
126 }
127
128 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
129         DWORD dwReserved, IBinding *pbind)
130 {
131     BindStatusCallback *This = STATUSCLB_THIS(iface);
132
133     TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
134
135     return IBindStatusCallback_OnStartBinding(This->callback, 0xff, pbind);
136 }
137
138 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
139 {
140     BindStatusCallback *This = STATUSCLB_THIS(iface);
141
142     TRACE("(%p)->(%p)\n", This, pnPriority);
143
144     return IBindStatusCallback_GetPriority(This->callback, pnPriority);
145 }
146
147 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
148 {
149     BindStatusCallback *This = STATUSCLB_THIS(iface);
150
151     TRACE("(%p)->(%d)\n", This, reserved);
152
153     return IBindStatusCallback_OnLowResource(This->callback, reserved);
154 }
155
156 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
157         ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
158 {
159     BindStatusCallback *This = STATUSCLB_THIS(iface);
160
161     TRACE("%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
162             debugstr_w(szStatusText));
163
164     return IBindStatusCallback_OnProgress(This->callback, ulProgress,
165             ulProgressMax, ulStatusCode, szStatusText);
166 }
167
168 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
169         HRESULT hresult, LPCWSTR szError)
170 {
171     BindStatusCallback *This = STATUSCLB_THIS(iface);
172
173     TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
174
175     return IBindStatusCallback_OnStopBinding(This->callback, hresult, szError);
176 }
177
178 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
179         DWORD *grfBINDF, BINDINFO *pbindinfo)
180 {
181     BindStatusCallback *This = STATUSCLB_THIS(iface);
182
183     TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
184
185     return IBindStatusCallback_GetBindInfo(This->callback, grfBINDF, pbindinfo);
186 }
187
188 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
189         DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
190 {
191     BindStatusCallback *This = STATUSCLB_THIS(iface);
192
193     TRACE("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
194
195     return IBindStatusCallback_OnDataAvailable(This->callback, grfBSCF, dwSize, pformatetc, pstgmed);
196 }
197
198 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
199         REFIID riid, IUnknown *punk)
200 {
201     BindStatusCallback *This = STATUSCLB_THIS(iface);
202
203     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
204
205     return IBindStatusCallback_OnObjectAvailable(This->callback, riid, punk);
206 }
207
208 #undef STATUSCLB_THIS
209
210 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
211     BindStatusCallback_QueryInterface,
212     BindStatusCallback_AddRef,
213     BindStatusCallback_Release,
214     BindStatusCallback_OnStartBinding,
215     BindStatusCallback_GetPriority,
216     BindStatusCallback_OnLowResource,
217     BindStatusCallback_OnProgress,
218     BindStatusCallback_OnStopBinding,
219     BindStatusCallback_GetBindInfo,
220     BindStatusCallback_OnDataAvailable,
221     BindStatusCallback_OnObjectAvailable
222 };
223
224 #define SERVPROV_THIS(iface) DEFINE_THIS(BindStatusCallback, ServiceProvider, iface)
225
226 static HRESULT WINAPI BSCServiceProvider_QueryInterface(IServiceProvider *iface,
227         REFIID riid, void **ppv)
228 {
229     BindStatusCallback *This = SERVPROV_THIS(iface);
230     return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
231 }
232
233 static ULONG WINAPI BSCServiceProvider_AddRef(IServiceProvider *iface)
234 {
235     BindStatusCallback *This = SERVPROV_THIS(iface);
236     return IBindStatusCallback_AddRef(STATUSCLB(This));
237 }
238
239 static ULONG WINAPI BSCServiceProvider_Release(IServiceProvider *iface)
240 {
241     BindStatusCallback *This = SERVPROV_THIS(iface);
242     return IBindStatusCallback_Release(STATUSCLB(This));
243 }
244
245 static HRESULT WINAPI BSCServiceProvider_QueryService(IServiceProvider *iface,
246         REFGUID guidService, REFIID riid, void **ppv)
247 {
248     BindStatusCallback *This = SERVPROV_THIS(iface);
249     HRESULT hres;
250
251     if(IsEqualGUID(&IID_IHttpNegotiate, guidService)) {
252         TRACE("(%p)->(IID_IHttpNegotiate %s %p)\n", This, debugstr_guid(riid), ppv);
253
254         if(!This->init_http_negotiate) {
255             This->init_http_negotiate = TRUE;
256             hres = IBindStatusCallback_QueryInterface(This->callback, &IID_IHttpNegotiate,
257                     (void**)&This->http_negotiate);
258             if(FAILED(hres) && This->serv_prov)
259                 IServiceProvider_QueryService(This->serv_prov, &IID_IHttpNegotiate,
260                         &IID_IHttpNegotiate, (void**)&This->http_negotiate);
261         }
262
263         return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
264     }
265
266     if(IsEqualGUID(&IID_IHttpNegotiate2, guidService)) {
267         TRACE("(%p)->(IID_IHttpNegotiate2 %s %p)\n", This, debugstr_guid(riid), ppv);
268
269         if(!This->init_http_negotiate2) {
270             This->init_http_negotiate2 = TRUE;
271             hres = IBindStatusCallback_QueryInterface(This->callback, &IID_IHttpNegotiate2,
272                     (void**)&This->http_negotiate2);
273             if(FAILED(hres) && This->serv_prov)
274                 IServiceProvider_QueryService(This->serv_prov, &IID_IHttpNegotiate2,
275                         &IID_IHttpNegotiate2, (void**)&This->http_negotiate2);
276         }
277
278         return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
279     }
280
281     if(IsEqualGUID(&IID_IAuthenticate, guidService)) {
282         TRACE("(%p)->(IID_IAuthenticate %s %p)\n", This, debugstr_guid(riid), ppv);
283
284         if(!This->init_authenticate) {
285             This->init_authenticate = TRUE;
286             hres = IBindStatusCallback_QueryInterface(This->callback, &IID_IAuthenticate,
287                     (void**)&This->authenticate);
288             if(FAILED(hres) && This->serv_prov)
289                 IServiceProvider_QueryService(This->serv_prov, &IID_IAuthenticate,
290                         &IID_IAuthenticate, (void**)&This->authenticate);
291         }
292
293         return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
294     }
295
296     TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
297
298     hres = IBindStatusCallback_QueryInterface(This->callback, riid, ppv);
299     if(SUCCEEDED(hres))
300         return S_OK;
301
302     if(This->serv_prov) {
303         hres = IServiceProvider_QueryService(This->serv_prov, guidService, riid, ppv);
304         if(SUCCEEDED(hres))
305             return S_OK;
306     }
307
308     return E_NOINTERFACE;
309 }
310
311 #undef SERVPROV_THIS
312
313 static const IServiceProviderVtbl BSCServiceProviderVtbl = {
314     BSCServiceProvider_QueryInterface,
315     BSCServiceProvider_AddRef,
316     BSCServiceProvider_Release,
317     BSCServiceProvider_QueryService
318 };
319
320 #define HTTPNEG2_THIS(iface) DEFINE_THIS(BindStatusCallback, HttpNegotiate2, iface)
321
322 static HRESULT WINAPI BSCHttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
323         REFIID riid, void **ppv)
324 {
325     BindStatusCallback *This = HTTPNEG2_THIS(iface);
326     return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
327 }
328
329 static ULONG WINAPI BSCHttpNegotiate_AddRef(IHttpNegotiate2 *iface)
330 {
331     BindStatusCallback *This = HTTPNEG2_THIS(iface);
332     return IBindStatusCallback_AddRef(STATUSCLB(This));
333 }
334
335 static ULONG WINAPI BSCHttpNegotiate_Release(IHttpNegotiate2 *iface)
336 {
337     BindStatusCallback *This = HTTPNEG2_THIS(iface);
338     return IBindStatusCallback_Release(STATUSCLB(This));
339 }
340
341 static HRESULT WINAPI BSCHttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
342         LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
343 {
344     BindStatusCallback *This = HTTPNEG2_THIS(iface);
345
346     TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders), dwReserved,
347           pszAdditionalHeaders);
348
349     *pszAdditionalHeaders = NULL;
350
351     if(!This->http_negotiate)
352         return S_OK;
353
354     return IHttpNegotiate_BeginningTransaction(This->http_negotiate, szURL, szHeaders,
355                                                dwReserved, pszAdditionalHeaders);
356 }
357
358 static HRESULT WINAPI BSCHttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
359         LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders,
360         LPWSTR *pszAdditionalRequestHeaders)
361 {
362     BindStatusCallback *This = HTTPNEG2_THIS(iface);
363     LPWSTR additional_headers = NULL;
364     HRESULT hres = S_OK;
365
366     TRACE("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
367           debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
368
369     if(This->http_negotiate)
370         hres = IHttpNegotiate_OnResponse(This->http_negotiate, dwResponseCode, szResponseHeaders,
371                                          szRequestHeaders, &additional_headers);
372
373     if(pszAdditionalRequestHeaders)
374         *pszAdditionalRequestHeaders = additional_headers;
375     else if(additional_headers)
376         CoTaskMemFree(additional_headers);
377
378     return hres;
379 }
380
381 static HRESULT WINAPI BSCHttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
382         BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
383 {
384     BindStatusCallback *This = HTTPNEG2_THIS(iface);
385
386     TRACE("(%p)->(%p %p %ld)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
387
388     if(!This->http_negotiate2)
389         return E_FAIL;
390
391     return IHttpNegotiate2_GetRootSecurityId(This->http_negotiate2, pbSecurityId,
392                                              pcbSecurityId, dwReserved);
393 }
394
395 #undef HTTPNEG2_THIS
396
397 static const IHttpNegotiate2Vtbl BSCHttpNegotiateVtbl = {
398     BSCHttpNegotiate_QueryInterface,
399     BSCHttpNegotiate_AddRef,
400     BSCHttpNegotiate_Release,
401     BSCHttpNegotiate_BeginningTransaction,
402     BSCHttpNegotiate_OnResponse,
403     BSCHttpNegotiate_GetRootSecurityId
404 };
405
406 #define AUTHENTICATE_THIS(iface)  DEFINE_THIS(BindStatusCallback, Authenticate, iface)
407
408 static HRESULT WINAPI BSCAuthenticate_QueryInterface(IAuthenticate *iface, REFIID riid, void **ppv)
409 {
410     BindStatusCallback *This = AUTHENTICATE_THIS(iface);
411     return IBindStatusCallback_QueryInterface(AUTHENTICATE(This), riid, ppv);
412 }
413
414 static ULONG WINAPI BSCAuthenticate_AddRef(IAuthenticate *iface)
415 {
416     BindStatusCallback *This = AUTHENTICATE_THIS(iface);
417     return IBindStatusCallback_AddRef(STATUSCLB(This));
418 }
419
420 static ULONG WINAPI BSCAuthenticate_Release(IAuthenticate *iface)
421 {
422     BindStatusCallback *This = AUTHENTICATE_THIS(iface);
423     return IBindStatusCallback_Release(STATUSCLB(This));
424 }
425
426 static HRESULT WINAPI BSCAuthenticate_Authenticate(IAuthenticate *iface,
427         HWND *phwnd, LPWSTR *pszUsername, LPWSTR *pszPassword)
428 {
429     BindStatusCallback *This = AUTHENTICATE_THIS(iface);
430     FIXME("(%p)->(%p %p %p)\n", This, phwnd, pszUsername, pszPassword);
431     return E_NOTIMPL;
432 }
433
434 #undef AUTHENTICATE_THIS
435
436 static const IAuthenticateVtbl BSCAuthenticateVtbl = {
437     BSCAuthenticate_QueryInterface,
438     BSCAuthenticate_AddRef,
439     BSCAuthenticate_Release,
440     BSCAuthenticate_Authenticate
441 };
442
443 static IBindStatusCallback *create_bsc(IBindStatusCallback *bsc)
444 {
445     BindStatusCallback *ret = heap_alloc_zero(sizeof(BindStatusCallback));
446
447     ret->lpBindStatusCallbackVtbl = &BindStatusCallbackVtbl;
448     ret->lpServiceProviderVtbl    = &BSCServiceProviderVtbl;
449     ret->lpHttpNegotiate2Vtbl     = &BSCHttpNegotiateVtbl;
450     ret->lpAuthenticateVtbl       = &BSCAuthenticateVtbl;
451
452     ret->ref = 1;
453
454     IBindStatusCallback_AddRef(bsc);
455     ret->callback = bsc;
456
457     IBindStatusCallback_QueryInterface(bsc, &IID_IServiceProvider, (void**)&ret->serv_prov);
458
459     return STATUSCLB(ret);
460 }
461
462 /***********************************************************************
463  *           RegisterBindStatusCallback (urlmon.@)
464  *
465  * Register a bind status callback.
466  *
467  * PARAMS
468  *  pbc           [I] Binding context
469  *  pbsc          [I] Callback to register
470  *  ppbscPrevious [O] Destination for previous callback
471  *  dwReserved    [I] Reserved, must be 0.
472  *
473  * RETURNS
474  *    Success: S_OK.
475  *    Failure: E_INVALIDARG, if any argument is invalid, or
476  *             E_OUTOFMEMORY if memory allocation fails.
477  */
478 HRESULT WINAPI RegisterBindStatusCallback(IBindCtx *pbc, IBindStatusCallback *pbsc,
479         IBindStatusCallback **ppbscPrevious, DWORD dwReserved)
480 {
481     BindStatusCallback *holder;
482     IBindStatusCallback *bsc, *prev = NULL;
483     IUnknown *unk;
484     HRESULT hres;
485
486     TRACE("(%p %p %p %x)\n", pbc, pbsc, ppbscPrevious, dwReserved);
487
488     if (!pbc || !pbsc)
489         return E_INVALIDARG;
490
491     hres = IBindCtx_GetObjectParam(pbc, BSCBHolder, &unk);
492     if(SUCCEEDED(hres)) {
493         hres = IUnknown_QueryInterface(unk, &IID_IBindStatusCallback, (void**)&bsc);
494         if(SUCCEEDED(hres)) {
495             hres = IBindStatusCallback_QueryInterface(bsc, &IID_IBindStatusCallbackHolder, (void**)&holder);
496             if(SUCCEEDED(hres)) {
497                 prev = holder->callback;
498                 IBindStatusCallback_AddRef(prev);
499                 IBindStatusCallback_Release(bsc);
500                 IBindStatusCallback_Release(STATUSCLB(holder));
501             }else {
502                 prev = bsc;
503             }
504         }
505
506         IUnknown_Release(unk);
507         IBindCtx_RevokeObjectParam(pbc, BSCBHolder);
508     }
509
510     bsc = create_bsc(pbsc);
511     hres = IBindCtx_RegisterObjectParam(pbc, BSCBHolder, (IUnknown*)bsc);
512     IBindStatusCallback_Release(bsc);
513     if(FAILED(hres)) {
514         if(prev)
515             IBindStatusCallback_Release(prev);
516         return hres;
517     }
518
519     if(ppbscPrevious)
520         *ppbscPrevious = prev;
521     return S_OK;
522 }
523
524 /***********************************************************************
525  *           RevokeBindStatusCallback (URLMON.@)
526  *
527  * Unregister a bind status callback.
528  *
529  *  pbc           [I] Binding context
530  *  pbsc          [I] Callback to unregister
531  *
532  * RETURNS
533  *    Success: S_OK.
534  *    Failure: E_INVALIDARG, if any argument is invalid
535  */
536 HRESULT WINAPI RevokeBindStatusCallback(IBindCtx *pbc, IBindStatusCallback *pbsc)
537 {
538     BindStatusCallback *holder;
539     IBindStatusCallback *callback;
540     IUnknown *unk;
541     BOOL dorevoke = FALSE;
542     HRESULT hres;
543
544     TRACE("(%p %p)\n", pbc, pbsc);
545
546     if (!pbc || !pbsc)
547         return E_INVALIDARG;
548
549     hres = IBindCtx_GetObjectParam(pbc, BSCBHolder, &unk);
550     if(FAILED(hres))
551         return S_OK;
552
553     hres = IUnknown_QueryInterface(unk, &IID_IBindStatusCallback, (void**)&callback);
554     IUnknown_Release(unk);
555     if(FAILED(hres))
556         return S_OK;
557
558     hres = IBindStatusCallback_QueryInterface(callback, &IID_IBindStatusCallbackHolder, (void**)&holder);
559     if(SUCCEEDED(hres)) {
560         if(pbsc == holder->callback)
561             dorevoke = TRUE;
562         IBindStatusCallback_Release(STATUSCLB(holder));
563     }else if(pbsc == callback) {
564         dorevoke = TRUE;
565     }
566     IBindStatusCallback_Release(callback);
567
568     if(dorevoke)
569         IBindCtx_RevokeObjectParam(pbc, BSCBHolder);
570
571     return S_OK;
572 }
573
574 typedef struct {
575     const IBindCtxVtbl *lpBindCtxVtbl;
576
577     LONG ref;
578
579     IBindCtx *bindctx;
580 } AsyncBindCtx;
581
582 #define BINDCTX(x)  ((IBindCtx*)  &(x)->lpBindCtxVtbl)
583
584 #define BINDCTX_THIS(iface) DEFINE_THIS(AsyncBindCtx, BindCtx, iface)
585
586 static HRESULT WINAPI AsyncBindCtx_QueryInterface(IBindCtx *iface, REFIID riid, void **ppv)
587 {
588     AsyncBindCtx *This = BINDCTX_THIS(iface);
589
590     *ppv = NULL;
591
592     if(IsEqualGUID(riid, &IID_IUnknown)) {
593         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
594         *ppv = BINDCTX(This);
595     }else if(IsEqualGUID(riid, &IID_IBindCtx)) {
596         TRACE("(%p)->(IID_IBindCtx %p)\n", This, ppv);
597         *ppv = BINDCTX(This);
598     }else if(IsEqualGUID(riid, &IID_IAsyncBindCtx)) {
599         TRACE("(%p)->(IID_IAsyncBindCtx %p)\n", This, ppv);
600         *ppv = BINDCTX(This);
601     }
602
603     if(*ppv) {
604         IUnknown_AddRef((IUnknown*)*ppv);
605         return S_OK;
606     }
607
608     FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
609     return E_NOINTERFACE;
610 }
611
612 static ULONG WINAPI AsyncBindCtx_AddRef(IBindCtx *iface)
613 {
614     AsyncBindCtx *This = BINDCTX_THIS(iface);
615     LONG ref = InterlockedIncrement(&This->ref);
616
617     TRACE("(%p) ref=%d\n", This, ref);
618
619     return ref;
620 }
621
622 static ULONG WINAPI AsyncBindCtx_Release(IBindCtx *iface)
623 {
624     AsyncBindCtx *This = BINDCTX_THIS(iface);
625     LONG ref = InterlockedDecrement(&This->ref);
626
627     TRACE("(%p) ref=%d\n", This, ref);
628
629     if(!ref) {
630         IBindCtx_Release(This->bindctx);
631         heap_free(This);
632     }
633
634     return ref;
635 }
636
637 static HRESULT WINAPI AsyncBindCtx_RegisterObjectBound(IBindCtx *iface, IUnknown *punk)
638 {
639     AsyncBindCtx *This = BINDCTX_THIS(iface);
640
641     TRACE("(%p)->(%p)\n", This, punk);
642
643     return IBindCtx_RegisterObjectBound(This->bindctx, punk);
644 }
645
646 static HRESULT WINAPI AsyncBindCtx_RevokeObjectBound(IBindCtx *iface, IUnknown *punk)
647 {
648     AsyncBindCtx *This = BINDCTX_THIS(iface);
649
650     TRACE("(%p %p)\n", This, punk);
651
652     return IBindCtx_RevokeObjectBound(This->bindctx, punk);
653 }
654
655 static HRESULT WINAPI AsyncBindCtx_ReleaseBoundObjects(IBindCtx *iface)
656 {
657     AsyncBindCtx *This = BINDCTX_THIS(iface);
658
659     TRACE("(%p)\n", This);
660
661     return IBindCtx_ReleaseBoundObjects(This->bindctx);
662 }
663
664 static HRESULT WINAPI AsyncBindCtx_SetBindOptions(IBindCtx *iface, BIND_OPTS *pbindopts)
665 {
666     AsyncBindCtx *This = BINDCTX_THIS(iface);
667
668     TRACE("(%p)->(%p)\n", This, pbindopts);
669
670     return IBindCtx_SetBindOptions(This->bindctx, pbindopts);
671 }
672
673 static HRESULT WINAPI AsyncBindCtx_GetBindOptions(IBindCtx *iface, BIND_OPTS *pbindopts)
674 {
675     AsyncBindCtx *This = BINDCTX_THIS(iface);
676
677     TRACE("(%p)->(%p)\n", This, pbindopts);
678
679     return IBindCtx_GetBindOptions(This->bindctx, pbindopts);
680 }
681
682 static HRESULT WINAPI AsyncBindCtx_GetRunningObjectTable(IBindCtx *iface, IRunningObjectTable **pprot)
683 {
684     AsyncBindCtx *This = BINDCTX_THIS(iface);
685
686     TRACE("(%p)->(%p)\n", This, pprot);
687
688     return IBindCtx_GetRunningObjectTable(This->bindctx, pprot);
689 }
690
691 static HRESULT WINAPI AsyncBindCtx_RegisterObjectParam(IBindCtx *iface, LPOLESTR pszkey, IUnknown *punk)
692 {
693     AsyncBindCtx *This = BINDCTX_THIS(iface);
694
695     TRACE("(%p)->(%s %p)\n", This, debugstr_w(pszkey), punk);
696
697     return IBindCtx_RegisterObjectParam(This->bindctx, pszkey, punk);
698 }
699
700 static HRESULT WINAPI AsyncBindCtx_GetObjectParam(IBindCtx* iface, LPOLESTR pszkey, IUnknown **punk)
701 {
702     AsyncBindCtx *This = BINDCTX_THIS(iface);
703
704     TRACE("(%p)->(%s %p)\n", This, debugstr_w(pszkey), punk);
705
706     return IBindCtx_GetObjectParam(This->bindctx, pszkey, punk);
707 }
708
709 static HRESULT WINAPI AsyncBindCtx_RevokeObjectParam(IBindCtx *iface, LPOLESTR pszkey)
710 {
711     AsyncBindCtx *This = BINDCTX_THIS(iface);
712
713     TRACE("(%p)->(%s)\n", This, debugstr_w(pszkey));
714
715     return IBindCtx_RevokeObjectParam(This->bindctx, pszkey);
716 }
717
718 static HRESULT WINAPI AsyncBindCtx_EnumObjectParam(IBindCtx *iface, IEnumString **pszkey)
719 {
720     AsyncBindCtx *This = BINDCTX_THIS(iface);
721
722     TRACE("(%p)->(%p)\n", This, pszkey);
723
724     return IBindCtx_EnumObjectParam(This->bindctx, pszkey);
725 }
726
727 #undef BINDCTX_THIS
728
729 static const IBindCtxVtbl AsyncBindCtxVtbl =
730 {
731     AsyncBindCtx_QueryInterface,
732     AsyncBindCtx_AddRef,
733     AsyncBindCtx_Release,
734     AsyncBindCtx_RegisterObjectBound,
735     AsyncBindCtx_RevokeObjectBound,
736     AsyncBindCtx_ReleaseBoundObjects,
737     AsyncBindCtx_SetBindOptions,
738     AsyncBindCtx_GetBindOptions,
739     AsyncBindCtx_GetRunningObjectTable,
740     AsyncBindCtx_RegisterObjectParam,
741     AsyncBindCtx_GetObjectParam,
742     AsyncBindCtx_EnumObjectParam,
743     AsyncBindCtx_RevokeObjectParam
744 };
745
746 static HRESULT init_bindctx(IBindCtx *bindctx, DWORD options,
747        IBindStatusCallback *callback, IEnumFORMATETC *format)
748 {
749     BIND_OPTS bindopts;
750     HRESULT hres;
751
752     if(options)
753         FIXME("not supported options %08x\n", options);
754     if(format)
755         FIXME("format is not supported\n");
756
757     bindopts.cbStruct = sizeof(BIND_OPTS);
758     bindopts.grfFlags = BIND_MAYBOTHERUSER;
759     bindopts.grfMode = STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
760     bindopts.dwTickCountDeadline = 0;
761
762     hres = IBindCtx_SetBindOptions(bindctx, &bindopts);
763     if(FAILED(hres))
764        return hres;
765
766     if(callback) {
767         hres = RegisterBindStatusCallback(bindctx, callback, NULL, 0);
768         if(FAILED(hres))
769             return hres;
770     }
771
772     return S_OK;
773 }
774
775 /***********************************************************************
776  *           CreateAsyncBindCtx (urlmon.@)
777  */
778 HRESULT WINAPI CreateAsyncBindCtx(DWORD reserved, IBindStatusCallback *callback,
779         IEnumFORMATETC *format, IBindCtx **pbind)
780 {
781     IBindCtx *bindctx;
782     HRESULT hres;
783
784     TRACE("(%08x %p %p %p)\n", reserved, callback, format, pbind);
785
786     if(!pbind || !callback)
787         return E_INVALIDARG;
788
789     hres = CreateBindCtx(0, &bindctx);
790     if(FAILED(hres))
791         return hres;
792
793     hres = init_bindctx(bindctx, 0, callback, format);
794     if(FAILED(hres)) {
795         IBindCtx_Release(bindctx);
796         return hres;
797     }
798
799     *pbind = bindctx;
800     return S_OK;
801 }
802
803 /***********************************************************************
804  *           CreateAsyncBindCtxEx (urlmon.@)
805  *
806  * Create an asynchronous bind context.
807  */
808 HRESULT WINAPI CreateAsyncBindCtxEx(IBindCtx *ibind, DWORD options,
809         IBindStatusCallback *callback, IEnumFORMATETC *format, IBindCtx** pbind,
810         DWORD reserved)
811 {
812     AsyncBindCtx *ret;
813     IBindCtx *bindctx;
814     HRESULT hres;
815
816     TRACE("(%p %08x %p %p %p %d)\n", ibind, options, callback, format, pbind, reserved);
817
818     if(!pbind)
819         return E_INVALIDARG;
820
821     if(reserved)
822         WARN("reserved=%d\n", reserved);
823
824     if(ibind) {
825         IBindCtx_AddRef(ibind);
826         bindctx = ibind;
827     }else {
828         hres = CreateBindCtx(0, &bindctx);
829         if(FAILED(hres))
830             return hres;
831     }
832
833     ret = heap_alloc(sizeof(AsyncBindCtx));
834
835     ret->lpBindCtxVtbl = &AsyncBindCtxVtbl;
836     ret->ref = 1;
837     ret->bindctx = bindctx;
838
839     hres = init_bindctx(BINDCTX(ret), options, callback, format);
840     if(FAILED(hres)) {
841         IBindCtx_Release(BINDCTX(ret));
842         return hres;
843     }
844
845     *pbind = BINDCTX(ret);
846     return S_OK;
847 }