ntdll: Implement RtlValidRelativeSecurityDescriptor.
[wine.git] / dlls / mshtml / navigate.c
blobed61ab01e8f7cdb33c49644dd880afd4832ce646
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 "config.h"
21 #include <stdarg.h>
22 #include <assert.h>
24 #define COBJMACROS
25 #define NONAMELESSUNION
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "winreg.h"
31 #include "ole2.h"
32 #include "hlguids.h"
33 #include "shlguid.h"
34 #include "wininet.h"
35 #include "shlwapi.h"
36 #include "htiface.h"
37 #include "shdeprecated.h"
39 #include "wine/debug.h"
41 #include "mshtml_private.h"
42 #include "htmlscript.h"
43 #include "binding.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
47 #define CONTENT_LENGTH "Content-Length"
48 #define UTF8_STR "utf-8"
49 #define UTF16_STR "utf-16"
51 static const WCHAR emptyW[] = {0};
52 static const WCHAR text_htmlW[] = {'t','e','x','t','/','h','t','m','l',0};
54 struct nsProtocolStream {
55 nsIInputStream nsIInputStream_iface;
57 LONG ref;
59 char buf[1024];
60 DWORD buf_size;
63 static inline nsProtocolStream *impl_from_nsIInputStream(nsIInputStream *iface)
65 return CONTAINING_RECORD(iface, nsProtocolStream, nsIInputStream_iface);
68 static nsresult NSAPI nsInputStream_QueryInterface(nsIInputStream *iface, nsIIDRef riid,
69 void **result)
71 nsProtocolStream *This = impl_from_nsIInputStream(iface);
73 *result = NULL;
75 if(IsEqualGUID(&IID_nsISupports, riid)) {
76 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
77 *result = &This->nsIInputStream_iface;
78 }else if(IsEqualGUID(&IID_nsIInputStream, riid)) {
79 TRACE("(%p)->(IID_nsIInputStream %p)\n", This, result);
80 *result = &This->nsIInputStream_iface;
83 if(*result) {
84 nsIInputStream_AddRef(&This->nsIInputStream_iface);
85 return NS_OK;
88 WARN("unsupported interface %s\n", debugstr_guid(riid));
89 return NS_NOINTERFACE;
92 static nsrefcnt NSAPI nsInputStream_AddRef(nsIInputStream *iface)
94 nsProtocolStream *This = impl_from_nsIInputStream(iface);
95 LONG ref = InterlockedIncrement(&This->ref);
97 TRACE("(%p) ref=%d\n", This, ref);
99 return ref;
103 static nsrefcnt NSAPI nsInputStream_Release(nsIInputStream *iface)
105 nsProtocolStream *This = impl_from_nsIInputStream(iface);
106 LONG ref = InterlockedDecrement(&This->ref);
108 TRACE("(%p) ref=%d\n", This, ref);
110 if(!ref)
111 heap_free(This);
113 return ref;
116 static nsresult NSAPI nsInputStream_Close(nsIInputStream *iface)
118 nsProtocolStream *This = impl_from_nsIInputStream(iface);
119 FIXME("(%p)\n", This);
120 return NS_ERROR_NOT_IMPLEMENTED;
123 static nsresult NSAPI nsInputStream_Available(nsIInputStream *iface, UINT64 *_retval)
125 nsProtocolStream *This = impl_from_nsIInputStream(iface);
126 FIXME("(%p)->(%p)\n", This, _retval);
127 return NS_ERROR_NOT_IMPLEMENTED;
130 static nsresult NSAPI nsInputStream_Read(nsIInputStream *iface, char *aBuf, UINT32 aCount,
131 UINT32 *_retval)
133 nsProtocolStream *This = impl_from_nsIInputStream(iface);
134 DWORD read = aCount;
136 TRACE("(%p)->(%p %d %p)\n", This, aBuf, aCount, _retval);
138 if(read > This->buf_size)
139 read = This->buf_size;
141 if(read) {
142 memcpy(aBuf, This->buf, read);
143 if(read < This->buf_size)
144 memmove(This->buf, This->buf+read, This->buf_size-read);
145 This->buf_size -= read;
148 *_retval = read;
149 return NS_OK;
152 static nsresult NSAPI nsInputStream_ReadSegments(nsIInputStream *iface,
153 nsresult (WINAPI *aWriter)(nsIInputStream*,void*,const char*,UINT32,UINT32,UINT32*),
154 void *aClousure, UINT32 aCount, UINT32 *_retval)
156 nsProtocolStream *This = impl_from_nsIInputStream(iface);
157 UINT32 written = 0;
158 nsresult nsres;
160 TRACE("(%p)->(%p %p %d %p)\n", This, aWriter, aClousure, aCount, _retval);
162 if(!This->buf_size)
163 return S_OK;
165 if(aCount > This->buf_size)
166 aCount = This->buf_size;
168 nsres = aWriter(&This->nsIInputStream_iface, aClousure, This->buf, 0, aCount, &written);
169 if(NS_FAILED(nsres))
170 TRACE("aWriter failed: %08x\n", nsres);
171 else if(written != This->buf_size)
172 FIXME("written %d != buf_size %d\n", written, This->buf_size);
174 This->buf_size -= written;
176 *_retval = written;
177 return nsres;
180 static nsresult NSAPI nsInputStream_IsNonBlocking(nsIInputStream *iface, cpp_bool *_retval)
182 nsProtocolStream *This = impl_from_nsIInputStream(iface);
183 FIXME("(%p)->(%p)\n", This, _retval);
184 return NS_ERROR_NOT_IMPLEMENTED;
187 static const nsIInputStreamVtbl nsInputStreamVtbl = {
188 nsInputStream_QueryInterface,
189 nsInputStream_AddRef,
190 nsInputStream_Release,
191 nsInputStream_Close,
192 nsInputStream_Available,
193 nsInputStream_Read,
194 nsInputStream_ReadSegments,
195 nsInputStream_IsNonBlocking
198 static nsProtocolStream *create_nsprotocol_stream(void)
200 nsProtocolStream *ret;
202 ret = heap_alloc(sizeof(nsProtocolStream));
203 if(!ret)
204 return NULL;
206 ret->nsIInputStream_iface.lpVtbl = &nsInputStreamVtbl;
207 ret->ref = 1;
208 ret->buf_size = 0;
210 return ret;
213 static void release_request_data(request_data_t *request_data)
215 if(request_data->post_stream)
216 nsIInputStream_Release(request_data->post_stream);
217 heap_free(request_data->headers);
218 if(request_data->post_data)
219 GlobalFree(request_data->post_data);
222 static inline BSCallback *impl_from_IBindStatusCallback(IBindStatusCallback *iface)
224 return CONTAINING_RECORD(iface, BSCallback, IBindStatusCallback_iface);
227 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
228 REFIID riid, void **ppv)
230 BSCallback *This = impl_from_IBindStatusCallback(iface);
232 *ppv = NULL;
233 if(IsEqualGUID(&IID_IUnknown, riid)) {
234 TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
235 *ppv = &This->IBindStatusCallback_iface;
236 }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
237 TRACE("(%p)->(IID_IBindStatusCallback, %p)\n", This, ppv);
238 *ppv = &This->IBindStatusCallback_iface;
239 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
240 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
241 *ppv = &This->IServiceProvider_iface;
242 }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
243 TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
244 *ppv = &This->IHttpNegotiate2_iface;
245 }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
246 TRACE("(%p)->(IID_IHttpNegotiate2 %p)\n", This, ppv);
247 *ppv = &This->IHttpNegotiate2_iface;
248 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
249 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
250 *ppv = &This->IInternetBindInfo_iface;
253 if(*ppv) {
254 IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
255 return S_OK;
258 TRACE("Unsupported riid = %s\n", debugstr_guid(riid));
259 return E_NOINTERFACE;
262 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
264 BSCallback *This = impl_from_IBindStatusCallback(iface);
265 LONG ref = InterlockedIncrement(&This->ref);
267 TRACE("(%p) ref = %d\n", This, ref);
269 return ref;
272 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
274 BSCallback *This = impl_from_IBindStatusCallback(iface);
275 LONG ref = InterlockedDecrement(&This->ref);
277 TRACE("(%p) ref = %d\n", This, ref);
279 if(!ref) {
280 release_request_data(&This->request_data);
281 if(This->mon)
282 IMoniker_Release(This->mon);
283 if(This->binding)
284 IBinding_Release(This->binding);
285 list_remove(&This->entry);
286 list_init(&This->entry);
288 This->vtbl->destroy(This);
291 return ref;
294 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
295 DWORD dwReserved, IBinding *pbind)
297 BSCallback *This = impl_from_IBindStatusCallback(iface);
299 TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
301 IBinding_AddRef(pbind);
302 This->binding = pbind;
304 if(This->window)
305 list_add_head(&This->window->bindings, &This->entry);
307 return This->vtbl->start_binding(This);
310 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
312 BSCallback *This = impl_from_IBindStatusCallback(iface);
313 FIXME("(%p)->(%p)\n", This, pnPriority);
314 return E_NOTIMPL;
317 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
319 BSCallback *This = impl_from_IBindStatusCallback(iface);
320 FIXME("(%p)->(%d)\n", This, reserved);
321 return E_NOTIMPL;
324 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
325 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
327 BSCallback *This = impl_from_IBindStatusCallback(iface);
329 TRACE("%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
330 debugstr_w(szStatusText));
332 return This->vtbl->on_progress(This, ulStatusCode, szStatusText);
335 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
336 HRESULT hresult, LPCWSTR szError)
338 BSCallback *This = impl_from_IBindStatusCallback(iface);
339 HRESULT hres;
341 TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
343 /* NOTE: IE7 calls GetBindResult here */
345 hres = This->vtbl->stop_binding(This, hresult);
347 if(This->binding) {
348 IBinding_Release(This->binding);
349 This->binding = NULL;
352 if(This->mon) {
353 IMoniker_Release(This->mon);
354 This->mon = NULL;
357 list_remove(&This->entry);
358 list_init(&This->entry);
359 This->window = NULL;
361 return hres;
364 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
365 DWORD *grfBINDF, BINDINFO *pbindinfo)
367 BSCallback *This = impl_from_IBindStatusCallback(iface);
368 DWORD size;
370 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
372 if(!This->bindinfo_ready) {
373 HRESULT hres;
375 hres = This->vtbl->init_bindinfo(This);
376 if(FAILED(hres))
377 return hres;
379 This->bindinfo_ready = TRUE;
382 *grfBINDF = This->bindf;
384 size = pbindinfo->cbSize;
385 memset(pbindinfo, 0, size);
386 pbindinfo->cbSize = size;
388 pbindinfo->cbstgmedData = This->request_data.post_data_len;
389 pbindinfo->dwCodePage = CP_UTF8;
390 pbindinfo->dwOptions = 0x80000;
392 if(This->request_data.post_data_len) {
393 pbindinfo->dwBindVerb = BINDVERB_POST;
395 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
396 pbindinfo->stgmedData.u.hGlobal = This->request_data.post_data;
397 pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)&This->IBindStatusCallback_iface;
398 IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
401 return S_OK;
404 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
405 DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
407 BSCallback *This = impl_from_IBindStatusCallback(iface);
409 TRACE("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
411 return This->vtbl->read_data(This, pstgmed->u.pstm);
414 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
415 REFIID riid, IUnknown *punk)
417 BSCallback *This = impl_from_IBindStatusCallback(iface);
418 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
419 return E_NOTIMPL;
422 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
423 BindStatusCallback_QueryInterface,
424 BindStatusCallback_AddRef,
425 BindStatusCallback_Release,
426 BindStatusCallback_OnStartBinding,
427 BindStatusCallback_GetPriority,
428 BindStatusCallback_OnLowResource,
429 BindStatusCallback_OnProgress,
430 BindStatusCallback_OnStopBinding,
431 BindStatusCallback_GetBindInfo,
432 BindStatusCallback_OnDataAvailable,
433 BindStatusCallback_OnObjectAvailable
436 static inline BSCallback *impl_from_IHttpNegotiate2(IHttpNegotiate2 *iface)
438 return CONTAINING_RECORD(iface, BSCallback, IHttpNegotiate2_iface);
441 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
442 REFIID riid, void **ppv)
444 BSCallback *This = impl_from_IHttpNegotiate2(iface);
445 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
448 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
450 BSCallback *This = impl_from_IHttpNegotiate2(iface);
451 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
454 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
456 BSCallback *This = impl_from_IHttpNegotiate2(iface);
457 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
460 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
461 LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
463 BSCallback *This = impl_from_IHttpNegotiate2(iface);
464 HRESULT hres;
466 TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
467 dwReserved, pszAdditionalHeaders);
469 *pszAdditionalHeaders = NULL;
471 hres = This->vtbl->beginning_transaction(This, pszAdditionalHeaders);
472 if(hres != S_FALSE)
473 return hres;
475 if(This->request_data.headers) {
476 DWORD size;
478 size = (strlenW(This->request_data.headers)+1)*sizeof(WCHAR);
479 *pszAdditionalHeaders = CoTaskMemAlloc(size);
480 if(!*pszAdditionalHeaders)
481 return E_OUTOFMEMORY;
482 memcpy(*pszAdditionalHeaders, This->request_data.headers, size);
485 return S_OK;
488 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
489 LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
491 BSCallback *This = impl_from_IHttpNegotiate2(iface);
493 TRACE("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
494 debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
496 return This->vtbl->on_response(This, dwResponseCode, szResponseHeaders);
499 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
500 BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
502 BSCallback *This = impl_from_IHttpNegotiate2(iface);
503 FIXME("(%p)->(%p %p %ld)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
504 return E_NOTIMPL;
507 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
508 HttpNegotiate_QueryInterface,
509 HttpNegotiate_AddRef,
510 HttpNegotiate_Release,
511 HttpNegotiate_BeginningTransaction,
512 HttpNegotiate_OnResponse,
513 HttpNegotiate_GetRootSecurityId
516 static inline BSCallback *impl_from_IInternetBindInfo(IInternetBindInfo *iface)
518 return CONTAINING_RECORD(iface, BSCallback, IInternetBindInfo_iface);
521 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
522 REFIID riid, void **ppv)
524 BSCallback *This = impl_from_IInternetBindInfo(iface);
525 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
528 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
530 BSCallback *This = impl_from_IInternetBindInfo(iface);
531 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
534 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
536 BSCallback *This = impl_from_IInternetBindInfo(iface);
537 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
540 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
541 DWORD *grfBINDF, BINDINFO *pbindinfo)
543 BSCallback *This = impl_from_IInternetBindInfo(iface);
544 FIXME("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
545 return E_NOTIMPL;
548 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
549 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
551 BSCallback *This = impl_from_IInternetBindInfo(iface);
552 FIXME("(%p)->(%u %p %u %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
553 return E_NOTIMPL;
556 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
557 InternetBindInfo_QueryInterface,
558 InternetBindInfo_AddRef,
559 InternetBindInfo_Release,
560 InternetBindInfo_GetBindInfo,
561 InternetBindInfo_GetBindString
564 static inline BSCallback *impl_from_IServiceProvider(IServiceProvider *iface)
566 return CONTAINING_RECORD(iface, BSCallback, IServiceProvider_iface);
569 static HRESULT WINAPI BSCServiceProvider_QueryInterface(IServiceProvider *iface,
570 REFIID riid, void **ppv)
572 BSCallback *This = impl_from_IServiceProvider(iface);
573 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
576 static ULONG WINAPI BSCServiceProvider_AddRef(IServiceProvider *iface)
578 BSCallback *This = impl_from_IServiceProvider(iface);
579 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
582 static ULONG WINAPI BSCServiceProvider_Release(IServiceProvider *iface)
584 BSCallback *This = impl_from_IServiceProvider(iface);
585 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
588 static HRESULT WINAPI BSCServiceProvider_QueryService(IServiceProvider *iface,
589 REFGUID guidService, REFIID riid, void **ppv)
591 BSCallback *This = impl_from_IServiceProvider(iface);
593 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
595 if(This->window && IsEqualGUID(guidService, &IID_IWindowForBindingUI))
596 return IServiceProvider_QueryService(&This->window->base.IServiceProvider_iface, guidService, riid, ppv);
597 return E_NOINTERFACE;
600 static const IServiceProviderVtbl ServiceProviderVtbl = {
601 BSCServiceProvider_QueryInterface,
602 BSCServiceProvider_AddRef,
603 BSCServiceProvider_Release,
604 BSCServiceProvider_QueryService
607 void init_bscallback(BSCallback *This, const BSCallbackVtbl *vtbl, IMoniker *mon, DWORD bindf)
609 This->IBindStatusCallback_iface.lpVtbl = &BindStatusCallbackVtbl;
610 This->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
611 This->IHttpNegotiate2_iface.lpVtbl = &HttpNegotiate2Vtbl;
612 This->IInternetBindInfo_iface.lpVtbl = &InternetBindInfoVtbl;
613 This->vtbl = vtbl;
614 This->ref = 1;
615 This->bindf = bindf;
616 This->bom = BOM_NONE;
618 list_init(&This->entry);
620 if(mon)
621 IMoniker_AddRef(mon);
622 This->mon = mon;
625 HRESULT read_stream(BSCallback *This, IStream *stream, void *buf, DWORD size, DWORD *ret_size)
627 DWORD read_size = 0, skip=0;
628 BYTE *data = buf;
629 HRESULT hres;
631 hres = IStream_Read(stream, buf, size, &read_size);
633 if(!This->readed && This->bom == BOM_NONE) {
634 if(read_size >= 2 && data[0] == 0xff && data[1] == 0xfe) {
635 This->bom = BOM_UTF16;
636 skip = 2;
637 }else if(read_size >= 3 && data[0] == 0xef && data[1] == 0xbb && data[2] == 0xbf) {
638 This->bom = BOM_UTF8;
639 skip = 3;
641 if(skip) {
642 read_size -= skip;
643 if(read_size)
644 memmove(data, data+skip, read_size);
648 This->readed += read_size;
649 *ret_size = read_size;
650 return hres;
653 static void parse_content_type(nsChannelBSC *This, const WCHAR *value)
655 const WCHAR *ptr;
656 size_t len;
658 static const WCHAR charsetW[] = {'c','h','a','r','s','e','t','='};
660 ptr = strchrW(value, ';');
661 if(!ptr)
662 return;
664 ptr++;
665 while(*ptr && isspaceW(*ptr))
666 ptr++;
668 len = strlenW(value);
669 if(ptr + sizeof(charsetW)/sizeof(WCHAR) < value+len && !memicmpW(ptr, charsetW, sizeof(charsetW)/sizeof(WCHAR))) {
670 size_t charset_len, lena;
671 nsACString charset_str;
672 const WCHAR *charset;
673 char *charseta;
675 ptr += sizeof(charsetW)/sizeof(WCHAR);
677 if(*ptr == '\'') {
678 FIXME("Quoted value\n");
679 return;
680 }else {
681 charset = ptr;
682 while(*ptr && *ptr != ',')
683 ptr++;
684 charset_len = ptr-charset;
687 lena = WideCharToMultiByte(CP_ACP, 0, charset, charset_len, NULL, 0, NULL, NULL);
688 charseta = heap_alloc(lena+1);
689 if(!charseta)
690 return;
692 WideCharToMultiByte(CP_ACP, 0, charset, charset_len, charseta, lena, NULL, NULL);
693 charseta[lena] = 0;
695 nsACString_InitDepend(&charset_str, charseta);
696 nsIHttpChannel_SetContentCharset(&This->nschannel->nsIHttpChannel_iface, &charset_str);
697 nsACString_Finish(&charset_str);
698 heap_free(charseta);
699 }else {
700 FIXME("unhandled: %s\n", debugstr_wn(ptr, len - (ptr-value)));
704 static HRESULT parse_headers(const WCHAR *headers, struct list *headers_list)
706 const WCHAR *header, *header_end, *colon, *value;
707 HRESULT hres;
709 header = headers;
710 while(*header) {
711 if(header[0] == '\r' && header[1] == '\n' && !header[2])
712 break;
713 for(colon = header; *colon && *colon != ':' && *colon != '\r'; colon++);
714 if(*colon != ':')
715 return E_FAIL;
717 value = colon+1;
718 while(*value == ' ')
719 value++;
720 if(!*value)
721 return E_FAIL;
723 for(header_end = value+1; *header_end && *header_end != '\r'; header_end++);
725 hres = set_http_header(headers_list, header, colon-header, value, header_end-value);
726 if(FAILED(hres))
727 return hres;
729 header = header_end;
730 if(header[0] == '\r' && header[1] == '\n')
731 header += 2;
734 return S_OK;
737 static HRESULT process_response_headers(nsChannelBSC *This, const WCHAR *headers)
739 http_header_t *iter;
740 HRESULT hres;
742 static const WCHAR content_typeW[] = {'c','o','n','t','e','n','t','-','t','y','p','e',0};
744 hres = parse_headers(headers, &This->nschannel->response_headers);
745 if(FAILED(hres))
746 return hres;
748 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->response_headers, http_header_t, entry) {
749 if(!strcmpiW(iter->header, content_typeW))
750 parse_content_type(This, iter->data);
753 return S_OK;
756 static void query_http_info(nsChannelBSC *This, IWinInetHttpInfo *wininet_info)
758 const WCHAR *ptr;
759 DWORD len = 0;
760 WCHAR *buf;
762 IWinInetHttpInfo_QueryInfo(wininet_info, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &len, NULL, NULL);
763 if(!len)
764 return;
766 buf = heap_alloc(len);
767 if(!buf)
768 return;
770 IWinInetHttpInfo_QueryInfo(wininet_info, HTTP_QUERY_RAW_HEADERS_CRLF, buf, &len, NULL, NULL);
771 if(!len) {
772 heap_free(buf);
773 return;
776 ptr = strchrW(buf, '\r');
777 if(ptr && ptr[1] == '\n') {
778 ptr += 2;
779 process_response_headers(This, ptr);
782 heap_free(buf);
785 HRESULT start_binding(HTMLInnerWindow *inner_window, BSCallback *bscallback, IBindCtx *bctx)
787 IStream *str = NULL;
788 HRESULT hres;
790 TRACE("(%p %p %p)\n", inner_window, bscallback, bctx);
792 bscallback->window = inner_window;
794 /* NOTE: IE7 calls IsSystemMoniker here*/
796 if(bctx) {
797 hres = RegisterBindStatusCallback(bctx, &bscallback->IBindStatusCallback_iface, NULL, 0);
798 if(SUCCEEDED(hres))
799 IBindCtx_AddRef(bctx);
800 }else {
801 hres = CreateAsyncBindCtx(0, &bscallback->IBindStatusCallback_iface, NULL, &bctx);
804 if(FAILED(hres)) {
805 bscallback->window = NULL;
806 return hres;
809 hres = IMoniker_BindToStorage(bscallback->mon, bctx, NULL, &IID_IStream, (void**)&str);
810 IBindCtx_Release(bctx);
811 if(FAILED(hres)) {
812 WARN("BindToStorage failed: %08x\n", hres);
813 bscallback->window = NULL;
814 return hres;
817 if(str)
818 IStream_Release(str);
820 return S_OK;
823 static HRESULT read_post_data_stream(nsIInputStream *stream, BOOL contains_headers, struct list *headers_list,
824 request_data_t *request_data)
826 nsISeekableStream *seekable_stream;
827 UINT64 available = 0;
828 UINT32 data_len = 0;
829 char *data, *post_data;
830 nsresult nsres;
831 HRESULT hres = S_OK;
833 if(!stream)
834 return S_OK;
836 nsres = nsIInputStream_Available(stream, &available);
837 if(NS_FAILED(nsres))
838 return E_FAIL;
840 post_data = data = GlobalAlloc(0, available+1);
841 if(!data)
842 return E_OUTOFMEMORY;
844 nsres = nsIInputStream_Read(stream, data, available, &data_len);
845 if(NS_FAILED(nsres)) {
846 GlobalFree(data);
847 return E_FAIL;
850 if(contains_headers) {
851 if(data_len >= 2 && data[0] == '\r' && data[1] == '\n') {
852 post_data = data+2;
853 data_len -= 2;
854 }else {
855 WCHAR *headers;
856 DWORD size;
857 char *ptr;
859 post_data += data_len;
860 for(ptr = data; ptr+4 < data+data_len; ptr++) {
861 if(!memcmp(ptr, "\r\n\r\n", 4)) {
862 ptr += 2;
863 post_data = ptr+2;
864 break;
868 data_len -= post_data-data;
870 size = MultiByteToWideChar(CP_ACP, 0, data, ptr-data, NULL, 0);
871 headers = heap_alloc((size+1)*sizeof(WCHAR));
872 if(headers) {
873 MultiByteToWideChar(CP_ACP, 0, data, ptr-data, headers, size);
874 headers[size] = 0;
875 if(headers_list)
876 hres = parse_headers(headers, headers_list);
877 if(SUCCEEDED(hres))
878 request_data->headers = headers;
879 else
880 heap_free(headers);
881 }else {
882 hres = E_OUTOFMEMORY;
887 if(FAILED(hres)) {
888 GlobalFree(data);
889 return hres;
892 if(!data_len) {
893 GlobalFree(data);
894 post_data = NULL;
895 }else if(post_data != data) {
896 char *new_data;
898 new_data = GlobalAlloc(0, data_len+1);
899 if(new_data)
900 memcpy(new_data, post_data, data_len);
901 GlobalFree(data);
902 if(!new_data)
903 return E_OUTOFMEMORY;
904 post_data = new_data;
907 if(post_data)
908 post_data[data_len] = 0;
909 request_data->post_data = post_data;
910 request_data->post_data_len = data_len;
912 nsres = nsIInputStream_QueryInterface(stream, &IID_nsISeekableStream, (void**)&seekable_stream);
913 assert(nsres == NS_OK);
915 nsres = nsISeekableStream_Seek(seekable_stream, NS_SEEK_SET, 0);
916 assert(nsres == NS_OK);
918 nsISeekableStream_Release(seekable_stream);
920 nsIInputStream_AddRef(stream);
921 request_data->post_stream = stream;
922 TRACE("post_data = %s\n", debugstr_an(request_data->post_data, request_data->post_data_len));
923 return S_OK;
926 static HRESULT on_start_nsrequest(nsChannelBSC *This)
928 nsresult nsres;
930 /* FIXME: it's needed for http connections from BindToObject. */
931 if(!This->nschannel->response_status)
932 This->nschannel->response_status = 200;
934 nsres = nsIStreamListener_OnStartRequest(This->nslistener,
935 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext);
936 if(NS_FAILED(nsres)) {
937 FIXME("OnStartRequest failed: %08x\n", nsres);
938 return E_FAIL;
941 if(This->is_doc_channel) {
942 if(!This->bsc.window)
943 return E_ABORT; /* Binding aborted in OnStartRequest call. */
944 update_window_doc(This->bsc.window);
945 if(This->bsc.window->base.outer_window->readystate != READYSTATE_LOADING)
946 set_ready_state(This->bsc.window->base.outer_window, READYSTATE_LOADING);
949 return S_OK;
952 static void on_stop_nsrequest(nsChannelBSC *This, HRESULT result)
954 nsresult nsres, request_result;
956 switch(result) {
957 case S_OK:
958 request_result = NS_OK;
959 break;
960 case E_ABORT:
961 request_result = NS_BINDING_ABORTED;
962 break;
963 default:
964 request_result = NS_ERROR_FAILURE;
967 if(This->nslistener) {
968 nsres = nsIStreamListener_OnStopRequest(This->nslistener,
969 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext,
970 request_result);
971 if(NS_FAILED(nsres))
972 WARN("OnStopRequest failed: %08x\n", nsres);
975 if(This->nschannel && This->nschannel->load_group) {
976 nsres = nsILoadGroup_RemoveRequest(This->nschannel->load_group,
977 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, NULL, request_result);
978 if(NS_FAILED(nsres))
979 ERR("RemoveRequest failed: %08x\n", nsres);
983 static HRESULT read_stream_data(nsChannelBSC *This, IStream *stream)
985 DWORD read;
986 nsresult nsres;
987 HRESULT hres;
989 if(!This->response_processed) {
990 IWinInetHttpInfo *wininet_info;
992 This->response_processed = TRUE;
993 if(This->bsc.binding) {
994 hres = IBinding_QueryInterface(This->bsc.binding, &IID_IWinInetHttpInfo, (void**)&wininet_info);
995 if(SUCCEEDED(hres)) {
996 query_http_info(This, wininet_info);
997 IWinInetHttpInfo_Release(wininet_info);
1002 if(!This->nschannel)
1003 return S_OK;
1005 if(!This->nslistener) {
1006 BYTE buf[1024];
1008 do {
1009 hres = read_stream(&This->bsc, stream, buf, sizeof(buf), &read);
1010 }while(hres == S_OK && read);
1012 return S_OK;
1015 if(!This->nsstream) {
1016 This->nsstream = create_nsprotocol_stream();
1017 if(!This->nsstream)
1018 return E_OUTOFMEMORY;
1021 do {
1022 BOOL first_read = !This->bsc.readed;
1024 hres = read_stream(&This->bsc, stream, This->nsstream->buf+This->nsstream->buf_size,
1025 sizeof(This->nsstream->buf)-This->nsstream->buf_size, &read);
1026 if(!read)
1027 break;
1029 This->nsstream->buf_size += read;
1031 if(first_read) {
1032 switch(This->bsc.bom) {
1033 case BOM_UTF8:
1034 This->nschannel->charset = heap_strdupA(UTF8_STR);
1035 break;
1036 case BOM_UTF16:
1037 This->nschannel->charset = heap_strdupA(UTF16_STR);
1038 case BOM_NONE:
1039 /* FIXME: Get charset from HTTP headers */;
1042 if(!This->nschannel->content_type) {
1043 WCHAR *mime;
1045 hres = FindMimeFromData(NULL, NULL, This->nsstream->buf, This->nsstream->buf_size,
1046 This->is_doc_channel ? text_htmlW : NULL, 0, &mime, 0);
1047 if(FAILED(hres))
1048 return hres;
1050 TRACE("Found MIME %s\n", debugstr_w(mime));
1052 This->nschannel->content_type = heap_strdupWtoA(mime);
1053 CoTaskMemFree(mime);
1054 if(!This->nschannel->content_type)
1055 return E_OUTOFMEMORY;
1058 hres = on_start_nsrequest(This);
1059 if(FAILED(hres))
1060 return hres;
1063 nsres = nsIStreamListener_OnDataAvailable(This->nslistener,
1064 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext,
1065 &This->nsstream->nsIInputStream_iface, This->bsc.readed-This->nsstream->buf_size,
1066 This->nsstream->buf_size);
1067 if(NS_FAILED(nsres))
1068 ERR("OnDataAvailable failed: %08x\n", nsres);
1070 if(This->nsstream->buf_size == sizeof(This->nsstream->buf)) {
1071 ERR("buffer is full\n");
1072 break;
1074 }while(hres == S_OK);
1076 return S_OK;
1079 typedef struct {
1080 nsIAsyncVerifyRedirectCallback nsIAsyncVerifyRedirectCallback_iface;
1082 LONG ref;
1084 nsChannel *nschannel;
1085 nsChannelBSC *bsc;
1086 } nsRedirectCallback;
1088 static nsRedirectCallback *impl_from_nsIAsyncVerifyRedirectCallback(nsIAsyncVerifyRedirectCallback *iface)
1090 return CONTAINING_RECORD(iface, nsRedirectCallback, nsIAsyncVerifyRedirectCallback_iface);
1093 static nsresult NSAPI nsAsyncVerifyRedirectCallback_QueryInterface(nsIAsyncVerifyRedirectCallback *iface,
1094 nsIIDRef riid, void **result)
1096 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1098 if(IsEqualGUID(&IID_nsISupports, riid)) {
1099 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1100 *result = &This->nsIAsyncVerifyRedirectCallback_iface;
1101 }else if(IsEqualGUID(&IID_nsIAsyncVerifyRedirectCallback, riid)) {
1102 TRACE("(%p)->(IID_nsIAsyncVerifyRedirectCallback %p)\n", This, result);
1103 *result = &This->nsIAsyncVerifyRedirectCallback_iface;
1104 }else {
1105 *result = NULL;
1106 WARN("unimplemented iface %s\n", debugstr_guid(riid));
1107 return NS_NOINTERFACE;
1110 nsISupports_AddRef((nsISupports*)*result);
1111 return NS_OK;
1114 static nsrefcnt NSAPI nsAsyncVerifyRedirectCallback_AddRef(nsIAsyncVerifyRedirectCallback *iface)
1116 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1117 LONG ref = InterlockedIncrement(&This->ref);
1119 TRACE("(%p) ref=%d\n", This, ref);
1121 return ref;
1124 static nsrefcnt NSAPI nsAsyncVerifyRedirectCallback_Release(nsIAsyncVerifyRedirectCallback *iface)
1126 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1127 LONG ref = InterlockedDecrement(&This->ref);
1129 TRACE("(%p) ref=%d\n", This, ref);
1131 if(!ref) {
1132 IBindStatusCallback_Release(&This->bsc->bsc.IBindStatusCallback_iface);
1133 nsIHttpChannel_Release(&This->nschannel->nsIHttpChannel_iface);
1134 heap_free(This);
1137 return ref;
1140 static nsresult NSAPI nsAsyncVerifyRedirectCallback_OnRedirectVerifyCallback(nsIAsyncVerifyRedirectCallback *iface, nsresult result)
1142 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1143 nsChannel *old_nschannel;
1144 nsresult nsres;
1146 TRACE("(%p)->(%08x)\n", This, result);
1148 old_nschannel = This->bsc->nschannel;
1149 nsIHttpChannel_AddRef(&This->nschannel->nsIHttpChannel_iface);
1150 This->bsc->nschannel = This->nschannel;
1152 if(This->nschannel->load_group) {
1153 nsres = nsILoadGroup_AddRequest(This->nschannel->load_group, (nsIRequest*)&This->nschannel->nsIHttpChannel_iface,
1154 NULL);
1155 if(NS_FAILED(nsres))
1156 ERR("AddRequest failed: %08x\n", nsres);
1159 if(This->bsc->is_doc_channel) {
1160 IUri *uri = nsuri_get_uri(This->nschannel->uri);
1162 if(uri) {
1163 set_current_uri(This->bsc->bsc.window->base.outer_window, uri);
1164 IUri_Release(uri);
1165 }else {
1166 WARN("Could not get IUri from nsWineURI\n");
1170 if(old_nschannel) {
1171 if(old_nschannel->load_group) {
1172 nsres = nsILoadGroup_RemoveRequest(old_nschannel->load_group,
1173 (nsIRequest*)&old_nschannel->nsIHttpChannel_iface, NULL, NS_OK);
1174 if(NS_FAILED(nsres))
1175 ERR("RemoveRequest failed: %08x\n", nsres);
1177 nsIHttpChannel_Release(&old_nschannel->nsIHttpChannel_iface);
1180 return NS_OK;
1183 static const nsIAsyncVerifyRedirectCallbackVtbl nsAsyncVerifyRedirectCallbackVtbl = {
1184 nsAsyncVerifyRedirectCallback_QueryInterface,
1185 nsAsyncVerifyRedirectCallback_AddRef,
1186 nsAsyncVerifyRedirectCallback_Release,
1187 nsAsyncVerifyRedirectCallback_OnRedirectVerifyCallback
1190 static HRESULT create_redirect_callback(nsChannel *nschannel, nsChannelBSC *bsc, nsRedirectCallback **ret)
1192 nsRedirectCallback *callback;
1194 callback = heap_alloc(sizeof(*callback));
1195 if(!callback)
1196 return E_OUTOFMEMORY;
1198 callback->nsIAsyncVerifyRedirectCallback_iface.lpVtbl = &nsAsyncVerifyRedirectCallbackVtbl;
1199 callback->ref = 1;
1201 nsIHttpChannel_AddRef(&nschannel->nsIHttpChannel_iface);
1202 callback->nschannel = nschannel;
1204 IBindStatusCallback_AddRef(&bsc->bsc.IBindStatusCallback_iface);
1205 callback->bsc = bsc;
1207 *ret = callback;
1208 return S_OK;
1211 static inline nsChannelBSC *nsChannelBSC_from_BSCallback(BSCallback *iface)
1213 return CONTAINING_RECORD(iface, nsChannelBSC, bsc);
1216 static void nsChannelBSC_destroy(BSCallback *bsc)
1218 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1220 if(This->nschannel)
1221 nsIHttpChannel_Release(&This->nschannel->nsIHttpChannel_iface);
1222 if(This->nslistener)
1223 nsIStreamListener_Release(This->nslistener);
1224 if(This->nscontext)
1225 nsISupports_Release(This->nscontext);
1226 if(This->nsstream)
1227 nsIInputStream_Release(&This->nsstream->nsIInputStream_iface);
1228 heap_free(This);
1231 static HRESULT nsChannelBSC_start_binding(BSCallback *bsc)
1233 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1235 if(This->is_doc_channel)
1236 This->bsc.window->base.outer_window->base.inner_window->doc->skip_mutation_notif = FALSE;
1238 return S_OK;
1241 static HRESULT nsChannelBSC_init_bindinfo(BSCallback *bsc)
1243 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1244 nsChannel *nschannel = This->nschannel;
1245 HRESULT hres;
1247 if(nschannel && nschannel->post_data_stream) {
1248 hres = read_post_data_stream(nschannel->post_data_stream, nschannel->post_data_contains_headers,
1249 &nschannel->request_headers, &This->bsc.request_data);
1250 if(FAILED(hres))
1251 return hres;
1254 return S_OK;
1257 typedef struct {
1258 task_t header;
1259 nsChannelBSC *bsc;
1260 } stop_request_task_t;
1262 static void stop_request_proc(task_t *_task)
1264 stop_request_task_t *task = (stop_request_task_t*)_task;
1266 TRACE("(%p)\n", task->bsc);
1268 list_remove(&task->bsc->bsc.entry);
1269 list_init(&task->bsc->bsc.entry);
1270 on_stop_nsrequest(task->bsc, S_OK);
1273 static void stop_request_task_destr(task_t *_task)
1275 stop_request_task_t *task = (stop_request_task_t*)_task;
1277 IBindStatusCallback_Release(&task->bsc->bsc.IBindStatusCallback_iface);
1278 heap_free(task);
1281 static HRESULT async_stop_request(nsChannelBSC *This)
1283 stop_request_task_t *task;
1285 if(!This->bsc.readed) {
1286 TRACE("No data read, calling OnStartRequest\n");
1287 on_start_nsrequest(This);
1290 task = heap_alloc(sizeof(*task));
1291 if(!task)
1292 return E_OUTOFMEMORY;
1294 IBindStatusCallback_AddRef(&This->bsc.IBindStatusCallback_iface);
1295 task->bsc = This;
1297 return push_task(&task->header, stop_request_proc, stop_request_task_destr, This->bsc.window->task_magic);
1300 static void handle_navigation_error(nsChannelBSC *This, DWORD result)
1302 HTMLOuterWindow *outer_window;
1303 HTMLDocumentObj *doc;
1304 BOOL is_error_url;
1305 SAFEARRAY *sa;
1306 SAFEARRAYBOUND bound;
1307 VARIANT var, varOut;
1308 LONG ind;
1309 BSTR unk;
1310 HRESULT hres;
1312 if(!This->is_doc_channel || !This->bsc.window)
1313 return;
1315 outer_window = This->bsc.window->base.outer_window;
1317 doc = outer_window->doc_obj;
1318 if(!doc || !doc->doc_object_service || !doc->client)
1319 return;
1321 hres = IDocObjectService_IsErrorUrl(doc->doc_object_service,
1322 outer_window->url, &is_error_url);
1323 if(FAILED(hres) || is_error_url)
1324 return;
1326 if(!doc->client_cmdtrg)
1327 return;
1329 bound.lLbound = 0;
1330 bound.cElements = 8;
1331 sa = SafeArrayCreate(VT_VARIANT, 1, &bound);
1332 if(!sa)
1333 return;
1335 ind = 0;
1336 V_VT(&var) = VT_I4;
1337 V_I4(&var) = result;
1338 SafeArrayPutElement(sa, &ind, &var);
1340 ind = 1;
1341 V_VT(&var) = VT_BSTR;
1342 V_BSTR(&var) = outer_window->url;
1343 SafeArrayPutElement(sa, &ind, &var);
1345 ind = 3;
1346 V_VT(&var) = VT_UNKNOWN;
1347 V_UNKNOWN(&var) = (IUnknown*)&outer_window->base.IHTMLWindow2_iface;
1348 SafeArrayPutElement(sa, &ind, &var);
1350 /* FIXME: what are the following fields for? */
1351 ind = 2;
1352 V_VT(&var) = VT_UNKNOWN;
1353 V_UNKNOWN(&var) = NULL;
1354 SafeArrayPutElement(sa, &ind, &var);
1356 ind = 4;
1357 V_VT(&var) = VT_BOOL;
1358 V_BOOL(&var) = FALSE;
1359 SafeArrayPutElement(sa, &ind, &var);
1361 ind = 5;
1362 V_VT(&var) = VT_BOOL;
1363 V_BOOL(&var) = FALSE;
1364 SafeArrayPutElement(sa, &ind, &var);
1366 ind = 6;
1367 V_VT(&var) = VT_BSTR;
1368 unk = SysAllocString(NULL);
1369 V_BSTR(&var) = unk;
1370 SafeArrayPutElement(sa, &ind, &var);
1372 ind = 7;
1373 V_VT(&var) = VT_UNKNOWN;
1374 V_UNKNOWN(&var) = NULL;
1375 SafeArrayPutElement(sa, &ind, &var);
1377 V_VT(&var) = VT_ARRAY;
1378 V_ARRAY(&var) = sa;
1379 V_VT(&varOut) = VT_BOOL;
1380 V_BOOL(&varOut) = VARIANT_TRUE;
1381 IOleCommandTarget_Exec(doc->client_cmdtrg, &CGID_DocHostCmdPriv, 1, 0, &var, FAILED(hres)?NULL:&varOut);
1383 SysFreeString(unk);
1384 SafeArrayDestroy(sa);
1387 static HRESULT nsChannelBSC_stop_binding(BSCallback *bsc, HRESULT result)
1389 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1391 if(result != E_ABORT) {
1392 if(FAILED(result))
1393 handle_navigation_error(This, result);
1394 else if(This->is_doc_channel && This->nschannel) {
1395 result = async_stop_request(This);
1396 if(SUCCEEDED(result))
1397 return S_OK;
1401 on_stop_nsrequest(This, result);
1402 return S_OK;
1405 static HRESULT nsChannelBSC_read_data(BSCallback *bsc, IStream *stream)
1407 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1409 return read_stream_data(This, stream);
1412 static HRESULT handle_redirect(nsChannelBSC *This, const WCHAR *new_url)
1414 nsRedirectCallback *callback;
1415 nsIChannelEventSink *sink;
1416 nsChannel *new_channel;
1417 nsresult nsres;
1418 HRESULT hres;
1420 TRACE("(%p)->(%s)\n", This, debugstr_w(new_url));
1422 if(!This->nschannel || !This->nschannel->notif_callback)
1423 return S_OK;
1425 nsres = nsIInterfaceRequestor_GetInterface(This->nschannel->notif_callback, &IID_nsIChannelEventSink, (void**)&sink);
1426 if(NS_FAILED(nsres))
1427 return S_OK;
1429 hres = create_redirect_nschannel(new_url, This->nschannel, &new_channel);
1430 if(SUCCEEDED(hres)) {
1431 TRACE("%p %p->%p\n", This, This->nschannel, new_channel);
1433 hres = create_redirect_callback(new_channel, This, &callback);
1434 nsIHttpChannel_Release(&new_channel->nsIHttpChannel_iface);
1437 if(SUCCEEDED(hres)) {
1438 nsres = nsIChannelEventSink_AsyncOnChannelRedirect(sink, (nsIChannel*)&This->nschannel->nsIHttpChannel_iface,
1439 (nsIChannel*)&callback->nschannel->nsIHttpChannel_iface, REDIRECT_TEMPORARY, /* FIXME */
1440 &callback->nsIAsyncVerifyRedirectCallback_iface);
1442 if(NS_FAILED(nsres))
1443 FIXME("AsyncOnChannelRedirect failed: %08x\n", hres);
1444 else if(This->nschannel != callback->nschannel)
1445 FIXME("nschannel not updated\n");
1447 nsIAsyncVerifyRedirectCallback_Release(&callback->nsIAsyncVerifyRedirectCallback_iface);
1450 nsIChannelEventSink_Release(sink);
1451 return hres;
1454 static BOOL is_supported_doc_mime(const WCHAR *mime)
1456 char *nscat, *mimea;
1457 BOOL ret;
1459 mimea = heap_strdupWtoA(mime);
1460 if(!mimea)
1461 return FALSE;
1463 nscat = get_nscategory_entry("Gecko-Content-Viewers", mimea);
1465 ret = nscat != NULL && !strcmp(nscat, "@mozilla.org/content/document-loader-factory;1");
1467 heap_free(mimea);
1468 nsfree(nscat);
1469 return ret;
1472 static IUri *get_moniker_uri(IMoniker *mon)
1474 IUriContainer *uri_container;
1475 IUri *ret = NULL;
1476 HRESULT hres;
1478 hres = IMoniker_QueryInterface(mon, &IID_IUriContainer, (void**)&uri_container);
1479 if(SUCCEEDED(hres)) {
1480 hres = IUriContainer_GetIUri(uri_container, &ret);
1481 IUriContainer_Release(uri_container);
1482 if(FAILED(hres))
1483 return NULL;
1484 }else {
1485 FIXME("No IUriContainer\n");
1488 return ret;
1491 static void handle_extern_mime_navigation(nsChannelBSC *This)
1493 IWebBrowserPriv2IE9 *webbrowser_priv;
1494 IOleCommandTarget *cmdtrg;
1495 HTMLDocumentObj *doc_obj;
1496 IBindCtx *bind_ctx;
1497 IUri *uri;
1498 VARIANT flags;
1499 HRESULT hres;
1501 if(!This->bsc.window || !This->bsc.window->base.outer_window || !This->bsc.window->base.outer_window->doc_obj)
1502 return;
1504 doc_obj = This->bsc.window->base.outer_window->doc_obj;
1506 hres = IOleClientSite_QueryInterface(doc_obj->client, &IID_IOleCommandTarget, (void**)&cmdtrg);
1507 if(SUCCEEDED(hres)) {
1508 IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 62, 0, NULL, NULL);
1509 IOleCommandTarget_Release(cmdtrg);
1512 set_document_navigation(doc_obj, FALSE);
1514 if(!doc_obj->webbrowser) {
1515 FIXME("unimplemented in non-webbrowser mode\n");
1516 return;
1519 uri = get_moniker_uri(This->bsc.mon);
1520 if(!uri)
1521 return;
1523 hres = CreateBindCtx(0, &bind_ctx);
1524 if(FAILED(hres)) {
1525 IUri_Release(uri);
1526 return;
1529 V_VT(&flags) = VT_I4;
1530 V_I4(&flags) = navHyperlink;
1532 hres = IUnknown_QueryInterface(doc_obj->webbrowser, &IID_IWebBrowserPriv2IE8, (void**)&webbrowser_priv);
1533 if(SUCCEEDED(hres)) {
1534 hres = IWebBrowserPriv2IE9_NavigateWithBindCtx2(webbrowser_priv, uri, &flags, NULL, NULL, NULL, bind_ctx, NULL, 0);
1535 IWebBrowserPriv2IE9_Release(webbrowser_priv);
1536 }else {
1537 IWebBrowserPriv *webbrowser_priv_old;
1538 VARIANT uriv;
1540 hres = IUnknown_QueryInterface(doc_obj->webbrowser, &IID_IWebBrowserPriv, (void**)&webbrowser_priv_old);
1541 if(SUCCEEDED(hres)) {
1542 V_VT(&uriv) = VT_BSTR;
1543 IUri_GetDisplayUri(uri, &V_BSTR(&uriv));
1545 hres = IWebBrowserPriv_NavigateWithBindCtx(webbrowser_priv_old, &uriv, &flags, NULL, NULL, NULL, bind_ctx, NULL);
1547 SysFreeString(V_BSTR(&uriv));
1548 IWebBrowserPriv_Release(webbrowser_priv_old);
1552 IUri_Release(uri);
1555 static HRESULT nsChannelBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
1557 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1559 switch(status_code) {
1560 case BINDSTATUS_MIMETYPEAVAILABLE:
1561 if(This->is_doc_channel && !is_supported_doc_mime(status_text)) {
1562 FIXME("External MIME: %s\n", debugstr_w(status_text));
1564 handle_extern_mime_navigation(This);
1566 This->nschannel = NULL;
1569 if(!This->nschannel)
1570 return S_OK;
1572 heap_free(This->nschannel->content_type);
1573 This->nschannel->content_type = heap_strdupWtoA(status_text);
1574 break;
1575 case BINDSTATUS_REDIRECTING:
1576 return handle_redirect(This, status_text);
1577 case BINDSTATUS_BEGINDOWNLOADDATA: {
1578 IWinInetHttpInfo *http_info;
1579 DWORD status, size = sizeof(DWORD);
1580 HRESULT hres;
1582 if(!This->bsc.binding)
1583 break;
1585 hres = IBinding_QueryInterface(This->bsc.binding, &IID_IWinInetHttpInfo, (void**)&http_info);
1586 if(FAILED(hres))
1587 break;
1589 hres = IWinInetHttpInfo_QueryInfo(http_info,
1590 HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL, NULL);
1591 IWinInetHttpInfo_Release(http_info);
1592 if(FAILED(hres) || status == HTTP_STATUS_OK)
1593 break;
1595 handle_navigation_error(This, status);
1599 return S_OK;
1602 static HRESULT process_response_status_text(const WCHAR *header, const WCHAR *header_end, char **status_text)
1604 header = strchrW(header + 1, ' ');
1605 if(!header || header >= header_end)
1606 return E_FAIL;
1607 header = strchrW(header + 1, ' ');
1608 if(!header || header >= header_end)
1609 return E_FAIL;
1610 ++header;
1612 *status_text = heap_strndupWtoU(header, header_end - header);
1614 if(!*status_text)
1615 return E_OUTOFMEMORY;
1617 return S_OK;
1620 static HRESULT nsChannelBSC_on_response(BSCallback *bsc, DWORD response_code,
1621 LPCWSTR response_headers)
1623 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1624 char *str;
1625 HRESULT hres;
1627 This->response_processed = TRUE;
1628 This->nschannel->response_status = response_code;
1630 if(response_headers) {
1631 const WCHAR *headers;
1633 headers = strchrW(response_headers, '\r');
1634 hres = process_response_status_text(response_headers, headers, &str);
1635 if(FAILED(hres)) {
1636 WARN("parsing headers failed: %08x\n", hres);
1637 return hres;
1640 heap_free(This->nschannel->response_status_text);
1641 This->nschannel->response_status_text = str;
1643 if(headers && headers[1] == '\n') {
1644 headers += 2;
1645 hres = process_response_headers(This, headers);
1646 if(FAILED(hres)) {
1647 WARN("parsing headers failed: %08x\n", hres);
1648 return hres;
1653 return S_OK;
1656 static HRESULT nsChannelBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
1658 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1659 http_header_t *iter;
1660 DWORD len = 0;
1661 WCHAR *ptr;
1663 static const WCHAR content_lengthW[] =
1664 {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0};
1666 if(!This->nschannel)
1667 return S_FALSE;
1669 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->request_headers, http_header_t, entry) {
1670 if(strcmpW(iter->header, content_lengthW))
1671 len += strlenW(iter->header) + 2 /* ": " */ + strlenW(iter->data) + 2 /* "\r\n" */;
1674 if(!len)
1675 return S_OK;
1677 *additional_headers = ptr = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
1678 if(!ptr)
1679 return E_OUTOFMEMORY;
1681 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->request_headers, http_header_t, entry) {
1682 if(!strcmpW(iter->header, content_lengthW))
1683 continue;
1685 len = strlenW(iter->header);
1686 memcpy(ptr, iter->header, len*sizeof(WCHAR));
1687 ptr += len;
1689 *ptr++ = ':';
1690 *ptr++ = ' ';
1692 len = strlenW(iter->data);
1693 memcpy(ptr, iter->data, len*sizeof(WCHAR));
1694 ptr += len;
1696 *ptr++ = '\r';
1697 *ptr++ = '\n';
1700 *ptr = 0;
1702 return S_OK;
1705 static const BSCallbackVtbl nsChannelBSCVtbl = {
1706 nsChannelBSC_destroy,
1707 nsChannelBSC_init_bindinfo,
1708 nsChannelBSC_start_binding,
1709 nsChannelBSC_stop_binding,
1710 nsChannelBSC_read_data,
1711 nsChannelBSC_on_progress,
1712 nsChannelBSC_on_response,
1713 nsChannelBSC_beginning_transaction
1716 HRESULT create_channelbsc(IMoniker *mon, const WCHAR *headers, BYTE *post_data, DWORD post_data_size,
1717 BOOL is_doc_binding, nsChannelBSC **retval)
1719 nsChannelBSC *ret;
1720 DWORD bindf;
1722 ret = heap_alloc_zero(sizeof(*ret));
1723 if(!ret)
1724 return E_OUTOFMEMORY;
1726 bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA;
1727 if(post_data_size)
1728 bindf |= BINDF_FORMS_SUBMIT | BINDF_PRAGMA_NO_CACHE | BINDF_HYPERLINK | BINDF_GETNEWESTVERSION;
1730 init_bscallback(&ret->bsc, &nsChannelBSCVtbl, mon, bindf);
1731 ret->is_doc_channel = is_doc_binding;
1733 if(headers) {
1734 ret->bsc.request_data.headers = heap_strdupW(headers);
1735 if(!ret->bsc.request_data.headers) {
1736 IBindStatusCallback_Release(&ret->bsc.IBindStatusCallback_iface);
1737 return E_OUTOFMEMORY;
1741 if(post_data) {
1742 ret->bsc.request_data.post_data = GlobalAlloc(0, post_data_size+1);
1743 if(!ret->bsc.request_data.post_data) {
1744 release_request_data(&ret->bsc.request_data);
1745 IBindStatusCallback_Release(&ret->bsc.IBindStatusCallback_iface);
1746 return E_OUTOFMEMORY;
1749 memcpy(ret->bsc.request_data.post_data, post_data, post_data_size);
1750 ((BYTE*)ret->bsc.request_data.post_data)[post_data_size] = 0;
1751 ret->bsc.request_data.post_data_len = post_data_size;
1754 TRACE("created %p\n", ret);
1755 *retval = ret;
1756 return S_OK;
1759 typedef struct {
1760 task_t header;
1761 HTMLOuterWindow *window;
1762 HTMLInnerWindow *pending_window;
1763 } start_doc_binding_task_t;
1765 static void start_doc_binding_proc(task_t *_task)
1767 start_doc_binding_task_t *task = (start_doc_binding_task_t*)_task;
1769 set_current_mon(task->window, task->pending_window->bscallback->bsc.mon, BINDING_NAVIGATED);
1770 start_binding(task->pending_window, &task->pending_window->bscallback->bsc, NULL);
1773 static void start_doc_binding_task_destr(task_t *_task)
1775 start_doc_binding_task_t *task = (start_doc_binding_task_t*)_task;
1777 IHTMLWindow2_Release(&task->pending_window->base.IHTMLWindow2_iface);
1778 heap_free(task);
1781 HRESULT async_start_doc_binding(HTMLOuterWindow *window, HTMLInnerWindow *pending_window)
1783 start_doc_binding_task_t *task;
1785 TRACE("%p\n", pending_window);
1787 task = heap_alloc(sizeof(start_doc_binding_task_t));
1788 if(!task)
1789 return E_OUTOFMEMORY;
1791 task->window = window;
1792 task->pending_window = pending_window;
1793 IHTMLWindow2_AddRef(&pending_window->base.IHTMLWindow2_iface);
1795 return push_task(&task->header, start_doc_binding_proc, start_doc_binding_task_destr, pending_window->task_magic);
1798 void abort_window_bindings(HTMLInnerWindow *window)
1800 BSCallback *iter;
1802 remove_target_tasks(window->task_magic);
1804 while(!list_empty(&window->bindings)) {
1805 iter = LIST_ENTRY(window->bindings.next, BSCallback, entry);
1807 TRACE("Aborting %p\n", iter);
1809 IBindStatusCallback_AddRef(&iter->IBindStatusCallback_iface);
1811 if(iter->binding)
1812 IBinding_Abort(iter->binding);
1813 else
1814 iter->vtbl->stop_binding(iter, E_ABORT);
1816 iter->window = NULL;
1817 list_remove(&iter->entry);
1818 list_init(&iter->entry);
1820 IBindStatusCallback_Release(&iter->IBindStatusCallback_iface);
1823 if(window->bscallback) {
1824 IBindStatusCallback_Release(&window->bscallback->bsc.IBindStatusCallback_iface);
1825 window->bscallback = NULL;
1828 if(window->mon) {
1829 IMoniker_Release(window->mon);
1830 window->mon = NULL;
1834 HRESULT channelbsc_load_stream(HTMLInnerWindow *pending_window, IMoniker *mon, IStream *stream)
1836 nsChannelBSC *bscallback = pending_window->bscallback;
1837 HRESULT hres = S_OK;
1839 if(!bscallback->nschannel) {
1840 ERR("NULL nschannel\n");
1841 return E_FAIL;
1844 bscallback->nschannel->content_type = heap_strdupA("text/html");
1845 if(!bscallback->nschannel->content_type)
1846 return E_OUTOFMEMORY;
1848 set_current_mon(pending_window->base.outer_window, mon, 0);
1850 bscallback->bsc.window = pending_window;
1851 if(stream)
1852 hres = read_stream_data(bscallback, stream);
1853 if(SUCCEEDED(hres))
1854 hres = async_stop_request(bscallback);
1855 if(FAILED(hres))
1856 IBindStatusCallback_OnStopBinding(&bscallback->bsc.IBindStatusCallback_iface, hres,
1857 ERROR_SUCCESS);
1859 return hres;
1862 void channelbsc_set_channel(nsChannelBSC *This, nsChannel *channel, nsIStreamListener *listener, nsISupports *context)
1864 nsIHttpChannel_AddRef(&channel->nsIHttpChannel_iface);
1865 This->nschannel = channel;
1867 nsIStreamListener_AddRef(listener);
1868 This->nslistener = listener;
1870 if(context) {
1871 nsISupports_AddRef(context);
1872 This->nscontext = context;
1875 if(This->bsc.request_data.headers) {
1876 HRESULT hres;
1878 hres = parse_headers(This->bsc.request_data.headers, &channel->request_headers);
1879 heap_free(This->bsc.request_data.headers);
1880 This->bsc.request_data.headers = NULL;
1881 if(FAILED(hres))
1882 WARN("parse_headers failed: %08x\n", hres);
1886 typedef struct {
1887 task_t header;
1888 HTMLOuterWindow *window;
1889 IUri *uri;
1890 } navigate_javascript_task_t;
1892 static void navigate_javascript_proc(task_t *_task)
1894 navigate_javascript_task_t *task = (navigate_javascript_task_t*)_task;
1895 HTMLOuterWindow *window = task->window;
1896 VARIANT v;
1897 BSTR code;
1898 HRESULT hres;
1900 static const WCHAR jscriptW[] = {'j','s','c','r','i','p','t',0};
1902 task->window->readystate = READYSTATE_COMPLETE;
1904 hres = IUri_GetPath(task->uri, &code);
1905 if(FAILED(hres))
1906 return;
1908 hres = UrlUnescapeW(code, NULL, NULL, URL_UNESCAPE_INPLACE);
1909 if(FAILED(hres)) {
1910 SysFreeString(code);
1911 return;
1914 set_download_state(window->doc_obj, 1);
1916 V_VT(&v) = VT_EMPTY;
1917 hres = exec_script(window->base.inner_window, code, jscriptW, &v);
1918 SysFreeString(code);
1919 if(SUCCEEDED(hres) && V_VT(&v) != VT_EMPTY) {
1920 FIXME("javascirpt URL returned %s\n", debugstr_variant(&v));
1921 VariantClear(&v);
1924 if(window->doc_obj->view_sink)
1925 IAdviseSink_OnViewChange(window->doc_obj->view_sink, DVASPECT_CONTENT, -1);
1927 set_download_state(window->doc_obj, 0);
1930 static void navigate_javascript_task_destr(task_t *_task)
1932 navigate_javascript_task_t *task = (navigate_javascript_task_t*)_task;
1934 IUri_Release(task->uri);
1935 heap_free(task);
1938 typedef struct {
1939 task_t header;
1940 HTMLOuterWindow *window;
1941 nsChannelBSC *bscallback;
1942 DWORD flags;
1943 IMoniker *mon;
1944 IUri *uri;
1945 } navigate_task_t;
1947 static void navigate_proc(task_t *_task)
1949 navigate_task_t *task = (navigate_task_t*)_task;
1950 HRESULT hres;
1952 hres = set_moniker(task->window, task->mon, task->uri, NULL, task->bscallback, TRUE);
1953 if(SUCCEEDED(hres)) {
1954 set_current_mon(task->window, task->bscallback->bsc.mon, task->flags);
1955 set_current_uri(task->window, task->uri);
1956 start_binding(task->window->pending_window, &task->bscallback->bsc, NULL);
1960 static void navigate_task_destr(task_t *_task)
1962 navigate_task_t *task = (navigate_task_t*)_task;
1964 IBindStatusCallback_Release(&task->bscallback->bsc.IBindStatusCallback_iface);
1965 IMoniker_Release(task->mon);
1966 IUri_Release(task->uri);
1967 heap_free(task);
1970 static HRESULT navigate_fragment(HTMLOuterWindow *window, IUri *uri)
1972 nsIDOMLocation *nslocation;
1973 nsAString nsfrag_str;
1974 WCHAR *selector;
1975 BSTR frag;
1976 nsresult nsres;
1977 HRESULT hres;
1979 const WCHAR selector_formatW[] = {'a','[','i','d','=','"','%','s','"',']',0};
1981 set_current_uri(window, uri);
1983 nsres = nsIDOMWindow_GetLocation(window->nswindow, &nslocation);
1984 if(FAILED(nsres) || !nslocation)
1985 return E_FAIL;
1987 hres = IUri_GetFragment(uri, &frag);
1988 if(FAILED(hres)) {
1989 nsIDOMLocation_Release(nslocation);
1990 return hres;
1993 nsAString_InitDepend(&nsfrag_str, frag);
1994 nsres = nsIDOMLocation_SetHash(nslocation, &nsfrag_str);
1995 nsAString_Finish(&nsfrag_str);
1996 nsIDOMLocation_Release(nslocation);
1997 if(NS_FAILED(nsres))
1998 ERR("SetHash failed: %08x\n", nsres);
2001 * IE supports scrolling to anchor elements with "#hash" ids (note that '#' is part of the id),
2002 * while Gecko scrolls only to elements with "hash" ids. We scroll the page ourselves if
2003 * a[id="#hash"] element can be found.
2005 selector = heap_alloc(sizeof(selector_formatW)+SysStringLen(frag)*sizeof(WCHAR));
2006 if(selector) {
2007 nsIDOMElement *nselem = NULL;
2008 nsAString selector_str;
2010 sprintfW(selector, selector_formatW, frag);
2011 nsAString_InitDepend(&selector_str, selector);
2012 /* NOTE: Gecko doesn't set result to NULL if there is no match, so nselem must be initialized */
2013 nsres = nsIDOMHTMLDocument_QuerySelector(window->base.inner_window->doc->nsdoc, &selector_str, &nselem);
2014 nsAString_Finish(&selector_str);
2015 heap_free(selector);
2016 if(NS_SUCCEEDED(nsres) && nselem) {
2017 nsIDOMHTMLElement *html_elem;
2019 nsres = nsIDOMElement_QueryInterface(nselem, &IID_nsIDOMHTMLElement, (void**)&html_elem);
2020 nsIDOMElement_Release(nselem);
2021 if(NS_SUCCEEDED(nsres)) {
2022 nsIDOMHTMLElement_ScrollIntoView(html_elem, TRUE, 1);
2023 nsIDOMHTMLElement_Release(html_elem);
2028 SysFreeString(frag);
2030 if(window->doc_obj->doc_object_service) {
2031 IDocObjectService_FireNavigateComplete2(window->doc_obj->doc_object_service, &window->base.IHTMLWindow2_iface, 0x10);
2032 IDocObjectService_FireDocumentComplete(window->doc_obj->doc_object_service, &window->base.IHTMLWindow2_iface, 0);
2036 return S_OK;
2039 HRESULT super_navigate(HTMLOuterWindow *window, IUri *uri, DWORD flags, const WCHAR *headers, BYTE *post_data, DWORD post_data_size)
2041 nsChannelBSC *bsc;
2042 IUri *uri_nofrag;
2043 IMoniker *mon;
2044 DWORD scheme;
2045 HRESULT hres;
2047 uri_nofrag = get_uri_nofrag(uri);
2048 if(!uri_nofrag)
2049 return E_FAIL;
2051 if(window->doc_obj->client && !(flags & BINDING_REFRESH)) {
2052 IOleCommandTarget *cmdtrg;
2054 hres = IOleClientSite_QueryInterface(window->doc_obj->client, &IID_IOleCommandTarget, (void**)&cmdtrg);
2055 if(SUCCEEDED(hres)) {
2056 VARIANT in, out;
2057 BSTR url_str;
2059 hres = IUri_GetDisplayUri(uri_nofrag, &url_str);
2060 if(SUCCEEDED(hres)) {
2061 V_VT(&in) = VT_BSTR;
2062 V_BSTR(&in) = url_str;
2063 V_VT(&out) = VT_BOOL;
2064 V_BOOL(&out) = VARIANT_TRUE;
2065 hres = IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 67, 0, &in, &out);
2066 IOleCommandTarget_Release(cmdtrg);
2067 if(SUCCEEDED(hres))
2068 VariantClear(&out);
2069 SysFreeString(url_str);
2074 if(!(flags & BINDING_NOFRAG) && window->uri_nofrag && !post_data_size) {
2075 BOOL eq;
2077 hres = IUri_IsEqual(uri_nofrag, window->uri_nofrag, &eq);
2078 if(SUCCEEDED(hres) && eq) {
2079 IUri_Release(uri_nofrag);
2080 TRACE("fragment navigate\n");
2081 return navigate_fragment(window, uri);
2085 hres = CreateURLMonikerEx2(NULL, uri_nofrag, &mon, URL_MK_UNIFORM);
2086 IUri_Release(uri_nofrag);
2087 if(FAILED(hres))
2088 return hres;
2090 /* FIXME: Why not set_ready_state? */
2091 window->readystate = READYSTATE_UNINITIALIZED;
2093 hres = create_channelbsc(mon, headers, post_data, post_data_size, TRUE, &bsc);
2094 if(FAILED(hres)) {
2095 IMoniker_Release(mon);
2096 return hres;
2099 prepare_for_binding(&window->doc_obj->basedoc, mon, flags);
2101 hres = IUri_GetScheme(uri, &scheme);
2102 if(SUCCEEDED(hres) && scheme == URL_SCHEME_JAVASCRIPT) {
2103 navigate_javascript_task_t *task;
2105 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
2106 IMoniker_Release(mon);
2108 task = heap_alloc(sizeof(*task));
2109 if(!task)
2110 return E_OUTOFMEMORY;
2112 /* Why silently? */
2113 window->readystate = READYSTATE_COMPLETE;
2114 if(!(flags & BINDING_FROMHIST))
2115 call_docview_84(window->doc_obj);
2117 IUri_AddRef(uri);
2118 task->window = window;
2119 task->uri = uri;
2120 hres = push_task(&task->header, navigate_javascript_proc, navigate_javascript_task_destr, window->task_magic);
2121 }else if(flags & BINDING_SUBMIT) {
2122 hres = set_moniker(window, mon, uri, NULL, bsc, TRUE);
2123 if(SUCCEEDED(hres))
2124 hres = start_binding(window->pending_window, &bsc->bsc, NULL);
2125 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
2126 IMoniker_Release(mon);
2127 }else {
2128 navigate_task_t *task;
2130 task = heap_alloc(sizeof(*task));
2131 if(!task) {
2132 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
2133 IMoniker_Release(mon);
2134 return E_OUTOFMEMORY;
2137 /* Silently and repeated when real loading starts? */
2138 window->readystate = READYSTATE_LOADING;
2139 if(!(flags & (BINDING_FROMHIST|BINDING_REFRESH)))
2140 call_docview_84(window->doc_obj);
2142 task->window = window;
2143 task->bscallback = bsc;
2144 task->flags = flags;
2145 task->mon = mon;
2147 IUri_AddRef(uri);
2148 task->uri = uri;
2149 hres = push_task(&task->header, navigate_proc, navigate_task_destr, window->task_magic);
2152 return hres;
2155 HRESULT navigate_new_window(HTMLOuterWindow *window, IUri *uri, const WCHAR *name, request_data_t *request_data, IHTMLWindow2 **ret)
2157 IWebBrowser2 *web_browser;
2158 IHTMLWindow2 *new_window;
2159 IBindCtx *bind_ctx;
2160 nsChannelBSC *bsc;
2161 HRESULT hres;
2163 if(request_data)
2164 hres = create_channelbsc(NULL, request_data->headers,
2165 request_data->post_data, request_data->post_data_len, FALSE,
2166 &bsc);
2167 else
2168 hres = create_channelbsc(NULL, NULL, NULL, 0, FALSE, &bsc);
2169 if(FAILED(hres))
2170 return hres;
2172 hres = CreateAsyncBindCtx(0, &bsc->bsc.IBindStatusCallback_iface, NULL, &bind_ctx);
2173 if(FAILED(hres)) {
2174 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
2175 return hres;
2178 hres = CoCreateInstance(&CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER,
2179 &IID_IWebBrowser2, (void**)&web_browser);
2180 if(SUCCEEDED(hres)) {
2181 ITargetFramePriv2 *target_frame_priv;
2183 hres = IWebBrowser2_QueryInterface(web_browser, &IID_ITargetFramePriv2, (void**)&target_frame_priv);
2184 if(SUCCEEDED(hres)) {
2185 hres = ITargetFramePriv2_AggregatedNavigation2(target_frame_priv,
2186 HLNF_DISABLEWINDOWRESTRICTIONS|HLNF_OPENINNEWWINDOW, bind_ctx, &bsc->bsc.IBindStatusCallback_iface,
2187 name, uri, emptyW);
2188 ITargetFramePriv2_Release(target_frame_priv);
2190 if(SUCCEEDED(hres))
2191 hres = do_query_service((IUnknown*)web_browser, &SID_SHTMLWindow, &IID_IHTMLWindow2, (void**)&new_window);
2193 if(FAILED(hres)) {
2194 IWebBrowser2_Quit(web_browser);
2195 IWebBrowser2_Release(web_browser);
2197 }else {
2198 WARN("Could not create InternetExplorer instance: %08x\n", hres);
2201 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
2202 IBindCtx_Release(bind_ctx);
2203 if(FAILED(hres))
2204 return hres;
2206 IWebBrowser2_put_Visible(web_browser, VARIANT_TRUE);
2207 IWebBrowser2_Release(web_browser);
2209 if(ret)
2210 *ret = new_window;
2211 else
2212 IHTMLWindow2_Release(new_window);
2213 return S_OK;
2216 HRESULT hlink_frame_navigate(HTMLDocument *doc, LPCWSTR url, nsChannel *nschannel, DWORD hlnf, BOOL *cancel)
2218 IHlinkFrame *hlink_frame;
2219 nsChannelBSC *callback;
2220 IBindCtx *bindctx;
2221 IMoniker *mon;
2222 IHlink *hlink;
2223 HRESULT hres;
2225 *cancel = FALSE;
2227 hres = do_query_service((IUnknown*)doc->doc_obj->client, &IID_IHlinkFrame, &IID_IHlinkFrame,
2228 (void**)&hlink_frame);
2229 if(FAILED(hres))
2230 return S_OK;
2232 hres = create_channelbsc(NULL, NULL, NULL, 0, FALSE, &callback);
2233 if(FAILED(hres)) {
2234 IHlinkFrame_Release(hlink_frame);
2235 return hres;
2238 if(nschannel)
2239 read_post_data_stream(nschannel->post_data_stream, nschannel->post_data_contains_headers,
2240 &nschannel->request_headers, &callback->bsc.request_data);
2242 hres = CreateAsyncBindCtx(0, &callback->bsc.IBindStatusCallback_iface, NULL, &bindctx);
2243 if(SUCCEEDED(hres))
2244 hres = CoCreateInstance(&CLSID_StdHlink, NULL, CLSCTX_INPROC_SERVER,
2245 &IID_IHlink, (LPVOID*)&hlink);
2247 if(SUCCEEDED(hres))
2248 hres = CreateURLMoniker(NULL, url, &mon);
2250 if(SUCCEEDED(hres)) {
2251 IHlink_SetMonikerReference(hlink, HLINKSETF_TARGET, mon, NULL);
2253 if(hlnf & HLNF_OPENINNEWWINDOW) {
2254 static const WCHAR wszBlank[] = {'_','b','l','a','n','k',0};
2255 IHlink_SetTargetFrameName(hlink, wszBlank); /* FIXME */
2258 hres = IHlinkFrame_Navigate(hlink_frame, hlnf, bindctx,
2259 &callback->bsc.IBindStatusCallback_iface, hlink);
2260 IMoniker_Release(mon);
2261 *cancel = hres == S_OK;
2262 hres = S_OK;
2265 IHlinkFrame_Release(hlink_frame);
2266 IBindCtx_Release(bindctx);
2267 IBindStatusCallback_Release(&callback->bsc.IBindStatusCallback_iface);
2268 return hres;
2271 static HRESULT navigate_uri(HTMLOuterWindow *window, IUri *uri, const WCHAR *display_uri, const request_data_t *request_data,
2272 DWORD flags)
2274 nsWineURI *nsuri;
2275 HRESULT hres;
2277 TRACE("%s\n", debugstr_w(display_uri));
2279 if(window->doc_obj && window->doc_obj->webbrowser) {
2280 DWORD post_data_len = request_data ? request_data->post_data_len : 0;
2281 void *post_data = post_data_len ? request_data->post_data : NULL;
2282 const WCHAR *headers = request_data ? request_data->headers : NULL;
2284 if(!(flags & BINDING_REFRESH)) {
2285 BSTR frame_name = NULL;
2286 BOOL cancel = FALSE;
2288 if(window != window->doc_obj->basedoc.window) {
2289 hres = IHTMLWindow2_get_name(&window->base.IHTMLWindow2_iface, &frame_name);
2290 if(FAILED(hres))
2291 return hres;
2294 hres = IDocObjectService_FireBeforeNavigate2(window->doc_obj->doc_object_service, NULL, display_uri, 0x40,
2295 frame_name, post_data, post_data_len ? post_data_len+1 : 0, headers, TRUE, &cancel);
2296 SysFreeString(frame_name);
2297 if(SUCCEEDED(hres) && cancel) {
2298 TRACE("Navigation canceled\n");
2299 return S_OK;
2303 if(window == window->doc_obj->basedoc.window)
2304 return super_navigate(window, uri, flags, headers, post_data, post_data_len);
2307 if(window->doc_obj && window == window->doc_obj->basedoc.window) {
2308 BOOL cancel;
2310 hres = hlink_frame_navigate(&window->base.inner_window->doc->basedoc, display_uri, NULL, 0, &cancel);
2311 if(FAILED(hres))
2312 return hres;
2314 if(cancel) {
2315 TRACE("Navigation handled by hlink frame\n");
2316 return S_OK;
2320 hres = create_doc_uri(window, uri, &nsuri);
2321 if(FAILED(hres))
2322 return hres;
2324 hres = load_nsuri(window, nsuri, request_data ? request_data->post_stream : NULL, NULL, LOAD_FLAGS_NONE);
2325 nsISupports_Release((nsISupports*)nsuri);
2326 return hres;
2329 HRESULT load_uri(HTMLOuterWindow *window, IUri *uri, DWORD flags)
2331 BSTR display_uri;
2332 HRESULT hres;
2334 hres = IUri_GetDisplayUri(uri, &display_uri);
2335 if(FAILED(hres))
2336 return hres;
2338 hres = navigate_uri(window, uri, display_uri, NULL, flags);
2339 SysFreeString(display_uri);
2340 return hres;
2343 static HRESULT translate_uri(HTMLOuterWindow *window, IUri *orig_uri, BSTR *ret_display_uri, IUri **ret_uri)
2345 IUri *uri = NULL;
2346 BSTR display_uri;
2347 HRESULT hres;
2349 hres = IUri_GetDisplayUri(orig_uri, &display_uri);
2350 if(FAILED(hres))
2351 return hres;
2353 if(window->doc_obj && window->doc_obj->hostui) {
2354 OLECHAR *translated_url = NULL;
2356 hres = IDocHostUIHandler_TranslateUrl(window->doc_obj->hostui, 0, display_uri,
2357 &translated_url);
2358 if(hres == S_OK && translated_url) {
2359 TRACE("%08x %s -> %s\n", hres, debugstr_w(display_uri), debugstr_w(translated_url));
2360 SysFreeString(display_uri);
2361 hres = create_uri(translated_url, 0, &uri);
2362 CoTaskMemFree(translated_url);
2363 if(FAILED(hres))
2364 return hres;
2366 hres = IUri_GetDisplayUri(uri, &display_uri);
2367 if(FAILED(hres)) {
2368 IUri_Release(uri);
2369 return hres;
2374 if(!uri) {
2375 IUri_AddRef(orig_uri);
2376 uri = orig_uri;
2379 *ret_display_uri = display_uri;
2380 *ret_uri = uri;
2381 return S_OK;
2384 HRESULT submit_form(HTMLOuterWindow *window, const WCHAR *target, IUri *submit_uri, nsIInputStream *post_stream)
2386 request_data_t request_data = {NULL};
2387 HRESULT hres;
2389 hres = read_post_data_stream(post_stream, TRUE, NULL, &request_data);
2390 if(FAILED(hres))
2391 return hres;
2393 if(window) {
2394 IUri *uri;
2395 BSTR display_uri;
2397 window->readystate_locked++;
2399 hres = translate_uri(window, submit_uri, &display_uri, &uri);
2400 if(SUCCEEDED(hres)) {
2401 hres = navigate_uri(window, uri, display_uri, &request_data, BINDING_NAVIGATED|BINDING_SUBMIT);
2402 IUri_Release(uri);
2403 SysFreeString(display_uri);
2406 window->readystate_locked--;
2407 }else
2408 hres = navigate_new_window(window, submit_uri, target, &request_data, NULL);
2410 release_request_data(&request_data);
2411 return hres;
2414 HRESULT navigate_url(HTMLOuterWindow *window, const WCHAR *new_url, IUri *base_uri, DWORD flags)
2416 IUri *uri, *nav_uri;
2417 BSTR display_uri;
2418 HRESULT hres;
2420 if(new_url && base_uri)
2421 hres = CoInternetCombineUrlEx(base_uri, new_url, URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO,
2422 &nav_uri, 0);
2423 else
2424 hres = create_uri(new_url, 0, &nav_uri);
2425 if(FAILED(hres))
2426 return hres;
2428 hres = translate_uri(window, nav_uri, &display_uri, &uri);
2429 IUri_Release(nav_uri);
2430 if(FAILED(hres))
2431 return hres;
2433 hres = navigate_uri(window, uri, display_uri, NULL, flags);
2434 IUri_Release(uri);
2435 SysFreeString(display_uri);
2436 return hres;