2 * Copyright 2007-2009 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 "wine/debug.h"
22 WINE_DEFAULT_DEBUG_CHANNEL(urlmon
);
24 typedef void (*task_proc_t
)(BindProtocol
*,task_header_t
*);
26 struct _task_header_t
{
31 #define BUFFER_SIZE 2048
32 #define MIME_TEST_SIZE 255
34 #define WM_MK_CONTINUE (WM_USER+101)
35 #define WM_MK_RELEASE (WM_USER+102)
37 static LRESULT WINAPI
notif_wnd_proc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
40 case WM_MK_CONTINUE
: {
41 BindProtocol
*This
= (BindProtocol
*)lParam
;
45 EnterCriticalSection(&This
->section
);
47 task
= This
->task_queue_head
;
49 This
->task_queue_head
= task
->next
;
50 if(!This
->task_queue_head
)
51 This
->task_queue_tail
= NULL
;
54 LeaveCriticalSection(&This
->section
);
59 This
->continue_call
++;
60 task
->proc(This
, task
);
61 This
->continue_call
--;
64 IInternetProtocolEx_Release(&This
->IInternetProtocolEx_iface
);
68 tls_data_t
*data
= get_tls_data();
70 if(!--data
->notif_hwnd_cnt
) {
72 data
->notif_hwnd
= NULL
;
77 return DefWindowProcW(hwnd
, msg
, wParam
, lParam
);
80 HWND
get_notif_hwnd(void)
82 static ATOM wnd_class
= 0;
85 static const WCHAR wszURLMonikerNotificationWindow
[] =
86 {'U','R','L',' ','M','o','n','i','k','e','r',' ',
87 'N','o','t','i','f','i','c','a','t','i','o','n',' ','W','i','n','d','o','w',0};
89 tls_data
= get_tls_data();
93 if(tls_data
->notif_hwnd_cnt
) {
94 tls_data
->notif_hwnd_cnt
++;
95 return tls_data
->notif_hwnd
;
99 static WNDCLASSEXW wndclass
= {
101 notif_wnd_proc
, 0, 0,
102 NULL
, NULL
, NULL
, NULL
, NULL
,
103 wszURLMonikerNotificationWindow
,
107 wndclass
.hInstance
= hProxyDll
;
109 wnd_class
= RegisterClassExW(&wndclass
);
110 if (!wnd_class
&& GetLastError() == ERROR_CLASS_ALREADY_EXISTS
)
114 tls_data
->notif_hwnd
= CreateWindowExW(0, wszURLMonikerNotificationWindow
,
115 wszURLMonikerNotificationWindow
, 0, 0, 0, 0, 0, HWND_MESSAGE
,
116 NULL
, hProxyDll
, NULL
);
117 if(tls_data
->notif_hwnd
)
118 tls_data
->notif_hwnd_cnt
++;
120 TRACE("hwnd = %p\n", tls_data
->notif_hwnd
);
122 return tls_data
->notif_hwnd
;
125 void release_notif_hwnd(HWND hwnd
)
127 tls_data_t
*data
= get_tls_data();
132 if(data
->notif_hwnd
!= hwnd
) {
133 PostMessageW(data
->notif_hwnd
, WM_MK_RELEASE
, 0, 0);
137 if(!--data
->notif_hwnd_cnt
) {
138 DestroyWindow(data
->notif_hwnd
);
139 data
->notif_hwnd
= NULL
;
143 static void push_task(BindProtocol
*This
, task_header_t
*task
, task_proc_t proc
)
145 BOOL do_post
= FALSE
;
150 EnterCriticalSection(&This
->section
);
152 if(This
->task_queue_tail
) {
153 This
->task_queue_tail
->next
= task
;
154 This
->task_queue_tail
= task
;
156 This
->task_queue_tail
= This
->task_queue_head
= task
;
157 do_post
= !This
->continue_call
;
160 LeaveCriticalSection(&This
->section
);
163 IInternetProtocolEx_AddRef(&This
->IInternetProtocolEx_iface
);
164 PostMessageW(This
->notif_hwnd
, WM_MK_CONTINUE
, 0, (LPARAM
)This
);
168 static inline BOOL
do_direct_notif(BindProtocol
*This
)
170 return !(This
->pi
& PI_APARTMENTTHREADED
) || (This
->apartment_thread
== GetCurrentThreadId() && !This
->continue_call
);
173 static HRESULT
handle_mime_filter(BindProtocol
*This
, IInternetProtocol
*mime_filter
, LPCWSTR mime
)
175 PROTOCOLFILTERDATA filter_data
= { sizeof(PROTOCOLFILTERDATA
), NULL
, NULL
, NULL
, 0 };
176 IInternetProtocolSink
*protocol_sink
, *old_sink
;
177 ProtocolProxy
*filter_proxy
;
180 hres
= IInternetProtocol_QueryInterface(mime_filter
, &IID_IInternetProtocolSink
, (void**)&protocol_sink
);
184 hres
= create_protocol_proxy(&This
->default_protocol_handler
.IInternetProtocol_iface
, This
->protocol_sink
, &filter_proxy
);
186 IInternetProtocolSink_Release(protocol_sink
);
190 old_sink
= This
->protocol_sink
;
191 This
->protocol_sink
= protocol_sink
;
192 This
->filter_proxy
= filter_proxy
;
194 IInternetProtocol_AddRef(mime_filter
);
195 This
->protocol_handler
= mime_filter
;
197 filter_data
.pProtocol
= &filter_proxy
->IInternetProtocol_iface
;
198 hres
= IInternetProtocol_Start(mime_filter
, mime
, &filter_proxy
->IInternetProtocolSink_iface
,
199 &This
->IInternetBindInfo_iface
, PI_FILTER_MODE
|PI_FORCE_ASYNC
,
200 (HANDLE_PTR
)&filter_data
);
202 IInternetProtocolSink_Release(old_sink
);
206 /* NOTE: IE9 calls it on the new protocol_sink. It doesn't make sense to is seems to be a bug there. */
207 IInternetProtocolSink_ReportProgress(old_sink
, BINDSTATUS_LOADINGMIMEHANDLER
, NULL
);
208 IInternetProtocolSink_Release(old_sink
);
210 This
->pi
&= ~PI_MIMEVERIFICATION
; /* FIXME: more tests */
214 static void mime_available(BindProtocol
*This
, LPCWSTR mime
, BOOL verified
)
216 IInternetProtocol
*mime_filter
;
219 heap_free(This
->mime
);
222 mime_filter
= get_mime_filter(mime
);
224 TRACE("Got mime filter for %s\n", debugstr_w(mime
));
226 hres
= handle_mime_filter(This
, mime_filter
, mime
);
227 IInternetProtocol_Release(mime_filter
);
229 FIXME("MIME filter failed: %08x\n", hres
);
231 This
->mime
= heap_strdupW(mime
);
233 if(verified
|| !(This
->pi
& PI_MIMEVERIFICATION
)) {
234 This
->reported_mime
= TRUE
;
236 if(This
->protocol_sink
)
237 IInternetProtocolSink_ReportProgress(This
->protocol_sink
, BINDSTATUS_MIMETYPEAVAILABLE
, mime
);
242 static inline BindProtocol
*impl_from_IInternetProtocolEx(IInternetProtocolEx
*iface
)
244 return CONTAINING_RECORD(iface
, BindProtocol
, IInternetProtocolEx_iface
);
247 static HRESULT WINAPI
BindProtocol_QueryInterface(IInternetProtocolEx
*iface
, REFIID riid
, void **ppv
)
249 BindProtocol
*This
= impl_from_IInternetProtocolEx(iface
);
252 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
253 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
254 *ppv
= &This
->IInternetProtocolEx_iface
;
255 }else if(IsEqualGUID(&IID_IInternetProtocolRoot
, riid
)) {
256 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This
, ppv
);
257 *ppv
= &This
->IInternetProtocolEx_iface
;
258 }else if(IsEqualGUID(&IID_IInternetProtocol
, riid
)) {
259 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This
, ppv
);
260 *ppv
= &This
->IInternetProtocolEx_iface
;
261 }else if(IsEqualGUID(&IID_IInternetProtocolEx
, riid
)) {
262 TRACE("(%p)->(IID_IInternetProtocolEx %p)\n", This
, ppv
);
263 *ppv
= &This
->IInternetProtocolEx_iface
;
264 }else if(IsEqualGUID(&IID_IInternetBindInfo
, riid
)) {
265 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This
, ppv
);
266 *ppv
= &This
->IInternetBindInfo_iface
;
267 }else if(IsEqualGUID(&IID_IInternetPriority
, riid
)) {
268 TRACE("(%p)->(IID_IInternetPriority %p)\n", This
, ppv
);
269 *ppv
= &This
->IInternetPriority_iface
;
270 }else if(IsEqualGUID(&IID_IAuthenticate
, riid
)) {
271 FIXME("(%p)->(IID_IAuthenticate %p)\n", This
, ppv
);
272 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
273 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
274 *ppv
= &This
->IServiceProvider_iface
;
275 }else if(IsEqualGUID(&IID_IInternetProtocolSink
, riid
)) {
276 TRACE("(%p)->(IID_IInternetProtocolSink %p)\n", This
, ppv
);
277 *ppv
= &This
->IInternetProtocolSink_iface
;
278 }else if(IsEqualGUID(&IID_IWinInetInfo
, riid
)) {
279 TRACE("(%p)->(IID_IWinInetInfo %p)\n", This
, ppv
);
282 IWinInetInfo
*inet_info
;
285 hres
= IInternetProtocol_QueryInterface(This
->protocol
, &IID_IWinInetInfo
, (void**)&inet_info
);
286 if(SUCCEEDED(hres
)) {
287 *ppv
= &This
->IWinInetHttpInfo_iface
;
288 IWinInetInfo_Release(inet_info
);
291 }else if(IsEqualGUID(&IID_IWinInetHttpInfo
, riid
)) {
292 TRACE("(%p)->(IID_IWinInetHttpInfo %p)\n", This
, ppv
);
295 IWinInetHttpInfo
*http_info
;
298 hres
= IInternetProtocol_QueryInterface(This
->protocol
, &IID_IWinInetHttpInfo
, (void**)&http_info
);
299 if(SUCCEEDED(hres
)) {
300 *ppv
= &This
->IWinInetHttpInfo_iface
;
301 IWinInetHttpInfo_Release(http_info
);
305 WARN("not supported interface %s\n", debugstr_guid(riid
));
309 return E_NOINTERFACE
;
311 IUnknown_AddRef((IUnknown
*)*ppv
);
315 static ULONG WINAPI
BindProtocol_AddRef(IInternetProtocolEx
*iface
)
317 BindProtocol
*This
= impl_from_IInternetProtocolEx(iface
);
318 LONG ref
= InterlockedIncrement(&This
->ref
);
319 TRACE("(%p) ref=%d\n", This
, ref
);
323 static ULONG WINAPI
BindProtocol_Release(IInternetProtocolEx
*iface
)
325 BindProtocol
*This
= impl_from_IInternetProtocolEx(iface
);
326 LONG ref
= InterlockedDecrement(&This
->ref
);
328 TRACE("(%p) ref=%d\n", This
, ref
);
331 if(This
->wininet_info
)
332 IWinInetInfo_Release(This
->wininet_info
);
333 if(This
->wininet_http_info
)
334 IWinInetHttpInfo_Release(This
->wininet_http_info
);
336 IInternetProtocol_Release(This
->protocol
);
338 IInternetBindInfo_Release(This
->bind_info
);
339 if(This
->protocol_handler
&& This
->protocol_handler
!= &This
->default_protocol_handler
.IInternetProtocol_iface
)
340 IInternetProtocol_Release(This
->protocol_handler
);
341 if(This
->filter_proxy
)
342 IInternetProtocol_Release(&This
->filter_proxy
->IInternetProtocol_iface
);
344 IUri_Release(This
->uri
);
346 set_binding_sink(This
, NULL
, NULL
);
349 release_notif_hwnd(This
->notif_hwnd
);
350 This
->section
.DebugInfo
->Spare
[0] = 0;
351 DeleteCriticalSection(&This
->section
);
353 heap_free(This
->mime
);
356 URLMON_UnlockModule();
362 static HRESULT WINAPI
BindProtocol_Start(IInternetProtocolEx
*iface
, LPCWSTR szUrl
,
363 IInternetProtocolSink
*pOIProtSink
, IInternetBindInfo
*pOIBindInfo
,
364 DWORD grfPI
, HANDLE_PTR dwReserved
)
366 BindProtocol
*This
= impl_from_IInternetProtocolEx(iface
);
370 TRACE("(%p)->(%s %p %p %08x %lx)\n", This
, debugstr_w(szUrl
), pOIProtSink
,
371 pOIBindInfo
, grfPI
, dwReserved
);
373 hres
= CreateUri(szUrl
, Uri_CREATE_FILE_USE_DOS_PATH
, 0, &uri
);
377 hres
= IInternetProtocolEx_StartEx(&This
->IInternetProtocolEx_iface
, uri
, pOIProtSink
,
378 pOIBindInfo
, grfPI
, (HANDLE
*)dwReserved
);
384 static HRESULT WINAPI
BindProtocol_Continue(IInternetProtocolEx
*iface
, PROTOCOLDATA
*pProtocolData
)
386 BindProtocol
*This
= impl_from_IInternetProtocolEx(iface
);
388 TRACE("(%p)->(%p)\n", This
, pProtocolData
);
390 return IInternetProtocol_Continue(This
->protocol_handler
, pProtocolData
);
393 static HRESULT WINAPI
BindProtocol_Abort(IInternetProtocolEx
*iface
, HRESULT hrReason
,
396 BindProtocol
*This
= impl_from_IInternetProtocolEx(iface
);
398 TRACE("(%p)->(%08x %08x)\n", This
, hrReason
, dwOptions
);
400 return IInternetProtocol_Abort(This
->protocol_handler
, hrReason
, dwOptions
);
403 static HRESULT WINAPI
BindProtocol_Terminate(IInternetProtocolEx
*iface
, DWORD dwOptions
)
405 BindProtocol
*This
= impl_from_IInternetProtocolEx(iface
);
407 TRACE("(%p)->(%08x)\n", This
, dwOptions
);
409 return IInternetProtocol_Terminate(This
->protocol_handler
, dwOptions
);
412 static HRESULT WINAPI
BindProtocol_Suspend(IInternetProtocolEx
*iface
)
414 BindProtocol
*This
= impl_from_IInternetProtocolEx(iface
);
415 FIXME("(%p)\n", This
);
419 static HRESULT WINAPI
BindProtocol_Resume(IInternetProtocolEx
*iface
)
421 BindProtocol
*This
= impl_from_IInternetProtocolEx(iface
);
422 FIXME("(%p)\n", This
);
426 static HRESULT WINAPI
BindProtocol_Read(IInternetProtocolEx
*iface
, void *pv
,
427 ULONG cb
, ULONG
*pcbRead
)
429 BindProtocol
*This
= impl_from_IInternetProtocolEx(iface
);
431 TRACE("(%p)->(%p %u %p)\n", This
, pv
, cb
, pcbRead
);
435 return IInternetProtocol_Read(This
->protocol_handler
, pv
, cb
, pcbRead
);
438 static HRESULT WINAPI
BindProtocol_Seek(IInternetProtocolEx
*iface
, LARGE_INTEGER dlibMove
,
439 DWORD dwOrigin
, ULARGE_INTEGER
*plibNewPosition
)
441 BindProtocol
*This
= impl_from_IInternetProtocolEx(iface
);
442 FIXME("(%p)->(%d %d %p)\n", This
, dlibMove
.u
.LowPart
, dwOrigin
, plibNewPosition
);
446 static HRESULT WINAPI
BindProtocol_LockRequest(IInternetProtocolEx
*iface
, DWORD dwOptions
)
448 BindProtocol
*This
= impl_from_IInternetProtocolEx(iface
);
450 TRACE("(%p)->(%08x)\n", This
, dwOptions
);
452 return IInternetProtocol_LockRequest(This
->protocol_handler
, dwOptions
);
455 static HRESULT WINAPI
BindProtocol_UnlockRequest(IInternetProtocolEx
*iface
)
457 BindProtocol
*This
= impl_from_IInternetProtocolEx(iface
);
459 TRACE("(%p)\n", This
);
461 return IInternetProtocol_UnlockRequest(This
->protocol_handler
);
464 static HRESULT WINAPI
BindProtocol_StartEx(IInternetProtocolEx
*iface
, IUri
*pUri
,
465 IInternetProtocolSink
*pOIProtSink
, IInternetBindInfo
*pOIBindInfo
,
466 DWORD grfPI
, HANDLE
*dwReserved
)
468 BindProtocol
*This
= impl_from_IInternetProtocolEx(iface
);
469 IInternetProtocol
*protocol
= NULL
;
470 IInternetProtocolEx
*protocolex
;
471 IInternetPriority
*priority
;
472 IServiceProvider
*service_provider
;
473 BOOL urlmon_protocol
= FALSE
;
474 CLSID clsid
= IID_NULL
;
478 TRACE("(%p)->(%p %p %p %08x %p)\n", This
, pUri
, pOIProtSink
, pOIBindInfo
, grfPI
, dwReserved
);
480 if(!pUri
|| !pOIProtSink
|| !pOIBindInfo
)
488 hres
= IInternetProtocolSink_QueryInterface(pOIProtSink
, &IID_IServiceProvider
,
489 (void**)&service_provider
);
490 if(SUCCEEDED(hres
)) {
491 /* FIXME: What's protocol CLSID here? */
492 IServiceProvider_QueryService(service_provider
, &IID_IInternetProtocol
,
493 &IID_IInternetProtocol
, (void**)&protocol
);
494 IServiceProvider_Release(service_provider
);
501 hres
= get_protocol_handler(pUri
, &clsid
, &urlmon_protocol
, &cf
);
505 if(This
->from_urlmon
) {
506 hres
= IClassFactory_CreateInstance(cf
, NULL
, &IID_IInternetProtocol
, (void**)&protocol
);
507 IClassFactory_Release(cf
);
511 hres
= IClassFactory_CreateInstance(cf
, (IUnknown
*)&This
->IInternetBindInfo_iface
,
512 &IID_IUnknown
, (void**)&unk
);
513 IClassFactory_Release(cf
);
517 hres
= IUnknown_QueryInterface(unk
, &IID_IInternetProtocol
, (void**)&protocol
);
518 IUnknown_Release(unk
);
524 StringFromCLSID(&clsid
, &clsid_str
);
525 IInternetProtocolSink_ReportProgress(pOIProtSink
, BINDSTATUS_PROTOCOLCLASSID
, clsid_str
);
526 CoTaskMemFree(clsid_str
);
528 This
->protocol
= protocol
;
530 if(urlmon_protocol
) {
531 IInternetProtocol_QueryInterface(protocol
, &IID_IWinInetInfo
, (void**)&This
->wininet_info
);
532 IInternetProtocol_QueryInterface(protocol
, &IID_IWinInetHttpInfo
, (void**)&This
->wininet_http_info
);
535 set_binding_sink(This
, pOIProtSink
, pOIBindInfo
);
537 hres
= IInternetProtocol_QueryInterface(protocol
, &IID_IInternetPriority
, (void**)&priority
);
538 if(SUCCEEDED(hres
)) {
539 IInternetPriority_SetPriority(priority
, This
->priority
);
540 IInternetPriority_Release(priority
);
543 hres
= IInternetProtocol_QueryInterface(protocol
, &IID_IInternetProtocolEx
, (void**)&protocolex
);
544 if(SUCCEEDED(hres
)) {
545 hres
= IInternetProtocolEx_StartEx(protocolex
, pUri
, &This
->IInternetProtocolSink_iface
,
546 &This
->IInternetBindInfo_iface
, 0, NULL
);
547 IInternetProtocolEx_Release(protocolex
);
551 hres
= IUri_GetDisplayUri(pUri
, &display_uri
);
555 hres
= IInternetProtocol_Start(protocol
, display_uri
, &This
->IInternetProtocolSink_iface
,
556 &This
->IInternetBindInfo_iface
, 0, 0);
557 SysFreeString(display_uri
);
563 void set_binding_sink(BindProtocol
*This
, IInternetProtocolSink
*sink
, IInternetBindInfo
*bind_info
)
565 IInternetProtocolSink
*prev_sink
;
566 IServiceProvider
*service_provider
= NULL
;
569 IInternetProtocolSink_AddRef(sink
);
570 prev_sink
= InterlockedExchangePointer((void**)&This
->protocol_sink
, sink
);
572 IInternetProtocolSink_Release(prev_sink
);
575 IInternetProtocolSink_QueryInterface(sink
, &IID_IServiceProvider
, (void**)&service_provider
);
576 service_provider
= InterlockedExchangePointer((void**)&This
->service_provider
, service_provider
);
578 IServiceProvider_Release(service_provider
);
581 IInternetBindInfo_AddRef(bind_info
);
582 bind_info
= InterlockedExchangePointer((void**)&This
->bind_info
, bind_info
);
584 IInternetBindInfo_Release(bind_info
);
587 static const IInternetProtocolExVtbl BindProtocolVtbl
= {
588 BindProtocol_QueryInterface
,
590 BindProtocol_Release
,
592 BindProtocol_Continue
,
594 BindProtocol_Terminate
,
595 BindProtocol_Suspend
,
599 BindProtocol_LockRequest
,
600 BindProtocol_UnlockRequest
,
604 static inline BindProtocol
*impl_from_IInternetProtocol(IInternetProtocol
*iface
)
606 return CONTAINING_RECORD(iface
, BindProtocol
, default_protocol_handler
.IInternetProtocol_iface
);
609 static HRESULT WINAPI
ProtocolHandler_QueryInterface(IInternetProtocol
*iface
, REFIID riid
, void **ppv
)
611 ERR("should not be called\n");
612 return E_NOINTERFACE
;
615 static ULONG WINAPI
ProtocolHandler_AddRef(IInternetProtocol
*iface
)
617 BindProtocol
*This
= impl_from_IInternetProtocol(iface
);
618 return IInternetProtocolEx_AddRef(&This
->IInternetProtocolEx_iface
);
621 static ULONG WINAPI
ProtocolHandler_Release(IInternetProtocol
*iface
)
623 BindProtocol
*This
= impl_from_IInternetProtocol(iface
);
624 return IInternetProtocolEx_Release(&This
->IInternetProtocolEx_iface
);
627 static HRESULT WINAPI
ProtocolHandler_Start(IInternetProtocol
*iface
, LPCWSTR szUrl
,
628 IInternetProtocolSink
*pOIProtSink
, IInternetBindInfo
*pOIBindInfo
,
629 DWORD grfPI
, HANDLE_PTR dwReserved
)
631 ERR("Should not be called\n");
635 static HRESULT WINAPI
ProtocolHandler_Continue(IInternetProtocol
*iface
, PROTOCOLDATA
*pProtocolData
)
637 BindProtocol
*This
= impl_from_IInternetProtocol(iface
);
640 TRACE("(%p)->(%p)\n", This
, pProtocolData
);
642 hres
= IInternetProtocol_Continue(This
->protocol
, pProtocolData
);
644 heap_free(pProtocolData
);
648 static HRESULT WINAPI
ProtocolHandler_Abort(IInternetProtocol
*iface
, HRESULT hrReason
,
651 BindProtocol
*This
= impl_from_IInternetProtocol(iface
);
653 TRACE("(%p)->(%08x %08x)\n", This
, hrReason
, dwOptions
);
655 if(This
->protocol
&& !This
->reported_result
)
656 return IInternetProtocol_Abort(This
->protocol
, hrReason
, dwOptions
);
661 static HRESULT WINAPI
ProtocolHandler_Terminate(IInternetProtocol
*iface
, DWORD dwOptions
)
663 BindProtocol
*This
= impl_from_IInternetProtocol(iface
);
665 TRACE("(%p)->(%08x)\n", This
, dwOptions
);
667 if(!This
->reported_result
)
670 IInternetProtocol_Terminate(This
->protocol
, 0);
672 if(This
->filter_proxy
) {
673 IInternetProtocol_Release(&This
->filter_proxy
->IInternetProtocol_iface
);
674 This
->filter_proxy
= NULL
;
677 set_binding_sink(This
, NULL
, NULL
);
679 if(This
->bind_info
) {
680 IInternetBindInfo_Release(This
->bind_info
);
681 This
->bind_info
= NULL
;
687 static HRESULT WINAPI
ProtocolHandler_Suspend(IInternetProtocol
*iface
)
689 BindProtocol
*This
= impl_from_IInternetProtocol(iface
);
690 FIXME("(%p)\n", This
);
694 static HRESULT WINAPI
ProtocolHandler_Resume(IInternetProtocol
*iface
)
696 BindProtocol
*This
= impl_from_IInternetProtocol(iface
);
697 FIXME("(%p)\n", This
);
701 static HRESULT WINAPI
ProtocolHandler_Read(IInternetProtocol
*iface
, void *pv
,
702 ULONG cb
, ULONG
*pcbRead
)
704 BindProtocol
*This
= impl_from_IInternetProtocol(iface
);
708 TRACE("(%p)->(%p %u %p)\n", This
, pv
, cb
, pcbRead
);
711 read
= min(cb
, This
->buf_size
);
712 memcpy(pv
, This
->buf
, read
);
714 if(read
== This
->buf_size
) {
715 heap_free(This
->buf
);
718 memmove(This
->buf
, This
->buf
+cb
, This
->buf_size
-cb
);
721 This
->buf_size
-= read
;
727 hres
= IInternetProtocol_Read(This
->protocol
, (BYTE
*)pv
+read
, cb
-read
, &cread
);
735 static HRESULT WINAPI
ProtocolHandler_Seek(IInternetProtocol
*iface
, LARGE_INTEGER dlibMove
,
736 DWORD dwOrigin
, ULARGE_INTEGER
*plibNewPosition
)
738 BindProtocol
*This
= impl_from_IInternetProtocol(iface
);
739 FIXME("(%p)->(%d %d %p)\n", This
, dlibMove
.u
.LowPart
, dwOrigin
, plibNewPosition
);
743 static HRESULT WINAPI
ProtocolHandler_LockRequest(IInternetProtocol
*iface
, DWORD dwOptions
)
745 BindProtocol
*This
= impl_from_IInternetProtocol(iface
);
747 TRACE("(%p)->(%08x)\n", This
, dwOptions
);
749 return IInternetProtocol_LockRequest(This
->protocol
, dwOptions
);
752 static HRESULT WINAPI
ProtocolHandler_UnlockRequest(IInternetProtocol
*iface
)
754 BindProtocol
*This
= impl_from_IInternetProtocol(iface
);
756 TRACE("(%p)\n", This
);
758 return IInternetProtocol_UnlockRequest(This
->protocol
);
761 static const IInternetProtocolVtbl InternetProtocolHandlerVtbl
= {
762 ProtocolHandler_QueryInterface
,
763 ProtocolHandler_AddRef
,
764 ProtocolHandler_Release
,
765 ProtocolHandler_Start
,
766 ProtocolHandler_Continue
,
767 ProtocolHandler_Abort
,
768 ProtocolHandler_Terminate
,
769 ProtocolHandler_Suspend
,
770 ProtocolHandler_Resume
,
771 ProtocolHandler_Read
,
772 ProtocolHandler_Seek
,
773 ProtocolHandler_LockRequest
,
774 ProtocolHandler_UnlockRequest
777 static inline BindProtocol
*impl_from_IInternetBindInfo(IInternetBindInfo
*iface
)
779 return CONTAINING_RECORD(iface
, BindProtocol
, IInternetBindInfo_iface
);
782 static HRESULT WINAPI
BindInfo_QueryInterface(IInternetBindInfo
*iface
,
783 REFIID riid
, void **ppv
)
785 BindProtocol
*This
= impl_from_IInternetBindInfo(iface
);
786 return IInternetProtocolEx_QueryInterface(&This
->IInternetProtocolEx_iface
, riid
, ppv
);
789 static ULONG WINAPI
BindInfo_AddRef(IInternetBindInfo
*iface
)
791 BindProtocol
*This
= impl_from_IInternetBindInfo(iface
);
792 return IInternetProtocolEx_AddRef(&This
->IInternetProtocolEx_iface
);
795 static ULONG WINAPI
BindInfo_Release(IInternetBindInfo
*iface
)
797 BindProtocol
*This
= impl_from_IInternetBindInfo(iface
);
798 return IInternetProtocolEx_Release(&This
->IInternetProtocolEx_iface
);
801 static HRESULT WINAPI
BindInfo_GetBindInfo(IInternetBindInfo
*iface
,
802 DWORD
*grfBINDF
, BINDINFO
*pbindinfo
)
804 BindProtocol
*This
= impl_from_IInternetBindInfo(iface
);
807 TRACE("(%p)->(%p %p)\n", This
, grfBINDF
, pbindinfo
);
809 hres
= IInternetBindInfo_GetBindInfo(This
->bind_info
, grfBINDF
, pbindinfo
);
811 WARN("GetBindInfo failed: %08x\n", hres
);
815 *grfBINDF
|= BINDF_FROMURLMON
;
819 static HRESULT WINAPI
BindInfo_GetBindString(IInternetBindInfo
*iface
,
820 ULONG ulStringType
, LPOLESTR
*ppwzStr
, ULONG cEl
, ULONG
*pcElFetched
)
822 BindProtocol
*This
= impl_from_IInternetBindInfo(iface
);
824 TRACE("(%p)->(%d %p %d %p)\n", This
, ulStringType
, ppwzStr
, cEl
, pcElFetched
);
826 return IInternetBindInfo_GetBindString(This
->bind_info
, ulStringType
, ppwzStr
, cEl
, pcElFetched
);
829 static const IInternetBindInfoVtbl InternetBindInfoVtbl
= {
830 BindInfo_QueryInterface
,
833 BindInfo_GetBindInfo
,
834 BindInfo_GetBindString
837 static inline BindProtocol
*impl_from_IInternetPriority(IInternetPriority
*iface
)
839 return CONTAINING_RECORD(iface
, BindProtocol
, IInternetPriority_iface
);
842 static HRESULT WINAPI
InternetPriority_QueryInterface(IInternetPriority
*iface
,
843 REFIID riid
, void **ppv
)
845 BindProtocol
*This
= impl_from_IInternetPriority(iface
);
846 return IInternetProtocolEx_QueryInterface(&This
->IInternetProtocolEx_iface
, riid
, ppv
);
849 static ULONG WINAPI
InternetPriority_AddRef(IInternetPriority
*iface
)
851 BindProtocol
*This
= impl_from_IInternetPriority(iface
);
852 return IInternetProtocolEx_AddRef(&This
->IInternetProtocolEx_iface
);
855 static ULONG WINAPI
InternetPriority_Release(IInternetPriority
*iface
)
857 BindProtocol
*This
= impl_from_IInternetPriority(iface
);
858 return IInternetProtocolEx_Release(&This
->IInternetProtocolEx_iface
);
861 static HRESULT WINAPI
InternetPriority_SetPriority(IInternetPriority
*iface
, LONG nPriority
)
863 BindProtocol
*This
= impl_from_IInternetPriority(iface
);
865 TRACE("(%p)->(%d)\n", This
, nPriority
);
867 This
->priority
= nPriority
;
871 static HRESULT WINAPI
InternetPriority_GetPriority(IInternetPriority
*iface
, LONG
*pnPriority
)
873 BindProtocol
*This
= impl_from_IInternetPriority(iface
);
875 TRACE("(%p)->(%p)\n", This
, pnPriority
);
877 *pnPriority
= This
->priority
;
881 static const IInternetPriorityVtbl InternetPriorityVtbl
= {
882 InternetPriority_QueryInterface
,
883 InternetPriority_AddRef
,
884 InternetPriority_Release
,
885 InternetPriority_SetPriority
,
886 InternetPriority_GetPriority
890 static inline BindProtocol
*impl_from_IInternetProtocolSink(IInternetProtocolSink
*iface
)
892 return CONTAINING_RECORD(iface
, BindProtocol
, IInternetProtocolSink_iface
);
895 static HRESULT WINAPI
BPInternetProtocolSink_QueryInterface(IInternetProtocolSink
*iface
,
896 REFIID riid
, void **ppv
)
898 BindProtocol
*This
= impl_from_IInternetProtocolSink(iface
);
899 return IInternetProtocolEx_QueryInterface(&This
->IInternetProtocolEx_iface
, riid
, ppv
);
902 static ULONG WINAPI
BPInternetProtocolSink_AddRef(IInternetProtocolSink
*iface
)
904 BindProtocol
*This
= impl_from_IInternetProtocolSink(iface
);
905 return IInternetProtocolEx_AddRef(&This
->IInternetProtocolEx_iface
);
908 static ULONG WINAPI
BPInternetProtocolSink_Release(IInternetProtocolSink
*iface
)
910 BindProtocol
*This
= impl_from_IInternetProtocolSink(iface
);
911 return IInternetProtocolEx_Release(&This
->IInternetProtocolEx_iface
);
915 task_header_t header
;
919 static void switch_proc(BindProtocol
*bind
, task_header_t
*t
)
921 switch_task_t
*task
= (switch_task_t
*)t
;
923 IInternetProtocol_Continue(bind
->protocol_handler
, task
->data
);
928 static HRESULT WINAPI
BPInternetProtocolSink_Switch(IInternetProtocolSink
*iface
,
929 PROTOCOLDATA
*pProtocolData
)
931 BindProtocol
*This
= impl_from_IInternetProtocolSink(iface
);
934 TRACE("(%p)->(%p)\n", This
, pProtocolData
);
936 TRACE("flags %x state %x data %p cb %u\n", pProtocolData
->grfFlags
, pProtocolData
->dwState
,
937 pProtocolData
->pData
, pProtocolData
->cbData
);
939 data
= heap_alloc(sizeof(PROTOCOLDATA
));
941 return E_OUTOFMEMORY
;
942 memcpy(data
, pProtocolData
, sizeof(PROTOCOLDATA
));
944 if(!do_direct_notif(This
)) {
947 task
= heap_alloc(sizeof(switch_task_t
));
949 return E_OUTOFMEMORY
;
953 push_task(This
, &task
->header
, switch_proc
);
957 if(!This
->protocol_sink
) {
958 IInternetProtocol_Continue(This
->protocol_handler
, data
);
962 return IInternetProtocolSink_Switch(This
->protocol_sink
, data
);
965 static void report_progress(BindProtocol
*This
, ULONG status_code
, LPCWSTR status_text
)
967 switch(status_code
) {
968 case BINDSTATUS_FINDINGRESOURCE
:
969 case BINDSTATUS_CONNECTING
:
970 case BINDSTATUS_REDIRECTING
:
971 case BINDSTATUS_SENDINGREQUEST
:
972 case BINDSTATUS_CACHEFILENAMEAVAILABLE
:
973 case BINDSTATUS_DIRECTBIND
:
974 case BINDSTATUS_ACCEPTRANGES
:
975 if(This
->protocol_sink
)
976 IInternetProtocolSink_ReportProgress(This
->protocol_sink
, status_code
, status_text
);
979 case BINDSTATUS_BEGINDOWNLOADDATA
:
980 if(This
->protocol_sink
)
981 IInternetProtocolSink_ReportData(This
->protocol_sink
, This
->bscf
, This
->progress
, This
->progress_max
);
984 case BINDSTATUS_MIMETYPEAVAILABLE
:
985 mime_available(This
, status_text
, FALSE
);
988 case BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE
:
989 mime_available(This
, status_text
, TRUE
);
993 FIXME("unsupported ulStatusCode %u\n", status_code
);
998 task_header_t header
;
1002 } on_progress_task_t
;
1004 static void on_progress_proc(BindProtocol
*This
, task_header_t
*t
)
1006 on_progress_task_t
*task
= (on_progress_task_t
*)t
;
1008 report_progress(This
, task
->status_code
, task
->status_text
);
1010 heap_free(task
->status_text
);
1014 static HRESULT WINAPI
BPInternetProtocolSink_ReportProgress(IInternetProtocolSink
*iface
,
1015 ULONG ulStatusCode
, LPCWSTR szStatusText
)
1017 BindProtocol
*This
= impl_from_IInternetProtocolSink(iface
);
1019 TRACE("(%p)->(%u %s)\n", This
, ulStatusCode
, debugstr_w(szStatusText
));
1021 if(do_direct_notif(This
)) {
1022 report_progress(This
, ulStatusCode
, szStatusText
);
1024 on_progress_task_t
*task
;
1026 task
= heap_alloc(sizeof(on_progress_task_t
));
1028 task
->status_code
= ulStatusCode
;
1029 task
->status_text
= heap_strdupW(szStatusText
);
1031 push_task(This
, &task
->header
, on_progress_proc
);
1037 static HRESULT
report_data(BindProtocol
*This
, DWORD bscf
, ULONG progress
, ULONG progress_max
)
1040 This
->progress
= progress
;
1041 This
->progress_max
= progress_max
;
1043 if(!This
->protocol_sink
)
1046 if((This
->pi
& PI_MIMEVERIFICATION
) && !This
->reported_mime
) {
1047 BYTE buf
[BUFFER_SIZE
];
1054 hres
= IInternetProtocol_Read(This
->protocol
, buf
,
1055 sizeof(buf
)-This
->buf_size
, &read
);
1056 if(FAILED(hres
) && hres
!= E_PENDING
)
1060 This
->buf
= heap_alloc(BUFFER_SIZE
);
1062 return E_OUTOFMEMORY
;
1063 }else if(read
+ This
->buf_size
> BUFFER_SIZE
) {
1066 tmp
= heap_realloc(This
->buf
, read
+This
->buf_size
);
1068 return E_OUTOFMEMORY
;
1072 memcpy(This
->buf
+This
->buf_size
, buf
, read
);
1073 This
->buf_size
+= read
;
1074 }while(This
->buf_size
< MIME_TEST_SIZE
&& hres
== S_OK
);
1076 if(This
->buf_size
< MIME_TEST_SIZE
&& hres
!= S_FALSE
)
1079 bscf
= BSCF_FIRSTDATANOTIFICATION
;
1081 bscf
|= BSCF_LASTDATANOTIFICATION
|BSCF_DATAFULLYAVAILABLE
;
1083 if(!This
->reported_mime
) {
1086 hres
= IUri_GetRawUri(This
->uri
, &raw_uri
);
1090 hres
= FindMimeFromData(NULL
, raw_uri
, This
->buf
, min(This
->buf_size
, MIME_TEST_SIZE
),
1091 This
->mime
, 0, &mime
, 0);
1092 SysFreeString(raw_uri
);
1096 mime_available(This
, mime
, TRUE
);
1097 CoTaskMemFree(mime
);
1101 if(!This
->protocol_sink
)
1104 return IInternetProtocolSink_ReportData(This
->protocol_sink
, bscf
, progress
, progress_max
);
1108 task_header_t header
;
1112 } report_data_task_t
;
1114 static void report_data_proc(BindProtocol
*This
, task_header_t
*t
)
1116 report_data_task_t
*task
= (report_data_task_t
*)t
;
1118 report_data(This
, task
->bscf
, task
->progress
, task
->progress_max
);
1122 static HRESULT WINAPI
BPInternetProtocolSink_ReportData(IInternetProtocolSink
*iface
,
1123 DWORD grfBSCF
, ULONG ulProgress
, ULONG ulProgressMax
)
1125 BindProtocol
*This
= impl_from_IInternetProtocolSink(iface
);
1127 TRACE("(%p)->(%d %u %u)\n", This
, grfBSCF
, ulProgress
, ulProgressMax
);
1129 if(!This
->protocol_sink
)
1132 if(!do_direct_notif(This
)) {
1133 report_data_task_t
*task
;
1135 task
= heap_alloc(sizeof(report_data_task_t
));
1137 return E_OUTOFMEMORY
;
1139 task
->bscf
= grfBSCF
;
1140 task
->progress
= ulProgress
;
1141 task
->progress_max
= ulProgressMax
;
1143 push_task(This
, &task
->header
, report_data_proc
);
1147 return report_data(This
, grfBSCF
, ulProgress
, ulProgressMax
);
1151 task_header_t header
;
1156 } report_result_task_t
;
1158 static void report_result_proc(BindProtocol
*This
, task_header_t
*t
)
1160 report_result_task_t
*task
= (report_result_task_t
*)t
;
1162 if(This
->protocol_sink
)
1163 IInternetProtocolSink_ReportResult(This
->protocol_sink
, task
->hres
, task
->err
, task
->str
);
1165 heap_free(task
->str
);
1169 static HRESULT WINAPI
BPInternetProtocolSink_ReportResult(IInternetProtocolSink
*iface
,
1170 HRESULT hrResult
, DWORD dwError
, LPCWSTR szResult
)
1172 BindProtocol
*This
= impl_from_IInternetProtocolSink(iface
);
1174 TRACE("(%p)->(%08x %d %s)\n", This
, hrResult
, dwError
, debugstr_w(szResult
));
1176 if(!This
->protocol_sink
)
1179 This
->reported_result
= TRUE
;
1181 if(!do_direct_notif(This
)) {
1182 report_result_task_t
*task
;
1184 task
= heap_alloc(sizeof(report_result_task_t
));
1186 return E_OUTOFMEMORY
;
1188 task
->hres
= hrResult
;
1189 task
->err
= dwError
;
1190 task
->str
= heap_strdupW(szResult
);
1192 push_task(This
, &task
->header
, report_result_proc
);
1196 return IInternetProtocolSink_ReportResult(This
->protocol_sink
, hrResult
, dwError
, szResult
);
1199 static const IInternetProtocolSinkVtbl InternetProtocolSinkVtbl
= {
1200 BPInternetProtocolSink_QueryInterface
,
1201 BPInternetProtocolSink_AddRef
,
1202 BPInternetProtocolSink_Release
,
1203 BPInternetProtocolSink_Switch
,
1204 BPInternetProtocolSink_ReportProgress
,
1205 BPInternetProtocolSink_ReportData
,
1206 BPInternetProtocolSink_ReportResult
1209 static inline BindProtocol
*impl_from_IWinInetHttpInfo(IWinInetHttpInfo
*iface
)
1211 return CONTAINING_RECORD(iface
, BindProtocol
, IWinInetHttpInfo_iface
);
1214 static HRESULT WINAPI
WinInetHttpInfo_QueryInterface(IWinInetHttpInfo
*iface
, REFIID riid
, void **ppv
)
1216 BindProtocol
*This
= impl_from_IWinInetHttpInfo(iface
);
1217 return IInternetProtocolEx_QueryInterface(&This
->IInternetProtocolEx_iface
, riid
, ppv
);
1220 static ULONG WINAPI
WinInetHttpInfo_AddRef(IWinInetHttpInfo
*iface
)
1222 BindProtocol
*This
= impl_from_IWinInetHttpInfo(iface
);
1223 return IInternetProtocolEx_AddRef(&This
->IInternetProtocolEx_iface
);
1226 static ULONG WINAPI
WinInetHttpInfo_Release(IWinInetHttpInfo
*iface
)
1228 BindProtocol
*This
= impl_from_IWinInetHttpInfo(iface
);
1229 return IInternetProtocolEx_Release(&This
->IInternetProtocolEx_iface
);
1232 static HRESULT WINAPI
WinInetHttpInfo_QueryOption(IWinInetHttpInfo
*iface
, DWORD dwOption
,
1233 void *pBuffer
, DWORD
*pcbBuffer
)
1235 BindProtocol
*This
= impl_from_IWinInetHttpInfo(iface
);
1236 FIXME("(%p)->(%x %p %p)\n", This
, dwOption
, pBuffer
, pcbBuffer
);
1240 static HRESULT WINAPI
WinInetHttpInfo_QueryInfo(IWinInetHttpInfo
*iface
, DWORD dwOption
,
1241 void *pBuffer
, DWORD
*pcbBuffer
, DWORD
*pdwFlags
, DWORD
*pdwReserved
)
1243 BindProtocol
*This
= impl_from_IWinInetHttpInfo(iface
);
1244 FIXME("(%p)->(%x %p %p %p %p)\n", This
, dwOption
, pBuffer
, pcbBuffer
, pdwFlags
, pdwReserved
);
1248 static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl
= {
1249 WinInetHttpInfo_QueryInterface
,
1250 WinInetHttpInfo_AddRef
,
1251 WinInetHttpInfo_Release
,
1252 WinInetHttpInfo_QueryOption
,
1253 WinInetHttpInfo_QueryInfo
1256 static inline BindProtocol
*impl_from_IServiceProvider(IServiceProvider
*iface
)
1258 return CONTAINING_RECORD(iface
, BindProtocol
, IServiceProvider_iface
);
1261 static HRESULT WINAPI
BPServiceProvider_QueryInterface(IServiceProvider
*iface
,
1262 REFIID riid
, void **ppv
)
1264 BindProtocol
*This
= impl_from_IServiceProvider(iface
);
1265 return IInternetProtocolEx_QueryInterface(&This
->IInternetProtocolEx_iface
, riid
, ppv
);
1268 static ULONG WINAPI
BPServiceProvider_AddRef(IServiceProvider
*iface
)
1270 BindProtocol
*This
= impl_from_IServiceProvider(iface
);
1271 return IInternetProtocolEx_AddRef(&This
->IInternetProtocolEx_iface
);
1274 static ULONG WINAPI
BPServiceProvider_Release(IServiceProvider
*iface
)
1276 BindProtocol
*This
= impl_from_IServiceProvider(iface
);
1277 return IInternetProtocolEx_Release(&This
->IInternetProtocolEx_iface
);
1280 static HRESULT WINAPI
BPServiceProvider_QueryService(IServiceProvider
*iface
,
1281 REFGUID guidService
, REFIID riid
, void **ppv
)
1283 BindProtocol
*This
= impl_from_IServiceProvider(iface
);
1285 TRACE("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
1287 if(!This
->service_provider
)
1288 return E_NOINTERFACE
;
1290 return IServiceProvider_QueryService(This
->service_provider
, guidService
, riid
, ppv
);
1293 static const IServiceProviderVtbl ServiceProviderVtbl
= {
1294 BPServiceProvider_QueryInterface
,
1295 BPServiceProvider_AddRef
,
1296 BPServiceProvider_Release
,
1297 BPServiceProvider_QueryService
1300 HRESULT
create_binding_protocol(BOOL from_urlmon
, BindProtocol
**protocol
)
1302 BindProtocol
*ret
= heap_alloc_zero(sizeof(BindProtocol
));
1304 ret
->IInternetProtocolEx_iface
.lpVtbl
= &BindProtocolVtbl
;
1305 ret
->IInternetBindInfo_iface
.lpVtbl
= &InternetBindInfoVtbl
;
1306 ret
->IInternetPriority_iface
.lpVtbl
= &InternetPriorityVtbl
;
1307 ret
->IServiceProvider_iface
.lpVtbl
= &ServiceProviderVtbl
;
1308 ret
->IInternetProtocolSink_iface
.lpVtbl
= &InternetProtocolSinkVtbl
;
1309 ret
->IWinInetHttpInfo_iface
.lpVtbl
= &WinInetHttpInfoVtbl
;
1311 ret
->default_protocol_handler
.IInternetProtocol_iface
.lpVtbl
= &InternetProtocolHandlerVtbl
;
1314 ret
->from_urlmon
= from_urlmon
;
1315 ret
->apartment_thread
= GetCurrentThreadId();
1316 ret
->notif_hwnd
= get_notif_hwnd();
1317 ret
->protocol_handler
= &ret
->default_protocol_handler
.IInternetProtocol_iface
;
1318 InitializeCriticalSection(&ret
->section
);
1319 ret
->section
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": BindProtocol.section");
1321 URLMON_LockModule();