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