mshtml: Implement IHTMLCurrentStyle_get_margin.
[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 static nsINetUtil *net_util;
51
52 static const WCHAR about_blankW[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
53
54 typedef struct {
55     const nsIWineURIVtbl *lpWineURIVtbl;
56
57     LONG ref;
58
59     nsIURI *uri;
60     NSContainer *container;
61     LPWSTR wine_url;
62     PRBool is_doc_uri;
63     BOOL use_wine_url;
64 } nsURI;
65
66 #define NSURI(x)         ((nsIURI*)            &(x)->lpWineURIVtbl)
67 #define NSWINEURI(x)     ((nsIWineURI*)        &(x)->lpWineURIVtbl)
68
69 static nsresult create_uri(nsIURI*,NSContainer*,nsIWineURI**);
70
71 HRESULT nsuri_to_url(LPCWSTR nsuri, BSTR *ret)
72 {
73     const WCHAR *ptr = nsuri;
74
75     static const WCHAR wine_prefixW[] = {'w','i','n','e',':'};
76
77     if(!strncmpW(nsuri, wine_prefixW, sizeof(wine_prefixW)/sizeof(WCHAR)))
78         ptr += sizeof(wine_prefixW)/sizeof(WCHAR);
79
80     *ret = SysAllocString(ptr);
81     if(!*ret)
82         return E_OUTOFMEMORY;
83
84     TRACE("%s -> %s\n", debugstr_w(nsuri), debugstr_w(*ret));
85     return S_OK;
86 }
87
88 static BOOL exec_shldocvw_67(HTMLDocument *doc, LPCWSTR url)
89 {
90     IOleCommandTarget *cmdtrg = NULL;
91     HRESULT hres;
92
93     hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget,
94                                          (void**)&cmdtrg);
95     if(SUCCEEDED(hres)) {
96         VARIANT varUrl, varRes;
97
98         V_VT(&varUrl) = VT_BSTR;
99         V_BSTR(&varUrl) = SysAllocString(url);
100         V_VT(&varRes) = VT_BOOL;
101
102         hres = IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 67, 0, &varUrl, &varRes);
103
104         IOleCommandTarget_Release(cmdtrg);
105         SysFreeString(V_BSTR(&varUrl));
106
107         if(SUCCEEDED(hres) && !V_BOOL(&varRes)) {
108             TRACE("got VARIANT_FALSE, do not load\n");
109             return FALSE;
110         }
111     }
112
113     return TRUE;
114 }
115
116 static BOOL before_async_open(nsChannel *channel, NSContainer *container)
117 {
118     IServiceProvider *service_provider;
119     HTMLDocument *doc = container->doc;
120     DWORD hlnf = 0;
121     LPCWSTR uri;
122     HRESULT hres;
123
124     nsIWineURI_GetWineURL(channel->uri, &uri);
125     if(!uri) {
126         ERR("GetWineURL returned NULL\n");
127         return TRUE;
128     }
129
130     if(!doc) {
131         NSContainer *container_iter = container;
132
133         hlnf = HLNF_OPENINNEWWINDOW;
134         while(!container_iter->doc)
135             container_iter = container_iter->parent;
136         doc = container_iter->doc;
137     }
138
139     if(!doc->client)
140         return TRUE;
141
142     if(!hlnf && !exec_shldocvw_67(doc, uri))
143         return FALSE;
144
145     hres = IOleClientSite_QueryInterface(doc->client, &IID_IServiceProvider,
146                                          (void**)&service_provider);
147     if(SUCCEEDED(hres)) {
148         IHlinkFrame *hlink_frame;
149
150         hres = IServiceProvider_QueryService(service_provider, &IID_IHlinkFrame,
151                                              &IID_IHlinkFrame, (void**)&hlink_frame);
152         IServiceProvider_Release(service_provider);
153         if(SUCCEEDED(hres)) {
154             hlink_frame_navigate(doc, hlink_frame, uri, channel->post_data_stream, hlnf);
155             IHlinkFrame_Release(hlink_frame);
156
157             return FALSE;
158         }
159     }
160
161     return TRUE;
162 }
163
164 #define NSCHANNEL_THIS(iface) DEFINE_THIS(nsChannel, HttpChannel, iface)
165
166 static nsresult NSAPI nsChannel_QueryInterface(nsIHttpChannel *iface, nsIIDRef riid, nsQIResult result)
167 {
168     nsChannel *This = NSCHANNEL_THIS(iface);
169
170     if(IsEqualGUID(&IID_nsISupports, riid)) {
171         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
172         *result = NSCHANNEL(This);
173     }else if(IsEqualGUID(&IID_nsIRequest, riid)) {
174         TRACE("(%p)->(IID_nsIRequest %p)\n", This, result);
175         *result = NSCHANNEL(This);
176     }else if(IsEqualGUID(&IID_nsIChannel, riid)) {
177         TRACE("(%p)->(IID_nsIChannel %p)\n", This, result);
178         *result = NSCHANNEL(This);
179     }else if(IsEqualGUID(&IID_nsIHttpChannel, riid)) {
180         TRACE("(%p)->(IID_nsIHttpChannel %p)\n", This, result);
181         *result = This->http_channel ? NSHTTPCHANNEL(This) : NULL;
182     }else if(IsEqualGUID(&IID_nsIUploadChannel, riid)) {
183         TRACE("(%p)->(IID_nsIUploadChannel %p)\n", This, result);
184         *result = NSUPCHANNEL(This);
185     }else if(IsEqualGUID(&IID_nsIHttpChannelInternal, riid)) {
186         TRACE("(%p)->(IID_nsIHttpChannelInternal %p)\n", This, result);
187         *result = This->http_channel_internal ? NSHTTPINTERNAL(This) : NULL;
188     }else {
189         TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
190         *result = NULL;
191     }
192
193     if(*result) {
194         nsIChannel_AddRef(NSCHANNEL(This));
195         return NS_OK;
196     }
197
198     return NS_NOINTERFACE;
199 }
200
201 static nsrefcnt NSAPI nsChannel_AddRef(nsIHttpChannel *iface)
202 {
203     nsChannel *This = NSCHANNEL_THIS(iface);
204     nsrefcnt ref = InterlockedIncrement(&This->ref);
205
206     TRACE("(%p) ref=%d\n", This, ref);
207
208     return ref;
209 }
210
211 static nsrefcnt NSAPI nsChannel_Release(nsIHttpChannel *iface)
212 {
213     nsChannel *This = NSCHANNEL_THIS(iface);
214     LONG ref = InterlockedDecrement(&This->ref);
215
216     if(!ref) {
217         nsIWineURI_Release(This->uri);
218         if(This->channel)
219             nsIChannel_Release(This->channel);
220         if(This->http_channel)
221             nsIHttpChannel_Release(This->http_channel);
222         if(This->http_channel_internal)
223             nsIHttpChannel_Release(This->http_channel_internal);
224         if(This->owner)
225             nsISupports_Release(This->owner);
226         if(This->post_data_stream)
227             nsIInputStream_Release(This->post_data_stream);
228         if(This->load_group)
229             nsILoadGroup_Release(This->load_group);
230         if(This->notif_callback)
231             nsIInterfaceRequestor_Release(This->notif_callback);
232         if(This->original_uri)
233             nsIURI_Release(This->original_uri);
234         heap_free(This->content_type);
235         heap_free(This->charset);
236         heap_free(This);
237     }
238
239     return ref;
240 }
241
242 static nsresult NSAPI nsChannel_GetName(nsIHttpChannel *iface, nsACString *aName)
243 {
244     nsChannel *This = NSCHANNEL_THIS(iface);
245
246     TRACE("(%p)->(%p)\n", This, aName);
247
248     if(This->channel)
249         return nsIChannel_GetName(This->channel, aName);
250
251     return NS_ERROR_NOT_IMPLEMENTED;
252 }
253
254 static nsresult NSAPI nsChannel_IsPending(nsIHttpChannel *iface, PRBool *_retval)
255 {
256     nsChannel *This = NSCHANNEL_THIS(iface);
257
258     TRACE("(%p)->(%p)\n", This, _retval);
259
260     if(This->channel)
261         return nsIChannel_IsPending(This->channel, _retval);
262
263     FIXME("default action not implemented\n");
264     return NS_ERROR_NOT_IMPLEMENTED;
265 }
266
267 static nsresult NSAPI nsChannel_GetStatus(nsIHttpChannel *iface, nsresult *aStatus)
268 {
269     nsChannel *This = NSCHANNEL_THIS(iface);
270
271     TRACE("(%p)->(%p)\n", This, aStatus);
272
273     if(This->channel)
274         return nsIChannel_GetStatus(This->channel, aStatus);
275
276     TRACE("returning NS_OK\n");
277     return *aStatus = NS_OK;
278 }
279
280 static nsresult NSAPI nsChannel_Cancel(nsIHttpChannel *iface, nsresult aStatus)
281 {
282     nsChannel *This = NSCHANNEL_THIS(iface);
283
284     TRACE("(%p)->(%08x)\n", This, aStatus);
285
286     if(This->channel)
287         return nsIChannel_Cancel(This->channel, aStatus);
288
289     FIXME("default action not implemented\n");
290     return NS_ERROR_NOT_IMPLEMENTED;
291 }
292
293 static nsresult NSAPI nsChannel_Suspend(nsIHttpChannel *iface)
294 {
295     nsChannel *This = NSCHANNEL_THIS(iface);
296
297     TRACE("(%p)\n", This);
298
299     if(This->channel)
300         return nsIChannel_Suspend(This->channel);
301
302     FIXME("default action not implemented\n");
303     return NS_ERROR_NOT_IMPLEMENTED;
304 }
305
306 static nsresult NSAPI nsChannel_Resume(nsIHttpChannel *iface)
307 {
308     nsChannel *This = NSCHANNEL_THIS(iface);
309
310     TRACE("(%p)\n", This);
311
312     if(This->channel)
313         return nsIChannel_Resume(This->channel);
314
315     FIXME("default action not implemented\n");
316     return NS_ERROR_NOT_IMPLEMENTED;
317 }
318
319 static nsresult NSAPI nsChannel_GetLoadGroup(nsIHttpChannel *iface, nsILoadGroup **aLoadGroup)
320 {
321     nsChannel *This = NSCHANNEL_THIS(iface);
322
323     TRACE("(%p)->(%p)\n", This, aLoadGroup);
324
325     if(This->load_group)
326         nsILoadGroup_AddRef(This->load_group);
327
328     *aLoadGroup = This->load_group;
329     return NS_OK;
330 }
331
332 static nsresult NSAPI nsChannel_SetLoadGroup(nsIHttpChannel *iface, nsILoadGroup *aLoadGroup)
333 {
334     nsChannel *This = NSCHANNEL_THIS(iface);
335
336     TRACE("(%p)->(%p)\n", This, aLoadGroup);
337
338     if(This->load_group)
339         nsILoadGroup_Release(This->load_group);
340     if(aLoadGroup)
341         nsILoadGroup_AddRef(aLoadGroup);
342
343     This->load_group = aLoadGroup;
344
345     if(This->channel)
346         return nsIChannel_SetLoadGroup(This->channel, aLoadGroup);
347     return NS_OK;
348 }
349
350 static nsresult NSAPI nsChannel_GetLoadFlags(nsIHttpChannel *iface, nsLoadFlags *aLoadFlags)
351 {
352     nsChannel *This = NSCHANNEL_THIS(iface);
353
354     TRACE("(%p)->(%p)\n", This, aLoadFlags);
355
356     *aLoadFlags = This->load_flags;
357     return NS_OK;
358 }
359
360 static nsresult NSAPI nsChannel_SetLoadFlags(nsIHttpChannel *iface, nsLoadFlags aLoadFlags)
361 {
362     nsChannel *This = NSCHANNEL_THIS(iface);
363
364     TRACE("(%p)->(%08x)\n", This, aLoadFlags);
365
366     This->load_flags = aLoadFlags;
367
368     if(This->channel)
369         return nsIChannel_SetLoadFlags(This->channel, aLoadFlags);
370     return NS_OK;
371 }
372
373 static nsresult NSAPI nsChannel_GetOriginalURI(nsIHttpChannel *iface, nsIURI **aOriginalURI)
374 {
375     nsChannel *This = NSCHANNEL_THIS(iface);
376
377     TRACE("(%p)->(%p)\n", This, aOriginalURI);
378
379     if(This->original_uri)
380         nsIURI_AddRef(This->original_uri);
381
382     *aOriginalURI = This->original_uri;
383     return NS_OK;
384 }
385
386 static nsresult NSAPI nsChannel_SetOriginalURI(nsIHttpChannel *iface, nsIURI *aOriginalURI)
387 {
388     nsChannel *This = NSCHANNEL_THIS(iface);
389
390     TRACE("(%p)->(%p)\n", This, aOriginalURI);
391
392     if(This->original_uri)
393         nsIURI_Release(This->original_uri);
394
395     nsIURI_AddRef(aOriginalURI);
396     This->original_uri = aOriginalURI;
397
398     if(This->channel)
399         return nsIChannel_SetOriginalURI(This->channel, aOriginalURI);
400     return NS_OK;
401 }
402
403 static nsresult NSAPI nsChannel_GetURI(nsIHttpChannel *iface, nsIURI **aURI)
404 {
405     nsChannel *This = NSCHANNEL_THIS(iface);
406
407     TRACE("(%p)->(%p)\n", This, aURI);
408
409     nsIWineURI_AddRef(This->uri);
410     *aURI = (nsIURI*)This->uri;
411
412     return NS_OK;
413 }
414
415 static nsresult NSAPI nsChannel_GetOwner(nsIHttpChannel *iface, nsISupports **aOwner)
416 {
417     nsChannel *This = NSCHANNEL_THIS(iface);
418
419     TRACE("(%p)->(%p)\n", This, aOwner);
420
421     if(This->channel)
422         return nsIChannel_GetOwner(This->channel, aOwner);
423
424     if(This->owner)
425         nsISupports_AddRef(This->owner);
426     *aOwner = This->owner;
427
428     return NS_OK;
429 }
430
431 static nsresult NSAPI nsChannel_SetOwner(nsIHttpChannel *iface, nsISupports *aOwner)
432 {
433     nsChannel *This = NSCHANNEL_THIS(iface);
434
435     TRACE("(%p)->(%p)\n", This, aOwner);
436
437     if(This->channel)
438         return nsIChannel_SetOwner(This->channel, aOwner);
439
440     if(aOwner)
441         nsISupports_AddRef(aOwner);
442     if(This->owner)
443         nsISupports_Release(This->owner);
444     This->owner = aOwner;
445
446     return NS_OK;
447 }
448
449 static nsresult NSAPI nsChannel_GetNotificationCallbacks(nsIHttpChannel *iface,
450         nsIInterfaceRequestor **aNotificationCallbacks)
451 {
452     nsChannel *This = NSCHANNEL_THIS(iface);
453
454     TRACE("(%p)->(%p)\n", This, aNotificationCallbacks);
455
456     if(This->notif_callback)
457         nsIInterfaceRequestor_AddRef(This->notif_callback);
458     *aNotificationCallbacks = This->notif_callback;
459
460     return NS_OK;
461 }
462
463 static nsresult NSAPI nsChannel_SetNotificationCallbacks(nsIHttpChannel *iface,
464         nsIInterfaceRequestor *aNotificationCallbacks)
465 {
466     nsChannel *This = NSCHANNEL_THIS(iface);
467
468     TRACE("(%p)->(%p)\n", This, aNotificationCallbacks);
469
470     if(This->notif_callback)
471         nsIInterfaceRequestor_Release(This->notif_callback);
472     if(aNotificationCallbacks)
473         nsIInterfaceRequestor_AddRef(aNotificationCallbacks);
474
475     This->notif_callback = aNotificationCallbacks;
476
477     if(This->channel)
478         return nsIChannel_SetNotificationCallbacks(This->channel, aNotificationCallbacks);
479     return NS_OK;
480 }
481
482 static nsresult NSAPI nsChannel_GetSecurityInfo(nsIHttpChannel *iface, nsISupports **aSecurityInfo)
483 {
484     nsChannel *This = NSCHANNEL_THIS(iface);
485
486     TRACE("(%p)->(%p)\n", This, aSecurityInfo);
487
488     if(This->channel)
489         return nsIChannel_GetSecurityInfo(This->channel, aSecurityInfo);
490
491     FIXME("default action not implemented\n");
492     return NS_ERROR_NOT_IMPLEMENTED;
493 }
494
495 static nsresult NSAPI nsChannel_GetContentType(nsIHttpChannel *iface, nsACString *aContentType)
496 {
497     nsChannel *This = NSCHANNEL_THIS(iface);
498
499     TRACE("(%p)->(%p)\n", This, aContentType);
500
501     if(This->content_type) {
502         nsACString_SetData(aContentType, This->content_type);
503         return S_OK;
504     }
505
506     if(This->channel)
507         return nsIChannel_GetContentType(This->channel, aContentType);
508
509     TRACE("returning default text/html\n");
510     nsACString_SetData(aContentType, "text/html");
511     return NS_OK;
512 }
513
514 static nsresult NSAPI nsChannel_SetContentType(nsIHttpChannel *iface,
515                                                const nsACString *aContentType)
516 {
517     nsChannel *This = NSCHANNEL_THIS(iface);
518     const char *content_type;
519
520     TRACE("(%p)->(%p)\n", This, aContentType);
521
522     nsACString_GetData(aContentType, &content_type);
523
524     TRACE("content_type %s\n", content_type);
525
526     heap_free(This->content_type);
527     This->content_type = heap_strdupA(content_type);
528
529     if(This->channel)
530         return nsIChannel_SetContentType(This->channel, aContentType);
531
532     return NS_OK;
533 }
534
535 static nsresult NSAPI nsChannel_GetContentCharset(nsIHttpChannel *iface,
536                                                   nsACString *aContentCharset)
537 {
538     nsChannel *This = NSCHANNEL_THIS(iface);
539
540     TRACE("(%p)->(%p)\n", This, aContentCharset);
541
542     if(This->charset) {
543         nsACString_SetData(aContentCharset, This->charset);
544         return NS_OK;
545     }
546
547     if(This->channel) {
548         nsresult nsres = nsIChannel_GetContentCharset(This->channel, aContentCharset);
549         const char *ch;
550         nsACString_GetData(aContentCharset, &ch);
551         return nsres;
552     }
553
554     nsACString_SetData(aContentCharset, "");
555     return NS_OK;
556 }
557
558 static nsresult NSAPI nsChannel_SetContentCharset(nsIHttpChannel *iface,
559                                                   const nsACString *aContentCharset)
560 {
561     nsChannel *This = NSCHANNEL_THIS(iface);
562
563     TRACE("(%p)->(%p)\n", This, aContentCharset);
564
565     if(This->channel)
566         return nsIChannel_SetContentCharset(This->channel, aContentCharset);
567
568     FIXME("default action not implemented\n");
569     return NS_ERROR_NOT_IMPLEMENTED;
570 }
571
572 static nsresult NSAPI nsChannel_GetContentLength(nsIHttpChannel *iface, PRInt32 *aContentLength)
573 {
574     nsChannel *This = NSCHANNEL_THIS(iface);
575
576     TRACE("(%p)->(%p)\n", This, aContentLength);
577
578     if(This->channel)
579         return nsIChannel_GetContentLength(This->channel, aContentLength);
580
581     FIXME("default action not implemented\n");
582     return NS_ERROR_NOT_IMPLEMENTED;
583 }
584
585 static nsresult NSAPI nsChannel_SetContentLength(nsIHttpChannel *iface, PRInt32 aContentLength)
586 {
587     nsChannel *This = NSCHANNEL_THIS(iface);
588
589     TRACE("(%p)->(%d)\n", This, aContentLength);
590
591     if(This->channel)
592         return nsIChannel_SetContentLength(This->channel, aContentLength);
593
594     FIXME("default action not implemented\n");
595     return NS_ERROR_NOT_IMPLEMENTED;
596 }
597
598 static nsresult NSAPI nsChannel_Open(nsIHttpChannel *iface, nsIInputStream **_retval)
599 {
600     nsChannel *This = NSCHANNEL_THIS(iface);
601
602     TRACE("(%p)->(%p)\n", This, _retval);
603
604     if(This->channel)
605         return nsIChannel_Open(This->channel, _retval);
606
607     FIXME("default action not implemented\n");
608     return NS_ERROR_NOT_IMPLEMENTED;
609 }
610
611 static HRESULT create_mon_for_nschannel(nsChannel *channel, IMoniker **mon)
612 {
613     nsIWineURI *wine_uri;
614     LPCWSTR wine_url;
615     nsresult nsres;
616     HRESULT hres;
617
618     if(!channel->original_uri) {
619         ERR("original_uri == NULL\n");
620         return E_FAIL;
621     }
622
623     nsres = nsIURI_QueryInterface(channel->original_uri, &IID_nsIWineURI, (void**)&wine_uri);
624     if(NS_FAILED(nsres)) {
625         ERR("Could not get nsIWineURI: %08x\n", nsres);
626         return E_FAIL;
627     }
628
629     nsIWineURI_GetWineURL(wine_uri, &wine_url);
630     nsIWineURI_Release(wine_uri);
631     if(!wine_url) {
632         TRACE("wine_url == NULL\n");
633         return E_FAIL;
634     }
635
636     hres = CreateURLMoniker(NULL, wine_url, mon);
637     if(FAILED(hres))
638         WARN("CreateURLMonikrer failed: %08x\n", hres);
639
640     return hres;
641 }
642
643 static NSContainer *get_nscontainer_from_load_group(nsChannel *This)
644 {
645     NSContainer *container;
646     nsIChannel *channel;
647     nsIRequest *req;
648     nsIWineURI *wine_uri;
649     nsIURI *uri;
650     nsresult nsres;
651
652     nsres = nsILoadGroup_GetDefaultLoadRequest(This->load_group, &req);
653     if(NS_FAILED(nsres)) {
654         ERR("GetDefaultLoadRequest failed: %08x\n", nsres);
655         return NULL;
656     }
657
658     if(!req)
659         return NULL;
660
661     nsres = nsIRequest_QueryInterface(req, &IID_nsIChannel, (void**)&channel);
662     nsIRequest_Release(req);
663     if(NS_FAILED(nsres)) {
664         WARN("Could not get nsIChannel interface: %08x\n", nsres);
665         return NULL;
666     }
667
668     nsres = nsIChannel_GetURI(channel, &uri);
669     nsIChannel_Release(channel);
670     if(NS_FAILED(nsres)) {
671         ERR("GetURI failed: %08x\n", nsres);
672         return NULL;
673     }
674
675     nsres = nsIURI_QueryInterface(uri, &IID_nsIWineURI, (void**)&wine_uri);
676     nsIURI_Release(uri);
677     if(NS_FAILED(nsres)) {
678         TRACE("Could not get nsIWineURI: %08x\n", nsres);
679         return NULL;
680     }
681
682     nsIWineURI_GetNSContainer(wine_uri, &container);
683     nsIWineURI_Release(wine_uri);
684
685     return container;
686 }
687
688 static nsresult async_open_doc_uri(nsChannel *This, NSContainer *container,
689         nsIStreamListener *listener, nsISupports *context, BOOL *open)
690 {
691     IMoniker *mon;
692     HRESULT hres;
693
694     *open = FALSE;
695
696     if(container->bscallback) {
697         channelbsc_set_channel(container->bscallback, This, listener, context);
698
699         if(container->doc && container->doc->mime) {
700             heap_free(This->content_type);
701             This->content_type = heap_strdupWtoA(container->doc->mime);
702         }
703
704         return NS_OK;
705     }else  {
706         BOOL cont = before_async_open(This, container);
707
708         if(!cont) {
709             TRACE("canceled\n");
710             return NS_ERROR_UNEXPECTED;
711         }
712
713         if(!container->doc) {
714             return This->channel
715                 ?  nsIChannel_AsyncOpen(This->channel, listener, context)
716                 : NS_ERROR_UNEXPECTED;
717         }
718
719         hres = create_mon_for_nschannel(This, &mon);
720         if(FAILED(hres)) {
721             return NS_ERROR_UNEXPECTED;
722         }
723         set_current_mon(container->doc, mon);
724     }
725
726     *open = TRUE;
727     return NS_OK;
728 }
729
730 static nsresult async_open(nsChannel *This, NSContainer *container, nsIStreamListener *listener,
731         nsISupports *context)
732 {
733     nsChannelBSC *bscallback;
734     IMoniker *mon = NULL;
735     task_t *task;
736     HRESULT hres;
737
738     hres = create_mon_for_nschannel(This, &mon);
739     if(FAILED(hres))
740         return NS_ERROR_UNEXPECTED;
741
742     bscallback = create_channelbsc(mon);
743     IMoniker_Release(mon);
744
745     channelbsc_set_channel(bscallback, This, listener, context);
746
747     task = heap_alloc(sizeof(task_t));
748
749     task->doc = container->doc;
750     task->task_id = TASK_START_BINDING;
751     task->next = NULL;
752     task->bscallback = bscallback;
753
754     push_task(task);
755
756     return NS_OK;
757 }
758
759 static nsresult NSAPI nsChannel_AsyncOpen(nsIHttpChannel *iface, nsIStreamListener *aListener,
760                                           nsISupports *aContext)
761 {
762     nsChannel *This = NSCHANNEL_THIS(iface);
763     NSContainer *container;
764     PRBool is_doc_uri;
765     BOOL open = TRUE;
766     nsresult nsres = NS_OK;
767
768     TRACE("(%p)->(%p %p)\n", This, aListener, aContext);
769
770     nsIWineURI_GetNSContainer(This->uri, &container);
771
772     if(!container && This->load_group) {
773         container = get_nscontainer_from_load_group(This);
774         if(container)
775             nsIWineURI_SetNSContainer(This->uri, container);
776     }
777
778     if(!container) {
779         TRACE("container = NULL\n");
780         return This->channel
781             ? nsIChannel_AsyncOpen(This->channel, aListener, aContext)
782             : NS_ERROR_UNEXPECTED;
783     }
784
785     nsIWineURI_GetIsDocumentURI(This->uri, &is_doc_uri);
786
787     if(is_doc_uri && (This->load_flags & LOAD_INITIAL_DOCUMENT_URI))
788         nsres = async_open_doc_uri(This, container, aListener, aContext, &open);
789
790     if(open)
791         nsres = async_open(This, container, aListener, aContext);
792
793     nsIWebBrowserChrome_Release(NSWBCHROME(container));
794     return nsres;
795 }
796
797 static nsresult NSAPI nsChannel_GetRequestMethod(nsIHttpChannel *iface, nsACString *aRequestMethod)
798 {
799     nsChannel *This = NSCHANNEL_THIS(iface);
800
801     TRACE("(%p)->(%p)\n", This, aRequestMethod);
802
803     if(This->http_channel)
804         return nsIHttpChannel_GetRequestMethod(This->http_channel, aRequestMethod);
805
806     return NS_ERROR_NOT_IMPLEMENTED;
807 }
808
809 static nsresult NSAPI nsChannel_SetRequestMethod(nsIHttpChannel *iface,
810                                                  const nsACString *aRequestMethod)
811 {
812     nsChannel *This = NSCHANNEL_THIS(iface);
813
814     TRACE("(%p)->(%p)\n", This, aRequestMethod);
815
816     if(This->http_channel)
817         return nsIHttpChannel_SetRequestMethod(This->http_channel, aRequestMethod);
818
819     return NS_ERROR_NOT_IMPLEMENTED;
820 }
821
822 static nsresult NSAPI nsChannel_GetReferrer(nsIHttpChannel *iface, nsIURI **aReferrer)
823 {
824     nsChannel *This = NSCHANNEL_THIS(iface);
825
826     TRACE("(%p)->(%p)\n", This, aReferrer);
827
828     if(This->http_channel)
829         return nsIHttpChannel_GetReferrer(This->http_channel, aReferrer);
830
831     return NS_ERROR_NOT_IMPLEMENTED;
832 }
833
834 static nsresult NSAPI nsChannel_SetReferrer(nsIHttpChannel *iface, nsIURI *aReferrer)
835 {
836     nsChannel *This = NSCHANNEL_THIS(iface);
837
838     TRACE("(%p)->(%p)\n", This, aReferrer);
839
840     if(This->http_channel)
841         return nsIHttpChannel_SetReferrer(This->http_channel, aReferrer);
842
843     return NS_ERROR_NOT_IMPLEMENTED;
844 }
845
846 static nsresult NSAPI nsChannel_GetRequestHeader(nsIHttpChannel *iface,
847          const nsACString *aHeader, nsACString *_retval)
848 {
849     nsChannel *This = NSCHANNEL_THIS(iface);
850
851     TRACE("(%p)->(%p %p)\n", This, aHeader, _retval);
852
853     if(This->http_channel)
854         return nsIHttpChannel_GetRequestHeader(This->http_channel, aHeader, _retval);
855
856     return NS_ERROR_NOT_IMPLEMENTED;
857 }
858
859 static nsresult NSAPI nsChannel_SetRequestHeader(nsIHttpChannel *iface,
860          const nsACString *aHeader, const nsACString *aValue, PRBool aMerge)
861 {
862     nsChannel *This = NSCHANNEL_THIS(iface);
863
864     TRACE("(%p)->(%p %p %x)\n", This, aHeader, aValue, aMerge);
865
866     if(This->http_channel)
867         return nsIHttpChannel_SetRequestHeader(This->http_channel, aHeader, aValue, aMerge);
868
869     return NS_ERROR_NOT_IMPLEMENTED;
870 }
871
872 static nsresult NSAPI nsChannel_VisitRequestHeaders(nsIHttpChannel *iface,
873                                                     nsIHttpHeaderVisitor *aVisitor)
874 {
875     nsChannel *This = NSCHANNEL_THIS(iface);
876
877     TRACE("(%p)->(%p)\n", This, aVisitor);
878
879     if(This->http_channel)
880         return nsIHttpChannel_VisitRequestHeaders(This->http_channel, aVisitor);
881
882     return NS_ERROR_NOT_IMPLEMENTED;
883 }
884
885 static nsresult NSAPI nsChannel_GetAllowPipelining(nsIHttpChannel *iface, PRBool *aAllowPipelining)
886 {
887     nsChannel *This = NSCHANNEL_THIS(iface);
888
889     TRACE("(%p)->(%p)\n", This, aAllowPipelining);
890
891     if(This->http_channel)
892         return nsIHttpChannel_GetAllowPipelining(This->http_channel, aAllowPipelining);
893
894     return NS_ERROR_NOT_IMPLEMENTED;
895 }
896
897 static nsresult NSAPI nsChannel_SetAllowPipelining(nsIHttpChannel *iface, PRBool aAllowPipelining)
898 {
899     nsChannel *This = NSCHANNEL_THIS(iface);
900
901     TRACE("(%p)->(%x)\n", This, aAllowPipelining);
902
903     if(This->http_channel)
904         return nsIHttpChannel_SetAllowPipelining(This->http_channel, aAllowPipelining);
905
906     return NS_ERROR_NOT_IMPLEMENTED;
907 }
908
909 static nsresult NSAPI nsChannel_GetRedirectionLimit(nsIHttpChannel *iface, PRUint32 *aRedirectionLimit)
910 {
911     nsChannel *This = NSCHANNEL_THIS(iface);
912
913     TRACE("(%p)->(%p)\n", This, aRedirectionLimit);
914
915     if(This->http_channel)
916         return nsIHttpChannel_GetRedirectionLimit(This->http_channel, aRedirectionLimit);
917
918     return NS_ERROR_NOT_IMPLEMENTED;
919 }
920
921 static nsresult NSAPI nsChannel_SetRedirectionLimit(nsIHttpChannel *iface, PRUint32 aRedirectionLimit)
922 {
923     nsChannel *This = NSCHANNEL_THIS(iface);
924
925     TRACE("(%p)->(%u)\n", This, aRedirectionLimit);
926
927     if(This->http_channel)
928         return nsIHttpChannel_SetRedirectionLimit(This->http_channel, aRedirectionLimit);
929
930     return NS_ERROR_NOT_IMPLEMENTED;
931 }
932
933 static nsresult NSAPI nsChannel_GetResponseStatus(nsIHttpChannel *iface, PRUint32 *aResponseStatus)
934 {
935     nsChannel *This = NSCHANNEL_THIS(iface);
936
937     TRACE("(%p)->(%p)\n", This, aResponseStatus);
938
939     if(This->response_status) {
940         *aResponseStatus = This->response_status;
941         return NS_OK;
942     }
943
944     if(This->http_channel)
945         return nsIHttpChannel_GetResponseStatus(This->http_channel, aResponseStatus);
946
947     return NS_ERROR_UNEXPECTED;
948 }
949
950 static nsresult NSAPI nsChannel_GetResponseStatusText(nsIHttpChannel *iface,
951                                                       nsACString *aResponseStatusText)
952 {
953     nsChannel *This = NSCHANNEL_THIS(iface);
954
955     TRACE("(%p)->(%p)\n", This, aResponseStatusText);
956
957     if(This->http_channel)
958         return nsIHttpChannel_GetResponseStatusText(This->http_channel, aResponseStatusText);
959
960     return NS_ERROR_NOT_IMPLEMENTED;
961 }
962
963 static nsresult NSAPI nsChannel_GetRequestSucceeded(nsIHttpChannel *iface,
964                                                     PRBool *aRequestSucceeded)
965 {
966     nsChannel *This = NSCHANNEL_THIS(iface);
967
968     TRACE("(%p)->(%p)\n", This, aRequestSucceeded);
969
970     if(This->http_channel)
971         return nsIHttpChannel_GetRequestSucceeded(This->http_channel, aRequestSucceeded);
972
973     return NS_ERROR_NOT_IMPLEMENTED;
974 }
975
976 static nsresult NSAPI nsChannel_GetResponseHeader(nsIHttpChannel *iface,
977          const nsACString *header, nsACString *_retval)
978 {
979     nsChannel *This = NSCHANNEL_THIS(iface);
980
981     TRACE("(%p)->(%p %p)\n", This, header, _retval);
982
983     if(This->http_channel)
984         return nsIHttpChannel_GetResponseHeader(This->http_channel, header, _retval);
985
986     return NS_ERROR_NOT_IMPLEMENTED;
987 }
988
989 static nsresult NSAPI nsChannel_SetResponseHeader(nsIHttpChannel *iface,
990         const nsACString *header, const nsACString *value, PRBool merge)
991 {
992     nsChannel *This = NSCHANNEL_THIS(iface);
993
994     TRACE("(%p)->(%p %p %x)\n", This, header, value, merge);
995
996     if(This->http_channel)
997         return nsIHttpChannel_SetResponseHeader(This->http_channel, header, value, merge);
998
999     return NS_ERROR_NOT_IMPLEMENTED;
1000 }
1001
1002 static nsresult NSAPI nsChannel_VisitResponseHeaders(nsIHttpChannel *iface,
1003         nsIHttpHeaderVisitor *aVisitor)
1004 {
1005     nsChannel *This = NSCHANNEL_THIS(iface);
1006
1007     TRACE("(%p)->(%p)\n", This, aVisitor);
1008
1009     if(This->http_channel)
1010         return nsIHttpChannel_VisitResponseHeaders(This->http_channel, aVisitor);
1011
1012     return NS_ERROR_NOT_IMPLEMENTED;
1013 }
1014
1015 static nsresult NSAPI nsChannel_IsNoStoreResponse(nsIHttpChannel *iface, PRBool *_retval)
1016 {
1017     nsChannel *This = NSCHANNEL_THIS(iface);
1018
1019     TRACE("(%p)->(%p)\n", This, _retval);
1020
1021     if(This->http_channel)
1022         return nsIHttpChannel_IsNoStoreResponse(This->http_channel, _retval);
1023
1024     return NS_ERROR_NOT_IMPLEMENTED;
1025 }
1026
1027 static nsresult NSAPI nsChannel_IsNoCacheResponse(nsIHttpChannel *iface, PRBool *_retval)
1028 {
1029     nsChannel *This = NSCHANNEL_THIS(iface);
1030
1031     TRACE("(%p)->(%p)\n", This, _retval);
1032
1033     if(This->http_channel)
1034         return nsIHttpChannel_IsNoCacheResponse(This->http_channel, _retval);
1035
1036     return NS_ERROR_NOT_IMPLEMENTED;
1037 }
1038
1039 #undef NSCHANNEL_THIS
1040
1041 static const nsIHttpChannelVtbl nsChannelVtbl = {
1042     nsChannel_QueryInterface,
1043     nsChannel_AddRef,
1044     nsChannel_Release,
1045     nsChannel_GetName,
1046     nsChannel_IsPending,
1047     nsChannel_GetStatus,
1048     nsChannel_Cancel,
1049     nsChannel_Suspend,
1050     nsChannel_Resume,
1051     nsChannel_GetLoadGroup,
1052     nsChannel_SetLoadGroup,
1053     nsChannel_GetLoadFlags,
1054     nsChannel_SetLoadFlags,
1055     nsChannel_GetOriginalURI,
1056     nsChannel_SetOriginalURI,
1057     nsChannel_GetURI,
1058     nsChannel_GetOwner,
1059     nsChannel_SetOwner,
1060     nsChannel_GetNotificationCallbacks,
1061     nsChannel_SetNotificationCallbacks,
1062     nsChannel_GetSecurityInfo,
1063     nsChannel_GetContentType,
1064     nsChannel_SetContentType,
1065     nsChannel_GetContentCharset,
1066     nsChannel_SetContentCharset,
1067     nsChannel_GetContentLength,
1068     nsChannel_SetContentLength,
1069     nsChannel_Open,
1070     nsChannel_AsyncOpen,
1071     nsChannel_GetRequestMethod,
1072     nsChannel_SetRequestMethod,
1073     nsChannel_GetReferrer,
1074     nsChannel_SetReferrer,
1075     nsChannel_GetRequestHeader,
1076     nsChannel_SetRequestHeader,
1077     nsChannel_VisitRequestHeaders,
1078     nsChannel_GetAllowPipelining,
1079     nsChannel_SetAllowPipelining,
1080     nsChannel_GetRedirectionLimit,
1081     nsChannel_SetRedirectionLimit,
1082     nsChannel_GetResponseStatus,
1083     nsChannel_GetResponseStatusText,
1084     nsChannel_GetRequestSucceeded,
1085     nsChannel_GetResponseHeader,
1086     nsChannel_SetResponseHeader,
1087     nsChannel_VisitResponseHeaders,
1088     nsChannel_IsNoStoreResponse,
1089     nsChannel_IsNoCacheResponse
1090 };
1091
1092 #define NSUPCHANNEL_THIS(iface) DEFINE_THIS(nsChannel, UploadChannel, iface)
1093
1094 static nsresult NSAPI nsUploadChannel_QueryInterface(nsIUploadChannel *iface, nsIIDRef riid,
1095                                                      nsQIResult result)
1096 {
1097     nsChannel *This = NSUPCHANNEL_THIS(iface);
1098     return nsIChannel_QueryInterface(NSCHANNEL(This), riid, result);
1099 }
1100
1101 static nsrefcnt NSAPI nsUploadChannel_AddRef(nsIUploadChannel *iface)
1102 {
1103     nsChannel *This = NSUPCHANNEL_THIS(iface);
1104     return nsIChannel_AddRef(NSCHANNEL(This));
1105 }
1106
1107 static nsrefcnt NSAPI nsUploadChannel_Release(nsIUploadChannel *iface)
1108 {
1109     nsChannel *This = NSUPCHANNEL_THIS(iface);
1110     return nsIChannel_Release(NSCHANNEL(This));
1111 }
1112
1113 static nsresult NSAPI nsUploadChannel_SetUploadStream(nsIUploadChannel *iface,
1114         nsIInputStream *aStream, const nsACString *aContentType, PRInt32 aContentLength)
1115 {
1116     nsChannel *This = NSUPCHANNEL_THIS(iface);
1117     const char *content_type;
1118     nsresult nsres;
1119
1120     TRACE("(%p)->(%p %p %d)\n", This, aStream, aContentType, aContentLength);
1121
1122     if(This->post_data_stream)
1123         nsIInputStream_Release(This->post_data_stream);
1124
1125     if(aContentType) {
1126         nsACString_GetData(aContentType, &content_type);
1127         if(*content_type)
1128             FIXME("Unsupported aContentType argument: %s\n", debugstr_a(content_type));
1129     }
1130
1131     if(aContentLength != -1)
1132         FIXME("Unsupported acontentLength = %d\n", aContentLength);
1133
1134     if(This->post_data_stream)
1135         nsIInputStream_Release(This->post_data_stream);
1136     This->post_data_stream = aStream;
1137     if(aStream)
1138         nsIInputStream_AddRef(aStream);
1139
1140     if(This->post_data_stream) {
1141         nsIUploadChannel *upload_channel;
1142
1143         nsres = nsIChannel_QueryInterface(This->channel, &IID_nsIUploadChannel,
1144                 (void**)&upload_channel);
1145         if(NS_SUCCEEDED(nsres)) {
1146             nsres = nsIUploadChannel_SetUploadStream(upload_channel, aStream, aContentType, aContentLength);
1147             nsIUploadChannel_Release(upload_channel);
1148             if(NS_FAILED(nsres))
1149                 WARN("SetUploadStream failed: %08x\n", nsres);
1150
1151         }
1152     }
1153
1154     return NS_OK;
1155 }
1156
1157 static nsresult NSAPI nsUploadChannel_GetUploadStream(nsIUploadChannel *iface,
1158         nsIInputStream **aUploadStream)
1159 {
1160     nsChannel *This = NSUPCHANNEL_THIS(iface);
1161
1162     TRACE("(%p)->(%p)\n", This, aUploadStream);
1163
1164     if(This->post_data_stream)
1165         nsIInputStream_AddRef(This->post_data_stream);
1166
1167     *aUploadStream = This->post_data_stream;
1168     return NS_OK;
1169 }
1170
1171 #undef NSUPCHANNEL_THIS
1172
1173 static const nsIUploadChannelVtbl nsUploadChannelVtbl = {
1174     nsUploadChannel_QueryInterface,
1175     nsUploadChannel_AddRef,
1176     nsUploadChannel_Release,
1177     nsUploadChannel_SetUploadStream,
1178     nsUploadChannel_GetUploadStream
1179 };
1180
1181 #define NSHTTPINTERNAL_THIS(iface) DEFINE_THIS(nsChannel, IHttpChannelInternal, iface)
1182
1183 static nsresult NSAPI nsHttpChannelInternal_QueryInterface(nsIHttpChannelInternal *iface, nsIIDRef riid,
1184         nsQIResult result)
1185 {
1186     nsChannel *This = NSHTTPINTERNAL_THIS(iface);
1187     return nsIChannel_QueryInterface(NSCHANNEL(This), riid, result);
1188 }
1189
1190 static nsrefcnt NSAPI nsHttpChannelInternal_AddRef(nsIHttpChannelInternal *iface)
1191 {
1192     nsChannel *This = NSHTTPINTERNAL_THIS(iface);
1193     return nsIChannel_AddRef(NSCHANNEL(This));
1194 }
1195
1196 static nsrefcnt NSAPI nsHttpChannelInternal_Release(nsIHttpChannelInternal *iface)
1197 {
1198     nsChannel *This = NSHTTPINTERNAL_THIS(iface);
1199     return nsIChannel_Release(NSCHANNEL(This));
1200 }
1201
1202 static nsresult NSAPI nsHttpChannelInternal_GetDocumentURI(nsIHttpChannelInternal *iface, nsIURI **aDocumentURI)
1203 {
1204     nsChannel *This = NSHTTPINTERNAL_THIS(iface);
1205
1206     TRACE("(%p)->()\n", This);
1207
1208     if(This->http_channel_internal)
1209         return nsIHttpChannelInternal_GetDocumentURI(This->http_channel_internal, aDocumentURI);
1210     return NS_ERROR_NOT_IMPLEMENTED;
1211 }
1212
1213 static nsresult NSAPI nsHttpChannelInternal_SetDocumentURI(nsIHttpChannelInternal *iface, nsIURI *aDocumentURI)
1214 {
1215     nsChannel *This = NSHTTPINTERNAL_THIS(iface);
1216
1217     TRACE("(%p)->()\n", This);
1218
1219     if(This->http_channel_internal)
1220         return nsIHttpChannelInternal_SetDocumentURI(This->http_channel_internal, aDocumentURI);
1221     return NS_ERROR_NOT_IMPLEMENTED;
1222 }
1223
1224 static nsresult NSAPI nsHttpChannelInternal_GetRequestVersion(nsIHttpChannelInternal *iface, PRUint32 *major, PRUint32 *minor)
1225 {
1226     nsChannel *This = NSHTTPINTERNAL_THIS(iface);
1227
1228     TRACE("(%p)->()\n", This);
1229
1230     if(This->http_channel_internal)
1231         return nsIHttpChannelInternal_GetRequestVersion(This->http_channel_internal, major, minor);
1232     return NS_ERROR_NOT_IMPLEMENTED;
1233 }
1234
1235 static nsresult NSAPI nsHttpChannelInternal_GetResponseVersion(nsIHttpChannelInternal *iface, PRUint32 *major, PRUint32 *minor)
1236 {
1237     nsChannel *This = NSHTTPINTERNAL_THIS(iface);
1238
1239     TRACE("(%p)->()\n", This);
1240
1241     if(This->http_channel_internal)
1242         return nsIHttpChannelInternal_GetResponseVersion(This->http_channel_internal, major, minor);
1243     return NS_ERROR_NOT_IMPLEMENTED;
1244 }
1245
1246 static nsresult NSAPI nsHttpChannelInternal_SetCookie(nsIHttpChannelInternal *iface, const char *aCookieHeader)
1247 {
1248     nsChannel *This = NSHTTPINTERNAL_THIS(iface);
1249
1250     TRACE("(%p)->()\n", This);
1251
1252     if(This->http_channel_internal)
1253         return nsIHttpChannelInternal_SetCookie(This->http_channel_internal, aCookieHeader);
1254     return NS_ERROR_NOT_IMPLEMENTED;
1255 }
1256
1257 static nsresult NSAPI nsHttpChannelInternal_SetupFallbackChannel(nsIHttpChannelInternal *iface, const char *aFallbackKey)
1258 {
1259     nsChannel *This = NSHTTPINTERNAL_THIS(iface);
1260
1261     TRACE("(%p)->()\n", This);
1262
1263     if(This->http_channel_internal)
1264         return nsIHttpChannelInternal_SetupFallbackChannel(This->http_channel_internal, aFallbackKey);
1265     return NS_ERROR_NOT_IMPLEMENTED;
1266 }
1267
1268 static nsresult NSAPI nsHttpChannelInternal_GetForceAllowThirdPartyCookie(nsIHttpChannelInternal *iface, PRBool *aForceThirdPartyCookie)
1269 {
1270     nsChannel *This = NSHTTPINTERNAL_THIS(iface);
1271
1272     TRACE("(%p)->()\n", This);
1273
1274     if(This->http_channel_internal)
1275         return nsIHttpChannelInternal_GetForceAllowThirdPartyCookie(This->http_channel_internal, aForceThirdPartyCookie);
1276     return NS_ERROR_NOT_IMPLEMENTED;
1277 }
1278
1279 static nsresult NSAPI nsHttpChannelInternal_SetForceAllowThirdPartyCookie(nsIHttpChannelInternal *iface, PRBool aForceThirdPartyCookie)
1280 {
1281     nsChannel *This = NSHTTPINTERNAL_THIS(iface);
1282
1283     TRACE("(%p)->()\n", This);
1284
1285     if(This->http_channel_internal)
1286         return nsIHttpChannelInternal_SetForceAllowThirdPartyCookie(This->http_channel_internal, aForceThirdPartyCookie);
1287     return NS_ERROR_NOT_IMPLEMENTED;
1288 }
1289
1290 #undef NSHTTPINTERNAL_THIS
1291
1292 static const nsIHttpChannelInternalVtbl nsHttpChannelInternalVtbl = {
1293     nsHttpChannelInternal_QueryInterface,
1294     nsHttpChannelInternal_AddRef,
1295     nsHttpChannelInternal_Release,
1296     nsHttpChannelInternal_GetDocumentURI,
1297     nsHttpChannelInternal_SetDocumentURI,
1298     nsHttpChannelInternal_GetRequestVersion,
1299     nsHttpChannelInternal_GetResponseVersion,
1300     nsHttpChannelInternal_SetCookie,
1301     nsHttpChannelInternal_SetupFallbackChannel,
1302     nsHttpChannelInternal_GetForceAllowThirdPartyCookie,
1303     nsHttpChannelInternal_SetForceAllowThirdPartyCookie
1304 };
1305
1306 #define NSURI_THIS(iface) DEFINE_THIS(nsURI, WineURI, iface)
1307
1308 static nsresult NSAPI nsURI_QueryInterface(nsIWineURI *iface, nsIIDRef riid, nsQIResult result)
1309 {
1310     nsURI *This = NSURI_THIS(iface);
1311
1312     *result = NULL;
1313
1314     if(IsEqualGUID(&IID_nsISupports, riid)) {
1315         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1316         *result = NSURI(This);
1317     }else if(IsEqualGUID(&IID_nsIURI, riid)) {
1318         TRACE("(%p)->(IID_nsIURI %p)\n", This, result);
1319         *result = NSURI(This);
1320     }else if(IsEqualGUID(&IID_nsIWineURI, riid)) {
1321         TRACE("(%p)->(IID_nsIWineURI %p)\n", This, result);
1322         *result = NSURI(This);
1323     }
1324
1325     if(*result) {
1326         nsIURI_AddRef(NSURI(This));
1327         return NS_OK;
1328     }
1329
1330     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1331     return This->uri ? nsIURI_QueryInterface(This->uri, riid, result) : NS_NOINTERFACE;
1332 }
1333
1334 static nsrefcnt NSAPI nsURI_AddRef(nsIWineURI *iface)
1335 {
1336     nsURI *This = NSURI_THIS(iface);
1337     LONG ref = InterlockedIncrement(&This->ref);
1338
1339     TRACE("(%p) ref=%d\n", This, ref);
1340
1341     return ref;
1342 }
1343
1344 static nsrefcnt NSAPI nsURI_Release(nsIWineURI *iface)
1345 {
1346     nsURI *This = NSURI_THIS(iface);
1347     LONG ref = InterlockedDecrement(&This->ref);
1348
1349     TRACE("(%p) ref=%d\n", This, ref);
1350
1351     if(!ref) {
1352         if(This->container)
1353             nsIWebBrowserChrome_Release(NSWBCHROME(This->container));
1354         if(This->uri)
1355             nsIURI_Release(This->uri);
1356         heap_free(This->wine_url);
1357         heap_free(This);
1358     }
1359
1360     return ref;
1361 }
1362
1363 static nsresult NSAPI nsURI_GetSpec(nsIWineURI *iface, nsACString *aSpec)
1364 {
1365     nsURI *This = NSURI_THIS(iface);
1366
1367     TRACE("(%p)->(%p)\n", This, aSpec);
1368
1369     if(This->use_wine_url) {
1370         char speca[INTERNET_MAX_URL_LENGTH] = "wine:";
1371         WideCharToMultiByte(CP_ACP, 0, This->wine_url, -1, speca+5, sizeof(speca)-5, NULL, NULL);
1372         nsACString_SetData(aSpec, speca);
1373
1374         return NS_OK;
1375     }
1376
1377     if(This->uri)
1378         return nsIURI_GetSpec(This->uri, aSpec);
1379
1380     TRACE("returning error\n");
1381     return NS_ERROR_NOT_IMPLEMENTED;
1382
1383 }
1384
1385 static nsresult NSAPI nsURI_SetSpec(nsIWineURI *iface, const nsACString *aSpec)
1386 {
1387     nsURI *This = NSURI_THIS(iface);
1388
1389     TRACE("(%p)->(%p)\n", This, aSpec);
1390
1391     if(This->uri)
1392         return nsIURI_SetSpec(This->uri, aSpec);
1393
1394     FIXME("default action not implemented\n");
1395     return NS_ERROR_NOT_IMPLEMENTED;
1396 }
1397
1398 static nsresult NSAPI nsURI_GetPrePath(nsIWineURI *iface, nsACString *aPrePath)
1399 {
1400     nsURI *This = NSURI_THIS(iface);
1401
1402     TRACE("(%p)->(%p)\n", This, aPrePath);
1403
1404     if(This->uri)
1405         return nsIURI_GetPrePath(This->uri, aPrePath);
1406
1407     FIXME("default action not implemented\n");
1408     return NS_ERROR_NOT_IMPLEMENTED;
1409 }
1410
1411 static nsresult NSAPI nsURI_GetScheme(nsIWineURI *iface, nsACString *aScheme)
1412 {
1413     nsURI *This = NSURI_THIS(iface);
1414
1415     TRACE("(%p)->(%p)\n", This, aScheme);
1416
1417     if(This->use_wine_url && strcmpW(This->wine_url, about_blankW)) {
1418         /*
1419          * For Gecko we set scheme to unknown so it won't be handled
1420          * as any special case.
1421          */
1422         nsACString_SetData(aScheme, "wine");
1423         return NS_OK;
1424     }
1425
1426     if(This->uri)
1427         return nsIURI_GetScheme(This->uri, aScheme);
1428
1429     TRACE("returning error\n");
1430     return NS_ERROR_NOT_IMPLEMENTED;
1431 }
1432
1433 static nsresult NSAPI nsURI_SetScheme(nsIWineURI *iface, const nsACString *aScheme)
1434 {
1435     nsURI *This = NSURI_THIS(iface);
1436
1437     TRACE("(%p)->(%p)\n", This, aScheme);
1438
1439     if(This->uri)
1440         return nsIURI_SetScheme(This->uri, aScheme);
1441
1442     FIXME("default action not implemented\n");
1443     return NS_ERROR_NOT_IMPLEMENTED;
1444 }
1445
1446 static nsresult NSAPI nsURI_GetUserPass(nsIWineURI *iface, nsACString *aUserPass)
1447 {
1448     nsURI *This = NSURI_THIS(iface);
1449
1450     TRACE("(%p)->(%p)\n", This, aUserPass);
1451
1452     if(This->uri)
1453         return nsIURI_GetUserPass(This->uri, aUserPass);
1454
1455     FIXME("default action not implemented\n");
1456     return NS_ERROR_NOT_IMPLEMENTED;
1457 }
1458
1459 static nsresult NSAPI nsURI_SetUserPass(nsIWineURI *iface, const nsACString *aUserPass)
1460 {
1461     nsURI *This = NSURI_THIS(iface);
1462
1463     TRACE("(%p)->(%p)\n", This, aUserPass);
1464
1465     if(This->uri)
1466         return nsIURI_SetUserPass(This->uri, aUserPass);
1467
1468     FIXME("default action not implemented\n");
1469     return NS_ERROR_NOT_IMPLEMENTED;
1470 }
1471
1472 static nsresult NSAPI nsURI_GetUsername(nsIWineURI *iface, nsACString *aUsername)
1473 {
1474     nsURI *This = NSURI_THIS(iface);
1475
1476     TRACE("(%p)->(%p)\n", This, aUsername);
1477
1478     if(This->uri)
1479         return nsIURI_GetUsername(This->uri, aUsername);
1480
1481     FIXME("default action not implemented\n");
1482     return NS_ERROR_NOT_IMPLEMENTED;
1483 }
1484
1485 static nsresult NSAPI nsURI_SetUsername(nsIWineURI *iface, const nsACString *aUsername)
1486 {
1487     nsURI *This = NSURI_THIS(iface);
1488
1489     TRACE("(%p)->(%p)\n", This, aUsername);
1490
1491     if(This->uri)
1492         return nsIURI_SetUsername(This->uri, aUsername);
1493
1494     FIXME("default action not implemented\n");
1495     return NS_ERROR_NOT_IMPLEMENTED;
1496 }
1497
1498 static nsresult NSAPI nsURI_GetPassword(nsIWineURI *iface, nsACString *aPassword)
1499 {
1500     nsURI *This = NSURI_THIS(iface);
1501
1502     TRACE("(%p)->(%p)\n", This, aPassword);
1503
1504     if(This->uri)
1505         return nsIURI_GetPassword(This->uri, aPassword);
1506
1507     FIXME("default action not implemented\n");
1508     return NS_ERROR_NOT_IMPLEMENTED;
1509 }
1510
1511 static nsresult NSAPI nsURI_SetPassword(nsIWineURI *iface, const nsACString *aPassword)
1512 {
1513     nsURI *This = NSURI_THIS(iface);
1514
1515     TRACE("(%p)->(%p)\n", This, aPassword);
1516
1517     if(This->uri)
1518         return nsIURI_SetPassword(This->uri, aPassword);
1519
1520     FIXME("default action not implemented\n");
1521     return NS_ERROR_NOT_IMPLEMENTED;
1522 }
1523
1524 static nsresult NSAPI nsURI_GetHostPort(nsIWineURI *iface, nsACString *aHostPort)
1525 {
1526     nsURI *This = NSURI_THIS(iface);
1527
1528     TRACE("(%p)->(%p)\n", This, aHostPort);
1529
1530     if(This->uri)
1531         return nsIURI_GetHostPort(This->uri, aHostPort);
1532
1533     FIXME("default action not implemented\n");
1534     return NS_ERROR_NOT_IMPLEMENTED;
1535 }
1536
1537 static nsresult NSAPI nsURI_SetHostPort(nsIWineURI *iface, const nsACString *aHostPort)
1538 {
1539     nsURI *This = NSURI_THIS(iface);
1540
1541     TRACE("(%p)->(%p)\n", This, aHostPort);
1542
1543     if(This->uri)
1544         return nsIURI_SetHostPort(This->uri, aHostPort);
1545
1546     FIXME("default action not implemented\n");
1547     return NS_ERROR_NOT_IMPLEMENTED;
1548 }
1549
1550 static nsresult NSAPI nsURI_GetHost(nsIWineURI *iface, nsACString *aHost)
1551 {
1552     nsURI *This = NSURI_THIS(iface);
1553
1554     TRACE("(%p)->(%p)\n", This, aHost);
1555
1556     if(This->uri)
1557         return nsIURI_GetHost(This->uri, aHost);
1558
1559     FIXME("default action not implemented\n");
1560     return NS_ERROR_NOT_IMPLEMENTED;
1561 }
1562
1563 static nsresult NSAPI nsURI_SetHost(nsIWineURI *iface, const nsACString *aHost)
1564 {
1565     nsURI *This = NSURI_THIS(iface);
1566
1567     TRACE("(%p)->(%p)\n", This, aHost);
1568
1569     if(This->uri)
1570         return nsIURI_SetHost(This->uri, aHost);
1571
1572     FIXME("default action not implemented\n");
1573     return NS_ERROR_NOT_IMPLEMENTED;
1574 }
1575
1576 static nsresult NSAPI nsURI_GetPort(nsIWineURI *iface, PRInt32 *aPort)
1577 {
1578     nsURI *This = NSURI_THIS(iface);
1579
1580     TRACE("(%p)->(%p)\n", This, aPort);
1581
1582     if(This->uri)
1583         return nsIURI_GetPort(This->uri, aPort);
1584
1585     FIXME("default action not implemented\n");
1586     return NS_ERROR_NOT_IMPLEMENTED;
1587 }
1588
1589 static nsresult NSAPI nsURI_SetPort(nsIWineURI *iface, PRInt32 aPort)
1590 {
1591     nsURI *This = NSURI_THIS(iface);
1592
1593     TRACE("(%p)->(%d)\n", This, aPort);
1594
1595     if(This->uri)
1596         return nsIURI_SetPort(This->uri, aPort);
1597
1598     FIXME("default action not implemented\n");
1599     return NS_ERROR_NOT_IMPLEMENTED;
1600 }
1601
1602 static nsresult NSAPI nsURI_GetPath(nsIWineURI *iface, nsACString *aPath)
1603 {
1604     nsURI *This = NSURI_THIS(iface);
1605
1606     TRACE("(%p)->(%p)\n", This, aPath);
1607
1608     if(This->uri)
1609         return nsIURI_GetPath(This->uri, aPath);
1610
1611     FIXME("default action not implemented\n");
1612     return NS_ERROR_NOT_IMPLEMENTED;
1613 }
1614
1615 static nsresult NSAPI nsURI_SetPath(nsIWineURI *iface, const nsACString *aPath)
1616 {
1617     nsURI *This = NSURI_THIS(iface);
1618     const char *path;
1619
1620     nsACString_GetData(aPath, &path);
1621     TRACE("(%p)->(%p(%s))\n", This, aPath, debugstr_a(path));
1622
1623
1624     if(This->wine_url) {
1625         WCHAR new_url[INTERNET_MAX_URL_LENGTH];
1626         DWORD size = sizeof(new_url)/sizeof(WCHAR);
1627         LPWSTR pathw;
1628         HRESULT hres;
1629
1630         pathw = heap_strdupAtoW(path);
1631         hres = UrlCombineW(This->wine_url, pathw, new_url, &size, 0);
1632         heap_free(pathw);
1633         if(SUCCEEDED(hres))
1634             nsIWineURI_SetWineURL(NSWINEURI(This), new_url);
1635         else
1636             WARN("UrlCombine failed: %08x\n", hres);
1637     }
1638
1639     if(!This->uri)
1640         return NS_OK;
1641
1642     return nsIURI_SetPath(This->uri, aPath);
1643 }
1644
1645 static nsresult NSAPI nsURI_Equals(nsIWineURI *iface, nsIURI *other, PRBool *_retval)
1646 {
1647     nsURI *This = NSURI_THIS(iface);
1648     nsIWineURI *wine_uri;
1649     LPCWSTR other_url = NULL;
1650     nsresult nsres;
1651
1652     TRACE("(%p)->(%p %p)\n", This, other, _retval);
1653
1654     if(This->uri)
1655         return nsIURI_Equals(This->uri, other, _retval);
1656
1657     nsres = nsIURI_QueryInterface(other, &IID_nsIWineURI, (void**)&wine_uri);
1658     if(NS_FAILED(nsres)) {
1659         TRACE("Could not get nsIWineURI interface\n");
1660         *_retval = FALSE;
1661         return NS_OK;
1662     }
1663
1664     nsIWineURI_GetWineURL(wine_uri, &other_url);
1665     *_retval = !UrlCompareW(This->wine_url, other_url, TRUE);
1666     nsIWineURI_Release(wine_uri);
1667
1668     return NS_OK;
1669 }
1670
1671 static nsresult NSAPI nsURI_SchemeIs(nsIWineURI *iface, const char *scheme, PRBool *_retval)
1672 {
1673     nsURI *This = NSURI_THIS(iface);
1674
1675     TRACE("(%p)->(%s %p)\n", This, debugstr_a(scheme), _retval);
1676
1677     if(This->use_wine_url) {
1678         WCHAR buf[INTERNET_MAX_SCHEME_LENGTH];
1679         int len = MultiByteToWideChar(CP_ACP, 0, scheme, -1, buf, sizeof(buf)/sizeof(WCHAR))-1;
1680
1681         *_retval = lstrlenW(This->wine_url) > len
1682             && This->wine_url[len] == ':'
1683             && !memcmp(buf, This->wine_url, len*sizeof(WCHAR));
1684         return NS_OK;
1685     }
1686
1687     if(This->uri)
1688         return nsIURI_SchemeIs(This->uri, scheme, _retval);
1689
1690     TRACE("returning error\n");
1691     return NS_ERROR_NOT_IMPLEMENTED;
1692 }
1693
1694 static nsresult NSAPI nsURI_Clone(nsIWineURI *iface, nsIURI **_retval)
1695 {
1696     nsURI *This = NSURI_THIS(iface);
1697     nsIURI *nsuri = NULL;
1698     nsIWineURI *wine_uri;
1699     nsresult nsres;
1700
1701     TRACE("(%p)->(%p)\n", This, _retval);
1702
1703     if(This->uri) {
1704         nsres = nsIURI_Clone(This->uri, &nsuri);
1705         if(NS_FAILED(nsres)) {
1706             WARN("Clone failed: %08x\n", nsres);
1707             return nsres;
1708         }
1709     }
1710
1711     nsres = create_uri(nsuri, This->container, &wine_uri);
1712     if(NS_FAILED(nsres)) {
1713         WARN("create_uri failed: %08x\n", nsres);
1714         return nsres;
1715     }
1716
1717     *_retval = (nsIURI*)wine_uri;
1718     return nsIWineURI_SetWineURL(wine_uri, This->wine_url);
1719 }
1720
1721 static nsresult NSAPI nsURI_Resolve(nsIWineURI *iface, const nsACString *arelativePath,
1722         nsACString *_retval)
1723 {
1724     nsURI *This = NSURI_THIS(iface);
1725
1726     TRACE("(%p)->(%p %p)\n", This, arelativePath, _retval);
1727
1728     if(This->uri)
1729         return nsIURI_Resolve(This->uri, arelativePath, _retval);
1730
1731     FIXME("default action not implemented\n");
1732     return NS_ERROR_NOT_IMPLEMENTED;
1733 }
1734
1735 static nsresult NSAPI nsURI_GetAsciiSpec(nsIWineURI *iface, nsACString *aAsciiSpec)
1736 {
1737     nsURI *This = NSURI_THIS(iface);
1738
1739     TRACE("(%p)->(%p)\n", This, aAsciiSpec);
1740
1741     if(This->use_wine_url)
1742         return nsIURI_GetSpec(NSURI(This), aAsciiSpec);
1743
1744     if(This->uri)
1745         return nsIURI_GetAsciiSpec(This->uri, aAsciiSpec);
1746
1747     TRACE("returning error\n");
1748     return NS_ERROR_NOT_IMPLEMENTED;
1749 }
1750
1751 static nsresult NSAPI nsURI_GetAsciiHost(nsIWineURI *iface, nsACString *aAsciiHost)
1752 {
1753     nsURI *This = NSURI_THIS(iface);
1754
1755     TRACE("(%p)->(%p)\n", This, aAsciiHost);
1756
1757     if(This->uri)
1758         return nsIURI_GetAsciiHost(This->uri, aAsciiHost);
1759
1760     FIXME("default action not implemented\n");
1761     return NS_ERROR_NOT_IMPLEMENTED;
1762 }
1763
1764 static nsresult NSAPI nsURI_GetOriginCharset(nsIWineURI *iface, nsACString *aOriginCharset)
1765 {
1766     nsURI *This = NSURI_THIS(iface);
1767
1768     TRACE("(%p)->(%p)\n", This, aOriginCharset);
1769
1770     if(This->uri)
1771         return nsIURI_GetOriginCharset(This->uri, aOriginCharset);
1772
1773     FIXME("default action not implemented\n");
1774     return NS_ERROR_NOT_IMPLEMENTED;
1775 }
1776
1777 static nsresult NSAPI nsURI_GetNSContainer(nsIWineURI *iface, NSContainer **aContainer)
1778 {
1779     nsURI *This = NSURI_THIS(iface);
1780
1781     TRACE("(%p)->(%p)\n", This, aContainer);
1782
1783     if(This->container)
1784         nsIWebBrowserChrome_AddRef(NSWBCHROME(This->container));
1785     *aContainer = This->container;
1786
1787     return NS_OK;
1788 }
1789
1790 static nsresult NSAPI nsURI_SetNSContainer(nsIWineURI *iface, NSContainer *aContainer)
1791 {
1792     nsURI *This = NSURI_THIS(iface);
1793
1794     TRACE("(%p)->(%p)\n", This, aContainer);
1795
1796     if(This->container) {
1797         if(This->container == aContainer)
1798             return NS_OK;
1799         TRACE("Changing %p -> %p\n", This->container, aContainer);
1800         nsIWebBrowserChrome_Release(NSWBCHROME(This->container));
1801     }
1802
1803     if(aContainer)
1804         nsIWebBrowserChrome_AddRef(NSWBCHROME(aContainer));
1805     This->container = aContainer;
1806
1807     return NS_OK;
1808 }
1809
1810 static nsresult NSAPI nsURI_GetIsDocumentURI(nsIWineURI *iface, PRBool *aIsDocumentURI)
1811 {
1812     nsURI *This = NSURI_THIS(iface);
1813
1814     TRACE("(%p)->(%p)\n", This, aIsDocumentURI);
1815
1816     *aIsDocumentURI = This->is_doc_uri;
1817     return NS_OK;
1818 }
1819
1820 static nsresult NSAPI nsURI_SetIsDocumentURI(nsIWineURI *iface, PRBool aIsDocumentURI)
1821 {
1822     nsURI *This = NSURI_THIS(iface);
1823
1824     TRACE("(%p)->(%x)\n", This, aIsDocumentURI);
1825
1826     This->is_doc_uri = aIsDocumentURI;
1827     return NS_OK;
1828 }
1829
1830 static nsresult NSAPI nsURI_GetWineURL(nsIWineURI *iface, LPCWSTR *aURL)
1831 {
1832     nsURI *This = NSURI_THIS(iface);
1833
1834     TRACE("(%p)->(%p)\n", This, aURL);
1835
1836     *aURL = This->wine_url;
1837     return NS_OK;
1838 }
1839
1840 static nsresult NSAPI nsURI_SetWineURL(nsIWineURI *iface, LPCWSTR aURL)
1841 {
1842     nsURI *This = NSURI_THIS(iface);
1843
1844     static const WCHAR wszFtp[]   = {'f','t','p',':'};
1845     static const WCHAR wszHttp[]  = {'h','t','t','p',':'};
1846     static const WCHAR wszHttps[] = {'h','t','t','p','s',':'};
1847
1848     TRACE("(%p)->(%s)\n", This, debugstr_w(aURL));
1849
1850     heap_free(This->wine_url);
1851
1852     if(aURL) {
1853         int len = strlenW(aURL)+1;
1854         This->wine_url = heap_alloc(len*sizeof(WCHAR));
1855         memcpy(This->wine_url, aURL, len*sizeof(WCHAR));
1856
1857         /* FIXME: Always use wine url */
1858         This->use_wine_url =
1859                strncmpW(aURL, wszFtp,   sizeof(wszFtp)/sizeof(WCHAR))
1860             && strncmpW(aURL, wszHttp,  sizeof(wszHttp)/sizeof(WCHAR))
1861             && strncmpW(aURL, wszHttps, sizeof(wszHttps)/sizeof(WCHAR));
1862     }else {
1863         This->wine_url = NULL;
1864         This->use_wine_url = FALSE;
1865     }
1866
1867     return NS_OK;
1868 }
1869
1870 #undef NSURI_THIS
1871
1872 static const nsIWineURIVtbl nsWineURIVtbl = {
1873     nsURI_QueryInterface,
1874     nsURI_AddRef,
1875     nsURI_Release,
1876     nsURI_GetSpec,
1877     nsURI_SetSpec,
1878     nsURI_GetPrePath,
1879     nsURI_GetScheme,
1880     nsURI_SetScheme,
1881     nsURI_GetUserPass,
1882     nsURI_SetUserPass,
1883     nsURI_GetUsername,
1884     nsURI_SetUsername,
1885     nsURI_GetPassword,
1886     nsURI_SetPassword,
1887     nsURI_GetHostPort,
1888     nsURI_SetHostPort,
1889     nsURI_GetHost,
1890     nsURI_SetHost,
1891     nsURI_GetPort,
1892     nsURI_SetPort,
1893     nsURI_GetPath,
1894     nsURI_SetPath,
1895     nsURI_Equals,
1896     nsURI_SchemeIs,
1897     nsURI_Clone,
1898     nsURI_Resolve,
1899     nsURI_GetAsciiSpec,
1900     nsURI_GetAsciiHost,
1901     nsURI_GetOriginCharset,
1902     nsURI_GetNSContainer,
1903     nsURI_SetNSContainer,
1904     nsURI_GetIsDocumentURI,
1905     nsURI_SetIsDocumentURI,
1906     nsURI_GetWineURL,
1907     nsURI_SetWineURL
1908 };
1909
1910 static nsresult create_uri(nsIURI *uri, NSContainer *container, nsIWineURI **_retval)
1911 {
1912     nsURI *ret = heap_alloc(sizeof(nsURI));
1913
1914     ret->lpWineURIVtbl = &nsWineURIVtbl;
1915     ret->ref = 1;
1916     ret->uri = uri;
1917     ret->container = container;
1918     ret->wine_url = NULL;
1919     ret->is_doc_uri = FALSE;
1920     ret->use_wine_url = FALSE;
1921
1922     if(container)
1923         nsIWebBrowserChrome_AddRef(NSWBCHROME(container));
1924
1925     TRACE("retval=%p\n", ret);
1926     *_retval = NSWINEURI(ret);
1927     return NS_OK;
1928 }
1929
1930 typedef struct {
1931     const nsIProtocolHandlerVtbl  *lpProtocolHandlerVtbl;
1932
1933     LONG ref;
1934
1935     nsIProtocolHandler *nshandler;
1936 } nsProtocolHandler;
1937
1938 #define NSPROTHANDLER(x)  ((nsIProtocolHandler*)  &(x)->lpProtocolHandlerVtbl)
1939
1940 #define NSPROTHANDLER_THIS(iface) DEFINE_THIS(nsProtocolHandler, ProtocolHandler, iface)
1941
1942 static nsresult NSAPI nsProtocolHandler_QueryInterface(nsIProtocolHandler *iface, nsIIDRef riid,
1943         nsQIResult result)
1944 {
1945     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1946
1947     *result = NULL;
1948
1949     if(IsEqualGUID(&IID_nsISupports, riid)) {
1950         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1951         *result = NSPROTHANDLER(This);
1952     }else if(IsEqualGUID(&IID_nsIProtocolHandler, riid)) {
1953         TRACE("(%p)->(IID_nsIProtocolHandler %p)\n", This, result);
1954         *result = NSPROTHANDLER(This);
1955     }else if(IsEqualGUID(&IID_nsIExternalProtocolHandler, riid)) {
1956         TRACE("(%p)->(IID_nsIExternalProtocolHandler %p), returning NULL\n", This, result);
1957         return NS_NOINTERFACE;
1958     }
1959
1960     if(*result) {
1961         nsISupports_AddRef((nsISupports*)*result);
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 nsProtocolHandler_AddRef(nsIProtocolHandler *iface)
1970 {
1971     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1972     LONG ref = InterlockedIncrement(&This->ref);
1973
1974     TRACE("(%p) ref=%d\n", This, ref);
1975
1976     return ref;
1977 }
1978
1979 static nsrefcnt NSAPI nsProtocolHandler_Release(nsIProtocolHandler *iface)
1980 {
1981     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1982     LONG ref = InterlockedDecrement(&This->ref);
1983
1984     TRACE("(%p) ref=%d\n", This, ref);
1985
1986     if(!ref) {
1987         if(This->nshandler)
1988             nsIProtocolHandler_Release(This->nshandler);
1989         heap_free(This);
1990     }
1991
1992     return ref;
1993 }
1994
1995 static nsresult NSAPI nsProtocolHandler_GetScheme(nsIProtocolHandler *iface, nsACString *aScheme)
1996 {
1997     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1998
1999     TRACE("(%p)->(%p)\n", This, aScheme);
2000
2001     if(This->nshandler)
2002         return nsIProtocolHandler_GetScheme(This->nshandler, aScheme);
2003     return NS_ERROR_NOT_IMPLEMENTED;
2004 }
2005
2006 static nsresult NSAPI nsProtocolHandler_GetDefaultPort(nsIProtocolHandler *iface,
2007         PRInt32 *aDefaultPort)
2008 {
2009     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
2010
2011     TRACE("(%p)->(%p)\n", This, aDefaultPort);
2012
2013     if(This->nshandler)
2014         return nsIProtocolHandler_GetDefaultPort(This->nshandler, aDefaultPort);
2015     return NS_ERROR_NOT_IMPLEMENTED;
2016 }
2017
2018 static nsresult NSAPI nsProtocolHandler_GetProtocolFlags(nsIProtocolHandler *iface,
2019                                                          PRUint32 *aProtocolFlags)
2020 {
2021     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
2022
2023     TRACE("(%p)->(%p)\n", This, aProtocolFlags);
2024
2025     if(This->nshandler)
2026         return nsIProtocolHandler_GetProtocolFlags(This->nshandler, aProtocolFlags);
2027     return NS_ERROR_NOT_IMPLEMENTED;
2028 }
2029
2030 static nsresult NSAPI nsProtocolHandler_NewURI(nsIProtocolHandler *iface,
2031         const nsACString *aSpec, const char *aOriginCharset, nsIURI *aBaseURI, nsIURI **_retval)
2032 {
2033     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
2034
2035     TRACE("((%p)->%p %s %p %p)\n", This, aSpec, debugstr_a(aOriginCharset), aBaseURI, _retval);
2036
2037     if(This->nshandler)
2038         return nsIProtocolHandler_NewURI(This->nshandler, aSpec, aOriginCharset, aBaseURI, _retval);
2039     return NS_ERROR_NOT_IMPLEMENTED;
2040 }
2041
2042 static nsresult NSAPI nsProtocolHandler_NewChannel(nsIProtocolHandler *iface,
2043         nsIURI *aURI, nsIChannel **_retval)
2044 {
2045     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
2046
2047     TRACE("(%p)->(%p %p)\n", This, aURI, _retval);
2048
2049     if(This->nshandler)
2050         return nsIProtocolHandler_NewChannel(This->nshandler, aURI, _retval);
2051     return NS_ERROR_NOT_IMPLEMENTED;
2052 }
2053
2054 static nsresult NSAPI nsProtocolHandler_AllowPort(nsIProtocolHandler *iface,
2055         PRInt32 port, const char *scheme, PRBool *_retval)
2056 {
2057     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
2058
2059     TRACE("(%p)->(%d %s %p)\n", This, port, debugstr_a(scheme), _retval);
2060
2061     if(This->nshandler)
2062         return nsIProtocolHandler_AllowPort(This->nshandler, port, scheme, _retval);
2063     return NS_ERROR_NOT_IMPLEMENTED;
2064 }
2065
2066 #undef NSPROTHANDLER_THIS
2067
2068 static const nsIProtocolHandlerVtbl nsProtocolHandlerVtbl = {
2069     nsProtocolHandler_QueryInterface,
2070     nsProtocolHandler_AddRef,
2071     nsProtocolHandler_Release,
2072     nsProtocolHandler_GetScheme,
2073     nsProtocolHandler_GetDefaultPort,
2074     nsProtocolHandler_GetProtocolFlags,
2075     nsProtocolHandler_NewURI,
2076     nsProtocolHandler_NewChannel,
2077     nsProtocolHandler_AllowPort
2078 };
2079
2080 static nsIProtocolHandler *create_protocol_handler(nsIProtocolHandler *nshandler)
2081 {
2082     nsProtocolHandler *ret = heap_alloc(sizeof(nsProtocolHandler));
2083
2084     ret->lpProtocolHandlerVtbl = &nsProtocolHandlerVtbl;
2085     ret->ref = 1;
2086     ret->nshandler = nshandler;
2087
2088     return NSPROTHANDLER(ret);
2089 }
2090
2091 static nsresult NSAPI nsIOService_QueryInterface(nsIIOService*,nsIIDRef,nsQIResult);
2092
2093 static nsrefcnt NSAPI nsIOService_AddRef(nsIIOService *iface)
2094 {
2095     return 2;
2096 }
2097
2098 static nsrefcnt NSAPI nsIOService_Release(nsIIOService *iface)
2099 {
2100     return 1;
2101 }
2102
2103 static nsresult NSAPI nsIOService_GetProtocolHandler(nsIIOService *iface, const char *aScheme,
2104                                                      nsIProtocolHandler **_retval)
2105 {
2106     nsIExternalProtocolHandler *nsexthandler;
2107     nsIProtocolHandler *nshandler;
2108     nsresult nsres;
2109
2110     TRACE("(%s %p)\n", debugstr_a(aScheme), _retval);
2111
2112     nsres = nsIIOService_GetProtocolHandler(nsio, aScheme, &nshandler);
2113     if(NS_FAILED(nsres)) {
2114         WARN("GetProtocolHandler failed: %08x\n", nsres);
2115         return nsres;
2116     }
2117
2118     nsres = nsIProtocolHandler_QueryInterface(nshandler, &IID_nsIExternalProtocolHandler,
2119                                               (void**)&nsexthandler);
2120     if(NS_FAILED(nsres)) {
2121         *_retval = nshandler;
2122         return NS_OK;
2123     }
2124
2125     nsIExternalProtocolHandler_Release(nsexthandler);
2126     *_retval = create_protocol_handler(nshandler);
2127     TRACE("return %p\n", *_retval);
2128     return NS_OK;
2129 }
2130
2131 static nsresult NSAPI nsIOService_GetProtocolFlags(nsIIOService *iface, const char *aScheme,
2132                                                     PRUint32 *_retval)
2133 {
2134     TRACE("(%s %p)\n", debugstr_a(aScheme), _retval);
2135     return nsIIOService_GetProtocolFlags(nsio, aScheme, _retval);
2136 }
2137
2138 static BOOL is_gecko_special_uri(const char *spec)
2139 {
2140     static const char *special_schemes[] = {"chrome:", "jar:", "resource:", "javascript:", "wyciwyg:"};
2141     int i;
2142
2143     for(i=0; i < sizeof(special_schemes)/sizeof(*special_schemes); i++) {
2144         if(!strncasecmp(spec, special_schemes[i], strlen(special_schemes[i])))
2145             return TRUE;
2146     }
2147
2148     return FALSE;
2149 }
2150
2151 static nsresult NSAPI nsIOService_NewURI(nsIIOService *iface, const nsACString *aSpec,
2152         const char *aOriginCharset, nsIURI *aBaseURI, nsIURI **_retval)
2153 {
2154     const char *spec = NULL;
2155     NSContainer *nscontainer = NULL;
2156     nsIURI *uri = NULL;
2157     LPCWSTR base_wine_url = NULL;
2158     nsIWineURI *base_wine_uri = NULL, *wine_uri;
2159     BOOL is_wine_uri = FALSE;
2160     nsresult nsres;
2161
2162     nsACString_GetData(aSpec, &spec);
2163
2164     TRACE("(%p(%s) %s %p %p)\n", aSpec, debugstr_a(spec), debugstr_a(aOriginCharset),
2165           aBaseURI, _retval);
2166
2167     if(is_gecko_special_uri(spec))
2168         return nsIIOService_NewURI(nsio, aSpec, aOriginCharset, aBaseURI, _retval);
2169
2170     if(!strncmp(spec, "wine:", 5)) {
2171         spec += 5;
2172         is_wine_uri = TRUE;
2173     }
2174
2175     if(aBaseURI) {
2176         nsACString base_uri_str;
2177         const char *base_uri = NULL;
2178
2179         nsACString_Init(&base_uri_str, NULL);
2180
2181         nsres = nsIURI_GetSpec(aBaseURI, &base_uri_str);
2182         if(NS_SUCCEEDED(nsres)) {
2183             nsACString_GetData(&base_uri_str, &base_uri);
2184             TRACE("base_uri=%s\n", debugstr_a(base_uri));
2185         }else {
2186             ERR("GetSpec failed: %08x\n", nsres);
2187         }
2188
2189         nsACString_Finish(&base_uri_str);
2190     }
2191
2192     nsres = nsIIOService_NewURI(nsio, aSpec, aOriginCharset, aBaseURI, &uri);
2193     if(NS_FAILED(nsres))
2194         TRACE("NewURI failed: %08x\n", nsres);
2195
2196     if(aBaseURI) {
2197         nsres = nsIURI_QueryInterface(aBaseURI, &IID_nsIWineURI, (void**)&base_wine_uri);
2198         if(NS_SUCCEEDED(nsres)) {
2199             nsIWineURI_GetNSContainer(base_wine_uri, &nscontainer);
2200             nsIWineURI_GetWineURL(base_wine_uri, &base_wine_url);
2201         }else {
2202             TRACE("Could not get base nsIWineURI: %08x\n", nsres);
2203         }
2204     }
2205
2206     TRACE("nscontainer = %p\n", nscontainer);
2207
2208     nsres = create_uri(uri, nscontainer, &wine_uri);
2209     *_retval = (nsIURI*)wine_uri;
2210
2211     if(nscontainer)
2212         nsIWebBrowserChrome_Release(NSWBCHROME(nscontainer));
2213
2214     if(base_wine_url) {
2215         WCHAR url[INTERNET_MAX_URL_LENGTH], rel_url[INTERNET_MAX_URL_LENGTH];
2216         DWORD len;
2217         HRESULT hres;
2218
2219         MultiByteToWideChar(CP_ACP, 0, spec, -1, rel_url, sizeof(rel_url)/sizeof(WCHAR));
2220
2221         hres = CoInternetCombineUrl(base_wine_url, rel_url,
2222                                     URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO,
2223                                     url, sizeof(url)/sizeof(WCHAR), &len, 0);
2224         if(SUCCEEDED(hres))
2225             nsIWineURI_SetWineURL(wine_uri, url);
2226         else
2227              WARN("CoCombineUrl failed: %08x\n", hres);
2228     }else if(is_wine_uri) {
2229         WCHAR url[INTERNET_MAX_URL_LENGTH];
2230
2231         MultiByteToWideChar(CP_ACP, 0, spec, -1, url, sizeof(url)/sizeof(WCHAR));
2232         nsIWineURI_SetWineURL(wine_uri, url);
2233     }
2234
2235     if(base_wine_uri)
2236         nsIWineURI_Release(base_wine_uri);
2237
2238     return nsres;
2239 }
2240
2241 static nsresult NSAPI nsIOService_NewFileURI(nsIIOService *iface, nsIFile *aFile,
2242                                              nsIURI **_retval)
2243 {
2244     TRACE("(%p %p)\n", aFile, _retval);
2245     return nsIIOService_NewFileURI(nsio, aFile, _retval);
2246 }
2247
2248 static nsresult NSAPI nsIOService_NewChannelFromURI(nsIIOService *iface, nsIURI *aURI,
2249                                                      nsIChannel **_retval)
2250 {
2251     nsIChannel *channel = NULL;
2252     nsChannel *ret;
2253     nsIWineURI *wine_uri;
2254     nsresult nsres;
2255
2256     TRACE("(%p %p)\n", aURI, _retval);
2257
2258     nsres = nsIIOService_NewChannelFromURI(nsio, aURI, &channel);
2259     if(NS_FAILED(nsres) && nsres != NS_ERROR_UNKNOWN_PROTOCOL) {
2260         WARN("NewChannelFromURI failed: %08x\n", nsres);
2261         *_retval = channel;
2262         return nsres;
2263     }
2264
2265     nsres = nsIURI_QueryInterface(aURI, &IID_nsIWineURI, (void**)&wine_uri);
2266     if(NS_FAILED(nsres)) {
2267         WARN("Could not get nsIWineURI: %08x\n", nsres);
2268         *_retval = channel;
2269         return channel ? NS_OK : NS_ERROR_UNEXPECTED;
2270     }
2271
2272     ret = heap_alloc_zero(sizeof(nsChannel));
2273
2274     ret->lpHttpChannelVtbl = &nsChannelVtbl;
2275     ret->lpUploadChannelVtbl = &nsUploadChannelVtbl;
2276     ret->lpIHttpChannelInternalVtbl = &nsHttpChannelInternalVtbl;
2277     ret->ref = 1;
2278     ret->channel = channel;
2279     ret->uri = wine_uri;
2280
2281     nsIURI_AddRef(aURI);
2282     ret->original_uri = aURI;
2283
2284     if(channel) {
2285         nsIChannel_QueryInterface(channel, &IID_nsIHttpChannel, (void**)&ret->http_channel);
2286         nsIChannel_QueryInterface(channel, &IID_nsIHttpChannelInternal, (void**)&ret->http_channel_internal);
2287     }
2288
2289     *_retval = NSCHANNEL(ret);
2290     return NS_OK;
2291 }
2292
2293 static nsresult NSAPI nsIOService_NewChannel(nsIIOService *iface, const nsACString *aSpec,
2294         const char *aOriginCharset, nsIURI *aBaseURI, nsIChannel **_retval)
2295 {
2296     TRACE("(%p %s %p %p)\n", aSpec, debugstr_a(aOriginCharset), aBaseURI, _retval);
2297     return nsIIOService_NewChannel(nsio, aSpec, aOriginCharset, aBaseURI, _retval);
2298 }
2299
2300 static nsresult NSAPI nsIOService_GetOffline(nsIIOService *iface, PRBool *aOffline)
2301 {
2302     TRACE("(%p)\n", aOffline);
2303     return nsIIOService_GetOffline(nsio, aOffline);
2304 }
2305
2306 static nsresult NSAPI nsIOService_SetOffline(nsIIOService *iface, PRBool aOffline)
2307 {
2308     TRACE("(%x)\n", aOffline);
2309     return nsIIOService_SetOffline(nsio, aOffline);
2310 }
2311
2312 static nsresult NSAPI nsIOService_AllowPort(nsIIOService *iface, PRInt32 aPort,
2313                                              const char *aScheme, PRBool *_retval)
2314 {
2315     TRACE("(%d %s %p)\n", aPort, debugstr_a(aScheme), _retval);
2316     return nsIIOService_AllowPort(nsio, aPort, debugstr_a(aScheme), _retval);
2317 }
2318
2319 static nsresult NSAPI nsIOService_ExtractScheme(nsIIOService *iface, const nsACString *urlString,
2320                                                  nsACString * _retval)
2321 {
2322     TRACE("(%p %p)\n", urlString, _retval);
2323     return nsIIOService_ExtractScheme(nsio, urlString, _retval);
2324 }
2325
2326 static const nsIIOServiceVtbl nsIOServiceVtbl = {
2327     nsIOService_QueryInterface,
2328     nsIOService_AddRef,
2329     nsIOService_Release,
2330     nsIOService_GetProtocolHandler,
2331     nsIOService_GetProtocolFlags,
2332     nsIOService_NewURI,
2333     nsIOService_NewFileURI,
2334     nsIOService_NewChannelFromURI,
2335     nsIOService_NewChannel,
2336     nsIOService_GetOffline,
2337     nsIOService_SetOffline,
2338     nsIOService_AllowPort,
2339     nsIOService_ExtractScheme
2340 };
2341
2342 static nsIIOService nsIOService = { &nsIOServiceVtbl };
2343
2344 static nsresult NSAPI nsNetUtil_QueryInterface(nsINetUtil *iface, nsIIDRef riid,
2345                                                nsQIResult result)
2346 {
2347     return nsIIOService_QueryInterface(&nsIOService, riid, result);
2348 }
2349
2350 static nsrefcnt NSAPI nsNetUtil_AddRef(nsINetUtil *iface)
2351 {
2352     return 2;
2353 }
2354
2355 static nsrefcnt NSAPI nsNetUtil_Release(nsINetUtil *iface)
2356 {
2357     return 1;
2358 }
2359
2360 static nsresult NSAPI nsNetUtil_ParseContentType(nsINetUtil *iface, const nsACString *aTypeHeader,
2361         nsACString *aCharset, PRBool *aHadCharset, nsACString *aContentType)
2362 {
2363     TRACE("(%p %p %p %p)\n", aTypeHeader, aCharset, aHadCharset, aContentType);
2364
2365     return nsINetUtil_ParseContentType(net_util, aTypeHeader, aCharset, aHadCharset, aContentType);
2366 }
2367
2368 static nsresult NSAPI nsNetUtil_ProtocolHasFlags(nsINetUtil *iface, nsIURI *aURI, PRUint32 aFlags, PRBool *_retval)
2369 {
2370     TRACE("()\n");
2371
2372     return nsINetUtil_ProtocolHasFlags(net_util, aURI, aFlags, _retval);
2373 }
2374
2375 static nsresult NSAPI nsNetUtil_URIChainHasFlags(nsINetUtil *iface, nsIURI *aURI, PRUint32 aFlags, PRBool *_retval)
2376 {
2377     TRACE("(%p %08x %p)\n", aURI, aFlags, _retval);
2378
2379     if(aFlags == (1<<11)) {
2380         *_retval = FALSE;
2381         return NS_OK;
2382     }
2383
2384     return nsINetUtil_URIChainHasFlags(net_util, aURI, aFlags, _retval);
2385 }
2386
2387 static nsresult NSAPI nsNetUtil_ToImmutableURI(nsINetUtil *iface, nsIURI *aURI, nsIURI **_retval)
2388 {
2389     TRACE("(%p %p)\n", aURI, _retval);
2390
2391     return nsINetUtil_ToImmutableURI(net_util, aURI, _retval);
2392 }
2393
2394 static nsresult NSAPI nsNetUtil_EscapeString(nsINetUtil *iface, const nsACString *aString,
2395                                              PRUint32 aEscapeType, nsACString *_retval)
2396 {
2397     TRACE("(%p %x %p)\n", aString, aEscapeType, _retval);
2398
2399     return nsINetUtil_EscapeString(net_util, aString, aEscapeType, _retval);
2400 }
2401
2402 static nsresult NSAPI nsNetUtil_EscapeURL(nsINetUtil *iface, const nsACString *aStr, PRUint32 aFlags,
2403                                           nsACString *_retval)
2404 {
2405     TRACE("(%p %08x %p)\n", aStr, aFlags, _retval);
2406
2407     return nsINetUtil_EscapeURL(net_util, aStr, aFlags, _retval);
2408 }
2409
2410 static nsresult NSAPI nsNetUtil_UnescapeString(nsINetUtil *iface, const nsACString *aStr,
2411                                                PRUint32 aFlags, nsACString *_retval)
2412 {
2413     TRACE("(%p %08x %p)\n", aStr, aFlags, _retval);
2414
2415     return nsINetUtil_UnescapeString(net_util, aStr, aFlags, _retval);
2416 }
2417
2418 static nsresult NSAPI nsNetUtil_ExtractCharsetFromContentType(nsINetUtil *iface, const nsACString *aTypeHeader,
2419         nsACString *aCharset, PRInt32 *aCharsetStart, PRInt32 *aCharsetEnd, PRBool *_retval)
2420 {
2421     TRACE("(%p %p %p %p %p)\n", aTypeHeader, aCharset, aCharsetStart, aCharsetEnd, _retval);
2422
2423     return nsINetUtil_ExtractCharsetFromContentType(net_util, aTypeHeader, aCharset, aCharsetStart, aCharsetEnd, _retval);
2424 }
2425
2426 static const nsINetUtilVtbl nsNetUtilVtbl = {
2427     nsNetUtil_QueryInterface,
2428     nsNetUtil_AddRef,
2429     nsNetUtil_Release,
2430     nsNetUtil_ParseContentType,
2431     nsNetUtil_ProtocolHasFlags,
2432     nsNetUtil_URIChainHasFlags,
2433     nsNetUtil_ToImmutableURI,
2434     nsNetUtil_EscapeString,
2435     nsNetUtil_EscapeURL,
2436     nsNetUtil_UnescapeString,
2437     nsNetUtil_ExtractCharsetFromContentType
2438 };
2439
2440 static nsINetUtil nsNetUtil = { &nsNetUtilVtbl };
2441
2442 static nsresult NSAPI nsIOService_QueryInterface(nsIIOService *iface, nsIIDRef riid,
2443                                                  nsQIResult result)
2444 {
2445     *result = NULL;
2446
2447     if(IsEqualGUID(&IID_nsISupports, riid))
2448         *result = &nsIOService;
2449     else if(IsEqualGUID(&IID_nsIIOService, riid))
2450         *result = &nsIOService;
2451     else if(IsEqualGUID(&IID_nsINetUtil, riid))
2452         *result = &nsNetUtil;
2453
2454     if(*result) {
2455         nsISupports_AddRef((nsISupports*)*result);
2456         return NS_OK;
2457     }
2458
2459     FIXME("(%s %p)\n", debugstr_guid(riid), result);
2460     return NS_NOINTERFACE;
2461 }
2462
2463 static nsresult NSAPI nsIOServiceFactory_QueryInterface(nsIFactory *iface, nsIIDRef riid,
2464                                                         nsQIResult result)
2465 {
2466     *result = NULL;
2467
2468     if(IsEqualGUID(&IID_nsISupports, riid)) {
2469         TRACE("(IID_nsISupports %p)\n", result);
2470         *result = iface;
2471     }else if(IsEqualGUID(&IID_nsIFactory, riid)) {
2472         TRACE("(IID_nsIFactory %p)\n", result);
2473         *result = iface;
2474     }
2475
2476     if(*result) {
2477         nsIFactory_AddRef(iface);
2478         return NS_OK;
2479     }
2480
2481     WARN("(%s %p)\n", debugstr_guid(riid), result);
2482     return NS_NOINTERFACE;
2483 }
2484
2485 static nsrefcnt NSAPI nsIOServiceFactory_AddRef(nsIFactory *iface)
2486 {
2487     return 2;
2488 }
2489
2490 static nsrefcnt NSAPI nsIOServiceFactory_Release(nsIFactory *iface)
2491 {
2492     return 1;
2493 }
2494
2495 static nsresult NSAPI nsIOServiceFactory_CreateInstance(nsIFactory *iface,
2496         nsISupports *aOuter, const nsIID *iid, void **result)
2497 {
2498     return nsIIOService_QueryInterface(&nsIOService, iid, result);
2499 }
2500
2501 static nsresult NSAPI nsIOServiceFactory_LockFactory(nsIFactory *iface, PRBool lock)
2502 {
2503     WARN("(%x)\n", lock);
2504     return NS_OK;
2505 }
2506
2507 static const nsIFactoryVtbl nsIOServiceFactoryVtbl = {
2508     nsIOServiceFactory_QueryInterface,
2509     nsIOServiceFactory_AddRef,
2510     nsIOServiceFactory_Release,
2511     nsIOServiceFactory_CreateInstance,
2512     nsIOServiceFactory_LockFactory
2513 };
2514
2515 static nsIFactory nsIOServiceFactory = { &nsIOServiceFactoryVtbl };
2516
2517 void init_nsio(nsIComponentManager *component_manager, nsIComponentRegistrar *registrar)
2518 {
2519     nsIFactory *old_factory = NULL;
2520     nsresult nsres;
2521
2522     nsres = nsIComponentManager_GetClassObject(component_manager, &NS_IOSERVICE_CID,
2523                                                &IID_nsIFactory, (void**)&old_factory);
2524     if(NS_FAILED(nsres)) {
2525         ERR("Could not get factory: %08x\n", nsres);
2526         return;
2527     }
2528
2529     nsres = nsIFactory_CreateInstance(old_factory, NULL, &IID_nsIIOService, (void**)&nsio);
2530     if(NS_FAILED(nsres)) {
2531         ERR("Couldn not create nsIOService instance %08x\n", nsres);
2532         nsIFactory_Release(old_factory);
2533         return;
2534     }
2535
2536     nsres = nsIIOService_QueryInterface(nsio, &IID_nsINetUtil, (void**)&net_util);
2537     if(NS_FAILED(nsres)) {
2538         WARN("Could not get nsINetUtil interface: %08x\n", nsres);
2539         nsIIOService_Release(nsio);
2540         return;
2541     }
2542
2543     nsres = nsIComponentRegistrar_UnregisterFactory(registrar, &NS_IOSERVICE_CID, old_factory);
2544     nsIFactory_Release(old_factory);
2545     if(NS_FAILED(nsres))
2546         ERR("UnregisterFactory failed: %08x\n", nsres);
2547
2548     nsres = nsIComponentRegistrar_RegisterFactory(registrar, &NS_IOSERVICE_CID,
2549             NS_IOSERVICE_CLASSNAME, NS_IOSERVICE_CONTRACTID, &nsIOServiceFactory);
2550     if(NS_FAILED(nsres))
2551         ERR("RegisterFactory failed: %08x\n", nsres);
2552 }
2553
2554 void release_nsio(void)
2555 {
2556     if(net_util) {
2557         nsINetUtil_Release(net_util);
2558         net_util = NULL;
2559     }
2560
2561     if(nsio) {
2562         nsIIOService_Release(nsio);
2563         nsio = NULL;
2564     }
2565 }