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
;
110 static const WCHAR wszFile
[] = {'f','i','l','e',':'};
112 TRACE("(%p)->(%s %p %p %08lx %ld)\n", This
, debugstr_w(szUrl
), pOIProtSink
,
113 pOIBindInfo
, grfPI
, dwReserved
);
115 memset(&bindinfo
, 0, sizeof(bindinfo
));
116 bindinfo
.cbSize
= sizeof(BINDINFO
);
117 IInternetBindInfo_GetBindInfo(pOIBindInfo
, &grfBINDF
, &bindinfo
);
119 if(lstrlenW(szUrl
) < sizeof(wszFile
)/sizeof(WCHAR
)
120 || memcmp(szUrl
, wszFile
, sizeof(wszFile
)))
123 len
= lstrlenW(szUrl
)+16;
124 url
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
125 hres
= CoInternetParseUrl(szUrl
, PARSE_ENCODE
, 0, url
, len
, &len
, 0);
127 HeapFree(GetProcessHeap(), 0, url
);
131 hres
= FindMimeFromData(NULL
, url
, NULL
, 0, NULL
, 0, &mime
, 0);
133 IInternetProtocolSink_ReportProgress(pOIProtSink
, BINDSTATUS_DIRECTBIND
, mime
);
136 IInternetProtocolSink_ReportProgress(pOIProtSink
, BINDSTATUS_SENDINGREQUEST
, &null_char
);
138 This
->file
= CreateFileW(url
+sizeof(wszFile
)/sizeof(WCHAR
), GENERIC_READ
, FILE_SHARE_READ
,
139 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
141 if(This
->file
== INVALID_HANDLE_VALUE
) {
143 IInternetProtocolSink_ReportResult(pOIProtSink
, INET_E_RESOURCE_NOT_FOUND
,
144 GetLastError(), NULL
);
145 HeapFree(GetProcessHeap(), 0, url
);
147 return INET_E_RESOURCE_NOT_FOUND
;
150 IInternetProtocolSink_ReportProgress(pOIProtSink
, BINDSTATUS_CACHEFILENAMEAVAILABLE
,
151 url
+sizeof(wszFile
)/sizeof(WCHAR
));
153 IInternetProtocolSink_ReportProgress(pOIProtSink
, BINDSTATUS_MIMETYPEAVAILABLE
, mime
);
154 IInternetProtocolSink_ReportResult(pOIProtSink
, S_OK
, 0, NULL
);
158 HeapFree(GetProcessHeap(), 0, url
);
160 if(GetFileSizeEx(This
->file
, &size
))
161 IInternetProtocolSink_ReportData(pOIProtSink
,
162 BSCF_FIRSTDATANOTIFICATION
|BSCF_LASTDATANOTIFICATION
,
163 size
.u
.LowPart
, size
.u
.LowPart
);
168 static HRESULT WINAPI
FileProtocol_Continue(IInternetProtocol
*iface
, PROTOCOLDATA
*pProtocolData
)
170 FileProtocol
*This
= PROTOCOL_THIS(iface
);
171 FIXME("(%p)->(%p)\n", This
, pProtocolData
);
175 static HRESULT WINAPI
FileProtocol_Abort(IInternetProtocol
*iface
, HRESULT hrReason
,
178 FileProtocol
*This
= PROTOCOL_THIS(iface
);
179 FIXME("(%p)->(%08lx %08lx)\n", This
, hrReason
, dwOptions
);
183 static HRESULT WINAPI
FileProtocol_Terminate(IInternetProtocol
*iface
, DWORD dwOptions
)
185 FileProtocol
*This
= PROTOCOL_THIS(iface
);
187 TRACE("(%p)->(%08lx)\n", This
, dwOptions
);
192 static HRESULT WINAPI
FileProtocol_Suspend(IInternetProtocol
*iface
)
194 FileProtocol
*This
= PROTOCOL_THIS(iface
);
195 FIXME("(%p)\n", This
);
199 static HRESULT WINAPI
FileProtocol_Resume(IInternetProtocol
*iface
)
201 FileProtocol
*This
= PROTOCOL_THIS(iface
);
202 FIXME("(%p)\n", This
);
206 static HRESULT WINAPI
FileProtocol_Read(IInternetProtocol
*iface
, void *pv
,
207 ULONG cb
, ULONG
*pcbRead
)
209 FileProtocol
*This
= PROTOCOL_THIS(iface
);
212 TRACE("(%p)->(%p %lu %p)\n", This
, pv
, cb
, pcbRead
);
215 return INET_E_DATA_NOT_AVAILABLE
;
217 ReadFile(This
->file
, pv
, cb
, &read
, NULL
);
222 return cb
== read
? S_OK
: S_FALSE
;
225 static HRESULT WINAPI
FileProtocol_Seek(IInternetProtocol
*iface
, LARGE_INTEGER dlibMove
,
226 DWORD dwOrgin
, ULARGE_INTEGER
*plibNewPosition
)
228 FileProtocol
*This
= PROTOCOL_THIS(iface
);
229 FIXME("(%p)->(%ld %ld %p)\n", This
, dlibMove
.u
.LowPart
, dwOrgin
, plibNewPosition
);
233 static HRESULT WINAPI
FileProtocol_LockRequest(IInternetProtocol
*iface
, DWORD dwOptions
)
235 FileProtocol
*This
= PROTOCOL_THIS(iface
);
237 TRACE("(%p)->(%08lx)\n", This
, dwOptions
);
242 static HRESULT WINAPI
FileProtocol_UnlockRequest(IInternetProtocol
*iface
)
244 FileProtocol
*This
= PROTOCOL_THIS(iface
);
246 TRACE("(%p)\n", This
);
253 static const IInternetProtocolVtbl FileProtocolVtbl
= {
254 FileProtocol_QueryInterface
,
256 FileProtocol_Release
,
258 FileProtocol_Continue
,
260 FileProtocol_Terminate
,
261 FileProtocol_Suspend
,
265 FileProtocol_LockRequest
,
266 FileProtocol_UnlockRequest
269 HRESULT
FileProtocol_Construct(IUnknown
*pUnkOuter
, LPVOID
*ppobj
)
273 TRACE("(%p %p)\n", pUnkOuter
, ppobj
);
277 ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(FileProtocol
));
279 ret
->lpInternetProtocolVtbl
= &FileProtocolVtbl
;
283 *ppobj
= PROTOCOL(ret
);