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