urlmon: Fixed some tests on IE11.
[wine/wine-gecko.git] / dlls / urlmon / binding.c
blob306e9b355daba153e93216500e460291752ea379
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"
21 #include "shlwapi.h"
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
27 static WCHAR cbinding_contextW[] = {'C','B','i','n','d','i','n','g',' ','C','o','n','t','e','x','t',0};
28 static WCHAR bscb_holderW[] = { '_','B','S','C','B','_','H','o','l','d','e','r','_',0 };
30 typedef struct {
31 IUnknown IUnknown_iface;
33 LONG ref;
35 IInternetProtocolEx *protocol;
37 HANDLE file;
38 HRESULT hres;
40 LPWSTR cache_file;
41 } stgmed_buf_t;
43 typedef struct _stgmed_obj_t stgmed_obj_t;
45 typedef struct {
46 void (*release)(stgmed_obj_t*);
47 HRESULT (*fill_stgmed)(stgmed_obj_t*,STGMEDIUM*);
48 HRESULT (*get_result)(stgmed_obj_t*,DWORD,void**);
49 } stgmed_obj_vtbl;
51 struct _stgmed_obj_t {
52 const stgmed_obj_vtbl *vtbl;
55 typedef enum {
56 BEFORE_DOWNLOAD,
57 DOWNLOADING,
58 END_DOWNLOAD
59 } download_state_t;
61 #define BINDING_LOCKED 0x0001
62 #define BINDING_STOPPED 0x0002
63 #define BINDING_OBJAVAIL 0x0004
64 #define BINDING_ABORTED 0x0008
66 typedef struct {
67 IBinding IBinding_iface;
68 IInternetProtocolSink IInternetProtocolSink_iface;
69 IInternetBindInfo IInternetBindInfo_iface;
70 IWinInetHttpInfo IWinInetHttpInfo_iface;
71 IServiceProvider IServiceProvider_iface;
73 LONG ref;
75 IBindStatusCallback *callback;
76 IServiceProvider *service_provider;
78 BindProtocol *protocol;
80 stgmed_buf_t *stgmed_buf;
81 stgmed_obj_t *stgmed_obj;
83 BINDINFO bindinfo;
84 DWORD bindf;
85 BOOL to_object;
86 LPWSTR mime;
87 UINT clipboard_format;
88 LPWSTR url;
89 LPWSTR redirect_url;
90 IID iid;
91 BOOL report_mime;
92 BOOL use_cache_file;
93 DWORD state;
94 HRESULT hres;
95 CLSID clsid;
96 download_state_t download_state;
97 IUnknown *obj;
98 IMoniker *mon;
99 IBindCtx *bctx;
100 HWND notif_hwnd;
102 CRITICAL_SECTION section;
103 } Binding;
105 static void read_protocol_data(stgmed_buf_t *stgmed_buf)
107 BYTE buf[8192];
108 DWORD read;
109 HRESULT hres;
111 do hres = IInternetProtocolEx_Read(stgmed_buf->protocol, buf, sizeof(buf), &read);
112 while(hres == S_OK);
115 static void dump_BINDINFO(BINDINFO *bi)
117 static const char * const BINDINFOF_str[] = {
118 "#0",
119 "BINDINFOF_URLENCODESTGMEDDATA",
120 "BINDINFOF_URLENCODEDEXTRAINFO"
123 static const char * const BINDVERB_str[] = {
124 "BINDVERB_GET",
125 "BINDVERB_POST",
126 "BINDVERB_PUT",
127 "BINDVERB_CUSTOM"
130 TRACE("\n"
131 "BINDINFO = {\n"
132 " %d, %s,\n"
133 " {%d, %p, %p},\n"
134 " %s,\n"
135 " %s,\n"
136 " %s,\n"
137 " %d, %08x, %d, %d\n"
138 " {%d %p %x},\n"
139 " %s\n"
140 " %p, %d\n"
141 "}\n",
143 bi->cbSize, debugstr_w(bi->szExtraInfo),
144 bi->stgmedData.tymed, bi->stgmedData.u.hGlobal, bi->stgmedData.pUnkForRelease,
145 bi->grfBindInfoF > BINDINFOF_URLENCODEDEXTRAINFO
146 ? "unknown" : BINDINFOF_str[bi->grfBindInfoF],
147 bi->dwBindVerb > BINDVERB_CUSTOM
148 ? "unknown" : BINDVERB_str[bi->dwBindVerb],
149 debugstr_w(bi->szCustomVerb),
150 bi->cbstgmedData, bi->dwOptions, bi->dwOptionsFlags, bi->dwCodePage,
151 bi->securityAttributes.nLength,
152 bi->securityAttributes.lpSecurityDescriptor,
153 bi->securityAttributes.bInheritHandle,
154 debugstr_guid(&bi->iid),
155 bi->pUnk, bi->dwReserved
159 static void mime_available(Binding *This, LPCWSTR mime)
161 heap_free(This->mime);
162 This->mime = heap_strdupW(mime);
164 if(!This->mime || !This->report_mime)
165 return;
167 IBindStatusCallback_OnProgress(This->callback, 0, 0, BINDSTATUS_MIMETYPEAVAILABLE, This->mime);
169 This->clipboard_format = RegisterClipboardFormatW(This->mime);
172 static void stop_binding(Binding *binding, HRESULT hres, LPCWSTR str)
174 if(binding->state & BINDING_LOCKED) {
175 IInternetProtocolEx_UnlockRequest(&binding->protocol->IInternetProtocolEx_iface);
176 binding->state &= ~BINDING_LOCKED;
179 if(!(binding->state & BINDING_STOPPED)) {
180 binding->state |= BINDING_STOPPED;
182 binding->hres = hres;
183 IBindStatusCallback_OnStopBinding(binding->callback, hres, str);
187 static LPWSTR get_mime_clsid(LPCWSTR mime, CLSID *clsid)
189 LPWSTR key_name, ret;
190 DWORD res, type, size;
191 HKEY hkey;
192 int len;
193 HRESULT hres;
195 static const WCHAR mime_keyW[] =
196 {'M','I','M','E','\\','D','a','t','a','b','a','s','e','\\',
197 'C','o','n','t','e','n','t',' ','T','y','p','e','\\'};
198 static const WCHAR clsidW[] = {'C','L','S','I','D',0};
200 len = strlenW(mime)+1;
201 key_name = heap_alloc(sizeof(mime_keyW) + len*sizeof(WCHAR));
202 memcpy(key_name, mime_keyW, sizeof(mime_keyW));
203 strcpyW(key_name + sizeof(mime_keyW)/sizeof(WCHAR), mime);
205 res = RegOpenKeyW(HKEY_CLASSES_ROOT, key_name, &hkey);
206 heap_free(key_name);
207 if(res != ERROR_SUCCESS) {
208 WARN("Could not open MIME key: %x\n", res);
209 return NULL;
212 size = 50*sizeof(WCHAR);
213 ret = heap_alloc(size);
214 res = RegQueryValueExW(hkey, clsidW, NULL, &type, (LPBYTE)ret, &size);
215 RegCloseKey(hkey);
216 if(res != ERROR_SUCCESS) {
217 WARN("Could not get CLSID: %08x\n", res);
218 heap_free(ret);
219 return NULL;
222 hres = CLSIDFromString(ret, clsid);
223 if(FAILED(hres)) {
224 WARN("Could not parse CLSID: %08x\n", hres);
225 heap_free(ret);
226 return NULL;
229 return ret;
232 static void load_doc_mon(Binding *binding, IPersistMoniker *persist)
234 IBindCtx *bctx;
235 HRESULT hres;
237 hres = CreateAsyncBindCtxEx(binding->bctx, 0, NULL, NULL, &bctx, 0);
238 if(FAILED(hres)) {
239 WARN("CreateAsyncBindCtxEx failed: %08x\n", hres);
240 return;
243 IBindCtx_RevokeObjectParam(bctx, bscb_holderW);
244 IBindCtx_RegisterObjectParam(bctx, cbinding_contextW, (IUnknown*)&binding->IBinding_iface);
246 hres = IPersistMoniker_Load(persist, binding->download_state == END_DOWNLOAD, binding->mon, bctx, 0x12);
247 IBindCtx_RevokeObjectParam(bctx, cbinding_contextW);
248 IBindCtx_Release(bctx);
249 if(FAILED(hres))
250 FIXME("Load failed: %08x\n", hres);
253 static HRESULT create_mime_object(Binding *binding, const CLSID *clsid, LPCWSTR clsid_str)
255 IPersistMoniker *persist;
256 HRESULT hres;
258 hres = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
259 &binding->iid, (void**)&binding->obj);
260 if(FAILED(hres)) {
261 WARN("CoCreateInstance failed: %08x\n", hres);
262 return INET_E_CANNOT_INSTANTIATE_OBJECT;
265 binding->state |= BINDING_OBJAVAIL;
267 hres = IUnknown_QueryInterface(binding->obj, &IID_IPersistMoniker, (void**)&persist);
268 if(SUCCEEDED(hres)) {
269 IMonikerProp *prop;
271 hres = IPersistMoniker_QueryInterface(persist, &IID_IMonikerProp, (void**)&prop);
272 if(SUCCEEDED(hres)) {
273 IMonikerProp_PutProperty(prop, MIMETYPEPROP, binding->mime);
274 IMonikerProp_PutProperty(prop, CLASSIDPROP, clsid_str);
275 IMonikerProp_Release(prop);
278 load_doc_mon(binding, persist);
280 IPersistMoniker_Release(persist);
281 }else {
282 FIXME("Could not get IPersistMoniker: %08x\n", hres);
283 /* FIXME: Try query IPersistFile */
286 IBindStatusCallback_OnObjectAvailable(binding->callback, &binding->iid, binding->obj);
288 return S_OK;
291 static void create_object(Binding *binding)
293 LPWSTR clsid_str;
294 CLSID clsid;
295 HRESULT hres;
297 if(!binding->mime) {
298 FIXME("MIME not available\n");
299 return;
302 if((clsid_str = get_mime_clsid(binding->mime, &clsid)))
303 IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_CLASSIDAVAILABLE, clsid_str);
305 IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_BEGINSYNCOPERATION, NULL);
307 if(clsid_str) {
308 hres = create_mime_object(binding, &clsid, clsid_str);
309 heap_free(clsid_str);
310 }else {
311 FIXME("Could not find object for MIME %s\n", debugstr_w(binding->mime));
312 hres = REGDB_E_CLASSNOTREG;
315 IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_ENDSYNCOPERATION, NULL);
316 binding->clsid = CLSID_NULL;
318 stop_binding(binding, hres, NULL);
319 if(FAILED(hres))
320 IInternetProtocolEx_Terminate(&binding->protocol->IInternetProtocolEx_iface, 0);
323 static void cache_file_available(Binding *This, const WCHAR *file_name)
325 heap_free(This->stgmed_buf->cache_file);
326 This->stgmed_buf->cache_file = heap_strdupW(file_name);
328 if(This->use_cache_file) {
329 This->stgmed_buf->file = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
330 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
331 if(This->stgmed_buf->file == INVALID_HANDLE_VALUE)
332 WARN("CreateFile failed: %u\n", GetLastError());
336 static inline stgmed_buf_t *impl_from_IUnknown(IUnknown *iface)
338 return CONTAINING_RECORD(iface, stgmed_buf_t, IUnknown_iface);
341 static HRESULT WINAPI StgMedUnk_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
343 stgmed_buf_t *This = impl_from_IUnknown(iface);
345 *ppv = NULL;
347 if(IsEqualGUID(riid, &IID_IUnknown)) {
348 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
350 *ppv = &This->IUnknown_iface;
351 IUnknown_AddRef(&This->IUnknown_iface);
352 return S_OK;
355 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
356 return E_NOINTERFACE;
359 static ULONG WINAPI StgMedUnk_AddRef(IUnknown *iface)
361 stgmed_buf_t *This = impl_from_IUnknown(iface);
362 LONG ref = InterlockedIncrement(&This->ref);
364 TRACE("(%p) ref=%d\n", This, ref);
366 return ref;
369 static ULONG WINAPI StgMedUnk_Release(IUnknown *iface)
371 stgmed_buf_t *This = impl_from_IUnknown(iface);
372 LONG ref = InterlockedDecrement(&This->ref);
374 TRACE("(%p) ref=%d\n", This, ref);
376 if(!ref) {
377 if(This->file != INVALID_HANDLE_VALUE)
378 CloseHandle(This->file);
379 IInternetProtocolEx_Release(This->protocol);
380 heap_free(This->cache_file);
381 heap_free(This);
383 URLMON_UnlockModule();
386 return ref;
389 static const IUnknownVtbl StgMedUnkVtbl = {
390 StgMedUnk_QueryInterface,
391 StgMedUnk_AddRef,
392 StgMedUnk_Release
395 static stgmed_buf_t *create_stgmed_buf(IInternetProtocolEx *protocol)
397 stgmed_buf_t *ret = heap_alloc(sizeof(*ret));
399 ret->IUnknown_iface.lpVtbl = &StgMedUnkVtbl;
400 ret->ref = 1;
401 ret->file = INVALID_HANDLE_VALUE;
402 ret->hres = S_OK;
403 ret->cache_file = NULL;
405 IInternetProtocolEx_AddRef(protocol);
406 ret->protocol = protocol;
408 URLMON_LockModule();
410 return ret;
413 typedef struct {
414 stgmed_obj_t stgmed_obj;
415 IStream IStream_iface;
417 LONG ref;
419 stgmed_buf_t *buf;
420 } ProtocolStream;
422 static inline ProtocolStream *impl_from_IStream(IStream *iface)
424 return CONTAINING_RECORD(iface, ProtocolStream, IStream_iface);
427 static HRESULT WINAPI ProtocolStream_QueryInterface(IStream *iface,
428 REFIID riid, void **ppv)
430 ProtocolStream *This = impl_from_IStream(iface);
432 *ppv = NULL;
434 if(IsEqualGUID(&IID_IUnknown, riid)) {
435 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
436 *ppv = &This->IStream_iface;
437 }else if(IsEqualGUID(&IID_ISequentialStream, riid)) {
438 TRACE("(%p)->(IID_ISequentialStream %p)\n", This, ppv);
439 *ppv = &This->IStream_iface;
440 }else if(IsEqualGUID(&IID_IStream, riid)) {
441 TRACE("(%p)->(IID_IStream %p)\n", This, ppv);
442 *ppv = &This->IStream_iface;
445 if(*ppv) {
446 IStream_AddRef(&This->IStream_iface);
447 return S_OK;
450 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
451 return E_NOINTERFACE;
454 static ULONG WINAPI ProtocolStream_AddRef(IStream *iface)
456 ProtocolStream *This = impl_from_IStream(iface);
457 LONG ref = InterlockedIncrement(&This->ref);
459 TRACE("(%p) ref=%d\n", This, ref);
461 return ref;
464 static ULONG WINAPI ProtocolStream_Release(IStream *iface)
466 ProtocolStream *This = impl_from_IStream(iface);
467 LONG ref = InterlockedDecrement(&This->ref);
469 TRACE("(%p) ref=%d\n", This, ref);
471 if(!ref) {
472 IUnknown_Release(&This->buf->IUnknown_iface);
473 heap_free(This);
475 URLMON_UnlockModule();
478 return ref;
481 static HRESULT WINAPI ProtocolStream_Read(IStream *iface, void *pv,
482 ULONG cb, ULONG *pcbRead)
484 ProtocolStream *This = impl_from_IStream(iface);
485 DWORD read = 0;
486 HRESULT hres;
488 TRACE("(%p)->(%p %d %p)\n", This, pv, cb, pcbRead);
490 if(This->buf->file == INVALID_HANDLE_VALUE) {
491 hres = This->buf->hres = IInternetProtocolEx_Read(This->buf->protocol, (PBYTE)pv, cb, &read);
492 }else {
493 hres = ReadFile(This->buf->file, pv, cb, &read, NULL) ? S_OK : INET_E_DOWNLOAD_FAILURE;
496 if (pcbRead)
497 *pcbRead = read;
499 if(hres == E_PENDING)
500 return E_PENDING;
501 else if(FAILED(hres))
502 FIXME("Read failed: %08x\n", hres);
504 return read ? S_OK : S_FALSE;
507 static HRESULT WINAPI ProtocolStream_Write(IStream *iface, const void *pv,
508 ULONG cb, ULONG *pcbWritten)
510 ProtocolStream *This = impl_from_IStream(iface);
512 TRACE("(%p)->(%p %d %p)\n", This, pv, cb, pcbWritten);
514 return STG_E_ACCESSDENIED;
517 static HRESULT WINAPI ProtocolStream_Seek(IStream *iface, LARGE_INTEGER dlibMove,
518 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
520 ProtocolStream *This = impl_from_IStream(iface);
521 FIXME("(%p)->(%d %08x %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
522 return E_NOTIMPL;
525 static HRESULT WINAPI ProtocolStream_SetSize(IStream *iface, ULARGE_INTEGER libNewSize)
527 ProtocolStream *This = impl_from_IStream(iface);
528 FIXME("(%p)->(%d)\n", This, libNewSize.u.LowPart);
529 return E_NOTIMPL;
532 static HRESULT WINAPI ProtocolStream_CopyTo(IStream *iface, IStream *pstm,
533 ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten)
535 ProtocolStream *This = impl_from_IStream(iface);
536 FIXME("(%p)->(%p %d %p %p)\n", This, pstm, cb.u.LowPart, pcbRead, pcbWritten);
537 return E_NOTIMPL;
540 static HRESULT WINAPI ProtocolStream_Commit(IStream *iface, DWORD grfCommitFlags)
542 ProtocolStream *This = impl_from_IStream(iface);
544 TRACE("(%p)->(%08x)\n", This, grfCommitFlags);
546 return E_NOTIMPL;
549 static HRESULT WINAPI ProtocolStream_Revert(IStream *iface)
551 ProtocolStream *This = impl_from_IStream(iface);
553 TRACE("(%p)\n", This);
555 return E_NOTIMPL;
558 static HRESULT WINAPI ProtocolStream_LockRegion(IStream *iface, ULARGE_INTEGER libOffset,
559 ULARGE_INTEGER cb, DWORD dwLockType)
561 ProtocolStream *This = impl_from_IStream(iface);
562 FIXME("(%p)->(%d %d %d)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
563 return E_NOTIMPL;
566 static HRESULT WINAPI ProtocolStream_UnlockRegion(IStream *iface,
567 ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
569 ProtocolStream *This = impl_from_IStream(iface);
570 FIXME("(%p)->(%d %d %d)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
571 return E_NOTIMPL;
574 static HRESULT WINAPI ProtocolStream_Stat(IStream *iface, STATSTG *pstatstg,
575 DWORD dwStatFlag)
577 ProtocolStream *This = impl_from_IStream(iface);
578 TRACE("(%p)->(%p %08x)\n", This, pstatstg, dwStatFlag);
580 if(!pstatstg)
581 return E_FAIL;
583 memset(pstatstg, 0, sizeof(STATSTG));
585 if(!(dwStatFlag&STATFLAG_NONAME) && This->buf->cache_file) {
586 pstatstg->pwcsName = CoTaskMemAlloc((lstrlenW(This->buf->cache_file)+1)*sizeof(WCHAR));
587 if(!pstatstg->pwcsName)
588 return STG_E_INSUFFICIENTMEMORY;
590 lstrcpyW(pstatstg->pwcsName, This->buf->cache_file);
593 pstatstg->type = STGTY_STREAM;
594 if(This->buf->file != INVALID_HANDLE_VALUE) {
595 GetFileSizeEx(This->buf->file, (PLARGE_INTEGER)&pstatstg->cbSize);
596 GetFileTime(This->buf->file, &pstatstg->ctime, &pstatstg->atime, &pstatstg->mtime);
597 if(pstatstg->cbSize.QuadPart)
598 pstatstg->grfMode = GENERIC_READ;
601 return S_OK;
604 static HRESULT WINAPI ProtocolStream_Clone(IStream *iface, IStream **ppstm)
606 ProtocolStream *This = impl_from_IStream(iface);
607 FIXME("(%p)->(%p)\n", This, ppstm);
608 return E_NOTIMPL;
611 static const IStreamVtbl ProtocolStreamVtbl = {
612 ProtocolStream_QueryInterface,
613 ProtocolStream_AddRef,
614 ProtocolStream_Release,
615 ProtocolStream_Read,
616 ProtocolStream_Write,
617 ProtocolStream_Seek,
618 ProtocolStream_SetSize,
619 ProtocolStream_CopyTo,
620 ProtocolStream_Commit,
621 ProtocolStream_Revert,
622 ProtocolStream_LockRegion,
623 ProtocolStream_UnlockRegion,
624 ProtocolStream_Stat,
625 ProtocolStream_Clone
628 static void stgmed_stream_release(stgmed_obj_t *obj)
630 ProtocolStream *stream = (ProtocolStream*)obj;
631 IStream_Release(&stream->IStream_iface);
634 static HRESULT stgmed_stream_fill_stgmed(stgmed_obj_t *obj, STGMEDIUM *stgmed)
636 ProtocolStream *stream = (ProtocolStream*)obj;
638 stgmed->tymed = TYMED_ISTREAM;
639 stgmed->u.pstm = &stream->IStream_iface;
640 stgmed->pUnkForRelease = &stream->buf->IUnknown_iface;
642 return S_OK;
645 static HRESULT stgmed_stream_get_result(stgmed_obj_t *obj, DWORD bindf, void **result)
647 ProtocolStream *stream = (ProtocolStream*)obj;
649 if(!(bindf & BINDF_ASYNCHRONOUS) && stream->buf->file == INVALID_HANDLE_VALUE
650 && stream->buf->hres != S_FALSE)
651 return INET_E_DATA_NOT_AVAILABLE;
653 IStream_AddRef(&stream->IStream_iface);
654 *result = &stream->IStream_iface;
655 return S_OK;
658 static const stgmed_obj_vtbl stgmed_stream_vtbl = {
659 stgmed_stream_release,
660 stgmed_stream_fill_stgmed,
661 stgmed_stream_get_result
664 typedef struct {
665 stgmed_obj_t stgmed_obj;
666 stgmed_buf_t *buf;
667 } stgmed_file_obj_t;
669 static stgmed_obj_t *create_stgmed_stream(stgmed_buf_t *buf)
671 ProtocolStream *ret = heap_alloc(sizeof(ProtocolStream));
673 ret->stgmed_obj.vtbl = &stgmed_stream_vtbl;
674 ret->IStream_iface.lpVtbl = &ProtocolStreamVtbl;
675 ret->ref = 1;
677 IUnknown_AddRef(&buf->IUnknown_iface);
678 ret->buf = buf;
680 URLMON_LockModule();
682 return &ret->stgmed_obj;
685 static void stgmed_file_release(stgmed_obj_t *obj)
687 stgmed_file_obj_t *file_obj = (stgmed_file_obj_t*)obj;
689 IUnknown_Release(&file_obj->buf->IUnknown_iface);
690 heap_free(file_obj);
693 static HRESULT stgmed_file_fill_stgmed(stgmed_obj_t *obj, STGMEDIUM *stgmed)
695 stgmed_file_obj_t *file_obj = (stgmed_file_obj_t*)obj;
697 if(!file_obj->buf->cache_file) {
698 WARN("cache_file not set\n");
699 return INET_E_DATA_NOT_AVAILABLE;
702 read_protocol_data(file_obj->buf);
704 stgmed->tymed = TYMED_FILE;
705 stgmed->u.lpszFileName = file_obj->buf->cache_file;
706 stgmed->pUnkForRelease = &file_obj->buf->IUnknown_iface;
708 return S_OK;
711 static HRESULT stgmed_file_get_result(stgmed_obj_t *obj, DWORD bindf, void **result)
713 return bindf & BINDF_ASYNCHRONOUS ? MK_S_ASYNCHRONOUS : S_OK;
716 static const stgmed_obj_vtbl stgmed_file_vtbl = {
717 stgmed_file_release,
718 stgmed_file_fill_stgmed,
719 stgmed_file_get_result
722 static stgmed_obj_t *create_stgmed_file(stgmed_buf_t *buf)
724 stgmed_file_obj_t *ret = heap_alloc(sizeof(*ret));
726 ret->stgmed_obj.vtbl = &stgmed_file_vtbl;
728 IUnknown_AddRef(&buf->IUnknown_iface);
729 ret->buf = buf;
731 return &ret->stgmed_obj;
734 static inline Binding *impl_from_IBinding(IBinding *iface)
736 return CONTAINING_RECORD(iface, Binding, IBinding_iface);
739 static HRESULT WINAPI Binding_QueryInterface(IBinding *iface, REFIID riid, void **ppv)
741 Binding *This = impl_from_IBinding(iface);
743 *ppv = NULL;
745 if(IsEqualGUID(&IID_IUnknown, riid)) {
746 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
747 *ppv = &This->IBinding_iface;
748 }else if(IsEqualGUID(&IID_IBinding, riid)) {
749 TRACE("(%p)->(IID_IBinding %p)\n", This, ppv);
750 *ppv = &This->IBinding_iface;
751 }else if(IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
752 TRACE("(%p)->(IID_IInternetProtocolSink %p)\n", This, ppv);
753 *ppv = &This->IInternetProtocolSink_iface;
754 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
755 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
756 *ppv = &This->IInternetBindInfo_iface;
757 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
758 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
759 *ppv = &This->IServiceProvider_iface;
760 }else if(IsEqualGUID(&IID_IWinInetInfo, riid)) {
761 TRACE("(%p)->(IID_IWinInetInfo %p)\n", This, ppv);
763 /* NOTE: This violidates COM rules, but tests prove that we should do it */
764 if(!This->protocol->wininet_info)
765 return E_NOINTERFACE;
767 *ppv = &This->IWinInetHttpInfo_iface;
768 }else if(IsEqualGUID(&IID_IWinInetHttpInfo, riid)) {
769 TRACE("(%p)->(IID_IWinInetHttpInfo %p)\n", This, ppv);
771 if(!This->protocol->wininet_http_info)
772 return E_NOINTERFACE;
774 *ppv = &This->IWinInetHttpInfo_iface;
777 if(*ppv) {
778 IBinding_AddRef(&This->IBinding_iface);
779 return S_OK;
782 WARN("Unsupported interface %s\n", debugstr_guid(riid));
783 return E_NOINTERFACE;
786 static ULONG WINAPI Binding_AddRef(IBinding *iface)
788 Binding *This = impl_from_IBinding(iface);
789 LONG ref = InterlockedIncrement(&This->ref);
791 TRACE("(%p) ref=%d\n", This, ref);
793 return ref;
796 static ULONG WINAPI Binding_Release(IBinding *iface)
798 Binding *This = impl_from_IBinding(iface);
799 LONG ref = InterlockedDecrement(&This->ref);
801 TRACE("(%p) ref=%d\n", This, ref);
803 if(!ref) {
804 if(This->notif_hwnd)
805 release_notif_hwnd(This->notif_hwnd);
806 if(This->mon)
807 IMoniker_Release(This->mon);
808 if(This->callback)
809 IBindStatusCallback_Release(This->callback);
810 if(This->protocol)
811 IInternetProtocolEx_Release(&This->protocol->IInternetProtocolEx_iface);
812 if(This->service_provider)
813 IServiceProvider_Release(This->service_provider);
814 if(This->stgmed_buf)
815 IUnknown_Release(&This->stgmed_buf->IUnknown_iface);
816 if(This->stgmed_obj)
817 This->stgmed_obj->vtbl->release(This->stgmed_obj);
818 if(This->obj)
819 IUnknown_Release(This->obj);
820 if(This->bctx)
821 IBindCtx_Release(This->bctx);
823 ReleaseBindInfo(&This->bindinfo);
824 This->section.DebugInfo->Spare[0] = 0;
825 DeleteCriticalSection(&This->section);
826 SysFreeString(This->url);
827 heap_free(This->mime);
828 heap_free(This->redirect_url);
829 heap_free(This);
831 URLMON_UnlockModule();
834 return ref;
837 static HRESULT WINAPI Binding_Abort(IBinding *iface)
839 Binding *This = impl_from_IBinding(iface);
840 HRESULT hres;
842 TRACE("(%p)\n", This);
844 if(This->state & BINDING_ABORTED)
845 return E_FAIL;
847 hres = IInternetProtocolEx_Abort(&This->protocol->IInternetProtocolEx_iface, E_ABORT,
848 ERROR_SUCCESS);
849 if(FAILED(hres))
850 return hres;
852 This->state |= BINDING_ABORTED;
853 return S_OK;
856 static HRESULT WINAPI Binding_Suspend(IBinding *iface)
858 Binding *This = impl_from_IBinding(iface);
859 FIXME("(%p)\n", This);
860 return E_NOTIMPL;
863 static HRESULT WINAPI Binding_Resume(IBinding *iface)
865 Binding *This = impl_from_IBinding(iface);
866 FIXME("(%p)\n", This);
867 return E_NOTIMPL;
870 static HRESULT WINAPI Binding_SetPriority(IBinding *iface, LONG nPriority)
872 Binding *This = impl_from_IBinding(iface);
873 FIXME("(%p)->(%d)\n", This, nPriority);
874 return E_NOTIMPL;
877 static HRESULT WINAPI Binding_GetPriority(IBinding *iface, LONG *pnPriority)
879 Binding *This = impl_from_IBinding(iface);
880 FIXME("(%p)->(%p)\n", This, pnPriority);
881 return E_NOTIMPL;
884 static HRESULT WINAPI Binding_GetBindResult(IBinding *iface, CLSID *pclsidProtocol,
885 DWORD *pdwResult, LPOLESTR *pszResult, DWORD *pdwReserved)
887 Binding *This = impl_from_IBinding(iface);
889 TRACE("(%p)->(%p %p %p %p)\n", This, pclsidProtocol, pdwResult, pszResult, pdwReserved);
891 if(!pdwResult || !pszResult || pdwReserved)
892 return E_INVALIDARG;
894 if(!(This->state & BINDING_STOPPED)) {
895 *pclsidProtocol = CLSID_NULL;
896 *pdwResult = 0;
897 *pszResult = NULL;
898 return S_OK;
901 *pclsidProtocol = This->hres==S_OK ? CLSID_NULL : This->clsid;
902 *pdwResult = This->hres;
903 *pszResult = NULL;
904 return S_OK;
907 static const IBindingVtbl BindingVtbl = {
908 Binding_QueryInterface,
909 Binding_AddRef,
910 Binding_Release,
911 Binding_Abort,
912 Binding_Suspend,
913 Binding_Resume,
914 Binding_SetPriority,
915 Binding_GetPriority,
916 Binding_GetBindResult
919 static Binding *get_bctx_binding(IBindCtx *bctx)
921 IBinding *binding;
922 IUnknown *unk;
923 HRESULT hres;
925 hres = IBindCtx_GetObjectParam(bctx, cbinding_contextW, &unk);
926 if(FAILED(hres))
927 return NULL;
929 hres = IUnknown_QueryInterface(unk, &IID_IBinding, (void**)&binding);
930 IUnknown_Release(unk);
931 if(FAILED(hres))
932 return NULL;
934 if (binding->lpVtbl != &BindingVtbl)
935 return NULL;
936 return impl_from_IBinding(binding);
939 static inline Binding *impl_from_IInternetProtocolSink(IInternetProtocolSink *iface)
941 return CONTAINING_RECORD(iface, Binding, IInternetProtocolSink_iface);
944 static HRESULT WINAPI InternetProtocolSink_QueryInterface(IInternetProtocolSink *iface,
945 REFIID riid, void **ppv)
947 Binding *This = impl_from_IInternetProtocolSink(iface);
948 return IBinding_QueryInterface(&This->IBinding_iface, riid, ppv);
951 static ULONG WINAPI InternetProtocolSink_AddRef(IInternetProtocolSink *iface)
953 Binding *This = impl_from_IInternetProtocolSink(iface);
954 return IBinding_AddRef(&This->IBinding_iface);
957 static ULONG WINAPI InternetProtocolSink_Release(IInternetProtocolSink *iface)
959 Binding *This = impl_from_IInternetProtocolSink(iface);
960 return IBinding_Release(&This->IBinding_iface);
963 static HRESULT WINAPI InternetProtocolSink_Switch(IInternetProtocolSink *iface,
964 PROTOCOLDATA *pProtocolData)
966 Binding *This = impl_from_IInternetProtocolSink(iface);
968 WARN("(%p)->(%p)\n", This, pProtocolData);
970 return E_FAIL;
973 static void on_progress(Binding *This, ULONG progress, ULONG progress_max,
974 ULONG status_code, LPCWSTR status_text)
976 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
977 status_code, status_text);
980 static HRESULT WINAPI InternetProtocolSink_ReportProgress(IInternetProtocolSink *iface,
981 ULONG ulStatusCode, LPCWSTR szStatusText)
983 Binding *This = impl_from_IInternetProtocolSink(iface);
985 TRACE("(%p)->(%s %s)\n", This, debugstr_bindstatus(ulStatusCode), debugstr_w(szStatusText));
987 switch(ulStatusCode) {
988 case BINDSTATUS_FINDINGRESOURCE:
989 on_progress(This, 0, 0, BINDSTATUS_FINDINGRESOURCE, szStatusText);
990 break;
991 case BINDSTATUS_CONNECTING:
992 on_progress(This, 0, 0, BINDSTATUS_CONNECTING, szStatusText);
993 break;
994 case BINDSTATUS_REDIRECTING:
995 heap_free(This->redirect_url);
996 This->redirect_url = heap_strdupW(szStatusText);
997 on_progress(This, 0, 0, BINDSTATUS_REDIRECTING, szStatusText);
998 break;
999 case BINDSTATUS_BEGINDOWNLOADDATA:
1000 break;
1001 case BINDSTATUS_SENDINGREQUEST:
1002 on_progress(This, 0, 0, BINDSTATUS_SENDINGREQUEST, szStatusText);
1003 break;
1004 case BINDSTATUS_PROTOCOLCLASSID:
1005 CLSIDFromString(szStatusText, &This->clsid);
1006 break;
1007 case BINDSTATUS_MIMETYPEAVAILABLE:
1008 case BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE:
1009 mime_available(This, szStatusText);
1010 break;
1011 case BINDSTATUS_CACHEFILENAMEAVAILABLE:
1012 cache_file_available(This, szStatusText);
1013 break;
1014 case BINDSTATUS_DECODING:
1015 IBindStatusCallback_OnProgress(This->callback, 0, 0, BINDSTATUS_DECODING, szStatusText);
1016 break;
1017 case BINDSTATUS_LOADINGMIMEHANDLER:
1018 on_progress(This, 0, 0, BINDSTATUS_LOADINGMIMEHANDLER, szStatusText);
1019 break;
1020 case BINDSTATUS_DIRECTBIND: /* FIXME: Handle BINDSTATUS_DIRECTBIND in BindProtocol */
1021 This->report_mime = FALSE;
1022 break;
1023 case BINDSTATUS_ACCEPTRANGES:
1024 break;
1025 default:
1026 FIXME("Unhandled status code %d\n", ulStatusCode);
1027 return E_NOTIMPL;
1030 return S_OK;
1033 static void report_data(Binding *This, DWORD bscf, ULONG progress, ULONG progress_max)
1035 FORMATETC formatetc = {0, NULL, 1, -1, TYMED_ISTREAM};
1036 BOOL sent_begindownloaddata = FALSE;
1038 TRACE("(%p)->(%d %u %u)\n", This, bscf, progress, progress_max);
1040 if(This->download_state == END_DOWNLOAD || (This->state & BINDING_ABORTED)) {
1041 read_protocol_data(This->stgmed_buf);
1042 return;
1045 if(This->state & BINDING_STOPPED)
1046 return;
1048 if(This->stgmed_buf->file != INVALID_HANDLE_VALUE)
1049 read_protocol_data(This->stgmed_buf);
1051 if(This->download_state == BEFORE_DOWNLOAD) {
1052 This->download_state = DOWNLOADING;
1053 sent_begindownloaddata = TRUE;
1054 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1055 BINDSTATUS_BEGINDOWNLOADDATA, This->url);
1057 if(This->stgmed_buf->cache_file)
1058 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1059 BINDSTATUS_CACHEFILENAMEAVAILABLE, This->stgmed_buf->cache_file);
1062 if(This->stgmed_buf->hres == S_FALSE || (bscf & BSCF_LASTDATANOTIFICATION)) {
1063 This->download_state = END_DOWNLOAD;
1064 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1065 BINDSTATUS_ENDDOWNLOADDATA, This->url);
1066 }else if(!sent_begindownloaddata) {
1067 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1068 BINDSTATUS_DOWNLOADINGDATA, This->url);
1071 if(This->state & (BINDING_STOPPED|BINDING_ABORTED))
1072 return;
1074 if(This->to_object) {
1075 if(!(This->state & BINDING_OBJAVAIL)) {
1076 IBinding_AddRef(&This->IBinding_iface);
1077 create_object(This);
1078 IBinding_Release(&This->IBinding_iface);
1080 }else {
1081 STGMEDIUM stgmed;
1082 HRESULT hres;
1084 if(!(This->state & BINDING_LOCKED)) {
1085 HRESULT hres = IInternetProtocolEx_LockRequest(
1086 &This->protocol->IInternetProtocolEx_iface, 0);
1087 if(SUCCEEDED(hres))
1088 This->state |= BINDING_LOCKED;
1091 hres = This->stgmed_obj->vtbl->fill_stgmed(This->stgmed_obj, &stgmed);
1092 if(FAILED(hres)) {
1093 stop_binding(This, hres, NULL);
1094 return;
1097 formatetc.tymed = stgmed.tymed;
1098 formatetc.cfFormat = This->clipboard_format;
1100 hres = IBindStatusCallback_OnDataAvailable(This->callback, bscf, progress,
1101 &formatetc, &stgmed);
1102 if(hres != S_OK) {
1103 if(This->download_state != END_DOWNLOAD) {
1104 This->download_state = END_DOWNLOAD;
1105 IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
1106 BINDSTATUS_ENDDOWNLOADDATA, This->url);
1109 WARN("OnDataAvailable returned %x\n", hres);
1110 stop_binding(This, hres, NULL);
1111 return;
1114 if(This->download_state == END_DOWNLOAD)
1115 stop_binding(This, S_OK, NULL);
1119 static HRESULT WINAPI InternetProtocolSink_ReportData(IInternetProtocolSink *iface,
1120 DWORD grfBSCF, ULONG ulProgress, ULONG ulProgressMax)
1122 Binding *This = impl_from_IInternetProtocolSink(iface);
1124 TRACE("(%p)->(%d %u %u)\n", This, grfBSCF, ulProgress, ulProgressMax);
1126 report_data(This, grfBSCF, ulProgress, ulProgressMax);
1127 return S_OK;
1130 static HRESULT WINAPI InternetProtocolSink_ReportResult(IInternetProtocolSink *iface,
1131 HRESULT hrResult, DWORD dwError, LPCWSTR szResult)
1133 Binding *This = impl_from_IInternetProtocolSink(iface);
1135 TRACE("(%p)->(%08x %d %s)\n", This, hrResult, dwError, debugstr_w(szResult));
1137 stop_binding(This, hrResult, szResult);
1139 IInternetProtocolEx_Terminate(&This->protocol->IInternetProtocolEx_iface, 0);
1140 return S_OK;
1143 static const IInternetProtocolSinkVtbl InternetProtocolSinkVtbl = {
1144 InternetProtocolSink_QueryInterface,
1145 InternetProtocolSink_AddRef,
1146 InternetProtocolSink_Release,
1147 InternetProtocolSink_Switch,
1148 InternetProtocolSink_ReportProgress,
1149 InternetProtocolSink_ReportData,
1150 InternetProtocolSink_ReportResult
1153 static inline Binding *impl_from_IInternetBindInfo(IInternetBindInfo *iface)
1155 return CONTAINING_RECORD(iface, Binding, IInternetBindInfo_iface);
1158 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
1159 REFIID riid, void **ppv)
1161 Binding *This = impl_from_IInternetBindInfo(iface);
1162 return IBinding_QueryInterface(&This->IBinding_iface, riid, ppv);
1165 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
1167 Binding *This = impl_from_IInternetBindInfo(iface);
1168 return IBinding_AddRef(&This->IBinding_iface);
1171 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
1173 Binding *This = impl_from_IInternetBindInfo(iface);
1174 return IBinding_Release(&This->IBinding_iface);
1177 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
1178 DWORD *grfBINDF, BINDINFO *pbindinfo)
1180 Binding *This = impl_from_IInternetBindInfo(iface);
1182 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
1184 *grfBINDF = This->bindf;
1185 return CopyBindInfo(&This->bindinfo, pbindinfo);
1188 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
1189 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
1191 Binding *This = impl_from_IInternetBindInfo(iface);
1193 TRACE("(%p)->(%d %p %d %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
1195 switch(ulStringType) {
1196 case BINDSTRING_ACCEPT_MIMES: {
1197 static const WCHAR wszMimes[] = {'*','/','*',0};
1199 if(!ppwzStr || !pcElFetched)
1200 return E_INVALIDARG;
1202 ppwzStr[0] = CoTaskMemAlloc(sizeof(wszMimes));
1203 memcpy(ppwzStr[0], wszMimes, sizeof(wszMimes));
1204 *pcElFetched = 1;
1205 return S_OK;
1207 case BINDSTRING_USER_AGENT: {
1208 IInternetBindInfo *bindinfo = NULL;
1209 HRESULT hres;
1211 hres = IBindStatusCallback_QueryInterface(This->callback, &IID_IInternetBindInfo,
1212 (void**)&bindinfo);
1213 if(FAILED(hres))
1214 return hres;
1216 hres = IInternetBindInfo_GetBindString(bindinfo, ulStringType, ppwzStr,
1217 cEl, pcElFetched);
1218 IInternetBindInfo_Release(bindinfo);
1220 return hres;
1222 case BINDSTRING_URL: {
1223 DWORD size = (SysStringLen(This->url)+1) * sizeof(WCHAR);
1225 if(!ppwzStr || !pcElFetched)
1226 return E_INVALIDARG;
1228 *ppwzStr = CoTaskMemAlloc(size);
1229 memcpy(*ppwzStr, This->url, size);
1230 *pcElFetched = 1;
1231 return S_OK;
1235 FIXME("not supported string type %d\n", ulStringType);
1236 return E_NOTIMPL;
1239 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
1240 InternetBindInfo_QueryInterface,
1241 InternetBindInfo_AddRef,
1242 InternetBindInfo_Release,
1243 InternetBindInfo_GetBindInfo,
1244 InternetBindInfo_GetBindString
1247 static inline Binding *impl_from_IWinInetHttpInfo(IWinInetHttpInfo *iface)
1249 return CONTAINING_RECORD(iface, Binding, IWinInetHttpInfo_iface);
1252 static HRESULT WINAPI WinInetHttpInfo_QueryInterface(IWinInetHttpInfo *iface, REFIID riid, void **ppv)
1254 Binding *This = impl_from_IWinInetHttpInfo(iface);
1255 return IBinding_QueryInterface(&This->IBinding_iface, riid, ppv);
1258 static ULONG WINAPI WinInetHttpInfo_AddRef(IWinInetHttpInfo *iface)
1260 Binding *This = impl_from_IWinInetHttpInfo(iface);
1261 return IBinding_AddRef(&This->IBinding_iface);
1264 static ULONG WINAPI WinInetHttpInfo_Release(IWinInetHttpInfo *iface)
1266 Binding *This = impl_from_IWinInetHttpInfo(iface);
1267 return IBinding_Release(&This->IBinding_iface);
1270 static HRESULT WINAPI WinInetHttpInfo_QueryOption(IWinInetHttpInfo *iface, DWORD dwOption,
1271 void *pBuffer, DWORD *pcbBuffer)
1273 Binding *This = impl_from_IWinInetHttpInfo(iface);
1274 TRACE("(%p)->(%x %p %p)\n", This, dwOption, pBuffer, pcbBuffer);
1276 if(!This->protocol->wininet_info)
1277 return E_FAIL;
1279 return IWinInetInfo_QueryOption(This->protocol->wininet_info,
1280 dwOption, pBuffer, pcbBuffer);
1283 static HRESULT WINAPI WinInetHttpInfo_QueryInfo(IWinInetHttpInfo *iface, DWORD dwOption,
1284 void *pBuffer, DWORD *pcbBuffer, DWORD *pdwFlags, DWORD *pdwReserved)
1286 Binding *This = impl_from_IWinInetHttpInfo(iface);
1287 TRACE("(%p)->(%x %p %p %p %p)\n", This, dwOption, pBuffer, pcbBuffer, pdwFlags, pdwReserved);
1289 if(!This->protocol->wininet_http_info)
1290 return E_FAIL;
1292 return IWinInetHttpInfo_QueryInfo(This->protocol->wininet_http_info,
1293 dwOption, pBuffer, pcbBuffer, pdwFlags, pdwReserved);
1296 static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl = {
1297 WinInetHttpInfo_QueryInterface,
1298 WinInetHttpInfo_AddRef,
1299 WinInetHttpInfo_Release,
1300 WinInetHttpInfo_QueryOption,
1301 WinInetHttpInfo_QueryInfo
1304 static inline Binding *impl_from_IServiceProvider(IServiceProvider *iface)
1306 return CONTAINING_RECORD(iface, Binding, IServiceProvider_iface);
1309 static HRESULT WINAPI ServiceProvider_QueryInterface(IServiceProvider *iface,
1310 REFIID riid, void **ppv)
1312 Binding *This = impl_from_IServiceProvider(iface);
1313 return IBinding_QueryInterface(&This->IBinding_iface, riid, ppv);
1316 static ULONG WINAPI ServiceProvider_AddRef(IServiceProvider *iface)
1318 Binding *This = impl_from_IServiceProvider(iface);
1319 return IBinding_AddRef(&This->IBinding_iface);
1322 static ULONG WINAPI ServiceProvider_Release(IServiceProvider *iface)
1324 Binding *This = impl_from_IServiceProvider(iface);
1325 return IBinding_Release(&This->IBinding_iface);
1328 static HRESULT WINAPI ServiceProvider_QueryService(IServiceProvider *iface,
1329 REFGUID guidService, REFIID riid, void **ppv)
1331 Binding *This = impl_from_IServiceProvider(iface);
1332 HRESULT hres;
1334 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
1336 if(This->service_provider) {
1337 hres = IServiceProvider_QueryService(This->service_provider, guidService,
1338 riid, ppv);
1339 if(SUCCEEDED(hres))
1340 return hres;
1343 WARN("unknown service %s\n", debugstr_guid(guidService));
1344 return E_NOINTERFACE;
1347 static const IServiceProviderVtbl ServiceProviderVtbl = {
1348 ServiceProvider_QueryInterface,
1349 ServiceProvider_AddRef,
1350 ServiceProvider_Release,
1351 ServiceProvider_QueryService
1354 static HRESULT get_callback(IBindCtx *pbc, IBindStatusCallback **callback)
1356 IUnknown *unk;
1357 HRESULT hres;
1359 hres = IBindCtx_GetObjectParam(pbc, bscb_holderW, &unk);
1360 if(FAILED(hres))
1361 return create_default_callback(callback);
1363 hres = IUnknown_QueryInterface(unk, &IID_IBindStatusCallback, (void**)callback);
1364 IUnknown_Release(unk);
1365 return hres;
1368 static BOOL is_urlmon_protocol(IUri *uri)
1370 DWORD scheme;
1371 HRESULT hres;
1373 hres = IUri_GetScheme(uri, &scheme);
1374 if(FAILED(hres))
1375 return FALSE;
1377 switch(scheme) {
1378 case URL_SCHEME_FILE:
1379 case URL_SCHEME_FTP:
1380 case URL_SCHEME_GOPHER:
1381 case URL_SCHEME_HTTP:
1382 case URL_SCHEME_HTTPS:
1383 case URL_SCHEME_MK:
1384 return TRUE;
1387 return FALSE;
1390 static HRESULT Binding_Create(IMoniker *mon, Binding *binding_ctx, IUri *uri, IBindCtx *pbc,
1391 BOOL to_obj, REFIID riid, Binding **binding)
1393 Binding *ret;
1394 HRESULT hres;
1396 URLMON_LockModule();
1398 ret = heap_alloc_zero(sizeof(Binding));
1400 ret->IBinding_iface.lpVtbl = &BindingVtbl;
1401 ret->IInternetProtocolSink_iface.lpVtbl = &InternetProtocolSinkVtbl;
1402 ret->IInternetBindInfo_iface.lpVtbl = &InternetBindInfoVtbl;
1403 ret->IWinInetHttpInfo_iface.lpVtbl = &WinInetHttpInfoVtbl;
1404 ret->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
1406 ret->ref = 1;
1408 ret->to_object = to_obj;
1409 ret->iid = *riid;
1410 ret->notif_hwnd = get_notif_hwnd();
1411 ret->report_mime = !binding_ctx;
1412 ret->download_state = BEFORE_DOWNLOAD;
1414 if(to_obj) {
1415 IBindCtx_AddRef(pbc);
1416 ret->bctx = pbc;
1419 if(mon) {
1420 IMoniker_AddRef(mon);
1421 ret->mon = mon;
1424 ret->bindinfo.cbSize = sizeof(BINDINFO);
1426 InitializeCriticalSection(&ret->section);
1427 ret->section.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": Binding.section");
1429 hres = get_callback(pbc, &ret->callback);
1430 if(FAILED(hres)) {
1431 WARN("Could not get IBindStatusCallback\n");
1432 IBinding_Release(&ret->IBinding_iface);
1433 return hres;
1436 IBindStatusCallback_QueryInterface(ret->callback, &IID_IServiceProvider,
1437 (void**)&ret->service_provider);
1439 if(binding_ctx) {
1440 ret->protocol = binding_ctx->protocol;
1441 IInternetProtocolEx_AddRef(&ret->protocol->IInternetProtocolEx_iface);
1442 }else {
1443 hres = create_binding_protocol(TRUE, &ret->protocol);
1444 if(FAILED(hres)) {
1445 WARN("Could not get protocol handler\n");
1446 IBinding_Release(&ret->IBinding_iface);
1447 return hres;
1451 hres = IBindStatusCallback_GetBindInfo(ret->callback, &ret->bindf, &ret->bindinfo);
1452 if(FAILED(hres)) {
1453 WARN("GetBindInfo failed: %08x\n", hres);
1454 IBinding_Release(&ret->IBinding_iface);
1455 return hres;
1458 TRACE("bindf %08x\n", ret->bindf);
1459 dump_BINDINFO(&ret->bindinfo);
1461 ret->bindf |= BINDF_FROMURLMON;
1462 if(to_obj)
1463 ret->bindinfo.dwOptions |= 0x100000;
1465 if(!(ret->bindf & BINDF_ASYNCHRONOUS) || !(ret->bindf & BINDF_PULLDATA)) {
1466 ret->bindf |= BINDF_NEEDFILE;
1467 ret->use_cache_file = TRUE;
1468 }else if(!is_urlmon_protocol(uri)) {
1469 ret->bindf |= BINDF_NEEDFILE;
1472 hres = IUri_GetDisplayUri(uri, &ret->url);
1473 if(FAILED(hres)) {
1474 IBinding_Release(&ret->IBinding_iface);
1475 return hres;
1478 if(binding_ctx) {
1479 ret->stgmed_buf = binding_ctx->stgmed_buf;
1480 IUnknown_AddRef(&ret->stgmed_buf->IUnknown_iface);
1481 ret->clipboard_format = binding_ctx->clipboard_format;
1482 }else {
1483 ret->stgmed_buf = create_stgmed_buf(&ret->protocol->IInternetProtocolEx_iface);
1486 if(to_obj) {
1487 ret->stgmed_obj = NULL;
1488 }else if(IsEqualGUID(&IID_IStream, riid)) {
1489 ret->stgmed_obj = create_stgmed_stream(ret->stgmed_buf);
1490 }else if(IsEqualGUID(&IID_IUnknown, riid)) {
1491 ret->bindf |= BINDF_NEEDFILE;
1492 ret->stgmed_obj = create_stgmed_file(ret->stgmed_buf);
1493 }else {
1494 FIXME("Unsupported riid %s\n", debugstr_guid(riid));
1495 IBinding_Release(&ret->IBinding_iface);
1496 return E_NOTIMPL;
1499 *binding = ret;
1500 return S_OK;
1503 static HRESULT start_binding(IMoniker *mon, Binding *binding_ctx, IUri *uri, IBindCtx *pbc,
1504 BOOL to_obj, REFIID riid, Binding **ret)
1506 Binding *binding = NULL;
1507 HRESULT hres;
1508 MSG msg;
1510 hres = Binding_Create(mon, binding_ctx, uri, pbc, to_obj, riid, &binding);
1511 if(FAILED(hres))
1512 return hres;
1514 hres = IBindStatusCallback_OnStartBinding(binding->callback, 0, &binding->IBinding_iface);
1515 if(FAILED(hres)) {
1516 WARN("OnStartBinding failed: %08x\n", hres);
1517 if(hres != E_ABORT && hres != E_NOTIMPL)
1518 hres = INET_E_DOWNLOAD_FAILURE;
1520 stop_binding(binding, hres, NULL);
1521 IBinding_Release(&binding->IBinding_iface);
1522 return hres;
1525 if(binding_ctx) {
1526 set_binding_sink(binding->protocol, &binding->IInternetProtocolSink_iface,
1527 &binding->IInternetBindInfo_iface);
1528 if(binding_ctx->redirect_url)
1529 IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_REDIRECTING, binding_ctx->redirect_url);
1530 report_data(binding, BSCF_FIRSTDATANOTIFICATION | (binding_ctx->download_state == END_DOWNLOAD ? BSCF_LASTDATANOTIFICATION : 0),
1531 0, 0);
1532 }else {
1533 hres = IInternetProtocolEx_StartEx(&binding->protocol->IInternetProtocolEx_iface, uri,
1534 &binding->IInternetProtocolSink_iface, &binding->IInternetBindInfo_iface,
1535 PI_APARTMENTTHREADED|PI_MIMEVERIFICATION, 0);
1537 TRACE("start ret %08x\n", hres);
1539 if(FAILED(hres) && hres != E_PENDING) {
1540 stop_binding(binding, hres, NULL);
1541 IBinding_Release(&binding->IBinding_iface);
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(IUri *uri, IBindCtx *pbc, REFIID riid, void **ppv)
1562 Binding *binding = NULL, *binding_ctx;
1563 HRESULT hres;
1565 binding_ctx = get_bctx_binding(pbc);
1567 hres = start_binding(NULL, binding_ctx, uri, pbc, FALSE, riid, &binding);
1568 if(binding_ctx)
1569 IBinding_Release(&binding_ctx->IBinding_iface);
1570 if(FAILED(hres))
1571 return hres;
1573 if(binding->hres == S_OK && binding->download_state != BEFORE_DOWNLOAD /* FIXME */) {
1574 if((binding->state & BINDING_STOPPED) && (binding->state & BINDING_LOCKED))
1575 IInternetProtocolEx_UnlockRequest(&binding->protocol->IInternetProtocolEx_iface);
1577 hres = binding->stgmed_obj->vtbl->get_result(binding->stgmed_obj, binding->bindf, ppv);
1578 }else if(binding->bindf & BINDF_ASYNCHRONOUS) {
1579 hres = MK_S_ASYNCHRONOUS;
1580 }else {
1581 hres = FAILED(binding->hres) ? binding->hres : S_OK;
1584 IBinding_Release(&binding->IBinding_iface);
1586 return hres;
1589 HRESULT bind_to_object(IMoniker *mon, IUri *uri, IBindCtx *pbc, REFIID riid, void **ppv)
1591 Binding *binding;
1592 HRESULT hres;
1594 *ppv = NULL;
1596 hres = start_binding(mon, NULL, uri, pbc, TRUE, riid, &binding);
1597 if(FAILED(hres))
1598 return hres;
1600 if(binding->hres != S_OK) {
1601 hres = SUCCEEDED(binding->hres) ? S_OK : binding->hres;
1602 }else if(binding->bindf & BINDF_ASYNCHRONOUS) {
1603 hres = MK_S_ASYNCHRONOUS;
1604 }else {
1605 *ppv = binding->obj;
1606 IUnknown_AddRef(binding->obj);
1607 hres = S_OK;
1610 IBinding_Release(&binding->IBinding_iface);
1612 return hres;