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