mshtml: Added IHTMLBodyElement::get_link implementation.
[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 "mshtml.h"
26 #include "exdispid.h"
27 #include "shellapi.h"
28 #include "winreg.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(shdocvw);
31
32 static const WCHAR emptyW[] = {0};
33
34 typedef struct {
35     const IBindStatusCallbackVtbl  *lpBindStatusCallbackVtbl;
36     const IHttpNegotiateVtbl       *lpHttpNegotiateVtbl;
37
38     LONG ref;
39
40     DocHost *doc_host;
41
42     LPWSTR url;
43     HGLOBAL post_data;
44     BSTR headers;
45     ULONG post_data_len;
46 } BindStatusCallback;
47
48 #define BINDSC(x)  ((IBindStatusCallback*) &(x)->lpBindStatusCallbackVtbl)
49 #define HTTPNEG(x) ((IHttpNegotiate*)      &(x)->lpHttpNegotiateVtbl)
50
51 static void dump_BINDINFO(BINDINFO *bi)
52 {
53     static const char * const BINDINFOF_str[] = {
54         "#0",
55         "BINDINFOF_URLENCODESTGMEDDATA",
56         "BINDINFOF_URLENCODEDEXTRAINFO"
57     };
58
59     static const char * const BINDVERB_str[] = {
60         "BINDVERB_GET",
61         "BINDVERB_POST",
62         "BINDVERB_PUT",
63         "BINDVERB_CUSTOM"
64     };
65
66     TRACE("\n"
67             "BINDINFO = {\n"
68             "    %d, %s,\n"
69             "    {%d, %p, %p},\n"
70             "    %s,\n"
71             "    %s,\n"
72             "    %s,\n"
73             "    %d, %08x, %d, %d\n"
74             "    {%d %p %x},\n"
75             "    %s\n"
76             "    %p, %d\n"
77             "}\n",
78
79             bi->cbSize, debugstr_w(bi->szExtraInfo),
80             bi->stgmedData.tymed, bi->stgmedData.u.hGlobal, bi->stgmedData.pUnkForRelease,
81             bi->grfBindInfoF > BINDINFOF_URLENCODEDEXTRAINFO
82                 ? "unknown" : BINDINFOF_str[bi->grfBindInfoF],
83             bi->dwBindVerb > BINDVERB_CUSTOM
84                 ? "unknown" : BINDVERB_str[bi->dwBindVerb],
85             debugstr_w(bi->szCustomVerb),
86             bi->cbstgmedData, bi->dwOptions, bi->dwOptionsFlags, bi->dwCodePage,
87             bi->securityAttributes.nLength,
88             bi->securityAttributes.lpSecurityDescriptor,
89             bi->securityAttributes.bInheritHandle,
90             debugstr_guid(&bi->iid),
91             bi->pUnk, bi->dwReserved
92             );
93 }
94
95 static void set_status_text(BindStatusCallback *This, LPCWSTR str)
96 {
97     VARIANTARG arg;
98     DISPPARAMS dispparams = {&arg, NULL, 1, 0};
99
100     if(!This->doc_host)
101         return;
102
103     V_VT(&arg) = VT_BSTR;
104     V_BSTR(&arg) = str ? SysAllocString(str) : NULL;
105     call_sink(This->doc_host->cps.wbe2, DISPID_STATUSTEXTCHANGE, &dispparams);
106     VariantClear(&arg);
107
108     if(This->doc_host->frame)
109         IOleInPlaceFrame_SetStatusText(This->doc_host->frame, str);
110 }
111
112 #define BINDSC_THIS(iface) DEFINE_THIS(BindStatusCallback, BindStatusCallback, iface)
113
114 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
115                                                         REFIID riid, void **ppv)
116 {
117     BindStatusCallback *This = BINDSC_THIS(iface);
118
119     *ppv = NULL;
120
121     if(IsEqualGUID(&IID_IUnknown, riid)) {
122         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
123         *ppv = BINDSC(This);
124     }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
125         TRACE("(%p)->(IID_IBindStatusCallback %p)\n", This, ppv);
126         *ppv = BINDSC(This);
127     }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
128         TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
129         *ppv = HTTPNEG(This);
130     }
131
132     if(*ppv) {
133         IBindStatusCallback_AddRef(BINDSC(This));
134         return S_OK;
135     }
136
137     WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
138     return E_NOINTERFACE;
139 }
140
141 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
142 {
143     BindStatusCallback *This = BINDSC_THIS(iface);
144     LONG ref = InterlockedIncrement(&This->ref);
145
146     TRACE("(%p) ref=%d\n", This, ref);
147
148     return ref;
149 }
150
151 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
152 {
153     BindStatusCallback *This = BINDSC_THIS(iface);
154     LONG ref = InterlockedDecrement(&This->ref);
155
156     TRACE("(%p) ref=%d\n", This, ref);
157
158     if(!ref) {
159         if(This->doc_host)
160             IOleClientSite_Release(CLIENTSITE(This->doc_host));
161         if(This->post_data)
162             GlobalFree(This->post_data);
163         if(This->headers)
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 E_NOTIMPL;
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 void object_available_proc(DocHost *This, task_header_t *task)
273 {
274     object_available(This);
275 }
276
277 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
278         REFIID riid, IUnknown *punk)
279 {
280     BindStatusCallback *This = BINDSC_THIS(iface);
281     task_header_t *task;
282     IOleObject *oleobj;
283     HRESULT hres;
284
285     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
286
287     IUnknown_AddRef(punk);
288     This->doc_host->document = punk;
289
290     hres = IUnknown_QueryInterface(punk, &IID_IOleObject, (void**)&oleobj);
291     if(SUCCEEDED(hres)) {
292         CLSID clsid;
293
294         hres = IOleObject_GetUserClassID(oleobj, &clsid);
295         if(SUCCEEDED(hres))
296             TRACE("Got clsid %s\n",
297                   IsEqualGUID(&clsid, &CLSID_HTMLDocument) ? "CLSID_HTMLDocument" : debugstr_guid(&clsid));
298
299         hres = IOleObject_SetClientSite(oleobj, CLIENTSITE(This->doc_host));
300         if(FAILED(hres))
301             FIXME("SetClientSite failed: %08x\n", hres);
302
303         IOleObject_Release(oleobj);
304     }else {
305         FIXME("Could not get IOleObject iface: %08x\n", hres);
306     }
307
308     /* FIXME: Call SetAdvise */
309     /* FIXME: Call Invoke(DISPID_READYSTATE) */
310
311     task = heap_alloc(sizeof(*task));
312     push_dochost_task(This->doc_host, task, object_available_proc, FALSE);
313
314     return S_OK;
315 }
316
317 #undef BSC_THIS
318
319 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
320     BindStatusCallback_QueryInterface,
321     BindStatusCallback_AddRef,
322     BindStatusCallback_Release,
323     BindStatusCallback_OnStartBinding,
324     BindStatusCallback_GetPriority,
325     BindStatusCallback_OnLowResource,
326     BindStatusCallback_OnProgress,
327     BindStatusCallback_OnStopBinding,
328     BindStatusCallback_GetBindInfo,
329     BindStatusCallback_OnDataAvailable,
330     BindStatusCallback_OnObjectAvailable
331 };
332
333 #define HTTPNEG_THIS(iface) DEFINE_THIS(BindStatusCallback, HttpNegotiate, iface)
334
335 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate *iface,
336                                                    REFIID riid, void **ppv)
337 {
338     BindStatusCallback *This = HTTPNEG_THIS(iface);
339     return IBindStatusCallback_QueryInterface(BINDSC(This), riid, ppv);
340 }
341
342 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate *iface)
343 {
344     BindStatusCallback *This = HTTPNEG_THIS(iface);
345     return IBindStatusCallback_AddRef(BINDSC(This));
346 }
347
348 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate *iface)
349 {
350     BindStatusCallback *This = HTTPNEG_THIS(iface);
351     return IBindStatusCallback_Release(BINDSC(This));
352 }
353
354 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate *iface,
355         LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
356 {
357     BindStatusCallback *This = HTTPNEG_THIS(iface);
358
359     FIXME("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
360           dwReserved, pszAdditionalHeaders);
361
362     if(This->headers) {
363         int size = (strlenW(This->headers)+1)*sizeof(WCHAR);
364         *pszAdditionalHeaders = CoTaskMemAlloc(size);
365         memcpy(*pszAdditionalHeaders, This->headers, size);
366     }
367
368     return S_OK;
369 }
370
371 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate *iface,
372         DWORD dwResponseCode, LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders,
373         LPWSTR *pszAdditionalRequestHeaders)
374 {
375     BindStatusCallback *This = HTTPNEG_THIS(iface);
376     FIXME("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
377           debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
378     return E_NOTIMPL;
379 }
380
381 #undef HTTPNEG_THIS
382
383 static const IHttpNegotiateVtbl HttpNegotiateVtbl = {
384     HttpNegotiate_QueryInterface,
385     HttpNegotiate_AddRef,
386     HttpNegotiate_Release,
387     HttpNegotiate_BeginningTransaction,
388     HttpNegotiate_OnResponse
389 };
390
391 static BindStatusCallback *create_callback(DocHost *doc_host, LPCWSTR url, PBYTE post_data,
392         ULONG post_data_len, LPCWSTR headers)
393 {
394     BindStatusCallback *ret = heap_alloc(sizeof(BindStatusCallback));
395
396     ret->lpBindStatusCallbackVtbl = &BindStatusCallbackVtbl;
397     ret->lpHttpNegotiateVtbl      = &HttpNegotiateVtbl;
398
399     ret->ref = 1;
400     ret->url = heap_strdupW(url);
401     ret->post_data = NULL;
402     ret->post_data_len = post_data_len;
403     ret->headers = headers ? SysAllocString(headers) : NULL;
404
405     ret->doc_host = doc_host;
406     IOleClientSite_AddRef(CLIENTSITE(doc_host));
407
408     if(post_data) {
409         ret->post_data = GlobalAlloc(0, post_data_len);
410         memcpy(ret->post_data, post_data, post_data_len);
411     }
412
413     return ret;
414 }
415
416 static void on_before_navigate2(DocHost *This, LPCWSTR url, const BYTE *post_data,
417                                 ULONG post_data_len, LPWSTR headers, VARIANT_BOOL *cancel)
418 {
419     VARIANT var_url, var_flags, var_frame_name, var_post_data, var_post_data2, var_headers;
420     DISPPARAMS dispparams;
421     VARIANTARG params[7];
422
423     dispparams.cArgs = 7;
424     dispparams.cNamedArgs = 0;
425     dispparams.rgdispidNamedArgs = NULL;
426     dispparams.rgvarg = params;
427
428     V_VT(params) = VT_BOOL|VT_BYREF;
429     V_BOOLREF(params) = cancel;
430
431     V_VT(params+1) = (VT_BYREF|VT_VARIANT);
432     V_VARIANTREF(params+1) = &var_headers;
433     V_VT(&var_headers) = VT_BSTR;
434     V_BSTR(&var_headers) = headers;
435
436     V_VT(params+2) = (VT_BYREF|VT_VARIANT);
437     V_VARIANTREF(params+2) = &var_post_data2;
438     V_VT(&var_post_data2) = (VT_BYREF|VT_VARIANT);
439     V_VARIANTREF(&var_post_data2) = &var_post_data;
440     VariantInit(&var_post_data);
441
442     if(post_data_len) {
443         SAFEARRAYBOUND bound = {post_data_len, 0};
444         void *data;
445
446         V_VT(&var_post_data) = VT_UI1|VT_ARRAY;
447         V_ARRAY(&var_post_data) = SafeArrayCreate(VT_UI1, 1, &bound);
448
449         SafeArrayAccessData(V_ARRAY(&var_post_data), &data);
450         memcpy(data, post_data, post_data_len);
451         SafeArrayUnaccessData(V_ARRAY(&var_post_data));
452     }
453
454     V_VT(params+3) = (VT_BYREF|VT_VARIANT);
455     V_VARIANTREF(params+3) = &var_frame_name;
456     V_VT(&var_frame_name) = VT_BSTR;
457     V_BSTR(&var_frame_name) = NULL;
458
459     V_VT(params+4) = (VT_BYREF|VT_VARIANT);
460     V_VARIANTREF(params+4) = &var_flags;
461     V_VT(&var_flags) = VT_I4;
462     V_I4(&var_flags) = 0;
463
464     V_VT(params+5) = (VT_BYREF|VT_VARIANT);
465     V_VARIANTREF(params+5) = &var_url;
466     V_VT(&var_url) = VT_BSTR;
467     V_BSTR(&var_url) = SysAllocString(url);
468
469     V_VT(params+6) = (VT_DISPATCH);
470     V_DISPATCH(params+6) = This->disp;
471
472     call_sink(This->cps.wbe2, DISPID_BEFORENAVIGATE2, &dispparams);
473
474     SysFreeString(V_BSTR(&var_url));
475     if(post_data_len)
476         SafeArrayDestroy(V_ARRAY(&var_post_data));
477 }
478
479 /* FIXME: urlmon should handle it */
480 static BOOL try_application_url(LPCWSTR url)
481 {
482     SHELLEXECUTEINFOW exec_info;
483     WCHAR app[64];
484     HKEY hkey;
485     DWORD res, type;
486     HRESULT hres;
487
488     static const WCHAR wszURLProtocol[] = {'U','R','L',' ','P','r','o','t','o','c','o','l',0};
489
490     hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, app, sizeof(app)/sizeof(WCHAR), NULL, 0);
491     if(FAILED(hres))
492         return FALSE;
493
494     res = RegOpenKeyW(HKEY_CLASSES_ROOT, app, &hkey);
495     if(res != ERROR_SUCCESS)
496         return FALSE;
497
498     res = RegQueryValueExW(hkey, wszURLProtocol, NULL, &type, NULL, NULL);
499     RegCloseKey(hkey);
500     if(res != ERROR_SUCCESS || type != REG_SZ)
501         return FALSE;
502
503     TRACE("openning application %s\n", debugstr_w(app));
504  
505     memset(&exec_info, 0, sizeof(exec_info));
506     exec_info.cbSize = sizeof(exec_info);
507     exec_info.lpFile = url;
508     exec_info.nShow = SW_SHOW;
509
510     return ShellExecuteExW(&exec_info);
511 }
512
513 static HRESULT http_load_hack(DocHost *This, IMoniker *mon, IBindStatusCallback *callback, IBindCtx *bindctx)
514 {
515     IPersistMoniker *persist;
516     IUnknown *doc;
517     HRESULT hres;
518
519     /*
520      * FIXME:
521      * We should use URLMoniker's BindToObject instead creating HTMLDocument here.
522      * This should be fixed when mshtml.dll and urlmon.dll will be good enough.
523      */
524
525     hres = CoCreateInstance(&CLSID_HTMLDocument, NULL,
526                             CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
527                             &IID_IUnknown, (void**)&doc);
528
529     if(FAILED(hres)) {
530         ERR("Could not create HTMLDocument: %08x\n", hres);
531         return hres;
532     }
533
534     hres = IUnknown_QueryInterface(doc, &IID_IPersistMoniker, (void**)&persist);
535     if(FAILED(hres)) {
536         IUnknown_Release(doc);
537         return hres;
538     }
539
540     hres = IPersistMoniker_Load(persist, FALSE, mon, bindctx, 0);
541     IPersistMoniker_Release(persist);
542
543     if(SUCCEEDED(hres))
544         hres = IBindStatusCallback_OnObjectAvailable(callback, &IID_IUnknown, doc);
545     else
546         WARN("Load failed: %08x\n", hres);
547
548     IUnknown_Release(doc);
549
550     return IBindStatusCallback_OnStopBinding(callback, hres, NULL);
551 }
552
553 static HRESULT bind_to_object(DocHost *This, IMoniker *mon, LPCWSTR url, IBindCtx *bindctx,
554                               IBindStatusCallback *callback)
555 {
556     WCHAR schema[30];
557     DWORD schema_len;
558     HRESULT hres;
559
560     static const WCHAR httpW[] = {'h','t','t','p',0};
561     static const WCHAR httpsW[] = {'h','t','t','p','s',0};
562     static const WCHAR ftpW[]= {'f','t','p',0};
563
564     if(mon) {
565         IMoniker_AddRef(mon);
566     }else {
567         hres = CreateURLMoniker(NULL, url, &mon);
568         if(FAILED(hres)) {
569             WARN("CreateURLMoniker failed: %08x\n", hres);
570             return hres;
571         }
572     }
573
574     CoTaskMemFree(This->url);
575     hres = IMoniker_GetDisplayName(mon, 0, NULL, &This->url);
576     if(FAILED(hres))
577         FIXME("GetDisplayName failed: %08x\n", hres);
578
579     IBindCtx_RegisterObjectParam(bindctx, (LPOLESTR)SZ_HTML_CLIENTSITE_OBJECTPARAM,
580                                  (IUnknown*)CLIENTSITE(This));
581
582     hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, schema, sizeof(schema)/sizeof(schema[0]),
583             &schema_len, 0);
584     if(SUCCEEDED(hres) &&
585        (!strcmpW(schema, httpW) || !strcmpW(schema, httpsW) || !strcmpW(schema, ftpW))) {
586         hres = http_load_hack(This, mon, callback, bindctx);
587     }else {
588         IUnknown *unk = NULL;
589
590         hres = IMoniker_BindToObject(mon, bindctx, NULL, &IID_IUnknown, (void**)&unk);
591         if(SUCCEEDED(hres)) {
592             hres = S_OK;
593             if(unk)
594                 IUnknown_Release(unk);
595         }else if(try_application_url(url)) {
596             hres = S_OK;
597         }else {
598             FIXME("BindToObject failed: %08x\n", hres);
599         }
600     }
601
602     IMoniker_Release(mon);
603     return S_OK;
604 }
605
606 static HRESULT navigate_bsc(DocHost *This, BindStatusCallback *bsc, IMoniker *mon)
607 {
608     IBindCtx *bindctx;
609     VARIANT_BOOL cancel = VARIANT_FALSE;
610     HRESULT hres;
611
612     on_before_navigate2(This, bsc->url, bsc->post_data, bsc->post_data_len, bsc->headers, &cancel);
613     if(cancel) {
614         FIXME("Navigation canceled\n");
615         return S_OK;
616     }
617
618     if(This->document)
619         deactivate_document(This);
620
621     CreateAsyncBindCtx(0, BINDSC(bsc), 0, &bindctx);
622
623     if(This->frame)
624         IOleInPlaceFrame_EnableModeless(This->frame, FALSE);
625
626     hres = bind_to_object(This, mon, bsc->url, bindctx, BINDSC(bsc));
627
628     if(This->frame)
629         IOleInPlaceFrame_EnableModeless(This->frame, TRUE);
630
631     IBindCtx_Release(bindctx);
632
633     return hres;
634 }
635
636 typedef struct {
637     task_header_t header;
638     BindStatusCallback *bsc;
639 } task_navigate_bsc_t;
640
641 static void navigate_bsc_proc(DocHost *This, task_header_t *t)
642 {
643     task_navigate_bsc_t *task = (task_navigate_bsc_t*)t;
644
645     if(!This->hwnd)
646         create_doc_view_hwnd(This);
647
648     navigate_bsc(This, task->bsc, NULL);
649
650     IBindStatusCallback_Release(BINDSC(task->bsc));
651 }
652
653
654 HRESULT navigate_url(DocHost *This, LPCWSTR url, const VARIANT *Flags,
655                      const VARIANT *TargetFrameName, VARIANT *PostData, VARIANT *Headers)
656 {
657     task_navigate_bsc_t *task;
658     PBYTE post_data = NULL;
659     ULONG post_data_len = 0;
660     LPWSTR headers = NULL;
661
662     TRACE("navigating to %s\n", debugstr_w(url));
663
664     if((Flags && V_VT(Flags) != VT_EMPTY) 
665        || (TargetFrameName && V_VT(TargetFrameName) != VT_EMPTY))
666         FIXME("Unsupported args (Flags %p:%d; TargetFrameName %p:%d)\n",
667                 Flags, Flags ? V_VT(Flags) : -1, TargetFrameName,
668                 TargetFrameName ? V_VT(TargetFrameName) : -1);
669
670     if(PostData) {
671         TRACE("PostData vt=%d\n", V_VT(PostData));
672
673         if(V_VT(PostData) == (VT_ARRAY | VT_UI1)) {
674             SafeArrayAccessData(V_ARRAY(PostData), (void**)&post_data);
675             post_data_len = V_ARRAY(PostData)->rgsabound[0].cElements;
676         }
677     }
678
679     if(Headers && V_VT(Headers) != VT_EMPTY && V_VT(Headers) != VT_ERROR) {
680         if(V_VT(Headers) != VT_BSTR)
681             return E_INVALIDARG;
682
683         headers = V_BSTR(Headers);
684         TRACE("Headers: %s\n", debugstr_w(headers));
685     }
686
687     task = heap_alloc(sizeof(*task));
688     task->bsc = create_callback(This, url, post_data, post_data_len, headers);
689
690     if(post_data)
691         SafeArrayUnaccessData(V_ARRAY(PostData));
692
693     push_dochost_task(This, &task->header, navigate_bsc_proc, This->url == NULL);
694
695     return S_OK;
696 }
697
698 static HRESULT navigate_hlink(DocHost *This, IMoniker *mon, IBindCtx *bindctx,
699                               IBindStatusCallback *callback)
700 {
701     IHttpNegotiate *http_negotiate;
702     BindStatusCallback *bsc;
703     PBYTE post_data = NULL;
704     ULONG post_data_len = 0;
705     LPWSTR headers = NULL, url;
706     BINDINFO bindinfo;
707     DWORD bindf = 0;
708     HRESULT hres;
709
710     hres = IBindStatusCallback_QueryInterface(callback, &IID_IHttpNegotiate,
711                                               (void**)&http_negotiate);
712     if(SUCCEEDED(hres)) {
713         static const WCHAR null_string[] = {0};
714
715         IHttpNegotiate_BeginningTransaction(http_negotiate, null_string, null_string, 0,
716                                             &headers);
717         IHttpNegotiate_Release(http_negotiate);
718     }
719
720     memset(&bindinfo, 0, sizeof(bindinfo));
721     bindinfo.cbSize = sizeof(bindinfo);
722
723     hres = IBindStatusCallback_GetBindInfo(callback, &bindf, &bindinfo);
724     dump_BINDINFO(&bindinfo);
725     if(bindinfo.dwBindVerb == BINDVERB_POST) {
726         post_data_len = bindinfo.cbstgmedData;
727         if(post_data_len)
728             post_data = bindinfo.stgmedData.u.hGlobal;
729     }
730
731     hres = IMoniker_GetDisplayName(mon, 0, NULL, &url);
732     if(FAILED(hres))
733         FIXME("GetDisplayName failed: %08x\n", hres);
734
735     bsc = create_callback(This, url, post_data, post_data_len, headers);
736     CoTaskMemFree(url);
737
738     hres = navigate_bsc(This, bsc, mon);
739
740     IBindStatusCallback_Release(BINDSC(bsc));
741     CoTaskMemFree(headers);
742     ReleaseBindInfo(&bindinfo);
743
744     return hres;
745 }
746
747 HRESULT go_home(DocHost *This)
748 {
749     static const WCHAR wszAboutBlank[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
750
751     FIXME("stub\n");
752
753     return navigate_url(This, wszAboutBlank, NULL, NULL, NULL, NULL);
754 }
755
756 #define HLINKFRAME_THIS(iface) DEFINE_THIS(WebBrowser, HlinkFrame, iface)
757
758 static HRESULT WINAPI HlinkFrame_QueryInterface(IHlinkFrame *iface, REFIID riid, void **ppv)
759 {
760     WebBrowser *This = HLINKFRAME_THIS(iface);
761     return IWebBrowser2_QueryInterface(WEBBROWSER2(This), riid, ppv);
762 }
763
764 static ULONG WINAPI HlinkFrame_AddRef(IHlinkFrame *iface)
765 {
766     WebBrowser *This = HLINKFRAME_THIS(iface);
767     return IWebBrowser2_AddRef(WEBBROWSER2(This));
768 }
769
770 static ULONG WINAPI HlinkFrame_Release(IHlinkFrame *iface)
771 {
772     WebBrowser *This = HLINKFRAME_THIS(iface);
773     return IWebBrowser2_Release(WEBBROWSER2(This));
774 }
775
776 static HRESULT WINAPI HlinkFrame_SetBrowseContext(IHlinkFrame *iface,
777                                                   IHlinkBrowseContext *pihlbc)
778 {
779     WebBrowser *This = HLINKFRAME_THIS(iface);
780     FIXME("(%p)->(%p)\n", This, pihlbc);
781     return E_NOTIMPL;
782 }
783
784 static HRESULT WINAPI HlinkFrame_GetBrowseContext(IHlinkFrame *iface,
785                                                   IHlinkBrowseContext **ppihlbc)
786 {
787     WebBrowser *This = HLINKFRAME_THIS(iface);
788     FIXME("(%p)->(%p)\n", This, ppihlbc);
789     return E_NOTIMPL;
790 }
791
792 static HRESULT WINAPI HlinkFrame_Navigate(IHlinkFrame *iface, DWORD grfHLNF, LPBC pbc,
793                                           IBindStatusCallback *pibsc, IHlink *pihlNavigate)
794 {
795     WebBrowser *This = HLINKFRAME_THIS(iface);
796     IMoniker *mon;
797     LPWSTR location = NULL;
798
799     TRACE("(%p)->(%08x %p %p %p)\n", This, grfHLNF, pbc, pibsc, pihlNavigate);
800
801     if(grfHLNF)
802         FIXME("unsupported grfHLNF=%08x\n", grfHLNF);
803
804     /* Windows calls GetTargetFrameName here. */
805
806     IHlink_GetMonikerReference(pihlNavigate, 1, &mon, &location);
807
808     if(location) {
809         FIXME("location = %s\n", debugstr_w(location));
810         CoTaskMemFree(location);
811     }
812
813     /* Windows calls GetHlinkSite here */
814
815     if(grfHLNF & HLNF_OPENINNEWWINDOW) {
816         FIXME("Not supported HLNF_OPENINNEWWINDOW\n");
817         return E_NOTIMPL;
818     }
819
820     return navigate_hlink(&This->doc_host, mon, pbc, pibsc);
821 }
822
823 static HRESULT WINAPI HlinkFrame_OnNavigate(IHlinkFrame *iface, DWORD grfHLNF,
824         IMoniker *pimkTarget, LPCWSTR pwzLocation, LPCWSTR pwzFriendlyName, DWORD dwreserved)
825 {
826     WebBrowser *This = HLINKFRAME_THIS(iface);
827     FIXME("(%p)->(%08x %p %s %s %d)\n", This, grfHLNF, pimkTarget, debugstr_w(pwzLocation),
828           debugstr_w(pwzFriendlyName), dwreserved);
829     return E_NOTIMPL;
830 }
831
832 static HRESULT WINAPI HlinkFrame_UpdateHlink(IHlinkFrame *iface, ULONG uHLID,
833         IMoniker *pimkTarget, LPCWSTR pwzLocation, LPCWSTR pwzFriendlyName)
834 {
835     WebBrowser *This = HLINKFRAME_THIS(iface);
836     FIXME("(%p)->(%u %p %s %s)\n", This, uHLID, pimkTarget, debugstr_w(pwzLocation),
837           debugstr_w(pwzFriendlyName));
838     return E_NOTIMPL;
839 }
840
841 #undef HLINKFRAME_THIS
842
843 static const IHlinkFrameVtbl HlinkFrameVtbl = {
844     HlinkFrame_QueryInterface,
845     HlinkFrame_AddRef,
846     HlinkFrame_Release,
847     HlinkFrame_SetBrowseContext,
848     HlinkFrame_GetBrowseContext,
849     HlinkFrame_Navigate,
850     HlinkFrame_OnNavigate,
851     HlinkFrame_UpdateHlink
852 };
853
854 void WebBrowser_HlinkFrame_Init(WebBrowser *This)
855 {
856     This->lpHlinkFrameVtbl = &HlinkFrameVtbl;
857 }