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