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