shdocvw: Pass post data as SAFEARRAY to on_before_navigate2.
[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, SAFEARRAY *post_data, LPWSTR headers, VARIANT_BOOL *cancel)
382 {
383     VARIANT var_url, var_flags, var_frame_name, var_post_data, var_post_data2, var_headers;
384     DISPPARAMS dispparams;
385     VARIANTARG params[7];
386
387     dispparams.cArgs = 7;
388     dispparams.cNamedArgs = 0;
389     dispparams.rgdispidNamedArgs = NULL;
390     dispparams.rgvarg = params;
391
392     This->busy = VARIANT_TRUE;
393
394     V_VT(params) = VT_BOOL|VT_BYREF;
395     V_BOOLREF(params) = cancel;
396
397     V_VT(params+1) = (VT_BYREF|VT_VARIANT);
398     V_VARIANTREF(params+1) = &var_headers;
399     V_VT(&var_headers) = VT_BSTR;
400     V_BSTR(&var_headers) = headers;
401
402     V_VT(params+2) = (VT_BYREF|VT_VARIANT);
403     V_VARIANTREF(params+2) = &var_post_data2;
404     V_VT(&var_post_data2) = (VT_BYREF|VT_VARIANT);
405     V_VARIANTREF(&var_post_data2) = &var_post_data;
406
407     if(post_data) {
408         V_VT(&var_post_data) = VT_UI1|VT_ARRAY;
409         V_ARRAY(&var_post_data) = post_data;
410     }else {
411         V_VT(&var_post_data) = VT_EMPTY;
412     }
413
414     V_VT(params+3) = (VT_BYREF|VT_VARIANT);
415     V_VARIANTREF(params+3) = &var_frame_name;
416     V_VT(&var_frame_name) = VT_BSTR;
417     V_BSTR(&var_frame_name) = NULL;
418
419     V_VT(params+4) = (VT_BYREF|VT_VARIANT);
420     V_VARIANTREF(params+4) = &var_flags;
421     V_VT(&var_flags) = VT_I4;
422     V_I4(&var_flags) = 0;
423
424     V_VT(params+5) = (VT_BYREF|VT_VARIANT);
425     V_VARIANTREF(params+5) = &var_url;
426     V_VT(&var_url) = VT_BSTR;
427     V_BSTR(&var_url) = SysAllocString(url);
428
429     V_VT(params+6) = (VT_DISPATCH);
430     V_DISPATCH(params+6) = This->disp;
431
432     call_sink(This->cps.wbe2, DISPID_BEFORENAVIGATE2, &dispparams);
433
434     SysFreeString(V_BSTR(&var_url));
435 }
436
437 /* FIXME: urlmon should handle it */
438 static BOOL try_application_url(LPCWSTR url)
439 {
440     SHELLEXECUTEINFOW exec_info;
441     WCHAR app[64];
442     HKEY hkey;
443     DWORD res, type;
444     HRESULT hres;
445
446     static const WCHAR wszURLProtocol[] = {'U','R','L',' ','P','r','o','t','o','c','o','l',0};
447
448     hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, app, sizeof(app)/sizeof(WCHAR), NULL, 0);
449     if(FAILED(hres))
450         return FALSE;
451
452     res = RegOpenKeyW(HKEY_CLASSES_ROOT, app, &hkey);
453     if(res != ERROR_SUCCESS)
454         return FALSE;
455
456     res = RegQueryValueExW(hkey, wszURLProtocol, NULL, &type, NULL, NULL);
457     RegCloseKey(hkey);
458     if(res != ERROR_SUCCESS || type != REG_SZ)
459         return FALSE;
460
461     TRACE("openning application %s\n", debugstr_w(app));
462  
463     memset(&exec_info, 0, sizeof(exec_info));
464     exec_info.cbSize = sizeof(exec_info);
465     exec_info.lpFile = url;
466     exec_info.nShow = SW_SHOW;
467
468     return ShellExecuteExW(&exec_info);
469 }
470
471 static HRESULT create_moniker(LPCWSTR url, IMoniker **mon)
472 {
473     WCHAR new_url[INTERNET_MAX_URL_LENGTH];
474     DWORD size;
475     HRESULT hres;
476
477     if(PathIsURLW(url))
478         return CreateURLMoniker(NULL, url, mon);
479
480     if(url[1] == ':') {
481         size = sizeof(new_url);
482         hres = UrlCreateFromPathW(url, new_url, &size, 0);
483         if(FAILED(hres)) {
484             WARN("UrlCreateFromPathW failed: %08x\n", hres);
485             return hres;
486         }
487     }else {
488         size = sizeof(new_url);
489         hres = UrlApplySchemeW(url, new_url, &size, URL_APPLY_GUESSSCHEME);
490         TRACE("got %s\n", debugstr_w(new_url));
491         if(FAILED(hres)) {
492             WARN("UrlApplyScheme failed: %08x\n", hres);
493             return hres;
494         }
495     }
496
497     return CreateURLMoniker(NULL, new_url, mon);
498 }
499
500 static HRESULT bind_to_object(DocHost *This, IMoniker *mon, LPCWSTR url, IBindCtx *bindctx,
501                               IBindStatusCallback *callback)
502 {
503     IUnknown *unk = NULL;
504     HRESULT hres;
505
506     if(mon) {
507         IMoniker_AddRef(mon);
508     }else {
509         hres = create_moniker(url, &mon);
510         if(FAILED(hres))
511             return hres;
512     }
513
514     CoTaskMemFree(This->url);
515     hres = IMoniker_GetDisplayName(mon, 0, NULL, &This->url);
516     if(FAILED(hres))
517         FIXME("GetDisplayName failed: %08x\n", hres);
518
519     IBindCtx_RegisterObjectParam(bindctx, (LPOLESTR)SZ_HTML_CLIENTSITE_OBJECTPARAM,
520                                  (IUnknown*)CLIENTSITE(This));
521
522     hres = IMoniker_BindToObject(mon, bindctx, NULL, &IID_IUnknown, (void**)&unk);
523     if(SUCCEEDED(hres)) {
524         hres = S_OK;
525         if(unk)
526             IUnknown_Release(unk);
527     }else if(try_application_url(url)) {
528         hres = S_OK;
529     }else {
530         FIXME("BindToObject failed: %08x\n", hres);
531     }
532
533     IMoniker_Release(mon);
534     return S_OK;
535 }
536
537 static HRESULT navigate_bsc(DocHost *This, BindStatusCallback *bsc, IMoniker *mon)
538 {
539     VARIANT_BOOL cancel = VARIANT_FALSE;
540     SAFEARRAY *post_data = NULL;
541     IBindCtx *bindctx;
542     HRESULT hres;
543
544     set_doc_state(This, READYSTATE_LOADING);
545
546     if(bsc->post_data) {
547         post_data = SafeArrayCreateVector(VT_UI1, 0, bsc->post_data_len);
548         memcpy(post_data->pvData, post_data, bsc->post_data_len);
549     }
550
551     on_before_navigate2(This, bsc->url, post_data, bsc->headers, &cancel);
552     if(post_data)
553         SafeArrayDestroy(post_data);
554     if(cancel) {
555         FIXME("Navigation canceled\n");
556         return S_OK;
557     }
558
559     if(This->document)
560         deactivate_document(This);
561
562     CreateAsyncBindCtx(0, BINDSC(bsc), 0, &bindctx);
563
564     if(This->frame)
565         IOleInPlaceFrame_EnableModeless(This->frame, FALSE);
566
567     hres = bind_to_object(This, mon, bsc->url, bindctx, BINDSC(bsc));
568
569     if(This->frame)
570         IOleInPlaceFrame_EnableModeless(This->frame, TRUE);
571
572     IBindCtx_Release(bindctx);
573
574     return hres;
575 }
576
577 typedef struct {
578     task_header_t header;
579     BindStatusCallback *bsc;
580 } task_navigate_bsc_t;
581
582 static void navigate_bsc_proc(DocHost *This, task_header_t *t)
583 {
584     task_navigate_bsc_t *task = (task_navigate_bsc_t*)t;
585
586     if(!This->hwnd)
587         create_doc_view_hwnd(This);
588
589     navigate_bsc(This, task->bsc, NULL);
590
591     IBindStatusCallback_Release(BINDSC(task->bsc));
592 }
593
594
595 HRESULT navigate_url(DocHost *This, LPCWSTR url, const VARIANT *Flags,
596                      const VARIANT *TargetFrameName, VARIANT *PostData, VARIANT *Headers)
597 {
598     task_navigate_bsc_t *task;
599     PBYTE post_data = NULL;
600     ULONG post_data_len = 0;
601     LPWSTR headers = NULL;
602
603     TRACE("navigating to %s\n", debugstr_w(url));
604
605     if((Flags && V_VT(Flags) != VT_EMPTY) 
606        || (TargetFrameName && V_VT(TargetFrameName) != VT_EMPTY))
607         FIXME("Unsupported args (Flags %p:%d; TargetFrameName %p:%d)\n",
608                 Flags, Flags ? V_VT(Flags) : -1, TargetFrameName,
609                 TargetFrameName ? V_VT(TargetFrameName) : -1);
610
611     if(PostData) {
612         TRACE("PostData vt=%d\n", V_VT(PostData));
613
614         if(V_VT(PostData) == (VT_ARRAY | VT_UI1)) {
615             SafeArrayAccessData(V_ARRAY(PostData), (void**)&post_data);
616             post_data_len = V_ARRAY(PostData)->rgsabound[0].cElements;
617         }
618     }
619
620     if(Headers && V_VT(Headers) != VT_EMPTY && V_VT(Headers) != VT_ERROR) {
621         if(V_VT(Headers) != VT_BSTR)
622             return E_INVALIDARG;
623
624         headers = V_BSTR(Headers);
625         TRACE("Headers: %s\n", debugstr_w(headers));
626     }
627
628     task = heap_alloc(sizeof(*task));
629     task->bsc = create_callback(This, url, post_data, post_data_len, headers);
630
631     if(post_data)
632         SafeArrayUnaccessData(V_ARRAY(PostData));
633
634     push_dochost_task(This, &task->header, navigate_bsc_proc, This->url == NULL);
635
636     return S_OK;
637 }
638
639 static HRESULT navigate_hlink(DocHost *This, IMoniker *mon, IBindCtx *bindctx,
640                               IBindStatusCallback *callback)
641 {
642     IHttpNegotiate *http_negotiate;
643     BindStatusCallback *bsc;
644     PBYTE post_data = NULL;
645     ULONG post_data_len = 0;
646     LPWSTR headers = NULL, url;
647     BINDINFO bindinfo;
648     DWORD bindf = 0;
649     HRESULT hres;
650
651     hres = IBindStatusCallback_QueryInterface(callback, &IID_IHttpNegotiate,
652                                               (void**)&http_negotiate);
653     if(SUCCEEDED(hres)) {
654         static const WCHAR null_string[] = {0};
655
656         IHttpNegotiate_BeginningTransaction(http_negotiate, null_string, null_string, 0,
657                                             &headers);
658         IHttpNegotiate_Release(http_negotiate);
659     }
660
661     memset(&bindinfo, 0, sizeof(bindinfo));
662     bindinfo.cbSize = sizeof(bindinfo);
663
664     hres = IBindStatusCallback_GetBindInfo(callback, &bindf, &bindinfo);
665     dump_BINDINFO(&bindinfo);
666     if(bindinfo.dwBindVerb == BINDVERB_POST) {
667         post_data_len = bindinfo.cbstgmedData;
668         if(post_data_len)
669             post_data = bindinfo.stgmedData.u.hGlobal;
670     }
671
672     hres = IMoniker_GetDisplayName(mon, 0, NULL, &url);
673     if(FAILED(hres))
674         FIXME("GetDisplayName failed: %08x\n", hres);
675
676     bsc = create_callback(This, url, post_data, post_data_len, headers);
677     CoTaskMemFree(url);
678
679     hres = navigate_bsc(This, bsc, mon);
680
681     IBindStatusCallback_Release(BINDSC(bsc));
682     CoTaskMemFree(headers);
683     ReleaseBindInfo(&bindinfo);
684
685     return hres;
686 }
687
688 HRESULT go_home(DocHost *This)
689 {
690     HKEY hkey;
691     DWORD res, type, size;
692     WCHAR wszPageName[MAX_PATH];
693     static const WCHAR wszAboutBlank[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
694     static const WCHAR wszStartPage[] = {'S','t','a','r','t',' ','P','a','g','e',0};
695     static const WCHAR wszSubKey[] = {'S','o','f','t','w','a','r','e','\\',
696                                       'M','i','c','r','o','s','o','f','t','\\',
697                                       'I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r','\\',
698                                       'M','a','i','n',0};
699
700     res = RegOpenKeyW(HKEY_CURRENT_USER, wszSubKey, &hkey);
701     if (res != ERROR_SUCCESS)
702         return navigate_url(This, wszAboutBlank, NULL, NULL, NULL, NULL);
703
704     size = sizeof(wszPageName);
705     res = RegQueryValueExW(hkey, wszStartPage, NULL, &type, (LPBYTE)wszPageName, &size);
706     RegCloseKey(hkey);
707     if (res != ERROR_SUCCESS || type != REG_SZ)
708         return navigate_url(This, wszAboutBlank, NULL, NULL, NULL, NULL);
709
710     return navigate_url(This, wszPageName, NULL, NULL, NULL, NULL);
711 }
712
713 #define HLINKFRAME_THIS(iface) DEFINE_THIS(WebBrowser, HlinkFrame, iface)
714
715 static HRESULT WINAPI HlinkFrame_QueryInterface(IHlinkFrame *iface, REFIID riid, void **ppv)
716 {
717     WebBrowser *This = HLINKFRAME_THIS(iface);
718     return IWebBrowser2_QueryInterface(WEBBROWSER2(This), riid, ppv);
719 }
720
721 static ULONG WINAPI HlinkFrame_AddRef(IHlinkFrame *iface)
722 {
723     WebBrowser *This = HLINKFRAME_THIS(iface);
724     return IWebBrowser2_AddRef(WEBBROWSER2(This));
725 }
726
727 static ULONG WINAPI HlinkFrame_Release(IHlinkFrame *iface)
728 {
729     WebBrowser *This = HLINKFRAME_THIS(iface);
730     return IWebBrowser2_Release(WEBBROWSER2(This));
731 }
732
733 static HRESULT WINAPI HlinkFrame_SetBrowseContext(IHlinkFrame *iface,
734                                                   IHlinkBrowseContext *pihlbc)
735 {
736     WebBrowser *This = HLINKFRAME_THIS(iface);
737     FIXME("(%p)->(%p)\n", This, pihlbc);
738     return E_NOTIMPL;
739 }
740
741 static HRESULT WINAPI HlinkFrame_GetBrowseContext(IHlinkFrame *iface,
742                                                   IHlinkBrowseContext **ppihlbc)
743 {
744     WebBrowser *This = HLINKFRAME_THIS(iface);
745     FIXME("(%p)->(%p)\n", This, ppihlbc);
746     return E_NOTIMPL;
747 }
748
749 static HRESULT WINAPI HlinkFrame_Navigate(IHlinkFrame *iface, DWORD grfHLNF, LPBC pbc,
750                                           IBindStatusCallback *pibsc, IHlink *pihlNavigate)
751 {
752     WebBrowser *This = HLINKFRAME_THIS(iface);
753     IMoniker *mon;
754     LPWSTR location = NULL;
755
756     TRACE("(%p)->(%08x %p %p %p)\n", This, grfHLNF, pbc, pibsc, pihlNavigate);
757
758     if(grfHLNF)
759         FIXME("unsupported grfHLNF=%08x\n", grfHLNF);
760
761     /* Windows calls GetTargetFrameName here. */
762
763     IHlink_GetMonikerReference(pihlNavigate, 1, &mon, &location);
764
765     if(location) {
766         FIXME("location = %s\n", debugstr_w(location));
767         CoTaskMemFree(location);
768     }
769
770     /* Windows calls GetHlinkSite here */
771
772     if(grfHLNF & HLNF_OPENINNEWWINDOW) {
773         FIXME("Not supported HLNF_OPENINNEWWINDOW\n");
774         return E_NOTIMPL;
775     }
776
777     return navigate_hlink(&This->doc_host, mon, pbc, pibsc);
778 }
779
780 static HRESULT WINAPI HlinkFrame_OnNavigate(IHlinkFrame *iface, DWORD grfHLNF,
781         IMoniker *pimkTarget, LPCWSTR pwzLocation, LPCWSTR pwzFriendlyName, DWORD dwreserved)
782 {
783     WebBrowser *This = HLINKFRAME_THIS(iface);
784     FIXME("(%p)->(%08x %p %s %s %d)\n", This, grfHLNF, pimkTarget, debugstr_w(pwzLocation),
785           debugstr_w(pwzFriendlyName), dwreserved);
786     return E_NOTIMPL;
787 }
788
789 static HRESULT WINAPI HlinkFrame_UpdateHlink(IHlinkFrame *iface, ULONG uHLID,
790         IMoniker *pimkTarget, LPCWSTR pwzLocation, LPCWSTR pwzFriendlyName)
791 {
792     WebBrowser *This = HLINKFRAME_THIS(iface);
793     FIXME("(%p)->(%u %p %s %s)\n", This, uHLID, pimkTarget, debugstr_w(pwzLocation),
794           debugstr_w(pwzFriendlyName));
795     return E_NOTIMPL;
796 }
797
798 #undef HLINKFRAME_THIS
799
800 static const IHlinkFrameVtbl HlinkFrameVtbl = {
801     HlinkFrame_QueryInterface,
802     HlinkFrame_AddRef,
803     HlinkFrame_Release,
804     HlinkFrame_SetBrowseContext,
805     HlinkFrame_GetBrowseContext,
806     HlinkFrame_Navigate,
807     HlinkFrame_OnNavigate,
808     HlinkFrame_UpdateHlink
809 };
810
811 void WebBrowser_HlinkFrame_Init(WebBrowser *This)
812 {
813     This->lpHlinkFrameVtbl = &HlinkFrameVtbl;
814 }