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