mshtml: Don't share nsscript reference with nsnode.
[wine.git] / dlls / mshtml / htmlscript.c
blobdcab32fb7f9dfd8c6e21d848139351705c7ae14d
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
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
19 #include <stdarg.h>
20 #include <assert.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "ole2.h"
29 #include "wine/debug.h"
31 #include "mshtml_private.h"
32 #include "htmlscript.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
36 static inline HTMLScriptElement *impl_from_IHTMLScriptElement(IHTMLScriptElement *iface)
38 return CONTAINING_RECORD(iface, HTMLScriptElement, IHTMLScriptElement_iface);
41 static HRESULT WINAPI HTMLScriptElement_QueryInterface(IHTMLScriptElement *iface,
42 REFIID riid, void **ppv)
44 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
46 return IHTMLDOMNode_QueryInterface(&This->element.node.IHTMLDOMNode_iface, riid, ppv);
49 static ULONG WINAPI HTMLScriptElement_AddRef(IHTMLScriptElement *iface)
51 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
53 return IHTMLDOMNode_AddRef(&This->element.node.IHTMLDOMNode_iface);
56 static ULONG WINAPI HTMLScriptElement_Release(IHTMLScriptElement *iface)
58 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
60 return IHTMLDOMNode_Release(&This->element.node.IHTMLDOMNode_iface);
63 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);
69 static HRESULT WINAPI HTMLScriptElement_GetTypeInfo(IHTMLScriptElement *iface, UINT iTInfo,
70 LCID lcid, ITypeInfo **ppTInfo)
72 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
73 return IDispatchEx_GetTypeInfo(&This->element.node.dispex.IDispatchEx_iface, iTInfo, lcid,
74 ppTInfo);
77 static HRESULT WINAPI HTMLScriptElement_GetIDsOfNames(IHTMLScriptElement *iface, REFIID riid,
78 LPOLESTR *rgszNames, UINT cNames,
79 LCID lcid, DISPID *rgDispId)
81 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
82 return IDispatchEx_GetIDsOfNames(&This->element.node.dispex.IDispatchEx_iface, riid, rgszNames,
83 cNames, lcid, rgDispId);
86 static HRESULT WINAPI HTMLScriptElement_Invoke(IHTMLScriptElement *iface, DISPID dispIdMember,
87 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
88 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
90 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
91 return IDispatchEx_Invoke(&This->element.node.dispex.IDispatchEx_iface, dispIdMember, riid,
92 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
95 static HRESULT WINAPI HTMLScriptElement_put_src(IHTMLScriptElement *iface, BSTR v)
97 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
98 HTMLInnerWindow *window;
99 nsIDOMNode *parent;
100 nsAString src_str;
101 nsresult nsres;
103 TRACE("(%p)->(%s)\n", This, debugstr_w(v));
105 if(!This->element.node.doc || !This->element.node.doc->window) {
106 WARN("no windoow\n");
107 return E_UNEXPECTED;
110 window = This->element.node.doc->window;
112 nsAString_InitDepend(&src_str, v);
113 nsres = nsIDOMHTMLScriptElement_SetSrc(This->nsscript, &src_str);
114 nsAString_Finish(&src_str);
115 if(NS_FAILED(nsres)) {
116 ERR("SetSrc failed: %08x\n", nsres);
117 return E_FAIL;
120 if(This->parsed) {
121 WARN("already parsed\n");
122 return S_OK;
125 if(window->parser_callback_cnt) {
126 script_queue_entry_t *queue;
128 queue = heap_alloc(sizeof(*queue));
129 if(!queue)
130 return E_OUTOFMEMORY;
132 IHTMLScriptElement_AddRef(&This->IHTMLScriptElement_iface);
133 queue->script = This;
135 list_add_tail(&window->script_queue, &queue->entry);
136 return S_OK;
139 nsres = nsIDOMHTMLScriptElement_GetParentNode(This->nsscript, &parent);
140 if(NS_FAILED(nsres) || !parent) {
141 TRACE("No parent, not executing\n");
142 This->parse_on_bind = TRUE;
143 return S_OK;
146 nsIDOMNode_Release(parent);
147 doc_insert_script(window, This);
148 return S_OK;
151 static HRESULT WINAPI HTMLScriptElement_get_src(IHTMLScriptElement *iface, BSTR *p)
153 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
154 nsAString src_str;
155 nsresult nsres;
157 TRACE("(%p)->(%p)\n", This, p);
159 nsAString_Init(&src_str, NULL);
160 nsres = nsIDOMHTMLScriptElement_GetSrc(This->nsscript, &src_str);
161 return return_nsstr(nsres, &src_str, p);
164 static HRESULT WINAPI HTMLScriptElement_put_htmlFor(IHTMLScriptElement *iface, BSTR v)
166 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
167 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
168 return E_NOTIMPL;
171 static HRESULT WINAPI HTMLScriptElement_get_htmlFor(IHTMLScriptElement *iface, BSTR *p)
173 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
174 FIXME("(%p)->(%p)\n", This, p);
175 return E_NOTIMPL;
178 static HRESULT WINAPI HTMLScriptElement_put_event(IHTMLScriptElement *iface, BSTR v)
180 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
181 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
182 return E_NOTIMPL;
185 static HRESULT WINAPI HTMLScriptElement_get_event(IHTMLScriptElement *iface, BSTR *p)
187 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
188 FIXME("(%p)->(%p)\n", This, p);
189 return E_NOTIMPL;
192 static HRESULT WINAPI HTMLScriptElement_put_text(IHTMLScriptElement *iface, BSTR v)
194 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
195 HTMLInnerWindow *window;
196 nsIDOMNode *parent;
197 nsAString text_str;
198 nsresult nsres;
200 TRACE("(%p)->(%s)\n", This, debugstr_w(v));
202 if(!This->element.node.doc || !This->element.node.doc->window) {
203 WARN("no windoow\n");
204 return E_UNEXPECTED;
207 window = This->element.node.doc->window;
209 nsAString_InitDepend(&text_str, v);
210 nsres = nsIDOMHTMLScriptElement_SetText(This->nsscript, &text_str);
211 nsAString_Finish(&text_str);
212 if(NS_FAILED(nsres)) {
213 ERR("SetSrc failed: %08x\n", nsres);
214 return E_FAIL;
217 nsres = nsIDOMHTMLScriptElement_GetParentNode(This->nsscript, &parent);
218 if(NS_FAILED(nsres) || !parent) {
219 TRACE("No parent, not executing\n");
220 This->parse_on_bind = TRUE;
221 return S_OK;
224 nsIDOMNode_Release(parent);
225 doc_insert_script(window, This);
226 return S_OK;
229 static HRESULT WINAPI HTMLScriptElement_get_text(IHTMLScriptElement *iface, BSTR *p)
231 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
232 nsAString nsstr;
233 nsresult nsres;
235 TRACE("(%p)->(%p)\n", This, p);
237 nsAString_Init(&nsstr, NULL);
238 nsres = nsIDOMHTMLScriptElement_GetText(This->nsscript, &nsstr);
239 return return_nsstr(nsres, &nsstr, p);
242 static HRESULT WINAPI HTMLScriptElement_put_defer(IHTMLScriptElement *iface, VARIANT_BOOL v)
244 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
245 HRESULT hr = S_OK;
246 nsresult nsres;
248 TRACE("(%p)->(%x)\n", This, v);
250 nsres = nsIDOMHTMLScriptElement_SetDefer(This->nsscript, v != VARIANT_FALSE);
251 if(NS_FAILED(nsres))
253 hr = E_FAIL;
256 return hr;
259 static HRESULT WINAPI HTMLScriptElement_get_defer(IHTMLScriptElement *iface, VARIANT_BOOL *p)
261 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
262 cpp_bool defer = FALSE;
263 nsresult nsres;
265 TRACE("(%p)->(%p)\n", This, p);
267 if(!p)
268 return E_INVALIDARG;
270 nsres = nsIDOMHTMLScriptElement_GetDefer(This->nsscript, &defer);
271 if(NS_FAILED(nsres)) {
272 ERR("GetSrc failed: %08x\n", nsres);
275 *p = defer ? VARIANT_TRUE : VARIANT_FALSE;
277 TRACE("*p = %d\n", *p);
278 return S_OK;
281 static HRESULT WINAPI HTMLScriptElement_get_readyState(IHTMLScriptElement *iface, BSTR *p)
283 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
284 FIXME("(%p)->(%p)\n", This, p);
285 return E_NOTIMPL;
288 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 return E_NOTIMPL;
295 static HRESULT WINAPI HTMLScriptElement_get_onerror(IHTMLScriptElement *iface, VARIANT *p)
297 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
298 FIXME("(%p)->(%p)\n", This, p);
299 return E_NOTIMPL;
302 static HRESULT WINAPI HTMLScriptElement_put_type(IHTMLScriptElement *iface, BSTR v)
304 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
305 nsAString nstype_str;
306 nsresult nsres;
308 TRACE("(%p)->(%s)\n", This, debugstr_w(v));
310 nsAString_Init(&nstype_str, v);
311 nsres = nsIDOMHTMLScriptElement_SetType(This->nsscript, &nstype_str);
312 if (NS_FAILED(nsres))
313 ERR("SetType failed: %08x\n", nsres);
314 nsAString_Finish (&nstype_str);
316 return S_OK;
319 static HRESULT WINAPI HTMLScriptElement_get_type(IHTMLScriptElement *iface, BSTR *p)
321 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
322 nsAString nstype_str;
323 nsresult nsres;
325 TRACE("(%p)->(%p)\n", This, p);
327 nsAString_Init(&nstype_str, NULL);
328 nsres = nsIDOMHTMLScriptElement_GetType(This->nsscript, &nstype_str);
329 return return_nsstr(nsres, &nstype_str, p);
332 static const IHTMLScriptElementVtbl HTMLScriptElementVtbl = {
333 HTMLScriptElement_QueryInterface,
334 HTMLScriptElement_AddRef,
335 HTMLScriptElement_Release,
336 HTMLScriptElement_GetTypeInfoCount,
337 HTMLScriptElement_GetTypeInfo,
338 HTMLScriptElement_GetIDsOfNames,
339 HTMLScriptElement_Invoke,
340 HTMLScriptElement_put_src,
341 HTMLScriptElement_get_src,
342 HTMLScriptElement_put_htmlFor,
343 HTMLScriptElement_get_htmlFor,
344 HTMLScriptElement_put_event,
345 HTMLScriptElement_get_event,
346 HTMLScriptElement_put_text,
347 HTMLScriptElement_get_text,
348 HTMLScriptElement_put_defer,
349 HTMLScriptElement_get_defer,
350 HTMLScriptElement_get_readyState,
351 HTMLScriptElement_put_onerror,
352 HTMLScriptElement_get_onerror,
353 HTMLScriptElement_put_type,
354 HTMLScriptElement_get_type
357 static inline HTMLScriptElement *impl_from_HTMLDOMNode(HTMLDOMNode *iface)
359 return CONTAINING_RECORD(iface, HTMLScriptElement, element.node);
362 static HRESULT HTMLScriptElement_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
364 HTMLScriptElement *This = impl_from_HTMLDOMNode(iface);
366 *ppv = NULL;
368 if(IsEqualGUID(&IID_IUnknown, riid)) {
369 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
370 *ppv = &This->IHTMLScriptElement_iface;
371 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
372 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
373 *ppv = &This->IHTMLScriptElement_iface;
374 }else if(IsEqualGUID(&IID_IHTMLScriptElement, riid)) {
375 TRACE("(%p)->(IID_IHTMLScriptElement %p)\n", This, ppv);
376 *ppv = &This->IHTMLScriptElement_iface;
379 if(*ppv) {
380 IUnknown_AddRef((IUnknown*)*ppv);
381 return S_OK;
384 return HTMLElement_QI(&This->element.node, riid, ppv);
387 static HRESULT HTMLScriptElement_get_readystate(HTMLDOMNode *iface, BSTR *p)
389 HTMLScriptElement *This = impl_from_HTMLDOMNode(iface);
391 return IHTMLScriptElement_get_readyState(&This->IHTMLScriptElement_iface, p);
394 static void HTMLScriptElement_traverse(HTMLDOMNode *iface, nsCycleCollectionTraversalCallback *cb)
396 HTMLScriptElement *This = impl_from_HTMLDOMNode(iface);
398 if(This->nsscript)
399 note_cc_edge((nsISupports*)This->nsscript, "This->nsscript", cb);
402 static void HTMLScriptElement_unlink(HTMLDOMNode *iface)
404 HTMLScriptElement *This = impl_from_HTMLDOMNode(iface);
406 if(This->nsscript) {
407 nsIDOMHTMLScriptElement *nsscript = This->nsscript;
409 This->nsscript = NULL;
410 nsIDOMHTMLScriptElement_Release(nsscript);
414 static const NodeImplVtbl HTMLScriptElementImplVtbl = {
415 HTMLScriptElement_QI,
416 HTMLElement_destructor,
417 HTMLElement_cpc,
418 HTMLElement_clone,
419 HTMLElement_handle_event,
420 HTMLElement_get_attr_col,
421 NULL,
422 NULL,
423 NULL,
424 NULL,
425 NULL,
426 HTMLScriptElement_get_readystate,
427 NULL,
428 NULL,
429 NULL,
430 HTMLScriptElement_traverse,
431 HTMLScriptElement_unlink
434 HRESULT script_elem_from_nsscript(HTMLDocumentNode *doc, nsIDOMHTMLScriptElement *nsscript, HTMLScriptElement **ret)
436 HTMLDOMNode *node;
437 HRESULT hres;
439 hres = get_node(doc, (nsIDOMNode*)nsscript, TRUE, &node);
440 if(FAILED(hres))
441 return hres;
443 assert(node->vtbl == &HTMLScriptElementImplVtbl);
444 *ret = impl_from_HTMLDOMNode(node);
445 return S_OK;
448 static const tid_t HTMLScriptElement_iface_tids[] = {
449 HTMLELEMENT_TIDS,
450 IHTMLScriptElement_tid,
454 static dispex_static_data_t HTMLScriptElement_dispex = {
455 NULL,
456 DispHTMLScriptElement_tid,
457 NULL,
458 HTMLScriptElement_iface_tids
461 HRESULT HTMLScriptElement_Create(HTMLDocumentNode *doc, nsIDOMHTMLElement *nselem, HTMLElement **elem)
463 HTMLScriptElement *ret;
464 nsresult nsres;
466 ret = heap_alloc_zero(sizeof(HTMLScriptElement));
467 if(!ret)
468 return E_OUTOFMEMORY;
470 ret->IHTMLScriptElement_iface.lpVtbl = &HTMLScriptElementVtbl;
471 ret->element.node.vtbl = &HTMLScriptElementImplVtbl;
473 HTMLElement_Init(&ret->element, doc, nselem, &HTMLScriptElement_dispex);
475 nsres = nsIDOMHTMLElement_QueryInterface(nselem, &IID_nsIDOMHTMLScriptElement, (void**)&ret->nsscript);
476 assert(nsres == NS_OK);
478 *elem = &ret->element;
479 return S_OK;