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 HRESULT hres
= IInternetProtocolEx_LockRequest(
1130 &This
->protocol
->IInternetProtocolEx_iface
, 0);
1132 This
->state
|= BINDING_LOCKED
;
1135 hres
= This
->stgmed_obj
->vtbl
->fill_stgmed(This
->stgmed_obj
, &stgmed
);
1137 stop_binding(This
, hres
, NULL
);
1141 formatetc
.tymed
= stgmed
.tymed
;
1142 formatetc
.cfFormat
= This
->clipboard_format
;
1144 hres
= IBindStatusCallback_OnDataAvailable(This
->callback
, bscf
, progress
,
1145 &formatetc
, &stgmed
);
1147 if(This
->download_state
!= END_DOWNLOAD
) {
1148 This
->download_state
= END_DOWNLOAD
;
1149 IBindStatusCallback_OnProgress(This
->callback
, progress
, progress_max
,
1150 BINDSTATUS_ENDDOWNLOADDATA
, This
->url
);
1153 WARN("OnDataAvailable returned %x\n", hres
);
1154 stop_binding(This
, hres
, NULL
);
1158 if(This
->download_state
== END_DOWNLOAD
)
1159 stop_binding(This
, S_OK
, NULL
);
1163 static HRESULT WINAPI
InternetProtocolSink_ReportData(IInternetProtocolSink
*iface
,
1164 DWORD grfBSCF
, ULONG ulProgress
, ULONG ulProgressMax
)
1166 Binding
*This
= impl_from_IInternetProtocolSink(iface
);
1168 TRACE("(%p)->(%d %u %u)\n", This
, grfBSCF
, ulProgress
, ulProgressMax
);
1170 report_data(This
, grfBSCF
, ulProgress
, ulProgressMax
);
1174 static HRESULT WINAPI
InternetProtocolSink_ReportResult(IInternetProtocolSink
*iface
,
1175 HRESULT hrResult
, DWORD dwError
, LPCWSTR szResult
)
1177 Binding
*This
= impl_from_IInternetProtocolSink(iface
);
1179 TRACE("(%p)->(%08x %d %s)\n", This
, hrResult
, dwError
, debugstr_w(szResult
));
1181 stop_binding(This
, hrResult
, szResult
);
1183 IInternetProtocolEx_Terminate(&This
->protocol
->IInternetProtocolEx_iface
, 0);
1187 static const IInternetProtocolSinkVtbl InternetProtocolSinkVtbl
= {
1188 InternetProtocolSink_QueryInterface
,
1189 InternetProtocolSink_AddRef
,
1190 InternetProtocolSink_Release
,
1191 InternetProtocolSink_Switch
,
1192 InternetProtocolSink_ReportProgress
,
1193 InternetProtocolSink_ReportData
,
1194 InternetProtocolSink_ReportResult
1197 static inline Binding
*impl_from_IInternetBindInfo(IInternetBindInfo
*iface
)
1199 return CONTAINING_RECORD(iface
, Binding
, IInternetBindInfo_iface
);
1202 static HRESULT WINAPI
InternetBindInfo_QueryInterface(IInternetBindInfo
*iface
,
1203 REFIID riid
, void **ppv
)
1205 Binding
*This
= impl_from_IInternetBindInfo(iface
);
1206 return IBinding_QueryInterface(&This
->IBinding_iface
, riid
, ppv
);
1209 static ULONG WINAPI
InternetBindInfo_AddRef(IInternetBindInfo
*iface
)
1211 Binding
*This
= impl_from_IInternetBindInfo(iface
);
1212 return IBinding_AddRef(&This
->IBinding_iface
);
1215 static ULONG WINAPI
InternetBindInfo_Release(IInternetBindInfo
*iface
)
1217 Binding
*This
= impl_from_IInternetBindInfo(iface
);
1218 return IBinding_Release(&This
->IBinding_iface
);
1221 static HRESULT WINAPI
InternetBindInfo_GetBindInfo(IInternetBindInfo
*iface
,
1222 DWORD
*grfBINDF
, BINDINFO
*pbindinfo
)
1224 Binding
*This
= impl_from_IInternetBindInfo(iface
);
1226 TRACE("(%p)->(%p %p)\n", This
, grfBINDF
, pbindinfo
);
1228 *grfBINDF
= This
->bindf
;
1229 return CopyBindInfo(&This
->bindinfo
, pbindinfo
);
1232 static HRESULT WINAPI
InternetBindInfo_GetBindString(IInternetBindInfo
*iface
,
1233 ULONG ulStringType
, LPOLESTR
*ppwzStr
, ULONG cEl
, ULONG
*pcElFetched
)
1235 Binding
*This
= impl_from_IInternetBindInfo(iface
);
1237 TRACE("(%p)->(%d %p %d %p)\n", This
, ulStringType
, ppwzStr
, cEl
, pcElFetched
);
1239 switch(ulStringType
) {
1240 case BINDSTRING_ACCEPT_MIMES
: {
1241 if(!ppwzStr
|| !pcElFetched
)
1242 return E_INVALIDARG
;
1244 ppwzStr
[0] = CoTaskMemAlloc(sizeof(L
"*/*"));
1245 memcpy(ppwzStr
[0], L
"*/*", sizeof(L
"*/*"));
1249 case BINDSTRING_USER_AGENT
: {
1250 IInternetBindInfo
*bindinfo
= NULL
;
1253 hres
= IBindStatusCallback_QueryInterface(This
->callback
, &IID_IInternetBindInfo
,
1258 hres
= IInternetBindInfo_GetBindString(bindinfo
, ulStringType
, ppwzStr
,
1260 IInternetBindInfo_Release(bindinfo
);
1264 case BINDSTRING_URL
: {
1265 DWORD size
= (SysStringLen(This
->url
)+1) * sizeof(WCHAR
);
1267 if(!ppwzStr
|| !pcElFetched
)
1268 return E_INVALIDARG
;
1270 *ppwzStr
= CoTaskMemAlloc(size
);
1271 memcpy(*ppwzStr
, This
->url
, size
);
1277 FIXME("not supported string type %d\n", ulStringType
);
1281 static const IInternetBindInfoVtbl InternetBindInfoVtbl
= {
1282 InternetBindInfo_QueryInterface
,
1283 InternetBindInfo_AddRef
,
1284 InternetBindInfo_Release
,
1285 InternetBindInfo_GetBindInfo
,
1286 InternetBindInfo_GetBindString
1289 static inline Binding
*impl_from_IWinInetHttpInfo(IWinInetHttpInfo
*iface
)
1291 return CONTAINING_RECORD(iface
, Binding
, IWinInetHttpInfo_iface
);
1294 static HRESULT WINAPI
WinInetHttpInfo_QueryInterface(IWinInetHttpInfo
*iface
, REFIID riid
, void **ppv
)
1296 Binding
*This
= impl_from_IWinInetHttpInfo(iface
);
1297 return IBinding_QueryInterface(&This
->IBinding_iface
, riid
, ppv
);
1300 static ULONG WINAPI
WinInetHttpInfo_AddRef(IWinInetHttpInfo
*iface
)
1302 Binding
*This
= impl_from_IWinInetHttpInfo(iface
);
1303 return IBinding_AddRef(&This
->IBinding_iface
);
1306 static ULONG WINAPI
WinInetHttpInfo_Release(IWinInetHttpInfo
*iface
)
1308 Binding
*This
= impl_from_IWinInetHttpInfo(iface
);
1309 return IBinding_Release(&This
->IBinding_iface
);
1312 static HRESULT WINAPI
WinInetHttpInfo_QueryOption(IWinInetHttpInfo
*iface
, DWORD dwOption
,
1313 void *pBuffer
, DWORD
*pcbBuffer
)
1315 Binding
*This
= impl_from_IWinInetHttpInfo(iface
);
1316 IWinInetInfo
*wininet_info
;
1319 TRACE("(%p)->(%x %p %p)\n", This
, dwOption
, pBuffer
, pcbBuffer
);
1321 hres
= IInternetProtocolEx_QueryInterface(&This
->protocol
->IInternetProtocolEx_iface
,
1322 &IID_IWinInetInfo
, (void**)&wininet_info
);
1326 hres
= IWinInetInfo_QueryOption(wininet_info
, dwOption
, pBuffer
, pcbBuffer
);
1327 IWinInetInfo_Release(wininet_info
);
1331 static HRESULT WINAPI
WinInetHttpInfo_QueryInfo(IWinInetHttpInfo
*iface
, DWORD dwOption
,
1332 void *pBuffer
, DWORD
*pcbBuffer
, DWORD
*pdwFlags
, DWORD
*pdwReserved
)
1334 Binding
*This
= impl_from_IWinInetHttpInfo(iface
);
1335 IWinInetHttpInfo
*http_info
;
1338 TRACE("(%p)->(%x %p %p %p %p)\n", This
, dwOption
, pBuffer
, pcbBuffer
, pdwFlags
, pdwReserved
);
1340 hres
= IInternetProtocolEx_QueryInterface(&This
->protocol
->IInternetProtocolEx_iface
,
1341 &IID_IWinInetHttpInfo
, (void**)&http_info
);
1345 hres
= IWinInetHttpInfo_QueryInfo(http_info
, dwOption
, pBuffer
, pcbBuffer
, pdwFlags
, pdwReserved
);
1346 IWinInetHttpInfo_Release(http_info
);
1350 static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl
= {
1351 WinInetHttpInfo_QueryInterface
,
1352 WinInetHttpInfo_AddRef
,
1353 WinInetHttpInfo_Release
,
1354 WinInetHttpInfo_QueryOption
,
1355 WinInetHttpInfo_QueryInfo
1358 static inline Binding
*impl_from_IServiceProvider(IServiceProvider
*iface
)
1360 return CONTAINING_RECORD(iface
, Binding
, IServiceProvider_iface
);
1363 static HRESULT WINAPI
ServiceProvider_QueryInterface(IServiceProvider
*iface
,
1364 REFIID riid
, void **ppv
)
1366 Binding
*This
= impl_from_IServiceProvider(iface
);
1367 return IBinding_QueryInterface(&This
->IBinding_iface
, riid
, ppv
);
1370 static ULONG WINAPI
ServiceProvider_AddRef(IServiceProvider
*iface
)
1372 Binding
*This
= impl_from_IServiceProvider(iface
);
1373 return IBinding_AddRef(&This
->IBinding_iface
);
1376 static ULONG WINAPI
ServiceProvider_Release(IServiceProvider
*iface
)
1378 Binding
*This
= impl_from_IServiceProvider(iface
);
1379 return IBinding_Release(&This
->IBinding_iface
);
1382 static HRESULT WINAPI
ServiceProvider_QueryService(IServiceProvider
*iface
,
1383 REFGUID guidService
, REFIID riid
, void **ppv
)
1385 Binding
*This
= impl_from_IServiceProvider(iface
);
1388 TRACE("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
1390 if(This
->service_provider
) {
1391 hres
= IServiceProvider_QueryService(This
->service_provider
, guidService
,
1397 WARN("unknown service %s\n", debugstr_guid(guidService
));
1398 return E_NOINTERFACE
;
1401 static const IServiceProviderVtbl ServiceProviderVtbl
= {
1402 ServiceProvider_QueryInterface
,
1403 ServiceProvider_AddRef
,
1404 ServiceProvider_Release
,
1405 ServiceProvider_QueryService
1408 static HRESULT
get_callback(IBindCtx
*pbc
, IBindStatusCallback
**callback
)
1413 hres
= IBindCtx_GetObjectParam(pbc
, bscb_holderW
, &unk
);
1415 return create_default_callback(callback
);
1417 hres
= IUnknown_QueryInterface(unk
, &IID_IBindStatusCallback
, (void**)callback
);
1418 IUnknown_Release(unk
);
1422 static BOOL
is_urlmon_protocol(IUri
*uri
)
1427 hres
= IUri_GetScheme(uri
, &scheme
);
1432 case URL_SCHEME_FILE
:
1433 case URL_SCHEME_FTP
:
1434 case URL_SCHEME_GOPHER
:
1435 case URL_SCHEME_HTTP
:
1436 case URL_SCHEME_HTTPS
:
1444 static HRESULT
Binding_Create(IMoniker
*mon
, Binding
*binding_ctx
, IUri
*uri
, IBindCtx
*pbc
,
1445 BOOL to_obj
, REFIID riid
, Binding
**binding
)
1450 URLMON_LockModule();
1452 ret
= heap_alloc_zero(sizeof(Binding
));
1454 ret
->IBinding_iface
.lpVtbl
= &BindingVtbl
;
1455 ret
->IInternetProtocolSink_iface
.lpVtbl
= &InternetProtocolSinkVtbl
;
1456 ret
->IInternetBindInfo_iface
.lpVtbl
= &InternetBindInfoVtbl
;
1457 ret
->IWinInetHttpInfo_iface
.lpVtbl
= &WinInetHttpInfoVtbl
;
1458 ret
->IServiceProvider_iface
.lpVtbl
= &ServiceProviderVtbl
;
1462 ret
->to_object
= to_obj
;
1464 ret
->notif_hwnd
= get_notif_hwnd();
1465 ret
->report_mime
= !binding_ctx
;
1466 ret
->download_state
= BEFORE_DOWNLOAD
;
1469 IBindCtx_AddRef(pbc
);
1474 IMoniker_AddRef(mon
);
1478 ret
->bindinfo
.cbSize
= sizeof(BINDINFO
);
1480 InitializeCriticalSection(&ret
->section
);
1481 ret
->section
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": Binding.section");
1483 hres
= get_callback(pbc
, &ret
->callback
);
1485 WARN("Could not get IBindStatusCallback\n");
1486 IBinding_Release(&ret
->IBinding_iface
);
1490 IBindStatusCallback_QueryInterface(ret
->callback
, &IID_IServiceProvider
,
1491 (void**)&ret
->service_provider
);
1494 ret
->protocol
= binding_ctx
->protocol
;
1495 IInternetProtocolEx_AddRef(&ret
->protocol
->IInternetProtocolEx_iface
);
1497 hres
= create_binding_protocol(&ret
->protocol
);
1499 WARN("Could not get protocol handler\n");
1500 IBinding_Release(&ret
->IBinding_iface
);
1505 hres
= IBindStatusCallback_GetBindInfo(ret
->callback
, &ret
->bindf
, &ret
->bindinfo
);
1507 WARN("GetBindInfo failed: %08x\n", hres
);
1508 IBinding_Release(&ret
->IBinding_iface
);
1512 TRACE("bindf %08x\n", ret
->bindf
);
1513 dump_BINDINFO(&ret
->bindinfo
);
1515 ret
->bindf
|= BINDF_FROMURLMON
;
1517 ret
->bindinfo
.dwOptions
|= 0x100000;
1519 if(!(ret
->bindf
& BINDF_ASYNCHRONOUS
) || !(ret
->bindf
& BINDF_PULLDATA
)) {
1520 ret
->bindf
|= BINDF_NEEDFILE
;
1521 ret
->use_cache_file
= TRUE
;
1522 }else if(!is_urlmon_protocol(uri
)) {
1523 ret
->bindf
|= BINDF_NEEDFILE
;
1526 hres
= IUri_GetDisplayUri(uri
, &ret
->url
);
1528 IBinding_Release(&ret
->IBinding_iface
);
1533 ret
->stgmed_buf
= binding_ctx
->stgmed_buf
;
1534 IUnknown_AddRef(&ret
->stgmed_buf
->IUnknown_iface
);
1535 ret
->clipboard_format
= binding_ctx
->clipboard_format
;
1537 ret
->stgmed_buf
= create_stgmed_buf(&ret
->protocol
->IInternetProtocolEx_iface
);
1541 ret
->stgmed_obj
= NULL
;
1542 }else if(IsEqualGUID(&IID_IStream
, riid
)) {
1543 ret
->stgmed_obj
= create_stgmed_stream(ret
->stgmed_buf
);
1544 }else if(IsEqualGUID(&IID_IUnknown
, riid
)) {
1545 ret
->bindf
|= BINDF_NEEDFILE
;
1546 ret
->stgmed_obj
= create_stgmed_file(ret
->stgmed_buf
);
1548 FIXME("Unsupported riid %s\n", debugstr_guid(riid
));
1549 IBinding_Release(&ret
->IBinding_iface
);
1557 static HRESULT
start_binding(IMoniker
*mon
, Binding
*binding_ctx
, IUri
*uri
, IBindCtx
*pbc
,
1558 BOOL to_obj
, REFIID riid
, Binding
**ret
)
1560 Binding
*binding
= NULL
;
1564 hres
= Binding_Create(mon
, binding_ctx
, uri
, pbc
, to_obj
, riid
, &binding
);
1568 hres
= IBindStatusCallback_OnStartBinding(binding
->callback
, 0, &binding
->IBinding_iface
);
1570 WARN("OnStartBinding failed: %08x\n", hres
);
1571 if(hres
!= E_ABORT
&& hres
!= E_NOTIMPL
)
1572 hres
= INET_E_DOWNLOAD_FAILURE
;
1574 stop_binding(binding
, hres
, NULL
);
1575 IBinding_Release(&binding
->IBinding_iface
);
1580 set_binding_sink(binding
->protocol
, &binding
->IInternetProtocolSink_iface
,
1581 &binding
->IInternetBindInfo_iface
);
1582 if(binding_ctx
->redirect_url
)
1583 IBindStatusCallback_OnProgress(binding
->callback
, 0, 0, BINDSTATUS_REDIRECTING
, binding_ctx
->redirect_url
);
1584 report_data(binding
, BSCF_FIRSTDATANOTIFICATION
| (binding_ctx
->download_state
== END_DOWNLOAD
? BSCF_LASTDATANOTIFICATION
: 0),
1587 hres
= IInternetProtocolEx_StartEx(&binding
->protocol
->IInternetProtocolEx_iface
, uri
,
1588 &binding
->IInternetProtocolSink_iface
, &binding
->IInternetBindInfo_iface
,
1589 PI_APARTMENTTHREADED
|PI_MIMEVERIFICATION
, 0);
1591 TRACE("start ret %08x\n", hres
);
1593 if(FAILED(hres
) && hres
!= E_PENDING
) {
1594 stop_binding(binding
, hres
, NULL
);
1595 IBinding_Release(&binding
->IBinding_iface
);
1601 while(!(binding
->bindf
& BINDF_ASYNCHRONOUS
) &&
1602 !(binding
->state
& BINDING_STOPPED
)) {
1603 MsgWaitForMultipleObjects(0, NULL
, FALSE
, 5000, QS_POSTMESSAGE
);
1604 while (PeekMessageW(&msg
, binding
->notif_hwnd
, WM_USER
, WM_USER
+117, PM_REMOVE
|PM_NOYIELD
)) {
1605 TranslateMessage(&msg
);
1606 DispatchMessageW(&msg
);
1614 HRESULT
bind_to_storage(IUri
*uri
, IBindCtx
*pbc
, REFIID riid
, void **ppv
)
1616 Binding
*binding
= NULL
, *binding_ctx
;
1619 binding_ctx
= get_bctx_binding(pbc
);
1621 hres
= start_binding(NULL
, binding_ctx
, uri
, pbc
, FALSE
, riid
, &binding
);
1623 IBinding_Release(&binding_ctx
->IBinding_iface
);
1627 if(binding
->hres
== S_OK
&& binding
->download_state
!= BEFORE_DOWNLOAD
/* FIXME */) {
1628 if((binding
->state
& BINDING_STOPPED
) && (binding
->state
& BINDING_LOCKED
))
1629 IInternetProtocolEx_UnlockRequest(&binding
->protocol
->IInternetProtocolEx_iface
);
1631 hres
= binding
->stgmed_obj
->vtbl
->get_result(binding
->stgmed_obj
, binding
->bindf
, ppv
);
1632 }else if(binding
->bindf
& BINDF_ASYNCHRONOUS
) {
1633 hres
= MK_S_ASYNCHRONOUS
;
1635 hres
= FAILED(binding
->hres
) ? binding
->hres
: S_OK
;
1638 IBinding_Release(&binding
->IBinding_iface
);
1643 HRESULT
bind_to_object(IMoniker
*mon
, IUri
*uri
, IBindCtx
*pbc
, REFIID riid
, void **ppv
)
1650 hres
= start_binding(mon
, NULL
, uri
, pbc
, TRUE
, riid
, &binding
);
1654 if(binding
->hres
!= S_OK
) {
1655 hres
= SUCCEEDED(binding
->hres
) ? S_OK
: binding
->hres
;
1656 }else if(binding
->bindf
& BINDF_ASYNCHRONOUS
) {
1657 hres
= MK_S_ASYNCHRONOUS
;
1659 *ppv
= binding
->obj
;
1660 IUnknown_AddRef(binding
->obj
);
1664 IBinding_Release(&binding
->IBinding_iface
);