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