shdocvw: Separate document state from control state.
[wine] / dlls / shdocvw / navigate.c
1 /*
2  * Copyright 2006-2007 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 #define NONAMELESSUNION
20 #define NONAMELESSSTRUCT
21
22 #include "wine/debug.h"
23
24 #include "shdocvw.h"
25 #include "exdispid.h"
26 #include "shellapi.h"
27 #include "winreg.h"
28 #include "shlwapi.h"
29 #include "wininet.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(shdocvw);
32
33 static const WCHAR emptyW[] = {0};
34
35 typedef struct {
36     const IBindStatusCallbackVtbl  *lpBindStatusCallbackVtbl;
37     const IHttpNegotiateVtbl       *lpHttpNegotiateVtbl;
38
39     LONG ref;
40
41     DocHost *doc_host;
42
43     LPWSTR url;
44     HGLOBAL post_data;
45     BSTR headers;
46     ULONG post_data_len;
47 } BindStatusCallback;
48
49 #define BINDSC(x)  ((IBindStatusCallback*) &(x)->lpBindStatusCallbackVtbl)
50 #define HTTPNEG(x) ((IHttpNegotiate*)      &(x)->lpHttpNegotiateVtbl)
51
52 static void dump_BINDINFO(BINDINFO *bi)
53 {
54     static const char * const BINDINFOF_str[] = {
55         "#0",
56         "BINDINFOF_URLENCODESTGMEDDATA",
57         "BINDINFOF_URLENCODEDEXTRAINFO"
58     };
59
60     static const char * const BINDVERB_str[] = {
61         "BINDVERB_GET",
62         "BINDVERB_POST",
63         "BINDVERB_PUT",
64         "BINDVERB_CUSTOM"
65     };
66
67     TRACE("\n"
68             "BINDINFO = {\n"
69             "    %d, %s,\n"
70             "    {%d, %p, %p},\n"
71             "    %s,\n"
72             "    %s,\n"
73             "    %s,\n"
74             "    %d, %08x, %d, %d\n"
75             "    {%d %p %x},\n"
76             "    %s\n"
77             "    %p, %d\n"
78             "}\n",
79
80             bi->cbSize, debugstr_w(bi->szExtraInfo),
81             bi->stgmedData.tymed, bi->stgmedData.u.hGlobal, bi->stgmedData.pUnkForRelease,
82             bi->grfBindInfoF > BINDINFOF_URLENCODEDEXTRAINFO
83                 ? "unknown" : BINDINFOF_str[bi->grfBindInfoF],
84             bi->dwBindVerb > BINDVERB_CUSTOM
85                 ? "unknown" : BINDVERB_str[bi->dwBindVerb],
86             debugstr_w(bi->szCustomVerb),
87             bi->cbstgmedData, bi->dwOptions, bi->dwOptionsFlags, bi->dwCodePage,
88             bi->securityAttributes.nLength,
89             bi->securityAttributes.lpSecurityDescriptor,
90             bi->securityAttributes.bInheritHandle,
91             debugstr_guid(&bi->iid),
92             bi->pUnk, bi->dwReserved
93             );
94 }
95
96 static void set_status_text(BindStatusCallback *This, LPCWSTR str)
97 {
98     VARIANTARG arg;
99     DISPPARAMS dispparams = {&arg, NULL, 1, 0};
100
101     if(!This->doc_host)
102         return;
103
104     V_VT(&arg) = VT_BSTR;
105     V_BSTR(&arg) = str ? SysAllocString(str) : NULL;
106     call_sink(This->doc_host->cps.wbe2, DISPID_STATUSTEXTCHANGE, &dispparams);
107     VariantClear(&arg);
108
109     if(This->doc_host->frame)
110         IOleInPlaceFrame_SetStatusText(This->doc_host->frame, str);
111 }
112
113 #define BINDSC_THIS(iface) DEFINE_THIS(BindStatusCallback, BindStatusCallback, iface)
114
115 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
116                                                         REFIID riid, void **ppv)
117 {
118     BindStatusCallback *This = BINDSC_THIS(iface);
119
120     *ppv = NULL;
121
122     if(IsEqualGUID(&IID_IUnknown, riid)) {
123         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
124         *ppv = BINDSC(This);
125     }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
126         TRACE("(%p)->(IID_IBindStatusCallback %p)\n", This, ppv);
127         *ppv = BINDSC(This);
128     }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
129         TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
130         *ppv = HTTPNEG(This);
131     }
132
133     if(*ppv) {
134         IBindStatusCallback_AddRef(BINDSC(This));
135         return S_OK;
136     }
137
138     WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
139     return E_NOINTERFACE;
140 }
141
142 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
143 {
144     BindStatusCallback *This = BINDSC_THIS(iface);
145     LONG ref = InterlockedIncrement(&This->ref);
146
147     TRACE("(%p) ref=%d\n", This, ref);
148
149     return ref;
150 }
151
152 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
153 {
154     BindStatusCallback *This = BINDSC_THIS(iface);
155     LONG ref = InterlockedDecrement(&This->ref);
156
157     TRACE("(%p) ref=%d\n", This, ref);
158
159     if(!ref) {
160         if(This->doc_host)
161             IOleClientSite_Release(CLIENTSITE(This->doc_host));
162         if(This->post_data)
163             GlobalFree(This->post_data);
164         SysFreeString(This->headers);
165         heap_free(This->url);
166         heap_free(This);
167     }
168
169     return ref;
170 }
171
172 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
173        DWORD dwReserved, IBinding *pbind)
174 {
175     BindStatusCallback *This = BINDSC_THIS(iface);
176
177     TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
178
179     return S_OK;
180 }
181
182 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface,
183        LONG *pnPriority)
184 {
185     BindStatusCallback *This = BINDSC_THIS(iface);
186     FIXME("(%p)->(%p)\n", This, pnPriority);
187     return E_NOTIMPL;
188 }
189
190 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface,
191        DWORD reserved)
192 {
193     BindStatusCallback *This = BINDSC_THIS(iface);
194     FIXME("(%p)->(%d)\n", This, reserved);
195     return E_NOTIMPL;
196 }
197
198 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface,
199         ULONG ulProgress, ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
200 {
201     BindStatusCallback *This = BINDSC_THIS(iface);
202
203     TRACE("(%p)->(%d %d %d %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
204           debugstr_w(szStatusText));
205
206     switch(ulStatusCode) {
207     case BINDSTATUS_BEGINDOWNLOADDATA:
208         set_status_text(This, szStatusText); /* FIXME: "Start downloading from site: %s" */
209         return S_OK;
210     case BINDSTATUS_ENDDOWNLOADDATA:
211         set_status_text(This, szStatusText); /* FIXME: "Downloading from site: %s" */
212         return S_OK;
213     case BINDSTATUS_CLASSIDAVAILABLE:
214     case BINDSTATUS_MIMETYPEAVAILABLE:
215     case BINDSTATUS_BEGINSYNCOPERATION:
216     case BINDSTATUS_ENDSYNCOPERATION:
217         return S_OK;
218     default:
219         FIXME("status code %u\n", ulStatusCode);
220     }
221
222     return S_OK;
223 }
224
225 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
226         HRESULT hresult, LPCWSTR szError)
227 {
228     BindStatusCallback *This = BINDSC_THIS(iface);
229
230     TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
231
232     set_status_text(This, emptyW);
233
234     if(This->doc_host) {
235         IOleClientSite_Release(CLIENTSITE(This->doc_host));
236         This->doc_host = NULL;
237     }
238
239     return S_OK;
240 }
241
242 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
243         DWORD *grfBINDF, BINDINFO *pbindinfo)
244 {
245     BindStatusCallback *This = BINDSC_THIS(iface);
246
247     TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
248
249     *grfBINDF = BINDF_ASYNCHRONOUS;
250
251     if(This->post_data) {
252         pbindinfo->dwBindVerb = BINDVERB_POST;
253
254         pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
255         pbindinfo->stgmedData.u.hGlobal = This->post_data;
256         pbindinfo->cbstgmedData = This->post_data_len;
257         pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)BINDSC(This);
258         IBindStatusCallback_AddRef(BINDSC(This));
259     }
260
261     return S_OK;
262 }
263
264 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
265         DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
266 {
267     BindStatusCallback *This = BINDSC_THIS(iface);
268     FIXME("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
269     return E_NOTIMPL;
270 }
271
272 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
273         REFIID riid, IUnknown *punk)
274 {
275     BindStatusCallback *This = BINDSC_THIS(iface);
276
277     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
278
279     return dochost_object_available(This->doc_host, punk);
280 }
281
282 #undef BSC_THIS
283
284 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
285     BindStatusCallback_QueryInterface,
286     BindStatusCallback_AddRef,
287     BindStatusCallback_Release,
288     BindStatusCallback_OnStartBinding,
289     BindStatusCallback_GetPriority,
290     BindStatusCallback_OnLowResource,
291     BindStatusCallback_OnProgress,
292     BindStatusCallback_OnStopBinding,
293     BindStatusCallback_GetBindInfo,
294     BindStatusCallback_OnDataAvailable,
295     BindStatusCallback_OnObjectAvailable
296 };
297
298 #define HTTPNEG_THIS(iface) DEFINE_THIS(BindStatusCallback, HttpNegotiate, iface)
299
300 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate *iface,
301                                                    REFIID riid, void **ppv)
302 {
303     BindStatusCallback *This = HTTPNEG_THIS(iface);
304     return IBindStatusCallback_QueryInterface(BINDSC(This), riid, ppv);
305 }
306
307 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate *iface)
308 {
309     BindStatusCallback *This = HTTPNEG_THIS(iface);
310     return IBindStatusCallback_AddRef(BINDSC(This));
311 }
312
313 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate *iface)
314 {
315     BindStatusCallback *This = HTTPNEG_THIS(iface);
316     return IBindStatusCallback_Release(BINDSC(This));
317 }
318
319 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate *iface,
320         LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
321 {
322     BindStatusCallback *This = HTTPNEG_THIS(iface);
323
324     TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
325           dwReserved, pszAdditionalHeaders);
326
327     if(This->headers) {
328         int size = (strlenW(This->headers)+1)*sizeof(WCHAR);
329         *pszAdditionalHeaders = CoTaskMemAlloc(size);
330         memcpy(*pszAdditionalHeaders, This->headers, size);
331     }
332
333     return S_OK;
334 }
335
336 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate *iface,
337         DWORD dwResponseCode, LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders,
338         LPWSTR *pszAdditionalRequestHeaders)
339 {
340     BindStatusCallback *This = HTTPNEG_THIS(iface);
341     TRACE("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
342           debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
343     return S_OK;
344 }
345
346 #undef HTTPNEG_THIS
347
348 static const IHttpNegotiateVtbl HttpNegotiateVtbl = {
349     HttpNegotiate_QueryInterface,
350     HttpNegotiate_AddRef,
351     HttpNegotiate_Release,
352     HttpNegotiate_BeginningTransaction,
353     HttpNegotiate_OnResponse
354 };
355
356 static BindStatusCallback *create_callback(DocHost *doc_host, LPCWSTR url, PBYTE post_data,
357         ULONG post_data_len, LPCWSTR headers)
358 {
359     BindStatusCallback *ret = heap_alloc(sizeof(BindStatusCallback));
360
361     ret->lpBindStatusCallbackVtbl = &BindStatusCallbackVtbl;
362     ret->lpHttpNegotiateVtbl      = &HttpNegotiateVtbl;
363
364     ret->ref = 1;
365     ret->url = heap_strdupW(url);
366     ret->post_data = NULL;
367     ret->post_data_len = post_data_len;
368     ret->headers = headers ? SysAllocString(headers) : NULL;
369
370     ret->doc_host = doc_host;
371     IOleClientSite_AddRef(CLIENTSITE(doc_host));
372
373     if(post_data) {
374         ret->post_data = GlobalAlloc(0, post_data_len);
375         memcpy(ret->post_data, post_data, post_data_len);
376     }
377
378     return ret;
379 }
380
381 static void on_before_navigate2(DocHost *This, LPCWSTR url, const BYTE *post_data,
382                                 ULONG post_data_len, LPWSTR headers, VARIANT_BOOL *cancel)
383 {
384     VARIANT var_url, var_flags, var_frame_name, var_post_data, var_post_data2, var_headers;
385     DISPPARAMS dispparams;
386     VARIANTARG params[7];
387
388     dispparams.cArgs = 7;
389     dispparams.cNamedArgs = 0;
390     dispparams.rgdispidNamedArgs = NULL;
391     dispparams.rgvarg = params;
392
393     This->busy = VARIANT_TRUE;
394
395     V_VT(params) = VT_BOOL|VT_BYREF;
396     V_BOOLREF(params) = cancel;
397
398     V_VT(params+1) = (VT_BYREF|VT_VARIANT);
399     V_VARIANTREF(params+1) = &var_headers;
400     V_VT(&var_headers) = VT_BSTR;
401     V_BSTR(&var_headers) = headers;
402
403     V_VT(params+2) = (VT_BYREF|VT_VARIANT);
404     V_VARIANTREF(params+2) = &var_post_data2;
405     V_VT(&var_post_data2) = (VT_BYREF|VT_VARIANT);
406     V_VARIANTREF(&var_post_data2) = &var_post_data;
407     VariantInit(&var_post_data);
408
409     if(post_data_len) {
410         SAFEARRAYBOUND bound = {post_data_len, 0};
411         void *data;
412
413         V_VT(&var_post_data) = VT_UI1|VT_ARRAY;
414         V_ARRAY(&var_post_data) = SafeArrayCreate(VT_UI1, 1, &bound);
415
416         SafeArrayAccessData(V_ARRAY(&var_post_data), &data);
417         memcpy(data, post_data, post_data_len);
418         SafeArrayUnaccessData(V_ARRAY(&var_post_data));
419     }
420
421     V_VT(params+3) = (VT_BYREF|VT_VARIANT);
422     V_VARIANTREF(params+3) = &var_frame_name;
423     V_VT(&var_frame_name) = VT_BSTR;
424     V_BSTR(&var_frame_name) = NULL;
425
426     V_VT(params+4) = (VT_BYREF|VT_VARIANT);
427     V_VARIANTREF(params+4) = &var_flags;
428     V_VT(&var_flags) = VT_I4;
429     V_I4(&var_flags) = 0;
430
431     V_VT(params+5) = (VT_BYREF|VT_VARIANT);
432     V_VARIANTREF(params+5) = &var_url;
433     V_VT(&var_url) = VT_BSTR;
434     V_BSTR(&var_url) = SysAllocString(url);
435
436     V_VT(params+6) = (VT_DISPATCH);
437     V_DISPATCH(params+6) = This->disp;
438
439     call_sink(This->cps.wbe2, DISPID_BEFORENAVIGATE2, &dispparams);
440
441     SysFreeString(V_BSTR(&var_url));
442     if(post_data_len)
443         SafeArrayDestroy(V_ARRAY(&var_post_data));
444 }
445
446 /* FIXME: urlmon should handle it */
447 static BOOL try_application_url(LPCWSTR url)
448 {
449     SHELLEXECUTEINFOW exec_info;
450     WCHAR app[64];
451     HKEY hkey;
452     DWORD res, type;
453     HRESULT hres;
454
455     static const WCHAR wszURLProtocol[] = {'U','R','L',' ','P','r','o','t','o','c','o','l',0};
456
457     hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, app, sizeof(app)/sizeof(WCHAR), NULL, 0);
458     if(FAILED(hres))
459         return FALSE;
460
461     res = RegOpenKeyW(HKEY_CLASSES_ROOT, app, &hkey);
462     if(res != ERROR_SUCCESS)
463         return FALSE;
464
465     res = RegQueryValueExW(hkey, wszURLProtocol, NULL, &type, NULL, NULL);
466     RegCloseKey(hkey);
467     if(res != ERROR_SUCCESS || type != REG_SZ)
468         return FALSE;
469
470     TRACE("openning application %s\n", debugstr_w(app));
471  
472     memset(&exec_info, 0, sizeof(exec_info));
473     exec_info.cbSize = sizeof(exec_info);
474     exec_info.lpFile = url;
475     exec_info.nShow = SW_SHOW;
476
477     return ShellExecuteExW(&exec_info);
478 }
479
480 static HRESULT create_moniker(LPCWSTR url, IMoniker **mon)
481 {
482     WCHAR new_url[INTERNET_MAX_URL_LENGTH];
483     DWORD size;
484     HRESULT hres;
485
486     if(PathIsURLW(url))
487         return CreateURLMoniker(NULL, url, mon);
488
489     if(url[1] == ':') {
490         size = sizeof(new_url);
491         hres = UrlCreateFromPathW(url, new_url, &size, 0);
492         if(FAILED(hres)) {
493             WARN("UrlCreateFromPathW failed: %08x\n", hres);
494             return hres;
495         }
496     }else {
497         size = sizeof(new_url);
498         hres = UrlApplySchemeW(url, new_url, &size, URL_APPLY_GUESSSCHEME);
499         TRACE("got %s\n", debugstr_w(new_url));
500         if(FAILED(hres)) {
501             WARN("UrlApplyScheme failed: %08x\n", hres);
502             return hres;
503         }
504     }
505
506     return CreateURLMoniker(NULL, new_url, mon);
507 }
508
509 static HRESULT bind_to_object(DocHost *This, IMoniker *mon, LPCWSTR url, IBindCtx *bindctx,
510                               IBindStatusCallback *callback)
511 {
512     IUnknown *unk = NULL;
513     HRESULT hres;
514
515     if(mon) {
516         IMoniker_AddRef(mon);
517     }else {
518         hres = create_moniker(url, &mon);
519         if(FAILED(hres))
520             return hres;
521     }
522
523     CoTaskMemFree(This->url);
524     hres = IMoniker_GetDisplayName(mon, 0, NULL, &This->url);
525     if(FAILED(hres))
526         FIXME("GetDisplayName failed: %08x\n", hres);
527
528     IBindCtx_RegisterObjectParam(bindctx, (LPOLESTR)SZ_HTML_CLIENTSITE_OBJECTPARAM,
529                                  (IUnknown*)CLIENTSITE(This));
530
531     hres = IMoniker_BindToObject(mon, bindctx, NULL, &IID_IUnknown, (void**)&unk);
532     if(SUCCEEDED(hres)) {
533         hres = S_OK;
534         if(unk)
535             IUnknown_Release(unk);
536     }else if(try_application_url(url)) {
537         hres = S_OK;
538     }else {
539         FIXME("BindToObject failed: %08x\n", hres);
540     }
541
542     IMoniker_Release(mon);
543     return S_OK;
544 }
545
546 static HRESULT navigate_bsc(DocHost *This, BindStatusCallback *bsc, IMoniker *mon)
547 {
548     IBindCtx *bindctx;
549     VARIANT_BOOL cancel = VARIANT_FALSE;
550     HRESULT hres;
551
552     set_doc_state(This, READYSTATE_LOADING);
553
554     on_before_navigate2(This, bsc->url, bsc->post_data, bsc->post_data_len, bsc->headers, &cancel);
555     if(cancel) {
556         FIXME("Navigation canceled\n");
557         return S_OK;
558     }
559
560     if(This->document)
561         deactivate_document(This);
562
563     CreateAsyncBindCtx(0, BINDSC(bsc), 0, &bindctx);
564
565     if(This->frame)
566         IOleInPlaceFrame_EnableModeless(This->frame, FALSE);
567
568     hres = bind_to_object(This, mon, bsc->url, bindctx, BINDSC(bsc));
569
570     if(This->frame)
571         IOleInPlaceFrame_EnableModeless(This->frame, TRUE);
572
573     IBindCtx_Release(bindctx);
574
575     return hres;
576 }
577
578 typedef struct {
579     task_header_t header;
580     BindStatusCallback *bsc;
581 } task_navigate_bsc_t;
582
583 static void navigate_bsc_proc(DocHost *This, task_header_t *t)
584 {
585     task_navigate_bsc_t *task = (task_navigate_bsc_t*)t;
586
587     if(!This->hwnd)
588         create_doc_view_hwnd(This);
589
590     navigate_bsc(This, task->bsc, NULL);
591
592     IBindStatusCallback_Release(BINDSC(task->bsc));
593 }
594
595
596 HRESULT navigate_url(DocHost *This, LPCWSTR url, const VARIANT *Flags,
597                      const VARIANT *TargetFrameName, VARIANT *PostData, VARIANT *Headers)
598 {
599     task_navigate_bsc_t *task;
600     PBYTE post_data = NULL;
601     ULONG post_data_len = 0;
602     LPWSTR headers = NULL;
603
604     TRACE("navigating to %s\n", debugstr_w(url));
605
606     if((Flags && V_VT(Flags) != VT_EMPTY) 
607        || (TargetFrameName && V_VT(TargetFrameName) != VT_EMPTY))
608         FIXME("Unsupported args (Flags %p:%d; TargetFrameName %p:%d)\n",
609                 Flags, Flags ? V_VT(Flags) : -1, TargetFrameName,
610                 TargetFrameName ? V_VT(TargetFrameName) : -1);
611
612     if(PostData) {
613         TRACE("PostData vt=%d\n", V_VT(PostData));
614
615         if(V_VT(PostData) == (VT_ARRAY | VT_UI1)) {
616             SafeArrayAccessData(V_ARRAY(PostData), (void**)&post_data);
617             post_data_len = V_ARRAY(PostData)->rgsabound[0].cElements;
618         }
619     }
620
621     if(Headers && V_VT(Headers) != VT_EMPTY && V_VT(Headers) != VT_ERROR) {
622         if(V_VT(Headers) != VT_BSTR)
623             return E_INVALIDARG;
624
625         headers = V_BSTR(Headers);
626         TRACE("Headers: %s\n", debugstr_w(headers));
627     }
628
629     task = heap_alloc(sizeof(*task));
630     task->bsc = create_callback(This, url, post_data, post_data_len, headers);
631
632     if(post_data)
633         SafeArrayUnaccessData(V_ARRAY(PostData));
634
635     push_dochost_task(This, &task->header, navigate_bsc_proc, This->url == NULL);
636
637     return S_OK;
638 }
639
640 static HRESULT navigate_hlink(DocHost *This, IMoniker *mon, IBindCtx *bindctx,
641                               IBindStatusCallback *callback)
642 {
643     IHttpNegotiate *http_negotiate;
644     BindStatusCallback *bsc;
645     PBYTE post_data = NULL;
646     ULONG post_data_len = 0;
647     LPWSTR headers = NULL, url;
648     BINDINFO bindinfo;
649     DWORD bindf = 0;
650     HRESULT hres;
651
652     hres = IBindStatusCallback_QueryInterface(callback, &IID_IHttpNegotiate,
653                                               (void**)&http_negotiate);
654     if(SUCCEEDED(hres)) {
655         static const WCHAR null_string[] = {0};
656
657         IHttpNegotiate_BeginningTransaction(http_negotiate, null_string, null_string, 0,
658                                             &headers);
659         IHttpNegotiate_Release(http_negotiate);
660     }
661
662     memset(&bindinfo, 0, sizeof(bindinfo));
663     bindinfo.cbSize = sizeof(bindinfo);
664
665     hres = IBindStatusCallback_GetBindInfo(callback, &bindf, &bindinfo);
666     dump_BINDINFO(&bindinfo);
667     if(bindinfo.dwBindVerb == BINDVERB_POST) {
668         post_data_len = bindinfo.cbstgmedData;
669         if(post_data_len)
670             post_data = bindinfo.stgmedData.u.hGlobal;
671     }
672
673     hres = IMoniker_GetDisplayName(mon, 0, NULL, &url);
674     if(FAILED(hres))
675         FIXME("GetDisplayName failed: %08x\n", hres);
676
677     bsc = create_callback(This, url, post_data, post_data_len, headers);
678     CoTaskMemFree(url);
679
680     hres = navigate_bsc(This, bsc, mon);
681
682     IBindStatusCallback_Release(BINDSC(bsc));
683     CoTaskMemFree(headers);
684     ReleaseBindInfo(&bindinfo);
685
686     return hres;
687 }
688
689 HRESULT go_home(DocHost *This)
690 {
691     HKEY hkey;
692     DWORD res, type, size;
693     WCHAR wszPageName[MAX_PATH];
694     static const WCHAR wszAboutBlank[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
695     static const WCHAR wszStartPage[] = {'S','t','a','r','t',' ','P','a','g','e',0};
696     static const WCHAR wszSubKey[] = {'S','o','f','t','w','a','r','e','\\',
697                                       'M','i','c','r','o','s','o','f','t','\\',
698                                       'I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r','\\',
699                                       'M','a','i','n',0};
700
701     res = RegOpenKeyW(HKEY_CURRENT_USER, wszSubKey, &hkey);
702     if (res != ERROR_SUCCESS)
703         return navigate_url(This, wszAboutBlank, NULL, NULL, NULL, NULL);
704
705     size = sizeof(wszPageName);
706     res = RegQueryValueExW(hkey, wszStartPage, NULL, &type, (LPBYTE)wszPageName, &size);
707     RegCloseKey(hkey);
708     if (res != ERROR_SUCCESS || type != REG_SZ)
709         return navigate_url(This, wszAboutBlank, NULL, NULL, NULL, NULL);
710
711     return navigate_url(This, wszPageName, NULL, NULL, NULL, NULL);
712 }
713
714 #define HLINKFRAME_THIS(iface) DEFINE_THIS(WebBrowser, HlinkFrame, iface)
715
716 static HRESULT WINAPI HlinkFrame_QueryInterface(IHlinkFrame *iface, REFIID riid, void **ppv)
717 {
718     WebBrowser *This = HLINKFRAME_THIS(iface);
719     return IWebBrowser2_QueryInterface(WEBBROWSER2(This), riid, ppv);
720 }
721
722 static ULONG WINAPI HlinkFrame_AddRef(IHlinkFrame *iface)
723 {
724     WebBrowser *This = HLINKFRAME_THIS(iface);
725     return IWebBrowser2_AddRef(WEBBROWSER2(This));
726 }
727
728 static ULONG WINAPI HlinkFrame_Release(IHlinkFrame *iface)
729 {
730     WebBrowser *This = HLINKFRAME_THIS(iface);
731     return IWebBrowser2_Release(WEBBROWSER2(This));
732 }
733
734 static HRESULT WINAPI HlinkFrame_SetBrowseContext(IHlinkFrame *iface,
735                                                   IHlinkBrowseContext *pihlbc)
736 {
737     WebBrowser *This = HLINKFRAME_THIS(iface);
738     FIXME("(%p)->(%p)\n", This, pihlbc);
739     return E_NOTIMPL;
740 }
741
742 static HRESULT WINAPI HlinkFrame_GetBrowseContext(IHlinkFrame *iface,
743                                                   IHlinkBrowseContext **ppihlbc)
744 {
745     WebBrowser *This = HLINKFRAME_THIS(iface);
746     FIXME("(%p)->(%p)\n", This, ppihlbc);
747     return E_NOTIMPL;
748 }
749
750 static HRESULT WINAPI HlinkFrame_Navigate(IHlinkFrame *iface, DWORD grfHLNF, LPBC pbc,
751                                           IBindStatusCallback *pibsc, IHlink *pihlNavigate)
752 {
753     WebBrowser *This = HLINKFRAME_THIS(iface);
754     IMoniker *mon;
755     LPWSTR location = NULL;
756
757     TRACE("(%p)->(%08x %p %p %p)\n", This, grfHLNF, pbc, pibsc, pihlNavigate);
758
759     if(grfHLNF)
760         FIXME("unsupported grfHLNF=%08x\n", grfHLNF);
761
762     /* Windows calls GetTargetFrameName here. */
763
764     IHlink_GetMonikerReference(pihlNavigate, 1, &mon, &location);
765
766     if(location) {
767         FIXME("location = %s\n", debugstr_w(location));
768         CoTaskMemFree(location);
769     }
770
771     /* Windows calls GetHlinkSite here */
772
773     if(grfHLNF & HLNF_OPENINNEWWINDOW) {
774         FIXME("Not supported HLNF_OPENINNEWWINDOW\n");
775         return E_NOTIMPL;
776     }
777
778     return navigate_hlink(&This->doc_host, mon, pbc, pibsc);
779 }
780
781 static HRESULT WINAPI HlinkFrame_OnNavigate(IHlinkFrame *iface, DWORD grfHLNF,
782         IMoniker *pimkTarget, LPCWSTR pwzLocation, LPCWSTR pwzFriendlyName, DWORD dwreserved)
783 {
784     WebBrowser *This = HLINKFRAME_THIS(iface);
785     FIXME("(%p)->(%08x %p %s %s %d)\n", This, grfHLNF, pimkTarget, debugstr_w(pwzLocation),
786           debugstr_w(pwzFriendlyName), dwreserved);
787     return E_NOTIMPL;
788 }
789
790 static HRESULT WINAPI HlinkFrame_UpdateHlink(IHlinkFrame *iface, ULONG uHLID,
791         IMoniker *pimkTarget, LPCWSTR pwzLocation, LPCWSTR pwzFriendlyName)
792 {
793     WebBrowser *This = HLINKFRAME_THIS(iface);
794     FIXME("(%p)->(%u %p %s %s)\n", This, uHLID, pimkTarget, debugstr_w(pwzLocation),
795           debugstr_w(pwzFriendlyName));
796     return E_NOTIMPL;
797 }
798
799 #undef HLINKFRAME_THIS
800
801 static const IHlinkFrameVtbl HlinkFrameVtbl = {
802     HlinkFrame_QueryInterface,
803     HlinkFrame_AddRef,
804     HlinkFrame_Release,
805     HlinkFrame_SetBrowseContext,
806     HlinkFrame_GetBrowseContext,
807     HlinkFrame_Navigate,
808     HlinkFrame_OnNavigate,
809     HlinkFrame_UpdateHlink
810 };
811
812 void WebBrowser_HlinkFrame_Init(WebBrowser *This)
813 {
814     This->lpHlinkFrameVtbl = &HlinkFrameVtbl;
815 }