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"
20 #include "wine/debug.h"
22 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
24 typedef struct Binding Binding;
26 struct _task_header_t;
28 typedef void (*task_proc_t)(Binding*, struct _task_header_t*);
30 typedef struct _task_header_t {
32 struct _task_header_t *next;
36 const IStreamVtbl *lpStreamVtbl;
40 IInternetProtocol *protocol;
54 #define BINDING_LOCKED 0x0001
57 const IBindingVtbl *lpBindingVtbl;
58 const IInternetProtocolSinkVtbl *lpInternetProtocolSinkVtbl;
59 const IInternetBindInfoVtbl *lpInternetBindInfoVtbl;
60 const IServiceProviderVtbl *lpServiceProviderVtbl;
64 IBindStatusCallback *callback;
65 IInternetProtocol *protocol;
66 IServiceProvider *service_provider;
67 ProtocolStream *stream;
72 UINT clipboard_format;
77 download_state_t download_state;
79 DWORD apartment_thread;
84 task_header_t *task_queue_head, *task_queue_tail;
85 CRITICAL_SECTION section;
88 #define BINDING(x) ((IBinding*) &(x)->lpBindingVtbl)
89 #define PROTSINK(x) ((IInternetProtocolSink*) &(x)->lpInternetProtocolSinkVtbl)
90 #define BINDINF(x) ((IInternetBindInfo*) &(x)->lpInternetBindInfoVtbl)
91 #define SERVPROV(x) ((IServiceProvider*) &(x)->lpServiceProviderVtbl)
93 #define STREAM(x) ((IStream*) &(x)->lpStreamVtbl)
94 #define HTTPNEG2(x) ((IHttpNegotiate2*) &(x)->lpHttpNegotiate2Vtbl)
96 #define WM_MK_CONTINUE (WM_USER+101)
98 static void push_task(Binding *binding, task_header_t *task, task_proc_t proc)
103 EnterCriticalSection(&binding->section);
105 if(binding->task_queue_tail) {
106 binding->task_queue_tail->next = task;
107 binding->task_queue_tail = task;
109 binding->task_queue_tail = binding->task_queue_head = task;
112 LeaveCriticalSection(&binding->section);
115 static task_header_t *pop_task(Binding *binding)
119 EnterCriticalSection(&binding->section);
121 ret = binding->task_queue_head;
123 binding->task_queue_head = ret->next;
124 if(!binding->task_queue_head)
125 binding->task_queue_tail = NULL;
128 LeaveCriticalSection(&binding->section);
133 static void fill_stream_buffer(ProtocolStream *This)
137 if(sizeof(This->buf) == This->buf_size)
140 This->hres = IInternetProtocol_Read(This->protocol, This->buf+This->buf_size,
141 sizeof(This->buf)-This->buf_size, &read);
142 This->buf_size += read;
144 This->init_buf = TRUE;
147 static LRESULT WINAPI notif_wnd_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
149 if(msg == WM_MK_CONTINUE) {
150 Binding *binding = (Binding*)lParam;
153 while((task = pop_task(binding))) {
154 binding->continue_call++;
155 task->proc(binding, task);
156 binding->continue_call--;
159 IBinding_Release(BINDING(binding));
163 return DefWindowProcW(hwnd, msg, wParam, lParam);
166 static HWND get_notif_hwnd(void)
168 static ATOM wnd_class = 0;
171 static const WCHAR wszURLMonikerNotificationWindow[] =
172 {'U','R','L',' ','M','o','n','i','k','e','r',' ',
173 'N','o','t','i','f','i','c','a','t','i','o','n',' ','W','i','n','d','o','w',0};
176 static WNDCLASSEXW wndclass = {
178 notif_wnd_proc, 0, 0,
179 NULL, NULL, NULL, NULL, NULL,
180 wszURLMonikerNotificationWindow,
184 wndclass.hInstance = URLMON_hInstance;
186 wnd_class = RegisterClassExW(&wndclass);
187 if (!wnd_class && GetLastError() == ERROR_CLASS_ALREADY_EXISTS)
191 hwnd = CreateWindowExW(0, wszURLMonikerNotificationWindow,
192 wszURLMonikerNotificationWindow, 0, 0, 0, 0, 0, HWND_MESSAGE,
193 NULL, URLMON_hInstance, NULL);
195 TRACE("hwnd = %p\n", hwnd);
200 static void dump_BINDINFO(BINDINFO *bi)
202 static const char * const BINDINFOF_str[] = {
204 "BINDINFOF_URLENCODESTGMEDDATA",
205 "BINDINFOF_URLENCODEDEXTRAINFO"
208 static const char * const BINDVERB_str[] = {
222 " %d, %08x, %d, %d\n"
228 bi->cbSize, debugstr_w(bi->szExtraInfo),
229 bi->stgmedData.tymed, bi->stgmedData.u.hGlobal, bi->stgmedData.pUnkForRelease,
230 bi->grfBindInfoF > BINDINFOF_URLENCODEDEXTRAINFO
231 ? "unknown" : BINDINFOF_str[bi->grfBindInfoF],
232 bi->dwBindVerb > BINDVERB_CUSTOM
233 ? "unknown" : BINDVERB_str[bi->dwBindVerb],
234 debugstr_w(bi->szCustomVerb),
235 bi->cbstgmedData, bi->dwOptions, bi->dwOptionsFlags, bi->dwCodePage,
236 bi->securityAttributes.nLength,
237 bi->securityAttributes.lpSecurityDescriptor,
238 bi->securityAttributes.bInheritHandle,
239 debugstr_guid(&bi->iid),
240 bi->pUnk, bi->dwReserved
244 static void set_binding_mime(Binding *binding, LPCWSTR mime)
246 EnterCriticalSection(&binding->section);
248 if(binding->report_mime) {
249 heap_free(binding->mime);
250 binding->mime = heap_strdupW(mime);
253 LeaveCriticalSection(&binding->section);
256 static void handle_mime_available(Binding *binding, BOOL verify)
260 EnterCriticalSection(&binding->section);
261 report_mime = binding->report_mime;
262 binding->report_mime = FALSE;
263 LeaveCriticalSection(&binding->section);
271 fill_stream_buffer(binding->stream);
272 FindMimeFromData(NULL, binding->url, binding->stream->buf,
273 min(binding->stream->buf_size, 255), binding->mime, 0, &mime, 0);
275 heap_free(binding->mime);
276 binding->mime = heap_strdupW(mime);
280 IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_MIMETYPEAVAILABLE, binding->mime);
282 binding->clipboard_format = RegisterClipboardFormatW(binding->mime);
286 task_header_t header;
288 } mime_available_task_t;
290 static void mime_available_proc(Binding *binding, task_header_t *t)
292 mime_available_task_t *task = (mime_available_task_t*)t;
294 handle_mime_available(binding, task->verify);
299 static void mime_available(Binding *This, LPCWSTR mime, BOOL verify)
302 set_binding_mime(This, mime);
304 if(GetCurrentThreadId() == This->apartment_thread) {
305 handle_mime_available(This, verify);
307 mime_available_task_t *task = heap_alloc(sizeof(task_header_t));
308 task->verify = verify;
309 push_task(This, &task->header, mime_available_proc);
313 #define STREAM_THIS(iface) DEFINE_THIS(ProtocolStream, Stream, iface)
315 static HRESULT WINAPI ProtocolStream_QueryInterface(IStream *iface,
316 REFIID riid, void **ppv)
318 ProtocolStream *This = STREAM_THIS(iface);
322 if(IsEqualGUID(&IID_IUnknown, riid)) {
323 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
325 }else if(IsEqualGUID(&IID_ISequentialStream, riid)) {
326 TRACE("(%p)->(IID_ISequentialStream %p)\n", This, ppv);
328 }else if(IsEqualGUID(&IID_IStream, riid)) {
329 TRACE("(%p)->(IID_IStream %p)\n", This, ppv);
334 IStream_AddRef(STREAM(This));
338 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
339 return E_NOINTERFACE;
342 static ULONG WINAPI ProtocolStream_AddRef(IStream *iface)
344 ProtocolStream *This = STREAM_THIS(iface);
345 LONG ref = InterlockedIncrement(&This->ref);
347 TRACE("(%p) ref=%d\n", This, ref);
352 static ULONG WINAPI ProtocolStream_Release(IStream *iface)
354 ProtocolStream *This = STREAM_THIS(iface);
355 LONG ref = InterlockedDecrement(&This->ref);
357 TRACE("(%p) ref=%d\n", This, ref);
360 IInternetProtocol_Release(This->protocol);
363 URLMON_UnlockModule();
369 static HRESULT WINAPI ProtocolStream_Read(IStream *iface, void *pv,
370 ULONG cb, ULONG *pcbRead)
372 ProtocolStream *This = STREAM_THIS(iface);
373 DWORD read = 0, pread = 0;
375 TRACE("(%p)->(%p %d %p)\n", This, pv, cb, pcbRead);
380 if(read > This->buf_size)
381 read = This->buf_size;
383 memcpy(pv, This->buf, read);
385 if(read < This->buf_size)
386 memmove(This->buf, This->buf+read, This->buf_size-read);
387 This->buf_size -= read;
396 This->hres = IInternetProtocol_Read(This->protocol, (PBYTE)pv+read, cb-read, &pread);
398 *pcbRead = read + pread;
400 if(This->hres == E_PENDING)
402 else if(FAILED(This->hres))
403 FIXME("Read failed: %08x\n", This->hres);
405 return read || pread ? S_OK : S_FALSE;
408 static HRESULT WINAPI ProtocolStream_Write(IStream *iface, const void *pv,
409 ULONG cb, ULONG *pcbWritten)
411 ProtocolStream *This = STREAM_THIS(iface);
413 TRACE("(%p)->(%p %d %p)\n", This, pv, cb, pcbWritten);
415 return STG_E_ACCESSDENIED;
418 static HRESULT WINAPI ProtocolStream_Seek(IStream *iface, LARGE_INTEGER dlibMove,
419 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
421 ProtocolStream *This = STREAM_THIS(iface);
422 FIXME("(%p)->(%d %08x %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
426 static HRESULT WINAPI ProtocolStream_SetSize(IStream *iface, ULARGE_INTEGER libNewSize)
428 ProtocolStream *This = STREAM_THIS(iface);
429 FIXME("(%p)->(%d)\n", This, libNewSize.u.LowPart);
433 static HRESULT WINAPI ProtocolStream_CopyTo(IStream *iface, IStream *pstm,
434 ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten)
436 ProtocolStream *This = STREAM_THIS(iface);
437 FIXME("(%p)->(%p %d %p %p)\n", This, pstm, cb.u.LowPart, pcbRead, pcbWritten);
441 static HRESULT WINAPI ProtocolStream_Commit(IStream *iface, DWORD grfCommitFlags)
443 ProtocolStream *This = STREAM_THIS(iface);
445 TRACE("(%p)->(%08x)\n", This, grfCommitFlags);
450 static HRESULT WINAPI ProtocolStream_Revert(IStream *iface)
452 ProtocolStream *This = STREAM_THIS(iface);
454 TRACE("(%p)\n", This);
459 static HRESULT WINAPI ProtocolStream_LockRegion(IStream *iface, ULARGE_INTEGER libOffset,
460 ULARGE_INTEGER cb, DWORD dwLockType)
462 ProtocolStream *This = STREAM_THIS(iface);
463 FIXME("(%p)->(%d %d %d)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
467 static HRESULT WINAPI ProtocolStream_UnlockRegion(IStream *iface,
468 ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
470 ProtocolStream *This = STREAM_THIS(iface);
471 FIXME("(%p)->(%d %d %d)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
475 static HRESULT WINAPI ProtocolStream_Stat(IStream *iface, STATSTG *pstatstg,
478 ProtocolStream *This = STREAM_THIS(iface);
479 FIXME("(%p)->(%p %08x)\n", This, pstatstg, dwStatFlag);
483 static HRESULT WINAPI ProtocolStream_Clone(IStream *iface, IStream **ppstm)
485 ProtocolStream *This = STREAM_THIS(iface);
486 FIXME("(%p)->(%p)\n", This, ppstm);
492 static const IStreamVtbl ProtocolStreamVtbl = {
493 ProtocolStream_QueryInterface,
494 ProtocolStream_AddRef,
495 ProtocolStream_Release,
497 ProtocolStream_Write,
499 ProtocolStream_SetSize,
500 ProtocolStream_CopyTo,
501 ProtocolStream_Commit,
502 ProtocolStream_Revert,
503 ProtocolStream_LockRegion,
504 ProtocolStream_UnlockRegion,
509 #define BINDING_THIS(iface) DEFINE_THIS(Binding, Binding, iface)
511 static ProtocolStream *create_stream(IInternetProtocol *protocol)
513 ProtocolStream *ret = heap_alloc(sizeof(ProtocolStream));
515 ret->lpStreamVtbl = &ProtocolStreamVtbl;
518 ret->init_buf = FALSE;
521 IInternetProtocol_AddRef(protocol);
522 ret->protocol = protocol;
529 static HRESULT WINAPI Binding_QueryInterface(IBinding *iface, REFIID riid, void **ppv)
531 Binding *This = BINDING_THIS(iface);
535 if(IsEqualGUID(&IID_IUnknown, riid)) {
536 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
537 *ppv = BINDING(This);
538 }else if(IsEqualGUID(&IID_IBinding, riid)) {
539 TRACE("(%p)->(IID_IBinding %p)\n", This, ppv);
540 *ppv = BINDING(This);
541 }else if(IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
542 TRACE("(%p)->(IID_IInternetProtocolSink %p)\n", This, ppv);
543 *ppv = PROTSINK(This);
544 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
545 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
546 *ppv = BINDINF(This);
547 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
548 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
549 *ppv = SERVPROV(This);
553 IBinding_AddRef(BINDING(This));
557 WARN("Unsupported interface %s\n", debugstr_guid(riid));
558 return E_NOINTERFACE;
561 static ULONG WINAPI Binding_AddRef(IBinding *iface)
563 Binding *This = BINDING_THIS(iface);
564 LONG ref = InterlockedIncrement(&This->ref);
566 TRACE("(%p) ref=%d\n", This, ref);
571 static ULONG WINAPI Binding_Release(IBinding *iface)
573 Binding *This = BINDING_THIS(iface);
574 LONG ref = InterlockedDecrement(&This->ref);
576 TRACE("(%p) ref=%d\n", This, ref);
579 if (This->notif_hwnd)
580 DestroyWindow( This->notif_hwnd );
582 IBindStatusCallback_Release(This->callback);
584 IInternetProtocol_Release(This->protocol);
585 if(This->service_provider)
586 IServiceProvider_Release(This->service_provider);
588 IStream_Release(STREAM(This->stream));
590 ReleaseBindInfo(&This->bindinfo);
591 This->section.DebugInfo->Spare[0] = 0;
592 DeleteCriticalSection(&This->section);
593 heap_free(This->mime);
594 heap_free(This->url);
598 URLMON_UnlockModule();
604 static HRESULT WINAPI Binding_Abort(IBinding *iface)
606 Binding *This = BINDING_THIS(iface);
607 FIXME("(%p)\n", This);
611 static HRESULT WINAPI Binding_Suspend(IBinding *iface)
613 Binding *This = BINDING_THIS(iface);
614 FIXME("(%p)\n", This);
618 static HRESULT WINAPI Binding_Resume(IBinding *iface)
620 Binding *This = BINDING_THIS(iface);
621 FIXME("(%p)\n", This);
625 static HRESULT WINAPI Binding_SetPriority(IBinding *iface, LONG nPriority)
627 Binding *This = BINDING_THIS(iface);
628 FIXME("(%p)->(%d)\n", This, nPriority);
632 static HRESULT WINAPI Binding_GetPriority(IBinding *iface, LONG *pnPriority)
634 Binding *This = BINDING_THIS(iface);
635 FIXME("(%p)->(%p)\n", This, pnPriority);
639 static HRESULT WINAPI Binding_GetBindResult(IBinding *iface, CLSID *pclsidProtocol,
640 DWORD *pdwResult, LPOLESTR *pszResult, DWORD *pdwReserved)
642 Binding *This = BINDING_THIS(iface);
643 FIXME("(%p)->(%p %p %p %p)\n", This, pclsidProtocol, pdwResult, pszResult, pdwReserved);
649 static const IBindingVtbl BindingVtbl = {
650 Binding_QueryInterface,
658 Binding_GetBindResult
661 #define PROTSINK_THIS(iface) DEFINE_THIS(Binding, InternetProtocolSink, iface)
663 static HRESULT WINAPI InternetProtocolSink_QueryInterface(IInternetProtocolSink *iface,
664 REFIID riid, void **ppv)
666 Binding *This = PROTSINK_THIS(iface);
667 return IBinding_QueryInterface(BINDING(This), riid, ppv);
670 static ULONG WINAPI InternetProtocolSink_AddRef(IInternetProtocolSink *iface)
672 Binding *This = PROTSINK_THIS(iface);
673 return IBinding_AddRef(BINDING(This));
676 static ULONG WINAPI InternetProtocolSink_Release(IInternetProtocolSink *iface)
678 Binding *This = PROTSINK_THIS(iface);
679 return IBinding_Release(BINDING(This));
683 task_header_t header;
687 static void switch_proc(Binding *binding, task_header_t *t)
689 switch_task_t *task = (switch_task_t*)t;
691 IInternetProtocol_Continue(binding->protocol, &task->data);
696 static HRESULT WINAPI InternetProtocolSink_Switch(IInternetProtocolSink *iface,
697 PROTOCOLDATA *pProtocolData)
699 Binding *This = PROTSINK_THIS(iface);
702 TRACE("(%p)->(%p)\n", This, pProtocolData);
704 task = heap_alloc(sizeof(switch_task_t));
705 memcpy(&task->data, pProtocolData, sizeof(PROTOCOLDATA));
707 push_task(This, &task->header, switch_proc);
709 IBinding_AddRef(BINDING(This));
710 PostMessageW(This->notif_hwnd, WM_MK_CONTINUE, 0, (LPARAM)This);
716 task_header_t header;
723 } on_progress_task_t;
725 static void on_progress_proc(Binding *binding, task_header_t *t)
727 on_progress_task_t *task = (on_progress_task_t*)t;
729 IBindStatusCallback_OnProgress(binding->callback, task->progress,
730 task->progress_max, task->status_code, task->status_text);
732 heap_free(task->status_text);
736 static void on_progress(Binding *This, ULONG progress, ULONG progress_max,
737 ULONG status_code, LPCWSTR status_text)
739 on_progress_task_t *task;
741 if(GetCurrentThreadId() == This->apartment_thread && !This->continue_call) {
742 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
743 status_code, status_text);
747 task = heap_alloc(sizeof(on_progress_task_t));
749 task->progress = progress;
750 task->progress_max = progress_max;
751 task->status_code = status_code;
754 DWORD size = (strlenW(status_text)+1)*sizeof(WCHAR);
756 task->status_text = heap_alloc(size);
757 memcpy(task->status_text, status_text, size);
759 task->status_text = NULL;
762 push_task(This, &task->header, on_progress_proc);
764 if(GetCurrentThreadId() != This->apartment_thread) {
765 IBinding_AddRef(BINDING(This));
766 PostMessageW(This->notif_hwnd, WM_MK_CONTINUE, 0, (LPARAM)This);
770 static HRESULT WINAPI InternetProtocolSink_ReportProgress(IInternetProtocolSink *iface,
771 ULONG ulStatusCode, LPCWSTR szStatusText)
773 Binding *This = PROTSINK_THIS(iface);
775 TRACE("(%p)->(%u %s)\n", This, ulStatusCode, debugstr_w(szStatusText));
777 switch(ulStatusCode) {
778 case BINDSTATUS_FINDINGRESOURCE:
779 on_progress(This, 0, 0, BINDSTATUS_FINDINGRESOURCE, szStatusText);
781 case BINDSTATUS_CONNECTING:
782 on_progress(This, 0, 0, BINDSTATUS_CONNECTING, szStatusText);
784 case BINDSTATUS_BEGINDOWNLOADDATA:
785 fill_stream_buffer(This->stream);
787 case BINDSTATUS_MIMETYPEAVAILABLE:
788 set_binding_mime(This, szStatusText);
790 case BINDSTATUS_SENDINGREQUEST:
791 on_progress(This, 0, 0, BINDSTATUS_SENDINGREQUEST, szStatusText);
793 case BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE:
794 mime_available(This, szStatusText, FALSE);
796 case BINDSTATUS_CACHEFILENAMEAVAILABLE:
798 case BINDSTATUS_DIRECTBIND:
799 This->report_mime = FALSE;
802 FIXME("Unhandled status code %d\n", ulStatusCode);
809 static void report_data(Binding *This, DWORD bscf, ULONG progress, ULONG progress_max)
811 FORMATETC formatetc = {0, NULL, 1, -1, TYMED_ISTREAM};
812 BOOL sent_begindownloaddata = FALSE;
814 TRACE("(%p)->(%d %u %u)\n", This, bscf, progress, progress_max);
816 if(This->download_state == END_DOWNLOAD)
819 if(GetCurrentThreadId() != This->apartment_thread)
820 FIXME("called from worked hread\n");
822 if(This->report_mime)
823 mime_available(This, NULL, TRUE);
825 if(This->download_state == BEFORE_DOWNLOAD) {
826 fill_stream_buffer(This->stream);
828 This->download_state = DOWNLOADING;
829 sent_begindownloaddata = TRUE;
830 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
831 BINDSTATUS_BEGINDOWNLOADDATA, This->url);
834 if(This->stream->hres == S_FALSE || (bscf & BSCF_LASTDATANOTIFICATION)) {
835 This->download_state = END_DOWNLOAD;
836 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
837 BINDSTATUS_ENDDOWNLOADDATA, This->url);
838 }else if(!sent_begindownloaddata) {
839 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
840 BINDSTATUS_DOWNLOADINGDATA, This->url);
843 if(!(This->state & BINDING_LOCKED)) {
844 HRESULT hres = IInternetProtocol_LockRequest(This->protocol, 0);
846 This->state |= BINDING_LOCKED;
849 formatetc.cfFormat = This->clipboard_format;
850 IBindStatusCallback_OnDataAvailable(This->callback, bscf, progress,
851 &formatetc, &This->stgmed);
853 if(This->download_state == END_DOWNLOAD) {
854 IBindStatusCallback_OnStopBinding(This->callback, S_OK, NULL);
859 task_header_t header;
863 } report_data_task_t;
865 static void report_data_proc(Binding *binding, task_header_t *t)
867 report_data_task_t *task = (report_data_task_t*)t;
869 report_data(binding, task->bscf, task->progress, task->progress_max);
874 static HRESULT WINAPI InternetProtocolSink_ReportData(IInternetProtocolSink *iface,
875 DWORD grfBSCF, ULONG ulProgress, ULONG ulProgressMax)
877 Binding *This = PROTSINK_THIS(iface);
879 TRACE("(%p)->(%d %u %u)\n", This, grfBSCF, ulProgress, ulProgressMax);
881 if(GetCurrentThreadId() != This->apartment_thread)
882 FIXME("called from worked hread\n");
884 if(This->continue_call) {
885 report_data_task_t *task = heap_alloc(sizeof(report_data_task_t));
886 task->bscf = grfBSCF;
887 task->progress = ulProgress;
888 task->progress_max = ulProgressMax;
890 push_task(This, &task->header, report_data_proc);
892 report_data(This, grfBSCF, ulProgress, ulProgressMax);
898 static void report_result_proc(Binding *binding, task_header_t *t)
900 IInternetProtocol_Terminate(binding->protocol, 0);
902 if(binding->state & BINDING_LOCKED) {
903 IInternetProtocol_UnlockRequest(binding->protocol);
904 binding->state &= ~BINDING_LOCKED;
910 static HRESULT WINAPI InternetProtocolSink_ReportResult(IInternetProtocolSink *iface,
911 HRESULT hrResult, DWORD dwError, LPCWSTR szResult)
913 Binding *This = PROTSINK_THIS(iface);
915 TRACE("(%p)->(%08x %d %s)\n", This, hrResult, dwError, debugstr_w(szResult));
917 if(GetCurrentThreadId() == This->apartment_thread && !This->continue_call) {
918 IInternetProtocol_Terminate(This->protocol, 0);
920 task_header_t *task = heap_alloc(sizeof(task_header_t));
921 push_task(This, task, report_result_proc);
929 static const IInternetProtocolSinkVtbl InternetProtocolSinkVtbl = {
930 InternetProtocolSink_QueryInterface,
931 InternetProtocolSink_AddRef,
932 InternetProtocolSink_Release,
933 InternetProtocolSink_Switch,
934 InternetProtocolSink_ReportProgress,
935 InternetProtocolSink_ReportData,
936 InternetProtocolSink_ReportResult
939 #define BINDINF_THIS(iface) DEFINE_THIS(Binding, InternetBindInfo, iface)
941 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
942 REFIID riid, void **ppv)
944 Binding *This = BINDINF_THIS(iface);
945 return IBinding_QueryInterface(BINDING(This), riid, ppv);
948 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
950 Binding *This = BINDINF_THIS(iface);
951 return IBinding_AddRef(BINDING(This));
954 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
956 Binding *This = BINDINF_THIS(iface);
957 return IBinding_Release(BINDING(This));
960 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
961 DWORD *grfBINDF, BINDINFO *pbindinfo)
963 Binding *This = BINDINF_THIS(iface);
965 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
967 *grfBINDF = This->bindf;
969 memcpy(pbindinfo, &This->bindinfo, sizeof(BINDINFO));
971 if(pbindinfo->szExtraInfo || pbindinfo->szCustomVerb)
972 FIXME("copy strings\n");
974 if(pbindinfo->stgmedData.pUnkForRelease)
975 IUnknown_AddRef(pbindinfo->stgmedData.pUnkForRelease);
978 IUnknown_AddRef(pbindinfo->pUnk);
983 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
984 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
986 Binding *This = BINDINF_THIS(iface);
988 TRACE("(%p)->(%d %p %d %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
990 switch(ulStringType) {
991 case BINDSTRING_ACCEPT_MIMES: {
992 static const WCHAR wszMimes[] = {'*','/','*',0};
994 if(!ppwzStr || !pcElFetched)
997 ppwzStr[0] = CoTaskMemAlloc(sizeof(wszMimes));
998 memcpy(ppwzStr[0], wszMimes, sizeof(wszMimes));
1002 case BINDSTRING_USER_AGENT: {
1003 IInternetBindInfo *bindinfo = NULL;
1006 hres = IBindStatusCallback_QueryInterface(This->callback, &IID_IInternetBindInfo,
1011 hres = IInternetBindInfo_GetBindString(bindinfo, ulStringType, ppwzStr,
1013 IInternetBindInfo_Release(bindinfo);
1019 FIXME("not supported string type %d\n", ulStringType);
1025 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
1026 InternetBindInfo_QueryInterface,
1027 InternetBindInfo_AddRef,
1028 InternetBindInfo_Release,
1029 InternetBindInfo_GetBindInfo,
1030 InternetBindInfo_GetBindString
1033 #define SERVPROV_THIS(iface) DEFINE_THIS(Binding, ServiceProvider, iface)
1035 static HRESULT WINAPI ServiceProvider_QueryInterface(IServiceProvider *iface,
1036 REFIID riid, void **ppv)
1038 Binding *This = SERVPROV_THIS(iface);
1039 return IBinding_QueryInterface(BINDING(This), riid, ppv);
1042 static ULONG WINAPI ServiceProvider_AddRef(IServiceProvider *iface)
1044 Binding *This = SERVPROV_THIS(iface);
1045 return IBinding_AddRef(BINDING(This));
1048 static ULONG WINAPI ServiceProvider_Release(IServiceProvider *iface)
1050 Binding *This = SERVPROV_THIS(iface);
1051 return IBinding_Release(BINDING(This));
1054 static HRESULT WINAPI ServiceProvider_QueryService(IServiceProvider *iface,
1055 REFGUID guidService, REFIID riid, void **ppv)
1057 Binding *This = SERVPROV_THIS(iface);
1060 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
1062 if(This->service_provider) {
1063 hres = IServiceProvider_QueryService(This->service_provider, guidService,
1069 WARN("unknown service %s\n", debugstr_guid(guidService));
1073 #undef SERVPROV_THIS
1075 static const IServiceProviderVtbl ServiceProviderVtbl = {
1076 ServiceProvider_QueryInterface,
1077 ServiceProvider_AddRef,
1078 ServiceProvider_Release,
1079 ServiceProvider_QueryService
1082 static HRESULT get_callback(IBindCtx *pbc, IBindStatusCallback **callback)
1087 static WCHAR wszBSCBHolder[] = { '_','B','S','C','B','_','H','o','l','d','e','r','_',0 };
1089 hres = IBindCtx_GetObjectParam(pbc, wszBSCBHolder, &unk);
1090 if(SUCCEEDED(hres)) {
1091 hres = IUnknown_QueryInterface(unk, &IID_IBindStatusCallback, (void**)callback);
1092 IUnknown_Release(unk);
1095 return SUCCEEDED(hres) ? S_OK : MK_E_SYNTAX;
1098 static HRESULT get_protocol(Binding *This, LPCWSTR url)
1100 IClassFactory *cf = NULL;
1103 hres = IBindStatusCallback_QueryInterface(This->callback, &IID_IInternetProtocol,
1104 (void**)&This->protocol);
1108 if(This->service_provider) {
1109 hres = IServiceProvider_QueryService(This->service_provider, &IID_IInternetProtocol,
1110 &IID_IInternetProtocol, (void**)&This->protocol);
1115 hres = get_protocol_handler(url, NULL, &cf);
1119 hres = IClassFactory_CreateInstance(cf, NULL, &IID_IInternetProtocol, (void**)&This->protocol);
1120 IClassFactory_Release(cf);
1125 static BOOL is_urlmon_protocol(LPCWSTR url)
1127 static const WCHAR wszCdl[] = {'c','d','l'};
1128 static const WCHAR wszFile[] = {'f','i','l','e'};
1129 static const WCHAR wszFtp[] = {'f','t','p'};
1130 static const WCHAR wszGopher[] = {'g','o','p','h','e','r'};
1131 static const WCHAR wszHttp[] = {'h','t','t','p'};
1132 static const WCHAR wszHttps[] = {'h','t','t','p','s'};
1133 static const WCHAR wszMk[] = {'m','k'};
1135 static const struct {
1138 } protocol_list[] = {
1139 {wszCdl, sizeof(wszCdl) /sizeof(WCHAR)},
1140 {wszFile, sizeof(wszFile) /sizeof(WCHAR)},
1141 {wszFtp, sizeof(wszFtp) /sizeof(WCHAR)},
1142 {wszGopher, sizeof(wszGopher)/sizeof(WCHAR)},
1143 {wszHttp, sizeof(wszHttp) /sizeof(WCHAR)},
1144 {wszHttps, sizeof(wszHttps) /sizeof(WCHAR)},
1145 {wszMk, sizeof(wszMk) /sizeof(WCHAR)}
1148 int i, len = strlenW(url);
1150 for(i=0; i < sizeof(protocol_list)/sizeof(protocol_list[0]); i++) {
1151 if(len >= protocol_list[i].len
1152 && !memcmp(url, protocol_list[i].scheme, protocol_list[i].len*sizeof(WCHAR)))
1159 static HRESULT Binding_Create(LPCWSTR url, IBindCtx *pbc, REFIID riid, Binding **binding)
1165 if(!IsEqualGUID(&IID_IStream, riid)) {
1166 FIXME("Unsupported riid %s\n", debugstr_guid(riid));
1170 URLMON_LockModule();
1172 ret = heap_alloc(sizeof(Binding));
1174 ret->lpBindingVtbl = &BindingVtbl;
1175 ret->lpInternetProtocolSinkVtbl = &InternetProtocolSinkVtbl;
1176 ret->lpInternetBindInfoVtbl = &InternetBindInfoVtbl;
1177 ret->lpServiceProviderVtbl = &ServiceProviderVtbl;
1181 ret->callback = NULL;
1182 ret->protocol = NULL;
1183 ret->service_provider = NULL;
1186 ret->clipboard_format = 0;
1188 ret->apartment_thread = GetCurrentThreadId();
1189 ret->notif_hwnd = get_notif_hwnd();
1190 ret->report_mime = TRUE;
1191 ret->continue_call = 0;
1193 ret->download_state = BEFORE_DOWNLOAD;
1194 ret->task_queue_head = ret->task_queue_tail = NULL;
1196 memset(&ret->bindinfo, 0, sizeof(BINDINFO));
1197 ret->bindinfo.cbSize = sizeof(BINDINFO);
1200 InitializeCriticalSection(&ret->section);
1201 ret->section.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": Binding.section");
1203 hres = get_callback(pbc, &ret->callback);
1205 WARN("Could not get IBindStatusCallback\n");
1206 IBinding_Release(BINDING(ret));
1210 IBindStatusCallback_QueryInterface(ret->callback, &IID_IServiceProvider,
1211 (void**)&ret->service_provider);
1213 hres = get_protocol(ret, url);
1215 WARN("Could not get protocol handler\n");
1216 IBinding_Release(BINDING(ret));
1220 hres = IBindStatusCallback_GetBindInfo(ret->callback, &ret->bindf, &ret->bindinfo);
1222 WARN("GetBindInfo failed: %08x\n", hres);
1223 IBinding_Release(BINDING(ret));
1227 dump_BINDINFO(&ret->bindinfo);
1229 ret->bindf |= BINDF_FROMURLMON;
1231 if(!is_urlmon_protocol(url))
1232 ret->bindf |= BINDF_NEEDFILE;
1234 len = strlenW(url)+1;
1235 ret->url = heap_alloc(len*sizeof(WCHAR));
1236 memcpy(ret->url, url, len*sizeof(WCHAR));
1238 ret->stream = create_stream(ret->protocol);
1239 ret->stgmed.tymed = TYMED_ISTREAM;
1240 ret->stgmed.u.pstm = STREAM(ret->stream);
1241 ret->stgmed.pUnkForRelease = (IUnknown*)BINDING(ret); /* NOTE: Windows uses other IUnknown */
1247 HRESULT start_binding(LPCWSTR url, IBindCtx *pbc, REFIID riid, void **ppv)
1249 Binding *binding = NULL;
1255 hres = Binding_Create(url, pbc, riid, &binding);
1259 hres = IBindStatusCallback_OnStartBinding(binding->callback, 0, BINDING(binding));
1261 WARN("OnStartBinding failed: %08x\n", hres);
1262 IBindStatusCallback_OnStopBinding(binding->callback, 0x800c0008, NULL);
1263 IBinding_Release(BINDING(binding));
1267 hres = IInternetProtocol_Start(binding->protocol, url, PROTSINK(binding),
1268 BINDINF(binding), 0, 0);
1271 WARN("Start failed: %08x\n", hres);
1273 IInternetProtocol_Terminate(binding->protocol, 0);
1274 IBindStatusCallback_OnStopBinding(binding->callback, S_OK, NULL);
1275 IBinding_Release(BINDING(binding));
1280 while(!(binding->bindf & BINDF_ASYNCHRONOUS) &&
1281 binding->download_state != END_DOWNLOAD) {
1282 MsgWaitForMultipleObjects(0, NULL, FALSE, 5000, QS_POSTMESSAGE);
1283 while (PeekMessageW(&msg, binding->notif_hwnd, WM_USER, WM_USER+117, PM_REMOVE|PM_NOYIELD)) {
1284 TranslateMessage(&msg);
1285 DispatchMessageW(&msg);
1289 if(binding->stream->init_buf) {
1290 if(binding->state & BINDING_LOCKED)
1291 IInternetProtocol_UnlockRequest(binding->protocol);
1293 IStream_AddRef(STREAM(binding->stream));
1294 *ppv = binding->stream;
1298 hres = MK_S_ASYNCHRONOUS;
1301 IBinding_Release(BINDING(binding));