Remove redundant check.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
37     HANDLE file;
38
39     LONG ref;
40 } FileProtocol;
41
42 #define PROTOCOL_THIS(iface) DEFINE_THIS(FileProtocol, InternetProtocol, iface)
43
44 #define PROTOCOL(x)  ((IInternetProtocol*)  &(x)->lpInternetProtocolVtbl)
45
46 static HRESULT WINAPI FileProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
47 {
48     FileProtocol *This = PROTOCOL_THIS(iface);
49
50     *ppv = NULL;
51     if(IsEqualGUID(&IID_IUnknown, riid)) {
52         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
53         *ppv = PROTOCOL(This);
54     }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
55         TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
56         *ppv = PROTOCOL(This);
57     }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
58         TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
59         *ppv = PROTOCOL(This);
60     }
61
62     if(*ppv) {
63         IInternetProtocol_AddRef(iface);
64         return S_OK;
65     }
66
67     WARN("not supported interface %s\n", debugstr_guid(riid));
68     return E_NOINTERFACE;
69 }
70
71 static ULONG WINAPI FileProtocol_AddRef(IInternetProtocol *iface)
72 {
73     FileProtocol *This = PROTOCOL_THIS(iface);
74     LONG ref = InterlockedIncrement(&This->ref);
75     TRACE("(%p) ref=%ld\n", This, ref);
76     return ref;
77 }
78
79 static ULONG WINAPI FileProtocol_Release(IInternetProtocol *iface)
80 {
81     FileProtocol *This = PROTOCOL_THIS(iface);
82     LONG ref = InterlockedDecrement(&This->ref);
83
84     TRACE("(%p) ref=%ld\n", This, ref);
85
86     if(!ref) {
87         if(This->file)
88             CloseHandle(This->file);
89         HeapFree(GetProcessHeap(), 0, This);
90
91         URLMON_UnlockModule();
92     }
93
94     return ref;
95 }
96
97 static HRESULT WINAPI FileProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
98         IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
99         DWORD grfPI, DWORD dwReserved)
100 {
101     FileProtocol *This = PROTOCOL_THIS(iface);
102     BINDINFO bindinfo;
103     DWORD grfBINDF = 0;
104     LARGE_INTEGER size;
105
106     static const WCHAR wszFile[]  = {'f','i','l','e',':'};
107
108     TRACE("(%p)->(%s %p %p %08lx %ld)\n", This, debugstr_w(szUrl), pOIProtSink,
109             pOIBindInfo, grfPI, dwReserved);
110
111     memset(&bindinfo, 0, sizeof(bindinfo));
112     bindinfo.cbSize = sizeof(BINDINFO);
113     IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
114
115     if(lstrlenW(szUrl) < sizeof(wszFile)/sizeof(WCHAR)
116             || memcmp(szUrl, wszFile, sizeof(wszFile)))
117         return MK_E_SYNTAX;
118
119     /* FIXME:
120      * Implement MIME type checking
121      */
122
123     if(!This->file) {
124         This->file = CreateFileW(szUrl+sizeof(wszFile)/sizeof(WCHAR), GENERIC_READ, FILE_SHARE_READ,
125                 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
126
127         if(This->file == INVALID_HANDLE_VALUE) {
128             This->file = NULL;
129             IInternetProtocolSink_ReportResult(pOIProtSink, INET_E_RESOURCE_NOT_FOUND,
130                     GetLastError(), NULL);
131             return INET_E_RESOURCE_NOT_FOUND;
132         }
133
134         IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_CACHEFILENAMEAVAILABLE,
135                 szUrl+sizeof(wszFile)/sizeof(WCHAR));
136         IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
137     }
138
139     if(GetFileSizeEx(This->file, &size))
140         IInternetProtocolSink_ReportData(pOIProtSink,
141                 BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION,
142                 size.u.LowPart, size.u.LowPart);
143
144     return S_OK;
145 }
146
147 static HRESULT WINAPI FileProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
148 {
149     FileProtocol *This = PROTOCOL_THIS(iface);
150     FIXME("(%p)->(%p)\n", This, pProtocolData);
151     return E_NOTIMPL;
152 }
153
154 static HRESULT WINAPI FileProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
155         DWORD dwOptions)
156 {
157     FileProtocol *This = PROTOCOL_THIS(iface);
158     FIXME("(%p)->(%08lx %08lx)\n", This, hrReason, dwOptions);
159     return E_NOTIMPL;
160 }
161
162 static HRESULT WINAPI FileProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
163 {
164     FileProtocol *This = PROTOCOL_THIS(iface);
165
166     TRACE("(%p)->(%08lx)\n", This, dwOptions);
167
168     return S_OK;
169 }
170
171 static HRESULT WINAPI FileProtocol_Suspend(IInternetProtocol *iface)
172 {
173     FileProtocol *This = PROTOCOL_THIS(iface);
174     FIXME("(%p)\n", This);
175     return E_NOTIMPL;
176 }
177
178 static HRESULT WINAPI FileProtocol_Resume(IInternetProtocol *iface)
179 {
180     FileProtocol *This = PROTOCOL_THIS(iface);
181     FIXME("(%p)\n", This);
182     return E_NOTIMPL;
183 }
184
185 static HRESULT WINAPI FileProtocol_Read(IInternetProtocol *iface, void *pv,
186         ULONG cb, ULONG *pcbRead)
187 {
188     FileProtocol *This = PROTOCOL_THIS(iface);
189     DWORD read = 0;
190
191     TRACE("(%p)->(%p %lu %p)\n", This, pv, cb, pcbRead);
192
193     if(!This->file)
194         return INET_E_DATA_NOT_AVAILABLE;
195
196     ReadFile(This->file, pv, cb, &read, NULL);
197
198     if(pcbRead)
199         *pcbRead = read;
200     
201     return cb == read ? S_OK : S_FALSE;
202 }
203
204 static HRESULT WINAPI FileProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
205         DWORD dwOrgin, ULARGE_INTEGER *plibNewPosition)
206 {
207     FileProtocol *This = PROTOCOL_THIS(iface);
208     FIXME("(%p)->(%ld %ld %p)\n", This, dlibMove.u.LowPart, dwOrgin, plibNewPosition);
209     return E_NOTIMPL;
210 }
211
212 static HRESULT WINAPI FileProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
213 {
214     FileProtocol *This = PROTOCOL_THIS(iface);
215
216     TRACE("(%p)->(%08lx)\n", This, dwOptions);
217
218     return S_OK;
219 }
220
221 static HRESULT WINAPI FileProtocol_UnlockRequest(IInternetProtocol *iface)
222 {
223     FileProtocol *This = PROTOCOL_THIS(iface);
224
225     TRACE("(%p)\n", This);
226
227     return S_OK;
228 }
229
230 #undef PROTOCOL_THIS
231
232 static const IInternetProtocolVtbl FileProtocolVtbl = {
233     FileProtocol_QueryInterface,
234     FileProtocol_AddRef,
235     FileProtocol_Release,
236     FileProtocol_Start,
237     FileProtocol_Continue,
238     FileProtocol_Abort,
239     FileProtocol_Terminate,
240     FileProtocol_Suspend,
241     FileProtocol_Resume,
242     FileProtocol_Read,
243     FileProtocol_Seek,
244     FileProtocol_LockRequest,
245     FileProtocol_UnlockRequest
246 };
247
248 HRESULT FileProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
249 {
250     FileProtocol *ret;
251
252     TRACE("(%p %p)\n", pUnkOuter, ppobj);
253
254     URLMON_LockModule();
255
256     ret = HeapAlloc(GetProcessHeap(), 0, sizeof(FileProtocol));
257
258     ret->lpInternetProtocolVtbl = &FileProtocolVtbl;
259     ret->file = NULL;
260     ret->ref = 1;
261
262     *ppobj = PROTOCOL(ret);
263     
264     return S_OK;
265 }