gdi32: SetWinMetaFileBits: Use the whole device surface if the METAFILEPICT parameter...
[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 = HeapAlloc(GetProcessHeap(), 0, 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     HeapFree(GetProcessHeap(), 0, 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         HeapFree(GetProcessHeap(), 0, This->content);
217         HeapFree(GetProcessHeap(), 0, 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     FIXME("default action not implemented\n");
259     return NS_ERROR_NOT_IMPLEMENTED;
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     FIXME("default action not implemented\n");
484     return NS_ERROR_NOT_IMPLEMENTED;
485 }
486
487 static nsresult NSAPI nsChannel_SetContentType(nsIHttpChannel *iface,
488                                                const nsACString *aContentType)
489 {
490     nsChannel *This = NSCHANNEL_THIS(iface);
491
492     TRACE("(%p)->(%p)\n", This, aContentType);
493
494     if(This->channel)
495         return nsIChannel_SetContentType(This->channel, aContentType);
496
497     FIXME("default action not implemented\n");
498     return NS_ERROR_NOT_IMPLEMENTED;
499 }
500
501 static nsresult NSAPI nsChannel_GetContentCharset(nsIHttpChannel *iface,
502                                                   nsACString *aContentCharset)
503 {
504     nsChannel *This = NSCHANNEL_THIS(iface);
505
506     TRACE("(%p)->(%p)\n", This, aContentCharset);
507
508     if(This->channel)
509         return nsIChannel_GetContentCharset(This->channel, aContentCharset);
510
511     FIXME("default action not implemented\n");
512     return NS_ERROR_NOT_IMPLEMENTED;
513 }
514
515 static nsresult NSAPI nsChannel_SetContentCharset(nsIHttpChannel *iface,
516                                                   const nsACString *aContentCharset)
517 {
518     nsChannel *This = NSCHANNEL_THIS(iface);
519
520     TRACE("(%p)->(%p)\n", This, aContentCharset);
521
522     if(This->channel)
523         return nsIChannel_SetContentCharset(This->channel, aContentCharset);
524
525     FIXME("default action not implemented\n");
526     return NS_ERROR_NOT_IMPLEMENTED;
527 }
528
529 static nsresult NSAPI nsChannel_GetContentLength(nsIHttpChannel *iface, PRInt32 *aContentLength)
530 {
531     nsChannel *This = NSCHANNEL_THIS(iface);
532
533     TRACE("(%p)->(%p)\n", This, aContentLength);
534
535     if(This->channel)
536         return nsIChannel_GetContentLength(This->channel, aContentLength);
537
538     FIXME("default action not implemented\n");
539     return NS_ERROR_NOT_IMPLEMENTED;
540 }
541
542 static nsresult NSAPI nsChannel_SetContentLength(nsIHttpChannel *iface, PRInt32 aContentLength)
543 {
544     nsChannel *This = NSCHANNEL_THIS(iface);
545
546     TRACE("(%p)->(%ld)\n", This, aContentLength);
547
548     if(This->channel)
549         return nsIChannel_SetContentLength(This->channel, aContentLength);
550
551     FIXME("default action not implemented\n");
552     return NS_ERROR_NOT_IMPLEMENTED;
553 }
554
555 static nsresult NSAPI nsChannel_Open(nsIHttpChannel *iface, nsIInputStream **_retval)
556 {
557     nsChannel *This = NSCHANNEL_THIS(iface);
558
559     TRACE("(%p)->(%p)\n", This, _retval);
560
561     if(This->channel)
562         return nsIChannel_Open(This->channel, _retval);
563
564     FIXME("default action not implemented\n");
565     return NS_ERROR_NOT_IMPLEMENTED;
566 }
567
568 static nsresult NSAPI nsChannel_AsyncOpen(nsIHttpChannel *iface, nsIStreamListener *aListener,
569                                           nsISupports *aContext)
570 {
571     nsChannel *This = NSCHANNEL_THIS(iface);
572     nsresult nsres;
573
574     TRACE("(%p)->(%p %p)\n", This, aListener, aContext);
575
576     if(This->load_flags & LOAD_INITIAL_DOCUMENT_URI) {
577         NSContainer *container;
578
579         nsIWineURI_GetNSContainer(This->uri, &container);
580         if(!container) {
581             ERR("container = NULL\n");
582             return NS_ERROR_UNEXPECTED;
583         }
584
585         if(container->bscallback) {
586             nsIChannel_AddRef(NSCHANNEL(This));
587             container->bscallback->nschannel = This;
588
589             nsIStreamListener_AddRef(aListener);
590             container->bscallback->nslistener = aListener;
591
592             if(aContext) {
593                 nsISupports_AddRef(aContext);
594                 container->bscallback->nscontext = aContext;
595             }
596
597             nsIWebBrowserChrome_Release(NSWBCHROME(container));
598         }else {
599             BOOL cont = before_async_open(This, container);
600             nsIWebBrowserChrome_Release(NSWBCHROME(container));
601
602             if(!cont) {
603                 TRACE("canceled\n");
604                 return NS_ERROR_UNEXPECTED;
605             }
606         }
607     }
608
609     if(!This->channel) {
610         FIXME("channel == NULL\n");
611         return NS_ERROR_UNEXPECTED;
612     }
613
614     if(This->post_data_stream) {
615         nsIUploadChannel *upload_channel;
616
617         nsres = nsIChannel_QueryInterface(This->channel, &IID_nsIUploadChannel,
618                                           (void**)&upload_channel);
619         if(NS_SUCCEEDED(nsres)) {
620             nsACString empty_string;
621             nsACString_Init(&empty_string, "");
622
623             nsres = nsIUploadChannel_SetUploadStream(upload_channel, This->post_data_stream,
624                                                      &empty_string, -1);
625             nsIUploadChannel_Release(upload_channel);
626             if(NS_FAILED(nsres))
627                 WARN("SetUploadStream failed: %08lx\n", nsres);
628
629             nsACString_Finish(&empty_string);
630         }
631     }
632
633     return nsIChannel_AsyncOpen(This->channel, aListener, aContext);
634 }
635
636 static nsresult NSAPI nsChannel_GetRequestMethod(nsIHttpChannel *iface, nsACString *aRequestMethod)
637 {
638     nsChannel *This = NSCHANNEL_THIS(iface);
639
640     TRACE("(%p)->(%p)\n", This, aRequestMethod);
641
642     if(This->http_channel)
643         return nsIHttpChannel_GetRequestMethod(This->http_channel, aRequestMethod);
644
645     return NS_ERROR_NOT_IMPLEMENTED;
646 }
647
648 static nsresult NSAPI nsChannel_SetRequestMethod(nsIHttpChannel *iface,
649                                                  const nsACString *aRequestMethod)
650 {
651     nsChannel *This = NSCHANNEL_THIS(iface);
652
653     TRACE("(%p)->(%p)\n", This, aRequestMethod);
654
655     if(This->http_channel)
656         return nsIHttpChannel_SetRequestMethod(This->http_channel, aRequestMethod);
657
658     return NS_ERROR_NOT_IMPLEMENTED;
659 }
660
661 static nsresult NSAPI nsChannel_GetReferrer(nsIHttpChannel *iface, nsIURI **aReferrer)
662 {
663     nsChannel *This = NSCHANNEL_THIS(iface);
664
665     TRACE("(%p)->(%p)\n", This, aReferrer);
666
667     if(This->http_channel)
668         return nsIHttpChannel_GetReferrer(This->http_channel, aReferrer);
669
670     return NS_ERROR_NOT_IMPLEMENTED;
671 }
672
673 static nsresult NSAPI nsChannel_SetReferrer(nsIHttpChannel *iface, nsIURI *aReferrer)
674 {
675     nsChannel *This = NSCHANNEL_THIS(iface);
676
677     TRACE("(%p)->(%p)\n", This, aReferrer);
678
679     if(This->http_channel)
680         return nsIHttpChannel_SetReferrer(This->http_channel, aReferrer);
681
682     return NS_ERROR_NOT_IMPLEMENTED;
683 }
684
685 static nsresult NSAPI nsChannel_GetRequestHeader(nsIHttpChannel *iface,
686          const nsACString *aHeader, nsACString *_retval)
687 {
688     nsChannel *This = NSCHANNEL_THIS(iface);
689
690     TRACE("(%p)->(%p %p)\n", This, aHeader, _retval);
691
692     if(This->http_channel)
693         return nsIHttpChannel_GetRequestHeader(This->http_channel, aHeader, _retval);
694
695     return NS_ERROR_NOT_IMPLEMENTED;
696 }
697
698 static nsresult NSAPI nsChannel_SetRequestHeader(nsIHttpChannel *iface,
699          const nsACString *aHeader, const nsACString *aValue, PRBool aMerge)
700 {
701     nsChannel *This = NSCHANNEL_THIS(iface);
702
703     TRACE("(%p)->(%p %p %x)\n", This, aHeader, aValue, aMerge);
704
705     if(This->http_channel)
706         return nsIHttpChannel_SetRequestHeader(This->http_channel, aHeader, aValue, aMerge);
707
708     return NS_ERROR_NOT_IMPLEMENTED;
709 }
710
711 static nsresult NSAPI nsChannel_VisitRequestHeaders(nsIHttpChannel *iface,
712                                                     nsIHttpHeaderVisitor *aVisitor)
713 {
714     nsChannel *This = NSCHANNEL_THIS(iface);
715
716     TRACE("(%p)->(%p)\n", This, aVisitor);
717
718     if(This->http_channel)
719         return nsIHttpChannel_VisitRequestHeaders(This->http_channel, aVisitor);
720
721     return NS_ERROR_NOT_IMPLEMENTED;
722 }
723
724 static nsresult NSAPI nsChannel_GetAllowPipelining(nsIHttpChannel *iface, PRBool *aAllowPipelining)
725 {
726     nsChannel *This = NSCHANNEL_THIS(iface);
727
728     TRACE("(%p)->(%p)\n", This, aAllowPipelining);
729
730     if(This->http_channel)
731         return nsIHttpChannel_GetAllowPipelining(This->http_channel, aAllowPipelining);
732
733     return NS_ERROR_NOT_IMPLEMENTED;
734 }
735
736 static nsresult NSAPI nsChannel_SetAllowPipelining(nsIHttpChannel *iface, PRBool aAllowPipelining)
737 {
738     nsChannel *This = NSCHANNEL_THIS(iface);
739
740     TRACE("(%p)->(%x)\n", This, aAllowPipelining);
741
742     if(This->http_channel)
743         return nsIHttpChannel_SetAllowPipelining(This->http_channel, aAllowPipelining);
744
745     return NS_ERROR_NOT_IMPLEMENTED;
746 }
747
748 static nsresult NSAPI nsChannel_GetRedirectionLimit(nsIHttpChannel *iface, PRUint32 *aRedirectionLimit)
749 {
750     nsChannel *This = NSCHANNEL_THIS(iface);
751
752     TRACE("(%p)->(%p)\n", This, aRedirectionLimit);
753
754     if(This->http_channel)
755         return nsIHttpChannel_GetRedirectionLimit(This->http_channel, aRedirectionLimit);
756
757     return NS_ERROR_NOT_IMPLEMENTED;
758 }
759
760 static nsresult NSAPI nsChannel_SetRedirectionLimit(nsIHttpChannel *iface, PRUint32 aRedirectionLimit)
761 {
762     nsChannel *This = NSCHANNEL_THIS(iface);
763
764     TRACE("(%p)->(%lu)\n", This, aRedirectionLimit);
765
766     if(This->http_channel)
767         return nsIHttpChannel_SetRedirectionLimit(This->http_channel, aRedirectionLimit);
768
769     return NS_ERROR_NOT_IMPLEMENTED;
770 }
771
772 static nsresult NSAPI nsChannel_GetResponseStatus(nsIHttpChannel *iface, PRUint32 *aResponseStatus)
773 {
774     nsChannel *This = NSCHANNEL_THIS(iface);
775
776     TRACE("(%p)->(%p)\n", This, aResponseStatus);
777
778     if(This->http_channel)
779         return nsIHttpChannel_GetResponseStatus(This->http_channel, aResponseStatus);
780
781     return NS_ERROR_NOT_IMPLEMENTED;
782 }
783
784 static nsresult NSAPI nsChannel_GetResponseStatusText(nsIHttpChannel *iface,
785                                                       nsACString *aResponseStatusText)
786 {
787     nsChannel *This = NSCHANNEL_THIS(iface);
788
789     TRACE("(%p)->(%p)\n", This, aResponseStatusText);
790
791     if(This->http_channel)
792         return nsIHttpChannel_GetResponseStatusText(This->http_channel, aResponseStatusText);
793
794     return NS_ERROR_NOT_IMPLEMENTED;
795 }
796
797 static nsresult NSAPI nsChannel_GetRequestSucceeded(nsIHttpChannel *iface,
798                                                     PRBool *aRequestSucceeded)
799 {
800     nsChannel *This = NSCHANNEL_THIS(iface);
801
802     TRACE("(%p)->(%p)\n", This, aRequestSucceeded);
803
804     if(This->http_channel)
805         return nsIHttpChannel_GetRequestSucceeded(This->http_channel, aRequestSucceeded);
806
807     return NS_ERROR_NOT_IMPLEMENTED;
808 }
809
810 static nsresult NSAPI nsChannel_GetResponseHeader(nsIHttpChannel *iface,
811          const nsACString *header, nsACString *_retval)
812 {
813     nsChannel *This = NSCHANNEL_THIS(iface);
814
815     TRACE("(%p)->(%p %p)\n", This, header, _retval);
816
817     if(This->http_channel)
818         return nsIHttpChannel_GetResponseHeader(This->http_channel, header, _retval);
819
820     return NS_ERROR_NOT_IMPLEMENTED;
821 }
822
823 static nsresult NSAPI nsChannel_SetResponseHeader(nsIHttpChannel *iface,
824         const nsACString *header, const nsACString *value, PRBool merge)
825 {
826     nsChannel *This = NSCHANNEL_THIS(iface);
827
828     TRACE("(%p)->(%p %p %x)\n", This, header, value, merge);
829
830     if(This->http_channel)
831         return nsIHttpChannel_SetResponseHeader(This->http_channel, header, value, merge);
832
833     return NS_ERROR_NOT_IMPLEMENTED;
834 }
835
836 static nsresult NSAPI nsChannel_VisitResponseHeaders(nsIHttpChannel *iface,
837         nsIHttpHeaderVisitor *aVisitor)
838 {
839     nsChannel *This = NSCHANNEL_THIS(iface);
840
841     TRACE("(%p)->(%p)\n", This, aVisitor);
842
843     if(This->http_channel)
844         return nsIHttpChannel_VisitResponseHeaders(This->http_channel, aVisitor);
845
846     return NS_ERROR_NOT_IMPLEMENTED;
847 }
848
849 static nsresult NSAPI nsChannel_IsNoStoreResponse(nsIHttpChannel *iface, PRBool *_retval)
850 {
851     nsChannel *This = NSCHANNEL_THIS(iface);
852
853     TRACE("(%p)->(%p)\n", This, _retval);
854
855     if(This->http_channel)
856         return nsIHttpChannel_IsNoStoreResponse(This->http_channel, _retval);
857
858     return NS_ERROR_NOT_IMPLEMENTED;
859 }
860
861 static nsresult NSAPI nsChannel_IsNoCacheResponse(nsIHttpChannel *iface, PRBool *_retval)
862 {
863     nsChannel *This = NSCHANNEL_THIS(iface);
864
865     TRACE("(%p)->(%p)\n", This, _retval);
866
867     if(This->http_channel)
868         return nsIHttpChannel_IsNoCacheResponse(This->http_channel, _retval);
869
870     return NS_ERROR_NOT_IMPLEMENTED;
871 }
872
873 #undef NSCHANNEL_THIS
874
875 static const nsIHttpChannelVtbl nsChannelVtbl = {
876     nsChannel_QueryInterface,
877     nsChannel_AddRef,
878     nsChannel_Release,
879     nsChannel_GetName,
880     nsChannel_IsPending,
881     nsChannel_GetStatus,
882     nsChannel_Cancel,
883     nsChannel_Suspend,
884     nsChannel_Resume,
885     nsChannel_GetLoadGroup,
886     nsChannel_SetLoadGroup,
887     nsChannel_GetLoadFlags,
888     nsChannel_SetLoadFlags,
889     nsChannel_GetOriginalURI,
890     nsChannel_SetOriginalURI,
891     nsChannel_GetURI,
892     nsChannel_GetOwner,
893     nsChannel_SetOwner,
894     nsChannel_GetNotificationCallbacks,
895     nsChannel_SetNotificationCallbacks,
896     nsChannel_GetSecurityInfo,
897     nsChannel_GetContentType,
898     nsChannel_SetContentType,
899     nsChannel_GetContentCharset,
900     nsChannel_SetContentCharset,
901     nsChannel_GetContentLength,
902     nsChannel_SetContentLength,
903     nsChannel_Open,
904     nsChannel_AsyncOpen,
905     nsChannel_GetRequestMethod,
906     nsChannel_SetRequestMethod,
907     nsChannel_GetReferrer,
908     nsChannel_SetReferrer,
909     nsChannel_GetRequestHeader,
910     nsChannel_SetRequestHeader,
911     nsChannel_VisitRequestHeaders,
912     nsChannel_GetAllowPipelining,
913     nsChannel_SetAllowPipelining,
914     nsChannel_GetRedirectionLimit,
915     nsChannel_SetRedirectionLimit,
916     nsChannel_GetResponseStatus,
917     nsChannel_GetResponseStatusText,
918     nsChannel_GetRequestSucceeded,
919     nsChannel_GetResponseHeader,
920     nsChannel_SetResponseHeader,
921     nsChannel_VisitResponseHeaders,
922     nsChannel_IsNoStoreResponse,
923     nsChannel_IsNoCacheResponse
924 };
925
926 #define NSUPCHANNEL_THIS(iface) DEFINE_THIS(nsChannel, UploadChannel, iface)
927
928 static nsresult NSAPI nsUploadChannel_QueryInterface(nsIUploadChannel *iface, nsIIDRef riid,
929                                                      nsQIResult result)
930 {
931     nsChannel *This = NSUPCHANNEL_THIS(iface);
932     return nsIChannel_QueryInterface(NSCHANNEL(This), riid, result);
933 }
934
935 static nsrefcnt NSAPI nsUploadChannel_AddRef(nsIUploadChannel *iface)
936 {
937     nsChannel *This = NSUPCHANNEL_THIS(iface);
938     return nsIChannel_AddRef(NSCHANNEL(This));
939 }
940
941 static nsrefcnt NSAPI nsUploadChannel_Release(nsIUploadChannel *iface)
942 {
943     nsChannel *This = NSUPCHANNEL_THIS(iface);
944     return nsIChannel_Release(NSCHANNEL(This));
945 }
946
947 static nsresult NSAPI nsUploadChannel_SetUploadStream(nsIUploadChannel *iface,
948         nsIInputStream *aStream, const nsACString *aContentType, PRInt32 aContentLength)
949 {
950     nsChannel *This = NSUPCHANNEL_THIS(iface);
951     const char *content_type;
952
953     TRACE("(%p)->(%p %p %ld)\n", This, aStream, aContentType, aContentLength);
954
955     if(This->post_data_stream)
956         nsIInputStream_Release(This->post_data_stream);
957
958     if(aContentType) {
959         nsACString_GetData(aContentType, &content_type, NULL);
960         if(*content_type)
961             FIXME("Unsupported aContentType argument: %s\n", debugstr_a(content_type));
962     }
963
964     if(aContentLength != -1)
965         FIXME("Unsupported acontentLength = %ld\n", aContentLength);
966
967     if(aStream)
968         nsIInputStream_AddRef(aStream);
969     This->post_data_stream = aStream;
970
971     return NS_OK;
972 }
973
974 static nsresult NSAPI nsUploadChannel_GetUploadStream(nsIUploadChannel *iface,
975         nsIInputStream **aUploadStream)
976 {
977     nsChannel *This = NSUPCHANNEL_THIS(iface);
978
979     TRACE("(%p)->(%p)\n", This, aUploadStream);
980
981     if(This->post_data_stream)
982         nsIInputStream_AddRef(This->post_data_stream);
983
984     *aUploadStream = This->post_data_stream;
985     return NS_OK;
986 }
987
988 #undef NSUPCHANNEL_THIS
989
990 static const nsIUploadChannelVtbl nsUploadChannelVtbl = {
991     nsUploadChannel_QueryInterface,
992     nsUploadChannel_AddRef,
993     nsUploadChannel_Release,
994     nsUploadChannel_SetUploadStream,
995     nsUploadChannel_GetUploadStream
996 };
997
998 #define NSURI_THIS(iface) DEFINE_THIS(nsURI, WineURI, iface)
999
1000 static nsresult NSAPI nsURI_QueryInterface(nsIWineURI *iface, nsIIDRef riid, nsQIResult result)
1001 {
1002     nsURI *This = NSURI_THIS(iface);
1003
1004     *result = NULL;
1005
1006     if(IsEqualGUID(&IID_nsISupports, riid)) {
1007         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1008         *result = NSURI(This);
1009     }else if(IsEqualGUID(&IID_nsIURI, riid)) {
1010         TRACE("(%p)->(IID_nsIURI %p)\n", This, result);
1011         *result = NSURI(This);
1012     }else if(IsEqualGUID(&IID_nsIWineURI, riid)) {
1013         TRACE("(%p)->(IID_nsIWineURI %p)\n", This, result);
1014         *result = NSURI(This);
1015     }
1016
1017     if(*result) {
1018         nsIURI_AddRef(NSURI(This));
1019         return NS_OK;
1020     }
1021
1022     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1023     return nsIURI_QueryInterface(This->uri, riid, result);
1024 }
1025
1026 static nsrefcnt NSAPI nsURI_AddRef(nsIWineURI *iface)
1027 {
1028     nsURI *This = NSURI_THIS(iface);
1029     LONG ref = InterlockedIncrement(&This->ref);
1030
1031     TRACE("(%p) ref=%ld\n", This, ref);
1032
1033     return ref;
1034 }
1035
1036 static nsrefcnt NSAPI nsURI_Release(nsIWineURI *iface)
1037 {
1038     nsURI *This = NSURI_THIS(iface);
1039     LONG ref = InterlockedDecrement(&This->ref);
1040
1041     TRACE("(%p) ref=%ld\n", This, ref);
1042
1043     if(!ref) {
1044         if(This->container)
1045             nsIWebBrowserChrome_Release(NSWBCHROME(This->container));
1046         nsIURI_Release(This->uri);
1047         HeapFree(GetProcessHeap(), 0, This);
1048     }
1049
1050     return ref;
1051 }
1052
1053 static nsresult NSAPI nsURI_GetSpec(nsIWineURI *iface, nsACString *aSpec)
1054 {
1055     nsURI *This = NSURI_THIS(iface);
1056     TRACE("(%p)->(%p)\n", This, aSpec);
1057     return nsIURI_GetSpec(This->uri, aSpec);
1058 }
1059
1060 static nsresult NSAPI nsURI_SetSpec(nsIWineURI *iface, const nsACString *aSpec)
1061 {
1062     nsURI *This = NSURI_THIS(iface);
1063     TRACE("(%p)->(%p)\n", This, aSpec);
1064     return nsIURI_SetSpec(This->uri, aSpec);
1065 }
1066
1067 static nsresult NSAPI nsURI_GetPrePath(nsIWineURI *iface, nsACString *aPrePath)
1068 {
1069     nsURI *This = NSURI_THIS(iface);
1070     TRACE("(%p)->(%p)\n", This, aPrePath);
1071     return nsIURI_GetPrePath(This->uri, aPrePath);
1072 }
1073
1074 static nsresult NSAPI nsURI_GetScheme(nsIWineURI *iface, nsACString *aScheme)
1075 {
1076     nsURI *This = NSURI_THIS(iface);
1077     TRACE("(%p)->(%p)\n", This, aScheme);
1078     return nsIURI_GetScheme(This->uri, aScheme);
1079 }
1080
1081 static nsresult NSAPI nsURI_SetScheme(nsIWineURI *iface, const nsACString *aScheme)
1082 {
1083     nsURI *This = NSURI_THIS(iface);
1084     TRACE("(%p)->(%p)\n", This, aScheme);
1085     return nsIURI_SetScheme(This->uri, aScheme);
1086 }
1087
1088 static nsresult NSAPI nsURI_GetUserPass(nsIWineURI *iface, nsACString *aUserPass)
1089 {
1090     nsURI *This = NSURI_THIS(iface);
1091     TRACE("(%p)->(%p)\n", This, aUserPass);
1092     return nsIURI_GetUserPass(This->uri, aUserPass);
1093 }
1094
1095 static nsresult NSAPI nsURI_SetUserPass(nsIWineURI *iface, const nsACString *aUserPass)
1096 {
1097     nsURI *This = NSURI_THIS(iface);
1098     TRACE("(%p)->(%p)\n", This, aUserPass);
1099     return nsIURI_SetUserPass(This->uri, aUserPass);
1100 }
1101
1102 static nsresult NSAPI nsURI_GetUsername(nsIWineURI *iface, nsACString *aUsername)
1103 {
1104     nsURI *This = NSURI_THIS(iface);
1105     TRACE("(%p)->(%p)\n", This, aUsername);
1106     return nsIURI_GetUsername(This->uri, aUsername);
1107 }
1108
1109 static nsresult NSAPI nsURI_SetUsername(nsIWineURI *iface, const nsACString *aUsername)
1110 {
1111     nsURI *This = NSURI_THIS(iface);
1112     TRACE("(%p)->(%p)\n", This, aUsername);
1113     return nsIURI_SetUsername(This->uri, aUsername);
1114 }
1115
1116 static nsresult NSAPI nsURI_GetPassword(nsIWineURI *iface, nsACString *aPassword)
1117 {
1118     nsURI *This = NSURI_THIS(iface);
1119     TRACE("(%p)->(%p)\n", This, aPassword);
1120     return nsIURI_GetPassword(This->uri, aPassword);
1121 }
1122
1123 static nsresult NSAPI nsURI_SetPassword(nsIWineURI *iface, const nsACString *aPassword)
1124 {
1125     nsURI *This = NSURI_THIS(iface);
1126     TRACE("(%p)->(%p)\n", This, aPassword);
1127     return nsIURI_SetPassword(This->uri, aPassword);
1128 }
1129
1130 static nsresult NSAPI nsURI_GetHostPort(nsIWineURI *iface, nsACString *aHostPort)
1131 {
1132     nsURI *This = NSURI_THIS(iface);
1133     TRACE("(%p)->(%p)\n", This, aHostPort);
1134     return nsIURI_GetHostPort(This->uri, aHostPort);
1135 }
1136
1137 static nsresult NSAPI nsURI_SetHostPort(nsIWineURI *iface, const nsACString *aHostPort)
1138 {
1139     nsURI *This = NSURI_THIS(iface);
1140     TRACE("(%p)->(%p)\n", This, aHostPort);
1141     return nsIURI_SetHostPort(This->uri, aHostPort);
1142 }
1143
1144 static nsresult NSAPI nsURI_GetHost(nsIWineURI *iface, nsACString *aHost)
1145 {
1146     nsURI *This = NSURI_THIS(iface);
1147     TRACE("(%p)->(%p)\n", This, aHost);
1148     return nsIURI_GetHost(This->uri, aHost);
1149 }
1150
1151 static nsresult NSAPI nsURI_SetHost(nsIWineURI *iface, const nsACString *aHost)
1152 {
1153     nsURI *This = NSURI_THIS(iface);
1154     TRACE("(%p)->(%p)\n", This, aHost);
1155     return nsIURI_SetHost(This->uri, aHost);
1156 }
1157
1158 static nsresult NSAPI nsURI_GetPort(nsIWineURI *iface, PRInt32 *aPort)
1159 {
1160     nsURI *This = NSURI_THIS(iface);
1161     TRACE("(%p)->(%p)\n", This, aPort);
1162     return nsIURI_GetPort(This->uri, aPort);
1163 }
1164
1165 static nsresult NSAPI nsURI_SetPort(nsIWineURI *iface, PRInt32 aPort)
1166 {
1167     nsURI *This = NSURI_THIS(iface);
1168     TRACE("(%p)->(%ld)\n", This, aPort);
1169     return nsIURI_SetPort(This->uri, aPort);
1170 }
1171
1172 static nsresult NSAPI nsURI_GetPath(nsIWineURI *iface, nsACString *aPath)
1173 {
1174     nsURI *This = NSURI_THIS(iface);
1175     TRACE("(%p)->(%p)\n", This, aPath);
1176     return nsIURI_GetPath(This->uri, aPath);
1177 }
1178
1179 static nsresult NSAPI nsURI_SetPath(nsIWineURI *iface, const nsACString *aPath)
1180 {
1181     nsURI *This = NSURI_THIS(iface);
1182     TRACE("(%p)->(%p)\n", This, aPath);
1183     return nsIURI_SetPath(This->uri, aPath);
1184 }
1185
1186 static nsresult NSAPI nsURI_Equals(nsIWineURI *iface, nsIURI *other, PRBool *_retval)
1187 {
1188     nsURI *This = NSURI_THIS(iface);
1189     TRACE("(%p)->(%p %p)\n", This, other, _retval);
1190     return nsIURI_Equals(This->uri, other, _retval);
1191 }
1192
1193 static nsresult NSAPI nsURI_SchemeIs(nsIWineURI *iface, const char *scheme, PRBool *_retval)
1194 {
1195     nsURI *This = NSURI_THIS(iface);
1196     TRACE("(%p)->(%s %p)\n", This, debugstr_a(scheme), _retval);
1197     return nsIURI_SchemeIs(This->uri, scheme, _retval);
1198 }
1199
1200 static nsresult NSAPI nsURI_Clone(nsIWineURI *iface, nsIURI **_retval)
1201 {
1202     nsURI *This = NSURI_THIS(iface);
1203     nsIURI *uri;
1204     nsresult nsres;
1205
1206     TRACE("(%p)->(%p)\n", This, _retval);
1207
1208     nsres = nsIURI_Clone(This->uri, &uri);
1209     if(NS_FAILED(nsres)) {
1210         WARN("Clone failed: %08lx\n", nsres);
1211         return nsres;
1212     }
1213
1214     return create_uri(uri, This->container, _retval);
1215 }
1216
1217 static nsresult NSAPI nsURI_Resolve(nsIWineURI *iface, const nsACString *arelativePath,
1218         nsACString *_retval)
1219 {
1220     nsURI *This = NSURI_THIS(iface);
1221     TRACE("(%p)->(%p %p)\n", This, arelativePath, _retval);
1222     return nsIURI_Resolve(This->uri, arelativePath, _retval);
1223 }
1224
1225 static nsresult NSAPI nsURI_GetAsciiSpec(nsIWineURI *iface, nsACString *aAsciiSpec)
1226 {
1227     nsURI *This = NSURI_THIS(iface);
1228     TRACE("(%p)->(%p)\n", This, aAsciiSpec);
1229     return nsIURI_GetAsciiSpec(This->uri, aAsciiSpec);
1230 }
1231
1232 static nsresult NSAPI nsURI_GetAsciiHost(nsIWineURI *iface, nsACString *aAsciiHost)
1233 {
1234     nsURI *This = NSURI_THIS(iface);
1235     TRACE("(%p)->(%p)\n", This, aAsciiHost);
1236     return nsIURI_GetAsciiHost(This->uri, aAsciiHost);
1237 }
1238
1239 static nsresult NSAPI nsURI_GetOriginCharset(nsIWineURI *iface, nsACString *aOriginCharset)
1240 {
1241     nsURI *This = NSURI_THIS(iface);
1242     TRACE("(%p)->(%p)\n", This, aOriginCharset);
1243     return nsIURI_GetOriginCharset(This->uri, aOriginCharset);
1244 }
1245
1246 static nsresult NSAPI nsURI_GetNSContainer(nsIWineURI *iface, NSContainer **aContainer)
1247 {
1248     nsURI *This = NSURI_THIS(iface);
1249
1250     TRACE("(%p)->(%p)\n", This, aContainer);
1251
1252     if(This->container)
1253         nsIWebBrowserChrome_AddRef(NSWBCHROME(This->container));
1254     *aContainer = This->container;
1255
1256     return NS_OK;
1257 }
1258
1259 static nsresult NSAPI nsURI_SetNSContainer(nsIWineURI *iface, NSContainer *aContainer)
1260 {
1261     nsURI *This = NSURI_THIS(iface);
1262
1263     TRACE("(%p)->(%p)\n", This, aContainer);
1264
1265     if(This->container) {
1266         WARN("Container already set: %p\n", This->container);
1267         nsIWebBrowserChrome_Release(NSWBCHROME(This->container));
1268     }
1269
1270     if(aContainer)
1271         nsIWebBrowserChrome_AddRef(NSWBCHROME(aContainer));
1272     This->container = aContainer;
1273
1274     return NS_OK;
1275 }
1276
1277 #undef NSURI_THIS
1278
1279 static const nsIWineURIVtbl nsWineURIVtbl = {
1280     nsURI_QueryInterface,
1281     nsURI_AddRef,
1282     nsURI_Release,
1283     nsURI_GetSpec,
1284     nsURI_SetSpec,
1285     nsURI_GetPrePath,
1286     nsURI_GetScheme,
1287     nsURI_SetScheme,
1288     nsURI_GetUserPass,
1289     nsURI_SetUserPass,
1290     nsURI_GetUsername,
1291     nsURI_SetUsername,
1292     nsURI_GetPassword,
1293     nsURI_SetPassword,
1294     nsURI_GetHostPort,
1295     nsURI_SetHostPort,
1296     nsURI_GetHost,
1297     nsURI_SetHost,
1298     nsURI_GetPort,
1299     nsURI_SetPort,
1300     nsURI_GetPath,
1301     nsURI_SetPath,
1302     nsURI_Equals,
1303     nsURI_SchemeIs,
1304     nsURI_Clone,
1305     nsURI_Resolve,
1306     nsURI_GetAsciiSpec,
1307     nsURI_GetAsciiHost,
1308     nsURI_GetOriginCharset,
1309     nsURI_GetNSContainer,
1310     nsURI_SetNSContainer,
1311 };
1312
1313 static nsresult create_uri(nsIURI *uri, NSContainer *container, nsIURI **_retval)
1314 {
1315     nsURI *ret = HeapAlloc(GetProcessHeap(), 0, sizeof(nsURI));
1316
1317     ret->lpWineURIVtbl = &nsWineURIVtbl;
1318     ret->ref = 1;
1319     ret->uri = uri;
1320     ret->container = container;
1321
1322     if(container)
1323         nsIWebBrowserChrome_AddRef(NSWBCHROME(container));
1324
1325     TRACE("retval=%p\n", ret);
1326     *_retval = NSURI(ret);
1327     return NS_OK;
1328 }
1329
1330 static nsresult NSAPI nsIOService_QueryInterface(nsIIOService *iface, nsIIDRef riid,
1331                                                  nsQIResult result)
1332 {
1333     *result = NULL;
1334
1335     if(IsEqualGUID(&IID_nsISupports, riid)) {
1336         TRACE("(IID_nsISupports %p)\n", result);
1337         *result = iface;
1338     }else if(IsEqualGUID(&IID_nsIIOService, riid)) {
1339         TRACE("(IID_nsIIOService %p)\n", result);
1340         *result = iface;
1341     }
1342
1343     if(*result) {
1344         nsIIOService_AddRef(iface);
1345         return S_OK;
1346     }
1347
1348     WARN("(%s %p)\n", debugstr_guid(riid), result);
1349     return NS_NOINTERFACE;
1350 }
1351
1352 static nsrefcnt NSAPI nsIOService_AddRef(nsIIOService *iface)
1353 {
1354     return 2;
1355 }
1356
1357 static nsrefcnt NSAPI nsIOService_Release(nsIIOService *iface)
1358 {
1359     return 1;
1360 }
1361
1362 static nsresult NSAPI nsIOService_GetProtocolHandler(nsIIOService *iface, const char *aScheme,
1363                                                      nsIProtocolHandler **_retval)
1364 {
1365     TRACE("(%s %p)\n", debugstr_a(aScheme), _retval);
1366     return nsIIOService_GetProtocolHandler(nsio, aScheme, _retval);
1367 }
1368
1369 static nsresult NSAPI nsIOService_GetProtocolFlags(nsIIOService *iface, const char *aScheme,
1370                                                     PRUint32 *_retval)
1371 {
1372     TRACE("(%s %p)\n", debugstr_a(aScheme), _retval);
1373     return nsIIOService_GetProtocolFlags(nsio, aScheme, _retval);
1374 }
1375
1376 static nsresult NSAPI nsIOService_NewURI(nsIIOService *iface, const nsACString *aSpec,
1377         const char *aOriginCharset, nsIURI *aBaseURI, nsIURI **_retval)
1378 {
1379     const char *spec = NULL;
1380     nsIURI *uri;
1381     PRBool is_javascript = FALSE;
1382     nsresult nsres;
1383
1384     nsACString_GetData(aSpec, &spec, NULL);
1385
1386     TRACE("(%p(%s) %s %p %p)\n", aSpec, debugstr_a(spec), debugstr_a(aOriginCharset),
1387           aBaseURI, _retval);
1388
1389     if(aBaseURI) {
1390         nsACString base_uri_str;
1391         const char *base_uri = NULL;
1392
1393         nsACString_Init(&base_uri_str, NULL);
1394
1395         nsres = nsIURI_GetSpec(aBaseURI, &base_uri_str);
1396         if(NS_SUCCEEDED(nsres)) {
1397             nsACString_GetData(&base_uri_str, &base_uri, NULL);
1398             TRACE("uri=%s\n", debugstr_a(base_uri));
1399         }else {
1400             ERR("GetSpec failed: %08lx\n", nsres);
1401         }
1402
1403         nsACString_Finish(&base_uri_str);
1404     }
1405
1406     nsres = nsIIOService_NewURI(nsio, aSpec, aOriginCharset, aBaseURI, &uri);
1407     if(NS_FAILED(nsres)) {
1408         WARN("NewURI failed: %08lx\n", nsres);
1409         return nsres;
1410     }
1411
1412     nsIURI_SchemeIs(uri, "javascript", &is_javascript);
1413     if(is_javascript) {
1414         TRACE("returning javascript uri: %p\n", uri);
1415         *_retval = uri;
1416         return NS_OK;
1417     }
1418
1419     return create_uri(uri, NULL, _retval);
1420 }
1421
1422 static nsresult NSAPI nsIOService_NewFileURI(nsIIOService *iface, nsIFile *aFile,
1423                                              nsIURI **_retval)
1424 {
1425     TRACE("(%p %p)\n", aFile, _retval);
1426     return nsIIOService_NewFileURI(nsio, aFile, _retval);
1427 }
1428
1429 static nsresult NSAPI nsIOService_NewChannelFromURI(nsIIOService *iface, nsIURI *aURI,
1430                                                      nsIChannel **_retval)
1431 {
1432     nsIChannel *channel = NULL;
1433     nsChannel *ret;
1434     nsIWineURI *wine_uri;
1435     nsresult nsres;
1436
1437     TRACE("(%p %p)\n", aURI, _retval);
1438
1439     nsres = nsIIOService_NewChannelFromURI(nsio, aURI, &channel);
1440     if(NS_FAILED(nsres) && nsres != NS_ERROR_UNKNOWN_PROTOCOL) {
1441         WARN("NewChannelFromURI failed: %08lx\n", nsres);
1442         *_retval = channel;
1443         return nsres;
1444     }
1445
1446     nsres = nsIURI_QueryInterface(aURI, &IID_nsIWineURI, (void**)&wine_uri);
1447     if(NS_FAILED(nsres)) {
1448         WARN("Could not get nsIWineURI: %08lx\n", nsres);
1449         *_retval = channel;
1450         return channel ? NS_OK : NS_ERROR_UNEXPECTED;
1451     }
1452
1453     ret = HeapAlloc(GetProcessHeap(), 0, sizeof(nsChannel));
1454
1455     ret->lpHttpChannelVtbl = &nsChannelVtbl;
1456     ret->lpUploadChannelVtbl = &nsUploadChannelVtbl;
1457     ret->ref = 1;
1458     ret->channel = channel;
1459     ret->http_channel = NULL;
1460     ret->uri = wine_uri;
1461     ret->post_data_stream = NULL;
1462     ret->load_group = NULL;
1463     ret->notif_callback = NULL;
1464     ret->load_flags = 0;
1465     ret->content = NULL;
1466
1467     nsIURI_AddRef(aURI);
1468     ret->original_uri = aURI;
1469
1470     if(channel)
1471         nsIChannel_QueryInterface(channel, &IID_nsIHttpChannel, (void**)&ret->http_channel);
1472
1473     *_retval = NSCHANNEL(ret);
1474     return NS_OK;
1475 }
1476
1477 static nsresult NSAPI nsIOService_NewChannel(nsIIOService *iface, const nsACString *aSpec,
1478         const char *aOriginCharset, nsIURI *aBaseURI, nsIChannel **_retval)
1479 {
1480     TRACE("(%p %s %p %p)\n", aSpec, debugstr_a(aOriginCharset), aBaseURI, _retval);
1481     return nsIIOService_NewChannel(nsio, aSpec, aOriginCharset, aBaseURI, _retval);
1482 }
1483
1484 static nsresult NSAPI nsIOService_GetOffline(nsIIOService *iface, PRBool *aOffline)
1485 {
1486     TRACE("(%p)\n", aOffline);
1487     return nsIIOService_GetOffline(nsio, aOffline);
1488 }
1489
1490 static nsresult NSAPI nsIOService_SetOffline(nsIIOService *iface, PRBool aOffline)
1491 {
1492     TRACE("(%x)\n", aOffline);
1493     return nsIIOService_SetOffline(nsio, aOffline);
1494 }
1495
1496 static nsresult NSAPI nsIOService_AllowPort(nsIIOService *iface, PRInt32 aPort,
1497                                              const char *aScheme, PRBool *_retval)
1498 {
1499     TRACE("(%ld %s %p)\n", aPort, debugstr_a(aScheme), _retval);
1500     return nsIIOService_AllowPort(nsio, aPort, debugstr_a(aScheme), _retval);
1501 }
1502
1503 static nsresult NSAPI nsIOService_ExtractScheme(nsIIOService *iface, const nsACString *urlString,
1504                                                  nsACString * _retval)
1505 {
1506     TRACE("(%p %p)\n", urlString, _retval);
1507     return nsIIOService_ExtractScheme(nsio, urlString, _retval);
1508 }
1509
1510 static const nsIIOServiceVtbl nsIOServiceVtbl = {
1511     nsIOService_QueryInterface,
1512     nsIOService_AddRef,
1513     nsIOService_Release,
1514     nsIOService_GetProtocolHandler,
1515     nsIOService_GetProtocolFlags,
1516     nsIOService_NewURI,
1517     nsIOService_NewFileURI,
1518     nsIOService_NewChannelFromURI,
1519     nsIOService_NewChannel,
1520     nsIOService_GetOffline,
1521     nsIOService_SetOffline,
1522     nsIOService_AllowPort,
1523     nsIOService_ExtractScheme
1524 };
1525
1526 static nsIIOService nsIOService = { &nsIOServiceVtbl };
1527
1528 static nsresult NSAPI nsIOServiceFactory_QueryInterface(nsIFactory *iface, nsIIDRef riid,
1529                                                         nsQIResult result)
1530 {
1531     *result = NULL;
1532
1533     if(IsEqualGUID(&IID_nsISupports, riid)) {
1534         TRACE("(IID_nsISupoprts %p)\n", result);
1535         *result = iface;
1536     }else if(IsEqualGUID(&IID_nsIFactory, riid)) {
1537         TRACE("(IID_nsIFactory %p)\n", result);
1538         *result = iface;
1539     }
1540
1541     if(*result) {
1542         nsIFactory_AddRef(iface);
1543         return NS_OK;
1544     }
1545
1546     WARN("(%s %p)\n", debugstr_guid(riid), result);
1547     return NS_NOINTERFACE;
1548 }
1549
1550 static nsrefcnt NSAPI nsIOServiceFactory_AddRef(nsIFactory *iface)
1551 {
1552     return 2;
1553 }
1554
1555 static nsrefcnt NSAPI nsIOServiceFactory_Release(nsIFactory *iface)
1556 {
1557     return 1;
1558 }
1559
1560 static nsresult NSAPI nsIOServiceFactory_CreateInstance(nsIFactory *iface,
1561         nsISupports *aOuter, const nsIID *iid, void **result)
1562 {
1563     return nsIIOService_QueryInterface(&nsIOService, iid, result);
1564 }
1565
1566 static nsresult NSAPI nsIOServiceFactory_LockFactory(nsIFactory *iface, PRBool lock)
1567 {
1568     WARN("(%x)\n", lock);
1569     return NS_OK;
1570 }
1571
1572 static const nsIFactoryVtbl nsIOServiceFactoryVtbl = {
1573     nsIOServiceFactory_QueryInterface,
1574     nsIOServiceFactory_AddRef,
1575     nsIOServiceFactory_Release,
1576     nsIOServiceFactory_CreateInstance,
1577     nsIOServiceFactory_LockFactory
1578 };
1579
1580 static nsIFactory nsIOServiceFactory = { &nsIOServiceFactoryVtbl };
1581
1582 void init_nsio(nsIComponentManager *component_manager, nsIComponentRegistrar *registrar)
1583 {
1584     nsIFactory *old_factory = NULL;
1585     nsresult nsres;
1586
1587     nsres = nsIComponentManager_GetClassObject(component_manager, &NS_IOSERVICE_CID,
1588                                                &IID_nsIFactory, (void**)&old_factory);
1589     if(NS_FAILED(nsres)) {
1590         ERR("Could not get factory: %08lx\n", nsres);
1591         nsIFactory_Release(old_factory);
1592         return;
1593     }
1594
1595     nsres = nsIFactory_CreateInstance(old_factory, NULL, &IID_nsIIOService, (void**)&nsio);
1596     if(NS_FAILED(nsres)) {
1597         ERR("Couldn not create nsIOService instance %08lx\n", nsres);
1598         nsIFactory_Release(old_factory);
1599         return;
1600     }
1601
1602     nsres = nsIComponentRegistrar_UnregisterFactory(registrar, &NS_IOSERVICE_CID, old_factory);
1603     nsIFactory_Release(old_factory);
1604     if(NS_FAILED(nsres))
1605         ERR("UnregisterFactory failed: %08lx\n", nsres);
1606
1607     nsres = nsIComponentRegistrar_RegisterFactory(registrar, &NS_IOSERVICE_CID,
1608             NS_IOSERVICE_CLASSNAME, NS_IOSERVICE_CONTRACTID, &nsIOServiceFactory);
1609     if(NS_FAILED(nsres))
1610         ERR("RegisterFactory failed: %08lx\n", nsres);
1611 }
1612
1613 nsIURI *get_nsIURI(LPCWSTR url)
1614 {
1615     nsIURI *ret;
1616     nsACString acstr;
1617     nsresult nsres;
1618     char *urla;
1619     int len;
1620
1621     len = WideCharToMultiByte(CP_ACP, 0, url, -1, NULL, -1, NULL, NULL);
1622     urla = HeapAlloc(GetProcessHeap(), 0, len);
1623     WideCharToMultiByte(CP_ACP, 0, url, -1, urla, -1, NULL, NULL);
1624
1625     nsACString_Init(&acstr, urla);
1626
1627     nsres = nsIIOService_NewURI(nsio, &acstr, NULL, NULL, &ret);
1628     if(NS_FAILED(nsres))
1629         FIXME("NewURI failed: %08lx\n", nsres);
1630
1631     nsACString_Finish(&acstr);
1632     HeapFree(GetProcessHeap(), 0, urla);
1633
1634     return ret;
1635 }