winsock: Implement getnameinfo.
[wine] / dlls / mshtml / nsio.c
1 /*
2  * Copyright 2006 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 "ole2.h"
29 #include "shlguid.h"
30
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
33
34 #include "mshtml_private.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37
38 #define LOAD_INITIAL_DOCUMENT_URI 0x80000
39
40 #define NS_IOSERVICE_CLASSNAME "nsIOService"
41 #define NS_IOSERVICE_CONTRACTID "@mozilla.org/network/io-service;1"
42
43 static IID NS_IOSERVICE_CID =
44     {0x9ac9e770, 0x18bc, 0x11d3, {0x93, 0x37, 0x00, 0x10, 0x4b, 0xa0, 0xfd, 0x40}};
45
46 static nsIIOService *nsio = NULL;
47
48 typedef struct {
49     const nsIHttpChannelVtbl *lpHttpChannelVtbl;
50     const nsIUploadChannelVtbl *lpUploadChannelVtbl;
51
52     LONG ref;
53
54     nsIChannel *channel;
55     nsIHttpChannel *http_channel;
56     nsIWineURI *uri;
57     nsIInputStream *post_data_stream;
58     nsILoadGroup *load_group;
59     nsIInterfaceRequestor *notif_callback;
60     nsLoadFlags load_flags;
61     nsIURI *original_uri;
62 } nsChannel;
63
64 typedef struct {
65     const nsIWineURIVtbl *lpWineURIVtbl;
66
67     LONG ref;
68
69     nsIURI *uri;
70     NSContainer *container;
71 } nsURI;
72
73 #define NSCHANNEL(x)     ((nsIChannel*)        &(x)->lpHttpChannelVtbl)
74 #define NSHTTPCHANNEL(x) ((nsIHttpChannel*)    &(x)->lpHttpChannelVtbl)
75 #define NSUPCHANNEL(x)   ((nsIUploadChannel*)  &(x)->lpUploadChannelVtbl)
76 #define NSURI(x)         ((nsIURI*)            &(x)->lpWineURIVtbl)
77
78 static BOOL exec_shldocvw_67(NSContainer *container, LPCWSTR url)
79 {
80     IOleCommandTarget *cmdtrg = NULL;
81     HRESULT hres;
82
83     hres = IOleClientSite_QueryInterface(container->doc->client, &IID_IOleCommandTarget,
84                                          (void**)&cmdtrg);
85     if(SUCCEEDED(hres)) {
86         VARIANT varUrl, varRes;
87
88         V_VT(&varUrl) = VT_BSTR;
89         V_BSTR(&varUrl) = SysAllocString(url);
90         V_VT(&varRes) = VT_BOOL;
91
92         hres = IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 67, 0, &varUrl, &varRes);
93
94         IOleCommandTarget_Release(cmdtrg);
95         SysFreeString(V_BSTR(&varUrl));
96
97         if(SUCCEEDED(hres) && !V_BOOL(&varRes)) {
98             TRACE("got VARIANT_FALSE, do not load\n");
99             return FALSE;
100         }
101     }
102
103     return TRUE;
104 }
105
106 static BOOL handle_uri(NSContainer *container, nsChannel *channel, LPCWSTR uri)
107 {
108     IServiceProvider *service_provider;
109     HRESULT hres;
110
111     if(!exec_shldocvw_67(container, uri))
112         return FALSE;
113
114     hres = IOleClientSite_QueryInterface(container->doc->client, &IID_IServiceProvider,
115                                          (void**)&service_provider);
116     if(SUCCEEDED(hres)) {
117         IHlinkFrame *hlink_frame;
118
119         hres = IServiceProvider_QueryService(service_provider, &IID_IHlinkFrame,
120                                              &IID_IHlinkFrame, (void**)&hlink_frame);
121         if(SUCCEEDED(hres)) {
122             hlink_frame_navigate(container, hlink_frame, uri, channel->post_data_stream);
123             IHlinkFrame_Release(hlink_frame);
124
125             return FALSE;
126         }
127
128         IServiceProvider_Release(service_provider);
129     }
130
131     return TRUE;
132 }
133
134 static BOOL before_async_open(nsChannel *This)
135 {
136     nsACString *uri_str;
137     NSContainer *container;
138     const char *uria;
139     LPWSTR uri;
140     DWORD len;
141     BOOL ret;
142
143     if(!(This->load_flags & LOAD_INITIAL_DOCUMENT_URI))
144         return TRUE;
145
146     nsIWineURI_GetNSContainer(This->uri, &container);
147     if(!container) {
148         WARN("container = NULL\n");
149         return TRUE;
150     }
151
152     if(container->load_call) {
153         nsIWebBrowserChrome_Release(NSWBCHROME(container));
154         return TRUE;
155     }
156
157     uri_str = nsACString_Create();
158     nsIWineURI_GetSpec(This->uri, uri_str);
159     nsACString_GetData(uri_str, &uria, NULL);
160     len = MultiByteToWideChar(CP_ACP, 0, uria, -1, NULL, 0);
161     uri = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
162     MultiByteToWideChar(CP_ACP, 0, uria, -1, uri, len);
163     nsACString_Destroy(uri_str);
164
165     ret = handle_uri(container, This, uri);
166
167     nsIWebBrowserChrome_Release(NSWBCHROME(container));
168     HeapFree(GetProcessHeap(), 0, uri);
169
170     return ret;
171 }
172
173 #define NSCHANNEL_THIS(iface) DEFINE_THIS(nsChannel, HttpChannel, iface)
174
175 static nsresult NSAPI nsChannel_QueryInterface(nsIHttpChannel *iface, nsIIDRef riid, nsQIResult result)
176 {
177     nsChannel *This = NSCHANNEL_THIS(iface);
178
179     *result = NULL;
180
181     if(IsEqualGUID(&IID_nsISupports, riid)) {
182         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
183         *result = NSCHANNEL(This);
184     }else if(IsEqualGUID(&IID_nsIRequest, riid)) {
185         TRACE("(%p)->(IID_nsIRequest %p)\n", This, result);
186         *result = NSCHANNEL(This);
187     }else if(IsEqualGUID(&IID_nsIChannel, riid)) {
188         TRACE("(%p)->(IID_nsIChannel %p)\n", This, result);
189         *result = NSCHANNEL(This);
190     }else if(This->http_channel && IsEqualGUID(&IID_nsIHttpChannel, riid)) {
191         TRACE("(%p)->(IID_nsIHttpChannel %p)\n", This, result);
192         *result = NSHTTPCHANNEL(This);
193     }else if(IsEqualGUID(&IID_nsIUploadChannel, riid)) {
194         TRACE("(%p)->(IID_nsIUploadChannel %p)\n", This, result);
195         *result = NSUPCHANNEL(This);
196     }
197
198     if(*result) {
199         nsIChannel_AddRef(NSCHANNEL(This));
200         return NS_OK;
201     }
202
203     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
204
205     if(This->channel)
206         return nsIChannel_QueryInterface(This->channel, riid, result);
207     return NS_NOINTERFACE;
208 }
209
210 static nsrefcnt NSAPI nsChannel_AddRef(nsIHttpChannel *iface)
211 {
212     nsChannel *This = NSCHANNEL_THIS(iface);
213     nsrefcnt ref = InterlockedIncrement(&This->ref);
214
215     TRACE("(%p) ref=%ld\n", This, ref);
216
217     return ref;
218 }
219
220 static nsrefcnt NSAPI nsChannel_Release(nsIHttpChannel *iface)
221 {
222     nsChannel *This = NSCHANNEL_THIS(iface);
223     LONG ref = InterlockedDecrement(&This->ref);
224
225     if(!ref) {
226         nsIWineURI_Release(This->uri);
227         if(This->channel)
228             nsIChannel_Release(This->channel);
229         if(This->http_channel)
230             nsIHttpChannel_Release(This->http_channel);
231         if(This->post_data_stream)
232             nsIInputStream_Release(This->post_data_stream);
233         if(This->load_group)
234             nsILoadGroup_Release(This->load_group);
235         if(This->notif_callback)
236             nsIInterfaceRequestor_Release(This->notif_callback);
237         if(This->original_uri)
238             nsIURI_Release(This->original_uri);
239         HeapFree(GetProcessHeap(), 0, This);
240     }
241
242     return ref;
243 }
244
245 static nsresult NSAPI nsChannel_GetName(nsIHttpChannel *iface, nsACString *aName)
246 {
247     nsChannel *This = NSCHANNEL_THIS(iface);
248
249     TRACE("(%p)->(%p)\n", This, aName);
250
251     if(This->channel)
252         return nsIChannel_GetName(This->channel, aName);
253
254     FIXME("default action not implemented\n");
255     return NS_ERROR_NOT_IMPLEMENTED;
256 }
257
258 static nsresult NSAPI nsChannel_IsPending(nsIHttpChannel *iface, PRBool *_retval)
259 {
260     nsChannel *This = NSCHANNEL_THIS(iface);
261
262     TRACE("(%p)->(%p)\n", This, _retval);
263
264     if(This->channel)
265         return nsIChannel_IsPending(This->channel, _retval);
266
267     FIXME("default action not implemented\n");
268     return NS_ERROR_NOT_IMPLEMENTED;
269 }
270
271 static nsresult NSAPI nsChannel_GetStatus(nsIHttpChannel *iface, nsresult *aStatus)
272 {
273     nsChannel *This = NSCHANNEL_THIS(iface);
274
275     TRACE("(%p)->(%p)\n", This, aStatus);
276
277     if(This->channel)
278         return nsIChannel_GetStatus(This->channel, aStatus);
279
280     FIXME("default action not implemented\n");
281     return NS_ERROR_NOT_IMPLEMENTED;
282 }
283
284 static nsresult NSAPI nsChannel_Cancel(nsIHttpChannel *iface, nsresult aStatus)
285 {
286     nsChannel *This = NSCHANNEL_THIS(iface);
287
288     TRACE("(%p)->(%08lx)\n", This, aStatus);
289
290     if(This->channel)
291         return nsIChannel_Cancel(This->channel, aStatus);
292
293     FIXME("default action not implemented\n");
294     return NS_ERROR_NOT_IMPLEMENTED;
295 }
296
297 static nsresult NSAPI nsChannel_Suspend(nsIHttpChannel *iface)
298 {
299     nsChannel *This = NSCHANNEL_THIS(iface);
300
301     TRACE("(%p)\n", This);
302
303     if(This->channel)
304         return nsIChannel_Suspend(This->channel);
305
306     FIXME("default action not implemented\n");
307     return NS_ERROR_NOT_IMPLEMENTED;
308 }
309
310 static nsresult NSAPI nsChannel_Resume(nsIHttpChannel *iface)
311 {
312     nsChannel *This = NSCHANNEL_THIS(iface);
313
314     TRACE("(%p)\n", This);
315
316     if(This->channel)
317         return nsIChannel_Resume(This->channel);
318
319     FIXME("default action not implemented\n");
320     return NS_ERROR_NOT_IMPLEMENTED;
321 }
322
323 static nsresult NSAPI nsChannel_GetLoadGroup(nsIHttpChannel *iface, nsILoadGroup **aLoadGroup)
324 {
325     nsChannel *This = NSCHANNEL_THIS(iface);
326
327     TRACE("(%p)->(%p)\n", This, aLoadGroup);
328
329     if(This->load_group)
330         nsILoadGroup_AddRef(This->load_group);
331
332     *aLoadGroup = This->load_group;
333     return NS_OK;
334 }
335
336 static nsresult NSAPI nsChannel_SetLoadGroup(nsIHttpChannel *iface, nsILoadGroup *aLoadGroup)
337 {
338     nsChannel *This = NSCHANNEL_THIS(iface);
339
340     TRACE("(%p)->(%p)\n", This, aLoadGroup);
341
342     if(This->load_group)
343         nsILoadGroup_Release(This->load_group);
344     if(aLoadGroup)
345         nsILoadGroup_AddRef(aLoadGroup);
346
347     This->load_group = aLoadGroup;
348
349     if(This->channel)
350         return nsIChannel_SetLoadGroup(This->channel, aLoadGroup);
351     return NS_OK;
352 }
353
354 static nsresult NSAPI nsChannel_GetLoadFlags(nsIHttpChannel *iface, nsLoadFlags *aLoadFlags)
355 {
356     nsChannel *This = NSCHANNEL_THIS(iface);
357
358     TRACE("(%p)->(%p)\n", This, aLoadFlags);
359
360     *aLoadFlags = This->load_flags;
361     return NS_OK;
362 }
363
364 static nsresult NSAPI nsChannel_SetLoadFlags(nsIHttpChannel *iface, nsLoadFlags aLoadFlags)
365 {
366     nsChannel *This = NSCHANNEL_THIS(iface);
367
368     TRACE("(%p)->(%08lx)\n", This, aLoadFlags);
369
370     This->load_flags = aLoadFlags;
371
372     if(This->channel)
373         return nsIChannel_SetLoadFlags(This->channel, aLoadFlags);
374     return NS_OK;
375 }
376
377 static nsresult NSAPI nsChannel_GetOriginalURI(nsIHttpChannel *iface, nsIURI **aOriginalURI)
378 {
379     nsChannel *This = NSCHANNEL_THIS(iface);
380
381     TRACE("(%p)->(%p)\n", This, aOriginalURI);
382
383     if(This->original_uri)
384         nsIURI_AddRef(This->original_uri);
385
386     *aOriginalURI = This->original_uri;
387     return NS_OK;
388 }
389
390 static nsresult NSAPI nsChannel_SetOriginalURI(nsIHttpChannel *iface, nsIURI *aOriginalURI)
391 {
392     nsChannel *This = NSCHANNEL_THIS(iface);
393
394     TRACE("(%p)->(%p)\n", This, aOriginalURI);
395
396     if(This->original_uri)
397         nsIURI_Release(This->original_uri);
398
399     nsIURI_AddRef(aOriginalURI);
400     This->original_uri = aOriginalURI;
401
402     if(This->channel)
403         return nsIChannel_SetOriginalURI(This->channel, aOriginalURI);
404     return NS_OK;
405 }
406
407 static nsresult NSAPI nsChannel_GetURI(nsIHttpChannel *iface, nsIURI **aURI)
408 {
409     nsChannel *This = NSCHANNEL_THIS(iface);
410
411     TRACE("(%p)->(%p)\n", This, aURI);
412
413     nsIWineURI_AddRef(This->uri);
414     *aURI = (nsIURI*)This->uri;
415
416     return NS_OK;
417 }
418
419 static nsresult NSAPI nsChannel_GetOwner(nsIHttpChannel *iface, nsISupports **aOwner)
420 {
421     nsChannel *This = NSCHANNEL_THIS(iface);
422
423     TRACE("(%p)->(%p)\n", This, aOwner);
424
425     if(This->channel)
426         return nsIChannel_GetOwner(This->channel, aOwner);
427
428     FIXME("default action not implemented\n");
429     return NS_ERROR_NOT_IMPLEMENTED;
430 }
431
432 static nsresult NSAPI nsChannel_SetOwner(nsIHttpChannel *iface, nsISupports *aOwner)
433 {
434     nsChannel *This = NSCHANNEL_THIS(iface);
435
436     TRACE("(%p)->(%p)\n", This, aOwner);
437
438     if(This->channel)
439         return nsIChannel_SetOwner(This->channel, aOwner);
440
441     FIXME("default action not implemented\n");
442     return NS_ERROR_NOT_IMPLEMENTED;
443 }
444
445 static nsresult NSAPI nsChannel_GetNotificationCallbacks(nsIHttpChannel *iface,
446         nsIInterfaceRequestor **aNotificationCallbacks)
447 {
448     nsChannel *This = NSCHANNEL_THIS(iface);
449
450     TRACE("(%p)->(%p)\n", This, aNotificationCallbacks);
451
452     if(This->notif_callback)
453         nsIInterfaceRequestor_AddRef(This->notif_callback);
454     *aNotificationCallbacks = This->notif_callback;
455
456     return NS_OK;
457 }
458
459 static nsresult NSAPI nsChannel_SetNotificationCallbacks(nsIHttpChannel *iface,
460         nsIInterfaceRequestor *aNotificationCallbacks)
461 {
462     nsChannel *This = NSCHANNEL_THIS(iface);
463
464     TRACE("(%p)->(%p)\n", This, aNotificationCallbacks);
465
466     if(This->notif_callback)
467         nsIInterfaceRequestor_Release(This->notif_callback);
468     if(aNotificationCallbacks)
469         nsIInterfaceRequestor_AddRef(aNotificationCallbacks);
470
471     This->notif_callback = aNotificationCallbacks;
472
473     if(This->channel)
474         return nsIChannel_SetNotificationCallbacks(This->channel, aNotificationCallbacks);
475     return NS_OK;
476 }
477
478 static nsresult NSAPI nsChannel_GetSecurityInfo(nsIHttpChannel *iface, nsISupports **aSecurityInfo)
479 {
480     nsChannel *This = NSCHANNEL_THIS(iface);
481     TRACE("(%p)->(%p)\n", This, aSecurityInfo);
482     return nsIChannel_GetSecurityInfo(This->channel, aSecurityInfo);
483 }
484
485 static nsresult NSAPI nsChannel_GetContentType(nsIHttpChannel *iface, nsACString *aContentType)
486 {
487     nsChannel *This = NSCHANNEL_THIS(iface);
488
489     TRACE("(%p)->(%p)\n", This, aContentType);
490
491     if(This->channel)
492         return nsIChannel_GetContentType(This->channel, aContentType);
493
494     FIXME("default action not implemented\n");
495     return NS_ERROR_NOT_IMPLEMENTED;
496 }
497
498 static nsresult NSAPI nsChannel_SetContentType(nsIHttpChannel *iface,
499                                                const nsACString *aContentType)
500 {
501     nsChannel *This = NSCHANNEL_THIS(iface);
502
503     TRACE("(%p)->(%p)\n", This, aContentType);
504
505     if(This->channel)
506         return nsIChannel_SetContentType(This->channel, aContentType);
507
508     FIXME("default action not implemented\n");
509     return NS_ERROR_NOT_IMPLEMENTED;
510 }
511
512 static nsresult NSAPI nsChannel_GetContentCharset(nsIHttpChannel *iface,
513                                                   nsACString *aContentCharset)
514 {
515     nsChannel *This = NSCHANNEL_THIS(iface);
516
517     TRACE("(%p)->(%p)\n", This, aContentCharset);
518
519     if(This->channel)
520         return nsIChannel_GetContentCharset(This->channel, aContentCharset);
521
522     FIXME("default action not implemented\n");
523     return NS_ERROR_NOT_IMPLEMENTED;
524 }
525
526 static nsresult NSAPI nsChannel_SetContentCharset(nsIHttpChannel *iface,
527                                                   const nsACString *aContentCharset)
528 {
529     nsChannel *This = NSCHANNEL_THIS(iface);
530
531     TRACE("(%p)->(%p)\n", This, aContentCharset);
532
533     if(This->channel)
534         return nsIChannel_SetContentCharset(This->channel, aContentCharset);
535
536     FIXME("default action not implemented\n");
537     return NS_ERROR_NOT_IMPLEMENTED;
538 }
539
540 static nsresult NSAPI nsChannel_GetContentLength(nsIHttpChannel *iface, PRInt32 *aContentLength)
541 {
542     nsChannel *This = NSCHANNEL_THIS(iface);
543
544     TRACE("(%p)->(%p)\n", This, aContentLength);
545
546     if(This->channel)
547         return nsIChannel_GetContentLength(This->channel, aContentLength);
548
549     FIXME("default action not implemented\n");
550     return NS_ERROR_NOT_IMPLEMENTED;
551 }
552
553 static nsresult NSAPI nsChannel_SetContentLength(nsIHttpChannel *iface, PRInt32 aContentLength)
554 {
555     nsChannel *This = NSCHANNEL_THIS(iface);
556
557     TRACE("(%p)->(%ld)\n", This, aContentLength);
558
559     if(This->channel)
560         return nsIChannel_SetContentLength(This->channel, aContentLength);
561
562     FIXME("default action not implemented\n");
563     return NS_ERROR_NOT_IMPLEMENTED;
564 }
565
566 static nsresult NSAPI nsChannel_Open(nsIHttpChannel *iface, nsIInputStream **_retval)
567 {
568     nsChannel *This = NSCHANNEL_THIS(iface);
569
570     TRACE("(%p)->(%p)\n", This, _retval);
571
572     if(This->channel)
573         return nsIChannel_Open(This->channel, _retval);
574
575     FIXME("default action not implemented\n");
576     return NS_ERROR_NOT_IMPLEMENTED;
577 }
578
579 static nsresult NSAPI nsChannel_AsyncOpen(nsIHttpChannel *iface, nsIStreamListener *aListener,
580                                           nsISupports *aContext)
581 {
582     nsChannel *This = NSCHANNEL_THIS(iface);
583     nsresult nsres;
584
585     TRACE("(%p)->(%p %p)\n", This, aListener, aContext);
586
587     if(!before_async_open(This)) {
588         TRACE("canceled\n");
589         return NS_ERROR_UNEXPECTED;
590     }
591
592     if(!This->channel) {
593         FIXME("channel == NULL\n");
594         return NS_ERROR_UNEXPECTED;
595     }
596
597     if(This->post_data_stream) {
598         nsIUploadChannel *upload_channel;
599
600         nsres = nsIChannel_QueryInterface(This->channel, &IID_nsIUploadChannel,
601                                           (void**)&upload_channel);
602         if(NS_SUCCEEDED(nsres)) {
603             nsACString *empty_string = nsACString_Create();
604             nsACString_SetData(empty_string, "");
605
606             nsres = nsIUploadChannel_SetUploadStream(upload_channel, This->post_data_stream,
607                                                      empty_string, -1);
608             if(NS_FAILED(nsres))
609                 WARN("SetUploadStream failed: %08lx\n", nsres);
610         }
611     }
612
613     return nsIChannel_AsyncOpen(This->channel, aListener, aContext);
614 }
615
616 static nsresult NSAPI nsChannel_GetRequestMethod(nsIHttpChannel *iface, nsACString *aRequestMethod)
617 {
618     nsChannel *This = NSCHANNEL_THIS(iface);
619
620     TRACE("(%p)->(%p)\n", This, aRequestMethod);
621
622     if(This->http_channel)
623         return nsIHttpChannel_GetRequestMethod(This->http_channel, aRequestMethod);
624
625     return NS_ERROR_NOT_IMPLEMENTED;
626 }
627
628 static nsresult NSAPI nsChannel_SetRequestMethod(nsIHttpChannel *iface,
629                                                  const nsACString *aRequestMethod)
630 {
631     nsChannel *This = NSCHANNEL_THIS(iface);
632
633     TRACE("(%p)->(%p)\n", This, aRequestMethod);
634
635     if(This->http_channel)
636         return nsIHttpChannel_SetRequestMethod(This->http_channel, aRequestMethod);
637
638     return NS_ERROR_NOT_IMPLEMENTED;
639 }
640
641 static nsresult NSAPI nsChannel_GetReferrer(nsIHttpChannel *iface, nsIURI **aReferrer)
642 {
643     nsChannel *This = NSCHANNEL_THIS(iface);
644
645     TRACE("(%p)->(%p)\n", This, aReferrer);
646
647     if(This->http_channel)
648         return nsIHttpChannel_GetReferrer(This->http_channel, aReferrer);
649
650     return NS_ERROR_NOT_IMPLEMENTED;
651 }
652
653 static nsresult NSAPI nsChannel_SetReferrer(nsIHttpChannel *iface, nsIURI *aReferrer)
654 {
655     nsChannel *This = NSCHANNEL_THIS(iface);
656
657     TRACE("(%p)->(%p)\n", This, aReferrer);
658
659     if(This->http_channel)
660         return nsIHttpChannel_SetReferrer(This->http_channel, aReferrer);
661
662     return NS_ERROR_NOT_IMPLEMENTED;
663 }
664
665 static nsresult NSAPI nsChannel_GetRequestHeader(nsIHttpChannel *iface,
666          const nsACString *aHeader, nsACString *_retval)
667 {
668     nsChannel *This = NSCHANNEL_THIS(iface);
669
670     TRACE("(%p)->(%p %p)\n", This, aHeader, _retval);
671
672     if(This->http_channel)
673         return nsIHttpChannel_GetRequestHeader(This->http_channel, aHeader, _retval);
674
675     return NS_ERROR_NOT_IMPLEMENTED;
676 }
677
678 static nsresult NSAPI nsChannel_SetRequestHeader(nsIHttpChannel *iface,
679          const nsACString *aHeader, const nsACString *aValue, PRBool aMerge)
680 {
681     nsChannel *This = NSCHANNEL_THIS(iface);
682
683     TRACE("(%p)->(%p %p %x)\n", This, aHeader, aValue, aMerge);
684
685     if(This->http_channel)
686         return nsIHttpChannel_SetRequestHeader(This->http_channel, aHeader, aValue, aMerge);
687
688     return NS_ERROR_NOT_IMPLEMENTED;
689 }
690
691 static nsresult NSAPI nsChannel_VisitRequestHeaders(nsIHttpChannel *iface,
692                                                     nsIHttpHeaderVisitor *aVisitor)
693 {
694     nsChannel *This = NSCHANNEL_THIS(iface);
695
696     TRACE("(%p)->(%p)\n", This, aVisitor);
697
698     if(This->http_channel)
699         return nsIHttpChannel_VisitRequestHeaders(This->http_channel, aVisitor);
700
701     return NS_ERROR_NOT_IMPLEMENTED;
702 }
703
704 static nsresult NSAPI nsChannel_GetAllowPipelining(nsIHttpChannel *iface, PRBool *aAllowPipelining)
705 {
706     nsChannel *This = NSCHANNEL_THIS(iface);
707
708     TRACE("(%p)->(%p)\n", This, aAllowPipelining);
709
710     if(This->http_channel)
711         return nsIHttpChannel_GetAllowPipelining(This->http_channel, aAllowPipelining);
712
713     return NS_ERROR_NOT_IMPLEMENTED;
714 }
715
716 static nsresult NSAPI nsChannel_SetAllowPipelining(nsIHttpChannel *iface, PRBool aAllowPipelining)
717 {
718     nsChannel *This = NSCHANNEL_THIS(iface);
719
720     TRACE("(%p)->(%x)\n", This, aAllowPipelining);
721
722     if(This->http_channel)
723         return nsIHttpChannel_SetAllowPipelining(This->http_channel, aAllowPipelining);
724
725     return NS_ERROR_NOT_IMPLEMENTED;
726 }
727
728 static nsresult NSAPI nsChannel_GetRedirectionLimit(nsIHttpChannel *iface, PRUint32 *aRedirectionLimit)
729 {
730     nsChannel *This = NSCHANNEL_THIS(iface);
731
732     TRACE("(%p)->(%p)\n", This, aRedirectionLimit);
733
734     if(This->http_channel)
735         return nsIHttpChannel_GetRedirectionLimit(This->http_channel, aRedirectionLimit);
736
737     return NS_ERROR_NOT_IMPLEMENTED;
738 }
739
740 static nsresult NSAPI nsChannel_SetRedirectionLimit(nsIHttpChannel *iface, PRUint32 aRedirectionLimit)
741 {
742     nsChannel *This = NSCHANNEL_THIS(iface);
743
744     TRACE("(%p)->(%lu)\n", This, aRedirectionLimit);
745
746     if(This->http_channel)
747         return nsIHttpChannel_SetRedirectionLimit(This->http_channel, aRedirectionLimit);
748
749     return NS_ERROR_NOT_IMPLEMENTED;
750 }
751
752 static nsresult NSAPI nsChannel_GetResponseStatus(nsIHttpChannel *iface, PRUint32 *aResponseStatus)
753 {
754     nsChannel *This = NSCHANNEL_THIS(iface);
755
756     TRACE("(%p)->(%p)\n", This, aResponseStatus);
757
758     if(This->http_channel)
759         return nsIHttpChannel_GetResponseStatus(This->http_channel, aResponseStatus);
760
761     return NS_ERROR_NOT_IMPLEMENTED;
762 }
763
764 static nsresult NSAPI nsChannel_GetResponseStatusText(nsIHttpChannel *iface,
765                                                       nsACString *aResponseStatusText)
766 {
767     nsChannel *This = NSCHANNEL_THIS(iface);
768
769     TRACE("(%p)->(%p)\n", This, aResponseStatusText);
770
771     if(This->http_channel)
772         return nsIHttpChannel_GetResponseStatusText(This->http_channel, aResponseStatusText);
773
774     return NS_ERROR_NOT_IMPLEMENTED;
775 }
776
777 static nsresult NSAPI nsChannel_GetRequestSucceeded(nsIHttpChannel *iface,
778                                                     PRBool *aRequestSucceeded)
779 {
780     nsChannel *This = NSCHANNEL_THIS(iface);
781
782     TRACE("(%p)->(%p)\n", This, aRequestSucceeded);
783
784     if(This->http_channel)
785         return nsIHttpChannel_GetRequestSucceeded(This->http_channel, aRequestSucceeded);
786
787     return NS_ERROR_NOT_IMPLEMENTED;
788 }
789
790 static nsresult NSAPI nsChannel_GetResponseHeader(nsIHttpChannel *iface,
791          const nsACString *header, nsACString *_retval)
792 {
793     nsChannel *This = NSCHANNEL_THIS(iface);
794
795     TRACE("(%p)->(%p %p)\n", This, header, _retval);
796
797     if(This->http_channel)
798         return nsIHttpChannel_GetResponseHeader(This->http_channel, header, _retval);
799
800     return NS_ERROR_NOT_IMPLEMENTED;
801 }
802
803 static nsresult NSAPI nsChannel_SetResponseHeader(nsIHttpChannel *iface,
804         const nsACString *header, const nsACString *value, PRBool merge)
805 {
806     nsChannel *This = NSCHANNEL_THIS(iface);
807
808     TRACE("(%p)->(%p %p %x)\n", This, header, value, merge);
809
810     if(This->http_channel)
811         return nsIHttpChannel_SetResponseHeader(This->http_channel, header, value, merge);
812
813     return NS_ERROR_NOT_IMPLEMENTED;
814 }
815
816 static nsresult NSAPI nsChannel_VisitResponseHeaders(nsIHttpChannel *iface,
817         nsIHttpHeaderVisitor *aVisitor)
818 {
819     nsChannel *This = NSCHANNEL_THIS(iface);
820
821     TRACE("(%p)->(%p)\n", This, aVisitor);
822
823     if(This->http_channel)
824         return nsIHttpChannel_VisitResponseHeaders(This->http_channel, aVisitor);
825
826     return NS_ERROR_NOT_IMPLEMENTED;
827 }
828
829 static nsresult NSAPI nsChannel_IsNoStoreResponse(nsIHttpChannel *iface, PRBool *_retval)
830 {
831     nsChannel *This = NSCHANNEL_THIS(iface);
832
833     TRACE("(%p)->(%p)\n", This, _retval);
834
835     if(This->http_channel)
836         return nsIHttpChannel_IsNoStoreResponse(This->http_channel, _retval);
837
838     return NS_ERROR_NOT_IMPLEMENTED;
839 }
840
841 static nsresult NSAPI nsChannel_IsNoCacheResponse(nsIHttpChannel *iface, PRBool *_retval)
842 {
843     nsChannel *This = NSCHANNEL_THIS(iface);
844
845     TRACE("(%p)->(%p)\n", This, _retval);
846
847     if(This->http_channel)
848         return nsIHttpChannel_IsNoCacheResponse(This->http_channel, _retval);
849
850     return NS_ERROR_NOT_IMPLEMENTED;
851 }
852
853 #undef NSCHANNEL_THIS
854
855 static const nsIHttpChannelVtbl nsChannelVtbl = {
856     nsChannel_QueryInterface,
857     nsChannel_AddRef,
858     nsChannel_Release,
859     nsChannel_GetName,
860     nsChannel_IsPending,
861     nsChannel_GetStatus,
862     nsChannel_Cancel,
863     nsChannel_Suspend,
864     nsChannel_Resume,
865     nsChannel_GetLoadGroup,
866     nsChannel_SetLoadGroup,
867     nsChannel_GetLoadFlags,
868     nsChannel_SetLoadFlags,
869     nsChannel_GetOriginalURI,
870     nsChannel_SetOriginalURI,
871     nsChannel_GetURI,
872     nsChannel_GetOwner,
873     nsChannel_SetOwner,
874     nsChannel_GetNotificationCallbacks,
875     nsChannel_SetNotificationCallbacks,
876     nsChannel_GetSecurityInfo,
877     nsChannel_GetContentType,
878     nsChannel_SetContentType,
879     nsChannel_GetContentCharset,
880     nsChannel_SetContentCharset,
881     nsChannel_GetContentLength,
882     nsChannel_SetContentLength,
883     nsChannel_Open,
884     nsChannel_AsyncOpen,
885     nsChannel_GetRequestMethod,
886     nsChannel_SetRequestMethod,
887     nsChannel_GetReferrer,
888     nsChannel_SetReferrer,
889     nsChannel_GetRequestHeader,
890     nsChannel_SetRequestHeader,
891     nsChannel_VisitRequestHeaders,
892     nsChannel_GetAllowPipelining,
893     nsChannel_SetAllowPipelining,
894     nsChannel_GetRedirectionLimit,
895     nsChannel_SetRedirectionLimit,
896     nsChannel_GetResponseStatus,
897     nsChannel_GetResponseStatusText,
898     nsChannel_GetRequestSucceeded,
899     nsChannel_GetResponseHeader,
900     nsChannel_SetResponseHeader,
901     nsChannel_VisitResponseHeaders,
902     nsChannel_IsNoStoreResponse,
903     nsChannel_IsNoCacheResponse
904 };
905
906 #define NSUPCHANNEL_THIS(iface) DEFINE_THIS(nsChannel, UploadChannel, iface)
907
908 static nsresult NSAPI nsUploadChannel_QueryInterface(nsIUploadChannel *iface, nsIIDRef riid,
909                                                      nsQIResult result)
910 {
911     nsChannel *This = NSUPCHANNEL_THIS(iface);
912     return nsIChannel_QueryInterface(NSCHANNEL(This), riid, result);
913 }
914
915 static nsrefcnt NSAPI nsUploadChannel_AddRef(nsIUploadChannel *iface)
916 {
917     nsChannel *This = NSUPCHANNEL_THIS(iface);
918     return nsIChannel_AddRef(NSCHANNEL(This));
919 }
920
921 static nsrefcnt NSAPI nsUploadChannel_Release(nsIUploadChannel *iface)
922 {
923     nsChannel *This = NSUPCHANNEL_THIS(iface);
924     return nsIChannel_Release(NSCHANNEL(This));
925 }
926
927 static nsresult NSAPI nsUploadChannel_SetUploadStream(nsIUploadChannel *iface,
928         nsIInputStream *aStream, const nsACString *aContentType, PRInt32 aContentLength)
929 {
930     nsChannel *This = NSUPCHANNEL_THIS(iface);
931     const char *content_type;
932
933     TRACE("(%p)->(%p %p %ld)\n", This, aStream, aContentType, aContentLength);
934
935     if(This->post_data_stream)
936         nsIInputStream_Release(This->post_data_stream);
937
938     if(aContentType) {
939         nsACString_GetData(aContentType, &content_type, NULL);
940         if(*content_type)
941             FIXME("Unsupported aContentType argument: %s\n", debugstr_a(content_type));
942     }
943
944     if(aContentLength != -1)
945         FIXME("Unsupported acontentLength = %ld\n", aContentLength);
946
947     if(aStream)
948         nsIInputStream_AddRef(aStream);
949     This->post_data_stream = aStream;
950
951     return NS_OK;
952 }
953
954 static nsresult NSAPI nsUploadChannel_GetUploadStream(nsIUploadChannel *iface,
955         nsIInputStream **aUploadStream)
956 {
957     nsChannel *This = NSUPCHANNEL_THIS(iface);
958
959     TRACE("(%p)->(%p)\n", This, aUploadStream);
960
961     if(This->post_data_stream)
962         nsIInputStream_AddRef(This->post_data_stream);
963
964     *aUploadStream = This->post_data_stream;
965     return NS_OK;
966 }
967
968 #undef NSUPCHANNEL_THIS
969
970 static const nsIUploadChannelVtbl nsUploadChannelVtbl = {
971     nsUploadChannel_QueryInterface,
972     nsUploadChannel_AddRef,
973     nsUploadChannel_Release,
974     nsUploadChannel_SetUploadStream,
975     nsUploadChannel_GetUploadStream
976 };
977
978 #define NSURI_THIS(iface) DEFINE_THIS(nsURI, WineURI, iface)
979
980 static nsresult NSAPI nsURI_QueryInterface(nsIWineURI *iface, nsIIDRef riid, nsQIResult result)
981 {
982     nsURI *This = NSURI_THIS(iface);
983
984     *result = NULL;
985
986     if(IsEqualGUID(&IID_nsISupports, riid)) {
987         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
988         *result = NSURI(This);
989     }else if(IsEqualGUID(&IID_nsIURI, riid)) {
990         TRACE("(%p)->(IID_nsIURI %p)\n", This, result);
991         *result = NSURI(This);
992     }else if(IsEqualGUID(&IID_nsIWineURI, riid)) {
993         TRACE("(%p)->(IID_nsIWineURI %p)\n", This, result);
994         *result = NSURI(This);
995     }
996
997     if(*result) {
998         nsIURI_AddRef(NSURI(This));
999         return NS_OK;
1000     }
1001
1002     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1003     return nsIURI_QueryInterface(This->uri, riid, result);
1004 }
1005
1006 static nsrefcnt NSAPI nsURI_AddRef(nsIWineURI *iface)
1007 {
1008     nsURI *This = NSURI_THIS(iface);
1009     LONG ref = InterlockedIncrement(&This->ref);
1010
1011     TRACE("(%p) ref=%ld\n", This, ref);
1012
1013     return ref;
1014 }
1015
1016 static nsrefcnt NSAPI nsURI_Release(nsIWineURI *iface)
1017 {
1018     nsURI *This = NSURI_THIS(iface);
1019     LONG ref = InterlockedDecrement(&This->ref);
1020
1021     TRACE("(%p) ref=%ld\n", This, ref);
1022
1023     if(!ref) {
1024         if(This->container)
1025             nsIWebBrowserChrome_Release(NSWBCHROME(This->container));
1026         nsIURI_Release(This->uri);
1027         HeapFree(GetProcessHeap(), 0, This);
1028     }
1029
1030     return ref;
1031 }
1032
1033 static nsresult NSAPI nsURI_GetSpec(nsIWineURI *iface, nsACString *aSpec)
1034 {
1035     nsURI *This = NSURI_THIS(iface);
1036     TRACE("(%p)->(%p)\n", This, aSpec);
1037     return nsIURI_GetSpec(This->uri, aSpec);
1038 }
1039
1040 static nsresult NSAPI nsURI_SetSpec(nsIWineURI *iface, const nsACString *aSpec)
1041 {
1042     nsURI *This = NSURI_THIS(iface);
1043     TRACE("(%p)->(%p)\n", This, aSpec);
1044     return nsIURI_SetSpec(This->uri, aSpec);
1045 }
1046
1047 static nsresult NSAPI nsURI_GetPrePath(nsIWineURI *iface, nsACString *aPrePath)
1048 {
1049     nsURI *This = NSURI_THIS(iface);
1050     TRACE("(%p)->(%p)\n", This, aPrePath);
1051     return nsIURI_GetPrePath(This->uri, aPrePath);
1052 }
1053
1054 static nsresult NSAPI nsURI_GetScheme(nsIWineURI *iface, nsACString *aScheme)
1055 {
1056     nsURI *This = NSURI_THIS(iface);
1057     TRACE("(%p)->(%p)\n", This, aScheme);
1058     return nsIURI_GetScheme(This->uri, aScheme);
1059 }
1060
1061 static nsresult NSAPI nsURI_SetScheme(nsIWineURI *iface, const nsACString *aScheme)
1062 {
1063     nsURI *This = NSURI_THIS(iface);
1064     TRACE("(%p)->(%p)\n", This, aScheme);
1065     return nsIURI_SetScheme(This->uri, aScheme);
1066 }
1067
1068 static nsresult NSAPI nsURI_GetUserPass(nsIWineURI *iface, nsACString *aUserPass)
1069 {
1070     nsURI *This = NSURI_THIS(iface);
1071     TRACE("(%p)->(%p)\n", This, aUserPass);
1072     return nsIURI_GetUserPass(This->uri, aUserPass);
1073 }
1074
1075 static nsresult NSAPI nsURI_SetUserPass(nsIWineURI *iface, const nsACString *aUserPass)
1076 {
1077     nsURI *This = NSURI_THIS(iface);
1078     TRACE("(%p)->(%p)\n", This, aUserPass);
1079     return nsIURI_SetUserPass(This->uri, aUserPass);
1080 }
1081
1082 static nsresult NSAPI nsURI_GetUsername(nsIWineURI *iface, nsACString *aUsername)
1083 {
1084     nsURI *This = NSURI_THIS(iface);
1085     TRACE("(%p)->(%p)\n", This, aUsername);
1086     return nsIURI_GetUsername(This->uri, aUsername);
1087 }
1088
1089 static nsresult NSAPI nsURI_SetUsername(nsIWineURI *iface, const nsACString *aUsername)
1090 {
1091     nsURI *This = NSURI_THIS(iface);
1092     TRACE("(%p)->(%p)\n", This, aUsername);
1093     return nsIURI_SetUsername(This->uri, aUsername);
1094 }
1095
1096 static nsresult NSAPI nsURI_GetPassword(nsIWineURI *iface, nsACString *aPassword)
1097 {
1098     nsURI *This = NSURI_THIS(iface);
1099     TRACE("(%p)->(%p)\n", This, aPassword);
1100     return nsIURI_GetPassword(This->uri, aPassword);
1101 }
1102
1103 static nsresult NSAPI nsURI_SetPassword(nsIWineURI *iface, const nsACString *aPassword)
1104 {
1105     nsURI *This = NSURI_THIS(iface);
1106     TRACE("(%p)->(%p)\n", This, aPassword);
1107     return nsIURI_SetPassword(This->uri, aPassword);
1108 }
1109
1110 static nsresult NSAPI nsURI_GetHostPort(nsIWineURI *iface, nsACString *aHostPort)
1111 {
1112     nsURI *This = NSURI_THIS(iface);
1113     TRACE("(%p)->(%p)\n", This, aHostPort);
1114     return nsIURI_GetHostPort(This->uri, aHostPort);
1115 }
1116
1117 static nsresult NSAPI nsURI_SetHostPort(nsIWineURI *iface, const nsACString *aHostPort)
1118 {
1119     nsURI *This = NSURI_THIS(iface);
1120     TRACE("(%p)->(%p)\n", This, aHostPort);
1121     return nsIURI_SetHostPort(This->uri, aHostPort);
1122 }
1123
1124 static nsresult NSAPI nsURI_GetHost(nsIWineURI *iface, nsACString *aHost)
1125 {
1126     nsURI *This = NSURI_THIS(iface);
1127     TRACE("(%p)->(%p)\n", This, aHost);
1128     return nsIURI_GetHost(This->uri, aHost);
1129 }
1130
1131 static nsresult NSAPI nsURI_SetHost(nsIWineURI *iface, const nsACString *aHost)
1132 {
1133     nsURI *This = NSURI_THIS(iface);
1134     TRACE("(%p)->(%p)\n", This, aHost);
1135     return nsIURI_SetHost(This->uri, aHost);
1136 }
1137
1138 static nsresult NSAPI nsURI_GetPort(nsIWineURI *iface, PRInt32 *aPort)
1139 {
1140     nsURI *This = NSURI_THIS(iface);
1141     TRACE("(%p)->(%p)\n", This, aPort);
1142     return nsIURI_GetPort(This->uri, aPort);
1143 }
1144
1145 static nsresult NSAPI nsURI_SetPort(nsIWineURI *iface, PRInt32 aPort)
1146 {
1147     nsURI *This = NSURI_THIS(iface);
1148     TRACE("(%p)->(%ld)\n", This, aPort);
1149     return nsIURI_SetPort(This->uri, aPort);
1150 }
1151
1152 static nsresult NSAPI nsURI_GetPath(nsIWineURI *iface, nsACString *aPath)
1153 {
1154     nsURI *This = NSURI_THIS(iface);
1155     TRACE("(%p)->(%p)\n", This, aPath);
1156     return nsIURI_GetPath(This->uri, aPath);
1157 }
1158
1159 static nsresult NSAPI nsURI_SetPath(nsIWineURI *iface, const nsACString *aPath)
1160 {
1161     nsURI *This = NSURI_THIS(iface);
1162     TRACE("(%p)->(%p)\n", This, aPath);
1163     return nsIURI_SetPath(This->uri, aPath);
1164 }
1165
1166 static nsresult NSAPI nsURI_Equals(nsIWineURI *iface, nsIURI *other, PRBool *_retval)
1167 {
1168     nsURI *This = NSURI_THIS(iface);
1169     TRACE("(%p)->(%p %p)\n", This, other, _retval);
1170     return nsIURI_Equals(This->uri, other, _retval);
1171 }
1172
1173 static nsresult NSAPI nsURI_SchemeIs(nsIWineURI *iface, const char *scheme, PRBool *_retval)
1174 {
1175     nsURI *This = NSURI_THIS(iface);
1176     TRACE("(%p)->(%s %p)\n", This, debugstr_a(scheme), _retval);
1177     return nsIURI_SchemeIs(This->uri, scheme, _retval);
1178 }
1179
1180 static nsresult NSAPI nsURI_Clone(nsIWineURI *iface, nsIURI **_retval)
1181 {
1182     nsURI *This = NSURI_THIS(iface);
1183     TRACE("(%p)->(%p)\n", This, _retval);
1184     return nsIURI_Clone(This->uri, _retval);
1185 }
1186
1187 static nsresult NSAPI nsURI_Resolve(nsIWineURI *iface, const nsACString *arelativePath,
1188         nsACString *_retval)
1189 {
1190     nsURI *This = NSURI_THIS(iface);
1191     TRACE("(%p)->(%p %p)\n", This, arelativePath, _retval);
1192     return nsIURI_Resolve(This->uri, arelativePath, _retval);
1193 }
1194
1195 static nsresult NSAPI nsURI_GetAsciiSpec(nsIWineURI *iface, nsACString *aAsciiSpec)
1196 {
1197     nsURI *This = NSURI_THIS(iface);
1198     TRACE("(%p)->(%p)\n", This, aAsciiSpec);
1199     return nsIURI_GetAsciiSpec(This->uri, aAsciiSpec);
1200 }
1201
1202 static nsresult NSAPI nsURI_GetAsciiHost(nsIWineURI *iface, nsACString *aAsciiHost)
1203 {
1204     nsURI *This = NSURI_THIS(iface);
1205     TRACE("(%p)->(%p)\n", This, aAsciiHost);
1206     return nsIURI_GetAsciiHost(This->uri, aAsciiHost);
1207 }
1208
1209 static nsresult NSAPI nsURI_GetOriginCharset(nsIWineURI *iface, nsACString *aOriginCharset)
1210 {
1211     nsURI *This = NSURI_THIS(iface);
1212     TRACE("(%p)->(%p)\n", This, aOriginCharset);
1213     return nsIURI_GetOriginCharset(This->uri, aOriginCharset);
1214 }
1215
1216 static nsresult NSAPI nsURI_GetNSContainer(nsIWineURI *iface, NSContainer **aContainer)
1217 {
1218     nsURI *This = NSURI_THIS(iface);
1219
1220     TRACE("(%p)->(%p)\n", This, aContainer);
1221
1222     if(This->container)
1223         nsIWebBrowserChrome_AddRef(NSWBCHROME(This->container));
1224     *aContainer = This->container;
1225
1226     return NS_OK;
1227 }
1228
1229 static nsresult NSAPI nsURI_SetNSContainer(nsIWineURI *iface, NSContainer *aContainer)
1230 {
1231     nsURI *This = NSURI_THIS(iface);
1232
1233     TRACE("(%p)->(%p)\n", This, aContainer);
1234
1235     if(This->container) {
1236         WARN("Container already set: %p\n", This->container);
1237         nsIWebBrowserChrome_Release(NSWBCHROME(This->container));
1238     }
1239
1240     if(aContainer)
1241         nsIWebBrowserChrome_AddRef(NSWBCHROME(aContainer));
1242     This->container = aContainer;
1243
1244     return NS_OK;
1245 }
1246
1247 #undef NSURI_THIS
1248
1249 static const nsIWineURIVtbl nsWineURIVtbl = {
1250     nsURI_QueryInterface,
1251     nsURI_AddRef,
1252     nsURI_Release,
1253     nsURI_GetSpec,
1254     nsURI_SetSpec,
1255     nsURI_GetPrePath,
1256     nsURI_GetScheme,
1257     nsURI_SetScheme,
1258     nsURI_GetUserPass,
1259     nsURI_SetUserPass,
1260     nsURI_GetUsername,
1261     nsURI_SetUsername,
1262     nsURI_GetPassword,
1263     nsURI_SetPassword,
1264     nsURI_GetHostPort,
1265     nsURI_SetHostPort,
1266     nsURI_GetHost,
1267     nsURI_SetHost,
1268     nsURI_GetPort,
1269     nsURI_SetPort,
1270     nsURI_GetPath,
1271     nsURI_SetPath,
1272     nsURI_Equals,
1273     nsURI_SchemeIs,
1274     nsURI_Clone,
1275     nsURI_Resolve,
1276     nsURI_GetAsciiSpec,
1277     nsURI_GetAsciiHost,
1278     nsURI_GetOriginCharset,
1279     nsURI_GetNSContainer,
1280     nsURI_SetNSContainer,
1281 };
1282
1283 static nsresult NSAPI nsIOService_QueryInterface(nsIIOService *iface, nsIIDRef riid,
1284                                                  nsQIResult result)
1285 {
1286     *result = NULL;
1287
1288     if(IsEqualGUID(&IID_nsISupports, riid)) {
1289         TRACE("(IID_nsISupports %p)\n", result);
1290         *result = iface;
1291     }else if(IsEqualGUID(&IID_nsIIOService, riid)) {
1292         TRACE("(IID_nsIIOService %p)\n", result);
1293         *result = iface;
1294     }
1295
1296     if(*result) {
1297         nsIIOService_AddRef(iface);
1298         return S_OK;
1299     }
1300
1301     WARN("(%s %p)\n", debugstr_guid(riid), result);
1302     return NS_NOINTERFACE;
1303 }
1304
1305 static nsrefcnt NSAPI nsIOService_AddRef(nsIIOService *iface)
1306 {
1307     return 2;
1308 }
1309
1310 static nsrefcnt NSAPI nsIOService_Release(nsIIOService *iface)
1311 {
1312     return 1;
1313 }
1314
1315 static nsresult NSAPI nsIOService_GetProtocolHandler(nsIIOService *iface, const char *aScheme,
1316                                                      nsIProtocolHandler **_retval)
1317 {
1318     TRACE("(%s %p)\n", debugstr_a(aScheme), _retval);
1319     return nsIIOService_GetProtocolHandler(nsio, aScheme, _retval);
1320 }
1321
1322 static nsresult NSAPI nsIOService_GetProtocolFlags(nsIIOService *iface, const char *aScheme,
1323                                                     PRUint32 *_retval)
1324 {
1325     TRACE("(%s %p)\n", debugstr_a(aScheme), _retval);
1326     return nsIIOService_GetProtocolFlags(nsio, aScheme, _retval);
1327 }
1328
1329 static nsresult NSAPI nsIOService_NewURI(nsIIOService *iface, const nsACString *aSpec,
1330         const char *aOriginCharset, nsIURI *aBaseURI, nsIURI **_retval)
1331 {
1332     const char *spec = NULL;
1333     nsIURI *uri;
1334     nsURI *ret;
1335     PRBool is_javascript = FALSE;
1336     nsresult nsres;
1337
1338     nsACString_GetData(aSpec, &spec, NULL);
1339
1340     TRACE("(%p(%s) %s %p %p)\n", aSpec, debugstr_a(spec), debugstr_a(aOriginCharset),
1341           aBaseURI, _retval);
1342
1343     if(aBaseURI) {
1344         nsACString *base_uri_str = nsACString_Create();
1345         const char *base_uri = NULL;
1346
1347         nsres = nsIURI_GetSpec(aBaseURI, base_uri_str);
1348         if(NS_SUCCEEDED(nsres)) {
1349             nsACString_GetData(base_uri_str, &base_uri, NULL);
1350             TRACE("uri=%s\n", debugstr_a(base_uri));
1351         }else {
1352             ERR("GetSpec failed: %08lx\n", nsres);
1353         }
1354
1355         nsACString_Destroy(base_uri_str);
1356     }
1357
1358     nsres = nsIIOService_NewURI(nsio, aSpec, aOriginCharset, aBaseURI, &uri);
1359     if(NS_FAILED(nsres)) {
1360         WARN("NewURI failed: %08lx\n", nsres);
1361         return nsres;
1362     }
1363
1364     nsIURI_SchemeIs(uri, "javascript", &is_javascript);
1365     if(is_javascript) {
1366         TRACE("returning javascript uri: %p\n", uri);
1367         *_retval = uri;
1368         return NS_OK;
1369     }
1370
1371     ret = HeapAlloc(GetProcessHeap(), 0, sizeof(nsURI));
1372
1373     ret->lpWineURIVtbl = &nsWineURIVtbl;
1374     ret->ref = 1;
1375     ret->uri = uri;
1376     ret->container = NULL;
1377
1378     TRACE("_retval = %p\n", ret);
1379     *_retval = NSURI(ret);
1380     return NS_OK;
1381 }
1382
1383 static nsresult NSAPI nsIOService_NewFileURI(nsIIOService *iface, nsIFile *aFile,
1384                                              nsIURI **_retval)
1385 {
1386     TRACE("(%p %p)\n", aFile, _retval);
1387     return nsIIOService_NewFileURI(nsio, aFile, _retval);
1388 }
1389
1390 static nsresult NSAPI nsIOService_NewChannelFromURI(nsIIOService *iface, nsIURI *aURI,
1391                                                      nsIChannel **_retval)
1392 {
1393     nsIChannel *channel = NULL;
1394     nsChannel *ret;
1395     nsIWineURI *wine_uri;
1396     nsresult nsres;
1397
1398     TRACE("(%p %p)\n", aURI, _retval);
1399
1400     nsres = nsIIOService_NewChannelFromURI(nsio, aURI, &channel);
1401     if(NS_FAILED(nsres) && nsres != NS_ERROR_UNKNOWN_PROTOCOL) {
1402         WARN("NewChannelFromURI failed: %08lx\n", nsres);
1403         *_retval = channel;
1404         return nsres;
1405     }
1406
1407     nsres = nsIURI_QueryInterface(aURI, &IID_nsIWineURI, (void**)&wine_uri);
1408     if(NS_FAILED(nsres)) {
1409         WARN("Could not get nsIWineURI: %08lx\n", nsres);
1410         *_retval = channel;
1411         return NS_OK;
1412     }
1413
1414     ret = HeapAlloc(GetProcessHeap(), 0, sizeof(nsChannel));
1415
1416     ret->lpHttpChannelVtbl = &nsChannelVtbl;
1417     ret->lpUploadChannelVtbl = &nsUploadChannelVtbl;
1418     ret->ref = 1;
1419     ret->channel = channel;
1420     ret->http_channel = NULL;
1421     ret->uri = wine_uri;
1422     ret->post_data_stream = NULL;
1423     ret->load_group = NULL;
1424     ret->notif_callback = NULL;
1425     ret->load_flags = 0;
1426
1427     nsIURI_AddRef(aURI);
1428     ret->original_uri = aURI;
1429
1430     if(channel)
1431         nsIChannel_QueryInterface(channel, &IID_nsIHttpChannel, (void**)&ret->http_channel);
1432
1433     *_retval = NSCHANNEL(ret);
1434     return NS_OK;
1435 }
1436
1437 static nsresult NSAPI nsIOService_NewChannel(nsIIOService *iface, const nsACString *aSpec,
1438         const char *aOriginCharset, nsIURI *aBaseURI, nsIChannel **_retval)
1439 {
1440     TRACE("(%p %s %p %p)\n", aSpec, debugstr_a(aOriginCharset), aBaseURI, _retval);
1441     return nsIIOService_NewChannel(nsio, aSpec, aOriginCharset, aBaseURI, _retval);
1442 }
1443
1444 static nsresult NSAPI nsIOService_GetOffline(nsIIOService *iface, PRBool *aOffline)
1445 {
1446     TRACE("(%p)\n", aOffline);
1447     return nsIIOService_GetOffline(nsio, aOffline);
1448 }
1449
1450 static nsresult NSAPI nsIOService_SetOffline(nsIIOService *iface, PRBool aOffline)
1451 {
1452     TRACE("(%x)\n", aOffline);
1453     return nsIIOService_SetOffline(nsio, aOffline);
1454 }
1455
1456 static nsresult NSAPI nsIOService_AllowPort(nsIIOService *iface, PRInt32 aPort,
1457                                              const char *aScheme, PRBool *_retval)
1458 {
1459     TRACE("(%ld %s %p)\n", aPort, debugstr_a(aScheme), _retval);
1460     return nsIIOService_AllowPort(nsio, aPort, debugstr_a(aScheme), _retval);
1461 }
1462
1463 static nsresult NSAPI nsIOService_ExtractScheme(nsIIOService *iface, const nsACString *urlString,
1464                                                  nsACString * _retval)
1465 {
1466     TRACE("(%p %p)\n", urlString, _retval);
1467     return nsIIOService_ExtractScheme(nsio, urlString, _retval);
1468 }
1469
1470 static const nsIIOServiceVtbl nsIOServiceVtbl = {
1471     nsIOService_QueryInterface,
1472     nsIOService_AddRef,
1473     nsIOService_Release,
1474     nsIOService_GetProtocolHandler,
1475     nsIOService_GetProtocolFlags,
1476     nsIOService_NewURI,
1477     nsIOService_NewFileURI,
1478     nsIOService_NewChannelFromURI,
1479     nsIOService_NewChannel,
1480     nsIOService_GetOffline,
1481     nsIOService_SetOffline,
1482     nsIOService_AllowPort,
1483     nsIOService_ExtractScheme
1484 };
1485
1486 static nsIIOService nsIOService = { &nsIOServiceVtbl };
1487
1488 static nsresult NSAPI nsIOServiceFactory_QueryInterface(nsIFactory *iface, nsIIDRef riid,
1489                                                         nsQIResult result)
1490 {
1491     *result = NULL;
1492
1493     if(IsEqualGUID(&IID_nsISupports, riid)) {
1494         TRACE("(IID_nsISupoprts %p)\n", result);
1495         *result = iface;
1496     }else if(IsEqualGUID(&IID_nsIFactory, riid)) {
1497         TRACE("(IID_nsIFactory %p)\n", result);
1498         *result = iface;
1499     }
1500
1501     if(*result) {
1502         nsIFactory_AddRef(iface);
1503         return NS_OK;
1504     }
1505
1506     WARN("(%s %p)\n", debugstr_guid(riid), result);
1507     return NS_NOINTERFACE;
1508 }
1509
1510 static nsrefcnt NSAPI nsIOServiceFactory_AddRef(nsIFactory *iface)
1511 {
1512     return 2;
1513 }
1514
1515 static nsrefcnt NSAPI nsIOServiceFactory_Release(nsIFactory *iface)
1516 {
1517     return 1;
1518 }
1519
1520 static nsresult NSAPI nsIOServiceFactory_CreateInstance(nsIFactory *iface,
1521         nsISupports *aOuter, const nsIID *iid, void **result)
1522 {
1523     return nsIIOService_QueryInterface(&nsIOService, iid, result);
1524 }
1525
1526 static nsresult NSAPI nsIOServiceFactory_LockFactory(nsIFactory *iface, PRBool lock)
1527 {
1528     WARN("(%x)\n", lock);
1529     return NS_OK;
1530 }
1531
1532 static const nsIFactoryVtbl nsIOServiceFactoryVtbl = {
1533     nsIOServiceFactory_QueryInterface,
1534     nsIOServiceFactory_AddRef,
1535     nsIOServiceFactory_Release,
1536     nsIOServiceFactory_CreateInstance,
1537     nsIOServiceFactory_LockFactory
1538 };
1539
1540 static nsIFactory nsIOServiceFactory = { &nsIOServiceFactoryVtbl };
1541
1542 void init_nsio(nsIComponentManager *component_manager, nsIComponentRegistrar *registrar)
1543 {
1544     nsIFactory *old_factory = NULL;
1545     nsresult nsres;
1546
1547     nsres = nsIComponentManager_GetClassObject(component_manager, &NS_IOSERVICE_CID,
1548                                                &IID_nsIFactory, (void**)&old_factory);
1549     if(NS_FAILED(nsres)) {
1550         ERR("Could not get factory: %08lx\n", nsres);
1551         nsIFactory_Release(old_factory);
1552         return;
1553     }
1554
1555     nsres = nsIFactory_CreateInstance(old_factory, NULL, &IID_nsIIOService, (void**)&nsio);
1556     if(NS_FAILED(nsres)) {
1557         ERR("Couldn not create nsIOService instance %08lx\n", nsres);
1558         nsIFactory_Release(old_factory);
1559         return;
1560     }
1561
1562     nsres = nsIComponentRegistrar_UnregisterFactory(registrar, &NS_IOSERVICE_CID, old_factory);
1563     nsIFactory_Release(old_factory);
1564     if(NS_FAILED(nsres))
1565         ERR("UnregisterFactory failed: %08lx\n", nsres);
1566
1567     nsres = nsIComponentRegistrar_RegisterFactory(registrar, &NS_IOSERVICE_CID,
1568             NS_IOSERVICE_CLASSNAME, NS_IOSERVICE_CONTRACTID, &nsIOServiceFactory);
1569     if(NS_FAILED(nsres))
1570         ERR("RegisterFactory failed: %08lx\n", nsres);
1571 }
1572
1573 nsIURI *get_nsIURI(LPCWSTR url)
1574 {
1575     nsIURI *ret;
1576     nsACString *acstr;
1577     nsresult nsres;
1578     char *urla;
1579     int len;
1580
1581     len = WideCharToMultiByte(CP_ACP, 0, url, -1, NULL, -1, NULL, NULL);
1582     urla = HeapAlloc(GetProcessHeap(), 0, len);
1583     WideCharToMultiByte(CP_ACP, 0, url, -1, urla, -1, NULL, NULL);
1584
1585     acstr = nsACString_Create();
1586     nsACString_SetData(acstr, urla);
1587
1588     nsres = nsIIOService_NewURI(nsio, acstr, NULL, NULL, &ret);
1589     if(NS_FAILED(nsres))
1590         FIXME("NewURI failed: %08lx\n", nsres);
1591
1592     nsACString_Destroy(acstr);
1593     HeapFree(GetProcessHeap(), 0, urla);
1594
1595     return ret;
1596 }