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