mshtml: Store reference to document node in window object.
[wine] / dlls / mshtml / htmliframe.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 <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 "mshtml_private.h"
29
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
33
34 typedef struct {
35     HTMLElement element;
36     const IHTMLFrameBase2Vtbl  *lpIHTMLFrameBase2Vtbl;
37
38     LONG ref;
39
40     nsIDOMHTMLIFrameElement *nsiframe;
41     HTMLWindow *content_window;
42 } HTMLIFrame;
43
44 #define HTMLFRAMEBASE2(x)  (&(x)->lpIHTMLFrameBase2Vtbl)
45
46 #define HTMLFRAMEBASE2_THIS(iface) DEFINE_THIS(HTMLIFrame, IHTMLFrameBase2, iface)
47
48 static HRESULT WINAPI HTMLIFrameBase2_QueryInterface(IHTMLFrameBase2 *iface, REFIID riid, void **ppv)
49 {
50     HTMLIFrame *This = HTMLFRAMEBASE2_THIS(iface);
51
52     return IHTMLDOMNode_QueryInterface(HTMLDOMNODE(&This->element.node), riid, ppv);
53 }
54
55 static ULONG WINAPI HTMLIFrameBase2_AddRef(IHTMLFrameBase2 *iface)
56 {
57     HTMLIFrame *This = HTMLFRAMEBASE2_THIS(iface);
58
59     return IHTMLDOMNode_AddRef(HTMLDOMNODE(&This->element.node));
60 }
61
62 static ULONG WINAPI HTMLIFrameBase2_Release(IHTMLFrameBase2 *iface)
63 {
64     HTMLIFrame *This = HTMLFRAMEBASE2_THIS(iface);
65
66     return IHTMLDOMNode_Release(HTMLDOMNODE(&This->element.node));
67 }
68
69 static HRESULT WINAPI HTMLIFrameBase2_GetTypeInfoCount(IHTMLFrameBase2 *iface, UINT *pctinfo)
70 {
71     HTMLIFrame *This = HTMLFRAMEBASE2_THIS(iface);
72     FIXME("(%p)\n", This);
73     return E_NOTIMPL;
74 }
75
76 static HRESULT WINAPI HTMLIFrameBase2_GetTypeInfo(IHTMLFrameBase2 *iface, UINT iTInfo,
77         LCID lcid, ITypeInfo **ppTInfo)
78 {
79     HTMLIFrame *This = HTMLFRAMEBASE2_THIS(iface);
80     FIXME("(%p)\n", This);
81     return E_NOTIMPL;
82 }
83
84 static HRESULT WINAPI HTMLIFrameBase2_GetIDsOfNames(IHTMLFrameBase2 *iface, REFIID riid,
85         LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
86 {
87     HTMLIFrame *This = HTMLFRAMEBASE2_THIS(iface);
88     FIXME("(%p)\n", This);
89     return E_NOTIMPL;
90 }
91
92 static HRESULT WINAPI HTMLIFrameBase2_Invoke(IHTMLFrameBase2 *iface, DISPID dispIdMember,
93         REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
94         VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
95 {
96     HTMLIFrame *This = HTMLFRAMEBASE2_THIS(iface);
97     FIXME("(%p)\n", This);
98     return E_NOTIMPL;
99 }
100
101 static HRESULT WINAPI HTMLIFrameBase2_get_contentWindow(IHTMLFrameBase2 *iface, IHTMLWindow2 **p)
102 {
103     HTMLIFrame *This = HTMLFRAMEBASE2_THIS(iface);
104
105     TRACE("(%p)->(%p)\n", This, p);
106
107     if(!This->content_window) {
108         nsIDOMHTMLDocument *nshtmldoc;
109         HTMLDocumentNode *content_doc;
110         nsIDOMDocument *nsdoc;
111         HTMLWindow *window;
112         nsresult nsres;
113         HRESULT hres;
114
115         nsres = nsIDOMHTMLIFrameElement_GetContentDocument(This->nsiframe, &nsdoc);
116         if(NS_FAILED(nsres)) {
117             ERR("GetContentDocument failed: %08x\n", nsres);
118             return E_FAIL;
119         }
120
121         if(!nsdoc) {
122             FIXME("NULL contentDocument\n");
123             return E_FAIL;
124         }
125
126         nsres = nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMHTMLDocument, (void**)&nshtmldoc);
127         nsIDOMDocument_Release(nsdoc);
128         if(NS_FAILED(nsres)) {
129             ERR("Could not get nsIDOMHTMLDocument iface: %08x\n", nsres);
130             return E_FAIL;
131         }
132
133         hres = HTMLWindow_Create(This->element.node.doc->doc_obj, NULL, &window);
134         if(FAILED(hres)) {
135             nsIDOMDocument_Release(nsdoc);
136             return hres;
137         }
138
139         hres = create_doc_from_nsdoc(nshtmldoc, This->element.node.doc->doc_obj, window, &content_doc);
140         nsIDOMHTMLDocument_Release(nshtmldoc);
141         if(SUCCEEDED(hres))
142             window_set_docnode(window, content_doc);
143         else
144             IHTMLWindow2_Release(HTMLWINDOW2(window));
145         htmldoc_release(&content_doc->basedoc);
146         if(FAILED(hres))
147             return hres;
148
149         This->content_window = window;
150     }
151
152     IHTMLWindow2_AddRef(HTMLWINDOW2(This->content_window));
153     *p = HTMLWINDOW2(This->content_window);
154     return S_OK;
155 }
156
157 static HRESULT WINAPI HTMLIFrameBase2_put_onload(IHTMLFrameBase2 *iface, VARIANT v)
158 {
159     HTMLIFrame *This = HTMLFRAMEBASE2_THIS(iface);
160     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
161     return E_NOTIMPL;
162 }
163
164 static HRESULT WINAPI HTMLIFrameBase2_get_onload(IHTMLFrameBase2 *iface, VARIANT *p)
165 {
166     HTMLIFrame *This = HTMLFRAMEBASE2_THIS(iface);
167     FIXME("(%p)->(%p)\n", This, p);
168     return E_NOTIMPL;
169 }
170
171 static HRESULT WINAPI HTMLIFrameBase2_put_onreadystatechange(IHTMLFrameBase2 *iface, VARIANT v)
172 {
173     HTMLIFrame *This = HTMLFRAMEBASE2_THIS(iface);
174     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
175     return E_NOTIMPL;
176 }
177
178 static HRESULT WINAPI HTMLIFrameBase2_get_onreadystatechange(IHTMLFrameBase2 *iface, VARIANT *p)
179 {
180     HTMLIFrame *This = HTMLFRAMEBASE2_THIS(iface);
181     FIXME("(%p)->(%p)\n", This, p);
182     return E_NOTIMPL;
183 }
184
185 static HRESULT WINAPI HTMLIFrameBase2_get_readyState(IHTMLFrameBase2 *iface, BSTR *p)
186 {
187     HTMLIFrame *This = HTMLFRAMEBASE2_THIS(iface);
188     FIXME("(%p)->(%p)\n", This, p);
189     return E_NOTIMPL;
190 }
191
192 static HRESULT WINAPI HTMLIFrameBase2_put_allowTransparency(IHTMLFrameBase2 *iface, VARIANT_BOOL v)
193 {
194     HTMLIFrame *This = HTMLFRAMEBASE2_THIS(iface);
195     FIXME("(%p)->(%x)\n", This, v);
196     return E_NOTIMPL;
197 }
198
199 static HRESULT WINAPI HTMLIFrameBase2_get_allowTransparency(IHTMLFrameBase2 *iface, VARIANT_BOOL *p)
200 {
201     HTMLIFrame *This = HTMLFRAMEBASE2_THIS(iface);
202     FIXME("(%p)->(%p)\n", This, p);
203     return E_NOTIMPL;
204 }
205
206 #undef HTMLFRAMEBASE2_THIS
207
208 static const IHTMLFrameBase2Vtbl HTMLIFrameBase2Vtbl = {
209     HTMLIFrameBase2_QueryInterface,
210     HTMLIFrameBase2_AddRef,
211     HTMLIFrameBase2_Release,
212     HTMLIFrameBase2_GetTypeInfoCount,
213     HTMLIFrameBase2_GetTypeInfo,
214     HTMLIFrameBase2_GetIDsOfNames,
215     HTMLIFrameBase2_Invoke,
216     HTMLIFrameBase2_get_contentWindow,
217     HTMLIFrameBase2_put_onload,
218     HTMLIFrameBase2_get_onload,
219     HTMLIFrameBase2_put_onreadystatechange,
220     HTMLIFrameBase2_get_onreadystatechange,
221     HTMLIFrameBase2_get_readyState,
222     HTMLIFrameBase2_put_allowTransparency,
223     HTMLIFrameBase2_get_allowTransparency
224 };
225
226 #define HTMLIFRAME_NODE_THIS(iface) DEFINE_THIS2(HTMLIFrame, element.node, iface)
227
228 static HRESULT HTMLIFrame_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
229 {
230     HTMLIFrame *This = HTMLIFRAME_NODE_THIS(iface);
231
232     *ppv = NULL;
233
234     if(IsEqualGUID(&IID_IHTMLFrameBase2, riid)) {
235         TRACE("(%p)->(IID_IHTMLFrameBase2 %p)\n", This, ppv);
236         *ppv = HTMLFRAMEBASE2(This);
237     }else {
238         return HTMLElement_QI(&This->element.node, riid, ppv);
239     }
240
241     IUnknown_AddRef((IUnknown*)*ppv);
242     return S_OK;
243 }
244
245 static void HTMLIFrame_destructor(HTMLDOMNode *iface)
246 {
247     HTMLIFrame *This = HTMLIFRAME_NODE_THIS(iface);
248
249     if(This->content_window)
250         IHTMLWindow2_Release(HTMLWINDOW2(This->content_window));
251     if(This->nsiframe)
252         nsIDOMHTMLIFrameElement_Release(This->nsiframe);
253
254     HTMLElement_destructor(&This->element.node);
255 }
256
257 #undef HTMLIFRAME_NODE_THIS
258
259 static const NodeImplVtbl HTMLIFrameImplVtbl = {
260     HTMLIFrame_QI,
261     HTMLIFrame_destructor
262 };
263
264 static const tid_t HTMLIFrame_iface_tids[] = {
265     IHTMLDOMNode_tid,
266     IHTMLDOMNode2_tid,
267     IHTMLElement_tid,
268     IHTMLElement2_tid,
269     IHTMLElement3_tid,
270     IHTMLFrameBase2_tid,
271     0
272 };
273
274 static dispex_static_data_t HTMLIFrame_dispex = {
275     NULL,
276     DispHTMLIFrame_tid,
277     NULL,
278     HTMLIFrame_iface_tids
279 };
280
281 HTMLElement *HTMLIFrame_Create(nsIDOMHTMLElement *nselem)
282 {
283     HTMLIFrame *ret;
284     nsresult nsres;
285
286     ret = heap_alloc_zero(sizeof(HTMLIFrame));
287
288     ret->lpIHTMLFrameBase2Vtbl = &HTMLIFrameBase2Vtbl;
289     ret->element.node.vtbl = &HTMLIFrameImplVtbl;
290
291     init_dispex(&ret->element.node.dispex, (IUnknown*)HTMLFRAMEBASE2(ret), &HTMLIFrame_dispex);
292     HTMLElement_Init(&ret->element);
293
294     nsres = nsIDOMHTMLElement_QueryInterface(nselem, &IID_nsIDOMHTMLIFrameElement, (void**)&ret->nsiframe);
295     if(NS_FAILED(nsres))
296         ERR("Could not get nsIDOMHTMLIFrameElement iface: %08x\n", nsres);
297
298     return &ret->element;
299 }