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