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 #define NONAMELESSUNION
21 #include "urlmon_main.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(urlmon
);
29 static WCHAR cbinding_contextW
[] = L
"CBinding Context";
30 static WCHAR bscb_holderW
[] = L
"_BSCB_Holder_";
33 IUnknown IUnknown_iface
;
37 IInternetProtocolEx
*protocol
;
45 typedef struct _stgmed_obj_t stgmed_obj_t
;
48 void (*release
)(stgmed_obj_t
*);
49 HRESULT (*fill_stgmed
)(stgmed_obj_t
*,STGMEDIUM
*);
50 HRESULT (*get_result
)(stgmed_obj_t
*,DWORD
,void**);
53 struct _stgmed_obj_t
{
54 const stgmed_obj_vtbl
*vtbl
;
63 #define BINDING_LOCKED 0x0001
64 #define BINDING_STOPPED 0x0002
65 #define BINDING_OBJAVAIL 0x0004
66 #define BINDING_ABORTED 0x0008
69 IBinding IBinding_iface
;
70 IInternetProtocolSink IInternetProtocolSink_iface
;
71 IInternetBindInfo IInternetBindInfo_iface
;
72 IWinInetHttpInfo IWinInetHttpInfo_iface
;
73 IServiceProvider IServiceProvider_iface
;
77 IBindStatusCallback
*callback
;
78 IServiceProvider
*service_provider
;
80 BindProtocol
*protocol
;
82 stgmed_buf_t
*stgmed_buf
;
83 stgmed_obj_t
*stgmed_obj
;
89 UINT clipboard_format
;
98 download_state_t download_state
;
104 CRITICAL_SECTION section
;
107 static void read_protocol_data(stgmed_buf_t
*stgmed_buf
)
113 do hres
= IInternetProtocolEx_Read(stgmed_buf
->protocol
, buf
, sizeof(buf
), &read
);
117 static void dump_BINDINFO(BINDINFO
*bi
)
119 static const char * const BINDINFOF_str
[] = {
121 "BINDINFOF_URLENCODESTGMEDDATA",
122 "BINDINFOF_URLENCODEDEXTRAINFO"
125 static const char * const BINDVERB_str
[] = {
139 " %d, %08x, %d, %d\n"
145 bi
->cbSize
, debugstr_w(bi
->szExtraInfo
),
146 bi
->stgmedData
.tymed
, bi
->stgmedData
.u
.hGlobal
, bi
->stgmedData
.pUnkForRelease
,
147 bi
->grfBindInfoF
> BINDINFOF_URLENCODEDEXTRAINFO
148 ? "unknown" : BINDINFOF_str
[bi
->grfBindInfoF
],
149 bi
->dwBindVerb
> BINDVERB_CUSTOM
150 ? "unknown" : BINDVERB_str
[bi
->dwBindVerb
],
151 debugstr_w(bi
->szCustomVerb
),
152 bi
->cbstgmedData
, bi
->dwOptions
, bi
->dwOptionsFlags
, bi
->dwCodePage
,
153 bi
->securityAttributes
.nLength
,
154 bi
->securityAttributes
.lpSecurityDescriptor
,
155 bi
->securityAttributes
.bInheritHandle
,
156 debugstr_guid(&bi
->iid
),
157 bi
->pUnk
, bi
->dwReserved
161 static void mime_available(Binding
*This
, LPCWSTR mime
)
163 heap_free(This
->mime
);
164 This
->mime
= heap_strdupW(mime
);
166 if(!This
->mime
|| !This
->report_mime
)
169 IBindStatusCallback_OnProgress(This
->callback
, 0, 0, BINDSTATUS_MIMETYPEAVAILABLE
, This
->mime
);
171 This
->clipboard_format
= RegisterClipboardFormatW(This
->mime
);
174 static void stop_binding(Binding
*binding
, HRESULT hres
, LPCWSTR str
)
176 if(binding
->state
& BINDING_LOCKED
) {
177 IInternetProtocolEx_UnlockRequest(&binding
->protocol
->IInternetProtocolEx_iface
);
178 binding
->state
&= ~BINDING_LOCKED
;
181 if(!(binding
->state
& BINDING_STOPPED
)) {
182 binding
->state
|= BINDING_STOPPED
;
184 binding
->hres
= hres
;
185 IBindStatusCallback_OnStopBinding(binding
->callback
, hres
, str
);
189 static LPWSTR
get_mime_clsid(LPCWSTR mime
, CLSID
*clsid
)
191 LPWSTR key_name
, ret
;
192 DWORD res
, type
, size
;
197 static const WCHAR mime_keyW
[] =
198 {'M','I','M','E','\\','D','a','t','a','b','a','s','e','\\',
199 'C','o','n','t','e','n','t',' ','T','y','p','e','\\'};
201 len
= lstrlenW(mime
)+1;
202 key_name
= heap_alloc(sizeof(mime_keyW
) + len
*sizeof(WCHAR
));
203 memcpy(key_name
, mime_keyW
, sizeof(mime_keyW
));
204 lstrcpyW(key_name
+ ARRAY_SIZE(mime_keyW
), mime
);
206 res
= RegOpenKeyW(HKEY_CLASSES_ROOT
, key_name
, &hkey
);
208 if(res
!= ERROR_SUCCESS
) {
209 WARN("Could not open MIME key: %x\n", res
);
213 size
= 50*sizeof(WCHAR
);
214 ret
= heap_alloc(size
);
215 res
= RegQueryValueExW(hkey
, L
"CLSID", NULL
, &type
, (BYTE
*)ret
, &size
);
217 if(res
!= ERROR_SUCCESS
) {
218 WARN("Could not get CLSID: %08x\n", res
);
223 hres
= CLSIDFromString(ret
, clsid
);
225 WARN("Could not parse CLSID: %08x\n", hres
);
233 static void load_doc_mon(Binding
*binding
, IPersistMoniker
*persist
)
238 hres
= CreateAsyncBindCtxEx(binding
->bctx
, 0, NULL
, NULL
, &bctx
, 0);
240 WARN("CreateAsyncBindCtxEx failed: %08x\n", hres
);
244 IBindCtx_RevokeObjectParam(bctx
, bscb_holderW
);
245 IBindCtx_RegisterObjectParam(bctx
, cbinding_contextW
, (IUnknown
*)&binding
->IBinding_iface
);
247 hres
= IPersistMoniker_Load(persist
, binding
->download_state
== END_DOWNLOAD
, binding
->mon
, bctx
, 0x12);
248 IBindCtx_RevokeObjectParam(bctx
, cbinding_contextW
);
249 IBindCtx_Release(bctx
);
251 FIXME("Load failed: %08x\n", hres
);
254 static HRESULT
create_mime_object(Binding
*binding
, const CLSID
*clsid
, LPCWSTR clsid_str
)
256 IPersistMoniker
*persist
;
259 hres
= CoCreateInstance(clsid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
260 &binding
->iid
, (void**)&binding
->obj
);
262 WARN("CoCreateInstance failed: %08x\n", hres
);
263 return INET_E_CANNOT_INSTANTIATE_OBJECT
;
266 binding
->state
|= BINDING_OBJAVAIL
;
268 hres
= IUnknown_QueryInterface(binding
->obj
, &IID_IPersistMoniker
, (void**)&persist
);
269 if(SUCCEEDED(hres
)) {
272 hres
= IPersistMoniker_QueryInterface(persist
, &IID_IMonikerProp
, (void**)&prop
);
273 if(SUCCEEDED(hres
)) {
274 IMonikerProp_PutProperty(prop
, MIMETYPEPROP
, binding
->mime
);
275 IMonikerProp_PutProperty(prop
, CLASSIDPROP
, clsid_str
);
276 IMonikerProp_Release(prop
);
279 load_doc_mon(binding
, persist
);
281 IPersistMoniker_Release(persist
);
283 FIXME("Could not get IPersistMoniker: %08x\n", hres
);
284 /* FIXME: Try query IPersistFile */
287 IBindStatusCallback_OnObjectAvailable(binding
->callback
, &binding
->iid
, binding
->obj
);
292 static void create_object(Binding
*binding
)
299 FIXME("MIME not available\n");
303 if((clsid_str
= get_mime_clsid(binding
->mime
, &clsid
)))
304 IBindStatusCallback_OnProgress(binding
->callback
, 0, 0, BINDSTATUS_CLASSIDAVAILABLE
, clsid_str
);
306 IBindStatusCallback_OnProgress(binding
->callback
, 0, 0, BINDSTATUS_BEGINSYNCOPERATION
, NULL
);
309 hres
= create_mime_object(binding
, &clsid
, clsid_str
);
310 heap_free(clsid_str
);
312 FIXME("Could not find object for MIME %s\n", debugstr_w(binding
->mime
));
313 hres
= REGDB_E_CLASSNOTREG
;
316 IBindStatusCallback_OnProgress(binding
->callback
, 0, 0, BINDSTATUS_ENDSYNCOPERATION
, NULL
);
317 binding
->clsid
= CLSID_NULL
;
319 stop_binding(binding
, hres
, NULL
);
321 IInternetProtocolEx_Terminate(&binding
->protocol
->IInternetProtocolEx_iface
, 0);
324 static void cache_file_available(Binding
*This
, const WCHAR
*file_name
)
326 heap_free(This
->stgmed_buf
->cache_file
);
327 This
->stgmed_buf
->cache_file
= heap_strdupW(file_name
);
329 if(This
->use_cache_file
) {
330 This
->stgmed_buf
->file
= CreateFileW(file_name
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
,
331 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
332 if(This
->stgmed_buf
->file
== INVALID_HANDLE_VALUE
)
333 WARN("CreateFile failed: %u\n", GetLastError());
337 static inline stgmed_buf_t
*impl_from_IUnknown(IUnknown
*iface
)
339 return CONTAINING_RECORD(iface
, stgmed_buf_t
, IUnknown_iface
);
342 static HRESULT WINAPI
StgMedUnk_QueryInterface(IUnknown
*iface
, REFIID riid
, void **ppv
)
344 stgmed_buf_t
*This
= impl_from_IUnknown(iface
);
348 if(IsEqualGUID(riid
, &IID_IUnknown
)) {
349 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
351 *ppv
= &This
->IUnknown_iface
;
352 IUnknown_AddRef(&This
->IUnknown_iface
);
356 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
357 return E_NOINTERFACE
;
360 static ULONG WINAPI
StgMedUnk_AddRef(IUnknown
*iface
)
362 stgmed_buf_t
*This
= impl_from_IUnknown(iface
);
363 LONG ref
= InterlockedIncrement(&This
->ref
);
365 TRACE("(%p) ref=%d\n", This
, ref
);
370 static ULONG WINAPI
StgMedUnk_Release(IUnknown
*iface
)
372 stgmed_buf_t
*This
= impl_from_IUnknown(iface
);
373 LONG ref
= InterlockedDecrement(&This
->ref
);
375 TRACE("(%p) ref=%d\n", This
, ref
);
378 if(This
->file
!= INVALID_HANDLE_VALUE
)
379 CloseHandle(This
->file
);
380 IInternetProtocolEx_Release(This
->protocol
);
381 heap_free(This
->cache_file
);
384 URLMON_UnlockModule();
390 static const IUnknownVtbl StgMedUnkVtbl
= {
391 StgMedUnk_QueryInterface
,
396 static stgmed_buf_t
*create_stgmed_buf(IInternetProtocolEx
*protocol
)
398 stgmed_buf_t
*ret
= heap_alloc(sizeof(*ret
));
400 ret
->IUnknown_iface
.lpVtbl
= &StgMedUnkVtbl
;
402 ret
->file
= INVALID_HANDLE_VALUE
;
404 ret
->cache_file
= NULL
;
406 IInternetProtocolEx_AddRef(protocol
);
407 ret
->protocol
= protocol
;
415 stgmed_obj_t stgmed_obj
;
416 IStream IStream_iface
;
423 static inline ProtocolStream
*impl_from_IStream(IStream
*iface
)
425 return CONTAINING_RECORD(iface
, ProtocolStream
, IStream_iface
);
428 static HRESULT WINAPI
ProtocolStream_QueryInterface(IStream
*iface
,
429 REFIID riid
, void **ppv
)
431 ProtocolStream
*This
= impl_from_IStream(iface
);
435 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
436 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
437 *ppv
= &This
->IStream_iface
;
438 }else if(IsEqualGUID(&IID_ISequentialStream
, riid
)) {
439 TRACE("(%p)->(IID_ISequentialStream %p)\n", This
, ppv
);
440 *ppv
= &This
->IStream_iface
;
441 }else if(IsEqualGUID(&IID_IStream
, riid
)) {
442 TRACE("(%p)->(IID_IStream %p)\n", This
, ppv
);
443 *ppv
= &This
->IStream_iface
;
447 IStream_AddRef(&This
->IStream_iface
);
451 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
452 return E_NOINTERFACE
;
455 static ULONG WINAPI
ProtocolStream_AddRef(IStream
*iface
)
457 ProtocolStream
*This
= impl_from_IStream(iface
);
458 LONG ref
= InterlockedIncrement(&This
->ref
);
460 TRACE("(%p) ref=%d\n", This
, ref
);
465 static ULONG WINAPI
ProtocolStream_Release(IStream
*iface
)
467 ProtocolStream
*This
= impl_from_IStream(iface
);
468 LONG ref
= InterlockedDecrement(&This
->ref
);
470 TRACE("(%p) ref=%d\n", This
, ref
);
473 IUnknown_Release(&This
->buf
->IUnknown_iface
);
476 URLMON_UnlockModule();
482 static HRESULT WINAPI
ProtocolStream_Read(IStream
*iface
, void *pv
,
483 ULONG cb
, ULONG
*pcbRead
)
485 ProtocolStream
*This
= impl_from_IStream(iface
);
489 TRACE("(%p)->(%p %d %p)\n", This
, pv
, cb
, pcbRead
);
491 if(This
->buf
->file
== INVALID_HANDLE_VALUE
) {
492 hres
= This
->buf
->hres
= IInternetProtocolEx_Read(This
->buf
->protocol
, (PBYTE
)pv
, cb
, &read
);
494 hres
= ReadFile(This
->buf
->file
, pv
, cb
, &read
, NULL
) ? S_OK
: INET_E_DOWNLOAD_FAILURE
;
500 if(hres
== E_PENDING
)
502 else if(FAILED(hres
))
503 FIXME("Read failed: %08x\n", hres
);
505 return read
? S_OK
: S_FALSE
;
508 static HRESULT WINAPI
ProtocolStream_Write(IStream
*iface
, const void *pv
,
509 ULONG cb
, ULONG
*pcbWritten
)
511 ProtocolStream
*This
= impl_from_IStream(iface
);
513 TRACE("(%p)->(%p %d %p)\n", This
, pv
, cb
, pcbWritten
);
515 return STG_E_ACCESSDENIED
;
518 static HRESULT WINAPI
ProtocolStream_Seek(IStream
*iface
, LARGE_INTEGER dlibMove
,
519 DWORD dwOrigin
, ULARGE_INTEGER
*plibNewPosition
)
521 ProtocolStream
*This
= impl_from_IStream(iface
);
522 LARGE_INTEGER new_pos
;
525 TRACE("(%p)->(%d %08x %p)\n", This
, dlibMove
.u
.LowPart
, dwOrigin
, plibNewPosition
);
527 if(This
->buf
->file
== INVALID_HANDLE_VALUE
) {
528 /* We should probably call protocol handler's Seek. */
529 FIXME("no cache file, not supported\n");
534 case STREAM_SEEK_SET
:
537 case STREAM_SEEK_CUR
:
538 method
= FILE_CURRENT
;
540 case STREAM_SEEK_END
:
544 WARN("Invalid origin %x\n", dwOrigin
);
548 if(!SetFilePointerEx(This
->buf
->file
, dlibMove
, &new_pos
, method
)) {
549 FIXME("SetFilePointerEx failed: %u\n", GetLastError());
554 plibNewPosition
->QuadPart
= new_pos
.QuadPart
;
558 static HRESULT WINAPI
ProtocolStream_SetSize(IStream
*iface
, ULARGE_INTEGER libNewSize
)
560 ProtocolStream
*This
= impl_from_IStream(iface
);
561 FIXME("(%p)->(%d)\n", This
, libNewSize
.u
.LowPart
);
565 static HRESULT WINAPI
ProtocolStream_CopyTo(IStream
*iface
, IStream
*pstm
,
566 ULARGE_INTEGER cb
, ULARGE_INTEGER
*pcbRead
, ULARGE_INTEGER
*pcbWritten
)
568 ProtocolStream
*This
= impl_from_IStream(iface
);
569 FIXME("(%p)->(%p %d %p %p)\n", This
, pstm
, cb
.u
.LowPart
, pcbRead
, pcbWritten
);
573 static HRESULT WINAPI
ProtocolStream_Commit(IStream
*iface
, DWORD grfCommitFlags
)
575 ProtocolStream
*This
= impl_from_IStream(iface
);
577 TRACE("(%p)->(%08x)\n", This
, grfCommitFlags
);
582 static HRESULT WINAPI
ProtocolStream_Revert(IStream
*iface
)
584 ProtocolStream
*This
= impl_from_IStream(iface
);
586 TRACE("(%p)\n", This
);
591 static HRESULT WINAPI
ProtocolStream_LockRegion(IStream
*iface
, ULARGE_INTEGER libOffset
,
592 ULARGE_INTEGER cb
, DWORD dwLockType
)
594 ProtocolStream
*This
= impl_from_IStream(iface
);
595 FIXME("(%p)->(%d %d %d)\n", This
, libOffset
.u
.LowPart
, cb
.u
.LowPart
, dwLockType
);
599 static HRESULT WINAPI
ProtocolStream_UnlockRegion(IStream
*iface
,
600 ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
)
602 ProtocolStream
*This
= impl_from_IStream(iface
);
603 FIXME("(%p)->(%d %d %d)\n", This
, libOffset
.u
.LowPart
, cb
.u
.LowPart
, dwLockType
);
607 static HRESULT WINAPI
ProtocolStream_Stat(IStream
*iface
, STATSTG
*pstatstg
,
610 ProtocolStream
*This
= impl_from_IStream(iface
);
611 TRACE("(%p)->(%p %08x)\n", This
, pstatstg
, dwStatFlag
);
616 memset(pstatstg
, 0, sizeof(STATSTG
));
618 if(!(dwStatFlag
&STATFLAG_NONAME
) && This
->buf
->cache_file
) {
619 pstatstg
->pwcsName
= CoTaskMemAlloc((lstrlenW(This
->buf
->cache_file
)+1)*sizeof(WCHAR
));
620 if(!pstatstg
->pwcsName
)
621 return STG_E_INSUFFICIENTMEMORY
;
623 lstrcpyW(pstatstg
->pwcsName
, This
->buf
->cache_file
);
626 pstatstg
->type
= STGTY_STREAM
;
627 if(This
->buf
->file
!= INVALID_HANDLE_VALUE
) {
628 GetFileSizeEx(This
->buf
->file
, (PLARGE_INTEGER
)&pstatstg
->cbSize
);
629 GetFileTime(This
->buf
->file
, &pstatstg
->ctime
, &pstatstg
->atime
, &pstatstg
->mtime
);
630 if(pstatstg
->cbSize
.QuadPart
)
631 pstatstg
->grfMode
= GENERIC_READ
;
637 static HRESULT WINAPI
ProtocolStream_Clone(IStream
*iface
, IStream
**ppstm
)
639 ProtocolStream
*This
= impl_from_IStream(iface
);
640 FIXME("(%p)->(%p)\n", This
, ppstm
);
644 static const IStreamVtbl ProtocolStreamVtbl
= {
645 ProtocolStream_QueryInterface
,
646 ProtocolStream_AddRef
,
647 ProtocolStream_Release
,
649 ProtocolStream_Write
,
651 ProtocolStream_SetSize
,
652 ProtocolStream_CopyTo
,
653 ProtocolStream_Commit
,
654 ProtocolStream_Revert
,
655 ProtocolStream_LockRegion
,
656 ProtocolStream_UnlockRegion
,
661 static void stgmed_stream_release(stgmed_obj_t
*obj
)
663 ProtocolStream
*stream
= (ProtocolStream
*)obj
;
664 IStream_Release(&stream
->IStream_iface
);
667 static HRESULT
stgmed_stream_fill_stgmed(stgmed_obj_t
*obj
, STGMEDIUM
*stgmed
)
669 ProtocolStream
*stream
= (ProtocolStream
*)obj
;
671 stgmed
->tymed
= TYMED_ISTREAM
;
672 stgmed
->u
.pstm
= &stream
->IStream_iface
;
673 stgmed
->pUnkForRelease
= &stream
->buf
->IUnknown_iface
;
678 static HRESULT
stgmed_stream_get_result(stgmed_obj_t
*obj
, DWORD bindf
, void **result
)
680 ProtocolStream
*stream
= (ProtocolStream
*)obj
;
682 if(!(bindf
& BINDF_ASYNCHRONOUS
) && stream
->buf
->file
== INVALID_HANDLE_VALUE
683 && stream
->buf
->hres
!= S_FALSE
)
684 return INET_E_DATA_NOT_AVAILABLE
;
686 IStream_AddRef(&stream
->IStream_iface
);
687 *result
= &stream
->IStream_iface
;
691 static const stgmed_obj_vtbl stgmed_stream_vtbl
= {
692 stgmed_stream_release
,
693 stgmed_stream_fill_stgmed
,
694 stgmed_stream_get_result
698 stgmed_obj_t stgmed_obj
;
702 static stgmed_obj_t
*create_stgmed_stream(stgmed_buf_t
*buf
)
704 ProtocolStream
*ret
= heap_alloc(sizeof(ProtocolStream
));
706 ret
->stgmed_obj
.vtbl
= &stgmed_stream_vtbl
;
707 ret
->IStream_iface
.lpVtbl
= &ProtocolStreamVtbl
;
710 IUnknown_AddRef(&buf
->IUnknown_iface
);
715 return &ret
->stgmed_obj
;
718 static void stgmed_file_release(stgmed_obj_t
*obj
)
720 stgmed_file_obj_t
*file_obj
= (stgmed_file_obj_t
*)obj
;
722 IUnknown_Release(&file_obj
->buf
->IUnknown_iface
);
726 static HRESULT
stgmed_file_fill_stgmed(stgmed_obj_t
*obj
, STGMEDIUM
*stgmed
)
728 stgmed_file_obj_t
*file_obj
= (stgmed_file_obj_t
*)obj
;
730 if(!file_obj
->buf
->cache_file
) {
731 WARN("cache_file not set\n");
732 return INET_E_DATA_NOT_AVAILABLE
;
735 read_protocol_data(file_obj
->buf
);
737 stgmed
->tymed
= TYMED_FILE
;
738 stgmed
->u
.lpszFileName
= file_obj
->buf
->cache_file
;
739 stgmed
->pUnkForRelease
= &file_obj
->buf
->IUnknown_iface
;
744 static HRESULT
stgmed_file_get_result(stgmed_obj_t
*obj
, DWORD bindf
, void **result
)
746 return bindf
& BINDF_ASYNCHRONOUS
? MK_S_ASYNCHRONOUS
: S_OK
;
749 static const stgmed_obj_vtbl stgmed_file_vtbl
= {
751 stgmed_file_fill_stgmed
,
752 stgmed_file_get_result
755 static stgmed_obj_t
*create_stgmed_file(stgmed_buf_t
*buf
)
757 stgmed_file_obj_t
*ret
= heap_alloc(sizeof(*ret
));
759 ret
->stgmed_obj
.vtbl
= &stgmed_file_vtbl
;
761 IUnknown_AddRef(&buf
->IUnknown_iface
);
764 return &ret
->stgmed_obj
;
767 static inline Binding
*impl_from_IBinding(IBinding
*iface
)
769 return CONTAINING_RECORD(iface
, Binding
, IBinding_iface
);
772 static HRESULT WINAPI
Binding_QueryInterface(IBinding
*iface
, REFIID riid
, void **ppv
)
774 Binding
*This
= impl_from_IBinding(iface
);
778 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
779 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
780 *ppv
= &This
->IBinding_iface
;
781 }else if(IsEqualGUID(&IID_IBinding
, riid
)) {
782 TRACE("(%p)->(IID_IBinding %p)\n", This
, ppv
);
783 *ppv
= &This
->IBinding_iface
;
784 }else if(IsEqualGUID(&IID_IInternetProtocolSink
, riid
)) {
785 TRACE("(%p)->(IID_IInternetProtocolSink %p)\n", This
, ppv
);
786 *ppv
= &This
->IInternetProtocolSink_iface
;
787 }else if(IsEqualGUID(&IID_IInternetBindInfo
, riid
)) {
788 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This
, ppv
);
789 *ppv
= &This
->IInternetBindInfo_iface
;
790 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
791 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
792 *ppv
= &This
->IServiceProvider_iface
;
793 }else if(IsEqualGUID(&IID_IWinInetInfo
, riid
)) {
794 IWinInetInfo
*wininet_info
;
797 TRACE("(%p)->(IID_IWinInetInfo %p)\n", This
, ppv
);
799 /* NOTE: This violidates COM rules, but tests prove that we should do it */
800 hres
= IInternetProtocolEx_QueryInterface(&This
->protocol
->IInternetProtocolEx_iface
,
801 &IID_IWinInetInfo
, (void**)&wininet_info
);
802 if(SUCCEEDED(hres
)) {
803 IWinInetInfo_Release(wininet_info
);
804 *ppv
= &This
->IWinInetHttpInfo_iface
;
806 }else if(IsEqualGUID(&IID_IWinInetHttpInfo
, riid
)) {
807 IWinInetHttpInfo
*http_info
;
810 TRACE("(%p)->(IID_IWinInetHttpInfo %p)\n", This
, ppv
);
812 /* NOTE: This violidates COM rules, but tests prove that we should do it */
813 hres
= IInternetProtocolEx_QueryInterface(&This
->protocol
->IInternetProtocolEx_iface
,
814 &IID_IWinInetHttpInfo
, (void**)&http_info
);
815 if(SUCCEEDED(hres
)) {
816 IWinInetHttpInfo_Release(http_info
);
817 *ppv
= &This
->IWinInetHttpInfo_iface
;
822 IBinding_AddRef(&This
->IBinding_iface
);
826 WARN("Unsupported interface %s\n", debugstr_guid(riid
));
827 return E_NOINTERFACE
;
830 static ULONG WINAPI
Binding_AddRef(IBinding
*iface
)
832 Binding
*This
= impl_from_IBinding(iface
);
833 LONG ref
= InterlockedIncrement(&This
->ref
);
835 TRACE("(%p) ref=%d\n", This
, ref
);
840 static ULONG WINAPI
Binding_Release(IBinding
*iface
)
842 Binding
*This
= impl_from_IBinding(iface
);
843 LONG ref
= InterlockedDecrement(&This
->ref
);
845 TRACE("(%p) ref=%d\n", This
, ref
);
849 release_notif_hwnd(This
->notif_hwnd
);
851 IMoniker_Release(This
->mon
);
853 IBindStatusCallback_Release(This
->callback
);
855 IInternetProtocolEx_Release(&This
->protocol
->IInternetProtocolEx_iface
);
856 if(This
->service_provider
)
857 IServiceProvider_Release(This
->service_provider
);
859 IUnknown_Release(&This
->stgmed_buf
->IUnknown_iface
);
861 This
->stgmed_obj
->vtbl
->release(This
->stgmed_obj
);
863 IUnknown_Release(This
->obj
);
865 IBindCtx_Release(This
->bctx
);
867 ReleaseBindInfo(&This
->bindinfo
);
868 This
->section
.DebugInfo
->Spare
[0] = 0;
869 DeleteCriticalSection(&This
->section
);
870 SysFreeString(This
->url
);
871 heap_free(This
->mime
);
872 heap_free(This
->redirect_url
);
875 URLMON_UnlockModule();
881 static HRESULT WINAPI
Binding_Abort(IBinding
*iface
)
883 Binding
*This
= impl_from_IBinding(iface
);
886 TRACE("(%p)\n", This
);
888 if(This
->state
& BINDING_ABORTED
)
891 hres
= IInternetProtocolEx_Abort(&This
->protocol
->IInternetProtocolEx_iface
, E_ABORT
,
896 This
->state
|= BINDING_ABORTED
;
900 static HRESULT WINAPI
Binding_Suspend(IBinding
*iface
)
902 Binding
*This
= impl_from_IBinding(iface
);
903 FIXME("(%p)\n", This
);
907 static HRESULT WINAPI
Binding_Resume(IBinding
*iface
)
909 Binding
*This
= impl_from_IBinding(iface
);
910 FIXME("(%p)\n", This
);
914 static HRESULT WINAPI
Binding_SetPriority(IBinding
*iface
, LONG nPriority
)
916 Binding
*This
= impl_from_IBinding(iface
);
917 FIXME("(%p)->(%d)\n", This
, nPriority
);
921 static HRESULT WINAPI
Binding_GetPriority(IBinding
*iface
, LONG
*pnPriority
)
923 Binding
*This
= impl_from_IBinding(iface
);
924 FIXME("(%p)->(%p)\n", This
, pnPriority
);
928 static HRESULT WINAPI
Binding_GetBindResult(IBinding
*iface
, CLSID
*pclsidProtocol
,
929 DWORD
*pdwResult
, LPOLESTR
*pszResult
, DWORD
*pdwReserved
)
931 Binding
*This
= impl_from_IBinding(iface
);
933 TRACE("(%p)->(%p %p %p %p)\n", This
, pclsidProtocol
, pdwResult
, pszResult
, pdwReserved
);
935 if(!pdwResult
|| !pszResult
|| pdwReserved
)
938 if(!(This
->state
& BINDING_STOPPED
)) {
939 *pclsidProtocol
= CLSID_NULL
;
945 *pclsidProtocol
= This
->hres
==S_OK
? CLSID_NULL
: This
->clsid
;
946 *pdwResult
= This
->hres
;
951 static const IBindingVtbl BindingVtbl
= {
952 Binding_QueryInterface
,
960 Binding_GetBindResult
963 static Binding
*get_bctx_binding(IBindCtx
*bctx
)
969 hres
= IBindCtx_GetObjectParam(bctx
, cbinding_contextW
, &unk
);
973 hres
= IUnknown_QueryInterface(unk
, &IID_IBinding
, (void**)&binding
);
974 IUnknown_Release(unk
);
978 if (binding
->lpVtbl
!= &BindingVtbl
)
980 return impl_from_IBinding(binding
);
983 static inline Binding
*impl_from_IInternetProtocolSink(IInternetProtocolSink
*iface
)
985 return CONTAINING_RECORD(iface
, Binding
, IInternetProtocolSink_iface
);
988 static HRESULT WINAPI
InternetProtocolSink_QueryInterface(IInternetProtocolSink
*iface
,
989 REFIID riid
, void **ppv
)
991 Binding
*This
= impl_from_IInternetProtocolSink(iface
);
992 return IBinding_QueryInterface(&This
->IBinding_iface
, riid
, ppv
);
995 static ULONG WINAPI
InternetProtocolSink_AddRef(IInternetProtocolSink
*iface
)
997 Binding
*This
= impl_from_IInternetProtocolSink(iface
);
998 return IBinding_AddRef(&This
->IBinding_iface
);
1001 static ULONG WINAPI
InternetProtocolSink_Release(IInternetProtocolSink
*iface
)
1003 Binding
*This
= impl_from_IInternetProtocolSink(iface
);
1004 return IBinding_Release(&This
->IBinding_iface
);
1007 static HRESULT WINAPI
InternetProtocolSink_Switch(IInternetProtocolSink
*iface
,
1008 PROTOCOLDATA
*pProtocolData
)
1010 Binding
*This
= impl_from_IInternetProtocolSink(iface
);
1012 WARN("(%p)->(%p)\n", This
, pProtocolData
);
1017 static void on_progress(Binding
*This
, ULONG progress
, ULONG progress_max
,
1018 ULONG status_code
, LPCWSTR status_text
)
1020 IBindStatusCallback_OnProgress(This
->callback
, progress
, progress_max
,
1021 status_code
, status_text
);
1024 static HRESULT WINAPI
InternetProtocolSink_ReportProgress(IInternetProtocolSink
*iface
,
1025 ULONG ulStatusCode
, LPCWSTR szStatusText
)
1027 Binding
*This
= impl_from_IInternetProtocolSink(iface
);
1029 TRACE("(%p)->(%s %s)\n", This
, debugstr_bindstatus(ulStatusCode
), debugstr_w(szStatusText
));
1031 switch(ulStatusCode
) {
1032 case BINDSTATUS_FINDINGRESOURCE
:
1033 on_progress(This
, 0, 0, BINDSTATUS_FINDINGRESOURCE
, szStatusText
);
1035 case BINDSTATUS_CONNECTING
:
1036 on_progress(This
, 0, 0, BINDSTATUS_CONNECTING
, szStatusText
);
1038 case BINDSTATUS_REDIRECTING
:
1039 heap_free(This
->redirect_url
);
1040 This
->redirect_url
= heap_strdupW(szStatusText
);
1041 on_progress(This
, 0, 0, BINDSTATUS_REDIRECTING
, szStatusText
);
1043 case BINDSTATUS_BEGINDOWNLOADDATA
:
1045 case BINDSTATUS_SENDINGREQUEST
:
1046 on_progress(This
, 0, 0, BINDSTATUS_SENDINGREQUEST
, szStatusText
);
1048 case BINDSTATUS_PROTOCOLCLASSID
:
1049 CLSIDFromString(szStatusText
, &This
->clsid
);
1051 case BINDSTATUS_MIMETYPEAVAILABLE
:
1052 case BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE
:
1053 mime_available(This
, szStatusText
);
1055 case BINDSTATUS_CACHEFILENAMEAVAILABLE
:
1056 cache_file_available(This
, szStatusText
);
1058 case BINDSTATUS_DECODING
:
1059 IBindStatusCallback_OnProgress(This
->callback
, 0, 0, BINDSTATUS_DECODING
, szStatusText
);
1061 case BINDSTATUS_LOADINGMIMEHANDLER
:
1062 on_progress(This
, 0, 0, BINDSTATUS_LOADINGMIMEHANDLER
, szStatusText
);
1064 case BINDSTATUS_DIRECTBIND
: /* FIXME: Handle BINDSTATUS_DIRECTBIND in BindProtocol */
1065 This
->report_mime
= FALSE
;
1067 case BINDSTATUS_ACCEPTRANGES
:
1070 FIXME("Unhandled status code %d\n", ulStatusCode
);
1077 static void report_data(Binding
*This
, DWORD bscf
, ULONG progress
, ULONG progress_max
)
1079 FORMATETC formatetc
= {0, NULL
, 1, -1, TYMED_ISTREAM
};
1080 BOOL sent_begindownloaddata
= FALSE
;
1082 TRACE("(%p)->(%d %u %u)\n", This
, bscf
, progress
, progress_max
);
1084 if(This
->download_state
== END_DOWNLOAD
|| (This
->state
& BINDING_ABORTED
)) {
1085 read_protocol_data(This
->stgmed_buf
);
1089 if(This
->state
& BINDING_STOPPED
)
1092 if(This
->stgmed_buf
->file
!= INVALID_HANDLE_VALUE
)
1093 read_protocol_data(This
->stgmed_buf
);
1095 if(This
->download_state
== BEFORE_DOWNLOAD
) {
1096 This
->download_state
= DOWNLOADING
;
1097 sent_begindownloaddata
= TRUE
;
1098 IBindStatusCallback_OnProgress(This
->callback
, progress
, progress_max
,
1099 BINDSTATUS_BEGINDOWNLOADDATA
, This
->url
);
1101 if(This
->stgmed_buf
->cache_file
)
1102 IBindStatusCallback_OnProgress(This
->callback
, progress
, progress_max
,
1103 BINDSTATUS_CACHEFILENAMEAVAILABLE
, This
->stgmed_buf
->cache_file
);
1106 if(This
->stgmed_buf
->hres
== S_FALSE
|| (bscf
& BSCF_LASTDATANOTIFICATION
)) {
1107 This
->download_state
= END_DOWNLOAD
;
1108 IBindStatusCallback_OnProgress(This
->callback
, progress
, progress_max
,
1109 BINDSTATUS_ENDDOWNLOADDATA
, This
->url
);
1110 }else if(!sent_begindownloaddata
) {
1111 IBindStatusCallback_OnProgress(This
->callback
, progress
, progress_max
,
1112 BINDSTATUS_DOWNLOADINGDATA
, This
->url
);
1115 if(This
->state
& (BINDING_STOPPED
|BINDING_ABORTED
))
1118 if(This
->to_object
) {
1119 if(!(This
->state
& BINDING_OBJAVAIL
)) {
1120 IBinding_AddRef(&This
->IBinding_iface
);
1121 create_object(This
);
1122 IBinding_Release(&This
->IBinding_iface
);
1128 if(!(This
->state
& BINDING_LOCKED
)) {
1129 hres
= IInternetProtocolEx_LockRequest(&This
->protocol
->IInternetProtocolEx_iface
, 0);
1131 This
->state
|= BINDING_LOCKED
;
1134 hres
= This
->stgmed_obj
->vtbl
->fill_stgmed(This
->stgmed_obj
, &stgmed
);
1136 stop_binding(This
, hres
, NULL
);
1140 formatetc
.tymed
= stgmed
.tymed
;
1141 formatetc
.cfFormat
= This
->clipboard_format
;
1143 hres
= IBindStatusCallback_OnDataAvailable(This
->callback
, bscf
, progress
,
1144 &formatetc
, &stgmed
);
1146 if(This
->download_state
!= END_DOWNLOAD
) {
1147 This
->download_state
= END_DOWNLOAD
;
1148 IBindStatusCallback_OnProgress(This
->callback
, progress
, progress_max
,
1149 BINDSTATUS_ENDDOWNLOADDATA
, This
->url
);
1152 WARN("OnDataAvailable returned %x\n", hres
);
1153 stop_binding(This
, hres
, NULL
);
1157 if(This
->download_state
== END_DOWNLOAD
)
1158 stop_binding(This
, S_OK
, NULL
);
1162 static HRESULT WINAPI
InternetProtocolSink_ReportData(IInternetProtocolSink
*iface
,
1163 DWORD grfBSCF
, ULONG ulProgress
, ULONG ulProgressMax
)
1165 Binding
*This
= impl_from_IInternetProtocolSink(iface
);
1167 TRACE("(%p)->(%d %u %u)\n", This
, grfBSCF
, ulProgress
, ulProgressMax
);
1169 report_data(This
, grfBSCF
, ulProgress
, ulProgressMax
);
1173 static HRESULT WINAPI
InternetProtocolSink_ReportResult(IInternetProtocolSink
*iface
,
1174 HRESULT hrResult
, DWORD dwError
, LPCWSTR szResult
)
1176 Binding
*This
= impl_from_IInternetProtocolSink(iface
);
1178 TRACE("(%p)->(%08x %d %s)\n", This
, hrResult
, dwError
, debugstr_w(szResult
));
1180 stop_binding(This
, hrResult
, szResult
);
1182 IInternetProtocolEx_Terminate(&This
->protocol
->IInternetProtocolEx_iface
, 0);
1186 static const IInternetProtocolSinkVtbl InternetProtocolSinkVtbl
= {
1187 InternetProtocolSink_QueryInterface
,
1188 InternetProtocolSink_AddRef
,
1189 InternetProtocolSink_Release
,
1190 InternetProtocolSink_Switch
,
1191 InternetProtocolSink_ReportProgress
,
1192 InternetProtocolSink_ReportData
,
1193 InternetProtocolSink_ReportResult
1196 static inline Binding
*impl_from_IInternetBindInfo(IInternetBindInfo
*iface
)
1198 return CONTAINING_RECORD(iface
, Binding
, IInternetBindInfo_iface
);
1201 static HRESULT WINAPI
InternetBindInfo_QueryInterface(IInternetBindInfo
*iface
,
1202 REFIID riid
, void **ppv
)
1204 Binding
*This
= impl_from_IInternetBindInfo(iface
);
1205 return IBinding_QueryInterface(&This
->IBinding_iface
, riid
, ppv
);
1208 static ULONG WINAPI
InternetBindInfo_AddRef(IInternetBindInfo
*iface
)
1210 Binding
*This
= impl_from_IInternetBindInfo(iface
);
1211 return IBinding_AddRef(&This
->IBinding_iface
);
1214 static ULONG WINAPI
InternetBindInfo_Release(IInternetBindInfo
*iface
)
1216 Binding
*This
= impl_from_IInternetBindInfo(iface
);
1217 return IBinding_Release(&This
->IBinding_iface
);
1220 static HRESULT WINAPI
InternetBindInfo_GetBindInfo(IInternetBindInfo
*iface
,
1221 DWORD
*grfBINDF
, BINDINFO
*pbindinfo
)
1223 Binding
*This
= impl_from_IInternetBindInfo(iface
);
1225 TRACE("(%p)->(%p %p)\n", This
, grfBINDF
, pbindinfo
);
1227 *grfBINDF
= This
->bindf
;
1228 return CopyBindInfo(&This
->bindinfo
, pbindinfo
);
1231 static HRESULT WINAPI
InternetBindInfo_GetBindString(IInternetBindInfo
*iface
,
1232 ULONG ulStringType
, LPOLESTR
*ppwzStr
, ULONG cEl
, ULONG
*pcElFetched
)
1234 Binding
*This
= impl_from_IInternetBindInfo(iface
);
1236 TRACE("(%p)->(%d %p %d %p)\n", This
, ulStringType
, ppwzStr
, cEl
, pcElFetched
);
1238 switch(ulStringType
) {
1239 case BINDSTRING_ACCEPT_MIMES
: {
1240 if(!ppwzStr
|| !pcElFetched
)
1241 return E_INVALIDARG
;
1243 ppwzStr
[0] = CoTaskMemAlloc(sizeof(L
"*/*"));
1244 memcpy(ppwzStr
[0], L
"*/*", sizeof(L
"*/*"));
1248 case BINDSTRING_USER_AGENT
: {
1249 IInternetBindInfo
*bindinfo
= NULL
;
1252 hres
= IBindStatusCallback_QueryInterface(This
->callback
, &IID_IInternetBindInfo
,
1257 hres
= IInternetBindInfo_GetBindString(bindinfo
, ulStringType
, ppwzStr
,
1259 IInternetBindInfo_Release(bindinfo
);
1263 case BINDSTRING_URL
: {
1264 DWORD size
= (SysStringLen(This
->url
)+1) * sizeof(WCHAR
);
1266 if(!ppwzStr
|| !pcElFetched
)
1267 return E_INVALIDARG
;
1269 *ppwzStr
= CoTaskMemAlloc(size
);
1270 memcpy(*ppwzStr
, This
->url
, size
);
1276 FIXME("not supported string type %d\n", ulStringType
);
1280 static const IInternetBindInfoVtbl InternetBindInfoVtbl
= {
1281 InternetBindInfo_QueryInterface
,
1282 InternetBindInfo_AddRef
,
1283 InternetBindInfo_Release
,
1284 InternetBindInfo_GetBindInfo
,
1285 InternetBindInfo_GetBindString
1288 static inline Binding
*impl_from_IWinInetHttpInfo(IWinInetHttpInfo
*iface
)
1290 return CONTAINING_RECORD(iface
, Binding
, IWinInetHttpInfo_iface
);
1293 static HRESULT WINAPI
WinInetHttpInfo_QueryInterface(IWinInetHttpInfo
*iface
, REFIID riid
, void **ppv
)
1295 Binding
*This
= impl_from_IWinInetHttpInfo(iface
);
1296 return IBinding_QueryInterface(&This
->IBinding_iface
, riid
, ppv
);
1299 static ULONG WINAPI
WinInetHttpInfo_AddRef(IWinInetHttpInfo
*iface
)
1301 Binding
*This
= impl_from_IWinInetHttpInfo(iface
);
1302 return IBinding_AddRef(&This
->IBinding_iface
);
1305 static ULONG WINAPI
WinInetHttpInfo_Release(IWinInetHttpInfo
*iface
)
1307 Binding
*This
= impl_from_IWinInetHttpInfo(iface
);
1308 return IBinding_Release(&This
->IBinding_iface
);
1311 static HRESULT WINAPI
WinInetHttpInfo_QueryOption(IWinInetHttpInfo
*iface
, DWORD dwOption
,
1312 void *pBuffer
, DWORD
*pcbBuffer
)
1314 Binding
*This
= impl_from_IWinInetHttpInfo(iface
);
1315 IWinInetInfo
*wininet_info
;
1318 TRACE("(%p)->(%x %p %p)\n", This
, dwOption
, pBuffer
, pcbBuffer
);
1320 hres
= IInternetProtocolEx_QueryInterface(&This
->protocol
->IInternetProtocolEx_iface
,
1321 &IID_IWinInetInfo
, (void**)&wininet_info
);
1325 hres
= IWinInetInfo_QueryOption(wininet_info
, dwOption
, pBuffer
, pcbBuffer
);
1326 IWinInetInfo_Release(wininet_info
);
1330 static HRESULT WINAPI
WinInetHttpInfo_QueryInfo(IWinInetHttpInfo
*iface
, DWORD dwOption
,
1331 void *pBuffer
, DWORD
*pcbBuffer
, DWORD
*pdwFlags
, DWORD
*pdwReserved
)
1333 Binding
*This
= impl_from_IWinInetHttpInfo(iface
);
1334 IWinInetHttpInfo
*http_info
;
1337 TRACE("(%p)->(%x %p %p %p %p)\n", This
, dwOption
, pBuffer
, pcbBuffer
, pdwFlags
, pdwReserved
);
1339 hres
= IInternetProtocolEx_QueryInterface(&This
->protocol
->IInternetProtocolEx_iface
,
1340 &IID_IWinInetHttpInfo
, (void**)&http_info
);
1344 hres
= IWinInetHttpInfo_QueryInfo(http_info
, dwOption
, pBuffer
, pcbBuffer
, pdwFlags
, pdwReserved
);
1345 IWinInetHttpInfo_Release(http_info
);
1349 static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl
= {
1350 WinInetHttpInfo_QueryInterface
,
1351 WinInetHttpInfo_AddRef
,
1352 WinInetHttpInfo_Release
,
1353 WinInetHttpInfo_QueryOption
,
1354 WinInetHttpInfo_QueryInfo
1357 static inline Binding
*impl_from_IServiceProvider(IServiceProvider
*iface
)
1359 return CONTAINING_RECORD(iface
, Binding
, IServiceProvider_iface
);
1362 static HRESULT WINAPI
ServiceProvider_QueryInterface(IServiceProvider
*iface
,
1363 REFIID riid
, void **ppv
)
1365 Binding
*This
= impl_from_IServiceProvider(iface
);
1366 return IBinding_QueryInterface(&This
->IBinding_iface
, riid
, ppv
);
1369 static ULONG WINAPI
ServiceProvider_AddRef(IServiceProvider
*iface
)
1371 Binding
*This
= impl_from_IServiceProvider(iface
);
1372 return IBinding_AddRef(&This
->IBinding_iface
);
1375 static ULONG WINAPI
ServiceProvider_Release(IServiceProvider
*iface
)
1377 Binding
*This
= impl_from_IServiceProvider(iface
);
1378 return IBinding_Release(&This
->IBinding_iface
);
1381 static HRESULT WINAPI
ServiceProvider_QueryService(IServiceProvider
*iface
,
1382 REFGUID guidService
, REFIID riid
, void **ppv
)
1384 Binding
*This
= impl_from_IServiceProvider(iface
);
1387 TRACE("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
1389 if(This
->service_provider
) {
1390 hres
= IServiceProvider_QueryService(This
->service_provider
, guidService
,
1396 WARN("unknown service %s\n", debugstr_guid(guidService
));
1397 return E_NOINTERFACE
;
1400 static const IServiceProviderVtbl ServiceProviderVtbl
= {
1401 ServiceProvider_QueryInterface
,
1402 ServiceProvider_AddRef
,
1403 ServiceProvider_Release
,
1404 ServiceProvider_QueryService
1407 static HRESULT
get_callback(IBindCtx
*pbc
, IBindStatusCallback
**callback
)
1412 hres
= IBindCtx_GetObjectParam(pbc
, bscb_holderW
, &unk
);
1414 return create_default_callback(callback
);
1416 hres
= IUnknown_QueryInterface(unk
, &IID_IBindStatusCallback
, (void**)callback
);
1417 IUnknown_Release(unk
);
1421 static BOOL
is_urlmon_protocol(IUri
*uri
)
1426 hres
= IUri_GetScheme(uri
, &scheme
);
1431 case URL_SCHEME_FILE
:
1432 case URL_SCHEME_FTP
:
1433 case URL_SCHEME_GOPHER
:
1434 case URL_SCHEME_HTTP
:
1435 case URL_SCHEME_HTTPS
:
1443 static HRESULT
Binding_Create(IMoniker
*mon
, Binding
*binding_ctx
, IUri
*uri
, IBindCtx
*pbc
,
1444 BOOL to_obj
, REFIID riid
, Binding
**binding
)
1449 URLMON_LockModule();
1451 ret
= heap_alloc_zero(sizeof(Binding
));
1453 ret
->IBinding_iface
.lpVtbl
= &BindingVtbl
;
1454 ret
->IInternetProtocolSink_iface
.lpVtbl
= &InternetProtocolSinkVtbl
;
1455 ret
->IInternetBindInfo_iface
.lpVtbl
= &InternetBindInfoVtbl
;
1456 ret
->IWinInetHttpInfo_iface
.lpVtbl
= &WinInetHttpInfoVtbl
;
1457 ret
->IServiceProvider_iface
.lpVtbl
= &ServiceProviderVtbl
;
1461 ret
->to_object
= to_obj
;
1463 ret
->notif_hwnd
= get_notif_hwnd();
1464 ret
->report_mime
= !binding_ctx
;
1465 ret
->download_state
= BEFORE_DOWNLOAD
;
1468 IBindCtx_AddRef(pbc
);
1473 IMoniker_AddRef(mon
);
1477 ret
->bindinfo
.cbSize
= sizeof(BINDINFO
);
1479 InitializeCriticalSection(&ret
->section
);
1480 ret
->section
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": Binding.section");
1482 hres
= get_callback(pbc
, &ret
->callback
);
1484 WARN("Could not get IBindStatusCallback\n");
1485 IBinding_Release(&ret
->IBinding_iface
);
1489 IBindStatusCallback_QueryInterface(ret
->callback
, &IID_IServiceProvider
,
1490 (void**)&ret
->service_provider
);
1493 ret
->protocol
= binding_ctx
->protocol
;
1494 IInternetProtocolEx_AddRef(&ret
->protocol
->IInternetProtocolEx_iface
);
1496 hres
= create_binding_protocol(&ret
->protocol
);
1498 WARN("Could not get protocol handler\n");
1499 IBinding_Release(&ret
->IBinding_iface
);
1504 hres
= IBindStatusCallback_GetBindInfo(ret
->callback
, &ret
->bindf
, &ret
->bindinfo
);
1506 WARN("GetBindInfo failed: %08x\n", hres
);
1507 IBinding_Release(&ret
->IBinding_iface
);
1511 TRACE("bindf %08x\n", ret
->bindf
);
1512 dump_BINDINFO(&ret
->bindinfo
);
1514 ret
->bindf
|= BINDF_FROMURLMON
;
1516 ret
->bindinfo
.dwOptions
|= 0x100000;
1518 if(!(ret
->bindf
& BINDF_ASYNCHRONOUS
) || !(ret
->bindf
& BINDF_PULLDATA
)) {
1519 ret
->bindf
|= BINDF_NEEDFILE
;
1520 ret
->use_cache_file
= TRUE
;
1521 }else if(!is_urlmon_protocol(uri
)) {
1522 ret
->bindf
|= BINDF_NEEDFILE
;
1525 hres
= IUri_GetDisplayUri(uri
, &ret
->url
);
1527 IBinding_Release(&ret
->IBinding_iface
);
1532 ret
->stgmed_buf
= binding_ctx
->stgmed_buf
;
1533 IUnknown_AddRef(&ret
->stgmed_buf
->IUnknown_iface
);
1534 ret
->clipboard_format
= binding_ctx
->clipboard_format
;
1536 ret
->stgmed_buf
= create_stgmed_buf(&ret
->protocol
->IInternetProtocolEx_iface
);
1540 ret
->stgmed_obj
= NULL
;
1541 }else if(IsEqualGUID(&IID_IStream
, riid
)) {
1542 ret
->stgmed_obj
= create_stgmed_stream(ret
->stgmed_buf
);
1543 }else if(IsEqualGUID(&IID_IUnknown
, riid
)) {
1544 ret
->bindf
|= BINDF_NEEDFILE
;
1545 ret
->stgmed_obj
= create_stgmed_file(ret
->stgmed_buf
);
1547 FIXME("Unsupported riid %s\n", debugstr_guid(riid
));
1548 IBinding_Release(&ret
->IBinding_iface
);
1556 static HRESULT
start_binding(IMoniker
*mon
, Binding
*binding_ctx
, IUri
*uri
, IBindCtx
*pbc
,
1557 BOOL to_obj
, REFIID riid
, Binding
**ret
)
1559 Binding
*binding
= NULL
;
1563 hres
= Binding_Create(mon
, binding_ctx
, uri
, pbc
, to_obj
, riid
, &binding
);
1567 hres
= IBindStatusCallback_OnStartBinding(binding
->callback
, 0, &binding
->IBinding_iface
);
1569 WARN("OnStartBinding failed: %08x\n", hres
);
1570 if(hres
!= E_ABORT
&& hres
!= E_NOTIMPL
)
1571 hres
= INET_E_DOWNLOAD_FAILURE
;
1573 stop_binding(binding
, hres
, NULL
);
1574 IBinding_Release(&binding
->IBinding_iface
);
1579 set_binding_sink(binding
->protocol
, &binding
->IInternetProtocolSink_iface
,
1580 &binding
->IInternetBindInfo_iface
);
1581 if(binding_ctx
->redirect_url
)
1582 IBindStatusCallback_OnProgress(binding
->callback
, 0, 0, BINDSTATUS_REDIRECTING
, binding_ctx
->redirect_url
);
1583 report_data(binding
, BSCF_FIRSTDATANOTIFICATION
| (binding_ctx
->download_state
== END_DOWNLOAD
? BSCF_LASTDATANOTIFICATION
: 0),
1586 hres
= IInternetProtocolEx_StartEx(&binding
->protocol
->IInternetProtocolEx_iface
, uri
,
1587 &binding
->IInternetProtocolSink_iface
, &binding
->IInternetBindInfo_iface
,
1588 PI_APARTMENTTHREADED
|PI_MIMEVERIFICATION
, 0);
1590 TRACE("start ret %08x\n", hres
);
1592 if(FAILED(hres
) && hres
!= E_PENDING
) {
1593 stop_binding(binding
, hres
, NULL
);
1594 IBinding_Release(&binding
->IBinding_iface
);
1600 while(!(binding
->bindf
& BINDF_ASYNCHRONOUS
) &&
1601 !(binding
->state
& BINDING_STOPPED
)) {
1602 MsgWaitForMultipleObjects(0, NULL
, FALSE
, 5000, QS_POSTMESSAGE
);
1603 while (PeekMessageW(&msg
, binding
->notif_hwnd
, WM_USER
, WM_USER
+117, PM_REMOVE
|PM_NOYIELD
)) {
1604 TranslateMessage(&msg
);
1605 DispatchMessageW(&msg
);
1613 HRESULT
bind_to_storage(IUri
*uri
, IBindCtx
*pbc
, REFIID riid
, void **ppv
)
1615 Binding
*binding
= NULL
, *binding_ctx
;
1618 binding_ctx
= get_bctx_binding(pbc
);
1620 hres
= start_binding(NULL
, binding_ctx
, uri
, pbc
, FALSE
, riid
, &binding
);
1622 IBinding_Release(&binding_ctx
->IBinding_iface
);
1626 if(binding
->hres
== S_OK
&& binding
->download_state
!= BEFORE_DOWNLOAD
/* FIXME */) {
1627 if((binding
->state
& BINDING_STOPPED
) && (binding
->state
& BINDING_LOCKED
))
1628 IInternetProtocolEx_UnlockRequest(&binding
->protocol
->IInternetProtocolEx_iface
);
1630 hres
= binding
->stgmed_obj
->vtbl
->get_result(binding
->stgmed_obj
, binding
->bindf
, ppv
);
1631 }else if(binding
->bindf
& BINDF_ASYNCHRONOUS
) {
1632 hres
= MK_S_ASYNCHRONOUS
;
1634 hres
= FAILED(binding
->hres
) ? binding
->hres
: S_OK
;
1637 IBinding_Release(&binding
->IBinding_iface
);
1642 HRESULT
bind_to_object(IMoniker
*mon
, IUri
*uri
, IBindCtx
*pbc
, REFIID riid
, void **ppv
)
1649 hres
= start_binding(mon
, NULL
, uri
, pbc
, TRUE
, riid
, &binding
);
1653 if(binding
->hres
!= S_OK
) {
1654 hres
= SUCCEEDED(binding
->hres
) ? S_OK
: binding
->hres
;
1655 }else if(binding
->bindf
& BINDF_ASYNCHRONOUS
) {
1656 hres
= MK_S_ASYNCHRONOUS
;
1658 *ppv
= binding
->obj
;
1659 IUnknown_AddRef(binding
->obj
);
1663 IBinding_Release(&binding
->IBinding_iface
);