urlmon: Replace also bind_info in set_binding_sink.
[wine] / dlls / urlmon / gopher.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 typedef struct {
25     Protocol base;
26
27     const IInternetProtocolVtbl  *lpIInternetProtocolVtbl;
28     const IInternetPriorityVtbl  *lpInternetPriorityVtbl;
29
30     LONG ref;
31 } GopherProtocol;
32
33 #define PRIORITY(x)  ((IInternetPriority*)  &(x)->lpInternetPriorityVtbl)
34
35 #define ASYNCPROTOCOL_THIS(iface) DEFINE_THIS2(GopherProtocol, base, iface)
36
37 static HRESULT GopherProtocol_open_request(Protocol *prot, IUri *uri, DWORD request_flags,
38         HINTERNET internet_session, IInternetBindInfo *bind_info)
39 {
40     GopherProtocol *This = ASYNCPROTOCOL_THIS(prot);
41     BSTR url;
42     HRESULT hres;
43
44     hres = IUri_GetAbsoluteUri(uri, &url);
45     if(FAILED(hres))
46         return hres;
47
48     This->base.request = InternetOpenUrlW(internet_session, url, NULL, 0,
49             request_flags, (DWORD_PTR)&This->base);
50     SysFreeString(url);
51     if (!This->base.request && GetLastError() != ERROR_IO_PENDING) {
52         WARN("InternetOpenUrl failed: %d\n", GetLastError());
53         return INET_E_RESOURCE_NOT_FOUND;
54     }
55
56     return S_OK;
57 }
58
59 static HRESULT GopherProtocol_start_downloading(Protocol *prot)
60 {
61     return S_OK;
62 }
63
64 static void GopherProtocol_close_connection(Protocol *prot)
65 {
66 }
67
68 #undef ASYNCPROTOCOL_THIS
69
70 static const ProtocolVtbl AsyncProtocolVtbl = {
71     GopherProtocol_open_request,
72     GopherProtocol_start_downloading,
73     GopherProtocol_close_connection
74 };
75
76 #define PROTOCOL_THIS(iface) DEFINE_THIS(GopherProtocol, IInternetProtocol, iface)
77
78 static HRESULT WINAPI GopherProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
79 {
80     GopherProtocol *This = PROTOCOL_THIS(iface);
81
82     *ppv = NULL;
83     if(IsEqualGUID(&IID_IUnknown, riid)) {
84         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
85         *ppv = PROTOCOL(This);
86     }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
87         TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
88         *ppv = PROTOCOL(This);
89     }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
90         TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
91         *ppv = PROTOCOL(This);
92     }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
93         TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
94         *ppv = PRIORITY(This);
95     }
96
97     if(*ppv) {
98         IInternetProtocol_AddRef(iface);
99         return S_OK;
100     }
101
102     WARN("not supported interface %s\n", debugstr_guid(riid));
103     return E_NOINTERFACE;
104 }
105
106 static ULONG WINAPI GopherProtocol_AddRef(IInternetProtocol *iface)
107 {
108     GopherProtocol *This = PROTOCOL_THIS(iface);
109     LONG ref = InterlockedIncrement(&This->ref);
110     TRACE("(%p) ref=%d\n", This, ref);
111     return ref;
112 }
113
114 static ULONG WINAPI GopherProtocol_Release(IInternetProtocol *iface)
115 {
116     GopherProtocol *This = PROTOCOL_THIS(iface);
117     LONG ref = InterlockedDecrement(&This->ref);
118
119     TRACE("(%p) ref=%d\n", This, ref);
120
121     if(!ref) {
122         heap_free(This);
123
124         URLMON_UnlockModule();
125     }
126
127     return ref;
128 }
129
130 static HRESULT WINAPI GopherProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
131         IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
132         DWORD grfPI, HANDLE_PTR dwReserved)
133 {
134     GopherProtocol *This = PROTOCOL_THIS(iface);
135     IUri *uri;
136     HRESULT hres;
137
138     TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
139           pOIBindInfo, grfPI, dwReserved);
140
141     hres = CreateUri(szUrl, 0, 0, &uri);
142     if(FAILED(hres))
143         return hres;
144
145     hres = protocol_start(&This->base, PROTOCOL(This), uri, pOIProtSink, pOIBindInfo);
146
147     IUri_Release(uri);
148     return hres;
149 }
150
151 static HRESULT WINAPI GopherProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
152 {
153     GopherProtocol *This = PROTOCOL_THIS(iface);
154
155     TRACE("(%p)->(%p)\n", This, pProtocolData);
156
157     return protocol_continue(&This->base, pProtocolData);
158 }
159
160 static HRESULT WINAPI GopherProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
161         DWORD dwOptions)
162 {
163     GopherProtocol *This = PROTOCOL_THIS(iface);
164     FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
165     return E_NOTIMPL;
166 }
167
168 static HRESULT WINAPI GopherProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
169 {
170     GopherProtocol *This = PROTOCOL_THIS(iface);
171
172     TRACE("(%p)->(%08x)\n", This, dwOptions);
173
174     protocol_close_connection(&This->base);
175     return S_OK;
176 }
177
178 static HRESULT WINAPI GopherProtocol_Suspend(IInternetProtocol *iface)
179 {
180     GopherProtocol *This = PROTOCOL_THIS(iface);
181     FIXME("(%p)\n", This);
182     return E_NOTIMPL;
183 }
184
185 static HRESULT WINAPI GopherProtocol_Resume(IInternetProtocol *iface)
186 {
187     GopherProtocol *This = PROTOCOL_THIS(iface);
188     FIXME("(%p)\n", This);
189     return E_NOTIMPL;
190 }
191
192 static HRESULT WINAPI GopherProtocol_Read(IInternetProtocol *iface, void *pv,
193         ULONG cb, ULONG *pcbRead)
194 {
195     GopherProtocol *This = PROTOCOL_THIS(iface);
196
197     TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
198
199     return protocol_read(&This->base, pv, cb, pcbRead);
200 }
201
202 static HRESULT WINAPI GopherProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
203         DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
204 {
205     GopherProtocol *This = PROTOCOL_THIS(iface);
206     FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
207     return E_NOTIMPL;
208 }
209
210 static HRESULT WINAPI GopherProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
211 {
212     GopherProtocol *This = PROTOCOL_THIS(iface);
213
214     TRACE("(%p)->(%08x)\n", This, dwOptions);
215
216     return protocol_lock_request(&This->base);
217 }
218
219 static HRESULT WINAPI GopherProtocol_UnlockRequest(IInternetProtocol *iface)
220 {
221     GopherProtocol *This = PROTOCOL_THIS(iface);
222
223     TRACE("(%p)\n", This);
224
225     return protocol_unlock_request(&This->base);
226 }
227
228 #undef PROTOCOL_THIS
229
230 static const IInternetProtocolVtbl GopherProtocolVtbl = {
231     GopherProtocol_QueryInterface,
232     GopherProtocol_AddRef,
233     GopherProtocol_Release,
234     GopherProtocol_Start,
235     GopherProtocol_Continue,
236     GopherProtocol_Abort,
237     GopherProtocol_Terminate,
238     GopherProtocol_Suspend,
239     GopherProtocol_Resume,
240     GopherProtocol_Read,
241     GopherProtocol_Seek,
242     GopherProtocol_LockRequest,
243     GopherProtocol_UnlockRequest
244 };
245
246 #define PRIORITY_THIS(iface) DEFINE_THIS(GopherProtocol, InternetPriority, iface)
247
248 static HRESULT WINAPI GopherPriority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
249 {
250     GopherProtocol *This = PRIORITY_THIS(iface);
251     return IInternetProtocol_QueryInterface(PROTOCOL(This), riid, ppv);
252 }
253
254 static ULONG WINAPI GopherPriority_AddRef(IInternetPriority *iface)
255 {
256     GopherProtocol *This = PRIORITY_THIS(iface);
257     return IInternetProtocol_AddRef(PROTOCOL(This));
258 }
259
260 static ULONG WINAPI GopherPriority_Release(IInternetPriority *iface)
261 {
262     GopherProtocol *This = PRIORITY_THIS(iface);
263     return IInternetProtocol_Release(PROTOCOL(This));
264 }
265
266 static HRESULT WINAPI GopherPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
267 {
268     GopherProtocol *This = PRIORITY_THIS(iface);
269
270     TRACE("(%p)->(%d)\n", This, nPriority);
271
272     This->base.priority = nPriority;
273     return S_OK;
274 }
275
276 static HRESULT WINAPI GopherPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
277 {
278     GopherProtocol *This = PRIORITY_THIS(iface);
279
280     TRACE("(%p)->(%p)\n", This, pnPriority);
281
282     *pnPriority = This->base.priority;
283     return S_OK;
284 }
285
286 #undef PRIORITY_THIS
287
288 static const IInternetPriorityVtbl GopherPriorityVtbl = {
289     GopherPriority_QueryInterface,
290     GopherPriority_AddRef,
291     GopherPriority_Release,
292     GopherPriority_SetPriority,
293     GopherPriority_GetPriority
294 };
295
296 HRESULT GopherProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
297 {
298     GopherProtocol *ret;
299
300     TRACE("(%p %p)\n", pUnkOuter, ppobj);
301
302     URLMON_LockModule();
303
304     ret = heap_alloc_zero(sizeof(GopherProtocol));
305
306     ret->base.vtbl = &AsyncProtocolVtbl;
307     ret->lpIInternetProtocolVtbl = &GopherProtocolVtbl;
308     ret->lpInternetPriorityVtbl  = &GopherPriorityVtbl;
309     ret->ref = 1;
310
311     *ppobj = PROTOCOL(ret);
312
313     return S_OK;
314 }