urlmon: Added pluggable protocol support to CoInternetCombineUrlEx.
[wine] / dlls / urlmon / protproxy.c
1 /*
2  * Copyright 2009 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 #define PROTOCOL_THIS(iface) DEFINE_THIS(ProtocolProxy, IInternetProtocol, iface)
25
26 static HRESULT WINAPI ProtocolProxy_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
27 {
28     ProtocolProxy *This = PROTOCOL_THIS(iface);
29
30     *ppv = NULL;
31     if(IsEqualGUID(&IID_IUnknown, riid)) {
32         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
33         *ppv = PROTOCOL(This);
34     }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
35         TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
36         *ppv = PROTOCOL(This);
37     }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
38         TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
39         *ppv = PROTOCOL(This);
40     }else if(IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
41         TRACE("(%p)->(IID_IInternetProtocolSink %p)\n", This, ppv);
42         *ppv = PROTSINK(This);
43     }
44
45     if(*ppv) {
46         IInternetProtocol_AddRef(iface);
47         return S_OK;
48     }
49
50     WARN("not supported interface %s\n", debugstr_guid(riid));
51     return E_NOINTERFACE;
52 }
53
54 static ULONG WINAPI ProtocolProxy_AddRef(IInternetProtocol *iface)
55 {
56     ProtocolProxy *This = PROTOCOL_THIS(iface);
57     LONG ref = InterlockedIncrement(&This->ref);
58     TRACE("(%p) ref=%d\n", This, ref);
59     return ref;
60 }
61
62 static ULONG WINAPI ProtocolProxy_Release(IInternetProtocol *iface)
63 {
64     ProtocolProxy *This = PROTOCOL_THIS(iface);
65     LONG ref = InterlockedDecrement(&This->ref);
66
67     TRACE("(%p) ref=%d\n", This, ref);
68
69     if(!ref) {
70         if(This->protocol_sink)
71             IInternetProtocolSink_Release(This->protocol_sink);
72         if(This->protocol)
73             IInternetProtocol_Release(This->protocol);
74
75         heap_free(This);
76
77         URLMON_UnlockModule();
78     }
79
80     return ref;
81 }
82
83 static HRESULT WINAPI ProtocolProxy_Start(IInternetProtocol *iface, LPCWSTR szUrl,
84         IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
85         DWORD grfPI, HANDLE_PTR dwReserved)
86 {
87     ProtocolProxy *This = PROTOCOL_THIS(iface);
88
89     TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
90           pOIBindInfo, grfPI, dwReserved);
91
92     return IInternetProtocol_Start(This->protocol, szUrl, pOIProtSink, pOIBindInfo, grfPI, dwReserved);
93 }
94
95 static HRESULT WINAPI ProtocolProxy_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
96 {
97     ProtocolProxy *This = PROTOCOL_THIS(iface);
98
99     TRACE("(%p)->(%p)\n", This, pProtocolData);
100
101     return IInternetProtocol_Continue(This->protocol, pProtocolData);
102 }
103
104 static HRESULT WINAPI ProtocolProxy_Abort(IInternetProtocol *iface, HRESULT hrReason,
105         DWORD dwOptions)
106 {
107     ProtocolProxy *This = PROTOCOL_THIS(iface);
108     FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
109     return E_NOTIMPL;
110 }
111
112 static HRESULT WINAPI ProtocolProxy_Terminate(IInternetProtocol *iface, DWORD dwOptions)
113 {
114     ProtocolProxy *This = PROTOCOL_THIS(iface);
115
116     TRACE("(%p)->(%08x)\n", This, dwOptions);
117
118     return IInternetProtocol_Terminate(This->protocol, dwOptions);
119 }
120
121 static HRESULT WINAPI ProtocolProxy_Suspend(IInternetProtocol *iface)
122 {
123     ProtocolProxy *This = PROTOCOL_THIS(iface);
124     FIXME("(%p)\n", This);
125     return E_NOTIMPL;
126 }
127
128 static HRESULT WINAPI ProtocolProxy_Resume(IInternetProtocol *iface)
129 {
130     ProtocolProxy *This = PROTOCOL_THIS(iface);
131     FIXME("(%p)\n", This);
132     return E_NOTIMPL;
133 }
134
135 static HRESULT WINAPI ProtocolProxy_Read(IInternetProtocol *iface, void *pv,
136         ULONG cb, ULONG *pcbRead)
137 {
138     ProtocolProxy *This = PROTOCOL_THIS(iface);
139
140     TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
141
142     return IInternetProtocol_Read(This->protocol, pv, cb, pcbRead);
143 }
144
145 static HRESULT WINAPI ProtocolProxy_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
146         DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
147 {
148     ProtocolProxy *This = PROTOCOL_THIS(iface);
149     FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
150     return E_NOTIMPL;
151 }
152
153 static HRESULT WINAPI ProtocolProxy_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
154 {
155     ProtocolProxy *This = PROTOCOL_THIS(iface);
156
157     TRACE("(%p)->(%08x)\n", This, dwOptions);
158
159     return IInternetProtocol_LockRequest(This->protocol, dwOptions);
160 }
161
162 static HRESULT WINAPI ProtocolProxy_UnlockRequest(IInternetProtocol *iface)
163 {
164     ProtocolProxy *This = PROTOCOL_THIS(iface);
165
166     TRACE("(%p)\n", This);
167
168     return IInternetProtocol_UnlockRequest(This->protocol);
169 }
170
171 #undef PROTOCOL_THIS
172
173 static const IInternetProtocolVtbl ProtocolProxyVtbl = {
174     ProtocolProxy_QueryInterface,
175     ProtocolProxy_AddRef,
176     ProtocolProxy_Release,
177     ProtocolProxy_Start,
178     ProtocolProxy_Continue,
179     ProtocolProxy_Abort,
180     ProtocolProxy_Terminate,
181     ProtocolProxy_Suspend,
182     ProtocolProxy_Resume,
183     ProtocolProxy_Read,
184     ProtocolProxy_Seek,
185     ProtocolProxy_LockRequest,
186     ProtocolProxy_UnlockRequest
187 };
188
189 #define PROTSINK_THIS(iface) DEFINE_THIS(ProtocolProxy, IInternetProtocolSink, iface)
190
191 static HRESULT WINAPI ProtocolProxySink_QueryInterface(IInternetProtocolSink *iface,
192         REFIID riid, void **ppv)
193 {
194     ProtocolProxy *This = PROTSINK_THIS(iface);
195     return IInternetProtocol_QueryInterface(PROTOCOL(This), riid, ppv);
196 }
197
198 static ULONG WINAPI ProtocolProxySink_AddRef(IInternetProtocolSink *iface)
199 {
200     ProtocolProxy *This = PROTSINK_THIS(iface);
201     return IInternetProtocol_AddRef(PROTOCOL(This));
202 }
203
204 static ULONG WINAPI ProtocolProxySink_Release(IInternetProtocolSink *iface)
205 {
206     ProtocolProxy *This = PROTSINK_THIS(iface);
207     return IInternetProtocol_Release(PROTOCOL(This));
208 }
209
210 static HRESULT WINAPI ProtocolProxySink_Switch(IInternetProtocolSink *iface,
211         PROTOCOLDATA *pProtocolData)
212 {
213     ProtocolProxy *This = PROTSINK_THIS(iface);
214
215     TRACE("(%p)->(%p)\n", This, pProtocolData);
216
217     return IInternetProtocolSink_Switch(This->protocol_sink, pProtocolData);
218 }
219
220 static HRESULT WINAPI ProtocolProxySink_ReportProgress(IInternetProtocolSink *iface,
221         ULONG ulStatusCode, LPCWSTR szStatusText)
222 {
223     ProtocolProxy *This = PROTSINK_THIS(iface);
224
225     TRACE("(%p)->(%u %s)\n", This, ulStatusCode, debugstr_w(szStatusText));
226
227     switch(ulStatusCode) {
228     case BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE:
229         IInternetProtocolSink_ReportProgress(This->protocol_sink, BINDSTATUS_MIMETYPEAVAILABLE, szStatusText);
230         break;
231     default:
232         IInternetProtocolSink_ReportProgress(This->protocol_sink, ulStatusCode, szStatusText);
233     }
234
235     return S_OK;
236 }
237
238 static HRESULT WINAPI ProtocolProxySink_ReportData(IInternetProtocolSink *iface,
239         DWORD grfBSCF, ULONG ulProgress, ULONG ulProgressMax)
240 {
241     ProtocolProxy *This = PROTSINK_THIS(iface);
242
243     TRACE("(%p)->(%d %u %u)\n", This, grfBSCF, ulProgress, ulProgressMax);
244
245     return IInternetProtocolSink_ReportData(This->protocol_sink, grfBSCF, ulProgress, ulProgressMax);
246 }
247
248 static HRESULT WINAPI ProtocolProxySink_ReportResult(IInternetProtocolSink *iface,
249         HRESULT hrResult, DWORD dwError, LPCWSTR szResult)
250 {
251     ProtocolProxy *This = PROTSINK_THIS(iface);
252
253     TRACE("(%p)->(%08x %d %s)\n", This, hrResult, dwError, debugstr_w(szResult));
254
255     return IInternetProtocolSink_ReportResult(This->protocol_sink, hrResult, dwError, szResult);
256 }
257
258 #undef PROTSINK_THIS
259
260 static const IInternetProtocolSinkVtbl InternetProtocolSinkVtbl = {
261     ProtocolProxySink_QueryInterface,
262     ProtocolProxySink_AddRef,
263     ProtocolProxySink_Release,
264     ProtocolProxySink_Switch,
265     ProtocolProxySink_ReportProgress,
266     ProtocolProxySink_ReportData,
267     ProtocolProxySink_ReportResult
268 };
269
270 HRESULT create_protocol_proxy(IInternetProtocol *protocol, IInternetProtocolSink *protocol_sink, ProtocolProxy **ret)
271 {
272     ProtocolProxy *sink;
273
274     sink = heap_alloc(sizeof(ProtocolProxy));
275     if(!sink)
276         return E_OUTOFMEMORY;
277
278     sink->lpIInternetProtocolVtbl     = &ProtocolProxyVtbl;
279     sink->lpIInternetProtocolSinkVtbl = &InternetProtocolSinkVtbl;
280     sink->ref = 1;
281
282     IInternetProtocol_AddRef(protocol);
283     sink->protocol = protocol;
284
285     IInternetProtocolSink_AddRef(protocol_sink);
286     sink->protocol_sink = protocol_sink;
287
288     *ret = sink;
289     return S_OK;
290 }