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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "urlmon_main.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
35 const IInternetProtocolVtbl *lpInternetProtocolVtbl;
36 const IInternetPriorityVtbl *lpInternetPriorityVtbl;
44 #define PROTOCOL(x) ((IInternetProtocol*) &(x)->lpInternetProtocolVtbl)
45 #define PRIORITY(x) ((IInternetPriority*) &(x)->lpInternetPriorityVtbl)
47 #define PROTOCOL_THIS(iface) DEFINE_THIS(FileProtocol, InternetProtocol, iface)
49 static HRESULT WINAPI FileProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
51 FileProtocol *This = PROTOCOL_THIS(iface);
54 if(IsEqualGUID(&IID_IUnknown, riid)) {
55 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
56 *ppv = PROTOCOL(This);
57 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
58 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
59 *ppv = PROTOCOL(This);
60 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
61 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
62 *ppv = PROTOCOL(This);
63 }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
64 TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
65 *ppv = PRIORITY(This);
69 IInternetProtocol_AddRef(iface);
73 WARN("not supported interface %s\n", debugstr_guid(riid));
77 static ULONG WINAPI FileProtocol_AddRef(IInternetProtocol *iface)
79 FileProtocol *This = PROTOCOL_THIS(iface);
80 LONG ref = InterlockedIncrement(&This->ref);
81 TRACE("(%p) ref=%ld\n", This, ref);
85 static ULONG WINAPI FileProtocol_Release(IInternetProtocol *iface)
87 FileProtocol *This = PROTOCOL_THIS(iface);
88 LONG ref = InterlockedDecrement(&This->ref);
90 TRACE("(%p) ref=%ld\n", This, ref);
94 CloseHandle(This->file);
95 HeapFree(GetProcessHeap(), 0, This);
97 URLMON_UnlockModule();
103 static HRESULT WINAPI FileProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
104 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
105 DWORD grfPI, DWORD dwReserved)
107 FileProtocol *This = PROTOCOL_THIS(iface);
112 LPWSTR url, mime = NULL;
116 static const WCHAR wszFile[] = {'f','i','l','e',':'};
118 TRACE("(%p)->(%s %p %p %08lx %ld)\n", This, debugstr_w(szUrl), pOIProtSink,
119 pOIBindInfo, grfPI, dwReserved);
121 memset(&bindinfo, 0, sizeof(bindinfo));
122 bindinfo.cbSize = sizeof(BINDINFO);
123 IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
125 if(lstrlenW(szUrl) < sizeof(wszFile)/sizeof(WCHAR)
126 || memcmp(szUrl, wszFile, sizeof(wszFile)))
129 len = lstrlenW(szUrl)+16;
130 url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
131 hres = CoInternetParseUrl(szUrl, PARSE_ENCODE, 0, url, len, &len, 0);
133 HeapFree(GetProcessHeap(), 0, url);
137 hres = FindMimeFromData(NULL, url, NULL, 0, NULL, 0, &mime, 0);
139 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_DIRECTBIND, mime);
142 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST, &null_char);
144 This->file = CreateFileW(url+sizeof(wszFile)/sizeof(WCHAR), GENERIC_READ, FILE_SHARE_READ,
145 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
147 if(This->file == INVALID_HANDLE_VALUE) {
149 IInternetProtocolSink_ReportResult(pOIProtSink, INET_E_RESOURCE_NOT_FOUND,
150 GetLastError(), NULL);
151 HeapFree(GetProcessHeap(), 0, url);
153 return INET_E_RESOURCE_NOT_FOUND;
156 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_CACHEFILENAMEAVAILABLE,
157 url+sizeof(wszFile)/sizeof(WCHAR));
159 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, mime);
160 IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
164 HeapFree(GetProcessHeap(), 0, url);
166 if(GetFileSizeEx(This->file, &size))
167 IInternetProtocolSink_ReportData(pOIProtSink,
168 BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION,
169 size.u.LowPart, size.u.LowPart);
174 static HRESULT WINAPI FileProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
176 FileProtocol *This = PROTOCOL_THIS(iface);
177 FIXME("(%p)->(%p)\n", This, pProtocolData);
181 static HRESULT WINAPI FileProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
184 FileProtocol *This = PROTOCOL_THIS(iface);
185 FIXME("(%p)->(%08lx %08lx)\n", This, hrReason, dwOptions);
189 static HRESULT WINAPI FileProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
191 FileProtocol *This = PROTOCOL_THIS(iface);
193 TRACE("(%p)->(%08lx)\n", This, dwOptions);
198 static HRESULT WINAPI FileProtocol_Suspend(IInternetProtocol *iface)
200 FileProtocol *This = PROTOCOL_THIS(iface);
201 FIXME("(%p)\n", This);
205 static HRESULT WINAPI FileProtocol_Resume(IInternetProtocol *iface)
207 FileProtocol *This = PROTOCOL_THIS(iface);
208 FIXME("(%p)\n", This);
212 static HRESULT WINAPI FileProtocol_Read(IInternetProtocol *iface, void *pv,
213 ULONG cb, ULONG *pcbRead)
215 FileProtocol *This = PROTOCOL_THIS(iface);
218 TRACE("(%p)->(%p %lu %p)\n", This, pv, cb, pcbRead);
221 return INET_E_DATA_NOT_AVAILABLE;
223 ReadFile(This->file, pv, cb, &read, NULL);
228 return cb == read ? S_OK : S_FALSE;
231 static HRESULT WINAPI FileProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
232 DWORD dwOrgin, ULARGE_INTEGER *plibNewPosition)
234 FileProtocol *This = PROTOCOL_THIS(iface);
235 FIXME("(%p)->(%ld %ld %p)\n", This, dlibMove.u.LowPart, dwOrgin, plibNewPosition);
239 static HRESULT WINAPI FileProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
241 FileProtocol *This = PROTOCOL_THIS(iface);
243 TRACE("(%p)->(%08lx)\n", This, dwOptions);
248 static HRESULT WINAPI FileProtocol_UnlockRequest(IInternetProtocol *iface)
250 FileProtocol *This = PROTOCOL_THIS(iface);
252 TRACE("(%p)\n", This);
259 static const IInternetProtocolVtbl FileProtocolVtbl = {
260 FileProtocol_QueryInterface,
262 FileProtocol_Release,
264 FileProtocol_Continue,
266 FileProtocol_Terminate,
267 FileProtocol_Suspend,
271 FileProtocol_LockRequest,
272 FileProtocol_UnlockRequest
275 #define PRIORITY_THIS(iface) DEFINE_THIS(FileProtocol, InternetPriority, iface)
277 static HRESULT WINAPI FilePriority_QueryInterface(IInternetPriority *iface,
278 REFIID riid, void **ppv)
280 FileProtocol *This = PRIORITY_THIS(iface);
281 return IInternetProtocol_QueryInterface(PROTOCOL(This), riid, ppv);
284 static ULONG WINAPI FilePriority_AddRef(IInternetPriority *iface)
286 FileProtocol *This = PRIORITY_THIS(iface);
287 return IInternetProtocol_AddRef(PROTOCOL(This));
290 static ULONG WINAPI FilePriority_Release(IInternetPriority *iface)
292 FileProtocol *This = PRIORITY_THIS(iface);
293 return IInternetProtocol_Release(PROTOCOL(This));
296 static HRESULT WINAPI FilePriority_SetPriority(IInternetPriority *iface, LONG nPriority)
298 FileProtocol *This = PRIORITY_THIS(iface);
300 TRACE("(%p)->(%ld)\n", This, nPriority);
302 This->priority = nPriority;
306 static HRESULT WINAPI FilePriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
308 FileProtocol *This = PRIORITY_THIS(iface);
310 TRACE("(%p)->(%p)\n", This, pnPriority);
312 *pnPriority = This->priority;
318 static const IInternetPriorityVtbl FilePriorityVtbl = {
319 FilePriority_QueryInterface,
321 FilePriority_Release,
322 FilePriority_SetPriority,
323 FilePriority_GetPriority
326 HRESULT FileProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
330 TRACE("(%p %p)\n", pUnkOuter, ppobj);
334 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(FileProtocol));
336 ret->lpInternetProtocolVtbl = &FileProtocolVtbl;
337 ret->lpInternetPriorityVtbl = &FilePriorityVtbl;
342 *ppobj = PROTOCOL(ret);