The real, definitive, Visual C++ support branch. Accept no substitutes
[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 #include "wine/debug.h"
21
22 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
23
24 typedef struct {
25 const IInternetProtocolVtbl *lpInternetProtocolVtbl;
26
27 LONG ref;
28
29 IStream *stream;
30 } MkProtocol;
31
32 #define PROTOCOL_THIS(iface) DEFINE_THIS(MkProtocol, InternetProtocol, iface)
33
34 #define PROTOCOL(x) ((IInternetProtocol*) &(x)->lpInternetProtocolVtbl)
35
36 static HRESULT WINAPI MkProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
37 {
38 MkProtocol *This = PROTOCOL_THIS(iface);
39
40 *ppv = NULL;
41 if(IsEqualGUID(&IID_IUnknown, riid)) {
42 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
43 *ppv = PROTOCOL(This);
44 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
45 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
46 *ppv = PROTOCOL(This);
47 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
48 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
49 *ppv = PROTOCOL(This);
50 }
51
52 if(*ppv) {
53 IInternetProtocol_AddRef(iface);
54 return S_OK;
55 }
56
57 WARN("not supported interface %s\n", debugstr_guid(riid));
58 return E_NOINTERFACE;
59 }
60
61 static ULONG WINAPI MkProtocol_AddRef(IInternetProtocol *iface)
62 {
63 MkProtocol *This = PROTOCOL_THIS(iface);
64 LONG ref = InterlockedIncrement(&This->ref);
65 TRACE("(%p) ref=%d\n", This, ref);
66 return ref;
67 }
68
69 static ULONG WINAPI MkProtocol_Release(IInternetProtocol *iface)
70 {
71 MkProtocol *This = PROTOCOL_THIS(iface);
72 LONG ref = InterlockedDecrement(&This->ref);
73
74 TRACE("(%p) ref=%d\n", This, ref);
75
76 if(!ref) {
77 if(This->stream)
78 IStream_Release(This->stream);
79
80 heap_free(This);
81
82 URLMON_UnlockModule();
83 }
84
85 return ref;
86 }
87
88 static HRESULT report_result(IInternetProtocolSink *sink, HRESULT hres, DWORD dwError)
89 {
90 IInternetProtocolSink_ReportResult(sink, hres, dwError, NULL);
91 return hres;
92 }
93
94 static HRESULT WINAPI MkProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
95 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
96 DWORD grfPI, DWORD dwReserved)
97 {
98 MkProtocol *This = PROTOCOL_THIS(iface);
99 IParseDisplayName *pdn;
100 IMoniker *mon;
101 LPWSTR mime, progid, display_name;
102 LPCWSTR ptr, ptr2;
103 BINDINFO bindinfo;
104 STATSTG statstg;
105 DWORD bindf=0, eaten=0, len;
106 CLSID clsid;
107 HRESULT hres;
108
109 static const WCHAR wszMK[] = {'m','k',':','@'};
110
111 TRACE("(%p)->(%s %p %p %08x %d)\n", This, debugstr_w(szUrl), pOIProtSink,
112 pOIBindInfo, grfPI, dwReserved);
113
114 if(strncmpiW(szUrl, wszMK, sizeof(wszMK)/sizeof(WCHAR)))
115 return INET_E_INVALID_URL;
116
117 memset(&bindinfo, 0, sizeof(bindinfo));
118 bindinfo.cbSize = sizeof(BINDINFO);
119 hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &bindf, &bindinfo);
120 if(FAILED(hres)) {
121 WARN("GetBindInfo failed: %08x\n", hres);
122 return hres;
123 }
124
125 ReleaseBindInfo(&bindinfo);
126
127 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST, NULL);
128
129 hres = FindMimeFromData(NULL, szUrl, NULL, 0, NULL, 0, &mime, 0);
130 if(SUCCEEDED(hres)) {
131 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, mime);
132 CoTaskMemFree(mime);
133 }
134
135 ptr2 = szUrl + sizeof(wszMK)/sizeof(WCHAR);
136 ptr = strchrW(ptr2, ':');
137 if(!ptr)
138 return report_result(pOIProtSink, INET_E_RESOURCE_NOT_FOUND, ERROR_INVALID_PARAMETER);
139
140 progid = heap_alloc((ptr-ptr2+1)*sizeof(WCHAR));
141 memcpy(progid, ptr2, (ptr-ptr2)*sizeof(WCHAR));
142 progid[ptr-ptr2] = 0;
143 hres = CLSIDFromProgID(progid, &clsid);
144 heap_free(progid);
145 if(FAILED(hres))
146 return report_result(pOIProtSink, INET_E_RESOURCE_NOT_FOUND, ERROR_INVALID_PARAMETER);
147
148 hres = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
149 &IID_IParseDisplayName, (void**)&pdn);
150 if(FAILED(hres)) {
151 WARN("Could not create object %s\n", debugstr_guid(&clsid));
152 return report_result(pOIProtSink, hres, ERROR_INVALID_PARAMETER);
153 }
154
155 len = strlenW(--ptr2);
156 display_name = heap_alloc((len+1)*sizeof(WCHAR));
157 memcpy(display_name, ptr2, (len+1)*sizeof(WCHAR));
158 hres = IParseDisplayName_ParseDisplayName(pdn, NULL /* FIXME */, display_name, &eaten, &mon);
159 heap_free(display_name);
160 IParseDisplayName_Release(pdn);
161 if(FAILED(hres)) {
162 WARN("ParseDisplayName failed: %08x\n", hres);
163 return report_result(pOIProtSink, hres, ERROR_INVALID_PARAMETER);
164 }
165
166 if(This->stream) {
167 IStream_Release(This->stream);
168 This->stream = NULL;
169 }
170
171 hres = IMoniker_BindToStorage(mon, NULL /* FIXME */, NULL, &IID_IStream, (void**)&This->stream);
172 IMoniker_Release(mon);
173 if(FAILED(hres)) {
174 WARN("BindToStorage failed: %08x\n", hres);
175 return report_result(pOIProtSink, hres, ERROR_INVALID_PARAMETER);
176 }
177
178 hres = IStream_Stat(This->stream, &statstg, STATFLAG_NONAME);
179 if(FAILED(hres)) {
180 WARN("Stat failed: %08x\n", hres);
181 return report_result(pOIProtSink, hres, ERROR_INVALID_PARAMETER);
182 }
183
184 IInternetProtocolSink_ReportData(pOIProtSink,
185 BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION,
186 statstg.cbSize.u.LowPart, statstg.cbSize.u.LowPart);
187
188 return report_result(pOIProtSink, S_OK, ERROR_SUCCESS);
189 }
190
191 static HRESULT WINAPI MkProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
192 {
193 MkProtocol *This = PROTOCOL_THIS(iface);
194 FIXME("(%p)->(%p)\n", This, pProtocolData);
195 return E_NOTIMPL;
196 }
197
198 static HRESULT WINAPI MkProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
199 DWORD dwOptions)
200 {
201 MkProtocol *This = PROTOCOL_THIS(iface);
202 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
203 return E_NOTIMPL;
204 }
205
206 static HRESULT WINAPI MkProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
207 {
208 MkProtocol *This = PROTOCOL_THIS(iface);
209
210 TRACE("(%p)->(%08x)\n", This, dwOptions);
211
212 return S_OK;
213 }
214
215 static HRESULT WINAPI MkProtocol_Suspend(IInternetProtocol *iface)
216 {
217 MkProtocol *This = PROTOCOL_THIS(iface);
218 FIXME("(%p)\n", This);
219 return E_NOTIMPL;
220 }
221
222 static HRESULT WINAPI MkProtocol_Resume(IInternetProtocol *iface)
223 {
224 MkProtocol *This = PROTOCOL_THIS(iface);
225 FIXME("(%p)\n", This);
226 return E_NOTIMPL;
227 }
228
229 static HRESULT WINAPI MkProtocol_Read(IInternetProtocol *iface, void *pv,
230 ULONG cb, ULONG *pcbRead)
231 {
232 MkProtocol *This = PROTOCOL_THIS(iface);
233
234 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
235
236 if(!This->stream)
237 return E_FAIL;
238
239 return IStream_Read(This->stream, pv, cb, pcbRead);
240 }
241
242 static HRESULT WINAPI MkProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
243 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
244 {
245 MkProtocol *This = PROTOCOL_THIS(iface);
246 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
247 return E_NOTIMPL;
248 }
249
250 static HRESULT WINAPI MkProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
251 {
252 MkProtocol *This = PROTOCOL_THIS(iface);
253
254 TRACE("(%p)->(%08x)\n", This, dwOptions);
255
256 return S_OK;
257 }
258
259 static HRESULT WINAPI MkProtocol_UnlockRequest(IInternetProtocol *iface)
260 {
261 MkProtocol *This = PROTOCOL_THIS(iface);
262
263 TRACE("(%p)\n", This);
264
265 return S_OK;
266 }
267
268 #undef PROTOCOL_THIS
269
270 static const IInternetProtocolVtbl MkProtocolVtbl = {
271 MkProtocol_QueryInterface,
272 MkProtocol_AddRef,
273 MkProtocol_Release,
274 MkProtocol_Start,
275 MkProtocol_Continue,
276 MkProtocol_Abort,
277 MkProtocol_Terminate,
278 MkProtocol_Suspend,
279 MkProtocol_Resume,
280 MkProtocol_Read,
281 MkProtocol_Seek,
282 MkProtocol_LockRequest,
283 MkProtocol_UnlockRequest
284 };
285
286 HRESULT MkProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
287 {
288 MkProtocol *ret;
289
290 TRACE("(%p %p)\n", pUnkOuter, ppobj);
291
292 URLMON_LockModule();
293
294 ret = heap_alloc(sizeof(MkProtocol));
295
296 ret->lpInternetProtocolVtbl = &MkProtocolVtbl;
297 ret->ref = 1;
298 ret->stream = NULL;
299
300 /* NOTE:
301 * Native returns NULL ppobj and S_OK in CreateInstance if called with IID_IUnknown riid.
302 */
303 *ppobj = PROTOCOL(ret);
304
305 return S_OK;
306 }