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;
55 typedef struct _stgmed_obj_t stgmed_obj_t;
58 void (*release)(stgmed_obj_t*);
59 HRESULT (*fill_stgmed)(stgmed_obj_t*,STGMEDIUM*);
60 void *(*get_result)(stgmed_obj_t*);
63 struct _stgmed_obj_t {
64 const stgmed_obj_vtbl *vtbl;
67 #define STGMEDUNK(x) ((IUnknown*) &(x)->lpUnknownVtbl)
75 #define BINDING_LOCKED 0x0001
76 #define BINDING_STOPPED 0x0002
77 #define BINDING_OBJAVAIL 0x0004
80 const IBindingVtbl *lpBindingVtbl;
81 const IInternetProtocolSinkVtbl *lpInternetProtocolSinkVtbl;
82 const IInternetBindInfoVtbl *lpInternetBindInfoVtbl;
83 const IServiceProviderVtbl *lpServiceProviderVtbl;
87 IBindStatusCallback *callback;
88 IInternetProtocol *protocol;
89 IServiceProvider *service_provider;
91 stgmed_buf_t *stgmed_buf;
92 stgmed_obj_t *stgmed_obj;
98 UINT clipboard_format;
105 download_state_t download_state;
110 DWORD apartment_thread;
113 task_header_t *task_queue_head, *task_queue_tail;
114 CRITICAL_SECTION section;
117 #define BINDING(x) ((IBinding*) &(x)->lpBindingVtbl)
118 #define PROTSINK(x) ((IInternetProtocolSink*) &(x)->lpInternetProtocolSinkVtbl)
119 #define BINDINF(x) ((IInternetBindInfo*) &(x)->lpInternetBindInfoVtbl)
120 #define SERVPROV(x) ((IServiceProvider*) &(x)->lpServiceProviderVtbl)
122 #define STREAM(x) ((IStream*) &(x)->lpStreamVtbl)
123 #define HTTPNEG2(x) ((IHttpNegotiate2*) &(x)->lpHttpNegotiate2Vtbl)
125 #define WM_MK_CONTINUE (WM_USER+101)
127 static void push_task(Binding *binding, task_header_t *task, task_proc_t proc)
132 EnterCriticalSection(&binding->section);
134 if(binding->task_queue_tail) {
135 binding->task_queue_tail->next = task;
136 binding->task_queue_tail = task;
138 binding->task_queue_tail = binding->task_queue_head = task;
141 LeaveCriticalSection(&binding->section);
144 static task_header_t *pop_task(Binding *binding)
148 EnterCriticalSection(&binding->section);
150 ret = binding->task_queue_head;
152 binding->task_queue_head = ret->next;
153 if(!binding->task_queue_head)
154 binding->task_queue_tail = NULL;
157 LeaveCriticalSection(&binding->section);
162 static void fill_stgmed_buffer(stgmed_buf_t *buf)
166 if(sizeof(buf->buf) == buf->size)
169 buf->hres = IInternetProtocol_Read(buf->protocol, buf->buf+buf->size,
170 sizeof(buf->buf)-buf->size, &read);
176 static LRESULT WINAPI notif_wnd_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
178 if(msg == WM_MK_CONTINUE) {
179 Binding *binding = (Binding*)lParam;
182 while((task = pop_task(binding))) {
183 binding->continue_call++;
184 task->proc(binding, task);
185 binding->continue_call--;
188 IBinding_Release(BINDING(binding));
192 return DefWindowProcW(hwnd, msg, wParam, lParam);
195 static HWND get_notif_hwnd(void)
197 static ATOM wnd_class = 0;
200 static const WCHAR wszURLMonikerNotificationWindow[] =
201 {'U','R','L',' ','M','o','n','i','k','e','r',' ',
202 'N','o','t','i','f','i','c','a','t','i','o','n',' ','W','i','n','d','o','w',0};
205 static WNDCLASSEXW wndclass = {
207 notif_wnd_proc, 0, 0,
208 NULL, NULL, NULL, NULL, NULL,
209 wszURLMonikerNotificationWindow,
213 wndclass.hInstance = URLMON_hInstance;
215 wnd_class = RegisterClassExW(&wndclass);
216 if (!wnd_class && GetLastError() == ERROR_CLASS_ALREADY_EXISTS)
220 hwnd = CreateWindowExW(0, wszURLMonikerNotificationWindow,
221 wszURLMonikerNotificationWindow, 0, 0, 0, 0, 0, HWND_MESSAGE,
222 NULL, URLMON_hInstance, NULL);
224 TRACE("hwnd = %p\n", hwnd);
229 static void dump_BINDINFO(BINDINFO *bi)
231 static const char * const BINDINFOF_str[] = {
233 "BINDINFOF_URLENCODESTGMEDDATA",
234 "BINDINFOF_URLENCODEDEXTRAINFO"
237 static const char * const BINDVERB_str[] = {
251 " %d, %08x, %d, %d\n"
257 bi->cbSize, debugstr_w(bi->szExtraInfo),
258 bi->stgmedData.tymed, bi->stgmedData.u.hGlobal, bi->stgmedData.pUnkForRelease,
259 bi->grfBindInfoF > BINDINFOF_URLENCODEDEXTRAINFO
260 ? "unknown" : BINDINFOF_str[bi->grfBindInfoF],
261 bi->dwBindVerb > BINDVERB_CUSTOM
262 ? "unknown" : BINDVERB_str[bi->dwBindVerb],
263 debugstr_w(bi->szCustomVerb),
264 bi->cbstgmedData, bi->dwOptions, bi->dwOptionsFlags, bi->dwCodePage,
265 bi->securityAttributes.nLength,
266 bi->securityAttributes.lpSecurityDescriptor,
267 bi->securityAttributes.bInheritHandle,
268 debugstr_guid(&bi->iid),
269 bi->pUnk, bi->dwReserved
273 static void set_binding_mime(Binding *binding, LPCWSTR mime)
275 EnterCriticalSection(&binding->section);
277 if(binding->report_mime) {
278 heap_free(binding->mime);
279 binding->mime = heap_strdupW(mime);
282 LeaveCriticalSection(&binding->section);
285 static void handle_mime_available(Binding *binding, BOOL verify)
289 EnterCriticalSection(&binding->section);
290 report_mime = binding->report_mime;
291 binding->report_mime = FALSE;
292 LeaveCriticalSection(&binding->section);
300 fill_stgmed_buffer(binding->stgmed_buf);
301 FindMimeFromData(NULL, binding->url, binding->stgmed_buf->buf,
302 min(binding->stgmed_buf->size, 255), binding->mime, 0, &mime, 0);
304 heap_free(binding->mime);
305 binding->mime = heap_strdupW(mime);
309 IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_MIMETYPEAVAILABLE, binding->mime);
311 binding->clipboard_format = RegisterClipboardFormatW(binding->mime);
315 task_header_t header;
317 } mime_available_task_t;
319 static void mime_available_proc(Binding *binding, task_header_t *t)
321 mime_available_task_t *task = (mime_available_task_t*)t;
323 handle_mime_available(binding, task->verify);
328 static void mime_available(Binding *This, LPCWSTR mime, BOOL verify)
331 set_binding_mime(This, mime);
333 if(GetCurrentThreadId() == This->apartment_thread) {
334 handle_mime_available(This, verify);
336 mime_available_task_t *task = heap_alloc(sizeof(task_header_t));
337 task->verify = verify;
338 push_task(This, &task->header, mime_available_proc);
342 static void stop_binding(Binding *binding, HRESULT hres, LPCWSTR str)
344 if(binding->state & BINDING_LOCKED) {
345 IInternetProtocol_UnlockRequest(binding->protocol);
346 binding->state &= ~BINDING_LOCKED;
349 if(!(binding->state & BINDING_STOPPED)) {
350 binding->state |= BINDING_STOPPED;
352 IBindStatusCallback_OnStopBinding(binding->callback, hres, str);
353 binding->hres = hres;
357 static LPWSTR get_mime_clsid(LPCWSTR mime, CLSID *clsid)
359 LPWSTR key_name, ret;
360 DWORD res, type, size;
365 static const WCHAR mime_keyW[] =
366 {'M','I','M','E','\\','D','a','t','a','b','a','s','e','\\',
367 'C','o','n','t','e','n','t',' ','T','y','p','e','\\'};
368 static const WCHAR clsidW[] = {'C','L','S','I','D',0};
370 len = strlenW(mime)+1;
371 key_name = heap_alloc(sizeof(mime_keyW) + len*sizeof(WCHAR));
372 memcpy(key_name, mime_keyW, sizeof(mime_keyW));
373 strcpyW(key_name + sizeof(mime_keyW)/sizeof(WCHAR), mime);
375 res = RegOpenKeyW(HKEY_CLASSES_ROOT, key_name, &hkey);
377 if(res != ERROR_SUCCESS) {
378 WARN("Could not open MIME key: %x\n", res);
382 size = 50*sizeof(WCHAR);
383 ret = heap_alloc(size);
384 res = RegQueryValueExW(hkey, clsidW, NULL, &type, (LPBYTE)ret, &size);
386 if(res != ERROR_SUCCESS) {
387 WARN("Could not get CLSID: %08x\n", res);
392 hres = CLSIDFromString(ret, clsid);
394 WARN("Could not parse CLSID: %08x\n", hres);
402 static void load_doc_mon(Binding *binding, IPersistMoniker *persist)
407 hres = CreateAsyncBindCtxEx(binding->bctx, 0, NULL, NULL, &bctx, 0);
409 WARN("CreateAsyncBindCtxEx failed: %08x\n", hres);
413 IBindCtx_RevokeObjectParam(bctx, bscb_holderW);
414 IBindCtx_RegisterObjectParam(bctx, cbinding_contextW, (IUnknown*)BINDING(binding));
416 hres = IPersistMoniker_Load(persist, binding->download_state == END_DOWNLOAD, binding->mon, bctx, 0x12);
417 IBindCtx_RevokeObjectParam(bctx, cbinding_contextW);
418 IBindCtx_Release(bctx);
420 FIXME("Load failed: %08x\n", hres);
423 static HRESULT create_mime_object(Binding *binding, const CLSID *clsid, LPCWSTR clsid_str)
425 IPersistMoniker *persist;
428 hres = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
429 &binding->iid, (void**)&binding->obj);
431 WARN("CoCreateInstance failed: %08x\n", hres);
432 return INET_E_CANNOT_INSTANTIATE_OBJECT;
435 binding->state |= BINDING_OBJAVAIL;
437 hres = IUnknown_QueryInterface(binding->obj, &IID_IPersistMoniker, (void**)&persist);
438 if(SUCCEEDED(hres)) {
441 hres = IPersistMoniker_QueryInterface(persist, &IID_IMonikerProp, (void**)&prop);
442 if(SUCCEEDED(hres)) {
443 IMonikerProp_PutProperty(prop, MIMETYPEPROP, binding->mime);
444 IMonikerProp_PutProperty(prop, CLASSIDPROP, clsid_str);
445 IMonikerProp_Release(prop);
448 load_doc_mon(binding, persist);
450 IPersistMoniker_Release(persist);
452 FIXME("Could not get IPersistMoniker: %08x\n", hres);
453 /* FIXME: Try query IPersistFile */
456 IBindStatusCallback_OnObjectAvailable(binding->callback, &binding->iid, binding->obj);
461 static void create_object(Binding *binding)
468 FIXME("MIME not available\n");
472 if(!(clsid_str = get_mime_clsid(binding->mime, &clsid))) {
473 FIXME("Could not find object for MIME %s\n", debugstr_w(binding->mime));
477 IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_CLASSIDAVAILABLE, clsid_str);
478 IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_BEGINSYNCOPERATION, NULL);
480 hres = create_mime_object(binding, &clsid, clsid_str);
481 heap_free(clsid_str);
483 IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_ENDSYNCOPERATION, NULL);
485 stop_binding(binding, hres, NULL);
487 IInternetProtocol_Terminate(binding->protocol, 0);
490 #define STGMEDUNK_THIS(iface) DEFINE_THIS(stgmed_buf_t, Unknown, iface)
492 static HRESULT WINAPI StgMedUnk_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
494 stgmed_buf_t *This = STGMEDUNK_THIS(iface);
498 if(IsEqualGUID(riid, &IID_IUnknown)) {
499 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
501 *ppv = STGMEDUNK(This);
502 IUnknown_AddRef(STGMEDUNK(This));
506 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
507 return E_NOINTERFACE;
510 static ULONG WINAPI StgMedUnk_AddRef(IUnknown *iface)
512 stgmed_buf_t *This = STGMEDUNK_THIS(iface);
513 LONG ref = InterlockedIncrement(&This->ref);
515 TRACE("(%p) ref=%d\n", This, ref);
520 static ULONG WINAPI StgMedUnk_Release(IUnknown *iface)
522 stgmed_buf_t *This = STGMEDUNK_THIS(iface);
523 LONG ref = InterlockedDecrement(&This->ref);
525 TRACE("(%p) ref=%d\n", This, ref);
528 IInternetProtocol_Release(This->protocol);
529 heap_free(This->cache_file);
532 URLMON_UnlockModule();
538 #undef STGMEDUNK_THIS
540 static const IUnknownVtbl StgMedUnkVtbl = {
541 StgMedUnk_QueryInterface,
546 static stgmed_buf_t *create_stgmed_buf(IInternetProtocol *protocol)
548 stgmed_buf_t *ret = heap_alloc(sizeof(*ret));
550 ret->lpUnknownVtbl = &StgMedUnkVtbl;
555 ret->cache_file = NULL;
557 IInternetProtocol_AddRef(protocol);
558 ret->protocol = protocol;
566 stgmed_obj_t stgmed_obj;
567 const IStreamVtbl *lpStreamVtbl;
574 #define STREAM_THIS(iface) DEFINE_THIS(ProtocolStream, Stream, iface)
576 static HRESULT WINAPI ProtocolStream_QueryInterface(IStream *iface,
577 REFIID riid, void **ppv)
579 ProtocolStream *This = STREAM_THIS(iface);
583 if(IsEqualGUID(&IID_IUnknown, riid)) {
584 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
586 }else if(IsEqualGUID(&IID_ISequentialStream, riid)) {
587 TRACE("(%p)->(IID_ISequentialStream %p)\n", This, ppv);
589 }else if(IsEqualGUID(&IID_IStream, riid)) {
590 TRACE("(%p)->(IID_IStream %p)\n", This, ppv);
595 IStream_AddRef(STREAM(This));
599 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
600 return E_NOINTERFACE;
603 static ULONG WINAPI ProtocolStream_AddRef(IStream *iface)
605 ProtocolStream *This = STREAM_THIS(iface);
606 LONG ref = InterlockedIncrement(&This->ref);
608 TRACE("(%p) ref=%d\n", This, ref);
613 static ULONG WINAPI ProtocolStream_Release(IStream *iface)
615 ProtocolStream *This = STREAM_THIS(iface);
616 LONG ref = InterlockedDecrement(&This->ref);
618 TRACE("(%p) ref=%d\n", This, ref);
621 IUnknown_Release(STGMEDUNK(This->buf));
624 URLMON_UnlockModule();
630 static HRESULT WINAPI ProtocolStream_Read(IStream *iface, void *pv,
631 ULONG cb, ULONG *pcbRead)
633 ProtocolStream *This = STREAM_THIS(iface);
634 DWORD read = 0, pread = 0;
637 TRACE("(%p)->(%p %d %p)\n", This, pv, cb, pcbRead);
639 if(This->buf->size) {
642 if(read > This->buf->size)
643 read = This->buf->size;
645 memcpy(pv, This->buf->buf, read);
647 if(read < This->buf->size)
648 memmove(This->buf->buf, This->buf->buf+read, This->buf->size-read);
649 This->buf->size -= read;
658 hres = This->buf->hres = IInternetProtocol_Read(This->buf->protocol, (PBYTE)pv+read, cb-read, &pread);
660 *pcbRead = read + pread;
662 if(hres == E_PENDING)
664 else if(FAILED(hres))
665 FIXME("Read failed: %08x\n", hres);
667 return read || pread ? S_OK : S_FALSE;
670 static HRESULT WINAPI ProtocolStream_Write(IStream *iface, const void *pv,
671 ULONG cb, ULONG *pcbWritten)
673 ProtocolStream *This = STREAM_THIS(iface);
675 TRACE("(%p)->(%p %d %p)\n", This, pv, cb, pcbWritten);
677 return STG_E_ACCESSDENIED;
680 static HRESULT WINAPI ProtocolStream_Seek(IStream *iface, LARGE_INTEGER dlibMove,
681 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
683 ProtocolStream *This = STREAM_THIS(iface);
684 FIXME("(%p)->(%d %08x %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
688 static HRESULT WINAPI ProtocolStream_SetSize(IStream *iface, ULARGE_INTEGER libNewSize)
690 ProtocolStream *This = STREAM_THIS(iface);
691 FIXME("(%p)->(%d)\n", This, libNewSize.u.LowPart);
695 static HRESULT WINAPI ProtocolStream_CopyTo(IStream *iface, IStream *pstm,
696 ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten)
698 ProtocolStream *This = STREAM_THIS(iface);
699 FIXME("(%p)->(%p %d %p %p)\n", This, pstm, cb.u.LowPart, pcbRead, pcbWritten);
703 static HRESULT WINAPI ProtocolStream_Commit(IStream *iface, DWORD grfCommitFlags)
705 ProtocolStream *This = STREAM_THIS(iface);
707 TRACE("(%p)->(%08x)\n", This, grfCommitFlags);
712 static HRESULT WINAPI ProtocolStream_Revert(IStream *iface)
714 ProtocolStream *This = STREAM_THIS(iface);
716 TRACE("(%p)\n", This);
721 static HRESULT WINAPI ProtocolStream_LockRegion(IStream *iface, ULARGE_INTEGER libOffset,
722 ULARGE_INTEGER cb, DWORD dwLockType)
724 ProtocolStream *This = STREAM_THIS(iface);
725 FIXME("(%p)->(%d %d %d)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
729 static HRESULT WINAPI ProtocolStream_UnlockRegion(IStream *iface,
730 ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
732 ProtocolStream *This = STREAM_THIS(iface);
733 FIXME("(%p)->(%d %d %d)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
737 static HRESULT WINAPI ProtocolStream_Stat(IStream *iface, STATSTG *pstatstg,
740 ProtocolStream *This = STREAM_THIS(iface);
741 FIXME("(%p)->(%p %08x)\n", This, pstatstg, dwStatFlag);
745 static HRESULT WINAPI ProtocolStream_Clone(IStream *iface, IStream **ppstm)
747 ProtocolStream *This = STREAM_THIS(iface);
748 FIXME("(%p)->(%p)\n", This, ppstm);
754 static const IStreamVtbl ProtocolStreamVtbl = {
755 ProtocolStream_QueryInterface,
756 ProtocolStream_AddRef,
757 ProtocolStream_Release,
759 ProtocolStream_Write,
761 ProtocolStream_SetSize,
762 ProtocolStream_CopyTo,
763 ProtocolStream_Commit,
764 ProtocolStream_Revert,
765 ProtocolStream_LockRegion,
766 ProtocolStream_UnlockRegion,
771 static void stgmed_stream_release(stgmed_obj_t *obj)
773 ProtocolStream *stream = (ProtocolStream*)obj;
774 IStream_Release(STREAM(stream));
777 static HRESULT stgmed_stream_fill_stgmed(stgmed_obj_t *obj, STGMEDIUM *stgmed)
779 ProtocolStream *stream = (ProtocolStream*)obj;
781 stgmed->tymed = TYMED_ISTREAM;
782 stgmed->u.pstm = STREAM(stream);
783 stgmed->pUnkForRelease = STGMEDUNK(stream->buf);
788 static void *stgmed_stream_get_result(stgmed_obj_t *obj)
790 ProtocolStream *stream = (ProtocolStream*)obj;
792 IStream_AddRef(STREAM(stream));
793 return STREAM(stream);
796 static const stgmed_obj_vtbl stgmed_stream_vtbl = {
797 stgmed_stream_release,
798 stgmed_stream_fill_stgmed,
799 stgmed_stream_get_result
803 stgmed_obj_t stgmed_obj;
807 static stgmed_obj_t *create_stgmed_stream(stgmed_buf_t *buf)
809 ProtocolStream *ret = heap_alloc(sizeof(ProtocolStream));
811 ret->stgmed_obj.vtbl = &stgmed_stream_vtbl;
812 ret->lpStreamVtbl = &ProtocolStreamVtbl;
815 IUnknown_AddRef(STGMEDUNK(buf));
820 return &ret->stgmed_obj;
823 static void stgmed_file_release(stgmed_obj_t *obj)
825 stgmed_file_obj_t *file_obj = (stgmed_file_obj_t*)obj;
827 IUnknown_Release(STGMEDUNK(file_obj->buf));
831 static HRESULT stgmed_file_fill_stgmed(stgmed_obj_t *obj, STGMEDIUM *stgmed)
833 stgmed_file_obj_t *file_obj = (stgmed_file_obj_t*)obj;
835 if(!file_obj->buf->cache_file) {
836 WARN("cache_file not set\n");
837 return INET_E_DATA_NOT_AVAILABLE;
840 fill_stgmed_buffer(file_obj->buf);
841 if(file_obj->buf->size == sizeof(file_obj->buf->buf)) {
847 hres = IInternetProtocol_Read(file_obj->buf->protocol, buf, sizeof(buf), &read);
848 }while(hres == S_OK);
851 stgmed->tymed = TYMED_FILE;
852 stgmed->u.lpszFileName = file_obj->buf->cache_file;
853 stgmed->pUnkForRelease = STGMEDUNK(file_obj->buf);
858 static void *stgmed_file_get_result(stgmed_obj_t *obj)
863 static const stgmed_obj_vtbl stgmed_file_vtbl = {
865 stgmed_file_fill_stgmed,
866 stgmed_file_get_result
869 static stgmed_obj_t *create_stgmed_file(stgmed_buf_t *buf)
871 stgmed_file_obj_t *ret = heap_alloc(sizeof(*ret));
873 ret->stgmed_obj.vtbl = &stgmed_file_vtbl;
875 IUnknown_AddRef(STGMEDUNK(buf));
878 return &ret->stgmed_obj;
881 #define BINDING_THIS(iface) DEFINE_THIS(Binding, Binding, iface)
883 static HRESULT WINAPI Binding_QueryInterface(IBinding *iface, REFIID riid, void **ppv)
885 Binding *This = BINDING_THIS(iface);
889 if(IsEqualGUID(&IID_IUnknown, riid)) {
890 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
891 *ppv = BINDING(This);
892 }else if(IsEqualGUID(&IID_IBinding, riid)) {
893 TRACE("(%p)->(IID_IBinding %p)\n", This, ppv);
894 *ppv = BINDING(This);
895 }else if(IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
896 TRACE("(%p)->(IID_IInternetProtocolSink %p)\n", This, ppv);
897 *ppv = PROTSINK(This);
898 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
899 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
900 *ppv = BINDINF(This);
901 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
902 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
903 *ppv = SERVPROV(This);
907 IBinding_AddRef(BINDING(This));
911 WARN("Unsupported interface %s\n", debugstr_guid(riid));
912 return E_NOINTERFACE;
915 static ULONG WINAPI Binding_AddRef(IBinding *iface)
917 Binding *This = BINDING_THIS(iface);
918 LONG ref = InterlockedIncrement(&This->ref);
920 TRACE("(%p) ref=%d\n", This, ref);
925 static ULONG WINAPI Binding_Release(IBinding *iface)
927 Binding *This = BINDING_THIS(iface);
928 LONG ref = InterlockedDecrement(&This->ref);
930 TRACE("(%p) ref=%d\n", This, ref);
933 if (This->notif_hwnd)
934 DestroyWindow( This->notif_hwnd );
936 IMoniker_Release(This->mon);
938 IBindStatusCallback_Release(This->callback);
940 IInternetProtocol_Release(This->protocol);
941 if(This->service_provider)
942 IServiceProvider_Release(This->service_provider);
944 IUnknown_Release(STGMEDUNK(This->stgmed_buf));
946 This->stgmed_obj->vtbl->release(This->stgmed_obj);
948 IUnknown_Release(This->obj);
950 IBindCtx_Release(This->bctx);
952 ReleaseBindInfo(&This->bindinfo);
953 This->section.DebugInfo->Spare[0] = 0;
954 DeleteCriticalSection(&This->section);
955 heap_free(This->mime);
956 heap_free(This->url);
960 URLMON_UnlockModule();
966 static HRESULT WINAPI Binding_Abort(IBinding *iface)
968 Binding *This = BINDING_THIS(iface);
969 FIXME("(%p)\n", This);
973 static HRESULT WINAPI Binding_Suspend(IBinding *iface)
975 Binding *This = BINDING_THIS(iface);
976 FIXME("(%p)\n", This);
980 static HRESULT WINAPI Binding_Resume(IBinding *iface)
982 Binding *This = BINDING_THIS(iface);
983 FIXME("(%p)\n", This);
987 static HRESULT WINAPI Binding_SetPriority(IBinding *iface, LONG nPriority)
989 Binding *This = BINDING_THIS(iface);
990 FIXME("(%p)->(%d)\n", This, nPriority);
994 static HRESULT WINAPI Binding_GetPriority(IBinding *iface, LONG *pnPriority)
996 Binding *This = BINDING_THIS(iface);
997 FIXME("(%p)->(%p)\n", This, pnPriority);
1001 static HRESULT WINAPI Binding_GetBindResult(IBinding *iface, CLSID *pclsidProtocol,
1002 DWORD *pdwResult, LPOLESTR *pszResult, DWORD *pdwReserved)
1004 Binding *This = BINDING_THIS(iface);
1005 FIXME("(%p)->(%p %p %p %p)\n", This, pclsidProtocol, pdwResult, pszResult, pdwReserved);
1009 static Binding *get_bctx_binding(IBindCtx *bctx)
1015 hres = IBindCtx_GetObjectParam(bctx, cbinding_contextW, &unk);
1019 hres = IUnknown_QueryInterface(unk, &IID_IBinding, (void*)&binding);
1020 IUnknown_Release(unk);
1025 return BINDING_THIS(binding);
1030 static const IBindingVtbl BindingVtbl = {
1031 Binding_QueryInterface,
1037 Binding_SetPriority,
1038 Binding_GetPriority,
1039 Binding_GetBindResult
1042 #define PROTSINK_THIS(iface) DEFINE_THIS(Binding, InternetProtocolSink, iface)
1044 static HRESULT WINAPI InternetProtocolSink_QueryInterface(IInternetProtocolSink *iface,
1045 REFIID riid, void **ppv)
1047 Binding *This = PROTSINK_THIS(iface);
1048 return IBinding_QueryInterface(BINDING(This), riid, ppv);
1051 static ULONG WINAPI InternetProtocolSink_AddRef(IInternetProtocolSink *iface)
1053 Binding *This = PROTSINK_THIS(iface);
1054 return IBinding_AddRef(BINDING(This));
1057 static ULONG WINAPI InternetProtocolSink_Release(IInternetProtocolSink *iface)
1059 Binding *This = PROTSINK_THIS(iface);
1060 return IBinding_Release(BINDING(This));
1064 task_header_t header;
1068 static void switch_proc(Binding *binding, task_header_t *t)
1070 switch_task_t *task = (switch_task_t*)t;
1072 IInternetProtocol_Continue(binding->protocol, &task->data);
1077 static HRESULT WINAPI InternetProtocolSink_Switch(IInternetProtocolSink *iface,
1078 PROTOCOLDATA *pProtocolData)
1080 Binding *This = PROTSINK_THIS(iface);
1081 switch_task_t *task;
1083 TRACE("(%p)->(%p)\n", This, pProtocolData);
1085 task = heap_alloc(sizeof(switch_task_t));
1086 task->data = *pProtocolData;
1088 push_task(This, &task->header, switch_proc);
1090 IBinding_AddRef(BINDING(This));
1091 PostMessageW(This->notif_hwnd, WM_MK_CONTINUE, 0, (LPARAM)This);
1097 task_header_t header;
1104 } on_progress_task_t;
1106 static void on_progress_proc(Binding *binding, task_header_t *t)
1108 on_progress_task_t *task = (on_progress_task_t*)t;
1110 IBindStatusCallback_OnProgress(binding->callback, task->progress,
1111 task->progress_max, task->status_code, task->status_text);
1113 heap_free(task->status_text);
1117 static void on_progress(Binding *This, ULONG progress, ULONG progress_max,
1118 ULONG status_code, LPCWSTR status_text)
1120 on_progress_task_t *task;
1122 if(GetCurrentThreadId() == This->apartment_thread && !This->continue_call) {
1123 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1124 status_code, status_text);
1128 task = heap_alloc(sizeof(on_progress_task_t));
1130 task->progress = progress;
1131 task->progress_max = progress_max;
1132 task->status_code = status_code;
1135 DWORD size = (strlenW(status_text)+1)*sizeof(WCHAR);
1137 task->status_text = heap_alloc(size);
1138 memcpy(task->status_text, status_text, size);
1140 task->status_text = NULL;
1143 push_task(This, &task->header, on_progress_proc);
1145 if(GetCurrentThreadId() != This->apartment_thread) {
1146 IBinding_AddRef(BINDING(This));
1147 PostMessageW(This->notif_hwnd, WM_MK_CONTINUE, 0, (LPARAM)This);
1151 static HRESULT WINAPI InternetProtocolSink_ReportProgress(IInternetProtocolSink *iface,
1152 ULONG ulStatusCode, LPCWSTR szStatusText)
1154 Binding *This = PROTSINK_THIS(iface);
1156 TRACE("(%p)->(%u %s)\n", This, ulStatusCode, debugstr_w(szStatusText));
1158 switch(ulStatusCode) {
1159 case BINDSTATUS_FINDINGRESOURCE:
1160 on_progress(This, 0, 0, BINDSTATUS_FINDINGRESOURCE, szStatusText);
1162 case BINDSTATUS_CONNECTING:
1163 on_progress(This, 0, 0, BINDSTATUS_CONNECTING, szStatusText);
1165 case BINDSTATUS_BEGINDOWNLOADDATA:
1166 fill_stgmed_buffer(This->stgmed_buf);
1168 case BINDSTATUS_MIMETYPEAVAILABLE:
1169 set_binding_mime(This, szStatusText);
1171 case BINDSTATUS_SENDINGREQUEST:
1172 on_progress(This, 0, 0, BINDSTATUS_SENDINGREQUEST, szStatusText);
1174 case BINDSTATUS_PROTOCOLCLASSID:
1176 case BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE:
1177 mime_available(This, szStatusText, FALSE);
1179 case BINDSTATUS_CACHEFILENAMEAVAILABLE:
1180 heap_free(This->stgmed_buf->cache_file);
1181 This->stgmed_buf->cache_file = heap_strdupW(szStatusText);
1183 case BINDSTATUS_DIRECTBIND:
1184 This->report_mime = FALSE;
1186 case BINDSTATUS_ACCEPTRANGES:
1189 FIXME("Unhandled status code %d\n", ulStatusCode);
1196 static void report_data(Binding *This, DWORD bscf, ULONG progress, ULONG progress_max)
1198 FORMATETC formatetc = {0, NULL, 1, -1, TYMED_ISTREAM};
1199 BOOL sent_begindownloaddata = FALSE;
1201 TRACE("(%p)->(%d %u %u)\n", This, bscf, progress, progress_max);
1203 if(This->download_state == END_DOWNLOAD)
1206 if(GetCurrentThreadId() != This->apartment_thread)
1207 FIXME("called from worker thread\n");
1209 if(This->report_mime)
1210 mime_available(This, NULL, TRUE);
1212 if(This->download_state == BEFORE_DOWNLOAD) {
1213 fill_stgmed_buffer(This->stgmed_buf);
1215 This->download_state = DOWNLOADING;
1216 sent_begindownloaddata = TRUE;
1217 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1218 BINDSTATUS_BEGINDOWNLOADDATA, This->url);
1220 if(This->stgmed_buf->cache_file)
1221 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1222 BINDSTATUS_CACHEFILENAMEAVAILABLE, This->stgmed_buf->cache_file);
1225 if(This->stgmed_buf->hres == S_FALSE || (bscf & BSCF_LASTDATANOTIFICATION)) {
1226 This->download_state = END_DOWNLOAD;
1227 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1228 BINDSTATUS_ENDDOWNLOADDATA, This->url);
1229 }else if(!sent_begindownloaddata) {
1230 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1231 BINDSTATUS_DOWNLOADINGDATA, This->url);
1234 if(This->to_object) {
1235 if(!(This->state & BINDING_OBJAVAIL))
1236 create_object(This);
1241 if(!(This->state & BINDING_LOCKED)) {
1242 HRESULT hres = IInternetProtocol_LockRequest(This->protocol, 0);
1244 This->state |= BINDING_LOCKED;
1247 hres = This->stgmed_obj->vtbl->fill_stgmed(This->stgmed_obj, &stgmed);
1249 stop_binding(This, hres, NULL);
1253 formatetc.tymed = stgmed.tymed;
1254 formatetc.cfFormat = This->clipboard_format;
1256 IBindStatusCallback_OnDataAvailable(This->callback, bscf, progress,
1257 &formatetc, &stgmed);
1259 if(This->download_state == END_DOWNLOAD)
1260 stop_binding(This, S_OK, NULL);
1265 task_header_t header;
1269 } report_data_task_t;
1271 static void report_data_proc(Binding *binding, task_header_t *t)
1273 report_data_task_t *task = (report_data_task_t*)t;
1275 report_data(binding, task->bscf, task->progress, task->progress_max);
1280 static HRESULT WINAPI InternetProtocolSink_ReportData(IInternetProtocolSink *iface,
1281 DWORD grfBSCF, ULONG ulProgress, ULONG ulProgressMax)
1283 Binding *This = PROTSINK_THIS(iface);
1285 TRACE("(%p)->(%d %u %u)\n", This, grfBSCF, ulProgress, ulProgressMax);
1287 if(GetCurrentThreadId() != This->apartment_thread)
1288 FIXME("called from worked hread\n");
1290 if(This->continue_call) {
1291 report_data_task_t *task = heap_alloc(sizeof(report_data_task_t));
1292 task->bscf = grfBSCF;
1293 task->progress = ulProgress;
1294 task->progress_max = ulProgressMax;
1296 push_task(This, &task->header, report_data_proc);
1298 report_data(This, grfBSCF, ulProgress, ulProgressMax);
1305 task_header_t header;
1309 } report_result_task_t;
1311 static void report_result_proc(Binding *binding, task_header_t *t)
1313 report_result_task_t *task = (report_result_task_t*)t;
1315 IInternetProtocol_Terminate(binding->protocol, 0);
1317 stop_binding(binding, task->hres, task->str);
1319 heap_free(task->str);
1323 static HRESULT WINAPI InternetProtocolSink_ReportResult(IInternetProtocolSink *iface,
1324 HRESULT hrResult, DWORD dwError, LPCWSTR szResult)
1326 Binding *This = PROTSINK_THIS(iface);
1328 TRACE("(%p)->(%08x %d %s)\n", This, hrResult, dwError, debugstr_w(szResult));
1330 if(GetCurrentThreadId() == This->apartment_thread && !This->continue_call) {
1331 IInternetProtocol_Terminate(This->protocol, 0);
1332 stop_binding(This, hrResult, szResult);
1334 report_result_task_t *task = heap_alloc(sizeof(report_result_task_t));
1336 task->hres = hrResult;
1337 task->str = heap_strdupW(szResult);
1339 push_task(This, &task->header, report_result_proc);
1345 #undef PROTSINK_THIS
1347 static const IInternetProtocolSinkVtbl InternetProtocolSinkVtbl = {
1348 InternetProtocolSink_QueryInterface,
1349 InternetProtocolSink_AddRef,
1350 InternetProtocolSink_Release,
1351 InternetProtocolSink_Switch,
1352 InternetProtocolSink_ReportProgress,
1353 InternetProtocolSink_ReportData,
1354 InternetProtocolSink_ReportResult
1357 #define BINDINF_THIS(iface) DEFINE_THIS(Binding, InternetBindInfo, iface)
1359 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
1360 REFIID riid, void **ppv)
1362 Binding *This = BINDINF_THIS(iface);
1363 return IBinding_QueryInterface(BINDING(This), riid, ppv);
1366 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
1368 Binding *This = BINDINF_THIS(iface);
1369 return IBinding_AddRef(BINDING(This));
1372 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
1374 Binding *This = BINDINF_THIS(iface);
1375 return IBinding_Release(BINDING(This));
1378 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
1379 DWORD *grfBINDF, BINDINFO *pbindinfo)
1381 Binding *This = BINDINF_THIS(iface);
1383 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
1385 *grfBINDF = This->bindf;
1387 *pbindinfo = This->bindinfo;
1389 if(pbindinfo->szExtraInfo || pbindinfo->szCustomVerb)
1390 FIXME("copy strings\n");
1392 if(pbindinfo->stgmedData.pUnkForRelease)
1393 IUnknown_AddRef(pbindinfo->stgmedData.pUnkForRelease);
1396 IUnknown_AddRef(pbindinfo->pUnk);
1401 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
1402 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
1404 Binding *This = BINDINF_THIS(iface);
1406 TRACE("(%p)->(%d %p %d %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
1408 switch(ulStringType) {
1409 case BINDSTRING_ACCEPT_MIMES: {
1410 static const WCHAR wszMimes[] = {'*','/','*',0};
1412 if(!ppwzStr || !pcElFetched)
1413 return E_INVALIDARG;
1415 ppwzStr[0] = CoTaskMemAlloc(sizeof(wszMimes));
1416 memcpy(ppwzStr[0], wszMimes, sizeof(wszMimes));
1420 case BINDSTRING_USER_AGENT: {
1421 IInternetBindInfo *bindinfo = NULL;
1424 hres = IBindStatusCallback_QueryInterface(This->callback, &IID_IInternetBindInfo,
1429 hres = IInternetBindInfo_GetBindString(bindinfo, ulStringType, ppwzStr,
1431 IInternetBindInfo_Release(bindinfo);
1437 FIXME("not supported string type %d\n", ulStringType);
1443 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
1444 InternetBindInfo_QueryInterface,
1445 InternetBindInfo_AddRef,
1446 InternetBindInfo_Release,
1447 InternetBindInfo_GetBindInfo,
1448 InternetBindInfo_GetBindString
1451 #define SERVPROV_THIS(iface) DEFINE_THIS(Binding, ServiceProvider, iface)
1453 static HRESULT WINAPI ServiceProvider_QueryInterface(IServiceProvider *iface,
1454 REFIID riid, void **ppv)
1456 Binding *This = SERVPROV_THIS(iface);
1457 return IBinding_QueryInterface(BINDING(This), riid, ppv);
1460 static ULONG WINAPI ServiceProvider_AddRef(IServiceProvider *iface)
1462 Binding *This = SERVPROV_THIS(iface);
1463 return IBinding_AddRef(BINDING(This));
1466 static ULONG WINAPI ServiceProvider_Release(IServiceProvider *iface)
1468 Binding *This = SERVPROV_THIS(iface);
1469 return IBinding_Release(BINDING(This));
1472 static HRESULT WINAPI ServiceProvider_QueryService(IServiceProvider *iface,
1473 REFGUID guidService, REFIID riid, void **ppv)
1475 Binding *This = SERVPROV_THIS(iface);
1478 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
1480 if(This->service_provider) {
1481 hres = IServiceProvider_QueryService(This->service_provider, guidService,
1487 WARN("unknown service %s\n", debugstr_guid(guidService));
1491 #undef SERVPROV_THIS
1493 static const IServiceProviderVtbl ServiceProviderVtbl = {
1494 ServiceProvider_QueryInterface,
1495 ServiceProvider_AddRef,
1496 ServiceProvider_Release,
1497 ServiceProvider_QueryService
1500 static HRESULT get_callback(IBindCtx *pbc, IBindStatusCallback **callback)
1505 hres = IBindCtx_GetObjectParam(pbc, bscb_holderW, &unk);
1506 if(SUCCEEDED(hres)) {
1507 hres = IUnknown_QueryInterface(unk, &IID_IBindStatusCallback, (void**)callback);
1508 IUnknown_Release(unk);
1511 return SUCCEEDED(hres) ? S_OK : INET_E_DATA_NOT_AVAILABLE;
1514 static BOOL is_urlmon_protocol(LPCWSTR url)
1516 static const WCHAR wszCdl[] = {'c','d','l'};
1517 static const WCHAR wszFile[] = {'f','i','l','e'};
1518 static const WCHAR wszFtp[] = {'f','t','p'};
1519 static const WCHAR wszGopher[] = {'g','o','p','h','e','r'};
1520 static const WCHAR wszHttp[] = {'h','t','t','p'};
1521 static const WCHAR wszHttps[] = {'h','t','t','p','s'};
1522 static const WCHAR wszMk[] = {'m','k'};
1524 static const struct {
1527 } protocol_list[] = {
1528 {wszCdl, sizeof(wszCdl) /sizeof(WCHAR)},
1529 {wszFile, sizeof(wszFile) /sizeof(WCHAR)},
1530 {wszFtp, sizeof(wszFtp) /sizeof(WCHAR)},
1531 {wszGopher, sizeof(wszGopher)/sizeof(WCHAR)},
1532 {wszHttp, sizeof(wszHttp) /sizeof(WCHAR)},
1533 {wszHttps, sizeof(wszHttps) /sizeof(WCHAR)},
1534 {wszMk, sizeof(wszMk) /sizeof(WCHAR)}
1538 int len = lstrlenW(url);
1540 for(i=0; i < sizeof(protocol_list)/sizeof(protocol_list[0]); i++) {
1541 if(len >= protocol_list[i].len
1542 && !memcmp(url, protocol_list[i].scheme, protocol_list[i].len*sizeof(WCHAR)))
1549 static HRESULT Binding_Create(IMoniker *mon, Binding *binding_ctx, LPCWSTR url, IBindCtx *pbc,
1550 BOOL to_obj, REFIID riid, Binding **binding)
1555 URLMON_LockModule();
1557 ret = heap_alloc_zero(sizeof(Binding));
1559 ret->lpBindingVtbl = &BindingVtbl;
1560 ret->lpInternetProtocolSinkVtbl = &InternetProtocolSinkVtbl;
1561 ret->lpInternetBindInfoVtbl = &InternetBindInfoVtbl;
1562 ret->lpServiceProviderVtbl = &ServiceProviderVtbl;
1566 ret->to_object = to_obj;
1568 ret->apartment_thread = GetCurrentThreadId();
1569 ret->notif_hwnd = get_notif_hwnd();
1570 ret->report_mime = !binding_ctx;
1571 ret->download_state = BEFORE_DOWNLOAD;
1574 IBindCtx_AddRef(pbc);
1579 IMoniker_AddRef(mon);
1583 ret->bindinfo.cbSize = sizeof(BINDINFO);
1585 InitializeCriticalSection(&ret->section);
1586 ret->section.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": Binding.section");
1588 hres = get_callback(pbc, &ret->callback);
1590 WARN("Could not get IBindStatusCallback\n");
1591 IBinding_Release(BINDING(ret));
1595 IBindStatusCallback_QueryInterface(ret->callback, &IID_IServiceProvider,
1596 (void**)&ret->service_provider);
1599 ret->protocol = binding_ctx->protocol;
1600 IInternetProtocol_AddRef(ret->protocol);
1602 hres = create_binding_protocol(url, TRUE, &ret->protocol);
1604 WARN("Could not get protocol handler\n");
1605 IBinding_Release(BINDING(ret));
1610 hres = IBindStatusCallback_GetBindInfo(ret->callback, &ret->bindf, &ret->bindinfo);
1612 WARN("GetBindInfo failed: %08x\n", hres);
1613 IBinding_Release(BINDING(ret));
1617 TRACE("bindf %08x\n", ret->bindf);
1618 dump_BINDINFO(&ret->bindinfo);
1620 ret->bindf |= BINDF_FROMURLMON;
1622 ret->bindinfo.dwOptions |= 0x100000;
1624 if(!is_urlmon_protocol(url))
1625 ret->bindf |= BINDF_NEEDFILE;
1627 ret->url = heap_strdupW(url);
1630 ret->stgmed_buf = binding_ctx->stgmed_buf;
1631 IUnknown_AddRef(STGMEDUNK(ret->stgmed_buf));
1632 ret->clipboard_format = binding_ctx->clipboard_format;
1634 ret->stgmed_buf = create_stgmed_buf(ret->protocol);
1638 ret->stgmed_obj = NULL;
1639 }else if(IsEqualGUID(&IID_IStream, riid)) {
1640 ret->stgmed_obj = create_stgmed_stream(ret->stgmed_buf);
1641 }else if(IsEqualGUID(&IID_IUnknown, riid)) {
1642 ret->bindf |= BINDF_NEEDFILE;
1643 ret->stgmed_obj = create_stgmed_file(ret->stgmed_buf);
1645 FIXME("Unsupported riid %s\n", debugstr_guid(riid));
1646 IBinding_Release(BINDING(ret));
1654 static HRESULT start_binding(IMoniker *mon, Binding *binding_ctx, LPCWSTR url, IBindCtx *pbc,
1655 BOOL to_obj, REFIID riid, Binding **ret)
1657 Binding *binding = NULL;
1661 hres = Binding_Create(mon, binding_ctx, url, pbc, to_obj, riid, &binding);
1665 hres = IBindStatusCallback_OnStartBinding(binding->callback, 0, BINDING(binding));
1667 WARN("OnStartBinding failed: %08x\n", hres);
1668 stop_binding(binding, INET_E_DOWNLOAD_FAILURE, NULL);
1669 IBinding_Release(BINDING(binding));
1674 set_binding_sink(binding->protocol, PROTSINK(binding));
1675 report_data(binding, 0, 0, 0);
1677 hres = IInternetProtocol_Start(binding->protocol, url, PROTSINK(binding),
1678 BINDINF(binding), 0, 0);
1680 TRACE("start ret %08x\n", hres);
1682 if(FAILED(hres) && hres != E_PENDING) {
1683 stop_binding(binding, hres, NULL);
1684 IBinding_Release(BINDING(binding));
1690 while(!(binding->bindf & BINDF_ASYNCHRONOUS) &&
1691 !(binding->state & BINDING_STOPPED)) {
1692 MsgWaitForMultipleObjects(0, NULL, FALSE, 5000, QS_POSTMESSAGE);
1693 while (PeekMessageW(&msg, binding->notif_hwnd, WM_USER, WM_USER+117, PM_REMOVE|PM_NOYIELD)) {
1694 TranslateMessage(&msg);
1695 DispatchMessageW(&msg);
1703 HRESULT bind_to_storage(LPCWSTR url, IBindCtx *pbc, REFIID riid, void **ppv)
1705 Binding *binding = NULL, *binding_ctx;
1710 binding_ctx = get_bctx_binding(pbc);
1712 hres = start_binding(NULL, binding_ctx, url, pbc, FALSE, riid, &binding);
1714 IBinding_Release(BINDING(binding_ctx));
1718 if(binding->hres == S_OK && binding->stgmed_buf->init) {
1719 if((binding->state & BINDING_STOPPED) && (binding->state & BINDING_LOCKED))
1720 IInternetProtocol_UnlockRequest(binding->protocol);
1722 *ppv = binding->stgmed_obj->vtbl->get_result(binding->stgmed_obj);
1725 IBinding_Release(BINDING(binding));
1727 return *ppv ? S_OK : MK_S_ASYNCHRONOUS;
1730 HRESULT bind_to_object(IMoniker *mon, LPCWSTR url, IBindCtx *pbc, REFIID riid, void **ppv)
1737 hres = start_binding(mon, NULL, url, pbc, TRUE, riid, &binding);
1741 if(binding->hres != S_OK) {
1742 hres = SUCCEEDED(binding->hres) ? S_OK : binding->hres;
1743 }else if(binding->bindf & BINDF_ASYNCHRONOUS) {
1744 hres = MK_S_ASYNCHRONOUS;
1746 *ppv = binding->obj;
1747 IUnknown_AddRef(binding->obj);
1751 IBinding_Release(BINDING(binding));