urlmon: Separate buffer from stream object.
[wine/hacks.git] / dlls / urlmon / binding.c
blob48f537bad5c7d68cb18c69cc4a2bfba5aa8a34f4
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 "urlmon_main.h"
20 #include "winreg.h"
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
26 static WCHAR cbinding_contextW[] = {'C','B','i','n','d','i','n','g',' ','C','o','n','t','e','x','t',0};
27 static WCHAR bscb_holderW[] = { '_','B','S','C','B','_','H','o','l','d','e','r','_',0 };
29 typedef struct Binding Binding;
31 struct _task_header_t;
33 typedef void (*task_proc_t)(Binding*, struct _task_header_t*);
35 typedef struct _task_header_t {
36 task_proc_t proc;
37 struct _task_header_t *next;
38 } task_header_t;
40 typedef struct {
41 const IUnknownVtbl *lpUnknownVtbl;
43 LONG ref;
45 IInternetProtocol *protocol;
47 BYTE buf[1024*8];
48 DWORD size;
49 BOOL init;
50 HRESULT hres;
51 } stgmed_buf_t;
53 #define STGMEDUNK(x) ((IUnknown*) &(x)->lpUnknownVtbl)
55 typedef struct {
56 const IStreamVtbl *lpStreamVtbl;
58 LONG ref;
60 stgmed_buf_t *buf;
61 } ProtocolStream;
63 typedef enum {
64 BEFORE_DOWNLOAD,
65 DOWNLOADING,
66 END_DOWNLOAD
67 } download_state_t;
69 #define BINDING_LOCKED 0x0001
70 #define BINDING_STOPPED 0x0002
71 #define BINDING_OBJAVAIL 0x0004
73 struct Binding {
74 const IBindingVtbl *lpBindingVtbl;
75 const IInternetProtocolSinkVtbl *lpInternetProtocolSinkVtbl;
76 const IInternetBindInfoVtbl *lpInternetBindInfoVtbl;
77 const IServiceProviderVtbl *lpServiceProviderVtbl;
79 LONG ref;
81 IBindStatusCallback *callback;
82 IInternetProtocol *protocol;
83 IServiceProvider *service_provider;
85 stgmed_buf_t *stgmed_buf;
86 ProtocolStream *stream;
88 BINDINFO bindinfo;
89 DWORD bindf;
90 BOOL to_object;
91 LPWSTR mime;
92 UINT clipboard_format;
93 LPWSTR url;
94 IID iid;
95 BOOL report_mime;
96 DWORD continue_call;
97 DWORD state;
98 HRESULT hres;
99 download_state_t download_state;
100 IUnknown *obj;
101 IMoniker *mon;
102 IBindCtx *bctx;
104 DWORD apartment_thread;
105 HWND notif_hwnd;
107 STGMEDIUM stgmed;
109 task_header_t *task_queue_head, *task_queue_tail;
110 CRITICAL_SECTION section;
113 #define BINDING(x) ((IBinding*) &(x)->lpBindingVtbl)
114 #define PROTSINK(x) ((IInternetProtocolSink*) &(x)->lpInternetProtocolSinkVtbl)
115 #define BINDINF(x) ((IInternetBindInfo*) &(x)->lpInternetBindInfoVtbl)
116 #define SERVPROV(x) ((IServiceProvider*) &(x)->lpServiceProviderVtbl)
118 #define STREAM(x) ((IStream*) &(x)->lpStreamVtbl)
119 #define HTTPNEG2(x) ((IHttpNegotiate2*) &(x)->lpHttpNegotiate2Vtbl)
121 #define WM_MK_CONTINUE (WM_USER+101)
123 static void push_task(Binding *binding, task_header_t *task, task_proc_t proc)
125 task->proc = proc;
126 task->next = NULL;
128 EnterCriticalSection(&binding->section);
130 if(binding->task_queue_tail) {
131 binding->task_queue_tail->next = task;
132 binding->task_queue_tail = task;
133 }else {
134 binding->task_queue_tail = binding->task_queue_head = task;
137 LeaveCriticalSection(&binding->section);
140 static task_header_t *pop_task(Binding *binding)
142 task_header_t *ret;
144 EnterCriticalSection(&binding->section);
146 ret = binding->task_queue_head;
147 if(ret) {
148 binding->task_queue_head = ret->next;
149 if(!binding->task_queue_head)
150 binding->task_queue_tail = NULL;
153 LeaveCriticalSection(&binding->section);
155 return ret;
158 static void fill_stgmed_buffer(stgmed_buf_t *buf)
160 DWORD read = 0;
162 if(sizeof(buf->buf) == buf->size)
163 return;
165 buf->hres = IInternetProtocol_Read(buf->protocol, buf->buf+buf->size,
166 sizeof(buf->buf)-buf->size, &read);
167 buf->size += read;
168 if(read > 0)
169 buf->init = TRUE;
172 static LRESULT WINAPI notif_wnd_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
174 if(msg == WM_MK_CONTINUE) {
175 Binding *binding = (Binding*)lParam;
176 task_header_t *task;
178 while((task = pop_task(binding))) {
179 binding->continue_call++;
180 task->proc(binding, task);
181 binding->continue_call--;
184 IBinding_Release(BINDING(binding));
185 return 0;
188 return DefWindowProcW(hwnd, msg, wParam, lParam);
191 static HWND get_notif_hwnd(void)
193 static ATOM wnd_class = 0;
194 HWND hwnd;
196 static const WCHAR wszURLMonikerNotificationWindow[] =
197 {'U','R','L',' ','M','o','n','i','k','e','r',' ',
198 'N','o','t','i','f','i','c','a','t','i','o','n',' ','W','i','n','d','o','w',0};
200 if(!wnd_class) {
201 static WNDCLASSEXW wndclass = {
202 sizeof(wndclass), 0,
203 notif_wnd_proc, 0, 0,
204 NULL, NULL, NULL, NULL, NULL,
205 wszURLMonikerNotificationWindow,
206 NULL
209 wndclass.hInstance = URLMON_hInstance;
211 wnd_class = RegisterClassExW(&wndclass);
212 if (!wnd_class && GetLastError() == ERROR_CLASS_ALREADY_EXISTS)
213 wnd_class = 1;
216 hwnd = CreateWindowExW(0, wszURLMonikerNotificationWindow,
217 wszURLMonikerNotificationWindow, 0, 0, 0, 0, 0, HWND_MESSAGE,
218 NULL, URLMON_hInstance, NULL);
220 TRACE("hwnd = %p\n", hwnd);
222 return hwnd;
225 static void dump_BINDINFO(BINDINFO *bi)
227 static const char * const BINDINFOF_str[] = {
228 "#0",
229 "BINDINFOF_URLENCODESTGMEDDATA",
230 "BINDINFOF_URLENCODEDEXTRAINFO"
233 static const char * const BINDVERB_str[] = {
234 "BINDVERB_GET",
235 "BINDVERB_POST",
236 "BINDVERB_PUT",
237 "BINDVERB_CUSTOM"
240 TRACE("\n"
241 "BINDINFO = {\n"
242 " %d, %s,\n"
243 " {%d, %p, %p},\n"
244 " %s,\n"
245 " %s,\n"
246 " %s,\n"
247 " %d, %08x, %d, %d\n"
248 " {%d %p %x},\n"
249 " %s\n"
250 " %p, %d\n"
251 "}\n",
253 bi->cbSize, debugstr_w(bi->szExtraInfo),
254 bi->stgmedData.tymed, bi->stgmedData.u.hGlobal, bi->stgmedData.pUnkForRelease,
255 bi->grfBindInfoF > BINDINFOF_URLENCODEDEXTRAINFO
256 ? "unknown" : BINDINFOF_str[bi->grfBindInfoF],
257 bi->dwBindVerb > BINDVERB_CUSTOM
258 ? "unknown" : BINDVERB_str[bi->dwBindVerb],
259 debugstr_w(bi->szCustomVerb),
260 bi->cbstgmedData, bi->dwOptions, bi->dwOptionsFlags, bi->dwCodePage,
261 bi->securityAttributes.nLength,
262 bi->securityAttributes.lpSecurityDescriptor,
263 bi->securityAttributes.bInheritHandle,
264 debugstr_guid(&bi->iid),
265 bi->pUnk, bi->dwReserved
269 static void set_binding_mime(Binding *binding, LPCWSTR mime)
271 EnterCriticalSection(&binding->section);
273 if(binding->report_mime) {
274 heap_free(binding->mime);
275 binding->mime = heap_strdupW(mime);
278 LeaveCriticalSection(&binding->section);
281 static void handle_mime_available(Binding *binding, BOOL verify)
283 BOOL report_mime;
285 EnterCriticalSection(&binding->section);
286 report_mime = binding->report_mime;
287 binding->report_mime = FALSE;
288 LeaveCriticalSection(&binding->section);
290 if(!report_mime)
291 return;
293 if(verify) {
294 LPWSTR mime = NULL;
296 fill_stgmed_buffer(binding->stgmed_buf);
297 FindMimeFromData(NULL, binding->url, binding->stgmed_buf->buf,
298 min(binding->stgmed_buf->size, 255), binding->mime, 0, &mime, 0);
300 heap_free(binding->mime);
301 binding->mime = heap_strdupW(mime);
302 CoTaskMemFree(mime);
305 IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_MIMETYPEAVAILABLE, binding->mime);
307 binding->clipboard_format = RegisterClipboardFormatW(binding->mime);
310 typedef struct {
311 task_header_t header;
312 BOOL verify;
313 } mime_available_task_t;
315 static void mime_available_proc(Binding *binding, task_header_t *t)
317 mime_available_task_t *task = (mime_available_task_t*)t;
319 handle_mime_available(binding, task->verify);
321 heap_free(task);
324 static void mime_available(Binding *This, LPCWSTR mime, BOOL verify)
326 if(mime)
327 set_binding_mime(This, mime);
329 if(GetCurrentThreadId() == This->apartment_thread) {
330 handle_mime_available(This, verify);
331 }else {
332 mime_available_task_t *task = heap_alloc(sizeof(task_header_t));
333 task->verify = verify;
334 push_task(This, &task->header, mime_available_proc);
338 static void stop_binding(Binding *binding, HRESULT hres, LPCWSTR str)
340 if(binding->state & BINDING_LOCKED) {
341 IInternetProtocol_UnlockRequest(binding->protocol);
342 binding->state &= ~BINDING_LOCKED;
345 if(!(binding->state & BINDING_STOPPED)) {
346 binding->state |= BINDING_STOPPED;
348 IBindStatusCallback_OnStopBinding(binding->callback, hres, str);
349 binding->hres = hres;
353 static LPWSTR get_mime_clsid(LPCWSTR mime, CLSID *clsid)
355 LPWSTR key_name, ret;
356 DWORD res, type, size;
357 HKEY hkey;
358 int len;
359 HRESULT hres;
361 static const WCHAR mime_keyW[] =
362 {'M','I','M','E','\\','D','a','t','a','b','a','s','e','\\',
363 'C','o','n','t','e','n','t',' ','T','y','p','e','\\'};
364 static const WCHAR clsidW[] = {'C','L','S','I','D',0};
366 len = strlenW(mime)+1;
367 key_name = heap_alloc(sizeof(mime_keyW) + len*sizeof(WCHAR));
368 memcpy(key_name, mime_keyW, sizeof(mime_keyW));
369 strcpyW(key_name + sizeof(mime_keyW)/sizeof(WCHAR), mime);
371 res = RegOpenKeyW(HKEY_CLASSES_ROOT, key_name, &hkey);
372 heap_free(key_name);
373 if(res != ERROR_SUCCESS) {
374 WARN("Could not open MIME key: %x\n", res);
375 return NULL;
378 size = 50*sizeof(WCHAR);
379 ret = heap_alloc(size);
380 res = RegQueryValueExW(hkey, clsidW, NULL, &type, (LPBYTE)ret, &size);
381 RegCloseKey(hkey);
382 if(res != ERROR_SUCCESS) {
383 WARN("Could not get CLSID: %08x\n", res);
384 heap_free(ret);
385 return NULL;
388 hres = CLSIDFromString(ret, clsid);
389 if(FAILED(hres)) {
390 WARN("Could not parse CLSID: %08x\n", hres);
391 heap_free(ret);
392 return NULL;
395 return ret;
398 static void load_doc_mon(Binding *binding, IPersistMoniker *persist)
400 IBindCtx *bctx;
401 HRESULT hres;
403 hres = CreateAsyncBindCtxEx(binding->bctx, 0, NULL, NULL, &bctx, 0);
404 if(FAILED(hres)) {
405 WARN("CreateAsyncBindCtxEx failed: %08x\n", hres);
406 return;
409 IBindCtx_RevokeObjectParam(bctx, bscb_holderW);
410 IBindCtx_RegisterObjectParam(bctx, cbinding_contextW, (IUnknown*)BINDING(binding));
412 hres = IPersistMoniker_Load(persist, binding->download_state == END_DOWNLOAD, binding->mon, bctx, 0x12);
413 IBindCtx_RevokeObjectParam(bctx, cbinding_contextW);
414 IBindCtx_Release(bctx);
415 if(FAILED(hres))
416 FIXME("Load failed: %08x\n", hres);
419 static void create_object(Binding *binding)
421 IPersistMoniker *persist;
422 LPWSTR clsid_str;
423 CLSID clsid;
424 HRESULT hres;
426 if(!binding->mime) {
427 FIXME("MIME unavailable\n");
428 return;
431 if(!(clsid_str = get_mime_clsid(binding->mime, &clsid))) {
432 FIXME("Could not find object for MIME %s\n", debugstr_w(binding->mime));
433 return;
436 IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_CLASSIDAVAILABLE, clsid_str);
438 IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_BEGINSYNCOPERATION, NULL);
440 hres = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
441 &binding->iid, (void**)&binding->obj);
442 if(FAILED(hres))
443 FIXME("CoCreateInstance failed: %08x\n", hres);
445 binding->state |= BINDING_OBJAVAIL;
447 hres = IUnknown_QueryInterface(binding->obj, &IID_IPersistMoniker, (void**)&persist);
448 if(SUCCEEDED(hres)) {
449 IMonikerProp *prop;
451 hres = IPersistMoniker_QueryInterface(persist, &IID_IMonikerProp, (void**)&prop);
452 if(SUCCEEDED(hres)) {
453 IMonikerProp_PutProperty(prop, MIMETYPEPROP, binding->mime);
454 IMonikerProp_PutProperty(prop, CLASSIDPROP, clsid_str);
455 IMonikerProp_Release(prop);
458 load_doc_mon(binding, persist);
460 IPersistMoniker_Release(persist);
461 }else {
462 FIXME("Could not get IPersistMoniker: %08x\n", hres);
463 /* FIXME: Try query IPersistFile */
466 heap_free(clsid_str);
468 IBindStatusCallback_OnObjectAvailable(binding->callback, &binding->iid, binding->obj);
470 IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_ENDSYNCOPERATION, NULL);
472 stop_binding(binding, S_OK, NULL);
475 #define STGMEDUNK_THIS(iface) DEFINE_THIS(stgmed_buf_t, Unknown, iface)
477 static HRESULT WINAPI StgMedUnk_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
479 stgmed_buf_t *This = STGMEDUNK_THIS(iface);
481 *ppv = NULL;
483 if(IsEqualGUID(riid, &IID_IUnknown)) {
484 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
486 *ppv = STGMEDUNK(This);
487 IUnknown_AddRef(STGMEDUNK(This));
488 return S_OK;
491 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
492 return E_NOINTERFACE;
495 static ULONG WINAPI StgMedUnk_AddRef(IUnknown *iface)
497 stgmed_buf_t *This = STGMEDUNK_THIS(iface);
498 LONG ref = InterlockedIncrement(&This->ref);
500 TRACE("(%p) ref=%d\n", This, ref);
502 return ref;
505 static ULONG WINAPI StgMedUnk_Release(IUnknown *iface)
507 stgmed_buf_t *This = STGMEDUNK_THIS(iface);
508 LONG ref = InterlockedDecrement(&This->ref);
510 TRACE("(%p) ref=%d\n", This, ref);
512 if(!ref) {
513 IInternetProtocol_Release(This->protocol);
514 heap_free(This);
516 URLMON_UnlockModule();
519 return ref;
522 #undef STGMEDUNK_THIS
524 static const IUnknownVtbl StgMedUnkVtbl = {
525 StgMedUnk_QueryInterface,
526 StgMedUnk_AddRef,
527 StgMedUnk_Release
530 #define STREAM_THIS(iface) DEFINE_THIS(ProtocolStream, Stream, iface)
532 static HRESULT WINAPI ProtocolStream_QueryInterface(IStream *iface,
533 REFIID riid, void **ppv)
535 ProtocolStream *This = STREAM_THIS(iface);
537 *ppv = NULL;
539 if(IsEqualGUID(&IID_IUnknown, riid)) {
540 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
541 *ppv = STREAM(This);
542 }else if(IsEqualGUID(&IID_ISequentialStream, riid)) {
543 TRACE("(%p)->(IID_ISequentialStream %p)\n", This, ppv);
544 *ppv = STREAM(This);
545 }else if(IsEqualGUID(&IID_IStream, riid)) {
546 TRACE("(%p)->(IID_IStream %p)\n", This, ppv);
547 *ppv = STREAM(This);
550 if(*ppv) {
551 IStream_AddRef(STREAM(This));
552 return S_OK;
555 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
556 return E_NOINTERFACE;
559 static ULONG WINAPI ProtocolStream_AddRef(IStream *iface)
561 ProtocolStream *This = STREAM_THIS(iface);
562 LONG ref = InterlockedIncrement(&This->ref);
564 TRACE("(%p) ref=%d\n", This, ref);
566 return ref;
569 static ULONG WINAPI ProtocolStream_Release(IStream *iface)
571 ProtocolStream *This = STREAM_THIS(iface);
572 LONG ref = InterlockedDecrement(&This->ref);
574 TRACE("(%p) ref=%d\n", This, ref);
576 if(!ref) {
577 IUnknown_Release(STGMEDUNK(This->buf));
578 heap_free(This);
580 URLMON_UnlockModule();
583 return ref;
586 static HRESULT WINAPI ProtocolStream_Read(IStream *iface, void *pv,
587 ULONG cb, ULONG *pcbRead)
589 ProtocolStream *This = STREAM_THIS(iface);
590 DWORD read = 0, pread = 0;
591 HRESULT hres;
593 TRACE("(%p)->(%p %d %p)\n", This, pv, cb, pcbRead);
595 if(This->buf->size) {
596 read = cb;
598 if(read > This->buf->size)
599 read = This->buf->size;
601 memcpy(pv, This->buf->buf, read);
603 if(read < This->buf->size)
604 memmove(This->buf->buf, This->buf->buf+read, This->buf->size-read);
605 This->buf->size -= read;
608 if(read == cb) {
609 if (pcbRead)
610 *pcbRead = read;
611 return S_OK;
614 hres = This->buf->hres = IInternetProtocol_Read(This->buf->protocol, (PBYTE)pv+read, cb-read, &pread);
615 if (pcbRead)
616 *pcbRead = read + pread;
618 if(hres == E_PENDING)
619 return E_PENDING;
620 else if(FAILED(hres))
621 FIXME("Read failed: %08x\n", hres);
623 return read || pread ? S_OK : S_FALSE;
626 static HRESULT WINAPI ProtocolStream_Write(IStream *iface, const void *pv,
627 ULONG cb, ULONG *pcbWritten)
629 ProtocolStream *This = STREAM_THIS(iface);
631 TRACE("(%p)->(%p %d %p)\n", This, pv, cb, pcbWritten);
633 return STG_E_ACCESSDENIED;
636 static HRESULT WINAPI ProtocolStream_Seek(IStream *iface, LARGE_INTEGER dlibMove,
637 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
639 ProtocolStream *This = STREAM_THIS(iface);
640 FIXME("(%p)->(%d %08x %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
641 return E_NOTIMPL;
644 static HRESULT WINAPI ProtocolStream_SetSize(IStream *iface, ULARGE_INTEGER libNewSize)
646 ProtocolStream *This = STREAM_THIS(iface);
647 FIXME("(%p)->(%d)\n", This, libNewSize.u.LowPart);
648 return E_NOTIMPL;
651 static HRESULT WINAPI ProtocolStream_CopyTo(IStream *iface, IStream *pstm,
652 ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten)
654 ProtocolStream *This = STREAM_THIS(iface);
655 FIXME("(%p)->(%p %d %p %p)\n", This, pstm, cb.u.LowPart, pcbRead, pcbWritten);
656 return E_NOTIMPL;
659 static HRESULT WINAPI ProtocolStream_Commit(IStream *iface, DWORD grfCommitFlags)
661 ProtocolStream *This = STREAM_THIS(iface);
663 TRACE("(%p)->(%08x)\n", This, grfCommitFlags);
665 return E_NOTIMPL;
668 static HRESULT WINAPI ProtocolStream_Revert(IStream *iface)
670 ProtocolStream *This = STREAM_THIS(iface);
672 TRACE("(%p)\n", This);
674 return E_NOTIMPL;
677 static HRESULT WINAPI ProtocolStream_LockRegion(IStream *iface, ULARGE_INTEGER libOffset,
678 ULARGE_INTEGER cb, DWORD dwLockType)
680 ProtocolStream *This = STREAM_THIS(iface);
681 FIXME("(%p)->(%d %d %d)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
682 return E_NOTIMPL;
685 static HRESULT WINAPI ProtocolStream_UnlockRegion(IStream *iface,
686 ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
688 ProtocolStream *This = STREAM_THIS(iface);
689 FIXME("(%p)->(%d %d %d)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
690 return E_NOTIMPL;
693 static HRESULT WINAPI ProtocolStream_Stat(IStream *iface, STATSTG *pstatstg,
694 DWORD dwStatFlag)
696 ProtocolStream *This = STREAM_THIS(iface);
697 FIXME("(%p)->(%p %08x)\n", This, pstatstg, dwStatFlag);
698 return E_NOTIMPL;
701 static HRESULT WINAPI ProtocolStream_Clone(IStream *iface, IStream **ppstm)
703 ProtocolStream *This = STREAM_THIS(iface);
704 FIXME("(%p)->(%p)\n", This, ppstm);
705 return E_NOTIMPL;
708 #undef STREAM_THIS
710 static const IStreamVtbl ProtocolStreamVtbl = {
711 ProtocolStream_QueryInterface,
712 ProtocolStream_AddRef,
713 ProtocolStream_Release,
714 ProtocolStream_Read,
715 ProtocolStream_Write,
716 ProtocolStream_Seek,
717 ProtocolStream_SetSize,
718 ProtocolStream_CopyTo,
719 ProtocolStream_Commit,
720 ProtocolStream_Revert,
721 ProtocolStream_LockRegion,
722 ProtocolStream_UnlockRegion,
723 ProtocolStream_Stat,
724 ProtocolStream_Clone
727 #define BINDING_THIS(iface) DEFINE_THIS(Binding, Binding, iface)
729 static stgmed_buf_t *create_stgmed_buf(IInternetProtocol *protocol)
731 stgmed_buf_t *ret = heap_alloc(sizeof(*ret));
733 ret->lpUnknownVtbl = &StgMedUnkVtbl;
734 ret->ref = 1;
735 ret->size = 0;
736 ret->init = FALSE;
737 ret->hres = S_OK;
739 IInternetProtocol_AddRef(protocol);
740 ret->protocol = protocol;
742 URLMON_LockModule();
744 return ret;
747 static ProtocolStream *create_stream(stgmed_buf_t *buf)
749 ProtocolStream *ret = heap_alloc(sizeof(ProtocolStream));
751 ret->lpStreamVtbl = &ProtocolStreamVtbl;
752 ret->ref = 1;
754 IUnknown_AddRef(STGMEDUNK(buf));
755 ret->buf = buf;
757 URLMON_LockModule();
759 return ret;
762 static HRESULT WINAPI Binding_QueryInterface(IBinding *iface, REFIID riid, void **ppv)
764 Binding *This = BINDING_THIS(iface);
766 *ppv = NULL;
768 if(IsEqualGUID(&IID_IUnknown, riid)) {
769 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
770 *ppv = BINDING(This);
771 }else if(IsEqualGUID(&IID_IBinding, riid)) {
772 TRACE("(%p)->(IID_IBinding %p)\n", This, ppv);
773 *ppv = BINDING(This);
774 }else if(IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
775 TRACE("(%p)->(IID_IInternetProtocolSink %p)\n", This, ppv);
776 *ppv = PROTSINK(This);
777 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
778 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
779 *ppv = BINDINF(This);
780 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
781 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
782 *ppv = SERVPROV(This);
785 if(*ppv) {
786 IBinding_AddRef(BINDING(This));
787 return S_OK;
790 WARN("Unsupported interface %s\n", debugstr_guid(riid));
791 return E_NOINTERFACE;
794 static ULONG WINAPI Binding_AddRef(IBinding *iface)
796 Binding *This = BINDING_THIS(iface);
797 LONG ref = InterlockedIncrement(&This->ref);
799 TRACE("(%p) ref=%d\n", This, ref);
801 return ref;
804 static ULONG WINAPI Binding_Release(IBinding *iface)
806 Binding *This = BINDING_THIS(iface);
807 LONG ref = InterlockedDecrement(&This->ref);
809 TRACE("(%p) ref=%d\n", This, ref);
811 if(!ref) {
812 if (This->notif_hwnd)
813 DestroyWindow( This->notif_hwnd );
814 if(This->mon)
815 IMoniker_Release(This->mon);
816 if(This->callback)
817 IBindStatusCallback_Release(This->callback);
818 if(This->protocol)
819 IInternetProtocol_Release(This->protocol);
820 if(This->service_provider)
821 IServiceProvider_Release(This->service_provider);
822 if(This->stgmed_buf)
823 IUnknown_Release(STGMEDUNK(This->stgmed_buf));
824 if(This->stream)
825 IStream_Release(STREAM(This->stream));
826 if(This->obj)
827 IUnknown_Release(This->obj);
828 if(This->bctx)
829 IBindCtx_Release(This->bctx);
831 ReleaseBindInfo(&This->bindinfo);
832 This->section.DebugInfo->Spare[0] = 0;
833 DeleteCriticalSection(&This->section);
834 heap_free(This->mime);
835 heap_free(This->url);
837 heap_free(This);
839 URLMON_UnlockModule();
842 return ref;
845 static HRESULT WINAPI Binding_Abort(IBinding *iface)
847 Binding *This = BINDING_THIS(iface);
848 FIXME("(%p)\n", This);
849 return E_NOTIMPL;
852 static HRESULT WINAPI Binding_Suspend(IBinding *iface)
854 Binding *This = BINDING_THIS(iface);
855 FIXME("(%p)\n", This);
856 return E_NOTIMPL;
859 static HRESULT WINAPI Binding_Resume(IBinding *iface)
861 Binding *This = BINDING_THIS(iface);
862 FIXME("(%p)\n", This);
863 return E_NOTIMPL;
866 static HRESULT WINAPI Binding_SetPriority(IBinding *iface, LONG nPriority)
868 Binding *This = BINDING_THIS(iface);
869 FIXME("(%p)->(%d)\n", This, nPriority);
870 return E_NOTIMPL;
873 static HRESULT WINAPI Binding_GetPriority(IBinding *iface, LONG *pnPriority)
875 Binding *This = BINDING_THIS(iface);
876 FIXME("(%p)->(%p)\n", This, pnPriority);
877 return E_NOTIMPL;
880 static HRESULT WINAPI Binding_GetBindResult(IBinding *iface, CLSID *pclsidProtocol,
881 DWORD *pdwResult, LPOLESTR *pszResult, DWORD *pdwReserved)
883 Binding *This = BINDING_THIS(iface);
884 FIXME("(%p)->(%p %p %p %p)\n", This, pclsidProtocol, pdwResult, pszResult, pdwReserved);
885 return E_NOTIMPL;
888 static Binding *get_bctx_binding(IBindCtx *bctx)
890 IBinding *binding;
891 IUnknown *unk;
892 HRESULT hres;
894 hres = IBindCtx_GetObjectParam(bctx, cbinding_contextW, &unk);
895 if(FAILED(hres))
896 return NULL;
898 hres = IUnknown_QueryInterface(unk, &IID_IBinding, (void*)&binding);
899 IUnknown_Release(unk);
900 if(FAILED(hres))
901 return NULL;
903 /* FIXME!!! */
904 return BINDING_THIS(binding);
907 #undef BINDING_THIS
909 static const IBindingVtbl BindingVtbl = {
910 Binding_QueryInterface,
911 Binding_AddRef,
912 Binding_Release,
913 Binding_Abort,
914 Binding_Suspend,
915 Binding_Resume,
916 Binding_SetPriority,
917 Binding_GetPriority,
918 Binding_GetBindResult
921 #define PROTSINK_THIS(iface) DEFINE_THIS(Binding, InternetProtocolSink, iface)
923 static HRESULT WINAPI InternetProtocolSink_QueryInterface(IInternetProtocolSink *iface,
924 REFIID riid, void **ppv)
926 Binding *This = PROTSINK_THIS(iface);
927 return IBinding_QueryInterface(BINDING(This), riid, ppv);
930 static ULONG WINAPI InternetProtocolSink_AddRef(IInternetProtocolSink *iface)
932 Binding *This = PROTSINK_THIS(iface);
933 return IBinding_AddRef(BINDING(This));
936 static ULONG WINAPI InternetProtocolSink_Release(IInternetProtocolSink *iface)
938 Binding *This = PROTSINK_THIS(iface);
939 return IBinding_Release(BINDING(This));
942 typedef struct {
943 task_header_t header;
944 PROTOCOLDATA data;
945 } switch_task_t;
947 static void switch_proc(Binding *binding, task_header_t *t)
949 switch_task_t *task = (switch_task_t*)t;
951 IInternetProtocol_Continue(binding->protocol, &task->data);
953 heap_free(task);
956 static HRESULT WINAPI InternetProtocolSink_Switch(IInternetProtocolSink *iface,
957 PROTOCOLDATA *pProtocolData)
959 Binding *This = PROTSINK_THIS(iface);
960 switch_task_t *task;
962 TRACE("(%p)->(%p)\n", This, pProtocolData);
964 task = heap_alloc(sizeof(switch_task_t));
965 memcpy(&task->data, pProtocolData, sizeof(PROTOCOLDATA));
967 push_task(This, &task->header, switch_proc);
969 IBinding_AddRef(BINDING(This));
970 PostMessageW(This->notif_hwnd, WM_MK_CONTINUE, 0, (LPARAM)This);
972 return S_OK;
975 typedef struct {
976 task_header_t header;
978 Binding *binding;
979 ULONG progress;
980 ULONG progress_max;
981 ULONG status_code;
982 LPWSTR status_text;
983 } on_progress_task_t;
985 static void on_progress_proc(Binding *binding, task_header_t *t)
987 on_progress_task_t *task = (on_progress_task_t*)t;
989 IBindStatusCallback_OnProgress(binding->callback, task->progress,
990 task->progress_max, task->status_code, task->status_text);
992 heap_free(task->status_text);
993 heap_free(task);
996 static void on_progress(Binding *This, ULONG progress, ULONG progress_max,
997 ULONG status_code, LPCWSTR status_text)
999 on_progress_task_t *task;
1001 if(GetCurrentThreadId() == This->apartment_thread && !This->continue_call) {
1002 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1003 status_code, status_text);
1004 return;
1007 task = heap_alloc(sizeof(on_progress_task_t));
1009 task->progress = progress;
1010 task->progress_max = progress_max;
1011 task->status_code = status_code;
1013 if(status_text) {
1014 DWORD size = (strlenW(status_text)+1)*sizeof(WCHAR);
1016 task->status_text = heap_alloc(size);
1017 memcpy(task->status_text, status_text, size);
1018 }else {
1019 task->status_text = NULL;
1022 push_task(This, &task->header, on_progress_proc);
1024 if(GetCurrentThreadId() != This->apartment_thread) {
1025 IBinding_AddRef(BINDING(This));
1026 PostMessageW(This->notif_hwnd, WM_MK_CONTINUE, 0, (LPARAM)This);
1030 static HRESULT WINAPI InternetProtocolSink_ReportProgress(IInternetProtocolSink *iface,
1031 ULONG ulStatusCode, LPCWSTR szStatusText)
1033 Binding *This = PROTSINK_THIS(iface);
1035 TRACE("(%p)->(%u %s)\n", This, ulStatusCode, debugstr_w(szStatusText));
1037 switch(ulStatusCode) {
1038 case BINDSTATUS_FINDINGRESOURCE:
1039 on_progress(This, 0, 0, BINDSTATUS_FINDINGRESOURCE, szStatusText);
1040 break;
1041 case BINDSTATUS_CONNECTING:
1042 on_progress(This, 0, 0, BINDSTATUS_CONNECTING, szStatusText);
1043 break;
1044 case BINDSTATUS_BEGINDOWNLOADDATA:
1045 fill_stgmed_buffer(This->stgmed_buf);
1046 break;
1047 case BINDSTATUS_MIMETYPEAVAILABLE:
1048 set_binding_mime(This, szStatusText);
1049 break;
1050 case BINDSTATUS_SENDINGREQUEST:
1051 on_progress(This, 0, 0, BINDSTATUS_SENDINGREQUEST, szStatusText);
1052 break;
1053 case BINDSTATUS_PROTOCOLCLASSID:
1054 break;
1055 case BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE:
1056 mime_available(This, szStatusText, FALSE);
1057 break;
1058 case BINDSTATUS_CACHEFILENAMEAVAILABLE:
1059 break;
1060 case BINDSTATUS_DIRECTBIND:
1061 This->report_mime = FALSE;
1062 break;
1063 default:
1064 FIXME("Unhandled status code %d\n", ulStatusCode);
1065 return E_NOTIMPL;
1068 return S_OK;
1071 static void report_data(Binding *This, DWORD bscf, ULONG progress, ULONG progress_max)
1073 FORMATETC formatetc = {0, NULL, 1, -1, TYMED_ISTREAM};
1074 BOOL sent_begindownloaddata = FALSE;
1076 TRACE("(%p)->(%d %u %u)\n", This, bscf, progress, progress_max);
1078 if(This->download_state == END_DOWNLOAD)
1079 return;
1081 if(GetCurrentThreadId() != This->apartment_thread)
1082 FIXME("called from worker thread\n");
1084 if(This->report_mime)
1085 mime_available(This, NULL, TRUE);
1087 if(This->download_state == BEFORE_DOWNLOAD) {
1088 fill_stgmed_buffer(This->stgmed_buf);
1090 This->download_state = DOWNLOADING;
1091 sent_begindownloaddata = TRUE;
1092 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1093 BINDSTATUS_BEGINDOWNLOADDATA, This->url);
1096 if(This->stgmed_buf->hres == S_FALSE || (bscf & BSCF_LASTDATANOTIFICATION)) {
1097 This->download_state = END_DOWNLOAD;
1098 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1099 BINDSTATUS_ENDDOWNLOADDATA, This->url);
1100 }else if(!sent_begindownloaddata) {
1101 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1102 BINDSTATUS_DOWNLOADINGDATA, This->url);
1105 if(This->to_object) {
1106 if(!(This->state & BINDING_OBJAVAIL))
1107 create_object(This);
1108 }else {
1109 if(!(This->state & BINDING_LOCKED)) {
1110 HRESULT hres = IInternetProtocol_LockRequest(This->protocol, 0);
1111 if(SUCCEEDED(hres))
1112 This->state |= BINDING_LOCKED;
1115 formatetc.cfFormat = This->clipboard_format;
1116 IBindStatusCallback_OnDataAvailable(This->callback, bscf, progress,
1117 &formatetc, &This->stgmed);
1119 if(This->download_state == END_DOWNLOAD)
1120 stop_binding(This, S_OK, NULL);
1124 typedef struct {
1125 task_header_t header;
1126 DWORD bscf;
1127 ULONG progress;
1128 ULONG progress_max;
1129 } report_data_task_t;
1131 static void report_data_proc(Binding *binding, task_header_t *t)
1133 report_data_task_t *task = (report_data_task_t*)t;
1135 report_data(binding, task->bscf, task->progress, task->progress_max);
1137 heap_free(task);
1140 static HRESULT WINAPI InternetProtocolSink_ReportData(IInternetProtocolSink *iface,
1141 DWORD grfBSCF, ULONG ulProgress, ULONG ulProgressMax)
1143 Binding *This = PROTSINK_THIS(iface);
1145 TRACE("(%p)->(%d %u %u)\n", This, grfBSCF, ulProgress, ulProgressMax);
1147 if(GetCurrentThreadId() != This->apartment_thread)
1148 FIXME("called from worked hread\n");
1150 if(This->continue_call) {
1151 report_data_task_t *task = heap_alloc(sizeof(report_data_task_t));
1152 task->bscf = grfBSCF;
1153 task->progress = ulProgress;
1154 task->progress_max = ulProgressMax;
1156 push_task(This, &task->header, report_data_proc);
1157 }else {
1158 report_data(This, grfBSCF, ulProgress, ulProgressMax);
1161 return S_OK;
1164 typedef struct {
1165 task_header_t header;
1167 HRESULT hres;
1168 LPWSTR str;
1169 } report_result_task_t;
1171 static void report_result_proc(Binding *binding, task_header_t *t)
1173 report_result_task_t *task = (report_result_task_t*)t;
1175 IInternetProtocol_Terminate(binding->protocol, 0);
1177 stop_binding(binding, task->hres, task->str);
1179 heap_free(task->str);
1180 heap_free(task);
1183 static HRESULT WINAPI InternetProtocolSink_ReportResult(IInternetProtocolSink *iface,
1184 HRESULT hrResult, DWORD dwError, LPCWSTR szResult)
1186 Binding *This = PROTSINK_THIS(iface);
1188 TRACE("(%p)->(%08x %d %s)\n", This, hrResult, dwError, debugstr_w(szResult));
1190 if(GetCurrentThreadId() == This->apartment_thread && !This->continue_call) {
1191 IInternetProtocol_Terminate(This->protocol, 0);
1192 stop_binding(This, hrResult, szResult);
1193 }else {
1194 report_result_task_t *task = heap_alloc(sizeof(report_result_task_t));
1196 task->hres = hrResult;
1197 task->str = heap_strdupW(szResult);
1199 push_task(This, &task->header, report_result_proc);
1202 return S_OK;
1205 #undef PROTSINK_THIS
1207 static const IInternetProtocolSinkVtbl InternetProtocolSinkVtbl = {
1208 InternetProtocolSink_QueryInterface,
1209 InternetProtocolSink_AddRef,
1210 InternetProtocolSink_Release,
1211 InternetProtocolSink_Switch,
1212 InternetProtocolSink_ReportProgress,
1213 InternetProtocolSink_ReportData,
1214 InternetProtocolSink_ReportResult
1217 #define BINDINF_THIS(iface) DEFINE_THIS(Binding, InternetBindInfo, iface)
1219 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
1220 REFIID riid, void **ppv)
1222 Binding *This = BINDINF_THIS(iface);
1223 return IBinding_QueryInterface(BINDING(This), riid, ppv);
1226 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
1228 Binding *This = BINDINF_THIS(iface);
1229 return IBinding_AddRef(BINDING(This));
1232 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
1234 Binding *This = BINDINF_THIS(iface);
1235 return IBinding_Release(BINDING(This));
1238 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
1239 DWORD *grfBINDF, BINDINFO *pbindinfo)
1241 Binding *This = BINDINF_THIS(iface);
1243 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
1245 *grfBINDF = This->bindf;
1247 memcpy(pbindinfo, &This->bindinfo, sizeof(BINDINFO));
1249 if(pbindinfo->szExtraInfo || pbindinfo->szCustomVerb)
1250 FIXME("copy strings\n");
1252 if(pbindinfo->stgmedData.pUnkForRelease)
1253 IUnknown_AddRef(pbindinfo->stgmedData.pUnkForRelease);
1255 if(pbindinfo->pUnk)
1256 IUnknown_AddRef(pbindinfo->pUnk);
1258 return S_OK;
1261 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
1262 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
1264 Binding *This = BINDINF_THIS(iface);
1266 TRACE("(%p)->(%d %p %d %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
1268 switch(ulStringType) {
1269 case BINDSTRING_ACCEPT_MIMES: {
1270 static const WCHAR wszMimes[] = {'*','/','*',0};
1272 if(!ppwzStr || !pcElFetched)
1273 return E_INVALIDARG;
1275 ppwzStr[0] = CoTaskMemAlloc(sizeof(wszMimes));
1276 memcpy(ppwzStr[0], wszMimes, sizeof(wszMimes));
1277 *pcElFetched = 1;
1278 return S_OK;
1280 case BINDSTRING_USER_AGENT: {
1281 IInternetBindInfo *bindinfo = NULL;
1282 HRESULT hres;
1284 hres = IBindStatusCallback_QueryInterface(This->callback, &IID_IInternetBindInfo,
1285 (void**)&bindinfo);
1286 if(FAILED(hres))
1287 return hres;
1289 hres = IInternetBindInfo_GetBindString(bindinfo, ulStringType, ppwzStr,
1290 cEl, pcElFetched);
1291 IInternetBindInfo_Release(bindinfo);
1293 return hres;
1297 FIXME("not supported string type %d\n", ulStringType);
1298 return E_NOTIMPL;
1301 #undef BINDF_THIS
1303 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
1304 InternetBindInfo_QueryInterface,
1305 InternetBindInfo_AddRef,
1306 InternetBindInfo_Release,
1307 InternetBindInfo_GetBindInfo,
1308 InternetBindInfo_GetBindString
1311 #define SERVPROV_THIS(iface) DEFINE_THIS(Binding, ServiceProvider, iface)
1313 static HRESULT WINAPI ServiceProvider_QueryInterface(IServiceProvider *iface,
1314 REFIID riid, void **ppv)
1316 Binding *This = SERVPROV_THIS(iface);
1317 return IBinding_QueryInterface(BINDING(This), riid, ppv);
1320 static ULONG WINAPI ServiceProvider_AddRef(IServiceProvider *iface)
1322 Binding *This = SERVPROV_THIS(iface);
1323 return IBinding_AddRef(BINDING(This));
1326 static ULONG WINAPI ServiceProvider_Release(IServiceProvider *iface)
1328 Binding *This = SERVPROV_THIS(iface);
1329 return IBinding_Release(BINDING(This));
1332 static HRESULT WINAPI ServiceProvider_QueryService(IServiceProvider *iface,
1333 REFGUID guidService, REFIID riid, void **ppv)
1335 Binding *This = SERVPROV_THIS(iface);
1336 HRESULT hres;
1338 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
1340 if(This->service_provider) {
1341 hres = IServiceProvider_QueryService(This->service_provider, guidService,
1342 riid, ppv);
1343 if(SUCCEEDED(hres))
1344 return hres;
1347 WARN("unknown service %s\n", debugstr_guid(guidService));
1348 return E_NOTIMPL;
1351 #undef SERVPROV_THIS
1353 static const IServiceProviderVtbl ServiceProviderVtbl = {
1354 ServiceProvider_QueryInterface,
1355 ServiceProvider_AddRef,
1356 ServiceProvider_Release,
1357 ServiceProvider_QueryService
1360 static HRESULT get_callback(IBindCtx *pbc, IBindStatusCallback **callback)
1362 IUnknown *unk;
1363 HRESULT hres;
1365 hres = IBindCtx_GetObjectParam(pbc, bscb_holderW, &unk);
1366 if(SUCCEEDED(hres)) {
1367 hres = IUnknown_QueryInterface(unk, &IID_IBindStatusCallback, (void**)callback);
1368 IUnknown_Release(unk);
1371 return SUCCEEDED(hres) ? S_OK : INET_E_DATA_NOT_AVAILABLE;
1374 static BOOL is_urlmon_protocol(LPCWSTR url)
1376 static const WCHAR wszCdl[] = {'c','d','l'};
1377 static const WCHAR wszFile[] = {'f','i','l','e'};
1378 static const WCHAR wszFtp[] = {'f','t','p'};
1379 static const WCHAR wszGopher[] = {'g','o','p','h','e','r'};
1380 static const WCHAR wszHttp[] = {'h','t','t','p'};
1381 static const WCHAR wszHttps[] = {'h','t','t','p','s'};
1382 static const WCHAR wszMk[] = {'m','k'};
1384 static const struct {
1385 LPCWSTR scheme;
1386 int len;
1387 } protocol_list[] = {
1388 {wszCdl, sizeof(wszCdl) /sizeof(WCHAR)},
1389 {wszFile, sizeof(wszFile) /sizeof(WCHAR)},
1390 {wszFtp, sizeof(wszFtp) /sizeof(WCHAR)},
1391 {wszGopher, sizeof(wszGopher)/sizeof(WCHAR)},
1392 {wszHttp, sizeof(wszHttp) /sizeof(WCHAR)},
1393 {wszHttps, sizeof(wszHttps) /sizeof(WCHAR)},
1394 {wszMk, sizeof(wszMk) /sizeof(WCHAR)}
1397 int i, len = strlenW(url);
1399 for(i=0; i < sizeof(protocol_list)/sizeof(protocol_list[0]); i++) {
1400 if(len >= protocol_list[i].len
1401 && !memcmp(url, protocol_list[i].scheme, protocol_list[i].len*sizeof(WCHAR)))
1402 return TRUE;
1405 return FALSE;
1408 static HRESULT Binding_Create(IMoniker *mon, Binding *binding_ctx, LPCWSTR url, IBindCtx *pbc,
1409 BOOL to_obj, REFIID riid, Binding **binding)
1411 Binding *ret;
1412 HRESULT hres;
1414 if(!to_obj && !IsEqualGUID(&IID_IStream, riid)) {
1415 FIXME("Unsupported riid %s\n", debugstr_guid(riid));
1416 return E_NOTIMPL;
1419 URLMON_LockModule();
1421 ret = heap_alloc_zero(sizeof(Binding));
1423 ret->lpBindingVtbl = &BindingVtbl;
1424 ret->lpInternetProtocolSinkVtbl = &InternetProtocolSinkVtbl;
1425 ret->lpInternetBindInfoVtbl = &InternetBindInfoVtbl;
1426 ret->lpServiceProviderVtbl = &ServiceProviderVtbl;
1428 ret->ref = 1;
1430 ret->to_object = to_obj;
1431 ret->iid = *riid;
1432 ret->apartment_thread = GetCurrentThreadId();
1433 ret->notif_hwnd = get_notif_hwnd();
1434 ret->report_mime = !binding_ctx;
1435 ret->download_state = BEFORE_DOWNLOAD;
1437 if(to_obj) {
1438 IBindCtx_AddRef(pbc);
1439 ret->bctx = pbc;
1442 if(mon) {
1443 IMoniker_AddRef(mon);
1444 ret->mon = mon;
1447 ret->bindinfo.cbSize = sizeof(BINDINFO);
1449 InitializeCriticalSection(&ret->section);
1450 ret->section.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": Binding.section");
1452 hres = get_callback(pbc, &ret->callback);
1453 if(FAILED(hres)) {
1454 WARN("Could not get IBindStatusCallback\n");
1455 IBinding_Release(BINDING(ret));
1456 return hres;
1459 IBindStatusCallback_QueryInterface(ret->callback, &IID_IServiceProvider,
1460 (void**)&ret->service_provider);
1462 if(binding_ctx) {
1463 ret->protocol = binding_ctx->protocol;
1464 IInternetProtocol_AddRef(ret->protocol);
1465 }else {
1466 hres = create_binding_protocol(url, TRUE, &ret->protocol);
1467 if(FAILED(hres)) {
1468 WARN("Could not get protocol handler\n");
1469 IBinding_Release(BINDING(ret));
1470 return hres;
1474 hres = IBindStatusCallback_GetBindInfo(ret->callback, &ret->bindf, &ret->bindinfo);
1475 if(FAILED(hres)) {
1476 WARN("GetBindInfo failed: %08x\n", hres);
1477 IBinding_Release(BINDING(ret));
1478 return hres;
1481 TRACE("bindf %08x\n", ret->bindf);
1482 dump_BINDINFO(&ret->bindinfo);
1484 ret->bindf |= BINDF_FROMURLMON;
1485 if(to_obj)
1486 ret->bindinfo.dwOptions |= 0x100000;
1488 if(!is_urlmon_protocol(url))
1489 ret->bindf |= BINDF_NEEDFILE;
1491 ret->url = heap_strdupW(url);
1493 if(binding_ctx) {
1494 ret->stgmed_buf = binding_ctx->stgmed_buf;
1495 IUnknown_AddRef(STGMEDUNK(ret->stgmed_buf));
1496 ret->stream = binding_ctx->stream;
1497 IStream_AddRef(STREAM(binding_ctx->stream));
1498 ret->clipboard_format = binding_ctx->clipboard_format;
1499 }else {
1500 ret->stgmed_buf = create_stgmed_buf(ret->protocol);
1501 ret->stream = create_stream(ret->stgmed_buf);
1503 ret->stgmed.tymed = TYMED_ISTREAM;
1504 ret->stgmed.u.pstm = STREAM((ProtocolStream*)ret->stream);
1505 ret->stgmed.pUnkForRelease = (IUnknown*)BINDING(ret); /* NOTE: Windows uses other IUnknown */
1507 *binding = ret;
1508 return S_OK;
1511 static HRESULT start_binding(IMoniker *mon, Binding *binding_ctx, LPCWSTR url, IBindCtx *pbc,
1512 BOOL to_obj, REFIID riid, Binding **ret)
1514 Binding *binding = NULL;
1515 HRESULT hres;
1516 MSG msg;
1518 hres = Binding_Create(mon, binding_ctx, url, pbc, to_obj, riid, &binding);
1519 if(FAILED(hres))
1520 return hres;
1522 hres = IBindStatusCallback_OnStartBinding(binding->callback, 0, BINDING(binding));
1523 if(FAILED(hres)) {
1524 WARN("OnStartBinding failed: %08x\n", hres);
1525 stop_binding(binding, 0x800c0008, NULL);
1526 IBinding_Release(BINDING(binding));
1527 return hres;
1530 if(binding_ctx) {
1531 set_binding_sink(binding->protocol, PROTSINK(binding));
1532 report_data(binding, 0, 0, 0);
1533 }else {
1534 hres = IInternetProtocol_Start(binding->protocol, url, PROTSINK(binding),
1535 BINDINF(binding), 0, 0);
1537 TRACE("start ret %08x\n", hres);
1539 if(FAILED(hres)) {
1540 stop_binding(binding, hres, NULL);
1541 IBinding_Release(BINDING(binding));
1543 return hres;
1547 while(!(binding->bindf & BINDF_ASYNCHRONOUS) &&
1548 !(binding->state & BINDING_STOPPED)) {
1549 MsgWaitForMultipleObjects(0, NULL, FALSE, 5000, QS_POSTMESSAGE);
1550 while (PeekMessageW(&msg, binding->notif_hwnd, WM_USER, WM_USER+117, PM_REMOVE|PM_NOYIELD)) {
1551 TranslateMessage(&msg);
1552 DispatchMessageW(&msg);
1556 *ret = binding;
1557 return S_OK;
1560 HRESULT bind_to_storage(LPCWSTR url, IBindCtx *pbc, REFIID riid, void **ppv)
1562 Binding *binding = NULL, *binding_ctx;
1563 HRESULT hres;
1565 *ppv = NULL;
1567 binding_ctx = get_bctx_binding(pbc);
1569 hres = start_binding(NULL, binding_ctx, url, pbc, FALSE, riid, &binding);
1570 if(binding_ctx)
1571 IBinding_Release(BINDING(binding_ctx));
1572 if(FAILED(hres))
1573 return hres;
1575 if(binding->hres == S_OK && binding->stgmed_buf->init) {
1576 if((binding->state & BINDING_STOPPED) && (binding->state & BINDING_LOCKED))
1577 IInternetProtocol_UnlockRequest(binding->protocol);
1579 *ppv = binding->stgmed.u.pstm;
1580 IStream_AddRef(binding->stgmed.u.pstm);
1582 hres = S_OK;
1583 }else {
1584 hres = MK_S_ASYNCHRONOUS;
1587 IBinding_Release(BINDING(binding));
1589 return hres;
1592 HRESULT bind_to_object(IMoniker *mon, LPCWSTR url, IBindCtx *pbc, REFIID riid, void **ppv)
1594 Binding *binding;
1595 HRESULT hres;
1597 *ppv = NULL;
1599 hres = start_binding(mon, NULL, url, pbc, TRUE, riid, &binding);
1600 if(FAILED(hres))
1601 return hres;
1603 if(binding->hres != S_OK) {
1604 hres = SUCCEEDED(binding->hres) ? S_OK : binding->hres;
1605 }else if(binding->bindf & BINDF_ASYNCHRONOUS) {
1606 hres = MK_S_ASYNCHRONOUS;
1607 }else {
1608 *ppv = binding->obj;
1609 IUnknown_AddRef(binding->obj);
1610 hres = S_OK;
1613 IBinding_Release(BINDING(binding));
1615 return hres;