Release 20050930.
[wine/gsoc-2012-control.git] / dlls / urlmon / file.c
blobbe54cea221ee395045d0e96a0c44910dee331048
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <stdarg.h>
21 #define COBJMACROS
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"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
34 typedef struct {
35 const IInternetProtocolVtbl *lpInternetProtocolVtbl;
37 HANDLE file;
39 LONG ref;
40 } FileProtocol;
42 #define PROTOCOL_THIS(iface) DEFINE_THIS(FileProtocol, InternetProtocol, iface)
44 #define PROTOCOL(x) ((IInternetProtocol*) &(x)->lpInternetProtocolVtbl)
46 static HRESULT WINAPI FileProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
48 FileProtocol *This = PROTOCOL_THIS(iface);
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);
62 if(*ppv) {
63 IInternetProtocol_AddRef(iface);
64 return S_OK;
67 WARN("not supported interface %s\n", debugstr_guid(riid));
68 return E_NOINTERFACE;
71 static ULONG WINAPI FileProtocol_AddRef(IInternetProtocol *iface)
73 FileProtocol *This = PROTOCOL_THIS(iface);
74 LONG ref = InterlockedIncrement(&This->ref);
75 TRACE("(%p) ref=%ld\n", This, ref);
76 return ref;
79 static ULONG WINAPI FileProtocol_Release(IInternetProtocol *iface)
81 FileProtocol *This = PROTOCOL_THIS(iface);
82 LONG ref = InterlockedDecrement(&This->ref);
84 TRACE("(%p) ref=%ld\n", This, ref);
86 if(!ref) {
87 if(This->file)
88 CloseHandle(This->file);
89 HeapFree(GetProcessHeap(), 0, This);
91 URLMON_UnlockModule();
94 return ref;
97 static HRESULT WINAPI FileProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
98 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
99 DWORD grfPI, DWORD dwReserved)
101 FileProtocol *This = PROTOCOL_THIS(iface);
102 BINDINFO bindinfo;
103 DWORD grfBINDF = 0;
104 LARGE_INTEGER size;
105 DWORD len;
106 LPWSTR url, mime = NULL;
107 HRESULT hres;
109 static const WCHAR wszFile[] = {'f','i','l','e',':'};
111 TRACE("(%p)->(%s %p %p %08lx %ld)\n", This, debugstr_w(szUrl), pOIProtSink,
112 pOIBindInfo, grfPI, dwReserved);
114 memset(&bindinfo, 0, sizeof(bindinfo));
115 bindinfo.cbSize = sizeof(BINDINFO);
116 IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
118 if(lstrlenW(szUrl) < sizeof(wszFile)/sizeof(WCHAR)
119 || memcmp(szUrl, wszFile, sizeof(wszFile)))
120 return MK_E_SYNTAX;
122 len = lstrlenW(szUrl)+16;
123 url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
124 hres = CoInternetParseUrl(szUrl, PARSE_ENCODE, 0, url, len, &len, 0);
125 if(FAILED(hres)) {
126 HeapFree(GetProcessHeap(), 0, url);
127 return hres;
130 hres = FindMimeFromData(NULL, url, NULL, 0, NULL, 0, &mime, 0);
131 if(SUCCEEDED(hres))
132 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_DIRECTBIND, mime);
134 if(!This->file) {
135 This->file = CreateFileW(url+sizeof(wszFile)/sizeof(WCHAR), GENERIC_READ, FILE_SHARE_READ,
136 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
138 if(This->file == INVALID_HANDLE_VALUE) {
139 This->file = NULL;
140 IInternetProtocolSink_ReportResult(pOIProtSink, INET_E_RESOURCE_NOT_FOUND,
141 GetLastError(), NULL);
142 HeapFree(GetProcessHeap(), 0, url);
143 CoTaskMemFree(mime);
144 return INET_E_RESOURCE_NOT_FOUND;
147 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_CACHEFILENAMEAVAILABLE,
148 url+sizeof(wszFile)/sizeof(WCHAR));
149 if(mime)
150 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, mime);
151 IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
154 CoTaskMemFree(mime);
155 HeapFree(GetProcessHeap(), 0, url);
157 if(GetFileSizeEx(This->file, &size))
158 IInternetProtocolSink_ReportData(pOIProtSink,
159 BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION,
160 size.u.LowPart, size.u.LowPart);
162 return S_OK;
165 static HRESULT WINAPI FileProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
167 FileProtocol *This = PROTOCOL_THIS(iface);
168 FIXME("(%p)->(%p)\n", This, pProtocolData);
169 return E_NOTIMPL;
172 static HRESULT WINAPI FileProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
173 DWORD dwOptions)
175 FileProtocol *This = PROTOCOL_THIS(iface);
176 FIXME("(%p)->(%08lx %08lx)\n", This, hrReason, dwOptions);
177 return E_NOTIMPL;
180 static HRESULT WINAPI FileProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
182 FileProtocol *This = PROTOCOL_THIS(iface);
184 TRACE("(%p)->(%08lx)\n", This, dwOptions);
186 return S_OK;
189 static HRESULT WINAPI FileProtocol_Suspend(IInternetProtocol *iface)
191 FileProtocol *This = PROTOCOL_THIS(iface);
192 FIXME("(%p)\n", This);
193 return E_NOTIMPL;
196 static HRESULT WINAPI FileProtocol_Resume(IInternetProtocol *iface)
198 FileProtocol *This = PROTOCOL_THIS(iface);
199 FIXME("(%p)\n", This);
200 return E_NOTIMPL;
203 static HRESULT WINAPI FileProtocol_Read(IInternetProtocol *iface, void *pv,
204 ULONG cb, ULONG *pcbRead)
206 FileProtocol *This = PROTOCOL_THIS(iface);
207 DWORD read = 0;
209 TRACE("(%p)->(%p %lu %p)\n", This, pv, cb, pcbRead);
211 if(!This->file)
212 return INET_E_DATA_NOT_AVAILABLE;
214 ReadFile(This->file, pv, cb, &read, NULL);
216 if(pcbRead)
217 *pcbRead = read;
219 return cb == read ? S_OK : S_FALSE;
222 static HRESULT WINAPI FileProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
223 DWORD dwOrgin, ULARGE_INTEGER *plibNewPosition)
225 FileProtocol *This = PROTOCOL_THIS(iface);
226 FIXME("(%p)->(%ld %ld %p)\n", This, dlibMove.u.LowPart, dwOrgin, plibNewPosition);
227 return E_NOTIMPL;
230 static HRESULT WINAPI FileProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
232 FileProtocol *This = PROTOCOL_THIS(iface);
234 TRACE("(%p)->(%08lx)\n", This, dwOptions);
236 return S_OK;
239 static HRESULT WINAPI FileProtocol_UnlockRequest(IInternetProtocol *iface)
241 FileProtocol *This = PROTOCOL_THIS(iface);
243 TRACE("(%p)\n", This);
245 return S_OK;
248 #undef PROTOCOL_THIS
250 static const IInternetProtocolVtbl FileProtocolVtbl = {
251 FileProtocol_QueryInterface,
252 FileProtocol_AddRef,
253 FileProtocol_Release,
254 FileProtocol_Start,
255 FileProtocol_Continue,
256 FileProtocol_Abort,
257 FileProtocol_Terminate,
258 FileProtocol_Suspend,
259 FileProtocol_Resume,
260 FileProtocol_Read,
261 FileProtocol_Seek,
262 FileProtocol_LockRequest,
263 FileProtocol_UnlockRequest
266 HRESULT FileProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
268 FileProtocol *ret;
270 TRACE("(%p %p)\n", pUnkOuter, ppobj);
272 URLMON_LockModule();
274 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(FileProtocol));
276 ret->lpInternetProtocolVtbl = &FileProtocolVtbl;
277 ret->file = NULL;
278 ret->ref = 1;
280 *ppobj = PROTOCOL(ret);
282 return S_OK;