makefiles: Don't use standard libs for programs that specify -nodefaultlibs.
[wine/zf.git] / dlls / urlmon / bindprot.c
blobe9c97e8a9dba4e97e19c1e045dcdabe843eb5cc6
1 /*
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 {
27 task_proc_t proc;
28 task_header_t *next;
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 void process_tasks(BindProtocol *This)
39 task_header_t *task;
41 while(1) {
42 EnterCriticalSection(&This->section);
44 task = This->task_queue_head;
45 if(task) {
46 This->task_queue_head = task->next;
47 if(!This->task_queue_head)
48 This->task_queue_tail = NULL;
51 LeaveCriticalSection(&This->section);
53 if(!task)
54 break;
56 This->continue_call++;
57 task->proc(This, task);
58 This->continue_call--;
62 static LRESULT WINAPI notif_wnd_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
64 switch(msg) {
65 case WM_MK_CONTINUE: {
66 BindProtocol *This = (BindProtocol*)lParam;
68 process_tasks(This);
70 IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
71 return 0;
73 case WM_MK_RELEASE: {
74 tls_data_t *data = get_tls_data();
76 if(!--data->notif_hwnd_cnt) {
77 DestroyWindow(hwnd);
78 data->notif_hwnd = NULL;
83 return DefWindowProcW(hwnd, msg, wParam, lParam);
86 static const WCHAR wszURLMonikerNotificationWindow[] =
87 {'U','R','L',' ','M','o','n','i','k','e','r',' ',
88 'N','o','t','i','f','i','c','a','t','i','o','n',' ','W','i','n','d','o','w',0};
90 static ATOM notif_wnd_class;
92 static BOOL WINAPI register_notif_wnd_class(INIT_ONCE *once, void *param, void **context)
94 static WNDCLASSEXW wndclass = {
95 sizeof(wndclass), 0, notif_wnd_proc, 0, 0,
96 NULL, NULL, NULL, NULL, NULL,
97 wszURLMonikerNotificationWindow, NULL
100 wndclass.hInstance = hProxyDll;
101 notif_wnd_class = RegisterClassExW(&wndclass);
102 return TRUE;
105 void unregister_notif_wnd_class(void)
107 if(notif_wnd_class)
108 UnregisterClassW(MAKEINTRESOURCEW(notif_wnd_class), hProxyDll);
111 HWND get_notif_hwnd(void)
113 tls_data_t *tls_data;
115 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
117 tls_data = get_tls_data();
118 if(!tls_data)
119 return NULL;
121 if(tls_data->notif_hwnd_cnt) {
122 tls_data->notif_hwnd_cnt++;
123 return tls_data->notif_hwnd;
126 InitOnceExecuteOnce(&init_once, register_notif_wnd_class, NULL, NULL);
127 if(!notif_wnd_class)
128 return NULL;
130 tls_data->notif_hwnd = CreateWindowExW(0, MAKEINTRESOURCEW(notif_wnd_class),
131 wszURLMonikerNotificationWindow, 0, 0, 0, 0, 0, HWND_MESSAGE,
132 NULL, hProxyDll, NULL);
133 if(tls_data->notif_hwnd)
134 tls_data->notif_hwnd_cnt++;
136 TRACE("hwnd = %p\n", tls_data->notif_hwnd);
138 return tls_data->notif_hwnd;
141 void release_notif_hwnd(HWND hwnd)
143 tls_data_t *data = get_tls_data();
145 if(!data)
146 return;
148 if(data->notif_hwnd != hwnd) {
149 PostMessageW(data->notif_hwnd, WM_MK_RELEASE, 0, 0);
150 return;
153 if(!--data->notif_hwnd_cnt) {
154 DestroyWindow(data->notif_hwnd);
155 data->notif_hwnd = NULL;
159 static void push_task(BindProtocol *This, task_header_t *task, task_proc_t proc)
161 BOOL do_post = FALSE;
163 task->proc = proc;
164 task->next = NULL;
166 EnterCriticalSection(&This->section);
168 if(This->task_queue_tail) {
169 This->task_queue_tail->next = task;
170 This->task_queue_tail = task;
171 }else {
172 This->task_queue_tail = This->task_queue_head = task;
173 do_post = !This->continue_call;
176 LeaveCriticalSection(&This->section);
178 if(do_post) {
179 IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
180 PostMessageW(This->notif_hwnd, WM_MK_CONTINUE, 0, (LPARAM)This);
184 static inline BOOL is_apartment_thread(BindProtocol *This)
186 return This->apartment_thread == GetCurrentThreadId();
189 static inline BOOL do_direct_notif(BindProtocol *This)
191 return !(This->pi & PI_APARTMENTTHREADED) || (is_apartment_thread(This) && !This->continue_call);
194 static HRESULT handle_mime_filter(BindProtocol *This, IInternetProtocol *mime_filter)
196 PROTOCOLFILTERDATA filter_data = { sizeof(PROTOCOLFILTERDATA), NULL, NULL, NULL, 0 };
197 HRESULT hres;
199 hres = IInternetProtocol_QueryInterface(mime_filter, &IID_IInternetProtocolSink, (void**)&This->protocol_sink_handler);
200 if(FAILED(hres)) {
201 This->protocol_sink_handler = &This->default_protocol_handler.IInternetProtocolSink_iface;
202 return hres;
205 IInternetProtocol_AddRef(mime_filter);
206 This->protocol_handler = mime_filter;
208 filter_data.pProtocol = &This->default_protocol_handler.IInternetProtocol_iface;
209 hres = IInternetProtocol_Start(mime_filter, This->mime, &This->default_protocol_handler.IInternetProtocolSink_iface,
210 &This->IInternetBindInfo_iface, PI_FILTER_MODE|PI_FORCE_ASYNC,
211 (HANDLE_PTR)&filter_data);
212 if(FAILED(hres)) {
213 IInternetProtocolSink_Release(This->protocol_sink_handler);
214 IInternetProtocol_Release(This->protocol_handler);
215 This->protocol_sink_handler = &This->default_protocol_handler.IInternetProtocolSink_iface;
216 This->protocol_handler = &This->default_protocol_handler.IInternetProtocol_iface;
217 return hres;
220 /* NOTE: IE9 calls it on the new protocol_sink. It doesn't make sense so it seems to be a bug there. */
221 IInternetProtocolSink_ReportProgress(This->protocol_sink, BINDSTATUS_LOADINGMIMEHANDLER, NULL);
223 return S_OK;
226 static void mime_available(BindProtocol *This, LPCWSTR mime, BOOL verified)
228 IInternetProtocol *mime_filter;
229 HRESULT hres;
231 heap_free(This->mime);
232 This->mime = heap_strdupW(mime);
234 if(This->protocol_handler==&This->default_protocol_handler.IInternetProtocol_iface
235 && (mime_filter = get_mime_filter(mime))) {
236 TRACE("Got mime filter for %s\n", debugstr_w(mime));
238 hres = handle_mime_filter(This, mime_filter);
239 IInternetProtocol_Release(mime_filter);
240 if(FAILED(hres))
241 FIXME("MIME filter failed: %08x\n", hres);
244 if(This->reported_mime || verified || !(This->pi & PI_MIMEVERIFICATION)) {
245 This->reported_mime = TRUE;
246 IInternetProtocolSink_ReportProgress(This->protocol_sink, BINDSTATUS_MIMETYPEAVAILABLE, mime);
250 static inline BindProtocol *impl_from_IInternetProtocolEx(IInternetProtocolEx *iface)
252 return CONTAINING_RECORD(iface, BindProtocol, IInternetProtocolEx_iface);
255 static HRESULT WINAPI BindProtocol_QueryInterface(IInternetProtocolEx *iface, REFIID riid, void **ppv)
257 BindProtocol *This = impl_from_IInternetProtocolEx(iface);
259 *ppv = NULL;
260 if(IsEqualGUID(&IID_IUnknown, riid)) {
261 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
262 *ppv = &This->IInternetProtocolEx_iface;
263 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
264 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
265 *ppv = &This->IInternetProtocolEx_iface;
266 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
267 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
268 *ppv = &This->IInternetProtocolEx_iface;
269 }else if(IsEqualGUID(&IID_IInternetProtocolEx, riid)) {
270 TRACE("(%p)->(IID_IInternetProtocolEx %p)\n", This, ppv);
271 *ppv = &This->IInternetProtocolEx_iface;
272 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
273 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
274 *ppv = &This->IInternetBindInfo_iface;
275 }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
276 TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
277 *ppv = &This->IInternetPriority_iface;
278 }else if(IsEqualGUID(&IID_IAuthenticate, riid)) {
279 FIXME("(%p)->(IID_IAuthenticate %p)\n", This, ppv);
280 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
281 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
282 *ppv = &This->IServiceProvider_iface;
283 }else if(IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
284 TRACE("(%p)->(IID_IInternetProtocolSink %p)\n", This, ppv);
285 *ppv = &This->IInternetProtocolSink_iface;
286 }else if(This->protocol_unk) {
287 HRESULT hres;
288 hres = IUnknown_QueryInterface(This->protocol_unk, riid, ppv);
289 TRACE("(%p) aggregated handler returned %08x for %s\n", This, hres, debugstr_guid(riid));
290 return hres;
291 }else {
292 WARN("not supported interface %s\n", debugstr_guid(riid));
295 if(!*ppv)
296 return E_NOINTERFACE;
298 IUnknown_AddRef((IUnknown*)*ppv);
299 return S_OK;
302 static ULONG WINAPI BindProtocol_AddRef(IInternetProtocolEx *iface)
304 BindProtocol *This = impl_from_IInternetProtocolEx(iface);
305 LONG ref = InterlockedIncrement(&This->ref);
306 TRACE("(%p) ref=%d\n", This, ref);
307 return ref;
310 static void release_protocol_handler(BindProtocol *This)
312 if(This->protocol) {
313 IInternetProtocol_Release(This->protocol);
314 This->protocol = NULL;
316 if(This->protocol_handler && This->protocol_handler != &This->default_protocol_handler.IInternetProtocol_iface) {
317 IInternetProtocol_Release(This->protocol_handler);
318 This->protocol_handler = &This->default_protocol_handler.IInternetProtocol_iface;
320 if(This->protocol_sink_handler &&
321 This->protocol_sink_handler != &This->default_protocol_handler.IInternetProtocolSink_iface) {
322 IInternetProtocolSink_Release(This->protocol_sink_handler);
323 This->protocol_sink_handler = &This->default_protocol_handler.IInternetProtocolSink_iface;
325 if(This->protocol_unk) {
326 IUnknown_Release(This->protocol_unk);
327 This->protocol_unk = NULL;
331 static ULONG WINAPI BindProtocol_Release(IInternetProtocolEx *iface)
333 BindProtocol *This = impl_from_IInternetProtocolEx(iface);
334 LONG ref = InterlockedDecrement(&This->ref);
336 TRACE("(%p) ref=%d\n", This, ref);
338 if(!ref) {
339 release_protocol_handler(This);
340 if(This->redirect_callback)
341 IBindCallbackRedirect_Release(This->redirect_callback);
342 if(This->bind_info)
343 IInternetBindInfo_Release(This->bind_info);
344 if(This->uri)
345 IUri_Release(This->uri);
346 SysFreeString(This->display_uri);
348 set_binding_sink(This, NULL, NULL);
350 if(This->notif_hwnd)
351 release_notif_hwnd(This->notif_hwnd);
352 This->section.DebugInfo->Spare[0] = 0;
353 DeleteCriticalSection(&This->section);
355 heap_free(This->mime);
356 heap_free(This);
358 URLMON_UnlockModule();
361 return ref;
364 static HRESULT WINAPI BindProtocol_Start(IInternetProtocolEx *iface, LPCWSTR szUrl,
365 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
366 DWORD grfPI, HANDLE_PTR dwReserved)
368 BindProtocol *This = impl_from_IInternetProtocolEx(iface);
369 IUri *uri;
370 HRESULT hres;
372 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
373 pOIBindInfo, grfPI, dwReserved);
375 hres = CreateUri(szUrl, Uri_CREATE_FILE_USE_DOS_PATH, 0, &uri);
376 if(FAILED(hres))
377 return hres;
379 hres = IInternetProtocolEx_StartEx(&This->IInternetProtocolEx_iface, uri, pOIProtSink,
380 pOIBindInfo, grfPI, (HANDLE*)dwReserved);
382 IUri_Release(uri);
383 return hres;
386 static HRESULT WINAPI BindProtocol_Continue(IInternetProtocolEx *iface, PROTOCOLDATA *pProtocolData)
388 BindProtocol *This = impl_from_IInternetProtocolEx(iface);
390 TRACE("(%p)->(%p)\n", This, pProtocolData);
392 return IInternetProtocol_Continue(This->protocol_handler, pProtocolData);
395 static HRESULT WINAPI BindProtocol_Abort(IInternetProtocolEx *iface, HRESULT hrReason,
396 DWORD dwOptions)
398 BindProtocol *This = impl_from_IInternetProtocolEx(iface);
400 TRACE("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
402 return IInternetProtocol_Abort(This->protocol_handler, hrReason, dwOptions);
405 static HRESULT WINAPI BindProtocol_Terminate(IInternetProtocolEx *iface, DWORD dwOptions)
407 BindProtocol *This = impl_from_IInternetProtocolEx(iface);
409 TRACE("(%p)->(%08x)\n", This, dwOptions);
411 return IInternetProtocol_Terminate(This->protocol_handler, dwOptions);
414 static HRESULT WINAPI BindProtocol_Suspend(IInternetProtocolEx *iface)
416 BindProtocol *This = impl_from_IInternetProtocolEx(iface);
417 FIXME("(%p)\n", This);
418 return E_NOTIMPL;
421 static HRESULT WINAPI BindProtocol_Resume(IInternetProtocolEx *iface)
423 BindProtocol *This = impl_from_IInternetProtocolEx(iface);
424 FIXME("(%p)\n", This);
425 return E_NOTIMPL;
428 static HRESULT WINAPI BindProtocol_Read(IInternetProtocolEx *iface, void *pv,
429 ULONG cb, ULONG *pcbRead)
431 BindProtocol *This = impl_from_IInternetProtocolEx(iface);
433 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
435 if(pcbRead)
436 *pcbRead = 0;
437 return IInternetProtocol_Read(This->protocol_handler, pv, cb, pcbRead);
440 static HRESULT WINAPI BindProtocol_Seek(IInternetProtocolEx *iface, LARGE_INTEGER dlibMove,
441 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
443 BindProtocol *This = impl_from_IInternetProtocolEx(iface);
444 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
445 return E_NOTIMPL;
448 static HRESULT WINAPI BindProtocol_LockRequest(IInternetProtocolEx *iface, DWORD dwOptions)
450 BindProtocol *This = impl_from_IInternetProtocolEx(iface);
452 TRACE("(%p)->(%08x)\n", This, dwOptions);
454 return IInternetProtocol_LockRequest(This->protocol_handler, dwOptions);
457 static HRESULT WINAPI BindProtocol_UnlockRequest(IInternetProtocolEx *iface)
459 BindProtocol *This = impl_from_IInternetProtocolEx(iface);
461 TRACE("(%p)\n", This);
463 return IInternetProtocol_UnlockRequest(This->protocol_handler);
466 static HRESULT WINAPI BindProtocol_StartEx(IInternetProtocolEx *iface, IUri *pUri,
467 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
468 DWORD grfPI, HANDLE *dwReserved)
470 BindProtocol *This = impl_from_IInternetProtocolEx(iface);
471 IInternetProtocol *protocol = NULL;
472 IInternetProtocolEx *protocolex;
473 IInternetPriority *priority;
474 IServiceProvider *service_provider;
475 CLSID clsid = IID_NULL;
476 IUnknown *protocol_unk = NULL;
477 LPOLESTR clsid_str;
478 HRESULT hres;
480 TRACE("(%p)->(%p %p %p %08x %p)\n", This, pUri, pOIProtSink, pOIBindInfo, grfPI, dwReserved);
482 if(!pUri || !pOIProtSink || !pOIBindInfo)
483 return E_INVALIDARG;
485 This->pi = grfPI;
487 if(This->uri) {
488 SysFreeString(This->display_uri);
489 IUri_Release(This->uri);
491 IUri_AddRef(pUri);
492 This->uri = pUri;
494 hres = IInternetProtocolSink_QueryInterface(pOIProtSink, &IID_IServiceProvider,
495 (void**)&service_provider);
496 if(SUCCEEDED(hres)) {
497 /* FIXME: What's protocol CLSID here? */
498 IServiceProvider_QueryService(service_provider, &IID_IInternetProtocol,
499 &IID_IInternetProtocol, (void**)&protocol);
500 IServiceProvider_Release(service_provider);
503 if(!protocol) {
504 IClassFactory *cf;
506 hres = get_protocol_handler(pUri, &clsid, &cf);
507 if(FAILED(hres))
508 return hres;
510 hres = IClassFactory_CreateInstance(cf, (IUnknown*)&This->IInternetBindInfo_iface,
511 &IID_IUnknown, (void**)&protocol_unk);
512 if(SUCCEEDED(hres)) {
513 hres = IUnknown_QueryInterface(protocol_unk, &IID_IInternetProtocol, (void**)&protocol);
514 if(SUCCEEDED(hres))
515 This->protocol_unk = protocol_unk;
516 else
517 IUnknown_Release(protocol_unk);
519 else if(hres == CLASS_E_NOAGGREGATION)
520 hres = IClassFactory_CreateInstance(cf, NULL, &IID_IInternetProtocol, (void**)&protocol);
522 IClassFactory_Release(cf);
523 if(FAILED(hres))
524 return hres;
527 StringFromCLSID(&clsid, &clsid_str);
528 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_PROTOCOLCLASSID, clsid_str);
529 CoTaskMemFree(clsid_str);
531 This->protocol_unk = protocol_unk;
532 This->protocol = protocol;
534 if(!protocol_unk)
535 protocol_unk = (IUnknown*)protocol;
537 set_binding_sink(This, pOIProtSink, pOIBindInfo);
539 hres = IUnknown_QueryInterface(protocol_unk, &IID_IInternetPriority, (void**)&priority);
540 if(SUCCEEDED(hres)) {
541 IInternetPriority_SetPriority(priority, This->priority);
542 IInternetPriority_Release(priority);
545 hres = IUnknown_QueryInterface(protocol_unk, &IID_IInternetProtocolEx, (void**)&protocolex);
546 if(SUCCEEDED(hres)) {
547 hres = IInternetProtocolEx_StartEx(protocolex, pUri, &This->IInternetProtocolSink_iface,
548 &This->IInternetBindInfo_iface, 0, NULL);
549 IInternetProtocolEx_Release(protocolex);
550 }else {
551 hres = IUri_GetDisplayUri(pUri, &This->display_uri);
552 if(FAILED(hres))
553 return hres;
555 hres = IInternetProtocol_Start(protocol, This->display_uri, &This->IInternetProtocolSink_iface,
556 &This->IInternetBindInfo_iface, 0, 0);
559 if(SUCCEEDED(hres))
560 process_tasks(This);
561 return hres;
564 void set_binding_sink(BindProtocol *This, IInternetProtocolSink *sink, IInternetBindInfo *bind_info)
566 IInternetProtocolSink *prev_sink;
567 IServiceProvider *service_provider = NULL;
569 if(sink)
570 IInternetProtocolSink_AddRef(sink);
571 prev_sink = InterlockedExchangePointer((void**)&This->protocol_sink, sink);
572 if(prev_sink)
573 IInternetProtocolSink_Release(prev_sink);
575 if(sink)
576 IInternetProtocolSink_QueryInterface(sink, &IID_IServiceProvider, (void**)&service_provider);
577 service_provider = InterlockedExchangePointer((void**)&This->service_provider, service_provider);
578 if(service_provider)
579 IServiceProvider_Release(service_provider);
581 if(bind_info)
582 IInternetBindInfo_AddRef(bind_info);
583 bind_info = InterlockedExchangePointer((void**)&This->bind_info, bind_info);
584 if(bind_info)
585 IInternetBindInfo_Release(bind_info);
588 static const IInternetProtocolExVtbl BindProtocolVtbl = {
589 BindProtocol_QueryInterface,
590 BindProtocol_AddRef,
591 BindProtocol_Release,
592 BindProtocol_Start,
593 BindProtocol_Continue,
594 BindProtocol_Abort,
595 BindProtocol_Terminate,
596 BindProtocol_Suspend,
597 BindProtocol_Resume,
598 BindProtocol_Read,
599 BindProtocol_Seek,
600 BindProtocol_LockRequest,
601 BindProtocol_UnlockRequest,
602 BindProtocol_StartEx
605 static inline BindProtocol *impl_from_IInternetProtocol(IInternetProtocol *iface)
607 return CONTAINING_RECORD(iface, BindProtocol, default_protocol_handler.IInternetProtocol_iface);
610 static HRESULT WINAPI ProtocolHandler_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
612 BindProtocol *This = impl_from_IInternetProtocol(iface);
614 *ppv = NULL;
615 if(IsEqualGUID(&IID_IUnknown, riid)) {
616 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
617 *ppv = &This->default_protocol_handler.IInternetProtocol_iface;
618 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
619 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
620 *ppv = &This->default_protocol_handler.IInternetProtocol_iface;
621 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
622 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
623 *ppv = &This->default_protocol_handler.IInternetProtocol_iface;
624 }else if(IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
625 TRACE("(%p)->(IID_IInternetProtocolSink %p)\n", This, ppv);
626 *ppv = &This->default_protocol_handler.IInternetProtocolSink_iface;
629 if(*ppv) {
630 IInternetProtocol_AddRef(iface);
631 return S_OK;
634 WARN("not supported interface %s\n", debugstr_guid(riid));
635 return E_NOINTERFACE;
638 static ULONG WINAPI ProtocolHandler_AddRef(IInternetProtocol *iface)
640 BindProtocol *This = impl_from_IInternetProtocol(iface);
641 return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
644 static ULONG WINAPI ProtocolHandler_Release(IInternetProtocol *iface)
646 BindProtocol *This = impl_from_IInternetProtocol(iface);
647 return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
650 static HRESULT WINAPI ProtocolHandler_Start(IInternetProtocol *iface, LPCWSTR szUrl,
651 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
652 DWORD grfPI, HANDLE_PTR dwReserved)
654 ERR("Should not be called\n");
655 return E_NOTIMPL;
658 static HRESULT WINAPI ProtocolHandler_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
660 BindProtocol *This = impl_from_IInternetProtocol(iface);
661 IInternetProtocol *protocol = NULL;
662 HRESULT hres;
664 TRACE("(%p)->(%p)\n", This, pProtocolData);
666 /* FIXME: This should not be needed. */
667 if(!This->protocol) {
668 if(!This->protocol_unk)
669 return E_FAIL;
670 hres = IUnknown_QueryInterface(This->protocol_unk, &IID_IInternetProtocol, (void**)&protocol);
671 if(FAILED(hres))
672 return E_FAIL;
675 hres = IInternetProtocol_Continue(protocol ? protocol : This->protocol, pProtocolData);
677 heap_free(pProtocolData);
678 if(protocol)
679 IInternetProtocol_Release(protocol);
680 return hres;
683 static HRESULT WINAPI ProtocolHandler_Abort(IInternetProtocol *iface, HRESULT hrReason,
684 DWORD dwOptions)
686 BindProtocol *This = impl_from_IInternetProtocol(iface);
688 TRACE("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
690 if(This->protocol && !This->reported_result)
691 return IInternetProtocol_Abort(This->protocol, hrReason, dwOptions);
693 return S_OK;
696 static HRESULT WINAPI ProtocolHandler_Terminate(IInternetProtocol *iface, DWORD dwOptions)
698 BindProtocol *This = impl_from_IInternetProtocol(iface);
700 TRACE("(%p)->(%08x)\n", This, dwOptions);
702 if(!This->reported_result)
703 return E_FAIL;
705 /* This may get released in Terminate call. */
706 IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
708 if(This->protocol) {
709 IInternetProtocol_Terminate(This->protocol, 0);
711 if (This->protocol_unk) {
712 IInternetProtocol_Release(This->protocol);
713 This->protocol = NULL;
717 set_binding_sink(This, NULL, NULL);
719 if(This->bind_info) {
720 IInternetBindInfo_Release(This->bind_info);
721 This->bind_info = NULL;
724 if(This->redirect_callback) {
725 IBindCallbackRedirect_Release(This->redirect_callback);
726 This->redirect_callback = NULL;
729 IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
730 return S_OK;
733 static HRESULT WINAPI ProtocolHandler_Suspend(IInternetProtocol *iface)
735 BindProtocol *This = impl_from_IInternetProtocol(iface);
736 FIXME("(%p)\n", This);
737 return E_NOTIMPL;
740 static HRESULT WINAPI ProtocolHandler_Resume(IInternetProtocol *iface)
742 BindProtocol *This = impl_from_IInternetProtocol(iface);
743 FIXME("(%p)\n", This);
744 return E_NOTIMPL;
747 static HRESULT WINAPI ProtocolHandler_Read(IInternetProtocol *iface, void *pv,
748 ULONG cb, ULONG *pcbRead)
750 BindProtocol *This = impl_from_IInternetProtocol(iface);
751 ULONG read = 0;
752 HRESULT hres = S_OK;
754 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
756 if(This->buf_size) {
757 read = min(cb, This->buf_size);
758 memcpy(pv, This->buf, read);
760 if(read == This->buf_size) {
761 heap_free(This->buf);
762 This->buf = NULL;
763 }else {
764 memmove(This->buf, This->buf+cb, This->buf_size-cb);
767 This->buf_size -= read;
770 if(read < cb) {
771 IInternetProtocol *protocol;
772 ULONG cread = 0;
774 /* FIXME: We shouldn't need it, but out binding code currently depends on it. */
775 if(!This->protocol && This->protocol_unk) {
776 hres = IUnknown_QueryInterface(This->protocol_unk, &IID_IInternetProtocol,
777 (void**)&protocol);
778 if(FAILED(hres))
779 return E_ABORT;
780 }else {
781 protocol = This->protocol;
784 if(is_apartment_thread(This))
785 This->continue_call++;
786 hres = IInternetProtocol_Read(protocol, (BYTE*)pv+read, cb-read, &cread);
787 if(is_apartment_thread(This))
788 This->continue_call--;
789 read += cread;
791 if(!This->protocol)
792 IInternetProtocol_Release(protocol);
795 *pcbRead = read;
796 return hres;
799 static HRESULT WINAPI ProtocolHandler_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
800 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
802 BindProtocol *This = impl_from_IInternetProtocol(iface);
803 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
804 return E_NOTIMPL;
807 static HRESULT WINAPI ProtocolHandler_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
809 BindProtocol *This = impl_from_IInternetProtocol(iface);
811 TRACE("(%p)->(%08x)\n", This, dwOptions);
813 return IInternetProtocol_LockRequest(This->protocol, dwOptions);
816 static HRESULT WINAPI ProtocolHandler_UnlockRequest(IInternetProtocol *iface)
818 BindProtocol *This = impl_from_IInternetProtocol(iface);
820 TRACE("(%p)\n", This);
822 return IInternetProtocol_UnlockRequest(This->protocol);
825 static const IInternetProtocolVtbl InternetProtocolHandlerVtbl = {
826 ProtocolHandler_QueryInterface,
827 ProtocolHandler_AddRef,
828 ProtocolHandler_Release,
829 ProtocolHandler_Start,
830 ProtocolHandler_Continue,
831 ProtocolHandler_Abort,
832 ProtocolHandler_Terminate,
833 ProtocolHandler_Suspend,
834 ProtocolHandler_Resume,
835 ProtocolHandler_Read,
836 ProtocolHandler_Seek,
837 ProtocolHandler_LockRequest,
838 ProtocolHandler_UnlockRequest
841 static inline BindProtocol *impl_from_IInternetProtocolSinkHandler(IInternetProtocolSink *iface)
843 return CONTAINING_RECORD(iface, BindProtocol, default_protocol_handler.IInternetProtocolSink_iface);
846 static HRESULT WINAPI ProtocolSinkHandler_QueryInterface(IInternetProtocolSink *iface,
847 REFIID riid, void **ppvObject)
849 BindProtocol *This = impl_from_IInternetProtocolSinkHandler(iface);
850 return IInternetProtocol_QueryInterface(&This->default_protocol_handler.IInternetProtocol_iface,
851 riid, ppvObject);
854 static ULONG WINAPI ProtocolSinkHandler_AddRef(IInternetProtocolSink *iface)
856 BindProtocol *This = impl_from_IInternetProtocolSinkHandler(iface);
857 return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
860 static ULONG WINAPI ProtocolSinkHandler_Release(IInternetProtocolSink *iface)
862 BindProtocol *This = impl_from_IInternetProtocolSinkHandler(iface);
863 return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
866 static HRESULT WINAPI ProtocolSinkHandler_Switch(IInternetProtocolSink *iface,
867 PROTOCOLDATA *pProtocolData)
869 BindProtocol *This = impl_from_IInternetProtocolSinkHandler(iface);
871 TRACE("(%p)->(%p)\n", This, pProtocolData);
873 if(!This->protocol_sink) {
874 IInternetProtocol_Continue(This->protocol_handler, pProtocolData);
875 return S_OK;
878 return IInternetProtocolSink_Switch(This->protocol_sink, pProtocolData);
881 static HRESULT WINAPI ProtocolSinkHandler_ReportProgress(IInternetProtocolSink *iface,
882 ULONG status_code, LPCWSTR status_text)
884 BindProtocol *This = impl_from_IInternetProtocolSinkHandler(iface);
886 TRACE("(%p)->(%s %s)\n", This, debugstr_bindstatus(status_code), debugstr_w(status_text));
888 if(!This->protocol_sink)
889 return S_OK;
891 switch(status_code) {
892 case BINDSTATUS_FINDINGRESOURCE:
893 case BINDSTATUS_CONNECTING:
894 case BINDSTATUS_REDIRECTING:
895 case BINDSTATUS_SENDINGREQUEST:
896 case BINDSTATUS_CACHEFILENAMEAVAILABLE:
897 case BINDSTATUS_DIRECTBIND:
898 case BINDSTATUS_ACCEPTRANGES:
899 case BINDSTATUS_DECODING:
900 IInternetProtocolSink_ReportProgress(This->protocol_sink, status_code, status_text);
901 break;
903 case BINDSTATUS_BEGINDOWNLOADDATA:
904 IInternetProtocolSink_ReportData(This->protocol_sink, This->bscf, This->progress, This->progress_max);
905 break;
907 case BINDSTATUS_MIMETYPEAVAILABLE:
908 mime_available(This, status_text, FALSE);
909 break;
911 case BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE:
912 mime_available(This, status_text, TRUE);
913 break;
915 default:
916 FIXME("unsupported ulStatusCode %u\n", status_code);
919 return S_OK;
922 static HRESULT WINAPI ProtocolSinkHandler_ReportData(IInternetProtocolSink *iface,
923 DWORD bscf, ULONG progress, ULONG progress_max)
925 BindProtocol *This = impl_from_IInternetProtocolSinkHandler(iface);
927 TRACE("(%p)->(%x %u %u)\n", This, bscf, progress, progress_max);
929 This->bscf = bscf;
930 This->progress = progress;
931 This->progress_max = progress_max;
933 if(!This->protocol_sink)
934 return S_OK;
936 if((This->pi & PI_MIMEVERIFICATION) && !This->reported_mime) {
937 BYTE buf[BUFFER_SIZE];
938 DWORD read = 0;
939 LPWSTR mime;
940 HRESULT hres;
942 do {
943 read = 0;
944 if(is_apartment_thread(This))
945 This->continue_call++;
946 hres = IInternetProtocol_Read(This->protocol, buf,
947 sizeof(buf)-This->buf_size, &read);
948 if(is_apartment_thread(This))
949 This->continue_call--;
950 if(FAILED(hres) && hres != E_PENDING)
951 return hres;
953 if(!This->buf) {
954 This->buf = heap_alloc(BUFFER_SIZE);
955 if(!This->buf)
956 return E_OUTOFMEMORY;
957 }else if(read + This->buf_size > BUFFER_SIZE) {
958 BYTE *tmp;
960 tmp = heap_realloc(This->buf, read+This->buf_size);
961 if(!tmp)
962 return E_OUTOFMEMORY;
963 This->buf = tmp;
966 memcpy(This->buf+This->buf_size, buf, read);
967 This->buf_size += read;
968 }while(This->buf_size < MIME_TEST_SIZE && hres == S_OK);
970 if(This->buf_size < MIME_TEST_SIZE && hres != S_FALSE)
971 return S_OK;
973 bscf = BSCF_FIRSTDATANOTIFICATION;
974 if(hres == S_FALSE)
975 bscf |= BSCF_LASTDATANOTIFICATION|BSCF_DATAFULLYAVAILABLE;
977 if(!This->reported_mime) {
978 BSTR raw_uri;
980 hres = IUri_GetRawUri(This->uri, &raw_uri);
981 if(FAILED(hres))
982 return hres;
984 hres = FindMimeFromData(NULL, raw_uri, This->buf, min(This->buf_size, MIME_TEST_SIZE),
985 This->mime, 0, &mime, 0);
986 SysFreeString(raw_uri);
987 if(FAILED(hres))
988 return hres;
990 heap_free(This->mime);
991 This->mime = heap_strdupW(mime);
992 CoTaskMemFree(mime);
993 This->reported_mime = TRUE;
994 if(This->protocol_sink)
995 IInternetProtocolSink_ReportProgress(This->protocol_sink, BINDSTATUS_MIMETYPEAVAILABLE, This->mime);
999 if(!This->protocol_sink)
1000 return S_OK;
1002 return IInternetProtocolSink_ReportData(This->protocol_sink, bscf, progress, progress_max);
1005 static HRESULT handle_redirect(BindProtocol *This, const WCHAR *url)
1007 HRESULT hres;
1009 if(This->redirect_callback) {
1010 VARIANT_BOOL cancel = VARIANT_FALSE;
1011 IBindCallbackRedirect_Redirect(This->redirect_callback, url, &cancel);
1012 if(cancel)
1013 return INET_E_REDIRECT_FAILED;
1016 if(This->protocol_sink) {
1017 hres = IInternetProtocolSink_ReportProgress(This->protocol_sink, BINDSTATUS_REDIRECTING, url);
1018 if(FAILED(hres))
1019 return hres;
1022 IInternetProtocol_Terminate(This->protocol, 0); /* should this be done in StartEx? */
1023 release_protocol_handler(This);
1025 return IInternetProtocolEx_Start(&This->IInternetProtocolEx_iface, url, This->protocol_sink, This->bind_info, This->pi, 0);
1028 static HRESULT WINAPI ProtocolSinkHandler_ReportResult(IInternetProtocolSink *iface,
1029 HRESULT hrResult, DWORD dwError, LPCWSTR szResult)
1031 BindProtocol *This = impl_from_IInternetProtocolSinkHandler(iface);
1033 TRACE("(%p)->(%08x %d %s)\n", This, hrResult, dwError, debugstr_w(szResult));
1035 if(hrResult == INET_E_REDIRECT_FAILED) {
1036 hrResult = handle_redirect(This, szResult);
1037 if(hrResult == S_OK)
1038 return S_OK;
1039 szResult = NULL;
1042 if(This->protocol_sink)
1043 return IInternetProtocolSink_ReportResult(This->protocol_sink, hrResult, dwError, szResult);
1044 return S_OK;
1047 static const IInternetProtocolSinkVtbl InternetProtocolSinkHandlerVtbl = {
1048 ProtocolSinkHandler_QueryInterface,
1049 ProtocolSinkHandler_AddRef,
1050 ProtocolSinkHandler_Release,
1051 ProtocolSinkHandler_Switch,
1052 ProtocolSinkHandler_ReportProgress,
1053 ProtocolSinkHandler_ReportData,
1054 ProtocolSinkHandler_ReportResult
1057 static inline BindProtocol *impl_from_IInternetBindInfo(IInternetBindInfo *iface)
1059 return CONTAINING_RECORD(iface, BindProtocol, IInternetBindInfo_iface);
1062 static HRESULT WINAPI BindInfo_QueryInterface(IInternetBindInfo *iface,
1063 REFIID riid, void **ppv)
1065 BindProtocol *This = impl_from_IInternetBindInfo(iface);
1066 return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
1069 static ULONG WINAPI BindInfo_AddRef(IInternetBindInfo *iface)
1071 BindProtocol *This = impl_from_IInternetBindInfo(iface);
1072 return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
1075 static ULONG WINAPI BindInfo_Release(IInternetBindInfo *iface)
1077 BindProtocol *This = impl_from_IInternetBindInfo(iface);
1078 return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
1081 static HRESULT WINAPI BindInfo_GetBindInfo(IInternetBindInfo *iface,
1082 DWORD *grfBINDF, BINDINFO *pbindinfo)
1084 BindProtocol *This = impl_from_IInternetBindInfo(iface);
1085 HRESULT hres;
1087 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
1089 hres = IInternetBindInfo_GetBindInfo(This->bind_info, grfBINDF, pbindinfo);
1090 if(FAILED(hres)) {
1091 WARN("GetBindInfo failed: %08x\n", hres);
1092 return hres;
1095 if((pbindinfo->dwOptions & BINDINFO_OPTIONS_DISABLEAUTOREDIRECTS) && !This->redirect_callback) {
1096 IServiceProvider *service_provider;
1098 hres = IInternetProtocolSink_QueryInterface(This->protocol_sink, &IID_IServiceProvider, (void**)&service_provider);
1099 if(SUCCEEDED(hres)) {
1100 hres = IServiceProvider_QueryService(service_provider, &IID_IBindCallbackRedirect, &IID_IBindCallbackRedirect,
1101 (void**)&This->redirect_callback);
1102 IServiceProvider_Release(service_provider);
1106 *grfBINDF |= BINDF_FROMURLMON;
1107 return hres;
1110 static HRESULT WINAPI BindInfo_GetBindString(IInternetBindInfo *iface,
1111 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
1113 BindProtocol *This = impl_from_IInternetBindInfo(iface);
1115 TRACE("(%p)->(%d %p %d %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
1117 return IInternetBindInfo_GetBindString(This->bind_info, ulStringType, ppwzStr, cEl, pcElFetched);
1120 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
1121 BindInfo_QueryInterface,
1122 BindInfo_AddRef,
1123 BindInfo_Release,
1124 BindInfo_GetBindInfo,
1125 BindInfo_GetBindString
1128 static inline BindProtocol *impl_from_IInternetPriority(IInternetPriority *iface)
1130 return CONTAINING_RECORD(iface, BindProtocol, IInternetPriority_iface);
1133 static HRESULT WINAPI InternetPriority_QueryInterface(IInternetPriority *iface,
1134 REFIID riid, void **ppv)
1136 BindProtocol *This = impl_from_IInternetPriority(iface);
1137 return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
1140 static ULONG WINAPI InternetPriority_AddRef(IInternetPriority *iface)
1142 BindProtocol *This = impl_from_IInternetPriority(iface);
1143 return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
1146 static ULONG WINAPI InternetPriority_Release(IInternetPriority *iface)
1148 BindProtocol *This = impl_from_IInternetPriority(iface);
1149 return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
1152 static HRESULT WINAPI InternetPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
1154 BindProtocol *This = impl_from_IInternetPriority(iface);
1156 TRACE("(%p)->(%d)\n", This, nPriority);
1158 This->priority = nPriority;
1159 return S_OK;
1162 static HRESULT WINAPI InternetPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
1164 BindProtocol *This = impl_from_IInternetPriority(iface);
1166 TRACE("(%p)->(%p)\n", This, pnPriority);
1168 *pnPriority = This->priority;
1169 return S_OK;
1172 static const IInternetPriorityVtbl InternetPriorityVtbl = {
1173 InternetPriority_QueryInterface,
1174 InternetPriority_AddRef,
1175 InternetPriority_Release,
1176 InternetPriority_SetPriority,
1177 InternetPriority_GetPriority
1181 static inline BindProtocol *impl_from_IInternetProtocolSink(IInternetProtocolSink *iface)
1183 return CONTAINING_RECORD(iface, BindProtocol, IInternetProtocolSink_iface);
1186 static HRESULT WINAPI BPInternetProtocolSink_QueryInterface(IInternetProtocolSink *iface,
1187 REFIID riid, void **ppv)
1189 BindProtocol *This = impl_from_IInternetProtocolSink(iface);
1190 return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
1193 static ULONG WINAPI BPInternetProtocolSink_AddRef(IInternetProtocolSink *iface)
1195 BindProtocol *This = impl_from_IInternetProtocolSink(iface);
1196 return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
1199 static ULONG WINAPI BPInternetProtocolSink_Release(IInternetProtocolSink *iface)
1201 BindProtocol *This = impl_from_IInternetProtocolSink(iface);
1202 return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
1205 typedef struct {
1206 task_header_t header;
1207 PROTOCOLDATA *data;
1208 } switch_task_t;
1210 static void switch_proc(BindProtocol *bind, task_header_t *t)
1212 switch_task_t *task = (switch_task_t*)t;
1214 IInternetProtocol_Continue(bind->protocol_handler, task->data);
1216 heap_free(task);
1219 static HRESULT WINAPI BPInternetProtocolSink_Switch(IInternetProtocolSink *iface,
1220 PROTOCOLDATA *pProtocolData)
1222 BindProtocol *This = impl_from_IInternetProtocolSink(iface);
1223 PROTOCOLDATA *data;
1225 TRACE("(%p)->(%p)\n", This, pProtocolData);
1227 TRACE("flags %x state %x data %p cb %u\n", pProtocolData->grfFlags, pProtocolData->dwState,
1228 pProtocolData->pData, pProtocolData->cbData);
1230 data = heap_alloc(sizeof(PROTOCOLDATA));
1231 if(!data)
1232 return E_OUTOFMEMORY;
1233 memcpy(data, pProtocolData, sizeof(PROTOCOLDATA));
1235 if((This->pi&PI_APARTMENTTHREADED && pProtocolData->grfFlags&PI_FORCE_ASYNC)
1236 || !do_direct_notif(This)) {
1237 switch_task_t *task;
1239 task = heap_alloc(sizeof(switch_task_t));
1240 if(!task)
1242 heap_free(data);
1243 return E_OUTOFMEMORY;
1246 task->data = data;
1248 push_task(This, &task->header, switch_proc);
1249 return S_OK;
1252 return IInternetProtocolSink_Switch(This->protocol_sink_handler, data);
1255 typedef struct {
1256 task_header_t header;
1258 ULONG status_code;
1259 LPWSTR status_text;
1260 } on_progress_task_t;
1262 static void on_progress_proc(BindProtocol *This, task_header_t *t)
1264 on_progress_task_t *task = (on_progress_task_t*)t;
1266 IInternetProtocolSink_ReportProgress(This->protocol_sink_handler, task->status_code, task->status_text);
1268 heap_free(task->status_text);
1269 heap_free(task);
1272 static HRESULT WINAPI BPInternetProtocolSink_ReportProgress(IInternetProtocolSink *iface,
1273 ULONG ulStatusCode, LPCWSTR szStatusText)
1275 BindProtocol *This = impl_from_IInternetProtocolSink(iface);
1277 TRACE("(%p)->(%u %s)\n", This, ulStatusCode, debugstr_w(szStatusText));
1279 if(do_direct_notif(This)) {
1280 IInternetProtocolSink_ReportProgress(This->protocol_sink_handler, ulStatusCode, szStatusText);
1281 }else {
1282 on_progress_task_t *task;
1284 task = heap_alloc(sizeof(on_progress_task_t));
1286 task->status_code = ulStatusCode;
1287 task->status_text = heap_strdupW(szStatusText);
1289 push_task(This, &task->header, on_progress_proc);
1292 return S_OK;
1295 typedef struct {
1296 task_header_t header;
1297 DWORD bscf;
1298 ULONG progress;
1299 ULONG progress_max;
1300 } report_data_task_t;
1302 static void report_data_proc(BindProtocol *This, task_header_t *t)
1304 report_data_task_t *task = (report_data_task_t*)t;
1306 IInternetProtocolSink_ReportData(This->protocol_sink_handler,
1307 task->bscf, task->progress, task->progress_max);
1309 heap_free(task);
1312 static HRESULT WINAPI BPInternetProtocolSink_ReportData(IInternetProtocolSink *iface,
1313 DWORD grfBSCF, ULONG ulProgress, ULONG ulProgressMax)
1315 BindProtocol *This = impl_from_IInternetProtocolSink(iface);
1317 TRACE("(%p)->(%x %u %u)\n", This, grfBSCF, ulProgress, ulProgressMax);
1319 if(!This->protocol_sink)
1320 return S_OK;
1322 if(!do_direct_notif(This)) {
1323 report_data_task_t *task;
1325 task = heap_alloc(sizeof(report_data_task_t));
1326 if(!task)
1327 return E_OUTOFMEMORY;
1329 task->bscf = grfBSCF;
1330 task->progress = ulProgress;
1331 task->progress_max = ulProgressMax;
1333 push_task(This, &task->header, report_data_proc);
1334 return S_OK;
1337 return IInternetProtocolSink_ReportData(This->protocol_sink_handler,
1338 grfBSCF, ulProgress, ulProgressMax);
1341 typedef struct {
1342 task_header_t header;
1344 HRESULT hres;
1345 DWORD err;
1346 LPWSTR str;
1347 } report_result_task_t;
1349 static void report_result_proc(BindProtocol *This, task_header_t *t)
1351 report_result_task_t *task = (report_result_task_t*)t;
1353 IInternetProtocolSink_ReportResult(This->protocol_sink_handler, task->hres, task->err, task->str);
1355 heap_free(task->str);
1356 heap_free(task);
1359 static HRESULT WINAPI BPInternetProtocolSink_ReportResult(IInternetProtocolSink *iface,
1360 HRESULT hrResult, DWORD dwError, LPCWSTR szResult)
1362 BindProtocol *This = impl_from_IInternetProtocolSink(iface);
1364 TRACE("(%p)->(%08x %d %s)\n", This, hrResult, dwError, debugstr_w(szResult));
1366 if(!This->protocol_sink)
1367 return E_FAIL;
1368 This->reported_result = TRUE;
1370 if(!do_direct_notif(This)) {
1371 report_result_task_t *task;
1373 task = heap_alloc(sizeof(report_result_task_t));
1374 if(!task)
1375 return E_OUTOFMEMORY;
1377 task->hres = hrResult;
1378 task->err = dwError;
1379 task->str = heap_strdupW(szResult);
1381 push_task(This, &task->header, report_result_proc);
1382 return S_OK;
1385 return IInternetProtocolSink_ReportResult(This->protocol_sink_handler, hrResult, dwError, szResult);
1388 static const IInternetProtocolSinkVtbl InternetProtocolSinkVtbl = {
1389 BPInternetProtocolSink_QueryInterface,
1390 BPInternetProtocolSink_AddRef,
1391 BPInternetProtocolSink_Release,
1392 BPInternetProtocolSink_Switch,
1393 BPInternetProtocolSink_ReportProgress,
1394 BPInternetProtocolSink_ReportData,
1395 BPInternetProtocolSink_ReportResult
1398 static inline BindProtocol *impl_from_IServiceProvider(IServiceProvider *iface)
1400 return CONTAINING_RECORD(iface, BindProtocol, IServiceProvider_iface);
1403 static HRESULT WINAPI BPServiceProvider_QueryInterface(IServiceProvider *iface,
1404 REFIID riid, void **ppv)
1406 BindProtocol *This = impl_from_IServiceProvider(iface);
1407 return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
1410 static ULONG WINAPI BPServiceProvider_AddRef(IServiceProvider *iface)
1412 BindProtocol *This = impl_from_IServiceProvider(iface);
1413 return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
1416 static ULONG WINAPI BPServiceProvider_Release(IServiceProvider *iface)
1418 BindProtocol *This = impl_from_IServiceProvider(iface);
1419 return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
1422 static HRESULT WINAPI BPServiceProvider_QueryService(IServiceProvider *iface,
1423 REFGUID guidService, REFIID riid, void **ppv)
1425 BindProtocol *This = impl_from_IServiceProvider(iface);
1427 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
1429 if(!This->service_provider)
1430 return E_NOINTERFACE;
1432 return IServiceProvider_QueryService(This->service_provider, guidService, riid, ppv);
1435 static const IServiceProviderVtbl ServiceProviderVtbl = {
1436 BPServiceProvider_QueryInterface,
1437 BPServiceProvider_AddRef,
1438 BPServiceProvider_Release,
1439 BPServiceProvider_QueryService
1442 HRESULT create_binding_protocol(BindProtocol **protocol)
1444 BindProtocol *ret = heap_alloc_zero(sizeof(BindProtocol));
1446 ret->IInternetProtocolEx_iface.lpVtbl = &BindProtocolVtbl;
1447 ret->IInternetBindInfo_iface.lpVtbl = &InternetBindInfoVtbl;
1448 ret->IInternetPriority_iface.lpVtbl = &InternetPriorityVtbl;
1449 ret->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
1450 ret->IInternetProtocolSink_iface.lpVtbl = &InternetProtocolSinkVtbl;
1452 ret->default_protocol_handler.IInternetProtocol_iface.lpVtbl = &InternetProtocolHandlerVtbl;
1453 ret->default_protocol_handler.IInternetProtocolSink_iface.lpVtbl = &InternetProtocolSinkHandlerVtbl;
1455 ret->ref = 1;
1456 ret->apartment_thread = GetCurrentThreadId();
1457 ret->notif_hwnd = get_notif_hwnd();
1458 ret->protocol_handler = &ret->default_protocol_handler.IInternetProtocol_iface;
1459 ret->protocol_sink_handler = &ret->default_protocol_handler.IInternetProtocolSink_iface;
1460 InitializeCriticalSection(&ret->section);
1461 ret->section.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": BindProtocol.section");
1463 URLMON_LockModule();
1465 *protocol = ret;
1466 return S_OK;