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