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
28 #include "urlmon_main.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(urlmon
);
35 const IInternetProtocolVtbl
*lpInternetProtocolVtbl
;
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
);
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
);
63 IInternetProtocol_AddRef(iface
);
67 WARN("not supported interface %s\n", debugstr_guid(riid
));
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
);
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
);
88 CloseHandle(This
->file
);
89 HeapFree(GetProcessHeap(), 0, This
);
91 URLMON_UnlockModule();
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
);
106 LPWSTR url
, mime
= NULL
;
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
)))
122 len
= lstrlenW(szUrl
)+16;
123 url
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
124 hres
= CoInternetParseUrl(szUrl
, PARSE_ENCODE
, 0, url
, len
, &len
, 0);
126 HeapFree(GetProcessHeap(), 0, url
);
130 hres
= FindMimeFromData(NULL
, url
, NULL
, 0, NULL
, 0, &mime
, 0);
132 IInternetProtocolSink_ReportProgress(pOIProtSink
, BINDSTATUS_DIRECTBIND
, mime
);
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
) {
140 IInternetProtocolSink_ReportResult(pOIProtSink
, INET_E_RESOURCE_NOT_FOUND
,
141 GetLastError(), NULL
);
142 HeapFree(GetProcessHeap(), 0, url
);
144 return INET_E_RESOURCE_NOT_FOUND
;
147 IInternetProtocolSink_ReportProgress(pOIProtSink
, BINDSTATUS_CACHEFILENAMEAVAILABLE
,
148 url
+sizeof(wszFile
)/sizeof(WCHAR
));
150 IInternetProtocolSink_ReportProgress(pOIProtSink
, BINDSTATUS_MIMETYPEAVAILABLE
, mime
);
151 IInternetProtocolSink_ReportResult(pOIProtSink
, S_OK
, 0, NULL
);
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
);
165 static HRESULT WINAPI
FileProtocol_Continue(IInternetProtocol
*iface
, PROTOCOLDATA
*pProtocolData
)
167 FileProtocol
*This
= PROTOCOL_THIS(iface
);
168 FIXME("(%p)->(%p)\n", This
, pProtocolData
);
172 static HRESULT WINAPI
FileProtocol_Abort(IInternetProtocol
*iface
, HRESULT hrReason
,
175 FileProtocol
*This
= PROTOCOL_THIS(iface
);
176 FIXME("(%p)->(%08lx %08lx)\n", This
, hrReason
, dwOptions
);
180 static HRESULT WINAPI
FileProtocol_Terminate(IInternetProtocol
*iface
, DWORD dwOptions
)
182 FileProtocol
*This
= PROTOCOL_THIS(iface
);
184 TRACE("(%p)->(%08lx)\n", This
, dwOptions
);
189 static HRESULT WINAPI
FileProtocol_Suspend(IInternetProtocol
*iface
)
191 FileProtocol
*This
= PROTOCOL_THIS(iface
);
192 FIXME("(%p)\n", This
);
196 static HRESULT WINAPI
FileProtocol_Resume(IInternetProtocol
*iface
)
198 FileProtocol
*This
= PROTOCOL_THIS(iface
);
199 FIXME("(%p)\n", This
);
203 static HRESULT WINAPI
FileProtocol_Read(IInternetProtocol
*iface
, void *pv
,
204 ULONG cb
, ULONG
*pcbRead
)
206 FileProtocol
*This
= PROTOCOL_THIS(iface
);
209 TRACE("(%p)->(%p %lu %p)\n", This
, pv
, cb
, pcbRead
);
212 return INET_E_DATA_NOT_AVAILABLE
;
214 ReadFile(This
->file
, pv
, cb
, &read
, NULL
);
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
);
230 static HRESULT WINAPI
FileProtocol_LockRequest(IInternetProtocol
*iface
, DWORD dwOptions
)
232 FileProtocol
*This
= PROTOCOL_THIS(iface
);
234 TRACE("(%p)->(%08lx)\n", This
, dwOptions
);
239 static HRESULT WINAPI
FileProtocol_UnlockRequest(IInternetProtocol
*iface
)
241 FileProtocol
*This
= PROTOCOL_THIS(iface
);
243 TRACE("(%p)\n", This
);
250 static const IInternetProtocolVtbl FileProtocolVtbl
= {
251 FileProtocol_QueryInterface
,
253 FileProtocol_Release
,
255 FileProtocol_Continue
,
257 FileProtocol_Terminate
,
258 FileProtocol_Suspend
,
262 FileProtocol_LockRequest
,
263 FileProtocol_UnlockRequest
266 HRESULT
FileProtocol_Construct(IUnknown
*pUnkOuter
, LPVOID
*ppobj
)
270 TRACE("(%p %p)\n", pUnkOuter
, ppobj
);
274 ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(FileProtocol
));
276 ret
->lpInternetProtocolVtbl
= &FileProtocolVtbl
;
280 *ppobj
= PROTOCOL(ret
);