wined3d: Print lastpixel state fixme only once.
[wine/hacks.git] / dlls / urlmon / binding.c
blobcd82eb99fd6b0a0511e8260bb749be426f13e49c
1 /*
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 <stdarg.h>
21 #define COBJMACROS
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "ole2.h"
29 #include "urlmon.h"
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 {
44 task_proc_t proc;
45 struct _task_header_t *next;
46 } task_header_t;
48 typedef struct {
49 const IStreamVtbl *lpStreamVtbl;
51 LONG ref;
53 IInternetProtocol *protocol;
55 BYTE buf[1024*8];
56 DWORD buf_size;
57 BOOL init_buf;
58 HRESULT hres;
59 } ProtocolStream;
61 typedef enum {
62 BEFORE_DOWNLOAD,
63 DOWNLOADING,
64 END_DOWNLOAD
65 } download_state_t;
67 struct Binding {
68 const IBindingVtbl *lpBindingVtbl;
69 const IInternetProtocolSinkVtbl *lpInternetProtocolSinkVtbl;
70 const IInternetBindInfoVtbl *lpInternetBindInfoVtbl;
71 const IServiceProviderVtbl *lpServiceProviderVtbl;
73 LONG ref;
75 IBindStatusCallback *callback;
76 IInternetProtocol *protocol;
77 IServiceProvider *service_provider;
78 ProtocolStream *stream;
80 BINDINFO bindinfo;
81 DWORD bindf;
82 LPWSTR mime;
83 LPWSTR url;
84 BOOL report_mime;
85 DWORD continue_call;
86 BOOL request_locked;
87 download_state_t download_state;
89 DWORD apartment_thread;
90 HWND notif_hwnd;
92 STGMEDIUM stgmed;
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)
109 task->proc = proc;
110 task->next = NULL;
112 EnterCriticalSection(&binding->section);
114 if(binding->task_queue_tail)
115 binding->task_queue_tail->next = task;
116 else
117 binding->task_queue_tail = binding->task_queue_head = task;
119 LeaveCriticalSection(&binding->section);
122 static task_header_t *pop_task(Binding *binding)
124 task_header_t *ret;
126 EnterCriticalSection(&binding->section);
128 ret = binding->task_queue_head;
129 if(ret) {
130 binding->task_queue_head = ret->next;
131 if(!binding->task_queue_head)
132 binding->task_queue_tail = NULL;
135 LeaveCriticalSection(&binding->section);
137 return ret;
140 static void fill_stream_buffer(ProtocolStream *This)
142 DWORD read = 0;
144 if(sizeof(This->buf) == This->buf_size)
145 return;
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;
159 task_header_t *task;
161 while((task = pop_task(binding))) {
162 binding->continue_call++;
163 task->proc(binding, task);
164 binding->continue_call--;
167 IBinding_Release(BINDING(binding));
168 return 0;
171 return DefWindowProcW(hwnd, msg, wParam, lParam);
174 static HWND get_notif_hwnd(void)
176 static ATOM wnd_class = 0;
177 HWND hwnd;
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};
183 if(!wnd_class) {
184 static WNDCLASSEXW wndclass = {
185 sizeof(wndclass), 0,
186 notif_wnd_proc, 0, 0,
187 NULL, NULL, NULL, NULL, NULL,
188 wszURLMonikerNotificationWindow,
189 NULL
192 wndclass.hInstance = URLMON_hInstance;
194 wnd_class = RegisterClassExW(&wndclass);
195 if (!wnd_class && GetLastError() == ERROR_CLASS_ALREADY_EXISTS)
196 wnd_class = 1;
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);
205 return hwnd;
208 static void dump_BINDINFO(BINDINFO *bi)
210 static const char * const BINDINFOF_str[] = {
211 "#0",
212 "BINDINFOF_URLENCODESTGMEDDATA",
213 "BINDINFOF_URLENCODEDEXTRAINFO"
216 static const char * const BINDVERB_str[] = {
217 "BINDVERB_GET",
218 "BINDVERB_POST",
219 "BINDVERB_PUT",
220 "BINDVERB_CUSTOM"
223 TRACE("\n"
224 "BINDINFO = {\n"
225 " %d, %s,\n"
226 " {%d, %p, %p},\n"
227 " %s,\n"
228 " %s,\n"
229 " %s,\n"
230 " %d, %08x, %d, %d\n"
231 " {%d %p %x},\n"
232 " %s\n"
233 " %p, %d\n"
234 "}\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)
255 *ppv = NULL;
257 if(IsEqualGUID(&IID_IUnknown, riid)) {
258 TRACE("(IID_IUnknown %p)\n", ppv);
259 *ppv = iface;
260 }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
261 TRACE("(IID_IHttpNegotiate %p)\n", ppv);
262 *ppv = iface;
263 }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
264 TRACE("(IID_IHttpNegotiate2 %p)\n", ppv);
265 *ppv = iface;
268 if(*ppv) {
269 IHttpNegotiate2_AddRef(iface);
270 return S_OK;
273 WARN("Unsupported interface %s\n", debugstr_guid(riid));
274 return E_NOINTERFACE;
277 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
279 URLMON_LockModule();
280 return 2;
283 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
285 URLMON_UnlockModule();
286 return 1;
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;
296 return S_OK;
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;
308 return S_OK;
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 */
317 return E_FAIL;
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);
338 *ppv = NULL;
340 if(IsEqualGUID(&IID_IUnknown, riid)) {
341 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
342 *ppv = STREAM(This);
343 }else if(IsEqualGUID(&IID_ISequentialStream, riid)) {
344 TRACE("(%p)->(IID_ISequentialStream %p)\n", This, ppv);
345 *ppv = STREAM(This);
346 }else if(IsEqualGUID(&IID_IStream, riid)) {
347 TRACE("(%p)->(IID_IStream %p)\n", This, ppv);
348 *ppv = STREAM(This);
351 if(*ppv) {
352 IStream_AddRef(STREAM(This));
353 return S_OK;
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);
367 return 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);
377 if(!ref) {
378 IInternetProtocol_Release(This->protocol);
379 HeapFree(GetProcessHeap(), 0, This);
381 URLMON_UnlockModule();
384 return ref;
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);
395 if(This->buf_size) {
396 read = cb;
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;
408 if(read == cb) {
409 *pcbRead = read;
410 return S_OK;
413 This->hres = IInternetProtocol_Read(This->protocol, (PBYTE)pv+read, cb-read, &pread);
414 *pcbRead = read + pread;
416 if(This->hres == E_PENDING)
417 return 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);
439 return E_NOTIMPL;
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);
446 return E_NOTIMPL;
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);
454 return E_NOTIMPL;
457 static HRESULT WINAPI ProtocolStream_Commit(IStream *iface, DWORD grfCommitFlags)
459 ProtocolStream *This = STREAM_THIS(iface);
461 TRACE("(%p)->(%08x)\n", This, grfCommitFlags);
463 return E_NOTIMPL;
466 static HRESULT WINAPI ProtocolStream_Revert(IStream *iface)
468 ProtocolStream *This = STREAM_THIS(iface);
470 TRACE("(%p)\n", This);
472 return E_NOTIMPL;
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);
480 return E_NOTIMPL;
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);
488 return E_NOTIMPL;
491 static HRESULT WINAPI ProtocolStream_Stat(IStream *iface, STATSTG *pstatstg,
492 DWORD dwStatFlag)
494 ProtocolStream *This = STREAM_THIS(iface);
495 FIXME("(%p)->(%p %08x)\n", This, pstatstg, dwStatFlag);
496 return E_NOTIMPL;
499 static HRESULT WINAPI ProtocolStream_Clone(IStream *iface, IStream **ppstm)
501 ProtocolStream *This = STREAM_THIS(iface);
502 FIXME("(%p)->(%p)\n", This, ppstm);
503 return E_NOTIMPL;
506 #undef STREAM_THIS
508 static const IStreamVtbl ProtocolStreamVtbl = {
509 ProtocolStream_QueryInterface,
510 ProtocolStream_AddRef,
511 ProtocolStream_Release,
512 ProtocolStream_Read,
513 ProtocolStream_Write,
514 ProtocolStream_Seek,
515 ProtocolStream_SetSize,
516 ProtocolStream_CopyTo,
517 ProtocolStream_Commit,
518 ProtocolStream_Revert,
519 ProtocolStream_LockRegion,
520 ProtocolStream_UnlockRegion,
521 ProtocolStream_Stat,
522 ProtocolStream_Clone
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;
532 ret->ref = 1;
533 ret->buf_size = 0;
534 ret->init_buf = FALSE;
535 ret->hres = S_OK;
537 IInternetProtocol_AddRef(protocol);
538 ret->protocol = protocol;
540 URLMON_LockModule();
542 return ret;
545 static HRESULT WINAPI Binding_QueryInterface(IBinding *iface, REFIID riid, void **ppv)
547 Binding *This = BINDING_THIS(iface);
549 *ppv = NULL;
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);
568 if(*ppv) {
569 IBinding_AddRef(BINDING(This));
570 return S_OK;
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);
584 return 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);
594 if(!ref) {
595 if (This->notif_hwnd)
596 DestroyWindow( This->notif_hwnd );
597 if(This->callback)
598 IBindStatusCallback_Release(This->callback);
599 if(This->protocol)
600 IInternetProtocol_Release(This->protocol);
601 if(This->service_provider)
602 IServiceProvider_Release(This->service_provider);
603 if(This->stream)
604 IStream_Release(STREAM(This->stream));
606 ReleaseBindInfo(&This->bindinfo);
607 This->section.DebugInfo->Spare[0] = 0;
608 DeleteCriticalSection(&This->section);
609 HeapFree(GetProcessHeap(), 0, This->mime);
610 HeapFree(GetProcessHeap(), 0, This->url);
612 HeapFree(GetProcessHeap(), 0, This);
614 URLMON_UnlockModule();
617 return ref;
620 static HRESULT WINAPI Binding_Abort(IBinding *iface)
622 Binding *This = BINDING_THIS(iface);
623 FIXME("(%p)\n", This);
624 return E_NOTIMPL;
627 static HRESULT WINAPI Binding_Suspend(IBinding *iface)
629 Binding *This = BINDING_THIS(iface);
630 FIXME("(%p)\n", This);
631 return E_NOTIMPL;
634 static HRESULT WINAPI Binding_Resume(IBinding *iface)
636 Binding *This = BINDING_THIS(iface);
637 FIXME("(%p)\n", This);
638 return E_NOTIMPL;
641 static HRESULT WINAPI Binding_SetPriority(IBinding *iface, LONG nPriority)
643 Binding *This = BINDING_THIS(iface);
644 FIXME("(%p)->(%d)\n", This, nPriority);
645 return E_NOTIMPL;
648 static HRESULT WINAPI Binding_GetPriority(IBinding *iface, LONG *pnPriority)
650 Binding *This = BINDING_THIS(iface);
651 FIXME("(%p)->(%p)\n", This, pnPriority);
652 return E_NOTIMPL;
655 static HRESULT WINAPI Binding_GetBindResult(IBinding *iface, CLSID *pclsidProtocol,
656 DWORD *pdwResult, LPOLESTR *pszResult, DWORD *pdwReserved)
658 Binding *This = BINDING_THIS(iface);
659 FIXME("(%p)->(%p %p %p %p)\n", This, pclsidProtocol, pdwResult, pszResult, pdwReserved);
660 return E_NOTIMPL;
663 #undef BINDING_THIS
665 static const IBindingVtbl BindingVtbl = {
666 Binding_QueryInterface,
667 Binding_AddRef,
668 Binding_Release,
669 Binding_Abort,
670 Binding_Suspend,
671 Binding_Resume,
672 Binding_SetPriority,
673 Binding_GetPriority,
674 Binding_GetBindResult
677 #define PROTSINK_THIS(iface) DEFINE_THIS(Binding, InternetProtocolSink, iface)
679 static HRESULT WINAPI InternetProtocolSink_QueryInterface(IInternetProtocolSink *iface,
680 REFIID riid, void **ppv)
682 Binding *This = PROTSINK_THIS(iface);
683 return IBinding_QueryInterface(BINDING(This), riid, ppv);
686 static ULONG WINAPI InternetProtocolSink_AddRef(IInternetProtocolSink *iface)
688 Binding *This = PROTSINK_THIS(iface);
689 return IBinding_AddRef(BINDING(This));
692 static ULONG WINAPI InternetProtocolSink_Release(IInternetProtocolSink *iface)
694 Binding *This = PROTSINK_THIS(iface);
695 return IBinding_Release(BINDING(This));
698 typedef struct {
699 task_header_t header;
700 PROTOCOLDATA *data;
701 } switch_task_t;
703 static void switch_proc(Binding *binding, task_header_t *t)
705 switch_task_t *task = (switch_task_t*)t;
707 IInternetProtocol_Continue(binding->protocol, task->data);
709 HeapFree(GetProcessHeap(), 0, task);
712 static HRESULT WINAPI InternetProtocolSink_Switch(IInternetProtocolSink *iface,
713 PROTOCOLDATA *pProtocolData)
715 Binding *This = PROTSINK_THIS(iface);
716 switch_task_t *task;
718 TRACE("(%p)->(%p)\n", This, pProtocolData);
720 task = HeapAlloc(GetProcessHeap(), 0, sizeof(switch_task_t));
721 task->data = pProtocolData;
723 push_task(This, &task->header, switch_proc);
725 IBinding_AddRef(BINDING(This));
726 PostMessageW(This->notif_hwnd, WM_MK_CONTINUE, 0, (LPARAM)This);
728 return S_OK;
731 typedef struct {
732 task_header_t header;
734 Binding *binding;
735 ULONG progress;
736 ULONG progress_max;
737 ULONG status_code;
738 LPWSTR status_text;
739 } on_progress_task_t;
741 static void on_progress_proc(Binding *binding, task_header_t *t)
743 on_progress_task_t *task = (on_progress_task_t*)t;
745 IBindStatusCallback_OnProgress(binding->callback, task->progress,
746 task->progress_max, task->status_code, task->status_text);
748 HeapFree(GetProcessHeap(), 0, task->status_text);
749 HeapFree(GetProcessHeap(), 0, task);
752 static void on_progress(Binding *This, ULONG progress, ULONG progress_max,
753 ULONG status_code, LPCWSTR status_text)
755 on_progress_task_t *task;
757 if(GetCurrentThreadId() == This->apartment_thread && !This->continue_call) {
758 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
759 status_code, status_text);
760 return;
763 task = HeapAlloc(GetProcessHeap(), 0, sizeof(on_progress_task_t));
765 task->progress = progress;
766 task->progress_max = progress_max;
767 task->status_code = status_code;
769 if(status_text) {
770 DWORD size = (strlenW(status_text)+1)*sizeof(WCHAR);
772 task->status_text = HeapAlloc(GetProcessHeap(), 0, size);
773 memcpy(task->status_text, status_text, size);
774 }else {
775 task->status_text = NULL;
778 push_task(This, &task->header, on_progress_proc);
780 if(GetCurrentThreadId() != This->apartment_thread) {
781 IBinding_AddRef(BINDING(This));
782 PostMessageW(This->notif_hwnd, WM_MK_CONTINUE, 0, (LPARAM)This);
786 static HRESULT WINAPI InternetProtocolSink_ReportProgress(IInternetProtocolSink *iface,
787 ULONG ulStatusCode, LPCWSTR szStatusText)
789 Binding *This = PROTSINK_THIS(iface);
791 TRACE("(%p)->(%u %s)\n", This, ulStatusCode, debugstr_w(szStatusText));
793 switch(ulStatusCode) {
794 case BINDSTATUS_FINDINGRESOURCE:
795 on_progress(This, 0, 0, BINDSTATUS_FINDINGRESOURCE, szStatusText);
796 break;
797 case BINDSTATUS_CONNECTING:
798 on_progress(This, 0, 0, BINDSTATUS_CONNECTING, szStatusText);
799 break;
800 case BINDSTATUS_BEGINDOWNLOADDATA:
801 fill_stream_buffer(This->stream);
802 break;
803 case BINDSTATUS_MIMETYPEAVAILABLE: {
804 int len = strlenW(szStatusText)+1;
805 This->mime = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
806 memcpy(This->mime, szStatusText, len*sizeof(WCHAR));
807 break;
809 case BINDSTATUS_SENDINGREQUEST:
810 on_progress(This, 0, 0, BINDSTATUS_SENDINGREQUEST, szStatusText);
811 break;
812 case BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE:
813 This->report_mime = FALSE;
814 on_progress(This, 0, 0, BINDSTATUS_MIMETYPEAVAILABLE, szStatusText);
815 break;
816 case BINDSTATUS_CACHEFILENAMEAVAILABLE:
817 break;
818 case BINDSTATUS_DIRECTBIND:
819 This->report_mime = FALSE;
820 break;
821 default:
822 FIXME("Unhandled status code %d\n", ulStatusCode);
823 return E_NOTIMPL;
826 return S_OK;
829 static void report_data(Binding *This, DWORD bscf, ULONG progress, ULONG progress_max)
831 FORMATETC formatetc = {0, NULL, 1, -1, TYMED_ISTREAM};
833 TRACE("(%p)->(%d %u %u)\n", This, bscf, progress, progress_max);
835 if(This->download_state == END_DOWNLOAD)
836 return;
838 if(GetCurrentThreadId() != This->apartment_thread)
839 FIXME("called from worked hread\n");
841 if(This->report_mime) {
842 LPWSTR mime;
844 This->report_mime = FALSE;
846 fill_stream_buffer(This->stream);
848 FindMimeFromData(NULL, This->url, This->stream->buf,
849 min(This->stream->buf_size, 255), This->mime, 0, &mime, 0);
851 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
852 BINDSTATUS_MIMETYPEAVAILABLE, mime);
855 if(This->download_state == BEFORE_DOWNLOAD) {
856 fill_stream_buffer(This->stream);
858 This->download_state = DOWNLOADING;
859 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
860 BINDSTATUS_BEGINDOWNLOADDATA, This->url);
863 if(This->stream->hres == S_FALSE || (bscf & BSCF_LASTDATANOTIFICATION)) {
864 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
865 BINDSTATUS_ENDDOWNLOADDATA, This->url);
868 if(!This->request_locked) {
869 HRESULT hres = IInternetProtocol_LockRequest(This->protocol, 0);
870 This->request_locked = SUCCEEDED(hres);
873 fill_stream_buffer(This->stream);
875 IBindStatusCallback_OnDataAvailable(This->callback, bscf, This->stream->buf_size,
876 &formatetc, &This->stgmed);
878 if(This->stream->hres == S_FALSE) {
879 This->download_state = END_DOWNLOAD;
880 IBindStatusCallback_OnStopBinding(This->callback, S_OK, NULL);
884 typedef struct {
885 task_header_t header;
886 DWORD bscf;
887 ULONG progress;
888 ULONG progress_max;
889 } report_data_task_t;
891 static void report_data_proc(Binding *binding, task_header_t *t)
893 report_data_task_t *task = (report_data_task_t*)t;
895 report_data(binding, task->bscf, task->progress, task->progress_max);
897 HeapFree(GetProcessHeap(), 0, task);
900 static HRESULT WINAPI InternetProtocolSink_ReportData(IInternetProtocolSink *iface,
901 DWORD grfBSCF, ULONG ulProgress, ULONG ulProgressMax)
903 Binding *This = PROTSINK_THIS(iface);
905 TRACE("(%p)->(%d %u %u)\n", This, grfBSCF, ulProgress, ulProgressMax);
907 if(GetCurrentThreadId() != This->apartment_thread)
908 FIXME("called from worked hread\n");
910 if(This->continue_call) {
911 report_data_task_t *task = HeapAlloc(GetProcessHeap(), 0, sizeof(report_data_task_t));
912 task->bscf = grfBSCF;
913 task->progress = ulProgress;
914 task->progress_max = ulProgressMax;
916 push_task(This, &task->header, report_data_proc);
917 }else {
918 report_data(This, grfBSCF, ulProgress, ulProgressMax);
921 return S_OK;
924 static void report_result_proc(Binding *binding, task_header_t *t)
926 IInternetProtocol_Terminate(binding->protocol, 0);
928 if(binding->request_locked) {
929 IInternetProtocol_UnlockRequest(binding->protocol);
930 binding->request_locked = FALSE;
933 HeapFree(GetProcessHeap(), 0, t);
936 static HRESULT WINAPI InternetProtocolSink_ReportResult(IInternetProtocolSink *iface,
937 HRESULT hrResult, DWORD dwError, LPCWSTR szResult)
939 Binding *This = PROTSINK_THIS(iface);
941 TRACE("(%p)->(%08x %d %s)\n", This, hrResult, dwError, debugstr_w(szResult));
943 if(GetCurrentThreadId() == This->apartment_thread && !This->continue_call) {
944 IInternetProtocol_Terminate(This->protocol, 0);
945 }else {
946 task_header_t *task = HeapAlloc(GetProcessHeap(), 0, sizeof(task_header_t));
947 push_task(This, task, report_result_proc);
950 return S_OK;
953 #undef PROTSINK_THIS
955 static const IInternetProtocolSinkVtbl InternetProtocolSinkVtbl = {
956 InternetProtocolSink_QueryInterface,
957 InternetProtocolSink_AddRef,
958 InternetProtocolSink_Release,
959 InternetProtocolSink_Switch,
960 InternetProtocolSink_ReportProgress,
961 InternetProtocolSink_ReportData,
962 InternetProtocolSink_ReportResult
965 #define BINDINF_THIS(iface) DEFINE_THIS(Binding, InternetBindInfo, iface)
967 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
968 REFIID riid, void **ppv)
970 Binding *This = BINDINF_THIS(iface);
971 return IBinding_QueryInterface(BINDING(This), riid, ppv);
974 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
976 Binding *This = BINDINF_THIS(iface);
977 return IBinding_AddRef(BINDING(This));
980 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
982 Binding *This = BINDINF_THIS(iface);
983 return IBinding_Release(BINDING(This));
986 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
987 DWORD *grfBINDF, BINDINFO *pbindinfo)
989 Binding *This = BINDINF_THIS(iface);
991 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
993 *grfBINDF = This->bindf;
995 memcpy(pbindinfo, &This->bindinfo, sizeof(BINDINFO));
997 if(pbindinfo->szExtraInfo || pbindinfo->szCustomVerb)
998 FIXME("copy strings\n");
1000 if(pbindinfo->pUnk)
1001 IUnknown_AddRef(pbindinfo->pUnk);
1003 return S_OK;
1006 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
1007 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
1009 Binding *This = BINDINF_THIS(iface);
1011 TRACE("(%p)->(%d %p %d %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
1013 switch(ulStringType) {
1014 case BINDSTRING_ACCEPT_MIMES: {
1015 static const WCHAR wszMimes[] = {'*','/','*',0};
1017 if(!ppwzStr || !pcElFetched)
1018 return E_INVALIDARG;
1020 ppwzStr[0] = CoTaskMemAlloc(sizeof(wszMimes));
1021 memcpy(ppwzStr[0], wszMimes, sizeof(wszMimes));
1022 *pcElFetched = 1;
1023 return S_OK;
1025 case BINDSTRING_USER_AGENT: {
1026 IInternetBindInfo *bindinfo = NULL;
1027 HRESULT hres;
1029 hres = IBindStatusCallback_QueryInterface(This->callback, &IID_IInternetBindInfo,
1030 (void**)&bindinfo);
1031 if(FAILED(hres))
1032 return hres;
1034 hres = IInternetBindInfo_GetBindString(bindinfo, ulStringType, ppwzStr,
1035 cEl, pcElFetched);
1036 IInternetBindInfo_Release(bindinfo);
1038 return hres;
1042 FIXME("not supported string type %d\n", ulStringType);
1043 return E_NOTIMPL;
1046 #undef BINDF_THIS
1048 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
1049 InternetBindInfo_QueryInterface,
1050 InternetBindInfo_AddRef,
1051 InternetBindInfo_Release,
1052 InternetBindInfo_GetBindInfo,
1053 InternetBindInfo_GetBindString
1056 #define SERVPROV_THIS(iface) DEFINE_THIS(Binding, ServiceProvider, iface)
1058 static HRESULT WINAPI ServiceProvider_QueryInterface(IServiceProvider *iface,
1059 REFIID riid, void **ppv)
1061 Binding *This = SERVPROV_THIS(iface);
1062 return IBinding_QueryInterface(BINDING(This), riid, ppv);
1065 static ULONG WINAPI ServiceProvider_AddRef(IServiceProvider *iface)
1067 Binding *This = SERVPROV_THIS(iface);
1068 return IBinding_AddRef(BINDING(This));
1071 static ULONG WINAPI ServiceProvider_Release(IServiceProvider *iface)
1073 Binding *This = SERVPROV_THIS(iface);
1074 return IBinding_Release(BINDING(This));
1077 static HRESULT WINAPI ServiceProvider_QueryService(IServiceProvider *iface,
1078 REFGUID guidService, REFIID riid, void **ppv)
1080 Binding *This = SERVPROV_THIS(iface);
1081 HRESULT hres;
1083 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
1085 if(This->service_provider) {
1086 hres = IServiceProvider_QueryService(This->service_provider, guidService,
1087 riid, ppv);
1088 if(SUCCEEDED(hres))
1089 return hres;
1092 if(IsEqualGUID(&IID_IHttpNegotiate, guidService)
1093 || IsEqualGUID(&IID_IHttpNegotiate2, guidService))
1094 return IHttpNegotiate2_QueryInterface(&HttpNegotiate, riid, ppv);
1096 WARN("unknown service %s\n", debugstr_guid(guidService));
1097 return E_NOTIMPL;
1100 #undef SERVPROV_THIS
1102 static const IServiceProviderVtbl ServiceProviderVtbl = {
1103 ServiceProvider_QueryInterface,
1104 ServiceProvider_AddRef,
1105 ServiceProvider_Release,
1106 ServiceProvider_QueryService
1109 static HRESULT get_callback(IBindCtx *pbc, IBindStatusCallback **callback)
1111 HRESULT hres;
1113 static WCHAR wszBSCBHolder[] = { '_','B','S','C','B','_','H','o','l','d','e','r','_',0 };
1115 hres = IBindCtx_GetObjectParam(pbc, wszBSCBHolder, (IUnknown**)callback);
1116 if(FAILED(hres))
1117 return MK_E_SYNTAX;
1119 return S_OK;
1122 static HRESULT get_protocol(Binding *This, LPCWSTR url)
1124 IClassFactory *cf = NULL;
1125 HRESULT hres;
1127 hres = IBindStatusCallback_QueryInterface(This->callback, &IID_IInternetProtocol,
1128 (void**)&This->protocol);
1129 if(SUCCEEDED(hres))
1130 return S_OK;
1132 if(This->service_provider) {
1133 hres = IServiceProvider_QueryService(This->service_provider, &IID_IInternetProtocol,
1134 &IID_IInternetProtocol, (void**)&This->protocol);
1135 if(SUCCEEDED(hres))
1136 return S_OK;
1139 hres = get_protocol_handler(url, NULL, &cf);
1140 if(FAILED(hres))
1141 return hres;
1143 hres = IClassFactory_CreateInstance(cf, NULL, &IID_IInternetProtocol, (void**)&This->protocol);
1144 IClassFactory_Release(cf);
1146 return hres;
1149 static BOOL is_urlmon_protocol(LPCWSTR url)
1151 static const WCHAR wszCdl[] = {'c','d','l'};
1152 static const WCHAR wszFile[] = {'f','i','l','e'};
1153 static const WCHAR wszFtp[] = {'f','t','p'};
1154 static const WCHAR wszGopher[] = {'g','o','p','h','e','r'};
1155 static const WCHAR wszHttp[] = {'h','t','t','p'};
1156 static const WCHAR wszHttps[] = {'h','t','t','p','s'};
1157 static const WCHAR wszMk[] = {'m','k'};
1159 static const struct {
1160 LPCWSTR scheme;
1161 int len;
1162 } protocol_list[] = {
1163 {wszCdl, sizeof(wszCdl) /sizeof(WCHAR)},
1164 {wszFile, sizeof(wszFile) /sizeof(WCHAR)},
1165 {wszFtp, sizeof(wszFtp) /sizeof(WCHAR)},
1166 {wszGopher, sizeof(wszGopher)/sizeof(WCHAR)},
1167 {wszHttp, sizeof(wszHttp) /sizeof(WCHAR)},
1168 {wszHttps, sizeof(wszHttps) /sizeof(WCHAR)},
1169 {wszMk, sizeof(wszMk) /sizeof(WCHAR)}
1172 int i, len = strlenW(url);
1174 for(i=0; i < sizeof(protocol_list)/sizeof(protocol_list[0]); i++) {
1175 if(len >= protocol_list[i].len
1176 && !memcmp(url, protocol_list[i].scheme, protocol_list[i].len*sizeof(WCHAR)))
1177 return TRUE;
1180 return FALSE;
1183 static HRESULT Binding_Create(LPCWSTR url, IBindCtx *pbc, REFIID riid, Binding **binding)
1185 Binding *ret;
1186 int len;
1187 HRESULT hres;
1189 if(!IsEqualGUID(&IID_IStream, riid)) {
1190 FIXME("Unsupported riid %s\n", debugstr_guid(riid));
1191 return E_NOTIMPL;
1194 URLMON_LockModule();
1196 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(Binding));
1198 ret->lpBindingVtbl = &BindingVtbl;
1199 ret->lpInternetProtocolSinkVtbl = &InternetProtocolSinkVtbl;
1200 ret->lpInternetBindInfoVtbl = &InternetBindInfoVtbl;
1201 ret->lpServiceProviderVtbl = &ServiceProviderVtbl;
1203 ret->ref = 1;
1205 ret->callback = NULL;
1206 ret->protocol = NULL;
1207 ret->service_provider = NULL;
1208 ret->stream = NULL;
1209 ret->mime = NULL;
1210 ret->url = NULL;
1211 ret->apartment_thread = GetCurrentThreadId();
1212 ret->notif_hwnd = get_notif_hwnd();
1213 ret->report_mime = TRUE;
1214 ret->continue_call = 0;
1215 ret->request_locked = FALSE;
1216 ret->download_state = BEFORE_DOWNLOAD;
1217 ret->task_queue_head = ret->task_queue_tail = NULL;
1219 memset(&ret->bindinfo, 0, sizeof(BINDINFO));
1220 ret->bindinfo.cbSize = sizeof(BINDINFO);
1221 ret->bindf = 0;
1223 InitializeCriticalSection(&ret->section);
1224 ret->section.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": Binding.section");
1226 hres = get_callback(pbc, &ret->callback);
1227 if(FAILED(hres)) {
1228 WARN("Could not get IBindStatusCallback\n");
1229 IBinding_Release(BINDING(ret));
1230 return hres;
1233 IBindStatusCallback_QueryInterface(ret->callback, &IID_IServiceProvider,
1234 (void**)&ret->service_provider);
1236 hres = get_protocol(ret, url);
1237 if(FAILED(hres)) {
1238 WARN("Could not get protocol handler\n");
1239 IBinding_Release(BINDING(ret));
1240 return hres;
1243 hres = IBindStatusCallback_GetBindInfo(ret->callback, &ret->bindf, &ret->bindinfo);
1244 if(FAILED(hres)) {
1245 WARN("GetBindInfo failed: %08x\n", hres);
1246 IBinding_Release(BINDING(ret));
1247 return hres;
1250 dump_BINDINFO(&ret->bindinfo);
1252 ret->bindf |= BINDF_FROMURLMON;
1254 if(!is_urlmon_protocol(url))
1255 ret->bindf |= BINDF_NEEDFILE;
1257 len = strlenW(url)+1;
1258 ret->url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1259 memcpy(ret->url, url, len*sizeof(WCHAR));
1261 ret->stream = create_stream(ret->protocol);
1262 ret->stgmed.tymed = TYMED_ISTREAM;
1263 ret->stgmed.u.pstm = STREAM(ret->stream);
1264 ret->stgmed.pUnkForRelease = (IUnknown*)BINDING(ret); /* NOTE: Windows uses other IUnknown */
1266 *binding = ret;
1267 return S_OK;
1270 HRESULT start_binding(LPCWSTR url, IBindCtx *pbc, REFIID riid, void **ppv)
1272 Binding *binding = NULL;
1273 HRESULT hres;
1275 *ppv = NULL;
1277 hres = Binding_Create(url, pbc, riid, &binding);
1278 if(FAILED(hres))
1279 return hres;
1281 hres = IBindStatusCallback_OnStartBinding(binding->callback, 0, BINDING(binding));
1282 if(FAILED(hres)) {
1283 WARN("OnStartBinding failed: %08x\n", hres);
1284 IBindStatusCallback_OnStopBinding(binding->callback, 0x800c0008, NULL);
1285 IBinding_Release(BINDING(binding));
1286 return hres;
1289 hres = IInternetProtocol_Start(binding->protocol, url, PROTSINK(binding),
1290 BINDINF(binding), 0, 0);
1292 if(FAILED(hres)) {
1293 WARN("Start failed: %08x\n", hres);
1295 IInternetProtocol_Terminate(binding->protocol, 0);
1296 IBindStatusCallback_OnStopBinding(binding->callback, S_OK, NULL);
1297 IBinding_Release(BINDING(binding));
1299 return hres;
1302 if(binding->stream->init_buf) {
1303 if(binding->request_locked)
1304 IInternetProtocol_UnlockRequest(binding->protocol);
1306 IStream_AddRef(STREAM(binding->stream));
1307 *ppv = binding->stream;
1309 hres = S_OK;
1310 }else {
1311 hres = MK_S_ASYNCHRONOUS;
1314 IBinding_Release(BINDING(binding));
1316 return hres;