push 0f15bbd80d260bbd8adf052e820484a405c49375
[wine/hacks.git] / dlls / urlmon / mk.c
blob031224a93e6e65b89ae0f678cbc26833a5645fec
1 /*
2 * Copyright 2007 Jacek Caban for CodeWeavers
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
21 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
27 #include "urlmon.h"
28 #include "urlmon_main.h"
30 #include "wine/debug.h"
31 #include "wine/unicode.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
35 typedef struct {
36 const IInternetProtocolVtbl *lpInternetProtocolVtbl;
38 LONG ref;
40 IStream *stream;
41 } MkProtocol;
43 #define PROTOCOL_THIS(iface) DEFINE_THIS(MkProtocol, InternetProtocol, iface)
45 #define PROTOCOL(x) ((IInternetProtocol*) &(x)->lpInternetProtocolVtbl)
47 static HRESULT WINAPI MkProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
49 MkProtocol *This = PROTOCOL_THIS(iface);
51 *ppv = NULL;
52 if(IsEqualGUID(&IID_IUnknown, riid)) {
53 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
54 *ppv = PROTOCOL(This);
55 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
56 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
57 *ppv = PROTOCOL(This);
58 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
59 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
60 *ppv = PROTOCOL(This);
63 if(*ppv) {
64 IInternetProtocol_AddRef(iface);
65 return S_OK;
68 WARN("not supported interface %s\n", debugstr_guid(riid));
69 return E_NOINTERFACE;
72 static ULONG WINAPI MkProtocol_AddRef(IInternetProtocol *iface)
74 MkProtocol *This = PROTOCOL_THIS(iface);
75 LONG ref = InterlockedIncrement(&This->ref);
76 TRACE("(%p) ref=%d\n", This, ref);
77 return ref;
80 static ULONG WINAPI MkProtocol_Release(IInternetProtocol *iface)
82 MkProtocol *This = PROTOCOL_THIS(iface);
83 LONG ref = InterlockedDecrement(&This->ref);
85 TRACE("(%p) ref=%d\n", This, ref);
87 if(!ref) {
88 if(This->stream)
89 IStream_Release(This->stream);
91 heap_free(This);
93 URLMON_UnlockModule();
96 return ref;
99 static HRESULT report_result(IInternetProtocolSink *sink, HRESULT hres, DWORD dwError)
101 IInternetProtocolSink_ReportResult(sink, hres, dwError, NULL);
102 return hres;
105 static HRESULT WINAPI MkProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
106 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
107 DWORD grfPI, DWORD dwReserved)
109 MkProtocol *This = PROTOCOL_THIS(iface);
110 IParseDisplayName *pdn;
111 IMoniker *mon;
112 LPWSTR mime, progid, display_name;
113 LPCWSTR ptr, ptr2;
114 BINDINFO bindinfo;
115 STATSTG statstg;
116 DWORD bindf=0, eaten=0, len;
117 CLSID clsid;
118 HRESULT hres;
120 static const WCHAR wszMK[] = {'m','k',':'};
122 TRACE("(%p)->(%s %p %p %08x %d)\n", This, debugstr_w(szUrl), pOIProtSink,
123 pOIBindInfo, grfPI, dwReserved);
125 if(strncmpiW(szUrl, wszMK, sizeof(wszMK)/sizeof(WCHAR)))
126 return INET_E_INVALID_URL;
128 memset(&bindinfo, 0, sizeof(bindinfo));
129 bindinfo.cbSize = sizeof(BINDINFO);
130 hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &bindf, &bindinfo);
131 if(FAILED(hres)) {
132 WARN("GetBindInfo failed: %08x\n", hres);
133 return hres;
136 ReleaseBindInfo(&bindinfo);
138 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_DIRECTBIND, NULL);
139 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST, NULL);
141 hres = FindMimeFromData(NULL, szUrl, NULL, 0, NULL, 0, &mime, 0);
142 if(SUCCEEDED(hres)) {
143 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, mime);
144 CoTaskMemFree(mime);
147 ptr2 = szUrl + sizeof(wszMK)/sizeof(WCHAR);
148 if(*ptr2 != '@')
149 return report_result(pOIProtSink, INET_E_RESOURCE_NOT_FOUND, ERROR_INVALID_PARAMETER);
150 ptr2++;
152 ptr = strchrW(ptr2, ':');
153 if(!ptr)
154 return report_result(pOIProtSink, INET_E_RESOURCE_NOT_FOUND, ERROR_INVALID_PARAMETER);
156 progid = heap_alloc((ptr-ptr2+1)*sizeof(WCHAR));
157 memcpy(progid, ptr2, (ptr-ptr2)*sizeof(WCHAR));
158 progid[ptr-ptr2] = 0;
159 hres = CLSIDFromProgID(progid, &clsid);
160 heap_free(progid);
161 if(FAILED(hres))
162 return report_result(pOIProtSink, INET_E_RESOURCE_NOT_FOUND, ERROR_INVALID_PARAMETER);
164 hres = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
165 &IID_IParseDisplayName, (void**)&pdn);
166 if(FAILED(hres)) {
167 WARN("Could not create object %s\n", debugstr_guid(&clsid));
168 return report_result(pOIProtSink, hres, ERROR_INVALID_PARAMETER);
171 len = strlenW(--ptr2);
172 display_name = heap_alloc((len+1)*sizeof(WCHAR));
173 memcpy(display_name, ptr2, (len+1)*sizeof(WCHAR));
174 hres = IParseDisplayName_ParseDisplayName(pdn, NULL /* FIXME */, display_name, &eaten, &mon);
175 heap_free(display_name);
176 IParseDisplayName_Release(pdn);
177 if(FAILED(hres)) {
178 WARN("ParseDisplayName failed: %08x\n", hres);
179 return report_result(pOIProtSink, hres, ERROR_INVALID_PARAMETER);
182 if(This->stream) {
183 IStream_Release(This->stream);
184 This->stream = NULL;
187 hres = IMoniker_BindToStorage(mon, NULL /* FIXME */, NULL, &IID_IStream, (void**)&This->stream);
188 IMoniker_Release(mon);
189 if(FAILED(hres)) {
190 WARN("BindToStorage failed: %08x\n", hres);
191 return report_result(pOIProtSink, hres, ERROR_INVALID_PARAMETER);
194 hres = IStream_Stat(This->stream, &statstg, STATFLAG_NONAME);
195 if(FAILED(hres)) {
196 WARN("Stat failed: %08x\n", hres);
197 return report_result(pOIProtSink, hres, ERROR_INVALID_PARAMETER);
200 IInternetProtocolSink_ReportData(pOIProtSink,
201 BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION,
202 statstg.cbSize.u.LowPart, statstg.cbSize.u.LowPart);
204 return report_result(pOIProtSink, S_OK, ERROR_SUCCESS);
207 static HRESULT WINAPI MkProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
209 MkProtocol *This = PROTOCOL_THIS(iface);
210 FIXME("(%p)->(%p)\n", This, pProtocolData);
211 return E_NOTIMPL;
214 static HRESULT WINAPI MkProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
215 DWORD dwOptions)
217 MkProtocol *This = PROTOCOL_THIS(iface);
218 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
219 return E_NOTIMPL;
222 static HRESULT WINAPI MkProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
224 MkProtocol *This = PROTOCOL_THIS(iface);
226 TRACE("(%p)->(%08x)\n", This, dwOptions);
228 return S_OK;
231 static HRESULT WINAPI MkProtocol_Suspend(IInternetProtocol *iface)
233 MkProtocol *This = PROTOCOL_THIS(iface);
234 FIXME("(%p)\n", This);
235 return E_NOTIMPL;
238 static HRESULT WINAPI MkProtocol_Resume(IInternetProtocol *iface)
240 MkProtocol *This = PROTOCOL_THIS(iface);
241 FIXME("(%p)\n", This);
242 return E_NOTIMPL;
245 static HRESULT WINAPI MkProtocol_Read(IInternetProtocol *iface, void *pv,
246 ULONG cb, ULONG *pcbRead)
248 MkProtocol *This = PROTOCOL_THIS(iface);
250 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
252 if(!This->stream)
253 return E_FAIL;
255 return IStream_Read(This->stream, pv, cb, pcbRead);
258 static HRESULT WINAPI MkProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
259 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
261 MkProtocol *This = PROTOCOL_THIS(iface);
262 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
263 return E_NOTIMPL;
266 static HRESULT WINAPI MkProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
268 MkProtocol *This = PROTOCOL_THIS(iface);
270 TRACE("(%p)->(%08x)\n", This, dwOptions);
272 return S_OK;
275 static HRESULT WINAPI MkProtocol_UnlockRequest(IInternetProtocol *iface)
277 MkProtocol *This = PROTOCOL_THIS(iface);
279 TRACE("(%p)\n", This);
281 return S_OK;
284 #undef PROTOCOL_THIS
286 static const IInternetProtocolVtbl MkProtocolVtbl = {
287 MkProtocol_QueryInterface,
288 MkProtocol_AddRef,
289 MkProtocol_Release,
290 MkProtocol_Start,
291 MkProtocol_Continue,
292 MkProtocol_Abort,
293 MkProtocol_Terminate,
294 MkProtocol_Suspend,
295 MkProtocol_Resume,
296 MkProtocol_Read,
297 MkProtocol_Seek,
298 MkProtocol_LockRequest,
299 MkProtocol_UnlockRequest
302 HRESULT MkProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
304 MkProtocol *ret;
306 TRACE("(%p %p)\n", pUnkOuter, ppobj);
308 URLMON_LockModule();
310 ret = heap_alloc(sizeof(MkProtocol));
312 ret->lpInternetProtocolVtbl = &MkProtocolVtbl;
313 ret->ref = 1;
314 ret->stream = NULL;
316 /* NOTE:
317 * Native returns NULL ppobj and S_OK in CreateInstance if called with IID_IUnknown riid.
319 *ppobj = PROTOCOL(ret);
321 return S_OK;