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 *lpIInternetProtocolSinkVtbl;
82 const IInternetBindInfoVtbl *lpInternetBindInfoVtbl;
83 const IWinInetHttpInfoVtbl *lpWinInetHttpInfoVtbl;
84 const IServiceProviderVtbl *lpServiceProviderVtbl;
88 IBindStatusCallback *callback;
89 IInternetProtocol *protocol;
90 IServiceProvider *service_provider;
92 stgmed_buf_t *stgmed_buf;
93 stgmed_obj_t *stgmed_obj;
99 UINT clipboard_format;
105 download_state_t download_state;
111 CRITICAL_SECTION section;
114 #define BINDING(x) ((IBinding*) &(x)->lpBindingVtbl)
115 #define BINDINF(x) ((IInternetBindInfo*) &(x)->lpInternetBindInfoVtbl)
116 #define INETINFO(x) ((IWinInetHttpInfo*) &(x)->lpWinInetHttpInfoVtbl)
117 #define SERVPROV(x) ((IServiceProvider*) &(x)->lpServiceProviderVtbl)
119 #define STREAM(x) ((IStream*) &(x)->lpStreamVtbl)
120 #define HTTPNEG2(x) ((IHttpNegotiate2*) &(x)->lpHttpNegotiate2Vtbl)
122 static void fill_stgmed_buffer(stgmed_buf_t *buf)
126 if(sizeof(buf->buf) == buf->size)
129 buf->hres = IInternetProtocol_Read(buf->protocol, buf->buf+buf->size,
130 sizeof(buf->buf)-buf->size, &read);
136 static void dump_BINDINFO(BINDINFO *bi)
138 static const char * const BINDINFOF_str[] = {
140 "BINDINFOF_URLENCODESTGMEDDATA",
141 "BINDINFOF_URLENCODEDEXTRAINFO"
144 static const char * const BINDVERB_str[] = {
158 " %d, %08x, %d, %d\n"
164 bi->cbSize, debugstr_w(bi->szExtraInfo),
165 bi->stgmedData.tymed, bi->stgmedData.u.hGlobal, bi->stgmedData.pUnkForRelease,
166 bi->grfBindInfoF > BINDINFOF_URLENCODEDEXTRAINFO
167 ? "unknown" : BINDINFOF_str[bi->grfBindInfoF],
168 bi->dwBindVerb > BINDVERB_CUSTOM
169 ? "unknown" : BINDVERB_str[bi->dwBindVerb],
170 debugstr_w(bi->szCustomVerb),
171 bi->cbstgmedData, bi->dwOptions, bi->dwOptionsFlags, bi->dwCodePage,
172 bi->securityAttributes.nLength,
173 bi->securityAttributes.lpSecurityDescriptor,
174 bi->securityAttributes.bInheritHandle,
175 debugstr_guid(&bi->iid),
176 bi->pUnk, bi->dwReserved
180 static void mime_available(Binding *This, LPCWSTR mime)
182 heap_free(This->mime);
183 This->mime = heap_strdupW(mime);
185 if(!This->mime || !This->report_mime)
188 IBindStatusCallback_OnProgress(This->callback, 0, 0, BINDSTATUS_MIMETYPEAVAILABLE, This->mime);
190 This->clipboard_format = RegisterClipboardFormatW(This->mime);
193 static void stop_binding(Binding *binding, HRESULT hres, LPCWSTR str)
195 if(binding->state & BINDING_LOCKED) {
196 IInternetProtocol_UnlockRequest(binding->protocol);
197 binding->state &= ~BINDING_LOCKED;
200 if(!(binding->state & BINDING_STOPPED)) {
201 binding->state |= BINDING_STOPPED;
203 IBindStatusCallback_OnStopBinding(binding->callback, hres, str);
204 binding->hres = hres;
208 static LPWSTR get_mime_clsid(LPCWSTR mime, CLSID *clsid)
210 LPWSTR key_name, ret;
211 DWORD res, type, size;
216 static const WCHAR mime_keyW[] =
217 {'M','I','M','E','\\','D','a','t','a','b','a','s','e','\\',
218 'C','o','n','t','e','n','t',' ','T','y','p','e','\\'};
219 static const WCHAR clsidW[] = {'C','L','S','I','D',0};
221 len = strlenW(mime)+1;
222 key_name = heap_alloc(sizeof(mime_keyW) + len*sizeof(WCHAR));
223 memcpy(key_name, mime_keyW, sizeof(mime_keyW));
224 strcpyW(key_name + sizeof(mime_keyW)/sizeof(WCHAR), mime);
226 res = RegOpenKeyW(HKEY_CLASSES_ROOT, key_name, &hkey);
228 if(res != ERROR_SUCCESS) {
229 WARN("Could not open MIME key: %x\n", res);
233 size = 50*sizeof(WCHAR);
234 ret = heap_alloc(size);
235 res = RegQueryValueExW(hkey, clsidW, NULL, &type, (LPBYTE)ret, &size);
237 if(res != ERROR_SUCCESS) {
238 WARN("Could not get CLSID: %08x\n", res);
243 hres = CLSIDFromString(ret, clsid);
245 WARN("Could not parse CLSID: %08x\n", hres);
253 static void load_doc_mon(Binding *binding, IPersistMoniker *persist)
258 hres = CreateAsyncBindCtxEx(binding->bctx, 0, NULL, NULL, &bctx, 0);
260 WARN("CreateAsyncBindCtxEx failed: %08x\n", hres);
264 IBindCtx_RevokeObjectParam(bctx, bscb_holderW);
265 IBindCtx_RegisterObjectParam(bctx, cbinding_contextW, (IUnknown*)BINDING(binding));
267 hres = IPersistMoniker_Load(persist, binding->download_state == END_DOWNLOAD, binding->mon, bctx, 0x12);
268 IBindCtx_RevokeObjectParam(bctx, cbinding_contextW);
269 IBindCtx_Release(bctx);
271 FIXME("Load failed: %08x\n", hres);
274 static HRESULT create_mime_object(Binding *binding, const CLSID *clsid, LPCWSTR clsid_str)
276 IPersistMoniker *persist;
279 hres = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
280 &binding->iid, (void**)&binding->obj);
282 WARN("CoCreateInstance failed: %08x\n", hres);
283 return INET_E_CANNOT_INSTANTIATE_OBJECT;
286 binding->state |= BINDING_OBJAVAIL;
288 hres = IUnknown_QueryInterface(binding->obj, &IID_IPersistMoniker, (void**)&persist);
289 if(SUCCEEDED(hres)) {
292 hres = IPersistMoniker_QueryInterface(persist, &IID_IMonikerProp, (void**)&prop);
293 if(SUCCEEDED(hres)) {
294 IMonikerProp_PutProperty(prop, MIMETYPEPROP, binding->mime);
295 IMonikerProp_PutProperty(prop, CLASSIDPROP, clsid_str);
296 IMonikerProp_Release(prop);
299 load_doc_mon(binding, persist);
301 IPersistMoniker_Release(persist);
303 FIXME("Could not get IPersistMoniker: %08x\n", hres);
304 /* FIXME: Try query IPersistFile */
307 IBindStatusCallback_OnObjectAvailable(binding->callback, &binding->iid, binding->obj);
312 static void create_object(Binding *binding)
319 FIXME("MIME not available\n");
323 if(!(clsid_str = get_mime_clsid(binding->mime, &clsid))) {
324 FIXME("Could not find object for MIME %s\n", debugstr_w(binding->mime));
328 IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_CLASSIDAVAILABLE, clsid_str);
329 IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_BEGINSYNCOPERATION, NULL);
331 hres = create_mime_object(binding, &clsid, clsid_str);
332 heap_free(clsid_str);
334 IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_ENDSYNCOPERATION, NULL);
336 stop_binding(binding, hres, NULL);
338 IInternetProtocol_Terminate(binding->protocol, 0);
341 #define STGMEDUNK_THIS(iface) DEFINE_THIS(stgmed_buf_t, Unknown, iface)
343 static HRESULT WINAPI StgMedUnk_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
345 stgmed_buf_t *This = STGMEDUNK_THIS(iface);
349 if(IsEqualGUID(riid, &IID_IUnknown)) {
350 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
352 *ppv = STGMEDUNK(This);
353 IUnknown_AddRef(STGMEDUNK(This));
357 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
358 return E_NOINTERFACE;
361 static ULONG WINAPI StgMedUnk_AddRef(IUnknown *iface)
363 stgmed_buf_t *This = STGMEDUNK_THIS(iface);
364 LONG ref = InterlockedIncrement(&This->ref);
366 TRACE("(%p) ref=%d\n", This, ref);
371 static ULONG WINAPI StgMedUnk_Release(IUnknown *iface)
373 stgmed_buf_t *This = STGMEDUNK_THIS(iface);
374 LONG ref = InterlockedDecrement(&This->ref);
376 TRACE("(%p) ref=%d\n", This, ref);
379 IInternetProtocol_Release(This->protocol);
380 heap_free(This->cache_file);
383 URLMON_UnlockModule();
389 #undef STGMEDUNK_THIS
391 static const IUnknownVtbl StgMedUnkVtbl = {
392 StgMedUnk_QueryInterface,
397 static stgmed_buf_t *create_stgmed_buf(IInternetProtocol *protocol)
399 stgmed_buf_t *ret = heap_alloc(sizeof(*ret));
401 ret->lpUnknownVtbl = &StgMedUnkVtbl;
406 ret->cache_file = NULL;
408 IInternetProtocol_AddRef(protocol);
409 ret->protocol = protocol;
417 stgmed_obj_t stgmed_obj;
418 const IStreamVtbl *lpStreamVtbl;
425 #define STREAM_THIS(iface) DEFINE_THIS(ProtocolStream, Stream, iface)
427 static HRESULT WINAPI ProtocolStream_QueryInterface(IStream *iface,
428 REFIID riid, void **ppv)
430 ProtocolStream *This = STREAM_THIS(iface);
434 if(IsEqualGUID(&IID_IUnknown, riid)) {
435 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
437 }else if(IsEqualGUID(&IID_ISequentialStream, riid)) {
438 TRACE("(%p)->(IID_ISequentialStream %p)\n", This, ppv);
440 }else if(IsEqualGUID(&IID_IStream, riid)) {
441 TRACE("(%p)->(IID_IStream %p)\n", This, ppv);
446 IStream_AddRef(STREAM(This));
450 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
451 return E_NOINTERFACE;
454 static ULONG WINAPI ProtocolStream_AddRef(IStream *iface)
456 ProtocolStream *This = STREAM_THIS(iface);
457 LONG ref = InterlockedIncrement(&This->ref);
459 TRACE("(%p) ref=%d\n", This, ref);
464 static ULONG WINAPI ProtocolStream_Release(IStream *iface)
466 ProtocolStream *This = STREAM_THIS(iface);
467 LONG ref = InterlockedDecrement(&This->ref);
469 TRACE("(%p) ref=%d\n", This, ref);
472 IUnknown_Release(STGMEDUNK(This->buf));
475 URLMON_UnlockModule();
481 static HRESULT WINAPI ProtocolStream_Read(IStream *iface, void *pv,
482 ULONG cb, ULONG *pcbRead)
484 ProtocolStream *This = STREAM_THIS(iface);
485 DWORD read = 0, pread = 0;
488 TRACE("(%p)->(%p %d %p)\n", This, pv, cb, pcbRead);
490 if(This->buf->size) {
493 if(read > This->buf->size)
494 read = This->buf->size;
496 memcpy(pv, This->buf->buf, read);
498 if(read < This->buf->size)
499 memmove(This->buf->buf, This->buf->buf+read, This->buf->size-read);
500 This->buf->size -= read;
509 hres = This->buf->hres = IInternetProtocol_Read(This->buf->protocol, (PBYTE)pv+read, cb-read, &pread);
511 *pcbRead = read + pread;
513 if(hres == E_PENDING)
515 else if(FAILED(hres))
516 FIXME("Read failed: %08x\n", hres);
518 return read || pread ? S_OK : S_FALSE;
521 static HRESULT WINAPI ProtocolStream_Write(IStream *iface, const void *pv,
522 ULONG cb, ULONG *pcbWritten)
524 ProtocolStream *This = STREAM_THIS(iface);
526 TRACE("(%p)->(%p %d %p)\n", This, pv, cb, pcbWritten);
528 return STG_E_ACCESSDENIED;
531 static HRESULT WINAPI ProtocolStream_Seek(IStream *iface, LARGE_INTEGER dlibMove,
532 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
534 ProtocolStream *This = STREAM_THIS(iface);
535 FIXME("(%p)->(%d %08x %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
539 static HRESULT WINAPI ProtocolStream_SetSize(IStream *iface, ULARGE_INTEGER libNewSize)
541 ProtocolStream *This = STREAM_THIS(iface);
542 FIXME("(%p)->(%d)\n", This, libNewSize.u.LowPart);
546 static HRESULT WINAPI ProtocolStream_CopyTo(IStream *iface, IStream *pstm,
547 ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten)
549 ProtocolStream *This = STREAM_THIS(iface);
550 FIXME("(%p)->(%p %d %p %p)\n", This, pstm, cb.u.LowPart, pcbRead, pcbWritten);
554 static HRESULT WINAPI ProtocolStream_Commit(IStream *iface, DWORD grfCommitFlags)
556 ProtocolStream *This = STREAM_THIS(iface);
558 TRACE("(%p)->(%08x)\n", This, grfCommitFlags);
563 static HRESULT WINAPI ProtocolStream_Revert(IStream *iface)
565 ProtocolStream *This = STREAM_THIS(iface);
567 TRACE("(%p)\n", This);
572 static HRESULT WINAPI ProtocolStream_LockRegion(IStream *iface, ULARGE_INTEGER libOffset,
573 ULARGE_INTEGER cb, DWORD dwLockType)
575 ProtocolStream *This = STREAM_THIS(iface);
576 FIXME("(%p)->(%d %d %d)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
580 static HRESULT WINAPI ProtocolStream_UnlockRegion(IStream *iface,
581 ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
583 ProtocolStream *This = STREAM_THIS(iface);
584 FIXME("(%p)->(%d %d %d)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
588 static HRESULT WINAPI ProtocolStream_Stat(IStream *iface, STATSTG *pstatstg,
591 ProtocolStream *This = STREAM_THIS(iface);
592 FIXME("(%p)->(%p %08x)\n", This, pstatstg, dwStatFlag);
596 static HRESULT WINAPI ProtocolStream_Clone(IStream *iface, IStream **ppstm)
598 ProtocolStream *This = STREAM_THIS(iface);
599 FIXME("(%p)->(%p)\n", This, ppstm);
605 static const IStreamVtbl ProtocolStreamVtbl = {
606 ProtocolStream_QueryInterface,
607 ProtocolStream_AddRef,
608 ProtocolStream_Release,
610 ProtocolStream_Write,
612 ProtocolStream_SetSize,
613 ProtocolStream_CopyTo,
614 ProtocolStream_Commit,
615 ProtocolStream_Revert,
616 ProtocolStream_LockRegion,
617 ProtocolStream_UnlockRegion,
622 static void stgmed_stream_release(stgmed_obj_t *obj)
624 ProtocolStream *stream = (ProtocolStream*)obj;
625 IStream_Release(STREAM(stream));
628 static HRESULT stgmed_stream_fill_stgmed(stgmed_obj_t *obj, STGMEDIUM *stgmed)
630 ProtocolStream *stream = (ProtocolStream*)obj;
632 stgmed->tymed = TYMED_ISTREAM;
633 stgmed->u.pstm = STREAM(stream);
634 stgmed->pUnkForRelease = STGMEDUNK(stream->buf);
639 static void *stgmed_stream_get_result(stgmed_obj_t *obj)
641 ProtocolStream *stream = (ProtocolStream*)obj;
643 IStream_AddRef(STREAM(stream));
644 return STREAM(stream);
647 static const stgmed_obj_vtbl stgmed_stream_vtbl = {
648 stgmed_stream_release,
649 stgmed_stream_fill_stgmed,
650 stgmed_stream_get_result
654 stgmed_obj_t stgmed_obj;
658 static stgmed_obj_t *create_stgmed_stream(stgmed_buf_t *buf)
660 ProtocolStream *ret = heap_alloc(sizeof(ProtocolStream));
662 ret->stgmed_obj.vtbl = &stgmed_stream_vtbl;
663 ret->lpStreamVtbl = &ProtocolStreamVtbl;
666 IUnknown_AddRef(STGMEDUNK(buf));
671 return &ret->stgmed_obj;
674 static void stgmed_file_release(stgmed_obj_t *obj)
676 stgmed_file_obj_t *file_obj = (stgmed_file_obj_t*)obj;
678 IUnknown_Release(STGMEDUNK(file_obj->buf));
682 static HRESULT stgmed_file_fill_stgmed(stgmed_obj_t *obj, STGMEDIUM *stgmed)
684 stgmed_file_obj_t *file_obj = (stgmed_file_obj_t*)obj;
686 if(!file_obj->buf->cache_file) {
687 WARN("cache_file not set\n");
688 return INET_E_DATA_NOT_AVAILABLE;
691 fill_stgmed_buffer(file_obj->buf);
692 if(file_obj->buf->size == sizeof(file_obj->buf->buf)) {
698 hres = IInternetProtocol_Read(file_obj->buf->protocol, buf, sizeof(buf), &read);
699 }while(hres == S_OK);
702 stgmed->tymed = TYMED_FILE;
703 stgmed->u.lpszFileName = file_obj->buf->cache_file;
704 stgmed->pUnkForRelease = STGMEDUNK(file_obj->buf);
709 static void *stgmed_file_get_result(stgmed_obj_t *obj)
714 static const stgmed_obj_vtbl stgmed_file_vtbl = {
716 stgmed_file_fill_stgmed,
717 stgmed_file_get_result
720 static stgmed_obj_t *create_stgmed_file(stgmed_buf_t *buf)
722 stgmed_file_obj_t *ret = heap_alloc(sizeof(*ret));
724 ret->stgmed_obj.vtbl = &stgmed_file_vtbl;
726 IUnknown_AddRef(STGMEDUNK(buf));
729 return &ret->stgmed_obj;
732 #define BINDING_THIS(iface) DEFINE_THIS(Binding, Binding, iface)
734 static HRESULT WINAPI Binding_QueryInterface(IBinding *iface, REFIID riid, void **ppv)
736 Binding *This = BINDING_THIS(iface);
740 if(IsEqualGUID(&IID_IUnknown, riid)) {
741 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
742 *ppv = BINDING(This);
743 }else if(IsEqualGUID(&IID_IBinding, riid)) {
744 TRACE("(%p)->(IID_IBinding %p)\n", This, ppv);
745 *ppv = BINDING(This);
746 }else if(IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
747 TRACE("(%p)->(IID_IInternetProtocolSink %p)\n", This, ppv);
748 *ppv = PROTSINK(This);
749 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
750 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
751 *ppv = BINDINF(This);
752 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
753 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
754 *ppv = SERVPROV(This);
755 }else if(IsEqualGUID(&IID_IWinInetInfo, riid)) {
756 TRACE("(%p)->(IID_IWinInetInfo %p)\n", This, ppv);
758 /* NOTE: This violidates COM rules, but tests prove that we should do it */
759 if(!get_wininet_info(This->protocol))
760 return E_NOINTERFACE;
762 *ppv = INETINFO(This);
763 }else if(IsEqualGUID(&IID_IWinInetHttpInfo, riid)) {
764 IWinInetHttpInfo *http_info;
768 TRACE("(%p)->(IID_IWinInetHttpInfo %p)\n", This, ppv);
770 info = get_wininet_info(This->protocol);
772 return E_NOINTERFACE;
774 hres = IWinInetInfo_QueryInterface(info, &IID_IWinInetHttpInfo, (void**)&http_info);
776 return E_NOINTERFACE;
778 IWinInetHttpInfo_Release(http_info);
779 *ppv = INETINFO(This);
783 IBinding_AddRef(BINDING(This));
787 WARN("Unsupported interface %s\n", debugstr_guid(riid));
788 return E_NOINTERFACE;
791 static ULONG WINAPI Binding_AddRef(IBinding *iface)
793 Binding *This = BINDING_THIS(iface);
794 LONG ref = InterlockedIncrement(&This->ref);
796 TRACE("(%p) ref=%d\n", This, ref);
801 static ULONG WINAPI Binding_Release(IBinding *iface)
803 Binding *This = BINDING_THIS(iface);
804 LONG ref = InterlockedDecrement(&This->ref);
806 TRACE("(%p) ref=%d\n", This, ref);
810 release_notif_hwnd(This->notif_hwnd);
812 IMoniker_Release(This->mon);
814 IBindStatusCallback_Release(This->callback);
816 IInternetProtocol_Release(This->protocol);
817 if(This->service_provider)
818 IServiceProvider_Release(This->service_provider);
820 IUnknown_Release(STGMEDUNK(This->stgmed_buf));
822 This->stgmed_obj->vtbl->release(This->stgmed_obj);
824 IUnknown_Release(This->obj);
826 IBindCtx_Release(This->bctx);
828 ReleaseBindInfo(&This->bindinfo);
829 This->section.DebugInfo->Spare[0] = 0;
830 DeleteCriticalSection(&This->section);
831 heap_free(This->mime);
832 heap_free(This->url);
836 URLMON_UnlockModule();
842 static HRESULT WINAPI Binding_Abort(IBinding *iface)
844 Binding *This = BINDING_THIS(iface);
845 FIXME("(%p)\n", This);
849 static HRESULT WINAPI Binding_Suspend(IBinding *iface)
851 Binding *This = BINDING_THIS(iface);
852 FIXME("(%p)\n", This);
856 static HRESULT WINAPI Binding_Resume(IBinding *iface)
858 Binding *This = BINDING_THIS(iface);
859 FIXME("(%p)\n", This);
863 static HRESULT WINAPI Binding_SetPriority(IBinding *iface, LONG nPriority)
865 Binding *This = BINDING_THIS(iface);
866 FIXME("(%p)->(%d)\n", This, nPriority);
870 static HRESULT WINAPI Binding_GetPriority(IBinding *iface, LONG *pnPriority)
872 Binding *This = BINDING_THIS(iface);
873 FIXME("(%p)->(%p)\n", This, pnPriority);
877 static HRESULT WINAPI Binding_GetBindResult(IBinding *iface, CLSID *pclsidProtocol,
878 DWORD *pdwResult, LPOLESTR *pszResult, DWORD *pdwReserved)
880 Binding *This = BINDING_THIS(iface);
881 FIXME("(%p)->(%p %p %p %p)\n", This, pclsidProtocol, pdwResult, pszResult, pdwReserved);
885 static Binding *get_bctx_binding(IBindCtx *bctx)
891 hres = IBindCtx_GetObjectParam(bctx, cbinding_contextW, &unk);
895 hres = IUnknown_QueryInterface(unk, &IID_IBinding, (void*)&binding);
896 IUnknown_Release(unk);
901 return BINDING_THIS(binding);
906 static const IBindingVtbl BindingVtbl = {
907 Binding_QueryInterface,
915 Binding_GetBindResult
918 #define PROTSINK_THIS(iface) DEFINE_THIS(Binding, IInternetProtocolSink, iface)
920 static HRESULT WINAPI InternetProtocolSink_QueryInterface(IInternetProtocolSink *iface,
921 REFIID riid, void **ppv)
923 Binding *This = PROTSINK_THIS(iface);
924 return IBinding_QueryInterface(BINDING(This), riid, ppv);
927 static ULONG WINAPI InternetProtocolSink_AddRef(IInternetProtocolSink *iface)
929 Binding *This = PROTSINK_THIS(iface);
930 return IBinding_AddRef(BINDING(This));
933 static ULONG WINAPI InternetProtocolSink_Release(IInternetProtocolSink *iface)
935 Binding *This = PROTSINK_THIS(iface);
936 return IBinding_Release(BINDING(This));
939 static HRESULT WINAPI InternetProtocolSink_Switch(IInternetProtocolSink *iface,
940 PROTOCOLDATA *pProtocolData)
942 Binding *This = PROTSINK_THIS(iface);
944 WARN("(%p)->(%p)\n", This, pProtocolData);
949 static void on_progress(Binding *This, ULONG progress, ULONG progress_max,
950 ULONG status_code, LPCWSTR status_text)
952 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
953 status_code, status_text);
956 static HRESULT WINAPI InternetProtocolSink_ReportProgress(IInternetProtocolSink *iface,
957 ULONG ulStatusCode, LPCWSTR szStatusText)
959 Binding *This = PROTSINK_THIS(iface);
961 TRACE("(%p)->(%u %s)\n", This, ulStatusCode, debugstr_w(szStatusText));
963 switch(ulStatusCode) {
964 case BINDSTATUS_FINDINGRESOURCE:
965 on_progress(This, 0, 0, BINDSTATUS_FINDINGRESOURCE, szStatusText);
967 case BINDSTATUS_CONNECTING:
968 on_progress(This, 0, 0, BINDSTATUS_CONNECTING, szStatusText);
970 case BINDSTATUS_BEGINDOWNLOADDATA:
971 fill_stgmed_buffer(This->stgmed_buf);
973 case BINDSTATUS_SENDINGREQUEST:
974 on_progress(This, 0, 0, BINDSTATUS_SENDINGREQUEST, szStatusText);
976 case BINDSTATUS_PROTOCOLCLASSID:
978 case BINDSTATUS_MIMETYPEAVAILABLE:
979 case BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE:
980 mime_available(This, szStatusText);
982 case BINDSTATUS_CACHEFILENAMEAVAILABLE:
983 heap_free(This->stgmed_buf->cache_file);
984 This->stgmed_buf->cache_file = heap_strdupW(szStatusText);
986 case BINDSTATUS_DECODING:
987 IBindStatusCallback_OnProgress(This->callback, 0, 0, BINDSTATUS_DECODING, szStatusText);
989 case BINDSTATUS_LOADINGMIMEHANDLER:
990 on_progress(This, 0, 0, BINDSTATUS_LOADINGMIMEHANDLER, szStatusText);
992 case BINDSTATUS_DIRECTBIND: /* FIXME: Handle BINDSTATUS_DIRECTBIND in BindProtocol */
993 This->report_mime = FALSE;
995 case BINDSTATUS_ACCEPTRANGES:
998 FIXME("Unhandled status code %d\n", ulStatusCode);
1005 static void report_data(Binding *This, DWORD bscf, ULONG progress, ULONG progress_max)
1007 FORMATETC formatetc = {0, NULL, 1, -1, TYMED_ISTREAM};
1008 BOOL sent_begindownloaddata = FALSE;
1010 TRACE("(%p)->(%d %u %u)\n", This, bscf, progress, progress_max);
1012 if(This->download_state == END_DOWNLOAD || (This->state & BINDING_STOPPED))
1015 if(This->download_state == BEFORE_DOWNLOAD) {
1016 fill_stgmed_buffer(This->stgmed_buf);
1018 This->download_state = DOWNLOADING;
1019 sent_begindownloaddata = TRUE;
1020 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1021 BINDSTATUS_BEGINDOWNLOADDATA, This->url);
1023 if(This->stgmed_buf->cache_file)
1024 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1025 BINDSTATUS_CACHEFILENAMEAVAILABLE, This->stgmed_buf->cache_file);
1028 if(This->stgmed_buf->hres == S_FALSE || (bscf & BSCF_LASTDATANOTIFICATION)) {
1029 This->download_state = END_DOWNLOAD;
1030 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1031 BINDSTATUS_ENDDOWNLOADDATA, This->url);
1032 }else if(!sent_begindownloaddata) {
1033 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1034 BINDSTATUS_DOWNLOADINGDATA, This->url);
1037 if(This->to_object) {
1038 if(!(This->state & BINDING_OBJAVAIL))
1039 create_object(This);
1044 if(!(This->state & BINDING_LOCKED)) {
1045 HRESULT hres = IInternetProtocol_LockRequest(This->protocol, 0);
1047 This->state |= BINDING_LOCKED;
1050 hres = This->stgmed_obj->vtbl->fill_stgmed(This->stgmed_obj, &stgmed);
1052 stop_binding(This, hres, NULL);
1056 formatetc.tymed = stgmed.tymed;
1057 formatetc.cfFormat = This->clipboard_format;
1059 IBindStatusCallback_OnDataAvailable(This->callback, bscf, progress,
1060 &formatetc, &stgmed);
1062 if(This->download_state == END_DOWNLOAD)
1063 stop_binding(This, S_OK, NULL);
1067 static HRESULT WINAPI InternetProtocolSink_ReportData(IInternetProtocolSink *iface,
1068 DWORD grfBSCF, ULONG ulProgress, ULONG ulProgressMax)
1070 Binding *This = PROTSINK_THIS(iface);
1072 TRACE("(%p)->(%d %u %u)\n", This, grfBSCF, ulProgress, ulProgressMax);
1074 report_data(This, grfBSCF, ulProgress, ulProgressMax);
1078 static HRESULT WINAPI InternetProtocolSink_ReportResult(IInternetProtocolSink *iface,
1079 HRESULT hrResult, DWORD dwError, LPCWSTR szResult)
1081 Binding *This = PROTSINK_THIS(iface);
1083 TRACE("(%p)->(%08x %d %s)\n", This, hrResult, dwError, debugstr_w(szResult));
1085 stop_binding(This, hrResult, szResult);
1086 IInternetProtocol_Terminate(This->protocol, 0);
1090 #undef PROTSINK_THIS
1092 static const IInternetProtocolSinkVtbl InternetProtocolSinkVtbl = {
1093 InternetProtocolSink_QueryInterface,
1094 InternetProtocolSink_AddRef,
1095 InternetProtocolSink_Release,
1096 InternetProtocolSink_Switch,
1097 InternetProtocolSink_ReportProgress,
1098 InternetProtocolSink_ReportData,
1099 InternetProtocolSink_ReportResult
1102 #define BINDINF_THIS(iface) DEFINE_THIS(Binding, InternetBindInfo, iface)
1104 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
1105 REFIID riid, void **ppv)
1107 Binding *This = BINDINF_THIS(iface);
1108 return IBinding_QueryInterface(BINDING(This), riid, ppv);
1111 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
1113 Binding *This = BINDINF_THIS(iface);
1114 return IBinding_AddRef(BINDING(This));
1117 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
1119 Binding *This = BINDINF_THIS(iface);
1120 return IBinding_Release(BINDING(This));
1123 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
1124 DWORD *grfBINDF, BINDINFO *pbindinfo)
1126 Binding *This = BINDINF_THIS(iface);
1128 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
1130 *grfBINDF = This->bindf;
1132 *pbindinfo = This->bindinfo;
1134 if(pbindinfo->szExtraInfo || pbindinfo->szCustomVerb)
1135 FIXME("copy strings\n");
1137 if(pbindinfo->stgmedData.pUnkForRelease)
1138 IUnknown_AddRef(pbindinfo->stgmedData.pUnkForRelease);
1141 IUnknown_AddRef(pbindinfo->pUnk);
1146 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
1147 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
1149 Binding *This = BINDINF_THIS(iface);
1151 TRACE("(%p)->(%d %p %d %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
1153 switch(ulStringType) {
1154 case BINDSTRING_ACCEPT_MIMES: {
1155 static const WCHAR wszMimes[] = {'*','/','*',0};
1157 if(!ppwzStr || !pcElFetched)
1158 return E_INVALIDARG;
1160 ppwzStr[0] = CoTaskMemAlloc(sizeof(wszMimes));
1161 memcpy(ppwzStr[0], wszMimes, sizeof(wszMimes));
1165 case BINDSTRING_USER_AGENT: {
1166 IInternetBindInfo *bindinfo = NULL;
1169 hres = IBindStatusCallback_QueryInterface(This->callback, &IID_IInternetBindInfo,
1174 hres = IInternetBindInfo_GetBindString(bindinfo, ulStringType, ppwzStr,
1176 IInternetBindInfo_Release(bindinfo);
1180 case BINDSTRING_URL: {
1181 DWORD size = (strlenW(This->url)+1) * sizeof(WCHAR);
1183 if(!ppwzStr || !pcElFetched)
1184 return E_INVALIDARG;
1186 *ppwzStr = CoTaskMemAlloc(size);
1187 memcpy(*ppwzStr, This->url, size);
1193 FIXME("not supported string type %d\n", ulStringType);
1199 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
1200 InternetBindInfo_QueryInterface,
1201 InternetBindInfo_AddRef,
1202 InternetBindInfo_Release,
1203 InternetBindInfo_GetBindInfo,
1204 InternetBindInfo_GetBindString
1207 #define INETINFO_THIS(iface) DEFINE_THIS(Binding, WinInetHttpInfo, iface)
1209 static HRESULT WINAPI WinInetHttpInfo_QueryInterface(IWinInetHttpInfo *iface, REFIID riid, void **ppv)
1211 Binding *This = INETINFO_THIS(iface);
1212 return IBinding_QueryInterface(BINDING(This), riid, ppv);
1215 static ULONG WINAPI WinInetHttpInfo_AddRef(IWinInetHttpInfo *iface)
1217 Binding *This = INETINFO_THIS(iface);
1218 return IBinding_AddRef(BINDING(This));
1221 static ULONG WINAPI WinInetHttpInfo_Release(IWinInetHttpInfo *iface)
1223 Binding *This = INETINFO_THIS(iface);
1224 return IBinding_Release(BINDING(This));
1227 static HRESULT WINAPI WinInetHttpInfo_QueryOption(IWinInetHttpInfo *iface, DWORD dwOption,
1228 void *pBuffer, DWORD *pcbBuffer)
1230 Binding *This = INETINFO_THIS(iface);
1231 FIXME("(%p)->(%x %p %p)\n", This, dwOption, pBuffer, pcbBuffer);
1235 static HRESULT WINAPI WinInetHttpInfo_QueryInfo(IWinInetHttpInfo *iface, DWORD dwOption,
1236 void *pBuffer, DWORD *pcbBuffer, DWORD *pdwFlags, DWORD *pdwReserved)
1238 Binding *This = INETINFO_THIS(iface);
1239 FIXME("(%p)->(%x %p %p %p %p)\n", This, dwOption, pBuffer, pcbBuffer, pdwFlags, pdwReserved);
1243 #undef INETINFO_THIS
1245 static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl = {
1246 WinInetHttpInfo_QueryInterface,
1247 WinInetHttpInfo_AddRef,
1248 WinInetHttpInfo_Release,
1249 WinInetHttpInfo_QueryOption,
1250 WinInetHttpInfo_QueryInfo
1253 #define SERVPROV_THIS(iface) DEFINE_THIS(Binding, ServiceProvider, iface)
1255 static HRESULT WINAPI ServiceProvider_QueryInterface(IServiceProvider *iface,
1256 REFIID riid, void **ppv)
1258 Binding *This = SERVPROV_THIS(iface);
1259 return IBinding_QueryInterface(BINDING(This), riid, ppv);
1262 static ULONG WINAPI ServiceProvider_AddRef(IServiceProvider *iface)
1264 Binding *This = SERVPROV_THIS(iface);
1265 return IBinding_AddRef(BINDING(This));
1268 static ULONG WINAPI ServiceProvider_Release(IServiceProvider *iface)
1270 Binding *This = SERVPROV_THIS(iface);
1271 return IBinding_Release(BINDING(This));
1274 static HRESULT WINAPI ServiceProvider_QueryService(IServiceProvider *iface,
1275 REFGUID guidService, REFIID riid, void **ppv)
1277 Binding *This = SERVPROV_THIS(iface);
1280 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
1282 if(This->service_provider) {
1283 hres = IServiceProvider_QueryService(This->service_provider, guidService,
1289 WARN("unknown service %s\n", debugstr_guid(guidService));
1293 #undef SERVPROV_THIS
1295 static const IServiceProviderVtbl ServiceProviderVtbl = {
1296 ServiceProvider_QueryInterface,
1297 ServiceProvider_AddRef,
1298 ServiceProvider_Release,
1299 ServiceProvider_QueryService
1302 static HRESULT get_callback(IBindCtx *pbc, IBindStatusCallback **callback)
1307 hres = IBindCtx_GetObjectParam(pbc, bscb_holderW, &unk);
1308 if(SUCCEEDED(hres)) {
1309 hres = IUnknown_QueryInterface(unk, &IID_IBindStatusCallback, (void**)callback);
1310 IUnknown_Release(unk);
1313 return SUCCEEDED(hres) ? S_OK : INET_E_DATA_NOT_AVAILABLE;
1316 static BOOL is_urlmon_protocol(LPCWSTR url)
1318 static const WCHAR wszCdl[] = {'c','d','l'};
1319 static const WCHAR wszFile[] = {'f','i','l','e'};
1320 static const WCHAR wszFtp[] = {'f','t','p'};
1321 static const WCHAR wszGopher[] = {'g','o','p','h','e','r'};
1322 static const WCHAR wszHttp[] = {'h','t','t','p'};
1323 static const WCHAR wszHttps[] = {'h','t','t','p','s'};
1324 static const WCHAR wszMk[] = {'m','k'};
1326 static const struct {
1329 } protocol_list[] = {
1330 {wszCdl, sizeof(wszCdl) /sizeof(WCHAR)},
1331 {wszFile, sizeof(wszFile) /sizeof(WCHAR)},
1332 {wszFtp, sizeof(wszFtp) /sizeof(WCHAR)},
1333 {wszGopher, sizeof(wszGopher)/sizeof(WCHAR)},
1334 {wszHttp, sizeof(wszHttp) /sizeof(WCHAR)},
1335 {wszHttps, sizeof(wszHttps) /sizeof(WCHAR)},
1336 {wszMk, sizeof(wszMk) /sizeof(WCHAR)}
1340 int len = lstrlenW(url);
1342 for(i=0; i < sizeof(protocol_list)/sizeof(protocol_list[0]); i++) {
1343 if(len >= protocol_list[i].len
1344 && !memcmp(url, protocol_list[i].scheme, protocol_list[i].len*sizeof(WCHAR)))
1351 static HRESULT Binding_Create(IMoniker *mon, Binding *binding_ctx, LPCWSTR url, IBindCtx *pbc,
1352 BOOL to_obj, REFIID riid, Binding **binding)
1357 URLMON_LockModule();
1359 ret = heap_alloc_zero(sizeof(Binding));
1361 ret->lpBindingVtbl = &BindingVtbl;
1362 ret->lpIInternetProtocolSinkVtbl = &InternetProtocolSinkVtbl;
1363 ret->lpInternetBindInfoVtbl = &InternetBindInfoVtbl;
1364 ret->lpWinInetHttpInfoVtbl = &WinInetHttpInfoVtbl;
1365 ret->lpServiceProviderVtbl = &ServiceProviderVtbl;
1369 ret->to_object = to_obj;
1371 ret->notif_hwnd = get_notif_hwnd();
1372 ret->report_mime = !binding_ctx;
1373 ret->download_state = BEFORE_DOWNLOAD;
1376 IBindCtx_AddRef(pbc);
1381 IMoniker_AddRef(mon);
1385 ret->bindinfo.cbSize = sizeof(BINDINFO);
1387 InitializeCriticalSection(&ret->section);
1388 ret->section.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": Binding.section");
1390 hres = get_callback(pbc, &ret->callback);
1392 WARN("Could not get IBindStatusCallback\n");
1393 IBinding_Release(BINDING(ret));
1397 IBindStatusCallback_QueryInterface(ret->callback, &IID_IServiceProvider,
1398 (void**)&ret->service_provider);
1401 ret->protocol = binding_ctx->protocol;
1402 IInternetProtocol_AddRef(ret->protocol);
1404 hres = create_binding_protocol(url, TRUE, &ret->protocol);
1406 WARN("Could not get protocol handler\n");
1407 IBinding_Release(BINDING(ret));
1412 hres = IBindStatusCallback_GetBindInfo(ret->callback, &ret->bindf, &ret->bindinfo);
1414 WARN("GetBindInfo failed: %08x\n", hres);
1415 IBinding_Release(BINDING(ret));
1419 TRACE("bindf %08x\n", ret->bindf);
1420 dump_BINDINFO(&ret->bindinfo);
1422 ret->bindf |= BINDF_FROMURLMON;
1424 ret->bindinfo.dwOptions |= 0x100000;
1426 if(!is_urlmon_protocol(url))
1427 ret->bindf |= BINDF_NEEDFILE;
1429 ret->url = heap_strdupW(url);
1432 ret->stgmed_buf = binding_ctx->stgmed_buf;
1433 IUnknown_AddRef(STGMEDUNK(ret->stgmed_buf));
1434 ret->clipboard_format = binding_ctx->clipboard_format;
1436 ret->stgmed_buf = create_stgmed_buf(ret->protocol);
1440 ret->stgmed_obj = NULL;
1441 }else if(IsEqualGUID(&IID_IStream, riid)) {
1442 ret->stgmed_obj = create_stgmed_stream(ret->stgmed_buf);
1443 }else if(IsEqualGUID(&IID_IUnknown, riid)) {
1444 ret->bindf |= BINDF_NEEDFILE;
1445 ret->stgmed_obj = create_stgmed_file(ret->stgmed_buf);
1447 FIXME("Unsupported riid %s\n", debugstr_guid(riid));
1448 IBinding_Release(BINDING(ret));
1456 static HRESULT start_binding(IMoniker *mon, Binding *binding_ctx, LPCWSTR url, IBindCtx *pbc,
1457 BOOL to_obj, REFIID riid, Binding **ret)
1459 Binding *binding = NULL;
1463 hres = Binding_Create(mon, binding_ctx, url, pbc, to_obj, riid, &binding);
1467 hres = IBindStatusCallback_OnStartBinding(binding->callback, 0, BINDING(binding));
1469 WARN("OnStartBinding failed: %08x\n", hres);
1470 stop_binding(binding, INET_E_DOWNLOAD_FAILURE, NULL);
1471 IBinding_Release(BINDING(binding));
1476 set_binding_sink(binding->protocol, PROTSINK(binding));
1477 report_data(binding, 0, 0, 0);
1479 hres = IInternetProtocol_Start(binding->protocol, url, PROTSINK(binding),
1480 BINDINF(binding), PI_APARTMENTTHREADED|PI_MIMEVERIFICATION, 0);
1482 TRACE("start ret %08x\n", hres);
1484 if(FAILED(hres) && hres != E_PENDING) {
1485 stop_binding(binding, hres, NULL);
1486 IBinding_Release(BINDING(binding));
1492 while(!(binding->bindf & BINDF_ASYNCHRONOUS) &&
1493 !(binding->state & BINDING_STOPPED)) {
1494 MsgWaitForMultipleObjects(0, NULL, FALSE, 5000, QS_POSTMESSAGE);
1495 while (PeekMessageW(&msg, binding->notif_hwnd, WM_USER, WM_USER+117, PM_REMOVE|PM_NOYIELD)) {
1496 TranslateMessage(&msg);
1497 DispatchMessageW(&msg);
1505 HRESULT bind_to_storage(LPCWSTR url, IBindCtx *pbc, REFIID riid, void **ppv)
1507 Binding *binding = NULL, *binding_ctx;
1512 binding_ctx = get_bctx_binding(pbc);
1514 hres = start_binding(NULL, binding_ctx, url, pbc, FALSE, riid, &binding);
1516 IBinding_Release(BINDING(binding_ctx));
1520 if(binding->hres == S_OK && binding->stgmed_buf->init) {
1521 if((binding->state & BINDING_STOPPED) && (binding->state & BINDING_LOCKED))
1522 IInternetProtocol_UnlockRequest(binding->protocol);
1524 *ppv = binding->stgmed_obj->vtbl->get_result(binding->stgmed_obj);
1527 IBinding_Release(BINDING(binding));
1529 return *ppv ? S_OK : MK_S_ASYNCHRONOUS;
1532 HRESULT bind_to_object(IMoniker *mon, LPCWSTR url, IBindCtx *pbc, REFIID riid, void **ppv)
1539 hres = start_binding(mon, NULL, url, pbc, TRUE, riid, &binding);
1543 if(binding->hres != S_OK) {
1544 hres = SUCCEEDED(binding->hres) ? S_OK : binding->hres;
1545 }else if(binding->bindf & BINDF_ASYNCHRONOUS) {
1546 hres = MK_S_ASYNCHRONOUS;
1548 *ppv = binding->obj;
1549 IUnknown_AddRef(binding->obj);
1553 IBinding_Release(BINDING(binding));