Sync with trunk r63935.
[reactos.git] / dll / win32 / urlmon / mk.c
1 /*
2 * Copyright 2007 Jacek Caban for CodeWeavers
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 "urlmon_main.h"
20
21 typedef struct {
22 IInternetProtocolEx IInternetProtocolEx_iface;
23
24 LONG ref;
25
26 IStream *stream;
27 } MkProtocol;
28
29 static inline MkProtocol *impl_from_IInternetProtocolEx(IInternetProtocolEx *iface)
30 {
31 return CONTAINING_RECORD(iface, MkProtocol, IInternetProtocolEx_iface);
32 }
33
34 static HRESULT WINAPI MkProtocol_QueryInterface(IInternetProtocolEx *iface, REFIID riid, void **ppv)
35 {
36 MkProtocol *This = impl_from_IInternetProtocolEx(iface);
37
38 *ppv = NULL;
39 if(IsEqualGUID(&IID_IUnknown, riid)) {
40 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
41 *ppv = &This->IInternetProtocolEx_iface;
42 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
43 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
44 *ppv = &This->IInternetProtocolEx_iface;
45 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
46 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
47 *ppv = &This->IInternetProtocolEx_iface;
48 }else if(IsEqualGUID(&IID_IInternetProtocolEx, riid)) {
49 TRACE("(%p)->(IID_IInternetProtocolEx %p)\n", This, ppv);
50 *ppv = &This->IInternetProtocolEx_iface;
51 }
52
53 if(*ppv) {
54 IInternetProtocolEx_AddRef(iface);
55 return S_OK;
56 }
57
58 WARN("not supported interface %s\n", debugstr_guid(riid));
59 return E_NOINTERFACE;
60 }
61
62 static ULONG WINAPI MkProtocol_AddRef(IInternetProtocolEx *iface)
63 {
64 MkProtocol *This = impl_from_IInternetProtocolEx(iface);
65 LONG ref = InterlockedIncrement(&This->ref);
66 TRACE("(%p) ref=%d\n", This, ref);
67 return ref;
68 }
69
70 static ULONG WINAPI MkProtocol_Release(IInternetProtocolEx *iface)
71 {
72 MkProtocol *This = impl_from_IInternetProtocolEx(iface);
73 LONG ref = InterlockedDecrement(&This->ref);
74
75 TRACE("(%p) ref=%d\n", This, ref);
76
77 if(!ref) {
78 if(This->stream)
79 IStream_Release(This->stream);
80
81 heap_free(This);
82
83 URLMON_UnlockModule();
84 }
85
86 return ref;
87 }
88
89 static HRESULT report_result(IInternetProtocolSink *sink, HRESULT hres, DWORD dwError)
90 {
91 IInternetProtocolSink_ReportResult(sink, hres, dwError, NULL);
92 return hres;
93 }
94
95 static HRESULT WINAPI MkProtocol_Start(IInternetProtocolEx *iface, LPCWSTR szUrl,
96 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
97 DWORD grfPI, HANDLE_PTR dwReserved)
98 {
99 MkProtocol *This = impl_from_IInternetProtocolEx(iface);
100 HRESULT hres;
101 IUri *uri;
102
103 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
104 pOIBindInfo, grfPI, dwReserved);
105
106 hres = CreateUri(szUrl, 0, 0, &uri);
107 if(FAILED(hres))
108 return hres;
109
110 hres = IInternetProtocolEx_StartEx(&This->IInternetProtocolEx_iface, uri, pOIProtSink,
111 pOIBindInfo, grfPI, (HANDLE*)dwReserved);
112
113 IUri_Release(uri);
114 return hres;
115 }
116
117 static HRESULT WINAPI MkProtocol_Continue(IInternetProtocolEx *iface, PROTOCOLDATA *pProtocolData)
118 {
119 MkProtocol *This = impl_from_IInternetProtocolEx(iface);
120 FIXME("(%p)->(%p)\n", This, pProtocolData);
121 return E_NOTIMPL;
122 }
123
124 static HRESULT WINAPI MkProtocol_Abort(IInternetProtocolEx *iface, HRESULT hrReason,
125 DWORD dwOptions)
126 {
127 MkProtocol *This = impl_from_IInternetProtocolEx(iface);
128 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
129 return E_NOTIMPL;
130 }
131
132 static HRESULT WINAPI MkProtocol_Terminate(IInternetProtocolEx *iface, DWORD dwOptions)
133 {
134 MkProtocol *This = impl_from_IInternetProtocolEx(iface);
135
136 TRACE("(%p)->(%08x)\n", This, dwOptions);
137
138 return S_OK;
139 }
140
141 static HRESULT WINAPI MkProtocol_Suspend(IInternetProtocolEx *iface)
142 {
143 MkProtocol *This = impl_from_IInternetProtocolEx(iface);
144 FIXME("(%p)\n", This);
145 return E_NOTIMPL;
146 }
147
148 static HRESULT WINAPI MkProtocol_Resume(IInternetProtocolEx *iface)
149 {
150 MkProtocol *This = impl_from_IInternetProtocolEx(iface);
151 FIXME("(%p)\n", This);
152 return E_NOTIMPL;
153 }
154
155 static HRESULT WINAPI MkProtocol_Read(IInternetProtocolEx *iface, void *pv,
156 ULONG cb, ULONG *pcbRead)
157 {
158 MkProtocol *This = impl_from_IInternetProtocolEx(iface);
159
160 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
161
162 if(!This->stream)
163 return E_FAIL;
164
165 return IStream_Read(This->stream, pv, cb, pcbRead);
166 }
167
168 static HRESULT WINAPI MkProtocol_Seek(IInternetProtocolEx *iface, LARGE_INTEGER dlibMove,
169 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
170 {
171 MkProtocol *This = impl_from_IInternetProtocolEx(iface);
172 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
173 return E_NOTIMPL;
174 }
175
176 static HRESULT WINAPI MkProtocol_LockRequest(IInternetProtocolEx *iface, DWORD dwOptions)
177 {
178 MkProtocol *This = impl_from_IInternetProtocolEx(iface);
179
180 TRACE("(%p)->(%08x)\n", This, dwOptions);
181
182 return S_OK;
183 }
184
185 static HRESULT WINAPI MkProtocol_UnlockRequest(IInternetProtocolEx *iface)
186 {
187 MkProtocol *This = impl_from_IInternetProtocolEx(iface);
188
189 TRACE("(%p)\n", This);
190
191 return S_OK;
192 }
193
194 static HRESULT WINAPI MkProtocol_StartEx(IInternetProtocolEx *iface, IUri *pUri,
195 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
196 DWORD grfPI, HANDLE *dwReserved)
197 {
198 MkProtocol *This = impl_from_IInternetProtocolEx(iface);
199 LPWSTR mime, progid, display_name, colon_ptr;
200 DWORD path_size = INTERNET_MAX_URL_LENGTH;
201 DWORD bindf=0, eaten=0, scheme=0, len;
202 BSTR url, path_tmp, path = NULL;
203 IParseDisplayName *pdn;
204 BINDINFO bindinfo;
205 STATSTG statstg;
206 IMoniker *mon;
207 HRESULT hres;
208 CLSID clsid;
209
210 TRACE("(%p)->(%p %p %p %08x %p)\n", This, pUri, pOIProtSink,
211 pOIBindInfo, grfPI, dwReserved);
212
213 hres = IUri_GetScheme(pUri, &scheme);
214 if(FAILED(hres))
215 return hres;
216 if(scheme != URL_SCHEME_MK)
217 return INET_E_INVALID_URL;
218
219 memset(&bindinfo, 0, sizeof(bindinfo));
220 bindinfo.cbSize = sizeof(BINDINFO);
221 hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &bindf, &bindinfo);
222 if(FAILED(hres)) {
223 WARN("GetBindInfo failed: %08x\n", hres);
224 return hres;
225 }
226
227 ReleaseBindInfo(&bindinfo);
228
229 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST, NULL);
230
231 hres = IUri_GetDisplayUri(pUri, &url);
232 if(FAILED(hres))
233 return hres;
234 hres = FindMimeFromData(NULL, url, NULL, 0, NULL, 0, &mime, 0);
235 SysFreeString(url);
236 if(SUCCEEDED(hres)) {
237 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, mime);
238 CoTaskMemFree(mime);
239 }
240
241 hres = IUri_GetPath(pUri, &path_tmp);
242 if(FAILED(hres))
243 return hres;
244 path = heap_alloc(path_size);
245 hres = UrlUnescapeW((LPWSTR)path_tmp, path, &path_size, 0);
246 SysFreeString(path_tmp);
247 if (FAILED(hres))
248 {
249 heap_free(path);
250 return report_result(pOIProtSink, INET_E_RESOURCE_NOT_FOUND, ERROR_INVALID_PARAMETER);
251 }
252 progid = path+1; /* skip '@' symbol */
253 colon_ptr = strchrW(path, ':');
254 if(!colon_ptr)
255 {
256 heap_free(path);
257 return report_result(pOIProtSink, INET_E_RESOURCE_NOT_FOUND, ERROR_INVALID_PARAMETER);
258 }
259
260 len = strlenW(path);
261 display_name = heap_alloc((len+1)*sizeof(WCHAR));
262 memcpy(display_name, path, (len+1)*sizeof(WCHAR));
263
264 progid[colon_ptr-progid] = 0; /* overwrite ':' with NULL terminator */
265 hres = CLSIDFromProgID(progid, &clsid);
266 heap_free(path);
267 if(FAILED(hres))
268 {
269 heap_free(display_name);
270 return report_result(pOIProtSink, INET_E_RESOURCE_NOT_FOUND, ERROR_INVALID_PARAMETER);
271 }
272
273 hres = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
274 &IID_IParseDisplayName, (void**)&pdn);
275 if(FAILED(hres)) {
276 WARN("Could not create object %s\n", debugstr_guid(&clsid));
277 heap_free(display_name);
278 return report_result(pOIProtSink, hres, ERROR_INVALID_PARAMETER);
279 }
280
281 hres = IParseDisplayName_ParseDisplayName(pdn, NULL /* FIXME */, display_name, &eaten, &mon);
282 heap_free(display_name);
283 IParseDisplayName_Release(pdn);
284 if(FAILED(hres)) {
285 WARN("ParseDisplayName failed: %08x\n", hres);
286 return report_result(pOIProtSink, hres, ERROR_INVALID_PARAMETER);
287 }
288
289 if(This->stream) {
290 IStream_Release(This->stream);
291 This->stream = NULL;
292 }
293
294 hres = IMoniker_BindToStorage(mon, NULL /* FIXME */, NULL, &IID_IStream, (void**)&This->stream);
295 IMoniker_Release(mon);
296 if(FAILED(hres)) {
297 WARN("BindToStorage failed: %08x\n", hres);
298 return report_result(pOIProtSink, hres, ERROR_INVALID_PARAMETER);
299 }
300
301 hres = IStream_Stat(This->stream, &statstg, STATFLAG_NONAME);
302 if(FAILED(hres)) {
303 WARN("Stat failed: %08x\n", hres);
304 return report_result(pOIProtSink, hres, ERROR_INVALID_PARAMETER);
305 }
306
307 IInternetProtocolSink_ReportData(pOIProtSink,
308 BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION,
309 statstg.cbSize.u.LowPart, statstg.cbSize.u.LowPart);
310 return report_result(pOIProtSink, S_OK, ERROR_SUCCESS);
311 }
312
313 static const IInternetProtocolExVtbl MkProtocolVtbl = {
314 MkProtocol_QueryInterface,
315 MkProtocol_AddRef,
316 MkProtocol_Release,
317 MkProtocol_Start,
318 MkProtocol_Continue,
319 MkProtocol_Abort,
320 MkProtocol_Terminate,
321 MkProtocol_Suspend,
322 MkProtocol_Resume,
323 MkProtocol_Read,
324 MkProtocol_Seek,
325 MkProtocol_LockRequest,
326 MkProtocol_UnlockRequest,
327 MkProtocol_StartEx
328 };
329
330 HRESULT MkProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
331 {
332 MkProtocol *ret;
333
334 TRACE("(%p %p)\n", pUnkOuter, ppobj);
335
336 URLMON_LockModule();
337
338 ret = heap_alloc(sizeof(MkProtocol));
339
340 ret->IInternetProtocolEx_iface.lpVtbl = &MkProtocolVtbl;
341 ret->ref = 1;
342 ret->stream = NULL;
343
344 /* NOTE:
345 * Native returns NULL ppobj and S_OK in CreateInstance if called with IID_IUnknown riid.
346 */
347 *ppobj = &ret->IInternetProtocolEx_iface;
348
349 return S_OK;
350 }