dplayx: Corrected the way nameserver initializes the sessiondesc structure on broadca...
[wine] / dlls / mshtml / nsio.c
1 /*
2  * Copyright 2006-2007 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
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"
33
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
36
37 #include "mshtml_private.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
40
41 #define LOAD_INITIAL_DOCUMENT_URI 0x80000
42
43 #define NS_IOSERVICE_CLASSNAME "nsIOService"
44 #define NS_IOSERVICE_CONTRACTID "@mozilla.org/network/io-service;1"
45
46 static const IID NS_IOSERVICE_CID =
47     {0x9ac9e770, 0x18bc, 0x11d3, {0x93, 0x37, 0x00, 0x10, 0x4b, 0xa0, 0xfd, 0x40}};
48
49 static nsIIOService *nsio = NULL;
50
51 typedef struct {
52     const nsIWineURIVtbl *lpWineURIVtbl;
53
54     LONG ref;
55
56     nsIURI *uri;
57     NSContainer *container;
58     LPWSTR wine_url;
59     PRBool is_doc_uri;
60     BOOL use_wine_url;
61 } nsURI;
62
63 #define NSURI(x)         ((nsIURI*)            &(x)->lpWineURIVtbl)
64 #define NSWINEURI(x)     ((nsIWineURI*)        &(x)->lpWineURIVtbl)
65
66 static nsresult create_uri(nsIURI*,NSContainer*,nsIWineURI**);
67
68 static BOOL exec_shldocvw_67(HTMLDocument *doc, LPCWSTR url)
69 {
70     IOleCommandTarget *cmdtrg = NULL;
71     HRESULT hres;
72
73     hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget,
74                                          (void**)&cmdtrg);
75     if(SUCCEEDED(hres)) {
76         VARIANT varUrl, varRes;
77
78         V_VT(&varUrl) = VT_BSTR;
79         V_BSTR(&varUrl) = SysAllocString(url);
80         V_VT(&varRes) = VT_BOOL;
81
82         hres = IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 67, 0, &varUrl, &varRes);
83
84         IOleCommandTarget_Release(cmdtrg);
85         SysFreeString(V_BSTR(&varUrl));
86
87         if(SUCCEEDED(hres) && !V_BOOL(&varRes)) {
88             TRACE("got VARIANT_FALSE, do not load\n");
89             return FALSE;
90         }
91     }
92
93     return TRUE;
94 }
95
96 static BOOL handle_uri(NSContainer *container, nsChannel *channel, LPCWSTR uri)
97 {
98     IServiceProvider *service_provider;
99     HTMLDocument *doc = container->doc;
100     DWORD hlnf = 0;
101     HRESULT hres;
102
103     if(!doc) {
104         NSContainer *container_iter = container;
105
106         hlnf = HLNF_OPENINNEWWINDOW;
107         while(!container_iter->doc)
108             container_iter = container_iter->parent;
109         doc = container_iter->doc;
110     }
111
112     if(!hlnf && !exec_shldocvw_67(doc, uri))
113         return FALSE;
114
115     hres = IOleClientSite_QueryInterface(doc->client, &IID_IServiceProvider,
116                                          (void**)&service_provider);
117     if(SUCCEEDED(hres)) {
118         IHlinkFrame *hlink_frame;
119
120         hres = IServiceProvider_QueryService(service_provider, &IID_IHlinkFrame,
121                                              &IID_IHlinkFrame, (void**)&hlink_frame);
122         IServiceProvider_Release(service_provider);
123         if(SUCCEEDED(hres)) {
124             hlink_frame_navigate(doc, hlink_frame, uri, channel->post_data_stream, hlnf);
125             IHlinkFrame_Release(hlink_frame);
126
127             return FALSE;
128         }
129     }
130
131     return TRUE;
132 }
133
134 static BOOL before_async_open(nsChannel *channel, NSContainer *container)
135 {
136     nsACString uri_str;
137     const char *uria;
138     LPWSTR uri;
139     DWORD len;
140     BOOL ret;
141
142     nsACString_Init(&uri_str, NULL);
143     nsIWineURI_GetSpec(channel->uri, &uri_str);
144     nsACString_GetData(&uri_str, &uria, NULL);
145     len = MultiByteToWideChar(CP_ACP, 0, uria, -1, NULL, 0);
146     uri = mshtml_alloc(len*sizeof(WCHAR));
147     MultiByteToWideChar(CP_ACP, 0, uria, -1, uri, len);
148     nsACString_Finish(&uri_str);
149
150     ret = handle_uri(container, channel, uri);
151
152     mshtml_free(uri);
153
154     return ret;
155 }
156
157 #define NSCHANNEL_THIS(iface) DEFINE_THIS(nsChannel, HttpChannel, iface)
158
159 static nsresult NSAPI nsChannel_QueryInterface(nsIHttpChannel *iface, nsIIDRef riid, nsQIResult result)
160 {
161     nsChannel *This = NSCHANNEL_THIS(iface);
162
163     *result = NULL;
164
165     if(IsEqualGUID(&IID_nsISupports, riid)) {
166         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
167         *result = NSCHANNEL(This);
168     }else if(IsEqualGUID(&IID_nsIRequest, riid)) {
169         TRACE("(%p)->(IID_nsIRequest %p)\n", This, result);
170         *result = NSCHANNEL(This);
171     }else if(IsEqualGUID(&IID_nsIChannel, riid)) {
172         TRACE("(%p)->(IID_nsIChannel %p)\n", This, result);
173         *result = NSCHANNEL(This);
174     }else if(This->http_channel && IsEqualGUID(&IID_nsIHttpChannel, riid)) {
175         TRACE("(%p)->(IID_nsIHttpChannel %p)\n", This, result);
176         *result = NSHTTPCHANNEL(This);
177     }else if(IsEqualGUID(&IID_nsIUploadChannel, riid)) {
178         TRACE("(%p)->(IID_nsIUploadChannel %p)\n", This, result);
179         *result = NSUPCHANNEL(This);
180     }
181
182     if(*result) {
183         nsIChannel_AddRef(NSCHANNEL(This));
184         return NS_OK;
185     }
186
187     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
188
189     if(This->channel)
190         return nsIChannel_QueryInterface(This->channel, riid, result);
191     return NS_NOINTERFACE;
192 }
193
194 static nsrefcnt NSAPI nsChannel_AddRef(nsIHttpChannel *iface)
195 {
196     nsChannel *This = NSCHANNEL_THIS(iface);
197     nsrefcnt ref = InterlockedIncrement(&This->ref);
198
199     TRACE("(%p) ref=%d\n", This, ref);
200
201     return ref;
202 }
203
204 static nsrefcnt NSAPI nsChannel_Release(nsIHttpChannel *iface)
205 {
206     nsChannel *This = NSCHANNEL_THIS(iface);
207     LONG ref = InterlockedDecrement(&This->ref);
208
209     if(!ref) {
210         nsIWineURI_Release(This->uri);
211         if(This->channel)
212             nsIChannel_Release(This->channel);
213         if(This->http_channel)
214             nsIHttpChannel_Release(This->http_channel);
215         if(This->post_data_stream)
216             nsIInputStream_Release(This->post_data_stream);
217         if(This->load_group)
218             nsILoadGroup_Release(This->load_group);
219         if(This->notif_callback)
220             nsIInterfaceRequestor_Release(This->notif_callback);
221         if(This->original_uri)
222             nsIURI_Release(This->original_uri);
223         mshtml_free(This->content);
224         mshtml_free(This);
225     }
226
227     return ref;
228 }
229
230 static nsresult NSAPI nsChannel_GetName(nsIHttpChannel *iface, nsACString *aName)
231 {
232     nsChannel *This = NSCHANNEL_THIS(iface);
233
234     TRACE("(%p)->(%p)\n", This, aName);
235
236     if(This->channel)
237         return nsIChannel_GetName(This->channel, aName);
238
239     return NS_ERROR_NOT_IMPLEMENTED;
240 }
241
242 static nsresult NSAPI nsChannel_IsPending(nsIHttpChannel *iface, PRBool *_retval)
243 {
244     nsChannel *This = NSCHANNEL_THIS(iface);
245
246     TRACE("(%p)->(%p)\n", This, _retval);
247
248     if(This->channel)
249         return nsIChannel_IsPending(This->channel, _retval);
250
251     FIXME("default action not implemented\n");
252     return NS_ERROR_NOT_IMPLEMENTED;
253 }
254
255 static nsresult NSAPI nsChannel_GetStatus(nsIHttpChannel *iface, nsresult *aStatus)
256 {
257     nsChannel *This = NSCHANNEL_THIS(iface);
258
259     TRACE("(%p)->(%p)\n", This, aStatus);
260
261     if(This->channel)
262         return nsIChannel_GetStatus(This->channel, aStatus);
263
264     TRACE("returning NS_OK\n");
265     return *aStatus = NS_OK;
266 }
267
268 static nsresult NSAPI nsChannel_Cancel(nsIHttpChannel *iface, nsresult aStatus)
269 {
270     nsChannel *This = NSCHANNEL_THIS(iface);
271
272     TRACE("(%p)->(%08x)\n", This, aStatus);
273
274     if(This->channel)
275         return nsIChannel_Cancel(This->channel, aStatus);
276
277     FIXME("default action not implemented\n");
278     return NS_ERROR_NOT_IMPLEMENTED;
279 }
280
281 static nsresult NSAPI nsChannel_Suspend(nsIHttpChannel *iface)
282 {
283     nsChannel *This = NSCHANNEL_THIS(iface);
284
285     TRACE("(%p)\n", This);
286
287     if(This->channel)
288         return nsIChannel_Suspend(This->channel);
289
290     FIXME("default action not implemented\n");
291     return NS_ERROR_NOT_IMPLEMENTED;
292 }
293
294 static nsresult NSAPI nsChannel_Resume(nsIHttpChannel *iface)
295 {
296     nsChannel *This = NSCHANNEL_THIS(iface);
297
298     TRACE("(%p)\n", This);
299
300     if(This->channel)
301         return nsIChannel_Resume(This->channel);
302
303     FIXME("default action not implemented\n");
304     return NS_ERROR_NOT_IMPLEMENTED;
305 }
306
307 static nsresult NSAPI nsChannel_GetLoadGroup(nsIHttpChannel *iface, nsILoadGroup **aLoadGroup)
308 {
309     nsChannel *This = NSCHANNEL_THIS(iface);
310
311     TRACE("(%p)->(%p)\n", This, aLoadGroup);
312
313     if(This->load_group)
314         nsILoadGroup_AddRef(This->load_group);
315
316     *aLoadGroup = This->load_group;
317     return NS_OK;
318 }
319
320 static nsresult NSAPI nsChannel_SetLoadGroup(nsIHttpChannel *iface, nsILoadGroup *aLoadGroup)
321 {
322     nsChannel *This = NSCHANNEL_THIS(iface);
323
324     TRACE("(%p)->(%p)\n", This, aLoadGroup);
325
326     if(This->load_group)
327         nsILoadGroup_Release(This->load_group);
328     if(aLoadGroup)
329         nsILoadGroup_AddRef(aLoadGroup);
330
331     This->load_group = aLoadGroup;
332
333     if(This->channel)
334         return nsIChannel_SetLoadGroup(This->channel, aLoadGroup);
335     return NS_OK;
336 }
337
338 static nsresult NSAPI nsChannel_GetLoadFlags(nsIHttpChannel *iface, nsLoadFlags *aLoadFlags)
339 {
340     nsChannel *This = NSCHANNEL_THIS(iface);
341
342     TRACE("(%p)->(%p)\n", This, aLoadFlags);
343
344     *aLoadFlags = This->load_flags;
345     return NS_OK;
346 }
347
348 static nsresult NSAPI nsChannel_SetLoadFlags(nsIHttpChannel *iface, nsLoadFlags aLoadFlags)
349 {
350     nsChannel *This = NSCHANNEL_THIS(iface);
351
352     TRACE("(%p)->(%08x)\n", This, aLoadFlags);
353
354     This->load_flags = aLoadFlags;
355
356     if(This->channel)
357         return nsIChannel_SetLoadFlags(This->channel, aLoadFlags);
358     return NS_OK;
359 }
360
361 static nsresult NSAPI nsChannel_GetOriginalURI(nsIHttpChannel *iface, nsIURI **aOriginalURI)
362 {
363     nsChannel *This = NSCHANNEL_THIS(iface);
364
365     TRACE("(%p)->(%p)\n", This, aOriginalURI);
366
367     if(This->original_uri)
368         nsIURI_AddRef(This->original_uri);
369
370     *aOriginalURI = This->original_uri;
371     return NS_OK;
372 }
373
374 static nsresult NSAPI nsChannel_SetOriginalURI(nsIHttpChannel *iface, nsIURI *aOriginalURI)
375 {
376     nsChannel *This = NSCHANNEL_THIS(iface);
377
378     TRACE("(%p)->(%p)\n", This, aOriginalURI);
379
380     if(This->original_uri)
381         nsIURI_Release(This->original_uri);
382
383     nsIURI_AddRef(aOriginalURI);
384     This->original_uri = aOriginalURI;
385
386     if(This->channel)
387         return nsIChannel_SetOriginalURI(This->channel, aOriginalURI);
388     return NS_OK;
389 }
390
391 static nsresult NSAPI nsChannel_GetURI(nsIHttpChannel *iface, nsIURI **aURI)
392 {
393     nsChannel *This = NSCHANNEL_THIS(iface);
394
395     TRACE("(%p)->(%p)\n", This, aURI);
396
397     nsIWineURI_AddRef(This->uri);
398     *aURI = (nsIURI*)This->uri;
399
400     return NS_OK;
401 }
402
403 static nsresult NSAPI nsChannel_GetOwner(nsIHttpChannel *iface, nsISupports **aOwner)
404 {
405     nsChannel *This = NSCHANNEL_THIS(iface);
406
407     TRACE("(%p)->(%p)\n", This, aOwner);
408
409     if(This->channel)
410         return nsIChannel_GetOwner(This->channel, aOwner);
411
412     FIXME("default action not implemented\n");
413     return NS_ERROR_NOT_IMPLEMENTED;
414 }
415
416 static nsresult NSAPI nsChannel_SetOwner(nsIHttpChannel *iface, nsISupports *aOwner)
417 {
418     nsChannel *This = NSCHANNEL_THIS(iface);
419
420     TRACE("(%p)->(%p)\n", This, aOwner);
421
422     if(This->channel)
423         return nsIChannel_SetOwner(This->channel, aOwner);
424
425     FIXME("default action not implemented\n");
426     return NS_ERROR_NOT_IMPLEMENTED;
427 }
428
429 static nsresult NSAPI nsChannel_GetNotificationCallbacks(nsIHttpChannel *iface,
430         nsIInterfaceRequestor **aNotificationCallbacks)
431 {
432     nsChannel *This = NSCHANNEL_THIS(iface);
433
434     TRACE("(%p)->(%p)\n", This, aNotificationCallbacks);
435
436     if(This->notif_callback)
437         nsIInterfaceRequestor_AddRef(This->notif_callback);
438     *aNotificationCallbacks = This->notif_callback;
439
440     return NS_OK;
441 }
442
443 static nsresult NSAPI nsChannel_SetNotificationCallbacks(nsIHttpChannel *iface,
444         nsIInterfaceRequestor *aNotificationCallbacks)
445 {
446     nsChannel *This = NSCHANNEL_THIS(iface);
447
448     TRACE("(%p)->(%p)\n", This, aNotificationCallbacks);
449
450     if(This->notif_callback)
451         nsIInterfaceRequestor_Release(This->notif_callback);
452     if(aNotificationCallbacks)
453         nsIInterfaceRequestor_AddRef(aNotificationCallbacks);
454
455     This->notif_callback = aNotificationCallbacks;
456
457     if(This->channel)
458         return nsIChannel_SetNotificationCallbacks(This->channel, aNotificationCallbacks);
459     return NS_OK;
460 }
461
462 static nsresult NSAPI nsChannel_GetSecurityInfo(nsIHttpChannel *iface, nsISupports **aSecurityInfo)
463 {
464     nsChannel *This = NSCHANNEL_THIS(iface);
465
466     TRACE("(%p)->(%p)\n", This, aSecurityInfo);
467
468     if(This->channel)
469         return nsIChannel_GetSecurityInfo(This->channel, aSecurityInfo);
470
471     FIXME("default action not implemented\n");
472     return NS_ERROR_NOT_IMPLEMENTED;
473 }
474
475 static nsresult NSAPI nsChannel_GetContentType(nsIHttpChannel *iface, nsACString *aContentType)
476 {
477     nsChannel *This = NSCHANNEL_THIS(iface);
478
479     TRACE("(%p)->(%p)\n", This, aContentType);
480
481     if(This->content) {
482         nsACString_SetData(aContentType, This->content);
483         return S_OK;
484     }
485
486     if(This->channel)
487         return nsIChannel_GetContentType(This->channel, aContentType);
488
489     TRACE("returning default text/html\n");
490     nsACString_SetData(aContentType, "text/html");
491     return NS_OK;
492 }
493
494 static nsresult NSAPI nsChannel_SetContentType(nsIHttpChannel *iface,
495                                                const nsACString *aContentType)
496 {
497     nsChannel *This = NSCHANNEL_THIS(iface);
498
499     TRACE("(%p)->(%p)\n", This, aContentType);
500
501     if(This->channel)
502         return nsIChannel_SetContentType(This->channel, aContentType);
503
504     FIXME("default action not implemented\n");
505     return NS_ERROR_NOT_IMPLEMENTED;
506 }
507
508 static nsresult NSAPI nsChannel_GetContentCharset(nsIHttpChannel *iface,
509                                                   nsACString *aContentCharset)
510 {
511     nsChannel *This = NSCHANNEL_THIS(iface);
512
513     TRACE("(%p)->(%p)\n", This, aContentCharset);
514
515     if(This->channel)
516         return nsIChannel_GetContentCharset(This->channel, aContentCharset);
517
518     FIXME("default action not implemented\n");
519     return NS_ERROR_NOT_IMPLEMENTED;
520 }
521
522 static nsresult NSAPI nsChannel_SetContentCharset(nsIHttpChannel *iface,
523                                                   const nsACString *aContentCharset)
524 {
525     nsChannel *This = NSCHANNEL_THIS(iface);
526
527     TRACE("(%p)->(%p)\n", This, aContentCharset);
528
529     if(This->channel)
530         return nsIChannel_SetContentCharset(This->channel, aContentCharset);
531
532     FIXME("default action not implemented\n");
533     return NS_ERROR_NOT_IMPLEMENTED;
534 }
535
536 static nsresult NSAPI nsChannel_GetContentLength(nsIHttpChannel *iface, PRInt32 *aContentLength)
537 {
538     nsChannel *This = NSCHANNEL_THIS(iface);
539
540     TRACE("(%p)->(%p)\n", This, aContentLength);
541
542     if(This->channel)
543         return nsIChannel_GetContentLength(This->channel, aContentLength);
544
545     FIXME("default action not implemented\n");
546     return NS_ERROR_NOT_IMPLEMENTED;
547 }
548
549 static nsresult NSAPI nsChannel_SetContentLength(nsIHttpChannel *iface, PRInt32 aContentLength)
550 {
551     nsChannel *This = NSCHANNEL_THIS(iface);
552
553     TRACE("(%p)->(%d)\n", This, aContentLength);
554
555     if(This->channel)
556         return nsIChannel_SetContentLength(This->channel, aContentLength);
557
558     FIXME("default action not implemented\n");
559     return NS_ERROR_NOT_IMPLEMENTED;
560 }
561
562 static nsresult NSAPI nsChannel_Open(nsIHttpChannel *iface, nsIInputStream **_retval)
563 {
564     nsChannel *This = NSCHANNEL_THIS(iface);
565
566     TRACE("(%p)->(%p)\n", This, _retval);
567
568     if(This->channel)
569         return nsIChannel_Open(This->channel, _retval);
570
571     FIXME("default action not implemented\n");
572     return NS_ERROR_NOT_IMPLEMENTED;
573 }
574
575 static BOOL do_load_from_moniker_hack(nsChannel *This)
576 {
577     PRBool b = FALSE;
578
579     /* 
580      * We should always load the page from IMoniker, but Wine is not yet
581      * ready for this. This function is a heuristic, that decides which
582      * way of loading is better (Gecko implementation or IMoniker). The
583      * aim is to always return TRUE.
584      */
585
586     /* Load from moniker if there is no Gecko channel available */
587     if(!This->channel)
588         return TRUE;
589
590     /* Load about protocol from moniker */
591     nsIWineURI_SchemeIs(This->uri, "about", &b);
592     if(b)
593         return TRUE;
594
595     return FALSE;
596 }
597
598 static nsresult NSAPI nsChannel_AsyncOpen(nsIHttpChannel *iface, nsIStreamListener *aListener,
599                                           nsISupports *aContext)
600 {
601     nsChannel *This = NSCHANNEL_THIS(iface);
602     BSCallback *bscallback;
603     nsIWineURI *wine_uri;
604     IMoniker *mon;
605     PRBool is_doc_uri;
606     LPCWSTR wine_url;
607     nsresult nsres;
608     task_t *task;
609     HRESULT hres;
610
611     TRACE("(%p)->(%p %p)\n", This, aListener, aContext);
612
613     nsIWineURI_GetIsDocumentURI(This->uri, &is_doc_uri);
614
615     if(is_doc_uri && (This->load_flags & LOAD_INITIAL_DOCUMENT_URI)) {
616         NSContainer *container;
617
618         nsIWineURI_GetNSContainer(This->uri, &container);
619         if(!container) {
620             TRACE("container = NULL\n");
621             return nsIChannel_AsyncOpen(This->channel, aListener, aContext);
622         }
623
624         if(container->bscallback) {
625             nsIChannel_AddRef(NSCHANNEL(This));
626             container->bscallback->nschannel = This;
627
628             nsIStreamListener_AddRef(aListener);
629             container->bscallback->nslistener = aListener;
630
631             if(aContext) {
632                 nsISupports_AddRef(aContext);
633                 container->bscallback->nscontext = aContext;
634             }
635
636             nsIWebBrowserChrome_Release(NSWBCHROME(container));
637
638             if(do_load_from_moniker_hack(This))
639                 return WINE_NS_LOAD_FROM_MONIKER;
640         }else if(container->doc) {
641             BOOL cont = before_async_open(This, container);
642             nsIWebBrowserChrome_Release(NSWBCHROME(container));
643
644             if(!cont) {
645                 TRACE("canceled\n");
646                 return NS_ERROR_UNEXPECTED;
647             }
648         }else {
649             nsIWebBrowserChrome_Release(NSWBCHROME(container));
650             return This->channel
651                 ?  nsIChannel_AsyncOpen(This->channel, aListener, aContext)
652                 : NS_ERROR_UNEXPECTED;
653         }
654     }
655
656     if(This->channel) {
657         if(This->post_data_stream) {
658             nsIUploadChannel *upload_channel;
659
660             nsres = nsIChannel_QueryInterface(This->channel, &IID_nsIUploadChannel,
661                                           (void**)&upload_channel);
662             if(NS_SUCCEEDED(nsres)) {
663                 nsACString empty_string;
664                 nsACString_Init(&empty_string, "");
665
666                 nsres = nsIUploadChannel_SetUploadStream(upload_channel, This->post_data_stream,
667                                                          &empty_string, -1);
668                 nsIUploadChannel_Release(upload_channel);
669                 if(NS_FAILED(nsres))
670                     WARN("SetUploadStream failed: %08x\n", nsres);
671
672                 nsACString_Finish(&empty_string);
673             }
674         }
675
676         nsres = nsIChannel_AsyncOpen(This->channel, aListener, aContext);
677
678         if(NS_FAILED(nsres) && (This->load_flags & LOAD_INITIAL_DOCUMENT_URI))
679             return WINE_NS_LOAD_FROM_MONIKER;
680         return nsres;
681     }
682
683     TRACE("channel == NULL\n");
684
685     if(!This->original_uri) {
686         ERR("original_uri == NULL\n");
687         return NS_ERROR_UNEXPECTED;
688     }
689
690     nsres = nsIURI_QueryInterface(This->original_uri, &IID_nsIWineURI, (void**)&wine_uri);
691     if(NS_FAILED(nsres)) {
692         ERR("Could not get nsIWineURI: %08x\n", nsres);
693         return NS_ERROR_UNEXPECTED;
694     }
695
696     nsIWineURI_GetWineURL(wine_uri, &wine_url);
697     if(!wine_url) {
698         TRACE("wine_url == NULL\n");
699         return NS_ERROR_UNEXPECTED;
700     }
701
702     hres = CreateURLMoniker(NULL, wine_url, &mon);
703     if(FAILED(hres)) {
704         WARN("CreateURLMonikrer failed: %08x\n", hres);
705         return NS_ERROR_UNEXPECTED;
706     }
707
708     bscallback = create_bscallback(mon);
709     IMoniker_Release(mon);
710
711     nsIChannel_AddRef(NSCHANNEL(This));
712     bscallback->nschannel = This;
713
714     nsIStreamListener_AddRef(aListener);
715     bscallback->nslistener = aListener;
716
717     if(aContext) {
718         nsISupports_AddRef(aContext);
719         bscallback->nscontext = aContext;
720     }
721
722     task = mshtml_alloc(sizeof(task_t));
723
724     task->doc = bscallback->doc;
725     task->task_id = TASK_START_BINDING;
726     task->next = NULL;
727     task->bscallback = bscallback;
728
729     push_task(task);
730
731     return NS_OK;
732 }
733
734 static nsresult NSAPI nsChannel_GetRequestMethod(nsIHttpChannel *iface, nsACString *aRequestMethod)
735 {
736     nsChannel *This = NSCHANNEL_THIS(iface);
737
738     TRACE("(%p)->(%p)\n", This, aRequestMethod);
739
740     if(This->http_channel)
741         return nsIHttpChannel_GetRequestMethod(This->http_channel, aRequestMethod);
742
743     return NS_ERROR_NOT_IMPLEMENTED;
744 }
745
746 static nsresult NSAPI nsChannel_SetRequestMethod(nsIHttpChannel *iface,
747                                                  const nsACString *aRequestMethod)
748 {
749     nsChannel *This = NSCHANNEL_THIS(iface);
750
751     TRACE("(%p)->(%p)\n", This, aRequestMethod);
752
753     if(This->http_channel)
754         return nsIHttpChannel_SetRequestMethod(This->http_channel, aRequestMethod);
755
756     return NS_ERROR_NOT_IMPLEMENTED;
757 }
758
759 static nsresult NSAPI nsChannel_GetReferrer(nsIHttpChannel *iface, nsIURI **aReferrer)
760 {
761     nsChannel *This = NSCHANNEL_THIS(iface);
762
763     TRACE("(%p)->(%p)\n", This, aReferrer);
764
765     if(This->http_channel)
766         return nsIHttpChannel_GetReferrer(This->http_channel, aReferrer);
767
768     return NS_ERROR_NOT_IMPLEMENTED;
769 }
770
771 static nsresult NSAPI nsChannel_SetReferrer(nsIHttpChannel *iface, nsIURI *aReferrer)
772 {
773     nsChannel *This = NSCHANNEL_THIS(iface);
774
775     TRACE("(%p)->(%p)\n", This, aReferrer);
776
777     if(This->http_channel)
778         return nsIHttpChannel_SetReferrer(This->http_channel, aReferrer);
779
780     return NS_ERROR_NOT_IMPLEMENTED;
781 }
782
783 static nsresult NSAPI nsChannel_GetRequestHeader(nsIHttpChannel *iface,
784          const nsACString *aHeader, nsACString *_retval)
785 {
786     nsChannel *This = NSCHANNEL_THIS(iface);
787
788     TRACE("(%p)->(%p %p)\n", This, aHeader, _retval);
789
790     if(This->http_channel)
791         return nsIHttpChannel_GetRequestHeader(This->http_channel, aHeader, _retval);
792
793     return NS_ERROR_NOT_IMPLEMENTED;
794 }
795
796 static nsresult NSAPI nsChannel_SetRequestHeader(nsIHttpChannel *iface,
797          const nsACString *aHeader, const nsACString *aValue, PRBool aMerge)
798 {
799     nsChannel *This = NSCHANNEL_THIS(iface);
800
801     TRACE("(%p)->(%p %p %x)\n", This, aHeader, aValue, aMerge);
802
803     if(This->http_channel)
804         return nsIHttpChannel_SetRequestHeader(This->http_channel, aHeader, aValue, aMerge);
805
806     return NS_ERROR_NOT_IMPLEMENTED;
807 }
808
809 static nsresult NSAPI nsChannel_VisitRequestHeaders(nsIHttpChannel *iface,
810                                                     nsIHttpHeaderVisitor *aVisitor)
811 {
812     nsChannel *This = NSCHANNEL_THIS(iface);
813
814     TRACE("(%p)->(%p)\n", This, aVisitor);
815
816     if(This->http_channel)
817         return nsIHttpChannel_VisitRequestHeaders(This->http_channel, aVisitor);
818
819     return NS_ERROR_NOT_IMPLEMENTED;
820 }
821
822 static nsresult NSAPI nsChannel_GetAllowPipelining(nsIHttpChannel *iface, PRBool *aAllowPipelining)
823 {
824     nsChannel *This = NSCHANNEL_THIS(iface);
825
826     TRACE("(%p)->(%p)\n", This, aAllowPipelining);
827
828     if(This->http_channel)
829         return nsIHttpChannel_GetAllowPipelining(This->http_channel, aAllowPipelining);
830
831     return NS_ERROR_NOT_IMPLEMENTED;
832 }
833
834 static nsresult NSAPI nsChannel_SetAllowPipelining(nsIHttpChannel *iface, PRBool aAllowPipelining)
835 {
836     nsChannel *This = NSCHANNEL_THIS(iface);
837
838     TRACE("(%p)->(%x)\n", This, aAllowPipelining);
839
840     if(This->http_channel)
841         return nsIHttpChannel_SetAllowPipelining(This->http_channel, aAllowPipelining);
842
843     return NS_ERROR_NOT_IMPLEMENTED;
844 }
845
846 static nsresult NSAPI nsChannel_GetRedirectionLimit(nsIHttpChannel *iface, PRUint32 *aRedirectionLimit)
847 {
848     nsChannel *This = NSCHANNEL_THIS(iface);
849
850     TRACE("(%p)->(%p)\n", This, aRedirectionLimit);
851
852     if(This->http_channel)
853         return nsIHttpChannel_GetRedirectionLimit(This->http_channel, aRedirectionLimit);
854
855     return NS_ERROR_NOT_IMPLEMENTED;
856 }
857
858 static nsresult NSAPI nsChannel_SetRedirectionLimit(nsIHttpChannel *iface, PRUint32 aRedirectionLimit)
859 {
860     nsChannel *This = NSCHANNEL_THIS(iface);
861
862     TRACE("(%p)->(%u)\n", This, aRedirectionLimit);
863
864     if(This->http_channel)
865         return nsIHttpChannel_SetRedirectionLimit(This->http_channel, aRedirectionLimit);
866
867     return NS_ERROR_NOT_IMPLEMENTED;
868 }
869
870 static nsresult NSAPI nsChannel_GetResponseStatus(nsIHttpChannel *iface, PRUint32 *aResponseStatus)
871 {
872     nsChannel *This = NSCHANNEL_THIS(iface);
873
874     TRACE("(%p)->(%p)\n", This, aResponseStatus);
875
876     if(This->http_channel)
877         return nsIHttpChannel_GetResponseStatus(This->http_channel, aResponseStatus);
878
879     return NS_ERROR_NOT_IMPLEMENTED;
880 }
881
882 static nsresult NSAPI nsChannel_GetResponseStatusText(nsIHttpChannel *iface,
883                                                       nsACString *aResponseStatusText)
884 {
885     nsChannel *This = NSCHANNEL_THIS(iface);
886
887     TRACE("(%p)->(%p)\n", This, aResponseStatusText);
888
889     if(This->http_channel)
890         return nsIHttpChannel_GetResponseStatusText(This->http_channel, aResponseStatusText);
891
892     return NS_ERROR_NOT_IMPLEMENTED;
893 }
894
895 static nsresult NSAPI nsChannel_GetRequestSucceeded(nsIHttpChannel *iface,
896                                                     PRBool *aRequestSucceeded)
897 {
898     nsChannel *This = NSCHANNEL_THIS(iface);
899
900     TRACE("(%p)->(%p)\n", This, aRequestSucceeded);
901
902     if(This->http_channel)
903         return nsIHttpChannel_GetRequestSucceeded(This->http_channel, aRequestSucceeded);
904
905     return NS_ERROR_NOT_IMPLEMENTED;
906 }
907
908 static nsresult NSAPI nsChannel_GetResponseHeader(nsIHttpChannel *iface,
909          const nsACString *header, nsACString *_retval)
910 {
911     nsChannel *This = NSCHANNEL_THIS(iface);
912
913     TRACE("(%p)->(%p %p)\n", This, header, _retval);
914
915     if(This->http_channel)
916         return nsIHttpChannel_GetResponseHeader(This->http_channel, header, _retval);
917
918     return NS_ERROR_NOT_IMPLEMENTED;
919 }
920
921 static nsresult NSAPI nsChannel_SetResponseHeader(nsIHttpChannel *iface,
922         const nsACString *header, const nsACString *value, PRBool merge)
923 {
924     nsChannel *This = NSCHANNEL_THIS(iface);
925
926     TRACE("(%p)->(%p %p %x)\n", This, header, value, merge);
927
928     if(This->http_channel)
929         return nsIHttpChannel_SetResponseHeader(This->http_channel, header, value, merge);
930
931     return NS_ERROR_NOT_IMPLEMENTED;
932 }
933
934 static nsresult NSAPI nsChannel_VisitResponseHeaders(nsIHttpChannel *iface,
935         nsIHttpHeaderVisitor *aVisitor)
936 {
937     nsChannel *This = NSCHANNEL_THIS(iface);
938
939     TRACE("(%p)->(%p)\n", This, aVisitor);
940
941     if(This->http_channel)
942         return nsIHttpChannel_VisitResponseHeaders(This->http_channel, aVisitor);
943
944     return NS_ERROR_NOT_IMPLEMENTED;
945 }
946
947 static nsresult NSAPI nsChannel_IsNoStoreResponse(nsIHttpChannel *iface, PRBool *_retval)
948 {
949     nsChannel *This = NSCHANNEL_THIS(iface);
950
951     TRACE("(%p)->(%p)\n", This, _retval);
952
953     if(This->http_channel)
954         return nsIHttpChannel_IsNoStoreResponse(This->http_channel, _retval);
955
956     return NS_ERROR_NOT_IMPLEMENTED;
957 }
958
959 static nsresult NSAPI nsChannel_IsNoCacheResponse(nsIHttpChannel *iface, PRBool *_retval)
960 {
961     nsChannel *This = NSCHANNEL_THIS(iface);
962
963     TRACE("(%p)->(%p)\n", This, _retval);
964
965     if(This->http_channel)
966         return nsIHttpChannel_IsNoCacheResponse(This->http_channel, _retval);
967
968     return NS_ERROR_NOT_IMPLEMENTED;
969 }
970
971 #undef NSCHANNEL_THIS
972
973 static const nsIHttpChannelVtbl nsChannelVtbl = {
974     nsChannel_QueryInterface,
975     nsChannel_AddRef,
976     nsChannel_Release,
977     nsChannel_GetName,
978     nsChannel_IsPending,
979     nsChannel_GetStatus,
980     nsChannel_Cancel,
981     nsChannel_Suspend,
982     nsChannel_Resume,
983     nsChannel_GetLoadGroup,
984     nsChannel_SetLoadGroup,
985     nsChannel_GetLoadFlags,
986     nsChannel_SetLoadFlags,
987     nsChannel_GetOriginalURI,
988     nsChannel_SetOriginalURI,
989     nsChannel_GetURI,
990     nsChannel_GetOwner,
991     nsChannel_SetOwner,
992     nsChannel_GetNotificationCallbacks,
993     nsChannel_SetNotificationCallbacks,
994     nsChannel_GetSecurityInfo,
995     nsChannel_GetContentType,
996     nsChannel_SetContentType,
997     nsChannel_GetContentCharset,
998     nsChannel_SetContentCharset,
999     nsChannel_GetContentLength,
1000     nsChannel_SetContentLength,
1001     nsChannel_Open,
1002     nsChannel_AsyncOpen,
1003     nsChannel_GetRequestMethod,
1004     nsChannel_SetRequestMethod,
1005     nsChannel_GetReferrer,
1006     nsChannel_SetReferrer,
1007     nsChannel_GetRequestHeader,
1008     nsChannel_SetRequestHeader,
1009     nsChannel_VisitRequestHeaders,
1010     nsChannel_GetAllowPipelining,
1011     nsChannel_SetAllowPipelining,
1012     nsChannel_GetRedirectionLimit,
1013     nsChannel_SetRedirectionLimit,
1014     nsChannel_GetResponseStatus,
1015     nsChannel_GetResponseStatusText,
1016     nsChannel_GetRequestSucceeded,
1017     nsChannel_GetResponseHeader,
1018     nsChannel_SetResponseHeader,
1019     nsChannel_VisitResponseHeaders,
1020     nsChannel_IsNoStoreResponse,
1021     nsChannel_IsNoCacheResponse
1022 };
1023
1024 #define NSUPCHANNEL_THIS(iface) DEFINE_THIS(nsChannel, UploadChannel, iface)
1025
1026 static nsresult NSAPI nsUploadChannel_QueryInterface(nsIUploadChannel *iface, nsIIDRef riid,
1027                                                      nsQIResult result)
1028 {
1029     nsChannel *This = NSUPCHANNEL_THIS(iface);
1030     return nsIChannel_QueryInterface(NSCHANNEL(This), riid, result);
1031 }
1032
1033 static nsrefcnt NSAPI nsUploadChannel_AddRef(nsIUploadChannel *iface)
1034 {
1035     nsChannel *This = NSUPCHANNEL_THIS(iface);
1036     return nsIChannel_AddRef(NSCHANNEL(This));
1037 }
1038
1039 static nsrefcnt NSAPI nsUploadChannel_Release(nsIUploadChannel *iface)
1040 {
1041     nsChannel *This = NSUPCHANNEL_THIS(iface);
1042     return nsIChannel_Release(NSCHANNEL(This));
1043 }
1044
1045 static nsresult NSAPI nsUploadChannel_SetUploadStream(nsIUploadChannel *iface,
1046         nsIInputStream *aStream, const nsACString *aContentType, PRInt32 aContentLength)
1047 {
1048     nsChannel *This = NSUPCHANNEL_THIS(iface);
1049     const char *content_type;
1050
1051     TRACE("(%p)->(%p %p %d)\n", This, aStream, aContentType, aContentLength);
1052
1053     if(This->post_data_stream)
1054         nsIInputStream_Release(This->post_data_stream);
1055
1056     if(aContentType) {
1057         nsACString_GetData(aContentType, &content_type, NULL);
1058         if(*content_type)
1059             FIXME("Unsupported aContentType argument: %s\n", debugstr_a(content_type));
1060     }
1061
1062     if(aContentLength != -1)
1063         FIXME("Unsupported acontentLength = %d\n", aContentLength);
1064
1065     if(aStream)
1066         nsIInputStream_AddRef(aStream);
1067     This->post_data_stream = aStream;
1068
1069     return NS_OK;
1070 }
1071
1072 static nsresult NSAPI nsUploadChannel_GetUploadStream(nsIUploadChannel *iface,
1073         nsIInputStream **aUploadStream)
1074 {
1075     nsChannel *This = NSUPCHANNEL_THIS(iface);
1076
1077     TRACE("(%p)->(%p)\n", This, aUploadStream);
1078
1079     if(This->post_data_stream)
1080         nsIInputStream_AddRef(This->post_data_stream);
1081
1082     *aUploadStream = This->post_data_stream;
1083     return NS_OK;
1084 }
1085
1086 #undef NSUPCHANNEL_THIS
1087
1088 static const nsIUploadChannelVtbl nsUploadChannelVtbl = {
1089     nsUploadChannel_QueryInterface,
1090     nsUploadChannel_AddRef,
1091     nsUploadChannel_Release,
1092     nsUploadChannel_SetUploadStream,
1093     nsUploadChannel_GetUploadStream
1094 };
1095
1096 #define NSURI_THIS(iface) DEFINE_THIS(nsURI, WineURI, iface)
1097
1098 static nsresult NSAPI nsURI_QueryInterface(nsIWineURI *iface, nsIIDRef riid, nsQIResult result)
1099 {
1100     nsURI *This = NSURI_THIS(iface);
1101
1102     *result = NULL;
1103
1104     if(IsEqualGUID(&IID_nsISupports, riid)) {
1105         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1106         *result = NSURI(This);
1107     }else if(IsEqualGUID(&IID_nsIURI, riid)) {
1108         TRACE("(%p)->(IID_nsIURI %p)\n", This, result);
1109         *result = NSURI(This);
1110     }else if(IsEqualGUID(&IID_nsIWineURI, riid)) {
1111         TRACE("(%p)->(IID_nsIWineURI %p)\n", This, result);
1112         *result = NSURI(This);
1113     }
1114
1115     if(*result) {
1116         nsIURI_AddRef(NSURI(This));
1117         return NS_OK;
1118     }
1119
1120     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1121     return This->uri ? nsIURI_QueryInterface(This->uri, riid, result) : NS_NOINTERFACE;
1122 }
1123
1124 static nsrefcnt NSAPI nsURI_AddRef(nsIWineURI *iface)
1125 {
1126     nsURI *This = NSURI_THIS(iface);
1127     LONG ref = InterlockedIncrement(&This->ref);
1128
1129     TRACE("(%p) ref=%d\n", This, ref);
1130
1131     return ref;
1132 }
1133
1134 static nsrefcnt NSAPI nsURI_Release(nsIWineURI *iface)
1135 {
1136     nsURI *This = NSURI_THIS(iface);
1137     LONG ref = InterlockedDecrement(&This->ref);
1138
1139     TRACE("(%p) ref=%d\n", This, ref);
1140
1141     if(!ref) {
1142         if(This->container)
1143             nsIWebBrowserChrome_Release(NSWBCHROME(This->container));
1144         if(This->uri)
1145             nsIURI_Release(This->uri);
1146         mshtml_free(This->wine_url);
1147         mshtml_free(This);
1148     }
1149
1150     return ref;
1151 }
1152
1153 static nsresult NSAPI nsURI_GetSpec(nsIWineURI *iface, nsACString *aSpec)
1154 {
1155     nsURI *This = NSURI_THIS(iface);
1156
1157     TRACE("(%p)->(%p)\n", This, aSpec);
1158
1159     if(This->use_wine_url) {
1160         char speca[INTERNET_MAX_URL_LENGTH];
1161         WideCharToMultiByte(CP_ACP, 0, This->wine_url, -1, speca, sizeof(speca), NULL, NULL);
1162         nsACString_SetData(aSpec, speca);
1163
1164         return NS_OK;
1165     }
1166
1167     if(This->uri)
1168         return nsIURI_GetSpec(This->uri, aSpec);
1169
1170     TRACE("returning error\n");
1171     return NS_ERROR_NOT_IMPLEMENTED;
1172
1173 }
1174
1175 static nsresult NSAPI nsURI_SetSpec(nsIWineURI *iface, const nsACString *aSpec)
1176 {
1177     nsURI *This = NSURI_THIS(iface);
1178
1179     TRACE("(%p)->(%p)\n", This, aSpec);
1180
1181     if(This->uri)
1182         return nsIURI_SetSpec(This->uri, aSpec);
1183
1184     FIXME("default action not implemented\n");
1185     return NS_ERROR_NOT_IMPLEMENTED;
1186 }
1187
1188 static nsresult NSAPI nsURI_GetPrePath(nsIWineURI *iface, nsACString *aPrePath)
1189 {
1190     nsURI *This = NSURI_THIS(iface);
1191
1192     TRACE("(%p)->(%p)\n", This, aPrePath);
1193
1194     if(This->uri)
1195         return nsIURI_GetPrePath(This->uri, aPrePath);
1196
1197     FIXME("default action not implemented\n");
1198     return NS_ERROR_NOT_IMPLEMENTED;
1199 }
1200
1201 static nsresult NSAPI nsURI_GetScheme(nsIWineURI *iface, nsACString *aScheme)
1202 {
1203     nsURI *This = NSURI_THIS(iface);
1204
1205     TRACE("(%p)->(%p)\n", This, aScheme);
1206
1207     if(This->use_wine_url) {
1208         /*
1209          * For Gecko we set scheme to unknown so it won't be handled
1210          * as any special case.
1211          */
1212         nsACString_SetData(aScheme, "wine");
1213         return NS_OK;
1214     }
1215
1216     if(This->uri)
1217         return nsIURI_GetScheme(This->uri, aScheme);
1218
1219     TRACE("returning error\n");
1220     return NS_ERROR_NOT_IMPLEMENTED;
1221 }
1222
1223 static nsresult NSAPI nsURI_SetScheme(nsIWineURI *iface, const nsACString *aScheme)
1224 {
1225     nsURI *This = NSURI_THIS(iface);
1226
1227     TRACE("(%p)->(%p)\n", This, aScheme);
1228
1229     if(This->uri)
1230         return nsIURI_SetScheme(This->uri, aScheme);
1231
1232     FIXME("default action not implemented\n");
1233     return NS_ERROR_NOT_IMPLEMENTED;
1234 }
1235
1236 static nsresult NSAPI nsURI_GetUserPass(nsIWineURI *iface, nsACString *aUserPass)
1237 {
1238     nsURI *This = NSURI_THIS(iface);
1239
1240     TRACE("(%p)->(%p)\n", This, aUserPass);
1241
1242     if(This->uri)
1243         return nsIURI_GetUserPass(This->uri, aUserPass);
1244
1245     FIXME("default action not implemented\n");
1246     return NS_ERROR_NOT_IMPLEMENTED;
1247 }
1248
1249 static nsresult NSAPI nsURI_SetUserPass(nsIWineURI *iface, const nsACString *aUserPass)
1250 {
1251     nsURI *This = NSURI_THIS(iface);
1252
1253     TRACE("(%p)->(%p)\n", This, aUserPass);
1254
1255     if(This->uri)
1256         return nsIURI_SetUserPass(This->uri, aUserPass);
1257
1258     FIXME("default action not implemented\n");
1259     return NS_ERROR_NOT_IMPLEMENTED;
1260 }
1261
1262 static nsresult NSAPI nsURI_GetUsername(nsIWineURI *iface, nsACString *aUsername)
1263 {
1264     nsURI *This = NSURI_THIS(iface);
1265
1266     TRACE("(%p)->(%p)\n", This, aUsername);
1267
1268     if(This->uri)
1269         return nsIURI_GetUsername(This->uri, aUsername);
1270
1271     FIXME("default action not implemented\n");
1272     return NS_ERROR_NOT_IMPLEMENTED;
1273 }
1274
1275 static nsresult NSAPI nsURI_SetUsername(nsIWineURI *iface, const nsACString *aUsername)
1276 {
1277     nsURI *This = NSURI_THIS(iface);
1278
1279     TRACE("(%p)->(%p)\n", This, aUsername);
1280
1281     if(This->uri)
1282         return nsIURI_SetUsername(This->uri, aUsername);
1283
1284     FIXME("default action not implemented\n");
1285     return NS_ERROR_NOT_IMPLEMENTED;
1286 }
1287
1288 static nsresult NSAPI nsURI_GetPassword(nsIWineURI *iface, nsACString *aPassword)
1289 {
1290     nsURI *This = NSURI_THIS(iface);
1291
1292     TRACE("(%p)->(%p)\n", This, aPassword);
1293
1294     if(This->uri)
1295         return nsIURI_GetPassword(This->uri, aPassword);
1296
1297     FIXME("default action not implemented\n");
1298     return NS_ERROR_NOT_IMPLEMENTED;
1299 }
1300
1301 static nsresult NSAPI nsURI_SetPassword(nsIWineURI *iface, const nsACString *aPassword)
1302 {
1303     nsURI *This = NSURI_THIS(iface);
1304
1305     TRACE("(%p)->(%p)\n", This, aPassword);
1306
1307     if(This->uri)
1308         return nsIURI_SetPassword(This->uri, aPassword);
1309
1310     FIXME("default action not implemented\n");
1311     return NS_ERROR_NOT_IMPLEMENTED;
1312 }
1313
1314 static nsresult NSAPI nsURI_GetHostPort(nsIWineURI *iface, nsACString *aHostPort)
1315 {
1316     nsURI *This = NSURI_THIS(iface);
1317
1318     TRACE("(%p)->(%p)\n", This, aHostPort);
1319
1320     if(This->uri)
1321         return nsIURI_GetHostPort(This->uri, aHostPort);
1322
1323     FIXME("default action not implemented\n");
1324     return NS_ERROR_NOT_IMPLEMENTED;
1325 }
1326
1327 static nsresult NSAPI nsURI_SetHostPort(nsIWineURI *iface, const nsACString *aHostPort)
1328 {
1329     nsURI *This = NSURI_THIS(iface);
1330
1331     TRACE("(%p)->(%p)\n", This, aHostPort);
1332
1333     if(This->uri)
1334         return nsIURI_SetHostPort(This->uri, aHostPort);
1335
1336     FIXME("default action not implemented\n");
1337     return NS_ERROR_NOT_IMPLEMENTED;
1338 }
1339
1340 static nsresult NSAPI nsURI_GetHost(nsIWineURI *iface, nsACString *aHost)
1341 {
1342     nsURI *This = NSURI_THIS(iface);
1343
1344     TRACE("(%p)->(%p)\n", This, aHost);
1345
1346     if(This->uri)
1347         return nsIURI_GetHost(This->uri, aHost);
1348
1349     FIXME("default action not implemented\n");
1350     return NS_ERROR_NOT_IMPLEMENTED;
1351 }
1352
1353 static nsresult NSAPI nsURI_SetHost(nsIWineURI *iface, const nsACString *aHost)
1354 {
1355     nsURI *This = NSURI_THIS(iface);
1356
1357     TRACE("(%p)->(%p)\n", This, aHost);
1358
1359     if(This->uri)
1360         return nsIURI_SetHost(This->uri, aHost);
1361
1362     FIXME("default action not implemented\n");
1363     return NS_ERROR_NOT_IMPLEMENTED;
1364 }
1365
1366 static nsresult NSAPI nsURI_GetPort(nsIWineURI *iface, PRInt32 *aPort)
1367 {
1368     nsURI *This = NSURI_THIS(iface);
1369
1370     TRACE("(%p)->(%p)\n", This, aPort);
1371
1372     if(This->uri)
1373         return nsIURI_GetPort(This->uri, aPort);
1374
1375     FIXME("default action not implemented\n");
1376     return NS_ERROR_NOT_IMPLEMENTED;
1377 }
1378
1379 static nsresult NSAPI nsURI_SetPort(nsIWineURI *iface, PRInt32 aPort)
1380 {
1381     nsURI *This = NSURI_THIS(iface);
1382
1383     TRACE("(%p)->(%d)\n", This, aPort);
1384
1385     if(This->uri)
1386         return nsIURI_SetPort(This->uri, aPort);
1387
1388     FIXME("default action not implemented\n");
1389     return NS_ERROR_NOT_IMPLEMENTED;
1390 }
1391
1392 static nsresult NSAPI nsURI_GetPath(nsIWineURI *iface, nsACString *aPath)
1393 {
1394     nsURI *This = NSURI_THIS(iface);
1395
1396     TRACE("(%p)->(%p)\n", This, aPath);
1397
1398     if(This->uri)
1399         return nsIURI_GetPath(This->uri, aPath);
1400
1401     FIXME("default action not implemented\n");
1402     return NS_ERROR_NOT_IMPLEMENTED;
1403 }
1404
1405 static nsresult NSAPI nsURI_SetPath(nsIWineURI *iface, const nsACString *aPath)
1406 {
1407     nsURI *This = NSURI_THIS(iface);
1408
1409     TRACE("(%p)->(%p)\n", This, aPath);
1410
1411     if(This->uri)
1412         return nsIURI_SetPath(This->uri, aPath);
1413
1414     FIXME("default action not implemented\n");
1415     return NS_ERROR_NOT_IMPLEMENTED;
1416 }
1417
1418 static nsresult NSAPI nsURI_Equals(nsIWineURI *iface, nsIURI *other, PRBool *_retval)
1419 {
1420     nsURI *This = NSURI_THIS(iface);
1421
1422     TRACE("(%p)->(%p %p)\n", This, other, _retval);
1423
1424     if(This->uri)
1425         return nsIURI_Equals(This->uri, other, _retval);
1426
1427     FIXME("default action not implemented\n");
1428     return NS_ERROR_NOT_IMPLEMENTED;
1429 }
1430
1431 static nsresult NSAPI nsURI_SchemeIs(nsIWineURI *iface, const char *scheme, PRBool *_retval)
1432 {
1433     nsURI *This = NSURI_THIS(iface);
1434
1435     TRACE("(%p)->(%s %p)\n", This, debugstr_a(scheme), _retval);
1436
1437     if(This->use_wine_url) {
1438         WCHAR buf[INTERNET_MAX_SCHEME_LENGTH];
1439         int len = MultiByteToWideChar(CP_ACP, 0, scheme, -1, buf, sizeof(buf)/sizeof(WCHAR))-1;
1440
1441         *_retval = strlenW(This->wine_url) > len
1442             && This->wine_url[len] == ':'
1443             && !memcmp(buf, This->wine_url, len*sizeof(WCHAR));
1444         return NS_OK;
1445     }
1446
1447     if(This->uri)
1448         return nsIURI_SchemeIs(This->uri, scheme, _retval);
1449
1450     TRACE("returning error\n");
1451     return NS_ERROR_NOT_IMPLEMENTED;
1452 }
1453
1454 static nsresult NSAPI nsURI_Clone(nsIWineURI *iface, nsIURI **_retval)
1455 {
1456     nsURI *This = NSURI_THIS(iface);
1457
1458     TRACE("(%p)->(%p)\n", This, _retval);
1459
1460     if(This->uri) {
1461         nsIURI *uri;
1462         nsIWineURI *wine_uri;
1463         nsresult nsres;
1464
1465         nsres = nsIURI_Clone(This->uri, &uri);
1466         if(NS_FAILED(nsres)) {
1467             WARN("Clone failed: %08x\n", nsres);
1468             return nsres;
1469         }
1470
1471         nsres = create_uri(uri, This->container, &wine_uri);
1472         *_retval = (nsIURI*)wine_uri;
1473         if(NS_SUCCEEDED(nsres))
1474             return nsIWineURI_SetWineURL(wine_uri, This->wine_url);
1475         return nsres;
1476     }
1477
1478     FIXME("default action not implemented\n");
1479     return NS_ERROR_NOT_IMPLEMENTED;
1480 }
1481
1482 static nsresult NSAPI nsURI_Resolve(nsIWineURI *iface, const nsACString *arelativePath,
1483         nsACString *_retval)
1484 {
1485     nsURI *This = NSURI_THIS(iface);
1486
1487     TRACE("(%p)->(%p %p)\n", This, arelativePath, _retval);
1488
1489     if(This->uri)
1490         return nsIURI_Resolve(This->uri, arelativePath, _retval);
1491
1492     FIXME("default action not implemented\n");
1493     return NS_ERROR_NOT_IMPLEMENTED;
1494 }
1495
1496 static nsresult NSAPI nsURI_GetAsciiSpec(nsIWineURI *iface, nsACString *aAsciiSpec)
1497 {
1498     nsURI *This = NSURI_THIS(iface);
1499
1500     TRACE("(%p)->(%p)\n", This, aAsciiSpec);
1501
1502     if(This->wine_url)
1503         return nsIURI_GetSpec(NSURI(This), aAsciiSpec);
1504
1505     if(This->uri)
1506         return nsIURI_GetAsciiSpec(This->uri, aAsciiSpec);
1507
1508     TRACE("returning error\n");
1509     return NS_ERROR_NOT_IMPLEMENTED;
1510 }
1511
1512 static nsresult NSAPI nsURI_GetAsciiHost(nsIWineURI *iface, nsACString *aAsciiHost)
1513 {
1514     nsURI *This = NSURI_THIS(iface);
1515
1516     TRACE("(%p)->(%p)\n", This, aAsciiHost);
1517
1518     if(This->uri)
1519         return nsIURI_GetAsciiHost(This->uri, aAsciiHost);
1520
1521     FIXME("default action not implemented\n");
1522     return NS_ERROR_NOT_IMPLEMENTED;
1523 }
1524
1525 static nsresult NSAPI nsURI_GetOriginCharset(nsIWineURI *iface, nsACString *aOriginCharset)
1526 {
1527     nsURI *This = NSURI_THIS(iface);
1528
1529     TRACE("(%p)->(%p)\n", This, aOriginCharset);
1530
1531     if(This->uri)
1532         return nsIURI_GetOriginCharset(This->uri, aOriginCharset);
1533
1534     FIXME("default action not implemented\n");
1535     return NS_ERROR_NOT_IMPLEMENTED;
1536 }
1537
1538 static nsresult NSAPI nsURI_GetNSContainer(nsIWineURI *iface, NSContainer **aContainer)
1539 {
1540     nsURI *This = NSURI_THIS(iface);
1541
1542     TRACE("(%p)->(%p)\n", This, aContainer);
1543
1544     if(This->container)
1545         nsIWebBrowserChrome_AddRef(NSWBCHROME(This->container));
1546     *aContainer = This->container;
1547
1548     return NS_OK;
1549 }
1550
1551 static nsresult NSAPI nsURI_SetNSContainer(nsIWineURI *iface, NSContainer *aContainer)
1552 {
1553     nsURI *This = NSURI_THIS(iface);
1554
1555     TRACE("(%p)->(%p)\n", This, aContainer);
1556
1557     if(This->container) {
1558         if(This->container == aContainer)
1559             return NS_OK;
1560         WARN("Container already set: %p\n", This->container);
1561         nsIWebBrowserChrome_Release(NSWBCHROME(This->container));
1562     }
1563
1564     if(aContainer)
1565         nsIWebBrowserChrome_AddRef(NSWBCHROME(aContainer));
1566     This->container = aContainer;
1567
1568     return NS_OK;
1569 }
1570
1571 static nsresult NSAPI nsURI_GetIsDocumentURI(nsIWineURI *iface, PRBool *aIsDocumentURI)
1572 {
1573     nsURI *This = NSURI_THIS(iface);
1574
1575     TRACE("(%p)->(%p)\n", This, aIsDocumentURI);
1576
1577     *aIsDocumentURI = This->is_doc_uri;
1578     return NS_OK;
1579 }
1580
1581 static nsresult NSAPI nsURI_SetIsDocumentURI(nsIWineURI *iface, PRBool aIsDocumentURI)
1582 {
1583     nsURI *This = NSURI_THIS(iface);
1584
1585     TRACE("(%p)->(%x)\n", This, aIsDocumentURI);
1586
1587     This->is_doc_uri = aIsDocumentURI;
1588     return NS_OK;
1589 }
1590
1591 static nsresult NSAPI nsURI_GetWineURL(nsIWineURI *iface, LPCWSTR *aURL)
1592 {
1593     nsURI *This = NSURI_THIS(iface);
1594
1595     TRACE("(%p)->(%p)\n", This, aURL);
1596
1597     *aURL = This->wine_url;
1598     return NS_OK;
1599 }
1600
1601 static nsresult NSAPI nsURI_SetWineURL(nsIWineURI *iface, LPCWSTR aURL)
1602 {
1603     nsURI *This = NSURI_THIS(iface);
1604
1605     static const WCHAR wszFtp[]   = {'f','t','p',':'};
1606     static const WCHAR wszHttp[]  = {'h','t','t','p',':'};
1607     static const WCHAR wszHttps[] = {'h','t','t','p','s',':'};
1608
1609     TRACE("(%p)->(%s)\n", This, debugstr_w(aURL));
1610
1611     mshtml_free(This->wine_url);
1612
1613     if(aURL) {
1614         int len = strlenW(aURL)+1;
1615         This->wine_url = mshtml_alloc(len*sizeof(WCHAR));
1616         memcpy(This->wine_url, aURL, len*sizeof(WCHAR));
1617
1618         /* FIXME: Always use wine url */
1619         This->use_wine_url =
1620                strncmpW(aURL, wszFtp,   sizeof(wszFtp)/sizeof(WCHAR))
1621             && strncmpW(aURL, wszHttp,  sizeof(wszHttp)/sizeof(WCHAR))
1622             && strncmpW(aURL, wszHttps, sizeof(wszHttps)/sizeof(WCHAR));
1623     }else {
1624         This->wine_url = NULL;
1625         This->use_wine_url = FALSE;
1626     }
1627
1628     return NS_OK;
1629 }
1630
1631 #undef NSURI_THIS
1632
1633 static const nsIWineURIVtbl nsWineURIVtbl = {
1634     nsURI_QueryInterface,
1635     nsURI_AddRef,
1636     nsURI_Release,
1637     nsURI_GetSpec,
1638     nsURI_SetSpec,
1639     nsURI_GetPrePath,
1640     nsURI_GetScheme,
1641     nsURI_SetScheme,
1642     nsURI_GetUserPass,
1643     nsURI_SetUserPass,
1644     nsURI_GetUsername,
1645     nsURI_SetUsername,
1646     nsURI_GetPassword,
1647     nsURI_SetPassword,
1648     nsURI_GetHostPort,
1649     nsURI_SetHostPort,
1650     nsURI_GetHost,
1651     nsURI_SetHost,
1652     nsURI_GetPort,
1653     nsURI_SetPort,
1654     nsURI_GetPath,
1655     nsURI_SetPath,
1656     nsURI_Equals,
1657     nsURI_SchemeIs,
1658     nsURI_Clone,
1659     nsURI_Resolve,
1660     nsURI_GetAsciiSpec,
1661     nsURI_GetAsciiHost,
1662     nsURI_GetOriginCharset,
1663     nsURI_GetNSContainer,
1664     nsURI_SetNSContainer,
1665     nsURI_GetIsDocumentURI,
1666     nsURI_SetIsDocumentURI,
1667     nsURI_GetWineURL,
1668     nsURI_SetWineURL
1669 };
1670
1671 static nsresult create_uri(nsIURI *uri, NSContainer *container, nsIWineURI **_retval)
1672 {
1673     nsURI *ret = mshtml_alloc(sizeof(nsURI));
1674
1675     ret->lpWineURIVtbl = &nsWineURIVtbl;
1676     ret->ref = 1;
1677     ret->uri = uri;
1678     ret->container = container;
1679     ret->wine_url = NULL;
1680     ret->is_doc_uri = FALSE;
1681     ret->use_wine_url = FALSE;
1682
1683     if(container)
1684         nsIWebBrowserChrome_AddRef(NSWBCHROME(container));
1685
1686     TRACE("retval=%p\n", ret);
1687     *_retval = NSWINEURI(ret);
1688     return NS_OK;
1689 }
1690
1691 static nsresult NSAPI nsIOService_QueryInterface(nsIIOService *iface, nsIIDRef riid,
1692                                                  nsQIResult result)
1693 {
1694     *result = NULL;
1695
1696     if(IsEqualGUID(&IID_nsISupports, riid)) {
1697         TRACE("(IID_nsISupports %p)\n", result);
1698         *result = iface;
1699     }else if(IsEqualGUID(&IID_nsIIOService, riid)) {
1700         TRACE("(IID_nsIIOService %p)\n", result);
1701         *result = iface;
1702     }
1703
1704     if(*result) {
1705         nsIIOService_AddRef(iface);
1706         return S_OK;
1707     }
1708
1709     WARN("(%s %p)\n", debugstr_guid(riid), result);
1710     return NS_NOINTERFACE;
1711 }
1712
1713 static nsrefcnt NSAPI nsIOService_AddRef(nsIIOService *iface)
1714 {
1715     return 2;
1716 }
1717
1718 static nsrefcnt NSAPI nsIOService_Release(nsIIOService *iface)
1719 {
1720     return 1;
1721 }
1722
1723 static nsresult NSAPI nsIOService_GetProtocolHandler(nsIIOService *iface, const char *aScheme,
1724                                                      nsIProtocolHandler **_retval)
1725 {
1726     TRACE("(%s %p)\n", debugstr_a(aScheme), _retval);
1727     return nsIIOService_GetProtocolHandler(nsio, aScheme, _retval);
1728 }
1729
1730 static nsresult NSAPI nsIOService_GetProtocolFlags(nsIIOService *iface, const char *aScheme,
1731                                                     PRUint32 *_retval)
1732 {
1733     TRACE("(%s %p)\n", debugstr_a(aScheme), _retval);
1734     return nsIIOService_GetProtocolFlags(nsio, aScheme, _retval);
1735 }
1736
1737 static nsresult NSAPI nsIOService_NewURI(nsIIOService *iface, const nsACString *aSpec,
1738         const char *aOriginCharset, nsIURI *aBaseURI, nsIURI **_retval)
1739 {
1740     const char *spec = NULL;
1741     NSContainer *nscontainer = NULL;
1742     nsIURI *uri = NULL;
1743     PRBool is_javascript = FALSE;
1744     LPCWSTR base_wine_url = NULL;
1745     nsIWineURI *base_wine_uri = NULL, *wine_uri;
1746     nsresult nsres;
1747
1748     nsACString_GetData(aSpec, &spec, NULL);
1749
1750     TRACE("(%p(%s) %s %p %p)\n", aSpec, debugstr_a(spec), debugstr_a(aOriginCharset),
1751           aBaseURI, _retval);
1752
1753     if(aBaseURI) {
1754         nsACString base_uri_str;
1755         const char *base_uri = NULL;
1756
1757         static const char szChrome[] = "chrome:";
1758         static const char szResource[] = "resource:";
1759
1760         nsACString_Init(&base_uri_str, NULL);
1761
1762         nsres = nsIURI_GetSpec(aBaseURI, &base_uri_str);
1763         if(NS_SUCCEEDED(nsres)) {
1764             nsACString_GetData(&base_uri_str, &base_uri, NULL);
1765             TRACE("base_uri=%s\n", debugstr_a(base_uri));
1766         }else {
1767             ERR("GetSpec failed: %08x\n", nsres);
1768         }
1769
1770         nsACString_Finish(&base_uri_str);
1771
1772         if(!strncmp(spec, szChrome, sizeof(szChrome)-1)
1773            || !strncmp(spec, szResource, sizeof(szResource)-1))
1774             aBaseURI = NULL;
1775     }
1776
1777     nsres = nsIIOService_NewURI(nsio, aSpec, aOriginCharset, aBaseURI, &uri);
1778     if(NS_FAILED(nsres))
1779         TRACE("NewURI failed: %08x\n", nsres);
1780
1781     if(uri) {
1782         nsIURI_SchemeIs(uri, "javascript", &is_javascript);
1783         if(is_javascript) {
1784             TRACE("returning javascript uri: %p\n", uri);
1785             *_retval = uri;
1786             return NS_OK;
1787         }
1788     }
1789
1790     if(aBaseURI) {
1791         nsres = nsIURI_QueryInterface(aBaseURI, &IID_nsIWineURI, (void**)&base_wine_uri);
1792         if(NS_SUCCEEDED(nsres)) {
1793             nsIWineURI_GetNSContainer(base_wine_uri, &nscontainer);
1794             nsIWineURI_GetWineURL(base_wine_uri, &base_wine_url);
1795         }else {
1796             ERR("Could not get nsIWineURI: %08x\n", nsres);
1797         }
1798     }
1799
1800     nsres = create_uri(uri, nscontainer, &wine_uri);
1801     *_retval = (nsIURI*)wine_uri;
1802
1803     if(nscontainer)
1804         nsIWebBrowserChrome_Release(NSWBCHROME(nscontainer));
1805
1806     if(base_wine_url) {
1807         WCHAR url[INTERNET_MAX_URL_LENGTH], rel_url[INTERNET_MAX_URL_LENGTH];
1808         LPCSTR speca;
1809         DWORD len;
1810         HRESULT hres;
1811
1812         nsACString_GetData(aSpec, &speca, NULL);
1813         MultiByteToWideChar(CP_ACP, 0, speca, -1, rel_url, sizeof(rel_url)/sizeof(WCHAR));
1814
1815         hres = CoInternetCombineUrl(base_wine_url, rel_url,
1816                                     URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO,
1817                                     url, sizeof(url)/sizeof(WCHAR), &len, 0);
1818         if(SUCCEEDED(hres))
1819             nsIWineURI_SetWineURL(wine_uri, url);
1820         else
1821             WARN("CoCombineUrl failed: %08x\n", hres);
1822     }
1823
1824     if(base_wine_uri)
1825         nsIWineURI_Release(base_wine_uri);
1826
1827     return nsres;
1828 }
1829
1830 static nsresult NSAPI nsIOService_NewFileURI(nsIIOService *iface, nsIFile *aFile,
1831                                              nsIURI **_retval)
1832 {
1833     TRACE("(%p %p)\n", aFile, _retval);
1834     return nsIIOService_NewFileURI(nsio, aFile, _retval);
1835 }
1836
1837 static nsresult NSAPI nsIOService_NewChannelFromURI(nsIIOService *iface, nsIURI *aURI,
1838                                                      nsIChannel **_retval)
1839 {
1840     nsIChannel *channel = NULL;
1841     nsChannel *ret;
1842     nsIWineURI *wine_uri;
1843     nsresult nsres;
1844
1845     TRACE("(%p %p)\n", aURI, _retval);
1846
1847     nsres = nsIIOService_NewChannelFromURI(nsio, aURI, &channel);
1848     if(NS_FAILED(nsres) && nsres != NS_ERROR_UNKNOWN_PROTOCOL) {
1849         WARN("NewChannelFromURI failed: %08x\n", nsres);
1850         *_retval = channel;
1851         return nsres;
1852     }
1853
1854     nsres = nsIURI_QueryInterface(aURI, &IID_nsIWineURI, (void**)&wine_uri);
1855     if(NS_FAILED(nsres)) {
1856         WARN("Could not get nsIWineURI: %08x\n", nsres);
1857         *_retval = channel;
1858         return channel ? NS_OK : NS_ERROR_UNEXPECTED;
1859     }
1860
1861     ret = mshtml_alloc(sizeof(nsChannel));
1862
1863     ret->lpHttpChannelVtbl = &nsChannelVtbl;
1864     ret->lpUploadChannelVtbl = &nsUploadChannelVtbl;
1865     ret->ref = 1;
1866     ret->channel = channel;
1867     ret->http_channel = NULL;
1868     ret->uri = wine_uri;
1869     ret->post_data_stream = NULL;
1870     ret->load_group = NULL;
1871     ret->notif_callback = NULL;
1872     ret->load_flags = 0;
1873     ret->content = NULL;
1874
1875     nsIURI_AddRef(aURI);
1876     ret->original_uri = aURI;
1877
1878     if(channel)
1879         nsIChannel_QueryInterface(channel, &IID_nsIHttpChannel, (void**)&ret->http_channel);
1880
1881     *_retval = NSCHANNEL(ret);
1882     return NS_OK;
1883 }
1884
1885 static nsresult NSAPI nsIOService_NewChannel(nsIIOService *iface, const nsACString *aSpec,
1886         const char *aOriginCharset, nsIURI *aBaseURI, nsIChannel **_retval)
1887 {
1888     TRACE("(%p %s %p %p)\n", aSpec, debugstr_a(aOriginCharset), aBaseURI, _retval);
1889     return nsIIOService_NewChannel(nsio, aSpec, aOriginCharset, aBaseURI, _retval);
1890 }
1891
1892 static nsresult NSAPI nsIOService_GetOffline(nsIIOService *iface, PRBool *aOffline)
1893 {
1894     TRACE("(%p)\n", aOffline);
1895     return nsIIOService_GetOffline(nsio, aOffline);
1896 }
1897
1898 static nsresult NSAPI nsIOService_SetOffline(nsIIOService *iface, PRBool aOffline)
1899 {
1900     TRACE("(%x)\n", aOffline);
1901     return nsIIOService_SetOffline(nsio, aOffline);
1902 }
1903
1904 static nsresult NSAPI nsIOService_AllowPort(nsIIOService *iface, PRInt32 aPort,
1905                                              const char *aScheme, PRBool *_retval)
1906 {
1907     TRACE("(%d %s %p)\n", aPort, debugstr_a(aScheme), _retval);
1908     return nsIIOService_AllowPort(nsio, aPort, debugstr_a(aScheme), _retval);
1909 }
1910
1911 static nsresult NSAPI nsIOService_ExtractScheme(nsIIOService *iface, const nsACString *urlString,
1912                                                  nsACString * _retval)
1913 {
1914     TRACE("(%p %p)\n", urlString, _retval);
1915     return nsIIOService_ExtractScheme(nsio, urlString, _retval);
1916 }
1917
1918 static const nsIIOServiceVtbl nsIOServiceVtbl = {
1919     nsIOService_QueryInterface,
1920     nsIOService_AddRef,
1921     nsIOService_Release,
1922     nsIOService_GetProtocolHandler,
1923     nsIOService_GetProtocolFlags,
1924     nsIOService_NewURI,
1925     nsIOService_NewFileURI,
1926     nsIOService_NewChannelFromURI,
1927     nsIOService_NewChannel,
1928     nsIOService_GetOffline,
1929     nsIOService_SetOffline,
1930     nsIOService_AllowPort,
1931     nsIOService_ExtractScheme
1932 };
1933
1934 static nsIIOService nsIOService = { &nsIOServiceVtbl };
1935
1936 static nsresult NSAPI nsIOServiceFactory_QueryInterface(nsIFactory *iface, nsIIDRef riid,
1937                                                         nsQIResult result)
1938 {
1939     *result = NULL;
1940
1941     if(IsEqualGUID(&IID_nsISupports, riid)) {
1942         TRACE("(IID_nsISupoprts %p)\n", result);
1943         *result = iface;
1944     }else if(IsEqualGUID(&IID_nsIFactory, riid)) {
1945         TRACE("(IID_nsIFactory %p)\n", result);
1946         *result = iface;
1947     }
1948
1949     if(*result) {
1950         nsIFactory_AddRef(iface);
1951         return NS_OK;
1952     }
1953
1954     WARN("(%s %p)\n", debugstr_guid(riid), result);
1955     return NS_NOINTERFACE;
1956 }
1957
1958 static nsrefcnt NSAPI nsIOServiceFactory_AddRef(nsIFactory *iface)
1959 {
1960     return 2;
1961 }
1962
1963 static nsrefcnt NSAPI nsIOServiceFactory_Release(nsIFactory *iface)
1964 {
1965     return 1;
1966 }
1967
1968 static nsresult NSAPI nsIOServiceFactory_CreateInstance(nsIFactory *iface,
1969         nsISupports *aOuter, const nsIID *iid, void **result)
1970 {
1971     return nsIIOService_QueryInterface(&nsIOService, iid, result);
1972 }
1973
1974 static nsresult NSAPI nsIOServiceFactory_LockFactory(nsIFactory *iface, PRBool lock)
1975 {
1976     WARN("(%x)\n", lock);
1977     return NS_OK;
1978 }
1979
1980 static const nsIFactoryVtbl nsIOServiceFactoryVtbl = {
1981     nsIOServiceFactory_QueryInterface,
1982     nsIOServiceFactory_AddRef,
1983     nsIOServiceFactory_Release,
1984     nsIOServiceFactory_CreateInstance,
1985     nsIOServiceFactory_LockFactory
1986 };
1987
1988 static nsIFactory nsIOServiceFactory = { &nsIOServiceFactoryVtbl };
1989
1990 void init_nsio(nsIComponentManager *component_manager, nsIComponentRegistrar *registrar)
1991 {
1992     nsIFactory *old_factory = NULL;
1993     nsresult nsres;
1994
1995     nsres = nsIComponentManager_GetClassObject(component_manager, &NS_IOSERVICE_CID,
1996                                                &IID_nsIFactory, (void**)&old_factory);
1997     if(NS_FAILED(nsres)) {
1998         ERR("Could not get factory: %08x\n", nsres);
1999         nsIFactory_Release(old_factory);
2000         return;
2001     }
2002
2003     nsres = nsIFactory_CreateInstance(old_factory, NULL, &IID_nsIIOService, (void**)&nsio);
2004     if(NS_FAILED(nsres)) {
2005         ERR("Couldn not create nsIOService instance %08x\n", nsres);
2006         nsIFactory_Release(old_factory);
2007         return;
2008     }
2009
2010     nsres = nsIComponentRegistrar_UnregisterFactory(registrar, &NS_IOSERVICE_CID, old_factory);
2011     nsIFactory_Release(old_factory);
2012     if(NS_FAILED(nsres))
2013         ERR("UnregisterFactory failed: %08x\n", nsres);
2014
2015     nsres = nsIComponentRegistrar_RegisterFactory(registrar, &NS_IOSERVICE_CID,
2016             NS_IOSERVICE_CLASSNAME, NS_IOSERVICE_CONTRACTID, &nsIOServiceFactory);
2017     if(NS_FAILED(nsres))
2018         ERR("RegisterFactory failed: %08x\n", nsres);
2019 }