qedit: Fix return value of DllUnregisterServer.
[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 #include "mshtml.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 S_OK;
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 HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
274         REFIID riid, IUnknown *punk)
275 {
276     BindStatusCallback *This = BINDSC_THIS(iface);
277
278     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
279
280     return dochost_object_available(This->doc_host, punk);
281 }
282
283 #undef BSC_THIS
284
285 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
286     BindStatusCallback_QueryInterface,
287     BindStatusCallback_AddRef,
288     BindStatusCallback_Release,
289     BindStatusCallback_OnStartBinding,
290     BindStatusCallback_GetPriority,
291     BindStatusCallback_OnLowResource,
292     BindStatusCallback_OnProgress,
293     BindStatusCallback_OnStopBinding,
294     BindStatusCallback_GetBindInfo,
295     BindStatusCallback_OnDataAvailable,
296     BindStatusCallback_OnObjectAvailable
297 };
298
299 #define HTTPNEG_THIS(iface) DEFINE_THIS(BindStatusCallback, HttpNegotiate, iface)
300
301 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate *iface,
302                                                    REFIID riid, void **ppv)
303 {
304     BindStatusCallback *This = HTTPNEG_THIS(iface);
305     return IBindStatusCallback_QueryInterface(BINDSC(This), riid, ppv);
306 }
307
308 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate *iface)
309 {
310     BindStatusCallback *This = HTTPNEG_THIS(iface);
311     return IBindStatusCallback_AddRef(BINDSC(This));
312 }
313
314 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate *iface)
315 {
316     BindStatusCallback *This = HTTPNEG_THIS(iface);
317     return IBindStatusCallback_Release(BINDSC(This));
318 }
319
320 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate *iface,
321         LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
322 {
323     BindStatusCallback *This = HTTPNEG_THIS(iface);
324
325     TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
326           dwReserved, pszAdditionalHeaders);
327
328     if(This->headers) {
329         int size = (strlenW(This->headers)+1)*sizeof(WCHAR);
330         *pszAdditionalHeaders = CoTaskMemAlloc(size);
331         memcpy(*pszAdditionalHeaders, This->headers, size);
332     }
333
334     return S_OK;
335 }
336
337 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate *iface,
338         DWORD dwResponseCode, LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders,
339         LPWSTR *pszAdditionalRequestHeaders)
340 {
341     BindStatusCallback *This = HTTPNEG_THIS(iface);
342     TRACE("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
343           debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
344     return S_OK;
345 }
346
347 #undef HTTPNEG_THIS
348
349 static const IHttpNegotiateVtbl HttpNegotiateVtbl = {
350     HttpNegotiate_QueryInterface,
351     HttpNegotiate_AddRef,
352     HttpNegotiate_Release,
353     HttpNegotiate_BeginningTransaction,
354     HttpNegotiate_OnResponse
355 };
356
357 static BindStatusCallback *create_callback(DocHost *doc_host, LPCWSTR url, PBYTE post_data,
358         ULONG post_data_len, LPCWSTR headers)
359 {
360     BindStatusCallback *ret = heap_alloc(sizeof(BindStatusCallback));
361
362     ret->lpBindStatusCallbackVtbl = &BindStatusCallbackVtbl;
363     ret->lpHttpNegotiateVtbl      = &HttpNegotiateVtbl;
364
365     ret->ref = 1;
366     ret->url = heap_strdupW(url);
367     ret->post_data = NULL;
368     ret->post_data_len = post_data_len;
369     ret->headers = headers ? SysAllocString(headers) : NULL;
370
371     ret->doc_host = doc_host;
372     IOleClientSite_AddRef(CLIENTSITE(doc_host));
373
374     if(post_data) {
375         ret->post_data = GlobalAlloc(0, post_data_len);
376         memcpy(ret->post_data, post_data, post_data_len);
377     }
378
379     return ret;
380 }
381
382 static void on_before_navigate2(DocHost *This, LPCWSTR url, SAFEARRAY *post_data, LPWSTR headers, VARIANT_BOOL *cancel)
383 {
384     VARIANT var_url, var_flags, var_frame_name, var_post_data, var_post_data2, var_headers;
385     DISPPARAMS dispparams;
386     VARIANTARG params[7];
387
388     dispparams.cArgs = 7;
389     dispparams.cNamedArgs = 0;
390     dispparams.rgdispidNamedArgs = NULL;
391     dispparams.rgvarg = params;
392
393     This->busy = VARIANT_TRUE;
394
395     V_VT(params) = VT_BOOL|VT_BYREF;
396     V_BOOLREF(params) = cancel;
397
398     V_VT(params+1) = (VT_BYREF|VT_VARIANT);
399     V_VARIANTREF(params+1) = &var_headers;
400     V_VT(&var_headers) = VT_BSTR;
401     V_BSTR(&var_headers) = headers;
402
403     V_VT(params+2) = (VT_BYREF|VT_VARIANT);
404     V_VARIANTREF(params+2) = &var_post_data2;
405     V_VT(&var_post_data2) = (VT_BYREF|VT_VARIANT);
406     V_VARIANTREF(&var_post_data2) = &var_post_data;
407
408     if(post_data) {
409         V_VT(&var_post_data) = VT_UI1|VT_ARRAY;
410         V_ARRAY(&var_post_data) = post_data;
411     }else {
412         V_VT(&var_post_data) = VT_EMPTY;
413     }
414
415     V_VT(params+3) = (VT_BYREF|VT_VARIANT);
416     V_VARIANTREF(params+3) = &var_frame_name;
417     V_VT(&var_frame_name) = VT_BSTR;
418     V_BSTR(&var_frame_name) = NULL;
419
420     V_VT(params+4) = (VT_BYREF|VT_VARIANT);
421     V_VARIANTREF(params+4) = &var_flags;
422     V_VT(&var_flags) = VT_I4;
423     V_I4(&var_flags) = 0;
424
425     V_VT(params+5) = (VT_BYREF|VT_VARIANT);
426     V_VARIANTREF(params+5) = &var_url;
427     V_VT(&var_url) = VT_BSTR;
428     V_BSTR(&var_url) = SysAllocString(url);
429
430     V_VT(params+6) = (VT_DISPATCH);
431     V_DISPATCH(params+6) = This->disp;
432
433     call_sink(This->cps.wbe2, DISPID_BEFORENAVIGATE2, &dispparams);
434
435     SysFreeString(V_BSTR(&var_url));
436 }
437
438 /* FIXME: urlmon should handle it */
439 static BOOL try_application_url(LPCWSTR url)
440 {
441     SHELLEXECUTEINFOW exec_info;
442     WCHAR app[64];
443     HKEY hkey;
444     DWORD res, type;
445     HRESULT hres;
446
447     static const WCHAR wszURLProtocol[] = {'U','R','L',' ','P','r','o','t','o','c','o','l',0};
448
449     hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, app, sizeof(app)/sizeof(WCHAR), NULL, 0);
450     if(FAILED(hres))
451         return FALSE;
452
453     res = RegOpenKeyW(HKEY_CLASSES_ROOT, app, &hkey);
454     if(res != ERROR_SUCCESS)
455         return FALSE;
456
457     res = RegQueryValueExW(hkey, wszURLProtocol, NULL, &type, NULL, NULL);
458     RegCloseKey(hkey);
459     if(res != ERROR_SUCCESS || type != REG_SZ)
460         return FALSE;
461
462     TRACE("openning application %s\n", debugstr_w(app));
463  
464     memset(&exec_info, 0, sizeof(exec_info));
465     exec_info.cbSize = sizeof(exec_info);
466     exec_info.lpFile = url;
467     exec_info.nShow = SW_SHOW;
468
469     return ShellExecuteExW(&exec_info);
470 }
471
472 static HRESULT create_moniker(LPCWSTR url, IMoniker **mon)
473 {
474     WCHAR new_url[INTERNET_MAX_URL_LENGTH];
475     DWORD size;
476     HRESULT hres;
477
478     if(PathIsURLW(url))
479         return CreateURLMoniker(NULL, url, mon);
480
481     if(url[1] == ':') {
482         size = sizeof(new_url);
483         hres = UrlCreateFromPathW(url, new_url, &size, 0);
484         if(FAILED(hres)) {
485             WARN("UrlCreateFromPathW failed: %08x\n", hres);
486             return hres;
487         }
488     }else {
489         size = sizeof(new_url);
490         hres = UrlApplySchemeW(url, new_url, &size, URL_APPLY_GUESSSCHEME);
491         TRACE("got %s\n", debugstr_w(new_url));
492         if(FAILED(hres)) {
493             WARN("UrlApplyScheme failed: %08x\n", hres);
494             return hres;
495         }
496     }
497
498     return CreateURLMoniker(NULL, new_url, mon);
499 }
500
501 static HRESULT bind_to_object(DocHost *This, IMoniker *mon, LPCWSTR url, IBindCtx *bindctx,
502                               IBindStatusCallback *callback)
503 {
504     IUnknown *unk = NULL;
505     HRESULT hres;
506
507     if(mon) {
508         IMoniker_AddRef(mon);
509     }else {
510         hres = create_moniker(url, &mon);
511         if(FAILED(hres))
512             return hres;
513     }
514
515     CoTaskMemFree(This->url);
516     hres = IMoniker_GetDisplayName(mon, 0, NULL, &This->url);
517     if(FAILED(hres))
518         FIXME("GetDisplayName failed: %08x\n", hres);
519
520     IBindCtx_RegisterObjectParam(bindctx, (LPOLESTR)SZ_HTML_CLIENTSITE_OBJECTPARAM,
521                                  (IUnknown*)CLIENTSITE(This));
522
523     hres = IMoniker_BindToObject(mon, bindctx, NULL, &IID_IUnknown, (void**)&unk);
524     if(SUCCEEDED(hres)) {
525         hres = S_OK;
526         if(unk)
527             IUnknown_Release(unk);
528     }else if(try_application_url(url)) {
529         hres = S_OK;
530     }else {
531         FIXME("BindToObject failed: %08x\n", hres);
532     }
533
534     IMoniker_Release(mon);
535     return S_OK;
536 }
537
538 static void html_window_navigate(DocHost *This, IHTMLPrivateWindow *window, BSTR url, BSTR headers, SAFEARRAY *post_data)
539 {
540     VARIANT headers_var, post_data_var;
541     WCHAR *new_url;
542     BSTR empty_str;
543     DWORD size;
544     HRESULT hres;
545
546     size = (strlenW(url)+1)*sizeof(WCHAR);
547     new_url = CoTaskMemAlloc(size);
548     if(!new_url)
549         return;
550     memcpy(new_url, url, size);
551     CoTaskMemFree(This->url);
552     This->url = new_url;
553
554     empty_str = SysAllocStringLen(NULL, 0);
555
556     if(headers) {
557         V_VT(&headers_var) = VT_BSTR;
558         V_BSTR(&headers_var) = headers;
559     }else {
560         V_VT(&headers_var) = VT_EMPTY;
561     }
562
563     if(post_data) {
564         V_VT(&post_data_var) = VT_UI1|VT_ARRAY;
565         V_ARRAY(&post_data_var) = post_data;
566     }else {
567         V_VT(&post_data_var) = VT_EMPTY;
568     }
569
570     set_doc_state(This, READYSTATE_LOADING);
571     hres = IHTMLPrivateWindow_SuperNavigate(window, url, empty_str, NULL, NULL, &post_data_var, &headers_var, 0);
572     SysFreeString(empty_str);
573     if(FAILED(hres))
574         WARN("SuprtNavigate failed: %08x\n", hres);
575 }
576
577 typedef struct {
578     task_header_t header;
579     BSTR url;
580     BSTR headers;
581     SAFEARRAY *post_data;
582     BOOL async_notif;
583 } task_doc_navigate_t;
584
585 static HRESULT free_doc_navigate_task(task_doc_navigate_t *task, BOOL free_task)
586 {
587     SysFreeString(task->url);
588     SysFreeString(task->headers);
589     if(task->post_data)
590         SafeArrayDestroy(task->post_data);
591     if(free_task)
592         heap_free(task);
593     return E_OUTOFMEMORY;
594 }
595
596 static void doc_navigate_proc(DocHost *This, task_header_t *t)
597 {
598     task_doc_navigate_t *task = (task_doc_navigate_t*)t;
599     IHTMLPrivateWindow *priv_window;
600     HRESULT hres;
601
602     if(!This->doc_navigate)
603         return;
604
605     if(task->async_notif) {
606         VARIANT_BOOL cancel = VARIANT_FALSE;
607         on_before_navigate2(This, task->url, task->post_data, task->headers, &cancel);
608         if(cancel) {
609             TRACE("Navigation calnceled\n");
610             free_doc_navigate_task(task, FALSE);
611             return;
612         }
613     }
614
615     hres = IUnknown_QueryInterface(This->doc_navigate, &IID_IHTMLPrivateWindow, (void**)&priv_window);
616     if(SUCCEEDED(hres)) {
617         html_window_navigate(This, priv_window, task->url, task->headers, task->post_data);
618         IHTMLPrivateWindow_Release(priv_window);
619     }else {
620         WARN("Could not get IHTMLPrivateWindow iface: %08x\n", hres);
621     }
622
623     free_doc_navigate_task(task, FALSE);
624 }
625
626 static HRESULT async_doc_navigate(DocHost *This, LPCWSTR url, LPCWSTR headers, PBYTE post_data, ULONG post_data_size,
627         BOOL async_notif)
628 {
629     task_doc_navigate_t *task;
630
631     task = heap_alloc_zero(sizeof(*task));
632     if(!task)
633         return E_OUTOFMEMORY;
634
635     task->url = SysAllocString(url);
636     if(!task->url)
637         return free_doc_navigate_task(task, TRUE);
638
639     if(headers) {
640         task->headers = SysAllocString(headers);
641         if(!task->headers)
642             return free_doc_navigate_task(task, TRUE);
643     }
644
645     if(post_data) {
646         task->post_data = SafeArrayCreateVector(VT_UI1, 0, post_data_size);
647         if(!task->post_data)
648             return free_doc_navigate_task(task, TRUE);
649         memcpy(task->post_data->pvData, post_data, post_data_size);
650     }
651
652     if(!async_notif) {
653         VARIANT_BOOL cancel = VARIANT_FALSE;
654
655         on_before_navigate2(This, task->url, task->post_data, task->headers, &cancel);
656         if(cancel) {
657             TRACE("Navigation calnceled\n");
658             free_doc_navigate_task(task, TRUE);
659             return S_OK;
660         }
661     }
662
663     task->async_notif = async_notif;
664     push_dochost_task(This, &task->header, doc_navigate_proc, FALSE);
665     return S_OK;
666 }
667
668 static HRESULT navigate_bsc(DocHost *This, BindStatusCallback *bsc, IMoniker *mon)
669 {
670     VARIANT_BOOL cancel = VARIANT_FALSE;
671     SAFEARRAY *post_data = NULL;
672     IBindCtx *bindctx;
673     HRESULT hres;
674
675     set_doc_state(This, READYSTATE_LOADING);
676
677     if(bsc->post_data) {
678         post_data = SafeArrayCreateVector(VT_UI1, 0, bsc->post_data_len);
679         memcpy(post_data->pvData, post_data, bsc->post_data_len);
680     }
681
682     on_before_navigate2(This, bsc->url, post_data, bsc->headers, &cancel);
683     if(post_data)
684         SafeArrayDestroy(post_data);
685     if(cancel) {
686         FIXME("Navigation canceled\n");
687         return S_OK;
688     }
689
690     if(This->document)
691         deactivate_document(This);
692
693     CreateAsyncBindCtx(0, BINDSC(bsc), 0, &bindctx);
694
695     if(This->frame)
696         IOleInPlaceFrame_EnableModeless(This->frame, FALSE);
697
698     hres = bind_to_object(This, mon, bsc->url, bindctx, BINDSC(bsc));
699
700     if(This->frame)
701         IOleInPlaceFrame_EnableModeless(This->frame, TRUE);
702
703     IBindCtx_Release(bindctx);
704
705     return hres;
706 }
707
708 typedef struct {
709     task_header_t header;
710     BindStatusCallback *bsc;
711 } task_navigate_bsc_t;
712
713 static void navigate_bsc_proc(DocHost *This, task_header_t *t)
714 {
715     task_navigate_bsc_t *task = (task_navigate_bsc_t*)t;
716
717     if(!This->hwnd)
718         create_doc_view_hwnd(This);
719
720     navigate_bsc(This, task->bsc, NULL);
721
722     IBindStatusCallback_Release(BINDSC(task->bsc));
723 }
724
725
726 HRESULT navigate_url(DocHost *This, LPCWSTR url, const VARIANT *Flags,
727                      const VARIANT *TargetFrameName, VARIANT *PostData, VARIANT *Headers)
728 {
729     PBYTE post_data = NULL;
730     ULONG post_data_len = 0;
731     LPWSTR headers = NULL;
732     HRESULT hres = S_OK;
733
734     TRACE("navigating to %s\n", debugstr_w(url));
735
736     if((Flags && V_VT(Flags) != VT_EMPTY) 
737        || (TargetFrameName && V_VT(TargetFrameName) != VT_EMPTY))
738         FIXME("Unsupported args (Flags %p:%d; TargetFrameName %p:%d)\n",
739                 Flags, Flags ? V_VT(Flags) : -1, TargetFrameName,
740                 TargetFrameName ? V_VT(TargetFrameName) : -1);
741
742     if(PostData) {
743         TRACE("PostData vt=%d\n", V_VT(PostData));
744
745         if(V_VT(PostData) == (VT_ARRAY | VT_UI1)) {
746             SafeArrayAccessData(V_ARRAY(PostData), (void**)&post_data);
747             post_data_len = V_ARRAY(PostData)->rgsabound[0].cElements;
748         }
749     }
750
751     if(Headers && V_VT(Headers) != VT_EMPTY && V_VT(Headers) != VT_ERROR) {
752         if(V_VT(Headers) != VT_BSTR)
753             return E_INVALIDARG;
754
755         headers = V_BSTR(Headers);
756         TRACE("Headers: %s\n", debugstr_w(headers));
757     }
758
759     set_doc_state(This, READYSTATE_LOADING);
760     This->ready_state = READYSTATE_LOADING;
761
762     if(This->doc_navigate) {
763         hres = async_doc_navigate(This, url, headers, post_data, post_data_len, TRUE);
764     }else {
765         task_navigate_bsc_t *task;
766
767         task = heap_alloc(sizeof(*task));
768         task->bsc = create_callback(This, url, post_data, post_data_len, headers);
769         push_dochost_task(This, &task->header, navigate_bsc_proc, This->url == NULL);
770     }
771
772     if(post_data)
773         SafeArrayUnaccessData(V_ARRAY(PostData));
774
775     return hres;
776 }
777
778 static HRESULT navigate_hlink(DocHost *This, IMoniker *mon, IBindCtx *bindctx,
779                               IBindStatusCallback *callback)
780 {
781     IHttpNegotiate *http_negotiate;
782     BindStatusCallback *bsc;
783     PBYTE post_data = NULL;
784     ULONG post_data_len = 0;
785     LPWSTR headers = NULL, url;
786     BINDINFO bindinfo;
787     DWORD bindf = 0;
788     HRESULT hres;
789
790     hres = IMoniker_GetDisplayName(mon, 0, NULL, &url);
791     if(FAILED(hres))
792         FIXME("GetDisplayName failed: %08x\n", hres);
793
794     hres = IBindStatusCallback_QueryInterface(callback, &IID_IHttpNegotiate,
795                                               (void**)&http_negotiate);
796     if(SUCCEEDED(hres)) {
797         static const WCHAR null_string[] = {0};
798
799         IHttpNegotiate_BeginningTransaction(http_negotiate, null_string, null_string, 0,
800                                             &headers);
801         IHttpNegotiate_Release(http_negotiate);
802     }
803
804     memset(&bindinfo, 0, sizeof(bindinfo));
805     bindinfo.cbSize = sizeof(bindinfo);
806
807     hres = IBindStatusCallback_GetBindInfo(callback, &bindf, &bindinfo);
808     dump_BINDINFO(&bindinfo);
809     if(bindinfo.dwBindVerb == BINDVERB_POST) {
810         post_data_len = bindinfo.cbstgmedData;
811         if(post_data_len)
812             post_data = bindinfo.stgmedData.u.hGlobal;
813     }
814
815     if(This->doc_navigate) {
816         hres = async_doc_navigate(This, url, headers, post_data, post_data_len, FALSE);
817     }else {
818         bsc = create_callback(This, url, post_data, post_data_len, headers);
819         hres = navigate_bsc(This, bsc, mon);
820         IBindStatusCallback_Release(BINDSC(bsc));
821     }
822
823     CoTaskMemFree(url);
824     CoTaskMemFree(headers);
825     ReleaseBindInfo(&bindinfo);
826
827     return hres;
828 }
829
830 HRESULT go_home(DocHost *This)
831 {
832     HKEY hkey;
833     DWORD res, type, size;
834     WCHAR wszPageName[MAX_PATH];
835     static const WCHAR wszAboutBlank[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
836     static const WCHAR wszStartPage[] = {'S','t','a','r','t',' ','P','a','g','e',0};
837     static const WCHAR wszSubKey[] = {'S','o','f','t','w','a','r','e','\\',
838                                       'M','i','c','r','o','s','o','f','t','\\',
839                                       'I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r','\\',
840                                       'M','a','i','n',0};
841
842     res = RegOpenKeyW(HKEY_CURRENT_USER, wszSubKey, &hkey);
843     if (res != ERROR_SUCCESS)
844         return navigate_url(This, wszAboutBlank, NULL, NULL, NULL, NULL);
845
846     size = sizeof(wszPageName);
847     res = RegQueryValueExW(hkey, wszStartPage, NULL, &type, (LPBYTE)wszPageName, &size);
848     RegCloseKey(hkey);
849     if (res != ERROR_SUCCESS || type != REG_SZ)
850         return navigate_url(This, wszAboutBlank, NULL, NULL, NULL, NULL);
851
852     return navigate_url(This, wszPageName, NULL, NULL, NULL, NULL);
853 }
854
855 #define HLINKFRAME_THIS(iface) DEFINE_THIS(WebBrowser, HlinkFrame, iface)
856
857 static HRESULT WINAPI HlinkFrame_QueryInterface(IHlinkFrame *iface, REFIID riid, void **ppv)
858 {
859     WebBrowser *This = HLINKFRAME_THIS(iface);
860     return IWebBrowser2_QueryInterface(WEBBROWSER2(This), riid, ppv);
861 }
862
863 static ULONG WINAPI HlinkFrame_AddRef(IHlinkFrame *iface)
864 {
865     WebBrowser *This = HLINKFRAME_THIS(iface);
866     return IWebBrowser2_AddRef(WEBBROWSER2(This));
867 }
868
869 static ULONG WINAPI HlinkFrame_Release(IHlinkFrame *iface)
870 {
871     WebBrowser *This = HLINKFRAME_THIS(iface);
872     return IWebBrowser2_Release(WEBBROWSER2(This));
873 }
874
875 static HRESULT WINAPI HlinkFrame_SetBrowseContext(IHlinkFrame *iface,
876                                                   IHlinkBrowseContext *pihlbc)
877 {
878     WebBrowser *This = HLINKFRAME_THIS(iface);
879     FIXME("(%p)->(%p)\n", This, pihlbc);
880     return E_NOTIMPL;
881 }
882
883 static HRESULT WINAPI HlinkFrame_GetBrowseContext(IHlinkFrame *iface,
884                                                   IHlinkBrowseContext **ppihlbc)
885 {
886     WebBrowser *This = HLINKFRAME_THIS(iface);
887     FIXME("(%p)->(%p)\n", This, ppihlbc);
888     return E_NOTIMPL;
889 }
890
891 static HRESULT WINAPI HlinkFrame_Navigate(IHlinkFrame *iface, DWORD grfHLNF, LPBC pbc,
892                                           IBindStatusCallback *pibsc, IHlink *pihlNavigate)
893 {
894     WebBrowser *This = HLINKFRAME_THIS(iface);
895     IMoniker *mon;
896     LPWSTR location = NULL;
897
898     TRACE("(%p)->(%08x %p %p %p)\n", This, grfHLNF, pbc, pibsc, pihlNavigate);
899
900     if(grfHLNF)
901         FIXME("unsupported grfHLNF=%08x\n", grfHLNF);
902
903     /* Windows calls GetTargetFrameName here. */
904
905     IHlink_GetMonikerReference(pihlNavigate, 1, &mon, &location);
906
907     if(location) {
908         FIXME("location = %s\n", debugstr_w(location));
909         CoTaskMemFree(location);
910     }
911
912     /* Windows calls GetHlinkSite here */
913
914     if(grfHLNF & HLNF_OPENINNEWWINDOW) {
915         FIXME("Not supported HLNF_OPENINNEWWINDOW\n");
916         return E_NOTIMPL;
917     }
918
919     return navigate_hlink(&This->doc_host, mon, pbc, pibsc);
920 }
921
922 static HRESULT WINAPI HlinkFrame_OnNavigate(IHlinkFrame *iface, DWORD grfHLNF,
923         IMoniker *pimkTarget, LPCWSTR pwzLocation, LPCWSTR pwzFriendlyName, DWORD dwreserved)
924 {
925     WebBrowser *This = HLINKFRAME_THIS(iface);
926     FIXME("(%p)->(%08x %p %s %s %d)\n", This, grfHLNF, pimkTarget, debugstr_w(pwzLocation),
927           debugstr_w(pwzFriendlyName), dwreserved);
928     return E_NOTIMPL;
929 }
930
931 static HRESULT WINAPI HlinkFrame_UpdateHlink(IHlinkFrame *iface, ULONG uHLID,
932         IMoniker *pimkTarget, LPCWSTR pwzLocation, LPCWSTR pwzFriendlyName)
933 {
934     WebBrowser *This = HLINKFRAME_THIS(iface);
935     FIXME("(%p)->(%u %p %s %s)\n", This, uHLID, pimkTarget, debugstr_w(pwzLocation),
936           debugstr_w(pwzFriendlyName));
937     return E_NOTIMPL;
938 }
939
940 #undef HLINKFRAME_THIS
941
942 static const IHlinkFrameVtbl HlinkFrameVtbl = {
943     HlinkFrame_QueryInterface,
944     HlinkFrame_AddRef,
945     HlinkFrame_Release,
946     HlinkFrame_SetBrowseContext,
947     HlinkFrame_GetBrowseContext,
948     HlinkFrame_Navigate,
949     HlinkFrame_OnNavigate,
950     HlinkFrame_UpdateHlink
951 };
952
953 #define TARGETFRAME2_THIS(iface) DEFINE_THIS(WebBrowser, ITargetFrame2, iface)
954
955 static HRESULT WINAPI TargetFrame2_QueryInterface(ITargetFrame2 *iface, REFIID riid, void **ppv)
956 {
957     WebBrowser *This = TARGETFRAME2_THIS(iface);
958     return IWebBrowser2_QueryInterface(WEBBROWSER2(This), riid, ppv);
959 }
960
961 static ULONG WINAPI TargetFrame2_AddRef(ITargetFrame2 *iface)
962 {
963     WebBrowser *This = TARGETFRAME2_THIS(iface);
964     return IWebBrowser2_AddRef(WEBBROWSER2(This));
965 }
966
967 static ULONG WINAPI TargetFrame2_Release(ITargetFrame2 *iface)
968 {
969     WebBrowser *This = TARGETFRAME2_THIS(iface);
970     return IWebBrowser2_Release(WEBBROWSER2(This));
971 }
972
973 static HRESULT WINAPI TargetFrame2_SetFrameName(ITargetFrame2 *iface, LPCWSTR pszFrameName)
974 {
975     WebBrowser *This = TARGETFRAME2_THIS(iface);
976     FIXME("(%p)->(%s)\n", This, debugstr_w(pszFrameName));
977     return E_NOTIMPL;
978 }
979
980 static HRESULT WINAPI TargetFrame2_GetFrameName(ITargetFrame2 *iface, LPWSTR *ppszFrameName)
981 {
982     WebBrowser *This = TARGETFRAME2_THIS(iface);
983     FIXME("(%p)->(%p)\n", This, ppszFrameName);
984     return E_NOTIMPL;
985 }
986
987 static HRESULT WINAPI TargetFrame2_GetParentFrame(ITargetFrame2 *iface, IUnknown **ppunkParent)
988 {
989     WebBrowser *This = TARGETFRAME2_THIS(iface);
990     FIXME("(%p)->(%p)\n", This, ppunkParent);
991     return E_NOTIMPL;
992 }
993
994 static HRESULT WINAPI TargetFrame2_SetFrameSrc(ITargetFrame2 *iface, LPCWSTR pszFrameSrc)
995 {
996     WebBrowser *This = TARGETFRAME2_THIS(iface);
997     FIXME("(%p)->(%s)\n", This, debugstr_w(pszFrameSrc));
998     return E_NOTIMPL;
999 }
1000
1001 static HRESULT WINAPI TargetFrame2_GetFrameSrc(ITargetFrame2 *iface, LPWSTR *ppszFrameSrc)
1002 {
1003     WebBrowser *This = TARGETFRAME2_THIS(iface);
1004     FIXME("(%p)->()\n", This);
1005     return E_NOTIMPL;
1006 }
1007
1008 static HRESULT WINAPI TargetFrame2_GetFramesContainer(ITargetFrame2 *iface, IOleContainer **ppContainer)
1009 {
1010     WebBrowser *This = TARGETFRAME2_THIS(iface);
1011     FIXME("(%p)->(%p)\n", This, ppContainer);
1012     return E_NOTIMPL;
1013 }
1014
1015 static HRESULT WINAPI TargetFrame2_SetFrameOptions(ITargetFrame2 *iface, DWORD dwFlags)
1016 {
1017     WebBrowser *This = TARGETFRAME2_THIS(iface);
1018     FIXME("(%p)->(%x)\n", This, dwFlags);
1019     return E_NOTIMPL;
1020 }
1021
1022 static HRESULT WINAPI TargetFrame2_GetFrameOptions(ITargetFrame2 *iface, DWORD *pdwFlags)
1023 {
1024     WebBrowser *This = TARGETFRAME2_THIS(iface);
1025     FIXME("(%p)->(%p)\n", This, pdwFlags);
1026     return E_NOTIMPL;
1027 }
1028
1029 static HRESULT WINAPI TargetFrame2_SetFrameMargins(ITargetFrame2 *iface, DWORD dwWidth, DWORD dwHeight)
1030 {
1031     WebBrowser *This = TARGETFRAME2_THIS(iface);
1032     FIXME("(%p)->(%d %d)\n", This, dwWidth, dwHeight);
1033     return E_NOTIMPL;
1034 }
1035
1036 static HRESULT WINAPI TargetFrame2_GetFrameMargins(ITargetFrame2 *iface, DWORD *pdwWidth, DWORD *pdwHeight)
1037 {
1038     WebBrowser *This = TARGETFRAME2_THIS(iface);
1039     FIXME("(%p)->(%p %p)\n", This, pdwWidth, pdwHeight);
1040     return E_NOTIMPL;
1041 }
1042
1043 static HRESULT WINAPI TargetFrame2_FindFrame(ITargetFrame2 *iface, LPCWSTR pszTargetName, DWORD dwFlags, IUnknown **ppunkTargetFrame)
1044 {
1045     WebBrowser *This = TARGETFRAME2_THIS(iface);
1046     FIXME("(%p)->(%s %x %p)\n", This, debugstr_w(pszTargetName), dwFlags, ppunkTargetFrame);
1047     return E_NOTIMPL;
1048 }
1049
1050 static HRESULT WINAPI TargetFrame2_GetTargetAlias(ITargetFrame2 *iface, LPCWSTR pszTargetName, LPWSTR *ppszTargetAlias)
1051 {
1052     WebBrowser *This = TARGETFRAME2_THIS(iface);
1053     FIXME("(%p)->(%s %p)\n", This, debugstr_w(pszTargetName), ppszTargetAlias);
1054     return E_NOTIMPL;
1055 }
1056
1057 #undef TARGETFRAME2_THIS
1058
1059 static const ITargetFrame2Vtbl TargetFrame2Vtbl = {
1060     TargetFrame2_QueryInterface,
1061     TargetFrame2_AddRef,
1062     TargetFrame2_Release,
1063     TargetFrame2_SetFrameName,
1064     TargetFrame2_GetFrameName,
1065     TargetFrame2_GetParentFrame,
1066     TargetFrame2_SetFrameSrc,
1067     TargetFrame2_GetFrameSrc,
1068     TargetFrame2_GetFramesContainer,
1069     TargetFrame2_SetFrameOptions,
1070     TargetFrame2_GetFrameOptions,
1071     TargetFrame2_SetFrameMargins,
1072     TargetFrame2_GetFrameMargins,
1073     TargetFrame2_FindFrame,
1074     TargetFrame2_GetTargetAlias
1075 };
1076
1077 void WebBrowser_HlinkFrame_Init(WebBrowser *This)
1078 {
1079     This->lpHlinkFrameVtbl = &HlinkFrameVtbl;
1080     This->lpITargetFrame2Vtbl = &TargetFrame2Vtbl;
1081 }