2 * Copyright 2006 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 "wine/debug.h"
23 #include "wine/unicode.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(shdocvw);
34 const IBindStatusCallbackVtbl *lpBindStatusCallbackVtbl;
35 const IHttpNegotiateVtbl *lpHttpNegotiateVtbl;
44 #define BINDSC(x) ((IBindStatusCallback*) &(x)->lpBindStatusCallbackVtbl)
45 #define HTTPNEG(x) ((IHttpNegotiate*) &(x)->lpHttpNegotiateVtbl)
47 static void dump_BINDINFO(BINDINFO *bi)
49 static const char * const BINDINFOF_str[] = {
51 "BINDINFOF_URLENCODESTGMEDDATA",
52 "BINDINFOF_URLENCODEDEXTRAINFO"
55 static const char * const BINDVERB_str[] = {
75 bi->cbSize, debugstr_w(bi->szExtraInfo),
76 bi->stgmedData.tymed, bi->stgmedData.u.hGlobal, bi->stgmedData.pUnkForRelease,
77 bi->grfBindInfoF > BINDINFOF_URLENCODEDEXTRAINFO
78 ? "unknown" : BINDINFOF_str[bi->grfBindInfoF],
79 bi->dwBindVerb > BINDVERB_CUSTOM
80 ? "unknown" : BINDVERB_str[bi->dwBindVerb],
81 debugstr_w(bi->szCustomVerb),
82 bi->cbstgmedData, bi->dwOptions, bi->dwOptionsFlags, bi->dwCodePage,
83 bi->securityAttributes.nLength,
84 bi->securityAttributes.lpSecurityDescriptor,
85 bi->securityAttributes.bInheritHandle,
86 debugstr_guid(&bi->iid),
87 bi->pUnk, bi->dwReserved
91 #define BINDSC_THIS(iface) DEFINE_THIS(BindStatusCallback, BindStatusCallback, iface)
93 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
94 REFIID riid, void **ppv)
96 BindStatusCallback *This = BINDSC_THIS(iface);
100 if(IsEqualGUID(&IID_IUnknown, riid)) {
101 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
103 }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
104 TRACE("(%p)->(IID_IBindStatusCallback %p)\n", This, ppv);
106 }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
107 TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
108 *ppv = HTTPNEG(This);
112 IBindStatusCallback_AddRef(BINDSC(This));
116 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
117 return E_NOINTERFACE;
120 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
122 BindStatusCallback *This = BINDSC_THIS(iface);
123 LONG ref = InterlockedIncrement(&This->ref);
125 TRACE("(%p) ref=%d\n", This, ref);
130 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
132 BindStatusCallback *This = BINDSC_THIS(iface);
133 LONG ref = InterlockedDecrement(&This->ref);
135 TRACE("(%p) ref=%d\n", This, ref);
139 GlobalFree(This->post_data);
140 shdocvw_free(This->headers);
147 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
148 DWORD dwReserved, IBinding *pbind)
150 BindStatusCallback *This = BINDSC_THIS(iface);
151 FIXME("(%p)->(%d %p)\n", This, dwReserved, pbind);
155 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface,
158 BindStatusCallback *This = BINDSC_THIS(iface);
159 FIXME("(%p)->(%p)\n", This, pnPriority);
163 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface,
166 BindStatusCallback *This = BINDSC_THIS(iface);
167 FIXME("(%p)->(%d)\n", This, reserved);
171 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface,
172 ULONG ulProgress, ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
174 BindStatusCallback *This = BINDSC_THIS(iface);
175 FIXME("(%p)->(%d %d %d %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
176 debugstr_w(szStatusText));
180 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
181 HRESULT hresult, LPCWSTR szError)
183 BindStatusCallback *This = BINDSC_THIS(iface);
184 FIXME("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
188 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
189 DWORD *grfBINDF, BINDINFO *pbindinfo)
191 BindStatusCallback *This = BINDSC_THIS(iface);
193 FIXME("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
195 memset(pbindinfo, 0, sizeof(BINDINFO));
196 pbindinfo->cbSize = sizeof(BINDINFO);
198 pbindinfo->cbstgmedData = This->post_data_len;
200 if(This->post_data) {
201 pbindinfo->dwBindVerb = BINDVERB_POST;
203 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
204 pbindinfo->stgmedData.u.hGlobal = This->post_data;
205 pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)BINDSC(This);
206 IBindStatusCallback_AddRef(BINDSC(This));
212 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
213 DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
215 BindStatusCallback *This = BINDSC_THIS(iface);
216 FIXME("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
222 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
223 BindStatusCallback_QueryInterface,
224 BindStatusCallback_AddRef,
225 BindStatusCallback_Release,
226 BindStatusCallback_OnStartBinding,
227 BindStatusCallback_GetPriority,
228 BindStatusCallback_OnLowResource,
229 BindStatusCallback_OnProgress,
230 BindStatusCallback_OnStopBinding,
231 BindStatusCallback_GetBindInfo,
232 BindStatusCallback_OnDataAvailable
235 #define HTTPNEG_THIS(iface) DEFINE_THIS(BindStatusCallback, HttpNegotiate, iface)
237 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate *iface,
238 REFIID riid, void **ppv)
240 BindStatusCallback *This = HTTPNEG_THIS(iface);
241 return IBindStatusCallback_QueryInterface(BINDSC(This), riid, ppv);
244 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate *iface)
246 BindStatusCallback *This = HTTPNEG_THIS(iface);
247 return IBindStatusCallback_AddRef(BINDSC(This));
250 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate *iface)
252 BindStatusCallback *This = HTTPNEG_THIS(iface);
253 return IBindStatusCallback_Release(BINDSC(This));
256 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate *iface,
257 LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
259 BindStatusCallback *This = HTTPNEG_THIS(iface);
261 FIXME("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
262 dwReserved, pszAdditionalHeaders);
265 int size = (strlenW(This->headers)+1)*sizeof(WCHAR);
266 *pszAdditionalHeaders = CoTaskMemAlloc(size);
267 memcpy(*pszAdditionalHeaders, This->headers, size);
273 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate *iface,
274 DWORD dwResponseCode, LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders,
275 LPWSTR *pszAdditionalRequestHeaders)
277 BindStatusCallback *This = HTTPNEG_THIS(iface);
278 FIXME("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
279 debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
285 static const IHttpNegotiateVtbl HttpNegotiateVtbl = {
286 HttpNegotiate_QueryInterface,
287 HttpNegotiate_AddRef,
288 HttpNegotiate_Release,
289 HttpNegotiate_BeginningTransaction,
290 HttpNegotiate_OnResponse
293 static IBindStatusCallback *create_callback(DocHost *This, PBYTE post_data,
294 ULONG post_data_len, LPWSTR headers, VARIANT_BOOL *cancel)
296 BindStatusCallback *ret = shdocvw_alloc(sizeof(BindStatusCallback));
298 ret->lpBindStatusCallbackVtbl = &BindStatusCallbackVtbl;
299 ret->lpHttpNegotiateVtbl = &HttpNegotiateVtbl;
302 ret->post_data = NULL;
303 ret->post_data_len = post_data_len;
307 ret->post_data = GlobalAlloc(0, post_data_len);
308 memcpy(ret->post_data, post_data, post_data_len);
312 int size = (strlenW(headers)+1)*sizeof(WCHAR);
313 ret->headers = shdocvw_alloc(size);
314 memcpy(ret->headers, headers, size);
320 static void on_before_navigate2(DocHost *This, LPWSTR url, PBYTE post_data, ULONG post_data_len,
321 LPWSTR headers, VARIANT_BOOL *cancel)
323 VARIANT var_url, var_flags, var_frame_name, var_post_data, var_post_data2, var_headers;
324 DISPPARAMS dispparams;
325 VARIANTARG params[7];
327 dispparams.cArgs = 7;
328 dispparams.cNamedArgs = 0;
329 dispparams.rgdispidNamedArgs = NULL;
330 dispparams.rgvarg = params;
332 V_VT(params) = VT_BOOL|VT_BYREF;
333 V_BOOLREF(params) = cancel;
335 V_VT(params+1) = (VT_BYREF|VT_VARIANT);
336 V_VARIANTREF(params+1) = &var_headers;
337 V_VT(&var_headers) = VT_BSTR;
338 V_BSTR(&var_headers) = headers;
340 V_VT(params+2) = (VT_BYREF|VT_VARIANT);
341 V_VARIANTREF(params+2) = &var_post_data2;
342 V_VT(&var_post_data2) = (VT_BYREF|VT_VARIANT);
343 V_VARIANTREF(&var_post_data2) = &var_post_data;
344 VariantInit(&var_post_data);
347 SAFEARRAYBOUND bound = {post_data_len, 0};
350 V_VT(&var_post_data) = VT_UI1|VT_ARRAY;
351 V_ARRAY(&var_post_data) = SafeArrayCreate(VT_UI1, 1, &bound);
353 SafeArrayAccessData(V_ARRAY(&var_post_data), &data);
354 memcpy(data, post_data, post_data_len);
355 SafeArrayUnaccessData(V_ARRAY(&var_post_data));
358 V_VT(params+3) = (VT_BYREF|VT_VARIANT);
359 V_VARIANTREF(params+3) = &var_frame_name;
360 V_VT(&var_frame_name) = VT_BSTR;
361 V_BSTR(&var_frame_name) = NULL;
363 V_VT(params+4) = (VT_BYREF|VT_VARIANT);
364 V_VARIANTREF(params+4) = &var_flags;
365 V_VT(&var_flags) = VT_I4;
366 V_I4(&var_flags) = 0;
368 V_VT(params+5) = (VT_BYREF|VT_VARIANT);
369 V_VARIANTREF(params+5) = &var_url;
370 V_VT(&var_url) = VT_BSTR;
371 V_BSTR(&var_url) = SysAllocString(url);
373 V_VT(params+6) = (VT_DISPATCH);
374 V_DISPATCH(params+6) = This->disp;
376 call_sink(This->cps.wbe2, DISPID_BEFORENAVIGATE2, &dispparams);
378 SysFreeString(V_BSTR(&var_url));
380 SafeArrayDestroy(V_ARRAY(&var_post_data));
383 static BOOL try_application_url(LPCWSTR url)
385 SHELLEXECUTEINFOW exec_info;
391 static const WCHAR wszURLProtocol[] = {'U','R','L',' ','P','r','o','t','o','c','o','l',0};
393 hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, app, sizeof(app)/sizeof(WCHAR), NULL, 0);
397 res = RegOpenKeyW(HKEY_CLASSES_ROOT, app, &hkey);
398 if(res != ERROR_SUCCESS)
401 res = RegQueryValueExW(hkey, wszURLProtocol, NULL, &type, NULL, NULL);
403 if(res != ERROR_SUCCESS || type != REG_SZ)
406 TRACE("openning application %s\n", debugstr_w(app));
408 memset(&exec_info, 0, sizeof(exec_info));
409 exec_info.cbSize = sizeof(exec_info);
410 exec_info.lpFile = url;
411 exec_info.nShow = SW_SHOW;
413 return ShellExecuteExW(&exec_info);
416 static HRESULT navigate(DocHost *This, IMoniker *mon, IBindCtx *bindctx,
417 IBindStatusCallback *callback)
420 IPersistMoniker *persist;
421 VARIANT_BOOL cancel = VARIANT_FALSE;
429 IBindCtx_RegisterObjectParam(bindctx, (LPOLESTR)SZ_HTML_CLIENTSITE_OBJECTPARAM,
430 (IUnknown*)CLIENTSITE(This));
434 * We should use URLMoniker's BindToObject instead creating HTMLDocument here.
435 * This should be fixed when mshtml.dll and urlmon.dll will be good enough.
439 deactivate_document(This);
441 hres = CoCreateInstance(&CLSID_HTMLDocument, NULL,
442 CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
443 &IID_IUnknown, (void**)&This->document);
446 ERR("Could not create HTMLDocument: %08x\n", hres);
450 hres = IUnknown_QueryInterface(This->document, &IID_IPersistMoniker, (void**)&persist);
455 IPersistMoniker_Release(persist);
459 hres = IPersistMoniker_Load(persist, FALSE, mon, bindctx, 0);
460 IPersistMoniker_Release(persist);
462 WARN("Load failed: %08x\n", hres);
466 hres = IUnknown_QueryInterface(This->document, &IID_IOleObject, (void**)&oleobj);
470 hres = IOleObject_SetClientSite(oleobj, CLIENTSITE(This));
471 IOleObject_Release(oleobj);
473 PostMessageW(This->hwnd, WB_WM_NAVIGATE2, 0, 0);
479 static HRESULT bind_url_to_object(DocHost *This, LPCWSTR url, PBYTE post_data, ULONG post_data_len,
482 IBindStatusCallback *callback;
485 VARIANT_BOOL cancel = VARIANT_FALSE;
489 create_doc_view_hwnd(This);
491 hres = CreateURLMoniker(NULL, url, &mon);
493 WARN("CreateURLMoniker failed: %08x\n", hres);
497 IMoniker_GetDisplayName(mon, NULL, NULL, &This->url);
498 TRACE("navigating to %s\n", debugstr_w(This->url));
500 callback = create_callback(This, post_data, post_data_len, (LPWSTR)headers, &cancel);
501 CreateAsyncBindCtx(0, callback, 0, &bindctx);
503 hres = navigate(This, mon, bindctx, callback);
505 IMoniker_Release(mon);
510 HRESULT navigate_url(DocHost *This, BSTR url, VARIANT *Flags, VARIANT *TargetFrameName,
511 VARIANT *PostData, VARIANT *Headers)
513 PBYTE post_data = NULL;
514 ULONG post_data_len = 0;
515 LPWSTR headers = NULL;
518 TRACE("navigating to %s\n", debugstr_w(url));
520 if((Flags && V_VT(Flags) != VT_EMPTY)
521 || (TargetFrameName && V_VT(TargetFrameName) != VT_EMPTY))
522 FIXME("Unsupported arguments\n");
524 if(PostData && V_VT(PostData) != VT_EMPTY && V_VT(PostData) != VT_ERROR) {
525 if(V_VT(PostData) != (VT_ARRAY | VT_UI1)
526 || V_ARRAY(PostData)->cDims != 1) {
527 WARN("Invalid PostData\n");
531 SafeArrayAccessData(V_ARRAY(PostData), (void**)&post_data);
532 post_data_len = V_ARRAY(PostData)->rgsabound[0].cElements;
535 if(Headers && V_VT(Headers) != VT_EMPTY && V_VT(Headers) != VT_ERROR) {
536 if(V_VT(Headers) != VT_BSTR)
539 headers = V_BSTR(Headers);
540 TRACE("Headers: %s\n", debugstr_w(headers));
543 hres = bind_url_to_object(This, url, post_data, post_data_len, headers);
546 SafeArrayUnaccessData(V_ARRAY(PostData));
551 static HRESULT navigate_hlink(DocHost *This, IMoniker *mon, IBindCtx *bindctx,
552 IBindStatusCallback *callback)
554 IHttpNegotiate *http_negotiate;
556 PBYTE post_data = NULL;
557 ULONG post_data_len = 0;
558 LPWSTR headers = NULL;
559 VARIANT_BOOL cancel = VARIANT_FALSE;
564 IMoniker_GetDisplayName(mon, NULL, NULL, &url);
565 TRACE("navigating to %s\n", debugstr_w(url));
567 hres = IBindStatusCallback_QueryInterface(callback, &IID_IHttpNegotiate,
568 (void**)&http_negotiate);
569 if(SUCCEEDED(hres)) {
570 static const WCHAR null_string[] = {0};
572 IHttpNegotiate_BeginningTransaction(http_negotiate, null_string, null_string, 0,
574 IHttpNegotiate_Release(http_negotiate);
577 memset(&bindinfo, 0, sizeof(bindinfo));
578 bindinfo.cbSize = sizeof(bindinfo);
580 hres = IBindStatusCallback_GetBindInfo(callback, &bindf, &bindinfo);
581 dump_BINDINFO(&bindinfo);
582 if(bindinfo.dwBindVerb == BINDVERB_POST) {
583 post_data_len = bindinfo.cbstgmedData;
585 post_data = bindinfo.stgmedData.u.hGlobal;
588 on_before_navigate2(This, url, post_data, post_data_len, headers, &cancel);
590 CoTaskMemFree(headers);
591 ReleaseBindInfo(&bindinfo);
594 FIXME("navigation canceled\n");
599 /* FIXME: We should do it after BindToObject call */
600 if(try_application_url(url))
605 return navigate(This, mon, bindctx, callback);
608 #define HLINKFRAME_THIS(iface) DEFINE_THIS(WebBrowser, HlinkFrame, iface)
610 static HRESULT WINAPI HlinkFrame_QueryInterface(IHlinkFrame *iface, REFIID riid, void **ppv)
612 WebBrowser *This = HLINKFRAME_THIS(iface);
613 return IWebBrowser2_QueryInterface(WEBBROWSER2(This), riid, ppv);
616 static ULONG WINAPI HlinkFrame_AddRef(IHlinkFrame *iface)
618 WebBrowser *This = HLINKFRAME_THIS(iface);
619 return IWebBrowser2_AddRef(WEBBROWSER2(This));
622 static ULONG WINAPI HlinkFrame_Release(IHlinkFrame *iface)
624 WebBrowser *This = HLINKFRAME_THIS(iface);
625 return IWebBrowser2_Release(WEBBROWSER2(This));
628 static HRESULT WINAPI HlinkFrame_SetBrowseContext(IHlinkFrame *iface,
629 IHlinkBrowseContext *pihlbc)
631 WebBrowser *This = HLINKFRAME_THIS(iface);
632 FIXME("(%p)->(%p)\n", This, pihlbc);
636 static HRESULT WINAPI HlinkFrame_GetBrowseContext(IHlinkFrame *iface,
637 IHlinkBrowseContext **ppihlbc)
639 WebBrowser *This = HLINKFRAME_THIS(iface);
640 FIXME("(%p)->(%p)\n", This, ppihlbc);
644 static HRESULT WINAPI HlinkFrame_Navigate(IHlinkFrame *iface, DWORD grfHLNF, LPBC pbc,
645 IBindStatusCallback *pibsc, IHlink *pihlNavigate)
647 WebBrowser *This = HLINKFRAME_THIS(iface);
649 LPWSTR location = NULL;
651 TRACE("(%p)->(%08x %p %p %p)\n", This, grfHLNF, pbc, pibsc, pihlNavigate);
654 FIXME("unsupported grfHLNF=%08x\n", grfHLNF);
656 /* Windows calls GetTargetFrameName here. */
658 IHlink_GetMonikerReference(pihlNavigate, 1, &mon, &location);
661 FIXME("location = %s\n", debugstr_w(location));
662 CoTaskMemFree(location);
665 /* Windows calls GetHlinkSite here */
667 if(grfHLNF & HLNF_OPENINNEWWINDOW) {
668 FIXME("Not supported HLNF_OPENINNEWWINDOW\n");
672 return navigate_hlink(&This->doc_host, mon, pbc, pibsc);
675 static HRESULT WINAPI HlinkFrame_OnNavigate(IHlinkFrame *iface, DWORD grfHLNF,
676 IMoniker *pimkTarget, LPCWSTR pwzLocation, LPCWSTR pwzFriendlyName, DWORD dwreserved)
678 WebBrowser *This = HLINKFRAME_THIS(iface);
679 FIXME("(%p)->(%08x %p %s %s %d)\n", This, grfHLNF, pimkTarget, debugstr_w(pwzLocation),
680 debugstr_w(pwzFriendlyName), dwreserved);
684 static HRESULT WINAPI HlinkFrame_UpdateHlink(IHlinkFrame *iface, ULONG uHLID,
685 IMoniker *pimkTarget, LPCWSTR pwzLocation, LPCWSTR pwzFriendlyName)
687 WebBrowser *This = HLINKFRAME_THIS(iface);
688 FIXME("(%p)->(%u %p %s %s)\n", This, uHLID, pimkTarget, debugstr_w(pwzLocation),
689 debugstr_w(pwzFriendlyName));
693 #undef HLINKFRAME_THIS
695 static const IHlinkFrameVtbl HlinkFrameVtbl = {
696 HlinkFrame_QueryInterface,
699 HlinkFrame_SetBrowseContext,
700 HlinkFrame_GetBrowseContext,
702 HlinkFrame_OnNavigate,
703 HlinkFrame_UpdateHlink
706 void WebBrowser_HlinkFrame_Init(WebBrowser *This)
708 This->lpHlinkFrameVtbl = &HlinkFrameVtbl;