Sync with trunk (r49303)
[reactos.git] / dll / win32 / urlmon / bindprot.c
1 /*
2 * Copyright 2007-2009 Jacek Caban for CodeWeavers
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include "urlmon_main.h"
20 #include "wine/debug.h"
21
22 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
23
24 typedef struct BindProtocol BindProtocol;
25
26 struct _task_header_t;
27
28 typedef void (*task_proc_t)(BindProtocol*,struct _task_header_t*);
29
30 typedef struct _task_header_t {
31 task_proc_t proc;
32 struct _task_header_t *next;
33 } task_header_t;
34
35 struct BindProtocol {
36 const IInternetProtocolVtbl *lpIInternetProtocolVtbl;
37 const IInternetBindInfoVtbl *lpInternetBindInfoVtbl;
38 const IInternetPriorityVtbl *lpInternetPriorityVtbl;
39 const IServiceProviderVtbl *lpServiceProviderVtbl;
40 const IInternetProtocolSinkVtbl *lpIInternetProtocolSinkVtbl;
41 const IWinInetHttpInfoVtbl *lpIWinInetHttpInfoVtbl;
42
43 const IInternetProtocolVtbl *lpIInternetProtocolHandlerVtbl;
44
45 LONG ref;
46
47 IInternetProtocol *protocol;
48 IInternetProtocol *protocol_handler;
49 IInternetBindInfo *bind_info;
50 IInternetProtocolSink *protocol_sink;
51 IServiceProvider *service_provider;
52 IWinInetInfo *wininet_info;
53
54 LONG priority;
55
56 BOOL reported_result;
57 BOOL reported_mime;
58 BOOL from_urlmon;
59 DWORD pi;
60
61 DWORD apartment_thread;
62 HWND notif_hwnd;
63 DWORD continue_call;
64
65 CRITICAL_SECTION section;
66 task_header_t *task_queue_head, *task_queue_tail;
67
68 BYTE *buf;
69 DWORD buf_size;
70 LPWSTR mime;
71 LPWSTR url;
72 ProtocolProxy *filter_proxy;
73 };
74
75 #define BINDINFO(x) ((IInternetBindInfo*) &(x)->lpInternetBindInfoVtbl)
76 #define PRIORITY(x) ((IInternetPriority*) &(x)->lpInternetPriorityVtbl)
77 #define HTTPINFO(x) ((IWinInetHttpInfo*) &(x)->lpIWinInetHttpInfoVtbl)
78 #define SERVPROV(x) ((IServiceProvider*) &(x)->lpServiceProviderVtbl)
79
80 #define PROTOCOLHANDLER(x) ((IInternetProtocol*) &(x)->lpIInternetProtocolHandlerVtbl)
81
82 #define BUFFER_SIZE 2048
83 #define MIME_TEST_SIZE 255
84
85 #define WM_MK_CONTINUE (WM_USER+101)
86 #define WM_MK_RELEASE (WM_USER+102)
87
88 static LRESULT WINAPI notif_wnd_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
89 {
90 switch(msg) {
91 case WM_MK_CONTINUE: {
92 BindProtocol *This = (BindProtocol*)lParam;
93 task_header_t *task;
94
95 while(1) {
96 EnterCriticalSection(&This->section);
97
98 task = This->task_queue_head;
99 if(task) {
100 This->task_queue_head = task->next;
101 if(!This->task_queue_head)
102 This->task_queue_tail = NULL;
103 }
104
105 LeaveCriticalSection(&This->section);
106
107 if(!task)
108 break;
109
110 This->continue_call++;
111 task->proc(This, task);
112 This->continue_call--;
113 }
114
115 IInternetProtocol_Release(PROTOCOL(This));
116 return 0;
117 }
118 case WM_MK_RELEASE: {
119 tls_data_t *data = get_tls_data();
120
121 if(!--data->notif_hwnd_cnt) {
122 DestroyWindow(hwnd);
123 data->notif_hwnd = NULL;
124 }
125 }
126 }
127
128 return DefWindowProcW(hwnd, msg, wParam, lParam);
129 }
130
131 HWND get_notif_hwnd(void)
132 {
133 static ATOM wnd_class = 0;
134 tls_data_t *tls_data;
135
136 static const WCHAR wszURLMonikerNotificationWindow[] =
137 {'U','R','L',' ','M','o','n','i','k','e','r',' ',
138 'N','o','t','i','f','i','c','a','t','i','o','n',' ','W','i','n','d','o','w',0};
139
140 tls_data = get_tls_data();
141 if(!tls_data)
142 return NULL;
143
144 if(tls_data->notif_hwnd_cnt) {
145 tls_data->notif_hwnd_cnt++;
146 return tls_data->notif_hwnd;
147 }
148
149 if(!wnd_class) {
150 static WNDCLASSEXW wndclass = {
151 sizeof(wndclass), 0,
152 notif_wnd_proc, 0, 0,
153 NULL, NULL, NULL, NULL, NULL,
154 wszURLMonikerNotificationWindow,
155 NULL
156 };
157
158 wndclass.hInstance = hProxyDll;
159
160 wnd_class = RegisterClassExW(&wndclass);
161 if (!wnd_class && GetLastError() == ERROR_CLASS_ALREADY_EXISTS)
162 wnd_class = 1;
163 }
164
165 tls_data->notif_hwnd = CreateWindowExW(0, wszURLMonikerNotificationWindow,
166 wszURLMonikerNotificationWindow, 0, 0, 0, 0, 0, HWND_MESSAGE,
167 NULL, hProxyDll, NULL);
168 if(tls_data->notif_hwnd)
169 tls_data->notif_hwnd_cnt++;
170
171 TRACE("hwnd = %p\n", tls_data->notif_hwnd);
172
173 return tls_data->notif_hwnd;
174 }
175
176 void release_notif_hwnd(HWND hwnd)
177 {
178 tls_data_t *data = get_tls_data();
179
180 if(!data)
181 return;
182
183 if(data->notif_hwnd != hwnd) {
184 PostMessageW(data->notif_hwnd, WM_MK_RELEASE, 0, 0);
185 return;
186 }
187
188 if(!--data->notif_hwnd_cnt) {
189 DestroyWindow(data->notif_hwnd);
190 data->notif_hwnd = NULL;
191 }
192 }
193
194 static void push_task(BindProtocol *This, task_header_t *task, task_proc_t proc)
195 {
196 BOOL do_post = FALSE;
197
198 task->proc = proc;
199 task->next = NULL;
200
201 EnterCriticalSection(&This->section);
202
203 if(This->task_queue_tail) {
204 This->task_queue_tail->next = task;
205 This->task_queue_tail = task;
206 }else {
207 This->task_queue_tail = This->task_queue_head = task;
208 do_post = !This->continue_call;
209 }
210
211 LeaveCriticalSection(&This->section);
212
213 if(do_post) {
214 IInternetProtocol_AddRef(PROTOCOL(This));
215 PostMessageW(This->notif_hwnd, WM_MK_CONTINUE, 0, (LPARAM)This);
216 }
217 }
218
219 static inline BOOL do_direct_notif(BindProtocol *This)
220 {
221 return !(This->pi & PI_APARTMENTTHREADED) || (This->apartment_thread == GetCurrentThreadId() && !This->continue_call);
222 }
223
224 static HRESULT handle_mime_filter(BindProtocol *This, IInternetProtocol *mime_filter, LPCWSTR mime)
225 {
226 PROTOCOLFILTERDATA filter_data = { sizeof(PROTOCOLFILTERDATA), NULL, NULL, NULL, 0 };
227 IInternetProtocolSink *protocol_sink, *old_sink;
228 ProtocolProxy *filter_proxy;
229 HRESULT hres;
230
231 hres = IInternetProtocol_QueryInterface(mime_filter, &IID_IInternetProtocolSink, (void**)&protocol_sink);
232 if(FAILED(hres))
233 return hres;
234
235 hres = create_protocol_proxy(PROTOCOLHANDLER(This), This->protocol_sink, &filter_proxy);
236 if(FAILED(hres)) {
237 IInternetProtocolSink_Release(protocol_sink);
238 return hres;
239 }
240
241 old_sink = This->protocol_sink;
242 This->protocol_sink = protocol_sink;
243 This->filter_proxy = filter_proxy;
244
245 IInternetProtocol_AddRef(mime_filter);
246 This->protocol_handler = mime_filter;
247
248 filter_data.pProtocol = PROTOCOL(filter_proxy);
249 hres = IInternetProtocol_Start(mime_filter, mime, PROTSINK(filter_proxy), BINDINFO(This),
250 PI_FILTER_MODE|PI_FORCE_ASYNC, (HANDLE_PTR)&filter_data);
251 if(FAILED(hres)) {
252 IInternetProtocolSink_Release(old_sink);
253 return hres;
254 }
255
256 IInternetProtocolSink_ReportProgress(old_sink, BINDSTATUS_LOADINGMIMEHANDLER, NULL);
257 IInternetProtocolSink_Release(old_sink);
258
259 This->pi &= ~PI_MIMEVERIFICATION; /* FIXME: more tests */
260 return S_OK;
261 }
262
263 static void mime_available(BindProtocol *This, LPCWSTR mime, BOOL verified)
264 {
265 IInternetProtocol *mime_filter;
266 HRESULT hres;
267
268 heap_free(This->mime);
269 This->mime = NULL;
270
271 mime_filter = get_mime_filter(mime);
272 if(mime_filter) {
273 TRACE("Got mime filter for %s\n", debugstr_w(mime));
274
275 hres = handle_mime_filter(This, mime_filter, mime);
276 IInternetProtocol_Release(mime_filter);
277 if(FAILED(hres))
278 FIXME("MIME filter failed: %08x\n", hres);
279 }else {
280 This->mime = heap_strdupW(mime);
281
282 if(verified || !(This->pi & PI_MIMEVERIFICATION)) {
283 This->reported_mime = TRUE;
284
285 if(This->protocol_sink)
286 IInternetProtocolSink_ReportProgress(This->protocol_sink, BINDSTATUS_MIMETYPEAVAILABLE, mime);
287 }
288 }
289 }
290
291 #define PROTOCOL_THIS(iface) DEFINE_THIS(BindProtocol, IInternetProtocol, iface)
292
293 static HRESULT WINAPI BindProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
294 {
295 BindProtocol *This = PROTOCOL_THIS(iface);
296
297 *ppv = NULL;
298 if(IsEqualGUID(&IID_IUnknown, riid)) {
299 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
300 *ppv = PROTOCOL(This);
301 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
302 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
303 *ppv = PROTOCOL(This);
304 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
305 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
306 *ppv = PROTOCOL(This);
307 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
308 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
309 *ppv = BINDINFO(This);
310 }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
311 TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
312 *ppv = PRIORITY(This);
313 }else if(IsEqualGUID(&IID_IAuthenticate, riid)) {
314 FIXME("(%p)->(IID_IAuthenticate %p)\n", This, ppv);
315 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
316 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
317 *ppv = SERVPROV(This);
318 }else if(IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
319 TRACE("(%p)->(IID_IInternetProtocolSink %p)\n", This, ppv);
320 *ppv = PROTSINK(This);
321 }else if(IsEqualGUID(&IID_IWinInetInfo, riid)) {
322 TRACE("(%p)->(IID_IWinInetInfo %p)\n", This, ppv);
323
324 if(This->protocol) {
325 IWinInetInfo *inet_info;
326 HRESULT hres;
327
328 hres = IInternetProtocol_QueryInterface(This->protocol, &IID_IWinInetInfo, (void**)&inet_info);
329 if(SUCCEEDED(hres)) {
330 *ppv = HTTPINFO(This);
331 IWinInetInfo_Release(inet_info);
332 }
333 }
334 }else if(IsEqualGUID(&IID_IWinInetHttpInfo, riid)) {
335 TRACE("(%p)->(IID_IWinInetHttpInfo %p)\n", This, ppv);
336
337 if(This->protocol) {
338 IWinInetHttpInfo *http_info;
339 HRESULT hres;
340
341 hres = IInternetProtocol_QueryInterface(This->protocol, &IID_IWinInetHttpInfo, (void**)&http_info);
342 if(SUCCEEDED(hres)) {
343 *ppv = HTTPINFO(This);
344 IWinInetHttpInfo_Release(http_info);
345 }
346 }
347 }else {
348 WARN("not supported interface %s\n", debugstr_guid(riid));
349 }
350
351 if(!*ppv)
352 return E_NOINTERFACE;
353
354 IUnknown_AddRef((IUnknown*)*ppv);
355 return S_OK;
356 }
357
358 static ULONG WINAPI BindProtocol_AddRef(IInternetProtocol *iface)
359 {
360 BindProtocol *This = PROTOCOL_THIS(iface);
361 LONG ref = InterlockedIncrement(&This->ref);
362 TRACE("(%p) ref=%d\n", This, ref);
363 return ref;
364 }
365
366 static ULONG WINAPI BindProtocol_Release(IInternetProtocol *iface)
367 {
368 BindProtocol *This = PROTOCOL_THIS(iface);
369 LONG ref = InterlockedDecrement(&This->ref);
370
371 TRACE("(%p) ref=%d\n", This, ref);
372
373 if(!ref) {
374 if(This->wininet_info)
375 IWinInetInfo_Release(This->wininet_info);
376 if(This->protocol)
377 IInternetProtocol_Release(This->protocol);
378 if(This->bind_info)
379 IInternetBindInfo_Release(This->bind_info);
380 if(This->protocol_handler && This->protocol_handler != PROTOCOLHANDLER(This))
381 IInternetProtocol_Release(This->protocol_handler);
382 if(This->filter_proxy)
383 IInternetProtocol_Release(PROTOCOL(This->filter_proxy));
384
385 set_binding_sink(PROTOCOL(This), NULL, NULL);
386
387 if(This->notif_hwnd)
388 release_notif_hwnd(This->notif_hwnd);
389 DeleteCriticalSection(&This->section);
390
391 heap_free(This->mime);
392 heap_free(This->url);
393 heap_free(This);
394
395 URLMON_UnlockModule();
396 }
397
398 return ref;
399 }
400
401 static HRESULT WINAPI BindProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
402 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
403 DWORD grfPI, HANDLE_PTR dwReserved)
404 {
405 BindProtocol *This = PROTOCOL_THIS(iface);
406
407 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
408 pOIBindInfo, grfPI, dwReserved);
409
410 return IInternetProtocol_Start(This->protocol_handler, szUrl, pOIProtSink, pOIBindInfo, grfPI, dwReserved);
411 }
412
413 static HRESULT WINAPI BindProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
414 {
415 BindProtocol *This = PROTOCOL_THIS(iface);
416
417 TRACE("(%p)->(%p)\n", This, pProtocolData);
418
419 return IInternetProtocol_Continue(This->protocol_handler, pProtocolData);
420 }
421
422 static HRESULT WINAPI BindProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
423 DWORD dwOptions)
424 {
425 BindProtocol *This = PROTOCOL_THIS(iface);
426 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
427 return E_NOTIMPL;
428 }
429
430 static HRESULT WINAPI BindProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
431 {
432 BindProtocol *This = PROTOCOL_THIS(iface);
433
434 TRACE("(%p)->(%08x)\n", This, dwOptions);
435
436 return IInternetProtocol_Terminate(This->protocol_handler, dwOptions);
437 }
438
439 static HRESULT WINAPI BindProtocol_Suspend(IInternetProtocol *iface)
440 {
441 BindProtocol *This = PROTOCOL_THIS(iface);
442 FIXME("(%p)\n", This);
443 return E_NOTIMPL;
444 }
445
446 static HRESULT WINAPI BindProtocol_Resume(IInternetProtocol *iface)
447 {
448 BindProtocol *This = PROTOCOL_THIS(iface);
449 FIXME("(%p)\n", This);
450 return E_NOTIMPL;
451 }
452
453 static HRESULT WINAPI BindProtocol_Read(IInternetProtocol *iface, void *pv,
454 ULONG cb, ULONG *pcbRead)
455 {
456 BindProtocol *This = PROTOCOL_THIS(iface);
457
458 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
459
460 if(pcbRead)
461 *pcbRead = 0;
462 return IInternetProtocol_Read(This->protocol_handler, pv, cb, pcbRead);
463 }
464
465 static HRESULT WINAPI BindProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
466 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
467 {
468 BindProtocol *This = PROTOCOL_THIS(iface);
469 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
470 return E_NOTIMPL;
471 }
472
473 static HRESULT WINAPI BindProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
474 {
475 BindProtocol *This = PROTOCOL_THIS(iface);
476
477 TRACE("(%p)->(%08x)\n", This, dwOptions);
478
479 return IInternetProtocol_LockRequest(This->protocol_handler, dwOptions);
480 }
481
482 static HRESULT WINAPI BindProtocol_UnlockRequest(IInternetProtocol *iface)
483 {
484 BindProtocol *This = PROTOCOL_THIS(iface);
485
486 TRACE("(%p)\n", This);
487
488 return IInternetProtocol_UnlockRequest(This->protocol_handler);
489 }
490
491 void set_binding_sink(IInternetProtocol *bind_protocol, IInternetProtocolSink *sink, IInternetBindInfo *bind_info)
492 {
493 BindProtocol *This = PROTOCOL_THIS(bind_protocol);
494 IInternetProtocolSink *prev_sink;
495 IServiceProvider *service_provider = NULL;
496
497 if(sink)
498 IInternetProtocolSink_AddRef(sink);
499 prev_sink = InterlockedExchangePointer((void**)&This->protocol_sink, sink);
500 if(prev_sink)
501 IInternetProtocolSink_Release(prev_sink);
502
503 if(sink)
504 IInternetProtocolSink_QueryInterface(sink, &IID_IServiceProvider, (void**)&service_provider);
505 service_provider = InterlockedExchangePointer((void**)&This->service_provider, service_provider);
506 if(service_provider)
507 IServiceProvider_Release(service_provider);
508
509 if(bind_info)
510 IInternetBindInfo_AddRef(bind_info);
511 bind_info = InterlockedExchangePointer((void**)&This->bind_info, bind_info);
512 if(bind_info)
513 IInternetBindInfo_Release(bind_info);
514 }
515
516 IWinInetInfo *get_wininet_info(IInternetProtocol *bind_protocol)
517 {
518 BindProtocol *This = PROTOCOL_THIS(bind_protocol);
519
520 return This->wininet_info;
521 }
522
523 #undef PROTOCOL_THIS
524
525 static const IInternetProtocolVtbl BindProtocolVtbl = {
526 BindProtocol_QueryInterface,
527 BindProtocol_AddRef,
528 BindProtocol_Release,
529 BindProtocol_Start,
530 BindProtocol_Continue,
531 BindProtocol_Abort,
532 BindProtocol_Terminate,
533 BindProtocol_Suspend,
534 BindProtocol_Resume,
535 BindProtocol_Read,
536 BindProtocol_Seek,
537 BindProtocol_LockRequest,
538 BindProtocol_UnlockRequest
539 };
540
541 #define PROTOCOLHANDLER_THIS(iface) DEFINE_THIS(BindProtocol, IInternetProtocolHandler, iface)
542
543 static HRESULT WINAPI ProtocolHandler_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
544 {
545 ERR("should not be called\n");
546 return E_NOINTERFACE;
547 }
548
549 static ULONG WINAPI ProtocolHandler_AddRef(IInternetProtocol *iface)
550 {
551 BindProtocol *This = PROTOCOLHANDLER_THIS(iface);
552 return IInternetProtocol_AddRef(PROTOCOL(This));
553 }
554
555 static ULONG WINAPI ProtocolHandler_Release(IInternetProtocol *iface)
556 {
557 BindProtocol *This = PROTOCOLHANDLER_THIS(iface);
558 return IInternetProtocol_Release(PROTOCOL(This));
559 }
560
561 static HRESULT WINAPI ProtocolHandler_Start(IInternetProtocol *iface, LPCWSTR szUrl,
562 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
563 DWORD grfPI, HANDLE_PTR dwReserved)
564 {
565 BindProtocol *This = PROTOCOLHANDLER_THIS(iface);
566 IInternetProtocol *protocol = NULL;
567 IInternetPriority *priority;
568 IServiceProvider *service_provider;
569 BOOL urlmon_protocol = FALSE;
570 CLSID clsid = IID_NULL;
571 LPOLESTR clsid_str;
572 HRESULT hres;
573
574 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
575 pOIBindInfo, grfPI, dwReserved);
576
577 if(!szUrl || !pOIProtSink || !pOIBindInfo)
578 return E_INVALIDARG;
579
580 This->pi = grfPI;
581 This->url = heap_strdupW(szUrl);
582
583 hres = IInternetProtocolSink_QueryInterface(pOIProtSink, &IID_IServiceProvider,
584 (void**)&service_provider);
585 if(SUCCEEDED(hres)) {
586 /* FIXME: What's protocol CLSID here? */
587 IServiceProvider_QueryService(service_provider, &IID_IInternetProtocol,
588 &IID_IInternetProtocol, (void**)&protocol);
589 IServiceProvider_Release(service_provider);
590 }
591
592 if(!protocol) {
593 IClassFactory *cf;
594 IUnknown *unk;
595
596 hres = get_protocol_handler(szUrl, &clsid, &urlmon_protocol, &cf);
597 if(FAILED(hres))
598 return hres;
599
600 if(This->from_urlmon) {
601 hres = IClassFactory_CreateInstance(cf, NULL, &IID_IInternetProtocol, (void**)&protocol);
602 IClassFactory_Release(cf);
603 if(FAILED(hres))
604 return hres;
605 }else {
606 hres = IClassFactory_CreateInstance(cf, (IUnknown*)BINDINFO(This),
607 &IID_IUnknown, (void**)&unk);
608 IClassFactory_Release(cf);
609 if(FAILED(hres))
610 return hres;
611
612 hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocol, (void**)&protocol);
613 IUnknown_Release(unk);
614 if(FAILED(hres))
615 return hres;
616 }
617 }
618
619 StringFromCLSID(&clsid, &clsid_str);
620 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_PROTOCOLCLASSID, clsid_str);
621 CoTaskMemFree(clsid_str);
622
623 This->protocol = protocol;
624
625 if(urlmon_protocol)
626 IInternetProtocol_QueryInterface(protocol, &IID_IWinInetInfo, (void**)&This->wininet_info);
627
628 set_binding_sink(PROTOCOL(This), pOIProtSink, pOIBindInfo);
629
630 hres = IInternetProtocol_QueryInterface(protocol, &IID_IInternetPriority, (void**)&priority);
631 if(SUCCEEDED(hres)) {
632 IInternetPriority_SetPriority(priority, This->priority);
633 IInternetPriority_Release(priority);
634 }
635
636 return IInternetProtocol_Start(protocol, szUrl, PROTSINK(This), BINDINFO(This), 0, 0);
637 }
638
639 static HRESULT WINAPI ProtocolHandler_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
640 {
641 BindProtocol *This = PROTOCOLHANDLER_THIS(iface);
642 HRESULT hres;
643
644 TRACE("(%p)->(%p)\n", This, pProtocolData);
645
646 hres = IInternetProtocol_Continue(This->protocol, pProtocolData);
647
648 heap_free(pProtocolData);
649 return hres;
650 }
651
652 static HRESULT WINAPI ProtocolHandler_Abort(IInternetProtocol *iface, HRESULT hrReason,
653 DWORD dwOptions)
654 {
655 BindProtocol *This = PROTOCOLHANDLER_THIS(iface);
656 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
657 return E_NOTIMPL;
658 }
659
660 static HRESULT WINAPI ProtocolHandler_Terminate(IInternetProtocol *iface, DWORD dwOptions)
661 {
662 BindProtocol *This = PROTOCOLHANDLER_THIS(iface);
663
664 TRACE("(%p)->(%08x)\n", This, dwOptions);
665
666 if(!This->reported_result)
667 return E_FAIL;
668
669 IInternetProtocol_Terminate(This->protocol, 0);
670
671 if(This->filter_proxy) {
672 IInternetProtocol_Release(PROTOCOL(This->filter_proxy));
673 This->filter_proxy = NULL;
674 }
675
676 set_binding_sink(PROTOCOL(This), NULL, NULL);
677
678 if(This->bind_info) {
679 IInternetBindInfo_Release(This->bind_info);
680 This->bind_info = NULL;
681 }
682
683 return S_OK;
684 }
685
686 static HRESULT WINAPI ProtocolHandler_Suspend(IInternetProtocol *iface)
687 {
688 BindProtocol *This = PROTOCOLHANDLER_THIS(iface);
689 FIXME("(%p)\n", This);
690 return E_NOTIMPL;
691 }
692
693 static HRESULT WINAPI ProtocolHandler_Resume(IInternetProtocol *iface)
694 {
695 BindProtocol *This = PROTOCOLHANDLER_THIS(iface);
696 FIXME("(%p)\n", This);
697 return E_NOTIMPL;
698 }
699
700 static HRESULT WINAPI ProtocolHandler_Read(IInternetProtocol *iface, void *pv,
701 ULONG cb, ULONG *pcbRead)
702 {
703 BindProtocol *This = PROTOCOLHANDLER_THIS(iface);
704 ULONG read = 0;
705 HRESULT hres = S_OK;
706
707 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
708
709 if(This->buf_size) {
710 read = min(cb, This->buf_size);
711 memcpy(pv, This->buf, read);
712
713 if(read == This->buf_size) {
714 heap_free(This->buf);
715 This->buf = NULL;
716 }else {
717 memmove(This->buf, This->buf+cb, This->buf_size-cb);
718 }
719
720 This->buf_size -= read;
721 }
722
723 if(read < cb) {
724 ULONG cread = 0;
725
726 hres = IInternetProtocol_Read(This->protocol, (BYTE*)pv+read, cb-read, &cread);
727 read += cread;
728 }
729
730 *pcbRead = read;
731 return hres;
732 }
733
734 static HRESULT WINAPI ProtocolHandler_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
735 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
736 {
737 BindProtocol *This = PROTOCOLHANDLER_THIS(iface);
738 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
739 return E_NOTIMPL;
740 }
741
742 static HRESULT WINAPI ProtocolHandler_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
743 {
744 BindProtocol *This = PROTOCOLHANDLER_THIS(iface);
745
746 TRACE("(%p)->(%08x)\n", This, dwOptions);
747
748 return IInternetProtocol_LockRequest(This->protocol, dwOptions);
749 }
750
751 static HRESULT WINAPI ProtocolHandler_UnlockRequest(IInternetProtocol *iface)
752 {
753 BindProtocol *This = PROTOCOLHANDLER_THIS(iface);
754
755 TRACE("(%p)\n", This);
756
757 return IInternetProtocol_UnlockRequest(This->protocol);
758 }
759
760 #undef PROTOCOL_THIS
761
762 static const IInternetProtocolVtbl InternetProtocolHandlerVtbl = {
763 ProtocolHandler_QueryInterface,
764 ProtocolHandler_AddRef,
765 ProtocolHandler_Release,
766 ProtocolHandler_Start,
767 ProtocolHandler_Continue,
768 ProtocolHandler_Abort,
769 ProtocolHandler_Terminate,
770 ProtocolHandler_Suspend,
771 ProtocolHandler_Resume,
772 ProtocolHandler_Read,
773 ProtocolHandler_Seek,
774 ProtocolHandler_LockRequest,
775 ProtocolHandler_UnlockRequest
776 };
777
778 #define BINDINFO_THIS(iface) DEFINE_THIS(BindProtocol, InternetBindInfo, iface)
779
780 static HRESULT WINAPI BindInfo_QueryInterface(IInternetBindInfo *iface,
781 REFIID riid, void **ppv)
782 {
783 BindProtocol *This = BINDINFO_THIS(iface);
784 return IInternetProtocol_QueryInterface(PROTOCOL(This), riid, ppv);
785 }
786
787 static ULONG WINAPI BindInfo_AddRef(IInternetBindInfo *iface)
788 {
789 BindProtocol *This = BINDINFO_THIS(iface);
790 return IBinding_AddRef(PROTOCOL(This));
791 }
792
793 static ULONG WINAPI BindInfo_Release(IInternetBindInfo *iface)
794 {
795 BindProtocol *This = BINDINFO_THIS(iface);
796 return IBinding_Release(PROTOCOL(This));
797 }
798
799 static HRESULT WINAPI BindInfo_GetBindInfo(IInternetBindInfo *iface,
800 DWORD *grfBINDF, BINDINFO *pbindinfo)
801 {
802 BindProtocol *This = BINDINFO_THIS(iface);
803 HRESULT hres;
804
805 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
806
807 hres = IInternetBindInfo_GetBindInfo(This->bind_info, grfBINDF, pbindinfo);
808 if(FAILED(hres)) {
809 WARN("GetBindInfo failed: %08x\n", hres);
810 return hres;
811 }
812
813 *grfBINDF |= BINDF_FROMURLMON;
814 return hres;
815 }
816
817 static HRESULT WINAPI BindInfo_GetBindString(IInternetBindInfo *iface,
818 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
819 {
820 BindProtocol *This = BINDINFO_THIS(iface);
821
822 TRACE("(%p)->(%d %p %d %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
823
824 return IInternetBindInfo_GetBindString(This->bind_info, ulStringType, ppwzStr, cEl, pcElFetched);
825 }
826
827 #undef BINDFO_THIS
828
829 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
830 BindInfo_QueryInterface,
831 BindInfo_AddRef,
832 BindInfo_Release,
833 BindInfo_GetBindInfo,
834 BindInfo_GetBindString
835 };
836
837 #define PRIORITY_THIS(iface) DEFINE_THIS(BindProtocol, InternetPriority, iface)
838
839 static HRESULT WINAPI InternetPriority_QueryInterface(IInternetPriority *iface,
840 REFIID riid, void **ppv)
841 {
842 BindProtocol *This = PRIORITY_THIS(iface);
843 return IInternetProtocol_QueryInterface(PROTOCOL(This), riid, ppv);
844 }
845
846 static ULONG WINAPI InternetPriority_AddRef(IInternetPriority *iface)
847 {
848 BindProtocol *This = PRIORITY_THIS(iface);
849 return IInternetProtocol_AddRef(PROTOCOL(This));
850 }
851
852 static ULONG WINAPI InternetPriority_Release(IInternetPriority *iface)
853 {
854 BindProtocol *This = PRIORITY_THIS(iface);
855 return IInternetProtocol_Release(PROTOCOL(This));
856 }
857
858 static HRESULT WINAPI InternetPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
859 {
860 BindProtocol *This = PRIORITY_THIS(iface);
861
862 TRACE("(%p)->(%d)\n", This, nPriority);
863
864 This->priority = nPriority;
865 return S_OK;
866 }
867
868 static HRESULT WINAPI InternetPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
869 {
870 BindProtocol *This = PRIORITY_THIS(iface);
871
872 TRACE("(%p)->(%p)\n", This, pnPriority);
873
874 *pnPriority = This->priority;
875 return S_OK;
876 }
877
878 #undef PRIORITY_THIS
879
880 static const IInternetPriorityVtbl InternetPriorityVtbl = {
881 InternetPriority_QueryInterface,
882 InternetPriority_AddRef,
883 InternetPriority_Release,
884 InternetPriority_SetPriority,
885 InternetPriority_GetPriority
886
887 };
888
889 #define PROTSINK_THIS(iface) DEFINE_THIS(BindProtocol, IInternetProtocolSink, iface)
890
891 static HRESULT WINAPI BPInternetProtocolSink_QueryInterface(IInternetProtocolSink *iface,
892 REFIID riid, void **ppv)
893 {
894 BindProtocol *This = PROTSINK_THIS(iface);
895 return IInternetProtocol_QueryInterface(PROTOCOL(This), riid, ppv);
896 }
897
898 static ULONG WINAPI BPInternetProtocolSink_AddRef(IInternetProtocolSink *iface)
899 {
900 BindProtocol *This = PROTSINK_THIS(iface);
901 return IInternetProtocol_AddRef(PROTOCOL(This));
902 }
903
904 static ULONG WINAPI BPInternetProtocolSink_Release(IInternetProtocolSink *iface)
905 {
906 BindProtocol *This = PROTSINK_THIS(iface);
907 return IInternetProtocol_Release(PROTOCOL(This));
908 }
909
910 typedef struct {
911 task_header_t header;
912 PROTOCOLDATA *data;
913 } switch_task_t;
914
915 static void switch_proc(BindProtocol *bind, task_header_t *t)
916 {
917 switch_task_t *task = (switch_task_t*)t;
918
919 IInternetProtocol_Continue(bind->protocol_handler, task->data);
920
921 heap_free(task);
922 }
923
924 static HRESULT WINAPI BPInternetProtocolSink_Switch(IInternetProtocolSink *iface,
925 PROTOCOLDATA *pProtocolData)
926 {
927 BindProtocol *This = PROTSINK_THIS(iface);
928 PROTOCOLDATA *data;
929
930 TRACE("(%p)->(%p)\n", This, pProtocolData);
931
932 TRACE("flags %x state %x data %p cb %u\n", pProtocolData->grfFlags, pProtocolData->dwState,
933 pProtocolData->pData, pProtocolData->cbData);
934
935 data = heap_alloc(sizeof(PROTOCOLDATA));
936 if(!data)
937 return E_OUTOFMEMORY;
938 memcpy(data, pProtocolData, sizeof(PROTOCOLDATA));
939
940 if(!do_direct_notif(This)) {
941 switch_task_t *task;
942
943 task = heap_alloc(sizeof(switch_task_t));
944 if(!task)
945 return E_OUTOFMEMORY;
946
947 task->data = data;
948
949 push_task(This, &task->header, switch_proc);
950 return S_OK;
951 }
952
953 if(!This->protocol_sink) {
954 IInternetProtocol_Continue(This->protocol_handler, data);
955 return S_OK;
956 }
957
958 return IInternetProtocolSink_Switch(This->protocol_sink, data);
959 }
960
961 static void report_progress(BindProtocol *This, ULONG status_code, LPCWSTR status_text)
962 {
963 switch(status_code) {
964 case BINDSTATUS_FINDINGRESOURCE:
965 case BINDSTATUS_CONNECTING:
966 case BINDSTATUS_REDIRECTING:
967 case BINDSTATUS_BEGINDOWNLOADDATA:
968 case BINDSTATUS_SENDINGREQUEST:
969 case BINDSTATUS_CACHEFILENAMEAVAILABLE:
970 case BINDSTATUS_DIRECTBIND:
971 case BINDSTATUS_ACCEPTRANGES:
972 if(This->protocol_sink)
973 IInternetProtocolSink_ReportProgress(This->protocol_sink, status_code, status_text);
974 break;
975
976 case BINDSTATUS_MIMETYPEAVAILABLE:
977 mime_available(This, status_text, FALSE);
978 break;
979
980 case BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE:
981 mime_available(This, status_text, TRUE);
982 break;
983
984 default:
985 FIXME("unsupported ulStatusCode %u\n", status_code);
986 }
987 }
988
989 typedef struct {
990 task_header_t header;
991
992 ULONG status_code;
993 LPWSTR status_text;
994 } on_progress_task_t;
995
996 static void on_progress_proc(BindProtocol *This, task_header_t *t)
997 {
998 on_progress_task_t *task = (on_progress_task_t*)t;
999
1000 report_progress(This, task->status_code, task->status_text);
1001
1002 heap_free(task->status_text);
1003 heap_free(task);
1004 }
1005
1006 static HRESULT WINAPI BPInternetProtocolSink_ReportProgress(IInternetProtocolSink *iface,
1007 ULONG ulStatusCode, LPCWSTR szStatusText)
1008 {
1009 BindProtocol *This = PROTSINK_THIS(iface);
1010
1011 TRACE("(%p)->(%u %s)\n", This, ulStatusCode, debugstr_w(szStatusText));
1012
1013 if(do_direct_notif(This)) {
1014 report_progress(This, ulStatusCode, szStatusText);
1015 }else {
1016 on_progress_task_t *task;
1017
1018 task = heap_alloc(sizeof(on_progress_task_t));
1019
1020 task->status_code = ulStatusCode;
1021 task->status_text = heap_strdupW(szStatusText);
1022
1023 push_task(This, &task->header, on_progress_proc);
1024 }
1025
1026 return S_OK;
1027 }
1028
1029 static HRESULT report_data(BindProtocol *This, DWORD bscf, ULONG progress, ULONG progress_max)
1030 {
1031 if(!This->protocol_sink)
1032 return S_OK;
1033
1034 if((This->pi & PI_MIMEVERIFICATION) && !This->reported_mime) {
1035 BYTE buf[BUFFER_SIZE];
1036 DWORD read = 0;
1037 LPWSTR mime;
1038 HRESULT hres;
1039
1040 do {
1041 read = 0;
1042 hres = IInternetProtocol_Read(This->protocol, buf,
1043 sizeof(buf)-This->buf_size, &read);
1044 if(FAILED(hres) && hres != E_PENDING)
1045 return hres;
1046
1047 if(!This->buf) {
1048 This->buf = heap_alloc(BUFFER_SIZE);
1049 if(!This->buf)
1050 return E_OUTOFMEMORY;
1051 }else if(read + This->buf_size > BUFFER_SIZE) {
1052 BYTE *tmp;
1053
1054 tmp = heap_realloc(This->buf, read+This->buf_size);
1055 if(!tmp)
1056 return E_OUTOFMEMORY;
1057 This->buf = tmp;
1058 }
1059
1060 memcpy(This->buf+This->buf_size, buf, read);
1061 This->buf_size += read;
1062 }while(This->buf_size < MIME_TEST_SIZE && hres == S_OK);
1063
1064 if(This->buf_size < MIME_TEST_SIZE && hres != S_FALSE)
1065 return S_OK;
1066
1067 bscf = BSCF_FIRSTDATANOTIFICATION;
1068 if(hres == S_FALSE)
1069 bscf |= BSCF_LASTDATANOTIFICATION|BSCF_DATAFULLYAVAILABLE;
1070
1071 if(!This->reported_mime) {
1072 hres = FindMimeFromData(NULL, This->url, This->buf, min(This->buf_size, MIME_TEST_SIZE),
1073 This->mime, 0, &mime, 0);
1074 if(FAILED(hres))
1075 return hres;
1076
1077 mime_available(This, mime, TRUE);
1078 CoTaskMemFree(mime);
1079 }
1080 }
1081
1082 if(!This->protocol_sink)
1083 return S_OK;
1084
1085 return IInternetProtocolSink_ReportData(This->protocol_sink, bscf, progress, progress_max);
1086 }
1087
1088 typedef struct {
1089 task_header_t header;
1090 DWORD bscf;
1091 ULONG progress;
1092 ULONG progress_max;
1093 } report_data_task_t;
1094
1095 static void report_data_proc(BindProtocol *This, task_header_t *t)
1096 {
1097 report_data_task_t *task = (report_data_task_t*)t;
1098
1099 report_data(This, task->bscf, task->progress, task->progress_max);
1100 heap_free(task);
1101 }
1102
1103 static HRESULT WINAPI BPInternetProtocolSink_ReportData(IInternetProtocolSink *iface,
1104 DWORD grfBSCF, ULONG ulProgress, ULONG ulProgressMax)
1105 {
1106 BindProtocol *This = PROTSINK_THIS(iface);
1107
1108 TRACE("(%p)->(%d %u %u)\n", This, grfBSCF, ulProgress, ulProgressMax);
1109
1110 if(!This->protocol_sink)
1111 return S_OK;
1112
1113 if(!do_direct_notif(This)) {
1114 report_data_task_t *task;
1115
1116 task = heap_alloc(sizeof(report_data_task_t));
1117 if(!task)
1118 return E_OUTOFMEMORY;
1119
1120 task->bscf = grfBSCF;
1121 task->progress = ulProgress;
1122 task->progress_max = ulProgressMax;
1123
1124 push_task(This, &task->header, report_data_proc);
1125 return S_OK;
1126 }
1127
1128 return report_data(This, grfBSCF, ulProgress, ulProgressMax);
1129 }
1130
1131 typedef struct {
1132 task_header_t header;
1133
1134 HRESULT hres;
1135 DWORD err;
1136 LPWSTR str;
1137 } report_result_task_t;
1138
1139 static void report_result_proc(BindProtocol *This, task_header_t *t)
1140 {
1141 report_result_task_t *task = (report_result_task_t*)t;
1142
1143 if(This->protocol_sink)
1144 IInternetProtocolSink_ReportResult(This->protocol_sink, task->hres, task->err, task->str);
1145
1146 heap_free(task->str);
1147 heap_free(task);
1148 }
1149
1150 static HRESULT WINAPI BPInternetProtocolSink_ReportResult(IInternetProtocolSink *iface,
1151 HRESULT hrResult, DWORD dwError, LPCWSTR szResult)
1152 {
1153 BindProtocol *This = PROTSINK_THIS(iface);
1154
1155 TRACE("(%p)->(%08x %d %s)\n", This, hrResult, dwError, debugstr_w(szResult));
1156
1157 if(!This->protocol_sink)
1158 return E_FAIL;
1159
1160 This->reported_result = TRUE;
1161
1162 if(!do_direct_notif(This)) {
1163 report_result_task_t *task;
1164
1165 task = heap_alloc(sizeof(report_result_task_t));
1166 if(!task)
1167 return E_OUTOFMEMORY;
1168
1169 task->hres = hrResult;
1170 task->err = dwError;
1171 task->str = heap_strdupW(szResult);
1172
1173 push_task(This, &task->header, report_result_proc);
1174 return S_OK;
1175 }
1176
1177 return IInternetProtocolSink_ReportResult(This->protocol_sink, hrResult, dwError, szResult);
1178 }
1179
1180 #undef PROTSINK_THIS
1181
1182 static const IInternetProtocolSinkVtbl InternetProtocolSinkVtbl = {
1183 BPInternetProtocolSink_QueryInterface,
1184 BPInternetProtocolSink_AddRef,
1185 BPInternetProtocolSink_Release,
1186 BPInternetProtocolSink_Switch,
1187 BPInternetProtocolSink_ReportProgress,
1188 BPInternetProtocolSink_ReportData,
1189 BPInternetProtocolSink_ReportResult
1190 };
1191
1192 #define INETINFO_THIS(iface) DEFINE_THIS(BindProtocol, IWinInetHttpInfo, iface)
1193
1194 static HRESULT WINAPI WinInetHttpInfo_QueryInterface(IWinInetHttpInfo *iface, REFIID riid, void **ppv)
1195 {
1196 BindProtocol *This = INETINFO_THIS(iface);
1197 return IInternetProtocol_QueryInterface(PROTOCOL(This), riid, ppv);
1198 }
1199
1200 static ULONG WINAPI WinInetHttpInfo_AddRef(IWinInetHttpInfo *iface)
1201 {
1202 BindProtocol *This = INETINFO_THIS(iface);
1203 return IInternetProtocol_AddRef(PROTOCOL(This));
1204 }
1205
1206 static ULONG WINAPI WinInetHttpInfo_Release(IWinInetHttpInfo *iface)
1207 {
1208 BindProtocol *This = INETINFO_THIS(iface);
1209 return IInternetProtocol_Release(PROTOCOL(This));
1210 }
1211
1212 static HRESULT WINAPI WinInetHttpInfo_QueryOption(IWinInetHttpInfo *iface, DWORD dwOption,
1213 void *pBuffer, DWORD *pcbBuffer)
1214 {
1215 BindProtocol *This = INETINFO_THIS(iface);
1216 FIXME("(%p)->(%x %p %p)\n", This, dwOption, pBuffer, pcbBuffer);
1217 return E_NOTIMPL;
1218 }
1219
1220 static HRESULT WINAPI WinInetHttpInfo_QueryInfo(IWinInetHttpInfo *iface, DWORD dwOption,
1221 void *pBuffer, DWORD *pcbBuffer, DWORD *pdwFlags, DWORD *pdwReserved)
1222 {
1223 BindProtocol *This = INETINFO_THIS(iface);
1224 FIXME("(%p)->(%x %p %p %p %p)\n", This, dwOption, pBuffer, pcbBuffer, pdwFlags, pdwReserved);
1225 return E_NOTIMPL;
1226 }
1227
1228 #undef INETINFO_THIS
1229
1230 static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl = {
1231 WinInetHttpInfo_QueryInterface,
1232 WinInetHttpInfo_AddRef,
1233 WinInetHttpInfo_Release,
1234 WinInetHttpInfo_QueryOption,
1235 WinInetHttpInfo_QueryInfo
1236 };
1237
1238 #define SERVPROV_THIS(iface) DEFINE_THIS(BindProtocol, ServiceProvider, iface)
1239
1240 static HRESULT WINAPI BPServiceProvider_QueryInterface(IServiceProvider *iface,
1241 REFIID riid, void **ppv)
1242 {
1243 BindProtocol *This = SERVPROV_THIS(iface);
1244 return IInternetProtocol_QueryInterface(PROTOCOL(This), riid, ppv);
1245 }
1246
1247 static ULONG WINAPI BPServiceProvider_AddRef(IServiceProvider *iface)
1248 {
1249 BindProtocol *This = SERVPROV_THIS(iface);
1250 return IInternetProtocol_AddRef(PROTOCOL(This));
1251 }
1252
1253 static ULONG WINAPI BPServiceProvider_Release(IServiceProvider *iface)
1254 {
1255 BindProtocol *This = SERVPROV_THIS(iface);
1256 return IInternetProtocol_Release(PROTOCOL(This));
1257 }
1258
1259 static HRESULT WINAPI BPServiceProvider_QueryService(IServiceProvider *iface,
1260 REFGUID guidService, REFIID riid, void **ppv)
1261 {
1262 BindProtocol *This = SERVPROV_THIS(iface);
1263
1264 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
1265
1266 if(!This->service_provider)
1267 return E_NOINTERFACE;
1268
1269 return IServiceProvider_QueryService(This->service_provider, guidService, riid, ppv);
1270 }
1271
1272 #undef SERVPROV_THIS
1273
1274 static const IServiceProviderVtbl ServiceProviderVtbl = {
1275 BPServiceProvider_QueryInterface,
1276 BPServiceProvider_AddRef,
1277 BPServiceProvider_Release,
1278 BPServiceProvider_QueryService
1279 };
1280
1281 HRESULT create_binding_protocol(LPCWSTR url, BOOL from_urlmon, IInternetProtocol **protocol)
1282 {
1283 BindProtocol *ret = heap_alloc_zero(sizeof(BindProtocol));
1284
1285 ret->lpIInternetProtocolVtbl = &BindProtocolVtbl;
1286 ret->lpInternetBindInfoVtbl = &InternetBindInfoVtbl;
1287 ret->lpInternetPriorityVtbl = &InternetPriorityVtbl;
1288 ret->lpServiceProviderVtbl = &ServiceProviderVtbl;
1289 ret->lpIInternetProtocolSinkVtbl = &InternetProtocolSinkVtbl;
1290 ret->lpIInternetProtocolHandlerVtbl = &InternetProtocolHandlerVtbl;
1291 ret->lpIWinInetHttpInfoVtbl = &WinInetHttpInfoVtbl;
1292
1293 ret->ref = 1;
1294 ret->from_urlmon = from_urlmon;
1295 ret->apartment_thread = GetCurrentThreadId();
1296 ret->notif_hwnd = get_notif_hwnd();
1297 ret->protocol_handler = PROTOCOLHANDLER(ret);
1298 InitializeCriticalSection(&ret->section);
1299
1300 URLMON_LockModule();
1301
1302 *protocol = PROTOCOL(ret);
1303 return S_OK;
1304 }