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