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