2 * Copyright 2005 Jacek Caban
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
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
30 #include "urlmon_main.h"
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
37 typedef struct Binding Binding;
52 typedef struct _task_t {
56 on_progress_data on_progress;
57 PROTOCOLDATA *protocol_data;
62 const IStreamVtbl *lpStreamVtbl;
66 IInternetProtocol *protocol;
74 const IBindingVtbl *lpBindingVtbl;
75 const IInternetProtocolSinkVtbl *lpInternetProtocolSinkVtbl;
76 const IInternetBindInfoVtbl *lpInternetBindInfoVtbl;
77 const IServiceProviderVtbl *lpServiceProviderVtbl;
81 IBindStatusCallback *callback;
82 IInternetProtocol *protocol;
83 IServiceProvider *service_provider;
84 ProtocolStream *stream;
93 DWORD apartment_thread;
98 task_t *task_queue_head, *task_queue_tail;
99 CRITICAL_SECTION section;
102 #define BINDING(x) ((IBinding*) &(x)->lpBindingVtbl)
103 #define PROTSINK(x) ((IInternetProtocolSink*) &(x)->lpInternetProtocolSinkVtbl)
104 #define BINDINF(x) ((IInternetBindInfo*) &(x)->lpInternetBindInfoVtbl)
105 #define SERVPROV(x) ((IServiceProvider*) &(x)->lpServiceProviderVtbl)
107 #define STREAM(x) ((IStream*) &(x)->lpStreamVtbl)
109 #define WM_MK_CONTINUE (WM_USER+101)
111 static void push_task(Binding *binding, task_t *task)
115 EnterCriticalSection(&binding->section);
117 if(binding->task_queue_tail)
118 binding->task_queue_tail->next = task;
120 binding->task_queue_tail = binding->task_queue_head = task;
122 LeaveCriticalSection(&binding->section);
125 static task_t *pop_task(Binding *binding)
129 EnterCriticalSection(&binding->section);
131 ret = binding->task_queue_head;
133 binding->task_queue_head = ret->next;
134 if(!binding->task_queue_head)
135 binding->task_queue_tail = NULL;
138 LeaveCriticalSection(&binding->section);
143 static void do_task(Binding *binding, task_t *task)
146 case TASK_ON_PROGRESS: {
147 on_progress_data *data = &task->data.on_progress;
149 IBindStatusCallback_OnProgress(binding->callback, data->progress,
150 data->progress_max, data->status_code, data->status_text);
152 HeapFree(GetProcessHeap(), 0, data->status_text);
153 HeapFree(GetProcessHeap(), 0, data);
158 binding->continue_call++;
159 IInternetProtocol_Continue(binding->protocol, task->data.protocol_data);
160 binding->continue_call--;
164 static void do_tasks(Binding *This)
168 while((task = pop_task(This)))
172 static LRESULT WINAPI notif_wnd_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
174 if(msg == WM_MK_CONTINUE) {
175 Binding *binding = (Binding*)lParam;
179 IBinding_Release(BINDING(binding));
183 return DefWindowProcW(hwnd, msg, wParam, lParam);
186 static HWND get_notif_hwnd(void)
188 static ATOM wnd_class = 0;
191 static const WCHAR wszURLMonikerNotificationWindow[] =
192 {'U','R','L',' ','M','o','n','i','k','e','r',' ',
193 'N','o','t','i','f','i','c','a','t','i','o','n',' ','W','i','n','d','o','w',0};
196 static WNDCLASSEXW wndclass = {
198 notif_wnd_proc, 0, 0,
199 NULL, NULL, NULL, NULL, NULL,
200 wszURLMonikerNotificationWindow,
204 wndclass.hInstance = URLMON_hInstance;
206 wnd_class = RegisterClassExW(&wndclass);
210 urlmon_tls = TlsAlloc();
212 hwnd = TlsGetValue(urlmon_tls);
216 hwnd = CreateWindowExW(0, MAKEINTATOMW(wnd_class),
217 wszURLMonikerNotificationWindow, 0, 0, 0, 0, 0, HWND_MESSAGE,
218 NULL, URLMON_hInstance, NULL);
219 TlsSetValue(urlmon_tls, hwnd);
221 TRACE("hwnd = %p\n", hwnd);
226 static void on_progress(Binding *This, ULONG progress, ULONG progress_max,
227 ULONG status_code, LPCWSTR status_text)
231 if(GetCurrentThreadId() == This->apartment_thread && !This->continue_call) {
232 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
233 status_code, status_text);
237 task = HeapAlloc(GetProcessHeap(), 0, sizeof(task_t));
239 task->task = TASK_ON_PROGRESS;
240 task->data.on_progress.progress = progress;
241 task->data.on_progress.progress_max = progress_max;
242 task->data.on_progress.status_code = status_code;
245 DWORD size = (strlenW(status_text)+1)*sizeof(WCHAR);
247 task->data.on_progress.status_text = HeapAlloc(GetProcessHeap(), 0, size);
248 memcpy(task->data.on_progress.status_text, status_text, size);
250 task->data.on_progress.status_text = NULL;
253 push_task(This, task);
255 if(GetCurrentThreadId() != This->apartment_thread) {
256 IBinding_AddRef(BINDING(This));
257 PostMessageW(This->notif_hwnd, WM_MK_CONTINUE, 0, (LPARAM)This);
261 static void dump_BINDINFO(BINDINFO *bi)
263 static const char *BINDINFOF_str[] = {
265 "BINDINFOF_URLENCODESTGMEDDATA",
266 "BINDINFOF_URLENCODEDEXTRAINFO"
269 static const char *BINDVERB_str[] = {
283 " %ld, %08lx, %ld, %ld\n"
289 bi->cbSize, debugstr_w(bi->szExtraInfo),
290 bi->stgmedData.tymed, bi->stgmedData.u.hGlobal, bi->stgmedData.pUnkForRelease,
291 bi->grfBindInfoF > BINDINFOF_URLENCODEDEXTRAINFO
292 ? "unknown" : BINDINFOF_str[bi->grfBindInfoF],
293 bi->dwBindVerb > BINDVERB_CUSTOM
294 ? "unknown" : BINDVERB_str[bi->dwBindVerb],
295 debugstr_w(bi->szCustomVerb),
296 bi->cbStgmedData, bi->dwOptions, bi->dwOptionsFlags, bi->dwCodePage,
297 bi->securityAttributes.nLength,
298 bi->securityAttributes.lpSecurityDescriptor,
299 bi->securityAttributes.bInheritHandle,
300 debugstr_guid(&bi->iid),
301 bi->pUnk, bi->dwReserved
305 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
306 REFIID riid, void **ppv)
310 if(IsEqualGUID(&IID_IUnknown, riid)) {
311 TRACE("(IID_IUnknown %p)\n", ppv);
313 }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
314 TRACE("(IID_IHttpNegotiate %p)\n", ppv);
316 }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
317 TRACE("(IID_IHttpNegotiate2 %p)\n", ppv);
322 IHttpNegotiate2_AddRef(iface);
326 WARN("Unsupported interface %s\n", debugstr_guid(riid));
327 return E_NOINTERFACE;
330 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
336 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
338 URLMON_UnlockModule();
342 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
343 LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
345 TRACE("(%s %s %ld %p)\n", debugstr_w(szURL), debugstr_w(szHeaders), dwReserved,
346 pszAdditionalHeaders);
348 *pszAdditionalHeaders = NULL;
352 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
353 LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders,
354 LPWSTR *pszAdditionalRequestHeaders)
356 FIXME("(%ld %s %s %p)\n", dwResponseCode, debugstr_w(szResponseHeaders),
357 debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
361 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
362 BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
364 TRACE("(%p %p %ld)\n", pbSecurityId, pcbSecurityId, dwReserved);
366 /* That's all we have to do here */
370 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
371 HttpNegotiate_QueryInterface,
372 HttpNegotiate_AddRef,
373 HttpNegotiate_Release,
374 HttpNegotiate_BeginningTransaction,
375 HttpNegotiate_OnResponse,
376 HttpNegotiate_GetRootSecurityId
379 static IHttpNegotiate2 HttpNegotiate = { &HttpNegotiate2Vtbl };
381 #define STREAM_THIS(iface) DEFINE_THIS(ProtocolStream, Stream, iface)
383 static HRESULT WINAPI ProtocolStream_QueryInterface(IStream *iface,
384 REFIID riid, void **ppv)
386 ProtocolStream *This = STREAM_THIS(iface);
390 if(IsEqualGUID(&IID_IUnknown, riid)) {
391 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
393 }else if(IsEqualGUID(&IID_ISequentialStream, riid)) {
394 TRACE("(%p)->(IID_ISequentialStream %p)\n", This, ppv);
396 }else if(IsEqualGUID(&IID_IStream, riid)) {
397 TRACE("(%p)->(IID_IStream %p)\n", This, ppv);
402 IStream_AddRef(STREAM(This));
406 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
407 return E_NOINTERFACE;
410 static ULONG WINAPI ProtocolStream_AddRef(IStream *iface)
412 ProtocolStream *This = STREAM_THIS(iface);
413 LONG ref = InterlockedIncrement(&This->ref);
415 TRACE("(%p) ref=%ld\n", This, ref);
420 static ULONG WINAPI ProtocolStream_Release(IStream *iface)
422 ProtocolStream *This = STREAM_THIS(iface);
423 LONG ref = InterlockedDecrement(&This->ref);
425 TRACE("(%p) ref=%ld\n", This, ref);
428 IInternetProtocol_Release(This->protocol);
429 HeapFree(GetProcessHeap(), 0, This);
431 URLMON_UnlockModule();
437 static HRESULT WINAPI ProtocolStream_Read(IStream *iface, void *pv,
438 ULONG cb, ULONG *pcbRead)
440 ProtocolStream *This = STREAM_THIS(iface);
441 DWORD read = 0, pread = 0;
444 TRACE("(%p)->(%p %ld %p)\n", This, pv, cb, pcbRead);
449 if(read > This->buf_size)
450 read = This->buf_size;
452 memcpy(pv, This->buf, read);
454 if(read < This->buf_size)
455 memmove(This->buf, This->buf+read, This->buf_size-read);
456 This->buf_size -= read;
464 hres = IInternetProtocol_Read(This->protocol, (PBYTE)pv+read, cb-read, &pread);
465 *pcbRead = read + pread;
467 if(hres == E_PENDING)
469 else if(FAILED(hres))
470 FIXME("Read failed: %08lx\n", hres);
472 return read || pread ? S_OK : S_FALSE;
475 static HRESULT WINAPI ProtocolStream_Write(IStream *iface, const void *pv,
476 ULONG cb, ULONG *pcbWritten)
478 ProtocolStream *This = STREAM_THIS(iface);
480 TRACE("(%p)->(%p %ld %p)\n", This, pv, cb, pcbWritten);
482 return STG_E_ACCESSDENIED;
485 static HRESULT WINAPI ProtocolStream_Seek(IStream *iface, LARGE_INTEGER dlibMove,
486 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
488 ProtocolStream *This = STREAM_THIS(iface);
489 FIXME("(%p)->(%ld %08lx %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
493 static HRESULT WINAPI ProtocolStream_SetSize(IStream *iface, ULARGE_INTEGER libNewSize)
495 ProtocolStream *This = STREAM_THIS(iface);
496 FIXME("(%p)->(%ld)\n", This, libNewSize.u.LowPart);
500 static HRESULT WINAPI ProtocolStream_CopyTo(IStream *iface, IStream *pstm,
501 ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten)
503 ProtocolStream *This = STREAM_THIS(iface);
504 FIXME("(%p)->(%p %ld %p %p)\n", This, pstm, cb.u.LowPart, pcbRead, pcbWritten);
508 static HRESULT WINAPI ProtocolStream_Commit(IStream *iface, DWORD grfCommitFlags)
510 ProtocolStream *This = STREAM_THIS(iface);
512 TRACE("(%p)->(%08lx)\n", This, grfCommitFlags);
517 static HRESULT WINAPI ProtocolStream_Revert(IStream *iface)
519 ProtocolStream *This = STREAM_THIS(iface);
521 TRACE("(%p)\n", This);
526 static HRESULT WINAPI ProtocolStream_LockRegion(IStream *iface, ULARGE_INTEGER libOffset,
527 ULARGE_INTEGER cb, DWORD dwLockType)
529 ProtocolStream *This = STREAM_THIS(iface);
530 FIXME("(%p)->(%ld %ld %ld)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
534 static HRESULT WINAPI ProtocolStream_UnlockRegion(IStream *iface,
535 ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
537 ProtocolStream *This = STREAM_THIS(iface);
538 FIXME("(%p)->(%ld %ld %ld)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
542 static HRESULT WINAPI ProtocolStream_Stat(IStream *iface, STATSTG *pstatstg,
545 ProtocolStream *This = STREAM_THIS(iface);
546 FIXME("(%p)->(%p %08lx)\n", This, pstatstg, dwStatFlag);
550 static HRESULT WINAPI ProtocolStream_Clone(IStream *iface, IStream **ppstm)
552 ProtocolStream *This = STREAM_THIS(iface);
553 FIXME("(%p)->(%p)\n", This, ppstm);
559 static const IStreamVtbl ProtocolStreamVtbl = {
560 ProtocolStream_QueryInterface,
561 ProtocolStream_AddRef,
562 ProtocolStream_Release,
564 ProtocolStream_Write,
566 ProtocolStream_SetSize,
567 ProtocolStream_CopyTo,
568 ProtocolStream_Commit,
569 ProtocolStream_Revert,
570 ProtocolStream_LockRegion,
571 ProtocolStream_UnlockRegion,
576 #define BINDING_THIS(iface) DEFINE_THIS(Binding, Binding, iface)
578 static ProtocolStream *create_stream(IInternetProtocol *protocol)
580 ProtocolStream *ret = HeapAlloc(GetProcessHeap(), 0, sizeof(ProtocolStream));
582 ret->lpStreamVtbl = &ProtocolStreamVtbl;
585 ret->init_buf = FALSE;
587 IInternetProtocol_AddRef(protocol);
588 ret->protocol = protocol;
595 static void fill_stream_buffer(ProtocolStream *This)
600 hres = IInternetProtocol_Read(This->protocol, This->buf+This->buf_size,
601 sizeof(This->buf)-This->buf_size, &read);
602 if(SUCCEEDED(hres)) {
603 This->buf_size += read;
604 This->init_buf = TRUE;
608 static HRESULT WINAPI Binding_QueryInterface(IBinding *iface, REFIID riid, void **ppv)
610 Binding *This = BINDING_THIS(iface);
614 if(IsEqualGUID(&IID_IUnknown, riid)) {
615 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
616 *ppv = BINDING(This);
617 }else if(IsEqualGUID(&IID_IBinding, riid)) {
618 TRACE("(%p)->(IID_IBinding %p)\n", This, ppv);
619 *ppv = BINDING(This);
620 }else if(IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
621 TRACE("(%p)->(IID_IInternetProtocolSink %p)\n", This, ppv);
622 *ppv = PROTSINK(This);
623 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
624 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
625 *ppv = BINDINF(This);
626 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
627 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
628 *ppv = SERVPROV(This);
632 IBinding_AddRef(BINDING(This));
636 WARN("Unsupported interface %s\n", debugstr_guid(riid));
637 return E_NOINTERFACE;
640 static ULONG WINAPI Binding_AddRef(IBinding *iface)
642 Binding *This = BINDING_THIS(iface);
643 LONG ref = InterlockedIncrement(&This->ref);
645 TRACE("(%p) ref=%ld\n", This, ref);
650 static ULONG WINAPI Binding_Release(IBinding *iface)
652 Binding *This = BINDING_THIS(iface);
653 LONG ref = InterlockedDecrement(&This->ref);
655 TRACE("(%p) ref=%ld\n", This, ref);
659 IBindStatusCallback_Release(This->callback);
661 IInternetProtocol_Release(This->protocol);
662 if(This->service_provider)
663 IServiceProvider_Release(This->service_provider);
665 IStream_Release(STREAM(This->stream));
667 ReleaseBindInfo(&This->bindinfo);
668 DeleteCriticalSection(&This->section);
669 HeapFree(GetProcessHeap(), 0, This->mime);
670 HeapFree(GetProcessHeap(), 0, This->url);
672 HeapFree(GetProcessHeap(), 0, This);
674 URLMON_UnlockModule();
680 static HRESULT WINAPI Binding_Abort(IBinding *iface)
682 Binding *This = BINDING_THIS(iface);
683 FIXME("(%p)\n", This);
687 static HRESULT WINAPI Binding_Suspend(IBinding *iface)
689 Binding *This = BINDING_THIS(iface);
690 FIXME("(%p)\n", This);
694 static HRESULT WINAPI Binding_Resume(IBinding *iface)
696 Binding *This = BINDING_THIS(iface);
697 FIXME("(%p)\n", This);
701 static HRESULT WINAPI Binding_SetPriority(IBinding *iface, LONG nPriority)
703 Binding *This = BINDING_THIS(iface);
704 FIXME("(%p)->(%ld)\n", This, nPriority);
708 static HRESULT WINAPI Binding_GetPriority(IBinding *iface, LONG *pnPriority)
710 Binding *This = BINDING_THIS(iface);
711 FIXME("(%p)->(%p)\n", This, pnPriority);
715 static HRESULT WINAPI Binding_GetBindResult(IBinding *iface, CLSID *pclsidProtocol,
716 DWORD *pdwResult, LPOLESTR *pszResult, DWORD *pdwReserved)
718 Binding *This = BINDING_THIS(iface);
719 FIXME("(%p)->(%p %p %p %p)\n", This, pclsidProtocol, pdwResult, pszResult, pdwReserved);
725 static const IBindingVtbl BindingVtbl = {
726 Binding_QueryInterface,
734 Binding_GetBindResult
737 #define PROTSINK_THIS(iface) DEFINE_THIS(Binding, InternetProtocolSink, iface)
739 static HRESULT WINAPI InternetProtocolSink_QueryInterface(IInternetProtocolSink *iface,
740 REFIID riid, void **ppv)
742 Binding *This = PROTSINK_THIS(iface);
743 return IBinding_QueryInterface(BINDING(This), riid, ppv);
746 static ULONG WINAPI InternetProtocolSink_AddRef(IInternetProtocolSink *iface)
748 Binding *This = PROTSINK_THIS(iface);
749 return IBinding_AddRef(BINDING(This));
752 static ULONG WINAPI InternetProtocolSink_Release(IInternetProtocolSink *iface)
754 Binding *This = PROTSINK_THIS(iface);
755 return IBinding_Release(BINDING(This));
758 static HRESULT WINAPI InternetProtocolSink_Switch(IInternetProtocolSink *iface,
759 PROTOCOLDATA *pProtocolData)
761 Binding *This = PROTSINK_THIS(iface);
764 TRACE("(%p)->(%p)\n", This, pProtocolData);
766 task = HeapAlloc(GetProcessHeap(), 0, sizeof(task_t));
767 task->task = TASK_SWITCH;
768 task->data.protocol_data = pProtocolData;
770 push_task(This, task);
772 IBinding_AddRef(BINDING(This));
773 PostMessageW(This->notif_hwnd, WM_MK_CONTINUE, 0, (LPARAM)This);
778 static HRESULT WINAPI InternetProtocolSink_ReportProgress(IInternetProtocolSink *iface,
779 ULONG ulStatusCode, LPCWSTR szStatusText)
781 Binding *This = PROTSINK_THIS(iface);
783 TRACE("(%p)->(%lu %s)\n", This, ulStatusCode, debugstr_w(szStatusText));
785 switch(ulStatusCode) {
786 case BINDSTATUS_FINDINGRESOURCE:
787 on_progress(This, 0, 0, BINDSTATUS_FINDINGRESOURCE, szStatusText);
789 case BINDSTATUS_CONNECTING:
790 on_progress(This, 0, 0, BINDSTATUS_CONNECTING, szStatusText);
792 case BINDSTATUS_MIMETYPEAVAILABLE: {
793 int len = strlenW(szStatusText)+1;
794 This->mime = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
795 memcpy(This->mime, szStatusText, len*sizeof(WCHAR));
798 case BINDSTATUS_SENDINGREQUEST:
799 on_progress(This, 0, 0, BINDSTATUS_SENDINGREQUEST, szStatusText);
801 case BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE:
802 This->verified_mime = TRUE;
803 on_progress(This, 0, 0, BINDSTATUS_MIMETYPEAVAILABLE, szStatusText);
805 case BINDSTATUS_CACHEFILENAMEAVAILABLE:
808 FIXME("Unhandled status code %ld\n", ulStatusCode);
815 static HRESULT WINAPI InternetProtocolSink_ReportData(IInternetProtocolSink *iface,
816 DWORD grfBSCF, ULONG ulProgress, ULONG ulProgressMax)
818 Binding *This = PROTSINK_THIS(iface);
821 TRACE("(%p)->(%ld %lu %lu)\n", This, grfBSCF, ulProgress, ulProgressMax);
823 fill_stream_buffer(This->stream);
825 if(!This->verified_mime) {
828 This->verified_mime = TRUE;
830 /* FIXME: Always call FindMediaFromData (its implementation is not yet ready for this). */
834 FindMimeFromData(NULL, This->url, This->stream->buf,
835 min(This->stream->buf_size, 255), This->mime, 0, &mime, 0);
837 on_progress(This, ulProgress, ulProgressMax, BINDSTATUS_MIMETYPEAVAILABLE, mime);
840 if(grfBSCF & BSCF_FIRSTDATANOTIFICATION) {
841 on_progress(This, ulProgress, ulProgressMax, BINDSTATUS_BEGINDOWNLOADDATA, This->url);
844 if(grfBSCF & BSCF_LASTDATANOTIFICATION)
845 on_progress(This, ulProgress, ulProgressMax, BINDSTATUS_ENDDOWNLOADDATA, This->url);
847 if(grfBSCF & BSCF_FIRSTDATANOTIFICATION)
848 IInternetProtocol_LockRequest(This->protocol, 0);
850 formatetc.cfFormat = 0; /* FIXME */
851 formatetc.ptd = NULL;
852 formatetc.dwAspect = 1;
853 formatetc.lindex = -1;
854 formatetc.tymed = TYMED_ISTREAM;
856 IBindStatusCallback_OnDataAvailable(This->callback, grfBSCF, This->stream->buf_size,
857 &formatetc, &This->stgmed);
859 if(grfBSCF & BSCF_LASTDATANOTIFICATION)
860 IBindStatusCallback_OnStopBinding(This->callback, S_OK, NULL);
865 static HRESULT WINAPI InternetProtocolSink_ReportResult(IInternetProtocolSink *iface,
866 HRESULT hrResult, DWORD dwError, LPCWSTR szResult)
868 Binding *This = PROTSINK_THIS(iface);
870 TRACE("(%p)->(%08lx %ld %s)\n", This, hrResult, dwError, debugstr_w(szResult));
872 IInternetProtocol_Terminate(This->protocol, 0);
878 static const IInternetProtocolSinkVtbl InternetProtocolSinkVtbl = {
879 InternetProtocolSink_QueryInterface,
880 InternetProtocolSink_AddRef,
881 InternetProtocolSink_Release,
882 InternetProtocolSink_Switch,
883 InternetProtocolSink_ReportProgress,
884 InternetProtocolSink_ReportData,
885 InternetProtocolSink_ReportResult
888 #define BINDINF_THIS(iface) DEFINE_THIS(Binding, InternetBindInfo, iface)
890 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
891 REFIID riid, void **ppv)
893 Binding *This = BINDINF_THIS(iface);
894 return IBinding_QueryInterface(BINDING(This), riid, ppv);
897 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
899 Binding *This = BINDINF_THIS(iface);
900 return IBinding_AddRef(BINDING(This));
903 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
905 Binding *This = BINDINF_THIS(iface);
906 return IBinding_Release(BINDING(This));
909 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
910 DWORD *grfBINDF, BINDINFO *pbindinfo)
912 Binding *This = BINDINF_THIS(iface);
914 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
916 *grfBINDF = This->bindf;
918 memcpy(pbindinfo, &This->bindinfo, sizeof(BINDINFO));
920 if(pbindinfo->szExtraInfo || pbindinfo->szCustomVerb)
921 FIXME("copy strings\n");
924 IUnknown_AddRef(pbindinfo->pUnk);
929 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
930 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
932 Binding *This = BINDINF_THIS(iface);
934 TRACE("(%p)->(%ld %p %ld %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
936 switch(ulStringType) {
937 case BINDSTRING_ACCEPT_MIMES: {
938 static const WCHAR wszMimes[] = {'*','/','*',0};
940 if(!ppwzStr || !pcElFetched)
943 ppwzStr[0] = CoTaskMemAlloc(sizeof(wszMimes));
944 memcpy(ppwzStr[0], wszMimes, sizeof(wszMimes));
948 case BINDSTRING_USER_AGENT: {
949 IInternetBindInfo *bindinfo = NULL;
952 hres = IBindStatusCallback_QueryInterface(This->callback, &IID_IInternetBindInfo,
957 hres = IInternetBindInfo_GetBindString(bindinfo, ulStringType, ppwzStr,
959 IInternetBindInfo_Release(bindinfo);
965 FIXME("not supported string type %ld\n", ulStringType);
971 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
972 InternetBindInfo_QueryInterface,
973 InternetBindInfo_AddRef,
974 InternetBindInfo_Release,
975 InternetBindInfo_GetBindInfo,
976 InternetBindInfo_GetBindString
979 #define SERVPROV_THIS(iface) DEFINE_THIS(Binding, ServiceProvider, iface)
981 static HRESULT WINAPI ServiceProvider_QueryInterface(IServiceProvider *iface,
982 REFIID riid, void **ppv)
984 Binding *This = SERVPROV_THIS(iface);
985 return IBinding_QueryInterface(BINDING(This), riid, ppv);
988 static ULONG WINAPI ServiceProvider_AddRef(IServiceProvider *iface)
990 Binding *This = SERVPROV_THIS(iface);
991 return IBinding_AddRef(BINDING(This));
994 static ULONG WINAPI ServiceProvider_Release(IServiceProvider *iface)
996 Binding *This = SERVPROV_THIS(iface);
997 return IBinding_Release(BINDING(This));
1000 static HRESULT WINAPI ServiceProvider_QueryService(IServiceProvider *iface,
1001 REFGUID guidService, REFIID riid, void **ppv)
1003 Binding *This = SERVPROV_THIS(iface);
1006 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
1008 if(This->service_provider) {
1009 hres = IServiceProvider_QueryService(This->service_provider, guidService,
1015 if(IsEqualGUID(&IID_IHttpNegotiate, guidService)
1016 || IsEqualGUID(&IID_IHttpNegotiate2, guidService))
1017 return IHttpNegotiate2_QueryInterface(&HttpNegotiate, riid, ppv);
1019 WARN("unknown service %s\n", debugstr_guid(guidService));
1023 #undef SERVPROV_THIS
1025 static const IServiceProviderVtbl ServiceProviderVtbl = {
1026 ServiceProvider_QueryInterface,
1027 ServiceProvider_AddRef,
1028 ServiceProvider_Release,
1029 ServiceProvider_QueryService
1032 static HRESULT get_callback(IBindCtx *pbc, IBindStatusCallback **callback)
1036 static WCHAR wszBSCBHolder[] = { '_','B','S','C','B','_','H','o','l','d','e','r','_',0 };
1038 hres = IBindCtx_GetObjectParam(pbc, wszBSCBHolder, (IUnknown**)callback);
1045 static HRESULT get_protocol(Binding *This, LPCWSTR url)
1047 IClassFactory *cf = NULL;
1050 hres = IBindStatusCallback_QueryInterface(This->callback, &IID_IInternetProtocol,
1051 (void**)&This->protocol);
1055 if(This->service_provider) {
1056 hres = IServiceProvider_QueryService(This->service_provider, &IID_IInternetProtocol,
1057 &IID_IInternetProtocol, (void**)&This->protocol);
1062 hres = get_protocol_handler(url, &cf);
1066 hres = IClassFactory_CreateInstance(cf, NULL, &IID_IInternetProtocol, (void**)&This->protocol);
1067 IClassFactory_Release(cf);
1072 static HRESULT Binding_Create(LPCWSTR url, IBindCtx *pbc, REFIID riid, Binding **binding)
1078 static const WCHAR wszFile[] = {'f','i','l','e',':'};
1080 if(!IsEqualGUID(&IID_IStream, riid)) {
1081 FIXME("Unsupported riid %s\n", debugstr_guid(riid));
1085 URLMON_LockModule();
1087 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(Binding));
1089 ret->lpBindingVtbl = &BindingVtbl;
1090 ret->lpInternetProtocolSinkVtbl = &InternetProtocolSinkVtbl;
1091 ret->lpInternetBindInfoVtbl = &InternetBindInfoVtbl;
1092 ret->lpServiceProviderVtbl = &ServiceProviderVtbl;
1096 ret->callback = NULL;
1097 ret->protocol = NULL;
1098 ret->service_provider = NULL;
1102 ret->apartment_thread = GetCurrentThreadId();
1103 ret->notif_hwnd = get_notif_hwnd();
1104 ret->verified_mime = FALSE;
1105 ret->continue_call = 0;
1106 ret->task_queue_head = ret->task_queue_tail = NULL;
1108 memset(&ret->bindinfo, 0, sizeof(BINDINFO));
1109 ret->bindinfo.cbSize = sizeof(BINDINFO);
1112 InitializeCriticalSection(&ret->section);
1114 hres = get_callback(pbc, &ret->callback);
1116 WARN("Could not get IBindStatusCallback\n");
1117 IBinding_Release(BINDING(ret));
1121 IBindStatusCallback_QueryInterface(ret->callback, &IID_IServiceProvider,
1122 (void**)&ret->service_provider);
1124 hres = get_protocol(ret, url);
1126 WARN("Could not get protocol handler\n");
1127 IBinding_Release(BINDING(ret));
1131 hres = IBindStatusCallback_GetBindInfo(ret->callback, &ret->bindf, &ret->bindinfo);
1133 WARN("GetBindInfo failed: %08lx\n", hres);
1134 IBinding_Release(BINDING(ret));
1138 dump_BINDINFO(&ret->bindinfo);
1140 ret->bindf |= BINDF_FROMURLMON;
1142 len = strlenW(url)+1;
1144 if(len < sizeof(wszFile)/sizeof(WCHAR) || memcmp(wszFile, url, sizeof(wszFile)))
1145 ret->bindf |= BINDF_NEEDFILE;
1147 ret->url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1148 memcpy(ret->url, url, len*sizeof(WCHAR));
1150 ret->stream = create_stream(ret->protocol);
1151 ret->stgmed.tymed = TYMED_ISTREAM;
1152 ret->stgmed.u.pstm = STREAM(ret->stream);
1153 ret->stgmed.pUnkForRelease = (IUnknown*)BINDING(ret); /* NOTE: Windows uses other IUnknown */
1159 HRESULT start_binding(LPCWSTR url, IBindCtx *pbc, REFIID riid, void **ppv)
1161 Binding *binding = NULL;
1166 hres = Binding_Create(url, pbc, riid, &binding);
1170 hres = IBindStatusCallback_OnStartBinding(binding->callback, 0, BINDING(binding));
1172 WARN("OnStartBinding failed: %08lx\n", hres);
1173 IBindStatusCallback_OnStopBinding(binding->callback, 0x800c0008, NULL);
1174 IBinding_Release(BINDING(binding));
1178 hres = IInternetProtocol_Start(binding->protocol, url, PROTSINK(binding),
1179 BINDINF(binding), 0, 0);
1182 WARN("Start failed: %08lx\n", hres);
1184 IInternetProtocol_Terminate(binding->protocol, 0);
1185 IBindStatusCallback_OnStopBinding(binding->callback, S_OK, NULL);
1186 IBinding_Release(BINDING(binding));
1191 if(binding->stream->init_buf) {
1192 IInternetProtocol_UnlockRequest(binding->protocol);
1194 IStream_AddRef(STREAM(binding->stream));
1195 *ppv = binding->stream;
1199 hres = MK_S_ASYNCHRONOUS;
1202 IBinding_Release(BINDING(binding));