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
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;
39 struct _task_header_t;
41 typedef void (*task_proc_t)(Binding*, struct _task_header_t*);
43 typedef struct _task_header_t {
45 struct _task_header_t *next;
49 const IStreamVtbl *lpStreamVtbl;
53 IInternetProtocol *protocol;
68 const IBindingVtbl *lpBindingVtbl;
69 const IInternetProtocolSinkVtbl *lpInternetProtocolSinkVtbl;
70 const IInternetBindInfoVtbl *lpInternetBindInfoVtbl;
71 const IServiceProviderVtbl *lpServiceProviderVtbl;
75 IBindStatusCallback *callback;
76 IInternetProtocol *protocol;
77 IServiceProvider *service_provider;
78 ProtocolStream *stream;
87 download_state_t download_state;
89 DWORD apartment_thread;
94 task_header_t *task_queue_head, *task_queue_tail;
95 CRITICAL_SECTION section;
98 #define BINDING(x) ((IBinding*) &(x)->lpBindingVtbl)
99 #define PROTSINK(x) ((IInternetProtocolSink*) &(x)->lpInternetProtocolSinkVtbl)
100 #define BINDINF(x) ((IInternetBindInfo*) &(x)->lpInternetBindInfoVtbl)
101 #define SERVPROV(x) ((IServiceProvider*) &(x)->lpServiceProviderVtbl)
103 #define STREAM(x) ((IStream*) &(x)->lpStreamVtbl)
105 #define WM_MK_CONTINUE (WM_USER+101)
107 static void push_task(Binding *binding, task_header_t *task, task_proc_t proc)
112 EnterCriticalSection(&binding->section);
114 if(binding->task_queue_tail)
115 binding->task_queue_tail->next = task;
117 binding->task_queue_tail = binding->task_queue_head = task;
119 LeaveCriticalSection(&binding->section);
122 static task_header_t *pop_task(Binding *binding)
126 EnterCriticalSection(&binding->section);
128 ret = binding->task_queue_head;
130 binding->task_queue_head = ret->next;
131 if(!binding->task_queue_head)
132 binding->task_queue_tail = NULL;
135 LeaveCriticalSection(&binding->section);
140 static void fill_stream_buffer(ProtocolStream *This)
144 if(sizeof(This->buf) == This->buf_size)
147 This->hres = IInternetProtocol_Read(This->protocol, This->buf+This->buf_size,
148 sizeof(This->buf)-This->buf_size, &read);
149 if(SUCCEEDED(This->hres)) {
150 This->buf_size += read;
151 This->init_buf = TRUE;
155 static LRESULT WINAPI notif_wnd_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
157 if(msg == WM_MK_CONTINUE) {
158 Binding *binding = (Binding*)lParam;
161 while((task = pop_task(binding))) {
162 binding->continue_call++;
163 task->proc(binding, task);
164 binding->continue_call--;
167 IBinding_Release(BINDING(binding));
171 return DefWindowProcW(hwnd, msg, wParam, lParam);
174 static HWND get_notif_hwnd(void)
176 static ATOM wnd_class = 0;
179 static const WCHAR wszURLMonikerNotificationWindow[] =
180 {'U','R','L',' ','M','o','n','i','k','e','r',' ',
181 'N','o','t','i','f','i','c','a','t','i','o','n',' ','W','i','n','d','o','w',0};
184 static WNDCLASSEXW wndclass = {
186 notif_wnd_proc, 0, 0,
187 NULL, NULL, NULL, NULL, NULL,
188 wszURLMonikerNotificationWindow,
192 wndclass.hInstance = URLMON_hInstance;
194 wnd_class = RegisterClassExW(&wndclass);
195 if (!wnd_class && GetLastError() == ERROR_CLASS_ALREADY_EXISTS)
199 hwnd = CreateWindowExW(0, wszURLMonikerNotificationWindow,
200 wszURLMonikerNotificationWindow, 0, 0, 0, 0, 0, HWND_MESSAGE,
201 NULL, URLMON_hInstance, NULL);
203 TRACE("hwnd = %p\n", hwnd);
208 static void dump_BINDINFO(BINDINFO *bi)
210 static const char * const BINDINFOF_str[] = {
212 "BINDINFOF_URLENCODESTGMEDDATA",
213 "BINDINFOF_URLENCODEDEXTRAINFO"
216 static const char * const BINDVERB_str[] = {
230 " %d, %08x, %d, %d\n"
236 bi->cbSize, debugstr_w(bi->szExtraInfo),
237 bi->stgmedData.tymed, bi->stgmedData.u.hGlobal, bi->stgmedData.pUnkForRelease,
238 bi->grfBindInfoF > BINDINFOF_URLENCODEDEXTRAINFO
239 ? "unknown" : BINDINFOF_str[bi->grfBindInfoF],
240 bi->dwBindVerb > BINDVERB_CUSTOM
241 ? "unknown" : BINDVERB_str[bi->dwBindVerb],
242 debugstr_w(bi->szCustomVerb),
243 bi->cbstgmedData, bi->dwOptions, bi->dwOptionsFlags, bi->dwCodePage,
244 bi->securityAttributes.nLength,
245 bi->securityAttributes.lpSecurityDescriptor,
246 bi->securityAttributes.bInheritHandle,
247 debugstr_guid(&bi->iid),
248 bi->pUnk, bi->dwReserved
252 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
253 REFIID riid, void **ppv)
257 if(IsEqualGUID(&IID_IUnknown, riid)) {
258 TRACE("(IID_IUnknown %p)\n", ppv);
260 }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
261 TRACE("(IID_IHttpNegotiate %p)\n", ppv);
263 }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
264 TRACE("(IID_IHttpNegotiate2 %p)\n", ppv);
269 IHttpNegotiate2_AddRef(iface);
273 WARN("Unsupported interface %s\n", debugstr_guid(riid));
274 return E_NOINTERFACE;
277 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
283 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
285 URLMON_UnlockModule();
289 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
290 LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
292 TRACE("(%s %s %d %p)\n", debugstr_w(szURL), debugstr_w(szHeaders), dwReserved,
293 pszAdditionalHeaders);
295 *pszAdditionalHeaders = NULL;
299 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
300 LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders,
301 LPWSTR *pszAdditionalRequestHeaders)
303 TRACE("(%d %s %s %p)\n", dwResponseCode, debugstr_w(szResponseHeaders),
304 debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
306 if(pszAdditionalRequestHeaders)
307 *pszAdditionalRequestHeaders = NULL;
311 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
312 BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
314 TRACE("(%p %p %ld)\n", pbSecurityId, pcbSecurityId, dwReserved);
316 /* That's all we have to do here */
320 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
321 HttpNegotiate_QueryInterface,
322 HttpNegotiate_AddRef,
323 HttpNegotiate_Release,
324 HttpNegotiate_BeginningTransaction,
325 HttpNegotiate_OnResponse,
326 HttpNegotiate_GetRootSecurityId
329 static IHttpNegotiate2 HttpNegotiate = { &HttpNegotiate2Vtbl };
331 #define STREAM_THIS(iface) DEFINE_THIS(ProtocolStream, Stream, iface)
333 static HRESULT WINAPI ProtocolStream_QueryInterface(IStream *iface,
334 REFIID riid, void **ppv)
336 ProtocolStream *This = STREAM_THIS(iface);
340 if(IsEqualGUID(&IID_IUnknown, riid)) {
341 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
343 }else if(IsEqualGUID(&IID_ISequentialStream, riid)) {
344 TRACE("(%p)->(IID_ISequentialStream %p)\n", This, ppv);
346 }else if(IsEqualGUID(&IID_IStream, riid)) {
347 TRACE("(%p)->(IID_IStream %p)\n", This, ppv);
352 IStream_AddRef(STREAM(This));
356 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
357 return E_NOINTERFACE;
360 static ULONG WINAPI ProtocolStream_AddRef(IStream *iface)
362 ProtocolStream *This = STREAM_THIS(iface);
363 LONG ref = InterlockedIncrement(&This->ref);
365 TRACE("(%p) ref=%d\n", This, ref);
370 static ULONG WINAPI ProtocolStream_Release(IStream *iface)
372 ProtocolStream *This = STREAM_THIS(iface);
373 LONG ref = InterlockedDecrement(&This->ref);
375 TRACE("(%p) ref=%d\n", This, ref);
378 IInternetProtocol_Release(This->protocol);
379 HeapFree(GetProcessHeap(), 0, This);
381 URLMON_UnlockModule();
387 static HRESULT WINAPI ProtocolStream_Read(IStream *iface, void *pv,
388 ULONG cb, ULONG *pcbRead)
390 ProtocolStream *This = STREAM_THIS(iface);
391 DWORD read = 0, pread = 0;
393 TRACE("(%p)->(%p %d %p)\n", This, pv, cb, pcbRead);
398 if(read > This->buf_size)
399 read = This->buf_size;
401 memcpy(pv, This->buf, read);
403 if(read < This->buf_size)
404 memmove(This->buf, This->buf+read, This->buf_size-read);
405 This->buf_size -= read;
413 This->hres = IInternetProtocol_Read(This->protocol, (PBYTE)pv+read, cb-read, &pread);
414 *pcbRead = read + pread;
416 if(This->hres == E_PENDING)
418 else if(FAILED(This->hres))
419 FIXME("Read failed: %08x\n", This->hres);
421 return read || pread ? S_OK : S_FALSE;
424 static HRESULT WINAPI ProtocolStream_Write(IStream *iface, const void *pv,
425 ULONG cb, ULONG *pcbWritten)
427 ProtocolStream *This = STREAM_THIS(iface);
429 TRACE("(%p)->(%p %d %p)\n", This, pv, cb, pcbWritten);
431 return STG_E_ACCESSDENIED;
434 static HRESULT WINAPI ProtocolStream_Seek(IStream *iface, LARGE_INTEGER dlibMove,
435 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
437 ProtocolStream *This = STREAM_THIS(iface);
438 FIXME("(%p)->(%d %08x %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
442 static HRESULT WINAPI ProtocolStream_SetSize(IStream *iface, ULARGE_INTEGER libNewSize)
444 ProtocolStream *This = STREAM_THIS(iface);
445 FIXME("(%p)->(%d)\n", This, libNewSize.u.LowPart);
449 static HRESULT WINAPI ProtocolStream_CopyTo(IStream *iface, IStream *pstm,
450 ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten)
452 ProtocolStream *This = STREAM_THIS(iface);
453 FIXME("(%p)->(%p %d %p %p)\n", This, pstm, cb.u.LowPart, pcbRead, pcbWritten);
457 static HRESULT WINAPI ProtocolStream_Commit(IStream *iface, DWORD grfCommitFlags)
459 ProtocolStream *This = STREAM_THIS(iface);
461 TRACE("(%p)->(%08x)\n", This, grfCommitFlags);
466 static HRESULT WINAPI ProtocolStream_Revert(IStream *iface)
468 ProtocolStream *This = STREAM_THIS(iface);
470 TRACE("(%p)\n", This);
475 static HRESULT WINAPI ProtocolStream_LockRegion(IStream *iface, ULARGE_INTEGER libOffset,
476 ULARGE_INTEGER cb, DWORD dwLockType)
478 ProtocolStream *This = STREAM_THIS(iface);
479 FIXME("(%p)->(%d %d %d)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
483 static HRESULT WINAPI ProtocolStream_UnlockRegion(IStream *iface,
484 ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
486 ProtocolStream *This = STREAM_THIS(iface);
487 FIXME("(%p)->(%d %d %d)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
491 static HRESULT WINAPI ProtocolStream_Stat(IStream *iface, STATSTG *pstatstg,
494 ProtocolStream *This = STREAM_THIS(iface);
495 FIXME("(%p)->(%p %08x)\n", This, pstatstg, dwStatFlag);
499 static HRESULT WINAPI ProtocolStream_Clone(IStream *iface, IStream **ppstm)
501 ProtocolStream *This = STREAM_THIS(iface);
502 FIXME("(%p)->(%p)\n", This, ppstm);
508 static const IStreamVtbl ProtocolStreamVtbl = {
509 ProtocolStream_QueryInterface,
510 ProtocolStream_AddRef,
511 ProtocolStream_Release,
513 ProtocolStream_Write,
515 ProtocolStream_SetSize,
516 ProtocolStream_CopyTo,
517 ProtocolStream_Commit,
518 ProtocolStream_Revert,
519 ProtocolStream_LockRegion,
520 ProtocolStream_UnlockRegion,
525 #define BINDING_THIS(iface) DEFINE_THIS(Binding, Binding, iface)
527 static ProtocolStream *create_stream(IInternetProtocol *protocol)
529 ProtocolStream *ret = HeapAlloc(GetProcessHeap(), 0, sizeof(ProtocolStream));
531 ret->lpStreamVtbl = &ProtocolStreamVtbl;
534 ret->init_buf = FALSE;
537 IInternetProtocol_AddRef(protocol);
538 ret->protocol = protocol;
545 static HRESULT WINAPI Binding_QueryInterface(IBinding *iface, REFIID riid, void **ppv)
547 Binding *This = BINDING_THIS(iface);
551 if(IsEqualGUID(&IID_IUnknown, riid)) {
552 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
553 *ppv = BINDING(This);
554 }else if(IsEqualGUID(&IID_IBinding, riid)) {
555 TRACE("(%p)->(IID_IBinding %p)\n", This, ppv);
556 *ppv = BINDING(This);
557 }else if(IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
558 TRACE("(%p)->(IID_IInternetProtocolSink %p)\n", This, ppv);
559 *ppv = PROTSINK(This);
560 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
561 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
562 *ppv = BINDINF(This);
563 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
564 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
565 *ppv = SERVPROV(This);
569 IBinding_AddRef(BINDING(This));
573 WARN("Unsupported interface %s\n", debugstr_guid(riid));
574 return E_NOINTERFACE;
577 static ULONG WINAPI Binding_AddRef(IBinding *iface)
579 Binding *This = BINDING_THIS(iface);
580 LONG ref = InterlockedIncrement(&This->ref);
582 TRACE("(%p) ref=%d\n", This, ref);
587 static ULONG WINAPI Binding_Release(IBinding *iface)
589 Binding *This = BINDING_THIS(iface);
590 LONG ref = InterlockedDecrement(&This->ref);
592 TRACE("(%p) ref=%d\n", This, ref);
595 if (This->notif_hwnd)
596 DestroyWindow( This->notif_hwnd );
598 IBindStatusCallback_Release(This->callback);
600 IInternetProtocol_Release(This->protocol);
601 if(This->service_provider)
602 IServiceProvider_Release(This->service_provider);
604 IStream_Release(STREAM(This->stream));
606 ReleaseBindInfo(&This->bindinfo);
607 DeleteCriticalSection(&This->section);
608 HeapFree(GetProcessHeap(), 0, This->mime);
609 HeapFree(GetProcessHeap(), 0, This->url);
611 HeapFree(GetProcessHeap(), 0, This);
613 URLMON_UnlockModule();
619 static HRESULT WINAPI Binding_Abort(IBinding *iface)
621 Binding *This = BINDING_THIS(iface);
622 FIXME("(%p)\n", This);
626 static HRESULT WINAPI Binding_Suspend(IBinding *iface)
628 Binding *This = BINDING_THIS(iface);
629 FIXME("(%p)\n", This);
633 static HRESULT WINAPI Binding_Resume(IBinding *iface)
635 Binding *This = BINDING_THIS(iface);
636 FIXME("(%p)\n", This);
640 static HRESULT WINAPI Binding_SetPriority(IBinding *iface, LONG nPriority)
642 Binding *This = BINDING_THIS(iface);
643 FIXME("(%p)->(%d)\n", This, nPriority);
647 static HRESULT WINAPI Binding_GetPriority(IBinding *iface, LONG *pnPriority)
649 Binding *This = BINDING_THIS(iface);
650 FIXME("(%p)->(%p)\n", This, pnPriority);
654 static HRESULT WINAPI Binding_GetBindResult(IBinding *iface, CLSID *pclsidProtocol,
655 DWORD *pdwResult, LPOLESTR *pszResult, DWORD *pdwReserved)
657 Binding *This = BINDING_THIS(iface);
658 FIXME("(%p)->(%p %p %p %p)\n", This, pclsidProtocol, pdwResult, pszResult, pdwReserved);
664 static const IBindingVtbl BindingVtbl = {
665 Binding_QueryInterface,
673 Binding_GetBindResult
676 #define PROTSINK_THIS(iface) DEFINE_THIS(Binding, InternetProtocolSink, iface)
678 static HRESULT WINAPI InternetProtocolSink_QueryInterface(IInternetProtocolSink *iface,
679 REFIID riid, void **ppv)
681 Binding *This = PROTSINK_THIS(iface);
682 return IBinding_QueryInterface(BINDING(This), riid, ppv);
685 static ULONG WINAPI InternetProtocolSink_AddRef(IInternetProtocolSink *iface)
687 Binding *This = PROTSINK_THIS(iface);
688 return IBinding_AddRef(BINDING(This));
691 static ULONG WINAPI InternetProtocolSink_Release(IInternetProtocolSink *iface)
693 Binding *This = PROTSINK_THIS(iface);
694 return IBinding_Release(BINDING(This));
698 task_header_t header;
702 static void switch_proc(Binding *binding, task_header_t *t)
704 switch_task_t *task = (switch_task_t*)t;
706 IInternetProtocol_Continue(binding->protocol, task->data);
708 HeapFree(GetProcessHeap(), 0, task);
711 static HRESULT WINAPI InternetProtocolSink_Switch(IInternetProtocolSink *iface,
712 PROTOCOLDATA *pProtocolData)
714 Binding *This = PROTSINK_THIS(iface);
717 TRACE("(%p)->(%p)\n", This, pProtocolData);
719 task = HeapAlloc(GetProcessHeap(), 0, sizeof(switch_task_t));
720 task->data = pProtocolData;
722 push_task(This, &task->header, switch_proc);
724 IBinding_AddRef(BINDING(This));
725 PostMessageW(This->notif_hwnd, WM_MK_CONTINUE, 0, (LPARAM)This);
731 task_header_t header;
738 } on_progress_task_t;
740 static void on_progress_proc(Binding *binding, task_header_t *t)
742 on_progress_task_t *task = (on_progress_task_t*)t;
744 IBindStatusCallback_OnProgress(binding->callback, task->progress,
745 task->progress_max, task->status_code, task->status_text);
747 HeapFree(GetProcessHeap(), 0, task->status_text);
748 HeapFree(GetProcessHeap(), 0, task);
751 static void on_progress(Binding *This, ULONG progress, ULONG progress_max,
752 ULONG status_code, LPCWSTR status_text)
754 on_progress_task_t *task;
756 if(GetCurrentThreadId() == This->apartment_thread && !This->continue_call) {
757 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
758 status_code, status_text);
762 task = HeapAlloc(GetProcessHeap(), 0, sizeof(on_progress_task_t));
764 task->progress = progress;
765 task->progress_max = progress_max;
766 task->status_code = status_code;
769 DWORD size = (strlenW(status_text)+1)*sizeof(WCHAR);
771 task->status_text = HeapAlloc(GetProcessHeap(), 0, size);
772 memcpy(task->status_text, status_text, size);
774 task->status_text = NULL;
777 push_task(This, &task->header, on_progress_proc);
779 if(GetCurrentThreadId() != This->apartment_thread) {
780 IBinding_AddRef(BINDING(This));
781 PostMessageW(This->notif_hwnd, WM_MK_CONTINUE, 0, (LPARAM)This);
785 static HRESULT WINAPI InternetProtocolSink_ReportProgress(IInternetProtocolSink *iface,
786 ULONG ulStatusCode, LPCWSTR szStatusText)
788 Binding *This = PROTSINK_THIS(iface);
790 TRACE("(%p)->(%u %s)\n", This, ulStatusCode, debugstr_w(szStatusText));
792 switch(ulStatusCode) {
793 case BINDSTATUS_FINDINGRESOURCE:
794 on_progress(This, 0, 0, BINDSTATUS_FINDINGRESOURCE, szStatusText);
796 case BINDSTATUS_CONNECTING:
797 on_progress(This, 0, 0, BINDSTATUS_CONNECTING, szStatusText);
799 case BINDSTATUS_BEGINDOWNLOADDATA:
800 fill_stream_buffer(This->stream);
802 case BINDSTATUS_MIMETYPEAVAILABLE: {
803 int len = strlenW(szStatusText)+1;
804 This->mime = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
805 memcpy(This->mime, szStatusText, len*sizeof(WCHAR));
808 case BINDSTATUS_SENDINGREQUEST:
809 on_progress(This, 0, 0, BINDSTATUS_SENDINGREQUEST, szStatusText);
811 case BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE:
812 This->report_mime = FALSE;
813 on_progress(This, 0, 0, BINDSTATUS_MIMETYPEAVAILABLE, szStatusText);
815 case BINDSTATUS_CACHEFILENAMEAVAILABLE:
817 case BINDSTATUS_DIRECTBIND:
818 This->report_mime = FALSE;
821 FIXME("Unhandled status code %d\n", ulStatusCode);
828 static void report_data(Binding *This, DWORD bscf, ULONG progress, ULONG progress_max)
830 FORMATETC formatetc = {0, NULL, 1, -1, TYMED_ISTREAM};
832 TRACE("(%p)->(%d %u %u)\n", This, bscf, progress, progress_max);
834 if(This->download_state == END_DOWNLOAD)
837 if(GetCurrentThreadId() != This->apartment_thread)
838 FIXME("called from worked hread\n");
840 if(This->report_mime) {
843 This->report_mime = FALSE;
845 fill_stream_buffer(This->stream);
847 FindMimeFromData(NULL, This->url, This->stream->buf,
848 min(This->stream->buf_size, 255), This->mime, 0, &mime, 0);
850 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
851 BINDSTATUS_MIMETYPEAVAILABLE, mime);
854 if(This->download_state == BEFORE_DOWNLOAD) {
855 fill_stream_buffer(This->stream);
857 This->download_state = DOWNLOADING;
858 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
859 BINDSTATUS_BEGINDOWNLOADDATA, This->url);
862 if(This->stream->hres == S_FALSE || (bscf & BSCF_LASTDATANOTIFICATION)) {
863 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
864 BINDSTATUS_ENDDOWNLOADDATA, This->url);
867 if(!This->request_locked) {
868 HRESULT hres = IInternetProtocol_LockRequest(This->protocol, 0);
869 This->request_locked = SUCCEEDED(hres);
872 fill_stream_buffer(This->stream);
874 IBindStatusCallback_OnDataAvailable(This->callback, bscf, This->stream->buf_size,
875 &formatetc, &This->stgmed);
877 if(This->stream->hres == S_FALSE) {
878 This->download_state = END_DOWNLOAD;
879 IBindStatusCallback_OnStopBinding(This->callback, S_OK, NULL);
884 task_header_t header;
888 } report_data_task_t;
890 static void report_data_proc(Binding *binding, task_header_t *t)
892 report_data_task_t *task = (report_data_task_t*)t;
894 report_data(binding, task->bscf, task->progress, task->progress_max);
896 HeapFree(GetProcessHeap(), 0, task);
899 static HRESULT WINAPI InternetProtocolSink_ReportData(IInternetProtocolSink *iface,
900 DWORD grfBSCF, ULONG ulProgress, ULONG ulProgressMax)
902 Binding *This = PROTSINK_THIS(iface);
904 TRACE("(%p)->(%d %u %u)\n", This, grfBSCF, ulProgress, ulProgressMax);
906 if(GetCurrentThreadId() != This->apartment_thread)
907 FIXME("called from worked hread\n");
909 if(This->continue_call) {
910 report_data_task_t *task = HeapAlloc(GetProcessHeap(), 0, sizeof(report_data_task_t));
911 task->bscf = grfBSCF;
912 task->progress = ulProgress;
913 task->progress_max = ulProgressMax;
915 push_task(This, &task->header, report_data_proc);
917 report_data(This, grfBSCF, ulProgress, ulProgressMax);
923 static void report_result_proc(Binding *binding, task_header_t *t)
925 IInternetProtocol_Terminate(binding->protocol, 0);
927 if(binding->request_locked) {
928 IInternetProtocol_UnlockRequest(binding->protocol);
929 binding->request_locked = FALSE;
932 HeapFree(GetProcessHeap(), 0, t);
935 static HRESULT WINAPI InternetProtocolSink_ReportResult(IInternetProtocolSink *iface,
936 HRESULT hrResult, DWORD dwError, LPCWSTR szResult)
938 Binding *This = PROTSINK_THIS(iface);
940 TRACE("(%p)->(%08x %d %s)\n", This, hrResult, dwError, debugstr_w(szResult));
942 if(GetCurrentThreadId() == This->apartment_thread && !This->continue_call) {
943 IInternetProtocol_Terminate(This->protocol, 0);
945 task_header_t *task = HeapAlloc(GetProcessHeap(), 0, sizeof(task_header_t));
946 push_task(This, task, report_result_proc);
954 static const IInternetProtocolSinkVtbl InternetProtocolSinkVtbl = {
955 InternetProtocolSink_QueryInterface,
956 InternetProtocolSink_AddRef,
957 InternetProtocolSink_Release,
958 InternetProtocolSink_Switch,
959 InternetProtocolSink_ReportProgress,
960 InternetProtocolSink_ReportData,
961 InternetProtocolSink_ReportResult
964 #define BINDINF_THIS(iface) DEFINE_THIS(Binding, InternetBindInfo, iface)
966 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
967 REFIID riid, void **ppv)
969 Binding *This = BINDINF_THIS(iface);
970 return IBinding_QueryInterface(BINDING(This), riid, ppv);
973 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
975 Binding *This = BINDINF_THIS(iface);
976 return IBinding_AddRef(BINDING(This));
979 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
981 Binding *This = BINDINF_THIS(iface);
982 return IBinding_Release(BINDING(This));
985 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
986 DWORD *grfBINDF, BINDINFO *pbindinfo)
988 Binding *This = BINDINF_THIS(iface);
990 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
992 *grfBINDF = This->bindf;
994 memcpy(pbindinfo, &This->bindinfo, sizeof(BINDINFO));
996 if(pbindinfo->szExtraInfo || pbindinfo->szCustomVerb)
997 FIXME("copy strings\n");
1000 IUnknown_AddRef(pbindinfo->pUnk);
1005 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
1006 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
1008 Binding *This = BINDINF_THIS(iface);
1010 TRACE("(%p)->(%d %p %d %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
1012 switch(ulStringType) {
1013 case BINDSTRING_ACCEPT_MIMES: {
1014 static const WCHAR wszMimes[] = {'*','/','*',0};
1016 if(!ppwzStr || !pcElFetched)
1017 return E_INVALIDARG;
1019 ppwzStr[0] = CoTaskMemAlloc(sizeof(wszMimes));
1020 memcpy(ppwzStr[0], wszMimes, sizeof(wszMimes));
1024 case BINDSTRING_USER_AGENT: {
1025 IInternetBindInfo *bindinfo = NULL;
1028 hres = IBindStatusCallback_QueryInterface(This->callback, &IID_IInternetBindInfo,
1033 hres = IInternetBindInfo_GetBindString(bindinfo, ulStringType, ppwzStr,
1035 IInternetBindInfo_Release(bindinfo);
1041 FIXME("not supported string type %d\n", ulStringType);
1047 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
1048 InternetBindInfo_QueryInterface,
1049 InternetBindInfo_AddRef,
1050 InternetBindInfo_Release,
1051 InternetBindInfo_GetBindInfo,
1052 InternetBindInfo_GetBindString
1055 #define SERVPROV_THIS(iface) DEFINE_THIS(Binding, ServiceProvider, iface)
1057 static HRESULT WINAPI ServiceProvider_QueryInterface(IServiceProvider *iface,
1058 REFIID riid, void **ppv)
1060 Binding *This = SERVPROV_THIS(iface);
1061 return IBinding_QueryInterface(BINDING(This), riid, ppv);
1064 static ULONG WINAPI ServiceProvider_AddRef(IServiceProvider *iface)
1066 Binding *This = SERVPROV_THIS(iface);
1067 return IBinding_AddRef(BINDING(This));
1070 static ULONG WINAPI ServiceProvider_Release(IServiceProvider *iface)
1072 Binding *This = SERVPROV_THIS(iface);
1073 return IBinding_Release(BINDING(This));
1076 static HRESULT WINAPI ServiceProvider_QueryService(IServiceProvider *iface,
1077 REFGUID guidService, REFIID riid, void **ppv)
1079 Binding *This = SERVPROV_THIS(iface);
1082 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
1084 if(This->service_provider) {
1085 hres = IServiceProvider_QueryService(This->service_provider, guidService,
1091 if(IsEqualGUID(&IID_IHttpNegotiate, guidService)
1092 || IsEqualGUID(&IID_IHttpNegotiate2, guidService))
1093 return IHttpNegotiate2_QueryInterface(&HttpNegotiate, riid, ppv);
1095 WARN("unknown service %s\n", debugstr_guid(guidService));
1099 #undef SERVPROV_THIS
1101 static const IServiceProviderVtbl ServiceProviderVtbl = {
1102 ServiceProvider_QueryInterface,
1103 ServiceProvider_AddRef,
1104 ServiceProvider_Release,
1105 ServiceProvider_QueryService
1108 static HRESULT get_callback(IBindCtx *pbc, IBindStatusCallback **callback)
1112 static WCHAR wszBSCBHolder[] = { '_','B','S','C','B','_','H','o','l','d','e','r','_',0 };
1114 hres = IBindCtx_GetObjectParam(pbc, wszBSCBHolder, (IUnknown**)callback);
1121 static HRESULT get_protocol(Binding *This, LPCWSTR url)
1123 IClassFactory *cf = NULL;
1126 hres = IBindStatusCallback_QueryInterface(This->callback, &IID_IInternetProtocol,
1127 (void**)&This->protocol);
1131 if(This->service_provider) {
1132 hres = IServiceProvider_QueryService(This->service_provider, &IID_IInternetProtocol,
1133 &IID_IInternetProtocol, (void**)&This->protocol);
1138 hres = get_protocol_handler(url, NULL, &cf);
1142 hres = IClassFactory_CreateInstance(cf, NULL, &IID_IInternetProtocol, (void**)&This->protocol);
1143 IClassFactory_Release(cf);
1148 static BOOL is_urlmon_protocol(LPCWSTR url)
1150 static const WCHAR wszCdl[] = {'c','d','l'};
1151 static const WCHAR wszFile[] = {'f','i','l','e'};
1152 static const WCHAR wszFtp[] = {'f','t','p'};
1153 static const WCHAR wszGopher[] = {'g','o','p','h','e','r'};
1154 static const WCHAR wszHttp[] = {'h','t','t','p'};
1155 static const WCHAR wszHttps[] = {'h','t','t','p','s'};
1156 static const WCHAR wszMk[] = {'m','k'};
1158 static const struct {
1161 } protocol_list[] = {
1162 {wszCdl, sizeof(wszCdl) /sizeof(WCHAR)},
1163 {wszFile, sizeof(wszFile) /sizeof(WCHAR)},
1164 {wszFtp, sizeof(wszFtp) /sizeof(WCHAR)},
1165 {wszGopher, sizeof(wszGopher)/sizeof(WCHAR)},
1166 {wszHttp, sizeof(wszHttp) /sizeof(WCHAR)},
1167 {wszHttps, sizeof(wszHttps) /sizeof(WCHAR)},
1168 {wszMk, sizeof(wszMk) /sizeof(WCHAR)}
1171 int i, len = strlenW(url);
1173 for(i=0; i < sizeof(protocol_list)/sizeof(protocol_list[0]); i++) {
1174 if(len >= protocol_list[i].len
1175 && !memcmp(url, protocol_list[i].scheme, protocol_list[i].len*sizeof(WCHAR)))
1182 static HRESULT Binding_Create(LPCWSTR url, IBindCtx *pbc, REFIID riid, Binding **binding)
1188 if(!IsEqualGUID(&IID_IStream, riid)) {
1189 FIXME("Unsupported riid %s\n", debugstr_guid(riid));
1193 URLMON_LockModule();
1195 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(Binding));
1197 ret->lpBindingVtbl = &BindingVtbl;
1198 ret->lpInternetProtocolSinkVtbl = &InternetProtocolSinkVtbl;
1199 ret->lpInternetBindInfoVtbl = &InternetBindInfoVtbl;
1200 ret->lpServiceProviderVtbl = &ServiceProviderVtbl;
1204 ret->callback = NULL;
1205 ret->protocol = NULL;
1206 ret->service_provider = NULL;
1210 ret->apartment_thread = GetCurrentThreadId();
1211 ret->notif_hwnd = get_notif_hwnd();
1212 ret->report_mime = TRUE;
1213 ret->continue_call = 0;
1214 ret->request_locked = FALSE;
1215 ret->download_state = BEFORE_DOWNLOAD;
1216 ret->task_queue_head = ret->task_queue_tail = NULL;
1218 memset(&ret->bindinfo, 0, sizeof(BINDINFO));
1219 ret->bindinfo.cbSize = sizeof(BINDINFO);
1222 InitializeCriticalSection(&ret->section);
1224 hres = get_callback(pbc, &ret->callback);
1226 WARN("Could not get IBindStatusCallback\n");
1227 IBinding_Release(BINDING(ret));
1231 IBindStatusCallback_QueryInterface(ret->callback, &IID_IServiceProvider,
1232 (void**)&ret->service_provider);
1234 hres = get_protocol(ret, url);
1236 WARN("Could not get protocol handler\n");
1237 IBinding_Release(BINDING(ret));
1241 hres = IBindStatusCallback_GetBindInfo(ret->callback, &ret->bindf, &ret->bindinfo);
1243 WARN("GetBindInfo failed: %08x\n", hres);
1244 IBinding_Release(BINDING(ret));
1248 dump_BINDINFO(&ret->bindinfo);
1250 ret->bindf |= BINDF_FROMURLMON;
1252 if(!is_urlmon_protocol(url))
1253 ret->bindf |= BINDF_NEEDFILE;
1255 len = strlenW(url)+1;
1256 ret->url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1257 memcpy(ret->url, url, len*sizeof(WCHAR));
1259 ret->stream = create_stream(ret->protocol);
1260 ret->stgmed.tymed = TYMED_ISTREAM;
1261 ret->stgmed.u.pstm = STREAM(ret->stream);
1262 ret->stgmed.pUnkForRelease = (IUnknown*)BINDING(ret); /* NOTE: Windows uses other IUnknown */
1268 HRESULT start_binding(LPCWSTR url, IBindCtx *pbc, REFIID riid, void **ppv)
1270 Binding *binding = NULL;
1275 hres = Binding_Create(url, pbc, riid, &binding);
1279 hres = IBindStatusCallback_OnStartBinding(binding->callback, 0, BINDING(binding));
1281 WARN("OnStartBinding failed: %08x\n", hres);
1282 IBindStatusCallback_OnStopBinding(binding->callback, 0x800c0008, NULL);
1283 IBinding_Release(BINDING(binding));
1287 hres = IInternetProtocol_Start(binding->protocol, url, PROTSINK(binding),
1288 BINDINF(binding), 0, 0);
1291 WARN("Start failed: %08x\n", hres);
1293 IInternetProtocol_Terminate(binding->protocol, 0);
1294 IBindStatusCallback_OnStopBinding(binding->callback, S_OK, NULL);
1295 IBinding_Release(BINDING(binding));
1300 if(binding->stream->init_buf) {
1301 if(binding->request_locked)
1302 IInternetProtocol_UnlockRequest(binding->protocol);
1304 IStream_AddRef(STREAM(binding->stream));
1305 *ppv = binding->stream;
1309 hres = MK_S_ASYNCHRONOUS;
1312 IBinding_Release(BINDING(binding));