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