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