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