2 * Copyright 2005-2007 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"
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(urlmon
);
26 static WCHAR cbinding_contextW
[] = {'C','B','i','n','d','i','n','g',' ','C','o','n','t','e','x','t',0};
27 static WCHAR bscb_holderW
[] = { '_','B','S','C','B','_','H','o','l','d','e','r','_',0 };
29 typedef struct Binding Binding
;
31 struct _task_header_t
;
33 typedef void (*task_proc_t
)(Binding
*, struct _task_header_t
*);
35 typedef struct _task_header_t
{
37 struct _task_header_t
*next
;
41 const IUnknownVtbl
*lpUnknownVtbl
;
45 IInternetProtocol
*protocol
;
53 #define STGMEDUNK(x) ((IUnknown*) &(x)->lpUnknownVtbl)
56 const IStreamVtbl
*lpStreamVtbl
;
69 #define BINDING_LOCKED 0x0001
70 #define BINDING_STOPPED 0x0002
71 #define BINDING_OBJAVAIL 0x0004
74 const IBindingVtbl
*lpBindingVtbl
;
75 const IInternetProtocolSinkVtbl
*lpInternetProtocolSinkVtbl
;
76 const IInternetBindInfoVtbl
*lpInternetBindInfoVtbl
;
77 const IServiceProviderVtbl
*lpServiceProviderVtbl
;
81 IBindStatusCallback
*callback
;
82 IInternetProtocol
*protocol
;
83 IServiceProvider
*service_provider
;
85 stgmed_buf_t
*stgmed_buf
;
86 ProtocolStream
*stream
;
92 UINT clipboard_format
;
99 download_state_t download_state
;
104 DWORD apartment_thread
;
109 task_header_t
*task_queue_head
, *task_queue_tail
;
110 CRITICAL_SECTION section
;
113 #define BINDING(x) ((IBinding*) &(x)->lpBindingVtbl)
114 #define PROTSINK(x) ((IInternetProtocolSink*) &(x)->lpInternetProtocolSinkVtbl)
115 #define BINDINF(x) ((IInternetBindInfo*) &(x)->lpInternetBindInfoVtbl)
116 #define SERVPROV(x) ((IServiceProvider*) &(x)->lpServiceProviderVtbl)
118 #define STREAM(x) ((IStream*) &(x)->lpStreamVtbl)
119 #define HTTPNEG2(x) ((IHttpNegotiate2*) &(x)->lpHttpNegotiate2Vtbl)
121 #define WM_MK_CONTINUE (WM_USER+101)
123 static void push_task(Binding
*binding
, task_header_t
*task
, task_proc_t proc
)
128 EnterCriticalSection(&binding
->section
);
130 if(binding
->task_queue_tail
) {
131 binding
->task_queue_tail
->next
= task
;
132 binding
->task_queue_tail
= task
;
134 binding
->task_queue_tail
= binding
->task_queue_head
= task
;
137 LeaveCriticalSection(&binding
->section
);
140 static task_header_t
*pop_task(Binding
*binding
)
144 EnterCriticalSection(&binding
->section
);
146 ret
= binding
->task_queue_head
;
148 binding
->task_queue_head
= ret
->next
;
149 if(!binding
->task_queue_head
)
150 binding
->task_queue_tail
= NULL
;
153 LeaveCriticalSection(&binding
->section
);
158 static void fill_stgmed_buffer(stgmed_buf_t
*buf
)
162 if(sizeof(buf
->buf
) == buf
->size
)
165 buf
->hres
= IInternetProtocol_Read(buf
->protocol
, buf
->buf
+buf
->size
,
166 sizeof(buf
->buf
)-buf
->size
, &read
);
172 static LRESULT WINAPI
notif_wnd_proc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
174 if(msg
== WM_MK_CONTINUE
) {
175 Binding
*binding
= (Binding
*)lParam
;
178 while((task
= pop_task(binding
))) {
179 binding
->continue_call
++;
180 task
->proc(binding
, task
);
181 binding
->continue_call
--;
184 IBinding_Release(BINDING(binding
));
188 return DefWindowProcW(hwnd
, msg
, wParam
, lParam
);
191 static HWND
get_notif_hwnd(void)
193 static ATOM wnd_class
= 0;
196 static const WCHAR wszURLMonikerNotificationWindow
[] =
197 {'U','R','L',' ','M','o','n','i','k','e','r',' ',
198 'N','o','t','i','f','i','c','a','t','i','o','n',' ','W','i','n','d','o','w',0};
201 static WNDCLASSEXW wndclass
= {
203 notif_wnd_proc
, 0, 0,
204 NULL
, NULL
, NULL
, NULL
, NULL
,
205 wszURLMonikerNotificationWindow
,
209 wndclass
.hInstance
= URLMON_hInstance
;
211 wnd_class
= RegisterClassExW(&wndclass
);
212 if (!wnd_class
&& GetLastError() == ERROR_CLASS_ALREADY_EXISTS
)
216 hwnd
= CreateWindowExW(0, wszURLMonikerNotificationWindow
,
217 wszURLMonikerNotificationWindow
, 0, 0, 0, 0, 0, HWND_MESSAGE
,
218 NULL
, URLMON_hInstance
, NULL
);
220 TRACE("hwnd = %p\n", hwnd
);
225 static void dump_BINDINFO(BINDINFO
*bi
)
227 static const char * const BINDINFOF_str
[] = {
229 "BINDINFOF_URLENCODESTGMEDDATA",
230 "BINDINFOF_URLENCODEDEXTRAINFO"
233 static const char * const BINDVERB_str
[] = {
247 " %d, %08x, %d, %d\n"
253 bi
->cbSize
, debugstr_w(bi
->szExtraInfo
),
254 bi
->stgmedData
.tymed
, bi
->stgmedData
.u
.hGlobal
, bi
->stgmedData
.pUnkForRelease
,
255 bi
->grfBindInfoF
> BINDINFOF_URLENCODEDEXTRAINFO
256 ? "unknown" : BINDINFOF_str
[bi
->grfBindInfoF
],
257 bi
->dwBindVerb
> BINDVERB_CUSTOM
258 ? "unknown" : BINDVERB_str
[bi
->dwBindVerb
],
259 debugstr_w(bi
->szCustomVerb
),
260 bi
->cbstgmedData
, bi
->dwOptions
, bi
->dwOptionsFlags
, bi
->dwCodePage
,
261 bi
->securityAttributes
.nLength
,
262 bi
->securityAttributes
.lpSecurityDescriptor
,
263 bi
->securityAttributes
.bInheritHandle
,
264 debugstr_guid(&bi
->iid
),
265 bi
->pUnk
, bi
->dwReserved
269 static void set_binding_mime(Binding
*binding
, LPCWSTR mime
)
271 EnterCriticalSection(&binding
->section
);
273 if(binding
->report_mime
) {
274 heap_free(binding
->mime
);
275 binding
->mime
= heap_strdupW(mime
);
278 LeaveCriticalSection(&binding
->section
);
281 static void handle_mime_available(Binding
*binding
, BOOL verify
)
285 EnterCriticalSection(&binding
->section
);
286 report_mime
= binding
->report_mime
;
287 binding
->report_mime
= FALSE
;
288 LeaveCriticalSection(&binding
->section
);
296 fill_stgmed_buffer(binding
->stgmed_buf
);
297 FindMimeFromData(NULL
, binding
->url
, binding
->stgmed_buf
->buf
,
298 min(binding
->stgmed_buf
->size
, 255), binding
->mime
, 0, &mime
, 0);
300 heap_free(binding
->mime
);
301 binding
->mime
= heap_strdupW(mime
);
305 IBindStatusCallback_OnProgress(binding
->callback
, 0, 0, BINDSTATUS_MIMETYPEAVAILABLE
, binding
->mime
);
307 binding
->clipboard_format
= RegisterClipboardFormatW(binding
->mime
);
311 task_header_t header
;
313 } mime_available_task_t
;
315 static void mime_available_proc(Binding
*binding
, task_header_t
*t
)
317 mime_available_task_t
*task
= (mime_available_task_t
*)t
;
319 handle_mime_available(binding
, task
->verify
);
324 static void mime_available(Binding
*This
, LPCWSTR mime
, BOOL verify
)
327 set_binding_mime(This
, mime
);
329 if(GetCurrentThreadId() == This
->apartment_thread
) {
330 handle_mime_available(This
, verify
);
332 mime_available_task_t
*task
= heap_alloc(sizeof(task_header_t
));
333 task
->verify
= verify
;
334 push_task(This
, &task
->header
, mime_available_proc
);
338 static void stop_binding(Binding
*binding
, HRESULT hres
, LPCWSTR str
)
340 if(binding
->state
& BINDING_LOCKED
) {
341 IInternetProtocol_UnlockRequest(binding
->protocol
);
342 binding
->state
&= ~BINDING_LOCKED
;
345 if(!(binding
->state
& BINDING_STOPPED
)) {
346 binding
->state
|= BINDING_STOPPED
;
348 IBindStatusCallback_OnStopBinding(binding
->callback
, hres
, str
);
349 binding
->hres
= hres
;
353 static LPWSTR
get_mime_clsid(LPCWSTR mime
, CLSID
*clsid
)
355 LPWSTR key_name
, ret
;
356 DWORD res
, type
, size
;
361 static const WCHAR mime_keyW
[] =
362 {'M','I','M','E','\\','D','a','t','a','b','a','s','e','\\',
363 'C','o','n','t','e','n','t',' ','T','y','p','e','\\'};
364 static const WCHAR clsidW
[] = {'C','L','S','I','D',0};
366 len
= strlenW(mime
)+1;
367 key_name
= heap_alloc(sizeof(mime_keyW
) + len
*sizeof(WCHAR
));
368 memcpy(key_name
, mime_keyW
, sizeof(mime_keyW
));
369 strcpyW(key_name
+ sizeof(mime_keyW
)/sizeof(WCHAR
), mime
);
371 res
= RegOpenKeyW(HKEY_CLASSES_ROOT
, key_name
, &hkey
);
373 if(res
!= ERROR_SUCCESS
) {
374 WARN("Could not open MIME key: %x\n", res
);
378 size
= 50*sizeof(WCHAR
);
379 ret
= heap_alloc(size
);
380 res
= RegQueryValueExW(hkey
, clsidW
, NULL
, &type
, (LPBYTE
)ret
, &size
);
382 if(res
!= ERROR_SUCCESS
) {
383 WARN("Could not get CLSID: %08x\n", res
);
388 hres
= CLSIDFromString(ret
, clsid
);
390 WARN("Could not parse CLSID: %08x\n", hres
);
398 static void load_doc_mon(Binding
*binding
, IPersistMoniker
*persist
)
403 hres
= CreateAsyncBindCtxEx(binding
->bctx
, 0, NULL
, NULL
, &bctx
, 0);
405 WARN("CreateAsyncBindCtxEx failed: %08x\n", hres
);
409 IBindCtx_RevokeObjectParam(bctx
, bscb_holderW
);
410 IBindCtx_RegisterObjectParam(bctx
, cbinding_contextW
, (IUnknown
*)BINDING(binding
));
412 hres
= IPersistMoniker_Load(persist
, binding
->download_state
== END_DOWNLOAD
, binding
->mon
, bctx
, 0x12);
413 IBindCtx_RevokeObjectParam(bctx
, cbinding_contextW
);
414 IBindCtx_Release(bctx
);
416 FIXME("Load failed: %08x\n", hres
);
419 static void create_object(Binding
*binding
)
421 IPersistMoniker
*persist
;
427 FIXME("MIME unavailable\n");
431 if(!(clsid_str
= get_mime_clsid(binding
->mime
, &clsid
))) {
432 FIXME("Could not find object for MIME %s\n", debugstr_w(binding
->mime
));
436 IBindStatusCallback_OnProgress(binding
->callback
, 0, 0, BINDSTATUS_CLASSIDAVAILABLE
, clsid_str
);
438 IBindStatusCallback_OnProgress(binding
->callback
, 0, 0, BINDSTATUS_BEGINSYNCOPERATION
, NULL
);
440 hres
= CoCreateInstance(&clsid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
441 &binding
->iid
, (void**)&binding
->obj
);
443 FIXME("CoCreateInstance failed: %08x\n", hres
);
445 binding
->state
|= BINDING_OBJAVAIL
;
447 hres
= IUnknown_QueryInterface(binding
->obj
, &IID_IPersistMoniker
, (void**)&persist
);
448 if(SUCCEEDED(hres
)) {
451 hres
= IPersistMoniker_QueryInterface(persist
, &IID_IMonikerProp
, (void**)&prop
);
452 if(SUCCEEDED(hres
)) {
453 IMonikerProp_PutProperty(prop
, MIMETYPEPROP
, binding
->mime
);
454 IMonikerProp_PutProperty(prop
, CLASSIDPROP
, clsid_str
);
455 IMonikerProp_Release(prop
);
458 load_doc_mon(binding
, persist
);
460 IPersistMoniker_Release(persist
);
462 FIXME("Could not get IPersistMoniker: %08x\n", hres
);
463 /* FIXME: Try query IPersistFile */
466 heap_free(clsid_str
);
468 IBindStatusCallback_OnObjectAvailable(binding
->callback
, &binding
->iid
, binding
->obj
);
470 IBindStatusCallback_OnProgress(binding
->callback
, 0, 0, BINDSTATUS_ENDSYNCOPERATION
, NULL
);
472 stop_binding(binding
, S_OK
, NULL
);
475 #define STGMEDUNK_THIS(iface) DEFINE_THIS(stgmed_buf_t, Unknown, iface)
477 static HRESULT WINAPI
StgMedUnk_QueryInterface(IUnknown
*iface
, REFIID riid
, void **ppv
)
479 stgmed_buf_t
*This
= STGMEDUNK_THIS(iface
);
483 if(IsEqualGUID(riid
, &IID_IUnknown
)) {
484 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
486 *ppv
= STGMEDUNK(This
);
487 IUnknown_AddRef(STGMEDUNK(This
));
491 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
492 return E_NOINTERFACE
;
495 static ULONG WINAPI
StgMedUnk_AddRef(IUnknown
*iface
)
497 stgmed_buf_t
*This
= STGMEDUNK_THIS(iface
);
498 LONG ref
= InterlockedIncrement(&This
->ref
);
500 TRACE("(%p) ref=%d\n", This
, ref
);
505 static ULONG WINAPI
StgMedUnk_Release(IUnknown
*iface
)
507 stgmed_buf_t
*This
= STGMEDUNK_THIS(iface
);
508 LONG ref
= InterlockedDecrement(&This
->ref
);
510 TRACE("(%p) ref=%d\n", This
, ref
);
513 IInternetProtocol_Release(This
->protocol
);
516 URLMON_UnlockModule();
522 #undef STGMEDUNK_THIS
524 static const IUnknownVtbl StgMedUnkVtbl
= {
525 StgMedUnk_QueryInterface
,
530 #define STREAM_THIS(iface) DEFINE_THIS(ProtocolStream, Stream, iface)
532 static HRESULT WINAPI
ProtocolStream_QueryInterface(IStream
*iface
,
533 REFIID riid
, void **ppv
)
535 ProtocolStream
*This
= STREAM_THIS(iface
);
539 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
540 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
542 }else if(IsEqualGUID(&IID_ISequentialStream
, riid
)) {
543 TRACE("(%p)->(IID_ISequentialStream %p)\n", This
, ppv
);
545 }else if(IsEqualGUID(&IID_IStream
, riid
)) {
546 TRACE("(%p)->(IID_IStream %p)\n", This
, ppv
);
551 IStream_AddRef(STREAM(This
));
555 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
556 return E_NOINTERFACE
;
559 static ULONG WINAPI
ProtocolStream_AddRef(IStream
*iface
)
561 ProtocolStream
*This
= STREAM_THIS(iface
);
562 LONG ref
= InterlockedIncrement(&This
->ref
);
564 TRACE("(%p) ref=%d\n", This
, ref
);
569 static ULONG WINAPI
ProtocolStream_Release(IStream
*iface
)
571 ProtocolStream
*This
= STREAM_THIS(iface
);
572 LONG ref
= InterlockedDecrement(&This
->ref
);
574 TRACE("(%p) ref=%d\n", This
, ref
);
577 IUnknown_Release(STGMEDUNK(This
->buf
));
580 URLMON_UnlockModule();
586 static HRESULT WINAPI
ProtocolStream_Read(IStream
*iface
, void *pv
,
587 ULONG cb
, ULONG
*pcbRead
)
589 ProtocolStream
*This
= STREAM_THIS(iface
);
590 DWORD read
= 0, pread
= 0;
593 TRACE("(%p)->(%p %d %p)\n", This
, pv
, cb
, pcbRead
);
595 if(This
->buf
->size
) {
598 if(read
> This
->buf
->size
)
599 read
= This
->buf
->size
;
601 memcpy(pv
, This
->buf
->buf
, read
);
603 if(read
< This
->buf
->size
)
604 memmove(This
->buf
->buf
, This
->buf
->buf
+read
, This
->buf
->size
-read
);
605 This
->buf
->size
-= read
;
614 hres
= This
->buf
->hres
= IInternetProtocol_Read(This
->buf
->protocol
, (PBYTE
)pv
+read
, cb
-read
, &pread
);
616 *pcbRead
= read
+ pread
;
618 if(hres
== E_PENDING
)
620 else if(FAILED(hres
))
621 FIXME("Read failed: %08x\n", hres
);
623 return read
|| pread
? S_OK
: S_FALSE
;
626 static HRESULT WINAPI
ProtocolStream_Write(IStream
*iface
, const void *pv
,
627 ULONG cb
, ULONG
*pcbWritten
)
629 ProtocolStream
*This
= STREAM_THIS(iface
);
631 TRACE("(%p)->(%p %d %p)\n", This
, pv
, cb
, pcbWritten
);
633 return STG_E_ACCESSDENIED
;
636 static HRESULT WINAPI
ProtocolStream_Seek(IStream
*iface
, LARGE_INTEGER dlibMove
,
637 DWORD dwOrigin
, ULARGE_INTEGER
*plibNewPosition
)
639 ProtocolStream
*This
= STREAM_THIS(iface
);
640 FIXME("(%p)->(%d %08x %p)\n", This
, dlibMove
.u
.LowPart
, dwOrigin
, plibNewPosition
);
644 static HRESULT WINAPI
ProtocolStream_SetSize(IStream
*iface
, ULARGE_INTEGER libNewSize
)
646 ProtocolStream
*This
= STREAM_THIS(iface
);
647 FIXME("(%p)->(%d)\n", This
, libNewSize
.u
.LowPart
);
651 static HRESULT WINAPI
ProtocolStream_CopyTo(IStream
*iface
, IStream
*pstm
,
652 ULARGE_INTEGER cb
, ULARGE_INTEGER
*pcbRead
, ULARGE_INTEGER
*pcbWritten
)
654 ProtocolStream
*This
= STREAM_THIS(iface
);
655 FIXME("(%p)->(%p %d %p %p)\n", This
, pstm
, cb
.u
.LowPart
, pcbRead
, pcbWritten
);
659 static HRESULT WINAPI
ProtocolStream_Commit(IStream
*iface
, DWORD grfCommitFlags
)
661 ProtocolStream
*This
= STREAM_THIS(iface
);
663 TRACE("(%p)->(%08x)\n", This
, grfCommitFlags
);
668 static HRESULT WINAPI
ProtocolStream_Revert(IStream
*iface
)
670 ProtocolStream
*This
= STREAM_THIS(iface
);
672 TRACE("(%p)\n", This
);
677 static HRESULT WINAPI
ProtocolStream_LockRegion(IStream
*iface
, ULARGE_INTEGER libOffset
,
678 ULARGE_INTEGER cb
, DWORD dwLockType
)
680 ProtocolStream
*This
= STREAM_THIS(iface
);
681 FIXME("(%p)->(%d %d %d)\n", This
, libOffset
.u
.LowPart
, cb
.u
.LowPart
, dwLockType
);
685 static HRESULT WINAPI
ProtocolStream_UnlockRegion(IStream
*iface
,
686 ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
)
688 ProtocolStream
*This
= STREAM_THIS(iface
);
689 FIXME("(%p)->(%d %d %d)\n", This
, libOffset
.u
.LowPart
, cb
.u
.LowPart
, dwLockType
);
693 static HRESULT WINAPI
ProtocolStream_Stat(IStream
*iface
, STATSTG
*pstatstg
,
696 ProtocolStream
*This
= STREAM_THIS(iface
);
697 FIXME("(%p)->(%p %08x)\n", This
, pstatstg
, dwStatFlag
);
701 static HRESULT WINAPI
ProtocolStream_Clone(IStream
*iface
, IStream
**ppstm
)
703 ProtocolStream
*This
= STREAM_THIS(iface
);
704 FIXME("(%p)->(%p)\n", This
, ppstm
);
710 static const IStreamVtbl ProtocolStreamVtbl
= {
711 ProtocolStream_QueryInterface
,
712 ProtocolStream_AddRef
,
713 ProtocolStream_Release
,
715 ProtocolStream_Write
,
717 ProtocolStream_SetSize
,
718 ProtocolStream_CopyTo
,
719 ProtocolStream_Commit
,
720 ProtocolStream_Revert
,
721 ProtocolStream_LockRegion
,
722 ProtocolStream_UnlockRegion
,
727 #define BINDING_THIS(iface) DEFINE_THIS(Binding, Binding, iface)
729 static stgmed_buf_t
*create_stgmed_buf(IInternetProtocol
*protocol
)
731 stgmed_buf_t
*ret
= heap_alloc(sizeof(*ret
));
733 ret
->lpUnknownVtbl
= &StgMedUnkVtbl
;
739 IInternetProtocol_AddRef(protocol
);
740 ret
->protocol
= protocol
;
747 static ProtocolStream
*create_stream(stgmed_buf_t
*buf
)
749 ProtocolStream
*ret
= heap_alloc(sizeof(ProtocolStream
));
751 ret
->lpStreamVtbl
= &ProtocolStreamVtbl
;
754 IUnknown_AddRef(STGMEDUNK(buf
));
762 static HRESULT WINAPI
Binding_QueryInterface(IBinding
*iface
, REFIID riid
, void **ppv
)
764 Binding
*This
= BINDING_THIS(iface
);
768 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
769 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
770 *ppv
= BINDING(This
);
771 }else if(IsEqualGUID(&IID_IBinding
, riid
)) {
772 TRACE("(%p)->(IID_IBinding %p)\n", This
, ppv
);
773 *ppv
= BINDING(This
);
774 }else if(IsEqualGUID(&IID_IInternetProtocolSink
, riid
)) {
775 TRACE("(%p)->(IID_IInternetProtocolSink %p)\n", This
, ppv
);
776 *ppv
= PROTSINK(This
);
777 }else if(IsEqualGUID(&IID_IInternetBindInfo
, riid
)) {
778 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This
, ppv
);
779 *ppv
= BINDINF(This
);
780 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
781 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
782 *ppv
= SERVPROV(This
);
786 IBinding_AddRef(BINDING(This
));
790 WARN("Unsupported interface %s\n", debugstr_guid(riid
));
791 return E_NOINTERFACE
;
794 static ULONG WINAPI
Binding_AddRef(IBinding
*iface
)
796 Binding
*This
= BINDING_THIS(iface
);
797 LONG ref
= InterlockedIncrement(&This
->ref
);
799 TRACE("(%p) ref=%d\n", This
, ref
);
804 static ULONG WINAPI
Binding_Release(IBinding
*iface
)
806 Binding
*This
= BINDING_THIS(iface
);
807 LONG ref
= InterlockedDecrement(&This
->ref
);
809 TRACE("(%p) ref=%d\n", This
, ref
);
812 if (This
->notif_hwnd
)
813 DestroyWindow( This
->notif_hwnd
);
815 IMoniker_Release(This
->mon
);
817 IBindStatusCallback_Release(This
->callback
);
819 IInternetProtocol_Release(This
->protocol
);
820 if(This
->service_provider
)
821 IServiceProvider_Release(This
->service_provider
);
823 IUnknown_Release(STGMEDUNK(This
->stgmed_buf
));
825 IStream_Release(STREAM(This
->stream
));
827 IUnknown_Release(This
->obj
);
829 IBindCtx_Release(This
->bctx
);
831 ReleaseBindInfo(&This
->bindinfo
);
832 This
->section
.DebugInfo
->Spare
[0] = 0;
833 DeleteCriticalSection(&This
->section
);
834 heap_free(This
->mime
);
835 heap_free(This
->url
);
839 URLMON_UnlockModule();
845 static HRESULT WINAPI
Binding_Abort(IBinding
*iface
)
847 Binding
*This
= BINDING_THIS(iface
);
848 FIXME("(%p)\n", This
);
852 static HRESULT WINAPI
Binding_Suspend(IBinding
*iface
)
854 Binding
*This
= BINDING_THIS(iface
);
855 FIXME("(%p)\n", This
);
859 static HRESULT WINAPI
Binding_Resume(IBinding
*iface
)
861 Binding
*This
= BINDING_THIS(iface
);
862 FIXME("(%p)\n", This
);
866 static HRESULT WINAPI
Binding_SetPriority(IBinding
*iface
, LONG nPriority
)
868 Binding
*This
= BINDING_THIS(iface
);
869 FIXME("(%p)->(%d)\n", This
, nPriority
);
873 static HRESULT WINAPI
Binding_GetPriority(IBinding
*iface
, LONG
*pnPriority
)
875 Binding
*This
= BINDING_THIS(iface
);
876 FIXME("(%p)->(%p)\n", This
, pnPriority
);
880 static HRESULT WINAPI
Binding_GetBindResult(IBinding
*iface
, CLSID
*pclsidProtocol
,
881 DWORD
*pdwResult
, LPOLESTR
*pszResult
, DWORD
*pdwReserved
)
883 Binding
*This
= BINDING_THIS(iface
);
884 FIXME("(%p)->(%p %p %p %p)\n", This
, pclsidProtocol
, pdwResult
, pszResult
, pdwReserved
);
888 static Binding
*get_bctx_binding(IBindCtx
*bctx
)
894 hres
= IBindCtx_GetObjectParam(bctx
, cbinding_contextW
, &unk
);
898 hres
= IUnknown_QueryInterface(unk
, &IID_IBinding
, (void*)&binding
);
899 IUnknown_Release(unk
);
904 return BINDING_THIS(binding
);
909 static const IBindingVtbl BindingVtbl
= {
910 Binding_QueryInterface
,
918 Binding_GetBindResult
921 #define PROTSINK_THIS(iface) DEFINE_THIS(Binding, InternetProtocolSink, iface)
923 static HRESULT WINAPI
InternetProtocolSink_QueryInterface(IInternetProtocolSink
*iface
,
924 REFIID riid
, void **ppv
)
926 Binding
*This
= PROTSINK_THIS(iface
);
927 return IBinding_QueryInterface(BINDING(This
), riid
, ppv
);
930 static ULONG WINAPI
InternetProtocolSink_AddRef(IInternetProtocolSink
*iface
)
932 Binding
*This
= PROTSINK_THIS(iface
);
933 return IBinding_AddRef(BINDING(This
));
936 static ULONG WINAPI
InternetProtocolSink_Release(IInternetProtocolSink
*iface
)
938 Binding
*This
= PROTSINK_THIS(iface
);
939 return IBinding_Release(BINDING(This
));
943 task_header_t header
;
947 static void switch_proc(Binding
*binding
, task_header_t
*t
)
949 switch_task_t
*task
= (switch_task_t
*)t
;
951 IInternetProtocol_Continue(binding
->protocol
, &task
->data
);
956 static HRESULT WINAPI
InternetProtocolSink_Switch(IInternetProtocolSink
*iface
,
957 PROTOCOLDATA
*pProtocolData
)
959 Binding
*This
= PROTSINK_THIS(iface
);
962 TRACE("(%p)->(%p)\n", This
, pProtocolData
);
964 task
= heap_alloc(sizeof(switch_task_t
));
965 memcpy(&task
->data
, pProtocolData
, sizeof(PROTOCOLDATA
));
967 push_task(This
, &task
->header
, switch_proc
);
969 IBinding_AddRef(BINDING(This
));
970 PostMessageW(This
->notif_hwnd
, WM_MK_CONTINUE
, 0, (LPARAM
)This
);
976 task_header_t header
;
983 } on_progress_task_t
;
985 static void on_progress_proc(Binding
*binding
, task_header_t
*t
)
987 on_progress_task_t
*task
= (on_progress_task_t
*)t
;
989 IBindStatusCallback_OnProgress(binding
->callback
, task
->progress
,
990 task
->progress_max
, task
->status_code
, task
->status_text
);
992 heap_free(task
->status_text
);
996 static void on_progress(Binding
*This
, ULONG progress
, ULONG progress_max
,
997 ULONG status_code
, LPCWSTR status_text
)
999 on_progress_task_t
*task
;
1001 if(GetCurrentThreadId() == This
->apartment_thread
&& !This
->continue_call
) {
1002 IBindStatusCallback_OnProgress(This
->callback
, progress
, progress_max
,
1003 status_code
, status_text
);
1007 task
= heap_alloc(sizeof(on_progress_task_t
));
1009 task
->progress
= progress
;
1010 task
->progress_max
= progress_max
;
1011 task
->status_code
= status_code
;
1014 DWORD size
= (strlenW(status_text
)+1)*sizeof(WCHAR
);
1016 task
->status_text
= heap_alloc(size
);
1017 memcpy(task
->status_text
, status_text
, size
);
1019 task
->status_text
= NULL
;
1022 push_task(This
, &task
->header
, on_progress_proc
);
1024 if(GetCurrentThreadId() != This
->apartment_thread
) {
1025 IBinding_AddRef(BINDING(This
));
1026 PostMessageW(This
->notif_hwnd
, WM_MK_CONTINUE
, 0, (LPARAM
)This
);
1030 static HRESULT WINAPI
InternetProtocolSink_ReportProgress(IInternetProtocolSink
*iface
,
1031 ULONG ulStatusCode
, LPCWSTR szStatusText
)
1033 Binding
*This
= PROTSINK_THIS(iface
);
1035 TRACE("(%p)->(%u %s)\n", This
, ulStatusCode
, debugstr_w(szStatusText
));
1037 switch(ulStatusCode
) {
1038 case BINDSTATUS_FINDINGRESOURCE
:
1039 on_progress(This
, 0, 0, BINDSTATUS_FINDINGRESOURCE
, szStatusText
);
1041 case BINDSTATUS_CONNECTING
:
1042 on_progress(This
, 0, 0, BINDSTATUS_CONNECTING
, szStatusText
);
1044 case BINDSTATUS_BEGINDOWNLOADDATA
:
1045 fill_stgmed_buffer(This
->stgmed_buf
);
1047 case BINDSTATUS_MIMETYPEAVAILABLE
:
1048 set_binding_mime(This
, szStatusText
);
1050 case BINDSTATUS_SENDINGREQUEST
:
1051 on_progress(This
, 0, 0, BINDSTATUS_SENDINGREQUEST
, szStatusText
);
1053 case BINDSTATUS_PROTOCOLCLASSID
:
1055 case BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE
:
1056 mime_available(This
, szStatusText
, FALSE
);
1058 case BINDSTATUS_CACHEFILENAMEAVAILABLE
:
1060 case BINDSTATUS_DIRECTBIND
:
1061 This
->report_mime
= FALSE
;
1064 FIXME("Unhandled status code %d\n", ulStatusCode
);
1071 static void report_data(Binding
*This
, DWORD bscf
, ULONG progress
, ULONG progress_max
)
1073 FORMATETC formatetc
= {0, NULL
, 1, -1, TYMED_ISTREAM
};
1074 BOOL sent_begindownloaddata
= FALSE
;
1076 TRACE("(%p)->(%d %u %u)\n", This
, bscf
, progress
, progress_max
);
1078 if(This
->download_state
== END_DOWNLOAD
)
1081 if(GetCurrentThreadId() != This
->apartment_thread
)
1082 FIXME("called from worker thread\n");
1084 if(This
->report_mime
)
1085 mime_available(This
, NULL
, TRUE
);
1087 if(This
->download_state
== BEFORE_DOWNLOAD
) {
1088 fill_stgmed_buffer(This
->stgmed_buf
);
1090 This
->download_state
= DOWNLOADING
;
1091 sent_begindownloaddata
= TRUE
;
1092 IBindStatusCallback_OnProgress(This
->callback
, progress
, progress_max
,
1093 BINDSTATUS_BEGINDOWNLOADDATA
, This
->url
);
1096 if(This
->stgmed_buf
->hres
== S_FALSE
|| (bscf
& BSCF_LASTDATANOTIFICATION
)) {
1097 This
->download_state
= END_DOWNLOAD
;
1098 IBindStatusCallback_OnProgress(This
->callback
, progress
, progress_max
,
1099 BINDSTATUS_ENDDOWNLOADDATA
, This
->url
);
1100 }else if(!sent_begindownloaddata
) {
1101 IBindStatusCallback_OnProgress(This
->callback
, progress
, progress_max
,
1102 BINDSTATUS_DOWNLOADINGDATA
, This
->url
);
1105 if(This
->to_object
) {
1106 if(!(This
->state
& BINDING_OBJAVAIL
))
1107 create_object(This
);
1109 if(!(This
->state
& BINDING_LOCKED
)) {
1110 HRESULT hres
= IInternetProtocol_LockRequest(This
->protocol
, 0);
1112 This
->state
|= BINDING_LOCKED
;
1115 formatetc
.cfFormat
= This
->clipboard_format
;
1116 IBindStatusCallback_OnDataAvailable(This
->callback
, bscf
, progress
,
1117 &formatetc
, &This
->stgmed
);
1119 if(This
->download_state
== END_DOWNLOAD
)
1120 stop_binding(This
, S_OK
, NULL
);
1125 task_header_t header
;
1129 } report_data_task_t
;
1131 static void report_data_proc(Binding
*binding
, task_header_t
*t
)
1133 report_data_task_t
*task
= (report_data_task_t
*)t
;
1135 report_data(binding
, task
->bscf
, task
->progress
, task
->progress_max
);
1140 static HRESULT WINAPI
InternetProtocolSink_ReportData(IInternetProtocolSink
*iface
,
1141 DWORD grfBSCF
, ULONG ulProgress
, ULONG ulProgressMax
)
1143 Binding
*This
= PROTSINK_THIS(iface
);
1145 TRACE("(%p)->(%d %u %u)\n", This
, grfBSCF
, ulProgress
, ulProgressMax
);
1147 if(GetCurrentThreadId() != This
->apartment_thread
)
1148 FIXME("called from worked hread\n");
1150 if(This
->continue_call
) {
1151 report_data_task_t
*task
= heap_alloc(sizeof(report_data_task_t
));
1152 task
->bscf
= grfBSCF
;
1153 task
->progress
= ulProgress
;
1154 task
->progress_max
= ulProgressMax
;
1156 push_task(This
, &task
->header
, report_data_proc
);
1158 report_data(This
, grfBSCF
, ulProgress
, ulProgressMax
);
1165 task_header_t header
;
1169 } report_result_task_t
;
1171 static void report_result_proc(Binding
*binding
, task_header_t
*t
)
1173 report_result_task_t
*task
= (report_result_task_t
*)t
;
1175 IInternetProtocol_Terminate(binding
->protocol
, 0);
1177 stop_binding(binding
, task
->hres
, task
->str
);
1179 heap_free(task
->str
);
1183 static HRESULT WINAPI
InternetProtocolSink_ReportResult(IInternetProtocolSink
*iface
,
1184 HRESULT hrResult
, DWORD dwError
, LPCWSTR szResult
)
1186 Binding
*This
= PROTSINK_THIS(iface
);
1188 TRACE("(%p)->(%08x %d %s)\n", This
, hrResult
, dwError
, debugstr_w(szResult
));
1190 if(GetCurrentThreadId() == This
->apartment_thread
&& !This
->continue_call
) {
1191 IInternetProtocol_Terminate(This
->protocol
, 0);
1192 stop_binding(This
, hrResult
, szResult
);
1194 report_result_task_t
*task
= heap_alloc(sizeof(report_result_task_t
));
1196 task
->hres
= hrResult
;
1197 task
->str
= heap_strdupW(szResult
);
1199 push_task(This
, &task
->header
, report_result_proc
);
1205 #undef PROTSINK_THIS
1207 static const IInternetProtocolSinkVtbl InternetProtocolSinkVtbl
= {
1208 InternetProtocolSink_QueryInterface
,
1209 InternetProtocolSink_AddRef
,
1210 InternetProtocolSink_Release
,
1211 InternetProtocolSink_Switch
,
1212 InternetProtocolSink_ReportProgress
,
1213 InternetProtocolSink_ReportData
,
1214 InternetProtocolSink_ReportResult
1217 #define BINDINF_THIS(iface) DEFINE_THIS(Binding, InternetBindInfo, iface)
1219 static HRESULT WINAPI
InternetBindInfo_QueryInterface(IInternetBindInfo
*iface
,
1220 REFIID riid
, void **ppv
)
1222 Binding
*This
= BINDINF_THIS(iface
);
1223 return IBinding_QueryInterface(BINDING(This
), riid
, ppv
);
1226 static ULONG WINAPI
InternetBindInfo_AddRef(IInternetBindInfo
*iface
)
1228 Binding
*This
= BINDINF_THIS(iface
);
1229 return IBinding_AddRef(BINDING(This
));
1232 static ULONG WINAPI
InternetBindInfo_Release(IInternetBindInfo
*iface
)
1234 Binding
*This
= BINDINF_THIS(iface
);
1235 return IBinding_Release(BINDING(This
));
1238 static HRESULT WINAPI
InternetBindInfo_GetBindInfo(IInternetBindInfo
*iface
,
1239 DWORD
*grfBINDF
, BINDINFO
*pbindinfo
)
1241 Binding
*This
= BINDINF_THIS(iface
);
1243 TRACE("(%p)->(%p %p)\n", This
, grfBINDF
, pbindinfo
);
1245 *grfBINDF
= This
->bindf
;
1247 memcpy(pbindinfo
, &This
->bindinfo
, sizeof(BINDINFO
));
1249 if(pbindinfo
->szExtraInfo
|| pbindinfo
->szCustomVerb
)
1250 FIXME("copy strings\n");
1252 if(pbindinfo
->stgmedData
.pUnkForRelease
)
1253 IUnknown_AddRef(pbindinfo
->stgmedData
.pUnkForRelease
);
1256 IUnknown_AddRef(pbindinfo
->pUnk
);
1261 static HRESULT WINAPI
InternetBindInfo_GetBindString(IInternetBindInfo
*iface
,
1262 ULONG ulStringType
, LPOLESTR
*ppwzStr
, ULONG cEl
, ULONG
*pcElFetched
)
1264 Binding
*This
= BINDINF_THIS(iface
);
1266 TRACE("(%p)->(%d %p %d %p)\n", This
, ulStringType
, ppwzStr
, cEl
, pcElFetched
);
1268 switch(ulStringType
) {
1269 case BINDSTRING_ACCEPT_MIMES
: {
1270 static const WCHAR wszMimes
[] = {'*','/','*',0};
1272 if(!ppwzStr
|| !pcElFetched
)
1273 return E_INVALIDARG
;
1275 ppwzStr
[0] = CoTaskMemAlloc(sizeof(wszMimes
));
1276 memcpy(ppwzStr
[0], wszMimes
, sizeof(wszMimes
));
1280 case BINDSTRING_USER_AGENT
: {
1281 IInternetBindInfo
*bindinfo
= NULL
;
1284 hres
= IBindStatusCallback_QueryInterface(This
->callback
, &IID_IInternetBindInfo
,
1289 hres
= IInternetBindInfo_GetBindString(bindinfo
, ulStringType
, ppwzStr
,
1291 IInternetBindInfo_Release(bindinfo
);
1297 FIXME("not supported string type %d\n", ulStringType
);
1303 static const IInternetBindInfoVtbl InternetBindInfoVtbl
= {
1304 InternetBindInfo_QueryInterface
,
1305 InternetBindInfo_AddRef
,
1306 InternetBindInfo_Release
,
1307 InternetBindInfo_GetBindInfo
,
1308 InternetBindInfo_GetBindString
1311 #define SERVPROV_THIS(iface) DEFINE_THIS(Binding, ServiceProvider, iface)
1313 static HRESULT WINAPI
ServiceProvider_QueryInterface(IServiceProvider
*iface
,
1314 REFIID riid
, void **ppv
)
1316 Binding
*This
= SERVPROV_THIS(iface
);
1317 return IBinding_QueryInterface(BINDING(This
), riid
, ppv
);
1320 static ULONG WINAPI
ServiceProvider_AddRef(IServiceProvider
*iface
)
1322 Binding
*This
= SERVPROV_THIS(iface
);
1323 return IBinding_AddRef(BINDING(This
));
1326 static ULONG WINAPI
ServiceProvider_Release(IServiceProvider
*iface
)
1328 Binding
*This
= SERVPROV_THIS(iface
);
1329 return IBinding_Release(BINDING(This
));
1332 static HRESULT WINAPI
ServiceProvider_QueryService(IServiceProvider
*iface
,
1333 REFGUID guidService
, REFIID riid
, void **ppv
)
1335 Binding
*This
= SERVPROV_THIS(iface
);
1338 TRACE("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
1340 if(This
->service_provider
) {
1341 hres
= IServiceProvider_QueryService(This
->service_provider
, guidService
,
1347 WARN("unknown service %s\n", debugstr_guid(guidService
));
1351 #undef SERVPROV_THIS
1353 static const IServiceProviderVtbl ServiceProviderVtbl
= {
1354 ServiceProvider_QueryInterface
,
1355 ServiceProvider_AddRef
,
1356 ServiceProvider_Release
,
1357 ServiceProvider_QueryService
1360 static HRESULT
get_callback(IBindCtx
*pbc
, IBindStatusCallback
**callback
)
1365 hres
= IBindCtx_GetObjectParam(pbc
, bscb_holderW
, &unk
);
1366 if(SUCCEEDED(hres
)) {
1367 hres
= IUnknown_QueryInterface(unk
, &IID_IBindStatusCallback
, (void**)callback
);
1368 IUnknown_Release(unk
);
1371 return SUCCEEDED(hres
) ? S_OK
: INET_E_DATA_NOT_AVAILABLE
;
1374 static BOOL
is_urlmon_protocol(LPCWSTR url
)
1376 static const WCHAR wszCdl
[] = {'c','d','l'};
1377 static const WCHAR wszFile
[] = {'f','i','l','e'};
1378 static const WCHAR wszFtp
[] = {'f','t','p'};
1379 static const WCHAR wszGopher
[] = {'g','o','p','h','e','r'};
1380 static const WCHAR wszHttp
[] = {'h','t','t','p'};
1381 static const WCHAR wszHttps
[] = {'h','t','t','p','s'};
1382 static const WCHAR wszMk
[] = {'m','k'};
1384 static const struct {
1387 } protocol_list
[] = {
1388 {wszCdl
, sizeof(wszCdl
) /sizeof(WCHAR
)},
1389 {wszFile
, sizeof(wszFile
) /sizeof(WCHAR
)},
1390 {wszFtp
, sizeof(wszFtp
) /sizeof(WCHAR
)},
1391 {wszGopher
, sizeof(wszGopher
)/sizeof(WCHAR
)},
1392 {wszHttp
, sizeof(wszHttp
) /sizeof(WCHAR
)},
1393 {wszHttps
, sizeof(wszHttps
) /sizeof(WCHAR
)},
1394 {wszMk
, sizeof(wszMk
) /sizeof(WCHAR
)}
1397 int i
, len
= strlenW(url
);
1399 for(i
=0; i
< sizeof(protocol_list
)/sizeof(protocol_list
[0]); i
++) {
1400 if(len
>= protocol_list
[i
].len
1401 && !memcmp(url
, protocol_list
[i
].scheme
, protocol_list
[i
].len
*sizeof(WCHAR
)))
1408 static HRESULT
Binding_Create(IMoniker
*mon
, Binding
*binding_ctx
, LPCWSTR url
, IBindCtx
*pbc
,
1409 BOOL to_obj
, REFIID riid
, Binding
**binding
)
1414 if(!to_obj
&& !IsEqualGUID(&IID_IStream
, riid
)) {
1415 FIXME("Unsupported riid %s\n", debugstr_guid(riid
));
1419 URLMON_LockModule();
1421 ret
= heap_alloc_zero(sizeof(Binding
));
1423 ret
->lpBindingVtbl
= &BindingVtbl
;
1424 ret
->lpInternetProtocolSinkVtbl
= &InternetProtocolSinkVtbl
;
1425 ret
->lpInternetBindInfoVtbl
= &InternetBindInfoVtbl
;
1426 ret
->lpServiceProviderVtbl
= &ServiceProviderVtbl
;
1430 ret
->to_object
= to_obj
;
1432 ret
->apartment_thread
= GetCurrentThreadId();
1433 ret
->notif_hwnd
= get_notif_hwnd();
1434 ret
->report_mime
= !binding_ctx
;
1435 ret
->download_state
= BEFORE_DOWNLOAD
;
1438 IBindCtx_AddRef(pbc
);
1443 IMoniker_AddRef(mon
);
1447 ret
->bindinfo
.cbSize
= sizeof(BINDINFO
);
1449 InitializeCriticalSection(&ret
->section
);
1450 ret
->section
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": Binding.section");
1452 hres
= get_callback(pbc
, &ret
->callback
);
1454 WARN("Could not get IBindStatusCallback\n");
1455 IBinding_Release(BINDING(ret
));
1459 IBindStatusCallback_QueryInterface(ret
->callback
, &IID_IServiceProvider
,
1460 (void**)&ret
->service_provider
);
1463 ret
->protocol
= binding_ctx
->protocol
;
1464 IInternetProtocol_AddRef(ret
->protocol
);
1466 hres
= create_binding_protocol(url
, TRUE
, &ret
->protocol
);
1468 WARN("Could not get protocol handler\n");
1469 IBinding_Release(BINDING(ret
));
1474 hres
= IBindStatusCallback_GetBindInfo(ret
->callback
, &ret
->bindf
, &ret
->bindinfo
);
1476 WARN("GetBindInfo failed: %08x\n", hres
);
1477 IBinding_Release(BINDING(ret
));
1481 TRACE("bindf %08x\n", ret
->bindf
);
1482 dump_BINDINFO(&ret
->bindinfo
);
1484 ret
->bindf
|= BINDF_FROMURLMON
;
1486 ret
->bindinfo
.dwOptions
|= 0x100000;
1488 if(!is_urlmon_protocol(url
))
1489 ret
->bindf
|= BINDF_NEEDFILE
;
1491 ret
->url
= heap_strdupW(url
);
1494 ret
->stgmed_buf
= binding_ctx
->stgmed_buf
;
1495 IUnknown_AddRef(STGMEDUNK(ret
->stgmed_buf
));
1496 ret
->stream
= binding_ctx
->stream
;
1497 IStream_AddRef(STREAM(binding_ctx
->stream
));
1498 ret
->clipboard_format
= binding_ctx
->clipboard_format
;
1500 ret
->stgmed_buf
= create_stgmed_buf(ret
->protocol
);
1501 ret
->stream
= create_stream(ret
->stgmed_buf
);
1503 ret
->stgmed
.tymed
= TYMED_ISTREAM
;
1504 ret
->stgmed
.u
.pstm
= STREAM((ProtocolStream
*)ret
->stream
);
1505 ret
->stgmed
.pUnkForRelease
= (IUnknown
*)BINDING(ret
); /* NOTE: Windows uses other IUnknown */
1511 static HRESULT
start_binding(IMoniker
*mon
, Binding
*binding_ctx
, LPCWSTR url
, IBindCtx
*pbc
,
1512 BOOL to_obj
, REFIID riid
, Binding
**ret
)
1514 Binding
*binding
= NULL
;
1518 hres
= Binding_Create(mon
, binding_ctx
, url
, pbc
, to_obj
, riid
, &binding
);
1522 hres
= IBindStatusCallback_OnStartBinding(binding
->callback
, 0, BINDING(binding
));
1524 WARN("OnStartBinding failed: %08x\n", hres
);
1525 stop_binding(binding
, 0x800c0008, NULL
);
1526 IBinding_Release(BINDING(binding
));
1531 set_binding_sink(binding
->protocol
, PROTSINK(binding
));
1532 report_data(binding
, 0, 0, 0);
1534 hres
= IInternetProtocol_Start(binding
->protocol
, url
, PROTSINK(binding
),
1535 BINDINF(binding
), 0, 0);
1537 TRACE("start ret %08x\n", hres
);
1540 stop_binding(binding
, hres
, NULL
);
1541 IBinding_Release(BINDING(binding
));
1547 while(!(binding
->bindf
& BINDF_ASYNCHRONOUS
) &&
1548 !(binding
->state
& BINDING_STOPPED
)) {
1549 MsgWaitForMultipleObjects(0, NULL
, FALSE
, 5000, QS_POSTMESSAGE
);
1550 while (PeekMessageW(&msg
, binding
->notif_hwnd
, WM_USER
, WM_USER
+117, PM_REMOVE
|PM_NOYIELD
)) {
1551 TranslateMessage(&msg
);
1552 DispatchMessageW(&msg
);
1560 HRESULT
bind_to_storage(LPCWSTR url
, IBindCtx
*pbc
, REFIID riid
, void **ppv
)
1562 Binding
*binding
= NULL
, *binding_ctx
;
1567 binding_ctx
= get_bctx_binding(pbc
);
1569 hres
= start_binding(NULL
, binding_ctx
, url
, pbc
, FALSE
, riid
, &binding
);
1571 IBinding_Release(BINDING(binding_ctx
));
1575 if(binding
->hres
== S_OK
&& binding
->stgmed_buf
->init
) {
1576 if((binding
->state
& BINDING_STOPPED
) && (binding
->state
& BINDING_LOCKED
))
1577 IInternetProtocol_UnlockRequest(binding
->protocol
);
1579 *ppv
= binding
->stgmed
.u
.pstm
;
1580 IStream_AddRef(binding
->stgmed
.u
.pstm
);
1584 hres
= MK_S_ASYNCHRONOUS
;
1587 IBinding_Release(BINDING(binding
));
1592 HRESULT
bind_to_object(IMoniker
*mon
, LPCWSTR url
, IBindCtx
*pbc
, REFIID riid
, void **ppv
)
1599 hres
= start_binding(mon
, NULL
, url
, pbc
, TRUE
, riid
, &binding
);
1603 if(binding
->hres
!= S_OK
) {
1604 hres
= SUCCEEDED(binding
->hres
) ? S_OK
: binding
->hres
;
1605 }else if(binding
->bindf
& BINDF_ASYNCHRONOUS
) {
1606 hres
= MK_S_ASYNCHRONOUS
;
1608 *ppv
= binding
->obj
;
1609 IUnknown_AddRef(binding
->obj
);
1613 IBinding_Release(BINDING(binding
));