winspool.drv: Fix the character count passed into RegEnumKeyExW in get_local_monitors.
[wine] / dlls / mshtml / nsio.c
1 /*
2  * Copyright 2006-2007 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "ole2.h"
30 #include "shlguid.h"
31 #include "wininet.h"
32 #include "shlwapi.h"
33
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
36
37 #include "mshtml_private.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
40
41 #define LOAD_INITIAL_DOCUMENT_URI 0x80000
42
43 #define NS_IOSERVICE_CLASSNAME "nsIOService"
44 #define NS_IOSERVICE_CONTRACTID "@mozilla.org/network/io-service;1"
45
46 static const IID NS_IOSERVICE_CID =
47     {0x9ac9e770, 0x18bc, 0x11d3, {0x93, 0x37, 0x00, 0x10, 0x4b, 0xa0, 0xfd, 0x40}};
48
49 static nsIIOService *nsio = NULL;
50
51 static const WCHAR about_blankW[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
52
53 typedef struct {
54     const nsIWineURIVtbl *lpWineURIVtbl;
55
56     LONG ref;
57
58     nsIURI *uri;
59     NSContainer *container;
60     LPWSTR wine_url;
61     PRBool is_doc_uri;
62     BOOL use_wine_url;
63 } nsURI;
64
65 #define NSURI(x)         ((nsIURI*)            &(x)->lpWineURIVtbl)
66 #define NSWINEURI(x)     ((nsIWineURI*)        &(x)->lpWineURIVtbl)
67
68 static nsresult create_uri(nsIURI*,NSContainer*,nsIWineURI**);
69
70 static BOOL exec_shldocvw_67(HTMLDocument *doc, LPCWSTR url)
71 {
72     IOleCommandTarget *cmdtrg = NULL;
73     HRESULT hres;
74
75     hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget,
76                                          (void**)&cmdtrg);
77     if(SUCCEEDED(hres)) {
78         VARIANT varUrl, varRes;
79
80         V_VT(&varUrl) = VT_BSTR;
81         V_BSTR(&varUrl) = SysAllocString(url);
82         V_VT(&varRes) = VT_BOOL;
83
84         hres = IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 67, 0, &varUrl, &varRes);
85
86         IOleCommandTarget_Release(cmdtrg);
87         SysFreeString(V_BSTR(&varUrl));
88
89         if(SUCCEEDED(hres) && !V_BOOL(&varRes)) {
90             TRACE("got VARIANT_FALSE, do not load\n");
91             return FALSE;
92         }
93     }
94
95     return TRUE;
96 }
97
98 static BOOL before_async_open(nsChannel *channel, NSContainer *container)
99 {
100     IServiceProvider *service_provider;
101     HTMLDocument *doc = container->doc;
102     DWORD hlnf = 0;
103     LPCWSTR uri;
104     HRESULT hres;
105
106     nsIWineURI_GetWineURL(channel->uri, &uri);
107     if(!uri) {
108         ERR("GetWineURL returned NULL\n");
109         return TRUE;
110     }
111
112     if(!doc) {
113         NSContainer *container_iter = container;
114
115         hlnf = HLNF_OPENINNEWWINDOW;
116         while(!container_iter->doc)
117             container_iter = container_iter->parent;
118         doc = container_iter->doc;
119     }
120
121     if(!doc->client)
122         return TRUE;
123
124     if(!hlnf && !exec_shldocvw_67(doc, uri))
125         return FALSE;
126
127     hres = IOleClientSite_QueryInterface(doc->client, &IID_IServiceProvider,
128                                          (void**)&service_provider);
129     if(SUCCEEDED(hres)) {
130         IHlinkFrame *hlink_frame;
131
132         hres = IServiceProvider_QueryService(service_provider, &IID_IHlinkFrame,
133                                              &IID_IHlinkFrame, (void**)&hlink_frame);
134         IServiceProvider_Release(service_provider);
135         if(SUCCEEDED(hres)) {
136             hlink_frame_navigate(doc, hlink_frame, uri, channel->post_data_stream, hlnf);
137             IHlinkFrame_Release(hlink_frame);
138
139             return FALSE;
140         }
141     }
142
143     return TRUE;
144 }
145
146 #define NSCHANNEL_THIS(iface) DEFINE_THIS(nsChannel, HttpChannel, iface)
147
148 static nsresult NSAPI nsChannel_QueryInterface(nsIHttpChannel *iface, nsIIDRef riid, nsQIResult result)
149 {
150     nsChannel *This = NSCHANNEL_THIS(iface);
151
152     *result = NULL;
153
154     if(IsEqualGUID(&IID_nsISupports, riid)) {
155         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
156         *result = NSCHANNEL(This);
157     }else if(IsEqualGUID(&IID_nsIRequest, riid)) {
158         TRACE("(%p)->(IID_nsIRequest %p)\n", This, result);
159         *result = NSCHANNEL(This);
160     }else if(IsEqualGUID(&IID_nsIChannel, riid)) {
161         TRACE("(%p)->(IID_nsIChannel %p)\n", This, result);
162         *result = NSCHANNEL(This);
163     }else if(This->http_channel && IsEqualGUID(&IID_nsIHttpChannel, riid)) {
164         TRACE("(%p)->(IID_nsIHttpChannel %p)\n", This, result);
165         *result = NSHTTPCHANNEL(This);
166     }else if(IsEqualGUID(&IID_nsIUploadChannel, riid)) {
167         TRACE("(%p)->(IID_nsIUploadChannel %p)\n", This, result);
168         *result = NSUPCHANNEL(This);
169     }
170
171     if(*result) {
172         nsIChannel_AddRef(NSCHANNEL(This));
173         return NS_OK;
174     }
175
176     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
177
178     if(This->channel)
179         return nsIChannel_QueryInterface(This->channel, riid, result);
180     return NS_NOINTERFACE;
181 }
182
183 static nsrefcnt NSAPI nsChannel_AddRef(nsIHttpChannel *iface)
184 {
185     nsChannel *This = NSCHANNEL_THIS(iface);
186     nsrefcnt ref = InterlockedIncrement(&This->ref);
187
188     TRACE("(%p) ref=%d\n", This, ref);
189
190     return ref;
191 }
192
193 static nsrefcnt NSAPI nsChannel_Release(nsIHttpChannel *iface)
194 {
195     nsChannel *This = NSCHANNEL_THIS(iface);
196     LONG ref = InterlockedDecrement(&This->ref);
197
198     if(!ref) {
199         nsIWineURI_Release(This->uri);
200         if(This->channel)
201             nsIChannel_Release(This->channel);
202         if(This->http_channel)
203             nsIHttpChannel_Release(This->http_channel);
204         if(This->post_data_stream)
205             nsIInputStream_Release(This->post_data_stream);
206         if(This->load_group)
207             nsILoadGroup_Release(This->load_group);
208         if(This->notif_callback)
209             nsIInterfaceRequestor_Release(This->notif_callback);
210         if(This->original_uri)
211             nsIURI_Release(This->original_uri);
212         heap_free(This->content);
213         heap_free(This->charset);
214         heap_free(This);
215     }
216
217     return ref;
218 }
219
220 static nsresult NSAPI nsChannel_GetName(nsIHttpChannel *iface, nsACString *aName)
221 {
222     nsChannel *This = NSCHANNEL_THIS(iface);
223
224     TRACE("(%p)->(%p)\n", This, aName);
225
226     if(This->channel)
227         return nsIChannel_GetName(This->channel, aName);
228
229     return NS_ERROR_NOT_IMPLEMENTED;
230 }
231
232 static nsresult NSAPI nsChannel_IsPending(nsIHttpChannel *iface, PRBool *_retval)
233 {
234     nsChannel *This = NSCHANNEL_THIS(iface);
235
236     TRACE("(%p)->(%p)\n", This, _retval);
237
238     if(This->channel)
239         return nsIChannel_IsPending(This->channel, _retval);
240
241     FIXME("default action not implemented\n");
242     return NS_ERROR_NOT_IMPLEMENTED;
243 }
244
245 static nsresult NSAPI nsChannel_GetStatus(nsIHttpChannel *iface, nsresult *aStatus)
246 {
247     nsChannel *This = NSCHANNEL_THIS(iface);
248
249     TRACE("(%p)->(%p)\n", This, aStatus);
250
251     if(This->channel)
252         return nsIChannel_GetStatus(This->channel, aStatus);
253
254     TRACE("returning NS_OK\n");
255     return *aStatus = NS_OK;
256 }
257
258 static nsresult NSAPI nsChannel_Cancel(nsIHttpChannel *iface, nsresult aStatus)
259 {
260     nsChannel *This = NSCHANNEL_THIS(iface);
261
262     TRACE("(%p)->(%08x)\n", This, aStatus);
263
264     if(This->channel)
265         return nsIChannel_Cancel(This->channel, aStatus);
266
267     FIXME("default action not implemented\n");
268     return NS_ERROR_NOT_IMPLEMENTED;
269 }
270
271 static nsresult NSAPI nsChannel_Suspend(nsIHttpChannel *iface)
272 {
273     nsChannel *This = NSCHANNEL_THIS(iface);
274
275     TRACE("(%p)\n", This);
276
277     if(This->channel)
278         return nsIChannel_Suspend(This->channel);
279
280     FIXME("default action not implemented\n");
281     return NS_ERROR_NOT_IMPLEMENTED;
282 }
283
284 static nsresult NSAPI nsChannel_Resume(nsIHttpChannel *iface)
285 {
286     nsChannel *This = NSCHANNEL_THIS(iface);
287
288     TRACE("(%p)\n", This);
289
290     if(This->channel)
291         return nsIChannel_Resume(This->channel);
292
293     FIXME("default action not implemented\n");
294     return NS_ERROR_NOT_IMPLEMENTED;
295 }
296
297 static nsresult NSAPI nsChannel_GetLoadGroup(nsIHttpChannel *iface, nsILoadGroup **aLoadGroup)
298 {
299     nsChannel *This = NSCHANNEL_THIS(iface);
300
301     TRACE("(%p)->(%p)\n", This, aLoadGroup);
302
303     if(This->load_group)
304         nsILoadGroup_AddRef(This->load_group);
305
306     *aLoadGroup = This->load_group;
307     return NS_OK;
308 }
309
310 static nsresult NSAPI nsChannel_SetLoadGroup(nsIHttpChannel *iface, nsILoadGroup *aLoadGroup)
311 {
312     nsChannel *This = NSCHANNEL_THIS(iface);
313
314     TRACE("(%p)->(%p)\n", This, aLoadGroup);
315
316     if(This->load_group)
317         nsILoadGroup_Release(This->load_group);
318     if(aLoadGroup)
319         nsILoadGroup_AddRef(aLoadGroup);
320
321     This->load_group = aLoadGroup;
322
323     if(This->channel)
324         return nsIChannel_SetLoadGroup(This->channel, aLoadGroup);
325     return NS_OK;
326 }
327
328 static nsresult NSAPI nsChannel_GetLoadFlags(nsIHttpChannel *iface, nsLoadFlags *aLoadFlags)
329 {
330     nsChannel *This = NSCHANNEL_THIS(iface);
331
332     TRACE("(%p)->(%p)\n", This, aLoadFlags);
333
334     *aLoadFlags = This->load_flags;
335     return NS_OK;
336 }
337
338 static nsresult NSAPI nsChannel_SetLoadFlags(nsIHttpChannel *iface, nsLoadFlags aLoadFlags)
339 {
340     nsChannel *This = NSCHANNEL_THIS(iface);
341
342     TRACE("(%p)->(%08x)\n", This, aLoadFlags);
343
344     This->load_flags = aLoadFlags;
345
346     if(This->channel)
347         return nsIChannel_SetLoadFlags(This->channel, aLoadFlags);
348     return NS_OK;
349 }
350
351 static nsresult NSAPI nsChannel_GetOriginalURI(nsIHttpChannel *iface, nsIURI **aOriginalURI)
352 {
353     nsChannel *This = NSCHANNEL_THIS(iface);
354
355     TRACE("(%p)->(%p)\n", This, aOriginalURI);
356
357     if(This->original_uri)
358         nsIURI_AddRef(This->original_uri);
359
360     *aOriginalURI = This->original_uri;
361     return NS_OK;
362 }
363
364 static nsresult NSAPI nsChannel_SetOriginalURI(nsIHttpChannel *iface, nsIURI *aOriginalURI)
365 {
366     nsChannel *This = NSCHANNEL_THIS(iface);
367
368     TRACE("(%p)->(%p)\n", This, aOriginalURI);
369
370     if(This->original_uri)
371         nsIURI_Release(This->original_uri);
372
373     nsIURI_AddRef(aOriginalURI);
374     This->original_uri = aOriginalURI;
375
376     if(This->channel)
377         return nsIChannel_SetOriginalURI(This->channel, aOriginalURI);
378     return NS_OK;
379 }
380
381 static nsresult NSAPI nsChannel_GetURI(nsIHttpChannel *iface, nsIURI **aURI)
382 {
383     nsChannel *This = NSCHANNEL_THIS(iface);
384
385     TRACE("(%p)->(%p)\n", This, aURI);
386
387     nsIWineURI_AddRef(This->uri);
388     *aURI = (nsIURI*)This->uri;
389
390     return NS_OK;
391 }
392
393 static nsresult NSAPI nsChannel_GetOwner(nsIHttpChannel *iface, nsISupports **aOwner)
394 {
395     nsChannel *This = NSCHANNEL_THIS(iface);
396
397     TRACE("(%p)->(%p)\n", This, aOwner);
398
399     if(This->channel)
400         return nsIChannel_GetOwner(This->channel, aOwner);
401
402     FIXME("default action not implemented\n");
403     return NS_ERROR_NOT_IMPLEMENTED;
404 }
405
406 static nsresult NSAPI nsChannel_SetOwner(nsIHttpChannel *iface, nsISupports *aOwner)
407 {
408     nsChannel *This = NSCHANNEL_THIS(iface);
409
410     TRACE("(%p)->(%p)\n", This, aOwner);
411
412     if(This->channel)
413         return nsIChannel_SetOwner(This->channel, aOwner);
414
415     FIXME("default action not implemented\n");
416     return NS_ERROR_NOT_IMPLEMENTED;
417 }
418
419 static nsresult NSAPI nsChannel_GetNotificationCallbacks(nsIHttpChannel *iface,
420         nsIInterfaceRequestor **aNotificationCallbacks)
421 {
422     nsChannel *This = NSCHANNEL_THIS(iface);
423
424     TRACE("(%p)->(%p)\n", This, aNotificationCallbacks);
425
426     if(This->notif_callback)
427         nsIInterfaceRequestor_AddRef(This->notif_callback);
428     *aNotificationCallbacks = This->notif_callback;
429
430     return NS_OK;
431 }
432
433 static nsresult NSAPI nsChannel_SetNotificationCallbacks(nsIHttpChannel *iface,
434         nsIInterfaceRequestor *aNotificationCallbacks)
435 {
436     nsChannel *This = NSCHANNEL_THIS(iface);
437
438     TRACE("(%p)->(%p)\n", This, aNotificationCallbacks);
439
440     if(This->notif_callback)
441         nsIInterfaceRequestor_Release(This->notif_callback);
442     if(aNotificationCallbacks)
443         nsIInterfaceRequestor_AddRef(aNotificationCallbacks);
444
445     This->notif_callback = aNotificationCallbacks;
446
447     if(This->channel)
448         return nsIChannel_SetNotificationCallbacks(This->channel, aNotificationCallbacks);
449     return NS_OK;
450 }
451
452 static nsresult NSAPI nsChannel_GetSecurityInfo(nsIHttpChannel *iface, nsISupports **aSecurityInfo)
453 {
454     nsChannel *This = NSCHANNEL_THIS(iface);
455
456     TRACE("(%p)->(%p)\n", This, aSecurityInfo);
457
458     if(This->channel)
459         return nsIChannel_GetSecurityInfo(This->channel, aSecurityInfo);
460
461     FIXME("default action not implemented\n");
462     return NS_ERROR_NOT_IMPLEMENTED;
463 }
464
465 static nsresult NSAPI nsChannel_GetContentType(nsIHttpChannel *iface, nsACString *aContentType)
466 {
467     nsChannel *This = NSCHANNEL_THIS(iface);
468
469     TRACE("(%p)->(%p)\n", This, aContentType);
470
471     if(This->content) {
472         nsACString_SetData(aContentType, This->content);
473         return S_OK;
474     }
475
476     if(This->channel)
477         return nsIChannel_GetContentType(This->channel, aContentType);
478
479     TRACE("returning default text/html\n");
480     nsACString_SetData(aContentType, "text/html");
481     return NS_OK;
482 }
483
484 static nsresult NSAPI nsChannel_SetContentType(nsIHttpChannel *iface,
485                                                const nsACString *aContentType)
486 {
487     nsChannel *This = NSCHANNEL_THIS(iface);
488
489     TRACE("(%p)->(%p)\n", This, aContentType);
490
491     if(This->channel)
492         return nsIChannel_SetContentType(This->channel, aContentType);
493
494     FIXME("default action not implemented\n");
495     return NS_ERROR_NOT_IMPLEMENTED;
496 }
497
498 static nsresult NSAPI nsChannel_GetContentCharset(nsIHttpChannel *iface,
499                                                   nsACString *aContentCharset)
500 {
501     nsChannel *This = NSCHANNEL_THIS(iface);
502
503     TRACE("(%p)->(%p)\n", This, aContentCharset);
504
505     if(This->charset) {
506         nsACString_SetData(aContentCharset, This->charset);
507         return NS_OK;
508     }
509
510     if(This->channel) {
511         nsresult nsres = nsIChannel_GetContentCharset(This->channel, aContentCharset);
512         const char *ch;
513         nsACString_GetData(aContentCharset, &ch);
514         return nsres;
515     }
516
517     nsACString_SetData(aContentCharset, "");
518     return NS_OK;
519 }
520
521 static nsresult NSAPI nsChannel_SetContentCharset(nsIHttpChannel *iface,
522                                                   const nsACString *aContentCharset)
523 {
524     nsChannel *This = NSCHANNEL_THIS(iface);
525
526     TRACE("(%p)->(%p)\n", This, aContentCharset);
527
528     if(This->channel)
529         return nsIChannel_SetContentCharset(This->channel, aContentCharset);
530
531     FIXME("default action not implemented\n");
532     return NS_ERROR_NOT_IMPLEMENTED;
533 }
534
535 static nsresult NSAPI nsChannel_GetContentLength(nsIHttpChannel *iface, PRInt32 *aContentLength)
536 {
537     nsChannel *This = NSCHANNEL_THIS(iface);
538
539     TRACE("(%p)->(%p)\n", This, aContentLength);
540
541     if(This->channel)
542         return nsIChannel_GetContentLength(This->channel, aContentLength);
543
544     FIXME("default action not implemented\n");
545     return NS_ERROR_NOT_IMPLEMENTED;
546 }
547
548 static nsresult NSAPI nsChannel_SetContentLength(nsIHttpChannel *iface, PRInt32 aContentLength)
549 {
550     nsChannel *This = NSCHANNEL_THIS(iface);
551
552     TRACE("(%p)->(%d)\n", This, aContentLength);
553
554     if(This->channel)
555         return nsIChannel_SetContentLength(This->channel, aContentLength);
556
557     FIXME("default action not implemented\n");
558     return NS_ERROR_NOT_IMPLEMENTED;
559 }
560
561 static nsresult NSAPI nsChannel_Open(nsIHttpChannel *iface, nsIInputStream **_retval)
562 {
563     nsChannel *This = NSCHANNEL_THIS(iface);
564
565     TRACE("(%p)->(%p)\n", This, _retval);
566
567     if(This->channel)
568         return nsIChannel_Open(This->channel, _retval);
569
570     FIXME("default action not implemented\n");
571     return NS_ERROR_NOT_IMPLEMENTED;
572 }
573
574 static BOOL do_load_from_moniker_hack(nsChannel *This)
575 {
576     nsACString scheme_str;
577     nsresult nsres;
578     BOOL ret = TRUE;
579
580     /* 
581      * We should always load the page from IMoniker, but Wine is not yet
582      * ready for this. This function is a heuristic, that decides which
583      * way of loading is better (Gecko implementation or IMoniker). The
584      * aim is to always return TRUE.
585      */
586
587     /* Load from moniker if there is no Gecko channel available */
588     if(!This->channel)
589         return TRUE;
590
591     nsACString_Init(&scheme_str, NULL);
592     nsres = nsIWineURI_GetScheme(This->uri, &scheme_str);
593
594     if(NS_SUCCEEDED(nsres)) {
595         const char *scheme;
596
597         nsACString_GetData(&scheme_str, &scheme);
598         ret = !strcmp(scheme, "wine") || !strcmp(scheme, "about");
599     }
600
601     nsACString_Finish(&scheme_str);
602     return ret;
603 }
604
605 static HRESULT create_mon_for_nschannel(nsChannel *channel, IMoniker **mon)
606 {
607     nsIWineURI *wine_uri;
608     LPCWSTR wine_url;
609     nsresult nsres;
610     HRESULT hres;
611
612     if(!channel->original_uri) {
613         ERR("original_uri == NULL\n");
614         return E_FAIL;
615     }
616
617     nsres = nsIURI_QueryInterface(channel->original_uri, &IID_nsIWineURI, (void**)&wine_uri);
618     if(NS_FAILED(nsres)) {
619         ERR("Could not get nsIWineURI: %08x\n", nsres);
620         return E_FAIL;
621     }
622
623     nsIWineURI_GetWineURL(wine_uri, &wine_url);
624     nsIWineURI_Release(wine_uri);
625     if(!wine_url) {
626         TRACE("wine_url == NULL\n");
627         return E_FAIL;
628     }
629
630     hres = CreateURLMoniker(NULL, wine_url, mon);
631     if(FAILED(hres))
632         WARN("CreateURLMonikrer failed: %08x\n", hres);
633
634     return hres;
635 }
636
637 static nsresult async_open_doc_uri(nsChannel *This, NSContainer *container,
638         nsIStreamListener *listener, nsISupports *context, BOOL *open)
639 {
640     IMoniker *mon;
641     HRESULT hres;
642
643     *open = FALSE;
644
645     if(container->bscallback) {
646         nsIChannel_AddRef(NSCHANNEL(This));
647         container->bscallback->nschannel = This;
648
649         nsIStreamListener_AddRef(listener);
650         container->bscallback->nslistener = listener;
651
652         if(context) {
653             nsISupports_AddRef(context);
654             container->bscallback->nscontext = context;
655         }
656
657         if(container->doc && container->doc->mime) {
658             DWORD len;
659
660             heap_free(This->content);
661
662             len = WideCharToMultiByte(CP_ACP, 0, container->doc->mime, -1, NULL, 0, NULL, NULL);
663             This->content = heap_alloc(len);
664             WideCharToMultiByte(CP_ACP, 0, container->doc->mime, -1, This->content, -1, NULL, NULL);
665         }
666
667         if(do_load_from_moniker_hack(This))
668             return WINE_NS_LOAD_FROM_MONIKER;
669     }else  {
670         BOOL cont = before_async_open(This, container);
671
672         if(!cont) {
673             TRACE("canceled\n");
674             return NS_ERROR_UNEXPECTED;
675         }
676
677         if(!container->doc) {
678             return This->channel
679                 ?  nsIChannel_AsyncOpen(This->channel, listener, context)
680                 : NS_ERROR_UNEXPECTED;
681         }
682
683         hres = create_mon_for_nschannel(This, &mon);
684         if(FAILED(hres)) {
685             return NS_ERROR_UNEXPECTED;
686         }
687         set_current_mon(container->doc, mon);
688     }
689
690     *open = TRUE;
691     return NS_OK;
692 }
693
694 static nsresult async_open(nsChannel *This, NSContainer *container, nsIStreamListener *listener,
695         nsISupports *context)
696 {
697     BSCallback *bscallback;
698     IMoniker *mon = NULL;
699     nsresult nsres;
700     task_t *task;
701     HRESULT hres;
702
703     if(This->channel) {
704         if(This->post_data_stream) {
705             nsIUploadChannel *upload_channel;
706
707             nsres = nsIChannel_QueryInterface(This->channel, &IID_nsIUploadChannel,
708                                           (void**)&upload_channel);
709             if(NS_SUCCEEDED(nsres)) {
710                 nsACString empty_string;
711                 nsACString_Init(&empty_string, "");
712
713                 nsres = nsIUploadChannel_SetUploadStream(upload_channel, This->post_data_stream,
714                                                          &empty_string, -1);
715                 nsIUploadChannel_Release(upload_channel);
716                 if(NS_FAILED(nsres))
717                     WARN("SetUploadStream failed: %08x\n", nsres);
718
719                 nsACString_Finish(&empty_string);
720             }
721         }
722
723         nsres = nsIChannel_AsyncOpen(This->channel, listener, context);
724
725         if(mon)
726             IMoniker_Release(mon);
727
728         if(NS_FAILED(nsres) && (This->load_flags & LOAD_INITIAL_DOCUMENT_URI))
729             return WINE_NS_LOAD_FROM_MONIKER;
730         return nsres;
731     }
732
733     TRACE("channel == NULL\n");
734
735     hres = create_mon_for_nschannel(This, &mon);
736     if(FAILED(hres))
737         return NS_ERROR_UNEXPECTED;
738
739     bscallback = create_bscallback(mon);
740     IMoniker_Release(mon);
741
742     nsIChannel_AddRef(NSCHANNEL(This));
743     bscallback->nschannel = This;
744
745     nsIStreamListener_AddRef(listener);
746     bscallback->nslistener = listener;
747
748     if(context) {
749         nsISupports_AddRef(context);
750         bscallback->nscontext = context;
751     }
752
753     task = heap_alloc(sizeof(task_t));
754
755     task->doc = container->doc;
756     task->task_id = TASK_START_BINDING;
757     task->next = NULL;
758     task->bscallback = bscallback;
759
760     push_task(task);
761
762     return NS_OK;
763 }
764
765 static nsresult NSAPI nsChannel_AsyncOpen(nsIHttpChannel *iface, nsIStreamListener *aListener,
766                                           nsISupports *aContext)
767 {
768     nsChannel *This = NSCHANNEL_THIS(iface);
769     NSContainer *container;
770     PRBool is_doc_uri;
771     BOOL open = TRUE;
772     nsresult nsres = NS_OK;
773
774     TRACE("(%p)->(%p %p)\n", This, aListener, aContext);
775
776     nsIWineURI_GetNSContainer(This->uri, &container);
777     if(!container) {
778         TRACE("container = NULL\n");
779         return This->channel
780             ? nsIChannel_AsyncOpen(This->channel, aListener, aContext)
781             : NS_ERROR_UNEXPECTED;
782     }
783
784     nsIWineURI_GetIsDocumentURI(This->uri, &is_doc_uri);
785
786     if(is_doc_uri && (This->load_flags & LOAD_INITIAL_DOCUMENT_URI))
787         nsres = async_open_doc_uri(This, container, aListener, aContext, &open);
788
789     if(open)
790         nsres = async_open(This, container, aListener, aContext);
791
792     nsIWebBrowserChrome_Release(NSWBCHROME(container));
793     return nsres;
794 }
795
796 static nsresult NSAPI nsChannel_GetRequestMethod(nsIHttpChannel *iface, nsACString *aRequestMethod)
797 {
798     nsChannel *This = NSCHANNEL_THIS(iface);
799
800     TRACE("(%p)->(%p)\n", This, aRequestMethod);
801
802     if(This->http_channel)
803         return nsIHttpChannel_GetRequestMethod(This->http_channel, aRequestMethod);
804
805     return NS_ERROR_NOT_IMPLEMENTED;
806 }
807
808 static nsresult NSAPI nsChannel_SetRequestMethod(nsIHttpChannel *iface,
809                                                  const nsACString *aRequestMethod)
810 {
811     nsChannel *This = NSCHANNEL_THIS(iface);
812
813     TRACE("(%p)->(%p)\n", This, aRequestMethod);
814
815     if(This->http_channel)
816         return nsIHttpChannel_SetRequestMethod(This->http_channel, aRequestMethod);
817
818     return NS_ERROR_NOT_IMPLEMENTED;
819 }
820
821 static nsresult NSAPI nsChannel_GetReferrer(nsIHttpChannel *iface, nsIURI **aReferrer)
822 {
823     nsChannel *This = NSCHANNEL_THIS(iface);
824
825     TRACE("(%p)->(%p)\n", This, aReferrer);
826
827     if(This->http_channel)
828         return nsIHttpChannel_GetReferrer(This->http_channel, aReferrer);
829
830     return NS_ERROR_NOT_IMPLEMENTED;
831 }
832
833 static nsresult NSAPI nsChannel_SetReferrer(nsIHttpChannel *iface, nsIURI *aReferrer)
834 {
835     nsChannel *This = NSCHANNEL_THIS(iface);
836
837     TRACE("(%p)->(%p)\n", This, aReferrer);
838
839     if(This->http_channel)
840         return nsIHttpChannel_SetReferrer(This->http_channel, aReferrer);
841
842     return NS_ERROR_NOT_IMPLEMENTED;
843 }
844
845 static nsresult NSAPI nsChannel_GetRequestHeader(nsIHttpChannel *iface,
846          const nsACString *aHeader, nsACString *_retval)
847 {
848     nsChannel *This = NSCHANNEL_THIS(iface);
849
850     TRACE("(%p)->(%p %p)\n", This, aHeader, _retval);
851
852     if(This->http_channel)
853         return nsIHttpChannel_GetRequestHeader(This->http_channel, aHeader, _retval);
854
855     return NS_ERROR_NOT_IMPLEMENTED;
856 }
857
858 static nsresult NSAPI nsChannel_SetRequestHeader(nsIHttpChannel *iface,
859          const nsACString *aHeader, const nsACString *aValue, PRBool aMerge)
860 {
861     nsChannel *This = NSCHANNEL_THIS(iface);
862
863     TRACE("(%p)->(%p %p %x)\n", This, aHeader, aValue, aMerge);
864
865     if(This->http_channel)
866         return nsIHttpChannel_SetRequestHeader(This->http_channel, aHeader, aValue, aMerge);
867
868     return NS_ERROR_NOT_IMPLEMENTED;
869 }
870
871 static nsresult NSAPI nsChannel_VisitRequestHeaders(nsIHttpChannel *iface,
872                                                     nsIHttpHeaderVisitor *aVisitor)
873 {
874     nsChannel *This = NSCHANNEL_THIS(iface);
875
876     TRACE("(%p)->(%p)\n", This, aVisitor);
877
878     if(This->http_channel)
879         return nsIHttpChannel_VisitRequestHeaders(This->http_channel, aVisitor);
880
881     return NS_ERROR_NOT_IMPLEMENTED;
882 }
883
884 static nsresult NSAPI nsChannel_GetAllowPipelining(nsIHttpChannel *iface, PRBool *aAllowPipelining)
885 {
886     nsChannel *This = NSCHANNEL_THIS(iface);
887
888     TRACE("(%p)->(%p)\n", This, aAllowPipelining);
889
890     if(This->http_channel)
891         return nsIHttpChannel_GetAllowPipelining(This->http_channel, aAllowPipelining);
892
893     return NS_ERROR_NOT_IMPLEMENTED;
894 }
895
896 static nsresult NSAPI nsChannel_SetAllowPipelining(nsIHttpChannel *iface, PRBool aAllowPipelining)
897 {
898     nsChannel *This = NSCHANNEL_THIS(iface);
899
900     TRACE("(%p)->(%x)\n", This, aAllowPipelining);
901
902     if(This->http_channel)
903         return nsIHttpChannel_SetAllowPipelining(This->http_channel, aAllowPipelining);
904
905     return NS_ERROR_NOT_IMPLEMENTED;
906 }
907
908 static nsresult NSAPI nsChannel_GetRedirectionLimit(nsIHttpChannel *iface, PRUint32 *aRedirectionLimit)
909 {
910     nsChannel *This = NSCHANNEL_THIS(iface);
911
912     TRACE("(%p)->(%p)\n", This, aRedirectionLimit);
913
914     if(This->http_channel)
915         return nsIHttpChannel_GetRedirectionLimit(This->http_channel, aRedirectionLimit);
916
917     return NS_ERROR_NOT_IMPLEMENTED;
918 }
919
920 static nsresult NSAPI nsChannel_SetRedirectionLimit(nsIHttpChannel *iface, PRUint32 aRedirectionLimit)
921 {
922     nsChannel *This = NSCHANNEL_THIS(iface);
923
924     TRACE("(%p)->(%u)\n", This, aRedirectionLimit);
925
926     if(This->http_channel)
927         return nsIHttpChannel_SetRedirectionLimit(This->http_channel, aRedirectionLimit);
928
929     return NS_ERROR_NOT_IMPLEMENTED;
930 }
931
932 static nsresult NSAPI nsChannel_GetResponseStatus(nsIHttpChannel *iface, PRUint32 *aResponseStatus)
933 {
934     nsChannel *This = NSCHANNEL_THIS(iface);
935
936     TRACE("(%p)->(%p)\n", This, aResponseStatus);
937
938     if(This->http_channel)
939         return nsIHttpChannel_GetResponseStatus(This->http_channel, aResponseStatus);
940
941     return NS_ERROR_NOT_IMPLEMENTED;
942 }
943
944 static nsresult NSAPI nsChannel_GetResponseStatusText(nsIHttpChannel *iface,
945                                                       nsACString *aResponseStatusText)
946 {
947     nsChannel *This = NSCHANNEL_THIS(iface);
948
949     TRACE("(%p)->(%p)\n", This, aResponseStatusText);
950
951     if(This->http_channel)
952         return nsIHttpChannel_GetResponseStatusText(This->http_channel, aResponseStatusText);
953
954     return NS_ERROR_NOT_IMPLEMENTED;
955 }
956
957 static nsresult NSAPI nsChannel_GetRequestSucceeded(nsIHttpChannel *iface,
958                                                     PRBool *aRequestSucceeded)
959 {
960     nsChannel *This = NSCHANNEL_THIS(iface);
961
962     TRACE("(%p)->(%p)\n", This, aRequestSucceeded);
963
964     if(This->http_channel)
965         return nsIHttpChannel_GetRequestSucceeded(This->http_channel, aRequestSucceeded);
966
967     return NS_ERROR_NOT_IMPLEMENTED;
968 }
969
970 static nsresult NSAPI nsChannel_GetResponseHeader(nsIHttpChannel *iface,
971          const nsACString *header, nsACString *_retval)
972 {
973     nsChannel *This = NSCHANNEL_THIS(iface);
974
975     TRACE("(%p)->(%p %p)\n", This, header, _retval);
976
977     if(This->http_channel)
978         return nsIHttpChannel_GetResponseHeader(This->http_channel, header, _retval);
979
980     return NS_ERROR_NOT_IMPLEMENTED;
981 }
982
983 static nsresult NSAPI nsChannel_SetResponseHeader(nsIHttpChannel *iface,
984         const nsACString *header, const nsACString *value, PRBool merge)
985 {
986     nsChannel *This = NSCHANNEL_THIS(iface);
987
988     TRACE("(%p)->(%p %p %x)\n", This, header, value, merge);
989
990     if(This->http_channel)
991         return nsIHttpChannel_SetResponseHeader(This->http_channel, header, value, merge);
992
993     return NS_ERROR_NOT_IMPLEMENTED;
994 }
995
996 static nsresult NSAPI nsChannel_VisitResponseHeaders(nsIHttpChannel *iface,
997         nsIHttpHeaderVisitor *aVisitor)
998 {
999     nsChannel *This = NSCHANNEL_THIS(iface);
1000
1001     TRACE("(%p)->(%p)\n", This, aVisitor);
1002
1003     if(This->http_channel)
1004         return nsIHttpChannel_VisitResponseHeaders(This->http_channel, aVisitor);
1005
1006     return NS_ERROR_NOT_IMPLEMENTED;
1007 }
1008
1009 static nsresult NSAPI nsChannel_IsNoStoreResponse(nsIHttpChannel *iface, PRBool *_retval)
1010 {
1011     nsChannel *This = NSCHANNEL_THIS(iface);
1012
1013     TRACE("(%p)->(%p)\n", This, _retval);
1014
1015     if(This->http_channel)
1016         return nsIHttpChannel_IsNoStoreResponse(This->http_channel, _retval);
1017
1018     return NS_ERROR_NOT_IMPLEMENTED;
1019 }
1020
1021 static nsresult NSAPI nsChannel_IsNoCacheResponse(nsIHttpChannel *iface, PRBool *_retval)
1022 {
1023     nsChannel *This = NSCHANNEL_THIS(iface);
1024
1025     TRACE("(%p)->(%p)\n", This, _retval);
1026
1027     if(This->http_channel)
1028         return nsIHttpChannel_IsNoCacheResponse(This->http_channel, _retval);
1029
1030     return NS_ERROR_NOT_IMPLEMENTED;
1031 }
1032
1033 #undef NSCHANNEL_THIS
1034
1035 static const nsIHttpChannelVtbl nsChannelVtbl = {
1036     nsChannel_QueryInterface,
1037     nsChannel_AddRef,
1038     nsChannel_Release,
1039     nsChannel_GetName,
1040     nsChannel_IsPending,
1041     nsChannel_GetStatus,
1042     nsChannel_Cancel,
1043     nsChannel_Suspend,
1044     nsChannel_Resume,
1045     nsChannel_GetLoadGroup,
1046     nsChannel_SetLoadGroup,
1047     nsChannel_GetLoadFlags,
1048     nsChannel_SetLoadFlags,
1049     nsChannel_GetOriginalURI,
1050     nsChannel_SetOriginalURI,
1051     nsChannel_GetURI,
1052     nsChannel_GetOwner,
1053     nsChannel_SetOwner,
1054     nsChannel_GetNotificationCallbacks,
1055     nsChannel_SetNotificationCallbacks,
1056     nsChannel_GetSecurityInfo,
1057     nsChannel_GetContentType,
1058     nsChannel_SetContentType,
1059     nsChannel_GetContentCharset,
1060     nsChannel_SetContentCharset,
1061     nsChannel_GetContentLength,
1062     nsChannel_SetContentLength,
1063     nsChannel_Open,
1064     nsChannel_AsyncOpen,
1065     nsChannel_GetRequestMethod,
1066     nsChannel_SetRequestMethod,
1067     nsChannel_GetReferrer,
1068     nsChannel_SetReferrer,
1069     nsChannel_GetRequestHeader,
1070     nsChannel_SetRequestHeader,
1071     nsChannel_VisitRequestHeaders,
1072     nsChannel_GetAllowPipelining,
1073     nsChannel_SetAllowPipelining,
1074     nsChannel_GetRedirectionLimit,
1075     nsChannel_SetRedirectionLimit,
1076     nsChannel_GetResponseStatus,
1077     nsChannel_GetResponseStatusText,
1078     nsChannel_GetRequestSucceeded,
1079     nsChannel_GetResponseHeader,
1080     nsChannel_SetResponseHeader,
1081     nsChannel_VisitResponseHeaders,
1082     nsChannel_IsNoStoreResponse,
1083     nsChannel_IsNoCacheResponse
1084 };
1085
1086 #define NSUPCHANNEL_THIS(iface) DEFINE_THIS(nsChannel, UploadChannel, iface)
1087
1088 static nsresult NSAPI nsUploadChannel_QueryInterface(nsIUploadChannel *iface, nsIIDRef riid,
1089                                                      nsQIResult result)
1090 {
1091     nsChannel *This = NSUPCHANNEL_THIS(iface);
1092     return nsIChannel_QueryInterface(NSCHANNEL(This), riid, result);
1093 }
1094
1095 static nsrefcnt NSAPI nsUploadChannel_AddRef(nsIUploadChannel *iface)
1096 {
1097     nsChannel *This = NSUPCHANNEL_THIS(iface);
1098     return nsIChannel_AddRef(NSCHANNEL(This));
1099 }
1100
1101 static nsrefcnt NSAPI nsUploadChannel_Release(nsIUploadChannel *iface)
1102 {
1103     nsChannel *This = NSUPCHANNEL_THIS(iface);
1104     return nsIChannel_Release(NSCHANNEL(This));
1105 }
1106
1107 static nsresult NSAPI nsUploadChannel_SetUploadStream(nsIUploadChannel *iface,
1108         nsIInputStream *aStream, const nsACString *aContentType, PRInt32 aContentLength)
1109 {
1110     nsChannel *This = NSUPCHANNEL_THIS(iface);
1111     const char *content_type;
1112
1113     TRACE("(%p)->(%p %p %d)\n", This, aStream, aContentType, aContentLength);
1114
1115     if(This->post_data_stream)
1116         nsIInputStream_Release(This->post_data_stream);
1117
1118     if(aContentType) {
1119         nsACString_GetData(aContentType, &content_type);
1120         if(*content_type)
1121             FIXME("Unsupported aContentType argument: %s\n", debugstr_a(content_type));
1122     }
1123
1124     if(aContentLength != -1)
1125         FIXME("Unsupported acontentLength = %d\n", aContentLength);
1126
1127     if(aStream)
1128         nsIInputStream_AddRef(aStream);
1129     This->post_data_stream = aStream;
1130
1131     return NS_OK;
1132 }
1133
1134 static nsresult NSAPI nsUploadChannel_GetUploadStream(nsIUploadChannel *iface,
1135         nsIInputStream **aUploadStream)
1136 {
1137     nsChannel *This = NSUPCHANNEL_THIS(iface);
1138
1139     TRACE("(%p)->(%p)\n", This, aUploadStream);
1140
1141     if(This->post_data_stream)
1142         nsIInputStream_AddRef(This->post_data_stream);
1143
1144     *aUploadStream = This->post_data_stream;
1145     return NS_OK;
1146 }
1147
1148 #undef NSUPCHANNEL_THIS
1149
1150 static const nsIUploadChannelVtbl nsUploadChannelVtbl = {
1151     nsUploadChannel_QueryInterface,
1152     nsUploadChannel_AddRef,
1153     nsUploadChannel_Release,
1154     nsUploadChannel_SetUploadStream,
1155     nsUploadChannel_GetUploadStream
1156 };
1157
1158 #define NSURI_THIS(iface) DEFINE_THIS(nsURI, WineURI, iface)
1159
1160 static nsresult NSAPI nsURI_QueryInterface(nsIWineURI *iface, nsIIDRef riid, nsQIResult result)
1161 {
1162     nsURI *This = NSURI_THIS(iface);
1163
1164     *result = NULL;
1165
1166     if(IsEqualGUID(&IID_nsISupports, riid)) {
1167         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1168         *result = NSURI(This);
1169     }else if(IsEqualGUID(&IID_nsIURI, riid)) {
1170         TRACE("(%p)->(IID_nsIURI %p)\n", This, result);
1171         *result = NSURI(This);
1172     }else if(IsEqualGUID(&IID_nsIWineURI, riid)) {
1173         TRACE("(%p)->(IID_nsIWineURI %p)\n", This, result);
1174         *result = NSURI(This);
1175     }
1176
1177     if(*result) {
1178         nsIURI_AddRef(NSURI(This));
1179         return NS_OK;
1180     }
1181
1182     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1183     return This->uri ? nsIURI_QueryInterface(This->uri, riid, result) : NS_NOINTERFACE;
1184 }
1185
1186 static nsrefcnt NSAPI nsURI_AddRef(nsIWineURI *iface)
1187 {
1188     nsURI *This = NSURI_THIS(iface);
1189     LONG ref = InterlockedIncrement(&This->ref);
1190
1191     TRACE("(%p) ref=%d\n", This, ref);
1192
1193     return ref;
1194 }
1195
1196 static nsrefcnt NSAPI nsURI_Release(nsIWineURI *iface)
1197 {
1198     nsURI *This = NSURI_THIS(iface);
1199     LONG ref = InterlockedDecrement(&This->ref);
1200
1201     TRACE("(%p) ref=%d\n", This, ref);
1202
1203     if(!ref) {
1204         if(This->container)
1205             nsIWebBrowserChrome_Release(NSWBCHROME(This->container));
1206         if(This->uri)
1207             nsIURI_Release(This->uri);
1208         heap_free(This->wine_url);
1209         heap_free(This);
1210     }
1211
1212     return ref;
1213 }
1214
1215 static nsresult NSAPI nsURI_GetSpec(nsIWineURI *iface, nsACString *aSpec)
1216 {
1217     nsURI *This = NSURI_THIS(iface);
1218
1219     TRACE("(%p)->(%p)\n", This, aSpec);
1220
1221     if(This->use_wine_url) {
1222         char speca[INTERNET_MAX_URL_LENGTH] = "wine:";
1223         WideCharToMultiByte(CP_ACP, 0, This->wine_url, -1, speca+5, sizeof(speca)-5, NULL, NULL);
1224         nsACString_SetData(aSpec, speca);
1225
1226         return NS_OK;
1227     }
1228
1229     if(This->uri)
1230         return nsIURI_GetSpec(This->uri, aSpec);
1231
1232     TRACE("returning error\n");
1233     return NS_ERROR_NOT_IMPLEMENTED;
1234
1235 }
1236
1237 static nsresult NSAPI nsURI_SetSpec(nsIWineURI *iface, const nsACString *aSpec)
1238 {
1239     nsURI *This = NSURI_THIS(iface);
1240
1241     TRACE("(%p)->(%p)\n", This, aSpec);
1242
1243     if(This->uri)
1244         return nsIURI_SetSpec(This->uri, aSpec);
1245
1246     FIXME("default action not implemented\n");
1247     return NS_ERROR_NOT_IMPLEMENTED;
1248 }
1249
1250 static nsresult NSAPI nsURI_GetPrePath(nsIWineURI *iface, nsACString *aPrePath)
1251 {
1252     nsURI *This = NSURI_THIS(iface);
1253
1254     TRACE("(%p)->(%p)\n", This, aPrePath);
1255
1256     if(This->uri)
1257         return nsIURI_GetPrePath(This->uri, aPrePath);
1258
1259     FIXME("default action not implemented\n");
1260     return NS_ERROR_NOT_IMPLEMENTED;
1261 }
1262
1263 static nsresult NSAPI nsURI_GetScheme(nsIWineURI *iface, nsACString *aScheme)
1264 {
1265     nsURI *This = NSURI_THIS(iface);
1266
1267     TRACE("(%p)->(%p)\n", This, aScheme);
1268
1269     if(This->use_wine_url && strcmpW(This->wine_url, about_blankW)) {
1270         /*
1271          * For Gecko we set scheme to unknown so it won't be handled
1272          * as any special case.
1273          */
1274         nsACString_SetData(aScheme, "wine");
1275         return NS_OK;
1276     }
1277
1278     if(This->uri)
1279         return nsIURI_GetScheme(This->uri, aScheme);
1280
1281     TRACE("returning error\n");
1282     return NS_ERROR_NOT_IMPLEMENTED;
1283 }
1284
1285 static nsresult NSAPI nsURI_SetScheme(nsIWineURI *iface, const nsACString *aScheme)
1286 {
1287     nsURI *This = NSURI_THIS(iface);
1288
1289     TRACE("(%p)->(%p)\n", This, aScheme);
1290
1291     if(This->uri)
1292         return nsIURI_SetScheme(This->uri, aScheme);
1293
1294     FIXME("default action not implemented\n");
1295     return NS_ERROR_NOT_IMPLEMENTED;
1296 }
1297
1298 static nsresult NSAPI nsURI_GetUserPass(nsIWineURI *iface, nsACString *aUserPass)
1299 {
1300     nsURI *This = NSURI_THIS(iface);
1301
1302     TRACE("(%p)->(%p)\n", This, aUserPass);
1303
1304     if(This->uri)
1305         return nsIURI_GetUserPass(This->uri, aUserPass);
1306
1307     FIXME("default action not implemented\n");
1308     return NS_ERROR_NOT_IMPLEMENTED;
1309 }
1310
1311 static nsresult NSAPI nsURI_SetUserPass(nsIWineURI *iface, const nsACString *aUserPass)
1312 {
1313     nsURI *This = NSURI_THIS(iface);
1314
1315     TRACE("(%p)->(%p)\n", This, aUserPass);
1316
1317     if(This->uri)
1318         return nsIURI_SetUserPass(This->uri, aUserPass);
1319
1320     FIXME("default action not implemented\n");
1321     return NS_ERROR_NOT_IMPLEMENTED;
1322 }
1323
1324 static nsresult NSAPI nsURI_GetUsername(nsIWineURI *iface, nsACString *aUsername)
1325 {
1326     nsURI *This = NSURI_THIS(iface);
1327
1328     TRACE("(%p)->(%p)\n", This, aUsername);
1329
1330     if(This->uri)
1331         return nsIURI_GetUsername(This->uri, aUsername);
1332
1333     FIXME("default action not implemented\n");
1334     return NS_ERROR_NOT_IMPLEMENTED;
1335 }
1336
1337 static nsresult NSAPI nsURI_SetUsername(nsIWineURI *iface, const nsACString *aUsername)
1338 {
1339     nsURI *This = NSURI_THIS(iface);
1340
1341     TRACE("(%p)->(%p)\n", This, aUsername);
1342
1343     if(This->uri)
1344         return nsIURI_SetUsername(This->uri, aUsername);
1345
1346     FIXME("default action not implemented\n");
1347     return NS_ERROR_NOT_IMPLEMENTED;
1348 }
1349
1350 static nsresult NSAPI nsURI_GetPassword(nsIWineURI *iface, nsACString *aPassword)
1351 {
1352     nsURI *This = NSURI_THIS(iface);
1353
1354     TRACE("(%p)->(%p)\n", This, aPassword);
1355
1356     if(This->uri)
1357         return nsIURI_GetPassword(This->uri, aPassword);
1358
1359     FIXME("default action not implemented\n");
1360     return NS_ERROR_NOT_IMPLEMENTED;
1361 }
1362
1363 static nsresult NSAPI nsURI_SetPassword(nsIWineURI *iface, const nsACString *aPassword)
1364 {
1365     nsURI *This = NSURI_THIS(iface);
1366
1367     TRACE("(%p)->(%p)\n", This, aPassword);
1368
1369     if(This->uri)
1370         return nsIURI_SetPassword(This->uri, aPassword);
1371
1372     FIXME("default action not implemented\n");
1373     return NS_ERROR_NOT_IMPLEMENTED;
1374 }
1375
1376 static nsresult NSAPI nsURI_GetHostPort(nsIWineURI *iface, nsACString *aHostPort)
1377 {
1378     nsURI *This = NSURI_THIS(iface);
1379
1380     TRACE("(%p)->(%p)\n", This, aHostPort);
1381
1382     if(This->uri)
1383         return nsIURI_GetHostPort(This->uri, aHostPort);
1384
1385     FIXME("default action not implemented\n");
1386     return NS_ERROR_NOT_IMPLEMENTED;
1387 }
1388
1389 static nsresult NSAPI nsURI_SetHostPort(nsIWineURI *iface, const nsACString *aHostPort)
1390 {
1391     nsURI *This = NSURI_THIS(iface);
1392
1393     TRACE("(%p)->(%p)\n", This, aHostPort);
1394
1395     if(This->uri)
1396         return nsIURI_SetHostPort(This->uri, aHostPort);
1397
1398     FIXME("default action not implemented\n");
1399     return NS_ERROR_NOT_IMPLEMENTED;
1400 }
1401
1402 static nsresult NSAPI nsURI_GetHost(nsIWineURI *iface, nsACString *aHost)
1403 {
1404     nsURI *This = NSURI_THIS(iface);
1405
1406     TRACE("(%p)->(%p)\n", This, aHost);
1407
1408     if(This->uri)
1409         return nsIURI_GetHost(This->uri, aHost);
1410
1411     FIXME("default action not implemented\n");
1412     return NS_ERROR_NOT_IMPLEMENTED;
1413 }
1414
1415 static nsresult NSAPI nsURI_SetHost(nsIWineURI *iface, const nsACString *aHost)
1416 {
1417     nsURI *This = NSURI_THIS(iface);
1418
1419     TRACE("(%p)->(%p)\n", This, aHost);
1420
1421     if(This->uri)
1422         return nsIURI_SetHost(This->uri, aHost);
1423
1424     FIXME("default action not implemented\n");
1425     return NS_ERROR_NOT_IMPLEMENTED;
1426 }
1427
1428 static nsresult NSAPI nsURI_GetPort(nsIWineURI *iface, PRInt32 *aPort)
1429 {
1430     nsURI *This = NSURI_THIS(iface);
1431
1432     TRACE("(%p)->(%p)\n", This, aPort);
1433
1434     if(This->uri)
1435         return nsIURI_GetPort(This->uri, aPort);
1436
1437     FIXME("default action not implemented\n");
1438     return NS_ERROR_NOT_IMPLEMENTED;
1439 }
1440
1441 static nsresult NSAPI nsURI_SetPort(nsIWineURI *iface, PRInt32 aPort)
1442 {
1443     nsURI *This = NSURI_THIS(iface);
1444
1445     TRACE("(%p)->(%d)\n", This, aPort);
1446
1447     if(This->uri)
1448         return nsIURI_SetPort(This->uri, aPort);
1449
1450     FIXME("default action not implemented\n");
1451     return NS_ERROR_NOT_IMPLEMENTED;
1452 }
1453
1454 static nsresult NSAPI nsURI_GetPath(nsIWineURI *iface, nsACString *aPath)
1455 {
1456     nsURI *This = NSURI_THIS(iface);
1457
1458     TRACE("(%p)->(%p)\n", This, aPath);
1459
1460     if(This->uri)
1461         return nsIURI_GetPath(This->uri, aPath);
1462
1463     FIXME("default action not implemented\n");
1464     return NS_ERROR_NOT_IMPLEMENTED;
1465 }
1466
1467 static nsresult NSAPI nsURI_SetPath(nsIWineURI *iface, const nsACString *aPath)
1468 {
1469     nsURI *This = NSURI_THIS(iface);
1470     const char *path;
1471
1472     nsACString_GetData(aPath, &path);
1473     TRACE("(%p)->(%p(%s))\n", This, aPath, debugstr_a(path));
1474
1475
1476     if(This->wine_url) {
1477         WCHAR new_url[INTERNET_MAX_URL_LENGTH];
1478         DWORD size = sizeof(new_url)/sizeof(WCHAR);
1479         LPWSTR pathw;
1480         HRESULT hres;
1481
1482         pathw = heap_strdupAtoW(path);
1483         hres = UrlCombineW(This->wine_url, pathw, new_url, &size, 0);
1484         heap_free(pathw);
1485         if(SUCCEEDED(hres))
1486             nsIWineURI_SetWineURL(NSWINEURI(This), new_url);
1487         else
1488             WARN("UrlCombine failed: %08x\n", hres);
1489     }
1490
1491     if(!This->uri)
1492         return NS_OK;
1493
1494     return nsIURI_SetPath(This->uri, aPath);
1495 }
1496
1497 static nsresult NSAPI nsURI_Equals(nsIWineURI *iface, nsIURI *other, PRBool *_retval)
1498 {
1499     nsURI *This = NSURI_THIS(iface);
1500
1501     TRACE("(%p)->(%p %p)\n", This, other, _retval);
1502
1503     if(This->uri)
1504         return nsIURI_Equals(This->uri, other, _retval);
1505
1506     FIXME("default action not implemented\n");
1507     return NS_ERROR_NOT_IMPLEMENTED;
1508 }
1509
1510 static nsresult NSAPI nsURI_SchemeIs(nsIWineURI *iface, const char *scheme, PRBool *_retval)
1511 {
1512     nsURI *This = NSURI_THIS(iface);
1513
1514     TRACE("(%p)->(%s %p)\n", This, debugstr_a(scheme), _retval);
1515
1516     if(This->use_wine_url) {
1517         WCHAR buf[INTERNET_MAX_SCHEME_LENGTH];
1518         int len = MultiByteToWideChar(CP_ACP, 0, scheme, -1, buf, sizeof(buf)/sizeof(WCHAR))-1;
1519
1520         *_retval = strlenW(This->wine_url) > len
1521             && This->wine_url[len] == ':'
1522             && !memcmp(buf, This->wine_url, len*sizeof(WCHAR));
1523         return NS_OK;
1524     }
1525
1526     if(This->uri)
1527         return nsIURI_SchemeIs(This->uri, scheme, _retval);
1528
1529     TRACE("returning error\n");
1530     return NS_ERROR_NOT_IMPLEMENTED;
1531 }
1532
1533 static nsresult NSAPI nsURI_Clone(nsIWineURI *iface, nsIURI **_retval)
1534 {
1535     nsURI *This = NSURI_THIS(iface);
1536
1537     TRACE("(%p)->(%p)\n", This, _retval);
1538
1539     if(This->uri) {
1540         nsIURI *uri;
1541         nsIWineURI *wine_uri;
1542         nsresult nsres;
1543
1544         nsres = nsIURI_Clone(This->uri, &uri);
1545         if(NS_FAILED(nsres)) {
1546             WARN("Clone failed: %08x\n", nsres);
1547             return nsres;
1548         }
1549
1550         nsres = create_uri(uri, This->container, &wine_uri);
1551         *_retval = (nsIURI*)wine_uri;
1552         if(NS_SUCCEEDED(nsres))
1553             return nsIWineURI_SetWineURL(wine_uri, This->wine_url);
1554         return nsres;
1555     }
1556
1557     FIXME("default action not implemented\n");
1558     return NS_ERROR_NOT_IMPLEMENTED;
1559 }
1560
1561 static nsresult NSAPI nsURI_Resolve(nsIWineURI *iface, const nsACString *arelativePath,
1562         nsACString *_retval)
1563 {
1564     nsURI *This = NSURI_THIS(iface);
1565
1566     TRACE("(%p)->(%p %p)\n", This, arelativePath, _retval);
1567
1568     if(This->uri)
1569         return nsIURI_Resolve(This->uri, arelativePath, _retval);
1570
1571     FIXME("default action not implemented\n");
1572     return NS_ERROR_NOT_IMPLEMENTED;
1573 }
1574
1575 static nsresult NSAPI nsURI_GetAsciiSpec(nsIWineURI *iface, nsACString *aAsciiSpec)
1576 {
1577     nsURI *This = NSURI_THIS(iface);
1578
1579     TRACE("(%p)->(%p)\n", This, aAsciiSpec);
1580
1581     if(This->use_wine_url)
1582         return nsIURI_GetSpec(NSURI(This), aAsciiSpec);
1583
1584     if(This->uri)
1585         return nsIURI_GetAsciiSpec(This->uri, aAsciiSpec);
1586
1587     TRACE("returning error\n");
1588     return NS_ERROR_NOT_IMPLEMENTED;
1589 }
1590
1591 static nsresult NSAPI nsURI_GetAsciiHost(nsIWineURI *iface, nsACString *aAsciiHost)
1592 {
1593     nsURI *This = NSURI_THIS(iface);
1594
1595     TRACE("(%p)->(%p)\n", This, aAsciiHost);
1596
1597     if(This->uri)
1598         return nsIURI_GetAsciiHost(This->uri, aAsciiHost);
1599
1600     FIXME("default action not implemented\n");
1601     return NS_ERROR_NOT_IMPLEMENTED;
1602 }
1603
1604 static nsresult NSAPI nsURI_GetOriginCharset(nsIWineURI *iface, nsACString *aOriginCharset)
1605 {
1606     nsURI *This = NSURI_THIS(iface);
1607
1608     TRACE("(%p)->(%p)\n", This, aOriginCharset);
1609
1610     if(This->uri)
1611         return nsIURI_GetOriginCharset(This->uri, aOriginCharset);
1612
1613     FIXME("default action not implemented\n");
1614     return NS_ERROR_NOT_IMPLEMENTED;
1615 }
1616
1617 static nsresult NSAPI nsURI_GetNSContainer(nsIWineURI *iface, NSContainer **aContainer)
1618 {
1619     nsURI *This = NSURI_THIS(iface);
1620
1621     TRACE("(%p)->(%p)\n", This, aContainer);
1622
1623     if(This->container)
1624         nsIWebBrowserChrome_AddRef(NSWBCHROME(This->container));
1625     *aContainer = This->container;
1626
1627     return NS_OK;
1628 }
1629
1630 static nsresult NSAPI nsURI_SetNSContainer(nsIWineURI *iface, NSContainer *aContainer)
1631 {
1632     nsURI *This = NSURI_THIS(iface);
1633
1634     TRACE("(%p)->(%p)\n", This, aContainer);
1635
1636     if(This->container) {
1637         if(This->container == aContainer)
1638             return NS_OK;
1639         TRACE("Changing %p -> %p\n", This->container, aContainer);
1640         nsIWebBrowserChrome_Release(NSWBCHROME(This->container));
1641     }
1642
1643     if(aContainer)
1644         nsIWebBrowserChrome_AddRef(NSWBCHROME(aContainer));
1645     This->container = aContainer;
1646
1647     return NS_OK;
1648 }
1649
1650 static nsresult NSAPI nsURI_GetIsDocumentURI(nsIWineURI *iface, PRBool *aIsDocumentURI)
1651 {
1652     nsURI *This = NSURI_THIS(iface);
1653
1654     TRACE("(%p)->(%p)\n", This, aIsDocumentURI);
1655
1656     *aIsDocumentURI = This->is_doc_uri;
1657     return NS_OK;
1658 }
1659
1660 static nsresult NSAPI nsURI_SetIsDocumentURI(nsIWineURI *iface, PRBool aIsDocumentURI)
1661 {
1662     nsURI *This = NSURI_THIS(iface);
1663
1664     TRACE("(%p)->(%x)\n", This, aIsDocumentURI);
1665
1666     This->is_doc_uri = aIsDocumentURI;
1667     return NS_OK;
1668 }
1669
1670 static nsresult NSAPI nsURI_GetWineURL(nsIWineURI *iface, LPCWSTR *aURL)
1671 {
1672     nsURI *This = NSURI_THIS(iface);
1673
1674     TRACE("(%p)->(%p)\n", This, aURL);
1675
1676     *aURL = This->wine_url;
1677     return NS_OK;
1678 }
1679
1680 static nsresult NSAPI nsURI_SetWineURL(nsIWineURI *iface, LPCWSTR aURL)
1681 {
1682     nsURI *This = NSURI_THIS(iface);
1683
1684     static const WCHAR wszFtp[]   = {'f','t','p',':'};
1685     static const WCHAR wszHttp[]  = {'h','t','t','p',':'};
1686     static const WCHAR wszHttps[] = {'h','t','t','p','s',':'};
1687
1688     TRACE("(%p)->(%s)\n", This, debugstr_w(aURL));
1689
1690     heap_free(This->wine_url);
1691
1692     if(aURL) {
1693         int len = strlenW(aURL)+1;
1694         This->wine_url = heap_alloc(len*sizeof(WCHAR));
1695         memcpy(This->wine_url, aURL, len*sizeof(WCHAR));
1696
1697         /* FIXME: Always use wine url */
1698         This->use_wine_url =
1699                strncmpW(aURL, wszFtp,   sizeof(wszFtp)/sizeof(WCHAR))
1700             && strncmpW(aURL, wszHttp,  sizeof(wszHttp)/sizeof(WCHAR))
1701             && strncmpW(aURL, wszHttps, sizeof(wszHttps)/sizeof(WCHAR));
1702     }else {
1703         This->wine_url = NULL;
1704         This->use_wine_url = FALSE;
1705     }
1706
1707     return NS_OK;
1708 }
1709
1710 #undef NSURI_THIS
1711
1712 static const nsIWineURIVtbl nsWineURIVtbl = {
1713     nsURI_QueryInterface,
1714     nsURI_AddRef,
1715     nsURI_Release,
1716     nsURI_GetSpec,
1717     nsURI_SetSpec,
1718     nsURI_GetPrePath,
1719     nsURI_GetScheme,
1720     nsURI_SetScheme,
1721     nsURI_GetUserPass,
1722     nsURI_SetUserPass,
1723     nsURI_GetUsername,
1724     nsURI_SetUsername,
1725     nsURI_GetPassword,
1726     nsURI_SetPassword,
1727     nsURI_GetHostPort,
1728     nsURI_SetHostPort,
1729     nsURI_GetHost,
1730     nsURI_SetHost,
1731     nsURI_GetPort,
1732     nsURI_SetPort,
1733     nsURI_GetPath,
1734     nsURI_SetPath,
1735     nsURI_Equals,
1736     nsURI_SchemeIs,
1737     nsURI_Clone,
1738     nsURI_Resolve,
1739     nsURI_GetAsciiSpec,
1740     nsURI_GetAsciiHost,
1741     nsURI_GetOriginCharset,
1742     nsURI_GetNSContainer,
1743     nsURI_SetNSContainer,
1744     nsURI_GetIsDocumentURI,
1745     nsURI_SetIsDocumentURI,
1746     nsURI_GetWineURL,
1747     nsURI_SetWineURL
1748 };
1749
1750 static nsresult create_uri(nsIURI *uri, NSContainer *container, nsIWineURI **_retval)
1751 {
1752     nsURI *ret = heap_alloc(sizeof(nsURI));
1753
1754     ret->lpWineURIVtbl = &nsWineURIVtbl;
1755     ret->ref = 1;
1756     ret->uri = uri;
1757     ret->container = container;
1758     ret->wine_url = NULL;
1759     ret->is_doc_uri = FALSE;
1760     ret->use_wine_url = FALSE;
1761
1762     if(container)
1763         nsIWebBrowserChrome_AddRef(NSWBCHROME(container));
1764
1765     TRACE("retval=%p\n", ret);
1766     *_retval = NSWINEURI(ret);
1767     return NS_OK;
1768 }
1769
1770 typedef struct {
1771     const nsIProtocolHandlerVtbl  *lpProtocolHandlerVtbl;
1772
1773     LONG ref;
1774
1775     nsIProtocolHandler *nshandler;
1776 } nsProtocolHandler;
1777
1778 #define NSPROTHANDLER(x)  ((nsIProtocolHandler*)  &(x)->lpProtocolHandlerVtbl)
1779
1780 #define NSPROTHANDLER_THIS(iface) DEFINE_THIS(nsProtocolHandler, ProtocolHandler, iface)
1781
1782 static nsresult NSAPI nsProtocolHandler_QueryInterface(nsIProtocolHandler *iface, nsIIDRef riid,
1783         nsQIResult result)
1784 {
1785     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1786
1787     *result = NULL;
1788
1789     if(IsEqualGUID(&IID_nsISupports, riid)) {
1790         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1791         *result = NSPROTHANDLER(This);
1792     }else if(IsEqualGUID(&IID_nsIProtocolHandler, riid)) {
1793         TRACE("(%p)->(IID_nsIProtocolHandler %p)\n", This, result);
1794         *result = NSPROTHANDLER(This);
1795     }else if(IsEqualGUID(&IID_nsIExternalProtocolHandler, riid)) {
1796         TRACE("(%p)->(IID_nsIExternalProtocolHandler %p), returning NULL\n", This, result);
1797         return NS_NOINTERFACE;
1798     }
1799
1800     if(*result) {
1801         nsISupports_AddRef((nsISupports*)*result);
1802         return NS_OK;
1803     }
1804
1805     WARN("(%s %p)\n", debugstr_guid(riid), result);
1806     return NS_NOINTERFACE;
1807 }
1808
1809 static nsrefcnt NSAPI nsProtocolHandler_AddRef(nsIProtocolHandler *iface)
1810 {
1811     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1812     LONG ref = InterlockedIncrement(&This->ref);
1813
1814     TRACE("(%p) ref=%d\n", This, ref);
1815
1816     return ref;
1817 }
1818
1819 static nsrefcnt NSAPI nsProtocolHandler_Release(nsIProtocolHandler *iface)
1820 {
1821     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1822     LONG ref = InterlockedDecrement(&This->ref);
1823
1824     TRACE("(%p) ref=%d\n", This, ref);
1825
1826     if(!ref) {
1827         if(This->nshandler)
1828             nsIProtocolHandler_Release(This->nshandler);
1829         heap_free(This);
1830     }
1831
1832     return ref;
1833 }
1834
1835 static nsresult NSAPI nsProtocolHandler_GetScheme(nsIProtocolHandler *iface, nsACString *aScheme)
1836 {
1837     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1838
1839     TRACE("(%p)->(%p)\n", This, aScheme);
1840
1841     if(This->nshandler)
1842         return nsIProtocolHandler_GetScheme(This->nshandler, aScheme);
1843     return NS_ERROR_NOT_IMPLEMENTED;
1844 }
1845
1846 static nsresult NSAPI nsProtocolHandler_GetDefaultPort(nsIProtocolHandler *iface,
1847         PRInt32 *aDefaultPort)
1848 {
1849     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1850
1851     TRACE("(%p)->(%p)\n", This, aDefaultPort);
1852
1853     if(This->nshandler)
1854         return nsIProtocolHandler_GetDefaultPort(This->nshandler, aDefaultPort);
1855     return NS_ERROR_NOT_IMPLEMENTED;
1856 }
1857
1858 static nsresult NSAPI nsProtocolHandler_GetProtocolFlags(nsIProtocolHandler *iface,
1859                                                          PRUint32 *aProtocolFlags)
1860 {
1861     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1862
1863     TRACE("(%p)->(%p)\n", This, aProtocolFlags);
1864
1865     if(This->nshandler)
1866         return nsIProtocolHandler_GetProtocolFlags(This->nshandler, aProtocolFlags);
1867     return NS_ERROR_NOT_IMPLEMENTED;
1868 }
1869
1870 static nsresult NSAPI nsProtocolHandler_NewURI(nsIProtocolHandler *iface,
1871         const nsACString *aSpec, const char *aOriginCharset, nsIURI *aBaseURI, nsIURI **_retval)
1872 {
1873     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1874
1875     TRACE("((%p)->%p %s %p %p)\n", This, aSpec, debugstr_a(aOriginCharset), aBaseURI, _retval);
1876
1877     if(This->nshandler)
1878         return nsIProtocolHandler_NewURI(This->nshandler, aSpec, aOriginCharset, aBaseURI, _retval);
1879     return NS_ERROR_NOT_IMPLEMENTED;
1880 }
1881
1882 static nsresult NSAPI nsProtocolHandler_NewChannel(nsIProtocolHandler *iface,
1883         nsIURI *aURI, nsIChannel **_retval)
1884 {
1885     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1886
1887     TRACE("(%p)->(%p %p)\n", This, aURI, _retval);
1888
1889     if(This->nshandler)
1890         return nsIProtocolHandler_NewChannel(This->nshandler, aURI, _retval);
1891     return NS_ERROR_NOT_IMPLEMENTED;
1892 }
1893
1894 static nsresult NSAPI nsProtocolHandler_AllowPort(nsIProtocolHandler *iface,
1895         PRInt32 port, const char *scheme, PRBool *_retval)
1896 {
1897     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
1898
1899     TRACE("(%p)->(%d %s %p)\n", This, port, debugstr_a(scheme), _retval);
1900
1901     if(This->nshandler)
1902         return nsIProtocolHandler_AllowPort(This->nshandler, port, scheme, _retval);
1903     return NS_ERROR_NOT_IMPLEMENTED;
1904 }
1905
1906 #undef NSPROTHANDLER_THIS
1907
1908 static const nsIProtocolHandlerVtbl nsProtocolHandlerVtbl = {
1909     nsProtocolHandler_QueryInterface,
1910     nsProtocolHandler_AddRef,
1911     nsProtocolHandler_Release,
1912     nsProtocolHandler_GetScheme,
1913     nsProtocolHandler_GetDefaultPort,
1914     nsProtocolHandler_GetProtocolFlags,
1915     nsProtocolHandler_NewURI,
1916     nsProtocolHandler_NewChannel,
1917     nsProtocolHandler_AllowPort
1918 };
1919
1920 static nsIProtocolHandler *create_protocol_handler(nsIProtocolHandler *nshandler)
1921 {
1922     nsProtocolHandler *ret = heap_alloc(sizeof(nsProtocolHandler));
1923
1924     ret->lpProtocolHandlerVtbl = &nsProtocolHandlerVtbl;
1925     ret->ref = 1;
1926     ret->nshandler = nshandler;
1927
1928     return NSPROTHANDLER(ret);
1929 }
1930
1931 static nsresult NSAPI nsIOService_QueryInterface(nsIIOService*,nsIIDRef,nsQIResult);
1932
1933 static nsrefcnt NSAPI nsIOService_AddRef(nsIIOService *iface)
1934 {
1935     return 2;
1936 }
1937
1938 static nsrefcnt NSAPI nsIOService_Release(nsIIOService *iface)
1939 {
1940     return 1;
1941 }
1942
1943 static nsresult NSAPI nsIOService_GetProtocolHandler(nsIIOService *iface, const char *aScheme,
1944                                                      nsIProtocolHandler **_retval)
1945 {
1946     nsIExternalProtocolHandler *nsexthandler;
1947     nsIProtocolHandler *nshandler;
1948     nsresult nsres;
1949
1950     TRACE("(%s %p)\n", debugstr_a(aScheme), _retval);
1951
1952     nsres = nsIIOService_GetProtocolHandler(nsio, aScheme, &nshandler);
1953     if(NS_FAILED(nsres)) {
1954         WARN("GetProtocolHandler failed: %08x\n", nsres);
1955         return nsres;
1956     }
1957
1958     nsres = nsIProtocolHandler_QueryInterface(nshandler, &IID_nsIExternalProtocolHandler,
1959                                               (void**)&nsexthandler);
1960     if(NS_FAILED(nsres)) {
1961         *_retval = nshandler;
1962         return NS_OK;
1963     }
1964
1965     nsIExternalProtocolHandler_Release(nsexthandler);
1966     *_retval = create_protocol_handler(nshandler);
1967     TRACE("return %p\n", *_retval);
1968     return NS_OK;
1969 }
1970
1971 static nsresult NSAPI nsIOService_GetProtocolFlags(nsIIOService *iface, const char *aScheme,
1972                                                     PRUint32 *_retval)
1973 {
1974     TRACE("(%s %p)\n", debugstr_a(aScheme), _retval);
1975     return nsIIOService_GetProtocolFlags(nsio, aScheme, _retval);
1976 }
1977
1978 static BOOL is_gecko_special_uri(const char *spec)
1979 {
1980     static const char chromeW[] = "chrome:";
1981     static const char jarW[] = "jar:";
1982     static const char resourceW[] = "resource:";
1983     static const char javascriptW[] = "javascript:";
1984
1985     return !strncasecmp(spec, chromeW,     sizeof(chromeW)-1)
1986         || !strncasecmp(spec, resourceW,   sizeof(resourceW)-1)
1987         || !strncasecmp(spec, jarW,        sizeof(jarW)-1)
1988         || !strncasecmp(spec, javascriptW, sizeof(javascriptW)-1);
1989 }
1990
1991 static nsresult NSAPI nsIOService_NewURI(nsIIOService *iface, const nsACString *aSpec,
1992         const char *aOriginCharset, nsIURI *aBaseURI, nsIURI **_retval)
1993 {
1994     const char *spec = NULL;
1995     nsACString spec_str;
1996     NSContainer *nscontainer = NULL;
1997     nsIURI *uri = NULL;
1998     LPCWSTR base_wine_url = NULL;
1999     nsIWineURI *base_wine_uri = NULL, *wine_uri;
2000     BOOL is_wine_uri = FALSE;
2001     nsresult nsres;
2002
2003     nsACString_GetData(aSpec, &spec);
2004
2005     TRACE("(%p(%s) %s %p %p)\n", aSpec, debugstr_a(spec), debugstr_a(aOriginCharset),
2006           aBaseURI, _retval);
2007
2008     if(is_gecko_special_uri(spec))
2009         return nsIIOService_NewURI(nsio, aSpec, aOriginCharset, aBaseURI, _retval);
2010
2011     if(!strncmp(spec, "wine:", 5)) {
2012         spec += 5;
2013         is_wine_uri = TRUE;
2014     }
2015
2016     if(aBaseURI) {
2017         nsACString base_uri_str;
2018         const char *base_uri = NULL;
2019
2020         nsACString_Init(&base_uri_str, NULL);
2021
2022         nsres = nsIURI_GetSpec(aBaseURI, &base_uri_str);
2023         if(NS_SUCCEEDED(nsres)) {
2024             nsACString_GetData(&base_uri_str, &base_uri);
2025             TRACE("base_uri=%s\n", debugstr_a(base_uri));
2026         }else {
2027             ERR("GetSpec failed: %08x\n", nsres);
2028         }
2029
2030         nsACString_Finish(&base_uri_str);
2031     }
2032
2033     nsACString_Init(&spec_str, spec);
2034     nsres = nsIIOService_NewURI(nsio, aSpec, aOriginCharset, aBaseURI, &uri);
2035     nsACString_Finish(&spec_str);
2036     if(NS_FAILED(nsres))
2037         TRACE("NewURI failed: %08x\n", nsres);
2038
2039     if(aBaseURI) {
2040         nsres = nsIURI_QueryInterface(aBaseURI, &IID_nsIWineURI, (void**)&base_wine_uri);
2041         if(NS_SUCCEEDED(nsres)) {
2042             nsIWineURI_GetNSContainer(base_wine_uri, &nscontainer);
2043             nsIWineURI_GetWineURL(base_wine_uri, &base_wine_url);
2044         }else {
2045             TRACE("Could not get base nsIWineURI: %08x\n", nsres);
2046         }
2047     }
2048
2049     nsres = create_uri(uri, nscontainer, &wine_uri);
2050     *_retval = (nsIURI*)wine_uri;
2051
2052     if(nscontainer)
2053         nsIWebBrowserChrome_Release(NSWBCHROME(nscontainer));
2054
2055     if(base_wine_url) {
2056         WCHAR url[INTERNET_MAX_URL_LENGTH], rel_url[INTERNET_MAX_URL_LENGTH];
2057         DWORD len;
2058         HRESULT hres;
2059
2060         MultiByteToWideChar(CP_ACP, 0, spec, -1, rel_url, sizeof(rel_url)/sizeof(WCHAR));
2061
2062         hres = CoInternetCombineUrl(base_wine_url, rel_url,
2063                                     URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO,
2064                                     url, sizeof(url)/sizeof(WCHAR), &len, 0);
2065         if(SUCCEEDED(hres))
2066             nsIWineURI_SetWineURL(wine_uri, url);
2067         else
2068              WARN("CoCombineUrl failed: %08x\n", hres);
2069     }else if(is_wine_uri) {
2070         WCHAR url[INTERNET_MAX_URL_LENGTH];
2071
2072         MultiByteToWideChar(CP_ACP, 0, spec, -1, url, sizeof(url)/sizeof(WCHAR));
2073         nsIWineURI_SetWineURL(wine_uri, url);
2074     }
2075
2076     if(base_wine_uri)
2077         nsIWineURI_Release(base_wine_uri);
2078
2079     return nsres;
2080 }
2081
2082 static nsresult NSAPI nsIOService_NewFileURI(nsIIOService *iface, nsIFile *aFile,
2083                                              nsIURI **_retval)
2084 {
2085     TRACE("(%p %p)\n", aFile, _retval);
2086     return nsIIOService_NewFileURI(nsio, aFile, _retval);
2087 }
2088
2089 static nsresult NSAPI nsIOService_NewChannelFromURI(nsIIOService *iface, nsIURI *aURI,
2090                                                      nsIChannel **_retval)
2091 {
2092     nsIChannel *channel = NULL;
2093     nsChannel *ret;
2094     nsIWineURI *wine_uri;
2095     nsresult nsres;
2096
2097     TRACE("(%p %p)\n", aURI, _retval);
2098
2099     nsres = nsIIOService_NewChannelFromURI(nsio, aURI, &channel);
2100     if(NS_FAILED(nsres) && nsres != NS_ERROR_UNKNOWN_PROTOCOL) {
2101         WARN("NewChannelFromURI failed: %08x\n", nsres);
2102         *_retval = channel;
2103         return nsres;
2104     }
2105
2106     nsres = nsIURI_QueryInterface(aURI, &IID_nsIWineURI, (void**)&wine_uri);
2107     if(NS_FAILED(nsres)) {
2108         WARN("Could not get nsIWineURI: %08x\n", nsres);
2109         *_retval = channel;
2110         return channel ? NS_OK : NS_ERROR_UNEXPECTED;
2111     }
2112
2113     ret = heap_alloc(sizeof(nsChannel));
2114
2115     ret->lpHttpChannelVtbl = &nsChannelVtbl;
2116     ret->lpUploadChannelVtbl = &nsUploadChannelVtbl;
2117     ret->ref = 1;
2118     ret->channel = channel;
2119     ret->http_channel = NULL;
2120     ret->uri = wine_uri;
2121     ret->post_data_stream = NULL;
2122     ret->load_group = NULL;
2123     ret->notif_callback = NULL;
2124     ret->load_flags = 0;
2125     ret->content = NULL;
2126     ret->charset = NULL;
2127
2128     nsIURI_AddRef(aURI);
2129     ret->original_uri = aURI;
2130
2131     if(channel)
2132         nsIChannel_QueryInterface(channel, &IID_nsIHttpChannel, (void**)&ret->http_channel);
2133
2134     *_retval = NSCHANNEL(ret);
2135     return NS_OK;
2136 }
2137
2138 static nsresult NSAPI nsIOService_NewChannel(nsIIOService *iface, const nsACString *aSpec,
2139         const char *aOriginCharset, nsIURI *aBaseURI, nsIChannel **_retval)
2140 {
2141     TRACE("(%p %s %p %p)\n", aSpec, debugstr_a(aOriginCharset), aBaseURI, _retval);
2142     return nsIIOService_NewChannel(nsio, aSpec, aOriginCharset, aBaseURI, _retval);
2143 }
2144
2145 static nsresult NSAPI nsIOService_GetOffline(nsIIOService *iface, PRBool *aOffline)
2146 {
2147     TRACE("(%p)\n", aOffline);
2148     return nsIIOService_GetOffline(nsio, aOffline);
2149 }
2150
2151 static nsresult NSAPI nsIOService_SetOffline(nsIIOService *iface, PRBool aOffline)
2152 {
2153     TRACE("(%x)\n", aOffline);
2154     return nsIIOService_SetOffline(nsio, aOffline);
2155 }
2156
2157 static nsresult NSAPI nsIOService_AllowPort(nsIIOService *iface, PRInt32 aPort,
2158                                              const char *aScheme, PRBool *_retval)
2159 {
2160     TRACE("(%d %s %p)\n", aPort, debugstr_a(aScheme), _retval);
2161     return nsIIOService_AllowPort(nsio, aPort, debugstr_a(aScheme), _retval);
2162 }
2163
2164 static nsresult NSAPI nsIOService_ExtractScheme(nsIIOService *iface, const nsACString *urlString,
2165                                                  nsACString * _retval)
2166 {
2167     TRACE("(%p %p)\n", urlString, _retval);
2168     return nsIIOService_ExtractScheme(nsio, urlString, _retval);
2169 }
2170
2171 static const nsIIOServiceVtbl nsIOServiceVtbl = {
2172     nsIOService_QueryInterface,
2173     nsIOService_AddRef,
2174     nsIOService_Release,
2175     nsIOService_GetProtocolHandler,
2176     nsIOService_GetProtocolFlags,
2177     nsIOService_NewURI,
2178     nsIOService_NewFileURI,
2179     nsIOService_NewChannelFromURI,
2180     nsIOService_NewChannel,
2181     nsIOService_GetOffline,
2182     nsIOService_SetOffline,
2183     nsIOService_AllowPort,
2184     nsIOService_ExtractScheme
2185 };
2186
2187 static nsIIOService nsIOService = { &nsIOServiceVtbl };
2188
2189 static nsresult NSAPI nsNetUtil_QueryInterface(nsINetUtil *iface, nsIIDRef riid,
2190                                                nsQIResult result)
2191 {
2192     return nsIIOService_QueryInterface(&nsIOService, riid, result);
2193 }
2194
2195 static nsrefcnt NSAPI nsNetUtil_AddRef(nsINetUtil *iface)
2196 {
2197     return 2;
2198 }
2199
2200 static nsrefcnt NSAPI nsNetUtil_Release(nsINetUtil *iface)
2201 {
2202     return 1;
2203 }
2204
2205 static nsresult NSAPI nsNetUtil_ParseContentType(nsINetUtil *iface, const nsACString *aTypeHeader,
2206         nsACString *aCharset, PRBool *aHadCharset, nsACString *aContentType)
2207 {
2208     nsINetUtil *net_util;
2209     nsresult nsres;
2210
2211     TRACE("(%p %p %p %p)\n", aTypeHeader, aCharset, aHadCharset, aContentType);
2212
2213     nsres = nsIIOService_QueryInterface(nsio, &IID_nsINetUtil, (void**)&net_util);
2214     if(NS_FAILED(nsres)) {
2215         WARN("Could not get nsINetUtil interface: %08x\n", nsres);
2216         return nsres;
2217     }
2218
2219     nsres = nsINetUtil_ParseContentType(net_util, aTypeHeader, aCharset, aHadCharset, aContentType);
2220
2221     nsINetUtil_Release(net_util);
2222     return nsres;
2223 }
2224
2225 static const nsINetUtilVtbl nsNetUtilVtbl = {
2226     nsNetUtil_QueryInterface,
2227     nsNetUtil_AddRef,
2228     nsNetUtil_Release,
2229     nsNetUtil_ParseContentType
2230 };
2231
2232 static nsINetUtil nsNetUtil = { &nsNetUtilVtbl };
2233
2234 static nsresult NSAPI nsIOService_QueryInterface(nsIIOService *iface, nsIIDRef riid,
2235                                                  nsQIResult result)
2236 {
2237     *result = NULL;
2238
2239     if(IsEqualGUID(&IID_nsISupports, riid))
2240         *result = &nsIOService;
2241     else if(IsEqualGUID(&IID_nsIIOService, riid))
2242         *result = &nsIOService;
2243     else if(IsEqualGUID(&IID_nsINetUtil, riid))
2244         *result = &nsNetUtil;
2245
2246     if(*result) {
2247         nsISupports_AddRef((nsISupports*)*result);
2248         return NS_OK;
2249     }
2250
2251     FIXME("(%s %p)\n", debugstr_guid(riid), result);
2252     return NS_NOINTERFACE;
2253 }
2254
2255 static nsresult NSAPI nsIOServiceFactory_QueryInterface(nsIFactory *iface, nsIIDRef riid,
2256                                                         nsQIResult result)
2257 {
2258     *result = NULL;
2259
2260     if(IsEqualGUID(&IID_nsISupports, riid)) {
2261         TRACE("(IID_nsISupports %p)\n", result);
2262         *result = iface;
2263     }else if(IsEqualGUID(&IID_nsIFactory, riid)) {
2264         TRACE("(IID_nsIFactory %p)\n", result);
2265         *result = iface;
2266     }
2267
2268     if(*result) {
2269         nsIFactory_AddRef(iface);
2270         return NS_OK;
2271     }
2272
2273     WARN("(%s %p)\n", debugstr_guid(riid), result);
2274     return NS_NOINTERFACE;
2275 }
2276
2277 static nsrefcnt NSAPI nsIOServiceFactory_AddRef(nsIFactory *iface)
2278 {
2279     return 2;
2280 }
2281
2282 static nsrefcnt NSAPI nsIOServiceFactory_Release(nsIFactory *iface)
2283 {
2284     return 1;
2285 }
2286
2287 static nsresult NSAPI nsIOServiceFactory_CreateInstance(nsIFactory *iface,
2288         nsISupports *aOuter, const nsIID *iid, void **result)
2289 {
2290     return nsIIOService_QueryInterface(&nsIOService, iid, result);
2291 }
2292
2293 static nsresult NSAPI nsIOServiceFactory_LockFactory(nsIFactory *iface, PRBool lock)
2294 {
2295     WARN("(%x)\n", lock);
2296     return NS_OK;
2297 }
2298
2299 static const nsIFactoryVtbl nsIOServiceFactoryVtbl = {
2300     nsIOServiceFactory_QueryInterface,
2301     nsIOServiceFactory_AddRef,
2302     nsIOServiceFactory_Release,
2303     nsIOServiceFactory_CreateInstance,
2304     nsIOServiceFactory_LockFactory
2305 };
2306
2307 static nsIFactory nsIOServiceFactory = { &nsIOServiceFactoryVtbl };
2308
2309 void init_nsio(nsIComponentManager *component_manager, nsIComponentRegistrar *registrar)
2310 {
2311     nsIFactory *old_factory = NULL;
2312     nsresult nsres;
2313
2314     nsres = nsIComponentManager_GetClassObject(component_manager, &NS_IOSERVICE_CID,
2315                                                &IID_nsIFactory, (void**)&old_factory);
2316     if(NS_FAILED(nsres)) {
2317         ERR("Could not get factory: %08x\n", nsres);
2318         return;
2319     }
2320
2321     nsres = nsIFactory_CreateInstance(old_factory, NULL, &IID_nsIIOService, (void**)&nsio);
2322     if(NS_FAILED(nsres)) {
2323         ERR("Couldn not create nsIOService instance %08x\n", nsres);
2324         nsIFactory_Release(old_factory);
2325         return;
2326     }
2327
2328     nsres = nsIComponentRegistrar_UnregisterFactory(registrar, &NS_IOSERVICE_CID, old_factory);
2329     nsIFactory_Release(old_factory);
2330     if(NS_FAILED(nsres))
2331         ERR("UnregisterFactory failed: %08x\n", nsres);
2332
2333     nsres = nsIComponentRegistrar_RegisterFactory(registrar, &NS_IOSERVICE_CID,
2334             NS_IOSERVICE_CLASSNAME, NS_IOSERVICE_CONTRACTID, &nsIOServiceFactory);
2335     if(NS_FAILED(nsres))
2336         ERR("RegisterFactory failed: %08x\n", nsres);
2337 }