htmlscript.c 13.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright 2008 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 <stdarg.h>
20
#include <assert.h>
21 22 23 24 25 26 27 28 29 30 31

#define COBJMACROS

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

#include "wine/debug.h"

#include "mshtml_private.h"
32
#include "htmlscript.h"
33 34 35

WINE_DEFAULT_DEBUG_CHANNEL(mshtml);

36 37 38 39
static inline HTMLScriptElement *impl_from_IHTMLScriptElement(IHTMLScriptElement *iface)
{
    return CONTAINING_RECORD(iface, HTMLScriptElement, IHTMLScriptElement_iface);
}
40 41 42 43

static HRESULT WINAPI HTMLScriptElement_QueryInterface(IHTMLScriptElement *iface,
        REFIID riid, void **ppv)
{
44
    HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
45

46
    return IHTMLDOMNode_QueryInterface(&This->element.node.IHTMLDOMNode_iface, riid, ppv);
47 48 49 50
}

static ULONG WINAPI HTMLScriptElement_AddRef(IHTMLScriptElement *iface)
{
51
    HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
52

53
    return IHTMLDOMNode_AddRef(&This->element.node.IHTMLDOMNode_iface);
54 55 56 57
}

static ULONG WINAPI HTMLScriptElement_Release(IHTMLScriptElement *iface)
{
58
    HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
59

60
    return IHTMLDOMNode_Release(&This->element.node.IHTMLDOMNode_iface);
61 62 63 64
}

static HRESULT WINAPI HTMLScriptElement_GetTypeInfoCount(IHTMLScriptElement *iface, UINT *pctinfo)
{
65
    HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
66
    return IDispatchEx_GetTypeInfoCount(&This->element.node.dispex.IDispatchEx_iface, pctinfo);
67 68 69 70 71
}

static HRESULT WINAPI HTMLScriptElement_GetTypeInfo(IHTMLScriptElement *iface, UINT iTInfo,
                                              LCID lcid, ITypeInfo **ppTInfo)
{
72
    HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
73 74
    return IDispatchEx_GetTypeInfo(&This->element.node.dispex.IDispatchEx_iface, iTInfo, lcid,
            ppTInfo);
75 76 77 78 79 80
}

static HRESULT WINAPI HTMLScriptElement_GetIDsOfNames(IHTMLScriptElement *iface, REFIID riid,
                                                LPOLESTR *rgszNames, UINT cNames,
                                                LCID lcid, DISPID *rgDispId)
{
81
    HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
82 83
    return IDispatchEx_GetIDsOfNames(&This->element.node.dispex.IDispatchEx_iface, riid, rgszNames,
            cNames, lcid, rgDispId);
84 85 86 87 88 89
}

static HRESULT WINAPI HTMLScriptElement_Invoke(IHTMLScriptElement *iface, DISPID dispIdMember,
                            REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
                            VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
90
    HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
91 92
    return IDispatchEx_Invoke(&This->element.node.dispex.IDispatchEx_iface, dispIdMember, riid,
            lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
93 94 95 96
}

static HRESULT WINAPI HTMLScriptElement_put_src(IHTMLScriptElement *iface, BSTR v)
{
97
    HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
98 99 100 101 102
    HTMLInnerWindow *window;
    nsIDOMNode *parent;
    nsAString src_str;
    nsresult nsres;

103
    TRACE("(%p)->(%s)\n", This, debugstr_w(v));
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

    if(!This->element.node.doc || !This->element.node.doc->window) {
        WARN("no windoow\n");
        return E_UNEXPECTED;
    }

    window = This->element.node.doc->window;

    nsAString_InitDepend(&src_str, v);
    nsres = nsIDOMHTMLScriptElement_SetSrc(This->nsscript, &src_str);
    nsAString_Finish(&src_str);
    if(NS_FAILED(nsres)) {
        ERR("SetSrc failed: %08x\n", nsres);
        return E_FAIL;
    }

    if(This->parsed) {
        WARN("already parsed\n");
        return S_OK;
    }

    if(window->parser_callback_cnt) {
126 127 128 129 130 131 132 133 134 135 136
        script_queue_entry_t *queue;

        queue = heap_alloc(sizeof(*queue));
        if(!queue)
            return E_OUTOFMEMORY;

        IHTMLScriptElement_AddRef(&This->IHTMLScriptElement_iface);
        queue->script = This;

        list_add_tail(&window->script_queue, &queue->entry);
        return S_OK;
137 138 139 140
    }

    nsres = nsIDOMHTMLScriptElement_GetParentNode(This->nsscript, &parent);
    if(NS_FAILED(nsres) || !parent) {
141 142 143
        TRACE("No parent, not executing\n");
        This->parse_on_bind = TRUE;
        return S_OK;
144 145 146 147 148
    }

    nsIDOMNode_Release(parent);
    doc_insert_script(window, This);
    return S_OK;
149 150 151 152
}

static HRESULT WINAPI HTMLScriptElement_get_src(IHTMLScriptElement *iface, BSTR *p)
{
153
    HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
154 155 156 157 158 159 160 161
    nsAString src_str;
    nsresult nsres;

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

    nsAString_Init(&src_str, NULL);
    nsres = nsIDOMHTMLScriptElement_GetSrc(This->nsscript, &src_str);
    return return_nsstr(nsres, &src_str, p);
162 163 164 165
}

static HRESULT WINAPI HTMLScriptElement_put_htmlFor(IHTMLScriptElement *iface, BSTR v)
{
166
    HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
167 168 169 170 171 172
    FIXME("(%p)->(%s)\n", This, debugstr_w(v));
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLScriptElement_get_htmlFor(IHTMLScriptElement *iface, BSTR *p)
{
173
    HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
174 175 176 177 178 179
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLScriptElement_put_event(IHTMLScriptElement *iface, BSTR v)
{
180
    HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
181 182 183 184 185 186
    FIXME("(%p)->(%s)\n", This, debugstr_w(v));
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLScriptElement_get_event(IHTMLScriptElement *iface, BSTR *p)
{
187
    HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
188 189 190 191 192 193
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLScriptElement_put_text(IHTMLScriptElement *iface, BSTR v)
{
194
    HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
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
    HTMLInnerWindow *window;
    nsIDOMNode *parent;
    nsAString text_str;
    nsresult nsres;

    TRACE("(%p)->(%s)\n", This, debugstr_w(v));

    if(!This->element.node.doc || !This->element.node.doc->window) {
        WARN("no windoow\n");
        return E_UNEXPECTED;
    }

    window = This->element.node.doc->window;

    nsAString_InitDepend(&text_str, v);
    nsres = nsIDOMHTMLScriptElement_SetText(This->nsscript, &text_str);
    nsAString_Finish(&text_str);
    if(NS_FAILED(nsres)) {
        ERR("SetSrc failed: %08x\n", nsres);
        return E_FAIL;
    }

    nsres = nsIDOMHTMLScriptElement_GetParentNode(This->nsscript, &parent);
    if(NS_FAILED(nsres) || !parent) {
        TRACE("No parent, not executing\n");
        This->parse_on_bind = TRUE;
        return S_OK;
    }

    nsIDOMNode_Release(parent);
    doc_insert_script(window, This);
    return S_OK;
227 228 229 230
}

static HRESULT WINAPI HTMLScriptElement_get_text(IHTMLScriptElement *iface, BSTR *p)
{
231
    HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
232 233 234 235 236 237 238 239
    nsAString nsstr;
    nsresult nsres;

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

    nsAString_Init(&nsstr, NULL);
    nsres = nsIDOMHTMLScriptElement_GetText(This->nsscript, &nsstr);
    return return_nsstr(nsres, &nsstr, p);
240 241 242 243
}

static HRESULT WINAPI HTMLScriptElement_put_defer(IHTMLScriptElement *iface, VARIANT_BOOL v)
{
244
    HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
245 246 247 248 249 250 251 252 253 254 255 256
    HRESULT hr = S_OK;
    nsresult nsres;

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

    nsres = nsIDOMHTMLScriptElement_SetDefer(This->nsscript, v != VARIANT_FALSE);
    if(NS_FAILED(nsres))
    {
        hr = E_FAIL;
    }

    return hr;
257 258 259 260
}

static HRESULT WINAPI HTMLScriptElement_get_defer(IHTMLScriptElement *iface, VARIANT_BOOL *p)
{
261
    HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
262
    cpp_bool defer = FALSE;
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
    nsresult nsres;

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

    if(!p)
        return E_INVALIDARG;

    nsres = nsIDOMHTMLScriptElement_GetDefer(This->nsscript, &defer);
    if(NS_FAILED(nsres)) {
        ERR("GetSrc failed: %08x\n", nsres);
    }

    *p = defer ? VARIANT_TRUE : VARIANT_FALSE;

    TRACE("*p = %d\n", *p);
    return S_OK;
279 280 281 282
}

static HRESULT WINAPI HTMLScriptElement_get_readyState(IHTMLScriptElement *iface, BSTR *p)
{
283
    HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
284 285 286 287 288 289
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLScriptElement_put_onerror(IHTMLScriptElement *iface, VARIANT v)
{
290
    HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
291
    FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
292 293 294 295 296
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLScriptElement_get_onerror(IHTMLScriptElement *iface, VARIANT *p)
{
297
    HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
298 299 300 301 302 303
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLScriptElement_put_type(IHTMLScriptElement *iface, BSTR v)
{
304
    HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
305 306 307 308 309 310 311 312 313 314 315 316
    nsAString nstype_str;
    nsresult nsres;

    TRACE("(%p)->(%s)\n", This, debugstr_w(v));

    nsAString_Init(&nstype_str, v);
    nsres = nsIDOMHTMLScriptElement_SetType(This->nsscript, &nstype_str);
    if (NS_FAILED(nsres))
        ERR("SetType failed: %08x\n", nsres);
    nsAString_Finish (&nstype_str);

    return S_OK;
317 318 319 320
}

static HRESULT WINAPI HTMLScriptElement_get_type(IHTMLScriptElement *iface, BSTR *p)
{
321
    HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
322 323 324 325 326 327 328
    nsAString nstype_str;
    nsresult nsres;

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

    nsAString_Init(&nstype_str, NULL);
    nsres = nsIDOMHTMLScriptElement_GetType(This->nsscript, &nstype_str);
329
    return return_nsstr(nsres, &nstype_str, p);
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
}

static const IHTMLScriptElementVtbl HTMLScriptElementVtbl = {
    HTMLScriptElement_QueryInterface,
    HTMLScriptElement_AddRef,
    HTMLScriptElement_Release,
    HTMLScriptElement_GetTypeInfoCount,
    HTMLScriptElement_GetTypeInfo,
    HTMLScriptElement_GetIDsOfNames,
    HTMLScriptElement_Invoke,
    HTMLScriptElement_put_src,
    HTMLScriptElement_get_src,
    HTMLScriptElement_put_htmlFor,
    HTMLScriptElement_get_htmlFor,
    HTMLScriptElement_put_event,
    HTMLScriptElement_get_event,
    HTMLScriptElement_put_text,
    HTMLScriptElement_get_text,
    HTMLScriptElement_put_defer,
    HTMLScriptElement_get_defer,
    HTMLScriptElement_get_readyState,
    HTMLScriptElement_put_onerror,
    HTMLScriptElement_get_onerror,
    HTMLScriptElement_put_type,
    HTMLScriptElement_get_type
};

357 358 359 360
static inline HTMLScriptElement *impl_from_HTMLDOMNode(HTMLDOMNode *iface)
{
    return CONTAINING_RECORD(iface, HTMLScriptElement, element.node);
}
361 362 363

static HRESULT HTMLScriptElement_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
{
364
    HTMLScriptElement *This = impl_from_HTMLDOMNode(iface);
365 366 367 368 369

    *ppv = NULL;

    if(IsEqualGUID(&IID_IUnknown, riid)) {
        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
370
        *ppv = &This->IHTMLScriptElement_iface;
371 372
    }else if(IsEqualGUID(&IID_IDispatch, riid)) {
        TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
373
        *ppv = &This->IHTMLScriptElement_iface;
374 375
    }else if(IsEqualGUID(&IID_IHTMLScriptElement, riid)) {
        TRACE("(%p)->(IID_IHTMLScriptElement %p)\n", This, ppv);
376
        *ppv = &This->IHTMLScriptElement_iface;
377 378 379 380 381 382 383 384 385 386
    }

    if(*ppv) {
        IUnknown_AddRef((IUnknown*)*ppv);
        return S_OK;
    }

    return HTMLElement_QI(&This->element.node, riid, ppv);
}

387 388
static HRESULT HTMLScriptElement_get_readystate(HTMLDOMNode *iface, BSTR *p)
{
389
    HTMLScriptElement *This = impl_from_HTMLDOMNode(iface);
390

391
    return IHTMLScriptElement_get_readyState(&This->IHTMLScriptElement_iface, p);
392 393
}

394 395
static const NodeImplVtbl HTMLScriptElementImplVtbl = {
    HTMLScriptElement_QI,
396
    HTMLElement_destructor,
397
    HTMLElement_cpc,
398
    HTMLElement_clone,
399
    HTMLElement_handle_event,
400
    HTMLElement_get_attr_col,
401 402 403 404 405 406
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    HTMLScriptElement_get_readystate
407 408
};

409 410 411 412 413 414 415 416 417 418 419 420 421 422
HRESULT script_elem_from_nsscript(HTMLDocumentNode *doc, nsIDOMHTMLScriptElement *nsscript, HTMLScriptElement **ret)
{
    HTMLDOMNode *node;
    HRESULT hres;

    hres = get_node(doc, (nsIDOMNode*)nsscript, TRUE, &node);
    if(FAILED(hres))
        return hres;

    assert(node->vtbl == &HTMLScriptElementImplVtbl);
    *ret = impl_from_HTMLDOMNode(node);
    return S_OK;
}

423 424 425 426 427 428 429 430 431 432 433 434 435
static const tid_t HTMLScriptElement_iface_tids[] = {
    HTMLELEMENT_TIDS,
    IHTMLScriptElement_tid,
    0
};

static dispex_static_data_t HTMLScriptElement_dispex = {
    NULL,
    DispHTMLScriptElement_tid,
    NULL,
    HTMLScriptElement_iface_tids
};

436
HRESULT HTMLScriptElement_Create(HTMLDocumentNode *doc, nsIDOMHTMLElement *nselem, HTMLElement **elem)
437
{
438
    HTMLScriptElement *ret;
439
    nsresult nsres;
440

441 442 443 444
    ret = heap_alloc_zero(sizeof(HTMLScriptElement));
    if(!ret)
        return E_OUTOFMEMORY;

445
    ret->IHTMLScriptElement_iface.lpVtbl = &HTMLScriptElementVtbl;
446 447
    ret->element.node.vtbl = &HTMLScriptElementImplVtbl;

448 449
    HTMLElement_Init(&ret->element, doc, nselem, &HTMLScriptElement_dispex);

450
    nsres = nsIDOMHTMLElement_QueryInterface(nselem, &IID_nsIDOMHTMLScriptElement, (void**)&ret->nsscript);
451

452 453 454
    /* Share nsscript reference with nsnode */
    assert(nsres == NS_OK && (nsIDOMNode*)ret->nsscript == ret->element.node.nsnode);
    nsIDOMNode_Release(ret->element.node.nsnode);
455

456 457
    *elem = &ret->element;
    return S_OK;
458
}