mshtml: Avoid obj to iface cast in ProtocolFactory_Create.
[wine.git] / dlls / mshtml / protocol.c
blobf0325bf694503d9b299a34eec0fafeab9395fd61
1 /*
2 * Copyright 2005 Jacek Caban
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 "config.h"
21 #include <stdarg.h>
22 #include <stdio.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "ole2.h"
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
34 #include "mshtml_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
38 /********************************************************************
39 * common ProtocolFactory implementation
42 typedef struct {
43 IInternetProtocolInfo IInternetProtocolInfo_iface;
44 IClassFactory IClassFactory_iface;
45 } ProtocolFactory;
47 static inline ProtocolFactory *impl_from_IInternetProtocolInfo(IInternetProtocolInfo *iface)
49 return CONTAINING_RECORD(iface, ProtocolFactory, IInternetProtocolInfo_iface);
52 static HRESULT WINAPI InternetProtocolInfo_QueryInterface(IInternetProtocolInfo *iface, REFIID riid, void **ppv)
54 ProtocolFactory *This = impl_from_IInternetProtocolInfo(iface);
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;
68 if(!*ppv) {
69 WARN("unknown interface %s\n", debugstr_guid(riid));
70 return E_NOINTERFACE;
73 IInternetProtocolInfo_AddRef(iface);
74 return S_OK;
77 static ULONG WINAPI InternetProtocolInfo_AddRef(IInternetProtocolInfo *iface)
79 TRACE("(%p)\n", iface);
80 return 2;
83 static ULONG WINAPI InternetProtocolInfo_Release(IInternetProtocolInfo *iface)
85 TRACE("(%p)\n", iface);
86 return 1;
89 static HRESULT WINAPI InternetProtocolInfo_CombineUrl(IInternetProtocolInfo *iface,
90 LPCWSTR pwzBaseUrl, LPCWSTR pwzRelativeUrl, DWORD dwCombineFlags, LPWSTR pwzResult,
91 DWORD cchResult, DWORD* pcchResult, DWORD dwReserved)
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);
97 return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
100 static HRESULT WINAPI InternetProtocolInfo_CompareUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl1,
101 LPCWSTR pwzUrl2, DWORD dwCompareFlags)
103 TRACE("%p)->(%s %s %08x)\n", iface, debugstr_w(pwzUrl1), debugstr_w(pwzUrl2), dwCompareFlags);
104 return E_NOTIMPL;
107 static inline ProtocolFactory *impl_from_IClassFactory(IClassFactory *iface)
109 return CONTAINING_RECORD(iface, ProtocolFactory, IClassFactory_iface);
112 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
114 ProtocolFactory *This = impl_from_IClassFactory(iface);
115 return IInternetProtocolInfo_QueryInterface(&This->IInternetProtocolInfo_iface, riid, ppv);
118 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
120 ProtocolFactory *This = impl_from_IClassFactory(iface);
121 return IInternetProtocolInfo_AddRef(&This->IInternetProtocolInfo_iface);
124 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
126 ProtocolFactory *This = impl_from_IClassFactory(iface);
127 return IInternetProtocolInfo_Release(&This->IInternetProtocolInfo_iface);
130 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
132 TRACE("(%p)->(%x)\n", iface, dolock);
133 return S_OK;
136 /********************************************************************
137 * AboutProtocol implementation
140 typedef struct {
141 IInternetProtocol IInternetProtocol_iface;
143 LONG ref;
145 BYTE *data;
146 ULONG data_len;
147 ULONG cur;
149 IUnknown *pUnkOuter;
150 } AboutProtocol;
152 static inline AboutProtocol *AboutProtocol_from_IInternetProtocol(IInternetProtocol *iface)
154 return CONTAINING_RECORD(iface, AboutProtocol, IInternetProtocol_iface);
157 static HRESULT WINAPI AboutProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
159 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
161 *ppv = NULL;
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;
179 if(!*ppv) {
180 TRACE("unknown interface %s\n", debugstr_guid(riid));
181 return E_NOINTERFACE;
184 IInternetProtocol_AddRef(iface);
185 return S_OK;
188 static ULONG WINAPI AboutProtocol_AddRef(IInternetProtocol *iface)
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;
196 static ULONG WINAPI AboutProtocol_Release(IInternetProtocol *iface)
198 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
199 IUnknown *pUnkOuter = This->pUnkOuter;
200 ULONG ref = InterlockedDecrement(&This->ref);
202 TRACE("(%p) ref=%x\n", iface, ref);
204 if(!ref) {
205 heap_free(This->data);
206 heap_free(This);
209 return pUnkOuter ? IUnknown_Release(pUnkOuter) : ref;
212 static HRESULT WINAPI AboutProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
213 IInternetProtocolSink* pOIProtSink, IInternetBindInfo* pOIBindInfo,
214 DWORD grfPI, HANDLE_PTR dwReserved)
216 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
217 BINDINFO bindinfo;
218 DWORD grfBINDF = 0;
219 LPCWSTR text = NULL;
221 static const WCHAR html_begin[] = {0xfeff,'<','H','T','M','L','>',0};
222 static const WCHAR html_end[] = {'<','/','H','T','M','L','>',0};
223 static const WCHAR wszBlank[] = {'b','l','a','n','k',0};
224 static const WCHAR wszAbout[] = {'a','b','o','u','t',':'};
225 static const WCHAR wszTextHtml[] = {'t','e','x','t','/','h','t','m','l',0};
227 /* NOTE:
228 * the about protocol seems not to work as I would expect. It creates html document
229 * for a given url, eg. about:some_text -> <HTML>some_text</HTML> except for the case when
230 * some_text = "blank", when document is blank (<HTML></HMTL>). The same happens
231 * when the url does not have "about:" in the beginning.
234 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
235 pOIBindInfo, grfPI, dwReserved);
237 memset(&bindinfo, 0, sizeof(bindinfo));
238 bindinfo.cbSize = sizeof(BINDINFO);
239 IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
240 ReleaseBindInfo(&bindinfo);
242 TRACE("bindf %x\n", grfBINDF);
244 if(strlenW(szUrl)>=sizeof(wszAbout)/sizeof(WCHAR) && !memcmp(wszAbout, szUrl, sizeof(wszAbout))) {
245 text = szUrl + sizeof(wszAbout)/sizeof(WCHAR);
246 if(!strcmpW(wszBlank, text))
247 text = NULL;
250 This->data_len = sizeof(html_begin)+sizeof(html_end)-sizeof(WCHAR)
251 + (text ? strlenW(text)*sizeof(WCHAR) : 0);
252 This->data = heap_alloc(This->data_len);
254 memcpy(This->data, html_begin, sizeof(html_begin));
255 if(text)
256 strcatW((LPWSTR)This->data, text);
257 strcatW((LPWSTR)This->data, html_end);
259 This->cur = 0;
261 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, wszTextHtml);
263 IInternetProtocolSink_ReportData(pOIProtSink,
264 BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE,
265 This->data_len, This->data_len);
267 IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
269 return S_OK;
272 static HRESULT WINAPI AboutProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA* pProtocolData)
274 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
275 FIXME("(%p)->(%p)\n", This, pProtocolData);
276 return E_NOTIMPL;
279 static HRESULT WINAPI AboutProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
280 DWORD dwOptions)
282 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
283 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
284 return E_NOTIMPL;
287 static HRESULT WINAPI AboutProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
289 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
290 TRACE("(%p)->(%08x)\n", This, dwOptions);
291 return S_OK;
294 static HRESULT WINAPI AboutProtocol_Suspend(IInternetProtocol *iface)
296 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
297 FIXME("(%p)\n", This);
298 return E_NOTIMPL;
301 static HRESULT WINAPI AboutProtocol_Resume(IInternetProtocol *iface)
303 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
304 FIXME("(%p)\n", This);
305 return E_NOTIMPL;
308 static HRESULT WINAPI AboutProtocol_Read(IInternetProtocol *iface, void* pv, ULONG cb, ULONG* pcbRead)
310 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
312 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
314 if(!This->data)
315 return E_FAIL;
317 *pcbRead = (cb > This->data_len-This->cur ? This->data_len-This->cur : cb);
319 if(!*pcbRead)
320 return S_FALSE;
322 memcpy(pv, This->data+This->cur, *pcbRead);
323 This->cur += *pcbRead;
325 return S_OK;
328 static HRESULT WINAPI AboutProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
329 DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
331 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
332 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
333 return E_NOTIMPL;
336 static HRESULT WINAPI AboutProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
338 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
340 TRACE("(%p)->(%d)\n", This, dwOptions);
342 return S_OK;
345 static HRESULT WINAPI AboutProtocol_UnlockRequest(IInternetProtocol *iface)
347 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
349 TRACE("(%p)\n", This);
351 return S_OK;
354 static const IInternetProtocolVtbl AboutProtocolVtbl = {
355 AboutProtocol_QueryInterface,
356 AboutProtocol_AddRef,
357 AboutProtocol_Release,
358 AboutProtocol_Start,
359 AboutProtocol_Continue,
360 AboutProtocol_Abort,
361 AboutProtocol_Terminate,
362 AboutProtocol_Suspend,
363 AboutProtocol_Resume,
364 AboutProtocol_Read,
365 AboutProtocol_Seek,
366 AboutProtocol_LockRequest,
367 AboutProtocol_UnlockRequest
370 static HRESULT WINAPI AboutProtocolFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
371 REFIID riid, void **ppv)
373 AboutProtocol *ret;
374 HRESULT hres = S_OK;
376 TRACE("(%p)->(%p %s %p)\n", iface, pUnkOuter, debugstr_guid(riid), ppv);
378 ret = heap_alloc(sizeof(AboutProtocol));
379 ret->IInternetProtocol_iface.lpVtbl = &AboutProtocolVtbl;
380 ret->ref = 0;
382 ret->data = NULL;
383 ret->data_len = 0;
384 ret->cur = 0;
385 ret->pUnkOuter = pUnkOuter;
387 if(pUnkOuter) {
388 ret->ref = 1;
389 if(IsEqualGUID(&IID_IUnknown, riid))
390 *ppv = &ret->IInternetProtocol_iface;
391 else
392 hres = E_INVALIDARG;
393 }else {
394 hres = IInternetProtocol_QueryInterface(&ret->IInternetProtocol_iface, riid, ppv);
397 if(FAILED(hres))
398 heap_free(ret);
400 return hres;
403 static HRESULT WINAPI AboutProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
404 PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
405 DWORD* pcchResult, DWORD dwReserved)
407 TRACE("%p)->(%s %08x %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), ParseAction,
408 dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
410 if(ParseAction == PARSE_SECURITY_URL) {
411 unsigned int len = strlenW(pwzUrl);
413 if(len >= cchResult)
414 return S_FALSE;
416 memcpy(pwzResult, pwzUrl, (len+1)*sizeof(WCHAR));
417 return S_OK;
420 if(ParseAction == PARSE_DOMAIN) {
421 if(!pcchResult)
422 return E_POINTER;
424 if(pwzUrl)
425 *pcchResult = strlenW(pwzUrl)+1;
426 else
427 *pcchResult = 1;
428 return E_FAIL;
431 return INET_E_DEFAULT_ACTION;
434 static HRESULT WINAPI AboutProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
435 QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
436 DWORD dwReserved)
438 TRACE("%p)->(%s %08x %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), QueryOption, dwQueryFlags, pBuffer,
439 cbBuffer, pcbBuf, dwReserved);
441 switch(QueryOption) {
442 case QUERY_CAN_NAVIGATE:
443 return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
445 case QUERY_USES_NETWORK:
446 if(!pBuffer || cbBuffer < sizeof(DWORD))
447 return E_FAIL;
449 *(DWORD*)pBuffer = 0;
450 if(pcbBuf)
451 *pcbBuf = sizeof(DWORD);
453 break;
455 case QUERY_IS_CACHED:
456 FIXME("Unsupported option QUERY_IS_CACHED\n");
457 return E_NOTIMPL;
458 case QUERY_IS_INSTALLEDENTRY:
459 FIXME("Unsupported option QUERY_IS_INSTALLEDENTRY\n");
460 return E_NOTIMPL;
461 case QUERY_IS_CACHED_OR_MAPPED:
462 FIXME("Unsupported option QUERY_IS_CACHED_OR_MAPPED\n");
463 return E_NOTIMPL;
464 case QUERY_IS_SECURE:
465 FIXME("Unsupported option QUERY_IS_SECURE\n");
466 return E_NOTIMPL;
467 case QUERY_IS_SAFE:
468 FIXME("Unsupported option QUERY_IS_SAFE\n");
469 return E_NOTIMPL;
470 case QUERY_USES_HISTORYFOLDER:
471 FIXME("Unsupported option QUERY_USES_HISTORYFOLDER\n");
472 return E_FAIL;
473 default:
474 return E_FAIL;
477 return S_OK;
480 static const IInternetProtocolInfoVtbl AboutProtocolInfoVtbl = {
481 InternetProtocolInfo_QueryInterface,
482 InternetProtocolInfo_AddRef,
483 InternetProtocolInfo_Release,
484 AboutProtocolInfo_ParseUrl,
485 InternetProtocolInfo_CombineUrl,
486 InternetProtocolInfo_CompareUrl,
487 AboutProtocolInfo_QueryInfo
490 static const IClassFactoryVtbl AboutProtocolFactoryVtbl = {
491 ClassFactory_QueryInterface,
492 ClassFactory_AddRef,
493 ClassFactory_Release,
494 AboutProtocolFactory_CreateInstance,
495 ClassFactory_LockServer
498 static ProtocolFactory AboutProtocolFactory = {
499 { &AboutProtocolInfoVtbl },
500 { &AboutProtocolFactoryVtbl }
503 /********************************************************************
504 * ResProtocol implementation
507 typedef struct {
508 IInternetProtocol IInternetProtocol_iface;
509 LONG ref;
511 BYTE *data;
512 ULONG data_len;
513 ULONG cur;
515 IUnknown *pUnkOuter;
516 } ResProtocol;
518 static inline ResProtocol *ResProtocol_from_IInternetProtocol(IInternetProtocol *iface)
520 return CONTAINING_RECORD(iface, ResProtocol, IInternetProtocol_iface);
523 static HRESULT WINAPI ResProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
525 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
527 *ppv = NULL;
529 if(IsEqualGUID(&IID_IUnknown, riid)) {
530 TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
531 if(This->pUnkOuter)
532 return IUnknown_QueryInterface(This->pUnkOuter, &IID_IUnknown, ppv);
533 *ppv = &This->IInternetProtocol_iface;
534 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
535 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", iface, ppv);
536 *ppv = &This->IInternetProtocol_iface;
537 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
538 TRACE("(%p)->(IID_IInternetProtocol %p)\n", iface, ppv);
539 *ppv = &This->IInternetProtocol_iface;
540 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
541 FIXME("IServiceProvider is not implemented\n");
542 return E_NOINTERFACE;
545 if(!*ppv) {
546 TRACE("unknown interface %s\n", debugstr_guid(riid));
547 return E_NOINTERFACE;
550 IInternetProtocol_AddRef(iface);
551 return S_OK;
554 static ULONG WINAPI ResProtocol_AddRef(IInternetProtocol *iface)
556 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
557 ULONG ref = InterlockedIncrement(&This->ref);
558 TRACE("(%p) ref=%d\n", iface, ref);
559 return This->pUnkOuter ? IUnknown_AddRef(This->pUnkOuter) : ref;
562 static ULONG WINAPI ResProtocol_Release(IInternetProtocol *iface)
564 ResProtocol *This = (ResProtocol*)iface;
565 IUnknown *pUnkOuter = This->pUnkOuter;
566 ULONG ref = InterlockedDecrement(&This->ref);
568 TRACE("(%p) ref=%x\n", iface, ref);
570 if(!ref) {
571 heap_free(This->data);
572 heap_free(This);
575 return pUnkOuter ? IUnknown_Release(pUnkOuter) : ref;
578 static HRESULT WINAPI ResProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
579 IInternetProtocolSink* pOIProtSink, IInternetBindInfo* pOIBindInfo,
580 DWORD grfPI, HANDLE_PTR dwReserved)
582 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
583 DWORD grfBINDF = 0, len;
584 BINDINFO bindinfo;
585 LPWSTR url_dll, url_file, url, mime, res_type = (LPWSTR)RT_HTML;
586 HMODULE hdll;
587 HRSRC src;
588 HRESULT hres;
590 static const WCHAR wszRes[] = {'r','e','s',':','/','/'};
592 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
593 pOIBindInfo, grfPI, dwReserved);
595 memset(&bindinfo, 0, sizeof(bindinfo));
596 bindinfo.cbSize = sizeof(BINDINFO);
597 IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
598 ReleaseBindInfo(&bindinfo);
600 len = strlenW(szUrl)+16;
601 url = heap_alloc(len*sizeof(WCHAR));
602 hres = CoInternetParseUrl(szUrl, PARSE_ENCODE, 0, url, len, &len, 0);
603 if(FAILED(hres)) {
604 WARN("CoInternetParseUrl failed: %08x\n", hres);
605 heap_free(url);
606 IInternetProtocolSink_ReportResult(pOIProtSink, hres, 0, NULL);
607 return hres;
610 if(len < sizeof(wszRes)/sizeof(wszRes[0]) || memcmp(url, wszRes, sizeof(wszRes))) {
611 WARN("Wrong protocol of url: %s\n", debugstr_w(url));
612 IInternetProtocolSink_ReportResult(pOIProtSink, E_INVALIDARG, 0, NULL);
613 heap_free(url);
614 return E_INVALIDARG;
617 url_dll = url + sizeof(wszRes)/sizeof(wszRes[0]);
618 if(!(url_file = strrchrW(url_dll, '/'))) {
619 WARN("wrong url: %s\n", debugstr_w(url));
620 IInternetProtocolSink_ReportResult(pOIProtSink, MK_E_SYNTAX, 0, NULL);
621 heap_free(url);
622 return MK_E_SYNTAX;
625 *url_file++ = 0;
626 hdll = LoadLibraryExW(url_dll, NULL, LOAD_LIBRARY_AS_DATAFILE);
627 if(!hdll) {
628 if (!(res_type = strrchrW(url_dll, '/'))) {
629 WARN("Could not open dll: %s\n", debugstr_w(url_dll));
630 IInternetProtocolSink_ReportResult(pOIProtSink, HRESULT_FROM_WIN32(GetLastError()), 0, NULL);
631 heap_free(url);
632 return HRESULT_FROM_WIN32(GetLastError());
634 *res_type++ = 0;
636 hdll = LoadLibraryExW(url_dll, NULL, LOAD_LIBRARY_AS_DATAFILE);
637 if(!hdll) {
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());
645 TRACE("trying to find resource type %s, name %s\n", debugstr_w(res_type), debugstr_w(url_file));
647 src = FindResourceW(hdll, url_file, res_type);
648 if(!src) {
649 LPWSTR endpoint = NULL;
650 DWORD file_id = strtolW(url_file, &endpoint, 10);
651 if(endpoint == url_file+strlenW(url_file))
652 src = FindResourceW(hdll, MAKEINTRESOURCEW(file_id), MAKEINTRESOURCEW(RT_HTML));
654 if(!src) {
655 WARN("Could not find resource\n");
656 IInternetProtocolSink_ReportResult(pOIProtSink,
657 HRESULT_FROM_WIN32(GetLastError()), 0, NULL);
658 heap_free(url);
659 return HRESULT_FROM_WIN32(GetLastError());
663 if(This->data) {
664 WARN("data already loaded\n");
665 heap_free(This->data);
668 This->data_len = SizeofResource(hdll, src);
669 This->data = heap_alloc(This->data_len);
670 memcpy(This->data, LoadResource(hdll, src), This->data_len);
671 This->cur = 0;
673 FreeLibrary(hdll);
675 hres = FindMimeFromData(NULL, url_file, This->data, This->data_len, NULL, 0, &mime, 0);
676 heap_free(url);
677 if(SUCCEEDED(hres)) {
678 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, mime);
679 CoTaskMemFree(mime);
682 IInternetProtocolSink_ReportData(pOIProtSink,
683 BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE,
684 This->data_len, This->data_len);
686 IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
688 return S_OK;
691 static HRESULT WINAPI ResProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA* pProtocolData)
693 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
694 FIXME("(%p)->(%p)\n", This, pProtocolData);
695 return E_NOTIMPL;
698 static HRESULT WINAPI ResProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
699 DWORD dwOptions)
701 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
702 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
703 return E_NOTIMPL;
706 static HRESULT WINAPI ResProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
708 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
710 TRACE("(%p)->(%08x)\n", This, dwOptions);
712 /* test show that we don't have to do anything here */
713 return S_OK;
716 static HRESULT WINAPI ResProtocol_Suspend(IInternetProtocol *iface)
718 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
719 FIXME("(%p)\n", This);
720 return E_NOTIMPL;
723 static HRESULT WINAPI ResProtocol_Resume(IInternetProtocol *iface)
725 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
726 FIXME("(%p)\n", This);
727 return E_NOTIMPL;
730 static HRESULT WINAPI ResProtocol_Read(IInternetProtocol *iface, void* pv, ULONG cb, ULONG* pcbRead)
732 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
734 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
736 if(!This->data)
737 return E_FAIL;
739 *pcbRead = (cb > This->data_len-This->cur ? This->data_len-This->cur : cb);
741 if(!*pcbRead)
742 return S_FALSE;
744 memcpy(pv, This->data+This->cur, *pcbRead);
745 This->cur += *pcbRead;
747 return S_OK;
750 static HRESULT WINAPI ResProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
751 DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
753 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
754 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
755 return E_NOTIMPL;
758 static HRESULT WINAPI ResProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
760 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
762 TRACE("(%p)->(%d)\n", This, dwOptions);
764 /* test show that we don't have to do anything here */
765 return S_OK;
768 static HRESULT WINAPI ResProtocol_UnlockRequest(IInternetProtocol *iface)
770 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
772 TRACE("(%p)\n", This);
774 /* test show that we don't have to do anything here */
775 return S_OK;
778 static const IInternetProtocolVtbl ResProtocolVtbl = {
779 ResProtocol_QueryInterface,
780 ResProtocol_AddRef,
781 ResProtocol_Release,
782 ResProtocol_Start,
783 ResProtocol_Continue,
784 ResProtocol_Abort,
785 ResProtocol_Terminate,
786 ResProtocol_Suspend,
787 ResProtocol_Resume,
788 ResProtocol_Read,
789 ResProtocol_Seek,
790 ResProtocol_LockRequest,
791 ResProtocol_UnlockRequest
794 static HRESULT WINAPI ResProtocolFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
795 REFIID riid, void **ppv)
797 ResProtocol *ret;
798 HRESULT hres = S_OK;
800 TRACE("(%p)->(%p %s %p)\n", iface, pUnkOuter, debugstr_guid(riid), ppv);
802 ret = heap_alloc(sizeof(ResProtocol));
803 ret->IInternetProtocol_iface.lpVtbl = &ResProtocolVtbl;
804 ret->ref = 0;
805 ret->data = NULL;
806 ret->data_len = 0;
807 ret->cur = 0;
808 ret->pUnkOuter = pUnkOuter;
810 if(pUnkOuter) {
811 ret->ref = 1;
812 if(IsEqualGUID(&IID_IUnknown, riid))
813 *ppv = &ret->IInternetProtocol_iface;
814 else
815 hres = E_FAIL;
816 }else {
817 hres = IInternetProtocol_QueryInterface(&ret->IInternetProtocol_iface, riid, ppv);
820 if(FAILED(hres))
821 heap_free(ret);
823 return hres;
826 static HRESULT WINAPI ResProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
827 PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
828 DWORD* pcchResult, DWORD dwReserved)
830 TRACE("%p)->(%s %d %x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), ParseAction,
831 dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
833 if(ParseAction == PARSE_SECURITY_URL) {
834 WCHAR file_part[MAX_PATH], full_path[MAX_PATH];
835 WCHAR *ptr;
836 DWORD size, len;
838 static const WCHAR wszFile[] = {'f','i','l','e',':','/','/'};
839 static const WCHAR wszRes[] = {'r','e','s',':','/','/'};
841 if(strlenW(pwzUrl) <= sizeof(wszRes)/sizeof(WCHAR) || memcmp(pwzUrl, wszRes, sizeof(wszRes)))
842 return E_INVALIDARG;
844 ptr = strchrW(pwzUrl + sizeof(wszRes)/sizeof(WCHAR), '/');
845 if(!ptr)
846 return E_INVALIDARG;
848 len = ptr - (pwzUrl + sizeof(wszRes)/sizeof(WCHAR));
849 if(len >= sizeof(file_part)/sizeof(WCHAR)) {
850 FIXME("Too long URL\n");
851 return MK_E_SYNTAX;
854 memcpy(file_part, pwzUrl + sizeof(wszRes)/sizeof(WCHAR), len*sizeof(WCHAR));
855 file_part[len] = 0;
857 len = SearchPathW(NULL, file_part, NULL, sizeof(full_path)/sizeof(WCHAR), full_path, NULL);
858 if(!len) {
859 WARN("Could not find file %s\n", debugstr_w(file_part));
860 return MK_E_SYNTAX;
863 size = sizeof(wszFile)/sizeof(WCHAR) + len + 1;
864 if(pcchResult)
865 *pcchResult = size;
866 if(size >= cchResult)
867 return S_FALSE;
869 memcpy(pwzResult, wszFile, sizeof(wszFile));
870 memcpy(pwzResult + sizeof(wszFile)/sizeof(WCHAR), full_path, (len+1)*sizeof(WCHAR));
871 return S_OK;
874 if(ParseAction == PARSE_DOMAIN) {
875 if(!pcchResult)
876 return E_POINTER;
878 if(pwzUrl)
879 *pcchResult = strlenW(pwzUrl)+1;
880 else
881 *pcchResult = 1;
882 return E_FAIL;
885 return INET_E_DEFAULT_ACTION;
888 static HRESULT WINAPI ResProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
889 QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
890 DWORD dwReserved)
892 TRACE("%p)->(%s %08x %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), QueryOption, dwQueryFlags, pBuffer,
893 cbBuffer, pcbBuf, dwReserved);
895 switch(QueryOption) {
896 case QUERY_USES_NETWORK:
897 if(!pBuffer || cbBuffer < sizeof(DWORD))
898 return E_FAIL;
900 *(DWORD*)pBuffer = 0;
901 if(pcbBuf)
902 *pcbBuf = sizeof(DWORD);
903 break;
905 case QUERY_IS_SECURE:
906 FIXME("not supporte QUERY_IS_SECURE\n");
907 return E_NOTIMPL;
908 case QUERY_IS_SAFE:
909 FIXME("not supporte QUERY_IS_SAFE\n");
910 return E_NOTIMPL;
911 default:
912 return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
915 return S_OK;
918 static const IInternetProtocolInfoVtbl ResProtocolInfoVtbl = {
919 InternetProtocolInfo_QueryInterface,
920 InternetProtocolInfo_AddRef,
921 InternetProtocolInfo_Release,
922 ResProtocolInfo_ParseUrl,
923 InternetProtocolInfo_CombineUrl,
924 InternetProtocolInfo_CompareUrl,
925 ResProtocolInfo_QueryInfo
928 static const IClassFactoryVtbl ResProtocolFactoryVtbl = {
929 ClassFactory_QueryInterface,
930 ClassFactory_AddRef,
931 ClassFactory_Release,
932 ResProtocolFactory_CreateInstance,
933 ClassFactory_LockServer
936 static ProtocolFactory ResProtocolFactory = {
937 { &ResProtocolInfoVtbl },
938 { &ResProtocolFactoryVtbl }
941 /********************************************************************
942 * JSProtocol implementation
945 static HRESULT WINAPI JSProtocolFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
946 REFIID riid, void **ppv)
948 FIXME("(%p)->(%p %s %p)\n", iface, pUnkOuter, debugstr_guid(riid), ppv);
949 return E_NOTIMPL;
952 static HRESULT WINAPI JSProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
953 PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
954 DWORD* pcchResult, DWORD dwReserved)
956 TRACE("%p)->(%s %d %x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), ParseAction,
957 dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
959 switch(ParseAction) {
960 case PARSE_SECURITY_URL:
961 FIXME("PARSE_SECURITY_URL\n");
962 return E_NOTIMPL;
963 case PARSE_DOMAIN:
964 FIXME("PARSE_DOMAIN\n");
965 return E_NOTIMPL;
966 default:
967 return INET_E_DEFAULT_ACTION;
970 return S_OK;
973 static HRESULT WINAPI JSProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
974 QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
975 DWORD dwReserved)
977 TRACE("%p)->(%s %08x %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), QueryOption, dwQueryFlags, pBuffer,
978 cbBuffer, pcbBuf, dwReserved);
980 switch(QueryOption) {
981 case QUERY_USES_NETWORK:
982 if(!pBuffer || cbBuffer < sizeof(DWORD))
983 return E_FAIL;
985 *(DWORD*)pBuffer = 0;
986 if(pcbBuf)
987 *pcbBuf = sizeof(DWORD);
988 break;
990 case QUERY_IS_SECURE:
991 FIXME("not supporte QUERY_IS_SECURE\n");
992 return E_NOTIMPL;
994 default:
995 return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
998 return S_OK;
1001 static const IInternetProtocolInfoVtbl JSProtocolInfoVtbl = {
1002 InternetProtocolInfo_QueryInterface,
1003 InternetProtocolInfo_AddRef,
1004 InternetProtocolInfo_Release,
1005 JSProtocolInfo_ParseUrl,
1006 InternetProtocolInfo_CombineUrl,
1007 InternetProtocolInfo_CompareUrl,
1008 JSProtocolInfo_QueryInfo
1011 static const IClassFactoryVtbl JSProtocolFactoryVtbl = {
1012 ClassFactory_QueryInterface,
1013 ClassFactory_AddRef,
1014 ClassFactory_Release,
1015 JSProtocolFactory_CreateInstance,
1016 ClassFactory_LockServer
1019 static ProtocolFactory JSProtocolFactory = {
1020 { &JSProtocolInfoVtbl },
1021 { &JSProtocolFactoryVtbl }
1024 HRESULT ProtocolFactory_Create(REFCLSID rclsid, REFIID riid, void **ppv)
1026 ProtocolFactory *cf = NULL;
1028 if(IsEqualGUID(&CLSID_AboutProtocol, rclsid))
1029 cf = &AboutProtocolFactory;
1030 else if(IsEqualGUID(&CLSID_ResProtocol, rclsid))
1031 cf = &ResProtocolFactory;
1032 else if(IsEqualGUID(&CLSID_JSProtocol, rclsid))
1033 cf = &JSProtocolFactory;
1035 if(!cf) {
1036 FIXME("not implemented protocol %s\n", debugstr_guid(rclsid));
1037 return CLASS_E_CLASSNOTAVAILABLE;
1040 return IInternetProtocolInfo_QueryInterface(&cf->IInternetProtocolInfo_iface, riid, ppv);