winemac: Activate an app if it sets focus on a window shortly after a hot key is...
[wine.git] / dlls / mshtml / pluginhost.c
blob38a8fd5a30d4a19d65d6c9eeb585d6d059ed2e8d
1 /*
2 * Copyright 2010 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 "config.h"
21 #include <stdarg.h>
22 #include <assert.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "ole2.h"
30 #include "shlobj.h"
31 #include "mshtmdid.h"
33 #include "mshtml_private.h"
34 #include "pluginhost.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
40 const IID IID_HTMLPluginContainer =
41 {0xbd7a6050,0xb373,0x4f6f,{0xa4,0x93,0xdd,0x40,0xc5,0x23,0xa8,0x6a}};
43 static BOOL check_load_safety(PluginHost *host)
45 DWORD policy_size, policy;
46 struct CONFIRMSAFETY cs;
47 BYTE *ppolicy;
48 HRESULT hres;
50 cs.clsid = host->clsid;
51 cs.pUnk = host->plugin_unk;
52 cs.dwFlags = CONFIRMSAFETYACTION_LOADOBJECT;
54 hres = IInternetHostSecurityManager_QueryCustomPolicy(&host->doc->IInternetHostSecurityManager_iface,
55 &GUID_CUSTOM_CONFIRMOBJECTSAFETY, &ppolicy, &policy_size, (BYTE*)&cs, sizeof(cs), 0);
56 if(FAILED(hres))
57 return FALSE;
59 policy = *(DWORD*)ppolicy;
60 CoTaskMemFree(ppolicy);
61 return policy == URLPOLICY_ALLOW;
64 static BOOL check_script_safety(PluginHost *host)
66 DISPPARAMS params = {NULL,NULL,0,0};
67 DWORD policy_size, policy;
68 struct CONFIRMSAFETY cs;
69 BYTE *ppolicy;
70 ULONG err = 0;
71 VARIANT v;
72 HRESULT hres;
74 cs.clsid = host->clsid;
75 cs.pUnk = host->plugin_unk;
76 cs.dwFlags = 0;
78 hres = IInternetHostSecurityManager_QueryCustomPolicy(&host->doc->IInternetHostSecurityManager_iface,
79 &GUID_CUSTOM_CONFIRMOBJECTSAFETY, &ppolicy, &policy_size, (BYTE*)&cs, sizeof(cs), 0);
80 if(FAILED(hres))
81 return FALSE;
83 policy = *(DWORD*)ppolicy;
84 CoTaskMemFree(ppolicy);
86 if(policy != URLPOLICY_ALLOW)
87 return FALSE;
89 V_VT(&v) = VT_EMPTY;
90 hres = IDispatch_Invoke(host->disp, DISPID_SECURITYCTX, &IID_NULL, 0, DISPATCH_PROPERTYGET, &params, &v, NULL, &err);
91 if(SUCCEEDED(hres)) {
92 FIXME("Handle security ctx %s\n", debugstr_variant(&v));
93 return FALSE;
96 return TRUE;
99 static void update_readystate(PluginHost *host)
101 DISPPARAMS params = {NULL,NULL,0,0};
102 IDispatchEx *dispex;
103 IDispatch *disp;
104 ULONG err = 0;
105 VARIANT v;
106 HRESULT hres;
108 hres = IUnknown_QueryInterface(host->plugin_unk, &IID_IDispatchEx, (void**)&dispex);
109 if(SUCCEEDED(hres)) {
110 FIXME("Use IDispatchEx\n");
111 IDispatchEx_Release(dispex);
114 hres = IUnknown_QueryInterface(host->plugin_unk, &IID_IDispatch, (void**)&disp);
115 if(FAILED(hres))
116 return;
118 hres = IDispatch_Invoke(disp, DISPID_READYSTATE, &IID_NULL, 0, DISPATCH_PROPERTYGET, &params, &v, NULL, &err);
119 IDispatch_Release(disp);
120 if(SUCCEEDED(hres)) {
121 /* FIXME: make plugin readystate affect document readystate */
122 TRACE("readystate = %s\n", debugstr_variant(&v));
123 VariantClear(&v);
127 /* FIXME: We shouldn't need this function and we should embed plugin directly in the main document */
128 static void get_pos_rect(PluginHost *host, RECT *ret)
130 ret->top = 0;
131 ret->left = 0;
132 ret->bottom = host->rect.bottom - host->rect.top;
133 ret->right = host->rect.right - host->rect.left;
136 static void load_prop_bag(PluginHost *host, IPersistPropertyBag *persist_prop_bag)
138 IPropertyBag *prop_bag;
139 HRESULT hres;
141 hres = create_param_prop_bag(host->element->element.nselem, &prop_bag);
142 if(FAILED(hres))
143 return;
145 if(prop_bag && !check_load_safety(host)) {
146 IPropertyBag_Release(prop_bag);
147 prop_bag = NULL;
150 if(prop_bag) {
151 hres = IPersistPropertyBag_Load(persist_prop_bag, prop_bag, NULL);
152 IPropertyBag_Release(prop_bag);
153 if(FAILED(hres))
154 WARN("Load failed: %08x\n", hres);
155 }else {
156 hres = IPersistPropertyBag_InitNew(persist_prop_bag);
157 if(FAILED(hres))
158 WARN("InitNew failed: %08x\n", hres);
162 static void load_plugin(PluginHost *host)
164 IPersistPropertyBag2 *persist_prop_bag2;
165 IPersistPropertyBag *persist_prop_bag;
166 HRESULT hres;
168 hres = IUnknown_QueryInterface(host->plugin_unk, &IID_IPersistPropertyBag2, (void**)&persist_prop_bag2);
169 if(SUCCEEDED(hres)) {
170 FIXME("Use IPersistPropertyBag2 iface\n");
171 IPersistPropertyBag2_Release(persist_prop_bag2);
172 return;
175 hres = IUnknown_QueryInterface(host->plugin_unk, &IID_IPersistPropertyBag, (void**)&persist_prop_bag);
176 if(SUCCEEDED(hres)) {
177 load_prop_bag(host, persist_prop_bag);
178 IPersistPropertyBag_Release(persist_prop_bag);
179 return;
182 FIXME("No IPersistPropertyBag iface\n");
185 static void activate_plugin(PluginHost *host)
187 IClientSecurity *client_security;
188 IQuickActivate *quick_activate;
189 IOleObject *ole_obj = NULL;
190 IOleCommandTarget *cmdtrg;
191 IViewObjectEx *view_obj;
192 IDispatchEx *dispex;
193 IDispatch *disp;
194 RECT rect;
195 HRESULT hres;
197 if(!host->plugin_unk)
198 return;
200 /* Note native calls QI on plugin for an undocumented IID and CLSID_HTMLDocument */
202 /* FIXME: call FreezeEvents(TRUE) */
204 hres = IUnknown_QueryInterface(host->plugin_unk, &IID_IClientSecurity, (void**)&client_security);
205 if(SUCCEEDED(hres)) {
206 FIXME("Handle IClientSecurity\n");
207 IClientSecurity_Release(client_security);
208 return;
211 hres = IUnknown_QueryInterface(host->plugin_unk, &IID_IQuickActivate, (void**)&quick_activate);
212 if(SUCCEEDED(hres)) {
213 QACONTAINER container = {sizeof(container)};
214 QACONTROL control = {sizeof(control)};
216 TRACE("Using IQuickActivate\n");
218 container.pClientSite = &host->IOleClientSite_iface;
219 container.dwAmbientFlags = QACONTAINER_SUPPORTSMNEMONICS|QACONTAINER_MESSAGEREFLECT|QACONTAINER_USERMODE;
220 container.pAdviseSink = &host->IAdviseSinkEx_iface;
221 container.pPropertyNotifySink = &host->IPropertyNotifySink_iface;
223 hres = IQuickActivate_QuickActivate(quick_activate, &container, &control);
224 IQuickActivate_Release(quick_activate);
225 if(FAILED(hres))
226 FIXME("QuickActivate failed: %08x\n", hres);
227 }else {
228 DWORD status = 0;
230 hres = IUnknown_QueryInterface(host->plugin_unk, &IID_IOleObject, (void**)&ole_obj);
231 if(SUCCEEDED(hres)) {
232 hres = IOleObject_GetMiscStatus(ole_obj, DVASPECT_CONTENT, &status);
233 TRACE("GetMiscStatus returned %08x %x\n", hres, status);
235 hres = IOleObject_SetClientSite(ole_obj, &host->IOleClientSite_iface);
236 IOleObject_Release(ole_obj);
237 if(FAILED(hres)) {
238 FIXME("SetClientSite failed: %08x\n", hres);
239 return;
241 }else {
242 TRACE("Plugin does not support IOleObject\n");
246 load_plugin(host);
248 if(ole_obj) {
249 hres = IUnknown_QueryInterface(host->plugin_unk, &IID_IViewObjectEx, (void**)&view_obj);
250 if(SUCCEEDED(hres)) {
251 DWORD view_status = 0;
253 hres = IViewObjectEx_SetAdvise(view_obj, DVASPECT_CONTENT, 0, (IAdviseSink*)&host->IAdviseSinkEx_iface);
254 if(FAILED(hres))
255 WARN("SetAdvise failed: %08x\n", hres);
257 hres = IViewObjectEx_GetViewStatus(view_obj, &view_status);
258 IViewObjectEx_Release(view_obj);
259 TRACE("GetViewStatus returned %08x %x\n", hres, view_status);
263 update_readystate(host);
265 /* NOTE: Native QIs for IActiveScript, an undocumented IID, IOleControl and IRunnableObject */
267 hres = IUnknown_QueryInterface(host->plugin_unk, &IID_IDispatchEx, (void**)&dispex);
268 if(SUCCEEDED(hres)) {
269 FIXME("Use IDispatchEx\n");
270 host->disp = (IDispatch*)dispex;
271 }else {
272 hres = IUnknown_QueryInterface(host->plugin_unk, &IID_IDispatch, (void**)&disp);
273 if(SUCCEEDED(hres))
274 host->disp = disp;
275 else
276 TRACE("no IDispatch iface\n");
279 hres = IUnknown_QueryInterface(host->plugin_unk, &IID_IOleCommandTarget, (void**)&cmdtrg);
280 if(SUCCEEDED(hres)) {
281 FIXME("Use IOleCommandTarget\n");
282 IOleCommandTarget_Release(cmdtrg);
285 hres = IUnknown_QueryInterface(host->plugin_unk, &IID_IOleObject, (void**)&ole_obj);
286 if(FAILED(hres)) {
287 FIXME("Plugin does not support IOleObject\n");
288 return;
291 if(ole_obj) {
292 get_pos_rect(host, &rect);
293 hres = IOleObject_DoVerb(ole_obj, OLEIVERB_INPLACEACTIVATE, NULL, &host->IOleClientSite_iface, 0, host->hwnd, &rect);
294 IOleObject_Release(ole_obj);
295 if(FAILED(hres))
296 WARN("DoVerb failed: %08x\n", hres);
299 if(host->ip_object) {
300 HWND hwnd;
302 hres = IOleInPlaceObject_GetWindow(host->ip_object, &hwnd);
303 if(SUCCEEDED(hres))
304 TRACE("hwnd %p\n", hwnd);
308 void update_plugin_window(PluginHost *host, HWND hwnd, const RECT *rect)
310 BOOL rect_changed = FALSE;
312 if(!hwnd || (host->hwnd && host->hwnd != hwnd)) {
313 FIXME("unhandled hwnd\n");
314 return;
317 TRACE("%p %s\n", hwnd, wine_dbgstr_rect(rect));
319 if(memcmp(rect, &host->rect, sizeof(RECT))) {
320 host->rect = *rect;
321 rect_changed = TRUE;
324 if(!host->hwnd) {
325 host->hwnd = hwnd;
326 activate_plugin(host);
329 if(rect_changed && host->ip_object)
330 IOleInPlaceObject_SetObjectRects(host->ip_object, &host->rect, &host->rect);
333 static void notif_enabled(PluginHost *plugin_host)
335 DISPPARAMS args = {NULL, NULL, 0, 0};
336 IDispatch *disp;
337 ULONG err = 0;
338 VARIANT res;
339 HRESULT hres;
341 hres = IUnknown_QueryInterface(plugin_host->plugin_unk, &IID_IDispatch, (void**)&disp);
342 if(FAILED(hres)) {
343 FIXME("Could not get IDispatch iface: %08x\n", hres);
344 return;
347 V_VT(&res) = VT_EMPTY;
348 hres = IDispatch_Invoke(disp, DISPID_ENABLED, &IID_NULL, 0/*FIXME*/, DISPATCH_PROPERTYGET, &args, &res, NULL, &err);
349 IDispatch_Release(disp);
350 if(SUCCEEDED(hres)) {
351 FIXME("Got enabled %s\n", debugstr_variant(&res));
352 VariantClear(&res);
356 void notif_container_change(HTMLPluginContainer *plugin_container, DISPID dispid)
358 IOleControl *ole_control;
359 HRESULT hres;
361 if(!plugin_container->plugin_host || !plugin_container->plugin_host->plugin_unk)
362 return;
364 notif_enabled(plugin_container->plugin_host);
366 hres = IUnknown_QueryInterface(plugin_container->plugin_host->plugin_unk, &IID_IOleControl, (void**)&ole_control);
367 if(SUCCEEDED(hres)) {
368 IOleControl_OnAmbientPropertyChange(ole_control, dispid);
369 IOleControl_Release(ole_control);
373 HRESULT get_plugin_disp(HTMLPluginContainer *plugin_container, IDispatch **ret)
375 PluginHost *host;
377 host = plugin_container->plugin_host;
378 if(!host) {
379 ERR("No plugin host\n");
380 return E_UNEXPECTED;
383 if(!host->disp) {
384 *ret = NULL;
385 return S_OK;
388 if(!check_script_safety(host)) {
389 FIXME("Insecure object\n");
390 return E_FAIL;
393 IDispatch_AddRef(host->disp);
394 *ret = host->disp;
395 return S_OK;
398 HRESULT get_plugin_dispid(HTMLPluginContainer *plugin_container, WCHAR *name, DISPID *ret)
400 IDispatch *disp;
401 DISPID id;
402 DWORD i;
403 HRESULT hres;
405 if(!plugin_container->plugin_host) {
406 WARN("no plugin host\n");
407 return DISP_E_UNKNOWNNAME;
410 disp = plugin_container->plugin_host->disp;
411 if(!disp)
412 return DISP_E_UNKNOWNNAME;
414 hres = IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, &id);
415 if(FAILED(hres)) {
416 TRACE("no prop %s\n", debugstr_w(name));
417 return DISP_E_UNKNOWNNAME;
420 for(i=0; i < plugin_container->props_len; i++) {
421 if(id == plugin_container->props[i]) {
422 *ret = MSHTML_DISPID_CUSTOM_MIN+i;
423 return S_OK;
427 if(!plugin_container->props) {
428 plugin_container->props = heap_alloc(8*sizeof(DISPID));
429 if(!plugin_container->props)
430 return E_OUTOFMEMORY;
431 plugin_container->props_size = 8;
432 }else if(plugin_container->props_len == plugin_container->props_size) {
433 DISPID *new_props;
435 new_props = heap_realloc(plugin_container->props, plugin_container->props_size*2*sizeof(DISPID));
436 if(!new_props)
437 return E_OUTOFMEMORY;
439 plugin_container->props = new_props;
440 plugin_container->props_size *= 2;
443 plugin_container->props[plugin_container->props_len] = id;
444 *ret = MSHTML_DISPID_CUSTOM_MIN+plugin_container->props_len;
445 plugin_container->props_len++;
446 return S_OK;
449 HRESULT invoke_plugin_prop(HTMLPluginContainer *plugin_container, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
450 VARIANT *res, EXCEPINFO *ei)
452 PluginHost *host;
454 host = plugin_container->plugin_host;
455 if(!host || !host->disp) {
456 FIXME("Called with no disp\n");
457 return E_UNEXPECTED;
460 if(!check_script_safety(host)) {
461 FIXME("Insecure object\n");
462 return E_FAIL;
465 if(id < MSHTML_DISPID_CUSTOM_MIN || id > MSHTML_DISPID_CUSTOM_MIN + plugin_container->props_len) {
466 ERR("Invalid id\n");
467 return E_FAIL;
470 return IDispatch_Invoke(host->disp, plugin_container->props[id-MSHTML_DISPID_CUSTOM_MIN], &IID_NULL,
471 lcid, flags, params, res, ei, NULL);
474 typedef struct {
475 DISPID id;
476 IDispatch *disp;
477 } sink_entry_t;
479 struct PHEventSink {
480 IDispatch IDispatch_iface;
482 LONG ref;
484 PluginHost *host;
485 ITypeInfo *typeinfo;
486 GUID iid;
487 DWORD cookie;
488 BOOL is_dispiface;
490 sink_entry_t *handlers;
491 DWORD handlers_cnt;
492 DWORD handlers_size;
495 static sink_entry_t *find_sink_entry(PHEventSink *sink, DISPID id)
497 sink_entry_t *iter;
499 for(iter = sink->handlers; iter < sink->handlers+sink->handlers_cnt; iter++) {
500 if(iter->id == id)
501 return iter;
504 return NULL;
507 static void add_sink_handler(PHEventSink *sink, DISPID id, IDispatch *disp)
509 sink_entry_t *entry = find_sink_entry(sink, id);
511 if(entry) {
512 if(entry->disp)
513 IDispatch_Release(entry->disp);
514 }else {
515 if(!sink->handlers_size) {
516 sink->handlers = heap_alloc(4*sizeof(*sink->handlers));
517 if(!sink->handlers)
518 return;
519 sink->handlers_size = 4;
520 }else if(sink->handlers_cnt == sink->handlers_size) {
521 sink_entry_t *new_handlers;
523 new_handlers = heap_realloc(sink->handlers, 2*sink->handlers_size*sizeof(*sink->handlers));
524 if(!new_handlers)
525 return;
526 sink->handlers = new_handlers;
527 sink->handlers_size *= 2;
529 entry = sink->handlers + sink->handlers_cnt++;
530 entry->id = id;
533 IDispatch_AddRef(disp);
534 entry->disp = disp;
537 static inline PHEventSink *PHEventSink_from_IDispatch(IDispatch *iface)
539 return CONTAINING_RECORD(iface, PHEventSink, IDispatch_iface);
542 static HRESULT WINAPI PHEventSink_QueryInterface(IDispatch *iface, REFIID riid, void **ppv)
544 PHEventSink *This = PHEventSink_from_IDispatch(iface);
546 if(IsEqualGUID(riid, &IID_IUnknown)) {
547 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
548 *ppv = &This->IDispatch_iface;
549 }else if(IsEqualGUID(riid, &IID_IDispatch)) {
550 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
551 *ppv = &This->IDispatch_iface;
552 }else if(This->is_dispiface && IsEqualGUID(riid, &This->iid)) {
553 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
554 *ppv = &This->IDispatch_iface;
555 }else {
556 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
557 *ppv = NULL;
558 return E_NOINTERFACE;
561 IUnknown_AddRef((IUnknown*)*ppv);
562 return S_OK;
565 static ULONG WINAPI PHEventSink_AddRef(IDispatch *iface)
567 PHEventSink *This = PHEventSink_from_IDispatch(iface);
568 LONG ref = InterlockedIncrement(&This->ref);
570 TRACE("(%p)\n", This);
572 return ref;
575 static ULONG WINAPI PHEventSink_Release(IDispatch *iface)
577 PHEventSink *This = PHEventSink_from_IDispatch(iface);
578 LONG ref = InterlockedDecrement(&This->ref);
580 TRACE("(%p)\n", This);
582 if(!ref) {
583 unsigned i;
585 assert(!This->host);
587 for(i=0; i < This->handlers_cnt; i++) {
588 if(This->handlers[i].disp)
589 IDispatch_Release(This->handlers[i].disp);
591 heap_free(This->handlers);
592 heap_free(This);
595 return ref;
598 static HRESULT WINAPI PHEventSink_GetTypeInfoCount(IDispatch *iface, UINT *pctinfo)
600 PHEventSink *This = PHEventSink_from_IDispatch(iface);
601 FIXME("(%p)->(%p)\n", This, pctinfo);
602 return E_NOTIMPL;
605 static HRESULT WINAPI PHEventSink_GetTypeInfo(IDispatch *iface, UINT iTInfo,
606 LCID lcid, ITypeInfo **ppTInfo)
608 PHEventSink *This = PHEventSink_from_IDispatch(iface);
609 FIXME("(%p)->(%d %d %p)\n", This, iTInfo, lcid, ppTInfo);
610 return E_NOTIMPL;
613 static HRESULT WINAPI PHEventSink_GetIDsOfNames(IDispatch *iface, REFIID riid, LPOLESTR *rgszNames,
614 UINT cNames, LCID lcid, DISPID *rgDispId)
616 PHEventSink *This = PHEventSink_from_IDispatch(iface);
617 FIXME("(%p)->(%s %p %u %d %p)\n", This, debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
618 return E_NOTIMPL;
621 static HRESULT WINAPI PHEventSink_Invoke(IDispatch *iface, DISPID dispIdMember, REFIID riid, LCID lcid,
622 WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
624 PHEventSink *This = PHEventSink_from_IDispatch(iface);
625 IDispatchEx *dispex;
626 sink_entry_t *entry;
627 HRESULT hres;
629 TRACE("(%p)->(%d %s %d %x %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid), lcid, wFlags,
630 pDispParams, pVarResult, pExcepInfo, puArgErr);
632 if(!This->host) {
633 WARN("No host\n");
634 return E_UNEXPECTED;
637 entry = find_sink_entry(This, dispIdMember);
638 if(!entry || !entry->disp) {
639 WARN("No handler %d\n", dispIdMember);
640 if(pVarResult)
641 V_VT(pVarResult) = VT_EMPTY;
642 return S_OK;
645 hres = IDispatch_QueryInterface(entry->disp, &IID_IDispatchEx, (void**)&dispex);
647 TRACE("(%p) %d >>>\n", This, entry->id);
648 if(SUCCEEDED(hres)) {
649 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, wFlags, pDispParams, pVarResult, pExcepInfo, NULL);
650 IDispatchEx_Release(dispex);
651 }else {
652 hres = IDispatch_Invoke(entry->disp, DISPID_VALUE, riid, lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
654 if(SUCCEEDED(hres))
655 TRACE("(%p) %d <<<\n", This, entry->id);
656 else
657 WARN("(%p) %d <<< %08x\n", This, entry->id, hres);
658 return hres;
661 static const IDispatchVtbl PHCPDispatchVtbl = {
662 PHEventSink_QueryInterface,
663 PHEventSink_AddRef,
664 PHEventSink_Release,
665 PHEventSink_GetTypeInfoCount,
666 PHEventSink_GetTypeInfo,
667 PHEventSink_GetIDsOfNames,
668 PHEventSink_Invoke
671 static PHEventSink *create_event_sink(PluginHost *plugin_host, ITypeInfo *typeinfo)
673 IConnectionPointContainer *cp_container;
674 PHEventSink *ret;
675 IConnectionPoint *cp;
676 TYPEATTR *typeattr;
677 TYPEKIND typekind;
678 GUID guid;
679 HRESULT hres;
681 hres = ITypeInfo_GetTypeAttr(typeinfo, &typeattr);
682 if(FAILED(hres))
683 return NULL;
685 typekind = typeattr->typekind;
686 guid = typeattr->guid;
687 ITypeInfo_ReleaseTypeAttr(typeinfo, typeattr);
689 TRACE("guid %s typekind %d\n", debugstr_guid(&guid), typekind);
691 if(typekind != TKIND_INTERFACE && typekind != TKIND_DISPATCH) {
692 WARN("invalid typekind %d\n", typekind);
693 return NULL;
696 hres = IUnknown_QueryInterface(plugin_host->plugin_unk, &IID_IConnectionPointContainer, (void**)&cp_container);
697 if(FAILED(hres)) {
698 WARN("Could not get IConnectionPointContainer iface: %08x\n", hres);
699 return NULL;
702 hres = IConnectionPointContainer_FindConnectionPoint(cp_container, &guid, &cp);
703 IConnectionPointContainer_Release(cp_container);
704 if(FAILED(hres)) {
705 WARN("Could not find %s connection point\n", debugstr_guid(&guid));
706 return NULL;
709 ret = heap_alloc_zero(sizeof(*ret));
710 if(ret) {
711 ret->IDispatch_iface.lpVtbl = &PHCPDispatchVtbl;
712 ret->ref = 1;
713 ret->host = plugin_host;
714 ret->iid = guid;
715 ret->is_dispiface = typekind == TKIND_DISPATCH;
717 ITypeInfo_AddRef(typeinfo);
718 ret->typeinfo = typeinfo;
720 hres = IConnectionPoint_Advise(cp, (IUnknown*)&ret->IDispatch_iface, &ret->cookie);
721 }else {
722 hres = E_OUTOFMEMORY;
725 IConnectionPoint_Release(cp);
726 if(FAILED(hres)) {
727 WARN("Advise failed: %08x\n", hres);
728 return NULL;
731 return ret;
734 static ITypeInfo *get_eventiface_info(HTMLPluginContainer *plugin_container, ITypeInfo *class_info)
736 int impl_types, i, impl_flags;
737 ITypeInfo *ret = NULL;
738 TYPEATTR *typeattr;
739 HREFTYPE ref;
740 HRESULT hres;
742 hres = ITypeInfo_GetTypeAttr(class_info, &typeattr);
743 if(FAILED(hres))
744 return NULL;
746 if(typeattr->typekind != TKIND_COCLASS) {
747 WARN("not coclass\n");
748 ITypeInfo_ReleaseTypeAttr(class_info, typeattr);
749 return NULL;
752 impl_types = typeattr->cImplTypes;
753 ITypeInfo_ReleaseTypeAttr(class_info, typeattr);
755 for(i=0; i<impl_types; i++) {
756 hres = ITypeInfo_GetImplTypeFlags(class_info, i, &impl_flags);
757 if(FAILED(hres))
758 continue;
760 if((impl_flags & IMPLTYPEFLAG_FSOURCE)) {
761 if(!(impl_flags & IMPLTYPEFLAG_FDEFAULT)) {
762 FIXME("Handle non-default source iface\n");
763 continue;
766 hres = ITypeInfo_GetRefTypeOfImplType(class_info, i, &ref);
767 if(FAILED(hres))
768 continue;
770 hres = ITypeInfo_GetRefTypeInfo(class_info, ref, &ret);
771 if(FAILED(hres))
772 ret = NULL;
776 return ret;
779 void bind_activex_event(HTMLDocumentNode *doc, HTMLPluginContainer *plugin_container, WCHAR *event, IDispatch *disp)
781 PluginHost *plugin_host = plugin_container->plugin_host;
782 ITypeInfo *class_info, *source_info;
783 DISPID id;
784 HRESULT hres;
786 TRACE("(%p %p %s %p)\n", doc, plugin_host, debugstr_w(event), disp);
788 if(!plugin_host || !plugin_host->plugin_unk) {
789 WARN("detached element %p\n", plugin_host);
790 return;
793 if(plugin_host->sink) {
794 source_info = plugin_host->sink->typeinfo;
795 ITypeInfo_AddRef(source_info);
796 }else {
797 IProvideClassInfo *provide_ci;
799 hres = IUnknown_QueryInterface(plugin_host->plugin_unk, &IID_IProvideClassInfo, (void**)&provide_ci);
800 if(FAILED(hres)) {
801 FIXME("No IProvideClassInfo, try GetTypeInfo?\n");
802 return;
805 hres = IProvideClassInfo_GetClassInfo(provide_ci, &class_info);
806 IProvideClassInfo_Release(provide_ci);
807 if(FAILED(hres) || !class_info) {
808 WARN("GetClassInfo failed: %08x\n", hres);
809 return;
812 source_info = get_eventiface_info(plugin_container, class_info);
813 ITypeInfo_Release(class_info);
814 if(!source_info)
815 return;
818 hres = ITypeInfo_GetIDsOfNames(source_info, &event, 1, &id);
819 if(FAILED(hres))
820 WARN("Could not get disp id: %08x\n", hres);
821 else if(!plugin_host->sink)
822 plugin_host->sink = create_event_sink(plugin_host, source_info);
824 ITypeInfo_Release(source_info);
825 if(FAILED(hres) || !plugin_host->sink)
826 return;
828 add_sink_handler(plugin_host->sink, id, disp);
831 static inline PluginHost *impl_from_IOleClientSite(IOleClientSite *iface)
833 return CONTAINING_RECORD(iface, PluginHost, IOleClientSite_iface);
836 static HRESULT WINAPI PHClientSite_QueryInterface(IOleClientSite *iface, REFIID riid, void **ppv)
838 PluginHost *This = impl_from_IOleClientSite(iface);
840 if(IsEqualGUID(&IID_IUnknown, riid)) {
841 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
842 *ppv = &This->IOleClientSite_iface;
843 }else if(IsEqualGUID(&IID_IOleClientSite, riid)) {
844 TRACE("(%p)->(IID_IOleClientSite %p)\n", This, ppv);
845 *ppv = &This->IOleClientSite_iface;
846 }else if(IsEqualGUID(&IID_IAdviseSink, riid)) {
847 TRACE("(%p)->(IID_IAdviseSink %p)\n", This, ppv);
848 *ppv = &This->IAdviseSinkEx_iface;
849 }else if(IsEqualGUID(&IID_IAdviseSinkEx, riid)) {
850 TRACE("(%p)->(IID_IAdviseSinkEx %p)\n", This, ppv);
851 *ppv = &This->IAdviseSinkEx_iface;
852 }else if(IsEqualGUID(&IID_IPropertyNotifySink, riid)) {
853 TRACE("(%p)->(IID_IPropertyNotifySink %p)\n", This, ppv);
854 *ppv = &This->IPropertyNotifySink_iface;
855 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
856 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
857 *ppv = &This->IDispatch_iface;
858 }else if(IsEqualGUID(&IID_IOleWindow, riid)) {
859 TRACE("(%p)->(IID_IOleWindow %p)\n", This, ppv);
860 *ppv = &This->IOleInPlaceSiteEx_iface;
861 }else if(IsEqualGUID(&IID_IOleInPlaceSite, riid)) {
862 TRACE("(%p)->(IID_IOleInPlaceSite %p)\n", This, ppv);
863 *ppv = &This->IOleInPlaceSiteEx_iface;
864 }else if(IsEqualGUID(&IID_IOleInPlaceSiteEx, riid)) {
865 TRACE("(%p)->(IID_IOleInPlaceSiteEx %p)\n", This, ppv);
866 *ppv = &This->IOleInPlaceSiteEx_iface;
867 }else if(IsEqualGUID(&IID_IOleControlSite, riid)) {
868 TRACE("(%p)->(IID_IOleControlSite %p)\n", This, ppv);
869 *ppv = &This->IOleControlSite_iface;
870 }else if(IsEqualGUID(&IID_IBindHost, riid)) {
871 TRACE("(%p)->(IID_IBindHost %p)\n", This, ppv);
872 *ppv = &This->IBindHost_iface;
873 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
874 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
875 *ppv = &This->IServiceProvider_iface;
876 }else {
877 WARN("Unsupported interface %s\n", debugstr_guid(riid));
878 *ppv = NULL;
879 return E_NOINTERFACE;
882 IUnknown_AddRef((IUnknown*)*ppv);
883 return S_OK;
886 static ULONG WINAPI PHClientSite_AddRef(IOleClientSite *iface)
888 PluginHost *This = impl_from_IOleClientSite(iface);
889 LONG ref = InterlockedIncrement(&This->ref);
891 TRACE("(%p) ref=%d\n", This, ref);
893 return ref;
896 static ULONG WINAPI PHClientSite_Release(IOleClientSite *iface)
898 PluginHost *This = impl_from_IOleClientSite(iface);
899 LONG ref = InterlockedDecrement(&This->ref);
901 TRACE("(%p) ref=%d\n", This, ref);
903 if(!ref) {
904 if(This->disp)
905 IDispatch_Release(This->disp);
906 if(This->ip_object)
907 IOleInPlaceObject_Release(This->ip_object);
908 if(This->sink) {
909 This->sink->host = NULL;
910 IDispatch_Release(&This->sink->IDispatch_iface);
911 This->sink = NULL;
913 list_remove(&This->entry);
914 if(This->element)
915 This->element->plugin_host = NULL;
916 if(This->plugin_unk)
917 IUnknown_Release(This->plugin_unk);
918 heap_free(This);
921 return ref;
924 static HRESULT WINAPI PHClientSite_SaveObject(IOleClientSite *iface)
926 PluginHost *This = impl_from_IOleClientSite(iface);
927 FIXME("(%p)\n", This);
928 return E_NOTIMPL;
931 static HRESULT WINAPI PHClientSite_GetMoniker(IOleClientSite *iface, DWORD dwAssign,
932 DWORD dwWhichMoniker, IMoniker **ppmk)
934 PluginHost *This = impl_from_IOleClientSite(iface);
936 TRACE("(%p)->(%d %d %p)\n", This, dwAssign, dwWhichMoniker, ppmk);
938 switch(dwWhichMoniker) {
939 case OLEWHICHMK_CONTAINER:
940 if(!This->doc || !This->doc->window || !This->doc->window->mon) {
941 FIXME("no moniker\n");
942 return E_UNEXPECTED;
945 *ppmk = This->doc->window->mon;
946 IMoniker_AddRef(*ppmk);
947 break;
948 default:
949 FIXME("which %d\n", dwWhichMoniker);
950 return E_NOTIMPL;
953 return S_OK;
956 static HRESULT WINAPI PHClientSite_GetContainer(IOleClientSite *iface, IOleContainer **ppContainer)
958 PluginHost *This = impl_from_IOleClientSite(iface);
960 TRACE("(%p)->(%p)\n", This, ppContainer);
962 if(!This->doc) {
963 ERR("Called on detached object\n");
964 return E_UNEXPECTED;
967 *ppContainer = &This->doc->basedoc.IOleContainer_iface;
968 IOleContainer_AddRef(*ppContainer);
969 return S_OK;
972 static HRESULT WINAPI PHClientSite_ShowObject(IOleClientSite *iface)
974 PluginHost *This = impl_from_IOleClientSite(iface);
976 TRACE("(%p)\n", This);
978 return S_OK;
981 static HRESULT WINAPI PHClientSite_OnShowWindow(IOleClientSite *iface, BOOL fShow)
983 PluginHost *This = impl_from_IOleClientSite(iface);
984 FIXME("(%p)->(%x)\n", This, fShow);
985 return E_NOTIMPL;
988 static HRESULT WINAPI PHClientSite_RequestNewObjectLayout(IOleClientSite *iface)
990 PluginHost *This = impl_from_IOleClientSite(iface);
991 FIXME("(%p)\n", This);
992 return E_NOTIMPL;
995 static const IOleClientSiteVtbl OleClientSiteVtbl = {
996 PHClientSite_QueryInterface,
997 PHClientSite_AddRef,
998 PHClientSite_Release,
999 PHClientSite_SaveObject,
1000 PHClientSite_GetMoniker,
1001 PHClientSite_GetContainer,
1002 PHClientSite_ShowObject,
1003 PHClientSite_OnShowWindow,
1004 PHClientSite_RequestNewObjectLayout
1007 static inline PluginHost *impl_from_IAdviseSinkEx(IAdviseSinkEx *iface)
1009 return CONTAINING_RECORD(iface, PluginHost, IAdviseSinkEx_iface);
1012 static HRESULT WINAPI PHAdviseSinkEx_QueryInterface(IAdviseSinkEx *iface, REFIID riid, void **ppv)
1014 PluginHost *This = impl_from_IAdviseSinkEx(iface);
1015 return IOleClientSite_QueryInterface(&This->IOleClientSite_iface, riid, ppv);
1018 static ULONG WINAPI PHAdviseSinkEx_AddRef(IAdviseSinkEx *iface)
1020 PluginHost *This = impl_from_IAdviseSinkEx(iface);
1021 return IOleClientSite_AddRef(&This->IOleClientSite_iface);
1024 static ULONG WINAPI PHAdviseSinkEx_Release(IAdviseSinkEx *iface)
1026 PluginHost *This = impl_from_IAdviseSinkEx(iface);
1027 return IOleClientSite_Release(&This->IOleClientSite_iface);
1030 static void WINAPI PHAdviseSinkEx_OnDataChange(IAdviseSinkEx *iface, FORMATETC *pFormatetc, STGMEDIUM *pStgMedium)
1032 PluginHost *This = impl_from_IAdviseSinkEx(iface);
1033 FIXME("(%p)->(%p %p)\n", This, pFormatetc, pStgMedium);
1036 static void WINAPI PHAdviseSinkEx_OnViewChange(IAdviseSinkEx *iface, DWORD dwAspect, LONG lindex)
1038 PluginHost *This = impl_from_IAdviseSinkEx(iface);
1039 FIXME("(%p)->(%d %d)\n", This, dwAspect, lindex);
1042 static void WINAPI PHAdviseSinkEx_OnRename(IAdviseSinkEx *iface, IMoniker *pmk)
1044 PluginHost *This = impl_from_IAdviseSinkEx(iface);
1045 FIXME("(%p)->(%p)\n", This, pmk);
1048 static void WINAPI PHAdviseSinkEx_OnSave(IAdviseSinkEx *iface)
1050 PluginHost *This = impl_from_IAdviseSinkEx(iface);
1051 FIXME("(%p)\n", This);
1054 static void WINAPI PHAdviseSinkEx_OnClose(IAdviseSinkEx *iface)
1056 PluginHost *This = impl_from_IAdviseSinkEx(iface);
1057 FIXME("(%p)\n", This);
1060 static void WINAPI PHAdviseSinkEx_OnViewStatusChange(IAdviseSinkEx *iface, DWORD dwViewStatus)
1062 PluginHost *This = impl_from_IAdviseSinkEx(iface);
1063 FIXME("(%p)->(%d)\n", This, dwViewStatus);
1066 static const IAdviseSinkExVtbl AdviseSinkExVtbl = {
1067 PHAdviseSinkEx_QueryInterface,
1068 PHAdviseSinkEx_AddRef,
1069 PHAdviseSinkEx_Release,
1070 PHAdviseSinkEx_OnDataChange,
1071 PHAdviseSinkEx_OnViewChange,
1072 PHAdviseSinkEx_OnRename,
1073 PHAdviseSinkEx_OnSave,
1074 PHAdviseSinkEx_OnClose,
1075 PHAdviseSinkEx_OnViewStatusChange
1078 static inline PluginHost *impl_from_IPropertyNotifySink(IPropertyNotifySink *iface)
1080 return CONTAINING_RECORD(iface, PluginHost, IPropertyNotifySink_iface);
1083 static HRESULT WINAPI PHPropertyNotifySink_QueryInterface(IPropertyNotifySink *iface, REFIID riid, void **ppv)
1085 PluginHost *This = impl_from_IPropertyNotifySink(iface);
1086 return IOleClientSite_QueryInterface(&This->IOleClientSite_iface, riid, ppv);
1089 static ULONG WINAPI PHPropertyNotifySink_AddRef(IPropertyNotifySink *iface)
1091 PluginHost *This = impl_from_IPropertyNotifySink(iface);
1092 return IOleClientSite_AddRef(&This->IOleClientSite_iface);
1095 static ULONG WINAPI PHPropertyNotifySink_Release(IPropertyNotifySink *iface)
1097 PluginHost *This = impl_from_IPropertyNotifySink(iface);
1098 return IOleClientSite_Release(&This->IOleClientSite_iface);
1101 static HRESULT WINAPI PHPropertyNotifySink_OnChanged(IPropertyNotifySink *iface, DISPID dispID)
1103 PluginHost *This = impl_from_IPropertyNotifySink(iface);
1105 TRACE("(%p)->(%d)\n", This, dispID);
1107 switch(dispID) {
1108 case DISPID_READYSTATE:
1109 update_readystate(This);
1110 break;
1111 default :
1112 FIXME("Unimplemented dispID %d\n", dispID);
1113 return E_NOTIMPL;
1116 return S_OK;
1119 static HRESULT WINAPI PHPropertyNotifySink_OnRequestEdit(IPropertyNotifySink *iface, DISPID dispID)
1121 PluginHost *This = impl_from_IPropertyNotifySink(iface);
1122 FIXME("(%p)->(%d)\n", This, dispID);
1123 return E_NOTIMPL;
1126 static const IPropertyNotifySinkVtbl PropertyNotifySinkVtbl = {
1127 PHPropertyNotifySink_QueryInterface,
1128 PHPropertyNotifySink_AddRef,
1129 PHPropertyNotifySink_Release,
1130 PHPropertyNotifySink_OnChanged,
1131 PHPropertyNotifySink_OnRequestEdit
1134 static inline PluginHost *impl_from_IDispatch(IDispatch *iface)
1136 return CONTAINING_RECORD(iface, PluginHost, IDispatch_iface);
1139 static HRESULT WINAPI PHDispatch_QueryInterface(IDispatch *iface, REFIID riid, void **ppv)
1141 PluginHost *This = impl_from_IDispatch(iface);
1142 return IOleClientSite_QueryInterface(&This->IOleClientSite_iface, riid, ppv);
1145 static ULONG WINAPI PHDispatch_AddRef(IDispatch *iface)
1147 PluginHost *This = impl_from_IDispatch(iface);
1148 return IOleClientSite_AddRef(&This->IOleClientSite_iface);
1151 static ULONG WINAPI PHDispatch_Release(IDispatch *iface)
1153 PluginHost *This = impl_from_IDispatch(iface);
1154 return IOleClientSite_Release(&This->IOleClientSite_iface);
1157 static HRESULT WINAPI PHDispatch_GetTypeInfoCount(IDispatch *iface, UINT *pctinfo)
1159 PluginHost *This = impl_from_IDispatch(iface);
1160 FIXME("(%p)->(%p)\n", This, pctinfo);
1161 return E_NOTIMPL;
1164 static HRESULT WINAPI PHDispatch_GetTypeInfo(IDispatch *iface, UINT iTInfo,
1165 LCID lcid, ITypeInfo **ppTInfo)
1167 PluginHost *This = impl_from_IDispatch(iface);
1168 FIXME("(%p)->(%d %d %p)\n", This, iTInfo, lcid, ppTInfo);
1169 return E_NOTIMPL;
1172 static HRESULT WINAPI PHDispatch_GetIDsOfNames(IDispatch *iface, REFIID riid,
1173 LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
1175 PluginHost *This = impl_from_IDispatch(iface);
1176 FIXME("(%p)->(%s %p %d %d %p)\n", This, debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
1177 return E_NOTIMPL;
1180 static HRESULT WINAPI PHDispatch_Invoke(IDispatch *iface, DISPID dispid, REFIID riid, LCID lcid,
1181 WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1183 PluginHost *This = impl_from_IDispatch(iface);
1184 FIXME("(%p)->(%d %x %p %p)\n", This, dispid, wFlags, pDispParams, pVarResult);
1185 return E_NOTIMPL;
1188 static const IDispatchVtbl DispatchVtbl = {
1189 PHDispatch_QueryInterface,
1190 PHDispatch_AddRef,
1191 PHDispatch_Release,
1192 PHDispatch_GetTypeInfoCount,
1193 PHDispatch_GetTypeInfo,
1194 PHDispatch_GetIDsOfNames,
1195 PHDispatch_Invoke
1198 static inline PluginHost *impl_from_IOleInPlaceSiteEx(IOleInPlaceSiteEx *iface)
1200 return CONTAINING_RECORD(iface, PluginHost, IOleInPlaceSiteEx_iface);
1203 static HRESULT WINAPI PHInPlaceSite_QueryInterface(IOleInPlaceSiteEx *iface, REFIID riid, void **ppv)
1205 PluginHost *This = impl_from_IOleInPlaceSiteEx(iface);
1206 return IOleClientSite_QueryInterface(&This->IOleClientSite_iface, riid, ppv);
1209 static ULONG WINAPI PHInPlaceSite_AddRef(IOleInPlaceSiteEx *iface)
1211 PluginHost *This = impl_from_IOleInPlaceSiteEx(iface);
1212 return IOleClientSite_AddRef(&This->IOleClientSite_iface);
1215 static ULONG WINAPI PHInPlaceSite_Release(IOleInPlaceSiteEx *iface)
1217 PluginHost *This = impl_from_IOleInPlaceSiteEx(iface);
1218 return IOleClientSite_Release(&This->IOleClientSite_iface);
1221 static HRESULT WINAPI PHInPlaceSite_GetWindow(IOleInPlaceSiteEx *iface, HWND *phwnd)
1223 PluginHost *This = impl_from_IOleInPlaceSiteEx(iface);
1225 TRACE("(%p)->(%p)\n", This, phwnd);
1227 *phwnd = This->hwnd;
1228 return S_OK;
1231 static HRESULT WINAPI PHInPlaceSite_ContextSensitiveHelp(IOleInPlaceSiteEx *iface, BOOL fEnterMode)
1233 PluginHost *This = impl_from_IOleInPlaceSiteEx(iface);
1234 FIXME("(%p)->(%x)\n", This, fEnterMode);
1235 return E_NOTIMPL;
1238 static HRESULT WINAPI PHInPlaceSite_CanInPlaceActivate(IOleInPlaceSiteEx *iface)
1240 PluginHost *This = impl_from_IOleInPlaceSiteEx(iface);
1242 TRACE("(%p)\n", This);
1244 return S_OK;
1247 static HRESULT WINAPI PHInPlaceSite_OnInPlaceActivate(IOleInPlaceSiteEx *iface)
1249 PluginHost *This = impl_from_IOleInPlaceSiteEx(iface);
1250 FIXME("(%p)\n", This);
1251 return E_NOTIMPL;
1254 static HRESULT WINAPI PHInPlaceSite_OnUIActivate(IOleInPlaceSiteEx *iface)
1256 PluginHost *This = impl_from_IOleInPlaceSiteEx(iface);
1258 TRACE("(%p)\n", This);
1260 if(!This->plugin_unk) {
1261 ERR("No plugin object\n");
1262 return E_UNEXPECTED;
1265 This->ui_active = TRUE;
1267 notif_enabled(This);
1268 return S_OK;
1271 static HRESULT WINAPI PHInPlaceSite_GetWindowContext(IOleInPlaceSiteEx *iface,
1272 IOleInPlaceFrame **ppFrame, IOleInPlaceUIWindow **ppDoc, RECT *lprcPosRect,
1273 RECT *lprcClipRect, OLEINPLACEFRAMEINFO *frame_info)
1275 PluginHost *This = impl_from_IOleInPlaceSiteEx(iface);
1276 IOleInPlaceUIWindow *ip_window;
1277 IOleInPlaceFrame *ip_frame;
1278 RECT pr, cr;
1279 HRESULT hres;
1281 TRACE("(%p)->(%p %p %p %p %p)\n", This, ppFrame, ppDoc, lprcPosRect, lprcClipRect, frame_info);
1283 if(!This->doc || !This->doc->basedoc.doc_obj || !This->doc->basedoc.doc_obj->ipsite) {
1284 FIXME("No ipsite\n");
1285 return E_UNEXPECTED;
1288 hres = IOleInPlaceSite_GetWindowContext(This->doc->basedoc.doc_obj->ipsite, &ip_frame, &ip_window, &pr, &cr, frame_info);
1289 if(FAILED(hres)) {
1290 WARN("GetWindowContext failed: %08x\n", hres);
1291 return hres;
1294 if(ip_window)
1295 IOleInPlaceUIWindow_Release(ip_window);
1296 if(ip_frame)
1297 IOleInPlaceFrame_Release(ip_frame);
1299 hres = create_ip_frame(&ip_frame);
1300 if(FAILED(hres))
1301 return hres;
1303 hres = create_ip_window(ppDoc);
1304 if(FAILED(hres)) {
1305 IOleInPlaceFrame_Release(ip_frame);
1306 return hres;
1309 *ppFrame = ip_frame;
1310 *lprcPosRect = This->rect;
1311 *lprcClipRect = This->rect;
1312 return S_OK;
1315 static HRESULT WINAPI PHInPlaceSite_Scroll(IOleInPlaceSiteEx *iface, SIZE scrollExtent)
1317 PluginHost *This = impl_from_IOleInPlaceSiteEx(iface);
1318 FIXME("(%p)->({%d %d})\n", This, scrollExtent.cx, scrollExtent.cy);
1319 return E_NOTIMPL;
1322 static HRESULT WINAPI PHInPlaceSite_OnUIDeactivate(IOleInPlaceSiteEx *iface, BOOL fUndoable)
1324 PluginHost *This = impl_from_IOleInPlaceSiteEx(iface);
1325 FIXME("(%p)->(%x)\n", This, fUndoable);
1326 return E_NOTIMPL;
1329 static HRESULT WINAPI PHInPlaceSite_OnInPlaceDeactivate(IOleInPlaceSiteEx *iface)
1331 PluginHost *This = impl_from_IOleInPlaceSiteEx(iface);
1333 TRACE("(%p)\n", This);
1335 if(This->ip_object) {
1336 IOleInPlaceObject_Release(This->ip_object);
1337 This->ip_object = NULL;
1340 return S_OK;
1343 static HRESULT WINAPI PHInPlaceSite_DiscardUndoState(IOleInPlaceSiteEx *iface)
1345 PluginHost *This = impl_from_IOleInPlaceSiteEx(iface);
1346 FIXME("(%p)\n", This);
1347 return E_NOTIMPL;
1350 static HRESULT WINAPI PHInPlaceSite_DeactivateAndUndo(IOleInPlaceSiteEx *iface)
1352 PluginHost *This = impl_from_IOleInPlaceSiteEx(iface);
1353 FIXME("(%p)\n", This);
1354 return E_NOTIMPL;
1357 static HRESULT WINAPI PHInPlaceSite_OnPosRectChange(IOleInPlaceSiteEx *iface, LPCRECT lprcPosRect)
1359 PluginHost *This = impl_from_IOleInPlaceSiteEx(iface);
1360 FIXME("(%p)->(%p)\n", This, lprcPosRect);
1361 return E_NOTIMPL;
1364 static HRESULT WINAPI PHInPlaceSiteEx_OnInPlaceActivateEx(IOleInPlaceSiteEx *iface, BOOL *pfNoRedraw, DWORD dwFlags)
1366 PluginHost *This = impl_from_IOleInPlaceSiteEx(iface);
1367 HWND hwnd;
1368 HRESULT hres;
1370 TRACE("(%p)->(%p %x)\n", This, pfNoRedraw, dwFlags);
1372 if(This->ip_object)
1373 return S_OK;
1375 hres = IUnknown_QueryInterface(This->plugin_unk, &IID_IOleInPlaceObject, (void**)&This->ip_object);
1376 if(FAILED(hres))
1377 return hres;
1379 hres = IOleInPlaceObject_GetWindow(This->ip_object, &hwnd);
1380 if(SUCCEEDED(hres))
1381 FIXME("Use hwnd %p\n", hwnd);
1383 *pfNoRedraw = FALSE;
1384 return S_OK;
1387 static HRESULT WINAPI PHInPlaceSiteEx_OnInPlaceDeactivateEx(IOleInPlaceSiteEx *iface, BOOL fNoRedraw)
1389 PluginHost *This = impl_from_IOleInPlaceSiteEx(iface);
1390 FIXME("(%p)->(%x)\n", This, fNoRedraw);
1391 return E_NOTIMPL;
1394 static HRESULT WINAPI PHInPlaceSiteEx_RequestUIActivate(IOleInPlaceSiteEx *iface)
1396 PluginHost *This = impl_from_IOleInPlaceSiteEx(iface);
1397 FIXME("(%p)\n", This);
1398 return E_NOTIMPL;
1401 static const IOleInPlaceSiteExVtbl OleInPlaceSiteExVtbl = {
1402 PHInPlaceSite_QueryInterface,
1403 PHInPlaceSite_AddRef,
1404 PHInPlaceSite_Release,
1405 PHInPlaceSite_GetWindow,
1406 PHInPlaceSite_ContextSensitiveHelp,
1407 PHInPlaceSite_CanInPlaceActivate,
1408 PHInPlaceSite_OnInPlaceActivate,
1409 PHInPlaceSite_OnUIActivate,
1410 PHInPlaceSite_GetWindowContext,
1411 PHInPlaceSite_Scroll,
1412 PHInPlaceSite_OnUIDeactivate,
1413 PHInPlaceSite_OnInPlaceDeactivate,
1414 PHInPlaceSite_DiscardUndoState,
1415 PHInPlaceSite_DeactivateAndUndo,
1416 PHInPlaceSite_OnPosRectChange,
1417 PHInPlaceSiteEx_OnInPlaceActivateEx,
1418 PHInPlaceSiteEx_OnInPlaceDeactivateEx,
1419 PHInPlaceSiteEx_RequestUIActivate
1422 static inline PluginHost *impl_from_IOleControlSite(IOleControlSite *iface)
1424 return CONTAINING_RECORD(iface, PluginHost, IOleControlSite_iface);
1427 static HRESULT WINAPI PHControlSite_QueryInterface(IOleControlSite *iface, REFIID riid, void **ppv)
1429 PluginHost *This = impl_from_IOleControlSite(iface);
1430 return IOleClientSite_QueryInterface(&This->IOleClientSite_iface, riid, ppv);
1433 static ULONG WINAPI PHControlSite_AddRef(IOleControlSite *iface)
1435 PluginHost *This = impl_from_IOleControlSite(iface);
1436 return IOleClientSite_AddRef(&This->IOleClientSite_iface);
1439 static ULONG WINAPI PHControlSite_Release(IOleControlSite *iface)
1441 PluginHost *This = impl_from_IOleControlSite(iface);
1442 return IOleClientSite_Release(&This->IOleClientSite_iface);
1445 static HRESULT WINAPI PHControlSite_OnControlInfoChanged(IOleControlSite *iface)
1447 PluginHost *This = impl_from_IOleControlSite(iface);
1448 FIXME("(%p)\n", This);
1449 return E_NOTIMPL;
1452 static HRESULT WINAPI PHControlSite_LockInPlaceActive(IOleControlSite *iface, BOOL fLock)
1454 PluginHost *This = impl_from_IOleControlSite(iface);
1455 FIXME("(%p)->(%x)\n", This, fLock);
1456 return E_NOTIMPL;
1459 static HRESULT WINAPI PHControlSite_GetExtendedControl(IOleControlSite *iface, IDispatch **ppDisp)
1461 PluginHost *This = impl_from_IOleControlSite(iface);
1462 FIXME("(%p)->(%p)\n", This, ppDisp);
1463 return E_NOTIMPL;
1466 static HRESULT WINAPI PHControlSite_TransformCoords(IOleControlSite *iface, POINTL *pPtlHimetric, POINTF *pPtfContainer, DWORD dwFlags)
1468 PluginHost *This = impl_from_IOleControlSite(iface);
1469 FIXME("(%p)->(%p %p %x)\n", This, pPtlHimetric, pPtfContainer, dwFlags);
1470 return E_NOTIMPL;
1473 static HRESULT WINAPI PHControlSite_TranslateAccelerator(IOleControlSite *iface, MSG *pMsg, DWORD grfModifiers)
1475 PluginHost *This = impl_from_IOleControlSite(iface);
1476 FIXME("(%p)->(%x)\n", This, grfModifiers);
1477 return E_NOTIMPL;
1480 static HRESULT WINAPI PHControlSite_OnFocus(IOleControlSite *iface, BOOL fGotFocus)
1482 PluginHost *This = impl_from_IOleControlSite(iface);
1483 FIXME("(%p)->(%x)\n", This, fGotFocus);
1484 return E_NOTIMPL;
1487 static HRESULT WINAPI PHControlSite_ShowPropertyFrame(IOleControlSite *iface)
1489 PluginHost *This = impl_from_IOleControlSite(iface);
1490 FIXME("(%p)\n", This);
1491 return E_NOTIMPL;
1494 static const IOleControlSiteVtbl OleControlSiteVtbl = {
1495 PHControlSite_QueryInterface,
1496 PHControlSite_AddRef,
1497 PHControlSite_Release,
1498 PHControlSite_OnControlInfoChanged,
1499 PHControlSite_LockInPlaceActive,
1500 PHControlSite_GetExtendedControl,
1501 PHControlSite_TransformCoords,
1502 PHControlSite_TranslateAccelerator,
1503 PHControlSite_OnFocus,
1504 PHControlSite_ShowPropertyFrame
1507 static inline PluginHost *impl_from_IBindHost(IBindHost *iface)
1509 return CONTAINING_RECORD(iface, PluginHost, IBindHost_iface);
1512 static HRESULT WINAPI PHBindHost_QueryInterface(IBindHost *iface, REFIID riid, void **ppv)
1514 PluginHost *This = impl_from_IBindHost(iface);
1515 return IOleClientSite_QueryInterface(&This->IOleClientSite_iface, riid, ppv);
1518 static ULONG WINAPI PHBindHost_AddRef(IBindHost *iface)
1520 PluginHost *This = impl_from_IBindHost(iface);
1521 return IOleClientSite_AddRef(&This->IOleClientSite_iface);
1524 static ULONG WINAPI PHBindHost_Release(IBindHost *iface)
1526 PluginHost *This = impl_from_IBindHost(iface);
1527 return IOleClientSite_Release(&This->IOleClientSite_iface);
1530 static HRESULT WINAPI PHBindHost_CreateMoniker(IBindHost *iface, LPOLESTR szName, IBindCtx *pBC, IMoniker **ppmk, DWORD dwReserved)
1532 PluginHost *This = impl_from_IBindHost(iface);
1534 TRACE("(%p)->(%s %p %p %x)\n", This, debugstr_w(szName), pBC, ppmk, dwReserved);
1536 if(!This->doc || !This->doc->window || !This->doc->window->mon) {
1537 FIXME("no moniker\n");
1538 return E_UNEXPECTED;
1541 return CreateURLMoniker(This->doc->window->mon, szName, ppmk);
1544 static HRESULT WINAPI PHBindHost_MonikerBindToStorage(IBindHost *iface, IMoniker *pMk, IBindCtx *pBC,
1545 IBindStatusCallback *pBSC, REFIID riid, void **ppvObj)
1547 PluginHost *This = impl_from_IBindHost(iface);
1548 FIXME("(%p)->(%p %p %p %s %p)\n", This, pMk, pBC, pBSC, debugstr_guid(riid), ppvObj);
1549 return E_NOTIMPL;
1552 static HRESULT WINAPI PHBindHost_MonikerBindToObject(IBindHost *iface, IMoniker *pMk, IBindCtx *pBC,
1553 IBindStatusCallback *pBSC, REFIID riid, void **ppvObj)
1555 PluginHost *This = impl_from_IBindHost(iface);
1556 FIXME("(%p)->(%p %p %p %s %p)\n", This, pMk, pBC, pBSC, debugstr_guid(riid), ppvObj);
1557 return E_NOTIMPL;
1560 static const IBindHostVtbl BindHostVtbl = {
1561 PHBindHost_QueryInterface,
1562 PHBindHost_AddRef,
1563 PHBindHost_Release,
1564 PHBindHost_CreateMoniker,
1565 PHBindHost_MonikerBindToStorage,
1566 PHBindHost_MonikerBindToObject
1569 static inline PluginHost *impl_from_IServiceProvider(IServiceProvider *iface)
1571 return CONTAINING_RECORD(iface, PluginHost, IServiceProvider_iface);
1574 static HRESULT WINAPI PHServiceProvider_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
1576 PluginHost *This = impl_from_IServiceProvider(iface);
1577 return IOleClientSite_QueryInterface(&This->IOleClientSite_iface, riid, ppv);
1580 static ULONG WINAPI PHServiceProvider_AddRef(IServiceProvider *iface)
1582 PluginHost *This = impl_from_IServiceProvider(iface);
1583 return IOleClientSite_AddRef(&This->IOleClientSite_iface);
1586 static ULONG WINAPI PHServiceProvider_Release(IServiceProvider *iface)
1588 PluginHost *This = impl_from_IServiceProvider(iface);
1589 return IOleClientSite_Release(&This->IOleClientSite_iface);
1592 static HRESULT WINAPI PHServiceProvider_QueryService(IServiceProvider *iface, REFGUID guidService, REFIID riid, void **ppv)
1594 PluginHost *This = impl_from_IServiceProvider(iface);
1596 if(IsEqualGUID(guidService, &SID_SBindHost)) {
1597 TRACE("SID_SBindHost service\n");
1598 return IOleClientSite_QueryInterface(&This->IOleClientSite_iface, riid, ppv);
1601 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
1603 if(!This->doc || !This->doc->basedoc.window) {
1604 *ppv = NULL;
1605 return E_NOINTERFACE;
1608 return IServiceProvider_QueryService(&This->doc->basedoc.window->base.IServiceProvider_iface,
1609 guidService, riid, ppv);
1612 static const IServiceProviderVtbl ServiceProviderVtbl = {
1613 PHServiceProvider_QueryInterface,
1614 PHServiceProvider_AddRef,
1615 PHServiceProvider_Release,
1616 PHServiceProvider_QueryService
1619 static HRESULT assoc_element(PluginHost *host, HTMLDocumentNode *doc, nsIDOMElement *nselem)
1621 HTMLPluginContainer *container_elem;
1622 HTMLDOMNode *node;
1623 HRESULT hres;
1625 hres = get_node(doc, (nsIDOMNode*)nselem, TRUE, &node);
1626 if(FAILED(hres))
1627 return hres;
1629 hres = IHTMLDOMNode_QueryInterface(&node->IHTMLDOMNode_iface, &IID_HTMLPluginContainer,
1630 (void**)&container_elem);
1631 node_release(node);
1632 if(FAILED(hres)) {
1633 ERR("Not an object element\n");
1634 return hres;
1637 container_elem->plugin_host = host;
1638 host->element = container_elem;
1639 return S_OK;
1642 void detach_plugin_host(PluginHost *host)
1644 HRESULT hres;
1646 TRACE("%p\n", host);
1648 if(!host->doc)
1649 return;
1651 if(host->ip_object) {
1652 if(host->ui_active)
1653 IOleInPlaceObject_UIDeactivate(host->ip_object);
1654 IOleInPlaceObject_InPlaceDeactivate(host->ip_object);
1657 if(host->plugin_unk) {
1658 IOleObject *ole_obj;
1660 hres = IUnknown_QueryInterface(host->plugin_unk, &IID_IOleObject, (void**)&ole_obj);
1661 if(SUCCEEDED(hres)) {
1662 if(!host->ip_object)
1663 IOleObject_Close(ole_obj, OLECLOSE_NOSAVE);
1664 IOleObject_SetClientSite(ole_obj, NULL);
1665 IOleObject_Release(ole_obj);
1669 if(host->sink) {
1670 IConnectionPointContainer *cp_container;
1671 IConnectionPoint *cp;
1673 assert(host->plugin_unk != NULL);
1675 hres = IUnknown_QueryInterface(host->plugin_unk, &IID_IConnectionPointContainer, (void**)&cp_container);
1676 if(SUCCEEDED(hres)) {
1677 hres = IConnectionPointContainer_FindConnectionPoint(cp_container, &host->sink->iid, &cp);
1678 IConnectionPointContainer_Release(cp_container);
1679 if(SUCCEEDED(hres)) {
1680 IConnectionPoint_Unadvise(cp, host->sink->cookie);
1681 IConnectionPoint_Release(cp);
1685 host->sink->host = NULL;
1686 IDispatch_Release(&host->sink->IDispatch_iface);
1687 host->sink = NULL;
1690 if(host->element) {
1691 host->element->plugin_host = NULL;
1692 host->element = NULL;
1695 list_remove(&host->entry);
1696 list_init(&host->entry);
1697 host->doc = NULL;
1700 HRESULT create_plugin_host(HTMLDocumentNode *doc, nsIDOMElement *nselem, IUnknown *unk, const CLSID *clsid, PluginHost **ret)
1702 PluginHost *host;
1703 HRESULT hres;
1705 host = heap_alloc_zero(sizeof(*host));
1706 if(!host)
1707 return E_OUTOFMEMORY;
1709 host->IOleClientSite_iface.lpVtbl = &OleClientSiteVtbl;
1710 host->IAdviseSinkEx_iface.lpVtbl = &AdviseSinkExVtbl;
1711 host->IPropertyNotifySink_iface.lpVtbl = &PropertyNotifySinkVtbl;
1712 host->IDispatch_iface.lpVtbl = &DispatchVtbl;
1713 host->IOleInPlaceSiteEx_iface.lpVtbl = &OleInPlaceSiteExVtbl;
1714 host->IOleControlSite_iface.lpVtbl = &OleControlSiteVtbl;
1715 host->IBindHost_iface.lpVtbl = &BindHostVtbl;
1716 host->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
1718 host->ref = 1;
1720 hres = assoc_element(host, doc, nselem);
1721 if(FAILED(hres)) {
1722 heap_free(host);
1723 return hres;
1726 IUnknown_AddRef(unk);
1727 host->plugin_unk = unk;
1728 host->clsid = *clsid;
1730 host->doc = doc;
1731 list_add_tail(&doc->plugin_hosts, &host->entry);
1733 *ret = host;
1734 return S_OK;