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