[CMAKE]
[reactos.git] / dll / win32 / urlmon / file.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 "urlmon_main.h"
20 #include "winreg.h"
21 #include "shlwapi.h"
22
23 #include "wine/debug.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
26
27 typedef struct {
28 const IInternetProtocolExVtbl *lpIInternetProtocolExVtbl;
29 const IInternetPriorityVtbl *lpInternetPriorityVtbl;
30
31 HANDLE file;
32 ULONG size;
33 LONG priority;
34
35 LONG ref;
36 } FileProtocol;
37
38 #define PRIORITY(x) ((IInternetPriority*) &(x)->lpInternetPriorityVtbl)
39 #define PROTOCOLEX(x) ((IInternetProtocolEx*) &(x)->lpIInternetProtocolExVtbl)
40
41 #define PROTOCOL_THIS(iface) DEFINE_THIS(FileProtocol, IInternetProtocolEx, iface)
42
43 static HRESULT WINAPI FileProtocol_QueryInterface(IInternetProtocolEx *iface, REFIID riid, void **ppv)
44 {
45 FileProtocol *This = PROTOCOL_THIS(iface);
46
47 *ppv = NULL;
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);
63 }
64
65 if(*ppv) {
66 IInternetProtocol_AddRef(iface);
67 return S_OK;
68 }
69
70 WARN("not supported interface %s\n", debugstr_guid(riid));
71 return E_NOINTERFACE;
72 }
73
74 static ULONG WINAPI FileProtocol_AddRef(IInternetProtocolEx *iface)
75 {
76 FileProtocol *This = PROTOCOL_THIS(iface);
77 LONG ref = InterlockedIncrement(&This->ref);
78 TRACE("(%p) ref=%d\n", This, ref);
79 return ref;
80 }
81
82 static ULONG WINAPI FileProtocol_Release(IInternetProtocolEx *iface)
83 {
84 FileProtocol *This = PROTOCOL_THIS(iface);
85 LONG ref = InterlockedDecrement(&This->ref);
86
87 TRACE("(%p) ref=%d\n", This, ref);
88
89 if(!ref) {
90 if(This->file != INVALID_HANDLE_VALUE)
91 CloseHandle(This->file);
92 heap_free(This);
93
94 URLMON_UnlockModule();
95 }
96
97 return ref;
98 }
99
100 static HRESULT WINAPI FileProtocol_Start(IInternetProtocolEx *iface, LPCWSTR szUrl,
101 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
102 DWORD grfPI, HANDLE_PTR dwReserved)
103 {
104 FileProtocol *This = PROTOCOL_THIS(iface);
105 IUri *uri;
106 HRESULT hres;
107
108 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
109 pOIBindInfo, grfPI, dwReserved);
110
111 hres = CreateUri(szUrl, Uri_CREATE_FILE_USE_DOS_PATH, 0, &uri);
112 if(FAILED(hres))
113 return hres;
114
115 hres = IInternetProtocolEx_StartEx(PROTOCOLEX(This), uri, pOIProtSink, pOIBindInfo,
116 grfPI, (HANDLE*)dwReserved);
117
118 IUri_Release(uri);
119 return hres;
120 }
121
122 static HRESULT WINAPI FileProtocol_Continue(IInternetProtocolEx *iface, PROTOCOLDATA *pProtocolData)
123 {
124 FileProtocol *This = PROTOCOL_THIS(iface);
125 FIXME("(%p)->(%p)\n", This, pProtocolData);
126 return E_NOTIMPL;
127 }
128
129 static HRESULT WINAPI FileProtocol_Abort(IInternetProtocolEx *iface, HRESULT hrReason,
130 DWORD dwOptions)
131 {
132 FileProtocol *This = PROTOCOL_THIS(iface);
133 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
134 return E_NOTIMPL;
135 }
136
137 static HRESULT WINAPI FileProtocol_Terminate(IInternetProtocolEx *iface, DWORD dwOptions)
138 {
139 FileProtocol *This = PROTOCOL_THIS(iface);
140
141 TRACE("(%p)->(%08x)\n", This, dwOptions);
142
143 return S_OK;
144 }
145
146 static HRESULT WINAPI FileProtocol_Suspend(IInternetProtocolEx *iface)
147 {
148 FileProtocol *This = PROTOCOL_THIS(iface);
149 FIXME("(%p)\n", This);
150 return E_NOTIMPL;
151 }
152
153 static HRESULT WINAPI FileProtocol_Resume(IInternetProtocolEx *iface)
154 {
155 FileProtocol *This = PROTOCOL_THIS(iface);
156 FIXME("(%p)\n", This);
157 return E_NOTIMPL;
158 }
159
160 static HRESULT WINAPI FileProtocol_Read(IInternetProtocolEx *iface, void *pv,
161 ULONG cb, ULONG *pcbRead)
162 {
163 FileProtocol *This = PROTOCOL_THIS(iface);
164 DWORD read = 0;
165
166 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
167
168 if (pcbRead)
169 *pcbRead = 0;
170
171 if(This->file == INVALID_HANDLE_VALUE)
172 return INET_E_DATA_NOT_AVAILABLE;
173
174 if (!ReadFile(This->file, pv, cb, &read, NULL))
175 return INET_E_DOWNLOAD_FAILURE;
176
177 if(pcbRead)
178 *pcbRead = read;
179
180 return cb == read ? S_OK : S_FALSE;
181 }
182
183 static HRESULT WINAPI FileProtocol_Seek(IInternetProtocolEx *iface, LARGE_INTEGER dlibMove,
184 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
185 {
186 FileProtocol *This = PROTOCOL_THIS(iface);
187 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
188 return E_NOTIMPL;
189 }
190
191 static HRESULT WINAPI FileProtocol_LockRequest(IInternetProtocolEx *iface, DWORD dwOptions)
192 {
193 FileProtocol *This = PROTOCOL_THIS(iface);
194
195 TRACE("(%p)->(%08x)\n", This, dwOptions);
196
197 return S_OK;
198 }
199
200 static HRESULT WINAPI FileProtocol_UnlockRequest(IInternetProtocolEx *iface)
201 {
202 FileProtocol *This = PROTOCOL_THIS(iface);
203
204 TRACE("(%p)\n", This);
205
206 return S_OK;
207 }
208
209 static inline HRESULT report_result(IInternetProtocolSink *protocol_sink, HRESULT hres, DWORD res)
210 {
211 IInternetProtocolSink_ReportResult(protocol_sink, hres, res, NULL);
212 return hres;
213 }
214
215 static HRESULT open_file(FileProtocol *This, const WCHAR *path, IInternetProtocolSink *protocol_sink)
216 {
217 LARGE_INTEGER size;
218 HANDLE file;
219
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());
224
225 if(!GetFileSizeEx(file, &size)) {
226 CloseHandle(file);
227 return report_result(protocol_sink, INET_E_RESOURCE_NOT_FOUND, GetLastError());
228 }
229
230 This->file = file;
231 This->size = size.u.LowPart;
232
233 IInternetProtocolSink_ReportProgress(protocol_sink,
234 BINDSTATUS_CACHEFILENAMEAVAILABLE, path);
235 return S_OK;
236 }
237
238 static HRESULT WINAPI FileProtocol_StartEx(IInternetProtocolEx *iface, IUri *pUri,
239 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
240 DWORD grfPI, HANDLE *dwReserved)
241 {
242 FileProtocol *This = PROTOCOL_THIS(iface);
243 BINDINFO bindinfo;
244 DWORD grfBINDF = 0;
245 DWORD scheme;
246 LPWSTR mime = NULL;
247 WCHAR null_char = 0;
248 BSTR path, url;
249 HRESULT hres;
250
251 TRACE("(%p)->(%p %p %p %08x %p)\n", This, pUri, pOIProtSink,
252 pOIBindInfo, grfPI, dwReserved);
253
254 if(!pUri)
255 return E_INVALIDARG;
256
257 scheme = 0;
258 hres = IUri_GetScheme(pUri, &scheme);
259 if(FAILED(hres))
260 return hres;
261 if(scheme != URL_SCHEME_FILE)
262 return E_INVALIDARG;
263
264 memset(&bindinfo, 0, sizeof(bindinfo));
265 bindinfo.cbSize = sizeof(BINDINFO);
266 hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
267 if(FAILED(hres)) {
268 WARN("GetBindInfo failed: %08x\n", hres);
269 return hres;
270 }
271
272 ReleaseBindInfo(&bindinfo);
273
274 if(!(grfBINDF & BINDF_FROMURLMON))
275 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_DIRECTBIND, NULL);
276
277 if(This->file != INVALID_HANDLE_VALUE) {
278 IInternetProtocolSink_ReportData(pOIProtSink,
279 BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION,
280 This->size, This->size);
281 return S_OK;
282 }
283
284 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST, &null_char);
285
286 hres = IUri_GetPath(pUri, &path);
287 if(FAILED(hres)) {
288 ERR("GetPath failed: %08x\n", hres);
289 return report_result(pOIProtSink, hres, 0);
290 }
291
292 hres = open_file(This, path, pOIProtSink);
293 SysFreeString(path);
294 if(FAILED(hres))
295 return hres;
296
297 hres = IUri_GetDisplayUri(pUri, &url);
298 if(hres == S_OK) {
299 hres = FindMimeFromData(NULL, url, NULL, 0, NULL, 0, &mime, 0);
300 SysFreeString(url);
301 if(SUCCEEDED(hres)) {
302 IInternetProtocolSink_ReportProgress(pOIProtSink,
303 (grfBINDF & BINDF_FROMURLMON) ?
304 BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE : BINDSTATUS_MIMETYPEAVAILABLE,
305 mime);
306 CoTaskMemFree(mime);
307 }
308 }
309
310 IInternetProtocolSink_ReportData(pOIProtSink,
311 BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION,
312 This->size, This->size);
313
314 return report_result(pOIProtSink, S_OK, 0);
315 }
316
317 #undef PROTOCOL_THIS
318
319 static const IInternetProtocolExVtbl FileProtocolExVtbl = {
320 FileProtocol_QueryInterface,
321 FileProtocol_AddRef,
322 FileProtocol_Release,
323 FileProtocol_Start,
324 FileProtocol_Continue,
325 FileProtocol_Abort,
326 FileProtocol_Terminate,
327 FileProtocol_Suspend,
328 FileProtocol_Resume,
329 FileProtocol_Read,
330 FileProtocol_Seek,
331 FileProtocol_LockRequest,
332 FileProtocol_UnlockRequest,
333 FileProtocol_StartEx
334 };
335
336 #define PRIORITY_THIS(iface) DEFINE_THIS(FileProtocol, InternetPriority, iface)
337
338 static HRESULT WINAPI FilePriority_QueryInterface(IInternetPriority *iface,
339 REFIID riid, void **ppv)
340 {
341 FileProtocol *This = PRIORITY_THIS(iface);
342 return IInternetProtocolEx_QueryInterface(PROTOCOLEX(This), riid, ppv);
343 }
344
345 static ULONG WINAPI FilePriority_AddRef(IInternetPriority *iface)
346 {
347 FileProtocol *This = PRIORITY_THIS(iface);
348 return IInternetProtocolEx_AddRef(PROTOCOLEX(This));
349 }
350
351 static ULONG WINAPI FilePriority_Release(IInternetPriority *iface)
352 {
353 FileProtocol *This = PRIORITY_THIS(iface);
354 return IInternetProtocolEx_Release(PROTOCOLEX(This));
355 }
356
357 static HRESULT WINAPI FilePriority_SetPriority(IInternetPriority *iface, LONG nPriority)
358 {
359 FileProtocol *This = PRIORITY_THIS(iface);
360
361 TRACE("(%p)->(%d)\n", This, nPriority);
362
363 This->priority = nPriority;
364 return S_OK;
365 }
366
367 static HRESULT WINAPI FilePriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
368 {
369 FileProtocol *This = PRIORITY_THIS(iface);
370
371 TRACE("(%p)->(%p)\n", This, pnPriority);
372
373 *pnPriority = This->priority;
374 return S_OK;
375 }
376
377 #undef PRIORITY_THIS
378
379 static const IInternetPriorityVtbl FilePriorityVtbl = {
380 FilePriority_QueryInterface,
381 FilePriority_AddRef,
382 FilePriority_Release,
383 FilePriority_SetPriority,
384 FilePriority_GetPriority
385 };
386
387 HRESULT FileProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
388 {
389 FileProtocol *ret;
390
391 TRACE("(%p %p)\n", pUnkOuter, ppobj);
392
393 URLMON_LockModule();
394
395 ret = heap_alloc(sizeof(FileProtocol));
396
397 ret->lpIInternetProtocolExVtbl = &FileProtocolExVtbl;
398 ret->lpInternetPriorityVtbl = &FilePriorityVtbl;
399 ret->file = INVALID_HANDLE_VALUE;
400 ret->priority = 0;
401 ret->ref = 1;
402
403 *ppobj = PROTOCOLEX(ret);
404 return S_OK;
405 }