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