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"
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
27 static WCHAR cbinding_contextW[] = {'C','B','i','n','d','i','n','g',' ','C','o','n','t','e','x','t',0};
28 static WCHAR bscb_holderW[] = { '_','B','S','C','B','_','H','o','l','d','e','r','_',0 };
31 IUnknown IUnknown_iface;
35 IInternetProtocolEx *protocol;
43 typedef struct _stgmed_obj_t stgmed_obj_t;
46 void (*release)(stgmed_obj_t*);
47 HRESULT (*fill_stgmed)(stgmed_obj_t*,STGMEDIUM*);
48 HRESULT (*get_result)(stgmed_obj_t*,DWORD,void**);
51 struct _stgmed_obj_t {
52 const stgmed_obj_vtbl *vtbl;
61 #define BINDING_LOCKED 0x0001
62 #define BINDING_STOPPED 0x0002
63 #define BINDING_OBJAVAIL 0x0004
64 #define BINDING_ABORTED 0x0008
67 IBinding IBinding_iface;
68 IInternetProtocolSink IInternetProtocolSink_iface;
69 IInternetBindInfo IInternetBindInfo_iface;
70 IWinInetHttpInfo IWinInetHttpInfo_iface;
71 IServiceProvider IServiceProvider_iface;
75 IBindStatusCallback *callback;
76 IServiceProvider *service_provider;
78 BindProtocol *protocol;
80 stgmed_buf_t *stgmed_buf;
81 stgmed_obj_t *stgmed_obj;
87 UINT clipboard_format;
95 download_state_t download_state;
101 CRITICAL_SECTION section;
104 static void read_protocol_data(stgmed_buf_t *stgmed_buf)
110 do hres = IInternetProtocol_Read(stgmed_buf->protocol, buf, sizeof(buf), &read);
114 static void dump_BINDINFO(BINDINFO *bi)
116 static const char * const BINDINFOF_str[] = {
118 "BINDINFOF_URLENCODESTGMEDDATA",
119 "BINDINFOF_URLENCODEDEXTRAINFO"
122 static const char * const BINDVERB_str[] = {
136 " %d, %08x, %d, %d\n"
142 bi->cbSize, debugstr_w(bi->szExtraInfo),
143 bi->stgmedData.tymed, bi->stgmedData.u.hGlobal, bi->stgmedData.pUnkForRelease,
144 bi->grfBindInfoF > BINDINFOF_URLENCODEDEXTRAINFO
145 ? "unknown" : BINDINFOF_str[bi->grfBindInfoF],
146 bi->dwBindVerb > BINDVERB_CUSTOM
147 ? "unknown" : BINDVERB_str[bi->dwBindVerb],
148 debugstr_w(bi->szCustomVerb),
149 bi->cbstgmedData, bi->dwOptions, bi->dwOptionsFlags, bi->dwCodePage,
150 bi->securityAttributes.nLength,
151 bi->securityAttributes.lpSecurityDescriptor,
152 bi->securityAttributes.bInheritHandle,
153 debugstr_guid(&bi->iid),
154 bi->pUnk, bi->dwReserved
158 static void mime_available(Binding *This, LPCWSTR mime)
160 heap_free(This->mime);
161 This->mime = heap_strdupW(mime);
163 if(!This->mime || !This->report_mime)
166 IBindStatusCallback_OnProgress(This->callback, 0, 0, BINDSTATUS_MIMETYPEAVAILABLE, This->mime);
168 This->clipboard_format = RegisterClipboardFormatW(This->mime);
171 static void stop_binding(Binding *binding, HRESULT hres, LPCWSTR str)
173 if(binding->state & BINDING_LOCKED) {
174 IInternetProtocolEx_UnlockRequest(&binding->protocol->IInternetProtocolEx_iface);
175 binding->state &= ~BINDING_LOCKED;
178 if(!(binding->state & BINDING_STOPPED)) {
179 binding->state |= BINDING_STOPPED;
181 IBindStatusCallback_OnStopBinding(binding->callback, hres, str);
182 binding->hres = hres;
186 static LPWSTR get_mime_clsid(LPCWSTR mime, CLSID *clsid)
188 LPWSTR key_name, ret;
189 DWORD res, type, size;
194 static const WCHAR mime_keyW[] =
195 {'M','I','M','E','\\','D','a','t','a','b','a','s','e','\\',
196 'C','o','n','t','e','n','t',' ','T','y','p','e','\\'};
197 static const WCHAR clsidW[] = {'C','L','S','I','D',0};
199 len = strlenW(mime)+1;
200 key_name = heap_alloc(sizeof(mime_keyW) + len*sizeof(WCHAR));
201 memcpy(key_name, mime_keyW, sizeof(mime_keyW));
202 strcpyW(key_name + sizeof(mime_keyW)/sizeof(WCHAR), mime);
204 res = RegOpenKeyW(HKEY_CLASSES_ROOT, key_name, &hkey);
206 if(res != ERROR_SUCCESS) {
207 WARN("Could not open MIME key: %x\n", res);
211 size = 50*sizeof(WCHAR);
212 ret = heap_alloc(size);
213 res = RegQueryValueExW(hkey, clsidW, NULL, &type, (LPBYTE)ret, &size);
215 if(res != ERROR_SUCCESS) {
216 WARN("Could not get CLSID: %08x\n", res);
221 hres = CLSIDFromString(ret, clsid);
223 WARN("Could not parse CLSID: %08x\n", hres);
231 static void load_doc_mon(Binding *binding, IPersistMoniker *persist)
236 hres = CreateAsyncBindCtxEx(binding->bctx, 0, NULL, NULL, &bctx, 0);
238 WARN("CreateAsyncBindCtxEx failed: %08x\n", hres);
242 IBindCtx_RevokeObjectParam(bctx, bscb_holderW);
243 IBindCtx_RegisterObjectParam(bctx, cbinding_contextW, (IUnknown*)&binding->IBinding_iface);
245 hres = IPersistMoniker_Load(persist, binding->download_state == END_DOWNLOAD, binding->mon, bctx, 0x12);
246 IBindCtx_RevokeObjectParam(bctx, cbinding_contextW);
247 IBindCtx_Release(bctx);
249 FIXME("Load failed: %08x\n", hres);
252 static HRESULT create_mime_object(Binding *binding, const CLSID *clsid, LPCWSTR clsid_str)
254 IPersistMoniker *persist;
257 hres = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
258 &binding->iid, (void**)&binding->obj);
260 WARN("CoCreateInstance failed: %08x\n", hres);
261 return INET_E_CANNOT_INSTANTIATE_OBJECT;
264 binding->state |= BINDING_OBJAVAIL;
266 hres = IUnknown_QueryInterface(binding->obj, &IID_IPersistMoniker, (void**)&persist);
267 if(SUCCEEDED(hres)) {
270 hres = IPersistMoniker_QueryInterface(persist, &IID_IMonikerProp, (void**)&prop);
271 if(SUCCEEDED(hres)) {
272 IMonikerProp_PutProperty(prop, MIMETYPEPROP, binding->mime);
273 IMonikerProp_PutProperty(prop, CLASSIDPROP, clsid_str);
274 IMonikerProp_Release(prop);
277 load_doc_mon(binding, persist);
279 IPersistMoniker_Release(persist);
281 FIXME("Could not get IPersistMoniker: %08x\n", hres);
282 /* FIXME: Try query IPersistFile */
285 IBindStatusCallback_OnObjectAvailable(binding->callback, &binding->iid, binding->obj);
290 static void create_object(Binding *binding)
297 FIXME("MIME not available\n");
301 if(!(clsid_str = get_mime_clsid(binding->mime, &clsid))) {
302 FIXME("Could not find object for MIME %s\n", debugstr_w(binding->mime));
306 IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_CLASSIDAVAILABLE, clsid_str);
307 IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_BEGINSYNCOPERATION, NULL);
309 hres = create_mime_object(binding, &clsid, clsid_str);
310 heap_free(clsid_str);
312 IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_ENDSYNCOPERATION, NULL);
314 stop_binding(binding, hres, NULL);
316 IInternetProtocolEx_Terminate(&binding->protocol->IInternetProtocolEx_iface, 0);
319 static void cache_file_available(Binding *This, const WCHAR *file_name)
321 heap_free(This->stgmed_buf->cache_file);
322 This->stgmed_buf->cache_file = heap_strdupW(file_name);
324 if(This->use_cache_file) {
325 This->stgmed_buf->file = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
326 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
327 if(This->stgmed_buf->file == INVALID_HANDLE_VALUE)
328 WARN("CreateFile failed: %u\n", GetLastError());
332 static inline stgmed_buf_t *impl_from_IUnknown(IUnknown *iface)
334 return CONTAINING_RECORD(iface, stgmed_buf_t, IUnknown_iface);
337 static HRESULT WINAPI StgMedUnk_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
339 stgmed_buf_t *This = impl_from_IUnknown(iface);
343 if(IsEqualGUID(riid, &IID_IUnknown)) {
344 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
346 *ppv = &This->IUnknown_iface;
347 IUnknown_AddRef(&This->IUnknown_iface);
351 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
352 return E_NOINTERFACE;
355 static ULONG WINAPI StgMedUnk_AddRef(IUnknown *iface)
357 stgmed_buf_t *This = impl_from_IUnknown(iface);
358 LONG ref = InterlockedIncrement(&This->ref);
360 TRACE("(%p) ref=%d\n", This, ref);
365 static ULONG WINAPI StgMedUnk_Release(IUnknown *iface)
367 stgmed_buf_t *This = impl_from_IUnknown(iface);
368 LONG ref = InterlockedDecrement(&This->ref);
370 TRACE("(%p) ref=%d\n", This, ref);
373 if(This->file != INVALID_HANDLE_VALUE)
374 CloseHandle(This->file);
375 IInternetProtocol_Release(This->protocol);
376 heap_free(This->cache_file);
379 URLMON_UnlockModule();
385 static const IUnknownVtbl StgMedUnkVtbl = {
386 StgMedUnk_QueryInterface,
391 static stgmed_buf_t *create_stgmed_buf(IInternetProtocolEx *protocol)
393 stgmed_buf_t *ret = heap_alloc(sizeof(*ret));
395 ret->IUnknown_iface.lpVtbl = &StgMedUnkVtbl;
397 ret->file = INVALID_HANDLE_VALUE;
399 ret->cache_file = NULL;
401 IInternetProtocol_AddRef(protocol);
402 ret->protocol = protocol;
410 stgmed_obj_t stgmed_obj;
411 IStream IStream_iface;
418 static inline ProtocolStream *impl_from_IStream(IStream *iface)
420 return CONTAINING_RECORD(iface, ProtocolStream, IStream_iface);
423 static HRESULT WINAPI ProtocolStream_QueryInterface(IStream *iface,
424 REFIID riid, void **ppv)
426 ProtocolStream *This = impl_from_IStream(iface);
430 if(IsEqualGUID(&IID_IUnknown, riid)) {
431 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
432 *ppv = &This->IStream_iface;
433 }else if(IsEqualGUID(&IID_ISequentialStream, riid)) {
434 TRACE("(%p)->(IID_ISequentialStream %p)\n", This, ppv);
435 *ppv = &This->IStream_iface;
436 }else if(IsEqualGUID(&IID_IStream, riid)) {
437 TRACE("(%p)->(IID_IStream %p)\n", This, ppv);
438 *ppv = &This->IStream_iface;
442 IStream_AddRef(&This->IStream_iface);
446 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
447 return E_NOINTERFACE;
450 static ULONG WINAPI ProtocolStream_AddRef(IStream *iface)
452 ProtocolStream *This = impl_from_IStream(iface);
453 LONG ref = InterlockedIncrement(&This->ref);
455 TRACE("(%p) ref=%d\n", This, ref);
460 static ULONG WINAPI ProtocolStream_Release(IStream *iface)
462 ProtocolStream *This = impl_from_IStream(iface);
463 LONG ref = InterlockedDecrement(&This->ref);
465 TRACE("(%p) ref=%d\n", This, ref);
468 IUnknown_Release(&This->buf->IUnknown_iface);
471 URLMON_UnlockModule();
477 static HRESULT WINAPI ProtocolStream_Read(IStream *iface, void *pv,
478 ULONG cb, ULONG *pcbRead)
480 ProtocolStream *This = impl_from_IStream(iface);
484 TRACE("(%p)->(%p %d %p)\n", This, pv, cb, pcbRead);
486 if(This->buf->file == INVALID_HANDLE_VALUE) {
487 hres = This->buf->hres = IInternetProtocol_Read(This->buf->protocol, (PBYTE)pv, cb, &read);
489 hres = ReadFile(This->buf->file, pv, cb, &read, NULL) ? S_OK : INET_E_DOWNLOAD_FAILURE;
495 if(hres == E_PENDING)
497 else if(FAILED(hres))
498 FIXME("Read failed: %08x\n", hres);
500 return read ? S_OK : S_FALSE;
503 static HRESULT WINAPI ProtocolStream_Write(IStream *iface, const void *pv,
504 ULONG cb, ULONG *pcbWritten)
506 ProtocolStream *This = impl_from_IStream(iface);
508 TRACE("(%p)->(%p %d %p)\n", This, pv, cb, pcbWritten);
510 return STG_E_ACCESSDENIED;
513 static HRESULT WINAPI ProtocolStream_Seek(IStream *iface, LARGE_INTEGER dlibMove,
514 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
516 ProtocolStream *This = impl_from_IStream(iface);
517 FIXME("(%p)->(%d %08x %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
521 static HRESULT WINAPI ProtocolStream_SetSize(IStream *iface, ULARGE_INTEGER libNewSize)
523 ProtocolStream *This = impl_from_IStream(iface);
524 FIXME("(%p)->(%d)\n", This, libNewSize.u.LowPart);
528 static HRESULT WINAPI ProtocolStream_CopyTo(IStream *iface, IStream *pstm,
529 ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten)
531 ProtocolStream *This = impl_from_IStream(iface);
532 FIXME("(%p)->(%p %d %p %p)\n", This, pstm, cb.u.LowPart, pcbRead, pcbWritten);
536 static HRESULT WINAPI ProtocolStream_Commit(IStream *iface, DWORD grfCommitFlags)
538 ProtocolStream *This = impl_from_IStream(iface);
540 TRACE("(%p)->(%08x)\n", This, grfCommitFlags);
545 static HRESULT WINAPI ProtocolStream_Revert(IStream *iface)
547 ProtocolStream *This = impl_from_IStream(iface);
549 TRACE("(%p)\n", This);
554 static HRESULT WINAPI ProtocolStream_LockRegion(IStream *iface, ULARGE_INTEGER libOffset,
555 ULARGE_INTEGER cb, DWORD dwLockType)
557 ProtocolStream *This = impl_from_IStream(iface);
558 FIXME("(%p)->(%d %d %d)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
562 static HRESULT WINAPI ProtocolStream_UnlockRegion(IStream *iface,
563 ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
565 ProtocolStream *This = impl_from_IStream(iface);
566 FIXME("(%p)->(%d %d %d)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
570 static HRESULT WINAPI ProtocolStream_Stat(IStream *iface, STATSTG *pstatstg,
573 ProtocolStream *This = impl_from_IStream(iface);
574 TRACE("(%p)->(%p %08x)\n", This, pstatstg, dwStatFlag);
579 memset(pstatstg, 0, sizeof(STATSTG));
581 if(!(dwStatFlag&STATFLAG_NONAME) && This->buf->cache_file) {
582 pstatstg->pwcsName = CoTaskMemAlloc((lstrlenW(This->buf->cache_file)+1)*sizeof(WCHAR));
583 if(!pstatstg->pwcsName)
584 return STG_E_INSUFFICIENTMEMORY;
586 lstrcpyW(pstatstg->pwcsName, This->buf->cache_file);
589 pstatstg->type = STGTY_STREAM;
590 if(This->buf->file != INVALID_HANDLE_VALUE) {
591 GetFileSizeEx(This->buf->file, (PLARGE_INTEGER)&pstatstg->cbSize);
592 GetFileTime(This->buf->file, &pstatstg->ctime, &pstatstg->atime, &pstatstg->mtime);
593 if(pstatstg->cbSize.QuadPart)
594 pstatstg->grfMode = GENERIC_READ;
600 static HRESULT WINAPI ProtocolStream_Clone(IStream *iface, IStream **ppstm)
602 ProtocolStream *This = impl_from_IStream(iface);
603 FIXME("(%p)->(%p)\n", This, ppstm);
607 static const IStreamVtbl ProtocolStreamVtbl = {
608 ProtocolStream_QueryInterface,
609 ProtocolStream_AddRef,
610 ProtocolStream_Release,
612 ProtocolStream_Write,
614 ProtocolStream_SetSize,
615 ProtocolStream_CopyTo,
616 ProtocolStream_Commit,
617 ProtocolStream_Revert,
618 ProtocolStream_LockRegion,
619 ProtocolStream_UnlockRegion,
624 static void stgmed_stream_release(stgmed_obj_t *obj)
626 ProtocolStream *stream = (ProtocolStream*)obj;
627 IStream_Release(&stream->IStream_iface);
630 static HRESULT stgmed_stream_fill_stgmed(stgmed_obj_t *obj, STGMEDIUM *stgmed)
632 ProtocolStream *stream = (ProtocolStream*)obj;
634 stgmed->tymed = TYMED_ISTREAM;
635 stgmed->u.pstm = &stream->IStream_iface;
636 stgmed->pUnkForRelease = &stream->buf->IUnknown_iface;
641 static HRESULT stgmed_stream_get_result(stgmed_obj_t *obj, DWORD bindf, void **result)
643 ProtocolStream *stream = (ProtocolStream*)obj;
645 if(!(bindf & BINDF_ASYNCHRONOUS) && stream->buf->file == INVALID_HANDLE_VALUE
646 && stream->buf->hres != S_FALSE)
647 return INET_E_DATA_NOT_AVAILABLE;
649 IStream_AddRef(&stream->IStream_iface);
650 *result = &stream->IStream_iface;
654 static const stgmed_obj_vtbl stgmed_stream_vtbl = {
655 stgmed_stream_release,
656 stgmed_stream_fill_stgmed,
657 stgmed_stream_get_result
661 stgmed_obj_t stgmed_obj;
665 static stgmed_obj_t *create_stgmed_stream(stgmed_buf_t *buf)
667 ProtocolStream *ret = heap_alloc(sizeof(ProtocolStream));
669 ret->stgmed_obj.vtbl = &stgmed_stream_vtbl;
670 ret->IStream_iface.lpVtbl = &ProtocolStreamVtbl;
673 IUnknown_AddRef(&buf->IUnknown_iface);
678 return &ret->stgmed_obj;
681 static void stgmed_file_release(stgmed_obj_t *obj)
683 stgmed_file_obj_t *file_obj = (stgmed_file_obj_t*)obj;
685 IUnknown_Release(&file_obj->buf->IUnknown_iface);
689 static HRESULT stgmed_file_fill_stgmed(stgmed_obj_t *obj, STGMEDIUM *stgmed)
691 stgmed_file_obj_t *file_obj = (stgmed_file_obj_t*)obj;
693 if(!file_obj->buf->cache_file) {
694 WARN("cache_file not set\n");
695 return INET_E_DATA_NOT_AVAILABLE;
698 read_protocol_data(file_obj->buf);
700 stgmed->tymed = TYMED_FILE;
701 stgmed->u.lpszFileName = file_obj->buf->cache_file;
702 stgmed->pUnkForRelease = &file_obj->buf->IUnknown_iface;
707 static HRESULT stgmed_file_get_result(stgmed_obj_t *obj, DWORD bindf, void **result)
709 return bindf & BINDF_ASYNCHRONOUS ? MK_S_ASYNCHRONOUS : S_OK;
712 static const stgmed_obj_vtbl stgmed_file_vtbl = {
714 stgmed_file_fill_stgmed,
715 stgmed_file_get_result
718 static stgmed_obj_t *create_stgmed_file(stgmed_buf_t *buf)
720 stgmed_file_obj_t *ret = heap_alloc(sizeof(*ret));
722 ret->stgmed_obj.vtbl = &stgmed_file_vtbl;
724 IUnknown_AddRef(&buf->IUnknown_iface);
727 return &ret->stgmed_obj;
730 static inline Binding *impl_from_IBinding(IBinding *iface)
732 return CONTAINING_RECORD(iface, Binding, IBinding_iface);
735 static HRESULT WINAPI Binding_QueryInterface(IBinding *iface, REFIID riid, void **ppv)
737 Binding *This = impl_from_IBinding(iface);
741 if(IsEqualGUID(&IID_IUnknown, riid)) {
742 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
743 *ppv = &This->IBinding_iface;
744 }else if(IsEqualGUID(&IID_IBinding, riid)) {
745 TRACE("(%p)->(IID_IBinding %p)\n", This, ppv);
746 *ppv = &This->IBinding_iface;
747 }else if(IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
748 TRACE("(%p)->(IID_IInternetProtocolSink %p)\n", This, ppv);
749 *ppv = &This->IInternetProtocolSink_iface;
750 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
751 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
752 *ppv = &This->IInternetBindInfo_iface;
753 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
754 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
755 *ppv = &This->IServiceProvider_iface;
756 }else if(IsEqualGUID(&IID_IWinInetInfo, riid)) {
757 TRACE("(%p)->(IID_IWinInetInfo %p)\n", This, ppv);
759 /* NOTE: This violidates COM rules, but tests prove that we should do it */
760 if(!This->protocol->wininet_info)
761 return E_NOINTERFACE;
763 *ppv = &This->IWinInetHttpInfo_iface;
764 }else if(IsEqualGUID(&IID_IWinInetHttpInfo, riid)) {
765 TRACE("(%p)->(IID_IWinInetHttpInfo %p)\n", This, ppv);
767 if(!This->protocol->wininet_http_info)
768 return E_NOINTERFACE;
770 *ppv = &This->IWinInetHttpInfo_iface;
774 IBinding_AddRef(&This->IBinding_iface);
778 WARN("Unsupported interface %s\n", debugstr_guid(riid));
779 return E_NOINTERFACE;
782 static ULONG WINAPI Binding_AddRef(IBinding *iface)
784 Binding *This = impl_from_IBinding(iface);
785 LONG ref = InterlockedIncrement(&This->ref);
787 TRACE("(%p) ref=%d\n", This, ref);
792 static ULONG WINAPI Binding_Release(IBinding *iface)
794 Binding *This = impl_from_IBinding(iface);
795 LONG ref = InterlockedDecrement(&This->ref);
797 TRACE("(%p) ref=%d\n", This, ref);
801 release_notif_hwnd(This->notif_hwnd);
803 IMoniker_Release(This->mon);
805 IBindStatusCallback_Release(This->callback);
807 IInternetProtocolEx_Release(&This->protocol->IInternetProtocolEx_iface);
808 if(This->service_provider)
809 IServiceProvider_Release(This->service_provider);
811 IUnknown_Release(&This->stgmed_buf->IUnknown_iface);
813 This->stgmed_obj->vtbl->release(This->stgmed_obj);
815 IUnknown_Release(This->obj);
817 IBindCtx_Release(This->bctx);
819 ReleaseBindInfo(&This->bindinfo);
820 This->section.DebugInfo->Spare[0] = 0;
821 DeleteCriticalSection(&This->section);
822 SysFreeString(This->url);
823 heap_free(This->mime);
824 heap_free(This->redirect_url);
827 URLMON_UnlockModule();
833 static HRESULT WINAPI Binding_Abort(IBinding *iface)
835 Binding *This = impl_from_IBinding(iface);
838 TRACE("(%p)\n", This);
840 if(This->state & BINDING_ABORTED)
843 hres = IInternetProtocolEx_Abort(&This->protocol->IInternetProtocolEx_iface, E_ABORT,
848 This->state |= BINDING_ABORTED;
852 static HRESULT WINAPI Binding_Suspend(IBinding *iface)
854 Binding *This = impl_from_IBinding(iface);
855 FIXME("(%p)\n", This);
859 static HRESULT WINAPI Binding_Resume(IBinding *iface)
861 Binding *This = impl_from_IBinding(iface);
862 FIXME("(%p)\n", This);
866 static HRESULT WINAPI Binding_SetPriority(IBinding *iface, LONG nPriority)
868 Binding *This = impl_from_IBinding(iface);
869 FIXME("(%p)->(%d)\n", This, nPriority);
873 static HRESULT WINAPI Binding_GetPriority(IBinding *iface, LONG *pnPriority)
875 Binding *This = impl_from_IBinding(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 = impl_from_IBinding(iface);
884 FIXME("(%p)->(%p %p %p %p)\n", This, pclsidProtocol, pdwResult, pszResult, pdwReserved);
888 static const IBindingVtbl BindingVtbl = {
889 Binding_QueryInterface,
897 Binding_GetBindResult
900 static Binding *get_bctx_binding(IBindCtx *bctx)
906 hres = IBindCtx_GetObjectParam(bctx, cbinding_contextW, &unk);
910 hres = IUnknown_QueryInterface(unk, &IID_IBinding, (void**)&binding);
911 IUnknown_Release(unk);
915 if (binding->lpVtbl != &BindingVtbl)
917 return impl_from_IBinding(binding);
920 static inline Binding *impl_from_IInternetProtocolSink(IInternetProtocolSink *iface)
922 return CONTAINING_RECORD(iface, Binding, IInternetProtocolSink_iface);
925 static HRESULT WINAPI InternetProtocolSink_QueryInterface(IInternetProtocolSink *iface,
926 REFIID riid, void **ppv)
928 Binding *This = impl_from_IInternetProtocolSink(iface);
929 return IBinding_QueryInterface(&This->IBinding_iface, riid, ppv);
932 static ULONG WINAPI InternetProtocolSink_AddRef(IInternetProtocolSink *iface)
934 Binding *This = impl_from_IInternetProtocolSink(iface);
935 return IBinding_AddRef(&This->IBinding_iface);
938 static ULONG WINAPI InternetProtocolSink_Release(IInternetProtocolSink *iface)
940 Binding *This = impl_from_IInternetProtocolSink(iface);
941 return IBinding_Release(&This->IBinding_iface);
944 static HRESULT WINAPI InternetProtocolSink_Switch(IInternetProtocolSink *iface,
945 PROTOCOLDATA *pProtocolData)
947 Binding *This = impl_from_IInternetProtocolSink(iface);
949 WARN("(%p)->(%p)\n", This, pProtocolData);
954 static void on_progress(Binding *This, ULONG progress, ULONG progress_max,
955 ULONG status_code, LPCWSTR status_text)
957 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
958 status_code, status_text);
961 static HRESULT WINAPI InternetProtocolSink_ReportProgress(IInternetProtocolSink *iface,
962 ULONG ulStatusCode, LPCWSTR szStatusText)
964 Binding *This = impl_from_IInternetProtocolSink(iface);
966 TRACE("(%p)->(%u %s)\n", This, ulStatusCode, debugstr_w(szStatusText));
968 switch(ulStatusCode) {
969 case BINDSTATUS_FINDINGRESOURCE:
970 on_progress(This, 0, 0, BINDSTATUS_FINDINGRESOURCE, szStatusText);
972 case BINDSTATUS_CONNECTING:
973 on_progress(This, 0, 0, BINDSTATUS_CONNECTING, szStatusText);
975 case BINDSTATUS_REDIRECTING:
976 heap_free(This->redirect_url);
977 This->redirect_url = heap_strdupW(szStatusText);
978 on_progress(This, 0, 0, BINDSTATUS_REDIRECTING, szStatusText);
980 case BINDSTATUS_BEGINDOWNLOADDATA:
982 case BINDSTATUS_SENDINGREQUEST:
983 on_progress(This, 0, 0, BINDSTATUS_SENDINGREQUEST, szStatusText);
985 case BINDSTATUS_PROTOCOLCLASSID:
987 case BINDSTATUS_MIMETYPEAVAILABLE:
988 case BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE:
989 mime_available(This, szStatusText);
991 case BINDSTATUS_CACHEFILENAMEAVAILABLE:
992 cache_file_available(This, szStatusText);
994 case BINDSTATUS_DECODING:
995 IBindStatusCallback_OnProgress(This->callback, 0, 0, BINDSTATUS_DECODING, szStatusText);
997 case BINDSTATUS_LOADINGMIMEHANDLER:
998 on_progress(This, 0, 0, BINDSTATUS_LOADINGMIMEHANDLER, szStatusText);
1000 case BINDSTATUS_DIRECTBIND: /* FIXME: Handle BINDSTATUS_DIRECTBIND in BindProtocol */
1001 This->report_mime = FALSE;
1003 case BINDSTATUS_ACCEPTRANGES:
1006 FIXME("Unhandled status code %d\n", ulStatusCode);
1013 static void report_data(Binding *This, DWORD bscf, ULONG progress, ULONG progress_max)
1015 FORMATETC formatetc = {0, NULL, 1, -1, TYMED_ISTREAM};
1016 BOOL sent_begindownloaddata = FALSE;
1018 TRACE("(%p)->(%d %u %u)\n", This, bscf, progress, progress_max);
1020 if(This->download_state == END_DOWNLOAD || (This->state & BINDING_ABORTED)) {
1021 read_protocol_data(This->stgmed_buf);
1025 if(This->state & BINDING_STOPPED)
1028 if(This->stgmed_buf->file != INVALID_HANDLE_VALUE)
1029 read_protocol_data(This->stgmed_buf);
1031 if(This->download_state == BEFORE_DOWNLOAD) {
1032 This->download_state = DOWNLOADING;
1033 sent_begindownloaddata = TRUE;
1034 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1035 BINDSTATUS_BEGINDOWNLOADDATA, This->url);
1037 if(This->stgmed_buf->cache_file)
1038 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1039 BINDSTATUS_CACHEFILENAMEAVAILABLE, This->stgmed_buf->cache_file);
1042 if(This->stgmed_buf->hres == S_FALSE || (bscf & BSCF_LASTDATANOTIFICATION)) {
1043 This->download_state = END_DOWNLOAD;
1044 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1045 BINDSTATUS_ENDDOWNLOADDATA, This->url);
1046 }else if(!sent_begindownloaddata) {
1047 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1048 BINDSTATUS_DOWNLOADINGDATA, This->url);
1051 if(This->state & (BINDING_STOPPED|BINDING_ABORTED))
1054 if(This->to_object) {
1055 if(!(This->state & BINDING_OBJAVAIL)) {
1056 IBinding_AddRef(&This->IBinding_iface);
1057 create_object(This);
1058 IBinding_Release(&This->IBinding_iface);
1064 if(!(This->state & BINDING_LOCKED)) {
1065 HRESULT hres = IInternetProtocolEx_LockRequest(
1066 &This->protocol->IInternetProtocolEx_iface, 0);
1068 This->state |= BINDING_LOCKED;
1071 hres = This->stgmed_obj->vtbl->fill_stgmed(This->stgmed_obj, &stgmed);
1073 stop_binding(This, hres, NULL);
1077 formatetc.tymed = stgmed.tymed;
1078 formatetc.cfFormat = This->clipboard_format;
1080 IBindStatusCallback_OnDataAvailable(This->callback, bscf, progress,
1081 &formatetc, &stgmed);
1083 if(This->download_state == END_DOWNLOAD)
1084 stop_binding(This, S_OK, NULL);
1088 static HRESULT WINAPI InternetProtocolSink_ReportData(IInternetProtocolSink *iface,
1089 DWORD grfBSCF, ULONG ulProgress, ULONG ulProgressMax)
1091 Binding *This = impl_from_IInternetProtocolSink(iface);
1093 TRACE("(%p)->(%d %u %u)\n", This, grfBSCF, ulProgress, ulProgressMax);
1095 report_data(This, grfBSCF, ulProgress, ulProgressMax);
1099 static HRESULT WINAPI InternetProtocolSink_ReportResult(IInternetProtocolSink *iface,
1100 HRESULT hrResult, DWORD dwError, LPCWSTR szResult)
1102 Binding *This = impl_from_IInternetProtocolSink(iface);
1104 TRACE("(%p)->(%08x %d %s)\n", This, hrResult, dwError, debugstr_w(szResult));
1106 stop_binding(This, hrResult, szResult);
1107 IInternetProtocolEx_Terminate(&This->protocol->IInternetProtocolEx_iface, 0);
1111 static const IInternetProtocolSinkVtbl InternetProtocolSinkVtbl = {
1112 InternetProtocolSink_QueryInterface,
1113 InternetProtocolSink_AddRef,
1114 InternetProtocolSink_Release,
1115 InternetProtocolSink_Switch,
1116 InternetProtocolSink_ReportProgress,
1117 InternetProtocolSink_ReportData,
1118 InternetProtocolSink_ReportResult
1121 static inline Binding *impl_from_IInternetBindInfo(IInternetBindInfo *iface)
1123 return CONTAINING_RECORD(iface, Binding, IInternetBindInfo_iface);
1126 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
1127 REFIID riid, void **ppv)
1129 Binding *This = impl_from_IInternetBindInfo(iface);
1130 return IBinding_QueryInterface(&This->IBinding_iface, riid, ppv);
1133 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
1135 Binding *This = impl_from_IInternetBindInfo(iface);
1136 return IBinding_AddRef(&This->IBinding_iface);
1139 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
1141 Binding *This = impl_from_IInternetBindInfo(iface);
1142 return IBinding_Release(&This->IBinding_iface);
1145 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
1146 DWORD *grfBINDF, BINDINFO *pbindinfo)
1148 Binding *This = impl_from_IInternetBindInfo(iface);
1150 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
1152 *grfBINDF = This->bindf;
1153 return CopyBindInfo(&This->bindinfo, pbindinfo);
1156 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
1157 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
1159 Binding *This = impl_from_IInternetBindInfo(iface);
1161 TRACE("(%p)->(%d %p %d %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
1163 switch(ulStringType) {
1164 case BINDSTRING_ACCEPT_MIMES: {
1165 static const WCHAR wszMimes[] = {'*','/','*',0};
1167 if(!ppwzStr || !pcElFetched)
1168 return E_INVALIDARG;
1170 ppwzStr[0] = CoTaskMemAlloc(sizeof(wszMimes));
1171 memcpy(ppwzStr[0], wszMimes, sizeof(wszMimes));
1175 case BINDSTRING_USER_AGENT: {
1176 IInternetBindInfo *bindinfo = NULL;
1179 hres = IBindStatusCallback_QueryInterface(This->callback, &IID_IInternetBindInfo,
1184 hres = IInternetBindInfo_GetBindString(bindinfo, ulStringType, ppwzStr,
1186 IInternetBindInfo_Release(bindinfo);
1190 case BINDSTRING_URL: {
1191 DWORD size = (SysStringLen(This->url)+1) * sizeof(WCHAR);
1193 if(!ppwzStr || !pcElFetched)
1194 return E_INVALIDARG;
1196 *ppwzStr = CoTaskMemAlloc(size);
1197 memcpy(*ppwzStr, This->url, size);
1203 FIXME("not supported string type %d\n", ulStringType);
1207 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
1208 InternetBindInfo_QueryInterface,
1209 InternetBindInfo_AddRef,
1210 InternetBindInfo_Release,
1211 InternetBindInfo_GetBindInfo,
1212 InternetBindInfo_GetBindString
1215 static inline Binding *impl_from_IWinInetHttpInfo(IWinInetHttpInfo *iface)
1217 return CONTAINING_RECORD(iface, Binding, IWinInetHttpInfo_iface);
1220 static HRESULT WINAPI WinInetHttpInfo_QueryInterface(IWinInetHttpInfo *iface, REFIID riid, void **ppv)
1222 Binding *This = impl_from_IWinInetHttpInfo(iface);
1223 return IBinding_QueryInterface(&This->IBinding_iface, riid, ppv);
1226 static ULONG WINAPI WinInetHttpInfo_AddRef(IWinInetHttpInfo *iface)
1228 Binding *This = impl_from_IWinInetHttpInfo(iface);
1229 return IBinding_AddRef(&This->IBinding_iface);
1232 static ULONG WINAPI WinInetHttpInfo_Release(IWinInetHttpInfo *iface)
1234 Binding *This = impl_from_IWinInetHttpInfo(iface);
1235 return IBinding_Release(&This->IBinding_iface);
1238 static HRESULT WINAPI WinInetHttpInfo_QueryOption(IWinInetHttpInfo *iface, DWORD dwOption,
1239 void *pBuffer, DWORD *pcbBuffer)
1241 Binding *This = impl_from_IWinInetHttpInfo(iface);
1242 TRACE("(%p)->(%x %p %p)\n", This, dwOption, pBuffer, pcbBuffer);
1244 if(!This->protocol->wininet_info)
1247 return IWinInetInfo_QueryOption(This->protocol->wininet_info,
1248 dwOption, pBuffer, pcbBuffer);
1251 static HRESULT WINAPI WinInetHttpInfo_QueryInfo(IWinInetHttpInfo *iface, DWORD dwOption,
1252 void *pBuffer, DWORD *pcbBuffer, DWORD *pdwFlags, DWORD *pdwReserved)
1254 Binding *This = impl_from_IWinInetHttpInfo(iface);
1255 TRACE("(%p)->(%x %p %p %p %p)\n", This, dwOption, pBuffer, pcbBuffer, pdwFlags, pdwReserved);
1257 if(!This->protocol->wininet_http_info)
1260 return IWinInetHttpInfo_QueryInfo(This->protocol->wininet_http_info,
1261 dwOption, pBuffer, pcbBuffer, pdwFlags, pdwReserved);
1264 static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl = {
1265 WinInetHttpInfo_QueryInterface,
1266 WinInetHttpInfo_AddRef,
1267 WinInetHttpInfo_Release,
1268 WinInetHttpInfo_QueryOption,
1269 WinInetHttpInfo_QueryInfo
1272 static inline Binding *impl_from_IServiceProvider(IServiceProvider *iface)
1274 return CONTAINING_RECORD(iface, Binding, IServiceProvider_iface);
1277 static HRESULT WINAPI ServiceProvider_QueryInterface(IServiceProvider *iface,
1278 REFIID riid, void **ppv)
1280 Binding *This = impl_from_IServiceProvider(iface);
1281 return IBinding_QueryInterface(&This->IBinding_iface, riid, ppv);
1284 static ULONG WINAPI ServiceProvider_AddRef(IServiceProvider *iface)
1286 Binding *This = impl_from_IServiceProvider(iface);
1287 return IBinding_AddRef(&This->IBinding_iface);
1290 static ULONG WINAPI ServiceProvider_Release(IServiceProvider *iface)
1292 Binding *This = impl_from_IServiceProvider(iface);
1293 return IBinding_Release(&This->IBinding_iface);
1296 static HRESULT WINAPI ServiceProvider_QueryService(IServiceProvider *iface,
1297 REFGUID guidService, REFIID riid, void **ppv)
1299 Binding *This = impl_from_IServiceProvider(iface);
1302 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
1304 if(This->service_provider) {
1305 hres = IServiceProvider_QueryService(This->service_provider, guidService,
1311 WARN("unknown service %s\n", debugstr_guid(guidService));
1312 return E_NOINTERFACE;
1315 static const IServiceProviderVtbl ServiceProviderVtbl = {
1316 ServiceProvider_QueryInterface,
1317 ServiceProvider_AddRef,
1318 ServiceProvider_Release,
1319 ServiceProvider_QueryService
1322 static HRESULT get_callback(IBindCtx *pbc, IBindStatusCallback **callback)
1327 hres = IBindCtx_GetObjectParam(pbc, bscb_holderW, &unk);
1329 return create_default_callback(callback);
1331 hres = IUnknown_QueryInterface(unk, &IID_IBindStatusCallback, (void**)callback);
1332 IUnknown_Release(unk);
1336 static BOOL is_urlmon_protocol(IUri *uri)
1341 hres = IUri_GetScheme(uri, &scheme);
1346 case URL_SCHEME_FILE:
1347 case URL_SCHEME_FTP:
1348 case URL_SCHEME_GOPHER:
1349 case URL_SCHEME_HTTP:
1350 case URL_SCHEME_HTTPS:
1358 static HRESULT Binding_Create(IMoniker *mon, Binding *binding_ctx, IUri *uri, IBindCtx *pbc,
1359 BOOL to_obj, REFIID riid, Binding **binding)
1364 URLMON_LockModule();
1366 ret = heap_alloc_zero(sizeof(Binding));
1368 ret->IBinding_iface.lpVtbl = &BindingVtbl;
1369 ret->IInternetProtocolSink_iface.lpVtbl = &InternetProtocolSinkVtbl;
1370 ret->IInternetBindInfo_iface.lpVtbl = &InternetBindInfoVtbl;
1371 ret->IWinInetHttpInfo_iface.lpVtbl = &WinInetHttpInfoVtbl;
1372 ret->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
1376 ret->to_object = to_obj;
1378 ret->notif_hwnd = get_notif_hwnd();
1379 ret->report_mime = !binding_ctx;
1380 ret->download_state = BEFORE_DOWNLOAD;
1383 IBindCtx_AddRef(pbc);
1388 IMoniker_AddRef(mon);
1392 ret->bindinfo.cbSize = sizeof(BINDINFO);
1394 InitializeCriticalSection(&ret->section);
1395 ret->section.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": Binding.section");
1397 hres = get_callback(pbc, &ret->callback);
1399 WARN("Could not get IBindStatusCallback\n");
1400 IBinding_Release(&ret->IBinding_iface);
1404 IBindStatusCallback_QueryInterface(ret->callback, &IID_IServiceProvider,
1405 (void**)&ret->service_provider);
1408 ret->protocol = binding_ctx->protocol;
1409 IInternetProtocolEx_AddRef(&ret->protocol->IInternetProtocolEx_iface);
1411 hres = create_binding_protocol(TRUE, &ret->protocol);
1413 WARN("Could not get protocol handler\n");
1414 IBinding_Release(&ret->IBinding_iface);
1419 hres = IBindStatusCallback_GetBindInfo(ret->callback, &ret->bindf, &ret->bindinfo);
1421 WARN("GetBindInfo failed: %08x\n", hres);
1422 IBinding_Release(&ret->IBinding_iface);
1426 TRACE("bindf %08x\n", ret->bindf);
1427 dump_BINDINFO(&ret->bindinfo);
1429 ret->bindf |= BINDF_FROMURLMON;
1431 ret->bindinfo.dwOptions |= 0x100000;
1433 if(!(ret->bindf & BINDF_ASYNCHRONOUS) || !(ret->bindf & BINDF_PULLDATA)) {
1434 ret->bindf |= BINDF_NEEDFILE;
1435 ret->use_cache_file = TRUE;
1436 }else if(!is_urlmon_protocol(uri)) {
1437 ret->bindf |= BINDF_NEEDFILE;
1440 hres = IUri_GetDisplayUri(uri, &ret->url);
1442 IBinding_Release(&ret->IBinding_iface);
1447 ret->stgmed_buf = binding_ctx->stgmed_buf;
1448 IUnknown_AddRef(&ret->stgmed_buf->IUnknown_iface);
1449 ret->clipboard_format = binding_ctx->clipboard_format;
1451 ret->stgmed_buf = create_stgmed_buf(&ret->protocol->IInternetProtocolEx_iface);
1455 ret->stgmed_obj = NULL;
1456 }else if(IsEqualGUID(&IID_IStream, riid)) {
1457 ret->stgmed_obj = create_stgmed_stream(ret->stgmed_buf);
1458 }else if(IsEqualGUID(&IID_IUnknown, riid)) {
1459 ret->bindf |= BINDF_NEEDFILE;
1460 ret->stgmed_obj = create_stgmed_file(ret->stgmed_buf);
1462 FIXME("Unsupported riid %s\n", debugstr_guid(riid));
1463 IBinding_Release(&ret->IBinding_iface);
1471 static HRESULT start_binding(IMoniker *mon, Binding *binding_ctx, IUri *uri, IBindCtx *pbc,
1472 BOOL to_obj, REFIID riid, Binding **ret)
1474 Binding *binding = NULL;
1478 hres = Binding_Create(mon, binding_ctx, uri, pbc, to_obj, riid, &binding);
1482 hres = IBindStatusCallback_OnStartBinding(binding->callback, 0, &binding->IBinding_iface);
1484 WARN("OnStartBinding failed: %08x\n", hres);
1485 if(hres != E_ABORT && hres != E_NOTIMPL)
1486 hres = INET_E_DOWNLOAD_FAILURE;
1488 stop_binding(binding, hres, NULL);
1489 IBinding_Release(&binding->IBinding_iface);
1494 set_binding_sink(binding->protocol, &binding->IInternetProtocolSink_iface,
1495 &binding->IInternetBindInfo_iface);
1496 if(binding_ctx->redirect_url)
1497 IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_REDIRECTING, binding_ctx->redirect_url);
1498 report_data(binding, BSCF_FIRSTDATANOTIFICATION | (binding_ctx->download_state == END_DOWNLOAD ? BSCF_LASTDATANOTIFICATION : 0),
1501 hres = IInternetProtocolEx_StartEx(&binding->protocol->IInternetProtocolEx_iface, uri,
1502 &binding->IInternetProtocolSink_iface, &binding->IInternetBindInfo_iface,
1503 PI_APARTMENTTHREADED|PI_MIMEVERIFICATION, 0);
1505 TRACE("start ret %08x\n", hres);
1507 if(FAILED(hres) && hres != E_PENDING) {
1508 stop_binding(binding, hres, NULL);
1509 IBinding_Release(&binding->IBinding_iface);
1515 while(!(binding->bindf & BINDF_ASYNCHRONOUS) &&
1516 !(binding->state & BINDING_STOPPED)) {
1517 MsgWaitForMultipleObjects(0, NULL, FALSE, 5000, QS_POSTMESSAGE);
1518 while (PeekMessageW(&msg, binding->notif_hwnd, WM_USER, WM_USER+117, PM_REMOVE|PM_NOYIELD)) {
1519 TranslateMessage(&msg);
1520 DispatchMessageW(&msg);
1528 HRESULT bind_to_storage(IUri *uri, IBindCtx *pbc, REFIID riid, void **ppv)
1530 Binding *binding = NULL, *binding_ctx;
1533 binding_ctx = get_bctx_binding(pbc);
1535 hres = start_binding(NULL, binding_ctx, uri, pbc, FALSE, riid, &binding);
1537 IBinding_Release(&binding_ctx->IBinding_iface);
1541 if(binding->hres == S_OK && binding->download_state != BEFORE_DOWNLOAD /* FIXME */) {
1542 if((binding->state & BINDING_STOPPED) && (binding->state & BINDING_LOCKED))
1543 IInternetProtocolEx_UnlockRequest(&binding->protocol->IInternetProtocolEx_iface);
1545 hres = binding->stgmed_obj->vtbl->get_result(binding->stgmed_obj, binding->bindf, ppv);
1546 }else if(binding->bindf & BINDF_ASYNCHRONOUS) {
1547 hres = MK_S_ASYNCHRONOUS;
1549 hres = FAILED(binding->hres) ? binding->hres : S_OK;
1552 IBinding_Release(&binding->IBinding_iface);
1557 HRESULT bind_to_object(IMoniker *mon, IUri *uri, IBindCtx *pbc, REFIID riid, void **ppv)
1564 hres = start_binding(mon, NULL, uri, pbc, TRUE, riid, &binding);
1568 if(binding->hres != S_OK) {
1569 hres = SUCCEEDED(binding->hres) ? S_OK : binding->hres;
1570 }else if(binding->bindf & BINDF_ASYNCHRONOUS) {
1571 hres = MK_S_ASYNCHRONOUS;
1573 *ppv = binding->obj;
1574 IUnknown_AddRef(binding->obj);
1578 IBinding_Release(&binding->IBinding_iface);