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"
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 {
37 struct _task_header_t *next;
41 const IUnknownVtbl *lpUnknownVtbl;
45 IInternetProtocol *protocol;
55 typedef struct _stgmed_obj_t stgmed_obj_t;
58 void (*release)(stgmed_obj_t*);
59 HRESULT (*fill_stgmed)(stgmed_obj_t*,STGMEDIUM*);
60 void *(*get_result)(stgmed_obj_t*);
63 struct _stgmed_obj_t {
64 const stgmed_obj_vtbl *vtbl;
67 #define STGMEDUNK(x) ((IUnknown*) &(x)->lpUnknownVtbl)
75 #define BINDING_LOCKED 0x0001
76 #define BINDING_STOPPED 0x0002
77 #define BINDING_OBJAVAIL 0x0004
80 const IBindingVtbl *lpBindingVtbl;
81 const IInternetProtocolSinkVtbl *lpInternetProtocolSinkVtbl;
82 const IInternetBindInfoVtbl *lpInternetBindInfoVtbl;
83 const IWinInetHttpInfoVtbl *lpWinInetHttpInfoVtbl;
84 const IServiceProviderVtbl *lpServiceProviderVtbl;
88 IBindStatusCallback *callback;
89 IInternetProtocol *protocol;
90 IServiceProvider *service_provider;
92 stgmed_buf_t *stgmed_buf;
93 stgmed_obj_t *stgmed_obj;
99 UINT clipboard_format;
106 download_state_t download_state;
111 DWORD apartment_thread;
114 task_header_t *task_queue_head, *task_queue_tail;
115 CRITICAL_SECTION section;
118 #define BINDING(x) ((IBinding*) &(x)->lpBindingVtbl)
119 #define PROTSINK(x) ((IInternetProtocolSink*) &(x)->lpInternetProtocolSinkVtbl)
120 #define BINDINF(x) ((IInternetBindInfo*) &(x)->lpInternetBindInfoVtbl)
121 #define INETINFO(x) ((IWinInetHttpInfo*) &(x)->lpWinInetHttpInfoVtbl)
122 #define SERVPROV(x) ((IServiceProvider*) &(x)->lpServiceProviderVtbl)
124 #define STREAM(x) ((IStream*) &(x)->lpStreamVtbl)
125 #define HTTPNEG2(x) ((IHttpNegotiate2*) &(x)->lpHttpNegotiate2Vtbl)
127 #define WM_MK_CONTINUE (WM_USER+101)
128 #define WM_MK_RELEASE (WM_USER+102)
130 static void push_task(Binding *binding, task_header_t *task, task_proc_t proc)
135 EnterCriticalSection(&binding->section);
137 if(binding->task_queue_tail) {
138 binding->task_queue_tail->next = task;
139 binding->task_queue_tail = task;
141 binding->task_queue_tail = binding->task_queue_head = task;
144 LeaveCriticalSection(&binding->section);
147 static task_header_t *pop_task(Binding *binding)
151 EnterCriticalSection(&binding->section);
153 ret = binding->task_queue_head;
155 binding->task_queue_head = ret->next;
156 if(!binding->task_queue_head)
157 binding->task_queue_tail = NULL;
160 LeaveCriticalSection(&binding->section);
165 static void fill_stgmed_buffer(stgmed_buf_t *buf)
169 if(sizeof(buf->buf) == buf->size)
172 buf->hres = IInternetProtocol_Read(buf->protocol, buf->buf+buf->size,
173 sizeof(buf->buf)-buf->size, &read);
179 static LRESULT WINAPI notif_wnd_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
182 case WM_MK_CONTINUE: {
183 Binding *binding = (Binding*)lParam;
186 while((task = pop_task(binding))) {
187 binding->continue_call++;
188 task->proc(binding, task);
189 binding->continue_call--;
192 IBinding_Release(BINDING(binding));
195 case WM_MK_RELEASE: {
196 tls_data_t *data = get_tls_data();
198 if(!--data->notif_hwnd_cnt) {
200 data->notif_hwnd = NULL;
205 return DefWindowProcW(hwnd, msg, wParam, lParam);
208 static HWND get_notif_hwnd(void)
210 static ATOM wnd_class = 0;
211 tls_data_t *tls_data;
213 static const WCHAR wszURLMonikerNotificationWindow[] =
214 {'U','R','L',' ','M','o','n','i','k','e','r',' ',
215 'N','o','t','i','f','i','c','a','t','i','o','n',' ','W','i','n','d','o','w',0};
217 tls_data = get_tls_data();
221 if(tls_data->notif_hwnd_cnt) {
222 tls_data->notif_hwnd_cnt++;
223 return tls_data->notif_hwnd;
227 static WNDCLASSEXW wndclass = {
229 notif_wnd_proc, 0, 0,
230 NULL, NULL, NULL, NULL, NULL,
231 wszURLMonikerNotificationWindow,
235 wndclass.hInstance = URLMON_hInstance;
237 wnd_class = RegisterClassExW(&wndclass);
238 if (!wnd_class && GetLastError() == ERROR_CLASS_ALREADY_EXISTS)
242 tls_data->notif_hwnd = CreateWindowExW(0, wszURLMonikerNotificationWindow,
243 wszURLMonikerNotificationWindow, 0, 0, 0, 0, 0, HWND_MESSAGE,
244 NULL, URLMON_hInstance, NULL);
245 if(tls_data->notif_hwnd)
246 tls_data->notif_hwnd_cnt++;
248 TRACE("hwnd = %p\n", tls_data->notif_hwnd);
250 return tls_data->notif_hwnd;
253 static void release_notif_hwnd(HWND hwnd)
255 tls_data_t *data = get_tls_data();
257 if(!data || data->notif_hwnd != hwnd) {
258 PostMessageW(data->notif_hwnd, WM_MK_RELEASE, 0, 0);
262 if(!--data->notif_hwnd_cnt) {
263 DestroyWindow(data->notif_hwnd);
264 data->notif_hwnd = NULL;
268 static void dump_BINDINFO(BINDINFO *bi)
270 static const char * const BINDINFOF_str[] = {
272 "BINDINFOF_URLENCODESTGMEDDATA",
273 "BINDINFOF_URLENCODEDEXTRAINFO"
276 static const char * const BINDVERB_str[] = {
290 " %d, %08x, %d, %d\n"
296 bi->cbSize, debugstr_w(bi->szExtraInfo),
297 bi->stgmedData.tymed, bi->stgmedData.u.hGlobal, bi->stgmedData.pUnkForRelease,
298 bi->grfBindInfoF > BINDINFOF_URLENCODEDEXTRAINFO
299 ? "unknown" : BINDINFOF_str[bi->grfBindInfoF],
300 bi->dwBindVerb > BINDVERB_CUSTOM
301 ? "unknown" : BINDVERB_str[bi->dwBindVerb],
302 debugstr_w(bi->szCustomVerb),
303 bi->cbstgmedData, bi->dwOptions, bi->dwOptionsFlags, bi->dwCodePage,
304 bi->securityAttributes.nLength,
305 bi->securityAttributes.lpSecurityDescriptor,
306 bi->securityAttributes.bInheritHandle,
307 debugstr_guid(&bi->iid),
308 bi->pUnk, bi->dwReserved
312 static void set_binding_mime(Binding *binding, LPCWSTR mime)
314 EnterCriticalSection(&binding->section);
316 if(binding->report_mime) {
317 heap_free(binding->mime);
318 binding->mime = heap_strdupW(mime);
321 LeaveCriticalSection(&binding->section);
324 static void handle_mime_available(Binding *binding, BOOL verify)
328 EnterCriticalSection(&binding->section);
329 report_mime = binding->report_mime;
330 binding->report_mime = FALSE;
331 LeaveCriticalSection(&binding->section);
339 fill_stgmed_buffer(binding->stgmed_buf);
340 FindMimeFromData(NULL, binding->url, binding->stgmed_buf->buf,
341 min(binding->stgmed_buf->size, 255), binding->mime, 0, &mime, 0);
343 heap_free(binding->mime);
344 binding->mime = heap_strdupW(mime);
348 IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_MIMETYPEAVAILABLE, binding->mime);
350 binding->clipboard_format = RegisterClipboardFormatW(binding->mime);
354 task_header_t header;
356 } mime_available_task_t;
358 static void mime_available_proc(Binding *binding, task_header_t *t)
360 mime_available_task_t *task = (mime_available_task_t*)t;
362 handle_mime_available(binding, task->verify);
367 static void mime_available(Binding *This, LPCWSTR mime, BOOL verify)
370 set_binding_mime(This, mime);
372 if(GetCurrentThreadId() == This->apartment_thread) {
373 handle_mime_available(This, verify);
375 mime_available_task_t *task = heap_alloc(sizeof(task_header_t));
376 task->verify = verify;
377 push_task(This, &task->header, mime_available_proc);
381 static void stop_binding(Binding *binding, HRESULT hres, LPCWSTR str)
383 if(binding->state & BINDING_LOCKED) {
384 IInternetProtocol_UnlockRequest(binding->protocol);
385 binding->state &= ~BINDING_LOCKED;
388 if(!(binding->state & BINDING_STOPPED)) {
389 binding->state |= BINDING_STOPPED;
391 IBindStatusCallback_OnStopBinding(binding->callback, hres, str);
392 binding->hres = hres;
396 static LPWSTR get_mime_clsid(LPCWSTR mime, CLSID *clsid)
398 LPWSTR key_name, ret;
399 DWORD res, type, size;
404 static const WCHAR mime_keyW[] =
405 {'M','I','M','E','\\','D','a','t','a','b','a','s','e','\\',
406 'C','o','n','t','e','n','t',' ','T','y','p','e','\\'};
407 static const WCHAR clsidW[] = {'C','L','S','I','D',0};
409 len = strlenW(mime)+1;
410 key_name = heap_alloc(sizeof(mime_keyW) + len*sizeof(WCHAR));
411 memcpy(key_name, mime_keyW, sizeof(mime_keyW));
412 strcpyW(key_name + sizeof(mime_keyW)/sizeof(WCHAR), mime);
414 res = RegOpenKeyW(HKEY_CLASSES_ROOT, key_name, &hkey);
416 if(res != ERROR_SUCCESS) {
417 WARN("Could not open MIME key: %x\n", res);
421 size = 50*sizeof(WCHAR);
422 ret = heap_alloc(size);
423 res = RegQueryValueExW(hkey, clsidW, NULL, &type, (LPBYTE)ret, &size);
425 if(res != ERROR_SUCCESS) {
426 WARN("Could not get CLSID: %08x\n", res);
431 hres = CLSIDFromString(ret, clsid);
433 WARN("Could not parse CLSID: %08x\n", hres);
441 static void load_doc_mon(Binding *binding, IPersistMoniker *persist)
446 hres = CreateAsyncBindCtxEx(binding->bctx, 0, NULL, NULL, &bctx, 0);
448 WARN("CreateAsyncBindCtxEx failed: %08x\n", hres);
452 IBindCtx_RevokeObjectParam(bctx, bscb_holderW);
453 IBindCtx_RegisterObjectParam(bctx, cbinding_contextW, (IUnknown*)BINDING(binding));
455 hres = IPersistMoniker_Load(persist, binding->download_state == END_DOWNLOAD, binding->mon, bctx, 0x12);
456 IBindCtx_RevokeObjectParam(bctx, cbinding_contextW);
457 IBindCtx_Release(bctx);
459 FIXME("Load failed: %08x\n", hres);
462 static HRESULT create_mime_object(Binding *binding, const CLSID *clsid, LPCWSTR clsid_str)
464 IPersistMoniker *persist;
467 hres = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
468 &binding->iid, (void**)&binding->obj);
470 WARN("CoCreateInstance failed: %08x\n", hres);
471 return INET_E_CANNOT_INSTANTIATE_OBJECT;
474 binding->state |= BINDING_OBJAVAIL;
476 hres = IUnknown_QueryInterface(binding->obj, &IID_IPersistMoniker, (void**)&persist);
477 if(SUCCEEDED(hres)) {
480 hres = IPersistMoniker_QueryInterface(persist, &IID_IMonikerProp, (void**)&prop);
481 if(SUCCEEDED(hres)) {
482 IMonikerProp_PutProperty(prop, MIMETYPEPROP, binding->mime);
483 IMonikerProp_PutProperty(prop, CLASSIDPROP, clsid_str);
484 IMonikerProp_Release(prop);
487 load_doc_mon(binding, persist);
489 IPersistMoniker_Release(persist);
491 FIXME("Could not get IPersistMoniker: %08x\n", hres);
492 /* FIXME: Try query IPersistFile */
495 IBindStatusCallback_OnObjectAvailable(binding->callback, &binding->iid, binding->obj);
500 static void create_object(Binding *binding)
507 FIXME("MIME not available\n");
511 if(!(clsid_str = get_mime_clsid(binding->mime, &clsid))) {
512 FIXME("Could not find object for MIME %s\n", debugstr_w(binding->mime));
516 IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_CLASSIDAVAILABLE, clsid_str);
517 IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_BEGINSYNCOPERATION, NULL);
519 hres = create_mime_object(binding, &clsid, clsid_str);
520 heap_free(clsid_str);
522 IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_ENDSYNCOPERATION, NULL);
524 stop_binding(binding, hres, NULL);
526 IInternetProtocol_Terminate(binding->protocol, 0);
529 #define STGMEDUNK_THIS(iface) DEFINE_THIS(stgmed_buf_t, Unknown, iface)
531 static HRESULT WINAPI StgMedUnk_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
533 stgmed_buf_t *This = STGMEDUNK_THIS(iface);
537 if(IsEqualGUID(riid, &IID_IUnknown)) {
538 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
540 *ppv = STGMEDUNK(This);
541 IUnknown_AddRef(STGMEDUNK(This));
545 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
546 return E_NOINTERFACE;
549 static ULONG WINAPI StgMedUnk_AddRef(IUnknown *iface)
551 stgmed_buf_t *This = STGMEDUNK_THIS(iface);
552 LONG ref = InterlockedIncrement(&This->ref);
554 TRACE("(%p) ref=%d\n", This, ref);
559 static ULONG WINAPI StgMedUnk_Release(IUnknown *iface)
561 stgmed_buf_t *This = STGMEDUNK_THIS(iface);
562 LONG ref = InterlockedDecrement(&This->ref);
564 TRACE("(%p) ref=%d\n", This, ref);
567 IInternetProtocol_Release(This->protocol);
568 heap_free(This->cache_file);
571 URLMON_UnlockModule();
577 #undef STGMEDUNK_THIS
579 static const IUnknownVtbl StgMedUnkVtbl = {
580 StgMedUnk_QueryInterface,
585 static stgmed_buf_t *create_stgmed_buf(IInternetProtocol *protocol)
587 stgmed_buf_t *ret = heap_alloc(sizeof(*ret));
589 ret->lpUnknownVtbl = &StgMedUnkVtbl;
594 ret->cache_file = NULL;
596 IInternetProtocol_AddRef(protocol);
597 ret->protocol = protocol;
605 stgmed_obj_t stgmed_obj;
606 const IStreamVtbl *lpStreamVtbl;
613 #define STREAM_THIS(iface) DEFINE_THIS(ProtocolStream, Stream, iface)
615 static HRESULT WINAPI ProtocolStream_QueryInterface(IStream *iface,
616 REFIID riid, void **ppv)
618 ProtocolStream *This = STREAM_THIS(iface);
622 if(IsEqualGUID(&IID_IUnknown, riid)) {
623 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
625 }else if(IsEqualGUID(&IID_ISequentialStream, riid)) {
626 TRACE("(%p)->(IID_ISequentialStream %p)\n", This, ppv);
628 }else if(IsEqualGUID(&IID_IStream, riid)) {
629 TRACE("(%p)->(IID_IStream %p)\n", This, ppv);
634 IStream_AddRef(STREAM(This));
638 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
639 return E_NOINTERFACE;
642 static ULONG WINAPI ProtocolStream_AddRef(IStream *iface)
644 ProtocolStream *This = STREAM_THIS(iface);
645 LONG ref = InterlockedIncrement(&This->ref);
647 TRACE("(%p) ref=%d\n", This, ref);
652 static ULONG WINAPI ProtocolStream_Release(IStream *iface)
654 ProtocolStream *This = STREAM_THIS(iface);
655 LONG ref = InterlockedDecrement(&This->ref);
657 TRACE("(%p) ref=%d\n", This, ref);
660 IUnknown_Release(STGMEDUNK(This->buf));
663 URLMON_UnlockModule();
669 static HRESULT WINAPI ProtocolStream_Read(IStream *iface, void *pv,
670 ULONG cb, ULONG *pcbRead)
672 ProtocolStream *This = STREAM_THIS(iface);
673 DWORD read = 0, pread = 0;
676 TRACE("(%p)->(%p %d %p)\n", This, pv, cb, pcbRead);
678 if(This->buf->size) {
681 if(read > This->buf->size)
682 read = This->buf->size;
684 memcpy(pv, This->buf->buf, read);
686 if(read < This->buf->size)
687 memmove(This->buf->buf, This->buf->buf+read, This->buf->size-read);
688 This->buf->size -= read;
697 hres = This->buf->hres = IInternetProtocol_Read(This->buf->protocol, (PBYTE)pv+read, cb-read, &pread);
699 *pcbRead = read + pread;
701 if(hres == E_PENDING)
703 else if(FAILED(hres))
704 FIXME("Read failed: %08x\n", hres);
706 return read || pread ? S_OK : S_FALSE;
709 static HRESULT WINAPI ProtocolStream_Write(IStream *iface, const void *pv,
710 ULONG cb, ULONG *pcbWritten)
712 ProtocolStream *This = STREAM_THIS(iface);
714 TRACE("(%p)->(%p %d %p)\n", This, pv, cb, pcbWritten);
716 return STG_E_ACCESSDENIED;
719 static HRESULT WINAPI ProtocolStream_Seek(IStream *iface, LARGE_INTEGER dlibMove,
720 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
722 ProtocolStream *This = STREAM_THIS(iface);
723 FIXME("(%p)->(%d %08x %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
727 static HRESULT WINAPI ProtocolStream_SetSize(IStream *iface, ULARGE_INTEGER libNewSize)
729 ProtocolStream *This = STREAM_THIS(iface);
730 FIXME("(%p)->(%d)\n", This, libNewSize.u.LowPart);
734 static HRESULT WINAPI ProtocolStream_CopyTo(IStream *iface, IStream *pstm,
735 ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten)
737 ProtocolStream *This = STREAM_THIS(iface);
738 FIXME("(%p)->(%p %d %p %p)\n", This, pstm, cb.u.LowPart, pcbRead, pcbWritten);
742 static HRESULT WINAPI ProtocolStream_Commit(IStream *iface, DWORD grfCommitFlags)
744 ProtocolStream *This = STREAM_THIS(iface);
746 TRACE("(%p)->(%08x)\n", This, grfCommitFlags);
751 static HRESULT WINAPI ProtocolStream_Revert(IStream *iface)
753 ProtocolStream *This = STREAM_THIS(iface);
755 TRACE("(%p)\n", This);
760 static HRESULT WINAPI ProtocolStream_LockRegion(IStream *iface, ULARGE_INTEGER libOffset,
761 ULARGE_INTEGER cb, DWORD dwLockType)
763 ProtocolStream *This = STREAM_THIS(iface);
764 FIXME("(%p)->(%d %d %d)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
768 static HRESULT WINAPI ProtocolStream_UnlockRegion(IStream *iface,
769 ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
771 ProtocolStream *This = STREAM_THIS(iface);
772 FIXME("(%p)->(%d %d %d)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
776 static HRESULT WINAPI ProtocolStream_Stat(IStream *iface, STATSTG *pstatstg,
779 ProtocolStream *This = STREAM_THIS(iface);
780 FIXME("(%p)->(%p %08x)\n", This, pstatstg, dwStatFlag);
784 static HRESULT WINAPI ProtocolStream_Clone(IStream *iface, IStream **ppstm)
786 ProtocolStream *This = STREAM_THIS(iface);
787 FIXME("(%p)->(%p)\n", This, ppstm);
793 static const IStreamVtbl ProtocolStreamVtbl = {
794 ProtocolStream_QueryInterface,
795 ProtocolStream_AddRef,
796 ProtocolStream_Release,
798 ProtocolStream_Write,
800 ProtocolStream_SetSize,
801 ProtocolStream_CopyTo,
802 ProtocolStream_Commit,
803 ProtocolStream_Revert,
804 ProtocolStream_LockRegion,
805 ProtocolStream_UnlockRegion,
810 static void stgmed_stream_release(stgmed_obj_t *obj)
812 ProtocolStream *stream = (ProtocolStream*)obj;
813 IStream_Release(STREAM(stream));
816 static HRESULT stgmed_stream_fill_stgmed(stgmed_obj_t *obj, STGMEDIUM *stgmed)
818 ProtocolStream *stream = (ProtocolStream*)obj;
820 stgmed->tymed = TYMED_ISTREAM;
821 stgmed->u.pstm = STREAM(stream);
822 stgmed->pUnkForRelease = STGMEDUNK(stream->buf);
827 static void *stgmed_stream_get_result(stgmed_obj_t *obj)
829 ProtocolStream *stream = (ProtocolStream*)obj;
831 IStream_AddRef(STREAM(stream));
832 return STREAM(stream);
835 static const stgmed_obj_vtbl stgmed_stream_vtbl = {
836 stgmed_stream_release,
837 stgmed_stream_fill_stgmed,
838 stgmed_stream_get_result
842 stgmed_obj_t stgmed_obj;
846 static stgmed_obj_t *create_stgmed_stream(stgmed_buf_t *buf)
848 ProtocolStream *ret = heap_alloc(sizeof(ProtocolStream));
850 ret->stgmed_obj.vtbl = &stgmed_stream_vtbl;
851 ret->lpStreamVtbl = &ProtocolStreamVtbl;
854 IUnknown_AddRef(STGMEDUNK(buf));
859 return &ret->stgmed_obj;
862 static void stgmed_file_release(stgmed_obj_t *obj)
864 stgmed_file_obj_t *file_obj = (stgmed_file_obj_t*)obj;
866 IUnknown_Release(STGMEDUNK(file_obj->buf));
870 static HRESULT stgmed_file_fill_stgmed(stgmed_obj_t *obj, STGMEDIUM *stgmed)
872 stgmed_file_obj_t *file_obj = (stgmed_file_obj_t*)obj;
874 if(!file_obj->buf->cache_file) {
875 WARN("cache_file not set\n");
876 return INET_E_DATA_NOT_AVAILABLE;
879 fill_stgmed_buffer(file_obj->buf);
880 if(file_obj->buf->size == sizeof(file_obj->buf->buf)) {
886 hres = IInternetProtocol_Read(file_obj->buf->protocol, buf, sizeof(buf), &read);
887 }while(hres == S_OK);
890 stgmed->tymed = TYMED_FILE;
891 stgmed->u.lpszFileName = file_obj->buf->cache_file;
892 stgmed->pUnkForRelease = STGMEDUNK(file_obj->buf);
897 static void *stgmed_file_get_result(stgmed_obj_t *obj)
902 static const stgmed_obj_vtbl stgmed_file_vtbl = {
904 stgmed_file_fill_stgmed,
905 stgmed_file_get_result
908 static stgmed_obj_t *create_stgmed_file(stgmed_buf_t *buf)
910 stgmed_file_obj_t *ret = heap_alloc(sizeof(*ret));
912 ret->stgmed_obj.vtbl = &stgmed_file_vtbl;
914 IUnknown_AddRef(STGMEDUNK(buf));
917 return &ret->stgmed_obj;
920 #define BINDING_THIS(iface) DEFINE_THIS(Binding, Binding, iface)
922 static HRESULT WINAPI Binding_QueryInterface(IBinding *iface, REFIID riid, void **ppv)
924 Binding *This = BINDING_THIS(iface);
928 if(IsEqualGUID(&IID_IUnknown, riid)) {
929 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
930 *ppv = BINDING(This);
931 }else if(IsEqualGUID(&IID_IBinding, riid)) {
932 TRACE("(%p)->(IID_IBinding %p)\n", This, ppv);
933 *ppv = BINDING(This);
934 }else if(IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
935 TRACE("(%p)->(IID_IInternetProtocolSink %p)\n", This, ppv);
936 *ppv = PROTSINK(This);
937 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
938 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
939 *ppv = BINDINF(This);
940 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
941 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
942 *ppv = SERVPROV(This);
943 }else if(IsEqualGUID(&IID_IWinInetInfo, riid)) {
944 TRACE("(%p)->(IID_IWinInetInfo %p)\n", This, ppv);
946 /* NOTE: This violidates COM rules, but tests prove that we should do it */
947 if(!get_wininet_info(This->protocol))
948 return E_NOINTERFACE;
950 *ppv = INETINFO(This);
951 }else if(IsEqualGUID(&IID_IWinInetHttpInfo, riid)) {
952 IWinInetHttpInfo *http_info;
956 TRACE("(%p)->(IID_IWinInetHttpInfo %p)\n", This, ppv);
958 info = get_wininet_info(This->protocol);
960 return E_NOINTERFACE;
962 hres = IWinInetInfo_QueryInterface(info, &IID_IWinInetHttpInfo, (void**)&http_info);
964 return E_NOINTERFACE;
966 IWinInetHttpInfo_Release(http_info);
967 *ppv = INETINFO(This);
971 IBinding_AddRef(BINDING(This));
975 WARN("Unsupported interface %s\n", debugstr_guid(riid));
976 return E_NOINTERFACE;
979 static ULONG WINAPI Binding_AddRef(IBinding *iface)
981 Binding *This = BINDING_THIS(iface);
982 LONG ref = InterlockedIncrement(&This->ref);
984 TRACE("(%p) ref=%d\n", This, ref);
989 static ULONG WINAPI Binding_Release(IBinding *iface)
991 Binding *This = BINDING_THIS(iface);
992 LONG ref = InterlockedDecrement(&This->ref);
994 TRACE("(%p) ref=%d\n", This, ref);
998 release_notif_hwnd(This->notif_hwnd);
1000 IMoniker_Release(This->mon);
1002 IBindStatusCallback_Release(This->callback);
1004 IInternetProtocol_Release(This->protocol);
1005 if(This->service_provider)
1006 IServiceProvider_Release(This->service_provider);
1007 if(This->stgmed_buf)
1008 IUnknown_Release(STGMEDUNK(This->stgmed_buf));
1009 if(This->stgmed_obj)
1010 This->stgmed_obj->vtbl->release(This->stgmed_obj);
1012 IUnknown_Release(This->obj);
1014 IBindCtx_Release(This->bctx);
1016 ReleaseBindInfo(&This->bindinfo);
1017 This->section.DebugInfo->Spare[0] = 0;
1018 DeleteCriticalSection(&This->section);
1019 heap_free(This->mime);
1020 heap_free(This->url);
1024 URLMON_UnlockModule();
1030 static HRESULT WINAPI Binding_Abort(IBinding *iface)
1032 Binding *This = BINDING_THIS(iface);
1033 FIXME("(%p)\n", This);
1037 static HRESULT WINAPI Binding_Suspend(IBinding *iface)
1039 Binding *This = BINDING_THIS(iface);
1040 FIXME("(%p)\n", This);
1044 static HRESULT WINAPI Binding_Resume(IBinding *iface)
1046 Binding *This = BINDING_THIS(iface);
1047 FIXME("(%p)\n", This);
1051 static HRESULT WINAPI Binding_SetPriority(IBinding *iface, LONG nPriority)
1053 Binding *This = BINDING_THIS(iface);
1054 FIXME("(%p)->(%d)\n", This, nPriority);
1058 static HRESULT WINAPI Binding_GetPriority(IBinding *iface, LONG *pnPriority)
1060 Binding *This = BINDING_THIS(iface);
1061 FIXME("(%p)->(%p)\n", This, pnPriority);
1065 static HRESULT WINAPI Binding_GetBindResult(IBinding *iface, CLSID *pclsidProtocol,
1066 DWORD *pdwResult, LPOLESTR *pszResult, DWORD *pdwReserved)
1068 Binding *This = BINDING_THIS(iface);
1069 FIXME("(%p)->(%p %p %p %p)\n", This, pclsidProtocol, pdwResult, pszResult, pdwReserved);
1073 static Binding *get_bctx_binding(IBindCtx *bctx)
1079 hres = IBindCtx_GetObjectParam(bctx, cbinding_contextW, &unk);
1083 hres = IUnknown_QueryInterface(unk, &IID_IBinding, (void*)&binding);
1084 IUnknown_Release(unk);
1089 return BINDING_THIS(binding);
1094 static const IBindingVtbl BindingVtbl = {
1095 Binding_QueryInterface,
1101 Binding_SetPriority,
1102 Binding_GetPriority,
1103 Binding_GetBindResult
1106 #define PROTSINK_THIS(iface) DEFINE_THIS(Binding, InternetProtocolSink, iface)
1108 static HRESULT WINAPI InternetProtocolSink_QueryInterface(IInternetProtocolSink *iface,
1109 REFIID riid, void **ppv)
1111 Binding *This = PROTSINK_THIS(iface);
1112 return IBinding_QueryInterface(BINDING(This), riid, ppv);
1115 static ULONG WINAPI InternetProtocolSink_AddRef(IInternetProtocolSink *iface)
1117 Binding *This = PROTSINK_THIS(iface);
1118 return IBinding_AddRef(BINDING(This));
1121 static ULONG WINAPI InternetProtocolSink_Release(IInternetProtocolSink *iface)
1123 Binding *This = PROTSINK_THIS(iface);
1124 return IBinding_Release(BINDING(This));
1128 task_header_t header;
1132 static void switch_proc(Binding *binding, task_header_t *t)
1134 switch_task_t *task = (switch_task_t*)t;
1136 IInternetProtocol_Continue(binding->protocol, &task->data);
1141 static HRESULT WINAPI InternetProtocolSink_Switch(IInternetProtocolSink *iface,
1142 PROTOCOLDATA *pProtocolData)
1144 Binding *This = PROTSINK_THIS(iface);
1145 switch_task_t *task;
1147 TRACE("(%p)->(%p)\n", This, pProtocolData);
1149 task = heap_alloc(sizeof(switch_task_t));
1150 task->data = *pProtocolData;
1152 push_task(This, &task->header, switch_proc);
1154 IBinding_AddRef(BINDING(This));
1155 PostMessageW(This->notif_hwnd, WM_MK_CONTINUE, 0, (LPARAM)This);
1161 task_header_t header;
1168 } on_progress_task_t;
1170 static void on_progress_proc(Binding *binding, task_header_t *t)
1172 on_progress_task_t *task = (on_progress_task_t*)t;
1174 IBindStatusCallback_OnProgress(binding->callback, task->progress,
1175 task->progress_max, task->status_code, task->status_text);
1177 heap_free(task->status_text);
1181 static void on_progress(Binding *This, ULONG progress, ULONG progress_max,
1182 ULONG status_code, LPCWSTR status_text)
1184 on_progress_task_t *task;
1186 if(GetCurrentThreadId() == This->apartment_thread && !This->continue_call) {
1187 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1188 status_code, status_text);
1192 task = heap_alloc(sizeof(on_progress_task_t));
1194 task->progress = progress;
1195 task->progress_max = progress_max;
1196 task->status_code = status_code;
1199 DWORD size = (strlenW(status_text)+1)*sizeof(WCHAR);
1201 task->status_text = heap_alloc(size);
1202 memcpy(task->status_text, status_text, size);
1204 task->status_text = NULL;
1207 push_task(This, &task->header, on_progress_proc);
1209 if(GetCurrentThreadId() != This->apartment_thread) {
1210 IBinding_AddRef(BINDING(This));
1211 PostMessageW(This->notif_hwnd, WM_MK_CONTINUE, 0, (LPARAM)This);
1215 static HRESULT WINAPI InternetProtocolSink_ReportProgress(IInternetProtocolSink *iface,
1216 ULONG ulStatusCode, LPCWSTR szStatusText)
1218 Binding *This = PROTSINK_THIS(iface);
1220 TRACE("(%p)->(%u %s)\n", This, ulStatusCode, debugstr_w(szStatusText));
1222 switch(ulStatusCode) {
1223 case BINDSTATUS_FINDINGRESOURCE:
1224 on_progress(This, 0, 0, BINDSTATUS_FINDINGRESOURCE, szStatusText);
1226 case BINDSTATUS_CONNECTING:
1227 on_progress(This, 0, 0, BINDSTATUS_CONNECTING, szStatusText);
1229 case BINDSTATUS_BEGINDOWNLOADDATA:
1230 fill_stgmed_buffer(This->stgmed_buf);
1232 case BINDSTATUS_MIMETYPEAVAILABLE:
1233 set_binding_mime(This, szStatusText);
1235 case BINDSTATUS_SENDINGREQUEST:
1236 on_progress(This, 0, 0, BINDSTATUS_SENDINGREQUEST, szStatusText);
1238 case BINDSTATUS_PROTOCOLCLASSID:
1240 case BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE:
1241 mime_available(This, szStatusText, FALSE);
1243 case BINDSTATUS_CACHEFILENAMEAVAILABLE:
1244 heap_free(This->stgmed_buf->cache_file);
1245 This->stgmed_buf->cache_file = heap_strdupW(szStatusText);
1247 case BINDSTATUS_DIRECTBIND:
1248 This->report_mime = FALSE;
1250 case BINDSTATUS_ACCEPTRANGES:
1253 FIXME("Unhandled status code %d\n", ulStatusCode);
1260 static void report_data(Binding *This, DWORD bscf, ULONG progress, ULONG progress_max)
1262 FORMATETC formatetc = {0, NULL, 1, -1, TYMED_ISTREAM};
1263 BOOL sent_begindownloaddata = FALSE;
1265 TRACE("(%p)->(%d %u %u)\n", This, bscf, progress, progress_max);
1267 if(This->download_state == END_DOWNLOAD || (This->state & BINDING_STOPPED))
1270 if(GetCurrentThreadId() != This->apartment_thread)
1271 FIXME("called from worker thread\n");
1273 if(This->report_mime)
1274 mime_available(This, NULL, TRUE);
1276 if(This->download_state == BEFORE_DOWNLOAD) {
1277 fill_stgmed_buffer(This->stgmed_buf);
1279 This->download_state = DOWNLOADING;
1280 sent_begindownloaddata = TRUE;
1281 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1282 BINDSTATUS_BEGINDOWNLOADDATA, This->url);
1284 if(This->stgmed_buf->cache_file)
1285 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1286 BINDSTATUS_CACHEFILENAMEAVAILABLE, This->stgmed_buf->cache_file);
1289 if(This->stgmed_buf->hres == S_FALSE || (bscf & BSCF_LASTDATANOTIFICATION)) {
1290 This->download_state = END_DOWNLOAD;
1291 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1292 BINDSTATUS_ENDDOWNLOADDATA, This->url);
1293 }else if(!sent_begindownloaddata) {
1294 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1295 BINDSTATUS_DOWNLOADINGDATA, This->url);
1298 if(This->to_object) {
1299 if(!(This->state & BINDING_OBJAVAIL))
1300 create_object(This);
1305 if(!(This->state & BINDING_LOCKED)) {
1306 HRESULT hres = IInternetProtocol_LockRequest(This->protocol, 0);
1308 This->state |= BINDING_LOCKED;
1311 hres = This->stgmed_obj->vtbl->fill_stgmed(This->stgmed_obj, &stgmed);
1313 stop_binding(This, hres, NULL);
1317 formatetc.tymed = stgmed.tymed;
1318 formatetc.cfFormat = This->clipboard_format;
1320 IBindStatusCallback_OnDataAvailable(This->callback, bscf, progress,
1321 &formatetc, &stgmed);
1323 if(This->download_state == END_DOWNLOAD)
1324 stop_binding(This, S_OK, NULL);
1329 task_header_t header;
1333 } report_data_task_t;
1335 static void report_data_proc(Binding *binding, task_header_t *t)
1337 report_data_task_t *task = (report_data_task_t*)t;
1339 report_data(binding, task->bscf, task->progress, task->progress_max);
1344 static HRESULT WINAPI InternetProtocolSink_ReportData(IInternetProtocolSink *iface,
1345 DWORD grfBSCF, ULONG ulProgress, ULONG ulProgressMax)
1347 Binding *This = PROTSINK_THIS(iface);
1349 TRACE("(%p)->(%d %u %u)\n", This, grfBSCF, ulProgress, ulProgressMax);
1351 if(GetCurrentThreadId() != This->apartment_thread)
1352 FIXME("called from worked hread\n");
1354 if(This->continue_call) {
1355 report_data_task_t *task = heap_alloc(sizeof(report_data_task_t));
1356 task->bscf = grfBSCF;
1357 task->progress = ulProgress;
1358 task->progress_max = ulProgressMax;
1360 push_task(This, &task->header, report_data_proc);
1362 report_data(This, grfBSCF, ulProgress, ulProgressMax);
1369 task_header_t header;
1373 } report_result_task_t;
1375 static void report_result_proc(Binding *binding, task_header_t *t)
1377 report_result_task_t *task = (report_result_task_t*)t;
1379 IInternetProtocol_Terminate(binding->protocol, 0);
1381 stop_binding(binding, task->hres, task->str);
1383 heap_free(task->str);
1387 static HRESULT WINAPI InternetProtocolSink_ReportResult(IInternetProtocolSink *iface,
1388 HRESULT hrResult, DWORD dwError, LPCWSTR szResult)
1390 Binding *This = PROTSINK_THIS(iface);
1392 TRACE("(%p)->(%08x %d %s)\n", This, hrResult, dwError, debugstr_w(szResult));
1394 if(GetCurrentThreadId() == This->apartment_thread && !This->continue_call) {
1395 IInternetProtocol_Terminate(This->protocol, 0);
1396 stop_binding(This, hrResult, szResult);
1398 report_result_task_t *task = heap_alloc(sizeof(report_result_task_t));
1400 task->hres = hrResult;
1401 task->str = heap_strdupW(szResult);
1403 push_task(This, &task->header, report_result_proc);
1409 #undef PROTSINK_THIS
1411 static const IInternetProtocolSinkVtbl InternetProtocolSinkVtbl = {
1412 InternetProtocolSink_QueryInterface,
1413 InternetProtocolSink_AddRef,
1414 InternetProtocolSink_Release,
1415 InternetProtocolSink_Switch,
1416 InternetProtocolSink_ReportProgress,
1417 InternetProtocolSink_ReportData,
1418 InternetProtocolSink_ReportResult
1421 #define BINDINF_THIS(iface) DEFINE_THIS(Binding, InternetBindInfo, iface)
1423 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
1424 REFIID riid, void **ppv)
1426 Binding *This = BINDINF_THIS(iface);
1427 return IBinding_QueryInterface(BINDING(This), riid, ppv);
1430 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
1432 Binding *This = BINDINF_THIS(iface);
1433 return IBinding_AddRef(BINDING(This));
1436 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
1438 Binding *This = BINDINF_THIS(iface);
1439 return IBinding_Release(BINDING(This));
1442 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
1443 DWORD *grfBINDF, BINDINFO *pbindinfo)
1445 Binding *This = BINDINF_THIS(iface);
1447 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
1449 *grfBINDF = This->bindf;
1451 *pbindinfo = This->bindinfo;
1453 if(pbindinfo->szExtraInfo || pbindinfo->szCustomVerb)
1454 FIXME("copy strings\n");
1456 if(pbindinfo->stgmedData.pUnkForRelease)
1457 IUnknown_AddRef(pbindinfo->stgmedData.pUnkForRelease);
1460 IUnknown_AddRef(pbindinfo->pUnk);
1465 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
1466 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
1468 Binding *This = BINDINF_THIS(iface);
1470 TRACE("(%p)->(%d %p %d %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
1472 switch(ulStringType) {
1473 case BINDSTRING_ACCEPT_MIMES: {
1474 static const WCHAR wszMimes[] = {'*','/','*',0};
1476 if(!ppwzStr || !pcElFetched)
1477 return E_INVALIDARG;
1479 ppwzStr[0] = CoTaskMemAlloc(sizeof(wszMimes));
1480 memcpy(ppwzStr[0], wszMimes, sizeof(wszMimes));
1484 case BINDSTRING_USER_AGENT: {
1485 IInternetBindInfo *bindinfo = NULL;
1488 hres = IBindStatusCallback_QueryInterface(This->callback, &IID_IInternetBindInfo,
1493 hres = IInternetBindInfo_GetBindString(bindinfo, ulStringType, ppwzStr,
1495 IInternetBindInfo_Release(bindinfo);
1501 FIXME("not supported string type %d\n", ulStringType);
1507 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
1508 InternetBindInfo_QueryInterface,
1509 InternetBindInfo_AddRef,
1510 InternetBindInfo_Release,
1511 InternetBindInfo_GetBindInfo,
1512 InternetBindInfo_GetBindString
1515 #define INETINFO_THIS(iface) DEFINE_THIS(Binding, WinInetHttpInfo, iface)
1517 static HRESULT WINAPI WinInetHttpInfo_QueryInterface(IWinInetHttpInfo *iface, REFIID riid, void **ppv)
1519 Binding *This = INETINFO_THIS(iface);
1520 return IBinding_QueryInterface(BINDING(This), riid, ppv);
1523 static ULONG WINAPI WinInetHttpInfo_AddRef(IWinInetHttpInfo *iface)
1525 Binding *This = INETINFO_THIS(iface);
1526 return IBinding_AddRef(BINDING(This));
1529 static ULONG WINAPI WinInetHttpInfo_Release(IWinInetHttpInfo *iface)
1531 Binding *This = INETINFO_THIS(iface);
1532 return IBinding_Release(BINDING(This));
1535 static HRESULT WINAPI WinInetHttpInfo_QueryOption(IWinInetHttpInfo *iface, DWORD dwOption,
1536 void *pBuffer, DWORD *pcbBuffer)
1538 Binding *This = INETINFO_THIS(iface);
1539 FIXME("(%p)->(%x %p %p)\n", This, dwOption, pBuffer, pcbBuffer);
1543 static HRESULT WINAPI WinInetHttpInfo_QueryInfo(IWinInetHttpInfo *iface, DWORD dwOption,
1544 void *pBuffer, DWORD *pcbBuffer, DWORD *pdwFlags, DWORD *pdwReserved)
1546 Binding *This = INETINFO_THIS(iface);
1547 FIXME("(%p)->(%x %p %p %p %p)\n", This, dwOption, pBuffer, pcbBuffer, pdwFlags, pdwReserved);
1551 #undef INETINFO_THIS
1553 static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl = {
1554 WinInetHttpInfo_QueryInterface,
1555 WinInetHttpInfo_AddRef,
1556 WinInetHttpInfo_Release,
1557 WinInetHttpInfo_QueryOption,
1558 WinInetHttpInfo_QueryInfo
1561 #define SERVPROV_THIS(iface) DEFINE_THIS(Binding, ServiceProvider, iface)
1563 static HRESULT WINAPI ServiceProvider_QueryInterface(IServiceProvider *iface,
1564 REFIID riid, void **ppv)
1566 Binding *This = SERVPROV_THIS(iface);
1567 return IBinding_QueryInterface(BINDING(This), riid, ppv);
1570 static ULONG WINAPI ServiceProvider_AddRef(IServiceProvider *iface)
1572 Binding *This = SERVPROV_THIS(iface);
1573 return IBinding_AddRef(BINDING(This));
1576 static ULONG WINAPI ServiceProvider_Release(IServiceProvider *iface)
1578 Binding *This = SERVPROV_THIS(iface);
1579 return IBinding_Release(BINDING(This));
1582 static HRESULT WINAPI ServiceProvider_QueryService(IServiceProvider *iface,
1583 REFGUID guidService, REFIID riid, void **ppv)
1585 Binding *This = SERVPROV_THIS(iface);
1588 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
1590 if(This->service_provider) {
1591 hres = IServiceProvider_QueryService(This->service_provider, guidService,
1597 WARN("unknown service %s\n", debugstr_guid(guidService));
1601 #undef SERVPROV_THIS
1603 static const IServiceProviderVtbl ServiceProviderVtbl = {
1604 ServiceProvider_QueryInterface,
1605 ServiceProvider_AddRef,
1606 ServiceProvider_Release,
1607 ServiceProvider_QueryService
1610 static HRESULT get_callback(IBindCtx *pbc, IBindStatusCallback **callback)
1615 hres = IBindCtx_GetObjectParam(pbc, bscb_holderW, &unk);
1616 if(SUCCEEDED(hres)) {
1617 hres = IUnknown_QueryInterface(unk, &IID_IBindStatusCallback, (void**)callback);
1618 IUnknown_Release(unk);
1621 return SUCCEEDED(hres) ? S_OK : INET_E_DATA_NOT_AVAILABLE;
1624 static BOOL is_urlmon_protocol(LPCWSTR url)
1626 static const WCHAR wszCdl[] = {'c','d','l'};
1627 static const WCHAR wszFile[] = {'f','i','l','e'};
1628 static const WCHAR wszFtp[] = {'f','t','p'};
1629 static const WCHAR wszGopher[] = {'g','o','p','h','e','r'};
1630 static const WCHAR wszHttp[] = {'h','t','t','p'};
1631 static const WCHAR wszHttps[] = {'h','t','t','p','s'};
1632 static const WCHAR wszMk[] = {'m','k'};
1634 static const struct {
1637 } protocol_list[] = {
1638 {wszCdl, sizeof(wszCdl) /sizeof(WCHAR)},
1639 {wszFile, sizeof(wszFile) /sizeof(WCHAR)},
1640 {wszFtp, sizeof(wszFtp) /sizeof(WCHAR)},
1641 {wszGopher, sizeof(wszGopher)/sizeof(WCHAR)},
1642 {wszHttp, sizeof(wszHttp) /sizeof(WCHAR)},
1643 {wszHttps, sizeof(wszHttps) /sizeof(WCHAR)},
1644 {wszMk, sizeof(wszMk) /sizeof(WCHAR)}
1648 int len = lstrlenW(url);
1650 for(i=0; i < sizeof(protocol_list)/sizeof(protocol_list[0]); i++) {
1651 if(len >= protocol_list[i].len
1652 && !memcmp(url, protocol_list[i].scheme, protocol_list[i].len*sizeof(WCHAR)))
1659 static HRESULT Binding_Create(IMoniker *mon, Binding *binding_ctx, LPCWSTR url, IBindCtx *pbc,
1660 BOOL to_obj, REFIID riid, Binding **binding)
1665 URLMON_LockModule();
1667 ret = heap_alloc_zero(sizeof(Binding));
1669 ret->lpBindingVtbl = &BindingVtbl;
1670 ret->lpInternetProtocolSinkVtbl = &InternetProtocolSinkVtbl;
1671 ret->lpInternetBindInfoVtbl = &InternetBindInfoVtbl;
1672 ret->lpWinInetHttpInfoVtbl = &WinInetHttpInfoVtbl;
1673 ret->lpServiceProviderVtbl = &ServiceProviderVtbl;
1677 ret->to_object = to_obj;
1679 ret->apartment_thread = GetCurrentThreadId();
1680 ret->notif_hwnd = get_notif_hwnd();
1681 ret->report_mime = !binding_ctx;
1682 ret->download_state = BEFORE_DOWNLOAD;
1685 IBindCtx_AddRef(pbc);
1690 IMoniker_AddRef(mon);
1694 ret->bindinfo.cbSize = sizeof(BINDINFO);
1696 InitializeCriticalSection(&ret->section);
1697 ret->section.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": Binding.section");
1699 hres = get_callback(pbc, &ret->callback);
1701 WARN("Could not get IBindStatusCallback\n");
1702 IBinding_Release(BINDING(ret));
1706 IBindStatusCallback_QueryInterface(ret->callback, &IID_IServiceProvider,
1707 (void**)&ret->service_provider);
1710 ret->protocol = binding_ctx->protocol;
1711 IInternetProtocol_AddRef(ret->protocol);
1713 hres = create_binding_protocol(url, TRUE, &ret->protocol);
1715 WARN("Could not get protocol handler\n");
1716 IBinding_Release(BINDING(ret));
1721 hres = IBindStatusCallback_GetBindInfo(ret->callback, &ret->bindf, &ret->bindinfo);
1723 WARN("GetBindInfo failed: %08x\n", hres);
1724 IBinding_Release(BINDING(ret));
1728 TRACE("bindf %08x\n", ret->bindf);
1729 dump_BINDINFO(&ret->bindinfo);
1731 ret->bindf |= BINDF_FROMURLMON;
1733 ret->bindinfo.dwOptions |= 0x100000;
1735 if(!is_urlmon_protocol(url))
1736 ret->bindf |= BINDF_NEEDFILE;
1738 ret->url = heap_strdupW(url);
1741 ret->stgmed_buf = binding_ctx->stgmed_buf;
1742 IUnknown_AddRef(STGMEDUNK(ret->stgmed_buf));
1743 ret->clipboard_format = binding_ctx->clipboard_format;
1745 ret->stgmed_buf = create_stgmed_buf(ret->protocol);
1749 ret->stgmed_obj = NULL;
1750 }else if(IsEqualGUID(&IID_IStream, riid)) {
1751 ret->stgmed_obj = create_stgmed_stream(ret->stgmed_buf);
1752 }else if(IsEqualGUID(&IID_IUnknown, riid)) {
1753 ret->bindf |= BINDF_NEEDFILE;
1754 ret->stgmed_obj = create_stgmed_file(ret->stgmed_buf);
1756 FIXME("Unsupported riid %s\n", debugstr_guid(riid));
1757 IBinding_Release(BINDING(ret));
1765 static HRESULT start_binding(IMoniker *mon, Binding *binding_ctx, LPCWSTR url, IBindCtx *pbc,
1766 BOOL to_obj, REFIID riid, Binding **ret)
1768 Binding *binding = NULL;
1772 hres = Binding_Create(mon, binding_ctx, url, pbc, to_obj, riid, &binding);
1776 hres = IBindStatusCallback_OnStartBinding(binding->callback, 0, BINDING(binding));
1778 WARN("OnStartBinding failed: %08x\n", hres);
1779 stop_binding(binding, INET_E_DOWNLOAD_FAILURE, NULL);
1780 IBinding_Release(BINDING(binding));
1785 set_binding_sink(binding->protocol, PROTSINK(binding));
1786 report_data(binding, 0, 0, 0);
1788 hres = IInternetProtocol_Start(binding->protocol, url, PROTSINK(binding),
1789 BINDINF(binding), 0, 0);
1791 TRACE("start ret %08x\n", hres);
1793 if(FAILED(hres) && hres != E_PENDING) {
1794 stop_binding(binding, hres, NULL);
1795 IBinding_Release(BINDING(binding));
1801 while(!(binding->bindf & BINDF_ASYNCHRONOUS) &&
1802 !(binding->state & BINDING_STOPPED)) {
1803 MsgWaitForMultipleObjects(0, NULL, FALSE, 5000, QS_POSTMESSAGE);
1804 while (PeekMessageW(&msg, binding->notif_hwnd, WM_USER, WM_USER+117, PM_REMOVE|PM_NOYIELD)) {
1805 TranslateMessage(&msg);
1806 DispatchMessageW(&msg);
1814 HRESULT bind_to_storage(LPCWSTR url, IBindCtx *pbc, REFIID riid, void **ppv)
1816 Binding *binding = NULL, *binding_ctx;
1821 binding_ctx = get_bctx_binding(pbc);
1823 hres = start_binding(NULL, binding_ctx, url, pbc, FALSE, riid, &binding);
1825 IBinding_Release(BINDING(binding_ctx));
1829 if(binding->hres == S_OK && binding->stgmed_buf->init) {
1830 if((binding->state & BINDING_STOPPED) && (binding->state & BINDING_LOCKED))
1831 IInternetProtocol_UnlockRequest(binding->protocol);
1833 *ppv = binding->stgmed_obj->vtbl->get_result(binding->stgmed_obj);
1836 IBinding_Release(BINDING(binding));
1838 return *ppv ? S_OK : MK_S_ASYNCHRONOUS;
1841 HRESULT bind_to_object(IMoniker *mon, LPCWSTR url, IBindCtx *pbc, REFIID riid, void **ppv)
1848 hres = start_binding(mon, NULL, url, pbc, TRUE, riid, &binding);
1852 if(binding->hres != S_OK) {
1853 hres = SUCCEEDED(binding->hres) ? S_OK : binding->hres;
1854 }else if(binding->bindf & BINDF_ASYNCHRONOUS) {
1855 hres = MK_S_ASYNCHRONOUS;
1857 *ppv = binding->obj;
1858 IUnknown_AddRef(binding->obj);
1862 IBinding_Release(BINDING(binding));