jscript: Inherit Math from Object.
[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->response_status) {
984         *aResponseStatus = This->response_status;
985         return NS_OK;
986     }
987
988     if(This->http_channel)
989         return nsIHttpChannel_GetResponseStatus(This->http_channel, aResponseStatus);
990
991     return NS_ERROR_UNEXPECTED;
992 }
993
994 static nsresult NSAPI nsChannel_GetResponseStatusText(nsIHttpChannel *iface,
995                                                       nsACString *aResponseStatusText)
996 {
997     nsChannel *This = NSCHANNEL_THIS(iface);
998
999     TRACE("(%p)->(%p)\n", This, aResponseStatusText);
1000
1001     if(This->http_channel)
1002         return nsIHttpChannel_GetResponseStatusText(This->http_channel, aResponseStatusText);
1003
1004     return NS_ERROR_NOT_IMPLEMENTED;
1005 }
1006
1007 static nsresult NSAPI nsChannel_GetRequestSucceeded(nsIHttpChannel *iface,
1008                                                     PRBool *aRequestSucceeded)
1009 {
1010     nsChannel *This = NSCHANNEL_THIS(iface);
1011
1012     TRACE("(%p)->(%p)\n", This, aRequestSucceeded);
1013
1014     if(This->http_channel)
1015         return nsIHttpChannel_GetRequestSucceeded(This->http_channel, aRequestSucceeded);
1016
1017     return NS_ERROR_NOT_IMPLEMENTED;
1018 }
1019
1020 static nsresult NSAPI nsChannel_GetResponseHeader(nsIHttpChannel *iface,
1021          const nsACString *header, nsACString *_retval)
1022 {
1023     nsChannel *This = NSCHANNEL_THIS(iface);
1024
1025     TRACE("(%p)->(%p %p)\n", This, header, _retval);
1026
1027     if(This->http_channel)
1028         return nsIHttpChannel_GetResponseHeader(This->http_channel, header, _retval);
1029
1030     return NS_ERROR_NOT_IMPLEMENTED;
1031 }
1032
1033 static nsresult NSAPI nsChannel_SetResponseHeader(nsIHttpChannel *iface,
1034         const nsACString *header, const nsACString *value, PRBool merge)
1035 {
1036     nsChannel *This = NSCHANNEL_THIS(iface);
1037
1038     TRACE("(%p)->(%p %p %x)\n", This, header, value, merge);
1039
1040     if(This->http_channel)
1041         return nsIHttpChannel_SetResponseHeader(This->http_channel, header, value, merge);
1042
1043     return NS_ERROR_NOT_IMPLEMENTED;
1044 }
1045
1046 static nsresult NSAPI nsChannel_VisitResponseHeaders(nsIHttpChannel *iface,
1047         nsIHttpHeaderVisitor *aVisitor)
1048 {
1049     nsChannel *This = NSCHANNEL_THIS(iface);
1050
1051     TRACE("(%p)->(%p)\n", This, aVisitor);
1052
1053     if(This->http_channel)
1054         return nsIHttpChannel_VisitResponseHeaders(This->http_channel, aVisitor);
1055
1056     return NS_ERROR_NOT_IMPLEMENTED;
1057 }
1058
1059 static nsresult NSAPI nsChannel_IsNoStoreResponse(nsIHttpChannel *iface, PRBool *_retval)
1060 {
1061     nsChannel *This = NSCHANNEL_THIS(iface);
1062
1063     TRACE("(%p)->(%p)\n", This, _retval);
1064
1065     if(This->http_channel)
1066         return nsIHttpChannel_IsNoStoreResponse(This->http_channel, _retval);
1067
1068     return NS_ERROR_NOT_IMPLEMENTED;
1069 }
1070
1071 static nsresult NSAPI nsChannel_IsNoCacheResponse(nsIHttpChannel *iface, PRBool *_retval)
1072 {
1073     nsChannel *This = NSCHANNEL_THIS(iface);
1074
1075     TRACE("(%p)->(%p)\n", This, _retval);
1076
1077     if(This->http_channel)
1078         return nsIHttpChannel_IsNoCacheResponse(This->http_channel, _retval);
1079
1080     return NS_ERROR_NOT_IMPLEMENTED;
1081 }
1082
1083 #undef NSCHANNEL_THIS
1084
1085 static const nsIHttpChannelVtbl nsChannelVtbl = {
1086     nsChannel_QueryInterface,
1087     nsChannel_AddRef,
1088     nsChannel_Release,
1089     nsChannel_GetName,
1090     nsChannel_IsPending,
1091     nsChannel_GetStatus,
1092     nsChannel_Cancel,
1093     nsChannel_Suspend,
1094     nsChannel_Resume,
1095     nsChannel_GetLoadGroup,
1096     nsChannel_SetLoadGroup,
1097     nsChannel_GetLoadFlags,
1098     nsChannel_SetLoadFlags,
1099     nsChannel_GetOriginalURI,
1100     nsChannel_SetOriginalURI,
1101     nsChannel_GetURI,
1102     nsChannel_GetOwner,
1103     nsChannel_SetOwner,
1104     nsChannel_GetNotificationCallbacks,
1105     nsChannel_SetNotificationCallbacks,
1106     nsChannel_GetSecurityInfo,
1107     nsChannel_GetContentType,
1108     nsChannel_SetContentType,
1109     nsChannel_GetContentCharset,
1110     nsChannel_SetContentCharset,
1111     nsChannel_GetContentLength,
1112     nsChannel_SetContentLength,
1113     nsChannel_Open,
1114     nsChannel_AsyncOpen,
1115     nsChannel_GetRequestMethod,
1116     nsChannel_SetRequestMethod,
1117     nsChannel_GetReferrer,
1118     nsChannel_SetReferrer,
1119     nsChannel_GetRequestHeader,
1120     nsChannel_SetRequestHeader,
1121     nsChannel_VisitRequestHeaders,
1122     nsChannel_GetAllowPipelining,
1123     nsChannel_SetAllowPipelining,
1124     nsChannel_GetRedirectionLimit,
1125     nsChannel_SetRedirectionLimit,
1126     nsChannel_GetResponseStatus,
1127     nsChannel_GetResponseStatusText,
1128     nsChannel_GetRequestSucceeded,
1129     nsChannel_GetResponseHeader,
1130     nsChannel_SetResponseHeader,
1131     nsChannel_VisitResponseHeaders,
1132     nsChannel_IsNoStoreResponse,
1133     nsChannel_IsNoCacheResponse
1134 };
1135
1136 #define NSUPCHANNEL_THIS(iface) DEFINE_THIS(nsChannel, UploadChannel, iface)
1137
1138 static nsresult NSAPI nsUploadChannel_QueryInterface(nsIUploadChannel *iface, nsIIDRef riid,
1139                                                      nsQIResult result)
1140 {
1141     nsChannel *This = NSUPCHANNEL_THIS(iface);
1142     return nsIChannel_QueryInterface(NSCHANNEL(This), riid, result);
1143 }
1144
1145 static nsrefcnt NSAPI nsUploadChannel_AddRef(nsIUploadChannel *iface)
1146 {
1147     nsChannel *This = NSUPCHANNEL_THIS(iface);
1148     return nsIChannel_AddRef(NSCHANNEL(This));
1149 }
1150
1151 static nsrefcnt NSAPI nsUploadChannel_Release(nsIUploadChannel *iface)
1152 {
1153     nsChannel *This = NSUPCHANNEL_THIS(iface);
1154     return nsIChannel_Release(NSCHANNEL(This));
1155 }
1156
1157 static nsresult NSAPI nsUploadChannel_SetUploadStream(nsIUploadChannel *iface,
1158         nsIInputStream *aStream, const nsACString *aContentType, PRInt32 aContentLength)
1159 {
1160     nsChannel *This = NSUPCHANNEL_THIS(iface);
1161     const char *content_type;
1162     nsresult nsres;
1163
1164     TRACE("(%p)->(%p %p %d)\n", This, aStream, aContentType, aContentLength);
1165
1166     if(This->post_data_stream)
1167         nsIInputStream_Release(This->post_data_stream);
1168
1169     if(aContentType) {
1170         nsACString_GetData(aContentType, &content_type);
1171         if(*content_type)
1172             FIXME("Unsupported aContentType argument: %s\n", debugstr_a(content_type));
1173     }
1174
1175     if(aContentLength != -1)
1176         FIXME("Unsupported acontentLength = %d\n", aContentLength);
1177
1178     if(This->post_data_stream)
1179         nsIInputStream_Release(This->post_data_stream);
1180     This->post_data_stream = aStream;
1181     if(aStream)
1182         nsIInputStream_AddRef(aStream);
1183
1184     if(This->post_data_stream) {
1185         nsIUploadChannel *upload_channel;
1186
1187         nsres = nsIChannel_QueryInterface(This->channel, &IID_nsIUploadChannel,
1188                 (void**)&upload_channel);
1189         if(NS_SUCCEEDED(nsres)) {
1190             nsres = nsIUploadChannel_SetUploadStream(upload_channel, aStream, aContentType, aContentLength);
1191             nsIUploadChannel_Release(upload_channel);
1192             if(NS_FAILED(nsres))
1193                 WARN("SetUploadStream failed: %08x\n", nsres);
1194
1195         }
1196     }
1197
1198     return NS_OK;
1199 }
1200
1201 static nsresult NSAPI nsUploadChannel_GetUploadStream(nsIUploadChannel *iface,
1202         nsIInputStream **aUploadStream)
1203 {
1204     nsChannel *This = NSUPCHANNEL_THIS(iface);
1205
1206     TRACE("(%p)->(%p)\n", This, aUploadStream);
1207
1208     if(This->post_data_stream)
1209         nsIInputStream_AddRef(This->post_data_stream);
1210
1211     *aUploadStream = This->post_data_stream;
1212     return NS_OK;
1213 }
1214
1215 #undef NSUPCHANNEL_THIS
1216
1217 static const nsIUploadChannelVtbl nsUploadChannelVtbl = {
1218     nsUploadChannel_QueryInterface,
1219     nsUploadChannel_AddRef,
1220     nsUploadChannel_Release,
1221     nsUploadChannel_SetUploadStream,
1222     nsUploadChannel_GetUploadStream
1223 };
1224
1225 #define NSURI_THIS(iface) DEFINE_THIS(nsURI, WineURI, iface)
1226
1227 static nsresult NSAPI nsURI_QueryInterface(nsIWineURI *iface, nsIIDRef riid, nsQIResult result)
1228 {
1229     nsURI *This = NSURI_THIS(iface);
1230
1231     *result = NULL;
1232
1233     if(IsEqualGUID(&IID_nsISupports, riid)) {
1234         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1235         *result = NSURI(This);
1236     }else if(IsEqualGUID(&IID_nsIURI, riid)) {
1237         TRACE("(%p)->(IID_nsIURI %p)\n", This, result);
1238         *result = NSURI(This);
1239     }else if(IsEqualGUID(&IID_nsIWineURI, riid)) {
1240         TRACE("(%p)->(IID_nsIWineURI %p)\n", This, result);
1241         *result = NSURI(This);
1242     }
1243
1244     if(*result) {
1245         nsIURI_AddRef(NSURI(This));
1246         return NS_OK;
1247     }
1248
1249     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1250     return This->uri ? nsIURI_QueryInterface(This->uri, riid, result) : NS_NOINTERFACE;
1251 }
1252
1253 static nsrefcnt NSAPI nsURI_AddRef(nsIWineURI *iface)
1254 {
1255     nsURI *This = NSURI_THIS(iface);
1256     LONG ref = InterlockedIncrement(&This->ref);
1257
1258     TRACE("(%p) ref=%d\n", This, ref);
1259
1260     return ref;
1261 }
1262
1263 static nsrefcnt NSAPI nsURI_Release(nsIWineURI *iface)
1264 {
1265     nsURI *This = NSURI_THIS(iface);
1266     LONG ref = InterlockedDecrement(&This->ref);
1267
1268     TRACE("(%p) ref=%d\n", This, ref);
1269
1270     if(!ref) {
1271         if(This->container)
1272             nsIWebBrowserChrome_Release(NSWBCHROME(This->container));
1273         if(This->uri)
1274             nsIURI_Release(This->uri);
1275         heap_free(This->wine_url);
1276         heap_free(This);
1277     }
1278
1279     return ref;
1280 }
1281
1282 static nsresult NSAPI nsURI_GetSpec(nsIWineURI *iface, nsACString *aSpec)
1283 {
1284     nsURI *This = NSURI_THIS(iface);
1285
1286     TRACE("(%p)->(%p)\n", This, aSpec);
1287
1288     if(This->use_wine_url) {
1289         char speca[INTERNET_MAX_URL_LENGTH] = "wine:";
1290         WideCharToMultiByte(CP_ACP, 0, This->wine_url, -1, speca+5, sizeof(speca)-5, NULL, NULL);
1291         nsACString_SetData(aSpec, speca);
1292
1293         return NS_OK;
1294     }
1295
1296     if(This->uri)
1297         return nsIURI_GetSpec(This->uri, aSpec);
1298
1299     TRACE("returning error\n");
1300     return NS_ERROR_NOT_IMPLEMENTED;
1301
1302 }
1303
1304 static nsresult NSAPI nsURI_SetSpec(nsIWineURI *iface, const nsACString *aSpec)
1305 {
1306     nsURI *This = NSURI_THIS(iface);
1307
1308     TRACE("(%p)->(%p)\n", This, aSpec);
1309
1310     if(This->uri)
1311         return nsIURI_SetSpec(This->uri, aSpec);
1312
1313     FIXME("default action not implemented\n");
1314     return NS_ERROR_NOT_IMPLEMENTED;
1315 }
1316
1317 static nsresult NSAPI nsURI_GetPrePath(nsIWineURI *iface, nsACString *aPrePath)
1318 {
1319     nsURI *This = NSURI_THIS(iface);
1320
1321     TRACE("(%p)->(%p)\n", This, aPrePath);
1322
1323     if(This->uri)
1324         return nsIURI_GetPrePath(This->uri, aPrePath);
1325
1326     FIXME("default action not implemented\n");
1327     return NS_ERROR_NOT_IMPLEMENTED;
1328 }
1329
1330 static nsresult NSAPI nsURI_GetScheme(nsIWineURI *iface, nsACString *aScheme)
1331 {
1332     nsURI *This = NSURI_THIS(iface);
1333
1334     TRACE("(%p)->(%p)\n", This, aScheme);
1335
1336     if(This->use_wine_url && strcmpW(This->wine_url, about_blankW)) {
1337         /*
1338          * For Gecko we set scheme to unknown so it won't be handled
1339          * as any special case.
1340          */
1341         nsACString_SetData(aScheme, "wine");
1342         return NS_OK;
1343     }
1344
1345     if(This->uri)
1346         return nsIURI_GetScheme(This->uri, aScheme);
1347
1348     TRACE("returning error\n");
1349     return NS_ERROR_NOT_IMPLEMENTED;
1350 }
1351
1352 static nsresult NSAPI nsURI_SetScheme(nsIWineURI *iface, const nsACString *aScheme)
1353 {
1354     nsURI *This = NSURI_THIS(iface);
1355
1356     TRACE("(%p)->(%p)\n", This, aScheme);
1357
1358     if(This->uri)
1359         return nsIURI_SetScheme(This->uri, aScheme);
1360
1361     FIXME("default action not implemented\n");
1362     return NS_ERROR_NOT_IMPLEMENTED;
1363 }
1364
1365 static nsresult NSAPI nsURI_GetUserPass(nsIWineURI *iface, nsACString *aUserPass)
1366 {
1367     nsURI *This = NSURI_THIS(iface);
1368
1369     TRACE("(%p)->(%p)\n", This, aUserPass);
1370
1371     if(This->uri)
1372         return nsIURI_GetUserPass(This->uri, aUserPass);
1373
1374     FIXME("default action not implemented\n");
1375     return NS_ERROR_NOT_IMPLEMENTED;
1376 }
1377
1378 static nsresult NSAPI nsURI_SetUserPass(nsIWineURI *iface, const nsACString *aUserPass)
1379 {
1380     nsURI *This = NSURI_THIS(iface);
1381
1382     TRACE("(%p)->(%p)\n", This, aUserPass);
1383
1384     if(This->uri)
1385         return nsIURI_SetUserPass(This->uri, aUserPass);
1386
1387     FIXME("default action not implemented\n");
1388     return NS_ERROR_NOT_IMPLEMENTED;
1389 }
1390
1391 static nsresult NSAPI nsURI_GetUsername(nsIWineURI *iface, nsACString *aUsername)
1392 {
1393     nsURI *This = NSURI_THIS(iface);
1394
1395     TRACE("(%p)->(%p)\n", This, aUsername);
1396
1397     if(This->uri)
1398         return nsIURI_GetUsername(This->uri, aUsername);
1399
1400     FIXME("default action not implemented\n");
1401     return NS_ERROR_NOT_IMPLEMENTED;
1402 }
1403
1404 static nsresult NSAPI nsURI_SetUsername(nsIWineURI *iface, const nsACString *aUsername)
1405 {
1406     nsURI *This = NSURI_THIS(iface);
1407
1408     TRACE("(%p)->(%p)\n", This, aUsername);
1409
1410     if(This->uri)
1411         return nsIURI_SetUsername(This->uri, aUsername);
1412
1413     FIXME("default action not implemented\n");
1414     return NS_ERROR_NOT_IMPLEMENTED;
1415 }
1416
1417 static nsresult NSAPI nsURI_GetPassword(nsIWineURI *iface, nsACString *aPassword)
1418 {
1419     nsURI *This = NSURI_THIS(iface);
1420
1421     TRACE("(%p)->(%p)\n", This, aPassword);
1422
1423     if(This->uri)
1424         return nsIURI_GetPassword(This->uri, aPassword);
1425
1426     FIXME("default action not implemented\n");
1427     return NS_ERROR_NOT_IMPLEMENTED;
1428 }
1429
1430 static nsresult NSAPI nsURI_SetPassword(nsIWineURI *iface, const nsACString *aPassword)
1431 {
1432     nsURI *This = NSURI_THIS(iface);
1433
1434     TRACE("(%p)->(%p)\n", This, aPassword);
1435
1436     if(This->uri)
1437         return nsIURI_SetPassword(This->uri, aPassword);
1438
1439     FIXME("default action not implemented\n");
1440     return NS_ERROR_NOT_IMPLEMENTED;
1441 }
1442
1443 static nsresult NSAPI nsURI_GetHostPort(nsIWineURI *iface, nsACString *aHostPort)
1444 {
1445     nsURI *This = NSURI_THIS(iface);
1446
1447     TRACE("(%p)->(%p)\n", This, aHostPort);
1448
1449     if(This->uri)
1450         return nsIURI_GetHostPort(This->uri, aHostPort);
1451
1452     FIXME("default action not implemented\n");
1453     return NS_ERROR_NOT_IMPLEMENTED;
1454 }
1455
1456 static nsresult NSAPI nsURI_SetHostPort(nsIWineURI *iface, const nsACString *aHostPort)
1457 {
1458     nsURI *This = NSURI_THIS(iface);
1459
1460     TRACE("(%p)->(%p)\n", This, aHostPort);
1461
1462     if(This->uri)
1463         return nsIURI_SetHostPort(This->uri, aHostPort);
1464
1465     FIXME("default action not implemented\n");
1466     return NS_ERROR_NOT_IMPLEMENTED;
1467 }
1468
1469 static nsresult NSAPI nsURI_GetHost(nsIWineURI *iface, nsACString *aHost)
1470 {
1471     nsURI *This = NSURI_THIS(iface);
1472
1473     TRACE("(%p)->(%p)\n", This, aHost);
1474
1475     if(This->uri)
1476         return nsIURI_GetHost(This->uri, aHost);
1477
1478     FIXME("default action not implemented\n");
1479     return NS_ERROR_NOT_IMPLEMENTED;
1480 }
1481
1482 static nsresult NSAPI nsURI_SetHost(nsIWineURI *iface, const nsACString *aHost)
1483 {
1484     nsURI *This = NSURI_THIS(iface);
1485
1486     TRACE("(%p)->(%p)\n", This, aHost);
1487
1488     if(This->uri)
1489         return nsIURI_SetHost(This->uri, aHost);
1490
1491     FIXME("default action not implemented\n");
1492     return NS_ERROR_NOT_IMPLEMENTED;
1493 }
1494
1495 static nsresult NSAPI nsURI_GetPort(nsIWineURI *iface, PRInt32 *aPort)
1496 {
1497     nsURI *This = NSURI_THIS(iface);
1498
1499     TRACE("(%p)->(%p)\n", This, aPort);
1500
1501     if(This->uri)
1502         return nsIURI_GetPort(This->uri, aPort);
1503
1504     FIXME("default action not implemented\n");
1505     return NS_ERROR_NOT_IMPLEMENTED;
1506 }
1507
1508 static nsresult NSAPI nsURI_SetPort(nsIWineURI *iface, PRInt32 aPort)
1509 {
1510     nsURI *This = NSURI_THIS(iface);
1511
1512     TRACE("(%p)->(%d)\n", This, aPort);
1513
1514     if(This->uri)
1515         return nsIURI_SetPort(This->uri, aPort);
1516
1517     FIXME("default action not implemented\n");
1518     return NS_ERROR_NOT_IMPLEMENTED;
1519 }
1520
1521 static nsresult NSAPI nsURI_GetPath(nsIWineURI *iface, nsACString *aPath)
1522 {
1523     nsURI *This = NSURI_THIS(iface);
1524
1525     TRACE("(%p)->(%p)\n", This, aPath);
1526
1527     if(This->uri)
1528         return nsIURI_GetPath(This->uri, aPath);
1529
1530     FIXME("default action not implemented\n");
1531     return NS_ERROR_NOT_IMPLEMENTED;
1532 }
1533
1534 static nsresult NSAPI nsURI_SetPath(nsIWineURI *iface, const nsACString *aPath)
1535 {
1536     nsURI *This = NSURI_THIS(iface);
1537     const char *path;
1538
1539     nsACString_GetData(aPath, &path);
1540     TRACE("(%p)->(%p(%s))\n", This, aPath, debugstr_a(path));
1541
1542
1543     if(This->wine_url) {
1544         WCHAR new_url[INTERNET_MAX_URL_LENGTH];
1545         DWORD size = sizeof(new_url)/sizeof(WCHAR);
1546         LPWSTR pathw;
1547         HRESULT hres;
1548
1549         pathw = heap_strdupAtoW(path);
1550         hres = UrlCombineW(This->wine_url, pathw, new_url, &size, 0);
1551         heap_free(pathw);
1552         if(SUCCEEDED(hres))
1553             nsIWineURI_SetWineURL(NSWINEURI(This), new_url);
1554         else
1555             WARN("UrlCombine failed: %08x\n", hres);
1556     }
1557
1558     if(!This->uri)
1559         return NS_OK;
1560
1561     return nsIURI_SetPath(This->uri, aPath);
1562 }
1563
1564 static nsresult NSAPI nsURI_Equals(nsIWineURI *iface, nsIURI *other, PRBool *_retval)
1565 {
1566     nsURI *This = NSURI_THIS(iface);
1567     nsIWineURI *wine_uri;
1568     LPCWSTR other_url = NULL;
1569     nsresult nsres;
1570
1571     TRACE("(%p)->(%p %p)\n", This, other, _retval);
1572
1573     if(This->uri)
1574         return nsIURI_Equals(This->uri, other, _retval);
1575
1576     nsres = nsIURI_QueryInterface(other, &IID_nsIWineURI, (void**)&wine_uri);
1577     if(NS_FAILED(nsres)) {
1578         TRACE("Could not get nsIWineURI interface\n");
1579         *_retval = FALSE;
1580         return NS_OK;
1581     }
1582
1583     nsIWineURI_GetWineURL(wine_uri, &other_url);
1584     *_retval = !UrlCompareW(This->wine_url, other_url, TRUE);
1585     nsIWineURI_Release(wine_uri);
1586
1587     return NS_OK;
1588 }
1589
1590 static nsresult NSAPI nsURI_SchemeIs(nsIWineURI *iface, const char *scheme, PRBool *_retval)
1591 {
1592     nsURI *This = NSURI_THIS(iface);
1593
1594     TRACE("(%p)->(%s %p)\n", This, debugstr_a(scheme), _retval);
1595
1596     if(This->use_wine_url) {
1597         WCHAR buf[INTERNET_MAX_SCHEME_LENGTH];
1598         int len = MultiByteToWideChar(CP_ACP, 0, scheme, -1, buf, sizeof(buf)/sizeof(WCHAR))-1;
1599
1600         *_retval = lstrlenW(This->wine_url) > len
1601             && This->wine_url[len] == ':'
1602             && !memcmp(buf, This->wine_url, len*sizeof(WCHAR));
1603         return NS_OK;
1604     }
1605
1606     if(This->uri)
1607         return nsIURI_SchemeIs(This->uri, scheme, _retval);
1608
1609     TRACE("returning error\n");
1610     return NS_ERROR_NOT_IMPLEMENTED;
1611 }
1612
1613 static nsresult NSAPI nsURI_Clone(nsIWineURI *iface, nsIURI **_retval)
1614 {
1615     nsURI *This = NSURI_THIS(iface);
1616     nsIURI *nsuri = NULL;
1617     nsIWineURI *wine_uri;
1618     nsresult nsres;
1619
1620     TRACE("(%p)->(%p)\n", This, _retval);
1621
1622     if(This->uri) {
1623         nsres = nsIURI_Clone(This->uri, &nsuri);
1624         if(NS_FAILED(nsres)) {
1625             WARN("Clone failed: %08x\n", nsres);
1626             return nsres;
1627         }
1628     }
1629
1630     nsres = create_uri(nsuri, This->container, &wine_uri);
1631     if(NS_FAILED(nsres)) {
1632         WARN("create_uri failed: %08x\n", nsres);
1633         return nsres;
1634     }
1635
1636     *_retval = (nsIURI*)wine_uri;
1637     return nsIWineURI_SetWineURL(wine_uri, This->wine_url);
1638 }
1639
1640 static nsresult NSAPI nsURI_Resolve(nsIWineURI *iface, const nsACString *arelativePath,
1641         nsACString *_retval)
1642 {
1643     nsURI *This = NSURI_THIS(iface);
1644
1645     TRACE("(%p)->(%p %p)\n", This, arelativePath, _retval);
1646
1647     if(This->uri)
1648         return nsIURI_Resolve(This->uri, arelativePath, _retval);
1649
1650     FIXME("default action not implemented\n");
1651     return NS_ERROR_NOT_IMPLEMENTED;
1652 }
1653
1654 static nsresult NSAPI nsURI_GetAsciiSpec(nsIWineURI *iface, nsACString *aAsciiSpec)
1655 {
1656     nsURI *This = NSURI_THIS(iface);
1657
1658     TRACE("(%p)->(%p)\n", This, aAsciiSpec);
1659
1660     if(This->use_wine_url)
1661         return nsIURI_GetSpec(NSURI(This), aAsciiSpec);
1662
1663     if(This->uri)
1664         return nsIURI_GetAsciiSpec(This->uri, aAsciiSpec);
1665
1666     TRACE("returning error\n");
1667     return NS_ERROR_NOT_IMPLEMENTED;
1668 }
1669
1670 static nsresult NSAPI nsURI_GetAsciiHost(nsIWineURI *iface, nsACString *aAsciiHost)
1671 {
1672     nsURI *This = NSURI_THIS(iface);
1673
1674     TRACE("(%p)->(%p)\n", This, aAsciiHost);
1675
1676     if(This->uri)
1677         return nsIURI_GetAsciiHost(This->uri, aAsciiHost);
1678
1679     FIXME("default action not implemented\n");
1680     return NS_ERROR_NOT_IMPLEMENTED;
1681 }
1682
1683 static nsresult NSAPI nsURI_GetOriginCharset(nsIWineURI *iface, nsACString *aOriginCharset)
1684 {
1685     nsURI *This = NSURI_THIS(iface);
1686
1687     TRACE("(%p)->(%p)\n", This, aOriginCharset);
1688
1689     if(This->uri)
1690         return nsIURI_GetOriginCharset(This->uri, aOriginCharset);
1691
1692     FIXME("default action not implemented\n");
1693     return NS_ERROR_NOT_IMPLEMENTED;
1694 }
1695
1696 static nsresult NSAPI nsURI_GetNSContainer(nsIWineURI *iface, NSContainer **aContainer)
1697 {
1698     nsURI *This = NSURI_THIS(iface);
1699
1700     TRACE("(%p)->(%p)\n", This, aContainer);
1701
1702     if(This->container)
1703         nsIWebBrowserChrome_AddRef(NSWBCHROME(This->container));
1704     *aContainer = This->container;
1705
1706     return NS_OK;
1707 }
1708
1709 static nsresult NSAPI nsURI_SetNSContainer(nsIWineURI *iface, NSContainer *aContainer)
1710 {
1711     nsURI *This = NSURI_THIS(iface);
1712
1713     TRACE("(%p)->(%p)\n", This, aContainer);
1714
1715     if(This->container) {
1716         if(This->container == aContainer)
1717             return NS_OK;
1718         TRACE("Changing %p -> %p\n", This->container, aContainer);
1719         nsIWebBrowserChrome_Release(NSWBCHROME(This->container));
1720     }
1721
1722     if(aContainer)
1723         nsIWebBrowserChrome_AddRef(NSWBCHROME(aContainer));
1724     This->container = aContainer;
1725
1726     return NS_OK;
1727 }
1728
1729 static nsresult NSAPI nsURI_GetIsDocumentURI(nsIWineURI *iface, PRBool *aIsDocumentURI)
1730 {
1731     nsURI *This = NSURI_THIS(iface);
1732
1733     TRACE("(%p)->(%p)\n", This, aIsDocumentURI);
1734
1735     *aIsDocumentURI = This->is_doc_uri;
1736     return NS_OK;
1737 }
1738
1739 static nsresult NSAPI nsURI_SetIsDocumentURI(nsIWineURI *iface, PRBool aIsDocumentURI)
1740 {
1741     nsURI *This = NSURI_THIS(iface);
1742
1743     TRACE("(%p)->(%x)\n", This, aIsDocumentURI);
1744
1745     This->is_doc_uri = aIsDocumentURI;
1746     return NS_OK;
1747 }
1748
1749 static nsresult NSAPI nsURI_GetWineURL(nsIWineURI *iface, LPCWSTR *aURL)
1750 {
1751     nsURI *This = NSURI_THIS(iface);
1752
1753     TRACE("(%p)->(%p)\n", This, aURL);
1754
1755     *aURL = This->wine_url;
1756     return NS_OK;
1757 }
1758
1759 static nsresult NSAPI nsURI_SetWineURL(nsIWineURI *iface, LPCWSTR aURL)
1760 {
1761     nsURI *This = NSURI_THIS(iface);
1762
1763     static const WCHAR wszFtp[]   = {'f','t','p',':'};
1764     static const WCHAR wszHttp[]  = {'h','t','t','p',':'};
1765     static const WCHAR wszHttps[] = {'h','t','t','p','s',':'};
1766
1767     TRACE("(%p)->(%s)\n", This, debugstr_w(aURL));
1768
1769     heap_free(This->wine_url);
1770
1771     if(aURL) {
1772         int len = strlenW(aURL)+1;
1773         This->wine_url = heap_alloc(len*sizeof(WCHAR));
1774         memcpy(This->wine_url, aURL, len*sizeof(WCHAR));
1775
1776         /* FIXME: Always use wine url */
1777         This->use_wine_url =
1778                strncmpW(aURL, wszFtp,   sizeof(wszFtp)/sizeof(WCHAR))
1779             && strncmpW(aURL, wszHttp,  sizeof(wszHttp)/sizeof(WCHAR))
1780             && strncmpW(aURL, wszHttps, sizeof(wszHttps)/sizeof(WCHAR));
1781     }else {
1782         This->wine_url = NULL;
1783         This->use_wine_url = FALSE;
1784     }
1785
1786     return NS_OK;
1787 }
1788
1789 #undef NSURI_THIS
1790
1791 static const nsIWineURIVtbl nsWineURIVtbl = {
1792     nsURI_QueryInterface,
1793     nsURI_AddRef,
1794     nsURI_Release,
1795     nsURI_GetSpec,
1796     nsURI_SetSpec,
1797     nsURI_GetPrePath,
1798     nsURI_GetScheme,
1799     nsURI_SetScheme,
1800     nsURI_GetUserPass,
1801     nsURI_SetUserPass,
1802     nsURI_GetUsername,
1803     nsURI_SetUsername,
1804     nsURI_GetPassword,
1805     nsURI_SetPassword,
1806     nsURI_GetHostPort,
1807     nsURI_SetHostPort,
1808     nsURI_GetHost,
1809     nsURI_SetHost,
1810     nsURI_GetPort,
1811     nsURI_SetPort,
1812     nsURI_GetPath,
1813     nsURI_SetPath,
1814     nsURI_Equals,
1815     nsURI_SchemeIs,
1816     nsURI_Clone,
1817     nsURI_Resolve,
1818     nsURI_GetAsciiSpec,
1819     nsURI_GetAsciiHost,
1820     nsURI_GetOriginCharset,
1821     nsURI_GetNSContainer,
1822     nsURI_SetNSContainer,
1823     nsURI_GetIsDocumentURI,
1824     nsURI_SetIsDocumentURI,
1825     nsURI_GetWineURL,
1826     nsURI_SetWineURL
1827 };
1828
1829 static nsresult create_uri(nsIURI *uri, NSContainer *container, nsIWineURI **_retval)
1830 {
1831     nsURI *ret = heap_alloc(sizeof(nsURI));
1832
1833     ret->lpWineURIVtbl = &nsWineURIVtbl;
1834     ret->ref = 1;
1835     ret->uri = uri;
1836     ret->container = container;
1837     ret->wine_url = NULL;
1838     ret->is_doc_uri = FALSE;
1839     ret->use_wine_url = FALSE;
1840
1841     if(container)
1842         nsIWebBrowserChrome_AddRef(NSWBCHROME(container));
1843
1844     TRACE("retval=%p\n", ret);
1845     *_retval = NSWINEURI(ret);
1846     return NS_OK;
1847 }
1848
1849 typedef struct {
1850     const nsIProtocolHandlerVtbl  *lpProtocolHandlerVtbl;
1851
1852     LONG ref;
1853
1854     nsIProtocolHandler *nshandler;
1855 } nsProtocolHandler;
1856
1857 #define NSPROTHANDLER(x)  ((nsIProtocolHandler*)  &(x)->lpProtocolHandlerVtbl)
1858
1859 #define NSPROTHANDLER_THIS(iface) DEFINE_THIS(nsProtocolHandler, ProtocolHandler, iface)
1860
1861 static nsresult NSAPI nsProtocolHandler_QueryInterface(nsIProtocolHandler *iface, nsIIDRef riid,
1862         nsQIResult result)
1863 {
1864     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1865
1866     *result = NULL;
1867
1868     if(IsEqualGUID(&IID_nsISupports, riid)) {
1869         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1870         *result = NSPROTHANDLER(This);
1871     }else if(IsEqualGUID(&IID_nsIProtocolHandler, riid)) {
1872         TRACE("(%p)->(IID_nsIProtocolHandler %p)\n", This, result);
1873         *result = NSPROTHANDLER(This);
1874     }else if(IsEqualGUID(&IID_nsIExternalProtocolHandler, riid)) {
1875         TRACE("(%p)->(IID_nsIExternalProtocolHandler %p), returning NULL\n", This, result);
1876         return NS_NOINTERFACE;
1877     }
1878
1879     if(*result) {
1880         nsISupports_AddRef((nsISupports*)*result);
1881         return NS_OK;
1882     }
1883
1884     WARN("(%s %p)\n", debugstr_guid(riid), result);
1885     return NS_NOINTERFACE;
1886 }
1887
1888 static nsrefcnt NSAPI nsProtocolHandler_AddRef(nsIProtocolHandler *iface)
1889 {
1890     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1891     LONG ref = InterlockedIncrement(&This->ref);
1892
1893     TRACE("(%p) ref=%d\n", This, ref);
1894
1895     return ref;
1896 }
1897
1898 static nsrefcnt NSAPI nsProtocolHandler_Release(nsIProtocolHandler *iface)
1899 {
1900     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1901     LONG ref = InterlockedDecrement(&This->ref);
1902
1903     TRACE("(%p) ref=%d\n", This, ref);
1904
1905     if(!ref) {
1906         if(This->nshandler)
1907             nsIProtocolHandler_Release(This->nshandler);
1908         heap_free(This);
1909     }
1910
1911     return ref;
1912 }
1913
1914 static nsresult NSAPI nsProtocolHandler_GetScheme(nsIProtocolHandler *iface, nsACString *aScheme)
1915 {
1916     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1917
1918     TRACE("(%p)->(%p)\n", This, aScheme);
1919
1920     if(This->nshandler)
1921         return nsIProtocolHandler_GetScheme(This->nshandler, aScheme);
1922     return NS_ERROR_NOT_IMPLEMENTED;
1923 }
1924
1925 static nsresult NSAPI nsProtocolHandler_GetDefaultPort(nsIProtocolHandler *iface,
1926         PRInt32 *aDefaultPort)
1927 {
1928     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1929
1930     TRACE("(%p)->(%p)\n", This, aDefaultPort);
1931
1932     if(This->nshandler)
1933         return nsIProtocolHandler_GetDefaultPort(This->nshandler, aDefaultPort);
1934     return NS_ERROR_NOT_IMPLEMENTED;
1935 }
1936
1937 static nsresult NSAPI nsProtocolHandler_GetProtocolFlags(nsIProtocolHandler *iface,
1938                                                          PRUint32 *aProtocolFlags)
1939 {
1940     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1941
1942     TRACE("(%p)->(%p)\n", This, aProtocolFlags);
1943
1944     if(This->nshandler)
1945         return nsIProtocolHandler_GetProtocolFlags(This->nshandler, aProtocolFlags);
1946     return NS_ERROR_NOT_IMPLEMENTED;
1947 }
1948
1949 static nsresult NSAPI nsProtocolHandler_NewURI(nsIProtocolHandler *iface,
1950         const nsACString *aSpec, const char *aOriginCharset, nsIURI *aBaseURI, nsIURI **_retval)
1951 {
1952     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1953
1954     TRACE("((%p)->%p %s %p %p)\n", This, aSpec, debugstr_a(aOriginCharset), aBaseURI, _retval);
1955
1956     if(This->nshandler)
1957         return nsIProtocolHandler_NewURI(This->nshandler, aSpec, aOriginCharset, aBaseURI, _retval);
1958     return NS_ERROR_NOT_IMPLEMENTED;
1959 }
1960
1961 static nsresult NSAPI nsProtocolHandler_NewChannel(nsIProtocolHandler *iface,
1962         nsIURI *aURI, nsIChannel **_retval)
1963 {
1964     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1965
1966     TRACE("(%p)->(%p %p)\n", This, aURI, _retval);
1967
1968     if(This->nshandler)
1969         return nsIProtocolHandler_NewChannel(This->nshandler, aURI, _retval);
1970     return NS_ERROR_NOT_IMPLEMENTED;
1971 }
1972
1973 static nsresult NSAPI nsProtocolHandler_AllowPort(nsIProtocolHandler *iface,
1974         PRInt32 port, const char *scheme, PRBool *_retval)
1975 {
1976     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1977
1978     TRACE("(%p)->(%d %s %p)\n", This, port, debugstr_a(scheme), _retval);
1979
1980     if(This->nshandler)
1981         return nsIProtocolHandler_AllowPort(This->nshandler, port, scheme, _retval);
1982     return NS_ERROR_NOT_IMPLEMENTED;
1983 }
1984
1985 #undef NSPROTHANDLER_THIS
1986
1987 static const nsIProtocolHandlerVtbl nsProtocolHandlerVtbl = {
1988     nsProtocolHandler_QueryInterface,
1989     nsProtocolHandler_AddRef,
1990     nsProtocolHandler_Release,
1991     nsProtocolHandler_GetScheme,
1992     nsProtocolHandler_GetDefaultPort,
1993     nsProtocolHandler_GetProtocolFlags,
1994     nsProtocolHandler_NewURI,
1995     nsProtocolHandler_NewChannel,
1996     nsProtocolHandler_AllowPort
1997 };
1998
1999 static nsIProtocolHandler *create_protocol_handler(nsIProtocolHandler *nshandler)
2000 {
2001     nsProtocolHandler *ret = heap_alloc(sizeof(nsProtocolHandler));
2002
2003     ret->lpProtocolHandlerVtbl = &nsProtocolHandlerVtbl;
2004     ret->ref = 1;
2005     ret->nshandler = nshandler;
2006
2007     return NSPROTHANDLER(ret);
2008 }
2009
2010 static nsresult NSAPI nsIOService_QueryInterface(nsIIOService*,nsIIDRef,nsQIResult);
2011
2012 static nsrefcnt NSAPI nsIOService_AddRef(nsIIOService *iface)
2013 {
2014     return 2;
2015 }
2016
2017 static nsrefcnt NSAPI nsIOService_Release(nsIIOService *iface)
2018 {
2019     return 1;
2020 }
2021
2022 static nsresult NSAPI nsIOService_GetProtocolHandler(nsIIOService *iface, const char *aScheme,
2023                                                      nsIProtocolHandler **_retval)
2024 {
2025     nsIExternalProtocolHandler *nsexthandler;
2026     nsIProtocolHandler *nshandler;
2027     nsresult nsres;
2028
2029     TRACE("(%s %p)\n", debugstr_a(aScheme), _retval);
2030
2031     nsres = nsIIOService_GetProtocolHandler(nsio, aScheme, &nshandler);
2032     if(NS_FAILED(nsres)) {
2033         WARN("GetProtocolHandler failed: %08x\n", nsres);
2034         return nsres;
2035     }
2036
2037     nsres = nsIProtocolHandler_QueryInterface(nshandler, &IID_nsIExternalProtocolHandler,
2038                                               (void**)&nsexthandler);
2039     if(NS_FAILED(nsres)) {
2040         *_retval = nshandler;
2041         return NS_OK;
2042     }
2043
2044     nsIExternalProtocolHandler_Release(nsexthandler);
2045     *_retval = create_protocol_handler(nshandler);
2046     TRACE("return %p\n", *_retval);
2047     return NS_OK;
2048 }
2049
2050 static nsresult NSAPI nsIOService_GetProtocolFlags(nsIIOService *iface, const char *aScheme,
2051                                                     PRUint32 *_retval)
2052 {
2053     TRACE("(%s %p)\n", debugstr_a(aScheme), _retval);
2054     return nsIIOService_GetProtocolFlags(nsio, aScheme, _retval);
2055 }
2056
2057 static BOOL is_gecko_special_uri(const char *spec)
2058 {
2059     static const char chromeW[] = "chrome:";
2060     static const char jarW[] = "jar:";
2061     static const char resourceW[] = "resource:";
2062     static const char javascriptW[] = "javascript:";
2063
2064     return !strncasecmp(spec, chromeW,     sizeof(chromeW)-1)
2065         || !strncasecmp(spec, resourceW,   sizeof(resourceW)-1)
2066         || !strncasecmp(spec, jarW,        sizeof(jarW)-1)
2067         || !strncasecmp(spec, javascriptW, sizeof(javascriptW)-1);
2068 }
2069
2070 static nsresult NSAPI nsIOService_NewURI(nsIIOService *iface, const nsACString *aSpec,
2071         const char *aOriginCharset, nsIURI *aBaseURI, nsIURI **_retval)
2072 {
2073     const char *spec = NULL;
2074     NSContainer *nscontainer = NULL;
2075     nsIURI *uri = NULL;
2076     LPCWSTR base_wine_url = NULL;
2077     nsIWineURI *base_wine_uri = NULL, *wine_uri;
2078     BOOL is_wine_uri = FALSE;
2079     nsresult nsres;
2080
2081     nsACString_GetData(aSpec, &spec);
2082
2083     TRACE("(%p(%s) %s %p %p)\n", aSpec, debugstr_a(spec), debugstr_a(aOriginCharset),
2084           aBaseURI, _retval);
2085
2086     if(is_gecko_special_uri(spec))
2087         return nsIIOService_NewURI(nsio, aSpec, aOriginCharset, aBaseURI, _retval);
2088
2089     if(!strncmp(spec, "wine:", 5)) {
2090         spec += 5;
2091         is_wine_uri = TRUE;
2092     }
2093
2094     if(aBaseURI) {
2095         nsACString base_uri_str;
2096         const char *base_uri = NULL;
2097
2098         nsACString_Init(&base_uri_str, NULL);
2099
2100         nsres = nsIURI_GetSpec(aBaseURI, &base_uri_str);
2101         if(NS_SUCCEEDED(nsres)) {
2102             nsACString_GetData(&base_uri_str, &base_uri);
2103             TRACE("base_uri=%s\n", debugstr_a(base_uri));
2104         }else {
2105             ERR("GetSpec failed: %08x\n", nsres);
2106         }
2107
2108         nsACString_Finish(&base_uri_str);
2109     }
2110
2111     nsres = nsIIOService_NewURI(nsio, aSpec, aOriginCharset, aBaseURI, &uri);
2112     if(NS_FAILED(nsres))
2113         TRACE("NewURI failed: %08x\n", nsres);
2114
2115     if(aBaseURI) {
2116         nsres = nsIURI_QueryInterface(aBaseURI, &IID_nsIWineURI, (void**)&base_wine_uri);
2117         if(NS_SUCCEEDED(nsres)) {
2118             nsIWineURI_GetNSContainer(base_wine_uri, &nscontainer);
2119             nsIWineURI_GetWineURL(base_wine_uri, &base_wine_url);
2120         }else {
2121             TRACE("Could not get base nsIWineURI: %08x\n", nsres);
2122         }
2123     }
2124
2125     TRACE("nscontainer = %p\n", nscontainer);
2126
2127     nsres = create_uri(uri, nscontainer, &wine_uri);
2128     *_retval = (nsIURI*)wine_uri;
2129
2130     if(nscontainer)
2131         nsIWebBrowserChrome_Release(NSWBCHROME(nscontainer));
2132
2133     if(base_wine_url) {
2134         WCHAR url[INTERNET_MAX_URL_LENGTH], rel_url[INTERNET_MAX_URL_LENGTH];
2135         DWORD len;
2136         HRESULT hres;
2137
2138         MultiByteToWideChar(CP_ACP, 0, spec, -1, rel_url, sizeof(rel_url)/sizeof(WCHAR));
2139
2140         hres = CoInternetCombineUrl(base_wine_url, rel_url,
2141                                     URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO,
2142                                     url, sizeof(url)/sizeof(WCHAR), &len, 0);
2143         if(SUCCEEDED(hres))
2144             nsIWineURI_SetWineURL(wine_uri, url);
2145         else
2146              WARN("CoCombineUrl failed: %08x\n", hres);
2147     }else if(is_wine_uri) {
2148         WCHAR url[INTERNET_MAX_URL_LENGTH];
2149
2150         MultiByteToWideChar(CP_ACP, 0, spec, -1, url, sizeof(url)/sizeof(WCHAR));
2151         nsIWineURI_SetWineURL(wine_uri, url);
2152     }
2153
2154     if(base_wine_uri)
2155         nsIWineURI_Release(base_wine_uri);
2156
2157     return nsres;
2158 }
2159
2160 static nsresult NSAPI nsIOService_NewFileURI(nsIIOService *iface, nsIFile *aFile,
2161                                              nsIURI **_retval)
2162 {
2163     TRACE("(%p %p)\n", aFile, _retval);
2164     return nsIIOService_NewFileURI(nsio, aFile, _retval);
2165 }
2166
2167 static nsresult NSAPI nsIOService_NewChannelFromURI(nsIIOService *iface, nsIURI *aURI,
2168                                                      nsIChannel **_retval)
2169 {
2170     nsIChannel *channel = NULL;
2171     nsChannel *ret;
2172     nsIWineURI *wine_uri;
2173     nsresult nsres;
2174
2175     TRACE("(%p %p)\n", aURI, _retval);
2176
2177     nsres = nsIIOService_NewChannelFromURI(nsio, aURI, &channel);
2178     if(NS_FAILED(nsres) && nsres != NS_ERROR_UNKNOWN_PROTOCOL) {
2179         WARN("NewChannelFromURI failed: %08x\n", nsres);
2180         *_retval = channel;
2181         return nsres;
2182     }
2183
2184     nsres = nsIURI_QueryInterface(aURI, &IID_nsIWineURI, (void**)&wine_uri);
2185     if(NS_FAILED(nsres)) {
2186         WARN("Could not get nsIWineURI: %08x\n", nsres);
2187         *_retval = channel;
2188         return channel ? NS_OK : NS_ERROR_UNEXPECTED;
2189     }
2190
2191     ret = heap_alloc_zero(sizeof(nsChannel));
2192
2193     ret->lpHttpChannelVtbl = &nsChannelVtbl;
2194     ret->lpUploadChannelVtbl = &nsUploadChannelVtbl;
2195     ret->ref = 1;
2196     ret->channel = channel;
2197     ret->uri = wine_uri;
2198
2199     nsIURI_AddRef(aURI);
2200     ret->original_uri = aURI;
2201
2202     if(channel)
2203         nsIChannel_QueryInterface(channel, &IID_nsIHttpChannel, (void**)&ret->http_channel);
2204
2205     *_retval = NSCHANNEL(ret);
2206     return NS_OK;
2207 }
2208
2209 static nsresult NSAPI nsIOService_NewChannel(nsIIOService *iface, const nsACString *aSpec,
2210         const char *aOriginCharset, nsIURI *aBaseURI, nsIChannel **_retval)
2211 {
2212     TRACE("(%p %s %p %p)\n", aSpec, debugstr_a(aOriginCharset), aBaseURI, _retval);
2213     return nsIIOService_NewChannel(nsio, aSpec, aOriginCharset, aBaseURI, _retval);
2214 }
2215
2216 static nsresult NSAPI nsIOService_GetOffline(nsIIOService *iface, PRBool *aOffline)
2217 {
2218     TRACE("(%p)\n", aOffline);
2219     return nsIIOService_GetOffline(nsio, aOffline);
2220 }
2221
2222 static nsresult NSAPI nsIOService_SetOffline(nsIIOService *iface, PRBool aOffline)
2223 {
2224     TRACE("(%x)\n", aOffline);
2225     return nsIIOService_SetOffline(nsio, aOffline);
2226 }
2227
2228 static nsresult NSAPI nsIOService_AllowPort(nsIIOService *iface, PRInt32 aPort,
2229                                              const char *aScheme, PRBool *_retval)
2230 {
2231     TRACE("(%d %s %p)\n", aPort, debugstr_a(aScheme), _retval);
2232     return nsIIOService_AllowPort(nsio, aPort, debugstr_a(aScheme), _retval);
2233 }
2234
2235 static nsresult NSAPI nsIOService_ExtractScheme(nsIIOService *iface, const nsACString *urlString,
2236                                                  nsACString * _retval)
2237 {
2238     TRACE("(%p %p)\n", urlString, _retval);
2239     return nsIIOService_ExtractScheme(nsio, urlString, _retval);
2240 }
2241
2242 static const nsIIOServiceVtbl nsIOServiceVtbl = {
2243     nsIOService_QueryInterface,
2244     nsIOService_AddRef,
2245     nsIOService_Release,
2246     nsIOService_GetProtocolHandler,
2247     nsIOService_GetProtocolFlags,
2248     nsIOService_NewURI,
2249     nsIOService_NewFileURI,
2250     nsIOService_NewChannelFromURI,
2251     nsIOService_NewChannel,
2252     nsIOService_GetOffline,
2253     nsIOService_SetOffline,
2254     nsIOService_AllowPort,
2255     nsIOService_ExtractScheme
2256 };
2257
2258 static nsIIOService nsIOService = { &nsIOServiceVtbl };
2259
2260 static nsresult NSAPI nsNetUtil_QueryInterface(nsINetUtil *iface, nsIIDRef riid,
2261                                                nsQIResult result)
2262 {
2263     return nsIIOService_QueryInterface(&nsIOService, riid, result);
2264 }
2265
2266 static nsrefcnt NSAPI nsNetUtil_AddRef(nsINetUtil *iface)
2267 {
2268     return 2;
2269 }
2270
2271 static nsrefcnt NSAPI nsNetUtil_Release(nsINetUtil *iface)
2272 {
2273     return 1;
2274 }
2275
2276 static nsresult NSAPI nsNetUtil_ParseContentType(nsINetUtil *iface, const nsACString *aTypeHeader,
2277         nsACString *aCharset, PRBool *aHadCharset, nsACString *aContentType)
2278 {
2279     TRACE("(%p %p %p %p)\n", aTypeHeader, aCharset, aHadCharset, aContentType);
2280
2281     return nsINetUtil_ParseContentType(net_util, aTypeHeader, aCharset, aHadCharset, aContentType);
2282 }
2283
2284 static nsresult NSAPI nsNetUtil_ProtocolHasFlags(nsINetUtil *iface, nsIURI *aURI, PRUint32 aFlags, PRBool *_retval)
2285 {
2286     TRACE("()\n");
2287
2288     return nsINetUtil_ProtocolHasFlags(net_util, aURI, aFlags, _retval);
2289 }
2290
2291 static nsresult NSAPI nsNetUtil_URIChainHasFlags(nsINetUtil *iface, nsIURI *aURI, PRUint32 aFlags, PRBool *_retval)
2292 {
2293     TRACE("(%p %08x %p)\n", aURI, aFlags, _retval);
2294
2295     if(aFlags == (1<<11)) {
2296         *_retval = FALSE;
2297         return NS_OK;
2298     }
2299
2300     return nsINetUtil_URIChainHasFlags(net_util, aURI, aFlags, _retval);
2301 }
2302
2303 static nsresult NSAPI nsNetUtil_ToImmutableURI(nsINetUtil *iface, nsIURI *aURI, nsIURI **_retval)
2304 {
2305     TRACE("(%p %p)\n", aURI, _retval);
2306
2307     return nsINetUtil_ToImmutableURI(net_util, aURI, _retval);
2308 }
2309
2310 static nsresult NSAPI nsNetUtil_EscapeString(nsINetUtil *iface, const nsACString *aString,
2311                                              PRUint32 aEscapeType, nsACString *_retval)
2312 {
2313     TRACE("(%p %x %p)\n", aString, aEscapeType, _retval);
2314
2315     return nsINetUtil_EscapeString(net_util, aString, aEscapeType, _retval);
2316 }
2317
2318 static nsresult NSAPI nsNetUtil_EscapeURL(nsINetUtil *iface, const nsACString *aStr, PRUint32 aFlags,
2319                                           nsACString *_retval)
2320 {
2321     TRACE("(%p %08x %p)\n", aStr, aFlags, _retval);
2322
2323     return nsINetUtil_EscapeURL(net_util, aStr, aFlags, _retval);
2324 }
2325
2326 static nsresult NSAPI nsNetUtil_UnescapeString(nsINetUtil *iface, const nsACString *aStr,
2327                                                PRUint32 aFlags, nsACString *_retval)
2328 {
2329     TRACE("(%p %08x %p)\n", aStr, aFlags, _retval);
2330
2331     return nsINetUtil_UnescapeString(net_util, aStr, aFlags, _retval);
2332 }
2333
2334 static nsresult NSAPI nsNetUtil_ExtractCharsetFromContentType(nsINetUtil *iface, const nsACString *aTypeHeader,
2335         nsACString *aCharset, PRInt32 *aCharsetStart, PRInt32 *aCharsetEnd, PRBool *_retval)
2336 {
2337     TRACE("(%p %p %p %p %p)\n", aTypeHeader, aCharset, aCharsetStart, aCharsetEnd, _retval);
2338
2339     return nsINetUtil_ExtractCharsetFromContentType(net_util, aTypeHeader, aCharset, aCharsetStart, aCharsetEnd, _retval);
2340 }
2341
2342 static const nsINetUtilVtbl nsNetUtilVtbl = {
2343     nsNetUtil_QueryInterface,
2344     nsNetUtil_AddRef,
2345     nsNetUtil_Release,
2346     nsNetUtil_ParseContentType,
2347     nsNetUtil_ProtocolHasFlags,
2348     nsNetUtil_URIChainHasFlags,
2349     nsNetUtil_ToImmutableURI,
2350     nsNetUtil_EscapeString,
2351     nsNetUtil_EscapeURL,
2352     nsNetUtil_UnescapeString,
2353     nsNetUtil_ExtractCharsetFromContentType
2354 };
2355
2356 static nsINetUtil nsNetUtil = { &nsNetUtilVtbl };
2357
2358 static nsresult NSAPI nsIOService_QueryInterface(nsIIOService *iface, nsIIDRef riid,
2359                                                  nsQIResult result)
2360 {
2361     *result = NULL;
2362
2363     if(IsEqualGUID(&IID_nsISupports, riid))
2364         *result = &nsIOService;
2365     else if(IsEqualGUID(&IID_nsIIOService, riid))
2366         *result = &nsIOService;
2367     else if(IsEqualGUID(&IID_nsINetUtil, riid))
2368         *result = &nsNetUtil;
2369
2370     if(*result) {
2371         nsISupports_AddRef((nsISupports*)*result);
2372         return NS_OK;
2373     }
2374
2375     FIXME("(%s %p)\n", debugstr_guid(riid), result);
2376     return NS_NOINTERFACE;
2377 }
2378
2379 static nsresult NSAPI nsIOServiceFactory_QueryInterface(nsIFactory *iface, nsIIDRef riid,
2380                                                         nsQIResult result)
2381 {
2382     *result = NULL;
2383
2384     if(IsEqualGUID(&IID_nsISupports, riid)) {
2385         TRACE("(IID_nsISupports %p)\n", result);
2386         *result = iface;
2387     }else if(IsEqualGUID(&IID_nsIFactory, riid)) {
2388         TRACE("(IID_nsIFactory %p)\n", result);
2389         *result = iface;
2390     }
2391
2392     if(*result) {
2393         nsIFactory_AddRef(iface);
2394         return NS_OK;
2395     }
2396
2397     WARN("(%s %p)\n", debugstr_guid(riid), result);
2398     return NS_NOINTERFACE;
2399 }
2400
2401 static nsrefcnt NSAPI nsIOServiceFactory_AddRef(nsIFactory *iface)
2402 {
2403     return 2;
2404 }
2405
2406 static nsrefcnt NSAPI nsIOServiceFactory_Release(nsIFactory *iface)
2407 {
2408     return 1;
2409 }
2410
2411 static nsresult NSAPI nsIOServiceFactory_CreateInstance(nsIFactory *iface,
2412         nsISupports *aOuter, const nsIID *iid, void **result)
2413 {
2414     return nsIIOService_QueryInterface(&nsIOService, iid, result);
2415 }
2416
2417 static nsresult NSAPI nsIOServiceFactory_LockFactory(nsIFactory *iface, PRBool lock)
2418 {
2419     WARN("(%x)\n", lock);
2420     return NS_OK;
2421 }
2422
2423 static const nsIFactoryVtbl nsIOServiceFactoryVtbl = {
2424     nsIOServiceFactory_QueryInterface,
2425     nsIOServiceFactory_AddRef,
2426     nsIOServiceFactory_Release,
2427     nsIOServiceFactory_CreateInstance,
2428     nsIOServiceFactory_LockFactory
2429 };
2430
2431 static nsIFactory nsIOServiceFactory = { &nsIOServiceFactoryVtbl };
2432
2433 void init_nsio(nsIComponentManager *component_manager, nsIComponentRegistrar *registrar)
2434 {
2435     nsIFactory *old_factory = NULL;
2436     nsresult nsres;
2437
2438     nsres = nsIComponentManager_GetClassObject(component_manager, &NS_IOSERVICE_CID,
2439                                                &IID_nsIFactory, (void**)&old_factory);
2440     if(NS_FAILED(nsres)) {
2441         ERR("Could not get factory: %08x\n", nsres);
2442         return;
2443     }
2444
2445     nsres = nsIFactory_CreateInstance(old_factory, NULL, &IID_nsIIOService, (void**)&nsio);
2446     if(NS_FAILED(nsres)) {
2447         ERR("Couldn not create nsIOService instance %08x\n", nsres);
2448         nsIFactory_Release(old_factory);
2449         return;
2450     }
2451
2452     nsres = nsIIOService_QueryInterface(nsio, &IID_nsINetUtil, (void**)&net_util);
2453     if(NS_FAILED(nsres)) {
2454         WARN("Could not get nsINetUtil interface: %08x\n", nsres);
2455         nsIIOService_Release(nsio);
2456         return;
2457     }
2458
2459     nsres = nsIComponentRegistrar_UnregisterFactory(registrar, &NS_IOSERVICE_CID, old_factory);
2460     nsIFactory_Release(old_factory);
2461     if(NS_FAILED(nsres))
2462         ERR("UnregisterFactory failed: %08x\n", nsres);
2463
2464     nsres = nsIComponentRegistrar_RegisterFactory(registrar, &NS_IOSERVICE_CID,
2465             NS_IOSERVICE_CLASSNAME, NS_IOSERVICE_CONTRACTID, &nsIOServiceFactory);
2466     if(NS_FAILED(nsres))
2467         ERR("RegisterFactory failed: %08x\n", nsres);
2468 }
2469
2470 void release_nsio(void)
2471 {
2472     if(net_util) {
2473         nsINetUtil_Release(net_util);
2474         net_util = NULL;
2475     }
2476
2477     if(nsio) {
2478         nsIIOService_Release(nsio);
2479         nsio = NULL;
2480     }
2481 }