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 "urlmon_main.h"
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
28 const IInternetProtocolExVtbl *lpIInternetProtocolExVtbl;
29 const IInternetPriorityVtbl *lpInternetPriorityVtbl;
38 #define PRIORITY(x) ((IInternetPriority*) &(x)->lpInternetPriorityVtbl)
39 #define PROTOCOLEX(x) ((IInternetProtocolEx*) &(x)->lpIInternetProtocolExVtbl)
41 #define PROTOCOL_THIS(iface) DEFINE_THIS(FileProtocol, IInternetProtocolEx, iface)
43 static HRESULT WINAPI FileProtocol_QueryInterface(IInternetProtocolEx *iface, REFIID riid, void **ppv)
45 FileProtocol *This = PROTOCOL_THIS(iface);
48 if(IsEqualGUID(&IID_IUnknown, riid)) {
49 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
50 *ppv = PROTOCOLEX(This);
51 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
52 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
53 *ppv = PROTOCOLEX(This);
54 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
55 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
56 *ppv = PROTOCOLEX(This);
57 }else if(IsEqualGUID(&IID_IInternetProtocolEx, riid)) {
58 TRACE("(%p)->(IID_IInternetProtocolEx %p)\n", This, ppv);
59 *ppv = PROTOCOLEX(This);
60 }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
61 TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
62 *ppv = PRIORITY(This);
66 IInternetProtocol_AddRef(iface);
70 WARN("not supported interface %s\n", debugstr_guid(riid));
74 static ULONG WINAPI FileProtocol_AddRef(IInternetProtocolEx *iface)
76 FileProtocol *This = PROTOCOL_THIS(iface);
77 LONG ref = InterlockedIncrement(&This->ref);
78 TRACE("(%p) ref=%d\n", This, ref);
82 static ULONG WINAPI FileProtocol_Release(IInternetProtocolEx *iface)
84 FileProtocol *This = PROTOCOL_THIS(iface);
85 LONG ref = InterlockedDecrement(&This->ref);
87 TRACE("(%p) ref=%d\n", This, ref);
90 if(This->file != INVALID_HANDLE_VALUE)
91 CloseHandle(This->file);
94 URLMON_UnlockModule();
100 static HRESULT WINAPI FileProtocol_Start(IInternetProtocolEx *iface, LPCWSTR szUrl,
101 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
102 DWORD grfPI, HANDLE_PTR dwReserved)
104 FileProtocol *This = PROTOCOL_THIS(iface);
108 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
109 pOIBindInfo, grfPI, dwReserved);
111 hres = CreateUri(szUrl, Uri_CREATE_FILE_USE_DOS_PATH, 0, &uri);
115 hres = IInternetProtocolEx_StartEx(PROTOCOLEX(This), uri, pOIProtSink, pOIBindInfo,
116 grfPI, (HANDLE*)dwReserved);
122 static HRESULT WINAPI FileProtocol_Continue(IInternetProtocolEx *iface, PROTOCOLDATA *pProtocolData)
124 FileProtocol *This = PROTOCOL_THIS(iface);
125 FIXME("(%p)->(%p)\n", This, pProtocolData);
129 static HRESULT WINAPI FileProtocol_Abort(IInternetProtocolEx *iface, HRESULT hrReason,
132 FileProtocol *This = PROTOCOL_THIS(iface);
133 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
137 static HRESULT WINAPI FileProtocol_Terminate(IInternetProtocolEx *iface, DWORD dwOptions)
139 FileProtocol *This = PROTOCOL_THIS(iface);
141 TRACE("(%p)->(%08x)\n", This, dwOptions);
146 static HRESULT WINAPI FileProtocol_Suspend(IInternetProtocolEx *iface)
148 FileProtocol *This = PROTOCOL_THIS(iface);
149 FIXME("(%p)\n", This);
153 static HRESULT WINAPI FileProtocol_Resume(IInternetProtocolEx *iface)
155 FileProtocol *This = PROTOCOL_THIS(iface);
156 FIXME("(%p)\n", This);
160 static HRESULT WINAPI FileProtocol_Read(IInternetProtocolEx *iface, void *pv,
161 ULONG cb, ULONG *pcbRead)
163 FileProtocol *This = PROTOCOL_THIS(iface);
166 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
171 if(This->file == INVALID_HANDLE_VALUE)
172 return INET_E_DATA_NOT_AVAILABLE;
174 if (!ReadFile(This->file, pv, cb, &read, NULL))
175 return INET_E_DOWNLOAD_FAILURE;
180 return cb == read ? S_OK : S_FALSE;
183 static HRESULT WINAPI FileProtocol_Seek(IInternetProtocolEx *iface, LARGE_INTEGER dlibMove,
184 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
186 FileProtocol *This = PROTOCOL_THIS(iface);
187 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
191 static HRESULT WINAPI FileProtocol_LockRequest(IInternetProtocolEx *iface, DWORD dwOptions)
193 FileProtocol *This = PROTOCOL_THIS(iface);
195 TRACE("(%p)->(%08x)\n", This, dwOptions);
200 static HRESULT WINAPI FileProtocol_UnlockRequest(IInternetProtocolEx *iface)
202 FileProtocol *This = PROTOCOL_THIS(iface);
204 TRACE("(%p)\n", This);
209 static inline HRESULT report_result(IInternetProtocolSink *protocol_sink, HRESULT hres, DWORD res)
211 IInternetProtocolSink_ReportResult(protocol_sink, hres, res, NULL);
215 static HRESULT open_file(FileProtocol *This, const WCHAR *path, IInternetProtocolSink *protocol_sink)
220 file = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL,
221 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
222 if(file == INVALID_HANDLE_VALUE)
223 return report_result(protocol_sink, INET_E_RESOURCE_NOT_FOUND, GetLastError());
225 if(!GetFileSizeEx(file, &size)) {
227 return report_result(protocol_sink, INET_E_RESOURCE_NOT_FOUND, GetLastError());
231 This->size = size.u.LowPart;
233 IInternetProtocolSink_ReportProgress(protocol_sink,
234 BINDSTATUS_CACHEFILENAMEAVAILABLE, path);
238 static HRESULT WINAPI FileProtocol_StartEx(IInternetProtocolEx *iface, IUri *pUri,
239 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
240 DWORD grfPI, HANDLE *dwReserved)
242 FileProtocol *This = PROTOCOL_THIS(iface);
251 TRACE("(%p)->(%p %p %p %08x %p)\n", This, pUri, pOIProtSink,
252 pOIBindInfo, grfPI, dwReserved);
258 hres = IUri_GetScheme(pUri, &scheme);
261 if(scheme != URL_SCHEME_FILE)
264 memset(&bindinfo, 0, sizeof(bindinfo));
265 bindinfo.cbSize = sizeof(BINDINFO);
266 hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
268 WARN("GetBindInfo failed: %08x\n", hres);
272 ReleaseBindInfo(&bindinfo);
274 if(!(grfBINDF & BINDF_FROMURLMON))
275 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_DIRECTBIND, NULL);
277 if(This->file != INVALID_HANDLE_VALUE) {
278 IInternetProtocolSink_ReportData(pOIProtSink,
279 BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION,
280 This->size, This->size);
284 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST, &null_char);
286 hres = IUri_GetPath(pUri, &path);
288 ERR("GetPath failed: %08x\n", hres);
289 return report_result(pOIProtSink, hres, 0);
292 hres = open_file(This, path, pOIProtSink);
297 hres = IUri_GetDisplayUri(pUri, &url);
299 hres = FindMimeFromData(NULL, url, NULL, 0, NULL, 0, &mime, 0);
301 if(SUCCEEDED(hres)) {
302 IInternetProtocolSink_ReportProgress(pOIProtSink,
303 (grfBINDF & BINDF_FROMURLMON) ?
304 BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE : BINDSTATUS_MIMETYPEAVAILABLE,
310 IInternetProtocolSink_ReportData(pOIProtSink,
311 BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION,
312 This->size, This->size);
314 return report_result(pOIProtSink, S_OK, 0);
319 static const IInternetProtocolExVtbl FileProtocolExVtbl = {
320 FileProtocol_QueryInterface,
322 FileProtocol_Release,
324 FileProtocol_Continue,
326 FileProtocol_Terminate,
327 FileProtocol_Suspend,
331 FileProtocol_LockRequest,
332 FileProtocol_UnlockRequest,
336 #define PRIORITY_THIS(iface) DEFINE_THIS(FileProtocol, InternetPriority, iface)
338 static HRESULT WINAPI FilePriority_QueryInterface(IInternetPriority *iface,
339 REFIID riid, void **ppv)
341 FileProtocol *This = PRIORITY_THIS(iface);
342 return IInternetProtocolEx_QueryInterface(PROTOCOLEX(This), riid, ppv);
345 static ULONG WINAPI FilePriority_AddRef(IInternetPriority *iface)
347 FileProtocol *This = PRIORITY_THIS(iface);
348 return IInternetProtocolEx_AddRef(PROTOCOLEX(This));
351 static ULONG WINAPI FilePriority_Release(IInternetPriority *iface)
353 FileProtocol *This = PRIORITY_THIS(iface);
354 return IInternetProtocolEx_Release(PROTOCOLEX(This));
357 static HRESULT WINAPI FilePriority_SetPriority(IInternetPriority *iface, LONG nPriority)
359 FileProtocol *This = PRIORITY_THIS(iface);
361 TRACE("(%p)->(%d)\n", This, nPriority);
363 This->priority = nPriority;
367 static HRESULT WINAPI FilePriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
369 FileProtocol *This = PRIORITY_THIS(iface);
371 TRACE("(%p)->(%p)\n", This, pnPriority);
373 *pnPriority = This->priority;
379 static const IInternetPriorityVtbl FilePriorityVtbl = {
380 FilePriority_QueryInterface,
382 FilePriority_Release,
383 FilePriority_SetPriority,
384 FilePriority_GetPriority
387 HRESULT FileProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
391 TRACE("(%p %p)\n", pUnkOuter, ppobj);
395 ret = heap_alloc(sizeof(FileProtocol));
397 ret->lpIInternetProtocolExVtbl = &FileProtocolExVtbl;
398 ret->lpInternetPriorityVtbl = &FilePriorityVtbl;
399 ret->file = INVALID_HANDLE_VALUE;
403 *ppobj = PROTOCOLEX(ret);