rpcrt4: Free the memory for a registered interface that is in use and when WaitForCal...
[wine.git] / dlls / urlmon / file.c
blobdda1dd0f811ccec1dfd333cbfdc418bdccdcc62e
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 "urlmon_main.h"
20 #include "wine/debug.h"
22 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
24 typedef struct {
25 const IInternetProtocolVtbl *lpInternetProtocolVtbl;
26 const IInternetPriorityVtbl *lpInternetPriorityVtbl;
28 HANDLE file;
29 LONG priority;
31 LONG ref;
32 } FileProtocol;
34 #define PROTOCOL(x) ((IInternetProtocol*) &(x)->lpInternetProtocolVtbl)
35 #define PRIORITY(x) ((IInternetPriority*) &(x)->lpInternetPriorityVtbl)
37 #define PROTOCOL_THIS(iface) DEFINE_THIS(FileProtocol, InternetProtocol, iface)
39 static HRESULT WINAPI FileProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
41 FileProtocol *This = PROTOCOL_THIS(iface);
43 *ppv = NULL;
44 if(IsEqualGUID(&IID_IUnknown, riid)) {
45 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
46 *ppv = PROTOCOL(This);
47 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
48 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
49 *ppv = PROTOCOL(This);
50 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
51 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
52 *ppv = PROTOCOL(This);
53 }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
54 TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
55 *ppv = PRIORITY(This);
58 if(*ppv) {
59 IInternetProtocol_AddRef(iface);
60 return S_OK;
63 WARN("not supported interface %s\n", debugstr_guid(riid));
64 return E_NOINTERFACE;
67 static ULONG WINAPI FileProtocol_AddRef(IInternetProtocol *iface)
69 FileProtocol *This = PROTOCOL_THIS(iface);
70 LONG ref = InterlockedIncrement(&This->ref);
71 TRACE("(%p) ref=%d\n", This, ref);
72 return ref;
75 static ULONG WINAPI FileProtocol_Release(IInternetProtocol *iface)
77 FileProtocol *This = PROTOCOL_THIS(iface);
78 LONG ref = InterlockedDecrement(&This->ref);
80 TRACE("(%p) ref=%d\n", This, ref);
82 if(!ref) {
83 if(This->file)
84 CloseHandle(This->file);
85 heap_free(This);
87 URLMON_UnlockModule();
90 return ref;
93 static HRESULT WINAPI FileProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
94 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
95 DWORD grfPI, DWORD dwReserved)
97 FileProtocol *This = PROTOCOL_THIS(iface);
98 BINDINFO bindinfo;
99 DWORD grfBINDF = 0;
100 LARGE_INTEGER size;
101 DWORD len;
102 LPWSTR url, mime = NULL, file_name;
103 WCHAR null_char = 0;
104 BOOL first_call = FALSE;
105 HRESULT hres;
107 static const WCHAR wszFile[] = {'f','i','l','e',':'};
109 TRACE("(%p)->(%s %p %p %08x %d)\n", This, debugstr_w(szUrl), pOIProtSink,
110 pOIBindInfo, grfPI, dwReserved);
112 if(!szUrl || lstrlenW(szUrl) < sizeof(wszFile)/sizeof(WCHAR)
113 || memcmp(szUrl, wszFile, sizeof(wszFile)))
114 return E_INVALIDARG;
116 memset(&bindinfo, 0, sizeof(bindinfo));
117 bindinfo.cbSize = sizeof(BINDINFO);
118 hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
119 if(FAILED(hres)) {
120 WARN("GetBindInfo failed: %08x\n", hres);
121 return hres;
124 ReleaseBindInfo(&bindinfo);
126 len = lstrlenW(szUrl)+16;
127 url = heap_alloc(len*sizeof(WCHAR));
128 hres = CoInternetParseUrl(szUrl, PARSE_ENCODE, 0, url, len, &len, 0);
129 if(FAILED(hres)) {
130 heap_free(url);
131 return hres;
134 if(!(grfBINDF & BINDF_FROMURLMON))
135 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_DIRECTBIND, NULL);
137 if(!This->file) {
138 WCHAR *ptr;
140 first_call = TRUE;
142 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST, &null_char);
144 file_name = url+sizeof(wszFile)/sizeof(WCHAR);
145 if(file_name[0] == '/' && file_name[1] == '/')
146 file_name += 2;
147 if(*file_name == '/')
148 file_name++;
150 for(ptr = file_name; *ptr; ptr++) {
151 if(*ptr == '?' || *ptr == '#') {
152 *ptr = 0;
153 break;
157 if(file_name[1] == '|')
158 file_name[1] = ':';
160 This->file = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
161 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
163 if(This->file == INVALID_HANDLE_VALUE) {
164 This->file = NULL;
165 IInternetProtocolSink_ReportResult(pOIProtSink, INET_E_RESOURCE_NOT_FOUND,
166 GetLastError(), NULL);
167 heap_free(url);
168 return INET_E_RESOURCE_NOT_FOUND;
171 IInternetProtocolSink_ReportProgress(pOIProtSink,
172 BINDSTATUS_CACHEFILENAMEAVAILABLE, file_name);
174 hres = FindMimeFromData(NULL, url, NULL, 0, NULL, 0, &mime, 0);
175 if(SUCCEEDED(hres)) {
176 IInternetProtocolSink_ReportProgress(pOIProtSink,
177 (grfBINDF & BINDF_FROMURLMON) ?
178 BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE : BINDSTATUS_MIMETYPEAVAILABLE,
179 mime);
180 CoTaskMemFree(mime);
184 heap_free(url);
186 if(GetFileSizeEx(This->file, &size))
187 IInternetProtocolSink_ReportData(pOIProtSink,
188 BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION,
189 size.u.LowPart, size.u.LowPart);
191 if(first_call)
192 IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
194 return S_OK;
197 static HRESULT WINAPI FileProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
199 FileProtocol *This = PROTOCOL_THIS(iface);
200 FIXME("(%p)->(%p)\n", This, pProtocolData);
201 return E_NOTIMPL;
204 static HRESULT WINAPI FileProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
205 DWORD dwOptions)
207 FileProtocol *This = PROTOCOL_THIS(iface);
208 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
209 return E_NOTIMPL;
212 static HRESULT WINAPI FileProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
214 FileProtocol *This = PROTOCOL_THIS(iface);
216 TRACE("(%p)->(%08x)\n", This, dwOptions);
218 return S_OK;
221 static HRESULT WINAPI FileProtocol_Suspend(IInternetProtocol *iface)
223 FileProtocol *This = PROTOCOL_THIS(iface);
224 FIXME("(%p)\n", This);
225 return E_NOTIMPL;
228 static HRESULT WINAPI FileProtocol_Resume(IInternetProtocol *iface)
230 FileProtocol *This = PROTOCOL_THIS(iface);
231 FIXME("(%p)\n", This);
232 return E_NOTIMPL;
235 static HRESULT WINAPI FileProtocol_Read(IInternetProtocol *iface, void *pv,
236 ULONG cb, ULONG *pcbRead)
238 FileProtocol *This = PROTOCOL_THIS(iface);
239 DWORD read = 0;
241 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
243 if (pcbRead)
244 *pcbRead = 0;
246 if(!This->file)
247 return INET_E_DATA_NOT_AVAILABLE;
249 if (!ReadFile(This->file, pv, cb, &read, NULL))
250 return INET_E_DOWNLOAD_FAILURE;
252 if(pcbRead)
253 *pcbRead = read;
255 return cb == read ? S_OK : S_FALSE;
258 static HRESULT WINAPI FileProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
259 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
261 FileProtocol *This = PROTOCOL_THIS(iface);
262 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
263 return E_NOTIMPL;
266 static HRESULT WINAPI FileProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
268 FileProtocol *This = PROTOCOL_THIS(iface);
270 TRACE("(%p)->(%08x)\n", This, dwOptions);
272 return S_OK;
275 static HRESULT WINAPI FileProtocol_UnlockRequest(IInternetProtocol *iface)
277 FileProtocol *This = PROTOCOL_THIS(iface);
279 TRACE("(%p)\n", This);
281 return S_OK;
284 #undef PROTOCOL_THIS
286 static const IInternetProtocolVtbl FileProtocolVtbl = {
287 FileProtocol_QueryInterface,
288 FileProtocol_AddRef,
289 FileProtocol_Release,
290 FileProtocol_Start,
291 FileProtocol_Continue,
292 FileProtocol_Abort,
293 FileProtocol_Terminate,
294 FileProtocol_Suspend,
295 FileProtocol_Resume,
296 FileProtocol_Read,
297 FileProtocol_Seek,
298 FileProtocol_LockRequest,
299 FileProtocol_UnlockRequest
302 #define PRIORITY_THIS(iface) DEFINE_THIS(FileProtocol, InternetPriority, iface)
304 static HRESULT WINAPI FilePriority_QueryInterface(IInternetPriority *iface,
305 REFIID riid, void **ppv)
307 FileProtocol *This = PRIORITY_THIS(iface);
308 return IInternetProtocol_QueryInterface(PROTOCOL(This), riid, ppv);
311 static ULONG WINAPI FilePriority_AddRef(IInternetPriority *iface)
313 FileProtocol *This = PRIORITY_THIS(iface);
314 return IInternetProtocol_AddRef(PROTOCOL(This));
317 static ULONG WINAPI FilePriority_Release(IInternetPriority *iface)
319 FileProtocol *This = PRIORITY_THIS(iface);
320 return IInternetProtocol_Release(PROTOCOL(This));
323 static HRESULT WINAPI FilePriority_SetPriority(IInternetPriority *iface, LONG nPriority)
325 FileProtocol *This = PRIORITY_THIS(iface);
327 TRACE("(%p)->(%d)\n", This, nPriority);
329 This->priority = nPriority;
330 return S_OK;
333 static HRESULT WINAPI FilePriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
335 FileProtocol *This = PRIORITY_THIS(iface);
337 TRACE("(%p)->(%p)\n", This, pnPriority);
339 *pnPriority = This->priority;
340 return S_OK;
343 #undef PRIORITY_THIS
345 static const IInternetPriorityVtbl FilePriorityVtbl = {
346 FilePriority_QueryInterface,
347 FilePriority_AddRef,
348 FilePriority_Release,
349 FilePriority_SetPriority,
350 FilePriority_GetPriority
353 HRESULT FileProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
355 FileProtocol *ret;
357 TRACE("(%p %p)\n", pUnkOuter, ppobj);
359 URLMON_LockModule();
361 ret = heap_alloc(sizeof(FileProtocol));
363 ret->lpInternetProtocolVtbl = &FileProtocolVtbl;
364 ret->lpInternetPriorityVtbl = &FilePriorityVtbl;
365 ret->file = NULL;
366 ret->priority = 0;
367 ret->ref = 1;
369 *ppobj = PROTOCOL(ret);
371 return S_OK;