reg/tests: Use a helper function to delete registry values.
[wine.git] / dlls / mshtml / navigate.c
blob1597de9a0c45ee110aa57f3fde9f96f95ce1e154
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;
251 }else if(IsEqualGUID(&IID_IBindCallbackRedirect, riid)) {
252 TRACE("(%p)->(IID_IBindCallbackRedirect %p)\n", This, ppv);
253 *ppv = &This->IBindCallbackRedirect_iface;
256 if(*ppv) {
257 IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
258 return S_OK;
261 TRACE("Unsupported riid = %s\n", debugstr_guid(riid));
262 return E_NOINTERFACE;
265 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
267 BSCallback *This = impl_from_IBindStatusCallback(iface);
268 LONG ref = InterlockedIncrement(&This->ref);
270 TRACE("(%p) ref = %d\n", This, ref);
272 return ref;
275 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
277 BSCallback *This = impl_from_IBindStatusCallback(iface);
278 LONG ref = InterlockedDecrement(&This->ref);
280 TRACE("(%p) ref = %d\n", This, ref);
282 if(!ref) {
283 release_request_data(&This->request_data);
284 if(This->mon)
285 IMoniker_Release(This->mon);
286 if(This->binding)
287 IBinding_Release(This->binding);
288 list_remove(&This->entry);
289 list_init(&This->entry);
291 This->vtbl->destroy(This);
294 return ref;
297 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
298 DWORD dwReserved, IBinding *pbind)
300 BSCallback *This = impl_from_IBindStatusCallback(iface);
302 TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
304 IBinding_AddRef(pbind);
305 This->binding = pbind;
307 if(This->window)
308 list_add_head(&This->window->bindings, &This->entry);
310 return This->vtbl->start_binding(This);
313 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
315 BSCallback *This = impl_from_IBindStatusCallback(iface);
316 FIXME("(%p)->(%p)\n", This, pnPriority);
317 return E_NOTIMPL;
320 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
322 BSCallback *This = impl_from_IBindStatusCallback(iface);
323 FIXME("(%p)->(%d)\n", This, reserved);
324 return E_NOTIMPL;
327 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
328 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
330 BSCallback *This = impl_from_IBindStatusCallback(iface);
332 TRACE("%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
333 debugstr_w(szStatusText));
335 return This->vtbl->on_progress(This, ulStatusCode, szStatusText);
338 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
339 HRESULT hresult, LPCWSTR szError)
341 BSCallback *This = impl_from_IBindStatusCallback(iface);
342 HRESULT hres;
344 TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
346 /* NOTE: IE7 calls GetBindResult here */
348 hres = This->vtbl->stop_binding(This, hresult);
350 if(This->binding) {
351 IBinding_Release(This->binding);
352 This->binding = NULL;
355 if(This->mon) {
356 IMoniker_Release(This->mon);
357 This->mon = NULL;
360 list_remove(&This->entry);
361 list_init(&This->entry);
362 This->window = NULL;
364 return hres;
367 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
368 DWORD *grfBINDF, BINDINFO *pbindinfo)
370 BSCallback *This = impl_from_IBindStatusCallback(iface);
371 DWORD size;
373 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
375 if(!This->bindinfo_ready) {
376 HRESULT hres;
378 hres = This->vtbl->init_bindinfo(This);
379 if(FAILED(hres))
380 return hres;
382 This->bindinfo_ready = TRUE;
385 *grfBINDF = This->bindf;
387 size = pbindinfo->cbSize;
388 memset(pbindinfo, 0, size);
389 pbindinfo->cbSize = size;
391 pbindinfo->cbstgmedData = This->request_data.post_data_len;
392 pbindinfo->dwCodePage = CP_UTF8;
393 pbindinfo->dwOptions = This->bindinfo_options;
395 if(This->request_data.post_data_len) {
396 pbindinfo->dwBindVerb = BINDVERB_POST;
398 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
399 pbindinfo->stgmedData.u.hGlobal = This->request_data.post_data;
400 pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)&This->IBindStatusCallback_iface;
401 IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
404 return S_OK;
407 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
408 DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
410 BSCallback *This = impl_from_IBindStatusCallback(iface);
412 TRACE("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
414 return This->vtbl->read_data(This, pstgmed->u.pstm);
417 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
418 REFIID riid, IUnknown *punk)
420 BSCallback *This = impl_from_IBindStatusCallback(iface);
421 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
422 return E_NOTIMPL;
425 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
426 BindStatusCallback_QueryInterface,
427 BindStatusCallback_AddRef,
428 BindStatusCallback_Release,
429 BindStatusCallback_OnStartBinding,
430 BindStatusCallback_GetPriority,
431 BindStatusCallback_OnLowResource,
432 BindStatusCallback_OnProgress,
433 BindStatusCallback_OnStopBinding,
434 BindStatusCallback_GetBindInfo,
435 BindStatusCallback_OnDataAvailable,
436 BindStatusCallback_OnObjectAvailable
439 static inline BSCallback *impl_from_IHttpNegotiate2(IHttpNegotiate2 *iface)
441 return CONTAINING_RECORD(iface, BSCallback, IHttpNegotiate2_iface);
444 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
445 REFIID riid, void **ppv)
447 BSCallback *This = impl_from_IHttpNegotiate2(iface);
448 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
451 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
453 BSCallback *This = impl_from_IHttpNegotiate2(iface);
454 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
457 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
459 BSCallback *This = impl_from_IHttpNegotiate2(iface);
460 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
463 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
464 LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
466 BSCallback *This = impl_from_IHttpNegotiate2(iface);
467 HRESULT hres;
469 TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
470 dwReserved, pszAdditionalHeaders);
472 *pszAdditionalHeaders = NULL;
474 hres = This->vtbl->beginning_transaction(This, pszAdditionalHeaders);
475 if(hres != S_FALSE)
476 return hres;
478 if(This->request_data.headers) {
479 DWORD size;
481 size = (strlenW(This->request_data.headers)+1)*sizeof(WCHAR);
482 *pszAdditionalHeaders = CoTaskMemAlloc(size);
483 if(!*pszAdditionalHeaders)
484 return E_OUTOFMEMORY;
485 memcpy(*pszAdditionalHeaders, This->request_data.headers, size);
488 return S_OK;
491 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
492 LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
494 BSCallback *This = impl_from_IHttpNegotiate2(iface);
496 TRACE("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
497 debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
499 return This->vtbl->on_response(This, dwResponseCode, szResponseHeaders);
502 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
503 BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
505 BSCallback *This = impl_from_IHttpNegotiate2(iface);
506 TRACE("(%p)->(%p %p %ld)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
507 return E_NOTIMPL; /* FIXME */
510 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
511 HttpNegotiate_QueryInterface,
512 HttpNegotiate_AddRef,
513 HttpNegotiate_Release,
514 HttpNegotiate_BeginningTransaction,
515 HttpNegotiate_OnResponse,
516 HttpNegotiate_GetRootSecurityId
519 static inline BSCallback *impl_from_IInternetBindInfo(IInternetBindInfo *iface)
521 return CONTAINING_RECORD(iface, BSCallback, IInternetBindInfo_iface);
524 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
525 REFIID riid, void **ppv)
527 BSCallback *This = impl_from_IInternetBindInfo(iface);
528 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
531 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
533 BSCallback *This = impl_from_IInternetBindInfo(iface);
534 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
537 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
539 BSCallback *This = impl_from_IInternetBindInfo(iface);
540 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
543 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
544 DWORD *grfBINDF, BINDINFO *pbindinfo)
546 BSCallback *This = impl_from_IInternetBindInfo(iface);
547 FIXME("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
548 return E_NOTIMPL;
551 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
552 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
554 BSCallback *This = impl_from_IInternetBindInfo(iface);
555 FIXME("(%p)->(%u %p %u %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
556 return E_NOTIMPL;
559 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
560 InternetBindInfo_QueryInterface,
561 InternetBindInfo_AddRef,
562 InternetBindInfo_Release,
563 InternetBindInfo_GetBindInfo,
564 InternetBindInfo_GetBindString
567 static inline BSCallback *impl_from_IBindCallbackRedirect(IBindCallbackRedirect *iface)
569 return CONTAINING_RECORD(iface, BSCallback, IBindCallbackRedirect_iface);
572 static HRESULT WINAPI BindCallbackRedirect_QueryInterface(IBindCallbackRedirect *iface, REFIID riid, void **ppv)
574 BSCallback *This = impl_from_IBindCallbackRedirect(iface);
575 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
578 static ULONG WINAPI BindCallbackRedirect_AddRef(IBindCallbackRedirect *iface)
580 BSCallback *This = impl_from_IBindCallbackRedirect(iface);
581 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
584 static ULONG WINAPI BindCallbackRedirect_Release(IBindCallbackRedirect *iface)
586 BSCallback *This = impl_from_IBindCallbackRedirect(iface);
587 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
590 static HRESULT WINAPI BindCallbackRedirect_Redirect(IBindCallbackRedirect *iface, const WCHAR *url, VARIANT_BOOL *vbCancel)
592 BSCallback *This = impl_from_IBindCallbackRedirect(iface);
593 HTMLDocumentObj *doc_obj;
594 BOOL cancel = FALSE;
595 BSTR frame_name = NULL;
596 HRESULT hres = S_OK;
598 TRACE("(%p)->(%s %p)\n", This, debugstr_w(url), vbCancel);
600 if(This->window && This->window->base.outer_window && (doc_obj = This->window->base.outer_window->doc_obj)
601 && doc_obj->doc_object_service) {
602 if(This->window->base.outer_window != doc_obj->basedoc.window) {
603 hres = IHTMLWindow2_get_name(&This->window->base.IHTMLWindow2_iface, &frame_name);
604 if(FAILED(hres))
605 return hres;
608 hres = IDocObjectService_FireBeforeNavigate2(doc_obj->doc_object_service, NULL, url, 0x40,
609 frame_name, NULL, 0, NULL, TRUE, &cancel);
610 SysFreeString(frame_name);
613 *vbCancel = cancel ? VARIANT_TRUE : VARIANT_FALSE;
614 return hres;
617 static const IBindCallbackRedirectVtbl BindCallbackRedirectVtbl = {
618 BindCallbackRedirect_QueryInterface,
619 BindCallbackRedirect_AddRef,
620 BindCallbackRedirect_Release,
621 BindCallbackRedirect_Redirect
624 static inline BSCallback *impl_from_IServiceProvider(IServiceProvider *iface)
626 return CONTAINING_RECORD(iface, BSCallback, IServiceProvider_iface);
629 static HRESULT WINAPI BSCServiceProvider_QueryInterface(IServiceProvider *iface,
630 REFIID riid, void **ppv)
632 BSCallback *This = impl_from_IServiceProvider(iface);
633 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
636 static ULONG WINAPI BSCServiceProvider_AddRef(IServiceProvider *iface)
638 BSCallback *This = impl_from_IServiceProvider(iface);
639 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
642 static ULONG WINAPI BSCServiceProvider_Release(IServiceProvider *iface)
644 BSCallback *This = impl_from_IServiceProvider(iface);
645 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
648 static HRESULT WINAPI BSCServiceProvider_QueryService(IServiceProvider *iface,
649 REFGUID guidService, REFIID riid, void **ppv)
651 BSCallback *This = impl_from_IServiceProvider(iface);
653 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
655 if(This->window && IsEqualGUID(guidService, &IID_IWindowForBindingUI))
656 return IServiceProvider_QueryService(&This->window->base.IServiceProvider_iface, guidService, riid, ppv);
657 return E_NOINTERFACE;
660 static const IServiceProviderVtbl ServiceProviderVtbl = {
661 BSCServiceProvider_QueryInterface,
662 BSCServiceProvider_AddRef,
663 BSCServiceProvider_Release,
664 BSCServiceProvider_QueryService
667 void init_bscallback(BSCallback *This, const BSCallbackVtbl *vtbl, IMoniker *mon, DWORD bindf)
669 This->IBindStatusCallback_iface.lpVtbl = &BindStatusCallbackVtbl;
670 This->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
671 This->IHttpNegotiate2_iface.lpVtbl = &HttpNegotiate2Vtbl;
672 This->IInternetBindInfo_iface.lpVtbl = &InternetBindInfoVtbl;
673 This->IBindCallbackRedirect_iface.lpVtbl = &BindCallbackRedirectVtbl;
674 This->vtbl = vtbl;
675 This->ref = 1;
676 This->bindf = bindf;
677 This->bindinfo_options = BINDINFO_OPTIONS_USE_IE_ENCODING;
678 This->bom = BOM_NONE;
680 list_init(&This->entry);
682 if(mon)
683 IMoniker_AddRef(mon);
684 This->mon = mon;
687 HRESULT read_stream(BSCallback *This, IStream *stream, void *buf, DWORD size, DWORD *ret_size)
689 DWORD read_size = 0, skip=0;
690 BYTE *data = buf;
691 HRESULT hres;
693 hres = IStream_Read(stream, buf, size, &read_size);
695 if(!This->readed && This->bom == BOM_NONE) {
696 if(read_size >= 2 && data[0] == 0xff && data[1] == 0xfe) {
697 This->bom = BOM_UTF16;
698 skip = 2;
699 }else if(read_size >= 3 && data[0] == 0xef && data[1] == 0xbb && data[2] == 0xbf) {
700 This->bom = BOM_UTF8;
701 skip = 3;
703 if(skip) {
704 read_size -= skip;
705 if(read_size)
706 memmove(data, data+skip, read_size);
710 This->readed += read_size;
711 *ret_size = read_size;
712 return hres;
715 static void parse_content_type(nsChannelBSC *This, const WCHAR *value)
717 const WCHAR *ptr;
718 size_t len;
720 static const WCHAR charsetW[] = {'c','h','a','r','s','e','t','='};
722 ptr = strchrW(value, ';');
723 if(!ptr)
724 return;
726 ptr++;
727 while(*ptr && isspaceW(*ptr))
728 ptr++;
730 len = strlenW(value);
731 if(ptr + sizeof(charsetW)/sizeof(WCHAR) < value+len && !memicmpW(ptr, charsetW, sizeof(charsetW)/sizeof(WCHAR))) {
732 size_t charset_len, lena;
733 nsACString charset_str;
734 const WCHAR *charset;
735 char *charseta;
737 ptr += sizeof(charsetW)/sizeof(WCHAR);
739 if(*ptr == '\'') {
740 FIXME("Quoted value\n");
741 return;
742 }else {
743 charset = ptr;
744 while(*ptr && *ptr != ',')
745 ptr++;
746 charset_len = ptr-charset;
749 lena = WideCharToMultiByte(CP_ACP, 0, charset, charset_len, NULL, 0, NULL, NULL);
750 charseta = heap_alloc(lena+1);
751 if(!charseta)
752 return;
754 WideCharToMultiByte(CP_ACP, 0, charset, charset_len, charseta, lena, NULL, NULL);
755 charseta[lena] = 0;
757 nsACString_InitDepend(&charset_str, charseta);
758 nsIHttpChannel_SetContentCharset(&This->nschannel->nsIHttpChannel_iface, &charset_str);
759 nsACString_Finish(&charset_str);
760 heap_free(charseta);
761 }else {
762 FIXME("unhandled: %s\n", debugstr_wn(ptr, len - (ptr-value)));
766 static HRESULT parse_headers(const WCHAR *headers, struct list *headers_list)
768 const WCHAR *header, *header_end, *colon, *value;
769 HRESULT hres;
771 header = headers;
772 while(*header) {
773 if(header[0] == '\r' && header[1] == '\n' && !header[2])
774 break;
775 for(colon = header; *colon && *colon != ':' && *colon != '\r'; colon++);
776 if(*colon != ':')
777 return E_FAIL;
779 value = colon+1;
780 while(*value == ' ')
781 value++;
782 if(!*value)
783 return E_FAIL;
785 for(header_end = value+1; *header_end && *header_end != '\r'; header_end++);
787 hres = set_http_header(headers_list, header, colon-header, value, header_end-value);
788 if(FAILED(hres))
789 return hres;
791 header = header_end;
792 if(header[0] == '\r' && header[1] == '\n')
793 header += 2;
796 return S_OK;
799 static HRESULT process_response_headers(nsChannelBSC *This, const WCHAR *headers)
801 http_header_t *iter;
802 HRESULT hres;
804 static const WCHAR content_typeW[] = {'c','o','n','t','e','n','t','-','t','y','p','e',0};
806 hres = parse_headers(headers, &This->nschannel->response_headers);
807 if(FAILED(hres))
808 return hres;
810 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->response_headers, http_header_t, entry) {
811 if(!strcmpiW(iter->header, content_typeW))
812 parse_content_type(This, iter->data);
815 return S_OK;
818 static void query_http_info(nsChannelBSC *This, IWinInetHttpInfo *wininet_info)
820 const WCHAR *ptr;
821 DWORD len = 0;
822 WCHAR *buf;
824 IWinInetHttpInfo_QueryInfo(wininet_info, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &len, NULL, NULL);
825 if(!len)
826 return;
828 buf = heap_alloc(len);
829 if(!buf)
830 return;
832 IWinInetHttpInfo_QueryInfo(wininet_info, HTTP_QUERY_RAW_HEADERS_CRLF, buf, &len, NULL, NULL);
833 if(!len) {
834 heap_free(buf);
835 return;
838 ptr = strchrW(buf, '\r');
839 if(ptr && ptr[1] == '\n') {
840 ptr += 2;
841 process_response_headers(This, ptr);
844 heap_free(buf);
847 HRESULT start_binding(HTMLInnerWindow *inner_window, BSCallback *bscallback, IBindCtx *bctx)
849 IStream *str = NULL;
850 HRESULT hres;
852 TRACE("(%p %p %p)\n", inner_window, bscallback, bctx);
854 bscallback->window = inner_window;
856 /* NOTE: IE7 calls IsSystemMoniker here*/
858 if(bctx) {
859 hres = RegisterBindStatusCallback(bctx, &bscallback->IBindStatusCallback_iface, NULL, 0);
860 if(SUCCEEDED(hres))
861 IBindCtx_AddRef(bctx);
862 }else {
863 hres = CreateAsyncBindCtx(0, &bscallback->IBindStatusCallback_iface, NULL, &bctx);
866 if(FAILED(hres)) {
867 bscallback->window = NULL;
868 return hres;
871 hres = IMoniker_BindToStorage(bscallback->mon, bctx, NULL, &IID_IStream, (void**)&str);
872 IBindCtx_Release(bctx);
873 if(FAILED(hres)) {
874 WARN("BindToStorage failed: %08x\n", hres);
875 bscallback->window = NULL;
876 return hres;
879 if(str)
880 IStream_Release(str);
882 return S_OK;
885 static HRESULT read_post_data_stream(nsIInputStream *stream, BOOL contains_headers, struct list *headers_list,
886 request_data_t *request_data)
888 nsISeekableStream *seekable_stream;
889 UINT64 available = 0;
890 UINT32 data_len = 0;
891 char *data, *post_data;
892 nsresult nsres;
893 HRESULT hres = S_OK;
895 if(!stream)
896 return S_OK;
898 nsres = nsIInputStream_Available(stream, &available);
899 if(NS_FAILED(nsres))
900 return E_FAIL;
902 post_data = data = GlobalAlloc(0, available+1);
903 if(!data)
904 return E_OUTOFMEMORY;
906 nsres = nsIInputStream_Read(stream, data, available, &data_len);
907 if(NS_FAILED(nsres)) {
908 GlobalFree(data);
909 return E_FAIL;
912 if(contains_headers) {
913 if(data_len >= 2 && data[0] == '\r' && data[1] == '\n') {
914 post_data = data+2;
915 data_len -= 2;
916 }else {
917 WCHAR *headers;
918 DWORD size;
919 char *ptr;
921 post_data += data_len;
922 for(ptr = data; ptr+4 < data+data_len; ptr++) {
923 if(!memcmp(ptr, "\r\n\r\n", 4)) {
924 ptr += 2;
925 post_data = ptr+2;
926 break;
930 data_len -= post_data-data;
932 size = MultiByteToWideChar(CP_ACP, 0, data, ptr-data, NULL, 0);
933 headers = heap_alloc((size+1)*sizeof(WCHAR));
934 if(headers) {
935 MultiByteToWideChar(CP_ACP, 0, data, ptr-data, headers, size);
936 headers[size] = 0;
937 if(headers_list)
938 hres = parse_headers(headers, headers_list);
939 if(SUCCEEDED(hres))
940 request_data->headers = headers;
941 else
942 heap_free(headers);
943 }else {
944 hres = E_OUTOFMEMORY;
949 if(FAILED(hres)) {
950 GlobalFree(data);
951 return hres;
954 if(!data_len) {
955 GlobalFree(data);
956 post_data = NULL;
957 }else if(post_data != data) {
958 char *new_data;
960 new_data = GlobalAlloc(0, data_len+1);
961 if(new_data)
962 memcpy(new_data, post_data, data_len);
963 GlobalFree(data);
964 if(!new_data)
965 return E_OUTOFMEMORY;
966 post_data = new_data;
969 if(post_data)
970 post_data[data_len] = 0;
971 request_data->post_data = post_data;
972 request_data->post_data_len = data_len;
974 nsres = nsIInputStream_QueryInterface(stream, &IID_nsISeekableStream, (void**)&seekable_stream);
975 assert(nsres == NS_OK);
977 nsres = nsISeekableStream_Seek(seekable_stream, NS_SEEK_SET, 0);
978 assert(nsres == NS_OK);
980 nsISeekableStream_Release(seekable_stream);
982 nsIInputStream_AddRef(stream);
983 request_data->post_stream = stream;
984 TRACE("post_data = %s\n", debugstr_an(request_data->post_data, request_data->post_data_len));
985 return S_OK;
988 static HRESULT on_start_nsrequest(nsChannelBSC *This)
990 nsresult nsres;
992 This->nschannel->binding = This;
994 /* FIXME: it's needed for http connections from BindToObject. */
995 if(!This->nschannel->response_status)
996 This->nschannel->response_status = 200;
998 nsres = nsIStreamListener_OnStartRequest(This->nslistener,
999 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext);
1000 if(NS_FAILED(nsres)) {
1001 FIXME("OnStartRequest failed: %08x\n", nsres);
1002 return E_FAIL;
1005 if(This->is_doc_channel) {
1006 if(!This->bsc.window)
1007 return E_ABORT; /* Binding aborted in OnStartRequest call. */
1008 update_window_doc(This->bsc.window);
1009 if(This->bsc.window->base.outer_window->readystate != READYSTATE_LOADING)
1010 set_ready_state(This->bsc.window->base.outer_window, READYSTATE_LOADING);
1013 return S_OK;
1016 static void on_stop_nsrequest(nsChannelBSC *This, HRESULT result)
1018 nsresult nsres, request_result;
1020 switch(result) {
1021 case S_OK:
1022 request_result = NS_OK;
1023 break;
1024 case E_ABORT:
1025 request_result = NS_BINDING_ABORTED;
1026 break;
1027 default:
1028 request_result = NS_ERROR_FAILURE;
1031 if(This->nslistener) {
1032 nsres = nsIStreamListener_OnStopRequest(This->nslistener,
1033 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext,
1034 request_result);
1035 if(NS_FAILED(nsres))
1036 WARN("OnStopRequest failed: %08x\n", nsres);
1039 if(This->nschannel) {
1040 if(This->nschannel->load_group) {
1041 nsres = nsILoadGroup_RemoveRequest(This->nschannel->load_group,
1042 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, NULL, request_result);
1043 if(NS_FAILED(nsres))
1044 ERR("RemoveRequest failed: %08x\n", nsres);
1046 if(This->nschannel->binding == This)
1047 This->nschannel->binding = NULL;
1051 static HRESULT read_stream_data(nsChannelBSC *This, IStream *stream)
1053 DWORD read;
1054 nsresult nsres;
1055 HRESULT hres;
1057 if(!This->response_processed) {
1058 IWinInetHttpInfo *wininet_info;
1060 This->response_processed = TRUE;
1061 if(This->bsc.binding) {
1062 hres = IBinding_QueryInterface(This->bsc.binding, &IID_IWinInetHttpInfo, (void**)&wininet_info);
1063 if(SUCCEEDED(hres)) {
1064 query_http_info(This, wininet_info);
1065 IWinInetHttpInfo_Release(wininet_info);
1070 if(!This->nschannel)
1071 return S_OK;
1073 if(!This->nslistener) {
1074 BYTE buf[1024];
1076 do {
1077 hres = read_stream(&This->bsc, stream, buf, sizeof(buf), &read);
1078 }while(hres == S_OK && read);
1080 return S_OK;
1083 if(!This->nsstream) {
1084 This->nsstream = create_nsprotocol_stream();
1085 if(!This->nsstream)
1086 return E_OUTOFMEMORY;
1089 do {
1090 BOOL first_read = !This->bsc.readed;
1092 hres = read_stream(&This->bsc, stream, This->nsstream->buf+This->nsstream->buf_size,
1093 sizeof(This->nsstream->buf)-This->nsstream->buf_size, &read);
1094 if(!read)
1095 break;
1097 This->nsstream->buf_size += read;
1099 if(first_read) {
1100 switch(This->bsc.bom) {
1101 case BOM_UTF8:
1102 This->nschannel->charset = heap_strdupA(UTF8_STR);
1103 break;
1104 case BOM_UTF16:
1105 This->nschannel->charset = heap_strdupA(UTF16_STR);
1106 case BOM_NONE:
1107 /* FIXME: Get charset from HTTP headers */;
1110 if(!This->nschannel->content_type) {
1111 WCHAR *mime;
1113 hres = FindMimeFromData(NULL, NULL, This->nsstream->buf, This->nsstream->buf_size,
1114 This->is_doc_channel ? text_htmlW : NULL, 0, &mime, 0);
1115 if(FAILED(hres))
1116 return hres;
1118 TRACE("Found MIME %s\n", debugstr_w(mime));
1120 This->nschannel->content_type = heap_strdupWtoA(mime);
1121 CoTaskMemFree(mime);
1122 if(!This->nschannel->content_type)
1123 return E_OUTOFMEMORY;
1126 hres = on_start_nsrequest(This);
1127 if(FAILED(hres))
1128 return hres;
1131 nsres = nsIStreamListener_OnDataAvailable(This->nslistener,
1132 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext,
1133 &This->nsstream->nsIInputStream_iface, This->bsc.readed-This->nsstream->buf_size,
1134 This->nsstream->buf_size);
1135 if(NS_FAILED(nsres))
1136 ERR("OnDataAvailable failed: %08x\n", nsres);
1138 if(This->nsstream->buf_size == sizeof(This->nsstream->buf)) {
1139 ERR("buffer is full\n");
1140 break;
1142 }while(hres == S_OK);
1144 return S_OK;
1147 typedef struct {
1148 nsIAsyncVerifyRedirectCallback nsIAsyncVerifyRedirectCallback_iface;
1150 LONG ref;
1152 nsChannel *nschannel;
1153 nsChannelBSC *bsc;
1154 } nsRedirectCallback;
1156 static nsRedirectCallback *impl_from_nsIAsyncVerifyRedirectCallback(nsIAsyncVerifyRedirectCallback *iface)
1158 return CONTAINING_RECORD(iface, nsRedirectCallback, nsIAsyncVerifyRedirectCallback_iface);
1161 static nsresult NSAPI nsAsyncVerifyRedirectCallback_QueryInterface(nsIAsyncVerifyRedirectCallback *iface,
1162 nsIIDRef riid, void **result)
1164 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1166 if(IsEqualGUID(&IID_nsISupports, riid)) {
1167 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1168 *result = &This->nsIAsyncVerifyRedirectCallback_iface;
1169 }else if(IsEqualGUID(&IID_nsIAsyncVerifyRedirectCallback, riid)) {
1170 TRACE("(%p)->(IID_nsIAsyncVerifyRedirectCallback %p)\n", This, result);
1171 *result = &This->nsIAsyncVerifyRedirectCallback_iface;
1172 }else {
1173 *result = NULL;
1174 WARN("unimplemented iface %s\n", debugstr_guid(riid));
1175 return NS_NOINTERFACE;
1178 nsISupports_AddRef((nsISupports*)*result);
1179 return NS_OK;
1182 static nsrefcnt NSAPI nsAsyncVerifyRedirectCallback_AddRef(nsIAsyncVerifyRedirectCallback *iface)
1184 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1185 LONG ref = InterlockedIncrement(&This->ref);
1187 TRACE("(%p) ref=%d\n", This, ref);
1189 return ref;
1192 static nsrefcnt NSAPI nsAsyncVerifyRedirectCallback_Release(nsIAsyncVerifyRedirectCallback *iface)
1194 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1195 LONG ref = InterlockedDecrement(&This->ref);
1197 TRACE("(%p) ref=%d\n", This, ref);
1199 if(!ref) {
1200 IBindStatusCallback_Release(&This->bsc->bsc.IBindStatusCallback_iface);
1201 nsIHttpChannel_Release(&This->nschannel->nsIHttpChannel_iface);
1202 heap_free(This);
1205 return ref;
1208 static nsresult NSAPI nsAsyncVerifyRedirectCallback_OnRedirectVerifyCallback(nsIAsyncVerifyRedirectCallback *iface, nsresult result)
1210 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1211 nsChannel *old_nschannel;
1212 nsresult nsres;
1214 TRACE("(%p)->(%08x)\n", This, result);
1216 old_nschannel = This->bsc->nschannel;
1217 nsIHttpChannel_AddRef(&This->nschannel->nsIHttpChannel_iface);
1218 This->bsc->nschannel = This->nschannel;
1220 if(This->nschannel->load_group) {
1221 nsres = nsILoadGroup_AddRequest(This->nschannel->load_group, (nsIRequest*)&This->nschannel->nsIHttpChannel_iface,
1222 NULL);
1223 if(NS_FAILED(nsres))
1224 ERR("AddRequest failed: %08x\n", nsres);
1227 if(This->bsc->is_doc_channel) {
1228 IUri *uri = nsuri_get_uri(This->nschannel->uri);
1230 if(uri) {
1231 set_current_uri(This->bsc->bsc.window->base.outer_window, uri);
1232 IUri_Release(uri);
1233 }else {
1234 WARN("Could not get IUri from nsWineURI\n");
1238 if(old_nschannel) {
1239 if(old_nschannel->load_group) {
1240 nsres = nsILoadGroup_RemoveRequest(old_nschannel->load_group,
1241 (nsIRequest*)&old_nschannel->nsIHttpChannel_iface, NULL, NS_OK);
1242 if(NS_FAILED(nsres))
1243 ERR("RemoveRequest failed: %08x\n", nsres);
1245 nsIHttpChannel_Release(&old_nschannel->nsIHttpChannel_iface);
1248 return NS_OK;
1251 static const nsIAsyncVerifyRedirectCallbackVtbl nsAsyncVerifyRedirectCallbackVtbl = {
1252 nsAsyncVerifyRedirectCallback_QueryInterface,
1253 nsAsyncVerifyRedirectCallback_AddRef,
1254 nsAsyncVerifyRedirectCallback_Release,
1255 nsAsyncVerifyRedirectCallback_OnRedirectVerifyCallback
1258 static HRESULT create_redirect_callback(nsChannel *nschannel, nsChannelBSC *bsc, nsRedirectCallback **ret)
1260 nsRedirectCallback *callback;
1262 callback = heap_alloc(sizeof(*callback));
1263 if(!callback)
1264 return E_OUTOFMEMORY;
1266 callback->nsIAsyncVerifyRedirectCallback_iface.lpVtbl = &nsAsyncVerifyRedirectCallbackVtbl;
1267 callback->ref = 1;
1269 nsIHttpChannel_AddRef(&nschannel->nsIHttpChannel_iface);
1270 callback->nschannel = nschannel;
1272 IBindStatusCallback_AddRef(&bsc->bsc.IBindStatusCallback_iface);
1273 callback->bsc = bsc;
1275 *ret = callback;
1276 return S_OK;
1279 static inline nsChannelBSC *nsChannelBSC_from_BSCallback(BSCallback *iface)
1281 return CONTAINING_RECORD(iface, nsChannelBSC, bsc);
1284 static void nsChannelBSC_destroy(BSCallback *bsc)
1286 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1288 if(This->nschannel) {
1289 if(This->nschannel->binding == This)
1290 This->nschannel->binding = NULL;
1291 nsIHttpChannel_Release(&This->nschannel->nsIHttpChannel_iface);
1293 if(This->nslistener)
1294 nsIStreamListener_Release(This->nslistener);
1295 if(This->nscontext)
1296 nsISupports_Release(This->nscontext);
1297 if(This->nsstream)
1298 nsIInputStream_Release(&This->nsstream->nsIInputStream_iface);
1299 heap_free(This);
1302 static HRESULT nsChannelBSC_start_binding(BSCallback *bsc)
1304 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1306 if(This->is_doc_channel)
1307 This->bsc.window->base.outer_window->base.inner_window->doc->skip_mutation_notif = FALSE;
1309 return S_OK;
1312 static HRESULT nsChannelBSC_init_bindinfo(BSCallback *bsc)
1314 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1315 nsChannel *nschannel = This->nschannel;
1316 HTMLDocumentObj *doc_obj;
1317 HRESULT hres;
1319 if(This->is_doc_channel && This->bsc.window && This->bsc.window->base.outer_window
1320 && (doc_obj = This->bsc.window->base.outer_window->doc_obj)) {
1321 if(doc_obj->hostinfo.dwFlags & DOCHOSTUIFLAG_ENABLE_REDIRECT_NOTIFICATION)
1322 This->bsc.bindinfo_options |= BINDINFO_OPTIONS_DISABLEAUTOREDIRECTS;
1325 if(nschannel && nschannel->post_data_stream) {
1326 hres = read_post_data_stream(nschannel->post_data_stream, nschannel->post_data_contains_headers,
1327 &nschannel->request_headers, &This->bsc.request_data);
1328 if(FAILED(hres))
1329 return hres;
1332 return S_OK;
1335 typedef struct {
1336 task_t header;
1337 nsChannelBSC *bsc;
1338 } stop_request_task_t;
1340 static void stop_request_proc(task_t *_task)
1342 stop_request_task_t *task = (stop_request_task_t*)_task;
1344 TRACE("(%p)\n", task->bsc);
1346 list_remove(&task->bsc->bsc.entry);
1347 list_init(&task->bsc->bsc.entry);
1348 on_stop_nsrequest(task->bsc, S_OK);
1351 static void stop_request_task_destr(task_t *_task)
1353 stop_request_task_t *task = (stop_request_task_t*)_task;
1355 IBindStatusCallback_Release(&task->bsc->bsc.IBindStatusCallback_iface);
1356 heap_free(task);
1359 static HRESULT async_stop_request(nsChannelBSC *This)
1361 stop_request_task_t *task;
1363 if(!This->bsc.readed) {
1364 TRACE("No data read, calling OnStartRequest\n");
1365 on_start_nsrequest(This);
1368 task = heap_alloc(sizeof(*task));
1369 if(!task)
1370 return E_OUTOFMEMORY;
1372 IBindStatusCallback_AddRef(&This->bsc.IBindStatusCallback_iface);
1373 task->bsc = This;
1375 return push_task(&task->header, stop_request_proc, stop_request_task_destr, This->bsc.window->task_magic);
1378 static void handle_navigation_error(nsChannelBSC *This, DWORD result)
1380 HTMLOuterWindow *outer_window;
1381 HTMLDocumentObj *doc;
1382 BOOL is_error_url;
1383 SAFEARRAY *sa;
1384 SAFEARRAYBOUND bound;
1385 VARIANT var, varOut;
1386 LONG ind;
1387 BSTR unk;
1388 HRESULT hres;
1390 if(!This->is_doc_channel || !This->bsc.window)
1391 return;
1393 outer_window = This->bsc.window->base.outer_window;
1395 doc = outer_window->doc_obj;
1396 if(!doc || !doc->doc_object_service || !doc->client)
1397 return;
1399 hres = IDocObjectService_IsErrorUrl(doc->doc_object_service,
1400 outer_window->url, &is_error_url);
1401 if(FAILED(hres) || is_error_url)
1402 return;
1404 if(!doc->client_cmdtrg)
1405 return;
1407 bound.lLbound = 0;
1408 bound.cElements = 8;
1409 sa = SafeArrayCreate(VT_VARIANT, 1, &bound);
1410 if(!sa)
1411 return;
1413 ind = 0;
1414 V_VT(&var) = VT_I4;
1415 V_I4(&var) = result;
1416 SafeArrayPutElement(sa, &ind, &var);
1418 ind = 1;
1419 V_VT(&var) = VT_BSTR;
1420 V_BSTR(&var) = outer_window->url;
1421 SafeArrayPutElement(sa, &ind, &var);
1423 ind = 3;
1424 V_VT(&var) = VT_UNKNOWN;
1425 V_UNKNOWN(&var) = (IUnknown*)&outer_window->base.IHTMLWindow2_iface;
1426 SafeArrayPutElement(sa, &ind, &var);
1428 /* FIXME: what are the following fields for? */
1429 ind = 2;
1430 V_VT(&var) = VT_UNKNOWN;
1431 V_UNKNOWN(&var) = NULL;
1432 SafeArrayPutElement(sa, &ind, &var);
1434 ind = 4;
1435 V_VT(&var) = VT_BOOL;
1436 V_BOOL(&var) = FALSE;
1437 SafeArrayPutElement(sa, &ind, &var);
1439 ind = 5;
1440 V_VT(&var) = VT_BOOL;
1441 V_BOOL(&var) = FALSE;
1442 SafeArrayPutElement(sa, &ind, &var);
1444 ind = 6;
1445 V_VT(&var) = VT_BSTR;
1446 unk = SysAllocString(NULL);
1447 V_BSTR(&var) = unk;
1448 SafeArrayPutElement(sa, &ind, &var);
1450 ind = 7;
1451 V_VT(&var) = VT_UNKNOWN;
1452 V_UNKNOWN(&var) = NULL;
1453 SafeArrayPutElement(sa, &ind, &var);
1455 V_VT(&var) = VT_ARRAY;
1456 V_ARRAY(&var) = sa;
1457 V_VT(&varOut) = VT_BOOL;
1458 V_BOOL(&varOut) = VARIANT_TRUE;
1459 IOleCommandTarget_Exec(doc->client_cmdtrg, &CGID_DocHostCmdPriv, 1, 0, &var, FAILED(hres)?NULL:&varOut);
1461 SysFreeString(unk);
1462 SafeArrayDestroy(sa);
1465 static HRESULT nsChannelBSC_stop_binding(BSCallback *bsc, HRESULT result)
1467 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1469 if(result != E_ABORT && This->is_doc_channel && This->bsc.window) {
1470 if(FAILED(result))
1471 handle_navigation_error(This, result);
1472 else if(This->nschannel) {
1473 result = async_stop_request(This);
1474 if(SUCCEEDED(result))
1475 return S_OK;
1479 on_stop_nsrequest(This, result);
1480 return S_OK;
1483 static HRESULT nsChannelBSC_read_data(BSCallback *bsc, IStream *stream)
1485 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1487 return read_stream_data(This, stream);
1490 static HRESULT handle_redirect(nsChannelBSC *This, const WCHAR *new_url)
1492 nsRedirectCallback *callback;
1493 nsIChannelEventSink *sink;
1494 nsChannel *new_channel;
1495 IMoniker *mon;
1496 nsresult nsres;
1497 HRESULT hres;
1499 TRACE("(%p)->(%s)\n", This, debugstr_w(new_url));
1501 hres = CreateURLMoniker(NULL, new_url, &mon);
1502 if(FAILED(hres))
1503 return hres;
1504 IMoniker_Release(This->bsc.mon);
1505 This->bsc.mon = mon;
1507 if(!This->nschannel || !This->nschannel->notif_callback)
1508 return S_OK;
1510 nsres = nsIInterfaceRequestor_GetInterface(This->nschannel->notif_callback, &IID_nsIChannelEventSink, (void**)&sink);
1511 if(NS_FAILED(nsres))
1512 return S_OK;
1514 hres = create_redirect_nschannel(new_url, This->nschannel, &new_channel);
1515 if(SUCCEEDED(hres)) {
1516 TRACE("%p %p->%p\n", This, This->nschannel, new_channel);
1518 hres = create_redirect_callback(new_channel, This, &callback);
1519 nsIHttpChannel_Release(&new_channel->nsIHttpChannel_iface);
1522 if(SUCCEEDED(hres)) {
1523 nsres = nsIChannelEventSink_AsyncOnChannelRedirect(sink, (nsIChannel*)&This->nschannel->nsIHttpChannel_iface,
1524 (nsIChannel*)&callback->nschannel->nsIHttpChannel_iface, REDIRECT_TEMPORARY, /* FIXME */
1525 &callback->nsIAsyncVerifyRedirectCallback_iface);
1527 if(NS_FAILED(nsres))
1528 FIXME("AsyncOnChannelRedirect failed: %08x\n", hres);
1529 else if(This->nschannel != callback->nschannel)
1530 FIXME("nschannel not updated\n");
1532 nsIAsyncVerifyRedirectCallback_Release(&callback->nsIAsyncVerifyRedirectCallback_iface);
1535 nsIChannelEventSink_Release(sink);
1536 return hres;
1539 static BOOL is_supported_doc_mime(const WCHAR *mime)
1541 char *nscat, *mimea;
1542 BOOL ret;
1544 mimea = heap_strdupWtoA(mime);
1545 if(!mimea)
1546 return FALSE;
1548 nscat = get_nscategory_entry("Gecko-Content-Viewers", mimea);
1550 ret = nscat != NULL && !strcmp(nscat, "@mozilla.org/content/document-loader-factory;1");
1552 heap_free(mimea);
1553 nsfree(nscat);
1554 return ret;
1557 static IUri *get_moniker_uri(IMoniker *mon)
1559 IUriContainer *uri_container;
1560 IUri *ret = NULL;
1561 HRESULT hres;
1563 hres = IMoniker_QueryInterface(mon, &IID_IUriContainer, (void**)&uri_container);
1564 if(SUCCEEDED(hres)) {
1565 hres = IUriContainer_GetIUri(uri_container, &ret);
1566 IUriContainer_Release(uri_container);
1567 if(FAILED(hres))
1568 return NULL;
1569 }else {
1570 FIXME("No IUriContainer\n");
1573 return ret;
1576 static void handle_extern_mime_navigation(nsChannelBSC *This)
1578 IWebBrowserPriv2IE9 *webbrowser_priv;
1579 IOleCommandTarget *cmdtrg;
1580 HTMLDocumentObj *doc_obj;
1581 IBindCtx *bind_ctx;
1582 IUri *uri;
1583 VARIANT flags;
1584 HRESULT hres;
1586 if(!This->bsc.window || !This->bsc.window->base.outer_window || !This->bsc.window->base.outer_window->doc_obj)
1587 return;
1589 doc_obj = This->bsc.window->base.outer_window->doc_obj;
1591 hres = IOleClientSite_QueryInterface(doc_obj->client, &IID_IOleCommandTarget, (void**)&cmdtrg);
1592 if(SUCCEEDED(hres)) {
1593 IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 62, 0, NULL, NULL);
1594 IOleCommandTarget_Release(cmdtrg);
1597 set_document_navigation(doc_obj, FALSE);
1599 if(!doc_obj->webbrowser) {
1600 FIXME("unimplemented in non-webbrowser mode\n");
1601 return;
1604 uri = get_moniker_uri(This->bsc.mon);
1605 if(!uri)
1606 return;
1608 hres = CreateBindCtx(0, &bind_ctx);
1609 if(FAILED(hres)) {
1610 IUri_Release(uri);
1611 return;
1614 V_VT(&flags) = VT_I4;
1615 V_I4(&flags) = navHyperlink;
1617 hres = IUnknown_QueryInterface(doc_obj->webbrowser, &IID_IWebBrowserPriv2IE8, (void**)&webbrowser_priv);
1618 if(SUCCEEDED(hres)) {
1619 hres = IWebBrowserPriv2IE9_NavigateWithBindCtx2(webbrowser_priv, uri, &flags, NULL, NULL, NULL, bind_ctx, NULL, 0);
1620 IWebBrowserPriv2IE9_Release(webbrowser_priv);
1621 }else {
1622 IWebBrowserPriv *webbrowser_priv_old;
1623 VARIANT uriv;
1625 hres = IUnknown_QueryInterface(doc_obj->webbrowser, &IID_IWebBrowserPriv, (void**)&webbrowser_priv_old);
1626 if(SUCCEEDED(hres)) {
1627 V_VT(&uriv) = VT_BSTR;
1628 IUri_GetDisplayUri(uri, &V_BSTR(&uriv));
1630 hres = IWebBrowserPriv_NavigateWithBindCtx(webbrowser_priv_old, &uriv, &flags, NULL, NULL, NULL, bind_ctx, NULL);
1632 SysFreeString(V_BSTR(&uriv));
1633 IWebBrowserPriv_Release(webbrowser_priv_old);
1637 IUri_Release(uri);
1640 static HRESULT nsChannelBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
1642 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1644 switch(status_code) {
1645 case BINDSTATUS_MIMETYPEAVAILABLE:
1646 if(This->is_doc_channel && !is_supported_doc_mime(status_text)) {
1647 FIXME("External MIME: %s\n", debugstr_w(status_text));
1649 handle_extern_mime_navigation(This);
1651 This->nschannel = NULL;
1654 if(!This->nschannel)
1655 return S_OK;
1657 heap_free(This->nschannel->content_type);
1658 This->nschannel->content_type = heap_strdupWtoA(status_text);
1659 break;
1660 case BINDSTATUS_REDIRECTING:
1661 return handle_redirect(This, status_text);
1662 case BINDSTATUS_BEGINDOWNLOADDATA: {
1663 IWinInetHttpInfo *http_info;
1664 DWORD status, size = sizeof(DWORD);
1665 HRESULT hres;
1667 if(!This->bsc.binding)
1668 break;
1670 hres = IBinding_QueryInterface(This->bsc.binding, &IID_IWinInetHttpInfo, (void**)&http_info);
1671 if(FAILED(hres))
1672 break;
1674 hres = IWinInetHttpInfo_QueryInfo(http_info,
1675 HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL, NULL);
1676 IWinInetHttpInfo_Release(http_info);
1677 if(FAILED(hres) || status == HTTP_STATUS_OK)
1678 break;
1680 handle_navigation_error(This, status);
1684 return S_OK;
1687 static HRESULT process_response_status_text(const WCHAR *header, const WCHAR *header_end, char **status_text)
1689 header = strchrW(header + 1, ' ');
1690 if(!header || header >= header_end)
1691 return E_FAIL;
1692 header = strchrW(header + 1, ' ');
1693 if(!header || header >= header_end)
1694 return E_FAIL;
1695 ++header;
1697 *status_text = heap_strndupWtoU(header, header_end - header);
1699 if(!*status_text)
1700 return E_OUTOFMEMORY;
1702 return S_OK;
1705 static HRESULT nsChannelBSC_on_response(BSCallback *bsc, DWORD response_code,
1706 LPCWSTR response_headers)
1708 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1709 char *str;
1710 HRESULT hres;
1712 This->response_processed = TRUE;
1713 This->nschannel->response_status = response_code;
1715 if(response_headers) {
1716 const WCHAR *headers;
1718 headers = strchrW(response_headers, '\r');
1719 hres = process_response_status_text(response_headers, headers, &str);
1720 if(FAILED(hres)) {
1721 WARN("parsing headers failed: %08x\n", hres);
1722 return hres;
1725 heap_free(This->nschannel->response_status_text);
1726 This->nschannel->response_status_text = str;
1728 if(headers && headers[1] == '\n') {
1729 headers += 2;
1730 hres = process_response_headers(This, headers);
1731 if(FAILED(hres)) {
1732 WARN("parsing headers failed: %08x\n", hres);
1733 return hres;
1738 return S_OK;
1741 static HRESULT nsChannelBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
1743 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1744 http_header_t *iter;
1745 DWORD len = 0;
1746 WCHAR *ptr;
1748 static const WCHAR content_lengthW[] =
1749 {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0};
1751 if(!This->nschannel)
1752 return S_FALSE;
1754 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->request_headers, http_header_t, entry) {
1755 if(strcmpW(iter->header, content_lengthW))
1756 len += strlenW(iter->header) + 2 /* ": " */ + strlenW(iter->data) + 2 /* "\r\n" */;
1759 if(!len)
1760 return S_OK;
1762 *additional_headers = ptr = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
1763 if(!ptr)
1764 return E_OUTOFMEMORY;
1766 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->request_headers, http_header_t, entry) {
1767 if(!strcmpW(iter->header, content_lengthW))
1768 continue;
1770 len = strlenW(iter->header);
1771 memcpy(ptr, iter->header, len*sizeof(WCHAR));
1772 ptr += len;
1774 *ptr++ = ':';
1775 *ptr++ = ' ';
1777 len = strlenW(iter->data);
1778 memcpy(ptr, iter->data, len*sizeof(WCHAR));
1779 ptr += len;
1781 *ptr++ = '\r';
1782 *ptr++ = '\n';
1785 *ptr = 0;
1787 return S_OK;
1790 static const BSCallbackVtbl nsChannelBSCVtbl = {
1791 nsChannelBSC_destroy,
1792 nsChannelBSC_init_bindinfo,
1793 nsChannelBSC_start_binding,
1794 nsChannelBSC_stop_binding,
1795 nsChannelBSC_read_data,
1796 nsChannelBSC_on_progress,
1797 nsChannelBSC_on_response,
1798 nsChannelBSC_beginning_transaction
1801 HRESULT create_channelbsc(IMoniker *mon, const WCHAR *headers, BYTE *post_data, DWORD post_data_size,
1802 BOOL is_doc_binding, nsChannelBSC **retval)
1804 nsChannelBSC *ret;
1805 DWORD bindf;
1807 ret = heap_alloc_zero(sizeof(*ret));
1808 if(!ret)
1809 return E_OUTOFMEMORY;
1811 bindf = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA;
1812 if(post_data_size)
1813 bindf |= BINDF_FORMS_SUBMIT | BINDF_PRAGMA_NO_CACHE | BINDF_HYPERLINK | BINDF_GETNEWESTVERSION;
1815 init_bscallback(&ret->bsc, &nsChannelBSCVtbl, mon, bindf);
1816 ret->is_doc_channel = is_doc_binding;
1818 if(headers) {
1819 ret->bsc.request_data.headers = heap_strdupW(headers);
1820 if(!ret->bsc.request_data.headers) {
1821 IBindStatusCallback_Release(&ret->bsc.IBindStatusCallback_iface);
1822 return E_OUTOFMEMORY;
1826 if(post_data) {
1827 ret->bsc.request_data.post_data = GlobalAlloc(0, post_data_size+1);
1828 if(!ret->bsc.request_data.post_data) {
1829 release_request_data(&ret->bsc.request_data);
1830 IBindStatusCallback_Release(&ret->bsc.IBindStatusCallback_iface);
1831 return E_OUTOFMEMORY;
1834 memcpy(ret->bsc.request_data.post_data, post_data, post_data_size);
1835 ((BYTE*)ret->bsc.request_data.post_data)[post_data_size] = 0;
1836 ret->bsc.request_data.post_data_len = post_data_size;
1839 TRACE("created %p\n", ret);
1840 *retval = ret;
1841 return S_OK;
1844 typedef struct {
1845 task_t header;
1846 HTMLOuterWindow *window;
1847 HTMLInnerWindow *pending_window;
1848 } start_doc_binding_task_t;
1850 static void start_doc_binding_proc(task_t *_task)
1852 start_doc_binding_task_t *task = (start_doc_binding_task_t*)_task;
1854 set_current_mon(task->window, task->pending_window->bscallback->bsc.mon, BINDING_NAVIGATED);
1855 start_binding(task->pending_window, &task->pending_window->bscallback->bsc, NULL);
1858 static void start_doc_binding_task_destr(task_t *_task)
1860 start_doc_binding_task_t *task = (start_doc_binding_task_t*)_task;
1862 IHTMLWindow2_Release(&task->pending_window->base.IHTMLWindow2_iface);
1863 heap_free(task);
1866 HRESULT async_start_doc_binding(HTMLOuterWindow *window, HTMLInnerWindow *pending_window)
1868 start_doc_binding_task_t *task;
1870 TRACE("%p\n", pending_window);
1872 task = heap_alloc(sizeof(start_doc_binding_task_t));
1873 if(!task)
1874 return E_OUTOFMEMORY;
1876 task->window = window;
1877 task->pending_window = pending_window;
1878 IHTMLWindow2_AddRef(&pending_window->base.IHTMLWindow2_iface);
1880 return push_task(&task->header, start_doc_binding_proc, start_doc_binding_task_destr, pending_window->task_magic);
1883 void abort_window_bindings(HTMLInnerWindow *window)
1885 BSCallback *iter;
1887 remove_target_tasks(window->task_magic);
1889 while(!list_empty(&window->bindings)) {
1890 iter = LIST_ENTRY(window->bindings.next, BSCallback, entry);
1892 TRACE("Aborting %p\n", iter);
1894 IBindStatusCallback_AddRef(&iter->IBindStatusCallback_iface);
1896 if(iter->binding)
1897 IBinding_Abort(iter->binding);
1898 else
1899 iter->vtbl->stop_binding(iter, E_ABORT);
1901 iter->window = NULL;
1902 list_remove(&iter->entry);
1903 list_init(&iter->entry);
1905 IBindStatusCallback_Release(&iter->IBindStatusCallback_iface);
1908 if(window->bscallback) {
1909 IBindStatusCallback_Release(&window->bscallback->bsc.IBindStatusCallback_iface);
1910 window->bscallback = NULL;
1913 if(window->mon) {
1914 IMoniker_Release(window->mon);
1915 window->mon = NULL;
1919 HRESULT channelbsc_load_stream(HTMLInnerWindow *pending_window, IMoniker *mon, IStream *stream)
1921 nsChannelBSC *bscallback = pending_window->bscallback;
1922 HRESULT hres = S_OK;
1924 if(!bscallback->nschannel) {
1925 ERR("NULL nschannel\n");
1926 return E_FAIL;
1929 bscallback->nschannel->content_type = heap_strdupA("text/html");
1930 if(!bscallback->nschannel->content_type)
1931 return E_OUTOFMEMORY;
1933 set_current_mon(pending_window->base.outer_window, mon, 0);
1935 bscallback->bsc.window = pending_window;
1936 if(stream)
1937 hres = read_stream_data(bscallback, stream);
1938 if(SUCCEEDED(hres))
1939 hres = async_stop_request(bscallback);
1940 if(FAILED(hres))
1941 IBindStatusCallback_OnStopBinding(&bscallback->bsc.IBindStatusCallback_iface, hres, NULL);
1943 return hres;
1946 void channelbsc_set_channel(nsChannelBSC *This, nsChannel *channel, nsIStreamListener *listener, nsISupports *context)
1948 nsIHttpChannel_AddRef(&channel->nsIHttpChannel_iface);
1949 This->nschannel = channel;
1951 nsIStreamListener_AddRef(listener);
1952 This->nslistener = listener;
1954 if(context) {
1955 nsISupports_AddRef(context);
1956 This->nscontext = context;
1959 if(This->bsc.request_data.headers) {
1960 HRESULT hres;
1962 hres = parse_headers(This->bsc.request_data.headers, &channel->request_headers);
1963 heap_free(This->bsc.request_data.headers);
1964 This->bsc.request_data.headers = NULL;
1965 if(FAILED(hres))
1966 WARN("parse_headers failed: %08x\n", hres);
1970 typedef struct {
1971 task_t header;
1972 HTMLOuterWindow *window;
1973 IUri *uri;
1974 } navigate_javascript_task_t;
1976 static void navigate_javascript_proc(task_t *_task)
1978 navigate_javascript_task_t *task = (navigate_javascript_task_t*)_task;
1979 HTMLOuterWindow *window = task->window;
1980 VARIANT v;
1981 BSTR code;
1982 HRESULT hres;
1984 static const WCHAR jscriptW[] = {'j','s','c','r','i','p','t',0};
1986 task->window->readystate = READYSTATE_COMPLETE;
1988 hres = IUri_GetPath(task->uri, &code);
1989 if(FAILED(hres))
1990 return;
1992 hres = UrlUnescapeW(code, NULL, NULL, URL_UNESCAPE_INPLACE);
1993 if(FAILED(hres)) {
1994 SysFreeString(code);
1995 return;
1998 set_download_state(window->doc_obj, 1);
2000 V_VT(&v) = VT_EMPTY;
2001 hres = exec_script(window->base.inner_window, code, jscriptW, &v);
2002 SysFreeString(code);
2003 if(SUCCEEDED(hres) && V_VT(&v) != VT_EMPTY) {
2004 FIXME("javascirpt URL returned %s\n", debugstr_variant(&v));
2005 VariantClear(&v);
2008 if(window->doc_obj->view_sink)
2009 IAdviseSink_OnViewChange(window->doc_obj->view_sink, DVASPECT_CONTENT, -1);
2011 set_download_state(window->doc_obj, 0);
2014 static void navigate_javascript_task_destr(task_t *_task)
2016 navigate_javascript_task_t *task = (navigate_javascript_task_t*)_task;
2018 IUri_Release(task->uri);
2019 heap_free(task);
2022 typedef struct {
2023 task_t header;
2024 HTMLOuterWindow *window;
2025 nsChannelBSC *bscallback;
2026 DWORD flags;
2027 IMoniker *mon;
2028 IUri *uri;
2029 } navigate_task_t;
2031 static void navigate_proc(task_t *_task)
2033 navigate_task_t *task = (navigate_task_t*)_task;
2034 HRESULT hres;
2036 hres = set_moniker(task->window, task->mon, task->uri, NULL, task->bscallback, TRUE);
2037 if(SUCCEEDED(hres)) {
2038 set_current_mon(task->window, task->bscallback->bsc.mon, task->flags);
2039 set_current_uri(task->window, task->uri);
2040 start_binding(task->window->pending_window, &task->bscallback->bsc, NULL);
2044 static void navigate_task_destr(task_t *_task)
2046 navigate_task_t *task = (navigate_task_t*)_task;
2048 IBindStatusCallback_Release(&task->bscallback->bsc.IBindStatusCallback_iface);
2049 IMoniker_Release(task->mon);
2050 IUri_Release(task->uri);
2051 heap_free(task);
2054 static HRESULT navigate_fragment(HTMLOuterWindow *window, IUri *uri)
2056 nsIDOMLocation *nslocation;
2057 nsAString nsfrag_str;
2058 WCHAR *selector;
2059 BSTR frag;
2060 nsresult nsres;
2061 HRESULT hres;
2063 const WCHAR selector_formatW[] = {'a','[','i','d','=','"','%','s','"',']',0};
2065 set_current_uri(window, uri);
2067 nsres = nsIDOMWindow_GetLocation(window->nswindow, &nslocation);
2068 if(FAILED(nsres) || !nslocation)
2069 return E_FAIL;
2071 hres = IUri_GetFragment(uri, &frag);
2072 if(FAILED(hres)) {
2073 nsIDOMLocation_Release(nslocation);
2074 return hres;
2077 nsAString_InitDepend(&nsfrag_str, frag);
2078 nsres = nsIDOMLocation_SetHash(nslocation, &nsfrag_str);
2079 nsAString_Finish(&nsfrag_str);
2080 nsIDOMLocation_Release(nslocation);
2081 if(NS_FAILED(nsres))
2082 ERR("SetHash failed: %08x\n", nsres);
2085 * IE supports scrolling to anchor elements with "#hash" ids (note that '#' is part of the id),
2086 * while Gecko scrolls only to elements with "hash" ids. We scroll the page ourselves if
2087 * a[id="#hash"] element can be found.
2089 selector = heap_alloc(sizeof(selector_formatW)+SysStringLen(frag)*sizeof(WCHAR));
2090 if(selector) {
2091 nsIDOMElement *nselem = NULL;
2092 nsAString selector_str;
2094 sprintfW(selector, selector_formatW, frag);
2095 nsAString_InitDepend(&selector_str, selector);
2096 /* NOTE: Gecko doesn't set result to NULL if there is no match, so nselem must be initialized */
2097 nsres = nsIDOMHTMLDocument_QuerySelector(window->base.inner_window->doc->nsdoc, &selector_str, &nselem);
2098 nsAString_Finish(&selector_str);
2099 heap_free(selector);
2100 if(NS_SUCCEEDED(nsres) && nselem) {
2101 nsIDOMHTMLElement *html_elem;
2103 nsres = nsIDOMElement_QueryInterface(nselem, &IID_nsIDOMHTMLElement, (void**)&html_elem);
2104 nsIDOMElement_Release(nselem);
2105 if(NS_SUCCEEDED(nsres)) {
2106 nsIDOMHTMLElement_ScrollIntoView(html_elem, TRUE, 1);
2107 nsIDOMHTMLElement_Release(html_elem);
2112 SysFreeString(frag);
2114 if(window->doc_obj->doc_object_service) {
2115 IDocObjectService_FireNavigateComplete2(window->doc_obj->doc_object_service, &window->base.IHTMLWindow2_iface, 0x10);
2116 IDocObjectService_FireDocumentComplete(window->doc_obj->doc_object_service, &window->base.IHTMLWindow2_iface, 0);
2120 return S_OK;
2123 HRESULT super_navigate(HTMLOuterWindow *window, IUri *uri, DWORD flags, const WCHAR *headers, BYTE *post_data, DWORD post_data_size)
2125 nsChannelBSC *bsc;
2126 IUri *uri_nofrag;
2127 IMoniker *mon;
2128 DWORD scheme;
2129 HRESULT hres;
2131 uri_nofrag = get_uri_nofrag(uri);
2132 if(!uri_nofrag)
2133 return E_FAIL;
2135 if(window->doc_obj->client && !(flags & BINDING_REFRESH)) {
2136 IOleCommandTarget *cmdtrg;
2138 hres = IOleClientSite_QueryInterface(window->doc_obj->client, &IID_IOleCommandTarget, (void**)&cmdtrg);
2139 if(SUCCEEDED(hres)) {
2140 VARIANT in, out;
2141 BSTR url_str;
2143 hres = IUri_GetDisplayUri(uri_nofrag, &url_str);
2144 if(SUCCEEDED(hres)) {
2145 V_VT(&in) = VT_BSTR;
2146 V_BSTR(&in) = url_str;
2147 V_VT(&out) = VT_BOOL;
2148 V_BOOL(&out) = VARIANT_TRUE;
2149 hres = IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 67, 0, &in, &out);
2150 IOleCommandTarget_Release(cmdtrg);
2151 if(SUCCEEDED(hres))
2152 VariantClear(&out);
2153 SysFreeString(url_str);
2158 if(!(flags & BINDING_NOFRAG) && window->uri_nofrag && !post_data_size) {
2159 BOOL eq;
2161 hres = IUri_IsEqual(uri_nofrag, window->uri_nofrag, &eq);
2162 if(SUCCEEDED(hres) && eq) {
2163 IUri_Release(uri_nofrag);
2164 TRACE("fragment navigate\n");
2165 return navigate_fragment(window, uri);
2169 hres = CreateURLMonikerEx2(NULL, uri_nofrag, &mon, URL_MK_UNIFORM);
2170 IUri_Release(uri_nofrag);
2171 if(FAILED(hres))
2172 return hres;
2174 /* FIXME: Why not set_ready_state? */
2175 window->readystate = READYSTATE_UNINITIALIZED;
2177 hres = create_channelbsc(mon, headers, post_data, post_data_size, TRUE, &bsc);
2178 if(FAILED(hres)) {
2179 IMoniker_Release(mon);
2180 return hres;
2183 prepare_for_binding(&window->doc_obj->basedoc, mon, flags);
2185 hres = IUri_GetScheme(uri, &scheme);
2186 if(SUCCEEDED(hres) && scheme == URL_SCHEME_JAVASCRIPT) {
2187 navigate_javascript_task_t *task;
2189 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
2190 IMoniker_Release(mon);
2192 task = heap_alloc(sizeof(*task));
2193 if(!task)
2194 return E_OUTOFMEMORY;
2196 /* Why silently? */
2197 window->readystate = READYSTATE_COMPLETE;
2198 if(!(flags & BINDING_FROMHIST))
2199 call_docview_84(window->doc_obj);
2201 IUri_AddRef(uri);
2202 task->window = window;
2203 task->uri = uri;
2204 hres = push_task(&task->header, navigate_javascript_proc, navigate_javascript_task_destr, window->task_magic);
2205 }else if(flags & BINDING_SUBMIT) {
2206 hres = set_moniker(window, mon, uri, NULL, bsc, TRUE);
2207 if(SUCCEEDED(hres))
2208 hres = start_binding(window->pending_window, &bsc->bsc, NULL);
2209 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
2210 IMoniker_Release(mon);
2211 }else {
2212 navigate_task_t *task;
2214 task = heap_alloc(sizeof(*task));
2215 if(!task) {
2216 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
2217 IMoniker_Release(mon);
2218 return E_OUTOFMEMORY;
2221 /* Silently and repeated when real loading starts? */
2222 window->readystate = READYSTATE_LOADING;
2223 if(!(flags & (BINDING_FROMHIST|BINDING_REFRESH)))
2224 call_docview_84(window->doc_obj);
2226 task->window = window;
2227 task->bscallback = bsc;
2228 task->flags = flags;
2229 task->mon = mon;
2231 IUri_AddRef(uri);
2232 task->uri = uri;
2233 hres = push_task(&task->header, navigate_proc, navigate_task_destr, window->task_magic);
2236 return hres;
2239 HRESULT navigate_new_window(HTMLOuterWindow *window, IUri *uri, const WCHAR *name, request_data_t *request_data, IHTMLWindow2 **ret)
2241 INewWindowManager *new_window_mgr;
2242 BSTR display_uri, context_url;
2243 IWebBrowser2 *web_browser;
2244 IHTMLWindow2 *new_window;
2245 IBindCtx *bind_ctx;
2246 nsChannelBSC *bsc;
2247 HRESULT hres;
2249 if (window->doc_obj->client) {
2250 hres = do_query_service((IUnknown*)window->doc_obj->client, &SID_SNewWindowManager,
2251 &IID_INewWindowManager, (void**)&new_window_mgr);
2252 if (FAILED(hres)) {
2253 FIXME("No INewWindowManager\n");
2254 return hres;
2257 hres = IUri_GetDisplayUri(window->uri_nofrag, &context_url);
2258 if(FAILED(hres))
2259 return hres;
2261 hres = IUri_GetDisplayUri(uri, &display_uri);
2262 if(FAILED(hres)) {
2263 SysFreeString(context_url);
2264 return hres;
2267 hres = INewWindowManager_EvaluateNewWindow(new_window_mgr, display_uri, name, context_url,
2268 NULL, FALSE, window->doc_obj->has_popup ? 0 : NWMF_FIRST, 0);
2269 window->doc_obj->has_popup = TRUE;
2270 SysFreeString(display_uri);
2271 SysFreeString(context_url);
2272 INewWindowManager_Release(new_window_mgr);
2273 if(FAILED(hres)) {
2274 if(ret)
2275 *ret = NULL;
2276 return S_OK;
2281 if(request_data)
2282 hres = create_channelbsc(NULL, request_data->headers,
2283 request_data->post_data, request_data->post_data_len, FALSE,
2284 &bsc);
2285 else
2286 hres = create_channelbsc(NULL, NULL, NULL, 0, FALSE, &bsc);
2287 if(FAILED(hres))
2288 return hres;
2290 hres = CreateAsyncBindCtx(0, &bsc->bsc.IBindStatusCallback_iface, NULL, &bind_ctx);
2291 if(FAILED(hres)) {
2292 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
2293 return hres;
2296 hres = CoCreateInstance(&CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER,
2297 &IID_IWebBrowser2, (void**)&web_browser);
2298 if(SUCCEEDED(hres)) {
2299 ITargetFramePriv2 *target_frame_priv;
2301 hres = IWebBrowser2_QueryInterface(web_browser, &IID_ITargetFramePriv2, (void**)&target_frame_priv);
2302 if(SUCCEEDED(hres)) {
2303 hres = ITargetFramePriv2_AggregatedNavigation2(target_frame_priv,
2304 HLNF_DISABLEWINDOWRESTRICTIONS|HLNF_OPENINNEWWINDOW, bind_ctx, &bsc->bsc.IBindStatusCallback_iface,
2305 name, uri, emptyW);
2306 ITargetFramePriv2_Release(target_frame_priv);
2308 if(SUCCEEDED(hres))
2309 hres = do_query_service((IUnknown*)web_browser, &SID_SHTMLWindow, &IID_IHTMLWindow2, (void**)&new_window);
2311 if(FAILED(hres)) {
2312 IWebBrowser2_Quit(web_browser);
2313 IWebBrowser2_Release(web_browser);
2315 }else {
2316 WARN("Could not create InternetExplorer instance: %08x\n", hres);
2319 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
2320 IBindCtx_Release(bind_ctx);
2321 if(FAILED(hres))
2322 return hres;
2324 IWebBrowser2_put_Visible(web_browser, VARIANT_TRUE);
2325 IWebBrowser2_Release(web_browser);
2327 if(ret)
2328 *ret = new_window;
2329 else
2330 IHTMLWindow2_Release(new_window);
2331 return S_OK;
2334 HRESULT hlink_frame_navigate(HTMLDocument *doc, LPCWSTR url, nsChannel *nschannel, DWORD hlnf, BOOL *cancel)
2336 IHlinkFrame *hlink_frame;
2337 nsChannelBSC *callback;
2338 IBindCtx *bindctx;
2339 IMoniker *mon;
2340 IHlink *hlink;
2341 HRESULT hres;
2343 *cancel = FALSE;
2345 hres = do_query_service((IUnknown*)doc->doc_obj->client, &IID_IHlinkFrame, &IID_IHlinkFrame,
2346 (void**)&hlink_frame);
2347 if(FAILED(hres))
2348 return S_OK;
2350 hres = create_channelbsc(NULL, NULL, NULL, 0, FALSE, &callback);
2351 if(FAILED(hres)) {
2352 IHlinkFrame_Release(hlink_frame);
2353 return hres;
2356 if(nschannel)
2357 read_post_data_stream(nschannel->post_data_stream, nschannel->post_data_contains_headers,
2358 &nschannel->request_headers, &callback->bsc.request_data);
2360 hres = CreateAsyncBindCtx(0, &callback->bsc.IBindStatusCallback_iface, NULL, &bindctx);
2361 if(SUCCEEDED(hres))
2362 hres = CoCreateInstance(&CLSID_StdHlink, NULL, CLSCTX_INPROC_SERVER,
2363 &IID_IHlink, (LPVOID*)&hlink);
2365 if(SUCCEEDED(hres))
2366 hres = CreateURLMoniker(NULL, url, &mon);
2368 if(SUCCEEDED(hres)) {
2369 IHlink_SetMonikerReference(hlink, HLINKSETF_TARGET, mon, NULL);
2371 if(hlnf & HLNF_OPENINNEWWINDOW) {
2372 static const WCHAR wszBlank[] = {'_','b','l','a','n','k',0};
2373 IHlink_SetTargetFrameName(hlink, wszBlank); /* FIXME */
2376 hres = IHlinkFrame_Navigate(hlink_frame, hlnf, bindctx,
2377 &callback->bsc.IBindStatusCallback_iface, hlink);
2378 IMoniker_Release(mon);
2379 *cancel = hres == S_OK;
2380 hres = S_OK;
2383 IHlinkFrame_Release(hlink_frame);
2384 IBindCtx_Release(bindctx);
2385 IBindStatusCallback_Release(&callback->bsc.IBindStatusCallback_iface);
2386 return hres;
2389 static HRESULT navigate_uri(HTMLOuterWindow *window, IUri *uri, const WCHAR *display_uri, const request_data_t *request_data,
2390 DWORD flags)
2392 nsWineURI *nsuri;
2393 HRESULT hres;
2395 TRACE("%s\n", debugstr_w(display_uri));
2397 if(window->doc_obj && window->doc_obj->webbrowser) {
2398 DWORD post_data_len = request_data ? request_data->post_data_len : 0;
2399 void *post_data = post_data_len ? request_data->post_data : NULL;
2400 const WCHAR *headers = request_data ? request_data->headers : NULL;
2402 if(!(flags & BINDING_REFRESH)) {
2403 BSTR frame_name = NULL;
2404 BOOL cancel = FALSE;
2406 if(window != window->doc_obj->basedoc.window) {
2407 hres = IHTMLWindow2_get_name(&window->base.IHTMLWindow2_iface, &frame_name);
2408 if(FAILED(hres))
2409 return hres;
2412 hres = IDocObjectService_FireBeforeNavigate2(window->doc_obj->doc_object_service, NULL, display_uri, 0x40,
2413 frame_name, post_data, post_data_len ? post_data_len+1 : 0, headers, TRUE, &cancel);
2414 SysFreeString(frame_name);
2415 if(SUCCEEDED(hres) && cancel) {
2416 TRACE("Navigation canceled\n");
2417 return S_OK;
2421 if(window == window->doc_obj->basedoc.window)
2422 return super_navigate(window, uri, flags, headers, post_data, post_data_len);
2425 if(window->doc_obj && window == window->doc_obj->basedoc.window) {
2426 BOOL cancel;
2428 hres = hlink_frame_navigate(&window->base.inner_window->doc->basedoc, display_uri, NULL, 0, &cancel);
2429 if(FAILED(hres))
2430 return hres;
2432 if(cancel) {
2433 TRACE("Navigation handled by hlink frame\n");
2434 return S_OK;
2438 hres = create_doc_uri(window, uri, &nsuri);
2439 if(FAILED(hres))
2440 return hres;
2442 hres = load_nsuri(window, nsuri, request_data ? request_data->post_stream : NULL, NULL, LOAD_FLAGS_NONE);
2443 nsISupports_Release((nsISupports*)nsuri);
2444 return hres;
2447 HRESULT load_uri(HTMLOuterWindow *window, IUri *uri, DWORD flags)
2449 BSTR display_uri;
2450 HRESULT hres;
2452 hres = IUri_GetDisplayUri(uri, &display_uri);
2453 if(FAILED(hres))
2454 return hres;
2456 hres = navigate_uri(window, uri, display_uri, NULL, flags);
2457 SysFreeString(display_uri);
2458 return hres;
2461 static HRESULT translate_uri(HTMLOuterWindow *window, IUri *orig_uri, BSTR *ret_display_uri, IUri **ret_uri)
2463 IUri *uri = NULL;
2464 BSTR display_uri;
2465 HRESULT hres;
2467 hres = IUri_GetDisplayUri(orig_uri, &display_uri);
2468 if(FAILED(hres))
2469 return hres;
2471 if(window->doc_obj && window->doc_obj->hostui) {
2472 OLECHAR *translated_url = NULL;
2474 hres = IDocHostUIHandler_TranslateUrl(window->doc_obj->hostui, 0, display_uri,
2475 &translated_url);
2476 if(hres == S_OK && translated_url) {
2477 TRACE("%08x %s -> %s\n", hres, debugstr_w(display_uri), debugstr_w(translated_url));
2478 SysFreeString(display_uri);
2479 hres = create_uri(translated_url, 0, &uri);
2480 CoTaskMemFree(translated_url);
2481 if(FAILED(hres))
2482 return hres;
2484 hres = IUri_GetDisplayUri(uri, &display_uri);
2485 if(FAILED(hres)) {
2486 IUri_Release(uri);
2487 return hres;
2492 if(!uri) {
2493 IUri_AddRef(orig_uri);
2494 uri = orig_uri;
2497 *ret_display_uri = display_uri;
2498 *ret_uri = uri;
2499 return S_OK;
2502 HRESULT submit_form(HTMLOuterWindow *window, const WCHAR *target, IUri *submit_uri, nsIInputStream *post_stream)
2504 request_data_t request_data = {NULL};
2505 HRESULT hres;
2507 hres = read_post_data_stream(post_stream, TRUE, NULL, &request_data);
2508 if(FAILED(hres))
2509 return hres;
2511 if(window) {
2512 IUri *uri;
2513 BSTR display_uri;
2515 window->readystate_locked++;
2517 hres = translate_uri(window, submit_uri, &display_uri, &uri);
2518 if(SUCCEEDED(hres)) {
2519 hres = navigate_uri(window, uri, display_uri, &request_data, BINDING_NAVIGATED|BINDING_SUBMIT);
2520 IUri_Release(uri);
2521 SysFreeString(display_uri);
2524 window->readystate_locked--;
2525 }else
2526 hres = navigate_new_window(window, submit_uri, target, &request_data, NULL);
2528 release_request_data(&request_data);
2529 return hres;
2532 HRESULT navigate_url(HTMLOuterWindow *window, const WCHAR *new_url, IUri *base_uri, DWORD flags)
2534 IUri *uri, *nav_uri;
2535 BSTR display_uri;
2536 HRESULT hres;
2538 if(new_url && base_uri)
2539 hres = CoInternetCombineUrlEx(base_uri, new_url, URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO,
2540 &nav_uri, 0);
2541 else
2542 hres = create_uri(new_url, 0, &nav_uri);
2543 if(FAILED(hres))
2544 return hres;
2546 hres = translate_uri(window, nav_uri, &display_uri, &uri);
2547 IUri_Release(nav_uri);
2548 if(FAILED(hres))
2549 return hres;
2551 hres = navigate_uri(window, uri, display_uri, NULL, flags);
2552 IUri_Release(uri);
2553 SysFreeString(display_uri);
2554 return hres;