mshtml: Improve loading document detection in AsyncOpen.
[wine] / dlls / shdocvw / events.c
1 /*
2  * Implementation of event-related interfaces for WebBrowser control:
3  *
4  *  - IConnectionPointContainer
5  *  - IConnectionPoint
6  *
7  * Copyright 2001 John R. Sheets (for CodeWeavers)
8  * Copyright 2006 Jacek Caban for CodeWeavers
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 #include <string.h>
26 #include "wine/debug.h"
27 #include "shdocvw.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(shdocvw);
30
31 struct ConnectionPoint {
32     const IConnectionPointVtbl *lpConnectionPointVtbl;
33
34     IConnectionPointContainer *container;
35
36     IDispatch **sinks;
37     DWORD sinks_size;
38
39     IID iid;
40 };
41
42 #define CONPOINT(x)  ((IConnectionPoint*) &(x)->lpConnectionPointVtbl)
43
44 /**********************************************************************
45  * Implement the IConnectionPointContainer interface
46  */
47
48 #define CONPTCONT_THIS(iface) DEFINE_THIS(ConnectionPointContainer, ConnectionPointContainer, iface)
49
50 static HRESULT WINAPI ConnectionPointContainer_QueryInterface(IConnectionPointContainer *iface,
51         REFIID riid, LPVOID *ppv)
52 {
53     ConnectionPointContainer *This = CONPTCONT_THIS(iface);
54     return IUnknown_QueryInterface(This->impl, riid, ppv);
55 }
56
57 static ULONG WINAPI ConnectionPointContainer_AddRef(IConnectionPointContainer *iface)
58 {
59     ConnectionPointContainer *This = CONPTCONT_THIS(iface);
60     return IUnknown_AddRef(This->impl);
61 }
62
63 static ULONG WINAPI ConnectionPointContainer_Release(IConnectionPointContainer *iface)
64 {
65     ConnectionPointContainer *This = CONPTCONT_THIS(iface);
66     return IUnknown_Release(This->impl);
67 }
68
69 static HRESULT WINAPI ConnectionPointContainer_EnumConnectionPoints(IConnectionPointContainer *iface,
70         LPENUMCONNECTIONPOINTS *ppEnum)
71 {
72     ConnectionPointContainer *This = CONPTCONT_THIS(iface);
73     FIXME("(%p)->(%p)\n", This, ppEnum);
74     return E_NOTIMPL;
75 }
76
77 static HRESULT WINAPI ConnectionPointContainer_FindConnectionPoint(IConnectionPointContainer *iface,
78         REFIID riid, LPCONNECTIONPOINT *ppCP)
79 {
80     ConnectionPointContainer *This = CONPTCONT_THIS(iface);
81
82     if(!ppCP) {
83         WARN("ppCP == NULL\n");
84         return E_POINTER;
85     }
86
87     *ppCP = NULL;
88
89     if(IsEqualGUID(&DIID_DWebBrowserEvents2, riid)) {
90         TRACE("(%p)->(DIID_DWebBrowserEvents2 %p)\n", This, ppCP);
91         *ppCP = CONPOINT(This->wbe2);
92     }else if(IsEqualGUID(&DIID_DWebBrowserEvents, riid)) {
93         TRACE("(%p)->(DIID_DWebBrowserEvents %p)\n", This, ppCP);
94         *ppCP = CONPOINT(This->wbe);
95     }else if(IsEqualGUID(&IID_IPropertyNotifySink, riid)) {
96         TRACE("(%p)->(IID_IPropertyNotifySink %p)\n", This, ppCP);
97         *ppCP = CONPOINT(This->pns);
98     }
99
100     if(*ppCP) {
101         IConnectionPoint_AddRef(*ppCP);
102         return S_OK;
103     }
104
105     WARN("Unsupported IID %s\n", debugstr_guid(riid));
106     return CONNECT_E_NOCONNECTION;
107 }
108
109 #undef CONPTCONT_THIS
110
111 static const IConnectionPointContainerVtbl ConnectionPointContainerVtbl =
112 {
113     ConnectionPointContainer_QueryInterface,
114     ConnectionPointContainer_AddRef,
115     ConnectionPointContainer_Release,
116     ConnectionPointContainer_EnumConnectionPoints,
117     ConnectionPointContainer_FindConnectionPoint
118 };
119
120
121 /**********************************************************************
122  * Implement the IConnectionPoint interface
123  */
124
125 #define CONPOINT_THIS(iface) DEFINE_THIS(ConnectionPoint, ConnectionPoint, iface)
126
127 static HRESULT WINAPI ConnectionPoint_QueryInterface(IConnectionPoint *iface,
128                                                      REFIID riid, LPVOID *ppv)
129 {
130     ConnectionPoint *This = CONPOINT_THIS(iface);
131
132     *ppv = NULL;
133
134     if(IsEqualGUID(&IID_IUnknown, riid)) {
135         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
136         *ppv = CONPOINT(This);
137     }else if(IsEqualGUID(&IID_IConnectionPoint, riid)) {
138         TRACE("(%p)->(IID_IConnectionPoint %p)\n", This, ppv);
139         *ppv = CONPOINT(This);
140     }
141
142     if(*ppv) {
143         IConnectionPointContainer_AddRef(This->container);
144         return S_OK;
145     }
146
147     WARN("Unsupported interface %s\n", debugstr_guid(riid));
148     return E_NOINTERFACE;
149 }
150
151 static ULONG WINAPI ConnectionPoint_AddRef(IConnectionPoint *iface)
152 {
153     ConnectionPoint *This = CONPOINT_THIS(iface);
154     return IConnectionPointContainer_AddRef(This->container);
155 }
156
157 static ULONG WINAPI ConnectionPoint_Release(IConnectionPoint *iface)
158 {
159     ConnectionPoint *This = CONPOINT_THIS(iface);
160     return IConnectionPointContainer_Release(This->container);
161 }
162
163 static HRESULT WINAPI ConnectionPoint_GetConnectionInterface(IConnectionPoint *iface, IID *pIID)
164 {
165     ConnectionPoint *This = CONPOINT_THIS(iface);
166
167     TRACE("(%p)->(%p)\n", This, pIID);
168
169     memcpy(pIID, &This->iid, sizeof(IID));
170     return S_OK;
171 }
172
173 static HRESULT WINAPI ConnectionPoint_GetConnectionPointContainer(IConnectionPoint *iface,
174         IConnectionPointContainer **ppCPC)
175 {
176     ConnectionPoint *This = CONPOINT_THIS(iface);
177
178     TRACE("(%p)->(%p)\n", This, ppCPC);
179
180     *ppCPC = This->container;
181     IConnectionPointContainer_AddRef(This->container);
182     return S_OK;
183 }
184
185 static HRESULT WINAPI ConnectionPoint_Advise(IConnectionPoint *iface, IUnknown *pUnkSink,
186                                              DWORD *pdwCookie)
187 {
188     ConnectionPoint *This = CONPOINT_THIS(iface);
189     IDispatch *disp;
190     DWORD i;
191     HRESULT hres;
192
193     TRACE("(%p)->(%p %p)\n", This, pUnkSink, pdwCookie);
194
195     hres = IUnknown_QueryInterface(pUnkSink, &This->iid, (void**)&disp);
196     if(FAILED(hres)) {
197         hres = IUnknown_QueryInterface(pUnkSink, &IID_IDispatch, (void**)&disp);
198         if(FAILED(hres))
199             return CONNECT_E_CANNOTCONNECT;
200     }
201
202     if(This->sinks) {
203         for(i=0; i<This->sinks_size; i++) {
204             if(!This->sinks[i])
205                 break;
206         }
207
208         if(i == This->sinks_size)
209             This->sinks = shdocvw_realloc(This->sinks,
210                                           (++This->sinks_size)*sizeof(*This->sinks));
211     }else {
212         This->sinks = shdocvw_alloc(sizeof(*This->sinks));
213         This->sinks_size = 1;
214         i = 0;
215     }
216
217     This->sinks[i] = disp;
218     *pdwCookie = i+1;
219
220     return S_OK;
221 }
222
223 static HRESULT WINAPI ConnectionPoint_Unadvise(IConnectionPoint *iface, DWORD dwCookie)
224 {
225     ConnectionPoint *This = CONPOINT_THIS(iface);
226
227     TRACE("(%p)->(%d)\n", This, dwCookie);
228
229     if(!dwCookie || dwCookie > This->sinks_size || !This->sinks[dwCookie-1])
230         return CONNECT_E_NOCONNECTION;
231
232     IDispatch_Release(This->sinks[dwCookie-1]);
233     This->sinks[dwCookie-1] = NULL;
234
235     return S_OK;
236 }
237
238 static HRESULT WINAPI ConnectionPoint_EnumConnections(IConnectionPoint *iface,
239                                                       IEnumConnections **ppEnum)
240 {
241     ConnectionPoint *This = CONPOINT_THIS(iface);
242     FIXME("(%p)->(%p)\n", This, ppEnum);
243     return E_NOTIMPL;
244 }
245
246 #undef CONPOINT_THIS
247
248 static const IConnectionPointVtbl ConnectionPointVtbl =
249 {
250     ConnectionPoint_QueryInterface,
251     ConnectionPoint_AddRef,
252     ConnectionPoint_Release,
253     ConnectionPoint_GetConnectionInterface,
254     ConnectionPoint_GetConnectionPointContainer,
255     ConnectionPoint_Advise,
256     ConnectionPoint_Unadvise,
257     ConnectionPoint_EnumConnections
258 };
259
260 void call_sink(ConnectionPoint *This, DISPID dispid, DISPPARAMS *dispparams)
261 {
262     DWORD i;
263
264     for(i=0; i<This->sinks_size; i++) {
265         if(This->sinks[i])
266             IDispatch_Invoke(This->sinks[i], dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT,
267                              DISPATCH_METHOD, dispparams, NULL, NULL, NULL);
268     }
269 }
270
271 static void ConnectionPoint_Create(REFIID riid, ConnectionPoint **cp,
272                                    IConnectionPointContainer *container)
273 {
274     ConnectionPoint *ret = shdocvw_alloc(sizeof(ConnectionPoint));
275
276     ret->lpConnectionPointVtbl = &ConnectionPointVtbl;
277
278     ret->sinks = NULL;
279     ret->sinks_size = 0;
280     ret->container = container;
281
282     memcpy(&ret->iid, riid, sizeof(IID));
283
284     *cp = ret;
285 }
286
287 static void ConnectionPoint_Destroy(ConnectionPoint *This)
288 {
289     int i;
290
291     for(i=0; i<This->sinks_size; i++) {
292         if(This->sinks[i])
293             IDispatch_Release(This->sinks[i]);
294     }
295
296     shdocvw_free(This->sinks);
297     shdocvw_free(This);
298 }
299
300 void ConnectionPointContainer_Init(ConnectionPointContainer *This, IUnknown *impl)
301 {
302     This->lpConnectionPointContainerVtbl = &ConnectionPointContainerVtbl;
303
304     ConnectionPoint_Create(&DIID_DWebBrowserEvents2, &This->wbe2, CONPTCONT(This));
305     ConnectionPoint_Create(&DIID_DWebBrowserEvents,  &This->wbe,  CONPTCONT(This));
306     ConnectionPoint_Create(&IID_IPropertyNotifySink, &This->pns,  CONPTCONT(This));
307
308     This->impl = impl;
309 }
310
311 void ConnectionPointContainer_Destroy(ConnectionPointContainer *This)
312 {
313     ConnectionPoint_Destroy(This->wbe2);
314     ConnectionPoint_Destroy(This->wbe);
315     ConnectionPoint_Destroy(This->pns);
316 }