xref: /reactos/dll/win32/ieframe/dochost.c (revision c2c66aff)
1 /*
2  * Copyright 2005-2006 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 "ieframe.h"
20 
21 #include <initguid.h>
22 
23 DEFINE_OLEGUID(CGID_DocHostCmdPriv, 0x000214D4L, 0, 0);
24 
25 #define DOCHOST_DOCCANNAVIGATE  0
26 
27 /* Undocumented notification, see mshtml tests */
28 #define CMDID_EXPLORER_UPDATEHISTORY 38
29 
30 static ATOM doc_view_atom = 0;
31 
32 void push_dochost_task(DocHost *This, task_header_t *task, task_proc_t proc, task_destr_t destr, BOOL send)
33 {
34     BOOL is_empty;
35 
36     task->proc = proc;
37     task->destr = destr;
38 
39     is_empty = list_empty(&This->task_queue);
40     list_add_tail(&This->task_queue, &task->entry);
41 
42     if(send)
43         SendMessageW(This->frame_hwnd, WM_DOCHOSTTASK, 0, 0);
44     else if(is_empty)
45         PostMessageW(This->frame_hwnd, WM_DOCHOSTTASK, 0, 0);
46 }
47 
48 LRESULT process_dochost_tasks(DocHost *This)
49 {
50     task_header_t *task;
51 
52     while(!list_empty(&This->task_queue)) {
53         task = LIST_ENTRY(This->task_queue.next, task_header_t, entry);
54         list_remove(&task->entry);
55 
56         task->proc(This, task);
57         task->destr(task);
58     }
59 
60     return 0;
61 }
62 
63 void abort_dochost_tasks(DocHost *This, task_proc_t proc)
64 {
65     task_header_t *task, *cursor;
66 
67     LIST_FOR_EACH_ENTRY_SAFE(task, cursor, &This->task_queue, task_header_t, entry) {
68         if(proc && proc != task->proc)
69             continue;
70 
71         list_remove(&task->entry);
72         task->destr(task);
73     }
74 }
75 
76 void on_commandstate_change(DocHost *doc_host, LONG command, BOOL enable)
77 {
78     DISPPARAMS dispparams;
79     VARIANTARG params[2];
80 
81     TRACE("command=%d enable=%d\n", command, enable);
82 
83     dispparams.cArgs = 2;
84     dispparams.cNamedArgs = 0;
85     dispparams.rgdispidNamedArgs = NULL;
86     dispparams.rgvarg = params;
87 
88     V_VT(params) = VT_BOOL;
89     V_BOOL(params) = enable ? VARIANT_TRUE : VARIANT_FALSE;
90 
91     V_VT(params+1) = VT_I4;
92     V_I4(params+1) = command;
93 
94     call_sink(doc_host->cps.wbe2, DISPID_COMMANDSTATECHANGE, &dispparams);
95 
96     doc_host->container_vtbl->on_command_state_change(doc_host, command, enable);
97 }
98 
99 void update_navigation_commands(DocHost *dochost)
100 {
101     unsigned pos = dochost->travellog.loading_pos == -1 ? dochost->travellog.position : dochost->travellog.loading_pos;
102 
103     on_commandstate_change(dochost, CSC_NAVIGATEBACK, pos > 0);
104     on_commandstate_change(dochost, CSC_NAVIGATEFORWARD, pos < dochost->travellog.length);
105 }
106 
107 static void notif_complete(DocHost *This, DISPID dispid)
108 {
109     DISPPARAMS dispparams;
110     VARIANTARG params[2];
111     VARIANT url;
112 
113     dispparams.cArgs = 2;
114     dispparams.cNamedArgs = 0;
115     dispparams.rgdispidNamedArgs = NULL;
116     dispparams.rgvarg = params;
117 
118     V_VT(params) = (VT_BYREF|VT_VARIANT);
119     V_BYREF(params) = &url;
120 
121     V_VT(params+1) = VT_DISPATCH;
122     V_DISPATCH(params+1) = (IDispatch*)This->wb;
123 
124     V_VT(&url) = VT_BSTR;
125     V_BSTR(&url) = SysAllocString(This->url);
126 
127     TRACE("%d >>>\n", dispid);
128     call_sink(This->cps.wbe2, dispid, &dispparams);
129     TRACE("%d <<<\n", dispid);
130 
131     SysFreeString(V_BSTR(&url));
132     This->busy = VARIANT_FALSE;
133 }
134 
135 static void object_available(DocHost *This)
136 {
137     IHlinkTarget *hlink;
138     HRESULT hres;
139 
140     TRACE("(%p)\n", This);
141 
142     if(!This->document) {
143         WARN("document == NULL\n");
144         return;
145     }
146 
147     hres = IUnknown_QueryInterface(This->document, &IID_IHlinkTarget, (void**)&hlink);
148     if(SUCCEEDED(hres)) {
149         hres = IHlinkTarget_Navigate(hlink, 0, NULL);
150         IHlinkTarget_Release(hlink);
151         if(FAILED(hres))
152             FIXME("Navigate failed\n");
153     }else {
154         IOleObject *ole_object;
155         RECT rect;
156 
157         TRACE("No IHlink iface\n");
158 
159         hres = IUnknown_QueryInterface(This->document, &IID_IOleObject, (void**)&ole_object);
160         if(FAILED(hres)) {
161             FIXME("Could not get IOleObject iface: %08x\n", hres);
162             return;
163         }
164 
165         GetClientRect(This->hwnd, &rect);
166         hres = IOleObject_DoVerb(ole_object, OLEIVERB_SHOW, NULL, &This->IOleClientSite_iface, -1, This->hwnd, &rect);
167         IOleObject_Release(ole_object);
168         if(FAILED(hres))
169             FIXME("DoVerb failed: %08x\n", hres);
170     }
171 }
172 
173 static HRESULT get_doc_ready_state(DocHost *This, READYSTATE *ret)
174 {
175     DISPPARAMS dp = {NULL,NULL,0,0};
176     IDispatch *disp;
177     EXCEPINFO ei;
178     VARIANT var;
179     HRESULT hres;
180 
181     hres = IUnknown_QueryInterface(This->document, &IID_IDispatch, (void**)&disp);
182     if(FAILED(hres))
183         return hres;
184 
185     hres = IDispatch_Invoke(disp, DISPID_READYSTATE, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET,
186             &dp, &var, &ei, NULL);
187     IDispatch_Release(disp);
188     if(FAILED(hres)) {
189         WARN("Invoke(DISPID_READYSTATE failed: %08x\n", hres);
190         return hres;
191     }
192 
193     if(V_VT(&var) != VT_I4) {
194         WARN("V_VT(var) = %d\n", V_VT(&var));
195         VariantClear(&var);
196         return E_FAIL;
197     }
198 
199     *ret = V_I4(&var);
200     return S_OK;
201 }
202 
203 static void advise_prop_notif(DocHost *This, BOOL set)
204 {
205     IConnectionPointContainer *cp_container;
206     IConnectionPoint *cp;
207     HRESULT hres;
208 
209     hres = IUnknown_QueryInterface(This->document, &IID_IConnectionPointContainer, (void**)&cp_container);
210     if(FAILED(hres))
211         return;
212 
213     hres = IConnectionPointContainer_FindConnectionPoint(cp_container, &IID_IPropertyNotifySink, &cp);
214     IConnectionPointContainer_Release(cp_container);
215     if(FAILED(hres))
216         return;
217 
218     if(set)
219         hres = IConnectionPoint_Advise(cp, (IUnknown*)&This->IPropertyNotifySink_iface, &This->prop_notif_cookie);
220     else
221         hres = IConnectionPoint_Unadvise(cp, This->prop_notif_cookie);
222     IConnectionPoint_Release(cp);
223 
224     if(SUCCEEDED(hres))
225         This->is_prop_notif = set;
226 }
227 
228 void set_doc_state(DocHost *This, READYSTATE doc_state)
229 {
230     This->doc_state = doc_state;
231     if(doc_state > This->ready_state)
232         This->ready_state = doc_state;
233 }
234 
235 static void update_ready_state(DocHost *This, READYSTATE ready_state)
236 {
237     if(ready_state > READYSTATE_LOADING && This->travellog.loading_pos != -1) {
238         WARN("histupdate not notified\n");
239         This->travellog.position = This->travellog.loading_pos;
240         This->travellog.loading_pos = -1;
241     }
242 
243     if(ready_state > READYSTATE_LOADING && This->doc_state <= READYSTATE_LOADING && !This->browser_service /* FIXME */)
244         notif_complete(This, DISPID_NAVIGATECOMPLETE2);
245 
246     if(ready_state == READYSTATE_COMPLETE && This->doc_state < READYSTATE_COMPLETE) {
247         set_doc_state(This, READYSTATE_COMPLETE);
248         if(!This->browser_service) /* FIXME: Not fully correct */
249             notif_complete(This, DISPID_DOCUMENTCOMPLETE);
250     }else {
251         set_doc_state(This, ready_state);
252     }
253 }
254 
255 typedef struct {
256     task_header_t header;
257     IUnknown *doc;
258     READYSTATE ready_state;
259 } ready_state_task_t;
260 
261 static void ready_state_task_destr(task_header_t *_task)
262 {
263     ready_state_task_t *task = (ready_state_task_t*)_task;
264 
265     IUnknown_Release(task->doc);
266     heap_free(task);
267 }
268 
269 static void ready_state_proc(DocHost *This, task_header_t *_task)
270 {
271     ready_state_task_t *task = (ready_state_task_t*)_task;
272 
273     if(task->doc == This->document)
274         update_ready_state(This, task->ready_state);
275 }
276 
277 static void push_ready_state_task(DocHost *This, READYSTATE ready_state)
278 {
279     ready_state_task_t *task = heap_alloc(sizeof(ready_state_task_t));
280 
281     IUnknown_AddRef(This->document);
282     task->doc = This->document;
283     task->ready_state = ready_state;
284 
285     push_dochost_task(This, &task->header, ready_state_proc, ready_state_task_destr, FALSE);
286 }
287 
288 static void object_available_task_destr(task_header_t *task)
289 {
290     heap_free(task);
291 }
292 
293 static void object_available_proc(DocHost *This, task_header_t *task)
294 {
295     object_available(This);
296 }
297 
298 HRESULT dochost_object_available(DocHost *This, IUnknown *doc)
299 {
300     READYSTATE ready_state;
301     task_header_t *task;
302     IOleObject *oleobj;
303     HRESULT hres;
304 
305     IUnknown_AddRef(doc);
306     This->document = doc;
307 
308     hres = IUnknown_QueryInterface(doc, &IID_IOleObject, (void**)&oleobj);
309     if(SUCCEEDED(hres)) {
310         CLSID clsid;
311 
312         hres = IOleObject_GetUserClassID(oleobj, &clsid);
313         if(SUCCEEDED(hres))
314             TRACE("Got clsid %s\n",
315                   IsEqualGUID(&clsid, &CLSID_HTMLDocument) ? "CLSID_HTMLDocument" : debugstr_guid(&clsid));
316 
317         hres = IOleObject_SetClientSite(oleobj, &This->IOleClientSite_iface);
318         if(FAILED(hres))
319             FIXME("SetClientSite failed: %08x\n", hres);
320 
321         IOleObject_Release(oleobj);
322     }else {
323         FIXME("Could not get IOleObject iface: %08x\n", hres);
324     }
325 
326     /* FIXME: Call SetAdvise */
327 
328     task = heap_alloc(sizeof(*task));
329     push_dochost_task(This, task, object_available_proc, object_available_task_destr, FALSE);
330 
331     hres = get_doc_ready_state(This, &ready_state);
332     if(SUCCEEDED(hres)) {
333         if(ready_state == READYSTATE_COMPLETE)
334             push_ready_state_task(This, READYSTATE_COMPLETE);
335         if(ready_state != READYSTATE_COMPLETE || This->doc_navigate)
336             advise_prop_notif(This, TRUE);
337     }else if(!This->doc_navigate) {
338         /* If we can't get document's ready state, there is not much we can do.
339          * Assume that document is complete at this point. */
340         push_ready_state_task(This, READYSTATE_COMPLETE);
341     }
342 
343     return S_OK;
344 }
345 
346 static LRESULT resize_document(DocHost *This, LONG width, LONG height)
347 {
348     RECT rect = {0, 0, width, height};
349 
350     TRACE("(%p)->(%d %d)\n", This, width, height);
351 
352     if(This->view)
353         IOleDocumentView_SetRect(This->view, &rect);
354 
355     return 0;
356 }
357 
358 static LRESULT WINAPI doc_view_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
359 {
360     DocHost *This;
361 
362     static const WCHAR wszTHIS[] = {'T','H','I','S',0};
363 
364     if(msg == WM_CREATE) {
365         This = *(DocHost**)lParam;
366         SetPropW(hwnd, wszTHIS, This);
367     }else {
368         This = GetPropW(hwnd, wszTHIS);
369     }
370 
371     switch(msg) {
372     case WM_SIZE:
373         return resize_document(This, LOWORD(lParam), HIWORD(lParam));
374     }
375 
376     return DefWindowProcW(hwnd, msg, wParam, lParam);
377 }
378 
379 static void free_travellog_entry(travellog_entry_t *entry)
380 {
381     if(entry->stream)
382         IStream_Release(entry->stream);
383     heap_free(entry->url);
384 }
385 
386 static IStream *get_travellog_stream(DocHost *This)
387 {
388     IPersistHistory *persist_history;
389     IStream *stream;
390     HRESULT hres;
391 
392     hres = IUnknown_QueryInterface(This->document, &IID_IPersistHistory, (void**)&persist_history);
393     if(FAILED(hres))
394         return NULL;
395 
396     hres = CreateStreamOnHGlobal(NULL, TRUE, &stream);
397     if(SUCCEEDED(hres))
398         hres = IPersistHistory_SaveHistory(persist_history, stream);
399     IPersistHistory_Release(persist_history);
400     if(FAILED(hres)) {
401         IStream_Release(stream);
402         return NULL;
403     }
404 
405     return stream;
406 }
407 
408 static void dump_travellog(DocHost *This)
409 {
410     unsigned i;
411 
412     for(i=0; i < This->travellog.length; i++)
413         TRACE("%d: %s %s\n", i, i == This->travellog.position ? "=>" : "  ", debugstr_w(This->travellog.log[i].url));
414     if(i == This->travellog.position)
415         TRACE("%d: =>\n", i);
416 }
417 
418 static void update_travellog(DocHost *This)
419 {
420     travellog_entry_t *new_entry;
421 
422     static const WCHAR about_schemeW[] = {'a','b','o','u','t',':'};
423 
424     if(This->url && !strncmpiW(This->url, about_schemeW, sizeof(about_schemeW)/sizeof(*about_schemeW))) {
425         TRACE("Skipping about URL\n");
426         return;
427     }
428 
429     if(!This->travellog.log) {
430         This->travellog.log = heap_alloc(4 * sizeof(*This->travellog.log));
431         if(!This->travellog.log)
432             return;
433 
434         This->travellog.size = 4;
435     }else if(This->travellog.size < This->travellog.position+1) {
436         travellog_entry_t *new_travellog;
437 
438         new_travellog = heap_realloc(This->travellog.log, This->travellog.size*2*sizeof(*This->travellog.log));
439         if(!new_travellog)
440             return;
441 
442         This->travellog.log = new_travellog;
443         This->travellog.size *= 2;
444     }
445 
446     if(This->travellog.loading_pos == -1) {
447         /* Clear forward history. */
448         while(This->travellog.length > This->travellog.position)
449             free_travellog_entry(This->travellog.log + --This->travellog.length);
450     }
451 
452     new_entry = This->travellog.log + This->travellog.position;
453 
454     new_entry->url = heap_strdupW(This->url);
455     TRACE("Adding %s at %d\n", debugstr_w(This->url), This->travellog.position);
456     if(!new_entry->url)
457         return;
458 
459     new_entry->stream = get_travellog_stream(This);
460 
461     if(This->travellog.loading_pos == -1) {
462         This->travellog.position++;
463     }else {
464          This->travellog.position = This->travellog.loading_pos;
465          This->travellog.loading_pos = -1;
466     }
467     if(This->travellog.position > This->travellog.length)
468         This->travellog.length = This->travellog.position;
469 
470     dump_travellog(This);
471 }
472 
473 void create_doc_view_hwnd(DocHost *This)
474 {
475     RECT rect;
476 
477     static const WCHAR wszShell_DocObject_View[] =
478         {'S','h','e','l','l',' ','D','o','c','O','b','j','e','c','t',' ','V','i','e','w',0};
479 
480     if(!doc_view_atom) {
481         static WNDCLASSEXW wndclass = {
482             sizeof(wndclass),
483             CS_PARENTDC,
484             doc_view_proc,
485             0, 0 /* native uses 4*/, NULL, NULL, NULL,
486             (HBRUSH)(COLOR_WINDOW + 1), NULL,
487             wszShell_DocObject_View,
488             NULL
489         };
490 
491         wndclass.hInstance = ieframe_instance;
492 
493         doc_view_atom = RegisterClassExW(&wndclass);
494     }
495 
496     This->container_vtbl->get_docobj_rect(This, &rect);
497     This->hwnd = CreateWindowExW(0, wszShell_DocObject_View,
498          wszShell_DocObject_View,
499          WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_TABSTOP,
500          rect.left, rect.top, rect.right, rect.bottom, This->frame_hwnd,
501          NULL, ieframe_instance, This);
502 }
503 
504 void deactivate_document(DocHost *This)
505 {
506     IOleInPlaceObjectWindowless *winobj;
507     IOleObject *oleobj = NULL;
508     IHlinkTarget *hlink = NULL;
509     HRESULT hres;
510 
511     if(!This->document) return;
512 
513     if(This->doc_navigate) {
514         IUnknown_Release(This->doc_navigate);
515         This->doc_navigate = NULL;
516     }
517 
518     if(This->is_prop_notif)
519         advise_prop_notif(This, FALSE);
520 
521     if(This->view)
522         IOleDocumentView_UIActivate(This->view, FALSE);
523 
524     hres = IUnknown_QueryInterface(This->document, &IID_IOleInPlaceObjectWindowless,
525                                    (void**)&winobj);
526     if(SUCCEEDED(hres)) {
527         IOleInPlaceObjectWindowless_InPlaceDeactivate(winobj);
528         IOleInPlaceObjectWindowless_Release(winobj);
529     }
530 
531     if(This->view) {
532         IOleDocumentView_Show(This->view, FALSE);
533         IOleDocumentView_CloseView(This->view, 0);
534         IOleDocumentView_SetInPlaceSite(This->view, NULL);
535         IOleDocumentView_Release(This->view);
536         This->view = NULL;
537     }
538 
539     hres = IUnknown_QueryInterface(This->document, &IID_IOleObject, (void**)&oleobj);
540     if(SUCCEEDED(hres))
541         IOleObject_Close(oleobj, OLECLOSE_NOSAVE);
542 
543     hres = IUnknown_QueryInterface(This->document, &IID_IHlinkTarget, (void**)&hlink);
544     if(SUCCEEDED(hres)) {
545         IHlinkTarget_SetBrowseContext(hlink, NULL);
546         IHlinkTarget_Release(hlink);
547     }
548 
549     if(oleobj) {
550         IOleClientSite *client_site = NULL;
551 
552         IOleObject_GetClientSite(oleobj, &client_site);
553         if(client_site) {
554             if(client_site == &This->IOleClientSite_iface)
555                 IOleObject_SetClientSite(oleobj, NULL);
556             IOleClientSite_Release(client_site);
557         }
558 
559         IOleObject_Release(oleobj);
560     }
561 
562     IUnknown_Release(This->document);
563     This->document = NULL;
564 }
565 
566 HRESULT refresh_document(DocHost *This, const VARIANT *level)
567 {
568     IOleCommandTarget *cmdtrg;
569     VARIANT vin, vout;
570     HRESULT hres;
571 
572     if(level && (V_VT(level) != VT_I4 || V_I4(level) != REFRESH_NORMAL))
573         FIXME("Unsupported refresh level %s\n", debugstr_variant(level));
574 
575     if(!This->document) {
576         FIXME("no document\n");
577         return E_FAIL;
578     }
579 
580     hres = IUnknown_QueryInterface(This->document, &IID_IOleCommandTarget, (void**)&cmdtrg);
581     if(FAILED(hres))
582         return hres;
583 
584     V_VT(&vin) = VT_EMPTY;
585     V_VT(&vout) = VT_EMPTY;
586     hres = IOleCommandTarget_Exec(cmdtrg, NULL, OLECMDID_REFRESH, OLECMDEXECOPT_PROMPTUSER, &vin, &vout);
587     IOleCommandTarget_Release(cmdtrg);
588     if(FAILED(hres))
589         return hres;
590 
591     VariantClear(&vout);
592     return S_OK;
593 }
594 
595 void release_dochost_client(DocHost *This)
596 {
597     if(This->hwnd) {
598         DestroyWindow(This->hwnd);
599         This->hwnd = NULL;
600     }
601 
602     if(This->hostui) {
603         IDocHostUIHandler_Release(This->hostui);
604         This->hostui = NULL;
605     }
606 
607     if(This->client_disp) {
608         IDispatch_Release(This->client_disp);
609         This->client_disp = NULL;
610     }
611 
612     if(This->frame) {
613         IOleInPlaceFrame_Release(This->frame);
614         This->frame = NULL;
615     }
616 
617     if(This->olecmd) {
618         IOleCommandTarget_Release(This->olecmd);
619         This->olecmd = NULL;
620     }
621 }
622 
623 static inline DocHost *impl_from_IOleCommandTarget(IOleCommandTarget *iface)
624 {
625     return CONTAINING_RECORD(iface, DocHost, IOleCommandTarget_iface);
626 }
627 
628 static HRESULT WINAPI ClOleCommandTarget_QueryInterface(IOleCommandTarget *iface,
629         REFIID riid, void **ppv)
630 {
631     DocHost *This = impl_from_IOleCommandTarget(iface);
632     return IOleClientSite_QueryInterface(&This->IOleClientSite_iface, riid, ppv);
633 }
634 
635 static ULONG WINAPI ClOleCommandTarget_AddRef(IOleCommandTarget *iface)
636 {
637     DocHost *This = impl_from_IOleCommandTarget(iface);
638     return IOleClientSite_AddRef(&This->IOleClientSite_iface);
639 }
640 
641 static ULONG WINAPI ClOleCommandTarget_Release(IOleCommandTarget *iface)
642 {
643     DocHost *This = impl_from_IOleCommandTarget(iface);
644     return IOleClientSite_Release(&This->IOleClientSite_iface);
645 }
646 
647 static HRESULT WINAPI ClOleCommandTarget_QueryStatus(IOleCommandTarget *iface,
648         const GUID *pguidCmdGroup, ULONG cCmds, OLECMD prgCmds[], OLECMDTEXT *pCmdText)
649 {
650     DocHost *This = impl_from_IOleCommandTarget(iface);
651     ULONG i;
652 
653     TRACE("(%p)->(%s %u %p %p)\n", This, debugstr_guid(pguidCmdGroup), cCmds, prgCmds,
654           pCmdText);
655     for(i=0; prgCmds && i < cCmds; i++)
656         TRACE("unsupported command %u (%x)\n", prgCmds[i].cmdID, prgCmds[i].cmdf);
657 
658     return E_NOTIMPL;
659 }
660 
661 static HRESULT WINAPI ClOleCommandTarget_Exec(IOleCommandTarget *iface,
662         const GUID *pguidCmdGroup, DWORD nCmdID, DWORD nCmdexecopt, VARIANT *pvaIn,
663         VARIANT *pvaOut)
664 {
665     DocHost *This = impl_from_IOleCommandTarget(iface);
666 
667     TRACE("(%p)->(%s %d %d %s %s)\n", This, debugstr_guid(pguidCmdGroup), nCmdID, nCmdexecopt,
668             debugstr_variant(pvaIn), debugstr_variant(pvaOut));
669 
670     if(!pguidCmdGroup) {
671         switch(nCmdID) {
672         case OLECMDID_UPDATECOMMANDS:
673             if(!This->olecmd)
674                 return E_NOTIMPL;
675             return IOleCommandTarget_Exec(This->olecmd, pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
676         case OLECMDID_SETDOWNLOADSTATE:
677             if(This->olecmd)
678                 return IOleCommandTarget_Exec(This->olecmd, pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
679 
680             if(!pvaIn || V_VT(pvaIn) != VT_I4)
681                 return E_INVALIDARG;
682 
683             notify_download_state(This, V_I4(pvaIn));
684             return S_OK;
685         default:
686             TRACE("Unimplemented cmdid %d\n", nCmdID);
687             return E_NOTIMPL;
688         }
689     }
690 
691     if(IsEqualGUID(pguidCmdGroup, &CGID_DocHostCmdPriv)) {
692         switch(nCmdID) {
693         case DOCHOST_DOCCANNAVIGATE:
694             if(!pvaIn || V_VT(pvaIn) != VT_UNKNOWN)
695                 return E_INVALIDARG;
696 
697             if(This->doc_navigate)
698                 IUnknown_Release(This->doc_navigate);
699             IUnknown_AddRef(V_UNKNOWN(pvaIn));
700             This->doc_navigate = V_UNKNOWN(pvaIn);
701             return S_OK;
702 
703         case 1: {
704             IHTMLWindow2 *win2;
705             SAFEARRAY *sa = V_ARRAY(pvaIn);
706             VARIANT status_code, url, htmlwindow;
707             LONG ind;
708             HRESULT hres;
709 
710             if(V_VT(pvaIn) != VT_ARRAY || !sa || (SafeArrayGetDim(sa) != 1))
711                 return E_INVALIDARG;
712 
713             ind = 0;
714             hres = SafeArrayGetElement(sa, &ind, &status_code);
715             if(FAILED(hres) || V_VT(&status_code)!=VT_I4)
716                 return E_INVALIDARG;
717 
718             ind = 1;
719             hres = SafeArrayGetElement(sa, &ind, &url);
720             if(FAILED(hres) || V_VT(&url)!=VT_BSTR)
721                 return E_INVALIDARG;
722 
723             ind = 3;
724             hres = SafeArrayGetElement(sa, &ind, &htmlwindow);
725             if(FAILED(hres) || V_VT(&htmlwindow)!=VT_UNKNOWN || !V_UNKNOWN(&htmlwindow))
726                 return E_INVALIDARG;
727 
728             hres = IUnknown_QueryInterface(V_UNKNOWN(&htmlwindow), &IID_IHTMLWindow2, (void**)&win2);
729             if(FAILED(hres))
730                 return E_INVALIDARG;
731 
732             handle_navigation_error(This, V_I4(&status_code), V_BSTR(&url), win2);
733             IHTMLWindow2_Release(win2);
734             return S_OK;
735         }
736 
737         default:
738             TRACE("unsupported command %d of CGID_DocHostCmdPriv\n", nCmdID);
739             return E_NOTIMPL;
740         }
741     }
742 
743     if(IsEqualGUID(pguidCmdGroup, &CGID_Explorer)) {
744         switch(nCmdID) {
745         case CMDID_EXPLORER_UPDATEHISTORY:
746             update_travellog(This);
747             update_navigation_commands(This);
748             return S_OK;
749 
750         default:
751             TRACE("Unimplemented cmdid %d of CGID_Explorer\n", nCmdID);
752             return E_NOTIMPL;
753         }
754     }
755 
756     if(IsEqualGUID(pguidCmdGroup, &CGID_ShellDocView)) {
757         switch(nCmdID) {
758         default:
759             TRACE("Unimplemented cmdid %d of CGID_ShellDocView\n", nCmdID);
760             return E_NOTIMPL;
761         }
762     }
763 
764     if(IsEqualGUID(&CGID_DocHostCommandHandler, pguidCmdGroup)) {
765         if(!This->olecmd)
766             return E_NOTIMPL;
767         return IOleCommandTarget_Exec(This->olecmd, pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
768     }
769 
770     TRACE("Unimplemented cmdid %d of group %s\n", nCmdID, debugstr_guid(pguidCmdGroup));
771     return E_NOTIMPL;
772 }
773 
774 static const IOleCommandTargetVtbl OleCommandTargetVtbl = {
775     ClOleCommandTarget_QueryInterface,
776     ClOleCommandTarget_AddRef,
777     ClOleCommandTarget_Release,
778     ClOleCommandTarget_QueryStatus,
779     ClOleCommandTarget_Exec
780 };
781 
782 static inline DocHost *impl_from_IDocHostUIHandler2(IDocHostUIHandler2 *iface)
783 {
784     return CONTAINING_RECORD(iface, DocHost, IDocHostUIHandler2_iface);
785 }
786 
787 static HRESULT WINAPI DocHostUIHandler_QueryInterface(IDocHostUIHandler2 *iface,
788                                                       REFIID riid, void **ppv)
789 {
790     DocHost *This = impl_from_IDocHostUIHandler2(iface);
791     return IOleClientSite_QueryInterface(&This->IOleClientSite_iface, riid, ppv);
792 }
793 
794 static ULONG WINAPI DocHostUIHandler_AddRef(IDocHostUIHandler2 *iface)
795 {
796     DocHost *This = impl_from_IDocHostUIHandler2(iface);
797     return IOleClientSite_AddRef(&This->IOleClientSite_iface);
798 }
799 
800 static ULONG WINAPI DocHostUIHandler_Release(IDocHostUIHandler2 *iface)
801 {
802     DocHost *This = impl_from_IDocHostUIHandler2(iface);
803     return IOleClientSite_Release(&This->IOleClientSite_iface);
804 }
805 
806 static HRESULT WINAPI DocHostUIHandler_ShowContextMenu(IDocHostUIHandler2 *iface,
807          DWORD dwID, POINT *ppt, IUnknown *pcmdtReserved, IDispatch *pdispReserved)
808 {
809     DocHost *This = impl_from_IDocHostUIHandler2(iface);
810     HRESULT hres;
811 
812     TRACE("(%p)->(%d %p %p %p)\n", This, dwID, ppt, pcmdtReserved, pdispReserved);
813 
814     if(This->hostui) {
815         hres = IDocHostUIHandler_ShowContextMenu(This->hostui, dwID, ppt, pcmdtReserved,
816                                                  pdispReserved);
817         if(hres == S_OK)
818             return S_OK;
819     }
820 
821     FIXME("default action not implemented\n");
822     return E_NOTIMPL;
823 }
824 
825 static HRESULT WINAPI DocHostUIHandler_GetHostInfo(IDocHostUIHandler2 *iface,
826         DOCHOSTUIINFO *pInfo)
827 {
828     DocHost *This = impl_from_IDocHostUIHandler2(iface);
829     HRESULT hres;
830 
831     TRACE("(%p)->(%p)\n", This, pInfo);
832 
833     if(This->hostui) {
834         hres = IDocHostUIHandler_GetHostInfo(This->hostui, pInfo);
835         if(SUCCEEDED(hres))
836             return hres;
837     }
838 
839     pInfo->dwFlags = DOCHOSTUIFLAG_DISABLE_HELP_MENU | DOCHOSTUIFLAG_OPENNEWWIN
840         | DOCHOSTUIFLAG_URL_ENCODING_ENABLE_UTF8 | DOCHOSTUIFLAG_ENABLE_INPLACE_NAVIGATION
841         | DOCHOSTUIFLAG_IME_ENABLE_RECONVERSION;
842     return S_OK;
843 }
844 
845 static HRESULT WINAPI DocHostUIHandler_ShowUI(IDocHostUIHandler2 *iface, DWORD dwID,
846         IOleInPlaceActiveObject *pActiveObject, IOleCommandTarget *pCommandTarget,
847         IOleInPlaceFrame *pFrame, IOleInPlaceUIWindow *pDoc)
848 {
849     DocHost *This = impl_from_IDocHostUIHandler2(iface);
850     FIXME("(%p)->(%d %p %p %p %p)\n", This, dwID, pActiveObject, pCommandTarget,
851           pFrame, pDoc);
852     return E_NOTIMPL;
853 }
854 
855 static HRESULT WINAPI DocHostUIHandler_HideUI(IDocHostUIHandler2 *iface)
856 {
857     DocHost *This = impl_from_IDocHostUIHandler2(iface);
858     FIXME("(%p)\n", This);
859     return E_NOTIMPL;
860 }
861 
862 static HRESULT WINAPI DocHostUIHandler_UpdateUI(IDocHostUIHandler2 *iface)
863 {
864     DocHost *This = impl_from_IDocHostUIHandler2(iface);
865 
866     TRACE("(%p)\n", This);
867 
868     if(!This->hostui)
869         return S_FALSE;
870 
871     return IDocHostUIHandler_UpdateUI(This->hostui);
872 }
873 
874 static HRESULT WINAPI DocHostUIHandler_EnableModeless(IDocHostUIHandler2 *iface,
875                                                       BOOL fEnable)
876 {
877     DocHost *This = impl_from_IDocHostUIHandler2(iface);
878     FIXME("(%p)->(%x)\n", This, fEnable);
879     return E_NOTIMPL;
880 }
881 
882 static HRESULT WINAPI DocHostUIHandler_OnDocWindowActivate(IDocHostUIHandler2 *iface,
883                                                            BOOL fActivate)
884 {
885     DocHost *This = impl_from_IDocHostUIHandler2(iface);
886     FIXME("(%p)->(%x)\n", This, fActivate);
887     return E_NOTIMPL;
888 }
889 
890 static HRESULT WINAPI DocHostUIHandler_OnFrameWindowActivate(IDocHostUIHandler2 *iface,
891                                                              BOOL fActivate)
892 {
893     DocHost *This = impl_from_IDocHostUIHandler2(iface);
894     FIXME("(%p)->(%x)\n", This, fActivate);
895     return E_NOTIMPL;
896 }
897 
898 static HRESULT WINAPI DocHostUIHandler_ResizeBorder(IDocHostUIHandler2 *iface,
899         LPCRECT prcBorder, IOleInPlaceUIWindow *pUIWindow, BOOL fRameWindow)
900 {
901     DocHost *This = impl_from_IDocHostUIHandler2(iface);
902     FIXME("(%p)->(%p %p %X)\n", This, prcBorder, pUIWindow, fRameWindow);
903     return E_NOTIMPL;
904 }
905 
906 static HRESULT WINAPI DocHostUIHandler_TranslateAccelerator(IDocHostUIHandler2 *iface,
907         LPMSG lpMsg, const GUID *pguidCmdGroup, DWORD nCmdID)
908 {
909     DocHost *This = impl_from_IDocHostUIHandler2(iface);
910     HRESULT hr = S_FALSE;
911     TRACE("(%p)->(%p %p %d)\n", This, lpMsg, pguidCmdGroup, nCmdID);
912 
913     if(This->hostui)
914         hr = IDocHostUIHandler_TranslateAccelerator(This->hostui, lpMsg, pguidCmdGroup, nCmdID);
915 
916     return hr;
917 }
918 
919 static HRESULT WINAPI DocHostUIHandler_GetOptionKeyPath(IDocHostUIHandler2 *iface,
920         LPOLESTR *pchKey, DWORD dw)
921 {
922     DocHost *This = impl_from_IDocHostUIHandler2(iface);
923 
924     TRACE("(%p)->(%p %d)\n", This, pchKey, dw);
925 
926     if(This->hostui)
927         return IDocHostUIHandler_GetOptionKeyPath(This->hostui, pchKey, dw);
928 
929     return S_OK;
930 }
931 
932 static HRESULT WINAPI DocHostUIHandler_GetDropTarget(IDocHostUIHandler2 *iface,
933         IDropTarget *pDropTarget, IDropTarget **ppDropTarget)
934 {
935     DocHost *This = impl_from_IDocHostUIHandler2(iface);
936     FIXME("(%p)\n", This);
937     return E_NOTIMPL;
938 }
939 
940 static HRESULT WINAPI DocHostUIHandler_GetExternal(IDocHostUIHandler2 *iface,
941         IDispatch **ppDispatch)
942 {
943     DocHost *This = impl_from_IDocHostUIHandler2(iface);
944 
945     TRACE("(%p)->(%p)\n", This, ppDispatch);
946 
947     if(This->hostui)
948         return IDocHostUIHandler_GetExternal(This->hostui, ppDispatch);
949 
950     if(!This->shell_ui_helper) {
951         HRESULT hres;
952 
953         hres = create_shell_ui_helper(&This->shell_ui_helper);
954         if(FAILED(hres))
955             return hres;
956     }
957 
958     *ppDispatch = (IDispatch*)This->shell_ui_helper;
959     IDispatch_AddRef(*ppDispatch);
960     return S_OK;
961 }
962 
963 static HRESULT WINAPI DocHostUIHandler_TranslateUrl(IDocHostUIHandler2 *iface,
964         DWORD dwTranslate, OLECHAR *pchURLIn, OLECHAR **ppchURLOut)
965 {
966     DocHost *This = impl_from_IDocHostUIHandler2(iface);
967 
968     TRACE("(%p)->(%d %s %p)\n", This, dwTranslate, debugstr_w(pchURLIn), ppchURLOut);
969 
970     if(This->hostui)
971         return IDocHostUIHandler_TranslateUrl(This->hostui, dwTranslate,
972                                               pchURLIn, ppchURLOut);
973 
974     return S_FALSE;
975 }
976 
977 static HRESULT WINAPI DocHostUIHandler_FilterDataObject(IDocHostUIHandler2 *iface,
978         IDataObject *pDO, IDataObject **ppDORet)
979 {
980     DocHost *This = impl_from_IDocHostUIHandler2(iface);
981     FIXME("(%p)->(%p %p)\n", This, pDO, ppDORet);
982     return E_NOTIMPL;
983 }
984 
985 static HRESULT WINAPI DocHostUIHandler_GetOverrideKeyPath(IDocHostUIHandler2 *iface,
986         LPOLESTR *pchKey, DWORD dw)
987 {
988     DocHost *This = impl_from_IDocHostUIHandler2(iface);
989     IDocHostUIHandler2 *handler;
990     HRESULT hres;
991 
992     TRACE("(%p)->(%p %d)\n", This, pchKey, dw);
993 
994     if(!This->hostui)
995         return S_OK;
996 
997     hres = IDocHostUIHandler_QueryInterface(This->hostui, &IID_IDocHostUIHandler2,
998                                             (void**)&handler);
999     if(SUCCEEDED(hres)) {
1000         hres = IDocHostUIHandler2_GetOverrideKeyPath(handler, pchKey, dw);
1001         IDocHostUIHandler2_Release(handler);
1002         return hres;
1003     }
1004 
1005     return S_OK;
1006 }
1007 
1008 static const IDocHostUIHandler2Vtbl DocHostUIHandler2Vtbl = {
1009     DocHostUIHandler_QueryInterface,
1010     DocHostUIHandler_AddRef,
1011     DocHostUIHandler_Release,
1012     DocHostUIHandler_ShowContextMenu,
1013     DocHostUIHandler_GetHostInfo,
1014     DocHostUIHandler_ShowUI,
1015     DocHostUIHandler_HideUI,
1016     DocHostUIHandler_UpdateUI,
1017     DocHostUIHandler_EnableModeless,
1018     DocHostUIHandler_OnDocWindowActivate,
1019     DocHostUIHandler_OnFrameWindowActivate,
1020     DocHostUIHandler_ResizeBorder,
1021     DocHostUIHandler_TranslateAccelerator,
1022     DocHostUIHandler_GetOptionKeyPath,
1023     DocHostUIHandler_GetDropTarget,
1024     DocHostUIHandler_GetExternal,
1025     DocHostUIHandler_TranslateUrl,
1026     DocHostUIHandler_FilterDataObject,
1027     DocHostUIHandler_GetOverrideKeyPath
1028 };
1029 
1030 static inline DocHost *impl_from_IPropertyNotifySink(IPropertyNotifySink *iface)
1031 {
1032     return CONTAINING_RECORD(iface, DocHost, IPropertyNotifySink_iface);
1033 }
1034 
1035 static HRESULT WINAPI PropertyNotifySink_QueryInterface(IPropertyNotifySink *iface,
1036         REFIID riid, void **ppv)
1037 {
1038     DocHost *This = impl_from_IPropertyNotifySink(iface);
1039     return IOleClientSite_QueryInterface(&This->IOleClientSite_iface, riid, ppv);
1040 }
1041 
1042 static ULONG WINAPI PropertyNotifySink_AddRef(IPropertyNotifySink *iface)
1043 {
1044     DocHost *This = impl_from_IPropertyNotifySink(iface);
1045     return IOleClientSite_AddRef(&This->IOleClientSite_iface);
1046 }
1047 
1048 static ULONG WINAPI PropertyNotifySink_Release(IPropertyNotifySink *iface)
1049 {
1050     DocHost *This = impl_from_IPropertyNotifySink(iface);
1051     return IOleClientSite_Release(&This->IOleClientSite_iface);
1052 }
1053 
1054 static HRESULT WINAPI PropertyNotifySink_OnChanged(IPropertyNotifySink *iface, DISPID dispID)
1055 {
1056     DocHost *This = impl_from_IPropertyNotifySink(iface);
1057 
1058     TRACE("(%p)->(%d)\n", This, dispID);
1059 
1060     switch(dispID) {
1061     case DISPID_READYSTATE: {
1062         READYSTATE ready_state;
1063         HRESULT hres;
1064 
1065         hres = get_doc_ready_state(This, &ready_state);
1066         if(FAILED(hres))
1067             return hres;
1068 
1069         if(ready_state == READYSTATE_COMPLETE && !This->doc_navigate)
1070             advise_prop_notif(This, FALSE);
1071 
1072         update_ready_state(This, ready_state);
1073         break;
1074     }
1075     default:
1076         FIXME("unimplemented dispid %d\n", dispID);
1077         return E_NOTIMPL;
1078     }
1079 
1080     return S_OK;
1081 }
1082 
1083 static HRESULT WINAPI PropertyNotifySink_OnRequestEdit(IPropertyNotifySink *iface, DISPID dispID)
1084 {
1085     DocHost *This = impl_from_IPropertyNotifySink(iface);
1086     FIXME("(%p)->(%d)\n", This, dispID);
1087     return E_NOTIMPL;
1088 }
1089 
1090 static const IPropertyNotifySinkVtbl PropertyNotifySinkVtbl = {
1091     PropertyNotifySink_QueryInterface,
1092     PropertyNotifySink_AddRef,
1093     PropertyNotifySink_Release,
1094     PropertyNotifySink_OnChanged,
1095     PropertyNotifySink_OnRequestEdit
1096 };
1097 
1098 void DocHost_Init(DocHost *This, IWebBrowser2 *wb, const IDocHostContainerVtbl* container)
1099 {
1100     This->IDocHostUIHandler2_iface.lpVtbl  = &DocHostUIHandler2Vtbl;
1101     This->IOleCommandTarget_iface.lpVtbl   = &OleCommandTargetVtbl;
1102     This->IPropertyNotifySink_iface.lpVtbl = &PropertyNotifySinkVtbl;
1103 
1104     This->wb = wb;
1105     This->container_vtbl = container;
1106 
1107     This->ready_state = READYSTATE_UNINITIALIZED;
1108     list_init(&This->task_queue);
1109 
1110     This->travellog.loading_pos = -1;
1111 
1112     DocHost_ClientSite_Init(This);
1113     DocHost_Frame_Init(This);
1114 
1115     ConnectionPointContainer_Init(&This->cps, (IUnknown*)wb);
1116     IEHTMLWindow_Init(This);
1117     NewWindowManager_Init(This);
1118 }
1119 
1120 void DocHost_Release(DocHost *This)
1121 {
1122     if(This->shell_ui_helper)
1123         IShellUIHelper2_Release(This->shell_ui_helper);
1124 
1125     abort_dochost_tasks(This, NULL);
1126     release_dochost_client(This);
1127     DocHost_ClientSite_Release(This);
1128 
1129     ConnectionPointContainer_Destroy(&This->cps);
1130 
1131     while(This->travellog.length)
1132         free_travellog_entry(This->travellog.log + --This->travellog.length);
1133     heap_free(This->travellog.log);
1134 
1135     heap_free(This->url);
1136 }
1137