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