ddraw: Destroy the swapchain in the DllMain() cleanup handler.
[wine/zf.git] / dlls / ieframe / navigate.c
blob6c0ce41df25eae3ef0c48478b9de23e04a164433
1 /*
2 * Copyright 2006-2007 Jacek Caban for CodeWeavers
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.
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.
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
19 #define NONAMELESSUNION
20 #define NONAMELESSSTRUCT
22 #include "ieframe.h"
24 #include "exdispid.h"
25 #include "shellapi.h"
26 #include "winreg.h"
27 #include "shlwapi.h"
28 #include "wininet.h"
29 #include "mshtml.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(ieframe);
35 static const WCHAR emptyW[] = {0};
37 typedef struct {
38 IBindStatusCallback IBindStatusCallback_iface;
39 IHttpNegotiate IHttpNegotiate_iface;
41 LONG ref;
43 DocHost *doc_host;
44 IBinding *binding;
46 LPWSTR url;
47 HGLOBAL post_data;
48 BSTR headers;
49 ULONG post_data_len;
50 } BindStatusCallback;
52 static void dump_BINDINFO(BINDINFO *bi)
54 static const char * const BINDINFOF_str[] = {
55 "#0",
56 "BINDINFOF_URLENCODESTGMEDDATA",
57 "BINDINFOF_URLENCODEDEXTRAINFO"
60 static const char * const BINDVERB_str[] = {
61 "BINDVERB_GET",
62 "BINDVERB_POST",
63 "BINDVERB_PUT",
64 "BINDVERB_CUSTOM"
67 TRACE("\n"
68 "BINDINFO = {\n"
69 " %d, %s,\n"
70 " {%d, %p, %p},\n"
71 " %s,\n"
72 " %s,\n"
73 " %s,\n"
74 " %d, %08x, %d, %d\n"
75 " {%d %p %x},\n"
76 " %s\n"
77 " %p, %d\n"
78 "}\n",
80 bi->cbSize, debugstr_w(bi->szExtraInfo),
81 bi->stgmedData.tymed, bi->stgmedData.u.hGlobal, bi->stgmedData.pUnkForRelease,
82 bi->grfBindInfoF > BINDINFOF_URLENCODEDEXTRAINFO
83 ? "unknown" : BINDINFOF_str[bi->grfBindInfoF],
84 bi->dwBindVerb > BINDVERB_CUSTOM
85 ? "unknown" : BINDVERB_str[bi->dwBindVerb],
86 debugstr_w(bi->szCustomVerb),
87 bi->cbstgmedData, bi->dwOptions, bi->dwOptionsFlags, bi->dwCodePage,
88 bi->securityAttributes.nLength,
89 bi->securityAttributes.lpSecurityDescriptor,
90 bi->securityAttributes.bInheritHandle,
91 debugstr_guid(&bi->iid),
92 bi->pUnk, bi->dwReserved
96 static void set_status_text(BindStatusCallback *This, LPCWSTR str)
98 VARIANTARG arg;
99 DISPPARAMS dispparams = {&arg, NULL, 1, 0};
101 if(!This->doc_host)
102 return;
104 V_VT(&arg) = VT_BSTR;
105 V_BSTR(&arg) = str ? SysAllocString(str) : NULL;
106 call_sink(This->doc_host->cps.wbe2, DISPID_STATUSTEXTCHANGE, &dispparams);
107 VariantClear(&arg);
109 if(This->doc_host->frame)
110 IOleInPlaceFrame_SetStatusText(This->doc_host->frame, str);
113 HRESULT set_dochost_url(DocHost *This, const WCHAR *url)
115 WCHAR *new_url;
117 if(url) {
118 new_url = heap_strdupW(url);
119 if(!new_url)
120 return E_OUTOFMEMORY;
121 }else {
122 new_url = NULL;
125 heap_free(This->url);
126 This->url = new_url;
128 This->container_vtbl->SetURL(This, This->url);
129 return S_OK;
132 static inline BindStatusCallback *impl_from_IBindStatusCallback(IBindStatusCallback *iface)
134 return CONTAINING_RECORD(iface, BindStatusCallback, IBindStatusCallback_iface);
137 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
138 REFIID riid, void **ppv)
140 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
142 if(IsEqualGUID(&IID_IUnknown, riid)) {
143 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
144 *ppv = &This->IBindStatusCallback_iface;
145 }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
146 TRACE("(%p)->(IID_IBindStatusCallback %p)\n", This, ppv);
147 *ppv = &This->IBindStatusCallback_iface;
148 }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
149 TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
150 *ppv = &This->IHttpNegotiate_iface;
151 }else {
152 *ppv = NULL;
153 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
154 return E_NOINTERFACE;
157 IUnknown_AddRef((IUnknown*)*ppv);
158 return S_OK;
161 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
163 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
164 LONG ref = InterlockedIncrement(&This->ref);
166 TRACE("(%p) ref=%d\n", This, ref);
168 return ref;
171 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
173 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
174 LONG ref = InterlockedDecrement(&This->ref);
176 TRACE("(%p) ref=%d\n", This, ref);
178 if(!ref) {
179 if(This->doc_host)
180 IOleClientSite_Release(&This->doc_host->IOleClientSite_iface);
181 if(This->binding)
182 IBinding_Release(This->binding);
183 if(This->post_data)
184 GlobalFree(This->post_data);
185 SysFreeString(This->headers);
186 heap_free(This->url);
187 heap_free(This);
190 return ref;
193 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
194 DWORD dwReserved, IBinding *pbind)
196 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
198 TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
200 This->binding = pbind;
201 IBinding_AddRef(This->binding);
203 return S_OK;
206 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface,
207 LONG *pnPriority)
209 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
210 FIXME("(%p)->(%p)\n", This, pnPriority);
211 return E_NOTIMPL;
214 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface,
215 DWORD reserved)
217 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
218 FIXME("(%p)->(%d)\n", This, reserved);
219 return E_NOTIMPL;
222 static DWORD get_http_status_code(IBinding *binding)
224 IWinInetHttpInfo *http_info;
225 DWORD status, size = sizeof(DWORD);
226 HRESULT hres;
228 hres = IBinding_QueryInterface(binding, &IID_IWinInetHttpInfo, (void**)&http_info);
229 if(FAILED(hres))
230 return HTTP_STATUS_OK;
232 hres = IWinInetHttpInfo_QueryInfo(http_info, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER,
233 &status, &size, NULL, NULL);
234 IWinInetHttpInfo_Release(http_info);
236 if(FAILED(hres))
237 return HTTP_STATUS_OK;
238 return status;
241 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface,
242 ULONG ulProgress, ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
244 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
245 DWORD status_code;
247 TRACE("(%p)->(%d %d %d %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
248 debugstr_w(szStatusText));
250 switch(ulStatusCode) {
251 case BINDSTATUS_REDIRECTING:
252 return set_dochost_url(This->doc_host, szStatusText);
253 case BINDSTATUS_BEGINDOWNLOADDATA:
254 set_status_text(This, szStatusText); /* FIXME: "Start downloading from site: %s" */
255 status_code = get_http_status_code(This->binding);
256 if(status_code != HTTP_STATUS_OK)
257 handle_navigation_error(This->doc_host, status_code, This->url, NULL);
258 return S_OK;
259 case BINDSTATUS_ENDDOWNLOADDATA:
260 set_status_text(This, szStatusText); /* FIXME: "Downloading from site: %s" */
261 return S_OK;
262 case BINDSTATUS_CLASSIDAVAILABLE:
263 case BINDSTATUS_MIMETYPEAVAILABLE:
264 case BINDSTATUS_BEGINSYNCOPERATION:
265 case BINDSTATUS_ENDSYNCOPERATION:
266 return S_OK;
267 default:
268 FIXME("status code %u\n", ulStatusCode);
271 return S_OK;
274 void handle_navigation_error(DocHost* doc_host, HRESULT hres, BSTR url, IHTMLWindow2 *win2)
276 VARIANT var_status_code, var_frame_name, var_url;
277 DISPPARAMS dispparams;
278 VARIANTARG params[5];
279 VARIANT_BOOL cancel = VARIANT_FALSE;
281 dispparams.cArgs = 5;
282 dispparams.cNamedArgs = 0;
283 dispparams.rgdispidNamedArgs = NULL;
284 dispparams.rgvarg = params;
286 V_VT(params) = VT_BOOL|VT_BYREF;
287 V_BOOLREF(params) = &cancel;
289 V_VT(params+1) = VT_VARIANT|VT_BYREF;
290 V_VARIANTREF(params+1) = &var_status_code;
291 V_VT(&var_status_code) = VT_I4;
292 V_I4(&var_status_code) = hres;
294 V_VT(params+2) = VT_VARIANT|VT_BYREF;
295 V_VARIANTREF(params+2) = &var_frame_name;
296 V_VT(&var_frame_name) = VT_BSTR;
297 if(win2) {
298 hres = IHTMLWindow2_get_name(win2, &V_BSTR(&var_frame_name));
299 if(FAILED(hres))
300 V_BSTR(&var_frame_name) = NULL;
301 } else
302 V_BSTR(&var_frame_name) = NULL;
304 V_VT(params+3) = VT_VARIANT|VT_BYREF;
305 V_VARIANTREF(params+3) = &var_url;
306 V_VT(&var_url) = VT_BSTR;
307 V_BSTR(&var_url) = url;
309 V_VT(params+4) = VT_DISPATCH;
310 V_DISPATCH(params+4) = doc_host->disp;
312 call_sink(doc_host->cps.wbe2, DISPID_NAVIGATEERROR, &dispparams);
313 SysFreeString(V_BSTR(&var_frame_name));
315 if(!cancel)
316 FIXME("Navigate to error page\n");
319 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
320 HRESULT hresult, LPCWSTR szError)
322 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
324 TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
326 set_status_text(This, emptyW);
328 if(!This->doc_host)
329 return S_OK;
331 if(FAILED(hresult))
332 handle_navigation_error(This->doc_host, hresult, This->url, NULL);
334 IOleClientSite_Release(&This->doc_host->IOleClientSite_iface);
335 This->doc_host = NULL;
337 IBinding_Release(This->binding);
338 This->binding = NULL;
340 return S_OK;
343 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
344 DWORD *grfBINDF, BINDINFO *pbindinfo)
346 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
348 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
350 *grfBINDF = BINDF_ASYNCHRONOUS;
352 if(This->post_data) {
353 pbindinfo->dwBindVerb = BINDVERB_POST;
355 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
356 pbindinfo->stgmedData.u.hGlobal = This->post_data;
357 pbindinfo->cbstgmedData = This->post_data_len;
358 pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)&This->IBindStatusCallback_iface;
359 IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
362 return S_OK;
365 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
366 DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
368 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
369 FIXME("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
370 return E_NOTIMPL;
373 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
374 REFIID riid, IUnknown *punk)
376 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
378 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
380 return dochost_object_available(This->doc_host, punk);
383 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
384 BindStatusCallback_QueryInterface,
385 BindStatusCallback_AddRef,
386 BindStatusCallback_Release,
387 BindStatusCallback_OnStartBinding,
388 BindStatusCallback_GetPriority,
389 BindStatusCallback_OnLowResource,
390 BindStatusCallback_OnProgress,
391 BindStatusCallback_OnStopBinding,
392 BindStatusCallback_GetBindInfo,
393 BindStatusCallback_OnDataAvailable,
394 BindStatusCallback_OnObjectAvailable
397 static inline BindStatusCallback *impl_from_IHttpNegotiate(IHttpNegotiate *iface)
399 return CONTAINING_RECORD(iface, BindStatusCallback, IHttpNegotiate_iface);
402 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate *iface,
403 REFIID riid, void **ppv)
405 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
406 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
409 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate *iface)
411 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
412 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
415 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate *iface)
417 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
418 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
421 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate *iface,
422 LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
424 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
426 TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
427 dwReserved, pszAdditionalHeaders);
429 if(This->headers) {
430 int size = (strlenW(This->headers)+1)*sizeof(WCHAR);
431 *pszAdditionalHeaders = CoTaskMemAlloc(size);
432 memcpy(*pszAdditionalHeaders, This->headers, size);
435 return S_OK;
438 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate *iface,
439 DWORD dwResponseCode, LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders,
440 LPWSTR *pszAdditionalRequestHeaders)
442 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
443 TRACE("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
444 debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
445 return S_OK;
448 static const IHttpNegotiateVtbl HttpNegotiateVtbl = {
449 HttpNegotiate_QueryInterface,
450 HttpNegotiate_AddRef,
451 HttpNegotiate_Release,
452 HttpNegotiate_BeginningTransaction,
453 HttpNegotiate_OnResponse
456 static BindStatusCallback *create_callback(DocHost *doc_host, LPCWSTR url, PBYTE post_data,
457 ULONG post_data_len, LPCWSTR headers)
459 BindStatusCallback *ret = heap_alloc(sizeof(BindStatusCallback));
461 ret->IBindStatusCallback_iface.lpVtbl = &BindStatusCallbackVtbl;
462 ret->IHttpNegotiate_iface.lpVtbl = &HttpNegotiateVtbl;
464 ret->ref = 1;
465 ret->url = heap_strdupW(url);
466 ret->post_data = NULL;
467 ret->post_data_len = post_data_len;
468 ret->headers = headers ? SysAllocString(headers) : NULL;
470 ret->doc_host = doc_host;
471 IOleClientSite_AddRef(&doc_host->IOleClientSite_iface);
473 ret->binding = NULL;
475 if(post_data) {
476 ret->post_data = GlobalAlloc(0, post_data_len);
477 memcpy(ret->post_data, post_data, post_data_len);
480 return ret;
483 static void on_before_navigate2(DocHost *This, LPCWSTR url, SAFEARRAY *post_data, LPWSTR headers, VARIANT_BOOL *cancel)
485 VARIANT var_url, var_flags, var_frame_name, var_post_data, var_post_data2, var_headers;
486 DISPPARAMS dispparams;
487 VARIANTARG params[7];
489 dispparams.cArgs = 7;
490 dispparams.cNamedArgs = 0;
491 dispparams.rgdispidNamedArgs = NULL;
492 dispparams.rgvarg = params;
494 This->busy = VARIANT_TRUE;
496 V_VT(params) = VT_BOOL|VT_BYREF;
497 V_BOOLREF(params) = cancel;
499 V_VT(params+1) = (VT_BYREF|VT_VARIANT);
500 V_VARIANTREF(params+1) = &var_headers;
501 V_VT(&var_headers) = VT_BSTR;
502 V_BSTR(&var_headers) = headers;
504 V_VT(params+2) = (VT_BYREF|VT_VARIANT);
505 V_VARIANTREF(params+2) = &var_post_data2;
506 V_VT(&var_post_data2) = (VT_BYREF|VT_VARIANT);
507 V_VARIANTREF(&var_post_data2) = &var_post_data;
509 if(post_data) {
510 V_VT(&var_post_data) = VT_UI1|VT_ARRAY;
511 V_ARRAY(&var_post_data) = post_data;
512 }else {
513 V_VT(&var_post_data) = VT_EMPTY;
516 V_VT(params+3) = (VT_BYREF|VT_VARIANT);
517 V_VARIANTREF(params+3) = &var_frame_name;
518 V_VT(&var_frame_name) = VT_BSTR;
519 V_BSTR(&var_frame_name) = NULL;
521 V_VT(params+4) = (VT_BYREF|VT_VARIANT);
522 V_VARIANTREF(params+4) = &var_flags;
523 V_VT(&var_flags) = VT_I4;
524 V_I4(&var_flags) = 0;
526 V_VT(params+5) = (VT_BYREF|VT_VARIANT);
527 V_VARIANTREF(params+5) = &var_url;
528 V_VT(&var_url) = VT_BSTR;
529 V_BSTR(&var_url) = SysAllocString(url);
531 V_VT(params+6) = (VT_DISPATCH);
532 V_DISPATCH(params+6) = This->disp;
534 call_sink(This->cps.wbe2, DISPID_BEFORENAVIGATE2, &dispparams);
536 SysFreeString(V_BSTR(&var_url));
539 /* FIXME: urlmon should handle it */
540 static BOOL try_application_url(LPCWSTR url)
542 SHELLEXECUTEINFOW exec_info;
543 WCHAR app[64];
544 HKEY hkey;
545 DWORD res, type;
546 HRESULT hres;
548 static const WCHAR wszURLProtocol[] = {'U','R','L',' ','P','r','o','t','o','c','o','l',0};
550 hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, app, sizeof(app)/sizeof(WCHAR), NULL, 0);
551 if(FAILED(hres))
552 return FALSE;
554 res = RegOpenKeyW(HKEY_CLASSES_ROOT, app, &hkey);
555 if(res != ERROR_SUCCESS)
556 return FALSE;
558 res = RegQueryValueExW(hkey, wszURLProtocol, NULL, &type, NULL, NULL);
559 RegCloseKey(hkey);
560 if(res != ERROR_SUCCESS || type != REG_SZ)
561 return FALSE;
563 TRACE("opening application %s\n", debugstr_w(app));
565 memset(&exec_info, 0, sizeof(exec_info));
566 exec_info.cbSize = sizeof(exec_info);
567 exec_info.lpFile = url;
568 exec_info.nShow = SW_SHOW;
570 return ShellExecuteExW(&exec_info);
573 static HRESULT create_moniker(LPCWSTR url, IMoniker **mon)
575 WCHAR new_url[INTERNET_MAX_URL_LENGTH];
576 DWORD size;
577 HRESULT hres;
579 if(PathIsURLW(url))
580 return CreateURLMoniker(NULL, url, mon);
582 size = sizeof(new_url)/sizeof(WCHAR);
583 hres = UrlApplySchemeW(url, new_url, &size, URL_APPLY_GUESSSCHEME | URL_APPLY_GUESSFILE);
584 TRACE("was %s got %s\n", debugstr_w(url), debugstr_w(new_url));
585 if(FAILED(hres)) {
586 WARN("UrlApplyScheme failed: %08x\n", hres);
587 return hres;
590 return CreateURLMoniker(NULL, new_url, mon);
593 static HRESULT bind_to_object(DocHost *This, IMoniker *mon, LPCWSTR url, IBindCtx *bindctx,
594 IBindStatusCallback *callback)
596 IUnknown *unk = NULL;
597 WCHAR *display_name;
598 HRESULT hres;
600 if(mon) {
601 IMoniker_AddRef(mon);
602 }else {
603 hres = create_moniker(url, &mon);
604 if(FAILED(hres))
605 return hres;
608 hres = IMoniker_GetDisplayName(mon, 0, NULL, &display_name);
609 if(FAILED(hres)) {
610 FIXME("GetDisplayName failed: %08x\n", hres);
611 return hres;
614 hres = set_dochost_url(This, display_name);
615 CoTaskMemFree(display_name);
616 if(FAILED(hres))
617 return hres;
619 IBindCtx_RegisterObjectParam(bindctx, (LPOLESTR)SZ_HTML_CLIENTSITE_OBJECTPARAM,
620 (IUnknown*)&This->IOleClientSite_iface);
622 hres = IMoniker_BindToObject(mon, bindctx, NULL, &IID_IUnknown, (void**)&unk);
623 if(SUCCEEDED(hres)) {
624 hres = S_OK;
625 if(unk)
626 IUnknown_Release(unk);
627 }else if(try_application_url(url)) {
628 hres = S_OK;
629 }else {
630 FIXME("BindToObject failed: %08x\n", hres);
633 IMoniker_Release(mon);
634 return S_OK;
637 static void html_window_navigate(DocHost *This, IHTMLPrivateWindow *window, BSTR url, BSTR headers, SAFEARRAY *post_data)
639 VARIANT headers_var, post_data_var;
640 BSTR empty_str;
641 HRESULT hres;
643 hres = set_dochost_url(This, url);
644 if(FAILED(hres))
645 return;
647 empty_str = SysAllocStringLen(NULL, 0);
649 if(headers) {
650 V_VT(&headers_var) = VT_BSTR;
651 V_BSTR(&headers_var) = headers;
652 }else {
653 V_VT(&headers_var) = VT_EMPTY;
656 if(post_data) {
657 V_VT(&post_data_var) = VT_UI1|VT_ARRAY;
658 V_ARRAY(&post_data_var) = post_data;
659 }else {
660 V_VT(&post_data_var) = VT_EMPTY;
663 set_doc_state(This, READYSTATE_LOADING);
664 hres = IHTMLPrivateWindow_SuperNavigate(window, url, empty_str, NULL, NULL, &post_data_var, &headers_var, 0);
665 SysFreeString(empty_str);
666 if(FAILED(hres))
667 WARN("SuprtNavigate failed: %08x\n", hres);
670 typedef struct {
671 task_header_t header;
672 BSTR url;
673 BSTR headers;
674 SAFEARRAY *post_data;
675 BOOL async_notif;
676 } task_doc_navigate_t;
678 static void doc_navigate_task_destr(task_header_t *t)
680 task_doc_navigate_t *task = (task_doc_navigate_t*)t;
682 SysFreeString(task->url);
683 SysFreeString(task->headers);
684 if(task->post_data)
685 SafeArrayDestroy(task->post_data);
686 heap_free(task);
689 static void doc_navigate_proc(DocHost *This, task_header_t *t)
691 task_doc_navigate_t *task = (task_doc_navigate_t*)t;
692 IHTMLPrivateWindow *priv_window;
693 HRESULT hres;
695 if(!This->doc_navigate) {
696 ERR("Skip nav\n");
697 return;
700 if(task->async_notif) {
701 VARIANT_BOOL cancel = VARIANT_FALSE;
702 on_before_navigate2(This, task->url, task->post_data, task->headers, &cancel);
703 if(cancel) {
704 TRACE("Navigation canceled\n");
705 return;
709 hres = IUnknown_QueryInterface(This->doc_navigate, &IID_IHTMLPrivateWindow, (void**)&priv_window);
710 if(SUCCEEDED(hres)) {
711 html_window_navigate(This, priv_window, task->url, task->headers, task->post_data);
712 IHTMLPrivateWindow_Release(priv_window);
713 }else {
714 WARN("Could not get IHTMLPrivateWindow iface: %08x\n", hres);
718 static HRESULT async_doc_navigate(DocHost *This, LPCWSTR url, LPCWSTR headers, PBYTE post_data, ULONG post_data_size,
719 BOOL async_notif)
721 task_doc_navigate_t *task;
723 TRACE("%s\n", debugstr_w(url));
725 task = heap_alloc_zero(sizeof(*task));
726 if(!task)
727 return E_OUTOFMEMORY;
729 task->url = SysAllocString(url);
730 if(!task->url) {
731 doc_navigate_task_destr(&task->header);
732 return E_OUTOFMEMORY;
735 if(headers) {
736 task->headers = SysAllocString(headers);
737 if(!task->headers) {
738 doc_navigate_task_destr(&task->header);
739 return E_OUTOFMEMORY;
743 if(post_data) {
744 task->post_data = SafeArrayCreateVector(VT_UI1, 0, post_data_size);
745 if(!task->post_data) {
746 doc_navigate_task_destr(&task->header);
747 return E_OUTOFMEMORY;
750 memcpy(task->post_data->pvData, post_data, post_data_size);
753 if(!async_notif) {
754 VARIANT_BOOL cancel = VARIANT_FALSE;
756 on_before_navigate2(This, task->url, task->post_data, task->headers, &cancel);
757 if(cancel) {
758 TRACE("Navigation canceled\n");
759 doc_navigate_task_destr(&task->header);
760 return S_OK;
764 task->async_notif = async_notif;
765 abort_dochost_tasks(This, doc_navigate_proc);
766 push_dochost_task(This, &task->header, doc_navigate_proc, doc_navigate_task_destr, FALSE);
767 return S_OK;
770 static HRESULT navigate_bsc(DocHost *This, BindStatusCallback *bsc, IMoniker *mon)
772 VARIANT_BOOL cancel = VARIANT_FALSE;
773 SAFEARRAY *post_data = NULL;
774 IBindCtx *bindctx;
775 HRESULT hres;
777 set_doc_state(This, READYSTATE_LOADING);
779 if(bsc->post_data) {
780 post_data = SafeArrayCreateVector(VT_UI1, 0, bsc->post_data_len);
781 memcpy(post_data->pvData, post_data, bsc->post_data_len);
784 on_before_navigate2(This, bsc->url, post_data, bsc->headers, &cancel);
785 if(post_data)
786 SafeArrayDestroy(post_data);
787 if(cancel) {
788 FIXME("Navigation canceled\n");
789 return S_OK;
792 if(This->document)
793 deactivate_document(This);
795 CreateAsyncBindCtx(0, &bsc->IBindStatusCallback_iface, 0, &bindctx);
797 if(This->frame)
798 IOleInPlaceFrame_EnableModeless(This->frame, FALSE);
800 hres = bind_to_object(This, mon, bsc->url, bindctx, &bsc->IBindStatusCallback_iface);
802 if(This->frame)
803 IOleInPlaceFrame_EnableModeless(This->frame, TRUE);
805 IBindCtx_Release(bindctx);
807 return hres;
810 typedef struct {
811 task_header_t header;
812 BindStatusCallback *bsc;
813 } task_navigate_bsc_t;
815 static void navigate_bsc_task_destr(task_header_t *t)
817 task_navigate_bsc_t *task = (task_navigate_bsc_t*)t;
819 IBindStatusCallback_Release(&task->bsc->IBindStatusCallback_iface);
820 heap_free(task);
823 static void navigate_bsc_proc(DocHost *This, task_header_t *t)
825 task_navigate_bsc_t *task = (task_navigate_bsc_t*)t;
827 if(!This->hwnd)
828 create_doc_view_hwnd(This);
830 navigate_bsc(This, task->bsc, NULL);
834 HRESULT navigate_url(DocHost *This, LPCWSTR url, const VARIANT *Flags,
835 const VARIANT *TargetFrameName, VARIANT *PostData, VARIANT *Headers)
837 PBYTE post_data = NULL;
838 ULONG post_data_len = 0;
839 LPWSTR headers = NULL;
840 HRESULT hres = S_OK;
842 TRACE("navigating to %s\n", debugstr_w(url));
844 if((Flags && V_VT(Flags) != VT_EMPTY)
845 || (TargetFrameName && V_VT(TargetFrameName) != VT_EMPTY))
846 FIXME("Unsupported args (Flags %p:%d; TargetFrameName %p:%d)\n",
847 Flags, Flags ? V_VT(Flags) : -1, TargetFrameName,
848 TargetFrameName ? V_VT(TargetFrameName) : -1);
850 if(PostData && V_VT(PostData) == (VT_ARRAY | VT_UI1) && V_ARRAY(PostData)) {
851 SafeArrayAccessData(V_ARRAY(PostData), (void**)&post_data);
852 post_data_len = V_ARRAY(PostData)->rgsabound[0].cElements;
855 if(Headers && V_VT(Headers) == VT_BSTR) {
856 headers = V_BSTR(Headers);
857 TRACE("Headers: %s\n", debugstr_w(headers));
860 set_doc_state(This, READYSTATE_LOADING);
861 This->ready_state = READYSTATE_LOADING;
863 if(This->doc_navigate) {
864 WCHAR new_url[INTERNET_MAX_URL_LENGTH];
866 if(PathIsURLW(url)) {
867 new_url[0] = 0;
868 }else {
869 DWORD size;
871 size = sizeof(new_url)/sizeof(WCHAR);
872 hres = UrlApplySchemeW(url, new_url, &size,
873 URL_APPLY_GUESSSCHEME | URL_APPLY_GUESSFILE | URL_APPLY_DEFAULT);
874 if(FAILED(hres)) {
875 WARN("UrlApplyScheme failed: %08x\n", hres);
876 new_url[0] = 0;
880 hres = async_doc_navigate(This, *new_url ? new_url : url, headers, post_data,
881 post_data_len, TRUE);
882 }else {
883 task_navigate_bsc_t *task;
885 task = heap_alloc(sizeof(*task));
886 task->bsc = create_callback(This, url, post_data, post_data_len, headers);
887 push_dochost_task(This, &task->header, navigate_bsc_proc, navigate_bsc_task_destr, This->url == NULL);
890 if(post_data)
891 SafeArrayUnaccessData(V_ARRAY(PostData));
893 return hres;
896 static HRESULT navigate_hlink(DocHost *This, IMoniker *mon, IBindCtx *bindctx,
897 IBindStatusCallback *callback)
899 IHttpNegotiate *http_negotiate;
900 BindStatusCallback *bsc;
901 PBYTE post_data = NULL;
902 ULONG post_data_len = 0;
903 LPWSTR headers = NULL, url;
904 BINDINFO bindinfo;
905 DWORD bindf = 0;
906 HRESULT hres;
908 TRACE("\n");
910 hres = IMoniker_GetDisplayName(mon, 0, NULL, &url);
911 if(FAILED(hres))
912 FIXME("GetDisplayName failed: %08x\n", hres);
914 hres = IBindStatusCallback_QueryInterface(callback, &IID_IHttpNegotiate,
915 (void**)&http_negotiate);
916 if(SUCCEEDED(hres)) {
917 static const WCHAR null_string[] = {0};
919 IHttpNegotiate_BeginningTransaction(http_negotiate, null_string, null_string, 0,
920 &headers);
921 IHttpNegotiate_Release(http_negotiate);
924 memset(&bindinfo, 0, sizeof(bindinfo));
925 bindinfo.cbSize = sizeof(bindinfo);
927 hres = IBindStatusCallback_GetBindInfo(callback, &bindf, &bindinfo);
928 dump_BINDINFO(&bindinfo);
929 if(bindinfo.dwBindVerb == BINDVERB_POST) {
930 post_data_len = bindinfo.cbstgmedData;
931 if(post_data_len)
932 post_data = bindinfo.stgmedData.u.hGlobal;
935 if(This->doc_navigate) {
936 hres = async_doc_navigate(This, url, headers, post_data, post_data_len, FALSE);
937 }else {
938 bsc = create_callback(This, url, post_data, post_data_len, headers);
939 hres = navigate_bsc(This, bsc, mon);
940 IBindStatusCallback_Release(&bsc->IBindStatusCallback_iface);
943 CoTaskMemFree(url);
944 CoTaskMemFree(headers);
945 ReleaseBindInfo(&bindinfo);
947 return hres;
950 HRESULT go_home(DocHost *This)
952 HKEY hkey;
953 DWORD res, type, size;
954 WCHAR wszPageName[MAX_PATH];
955 static const WCHAR wszAboutBlank[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
956 static const WCHAR wszStartPage[] = {'S','t','a','r','t',' ','P','a','g','e',0};
957 static const WCHAR wszSubKey[] = {'S','o','f','t','w','a','r','e','\\',
958 'M','i','c','r','o','s','o','f','t','\\',
959 'I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r','\\',
960 'M','a','i','n',0};
962 res = RegOpenKeyW(HKEY_CURRENT_USER, wszSubKey, &hkey);
963 if (res != ERROR_SUCCESS)
964 return navigate_url(This, wszAboutBlank, NULL, NULL, NULL, NULL);
966 size = sizeof(wszPageName);
967 res = RegQueryValueExW(hkey, wszStartPage, NULL, &type, (LPBYTE)wszPageName, &size);
968 RegCloseKey(hkey);
969 if (res != ERROR_SUCCESS || type != REG_SZ)
970 return navigate_url(This, wszAboutBlank, NULL, NULL, NULL, NULL);
972 return navigate_url(This, wszPageName, NULL, NULL, NULL, NULL);
975 HRESULT get_location_url(DocHost *This, BSTR *ret)
977 FIXME("semi-stub\n");
979 *ret = This->url ? SysAllocString(This->url) : SysAllocStringLen(NULL, 0);
980 if(!*ret)
981 return E_OUTOFMEMORY;
983 return This->url ? S_OK : S_FALSE;
986 static inline HlinkFrame *impl_from_IHlinkFrame(IHlinkFrame *iface)
988 return CONTAINING_RECORD(iface, HlinkFrame, IHlinkFrame_iface);
991 static HRESULT WINAPI HlinkFrame_QueryInterface(IHlinkFrame *iface, REFIID riid, void **ppv)
993 HlinkFrame *This = impl_from_IHlinkFrame(iface);
994 return IUnknown_QueryInterface(This->outer, riid, ppv);
997 static ULONG WINAPI HlinkFrame_AddRef(IHlinkFrame *iface)
999 HlinkFrame *This = impl_from_IHlinkFrame(iface);
1000 return IUnknown_AddRef(This->outer);
1003 static ULONG WINAPI HlinkFrame_Release(IHlinkFrame *iface)
1005 HlinkFrame *This = impl_from_IHlinkFrame(iface);
1006 return IUnknown_Release(This->outer);
1009 static HRESULT WINAPI HlinkFrame_SetBrowseContext(IHlinkFrame *iface,
1010 IHlinkBrowseContext *pihlbc)
1012 HlinkFrame *This = impl_from_IHlinkFrame(iface);
1013 FIXME("(%p)->(%p)\n", This, pihlbc);
1014 return E_NOTIMPL;
1017 static HRESULT WINAPI HlinkFrame_GetBrowseContext(IHlinkFrame *iface,
1018 IHlinkBrowseContext **ppihlbc)
1020 HlinkFrame *This = impl_from_IHlinkFrame(iface);
1021 FIXME("(%p)->(%p)\n", This, ppihlbc);
1022 return E_NOTIMPL;
1025 static HRESULT WINAPI HlinkFrame_Navigate(IHlinkFrame *iface, DWORD grfHLNF, LPBC pbc,
1026 IBindStatusCallback *pibsc, IHlink *pihlNavigate)
1028 HlinkFrame *This = impl_from_IHlinkFrame(iface);
1029 IMoniker *mon;
1030 LPWSTR location = NULL;
1032 TRACE("(%p)->(%08x %p %p %p)\n", This, grfHLNF, pbc, pibsc, pihlNavigate);
1034 if(grfHLNF)
1035 FIXME("unsupported grfHLNF=%08x\n", grfHLNF);
1037 /* Windows calls GetTargetFrameName here. */
1039 IHlink_GetMonikerReference(pihlNavigate, 1, &mon, &location);
1041 if(location) {
1042 FIXME("location = %s\n", debugstr_w(location));
1043 CoTaskMemFree(location);
1046 /* Windows calls GetHlinkSite here */
1048 if(grfHLNF & HLNF_OPENINNEWWINDOW) {
1049 FIXME("Not supported HLNF_OPENINNEWWINDOW\n");
1050 return E_NOTIMPL;
1053 return navigate_hlink(This->doc_host, mon, pbc, pibsc);
1056 static HRESULT WINAPI HlinkFrame_OnNavigate(IHlinkFrame *iface, DWORD grfHLNF,
1057 IMoniker *pimkTarget, LPCWSTR pwzLocation, LPCWSTR pwzFriendlyName, DWORD dwreserved)
1059 HlinkFrame *This = impl_from_IHlinkFrame(iface);
1060 FIXME("(%p)->(%08x %p %s %s %d)\n", This, grfHLNF, pimkTarget, debugstr_w(pwzLocation),
1061 debugstr_w(pwzFriendlyName), dwreserved);
1062 return E_NOTIMPL;
1065 static HRESULT WINAPI HlinkFrame_UpdateHlink(IHlinkFrame *iface, ULONG uHLID,
1066 IMoniker *pimkTarget, LPCWSTR pwzLocation, LPCWSTR pwzFriendlyName)
1068 HlinkFrame *This = impl_from_IHlinkFrame(iface);
1069 FIXME("(%p)->(%u %p %s %s)\n", This, uHLID, pimkTarget, debugstr_w(pwzLocation),
1070 debugstr_w(pwzFriendlyName));
1071 return E_NOTIMPL;
1074 static const IHlinkFrameVtbl HlinkFrameVtbl = {
1075 HlinkFrame_QueryInterface,
1076 HlinkFrame_AddRef,
1077 HlinkFrame_Release,
1078 HlinkFrame_SetBrowseContext,
1079 HlinkFrame_GetBrowseContext,
1080 HlinkFrame_Navigate,
1081 HlinkFrame_OnNavigate,
1082 HlinkFrame_UpdateHlink
1085 static inline HlinkFrame *impl_from_ITargetFrame2(ITargetFrame2 *iface)
1087 return CONTAINING_RECORD(iface, HlinkFrame, IHlinkFrame_iface);
1090 static HRESULT WINAPI TargetFrame2_QueryInterface(ITargetFrame2 *iface, REFIID riid, void **ppv)
1092 HlinkFrame *This = impl_from_ITargetFrame2(iface);
1093 return IUnknown_QueryInterface(This->outer, riid, ppv);
1096 static ULONG WINAPI TargetFrame2_AddRef(ITargetFrame2 *iface)
1098 HlinkFrame *This = impl_from_ITargetFrame2(iface);
1099 return IUnknown_AddRef(This->outer);
1102 static ULONG WINAPI TargetFrame2_Release(ITargetFrame2 *iface)
1104 HlinkFrame *This = impl_from_ITargetFrame2(iface);
1105 return IUnknown_Release(This->outer);
1108 static HRESULT WINAPI TargetFrame2_SetFrameName(ITargetFrame2 *iface, LPCWSTR pszFrameName)
1110 HlinkFrame *This = impl_from_ITargetFrame2(iface);
1111 FIXME("(%p)->(%s)\n", This, debugstr_w(pszFrameName));
1112 return E_NOTIMPL;
1115 static HRESULT WINAPI TargetFrame2_GetFrameName(ITargetFrame2 *iface, LPWSTR *ppszFrameName)
1117 HlinkFrame *This = impl_from_ITargetFrame2(iface);
1118 FIXME("(%p)->(%p)\n", This, ppszFrameName);
1119 return E_NOTIMPL;
1122 static HRESULT WINAPI TargetFrame2_GetParentFrame(ITargetFrame2 *iface, IUnknown **ppunkParent)
1124 HlinkFrame *This = impl_from_ITargetFrame2(iface);
1125 FIXME("(%p)->(%p)\n", This, ppunkParent);
1126 return E_NOTIMPL;
1129 static HRESULT WINAPI TargetFrame2_SetFrameSrc(ITargetFrame2 *iface, LPCWSTR pszFrameSrc)
1131 HlinkFrame *This = impl_from_ITargetFrame2(iface);
1132 FIXME("(%p)->(%s)\n", This, debugstr_w(pszFrameSrc));
1133 return E_NOTIMPL;
1136 static HRESULT WINAPI TargetFrame2_GetFrameSrc(ITargetFrame2 *iface, LPWSTR *ppszFrameSrc)
1138 HlinkFrame *This = impl_from_ITargetFrame2(iface);
1139 FIXME("(%p)->()\n", This);
1140 return E_NOTIMPL;
1143 static HRESULT WINAPI TargetFrame2_GetFramesContainer(ITargetFrame2 *iface, IOleContainer **ppContainer)
1145 HlinkFrame *This = impl_from_ITargetFrame2(iface);
1146 FIXME("(%p)->(%p)\n", This, ppContainer);
1147 return E_NOTIMPL;
1150 static HRESULT WINAPI TargetFrame2_SetFrameOptions(ITargetFrame2 *iface, DWORD dwFlags)
1152 HlinkFrame *This = impl_from_ITargetFrame2(iface);
1153 FIXME("(%p)->(%x)\n", This, dwFlags);
1154 return E_NOTIMPL;
1157 static HRESULT WINAPI TargetFrame2_GetFrameOptions(ITargetFrame2 *iface, DWORD *pdwFlags)
1159 HlinkFrame *This = impl_from_ITargetFrame2(iface);
1160 FIXME("(%p)->(%p)\n", This, pdwFlags);
1161 return E_NOTIMPL;
1164 static HRESULT WINAPI TargetFrame2_SetFrameMargins(ITargetFrame2 *iface, DWORD dwWidth, DWORD dwHeight)
1166 HlinkFrame *This = impl_from_ITargetFrame2(iface);
1167 FIXME("(%p)->(%d %d)\n", This, dwWidth, dwHeight);
1168 return E_NOTIMPL;
1171 static HRESULT WINAPI TargetFrame2_GetFrameMargins(ITargetFrame2 *iface, DWORD *pdwWidth, DWORD *pdwHeight)
1173 HlinkFrame *This = impl_from_ITargetFrame2(iface);
1174 FIXME("(%p)->(%p %p)\n", This, pdwWidth, pdwHeight);
1175 return E_NOTIMPL;
1178 static HRESULT WINAPI TargetFrame2_FindFrame(ITargetFrame2 *iface, LPCWSTR pszTargetName, DWORD dwFlags, IUnknown **ppunkTargetFrame)
1180 HlinkFrame *This = impl_from_ITargetFrame2(iface);
1181 FIXME("(%p)->(%s %x %p)\n", This, debugstr_w(pszTargetName), dwFlags, ppunkTargetFrame);
1182 return E_NOTIMPL;
1185 static HRESULT WINAPI TargetFrame2_GetTargetAlias(ITargetFrame2 *iface, LPCWSTR pszTargetName, LPWSTR *ppszTargetAlias)
1187 HlinkFrame *This = impl_from_ITargetFrame2(iface);
1188 FIXME("(%p)->(%s %p)\n", This, debugstr_w(pszTargetName), ppszTargetAlias);
1189 return E_NOTIMPL;
1192 static const ITargetFrame2Vtbl TargetFrame2Vtbl = {
1193 TargetFrame2_QueryInterface,
1194 TargetFrame2_AddRef,
1195 TargetFrame2_Release,
1196 TargetFrame2_SetFrameName,
1197 TargetFrame2_GetFrameName,
1198 TargetFrame2_GetParentFrame,
1199 TargetFrame2_SetFrameSrc,
1200 TargetFrame2_GetFrameSrc,
1201 TargetFrame2_GetFramesContainer,
1202 TargetFrame2_SetFrameOptions,
1203 TargetFrame2_GetFrameOptions,
1204 TargetFrame2_SetFrameMargins,
1205 TargetFrame2_GetFrameMargins,
1206 TargetFrame2_FindFrame,
1207 TargetFrame2_GetTargetAlias
1210 BOOL HlinkFrame_QI(HlinkFrame *This, REFIID riid, void **ppv)
1212 if(IsEqualGUID(&IID_IHlinkFrame, riid)) {
1213 TRACE("(%p)->(IID_IHlinkFrame %p)\n", This, ppv);
1214 *ppv = &This->IHlinkFrame_iface;
1215 }else if(IsEqualGUID(&IID_ITargetFrame2, riid)) {
1216 TRACE("(%p)->(IID_ITargetFrame2 %p)\n", This, ppv);
1217 *ppv = &This->ITargetFrame2_iface;
1218 }else {
1219 return FALSE;
1222 IUnknown_AddRef((IUnknown*)*ppv);
1223 return TRUE;
1226 void HlinkFrame_Init(HlinkFrame *This, IUnknown *outer, DocHost *doc_host)
1228 This->IHlinkFrame_iface.lpVtbl = &HlinkFrameVtbl;
1229 This->ITargetFrame2_iface.lpVtbl = &TargetFrame2Vtbl;
1231 This->outer = outer;
1232 This->doc_host = doc_host;