bsc.c 8.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright 2008 Piotr Caban
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#define COBJMACROS
#define NONAMELESSUNION

#include "config.h"

#include <stdarg.h>
25 26 27 28 29
#ifdef HAVE_LIBXML2
# include <libxml/parser.h>
# include <libxml/xmlerror.h>
#endif

30 31 32 33
#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "ole2.h"
34
#include "msxml6.h"
35 36 37 38 39 40 41 42 43 44 45 46
#include "wininet.h"
#include "urlmon.h"
#include "winreg.h"
#include "shlwapi.h"

#include "wine/debug.h"

#include "msxml_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(msxml);

struct bsc_t {
47
    IBindStatusCallback IBindStatusCallback_iface;
48 49 50 51 52 53 54 55

    LONG ref;

    void *obj;
    HRESULT (*onDataAvailable)(void*,char*,DWORD);

    IBinding *binding;
    IStream *memstream;
56
    HRESULT hres;
57 58 59 60
};

static inline bsc_t *impl_from_IBindStatusCallback( IBindStatusCallback *iface )
{
61
    return CONTAINING_RECORD(iface, bsc_t, IBindStatusCallback_iface);
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
}

static HRESULT WINAPI bsc_QueryInterface(
    IBindStatusCallback *iface,
    REFIID riid,
    LPVOID *ppobj )
{
    if (IsEqualGUID(riid, &IID_IUnknown) ||
        IsEqualGUID(riid, &IID_IBindStatusCallback))
    {
        IBindStatusCallback_AddRef( iface );
        *ppobj = iface;
        return S_OK;
    }

77
    TRACE("interface %s not implemented\n", debugstr_guid(riid));
78
    *ppobj = NULL;
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    return E_NOINTERFACE;
}

static ULONG WINAPI bsc_AddRef(
    IBindStatusCallback *iface )
{
    bsc_t *This = impl_from_IBindStatusCallback(iface);
    LONG ref = InterlockedIncrement(&This->ref);

    TRACE("(%p) ref=%d\n", This, ref);

    return ref;
}

static ULONG WINAPI bsc_Release(
    IBindStatusCallback *iface )
{
    bsc_t *This = impl_from_IBindStatusCallback(iface);
    LONG ref = InterlockedDecrement(&This->ref);

    TRACE("(%p) ref=%d\n", This, ref);

    if(!ref) {
102 103 104
        if (This->binding)   IBinding_Release(This->binding);
        if (This->memstream) IStream_Release(This->memstream);
        heap_free(This);
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    }

    return ref;
}

static HRESULT WINAPI bsc_OnStartBinding(
        IBindStatusCallback* iface,
        DWORD dwReserved,
        IBinding* pib)
{
    bsc_t *This = impl_from_IBindStatusCallback(iface);
    HRESULT hr;

    TRACE("(%p)->(%x %p)\n", This, dwReserved, pib);

    This->binding = pib;
121
    IBinding_AddRef(pib);
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

    hr = CreateStreamOnHGlobal(NULL, TRUE, &This->memstream);
    if(FAILED(hr))
        return hr;

    return S_OK;
}

static HRESULT WINAPI bsc_GetPriority(
        IBindStatusCallback* iface,
        LONG* pnPriority)
{
    return S_OK;
}

static HRESULT WINAPI bsc_OnLowResource(
        IBindStatusCallback* iface,
        DWORD reserved)
{
    return S_OK;
}

static HRESULT WINAPI bsc_OnProgress(
        IBindStatusCallback* iface,
        ULONG ulProgress,
        ULONG ulProgressMax,
        ULONG ulStatusCode,
        LPCWSTR szStatusText)
{
    return S_OK;
}

static HRESULT WINAPI bsc_OnStopBinding(
        IBindStatusCallback* iface,
        HRESULT hresult,
        LPCWSTR szError)
{
    bsc_t *This = impl_from_IBindStatusCallback(iface);
    HRESULT hr = S_OK;

    TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));

    if(This->binding) {
        IBinding_Release(This->binding);
        This->binding = NULL;
    }

    if(This->obj && SUCCEEDED(hresult)) {
        HGLOBAL hglobal;
        hr = GetHGlobalFromStream(This->memstream, &hglobal);
        if(SUCCEEDED(hr))
        {
            DWORD len = GlobalSize(hglobal);
            char *ptr = GlobalLock(hglobal);

177
            This->hres = This->onDataAvailable(This->obj, ptr, len);
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

            GlobalUnlock(hglobal);
        }
    }

    return hr;
}

static HRESULT WINAPI bsc_GetBindInfo(
        IBindStatusCallback* iface,
        DWORD* grfBINDF,
        BINDINFO* pbindinfo)
{
    *grfBINDF = BINDF_GETNEWESTVERSION|BINDF_PULLDATA|BINDF_RESYNCHRONIZE|BINDF_PRAGMA_NO_CACHE;

    return S_OK;
}

static HRESULT WINAPI bsc_OnDataAvailable(
        IBindStatusCallback* iface,
        DWORD grfBSCF,
        DWORD dwSize,
        FORMATETC* pformatetc,
        STGMEDIUM* pstgmed)
{
    bsc_t *This = impl_from_IBindStatusCallback(iface);
    BYTE buf[4096];
    DWORD read, written;
    HRESULT hr;

    TRACE("(%p)->(%x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);

    do
    {
        hr = IStream_Read(pstgmed->u.pstm, buf, sizeof(buf), &read);
        if(FAILED(hr))
            break;

        hr = IStream_Write(This->memstream, buf, read, &written);
    } while(SUCCEEDED(hr) && written != 0 && read != 0);

    return S_OK;
}

static HRESULT WINAPI bsc_OnObjectAvailable(
        IBindStatusCallback* iface,
        REFIID riid,
        IUnknown* punk)
{
    return S_OK;
}

static const struct IBindStatusCallbackVtbl bsc_vtbl =
{
    bsc_QueryInterface,
    bsc_AddRef,
    bsc_Release,
    bsc_OnStartBinding,
    bsc_GetPriority,
    bsc_OnLowResource,
    bsc_OnProgress,
    bsc_OnStopBinding,
    bsc_GetBindInfo,
    bsc_OnDataAvailable,
    bsc_OnObjectAvailable
};

245
HRESULT create_uri(IUri *base, const WCHAR *url, IUri **uri)
246 247
{
    WCHAR fileUrl[INTERNET_MAX_URL_LENGTH];
248
    HRESULT hr;
249 250 251

    TRACE("%s\n", debugstr_w(url));

252
    if (!PathIsURLW(url))
253 254
    {
        WCHAR fullpath[MAX_PATH];
255
        DWORD needed = ARRAY_SIZE(fileUrl);
256

257 258 259 260
        lstrcpynW(fileUrl, url, ARRAY_SIZE(fileUrl));
        UrlUnescapeW(fileUrl, NULL, NULL, URL_UNESCAPE_INPLACE);

        if (!PathSearchAndQualifyW(fileUrl, fullpath, ARRAY_SIZE(fullpath)))
261 262 263 264 265
        {
            WARN("can't find path\n");
            return E_FAIL;
        }

266 267
        if (FAILED(UrlApplySchemeW(fullpath, fileUrl, &needed, URL_APPLY_GUESSSCHEME | URL_APPLY_GUESSFILE |
                URL_APPLY_DEFAULT)))
268
        {
269
            ERR("Failed to apply url scheme.\n");
270 271 272 273 274
            return E_FAIL;
        }
        url = fileUrl;
    }

275 276 277 278 279 280 281 282 283 284 285
    hr = CreateUri(url, Uri_CREATE_ALLOW_RELATIVE | Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME, 0, uri);
    if (hr == S_OK && base)
    {
        IUri *rebased_uri;

        hr = CoInternetCombineIUri(base, *uri, 0, &rebased_uri, 0);
        IUri_Release(*uri);
        *uri = rebased_uri;
    }

    return hr;
286 287 288 289 290 291 292 293 294
}

HRESULT create_moniker_from_url(LPCWSTR url, IMoniker **mon)
{
    HRESULT hr;
    IUri *uri;

    TRACE("%s\n", debugstr_w(url));

295
    if (FAILED(hr = create_uri(NULL, url, &uri)))
296 297 298 299 300
        return hr;

    hr = CreateURLMonikerEx2(NULL, uri, mon, 0);
    IUri_Release(uri);
    return hr;
301 302 303 304 305 306 307 308 309 310 311
}

HRESULT bind_url(IMoniker *mon, HRESULT (*onDataAvailable)(void*,char*,DWORD),
        void *obj, bsc_t **ret)
{
    bsc_t *bsc;
    IBindCtx *pbc;
    HRESULT hr;

    TRACE("%p\n", mon);

312 313 314 315
    hr = CreateBindCtx(0, &pbc);
    if(FAILED(hr))
        return hr;

316
    bsc = heap_alloc(sizeof(bsc_t));
317

318
    bsc->IBindStatusCallback_iface.lpVtbl = &bsc_vtbl;
319 320 321 322 323
    bsc->ref = 1;
    bsc->obj = obj;
    bsc->onDataAvailable = onDataAvailable;
    bsc->binding = NULL;
    bsc->memstream = NULL;
324
    bsc->hres = S_OK;
325

326
    hr = RegisterBindStatusCallback(pbc, &bsc->IBindStatusCallback_iface, NULL, 0);
327 328
    if(SUCCEEDED(hr))
    {
329 330 331 332
        IStream *stream;
        hr = IMoniker_BindToStorage(mon, pbc, NULL, &IID_IStream, (LPVOID*)&stream);
        if(stream)
            IStream_Release(stream);
333 334 335
        IBindCtx_Release(pbc);
    }

336 337
    if(FAILED(hr))
    {
338
        IBindStatusCallback_Release(&bsc->IBindStatusCallback_iface);
339 340 341
        bsc = NULL;
    }

342 343 344 345
    *ret = bsc;
    return hr;
}

346
HRESULT detach_bsc(bsc_t *bsc)
347
{
348 349
    HRESULT hres;

350 351 352 353
    if(bsc->binding)
        IBinding_Abort(bsc->binding);

    bsc->obj = NULL;
354
    hres = bsc->hres;
355
    IBindStatusCallback_Release(&bsc->IBindStatusCallback_iface);
356 357

    return hres;
358
}