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 BINDINFO(x) ((IInternetBindInfo*) &(x)->lpInternetBindInfoVtbl)
32 #define PRIORITY(x) ((IInternetPriority*) &(x)->lpInternetPriorityVtbl)
33 #define HTTPINFO(x) ((IWinInetHttpInfo*) &(x)->lpIWinInetHttpInfoVtbl)
34 #define SERVPROV(x) ((IServiceProvider*) &(x)->lpServiceProviderVtbl)
36 #define PROTOCOLHANDLER(x) ((IInternetProtocol*) &(x)->lpIInternetProtocolHandlerVtbl)
38 #define BUFFER_SIZE 2048
39 #define MIME_TEST_SIZE 255
41 #define WM_MK_CONTINUE (WM_USER+101)
42 #define WM_MK_RELEASE (WM_USER+102)
44 static LRESULT WINAPI notif_wnd_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
47 case WM_MK_CONTINUE: {
48 BindProtocol *This = (BindProtocol*)lParam;
52 EnterCriticalSection(&This->section);
54 task = This->task_queue_head;
56 This->task_queue_head = task->next;
57 if(!This->task_queue_head)
58 This->task_queue_tail = NULL;
61 LeaveCriticalSection(&This->section);
66 This->continue_call++;
67 task->proc(This, task);
68 This->continue_call--;
71 IInternetProtocolEx_Release(PROTOCOLEX(This));
75 tls_data_t *data = get_tls_data();
77 if(!--data->notif_hwnd_cnt) {
79 data->notif_hwnd = NULL;
84 return DefWindowProcW(hwnd, msg, wParam, lParam);
87 HWND get_notif_hwnd(void)
89 static ATOM wnd_class = 0;
92 static const WCHAR wszURLMonikerNotificationWindow[] =
93 {'U','R','L',' ','M','o','n','i','k','e','r',' ',
94 'N','o','t','i','f','i','c','a','t','i','o','n',' ','W','i','n','d','o','w',0};
96 tls_data = get_tls_data();
100 if(tls_data->notif_hwnd_cnt) {
101 tls_data->notif_hwnd_cnt++;
102 return tls_data->notif_hwnd;
106 static WNDCLASSEXW wndclass = {
108 notif_wnd_proc, 0, 0,
109 NULL, NULL, NULL, NULL, NULL,
110 wszURLMonikerNotificationWindow,
114 wndclass.hInstance = hProxyDll;
116 wnd_class = RegisterClassExW(&wndclass);
117 if (!wnd_class && GetLastError() == ERROR_CLASS_ALREADY_EXISTS)
121 tls_data->notif_hwnd = CreateWindowExW(0, wszURLMonikerNotificationWindow,
122 wszURLMonikerNotificationWindow, 0, 0, 0, 0, 0, HWND_MESSAGE,
123 NULL, hProxyDll, NULL);
124 if(tls_data->notif_hwnd)
125 tls_data->notif_hwnd_cnt++;
127 TRACE("hwnd = %p\n", tls_data->notif_hwnd);
129 return tls_data->notif_hwnd;
132 void release_notif_hwnd(HWND hwnd)
134 tls_data_t *data = get_tls_data();
139 if(data->notif_hwnd != hwnd) {
140 PostMessageW(data->notif_hwnd, WM_MK_RELEASE, 0, 0);
144 if(!--data->notif_hwnd_cnt) {
145 DestroyWindow(data->notif_hwnd);
146 data->notif_hwnd = NULL;
150 static void push_task(BindProtocol *This, task_header_t *task, task_proc_t proc)
152 BOOL do_post = FALSE;
157 EnterCriticalSection(&This->section);
159 if(This->task_queue_tail) {
160 This->task_queue_tail->next = task;
161 This->task_queue_tail = task;
163 This->task_queue_tail = This->task_queue_head = task;
164 do_post = !This->continue_call;
167 LeaveCriticalSection(&This->section);
170 IInternetProtocolEx_AddRef(PROTOCOLEX(This));
171 PostMessageW(This->notif_hwnd, WM_MK_CONTINUE, 0, (LPARAM)This);
175 static inline BOOL do_direct_notif(BindProtocol *This)
177 return !(This->pi & PI_APARTMENTTHREADED) || (This->apartment_thread == GetCurrentThreadId() && !This->continue_call);
180 static HRESULT handle_mime_filter(BindProtocol *This, IInternetProtocol *mime_filter, LPCWSTR mime)
182 PROTOCOLFILTERDATA filter_data = { sizeof(PROTOCOLFILTERDATA), NULL, NULL, NULL, 0 };
183 IInternetProtocolSink *protocol_sink, *old_sink;
184 ProtocolProxy *filter_proxy;
187 hres = IInternetProtocol_QueryInterface(mime_filter, &IID_IInternetProtocolSink, (void**)&protocol_sink);
191 hres = create_protocol_proxy(&This->default_protocol_handler.IInternetProtocol_iface, This->protocol_sink, &filter_proxy);
193 IInternetProtocolSink_Release(protocol_sink);
197 old_sink = This->protocol_sink;
198 This->protocol_sink = protocol_sink;
199 This->filter_proxy = filter_proxy;
201 IInternetProtocol_AddRef(mime_filter);
202 This->protocol_handler = mime_filter;
204 filter_data.pProtocol = PROTOCOL(filter_proxy);
205 hres = IInternetProtocol_Start(mime_filter, mime, PROTSINK(filter_proxy), BINDINFO(This),
206 PI_FILTER_MODE|PI_FORCE_ASYNC, (HANDLE_PTR)&filter_data);
208 IInternetProtocolSink_Release(old_sink);
212 IInternetProtocolSink_ReportProgress(old_sink, BINDSTATUS_LOADINGMIMEHANDLER, NULL);
213 IInternetProtocolSink_Release(old_sink);
215 This->pi &= ~PI_MIMEVERIFICATION; /* FIXME: more tests */
219 static void mime_available(BindProtocol *This, LPCWSTR mime, BOOL verified)
221 IInternetProtocol *mime_filter;
224 heap_free(This->mime);
227 mime_filter = get_mime_filter(mime);
229 TRACE("Got mime filter for %s\n", debugstr_w(mime));
231 hres = handle_mime_filter(This, mime_filter, mime);
232 IInternetProtocol_Release(mime_filter);
234 FIXME("MIME filter failed: %08x\n", hres);
236 This->mime = heap_strdupW(mime);
238 if(verified || !(This->pi & PI_MIMEVERIFICATION)) {
239 This->reported_mime = TRUE;
241 if(This->protocol_sink)
242 IInternetProtocolSink_ReportProgress(This->protocol_sink, BINDSTATUS_MIMETYPEAVAILABLE, mime);
247 #define PROTOCOL_THIS(iface) DEFINE_THIS(BindProtocol, IInternetProtocolEx, iface)
249 static HRESULT WINAPI BindProtocol_QueryInterface(IInternetProtocolEx *iface, REFIID riid, void **ppv)
251 BindProtocol *This = PROTOCOL_THIS(iface);
254 if(IsEqualGUID(&IID_IUnknown, riid)) {
255 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
256 *ppv = PROTOCOLEX(This);
257 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
258 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
259 *ppv = PROTOCOLEX(This);
260 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
261 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
262 *ppv = PROTOCOLEX(This);
263 }else if(IsEqualGUID(&IID_IInternetProtocolEx, riid)) {
264 TRACE("(%p)->(IID_IInternetProtocolEx %p)\n", This, ppv);
265 *ppv = PROTOCOLEX(This);
266 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
267 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
268 *ppv = BINDINFO(This);
269 }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
270 TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
271 *ppv = PRIORITY(This);
272 }else if(IsEqualGUID(&IID_IAuthenticate, riid)) {
273 FIXME("(%p)->(IID_IAuthenticate %p)\n", This, ppv);
274 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
275 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
276 *ppv = SERVPROV(This);
277 }else if(IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
278 TRACE("(%p)->(IID_IInternetProtocolSink %p)\n", This, ppv);
279 *ppv = PROTSINK(This);
280 }else if(IsEqualGUID(&IID_IWinInetInfo, riid)) {
281 TRACE("(%p)->(IID_IWinInetInfo %p)\n", This, ppv);
284 IWinInetInfo *inet_info;
287 hres = IInternetProtocol_QueryInterface(This->protocol, &IID_IWinInetInfo, (void**)&inet_info);
288 if(SUCCEEDED(hres)) {
289 *ppv = HTTPINFO(This);
290 IWinInetInfo_Release(inet_info);
293 }else if(IsEqualGUID(&IID_IWinInetHttpInfo, riid)) {
294 TRACE("(%p)->(IID_IWinInetHttpInfo %p)\n", This, ppv);
297 IWinInetHttpInfo *http_info;
300 hres = IInternetProtocol_QueryInterface(This->protocol, &IID_IWinInetHttpInfo, (void**)&http_info);
301 if(SUCCEEDED(hres)) {
302 *ppv = HTTPINFO(This);
303 IWinInetHttpInfo_Release(http_info);
307 WARN("not supported interface %s\n", debugstr_guid(riid));
311 return E_NOINTERFACE;
313 IUnknown_AddRef((IUnknown*)*ppv);
317 static ULONG WINAPI BindProtocol_AddRef(IInternetProtocolEx *iface)
319 BindProtocol *This = PROTOCOL_THIS(iface);
320 LONG ref = InterlockedIncrement(&This->ref);
321 TRACE("(%p) ref=%d\n", This, ref);
325 static ULONG WINAPI BindProtocol_Release(IInternetProtocolEx *iface)
327 BindProtocol *This = PROTOCOL_THIS(iface);
328 LONG ref = InterlockedDecrement(&This->ref);
330 TRACE("(%p) ref=%d\n", This, ref);
333 if(This->wininet_info)
334 IWinInetInfo_Release(This->wininet_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(PROTOCOL(This->filter_proxy));
344 IUri_Release(This->uri);
346 set_binding_sink(This, NULL, NULL);
349 release_notif_hwnd(This->notif_hwnd);
350 DeleteCriticalSection(&This->section);
352 heap_free(This->mime);
355 URLMON_UnlockModule();
361 static HRESULT WINAPI BindProtocol_Start(IInternetProtocolEx *iface, LPCWSTR szUrl,
362 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
363 DWORD grfPI, HANDLE_PTR dwReserved)
365 BindProtocol *This = PROTOCOL_THIS(iface);
369 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
370 pOIBindInfo, grfPI, dwReserved);
372 hres = CreateUri(szUrl, Uri_CREATE_FILE_USE_DOS_PATH, 0, &uri);
376 hres = IInternetProtocolEx_StartEx(PROTOCOLEX(This), uri, pOIProtSink, pOIBindInfo,
377 grfPI, (HANDLE*)dwReserved);
383 static HRESULT WINAPI BindProtocol_Continue(IInternetProtocolEx *iface, PROTOCOLDATA *pProtocolData)
385 BindProtocol *This = PROTOCOL_THIS(iface);
387 TRACE("(%p)->(%p)\n", This, pProtocolData);
389 return IInternetProtocol_Continue(This->protocol_handler, pProtocolData);
392 static HRESULT WINAPI BindProtocol_Abort(IInternetProtocolEx *iface, HRESULT hrReason,
395 BindProtocol *This = PROTOCOL_THIS(iface);
397 TRACE("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
399 return IInternetProtocol_Abort(This->protocol_handler, hrReason, dwOptions);
402 static HRESULT WINAPI BindProtocol_Terminate(IInternetProtocolEx *iface, DWORD dwOptions)
404 BindProtocol *This = PROTOCOL_THIS(iface);
406 TRACE("(%p)->(%08x)\n", This, dwOptions);
408 return IInternetProtocol_Terminate(This->protocol_handler, dwOptions);
411 static HRESULT WINAPI BindProtocol_Suspend(IInternetProtocolEx *iface)
413 BindProtocol *This = PROTOCOL_THIS(iface);
414 FIXME("(%p)\n", This);
418 static HRESULT WINAPI BindProtocol_Resume(IInternetProtocolEx *iface)
420 BindProtocol *This = PROTOCOL_THIS(iface);
421 FIXME("(%p)\n", This);
425 static HRESULT WINAPI BindProtocol_Read(IInternetProtocolEx *iface, void *pv,
426 ULONG cb, ULONG *pcbRead)
428 BindProtocol *This = PROTOCOL_THIS(iface);
430 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
434 return IInternetProtocol_Read(This->protocol_handler, pv, cb, pcbRead);
437 static HRESULT WINAPI BindProtocol_Seek(IInternetProtocolEx *iface, LARGE_INTEGER dlibMove,
438 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
440 BindProtocol *This = PROTOCOL_THIS(iface);
441 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
445 static HRESULT WINAPI BindProtocol_LockRequest(IInternetProtocolEx *iface, DWORD dwOptions)
447 BindProtocol *This = PROTOCOL_THIS(iface);
449 TRACE("(%p)->(%08x)\n", This, dwOptions);
451 return IInternetProtocol_LockRequest(This->protocol_handler, dwOptions);
454 static HRESULT WINAPI BindProtocol_UnlockRequest(IInternetProtocolEx *iface)
456 BindProtocol *This = PROTOCOL_THIS(iface);
458 TRACE("(%p)\n", This);
460 return IInternetProtocol_UnlockRequest(This->protocol_handler);
463 static HRESULT WINAPI BindProtocol_StartEx(IInternetProtocolEx *iface, IUri *pUri,
464 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
465 DWORD grfPI, HANDLE *dwReserved)
467 BindProtocol *This = PROTOCOL_THIS(iface);
468 IInternetProtocol *protocol = NULL;
469 IInternetProtocolEx *protocolex;
470 IInternetPriority *priority;
471 IServiceProvider *service_provider;
472 BOOL urlmon_protocol = FALSE;
473 CLSID clsid = IID_NULL;
477 TRACE("(%p)->(%p %p %p %08x %p)\n", This, pUri, pOIProtSink, pOIBindInfo, grfPI, dwReserved);
479 if(!pUri || !pOIProtSink || !pOIBindInfo)
487 hres = IInternetProtocolSink_QueryInterface(pOIProtSink, &IID_IServiceProvider,
488 (void**)&service_provider);
489 if(SUCCEEDED(hres)) {
490 /* FIXME: What's protocol CLSID here? */
491 IServiceProvider_QueryService(service_provider, &IID_IInternetProtocol,
492 &IID_IInternetProtocol, (void**)&protocol);
493 IServiceProvider_Release(service_provider);
500 hres = get_protocol_handler(pUri, &clsid, &urlmon_protocol, &cf);
504 if(This->from_urlmon) {
505 hres = IClassFactory_CreateInstance(cf, NULL, &IID_IInternetProtocol, (void**)&protocol);
506 IClassFactory_Release(cf);
510 hres = IClassFactory_CreateInstance(cf, (IUnknown*)BINDINFO(This),
511 &IID_IUnknown, (void**)&unk);
512 IClassFactory_Release(cf);
516 hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocol, (void**)&protocol);
517 IUnknown_Release(unk);
523 StringFromCLSID(&clsid, &clsid_str);
524 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_PROTOCOLCLASSID, clsid_str);
525 CoTaskMemFree(clsid_str);
527 This->protocol = protocol;
530 IInternetProtocol_QueryInterface(protocol, &IID_IWinInetInfo, (void**)&This->wininet_info);
532 set_binding_sink(This, pOIProtSink, pOIBindInfo);
534 hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetPriority, (void**)&priority);
535 if(SUCCEEDED(hres)) {
536 IInternetPriority_SetPriority(priority, This->priority);
537 IInternetPriority_Release(priority);
540 hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetProtocolEx, (void**)&protocolex);
541 if(SUCCEEDED(hres)) {
542 hres = IInternetProtocolEx_StartEx(protocolex, pUri, PROTSINK(This), BINDINFO(This), 0, NULL);
543 IInternetProtocolEx_Release(protocolex);
547 hres = IUri_GetDisplayUri(pUri, &display_uri);
551 hres = IInternetProtocol_Start(protocol, display_uri, PROTSINK(This), BINDINFO(This), 0, 0);
552 SysFreeString(display_uri);
558 void set_binding_sink(BindProtocol *This, IInternetProtocolSink *sink, IInternetBindInfo *bind_info)
560 IInternetProtocolSink *prev_sink;
561 IServiceProvider *service_provider = NULL;
564 IInternetProtocolSink_AddRef(sink);
565 prev_sink = InterlockedExchangePointer((void**)&This->protocol_sink, sink);
567 IInternetProtocolSink_Release(prev_sink);
570 IInternetProtocolSink_QueryInterface(sink, &IID_IServiceProvider, (void**)&service_provider);
571 service_provider = InterlockedExchangePointer((void**)&This->service_provider, service_provider);
573 IServiceProvider_Release(service_provider);
576 IInternetBindInfo_AddRef(bind_info);
577 bind_info = InterlockedExchangePointer((void**)&This->bind_info, bind_info);
579 IInternetBindInfo_Release(bind_info);
584 static const IInternetProtocolExVtbl BindProtocolVtbl = {
585 BindProtocol_QueryInterface,
587 BindProtocol_Release,
589 BindProtocol_Continue,
591 BindProtocol_Terminate,
592 BindProtocol_Suspend,
596 BindProtocol_LockRequest,
597 BindProtocol_UnlockRequest,
601 static inline BindProtocol *impl_from_IInternetProtocol(IInternetProtocol *iface)
603 return CONTAINING_RECORD(iface, BindProtocol, default_protocol_handler.IInternetProtocol_iface);
606 static HRESULT WINAPI ProtocolHandler_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
608 ERR("should not be called\n");
609 return E_NOINTERFACE;
612 static ULONG WINAPI ProtocolHandler_AddRef(IInternetProtocol *iface)
614 BindProtocol *This = impl_from_IInternetProtocol(iface);
615 return IInternetProtocolEx_AddRef(PROTOCOLEX(This));
618 static ULONG WINAPI ProtocolHandler_Release(IInternetProtocol *iface)
620 BindProtocol *This = impl_from_IInternetProtocol(iface);
621 return IInternetProtocolEx_Release(PROTOCOLEX(This));
624 static HRESULT WINAPI ProtocolHandler_Start(IInternetProtocol *iface, LPCWSTR szUrl,
625 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
626 DWORD grfPI, HANDLE_PTR dwReserved)
628 ERR("Should not be called\n");
632 static HRESULT WINAPI ProtocolHandler_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
634 BindProtocol *This = impl_from_IInternetProtocol(iface);
637 TRACE("(%p)->(%p)\n", This, pProtocolData);
639 hres = IInternetProtocol_Continue(This->protocol, pProtocolData);
641 heap_free(pProtocolData);
645 static HRESULT WINAPI ProtocolHandler_Abort(IInternetProtocol *iface, HRESULT hrReason,
648 BindProtocol *This = impl_from_IInternetProtocol(iface);
650 TRACE("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
652 if(This->protocol && !This->reported_result)
653 return IInternetProtocol_Abort(This->protocol, hrReason, dwOptions);
658 static HRESULT WINAPI ProtocolHandler_Terminate(IInternetProtocol *iface, DWORD dwOptions)
660 BindProtocol *This = impl_from_IInternetProtocol(iface);
662 TRACE("(%p)->(%08x)\n", This, dwOptions);
664 if(!This->reported_result)
667 IInternetProtocol_Terminate(This->protocol, 0);
669 if(This->filter_proxy) {
670 IInternetProtocol_Release(PROTOCOL(This->filter_proxy));
671 This->filter_proxy = NULL;
674 set_binding_sink(This, NULL, NULL);
676 if(This->bind_info) {
677 IInternetBindInfo_Release(This->bind_info);
678 This->bind_info = NULL;
684 static HRESULT WINAPI ProtocolHandler_Suspend(IInternetProtocol *iface)
686 BindProtocol *This = impl_from_IInternetProtocol(iface);
687 FIXME("(%p)\n", This);
691 static HRESULT WINAPI ProtocolHandler_Resume(IInternetProtocol *iface)
693 BindProtocol *This = impl_from_IInternetProtocol(iface);
694 FIXME("(%p)\n", This);
698 static HRESULT WINAPI ProtocolHandler_Read(IInternetProtocol *iface, void *pv,
699 ULONG cb, ULONG *pcbRead)
701 BindProtocol *This = impl_from_IInternetProtocol(iface);
705 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
708 read = min(cb, This->buf_size);
709 memcpy(pv, This->buf, read);
711 if(read == This->buf_size) {
712 heap_free(This->buf);
715 memmove(This->buf, This->buf+cb, This->buf_size-cb);
718 This->buf_size -= read;
724 hres = IInternetProtocol_Read(This->protocol, (BYTE*)pv+read, cb-read, &cread);
732 static HRESULT WINAPI ProtocolHandler_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
733 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
735 BindProtocol *This = impl_from_IInternetProtocol(iface);
736 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
740 static HRESULT WINAPI ProtocolHandler_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
742 BindProtocol *This = impl_from_IInternetProtocol(iface);
744 TRACE("(%p)->(%08x)\n", This, dwOptions);
746 return IInternetProtocol_LockRequest(This->protocol, dwOptions);
749 static HRESULT WINAPI ProtocolHandler_UnlockRequest(IInternetProtocol *iface)
751 BindProtocol *This = impl_from_IInternetProtocol(iface);
753 TRACE("(%p)\n", This);
755 return IInternetProtocol_UnlockRequest(This->protocol);
758 static const IInternetProtocolVtbl InternetProtocolHandlerVtbl = {
759 ProtocolHandler_QueryInterface,
760 ProtocolHandler_AddRef,
761 ProtocolHandler_Release,
762 ProtocolHandler_Start,
763 ProtocolHandler_Continue,
764 ProtocolHandler_Abort,
765 ProtocolHandler_Terminate,
766 ProtocolHandler_Suspend,
767 ProtocolHandler_Resume,
768 ProtocolHandler_Read,
769 ProtocolHandler_Seek,
770 ProtocolHandler_LockRequest,
771 ProtocolHandler_UnlockRequest
774 #define BINDINFO_THIS(iface) DEFINE_THIS(BindProtocol, InternetBindInfo, iface)
776 static HRESULT WINAPI BindInfo_QueryInterface(IInternetBindInfo *iface,
777 REFIID riid, void **ppv)
779 BindProtocol *This = BINDINFO_THIS(iface);
780 return IInternetProtocolEx_QueryInterface(PROTOCOLEX(This), riid, ppv);
783 static ULONG WINAPI BindInfo_AddRef(IInternetBindInfo *iface)
785 BindProtocol *This = BINDINFO_THIS(iface);
786 return IInternetProtocolEx_AddRef(PROTOCOLEX(This));
789 static ULONG WINAPI BindInfo_Release(IInternetBindInfo *iface)
791 BindProtocol *This = BINDINFO_THIS(iface);
792 return IInternetProtocolEx_Release(PROTOCOLEX(This));
795 static HRESULT WINAPI BindInfo_GetBindInfo(IInternetBindInfo *iface,
796 DWORD *grfBINDF, BINDINFO *pbindinfo)
798 BindProtocol *This = BINDINFO_THIS(iface);
801 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
803 hres = IInternetBindInfo_GetBindInfo(This->bind_info, grfBINDF, pbindinfo);
805 WARN("GetBindInfo failed: %08x\n", hres);
809 *grfBINDF |= BINDF_FROMURLMON;
813 static HRESULT WINAPI BindInfo_GetBindString(IInternetBindInfo *iface,
814 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
816 BindProtocol *This = BINDINFO_THIS(iface);
818 TRACE("(%p)->(%d %p %d %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
820 return IInternetBindInfo_GetBindString(This->bind_info, ulStringType, ppwzStr, cEl, pcElFetched);
825 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
826 BindInfo_QueryInterface,
829 BindInfo_GetBindInfo,
830 BindInfo_GetBindString
833 #define PRIORITY_THIS(iface) DEFINE_THIS(BindProtocol, InternetPriority, iface)
835 static HRESULT WINAPI InternetPriority_QueryInterface(IInternetPriority *iface,
836 REFIID riid, void **ppv)
838 BindProtocol *This = PRIORITY_THIS(iface);
839 return IInternetProtocolEx_QueryInterface(PROTOCOLEX(This), riid, ppv);
842 static ULONG WINAPI InternetPriority_AddRef(IInternetPriority *iface)
844 BindProtocol *This = PRIORITY_THIS(iface);
845 return IInternetProtocolEx_AddRef(PROTOCOLEX(This));
848 static ULONG WINAPI InternetPriority_Release(IInternetPriority *iface)
850 BindProtocol *This = PRIORITY_THIS(iface);
851 return IInternetProtocolEx_Release(PROTOCOLEX(This));
854 static HRESULT WINAPI InternetPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
856 BindProtocol *This = PRIORITY_THIS(iface);
858 TRACE("(%p)->(%d)\n", This, nPriority);
860 This->priority = nPriority;
864 static HRESULT WINAPI InternetPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
866 BindProtocol *This = PRIORITY_THIS(iface);
868 TRACE("(%p)->(%p)\n", This, pnPriority);
870 *pnPriority = This->priority;
876 static const IInternetPriorityVtbl InternetPriorityVtbl = {
877 InternetPriority_QueryInterface,
878 InternetPriority_AddRef,
879 InternetPriority_Release,
880 InternetPriority_SetPriority,
881 InternetPriority_GetPriority
885 #define PROTSINK_THIS(iface) DEFINE_THIS(BindProtocol, IInternetProtocolSink, iface)
887 static HRESULT WINAPI BPInternetProtocolSink_QueryInterface(IInternetProtocolSink *iface,
888 REFIID riid, void **ppv)
890 BindProtocol *This = PROTSINK_THIS(iface);
891 return IInternetProtocolEx_QueryInterface(PROTOCOLEX(This), riid, ppv);
894 static ULONG WINAPI BPInternetProtocolSink_AddRef(IInternetProtocolSink *iface)
896 BindProtocol *This = PROTSINK_THIS(iface);
897 return IInternetProtocolEx_AddRef(PROTOCOLEX(This));
900 static ULONG WINAPI BPInternetProtocolSink_Release(IInternetProtocolSink *iface)
902 BindProtocol *This = PROTSINK_THIS(iface);
903 return IInternetProtocolEx_Release(PROTOCOLEX(This));
907 task_header_t header;
911 static void switch_proc(BindProtocol *bind, task_header_t *t)
913 switch_task_t *task = (switch_task_t*)t;
915 IInternetProtocol_Continue(bind->protocol_handler, task->data);
920 static HRESULT WINAPI BPInternetProtocolSink_Switch(IInternetProtocolSink *iface,
921 PROTOCOLDATA *pProtocolData)
923 BindProtocol *This = PROTSINK_THIS(iface);
926 TRACE("(%p)->(%p)\n", This, pProtocolData);
928 TRACE("flags %x state %x data %p cb %u\n", pProtocolData->grfFlags, pProtocolData->dwState,
929 pProtocolData->pData, pProtocolData->cbData);
931 data = heap_alloc(sizeof(PROTOCOLDATA));
933 return E_OUTOFMEMORY;
934 memcpy(data, pProtocolData, sizeof(PROTOCOLDATA));
936 if(!do_direct_notif(This)) {
939 task = heap_alloc(sizeof(switch_task_t));
941 return E_OUTOFMEMORY;
945 push_task(This, &task->header, switch_proc);
949 if(!This->protocol_sink) {
950 IInternetProtocol_Continue(This->protocol_handler, data);
954 return IInternetProtocolSink_Switch(This->protocol_sink, data);
957 static void report_progress(BindProtocol *This, ULONG status_code, LPCWSTR status_text)
959 switch(status_code) {
960 case BINDSTATUS_FINDINGRESOURCE:
961 case BINDSTATUS_CONNECTING:
962 case BINDSTATUS_REDIRECTING:
963 case BINDSTATUS_BEGINDOWNLOADDATA:
964 case BINDSTATUS_SENDINGREQUEST:
965 case BINDSTATUS_CACHEFILENAMEAVAILABLE:
966 case BINDSTATUS_DIRECTBIND:
967 case BINDSTATUS_ACCEPTRANGES:
968 if(This->protocol_sink)
969 IInternetProtocolSink_ReportProgress(This->protocol_sink, status_code, status_text);
972 case BINDSTATUS_MIMETYPEAVAILABLE:
973 mime_available(This, status_text, FALSE);
976 case BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE:
977 mime_available(This, status_text, TRUE);
981 FIXME("unsupported ulStatusCode %u\n", status_code);
986 task_header_t header;
990 } on_progress_task_t;
992 static void on_progress_proc(BindProtocol *This, task_header_t *t)
994 on_progress_task_t *task = (on_progress_task_t*)t;
996 report_progress(This, task->status_code, task->status_text);
998 heap_free(task->status_text);
1002 static HRESULT WINAPI BPInternetProtocolSink_ReportProgress(IInternetProtocolSink *iface,
1003 ULONG ulStatusCode, LPCWSTR szStatusText)
1005 BindProtocol *This = PROTSINK_THIS(iface);
1007 TRACE("(%p)->(%u %s)\n", This, ulStatusCode, debugstr_w(szStatusText));
1009 if(do_direct_notif(This)) {
1010 report_progress(This, ulStatusCode, szStatusText);
1012 on_progress_task_t *task;
1014 task = heap_alloc(sizeof(on_progress_task_t));
1016 task->status_code = ulStatusCode;
1017 task->status_text = heap_strdupW(szStatusText);
1019 push_task(This, &task->header, on_progress_proc);
1025 static HRESULT report_data(BindProtocol *This, DWORD bscf, ULONG progress, ULONG progress_max)
1027 if(!This->protocol_sink)
1030 if((This->pi & PI_MIMEVERIFICATION) && !This->reported_mime) {
1031 BYTE buf[BUFFER_SIZE];
1038 hres = IInternetProtocol_Read(This->protocol, buf,
1039 sizeof(buf)-This->buf_size, &read);
1040 if(FAILED(hres) && hres != E_PENDING)
1044 This->buf = heap_alloc(BUFFER_SIZE);
1046 return E_OUTOFMEMORY;
1047 }else if(read + This->buf_size > BUFFER_SIZE) {
1050 tmp = heap_realloc(This->buf, read+This->buf_size);
1052 return E_OUTOFMEMORY;
1056 memcpy(This->buf+This->buf_size, buf, read);
1057 This->buf_size += read;
1058 }while(This->buf_size < MIME_TEST_SIZE && hres == S_OK);
1060 if(This->buf_size < MIME_TEST_SIZE && hres != S_FALSE)
1063 bscf = BSCF_FIRSTDATANOTIFICATION;
1065 bscf |= BSCF_LASTDATANOTIFICATION|BSCF_DATAFULLYAVAILABLE;
1067 if(!This->reported_mime) {
1070 hres = IUri_GetRawUri(This->uri, &raw_uri);
1074 hres = FindMimeFromData(NULL, raw_uri, This->buf, min(This->buf_size, MIME_TEST_SIZE),
1075 This->mime, 0, &mime, 0);
1076 SysFreeString(raw_uri);
1080 mime_available(This, mime, TRUE);
1081 CoTaskMemFree(mime);
1085 if(!This->protocol_sink)
1088 return IInternetProtocolSink_ReportData(This->protocol_sink, bscf, progress, progress_max);
1092 task_header_t header;
1096 } report_data_task_t;
1098 static void report_data_proc(BindProtocol *This, task_header_t *t)
1100 report_data_task_t *task = (report_data_task_t*)t;
1102 report_data(This, task->bscf, task->progress, task->progress_max);
1106 static HRESULT WINAPI BPInternetProtocolSink_ReportData(IInternetProtocolSink *iface,
1107 DWORD grfBSCF, ULONG ulProgress, ULONG ulProgressMax)
1109 BindProtocol *This = PROTSINK_THIS(iface);
1111 TRACE("(%p)->(%d %u %u)\n", This, grfBSCF, ulProgress, ulProgressMax);
1113 if(!This->protocol_sink)
1116 if(!do_direct_notif(This)) {
1117 report_data_task_t *task;
1119 task = heap_alloc(sizeof(report_data_task_t));
1121 return E_OUTOFMEMORY;
1123 task->bscf = grfBSCF;
1124 task->progress = ulProgress;
1125 task->progress_max = ulProgressMax;
1127 push_task(This, &task->header, report_data_proc);
1131 return report_data(This, grfBSCF, ulProgress, ulProgressMax);
1135 task_header_t header;
1140 } report_result_task_t;
1142 static void report_result_proc(BindProtocol *This, task_header_t *t)
1144 report_result_task_t *task = (report_result_task_t*)t;
1146 if(This->protocol_sink)
1147 IInternetProtocolSink_ReportResult(This->protocol_sink, task->hres, task->err, task->str);
1149 heap_free(task->str);
1153 static HRESULT WINAPI BPInternetProtocolSink_ReportResult(IInternetProtocolSink *iface,
1154 HRESULT hrResult, DWORD dwError, LPCWSTR szResult)
1156 BindProtocol *This = PROTSINK_THIS(iface);
1158 TRACE("(%p)->(%08x %d %s)\n", This, hrResult, dwError, debugstr_w(szResult));
1160 if(!This->protocol_sink)
1163 This->reported_result = TRUE;
1165 if(!do_direct_notif(This)) {
1166 report_result_task_t *task;
1168 task = heap_alloc(sizeof(report_result_task_t));
1170 return E_OUTOFMEMORY;
1172 task->hres = hrResult;
1173 task->err = dwError;
1174 task->str = heap_strdupW(szResult);
1176 push_task(This, &task->header, report_result_proc);
1180 return IInternetProtocolSink_ReportResult(This->protocol_sink, hrResult, dwError, szResult);
1183 #undef PROTSINK_THIS
1185 static const IInternetProtocolSinkVtbl InternetProtocolSinkVtbl = {
1186 BPInternetProtocolSink_QueryInterface,
1187 BPInternetProtocolSink_AddRef,
1188 BPInternetProtocolSink_Release,
1189 BPInternetProtocolSink_Switch,
1190 BPInternetProtocolSink_ReportProgress,
1191 BPInternetProtocolSink_ReportData,
1192 BPInternetProtocolSink_ReportResult
1195 #define INETINFO_THIS(iface) DEFINE_THIS(BindProtocol, IWinInetHttpInfo, iface)
1197 static HRESULT WINAPI WinInetHttpInfo_QueryInterface(IWinInetHttpInfo *iface, REFIID riid, void **ppv)
1199 BindProtocol *This = INETINFO_THIS(iface);
1200 return IInternetProtocolEx_QueryInterface(PROTOCOLEX(This), riid, ppv);
1203 static ULONG WINAPI WinInetHttpInfo_AddRef(IWinInetHttpInfo *iface)
1205 BindProtocol *This = INETINFO_THIS(iface);
1206 return IInternetProtocolEx_AddRef(PROTOCOLEX(This));
1209 static ULONG WINAPI WinInetHttpInfo_Release(IWinInetHttpInfo *iface)
1211 BindProtocol *This = INETINFO_THIS(iface);
1212 return IInternetProtocolEx_Release(PROTOCOLEX(This));
1215 static HRESULT WINAPI WinInetHttpInfo_QueryOption(IWinInetHttpInfo *iface, DWORD dwOption,
1216 void *pBuffer, DWORD *pcbBuffer)
1218 BindProtocol *This = INETINFO_THIS(iface);
1219 FIXME("(%p)->(%x %p %p)\n", This, dwOption, pBuffer, pcbBuffer);
1223 static HRESULT WINAPI WinInetHttpInfo_QueryInfo(IWinInetHttpInfo *iface, DWORD dwOption,
1224 void *pBuffer, DWORD *pcbBuffer, DWORD *pdwFlags, DWORD *pdwReserved)
1226 BindProtocol *This = INETINFO_THIS(iface);
1227 FIXME("(%p)->(%x %p %p %p %p)\n", This, dwOption, pBuffer, pcbBuffer, pdwFlags, pdwReserved);
1231 #undef INETINFO_THIS
1233 static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl = {
1234 WinInetHttpInfo_QueryInterface,
1235 WinInetHttpInfo_AddRef,
1236 WinInetHttpInfo_Release,
1237 WinInetHttpInfo_QueryOption,
1238 WinInetHttpInfo_QueryInfo
1241 #define SERVPROV_THIS(iface) DEFINE_THIS(BindProtocol, ServiceProvider, iface)
1243 static HRESULT WINAPI BPServiceProvider_QueryInterface(IServiceProvider *iface,
1244 REFIID riid, void **ppv)
1246 BindProtocol *This = SERVPROV_THIS(iface);
1247 return IInternetProtocolEx_QueryInterface(PROTOCOLEX(This), riid, ppv);
1250 static ULONG WINAPI BPServiceProvider_AddRef(IServiceProvider *iface)
1252 BindProtocol *This = SERVPROV_THIS(iface);
1253 return IInternetProtocolEx_AddRef(PROTOCOLEX(This));
1256 static ULONG WINAPI BPServiceProvider_Release(IServiceProvider *iface)
1258 BindProtocol *This = SERVPROV_THIS(iface);
1259 return IInternetProtocolEx_Release(PROTOCOLEX(This));
1262 static HRESULT WINAPI BPServiceProvider_QueryService(IServiceProvider *iface,
1263 REFGUID guidService, REFIID riid, void **ppv)
1265 BindProtocol *This = SERVPROV_THIS(iface);
1267 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
1269 if(!This->service_provider)
1270 return E_NOINTERFACE;
1272 return IServiceProvider_QueryService(This->service_provider, guidService, riid, ppv);
1275 #undef SERVPROV_THIS
1277 static const IServiceProviderVtbl ServiceProviderVtbl = {
1278 BPServiceProvider_QueryInterface,
1279 BPServiceProvider_AddRef,
1280 BPServiceProvider_Release,
1281 BPServiceProvider_QueryService
1284 HRESULT create_binding_protocol(BOOL from_urlmon, BindProtocol **protocol)
1286 BindProtocol *ret = heap_alloc_zero(sizeof(BindProtocol));
1288 ret->lpIInternetProtocolExVtbl = &BindProtocolVtbl;
1289 ret->lpInternetBindInfoVtbl = &InternetBindInfoVtbl;
1290 ret->lpInternetPriorityVtbl = &InternetPriorityVtbl;
1291 ret->lpServiceProviderVtbl = &ServiceProviderVtbl;
1292 ret->lpIInternetProtocolSinkVtbl = &InternetProtocolSinkVtbl;
1293 ret->lpIWinInetHttpInfoVtbl = &WinInetHttpInfoVtbl;
1295 ret->default_protocol_handler.IInternetProtocol_iface.lpVtbl = &InternetProtocolHandlerVtbl;
1298 ret->from_urlmon = from_urlmon;
1299 ret->apartment_thread = GetCurrentThreadId();
1300 ret->notif_hwnd = get_notif_hwnd();
1301 ret->protocol_handler = &ret->default_protocol_handler.IInternetProtocol_iface;
1302 InitializeCriticalSection(&ret->section);
1304 URLMON_LockModule();