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;
53 typedef struct _task_t {
57 on_progress_data on_progress;
58 PROTOCOLDATA *protocol_data;
64 const IStreamVtbl *lpStreamVtbl;
68 IInternetProtocol *protocol;
76 const IBindingVtbl *lpBindingVtbl;
77 const IInternetProtocolSinkVtbl *lpInternetProtocolSinkVtbl;
78 const IInternetBindInfoVtbl *lpInternetBindInfoVtbl;
79 const IServiceProviderVtbl *lpServiceProviderVtbl;
83 IBindStatusCallback *callback;
84 IInternetProtocol *protocol;
85 IServiceProvider *service_provider;
86 ProtocolStream *stream;
95 DWORD apartment_thread;
100 task_t *task_queue_head, *task_queue_tail;
101 CRITICAL_SECTION section;
104 #define BINDING(x) ((IBinding*) &(x)->lpBindingVtbl)
105 #define PROTSINK(x) ((IInternetProtocolSink*) &(x)->lpInternetProtocolSinkVtbl)
106 #define BINDINF(x) ((IInternetBindInfo*) &(x)->lpInternetBindInfoVtbl)
107 #define SERVPROV(x) ((IServiceProvider*) &(x)->lpServiceProviderVtbl)
109 #define STREAM(x) ((IStream*) &(x)->lpStreamVtbl)
111 #define WM_MK_CONTINUE (WM_USER+101)
113 static void push_task(Binding *binding, task_t *task)
117 EnterCriticalSection(&binding->section);
119 if(binding->task_queue_tail)
120 binding->task_queue_tail->next = task;
122 binding->task_queue_tail = binding->task_queue_head = task;
124 LeaveCriticalSection(&binding->section);
127 static task_t *pop_task(Binding *binding)
131 EnterCriticalSection(&binding->section);
133 ret = binding->task_queue_head;
135 binding->task_queue_head = ret->next;
136 if(!binding->task_queue_head)
137 binding->task_queue_tail = NULL;
140 LeaveCriticalSection(&binding->section);
145 static void fill_stream_buffer(ProtocolStream *This)
150 if(sizeof(This->buf) == This->buf_size)
153 hres = IInternetProtocol_Read(This->protocol, This->buf+This->buf_size,
154 sizeof(This->buf)-This->buf_size, &read);
155 if(SUCCEEDED(hres)) {
156 This->buf_size += read;
157 This->init_buf = TRUE;
161 static void on_data_available(Binding *binding, DWORD bscf)
163 FORMATETC formatetc = {0, NULL, 1, -1, TYMED_ISTREAM};
165 if(GetCurrentThreadId() != binding->apartment_thread)
166 FIXME("called from worked hread\n");
168 if(binding->continue_call) {
169 task_t *task = HeapAlloc(GetProcessHeap(), 0, sizeof(task_t));
170 task->task = TASK_DATA_AVAILABLE;
171 task->data.bscf = bscf;
173 push_task(binding, task);
178 fill_stream_buffer(binding->stream);
180 IBindStatusCallback_OnDataAvailable(binding->callback, bscf, binding->stream->buf_size,
181 &formatetc, &binding->stgmed);
185 static void do_task(Binding *binding, task_t *task)
188 case TASK_ON_PROGRESS: {
189 on_progress_data *data = &task->data.on_progress;
191 IBindStatusCallback_OnProgress(binding->callback, data->progress,
192 data->progress_max, data->status_code, data->status_text);
194 HeapFree(GetProcessHeap(), 0, data->status_text);
195 HeapFree(GetProcessHeap(), 0, data);
200 binding->continue_call++;
201 IInternetProtocol_Continue(binding->protocol, task->data.protocol_data);
202 binding->continue_call--;
205 case TASK_DATA_AVAILABLE:
206 on_data_available(binding, task->data.bscf);
210 static void do_tasks(Binding *This)
214 while((task = pop_task(This)))
218 static LRESULT WINAPI notif_wnd_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
220 if(msg == WM_MK_CONTINUE) {
221 Binding *binding = (Binding*)lParam;
225 IBinding_Release(BINDING(binding));
229 return DefWindowProcW(hwnd, msg, wParam, lParam);
232 static HWND get_notif_hwnd(void)
234 static ATOM wnd_class = 0;
237 static const WCHAR wszURLMonikerNotificationWindow[] =
238 {'U','R','L',' ','M','o','n','i','k','e','r',' ',
239 'N','o','t','i','f','i','c','a','t','i','o','n',' ','W','i','n','d','o','w',0};
242 static WNDCLASSEXW wndclass = {
244 notif_wnd_proc, 0, 0,
245 NULL, NULL, NULL, NULL, NULL,
246 wszURLMonikerNotificationWindow,
250 wndclass.hInstance = URLMON_hInstance;
252 wnd_class = RegisterClassExW(&wndclass);
256 urlmon_tls = TlsAlloc();
258 hwnd = TlsGetValue(urlmon_tls);
262 hwnd = CreateWindowExW(0, MAKEINTATOMW(wnd_class),
263 wszURLMonikerNotificationWindow, 0, 0, 0, 0, 0, HWND_MESSAGE,
264 NULL, URLMON_hInstance, NULL);
265 TlsSetValue(urlmon_tls, hwnd);
267 TRACE("hwnd = %p\n", hwnd);
272 static void on_progress(Binding *This, ULONG progress, ULONG progress_max,
273 ULONG status_code, LPCWSTR status_text)
277 if(GetCurrentThreadId() == This->apartment_thread && !This->continue_call) {
278 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
279 status_code, status_text);
283 task = HeapAlloc(GetProcessHeap(), 0, sizeof(task_t));
285 task->task = TASK_ON_PROGRESS;
286 task->data.on_progress.progress = progress;
287 task->data.on_progress.progress_max = progress_max;
288 task->data.on_progress.status_code = status_code;
291 DWORD size = (strlenW(status_text)+1)*sizeof(WCHAR);
293 task->data.on_progress.status_text = HeapAlloc(GetProcessHeap(), 0, size);
294 memcpy(task->data.on_progress.status_text, status_text, size);
296 task->data.on_progress.status_text = NULL;
299 push_task(This, task);
301 if(GetCurrentThreadId() != This->apartment_thread) {
302 IBinding_AddRef(BINDING(This));
303 PostMessageW(This->notif_hwnd, WM_MK_CONTINUE, 0, (LPARAM)This);
307 static void dump_BINDINFO(BINDINFO *bi)
309 static const char *BINDINFOF_str[] = {
311 "BINDINFOF_URLENCODESTGMEDDATA",
312 "BINDINFOF_URLENCODEDEXTRAINFO"
315 static const char *BINDVERB_str[] = {
329 " %ld, %08lx, %ld, %ld\n"
335 bi->cbSize, debugstr_w(bi->szExtraInfo),
336 bi->stgmedData.tymed, bi->stgmedData.u.hGlobal, bi->stgmedData.pUnkForRelease,
337 bi->grfBindInfoF > BINDINFOF_URLENCODEDEXTRAINFO
338 ? "unknown" : BINDINFOF_str[bi->grfBindInfoF],
339 bi->dwBindVerb > BINDVERB_CUSTOM
340 ? "unknown" : BINDVERB_str[bi->dwBindVerb],
341 debugstr_w(bi->szCustomVerb),
342 bi->cbStgmedData, bi->dwOptions, bi->dwOptionsFlags, bi->dwCodePage,
343 bi->securityAttributes.nLength,
344 bi->securityAttributes.lpSecurityDescriptor,
345 bi->securityAttributes.bInheritHandle,
346 debugstr_guid(&bi->iid),
347 bi->pUnk, bi->dwReserved
351 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
352 REFIID riid, void **ppv)
356 if(IsEqualGUID(&IID_IUnknown, riid)) {
357 TRACE("(IID_IUnknown %p)\n", ppv);
359 }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
360 TRACE("(IID_IHttpNegotiate %p)\n", ppv);
362 }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
363 TRACE("(IID_IHttpNegotiate2 %p)\n", ppv);
368 IHttpNegotiate2_AddRef(iface);
372 WARN("Unsupported interface %s\n", debugstr_guid(riid));
373 return E_NOINTERFACE;
376 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
382 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
384 URLMON_UnlockModule();
388 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
389 LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
391 TRACE("(%s %s %ld %p)\n", debugstr_w(szURL), debugstr_w(szHeaders), dwReserved,
392 pszAdditionalHeaders);
394 *pszAdditionalHeaders = NULL;
398 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
399 LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders,
400 LPWSTR *pszAdditionalRequestHeaders)
402 TRACE("(%ld %s %s %p)\n", dwResponseCode, debugstr_w(szResponseHeaders),
403 debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
405 if(pszAdditionalRequestHeaders)
406 *pszAdditionalRequestHeaders = NULL;
410 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
411 BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
413 TRACE("(%p %p %ld)\n", pbSecurityId, pcbSecurityId, dwReserved);
415 /* That's all we have to do here */
419 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
420 HttpNegotiate_QueryInterface,
421 HttpNegotiate_AddRef,
422 HttpNegotiate_Release,
423 HttpNegotiate_BeginningTransaction,
424 HttpNegotiate_OnResponse,
425 HttpNegotiate_GetRootSecurityId
428 static IHttpNegotiate2 HttpNegotiate = { &HttpNegotiate2Vtbl };
430 #define STREAM_THIS(iface) DEFINE_THIS(ProtocolStream, Stream, iface)
432 static HRESULT WINAPI ProtocolStream_QueryInterface(IStream *iface,
433 REFIID riid, void **ppv)
435 ProtocolStream *This = STREAM_THIS(iface);
439 if(IsEqualGUID(&IID_IUnknown, riid)) {
440 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
442 }else if(IsEqualGUID(&IID_ISequentialStream, riid)) {
443 TRACE("(%p)->(IID_ISequentialStream %p)\n", This, ppv);
445 }else if(IsEqualGUID(&IID_IStream, riid)) {
446 TRACE("(%p)->(IID_IStream %p)\n", This, ppv);
451 IStream_AddRef(STREAM(This));
455 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
456 return E_NOINTERFACE;
459 static ULONG WINAPI ProtocolStream_AddRef(IStream *iface)
461 ProtocolStream *This = STREAM_THIS(iface);
462 LONG ref = InterlockedIncrement(&This->ref);
464 TRACE("(%p) ref=%ld\n", This, ref);
469 static ULONG WINAPI ProtocolStream_Release(IStream *iface)
471 ProtocolStream *This = STREAM_THIS(iface);
472 LONG ref = InterlockedDecrement(&This->ref);
474 TRACE("(%p) ref=%ld\n", This, ref);
477 IInternetProtocol_Release(This->protocol);
478 HeapFree(GetProcessHeap(), 0, This);
480 URLMON_UnlockModule();
486 static HRESULT WINAPI ProtocolStream_Read(IStream *iface, void *pv,
487 ULONG cb, ULONG *pcbRead)
489 ProtocolStream *This = STREAM_THIS(iface);
490 DWORD read = 0, pread = 0;
493 TRACE("(%p)->(%p %ld %p)\n", This, pv, cb, pcbRead);
498 if(read > This->buf_size)
499 read = This->buf_size;
501 memcpy(pv, This->buf, read);
503 if(read < This->buf_size)
504 memmove(This->buf, This->buf+read, This->buf_size-read);
505 This->buf_size -= read;
513 hres = IInternetProtocol_Read(This->protocol, (PBYTE)pv+read, cb-read, &pread);
514 *pcbRead = read + pread;
516 if(hres == E_PENDING)
518 else if(FAILED(hres))
519 FIXME("Read failed: %08lx\n", hres);
521 return read || pread ? S_OK : S_FALSE;
524 static HRESULT WINAPI ProtocolStream_Write(IStream *iface, const void *pv,
525 ULONG cb, ULONG *pcbWritten)
527 ProtocolStream *This = STREAM_THIS(iface);
529 TRACE("(%p)->(%p %ld %p)\n", This, pv, cb, pcbWritten);
531 return STG_E_ACCESSDENIED;
534 static HRESULT WINAPI ProtocolStream_Seek(IStream *iface, LARGE_INTEGER dlibMove,
535 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
537 ProtocolStream *This = STREAM_THIS(iface);
538 FIXME("(%p)->(%ld %08lx %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
542 static HRESULT WINAPI ProtocolStream_SetSize(IStream *iface, ULARGE_INTEGER libNewSize)
544 ProtocolStream *This = STREAM_THIS(iface);
545 FIXME("(%p)->(%ld)\n", This, libNewSize.u.LowPart);
549 static HRESULT WINAPI ProtocolStream_CopyTo(IStream *iface, IStream *pstm,
550 ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten)
552 ProtocolStream *This = STREAM_THIS(iface);
553 FIXME("(%p)->(%p %ld %p %p)\n", This, pstm, cb.u.LowPart, pcbRead, pcbWritten);
557 static HRESULT WINAPI ProtocolStream_Commit(IStream *iface, DWORD grfCommitFlags)
559 ProtocolStream *This = STREAM_THIS(iface);
561 TRACE("(%p)->(%08lx)\n", This, grfCommitFlags);
566 static HRESULT WINAPI ProtocolStream_Revert(IStream *iface)
568 ProtocolStream *This = STREAM_THIS(iface);
570 TRACE("(%p)\n", This);
575 static HRESULT WINAPI ProtocolStream_LockRegion(IStream *iface, ULARGE_INTEGER libOffset,
576 ULARGE_INTEGER cb, DWORD dwLockType)
578 ProtocolStream *This = STREAM_THIS(iface);
579 FIXME("(%p)->(%ld %ld %ld)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
583 static HRESULT WINAPI ProtocolStream_UnlockRegion(IStream *iface,
584 ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
586 ProtocolStream *This = STREAM_THIS(iface);
587 FIXME("(%p)->(%ld %ld %ld)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
591 static HRESULT WINAPI ProtocolStream_Stat(IStream *iface, STATSTG *pstatstg,
594 ProtocolStream *This = STREAM_THIS(iface);
595 FIXME("(%p)->(%p %08lx)\n", This, pstatstg, dwStatFlag);
599 static HRESULT WINAPI ProtocolStream_Clone(IStream *iface, IStream **ppstm)
601 ProtocolStream *This = STREAM_THIS(iface);
602 FIXME("(%p)->(%p)\n", This, ppstm);
608 static const IStreamVtbl ProtocolStreamVtbl = {
609 ProtocolStream_QueryInterface,
610 ProtocolStream_AddRef,
611 ProtocolStream_Release,
613 ProtocolStream_Write,
615 ProtocolStream_SetSize,
616 ProtocolStream_CopyTo,
617 ProtocolStream_Commit,
618 ProtocolStream_Revert,
619 ProtocolStream_LockRegion,
620 ProtocolStream_UnlockRegion,
625 #define BINDING_THIS(iface) DEFINE_THIS(Binding, Binding, iface)
627 static ProtocolStream *create_stream(IInternetProtocol *protocol)
629 ProtocolStream *ret = HeapAlloc(GetProcessHeap(), 0, sizeof(ProtocolStream));
631 ret->lpStreamVtbl = &ProtocolStreamVtbl;
634 ret->init_buf = FALSE;
636 IInternetProtocol_AddRef(protocol);
637 ret->protocol = protocol;
644 static HRESULT WINAPI Binding_QueryInterface(IBinding *iface, REFIID riid, void **ppv)
646 Binding *This = BINDING_THIS(iface);
650 if(IsEqualGUID(&IID_IUnknown, riid)) {
651 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
652 *ppv = BINDING(This);
653 }else if(IsEqualGUID(&IID_IBinding, riid)) {
654 TRACE("(%p)->(IID_IBinding %p)\n", This, ppv);
655 *ppv = BINDING(This);
656 }else if(IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
657 TRACE("(%p)->(IID_IInternetProtocolSink %p)\n", This, ppv);
658 *ppv = PROTSINK(This);
659 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
660 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
661 *ppv = BINDINF(This);
662 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
663 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
664 *ppv = SERVPROV(This);
668 IBinding_AddRef(BINDING(This));
672 WARN("Unsupported interface %s\n", debugstr_guid(riid));
673 return E_NOINTERFACE;
676 static ULONG WINAPI Binding_AddRef(IBinding *iface)
678 Binding *This = BINDING_THIS(iface);
679 LONG ref = InterlockedIncrement(&This->ref);
681 TRACE("(%p) ref=%ld\n", This, ref);
686 static ULONG WINAPI Binding_Release(IBinding *iface)
688 Binding *This = BINDING_THIS(iface);
689 LONG ref = InterlockedDecrement(&This->ref);
691 TRACE("(%p) ref=%ld\n", This, ref);
695 IBindStatusCallback_Release(This->callback);
697 IInternetProtocol_Release(This->protocol);
698 if(This->service_provider)
699 IServiceProvider_Release(This->service_provider);
701 IStream_Release(STREAM(This->stream));
703 ReleaseBindInfo(&This->bindinfo);
704 DeleteCriticalSection(&This->section);
705 HeapFree(GetProcessHeap(), 0, This->mime);
706 HeapFree(GetProcessHeap(), 0, This->url);
708 HeapFree(GetProcessHeap(), 0, This);
710 URLMON_UnlockModule();
716 static HRESULT WINAPI Binding_Abort(IBinding *iface)
718 Binding *This = BINDING_THIS(iface);
719 FIXME("(%p)\n", This);
723 static HRESULT WINAPI Binding_Suspend(IBinding *iface)
725 Binding *This = BINDING_THIS(iface);
726 FIXME("(%p)\n", This);
730 static HRESULT WINAPI Binding_Resume(IBinding *iface)
732 Binding *This = BINDING_THIS(iface);
733 FIXME("(%p)\n", This);
737 static HRESULT WINAPI Binding_SetPriority(IBinding *iface, LONG nPriority)
739 Binding *This = BINDING_THIS(iface);
740 FIXME("(%p)->(%ld)\n", This, nPriority);
744 static HRESULT WINAPI Binding_GetPriority(IBinding *iface, LONG *pnPriority)
746 Binding *This = BINDING_THIS(iface);
747 FIXME("(%p)->(%p)\n", This, pnPriority);
751 static HRESULT WINAPI Binding_GetBindResult(IBinding *iface, CLSID *pclsidProtocol,
752 DWORD *pdwResult, LPOLESTR *pszResult, DWORD *pdwReserved)
754 Binding *This = BINDING_THIS(iface);
755 FIXME("(%p)->(%p %p %p %p)\n", This, pclsidProtocol, pdwResult, pszResult, pdwReserved);
761 static const IBindingVtbl BindingVtbl = {
762 Binding_QueryInterface,
770 Binding_GetBindResult
773 #define PROTSINK_THIS(iface) DEFINE_THIS(Binding, InternetProtocolSink, iface)
775 static HRESULT WINAPI InternetProtocolSink_QueryInterface(IInternetProtocolSink *iface,
776 REFIID riid, void **ppv)
778 Binding *This = PROTSINK_THIS(iface);
779 return IBinding_QueryInterface(BINDING(This), riid, ppv);
782 static ULONG WINAPI InternetProtocolSink_AddRef(IInternetProtocolSink *iface)
784 Binding *This = PROTSINK_THIS(iface);
785 return IBinding_AddRef(BINDING(This));
788 static ULONG WINAPI InternetProtocolSink_Release(IInternetProtocolSink *iface)
790 Binding *This = PROTSINK_THIS(iface);
791 return IBinding_Release(BINDING(This));
794 static HRESULT WINAPI InternetProtocolSink_Switch(IInternetProtocolSink *iface,
795 PROTOCOLDATA *pProtocolData)
797 Binding *This = PROTSINK_THIS(iface);
800 TRACE("(%p)->(%p)\n", This, pProtocolData);
802 task = HeapAlloc(GetProcessHeap(), 0, sizeof(task_t));
803 task->task = TASK_SWITCH;
804 task->data.protocol_data = pProtocolData;
806 push_task(This, task);
808 IBinding_AddRef(BINDING(This));
809 PostMessageW(This->notif_hwnd, WM_MK_CONTINUE, 0, (LPARAM)This);
814 static HRESULT WINAPI InternetProtocolSink_ReportProgress(IInternetProtocolSink *iface,
815 ULONG ulStatusCode, LPCWSTR szStatusText)
817 Binding *This = PROTSINK_THIS(iface);
819 TRACE("(%p)->(%lu %s)\n", This, ulStatusCode, debugstr_w(szStatusText));
821 switch(ulStatusCode) {
822 case BINDSTATUS_FINDINGRESOURCE:
823 on_progress(This, 0, 0, BINDSTATUS_FINDINGRESOURCE, szStatusText);
825 case BINDSTATUS_CONNECTING:
826 on_progress(This, 0, 0, BINDSTATUS_CONNECTING, szStatusText);
828 case BINDSTATUS_MIMETYPEAVAILABLE: {
829 int len = strlenW(szStatusText)+1;
830 This->mime = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
831 memcpy(This->mime, szStatusText, len*sizeof(WCHAR));
834 case BINDSTATUS_SENDINGREQUEST:
835 on_progress(This, 0, 0, BINDSTATUS_SENDINGREQUEST, szStatusText);
837 case BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE:
838 This->verified_mime = TRUE;
839 on_progress(This, 0, 0, BINDSTATUS_MIMETYPEAVAILABLE, szStatusText);
841 case BINDSTATUS_CACHEFILENAMEAVAILABLE:
844 FIXME("Unhandled status code %ld\n", ulStatusCode);
851 static HRESULT WINAPI InternetProtocolSink_ReportData(IInternetProtocolSink *iface,
852 DWORD grfBSCF, ULONG ulProgress, ULONG ulProgressMax)
854 Binding *This = PROTSINK_THIS(iface);
856 TRACE("(%p)->(%ld %lu %lu)\n", This, grfBSCF, ulProgress, ulProgressMax);
858 if(!This->verified_mime) {
861 This->verified_mime = TRUE;
863 fill_stream_buffer(This->stream);
865 FindMimeFromData(NULL, This->url, This->stream->buf,
866 min(This->stream->buf_size, 255), This->mime, 0, &mime, 0);
868 on_progress(This, ulProgress, ulProgressMax, BINDSTATUS_MIMETYPEAVAILABLE, mime);
871 if(grfBSCF & BSCF_FIRSTDATANOTIFICATION) {
872 on_progress(This, ulProgress, ulProgressMax, BINDSTATUS_BEGINDOWNLOADDATA, This->url);
875 if(grfBSCF & BSCF_LASTDATANOTIFICATION)
876 on_progress(This, ulProgress, ulProgressMax, BINDSTATUS_ENDDOWNLOADDATA, This->url);
878 if(grfBSCF & BSCF_FIRSTDATANOTIFICATION)
879 IInternetProtocol_LockRequest(This->protocol, 0);
881 on_data_available(This, grfBSCF);
883 if(grfBSCF & BSCF_LASTDATANOTIFICATION)
884 IBindStatusCallback_OnStopBinding(This->callback, S_OK, NULL);
889 static HRESULT WINAPI InternetProtocolSink_ReportResult(IInternetProtocolSink *iface,
890 HRESULT hrResult, DWORD dwError, LPCWSTR szResult)
892 Binding *This = PROTSINK_THIS(iface);
894 TRACE("(%p)->(%08lx %ld %s)\n", This, hrResult, dwError, debugstr_w(szResult));
896 IInternetProtocol_Terminate(This->protocol, 0);
902 static const IInternetProtocolSinkVtbl InternetProtocolSinkVtbl = {
903 InternetProtocolSink_QueryInterface,
904 InternetProtocolSink_AddRef,
905 InternetProtocolSink_Release,
906 InternetProtocolSink_Switch,
907 InternetProtocolSink_ReportProgress,
908 InternetProtocolSink_ReportData,
909 InternetProtocolSink_ReportResult
912 #define BINDINF_THIS(iface) DEFINE_THIS(Binding, InternetBindInfo, iface)
914 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
915 REFIID riid, void **ppv)
917 Binding *This = BINDINF_THIS(iface);
918 return IBinding_QueryInterface(BINDING(This), riid, ppv);
921 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
923 Binding *This = BINDINF_THIS(iface);
924 return IBinding_AddRef(BINDING(This));
927 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
929 Binding *This = BINDINF_THIS(iface);
930 return IBinding_Release(BINDING(This));
933 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
934 DWORD *grfBINDF, BINDINFO *pbindinfo)
936 Binding *This = BINDINF_THIS(iface);
938 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
940 *grfBINDF = This->bindf;
942 memcpy(pbindinfo, &This->bindinfo, sizeof(BINDINFO));
944 if(pbindinfo->szExtraInfo || pbindinfo->szCustomVerb)
945 FIXME("copy strings\n");
948 IUnknown_AddRef(pbindinfo->pUnk);
953 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
954 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
956 Binding *This = BINDINF_THIS(iface);
958 TRACE("(%p)->(%ld %p %ld %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
960 switch(ulStringType) {
961 case BINDSTRING_ACCEPT_MIMES: {
962 static const WCHAR wszMimes[] = {'*','/','*',0};
964 if(!ppwzStr || !pcElFetched)
967 ppwzStr[0] = CoTaskMemAlloc(sizeof(wszMimes));
968 memcpy(ppwzStr[0], wszMimes, sizeof(wszMimes));
972 case BINDSTRING_USER_AGENT: {
973 IInternetBindInfo *bindinfo = NULL;
976 hres = IBindStatusCallback_QueryInterface(This->callback, &IID_IInternetBindInfo,
981 hres = IInternetBindInfo_GetBindString(bindinfo, ulStringType, ppwzStr,
983 IInternetBindInfo_Release(bindinfo);
989 FIXME("not supported string type %ld\n", ulStringType);
995 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
996 InternetBindInfo_QueryInterface,
997 InternetBindInfo_AddRef,
998 InternetBindInfo_Release,
999 InternetBindInfo_GetBindInfo,
1000 InternetBindInfo_GetBindString
1003 #define SERVPROV_THIS(iface) DEFINE_THIS(Binding, ServiceProvider, iface)
1005 static HRESULT WINAPI ServiceProvider_QueryInterface(IServiceProvider *iface,
1006 REFIID riid, void **ppv)
1008 Binding *This = SERVPROV_THIS(iface);
1009 return IBinding_QueryInterface(BINDING(This), riid, ppv);
1012 static ULONG WINAPI ServiceProvider_AddRef(IServiceProvider *iface)
1014 Binding *This = SERVPROV_THIS(iface);
1015 return IBinding_AddRef(BINDING(This));
1018 static ULONG WINAPI ServiceProvider_Release(IServiceProvider *iface)
1020 Binding *This = SERVPROV_THIS(iface);
1021 return IBinding_Release(BINDING(This));
1024 static HRESULT WINAPI ServiceProvider_QueryService(IServiceProvider *iface,
1025 REFGUID guidService, REFIID riid, void **ppv)
1027 Binding *This = SERVPROV_THIS(iface);
1030 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
1032 if(This->service_provider) {
1033 hres = IServiceProvider_QueryService(This->service_provider, guidService,
1039 if(IsEqualGUID(&IID_IHttpNegotiate, guidService)
1040 || IsEqualGUID(&IID_IHttpNegotiate2, guidService))
1041 return IHttpNegotiate2_QueryInterface(&HttpNegotiate, riid, ppv);
1043 WARN("unknown service %s\n", debugstr_guid(guidService));
1047 #undef SERVPROV_THIS
1049 static const IServiceProviderVtbl ServiceProviderVtbl = {
1050 ServiceProvider_QueryInterface,
1051 ServiceProvider_AddRef,
1052 ServiceProvider_Release,
1053 ServiceProvider_QueryService
1056 static HRESULT get_callback(IBindCtx *pbc, IBindStatusCallback **callback)
1060 static WCHAR wszBSCBHolder[] = { '_','B','S','C','B','_','H','o','l','d','e','r','_',0 };
1062 hres = IBindCtx_GetObjectParam(pbc, wszBSCBHolder, (IUnknown**)callback);
1069 static HRESULT get_protocol(Binding *This, LPCWSTR url)
1071 IClassFactory *cf = NULL;
1074 hres = IBindStatusCallback_QueryInterface(This->callback, &IID_IInternetProtocol,
1075 (void**)&This->protocol);
1079 if(This->service_provider) {
1080 hres = IServiceProvider_QueryService(This->service_provider, &IID_IInternetProtocol,
1081 &IID_IInternetProtocol, (void**)&This->protocol);
1086 hres = get_protocol_handler(url, &cf);
1090 hres = IClassFactory_CreateInstance(cf, NULL, &IID_IInternetProtocol, (void**)&This->protocol);
1091 IClassFactory_Release(cf);
1096 static HRESULT Binding_Create(LPCWSTR url, IBindCtx *pbc, REFIID riid, Binding **binding)
1102 static const WCHAR wszFile[] = {'f','i','l','e',':'};
1104 if(!IsEqualGUID(&IID_IStream, riid)) {
1105 FIXME("Unsupported riid %s\n", debugstr_guid(riid));
1109 URLMON_LockModule();
1111 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(Binding));
1113 ret->lpBindingVtbl = &BindingVtbl;
1114 ret->lpInternetProtocolSinkVtbl = &InternetProtocolSinkVtbl;
1115 ret->lpInternetBindInfoVtbl = &InternetBindInfoVtbl;
1116 ret->lpServiceProviderVtbl = &ServiceProviderVtbl;
1120 ret->callback = NULL;
1121 ret->protocol = NULL;
1122 ret->service_provider = NULL;
1126 ret->apartment_thread = GetCurrentThreadId();
1127 ret->notif_hwnd = get_notif_hwnd();
1128 ret->verified_mime = FALSE;
1129 ret->continue_call = 0;
1130 ret->task_queue_head = ret->task_queue_tail = NULL;
1132 memset(&ret->bindinfo, 0, sizeof(BINDINFO));
1133 ret->bindinfo.cbSize = sizeof(BINDINFO);
1136 InitializeCriticalSection(&ret->section);
1138 hres = get_callback(pbc, &ret->callback);
1140 WARN("Could not get IBindStatusCallback\n");
1141 IBinding_Release(BINDING(ret));
1145 IBindStatusCallback_QueryInterface(ret->callback, &IID_IServiceProvider,
1146 (void**)&ret->service_provider);
1148 hres = get_protocol(ret, url);
1150 WARN("Could not get protocol handler\n");
1151 IBinding_Release(BINDING(ret));
1155 hres = IBindStatusCallback_GetBindInfo(ret->callback, &ret->bindf, &ret->bindinfo);
1157 WARN("GetBindInfo failed: %08lx\n", hres);
1158 IBinding_Release(BINDING(ret));
1162 dump_BINDINFO(&ret->bindinfo);
1164 ret->bindf |= BINDF_FROMURLMON;
1166 len = strlenW(url)+1;
1168 if(len < sizeof(wszFile)/sizeof(WCHAR) || memcmp(wszFile, url, sizeof(wszFile)))
1169 ret->bindf |= BINDF_NEEDFILE;
1171 ret->url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1172 memcpy(ret->url, url, len*sizeof(WCHAR));
1174 ret->stream = create_stream(ret->protocol);
1175 ret->stgmed.tymed = TYMED_ISTREAM;
1176 ret->stgmed.u.pstm = STREAM(ret->stream);
1177 ret->stgmed.pUnkForRelease = (IUnknown*)BINDING(ret); /* NOTE: Windows uses other IUnknown */
1183 HRESULT start_binding(LPCWSTR url, IBindCtx *pbc, REFIID riid, void **ppv)
1185 Binding *binding = NULL;
1190 hres = Binding_Create(url, pbc, riid, &binding);
1194 hres = IBindStatusCallback_OnStartBinding(binding->callback, 0, BINDING(binding));
1196 WARN("OnStartBinding failed: %08lx\n", hres);
1197 IBindStatusCallback_OnStopBinding(binding->callback, 0x800c0008, NULL);
1198 IBinding_Release(BINDING(binding));
1202 hres = IInternetProtocol_Start(binding->protocol, url, PROTSINK(binding),
1203 BINDINF(binding), 0, 0);
1206 WARN("Start failed: %08lx\n", hres);
1208 IInternetProtocol_Terminate(binding->protocol, 0);
1209 IBindStatusCallback_OnStopBinding(binding->callback, S_OK, NULL);
1210 IBinding_Release(BINDING(binding));
1215 if(binding->stream->init_buf) {
1216 IInternetProtocol_UnlockRequest(binding->protocol);
1218 IStream_AddRef(STREAM(binding->stream));
1219 *ppv = binding->stream;
1223 hres = MK_S_ASYNCHRONOUS;
1226 IBinding_Release(BINDING(binding));