binding.c 46.3 KB
Newer Older
1
/*
2
 * Copyright 2005-2007 Jacek Caban for CodeWeavers
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * 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
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 18 19
 */

#include "urlmon_main.h"
20
#include "winreg.h"
21
#include "shlwapi.h"
22

23 24 25 26
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(urlmon);

27
static WCHAR cbinding_contextW[] = {'C','B','i','n','d','i','n','g',' ','C','o','n','t','e','x','t',0};
28
static WCHAR bscb_holderW[] = { '_','B','S','C','B','_','H','o','l','d','e','r','_',0 };
29

30
typedef struct {
31
    IUnknown IUnknown_iface;
32 33 34

    LONG ref;

35
    IInternetProtocolEx *protocol;
36

37
    HANDLE file;
38
    HRESULT hres;
39 40

    LPWSTR cache_file;
41 42
} stgmed_buf_t;

43
typedef struct _stgmed_obj_t stgmed_obj_t;
44 45

typedef struct {
46 47
    void (*release)(stgmed_obj_t*);
    HRESULT (*fill_stgmed)(stgmed_obj_t*,STGMEDIUM*);
48
    HRESULT (*get_result)(stgmed_obj_t*,DWORD,void**);
49
} stgmed_obj_vtbl;
50

51 52 53
struct _stgmed_obj_t {
    const stgmed_obj_vtbl *vtbl;
};
54

55 56 57 58 59 60
typedef enum {
    BEFORE_DOWNLOAD,
    DOWNLOADING,
    END_DOWNLOAD
} download_state_t;

61 62 63
#define BINDING_LOCKED    0x0001
#define BINDING_STOPPED   0x0002
#define BINDING_OBJAVAIL  0x0004
64
#define BINDING_ABORTED   0x0008
65

66
typedef struct {
67
    IBinding              IBinding_iface;
68 69 70 71
    IInternetProtocolSink IInternetProtocolSink_iface;
    IInternetBindInfo     IInternetBindInfo_iface;
    IWinInetHttpInfo      IWinInetHttpInfo_iface;
    IServiceProvider      IServiceProvider_iface;
72 73 74 75

    LONG ref;

    IBindStatusCallback *callback;
76
    IServiceProvider *service_provider;
77

78 79
    BindProtocol *protocol;

80
    stgmed_buf_t *stgmed_buf;
81
    stgmed_obj_t *stgmed_obj;
82 83 84

    BINDINFO bindinfo;
    DWORD bindf;
85
    BOOL to_object;
86
    LPWSTR mime;
87
    UINT clipboard_format;
88
    LPWSTR url;
89
    LPWSTR redirect_url;
90
    IID iid;
91
    BOOL report_mime;
92
    BOOL use_cache_file;
93
    DWORD state;
94
    HRESULT hres;
95
    download_state_t download_state;
96 97 98
    IUnknown *obj;
    IMoniker *mon;
    IBindCtx *bctx;
99
    HWND notif_hwnd;
100

101
    CRITICAL_SECTION section;
102
} Binding;
103

104 105 106 107 108 109 110 111 112 113
static void read_protocol_data(stgmed_buf_t *stgmed_buf)
{
    BYTE buf[8192];
    DWORD read;
    HRESULT hres;

    do hres = IInternetProtocol_Read(stgmed_buf->protocol, buf, sizeof(buf), &read);
    while(hres == S_OK);
}

114 115
static void dump_BINDINFO(BINDINFO *bi)
{
116
    static const char * const BINDINFOF_str[] = {
117 118 119 120 121
        "#0",
        "BINDINFOF_URLENCODESTGMEDDATA",
        "BINDINFOF_URLENCODEDEXTRAINFO"
    };

122
    static const char * const BINDVERB_str[] = {
123 124 125 126 127 128 129 130
        "BINDVERB_GET",
        "BINDVERB_POST",
        "BINDVERB_PUT",
        "BINDVERB_CUSTOM"
    };

    TRACE("\n"
            "BINDINFO = {\n"
131 132
            "    %d, %s,\n"
            "    {%d, %p, %p},\n"
133 134 135
            "    %s,\n"
            "    %s,\n"
            "    %s,\n"
136 137
            "    %d, %08x, %d, %d\n"
            "    {%d %p %x},\n"
138
            "    %s\n"
139
            "    %p, %d\n"
140 141 142 143 144 145 146 147 148
            "}\n",

            bi->cbSize, debugstr_w(bi->szExtraInfo),
            bi->stgmedData.tymed, bi->stgmedData.u.hGlobal, bi->stgmedData.pUnkForRelease,
            bi->grfBindInfoF > BINDINFOF_URLENCODEDEXTRAINFO
                ? "unknown" : BINDINFOF_str[bi->grfBindInfoF],
            bi->dwBindVerb > BINDVERB_CUSTOM
                ? "unknown" : BINDVERB_str[bi->dwBindVerb],
            debugstr_w(bi->szCustomVerb),
149
            bi->cbstgmedData, bi->dwOptions, bi->dwOptionsFlags, bi->dwCodePage,
150 151 152 153 154 155 156 157
            bi->securityAttributes.nLength,
            bi->securityAttributes.lpSecurityDescriptor,
            bi->securityAttributes.bInheritHandle,
            debugstr_guid(&bi->iid),
            bi->pUnk, bi->dwReserved
            );
}

158
static void mime_available(Binding *This, LPCWSTR mime)
159
{
160 161
    heap_free(This->mime);
    This->mime = heap_strdupW(mime);
162

163
    if(!This->mime || !This->report_mime)
164 165
        return;

166
    IBindStatusCallback_OnProgress(This->callback, 0, 0, BINDSTATUS_MIMETYPEAVAILABLE, This->mime);
167

168
    This->clipboard_format = RegisterClipboardFormatW(This->mime);
169 170
}

171 172 173
static void stop_binding(Binding *binding, HRESULT hres, LPCWSTR str)
{
    if(binding->state & BINDING_LOCKED) {
174
        IInternetProtocolEx_UnlockRequest(&binding->protocol->IInternetProtocolEx_iface);
175 176 177 178 179 180 181 182 183 184 185
        binding->state &= ~BINDING_LOCKED;
    }

    if(!(binding->state & BINDING_STOPPED)) {
        binding->state |= BINDING_STOPPED;

        IBindStatusCallback_OnStopBinding(binding->callback, hres, str);
        binding->hres = hres;
    }
}

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
static LPWSTR get_mime_clsid(LPCWSTR mime, CLSID *clsid)
{
    LPWSTR key_name, ret;
    DWORD res, type, size;
    HKEY hkey;
    int len;
    HRESULT hres;

    static const WCHAR mime_keyW[] =
        {'M','I','M','E','\\','D','a','t','a','b','a','s','e','\\',
         'C','o','n','t','e','n','t',' ','T','y','p','e','\\'};
    static const WCHAR clsidW[] = {'C','L','S','I','D',0};

    len = strlenW(mime)+1;
    key_name = heap_alloc(sizeof(mime_keyW) + len*sizeof(WCHAR));
    memcpy(key_name, mime_keyW, sizeof(mime_keyW));
    strcpyW(key_name + sizeof(mime_keyW)/sizeof(WCHAR), mime);

    res = RegOpenKeyW(HKEY_CLASSES_ROOT, key_name, &hkey);
    heap_free(key_name);
    if(res != ERROR_SUCCESS) {
        WARN("Could not open MIME key: %x\n", res);
        return NULL;
    }

    size = 50*sizeof(WCHAR);
    ret = heap_alloc(size);
    res = RegQueryValueExW(hkey, clsidW, NULL, &type, (LPBYTE)ret, &size);
    RegCloseKey(hkey);
    if(res != ERROR_SUCCESS) {
        WARN("Could not get CLSID: %08x\n", res);
        heap_free(ret);
        return NULL;
    }

    hres = CLSIDFromString(ret, clsid);
    if(FAILED(hres)) {
        WARN("Could not parse CLSID: %08x\n", hres);
        heap_free(ret);
        return NULL;
    }

    return ret;
}

static void load_doc_mon(Binding *binding, IPersistMoniker *persist)
{
    IBindCtx *bctx;
    HRESULT hres;

    hres = CreateAsyncBindCtxEx(binding->bctx, 0, NULL, NULL, &bctx, 0);
    if(FAILED(hres)) {
        WARN("CreateAsyncBindCtxEx failed: %08x\n", hres);
        return;
    }

    IBindCtx_RevokeObjectParam(bctx, bscb_holderW);
243
    IBindCtx_RegisterObjectParam(bctx, cbinding_contextW, (IUnknown*)&binding->IBinding_iface);
244 245 246 247 248 249 250 251

    hres = IPersistMoniker_Load(persist, binding->download_state == END_DOWNLOAD, binding->mon, bctx, 0x12);
    IBindCtx_RevokeObjectParam(bctx, cbinding_contextW);
    IBindCtx_Release(bctx);
    if(FAILED(hres))
        FIXME("Load failed: %08x\n", hres);
}

252
static HRESULT create_mime_object(Binding *binding, const CLSID *clsid, LPCWSTR clsid_str)
253 254 255 256
{
    IPersistMoniker *persist;
    HRESULT hres;

257
    hres = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
258
                            &binding->iid, (void**)&binding->obj);
259 260 261 262
    if(FAILED(hres)) {
        WARN("CoCreateInstance failed: %08x\n", hres);
        return INET_E_CANNOT_INSTANTIATE_OBJECT;
    }
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286

    binding->state |= BINDING_OBJAVAIL;

    hres = IUnknown_QueryInterface(binding->obj, &IID_IPersistMoniker, (void**)&persist);
    if(SUCCEEDED(hres)) {
        IMonikerProp *prop;

        hres = IPersistMoniker_QueryInterface(persist, &IID_IMonikerProp, (void**)&prop);
        if(SUCCEEDED(hres)) {
            IMonikerProp_PutProperty(prop, MIMETYPEPROP, binding->mime);
            IMonikerProp_PutProperty(prop, CLASSIDPROP, clsid_str);
            IMonikerProp_Release(prop);
        }

        load_doc_mon(binding, persist);

        IPersistMoniker_Release(persist);
    }else {
        FIXME("Could not get IPersistMoniker: %08x\n", hres);
        /* FIXME: Try query IPersistFile */
    }

    IBindStatusCallback_OnObjectAvailable(binding->callback, &binding->iid, binding->obj);

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    return S_OK;
}

static void create_object(Binding *binding)
{
    LPWSTR clsid_str;
    CLSID clsid;
    HRESULT hres;

    if(!binding->mime) {
        FIXME("MIME not available\n");
        return;
    }

    if(!(clsid_str = get_mime_clsid(binding->mime, &clsid))) {
        FIXME("Could not find object for MIME %s\n", debugstr_w(binding->mime));
        return;
    }

    IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_CLASSIDAVAILABLE, clsid_str);
    IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_BEGINSYNCOPERATION, NULL);

    hres = create_mime_object(binding, &clsid, clsid_str);
    heap_free(clsid_str);

312 313
    IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_ENDSYNCOPERATION, NULL);

314 315
    stop_binding(binding, hres, NULL);
    if(FAILED(hres))
316
        IInternetProtocolEx_Terminate(&binding->protocol->IInternetProtocolEx_iface, 0);
317 318
}

319 320 321 322 323 324 325 326 327 328 329 330 331
static void cache_file_available(Binding *This, const WCHAR *file_name)
{
    heap_free(This->stgmed_buf->cache_file);
    This->stgmed_buf->cache_file = heap_strdupW(file_name);

    if(This->use_cache_file) {
        This->stgmed_buf->file = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
                OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
        if(This->stgmed_buf->file == INVALID_HANDLE_VALUE)
            WARN("CreateFile failed: %u\n", GetLastError());
    }
}

332 333 334 335
static inline stgmed_buf_t *impl_from_IUnknown(IUnknown *iface)
{
    return CONTAINING_RECORD(iface, stgmed_buf_t, IUnknown_iface);
}
336 337 338

static HRESULT WINAPI StgMedUnk_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
{
339
    stgmed_buf_t *This = impl_from_IUnknown(iface);
340 341 342 343 344 345

    *ppv = NULL;

    if(IsEqualGUID(riid, &IID_IUnknown)) {
        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);

346 347
        *ppv = &This->IUnknown_iface;
        IUnknown_AddRef(&This->IUnknown_iface);
348 349 350 351 352 353 354 355 356
        return S_OK;
    }

    TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
    return E_NOINTERFACE;
}

static ULONG WINAPI StgMedUnk_AddRef(IUnknown *iface)
{
357
    stgmed_buf_t *This = impl_from_IUnknown(iface);
358 359 360 361 362 363 364 365 366
    LONG ref = InterlockedIncrement(&This->ref);

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

    return ref;
}

static ULONG WINAPI StgMedUnk_Release(IUnknown *iface)
{
367
    stgmed_buf_t *This = impl_from_IUnknown(iface);
368 369 370 371 372
    LONG ref = InterlockedDecrement(&This->ref);

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

    if(!ref) {
373 374
        if(This->file != INVALID_HANDLE_VALUE)
            CloseHandle(This->file);
375
        IInternetProtocol_Release(This->protocol);
376
        heap_free(This->cache_file);
377 378 379 380 381 382 383 384 385 386 387 388 389 390
        heap_free(This);

        URLMON_UnlockModule();
    }

    return ref;
}

static const IUnknownVtbl StgMedUnkVtbl = {
    StgMedUnk_QueryInterface,
    StgMedUnk_AddRef,
    StgMedUnk_Release
};

391
static stgmed_buf_t *create_stgmed_buf(IInternetProtocolEx *protocol)
392 393 394
{
    stgmed_buf_t *ret = heap_alloc(sizeof(*ret));

395
    ret->IUnknown_iface.lpVtbl = &StgMedUnkVtbl;
396
    ret->ref = 1;
397
    ret->file = INVALID_HANDLE_VALUE;
398
    ret->hres = S_OK;
399
    ret->cache_file = NULL;
400 401 402 403 404 405 406 407 408 409 410

    IInternetProtocol_AddRef(protocol);
    ret->protocol = protocol;

    URLMON_LockModule();

    return ret;
}

typedef struct {
    stgmed_obj_t stgmed_obj;
411
    IStream IStream_iface;
412 413 414 415 416 417

    LONG ref;

    stgmed_buf_t *buf;
} ProtocolStream;

418 419 420 421
static inline ProtocolStream *impl_from_IStream(IStream *iface)
{
    return CONTAINING_RECORD(iface, ProtocolStream, IStream_iface);
}
422 423 424 425

static HRESULT WINAPI ProtocolStream_QueryInterface(IStream *iface,
                                                          REFIID riid, void **ppv)
{
426
    ProtocolStream *This = impl_from_IStream(iface);
427 428 429 430 431

    *ppv = NULL;

    if(IsEqualGUID(&IID_IUnknown, riid)) {
        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
432
        *ppv = &This->IStream_iface;
433 434
    }else if(IsEqualGUID(&IID_ISequentialStream, riid)) {
        TRACE("(%p)->(IID_ISequentialStream %p)\n", This, ppv);
435
        *ppv = &This->IStream_iface;
436 437
    }else if(IsEqualGUID(&IID_IStream, riid)) {
        TRACE("(%p)->(IID_IStream %p)\n", This, ppv);
438
        *ppv = &This->IStream_iface;
439 440 441
    }

    if(*ppv) {
442
        IStream_AddRef(&This->IStream_iface);
443 444 445 446 447 448 449 450 451
        return S_OK;
    }

    WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
    return E_NOINTERFACE;
}

static ULONG WINAPI ProtocolStream_AddRef(IStream *iface)
{
452
    ProtocolStream *This = impl_from_IStream(iface);
453 454
    LONG ref = InterlockedIncrement(&This->ref);

455
    TRACE("(%p) ref=%d\n", This, ref);
456 457 458 459 460 461

    return ref;
}

static ULONG WINAPI ProtocolStream_Release(IStream *iface)
{
462
    ProtocolStream *This = impl_from_IStream(iface);
463 464
    LONG ref = InterlockedDecrement(&This->ref);

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

    if(!ref) {
468
        IUnknown_Release(&This->buf->IUnknown_iface);
469
        heap_free(This);
Jacek Caban's avatar
Jacek Caban committed
470 471

        URLMON_UnlockModule();
472 473 474 475 476 477
    }

    return ref;
}

static HRESULT WINAPI ProtocolStream_Read(IStream *iface, void *pv,
478
                                          ULONG cb, ULONG *pcbRead)
479
{
480
    ProtocolStream *This = impl_from_IStream(iface);
481
    DWORD read = 0;
482
    HRESULT hres;
483

484
    TRACE("(%p)->(%p %d %p)\n", This, pv, cb, pcbRead);
485

486 487 488 489
    if(This->buf->file == INVALID_HANDLE_VALUE) {
        hres = This->buf->hres = IInternetProtocol_Read(This->buf->protocol, (PBYTE)pv, cb, &read);
    }else {
        hres = ReadFile(This->buf->file, pv, cb, &read, NULL) ? S_OK : INET_E_DOWNLOAD_FAILURE;
490 491
    }

492
    if (pcbRead)
493
        *pcbRead = read;
494

495
    if(hres == E_PENDING)
496
        return E_PENDING;
497 498
    else if(FAILED(hres))
        FIXME("Read failed: %08x\n", hres);
499

500
    return read ? S_OK : S_FALSE;
501 502 503 504 505
}

static HRESULT WINAPI ProtocolStream_Write(IStream *iface, const void *pv,
                                          ULONG cb, ULONG *pcbWritten)
{
506
    ProtocolStream *This = impl_from_IStream(iface);
507

508
    TRACE("(%p)->(%p %d %p)\n", This, pv, cb, pcbWritten);
509 510 511 512 513 514 515

    return STG_E_ACCESSDENIED;
}

static HRESULT WINAPI ProtocolStream_Seek(IStream *iface, LARGE_INTEGER dlibMove,
                                         DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
{
516
    ProtocolStream *This = impl_from_IStream(iface);
517
    FIXME("(%p)->(%d %08x %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
518 519 520 521 522
    return E_NOTIMPL;
}

static HRESULT WINAPI ProtocolStream_SetSize(IStream *iface, ULARGE_INTEGER libNewSize)
{
523
    ProtocolStream *This = impl_from_IStream(iface);
524
    FIXME("(%p)->(%d)\n", This, libNewSize.u.LowPart);
525 526 527 528 529 530
    return E_NOTIMPL;
}

static HRESULT WINAPI ProtocolStream_CopyTo(IStream *iface, IStream *pstm,
        ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten)
{
531
    ProtocolStream *This = impl_from_IStream(iface);
532
    FIXME("(%p)->(%p %d %p %p)\n", This, pstm, cb.u.LowPart, pcbRead, pcbWritten);
533 534 535 536 537
    return E_NOTIMPL;
}

static HRESULT WINAPI ProtocolStream_Commit(IStream *iface, DWORD grfCommitFlags)
{
538
    ProtocolStream *This = impl_from_IStream(iface);
539

540
    TRACE("(%p)->(%08x)\n", This, grfCommitFlags);
541 542 543 544 545 546

    return E_NOTIMPL;
}

static HRESULT WINAPI ProtocolStream_Revert(IStream *iface)
{
547
    ProtocolStream *This = impl_from_IStream(iface);
548 549 550 551 552 553 554 555 556

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

    return E_NOTIMPL;
}

static HRESULT WINAPI ProtocolStream_LockRegion(IStream *iface, ULARGE_INTEGER libOffset,
                                               ULARGE_INTEGER cb, DWORD dwLockType)
{
557
    ProtocolStream *This = impl_from_IStream(iface);
558
    FIXME("(%p)->(%d %d %d)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
559 560 561 562 563 564
    return E_NOTIMPL;
}

static HRESULT WINAPI ProtocolStream_UnlockRegion(IStream *iface,
        ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
{
565
    ProtocolStream *This = impl_from_IStream(iface);
566
    FIXME("(%p)->(%d %d %d)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
567 568 569 570 571 572
    return E_NOTIMPL;
}

static HRESULT WINAPI ProtocolStream_Stat(IStream *iface, STATSTG *pstatstg,
                                         DWORD dwStatFlag)
{
573
    ProtocolStream *This = impl_from_IStream(iface);
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
    TRACE("(%p)->(%p %08x)\n", This, pstatstg, dwStatFlag);

    if(!pstatstg)
        return E_FAIL;

    memset(pstatstg, 0, sizeof(STATSTG));

    if(!(dwStatFlag&STATFLAG_NONAME) && This->buf->cache_file) {
        pstatstg->pwcsName = CoTaskMemAlloc((lstrlenW(This->buf->cache_file)+1)*sizeof(WCHAR));
        if(!pstatstg->pwcsName)
            return STG_E_INSUFFICIENTMEMORY;

        lstrcpyW(pstatstg->pwcsName, This->buf->cache_file);
    }

    pstatstg->type = STGTY_STREAM;
    if(This->buf->file != INVALID_HANDLE_VALUE) {
        GetFileSizeEx(This->buf->file, (PLARGE_INTEGER)&pstatstg->cbSize);
        GetFileTime(This->buf->file, &pstatstg->ctime, &pstatstg->atime, &pstatstg->mtime);
        if(pstatstg->cbSize.QuadPart)
            pstatstg->grfMode = GENERIC_READ;
    }

    return S_OK;
598 599 600 601
}

static HRESULT WINAPI ProtocolStream_Clone(IStream *iface, IStream **ppstm)
{
602
    ProtocolStream *This = impl_from_IStream(iface);
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
    FIXME("(%p)->(%p)\n", This, ppstm);
    return E_NOTIMPL;
}

static const IStreamVtbl ProtocolStreamVtbl = {
    ProtocolStream_QueryInterface,
    ProtocolStream_AddRef,
    ProtocolStream_Release,
    ProtocolStream_Read,
    ProtocolStream_Write,
    ProtocolStream_Seek,
    ProtocolStream_SetSize,
    ProtocolStream_CopyTo,
    ProtocolStream_Commit,
    ProtocolStream_Revert,
    ProtocolStream_LockRegion,
    ProtocolStream_UnlockRegion,
    ProtocolStream_Stat,
    ProtocolStream_Clone
};

624 625 626
static void stgmed_stream_release(stgmed_obj_t *obj)
{
    ProtocolStream *stream = (ProtocolStream*)obj;
627
    IStream_Release(&stream->IStream_iface);
628
}
629

630
static HRESULT stgmed_stream_fill_stgmed(stgmed_obj_t *obj, STGMEDIUM *stgmed)
631
{
632
    ProtocolStream *stream = (ProtocolStream*)obj;
633

634
    stgmed->tymed = TYMED_ISTREAM;
635
    stgmed->u.pstm = &stream->IStream_iface;
636
    stgmed->pUnkForRelease = &stream->buf->IUnknown_iface;
637

638 639
    return S_OK;
}
640

641
static HRESULT stgmed_stream_get_result(stgmed_obj_t *obj, DWORD bindf, void **result)
642 643
{
    ProtocolStream *stream = (ProtocolStream*)obj;
Jacek Caban's avatar
Jacek Caban committed
644

645
    if(!(bindf & BINDF_ASYNCHRONOUS) && stream->buf->file == INVALID_HANDLE_VALUE
646
       && stream->buf->hres != S_FALSE)
647 648
        return INET_E_DATA_NOT_AVAILABLE;

649 650
    IStream_AddRef(&stream->IStream_iface);
    *result = &stream->IStream_iface;
651
    return S_OK;
652 653
}

654 655 656 657 658 659
static const stgmed_obj_vtbl stgmed_stream_vtbl = {
    stgmed_stream_release,
    stgmed_stream_fill_stgmed,
    stgmed_stream_get_result
};

660 661 662 663 664
typedef struct {
    stgmed_obj_t stgmed_obj;
    stgmed_buf_t *buf;
} stgmed_file_obj_t;

665
static stgmed_obj_t *create_stgmed_stream(stgmed_buf_t *buf)
666 667 668
{
    ProtocolStream *ret = heap_alloc(sizeof(ProtocolStream));

669
    ret->stgmed_obj.vtbl = &stgmed_stream_vtbl;
670
    ret->IStream_iface.lpVtbl = &ProtocolStreamVtbl;
671 672
    ret->ref = 1;

673
    IUnknown_AddRef(&buf->IUnknown_iface);
674 675 676 677
    ret->buf = buf;

    URLMON_LockModule();

678
    return &ret->stgmed_obj;
679 680
}

681 682
static void stgmed_file_release(stgmed_obj_t *obj)
{
683 684
    stgmed_file_obj_t *file_obj = (stgmed_file_obj_t*)obj;

685
    IUnknown_Release(&file_obj->buf->IUnknown_iface);
686
    heap_free(file_obj);
687 688 689 690 691 692 693 694 695 696 697
}

static HRESULT stgmed_file_fill_stgmed(stgmed_obj_t *obj, STGMEDIUM *stgmed)
{
    stgmed_file_obj_t *file_obj = (stgmed_file_obj_t*)obj;

    if(!file_obj->buf->cache_file) {
        WARN("cache_file not set\n");
        return INET_E_DATA_NOT_AVAILABLE;
    }

698
    read_protocol_data(file_obj->buf);
699 700 701

    stgmed->tymed = TYMED_FILE;
    stgmed->u.lpszFileName = file_obj->buf->cache_file;
702
    stgmed->pUnkForRelease = &file_obj->buf->IUnknown_iface;
703 704 705 706

    return S_OK;
}

707
static HRESULT stgmed_file_get_result(stgmed_obj_t *obj, DWORD bindf, void **result)
708
{
709
    return bindf & BINDF_ASYNCHRONOUS ? MK_S_ASYNCHRONOUS : S_OK;
710 711 712 713 714 715 716 717 718 719 720 721 722 723
}

static const stgmed_obj_vtbl stgmed_file_vtbl = {
    stgmed_file_release,
    stgmed_file_fill_stgmed,
    stgmed_file_get_result
};

static stgmed_obj_t *create_stgmed_file(stgmed_buf_t *buf)
{
    stgmed_file_obj_t *ret = heap_alloc(sizeof(*ret));

    ret->stgmed_obj.vtbl = &stgmed_file_vtbl;

724
    IUnknown_AddRef(&buf->IUnknown_iface);
725 726 727 728 729
    ret->buf = buf;

    return &ret->stgmed_obj;
}

730 731 732 733
static inline Binding *impl_from_IBinding(IBinding *iface)
{
    return CONTAINING_RECORD(iface, Binding, IBinding_iface);
}
734

735 736
static HRESULT WINAPI Binding_QueryInterface(IBinding *iface, REFIID riid, void **ppv)
{
737
    Binding *This = impl_from_IBinding(iface);
738 739 740 741 742

    *ppv = NULL;

    if(IsEqualGUID(&IID_IUnknown, riid)) {
        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
743
        *ppv = &This->IBinding_iface;
744 745
    }else if(IsEqualGUID(&IID_IBinding, riid)) {
        TRACE("(%p)->(IID_IBinding %p)\n", This, ppv);
746
        *ppv = &This->IBinding_iface;
747 748
    }else if(IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
        TRACE("(%p)->(IID_IInternetProtocolSink %p)\n", This, ppv);
749
        *ppv = &This->IInternetProtocolSink_iface;
750 751
    }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
        TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
752
        *ppv = &This->IInternetBindInfo_iface;
753 754
    }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
        TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
755
        *ppv = &This->IServiceProvider_iface;
756 757 758 759
    }else if(IsEqualGUID(&IID_IWinInetInfo, riid)) {
        TRACE("(%p)->(IID_IWinInetInfo %p)\n", This, ppv);

        /* NOTE: This violidates COM rules, but tests prove that we should do it */
760
        if(!This->protocol->wininet_info)
761 762
           return E_NOINTERFACE;

763
        *ppv = &This->IWinInetHttpInfo_iface;
764 765 766
    }else if(IsEqualGUID(&IID_IWinInetHttpInfo, riid)) {
        TRACE("(%p)->(IID_IWinInetHttpInfo %p)\n", This, ppv);

767
        if(!This->protocol->wininet_http_info)
768 769
            return E_NOINTERFACE;

770
        *ppv = &This->IWinInetHttpInfo_iface;
771 772
    }

Jacek Caban's avatar
Jacek Caban committed
773
    if(*ppv) {
774
        IBinding_AddRef(&This->IBinding_iface);
775
        return S_OK;
Jacek Caban's avatar
Jacek Caban committed
776
    }
777 778 779 780 781 782 783

    WARN("Unsupported interface %s\n", debugstr_guid(riid));
    return E_NOINTERFACE;
}

static ULONG WINAPI Binding_AddRef(IBinding *iface)
{
784
    Binding *This = impl_from_IBinding(iface);
785 786
    LONG ref = InterlockedIncrement(&This->ref);

787
    TRACE("(%p) ref=%d\n", This, ref);
788 789 790 791 792 793

    return ref;
}

static ULONG WINAPI Binding_Release(IBinding *iface)
{
794
    Binding *This = impl_from_IBinding(iface);
795 796
    LONG ref = InterlockedDecrement(&This->ref);

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

    if(!ref) {
800 801
        if(This->notif_hwnd)
            release_notif_hwnd(This->notif_hwnd);
802 803
        if(This->mon)
            IMoniker_Release(This->mon);
804 805 806
        if(This->callback)
            IBindStatusCallback_Release(This->callback);
        if(This->protocol)
807
            IInternetProtocolEx_Release(&This->protocol->IInternetProtocolEx_iface);
808 809
        if(This->service_provider)
            IServiceProvider_Release(This->service_provider);
810
        if(This->stgmed_buf)
811
            IUnknown_Release(&This->stgmed_buf->IUnknown_iface);
812 813
        if(This->stgmed_obj)
            This->stgmed_obj->vtbl->release(This->stgmed_obj);
814 815 816 817
        if(This->obj)
            IUnknown_Release(This->obj);
        if(This->bctx)
            IBindCtx_Release(This->bctx);
818 819

        ReleaseBindInfo(&This->bindinfo);
820
        This->section.DebugInfo->Spare[0] = 0;
821
        DeleteCriticalSection(&This->section);
822
        SysFreeString(This->url);
823
        heap_free(This->mime);
824
        heap_free(This->redirect_url);
825
        heap_free(This);
Jacek Caban's avatar
Jacek Caban committed
826 827

        URLMON_UnlockModule();
828 829 830 831 832 833 834
    }

    return ref;
}

static HRESULT WINAPI Binding_Abort(IBinding *iface)
{
835
    Binding *This = impl_from_IBinding(iface);
836 837 838 839 840 841 842
    HRESULT hres;

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

    if(This->state & BINDING_ABORTED)
        return E_FAIL;

843 844
    hres = IInternetProtocolEx_Abort(&This->protocol->IInternetProtocolEx_iface, E_ABORT,
            ERROR_SUCCESS);
845 846 847 848 849
    if(FAILED(hres))
        return hres;

    This->state |= BINDING_ABORTED;
    return S_OK;
850 851 852 853
}

static HRESULT WINAPI Binding_Suspend(IBinding *iface)
{
854
    Binding *This = impl_from_IBinding(iface);
855 856 857 858 859 860
    FIXME("(%p)\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI Binding_Resume(IBinding *iface)
{
861
    Binding *This = impl_from_IBinding(iface);
862 863 864 865 866 867
    FIXME("(%p)\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI Binding_SetPriority(IBinding *iface, LONG nPriority)
{
868
    Binding *This = impl_from_IBinding(iface);
869
    FIXME("(%p)->(%d)\n", This, nPriority);
870 871 872 873 874
    return E_NOTIMPL;
}

static HRESULT WINAPI Binding_GetPriority(IBinding *iface, LONG *pnPriority)
{
875
    Binding *This = impl_from_IBinding(iface);
876 877 878 879 880 881 882
    FIXME("(%p)->(%p)\n", This, pnPriority);
    return E_NOTIMPL;
}

static HRESULT WINAPI Binding_GetBindResult(IBinding *iface, CLSID *pclsidProtocol,
        DWORD *pdwResult, LPOLESTR *pszResult, DWORD *pdwReserved)
{
883
    Binding *This = impl_from_IBinding(iface);
884 885 886 887
    FIXME("(%p)->(%p %p %p %p)\n", This, pclsidProtocol, pdwResult, pszResult, pdwReserved);
    return E_NOTIMPL;
}

888 889 890 891 892 893 894 895 896 897 898 899
static const IBindingVtbl BindingVtbl = {
    Binding_QueryInterface,
    Binding_AddRef,
    Binding_Release,
    Binding_Abort,
    Binding_Suspend,
    Binding_Resume,
    Binding_SetPriority,
    Binding_GetPriority,
    Binding_GetBindResult
};

900 901 902 903 904 905 906 907 908 909
static Binding *get_bctx_binding(IBindCtx *bctx)
{
    IBinding *binding;
    IUnknown *unk;
    HRESULT hres;

    hres = IBindCtx_GetObjectParam(bctx, cbinding_contextW, &unk);
    if(FAILED(hres))
        return NULL;

910
    hres = IUnknown_QueryInterface(unk, &IID_IBinding, (void**)&binding);
911 912 913 914
    IUnknown_Release(unk);
    if(FAILED(hres))
        return NULL;

915 916 917
    if (binding->lpVtbl != &BindingVtbl)
        return NULL;
    return impl_from_IBinding(binding);
918 919
}

920 921 922 923
static inline Binding *impl_from_IInternetProtocolSink(IInternetProtocolSink *iface)
{
    return CONTAINING_RECORD(iface, Binding, IInternetProtocolSink_iface);
}
924 925 926 927

static HRESULT WINAPI InternetProtocolSink_QueryInterface(IInternetProtocolSink *iface,
        REFIID riid, void **ppv)
{
928
    Binding *This = impl_from_IInternetProtocolSink(iface);
929
    return IBinding_QueryInterface(&This->IBinding_iface, riid, ppv);
930 931 932 933
}

static ULONG WINAPI InternetProtocolSink_AddRef(IInternetProtocolSink *iface)
{
934
    Binding *This = impl_from_IInternetProtocolSink(iface);
935
    return IBinding_AddRef(&This->IBinding_iface);
936 937 938 939
}

static ULONG WINAPI InternetProtocolSink_Release(IInternetProtocolSink *iface)
{
940
    Binding *This = impl_from_IInternetProtocolSink(iface);
941
    return IBinding_Release(&This->IBinding_iface);
942 943 944 945 946
}

static HRESULT WINAPI InternetProtocolSink_Switch(IInternetProtocolSink *iface,
        PROTOCOLDATA *pProtocolData)
{
947
    Binding *This = impl_from_IInternetProtocolSink(iface);
948

949
    WARN("(%p)->(%p)\n", This, pProtocolData);
950

951
    return E_FAIL;
952 953
}

954 955 956
static void on_progress(Binding *This, ULONG progress, ULONG progress_max,
                        ULONG status_code, LPCWSTR status_text)
{
957 958
    IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
            status_code, status_text);
959 960
}

961 962 963
static HRESULT WINAPI InternetProtocolSink_ReportProgress(IInternetProtocolSink *iface,
        ULONG ulStatusCode, LPCWSTR szStatusText)
{
964
    Binding *This = impl_from_IInternetProtocolSink(iface);
965

966
    TRACE("(%p)->(%u %s)\n", This, ulStatusCode, debugstr_w(szStatusText));
967 968

    switch(ulStatusCode) {
969 970 971 972 973 974
    case BINDSTATUS_FINDINGRESOURCE:
        on_progress(This, 0, 0, BINDSTATUS_FINDINGRESOURCE, szStatusText);
        break;
    case BINDSTATUS_CONNECTING:
        on_progress(This, 0, 0, BINDSTATUS_CONNECTING, szStatusText);
        break;
975
    case BINDSTATUS_REDIRECTING:
976 977
        heap_free(This->redirect_url);
        This->redirect_url = heap_strdupW(szStatusText);
978 979
        on_progress(This, 0, 0, BINDSTATUS_REDIRECTING, szStatusText);
        break;
980 981
    case BINDSTATUS_BEGINDOWNLOADDATA:
        break;
982
    case BINDSTATUS_SENDINGREQUEST:
983
        on_progress(This, 0, 0, BINDSTATUS_SENDINGREQUEST, szStatusText);
984
        break;
985 986
    case BINDSTATUS_PROTOCOLCLASSID:
        break;
987
    case BINDSTATUS_MIMETYPEAVAILABLE:
988
    case BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE:
989
        mime_available(This, szStatusText);
990 991
        break;
    case BINDSTATUS_CACHEFILENAMEAVAILABLE:
992
        cache_file_available(This, szStatusText);
993
        break;
994 995 996 997 998 999
    case BINDSTATUS_DECODING:
        IBindStatusCallback_OnProgress(This->callback, 0, 0, BINDSTATUS_DECODING, szStatusText);
        break;
    case BINDSTATUS_LOADINGMIMEHANDLER:
        on_progress(This, 0, 0, BINDSTATUS_LOADINGMIMEHANDLER, szStatusText);
        break;
1000
    case BINDSTATUS_DIRECTBIND: /* FIXME: Handle BINDSTATUS_DIRECTBIND in BindProtocol */
1001 1002
        This->report_mime = FALSE;
        break;
1003 1004
    case BINDSTATUS_ACCEPTRANGES:
        break;
1005
    default:
1006
        FIXME("Unhandled status code %d\n", ulStatusCode);
1007 1008 1009 1010 1011 1012
        return E_NOTIMPL;
    };

    return S_OK;
}

1013
static void report_data(Binding *This, DWORD bscf, ULONG progress, ULONG progress_max)
1014 1015
{
    FORMATETC formatetc = {0, NULL, 1, -1, TYMED_ISTREAM};
1016
    BOOL sent_begindownloaddata = FALSE;
1017

1018 1019
    TRACE("(%p)->(%d %u %u)\n", This, bscf, progress, progress_max);

1020 1021 1022 1023 1024 1025
    if(This->download_state == END_DOWNLOAD || (This->state & BINDING_ABORTED)) {
        read_protocol_data(This->stgmed_buf);
        return;
    }

    if(This->state & BINDING_STOPPED)
1026 1027
        return;

1028 1029
    if(This->stgmed_buf->file != INVALID_HANDLE_VALUE)
        read_protocol_data(This->stgmed_buf);
1030

1031
    if(This->download_state == BEFORE_DOWNLOAD) {
1032
        This->download_state = DOWNLOADING;
1033
        sent_begindownloaddata = TRUE;
1034 1035
        IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
                BINDSTATUS_BEGINDOWNLOADDATA, This->url);
1036 1037 1038 1039

        if(This->stgmed_buf->cache_file)
            IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
                    BINDSTATUS_CACHEFILENAMEAVAILABLE, This->stgmed_buf->cache_file);
1040 1041
    }

1042
    if(This->stgmed_buf->hres == S_FALSE || (bscf & BSCF_LASTDATANOTIFICATION)) {
1043
        This->download_state = END_DOWNLOAD;
1044 1045
        IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
                BINDSTATUS_ENDDOWNLOADDATA, This->url);
1046 1047 1048
    }else if(!sent_begindownloaddata) {
        IBindStatusCallback_OnProgress(This->callback, progress, progress_max,
                BINDSTATUS_DOWNLOADINGDATA, This->url);
1049
    }
1050

1051 1052 1053
    if(This->state & (BINDING_STOPPED|BINDING_ABORTED))
        return;

1054
    if(This->to_object) {
1055
        if(!(This->state & BINDING_OBJAVAIL)) {
1056
            IBinding_AddRef(&This->IBinding_iface);
1057
            create_object(This);
1058
            IBinding_Release(&This->IBinding_iface);
1059
        }
1060
    }else {
1061
        STGMEDIUM stgmed;
1062
        HRESULT hres;
1063

1064
        if(!(This->state & BINDING_LOCKED)) {
1065 1066
            HRESULT hres = IInternetProtocolEx_LockRequest(
                    &This->protocol->IInternetProtocolEx_iface, 0);
1067 1068 1069
            if(SUCCEEDED(hres))
                This->state |= BINDING_LOCKED;
        }
1070

1071 1072 1073 1074 1075 1076 1077
        hres = This->stgmed_obj->vtbl->fill_stgmed(This->stgmed_obj, &stgmed);
        if(FAILED(hres)) {
            stop_binding(This, hres, NULL);
            return;
        }

        formatetc.tymed = stgmed.tymed;
1078
        formatetc.cfFormat = This->clipboard_format;
1079

1080
        IBindStatusCallback_OnDataAvailable(This->callback, bscf, progress,
1081
                &formatetc, &stgmed);
1082

1083 1084 1085
        if(This->download_state == END_DOWNLOAD)
            stop_binding(This, S_OK, NULL);
    }
1086 1087
}

1088 1089 1090
static HRESULT WINAPI InternetProtocolSink_ReportData(IInternetProtocolSink *iface,
        DWORD grfBSCF, ULONG ulProgress, ULONG ulProgressMax)
{
1091
    Binding *This = impl_from_IInternetProtocolSink(iface);
1092

1093
    TRACE("(%p)->(%d %u %u)\n", This, grfBSCF, ulProgress, ulProgressMax);
1094

1095
    report_data(This, grfBSCF, ulProgress, ulProgressMax);
1096 1097 1098 1099 1100 1101
    return S_OK;
}

static HRESULT WINAPI InternetProtocolSink_ReportResult(IInternetProtocolSink *iface,
        HRESULT hrResult, DWORD dwError, LPCWSTR szResult)
{
1102
    Binding *This = impl_from_IInternetProtocolSink(iface);
1103

1104
    TRACE("(%p)->(%08x %d %s)\n", This, hrResult, dwError, debugstr_w(szResult));
1105

1106
    stop_binding(This, hrResult, szResult);
1107
    IInternetProtocolEx_Terminate(&This->protocol->IInternetProtocolEx_iface, 0);
1108
    return S_OK;
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
}

static const IInternetProtocolSinkVtbl InternetProtocolSinkVtbl = {
    InternetProtocolSink_QueryInterface,
    InternetProtocolSink_AddRef,
    InternetProtocolSink_Release,
    InternetProtocolSink_Switch,
    InternetProtocolSink_ReportProgress,
    InternetProtocolSink_ReportData,
    InternetProtocolSink_ReportResult
};

1121 1122 1123 1124
static inline Binding *impl_from_IInternetBindInfo(IInternetBindInfo *iface)
{
    return CONTAINING_RECORD(iface, Binding, IInternetBindInfo_iface);
}
1125 1126 1127 1128

static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
        REFIID riid, void **ppv)
{
1129
    Binding *This = impl_from_IInternetBindInfo(iface);
1130
    return IBinding_QueryInterface(&This->IBinding_iface, riid, ppv);
1131 1132 1133 1134
}

static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
{
1135
    Binding *This = impl_from_IInternetBindInfo(iface);
1136
    return IBinding_AddRef(&This->IBinding_iface);
1137 1138 1139 1140
}

static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
{
1141
    Binding *This = impl_from_IInternetBindInfo(iface);
1142
    return IBinding_Release(&This->IBinding_iface);
1143 1144 1145 1146 1147
}

static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
        DWORD *grfBINDF, BINDINFO *pbindinfo)
{
1148
    Binding *This = impl_from_IInternetBindInfo(iface);
1149 1150 1151 1152 1153

    TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);

    *grfBINDF = This->bindf;

1154
    *pbindinfo = This->bindinfo;
1155 1156 1157 1158

    if(pbindinfo->szExtraInfo || pbindinfo->szCustomVerb)
        FIXME("copy strings\n");

1159 1160 1161
    if(pbindinfo->stgmedData.pUnkForRelease)
        IUnknown_AddRef(pbindinfo->stgmedData.pUnkForRelease);

1162 1163 1164 1165 1166 1167 1168 1169 1170
    if(pbindinfo->pUnk)
        IUnknown_AddRef(pbindinfo->pUnk);

    return S_OK;
}

static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
        ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
{
1171
    Binding *This = impl_from_IInternetBindInfo(iface);
1172

1173
    TRACE("(%p)->(%d %p %d %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201

    switch(ulStringType) {
    case BINDSTRING_ACCEPT_MIMES: {
        static const WCHAR wszMimes[] = {'*','/','*',0};

        if(!ppwzStr || !pcElFetched)
            return E_INVALIDARG;

        ppwzStr[0] = CoTaskMemAlloc(sizeof(wszMimes));
        memcpy(ppwzStr[0], wszMimes, sizeof(wszMimes));
        *pcElFetched = 1;
        return S_OK;
    }
    case BINDSTRING_USER_AGENT: {
        IInternetBindInfo *bindinfo = NULL;
        HRESULT hres;

        hres = IBindStatusCallback_QueryInterface(This->callback, &IID_IInternetBindInfo,
                                                  (void**)&bindinfo);
        if(FAILED(hres))
            return hres;

        hres = IInternetBindInfo_GetBindString(bindinfo, ulStringType, ppwzStr,
                                               cEl, pcElFetched);
        IInternetBindInfo_Release(bindinfo);

        return hres;
    }
1202
    case BINDSTRING_URL: {
1203
        DWORD size = (SysStringLen(This->url)+1) * sizeof(WCHAR);
1204 1205 1206 1207 1208 1209 1210 1211 1212

        if(!ppwzStr || !pcElFetched)
            return E_INVALIDARG;

        *ppwzStr = CoTaskMemAlloc(size);
        memcpy(*ppwzStr, This->url, size);
        *pcElFetched = 1;
        return S_OK;
    }
1213 1214
    }

1215
    FIXME("not supported string type %d\n", ulStringType);
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
    return E_NOTIMPL;
}

static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
    InternetBindInfo_QueryInterface,
    InternetBindInfo_AddRef,
    InternetBindInfo_Release,
    InternetBindInfo_GetBindInfo,
    InternetBindInfo_GetBindString
};

1227 1228 1229 1230
static inline Binding *impl_from_IWinInetHttpInfo(IWinInetHttpInfo *iface)
{
    return CONTAINING_RECORD(iface, Binding, IWinInetHttpInfo_iface);
}
1231 1232 1233

static HRESULT WINAPI WinInetHttpInfo_QueryInterface(IWinInetHttpInfo *iface, REFIID riid, void **ppv)
{
1234
    Binding *This = impl_from_IWinInetHttpInfo(iface);
1235
    return IBinding_QueryInterface(&This->IBinding_iface, riid, ppv);
1236 1237 1238 1239
}

static ULONG WINAPI WinInetHttpInfo_AddRef(IWinInetHttpInfo *iface)
{
1240
    Binding *This = impl_from_IWinInetHttpInfo(iface);
1241
    return IBinding_AddRef(&This->IBinding_iface);
1242 1243 1244 1245
}

static ULONG WINAPI WinInetHttpInfo_Release(IWinInetHttpInfo *iface)
{
1246
    Binding *This = impl_from_IWinInetHttpInfo(iface);
1247
    return IBinding_Release(&This->IBinding_iface);
1248 1249 1250 1251 1252
}

static HRESULT WINAPI WinInetHttpInfo_QueryOption(IWinInetHttpInfo *iface, DWORD dwOption,
        void *pBuffer, DWORD *pcbBuffer)
{
1253
    Binding *This = impl_from_IWinInetHttpInfo(iface);
1254 1255 1256 1257 1258 1259 1260
    TRACE("(%p)->(%x %p %p)\n", This, dwOption, pBuffer, pcbBuffer);

    if(!This->protocol->wininet_info)
        return E_FAIL;

    return IWinInetInfo_QueryOption(This->protocol->wininet_info,
            dwOption, pBuffer, pcbBuffer);
1261 1262 1263 1264 1265
}

static HRESULT WINAPI WinInetHttpInfo_QueryInfo(IWinInetHttpInfo *iface, DWORD dwOption,
        void *pBuffer, DWORD *pcbBuffer, DWORD *pdwFlags, DWORD *pdwReserved)
{
1266
    Binding *This = impl_from_IWinInetHttpInfo(iface);
1267 1268 1269 1270 1271 1272 1273
    TRACE("(%p)->(%x %p %p %p %p)\n", This, dwOption, pBuffer, pcbBuffer, pdwFlags, pdwReserved);

    if(!This->protocol->wininet_http_info)
        return E_FAIL;

    return IWinInetHttpInfo_QueryInfo(This->protocol->wininet_http_info,
            dwOption, pBuffer, pcbBuffer, pdwFlags, pdwReserved);
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
}

static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl = {
    WinInetHttpInfo_QueryInterface,
    WinInetHttpInfo_AddRef,
    WinInetHttpInfo_Release,
    WinInetHttpInfo_QueryOption,
    WinInetHttpInfo_QueryInfo
};

1284 1285 1286 1287
static inline Binding *impl_from_IServiceProvider(IServiceProvider *iface)
{
    return CONTAINING_RECORD(iface, Binding, IServiceProvider_iface);
}
1288 1289 1290 1291

static HRESULT WINAPI ServiceProvider_QueryInterface(IServiceProvider *iface,
        REFIID riid, void **ppv)
{
1292
    Binding *This = impl_from_IServiceProvider(iface);
1293
    return IBinding_QueryInterface(&This->IBinding_iface, riid, ppv);
1294 1295 1296 1297
}

static ULONG WINAPI ServiceProvider_AddRef(IServiceProvider *iface)
{
1298
    Binding *This = impl_from_IServiceProvider(iface);
1299
    return IBinding_AddRef(&This->IBinding_iface);
1300 1301 1302 1303
}

static ULONG WINAPI ServiceProvider_Release(IServiceProvider *iface)
{
1304
    Binding *This = impl_from_IServiceProvider(iface);
1305
    return IBinding_Release(&This->IBinding_iface);
1306 1307 1308 1309 1310
}

static HRESULT WINAPI ServiceProvider_QueryService(IServiceProvider *iface,
        REFGUID guidService, REFIID riid, void **ppv)
{
1311
    Binding *This = impl_from_IServiceProvider(iface);
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
    HRESULT hres;

    TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);

    if(This->service_provider) {
        hres = IServiceProvider_QueryService(This->service_provider, guidService,
                                             riid, ppv);
        if(SUCCEEDED(hres))
            return hres;
    }

    WARN("unknown service %s\n", debugstr_guid(guidService));
1324
    return E_NOINTERFACE;
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
}

static const IServiceProviderVtbl ServiceProviderVtbl = {
    ServiceProvider_QueryInterface,
    ServiceProvider_AddRef,
    ServiceProvider_Release,
    ServiceProvider_QueryService
};

static HRESULT get_callback(IBindCtx *pbc, IBindStatusCallback **callback)
{
1336
    IUnknown *unk;
1337 1338
    HRESULT hres;

1339
    hres = IBindCtx_GetObjectParam(pbc, bscb_holderW, &unk);
1340 1341
    if(FAILED(hres))
        return create_default_callback(callback);
1342

1343 1344 1345
    hres = IUnknown_QueryInterface(unk, &IID_IBindStatusCallback, (void**)callback);
    IUnknown_Release(unk);
    return hres;
1346 1347
}

1348 1349 1350 1351
static BOOL is_urlmon_protocol(IUri *uri)
{
    DWORD scheme;
    HRESULT hres;
1352

1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
    hres = IUri_GetScheme(uri, &scheme);
    if(FAILED(hres))
        return FALSE;

    switch(scheme) {
    case URL_SCHEME_FILE:
    case URL_SCHEME_FTP:
    case URL_SCHEME_GOPHER:
    case URL_SCHEME_HTTP:
    case URL_SCHEME_HTTPS:
    case URL_SCHEME_MK:
        return TRUE;
1365 1366 1367 1368 1369
    }

    return FALSE;
}

1370
static HRESULT Binding_Create(IMoniker *mon, Binding *binding_ctx, IUri *uri, IBindCtx *pbc,
1371
        BOOL to_obj, REFIID riid, Binding **binding)
1372 1373 1374 1375
{
    Binding *ret;
    HRESULT hres;

Jacek Caban's avatar
Jacek Caban committed
1376 1377
    URLMON_LockModule();

1378
    ret = heap_alloc_zero(sizeof(Binding));
1379

1380
    ret->IBinding_iface.lpVtbl = &BindingVtbl;
1381 1382 1383 1384
    ret->IInternetProtocolSink_iface.lpVtbl = &InternetProtocolSinkVtbl;
    ret->IInternetBindInfo_iface.lpVtbl = &InternetBindInfoVtbl;
    ret->IWinInetHttpInfo_iface.lpVtbl = &WinInetHttpInfoVtbl;
    ret->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
1385 1386 1387

    ret->ref = 1;

1388 1389
    ret->to_object = to_obj;
    ret->iid = *riid;
1390
    ret->notif_hwnd = get_notif_hwnd();
1391
    ret->report_mime = !binding_ctx;
1392
    ret->download_state = BEFORE_DOWNLOAD;
1393

1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
    if(to_obj) {
        IBindCtx_AddRef(pbc);
        ret->bctx = pbc;
    }

    if(mon) {
        IMoniker_AddRef(mon);
        ret->mon = mon;
    }

1404 1405
    ret->bindinfo.cbSize = sizeof(BINDINFO);

1406
    InitializeCriticalSection(&ret->section);
1407
    ret->section.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": Binding.section");
1408

1409 1410 1411
    hres = get_callback(pbc, &ret->callback);
    if(FAILED(hres)) {
        WARN("Could not get IBindStatusCallback\n");
1412
        IBinding_Release(&ret->IBinding_iface);
1413 1414 1415
        return hres;
    }

1416 1417 1418
    IBindStatusCallback_QueryInterface(ret->callback, &IID_IServiceProvider,
                                       (void**)&ret->service_provider);

1419 1420
    if(binding_ctx) {
        ret->protocol = binding_ctx->protocol;
1421
        IInternetProtocolEx_AddRef(&ret->protocol->IInternetProtocolEx_iface);
1422
    }else {
1423
        hres = create_binding_protocol(TRUE, &ret->protocol);
1424 1425
        if(FAILED(hres)) {
            WARN("Could not get protocol handler\n");
1426
            IBinding_Release(&ret->IBinding_iface);
1427 1428
            return hres;
        }
1429 1430 1431 1432
    }

    hres = IBindStatusCallback_GetBindInfo(ret->callback, &ret->bindf, &ret->bindinfo);
    if(FAILED(hres)) {
1433
        WARN("GetBindInfo failed: %08x\n", hres);
1434
        IBinding_Release(&ret->IBinding_iface);
1435 1436 1437
        return hres;
    }

1438
    TRACE("bindf %08x\n", ret->bindf);
1439 1440
    dump_BINDINFO(&ret->bindinfo);

1441
    ret->bindf |= BINDF_FROMURLMON;
1442 1443
    if(to_obj)
        ret->bindinfo.dwOptions |= 0x100000;
1444

1445
    if(!(ret->bindf & BINDF_ASYNCHRONOUS) || !(ret->bindf & BINDF_PULLDATA)) {
1446
        ret->bindf |= BINDF_NEEDFILE;
1447
        ret->use_cache_file = TRUE;
1448
    }else if(!is_urlmon_protocol(uri)) {
1449 1450
        ret->bindf |= BINDF_NEEDFILE;
    }
1451

1452 1453
    hres = IUri_GetDisplayUri(uri, &ret->url);
    if(FAILED(hres)) {
1454
        IBinding_Release(&ret->IBinding_iface);
1455 1456
        return hres;
    }
1457

1458
    if(binding_ctx) {
1459
        ret->stgmed_buf = binding_ctx->stgmed_buf;
1460
        IUnknown_AddRef(&ret->stgmed_buf->IUnknown_iface);
1461 1462
        ret->clipboard_format = binding_ctx->clipboard_format;
    }else {
1463
        ret->stgmed_buf = create_stgmed_buf(&ret->protocol->IInternetProtocolEx_iface);
1464
    }
1465

1466 1467 1468 1469
    if(to_obj) {
        ret->stgmed_obj = NULL;
    }else if(IsEqualGUID(&IID_IStream, riid)) {
        ret->stgmed_obj = create_stgmed_stream(ret->stgmed_buf);
1470 1471 1472
    }else if(IsEqualGUID(&IID_IUnknown, riid)) {
        ret->bindf |= BINDF_NEEDFILE;
        ret->stgmed_obj = create_stgmed_file(ret->stgmed_buf);
1473 1474
    }else {
        FIXME("Unsupported riid %s\n", debugstr_guid(riid));
1475
        IBinding_Release(&ret->IBinding_iface);
1476 1477
        return E_NOTIMPL;
    }
1478 1479 1480 1481 1482

    *binding = ret;
    return S_OK;
}

1483
static HRESULT start_binding(IMoniker *mon, Binding *binding_ctx, IUri *uri, IBindCtx *pbc,
1484
                             BOOL to_obj, REFIID riid, Binding **ret)
1485 1486 1487
{
    Binding *binding = NULL;
    HRESULT hres;
1488
    MSG msg;
1489

1490
    hres = Binding_Create(mon, binding_ctx, uri, pbc, to_obj, riid, &binding);
1491 1492 1493
    if(FAILED(hres))
        return hres;

1494
    hres = IBindStatusCallback_OnStartBinding(binding->callback, 0, &binding->IBinding_iface);
1495
    if(FAILED(hres)) {
1496
        WARN("OnStartBinding failed: %08x\n", hres);
1497 1498 1499 1500
        if(hres != E_ABORT && hres != E_NOTIMPL)
            hres = INET_E_DOWNLOAD_FAILURE;

        stop_binding(binding, hres, NULL);
1501
        IBinding_Release(&binding->IBinding_iface);
1502 1503 1504
        return hres;
    }

1505
    if(binding_ctx) {
1506 1507
        set_binding_sink(binding->protocol, &binding->IInternetProtocolSink_iface,
                &binding->IInternetBindInfo_iface);
1508 1509
        if(binding_ctx->redirect_url)
            IBindStatusCallback_OnProgress(binding->callback, 0, 0, BINDSTATUS_REDIRECTING, binding_ctx->redirect_url);
1510 1511
        report_data(binding, BSCF_FIRSTDATANOTIFICATION | (binding_ctx->download_state == END_DOWNLOAD ? BSCF_LASTDATANOTIFICATION : 0),
                0, 0);
1512
    }else {
1513
        hres = IInternetProtocolEx_StartEx(&binding->protocol->IInternetProtocolEx_iface, uri,
1514 1515
                &binding->IInternetProtocolSink_iface, &binding->IInternetBindInfo_iface,
                PI_APARTMENTTHREADED|PI_MIMEVERIFICATION, 0);
1516

1517
        TRACE("start ret %08x\n", hres);
1518

1519
        if(FAILED(hres) && hres != E_PENDING) {
1520
            stop_binding(binding, hres, NULL);
1521
            IBinding_Release(&binding->IBinding_iface);
1522

1523 1524
            return hres;
        }
1525 1526
    }

1527
    while(!(binding->bindf & BINDF_ASYNCHRONOUS) &&
1528
          !(binding->state & BINDING_STOPPED)) {
1529 1530 1531 1532 1533 1534 1535
        MsgWaitForMultipleObjects(0, NULL, FALSE, 5000, QS_POSTMESSAGE);
        while (PeekMessageW(&msg, binding->notif_hwnd, WM_USER, WM_USER+117, PM_REMOVE|PM_NOYIELD)) {
            TranslateMessage(&msg);
            DispatchMessageW(&msg);
        }
    }

1536 1537 1538 1539
    *ret = binding;
    return S_OK;
}

1540
HRESULT bind_to_storage(IUri *uri, IBindCtx *pbc, REFIID riid, void **ppv)
1541
{
1542
    Binding *binding = NULL, *binding_ctx;
1543 1544
    HRESULT hres;

1545 1546
    binding_ctx = get_bctx_binding(pbc);

1547
    hres = start_binding(NULL, binding_ctx, uri, pbc, FALSE, riid, &binding);
1548
    if(binding_ctx)
1549
        IBinding_Release(&binding_ctx->IBinding_iface);
1550 1551 1552
    if(FAILED(hres))
        return hres;

1553
    if(binding->hres == S_OK && binding->download_state != BEFORE_DOWNLOAD /* FIXME */) {
1554
        if((binding->state & BINDING_STOPPED) && (binding->state & BINDING_LOCKED))
1555
            IInternetProtocolEx_UnlockRequest(&binding->protocol->IInternetProtocolEx_iface);
1556

1557
        hres = binding->stgmed_obj->vtbl->get_result(binding->stgmed_obj, binding->bindf, ppv);
1558
    }else if(binding->bindf & BINDF_ASYNCHRONOUS) {
1559
        hres = MK_S_ASYNCHRONOUS;
1560 1561
    }else {
        hres = FAILED(binding->hres) ? binding->hres : S_OK;
1562
    }
1563

1564
    IBinding_Release(&binding->IBinding_iface);
1565

1566
    return hres;
1567
}
1568

1569
HRESULT bind_to_object(IMoniker *mon, IUri *uri, IBindCtx *pbc, REFIID riid, void **ppv)
1570 1571 1572 1573 1574 1575
{
    Binding *binding;
    HRESULT hres;

    *ppv = NULL;

1576
    hres = start_binding(mon, NULL, uri, pbc, TRUE, riid, &binding);
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
    if(FAILED(hres))
        return hres;

    if(binding->hres != S_OK) {
        hres = SUCCEEDED(binding->hres) ? S_OK : binding->hres;
    }else if(binding->bindf & BINDF_ASYNCHRONOUS) {
        hres = MK_S_ASYNCHRONOUS;
    }else {
        *ppv = binding->obj;
        IUnknown_AddRef(binding->obj);
        hres = S_OK;
    }

1590
    IBinding_Release(&binding->IBinding_iface);
1591 1592 1593

    return hres;
}