wine.inf: We should not override existing associations.
[wine/hacks.git] / dlls / urlmon / binding.c
blob4277cb2df3a2f4d82fb3f6e7fa38000fe0e1ecdf
1 /*
2 * Copyright 2005 Jacek Caban
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
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 enum task_enum {
40 TASK_ON_PROGRESS,
41 TASK_SWITCH,
42 TASK_DATA_AVAILABLE
45 typedef struct {
46 Binding *binding;
47 ULONG progress;
48 ULONG progress_max;
49 ULONG status_code;
50 LPWSTR status_text;
51 } on_progress_data;
53 typedef struct _task_t {
54 enum task_enum task;
55 struct _task_t *next;
56 union {
57 on_progress_data on_progress;
58 PROTOCOLDATA *protocol_data;
59 DWORD bscf;
60 } data;
61 } task_t;
63 typedef struct {
64 const IStreamVtbl *lpStreamVtbl;
66 LONG ref;
68 IInternetProtocol *protocol;
70 BYTE buf[1024*8];
71 DWORD buf_size;
72 BOOL init_buf;
73 } ProtocolStream;
75 struct Binding {
76 const IBindingVtbl *lpBindingVtbl;
77 const IInternetProtocolSinkVtbl *lpInternetProtocolSinkVtbl;
78 const IInternetBindInfoVtbl *lpInternetBindInfoVtbl;
79 const IServiceProviderVtbl *lpServiceProviderVtbl;
81 LONG ref;
83 IBindStatusCallback *callback;
84 IInternetProtocol *protocol;
85 IServiceProvider *service_provider;
86 ProtocolStream *stream;
88 BINDINFO bindinfo;
89 DWORD bindf;
90 LPWSTR mime;
91 LPWSTR url;
92 BOOL verified_mime;
93 DWORD continue_call;
95 DWORD apartment_thread;
96 HWND notif_hwnd;
98 STGMEDIUM stgmed;
100 task_t *task_queue_head, *task_queue_tail;
101 CRITICAL_SECTION section;
104 #define BINDING(x) ((IBinding*) &(x)->lpBindingVtbl)
105 #define PROTSINK(x) ((IInternetProtocolSink*) &(x)->lpInternetProtocolSinkVtbl)
106 #define BINDINF(x) ((IInternetBindInfo*) &(x)->lpInternetBindInfoVtbl)
107 #define SERVPROV(x) ((IServiceProvider*) &(x)->lpServiceProviderVtbl)
109 #define STREAM(x) ((IStream*) &(x)->lpStreamVtbl)
111 #define WM_MK_CONTINUE (WM_USER+101)
113 static void push_task(Binding *binding, task_t *task)
115 task->next = NULL;
117 EnterCriticalSection(&binding->section);
119 if(binding->task_queue_tail)
120 binding->task_queue_tail->next = task;
121 else
122 binding->task_queue_tail = binding->task_queue_head = task;
124 LeaveCriticalSection(&binding->section);
127 static task_t *pop_task(Binding *binding)
129 task_t *ret;
131 EnterCriticalSection(&binding->section);
133 ret = binding->task_queue_head;
134 if(ret) {
135 binding->task_queue_head = ret->next;
136 if(!binding->task_queue_head)
137 binding->task_queue_tail = NULL;
140 LeaveCriticalSection(&binding->section);
142 return ret;
145 static void fill_stream_buffer(ProtocolStream *This)
147 DWORD read = 0;
148 HRESULT hres;
150 if(sizeof(This->buf) == This->buf_size)
151 return;
153 hres = IInternetProtocol_Read(This->protocol, This->buf+This->buf_size,
154 sizeof(This->buf)-This->buf_size, &read);
155 if(SUCCEEDED(hres)) {
156 This->buf_size += read;
157 This->init_buf = TRUE;
161 static void on_data_available(Binding *binding, DWORD bscf)
163 FORMATETC formatetc = {0, NULL, 1, -1, TYMED_ISTREAM};
165 if(GetCurrentThreadId() != binding->apartment_thread)
166 FIXME("called from worked hread\n");
168 if(binding->continue_call) {
169 task_t *task = HeapAlloc(GetProcessHeap(), 0, sizeof(task_t));
170 task->task = TASK_DATA_AVAILABLE;
171 task->data.bscf = bscf;
173 push_task(binding, task);
175 return;
178 fill_stream_buffer(binding->stream);
180 IBindStatusCallback_OnDataAvailable(binding->callback, bscf, binding->stream->buf_size,
181 &formatetc, &binding->stgmed);
185 static void do_task(Binding *binding, task_t *task)
187 switch(task->task) {
188 case TASK_ON_PROGRESS: {
189 on_progress_data *data = &task->data.on_progress;
191 IBindStatusCallback_OnProgress(binding->callback, data->progress,
192 data->progress_max, data->status_code, data->status_text);
194 HeapFree(GetProcessHeap(), 0, data->status_text);
195 HeapFree(GetProcessHeap(), 0, data);
197 break;
199 case TASK_SWITCH:
200 binding->continue_call++;
201 IInternetProtocol_Continue(binding->protocol, task->data.protocol_data);
202 binding->continue_call--;
204 break;
205 case TASK_DATA_AVAILABLE:
206 on_data_available(binding, task->data.bscf);
210 static void do_tasks(Binding *This)
212 task_t *task;
214 while((task = pop_task(This)))
215 do_task(This, task);
218 static LRESULT WINAPI notif_wnd_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
220 if(msg == WM_MK_CONTINUE) {
221 Binding *binding = (Binding*)lParam;
223 do_tasks(binding);
225 IBinding_Release(BINDING(binding));
226 return 0;
229 return DefWindowProcW(hwnd, msg, wParam, lParam);
232 static HWND get_notif_hwnd(void)
234 static ATOM wnd_class = 0;
235 HWND hwnd;
237 static const WCHAR wszURLMonikerNotificationWindow[] =
238 {'U','R','L',' ','M','o','n','i','k','e','r',' ',
239 'N','o','t','i','f','i','c','a','t','i','o','n',' ','W','i','n','d','o','w',0};
241 if(!wnd_class) {
242 static WNDCLASSEXW wndclass = {
243 sizeof(wndclass), 0,
244 notif_wnd_proc, 0, 0,
245 NULL, NULL, NULL, NULL, NULL,
246 wszURLMonikerNotificationWindow,
247 NULL
250 wndclass.hInstance = URLMON_hInstance;
252 wnd_class = RegisterClassExW(&wndclass);
255 if(!urlmon_tls)
256 urlmon_tls = TlsAlloc();
258 hwnd = TlsGetValue(urlmon_tls);
259 if(hwnd)
260 return hwnd;
262 hwnd = CreateWindowExW(0, MAKEINTATOMW(wnd_class),
263 wszURLMonikerNotificationWindow, 0, 0, 0, 0, 0, HWND_MESSAGE,
264 NULL, URLMON_hInstance, NULL);
265 TlsSetValue(urlmon_tls, hwnd);
267 TRACE("hwnd = %p\n", hwnd);
269 return hwnd;
272 static void on_progress(Binding *This, ULONG progress, ULONG progress_max,
273 ULONG status_code, LPCWSTR status_text)
275 task_t *task;
277 if(GetCurrentThreadId() == This->apartment_thread && !This->continue_call) {
278 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
279 status_code, status_text);
280 return;
283 task = HeapAlloc(GetProcessHeap(), 0, sizeof(task_t));
285 task->task = TASK_ON_PROGRESS;
286 task->data.on_progress.progress = progress;
287 task->data.on_progress.progress_max = progress_max;
288 task->data.on_progress.status_code = status_code;
290 if(status_text) {
291 DWORD size = (strlenW(status_text)+1)*sizeof(WCHAR);
293 task->data.on_progress.status_text = HeapAlloc(GetProcessHeap(), 0, size);
294 memcpy(task->data.on_progress.status_text, status_text, size);
295 }else {
296 task->data.on_progress.status_text = NULL;
299 push_task(This, task);
301 if(GetCurrentThreadId() != This->apartment_thread) {
302 IBinding_AddRef(BINDING(This));
303 PostMessageW(This->notif_hwnd, WM_MK_CONTINUE, 0, (LPARAM)This);
307 static void dump_BINDINFO(BINDINFO *bi)
309 static const char *BINDINFOF_str[] = {
310 "#0",
311 "BINDINFOF_URLENCODESTGMEDDATA",
312 "BINDINFOF_URLENCODEDEXTRAINFO"
315 static const char *BINDVERB_str[] = {
316 "BINDVERB_GET",
317 "BINDVERB_POST",
318 "BINDVERB_PUT",
319 "BINDVERB_CUSTOM"
322 TRACE("\n"
323 "BINDINFO = {\n"
324 " %ld, %s,\n"
325 " {%ld, %p, %p},\n"
326 " %s,\n"
327 " %s,\n"
328 " %s,\n"
329 " %ld, %08lx, %ld, %ld\n"
330 " {%ld %p %x},\n"
331 " %s\n"
332 " %p, %ld\n"
333 "}\n",
335 bi->cbSize, debugstr_w(bi->szExtraInfo),
336 bi->stgmedData.tymed, bi->stgmedData.u.hGlobal, bi->stgmedData.pUnkForRelease,
337 bi->grfBindInfoF > BINDINFOF_URLENCODEDEXTRAINFO
338 ? "unknown" : BINDINFOF_str[bi->grfBindInfoF],
339 bi->dwBindVerb > BINDVERB_CUSTOM
340 ? "unknown" : BINDVERB_str[bi->dwBindVerb],
341 debugstr_w(bi->szCustomVerb),
342 bi->cbStgmedData, bi->dwOptions, bi->dwOptionsFlags, bi->dwCodePage,
343 bi->securityAttributes.nLength,
344 bi->securityAttributes.lpSecurityDescriptor,
345 bi->securityAttributes.bInheritHandle,
346 debugstr_guid(&bi->iid),
347 bi->pUnk, bi->dwReserved
351 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
352 REFIID riid, void **ppv)
354 *ppv = NULL;
356 if(IsEqualGUID(&IID_IUnknown, riid)) {
357 TRACE("(IID_IUnknown %p)\n", ppv);
358 *ppv = iface;
359 }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
360 TRACE("(IID_IHttpNegotiate %p)\n", ppv);
361 *ppv = iface;
362 }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
363 TRACE("(IID_IHttpNegotiate2 %p)\n", ppv);
364 *ppv = iface;
367 if(*ppv) {
368 IHttpNegotiate2_AddRef(iface);
369 return S_OK;
372 WARN("Unsupported interface %s\n", debugstr_guid(riid));
373 return E_NOINTERFACE;
376 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
378 URLMON_LockModule();
379 return 2;
382 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
384 URLMON_UnlockModule();
385 return 1;
388 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
389 LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
391 TRACE("(%s %s %ld %p)\n", debugstr_w(szURL), debugstr_w(szHeaders), dwReserved,
392 pszAdditionalHeaders);
394 *pszAdditionalHeaders = NULL;
395 return S_OK;
398 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
399 LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders,
400 LPWSTR *pszAdditionalRequestHeaders)
402 TRACE("(%ld %s %s %p)\n", dwResponseCode, debugstr_w(szResponseHeaders),
403 debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
405 if(pszAdditionalRequestHeaders)
406 *pszAdditionalRequestHeaders = NULL;
407 return S_OK;
410 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
411 BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
413 TRACE("(%p %p %ld)\n", pbSecurityId, pcbSecurityId, dwReserved);
415 /* That's all we have to do here */
416 return E_FAIL;
419 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
420 HttpNegotiate_QueryInterface,
421 HttpNegotiate_AddRef,
422 HttpNegotiate_Release,
423 HttpNegotiate_BeginningTransaction,
424 HttpNegotiate_OnResponse,
425 HttpNegotiate_GetRootSecurityId
428 static IHttpNegotiate2 HttpNegotiate = { &HttpNegotiate2Vtbl };
430 #define STREAM_THIS(iface) DEFINE_THIS(ProtocolStream, Stream, iface)
432 static HRESULT WINAPI ProtocolStream_QueryInterface(IStream *iface,
433 REFIID riid, void **ppv)
435 ProtocolStream *This = STREAM_THIS(iface);
437 *ppv = NULL;
439 if(IsEqualGUID(&IID_IUnknown, riid)) {
440 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
441 *ppv = STREAM(This);
442 }else if(IsEqualGUID(&IID_ISequentialStream, riid)) {
443 TRACE("(%p)->(IID_ISequentialStream %p)\n", This, ppv);
444 *ppv = STREAM(This);
445 }else if(IsEqualGUID(&IID_IStream, riid)) {
446 TRACE("(%p)->(IID_IStream %p)\n", This, ppv);
447 *ppv = STREAM(This);
450 if(*ppv) {
451 IStream_AddRef(STREAM(This));
452 return S_OK;
455 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
456 return E_NOINTERFACE;
459 static ULONG WINAPI ProtocolStream_AddRef(IStream *iface)
461 ProtocolStream *This = STREAM_THIS(iface);
462 LONG ref = InterlockedIncrement(&This->ref);
464 TRACE("(%p) ref=%ld\n", This, ref);
466 return ref;
469 static ULONG WINAPI ProtocolStream_Release(IStream *iface)
471 ProtocolStream *This = STREAM_THIS(iface);
472 LONG ref = InterlockedDecrement(&This->ref);
474 TRACE("(%p) ref=%ld\n", This, ref);
476 if(!ref) {
477 IInternetProtocol_Release(This->protocol);
478 HeapFree(GetProcessHeap(), 0, This);
480 URLMON_UnlockModule();
483 return ref;
486 static HRESULT WINAPI ProtocolStream_Read(IStream *iface, void *pv,
487 ULONG cb, ULONG *pcbRead)
489 ProtocolStream *This = STREAM_THIS(iface);
490 DWORD read = 0, pread = 0;
491 HRESULT hres;
493 TRACE("(%p)->(%p %ld %p)\n", This, pv, cb, pcbRead);
495 if(This->buf_size) {
496 read = cb;
498 if(read > This->buf_size)
499 read = This->buf_size;
501 memcpy(pv, This->buf, read);
503 if(read < This->buf_size)
504 memmove(This->buf, This->buf+read, This->buf_size-read);
505 This->buf_size -= read;
508 if(read == cb) {
509 *pcbRead = read;
510 return S_OK;
513 hres = IInternetProtocol_Read(This->protocol, (PBYTE)pv+read, cb-read, &pread);
514 *pcbRead = read + pread;
516 if(hres == E_PENDING)
517 return E_PENDING;
518 else if(FAILED(hres))
519 FIXME("Read failed: %08lx\n", hres);
521 return read || pread ? S_OK : S_FALSE;
524 static HRESULT WINAPI ProtocolStream_Write(IStream *iface, const void *pv,
525 ULONG cb, ULONG *pcbWritten)
527 ProtocolStream *This = STREAM_THIS(iface);
529 TRACE("(%p)->(%p %ld %p)\n", This, pv, cb, pcbWritten);
531 return STG_E_ACCESSDENIED;
534 static HRESULT WINAPI ProtocolStream_Seek(IStream *iface, LARGE_INTEGER dlibMove,
535 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
537 ProtocolStream *This = STREAM_THIS(iface);
538 FIXME("(%p)->(%ld %08lx %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
539 return E_NOTIMPL;
542 static HRESULT WINAPI ProtocolStream_SetSize(IStream *iface, ULARGE_INTEGER libNewSize)
544 ProtocolStream *This = STREAM_THIS(iface);
545 FIXME("(%p)->(%ld)\n", This, libNewSize.u.LowPart);
546 return E_NOTIMPL;
549 static HRESULT WINAPI ProtocolStream_CopyTo(IStream *iface, IStream *pstm,
550 ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten)
552 ProtocolStream *This = STREAM_THIS(iface);
553 FIXME("(%p)->(%p %ld %p %p)\n", This, pstm, cb.u.LowPart, pcbRead, pcbWritten);
554 return E_NOTIMPL;
557 static HRESULT WINAPI ProtocolStream_Commit(IStream *iface, DWORD grfCommitFlags)
559 ProtocolStream *This = STREAM_THIS(iface);
561 TRACE("(%p)->(%08lx)\n", This, grfCommitFlags);
563 return E_NOTIMPL;
566 static HRESULT WINAPI ProtocolStream_Revert(IStream *iface)
568 ProtocolStream *This = STREAM_THIS(iface);
570 TRACE("(%p)\n", This);
572 return E_NOTIMPL;
575 static HRESULT WINAPI ProtocolStream_LockRegion(IStream *iface, ULARGE_INTEGER libOffset,
576 ULARGE_INTEGER cb, DWORD dwLockType)
578 ProtocolStream *This = STREAM_THIS(iface);
579 FIXME("(%p)->(%ld %ld %ld)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
580 return E_NOTIMPL;
583 static HRESULT WINAPI ProtocolStream_UnlockRegion(IStream *iface,
584 ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
586 ProtocolStream *This = STREAM_THIS(iface);
587 FIXME("(%p)->(%ld %ld %ld)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
588 return E_NOTIMPL;
591 static HRESULT WINAPI ProtocolStream_Stat(IStream *iface, STATSTG *pstatstg,
592 DWORD dwStatFlag)
594 ProtocolStream *This = STREAM_THIS(iface);
595 FIXME("(%p)->(%p %08lx)\n", This, pstatstg, dwStatFlag);
596 return E_NOTIMPL;
599 static HRESULT WINAPI ProtocolStream_Clone(IStream *iface, IStream **ppstm)
601 ProtocolStream *This = STREAM_THIS(iface);
602 FIXME("(%p)->(%p)\n", This, ppstm);
603 return E_NOTIMPL;
606 #undef STREAM_THIS
608 static const IStreamVtbl ProtocolStreamVtbl = {
609 ProtocolStream_QueryInterface,
610 ProtocolStream_AddRef,
611 ProtocolStream_Release,
612 ProtocolStream_Read,
613 ProtocolStream_Write,
614 ProtocolStream_Seek,
615 ProtocolStream_SetSize,
616 ProtocolStream_CopyTo,
617 ProtocolStream_Commit,
618 ProtocolStream_Revert,
619 ProtocolStream_LockRegion,
620 ProtocolStream_UnlockRegion,
621 ProtocolStream_Stat,
622 ProtocolStream_Clone
625 #define BINDING_THIS(iface) DEFINE_THIS(Binding, Binding, iface)
627 static ProtocolStream *create_stream(IInternetProtocol *protocol)
629 ProtocolStream *ret = HeapAlloc(GetProcessHeap(), 0, sizeof(ProtocolStream));
631 ret->lpStreamVtbl = &ProtocolStreamVtbl;
632 ret->ref = 1;
633 ret->buf_size = 0;
634 ret->init_buf = FALSE;
636 IInternetProtocol_AddRef(protocol);
637 ret->protocol = protocol;
639 URLMON_LockModule();
641 return ret;
644 static HRESULT WINAPI Binding_QueryInterface(IBinding *iface, REFIID riid, void **ppv)
646 Binding *This = BINDING_THIS(iface);
648 *ppv = NULL;
650 if(IsEqualGUID(&IID_IUnknown, riid)) {
651 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
652 *ppv = BINDING(This);
653 }else if(IsEqualGUID(&IID_IBinding, riid)) {
654 TRACE("(%p)->(IID_IBinding %p)\n", This, ppv);
655 *ppv = BINDING(This);
656 }else if(IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
657 TRACE("(%p)->(IID_IInternetProtocolSink %p)\n", This, ppv);
658 *ppv = PROTSINK(This);
659 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
660 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
661 *ppv = BINDINF(This);
662 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
663 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
664 *ppv = SERVPROV(This);
667 if(*ppv) {
668 IBinding_AddRef(BINDING(This));
669 return S_OK;
672 WARN("Unsupported interface %s\n", debugstr_guid(riid));
673 return E_NOINTERFACE;
676 static ULONG WINAPI Binding_AddRef(IBinding *iface)
678 Binding *This = BINDING_THIS(iface);
679 LONG ref = InterlockedIncrement(&This->ref);
681 TRACE("(%p) ref=%ld\n", This, ref);
683 return ref;
686 static ULONG WINAPI Binding_Release(IBinding *iface)
688 Binding *This = BINDING_THIS(iface);
689 LONG ref = InterlockedDecrement(&This->ref);
691 TRACE("(%p) ref=%ld\n", This, ref);
693 if(!ref) {
694 if(This->callback)
695 IBindStatusCallback_Release(This->callback);
696 if(This->protocol)
697 IInternetProtocol_Release(This->protocol);
698 if(This->service_provider)
699 IServiceProvider_Release(This->service_provider);
700 if(This->stream)
701 IStream_Release(STREAM(This->stream));
703 ReleaseBindInfo(&This->bindinfo);
704 DeleteCriticalSection(&This->section);
705 HeapFree(GetProcessHeap(), 0, This->mime);
706 HeapFree(GetProcessHeap(), 0, This->url);
708 HeapFree(GetProcessHeap(), 0, This);
710 URLMON_UnlockModule();
713 return ref;
716 static HRESULT WINAPI Binding_Abort(IBinding *iface)
718 Binding *This = BINDING_THIS(iface);
719 FIXME("(%p)\n", This);
720 return E_NOTIMPL;
723 static HRESULT WINAPI Binding_Suspend(IBinding *iface)
725 Binding *This = BINDING_THIS(iface);
726 FIXME("(%p)\n", This);
727 return E_NOTIMPL;
730 static HRESULT WINAPI Binding_Resume(IBinding *iface)
732 Binding *This = BINDING_THIS(iface);
733 FIXME("(%p)\n", This);
734 return E_NOTIMPL;
737 static HRESULT WINAPI Binding_SetPriority(IBinding *iface, LONG nPriority)
739 Binding *This = BINDING_THIS(iface);
740 FIXME("(%p)->(%ld)\n", This, nPriority);
741 return E_NOTIMPL;
744 static HRESULT WINAPI Binding_GetPriority(IBinding *iface, LONG *pnPriority)
746 Binding *This = BINDING_THIS(iface);
747 FIXME("(%p)->(%p)\n", This, pnPriority);
748 return E_NOTIMPL;
751 static HRESULT WINAPI Binding_GetBindResult(IBinding *iface, CLSID *pclsidProtocol,
752 DWORD *pdwResult, LPOLESTR *pszResult, DWORD *pdwReserved)
754 Binding *This = BINDING_THIS(iface);
755 FIXME("(%p)->(%p %p %p %p)\n", This, pclsidProtocol, pdwResult, pszResult, pdwReserved);
756 return E_NOTIMPL;
759 #undef BINDING_THIS
761 static const IBindingVtbl BindingVtbl = {
762 Binding_QueryInterface,
763 Binding_AddRef,
764 Binding_Release,
765 Binding_Abort,
766 Binding_Suspend,
767 Binding_Resume,
768 Binding_SetPriority,
769 Binding_GetPriority,
770 Binding_GetBindResult
773 #define PROTSINK_THIS(iface) DEFINE_THIS(Binding, InternetProtocolSink, iface)
775 static HRESULT WINAPI InternetProtocolSink_QueryInterface(IInternetProtocolSink *iface,
776 REFIID riid, void **ppv)
778 Binding *This = PROTSINK_THIS(iface);
779 return IBinding_QueryInterface(BINDING(This), riid, ppv);
782 static ULONG WINAPI InternetProtocolSink_AddRef(IInternetProtocolSink *iface)
784 Binding *This = PROTSINK_THIS(iface);
785 return IBinding_AddRef(BINDING(This));
788 static ULONG WINAPI InternetProtocolSink_Release(IInternetProtocolSink *iface)
790 Binding *This = PROTSINK_THIS(iface);
791 return IBinding_Release(BINDING(This));
794 static HRESULT WINAPI InternetProtocolSink_Switch(IInternetProtocolSink *iface,
795 PROTOCOLDATA *pProtocolData)
797 Binding *This = PROTSINK_THIS(iface);
798 task_t *task;
800 TRACE("(%p)->(%p)\n", This, pProtocolData);
802 task = HeapAlloc(GetProcessHeap(), 0, sizeof(task_t));
803 task->task = TASK_SWITCH;
804 task->data.protocol_data = pProtocolData;
806 push_task(This, task);
808 IBinding_AddRef(BINDING(This));
809 PostMessageW(This->notif_hwnd, WM_MK_CONTINUE, 0, (LPARAM)This);
811 return S_OK;
814 static HRESULT WINAPI InternetProtocolSink_ReportProgress(IInternetProtocolSink *iface,
815 ULONG ulStatusCode, LPCWSTR szStatusText)
817 Binding *This = PROTSINK_THIS(iface);
819 TRACE("(%p)->(%lu %s)\n", This, ulStatusCode, debugstr_w(szStatusText));
821 switch(ulStatusCode) {
822 case BINDSTATUS_FINDINGRESOURCE:
823 on_progress(This, 0, 0, BINDSTATUS_FINDINGRESOURCE, szStatusText);
824 break;
825 case BINDSTATUS_CONNECTING:
826 on_progress(This, 0, 0, BINDSTATUS_CONNECTING, szStatusText);
827 break;
828 case BINDSTATUS_MIMETYPEAVAILABLE: {
829 int len = strlenW(szStatusText)+1;
830 This->mime = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
831 memcpy(This->mime, szStatusText, len*sizeof(WCHAR));
832 break;
834 case BINDSTATUS_SENDINGREQUEST:
835 on_progress(This, 0, 0, BINDSTATUS_SENDINGREQUEST, szStatusText);
836 break;
837 case BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE:
838 This->verified_mime = TRUE;
839 on_progress(This, 0, 0, BINDSTATUS_MIMETYPEAVAILABLE, szStatusText);
840 break;
841 case BINDSTATUS_CACHEFILENAMEAVAILABLE:
842 break;
843 default:
844 FIXME("Unhandled status code %ld\n", ulStatusCode);
845 return E_NOTIMPL;
848 return S_OK;
851 static HRESULT WINAPI InternetProtocolSink_ReportData(IInternetProtocolSink *iface,
852 DWORD grfBSCF, ULONG ulProgress, ULONG ulProgressMax)
854 Binding *This = PROTSINK_THIS(iface);
856 TRACE("(%p)->(%ld %lu %lu)\n", This, grfBSCF, ulProgress, ulProgressMax);
858 if(!This->verified_mime) {
859 LPWSTR mime;
861 This->verified_mime = TRUE;
863 fill_stream_buffer(This->stream);
865 FindMimeFromData(NULL, This->url, This->stream->buf,
866 min(This->stream->buf_size, 255), This->mime, 0, &mime, 0);
868 on_progress(This, ulProgress, ulProgressMax, BINDSTATUS_MIMETYPEAVAILABLE, mime);
871 if(grfBSCF & BSCF_FIRSTDATANOTIFICATION) {
872 on_progress(This, ulProgress, ulProgressMax, BINDSTATUS_BEGINDOWNLOADDATA, This->url);
875 if(grfBSCF & BSCF_LASTDATANOTIFICATION)
876 on_progress(This, ulProgress, ulProgressMax, BINDSTATUS_ENDDOWNLOADDATA, This->url);
878 if(grfBSCF & BSCF_FIRSTDATANOTIFICATION)
879 IInternetProtocol_LockRequest(This->protocol, 0);
881 on_data_available(This, grfBSCF);
883 if(grfBSCF & BSCF_LASTDATANOTIFICATION)
884 IBindStatusCallback_OnStopBinding(This->callback, S_OK, NULL);
886 return S_OK;
889 static HRESULT WINAPI InternetProtocolSink_ReportResult(IInternetProtocolSink *iface,
890 HRESULT hrResult, DWORD dwError, LPCWSTR szResult)
892 Binding *This = PROTSINK_THIS(iface);
894 TRACE("(%p)->(%08lx %ld %s)\n", This, hrResult, dwError, debugstr_w(szResult));
896 IInternetProtocol_Terminate(This->protocol, 0);
897 return S_OK;
900 #undef PROTSINK_THIS
902 static const IInternetProtocolSinkVtbl InternetProtocolSinkVtbl = {
903 InternetProtocolSink_QueryInterface,
904 InternetProtocolSink_AddRef,
905 InternetProtocolSink_Release,
906 InternetProtocolSink_Switch,
907 InternetProtocolSink_ReportProgress,
908 InternetProtocolSink_ReportData,
909 InternetProtocolSink_ReportResult
912 #define BINDINF_THIS(iface) DEFINE_THIS(Binding, InternetBindInfo, iface)
914 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
915 REFIID riid, void **ppv)
917 Binding *This = BINDINF_THIS(iface);
918 return IBinding_QueryInterface(BINDING(This), riid, ppv);
921 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
923 Binding *This = BINDINF_THIS(iface);
924 return IBinding_AddRef(BINDING(This));
927 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
929 Binding *This = BINDINF_THIS(iface);
930 return IBinding_Release(BINDING(This));
933 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
934 DWORD *grfBINDF, BINDINFO *pbindinfo)
936 Binding *This = BINDINF_THIS(iface);
938 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
940 *grfBINDF = This->bindf;
942 memcpy(pbindinfo, &This->bindinfo, sizeof(BINDINFO));
944 if(pbindinfo->szExtraInfo || pbindinfo->szCustomVerb)
945 FIXME("copy strings\n");
947 if(pbindinfo->pUnk)
948 IUnknown_AddRef(pbindinfo->pUnk);
950 return S_OK;
953 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
954 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
956 Binding *This = BINDINF_THIS(iface);
958 TRACE("(%p)->(%ld %p %ld %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
960 switch(ulStringType) {
961 case BINDSTRING_ACCEPT_MIMES: {
962 static const WCHAR wszMimes[] = {'*','/','*',0};
964 if(!ppwzStr || !pcElFetched)
965 return E_INVALIDARG;
967 ppwzStr[0] = CoTaskMemAlloc(sizeof(wszMimes));
968 memcpy(ppwzStr[0], wszMimes, sizeof(wszMimes));
969 *pcElFetched = 1;
970 return S_OK;
972 case BINDSTRING_USER_AGENT: {
973 IInternetBindInfo *bindinfo = NULL;
974 HRESULT hres;
976 hres = IBindStatusCallback_QueryInterface(This->callback, &IID_IInternetBindInfo,
977 (void**)&bindinfo);
978 if(FAILED(hres))
979 return hres;
981 hres = IInternetBindInfo_GetBindString(bindinfo, ulStringType, ppwzStr,
982 cEl, pcElFetched);
983 IInternetBindInfo_Release(bindinfo);
985 return hres;
989 FIXME("not supported string type %ld\n", ulStringType);
990 return E_NOTIMPL;
993 #undef BINDF_THIS
995 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
996 InternetBindInfo_QueryInterface,
997 InternetBindInfo_AddRef,
998 InternetBindInfo_Release,
999 InternetBindInfo_GetBindInfo,
1000 InternetBindInfo_GetBindString
1003 #define SERVPROV_THIS(iface) DEFINE_THIS(Binding, ServiceProvider, iface)
1005 static HRESULT WINAPI ServiceProvider_QueryInterface(IServiceProvider *iface,
1006 REFIID riid, void **ppv)
1008 Binding *This = SERVPROV_THIS(iface);
1009 return IBinding_QueryInterface(BINDING(This), riid, ppv);
1012 static ULONG WINAPI ServiceProvider_AddRef(IServiceProvider *iface)
1014 Binding *This = SERVPROV_THIS(iface);
1015 return IBinding_AddRef(BINDING(This));
1018 static ULONG WINAPI ServiceProvider_Release(IServiceProvider *iface)
1020 Binding *This = SERVPROV_THIS(iface);
1021 return IBinding_Release(BINDING(This));
1024 static HRESULT WINAPI ServiceProvider_QueryService(IServiceProvider *iface,
1025 REFGUID guidService, REFIID riid, void **ppv)
1027 Binding *This = SERVPROV_THIS(iface);
1028 HRESULT hres;
1030 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
1032 if(This->service_provider) {
1033 hres = IServiceProvider_QueryService(This->service_provider, guidService,
1034 riid, ppv);
1035 if(SUCCEEDED(hres))
1036 return hres;
1039 if(IsEqualGUID(&IID_IHttpNegotiate, guidService)
1040 || IsEqualGUID(&IID_IHttpNegotiate2, guidService))
1041 return IHttpNegotiate2_QueryInterface(&HttpNegotiate, riid, ppv);
1043 WARN("unknown service %s\n", debugstr_guid(guidService));
1044 return E_NOTIMPL;
1047 #undef SERVPROV_THIS
1049 static const IServiceProviderVtbl ServiceProviderVtbl = {
1050 ServiceProvider_QueryInterface,
1051 ServiceProvider_AddRef,
1052 ServiceProvider_Release,
1053 ServiceProvider_QueryService
1056 static HRESULT get_callback(IBindCtx *pbc, IBindStatusCallback **callback)
1058 HRESULT hres;
1060 static WCHAR wszBSCBHolder[] = { '_','B','S','C','B','_','H','o','l','d','e','r','_',0 };
1062 hres = IBindCtx_GetObjectParam(pbc, wszBSCBHolder, (IUnknown**)callback);
1063 if(FAILED(hres))
1064 return MK_E_SYNTAX;
1066 return S_OK;
1069 static HRESULT get_protocol(Binding *This, LPCWSTR url)
1071 IClassFactory *cf = NULL;
1072 HRESULT hres;
1074 hres = IBindStatusCallback_QueryInterface(This->callback, &IID_IInternetProtocol,
1075 (void**)&This->protocol);
1076 if(SUCCEEDED(hres))
1077 return S_OK;
1079 if(This->service_provider) {
1080 hres = IServiceProvider_QueryService(This->service_provider, &IID_IInternetProtocol,
1081 &IID_IInternetProtocol, (void**)&This->protocol);
1082 if(SUCCEEDED(hres))
1083 return S_OK;
1086 hres = get_protocol_handler(url, &cf);
1087 if(FAILED(hres))
1088 return hres;
1090 hres = IClassFactory_CreateInstance(cf, NULL, &IID_IInternetProtocol, (void**)&This->protocol);
1091 IClassFactory_Release(cf);
1093 return hres;
1096 static HRESULT Binding_Create(LPCWSTR url, IBindCtx *pbc, REFIID riid, Binding **binding)
1098 Binding *ret;
1099 int len;
1100 HRESULT hres;
1102 static const WCHAR wszFile[] = {'f','i','l','e',':'};
1104 if(!IsEqualGUID(&IID_IStream, riid)) {
1105 FIXME("Unsupported riid %s\n", debugstr_guid(riid));
1106 return E_NOTIMPL;
1109 URLMON_LockModule();
1111 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(Binding));
1113 ret->lpBindingVtbl = &BindingVtbl;
1114 ret->lpInternetProtocolSinkVtbl = &InternetProtocolSinkVtbl;
1115 ret->lpInternetBindInfoVtbl = &InternetBindInfoVtbl;
1116 ret->lpServiceProviderVtbl = &ServiceProviderVtbl;
1118 ret->ref = 1;
1120 ret->callback = NULL;
1121 ret->protocol = NULL;
1122 ret->service_provider = NULL;
1123 ret->stream = NULL;
1124 ret->mime = NULL;
1125 ret->url = NULL;
1126 ret->apartment_thread = GetCurrentThreadId();
1127 ret->notif_hwnd = get_notif_hwnd();
1128 ret->verified_mime = FALSE;
1129 ret->continue_call = 0;
1130 ret->task_queue_head = ret->task_queue_tail = NULL;
1132 memset(&ret->bindinfo, 0, sizeof(BINDINFO));
1133 ret->bindinfo.cbSize = sizeof(BINDINFO);
1134 ret->bindf = 0;
1136 InitializeCriticalSection(&ret->section);
1138 hres = get_callback(pbc, &ret->callback);
1139 if(FAILED(hres)) {
1140 WARN("Could not get IBindStatusCallback\n");
1141 IBinding_Release(BINDING(ret));
1142 return hres;
1145 IBindStatusCallback_QueryInterface(ret->callback, &IID_IServiceProvider,
1146 (void**)&ret->service_provider);
1148 hres = get_protocol(ret, url);
1149 if(FAILED(hres)) {
1150 WARN("Could not get protocol handler\n");
1151 IBinding_Release(BINDING(ret));
1152 return hres;
1155 hres = IBindStatusCallback_GetBindInfo(ret->callback, &ret->bindf, &ret->bindinfo);
1156 if(FAILED(hres)) {
1157 WARN("GetBindInfo failed: %08lx\n", hres);
1158 IBinding_Release(BINDING(ret));
1159 return hres;
1162 dump_BINDINFO(&ret->bindinfo);
1164 ret->bindf |= BINDF_FROMURLMON;
1166 len = strlenW(url)+1;
1168 if(len < sizeof(wszFile)/sizeof(WCHAR) || memcmp(wszFile, url, sizeof(wszFile)))
1169 ret->bindf |= BINDF_NEEDFILE;
1171 ret->url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1172 memcpy(ret->url, url, len*sizeof(WCHAR));
1174 ret->stream = create_stream(ret->protocol);
1175 ret->stgmed.tymed = TYMED_ISTREAM;
1176 ret->stgmed.u.pstm = STREAM(ret->stream);
1177 ret->stgmed.pUnkForRelease = (IUnknown*)BINDING(ret); /* NOTE: Windows uses other IUnknown */
1179 *binding = ret;
1180 return S_OK;
1183 HRESULT start_binding(LPCWSTR url, IBindCtx *pbc, REFIID riid, void **ppv)
1185 Binding *binding = NULL;
1186 HRESULT hres;
1188 *ppv = NULL;
1190 hres = Binding_Create(url, pbc, riid, &binding);
1191 if(FAILED(hres))
1192 return hres;
1194 hres = IBindStatusCallback_OnStartBinding(binding->callback, 0, BINDING(binding));
1195 if(FAILED(hres)) {
1196 WARN("OnStartBinding failed: %08lx\n", hres);
1197 IBindStatusCallback_OnStopBinding(binding->callback, 0x800c0008, NULL);
1198 IBinding_Release(BINDING(binding));
1199 return hres;
1202 hres = IInternetProtocol_Start(binding->protocol, url, PROTSINK(binding),
1203 BINDINF(binding), 0, 0);
1205 if(FAILED(hres)) {
1206 WARN("Start failed: %08lx\n", hres);
1208 IInternetProtocol_Terminate(binding->protocol, 0);
1209 IBindStatusCallback_OnStopBinding(binding->callback, S_OK, NULL);
1210 IBinding_Release(BINDING(binding));
1212 return hres;
1215 if(binding->stream->init_buf) {
1216 IInternetProtocol_UnlockRequest(binding->protocol);
1218 IStream_AddRef(STREAM(binding->stream));
1219 *ppv = binding->stream;
1221 hres = S_OK;
1222 }else {
1223 hres = MK_S_ASYNCHRONOUS;
1226 IBinding_Release(BINDING(binding));
1228 return hres;