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