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