mshtml: Release owner in nsChannel destructor.
[wine/wine64.git] / dlls / mshtml / nsio.c
blobc918d12ff72bdc4c564169df982638cc1877c193
1 /*
2 * Copyright 2006-2007 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
21 #include <stdarg.h>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "ole2.h"
30 #include "shlguid.h"
31 #include "wininet.h"
32 #include "shlwapi.h"
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
37 #include "mshtml_private.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
41 #define LOAD_INITIAL_DOCUMENT_URI 0x80000
43 #define NS_IOSERVICE_CLASSNAME "nsIOService"
44 #define NS_IOSERVICE_CONTRACTID "@mozilla.org/network/io-service;1"
46 static const IID NS_IOSERVICE_CID =
47 {0x9ac9e770, 0x18bc, 0x11d3, {0x93, 0x37, 0x00, 0x10, 0x4b, 0xa0, 0xfd, 0x40}};
49 static nsIIOService *nsio = NULL;
50 static nsINetUtil *net_util;
52 static const WCHAR about_blankW[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
54 typedef struct {
55 const nsIWineURIVtbl *lpWineURIVtbl;
57 LONG ref;
59 nsIURI *uri;
60 NSContainer *container;
61 LPWSTR wine_url;
62 PRBool is_doc_uri;
63 BOOL use_wine_url;
64 } nsURI;
66 #define NSURI(x) ((nsIURI*) &(x)->lpWineURIVtbl)
67 #define NSWINEURI(x) ((nsIWineURI*) &(x)->lpWineURIVtbl)
69 static nsresult create_uri(nsIURI*,NSContainer*,nsIWineURI**);
71 HRESULT nsuri_to_url(LPCWSTR nsuri, BSTR *ret)
73 const WCHAR *ptr = nsuri;
75 static const WCHAR wine_prefixW[] = {'w','i','n','e',':'};
77 if(!strncmpW(nsuri, wine_prefixW, sizeof(wine_prefixW)/sizeof(WCHAR)))
78 ptr += sizeof(wine_prefixW)/sizeof(WCHAR);
80 *ret = SysAllocString(ptr);
81 if(!*ret)
82 return E_OUTOFMEMORY;
84 TRACE("%s -> %s\n", debugstr_w(nsuri), debugstr_w(*ret));
85 return S_OK;
88 static BOOL exec_shldocvw_67(HTMLDocument *doc, LPCWSTR url)
90 IOleCommandTarget *cmdtrg = NULL;
91 HRESULT hres;
93 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget,
94 (void**)&cmdtrg);
95 if(SUCCEEDED(hres)) {
96 VARIANT varUrl, varRes;
98 V_VT(&varUrl) = VT_BSTR;
99 V_BSTR(&varUrl) = SysAllocString(url);
100 V_VT(&varRes) = VT_BOOL;
102 hres = IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 67, 0, &varUrl, &varRes);
104 IOleCommandTarget_Release(cmdtrg);
105 SysFreeString(V_BSTR(&varUrl));
107 if(SUCCEEDED(hres) && !V_BOOL(&varRes)) {
108 TRACE("got VARIANT_FALSE, do not load\n");
109 return FALSE;
113 return TRUE;
116 static BOOL before_async_open(nsChannel *channel, NSContainer *container)
118 IServiceProvider *service_provider;
119 HTMLDocument *doc = container->doc;
120 DWORD hlnf = 0;
121 LPCWSTR uri;
122 HRESULT hres;
124 nsIWineURI_GetWineURL(channel->uri, &uri);
125 if(!uri) {
126 ERR("GetWineURL returned NULL\n");
127 return TRUE;
130 if(!doc) {
131 NSContainer *container_iter = container;
133 hlnf = HLNF_OPENINNEWWINDOW;
134 while(!container_iter->doc)
135 container_iter = container_iter->parent;
136 doc = container_iter->doc;
139 if(!doc->client)
140 return TRUE;
142 if(!hlnf && !exec_shldocvw_67(doc, uri))
143 return FALSE;
145 hres = IOleClientSite_QueryInterface(doc->client, &IID_IServiceProvider,
146 (void**)&service_provider);
147 if(SUCCEEDED(hres)) {
148 IHlinkFrame *hlink_frame;
150 hres = IServiceProvider_QueryService(service_provider, &IID_IHlinkFrame,
151 &IID_IHlinkFrame, (void**)&hlink_frame);
152 IServiceProvider_Release(service_provider);
153 if(SUCCEEDED(hres)) {
154 hlink_frame_navigate(doc, hlink_frame, uri, channel->post_data_stream, hlnf);
155 IHlinkFrame_Release(hlink_frame);
157 return FALSE;
161 return TRUE;
164 #define NSCHANNEL_THIS(iface) DEFINE_THIS(nsChannel, HttpChannel, iface)
166 static nsresult NSAPI nsChannel_QueryInterface(nsIHttpChannel *iface, nsIIDRef riid, nsQIResult result)
168 nsChannel *This = NSCHANNEL_THIS(iface);
170 *result = NULL;
172 if(IsEqualGUID(&IID_nsISupports, riid)) {
173 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
174 *result = NSCHANNEL(This);
175 }else if(IsEqualGUID(&IID_nsIRequest, riid)) {
176 TRACE("(%p)->(IID_nsIRequest %p)\n", This, result);
177 *result = NSCHANNEL(This);
178 }else if(IsEqualGUID(&IID_nsIChannel, riid)) {
179 TRACE("(%p)->(IID_nsIChannel %p)\n", This, result);
180 *result = NSCHANNEL(This);
181 }else if(This->http_channel && IsEqualGUID(&IID_nsIHttpChannel, riid)) {
182 TRACE("(%p)->(IID_nsIHttpChannel %p)\n", This, result);
183 *result = NSHTTPCHANNEL(This);
184 }else if(IsEqualGUID(&IID_nsIUploadChannel, riid)) {
185 TRACE("(%p)->(IID_nsIUploadChannel %p)\n", This, result);
186 *result = NSUPCHANNEL(This);
189 if(*result) {
190 nsIChannel_AddRef(NSCHANNEL(This));
191 return NS_OK;
194 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
196 if(This->channel)
197 return nsIChannel_QueryInterface(This->channel, riid, result);
198 return NS_NOINTERFACE;
201 static nsrefcnt NSAPI nsChannel_AddRef(nsIHttpChannel *iface)
203 nsChannel *This = NSCHANNEL_THIS(iface);
204 nsrefcnt ref = InterlockedIncrement(&This->ref);
206 TRACE("(%p) ref=%d\n", This, ref);
208 return ref;
211 static nsrefcnt NSAPI nsChannel_Release(nsIHttpChannel *iface)
213 nsChannel *This = NSCHANNEL_THIS(iface);
214 LONG ref = InterlockedDecrement(&This->ref);
216 if(!ref) {
217 nsIWineURI_Release(This->uri);
218 if(This->channel)
219 nsIChannel_Release(This->channel);
220 if(This->http_channel)
221 nsIHttpChannel_Release(This->http_channel);
222 if(This->owner)
223 nsISupports_Release(This->owner);
224 if(This->post_data_stream)
225 nsIInputStream_Release(This->post_data_stream);
226 if(This->load_group)
227 nsILoadGroup_Release(This->load_group);
228 if(This->notif_callback)
229 nsIInterfaceRequestor_Release(This->notif_callback);
230 if(This->original_uri)
231 nsIURI_Release(This->original_uri);
232 heap_free(This->content_type);
233 heap_free(This->charset);
234 heap_free(This);
237 return ref;
240 static nsresult NSAPI nsChannel_GetName(nsIHttpChannel *iface, nsACString *aName)
242 nsChannel *This = NSCHANNEL_THIS(iface);
244 TRACE("(%p)->(%p)\n", This, aName);
246 if(This->channel)
247 return nsIChannel_GetName(This->channel, aName);
249 return NS_ERROR_NOT_IMPLEMENTED;
252 static nsresult NSAPI nsChannel_IsPending(nsIHttpChannel *iface, PRBool *_retval)
254 nsChannel *This = NSCHANNEL_THIS(iface);
256 TRACE("(%p)->(%p)\n", This, _retval);
258 if(This->channel)
259 return nsIChannel_IsPending(This->channel, _retval);
261 FIXME("default action not implemented\n");
262 return NS_ERROR_NOT_IMPLEMENTED;
265 static nsresult NSAPI nsChannel_GetStatus(nsIHttpChannel *iface, nsresult *aStatus)
267 nsChannel *This = NSCHANNEL_THIS(iface);
269 TRACE("(%p)->(%p)\n", This, aStatus);
271 if(This->channel)
272 return nsIChannel_GetStatus(This->channel, aStatus);
274 TRACE("returning NS_OK\n");
275 return *aStatus = NS_OK;
278 static nsresult NSAPI nsChannel_Cancel(nsIHttpChannel *iface, nsresult aStatus)
280 nsChannel *This = NSCHANNEL_THIS(iface);
282 TRACE("(%p)->(%08x)\n", This, aStatus);
284 if(This->channel)
285 return nsIChannel_Cancel(This->channel, aStatus);
287 FIXME("default action not implemented\n");
288 return NS_ERROR_NOT_IMPLEMENTED;
291 static nsresult NSAPI nsChannel_Suspend(nsIHttpChannel *iface)
293 nsChannel *This = NSCHANNEL_THIS(iface);
295 TRACE("(%p)\n", This);
297 if(This->channel)
298 return nsIChannel_Suspend(This->channel);
300 FIXME("default action not implemented\n");
301 return NS_ERROR_NOT_IMPLEMENTED;
304 static nsresult NSAPI nsChannel_Resume(nsIHttpChannel *iface)
306 nsChannel *This = NSCHANNEL_THIS(iface);
308 TRACE("(%p)\n", This);
310 if(This->channel)
311 return nsIChannel_Resume(This->channel);
313 FIXME("default action not implemented\n");
314 return NS_ERROR_NOT_IMPLEMENTED;
317 static nsresult NSAPI nsChannel_GetLoadGroup(nsIHttpChannel *iface, nsILoadGroup **aLoadGroup)
319 nsChannel *This = NSCHANNEL_THIS(iface);
321 TRACE("(%p)->(%p)\n", This, aLoadGroup);
323 if(This->load_group)
324 nsILoadGroup_AddRef(This->load_group);
326 *aLoadGroup = This->load_group;
327 return NS_OK;
330 static nsresult NSAPI nsChannel_SetLoadGroup(nsIHttpChannel *iface, nsILoadGroup *aLoadGroup)
332 nsChannel *This = NSCHANNEL_THIS(iface);
334 TRACE("(%p)->(%p)\n", This, aLoadGroup);
336 if(This->load_group)
337 nsILoadGroup_Release(This->load_group);
338 if(aLoadGroup)
339 nsILoadGroup_AddRef(aLoadGroup);
341 This->load_group = aLoadGroup;
343 if(This->channel)
344 return nsIChannel_SetLoadGroup(This->channel, aLoadGroup);
345 return NS_OK;
348 static nsresult NSAPI nsChannel_GetLoadFlags(nsIHttpChannel *iface, nsLoadFlags *aLoadFlags)
350 nsChannel *This = NSCHANNEL_THIS(iface);
352 TRACE("(%p)->(%p)\n", This, aLoadFlags);
354 *aLoadFlags = This->load_flags;
355 return NS_OK;
358 static nsresult NSAPI nsChannel_SetLoadFlags(nsIHttpChannel *iface, nsLoadFlags aLoadFlags)
360 nsChannel *This = NSCHANNEL_THIS(iface);
362 TRACE("(%p)->(%08x)\n", This, aLoadFlags);
364 This->load_flags = aLoadFlags;
366 if(This->channel)
367 return nsIChannel_SetLoadFlags(This->channel, aLoadFlags);
368 return NS_OK;
371 static nsresult NSAPI nsChannel_GetOriginalURI(nsIHttpChannel *iface, nsIURI **aOriginalURI)
373 nsChannel *This = NSCHANNEL_THIS(iface);
375 TRACE("(%p)->(%p)\n", This, aOriginalURI);
377 if(This->original_uri)
378 nsIURI_AddRef(This->original_uri);
380 *aOriginalURI = This->original_uri;
381 return NS_OK;
384 static nsresult NSAPI nsChannel_SetOriginalURI(nsIHttpChannel *iface, nsIURI *aOriginalURI)
386 nsChannel *This = NSCHANNEL_THIS(iface);
388 TRACE("(%p)->(%p)\n", This, aOriginalURI);
390 if(This->original_uri)
391 nsIURI_Release(This->original_uri);
393 nsIURI_AddRef(aOriginalURI);
394 This->original_uri = aOriginalURI;
396 if(This->channel)
397 return nsIChannel_SetOriginalURI(This->channel, aOriginalURI);
398 return NS_OK;
401 static nsresult NSAPI nsChannel_GetURI(nsIHttpChannel *iface, nsIURI **aURI)
403 nsChannel *This = NSCHANNEL_THIS(iface);
405 TRACE("(%p)->(%p)\n", This, aURI);
407 nsIWineURI_AddRef(This->uri);
408 *aURI = (nsIURI*)This->uri;
410 return NS_OK;
413 static nsresult NSAPI nsChannel_GetOwner(nsIHttpChannel *iface, nsISupports **aOwner)
415 nsChannel *This = NSCHANNEL_THIS(iface);
417 TRACE("(%p)->(%p)\n", This, aOwner);
419 if(This->channel)
420 return nsIChannel_GetOwner(This->channel, aOwner);
422 if(This->owner)
423 nsISupports_AddRef(This->owner);
424 *aOwner = This->owner;
426 return NS_OK;
429 static nsresult NSAPI nsChannel_SetOwner(nsIHttpChannel *iface, nsISupports *aOwner)
431 nsChannel *This = NSCHANNEL_THIS(iface);
433 TRACE("(%p)->(%p)\n", This, aOwner);
435 if(This->channel)
436 return nsIChannel_SetOwner(This->channel, aOwner);
438 if(aOwner)
439 nsISupports_AddRef(aOwner);
440 if(This->owner)
441 nsISupports_Release(This->owner);
442 This->owner = aOwner;
444 return NS_OK;
447 static nsresult NSAPI nsChannel_GetNotificationCallbacks(nsIHttpChannel *iface,
448 nsIInterfaceRequestor **aNotificationCallbacks)
450 nsChannel *This = NSCHANNEL_THIS(iface);
452 TRACE("(%p)->(%p)\n", This, aNotificationCallbacks);
454 if(This->notif_callback)
455 nsIInterfaceRequestor_AddRef(This->notif_callback);
456 *aNotificationCallbacks = This->notif_callback;
458 return NS_OK;
461 static nsresult NSAPI nsChannel_SetNotificationCallbacks(nsIHttpChannel *iface,
462 nsIInterfaceRequestor *aNotificationCallbacks)
464 nsChannel *This = NSCHANNEL_THIS(iface);
466 TRACE("(%p)->(%p)\n", This, aNotificationCallbacks);
468 if(This->notif_callback)
469 nsIInterfaceRequestor_Release(This->notif_callback);
470 if(aNotificationCallbacks)
471 nsIInterfaceRequestor_AddRef(aNotificationCallbacks);
473 This->notif_callback = aNotificationCallbacks;
475 if(This->channel)
476 return nsIChannel_SetNotificationCallbacks(This->channel, aNotificationCallbacks);
477 return NS_OK;
480 static nsresult NSAPI nsChannel_GetSecurityInfo(nsIHttpChannel *iface, nsISupports **aSecurityInfo)
482 nsChannel *This = NSCHANNEL_THIS(iface);
484 TRACE("(%p)->(%p)\n", This, aSecurityInfo);
486 if(This->channel)
487 return nsIChannel_GetSecurityInfo(This->channel, aSecurityInfo);
489 FIXME("default action not implemented\n");
490 return NS_ERROR_NOT_IMPLEMENTED;
493 static nsresult NSAPI nsChannel_GetContentType(nsIHttpChannel *iface, nsACString *aContentType)
495 nsChannel *This = NSCHANNEL_THIS(iface);
497 TRACE("(%p)->(%p)\n", This, aContentType);
499 if(This->content_type) {
500 nsACString_SetData(aContentType, This->content_type);
501 return S_OK;
504 if(This->channel)
505 return nsIChannel_GetContentType(This->channel, aContentType);
507 TRACE("returning default text/html\n");
508 nsACString_SetData(aContentType, "text/html");
509 return NS_OK;
512 static nsresult NSAPI nsChannel_SetContentType(nsIHttpChannel *iface,
513 const nsACString *aContentType)
515 nsChannel *This = NSCHANNEL_THIS(iface);
516 const char *content_type;
518 TRACE("(%p)->(%p)\n", This, aContentType);
520 nsACString_GetData(aContentType, &content_type);
522 TRACE("content_type %s\n", content_type);
524 heap_free(This->content_type);
525 This->content_type = heap_strdupA(content_type);
527 if(This->channel)
528 return nsIChannel_SetContentType(This->channel, aContentType);
530 return NS_OK;
533 static nsresult NSAPI nsChannel_GetContentCharset(nsIHttpChannel *iface,
534 nsACString *aContentCharset)
536 nsChannel *This = NSCHANNEL_THIS(iface);
538 TRACE("(%p)->(%p)\n", This, aContentCharset);
540 if(This->charset) {
541 nsACString_SetData(aContentCharset, This->charset);
542 return NS_OK;
545 if(This->channel) {
546 nsresult nsres = nsIChannel_GetContentCharset(This->channel, aContentCharset);
547 const char *ch;
548 nsACString_GetData(aContentCharset, &ch);
549 return nsres;
552 nsACString_SetData(aContentCharset, "");
553 return NS_OK;
556 static nsresult NSAPI nsChannel_SetContentCharset(nsIHttpChannel *iface,
557 const nsACString *aContentCharset)
559 nsChannel *This = NSCHANNEL_THIS(iface);
561 TRACE("(%p)->(%p)\n", This, aContentCharset);
563 if(This->channel)
564 return nsIChannel_SetContentCharset(This->channel, aContentCharset);
566 FIXME("default action not implemented\n");
567 return NS_ERROR_NOT_IMPLEMENTED;
570 static nsresult NSAPI nsChannel_GetContentLength(nsIHttpChannel *iface, PRInt32 *aContentLength)
572 nsChannel *This = NSCHANNEL_THIS(iface);
574 TRACE("(%p)->(%p)\n", This, aContentLength);
576 if(This->channel)
577 return nsIChannel_GetContentLength(This->channel, aContentLength);
579 FIXME("default action not implemented\n");
580 return NS_ERROR_NOT_IMPLEMENTED;
583 static nsresult NSAPI nsChannel_SetContentLength(nsIHttpChannel *iface, PRInt32 aContentLength)
585 nsChannel *This = NSCHANNEL_THIS(iface);
587 TRACE("(%p)->(%d)\n", This, aContentLength);
589 if(This->channel)
590 return nsIChannel_SetContentLength(This->channel, aContentLength);
592 FIXME("default action not implemented\n");
593 return NS_ERROR_NOT_IMPLEMENTED;
596 static nsresult NSAPI nsChannel_Open(nsIHttpChannel *iface, nsIInputStream **_retval)
598 nsChannel *This = NSCHANNEL_THIS(iface);
600 TRACE("(%p)->(%p)\n", This, _retval);
602 if(This->channel)
603 return nsIChannel_Open(This->channel, _retval);
605 FIXME("default action not implemented\n");
606 return NS_ERROR_NOT_IMPLEMENTED;
609 static BOOL do_load_from_moniker_hack(nsChannel *This)
611 nsACString scheme_str;
612 nsresult nsres;
613 BOOL ret = TRUE;
616 * We should always load the page from IMoniker, but Wine is not yet
617 * ready for this. This function is a heuristic, that decides which
618 * way of loading is better (Gecko implementation or IMoniker). The
619 * aim is to always return TRUE.
622 /* Load from moniker if there is no Gecko channel available */
623 if(!This->channel)
624 return TRUE;
626 nsACString_Init(&scheme_str, NULL);
627 nsres = nsIWineURI_GetScheme(This->uri, &scheme_str);
629 if(NS_SUCCEEDED(nsres)) {
630 const char *scheme;
632 nsACString_GetData(&scheme_str, &scheme);
633 ret = !strcmp(scheme, "wine") || !strcmp(scheme, "about");
636 nsACString_Finish(&scheme_str);
637 return ret;
640 static HRESULT create_mon_for_nschannel(nsChannel *channel, IMoniker **mon)
642 nsIWineURI *wine_uri;
643 LPCWSTR wine_url;
644 nsresult nsres;
645 HRESULT hres;
647 if(!channel->original_uri) {
648 ERR("original_uri == NULL\n");
649 return E_FAIL;
652 nsres = nsIURI_QueryInterface(channel->original_uri, &IID_nsIWineURI, (void**)&wine_uri);
653 if(NS_FAILED(nsres)) {
654 ERR("Could not get nsIWineURI: %08x\n", nsres);
655 return E_FAIL;
658 nsIWineURI_GetWineURL(wine_uri, &wine_url);
659 nsIWineURI_Release(wine_uri);
660 if(!wine_url) {
661 TRACE("wine_url == NULL\n");
662 return E_FAIL;
665 hres = CreateURLMoniker(NULL, wine_url, mon);
666 if(FAILED(hres))
667 WARN("CreateURLMonikrer failed: %08x\n", hres);
669 return hres;
672 static NSContainer *get_nscontainer_from_load_group(nsChannel *This)
674 NSContainer *container;
675 nsIChannel *channel;
676 nsIRequest *req;
677 nsIWineURI *wine_uri;
678 nsIURI *uri;
679 nsresult nsres;
681 nsres = nsILoadGroup_GetDefaultLoadRequest(This->load_group, &req);
682 if(NS_FAILED(nsres)) {
683 ERR("GetDefaultLoadRequest failed: %08x\n", nsres);
684 return NULL;
687 if(!req)
688 return NULL;
690 nsres = nsIRequest_QueryInterface(req, &IID_nsIChannel, (void**)&channel);
691 nsIRequest_Release(req);
692 if(NS_FAILED(nsres)) {
693 WARN("Could not get nsIChannel interface: %08x\n", nsres);
694 return NULL;
697 nsres = nsIChannel_GetURI(channel, &uri);
698 nsIChannel_Release(channel);
699 if(NS_FAILED(nsres)) {
700 ERR("GetURI failed: %08x\n", nsres);
701 return NULL;
704 nsres = nsIURI_QueryInterface(uri, &IID_nsIWineURI, (void**)&wine_uri);
705 nsIURI_Release(uri);
706 if(NS_FAILED(nsres)) {
707 TRACE("Could not get nsIWineURI: %08x\n", nsres);
708 return NULL;
711 nsIWineURI_GetNSContainer(wine_uri, &container);
712 nsIWineURI_Release(wine_uri);
714 return container;
717 static nsresult async_open_doc_uri(nsChannel *This, NSContainer *container,
718 nsIStreamListener *listener, nsISupports *context, BOOL *open)
720 IMoniker *mon;
721 HRESULT hres;
723 *open = FALSE;
725 if(container->bscallback) {
726 channelbsc_set_channel(container->bscallback, This, listener, context);
728 if(container->doc && container->doc->mime) {
729 heap_free(This->content_type);
730 This->content_type = heap_strdupWtoA(container->doc->mime);
733 if(do_load_from_moniker_hack(This))
734 return WINE_NS_LOAD_FROM_MONIKER;
735 }else {
736 BOOL cont = before_async_open(This, container);
738 if(!cont) {
739 TRACE("canceled\n");
740 return NS_ERROR_UNEXPECTED;
743 if(!container->doc) {
744 return This->channel
745 ? nsIChannel_AsyncOpen(This->channel, listener, context)
746 : NS_ERROR_UNEXPECTED;
749 hres = create_mon_for_nschannel(This, &mon);
750 if(FAILED(hres)) {
751 return NS_ERROR_UNEXPECTED;
753 set_current_mon(container->doc, mon);
756 *open = TRUE;
757 return NS_OK;
760 static nsresult async_open(nsChannel *This, NSContainer *container, nsIStreamListener *listener,
761 nsISupports *context)
763 nsChannelBSC *bscallback;
764 IMoniker *mon = NULL;
765 nsresult nsres;
766 task_t *task;
767 HRESULT hres;
769 if(This->channel) {
770 nsres = nsIChannel_AsyncOpen(This->channel, listener, context);
772 if(mon)
773 IMoniker_Release(mon);
775 if(NS_FAILED(nsres) && (This->load_flags & LOAD_INITIAL_DOCUMENT_URI))
776 return WINE_NS_LOAD_FROM_MONIKER;
777 return nsres;
780 TRACE("channel == NULL\n");
782 hres = create_mon_for_nschannel(This, &mon);
783 if(FAILED(hres))
784 return NS_ERROR_UNEXPECTED;
786 bscallback = create_channelbsc(mon);
787 IMoniker_Release(mon);
789 channelbsc_set_channel(bscallback, This, listener, context);
791 task = heap_alloc(sizeof(task_t));
793 task->doc = container->doc;
794 task->task_id = TASK_START_BINDING;
795 task->next = NULL;
796 task->bscallback = bscallback;
798 push_task(task);
800 return NS_OK;
803 static nsresult NSAPI nsChannel_AsyncOpen(nsIHttpChannel *iface, nsIStreamListener *aListener,
804 nsISupports *aContext)
806 nsChannel *This = NSCHANNEL_THIS(iface);
807 NSContainer *container;
808 PRBool is_doc_uri;
809 BOOL open = TRUE;
810 nsresult nsres = NS_OK;
812 TRACE("(%p)->(%p %p)\n", This, aListener, aContext);
814 nsIWineURI_GetNSContainer(This->uri, &container);
816 if(!container && This->load_group) {
817 container = get_nscontainer_from_load_group(This);
818 if(container)
819 nsIWineURI_SetNSContainer(This->uri, container);
822 if(!container) {
823 TRACE("container = NULL\n");
824 return This->channel
825 ? nsIChannel_AsyncOpen(This->channel, aListener, aContext)
826 : NS_ERROR_UNEXPECTED;
829 nsIWineURI_GetIsDocumentURI(This->uri, &is_doc_uri);
831 if(is_doc_uri && (This->load_flags & LOAD_INITIAL_DOCUMENT_URI))
832 nsres = async_open_doc_uri(This, container, aListener, aContext, &open);
834 if(open)
835 nsres = async_open(This, container, aListener, aContext);
837 nsIWebBrowserChrome_Release(NSWBCHROME(container));
838 return nsres;
841 static nsresult NSAPI nsChannel_GetRequestMethod(nsIHttpChannel *iface, nsACString *aRequestMethod)
843 nsChannel *This = NSCHANNEL_THIS(iface);
845 TRACE("(%p)->(%p)\n", This, aRequestMethod);
847 if(This->http_channel)
848 return nsIHttpChannel_GetRequestMethod(This->http_channel, aRequestMethod);
850 return NS_ERROR_NOT_IMPLEMENTED;
853 static nsresult NSAPI nsChannel_SetRequestMethod(nsIHttpChannel *iface,
854 const nsACString *aRequestMethod)
856 nsChannel *This = NSCHANNEL_THIS(iface);
858 TRACE("(%p)->(%p)\n", This, aRequestMethod);
860 if(This->http_channel)
861 return nsIHttpChannel_SetRequestMethod(This->http_channel, aRequestMethod);
863 return NS_ERROR_NOT_IMPLEMENTED;
866 static nsresult NSAPI nsChannel_GetReferrer(nsIHttpChannel *iface, nsIURI **aReferrer)
868 nsChannel *This = NSCHANNEL_THIS(iface);
870 TRACE("(%p)->(%p)\n", This, aReferrer);
872 if(This->http_channel)
873 return nsIHttpChannel_GetReferrer(This->http_channel, aReferrer);
875 return NS_ERROR_NOT_IMPLEMENTED;
878 static nsresult NSAPI nsChannel_SetReferrer(nsIHttpChannel *iface, nsIURI *aReferrer)
880 nsChannel *This = NSCHANNEL_THIS(iface);
882 TRACE("(%p)->(%p)\n", This, aReferrer);
884 if(This->http_channel)
885 return nsIHttpChannel_SetReferrer(This->http_channel, aReferrer);
887 return NS_ERROR_NOT_IMPLEMENTED;
890 static nsresult NSAPI nsChannel_GetRequestHeader(nsIHttpChannel *iface,
891 const nsACString *aHeader, nsACString *_retval)
893 nsChannel *This = NSCHANNEL_THIS(iface);
895 TRACE("(%p)->(%p %p)\n", This, aHeader, _retval);
897 if(This->http_channel)
898 return nsIHttpChannel_GetRequestHeader(This->http_channel, aHeader, _retval);
900 return NS_ERROR_NOT_IMPLEMENTED;
903 static nsresult NSAPI nsChannel_SetRequestHeader(nsIHttpChannel *iface,
904 const nsACString *aHeader, const nsACString *aValue, PRBool aMerge)
906 nsChannel *This = NSCHANNEL_THIS(iface);
908 TRACE("(%p)->(%p %p %x)\n", This, aHeader, aValue, aMerge);
910 if(This->http_channel)
911 return nsIHttpChannel_SetRequestHeader(This->http_channel, aHeader, aValue, aMerge);
913 return NS_ERROR_NOT_IMPLEMENTED;
916 static nsresult NSAPI nsChannel_VisitRequestHeaders(nsIHttpChannel *iface,
917 nsIHttpHeaderVisitor *aVisitor)
919 nsChannel *This = NSCHANNEL_THIS(iface);
921 TRACE("(%p)->(%p)\n", This, aVisitor);
923 if(This->http_channel)
924 return nsIHttpChannel_VisitRequestHeaders(This->http_channel, aVisitor);
926 return NS_ERROR_NOT_IMPLEMENTED;
929 static nsresult NSAPI nsChannel_GetAllowPipelining(nsIHttpChannel *iface, PRBool *aAllowPipelining)
931 nsChannel *This = NSCHANNEL_THIS(iface);
933 TRACE("(%p)->(%p)\n", This, aAllowPipelining);
935 if(This->http_channel)
936 return nsIHttpChannel_GetAllowPipelining(This->http_channel, aAllowPipelining);
938 return NS_ERROR_NOT_IMPLEMENTED;
941 static nsresult NSAPI nsChannel_SetAllowPipelining(nsIHttpChannel *iface, PRBool aAllowPipelining)
943 nsChannel *This = NSCHANNEL_THIS(iface);
945 TRACE("(%p)->(%x)\n", This, aAllowPipelining);
947 if(This->http_channel)
948 return nsIHttpChannel_SetAllowPipelining(This->http_channel, aAllowPipelining);
950 return NS_ERROR_NOT_IMPLEMENTED;
953 static nsresult NSAPI nsChannel_GetRedirectionLimit(nsIHttpChannel *iface, PRUint32 *aRedirectionLimit)
955 nsChannel *This = NSCHANNEL_THIS(iface);
957 TRACE("(%p)->(%p)\n", This, aRedirectionLimit);
959 if(This->http_channel)
960 return nsIHttpChannel_GetRedirectionLimit(This->http_channel, aRedirectionLimit);
962 return NS_ERROR_NOT_IMPLEMENTED;
965 static nsresult NSAPI nsChannel_SetRedirectionLimit(nsIHttpChannel *iface, PRUint32 aRedirectionLimit)
967 nsChannel *This = NSCHANNEL_THIS(iface);
969 TRACE("(%p)->(%u)\n", This, aRedirectionLimit);
971 if(This->http_channel)
972 return nsIHttpChannel_SetRedirectionLimit(This->http_channel, aRedirectionLimit);
974 return NS_ERROR_NOT_IMPLEMENTED;
977 static nsresult NSAPI nsChannel_GetResponseStatus(nsIHttpChannel *iface, PRUint32 *aResponseStatus)
979 nsChannel *This = NSCHANNEL_THIS(iface);
981 TRACE("(%p)->(%p)\n", This, aResponseStatus);
983 if(This->http_channel)
984 return nsIHttpChannel_GetResponseStatus(This->http_channel, aResponseStatus);
986 return NS_ERROR_NOT_IMPLEMENTED;
989 static nsresult NSAPI nsChannel_GetResponseStatusText(nsIHttpChannel *iface,
990 nsACString *aResponseStatusText)
992 nsChannel *This = NSCHANNEL_THIS(iface);
994 TRACE("(%p)->(%p)\n", This, aResponseStatusText);
996 if(This->http_channel)
997 return nsIHttpChannel_GetResponseStatusText(This->http_channel, aResponseStatusText);
999 return NS_ERROR_NOT_IMPLEMENTED;
1002 static nsresult NSAPI nsChannel_GetRequestSucceeded(nsIHttpChannel *iface,
1003 PRBool *aRequestSucceeded)
1005 nsChannel *This = NSCHANNEL_THIS(iface);
1007 TRACE("(%p)->(%p)\n", This, aRequestSucceeded);
1009 if(This->http_channel)
1010 return nsIHttpChannel_GetRequestSucceeded(This->http_channel, aRequestSucceeded);
1012 return NS_ERROR_NOT_IMPLEMENTED;
1015 static nsresult NSAPI nsChannel_GetResponseHeader(nsIHttpChannel *iface,
1016 const nsACString *header, nsACString *_retval)
1018 nsChannel *This = NSCHANNEL_THIS(iface);
1020 TRACE("(%p)->(%p %p)\n", This, header, _retval);
1022 if(This->http_channel)
1023 return nsIHttpChannel_GetResponseHeader(This->http_channel, header, _retval);
1025 return NS_ERROR_NOT_IMPLEMENTED;
1028 static nsresult NSAPI nsChannel_SetResponseHeader(nsIHttpChannel *iface,
1029 const nsACString *header, const nsACString *value, PRBool merge)
1031 nsChannel *This = NSCHANNEL_THIS(iface);
1033 TRACE("(%p)->(%p %p %x)\n", This, header, value, merge);
1035 if(This->http_channel)
1036 return nsIHttpChannel_SetResponseHeader(This->http_channel, header, value, merge);
1038 return NS_ERROR_NOT_IMPLEMENTED;
1041 static nsresult NSAPI nsChannel_VisitResponseHeaders(nsIHttpChannel *iface,
1042 nsIHttpHeaderVisitor *aVisitor)
1044 nsChannel *This = NSCHANNEL_THIS(iface);
1046 TRACE("(%p)->(%p)\n", This, aVisitor);
1048 if(This->http_channel)
1049 return nsIHttpChannel_VisitResponseHeaders(This->http_channel, aVisitor);
1051 return NS_ERROR_NOT_IMPLEMENTED;
1054 static nsresult NSAPI nsChannel_IsNoStoreResponse(nsIHttpChannel *iface, PRBool *_retval)
1056 nsChannel *This = NSCHANNEL_THIS(iface);
1058 TRACE("(%p)->(%p)\n", This, _retval);
1060 if(This->http_channel)
1061 return nsIHttpChannel_IsNoStoreResponse(This->http_channel, _retval);
1063 return NS_ERROR_NOT_IMPLEMENTED;
1066 static nsresult NSAPI nsChannel_IsNoCacheResponse(nsIHttpChannel *iface, PRBool *_retval)
1068 nsChannel *This = NSCHANNEL_THIS(iface);
1070 TRACE("(%p)->(%p)\n", This, _retval);
1072 if(This->http_channel)
1073 return nsIHttpChannel_IsNoCacheResponse(This->http_channel, _retval);
1075 return NS_ERROR_NOT_IMPLEMENTED;
1078 #undef NSCHANNEL_THIS
1080 static const nsIHttpChannelVtbl nsChannelVtbl = {
1081 nsChannel_QueryInterface,
1082 nsChannel_AddRef,
1083 nsChannel_Release,
1084 nsChannel_GetName,
1085 nsChannel_IsPending,
1086 nsChannel_GetStatus,
1087 nsChannel_Cancel,
1088 nsChannel_Suspend,
1089 nsChannel_Resume,
1090 nsChannel_GetLoadGroup,
1091 nsChannel_SetLoadGroup,
1092 nsChannel_GetLoadFlags,
1093 nsChannel_SetLoadFlags,
1094 nsChannel_GetOriginalURI,
1095 nsChannel_SetOriginalURI,
1096 nsChannel_GetURI,
1097 nsChannel_GetOwner,
1098 nsChannel_SetOwner,
1099 nsChannel_GetNotificationCallbacks,
1100 nsChannel_SetNotificationCallbacks,
1101 nsChannel_GetSecurityInfo,
1102 nsChannel_GetContentType,
1103 nsChannel_SetContentType,
1104 nsChannel_GetContentCharset,
1105 nsChannel_SetContentCharset,
1106 nsChannel_GetContentLength,
1107 nsChannel_SetContentLength,
1108 nsChannel_Open,
1109 nsChannel_AsyncOpen,
1110 nsChannel_GetRequestMethod,
1111 nsChannel_SetRequestMethod,
1112 nsChannel_GetReferrer,
1113 nsChannel_SetReferrer,
1114 nsChannel_GetRequestHeader,
1115 nsChannel_SetRequestHeader,
1116 nsChannel_VisitRequestHeaders,
1117 nsChannel_GetAllowPipelining,
1118 nsChannel_SetAllowPipelining,
1119 nsChannel_GetRedirectionLimit,
1120 nsChannel_SetRedirectionLimit,
1121 nsChannel_GetResponseStatus,
1122 nsChannel_GetResponseStatusText,
1123 nsChannel_GetRequestSucceeded,
1124 nsChannel_GetResponseHeader,
1125 nsChannel_SetResponseHeader,
1126 nsChannel_VisitResponseHeaders,
1127 nsChannel_IsNoStoreResponse,
1128 nsChannel_IsNoCacheResponse
1131 #define NSUPCHANNEL_THIS(iface) DEFINE_THIS(nsChannel, UploadChannel, iface)
1133 static nsresult NSAPI nsUploadChannel_QueryInterface(nsIUploadChannel *iface, nsIIDRef riid,
1134 nsQIResult result)
1136 nsChannel *This = NSUPCHANNEL_THIS(iface);
1137 return nsIChannel_QueryInterface(NSCHANNEL(This), riid, result);
1140 static nsrefcnt NSAPI nsUploadChannel_AddRef(nsIUploadChannel *iface)
1142 nsChannel *This = NSUPCHANNEL_THIS(iface);
1143 return nsIChannel_AddRef(NSCHANNEL(This));
1146 static nsrefcnt NSAPI nsUploadChannel_Release(nsIUploadChannel *iface)
1148 nsChannel *This = NSUPCHANNEL_THIS(iface);
1149 return nsIChannel_Release(NSCHANNEL(This));
1152 static nsresult NSAPI nsUploadChannel_SetUploadStream(nsIUploadChannel *iface,
1153 nsIInputStream *aStream, const nsACString *aContentType, PRInt32 aContentLength)
1155 nsChannel *This = NSUPCHANNEL_THIS(iface);
1156 const char *content_type;
1157 nsresult nsres;
1159 TRACE("(%p)->(%p %p %d)\n", This, aStream, aContentType, aContentLength);
1161 if(This->post_data_stream)
1162 nsIInputStream_Release(This->post_data_stream);
1164 if(aContentType) {
1165 nsACString_GetData(aContentType, &content_type);
1166 if(*content_type)
1167 FIXME("Unsupported aContentType argument: %s\n", debugstr_a(content_type));
1170 if(aContentLength != -1)
1171 FIXME("Unsupported acontentLength = %d\n", aContentLength);
1173 if(This->post_data_stream)
1174 nsIInputStream_Release(This->post_data_stream);
1175 This->post_data_stream = aStream;
1176 if(aStream)
1177 nsIInputStream_AddRef(aStream);
1179 if(This->post_data_stream) {
1180 nsIUploadChannel *upload_channel;
1182 nsres = nsIChannel_QueryInterface(This->channel, &IID_nsIUploadChannel,
1183 (void**)&upload_channel);
1184 if(NS_SUCCEEDED(nsres)) {
1185 nsres = nsIUploadChannel_SetUploadStream(upload_channel, aStream, aContentType, aContentLength);
1186 nsIUploadChannel_Release(upload_channel);
1187 if(NS_FAILED(nsres))
1188 WARN("SetUploadStream failed: %08x\n", nsres);
1193 return NS_OK;
1196 static nsresult NSAPI nsUploadChannel_GetUploadStream(nsIUploadChannel *iface,
1197 nsIInputStream **aUploadStream)
1199 nsChannel *This = NSUPCHANNEL_THIS(iface);
1201 TRACE("(%p)->(%p)\n", This, aUploadStream);
1203 if(This->post_data_stream)
1204 nsIInputStream_AddRef(This->post_data_stream);
1206 *aUploadStream = This->post_data_stream;
1207 return NS_OK;
1210 #undef NSUPCHANNEL_THIS
1212 static const nsIUploadChannelVtbl nsUploadChannelVtbl = {
1213 nsUploadChannel_QueryInterface,
1214 nsUploadChannel_AddRef,
1215 nsUploadChannel_Release,
1216 nsUploadChannel_SetUploadStream,
1217 nsUploadChannel_GetUploadStream
1220 #define NSURI_THIS(iface) DEFINE_THIS(nsURI, WineURI, iface)
1222 static nsresult NSAPI nsURI_QueryInterface(nsIWineURI *iface, nsIIDRef riid, nsQIResult result)
1224 nsURI *This = NSURI_THIS(iface);
1226 *result = NULL;
1228 if(IsEqualGUID(&IID_nsISupports, riid)) {
1229 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1230 *result = NSURI(This);
1231 }else if(IsEqualGUID(&IID_nsIURI, riid)) {
1232 TRACE("(%p)->(IID_nsIURI %p)\n", This, result);
1233 *result = NSURI(This);
1234 }else if(IsEqualGUID(&IID_nsIWineURI, riid)) {
1235 TRACE("(%p)->(IID_nsIWineURI %p)\n", This, result);
1236 *result = NSURI(This);
1239 if(*result) {
1240 nsIURI_AddRef(NSURI(This));
1241 return NS_OK;
1244 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1245 return This->uri ? nsIURI_QueryInterface(This->uri, riid, result) : NS_NOINTERFACE;
1248 static nsrefcnt NSAPI nsURI_AddRef(nsIWineURI *iface)
1250 nsURI *This = NSURI_THIS(iface);
1251 LONG ref = InterlockedIncrement(&This->ref);
1253 TRACE("(%p) ref=%d\n", This, ref);
1255 return ref;
1258 static nsrefcnt NSAPI nsURI_Release(nsIWineURI *iface)
1260 nsURI *This = NSURI_THIS(iface);
1261 LONG ref = InterlockedDecrement(&This->ref);
1263 TRACE("(%p) ref=%d\n", This, ref);
1265 if(!ref) {
1266 if(This->container)
1267 nsIWebBrowserChrome_Release(NSWBCHROME(This->container));
1268 if(This->uri)
1269 nsIURI_Release(This->uri);
1270 heap_free(This->wine_url);
1271 heap_free(This);
1274 return ref;
1277 static nsresult NSAPI nsURI_GetSpec(nsIWineURI *iface, nsACString *aSpec)
1279 nsURI *This = NSURI_THIS(iface);
1281 TRACE("(%p)->(%p)\n", This, aSpec);
1283 if(This->use_wine_url) {
1284 char speca[INTERNET_MAX_URL_LENGTH] = "wine:";
1285 WideCharToMultiByte(CP_ACP, 0, This->wine_url, -1, speca+5, sizeof(speca)-5, NULL, NULL);
1286 nsACString_SetData(aSpec, speca);
1288 return NS_OK;
1291 if(This->uri)
1292 return nsIURI_GetSpec(This->uri, aSpec);
1294 TRACE("returning error\n");
1295 return NS_ERROR_NOT_IMPLEMENTED;
1299 static nsresult NSAPI nsURI_SetSpec(nsIWineURI *iface, const nsACString *aSpec)
1301 nsURI *This = NSURI_THIS(iface);
1303 TRACE("(%p)->(%p)\n", This, aSpec);
1305 if(This->uri)
1306 return nsIURI_SetSpec(This->uri, aSpec);
1308 FIXME("default action not implemented\n");
1309 return NS_ERROR_NOT_IMPLEMENTED;
1312 static nsresult NSAPI nsURI_GetPrePath(nsIWineURI *iface, nsACString *aPrePath)
1314 nsURI *This = NSURI_THIS(iface);
1316 TRACE("(%p)->(%p)\n", This, aPrePath);
1318 if(This->uri)
1319 return nsIURI_GetPrePath(This->uri, aPrePath);
1321 FIXME("default action not implemented\n");
1322 return NS_ERROR_NOT_IMPLEMENTED;
1325 static nsresult NSAPI nsURI_GetScheme(nsIWineURI *iface, nsACString *aScheme)
1327 nsURI *This = NSURI_THIS(iface);
1329 TRACE("(%p)->(%p)\n", This, aScheme);
1331 if(This->use_wine_url && strcmpW(This->wine_url, about_blankW)) {
1333 * For Gecko we set scheme to unknown so it won't be handled
1334 * as any special case.
1336 nsACString_SetData(aScheme, "wine");
1337 return NS_OK;
1340 if(This->uri)
1341 return nsIURI_GetScheme(This->uri, aScheme);
1343 TRACE("returning error\n");
1344 return NS_ERROR_NOT_IMPLEMENTED;
1347 static nsresult NSAPI nsURI_SetScheme(nsIWineURI *iface, const nsACString *aScheme)
1349 nsURI *This = NSURI_THIS(iface);
1351 TRACE("(%p)->(%p)\n", This, aScheme);
1353 if(This->uri)
1354 return nsIURI_SetScheme(This->uri, aScheme);
1356 FIXME("default action not implemented\n");
1357 return NS_ERROR_NOT_IMPLEMENTED;
1360 static nsresult NSAPI nsURI_GetUserPass(nsIWineURI *iface, nsACString *aUserPass)
1362 nsURI *This = NSURI_THIS(iface);
1364 TRACE("(%p)->(%p)\n", This, aUserPass);
1366 if(This->uri)
1367 return nsIURI_GetUserPass(This->uri, aUserPass);
1369 FIXME("default action not implemented\n");
1370 return NS_ERROR_NOT_IMPLEMENTED;
1373 static nsresult NSAPI nsURI_SetUserPass(nsIWineURI *iface, const nsACString *aUserPass)
1375 nsURI *This = NSURI_THIS(iface);
1377 TRACE("(%p)->(%p)\n", This, aUserPass);
1379 if(This->uri)
1380 return nsIURI_SetUserPass(This->uri, aUserPass);
1382 FIXME("default action not implemented\n");
1383 return NS_ERROR_NOT_IMPLEMENTED;
1386 static nsresult NSAPI nsURI_GetUsername(nsIWineURI *iface, nsACString *aUsername)
1388 nsURI *This = NSURI_THIS(iface);
1390 TRACE("(%p)->(%p)\n", This, aUsername);
1392 if(This->uri)
1393 return nsIURI_GetUsername(This->uri, aUsername);
1395 FIXME("default action not implemented\n");
1396 return NS_ERROR_NOT_IMPLEMENTED;
1399 static nsresult NSAPI nsURI_SetUsername(nsIWineURI *iface, const nsACString *aUsername)
1401 nsURI *This = NSURI_THIS(iface);
1403 TRACE("(%p)->(%p)\n", This, aUsername);
1405 if(This->uri)
1406 return nsIURI_SetUsername(This->uri, aUsername);
1408 FIXME("default action not implemented\n");
1409 return NS_ERROR_NOT_IMPLEMENTED;
1412 static nsresult NSAPI nsURI_GetPassword(nsIWineURI *iface, nsACString *aPassword)
1414 nsURI *This = NSURI_THIS(iface);
1416 TRACE("(%p)->(%p)\n", This, aPassword);
1418 if(This->uri)
1419 return nsIURI_GetPassword(This->uri, aPassword);
1421 FIXME("default action not implemented\n");
1422 return NS_ERROR_NOT_IMPLEMENTED;
1425 static nsresult NSAPI nsURI_SetPassword(nsIWineURI *iface, const nsACString *aPassword)
1427 nsURI *This = NSURI_THIS(iface);
1429 TRACE("(%p)->(%p)\n", This, aPassword);
1431 if(This->uri)
1432 return nsIURI_SetPassword(This->uri, aPassword);
1434 FIXME("default action not implemented\n");
1435 return NS_ERROR_NOT_IMPLEMENTED;
1438 static nsresult NSAPI nsURI_GetHostPort(nsIWineURI *iface, nsACString *aHostPort)
1440 nsURI *This = NSURI_THIS(iface);
1442 TRACE("(%p)->(%p)\n", This, aHostPort);
1444 if(This->uri)
1445 return nsIURI_GetHostPort(This->uri, aHostPort);
1447 FIXME("default action not implemented\n");
1448 return NS_ERROR_NOT_IMPLEMENTED;
1451 static nsresult NSAPI nsURI_SetHostPort(nsIWineURI *iface, const nsACString *aHostPort)
1453 nsURI *This = NSURI_THIS(iface);
1455 TRACE("(%p)->(%p)\n", This, aHostPort);
1457 if(This->uri)
1458 return nsIURI_SetHostPort(This->uri, aHostPort);
1460 FIXME("default action not implemented\n");
1461 return NS_ERROR_NOT_IMPLEMENTED;
1464 static nsresult NSAPI nsURI_GetHost(nsIWineURI *iface, nsACString *aHost)
1466 nsURI *This = NSURI_THIS(iface);
1468 TRACE("(%p)->(%p)\n", This, aHost);
1470 if(This->uri)
1471 return nsIURI_GetHost(This->uri, aHost);
1473 FIXME("default action not implemented\n");
1474 return NS_ERROR_NOT_IMPLEMENTED;
1477 static nsresult NSAPI nsURI_SetHost(nsIWineURI *iface, const nsACString *aHost)
1479 nsURI *This = NSURI_THIS(iface);
1481 TRACE("(%p)->(%p)\n", This, aHost);
1483 if(This->uri)
1484 return nsIURI_SetHost(This->uri, aHost);
1486 FIXME("default action not implemented\n");
1487 return NS_ERROR_NOT_IMPLEMENTED;
1490 static nsresult NSAPI nsURI_GetPort(nsIWineURI *iface, PRInt32 *aPort)
1492 nsURI *This = NSURI_THIS(iface);
1494 TRACE("(%p)->(%p)\n", This, aPort);
1496 if(This->uri)
1497 return nsIURI_GetPort(This->uri, aPort);
1499 FIXME("default action not implemented\n");
1500 return NS_ERROR_NOT_IMPLEMENTED;
1503 static nsresult NSAPI nsURI_SetPort(nsIWineURI *iface, PRInt32 aPort)
1505 nsURI *This = NSURI_THIS(iface);
1507 TRACE("(%p)->(%d)\n", This, aPort);
1509 if(This->uri)
1510 return nsIURI_SetPort(This->uri, aPort);
1512 FIXME("default action not implemented\n");
1513 return NS_ERROR_NOT_IMPLEMENTED;
1516 static nsresult NSAPI nsURI_GetPath(nsIWineURI *iface, nsACString *aPath)
1518 nsURI *This = NSURI_THIS(iface);
1520 TRACE("(%p)->(%p)\n", This, aPath);
1522 if(This->uri)
1523 return nsIURI_GetPath(This->uri, aPath);
1525 FIXME("default action not implemented\n");
1526 return NS_ERROR_NOT_IMPLEMENTED;
1529 static nsresult NSAPI nsURI_SetPath(nsIWineURI *iface, const nsACString *aPath)
1531 nsURI *This = NSURI_THIS(iface);
1532 const char *path;
1534 nsACString_GetData(aPath, &path);
1535 TRACE("(%p)->(%p(%s))\n", This, aPath, debugstr_a(path));
1538 if(This->wine_url) {
1539 WCHAR new_url[INTERNET_MAX_URL_LENGTH];
1540 DWORD size = sizeof(new_url)/sizeof(WCHAR);
1541 LPWSTR pathw;
1542 HRESULT hres;
1544 pathw = heap_strdupAtoW(path);
1545 hres = UrlCombineW(This->wine_url, pathw, new_url, &size, 0);
1546 heap_free(pathw);
1547 if(SUCCEEDED(hres))
1548 nsIWineURI_SetWineURL(NSWINEURI(This), new_url);
1549 else
1550 WARN("UrlCombine failed: %08x\n", hres);
1553 if(!This->uri)
1554 return NS_OK;
1556 return nsIURI_SetPath(This->uri, aPath);
1559 static nsresult NSAPI nsURI_Equals(nsIWineURI *iface, nsIURI *other, PRBool *_retval)
1561 nsURI *This = NSURI_THIS(iface);
1562 nsIWineURI *wine_uri;
1563 LPCWSTR other_url = NULL;
1564 nsresult nsres;
1566 TRACE("(%p)->(%p %p)\n", This, other, _retval);
1568 if(This->uri)
1569 return nsIURI_Equals(This->uri, other, _retval);
1571 nsres = nsIURI_QueryInterface(other, &IID_nsIWineURI, (void**)&wine_uri);
1572 if(NS_FAILED(nsres)) {
1573 TRACE("Could not get nsIWineURI interface\n");
1574 *_retval = FALSE;
1575 return NS_OK;
1578 nsIWineURI_GetWineURL(wine_uri, &other_url);
1579 *_retval = !UrlCompareW(This->wine_url, other_url, TRUE);
1580 nsIWineURI_Release(wine_uri);
1582 return NS_OK;
1585 static nsresult NSAPI nsURI_SchemeIs(nsIWineURI *iface, const char *scheme, PRBool *_retval)
1587 nsURI *This = NSURI_THIS(iface);
1589 TRACE("(%p)->(%s %p)\n", This, debugstr_a(scheme), _retval);
1591 if(This->use_wine_url) {
1592 WCHAR buf[INTERNET_MAX_SCHEME_LENGTH];
1593 int len = MultiByteToWideChar(CP_ACP, 0, scheme, -1, buf, sizeof(buf)/sizeof(WCHAR))-1;
1595 *_retval = lstrlenW(This->wine_url) > len
1596 && This->wine_url[len] == ':'
1597 && !memcmp(buf, This->wine_url, len*sizeof(WCHAR));
1598 return NS_OK;
1601 if(This->uri)
1602 return nsIURI_SchemeIs(This->uri, scheme, _retval);
1604 TRACE("returning error\n");
1605 return NS_ERROR_NOT_IMPLEMENTED;
1608 static nsresult NSAPI nsURI_Clone(nsIWineURI *iface, nsIURI **_retval)
1610 nsURI *This = NSURI_THIS(iface);
1611 nsIURI *nsuri = NULL;
1612 nsIWineURI *wine_uri;
1613 nsresult nsres;
1615 TRACE("(%p)->(%p)\n", This, _retval);
1617 if(This->uri) {
1618 nsres = nsIURI_Clone(This->uri, &nsuri);
1619 if(NS_FAILED(nsres)) {
1620 WARN("Clone failed: %08x\n", nsres);
1621 return nsres;
1625 nsres = create_uri(nsuri, This->container, &wine_uri);
1626 if(NS_FAILED(nsres)) {
1627 WARN("create_uri failed: %08x\n", nsres);
1628 return nsres;
1631 *_retval = (nsIURI*)wine_uri;
1632 return nsIWineURI_SetWineURL(wine_uri, This->wine_url);
1635 static nsresult NSAPI nsURI_Resolve(nsIWineURI *iface, const nsACString *arelativePath,
1636 nsACString *_retval)
1638 nsURI *This = NSURI_THIS(iface);
1640 TRACE("(%p)->(%p %p)\n", This, arelativePath, _retval);
1642 if(This->uri)
1643 return nsIURI_Resolve(This->uri, arelativePath, _retval);
1645 FIXME("default action not implemented\n");
1646 return NS_ERROR_NOT_IMPLEMENTED;
1649 static nsresult NSAPI nsURI_GetAsciiSpec(nsIWineURI *iface, nsACString *aAsciiSpec)
1651 nsURI *This = NSURI_THIS(iface);
1653 TRACE("(%p)->(%p)\n", This, aAsciiSpec);
1655 if(This->use_wine_url)
1656 return nsIURI_GetSpec(NSURI(This), aAsciiSpec);
1658 if(This->uri)
1659 return nsIURI_GetAsciiSpec(This->uri, aAsciiSpec);
1661 TRACE("returning error\n");
1662 return NS_ERROR_NOT_IMPLEMENTED;
1665 static nsresult NSAPI nsURI_GetAsciiHost(nsIWineURI *iface, nsACString *aAsciiHost)
1667 nsURI *This = NSURI_THIS(iface);
1669 TRACE("(%p)->(%p)\n", This, aAsciiHost);
1671 if(This->uri)
1672 return nsIURI_GetAsciiHost(This->uri, aAsciiHost);
1674 FIXME("default action not implemented\n");
1675 return NS_ERROR_NOT_IMPLEMENTED;
1678 static nsresult NSAPI nsURI_GetOriginCharset(nsIWineURI *iface, nsACString *aOriginCharset)
1680 nsURI *This = NSURI_THIS(iface);
1682 TRACE("(%p)->(%p)\n", This, aOriginCharset);
1684 if(This->uri)
1685 return nsIURI_GetOriginCharset(This->uri, aOriginCharset);
1687 FIXME("default action not implemented\n");
1688 return NS_ERROR_NOT_IMPLEMENTED;
1691 static nsresult NSAPI nsURI_GetNSContainer(nsIWineURI *iface, NSContainer **aContainer)
1693 nsURI *This = NSURI_THIS(iface);
1695 TRACE("(%p)->(%p)\n", This, aContainer);
1697 if(This->container)
1698 nsIWebBrowserChrome_AddRef(NSWBCHROME(This->container));
1699 *aContainer = This->container;
1701 return NS_OK;
1704 static nsresult NSAPI nsURI_SetNSContainer(nsIWineURI *iface, NSContainer *aContainer)
1706 nsURI *This = NSURI_THIS(iface);
1708 TRACE("(%p)->(%p)\n", This, aContainer);
1710 if(This->container) {
1711 if(This->container == aContainer)
1712 return NS_OK;
1713 TRACE("Changing %p -> %p\n", This->container, aContainer);
1714 nsIWebBrowserChrome_Release(NSWBCHROME(This->container));
1717 if(aContainer)
1718 nsIWebBrowserChrome_AddRef(NSWBCHROME(aContainer));
1719 This->container = aContainer;
1721 return NS_OK;
1724 static nsresult NSAPI nsURI_GetIsDocumentURI(nsIWineURI *iface, PRBool *aIsDocumentURI)
1726 nsURI *This = NSURI_THIS(iface);
1728 TRACE("(%p)->(%p)\n", This, aIsDocumentURI);
1730 *aIsDocumentURI = This->is_doc_uri;
1731 return NS_OK;
1734 static nsresult NSAPI nsURI_SetIsDocumentURI(nsIWineURI *iface, PRBool aIsDocumentURI)
1736 nsURI *This = NSURI_THIS(iface);
1738 TRACE("(%p)->(%x)\n", This, aIsDocumentURI);
1740 This->is_doc_uri = aIsDocumentURI;
1741 return NS_OK;
1744 static nsresult NSAPI nsURI_GetWineURL(nsIWineURI *iface, LPCWSTR *aURL)
1746 nsURI *This = NSURI_THIS(iface);
1748 TRACE("(%p)->(%p)\n", This, aURL);
1750 *aURL = This->wine_url;
1751 return NS_OK;
1754 static nsresult NSAPI nsURI_SetWineURL(nsIWineURI *iface, LPCWSTR aURL)
1756 nsURI *This = NSURI_THIS(iface);
1758 static const WCHAR wszFtp[] = {'f','t','p',':'};
1759 static const WCHAR wszHttp[] = {'h','t','t','p',':'};
1760 static const WCHAR wszHttps[] = {'h','t','t','p','s',':'};
1762 TRACE("(%p)->(%s)\n", This, debugstr_w(aURL));
1764 heap_free(This->wine_url);
1766 if(aURL) {
1767 int len = strlenW(aURL)+1;
1768 This->wine_url = heap_alloc(len*sizeof(WCHAR));
1769 memcpy(This->wine_url, aURL, len*sizeof(WCHAR));
1771 /* FIXME: Always use wine url */
1772 This->use_wine_url =
1773 strncmpW(aURL, wszFtp, sizeof(wszFtp)/sizeof(WCHAR))
1774 && strncmpW(aURL, wszHttp, sizeof(wszHttp)/sizeof(WCHAR))
1775 && strncmpW(aURL, wszHttps, sizeof(wszHttps)/sizeof(WCHAR));
1776 }else {
1777 This->wine_url = NULL;
1778 This->use_wine_url = FALSE;
1781 return NS_OK;
1784 #undef NSURI_THIS
1786 static const nsIWineURIVtbl nsWineURIVtbl = {
1787 nsURI_QueryInterface,
1788 nsURI_AddRef,
1789 nsURI_Release,
1790 nsURI_GetSpec,
1791 nsURI_SetSpec,
1792 nsURI_GetPrePath,
1793 nsURI_GetScheme,
1794 nsURI_SetScheme,
1795 nsURI_GetUserPass,
1796 nsURI_SetUserPass,
1797 nsURI_GetUsername,
1798 nsURI_SetUsername,
1799 nsURI_GetPassword,
1800 nsURI_SetPassword,
1801 nsURI_GetHostPort,
1802 nsURI_SetHostPort,
1803 nsURI_GetHost,
1804 nsURI_SetHost,
1805 nsURI_GetPort,
1806 nsURI_SetPort,
1807 nsURI_GetPath,
1808 nsURI_SetPath,
1809 nsURI_Equals,
1810 nsURI_SchemeIs,
1811 nsURI_Clone,
1812 nsURI_Resolve,
1813 nsURI_GetAsciiSpec,
1814 nsURI_GetAsciiHost,
1815 nsURI_GetOriginCharset,
1816 nsURI_GetNSContainer,
1817 nsURI_SetNSContainer,
1818 nsURI_GetIsDocumentURI,
1819 nsURI_SetIsDocumentURI,
1820 nsURI_GetWineURL,
1821 nsURI_SetWineURL
1824 static nsresult create_uri(nsIURI *uri, NSContainer *container, nsIWineURI **_retval)
1826 nsURI *ret = heap_alloc(sizeof(nsURI));
1828 ret->lpWineURIVtbl = &nsWineURIVtbl;
1829 ret->ref = 1;
1830 ret->uri = uri;
1831 ret->container = container;
1832 ret->wine_url = NULL;
1833 ret->is_doc_uri = FALSE;
1834 ret->use_wine_url = FALSE;
1836 if(container)
1837 nsIWebBrowserChrome_AddRef(NSWBCHROME(container));
1839 TRACE("retval=%p\n", ret);
1840 *_retval = NSWINEURI(ret);
1841 return NS_OK;
1844 typedef struct {
1845 const nsIProtocolHandlerVtbl *lpProtocolHandlerVtbl;
1847 LONG ref;
1849 nsIProtocolHandler *nshandler;
1850 } nsProtocolHandler;
1852 #define NSPROTHANDLER(x) ((nsIProtocolHandler*) &(x)->lpProtocolHandlerVtbl)
1854 #define NSPROTHANDLER_THIS(iface) DEFINE_THIS(nsProtocolHandler, ProtocolHandler, iface)
1856 static nsresult NSAPI nsProtocolHandler_QueryInterface(nsIProtocolHandler *iface, nsIIDRef riid,
1857 nsQIResult result)
1859 nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1861 *result = NULL;
1863 if(IsEqualGUID(&IID_nsISupports, riid)) {
1864 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1865 *result = NSPROTHANDLER(This);
1866 }else if(IsEqualGUID(&IID_nsIProtocolHandler, riid)) {
1867 TRACE("(%p)->(IID_nsIProtocolHandler %p)\n", This, result);
1868 *result = NSPROTHANDLER(This);
1869 }else if(IsEqualGUID(&IID_nsIExternalProtocolHandler, riid)) {
1870 TRACE("(%p)->(IID_nsIExternalProtocolHandler %p), returning NULL\n", This, result);
1871 return NS_NOINTERFACE;
1874 if(*result) {
1875 nsISupports_AddRef((nsISupports*)*result);
1876 return NS_OK;
1879 WARN("(%s %p)\n", debugstr_guid(riid), result);
1880 return NS_NOINTERFACE;
1883 static nsrefcnt NSAPI nsProtocolHandler_AddRef(nsIProtocolHandler *iface)
1885 nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1886 LONG ref = InterlockedIncrement(&This->ref);
1888 TRACE("(%p) ref=%d\n", This, ref);
1890 return ref;
1893 static nsrefcnt NSAPI nsProtocolHandler_Release(nsIProtocolHandler *iface)
1895 nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1896 LONG ref = InterlockedDecrement(&This->ref);
1898 TRACE("(%p) ref=%d\n", This, ref);
1900 if(!ref) {
1901 if(This->nshandler)
1902 nsIProtocolHandler_Release(This->nshandler);
1903 heap_free(This);
1906 return ref;
1909 static nsresult NSAPI nsProtocolHandler_GetScheme(nsIProtocolHandler *iface, nsACString *aScheme)
1911 nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1913 TRACE("(%p)->(%p)\n", This, aScheme);
1915 if(This->nshandler)
1916 return nsIProtocolHandler_GetScheme(This->nshandler, aScheme);
1917 return NS_ERROR_NOT_IMPLEMENTED;
1920 static nsresult NSAPI nsProtocolHandler_GetDefaultPort(nsIProtocolHandler *iface,
1921 PRInt32 *aDefaultPort)
1923 nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1925 TRACE("(%p)->(%p)\n", This, aDefaultPort);
1927 if(This->nshandler)
1928 return nsIProtocolHandler_GetDefaultPort(This->nshandler, aDefaultPort);
1929 return NS_ERROR_NOT_IMPLEMENTED;
1932 static nsresult NSAPI nsProtocolHandler_GetProtocolFlags(nsIProtocolHandler *iface,
1933 PRUint32 *aProtocolFlags)
1935 nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1937 TRACE("(%p)->(%p)\n", This, aProtocolFlags);
1939 if(This->nshandler)
1940 return nsIProtocolHandler_GetProtocolFlags(This->nshandler, aProtocolFlags);
1941 return NS_ERROR_NOT_IMPLEMENTED;
1944 static nsresult NSAPI nsProtocolHandler_NewURI(nsIProtocolHandler *iface,
1945 const nsACString *aSpec, const char *aOriginCharset, nsIURI *aBaseURI, nsIURI **_retval)
1947 nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1949 TRACE("((%p)->%p %s %p %p)\n", This, aSpec, debugstr_a(aOriginCharset), aBaseURI, _retval);
1951 if(This->nshandler)
1952 return nsIProtocolHandler_NewURI(This->nshandler, aSpec, aOriginCharset, aBaseURI, _retval);
1953 return NS_ERROR_NOT_IMPLEMENTED;
1956 static nsresult NSAPI nsProtocolHandler_NewChannel(nsIProtocolHandler *iface,
1957 nsIURI *aURI, nsIChannel **_retval)
1959 nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1961 TRACE("(%p)->(%p %p)\n", This, aURI, _retval);
1963 if(This->nshandler)
1964 return nsIProtocolHandler_NewChannel(This->nshandler, aURI, _retval);
1965 return NS_ERROR_NOT_IMPLEMENTED;
1968 static nsresult NSAPI nsProtocolHandler_AllowPort(nsIProtocolHandler *iface,
1969 PRInt32 port, const char *scheme, PRBool *_retval)
1971 nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1973 TRACE("(%p)->(%d %s %p)\n", This, port, debugstr_a(scheme), _retval);
1975 if(This->nshandler)
1976 return nsIProtocolHandler_AllowPort(This->nshandler, port, scheme, _retval);
1977 return NS_ERROR_NOT_IMPLEMENTED;
1980 #undef NSPROTHANDLER_THIS
1982 static const nsIProtocolHandlerVtbl nsProtocolHandlerVtbl = {
1983 nsProtocolHandler_QueryInterface,
1984 nsProtocolHandler_AddRef,
1985 nsProtocolHandler_Release,
1986 nsProtocolHandler_GetScheme,
1987 nsProtocolHandler_GetDefaultPort,
1988 nsProtocolHandler_GetProtocolFlags,
1989 nsProtocolHandler_NewURI,
1990 nsProtocolHandler_NewChannel,
1991 nsProtocolHandler_AllowPort
1994 static nsIProtocolHandler *create_protocol_handler(nsIProtocolHandler *nshandler)
1996 nsProtocolHandler *ret = heap_alloc(sizeof(nsProtocolHandler));
1998 ret->lpProtocolHandlerVtbl = &nsProtocolHandlerVtbl;
1999 ret->ref = 1;
2000 ret->nshandler = nshandler;
2002 return NSPROTHANDLER(ret);
2005 static nsresult NSAPI nsIOService_QueryInterface(nsIIOService*,nsIIDRef,nsQIResult);
2007 static nsrefcnt NSAPI nsIOService_AddRef(nsIIOService *iface)
2009 return 2;
2012 static nsrefcnt NSAPI nsIOService_Release(nsIIOService *iface)
2014 return 1;
2017 static nsresult NSAPI nsIOService_GetProtocolHandler(nsIIOService *iface, const char *aScheme,
2018 nsIProtocolHandler **_retval)
2020 nsIExternalProtocolHandler *nsexthandler;
2021 nsIProtocolHandler *nshandler;
2022 nsresult nsres;
2024 TRACE("(%s %p)\n", debugstr_a(aScheme), _retval);
2026 nsres = nsIIOService_GetProtocolHandler(nsio, aScheme, &nshandler);
2027 if(NS_FAILED(nsres)) {
2028 WARN("GetProtocolHandler failed: %08x\n", nsres);
2029 return nsres;
2032 nsres = nsIProtocolHandler_QueryInterface(nshandler, &IID_nsIExternalProtocolHandler,
2033 (void**)&nsexthandler);
2034 if(NS_FAILED(nsres)) {
2035 *_retval = nshandler;
2036 return NS_OK;
2039 nsIExternalProtocolHandler_Release(nsexthandler);
2040 *_retval = create_protocol_handler(nshandler);
2041 TRACE("return %p\n", *_retval);
2042 return NS_OK;
2045 static nsresult NSAPI nsIOService_GetProtocolFlags(nsIIOService *iface, const char *aScheme,
2046 PRUint32 *_retval)
2048 TRACE("(%s %p)\n", debugstr_a(aScheme), _retval);
2049 return nsIIOService_GetProtocolFlags(nsio, aScheme, _retval);
2052 static BOOL is_gecko_special_uri(const char *spec)
2054 static const char chromeW[] = "chrome:";
2055 static const char jarW[] = "jar:";
2056 static const char resourceW[] = "resource:";
2057 static const char javascriptW[] = "javascript:";
2059 return !strncasecmp(spec, chromeW, sizeof(chromeW)-1)
2060 || !strncasecmp(spec, resourceW, sizeof(resourceW)-1)
2061 || !strncasecmp(spec, jarW, sizeof(jarW)-1)
2062 || !strncasecmp(spec, javascriptW, sizeof(javascriptW)-1);
2065 static nsresult NSAPI nsIOService_NewURI(nsIIOService *iface, const nsACString *aSpec,
2066 const char *aOriginCharset, nsIURI *aBaseURI, nsIURI **_retval)
2068 const char *spec = NULL;
2069 NSContainer *nscontainer = NULL;
2070 nsIURI *uri = NULL;
2071 LPCWSTR base_wine_url = NULL;
2072 nsIWineURI *base_wine_uri = NULL, *wine_uri;
2073 BOOL is_wine_uri = FALSE;
2074 nsresult nsres;
2076 nsACString_GetData(aSpec, &spec);
2078 TRACE("(%p(%s) %s %p %p)\n", aSpec, debugstr_a(spec), debugstr_a(aOriginCharset),
2079 aBaseURI, _retval);
2081 if(is_gecko_special_uri(spec))
2082 return nsIIOService_NewURI(nsio, aSpec, aOriginCharset, aBaseURI, _retval);
2084 if(!strncmp(spec, "wine:", 5)) {
2085 spec += 5;
2086 is_wine_uri = TRUE;
2089 if(aBaseURI) {
2090 nsACString base_uri_str;
2091 const char *base_uri = NULL;
2093 nsACString_Init(&base_uri_str, NULL);
2095 nsres = nsIURI_GetSpec(aBaseURI, &base_uri_str);
2096 if(NS_SUCCEEDED(nsres)) {
2097 nsACString_GetData(&base_uri_str, &base_uri);
2098 TRACE("base_uri=%s\n", debugstr_a(base_uri));
2099 }else {
2100 ERR("GetSpec failed: %08x\n", nsres);
2103 nsACString_Finish(&base_uri_str);
2106 nsres = nsIIOService_NewURI(nsio, aSpec, aOriginCharset, aBaseURI, &uri);
2107 if(NS_FAILED(nsres))
2108 TRACE("NewURI failed: %08x\n", nsres);
2110 if(aBaseURI) {
2111 nsres = nsIURI_QueryInterface(aBaseURI, &IID_nsIWineURI, (void**)&base_wine_uri);
2112 if(NS_SUCCEEDED(nsres)) {
2113 nsIWineURI_GetNSContainer(base_wine_uri, &nscontainer);
2114 nsIWineURI_GetWineURL(base_wine_uri, &base_wine_url);
2115 }else {
2116 TRACE("Could not get base nsIWineURI: %08x\n", nsres);
2120 TRACE("nscontainer = %p\n", nscontainer);
2122 nsres = create_uri(uri, nscontainer, &wine_uri);
2123 *_retval = (nsIURI*)wine_uri;
2125 if(nscontainer)
2126 nsIWebBrowserChrome_Release(NSWBCHROME(nscontainer));
2128 if(base_wine_url) {
2129 WCHAR url[INTERNET_MAX_URL_LENGTH], rel_url[INTERNET_MAX_URL_LENGTH];
2130 DWORD len;
2131 HRESULT hres;
2133 MultiByteToWideChar(CP_ACP, 0, spec, -1, rel_url, sizeof(rel_url)/sizeof(WCHAR));
2135 hres = CoInternetCombineUrl(base_wine_url, rel_url,
2136 URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO,
2137 url, sizeof(url)/sizeof(WCHAR), &len, 0);
2138 if(SUCCEEDED(hres))
2139 nsIWineURI_SetWineURL(wine_uri, url);
2140 else
2141 WARN("CoCombineUrl failed: %08x\n", hres);
2142 }else if(is_wine_uri) {
2143 WCHAR url[INTERNET_MAX_URL_LENGTH];
2145 MultiByteToWideChar(CP_ACP, 0, spec, -1, url, sizeof(url)/sizeof(WCHAR));
2146 nsIWineURI_SetWineURL(wine_uri, url);
2149 if(base_wine_uri)
2150 nsIWineURI_Release(base_wine_uri);
2152 return nsres;
2155 static nsresult NSAPI nsIOService_NewFileURI(nsIIOService *iface, nsIFile *aFile,
2156 nsIURI **_retval)
2158 TRACE("(%p %p)\n", aFile, _retval);
2159 return nsIIOService_NewFileURI(nsio, aFile, _retval);
2162 static nsresult NSAPI nsIOService_NewChannelFromURI(nsIIOService *iface, nsIURI *aURI,
2163 nsIChannel **_retval)
2165 nsIChannel *channel = NULL;
2166 nsChannel *ret;
2167 nsIWineURI *wine_uri;
2168 nsresult nsres;
2170 TRACE("(%p %p)\n", aURI, _retval);
2172 nsres = nsIIOService_NewChannelFromURI(nsio, aURI, &channel);
2173 if(NS_FAILED(nsres) && nsres != NS_ERROR_UNKNOWN_PROTOCOL) {
2174 WARN("NewChannelFromURI failed: %08x\n", nsres);
2175 *_retval = channel;
2176 return nsres;
2179 nsres = nsIURI_QueryInterface(aURI, &IID_nsIWineURI, (void**)&wine_uri);
2180 if(NS_FAILED(nsres)) {
2181 WARN("Could not get nsIWineURI: %08x\n", nsres);
2182 *_retval = channel;
2183 return channel ? NS_OK : NS_ERROR_UNEXPECTED;
2186 ret = heap_alloc_zero(sizeof(nsChannel));
2188 ret->lpHttpChannelVtbl = &nsChannelVtbl;
2189 ret->lpUploadChannelVtbl = &nsUploadChannelVtbl;
2190 ret->ref = 1;
2191 ret->channel = channel;
2192 ret->uri = wine_uri;
2194 nsIURI_AddRef(aURI);
2195 ret->original_uri = aURI;
2197 if(channel)
2198 nsIChannel_QueryInterface(channel, &IID_nsIHttpChannel, (void**)&ret->http_channel);
2200 *_retval = NSCHANNEL(ret);
2201 return NS_OK;
2204 static nsresult NSAPI nsIOService_NewChannel(nsIIOService *iface, const nsACString *aSpec,
2205 const char *aOriginCharset, nsIURI *aBaseURI, nsIChannel **_retval)
2207 TRACE("(%p %s %p %p)\n", aSpec, debugstr_a(aOriginCharset), aBaseURI, _retval);
2208 return nsIIOService_NewChannel(nsio, aSpec, aOriginCharset, aBaseURI, _retval);
2211 static nsresult NSAPI nsIOService_GetOffline(nsIIOService *iface, PRBool *aOffline)
2213 TRACE("(%p)\n", aOffline);
2214 return nsIIOService_GetOffline(nsio, aOffline);
2217 static nsresult NSAPI nsIOService_SetOffline(nsIIOService *iface, PRBool aOffline)
2219 TRACE("(%x)\n", aOffline);
2220 return nsIIOService_SetOffline(nsio, aOffline);
2223 static nsresult NSAPI nsIOService_AllowPort(nsIIOService *iface, PRInt32 aPort,
2224 const char *aScheme, PRBool *_retval)
2226 TRACE("(%d %s %p)\n", aPort, debugstr_a(aScheme), _retval);
2227 return nsIIOService_AllowPort(nsio, aPort, debugstr_a(aScheme), _retval);
2230 static nsresult NSAPI nsIOService_ExtractScheme(nsIIOService *iface, const nsACString *urlString,
2231 nsACString * _retval)
2233 TRACE("(%p %p)\n", urlString, _retval);
2234 return nsIIOService_ExtractScheme(nsio, urlString, _retval);
2237 static const nsIIOServiceVtbl nsIOServiceVtbl = {
2238 nsIOService_QueryInterface,
2239 nsIOService_AddRef,
2240 nsIOService_Release,
2241 nsIOService_GetProtocolHandler,
2242 nsIOService_GetProtocolFlags,
2243 nsIOService_NewURI,
2244 nsIOService_NewFileURI,
2245 nsIOService_NewChannelFromURI,
2246 nsIOService_NewChannel,
2247 nsIOService_GetOffline,
2248 nsIOService_SetOffline,
2249 nsIOService_AllowPort,
2250 nsIOService_ExtractScheme
2253 static nsIIOService nsIOService = { &nsIOServiceVtbl };
2255 static nsresult NSAPI nsNetUtil_QueryInterface(nsINetUtil *iface, nsIIDRef riid,
2256 nsQIResult result)
2258 return nsIIOService_QueryInterface(&nsIOService, riid, result);
2261 static nsrefcnt NSAPI nsNetUtil_AddRef(nsINetUtil *iface)
2263 return 2;
2266 static nsrefcnt NSAPI nsNetUtil_Release(nsINetUtil *iface)
2268 return 1;
2271 static nsresult NSAPI nsNetUtil_ParseContentType(nsINetUtil *iface, const nsACString *aTypeHeader,
2272 nsACString *aCharset, PRBool *aHadCharset, nsACString *aContentType)
2274 TRACE("(%p %p %p %p)\n", aTypeHeader, aCharset, aHadCharset, aContentType);
2276 return nsINetUtil_ParseContentType(net_util, aTypeHeader, aCharset, aHadCharset, aContentType);
2279 static const nsINetUtilVtbl nsNetUtilVtbl = {
2280 nsNetUtil_QueryInterface,
2281 nsNetUtil_AddRef,
2282 nsNetUtil_Release,
2283 nsNetUtil_ParseContentType
2286 static nsINetUtil nsNetUtil = { &nsNetUtilVtbl };
2288 static nsresult NSAPI nsIOService_QueryInterface(nsIIOService *iface, nsIIDRef riid,
2289 nsQIResult result)
2291 *result = NULL;
2293 if(IsEqualGUID(&IID_nsISupports, riid))
2294 *result = &nsIOService;
2295 else if(IsEqualGUID(&IID_nsIIOService, riid))
2296 *result = &nsIOService;
2297 else if(IsEqualGUID(&IID_nsINetUtil, riid))
2298 *result = &nsNetUtil;
2300 if(*result) {
2301 nsISupports_AddRef((nsISupports*)*result);
2302 return NS_OK;
2305 FIXME("(%s %p)\n", debugstr_guid(riid), result);
2306 return NS_NOINTERFACE;
2309 static nsresult NSAPI nsIOServiceFactory_QueryInterface(nsIFactory *iface, nsIIDRef riid,
2310 nsQIResult result)
2312 *result = NULL;
2314 if(IsEqualGUID(&IID_nsISupports, riid)) {
2315 TRACE("(IID_nsISupports %p)\n", result);
2316 *result = iface;
2317 }else if(IsEqualGUID(&IID_nsIFactory, riid)) {
2318 TRACE("(IID_nsIFactory %p)\n", result);
2319 *result = iface;
2322 if(*result) {
2323 nsIFactory_AddRef(iface);
2324 return NS_OK;
2327 WARN("(%s %p)\n", debugstr_guid(riid), result);
2328 return NS_NOINTERFACE;
2331 static nsrefcnt NSAPI nsIOServiceFactory_AddRef(nsIFactory *iface)
2333 return 2;
2336 static nsrefcnt NSAPI nsIOServiceFactory_Release(nsIFactory *iface)
2338 return 1;
2341 static nsresult NSAPI nsIOServiceFactory_CreateInstance(nsIFactory *iface,
2342 nsISupports *aOuter, const nsIID *iid, void **result)
2344 return nsIIOService_QueryInterface(&nsIOService, iid, result);
2347 static nsresult NSAPI nsIOServiceFactory_LockFactory(nsIFactory *iface, PRBool lock)
2349 WARN("(%x)\n", lock);
2350 return NS_OK;
2353 static const nsIFactoryVtbl nsIOServiceFactoryVtbl = {
2354 nsIOServiceFactory_QueryInterface,
2355 nsIOServiceFactory_AddRef,
2356 nsIOServiceFactory_Release,
2357 nsIOServiceFactory_CreateInstance,
2358 nsIOServiceFactory_LockFactory
2361 static nsIFactory nsIOServiceFactory = { &nsIOServiceFactoryVtbl };
2363 void init_nsio(nsIComponentManager *component_manager, nsIComponentRegistrar *registrar)
2365 nsIFactory *old_factory = NULL;
2366 nsresult nsres;
2368 nsres = nsIComponentManager_GetClassObject(component_manager, &NS_IOSERVICE_CID,
2369 &IID_nsIFactory, (void**)&old_factory);
2370 if(NS_FAILED(nsres)) {
2371 ERR("Could not get factory: %08x\n", nsres);
2372 return;
2375 nsres = nsIFactory_CreateInstance(old_factory, NULL, &IID_nsIIOService, (void**)&nsio);
2376 if(NS_FAILED(nsres)) {
2377 ERR("Couldn not create nsIOService instance %08x\n", nsres);
2378 nsIFactory_Release(old_factory);
2379 return;
2382 nsres = nsIIOService_QueryInterface(nsio, &IID_nsINetUtil, (void**)&net_util);
2383 if(NS_FAILED(nsres)) {
2384 WARN("Could not get nsINetUtil interface: %08x\n", nsres);
2385 nsIIOService_Release(nsio);
2386 return;
2389 nsres = nsIComponentRegistrar_UnregisterFactory(registrar, &NS_IOSERVICE_CID, old_factory);
2390 nsIFactory_Release(old_factory);
2391 if(NS_FAILED(nsres))
2392 ERR("UnregisterFactory failed: %08x\n", nsres);
2394 nsres = nsIComponentRegistrar_RegisterFactory(registrar, &NS_IOSERVICE_CID,
2395 NS_IOSERVICE_CLASSNAME, NS_IOSERVICE_CONTRACTID, &nsIOServiceFactory);
2396 if(NS_FAILED(nsres))
2397 ERR("RegisterFactory failed: %08x\n", nsres);
2400 void release_nsio(void)
2402 if(net_util) {
2403 nsINetUtil_Release(net_util);
2404 net_util = NULL;
2407 if(nsio) {
2408 nsIIOService_Release(nsio);
2409 nsio = NULL;