user32: Reimplement 16-bit clipboard functions on top of the 32-bit ones.
[wine] / dlls / mshtml / nsio.c
1 /*
2  * Copyright 2006-2007 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "ole2.h"
30 #include "shlguid.h"
31 #include "wininet.h"
32 #include "shlwapi.h"
33
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
36
37 #include "mshtml_private.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
40
41 #define LOAD_INITIAL_DOCUMENT_URI 0x80000
42
43 #define NS_IOSERVICE_CLASSNAME "nsIOService"
44 #define NS_IOSERVICE_CONTRACTID "@mozilla.org/network/io-service;1"
45
46 static const IID NS_IOSERVICE_CID =
47     {0x9ac9e770, 0x18bc, 0x11d3, {0x93, 0x37, 0x00, 0x10, 0x4b, 0xa0, 0xfd, 0x40}};
48
49 static nsIIOService *nsio = NULL;
50 static nsINetUtil *net_util;
51
52 static const WCHAR about_blankW[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
53
54 typedef struct {
55     const nsIWineURIVtbl *lpWineURIVtbl;
56
57     LONG ref;
58
59     nsIURI *uri;
60     nsIURL *nsurl;
61     NSContainer *container;
62     windowref_t *window_ref;
63     LPWSTR wine_url;
64     PRBool is_doc_uri;
65     BOOL use_wine_url;
66 } nsURI;
67
68 #define NSURI(x)         ((nsIURI*)            &(x)->lpWineURIVtbl)
69 #define NSWINEURI(x)     ((nsIWineURI*)        &(x)->lpWineURIVtbl)
70
71 static nsresult create_uri(nsIURI*,HTMLWindow*,NSContainer*,nsIWineURI**);
72
73 static const char *debugstr_nsacstr(const nsACString *nsstr)
74 {
75     const char *data;
76
77     nsACString_GetData(nsstr, &data);
78     return debugstr_a(data);
79 }
80
81 HRESULT nsuri_to_url(LPCWSTR nsuri, BOOL ret_empty, BSTR *ret)
82 {
83     const WCHAR *ptr = nsuri;
84
85     static const WCHAR wine_prefixW[] = {'w','i','n','e',':'};
86
87     if(!strncmpW(nsuri, wine_prefixW, sizeof(wine_prefixW)/sizeof(WCHAR)))
88         ptr += sizeof(wine_prefixW)/sizeof(WCHAR);
89
90     if(*ptr || ret_empty) {
91         *ret = SysAllocString(ptr);
92         if(!*ret)
93             return E_OUTOFMEMORY;
94     }else {
95         *ret = NULL;
96     }
97
98     TRACE("%s -> %s\n", debugstr_w(nsuri), debugstr_w(*ret));
99     return S_OK;
100 }
101
102 static BOOL exec_shldocvw_67(HTMLDocumentObj *doc, LPCWSTR url)
103 {
104     IOleCommandTarget *cmdtrg = NULL;
105     HRESULT hres;
106
107     hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&cmdtrg);
108     if(SUCCEEDED(hres)) {
109         VARIANT varUrl, varRes;
110
111         V_VT(&varUrl) = VT_BSTR;
112         V_BSTR(&varUrl) = SysAllocString(url);
113         V_VT(&varRes) = VT_BOOL;
114
115         hres = IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 67, 0, &varUrl, &varRes);
116
117         IOleCommandTarget_Release(cmdtrg);
118         SysFreeString(V_BSTR(&varUrl));
119
120         if(SUCCEEDED(hres) && !V_BOOL(&varRes)) {
121             TRACE("got VARIANT_FALSE, do not load\n");
122             return FALSE;
123         }
124     }
125
126     return TRUE;
127 }
128
129 static BOOL before_async_open(nsChannel *channel, NSContainer *container)
130 {
131     HTMLDocumentObj *doc = container->doc;
132     DWORD hlnf = 0;
133     LPCWSTR uri;
134     HRESULT hres;
135
136     nsIWineURI_GetWineURL(channel->uri, &uri);
137     if(!uri) {
138         ERR("GetWineURL returned NULL\n");
139         return TRUE;
140     }
141
142     if(!doc) {
143         NSContainer *container_iter = container;
144
145         hlnf = HLNF_OPENINNEWWINDOW;
146         while(!container_iter->doc)
147             container_iter = container_iter->parent;
148         doc = container_iter->doc;
149     }
150
151     if(!doc->client)
152         return TRUE;
153
154     if(!hlnf && !exec_shldocvw_67(doc, uri))
155         return FALSE;
156
157     hres = hlink_frame_navigate(&doc->basedoc, uri, channel->post_data_stream, hlnf);
158     return hres != S_OK;
159 }
160
161 static inline BOOL is_http_channel(nsChannel *This)
162 {
163     return This->url_scheme == URL_SCHEME_HTTP || This->url_scheme == URL_SCHEME_HTTP;
164 }
165
166 #define NSCHANNEL_THIS(iface) DEFINE_THIS(nsChannel, HttpChannel, iface)
167
168 static nsresult NSAPI nsChannel_QueryInterface(nsIHttpChannel *iface, nsIIDRef riid, nsQIResult result)
169 {
170     nsChannel *This = NSCHANNEL_THIS(iface);
171
172     if(IsEqualGUID(&IID_nsISupports, riid)) {
173         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
174         *result = NSCHANNEL(This);
175     }else if(IsEqualGUID(&IID_nsIRequest, riid)) {
176         TRACE("(%p)->(IID_nsIRequest %p)\n", This, result);
177         *result = NSCHANNEL(This);
178     }else if(IsEqualGUID(&IID_nsIChannel, riid)) {
179         TRACE("(%p)->(IID_nsIChannel %p)\n", This, result);
180         *result = NSCHANNEL(This);
181     }else if(IsEqualGUID(&IID_nsIHttpChannel, riid)) {
182         TRACE("(%p)->(IID_nsIHttpChannel %p)\n", This, result);
183         *result = is_http_channel(This) ? NSHTTPCHANNEL(This) : NULL;
184     }else if(IsEqualGUID(&IID_nsIUploadChannel, riid)) {
185         TRACE("(%p)->(IID_nsIUploadChannel %p)\n", This, result);
186         *result = NSUPCHANNEL(This);
187     }else if(IsEqualGUID(&IID_nsIHttpChannelInternal, riid)) {
188         TRACE("(%p)->(IID_nsIHttpChannelInternal %p)\n", This, result);
189         *result = is_http_channel(This) ? NSHTTPINTERNAL(This) : NULL;
190     }else {
191         TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
192         *result = NULL;
193     }
194
195     if(*result) {
196         nsIChannel_AddRef(NSCHANNEL(This));
197         return NS_OK;
198     }
199
200     return NS_NOINTERFACE;
201 }
202
203 static nsrefcnt NSAPI nsChannel_AddRef(nsIHttpChannel *iface)
204 {
205     nsChannel *This = NSCHANNEL_THIS(iface);
206     nsrefcnt ref = InterlockedIncrement(&This->ref);
207
208     TRACE("(%p) ref=%d\n", This, ref);
209
210     return ref;
211 }
212
213 static nsrefcnt NSAPI nsChannel_Release(nsIHttpChannel *iface)
214 {
215     nsChannel *This = NSCHANNEL_THIS(iface);
216     LONG ref = InterlockedDecrement(&This->ref);
217
218     if(!ref) {
219         nsIWineURI_Release(This->uri);
220         if(This->channel)
221             nsIChannel_Release(This->channel);
222         if(This->owner)
223             nsISupports_Release(This->owner);
224         if(This->post_data_stream)
225             nsIInputStream_Release(This->post_data_stream);
226         if(This->load_group)
227             nsILoadGroup_Release(This->load_group);
228         if(This->notif_callback)
229             nsIInterfaceRequestor_Release(This->notif_callback);
230         if(This->original_uri)
231             nsIURI_Release(This->original_uri);
232         heap_free(This->content_type);
233         heap_free(This->charset);
234         heap_free(This);
235     }
236
237     return ref;
238 }
239
240 static nsresult NSAPI nsChannel_GetName(nsIHttpChannel *iface, nsACString *aName)
241 {
242     nsChannel *This = NSCHANNEL_THIS(iface);
243
244     TRACE("(%p)->(%p)\n", This, aName);
245
246     if(This->channel)
247         return nsIChannel_GetName(This->channel, aName);
248
249     return NS_ERROR_NOT_IMPLEMENTED;
250 }
251
252 static nsresult NSAPI nsChannel_IsPending(nsIHttpChannel *iface, PRBool *_retval)
253 {
254     nsChannel *This = NSCHANNEL_THIS(iface);
255
256     TRACE("(%p)->(%p)\n", This, _retval);
257
258     if(This->channel)
259         return nsIChannel_IsPending(This->channel, _retval);
260
261     FIXME("default action not implemented\n");
262     return NS_ERROR_NOT_IMPLEMENTED;
263 }
264
265 static nsresult NSAPI nsChannel_GetStatus(nsIHttpChannel *iface, nsresult *aStatus)
266 {
267     nsChannel *This = NSCHANNEL_THIS(iface);
268
269     TRACE("(%p)->(%p)\n", This, aStatus);
270
271     if(This->channel)
272         return nsIChannel_GetStatus(This->channel, aStatus);
273
274     TRACE("returning NS_OK\n");
275     return *aStatus = NS_OK;
276 }
277
278 static nsresult NSAPI nsChannel_Cancel(nsIHttpChannel *iface, nsresult aStatus)
279 {
280     nsChannel *This = NSCHANNEL_THIS(iface);
281
282     TRACE("(%p)->(%08x)\n", This, aStatus);
283
284     if(This->channel)
285         return nsIChannel_Cancel(This->channel, aStatus);
286
287     FIXME("default action not implemented\n");
288     return NS_ERROR_NOT_IMPLEMENTED;
289 }
290
291 static nsresult NSAPI nsChannel_Suspend(nsIHttpChannel *iface)
292 {
293     nsChannel *This = NSCHANNEL_THIS(iface);
294
295     TRACE("(%p)\n", This);
296
297     if(This->channel)
298         return nsIChannel_Suspend(This->channel);
299
300     FIXME("default action not implemented\n");
301     return NS_ERROR_NOT_IMPLEMENTED;
302 }
303
304 static nsresult NSAPI nsChannel_Resume(nsIHttpChannel *iface)
305 {
306     nsChannel *This = NSCHANNEL_THIS(iface);
307
308     TRACE("(%p)\n", This);
309
310     if(This->channel)
311         return nsIChannel_Resume(This->channel);
312
313     FIXME("default action not implemented\n");
314     return NS_ERROR_NOT_IMPLEMENTED;
315 }
316
317 static nsresult NSAPI nsChannel_GetLoadGroup(nsIHttpChannel *iface, nsILoadGroup **aLoadGroup)
318 {
319     nsChannel *This = NSCHANNEL_THIS(iface);
320
321     TRACE("(%p)->(%p)\n", This, aLoadGroup);
322
323     if(This->load_group)
324         nsILoadGroup_AddRef(This->load_group);
325
326     *aLoadGroup = This->load_group;
327     return NS_OK;
328 }
329
330 static nsresult NSAPI nsChannel_SetLoadGroup(nsIHttpChannel *iface, nsILoadGroup *aLoadGroup)
331 {
332     nsChannel *This = NSCHANNEL_THIS(iface);
333
334     TRACE("(%p)->(%p)\n", This, aLoadGroup);
335
336     if(This->load_group)
337         nsILoadGroup_Release(This->load_group);
338     if(aLoadGroup)
339         nsILoadGroup_AddRef(aLoadGroup);
340
341     This->load_group = aLoadGroup;
342
343     if(This->channel)
344         return nsIChannel_SetLoadGroup(This->channel, aLoadGroup);
345     return NS_OK;
346 }
347
348 static nsresult NSAPI nsChannel_GetLoadFlags(nsIHttpChannel *iface, nsLoadFlags *aLoadFlags)
349 {
350     nsChannel *This = NSCHANNEL_THIS(iface);
351
352     TRACE("(%p)->(%p)\n", This, aLoadFlags);
353
354     *aLoadFlags = This->load_flags;
355     return NS_OK;
356 }
357
358 static nsresult NSAPI nsChannel_SetLoadFlags(nsIHttpChannel *iface, nsLoadFlags aLoadFlags)
359 {
360     nsChannel *This = NSCHANNEL_THIS(iface);
361
362     TRACE("(%p)->(%08x)\n", This, aLoadFlags);
363
364     This->load_flags = aLoadFlags;
365
366     if(This->channel)
367         return nsIChannel_SetLoadFlags(This->channel, aLoadFlags);
368     return NS_OK;
369 }
370
371 static nsresult NSAPI nsChannel_GetOriginalURI(nsIHttpChannel *iface, nsIURI **aOriginalURI)
372 {
373     nsChannel *This = NSCHANNEL_THIS(iface);
374
375     TRACE("(%p)->(%p)\n", This, aOriginalURI);
376
377     if(This->original_uri)
378         nsIURI_AddRef(This->original_uri);
379
380     *aOriginalURI = This->original_uri;
381     return NS_OK;
382 }
383
384 static nsresult NSAPI nsChannel_SetOriginalURI(nsIHttpChannel *iface, nsIURI *aOriginalURI)
385 {
386     nsChannel *This = NSCHANNEL_THIS(iface);
387
388     TRACE("(%p)->(%p)\n", This, aOriginalURI);
389
390     if(This->original_uri)
391         nsIURI_Release(This->original_uri);
392
393     nsIURI_AddRef(aOriginalURI);
394     This->original_uri = aOriginalURI;
395
396     if(This->channel)
397         return nsIChannel_SetOriginalURI(This->channel, aOriginalURI);
398     return NS_OK;
399 }
400
401 static nsresult NSAPI nsChannel_GetURI(nsIHttpChannel *iface, nsIURI **aURI)
402 {
403     nsChannel *This = NSCHANNEL_THIS(iface);
404
405     TRACE("(%p)->(%p)\n", This, aURI);
406
407     nsIWineURI_AddRef(This->uri);
408     *aURI = (nsIURI*)This->uri;
409
410     return NS_OK;
411 }
412
413 static nsresult NSAPI nsChannel_GetOwner(nsIHttpChannel *iface, nsISupports **aOwner)
414 {
415     nsChannel *This = NSCHANNEL_THIS(iface);
416
417     TRACE("(%p)->(%p)\n", This, aOwner);
418
419     if(This->channel)
420         return nsIChannel_GetOwner(This->channel, aOwner);
421
422     if(This->owner)
423         nsISupports_AddRef(This->owner);
424     *aOwner = This->owner;
425
426     return NS_OK;
427 }
428
429 static nsresult NSAPI nsChannel_SetOwner(nsIHttpChannel *iface, nsISupports *aOwner)
430 {
431     nsChannel *This = NSCHANNEL_THIS(iface);
432
433     TRACE("(%p)->(%p)\n", This, aOwner);
434
435     if(This->channel)
436         return nsIChannel_SetOwner(This->channel, aOwner);
437
438     if(aOwner)
439         nsISupports_AddRef(aOwner);
440     if(This->owner)
441         nsISupports_Release(This->owner);
442     This->owner = aOwner;
443
444     return NS_OK;
445 }
446
447 static nsresult NSAPI nsChannel_GetNotificationCallbacks(nsIHttpChannel *iface,
448         nsIInterfaceRequestor **aNotificationCallbacks)
449 {
450     nsChannel *This = NSCHANNEL_THIS(iface);
451
452     TRACE("(%p)->(%p)\n", This, aNotificationCallbacks);
453
454     if(This->notif_callback)
455         nsIInterfaceRequestor_AddRef(This->notif_callback);
456     *aNotificationCallbacks = This->notif_callback;
457
458     return NS_OK;
459 }
460
461 static nsresult NSAPI nsChannel_SetNotificationCallbacks(nsIHttpChannel *iface,
462         nsIInterfaceRequestor *aNotificationCallbacks)
463 {
464     nsChannel *This = NSCHANNEL_THIS(iface);
465
466     TRACE("(%p)->(%p)\n", This, aNotificationCallbacks);
467
468     if(This->notif_callback)
469         nsIInterfaceRequestor_Release(This->notif_callback);
470     if(aNotificationCallbacks)
471         nsIInterfaceRequestor_AddRef(aNotificationCallbacks);
472
473     This->notif_callback = aNotificationCallbacks;
474
475     if(This->channel)
476         return nsIChannel_SetNotificationCallbacks(This->channel, aNotificationCallbacks);
477     return NS_OK;
478 }
479
480 static nsresult NSAPI nsChannel_GetSecurityInfo(nsIHttpChannel *iface, nsISupports **aSecurityInfo)
481 {
482     nsChannel *This = NSCHANNEL_THIS(iface);
483
484     TRACE("(%p)->(%p)\n", This, aSecurityInfo);
485
486     if(This->channel)
487         return nsIChannel_GetSecurityInfo(This->channel, aSecurityInfo);
488
489     FIXME("default action not implemented\n");
490     return NS_ERROR_NOT_IMPLEMENTED;
491 }
492
493 static nsresult NSAPI nsChannel_GetContentType(nsIHttpChannel *iface, nsACString *aContentType)
494 {
495     nsChannel *This = NSCHANNEL_THIS(iface);
496
497     TRACE("(%p)->(%p)\n", This, aContentType);
498
499     if(This->content_type) {
500         nsACString_SetData(aContentType, This->content_type);
501         return S_OK;
502     }
503
504     if(This->channel)
505         return nsIChannel_GetContentType(This->channel, aContentType);
506
507     WARN("unknown type\n");
508     return NS_ERROR_FAILURE;
509 }
510
511 static nsresult NSAPI nsChannel_SetContentType(nsIHttpChannel *iface,
512                                                const nsACString *aContentType)
513 {
514     nsChannel *This = NSCHANNEL_THIS(iface);
515     const char *content_type;
516
517     TRACE("(%p)->(%p)\n", This, aContentType);
518
519     nsACString_GetData(aContentType, &content_type);
520
521     TRACE("content_type %s\n", content_type);
522
523     heap_free(This->content_type);
524     This->content_type = heap_strdupA(content_type);
525
526     if(This->channel)
527         return nsIChannel_SetContentType(This->channel, aContentType);
528
529     return NS_OK;
530 }
531
532 static nsresult NSAPI nsChannel_GetContentCharset(nsIHttpChannel *iface,
533                                                   nsACString *aContentCharset)
534 {
535     nsChannel *This = NSCHANNEL_THIS(iface);
536
537     TRACE("(%p)->(%p)\n", This, aContentCharset);
538
539     if(This->charset) {
540         nsACString_SetData(aContentCharset, This->charset);
541         return NS_OK;
542     }
543
544     if(This->channel) {
545         nsresult nsres = nsIChannel_GetContentCharset(This->channel, aContentCharset);
546         const char *ch;
547         nsACString_GetData(aContentCharset, &ch);
548         return nsres;
549     }
550
551     nsACString_SetData(aContentCharset, "");
552     return NS_OK;
553 }
554
555 static nsresult NSAPI nsChannel_SetContentCharset(nsIHttpChannel *iface,
556                                                   const nsACString *aContentCharset)
557 {
558     nsChannel *This = NSCHANNEL_THIS(iface);
559
560     TRACE("(%p)->(%p)\n", This, aContentCharset);
561
562     if(This->channel)
563         return nsIChannel_SetContentCharset(This->channel, aContentCharset);
564
565     FIXME("default action not implemented\n");
566     return NS_ERROR_NOT_IMPLEMENTED;
567 }
568
569 static nsresult NSAPI nsChannel_GetContentLength(nsIHttpChannel *iface, PRInt32 *aContentLength)
570 {
571     nsChannel *This = NSCHANNEL_THIS(iface);
572
573     TRACE("(%p)->(%p)\n", This, aContentLength);
574
575     if(This->channel)
576         return nsIChannel_GetContentLength(This->channel, aContentLength);
577
578     FIXME("default action not implemented\n");
579     return NS_ERROR_NOT_IMPLEMENTED;
580 }
581
582 static nsresult NSAPI nsChannel_SetContentLength(nsIHttpChannel *iface, PRInt32 aContentLength)
583 {
584     nsChannel *This = NSCHANNEL_THIS(iface);
585
586     TRACE("(%p)->(%d)\n", This, aContentLength);
587
588     if(This->channel)
589         return nsIChannel_SetContentLength(This->channel, aContentLength);
590
591     FIXME("default action not implemented\n");
592     return NS_ERROR_NOT_IMPLEMENTED;
593 }
594
595 static nsresult NSAPI nsChannel_Open(nsIHttpChannel *iface, nsIInputStream **_retval)
596 {
597     nsChannel *This = NSCHANNEL_THIS(iface);
598
599     TRACE("(%p)->(%p)\n", This, _retval);
600
601     if(This->channel)
602         return nsIChannel_Open(This->channel, _retval);
603
604     FIXME("default action not implemented\n");
605     return NS_ERROR_NOT_IMPLEMENTED;
606 }
607
608 static HRESULT create_mon_for_nschannel(nsChannel *channel, IMoniker **mon)
609 {
610     nsIWineURI *wine_uri;
611     LPCWSTR wine_url;
612     nsresult nsres;
613     HRESULT hres;
614
615     if(!channel->original_uri) {
616         ERR("original_uri == NULL\n");
617         return E_FAIL;
618     }
619
620     nsres = nsIURI_QueryInterface(channel->original_uri, &IID_nsIWineURI, (void**)&wine_uri);
621     if(NS_FAILED(nsres)) {
622         ERR("Could not get nsIWineURI: %08x\n", nsres);
623         return E_FAIL;
624     }
625
626     nsIWineURI_GetWineURL(wine_uri, &wine_url);
627     nsIWineURI_Release(wine_uri);
628     if(!wine_url) {
629         TRACE("wine_url == NULL\n");
630         return E_FAIL;
631     }
632
633     hres = CreateURLMoniker(NULL, wine_url, mon);
634     if(FAILED(hres))
635         WARN("CreateURLMoniker failed: %08x\n", hres);
636
637     return hres;
638 }
639
640 static HTMLWindow *get_window_from_load_group(nsChannel *This)
641 {
642     HTMLWindow *window;
643     nsIChannel *channel;
644     nsIRequest *req;
645     nsIWineURI *wine_uri;
646     nsIURI *uri;
647     nsresult nsres;
648
649     nsres = nsILoadGroup_GetDefaultLoadRequest(This->load_group, &req);
650     if(NS_FAILED(nsres)) {
651         ERR("GetDefaultLoadRequest failed: %08x\n", nsres);
652         return NULL;
653     }
654
655     if(!req)
656         return NULL;
657
658     nsres = nsIRequest_QueryInterface(req, &IID_nsIChannel, (void**)&channel);
659     nsIRequest_Release(req);
660     if(NS_FAILED(nsres)) {
661         WARN("Could not get nsIChannel interface: %08x\n", nsres);
662         return NULL;
663     }
664
665     nsres = nsIChannel_GetURI(channel, &uri);
666     nsIChannel_Release(channel);
667     if(NS_FAILED(nsres)) {
668         ERR("GetURI failed: %08x\n", nsres);
669         return NULL;
670     }
671
672     nsres = nsIURI_QueryInterface(uri, &IID_nsIWineURI, (void**)&wine_uri);
673     nsIURI_Release(uri);
674     if(NS_FAILED(nsres)) {
675         TRACE("Could not get nsIWineURI: %08x\n", nsres);
676         return NULL;
677     }
678
679     nsIWineURI_GetWindow(wine_uri, &window);
680     nsIWineURI_Release(wine_uri);
681
682     return window;
683 }
684
685 static HTMLWindow *get_channel_window(nsChannel *This)
686 {
687     nsIRequestObserver *req_observer;
688     nsIWebProgress *web_progress;
689     nsIDOMWindow *nswindow;
690     HTMLWindow *window;
691     nsresult nsres;
692
693     if(!This->load_group) {
694         ERR("NULL load_group\n");
695         return NULL;
696     }
697
698     nsres = nsILoadGroup_GetGroupObserver(This->load_group, &req_observer);
699     if(NS_FAILED(nsres) || !req_observer) {
700         ERR("GetGroupObserver failed: %08x\n", nsres);
701         return NULL;
702     }
703
704     nsres = nsIRequestObserver_QueryInterface(req_observer, &IID_nsIWebProgress, (void**)&web_progress);
705     nsIRequestObserver_Release(req_observer);
706     if(NS_FAILED(nsres)) {
707         ERR("Could not get nsIWebProgress iface: %08x\n", nsres);
708         return NULL;
709     }
710
711     nsres = nsIWebProgress_GetDOMWindow(web_progress, &nswindow);
712     nsIWebProgress_Release(web_progress);
713     if(NS_FAILED(nsres) || !nswindow) {
714         ERR("GetDOMWindow failed: %08x\n", nsres);
715         return NULL;
716     }
717
718     window = nswindow_to_window(nswindow);
719     nsIDOMWindow_Release(nswindow);
720
721     if(window)
722         IHTMLWindow2_AddRef(HTMLWINDOW2(window));
723     else
724         FIXME("NULL window for %p\n", nswindow);
725     return window;
726 }
727
728 typedef struct {
729     task_t header;
730     HTMLDocumentNode *doc;
731     nsChannelBSC *bscallback;
732 } start_binding_task_t;
733
734 static void start_binding_proc(task_t *_task)
735 {
736     start_binding_task_t *task = (start_binding_task_t*)_task;
737
738     start_binding(NULL, task->doc, (BSCallback*)task->bscallback, NULL);
739 }
740
741 typedef struct {
742     task_t header;
743     HTMLWindow *window;
744     nsChannelBSC *bscallback;
745 } start_doc_binding_task_t;
746
747 static void start_doc_binding_proc(task_t *_task)
748 {
749     start_doc_binding_task_t *task = (start_doc_binding_task_t*)_task;
750
751     start_binding(task->window, NULL, (BSCallback*)task->bscallback, NULL);
752     IUnknown_Release((IUnknown*)task->bscallback);
753 }
754
755 static nsresult async_open(nsChannel *This, HTMLWindow *window, BOOL is_doc_channel, nsIStreamListener *listener,
756         nsISupports *context)
757 {
758     nsChannelBSC *bscallback;
759     IMoniker *mon = NULL;
760     HRESULT hres;
761
762     hres = create_mon_for_nschannel(This, &mon);
763     if(FAILED(hres))
764         return NS_ERROR_UNEXPECTED;
765
766     if(is_doc_channel)
767         set_current_mon(window, mon);
768
769     bscallback = create_channelbsc(mon);
770     IMoniker_Release(mon);
771
772     channelbsc_set_channel(bscallback, This, listener, context);
773
774     if(is_doc_channel) {
775         start_doc_binding_task_t *task;
776
777         set_window_bscallback(window, bscallback);
778
779         task = heap_alloc(sizeof(start_doc_binding_task_t));
780         task->window = window;
781         task->bscallback = bscallback;
782         push_task(&task->header, start_doc_binding_proc, window->task_magic);
783     }else {
784         start_binding_task_t *task = heap_alloc(sizeof(start_binding_task_t));
785
786         task->doc = window->doc;
787         task->bscallback = bscallback;
788         push_task(&task->header, start_binding_proc, window->doc->basedoc.task_magic);
789     }
790
791     return NS_OK;
792 }
793
794 static nsresult NSAPI nsChannel_AsyncOpen(nsIHttpChannel *iface, nsIStreamListener *aListener,
795                                           nsISupports *aContext)
796 {
797     nsChannel *This = NSCHANNEL_THIS(iface);
798     HTMLWindow *window = NULL;
799     PRBool is_doc_uri;
800     BOOL open = TRUE;
801     nsresult nsres = NS_OK;
802
803     TRACE("(%p)->(%p %p)\n", This, aListener, aContext);
804
805     if(TRACE_ON(mshtml)) {
806         LPCWSTR url;
807
808         nsIWineURI_GetWineURL(This->uri, &url);
809         TRACE("opening %s\n", debugstr_w(url));
810     }
811
812     nsIWineURI_GetIsDocumentURI(This->uri, &is_doc_uri);
813     if(is_doc_uri) {
814         window = get_channel_window(This);
815         if(window) {
816             nsIWineURI_SetWindow(This->uri, window);
817         }else {
818             NSContainer *nscontainer;
819
820             nsIWineURI_GetNSContainer(This->uri, &nscontainer);
821             if(nscontainer) {
822                 BOOL b;
823
824                 /* nscontainer->doc should be NULL which means navigation to a new window */
825                 if(nscontainer->doc)
826                     FIXME("nscontainer->doc = %p\n", nscontainer->doc);
827
828                 b = before_async_open(This, nscontainer);
829                 nsIWebBrowserChrome_Release(NSWBCHROME(nscontainer));
830                 if(b)
831                     FIXME("Navigation not cancelled\n");
832                 return NS_ERROR_UNEXPECTED;
833             }
834         }
835     }
836
837     if(!window) {
838         nsIWineURI_GetWindow(This->uri, &window);
839
840         if(!window && This->load_group) {
841             window = get_window_from_load_group(This);
842             if(window)
843                 nsIWineURI_SetWindow(This->uri, window);
844         }
845     }
846
847     if(!window) {
848         ERR("window = NULL\n");
849         return This->channel
850             ? nsIChannel_AsyncOpen(This->channel, aListener, aContext)
851             : NS_ERROR_UNEXPECTED;
852     }
853
854     if(is_doc_uri && (This->load_flags & LOAD_INITIAL_DOCUMENT_URI) && window == window->doc_obj->basedoc.window) {
855         if(window->doc_obj->nscontainer->bscallback) {
856             NSContainer *nscontainer = window->doc_obj->nscontainer;
857
858             channelbsc_set_channel(nscontainer->bscallback, This, aListener, aContext);
859
860             if(nscontainer->doc->mime) {
861                 heap_free(This->content_type);
862                 This->content_type = heap_strdupWtoA(nscontainer->doc->mime);
863             }
864
865             open = FALSE;
866         }else {
867             open = before_async_open(This, window->doc_obj->nscontainer);
868             if(!open) {
869                 TRACE("canceled\n");
870                 nsres = NS_ERROR_UNEXPECTED;
871             }
872         }
873     }
874
875     if(open)
876         nsres = async_open(This, window, is_doc_uri, aListener, aContext);
877
878     if(window)
879         IHTMLWindow2_Release(HTMLWINDOW2(window));
880     return nsres;
881 }
882
883 static nsresult NSAPI nsChannel_GetRequestMethod(nsIHttpChannel *iface, nsACString *aRequestMethod)
884 {
885     nsChannel *This = NSCHANNEL_THIS(iface);
886
887     FIXME("(%p)->(%p)\n", This, aRequestMethod);
888
889     return NS_ERROR_NOT_IMPLEMENTED;
890 }
891
892 static nsresult NSAPI nsChannel_SetRequestMethod(nsIHttpChannel *iface,
893                                                  const nsACString *aRequestMethod)
894 {
895     nsChannel *This = NSCHANNEL_THIS(iface);
896
897     FIXME("(%p)->(%p)\n", This, aRequestMethod);
898
899     return NS_ERROR_NOT_IMPLEMENTED;
900 }
901
902 static nsresult NSAPI nsChannel_GetReferrer(nsIHttpChannel *iface, nsIURI **aReferrer)
903 {
904     nsChannel *This = NSCHANNEL_THIS(iface);
905
906     FIXME("(%p)->(%p)\n", This, aReferrer);
907
908     return NS_ERROR_NOT_IMPLEMENTED;
909 }
910
911 static nsresult NSAPI nsChannel_SetReferrer(nsIHttpChannel *iface, nsIURI *aReferrer)
912 {
913     nsChannel *This = NSCHANNEL_THIS(iface);
914
915     FIXME("(%p)->(%p)\n", This, aReferrer);
916
917     return NS_ERROR_NOT_IMPLEMENTED;
918 }
919
920 static nsresult NSAPI nsChannel_GetRequestHeader(nsIHttpChannel *iface,
921          const nsACString *aHeader, nsACString *_retval)
922 {
923     nsChannel *This = NSCHANNEL_THIS(iface);
924
925     FIXME("(%p)->(%p %p)\n", This, aHeader, _retval);
926
927     return NS_ERROR_NOT_IMPLEMENTED;
928 }
929
930 static nsresult NSAPI nsChannel_SetRequestHeader(nsIHttpChannel *iface,
931          const nsACString *aHeader, const nsACString *aValue, PRBool aMerge)
932 {
933     nsChannel *This = NSCHANNEL_THIS(iface);
934
935     FIXME("(%p)->(%p %p %x)\n", This, aHeader, aValue, aMerge);
936
937     return NS_ERROR_NOT_IMPLEMENTED;
938 }
939
940 static nsresult NSAPI nsChannel_VisitRequestHeaders(nsIHttpChannel *iface,
941                                                     nsIHttpHeaderVisitor *aVisitor)
942 {
943     nsChannel *This = NSCHANNEL_THIS(iface);
944
945     FIXME("(%p)->(%p)\n", This, aVisitor);
946
947     return NS_ERROR_NOT_IMPLEMENTED;
948 }
949
950 static nsresult NSAPI nsChannel_GetAllowPipelining(nsIHttpChannel *iface, PRBool *aAllowPipelining)
951 {
952     nsChannel *This = NSCHANNEL_THIS(iface);
953
954     FIXME("(%p)->(%p)\n", This, aAllowPipelining);
955
956     return NS_ERROR_NOT_IMPLEMENTED;
957 }
958
959 static nsresult NSAPI nsChannel_SetAllowPipelining(nsIHttpChannel *iface, PRBool aAllowPipelining)
960 {
961     nsChannel *This = NSCHANNEL_THIS(iface);
962
963     FIXME("(%p)->(%x)\n", This, aAllowPipelining);
964
965     return NS_ERROR_NOT_IMPLEMENTED;
966 }
967
968 static nsresult NSAPI nsChannel_GetRedirectionLimit(nsIHttpChannel *iface, PRUint32 *aRedirectionLimit)
969 {
970     nsChannel *This = NSCHANNEL_THIS(iface);
971
972     FIXME("(%p)->(%p)\n", This, aRedirectionLimit);
973
974     return NS_ERROR_NOT_IMPLEMENTED;
975 }
976
977 static nsresult NSAPI nsChannel_SetRedirectionLimit(nsIHttpChannel *iface, PRUint32 aRedirectionLimit)
978 {
979     nsChannel *This = NSCHANNEL_THIS(iface);
980
981     FIXME("(%p)->(%u)\n", This, aRedirectionLimit);
982
983     return NS_ERROR_NOT_IMPLEMENTED;
984 }
985
986 static nsresult NSAPI nsChannel_GetResponseStatus(nsIHttpChannel *iface, PRUint32 *aResponseStatus)
987 {
988     nsChannel *This = NSCHANNEL_THIS(iface);
989
990     TRACE("(%p)->(%p)\n", This, aResponseStatus);
991
992     if(This->response_status) {
993         *aResponseStatus = This->response_status;
994         return NS_OK;
995     }
996
997     WARN("No response status\n");
998     return NS_ERROR_UNEXPECTED;
999 }
1000
1001 static nsresult NSAPI nsChannel_GetResponseStatusText(nsIHttpChannel *iface,
1002                                                       nsACString *aResponseStatusText)
1003 {
1004     nsChannel *This = NSCHANNEL_THIS(iface);
1005
1006     FIXME("(%p)->(%p)\n", This, aResponseStatusText);
1007
1008     return NS_ERROR_NOT_IMPLEMENTED;
1009 }
1010
1011 static nsresult NSAPI nsChannel_GetRequestSucceeded(nsIHttpChannel *iface,
1012                                                     PRBool *aRequestSucceeded)
1013 {
1014     nsChannel *This = NSCHANNEL_THIS(iface);
1015
1016     FIXME("(%p)->(%p)\n", This, aRequestSucceeded);
1017
1018     return NS_ERROR_NOT_IMPLEMENTED;
1019 }
1020
1021 static nsresult NSAPI nsChannel_GetResponseHeader(nsIHttpChannel *iface,
1022          const nsACString *header, nsACString *_retval)
1023 {
1024     nsChannel *This = NSCHANNEL_THIS(iface);
1025
1026     FIXME("(%p)->(%p %p)\n", This, header, _retval);
1027
1028     return NS_ERROR_NOT_IMPLEMENTED;
1029 }
1030
1031 static nsresult NSAPI nsChannel_SetResponseHeader(nsIHttpChannel *iface,
1032         const nsACString *header, const nsACString *value, PRBool merge)
1033 {
1034     nsChannel *This = NSCHANNEL_THIS(iface);
1035
1036     FIXME("(%p)->(%p %p %x)\n", This, header, value, merge);
1037
1038     return NS_ERROR_NOT_IMPLEMENTED;
1039 }
1040
1041 static nsresult NSAPI nsChannel_VisitResponseHeaders(nsIHttpChannel *iface,
1042         nsIHttpHeaderVisitor *aVisitor)
1043 {
1044     nsChannel *This = NSCHANNEL_THIS(iface);
1045
1046     FIXME("(%p)->(%p)\n", This, aVisitor);
1047
1048     return NS_ERROR_NOT_IMPLEMENTED;
1049 }
1050
1051 static nsresult NSAPI nsChannel_IsNoStoreResponse(nsIHttpChannel *iface, PRBool *_retval)
1052 {
1053     nsChannel *This = NSCHANNEL_THIS(iface);
1054
1055     FIXME("(%p)->(%p)\n", This, _retval);
1056
1057     return NS_ERROR_NOT_IMPLEMENTED;
1058 }
1059
1060 static nsresult NSAPI nsChannel_IsNoCacheResponse(nsIHttpChannel *iface, PRBool *_retval)
1061 {
1062     nsChannel *This = NSCHANNEL_THIS(iface);
1063
1064     FIXME("(%p)->(%p)\n", This, _retval);
1065
1066     return NS_ERROR_NOT_IMPLEMENTED;
1067 }
1068
1069 #undef NSCHANNEL_THIS
1070
1071 static const nsIHttpChannelVtbl nsChannelVtbl = {
1072     nsChannel_QueryInterface,
1073     nsChannel_AddRef,
1074     nsChannel_Release,
1075     nsChannel_GetName,
1076     nsChannel_IsPending,
1077     nsChannel_GetStatus,
1078     nsChannel_Cancel,
1079     nsChannel_Suspend,
1080     nsChannel_Resume,
1081     nsChannel_GetLoadGroup,
1082     nsChannel_SetLoadGroup,
1083     nsChannel_GetLoadFlags,
1084     nsChannel_SetLoadFlags,
1085     nsChannel_GetOriginalURI,
1086     nsChannel_SetOriginalURI,
1087     nsChannel_GetURI,
1088     nsChannel_GetOwner,
1089     nsChannel_SetOwner,
1090     nsChannel_GetNotificationCallbacks,
1091     nsChannel_SetNotificationCallbacks,
1092     nsChannel_GetSecurityInfo,
1093     nsChannel_GetContentType,
1094     nsChannel_SetContentType,
1095     nsChannel_GetContentCharset,
1096     nsChannel_SetContentCharset,
1097     nsChannel_GetContentLength,
1098     nsChannel_SetContentLength,
1099     nsChannel_Open,
1100     nsChannel_AsyncOpen,
1101     nsChannel_GetRequestMethod,
1102     nsChannel_SetRequestMethod,
1103     nsChannel_GetReferrer,
1104     nsChannel_SetReferrer,
1105     nsChannel_GetRequestHeader,
1106     nsChannel_SetRequestHeader,
1107     nsChannel_VisitRequestHeaders,
1108     nsChannel_GetAllowPipelining,
1109     nsChannel_SetAllowPipelining,
1110     nsChannel_GetRedirectionLimit,
1111     nsChannel_SetRedirectionLimit,
1112     nsChannel_GetResponseStatus,
1113     nsChannel_GetResponseStatusText,
1114     nsChannel_GetRequestSucceeded,
1115     nsChannel_GetResponseHeader,
1116     nsChannel_SetResponseHeader,
1117     nsChannel_VisitResponseHeaders,
1118     nsChannel_IsNoStoreResponse,
1119     nsChannel_IsNoCacheResponse
1120 };
1121
1122 #define NSUPCHANNEL_THIS(iface) DEFINE_THIS(nsChannel, UploadChannel, iface)
1123
1124 static nsresult NSAPI nsUploadChannel_QueryInterface(nsIUploadChannel *iface, nsIIDRef riid,
1125                                                      nsQIResult result)
1126 {
1127     nsChannel *This = NSUPCHANNEL_THIS(iface);
1128     return nsIChannel_QueryInterface(NSCHANNEL(This), riid, result);
1129 }
1130
1131 static nsrefcnt NSAPI nsUploadChannel_AddRef(nsIUploadChannel *iface)
1132 {
1133     nsChannel *This = NSUPCHANNEL_THIS(iface);
1134     return nsIChannel_AddRef(NSCHANNEL(This));
1135 }
1136
1137 static nsrefcnt NSAPI nsUploadChannel_Release(nsIUploadChannel *iface)
1138 {
1139     nsChannel *This = NSUPCHANNEL_THIS(iface);
1140     return nsIChannel_Release(NSCHANNEL(This));
1141 }
1142
1143 static nsresult NSAPI nsUploadChannel_SetUploadStream(nsIUploadChannel *iface,
1144         nsIInputStream *aStream, const nsACString *aContentType, PRInt32 aContentLength)
1145 {
1146     nsChannel *This = NSUPCHANNEL_THIS(iface);
1147     const char *content_type;
1148     nsresult nsres;
1149
1150     TRACE("(%p)->(%p %p %d)\n", This, aStream, aContentType, aContentLength);
1151
1152     if(This->post_data_stream)
1153         nsIInputStream_Release(This->post_data_stream);
1154
1155     if(aContentType) {
1156         nsACString_GetData(aContentType, &content_type);
1157         if(*content_type)
1158             FIXME("Unsupported aContentType argument: %s\n", debugstr_a(content_type));
1159     }
1160
1161     if(aContentLength != -1)
1162         FIXME("Unsupported acontentLength = %d\n", aContentLength);
1163
1164     if(This->post_data_stream)
1165         nsIInputStream_Release(This->post_data_stream);
1166     This->post_data_stream = aStream;
1167     if(aStream)
1168         nsIInputStream_AddRef(aStream);
1169
1170     if(This->post_data_stream) {
1171         nsIUploadChannel *upload_channel;
1172
1173         nsres = nsIChannel_QueryInterface(This->channel, &IID_nsIUploadChannel,
1174                 (void**)&upload_channel);
1175         if(NS_SUCCEEDED(nsres)) {
1176             nsres = nsIUploadChannel_SetUploadStream(upload_channel, aStream, aContentType, aContentLength);
1177             nsIUploadChannel_Release(upload_channel);
1178             if(NS_FAILED(nsres))
1179                 WARN("SetUploadStream failed: %08x\n", nsres);
1180
1181         }
1182     }
1183
1184     return NS_OK;
1185 }
1186
1187 static nsresult NSAPI nsUploadChannel_GetUploadStream(nsIUploadChannel *iface,
1188         nsIInputStream **aUploadStream)
1189 {
1190     nsChannel *This = NSUPCHANNEL_THIS(iface);
1191
1192     TRACE("(%p)->(%p)\n", This, aUploadStream);
1193
1194     if(This->post_data_stream)
1195         nsIInputStream_AddRef(This->post_data_stream);
1196
1197     *aUploadStream = This->post_data_stream;
1198     return NS_OK;
1199 }
1200
1201 #undef NSUPCHANNEL_THIS
1202
1203 static const nsIUploadChannelVtbl nsUploadChannelVtbl = {
1204     nsUploadChannel_QueryInterface,
1205     nsUploadChannel_AddRef,
1206     nsUploadChannel_Release,
1207     nsUploadChannel_SetUploadStream,
1208     nsUploadChannel_GetUploadStream
1209 };
1210
1211 #define NSHTTPINTERNAL_THIS(iface) DEFINE_THIS(nsChannel, IHttpChannelInternal, iface)
1212
1213 static nsresult NSAPI nsHttpChannelInternal_QueryInterface(nsIHttpChannelInternal *iface, nsIIDRef riid,
1214         nsQIResult result)
1215 {
1216     nsChannel *This = NSHTTPINTERNAL_THIS(iface);
1217     return nsIChannel_QueryInterface(NSCHANNEL(This), riid, result);
1218 }
1219
1220 static nsrefcnt NSAPI nsHttpChannelInternal_AddRef(nsIHttpChannelInternal *iface)
1221 {
1222     nsChannel *This = NSHTTPINTERNAL_THIS(iface);
1223     return nsIChannel_AddRef(NSCHANNEL(This));
1224 }
1225
1226 static nsrefcnt NSAPI nsHttpChannelInternal_Release(nsIHttpChannelInternal *iface)
1227 {
1228     nsChannel *This = NSHTTPINTERNAL_THIS(iface);
1229     return nsIChannel_Release(NSCHANNEL(This));
1230 }
1231
1232 static nsresult NSAPI nsHttpChannelInternal_GetDocumentURI(nsIHttpChannelInternal *iface, nsIURI **aDocumentURI)
1233 {
1234     nsChannel *This = NSHTTPINTERNAL_THIS(iface);
1235
1236     FIXME("(%p)->()\n", This);
1237
1238     return NS_ERROR_NOT_IMPLEMENTED;
1239 }
1240
1241 static nsresult NSAPI nsHttpChannelInternal_SetDocumentURI(nsIHttpChannelInternal *iface, nsIURI *aDocumentURI)
1242 {
1243     nsChannel *This = NSHTTPINTERNAL_THIS(iface);
1244
1245     FIXME("(%p)->()\n", This);
1246
1247     return NS_ERROR_NOT_IMPLEMENTED;
1248 }
1249
1250 static nsresult NSAPI nsHttpChannelInternal_GetRequestVersion(nsIHttpChannelInternal *iface, PRUint32 *major, PRUint32 *minor)
1251 {
1252     nsChannel *This = NSHTTPINTERNAL_THIS(iface);
1253
1254     FIXME("(%p)->()\n", This);
1255
1256     return NS_ERROR_NOT_IMPLEMENTED;
1257 }
1258
1259 static nsresult NSAPI nsHttpChannelInternal_GetResponseVersion(nsIHttpChannelInternal *iface, PRUint32 *major, PRUint32 *minor)
1260 {
1261     nsChannel *This = NSHTTPINTERNAL_THIS(iface);
1262
1263     FIXME("(%p)->()\n", This);
1264
1265     return NS_ERROR_NOT_IMPLEMENTED;
1266 }
1267
1268 static nsresult NSAPI nsHttpChannelInternal_SetCookie(nsIHttpChannelInternal *iface, const char *aCookieHeader)
1269 {
1270     nsChannel *This = NSHTTPINTERNAL_THIS(iface);
1271
1272     FIXME("(%p)->()\n", This);
1273
1274     return NS_ERROR_NOT_IMPLEMENTED;
1275 }
1276
1277 static nsresult NSAPI nsHttpChannelInternal_SetupFallbackChannel(nsIHttpChannelInternal *iface, const char *aFallbackKey)
1278 {
1279     nsChannel *This = NSHTTPINTERNAL_THIS(iface);
1280
1281     FIXME("(%p)->()\n", This);
1282
1283     return NS_ERROR_NOT_IMPLEMENTED;
1284 }
1285
1286 static nsresult NSAPI nsHttpChannelInternal_GetForceAllowThirdPartyCookie(nsIHttpChannelInternal *iface, PRBool *aForceThirdPartyCookie)
1287 {
1288     nsChannel *This = NSHTTPINTERNAL_THIS(iface);
1289
1290     FIXME("(%p)->()\n", This);
1291
1292     return NS_ERROR_NOT_IMPLEMENTED;
1293 }
1294
1295 static nsresult NSAPI nsHttpChannelInternal_SetForceAllowThirdPartyCookie(nsIHttpChannelInternal *iface, PRBool aForceThirdPartyCookie)
1296 {
1297     nsChannel *This = NSHTTPINTERNAL_THIS(iface);
1298
1299     FIXME("(%p)->()\n", This);
1300
1301     return NS_ERROR_NOT_IMPLEMENTED;
1302 }
1303
1304 #undef NSHTTPINTERNAL_THIS
1305
1306 static const nsIHttpChannelInternalVtbl nsHttpChannelInternalVtbl = {
1307     nsHttpChannelInternal_QueryInterface,
1308     nsHttpChannelInternal_AddRef,
1309     nsHttpChannelInternal_Release,
1310     nsHttpChannelInternal_GetDocumentURI,
1311     nsHttpChannelInternal_SetDocumentURI,
1312     nsHttpChannelInternal_GetRequestVersion,
1313     nsHttpChannelInternal_GetResponseVersion,
1314     nsHttpChannelInternal_SetCookie,
1315     nsHttpChannelInternal_SetupFallbackChannel,
1316     nsHttpChannelInternal_GetForceAllowThirdPartyCookie,
1317     nsHttpChannelInternal_SetForceAllowThirdPartyCookie
1318 };
1319
1320 #define NSURI_THIS(iface) DEFINE_THIS(nsURI, WineURI, iface)
1321
1322 static nsresult NSAPI nsURI_QueryInterface(nsIWineURI *iface, nsIIDRef riid, nsQIResult result)
1323 {
1324     nsURI *This = NSURI_THIS(iface);
1325
1326     *result = NULL;
1327
1328     if(IsEqualGUID(&IID_nsISupports, riid)) {
1329         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
1330         *result = NSURI(This);
1331     }else if(IsEqualGUID(&IID_nsIURI, riid)) {
1332         TRACE("(%p)->(IID_nsIURI %p)\n", This, result);
1333         *result = NSURI(This);
1334     }else if(IsEqualGUID(&IID_nsIURL, riid)) {
1335         TRACE("(%p)->(IID_nsIURL %p)\n", This, result);
1336         *result = NSURI(This);
1337     }else if(IsEqualGUID(&IID_nsIWineURI, riid)) {
1338         TRACE("(%p)->(IID_nsIWineURI %p)\n", This, result);
1339         *result = NSURI(This);
1340     }
1341
1342     if(*result) {
1343         nsIURI_AddRef(NSURI(This));
1344         return NS_OK;
1345     }
1346
1347     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
1348     return This->uri ? nsIURI_QueryInterface(This->uri, riid, result) : NS_NOINTERFACE;
1349 }
1350
1351 static nsrefcnt NSAPI nsURI_AddRef(nsIWineURI *iface)
1352 {
1353     nsURI *This = NSURI_THIS(iface);
1354     LONG ref = InterlockedIncrement(&This->ref);
1355
1356     TRACE("(%p) ref=%d\n", This, ref);
1357
1358     return ref;
1359 }
1360
1361 static nsrefcnt NSAPI nsURI_Release(nsIWineURI *iface)
1362 {
1363     nsURI *This = NSURI_THIS(iface);
1364     LONG ref = InterlockedDecrement(&This->ref);
1365
1366     TRACE("(%p) ref=%d\n", This, ref);
1367
1368     if(!ref) {
1369         if(This->window_ref)
1370             windowref_release(This->window_ref);
1371         if(This->container)
1372             nsIWebBrowserChrome_Release(NSWBCHROME(This->container));
1373         if(This->nsurl)
1374             nsIURL_Release(This->nsurl);
1375         if(This->uri)
1376             nsIURI_Release(This->uri);
1377         heap_free(This->wine_url);
1378         heap_free(This);
1379     }
1380
1381     return ref;
1382 }
1383
1384 static nsresult NSAPI nsURI_GetSpec(nsIWineURI *iface, nsACString *aSpec)
1385 {
1386     nsURI *This = NSURI_THIS(iface);
1387
1388     TRACE("(%p)->(%p)\n", This, aSpec);
1389
1390     if(This->use_wine_url) {
1391         char speca[INTERNET_MAX_URL_LENGTH] = "wine:";
1392         WideCharToMultiByte(CP_ACP, 0, This->wine_url, -1, speca+5, sizeof(speca)-5, NULL, NULL);
1393         nsACString_SetData(aSpec, speca);
1394
1395         return NS_OK;
1396     }
1397
1398     if(This->uri)
1399         return nsIURI_GetSpec(This->uri, aSpec);
1400
1401     TRACE("returning error\n");
1402     return NS_ERROR_NOT_IMPLEMENTED;
1403
1404 }
1405
1406 static nsresult NSAPI nsURI_SetSpec(nsIWineURI *iface, const nsACString *aSpec)
1407 {
1408     nsURI *This = NSURI_THIS(iface);
1409
1410     TRACE("(%p)->(%p)\n", This, aSpec);
1411
1412     if(This->uri)
1413         return nsIURI_SetSpec(This->uri, aSpec);
1414
1415     FIXME("default action not implemented\n");
1416     return NS_ERROR_NOT_IMPLEMENTED;
1417 }
1418
1419 static nsresult NSAPI nsURI_GetPrePath(nsIWineURI *iface, nsACString *aPrePath)
1420 {
1421     nsURI *This = NSURI_THIS(iface);
1422
1423     TRACE("(%p)->(%p)\n", This, aPrePath);
1424
1425     if(This->uri)
1426         return nsIURI_GetPrePath(This->uri, aPrePath);
1427
1428     FIXME("default action not implemented\n");
1429     return NS_ERROR_NOT_IMPLEMENTED;
1430 }
1431
1432 static nsresult NSAPI nsURI_GetScheme(nsIWineURI *iface, nsACString *aScheme)
1433 {
1434     nsURI *This = NSURI_THIS(iface);
1435
1436     TRACE("(%p)->(%p)\n", This, aScheme);
1437
1438     if(This->use_wine_url) {
1439         /*
1440          * For Gecko we set scheme to unknown so it won't be handled
1441          * as any special case.
1442          */
1443         nsACString_SetData(aScheme, "wine");
1444         return NS_OK;
1445     }
1446
1447     if(This->uri)
1448         return nsIURI_GetScheme(This->uri, aScheme);
1449
1450     TRACE("returning error\n");
1451     return NS_ERROR_NOT_IMPLEMENTED;
1452 }
1453
1454 static nsresult NSAPI nsURI_SetScheme(nsIWineURI *iface, const nsACString *aScheme)
1455 {
1456     nsURI *This = NSURI_THIS(iface);
1457
1458     TRACE("(%p)->(%p)\n", This, aScheme);
1459
1460     if(This->uri)
1461         return nsIURI_SetScheme(This->uri, aScheme);
1462
1463     FIXME("default action not implemented\n");
1464     return NS_ERROR_NOT_IMPLEMENTED;
1465 }
1466
1467 static nsresult NSAPI nsURI_GetUserPass(nsIWineURI *iface, nsACString *aUserPass)
1468 {
1469     nsURI *This = NSURI_THIS(iface);
1470
1471     TRACE("(%p)->(%p)\n", This, aUserPass);
1472
1473     if(This->uri)
1474         return nsIURI_GetUserPass(This->uri, aUserPass);
1475
1476     FIXME("default action not implemented\n");
1477     return NS_ERROR_NOT_IMPLEMENTED;
1478 }
1479
1480 static nsresult NSAPI nsURI_SetUserPass(nsIWineURI *iface, const nsACString *aUserPass)
1481 {
1482     nsURI *This = NSURI_THIS(iface);
1483
1484     TRACE("(%p)->(%p)\n", This, aUserPass);
1485
1486     if(This->uri)
1487         return nsIURI_SetUserPass(This->uri, aUserPass);
1488
1489     FIXME("default action not implemented\n");
1490     return NS_ERROR_NOT_IMPLEMENTED;
1491 }
1492
1493 static nsresult NSAPI nsURI_GetUsername(nsIWineURI *iface, nsACString *aUsername)
1494 {
1495     nsURI *This = NSURI_THIS(iface);
1496
1497     TRACE("(%p)->(%p)\n", This, aUsername);
1498
1499     if(This->uri)
1500         return nsIURI_GetUsername(This->uri, aUsername);
1501
1502     FIXME("default action not implemented\n");
1503     return NS_ERROR_NOT_IMPLEMENTED;
1504 }
1505
1506 static nsresult NSAPI nsURI_SetUsername(nsIWineURI *iface, const nsACString *aUsername)
1507 {
1508     nsURI *This = NSURI_THIS(iface);
1509
1510     TRACE("(%p)->(%p)\n", This, aUsername);
1511
1512     if(This->uri)
1513         return nsIURI_SetUsername(This->uri, aUsername);
1514
1515     FIXME("default action not implemented\n");
1516     return NS_ERROR_NOT_IMPLEMENTED;
1517 }
1518
1519 static nsresult NSAPI nsURI_GetPassword(nsIWineURI *iface, nsACString *aPassword)
1520 {
1521     nsURI *This = NSURI_THIS(iface);
1522
1523     TRACE("(%p)->(%p)\n", This, aPassword);
1524
1525     if(This->uri)
1526         return nsIURI_GetPassword(This->uri, aPassword);
1527
1528     FIXME("default action not implemented\n");
1529     return NS_ERROR_NOT_IMPLEMENTED;
1530 }
1531
1532 static nsresult NSAPI nsURI_SetPassword(nsIWineURI *iface, const nsACString *aPassword)
1533 {
1534     nsURI *This = NSURI_THIS(iface);
1535
1536     TRACE("(%p)->(%p)\n", This, aPassword);
1537
1538     if(This->uri)
1539         return nsIURI_SetPassword(This->uri, aPassword);
1540
1541     FIXME("default action not implemented\n");
1542     return NS_ERROR_NOT_IMPLEMENTED;
1543 }
1544
1545 static nsresult NSAPI nsURI_GetHostPort(nsIWineURI *iface, nsACString *aHostPort)
1546 {
1547     nsURI *This = NSURI_THIS(iface);
1548
1549     TRACE("(%p)->(%p)\n", This, aHostPort);
1550
1551     if(This->uri)
1552         return nsIURI_GetHostPort(This->uri, aHostPort);
1553
1554     FIXME("default action not implemented\n");
1555     return NS_ERROR_NOT_IMPLEMENTED;
1556 }
1557
1558 static nsresult NSAPI nsURI_SetHostPort(nsIWineURI *iface, const nsACString *aHostPort)
1559 {
1560     nsURI *This = NSURI_THIS(iface);
1561
1562     TRACE("(%p)->(%p)\n", This, aHostPort);
1563
1564     if(This->uri)
1565         return nsIURI_SetHostPort(This->uri, aHostPort);
1566
1567     FIXME("default action not implemented\n");
1568     return NS_ERROR_NOT_IMPLEMENTED;
1569 }
1570
1571 static nsresult NSAPI nsURI_GetHost(nsIWineURI *iface, nsACString *aHost)
1572 {
1573     nsURI *This = NSURI_THIS(iface);
1574
1575     TRACE("(%p)->(%p)\n", This, aHost);
1576
1577     if(This->uri)
1578         return nsIURI_GetHost(This->uri, aHost);
1579
1580     FIXME("default action not implemented\n");
1581     return NS_ERROR_NOT_IMPLEMENTED;
1582 }
1583
1584 static nsresult NSAPI nsURI_SetHost(nsIWineURI *iface, const nsACString *aHost)
1585 {
1586     nsURI *This = NSURI_THIS(iface);
1587
1588     TRACE("(%p)->(%p)\n", This, aHost);
1589
1590     if(This->uri)
1591         return nsIURI_SetHost(This->uri, aHost);
1592
1593     FIXME("default action not implemented\n");
1594     return NS_ERROR_NOT_IMPLEMENTED;
1595 }
1596
1597 static nsresult NSAPI nsURI_GetPort(nsIWineURI *iface, PRInt32 *aPort)
1598 {
1599     nsURI *This = NSURI_THIS(iface);
1600
1601     TRACE("(%p)->(%p)\n", This, aPort);
1602
1603     if(This->uri)
1604         return nsIURI_GetPort(This->uri, aPort);
1605
1606     FIXME("default action not implemented\n");
1607     return NS_ERROR_NOT_IMPLEMENTED;
1608 }
1609
1610 static nsresult NSAPI nsURI_SetPort(nsIWineURI *iface, PRInt32 aPort)
1611 {
1612     nsURI *This = NSURI_THIS(iface);
1613
1614     TRACE("(%p)->(%d)\n", This, aPort);
1615
1616     if(This->uri)
1617         return nsIURI_SetPort(This->uri, aPort);
1618
1619     FIXME("default action not implemented\n");
1620     return NS_ERROR_NOT_IMPLEMENTED;
1621 }
1622
1623 static nsresult NSAPI nsURI_GetPath(nsIWineURI *iface, nsACString *aPath)
1624 {
1625     nsURI *This = NSURI_THIS(iface);
1626
1627     TRACE("(%p)->(%p)\n", This, aPath);
1628
1629     if(This->uri)
1630         return nsIURI_GetPath(This->uri, aPath);
1631
1632     FIXME("default action not implemented\n");
1633     return NS_ERROR_NOT_IMPLEMENTED;
1634 }
1635
1636 static nsresult NSAPI nsURI_SetPath(nsIWineURI *iface, const nsACString *aPath)
1637 {
1638     nsURI *This = NSURI_THIS(iface);
1639     const char *path;
1640
1641     nsACString_GetData(aPath, &path);
1642     TRACE("(%p)->(%p(%s))\n", This, aPath, debugstr_a(path));
1643
1644
1645     if(This->wine_url) {
1646         WCHAR new_url[INTERNET_MAX_URL_LENGTH];
1647         DWORD size = sizeof(new_url)/sizeof(WCHAR);
1648         LPWSTR pathw;
1649         HRESULT hres;
1650
1651         pathw = heap_strdupAtoW(path);
1652         hres = UrlCombineW(This->wine_url, pathw, new_url, &size, 0);
1653         heap_free(pathw);
1654         if(SUCCEEDED(hres))
1655             nsIWineURI_SetWineURL(NSWINEURI(This), new_url);
1656         else
1657             WARN("UrlCombine failed: %08x\n", hres);
1658     }
1659
1660     if(!This->uri)
1661         return NS_OK;
1662
1663     return nsIURI_SetPath(This->uri, aPath);
1664 }
1665
1666 static nsresult NSAPI nsURI_Equals(nsIWineURI *iface, nsIURI *other, PRBool *_retval)
1667 {
1668     nsURI *This = NSURI_THIS(iface);
1669     nsIWineURI *wine_uri;
1670     LPCWSTR other_url = NULL;
1671     nsresult nsres;
1672
1673     TRACE("(%p)->(%p %p)\n", This, other, _retval);
1674
1675     if(This->uri)
1676         return nsIURI_Equals(This->uri, other, _retval);
1677
1678     nsres = nsIURI_QueryInterface(other, &IID_nsIWineURI, (void**)&wine_uri);
1679     if(NS_FAILED(nsres)) {
1680         TRACE("Could not get nsIWineURI interface\n");
1681         *_retval = FALSE;
1682         return NS_OK;
1683     }
1684
1685     nsIWineURI_GetWineURL(wine_uri, &other_url);
1686     *_retval = other_url && !UrlCompareW(This->wine_url, other_url, TRUE);
1687     nsIWineURI_Release(wine_uri);
1688
1689     return NS_OK;
1690 }
1691
1692 static nsresult NSAPI nsURI_SchemeIs(nsIWineURI *iface, const char *scheme, PRBool *_retval)
1693 {
1694     nsURI *This = NSURI_THIS(iface);
1695
1696     TRACE("(%p)->(%s %p)\n", This, debugstr_a(scheme), _retval);
1697
1698     if(This->use_wine_url) {
1699         WCHAR buf[INTERNET_MAX_SCHEME_LENGTH];
1700         int len = MultiByteToWideChar(CP_ACP, 0, scheme, -1, buf, sizeof(buf)/sizeof(WCHAR))-1;
1701
1702         *_retval = lstrlenW(This->wine_url) > len
1703             && This->wine_url[len] == ':'
1704             && !memcmp(buf, This->wine_url, len*sizeof(WCHAR));
1705         return NS_OK;
1706     }
1707
1708     if(This->uri)
1709         return nsIURI_SchemeIs(This->uri, scheme, _retval);
1710
1711     TRACE("returning error\n");
1712     return NS_ERROR_NOT_IMPLEMENTED;
1713 }
1714
1715 static nsresult NSAPI nsURI_Clone(nsIWineURI *iface, nsIURI **_retval)
1716 {
1717     nsURI *This = NSURI_THIS(iface);
1718     nsIURI *nsuri = NULL;
1719     nsIWineURI *wine_uri;
1720     nsresult nsres;
1721
1722     TRACE("(%p)->(%p)\n", This, _retval);
1723
1724     if(This->uri) {
1725         nsres = nsIURI_Clone(This->uri, &nsuri);
1726         if(NS_FAILED(nsres)) {
1727             WARN("Clone failed: %08x\n", nsres);
1728             return nsres;
1729         }
1730     }
1731
1732     nsres = create_uri(nsuri, This->window_ref ? This->window_ref->window : NULL, This->container, &wine_uri);
1733     if(NS_FAILED(nsres)) {
1734         WARN("create_uri failed: %08x\n", nsres);
1735         return nsres;
1736     }
1737
1738     *_retval = (nsIURI*)wine_uri;
1739     return nsIWineURI_SetWineURL(wine_uri, This->wine_url);
1740 }
1741
1742 static nsresult NSAPI nsURI_Resolve(nsIWineURI *iface, const nsACString *arelativePath,
1743         nsACString *_retval)
1744 {
1745     nsURI *This = NSURI_THIS(iface);
1746
1747     TRACE("(%p)->(%p %p)\n", This, arelativePath, _retval);
1748
1749     if(This->uri)
1750         return nsIURI_Resolve(This->uri, arelativePath, _retval);
1751
1752     FIXME("default action not implemented\n");
1753     return NS_ERROR_NOT_IMPLEMENTED;
1754 }
1755
1756 static nsresult NSAPI nsURI_GetAsciiSpec(nsIWineURI *iface, nsACString *aAsciiSpec)
1757 {
1758     nsURI *This = NSURI_THIS(iface);
1759
1760     TRACE("(%p)->(%p)\n", This, aAsciiSpec);
1761
1762     if(This->use_wine_url)
1763         return nsIURI_GetSpec(NSURI(This), aAsciiSpec);
1764
1765     if(This->uri)
1766         return nsIURI_GetAsciiSpec(This->uri, aAsciiSpec);
1767
1768     TRACE("returning error\n");
1769     return NS_ERROR_NOT_IMPLEMENTED;
1770 }
1771
1772 static nsresult NSAPI nsURI_GetAsciiHost(nsIWineURI *iface, nsACString *aAsciiHost)
1773 {
1774     nsURI *This = NSURI_THIS(iface);
1775
1776     TRACE("(%p)->(%p)\n", This, aAsciiHost);
1777
1778     if(This->uri)
1779         return nsIURI_GetAsciiHost(This->uri, aAsciiHost);
1780
1781     FIXME("default action not implemented\n");
1782     return NS_ERROR_NOT_IMPLEMENTED;
1783 }
1784
1785 static nsresult NSAPI nsURI_GetOriginCharset(nsIWineURI *iface, nsACString *aOriginCharset)
1786 {
1787     nsURI *This = NSURI_THIS(iface);
1788
1789     TRACE("(%p)->(%p)\n", This, aOriginCharset);
1790
1791     if(This->uri)
1792         return nsIURI_GetOriginCharset(This->uri, aOriginCharset);
1793
1794     FIXME("default action not implemented\n");
1795     return NS_ERROR_NOT_IMPLEMENTED;
1796 }
1797
1798 static nsresult NSAPI nsURL_GetFilePath(nsIWineURI *iface, nsACString *aFilePath)
1799 {
1800     nsURI *This = NSURI_THIS(iface);
1801
1802     TRACE("(%p)->(%p)\n", This, aFilePath);
1803
1804     if(This->nsurl)
1805         return nsIURL_GetFilePath(This->nsurl, aFilePath);
1806
1807     FIXME("default action not implemented\n");
1808     return NS_ERROR_NOT_IMPLEMENTED;
1809 }
1810
1811 static nsresult NSAPI nsURL_SetFilePath(nsIWineURI *iface, const nsACString *aFilePath)
1812 {
1813     nsURI *This = NSURI_THIS(iface);
1814
1815     TRACE("(%p)->(%s)\n", This, debugstr_nsacstr(aFilePath));
1816
1817     if(This->nsurl)
1818         return nsIURL_SetFilePath(This->nsurl, aFilePath);
1819
1820     FIXME("default action not implemented\n");
1821     return NS_ERROR_NOT_IMPLEMENTED;
1822 }
1823
1824 static nsresult NSAPI nsURL_GetParam(nsIWineURI *iface, nsACString *aParam)
1825 {
1826     nsURI *This = NSURI_THIS(iface);
1827
1828     TRACE("(%p)->(%p)\n", This, aParam);
1829
1830     if(This->nsurl)
1831         return nsIURL_GetParam(This->nsurl, aParam);
1832
1833     FIXME("default action not implemented\n");
1834     return NS_ERROR_NOT_IMPLEMENTED;
1835 }
1836
1837 static nsresult NSAPI nsURL_SetParam(nsIWineURI *iface, const nsACString *aParam)
1838 {
1839     nsURI *This = NSURI_THIS(iface);
1840
1841     TRACE("(%p)->(%s)\n", This, debugstr_nsacstr(aParam));
1842
1843     if(This->nsurl)
1844         return nsIURL_SetParam(This->nsurl, aParam);
1845
1846     FIXME("default action not implemented\n");
1847     return NS_ERROR_NOT_IMPLEMENTED;
1848 }
1849
1850 static nsresult NSAPI nsURL_GetQuery(nsIWineURI *iface, nsACString *aQuery)
1851 {
1852     nsURI *This = NSURI_THIS(iface);
1853
1854     TRACE("(%p)->(%p)\n", This, aQuery);
1855
1856     if(This->nsurl)
1857         return nsIURL_GetQuery(This->nsurl, aQuery);
1858
1859     FIXME("default action not implemented\n");
1860     return NS_ERROR_NOT_IMPLEMENTED;
1861 }
1862
1863 static nsresult NSAPI nsURL_SetQuery(nsIWineURI *iface, const nsACString *aQuery)
1864 {
1865     nsURI *This = NSURI_THIS(iface);
1866     const WCHAR *ptr1, *ptr2;
1867     const char *query;
1868     WCHAR *new_url, *ptr;
1869     DWORD len, size;
1870
1871     TRACE("(%p)->(%s)\n", This, debugstr_nsacstr(aQuery));
1872
1873     if(This->nsurl)
1874         nsIURL_SetQuery(This->nsurl, aQuery);
1875
1876     if(!This->wine_url)
1877         return NS_OK;
1878
1879     nsACString_GetData(aQuery, &query);
1880     size = len = MultiByteToWideChar(CP_ACP, 0, query, -1, NULL, 0);
1881     ptr1 = strchrW(This->wine_url, '?');
1882     if(ptr1) {
1883         size += ptr1-This->wine_url;
1884         ptr2 = strchrW(ptr1, '#');
1885         if(ptr2)
1886             size += strlenW(ptr2);
1887     }else {
1888         ptr1 = This->wine_url + strlenW(This->wine_url);
1889         ptr2 = NULL;
1890         size += strlenW(This->wine_url);
1891     }
1892
1893     if(*query)
1894         size++;
1895
1896     new_url = heap_alloc(size*sizeof(WCHAR));
1897     memcpy(new_url, This->wine_url, (ptr1-This->wine_url)*sizeof(WCHAR));
1898     ptr = new_url + (ptr1-This->wine_url);
1899     if(*query) {
1900         *ptr++ = '?';
1901         MultiByteToWideChar(CP_ACP, 0, query, -1, ptr, len);
1902         ptr += len-1;
1903     }
1904     if(ptr2)
1905         strcpyW(ptr, ptr2);
1906     else
1907         *ptr = 0;
1908
1909     TRACE("setting %s\n", debugstr_w(new_url));
1910
1911     heap_free(This->wine_url);
1912     This->wine_url = new_url;
1913     return NS_OK;
1914 }
1915
1916 static nsresult NSAPI nsURL_GetRef(nsIWineURI *iface, nsACString *aRef)
1917 {
1918     nsURI *This = NSURI_THIS(iface);
1919
1920     TRACE("(%p)->(%p)\n", This, aRef);
1921
1922     if(This->nsurl)
1923         return nsIURL_GetRef(This->nsurl, aRef);
1924
1925     FIXME("default action not implemented\n");
1926     return NS_ERROR_NOT_IMPLEMENTED;
1927 }
1928
1929 static nsresult NSAPI nsURL_SetRef(nsIWineURI *iface, const nsACString *aRef)
1930 {
1931     nsURI *This = NSURI_THIS(iface);
1932     const char *refa;
1933
1934     TRACE("(%p)->(%s)\n", This, debugstr_nsacstr(aRef));
1935
1936     if(This->nsurl)
1937         return nsIURL_SetRef(This->nsurl, aRef);
1938
1939     nsACString_GetData(aRef, &refa);
1940     if(!*refa)
1941         return NS_OK;
1942
1943     FIXME("default action not implemented\n");
1944     return NS_ERROR_NOT_IMPLEMENTED;
1945 }
1946
1947 static nsresult NSAPI nsURL_GetDirectory(nsIWineURI *iface, nsACString *aDirectory)
1948 {
1949     nsURI *This = NSURI_THIS(iface);
1950
1951     TRACE("(%p)->(%p)\n", This, aDirectory);
1952
1953     if(This->nsurl)
1954         return nsIURL_GetDirectory(This->nsurl, aDirectory);
1955
1956     FIXME("default action not implemented\n");
1957     return NS_ERROR_NOT_IMPLEMENTED;
1958 }
1959
1960 static nsresult NSAPI nsURL_SetDirectory(nsIWineURI *iface, const nsACString *aDirectory)
1961 {
1962     nsURI *This = NSURI_THIS(iface);
1963
1964     TRACE("(%p)->(%s)\n", This, debugstr_nsacstr(aDirectory));
1965
1966     if(This->nsurl)
1967         return nsIURL_SetDirectory(This->nsurl, aDirectory);
1968
1969     FIXME("default action not implemented\n");
1970     return NS_ERROR_NOT_IMPLEMENTED;
1971 }
1972
1973 static nsresult NSAPI nsURL_GetFileName(nsIWineURI *iface, nsACString *aFileName)
1974 {
1975     nsURI *This = NSURI_THIS(iface);
1976
1977     TRACE("(%p)->(%p)\n", This, aFileName);
1978
1979     if(This->nsurl)
1980         return nsIURL_GetFileName(This->nsurl, aFileName);
1981
1982     FIXME("default action not implemented\n");
1983     return NS_ERROR_NOT_IMPLEMENTED;
1984 }
1985
1986 static nsresult NSAPI nsURL_SetFileName(nsIWineURI *iface, const nsACString *aFileName)
1987 {
1988     nsURI *This = NSURI_THIS(iface);
1989
1990     TRACE("(%p)->(%s)\n", This, debugstr_nsacstr(aFileName));
1991
1992     if(This->nsurl)
1993         return nsIURL_SetFileName(This->nsurl, aFileName);
1994
1995     FIXME("default action not implemented\n");
1996     return NS_ERROR_NOT_IMPLEMENTED;
1997 }
1998
1999 static nsresult NSAPI nsURL_GetFileBaseName(nsIWineURI *iface, nsACString *aFileBaseName)
2000 {
2001     nsURI *This = NSURI_THIS(iface);
2002
2003     TRACE("(%p)->(%p)\n", This, aFileBaseName);
2004
2005     if(This->nsurl)
2006         return nsIURL_GetFileBaseName(This->nsurl, aFileBaseName);
2007
2008     FIXME("default action not implemented\n");
2009     return NS_ERROR_NOT_IMPLEMENTED;
2010 }
2011
2012 static nsresult NSAPI nsURL_SetFileBaseName(nsIWineURI *iface, const nsACString *aFileBaseName)
2013 {
2014     nsURI *This = NSURI_THIS(iface);
2015
2016     TRACE("(%p)->(%s)\n", This, debugstr_nsacstr(aFileBaseName));
2017
2018     if(This->nsurl)
2019         return nsIURL_SetFileBaseName(This->nsurl, aFileBaseName);
2020
2021     FIXME("default action not implemented\n");
2022     return NS_ERROR_NOT_IMPLEMENTED;
2023 }
2024
2025 static nsresult NSAPI nsURL_GetFileExtension(nsIWineURI *iface, nsACString *aFileExtension)
2026 {
2027     nsURI *This = NSURI_THIS(iface);
2028
2029     TRACE("(%p)->(%p)\n", This, aFileExtension);
2030
2031     if(This->nsurl)
2032         return nsIURL_GetFileExtension(This->nsurl, aFileExtension);
2033
2034     FIXME("default action not implemented\n");
2035     return NS_ERROR_NOT_IMPLEMENTED;
2036 }
2037
2038 static nsresult NSAPI nsURL_SetFileExtension(nsIWineURI *iface, const nsACString *aFileExtension)
2039 {
2040     nsURI *This = NSURI_THIS(iface);
2041
2042     TRACE("(%p)->(%s)\n", This, debugstr_nsacstr(aFileExtension));
2043
2044     if(This->nsurl)
2045         return nsIURL_SetFileExtension(This->nsurl, aFileExtension);
2046
2047     FIXME("default action not implemented\n");
2048     return NS_ERROR_NOT_IMPLEMENTED;
2049 }
2050
2051 static nsresult NSAPI nsURL_GetCommonBaseSpec(nsIWineURI *iface, nsIURI *aURIToCompare, nsACString *_retval)
2052 {
2053     nsURI *This = NSURI_THIS(iface);
2054
2055     TRACE("(%p)->(%p %p)\n", This, aURIToCompare, _retval);
2056
2057     if(This->nsurl)
2058         return nsIURL_GetCommonBaseSpec(This->nsurl, aURIToCompare, _retval);
2059
2060     FIXME("default action not implemented\n");
2061     return NS_ERROR_NOT_IMPLEMENTED;
2062 }
2063
2064 static nsresult NSAPI nsURL_GetRelativeSpec(nsIWineURI *iface, nsIURI *aURIToCompare, nsACString *_retval)
2065 {
2066     nsURI *This = NSURI_THIS(iface);
2067
2068     TRACE("(%p)->(%p %p)\n", This, aURIToCompare, _retval);
2069
2070     if(This->nsurl)
2071         return nsIURL_GetRelativeSpec(This->nsurl, aURIToCompare, _retval);
2072
2073     FIXME("default action not implemented\n");
2074     return NS_ERROR_NOT_IMPLEMENTED;
2075 }
2076
2077 static nsresult NSAPI nsURI_GetNSContainer(nsIWineURI *iface, NSContainer **aContainer)
2078 {
2079     nsURI *This = NSURI_THIS(iface);
2080
2081     TRACE("(%p)->(%p)\n", This, aContainer);
2082
2083     if(This->container)
2084         nsIWebBrowserChrome_AddRef(NSWBCHROME(This->container));
2085     *aContainer = This->container;
2086
2087     return NS_OK;
2088 }
2089
2090 static nsresult NSAPI nsURI_SetNSContainer(nsIWineURI *iface, NSContainer *aContainer)
2091 {
2092     nsURI *This = NSURI_THIS(iface);
2093
2094     TRACE("(%p)->(%p)\n", This, aContainer);
2095
2096     if(This->container) {
2097         if(This->container == aContainer)
2098             return NS_OK;
2099         TRACE("Changing %p -> %p\n", This->container, aContainer);
2100         nsIWebBrowserChrome_Release(NSWBCHROME(This->container));
2101     }
2102
2103     if(aContainer)
2104         nsIWebBrowserChrome_AddRef(NSWBCHROME(aContainer));
2105     This->container = aContainer;
2106
2107     return NS_OK;
2108 }
2109
2110 static nsresult NSAPI nsURI_GetWindow(nsIWineURI *iface, HTMLWindow **aHTMLWindow)
2111 {
2112     nsURI *This = NSURI_THIS(iface);
2113
2114     TRACE("(%p)->(%p)\n", This, aHTMLWindow);
2115
2116     if(This->window_ref && This->window_ref->window) {
2117         IHTMLWindow2_AddRef(HTMLWINDOW2(This->window_ref->window));
2118         *aHTMLWindow = This->window_ref->window;
2119     }else {
2120         *aHTMLWindow = NULL;
2121     }
2122
2123     return NS_OK;
2124 }
2125
2126 static nsresult NSAPI nsURI_SetWindow(nsIWineURI *iface, HTMLWindow *aHTMLWindow)
2127 {
2128     nsURI *This = NSURI_THIS(iface);
2129
2130     TRACE("(%p)->(%p)\n", This, aHTMLWindow);
2131
2132     if(This->window_ref) {
2133         if(This->window_ref->window == aHTMLWindow)
2134             return NS_OK;
2135         TRACE("Changing %p -> %p\n", This->window_ref->window, aHTMLWindow);
2136         windowref_release(This->window_ref);
2137     }
2138
2139     if(aHTMLWindow) {
2140         windowref_addref(aHTMLWindow->window_ref);
2141         This->window_ref = aHTMLWindow->window_ref;
2142
2143         if(aHTMLWindow->doc_obj)
2144             nsIWineURI_SetNSContainer(NSWINEURI(This), aHTMLWindow->doc_obj->nscontainer);
2145     }else {
2146         This->window_ref = NULL;
2147     }
2148
2149     return NS_OK;
2150 }
2151
2152 static nsresult NSAPI nsURI_GetIsDocumentURI(nsIWineURI *iface, PRBool *aIsDocumentURI)
2153 {
2154     nsURI *This = NSURI_THIS(iface);
2155
2156     TRACE("(%p)->(%p)\n", This, aIsDocumentURI);
2157
2158     *aIsDocumentURI = This->is_doc_uri;
2159     return NS_OK;
2160 }
2161
2162 static nsresult NSAPI nsURI_SetIsDocumentURI(nsIWineURI *iface, PRBool aIsDocumentURI)
2163 {
2164     nsURI *This = NSURI_THIS(iface);
2165
2166     TRACE("(%p)->(%x)\n", This, aIsDocumentURI);
2167
2168     This->is_doc_uri = aIsDocumentURI;
2169     return NS_OK;
2170 }
2171
2172 static nsresult NSAPI nsURI_GetWineURL(nsIWineURI *iface, LPCWSTR *aURL)
2173 {
2174     nsURI *This = NSURI_THIS(iface);
2175
2176     TRACE("(%p)->(%p)\n", This, aURL);
2177
2178     *aURL = This->wine_url;
2179     return NS_OK;
2180 }
2181
2182 static nsresult NSAPI nsURI_SetWineURL(nsIWineURI *iface, LPCWSTR aURL)
2183 {
2184     nsURI *This = NSURI_THIS(iface);
2185
2186     static const WCHAR wszFtp[]   = {'f','t','p',':'};
2187     static const WCHAR wszHttp[]  = {'h','t','t','p',':'};
2188     static const WCHAR wszHttps[] = {'h','t','t','p','s',':'};
2189
2190     TRACE("(%p)->(%s)\n", This, debugstr_w(aURL));
2191
2192     heap_free(This->wine_url);
2193
2194     if(aURL) {
2195         int len = strlenW(aURL)+1;
2196         This->wine_url = heap_alloc(len*sizeof(WCHAR));
2197         memcpy(This->wine_url, aURL, len*sizeof(WCHAR));
2198
2199         if(This->uri) {
2200             /* FIXME: Always use wine url */
2201             This->use_wine_url =
2202                    strncmpW(aURL, wszFtp,   sizeof(wszFtp)/sizeof(WCHAR))
2203                 && strncmpW(aURL, wszHttp,  sizeof(wszHttp)/sizeof(WCHAR))
2204                 && strncmpW(aURL, wszHttps, sizeof(wszHttps)/sizeof(WCHAR));
2205         }else {
2206             This->use_wine_url = TRUE;
2207         }
2208     }else {
2209         This->wine_url = NULL;
2210         This->use_wine_url = FALSE;
2211     }
2212
2213     return NS_OK;
2214 }
2215
2216 #undef NSURI_THIS
2217
2218 static const nsIWineURIVtbl nsWineURIVtbl = {
2219     nsURI_QueryInterface,
2220     nsURI_AddRef,
2221     nsURI_Release,
2222     nsURI_GetSpec,
2223     nsURI_SetSpec,
2224     nsURI_GetPrePath,
2225     nsURI_GetScheme,
2226     nsURI_SetScheme,
2227     nsURI_GetUserPass,
2228     nsURI_SetUserPass,
2229     nsURI_GetUsername,
2230     nsURI_SetUsername,
2231     nsURI_GetPassword,
2232     nsURI_SetPassword,
2233     nsURI_GetHostPort,
2234     nsURI_SetHostPort,
2235     nsURI_GetHost,
2236     nsURI_SetHost,
2237     nsURI_GetPort,
2238     nsURI_SetPort,
2239     nsURI_GetPath,
2240     nsURI_SetPath,
2241     nsURI_Equals,
2242     nsURI_SchemeIs,
2243     nsURI_Clone,
2244     nsURI_Resolve,
2245     nsURI_GetAsciiSpec,
2246     nsURI_GetAsciiHost,
2247     nsURI_GetOriginCharset,
2248     nsURL_GetFilePath,
2249     nsURL_SetFilePath,
2250     nsURL_GetParam,
2251     nsURL_SetParam,
2252     nsURL_GetQuery,
2253     nsURL_SetQuery,
2254     nsURL_GetRef,
2255     nsURL_SetRef,
2256     nsURL_GetDirectory,
2257     nsURL_SetDirectory,
2258     nsURL_GetFileName,
2259     nsURL_SetFileName,
2260     nsURL_GetFileBaseName,
2261     nsURL_SetFileBaseName,
2262     nsURL_GetFileExtension,
2263     nsURL_SetFileExtension,
2264     nsURL_GetCommonBaseSpec,
2265     nsURL_GetRelativeSpec,
2266     nsURI_GetNSContainer,
2267     nsURI_SetNSContainer,
2268     nsURI_GetWindow,
2269     nsURI_SetWindow,
2270     nsURI_GetIsDocumentURI,
2271     nsURI_SetIsDocumentURI,
2272     nsURI_GetWineURL,
2273     nsURI_SetWineURL
2274 };
2275
2276 static nsresult create_uri(nsIURI *uri, HTMLWindow *window, NSContainer *container, nsIWineURI **_retval)
2277 {
2278     nsURI *ret = heap_alloc_zero(sizeof(nsURI));
2279
2280     ret->lpWineURIVtbl = &nsWineURIVtbl;
2281     ret->ref = 1;
2282     ret->uri = uri;
2283
2284     nsIWineURI_SetNSContainer(NSWINEURI(ret), container);
2285     nsIWineURI_SetWindow(NSWINEURI(ret), window);
2286
2287     if(uri)
2288         nsIURI_QueryInterface(uri, &IID_nsIURL, (void**)&ret->nsurl);
2289     else
2290         ret->nsurl = NULL;
2291
2292     TRACE("retval=%p\n", ret);
2293     *_retval = NSWINEURI(ret);
2294     return NS_OK;
2295 }
2296
2297 HRESULT create_doc_uri(HTMLWindow *window, WCHAR *url, nsIWineURI **ret)
2298 {
2299     nsIWineURI *uri;
2300     nsresult nsres;
2301
2302     nsres = create_uri(NULL, window, window->doc_obj->nscontainer, &uri);
2303     if(NS_FAILED(nsres))
2304         return E_FAIL;
2305
2306     nsIWineURI_SetWineURL(uri, url);
2307     nsIWineURI_SetIsDocumentURI(uri, TRUE);
2308
2309     *ret = uri;
2310     return S_OK;
2311 }
2312
2313 typedef struct {
2314     const nsIProtocolHandlerVtbl  *lpProtocolHandlerVtbl;
2315
2316     LONG ref;
2317
2318     nsIProtocolHandler *nshandler;
2319 } nsProtocolHandler;
2320
2321 #define NSPROTHANDLER(x)  ((nsIProtocolHandler*)  &(x)->lpProtocolHandlerVtbl)
2322
2323 #define NSPROTHANDLER_THIS(iface) DEFINE_THIS(nsProtocolHandler, ProtocolHandler, iface)
2324
2325 static nsresult NSAPI nsProtocolHandler_QueryInterface(nsIProtocolHandler *iface, nsIIDRef riid,
2326         nsQIResult result)
2327 {
2328     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
2329
2330     *result = NULL;
2331
2332     if(IsEqualGUID(&IID_nsISupports, riid)) {
2333         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
2334         *result = NSPROTHANDLER(This);
2335     }else if(IsEqualGUID(&IID_nsIProtocolHandler, riid)) {
2336         TRACE("(%p)->(IID_nsIProtocolHandler %p)\n", This, result);
2337         *result = NSPROTHANDLER(This);
2338     }else if(IsEqualGUID(&IID_nsIExternalProtocolHandler, riid)) {
2339         TRACE("(%p)->(IID_nsIExternalProtocolHandler %p), returning NULL\n", This, result);
2340         return NS_NOINTERFACE;
2341     }
2342
2343     if(*result) {
2344         nsISupports_AddRef((nsISupports*)*result);
2345         return NS_OK;
2346     }
2347
2348     WARN("(%s %p)\n", debugstr_guid(riid), result);
2349     return NS_NOINTERFACE;
2350 }
2351
2352 static nsrefcnt NSAPI nsProtocolHandler_AddRef(nsIProtocolHandler *iface)
2353 {
2354     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
2355     LONG ref = InterlockedIncrement(&This->ref);
2356
2357     TRACE("(%p) ref=%d\n", This, ref);
2358
2359     return ref;
2360 }
2361
2362 static nsrefcnt NSAPI nsProtocolHandler_Release(nsIProtocolHandler *iface)
2363 {
2364     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
2365     LONG ref = InterlockedDecrement(&This->ref);
2366
2367     TRACE("(%p) ref=%d\n", This, ref);
2368
2369     if(!ref) {
2370         if(This->nshandler)
2371             nsIProtocolHandler_Release(This->nshandler);
2372         heap_free(This);
2373     }
2374
2375     return ref;
2376 }
2377
2378 static nsresult NSAPI nsProtocolHandler_GetScheme(nsIProtocolHandler *iface, nsACString *aScheme)
2379 {
2380     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
2381
2382     TRACE("(%p)->(%p)\n", This, aScheme);
2383
2384     if(This->nshandler)
2385         return nsIProtocolHandler_GetScheme(This->nshandler, aScheme);
2386     return NS_ERROR_NOT_IMPLEMENTED;
2387 }
2388
2389 static nsresult NSAPI nsProtocolHandler_GetDefaultPort(nsIProtocolHandler *iface,
2390         PRInt32 *aDefaultPort)
2391 {
2392     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
2393
2394     TRACE("(%p)->(%p)\n", This, aDefaultPort);
2395
2396     if(This->nshandler)
2397         return nsIProtocolHandler_GetDefaultPort(This->nshandler, aDefaultPort);
2398     return NS_ERROR_NOT_IMPLEMENTED;
2399 }
2400
2401 static nsresult NSAPI nsProtocolHandler_GetProtocolFlags(nsIProtocolHandler *iface,
2402                                                          PRUint32 *aProtocolFlags)
2403 {
2404     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
2405
2406     TRACE("(%p)->(%p)\n", This, aProtocolFlags);
2407
2408     if(This->nshandler)
2409         return nsIProtocolHandler_GetProtocolFlags(This->nshandler, aProtocolFlags);
2410     return NS_ERROR_NOT_IMPLEMENTED;
2411 }
2412
2413 static nsresult NSAPI nsProtocolHandler_NewURI(nsIProtocolHandler *iface,
2414         const nsACString *aSpec, const char *aOriginCharset, nsIURI *aBaseURI, nsIURI **_retval)
2415 {
2416     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
2417
2418     TRACE("((%p)->%p %s %p %p)\n", This, aSpec, debugstr_a(aOriginCharset), aBaseURI, _retval);
2419
2420     if(This->nshandler)
2421         return nsIProtocolHandler_NewURI(This->nshandler, aSpec, aOriginCharset, aBaseURI, _retval);
2422     return NS_ERROR_NOT_IMPLEMENTED;
2423 }
2424
2425 static nsresult NSAPI nsProtocolHandler_NewChannel(nsIProtocolHandler *iface,
2426         nsIURI *aURI, nsIChannel **_retval)
2427 {
2428     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
2429
2430     TRACE("(%p)->(%p %p)\n", This, aURI, _retval);
2431
2432     if(This->nshandler)
2433         return nsIProtocolHandler_NewChannel(This->nshandler, aURI, _retval);
2434     return NS_ERROR_NOT_IMPLEMENTED;
2435 }
2436
2437 static nsresult NSAPI nsProtocolHandler_AllowPort(nsIProtocolHandler *iface,
2438         PRInt32 port, const char *scheme, PRBool *_retval)
2439 {
2440     nsProtocolHandler *This = NSPROTHANDLER_THIS(iface);
2441
2442     TRACE("(%p)->(%d %s %p)\n", This, port, debugstr_a(scheme), _retval);
2443
2444     if(This->nshandler)
2445         return nsIProtocolHandler_AllowPort(This->nshandler, port, scheme, _retval);
2446     return NS_ERROR_NOT_IMPLEMENTED;
2447 }
2448
2449 #undef NSPROTHANDLER_THIS
2450
2451 static const nsIProtocolHandlerVtbl nsProtocolHandlerVtbl = {
2452     nsProtocolHandler_QueryInterface,
2453     nsProtocolHandler_AddRef,
2454     nsProtocolHandler_Release,
2455     nsProtocolHandler_GetScheme,
2456     nsProtocolHandler_GetDefaultPort,
2457     nsProtocolHandler_GetProtocolFlags,
2458     nsProtocolHandler_NewURI,
2459     nsProtocolHandler_NewChannel,
2460     nsProtocolHandler_AllowPort
2461 };
2462
2463 static nsIProtocolHandler *create_protocol_handler(nsIProtocolHandler *nshandler)
2464 {
2465     nsProtocolHandler *ret = heap_alloc(sizeof(nsProtocolHandler));
2466
2467     ret->lpProtocolHandlerVtbl = &nsProtocolHandlerVtbl;
2468     ret->ref = 1;
2469     ret->nshandler = nshandler;
2470
2471     return NSPROTHANDLER(ret);
2472 }
2473
2474 static nsresult NSAPI nsIOService_QueryInterface(nsIIOService*,nsIIDRef,nsQIResult);
2475
2476 static nsrefcnt NSAPI nsIOService_AddRef(nsIIOService *iface)
2477 {
2478     return 2;
2479 }
2480
2481 static nsrefcnt NSAPI nsIOService_Release(nsIIOService *iface)
2482 {
2483     return 1;
2484 }
2485
2486 static nsresult NSAPI nsIOService_GetProtocolHandler(nsIIOService *iface, const char *aScheme,
2487                                                      nsIProtocolHandler **_retval)
2488 {
2489     nsIExternalProtocolHandler *nsexthandler;
2490     nsIProtocolHandler *nshandler;
2491     nsresult nsres;
2492
2493     TRACE("(%s %p)\n", debugstr_a(aScheme), _retval);
2494
2495     nsres = nsIIOService_GetProtocolHandler(nsio, aScheme, &nshandler);
2496     if(NS_FAILED(nsres)) {
2497         WARN("GetProtocolHandler failed: %08x\n", nsres);
2498         return nsres;
2499     }
2500
2501     nsres = nsIProtocolHandler_QueryInterface(nshandler, &IID_nsIExternalProtocolHandler,
2502                                               (void**)&nsexthandler);
2503     if(NS_FAILED(nsres)) {
2504         *_retval = nshandler;
2505         return NS_OK;
2506     }
2507
2508     nsIExternalProtocolHandler_Release(nsexthandler);
2509     *_retval = create_protocol_handler(nshandler);
2510     TRACE("return %p\n", *_retval);
2511     return NS_OK;
2512 }
2513
2514 static nsresult NSAPI nsIOService_GetProtocolFlags(nsIIOService *iface, const char *aScheme,
2515                                                     PRUint32 *_retval)
2516 {
2517     TRACE("(%s %p)\n", debugstr_a(aScheme), _retval);
2518     return nsIIOService_GetProtocolFlags(nsio, aScheme, _retval);
2519 }
2520
2521 static BOOL is_gecko_special_uri(const char *spec)
2522 {
2523     static const char *special_schemes[] = {"chrome:", "jar:", "resource:", "javascript:", "wyciwyg:"};
2524     int i;
2525
2526     for(i=0; i < sizeof(special_schemes)/sizeof(*special_schemes); i++) {
2527         if(!strncasecmp(spec, special_schemes[i], strlen(special_schemes[i])))
2528             return TRUE;
2529     }
2530
2531     return FALSE;
2532 }
2533
2534 static nsresult NSAPI nsIOService_NewURI(nsIIOService *iface, const nsACString *aSpec,
2535         const char *aOriginCharset, nsIURI *aBaseURI, nsIURI **_retval)
2536 {
2537     const char *spec = NULL;
2538     HTMLWindow *window = NULL;
2539     nsIURI *uri = NULL;
2540     LPCWSTR base_wine_url = NULL;
2541     nsIWineURI *base_wine_uri = NULL, *wine_uri;
2542     BOOL is_wine_uri = FALSE;
2543     nsresult nsres;
2544
2545     nsACString_GetData(aSpec, &spec);
2546
2547     TRACE("(%p(%s) %s %p %p)\n", aSpec, debugstr_a(spec), debugstr_a(aOriginCharset),
2548           aBaseURI, _retval);
2549
2550     if(is_gecko_special_uri(spec))
2551         return nsIIOService_NewURI(nsio, aSpec, aOriginCharset, aBaseURI, _retval);
2552
2553     if(!strncmp(spec, "wine:", 5)) {
2554         spec += 5;
2555         is_wine_uri = TRUE;
2556     }
2557
2558     if(aBaseURI) {
2559         PARSEDURLA parsed_url = {sizeof(PARSEDURLA)};
2560
2561         nsres = nsIURI_QueryInterface(aBaseURI, &IID_nsIWineURI, (void**)&base_wine_uri);
2562         if(NS_SUCCEEDED(nsres)) {
2563             nsIWineURI_GetWineURL(base_wine_uri, &base_wine_url);
2564             nsIWineURI_GetWindow(base_wine_uri, &window);
2565             TRACE("base url: %s window: %p\n", debugstr_w(base_wine_url), window);
2566         }else if(FAILED(ParseURLA(spec, &parsed_url))) {
2567             TRACE("not wraping\n");
2568             return nsIIOService_NewURI(nsio, aSpec, aOriginCharset, aBaseURI, _retval);
2569         }else {
2570             WARN("Could not get base nsIWineURI: %08x\n", nsres);
2571         }
2572     }
2573
2574     nsres = nsIIOService_NewURI(nsio, aSpec, aOriginCharset, aBaseURI, &uri);
2575     if(NS_FAILED(nsres))
2576         TRACE("NewURI failed: %08x\n", nsres);
2577
2578     nsres = create_uri(uri, window, NULL, &wine_uri);
2579     *_retval = (nsIURI*)wine_uri;
2580
2581     if(window)
2582         IHTMLWindow2_Release(HTMLWINDOW2(window));
2583
2584     if(base_wine_url) {
2585         WCHAR url[INTERNET_MAX_URL_LENGTH], rel_url[INTERNET_MAX_URL_LENGTH];
2586         DWORD len;
2587         HRESULT hres;
2588
2589         MultiByteToWideChar(CP_ACP, 0, spec, -1, rel_url, sizeof(rel_url)/sizeof(WCHAR));
2590
2591         hres = CoInternetCombineUrl(base_wine_url, rel_url,
2592                                     URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO,
2593                                     url, sizeof(url)/sizeof(WCHAR), &len, 0);
2594         if(SUCCEEDED(hres))
2595             nsIWineURI_SetWineURL(wine_uri, url);
2596         else
2597              WARN("CoCombineUrl failed: %08x\n", hres);
2598     }else if(is_wine_uri) {
2599         WCHAR url[INTERNET_MAX_URL_LENGTH];
2600
2601         MultiByteToWideChar(CP_ACP, 0, spec, -1, url, sizeof(url)/sizeof(WCHAR));
2602         nsIWineURI_SetWineURL(wine_uri, url);
2603     }
2604
2605     if(base_wine_uri)
2606         nsIWineURI_Release(base_wine_uri);
2607
2608     return nsres;
2609 }
2610
2611 static nsresult NSAPI nsIOService_NewFileURI(nsIIOService *iface, nsIFile *aFile,
2612                                              nsIURI **_retval)
2613 {
2614     TRACE("(%p %p)\n", aFile, _retval);
2615     return nsIIOService_NewFileURI(nsio, aFile, _retval);
2616 }
2617
2618 static nsresult NSAPI nsIOService_NewChannelFromURI(nsIIOService *iface, nsIURI *aURI,
2619                                                      nsIChannel **_retval)
2620 {
2621     PARSEDURLW parsed_url = {sizeof(PARSEDURLW)};
2622     nsIChannel *channel = NULL;
2623     nsChannel *ret;
2624     nsIWineURI *wine_uri;
2625     const WCHAR *url;
2626     nsresult nsres;
2627
2628     TRACE("(%p %p)\n", aURI, _retval);
2629
2630     nsres = nsIURI_QueryInterface(aURI, &IID_nsIWineURI, (void**)&wine_uri);
2631     if(NS_FAILED(nsres)) {
2632         TRACE("Could not get nsIWineURI: %08x\n", nsres);
2633         return nsIIOService_NewChannelFromURI(nsio, aURI, _retval);
2634     }
2635
2636     nsIIOService_NewChannelFromURI(nsio, aURI, &channel);
2637
2638     ret = heap_alloc_zero(sizeof(nsChannel));
2639
2640     ret->lpHttpChannelVtbl = &nsChannelVtbl;
2641     ret->lpUploadChannelVtbl = &nsUploadChannelVtbl;
2642     ret->lpIHttpChannelInternalVtbl = &nsHttpChannelInternalVtbl;
2643     ret->ref = 1;
2644     ret->channel = channel;
2645     ret->uri = wine_uri;
2646
2647     nsIURI_AddRef(aURI);
2648     ret->original_uri = aURI;
2649
2650     nsIWineURI_GetWineURL(wine_uri, &url);
2651     ret->url_scheme = url && SUCCEEDED(ParseURLW(url, &parsed_url)) ? parsed_url.nScheme : URL_SCHEME_UNKNOWN;
2652
2653     *_retval = NSCHANNEL(ret);
2654     return NS_OK;
2655 }
2656
2657 static nsresult NSAPI nsIOService_NewChannel(nsIIOService *iface, const nsACString *aSpec,
2658         const char *aOriginCharset, nsIURI *aBaseURI, nsIChannel **_retval)
2659 {
2660     TRACE("(%p %s %p %p)\n", aSpec, debugstr_a(aOriginCharset), aBaseURI, _retval);
2661     return nsIIOService_NewChannel(nsio, aSpec, aOriginCharset, aBaseURI, _retval);
2662 }
2663
2664 static nsresult NSAPI nsIOService_GetOffline(nsIIOService *iface, PRBool *aOffline)
2665 {
2666     TRACE("(%p)\n", aOffline);
2667     return nsIIOService_GetOffline(nsio, aOffline);
2668 }
2669
2670 static nsresult NSAPI nsIOService_SetOffline(nsIIOService *iface, PRBool aOffline)
2671 {
2672     TRACE("(%x)\n", aOffline);
2673     return nsIIOService_SetOffline(nsio, aOffline);
2674 }
2675
2676 static nsresult NSAPI nsIOService_AllowPort(nsIIOService *iface, PRInt32 aPort,
2677                                              const char *aScheme, PRBool *_retval)
2678 {
2679     TRACE("(%d %s %p)\n", aPort, debugstr_a(aScheme), _retval);
2680     return nsIIOService_AllowPort(nsio, aPort, debugstr_a(aScheme), _retval);
2681 }
2682
2683 static nsresult NSAPI nsIOService_ExtractScheme(nsIIOService *iface, const nsACString *urlString,
2684                                                  nsACString * _retval)
2685 {
2686     TRACE("(%p %p)\n", urlString, _retval);
2687     return nsIIOService_ExtractScheme(nsio, urlString, _retval);
2688 }
2689
2690 static const nsIIOServiceVtbl nsIOServiceVtbl = {
2691     nsIOService_QueryInterface,
2692     nsIOService_AddRef,
2693     nsIOService_Release,
2694     nsIOService_GetProtocolHandler,
2695     nsIOService_GetProtocolFlags,
2696     nsIOService_NewURI,
2697     nsIOService_NewFileURI,
2698     nsIOService_NewChannelFromURI,
2699     nsIOService_NewChannel,
2700     nsIOService_GetOffline,
2701     nsIOService_SetOffline,
2702     nsIOService_AllowPort,
2703     nsIOService_ExtractScheme
2704 };
2705
2706 static nsIIOService nsIOService = { &nsIOServiceVtbl };
2707
2708 static nsresult NSAPI nsNetUtil_QueryInterface(nsINetUtil *iface, nsIIDRef riid,
2709                                                nsQIResult result)
2710 {
2711     return nsIIOService_QueryInterface(&nsIOService, riid, result);
2712 }
2713
2714 static nsrefcnt NSAPI nsNetUtil_AddRef(nsINetUtil *iface)
2715 {
2716     return 2;
2717 }
2718
2719 static nsrefcnt NSAPI nsNetUtil_Release(nsINetUtil *iface)
2720 {
2721     return 1;
2722 }
2723
2724 static nsresult NSAPI nsNetUtil_ParseContentType(nsINetUtil *iface, const nsACString *aTypeHeader,
2725         nsACString *aCharset, PRBool *aHadCharset, nsACString *aContentType)
2726 {
2727     TRACE("(%p %p %p %p)\n", aTypeHeader, aCharset, aHadCharset, aContentType);
2728
2729     return nsINetUtil_ParseContentType(net_util, aTypeHeader, aCharset, aHadCharset, aContentType);
2730 }
2731
2732 static nsresult NSAPI nsNetUtil_ProtocolHasFlags(nsINetUtil *iface, nsIURI *aURI, PRUint32 aFlags, PRBool *_retval)
2733 {
2734     TRACE("()\n");
2735
2736     return nsINetUtil_ProtocolHasFlags(net_util, aURI, aFlags, _retval);
2737 }
2738
2739 static nsresult NSAPI nsNetUtil_URIChainHasFlags(nsINetUtil *iface, nsIURI *aURI, PRUint32 aFlags, PRBool *_retval)
2740 {
2741     TRACE("(%p %08x %p)\n", aURI, aFlags, _retval);
2742
2743     if(aFlags == (1<<11)) {
2744         *_retval = FALSE;
2745         return NS_OK;
2746     }
2747
2748     return nsINetUtil_URIChainHasFlags(net_util, aURI, aFlags, _retval);
2749 }
2750
2751 static nsresult NSAPI nsNetUtil_ToImmutableURI(nsINetUtil *iface, nsIURI *aURI, nsIURI **_retval)
2752 {
2753     TRACE("(%p %p)\n", aURI, _retval);
2754
2755     return nsINetUtil_ToImmutableURI(net_util, aURI, _retval);
2756 }
2757
2758 static nsresult NSAPI nsNetUtil_EscapeString(nsINetUtil *iface, const nsACString *aString,
2759                                              PRUint32 aEscapeType, nsACString *_retval)
2760 {
2761     TRACE("(%p %x %p)\n", aString, aEscapeType, _retval);
2762
2763     return nsINetUtil_EscapeString(net_util, aString, aEscapeType, _retval);
2764 }
2765
2766 static nsresult NSAPI nsNetUtil_EscapeURL(nsINetUtil *iface, const nsACString *aStr, PRUint32 aFlags,
2767                                           nsACString *_retval)
2768 {
2769     TRACE("(%p %08x %p)\n", aStr, aFlags, _retval);
2770
2771     return nsINetUtil_EscapeURL(net_util, aStr, aFlags, _retval);
2772 }
2773
2774 static nsresult NSAPI nsNetUtil_UnescapeString(nsINetUtil *iface, const nsACString *aStr,
2775                                                PRUint32 aFlags, nsACString *_retval)
2776 {
2777     TRACE("(%p %08x %p)\n", aStr, aFlags, _retval);
2778
2779     return nsINetUtil_UnescapeString(net_util, aStr, aFlags, _retval);
2780 }
2781
2782 static nsresult NSAPI nsNetUtil_ExtractCharsetFromContentType(nsINetUtil *iface, const nsACString *aTypeHeader,
2783         nsACString *aCharset, PRInt32 *aCharsetStart, PRInt32 *aCharsetEnd, PRBool *_retval)
2784 {
2785     TRACE("(%p %p %p %p %p)\n", aTypeHeader, aCharset, aCharsetStart, aCharsetEnd, _retval);
2786
2787     return nsINetUtil_ExtractCharsetFromContentType(net_util, aTypeHeader, aCharset, aCharsetStart, aCharsetEnd, _retval);
2788 }
2789
2790 static const nsINetUtilVtbl nsNetUtilVtbl = {
2791     nsNetUtil_QueryInterface,
2792     nsNetUtil_AddRef,
2793     nsNetUtil_Release,
2794     nsNetUtil_ParseContentType,
2795     nsNetUtil_ProtocolHasFlags,
2796     nsNetUtil_URIChainHasFlags,
2797     nsNetUtil_ToImmutableURI,
2798     nsNetUtil_EscapeString,
2799     nsNetUtil_EscapeURL,
2800     nsNetUtil_UnescapeString,
2801     nsNetUtil_ExtractCharsetFromContentType
2802 };
2803
2804 static nsINetUtil nsNetUtil = { &nsNetUtilVtbl };
2805
2806 static nsresult NSAPI nsIOService_QueryInterface(nsIIOService *iface, nsIIDRef riid,
2807                                                  nsQIResult result)
2808 {
2809     *result = NULL;
2810
2811     if(IsEqualGUID(&IID_nsISupports, riid))
2812         *result = &nsIOService;
2813     else if(IsEqualGUID(&IID_nsIIOService, riid))
2814         *result = &nsIOService;
2815     else if(IsEqualGUID(&IID_nsINetUtil, riid))
2816         *result = &nsNetUtil;
2817
2818     if(*result) {
2819         nsISupports_AddRef((nsISupports*)*result);
2820         return NS_OK;
2821     }
2822
2823     FIXME("(%s %p)\n", debugstr_guid(riid), result);
2824     return NS_NOINTERFACE;
2825 }
2826
2827 static nsresult NSAPI nsIOServiceFactory_QueryInterface(nsIFactory *iface, nsIIDRef riid,
2828                                                         nsQIResult result)
2829 {
2830     *result = NULL;
2831
2832     if(IsEqualGUID(&IID_nsISupports, riid)) {
2833         TRACE("(IID_nsISupports %p)\n", result);
2834         *result = iface;
2835     }else if(IsEqualGUID(&IID_nsIFactory, riid)) {
2836         TRACE("(IID_nsIFactory %p)\n", result);
2837         *result = iface;
2838     }
2839
2840     if(*result) {
2841         nsIFactory_AddRef(iface);
2842         return NS_OK;
2843     }
2844
2845     WARN("(%s %p)\n", debugstr_guid(riid), result);
2846     return NS_NOINTERFACE;
2847 }
2848
2849 static nsrefcnt NSAPI nsIOServiceFactory_AddRef(nsIFactory *iface)
2850 {
2851     return 2;
2852 }
2853
2854 static nsrefcnt NSAPI nsIOServiceFactory_Release(nsIFactory *iface)
2855 {
2856     return 1;
2857 }
2858
2859 static nsresult NSAPI nsIOServiceFactory_CreateInstance(nsIFactory *iface,
2860         nsISupports *aOuter, const nsIID *iid, void **result)
2861 {
2862     return nsIIOService_QueryInterface(&nsIOService, iid, result);
2863 }
2864
2865 static nsresult NSAPI nsIOServiceFactory_LockFactory(nsIFactory *iface, PRBool lock)
2866 {
2867     WARN("(%x)\n", lock);
2868     return NS_OK;
2869 }
2870
2871 static const nsIFactoryVtbl nsIOServiceFactoryVtbl = {
2872     nsIOServiceFactory_QueryInterface,
2873     nsIOServiceFactory_AddRef,
2874     nsIOServiceFactory_Release,
2875     nsIOServiceFactory_CreateInstance,
2876     nsIOServiceFactory_LockFactory
2877 };
2878
2879 static nsIFactory nsIOServiceFactory = { &nsIOServiceFactoryVtbl };
2880
2881 void init_nsio(nsIComponentManager *component_manager, nsIComponentRegistrar *registrar)
2882 {
2883     nsIFactory *old_factory = NULL;
2884     nsresult nsres;
2885
2886     nsres = nsIComponentManager_GetClassObject(component_manager, &NS_IOSERVICE_CID,
2887                                                &IID_nsIFactory, (void**)&old_factory);
2888     if(NS_FAILED(nsres)) {
2889         ERR("Could not get factory: %08x\n", nsres);
2890         return;
2891     }
2892
2893     nsres = nsIFactory_CreateInstance(old_factory, NULL, &IID_nsIIOService, (void**)&nsio);
2894     if(NS_FAILED(nsres)) {
2895         ERR("Couldn not create nsIOService instance %08x\n", nsres);
2896         nsIFactory_Release(old_factory);
2897         return;
2898     }
2899
2900     nsres = nsIIOService_QueryInterface(nsio, &IID_nsINetUtil, (void**)&net_util);
2901     if(NS_FAILED(nsres)) {
2902         WARN("Could not get nsINetUtil interface: %08x\n", nsres);
2903         nsIIOService_Release(nsio);
2904         return;
2905     }
2906
2907     nsres = nsIComponentRegistrar_UnregisterFactory(registrar, &NS_IOSERVICE_CID, old_factory);
2908     nsIFactory_Release(old_factory);
2909     if(NS_FAILED(nsres))
2910         ERR("UnregisterFactory failed: %08x\n", nsres);
2911
2912     nsres = nsIComponentRegistrar_RegisterFactory(registrar, &NS_IOSERVICE_CID,
2913             NS_IOSERVICE_CLASSNAME, NS_IOSERVICE_CONTRACTID, &nsIOServiceFactory);
2914     if(NS_FAILED(nsres))
2915         ERR("RegisterFactory failed: %08x\n", nsres);
2916 }
2917
2918 void release_nsio(void)
2919 {
2920     if(net_util) {
2921         nsINetUtil_Release(net_util);
2922         net_util = NULL;
2923     }
2924
2925     if(nsio) {
2926         nsIIOService_Release(nsio);
2927         nsio = NULL;
2928     }
2929 }