kernel32: Don't use HIWORD to check for swapped args in GetCurrentDirectoryA.
[wine.git] / dlls / urlmon / mk.c
blob0befe5c34c6915785af3dcb835fc4829e875608d
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 "urlmon_main.h"
20 #include "wine/debug.h"
22 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
24 typedef struct {
25 const IInternetProtocolVtbl *lpIInternetProtocolVtbl;
27 LONG ref;
29 IStream *stream;
30 } MkProtocol;
32 #define PROTOCOL_THIS(iface) DEFINE_THIS(MkProtocol, IInternetProtocol, iface)
34 static HRESULT WINAPI MkProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
36 MkProtocol *This = PROTOCOL_THIS(iface);
38 *ppv = NULL;
39 if(IsEqualGUID(&IID_IUnknown, riid)) {
40 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
41 *ppv = PROTOCOL(This);
42 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
43 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
44 *ppv = PROTOCOL(This);
45 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
46 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
47 *ppv = PROTOCOL(This);
50 if(*ppv) {
51 IInternetProtocol_AddRef(iface);
52 return S_OK;
55 WARN("not supported interface %s\n", debugstr_guid(riid));
56 return E_NOINTERFACE;
59 static ULONG WINAPI MkProtocol_AddRef(IInternetProtocol *iface)
61 MkProtocol *This = PROTOCOL_THIS(iface);
62 LONG ref = InterlockedIncrement(&This->ref);
63 TRACE("(%p) ref=%d\n", This, ref);
64 return ref;
67 static ULONG WINAPI MkProtocol_Release(IInternetProtocol *iface)
69 MkProtocol *This = PROTOCOL_THIS(iface);
70 LONG ref = InterlockedDecrement(&This->ref);
72 TRACE("(%p) ref=%d\n", This, ref);
74 if(!ref) {
75 if(This->stream)
76 IStream_Release(This->stream);
78 heap_free(This);
80 URLMON_UnlockModule();
83 return ref;
86 static HRESULT report_result(IInternetProtocolSink *sink, HRESULT hres, DWORD dwError)
88 IInternetProtocolSink_ReportResult(sink, hres, dwError, NULL);
89 return hres;
92 static HRESULT WINAPI MkProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
93 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
94 DWORD grfPI, HANDLE_PTR dwReserved)
96 MkProtocol *This = PROTOCOL_THIS(iface);
97 IParseDisplayName *pdn;
98 IMoniker *mon;
99 LPWSTR mime, progid, display_name;
100 LPCWSTR ptr, ptr2;
101 BINDINFO bindinfo;
102 STATSTG statstg;
103 DWORD bindf=0, eaten=0, len;
104 CLSID clsid;
105 HRESULT hres;
107 static const WCHAR wszMK[] = {'m','k',':','@'};
109 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
110 pOIBindInfo, grfPI, dwReserved);
112 if(strncmpiW(szUrl, wszMK, sizeof(wszMK)/sizeof(WCHAR)))
113 return INET_E_INVALID_URL;
115 memset(&bindinfo, 0, sizeof(bindinfo));
116 bindinfo.cbSize = sizeof(BINDINFO);
117 hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &bindf, &bindinfo);
118 if(FAILED(hres)) {
119 WARN("GetBindInfo failed: %08x\n", hres);
120 return hres;
123 ReleaseBindInfo(&bindinfo);
125 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST, NULL);
127 hres = FindMimeFromData(NULL, szUrl, NULL, 0, NULL, 0, &mime, 0);
128 if(SUCCEEDED(hres)) {
129 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, mime);
130 CoTaskMemFree(mime);
133 ptr2 = szUrl + sizeof(wszMK)/sizeof(WCHAR);
134 ptr = strchrW(ptr2, ':');
135 if(!ptr)
136 return report_result(pOIProtSink, INET_E_RESOURCE_NOT_FOUND, ERROR_INVALID_PARAMETER);
138 progid = heap_alloc((ptr-ptr2+1)*sizeof(WCHAR));
139 memcpy(progid, ptr2, (ptr-ptr2)*sizeof(WCHAR));
140 progid[ptr-ptr2] = 0;
141 hres = CLSIDFromProgID(progid, &clsid);
142 heap_free(progid);
143 if(FAILED(hres))
144 return report_result(pOIProtSink, INET_E_RESOURCE_NOT_FOUND, ERROR_INVALID_PARAMETER);
146 hres = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
147 &IID_IParseDisplayName, (void**)&pdn);
148 if(FAILED(hres)) {
149 WARN("Could not create object %s\n", debugstr_guid(&clsid));
150 return report_result(pOIProtSink, hres, ERROR_INVALID_PARAMETER);
153 len = strlenW(--ptr2);
154 display_name = heap_alloc((len+1)*sizeof(WCHAR));
155 memcpy(display_name, ptr2, (len+1)*sizeof(WCHAR));
156 hres = IParseDisplayName_ParseDisplayName(pdn, NULL /* FIXME */, display_name, &eaten, &mon);
157 heap_free(display_name);
158 IParseDisplayName_Release(pdn);
159 if(FAILED(hres)) {
160 WARN("ParseDisplayName failed: %08x\n", hres);
161 return report_result(pOIProtSink, hres, ERROR_INVALID_PARAMETER);
164 if(This->stream) {
165 IStream_Release(This->stream);
166 This->stream = NULL;
169 hres = IMoniker_BindToStorage(mon, NULL /* FIXME */, NULL, &IID_IStream, (void**)&This->stream);
170 IMoniker_Release(mon);
171 if(FAILED(hres)) {
172 WARN("BindToStorage failed: %08x\n", hres);
173 return report_result(pOIProtSink, hres, ERROR_INVALID_PARAMETER);
176 hres = IStream_Stat(This->stream, &statstg, STATFLAG_NONAME);
177 if(FAILED(hres)) {
178 WARN("Stat failed: %08x\n", hres);
179 return report_result(pOIProtSink, hres, ERROR_INVALID_PARAMETER);
182 IInternetProtocolSink_ReportData(pOIProtSink,
183 BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION,
184 statstg.cbSize.u.LowPart, statstg.cbSize.u.LowPart);
186 return report_result(pOIProtSink, S_OK, ERROR_SUCCESS);
189 static HRESULT WINAPI MkProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
191 MkProtocol *This = PROTOCOL_THIS(iface);
192 FIXME("(%p)->(%p)\n", This, pProtocolData);
193 return E_NOTIMPL;
196 static HRESULT WINAPI MkProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
197 DWORD dwOptions)
199 MkProtocol *This = PROTOCOL_THIS(iface);
200 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
201 return E_NOTIMPL;
204 static HRESULT WINAPI MkProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
206 MkProtocol *This = PROTOCOL_THIS(iface);
208 TRACE("(%p)->(%08x)\n", This, dwOptions);
210 return S_OK;
213 static HRESULT WINAPI MkProtocol_Suspend(IInternetProtocol *iface)
215 MkProtocol *This = PROTOCOL_THIS(iface);
216 FIXME("(%p)\n", This);
217 return E_NOTIMPL;
220 static HRESULT WINAPI MkProtocol_Resume(IInternetProtocol *iface)
222 MkProtocol *This = PROTOCOL_THIS(iface);
223 FIXME("(%p)\n", This);
224 return E_NOTIMPL;
227 static HRESULT WINAPI MkProtocol_Read(IInternetProtocol *iface, void *pv,
228 ULONG cb, ULONG *pcbRead)
230 MkProtocol *This = PROTOCOL_THIS(iface);
232 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
234 if(!This->stream)
235 return E_FAIL;
237 return IStream_Read(This->stream, pv, cb, pcbRead);
240 static HRESULT WINAPI MkProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
241 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
243 MkProtocol *This = PROTOCOL_THIS(iface);
244 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
245 return E_NOTIMPL;
248 static HRESULT WINAPI MkProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
250 MkProtocol *This = PROTOCOL_THIS(iface);
252 TRACE("(%p)->(%08x)\n", This, dwOptions);
254 return S_OK;
257 static HRESULT WINAPI MkProtocol_UnlockRequest(IInternetProtocol *iface)
259 MkProtocol *This = PROTOCOL_THIS(iface);
261 TRACE("(%p)\n", This);
263 return S_OK;
266 #undef PROTOCOL_THIS
268 static const IInternetProtocolVtbl MkProtocolVtbl = {
269 MkProtocol_QueryInterface,
270 MkProtocol_AddRef,
271 MkProtocol_Release,
272 MkProtocol_Start,
273 MkProtocol_Continue,
274 MkProtocol_Abort,
275 MkProtocol_Terminate,
276 MkProtocol_Suspend,
277 MkProtocol_Resume,
278 MkProtocol_Read,
279 MkProtocol_Seek,
280 MkProtocol_LockRequest,
281 MkProtocol_UnlockRequest
284 HRESULT MkProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
286 MkProtocol *ret;
288 TRACE("(%p %p)\n", pUnkOuter, ppobj);
290 URLMON_LockModule();
292 ret = heap_alloc(sizeof(MkProtocol));
294 ret->lpIInternetProtocolVtbl = &MkProtocolVtbl;
295 ret->ref = 1;
296 ret->stream = NULL;
298 /* NOTE:
299 * Native returns NULL ppobj and S_OK in CreateInstance if called with IID_IUnknown riid.
301 *ppobj = PROTOCOL(ret);
303 return S_OK;