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