mshtml: Remove no longer used get_nsIURI.
[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=%ld\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)->(%08lx)\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)->(%08lx)\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)->(%ld)\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                 return WINE_NS_LOAD_FROM_MONIKER;
607         }else {
608             BOOL cont = before_async_open(This, container);
609             nsIWebBrowserChrome_Release(NSWBCHROME(container));
610
611             if(!cont) {
612                 TRACE("canceled\n");
613                 return NS_ERROR_UNEXPECTED;
614             }
615         }
616     }
617
618     if(This->channel) {
619         if(This->post_data_stream) {
620             nsIUploadChannel *upload_channel;
621
622             nsres = nsIChannel_QueryInterface(This->channel, &IID_nsIUploadChannel,
623                                           (void**)&upload_channel);
624             if(NS_SUCCEEDED(nsres)) {
625                 nsACString empty_string;
626                 nsACString_Init(&empty_string, "");
627
628                 nsres = nsIUploadChannel_SetUploadStream(upload_channel, This->post_data_stream,
629                                                          &empty_string, -1);
630                 nsIUploadChannel_Release(upload_channel);
631                 if(NS_FAILED(nsres))
632                     WARN("SetUploadStream failed: %08lx\n", nsres);
633
634                 nsACString_Finish(&empty_string);
635             }
636         }
637
638         return nsIChannel_AsyncOpen(This->channel, aListener, aContext);
639     }
640
641     TRACE("channel == NULL\n");
642
643     if(!This->original_uri) {
644         ERR("original_uri == NULL\n");
645         return NS_ERROR_UNEXPECTED;
646     }
647
648     nsres = nsIURI_QueryInterface(This->original_uri, &IID_nsIWineURI, (void**)&wine_uri);
649     if(NS_FAILED(nsres)) {
650         ERR("Could not get nsIWineURI: %08lx\n", nsres);
651         return NS_ERROR_UNEXPECTED;
652     }
653
654     nsIWineURI_GetMoniker(wine_uri, &mon);
655     nsIWineURI_Release(wine_uri);
656
657     if(!mon) {
658         WARN("mon == NULL\n");
659         return NS_ERROR_UNEXPECTED;
660     }
661
662     bscallback = create_bscallback(NULL, mon);
663     IMoniker_Release(mon);
664
665     nsIChannel_AddRef(NSCHANNEL(This));
666     bscallback->nschannel = This;
667
668     nsIStreamListener_AddRef(aListener);
669     bscallback->nslistener = aListener;
670
671     if(aContext) {
672         nsISupports_AddRef(aContext);
673         bscallback->nscontext = aContext;
674     }
675
676     start_binding(bscallback);
677     IBindStatusCallback_Release(STATUSCLB(bscallback));
678
679     return NS_OK;
680 }
681
682 static nsresult NSAPI nsChannel_GetRequestMethod(nsIHttpChannel *iface, nsACString *aRequestMethod)
683 {
684     nsChannel *This = NSCHANNEL_THIS(iface);
685
686     TRACE("(%p)->(%p)\n", This, aRequestMethod);
687
688     if(This->http_channel)
689         return nsIHttpChannel_GetRequestMethod(This->http_channel, aRequestMethod);
690
691     return NS_ERROR_NOT_IMPLEMENTED;
692 }
693
694 static nsresult NSAPI nsChannel_SetRequestMethod(nsIHttpChannel *iface,
695                                                  const nsACString *aRequestMethod)
696 {
697     nsChannel *This = NSCHANNEL_THIS(iface);
698
699     TRACE("(%p)->(%p)\n", This, aRequestMethod);
700
701     if(This->http_channel)
702         return nsIHttpChannel_SetRequestMethod(This->http_channel, aRequestMethod);
703
704     return NS_ERROR_NOT_IMPLEMENTED;
705 }
706
707 static nsresult NSAPI nsChannel_GetReferrer(nsIHttpChannel *iface, nsIURI **aReferrer)
708 {
709     nsChannel *This = NSCHANNEL_THIS(iface);
710
711     TRACE("(%p)->(%p)\n", This, aReferrer);
712
713     if(This->http_channel)
714         return nsIHttpChannel_GetReferrer(This->http_channel, aReferrer);
715
716     return NS_ERROR_NOT_IMPLEMENTED;
717 }
718
719 static nsresult NSAPI nsChannel_SetReferrer(nsIHttpChannel *iface, nsIURI *aReferrer)
720 {
721     nsChannel *This = NSCHANNEL_THIS(iface);
722
723     TRACE("(%p)->(%p)\n", This, aReferrer);
724
725     if(This->http_channel)
726         return nsIHttpChannel_SetReferrer(This->http_channel, aReferrer);
727
728     return NS_ERROR_NOT_IMPLEMENTED;
729 }
730
731 static nsresult NSAPI nsChannel_GetRequestHeader(nsIHttpChannel *iface,
732          const nsACString *aHeader, nsACString *_retval)
733 {
734     nsChannel *This = NSCHANNEL_THIS(iface);
735
736     TRACE("(%p)->(%p %p)\n", This, aHeader, _retval);
737
738     if(This->http_channel)
739         return nsIHttpChannel_GetRequestHeader(This->http_channel, aHeader, _retval);
740
741     return NS_ERROR_NOT_IMPLEMENTED;
742 }
743
744 static nsresult NSAPI nsChannel_SetRequestHeader(nsIHttpChannel *iface,
745          const nsACString *aHeader, const nsACString *aValue, PRBool aMerge)
746 {
747     nsChannel *This = NSCHANNEL_THIS(iface);
748
749     TRACE("(%p)->(%p %p %x)\n", This, aHeader, aValue, aMerge);
750
751     if(This->http_channel)
752         return nsIHttpChannel_SetRequestHeader(This->http_channel, aHeader, aValue, aMerge);
753
754     return NS_ERROR_NOT_IMPLEMENTED;
755 }
756
757 static nsresult NSAPI nsChannel_VisitRequestHeaders(nsIHttpChannel *iface,
758                                                     nsIHttpHeaderVisitor *aVisitor)
759 {
760     nsChannel *This = NSCHANNEL_THIS(iface);
761
762     TRACE("(%p)->(%p)\n", This, aVisitor);
763
764     if(This->http_channel)
765         return nsIHttpChannel_VisitRequestHeaders(This->http_channel, aVisitor);
766
767     return NS_ERROR_NOT_IMPLEMENTED;
768 }
769
770 static nsresult NSAPI nsChannel_GetAllowPipelining(nsIHttpChannel *iface, PRBool *aAllowPipelining)
771 {
772     nsChannel *This = NSCHANNEL_THIS(iface);
773
774     TRACE("(%p)->(%p)\n", This, aAllowPipelining);
775
776     if(This->http_channel)
777         return nsIHttpChannel_GetAllowPipelining(This->http_channel, aAllowPipelining);
778
779     return NS_ERROR_NOT_IMPLEMENTED;
780 }
781
782 static nsresult NSAPI nsChannel_SetAllowPipelining(nsIHttpChannel *iface, PRBool aAllowPipelining)
783 {
784     nsChannel *This = NSCHANNEL_THIS(iface);
785
786     TRACE("(%p)->(%x)\n", This, aAllowPipelining);
787
788     if(This->http_channel)
789         return nsIHttpChannel_SetAllowPipelining(This->http_channel, aAllowPipelining);
790
791     return NS_ERROR_NOT_IMPLEMENTED;
792 }
793
794 static nsresult NSAPI nsChannel_GetRedirectionLimit(nsIHttpChannel *iface, PRUint32 *aRedirectionLimit)
795 {
796     nsChannel *This = NSCHANNEL_THIS(iface);
797
798     TRACE("(%p)->(%p)\n", This, aRedirectionLimit);
799
800     if(This->http_channel)
801         return nsIHttpChannel_GetRedirectionLimit(This->http_channel, aRedirectionLimit);
802
803     return NS_ERROR_NOT_IMPLEMENTED;
804 }
805
806 static nsresult NSAPI nsChannel_SetRedirectionLimit(nsIHttpChannel *iface, PRUint32 aRedirectionLimit)
807 {
808     nsChannel *This = NSCHANNEL_THIS(iface);
809
810     TRACE("(%p)->(%lu)\n", This, aRedirectionLimit);
811
812     if(This->http_channel)
813         return nsIHttpChannel_SetRedirectionLimit(This->http_channel, aRedirectionLimit);
814
815     return NS_ERROR_NOT_IMPLEMENTED;
816 }
817
818 static nsresult NSAPI nsChannel_GetResponseStatus(nsIHttpChannel *iface, PRUint32 *aResponseStatus)
819 {
820     nsChannel *This = NSCHANNEL_THIS(iface);
821
822     TRACE("(%p)->(%p)\n", This, aResponseStatus);
823
824     if(This->http_channel)
825         return nsIHttpChannel_GetResponseStatus(This->http_channel, aResponseStatus);
826
827     return NS_ERROR_NOT_IMPLEMENTED;
828 }
829
830 static nsresult NSAPI nsChannel_GetResponseStatusText(nsIHttpChannel *iface,
831                                                       nsACString *aResponseStatusText)
832 {
833     nsChannel *This = NSCHANNEL_THIS(iface);
834
835     TRACE("(%p)->(%p)\n", This, aResponseStatusText);
836
837     if(This->http_channel)
838         return nsIHttpChannel_GetResponseStatusText(This->http_channel, aResponseStatusText);
839
840     return NS_ERROR_NOT_IMPLEMENTED;
841 }
842
843 static nsresult NSAPI nsChannel_GetRequestSucceeded(nsIHttpChannel *iface,
844                                                     PRBool *aRequestSucceeded)
845 {
846     nsChannel *This = NSCHANNEL_THIS(iface);
847
848     TRACE("(%p)->(%p)\n", This, aRequestSucceeded);
849
850     if(This->http_channel)
851         return nsIHttpChannel_GetRequestSucceeded(This->http_channel, aRequestSucceeded);
852
853     return NS_ERROR_NOT_IMPLEMENTED;
854 }
855
856 static nsresult NSAPI nsChannel_GetResponseHeader(nsIHttpChannel *iface,
857          const nsACString *header, nsACString *_retval)
858 {
859     nsChannel *This = NSCHANNEL_THIS(iface);
860
861     TRACE("(%p)->(%p %p)\n", This, header, _retval);
862
863     if(This->http_channel)
864         return nsIHttpChannel_GetResponseHeader(This->http_channel, header, _retval);
865
866     return NS_ERROR_NOT_IMPLEMENTED;
867 }
868
869 static nsresult NSAPI nsChannel_SetResponseHeader(nsIHttpChannel *iface,
870         const nsACString *header, const nsACString *value, PRBool merge)
871 {
872     nsChannel *This = NSCHANNEL_THIS(iface);
873
874     TRACE("(%p)->(%p %p %x)\n", This, header, value, merge);
875
876     if(This->http_channel)
877         return nsIHttpChannel_SetResponseHeader(This->http_channel, header, value, merge);
878
879     return NS_ERROR_NOT_IMPLEMENTED;
880 }
881
882 static nsresult NSAPI nsChannel_VisitResponseHeaders(nsIHttpChannel *iface,
883         nsIHttpHeaderVisitor *aVisitor)
884 {
885     nsChannel *This = NSCHANNEL_THIS(iface);
886
887     TRACE("(%p)->(%p)\n", This, aVisitor);
888
889     if(This->http_channel)
890         return nsIHttpChannel_VisitResponseHeaders(This->http_channel, aVisitor);
891
892     return NS_ERROR_NOT_IMPLEMENTED;
893 }
894
895 static nsresult NSAPI nsChannel_IsNoStoreResponse(nsIHttpChannel *iface, PRBool *_retval)
896 {
897     nsChannel *This = NSCHANNEL_THIS(iface);
898
899     TRACE("(%p)->(%p)\n", This, _retval);
900
901     if(This->http_channel)
902         return nsIHttpChannel_IsNoStoreResponse(This->http_channel, _retval);
903
904     return NS_ERROR_NOT_IMPLEMENTED;
905 }
906
907 static nsresult NSAPI nsChannel_IsNoCacheResponse(nsIHttpChannel *iface, PRBool *_retval)
908 {
909     nsChannel *This = NSCHANNEL_THIS(iface);
910
911     TRACE("(%p)->(%p)\n", This, _retval);
912
913     if(This->http_channel)
914         return nsIHttpChannel_IsNoCacheResponse(This->http_channel, _retval);
915
916     return NS_ERROR_NOT_IMPLEMENTED;
917 }
918
919 #undef NSCHANNEL_THIS
920
921 static const nsIHttpChannelVtbl nsChannelVtbl = {
922     nsChannel_QueryInterface,
923     nsChannel_AddRef,
924     nsChannel_Release,
925     nsChannel_GetName,
926     nsChannel_IsPending,
927     nsChannel_GetStatus,
928     nsChannel_Cancel,
929     nsChannel_Suspend,
930     nsChannel_Resume,
931     nsChannel_GetLoadGroup,
932     nsChannel_SetLoadGroup,
933     nsChannel_GetLoadFlags,
934     nsChannel_SetLoadFlags,
935     nsChannel_GetOriginalURI,
936     nsChannel_SetOriginalURI,
937     nsChannel_GetURI,
938     nsChannel_GetOwner,
939     nsChannel_SetOwner,
940     nsChannel_GetNotificationCallbacks,
941     nsChannel_SetNotificationCallbacks,
942     nsChannel_GetSecurityInfo,
943     nsChannel_GetContentType,
944     nsChannel_SetContentType,
945     nsChannel_GetContentCharset,
946     nsChannel_SetContentCharset,
947     nsChannel_GetContentLength,
948     nsChannel_SetContentLength,
949     nsChannel_Open,
950     nsChannel_AsyncOpen,
951     nsChannel_GetRequestMethod,
952     nsChannel_SetRequestMethod,
953     nsChannel_GetReferrer,
954     nsChannel_SetReferrer,
955     nsChannel_GetRequestHeader,
956     nsChannel_SetRequestHeader,
957     nsChannel_VisitRequestHeaders,
958     nsChannel_GetAllowPipelining,
959     nsChannel_SetAllowPipelining,
960     nsChannel_GetRedirectionLimit,
961     nsChannel_SetRedirectionLimit,
962     nsChannel_GetResponseStatus,
963     nsChannel_GetResponseStatusText,
964     nsChannel_GetRequestSucceeded,
965     nsChannel_GetResponseHeader,
966     nsChannel_SetResponseHeader,
967     nsChannel_VisitResponseHeaders,
968     nsChannel_IsNoStoreResponse,
969     nsChannel_IsNoCacheResponse
970 };
971
972 #define NSUPCHANNEL_THIS(iface) DEFINE_THIS(nsChannel, UploadChannel, iface)
973
974 static nsresult NSAPI nsUploadChannel_QueryInterface(nsIUploadChannel *iface, nsIIDRef riid,
975                                                      nsQIResult result)
976 {
977     nsChannel *This = NSUPCHANNEL_THIS(iface);
978     return nsIChannel_QueryInterface(NSCHANNEL(This), riid, result);
979 }
980
981 static nsrefcnt NSAPI nsUploadChannel_AddRef(nsIUploadChannel *iface)
982 {
983     nsChannel *This = NSUPCHANNEL_THIS(iface);
984     return nsIChannel_AddRef(NSCHANNEL(This));
985 }
986
987 static nsrefcnt NSAPI nsUploadChannel_Release(nsIUploadChannel *iface)
988 {
989     nsChannel *This = NSUPCHANNEL_THIS(iface);
990     return nsIChannel_Release(NSCHANNEL(This));
991 }
992
993 static nsresult NSAPI nsUploadChannel_SetUploadStream(nsIUploadChannel *iface,
994         nsIInputStream *aStream, const nsACString *aContentType, PRInt32 aContentLength)
995 {
996     nsChannel *This = NSUPCHANNEL_THIS(iface);
997     const char *content_type;
998
999     TRACE("(%p)->(%p %p %ld)\n", This, aStream, aContentType, aContentLength);
1000
1001     if(This->post_data_stream)
1002         nsIInputStream_Release(This->post_data_stream);
1003
1004     if(aContentType) {
1005         nsACString_GetData(aContentType, &content_type, NULL);
1006         if(*content_type)
1007             FIXME("Unsupported aContentType argument: %s\n", debugstr_a(content_type));
1008     }
1009
1010     if(aContentLength != -1)
1011         FIXME("Unsupported acontentLength = %ld\n", aContentLength);
1012
1013     if(aStream)
1014         nsIInputStream_AddRef(aStream);
1015     This->post_data_stream = aStream;
1016
1017     return NS_OK;
1018 }
1019
1020 static nsresult NSAPI nsUploadChannel_GetUploadStream(nsIUploadChannel *iface,
1021         nsIInputStream **aUploadStream)
1022 {
1023     nsChannel *This = NSUPCHANNEL_THIS(iface);
1024
1025     TRACE("(%p)->(%p)\n", This, aUploadStream);
1026
1027     if(This->post_data_stream)
1028         nsIInputStream_AddRef(This->post_data_stream);
1029
1030     *aUploadStream = This->post_data_stream;
1031     return NS_OK;
1032 }
1033
1034 #undef NSUPCHANNEL_THIS
1035
1036 static const nsIUploadChannelVtbl nsUploadChannelVtbl = {
1037     nsUploadChannel_QueryInterface,
1038     nsUploadChannel_AddRef,
1039     nsUploadChannel_Release,
1040     nsUploadChannel_SetUploadStream,
1041     nsUploadChannel_GetUploadStream
1042 };
1043
1044 #define NSURI_THIS(iface) DEFINE_THIS(nsURI, WineURI, iface)
1045
1046 static nsresult NSAPI nsURI_QueryInterface(nsIWineURI *iface, nsIIDRef riid, nsQIResult result)
1047 {
1048     nsURI *This = NSURI_THIS(iface);
1049
1050     *result = NULL;
1051
1052     if(IsEqualGUID(&IID_nsISupports, riid)) {
1053         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1054         *result = NSURI(This);
1055     }else if(IsEqualGUID(&IID_nsIURI, riid)) {
1056         TRACE("(%p)->(IID_nsIURI %p)\n", This, result);
1057         *result = NSURI(This);
1058     }else if(IsEqualGUID(&IID_nsIWineURI, riid)) {
1059         TRACE("(%p)->(IID_nsIWineURI %p)\n", This, result);
1060         *result = NSURI(This);
1061     }
1062
1063     if(*result) {
1064         nsIURI_AddRef(NSURI(This));
1065         return NS_OK;
1066     }
1067
1068     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1069     return This->uri ? nsIURI_QueryInterface(This->uri, riid, result) : NS_NOINTERFACE;
1070 }
1071
1072 static nsrefcnt NSAPI nsURI_AddRef(nsIWineURI *iface)
1073 {
1074     nsURI *This = NSURI_THIS(iface);
1075     LONG ref = InterlockedIncrement(&This->ref);
1076
1077     TRACE("(%p) ref=%ld\n", This, ref);
1078
1079     return ref;
1080 }
1081
1082 static nsrefcnt NSAPI nsURI_Release(nsIWineURI *iface)
1083 {
1084     nsURI *This = NSURI_THIS(iface);
1085     LONG ref = InterlockedDecrement(&This->ref);
1086
1087     TRACE("(%p) ref=%ld\n", This, ref);
1088
1089     if(!ref) {
1090         if(This->container)
1091             nsIWebBrowserChrome_Release(NSWBCHROME(This->container));
1092         if(This->uri)
1093             nsIURI_Release(This->uri);
1094         if(This->mon)
1095             IMoniker_Release(This->mon);
1096         mshtml_free(This->spec);
1097         mshtml_free(This);
1098     }
1099
1100     return ref;
1101 }
1102
1103 static nsresult NSAPI nsURI_GetSpec(nsIWineURI *iface, nsACString *aSpec)
1104 {
1105     nsURI *This = NSURI_THIS(iface);
1106
1107     TRACE("(%p)->(%p)\n", This, aSpec);
1108
1109     if(This->uri)
1110         return nsIURI_GetSpec(This->uri, aSpec);
1111
1112     if(This->spec) {
1113         nsACString_Init(aSpec, This->spec);
1114         return NS_OK;
1115     }
1116
1117     WARN("mon and uri are NULL\n");
1118     return NS_ERROR_NOT_IMPLEMENTED;
1119
1120 }
1121
1122 static nsresult NSAPI nsURI_SetSpec(nsIWineURI *iface, const nsACString *aSpec)
1123 {
1124     nsURI *This = NSURI_THIS(iface);
1125
1126     TRACE("(%p)->(%p)\n", This, aSpec);
1127
1128     if(This->uri)
1129         return nsIURI_SetSpec(This->uri, aSpec);
1130
1131     FIXME("default action not implemented\n");
1132     return NS_ERROR_NOT_IMPLEMENTED;
1133 }
1134
1135 static nsresult NSAPI nsURI_GetPrePath(nsIWineURI *iface, nsACString *aPrePath)
1136 {
1137     nsURI *This = NSURI_THIS(iface);
1138
1139     TRACE("(%p)->(%p)\n", This, aPrePath);
1140
1141     if(This->uri)
1142         return nsIURI_GetPrePath(This->uri, aPrePath);
1143
1144     FIXME("default action not implemented\n");
1145     return NS_ERROR_NOT_IMPLEMENTED;
1146 }
1147
1148 static nsresult NSAPI nsURI_GetScheme(nsIWineURI *iface, nsACString *aScheme)
1149 {
1150     nsURI *This = NSURI_THIS(iface);
1151
1152     TRACE("(%p)->(%p)\n", This, aScheme);
1153
1154     if(This->uri)
1155         return nsIURI_GetScheme(This->uri, aScheme);
1156
1157     FIXME("default action not implemented\n");
1158     return NS_ERROR_NOT_IMPLEMENTED;
1159 }
1160
1161 static nsresult NSAPI nsURI_SetScheme(nsIWineURI *iface, const nsACString *aScheme)
1162 {
1163     nsURI *This = NSURI_THIS(iface);
1164
1165     TRACE("(%p)->(%p)\n", This, aScheme);
1166
1167     if(This->uri)
1168         return nsIURI_SetScheme(This->uri, aScheme);
1169
1170     FIXME("default action not implemented\n");
1171     return NS_ERROR_NOT_IMPLEMENTED;
1172 }
1173
1174 static nsresult NSAPI nsURI_GetUserPass(nsIWineURI *iface, nsACString *aUserPass)
1175 {
1176     nsURI *This = NSURI_THIS(iface);
1177
1178     TRACE("(%p)->(%p)\n", This, aUserPass);
1179
1180     if(This->uri)
1181         return nsIURI_GetUserPass(This->uri, aUserPass);
1182
1183     FIXME("default action not implemented\n");
1184     return NS_ERROR_NOT_IMPLEMENTED;
1185 }
1186
1187 static nsresult NSAPI nsURI_SetUserPass(nsIWineURI *iface, const nsACString *aUserPass)
1188 {
1189     nsURI *This = NSURI_THIS(iface);
1190
1191     TRACE("(%p)->(%p)\n", This, aUserPass);
1192
1193     if(This->uri)
1194         return nsIURI_SetUserPass(This->uri, aUserPass);
1195
1196     FIXME("default action not implemented\n");
1197     return NS_ERROR_NOT_IMPLEMENTED;
1198 }
1199
1200 static nsresult NSAPI nsURI_GetUsername(nsIWineURI *iface, nsACString *aUsername)
1201 {
1202     nsURI *This = NSURI_THIS(iface);
1203
1204     TRACE("(%p)->(%p)\n", This, aUsername);
1205
1206     if(This->uri)
1207         return nsIURI_GetUsername(This->uri, aUsername);
1208
1209     FIXME("default action not implemented\n");
1210     return NS_ERROR_NOT_IMPLEMENTED;
1211 }
1212
1213 static nsresult NSAPI nsURI_SetUsername(nsIWineURI *iface, const nsACString *aUsername)
1214 {
1215     nsURI *This = NSURI_THIS(iface);
1216
1217     TRACE("(%p)->(%p)\n", This, aUsername);
1218
1219     if(This->uri)
1220         return nsIURI_SetUsername(This->uri, aUsername);
1221
1222     FIXME("default action not implemented\n");
1223     return NS_ERROR_NOT_IMPLEMENTED;
1224 }
1225
1226 static nsresult NSAPI nsURI_GetPassword(nsIWineURI *iface, nsACString *aPassword)
1227 {
1228     nsURI *This = NSURI_THIS(iface);
1229
1230     TRACE("(%p)->(%p)\n", This, aPassword);
1231
1232     if(This->uri)
1233         return nsIURI_GetPassword(This->uri, aPassword);
1234
1235     FIXME("default action not implemented\n");
1236     return NS_ERROR_NOT_IMPLEMENTED;
1237 }
1238
1239 static nsresult NSAPI nsURI_SetPassword(nsIWineURI *iface, const nsACString *aPassword)
1240 {
1241     nsURI *This = NSURI_THIS(iface);
1242
1243     TRACE("(%p)->(%p)\n", This, aPassword);
1244
1245     if(This->uri)
1246         return nsIURI_SetPassword(This->uri, aPassword);
1247
1248     FIXME("default action not implemented\n");
1249     return NS_ERROR_NOT_IMPLEMENTED;
1250 }
1251
1252 static nsresult NSAPI nsURI_GetHostPort(nsIWineURI *iface, nsACString *aHostPort)
1253 {
1254     nsURI *This = NSURI_THIS(iface);
1255
1256     TRACE("(%p)->(%p)\n", This, aHostPort);
1257
1258     if(This->uri)
1259         return nsIURI_GetHostPort(This->uri, aHostPort);
1260
1261     FIXME("default action not implemented\n");
1262     return NS_ERROR_NOT_IMPLEMENTED;
1263 }
1264
1265 static nsresult NSAPI nsURI_SetHostPort(nsIWineURI *iface, const nsACString *aHostPort)
1266 {
1267     nsURI *This = NSURI_THIS(iface);
1268
1269     TRACE("(%p)->(%p)\n", This, aHostPort);
1270
1271     if(This->uri)
1272         return nsIURI_SetHostPort(This->uri, aHostPort);
1273
1274     FIXME("default action not implemented\n");
1275     return NS_ERROR_NOT_IMPLEMENTED;
1276 }
1277
1278 static nsresult NSAPI nsURI_GetHost(nsIWineURI *iface, nsACString *aHost)
1279 {
1280     nsURI *This = NSURI_THIS(iface);
1281
1282     TRACE("(%p)->(%p)\n", This, aHost);
1283
1284     if(This->uri)
1285         return nsIURI_GetHost(This->uri, aHost);
1286
1287     FIXME("default action not implemented\n");
1288     return NS_ERROR_NOT_IMPLEMENTED;
1289 }
1290
1291 static nsresult NSAPI nsURI_SetHost(nsIWineURI *iface, const nsACString *aHost)
1292 {
1293     nsURI *This = NSURI_THIS(iface);
1294
1295     TRACE("(%p)->(%p)\n", This, aHost);
1296
1297     if(This->uri)
1298         return nsIURI_SetHost(This->uri, aHost);
1299
1300     FIXME("default action not implemented\n");
1301     return NS_ERROR_NOT_IMPLEMENTED;
1302 }
1303
1304 static nsresult NSAPI nsURI_GetPort(nsIWineURI *iface, PRInt32 *aPort)
1305 {
1306     nsURI *This = NSURI_THIS(iface);
1307
1308     TRACE("(%p)->(%p)\n", This, aPort);
1309
1310     if(This->uri)
1311         return nsIURI_GetPort(This->uri, aPort);
1312
1313     FIXME("default action not implemented\n");
1314     return NS_ERROR_NOT_IMPLEMENTED;
1315 }
1316
1317 static nsresult NSAPI nsURI_SetPort(nsIWineURI *iface, PRInt32 aPort)
1318 {
1319     nsURI *This = NSURI_THIS(iface);
1320
1321     TRACE("(%p)->(%ld)\n", This, aPort);
1322
1323     if(This->uri)
1324         return nsIURI_SetPort(This->uri, aPort);
1325
1326     FIXME("default action not implemented\n");
1327     return NS_ERROR_NOT_IMPLEMENTED;
1328 }
1329
1330 static nsresult NSAPI nsURI_GetPath(nsIWineURI *iface, nsACString *aPath)
1331 {
1332     nsURI *This = NSURI_THIS(iface);
1333
1334     TRACE("(%p)->(%p)\n", This, aPath);
1335
1336     if(This->uri)
1337         return nsIURI_GetPath(This->uri, aPath);
1338
1339     FIXME("default action not implemented\n");
1340     return NS_ERROR_NOT_IMPLEMENTED;
1341 }
1342
1343 static nsresult NSAPI nsURI_SetPath(nsIWineURI *iface, const nsACString *aPath)
1344 {
1345     nsURI *This = NSURI_THIS(iface);
1346
1347     TRACE("(%p)->(%p)\n", This, aPath);
1348
1349     if(This->uri)
1350         return nsIURI_SetPath(This->uri, aPath);
1351
1352     FIXME("default action not implemented\n");
1353     return NS_ERROR_NOT_IMPLEMENTED;
1354 }
1355
1356 static nsresult NSAPI nsURI_Equals(nsIWineURI *iface, nsIURI *other, PRBool *_retval)
1357 {
1358     nsURI *This = NSURI_THIS(iface);
1359
1360     TRACE("(%p)->(%p %p)\n", This, other, _retval);
1361
1362     if(This->uri)
1363         return nsIURI_Equals(This->uri, other, _retval);
1364
1365     FIXME("default action not implemented\n");
1366     return NS_ERROR_NOT_IMPLEMENTED;
1367 }
1368
1369 static nsresult NSAPI nsURI_SchemeIs(nsIWineURI *iface, const char *scheme, PRBool *_retval)
1370 {
1371     nsURI *This = NSURI_THIS(iface);
1372
1373     TRACE("(%p)->(%s %p)\n", This, debugstr_a(scheme), _retval);
1374
1375     if(This->uri)
1376         return nsIURI_SchemeIs(This->uri, scheme, _retval);
1377
1378     FIXME("default action not implemented\n");
1379     return NS_ERROR_NOT_IMPLEMENTED;
1380 }
1381
1382 static nsresult NSAPI nsURI_Clone(nsIWineURI *iface, nsIURI **_retval)
1383 {
1384     nsURI *This = NSURI_THIS(iface);
1385
1386     TRACE("(%p)->(%p)\n", This, _retval);
1387
1388     if(This->uri) {
1389         nsIURI *uri;
1390         nsresult nsres;
1391
1392         nsres = nsIURI_Clone(This->uri, &uri);
1393         if(NS_FAILED(nsres)) {
1394             WARN("Clone failed: %08lx\n", nsres);
1395             return nsres;
1396         }
1397
1398         return create_uri(uri, This->container, _retval);
1399     }
1400
1401     FIXME("default action not implemented\n");
1402     return NS_ERROR_NOT_IMPLEMENTED;
1403 }
1404
1405 static nsresult NSAPI nsURI_Resolve(nsIWineURI *iface, const nsACString *arelativePath,
1406         nsACString *_retval)
1407 {
1408     nsURI *This = NSURI_THIS(iface);
1409
1410     TRACE("(%p)->(%p %p)\n", This, arelativePath, _retval);
1411
1412     if(This->uri)
1413         return nsIURI_Resolve(This->uri, arelativePath, _retval);
1414
1415     FIXME("default action not implemented\n");
1416     return NS_ERROR_NOT_IMPLEMENTED;
1417 }
1418
1419 static nsresult NSAPI nsURI_GetAsciiSpec(nsIWineURI *iface, nsACString *aAsciiSpec)
1420 {
1421     nsURI *This = NSURI_THIS(iface);
1422
1423     TRACE("(%p)->(%p)\n", This, aAsciiSpec);
1424
1425     if(This->uri)
1426         return nsIURI_GetAsciiSpec(This->uri, aAsciiSpec);
1427
1428     FIXME("default action not implemented\n");
1429     return NS_ERROR_NOT_IMPLEMENTED;
1430 }
1431
1432 static nsresult NSAPI nsURI_GetAsciiHost(nsIWineURI *iface, nsACString *aAsciiHost)
1433 {
1434     nsURI *This = NSURI_THIS(iface);
1435
1436     TRACE("(%p)->(%p)\n", This, aAsciiHost);
1437
1438     if(This->uri)
1439         return nsIURI_GetAsciiHost(This->uri, aAsciiHost);
1440
1441     FIXME("default action not implemented\n");
1442     return NS_ERROR_NOT_IMPLEMENTED;
1443 }
1444
1445 static nsresult NSAPI nsURI_GetOriginCharset(nsIWineURI *iface, nsACString *aOriginCharset)
1446 {
1447     nsURI *This = NSURI_THIS(iface);
1448
1449     TRACE("(%p)->(%p)\n", This, aOriginCharset);
1450
1451     if(This->uri)
1452         return nsIURI_GetOriginCharset(This->uri, aOriginCharset);
1453
1454     FIXME("default action not implemented\n");
1455     return NS_ERROR_NOT_IMPLEMENTED;
1456 }
1457
1458 static nsresult NSAPI nsURI_GetNSContainer(nsIWineURI *iface, NSContainer **aContainer)
1459 {
1460     nsURI *This = NSURI_THIS(iface);
1461
1462     TRACE("(%p)->(%p)\n", This, aContainer);
1463
1464     if(This->container)
1465         nsIWebBrowserChrome_AddRef(NSWBCHROME(This->container));
1466     *aContainer = This->container;
1467
1468     return NS_OK;
1469 }
1470
1471 static nsresult NSAPI nsURI_SetNSContainer(nsIWineURI *iface, NSContainer *aContainer)
1472 {
1473     nsURI *This = NSURI_THIS(iface);
1474
1475     TRACE("(%p)->(%p)\n", This, aContainer);
1476
1477     if(This->container) {
1478         WARN("Container already set: %p\n", This->container);
1479         nsIWebBrowserChrome_Release(NSWBCHROME(This->container));
1480     }
1481
1482     if(aContainer)
1483         nsIWebBrowserChrome_AddRef(NSWBCHROME(aContainer));
1484     This->container = aContainer;
1485
1486     return NS_OK;
1487 }
1488
1489 static nsresult NSAPI nsURI_GetMoniker(nsIWineURI *iface, IMoniker **aMoniker)
1490 {
1491     nsURI *This = NSURI_THIS(iface);
1492
1493     TRACE("(%p)->(%p)\n", This, aMoniker);
1494
1495     if(This->mon)
1496         IMoniker_AddRef(This->mon);
1497     *aMoniker = This->mon;
1498
1499     return NS_OK;
1500 }
1501
1502 static nsresult NSAPI nsURI_SetMoniker(nsIWineURI *iface, IMoniker *aMoniker)
1503 {
1504     nsURI *This = NSURI_THIS(iface);
1505
1506     TRACE("(%p)->(%p)\n", This, aMoniker);
1507
1508     if(This->mon) {
1509         WARN("Moniker already set: %p\n", This->container);
1510         IMoniker_Release(This->mon);
1511
1512         mshtml_free(This->spec);
1513         This->spec = NULL;
1514     }
1515
1516     if(aMoniker) {
1517         LPWSTR url = NULL;
1518         HRESULT hres;
1519
1520         hres = IMoniker_GetDisplayName(aMoniker, NULL, NULL, &url);
1521         if(SUCCEEDED(hres)) {
1522             DWORD len;
1523
1524             len = WideCharToMultiByte(CP_ACP, 0, url, -1, NULL, 0, NULL, NULL);
1525             This->spec = mshtml_alloc(len*sizeof(WCHAR));
1526             WideCharToMultiByte(CP_ACP, 0, url, -1, This->spec, -1, NULL, NULL);
1527             CoTaskMemFree(url);
1528
1529             TRACE("spec %s\n", debugstr_a(This->spec));
1530         }else {
1531             ERR("GetDisplayName failed: %08lx\n", hres);
1532         }
1533
1534         IMoniker_AddRef(aMoniker);
1535     }
1536     This->mon = aMoniker;
1537
1538     return NS_OK;
1539 }
1540
1541 #undef NSURI_THIS
1542
1543 static const nsIWineURIVtbl nsWineURIVtbl = {
1544     nsURI_QueryInterface,
1545     nsURI_AddRef,
1546     nsURI_Release,
1547     nsURI_GetSpec,
1548     nsURI_SetSpec,
1549     nsURI_GetPrePath,
1550     nsURI_GetScheme,
1551     nsURI_SetScheme,
1552     nsURI_GetUserPass,
1553     nsURI_SetUserPass,
1554     nsURI_GetUsername,
1555     nsURI_SetUsername,
1556     nsURI_GetPassword,
1557     nsURI_SetPassword,
1558     nsURI_GetHostPort,
1559     nsURI_SetHostPort,
1560     nsURI_GetHost,
1561     nsURI_SetHost,
1562     nsURI_GetPort,
1563     nsURI_SetPort,
1564     nsURI_GetPath,
1565     nsURI_SetPath,
1566     nsURI_Equals,
1567     nsURI_SchemeIs,
1568     nsURI_Clone,
1569     nsURI_Resolve,
1570     nsURI_GetAsciiSpec,
1571     nsURI_GetAsciiHost,
1572     nsURI_GetOriginCharset,
1573     nsURI_GetNSContainer,
1574     nsURI_SetNSContainer,
1575     nsURI_GetMoniker,
1576     nsURI_SetMoniker
1577 };
1578
1579 static nsresult create_uri(nsIURI *uri, NSContainer *container, nsIURI **_retval)
1580 {
1581     nsURI *ret = mshtml_alloc(sizeof(nsURI));
1582
1583     ret->lpWineURIVtbl = &nsWineURIVtbl;
1584     ret->ref = 1;
1585     ret->uri = uri;
1586     ret->container = container;
1587     ret->mon = NULL;
1588     ret->spec = NULL;
1589
1590     if(container)
1591         nsIWebBrowserChrome_AddRef(NSWBCHROME(container));
1592
1593     TRACE("retval=%p\n", ret);
1594     *_retval = NSURI(ret);
1595     return NS_OK;
1596 }
1597
1598 static nsresult NSAPI nsIOService_QueryInterface(nsIIOService *iface, nsIIDRef riid,
1599                                                  nsQIResult result)
1600 {
1601     *result = NULL;
1602
1603     if(IsEqualGUID(&IID_nsISupports, riid)) {
1604         TRACE("(IID_nsISupports %p)\n", result);
1605         *result = iface;
1606     }else if(IsEqualGUID(&IID_nsIIOService, riid)) {
1607         TRACE("(IID_nsIIOService %p)\n", result);
1608         *result = iface;
1609     }
1610
1611     if(*result) {
1612         nsIIOService_AddRef(iface);
1613         return S_OK;
1614     }
1615
1616     WARN("(%s %p)\n", debugstr_guid(riid), result);
1617     return NS_NOINTERFACE;
1618 }
1619
1620 static nsrefcnt NSAPI nsIOService_AddRef(nsIIOService *iface)
1621 {
1622     return 2;
1623 }
1624
1625 static nsrefcnt NSAPI nsIOService_Release(nsIIOService *iface)
1626 {
1627     return 1;
1628 }
1629
1630 static nsresult NSAPI nsIOService_GetProtocolHandler(nsIIOService *iface, const char *aScheme,
1631                                                      nsIProtocolHandler **_retval)
1632 {
1633     TRACE("(%s %p)\n", debugstr_a(aScheme), _retval);
1634     return nsIIOService_GetProtocolHandler(nsio, aScheme, _retval);
1635 }
1636
1637 static nsresult NSAPI nsIOService_GetProtocolFlags(nsIIOService *iface, const char *aScheme,
1638                                                     PRUint32 *_retval)
1639 {
1640     TRACE("(%s %p)\n", debugstr_a(aScheme), _retval);
1641     return nsIIOService_GetProtocolFlags(nsio, aScheme, _retval);
1642 }
1643
1644 static nsresult NSAPI nsIOService_NewURI(nsIIOService *iface, const nsACString *aSpec,
1645         const char *aOriginCharset, nsIURI *aBaseURI, nsIURI **_retval)
1646 {
1647     const char *spec = NULL;
1648     NSContainer *nscontainer = NULL;
1649     nsIURI *uri = NULL;
1650     PRBool is_javascript = FALSE;
1651     IMoniker *base_mon = NULL;
1652     nsresult nsres;
1653
1654     nsACString_GetData(aSpec, &spec, NULL);
1655
1656     TRACE("(%p(%s) %s %p %p)\n", aSpec, debugstr_a(spec), debugstr_a(aOriginCharset),
1657           aBaseURI, _retval);
1658
1659     if(aBaseURI) {
1660         nsACString base_uri_str;
1661         const char *base_uri = NULL;
1662
1663         nsACString_Init(&base_uri_str, NULL);
1664
1665         nsres = nsIURI_GetSpec(aBaseURI, &base_uri_str);
1666         if(NS_SUCCEEDED(nsres)) {
1667             nsACString_GetData(&base_uri_str, &base_uri, NULL);
1668             TRACE("uri=%s\n", debugstr_a(base_uri));
1669         }else {
1670             ERR("GetSpec failed: %08lx\n", nsres);
1671         }
1672
1673         nsACString_Finish(&base_uri_str);
1674     }
1675
1676     nsres = nsIIOService_NewURI(nsio, aSpec, aOriginCharset, aBaseURI, &uri);
1677     if(NS_FAILED(nsres))
1678         TRACE("NewURI failed: %08lx\n", nsres);
1679
1680     if(uri) {
1681         nsIURI_SchemeIs(uri, "javascript", &is_javascript);
1682         if(is_javascript) {
1683             TRACE("returning javascript uri: %p\n", uri);
1684             *_retval = uri;
1685             return NS_OK;
1686         }
1687     }
1688
1689     if(aBaseURI) {
1690         nsIWineURI *wine_uri;
1691
1692         nsres = nsIURI_QueryInterface(aBaseURI, &IID_nsIWineURI, (void**)&wine_uri);
1693         if(NS_SUCCEEDED(nsres)) {
1694             nsIWineURI_GetNSContainer(wine_uri, &nscontainer);
1695             nsIWineURI_GetMoniker(wine_uri, &base_mon);
1696             nsIWineURI_Release(wine_uri);
1697         }else {
1698             ERR("Could not get nsIWineURI: %08lx\n", nsres);
1699         }
1700     }
1701
1702     nsres = create_uri(uri, nscontainer, _retval);
1703
1704     if(nscontainer)
1705         nsIWebBrowserChrome_Release(NSWBCHROME(nscontainer));
1706
1707     if(base_mon) {
1708         LPWSTR url;
1709         IMoniker *mon;
1710         DWORD len;
1711         HRESULT hres;
1712
1713         len = MultiByteToWideChar(CP_ACP, 0, spec, -1, NULL, 0);
1714         url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1715         MultiByteToWideChar(CP_ACP, 0, spec, -1, url, -1);
1716
1717         hres = CreateURLMoniker(base_mon, url, &mon);
1718         HeapFree(GetProcessHeap(), 0, url);
1719         if(SUCCEEDED(hres)) {
1720             nsIWineURI_SetMoniker((nsIWineURI*)*_retval, mon);
1721             IMoniker_Release(mon);
1722         }else {
1723             WARN("CreateURLMoniker failed: %08lx\n", hres);
1724         }
1725     }
1726
1727     return nsres;
1728 }
1729
1730 static nsresult NSAPI nsIOService_NewFileURI(nsIIOService *iface, nsIFile *aFile,
1731                                              nsIURI **_retval)
1732 {
1733     TRACE("(%p %p)\n", aFile, _retval);
1734     return nsIIOService_NewFileURI(nsio, aFile, _retval);
1735 }
1736
1737 static nsresult NSAPI nsIOService_NewChannelFromURI(nsIIOService *iface, nsIURI *aURI,
1738                                                      nsIChannel **_retval)
1739 {
1740     nsIChannel *channel = NULL;
1741     nsChannel *ret;
1742     nsIWineURI *wine_uri;
1743     nsresult nsres;
1744
1745     TRACE("(%p %p)\n", aURI, _retval);
1746
1747     nsres = nsIIOService_NewChannelFromURI(nsio, aURI, &channel);
1748     if(NS_FAILED(nsres) && nsres != NS_ERROR_UNKNOWN_PROTOCOL) {
1749         WARN("NewChannelFromURI failed: %08lx\n", nsres);
1750         *_retval = channel;
1751         return nsres;
1752     }
1753
1754     nsres = nsIURI_QueryInterface(aURI, &IID_nsIWineURI, (void**)&wine_uri);
1755     if(NS_FAILED(nsres)) {
1756         WARN("Could not get nsIWineURI: %08lx\n", nsres);
1757         *_retval = channel;
1758         return channel ? NS_OK : NS_ERROR_UNEXPECTED;
1759     }
1760
1761     ret = mshtml_alloc(sizeof(nsChannel));
1762
1763     ret->lpHttpChannelVtbl = &nsChannelVtbl;
1764     ret->lpUploadChannelVtbl = &nsUploadChannelVtbl;
1765     ret->ref = 1;
1766     ret->channel = channel;
1767     ret->http_channel = NULL;
1768     ret->uri = wine_uri;
1769     ret->post_data_stream = NULL;
1770     ret->load_group = NULL;
1771     ret->notif_callback = NULL;
1772     ret->load_flags = 0;
1773     ret->content = NULL;
1774
1775     nsIURI_AddRef(aURI);
1776     ret->original_uri = aURI;
1777
1778     if(channel)
1779         nsIChannel_QueryInterface(channel, &IID_nsIHttpChannel, (void**)&ret->http_channel);
1780
1781     *_retval = NSCHANNEL(ret);
1782     return NS_OK;
1783 }
1784
1785 static nsresult NSAPI nsIOService_NewChannel(nsIIOService *iface, const nsACString *aSpec,
1786         const char *aOriginCharset, nsIURI *aBaseURI, nsIChannel **_retval)
1787 {
1788     TRACE("(%p %s %p %p)\n", aSpec, debugstr_a(aOriginCharset), aBaseURI, _retval);
1789     return nsIIOService_NewChannel(nsio, aSpec, aOriginCharset, aBaseURI, _retval);
1790 }
1791
1792 static nsresult NSAPI nsIOService_GetOffline(nsIIOService *iface, PRBool *aOffline)
1793 {
1794     TRACE("(%p)\n", aOffline);
1795     return nsIIOService_GetOffline(nsio, aOffline);
1796 }
1797
1798 static nsresult NSAPI nsIOService_SetOffline(nsIIOService *iface, PRBool aOffline)
1799 {
1800     TRACE("(%x)\n", aOffline);
1801     return nsIIOService_SetOffline(nsio, aOffline);
1802 }
1803
1804 static nsresult NSAPI nsIOService_AllowPort(nsIIOService *iface, PRInt32 aPort,
1805                                              const char *aScheme, PRBool *_retval)
1806 {
1807     TRACE("(%ld %s %p)\n", aPort, debugstr_a(aScheme), _retval);
1808     return nsIIOService_AllowPort(nsio, aPort, debugstr_a(aScheme), _retval);
1809 }
1810
1811 static nsresult NSAPI nsIOService_ExtractScheme(nsIIOService *iface, const nsACString *urlString,
1812                                                  nsACString * _retval)
1813 {
1814     TRACE("(%p %p)\n", urlString, _retval);
1815     return nsIIOService_ExtractScheme(nsio, urlString, _retval);
1816 }
1817
1818 static const nsIIOServiceVtbl nsIOServiceVtbl = {
1819     nsIOService_QueryInterface,
1820     nsIOService_AddRef,
1821     nsIOService_Release,
1822     nsIOService_GetProtocolHandler,
1823     nsIOService_GetProtocolFlags,
1824     nsIOService_NewURI,
1825     nsIOService_NewFileURI,
1826     nsIOService_NewChannelFromURI,
1827     nsIOService_NewChannel,
1828     nsIOService_GetOffline,
1829     nsIOService_SetOffline,
1830     nsIOService_AllowPort,
1831     nsIOService_ExtractScheme
1832 };
1833
1834 static nsIIOService nsIOService = { &nsIOServiceVtbl };
1835
1836 static nsresult NSAPI nsIOServiceFactory_QueryInterface(nsIFactory *iface, nsIIDRef riid,
1837                                                         nsQIResult result)
1838 {
1839     *result = NULL;
1840
1841     if(IsEqualGUID(&IID_nsISupports, riid)) {
1842         TRACE("(IID_nsISupoprts %p)\n", result);
1843         *result = iface;
1844     }else if(IsEqualGUID(&IID_nsIFactory, riid)) {
1845         TRACE("(IID_nsIFactory %p)\n", result);
1846         *result = iface;
1847     }
1848
1849     if(*result) {
1850         nsIFactory_AddRef(iface);
1851         return NS_OK;
1852     }
1853
1854     WARN("(%s %p)\n", debugstr_guid(riid), result);
1855     return NS_NOINTERFACE;
1856 }
1857
1858 static nsrefcnt NSAPI nsIOServiceFactory_AddRef(nsIFactory *iface)
1859 {
1860     return 2;
1861 }
1862
1863 static nsrefcnt NSAPI nsIOServiceFactory_Release(nsIFactory *iface)
1864 {
1865     return 1;
1866 }
1867
1868 static nsresult NSAPI nsIOServiceFactory_CreateInstance(nsIFactory *iface,
1869         nsISupports *aOuter, const nsIID *iid, void **result)
1870 {
1871     return nsIIOService_QueryInterface(&nsIOService, iid, result);
1872 }
1873
1874 static nsresult NSAPI nsIOServiceFactory_LockFactory(nsIFactory *iface, PRBool lock)
1875 {
1876     WARN("(%x)\n", lock);
1877     return NS_OK;
1878 }
1879
1880 static const nsIFactoryVtbl nsIOServiceFactoryVtbl = {
1881     nsIOServiceFactory_QueryInterface,
1882     nsIOServiceFactory_AddRef,
1883     nsIOServiceFactory_Release,
1884     nsIOServiceFactory_CreateInstance,
1885     nsIOServiceFactory_LockFactory
1886 };
1887
1888 static nsIFactory nsIOServiceFactory = { &nsIOServiceFactoryVtbl };
1889
1890 void init_nsio(nsIComponentManager *component_manager, nsIComponentRegistrar *registrar)
1891 {
1892     nsIFactory *old_factory = NULL;
1893     nsresult nsres;
1894
1895     nsres = nsIComponentManager_GetClassObject(component_manager, &NS_IOSERVICE_CID,
1896                                                &IID_nsIFactory, (void**)&old_factory);
1897     if(NS_FAILED(nsres)) {
1898         ERR("Could not get factory: %08lx\n", nsres);
1899         nsIFactory_Release(old_factory);
1900         return;
1901     }
1902
1903     nsres = nsIFactory_CreateInstance(old_factory, NULL, &IID_nsIIOService, (void**)&nsio);
1904     if(NS_FAILED(nsres)) {
1905         ERR("Couldn not create nsIOService instance %08lx\n", nsres);
1906         nsIFactory_Release(old_factory);
1907         return;
1908     }
1909
1910     nsres = nsIComponentRegistrar_UnregisterFactory(registrar, &NS_IOSERVICE_CID, old_factory);
1911     nsIFactory_Release(old_factory);
1912     if(NS_FAILED(nsres))
1913         ERR("UnregisterFactory failed: %08lx\n", nsres);
1914
1915     nsres = nsIComponentRegistrar_RegisterFactory(registrar, &NS_IOSERVICE_CID,
1916             NS_IOSERVICE_CLASSNAME, NS_IOSERVICE_CONTRACTID, &nsIOServiceFactory);
1917     if(NS_FAILED(nsres))
1918         ERR("RegisterFactory failed: %08lx\n", nsres);
1919 }