urlmon/tests: Add tests for calls to IBindStatusCallback_QueryInterface.
[wine] / dlls / 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 <stdarg.h>
20
21 #define COBJMACROS
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
27 #include "urlmon.h"
28 #include "urlmon_main.h"
29
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
33
34 typedef struct {
35     const IInternetProtocolVtbl  *lpInternetProtocolVtbl;
36     const IInternetPriorityVtbl  *lpInternetPriorityVtbl;
37
38     HANDLE file;
39     LONG priority;
40
41     LONG ref;
42 } FileProtocol;
43
44 #define PROTOCOL(x)  ((IInternetProtocol*)  &(x)->lpInternetProtocolVtbl)
45 #define PRIORITY(x)  ((IInternetPriority*)  &(x)->lpInternetPriorityVtbl)
46
47 #define PROTOCOL_THIS(iface) DEFINE_THIS(FileProtocol, InternetProtocol, iface)
48
49 static HRESULT WINAPI FileProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
50 {
51     FileProtocol *This = PROTOCOL_THIS(iface);
52
53     *ppv = NULL;
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);
66     }
67
68     if(*ppv) {
69         IInternetProtocol_AddRef(iface);
70         return S_OK;
71     }
72
73     WARN("not supported interface %s\n", debugstr_guid(riid));
74     return E_NOINTERFACE;
75 }
76
77 static ULONG WINAPI FileProtocol_AddRef(IInternetProtocol *iface)
78 {
79     FileProtocol *This = PROTOCOL_THIS(iface);
80     LONG ref = InterlockedIncrement(&This->ref);
81     TRACE("(%p) ref=%d\n", This, ref);
82     return ref;
83 }
84
85 static ULONG WINAPI FileProtocol_Release(IInternetProtocol *iface)
86 {
87     FileProtocol *This = PROTOCOL_THIS(iface);
88     LONG ref = InterlockedDecrement(&This->ref);
89
90     TRACE("(%p) ref=%d\n", This, ref);
91
92     if(!ref) {
93         if(This->file)
94             CloseHandle(This->file);
95         HeapFree(GetProcessHeap(), 0, This);
96
97         URLMON_UnlockModule();
98     }
99
100     return ref;
101 }
102
103 static HRESULT WINAPI FileProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
104         IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
105         DWORD grfPI, DWORD dwReserved)
106 {
107     FileProtocol *This = PROTOCOL_THIS(iface);
108     BINDINFO bindinfo;
109     DWORD grfBINDF = 0;
110     LARGE_INTEGER size;
111     DWORD len;
112     LPWSTR url, mime = NULL;
113     LPCWSTR file_name;
114     WCHAR null_char = 0;
115     BOOL first_call = FALSE;
116     HRESULT hres;
117
118     static const WCHAR wszFile[]  = {'f','i','l','e',':'};
119
120     TRACE("(%p)->(%s %p %p %08x %d)\n", This, debugstr_w(szUrl), pOIProtSink,
121             pOIBindInfo, grfPI, dwReserved);
122
123     memset(&bindinfo, 0, sizeof(bindinfo));
124     bindinfo.cbSize = sizeof(BINDINFO);
125     hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
126     if(FAILED(hres)) {
127         WARN("GetBindInfo failed: %08x\n", hres);
128         return hres;
129     }
130
131     ReleaseBindInfo(&bindinfo);
132
133     if(lstrlenW(szUrl) < sizeof(wszFile)/sizeof(WCHAR)
134             || memcmp(szUrl, wszFile, sizeof(wszFile)))
135         return MK_E_SYNTAX;
136
137     len = lstrlenW(szUrl)+16;
138     url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
139     hres = CoInternetParseUrl(szUrl, PARSE_ENCODE, 0, url, len, &len, 0);
140     if(FAILED(hres)) {
141         HeapFree(GetProcessHeap(), 0, url);
142         return hres;
143     }
144
145     if(!(grfBINDF & BINDF_FROMURLMON))
146         IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_DIRECTBIND, NULL);
147
148     if(!This->file) {
149         first_call = TRUE;
150
151         IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST, &null_char);
152
153         file_name = url+sizeof(wszFile)/sizeof(WCHAR);
154         if(file_name[0] == '/' && file_name[1] == '/')
155             file_name += 2;
156         if(*file_name == '/')
157             file_name++;
158
159         This->file = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
160                                  OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
161
162         if(This->file == INVALID_HANDLE_VALUE) {
163             This->file = NULL;
164             IInternetProtocolSink_ReportResult(pOIProtSink, INET_E_RESOURCE_NOT_FOUND,
165                     GetLastError(), NULL);
166             HeapFree(GetProcessHeap(), 0, url);
167             return INET_E_RESOURCE_NOT_FOUND;
168         }
169
170         IInternetProtocolSink_ReportProgress(pOIProtSink,
171                 BINDSTATUS_CACHEFILENAMEAVAILABLE, file_name);
172
173         hres = FindMimeFromData(NULL, url, NULL, 0, NULL, 0, &mime, 0);
174         if(SUCCEEDED(hres)) {
175             IInternetProtocolSink_ReportProgress(pOIProtSink,
176                     (grfBINDF & BINDF_FROMURLMON) ?
177                     BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE : BINDSTATUS_MIMETYPEAVAILABLE,
178                     mime);
179             CoTaskMemFree(mime);
180         }
181     }
182
183     HeapFree(GetProcessHeap(), 0, url);
184
185     if(GetFileSizeEx(This->file, &size))
186         IInternetProtocolSink_ReportData(pOIProtSink,
187                 BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION,
188                 size.u.LowPart, size.u.LowPart);
189
190     if(first_call)
191         IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
192
193     return S_OK;
194 }
195
196 static HRESULT WINAPI FileProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
197 {
198     FileProtocol *This = PROTOCOL_THIS(iface);
199     FIXME("(%p)->(%p)\n", This, pProtocolData);
200     return E_NOTIMPL;
201 }
202
203 static HRESULT WINAPI FileProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
204         DWORD dwOptions)
205 {
206     FileProtocol *This = PROTOCOL_THIS(iface);
207     FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
208     return E_NOTIMPL;
209 }
210
211 static HRESULT WINAPI FileProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
212 {
213     FileProtocol *This = PROTOCOL_THIS(iface);
214
215     TRACE("(%p)->(%08x)\n", This, dwOptions);
216
217     return S_OK;
218 }
219
220 static HRESULT WINAPI FileProtocol_Suspend(IInternetProtocol *iface)
221 {
222     FileProtocol *This = PROTOCOL_THIS(iface);
223     FIXME("(%p)\n", This);
224     return E_NOTIMPL;
225 }
226
227 static HRESULT WINAPI FileProtocol_Resume(IInternetProtocol *iface)
228 {
229     FileProtocol *This = PROTOCOL_THIS(iface);
230     FIXME("(%p)\n", This);
231     return E_NOTIMPL;
232 }
233
234 static HRESULT WINAPI FileProtocol_Read(IInternetProtocol *iface, void *pv,
235         ULONG cb, ULONG *pcbRead)
236 {
237     FileProtocol *This = PROTOCOL_THIS(iface);
238     DWORD read = 0;
239
240     TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
241
242     if(!This->file)
243         return INET_E_DATA_NOT_AVAILABLE;
244
245     ReadFile(This->file, pv, cb, &read, NULL);
246
247     if(pcbRead)
248         *pcbRead = read;
249     
250     return cb == read ? S_OK : S_FALSE;
251 }
252
253 static HRESULT WINAPI FileProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
254         DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
255 {
256     FileProtocol *This = PROTOCOL_THIS(iface);
257     FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
258     return E_NOTIMPL;
259 }
260
261 static HRESULT WINAPI FileProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
262 {
263     FileProtocol *This = PROTOCOL_THIS(iface);
264
265     TRACE("(%p)->(%08x)\n", This, dwOptions);
266
267     return S_OK;
268 }
269
270 static HRESULT WINAPI FileProtocol_UnlockRequest(IInternetProtocol *iface)
271 {
272     FileProtocol *This = PROTOCOL_THIS(iface);
273
274     TRACE("(%p)\n", This);
275
276     return S_OK;
277 }
278
279 #undef PROTOCOL_THIS
280
281 static const IInternetProtocolVtbl FileProtocolVtbl = {
282     FileProtocol_QueryInterface,
283     FileProtocol_AddRef,
284     FileProtocol_Release,
285     FileProtocol_Start,
286     FileProtocol_Continue,
287     FileProtocol_Abort,
288     FileProtocol_Terminate,
289     FileProtocol_Suspend,
290     FileProtocol_Resume,
291     FileProtocol_Read,
292     FileProtocol_Seek,
293     FileProtocol_LockRequest,
294     FileProtocol_UnlockRequest
295 };
296
297 #define PRIORITY_THIS(iface) DEFINE_THIS(FileProtocol, InternetPriority, iface)
298
299 static HRESULT WINAPI FilePriority_QueryInterface(IInternetPriority *iface,
300                                                   REFIID riid, void **ppv)
301 {
302     FileProtocol *This = PRIORITY_THIS(iface);
303     return IInternetProtocol_QueryInterface(PROTOCOL(This), riid, ppv);
304 }
305
306 static ULONG WINAPI FilePriority_AddRef(IInternetPriority *iface)
307 {
308     FileProtocol *This = PRIORITY_THIS(iface);
309     return IInternetProtocol_AddRef(PROTOCOL(This));
310 }
311
312 static ULONG WINAPI FilePriority_Release(IInternetPriority *iface)
313 {
314     FileProtocol *This = PRIORITY_THIS(iface);
315     return IInternetProtocol_Release(PROTOCOL(This));
316 }
317
318 static HRESULT WINAPI FilePriority_SetPriority(IInternetPriority *iface, LONG nPriority)
319 {
320     FileProtocol *This = PRIORITY_THIS(iface);
321
322     TRACE("(%p)->(%d)\n", This, nPriority);
323
324     This->priority = nPriority;
325     return S_OK;
326 }
327
328 static HRESULT WINAPI FilePriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
329 {
330     FileProtocol *This = PRIORITY_THIS(iface);
331
332     TRACE("(%p)->(%p)\n", This, pnPriority);
333
334     *pnPriority = This->priority;
335     return S_OK;
336 }
337
338 #undef PRIORITY_THIS
339
340 static const IInternetPriorityVtbl FilePriorityVtbl = {
341     FilePriority_QueryInterface,
342     FilePriority_AddRef,
343     FilePriority_Release,
344     FilePriority_SetPriority,
345     FilePriority_GetPriority
346 };
347
348 HRESULT FileProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
349 {
350     FileProtocol *ret;
351
352     TRACE("(%p %p)\n", pUnkOuter, ppobj);
353
354     URLMON_LockModule();
355
356     ret = HeapAlloc(GetProcessHeap(), 0, sizeof(FileProtocol));
357
358     ret->lpInternetProtocolVtbl = &FileProtocolVtbl;
359     ret->lpInternetPriorityVtbl = &FilePriorityVtbl;
360     ret->file = NULL;
361     ret->priority = 0;
362     ret->ref = 1;
363
364     *ppobj = PROTOCOL(ret);
365     
366     return S_OK;
367 }