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