msxml3: Fix building on Leopard.
[wine] / dlls / urlmon / download.c
1 /*
2  * Copyright 2008 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     const IBindStatusCallbackVtbl  *lpBindStatusCallbackVtbl;
26     const IServiceProviderVtbl     *lpServiceProviderVtbl;
27
28     LONG ref;
29
30     IBindStatusCallback *callback;
31     LPWSTR file_name;
32     LPWSTR cache_file;
33 } DownloadBSC;
34
35 #define STATUSCLB(x)     ((IBindStatusCallback*)  &(x)->lpBindStatusCallbackVtbl)
36 #define SERVPROV(x)      ((IServiceProvider*)     &(x)->lpServiceProviderVtbl)
37
38 #define STATUSCLB_THIS(iface) DEFINE_THIS(DownloadBSC, BindStatusCallback, iface)
39
40 static HRESULT WINAPI DownloadBSC_QueryInterface(IBindStatusCallback *iface,
41         REFIID riid, void **ppv)
42 {
43     DownloadBSC *This = STATUSCLB_THIS(iface);
44
45     *ppv = NULL;
46
47     if(IsEqualGUID(&IID_IUnknown, riid)) {
48         TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
49         *ppv = STATUSCLB(This);
50     }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
51         TRACE("(%p)->(IID_IBindStatusCallback, %p)\n", This, ppv);
52         *ppv = STATUSCLB(This);
53     }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
54         TRACE("(%p)->(IID_IServiceProvider, %p)\n", This, ppv);
55         *ppv = SERVPROV(This);
56     }
57
58     if(*ppv) {
59         IBindStatusCallback_AddRef((IUnknown*)*ppv);
60         return S_OK;
61     }
62
63     TRACE("Unsupported riid = %s\n", debugstr_guid(riid));
64     return E_NOINTERFACE;
65 }
66
67 static ULONG WINAPI DownloadBSC_AddRef(IBindStatusCallback *iface)
68 {
69     DownloadBSC *This = STATUSCLB_THIS(iface);
70     LONG ref = InterlockedIncrement(&This->ref);
71
72     TRACE("(%p) ref = %d\n", This, ref);
73
74     return ref;
75 }
76
77 static ULONG WINAPI DownloadBSC_Release(IBindStatusCallback *iface)
78 {
79     DownloadBSC *This = STATUSCLB_THIS(iface);
80     LONG ref = InterlockedDecrement(&This->ref);
81
82     TRACE("(%p) ref = %d\n", This, ref);
83
84     if(!ref) {
85         if(This->callback)
86             IBindStatusCallback_Release(This->callback);
87         heap_free(This->file_name);
88         heap_free(This->cache_file);
89         heap_free(This);
90     }
91
92     return ref;
93 }
94
95 static HRESULT WINAPI DownloadBSC_OnStartBinding(IBindStatusCallback *iface,
96         DWORD dwReserved, IBinding *pbind)
97 {
98     DownloadBSC *This = STATUSCLB_THIS(iface);
99
100     TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
101
102     if(This->callback)
103         IBindStatusCallback_OnStartBinding(This->callback, dwReserved, pbind);
104
105     return S_OK;
106 }
107
108 static HRESULT WINAPI DownloadBSC_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
109 {
110     DownloadBSC *This = STATUSCLB_THIS(iface);
111     FIXME("(%p)->(%p)\n", This, pnPriority);
112     return E_NOTIMPL;
113 }
114
115 static HRESULT WINAPI DownloadBSC_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
116 {
117     DownloadBSC *This = STATUSCLB_THIS(iface);
118     FIXME("(%p)->(%d)\n", This, reserved);
119     return E_NOTIMPL;
120 }
121
122 static HRESULT on_progress(DownloadBSC *This, ULONG progress, ULONG progress_max, ULONG status_code, LPCWSTR status_text)
123 {
124     HRESULT hres;
125
126     if(!This->callback)
127         return S_OK;
128
129     hres = IBindStatusCallback_OnProgress(This->callback, progress, progress_max, status_code, status_text);
130     return hres;
131 }
132
133 static HRESULT WINAPI DownloadBSC_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
134         ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
135 {
136     DownloadBSC *This = STATUSCLB_THIS(iface);
137     HRESULT hres = S_OK;
138
139     TRACE("%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
140             debugstr_w(szStatusText));
141
142     switch(ulStatusCode) {
143     case BINDSTATUS_CONNECTING:
144     case BINDSTATUS_BEGINDOWNLOADDATA:
145     case BINDSTATUS_DOWNLOADINGDATA:
146     case BINDSTATUS_ENDDOWNLOADDATA:
147     case BINDSTATUS_SENDINGREQUEST:
148     case BINDSTATUS_MIMETYPEAVAILABLE:
149         hres = on_progress(This, ulProgress, ulProgressMax, ulStatusCode, szStatusText);
150         break;
151
152     case BINDSTATUS_CACHEFILENAMEAVAILABLE:
153         hres = on_progress(This, ulProgress, ulProgressMax, ulStatusCode, szStatusText);
154         This->cache_file = heap_strdupW(szStatusText);
155         break;
156
157     case BINDSTATUS_FINDINGRESOURCE: /* FIXME */
158         break;
159
160     default:
161         FIXME("Unsupported status %u\n", ulStatusCode);
162     }
163
164     return hres;
165 }
166
167 static HRESULT WINAPI DownloadBSC_OnStopBinding(IBindStatusCallback *iface,
168         HRESULT hresult, LPCWSTR szError)
169 {
170     DownloadBSC *This = STATUSCLB_THIS(iface);
171
172     TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
173
174     if(This->file_name) {
175         if(This->cache_file) {
176             BOOL b;
177
178             b = CopyFileW(This->cache_file, This->file_name, FALSE);
179             if(!b)
180                 FIXME("CopyFile failed: %u\n", GetLastError());
181         }else {
182             FIXME("No cache file\n");
183         }
184     }
185
186     if(This->callback)
187         IBindStatusCallback_OnStopBinding(This->callback, hresult, szError);
188
189     return S_OK;
190 }
191
192 static HRESULT WINAPI DownloadBSC_GetBindInfo(IBindStatusCallback *iface,
193         DWORD *grfBINDF, BINDINFO *pbindinfo)
194 {
195     DownloadBSC *This = STATUSCLB_THIS(iface);
196     DWORD bindf = 0;
197
198     TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
199
200     if(This->callback) {
201         BINDINFO bindinfo;
202         HRESULT hres;
203
204         memset(&bindinfo, 0, sizeof(bindinfo));
205         bindinfo.cbSize = sizeof(bindinfo);
206
207         hres = IBindStatusCallback_GetBindInfo(This->callback, &bindf, &bindinfo);
208         if(SUCCEEDED(hres))
209             ReleaseBindInfo(&bindinfo);
210     }
211
212     *grfBINDF = BINDF_PULLDATA | BINDF_NEEDFILE | (bindf & BINDF_ENFORCERESTRICTED);
213     return S_OK;
214 }
215
216 static HRESULT WINAPI DownloadBSC_OnDataAvailable(IBindStatusCallback *iface,
217         DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
218 {
219     DownloadBSC *This = STATUSCLB_THIS(iface);
220
221     TRACE("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
222
223     return S_OK;
224 }
225
226 static HRESULT WINAPI DownloadBSC_OnObjectAvailable(IBindStatusCallback *iface,
227         REFIID riid, IUnknown *punk)
228 {
229     DownloadBSC *This = STATUSCLB_THIS(iface);
230     FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
231     return E_NOTIMPL;
232 }
233
234 #undef STATUSCLB_THIS
235
236 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
237     DownloadBSC_QueryInterface,
238     DownloadBSC_AddRef,
239     DownloadBSC_Release,
240     DownloadBSC_OnStartBinding,
241     DownloadBSC_GetPriority,
242     DownloadBSC_OnLowResource,
243     DownloadBSC_OnProgress,
244     DownloadBSC_OnStopBinding,
245     DownloadBSC_GetBindInfo,
246     DownloadBSC_OnDataAvailable,
247     DownloadBSC_OnObjectAvailable
248 };
249
250 #define SERVPROV_THIS(iface) DEFINE_THIS(DownloadBSC, ServiceProvider, iface)
251
252 static HRESULT WINAPI DwlServiceProvider_QueryInterface(IServiceProvider *iface,
253         REFIID riid, void **ppv)
254 {
255     DownloadBSC *This = SERVPROV_THIS(iface);
256     return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
257 }
258
259 static ULONG WINAPI DwlServiceProvider_AddRef(IServiceProvider *iface)
260 {
261     DownloadBSC *This = SERVPROV_THIS(iface);
262     return IBindStatusCallback_AddRef(STATUSCLB(This));
263 }
264
265 static ULONG WINAPI DwlServiceProvider_Release(IServiceProvider *iface)
266 {
267     DownloadBSC *This = SERVPROV_THIS(iface);
268     return IBindStatusCallback_Release(STATUSCLB(This));
269 }
270
271 static HRESULT WINAPI DwlServiceProvider_QueryService(IServiceProvider *iface,
272         REFGUID guidService, REFIID riid, void **ppv)
273 {
274     DownloadBSC *This = SERVPROV_THIS(iface);
275     IServiceProvider *serv_prov;
276     HRESULT hres;
277
278     TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
279
280     if(!This->callback)
281         return E_NOINTERFACE;
282
283     hres = IBindStatusCallback_QueryInterface(This->callback, riid, ppv);
284     if(SUCCEEDED(hres))
285         return S_OK;
286
287     hres = IBindStatusCallback_QueryInterface(This->callback, &IID_IServiceProvider, (void**)&serv_prov);
288     if(SUCCEEDED(hres)) {
289         hres = IServiceProvider_QueryService(serv_prov, guidService, riid, ppv);
290         IServiceProvider_Release(serv_prov);
291         return hres;
292     }
293
294     return E_NOINTERFACE;
295 }
296
297 #undef SERVPROV_THIS
298
299 static const IServiceProviderVtbl ServiceProviderVtbl = {
300     DwlServiceProvider_QueryInterface,
301     DwlServiceProvider_AddRef,
302     DwlServiceProvider_Release,
303     DwlServiceProvider_QueryService
304 };
305
306 static HRESULT DownloadBSC_Create(IBindStatusCallback *callback, LPCWSTR file_name, IBindStatusCallback **ret_callback)
307 {
308     DownloadBSC *ret = heap_alloc(sizeof(*ret));
309
310     ret->lpBindStatusCallbackVtbl = &BindStatusCallbackVtbl;
311     ret->lpServiceProviderVtbl    = &ServiceProviderVtbl;
312     ret->ref = 1;
313     ret->file_name = heap_strdupW(file_name);
314     ret->cache_file = NULL;
315
316     if(callback)
317         IBindStatusCallback_AddRef(callback);
318     ret->callback = callback;
319
320     *ret_callback = STATUSCLB(ret);
321     return S_OK;
322 }
323
324 HRESULT create_default_callback(IBindStatusCallback **ret)
325 {
326     IBindStatusCallback *callback;
327     HRESULT hres;
328
329     hres = DownloadBSC_Create(NULL, NULL, &callback);
330     if(FAILED(hres))
331         return hres;
332
333     hres = wrap_callback(callback, ret);
334     IBindStatusCallback_Release(callback);
335     return hres;
336 }
337
338 /***********************************************************************
339  *           URLDownloadToFileW (URLMON.@)
340  *
341  * Downloads URL szURL to file szFileName and call lpfnCB callback to
342  * report progress.
343  *
344  * PARAMS
345  *  pCaller    [I] controlling IUnknown interface.
346  *  szURL      [I] URL of the file to download
347  *  szFileName [I] file name to store the content of the URL
348  *  dwReserved [I] reserved - set to 0
349  *  lpfnCB     [I] callback for progress report
350  *
351  * RETURNS
352  *  S_OK on success
353  */
354 HRESULT WINAPI URLDownloadToFileW(LPUNKNOWN pCaller, LPCWSTR szURL, LPCWSTR szFileName,
355         DWORD dwReserved, LPBINDSTATUSCALLBACK lpfnCB)
356 {
357     IBindStatusCallback *callback;
358     IUnknown *unk;
359     IMoniker *mon;
360     IBindCtx *bindctx;
361     HRESULT hres;
362
363     TRACE("(%p %s %s %d %p)\n", pCaller, debugstr_w(szURL), debugstr_w(szFileName), dwReserved, lpfnCB);
364
365     if(pCaller)
366         FIXME("pCaller not supported\n");
367
368     hres = DownloadBSC_Create(lpfnCB, szFileName, &callback);
369     if(FAILED(hres))
370         return hres;
371
372     hres = CreateAsyncBindCtx(0, callback, NULL, &bindctx);
373     IBindStatusCallback_Release(callback);
374     if(FAILED(hres))
375         return hres;
376
377     hres = CreateURLMoniker(NULL, szURL, &mon);
378     if(FAILED(hres)) {
379         IBindCtx_Release(bindctx);
380         return hres;
381     }
382
383     hres = IMoniker_BindToStorage(mon, bindctx, NULL, &IID_IUnknown, (void**)&unk);
384     IMoniker_Release(mon);
385     IBindCtx_Release(bindctx);
386
387     if(unk)
388         IUnknown_Release(unk);
389
390     return hres == MK_S_ASYNCHRONOUS ? S_OK : hres;
391 }
392
393 /***********************************************************************
394  *           URLDownloadToFileA (URLMON.@)
395  *
396  * Downloads URL szURL to rile szFileName and call lpfnCB callback to
397  * report progress.
398  *
399  * PARAMS
400  *  pCaller    [I] controlling IUnknown interface.
401  *  szURL      [I] URL of the file to download
402  *  szFileName [I] file name to store the content of the URL
403  *  dwReserved [I] reserved - set to 0
404  *  lpfnCB     [I] callback for progress report
405  *
406  * RETURNS
407  *  S_OK on success
408  */
409 HRESULT WINAPI URLDownloadToFileA(LPUNKNOWN pCaller, LPCSTR szURL, LPCSTR szFileName, DWORD dwReserved,
410         LPBINDSTATUSCALLBACK lpfnCB)
411 {
412     LPWSTR urlW, file_nameW;
413     HRESULT hres;
414
415     TRACE("(%p %s %s %d %p)\n", pCaller, debugstr_a(szURL), debugstr_a(szFileName), dwReserved, lpfnCB);
416
417     urlW = heap_strdupAtoW(szURL);
418     file_nameW = heap_strdupAtoW(szFileName);
419
420     hres = URLDownloadToFileW(pCaller, urlW, file_nameW, dwReserved, lpfnCB);
421
422     heap_free(urlW);
423     heap_free(file_nameW);
424
425     return hres;
426 }