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 IHttpNegotiate2Vtbl *lpHttpNegotiate2Vtbl;
53 IHttpNegotiate *http_negotiate;
54 IHttpNegotiate2 *http_negotiate2;
55 } HttpNegotiate2Wrapper;
58 const IStreamVtbl *lpStreamVtbl;
62 IInternetProtocol *protocol;
77 const IBindingVtbl *lpBindingVtbl;
78 const IInternetProtocolSinkVtbl *lpInternetProtocolSinkVtbl;
79 const IInternetBindInfoVtbl *lpInternetBindInfoVtbl;
80 const IServiceProviderVtbl *lpServiceProviderVtbl;
84 IBindStatusCallback *callback;
85 IInternetProtocol *protocol;
86 IServiceProvider *service_provider;
87 ProtocolStream *stream;
88 HttpNegotiate2Wrapper *httpneg2_wrapper;
97 download_state_t download_state;
99 DWORD apartment_thread;
104 task_header_t *task_queue_head, *task_queue_tail;
105 CRITICAL_SECTION section;
108 #define BINDING(x) ((IBinding*) &(x)->lpBindingVtbl)
109 #define PROTSINK(x) ((IInternetProtocolSink*) &(x)->lpInternetProtocolSinkVtbl)
110 #define BINDINF(x) ((IInternetBindInfo*) &(x)->lpInternetBindInfoVtbl)
111 #define SERVPROV(x) ((IServiceProvider*) &(x)->lpServiceProviderVtbl)
113 #define STREAM(x) ((IStream*) &(x)->lpStreamVtbl)
114 #define HTTPNEG2(x) ((IHttpNegotiate2*) &(x)->lpHttpNegotiate2Vtbl)
116 #define WM_MK_CONTINUE (WM_USER+101)
118 static void push_task(Binding *binding, task_header_t *task, task_proc_t proc)
123 EnterCriticalSection(&binding->section);
125 if(binding->task_queue_tail) {
126 binding->task_queue_tail->next = task;
127 binding->task_queue_tail = task;
129 binding->task_queue_tail = binding->task_queue_head = task;
132 LeaveCriticalSection(&binding->section);
135 static task_header_t *pop_task(Binding *binding)
139 EnterCriticalSection(&binding->section);
141 ret = binding->task_queue_head;
143 binding->task_queue_head = ret->next;
144 if(!binding->task_queue_head)
145 binding->task_queue_tail = NULL;
148 LeaveCriticalSection(&binding->section);
153 static void fill_stream_buffer(ProtocolStream *This)
157 if(sizeof(This->buf) == This->buf_size)
160 This->hres = IInternetProtocol_Read(This->protocol, This->buf+This->buf_size,
161 sizeof(This->buf)-This->buf_size, &read);
162 This->buf_size += read;
164 This->init_buf = TRUE;
167 static LRESULT WINAPI notif_wnd_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
169 if(msg == WM_MK_CONTINUE) {
170 Binding *binding = (Binding*)lParam;
173 while((task = pop_task(binding))) {
174 binding->continue_call++;
175 task->proc(binding, task);
176 binding->continue_call--;
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);
207 if (!wnd_class && GetLastError() == ERROR_CLASS_ALREADY_EXISTS)
211 hwnd = CreateWindowExW(0, wszURLMonikerNotificationWindow,
212 wszURLMonikerNotificationWindow, 0, 0, 0, 0, 0, HWND_MESSAGE,
213 NULL, URLMON_hInstance, NULL);
215 TRACE("hwnd = %p\n", hwnd);
220 static void dump_BINDINFO(BINDINFO *bi)
222 static const char * const BINDINFOF_str[] = {
224 "BINDINFOF_URLENCODESTGMEDDATA",
225 "BINDINFOF_URLENCODEDEXTRAINFO"
228 static const char * const BINDVERB_str[] = {
242 " %d, %08x, %d, %d\n"
248 bi->cbSize, debugstr_w(bi->szExtraInfo),
249 bi->stgmedData.tymed, bi->stgmedData.u.hGlobal, bi->stgmedData.pUnkForRelease,
250 bi->grfBindInfoF > BINDINFOF_URLENCODEDEXTRAINFO
251 ? "unknown" : BINDINFOF_str[bi->grfBindInfoF],
252 bi->dwBindVerb > BINDVERB_CUSTOM
253 ? "unknown" : BINDVERB_str[bi->dwBindVerb],
254 debugstr_w(bi->szCustomVerb),
255 bi->cbstgmedData, bi->dwOptions, bi->dwOptionsFlags, bi->dwCodePage,
256 bi->securityAttributes.nLength,
257 bi->securityAttributes.lpSecurityDescriptor,
258 bi->securityAttributes.bInheritHandle,
259 debugstr_guid(&bi->iid),
260 bi->pUnk, bi->dwReserved
264 #define HTTPNEG2_THIS(iface) DEFINE_THIS(HttpNegotiate2Wrapper, HttpNegotiate2, iface)
266 static HRESULT WINAPI HttpNegotiate2Wrapper_QueryInterface(IHttpNegotiate2 *iface,
267 REFIID riid, void **ppv)
269 HttpNegotiate2Wrapper *This = HTTPNEG2_THIS(iface);
273 if(IsEqualGUID(&IID_IUnknown, riid)) {
274 TRACE("(IID_IUnknown %p)\n", ppv);
275 *ppv = HTTPNEG2(This);
276 }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
277 TRACE("(IID_IHttpNegotiate %p)\n", ppv);
278 *ppv = HTTPNEG2(This);
279 }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
280 TRACE("(IID_IHttpNegotiate2 %p)\n", ppv);
281 *ppv = HTTPNEG2(This);
285 IHttpNegotiate2_AddRef(HTTPNEG2(This));
289 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
290 return E_NOINTERFACE;
293 static ULONG WINAPI HttpNegotiate2Wrapper_AddRef(IHttpNegotiate2 *iface)
295 HttpNegotiate2Wrapper *This = HTTPNEG2_THIS(iface);
296 LONG ref = InterlockedIncrement(&This->ref);
298 TRACE("(%p) ref=%d\n", This, ref);
303 static ULONG WINAPI HttpNegotiate2Wrapper_Release(IHttpNegotiate2 *iface)
305 HttpNegotiate2Wrapper *This = HTTPNEG2_THIS(iface);
306 LONG ref = InterlockedDecrement(&This->ref);
308 TRACE("(%p) ref=%d\n", This, ref);
311 if (This->http_negotiate)
312 IHttpNegotiate_Release(This->http_negotiate);
313 if (This->http_negotiate2)
314 IHttpNegotiate2_Release(This->http_negotiate2);
315 HeapFree(GetProcessHeap(), 0, This);
317 URLMON_UnlockModule();
323 static HRESULT WINAPI HttpNegotiate2Wrapper_BeginningTransaction(IHttpNegotiate2 *iface,
324 LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
326 HttpNegotiate2Wrapper *This = HTTPNEG2_THIS(iface);
328 TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders), dwReserved,
329 pszAdditionalHeaders);
331 if(This->http_negotiate)
332 return IHttpNegotiate_BeginningTransaction(This->http_negotiate, szURL, szHeaders,
333 dwReserved, pszAdditionalHeaders);
335 *pszAdditionalHeaders = NULL;
339 static HRESULT WINAPI HttpNegotiate2Wrapper_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
340 LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders,
341 LPWSTR *pszAdditionalRequestHeaders)
343 HttpNegotiate2Wrapper *This = HTTPNEG2_THIS(iface);
344 LPWSTR szAdditionalRequestHeaders = NULL;
347 TRACE("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
348 debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
350 /* IHttpNegotiate2_OnResponse expects pszAdditionalHeaders to be non-NULL when it is
351 * implemented as part of IBindStatusCallback, but it is NULL when called directly from
353 if(!pszAdditionalRequestHeaders)
354 pszAdditionalRequestHeaders = &szAdditionalRequestHeaders;
356 if(This->http_negotiate)
358 hres = IHttpNegotiate_OnResponse(This->http_negotiate, dwResponseCode, szResponseHeaders,
359 szRequestHeaders, pszAdditionalRequestHeaders);
360 if(pszAdditionalRequestHeaders == &szAdditionalRequestHeaders &&
361 szAdditionalRequestHeaders)
362 CoTaskMemFree(szAdditionalRequestHeaders);
366 *pszAdditionalRequestHeaders = NULL;
372 static HRESULT WINAPI HttpNegotiate2Wrapper_GetRootSecurityId(IHttpNegotiate2 *iface,
373 BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
375 HttpNegotiate2Wrapper *This = HTTPNEG2_THIS(iface);
377 TRACE("(%p)->(%p %p %ld)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
379 if (This->http_negotiate2)
380 return IHttpNegotiate2_GetRootSecurityId(This->http_negotiate2, pbSecurityId,
381 pcbSecurityId, dwReserved);
383 /* That's all we have to do here */
389 static const IHttpNegotiate2Vtbl HttpNegotiate2WrapperVtbl = {
390 HttpNegotiate2Wrapper_QueryInterface,
391 HttpNegotiate2Wrapper_AddRef,
392 HttpNegotiate2Wrapper_Release,
393 HttpNegotiate2Wrapper_BeginningTransaction,
394 HttpNegotiate2Wrapper_OnResponse,
395 HttpNegotiate2Wrapper_GetRootSecurityId
398 static HttpNegotiate2Wrapper *create_httpneg2_wrapper(void)
400 HttpNegotiate2Wrapper *ret = HeapAlloc(GetProcessHeap(), 0, sizeof(HttpNegotiate2Wrapper));
402 ret->lpHttpNegotiate2Vtbl = &HttpNegotiate2WrapperVtbl;
404 ret->http_negotiate = NULL;
405 ret->http_negotiate2 = NULL;
412 #define STREAM_THIS(iface) DEFINE_THIS(ProtocolStream, Stream, iface)
414 static HRESULT WINAPI ProtocolStream_QueryInterface(IStream *iface,
415 REFIID riid, void **ppv)
417 ProtocolStream *This = STREAM_THIS(iface);
421 if(IsEqualGUID(&IID_IUnknown, riid)) {
422 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
424 }else if(IsEqualGUID(&IID_ISequentialStream, riid)) {
425 TRACE("(%p)->(IID_ISequentialStream %p)\n", This, ppv);
427 }else if(IsEqualGUID(&IID_IStream, riid)) {
428 TRACE("(%p)->(IID_IStream %p)\n", This, ppv);
433 IStream_AddRef(STREAM(This));
437 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
438 return E_NOINTERFACE;
441 static ULONG WINAPI ProtocolStream_AddRef(IStream *iface)
443 ProtocolStream *This = STREAM_THIS(iface);
444 LONG ref = InterlockedIncrement(&This->ref);
446 TRACE("(%p) ref=%d\n", This, ref);
451 static ULONG WINAPI ProtocolStream_Release(IStream *iface)
453 ProtocolStream *This = STREAM_THIS(iface);
454 LONG ref = InterlockedDecrement(&This->ref);
456 TRACE("(%p) ref=%d\n", This, ref);
459 IInternetProtocol_Release(This->protocol);
460 HeapFree(GetProcessHeap(), 0, This);
462 URLMON_UnlockModule();
468 static HRESULT WINAPI ProtocolStream_Read(IStream *iface, void *pv,
469 ULONG cb, ULONG *pcbRead)
471 ProtocolStream *This = STREAM_THIS(iface);
472 DWORD read = 0, pread = 0;
474 TRACE("(%p)->(%p %d %p)\n", This, pv, cb, pcbRead);
479 if(read > This->buf_size)
480 read = This->buf_size;
482 memcpy(pv, This->buf, read);
484 if(read < This->buf_size)
485 memmove(This->buf, This->buf+read, This->buf_size-read);
486 This->buf_size -= read;
495 This->hres = IInternetProtocol_Read(This->protocol, (PBYTE)pv+read, cb-read, &pread);
497 *pcbRead = read + pread;
499 if(This->hres == E_PENDING)
501 else if(FAILED(This->hres))
502 FIXME("Read failed: %08x\n", This->hres);
504 return read || pread ? S_OK : S_FALSE;
507 static HRESULT WINAPI ProtocolStream_Write(IStream *iface, const void *pv,
508 ULONG cb, ULONG *pcbWritten)
510 ProtocolStream *This = STREAM_THIS(iface);
512 TRACE("(%p)->(%p %d %p)\n", This, pv, cb, pcbWritten);
514 return STG_E_ACCESSDENIED;
517 static HRESULT WINAPI ProtocolStream_Seek(IStream *iface, LARGE_INTEGER dlibMove,
518 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
520 ProtocolStream *This = STREAM_THIS(iface);
521 FIXME("(%p)->(%d %08x %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
525 static HRESULT WINAPI ProtocolStream_SetSize(IStream *iface, ULARGE_INTEGER libNewSize)
527 ProtocolStream *This = STREAM_THIS(iface);
528 FIXME("(%p)->(%d)\n", This, libNewSize.u.LowPart);
532 static HRESULT WINAPI ProtocolStream_CopyTo(IStream *iface, IStream *pstm,
533 ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten)
535 ProtocolStream *This = STREAM_THIS(iface);
536 FIXME("(%p)->(%p %d %p %p)\n", This, pstm, cb.u.LowPart, pcbRead, pcbWritten);
540 static HRESULT WINAPI ProtocolStream_Commit(IStream *iface, DWORD grfCommitFlags)
542 ProtocolStream *This = STREAM_THIS(iface);
544 TRACE("(%p)->(%08x)\n", This, grfCommitFlags);
549 static HRESULT WINAPI ProtocolStream_Revert(IStream *iface)
551 ProtocolStream *This = STREAM_THIS(iface);
553 TRACE("(%p)\n", This);
558 static HRESULT WINAPI ProtocolStream_LockRegion(IStream *iface, ULARGE_INTEGER libOffset,
559 ULARGE_INTEGER cb, DWORD dwLockType)
561 ProtocolStream *This = STREAM_THIS(iface);
562 FIXME("(%p)->(%d %d %d)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
566 static HRESULT WINAPI ProtocolStream_UnlockRegion(IStream *iface,
567 ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
569 ProtocolStream *This = STREAM_THIS(iface);
570 FIXME("(%p)->(%d %d %d)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
574 static HRESULT WINAPI ProtocolStream_Stat(IStream *iface, STATSTG *pstatstg,
577 ProtocolStream *This = STREAM_THIS(iface);
578 FIXME("(%p)->(%p %08x)\n", This, pstatstg, dwStatFlag);
582 static HRESULT WINAPI ProtocolStream_Clone(IStream *iface, IStream **ppstm)
584 ProtocolStream *This = STREAM_THIS(iface);
585 FIXME("(%p)->(%p)\n", This, ppstm);
591 static const IStreamVtbl ProtocolStreamVtbl = {
592 ProtocolStream_QueryInterface,
593 ProtocolStream_AddRef,
594 ProtocolStream_Release,
596 ProtocolStream_Write,
598 ProtocolStream_SetSize,
599 ProtocolStream_CopyTo,
600 ProtocolStream_Commit,
601 ProtocolStream_Revert,
602 ProtocolStream_LockRegion,
603 ProtocolStream_UnlockRegion,
608 #define BINDING_THIS(iface) DEFINE_THIS(Binding, Binding, iface)
610 static ProtocolStream *create_stream(IInternetProtocol *protocol)
612 ProtocolStream *ret = HeapAlloc(GetProcessHeap(), 0, sizeof(ProtocolStream));
614 ret->lpStreamVtbl = &ProtocolStreamVtbl;
617 ret->init_buf = FALSE;
620 IInternetProtocol_AddRef(protocol);
621 ret->protocol = protocol;
628 static HRESULT WINAPI Binding_QueryInterface(IBinding *iface, REFIID riid, void **ppv)
630 Binding *This = BINDING_THIS(iface);
634 if(IsEqualGUID(&IID_IUnknown, riid)) {
635 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
636 *ppv = BINDING(This);
637 }else if(IsEqualGUID(&IID_IBinding, riid)) {
638 TRACE("(%p)->(IID_IBinding %p)\n", This, ppv);
639 *ppv = BINDING(This);
640 }else if(IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
641 TRACE("(%p)->(IID_IInternetProtocolSink %p)\n", This, ppv);
642 *ppv = PROTSINK(This);
643 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
644 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
645 *ppv = BINDINF(This);
646 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
647 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
648 *ppv = SERVPROV(This);
652 IBinding_AddRef(BINDING(This));
656 WARN("Unsupported interface %s\n", debugstr_guid(riid));
657 return E_NOINTERFACE;
660 static ULONG WINAPI Binding_AddRef(IBinding *iface)
662 Binding *This = BINDING_THIS(iface);
663 LONG ref = InterlockedIncrement(&This->ref);
665 TRACE("(%p) ref=%d\n", This, ref);
670 static ULONG WINAPI Binding_Release(IBinding *iface)
672 Binding *This = BINDING_THIS(iface);
673 LONG ref = InterlockedDecrement(&This->ref);
675 TRACE("(%p) ref=%d\n", This, ref);
678 if (This->notif_hwnd)
679 DestroyWindow( This->notif_hwnd );
681 IBindStatusCallback_Release(This->callback);
683 IInternetProtocol_Release(This->protocol);
684 if(This->service_provider)
685 IServiceProvider_Release(This->service_provider);
687 IStream_Release(STREAM(This->stream));
688 if(This->httpneg2_wrapper)
689 IHttpNegotiate2_Release(HTTPNEG2(This->httpneg2_wrapper));
691 ReleaseBindInfo(&This->bindinfo);
692 This->section.DebugInfo->Spare[0] = 0;
693 DeleteCriticalSection(&This->section);
694 HeapFree(GetProcessHeap(), 0, This->mime);
695 HeapFree(GetProcessHeap(), 0, This->url);
697 HeapFree(GetProcessHeap(), 0, This);
699 URLMON_UnlockModule();
705 static HRESULT WINAPI Binding_Abort(IBinding *iface)
707 Binding *This = BINDING_THIS(iface);
708 FIXME("(%p)\n", This);
712 static HRESULT WINAPI Binding_Suspend(IBinding *iface)
714 Binding *This = BINDING_THIS(iface);
715 FIXME("(%p)\n", This);
719 static HRESULT WINAPI Binding_Resume(IBinding *iface)
721 Binding *This = BINDING_THIS(iface);
722 FIXME("(%p)\n", This);
726 static HRESULT WINAPI Binding_SetPriority(IBinding *iface, LONG nPriority)
728 Binding *This = BINDING_THIS(iface);
729 FIXME("(%p)->(%d)\n", This, nPriority);
733 static HRESULT WINAPI Binding_GetPriority(IBinding *iface, LONG *pnPriority)
735 Binding *This = BINDING_THIS(iface);
736 FIXME("(%p)->(%p)\n", This, pnPriority);
740 static HRESULT WINAPI Binding_GetBindResult(IBinding *iface, CLSID *pclsidProtocol,
741 DWORD *pdwResult, LPOLESTR *pszResult, DWORD *pdwReserved)
743 Binding *This = BINDING_THIS(iface);
744 FIXME("(%p)->(%p %p %p %p)\n", This, pclsidProtocol, pdwResult, pszResult, pdwReserved);
750 static const IBindingVtbl BindingVtbl = {
751 Binding_QueryInterface,
759 Binding_GetBindResult
762 #define PROTSINK_THIS(iface) DEFINE_THIS(Binding, InternetProtocolSink, iface)
764 static HRESULT WINAPI InternetProtocolSink_QueryInterface(IInternetProtocolSink *iface,
765 REFIID riid, void **ppv)
767 Binding *This = PROTSINK_THIS(iface);
768 return IBinding_QueryInterface(BINDING(This), riid, ppv);
771 static ULONG WINAPI InternetProtocolSink_AddRef(IInternetProtocolSink *iface)
773 Binding *This = PROTSINK_THIS(iface);
774 return IBinding_AddRef(BINDING(This));
777 static ULONG WINAPI InternetProtocolSink_Release(IInternetProtocolSink *iface)
779 Binding *This = PROTSINK_THIS(iface);
780 return IBinding_Release(BINDING(This));
784 task_header_t header;
788 static void switch_proc(Binding *binding, task_header_t *t)
790 switch_task_t *task = (switch_task_t*)t;
792 IInternetProtocol_Continue(binding->protocol, task->data);
794 HeapFree(GetProcessHeap(), 0, task);
797 static HRESULT WINAPI InternetProtocolSink_Switch(IInternetProtocolSink *iface,
798 PROTOCOLDATA *pProtocolData)
800 Binding *This = PROTSINK_THIS(iface);
803 TRACE("(%p)->(%p)\n", This, pProtocolData);
805 task = HeapAlloc(GetProcessHeap(), 0, sizeof(switch_task_t));
806 task->data = pProtocolData;
808 push_task(This, &task->header, switch_proc);
810 IBinding_AddRef(BINDING(This));
811 PostMessageW(This->notif_hwnd, WM_MK_CONTINUE, 0, (LPARAM)This);
817 task_header_t header;
824 } on_progress_task_t;
826 static void on_progress_proc(Binding *binding, task_header_t *t)
828 on_progress_task_t *task = (on_progress_task_t*)t;
830 IBindStatusCallback_OnProgress(binding->callback, task->progress,
831 task->progress_max, task->status_code, task->status_text);
833 HeapFree(GetProcessHeap(), 0, task->status_text);
834 HeapFree(GetProcessHeap(), 0, task);
837 static void on_progress(Binding *This, ULONG progress, ULONG progress_max,
838 ULONG status_code, LPCWSTR status_text)
840 on_progress_task_t *task;
842 if(GetCurrentThreadId() == This->apartment_thread && !This->continue_call) {
843 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
844 status_code, status_text);
848 task = HeapAlloc(GetProcessHeap(), 0, sizeof(on_progress_task_t));
850 task->progress = progress;
851 task->progress_max = progress_max;
852 task->status_code = status_code;
855 DWORD size = (strlenW(status_text)+1)*sizeof(WCHAR);
857 task->status_text = HeapAlloc(GetProcessHeap(), 0, size);
858 memcpy(task->status_text, status_text, size);
860 task->status_text = NULL;
863 push_task(This, &task->header, on_progress_proc);
865 if(GetCurrentThreadId() != This->apartment_thread) {
866 IBinding_AddRef(BINDING(This));
867 PostMessageW(This->notif_hwnd, WM_MK_CONTINUE, 0, (LPARAM)This);
871 static HRESULT WINAPI InternetProtocolSink_ReportProgress(IInternetProtocolSink *iface,
872 ULONG ulStatusCode, LPCWSTR szStatusText)
874 Binding *This = PROTSINK_THIS(iface);
876 TRACE("(%p)->(%u %s)\n", This, ulStatusCode, debugstr_w(szStatusText));
878 switch(ulStatusCode) {
879 case BINDSTATUS_FINDINGRESOURCE:
880 on_progress(This, 0, 0, BINDSTATUS_FINDINGRESOURCE, szStatusText);
882 case BINDSTATUS_CONNECTING:
883 on_progress(This, 0, 0, BINDSTATUS_CONNECTING, szStatusText);
885 case BINDSTATUS_BEGINDOWNLOADDATA:
886 fill_stream_buffer(This->stream);
888 case BINDSTATUS_MIMETYPEAVAILABLE: {
889 int len = strlenW(szStatusText)+1;
890 This->mime = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
891 memcpy(This->mime, szStatusText, len*sizeof(WCHAR));
894 case BINDSTATUS_SENDINGREQUEST:
895 on_progress(This, 0, 0, BINDSTATUS_SENDINGREQUEST, szStatusText);
897 case BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE:
898 This->report_mime = FALSE;
899 on_progress(This, 0, 0, BINDSTATUS_MIMETYPEAVAILABLE, szStatusText);
901 case BINDSTATUS_CACHEFILENAMEAVAILABLE:
903 case BINDSTATUS_DIRECTBIND:
904 This->report_mime = FALSE;
907 FIXME("Unhandled status code %d\n", ulStatusCode);
914 static void report_data(Binding *This, DWORD bscf, ULONG progress, ULONG progress_max)
916 FORMATETC formatetc = {0, NULL, 1, -1, TYMED_ISTREAM};
917 BOOL sent_begindownloaddata = FALSE;
919 TRACE("(%p)->(%d %u %u)\n", This, bscf, progress, progress_max);
921 if(This->download_state == END_DOWNLOAD)
924 if(GetCurrentThreadId() != This->apartment_thread)
925 FIXME("called from worked hread\n");
927 if(This->report_mime) {
930 This->report_mime = FALSE;
932 fill_stream_buffer(This->stream);
934 FindMimeFromData(NULL, This->url, This->stream->buf,
935 min(This->stream->buf_size, 255), This->mime, 0, &mime, 0);
937 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
938 BINDSTATUS_MIMETYPEAVAILABLE, mime);
941 if(This->download_state == BEFORE_DOWNLOAD) {
942 fill_stream_buffer(This->stream);
944 This->download_state = DOWNLOADING;
945 sent_begindownloaddata = TRUE;
946 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
947 BINDSTATUS_BEGINDOWNLOADDATA, This->url);
950 if(This->stream->hres == S_FALSE || (bscf & BSCF_LASTDATANOTIFICATION)) {
951 This->download_state = END_DOWNLOAD;
952 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
953 BINDSTATUS_ENDDOWNLOADDATA, This->url);
954 }else if(!sent_begindownloaddata) {
955 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
956 BINDSTATUS_DOWNLOADINGDATA, This->url);
959 if(!This->request_locked) {
960 HRESULT hres = IInternetProtocol_LockRequest(This->protocol, 0);
961 This->request_locked = SUCCEEDED(hres);
964 IBindStatusCallback_OnDataAvailable(This->callback, bscf, progress,
965 &formatetc, &This->stgmed);
967 if(This->download_state == END_DOWNLOAD) {
968 IBindStatusCallback_OnStopBinding(This->callback, S_OK, NULL);
973 task_header_t header;
977 } report_data_task_t;
979 static void report_data_proc(Binding *binding, task_header_t *t)
981 report_data_task_t *task = (report_data_task_t*)t;
983 report_data(binding, task->bscf, task->progress, task->progress_max);
985 HeapFree(GetProcessHeap(), 0, task);
988 static HRESULT WINAPI InternetProtocolSink_ReportData(IInternetProtocolSink *iface,
989 DWORD grfBSCF, ULONG ulProgress, ULONG ulProgressMax)
991 Binding *This = PROTSINK_THIS(iface);
993 TRACE("(%p)->(%d %u %u)\n", This, grfBSCF, ulProgress, ulProgressMax);
995 if(GetCurrentThreadId() != This->apartment_thread)
996 FIXME("called from worked hread\n");
998 if(This->continue_call) {
999 report_data_task_t *task = HeapAlloc(GetProcessHeap(), 0, sizeof(report_data_task_t));
1000 task->bscf = grfBSCF;
1001 task->progress = ulProgress;
1002 task->progress_max = ulProgressMax;
1004 push_task(This, &task->header, report_data_proc);
1006 report_data(This, grfBSCF, ulProgress, ulProgressMax);
1012 static void report_result_proc(Binding *binding, task_header_t *t)
1014 IInternetProtocol_Terminate(binding->protocol, 0);
1016 if(binding->request_locked) {
1017 IInternetProtocol_UnlockRequest(binding->protocol);
1018 binding->request_locked = FALSE;
1021 HeapFree(GetProcessHeap(), 0, t);
1024 static HRESULT WINAPI InternetProtocolSink_ReportResult(IInternetProtocolSink *iface,
1025 HRESULT hrResult, DWORD dwError, LPCWSTR szResult)
1027 Binding *This = PROTSINK_THIS(iface);
1029 TRACE("(%p)->(%08x %d %s)\n", This, hrResult, dwError, debugstr_w(szResult));
1031 if(GetCurrentThreadId() == This->apartment_thread && !This->continue_call) {
1032 IInternetProtocol_Terminate(This->protocol, 0);
1034 task_header_t *task = HeapAlloc(GetProcessHeap(), 0, sizeof(task_header_t));
1035 push_task(This, task, report_result_proc);
1041 #undef PROTSINK_THIS
1043 static const IInternetProtocolSinkVtbl InternetProtocolSinkVtbl = {
1044 InternetProtocolSink_QueryInterface,
1045 InternetProtocolSink_AddRef,
1046 InternetProtocolSink_Release,
1047 InternetProtocolSink_Switch,
1048 InternetProtocolSink_ReportProgress,
1049 InternetProtocolSink_ReportData,
1050 InternetProtocolSink_ReportResult
1053 #define BINDINF_THIS(iface) DEFINE_THIS(Binding, InternetBindInfo, iface)
1055 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
1056 REFIID riid, void **ppv)
1058 Binding *This = BINDINF_THIS(iface);
1059 return IBinding_QueryInterface(BINDING(This), riid, ppv);
1062 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
1064 Binding *This = BINDINF_THIS(iface);
1065 return IBinding_AddRef(BINDING(This));
1068 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
1070 Binding *This = BINDINF_THIS(iface);
1071 return IBinding_Release(BINDING(This));
1074 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
1075 DWORD *grfBINDF, BINDINFO *pbindinfo)
1077 Binding *This = BINDINF_THIS(iface);
1079 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
1081 *grfBINDF = This->bindf;
1083 memcpy(pbindinfo, &This->bindinfo, sizeof(BINDINFO));
1085 if(pbindinfo->szExtraInfo || pbindinfo->szCustomVerb)
1086 FIXME("copy strings\n");
1088 if(pbindinfo->stgmedData.pUnkForRelease)
1089 IUnknown_AddRef(pbindinfo->stgmedData.pUnkForRelease);
1092 IUnknown_AddRef(pbindinfo->pUnk);
1097 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
1098 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
1100 Binding *This = BINDINF_THIS(iface);
1102 TRACE("(%p)->(%d %p %d %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
1104 switch(ulStringType) {
1105 case BINDSTRING_ACCEPT_MIMES: {
1106 static const WCHAR wszMimes[] = {'*','/','*',0};
1108 if(!ppwzStr || !pcElFetched)
1109 return E_INVALIDARG;
1111 ppwzStr[0] = CoTaskMemAlloc(sizeof(wszMimes));
1112 memcpy(ppwzStr[0], wszMimes, sizeof(wszMimes));
1116 case BINDSTRING_USER_AGENT: {
1117 IInternetBindInfo *bindinfo = NULL;
1120 hres = IBindStatusCallback_QueryInterface(This->callback, &IID_IInternetBindInfo,
1125 hres = IInternetBindInfo_GetBindString(bindinfo, ulStringType, ppwzStr,
1127 IInternetBindInfo_Release(bindinfo);
1133 FIXME("not supported string type %d\n", ulStringType);
1139 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
1140 InternetBindInfo_QueryInterface,
1141 InternetBindInfo_AddRef,
1142 InternetBindInfo_Release,
1143 InternetBindInfo_GetBindInfo,
1144 InternetBindInfo_GetBindString
1147 #define SERVPROV_THIS(iface) DEFINE_THIS(Binding, ServiceProvider, iface)
1149 static HRESULT WINAPI ServiceProvider_QueryInterface(IServiceProvider *iface,
1150 REFIID riid, void **ppv)
1152 Binding *This = SERVPROV_THIS(iface);
1153 return IBinding_QueryInterface(BINDING(This), riid, ppv);
1156 static ULONG WINAPI ServiceProvider_AddRef(IServiceProvider *iface)
1158 Binding *This = SERVPROV_THIS(iface);
1159 return IBinding_AddRef(BINDING(This));
1162 static ULONG WINAPI ServiceProvider_Release(IServiceProvider *iface)
1164 Binding *This = SERVPROV_THIS(iface);
1165 return IBinding_Release(BINDING(This));
1168 static HRESULT WINAPI ServiceProvider_QueryService(IServiceProvider *iface,
1169 REFGUID guidService, REFIID riid, void **ppv)
1171 Binding *This = SERVPROV_THIS(iface);
1174 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
1176 if(This->service_provider) {
1177 hres = IServiceProvider_QueryService(This->service_provider, guidService,
1183 if(IsEqualGUID(&IID_IHttpNegotiate, guidService)
1184 || IsEqualGUID(&IID_IHttpNegotiate2, guidService)) {
1185 if(!This->httpneg2_wrapper) {
1186 WARN("HttpNegotiate2Wrapper expected to be non-NULL\n");
1188 if(IsEqualGUID(&IID_IHttpNegotiate, guidService))
1189 IBindStatusCallback_QueryInterface(This->callback, riid,
1190 (void **)&This->httpneg2_wrapper->http_negotiate);
1192 IBindStatusCallback_QueryInterface(This->callback, riid,
1193 (void **)&This->httpneg2_wrapper->http_negotiate2);
1195 return IHttpNegotiate2_QueryInterface(HTTPNEG2(This->httpneg2_wrapper), riid, ppv);
1199 WARN("unknown service %s\n", debugstr_guid(guidService));
1203 #undef SERVPROV_THIS
1205 static const IServiceProviderVtbl ServiceProviderVtbl = {
1206 ServiceProvider_QueryInterface,
1207 ServiceProvider_AddRef,
1208 ServiceProvider_Release,
1209 ServiceProvider_QueryService
1212 static HRESULT get_callback(IBindCtx *pbc, IBindStatusCallback **callback)
1216 static WCHAR wszBSCBHolder[] = { '_','B','S','C','B','_','H','o','l','d','e','r','_',0 };
1218 hres = IBindCtx_GetObjectParam(pbc, wszBSCBHolder, (IUnknown**)callback);
1225 static HRESULT get_protocol(Binding *This, LPCWSTR url)
1227 IClassFactory *cf = NULL;
1230 hres = IBindStatusCallback_QueryInterface(This->callback, &IID_IInternetProtocol,
1231 (void**)&This->protocol);
1235 if(This->service_provider) {
1236 hres = IServiceProvider_QueryService(This->service_provider, &IID_IInternetProtocol,
1237 &IID_IInternetProtocol, (void**)&This->protocol);
1242 hres = get_protocol_handler(url, NULL, &cf);
1246 hres = IClassFactory_CreateInstance(cf, NULL, &IID_IInternetProtocol, (void**)&This->protocol);
1247 IClassFactory_Release(cf);
1252 static BOOL is_urlmon_protocol(LPCWSTR url)
1254 static const WCHAR wszCdl[] = {'c','d','l'};
1255 static const WCHAR wszFile[] = {'f','i','l','e'};
1256 static const WCHAR wszFtp[] = {'f','t','p'};
1257 static const WCHAR wszGopher[] = {'g','o','p','h','e','r'};
1258 static const WCHAR wszHttp[] = {'h','t','t','p'};
1259 static const WCHAR wszHttps[] = {'h','t','t','p','s'};
1260 static const WCHAR wszMk[] = {'m','k'};
1262 static const struct {
1265 } protocol_list[] = {
1266 {wszCdl, sizeof(wszCdl) /sizeof(WCHAR)},
1267 {wszFile, sizeof(wszFile) /sizeof(WCHAR)},
1268 {wszFtp, sizeof(wszFtp) /sizeof(WCHAR)},
1269 {wszGopher, sizeof(wszGopher)/sizeof(WCHAR)},
1270 {wszHttp, sizeof(wszHttp) /sizeof(WCHAR)},
1271 {wszHttps, sizeof(wszHttps) /sizeof(WCHAR)},
1272 {wszMk, sizeof(wszMk) /sizeof(WCHAR)}
1275 int i, len = strlenW(url);
1277 for(i=0; i < sizeof(protocol_list)/sizeof(protocol_list[0]); i++) {
1278 if(len >= protocol_list[i].len
1279 && !memcmp(url, protocol_list[i].scheme, protocol_list[i].len*sizeof(WCHAR)))
1286 static HRESULT Binding_Create(LPCWSTR url, IBindCtx *pbc, REFIID riid, Binding **binding)
1292 if(!IsEqualGUID(&IID_IStream, riid)) {
1293 FIXME("Unsupported riid %s\n", debugstr_guid(riid));
1297 URLMON_LockModule();
1299 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(Binding));
1301 ret->lpBindingVtbl = &BindingVtbl;
1302 ret->lpInternetProtocolSinkVtbl = &InternetProtocolSinkVtbl;
1303 ret->lpInternetBindInfoVtbl = &InternetBindInfoVtbl;
1304 ret->lpServiceProviderVtbl = &ServiceProviderVtbl;
1308 ret->callback = NULL;
1309 ret->protocol = NULL;
1310 ret->service_provider = NULL;
1312 ret->httpneg2_wrapper = NULL;
1315 ret->apartment_thread = GetCurrentThreadId();
1316 ret->notif_hwnd = get_notif_hwnd();
1317 ret->report_mime = TRUE;
1318 ret->continue_call = 0;
1319 ret->request_locked = FALSE;
1320 ret->download_state = BEFORE_DOWNLOAD;
1321 ret->task_queue_head = ret->task_queue_tail = NULL;
1323 memset(&ret->bindinfo, 0, sizeof(BINDINFO));
1324 ret->bindinfo.cbSize = sizeof(BINDINFO);
1327 InitializeCriticalSection(&ret->section);
1328 ret->section.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": Binding.section");
1330 hres = get_callback(pbc, &ret->callback);
1332 WARN("Could not get IBindStatusCallback\n");
1333 IBinding_Release(BINDING(ret));
1337 IBindStatusCallback_QueryInterface(ret->callback, &IID_IServiceProvider,
1338 (void**)&ret->service_provider);
1340 hres = get_protocol(ret, url);
1342 WARN("Could not get protocol handler\n");
1343 IBinding_Release(BINDING(ret));
1347 hres = IBindStatusCallback_GetBindInfo(ret->callback, &ret->bindf, &ret->bindinfo);
1349 WARN("GetBindInfo failed: %08x\n", hres);
1350 IBinding_Release(BINDING(ret));
1354 dump_BINDINFO(&ret->bindinfo);
1356 ret->bindf |= BINDF_FROMURLMON;
1358 if(!is_urlmon_protocol(url))
1359 ret->bindf |= BINDF_NEEDFILE;
1361 len = strlenW(url)+1;
1362 ret->url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1363 memcpy(ret->url, url, len*sizeof(WCHAR));
1365 ret->stream = create_stream(ret->protocol);
1366 ret->stgmed.tymed = TYMED_ISTREAM;
1367 ret->stgmed.u.pstm = STREAM(ret->stream);
1368 ret->stgmed.pUnkForRelease = (IUnknown*)BINDING(ret); /* NOTE: Windows uses other IUnknown */
1370 ret->httpneg2_wrapper = create_httpneg2_wrapper();
1376 HRESULT start_binding(LPCWSTR url, IBindCtx *pbc, REFIID riid, void **ppv)
1378 Binding *binding = NULL;
1384 hres = Binding_Create(url, pbc, riid, &binding);
1388 hres = IBindStatusCallback_OnStartBinding(binding->callback, 0, BINDING(binding));
1390 WARN("OnStartBinding failed: %08x\n", hres);
1391 IBindStatusCallback_OnStopBinding(binding->callback, 0x800c0008, NULL);
1392 IBinding_Release(BINDING(binding));
1396 hres = IInternetProtocol_Start(binding->protocol, url, PROTSINK(binding),
1397 BINDINF(binding), 0, 0);
1400 WARN("Start failed: %08x\n", hres);
1402 IInternetProtocol_Terminate(binding->protocol, 0);
1403 IBindStatusCallback_OnStopBinding(binding->callback, S_OK, NULL);
1404 IBinding_Release(BINDING(binding));
1409 while(!(binding->bindf & BINDF_ASYNCHRONOUS) &&
1410 binding->download_state != END_DOWNLOAD) {
1411 MsgWaitForMultipleObjects(0, NULL, FALSE, 5000, QS_POSTMESSAGE);
1412 while (PeekMessageW(&msg, binding->notif_hwnd, WM_USER, WM_USER+117, PM_REMOVE|PM_NOYIELD)) {
1413 TranslateMessage(&msg);
1414 DispatchMessageW(&msg);
1418 if(binding->stream->init_buf) {
1419 if(binding->request_locked)
1420 IInternetProtocol_UnlockRequest(binding->protocol);
1422 IStream_AddRef(STREAM(binding->stream));
1423 *ppv = binding->stream;
1427 hres = MK_S_ASYNCHRONOUS;
1430 IBinding_Release(BINDING(binding));