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