loadopts.c 5.02 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 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
 * Copyright 2006 Jacek Caban for CodeWeavers
 *
 * 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
 */

#include "config.h"

#include <stdarg.h>
#include <stdio.h>

#define COBJMACROS

#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "ole2.h"
#include "optary.h"

#include "wine/debug.h"

#include "mshtml_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(mshtml);

38 39 40 41 42 43 44 45
typedef struct load_opt {
    DWORD option;
    PVOID buffer;
    DWORD size;

    struct load_opt *next;
} load_opt;

46 47 48 49
typedef struct {
    const IHtmlLoadOptionsVtbl *lpHtmlLoadOptionsVtbl;

    LONG ref;
50 51

    load_opt *opts;
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
} HTMLLoadOptions;

#define LOADOPTS(x)  ((IHtmlLoadOptions*) &(x)->lpHtmlLoadOptionsVtbl)

#define LOADOPTS_THIS(iface) DEFINE_THIS(HTMLLoadOptions, HtmlLoadOptions, iface)

static HRESULT WINAPI HtmlLoadOptions_QueryInterface(IHtmlLoadOptions *iface,
        REFIID riid, void **ppv)
{
    HTMLLoadOptions *This = LOADOPTS_THIS(iface);

    *ppv = NULL;

    if(IsEqualGUID(&IID_IUnknown, riid)) {
        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
        *ppv = LOADOPTS(This);
    }else if(IsEqualGUID(&IID_IOptionArray, riid)) {
        TRACE("(%p)->(IID_IOptionArray %p)\n", This, ppv);
        *ppv = LOADOPTS(This);
    }else if(IsEqualGUID(&IID_IHtmlLoadOptions, riid)) {
        TRACE("(%p)->(IID_IHtmlLoadOptions %p)\n", This, ppv);
        *ppv = LOADOPTS(This);
    }

    if(*ppv) {
        IHtmlLoadOptions_AddRef(LOADOPTS(This));
        return S_OK;
    }

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

static ULONG WINAPI HtmlLoadOptions_AddRef(IHtmlLoadOptions *iface)
{
    HTMLLoadOptions *This = LOADOPTS_THIS(iface);
    LONG ref = InterlockedIncrement(&This->ref);

90
    TRACE("(%p) ref=%d\n", This, ref);
91 92 93 94 95 96 97 98 99

    return ref;
}

static ULONG WINAPI HtmlLoadOptions_Release(IHtmlLoadOptions *iface)
{
    HTMLLoadOptions *This = LOADOPTS_THIS(iface);
    LONG ref = InterlockedDecrement(&This->ref);

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

102 103 104 105 106 107 108
    if(!ref) {
        load_opt *iter = This->opts, *last;

        while(iter) {
            last = iter;
            iter = iter->next;

109 110
            heap_free(last->buffer);
            heap_free(last);
111 112
        }

113
        heap_free(This);
114
    }
115 116 117 118 119 120 121 122

    return ref;
}

static HRESULT WINAPI HtmlLoadOptions_QueryOption(IHtmlLoadOptions *iface, DWORD dwOption,
        LPVOID pBuffer, ULONG *pcbBuf)
{
    HTMLLoadOptions *This = LOADOPTS_THIS(iface);
123 124
    load_opt *iter;

125
    TRACE("(%p)->(%d %p %p)\n", This, dwOption, pBuffer, pcbBuf);
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

    for(iter = This->opts; iter; iter = iter->next) {
        if(iter->option == dwOption)
            break;
    }

    if(!iter) {
        *pcbBuf = 0;
        return S_OK;
    }

    if(*pcbBuf < iter->size) {
        *pcbBuf = iter->size;
        return E_FAIL;
    }

    memcpy(pBuffer, iter->buffer, iter->size);
    *pcbBuf = iter->size;

    return S_OK;
146 147 148 149 150 151
}

static HRESULT WINAPI HtmlLoadOptions_SetOption(IHtmlLoadOptions *iface, DWORD dwOption,
        LPVOID pBuffer, ULONG cbBuf)
{
    HTMLLoadOptions *This = LOADOPTS_THIS(iface);
152 153
    load_opt *iter = NULL;

154
    TRACE("(%p)->(%d %p %d)\n", This, dwOption, pBuffer, cbBuf);
155 156 157 158 159 160 161

    for(iter = This->opts; iter; iter = iter->next) {
        if(iter->option == dwOption)
            break;
    }

    if(!iter) {
162
        iter = heap_alloc(sizeof(load_opt));
163 164 165 166 167
        iter->next = This->opts;
        This->opts = iter;

        iter->option = dwOption;
    }else {
168
        heap_free(iter->buffer);
169 170 171 172 173 174 175 176 177 178
    }

    if(!cbBuf) {
        iter->buffer = NULL;
        iter->size = 0;

        return S_OK;
    }

    iter->size = cbBuf;
179
    iter->buffer = heap_alloc(cbBuf);
180 181 182
    memcpy(iter->buffer, pBuffer, iter->size);

    return S_OK;
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
}

#undef LOADOPTS_THIS

static const IHtmlLoadOptionsVtbl HtmlLoadOptionsVtbl = {
    HtmlLoadOptions_QueryInterface,
    HtmlLoadOptions_AddRef,
    HtmlLoadOptions_Release,
    HtmlLoadOptions_QueryOption,
    HtmlLoadOptions_SetOption
};

HRESULT HTMLLoadOptions_Create(IUnknown *pUnkOuter, REFIID riid, void** ppv)
{
    HTMLLoadOptions *ret;
    HRESULT hres;

    TRACE("(%p %s %p)\n", pUnkOuter, debugstr_guid(riid), ppv);

202
    ret = heap_alloc(sizeof(HTMLLoadOptions));
203 204 205

    ret->lpHtmlLoadOptionsVtbl = &HtmlLoadOptionsVtbl;
    ret->ref = 1;
206
    ret->opts = NULL;
207 208 209 210 211 212

    hres = IHtmlLoadOptions_QueryInterface(LOADOPTS(ret), riid, ppv);
    IHtmlLoadOptions_Release(LOADOPTS(ret));

    return hres;
}