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