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