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