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