crypt32: NULL ptr could leak into function (Coverity).
[wine] / dlls / mshtml / htmloption.c
1 /*
2  * Copyright 2007 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <stdarg.h>
20
21 #define COBJMACROS
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
27
28 #include "wine/debug.h"
29
30 #include "mshtml_private.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
33
34 struct HTMLOptionElement {
35     HTMLElement element;
36
37     const IHTMLOptionElementVtbl *lpHTMLOptionElementVtbl;
38
39     nsIDOMHTMLOptionElement *nsoption;
40 };
41
42 #define HTMLOPTION(x)  (&(x)->lpHTMLOptionElementVtbl)
43
44 #define HTMLOPTION_THIS(iface) DEFINE_THIS(HTMLOptionElement, HTMLOptionElement, iface)
45
46 static HRESULT WINAPI HTMLOptionElement_QueryInterface(IHTMLOptionElement *iface,
47         REFIID riid, void **ppv)
48 {
49     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
50
51     return IHTMLDOMNode_QueryInterface(&This->element.node.IHTMLDOMNode_iface, riid, ppv);
52 }
53
54 static ULONG WINAPI HTMLOptionElement_AddRef(IHTMLOptionElement *iface)
55 {
56     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
57
58     return IHTMLDOMNode_AddRef(&This->element.node.IHTMLDOMNode_iface);
59 }
60
61 static ULONG WINAPI HTMLOptionElement_Release(IHTMLOptionElement *iface)
62 {
63     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
64
65     return IHTMLDOMNode_Release(&This->element.node.IHTMLDOMNode_iface);
66 }
67
68 static HRESULT WINAPI HTMLOptionElement_GetTypeInfoCount(IHTMLOptionElement *iface, UINT *pctinfo)
69 {
70     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
71     return IDispatchEx_GetTypeInfoCount(&This->element.node.dispex.IDispatchEx_iface, pctinfo);
72 }
73
74 static HRESULT WINAPI HTMLOptionElement_GetTypeInfo(IHTMLOptionElement *iface, UINT iTInfo,
75                                               LCID lcid, ITypeInfo **ppTInfo)
76 {
77     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
78     return IDispatchEx_GetTypeInfo(&This->element.node.dispex.IDispatchEx_iface, iTInfo, lcid,
79             ppTInfo);
80 }
81
82 static HRESULT WINAPI HTMLOptionElement_GetIDsOfNames(IHTMLOptionElement *iface, REFIID riid,
83                                                 LPOLESTR *rgszNames, UINT cNames,
84                                                 LCID lcid, DISPID *rgDispId)
85 {
86     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
87     return IDispatchEx_GetIDsOfNames(&This->element.node.dispex.IDispatchEx_iface, riid, rgszNames,
88             cNames, lcid, rgDispId);
89 }
90
91 static HRESULT WINAPI HTMLOptionElement_Invoke(IHTMLOptionElement *iface, DISPID dispIdMember,
92                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
93                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
94 {
95     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
96     return IDispatchEx_Invoke(&This->element.node.dispex.IDispatchEx_iface, dispIdMember, riid,
97             lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
98 }
99
100 static HRESULT WINAPI HTMLOptionElement_put_selected(IHTMLOptionElement *iface, VARIANT_BOOL v)
101 {
102     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
103     nsresult nsres;
104
105     TRACE("(%p)->(%x)\n", This, v);
106
107     nsres = nsIDOMHTMLOptionElement_SetSelected(This->nsoption, v != VARIANT_FALSE);
108     if(NS_FAILED(nsres)) {
109         ERR("SetSelected failed: %08x\n", nsres);
110         return E_FAIL;
111     }
112
113     return S_OK;
114 }
115
116 static HRESULT WINAPI HTMLOptionElement_get_selected(IHTMLOptionElement *iface, VARIANT_BOOL *p)
117 {
118     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
119     PRBool selected;
120     nsresult nsres;
121
122     TRACE("(%p)->(%p)\n", This, p);
123
124     nsres = nsIDOMHTMLOptionElement_GetSelected(This->nsoption, &selected);
125     if(NS_FAILED(nsres)) {
126         ERR("GetSelected failed: %08x\n", nsres);
127         return E_FAIL;
128     }
129
130     *p = selected ? VARIANT_TRUE : VARIANT_FALSE;
131     return S_OK;
132 }
133
134 static HRESULT WINAPI HTMLOptionElement_put_value(IHTMLOptionElement *iface, BSTR v)
135 {
136     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
137     nsAString value_str;
138     nsresult nsres;
139
140     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
141
142     nsAString_InitDepend(&value_str, v);
143     nsres = nsIDOMHTMLOptionElement_SetValue(This->nsoption, &value_str);
144     nsAString_Finish(&value_str);
145     if(NS_FAILED(nsres))
146         ERR("SetValue failed: %08x\n", nsres);
147
148     return S_OK;
149 }
150
151 static HRESULT WINAPI HTMLOptionElement_get_value(IHTMLOptionElement *iface, BSTR *p)
152 {
153     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
154     nsAString value_str;
155     const PRUnichar *value;
156     nsresult nsres;
157
158     TRACE("(%p)->(%p)\n", This, p);
159
160     nsAString_Init(&value_str, NULL);
161     nsres = nsIDOMHTMLOptionElement_GetValue(This->nsoption, &value_str);
162     if(NS_SUCCEEDED(nsres)) {
163         nsAString_GetData(&value_str, &value);
164         *p = SysAllocString(value);
165     }else {
166         ERR("GetValue failed: %08x\n", nsres);
167         *p = NULL;
168     }
169     nsAString_Finish(&value_str);
170
171     return S_OK;
172 }
173
174 static HRESULT WINAPI HTMLOptionElement_put_defaultSelected(IHTMLOptionElement *iface, VARIANT_BOOL v)
175 {
176     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
177     FIXME("(%p)->(%x)\n", This, v);
178     return E_NOTIMPL;
179 }
180
181 static HRESULT WINAPI HTMLOptionElement_get_defaultSelected(IHTMLOptionElement *iface, VARIANT_BOOL *p)
182 {
183     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
184     FIXME("(%p)->(%p)\n", This, p);
185     return E_NOTIMPL;
186 }
187
188 static HRESULT WINAPI HTMLOptionElement_put_index(IHTMLOptionElement *iface, LONG v)
189 {
190     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
191     FIXME("(%p)->(%d)\n", This, v);
192     return E_NOTIMPL;
193 }
194
195 static HRESULT WINAPI HTMLOptionElement_get_index(IHTMLOptionElement *iface, LONG *p)
196 {
197     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
198     FIXME("(%p)->(%p)\n", This, p);
199     return E_NOTIMPL;
200 }
201
202 static HRESULT WINAPI HTMLOptionElement_put_text(IHTMLOptionElement *iface, BSTR v)
203 {
204     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
205     nsIDOMText *text_node;
206     nsAString text_str;
207     nsIDOMNode *tmp;
208     nsresult nsres;
209
210     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
211
212     if(!This->element.node.doc->nsdoc) {
213         WARN("NULL nsdoc\n");
214         return E_UNEXPECTED;
215     }
216
217     while(1) {
218         nsIDOMNode *child;
219
220         nsres = nsIDOMHTMLOptionElement_GetFirstChild(This->nsoption, &child);
221         if(NS_FAILED(nsres) || !child)
222             break;
223
224         nsres = nsIDOMHTMLOptionElement_RemoveChild(This->nsoption, child, &tmp);
225         nsIDOMNode_Release(child);
226         if(NS_SUCCEEDED(nsres)) {
227             nsIDOMNode_Release(tmp);
228         }else {
229             ERR("RemoveChild failed: %08x\n", nsres);
230             break;
231         }
232     }
233
234     nsAString_InitDepend(&text_str, v);
235     nsres = nsIDOMHTMLDocument_CreateTextNode(This->element.node.doc->nsdoc, &text_str, &text_node);
236     nsAString_Finish(&text_str);
237     if(NS_FAILED(nsres)) {
238         ERR("CreateTextNode failed: %08x\n", nsres);
239         return E_FAIL;
240     }
241
242     nsres = nsIDOMHTMLOptionElement_AppendChild(This->nsoption, (nsIDOMNode*)text_node, &tmp);
243     if(NS_SUCCEEDED(nsres))
244         nsIDOMNode_Release(tmp);
245     else
246         ERR("AppendChild failed: %08x\n", nsres);
247
248     return S_OK;
249 }
250
251 static HRESULT WINAPI HTMLOptionElement_get_text(IHTMLOptionElement *iface, BSTR *p)
252 {
253     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
254     nsAString text_str;
255     const PRUnichar *text;
256     nsresult nsres;
257
258     TRACE("(%p)->(%p)\n", This, p);
259
260     nsAString_Init(&text_str, NULL);
261     nsres = nsIDOMHTMLOptionElement_GetText(This->nsoption, &text_str);
262     if(NS_SUCCEEDED(nsres)) {
263         nsAString_GetData(&text_str, &text);
264         *p = SysAllocString(text);
265     }else {
266         ERR("GetText failed: %08x\n", nsres);
267         *p = NULL;
268     }
269     nsAString_Finish(&text_str);
270
271     return S_OK;
272 }
273
274 static HRESULT WINAPI HTMLOptionElement_get_form(IHTMLOptionElement *iface, IHTMLFormElement **p)
275 {
276     HTMLOptionElement *This = HTMLOPTION_THIS(iface);
277     FIXME("(%p)->(%p)\n", This, p);
278     return E_NOTIMPL;
279 }
280
281 #undef HTMLOPTION_THIS
282
283 static const IHTMLOptionElementVtbl HTMLOptionElementVtbl = {
284     HTMLOptionElement_QueryInterface,
285     HTMLOptionElement_AddRef,
286     HTMLOptionElement_Release,
287     HTMLOptionElement_GetTypeInfoCount,
288     HTMLOptionElement_GetTypeInfo,
289     HTMLOptionElement_GetIDsOfNames,
290     HTMLOptionElement_Invoke,
291     HTMLOptionElement_put_selected,
292     HTMLOptionElement_get_selected,
293     HTMLOptionElement_put_value,
294     HTMLOptionElement_get_value,
295     HTMLOptionElement_put_defaultSelected,
296     HTMLOptionElement_get_defaultSelected,
297     HTMLOptionElement_put_index,
298     HTMLOptionElement_get_index,
299     HTMLOptionElement_put_text,
300     HTMLOptionElement_get_text,
301     HTMLOptionElement_get_form
302 };
303
304 static inline HTMLOptionElement *impl_from_HTMLDOMNode(HTMLDOMNode *iface)
305 {
306     return CONTAINING_RECORD(iface, HTMLOptionElement, element.node);
307 }
308
309 static HRESULT HTMLOptionElement_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
310 {
311     HTMLOptionElement *This = impl_from_HTMLDOMNode(iface);
312
313     *ppv = NULL;
314
315     if(IsEqualGUID(&IID_IUnknown, riid)) {
316         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
317         *ppv = HTMLOPTION(This);
318     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
319         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
320         *ppv = HTMLOPTION(This);
321     }else if(IsEqualGUID(&IID_IHTMLOptionElement, riid)) {
322         TRACE("(%p)->(IID_IHTMLOptionElement %p)\n", This, ppv);
323         *ppv = HTMLOPTION(This);
324     }
325
326     if(*ppv) {
327         IUnknown_AddRef((IUnknown*)*ppv);
328         return S_OK;
329     }
330
331     return HTMLElement_QI(&This->element.node, riid, ppv);
332 }
333
334 static void HTMLOptionElement_destructor(HTMLDOMNode *iface)
335 {
336     HTMLOptionElement *This = impl_from_HTMLDOMNode(iface);
337
338     if(This->nsoption)
339         nsIDOMHTMLOptionElement_Release(This->nsoption);
340
341     HTMLElement_destructor(&This->element.node);
342 }
343
344 static const NodeImplVtbl HTMLOptionElementImplVtbl = {
345     HTMLOptionElement_QI,
346     HTMLOptionElement_destructor,
347     HTMLElement_clone
348 };
349
350 static const tid_t HTMLOptionElement_iface_tids[] = {
351     HTMLELEMENT_TIDS,
352     IHTMLOptionElement_tid,
353     0
354 };
355 static dispex_static_data_t HTMLOptionElement_dispex = {
356     NULL,
357     DispHTMLOptionElement_tid,
358     NULL,
359     HTMLOptionElement_iface_tids
360 };
361
362 HRESULT HTMLOptionElement_Create(HTMLDocumentNode *doc, nsIDOMHTMLElement *nselem, HTMLElement **elem)
363 {
364     HTMLOptionElement *ret;
365     nsresult nsres;
366
367     ret = heap_alloc_zero(sizeof(HTMLOptionElement));
368     if(!ret)
369         return E_OUTOFMEMORY;
370
371     ret->lpHTMLOptionElementVtbl = &HTMLOptionElementVtbl;
372     ret->element.node.vtbl = &HTMLOptionElementImplVtbl;
373
374     nsres = nsIDOMHTMLElement_QueryInterface(nselem, &IID_nsIDOMHTMLOptionElement, (void**)&ret->nsoption);
375     if(NS_FAILED(nsres)) {
376         ERR("Could not get nsIDOMHTMLOptionElement interface: %08x\n", nsres);
377         heap_free(ret);
378         return E_FAIL;
379     }
380
381     HTMLElement_Init(&ret->element, doc, nselem, &HTMLOptionElement_dispex);
382
383     *elem = &ret->element;
384     return S_OK;
385 }
386
387 #define HTMLOPTFACTORY_THIS(iface) DEFINE_THIS(HTMLOptionElementFactory, HTMLOptionElementFactory, iface)
388
389 static HRESULT WINAPI HTMLOptionElementFactory_QueryInterface(IHTMLOptionElementFactory *iface,
390                                                               REFIID riid, void **ppv)
391 {
392     HTMLOptionElementFactory *This = HTMLOPTFACTORY_THIS(iface);
393
394     *ppv = NULL;
395
396     if(IsEqualGUID(&IID_IUnknown, riid)) {
397         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
398         *ppv = HTMLOPTFACTORY(This);
399     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
400         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
401         *ppv = HTMLOPTFACTORY(This);
402     }else if(IsEqualGUID(&IID_IHTMLOptionElementFactory, riid)) {
403         TRACE("(%p)->(IID_IHTMLOptionElementFactory %p)\n", This, ppv);
404         *ppv = HTMLOPTFACTORY(This);
405     }
406
407     if(*ppv) {
408         IUnknown_AddRef((IUnknown*)*ppv);
409         return S_OK;
410     }
411
412     WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
413     return E_NOINTERFACE;
414 }
415
416 static ULONG WINAPI HTMLOptionElementFactory_AddRef(IHTMLOptionElementFactory *iface)
417 {
418     HTMLOptionElementFactory *This = HTMLOPTFACTORY_THIS(iface);
419     LONG ref = InterlockedIncrement(&This->ref);
420
421     TRACE("(%p) ref=%d\n", This, ref);
422
423     return ref;
424 }
425
426 static ULONG WINAPI HTMLOptionElementFactory_Release(IHTMLOptionElementFactory *iface)
427 {
428     HTMLOptionElementFactory *This = HTMLOPTFACTORY_THIS(iface);
429     LONG ref = InterlockedDecrement(&This->ref);
430
431     TRACE("(%p) ref=%d\n", This, ref);
432
433     if(!ref)
434         heap_free(This);
435
436     return ref;
437 }
438
439 static HRESULT WINAPI HTMLOptionElementFactory_GetTypeInfoCount(IHTMLOptionElementFactory *iface, UINT *pctinfo)
440 {
441     HTMLOptionElementFactory *This = HTMLOPTFACTORY_THIS(iface);
442     FIXME("(%p)->(%p)\n", This, pctinfo);
443     return E_NOTIMPL;
444 }
445
446 static HRESULT WINAPI HTMLOptionElementFactory_GetTypeInfo(IHTMLOptionElementFactory *iface, UINT iTInfo,
447                                               LCID lcid, ITypeInfo **ppTInfo)
448 {
449     HTMLOptionElementFactory *This = HTMLOPTFACTORY_THIS(iface);
450     FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
451     return E_NOTIMPL;
452 }
453
454 static HRESULT WINAPI HTMLOptionElementFactory_GetIDsOfNames(IHTMLOptionElementFactory *iface, REFIID riid,
455                                                 LPOLESTR *rgszNames, UINT cNames,
456                                                 LCID lcid, DISPID *rgDispId)
457 {
458     HTMLOptionElementFactory *This = HTMLOPTFACTORY_THIS(iface);
459     FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
460                                         lcid, rgDispId);
461     return E_NOTIMPL;
462 }
463
464 static HRESULT WINAPI HTMLOptionElementFactory_Invoke(IHTMLOptionElementFactory *iface, DISPID dispIdMember,
465                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
466                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
467 {
468     HTMLOptionElementFactory *This = HTMLOPTFACTORY_THIS(iface);
469     FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
470             lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
471     return E_NOTIMPL;
472 }
473
474 static HRESULT WINAPI HTMLOptionElementFactory_create(IHTMLOptionElementFactory *iface,
475         VARIANT text, VARIANT value, VARIANT defaultselected, VARIANT selected,
476         IHTMLOptionElement **optelem)
477 {
478     HTMLOptionElementFactory *This = HTMLOPTFACTORY_THIS(iface);
479     nsIDOMHTMLElement *nselem;
480     HTMLDOMNode *node;
481     HRESULT hres;
482
483     static const PRUnichar optionW[] = {'O','P','T','I','O','N',0};
484
485     TRACE("(%p)->(%s %s %s %s %p)\n", This, debugstr_variant(&text), debugstr_variant(&value),
486           debugstr_variant(&defaultselected), debugstr_variant(&selected), optelem);
487
488     if(!This->window || !This->window->doc) {
489         WARN("NULL doc\n");
490         return E_UNEXPECTED;
491     }
492
493     *optelem = NULL;
494
495     hres = create_nselem(This->window->doc, optionW, &nselem);
496     if(FAILED(hres))
497         return hres;
498
499     hres = get_node(This->window->doc, (nsIDOMNode*)nselem, TRUE, &node);
500     nsIDOMHTMLElement_Release(nselem);
501     if(FAILED(hres))
502         return hres;
503
504     hres = IHTMLDOMNode_QueryInterface(&node->IHTMLDOMNode_iface,
505             &IID_IHTMLOptionElement, (void**)optelem);
506
507     if(V_VT(&text) == VT_BSTR)
508         IHTMLOptionElement_put_text(*optelem, V_BSTR(&text));
509     else if(V_VT(&text) != VT_EMPTY)
510         FIXME("Unsupported text vt=%d\n", V_VT(&text));
511
512     if(V_VT(&value) == VT_BSTR)
513         IHTMLOptionElement_put_value(*optelem, V_BSTR(&value));
514     else if(V_VT(&value) != VT_EMPTY)
515         FIXME("Unsupported value vt=%d\n", V_VT(&value));
516
517     if(V_VT(&defaultselected) != VT_EMPTY)
518         FIXME("Unsupported defaultselected vt=%d\n", V_VT(&defaultselected));
519     if(V_VT(&selected) != VT_EMPTY)
520         FIXME("Unsupported selected vt=%d\n", V_VT(&selected));
521
522     return S_OK;
523 }
524
525 #undef HTMLOPTFACTORY_THIS
526
527 static const IHTMLOptionElementFactoryVtbl HTMLOptionElementFactoryVtbl = {
528     HTMLOptionElementFactory_QueryInterface,
529     HTMLOptionElementFactory_AddRef,
530     HTMLOptionElementFactory_Release,
531     HTMLOptionElementFactory_GetTypeInfoCount,
532     HTMLOptionElementFactory_GetTypeInfo,
533     HTMLOptionElementFactory_GetIDsOfNames,
534     HTMLOptionElementFactory_Invoke,
535     HTMLOptionElementFactory_create
536 };
537
538 HTMLOptionElementFactory *HTMLOptionElementFactory_Create(HTMLWindow *window)
539 {
540     HTMLOptionElementFactory *ret;
541
542     ret = heap_alloc(sizeof(HTMLOptionElementFactory));
543
544     ret->lpHTMLOptionElementFactoryVtbl = &HTMLOptionElementFactoryVtbl;
545     ret->ref = 1;
546     ret->window = window;
547
548     return ret;
549 }