dsdmo: Add Flanger effect stub.
[wine.git] / dlls / mshtml / navigate.c
blobeb365c99af907a64b86edb52d0813a4bfdd60d0d
1 /*
2 * Copyright 2006-2010 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 #include <stdarg.h>
21 #define COBJMACROS
22 #define NONAMELESSUNION
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "winreg.h"
28 #include "ole2.h"
29 #include "hlguids.h"
30 #include "shlguid.h"
31 #include "wininet.h"
32 #include "shlwapi.h"
33 #include "htiface.h"
34 #include "shdeprecated.h"
36 #include "wine/debug.h"
38 #include "mshtml_private.h"
39 #include "htmlscript.h"
40 #include "binding.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
44 struct nsProtocolStream {
45 nsIInputStream nsIInputStream_iface;
47 LONG ref;
49 char buf[1024];
50 DWORD buf_size;
53 static inline nsProtocolStream *impl_from_nsIInputStream(nsIInputStream *iface)
55 return CONTAINING_RECORD(iface, nsProtocolStream, nsIInputStream_iface);
58 static nsresult NSAPI nsInputStream_QueryInterface(nsIInputStream *iface, nsIIDRef riid,
59 void **result)
61 nsProtocolStream *This = impl_from_nsIInputStream(iface);
63 *result = NULL;
65 if(IsEqualGUID(&IID_nsISupports, riid)) {
66 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
67 *result = &This->nsIInputStream_iface;
68 }else if(IsEqualGUID(&IID_nsIInputStream, riid)) {
69 TRACE("(%p)->(IID_nsIInputStream %p)\n", This, result);
70 *result = &This->nsIInputStream_iface;
73 if(*result) {
74 nsIInputStream_AddRef(&This->nsIInputStream_iface);
75 return NS_OK;
78 WARN("unsupported interface %s\n", debugstr_guid(riid));
79 return NS_NOINTERFACE;
82 static nsrefcnt NSAPI nsInputStream_AddRef(nsIInputStream *iface)
84 nsProtocolStream *This = impl_from_nsIInputStream(iface);
85 LONG ref = InterlockedIncrement(&This->ref);
87 TRACE("(%p) ref=%ld\n", This, ref);
89 return ref;
93 static nsrefcnt NSAPI nsInputStream_Release(nsIInputStream *iface)
95 nsProtocolStream *This = impl_from_nsIInputStream(iface);
96 LONG ref = InterlockedDecrement(&This->ref);
98 TRACE("(%p) ref=%ld\n", This, ref);
100 if(!ref)
101 free(This);
103 return ref;
106 static nsresult NSAPI nsInputStream_Close(nsIInputStream *iface)
108 nsProtocolStream *This = impl_from_nsIInputStream(iface);
109 FIXME("(%p)\n", This);
110 return NS_ERROR_NOT_IMPLEMENTED;
113 static nsresult NSAPI nsInputStream_Available(nsIInputStream *iface, UINT64 *_retval)
115 nsProtocolStream *This = impl_from_nsIInputStream(iface);
116 FIXME("(%p)->(%p)\n", This, _retval);
117 return NS_ERROR_NOT_IMPLEMENTED;
120 static nsresult NSAPI nsInputStream_Read(nsIInputStream *iface, char *aBuf, UINT32 aCount,
121 UINT32 *_retval)
123 nsProtocolStream *This = impl_from_nsIInputStream(iface);
124 DWORD read = aCount;
126 TRACE("(%p)->(%p %d %p)\n", This, aBuf, aCount, _retval);
128 if(read > This->buf_size)
129 read = This->buf_size;
131 if(read) {
132 memcpy(aBuf, This->buf, read);
133 if(read < This->buf_size)
134 memmove(This->buf, This->buf+read, This->buf_size-read);
135 This->buf_size -= read;
138 *_retval = read;
139 return NS_OK;
142 static nsresult NSAPI nsInputStream_ReadSegments(nsIInputStream *iface,
143 nsresult (WINAPI *aWriter)(nsIInputStream*,void*,const char*,UINT32,UINT32,UINT32*),
144 void *aClousure, UINT32 aCount, UINT32 *_retval)
146 nsProtocolStream *This = impl_from_nsIInputStream(iface);
147 UINT32 written = 0;
148 nsresult nsres;
150 TRACE("(%p)->(%p %p %d %p)\n", This, aWriter, aClousure, aCount, _retval);
152 if(!This->buf_size)
153 return S_OK;
155 if(aCount > This->buf_size)
156 aCount = This->buf_size;
158 nsres = aWriter(&This->nsIInputStream_iface, aClousure, This->buf, 0, aCount, &written);
159 if(NS_FAILED(nsres))
160 TRACE("aWriter failed: %08lx\n", nsres);
161 else if(written != This->buf_size)
162 FIXME("written %d != buf_size %ld\n", written, This->buf_size);
164 This->buf_size -= written;
166 *_retval = written;
167 return nsres;
170 static nsresult NSAPI nsInputStream_IsNonBlocking(nsIInputStream *iface, cpp_bool *_retval)
172 nsProtocolStream *This = impl_from_nsIInputStream(iface);
173 FIXME("(%p)->(%p)\n", This, _retval);
174 return NS_ERROR_NOT_IMPLEMENTED;
177 static const nsIInputStreamVtbl nsInputStreamVtbl = {
178 nsInputStream_QueryInterface,
179 nsInputStream_AddRef,
180 nsInputStream_Release,
181 nsInputStream_Close,
182 nsInputStream_Available,
183 nsInputStream_Read,
184 nsInputStream_ReadSegments,
185 nsInputStream_IsNonBlocking
188 static nsProtocolStream *create_nsprotocol_stream(void)
190 nsProtocolStream *ret;
192 ret = malloc(sizeof(nsProtocolStream));
193 if(!ret)
194 return NULL;
196 ret->nsIInputStream_iface.lpVtbl = &nsInputStreamVtbl;
197 ret->ref = 1;
198 ret->buf_size = 0;
200 return ret;
203 static void release_request_data(request_data_t *request_data)
205 if(request_data->post_stream)
206 nsIInputStream_Release(request_data->post_stream);
207 free(request_data->headers);
208 if(request_data->post_data)
209 GlobalFree(request_data->post_data);
212 static inline BSCallback *impl_from_IBindStatusCallback(IBindStatusCallback *iface)
214 return CONTAINING_RECORD(iface, BSCallback, IBindStatusCallback_iface);
217 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
218 REFIID riid, void **ppv)
220 BSCallback *This = impl_from_IBindStatusCallback(iface);
222 *ppv = NULL;
223 if(IsEqualGUID(&IID_IUnknown, riid)) {
224 TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
225 *ppv = &This->IBindStatusCallback_iface;
226 }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
227 TRACE("(%p)->(IID_IBindStatusCallback, %p)\n", This, ppv);
228 *ppv = &This->IBindStatusCallback_iface;
229 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
230 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
231 *ppv = &This->IServiceProvider_iface;
232 }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
233 TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
234 *ppv = &This->IHttpNegotiate2_iface;
235 }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
236 TRACE("(%p)->(IID_IHttpNegotiate2 %p)\n", This, ppv);
237 *ppv = &This->IHttpNegotiate2_iface;
238 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
239 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
240 *ppv = &This->IInternetBindInfo_iface;
241 }else if(IsEqualGUID(&IID_IBindCallbackRedirect, riid)) {
242 TRACE("(%p)->(IID_IBindCallbackRedirect %p)\n", This, ppv);
243 *ppv = &This->IBindCallbackRedirect_iface;
246 if(*ppv) {
247 IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
248 return S_OK;
251 TRACE("Unsupported riid = %s\n", debugstr_guid(riid));
252 return E_NOINTERFACE;
255 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
257 BSCallback *This = impl_from_IBindStatusCallback(iface);
258 LONG ref = InterlockedIncrement(&This->ref);
260 TRACE("(%p) ref = %ld\n", This, ref);
262 return ref;
265 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
267 BSCallback *This = impl_from_IBindStatusCallback(iface);
268 LONG ref = InterlockedDecrement(&This->ref);
270 TRACE("(%p) ref = %ld\n", This, ref);
272 if(!ref) {
273 release_request_data(&This->request_data);
274 if(This->mon)
275 IMoniker_Release(This->mon);
276 if(This->binding)
277 IBinding_Release(This->binding);
278 list_remove(&This->entry);
279 list_init(&This->entry);
281 This->vtbl->destroy(This);
284 return ref;
287 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
288 DWORD dwReserved, IBinding *pbind)
290 BSCallback *This = impl_from_IBindStatusCallback(iface);
292 TRACE("(%p)->(%ld %p)\n", This, dwReserved, pbind);
294 IBinding_AddRef(pbind);
295 This->binding = pbind;
297 if(This->window)
298 list_add_head(&This->window->bindings, &This->entry);
300 return This->vtbl->start_binding(This);
303 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
305 BSCallback *This = impl_from_IBindStatusCallback(iface);
306 FIXME("(%p)->(%p)\n", This, pnPriority);
307 return E_NOTIMPL;
310 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
312 BSCallback *This = impl_from_IBindStatusCallback(iface);
313 FIXME("(%p)->(%ld)\n", This, reserved);
314 return E_NOTIMPL;
317 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
318 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
320 BSCallback *This = impl_from_IBindStatusCallback(iface);
322 TRACE("%p)->(%lu %lu %lu %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
323 debugstr_w(szStatusText));
325 return This->vtbl->on_progress(This, ulProgress, ulProgressMax, ulStatusCode, szStatusText);
328 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
329 HRESULT hresult, LPCWSTR szError)
331 BSCallback *This = impl_from_IBindStatusCallback(iface);
332 HRESULT hres;
334 TRACE("(%p)->(%08lx %s)\n", This, hresult, debugstr_w(szError));
336 /* NOTE: IE7 calls GetBindResult here */
338 hres = This->vtbl->stop_binding(This, hresult);
340 if(This->binding) {
341 IBinding_Release(This->binding);
342 This->binding = NULL;
345 if(This->mon) {
346 IMoniker_Release(This->mon);
347 This->mon = NULL;
350 list_remove(&This->entry);
351 list_init(&This->entry);
352 This->window = NULL;
354 return hres;
357 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
358 DWORD *grfBINDF, BINDINFO *pbindinfo)
360 BSCallback *This = impl_from_IBindStatusCallback(iface);
361 DWORD size;
363 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
365 if(!This->bindinfo_ready) {
366 HRESULT hres;
368 hres = This->vtbl->init_bindinfo(This);
369 if(FAILED(hres))
370 return hres;
372 This->bindinfo_ready = TRUE;
375 *grfBINDF = This->bindf;
377 size = pbindinfo->cbSize;
378 memset(pbindinfo, 0, size);
379 pbindinfo->cbSize = size;
381 pbindinfo->cbstgmedData = This->request_data.post_data_len;
382 pbindinfo->dwCodePage = CP_UTF8;
383 pbindinfo->dwOptions = This->bindinfo_options;
385 if(This->request_data.post_data_len) {
386 pbindinfo->dwBindVerb = BINDVERB_POST;
388 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
389 pbindinfo->stgmedData.u.hGlobal = This->request_data.post_data;
390 pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)&This->IBindStatusCallback_iface;
391 IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
394 return S_OK;
397 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
398 DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
400 BSCallback *This = impl_from_IBindStatusCallback(iface);
402 TRACE("(%p)->(%08lx %ld %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
404 return This->vtbl->read_data(This, pstgmed->u.pstm);
407 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
408 REFIID riid, IUnknown *punk)
410 BSCallback *This = impl_from_IBindStatusCallback(iface);
411 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
412 return E_NOTIMPL;
415 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
416 BindStatusCallback_QueryInterface,
417 BindStatusCallback_AddRef,
418 BindStatusCallback_Release,
419 BindStatusCallback_OnStartBinding,
420 BindStatusCallback_GetPriority,
421 BindStatusCallback_OnLowResource,
422 BindStatusCallback_OnProgress,
423 BindStatusCallback_OnStopBinding,
424 BindStatusCallback_GetBindInfo,
425 BindStatusCallback_OnDataAvailable,
426 BindStatusCallback_OnObjectAvailable
429 static inline BSCallback *impl_from_IHttpNegotiate2(IHttpNegotiate2 *iface)
431 return CONTAINING_RECORD(iface, BSCallback, IHttpNegotiate2_iface);
434 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
435 REFIID riid, void **ppv)
437 BSCallback *This = impl_from_IHttpNegotiate2(iface);
438 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
441 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
443 BSCallback *This = impl_from_IHttpNegotiate2(iface);
444 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
447 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
449 BSCallback *This = impl_from_IHttpNegotiate2(iface);
450 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
453 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
454 LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
456 BSCallback *This = impl_from_IHttpNegotiate2(iface);
457 HRESULT hres;
459 TRACE("(%p)->(%s %s %ld %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
460 dwReserved, pszAdditionalHeaders);
462 *pszAdditionalHeaders = NULL;
464 hres = This->vtbl->beginning_transaction(This, pszAdditionalHeaders);
465 if(hres != S_FALSE)
466 return hres;
468 if(This->request_data.headers) {
469 DWORD size;
471 size = (lstrlenW(This->request_data.headers)+1)*sizeof(WCHAR);
472 *pszAdditionalHeaders = CoTaskMemAlloc(size);
473 if(!*pszAdditionalHeaders)
474 return E_OUTOFMEMORY;
475 memcpy(*pszAdditionalHeaders, This->request_data.headers, size);
478 return S_OK;
481 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
482 LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
484 BSCallback *This = impl_from_IHttpNegotiate2(iface);
486 TRACE("(%p)->(%ld %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
487 debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
489 return This->vtbl->on_response(This, dwResponseCode, szResponseHeaders);
492 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
493 BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
495 BSCallback *This = impl_from_IHttpNegotiate2(iface);
496 TRACE("(%p)->(%p %p %Id)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
497 return E_NOTIMPL; /* FIXME */
500 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
501 HttpNegotiate_QueryInterface,
502 HttpNegotiate_AddRef,
503 HttpNegotiate_Release,
504 HttpNegotiate_BeginningTransaction,
505 HttpNegotiate_OnResponse,
506 HttpNegotiate_GetRootSecurityId
509 static inline BSCallback *impl_from_IInternetBindInfo(IInternetBindInfo *iface)
511 return CONTAINING_RECORD(iface, BSCallback, IInternetBindInfo_iface);
514 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
515 REFIID riid, void **ppv)
517 BSCallback *This = impl_from_IInternetBindInfo(iface);
518 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
521 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
523 BSCallback *This = impl_from_IInternetBindInfo(iface);
524 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
527 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
529 BSCallback *This = impl_from_IInternetBindInfo(iface);
530 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
533 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
534 DWORD *grfBINDF, BINDINFO *pbindinfo)
536 BSCallback *This = impl_from_IInternetBindInfo(iface);
537 FIXME("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
538 return E_NOTIMPL;
541 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
542 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
544 BSCallback *This = impl_from_IInternetBindInfo(iface);
545 FIXME("(%p)->(%lu %p %lu %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
546 return E_NOTIMPL;
549 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
550 InternetBindInfo_QueryInterface,
551 InternetBindInfo_AddRef,
552 InternetBindInfo_Release,
553 InternetBindInfo_GetBindInfo,
554 InternetBindInfo_GetBindString
557 static inline BSCallback *impl_from_IBindCallbackRedirect(IBindCallbackRedirect *iface)
559 return CONTAINING_RECORD(iface, BSCallback, IBindCallbackRedirect_iface);
562 static HRESULT WINAPI BindCallbackRedirect_QueryInterface(IBindCallbackRedirect *iface, REFIID riid, void **ppv)
564 BSCallback *This = impl_from_IBindCallbackRedirect(iface);
565 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
568 static ULONG WINAPI BindCallbackRedirect_AddRef(IBindCallbackRedirect *iface)
570 BSCallback *This = impl_from_IBindCallbackRedirect(iface);
571 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
574 static ULONG WINAPI BindCallbackRedirect_Release(IBindCallbackRedirect *iface)
576 BSCallback *This = impl_from_IBindCallbackRedirect(iface);
577 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
580 static HRESULT WINAPI BindCallbackRedirect_Redirect(IBindCallbackRedirect *iface, const WCHAR *url, VARIANT_BOOL *vbCancel)
582 BSCallback *This = impl_from_IBindCallbackRedirect(iface);
583 GeckoBrowser *browser;
584 BOOL cancel = FALSE;
585 BSTR frame_name = NULL;
586 HRESULT hres = S_OK;
588 TRACE("(%p)->(%s %p)\n", This, debugstr_w(url), vbCancel);
590 if(This->window && This->window->base.outer_window && (browser = This->window->base.outer_window->browser)
591 && browser->doc->doc_object_service) {
592 if(is_main_content_window(This->window->base.outer_window)) {
593 hres = IHTMLWindow2_get_name(&This->window->base.IHTMLWindow2_iface, &frame_name);
594 if(FAILED(hres))
595 return hres;
598 hres = IDocObjectService_FireBeforeNavigate2(browser->doc->doc_object_service, NULL, url, 0x40,
599 frame_name, NULL, 0, NULL, TRUE, &cancel);
600 SysFreeString(frame_name);
603 *vbCancel = variant_bool(cancel);
604 return hres;
607 static const IBindCallbackRedirectVtbl BindCallbackRedirectVtbl = {
608 BindCallbackRedirect_QueryInterface,
609 BindCallbackRedirect_AddRef,
610 BindCallbackRedirect_Release,
611 BindCallbackRedirect_Redirect
614 static inline BSCallback *impl_from_IServiceProvider(IServiceProvider *iface)
616 return CONTAINING_RECORD(iface, BSCallback, IServiceProvider_iface);
619 static HRESULT WINAPI BSCServiceProvider_QueryInterface(IServiceProvider *iface,
620 REFIID riid, void **ppv)
622 BSCallback *This = impl_from_IServiceProvider(iface);
623 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
626 static ULONG WINAPI BSCServiceProvider_AddRef(IServiceProvider *iface)
628 BSCallback *This = impl_from_IServiceProvider(iface);
629 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
632 static ULONG WINAPI BSCServiceProvider_Release(IServiceProvider *iface)
634 BSCallback *This = impl_from_IServiceProvider(iface);
635 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
638 static HRESULT WINAPI BSCServiceProvider_QueryService(IServiceProvider *iface,
639 REFGUID guidService, REFIID riid, void **ppv)
641 BSCallback *This = impl_from_IServiceProvider(iface);
643 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
645 if(This->window && IsEqualGUID(guidService, &IID_IWindowForBindingUI))
646 return IServiceProvider_QueryService(&This->window->base.IServiceProvider_iface, guidService, riid, ppv);
647 return E_NOINTERFACE;
650 static const IServiceProviderVtbl ServiceProviderVtbl = {
651 BSCServiceProvider_QueryInterface,
652 BSCServiceProvider_AddRef,
653 BSCServiceProvider_Release,
654 BSCServiceProvider_QueryService
657 void init_bscallback(BSCallback *This, const BSCallbackVtbl *vtbl, IMoniker *mon, DWORD bindf)
659 This->IBindStatusCallback_iface.lpVtbl = &BindStatusCallbackVtbl;
660 This->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
661 This->IHttpNegotiate2_iface.lpVtbl = &HttpNegotiate2Vtbl;
662 This->IInternetBindInfo_iface.lpVtbl = &InternetBindInfoVtbl;
663 This->IBindCallbackRedirect_iface.lpVtbl = &BindCallbackRedirectVtbl;
664 This->vtbl = vtbl;
665 This->ref = 1;
666 This->bindf = bindf;
667 This->bindinfo_options = BINDINFO_OPTIONS_USE_IE_ENCODING;
668 This->bom = BOM_NONE;
670 list_init(&This->entry);
672 if(mon)
673 IMoniker_AddRef(mon);
674 This->mon = mon;
677 HRESULT read_stream(BSCallback *This, IStream *stream, void *buf, DWORD size, DWORD *ret_size)
679 DWORD read_size = 0, skip=0;
680 BYTE *data = buf;
681 HRESULT hres;
683 hres = IStream_Read(stream, buf, size, &read_size);
685 if(!This->read && This->bom == BOM_NONE) {
686 if(read_size >= 2 && data[0] == 0xff && data[1] == 0xfe) {
687 This->bom = BOM_UTF16;
688 skip = 2;
689 }else if(read_size >= 3 && data[0] == 0xef && data[1] == 0xbb && data[2] == 0xbf) {
690 This->bom = BOM_UTF8;
691 skip = 3;
693 if(skip) {
694 read_size -= skip;
695 if(read_size)
696 memmove(data, data+skip, read_size);
700 This->read += read_size;
701 *ret_size = read_size;
702 return hres;
705 static void parse_content_type(nsChannelBSC *This, const WCHAR *value)
707 const WCHAR *ptr, *beg, *end;
708 size_t len = wcslen(value);
709 char *content_type;
711 static const WCHAR charsetW[] = {'c','h','a','r','s','e','t','='};
713 ptr = wcschr(value, ';');
715 if(!This->nschannel->content_type || !(This->nschannel->load_flags & LOAD_CALL_CONTENT_SNIFFERS)) {
716 for(end = ptr ? ptr : value + len; end > value; end--)
717 if(!iswspace(end[-1]))
718 break;
719 for(beg = value; beg < end; beg++)
720 if(!iswspace(*beg))
721 break;
723 if((content_type = strndupWtoU(beg, end - beg))) {
724 free(This->nschannel->content_type);
725 This->nschannel->content_type = content_type;
726 strlwr(content_type);
730 if(!ptr)
731 return;
733 ptr++;
734 while(*ptr && iswspace(*ptr))
735 ptr++;
737 if(ptr + ARRAY_SIZE(charsetW) < value+len && !wcsnicmp(ptr, charsetW, ARRAY_SIZE(charsetW))) {
738 size_t charset_len, lena;
739 nsACString charset_str;
740 const WCHAR *charset;
741 char *charseta;
743 ptr += ARRAY_SIZE(charsetW);
745 if(*ptr == '\'') {
746 FIXME("Quoted value\n");
747 return;
748 }else {
749 charset = ptr;
750 while(*ptr && *ptr != ',')
751 ptr++;
752 charset_len = ptr-charset;
755 lena = WideCharToMultiByte(CP_ACP, 0, charset, charset_len, NULL, 0, NULL, NULL);
756 charseta = malloc(lena + 1);
757 if(!charseta)
758 return;
760 WideCharToMultiByte(CP_ACP, 0, charset, charset_len, charseta, lena, NULL, NULL);
761 charseta[lena] = 0;
763 nsACString_InitDepend(&charset_str, charseta);
764 nsIHttpChannel_SetContentCharset(&This->nschannel->nsIHttpChannel_iface, &charset_str);
765 nsACString_Finish(&charset_str);
766 free(charseta);
767 }else {
768 FIXME("unhandled: %s\n", debugstr_wn(ptr, len - (ptr-value)));
772 static HRESULT parse_headers(const WCHAR *headers, struct list *headers_list)
774 const WCHAR *header, *header_end, *colon, *value;
775 HRESULT hres;
777 header = headers;
778 while(*header) {
779 if(header[0] == '\r' && header[1] == '\n' && !header[2])
780 break;
781 for(colon = header; *colon && *colon != ':' && *colon != '\r'; colon++);
782 if(*colon != ':')
783 return E_FAIL;
785 value = colon+1;
786 while(*value == ' ')
787 value++;
788 if(!*value)
789 return E_FAIL;
791 for(header_end = value+1; *header_end && *header_end != '\r'; header_end++);
793 hres = set_http_header(headers_list, header, colon-header, value, header_end-value);
794 if(FAILED(hres))
795 return hres;
797 header = header_end;
798 if(header[0] == '\r' && header[1] == '\n')
799 header += 2;
802 return S_OK;
805 static HRESULT process_response_headers(nsChannelBSC *This, const WCHAR *headers)
807 http_header_t *iter;
808 HRESULT hres;
810 hres = parse_headers(headers, &This->nschannel->response_headers);
811 if(FAILED(hres))
812 return hres;
814 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->response_headers, http_header_t, entry) {
815 if(!wcsicmp(iter->header, L"content-type"))
816 parse_content_type(This, iter->data);
819 return S_OK;
822 static void query_http_info(nsChannelBSC *This, IWinInetHttpInfo *wininet_info)
824 const WCHAR *ptr;
825 DWORD len = 0;
826 WCHAR *buf;
828 IWinInetHttpInfo_QueryInfo(wininet_info, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &len, NULL, NULL);
829 if(!len)
830 return;
832 buf = malloc(len);
833 if(!buf)
834 return;
836 IWinInetHttpInfo_QueryInfo(wininet_info, HTTP_QUERY_RAW_HEADERS_CRLF, buf, &len, NULL, NULL);
837 if(!len) {
838 free(buf);
839 return;
842 ptr = wcschr(buf, '\r');
843 if(ptr && ptr[1] == '\n') {
844 ptr += 2;
845 process_response_headers(This, ptr);
848 free(buf);
851 HRESULT start_binding(HTMLInnerWindow *inner_window, BSCallback *bscallback, IBindCtx *bctx)
853 IStream *str = NULL;
854 HRESULT hres;
856 TRACE("(%p %p %p)\n", inner_window, bscallback, bctx);
858 bscallback->window = inner_window;
860 /* NOTE: IE7 calls IsSystemMoniker here*/
862 if(bctx) {
863 hres = RegisterBindStatusCallback(bctx, &bscallback->IBindStatusCallback_iface, NULL, 0);
864 if(SUCCEEDED(hres))
865 IBindCtx_AddRef(bctx);
866 }else {
867 hres = CreateAsyncBindCtx(0, &bscallback->IBindStatusCallback_iface, NULL, &bctx);
870 if(FAILED(hres)) {
871 bscallback->window = NULL;
872 return hres;
875 hres = IMoniker_BindToStorage(bscallback->mon, bctx, NULL, &IID_IStream, (void**)&str);
876 IBindCtx_Release(bctx);
877 if(FAILED(hres)) {
878 WARN("BindToStorage failed: %08lx\n", hres);
879 bscallback->window = NULL;
880 return hres;
883 if(str)
884 IStream_Release(str);
886 return S_OK;
889 static HRESULT read_post_data_stream(nsIInputStream *stream, BOOL contains_headers, struct list *headers_list,
890 request_data_t *request_data)
892 nsISeekableStream *seekable_stream;
893 UINT64 available = 0;
894 UINT32 data_len = 0;
895 char *data, *post_data;
896 nsresult nsres;
897 HRESULT hres = S_OK;
899 if(!stream)
900 return S_OK;
902 nsres = nsIInputStream_Available(stream, &available);
903 if(NS_FAILED(nsres))
904 return E_FAIL;
906 post_data = data = GlobalAlloc(0, available+1);
907 if(!data)
908 return E_OUTOFMEMORY;
910 nsres = nsIInputStream_Read(stream, data, available, &data_len);
911 if(NS_FAILED(nsres)) {
912 GlobalFree(data);
913 return E_FAIL;
916 if(contains_headers) {
917 if(data_len >= 2 && data[0] == '\r' && data[1] == '\n') {
918 post_data = data+2;
919 data_len -= 2;
920 }else {
921 WCHAR *headers;
922 DWORD size;
923 char *ptr;
925 post_data += data_len;
926 for(ptr = data; ptr+4 < data+data_len; ptr++) {
927 if(!memcmp(ptr, "\r\n\r\n", 4)) {
928 ptr += 2;
929 post_data = ptr+2;
930 break;
934 data_len -= post_data-data;
936 size = MultiByteToWideChar(CP_ACP, 0, data, ptr-data, NULL, 0);
937 headers = malloc((size + 1) * sizeof(WCHAR));
938 if(headers) {
939 MultiByteToWideChar(CP_ACP, 0, data, ptr-data, headers, size);
940 headers[size] = 0;
941 if(headers_list)
942 hres = parse_headers(headers, headers_list);
943 if(SUCCEEDED(hres))
944 request_data->headers = headers;
945 else
946 free(headers);
947 }else {
948 hres = E_OUTOFMEMORY;
953 if(FAILED(hres)) {
954 GlobalFree(data);
955 return hres;
958 if(!data_len) {
959 GlobalFree(data);
960 post_data = NULL;
961 }else if(post_data != data) {
962 char *new_data;
964 new_data = GlobalAlloc(0, data_len+1);
965 if(new_data)
966 memcpy(new_data, post_data, data_len);
967 GlobalFree(data);
968 if(!new_data)
969 return E_OUTOFMEMORY;
970 post_data = new_data;
973 if(post_data)
974 post_data[data_len] = 0;
975 request_data->post_data = post_data;
976 request_data->post_data_len = data_len;
978 nsres = nsIInputStream_QueryInterface(stream, &IID_nsISeekableStream, (void**)&seekable_stream);
979 assert(nsres == NS_OK);
981 nsres = nsISeekableStream_Seek(seekable_stream, NS_SEEK_SET, 0);
982 assert(nsres == NS_OK);
984 nsISeekableStream_Release(seekable_stream);
986 nsIInputStream_AddRef(stream);
987 request_data->post_stream = stream;
988 TRACE("post_data = %s\n", debugstr_an(request_data->post_data, request_data->post_data_len));
989 return S_OK;
992 static HRESULT on_start_nsrequest(nsChannelBSC *This)
994 nsresult nsres;
996 /* Async request can be cancelled before we got to it */
997 if(NS_FAILED(This->nschannel->status))
998 return E_ABORT; /* FIXME: map status to HRESULT */
1000 This->nschannel->binding = This;
1002 /* FIXME: it's needed for http connections from BindToObject. */
1003 if(!This->nschannel->response_status)
1004 This->nschannel->response_status = 200;
1006 nsres = nsIStreamListener_OnStartRequest(This->nslistener,
1007 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext);
1008 if(NS_FAILED(nsres)) {
1009 FIXME("OnStartRequest failed: %08lx\n", nsres);
1010 return E_FAIL;
1013 if(This->is_doc_channel) {
1014 HRESULT hres;
1016 if(!This->bsc.window)
1017 return E_ABORT; /* Binding aborted in OnStartRequest call. */
1018 hres = update_window_doc(This->bsc.window);
1019 if(FAILED(hres))
1020 return hres;
1022 if(This->bsc.binding)
1023 process_document_response_headers(This->bsc.window->doc, This->bsc.binding);
1024 if(This->bsc.window->base.outer_window->readystate != READYSTATE_LOADING)
1025 set_ready_state(This->bsc.window->base.outer_window, READYSTATE_LOADING);
1028 return S_OK;
1031 static void on_stop_nsrequest(nsChannelBSC *This, HRESULT result)
1033 nsresult nsres, request_result;
1035 switch(result) {
1036 case S_OK:
1037 request_result = NS_OK;
1038 break;
1039 case E_ABORT:
1040 request_result = NS_BINDING_ABORTED;
1041 break;
1042 default:
1043 request_result = NS_ERROR_FAILURE;
1046 if(This->nslistener) {
1047 nsres = nsIStreamListener_OnStopRequest(This->nslistener,
1048 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext,
1049 request_result);
1050 if(NS_FAILED(nsres))
1051 WARN("OnStopRequest failed: %08lx\n", nsres);
1054 if(This->nschannel) {
1055 if(This->nschannel->load_group) {
1056 nsres = nsILoadGroup_RemoveRequest(This->nschannel->load_group,
1057 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, NULL, request_result);
1058 if(NS_FAILED(nsres))
1059 ERR("RemoveRequest failed: %08lx\n", nsres);
1061 if(This->nschannel->binding == This)
1062 This->nschannel->binding = NULL;
1066 static void notify_progress(nsChannelBSC *This)
1068 nsChannel *nschannel = This->nschannel;
1069 nsIProgressEventSink *sink = NULL;
1070 nsresult nsres;
1072 if(!nschannel)
1073 return;
1075 if(nschannel->notif_callback)
1076 if(NS_FAILED(nsIInterfaceRequestor_GetInterface(nschannel->notif_callback, &IID_nsIProgressEventSink, (void**)&sink)))
1077 sink = NULL;
1079 if(!sink && nschannel->load_group) {
1080 nsIRequestObserver *req_observer;
1082 if(NS_SUCCEEDED(nsILoadGroup_GetGroupObserver(nschannel->load_group, &req_observer)) && req_observer) {
1083 nsres = nsIRequestObserver_QueryInterface(req_observer, &IID_nsIProgressEventSink, (void**)&sink);
1084 nsIRequestObserver_Release(req_observer);
1085 if(NS_FAILED(nsres))
1086 sink = NULL;
1090 if(sink) {
1091 nsIProgressEventSink_OnProgress(sink, (nsIRequest*)&nschannel->nsIHttpChannel_iface, This->nscontext,
1092 This->progress, (This->total == ~0) ? -1 : This->total);
1093 nsIProgressEventSink_Release(sink);
1097 static HRESULT read_stream_data(nsChannelBSC *This, IStream *stream)
1099 DWORD read;
1100 nsresult nsres;
1101 HRESULT hres;
1103 if(!This->response_processed) {
1104 IWinInetHttpInfo *wininet_info;
1106 if(This->is_doc_channel)
1107 This->bsc.window->performance_timing->response_start_time = get_time_stamp();
1109 This->response_processed = TRUE;
1110 if(This->bsc.binding) {
1111 hres = IBinding_QueryInterface(This->bsc.binding, &IID_IWinInetHttpInfo, (void**)&wininet_info);
1112 if(SUCCEEDED(hres)) {
1113 query_http_info(This, wininet_info);
1114 IWinInetHttpInfo_Release(wininet_info);
1119 if(!This->nschannel)
1120 return S_OK;
1122 if(!This->nslistener) {
1123 BYTE buf[1024];
1125 do {
1126 hres = read_stream(&This->bsc, stream, buf, sizeof(buf), &read);
1127 }while(hres == S_OK && read);
1129 return S_OK;
1132 if(!This->nsstream) {
1133 This->nsstream = create_nsprotocol_stream();
1134 if(!This->nsstream)
1135 return E_OUTOFMEMORY;
1138 do {
1139 BOOL first_read = !This->bsc.read;
1141 hres = read_stream(&This->bsc, stream, This->nsstream->buf+This->nsstream->buf_size,
1142 sizeof(This->nsstream->buf)-This->nsstream->buf_size, &read);
1143 if(!read)
1144 break;
1146 This->nsstream->buf_size += read;
1148 if(first_read) {
1149 switch(This->bsc.bom) {
1150 case BOM_UTF8:
1151 This->nschannel->charset = strdup("utf-8");
1152 break;
1153 case BOM_UTF16:
1154 This->nschannel->charset = strdup("utf-16");
1155 case BOM_NONE:
1156 /* FIXME: Get charset from HTTP headers */;
1159 if(!This->nschannel->content_type) {
1160 WCHAR *mime;
1162 hres = FindMimeFromData(NULL, NULL, This->nsstream->buf, This->nsstream->buf_size,
1163 This->is_doc_channel ? L"text/html" : NULL, 0, &mime, 0);
1164 if(FAILED(hres))
1165 return hres;
1167 TRACE("Found MIME %s\n", debugstr_w(mime));
1169 This->nschannel->content_type = strdupWtoA(mime);
1170 CoTaskMemFree(mime);
1171 if(!This->nschannel->content_type)
1172 return E_OUTOFMEMORY;
1175 hres = on_start_nsrequest(This);
1176 if(FAILED(hres))
1177 return hres;
1180 notify_progress(This);
1182 nsres = nsIStreamListener_OnDataAvailable(This->nslistener,
1183 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext,
1184 &This->nsstream->nsIInputStream_iface, This->bsc.read-This->nsstream->buf_size,
1185 This->nsstream->buf_size);
1186 if(NS_FAILED(nsres)) {
1187 WARN("OnDataAvailable failed: %08lx\n", nsres);
1188 return map_nsresult(nsres);
1191 if(This->nsstream->buf_size == sizeof(This->nsstream->buf)) {
1192 ERR("buffer is full\n");
1193 break;
1195 }while(hres == S_OK);
1197 return S_OK;
1200 typedef struct {
1201 nsIAsyncVerifyRedirectCallback nsIAsyncVerifyRedirectCallback_iface;
1203 LONG ref;
1205 nsChannel *nschannel;
1206 nsChannelBSC *bsc;
1207 } nsRedirectCallback;
1209 static nsRedirectCallback *impl_from_nsIAsyncVerifyRedirectCallback(nsIAsyncVerifyRedirectCallback *iface)
1211 return CONTAINING_RECORD(iface, nsRedirectCallback, nsIAsyncVerifyRedirectCallback_iface);
1214 static nsresult NSAPI nsAsyncVerifyRedirectCallback_QueryInterface(nsIAsyncVerifyRedirectCallback *iface,
1215 nsIIDRef riid, void **result)
1217 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1219 if(IsEqualGUID(&IID_nsISupports, riid)) {
1220 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1221 *result = &This->nsIAsyncVerifyRedirectCallback_iface;
1222 }else if(IsEqualGUID(&IID_nsIAsyncVerifyRedirectCallback, riid)) {
1223 TRACE("(%p)->(IID_nsIAsyncVerifyRedirectCallback %p)\n", This, result);
1224 *result = &This->nsIAsyncVerifyRedirectCallback_iface;
1225 }else {
1226 *result = NULL;
1227 WARN("unimplemented iface %s\n", debugstr_guid(riid));
1228 return NS_NOINTERFACE;
1231 nsISupports_AddRef((nsISupports*)*result);
1232 return NS_OK;
1235 static nsrefcnt NSAPI nsAsyncVerifyRedirectCallback_AddRef(nsIAsyncVerifyRedirectCallback *iface)
1237 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1238 LONG ref = InterlockedIncrement(&This->ref);
1240 TRACE("(%p) ref=%ld\n", This, ref);
1242 return ref;
1245 static nsrefcnt NSAPI nsAsyncVerifyRedirectCallback_Release(nsIAsyncVerifyRedirectCallback *iface)
1247 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1248 LONG ref = InterlockedDecrement(&This->ref);
1250 TRACE("(%p) ref=%ld\n", This, ref);
1252 if(!ref) {
1253 IBindStatusCallback_Release(&This->bsc->bsc.IBindStatusCallback_iface);
1254 nsIHttpChannel_Release(&This->nschannel->nsIHttpChannel_iface);
1255 free(This);
1258 return ref;
1261 static nsresult NSAPI nsAsyncVerifyRedirectCallback_OnRedirectVerifyCallback(nsIAsyncVerifyRedirectCallback *iface, nsresult result)
1263 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1264 nsChannel *old_nschannel;
1265 nsresult nsres;
1267 TRACE("(%p)->(%08lx)\n", This, result);
1269 old_nschannel = This->bsc->nschannel;
1270 nsIHttpChannel_AddRef(&This->nschannel->nsIHttpChannel_iface);
1271 This->bsc->nschannel = This->nschannel;
1273 if(This->nschannel->load_group) {
1274 nsres = nsILoadGroup_AddRequest(This->nschannel->load_group, (nsIRequest*)&This->nschannel->nsIHttpChannel_iface,
1275 NULL);
1276 if(NS_FAILED(nsres))
1277 ERR("AddRequest failed: %08lx\n", nsres);
1280 if(This->bsc->is_doc_channel && This->bsc->bsc.window && This->bsc->bsc.window->base.outer_window) {
1281 IUri *uri = nsuri_get_uri(This->nschannel->uri);
1283 if(uri) {
1284 set_current_uri(This->bsc->bsc.window->base.outer_window, uri);
1285 IUri_Release(uri);
1286 }else {
1287 WARN("Could not get IUri from nsWineURI\n");
1291 if(old_nschannel) {
1292 if(old_nschannel->load_group) {
1293 nsres = nsILoadGroup_RemoveRequest(old_nschannel->load_group,
1294 (nsIRequest*)&old_nschannel->nsIHttpChannel_iface, NULL, NS_OK);
1295 if(NS_FAILED(nsres))
1296 ERR("RemoveRequest failed: %08lx\n", nsres);
1298 nsIHttpChannel_Release(&old_nschannel->nsIHttpChannel_iface);
1301 return NS_OK;
1304 static const nsIAsyncVerifyRedirectCallbackVtbl nsAsyncVerifyRedirectCallbackVtbl = {
1305 nsAsyncVerifyRedirectCallback_QueryInterface,
1306 nsAsyncVerifyRedirectCallback_AddRef,
1307 nsAsyncVerifyRedirectCallback_Release,
1308 nsAsyncVerifyRedirectCallback_OnRedirectVerifyCallback
1311 static HRESULT create_redirect_callback(nsChannel *nschannel, nsChannelBSC *bsc, nsRedirectCallback **ret)
1313 nsRedirectCallback *callback;
1315 callback = malloc(sizeof(*callback));
1316 if(!callback)
1317 return E_OUTOFMEMORY;
1319 callback->nsIAsyncVerifyRedirectCallback_iface.lpVtbl = &nsAsyncVerifyRedirectCallbackVtbl;
1320 callback->ref = 1;
1322 nsIHttpChannel_AddRef(&nschannel->nsIHttpChannel_iface);
1323 callback->nschannel = nschannel;
1325 IBindStatusCallback_AddRef(&bsc->bsc.IBindStatusCallback_iface);
1326 callback->bsc = bsc;
1328 *ret = callback;
1329 return S_OK;
1332 static inline nsChannelBSC *nsChannelBSC_from_BSCallback(BSCallback *iface)
1334 return CONTAINING_RECORD(iface, nsChannelBSC, bsc);
1337 static void nsChannelBSC_destroy(BSCallback *bsc)
1339 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1341 if(This->nschannel) {
1342 if(This->nschannel->binding == This)
1343 This->nschannel->binding = NULL;
1344 nsIHttpChannel_Release(&This->nschannel->nsIHttpChannel_iface);
1346 if(This->nslistener)
1347 nsIStreamListener_Release(This->nslistener);
1348 if(This->nscontext)
1349 nsISupports_Release(This->nscontext);
1350 if(This->nsstream)
1351 nsIInputStream_Release(&This->nsstream->nsIInputStream_iface);
1352 free(This);
1355 static HRESULT nsChannelBSC_start_binding(BSCallback *bsc)
1357 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1359 if(This->is_doc_channel) {
1360 DWORD flags = This->bsc.window->base.outer_window->load_flags;
1362 if(flags & BINDING_FROMHIST)
1363 This->bsc.window->performance_timing->navigation_type = 2; /* TYPE_BACK_FORWARD */
1364 if(flags & BINDING_REFRESH)
1365 This->bsc.window->performance_timing->navigation_type = 1; /* TYPE_RELOAD */
1367 This->bsc.window->base.outer_window->base.inner_window->doc->skip_mutation_notif = FALSE;
1368 This->bsc.window->performance_timing->navigation_start_time = get_time_stamp();
1371 return S_OK;
1374 static HRESULT nsChannelBSC_init_bindinfo(BSCallback *bsc)
1376 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1377 nsChannel *nschannel = This->nschannel;
1378 GeckoBrowser *browser;
1379 HRESULT hres;
1381 if(This->is_doc_channel && This->bsc.window && This->bsc.window->base.outer_window
1382 && (browser = This->bsc.window->base.outer_window->browser)) {
1383 if(browser->doc->hostinfo.dwFlags & DOCHOSTUIFLAG_ENABLE_REDIRECT_NOTIFICATION)
1384 This->bsc.bindinfo_options |= BINDINFO_OPTIONS_DISABLEAUTOREDIRECTS;
1387 if(nschannel && nschannel->post_data_stream) {
1388 hres = read_post_data_stream(nschannel->post_data_stream, nschannel->post_data_contains_headers,
1389 &nschannel->request_headers, &This->bsc.request_data);
1390 if(FAILED(hres))
1391 return hres;
1394 return S_OK;
1397 typedef struct {
1398 task_t header;
1399 nsChannelBSC *bsc;
1400 } stop_request_task_t;
1402 static void stop_request_proc(task_t *_task)
1404 stop_request_task_t *task = (stop_request_task_t*)_task;
1406 TRACE("(%p)\n", task->bsc);
1408 list_remove(&task->bsc->bsc.entry);
1409 list_init(&task->bsc->bsc.entry);
1410 on_stop_nsrequest(task->bsc, S_OK);
1413 static void stop_request_task_destr(task_t *_task)
1415 stop_request_task_t *task = (stop_request_task_t*)_task;
1417 IBindStatusCallback_Release(&task->bsc->bsc.IBindStatusCallback_iface);
1420 static HRESULT async_stop_request(nsChannelBSC *This)
1422 HTMLInnerWindow *window = This->bsc.window;
1423 stop_request_task_t *task;
1424 HRESULT hres;
1426 IHTMLWindow2_AddRef(&window->base.IHTMLWindow2_iface);
1427 if(!This->bsc.read) {
1428 TRACE("No data read, calling OnStartRequest\n");
1429 on_start_nsrequest(This);
1432 task = malloc(sizeof(*task));
1433 if(!task)
1434 return E_OUTOFMEMORY;
1436 IBindStatusCallback_AddRef(&This->bsc.IBindStatusCallback_iface);
1437 task->bsc = This;
1439 hres = push_task(&task->header, stop_request_proc, stop_request_task_destr, window->task_magic);
1440 IHTMLWindow2_Release(&window->base.IHTMLWindow2_iface);
1441 return hres;
1444 static void handle_navigation_error(nsChannelBSC *This, DWORD result)
1446 HTMLOuterWindow *outer_window;
1447 HTMLDocumentObj *doc;
1448 BOOL is_error_url;
1449 SAFEARRAY *sa;
1450 SAFEARRAYBOUND bound;
1451 VARIANT var, varOut;
1452 LONG ind;
1453 BSTR unk;
1454 HRESULT hres;
1456 if(!This->is_doc_channel || !This->bsc.window || !This->bsc.window->base.outer_window
1457 || !This->bsc.window->base.outer_window->browser)
1458 return;
1460 outer_window = This->bsc.window->base.outer_window;
1461 doc = outer_window->browser->doc;
1462 if(!doc->doc_object_service || !doc->client)
1463 return;
1465 hres = IDocObjectService_IsErrorUrl(doc->doc_object_service,
1466 outer_window->url, &is_error_url);
1467 if(FAILED(hres) || is_error_url)
1468 return;
1470 if(!doc->client_cmdtrg)
1471 return;
1473 bound.lLbound = 0;
1474 bound.cElements = 8;
1475 sa = SafeArrayCreate(VT_VARIANT, 1, &bound);
1476 if(!sa)
1477 return;
1479 ind = 0;
1480 V_VT(&var) = VT_I4;
1481 V_I4(&var) = result;
1482 SafeArrayPutElement(sa, &ind, &var);
1484 ind = 1;
1485 V_VT(&var) = VT_BSTR;
1486 V_BSTR(&var) = outer_window->url;
1487 SafeArrayPutElement(sa, &ind, &var);
1489 ind = 3;
1490 V_VT(&var) = VT_UNKNOWN;
1491 V_UNKNOWN(&var) = (IUnknown*)&outer_window->base.IHTMLWindow2_iface;
1492 SafeArrayPutElement(sa, &ind, &var);
1494 /* FIXME: what are the following fields for? */
1495 ind = 2;
1496 V_VT(&var) = VT_UNKNOWN;
1497 V_UNKNOWN(&var) = NULL;
1498 SafeArrayPutElement(sa, &ind, &var);
1500 ind = 4;
1501 V_VT(&var) = VT_BOOL;
1502 V_BOOL(&var) = FALSE;
1503 SafeArrayPutElement(sa, &ind, &var);
1505 ind = 5;
1506 V_VT(&var) = VT_BOOL;
1507 V_BOOL(&var) = FALSE;
1508 SafeArrayPutElement(sa, &ind, &var);
1510 ind = 6;
1511 V_VT(&var) = VT_BSTR;
1512 unk = SysAllocString(NULL);
1513 V_BSTR(&var) = unk;
1514 SafeArrayPutElement(sa, &ind, &var);
1516 ind = 7;
1517 V_VT(&var) = VT_UNKNOWN;
1518 V_UNKNOWN(&var) = NULL;
1519 SafeArrayPutElement(sa, &ind, &var);
1521 V_VT(&var) = VT_ARRAY;
1522 V_ARRAY(&var) = sa;
1523 V_VT(&varOut) = VT_BOOL;
1524 V_BOOL(&varOut) = VARIANT_TRUE;
1525 IOleCommandTarget_Exec(doc->client_cmdtrg, &CGID_DocHostCmdPriv, 1, 0, &var, FAILED(hres)?NULL:&varOut);
1527 SysFreeString(unk);
1528 SafeArrayDestroy(sa);
1531 static HRESULT nsChannelBSC_stop_binding(BSCallback *bsc, HRESULT result)
1533 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1535 if(This->is_doc_channel && This->bsc.window) {
1536 This->bsc.window->performance_timing->response_end_time = get_time_stamp();
1537 if(result != E_ABORT) {
1538 if(FAILED(result))
1539 handle_navigation_error(This, result);
1540 else if(This->nschannel) {
1541 result = async_stop_request(This);
1542 if(SUCCEEDED(result))
1543 return S_OK;
1548 on_stop_nsrequest(This, result);
1549 return S_OK;
1552 static HRESULT nsChannelBSC_read_data(BSCallback *bsc, IStream *stream)
1554 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1556 return read_stream_data(This, stream);
1559 static HRESULT handle_redirect(nsChannelBSC *This, const WCHAR *new_url)
1561 nsRedirectCallback *callback;
1562 nsIChannelEventSink *sink;
1563 nsChannel *new_channel;
1564 IMoniker *mon;
1565 nsresult nsres;
1566 HRESULT hres;
1568 TRACE("(%p)->(%s)\n", This, debugstr_w(new_url));
1570 hres = CreateURLMoniker(NULL, new_url, &mon);
1571 if(FAILED(hres))
1572 return hres;
1573 IMoniker_Release(This->bsc.mon);
1574 This->bsc.mon = mon;
1576 if(!This->nschannel || !This->nschannel->notif_callback)
1577 return S_OK;
1579 nsres = nsIInterfaceRequestor_GetInterface(This->nschannel->notif_callback, &IID_nsIChannelEventSink, (void**)&sink);
1580 if(NS_FAILED(nsres))
1581 return S_OK;
1583 hres = create_redirect_nschannel(new_url, This->nschannel, &new_channel);
1584 if(SUCCEEDED(hres)) {
1585 TRACE("%p %p->%p\n", This, This->nschannel, new_channel);
1587 hres = create_redirect_callback(new_channel, This, &callback);
1588 nsIHttpChannel_Release(&new_channel->nsIHttpChannel_iface);
1591 if(SUCCEEDED(hres)) {
1592 nsres = nsIChannelEventSink_AsyncOnChannelRedirect(sink, (nsIChannel*)&This->nschannel->nsIHttpChannel_iface,
1593 (nsIChannel*)&callback->nschannel->nsIHttpChannel_iface, REDIRECT_TEMPORARY, /* FIXME */
1594 &callback->nsIAsyncVerifyRedirectCallback_iface);
1596 if(NS_FAILED(nsres))
1597 FIXME("AsyncOnChannelRedirect failed: %08lx\n", hres);
1598 else if(This->nschannel != callback->nschannel)
1599 FIXME("nschannel not updated\n");
1601 nsIAsyncVerifyRedirectCallback_Release(&callback->nsIAsyncVerifyRedirectCallback_iface);
1604 nsIChannelEventSink_Release(sink);
1605 return hres;
1608 static BOOL is_supported_doc_mime(const WCHAR *mime)
1610 char *nscat, *mimea;
1611 BOOL ret;
1613 mimea = strdupWtoA(mime);
1614 if(!mimea)
1615 return FALSE;
1617 nscat = get_nscategory_entry("Gecko-Content-Viewers", mimea);
1619 ret = nscat != NULL && !strcmp(nscat, "@mozilla.org/content/document-loader-factory;1");
1621 free(mimea);
1622 nsfree(nscat);
1623 return ret;
1626 static IUri *get_moniker_uri(IMoniker *mon)
1628 IUriContainer *uri_container;
1629 IUri *ret = NULL;
1630 HRESULT hres;
1632 hres = IMoniker_QueryInterface(mon, &IID_IUriContainer, (void**)&uri_container);
1633 if(SUCCEEDED(hres)) {
1634 hres = IUriContainer_GetIUri(uri_container, &ret);
1635 IUriContainer_Release(uri_container);
1636 if(FAILED(hres))
1637 return NULL;
1638 }else {
1639 FIXME("No IUriContainer\n");
1642 return ret;
1645 static void handle_extern_mime_navigation(nsChannelBSC *This)
1647 IWebBrowserPriv2IE9 *webbrowser_priv;
1648 IOleCommandTarget *cmdtrg;
1649 HTMLDocumentObj *doc_obj;
1650 IBindCtx *bind_ctx;
1651 IUri *uri;
1652 VARIANT flags;
1653 HRESULT hres;
1655 if(!This->bsc.window || !This->bsc.window->base.outer_window || !This->bsc.window->base.outer_window->browser)
1656 return;
1658 doc_obj = This->bsc.window->base.outer_window->browser->doc;
1659 IUnknown_AddRef(doc_obj->outer_unk);
1661 hres = IOleClientSite_QueryInterface(doc_obj->client, &IID_IOleCommandTarget, (void**)&cmdtrg);
1662 if(SUCCEEDED(hres)) {
1663 IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 62, 0, NULL, NULL);
1664 IOleCommandTarget_Release(cmdtrg);
1667 set_document_navigation(doc_obj, FALSE);
1669 if(!doc_obj->webbrowser) {
1670 FIXME("unimplemented in non-webbrowser mode\n");
1671 goto done;
1674 uri = get_moniker_uri(This->bsc.mon);
1675 if(!uri)
1676 goto done;
1678 hres = CreateBindCtx(0, &bind_ctx);
1679 if(FAILED(hres)) {
1680 IUri_Release(uri);
1681 goto done;
1684 V_VT(&flags) = VT_I4;
1685 V_I4(&flags) = navHyperlink;
1687 hres = IUnknown_QueryInterface(doc_obj->webbrowser, &IID_IWebBrowserPriv2IE8, (void**)&webbrowser_priv);
1688 if(SUCCEEDED(hres)) {
1689 hres = IWebBrowserPriv2IE9_NavigateWithBindCtx2(webbrowser_priv, uri, &flags, NULL, NULL, NULL, bind_ctx, NULL, 0);
1690 IWebBrowserPriv2IE9_Release(webbrowser_priv);
1691 }else {
1692 IWebBrowserPriv *webbrowser_priv_old;
1693 VARIANT uriv;
1695 hres = IUnknown_QueryInterface(doc_obj->webbrowser, &IID_IWebBrowserPriv, (void**)&webbrowser_priv_old);
1696 if(SUCCEEDED(hres)) {
1697 V_VT(&uriv) = VT_BSTR;
1698 V_BSTR(&uriv) = NULL;
1699 hres = IUri_GetDisplayUri(uri, &V_BSTR(&uriv));
1701 if(hres == S_OK)
1702 hres = IWebBrowserPriv_NavigateWithBindCtx(webbrowser_priv_old, &uriv, &flags, NULL, NULL, NULL, bind_ctx, NULL);
1704 SysFreeString(V_BSTR(&uriv));
1705 IWebBrowserPriv_Release(webbrowser_priv_old);
1709 IUri_Release(uri);
1711 done:
1712 IUnknown_Release(doc_obj->outer_unk);
1715 static HRESULT nsChannelBSC_on_progress(BSCallback *bsc, ULONG progress, ULONG total, ULONG status_code, LPCWSTR status_text)
1717 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1719 switch(status_code) {
1720 case BINDSTATUS_MIMETYPEAVAILABLE:
1721 if(This->is_doc_channel && !is_supported_doc_mime(status_text)) {
1722 FIXME("External MIME: %s\n", debugstr_w(status_text));
1724 handle_extern_mime_navigation(This);
1726 This->nschannel = NULL;
1729 if(!This->nschannel ||
1730 (This->nschannel->content_type && !(This->nschannel->load_flags & LOAD_CALL_CONTENT_SNIFFERS)))
1731 return S_OK;
1733 free(This->nschannel->content_type);
1734 This->nschannel->content_type = strdupWtoA(status_text);
1735 break;
1736 case BINDSTATUS_REDIRECTING:
1737 if(This->is_doc_channel) {
1738 This->bsc.window->performance_timing->redirect_count++;
1739 if(!This->bsc.window->performance_timing->redirect_time)
1740 This->bsc.window->performance_timing->redirect_time = get_time_stamp();
1742 return handle_redirect(This, status_text);
1743 case BINDSTATUS_FINDINGRESOURCE:
1744 if(This->is_doc_channel && !This->bsc.window->performance_timing->dns_lookup_time)
1745 This->bsc.window->performance_timing->dns_lookup_time = get_time_stamp();
1746 break;
1747 case BINDSTATUS_CONNECTING:
1748 if(This->is_doc_channel)
1749 This->bsc.window->performance_timing->connect_time = get_time_stamp();
1750 break;
1751 case BINDSTATUS_SENDINGREQUEST:
1752 if(This->is_doc_channel)
1753 This->bsc.window->performance_timing->request_time = get_time_stamp();
1754 break;
1755 case BINDSTATUS_BEGINDOWNLOADDATA: {
1756 IWinInetHttpInfo *http_info;
1757 DWORD status, size = sizeof(DWORD);
1758 HRESULT hres;
1760 if(This->bsc.binding) {
1761 hres = IBinding_QueryInterface(This->bsc.binding, &IID_IWinInetHttpInfo, (void**)&http_info);
1762 if(SUCCEEDED(hres)) {
1763 hres = IWinInetHttpInfo_QueryInfo(http_info,
1764 HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL, NULL);
1765 IWinInetHttpInfo_Release(http_info);
1766 if(SUCCEEDED(hres) && status != HTTP_STATUS_OK)
1767 handle_navigation_error(This, status);
1770 /* fall through */
1772 case BINDSTATUS_DOWNLOADINGDATA:
1773 case BINDSTATUS_ENDDOWNLOADDATA:
1774 /* Defer it to just before calling OnDataAvailable, otherwise it can have wrong state */
1775 This->progress = progress;
1776 This->total = total;
1777 break;
1780 return S_OK;
1783 static HRESULT process_response_status_text(const WCHAR *header, const WCHAR *header_end, char **status_text)
1785 header = wcschr(header + 1, ' ');
1786 if(!header || header >= header_end)
1787 return E_FAIL;
1788 header = wcschr(header + 1, ' ');
1789 if(!header || header >= header_end)
1790 header = header_end;
1791 else
1792 ++header;
1794 *status_text = strndupWtoU(header, header_end - header);
1796 if(!*status_text)
1797 return E_OUTOFMEMORY;
1799 return S_OK;
1802 static HRESULT nsChannelBSC_on_response(BSCallback *bsc, DWORD response_code,
1803 LPCWSTR response_headers)
1805 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1806 char *str;
1807 HRESULT hres;
1809 if(This->is_doc_channel)
1810 This->bsc.window->performance_timing->response_start_time = get_time_stamp();
1812 This->response_processed = TRUE;
1813 This->nschannel->response_status = response_code;
1815 if(response_headers) {
1816 const WCHAR *headers;
1818 headers = wcschr(response_headers, '\r');
1819 hres = process_response_status_text(response_headers, headers, &str);
1820 if(FAILED(hres)) {
1821 WARN("parsing headers failed: %08lx\n", hres);
1822 return hres;
1825 free(This->nschannel->response_status_text);
1826 This->nschannel->response_status_text = str;
1828 if(headers && headers[1] == '\n') {
1829 headers += 2;
1830 hres = process_response_headers(This, headers);
1831 if(FAILED(hres)) {
1832 WARN("parsing headers failed: %08lx\n", hres);
1833 return hres;
1838 return S_OK;
1841 static HRESULT nsChannelBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
1843 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1844 http_header_t *iter;
1845 DWORD len = 0;
1846 WCHAR *ptr;
1848 if(!This->nschannel)
1849 return S_FALSE;
1851 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->request_headers, http_header_t, entry) {
1852 if(wcscmp(iter->header, L"Content-Length"))
1853 len += lstrlenW(iter->header) + 2 /* ": " */ + lstrlenW(iter->data) + 2 /* "\r\n" */;
1856 if(!len)
1857 return S_OK;
1859 *additional_headers = ptr = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
1860 if(!ptr)
1861 return E_OUTOFMEMORY;
1863 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->request_headers, http_header_t, entry) {
1864 if(!wcscmp(iter->header, L"Content-Length"))
1865 continue;
1867 len = lstrlenW(iter->header);
1868 memcpy(ptr, iter->header, len*sizeof(WCHAR));
1869 ptr += len;
1871 *ptr++ = ':';
1872 *ptr++ = ' ';
1874 len = lstrlenW(iter->data);
1875 memcpy(ptr, iter->data, len*sizeof(WCHAR));
1876 ptr += len;
1878 *ptr++ = '\r';
1879 *ptr++ = '\n';
1882 *ptr = 0;
1884 return S_OK;
1887 static const BSCallbackVtbl nsChannelBSCVtbl = {
1888 nsChannelBSC_destroy,
1889 nsChannelBSC_init_bindinfo,
1890 nsChannelBSC_start_binding,
1891 nsChannelBSC_stop_binding,
1892 nsChannelBSC_read_data,
1893 nsChannelBSC_on_progress,
1894 nsChannelBSC_on_response,
1895 nsChannelBSC_beginning_transaction
1898 HRESULT create_channelbsc(IMoniker *mon, const WCHAR *headers, BYTE *post_data, DWORD post_data_size,
1899 BOOL is_doc_binding, nsChannelBSC **retval)
1901 nsChannelBSC *ret;
1902 DWORD bindf;
1904 ret = calloc(1, sizeof(*ret));
1905 if(!ret)
1906 return E_OUTOFMEMORY;
1908 bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA;
1909 if(post_data_size)
1910 bindf |= BINDF_FORMS_SUBMIT | BINDF_PRAGMA_NO_CACHE | BINDF_HYPERLINK | BINDF_GETNEWESTVERSION;
1912 init_bscallback(&ret->bsc, &nsChannelBSCVtbl, mon, bindf);
1913 ret->is_doc_channel = is_doc_binding;
1915 if(headers) {
1916 ret->bsc.request_data.headers = wcsdup(headers);
1917 if(!ret->bsc.request_data.headers) {
1918 IBindStatusCallback_Release(&ret->bsc.IBindStatusCallback_iface);
1919 return E_OUTOFMEMORY;
1923 if(post_data) {
1924 ret->bsc.request_data.post_data = GlobalAlloc(0, post_data_size+1);
1925 if(!ret->bsc.request_data.post_data) {
1926 release_request_data(&ret->bsc.request_data);
1927 IBindStatusCallback_Release(&ret->bsc.IBindStatusCallback_iface);
1928 return E_OUTOFMEMORY;
1931 memcpy(ret->bsc.request_data.post_data, post_data, post_data_size);
1932 ((BYTE*)ret->bsc.request_data.post_data)[post_data_size] = 0;
1933 ret->bsc.request_data.post_data_len = post_data_size;
1936 TRACE("created %p\n", ret);
1937 *retval = ret;
1938 return S_OK;
1941 typedef struct {
1942 task_t header;
1943 DWORD flags;
1944 HTMLOuterWindow *window;
1945 HTMLInnerWindow *pending_window;
1946 } start_doc_binding_task_t;
1948 static void start_doc_binding_proc(task_t *_task)
1950 start_doc_binding_task_t *task = (start_doc_binding_task_t*)_task;
1951 HTMLOuterWindow *window = task->window;
1953 IHTMLWindow2_AddRef(&window->base.IHTMLWindow2_iface);
1954 set_current_mon(window, task->pending_window->bscallback->bsc.mon, task->flags);
1955 IHTMLWindow2_Release(&window->base.IHTMLWindow2_iface);
1957 start_binding(task->pending_window, &task->pending_window->bscallback->bsc, NULL);
1960 static void start_doc_binding_task_destr(task_t *_task)
1962 start_doc_binding_task_t *task = (start_doc_binding_task_t*)_task;
1964 IHTMLWindow2_Release(&task->pending_window->base.IHTMLWindow2_iface);
1967 HRESULT async_start_doc_binding(HTMLOuterWindow *window, HTMLInnerWindow *pending_window, DWORD flags)
1969 start_doc_binding_task_t *task;
1971 TRACE("%p\n", pending_window);
1973 task = malloc(sizeof(start_doc_binding_task_t));
1974 if(!task)
1975 return E_OUTOFMEMORY;
1977 task->flags = flags;
1978 task->window = window;
1979 task->pending_window = pending_window;
1980 IHTMLWindow2_AddRef(&pending_window->base.IHTMLWindow2_iface);
1982 return push_task(&task->header, start_doc_binding_proc, start_doc_binding_task_destr, pending_window->task_magic);
1985 void abort_window_bindings(HTMLInnerWindow *window)
1987 BSCallback *iter;
1989 remove_target_tasks(window->task_magic);
1991 while(!list_empty(&window->bindings)) {
1992 iter = LIST_ENTRY(window->bindings.next, BSCallback, entry);
1994 TRACE("Aborting %p\n", iter);
1996 IBindStatusCallback_AddRef(&iter->IBindStatusCallback_iface);
1998 if(iter->binding)
1999 IBinding_Abort(iter->binding);
2000 else
2001 iter->vtbl->stop_binding(iter, E_ABORT);
2003 iter->window = NULL;
2004 list_remove(&iter->entry);
2005 list_init(&iter->entry);
2007 IBindStatusCallback_Release(&iter->IBindStatusCallback_iface);
2010 if(window->bscallback) {
2011 IBindStatusCallback_Release(&window->bscallback->bsc.IBindStatusCallback_iface);
2012 window->bscallback = NULL;
2015 if(window->mon) {
2016 IMoniker_Release(window->mon);
2017 window->mon = NULL;
2021 HRESULT channelbsc_load_stream(HTMLInnerWindow *pending_window, IMoniker *mon, IStream *stream)
2023 HTMLOuterWindow *window = pending_window->base.outer_window;
2024 nsChannelBSC *bscallback = pending_window->bscallback;
2025 HRESULT hres = S_OK;
2027 if(!bscallback->nschannel) {
2028 ERR("NULL nschannel\n");
2029 return E_FAIL;
2032 bscallback->nschannel->content_type = strdup("text/html");
2033 if(!bscallback->nschannel->content_type)
2034 return E_OUTOFMEMORY;
2036 IHTMLWindow2_AddRef(&window->base.IHTMLWindow2_iface);
2037 set_current_mon(window, mon, 0);
2038 IHTMLWindow2_Release(&window->base.IHTMLWindow2_iface);
2040 bscallback->bsc.window = pending_window;
2041 if(stream)
2042 hres = read_stream_data(bscallback, stream);
2043 if(SUCCEEDED(hres))
2044 hres = async_stop_request(bscallback);
2045 if(FAILED(hres))
2046 IBindStatusCallback_OnStopBinding(&bscallback->bsc.IBindStatusCallback_iface, hres, NULL);
2048 return hres;
2051 void channelbsc_set_channel(nsChannelBSC *This, nsChannel *channel, nsIStreamListener *listener, nsISupports *context)
2053 nsIHttpChannel_AddRef(&channel->nsIHttpChannel_iface);
2054 This->nschannel = channel;
2056 nsIStreamListener_AddRef(listener);
2057 This->nslistener = listener;
2059 if(context) {
2060 nsISupports_AddRef(context);
2061 This->nscontext = context;
2064 if(This->bsc.request_data.headers) {
2065 HRESULT hres;
2067 hres = parse_headers(This->bsc.request_data.headers, &channel->request_headers);
2068 free(This->bsc.request_data.headers);
2069 This->bsc.request_data.headers = NULL;
2070 if(FAILED(hres))
2071 WARN("parse_headers failed: %08lx\n", hres);
2075 typedef struct {
2076 task_t header;
2077 HTMLOuterWindow *window;
2078 IUri *uri;
2079 } navigate_javascript_task_t;
2081 static void navigate_javascript_proc(task_t *_task)
2083 navigate_javascript_task_t *task = (navigate_javascript_task_t*)_task;
2084 HTMLInnerWindow *inner_window = task->window->base.inner_window;
2085 HTMLOuterWindow *window = task->window;
2086 HTMLDocumentObj *doc = NULL;
2087 BSTR code = NULL;
2088 VARIANT v;
2089 HRESULT hres;
2091 window->readystate = READYSTATE_COMPLETE;
2092 if(window->browser) {
2093 doc = window->browser->doc;
2094 IUnknown_AddRef(doc->outer_unk);
2096 IHTMLWindow2_AddRef(&inner_window->base.IHTMLWindow2_iface);
2098 hres = IUri_GetPath(task->uri, &code);
2099 if(hres != S_OK) {
2100 SysFreeString(code);
2101 goto done;
2104 hres = UrlUnescapeW(code, NULL, NULL, URL_UNESCAPE_INPLACE);
2105 if(FAILED(hres)) {
2106 SysFreeString(code);
2107 goto done;
2110 if(doc)
2111 set_download_state(doc, 1);
2113 V_VT(&v) = VT_EMPTY;
2114 hres = exec_script(inner_window, code, L"jscript", &v);
2115 SysFreeString(code);
2116 if(SUCCEEDED(hres) && V_VT(&v) != VT_EMPTY) {
2117 FIXME("javascirpt URL returned %s\n", debugstr_variant(&v));
2118 VariantClear(&v);
2121 if(doc) {
2122 if(doc->view_sink)
2123 IAdviseSink_OnViewChange(doc->view_sink, DVASPECT_CONTENT, -1);
2125 set_download_state(doc, 0);
2128 done:
2129 IHTMLWindow2_Release(&inner_window->base.IHTMLWindow2_iface);
2130 if(doc)
2131 IUnknown_Release(doc->outer_unk);
2134 static void navigate_javascript_task_destr(task_t *_task)
2136 navigate_javascript_task_t *task = (navigate_javascript_task_t*)_task;
2138 IUri_Release(task->uri);
2141 typedef struct {
2142 task_t header;
2143 HTMLOuterWindow *window;
2144 nsChannelBSC *bscallback;
2145 DWORD flags;
2146 IMoniker *mon;
2147 IUri *uri;
2148 } navigate_task_t;
2150 static void navigate_proc(task_t *_task)
2152 navigate_task_t *task = (navigate_task_t*)_task;
2153 HTMLOuterWindow *window = task->window;
2154 HRESULT hres;
2156 IHTMLWindow2_AddRef(&window->base.IHTMLWindow2_iface);
2158 hres = set_moniker(window, task->mon, task->uri, NULL, task->bscallback, TRUE);
2159 if(SUCCEEDED(hres)) {
2160 set_current_mon(window, task->bscallback->bsc.mon, task->flags);
2161 set_current_uri(window, task->uri);
2162 start_binding(window->pending_window, &task->bscallback->bsc, NULL);
2165 IHTMLWindow2_Release(&window->base.IHTMLWindow2_iface);
2168 static void navigate_task_destr(task_t *_task)
2170 navigate_task_t *task = (navigate_task_t*)_task;
2172 IBindStatusCallback_Release(&task->bscallback->bsc.IBindStatusCallback_iface);
2173 IMoniker_Release(task->mon);
2174 IUri_Release(task->uri);
2177 static HRESULT navigate_fragment(HTMLOuterWindow *window, IUri *uri)
2179 nsIDOMLocation *nslocation;
2180 nsAString nsfrag_str;
2181 BSTR frag = NULL;
2182 WCHAR *selector;
2183 nsresult nsres;
2184 HRESULT hres;
2185 static const WCHAR selector_formatW[] = L"a[id=\"%s\"]";
2187 set_current_uri(window, uri);
2189 nsres = nsIDOMWindow_GetLocation(window->nswindow, &nslocation);
2190 if(FAILED(nsres) || !nslocation)
2191 return E_FAIL;
2193 hres = IUri_GetFragment(uri, &frag);
2194 if(hres != S_OK) {
2195 SysFreeString(frag);
2196 nsIDOMLocation_Release(nslocation);
2197 return FAILED(hres) ? hres : S_OK;
2200 nsAString_InitDepend(&nsfrag_str, frag);
2201 nsres = nsIDOMLocation_SetHash(nslocation, &nsfrag_str);
2202 nsAString_Finish(&nsfrag_str);
2203 nsIDOMLocation_Release(nslocation);
2204 if(NS_FAILED(nsres))
2205 ERR("SetHash failed: %08lx\n", nsres);
2208 * IE supports scrolling to anchor elements with "#hash" ids (note that '#' is part of the id),
2209 * while Gecko scrolls only to elements with "hash" ids. We scroll the page ourselves if
2210 * a[id="#hash"] element can be found.
2212 selector = malloc(sizeof(selector_formatW) + SysStringLen(frag) * sizeof(WCHAR));
2213 if(selector) {
2214 nsIDOMElement *nselem = NULL;
2215 nsAString selector_str;
2217 swprintf(selector, ARRAY_SIZE(selector_formatW)+SysStringLen(frag), selector_formatW, frag);
2218 nsAString_InitDepend(&selector_str, selector);
2219 /* NOTE: Gecko doesn't set result to NULL if there is no match, so nselem must be initialized */
2220 nsres = nsIDOMDocument_QuerySelector(window->base.inner_window->doc->dom_document, &selector_str, &nselem);
2221 nsAString_Finish(&selector_str);
2222 free(selector);
2223 if(NS_SUCCEEDED(nsres) && nselem) {
2224 nsIDOMHTMLElement *html_elem;
2226 nsres = nsIDOMElement_QueryInterface(nselem, &IID_nsIDOMHTMLElement, (void**)&html_elem);
2227 nsIDOMElement_Release(nselem);
2228 if(NS_SUCCEEDED(nsres)) {
2229 nsIDOMHTMLElement_ScrollIntoView(html_elem, TRUE, 1);
2230 nsIDOMHTMLElement_Release(html_elem);
2235 SysFreeString(frag);
2237 if(window->browser->doc->doc_object_service) {
2238 HTMLDocumentObj *doc_obj = window->browser->doc;
2240 IUnknown_AddRef(doc_obj->outer_unk);
2241 IDocObjectService_FireNavigateComplete2(doc_obj->doc_object_service, &window->base.IHTMLWindow2_iface, 0x10);
2242 IDocObjectService_FireDocumentComplete(doc_obj->doc_object_service, &window->base.IHTMLWindow2_iface, 0);
2243 IUnknown_Release(doc_obj->outer_unk);
2246 return S_OK;
2249 HRESULT super_navigate(HTMLOuterWindow *window, IUri *uri, DWORD flags, const WCHAR *headers, BYTE *post_data, DWORD post_data_size)
2251 HTMLDocumentObj *doc_obj;
2252 nsChannelBSC *bsc;
2253 IUri *uri_nofrag;
2254 IMoniker *mon;
2255 DWORD scheme;
2256 HRESULT hres;
2258 uri_nofrag = get_uri_nofrag(uri);
2259 if(!uri_nofrag)
2260 return E_FAIL;
2262 doc_obj = window->browser->doc;
2263 IUnknown_AddRef(doc_obj->outer_unk);
2265 if(doc_obj->client && !(flags & BINDING_REFRESH)) {
2266 IOleCommandTarget *cmdtrg;
2268 hres = IOleClientSite_QueryInterface(doc_obj->client, &IID_IOleCommandTarget, (void**)&cmdtrg);
2269 if(SUCCEEDED(hres)) {
2270 VARIANT in, out;
2271 BSTR url_str;
2273 hres = IUri_GetDisplayUri(uri_nofrag, &url_str);
2274 if(SUCCEEDED(hres)) {
2275 V_VT(&in) = VT_BSTR;
2276 V_BSTR(&in) = url_str;
2277 V_VT(&out) = VT_BOOL;
2278 V_BOOL(&out) = VARIANT_TRUE;
2279 hres = IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 67, 0, &in, &out);
2280 IOleCommandTarget_Release(cmdtrg);
2281 if(SUCCEEDED(hres))
2282 VariantClear(&out);
2283 SysFreeString(url_str);
2288 if(!(flags & BINDING_NOFRAG) && window->uri_nofrag && !post_data_size) {
2289 BOOL eq;
2291 hres = IUri_IsEqual(uri_nofrag, window->uri_nofrag, &eq);
2292 if(SUCCEEDED(hres) && eq) {
2293 IUri_Release(uri_nofrag);
2294 TRACE("fragment navigate\n");
2295 hres = navigate_fragment(window, uri);
2296 goto done;
2300 hres = CreateURLMonikerEx2(NULL, uri_nofrag, &mon, URL_MK_UNIFORM);
2301 IUri_Release(uri_nofrag);
2302 if(FAILED(hres))
2303 goto done;
2305 /* FIXME: Why not set_ready_state? */
2306 window->readystate = READYSTATE_UNINITIALIZED;
2308 hres = create_channelbsc(mon, headers, post_data, post_data_size, TRUE, &bsc);
2309 if(FAILED(hres)) {
2310 IMoniker_Release(mon);
2311 goto done;
2314 prepare_for_binding(doc_obj, mon, flags);
2316 hres = IUri_GetScheme(uri, &scheme);
2317 if(hres == S_OK && scheme == URL_SCHEME_JAVASCRIPT) {
2318 navigate_javascript_task_t *task;
2320 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
2321 IMoniker_Release(mon);
2323 task = malloc(sizeof(*task));
2324 if(!task) {
2325 hres = E_OUTOFMEMORY;
2326 goto done;
2329 /* Why silently? */
2330 window->readystate = READYSTATE_COMPLETE;
2331 if(!(flags & BINDING_FROMHIST))
2332 call_docview_84(doc_obj);
2334 IUri_AddRef(uri);
2335 task->window = window;
2336 task->uri = uri;
2337 hres = push_task(&task->header, navigate_javascript_proc, navigate_javascript_task_destr, window->task_magic);
2338 }else if(flags & BINDING_SUBMIT) {
2339 hres = set_moniker(window, mon, uri, NULL, bsc, TRUE);
2340 if(SUCCEEDED(hres))
2341 hres = start_binding(window->pending_window, &bsc->bsc, NULL);
2342 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
2343 IMoniker_Release(mon);
2344 }else {
2345 navigate_task_t *task;
2347 task = malloc(sizeof(*task));
2348 if(!task) {
2349 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
2350 IMoniker_Release(mon);
2351 hres = E_OUTOFMEMORY;
2352 goto done;
2355 /* Silently and repeated when real loading starts? */
2356 window->readystate = READYSTATE_LOADING;
2357 if(!(flags & (BINDING_FROMHIST|BINDING_REFRESH)))
2358 call_docview_84(doc_obj);
2360 task->window = window;
2361 task->bscallback = bsc;
2362 task->flags = flags;
2363 task->mon = mon;
2365 IUri_AddRef(uri);
2366 task->uri = uri;
2367 hres = push_task(&task->header, navigate_proc, navigate_task_destr, window->task_magic);
2370 done:
2371 IUnknown_Release(doc_obj->outer_unk);
2372 return hres;
2375 HRESULT navigate_new_window(HTMLOuterWindow *window, IUri *uri, const WCHAR *name, request_data_t *request_data, IHTMLWindow2 **ret)
2377 INewWindowManager *new_window_mgr;
2378 BSTR display_uri, context_url;
2379 IWebBrowser2 *web_browser;
2380 IHTMLWindow2 *new_window;
2381 IBindCtx *bind_ctx;
2382 nsChannelBSC *bsc;
2383 HRESULT hres;
2385 if(!window->browser)
2386 return E_UNEXPECTED;
2388 if (window->browser->doc->client) {
2389 hres = do_query_service((IUnknown*)window->browser->doc->client, &SID_SNewWindowManager,
2390 &IID_INewWindowManager, (void**)&new_window_mgr);
2391 if (FAILED(hres)) {
2392 FIXME("No INewWindowManager\n");
2393 return hres;
2396 hres = IUri_GetDisplayUri(window->uri_nofrag, &context_url);
2397 if(FAILED(hres))
2398 return hres;
2400 hres = IUri_GetDisplayUri(uri, &display_uri);
2401 if(FAILED(hres)) {
2402 SysFreeString(context_url);
2403 return hres;
2406 hres = INewWindowManager_EvaluateNewWindow(new_window_mgr, display_uri, name, context_url,
2407 NULL, FALSE, window->browser->doc->has_popup ? 0 : NWMF_FIRST, 0);
2408 window->browser->doc->has_popup = TRUE;
2409 SysFreeString(display_uri);
2410 SysFreeString(context_url);
2411 INewWindowManager_Release(new_window_mgr);
2412 if(FAILED(hres)) {
2413 if(ret)
2414 *ret = NULL;
2415 return S_OK;
2420 if(request_data)
2421 hres = create_channelbsc(NULL, request_data->headers,
2422 request_data->post_data, request_data->post_data_len, FALSE,
2423 &bsc);
2424 else
2425 hres = create_channelbsc(NULL, NULL, NULL, 0, FALSE, &bsc);
2426 if(FAILED(hres))
2427 return hres;
2429 hres = CreateAsyncBindCtx(0, &bsc->bsc.IBindStatusCallback_iface, NULL, &bind_ctx);
2430 if(FAILED(hres)) {
2431 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
2432 return hres;
2435 hres = CoCreateInstance(&CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER,
2436 &IID_IWebBrowser2, (void**)&web_browser);
2437 if(SUCCEEDED(hres)) {
2438 ITargetFramePriv2 *target_frame_priv;
2440 hres = IWebBrowser2_QueryInterface(web_browser, &IID_ITargetFramePriv2, (void**)&target_frame_priv);
2441 if(SUCCEEDED(hres)) {
2442 hres = ITargetFramePriv2_AggregatedNavigation2(target_frame_priv,
2443 HLNF_DISABLEWINDOWRESTRICTIONS|HLNF_OPENINNEWWINDOW, bind_ctx, &bsc->bsc.IBindStatusCallback_iface,
2444 name, uri, L"");
2445 ITargetFramePriv2_Release(target_frame_priv);
2447 if(SUCCEEDED(hres))
2448 hres = do_query_service((IUnknown*)web_browser, &SID_SHTMLWindow, &IID_IHTMLWindow2, (void**)&new_window);
2450 if(FAILED(hres)) {
2451 IWebBrowser2_Quit(web_browser);
2452 IWebBrowser2_Release(web_browser);
2454 }else {
2455 WARN("Could not create InternetExplorer instance: %08lx\n", hres);
2458 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
2459 IBindCtx_Release(bind_ctx);
2460 if(FAILED(hres))
2461 return hres;
2463 IWebBrowser2_put_Visible(web_browser, VARIANT_TRUE);
2464 IWebBrowser2_Release(web_browser);
2466 if(ret)
2467 *ret = new_window;
2468 else
2469 IHTMLWindow2_Release(new_window);
2470 return S_OK;
2473 HRESULT hlink_frame_navigate(HTMLDocumentObj *doc, LPCWSTR url, nsChannel *nschannel, DWORD hlnf, BOOL *cancel)
2475 IHlinkFrame *hlink_frame;
2476 nsChannelBSC *callback;
2477 IBindCtx *bindctx;
2478 IMoniker *mon;
2479 IHlink *hlink;
2480 HRESULT hres;
2482 *cancel = FALSE;
2484 hres = do_query_service((IUnknown*)doc->client, &IID_IHlinkFrame, &IID_IHlinkFrame,
2485 (void**)&hlink_frame);
2486 if(FAILED(hres))
2487 return S_OK;
2489 hres = create_channelbsc(NULL, NULL, NULL, 0, FALSE, &callback);
2490 if(FAILED(hres)) {
2491 IHlinkFrame_Release(hlink_frame);
2492 return hres;
2495 if(nschannel)
2496 read_post_data_stream(nschannel->post_data_stream, nschannel->post_data_contains_headers,
2497 &nschannel->request_headers, &callback->bsc.request_data);
2499 hres = CreateAsyncBindCtx(0, &callback->bsc.IBindStatusCallback_iface, NULL, &bindctx);
2500 if(SUCCEEDED(hres))
2501 hres = CoCreateInstance(&CLSID_StdHlink, NULL, CLSCTX_INPROC_SERVER,
2502 &IID_IHlink, (LPVOID*)&hlink);
2504 if(SUCCEEDED(hres))
2505 hres = CreateURLMoniker(NULL, url, &mon);
2507 if(SUCCEEDED(hres)) {
2508 IHlink_SetMonikerReference(hlink, HLINKSETF_TARGET, mon, NULL);
2510 if(hlnf & HLNF_OPENINNEWWINDOW) {
2511 IHlink_SetTargetFrameName(hlink, L"_blank"); /* FIXME */
2514 hres = IHlinkFrame_Navigate(hlink_frame, hlnf, bindctx,
2515 &callback->bsc.IBindStatusCallback_iface, hlink);
2516 IMoniker_Release(mon);
2517 *cancel = hres == S_OK;
2518 hres = S_OK;
2521 IHlinkFrame_Release(hlink_frame);
2522 IBindCtx_Release(bindctx);
2523 IBindStatusCallback_Release(&callback->bsc.IBindStatusCallback_iface);
2524 return hres;
2527 static HRESULT navigate_uri(HTMLOuterWindow *window, IUri *uri, const WCHAR *display_uri, const request_data_t *request_data,
2528 DWORD flags)
2530 DWORD post_data_len = request_data ? request_data->post_data_len : 0;
2531 nsWineURI *nsuri;
2532 HRESULT hres;
2534 TRACE("%s\n", debugstr_w(display_uri));
2536 if(!window->browser)
2537 return E_UNEXPECTED;
2539 if(window->browser->doc->webbrowser) {
2540 void *post_data = post_data_len ? request_data->post_data : NULL;
2541 const WCHAR *headers = request_data ? request_data->headers : NULL;
2543 if(!(flags & BINDING_REFRESH)) {
2544 BSTR frame_name = NULL;
2545 BOOL cancel = FALSE;
2547 if(!is_main_content_window(window)) {
2548 hres = IHTMLWindow2_get_name(&window->base.IHTMLWindow2_iface, &frame_name);
2549 if(FAILED(hres))
2550 return hres;
2553 hres = IDocObjectService_FireBeforeNavigate2(window->browser->doc->doc_object_service, NULL, display_uri, 0x40,
2554 frame_name, post_data, post_data_len ? post_data_len+1 : 0, headers, TRUE, &cancel);
2555 SysFreeString(frame_name);
2556 if(SUCCEEDED(hres) && cancel) {
2557 TRACE("Navigation canceled\n");
2558 return S_OK;
2562 if(!window->browser)
2563 return S_OK;
2565 if(is_main_content_window(window))
2566 return super_navigate(window, uri, flags, headers, post_data, post_data_len);
2569 if(!(flags & BINDING_NOFRAG) && window->uri_nofrag && !post_data_len &&
2570 compare_uri_ignoring_frag(window->uri_nofrag, uri))
2571 return navigate_fragment(window, uri);
2573 if(is_main_content_window(window)) {
2574 BOOL cancel;
2576 hres = hlink_frame_navigate(window->base.inner_window->doc->doc_obj, display_uri, NULL, 0, &cancel);
2577 if(FAILED(hres))
2578 return hres;
2580 if(cancel) {
2581 TRACE("Navigation handled by hlink frame\n");
2582 return S_OK;
2586 hres = create_doc_uri(uri, &nsuri);
2587 if(FAILED(hres))
2588 return hres;
2590 hres = load_nsuri(window, nsuri, request_data ? request_data->post_stream : NULL, NULL,
2591 (flags & BINDING_REFRESH) ? LOAD_FLAGS_IS_REFRESH : LOAD_FLAGS_NONE);
2592 nsISupports_Release((nsISupports*)nsuri);
2593 return hres;
2596 HRESULT load_uri(HTMLOuterWindow *window, IUri *uri, DWORD flags)
2598 BSTR display_uri;
2599 HRESULT hres;
2601 hres = IUri_GetDisplayUri(uri, &display_uri);
2602 if(FAILED(hres))
2603 return hres;
2605 hres = navigate_uri(window, uri, display_uri, NULL, flags);
2606 SysFreeString(display_uri);
2607 return hres;
2610 static HRESULT translate_uri(HTMLOuterWindow *window, IUri *orig_uri, BSTR *ret_display_uri, IUri **ret_uri)
2612 IUri *uri = NULL;
2613 BSTR display_uri;
2614 HRESULT hres;
2616 hres = IUri_GetDisplayUri(orig_uri, &display_uri);
2617 if(FAILED(hres))
2618 return hres;
2620 if(window->browser->doc->hostui) {
2621 OLECHAR *translated_url = NULL;
2623 hres = IDocHostUIHandler_TranslateUrl(window->browser->doc->hostui, 0, display_uri,
2624 &translated_url);
2625 if(hres == S_OK && translated_url) {
2626 TRACE("%08lx %s -> %s\n", hres, debugstr_w(display_uri), debugstr_w(translated_url));
2627 SysFreeString(display_uri);
2628 hres = create_uri(translated_url, 0, &uri);
2629 CoTaskMemFree(translated_url);
2630 if(FAILED(hres))
2631 return hres;
2633 hres = IUri_GetDisplayUri(uri, &display_uri);
2634 if(FAILED(hres)) {
2635 IUri_Release(uri);
2636 return hres;
2641 if(!uri) {
2642 IUri_AddRef(orig_uri);
2643 uri = orig_uri;
2646 *ret_display_uri = display_uri;
2647 *ret_uri = uri;
2648 return S_OK;
2651 HRESULT submit_form(HTMLOuterWindow *window, const WCHAR *target, IUri *submit_uri, nsIInputStream *post_stream)
2653 request_data_t request_data = {NULL};
2654 HRESULT hres;
2656 hres = read_post_data_stream(post_stream, TRUE, NULL, &request_data);
2657 if(FAILED(hres))
2658 return hres;
2660 if(window) {
2661 IUri *uri;
2662 BSTR display_uri;
2664 window->readystate_locked++;
2666 hres = translate_uri(window, submit_uri, &display_uri, &uri);
2667 if(SUCCEEDED(hres)) {
2668 hres = navigate_uri(window, uri, display_uri, &request_data, BINDING_NAVIGATED|BINDING_SUBMIT);
2669 IUri_Release(uri);
2670 SysFreeString(display_uri);
2673 window->readystate_locked--;
2674 }else
2675 hres = navigate_new_window(window, submit_uri, target, &request_data, NULL);
2677 release_request_data(&request_data);
2678 return hres;
2681 HRESULT navigate_url(HTMLOuterWindow *window, const WCHAR *new_url, IUri *base_uri, DWORD flags)
2683 IUri *uri, *nav_uri;
2684 BSTR display_uri;
2685 HRESULT hres;
2687 if(new_url && base_uri)
2688 hres = CoInternetCombineUrlEx(base_uri, new_url, URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO,
2689 &nav_uri, 0);
2690 else
2691 hres = create_uri(new_url, 0, &nav_uri);
2692 if(FAILED(hres))
2693 return hres;
2694 IHTMLWindow2_AddRef(&window->base.IHTMLWindow2_iface);
2696 hres = translate_uri(window, nav_uri, &display_uri, &uri);
2697 IUri_Release(nav_uri);
2698 if(SUCCEEDED(hres)) {
2699 hres = navigate_uri(window, uri, display_uri, NULL, flags);
2700 IUri_Release(uri);
2701 SysFreeString(display_uri);
2704 IHTMLWindow2_Release(&window->base.IHTMLWindow2_iface);
2705 return hres;