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