mshtml: Don't crash if nsWebBrowser could not be created.
[wine] / dlls / mshtml / htmlscript.c
1 /*
2  * Copyright 2008 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 "config.h"
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winnls.h"
30 #include "ole2.h"
31
32 #include "wine/debug.h"
33
34 #include "mshtml_private.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37
38 typedef struct {
39     HTMLElement element;
40
41     const IHTMLScriptElementVtbl *lpHTMLScriptElementVtbl;
42
43     nsIDOMHTMLScriptElement *nsscript;
44 } HTMLScriptElement;
45
46 #define HTMLSCRIPT(x)  ((IHTMLScriptElement*)  &(x)->lpHTMLScriptElementVtbl)
47
48 #define HTMLSCRIPT_THIS(iface) DEFINE_THIS(HTMLScriptElement, HTMLScriptElement, iface)
49
50 static HRESULT WINAPI HTMLScriptElement_QueryInterface(IHTMLScriptElement *iface,
51         REFIID riid, void **ppv)
52 {
53     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
54
55     return IHTMLDOMNode_QueryInterface(HTMLDOMNODE(&This->element.node), riid, ppv);
56 }
57
58 static ULONG WINAPI HTMLScriptElement_AddRef(IHTMLScriptElement *iface)
59 {
60     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
61
62     return IHTMLDOMNode_AddRef(HTMLDOMNODE(&This->element.node));
63 }
64
65 static ULONG WINAPI HTMLScriptElement_Release(IHTMLScriptElement *iface)
66 {
67     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
68
69     return IHTMLDOMNode_Release(HTMLDOMNODE(&This->element.node));
70 }
71
72 static HRESULT WINAPI HTMLScriptElement_GetTypeInfoCount(IHTMLScriptElement *iface, UINT *pctinfo)
73 {
74     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
75     FIXME("(%p)->(%p)\n", This, pctinfo);
76     return E_NOTIMPL;
77 }
78
79 static HRESULT WINAPI HTMLScriptElement_GetTypeInfo(IHTMLScriptElement *iface, UINT iTInfo,
80                                               LCID lcid, ITypeInfo **ppTInfo)
81 {
82     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
83     FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
84     return E_NOTIMPL;
85 }
86
87 static HRESULT WINAPI HTMLScriptElement_GetIDsOfNames(IHTMLScriptElement *iface, REFIID riid,
88                                                 LPOLESTR *rgszNames, UINT cNames,
89                                                 LCID lcid, DISPID *rgDispId)
90 {
91     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
92     FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
93                                         lcid, rgDispId);
94     return E_NOTIMPL;
95 }
96
97 static HRESULT WINAPI HTMLScriptElement_Invoke(IHTMLScriptElement *iface, DISPID dispIdMember,
98                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
99                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
100 {
101     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
102     FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
103             lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
104     return E_NOTIMPL;
105 }
106
107 static HRESULT WINAPI HTMLScriptElement_put_src(IHTMLScriptElement *iface, BSTR v)
108 {
109     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
110     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
111     return E_NOTIMPL;
112 }
113
114 static HRESULT WINAPI HTMLScriptElement_get_src(IHTMLScriptElement *iface, BSTR *p)
115 {
116     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
117     FIXME("(%p)->(%p)\n", This, p);
118     return E_NOTIMPL;
119 }
120
121 static HRESULT WINAPI HTMLScriptElement_put_htmlFor(IHTMLScriptElement *iface, BSTR v)
122 {
123     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
124     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
125     return E_NOTIMPL;
126 }
127
128 static HRESULT WINAPI HTMLScriptElement_get_htmlFor(IHTMLScriptElement *iface, BSTR *p)
129 {
130     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
131     FIXME("(%p)->(%p)\n", This, p);
132     return E_NOTIMPL;
133 }
134
135 static HRESULT WINAPI HTMLScriptElement_put_event(IHTMLScriptElement *iface, BSTR v)
136 {
137     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
138     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
139     return E_NOTIMPL;
140 }
141
142 static HRESULT WINAPI HTMLScriptElement_get_event(IHTMLScriptElement *iface, BSTR *p)
143 {
144     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
145     FIXME("(%p)->(%p)\n", This, p);
146     return E_NOTIMPL;
147 }
148
149 static HRESULT WINAPI HTMLScriptElement_put_text(IHTMLScriptElement *iface, BSTR v)
150 {
151     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
152     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
153     return E_NOTIMPL;
154 }
155
156 static HRESULT WINAPI HTMLScriptElement_get_text(IHTMLScriptElement *iface, BSTR *p)
157 {
158     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
159     FIXME("(%p)->(%p)\n", This, p);
160     return E_NOTIMPL;
161 }
162
163 static HRESULT WINAPI HTMLScriptElement_put_defer(IHTMLScriptElement *iface, VARIANT_BOOL v)
164 {
165     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
166     FIXME("(%p)->(%x)\n", This, v);
167     return E_NOTIMPL;
168 }
169
170 static HRESULT WINAPI HTMLScriptElement_get_defer(IHTMLScriptElement *iface, VARIANT_BOOL *p)
171 {
172     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
173     FIXME("(%p)->(%p)\n", This, p);
174     return E_NOTIMPL;
175 }
176
177 static HRESULT WINAPI HTMLScriptElement_get_readyState(IHTMLScriptElement *iface, BSTR *p)
178 {
179     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
180     FIXME("(%p)->(%p)\n", This, p);
181     return E_NOTIMPL;
182 }
183
184 static HRESULT WINAPI HTMLScriptElement_put_onerror(IHTMLScriptElement *iface, VARIANT v)
185 {
186     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
187     FIXME("(%p)->(v(%d))\n", This, V_VT(&v));
188     return E_NOTIMPL;
189 }
190
191 static HRESULT WINAPI HTMLScriptElement_get_onerror(IHTMLScriptElement *iface, VARIANT *p)
192 {
193     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
194     FIXME("(%p)->(%p)\n", This, p);
195     return E_NOTIMPL;
196 }
197
198 static HRESULT WINAPI HTMLScriptElement_put_type(IHTMLScriptElement *iface, BSTR v)
199 {
200     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
201     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
202     return E_NOTIMPL;
203 }
204
205 static HRESULT WINAPI HTMLScriptElement_get_type(IHTMLScriptElement *iface, BSTR *p)
206 {
207     HTMLScriptElement *This = HTMLSCRIPT_THIS(iface);
208     const PRUnichar *nstype;
209     nsAString nstype_str;
210     nsresult nsres;
211
212     TRACE("(%p)->(%p)\n", This, p);
213
214     nsAString_Init(&nstype_str, NULL);
215     nsres = nsIDOMHTMLScriptElement_GetType(This->nsscript, &nstype_str);
216     if(NS_FAILED(nsres))
217         ERR("GetType failed: %08x\n", nsres);
218
219     nsAString_GetData(&nstype_str, &nstype);
220     *p = *nstype ? SysAllocString(nstype) : NULL;
221     nsAString_Finish(&nstype_str);
222
223     return S_OK;
224 }
225
226 static const IHTMLScriptElementVtbl HTMLScriptElementVtbl = {
227     HTMLScriptElement_QueryInterface,
228     HTMLScriptElement_AddRef,
229     HTMLScriptElement_Release,
230     HTMLScriptElement_GetTypeInfoCount,
231     HTMLScriptElement_GetTypeInfo,
232     HTMLScriptElement_GetIDsOfNames,
233     HTMLScriptElement_Invoke,
234     HTMLScriptElement_put_src,
235     HTMLScriptElement_get_src,
236     HTMLScriptElement_put_htmlFor,
237     HTMLScriptElement_get_htmlFor,
238     HTMLScriptElement_put_event,
239     HTMLScriptElement_get_event,
240     HTMLScriptElement_put_text,
241     HTMLScriptElement_get_text,
242     HTMLScriptElement_put_defer,
243     HTMLScriptElement_get_defer,
244     HTMLScriptElement_get_readyState,
245     HTMLScriptElement_put_onerror,
246     HTMLScriptElement_get_onerror,
247     HTMLScriptElement_put_type,
248     HTMLScriptElement_get_type
249 };
250
251 #undef HTMLSCRIPT_THIS
252
253 #define HTMLSCRIPT_NODE_THIS(iface) DEFINE_THIS2(HTMLScriptElement, element.node, iface)
254
255 static HRESULT HTMLScriptElement_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
256 {
257     HTMLScriptElement *This = HTMLSCRIPT_NODE_THIS(iface);
258
259     *ppv = NULL;
260
261     if(IsEqualGUID(&IID_IUnknown, riid)) {
262         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
263         *ppv = HTMLSCRIPT(This);
264     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
265         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
266         *ppv = HTMLSCRIPT(This);
267     }else if(IsEqualGUID(&IID_IHTMLScriptElement, riid)) {
268         TRACE("(%p)->(IID_IHTMLScriptElement %p)\n", This, ppv);
269         *ppv = HTMLSCRIPT(This);
270     }
271
272     if(*ppv) {
273         IUnknown_AddRef((IUnknown*)*ppv);
274         return S_OK;
275     }
276
277     return HTMLElement_QI(&This->element.node, riid, ppv);
278 }
279
280 static void HTMLScriptElement_destructor(HTMLDOMNode *iface)
281 {
282     HTMLScriptElement *This = HTMLSCRIPT_NODE_THIS(iface);
283     HTMLElement_destructor(&This->element.node);
284 }
285
286 #undef HTMLSCRIPT_NODE_THIS
287
288 static const NodeImplVtbl HTMLScriptElementImplVtbl = {
289     HTMLScriptElement_QI,
290     HTMLScriptElement_destructor
291 };
292
293 HTMLElement *HTMLScriptElement_Create(nsIDOMHTMLElement *nselem)
294 {
295     HTMLScriptElement *ret = heap_alloc(sizeof(HTMLScriptElement));
296     nsresult nsres;
297
298     HTMLElement_Init(&ret->element);
299
300     ret->lpHTMLScriptElementVtbl = &HTMLScriptElementVtbl;
301     ret->element.node.vtbl = &HTMLScriptElementImplVtbl;
302
303     nsres = nsIDOMHTMLElement_QueryInterface(nselem, &IID_nsIDOMHTMLScriptElement, (void**)&ret->nsscript);
304     if(NS_FAILED(nsres))
305         ERR("Could not get nsIDOMHTMLScriptElement: %08x\n", nsres);
306
307     return &ret->element;
308 }