2 * Copyright 2008 Piotr Caban
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.
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.
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
20 #define NONAMELESSUNION
35 #include "wine/debug.h"
37 #include "msxml_private.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
42 const struct IBindStatusCallbackVtbl *lpVtbl;
47 HRESULT (*onDataAvailable)(void*,char*,DWORD);
53 static inline bsc_t *impl_from_IBindStatusCallback( IBindStatusCallback *iface )
55 return (bsc_t *)((char*)iface - FIELD_OFFSET(bsc_t, lpVtbl));
58 static HRESULT WINAPI bsc_QueryInterface(
59 IBindStatusCallback *iface,
63 if (IsEqualGUID(riid, &IID_IUnknown) ||
64 IsEqualGUID(riid, &IID_IBindStatusCallback))
66 IBindStatusCallback_AddRef( iface );
71 TRACE("interface %s not implemented\n", debugstr_guid(riid));
75 static ULONG WINAPI bsc_AddRef(
76 IBindStatusCallback *iface )
78 bsc_t *This = impl_from_IBindStatusCallback(iface);
79 LONG ref = InterlockedIncrement(&This->ref);
81 TRACE("(%p) ref=%d\n", This, ref);
86 static ULONG WINAPI bsc_Release(
87 IBindStatusCallback *iface )
89 bsc_t *This = impl_from_IBindStatusCallback(iface);
90 LONG ref = InterlockedDecrement(&This->ref);
92 TRACE("(%p) ref=%d\n", This, ref);
95 if (This->binding) IBinding_Release(This->binding);
96 if (This->memstream) IStream_Release(This->memstream);
103 static HRESULT WINAPI bsc_OnStartBinding(
104 IBindStatusCallback* iface,
108 bsc_t *This = impl_from_IBindStatusCallback(iface);
111 TRACE("(%p)->(%x %p)\n", This, dwReserved, pib);
114 IBinding_AddRef(pib);
116 hr = CreateStreamOnHGlobal(NULL, TRUE, &This->memstream);
123 static HRESULT WINAPI bsc_GetPriority(
124 IBindStatusCallback* iface,
130 static HRESULT WINAPI bsc_OnLowResource(
131 IBindStatusCallback* iface,
137 static HRESULT WINAPI bsc_OnProgress(
138 IBindStatusCallback* iface,
142 LPCWSTR szStatusText)
147 static HRESULT WINAPI bsc_OnStopBinding(
148 IBindStatusCallback* iface,
152 bsc_t *This = impl_from_IBindStatusCallback(iface);
155 TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
158 IBinding_Release(This->binding);
159 This->binding = NULL;
162 if(This->obj && SUCCEEDED(hresult)) {
164 hr = GetHGlobalFromStream(This->memstream, &hglobal);
167 DWORD len = GlobalSize(hglobal);
168 char *ptr = GlobalLock(hglobal);
170 hr = This->onDataAvailable(This->obj, ptr, len);
172 GlobalUnlock(hglobal);
179 static HRESULT WINAPI bsc_GetBindInfo(
180 IBindStatusCallback* iface,
184 *grfBINDF = BINDF_GETNEWESTVERSION|BINDF_PULLDATA|BINDF_RESYNCHRONIZE|BINDF_PRAGMA_NO_CACHE;
189 static HRESULT WINAPI bsc_OnDataAvailable(
190 IBindStatusCallback* iface,
193 FORMATETC* pformatetc,
196 bsc_t *This = impl_from_IBindStatusCallback(iface);
201 TRACE("(%p)->(%x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
205 hr = IStream_Read(pstgmed->u.pstm, buf, sizeof(buf), &read);
209 hr = IStream_Write(This->memstream, buf, read, &written);
210 } while(SUCCEEDED(hr) && written != 0 && read != 0);
215 static HRESULT WINAPI bsc_OnObjectAvailable(
216 IBindStatusCallback* iface,
223 static const struct IBindStatusCallbackVtbl bsc_vtbl =
235 bsc_OnObjectAvailable
238 HRESULT bind_url(LPCWSTR url, HRESULT (*onDataAvailable)(void*,char*,DWORD), void *obj, bsc_t **ret)
240 WCHAR fileUrl[INTERNET_MAX_URL_LENGTH];
245 TRACE("%s\n", debugstr_w(url));
249 WCHAR fullpath[MAX_PATH];
250 DWORD needed = sizeof(fileUrl)/sizeof(WCHAR);
252 if(!PathSearchAndQualifyW(url, fullpath, sizeof(fullpath)/sizeof(WCHAR)))
254 WARN("can't find path\n");
258 if(FAILED(UrlCreateFromPathW(url, fileUrl, &needed, 0)))
260 ERR("can't create url from path\n");
266 hr = CreateBindCtx(0, &pbc);
270 bsc = heap_alloc(sizeof(bsc_t));
272 bsc->lpVtbl = &bsc_vtbl;
275 bsc->onDataAvailable = onDataAvailable;
277 bsc->memstream = NULL;
279 hr = RegisterBindStatusCallback(pbc, (IBindStatusCallback*)&bsc->lpVtbl, NULL, 0);
284 hr = CreateURLMoniker(NULL, url, &moniker);
288 hr = IMoniker_BindToStorage(moniker, pbc, NULL, &IID_IStream, (LPVOID*)&stream);
289 IMoniker_Release(moniker);
291 IStream_Release(stream);
293 IBindCtx_Release(pbc);
298 IBindStatusCallback_Release((IBindStatusCallback*)&bsc->lpVtbl);
306 void detach_bsc(bsc_t *bsc)
309 IBinding_Abort(bsc->binding);
312 IBindStatusCallback_Release((IBindStatusCallback*)&bsc->lpVtbl);