push f2fb4b43e6a5ad960b5745b9c70e0a8fefaca935
[wine/hacks.git] / dlls / mshtml / nsio.c
blob0385f4ac25ba639aaaa01d7db35e30b0a5e37a1f
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;
51 typedef struct {
52 const nsIWineURIVtbl *lpWineURIVtbl;
54 LONG ref;
56 nsIURI *uri;
57 NSContainer *container;
58 LPWSTR wine_url;
59 PRBool is_doc_uri;
60 BOOL use_wine_url;
61 } nsURI;
63 #define NSURI(x) ((nsIURI*) &(x)->lpWineURIVtbl)
64 #define NSWINEURI(x) ((nsIWineURI*) &(x)->lpWineURIVtbl)
66 static nsresult create_uri(nsIURI*,NSContainer*,nsIWineURI**);
68 static BOOL exec_shldocvw_67(HTMLDocument *doc, LPCWSTR url)
70 IOleCommandTarget *cmdtrg = NULL;
71 HRESULT hres;
73 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget,
74 (void**)&cmdtrg);
75 if(SUCCEEDED(hres)) {
76 VARIANT varUrl, varRes;
78 V_VT(&varUrl) = VT_BSTR;
79 V_BSTR(&varUrl) = SysAllocString(url);
80 V_VT(&varRes) = VT_BOOL;
82 hres = IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 67, 0, &varUrl, &varRes);
84 IOleCommandTarget_Release(cmdtrg);
85 SysFreeString(V_BSTR(&varUrl));
87 if(SUCCEEDED(hres) && !V_BOOL(&varRes)) {
88 TRACE("got VARIANT_FALSE, do not load\n");
89 return FALSE;
93 return TRUE;
96 static BOOL before_async_open(nsChannel *channel, NSContainer *container)
98 IServiceProvider *service_provider;
99 HTMLDocument *doc = container->doc;
100 DWORD hlnf = 0;
101 LPCWSTR uri;
102 HRESULT hres;
104 nsIWineURI_GetWineURL(channel->uri, &uri);
105 if(!uri) {
106 ERR("GetWineURL returned NULL\n");
107 return TRUE;
110 if(!doc) {
111 NSContainer *container_iter = container;
113 hlnf = HLNF_OPENINNEWWINDOW;
114 while(!container_iter->doc)
115 container_iter = container_iter->parent;
116 doc = container_iter->doc;
119 if(!doc->client)
120 return TRUE;
122 if(!hlnf && !exec_shldocvw_67(doc, uri))
123 return FALSE;
125 hres = IOleClientSite_QueryInterface(doc->client, &IID_IServiceProvider,
126 (void**)&service_provider);
127 if(SUCCEEDED(hres)) {
128 IHlinkFrame *hlink_frame;
130 hres = IServiceProvider_QueryService(service_provider, &IID_IHlinkFrame,
131 &IID_IHlinkFrame, (void**)&hlink_frame);
132 IServiceProvider_Release(service_provider);
133 if(SUCCEEDED(hres)) {
134 hlink_frame_navigate(doc, hlink_frame, uri, channel->post_data_stream, hlnf);
135 IHlinkFrame_Release(hlink_frame);
137 return FALSE;
141 return TRUE;
144 #define NSCHANNEL_THIS(iface) DEFINE_THIS(nsChannel, HttpChannel, iface)
146 static nsresult NSAPI nsChannel_QueryInterface(nsIHttpChannel *iface, nsIIDRef riid, nsQIResult result)
148 nsChannel *This = NSCHANNEL_THIS(iface);
150 *result = NULL;
152 if(IsEqualGUID(&IID_nsISupports, riid)) {
153 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
154 *result = NSCHANNEL(This);
155 }else if(IsEqualGUID(&IID_nsIRequest, riid)) {
156 TRACE("(%p)->(IID_nsIRequest %p)\n", This, result);
157 *result = NSCHANNEL(This);
158 }else if(IsEqualGUID(&IID_nsIChannel, riid)) {
159 TRACE("(%p)->(IID_nsIChannel %p)\n", This, result);
160 *result = NSCHANNEL(This);
161 }else if(This->http_channel && IsEqualGUID(&IID_nsIHttpChannel, riid)) {
162 TRACE("(%p)->(IID_nsIHttpChannel %p)\n", This, result);
163 *result = NSHTTPCHANNEL(This);
164 }else if(IsEqualGUID(&IID_nsIUploadChannel, riid)) {
165 TRACE("(%p)->(IID_nsIUploadChannel %p)\n", This, result);
166 *result = NSUPCHANNEL(This);
169 if(*result) {
170 nsIChannel_AddRef(NSCHANNEL(This));
171 return NS_OK;
174 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
176 if(This->channel)
177 return nsIChannel_QueryInterface(This->channel, riid, result);
178 return NS_NOINTERFACE;
181 static nsrefcnt NSAPI nsChannel_AddRef(nsIHttpChannel *iface)
183 nsChannel *This = NSCHANNEL_THIS(iface);
184 nsrefcnt ref = InterlockedIncrement(&This->ref);
186 TRACE("(%p) ref=%d\n", This, ref);
188 return ref;
191 static nsrefcnt NSAPI nsChannel_Release(nsIHttpChannel *iface)
193 nsChannel *This = NSCHANNEL_THIS(iface);
194 LONG ref = InterlockedDecrement(&This->ref);
196 if(!ref) {
197 nsIWineURI_Release(This->uri);
198 if(This->channel)
199 nsIChannel_Release(This->channel);
200 if(This->http_channel)
201 nsIHttpChannel_Release(This->http_channel);
202 if(This->post_data_stream)
203 nsIInputStream_Release(This->post_data_stream);
204 if(This->load_group)
205 nsILoadGroup_Release(This->load_group);
206 if(This->notif_callback)
207 nsIInterfaceRequestor_Release(This->notif_callback);
208 if(This->original_uri)
209 nsIURI_Release(This->original_uri);
210 heap_free(This->content);
211 heap_free(This->charset);
212 heap_free(This);
215 return ref;
218 static nsresult NSAPI nsChannel_GetName(nsIHttpChannel *iface, nsACString *aName)
220 nsChannel *This = NSCHANNEL_THIS(iface);
222 TRACE("(%p)->(%p)\n", This, aName);
224 if(This->channel)
225 return nsIChannel_GetName(This->channel, aName);
227 return NS_ERROR_NOT_IMPLEMENTED;
230 static nsresult NSAPI nsChannel_IsPending(nsIHttpChannel *iface, PRBool *_retval)
232 nsChannel *This = NSCHANNEL_THIS(iface);
234 TRACE("(%p)->(%p)\n", This, _retval);
236 if(This->channel)
237 return nsIChannel_IsPending(This->channel, _retval);
239 FIXME("default action not implemented\n");
240 return NS_ERROR_NOT_IMPLEMENTED;
243 static nsresult NSAPI nsChannel_GetStatus(nsIHttpChannel *iface, nsresult *aStatus)
245 nsChannel *This = NSCHANNEL_THIS(iface);
247 TRACE("(%p)->(%p)\n", This, aStatus);
249 if(This->channel)
250 return nsIChannel_GetStatus(This->channel, aStatus);
252 TRACE("returning NS_OK\n");
253 return *aStatus = NS_OK;
256 static nsresult NSAPI nsChannel_Cancel(nsIHttpChannel *iface, nsresult aStatus)
258 nsChannel *This = NSCHANNEL_THIS(iface);
260 TRACE("(%p)->(%08x)\n", This, aStatus);
262 if(This->channel)
263 return nsIChannel_Cancel(This->channel, aStatus);
265 FIXME("default action not implemented\n");
266 return NS_ERROR_NOT_IMPLEMENTED;
269 static nsresult NSAPI nsChannel_Suspend(nsIHttpChannel *iface)
271 nsChannel *This = NSCHANNEL_THIS(iface);
273 TRACE("(%p)\n", This);
275 if(This->channel)
276 return nsIChannel_Suspend(This->channel);
278 FIXME("default action not implemented\n");
279 return NS_ERROR_NOT_IMPLEMENTED;
282 static nsresult NSAPI nsChannel_Resume(nsIHttpChannel *iface)
284 nsChannel *This = NSCHANNEL_THIS(iface);
286 TRACE("(%p)\n", This);
288 if(This->channel)
289 return nsIChannel_Resume(This->channel);
291 FIXME("default action not implemented\n");
292 return NS_ERROR_NOT_IMPLEMENTED;
295 static nsresult NSAPI nsChannel_GetLoadGroup(nsIHttpChannel *iface, nsILoadGroup **aLoadGroup)
297 nsChannel *This = NSCHANNEL_THIS(iface);
299 TRACE("(%p)->(%p)\n", This, aLoadGroup);
301 if(This->load_group)
302 nsILoadGroup_AddRef(This->load_group);
304 *aLoadGroup = This->load_group;
305 return NS_OK;
308 static nsresult NSAPI nsChannel_SetLoadGroup(nsIHttpChannel *iface, nsILoadGroup *aLoadGroup)
310 nsChannel *This = NSCHANNEL_THIS(iface);
312 TRACE("(%p)->(%p)\n", This, aLoadGroup);
314 if(This->load_group)
315 nsILoadGroup_Release(This->load_group);
316 if(aLoadGroup)
317 nsILoadGroup_AddRef(aLoadGroup);
319 This->load_group = aLoadGroup;
321 if(This->channel)
322 return nsIChannel_SetLoadGroup(This->channel, aLoadGroup);
323 return NS_OK;
326 static nsresult NSAPI nsChannel_GetLoadFlags(nsIHttpChannel *iface, nsLoadFlags *aLoadFlags)
328 nsChannel *This = NSCHANNEL_THIS(iface);
330 TRACE("(%p)->(%p)\n", This, aLoadFlags);
332 *aLoadFlags = This->load_flags;
333 return NS_OK;
336 static nsresult NSAPI nsChannel_SetLoadFlags(nsIHttpChannel *iface, nsLoadFlags aLoadFlags)
338 nsChannel *This = NSCHANNEL_THIS(iface);
340 TRACE("(%p)->(%08x)\n", This, aLoadFlags);
342 This->load_flags = aLoadFlags;
344 if(This->channel)
345 return nsIChannel_SetLoadFlags(This->channel, aLoadFlags);
346 return NS_OK;
349 static nsresult NSAPI nsChannel_GetOriginalURI(nsIHttpChannel *iface, nsIURI **aOriginalURI)
351 nsChannel *This = NSCHANNEL_THIS(iface);
353 TRACE("(%p)->(%p)\n", This, aOriginalURI);
355 if(This->original_uri)
356 nsIURI_AddRef(This->original_uri);
358 *aOriginalURI = This->original_uri;
359 return NS_OK;
362 static nsresult NSAPI nsChannel_SetOriginalURI(nsIHttpChannel *iface, nsIURI *aOriginalURI)
364 nsChannel *This = NSCHANNEL_THIS(iface);
366 TRACE("(%p)->(%p)\n", This, aOriginalURI);
368 if(This->original_uri)
369 nsIURI_Release(This->original_uri);
371 nsIURI_AddRef(aOriginalURI);
372 This->original_uri = aOriginalURI;
374 if(This->channel)
375 return nsIChannel_SetOriginalURI(This->channel, aOriginalURI);
376 return NS_OK;
379 static nsresult NSAPI nsChannel_GetURI(nsIHttpChannel *iface, nsIURI **aURI)
381 nsChannel *This = NSCHANNEL_THIS(iface);
383 TRACE("(%p)->(%p)\n", This, aURI);
385 nsIWineURI_AddRef(This->uri);
386 *aURI = (nsIURI*)This->uri;
388 return NS_OK;
391 static nsresult NSAPI nsChannel_GetOwner(nsIHttpChannel *iface, nsISupports **aOwner)
393 nsChannel *This = NSCHANNEL_THIS(iface);
395 TRACE("(%p)->(%p)\n", This, aOwner);
397 if(This->channel)
398 return nsIChannel_GetOwner(This->channel, aOwner);
400 FIXME("default action not implemented\n");
401 return NS_ERROR_NOT_IMPLEMENTED;
404 static nsresult NSAPI nsChannel_SetOwner(nsIHttpChannel *iface, nsISupports *aOwner)
406 nsChannel *This = NSCHANNEL_THIS(iface);
408 TRACE("(%p)->(%p)\n", This, aOwner);
410 if(This->channel)
411 return nsIChannel_SetOwner(This->channel, aOwner);
413 FIXME("default action not implemented\n");
414 return NS_ERROR_NOT_IMPLEMENTED;
417 static nsresult NSAPI nsChannel_GetNotificationCallbacks(nsIHttpChannel *iface,
418 nsIInterfaceRequestor **aNotificationCallbacks)
420 nsChannel *This = NSCHANNEL_THIS(iface);
422 TRACE("(%p)->(%p)\n", This, aNotificationCallbacks);
424 if(This->notif_callback)
425 nsIInterfaceRequestor_AddRef(This->notif_callback);
426 *aNotificationCallbacks = This->notif_callback;
428 return NS_OK;
431 static nsresult NSAPI nsChannel_SetNotificationCallbacks(nsIHttpChannel *iface,
432 nsIInterfaceRequestor *aNotificationCallbacks)
434 nsChannel *This = NSCHANNEL_THIS(iface);
436 TRACE("(%p)->(%p)\n", This, aNotificationCallbacks);
438 if(This->notif_callback)
439 nsIInterfaceRequestor_Release(This->notif_callback);
440 if(aNotificationCallbacks)
441 nsIInterfaceRequestor_AddRef(aNotificationCallbacks);
443 This->notif_callback = aNotificationCallbacks;
445 if(This->channel)
446 return nsIChannel_SetNotificationCallbacks(This->channel, aNotificationCallbacks);
447 return NS_OK;
450 static nsresult NSAPI nsChannel_GetSecurityInfo(nsIHttpChannel *iface, nsISupports **aSecurityInfo)
452 nsChannel *This = NSCHANNEL_THIS(iface);
454 TRACE("(%p)->(%p)\n", This, aSecurityInfo);
456 if(This->channel)
457 return nsIChannel_GetSecurityInfo(This->channel, aSecurityInfo);
459 FIXME("default action not implemented\n");
460 return NS_ERROR_NOT_IMPLEMENTED;
463 static nsresult NSAPI nsChannel_GetContentType(nsIHttpChannel *iface, nsACString *aContentType)
465 nsChannel *This = NSCHANNEL_THIS(iface);
467 TRACE("(%p)->(%p)\n", This, aContentType);
469 if(This->content) {
470 nsACString_SetData(aContentType, This->content);
471 return S_OK;
474 if(This->channel)
475 return nsIChannel_GetContentType(This->channel, aContentType);
477 TRACE("returning default text/html\n");
478 nsACString_SetData(aContentType, "text/html");
479 return NS_OK;
482 static nsresult NSAPI nsChannel_SetContentType(nsIHttpChannel *iface,
483 const nsACString *aContentType)
485 nsChannel *This = NSCHANNEL_THIS(iface);
487 TRACE("(%p)->(%p)\n", This, aContentType);
489 if(This->channel)
490 return nsIChannel_SetContentType(This->channel, aContentType);
492 FIXME("default action not implemented\n");
493 return NS_ERROR_NOT_IMPLEMENTED;
496 static nsresult NSAPI nsChannel_GetContentCharset(nsIHttpChannel *iface,
497 nsACString *aContentCharset)
499 nsChannel *This = NSCHANNEL_THIS(iface);
501 TRACE("(%p)->(%p)\n", This, aContentCharset);
503 if(This->charset) {
504 nsACString_SetData(aContentCharset, This->charset);
505 return NS_OK;
508 if(This->channel) {
509 nsresult nsres = nsIChannel_GetContentCharset(This->channel, aContentCharset);
510 const char *ch;
511 nsACString_GetData(aContentCharset, &ch);
512 return nsres;
515 nsACString_SetData(aContentCharset, "");
516 return NS_OK;
519 static nsresult NSAPI nsChannel_SetContentCharset(nsIHttpChannel *iface,
520 const nsACString *aContentCharset)
522 nsChannel *This = NSCHANNEL_THIS(iface);
524 TRACE("(%p)->(%p)\n", This, aContentCharset);
526 if(This->channel)
527 return nsIChannel_SetContentCharset(This->channel, aContentCharset);
529 FIXME("default action not implemented\n");
530 return NS_ERROR_NOT_IMPLEMENTED;
533 static nsresult NSAPI nsChannel_GetContentLength(nsIHttpChannel *iface, PRInt32 *aContentLength)
535 nsChannel *This = NSCHANNEL_THIS(iface);
537 TRACE("(%p)->(%p)\n", This, aContentLength);
539 if(This->channel)
540 return nsIChannel_GetContentLength(This->channel, aContentLength);
542 FIXME("default action not implemented\n");
543 return NS_ERROR_NOT_IMPLEMENTED;
546 static nsresult NSAPI nsChannel_SetContentLength(nsIHttpChannel *iface, PRInt32 aContentLength)
548 nsChannel *This = NSCHANNEL_THIS(iface);
550 TRACE("(%p)->(%d)\n", This, aContentLength);
552 if(This->channel)
553 return nsIChannel_SetContentLength(This->channel, aContentLength);
555 FIXME("default action not implemented\n");
556 return NS_ERROR_NOT_IMPLEMENTED;
559 static nsresult NSAPI nsChannel_Open(nsIHttpChannel *iface, nsIInputStream **_retval)
561 nsChannel *This = NSCHANNEL_THIS(iface);
563 TRACE("(%p)->(%p)\n", This, _retval);
565 if(This->channel)
566 return nsIChannel_Open(This->channel, _retval);
568 FIXME("default action not implemented\n");
569 return NS_ERROR_NOT_IMPLEMENTED;
572 static BOOL do_load_from_moniker_hack(nsChannel *This)
574 nsACString scheme_str;
575 nsresult nsres;
576 BOOL ret = TRUE;
579 * We should always load the page from IMoniker, but Wine is not yet
580 * ready for this. This function is a heuristic, that decides which
581 * way of loading is better (Gecko implementation or IMoniker). The
582 * aim is to always return TRUE.
585 /* Load from moniker if there is no Gecko channel available */
586 if(!This->channel)
587 return TRUE;
589 nsACString_Init(&scheme_str, NULL);
590 nsres = nsIWineURI_GetScheme(This->uri, &scheme_str);
592 if(NS_SUCCEEDED(nsres)) {
593 const char *scheme;
595 nsACString_GetData(&scheme_str, &scheme);
596 ret = !strcmp(scheme, "wine");
599 nsACString_Finish(&scheme_str);
600 return ret;
603 static HRESULT create_mon_for_nschannel(nsChannel *channel, IMoniker **mon)
605 nsIWineURI *wine_uri;
606 LPCWSTR wine_url;
607 nsresult nsres;
608 HRESULT hres;
610 if(!channel->original_uri) {
611 ERR("original_uri == NULL\n");
612 return E_FAIL;
615 nsres = nsIURI_QueryInterface(channel->original_uri, &IID_nsIWineURI, (void**)&wine_uri);
616 if(NS_FAILED(nsres)) {
617 ERR("Could not get nsIWineURI: %08x\n", nsres);
618 return E_FAIL;
621 nsIWineURI_GetWineURL(wine_uri, &wine_url);
622 nsIWineURI_Release(wine_uri);
623 if(!wine_url) {
624 TRACE("wine_url == NULL\n");
625 return E_FAIL;
628 hres = CreateURLMoniker(NULL, wine_url, mon);
629 if(FAILED(hres))
630 WARN("CreateURLMonikrer failed: %08x\n", hres);
632 return hres;
635 static nsresult async_open_doc_uri(nsChannel *This, NSContainer *container,
636 nsIStreamListener *listener, nsISupports *context, BOOL *open)
638 IMoniker *mon;
639 HRESULT hres;
641 *open = FALSE;
643 if(container->bscallback) {
644 nsIChannel_AddRef(NSCHANNEL(This));
645 container->bscallback->nschannel = This;
647 nsIStreamListener_AddRef(listener);
648 container->bscallback->nslistener = listener;
650 if(context) {
651 nsISupports_AddRef(context);
652 container->bscallback->nscontext = context;
655 if(container->doc && container->doc->mime) {
656 DWORD len;
658 heap_free(This->content);
660 len = WideCharToMultiByte(CP_ACP, 0, container->doc->mime, -1, NULL, 0, NULL, NULL);
661 This->content = heap_alloc(len);
662 WideCharToMultiByte(CP_ACP, 0, container->doc->mime, -1, This->content, -1, NULL, NULL);
665 if(do_load_from_moniker_hack(This))
666 return WINE_NS_LOAD_FROM_MONIKER;
667 }else {
668 BOOL cont = before_async_open(This, container);
670 if(!cont) {
671 TRACE("canceled\n");
672 return NS_ERROR_UNEXPECTED;
675 if(!container->doc) {
676 return This->channel
677 ? nsIChannel_AsyncOpen(This->channel, listener, context)
678 : NS_ERROR_UNEXPECTED;
681 hres = create_mon_for_nschannel(This, &mon);
682 if(FAILED(hres)) {
683 return NS_ERROR_UNEXPECTED;
685 set_current_mon(container->doc, mon);
688 *open = TRUE;
689 return NS_OK;
692 static nsresult async_open(nsChannel *This, NSContainer *container, nsIStreamListener *listener,
693 nsISupports *context)
695 BSCallback *bscallback;
696 IMoniker *mon = NULL;
697 nsresult nsres;
698 task_t *task;
699 HRESULT hres;
701 if(This->channel) {
702 if(This->post_data_stream) {
703 nsIUploadChannel *upload_channel;
705 nsres = nsIChannel_QueryInterface(This->channel, &IID_nsIUploadChannel,
706 (void**)&upload_channel);
707 if(NS_SUCCEEDED(nsres)) {
708 nsACString empty_string;
709 nsACString_Init(&empty_string, "");
711 nsres = nsIUploadChannel_SetUploadStream(upload_channel, This->post_data_stream,
712 &empty_string, -1);
713 nsIUploadChannel_Release(upload_channel);
714 if(NS_FAILED(nsres))
715 WARN("SetUploadStream failed: %08x\n", nsres);
717 nsACString_Finish(&empty_string);
721 nsres = nsIChannel_AsyncOpen(This->channel, listener, context);
723 if(mon)
724 IMoniker_Release(mon);
726 if(NS_FAILED(nsres) && (This->load_flags & LOAD_INITIAL_DOCUMENT_URI))
727 return WINE_NS_LOAD_FROM_MONIKER;
728 return nsres;
731 TRACE("channel == NULL\n");
733 hres = create_mon_for_nschannel(This, &mon);
734 if(FAILED(hres))
735 return NS_ERROR_UNEXPECTED;
737 bscallback = create_bscallback(mon);
738 IMoniker_Release(mon);
740 nsIChannel_AddRef(NSCHANNEL(This));
741 bscallback->nschannel = This;
743 nsIStreamListener_AddRef(listener);
744 bscallback->nslistener = listener;
746 if(context) {
747 nsISupports_AddRef(context);
748 bscallback->nscontext = context;
751 task = heap_alloc(sizeof(task_t));
753 task->doc = container->doc;
754 task->task_id = TASK_START_BINDING;
755 task->next = NULL;
756 task->bscallback = bscallback;
758 push_task(task);
760 return NS_OK;
763 static nsresult NSAPI nsChannel_AsyncOpen(nsIHttpChannel *iface, nsIStreamListener *aListener,
764 nsISupports *aContext)
766 nsChannel *This = NSCHANNEL_THIS(iface);
767 NSContainer *container;
768 PRBool is_doc_uri;
769 BOOL open = TRUE;
770 nsresult nsres = NS_OK;
772 TRACE("(%p)->(%p %p)\n", This, aListener, aContext);
774 nsIWineURI_GetNSContainer(This->uri, &container);
775 if(!container) {
776 TRACE("container = NULL\n");
777 return This->channel
778 ? nsIChannel_AsyncOpen(This->channel, aListener, aContext)
779 : NS_ERROR_UNEXPECTED;
782 nsIWineURI_GetIsDocumentURI(This->uri, &is_doc_uri);
784 if(is_doc_uri && (This->load_flags & LOAD_INITIAL_DOCUMENT_URI))
785 nsres = async_open_doc_uri(This, container, aListener, aContext, &open);
787 if(open)
788 nsres = async_open(This, container, aListener, aContext);
790 nsIWebBrowserChrome_Release(NSWBCHROME(container));
791 return nsres;
794 static nsresult NSAPI nsChannel_GetRequestMethod(nsIHttpChannel *iface, nsACString *aRequestMethod)
796 nsChannel *This = NSCHANNEL_THIS(iface);
798 TRACE("(%p)->(%p)\n", This, aRequestMethod);
800 if(This->http_channel)
801 return nsIHttpChannel_GetRequestMethod(This->http_channel, aRequestMethod);
803 return NS_ERROR_NOT_IMPLEMENTED;
806 static nsresult NSAPI nsChannel_SetRequestMethod(nsIHttpChannel *iface,
807 const nsACString *aRequestMethod)
809 nsChannel *This = NSCHANNEL_THIS(iface);
811 TRACE("(%p)->(%p)\n", This, aRequestMethod);
813 if(This->http_channel)
814 return nsIHttpChannel_SetRequestMethod(This->http_channel, aRequestMethod);
816 return NS_ERROR_NOT_IMPLEMENTED;
819 static nsresult NSAPI nsChannel_GetReferrer(nsIHttpChannel *iface, nsIURI **aReferrer)
821 nsChannel *This = NSCHANNEL_THIS(iface);
823 TRACE("(%p)->(%p)\n", This, aReferrer);
825 if(This->http_channel)
826 return nsIHttpChannel_GetReferrer(This->http_channel, aReferrer);
828 return NS_ERROR_NOT_IMPLEMENTED;
831 static nsresult NSAPI nsChannel_SetReferrer(nsIHttpChannel *iface, nsIURI *aReferrer)
833 nsChannel *This = NSCHANNEL_THIS(iface);
835 TRACE("(%p)->(%p)\n", This, aReferrer);
837 if(This->http_channel)
838 return nsIHttpChannel_SetReferrer(This->http_channel, aReferrer);
840 return NS_ERROR_NOT_IMPLEMENTED;
843 static nsresult NSAPI nsChannel_GetRequestHeader(nsIHttpChannel *iface,
844 const nsACString *aHeader, nsACString *_retval)
846 nsChannel *This = NSCHANNEL_THIS(iface);
848 TRACE("(%p)->(%p %p)\n", This, aHeader, _retval);
850 if(This->http_channel)
851 return nsIHttpChannel_GetRequestHeader(This->http_channel, aHeader, _retval);
853 return NS_ERROR_NOT_IMPLEMENTED;
856 static nsresult NSAPI nsChannel_SetRequestHeader(nsIHttpChannel *iface,
857 const nsACString *aHeader, const nsACString *aValue, PRBool aMerge)
859 nsChannel *This = NSCHANNEL_THIS(iface);
861 TRACE("(%p)->(%p %p %x)\n", This, aHeader, aValue, aMerge);
863 if(This->http_channel)
864 return nsIHttpChannel_SetRequestHeader(This->http_channel, aHeader, aValue, aMerge);
866 return NS_ERROR_NOT_IMPLEMENTED;
869 static nsresult NSAPI nsChannel_VisitRequestHeaders(nsIHttpChannel *iface,
870 nsIHttpHeaderVisitor *aVisitor)
872 nsChannel *This = NSCHANNEL_THIS(iface);
874 TRACE("(%p)->(%p)\n", This, aVisitor);
876 if(This->http_channel)
877 return nsIHttpChannel_VisitRequestHeaders(This->http_channel, aVisitor);
879 return NS_ERROR_NOT_IMPLEMENTED;
882 static nsresult NSAPI nsChannel_GetAllowPipelining(nsIHttpChannel *iface, PRBool *aAllowPipelining)
884 nsChannel *This = NSCHANNEL_THIS(iface);
886 TRACE("(%p)->(%p)\n", This, aAllowPipelining);
888 if(This->http_channel)
889 return nsIHttpChannel_GetAllowPipelining(This->http_channel, aAllowPipelining);
891 return NS_ERROR_NOT_IMPLEMENTED;
894 static nsresult NSAPI nsChannel_SetAllowPipelining(nsIHttpChannel *iface, PRBool aAllowPipelining)
896 nsChannel *This = NSCHANNEL_THIS(iface);
898 TRACE("(%p)->(%x)\n", This, aAllowPipelining);
900 if(This->http_channel)
901 return nsIHttpChannel_SetAllowPipelining(This->http_channel, aAllowPipelining);
903 return NS_ERROR_NOT_IMPLEMENTED;
906 static nsresult NSAPI nsChannel_GetRedirectionLimit(nsIHttpChannel *iface, PRUint32 *aRedirectionLimit)
908 nsChannel *This = NSCHANNEL_THIS(iface);
910 TRACE("(%p)->(%p)\n", This, aRedirectionLimit);
912 if(This->http_channel)
913 return nsIHttpChannel_GetRedirectionLimit(This->http_channel, aRedirectionLimit);
915 return NS_ERROR_NOT_IMPLEMENTED;
918 static nsresult NSAPI nsChannel_SetRedirectionLimit(nsIHttpChannel *iface, PRUint32 aRedirectionLimit)
920 nsChannel *This = NSCHANNEL_THIS(iface);
922 TRACE("(%p)->(%u)\n", This, aRedirectionLimit);
924 if(This->http_channel)
925 return nsIHttpChannel_SetRedirectionLimit(This->http_channel, aRedirectionLimit);
927 return NS_ERROR_NOT_IMPLEMENTED;
930 static nsresult NSAPI nsChannel_GetResponseStatus(nsIHttpChannel *iface, PRUint32 *aResponseStatus)
932 nsChannel *This = NSCHANNEL_THIS(iface);
934 TRACE("(%p)->(%p)\n", This, aResponseStatus);
936 if(This->http_channel)
937 return nsIHttpChannel_GetResponseStatus(This->http_channel, aResponseStatus);
939 return NS_ERROR_NOT_IMPLEMENTED;
942 static nsresult NSAPI nsChannel_GetResponseStatusText(nsIHttpChannel *iface,
943 nsACString *aResponseStatusText)
945 nsChannel *This = NSCHANNEL_THIS(iface);
947 TRACE("(%p)->(%p)\n", This, aResponseStatusText);
949 if(This->http_channel)
950 return nsIHttpChannel_GetResponseStatusText(This->http_channel, aResponseStatusText);
952 return NS_ERROR_NOT_IMPLEMENTED;
955 static nsresult NSAPI nsChannel_GetRequestSucceeded(nsIHttpChannel *iface,
956 PRBool *aRequestSucceeded)
958 nsChannel *This = NSCHANNEL_THIS(iface);
960 TRACE("(%p)->(%p)\n", This, aRequestSucceeded);
962 if(This->http_channel)
963 return nsIHttpChannel_GetRequestSucceeded(This->http_channel, aRequestSucceeded);
965 return NS_ERROR_NOT_IMPLEMENTED;
968 static nsresult NSAPI nsChannel_GetResponseHeader(nsIHttpChannel *iface,
969 const nsACString *header, nsACString *_retval)
971 nsChannel *This = NSCHANNEL_THIS(iface);
973 TRACE("(%p)->(%p %p)\n", This, header, _retval);
975 if(This->http_channel)
976 return nsIHttpChannel_GetResponseHeader(This->http_channel, header, _retval);
978 return NS_ERROR_NOT_IMPLEMENTED;
981 static nsresult NSAPI nsChannel_SetResponseHeader(nsIHttpChannel *iface,
982 const nsACString *header, const nsACString *value, PRBool merge)
984 nsChannel *This = NSCHANNEL_THIS(iface);
986 TRACE("(%p)->(%p %p %x)\n", This, header, value, merge);
988 if(This->http_channel)
989 return nsIHttpChannel_SetResponseHeader(This->http_channel, header, value, merge);
991 return NS_ERROR_NOT_IMPLEMENTED;
994 static nsresult NSAPI nsChannel_VisitResponseHeaders(nsIHttpChannel *iface,
995 nsIHttpHeaderVisitor *aVisitor)
997 nsChannel *This = NSCHANNEL_THIS(iface);
999 TRACE("(%p)->(%p)\n", This, aVisitor);
1001 if(This->http_channel)
1002 return nsIHttpChannel_VisitResponseHeaders(This->http_channel, aVisitor);
1004 return NS_ERROR_NOT_IMPLEMENTED;
1007 static nsresult NSAPI nsChannel_IsNoStoreResponse(nsIHttpChannel *iface, PRBool *_retval)
1009 nsChannel *This = NSCHANNEL_THIS(iface);
1011 TRACE("(%p)->(%p)\n", This, _retval);
1013 if(This->http_channel)
1014 return nsIHttpChannel_IsNoStoreResponse(This->http_channel, _retval);
1016 return NS_ERROR_NOT_IMPLEMENTED;
1019 static nsresult NSAPI nsChannel_IsNoCacheResponse(nsIHttpChannel *iface, PRBool *_retval)
1021 nsChannel *This = NSCHANNEL_THIS(iface);
1023 TRACE("(%p)->(%p)\n", This, _retval);
1025 if(This->http_channel)
1026 return nsIHttpChannel_IsNoCacheResponse(This->http_channel, _retval);
1028 return NS_ERROR_NOT_IMPLEMENTED;
1031 #undef NSCHANNEL_THIS
1033 static const nsIHttpChannelVtbl nsChannelVtbl = {
1034 nsChannel_QueryInterface,
1035 nsChannel_AddRef,
1036 nsChannel_Release,
1037 nsChannel_GetName,
1038 nsChannel_IsPending,
1039 nsChannel_GetStatus,
1040 nsChannel_Cancel,
1041 nsChannel_Suspend,
1042 nsChannel_Resume,
1043 nsChannel_GetLoadGroup,
1044 nsChannel_SetLoadGroup,
1045 nsChannel_GetLoadFlags,
1046 nsChannel_SetLoadFlags,
1047 nsChannel_GetOriginalURI,
1048 nsChannel_SetOriginalURI,
1049 nsChannel_GetURI,
1050 nsChannel_GetOwner,
1051 nsChannel_SetOwner,
1052 nsChannel_GetNotificationCallbacks,
1053 nsChannel_SetNotificationCallbacks,
1054 nsChannel_GetSecurityInfo,
1055 nsChannel_GetContentType,
1056 nsChannel_SetContentType,
1057 nsChannel_GetContentCharset,
1058 nsChannel_SetContentCharset,
1059 nsChannel_GetContentLength,
1060 nsChannel_SetContentLength,
1061 nsChannel_Open,
1062 nsChannel_AsyncOpen,
1063 nsChannel_GetRequestMethod,
1064 nsChannel_SetRequestMethod,
1065 nsChannel_GetReferrer,
1066 nsChannel_SetReferrer,
1067 nsChannel_GetRequestHeader,
1068 nsChannel_SetRequestHeader,
1069 nsChannel_VisitRequestHeaders,
1070 nsChannel_GetAllowPipelining,
1071 nsChannel_SetAllowPipelining,
1072 nsChannel_GetRedirectionLimit,
1073 nsChannel_SetRedirectionLimit,
1074 nsChannel_GetResponseStatus,
1075 nsChannel_GetResponseStatusText,
1076 nsChannel_GetRequestSucceeded,
1077 nsChannel_GetResponseHeader,
1078 nsChannel_SetResponseHeader,
1079 nsChannel_VisitResponseHeaders,
1080 nsChannel_IsNoStoreResponse,
1081 nsChannel_IsNoCacheResponse
1084 #define NSUPCHANNEL_THIS(iface) DEFINE_THIS(nsChannel, UploadChannel, iface)
1086 static nsresult NSAPI nsUploadChannel_QueryInterface(nsIUploadChannel *iface, nsIIDRef riid,
1087 nsQIResult result)
1089 nsChannel *This = NSUPCHANNEL_THIS(iface);
1090 return nsIChannel_QueryInterface(NSCHANNEL(This), riid, result);
1093 static nsrefcnt NSAPI nsUploadChannel_AddRef(nsIUploadChannel *iface)
1095 nsChannel *This = NSUPCHANNEL_THIS(iface);
1096 return nsIChannel_AddRef(NSCHANNEL(This));
1099 static nsrefcnt NSAPI nsUploadChannel_Release(nsIUploadChannel *iface)
1101 nsChannel *This = NSUPCHANNEL_THIS(iface);
1102 return nsIChannel_Release(NSCHANNEL(This));
1105 static nsresult NSAPI nsUploadChannel_SetUploadStream(nsIUploadChannel *iface,
1106 nsIInputStream *aStream, const nsACString *aContentType, PRInt32 aContentLength)
1108 nsChannel *This = NSUPCHANNEL_THIS(iface);
1109 const char *content_type;
1111 TRACE("(%p)->(%p %p %d)\n", This, aStream, aContentType, aContentLength);
1113 if(This->post_data_stream)
1114 nsIInputStream_Release(This->post_data_stream);
1116 if(aContentType) {
1117 nsACString_GetData(aContentType, &content_type);
1118 if(*content_type)
1119 FIXME("Unsupported aContentType argument: %s\n", debugstr_a(content_type));
1122 if(aContentLength != -1)
1123 FIXME("Unsupported acontentLength = %d\n", aContentLength);
1125 if(aStream)
1126 nsIInputStream_AddRef(aStream);
1127 This->post_data_stream = aStream;
1129 return NS_OK;
1132 static nsresult NSAPI nsUploadChannel_GetUploadStream(nsIUploadChannel *iface,
1133 nsIInputStream **aUploadStream)
1135 nsChannel *This = NSUPCHANNEL_THIS(iface);
1137 TRACE("(%p)->(%p)\n", This, aUploadStream);
1139 if(This->post_data_stream)
1140 nsIInputStream_AddRef(This->post_data_stream);
1142 *aUploadStream = This->post_data_stream;
1143 return NS_OK;
1146 #undef NSUPCHANNEL_THIS
1148 static const nsIUploadChannelVtbl nsUploadChannelVtbl = {
1149 nsUploadChannel_QueryInterface,
1150 nsUploadChannel_AddRef,
1151 nsUploadChannel_Release,
1152 nsUploadChannel_SetUploadStream,
1153 nsUploadChannel_GetUploadStream
1156 #define NSURI_THIS(iface) DEFINE_THIS(nsURI, WineURI, iface)
1158 static nsresult NSAPI nsURI_QueryInterface(nsIWineURI *iface, nsIIDRef riid, nsQIResult result)
1160 nsURI *This = NSURI_THIS(iface);
1162 *result = NULL;
1164 if(IsEqualGUID(&IID_nsISupports, riid)) {
1165 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1166 *result = NSURI(This);
1167 }else if(IsEqualGUID(&IID_nsIURI, riid)) {
1168 TRACE("(%p)->(IID_nsIURI %p)\n", This, result);
1169 *result = NSURI(This);
1170 }else if(IsEqualGUID(&IID_nsIWineURI, riid)) {
1171 TRACE("(%p)->(IID_nsIWineURI %p)\n", This, result);
1172 *result = NSURI(This);
1175 if(*result) {
1176 nsIURI_AddRef(NSURI(This));
1177 return NS_OK;
1180 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1181 return This->uri ? nsIURI_QueryInterface(This->uri, riid, result) : NS_NOINTERFACE;
1184 static nsrefcnt NSAPI nsURI_AddRef(nsIWineURI *iface)
1186 nsURI *This = NSURI_THIS(iface);
1187 LONG ref = InterlockedIncrement(&This->ref);
1189 TRACE("(%p) ref=%d\n", This, ref);
1191 return ref;
1194 static nsrefcnt NSAPI nsURI_Release(nsIWineURI *iface)
1196 nsURI *This = NSURI_THIS(iface);
1197 LONG ref = InterlockedDecrement(&This->ref);
1199 TRACE("(%p) ref=%d\n", This, ref);
1201 if(!ref) {
1202 if(This->container)
1203 nsIWebBrowserChrome_Release(NSWBCHROME(This->container));
1204 if(This->uri)
1205 nsIURI_Release(This->uri);
1206 heap_free(This->wine_url);
1207 heap_free(This);
1210 return ref;
1213 static nsresult NSAPI nsURI_GetSpec(nsIWineURI *iface, nsACString *aSpec)
1215 nsURI *This = NSURI_THIS(iface);
1217 TRACE("(%p)->(%p)\n", This, aSpec);
1219 if(This->use_wine_url) {
1220 char speca[INTERNET_MAX_URL_LENGTH] = "wine:";
1221 WideCharToMultiByte(CP_ACP, 0, This->wine_url, -1, speca+5, sizeof(speca)-5, NULL, NULL);
1222 nsACString_SetData(aSpec, speca);
1224 return NS_OK;
1227 if(This->uri)
1228 return nsIURI_GetSpec(This->uri, aSpec);
1230 TRACE("returning error\n");
1231 return NS_ERROR_NOT_IMPLEMENTED;
1235 static nsresult NSAPI nsURI_SetSpec(nsIWineURI *iface, const nsACString *aSpec)
1237 nsURI *This = NSURI_THIS(iface);
1239 TRACE("(%p)->(%p)\n", This, aSpec);
1241 if(This->uri)
1242 return nsIURI_SetSpec(This->uri, aSpec);
1244 FIXME("default action not implemented\n");
1245 return NS_ERROR_NOT_IMPLEMENTED;
1248 static nsresult NSAPI nsURI_GetPrePath(nsIWineURI *iface, nsACString *aPrePath)
1250 nsURI *This = NSURI_THIS(iface);
1252 TRACE("(%p)->(%p)\n", This, aPrePath);
1254 if(This->uri)
1255 return nsIURI_GetPrePath(This->uri, aPrePath);
1257 FIXME("default action not implemented\n");
1258 return NS_ERROR_NOT_IMPLEMENTED;
1261 static nsresult NSAPI nsURI_GetScheme(nsIWineURI *iface, nsACString *aScheme)
1263 nsURI *This = NSURI_THIS(iface);
1265 TRACE("(%p)->(%p)\n", This, aScheme);
1267 if(This->use_wine_url) {
1269 * For Gecko we set scheme to unknown so it won't be handled
1270 * as any special case.
1272 nsACString_SetData(aScheme, "wine");
1273 return NS_OK;
1276 if(This->uri)
1277 return nsIURI_GetScheme(This->uri, aScheme);
1279 TRACE("returning error\n");
1280 return NS_ERROR_NOT_IMPLEMENTED;
1283 static nsresult NSAPI nsURI_SetScheme(nsIWineURI *iface, const nsACString *aScheme)
1285 nsURI *This = NSURI_THIS(iface);
1287 TRACE("(%p)->(%p)\n", This, aScheme);
1289 if(This->uri)
1290 return nsIURI_SetScheme(This->uri, aScheme);
1292 FIXME("default action not implemented\n");
1293 return NS_ERROR_NOT_IMPLEMENTED;
1296 static nsresult NSAPI nsURI_GetUserPass(nsIWineURI *iface, nsACString *aUserPass)
1298 nsURI *This = NSURI_THIS(iface);
1300 TRACE("(%p)->(%p)\n", This, aUserPass);
1302 if(This->uri)
1303 return nsIURI_GetUserPass(This->uri, aUserPass);
1305 FIXME("default action not implemented\n");
1306 return NS_ERROR_NOT_IMPLEMENTED;
1309 static nsresult NSAPI nsURI_SetUserPass(nsIWineURI *iface, const nsACString *aUserPass)
1311 nsURI *This = NSURI_THIS(iface);
1313 TRACE("(%p)->(%p)\n", This, aUserPass);
1315 if(This->uri)
1316 return nsIURI_SetUserPass(This->uri, aUserPass);
1318 FIXME("default action not implemented\n");
1319 return NS_ERROR_NOT_IMPLEMENTED;
1322 static nsresult NSAPI nsURI_GetUsername(nsIWineURI *iface, nsACString *aUsername)
1324 nsURI *This = NSURI_THIS(iface);
1326 TRACE("(%p)->(%p)\n", This, aUsername);
1328 if(This->uri)
1329 return nsIURI_GetUsername(This->uri, aUsername);
1331 FIXME("default action not implemented\n");
1332 return NS_ERROR_NOT_IMPLEMENTED;
1335 static nsresult NSAPI nsURI_SetUsername(nsIWineURI *iface, const nsACString *aUsername)
1337 nsURI *This = NSURI_THIS(iface);
1339 TRACE("(%p)->(%p)\n", This, aUsername);
1341 if(This->uri)
1342 return nsIURI_SetUsername(This->uri, aUsername);
1344 FIXME("default action not implemented\n");
1345 return NS_ERROR_NOT_IMPLEMENTED;
1348 static nsresult NSAPI nsURI_GetPassword(nsIWineURI *iface, nsACString *aPassword)
1350 nsURI *This = NSURI_THIS(iface);
1352 TRACE("(%p)->(%p)\n", This, aPassword);
1354 if(This->uri)
1355 return nsIURI_GetPassword(This->uri, aPassword);
1357 FIXME("default action not implemented\n");
1358 return NS_ERROR_NOT_IMPLEMENTED;
1361 static nsresult NSAPI nsURI_SetPassword(nsIWineURI *iface, const nsACString *aPassword)
1363 nsURI *This = NSURI_THIS(iface);
1365 TRACE("(%p)->(%p)\n", This, aPassword);
1367 if(This->uri)
1368 return nsIURI_SetPassword(This->uri, aPassword);
1370 FIXME("default action not implemented\n");
1371 return NS_ERROR_NOT_IMPLEMENTED;
1374 static nsresult NSAPI nsURI_GetHostPort(nsIWineURI *iface, nsACString *aHostPort)
1376 nsURI *This = NSURI_THIS(iface);
1378 TRACE("(%p)->(%p)\n", This, aHostPort);
1380 if(This->uri)
1381 return nsIURI_GetHostPort(This->uri, aHostPort);
1383 FIXME("default action not implemented\n");
1384 return NS_ERROR_NOT_IMPLEMENTED;
1387 static nsresult NSAPI nsURI_SetHostPort(nsIWineURI *iface, const nsACString *aHostPort)
1389 nsURI *This = NSURI_THIS(iface);
1391 TRACE("(%p)->(%p)\n", This, aHostPort);
1393 if(This->uri)
1394 return nsIURI_SetHostPort(This->uri, aHostPort);
1396 FIXME("default action not implemented\n");
1397 return NS_ERROR_NOT_IMPLEMENTED;
1400 static nsresult NSAPI nsURI_GetHost(nsIWineURI *iface, nsACString *aHost)
1402 nsURI *This = NSURI_THIS(iface);
1404 TRACE("(%p)->(%p)\n", This, aHost);
1406 if(This->uri)
1407 return nsIURI_GetHost(This->uri, aHost);
1409 FIXME("default action not implemented\n");
1410 return NS_ERROR_NOT_IMPLEMENTED;
1413 static nsresult NSAPI nsURI_SetHost(nsIWineURI *iface, const nsACString *aHost)
1415 nsURI *This = NSURI_THIS(iface);
1417 TRACE("(%p)->(%p)\n", This, aHost);
1419 if(This->uri)
1420 return nsIURI_SetHost(This->uri, aHost);
1422 FIXME("default action not implemented\n");
1423 return NS_ERROR_NOT_IMPLEMENTED;
1426 static nsresult NSAPI nsURI_GetPort(nsIWineURI *iface, PRInt32 *aPort)
1428 nsURI *This = NSURI_THIS(iface);
1430 TRACE("(%p)->(%p)\n", This, aPort);
1432 if(This->uri)
1433 return nsIURI_GetPort(This->uri, aPort);
1435 FIXME("default action not implemented\n");
1436 return NS_ERROR_NOT_IMPLEMENTED;
1439 static nsresult NSAPI nsURI_SetPort(nsIWineURI *iface, PRInt32 aPort)
1441 nsURI *This = NSURI_THIS(iface);
1443 TRACE("(%p)->(%d)\n", This, aPort);
1445 if(This->uri)
1446 return nsIURI_SetPort(This->uri, aPort);
1448 FIXME("default action not implemented\n");
1449 return NS_ERROR_NOT_IMPLEMENTED;
1452 static nsresult NSAPI nsURI_GetPath(nsIWineURI *iface, nsACString *aPath)
1454 nsURI *This = NSURI_THIS(iface);
1456 TRACE("(%p)->(%p)\n", This, aPath);
1458 if(This->uri)
1459 return nsIURI_GetPath(This->uri, aPath);
1461 FIXME("default action not implemented\n");
1462 return NS_ERROR_NOT_IMPLEMENTED;
1465 static nsresult NSAPI nsURI_SetPath(nsIWineURI *iface, const nsACString *aPath)
1467 nsURI *This = NSURI_THIS(iface);
1469 TRACE("(%p)->(%p)\n", This, aPath);
1471 if(This->uri)
1472 return nsIURI_SetPath(This->uri, aPath);
1474 FIXME("default action not implemented\n");
1475 return NS_ERROR_NOT_IMPLEMENTED;
1478 static nsresult NSAPI nsURI_Equals(nsIWineURI *iface, nsIURI *other, PRBool *_retval)
1480 nsURI *This = NSURI_THIS(iface);
1482 TRACE("(%p)->(%p %p)\n", This, other, _retval);
1484 if(This->uri)
1485 return nsIURI_Equals(This->uri, other, _retval);
1487 FIXME("default action not implemented\n");
1488 return NS_ERROR_NOT_IMPLEMENTED;
1491 static nsresult NSAPI nsURI_SchemeIs(nsIWineURI *iface, const char *scheme, PRBool *_retval)
1493 nsURI *This = NSURI_THIS(iface);
1495 TRACE("(%p)->(%s %p)\n", This, debugstr_a(scheme), _retval);
1497 if(This->use_wine_url) {
1498 WCHAR buf[INTERNET_MAX_SCHEME_LENGTH];
1499 int len = MultiByteToWideChar(CP_ACP, 0, scheme, -1, buf, sizeof(buf)/sizeof(WCHAR))-1;
1501 *_retval = strlenW(This->wine_url) > len
1502 && This->wine_url[len] == ':'
1503 && !memcmp(buf, This->wine_url, len*sizeof(WCHAR));
1504 return NS_OK;
1507 if(This->uri)
1508 return nsIURI_SchemeIs(This->uri, scheme, _retval);
1510 TRACE("returning error\n");
1511 return NS_ERROR_NOT_IMPLEMENTED;
1514 static nsresult NSAPI nsURI_Clone(nsIWineURI *iface, nsIURI **_retval)
1516 nsURI *This = NSURI_THIS(iface);
1518 TRACE("(%p)->(%p)\n", This, _retval);
1520 if(This->uri) {
1521 nsIURI *uri;
1522 nsIWineURI *wine_uri;
1523 nsresult nsres;
1525 nsres = nsIURI_Clone(This->uri, &uri);
1526 if(NS_FAILED(nsres)) {
1527 WARN("Clone failed: %08x\n", nsres);
1528 return nsres;
1531 nsres = create_uri(uri, This->container, &wine_uri);
1532 *_retval = (nsIURI*)wine_uri;
1533 if(NS_SUCCEEDED(nsres))
1534 return nsIWineURI_SetWineURL(wine_uri, This->wine_url);
1535 return nsres;
1538 FIXME("default action not implemented\n");
1539 return NS_ERROR_NOT_IMPLEMENTED;
1542 static nsresult NSAPI nsURI_Resolve(nsIWineURI *iface, const nsACString *arelativePath,
1543 nsACString *_retval)
1545 nsURI *This = NSURI_THIS(iface);
1547 TRACE("(%p)->(%p %p)\n", This, arelativePath, _retval);
1549 if(This->uri)
1550 return nsIURI_Resolve(This->uri, arelativePath, _retval);
1552 FIXME("default action not implemented\n");
1553 return NS_ERROR_NOT_IMPLEMENTED;
1556 static nsresult NSAPI nsURI_GetAsciiSpec(nsIWineURI *iface, nsACString *aAsciiSpec)
1558 nsURI *This = NSURI_THIS(iface);
1560 TRACE("(%p)->(%p)\n", This, aAsciiSpec);
1562 if(This->use_wine_url)
1563 return nsIURI_GetSpec(NSURI(This), aAsciiSpec);
1565 if(This->uri)
1566 return nsIURI_GetAsciiSpec(This->uri, aAsciiSpec);
1568 TRACE("returning error\n");
1569 return NS_ERROR_NOT_IMPLEMENTED;
1572 static nsresult NSAPI nsURI_GetAsciiHost(nsIWineURI *iface, nsACString *aAsciiHost)
1574 nsURI *This = NSURI_THIS(iface);
1576 TRACE("(%p)->(%p)\n", This, aAsciiHost);
1578 if(This->uri)
1579 return nsIURI_GetAsciiHost(This->uri, aAsciiHost);
1581 FIXME("default action not implemented\n");
1582 return NS_ERROR_NOT_IMPLEMENTED;
1585 static nsresult NSAPI nsURI_GetOriginCharset(nsIWineURI *iface, nsACString *aOriginCharset)
1587 nsURI *This = NSURI_THIS(iface);
1589 TRACE("(%p)->(%p)\n", This, aOriginCharset);
1591 if(This->uri)
1592 return nsIURI_GetOriginCharset(This->uri, aOriginCharset);
1594 FIXME("default action not implemented\n");
1595 return NS_ERROR_NOT_IMPLEMENTED;
1598 static nsresult NSAPI nsURI_GetNSContainer(nsIWineURI *iface, NSContainer **aContainer)
1600 nsURI *This = NSURI_THIS(iface);
1602 TRACE("(%p)->(%p)\n", This, aContainer);
1604 if(This->container)
1605 nsIWebBrowserChrome_AddRef(NSWBCHROME(This->container));
1606 *aContainer = This->container;
1608 return NS_OK;
1611 static nsresult NSAPI nsURI_SetNSContainer(nsIWineURI *iface, NSContainer *aContainer)
1613 nsURI *This = NSURI_THIS(iface);
1615 TRACE("(%p)->(%p)\n", This, aContainer);
1617 if(This->container) {
1618 if(This->container == aContainer)
1619 return NS_OK;
1620 TRACE("Changing %p -> %p\n", This->container, aContainer);
1621 nsIWebBrowserChrome_Release(NSWBCHROME(This->container));
1624 if(aContainer)
1625 nsIWebBrowserChrome_AddRef(NSWBCHROME(aContainer));
1626 This->container = aContainer;
1628 return NS_OK;
1631 static nsresult NSAPI nsURI_GetIsDocumentURI(nsIWineURI *iface, PRBool *aIsDocumentURI)
1633 nsURI *This = NSURI_THIS(iface);
1635 TRACE("(%p)->(%p)\n", This, aIsDocumentURI);
1637 *aIsDocumentURI = This->is_doc_uri;
1638 return NS_OK;
1641 static nsresult NSAPI nsURI_SetIsDocumentURI(nsIWineURI *iface, PRBool aIsDocumentURI)
1643 nsURI *This = NSURI_THIS(iface);
1645 TRACE("(%p)->(%x)\n", This, aIsDocumentURI);
1647 This->is_doc_uri = aIsDocumentURI;
1648 return NS_OK;
1651 static nsresult NSAPI nsURI_GetWineURL(nsIWineURI *iface, LPCWSTR *aURL)
1653 nsURI *This = NSURI_THIS(iface);
1655 TRACE("(%p)->(%p)\n", This, aURL);
1657 *aURL = This->wine_url;
1658 return NS_OK;
1661 static nsresult NSAPI nsURI_SetWineURL(nsIWineURI *iface, LPCWSTR aURL)
1663 nsURI *This = NSURI_THIS(iface);
1665 static const WCHAR wszFtp[] = {'f','t','p',':'};
1666 static const WCHAR wszHttp[] = {'h','t','t','p',':'};
1667 static const WCHAR wszHttps[] = {'h','t','t','p','s',':'};
1669 TRACE("(%p)->(%s)\n", This, debugstr_w(aURL));
1671 heap_free(This->wine_url);
1673 if(aURL) {
1674 int len = strlenW(aURL)+1;
1675 This->wine_url = heap_alloc(len*sizeof(WCHAR));
1676 memcpy(This->wine_url, aURL, len*sizeof(WCHAR));
1678 /* FIXME: Always use wine url */
1679 This->use_wine_url =
1680 strncmpW(aURL, wszFtp, sizeof(wszFtp)/sizeof(WCHAR))
1681 && strncmpW(aURL, wszHttp, sizeof(wszHttp)/sizeof(WCHAR))
1682 && strncmpW(aURL, wszHttps, sizeof(wszHttps)/sizeof(WCHAR));
1683 }else {
1684 This->wine_url = NULL;
1685 This->use_wine_url = FALSE;
1688 return NS_OK;
1691 #undef NSURI_THIS
1693 static const nsIWineURIVtbl nsWineURIVtbl = {
1694 nsURI_QueryInterface,
1695 nsURI_AddRef,
1696 nsURI_Release,
1697 nsURI_GetSpec,
1698 nsURI_SetSpec,
1699 nsURI_GetPrePath,
1700 nsURI_GetScheme,
1701 nsURI_SetScheme,
1702 nsURI_GetUserPass,
1703 nsURI_SetUserPass,
1704 nsURI_GetUsername,
1705 nsURI_SetUsername,
1706 nsURI_GetPassword,
1707 nsURI_SetPassword,
1708 nsURI_GetHostPort,
1709 nsURI_SetHostPort,
1710 nsURI_GetHost,
1711 nsURI_SetHost,
1712 nsURI_GetPort,
1713 nsURI_SetPort,
1714 nsURI_GetPath,
1715 nsURI_SetPath,
1716 nsURI_Equals,
1717 nsURI_SchemeIs,
1718 nsURI_Clone,
1719 nsURI_Resolve,
1720 nsURI_GetAsciiSpec,
1721 nsURI_GetAsciiHost,
1722 nsURI_GetOriginCharset,
1723 nsURI_GetNSContainer,
1724 nsURI_SetNSContainer,
1725 nsURI_GetIsDocumentURI,
1726 nsURI_SetIsDocumentURI,
1727 nsURI_GetWineURL,
1728 nsURI_SetWineURL
1731 static nsresult create_uri(nsIURI *uri, NSContainer *container, nsIWineURI **_retval)
1733 nsURI *ret = heap_alloc(sizeof(nsURI));
1735 ret->lpWineURIVtbl = &nsWineURIVtbl;
1736 ret->ref = 1;
1737 ret->uri = uri;
1738 ret->container = container;
1739 ret->wine_url = NULL;
1740 ret->is_doc_uri = FALSE;
1741 ret->use_wine_url = FALSE;
1743 if(container)
1744 nsIWebBrowserChrome_AddRef(NSWBCHROME(container));
1746 TRACE("retval=%p\n", ret);
1747 *_retval = NSWINEURI(ret);
1748 return NS_OK;
1751 typedef struct {
1752 const nsIProtocolHandlerVtbl *lpProtocolHandlerVtbl;
1754 LONG ref;
1756 nsIProtocolHandler *nshandler;
1757 } nsProtocolHandler;
1759 #define NSPROTHANDLER(x) ((nsIProtocolHandler*) &(x)->lpProtocolHandlerVtbl)
1761 #define NSPROTHANDLER_THIS(iface) DEFINE_THIS(nsProtocolHandler, ProtocolHandler, iface)
1763 static nsresult NSAPI nsProtocolHandler_QueryInterface(nsIProtocolHandler *iface, nsIIDRef riid,
1764 nsQIResult result)
1766 nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1768 *result = NULL;
1770 if(IsEqualGUID(&IID_nsISupports, riid)) {
1771 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1772 *result = NSPROTHANDLER(This);
1773 }else if(IsEqualGUID(&IID_nsIProtocolHandler, riid)) {
1774 TRACE("(%p)->(IID_nsIProtocolHandler %p)\n", This, result);
1775 *result = NSPROTHANDLER(This);
1776 }else if(IsEqualGUID(&IID_nsIExternalProtocolHandler, riid)) {
1777 TRACE("(%p)->(IID_nsIExternalProtocolHandler %p), returning NULL\n", This, result);
1778 return NS_NOINTERFACE;
1781 if(*result) {
1782 nsISupports_AddRef((nsISupports*)*result);
1783 return NS_OK;
1786 WARN("(%s %p)\n", debugstr_guid(riid), result);
1787 return NS_NOINTERFACE;
1790 static nsrefcnt NSAPI nsProtocolHandler_AddRef(nsIProtocolHandler *iface)
1792 nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1793 LONG ref = InterlockedIncrement(&This->ref);
1795 TRACE("(%p) ref=%d\n", This, ref);
1797 return ref;
1800 static nsrefcnt NSAPI nsProtocolHandler_Release(nsIProtocolHandler *iface)
1802 nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1803 LONG ref = InterlockedDecrement(&This->ref);
1805 TRACE("(%p) ref=%d\n", This, ref);
1807 if(!ref) {
1808 if(This->nshandler)
1809 nsIProtocolHandler_Release(This->nshandler);
1810 heap_free(This);
1813 return ref;
1816 static nsresult NSAPI nsProtocolHandler_GetScheme(nsIProtocolHandler *iface, nsACString *aScheme)
1818 nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1820 TRACE("(%p)->(%p)\n", This, aScheme);
1822 if(This->nshandler)
1823 return nsIProtocolHandler_GetScheme(This->nshandler, aScheme);
1824 return NS_ERROR_NOT_IMPLEMENTED;
1827 static nsresult NSAPI nsProtocolHandler_GetDefaultPort(nsIProtocolHandler *iface,
1828 PRInt32 *aDefaultPort)
1830 nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1832 TRACE("(%p)->(%p)\n", This, aDefaultPort);
1834 if(This->nshandler)
1835 return nsIProtocolHandler_GetDefaultPort(This->nshandler, aDefaultPort);
1836 return NS_ERROR_NOT_IMPLEMENTED;
1839 static nsresult NSAPI nsProtocolHandler_GetProtocolFlags(nsIProtocolHandler *iface,
1840 PRUint32 *aProtocolFlags)
1842 nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1844 TRACE("(%p)->(%p)\n", This, aProtocolFlags);
1846 if(This->nshandler)
1847 return nsIProtocolHandler_GetProtocolFlags(This->nshandler, aProtocolFlags);
1848 return NS_ERROR_NOT_IMPLEMENTED;
1851 static nsresult NSAPI nsProtocolHandler_NewURI(nsIProtocolHandler *iface,
1852 const nsACString *aSpec, const char *aOriginCharset, nsIURI *aBaseURI, nsIURI **_retval)
1854 nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1856 TRACE("((%p)->%p %s %p %p)\n", This, aSpec, debugstr_a(aOriginCharset), aBaseURI, _retval);
1858 if(This->nshandler)
1859 return nsIProtocolHandler_NewURI(This->nshandler, aSpec, aOriginCharset, aBaseURI, _retval);
1860 return NS_ERROR_NOT_IMPLEMENTED;
1863 static nsresult NSAPI nsProtocolHandler_NewChannel(nsIProtocolHandler *iface,
1864 nsIURI *aURI, nsIChannel **_retval)
1866 nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1868 TRACE("(%p)->(%p %p)\n", This, aURI, _retval);
1870 if(This->nshandler)
1871 return nsIProtocolHandler_NewChannel(This->nshandler, aURI, _retval);
1872 return NS_ERROR_NOT_IMPLEMENTED;
1875 static nsresult NSAPI nsProtocolHandler_AllowPort(nsIProtocolHandler *iface,
1876 PRInt32 port, const char *scheme, PRBool *_retval)
1878 nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1880 TRACE("(%p)->(%d %s %p)\n", This, port, debugstr_a(scheme), _retval);
1882 if(This->nshandler)
1883 return nsIProtocolHandler_AllowPort(This->nshandler, port, scheme, _retval);
1884 return NS_ERROR_NOT_IMPLEMENTED;
1887 #undef NSPROTHANDLER_THIS
1889 static const nsIProtocolHandlerVtbl nsProtocolHandlerVtbl = {
1890 nsProtocolHandler_QueryInterface,
1891 nsProtocolHandler_AddRef,
1892 nsProtocolHandler_Release,
1893 nsProtocolHandler_GetScheme,
1894 nsProtocolHandler_GetDefaultPort,
1895 nsProtocolHandler_GetProtocolFlags,
1896 nsProtocolHandler_NewURI,
1897 nsProtocolHandler_NewChannel,
1898 nsProtocolHandler_AllowPort
1901 static nsIProtocolHandler *create_protocol_handler(nsIProtocolHandler *nshandler)
1903 nsProtocolHandler *ret = heap_alloc(sizeof(nsProtocolHandler));
1905 ret->lpProtocolHandlerVtbl = &nsProtocolHandlerVtbl;
1906 ret->ref = 1;
1907 ret->nshandler = nshandler;
1909 return NSPROTHANDLER(ret);
1912 static nsresult NSAPI nsIOService_QueryInterface(nsIIOService *iface, nsIIDRef riid,
1913 nsQIResult result)
1915 *result = NULL;
1917 if(IsEqualGUID(&IID_nsISupports, riid)
1918 || IsEqualGUID(&IID_nsIIOService, riid)) {
1919 *result = iface;
1920 nsIIOService_AddRef(iface);
1921 return S_OK;
1924 WARN("(%s %p)\n", debugstr_guid(riid), result);
1925 return NS_NOINTERFACE;
1928 static nsrefcnt NSAPI nsIOService_AddRef(nsIIOService *iface)
1930 return 2;
1933 static nsrefcnt NSAPI nsIOService_Release(nsIIOService *iface)
1935 return 1;
1938 static nsresult NSAPI nsIOService_GetProtocolHandler(nsIIOService *iface, const char *aScheme,
1939 nsIProtocolHandler **_retval)
1941 nsIExternalProtocolHandler *nsexthandler;
1942 nsIProtocolHandler *nshandler;
1943 nsresult nsres;
1945 TRACE("(%s %p)\n", debugstr_a(aScheme), _retval);
1947 nsres = nsIIOService_GetProtocolHandler(nsio, aScheme, &nshandler);
1948 if(NS_FAILED(nsres)) {
1949 WARN("GetProtocolHandler failed: %08x\n", nsres);
1950 return nsres;
1953 nsres = nsIProtocolHandler_QueryInterface(nshandler, &IID_nsIExternalProtocolHandler,
1954 (void**)&nsexthandler);
1955 if(NS_FAILED(nsres)) {
1956 *_retval = nshandler;
1957 return NS_OK;
1960 nsIExternalProtocolHandler_Release(nsexthandler);
1961 *_retval = create_protocol_handler(nshandler);
1962 TRACE("return %p\n", *_retval);
1963 return NS_OK;
1966 static nsresult NSAPI nsIOService_GetProtocolFlags(nsIIOService *iface, const char *aScheme,
1967 PRUint32 *_retval)
1969 TRACE("(%s %p)\n", debugstr_a(aScheme), _retval);
1970 return nsIIOService_GetProtocolFlags(nsio, aScheme, _retval);
1973 static BOOL is_gecko_special_uri(const char *spec)
1975 static const char chromeW[] = "chrome:";
1976 static const char jarW[] = "jar:";
1977 static const char resourceW[] = "resource:";
1978 static const char javascriptW[] = "javascript:";
1980 return !strncasecmp(spec, chromeW, sizeof(chromeW)-1)
1981 || !strncasecmp(spec, resourceW, sizeof(resourceW)-1)
1982 || !strncasecmp(spec, jarW, sizeof(jarW)-1)
1983 || !strncasecmp(spec, javascriptW, sizeof(javascriptW)-1);
1986 static nsresult NSAPI nsIOService_NewURI(nsIIOService *iface, const nsACString *aSpec,
1987 const char *aOriginCharset, nsIURI *aBaseURI, nsIURI **_retval)
1989 const char *spec = NULL;
1990 nsACString spec_str;
1991 NSContainer *nscontainer = NULL;
1992 nsIURI *uri = NULL;
1993 LPCWSTR base_wine_url = NULL;
1994 nsIWineURI *base_wine_uri = NULL, *wine_uri;
1995 BOOL is_wine_uri = FALSE;
1996 nsresult nsres;
1998 nsACString_GetData(aSpec, &spec);
2000 TRACE("(%p(%s) %s %p %p)\n", aSpec, debugstr_a(spec), debugstr_a(aOriginCharset),
2001 aBaseURI, _retval);
2003 if(is_gecko_special_uri(spec))
2004 return nsIIOService_NewURI(nsio, aSpec, aOriginCharset, aBaseURI, _retval);
2006 if(!strncmp(spec, "wine:", 5)) {
2007 spec += 5;
2008 is_wine_uri = TRUE;
2011 if(aBaseURI) {
2012 nsACString base_uri_str;
2013 const char *base_uri = NULL;
2015 nsACString_Init(&base_uri_str, NULL);
2017 nsres = nsIURI_GetSpec(aBaseURI, &base_uri_str);
2018 if(NS_SUCCEEDED(nsres)) {
2019 nsACString_GetData(&base_uri_str, &base_uri);
2020 TRACE("base_uri=%s\n", debugstr_a(base_uri));
2021 }else {
2022 ERR("GetSpec failed: %08x\n", nsres);
2025 nsACString_Finish(&base_uri_str);
2028 nsACString_Init(&spec_str, spec);
2029 nsres = nsIIOService_NewURI(nsio, aSpec, aOriginCharset, aBaseURI, &uri);
2030 nsACString_Finish(&spec_str);
2031 if(NS_FAILED(nsres))
2032 TRACE("NewURI failed: %08x\n", nsres);
2034 if(aBaseURI) {
2035 nsres = nsIURI_QueryInterface(aBaseURI, &IID_nsIWineURI, (void**)&base_wine_uri);
2036 if(NS_SUCCEEDED(nsres)) {
2037 nsIWineURI_GetNSContainer(base_wine_uri, &nscontainer);
2038 nsIWineURI_GetWineURL(base_wine_uri, &base_wine_url);
2039 }else {
2040 TRACE("Could not get base nsIWineURI: %08x\n", nsres);
2044 nsres = create_uri(uri, nscontainer, &wine_uri);
2045 *_retval = (nsIURI*)wine_uri;
2047 if(nscontainer)
2048 nsIWebBrowserChrome_Release(NSWBCHROME(nscontainer));
2050 if(base_wine_url) {
2051 WCHAR url[INTERNET_MAX_URL_LENGTH], rel_url[INTERNET_MAX_URL_LENGTH];
2052 DWORD len;
2053 HRESULT hres;
2055 MultiByteToWideChar(CP_ACP, 0, spec, -1, rel_url, sizeof(rel_url)/sizeof(WCHAR));
2057 hres = CoInternetCombineUrl(base_wine_url, rel_url,
2058 URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO,
2059 url, sizeof(url)/sizeof(WCHAR), &len, 0);
2060 if(SUCCEEDED(hres))
2061 nsIWineURI_SetWineURL(wine_uri, url);
2062 else
2063 WARN("CoCombineUrl failed: %08x\n", hres);
2064 }else if(is_wine_uri) {
2065 WCHAR url[INTERNET_MAX_URL_LENGTH];
2067 MultiByteToWideChar(CP_ACP, 0, spec, -1, url, sizeof(url)/sizeof(WCHAR));
2068 nsIWineURI_SetWineURL(wine_uri, url);
2071 if(base_wine_uri)
2072 nsIWineURI_Release(base_wine_uri);
2074 return nsres;
2077 static nsresult NSAPI nsIOService_NewFileURI(nsIIOService *iface, nsIFile *aFile,
2078 nsIURI **_retval)
2080 TRACE("(%p %p)\n", aFile, _retval);
2081 return nsIIOService_NewFileURI(nsio, aFile, _retval);
2084 static nsresult NSAPI nsIOService_NewChannelFromURI(nsIIOService *iface, nsIURI *aURI,
2085 nsIChannel **_retval)
2087 nsIChannel *channel = NULL;
2088 nsChannel *ret;
2089 nsIWineURI *wine_uri;
2090 nsresult nsres;
2092 TRACE("(%p %p)\n", aURI, _retval);
2094 nsres = nsIIOService_NewChannelFromURI(nsio, aURI, &channel);
2095 if(NS_FAILED(nsres) && nsres != NS_ERROR_UNKNOWN_PROTOCOL) {
2096 WARN("NewChannelFromURI failed: %08x\n", nsres);
2097 *_retval = channel;
2098 return nsres;
2101 nsres = nsIURI_QueryInterface(aURI, &IID_nsIWineURI, (void**)&wine_uri);
2102 if(NS_FAILED(nsres)) {
2103 WARN("Could not get nsIWineURI: %08x\n", nsres);
2104 *_retval = channel;
2105 return channel ? NS_OK : NS_ERROR_UNEXPECTED;
2108 ret = heap_alloc(sizeof(nsChannel));
2110 ret->lpHttpChannelVtbl = &nsChannelVtbl;
2111 ret->lpUploadChannelVtbl = &nsUploadChannelVtbl;
2112 ret->ref = 1;
2113 ret->channel = channel;
2114 ret->http_channel = NULL;
2115 ret->uri = wine_uri;
2116 ret->post_data_stream = NULL;
2117 ret->load_group = NULL;
2118 ret->notif_callback = NULL;
2119 ret->load_flags = 0;
2120 ret->content = NULL;
2121 ret->charset = NULL;
2123 nsIURI_AddRef(aURI);
2124 ret->original_uri = aURI;
2126 if(channel)
2127 nsIChannel_QueryInterface(channel, &IID_nsIHttpChannel, (void**)&ret->http_channel);
2129 *_retval = NSCHANNEL(ret);
2130 return NS_OK;
2133 static nsresult NSAPI nsIOService_NewChannel(nsIIOService *iface, const nsACString *aSpec,
2134 const char *aOriginCharset, nsIURI *aBaseURI, nsIChannel **_retval)
2136 TRACE("(%p %s %p %p)\n", aSpec, debugstr_a(aOriginCharset), aBaseURI, _retval);
2137 return nsIIOService_NewChannel(nsio, aSpec, aOriginCharset, aBaseURI, _retval);
2140 static nsresult NSAPI nsIOService_GetOffline(nsIIOService *iface, PRBool *aOffline)
2142 TRACE("(%p)\n", aOffline);
2143 return nsIIOService_GetOffline(nsio, aOffline);
2146 static nsresult NSAPI nsIOService_SetOffline(nsIIOService *iface, PRBool aOffline)
2148 TRACE("(%x)\n", aOffline);
2149 return nsIIOService_SetOffline(nsio, aOffline);
2152 static nsresult NSAPI nsIOService_AllowPort(nsIIOService *iface, PRInt32 aPort,
2153 const char *aScheme, PRBool *_retval)
2155 TRACE("(%d %s %p)\n", aPort, debugstr_a(aScheme), _retval);
2156 return nsIIOService_AllowPort(nsio, aPort, debugstr_a(aScheme), _retval);
2159 static nsresult NSAPI nsIOService_ExtractScheme(nsIIOService *iface, const nsACString *urlString,
2160 nsACString * _retval)
2162 TRACE("(%p %p)\n", urlString, _retval);
2163 return nsIIOService_ExtractScheme(nsio, urlString, _retval);
2166 static const nsIIOServiceVtbl nsIOServiceVtbl = {
2167 nsIOService_QueryInterface,
2168 nsIOService_AddRef,
2169 nsIOService_Release,
2170 nsIOService_GetProtocolHandler,
2171 nsIOService_GetProtocolFlags,
2172 nsIOService_NewURI,
2173 nsIOService_NewFileURI,
2174 nsIOService_NewChannelFromURI,
2175 nsIOService_NewChannel,
2176 nsIOService_GetOffline,
2177 nsIOService_SetOffline,
2178 nsIOService_AllowPort,
2179 nsIOService_ExtractScheme
2182 static nsIIOService nsIOService = { &nsIOServiceVtbl };
2184 static nsresult NSAPI nsIOServiceFactory_QueryInterface(nsIFactory *iface, nsIIDRef riid,
2185 nsQIResult result)
2187 *result = NULL;
2189 if(IsEqualGUID(&IID_nsISupports, riid)) {
2190 TRACE("(IID_nsISupoprts %p)\n", result);
2191 *result = iface;
2192 }else if(IsEqualGUID(&IID_nsIFactory, riid)) {
2193 TRACE("(IID_nsIFactory %p)\n", result);
2194 *result = iface;
2197 if(*result) {
2198 nsIFactory_AddRef(iface);
2199 return NS_OK;
2202 WARN("(%s %p)\n", debugstr_guid(riid), result);
2203 return NS_NOINTERFACE;
2206 static nsrefcnt NSAPI nsIOServiceFactory_AddRef(nsIFactory *iface)
2208 return 2;
2211 static nsrefcnt NSAPI nsIOServiceFactory_Release(nsIFactory *iface)
2213 return 1;
2216 static nsresult NSAPI nsIOServiceFactory_CreateInstance(nsIFactory *iface,
2217 nsISupports *aOuter, const nsIID *iid, void **result)
2219 return nsIIOService_QueryInterface(&nsIOService, iid, result);
2222 static nsresult NSAPI nsIOServiceFactory_LockFactory(nsIFactory *iface, PRBool lock)
2224 WARN("(%x)\n", lock);
2225 return NS_OK;
2228 static const nsIFactoryVtbl nsIOServiceFactoryVtbl = {
2229 nsIOServiceFactory_QueryInterface,
2230 nsIOServiceFactory_AddRef,
2231 nsIOServiceFactory_Release,
2232 nsIOServiceFactory_CreateInstance,
2233 nsIOServiceFactory_LockFactory
2236 static nsIFactory nsIOServiceFactory = { &nsIOServiceFactoryVtbl };
2238 void init_nsio(nsIComponentManager *component_manager, nsIComponentRegistrar *registrar)
2240 nsIFactory *old_factory = NULL;
2241 nsresult nsres;
2243 nsres = nsIComponentManager_GetClassObject(component_manager, &NS_IOSERVICE_CID,
2244 &IID_nsIFactory, (void**)&old_factory);
2245 if(NS_FAILED(nsres)) {
2246 ERR("Could not get factory: %08x\n", nsres);
2247 return;
2250 nsres = nsIFactory_CreateInstance(old_factory, NULL, &IID_nsIIOService, (void**)&nsio);
2251 if(NS_FAILED(nsres)) {
2252 ERR("Couldn not create nsIOService instance %08x\n", nsres);
2253 nsIFactory_Release(old_factory);
2254 return;
2257 nsres = nsIComponentRegistrar_UnregisterFactory(registrar, &NS_IOSERVICE_CID, old_factory);
2258 nsIFactory_Release(old_factory);
2259 if(NS_FAILED(nsres))
2260 ERR("UnregisterFactory failed: %08x\n", nsres);
2262 nsres = nsIComponentRegistrar_RegisterFactory(registrar, &NS_IOSERVICE_CID,
2263 NS_IOSERVICE_CLASSNAME, NS_IOSERVICE_CONTRACTID, &nsIOServiceFactory);
2264 if(NS_FAILED(nsres))
2265 ERR("RegisterFactory failed: %08x\n", nsres);