mshtml: Handle IHlinkFrame service.
[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         nsIURI_Release(This->uri);
860         HeapFree(GetProcessHeap(), 0, This);
861     }
862
863     return ref;
864 }
865
866 static nsresult NSAPI nsURI_GetSpec(nsIWineURI *iface, nsACString *aSpec)
867 {
868     nsURI *This = NSURI_THIS(iface);
869     TRACE("(%p)->(%p)\n", This, aSpec);
870     return nsIURI_GetSpec(This->uri, aSpec);
871 }
872
873 static nsresult NSAPI nsURI_SetSpec(nsIWineURI *iface, const nsACString *aSpec)
874 {
875     nsURI *This = NSURI_THIS(iface);
876     TRACE("(%p)->(%p)\n", This, aSpec);
877     return nsIURI_SetSpec(This->uri, aSpec);
878 }
879
880 static nsresult NSAPI nsURI_GetPrePath(nsIWineURI *iface, nsACString *aPrePath)
881 {
882     nsURI *This = NSURI_THIS(iface);
883     TRACE("(%p)->(%p)\n", This, aPrePath);
884     return nsIURI_GetPrePath(This->uri, aPrePath);
885 }
886
887 static nsresult NSAPI nsURI_GetScheme(nsIWineURI *iface, nsACString *aScheme)
888 {
889     nsURI *This = NSURI_THIS(iface);
890     TRACE("(%p)->(%p)\n", This, aScheme);
891     return nsIURI_GetScheme(This->uri, aScheme);
892 }
893
894 static nsresult NSAPI nsURI_SetScheme(nsIWineURI *iface, const nsACString *aScheme)
895 {
896     nsURI *This = NSURI_THIS(iface);
897     TRACE("(%p)->(%p)\n", This, aScheme);
898     return nsIURI_SetScheme(This->uri, aScheme);
899 }
900
901 static nsresult NSAPI nsURI_GetUserPass(nsIWineURI *iface, nsACString *aUserPass)
902 {
903     nsURI *This = NSURI_THIS(iface);
904     TRACE("(%p)->(%p)\n", This, aUserPass);
905     return nsIURI_GetUserPass(This->uri, aUserPass);
906 }
907
908 static nsresult NSAPI nsURI_SetUserPass(nsIWineURI *iface, const nsACString *aUserPass)
909 {
910     nsURI *This = NSURI_THIS(iface);
911     TRACE("(%p)->(%p)\n", This, aUserPass);
912     return nsIURI_SetUserPass(This->uri, aUserPass);
913 }
914
915 static nsresult NSAPI nsURI_GetUsername(nsIWineURI *iface, nsACString *aUsername)
916 {
917     nsURI *This = NSURI_THIS(iface);
918     TRACE("(%p)->(%p)\n", This, aUsername);
919     return nsIURI_GetUsername(This->uri, aUsername);
920 }
921
922 static nsresult NSAPI nsURI_SetUsername(nsIWineURI *iface, const nsACString *aUsername)
923 {
924     nsURI *This = NSURI_THIS(iface);
925     TRACE("(%p)->(%p)\n", This, aUsername);
926     return nsIURI_SetUsername(This->uri, aUsername);
927 }
928
929 static nsresult NSAPI nsURI_GetPassword(nsIWineURI *iface, nsACString *aPassword)
930 {
931     nsURI *This = NSURI_THIS(iface);
932     TRACE("(%p)->(%p)\n", This, aPassword);
933     return nsIURI_GetPassword(This->uri, aPassword);
934 }
935
936 static nsresult NSAPI nsURI_SetPassword(nsIWineURI *iface, const nsACString *aPassword)
937 {
938     nsURI *This = NSURI_THIS(iface);
939     TRACE("(%p)->(%p)\n", This, aPassword);
940     return nsIURI_SetPassword(This->uri, aPassword);
941 }
942
943 static nsresult NSAPI nsURI_GetHostPort(nsIWineURI *iface, nsACString *aHostPort)
944 {
945     nsURI *This = NSURI_THIS(iface);
946     TRACE("(%p)->(%p)\n", This, aHostPort);
947     return nsIURI_GetHostPort(This->uri, aHostPort);
948 }
949
950 static nsresult NSAPI nsURI_SetHostPort(nsIWineURI *iface, const nsACString *aHostPort)
951 {
952     nsURI *This = NSURI_THIS(iface);
953     TRACE("(%p)->(%p)\n", This, aHostPort);
954     return nsIURI_SetHostPort(This->uri, aHostPort);
955 }
956
957 static nsresult NSAPI nsURI_GetHost(nsIWineURI *iface, nsACString *aHost)
958 {
959     nsURI *This = NSURI_THIS(iface);
960     TRACE("(%p)->(%p)\n", This, aHost);
961     return nsIURI_GetHost(This->uri, aHost);
962 }
963
964 static nsresult NSAPI nsURI_SetHost(nsIWineURI *iface, const nsACString *aHost)
965 {
966     nsURI *This = NSURI_THIS(iface);
967     TRACE("(%p)->(%p)\n", This, aHost);
968     return nsIURI_SetHost(This->uri, aHost);
969 }
970
971 static nsresult NSAPI nsURI_GetPort(nsIWineURI *iface, PRInt32 *aPort)
972 {
973     nsURI *This = NSURI_THIS(iface);
974     TRACE("(%p)->(%p)\n", This, aPort);
975     return nsIURI_GetPort(This->uri, aPort);
976 }
977
978 static nsresult NSAPI nsURI_SetPort(nsIWineURI *iface, PRInt32 aPort)
979 {
980     nsURI *This = NSURI_THIS(iface);
981     TRACE("(%p)->(%ld)\n", This, aPort);
982     return nsIURI_SetPort(This->uri, aPort);
983 }
984
985 static nsresult NSAPI nsURI_GetPath(nsIWineURI *iface, nsACString *aPath)
986 {
987     nsURI *This = NSURI_THIS(iface);
988     TRACE("(%p)->(%p)\n", This, aPath);
989     return nsIURI_GetPath(This->uri, aPath);
990 }
991
992 static nsresult NSAPI nsURI_SetPath(nsIWineURI *iface, const nsACString *aPath)
993 {
994     nsURI *This = NSURI_THIS(iface);
995     TRACE("(%p)->(%p)\n", This, aPath);
996     return nsIURI_SetPath(This->uri, aPath);
997 }
998
999 static nsresult NSAPI nsURI_Equals(nsIWineURI *iface, nsIURI *other, PRBool *_retval)
1000 {
1001     nsURI *This = NSURI_THIS(iface);
1002     TRACE("(%p)->(%p %p)\n", This, other, _retval);
1003     return nsIURI_Equals(This->uri, other, _retval);
1004 }
1005
1006 static nsresult NSAPI nsURI_SchemeIs(nsIWineURI *iface, const char *scheme, PRBool *_retval)
1007 {
1008     nsURI *This = NSURI_THIS(iface);
1009     TRACE("(%p)->(%s %p)\n", This, debugstr_a(scheme), _retval);
1010     return nsIURI_SchemeIs(This->uri, scheme, _retval);
1011 }
1012
1013 static nsresult NSAPI nsURI_Clone(nsIWineURI *iface, nsIURI **_retval)
1014 {
1015     nsURI *This = NSURI_THIS(iface);
1016     TRACE("(%p)->(%p)\n", This, _retval);
1017     return nsIURI_Clone(This->uri, _retval);
1018 }
1019
1020 static nsresult NSAPI nsURI_Resolve(nsIWineURI *iface, const nsACString *arelativePath,
1021         nsACString *_retval)
1022 {
1023     nsURI *This = NSURI_THIS(iface);
1024     TRACE("(%p)->(%p %p)\n", This, arelativePath, _retval);
1025     return nsIURI_Resolve(This->uri, arelativePath, _retval);
1026 }
1027
1028 static nsresult NSAPI nsURI_GetAsciiSpec(nsIWineURI *iface, nsACString *aAsciiSpec)
1029 {
1030     nsURI *This = NSURI_THIS(iface);
1031     TRACE("(%p)->(%p)\n", This, aAsciiSpec);
1032     return nsIURI_GetAsciiSpec(This->uri, aAsciiSpec);
1033 }
1034
1035 static nsresult NSAPI nsURI_GetAsciiHost(nsIWineURI *iface, nsACString *aAsciiHost)
1036 {
1037     nsURI *This = NSURI_THIS(iface);
1038     TRACE("(%p)->(%p)\n", This, aAsciiHost);
1039     return nsIURI_GetAsciiHost(This->uri, aAsciiHost);
1040 }
1041
1042 static nsresult NSAPI nsURI_GetOriginCharset(nsIWineURI *iface, nsACString *aOriginCharset)
1043 {
1044     nsURI *This = NSURI_THIS(iface);
1045     TRACE("(%p)->(%p)\n", This, aOriginCharset);
1046     return nsIURI_GetOriginCharset(This->uri, aOriginCharset);
1047 }
1048
1049 static nsresult NSAPI nsURI_GetNSContainer(nsIWineURI *iface, NSContainer **aContainer)
1050 {
1051     nsURI *This = NSURI_THIS(iface);
1052
1053     TRACE("(%p)->(%p)\n", This, aContainer);
1054
1055     if(This->container)
1056         nsIWebBrowserChrome_AddRef(NSWBCHROME(This->container));
1057     *aContainer = This->container;
1058
1059     return NS_OK;
1060 }
1061
1062 static nsresult NSAPI nsURI_SetNSContainer(nsIWineURI *iface, NSContainer *aContainer)
1063 {
1064     nsURI *This = NSURI_THIS(iface);
1065
1066     TRACE("(%p)->(%p)\n", This, aContainer);
1067
1068     if(This->container) {
1069         WARN("Container already set: %p\n", This->container);
1070         nsIWebBrowserChrome_Release(NSWBCHROME(This->container));
1071     }
1072
1073     if(aContainer)
1074         nsIWebBrowserChrome_AddRef(NSWBCHROME(aContainer));
1075     This->container = aContainer;
1076
1077     return NS_OK;
1078 }
1079
1080 #undef NSURI_THIS
1081
1082 static const nsIWineURIVtbl nsWineURIVtbl = {
1083     nsURI_QueryInterface,
1084     nsURI_AddRef,
1085     nsURI_Release,
1086     nsURI_GetSpec,
1087     nsURI_SetSpec,
1088     nsURI_GetPrePath,
1089     nsURI_GetScheme,
1090     nsURI_SetScheme,
1091     nsURI_GetUserPass,
1092     nsURI_SetUserPass,
1093     nsURI_GetUsername,
1094     nsURI_SetUsername,
1095     nsURI_GetPassword,
1096     nsURI_SetPassword,
1097     nsURI_GetHostPort,
1098     nsURI_SetHostPort,
1099     nsURI_GetHost,
1100     nsURI_SetHost,
1101     nsURI_GetPort,
1102     nsURI_SetPort,
1103     nsURI_GetPath,
1104     nsURI_SetPath,
1105     nsURI_Equals,
1106     nsURI_SchemeIs,
1107     nsURI_Clone,
1108     nsURI_Resolve,
1109     nsURI_GetAsciiSpec,
1110     nsURI_GetAsciiHost,
1111     nsURI_GetOriginCharset,
1112     nsURI_GetNSContainer,
1113     nsURI_SetNSContainer,
1114 };
1115
1116 static nsresult NSAPI nsIOService_QueryInterface(nsIIOService *iface, nsIIDRef riid,
1117                                                  nsQIResult result)
1118 {
1119     *result = NULL;
1120
1121     if(IsEqualGUID(&IID_nsISupports, riid)) {
1122         TRACE("(IID_nsISupports %p)\n", result);
1123         *result = iface;
1124     }else if(IsEqualGUID(&IID_nsIIOService, riid)) {
1125         TRACE("(IID_nsIIOService %p)\n", result);
1126         *result = iface;
1127     }
1128
1129     if(*result) {
1130         nsIIOService_AddRef(iface);
1131         return S_OK;
1132     }
1133
1134     WARN("(%s %p)\n", debugstr_guid(riid), result);
1135     return NS_NOINTERFACE;
1136 }
1137
1138 static nsrefcnt NSAPI nsIOService_AddRef(nsIIOService *iface)
1139 {
1140     return 2;
1141 }
1142
1143 static nsrefcnt NSAPI nsIOService_Release(nsIIOService *iface)
1144 {
1145     return 1;
1146 }
1147
1148 static nsresult NSAPI nsIOService_GetProtocolHandler(nsIIOService *iface, const char *aScheme,
1149                                                      nsIProtocolHandler **_retval)
1150 {
1151     TRACE("(%s %p)\n", debugstr_a(aScheme), _retval);
1152     return nsIIOService_GetProtocolHandler(nsio, aScheme, _retval);
1153 }
1154
1155 static nsresult NSAPI nsIOService_GetProtocolFlags(nsIIOService *iface, const char *aScheme,
1156                                                     PRUint32 *_retval)
1157 {
1158     TRACE("(%s %p)\n", debugstr_a(aScheme), _retval);
1159     return nsIIOService_GetProtocolFlags(nsio, aScheme, _retval);
1160 }
1161
1162 static nsresult NSAPI nsIOService_NewURI(nsIIOService *iface, const nsACString *aSpec,
1163         const char *aOriginCharset, nsIURI *aBaseURI, nsIURI **_retval)
1164 {
1165     const char *spec = NULL;
1166     nsIURI *uri;
1167     nsURI *ret;
1168     PRBool is_javascript = FALSE;
1169     nsresult nsres;
1170
1171     nsACString_GetData(aSpec, &spec, NULL);
1172
1173     TRACE("(%p(%s) %s %p %p)\n", aSpec, debugstr_a(spec), debugstr_a(aOriginCharset),
1174           aBaseURI, _retval);
1175
1176     if(aBaseURI) {
1177         nsACString *base_uri_str = nsACString_Create();
1178         const char *base_uri = NULL;
1179
1180         nsres = nsIURI_GetSpec(aBaseURI, base_uri_str);
1181         if(NS_SUCCEEDED(nsres)) {
1182             nsACString_GetData(base_uri_str, &base_uri, NULL);
1183             TRACE("uri=%s\n", debugstr_a(base_uri));
1184         }else {
1185             ERR("GetSpec failed: %08lx\n", nsres);
1186         }
1187
1188         nsACString_Destroy(base_uri_str);
1189     }
1190
1191     nsres = nsIIOService_NewURI(nsio, aSpec, aOriginCharset, aBaseURI, &uri);
1192     if(NS_FAILED(nsres)) {
1193         WARN("NewURI failed: %08lx\n", nsres);
1194         return nsres;
1195     }
1196
1197     nsIURI_SchemeIs(uri, "javascript", &is_javascript);
1198     if(is_javascript) {
1199         TRACE("returning javascript uri: %p\n", uri);
1200         *_retval = uri;
1201         return NS_OK;
1202     }
1203
1204     ret = HeapAlloc(GetProcessHeap(), 0, sizeof(nsURI));
1205
1206     ret->lpWineURIVtbl = &nsWineURIVtbl;
1207     ret->ref = 1;
1208     ret->uri = uri;
1209     ret->container = NULL;
1210
1211     TRACE("_retval = %p\n", ret);
1212     *_retval = NSURI(ret);
1213     return NS_OK;
1214 }
1215
1216 static nsresult NSAPI nsIOService_NewFileURI(nsIIOService *iface, nsIFile *aFile,
1217                                              nsIURI **_retval)
1218 {
1219     TRACE("(%p %p)\n", aFile, _retval);
1220     return nsIIOService_NewFileURI(nsio, aFile, _retval);
1221 }
1222
1223 static nsresult NSAPI nsIOService_NewChannelFromURI(nsIIOService *iface, nsIURI *aURI,
1224                                                      nsIChannel **_retval)
1225 {
1226     nsIChannel *channel = NULL;
1227     nsChannel *ret;
1228     nsIWineURI *wine_uri;
1229     nsresult nsres;
1230
1231     TRACE("(%p %p)\n", aURI, _retval);
1232
1233     nsres = nsIIOService_NewChannelFromURI(nsio, aURI, &channel);
1234     if(NS_FAILED(nsres)) {
1235         WARN("NewChannelFromURI failed: %08lx\n", nsres);
1236         *_retval = channel;
1237         return nsres;
1238     }
1239
1240     nsres = nsIURI_QueryInterface(aURI, &IID_nsIWineURI, (void**)&wine_uri);
1241     if(NS_FAILED(nsres)) {
1242         WARN("Could not get nsIWineURI: %08lx\n", nsres);
1243         *_retval = channel;
1244         return NS_OK;
1245     }
1246
1247     ret = HeapAlloc(GetProcessHeap(), 0, sizeof(nsChannel));
1248
1249     ret->lpHttpChannelVtbl = &nsChannelVtbl;
1250     ret->lpUploadChannelVtbl = &nsUploadChannelVtbl;
1251     ret->ref = 1;
1252     ret->channel = channel;
1253     ret->http_channel = NULL;
1254     ret->uri = wine_uri;
1255     ret->post_data_stream = NULL;
1256
1257     nsIChannel_QueryInterface(ret->channel, &IID_nsIHttpChannel, (void**)&ret->http_channel);
1258
1259     *_retval = NSCHANNEL(ret);
1260     return NS_OK;
1261 }
1262
1263 static nsresult NSAPI nsIOService_NewChannel(nsIIOService *iface, const nsACString *aSpec,
1264         const char *aOriginCharset, nsIURI *aBaseURI, nsIChannel **_retval)
1265 {
1266     TRACE("(%p %s %p %p)\n", aSpec, debugstr_a(aOriginCharset), aBaseURI, _retval);
1267     return nsIIOService_NewChannel(nsio, aSpec, aOriginCharset, aBaseURI, _retval);
1268 }
1269
1270 static nsresult NSAPI nsIOService_GetOffline(nsIIOService *iface, PRBool *aOffline)
1271 {
1272     TRACE("(%p)\n", aOffline);
1273     return nsIIOService_GetOffline(nsio, aOffline);
1274 }
1275
1276 static nsresult NSAPI nsIOService_SetOffline(nsIIOService *iface, PRBool aOffline)
1277 {
1278     TRACE("(%x)\n", aOffline);
1279     return nsIIOService_SetOffline(nsio, aOffline);
1280 }
1281
1282 static nsresult NSAPI nsIOService_AllowPort(nsIIOService *iface, PRInt32 aPort,
1283                                              const char *aScheme, PRBool *_retval)
1284 {
1285     TRACE("(%ld %s %p)\n", aPort, debugstr_a(aScheme), _retval);
1286     return nsIIOService_AllowPort(nsio, aPort, debugstr_a(aScheme), _retval);
1287 }
1288
1289 static nsresult NSAPI nsIOService_ExtractScheme(nsIIOService *iface, const nsACString *urlString,
1290                                                  nsACString * _retval)
1291 {
1292     TRACE("(%p %p)\n", urlString, _retval);
1293     return nsIIOService_ExtractScheme(nsio, urlString, _retval);
1294 }
1295
1296 static const nsIIOServiceVtbl nsIOServiceVtbl = {
1297     nsIOService_QueryInterface,
1298     nsIOService_AddRef,
1299     nsIOService_Release,
1300     nsIOService_GetProtocolHandler,
1301     nsIOService_GetProtocolFlags,
1302     nsIOService_NewURI,
1303     nsIOService_NewFileURI,
1304     nsIOService_NewChannelFromURI,
1305     nsIOService_NewChannel,
1306     nsIOService_GetOffline,
1307     nsIOService_SetOffline,
1308     nsIOService_AllowPort,
1309     nsIOService_ExtractScheme
1310 };
1311
1312 static nsIIOService nsIOService = { &nsIOServiceVtbl };
1313
1314 static nsresult NSAPI nsIOServiceFactory_QueryInterface(nsIFactory *iface, nsIIDRef riid,
1315                                                         nsQIResult result)
1316 {
1317     *result = NULL;
1318
1319     if(IsEqualGUID(&IID_nsISupports, riid)) {
1320         TRACE("(IID_nsISupoprts %p)\n", result);
1321         *result = iface;
1322     }else if(IsEqualGUID(&IID_nsIFactory, riid)) {
1323         TRACE("(IID_nsIFactory %p)\n", result);
1324         *result = iface;
1325     }
1326
1327     if(*result) {
1328         nsIFactory_AddRef(iface);
1329         return NS_OK;
1330     }
1331
1332     WARN("(%s %p)\n", debugstr_guid(riid), result);
1333     return NS_NOINTERFACE;
1334 }
1335
1336 static nsrefcnt NSAPI nsIOServiceFactory_AddRef(nsIFactory *iface)
1337 {
1338     return 2;
1339 }
1340
1341 static nsrefcnt NSAPI nsIOServiceFactory_Release(nsIFactory *iface)
1342 {
1343     return 1;
1344 }
1345
1346 static nsresult NSAPI nsIOServiceFactory_CreateInstance(nsIFactory *iface,
1347         nsISupports *aOuter, const nsIID *iid, void **result)
1348 {
1349     return nsIIOService_QueryInterface(&nsIOService, iid, result);
1350 }
1351
1352 static nsresult NSAPI nsIOServiceFactory_LockFactory(nsIFactory *iface, PRBool lock)
1353 {
1354     WARN("(%x)\n", lock);
1355     return NS_OK;
1356 }
1357
1358 static const nsIFactoryVtbl nsIOServiceFactoryVtbl = {
1359     nsIOServiceFactory_QueryInterface,
1360     nsIOServiceFactory_AddRef,
1361     nsIOServiceFactory_Release,
1362     nsIOServiceFactory_CreateInstance,
1363     nsIOServiceFactory_LockFactory
1364 };
1365
1366 static nsIFactory nsIOServiceFactory = { &nsIOServiceFactoryVtbl };
1367
1368 void init_nsio(nsIComponentManager *component_manager, nsIComponentRegistrar *registrar)
1369 {
1370     nsIFactory *old_factory = NULL;
1371     nsresult nsres;
1372
1373     nsres = nsIComponentManager_GetClassObject(component_manager, &NS_IOSERVICE_CID,
1374                                                &IID_nsIFactory, (void**)&old_factory);
1375     if(NS_FAILED(nsres)) {
1376         ERR("Could not get factory: %08lx\n", nsres);
1377         nsIFactory_Release(old_factory);
1378         return;
1379     }
1380
1381     nsres = nsIFactory_CreateInstance(old_factory, NULL, &IID_nsIIOService, (void**)&nsio);
1382     if(NS_FAILED(nsres)) {
1383         ERR("Couldn not create nsIOService instance %08lx\n", nsres);
1384         nsIFactory_Release(old_factory);
1385         return;
1386     }
1387
1388     nsres = nsIComponentRegistrar_UnregisterFactory(registrar, &NS_IOSERVICE_CID, old_factory);
1389     nsIFactory_Release(old_factory);
1390     if(NS_FAILED(nsres))
1391         ERR("UnregisterFactory failed: %08lx\n", nsres);
1392
1393     nsres = nsIComponentRegistrar_RegisterFactory(registrar, &NS_IOSERVICE_CID,
1394             NS_IOSERVICE_CLASSNAME, NS_IOSERVICE_CONTRACTID, &nsIOServiceFactory);
1395     if(NS_FAILED(nsres))
1396         ERR("RegisterFactory failed: %08lx\n", nsres);
1397 }
1398
1399 nsIURI *get_nsIURI(LPCWSTR url)
1400 {
1401     nsIURI *ret;
1402     nsACString *acstr;
1403     nsresult nsres;
1404     char *urla;
1405     int len;
1406
1407     len = WideCharToMultiByte(CP_ACP, 0, url, -1, NULL, -1, NULL, NULL);
1408     urla = HeapAlloc(GetProcessHeap(), 0, len);
1409     WideCharToMultiByte(CP_ACP, 0, url, -1, urla, -1, NULL, NULL);
1410
1411     acstr = nsACString_Create();
1412     nsACString_SetData(acstr, urla);
1413
1414     nsres = nsIIOService_NewURI(nsio, acstr, NULL, NULL, &ret);
1415     if(NS_FAILED(nsres))
1416         FIXME("NewURI failed: %08lx\n", nsres);
1417
1418     nsACString_Destroy(acstr);
1419     HeapFree(GetProcessHeap(), 0, urla);
1420
1421     return ret;
1422 }