mshtml: Correctly set NULL event.
[wine] / dlls / mshtml / protocol.c
1 /*
2  * Copyright 2005 Jacek Caban
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 "config.h"
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "ole2.h"
30
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
33
34 #include "mshtml_private.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37
38 /********************************************************************
39  * common ProtocolFactory implementation
40  */
41
42 typedef struct {
43     IInternetProtocolInfo IInternetProtocolInfo_iface;
44     IClassFactory         IClassFactory_iface;
45 } ProtocolFactory;
46
47 static inline ProtocolFactory *impl_from_IInternetProtocolInfo(IInternetProtocolInfo *iface)
48 {
49     return CONTAINING_RECORD(iface, ProtocolFactory, IInternetProtocolInfo_iface);
50 }
51
52 static HRESULT WINAPI InternetProtocolInfo_QueryInterface(IInternetProtocolInfo *iface, REFIID riid, void **ppv)
53 {
54     ProtocolFactory *This = impl_from_IInternetProtocolInfo(iface);
55
56     *ppv = NULL;
57     if(IsEqualGUID(&IID_IUnknown, riid)) {
58         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
59         *ppv = &This->IInternetProtocolInfo_iface;
60     }else if(IsEqualGUID(&IID_IInternetProtocolInfo, riid)) {
61         TRACE("(%p)->(IID_IInternetProtocolInfo %p)\n", This, ppv);
62         *ppv = &This->IInternetProtocolInfo_iface;
63     }else if(IsEqualGUID(&IID_IClassFactory, riid)) {
64         TRACE("(%p)->(IID_IClassFactory %p)\n", This, ppv);
65         *ppv = &This->IClassFactory_iface;
66     }
67
68     if(!*ppv) {
69         WARN("unknown interface %s\n", debugstr_guid(riid));
70         return E_NOINTERFACE;
71     }
72
73     IInternetProtocolInfo_AddRef(iface);
74     return S_OK;
75 }
76
77 static ULONG WINAPI InternetProtocolInfo_AddRef(IInternetProtocolInfo *iface)
78 {
79     TRACE("(%p)\n", iface);
80     return 2;
81 }
82
83 static ULONG WINAPI InternetProtocolInfo_Release(IInternetProtocolInfo *iface)
84 {
85     TRACE("(%p)\n", iface);
86     return 1;
87 }
88
89 static HRESULT WINAPI InternetProtocolInfo_CombineUrl(IInternetProtocolInfo *iface,
90         LPCWSTR pwzBaseUrl, LPCWSTR pwzRelativeUrl, DWORD dwCombineFlags, LPWSTR pwzResult,
91         DWORD cchResult, DWORD* pcchResult, DWORD dwReserved)
92 {
93     TRACE("%p)->(%s %s %08x %p %d %p %d)\n", iface, debugstr_w(pwzBaseUrl),
94             debugstr_w(pwzRelativeUrl), dwCombineFlags, pwzResult, cchResult,
95             pcchResult, dwReserved);
96
97     return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
98 }
99
100 static HRESULT WINAPI InternetProtocolInfo_CompareUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl1,
101         LPCWSTR pwzUrl2, DWORD dwCompareFlags)
102 {
103     TRACE("%p)->(%s %s %08x)\n", iface, debugstr_w(pwzUrl1), debugstr_w(pwzUrl2), dwCompareFlags);
104     return E_NOTIMPL;
105 }
106
107 static inline ProtocolFactory *impl_from_IClassFactory(IClassFactory *iface)
108 {
109     return CONTAINING_RECORD(iface, ProtocolFactory, IClassFactory_iface);
110 }
111
112 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
113 {
114     ProtocolFactory *This = impl_from_IClassFactory(iface);
115     return IInternetProtocolInfo_QueryInterface(&This->IInternetProtocolInfo_iface, riid, ppv);
116 }
117
118 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
119 {
120     ProtocolFactory *This = impl_from_IClassFactory(iface);
121     return IInternetProtocolInfo_AddRef(&This->IInternetProtocolInfo_iface);
122 }
123
124 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
125 {
126     ProtocolFactory *This = impl_from_IClassFactory(iface);
127     return IInternetProtocolInfo_Release(&This->IInternetProtocolInfo_iface);
128 }
129
130 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
131 {
132     TRACE("(%p)->(%x)\n", iface, dolock);
133     return S_OK;
134 }
135
136 /********************************************************************
137  * AboutProtocol implementation
138  */
139
140 typedef struct {
141     IInternetProtocol IInternetProtocol_iface;
142
143     LONG ref;
144
145     BYTE *data;
146     ULONG data_len;
147     ULONG cur;
148
149     IUnknown *pUnkOuter;
150 } AboutProtocol;
151
152 static inline AboutProtocol *AboutProtocol_from_IInternetProtocol(IInternetProtocol *iface)
153 {
154     return CONTAINING_RECORD(iface, AboutProtocol, IInternetProtocol_iface);
155 }
156
157 static HRESULT WINAPI AboutProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
158 {
159     AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
160
161     *ppv = NULL;
162
163     if(IsEqualGUID(&IID_IUnknown, riid)) {
164         TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
165         if(This->pUnkOuter)
166             return IUnknown_QueryInterface(This->pUnkOuter, riid, ppv);
167         *ppv = &This->IInternetProtocol_iface;
168     }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
169         TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", iface, ppv);
170         *ppv = &This->IInternetProtocol_iface;
171     }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
172         TRACE("(%p)->(IID_IInternetProtocol %p)\n", iface, ppv);
173         *ppv = &This->IInternetProtocol_iface;
174     }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
175         FIXME("IServiceProvider is not implemented\n");
176         return E_NOINTERFACE;
177     }
178
179     if(!*ppv) {
180         TRACE("unknown interface %s\n", debugstr_guid(riid));
181         return E_NOINTERFACE;
182     }
183
184     IInternetProtocol_AddRef(iface);
185     return S_OK;
186 }
187
188 static ULONG WINAPI AboutProtocol_AddRef(IInternetProtocol *iface)
189 {
190     AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
191     ULONG ref = InterlockedIncrement(&This->ref);
192     TRACE("(%p) ref=%d\n", iface, ref);
193     return This->pUnkOuter ? IUnknown_AddRef(This->pUnkOuter) : ref;
194 }
195
196 static ULONG WINAPI AboutProtocol_Release(IInternetProtocol *iface)
197 {
198     AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
199     IUnknown *pUnkOuter = This->pUnkOuter;
200     ULONG ref = InterlockedDecrement(&This->ref);
201
202     TRACE("(%p) ref=%x\n", iface, ref);
203
204     if(!ref) {
205         heap_free(This->data);
206         heap_free(This);
207     }
208
209     return pUnkOuter ? IUnknown_Release(pUnkOuter) : ref;
210 }
211
212 static HRESULT WINAPI AboutProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
213         IInternetProtocolSink* pOIProtSink, IInternetBindInfo* pOIBindInfo,
214         DWORD grfPI, HANDLE_PTR dwReserved)
215 {
216     AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
217     BINDINFO bindinfo;
218     DWORD grfBINDF = 0;
219     LPCWSTR text = NULL;
220     DWORD data_len;
221     BYTE *data;
222
223     static const WCHAR html_begin[] = {0xfeff,'<','H','T','M','L','>',0};
224     static const WCHAR html_end[] = {'<','/','H','T','M','L','>',0};
225     static const WCHAR wszBlank[] = {'b','l','a','n','k',0};
226     static const WCHAR wszAbout[] = {'a','b','o','u','t',':'};
227     static const WCHAR wszTextHtml[] = {'t','e','x','t','/','h','t','m','l',0};
228
229     /* NOTE:
230      * the about protocol seems not to work as I would expect. It creates html document
231      * for a given url, eg. about:some_text -> <HTML>some_text</HTML> except for the case when
232      * some_text = "blank", when document is blank (<HTML></HMTL>). The same happens
233      * when the url does not have "about:" in the beginning.
234      */
235
236     TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
237             pOIBindInfo, grfPI, dwReserved);
238
239     memset(&bindinfo, 0, sizeof(bindinfo));
240     bindinfo.cbSize = sizeof(BINDINFO);
241     IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
242     ReleaseBindInfo(&bindinfo);
243
244     TRACE("bindf %x\n", grfBINDF);
245
246     if(strlenW(szUrl)>=sizeof(wszAbout)/sizeof(WCHAR) && !memcmp(wszAbout, szUrl, sizeof(wszAbout))) {
247         text = szUrl + sizeof(wszAbout)/sizeof(WCHAR);
248         if(!strcmpW(wszBlank, text))
249             text = NULL;
250     }
251
252     data_len = sizeof(html_begin)+sizeof(html_end)-sizeof(WCHAR)
253         + (text ? strlenW(text)*sizeof(WCHAR) : 0);
254     data = heap_alloc(data_len);
255     if(!data)
256         return E_OUTOFMEMORY;
257
258     heap_free(This->data);
259     This->data = data;
260     This->data_len = data_len;
261
262     memcpy(This->data, html_begin, sizeof(html_begin));
263     if(text)
264         strcatW((LPWSTR)This->data, text);
265     strcatW((LPWSTR)This->data, html_end);
266     
267     This->cur = 0;
268
269     IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, wszTextHtml);
270
271     IInternetProtocolSink_ReportData(pOIProtSink,
272             BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE,
273             This->data_len, This->data_len);
274
275     IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
276
277     return S_OK;
278 }
279
280 static HRESULT WINAPI AboutProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA* pProtocolData)
281 {
282     AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
283     FIXME("(%p)->(%p)\n", This, pProtocolData);
284     return E_NOTIMPL;
285 }
286
287 static HRESULT WINAPI AboutProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
288         DWORD dwOptions)
289 {
290     AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
291     FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
292     return E_NOTIMPL;
293 }
294
295 static HRESULT WINAPI AboutProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
296 {
297     AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
298     TRACE("(%p)->(%08x)\n", This, dwOptions);
299     return S_OK;
300 }
301
302 static HRESULT WINAPI AboutProtocol_Suspend(IInternetProtocol *iface)
303 {
304     AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
305     FIXME("(%p)\n", This);
306     return E_NOTIMPL;
307 }
308
309 static HRESULT WINAPI AboutProtocol_Resume(IInternetProtocol *iface)
310 {
311     AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
312     FIXME("(%p)\n", This);
313     return E_NOTIMPL;
314 }
315
316 static HRESULT WINAPI AboutProtocol_Read(IInternetProtocol *iface, void* pv, ULONG cb, ULONG* pcbRead)
317 {
318     AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
319
320     TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
321
322     if(!This->data)
323         return E_FAIL;
324
325     *pcbRead = (cb > This->data_len-This->cur ? This->data_len-This->cur : cb);
326
327     if(!*pcbRead)
328         return S_FALSE;
329
330     memcpy(pv, This->data+This->cur, *pcbRead);
331     This->cur += *pcbRead;
332
333     return S_OK;
334 }
335
336 static HRESULT WINAPI AboutProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
337         DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
338 {
339     AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
340     FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
341     return E_NOTIMPL;
342 }
343
344 static HRESULT WINAPI AboutProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
345 {
346     AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
347
348     TRACE("(%p)->(%d)\n", This, dwOptions);
349
350     return S_OK;
351 }
352
353 static HRESULT WINAPI AboutProtocol_UnlockRequest(IInternetProtocol *iface)
354 {
355     AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
356
357     TRACE("(%p)\n", This);
358
359     return S_OK;
360 }
361
362 static const IInternetProtocolVtbl AboutProtocolVtbl = {
363     AboutProtocol_QueryInterface,
364     AboutProtocol_AddRef,
365     AboutProtocol_Release,
366     AboutProtocol_Start,
367     AboutProtocol_Continue,
368     AboutProtocol_Abort,
369     AboutProtocol_Terminate,
370     AboutProtocol_Suspend,
371     AboutProtocol_Resume,
372     AboutProtocol_Read,
373     AboutProtocol_Seek,
374     AboutProtocol_LockRequest,
375     AboutProtocol_UnlockRequest
376 };
377
378 static HRESULT WINAPI AboutProtocolFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
379         REFIID riid, void **ppv)
380 {
381     AboutProtocol *ret;
382     HRESULT hres = S_OK;
383
384     TRACE("(%p)->(%p %s %p)\n", iface, pUnkOuter, debugstr_guid(riid), ppv);
385
386     ret = heap_alloc(sizeof(AboutProtocol));
387     ret->IInternetProtocol_iface.lpVtbl = &AboutProtocolVtbl;
388     ret->ref = 0;
389
390     ret->data = NULL;
391     ret->data_len = 0;
392     ret->cur = 0;
393     ret->pUnkOuter = pUnkOuter;
394
395     if(pUnkOuter) {
396         ret->ref = 1;
397         if(IsEqualGUID(&IID_IUnknown, riid))
398             *ppv = &ret->IInternetProtocol_iface;
399         else
400             hres = E_INVALIDARG;
401     }else {
402         hres = IInternetProtocol_QueryInterface(&ret->IInternetProtocol_iface, riid, ppv);
403     }
404
405     if(FAILED(hres))
406         heap_free(ret);
407
408     return hres;
409 }
410
411 static HRESULT WINAPI AboutProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
412         PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
413         DWORD* pcchResult, DWORD dwReserved)
414 {
415     TRACE("%p)->(%s %d %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), ParseAction,
416             dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
417
418     if(ParseAction == PARSE_SECURITY_URL) {
419         unsigned int len = strlenW(pwzUrl)+1;
420
421         *pcchResult = len;
422         if(len > cchResult)
423             return S_FALSE;
424
425         memcpy(pwzResult, pwzUrl, len*sizeof(WCHAR));
426         return S_OK;
427     }
428
429     if(ParseAction == PARSE_DOMAIN) {
430         if(!pcchResult)
431             return E_POINTER;
432
433         if(pwzUrl)
434             *pcchResult = strlenW(pwzUrl)+1;
435         else
436             *pcchResult = 1;
437         return E_FAIL;
438     }
439
440     return INET_E_DEFAULT_ACTION;
441 }
442
443 static HRESULT WINAPI AboutProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
444         QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
445         DWORD dwReserved)
446 {
447     TRACE("%p)->(%s %08x %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), QueryOption, dwQueryFlags, pBuffer,
448           cbBuffer, pcbBuf, dwReserved);
449
450     switch(QueryOption) {
451     case QUERY_CAN_NAVIGATE:
452         return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
453
454     case QUERY_USES_NETWORK:
455         if(!pBuffer || cbBuffer < sizeof(DWORD))
456             return E_FAIL;
457
458         *(DWORD*)pBuffer = 0;
459         if(pcbBuf)
460             *pcbBuf = sizeof(DWORD);
461
462         break;
463
464     case QUERY_IS_CACHED:
465         FIXME("Unsupported option QUERY_IS_CACHED\n");
466         return E_NOTIMPL;
467     case QUERY_IS_INSTALLEDENTRY:
468         FIXME("Unsupported option QUERY_IS_INSTALLEDENTRY\n");
469         return E_NOTIMPL;
470     case QUERY_IS_CACHED_OR_MAPPED:
471         FIXME("Unsupported option QUERY_IS_CACHED_OR_MAPPED\n");
472         return E_NOTIMPL;
473     case QUERY_IS_SECURE:
474         FIXME("Unsupported option QUERY_IS_SECURE\n");
475         return E_NOTIMPL;
476     case QUERY_IS_SAFE:
477         FIXME("Unsupported option QUERY_IS_SAFE\n");
478         return E_NOTIMPL;
479     case QUERY_USES_HISTORYFOLDER:
480         FIXME("Unsupported option QUERY_USES_HISTORYFOLDER\n");
481         return E_FAIL;
482     default:
483         return E_FAIL;
484     }
485
486     return S_OK;
487 }
488
489 static const IInternetProtocolInfoVtbl AboutProtocolInfoVtbl = {
490     InternetProtocolInfo_QueryInterface,
491     InternetProtocolInfo_AddRef,
492     InternetProtocolInfo_Release,
493     AboutProtocolInfo_ParseUrl,
494     InternetProtocolInfo_CombineUrl,
495     InternetProtocolInfo_CompareUrl,
496     AboutProtocolInfo_QueryInfo
497 };
498
499 static const IClassFactoryVtbl AboutProtocolFactoryVtbl = {
500     ClassFactory_QueryInterface,
501     ClassFactory_AddRef,
502     ClassFactory_Release,
503     AboutProtocolFactory_CreateInstance,
504     ClassFactory_LockServer
505 };
506
507 static ProtocolFactory AboutProtocolFactory = {
508     { &AboutProtocolInfoVtbl },
509     { &AboutProtocolFactoryVtbl }
510 };
511
512 /********************************************************************
513  * ResProtocol implementation
514  */
515
516 typedef struct {
517     IInternetProtocol IInternetProtocol_iface;
518     LONG ref;
519
520     BYTE *data;
521     ULONG data_len;
522     ULONG cur;
523
524     IUnknown *pUnkOuter;
525 } ResProtocol;
526
527 static inline ResProtocol *ResProtocol_from_IInternetProtocol(IInternetProtocol *iface)
528 {
529     return CONTAINING_RECORD(iface, ResProtocol, IInternetProtocol_iface);
530 }
531
532 static HRESULT WINAPI ResProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
533 {
534     ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
535
536     *ppv = NULL;
537
538     if(IsEqualGUID(&IID_IUnknown, riid)) {
539         TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
540         if(This->pUnkOuter)
541             return IUnknown_QueryInterface(This->pUnkOuter, &IID_IUnknown, ppv);
542         *ppv = &This->IInternetProtocol_iface;
543     }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
544         TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", iface, ppv);
545         *ppv = &This->IInternetProtocol_iface;
546     }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
547         TRACE("(%p)->(IID_IInternetProtocol %p)\n", iface, ppv);
548         *ppv = &This->IInternetProtocol_iface;
549     }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
550         FIXME("IServiceProvider is not implemented\n");
551         return E_NOINTERFACE;
552     }
553
554     if(!*ppv) {
555         TRACE("unknown interface %s\n", debugstr_guid(riid));
556         return E_NOINTERFACE;
557     }
558
559     IInternetProtocol_AddRef(iface);
560     return S_OK;
561 }
562
563 static ULONG WINAPI ResProtocol_AddRef(IInternetProtocol *iface)
564 {
565     ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
566     ULONG ref = InterlockedIncrement(&This->ref);
567     TRACE("(%p) ref=%d\n", iface, ref);
568     return This->pUnkOuter ? IUnknown_AddRef(This->pUnkOuter) : ref;
569 }
570
571 static ULONG WINAPI ResProtocol_Release(IInternetProtocol *iface)
572 {
573     ResProtocol *This = (ResProtocol*)iface;
574     IUnknown *pUnkOuter = This->pUnkOuter;
575     ULONG ref = InterlockedDecrement(&This->ref);
576
577     TRACE("(%p) ref=%x\n", iface, ref);
578
579     if(!ref) {
580         heap_free(This->data);
581         heap_free(This);
582     }
583
584     return pUnkOuter ? IUnknown_Release(pUnkOuter) : ref;
585 }
586
587 static HRESULT WINAPI ResProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
588         IInternetProtocolSink* pOIProtSink, IInternetBindInfo* pOIBindInfo,
589         DWORD grfPI, HANDLE_PTR dwReserved)
590 {
591     ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
592     DWORD grfBINDF = 0, len;
593     BINDINFO bindinfo;
594     LPWSTR url_dll, url_file, url, mime, res_type = (LPWSTR)RT_HTML;
595     HMODULE hdll;
596     HRSRC src;
597     HRESULT hres;
598
599     static const WCHAR wszRes[] = {'r','e','s',':','/','/'};
600
601     TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
602             pOIBindInfo, grfPI, dwReserved);
603
604     memset(&bindinfo, 0, sizeof(bindinfo));
605     bindinfo.cbSize = sizeof(BINDINFO);
606     IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
607     ReleaseBindInfo(&bindinfo);
608
609     len = strlenW(szUrl)+16;
610     url = heap_alloc(len*sizeof(WCHAR));
611     hres = CoInternetParseUrl(szUrl, PARSE_ENCODE, 0, url, len, &len, 0);
612     if(FAILED(hres)) {
613         WARN("CoInternetParseUrl failed: %08x\n", hres);
614         heap_free(url);
615         IInternetProtocolSink_ReportResult(pOIProtSink, hres, 0, NULL);
616         return hres;
617     }
618
619     if(len < sizeof(wszRes)/sizeof(wszRes[0]) || memcmp(url, wszRes, sizeof(wszRes))) {
620         WARN("Wrong protocol of url: %s\n", debugstr_w(url));
621         IInternetProtocolSink_ReportResult(pOIProtSink, E_INVALIDARG, 0, NULL);
622         heap_free(url);
623         return E_INVALIDARG;
624     }
625
626     url_dll = url + sizeof(wszRes)/sizeof(wszRes[0]);
627     if(!(url_file = strrchrW(url_dll, '/'))) {
628         WARN("wrong url: %s\n", debugstr_w(url));
629         IInternetProtocolSink_ReportResult(pOIProtSink, MK_E_SYNTAX, 0, NULL);
630         heap_free(url);
631         return MK_E_SYNTAX;
632     }
633
634     *url_file++ = 0;
635     hdll = LoadLibraryExW(url_dll, NULL, LOAD_LIBRARY_AS_DATAFILE);
636     if(!hdll) {
637         if (!(res_type = strrchrW(url_dll, '/'))) {
638             WARN("Could not open dll: %s\n", debugstr_w(url_dll));
639             IInternetProtocolSink_ReportResult(pOIProtSink, HRESULT_FROM_WIN32(GetLastError()), 0, NULL);
640             heap_free(url);
641             return HRESULT_FROM_WIN32(GetLastError());
642         }
643         *res_type++ = 0;
644
645         hdll = LoadLibraryExW(url_dll, NULL, LOAD_LIBRARY_AS_DATAFILE);
646         if(!hdll) {
647             WARN("Could not open dll: %s\n", debugstr_w(url_dll));
648             IInternetProtocolSink_ReportResult(pOIProtSink, HRESULT_FROM_WIN32(GetLastError()), 0, NULL);
649             heap_free(url);
650             return HRESULT_FROM_WIN32(GetLastError());
651         }
652     }
653
654     TRACE("trying to find resource type %s, name %s\n", debugstr_w(res_type), debugstr_w(url_file));
655
656     src = FindResourceW(hdll, url_file, res_type);
657     if(!src) {
658         LPWSTR endpoint = NULL;
659         DWORD file_id = strtolW(url_file, &endpoint, 10);
660         if(endpoint == url_file+strlenW(url_file))
661             src = FindResourceW(hdll, MAKEINTRESOURCEW(file_id), MAKEINTRESOURCEW(RT_HTML));
662
663         if(!src) {
664             WARN("Could not find resource\n");
665             IInternetProtocolSink_ReportResult(pOIProtSink,
666                     HRESULT_FROM_WIN32(GetLastError()), 0, NULL);
667             heap_free(url);
668             return HRESULT_FROM_WIN32(GetLastError());
669         }
670     }
671
672     if(This->data) {
673         WARN("data already loaded\n");
674         heap_free(This->data);
675     }
676
677     This->data_len = SizeofResource(hdll, src);
678     This->data = heap_alloc(This->data_len);
679     memcpy(This->data, LoadResource(hdll, src), This->data_len);
680     This->cur = 0;
681
682     FreeLibrary(hdll);
683
684     hres = FindMimeFromData(NULL, url_file, This->data, This->data_len, NULL, 0, &mime, 0);
685     heap_free(url);
686     if(SUCCEEDED(hres)) {
687         IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, mime);
688         CoTaskMemFree(mime);
689     }
690
691     IInternetProtocolSink_ReportData(pOIProtSink,
692             BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE,
693             This->data_len, This->data_len);
694
695     IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
696     
697     return S_OK;
698 }
699
700 static HRESULT WINAPI ResProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA* pProtocolData)
701 {
702     ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
703     FIXME("(%p)->(%p)\n", This, pProtocolData);
704     return E_NOTIMPL;
705 }
706
707 static HRESULT WINAPI ResProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
708         DWORD dwOptions)
709 {
710     ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
711     FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
712     return E_NOTIMPL;
713 }
714
715 static HRESULT WINAPI ResProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
716 {
717     ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
718
719     TRACE("(%p)->(%08x)\n", This, dwOptions);
720
721     /* test show that we don't have to do anything here */
722     return S_OK;
723 }
724
725 static HRESULT WINAPI ResProtocol_Suspend(IInternetProtocol *iface)
726 {
727     ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
728     FIXME("(%p)\n", This);
729     return E_NOTIMPL;
730 }
731
732 static HRESULT WINAPI ResProtocol_Resume(IInternetProtocol *iface)
733 {
734     ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
735     FIXME("(%p)\n", This);
736     return E_NOTIMPL;
737 }
738
739 static HRESULT WINAPI ResProtocol_Read(IInternetProtocol *iface, void* pv, ULONG cb, ULONG* pcbRead)
740 {
741     ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
742
743     TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
744
745     if(!This->data)
746         return E_FAIL;
747
748     *pcbRead = (cb > This->data_len-This->cur ? This->data_len-This->cur : cb);
749
750     if(!*pcbRead)
751         return S_FALSE;
752
753     memcpy(pv, This->data+This->cur, *pcbRead);
754     This->cur += *pcbRead;
755
756     return S_OK;
757 }
758
759 static HRESULT WINAPI ResProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
760         DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
761 {
762     ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
763     FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
764     return E_NOTIMPL;
765 }
766
767 static HRESULT WINAPI ResProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
768 {
769     ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
770
771     TRACE("(%p)->(%d)\n", This, dwOptions);
772
773     /* test show that we don't have to do anything here */
774     return S_OK;
775 }
776
777 static HRESULT WINAPI ResProtocol_UnlockRequest(IInternetProtocol *iface)
778 {
779     ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
780
781     TRACE("(%p)\n", This);
782
783     /* test show that we don't have to do anything here */
784     return S_OK;
785 }
786
787 static const IInternetProtocolVtbl ResProtocolVtbl = {
788     ResProtocol_QueryInterface,
789     ResProtocol_AddRef,
790     ResProtocol_Release,
791     ResProtocol_Start,
792     ResProtocol_Continue,
793     ResProtocol_Abort,
794     ResProtocol_Terminate,
795     ResProtocol_Suspend,
796     ResProtocol_Resume,
797     ResProtocol_Read,
798     ResProtocol_Seek,
799     ResProtocol_LockRequest,
800     ResProtocol_UnlockRequest
801 };
802
803 static HRESULT WINAPI ResProtocolFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
804         REFIID riid, void **ppv)
805 {
806     ResProtocol *ret;
807     HRESULT hres = S_OK;
808
809     TRACE("(%p)->(%p %s %p)\n", iface, pUnkOuter, debugstr_guid(riid), ppv);
810
811     ret = heap_alloc(sizeof(ResProtocol));
812     ret->IInternetProtocol_iface.lpVtbl = &ResProtocolVtbl;
813     ret->ref = 0;
814     ret->data = NULL;
815     ret->data_len = 0;
816     ret->cur = 0;
817     ret->pUnkOuter = pUnkOuter;
818
819     if(pUnkOuter) {
820         ret->ref = 1;
821         if(IsEqualGUID(&IID_IUnknown, riid))
822             *ppv = &ret->IInternetProtocol_iface;
823         else
824             hres = E_FAIL;
825     }else {
826         hres = IInternetProtocol_QueryInterface(&ret->IInternetProtocol_iface, riid, ppv);
827     }
828
829     if(FAILED(hres))
830         heap_free(ret);
831
832     return hres;
833 }
834
835 static HRESULT WINAPI ResProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
836         PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
837         DWORD* pcchResult, DWORD dwReserved)
838 {
839     TRACE("%p)->(%s %d %x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), ParseAction,
840             dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
841
842     if(ParseAction == PARSE_SECURITY_URL) {
843         WCHAR file_part[MAX_PATH], full_path[MAX_PATH];
844         WCHAR *ptr;
845         DWORD size, len;
846
847         static const WCHAR wszFile[] = {'f','i','l','e',':','/','/'};
848         static const WCHAR wszRes[] = {'r','e','s',':','/','/'};
849
850         if(strlenW(pwzUrl) <= sizeof(wszRes)/sizeof(WCHAR) || memcmp(pwzUrl, wszRes, sizeof(wszRes)))
851             return E_INVALIDARG;
852
853         ptr = strchrW(pwzUrl + sizeof(wszRes)/sizeof(WCHAR), '/');
854         if(!ptr)
855             return E_INVALIDARG;
856
857         len = ptr - (pwzUrl + sizeof(wszRes)/sizeof(WCHAR));
858         if(len >= sizeof(file_part)/sizeof(WCHAR)) {
859             FIXME("Too long URL\n");
860             return MK_E_SYNTAX;
861         }
862
863         memcpy(file_part, pwzUrl + sizeof(wszRes)/sizeof(WCHAR), len*sizeof(WCHAR));
864         file_part[len] = 0;
865
866         len = SearchPathW(NULL, file_part, NULL, sizeof(full_path)/sizeof(WCHAR), full_path, NULL);
867         if(!len) {
868             WARN("Could not find file %s\n", debugstr_w(file_part));
869             return MK_E_SYNTAX;
870         }
871
872         size = sizeof(wszFile)/sizeof(WCHAR) + len + 1;
873         if(pcchResult)
874             *pcchResult = size;
875         if(size > cchResult)
876             return S_FALSE;
877
878         memcpy(pwzResult, wszFile, sizeof(wszFile));
879         memcpy(pwzResult + sizeof(wszFile)/sizeof(WCHAR), full_path, (len+1)*sizeof(WCHAR));
880         return S_OK;
881     }
882
883     if(ParseAction == PARSE_DOMAIN) {
884         if(!pcchResult)
885             return E_POINTER;
886
887         if(pwzUrl)
888             *pcchResult = strlenW(pwzUrl)+1;
889         else
890             *pcchResult = 1;
891         return E_FAIL;
892     }
893
894     return INET_E_DEFAULT_ACTION;
895 }
896
897 static HRESULT WINAPI ResProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
898         QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
899         DWORD dwReserved)
900 {
901     TRACE("%p)->(%s %08x %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), QueryOption, dwQueryFlags, pBuffer,
902           cbBuffer, pcbBuf, dwReserved);
903
904     switch(QueryOption) {
905     case QUERY_USES_NETWORK:
906         if(!pBuffer || cbBuffer < sizeof(DWORD))
907             return E_FAIL;
908
909         *(DWORD*)pBuffer = 0;
910         if(pcbBuf)
911             *pcbBuf = sizeof(DWORD);
912         break;
913
914     case QUERY_IS_SECURE:
915         FIXME("not supporte QUERY_IS_SECURE\n");
916         return E_NOTIMPL;
917     case QUERY_IS_SAFE:
918         FIXME("not supporte QUERY_IS_SAFE\n");
919         return E_NOTIMPL;
920     default:
921         return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
922     }
923
924     return S_OK;
925 }
926
927 static const IInternetProtocolInfoVtbl ResProtocolInfoVtbl = {
928     InternetProtocolInfo_QueryInterface,
929     InternetProtocolInfo_AddRef,
930     InternetProtocolInfo_Release,
931     ResProtocolInfo_ParseUrl,
932     InternetProtocolInfo_CombineUrl,
933     InternetProtocolInfo_CompareUrl,
934     ResProtocolInfo_QueryInfo
935 };
936
937 static const IClassFactoryVtbl ResProtocolFactoryVtbl = {
938     ClassFactory_QueryInterface,
939     ClassFactory_AddRef,
940     ClassFactory_Release,
941     ResProtocolFactory_CreateInstance,
942     ClassFactory_LockServer
943 };
944
945 static ProtocolFactory ResProtocolFactory = {
946     { &ResProtocolInfoVtbl },
947     { &ResProtocolFactoryVtbl }
948 };
949
950 /********************************************************************
951  * JSProtocol implementation
952  */
953
954 static HRESULT WINAPI JSProtocolFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
955         REFIID riid, void **ppv)
956 {
957     FIXME("(%p)->(%p %s %p)\n", iface, pUnkOuter, debugstr_guid(riid), ppv);
958     return E_NOTIMPL;
959 }
960
961 static HRESULT WINAPI JSProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
962         PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
963         DWORD* pcchResult, DWORD dwReserved)
964 {
965     TRACE("%p)->(%s %d %x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), ParseAction,
966           dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
967
968     switch(ParseAction) {
969     case PARSE_SECURITY_URL:
970         FIXME("PARSE_SECURITY_URL\n");
971         return E_NOTIMPL;
972     case PARSE_DOMAIN:
973         FIXME("PARSE_DOMAIN\n");
974         return E_NOTIMPL;
975     default:
976         return INET_E_DEFAULT_ACTION;
977     }
978
979     return S_OK;
980 }
981
982 static HRESULT WINAPI JSProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
983         QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
984         DWORD dwReserved)
985 {
986     TRACE("%p)->(%s %08x %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), QueryOption, dwQueryFlags, pBuffer,
987           cbBuffer, pcbBuf, dwReserved);
988
989     switch(QueryOption) {
990     case QUERY_USES_NETWORK:
991         if(!pBuffer || cbBuffer < sizeof(DWORD))
992             return E_FAIL;
993
994         *(DWORD*)pBuffer = 0;
995         if(pcbBuf)
996             *pcbBuf = sizeof(DWORD);
997         break;
998
999     case QUERY_IS_SECURE:
1000         FIXME("not supporte QUERY_IS_SECURE\n");
1001         return E_NOTIMPL;
1002
1003     default:
1004         return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
1005     }
1006
1007     return S_OK;
1008 }
1009
1010 static const IInternetProtocolInfoVtbl JSProtocolInfoVtbl = {
1011     InternetProtocolInfo_QueryInterface,
1012     InternetProtocolInfo_AddRef,
1013     InternetProtocolInfo_Release,
1014     JSProtocolInfo_ParseUrl,
1015     InternetProtocolInfo_CombineUrl,
1016     InternetProtocolInfo_CompareUrl,
1017     JSProtocolInfo_QueryInfo
1018 };
1019
1020 static const IClassFactoryVtbl JSProtocolFactoryVtbl = {
1021     ClassFactory_QueryInterface,
1022     ClassFactory_AddRef,
1023     ClassFactory_Release,
1024     JSProtocolFactory_CreateInstance,
1025     ClassFactory_LockServer
1026 };
1027
1028 static ProtocolFactory JSProtocolFactory = {
1029     { &JSProtocolInfoVtbl },
1030     { &JSProtocolFactoryVtbl }
1031 };
1032
1033 HRESULT ProtocolFactory_Create(REFCLSID rclsid, REFIID riid, void **ppv)
1034 {
1035     ProtocolFactory *cf = NULL;
1036
1037     if(IsEqualGUID(&CLSID_AboutProtocol, rclsid))
1038         cf = &AboutProtocolFactory;
1039     else if(IsEqualGUID(&CLSID_ResProtocol, rclsid))
1040         cf = &ResProtocolFactory;
1041     else if(IsEqualGUID(&CLSID_JSProtocol, rclsid))
1042         cf = &JSProtocolFactory;
1043
1044     if(!cf) {
1045         FIXME("not implemented protocol %s\n", debugstr_guid(rclsid));
1046         return CLASS_E_CLASSNOTAVAILABLE;
1047     }
1048  
1049     return IInternetProtocolInfo_QueryInterface(&cf->IInternetProtocolInfo_iface, riid, ppv);
1050 }