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