server: Move socket async activation to sock_poll_event.
[wine] / dlls / 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  *lpIInternetProtocolVtbl;
26
27     LONG ref;
28
29     IStream *stream;
30 } MkProtocol;
31
32 #define PROTOCOL_THIS(iface) DEFINE_THIS(MkProtocol, IInternetProtocol, iface)
33
34 static HRESULT WINAPI MkProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
35 {
36     MkProtocol *This = PROTOCOL_THIS(iface);
37
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);
48     }
49
50     if(*ppv) {
51         IInternetProtocol_AddRef(iface);
52         return S_OK;
53     }
54
55     WARN("not supported interface %s\n", debugstr_guid(riid));
56     return E_NOINTERFACE;
57 }
58
59 static ULONG WINAPI MkProtocol_AddRef(IInternetProtocol *iface)
60 {
61     MkProtocol *This = PROTOCOL_THIS(iface);
62     LONG ref = InterlockedIncrement(&This->ref);
63     TRACE("(%p) ref=%d\n", This, ref);
64     return ref;
65 }
66
67 static ULONG WINAPI MkProtocol_Release(IInternetProtocol *iface)
68 {
69     MkProtocol *This = PROTOCOL_THIS(iface);
70     LONG ref = InterlockedDecrement(&This->ref);
71
72     TRACE("(%p) ref=%d\n", This, ref);
73
74     if(!ref) {
75         if(This->stream)
76             IStream_Release(This->stream);
77
78         heap_free(This);
79
80         URLMON_UnlockModule();
81     }
82
83     return ref;
84 }
85
86 static HRESULT report_result(IInternetProtocolSink *sink, HRESULT hres, DWORD dwError)
87 {
88     IInternetProtocolSink_ReportResult(sink, hres, dwError, NULL);
89     return hres;
90 }
91
92 static HRESULT WINAPI MkProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
93         IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
94         DWORD grfPI, HANDLE_PTR dwReserved)
95 {
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;
106
107     static const WCHAR wszMK[] = {'m','k',':','@'};
108
109     TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
110             pOIBindInfo, grfPI, dwReserved);
111
112     if(strncmpiW(szUrl, wszMK, sizeof(wszMK)/sizeof(WCHAR)))
113         return INET_E_INVALID_URL;
114
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;
121     }
122
123     ReleaseBindInfo(&bindinfo);
124
125     IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST, NULL);
126
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);
131     }
132
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);
137
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);
145
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);
151     }
152
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);
162     }
163
164     if(This->stream) {
165         IStream_Release(This->stream);
166         This->stream = NULL;
167     }
168
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);
174     }
175
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);
180     }
181
182     IInternetProtocolSink_ReportData(pOIProtSink,
183             BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION,
184             statstg.cbSize.u.LowPart, statstg.cbSize.u.LowPart);
185
186     return report_result(pOIProtSink, S_OK, ERROR_SUCCESS);
187 }
188
189 static HRESULT WINAPI MkProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
190 {
191     MkProtocol *This = PROTOCOL_THIS(iface);
192     FIXME("(%p)->(%p)\n", This, pProtocolData);
193     return E_NOTIMPL;
194 }
195
196 static HRESULT WINAPI MkProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
197         DWORD dwOptions)
198 {
199     MkProtocol *This = PROTOCOL_THIS(iface);
200     FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
201     return E_NOTIMPL;
202 }
203
204 static HRESULT WINAPI MkProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
205 {
206     MkProtocol *This = PROTOCOL_THIS(iface);
207
208     TRACE("(%p)->(%08x)\n", This, dwOptions);
209
210     return S_OK;
211 }
212
213 static HRESULT WINAPI MkProtocol_Suspend(IInternetProtocol *iface)
214 {
215     MkProtocol *This = PROTOCOL_THIS(iface);
216     FIXME("(%p)\n", This);
217     return E_NOTIMPL;
218 }
219
220 static HRESULT WINAPI MkProtocol_Resume(IInternetProtocol *iface)
221 {
222     MkProtocol *This = PROTOCOL_THIS(iface);
223     FIXME("(%p)\n", This);
224     return E_NOTIMPL;
225 }
226
227 static HRESULT WINAPI MkProtocol_Read(IInternetProtocol *iface, void *pv,
228         ULONG cb, ULONG *pcbRead)
229 {
230     MkProtocol *This = PROTOCOL_THIS(iface);
231
232     TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
233
234     if(!This->stream)
235         return E_FAIL;
236
237     return IStream_Read(This->stream, pv, cb, pcbRead);
238 }
239
240 static HRESULT WINAPI MkProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
241         DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
242 {
243     MkProtocol *This = PROTOCOL_THIS(iface);
244     FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
245     return E_NOTIMPL;
246 }
247
248 static HRESULT WINAPI MkProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
249 {
250     MkProtocol *This = PROTOCOL_THIS(iface);
251
252     TRACE("(%p)->(%08x)\n", This, dwOptions);
253
254     return S_OK;
255 }
256
257 static HRESULT WINAPI MkProtocol_UnlockRequest(IInternetProtocol *iface)
258 {
259     MkProtocol *This = PROTOCOL_THIS(iface);
260
261     TRACE("(%p)\n", This);
262
263     return S_OK;
264 }
265
266 #undef PROTOCOL_THIS
267
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
282 };
283
284 HRESULT MkProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
285 {
286     MkProtocol *ret;
287
288     TRACE("(%p %p)\n", pUnkOuter, ppobj);
289
290     URLMON_LockModule();
291
292     ret = heap_alloc(sizeof(MkProtocol));
293
294     ret->lpIInternetProtocolVtbl = &MkProtocolVtbl;
295     ret->ref = 1;
296     ret->stream = NULL;
297
298     /* NOTE:
299      * Native returns NULL ppobj and S_OK in CreateInstance if called with IID_IUnknown riid.
300      */
301     *ppobj = PROTOCOL(ret);
302
303     return S_OK;
304 }