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