- Use only stored result of Interlocked* in AddRef/Release.
[wine] / dlls / urlmon / umon.c
1 /*
2  * UrlMon
3  *
4  * Copyright 1999 Ulrich Czekalla for Corel Corporation
5  * Copyright 2002 Huw D M Davies for CodeWeavers
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #define COM_NO_WINDOWS_H
23 #include <stdarg.h>
24 #include <stdio.h>
25
26 #define COBJMACROS
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winreg.h"
33 #include "winternl.h"
34 #include "winuser.h"
35 #include "objbase.h"
36 #include "wine/debug.h"
37 #include "wine/unicode.h"
38 #include "ole2.h"
39 #include "urlmon.h"
40 #include "wininet.h"
41 #include "shlwapi.h"
42 #include "urlmon_main.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
45
46 /* native urlmon.dll uses this key, too */
47 static const WCHAR BSCBHolder[] = { '_','B','S','C','B','_','H','o','l','d','e','r','_',0 };
48
49 /*static BOOL registered_wndclass = FALSE;*/
50
51 /* filemoniker data structure */
52 typedef struct URLMonikerImpl{
53
54     IMonikerVtbl*  lpvtbl1;  /* VTable relative to the IMoniker interface.*/
55     IBindingVtbl*  lpvtbl2;  /* VTable to IBinding interface */
56
57     ULONG ref; /* reference counter for this object */
58
59     LPOLESTR URLName; /* URL string identified by this URLmoniker */
60
61     HWND hwndCallback;
62     IBindCtx *pBC;
63     HINTERNET hinternet, hconnect, hrequest;
64 } URLMonikerImpl;
65
66 /*******************************************************************************
67  *        URLMoniker_QueryInterface
68  *******************************************************************************/
69 static HRESULT WINAPI URLMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
70 {
71     URLMonikerImpl *This = (URLMonikerImpl *)iface;
72
73     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObject);
74
75     /* Perform a sanity check on the parameters.*/
76     if ( (This==0) || (ppvObject==0) )
77         return E_INVALIDARG;
78
79     /* Initialize the return parameter */
80     *ppvObject = 0;
81
82     /* Compare the riid with the interface IDs implemented by this object.*/
83     if (IsEqualIID(&IID_IUnknown, riid)      ||
84         IsEqualIID(&IID_IPersist, riid)      ||
85         IsEqualIID(&IID_IPersistStream,riid) ||
86         IsEqualIID(&IID_IMoniker, riid)
87        )
88         *ppvObject = iface;
89
90     /* Check that we obtained an interface.*/
91     if ((*ppvObject)==0)
92         return E_NOINTERFACE;
93
94     /* Query Interface always increases the reference count by one when it is successful */
95     IMoniker_AddRef(iface);
96
97     return S_OK;
98 }
99
100 /******************************************************************************
101  *        URLMoniker_AddRef
102  ******************************************************************************/
103 static ULONG WINAPI URLMonikerImpl_AddRef(IMoniker* iface)
104 {
105     URLMonikerImpl *This = (URLMonikerImpl *)iface;
106     ULONG refCount = InterlockedIncrement(&This->ref);
107
108     TRACE("(%p)->(ref before=%lu)\n",This, refCount - 1);
109
110     return refCount;
111 }
112
113 /******************************************************************************
114  *        URLMoniker_Release
115  ******************************************************************************/
116 static ULONG WINAPI URLMonikerImpl_Release(IMoniker* iface)
117 {
118     URLMonikerImpl *This = (URLMonikerImpl *)iface;
119     ULONG refCount = InterlockedDecrement(&This->ref);
120
121     TRACE("(%p)->(ref before=%lu)\n",This, refCount + 1);
122
123     /* destroy the object if there's no more reference on it */
124     if (!refCount) {
125         HeapFree(GetProcessHeap(),0,This->URLName);
126         HeapFree(GetProcessHeap(),0,This);
127     }
128
129     return refCount;
130 }
131
132 /******************************************************************************
133  *        URLMoniker_GetClassID
134  ******************************************************************************/
135 static HRESULT WINAPI URLMonikerImpl_GetClassID(IMoniker* iface,
136                                                 CLSID *pClassID)/* Pointer to CLSID of object */
137 {
138     URLMonikerImpl *This = (URLMonikerImpl *)iface;
139
140     TRACE("(%p,%p)\n",This,pClassID);
141
142     if (pClassID==NULL)
143         return E_POINTER;
144     /* Windows always returns CLSID_StdURLMoniker */
145     *pClassID = CLSID_StdURLMoniker;
146     return S_OK;
147 }
148
149 /******************************************************************************
150  *        URLMoniker_IsDirty
151  ******************************************************************************/
152 static HRESULT WINAPI URLMonikerImpl_IsDirty(IMoniker* iface)
153 {
154     URLMonikerImpl *This = (URLMonikerImpl *)iface;
155     /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
156        method in the OLE-provided moniker interfaces always return S_FALSE because
157        their internal state never changes. */
158
159     TRACE("(%p)\n",This);
160
161     return S_FALSE;
162 }
163
164 /******************************************************************************
165  *        URLMoniker_Load
166  *
167  * NOTE
168  *  Writes a ULONG containing length of unicode string, followed
169  *  by that many unicode characters
170  ******************************************************************************/
171 static HRESULT WINAPI URLMonikerImpl_Load(IMoniker* iface,IStream* pStm)
172 {
173     URLMonikerImpl *This = (URLMonikerImpl *)iface;
174     
175     HRESULT res;
176     ULONG len;
177     ULONG got;
178     TRACE("(%p,%p)\n",This,pStm);
179
180     if(!pStm)
181         return E_INVALIDARG;
182
183     res = IStream_Read(pStm, &len, sizeof(ULONG), &got);
184     if(SUCCEEDED(res)) {
185         if(got == sizeof(ULONG)) {
186             HeapFree(GetProcessHeap(), 0, This->URLName);
187             This->URLName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(len+1));
188             if(!This->URLName)
189                 res = E_OUTOFMEMORY;
190             else {
191                 res = IStream_Read(pStm, This->URLName, len, NULL);
192                 This->URLName[len] = 0;
193             }
194         }
195         else
196             res = E_FAIL;
197     }
198     return res;
199 }
200
201 /******************************************************************************
202  *        URLMoniker_Save
203  ******************************************************************************/
204 static HRESULT WINAPI URLMonikerImpl_Save(IMoniker* iface,
205                                           IStream* pStm,/* pointer to the stream where the object is to be saved */
206                                           BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
207 {
208     URLMonikerImpl *This = (URLMonikerImpl *)iface;
209
210     HRESULT res;
211     ULONG len;
212     TRACE("(%p,%p,%d)\n",This,pStm,fClearDirty);
213
214     if(!pStm)
215         return E_INVALIDARG;
216
217     len = strlenW(This->URLName);
218     res=IStream_Write(pStm,&len,sizeof(ULONG),NULL);
219     if(SUCCEEDED(res))
220         res=IStream_Write(pStm,&This->URLName,len*sizeof(WCHAR),NULL);
221     return res;
222
223 }
224
225 /******************************************************************************
226  *        URLMoniker_GetSizeMax
227  ******************************************************************************/
228 static HRESULT WINAPI URLMonikerImpl_GetSizeMax(IMoniker* iface,
229                                                 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
230 {
231     URLMonikerImpl *This = (URLMonikerImpl *)iface;
232
233     TRACE("(%p,%p)\n",This,pcbSize);
234
235     if(!pcbSize)
236         return E_INVALIDARG;
237
238     pcbSize->u.LowPart = sizeof(ULONG) + (strlenW(This->URLName) * sizeof(WCHAR));
239     pcbSize->u.HighPart = 0;
240     return S_OK;
241 }
242
243 /******************************************************************************
244  *                  URLMoniker_BindToObject
245  ******************************************************************************/
246 static HRESULT WINAPI URLMonikerImpl_BindToObject(IMoniker* iface,
247                                                   IBindCtx* pbc,
248                                                   IMoniker* pmkToLeft,
249                                                   REFIID riid,
250                                                   VOID** ppvResult)
251 {
252     URLMonikerImpl *This = (URLMonikerImpl *)iface;
253
254     *ppvResult=0;
255
256     FIXME("(%p)->(%p,%p,%s,%p): stub\n",This,pbc,pmkToLeft,debugstr_guid(riid),
257           ppvResult);
258
259     return E_NOTIMPL;
260 }
261
262 typedef struct {
263     enum {OnProgress, OnDataAvailable} callback;
264 } URLMON_CallbackData;
265
266
267 #if 0
268 static LRESULT CALLBACK URLMON_WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
269 {
270     return DefWindowProcA(hwnd, msg, wparam, lparam);
271 }
272
273 static void PostOnProgress(URLMonikerImpl *This, UINT progress, UINT maxprogress, DWORD status, LPCWSTR *str)
274 {
275 }
276
277 static void CALLBACK URLMON_InternetCallback(HINTERNET hinet, /*DWORD_PTR*/ DWORD context, DWORD status,
278                                              void *status_info, DWORD status_info_len)
279 {
280     URLMonikerImpl *This = (URLMonikerImpl *)context;
281     TRACE("handle %p this %p status %08lx\n", hinet, This, status);
282
283     if(This->filesize == -1) {
284         switch(status) {
285         case INTERNET_STATUS_RESOLVING_NAME:
286             PostOnProgess(This, 0, 0, BINDSTATUS_FINDINGRESOURCE, status_info);
287             break;
288         case INTERNET_STATUS_CONNECTING_TO_SERVER:
289             PostOnProgress(This, 0, 0, BINDSTATUS_CONNECTING, NULL);
290             break;
291         case INTERNET_STATUS_SENDING_REQUEST:
292             PostOnProgress(This, 0, 0, BINDSTATUS_SENDINGREQUEST, NULL);
293             break;
294         case INTERNET_REQUEST_COMPLETE:
295           {
296               DWORD len, lensz = sizeof(len);
297
298             HttpQueryInfoW(hrequest, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &len, &lensz, NULL);
299             TRACE("res = %ld gle = %08lx url len = %ld\n", hres, GetLastError(), len);
300             This->filesize = len;
301             break;
302           }
303         }
304     }
305
306     return;
307 }
308 #endif
309 /******************************************************************************
310  *        URLMoniker_BindToStorage
311  ******************************************************************************/
312 static HRESULT WINAPI URLMonikerImpl_BindToStorage(IMoniker* iface,
313                                                    IBindCtx* pbc,
314                                                    IMoniker* pmkToLeft,
315                                                    REFIID riid,
316                                                    VOID** ppvObject)
317 {
318     URLMonikerImpl *This = (URLMonikerImpl *)iface;
319     HRESULT hres;
320     IBindStatusCallback *pbscb;
321     BINDINFO bi;
322     DWORD bindf;
323     IStream *pstr;
324
325     FIXME("(%p)->(%p,%p,%s,%p): stub\n",This,pbc,pmkToLeft,debugstr_guid(riid),ppvObject);
326     if(pmkToLeft) {
327         FIXME("pmkToLeft != NULL\n");
328         return E_NOTIMPL;
329     }
330     if(!IsEqualIID(&IID_IStream, riid)) {
331         FIXME("unsupported iid\n");
332         return E_NOTIMPL;
333     }
334
335     /* FIXME This is a bad hack (tm).  We should clearly download to a temporary file.
336        We also need to implement IStream ourselves so that IStream_Read can return
337        E_PENDING */
338
339     hres = CreateStreamOnHGlobal(0, TRUE, &pstr);
340
341     if(SUCCEEDED(hres)) {
342         TRACE("Created dummy stream...\n");
343
344         hres = IBindCtx_GetObjectParam(pbc, (LPOLESTR)BSCBHolder, (IUnknown**)&pbscb);
345         if(SUCCEEDED(hres)) {
346             TRACE("Got IBindStatusCallback...\n");
347
348             memset(&bi, 0, sizeof(bi));
349             bi.cbSize = sizeof(bi);
350             bindf = 0;
351             hres = IBindStatusCallback_GetBindInfo(pbscb, &bindf, &bi);
352             if(SUCCEEDED(hres)) {
353                 URL_COMPONENTSW url;
354                 WCHAR *host, *path;
355                 DWORD len, lensz = sizeof(len), total_read = 0;
356                 LARGE_INTEGER last_read_pos;
357                 FORMATETC fmt;
358                 STGMEDIUM stg;
359
360                 TRACE("got bindinfo. bindf = %08lx extrainfo = %s bindinfof = %08lx bindverb = %08lx iid %s\n",
361                       bindf, debugstr_w(bi.szExtraInfo), bi.grfBindInfoF, bi.dwBindVerb, debugstr_guid(&bi.iid));
362                 hres = IBindStatusCallback_OnStartBinding(pbscb, 0, (IBinding*)&This->lpvtbl2);
363                 TRACE("OnStartBinding rets %08lx\n", hres);
364
365 #if 0
366                 if(!registered_wndclass) {
367                     WNDCLASSA urlmon_wndclass = {0, URLMON_WndProc,0, 0, URLMON_hInstance, 0, 0, 0, NULL, "URLMON_Callback_Window_Class"};
368                     RegisterClassA(&urlmon_wndclass);
369                     registered_wndclass = TRUE;
370                 }
371
372                 This->hwndCallback = CreateWindowA("URLMON_Callback_Window_Class", NULL, 0, 0, 0, 0, 0, 0, 0,
373                                                    URLMON_hInstance, NULL);
374
375 #endif
376                 memset(&url, 0, sizeof(url));
377                 url.dwStructSize = sizeof(url);
378                 url.dwSchemeLength = url.dwHostNameLength = url.dwUrlPathLength = 1;
379                 InternetCrackUrlW(This->URLName, 0, 0, &url);
380                 host = HeapAlloc(GetProcessHeap(), 0, (url.dwHostNameLength + 1) * sizeof(WCHAR));
381                 memcpy(host, url.lpszHostName, url.dwHostNameLength * sizeof(WCHAR));
382                 host[url.dwHostNameLength] = '\0';
383                 path = HeapAlloc(GetProcessHeap(), 0, (url.dwUrlPathLength + 1) * sizeof(WCHAR));
384                 memcpy(path, url.lpszUrlPath, url.dwUrlPathLength * sizeof(WCHAR));
385                 path[url.dwUrlPathLength] = '\0';
386
387                 This->hinternet = InternetOpenA("User Agent", 0, NULL, NULL, 0 /*INTERNET_FLAG_ASYNC*/);
388 /*              InternetSetStatusCallback(This->hinternet, URLMON_InternetCallback);*/
389
390                 This->hconnect = InternetConnectW(This->hinternet, host, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
391                                                   INTERNET_SERVICE_HTTP, 0, (DWORD)This);
392                 This->hrequest = HttpOpenRequestW(This->hconnect, NULL, path, NULL, NULL, NULL, 0, (DWORD)This);
393
394                 hres = IBindStatusCallback_OnProgress(pbscb, 0, 0, 0x22, NULL);
395                 hres = IBindStatusCallback_OnProgress(pbscb, 0, 0, BINDSTATUS_FINDINGRESOURCE, NULL);
396                 hres = IBindStatusCallback_OnProgress(pbscb, 0, 0, BINDSTATUS_CONNECTING, NULL);
397                 hres = IBindStatusCallback_OnProgress(pbscb, 0, 0, BINDSTATUS_SENDINGREQUEST, NULL);
398                 hres = E_OUTOFMEMORY; /* FIXME */
399                 if(HttpSendRequestW(This->hrequest, NULL, 0, NULL, 0)) {
400                     len = 0;
401                     HttpQueryInfoW(This->hrequest, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &len, &lensz, NULL);
402
403                     TRACE("res = %ld gle = %08lx url len = %ld\n", hres, GetLastError(), len);
404
405                     last_read_pos.u.LowPart = last_read_pos.u.HighPart = 0;
406                     fmt.cfFormat = 0;
407                     fmt.ptd = NULL;
408                     fmt.dwAspect = 0;
409                     fmt.lindex = -1;
410                     fmt.tymed = TYMED_ISTREAM;
411                     stg.tymed = TYMED_ISTREAM;
412                     stg.u.pstm = pstr;
413                     stg.pUnkForRelease = NULL;
414
415                     while(1) {
416                         char buf[4096];
417                         DWORD bufread;
418                         DWORD written;
419                         if(InternetReadFile(This->hrequest, buf, sizeof(buf), &bufread)) {
420                             TRACE("read %ld bytes %s...\n", bufread, debugstr_an(buf, 10));
421                             if(bufread == 0) break;
422                             IStream_Write(pstr, buf, bufread, &written);
423                             total_read += bufread;
424                             IStream_Seek(pstr, last_read_pos, STREAM_SEEK_SET, NULL);
425                             hres = IBindStatusCallback_OnProgress(pbscb, total_read, len, (total_read == bufread) ?
426                                                                   BINDSTATUS_BEGINDOWNLOADDATA :
427                                                                   BINDSTATUS_DOWNLOADINGDATA, NULL);
428                             hres = IBindStatusCallback_OnDataAvailable(pbscb,
429                                                                        (total_read == bufread) ? BSCF_FIRSTDATANOTIFICATION :
430                                                                        BSCF_INTERMEDIATEDATANOTIFICATION,
431                                                                        total_read, &fmt, &stg);
432                             last_read_pos.u.LowPart += bufread; /* FIXME */
433                         } else
434                             break;
435                     }
436                     hres = IBindStatusCallback_OnProgress(pbscb, total_read, len, BINDSTATUS_ENDDOWNLOADDATA, NULL);
437                     hres = IBindStatusCallback_OnDataAvailable(pbscb, BSCF_LASTDATANOTIFICATION, total_read, &fmt, &stg);
438                     TRACE("OnDataAvail rets %08lx\n", hres);
439                     hres = IBindStatusCallback_OnStopBinding(pbscb, S_OK, NULL);
440                     TRACE("OnStop rets %08lx\n", hres);
441                     hres = S_OK;
442                 }
443                 InternetCloseHandle(This->hrequest);
444                 InternetCloseHandle(This->hconnect);
445                 InternetCloseHandle(This->hinternet);
446                 IBindStatusCallback_Release(pbscb);
447             }
448         }
449     }
450     *ppvObject = (VOID*)pstr;
451     return hres;
452 }
453
454 /******************************************************************************
455  *        URLMoniker_Reduce
456  ******************************************************************************/
457 static HRESULT WINAPI URLMonikerImpl_Reduce(IMoniker* iface,
458                                             IBindCtx* pbc,
459                                             DWORD dwReduceHowFar,
460                                             IMoniker** ppmkToLeft,
461                                             IMoniker** ppmkReduced)
462 {
463     URLMonikerImpl *This = (URLMonikerImpl *)iface;
464     
465     TRACE("(%p,%p,%ld,%p,%p)\n",This,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
466
467     if(!ppmkReduced)
468         return E_INVALIDARG;
469
470     URLMonikerImpl_AddRef(iface);
471     *ppmkReduced = iface;
472     return MK_S_REDUCED_TO_SELF;
473 }
474
475 /******************************************************************************
476  *        URLMoniker_ComposeWith
477  ******************************************************************************/
478 static HRESULT WINAPI URLMonikerImpl_ComposeWith(IMoniker* iface,
479                                                  IMoniker* pmkRight,
480                                                  BOOL fOnlyIfNotGeneric,
481                                                  IMoniker** ppmkComposite)
482 {
483     URLMonikerImpl *This = (URLMonikerImpl *)iface;
484     FIXME("(%p)->(%p,%d,%p): stub\n",This,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
485
486     return E_NOTIMPL;
487 }
488
489 /******************************************************************************
490  *        URLMoniker_Enum
491  ******************************************************************************/
492 static HRESULT WINAPI URLMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
493 {
494     URLMonikerImpl *This = (URLMonikerImpl *)iface;
495     TRACE("(%p,%d,%p)\n",This,fForward,ppenumMoniker);
496
497     if(!ppenumMoniker)
498         return E_INVALIDARG;
499
500     /* Does not support sub-monikers */
501     *ppenumMoniker = NULL;
502     return S_OK;
503 }
504
505 /******************************************************************************
506  *        URLMoniker_IsEqual
507  ******************************************************************************/
508 static HRESULT WINAPI URLMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
509 {
510     URLMonikerImpl *This = (URLMonikerImpl *)iface;
511     CLSID clsid;
512     LPOLESTR urlPath;
513     IBindCtx* bind;
514     HRESULT res;
515
516     TRACE("(%p,%p)\n",This,pmkOtherMoniker);
517
518     if(pmkOtherMoniker==NULL)
519         return E_INVALIDARG;
520
521     IMoniker_GetClassID(pmkOtherMoniker,&clsid);
522
523     if(!IsEqualCLSID(&clsid,&CLSID_StdURLMoniker))
524         return S_FALSE;
525
526     res = CreateBindCtx(0,&bind);
527     if(FAILED(res))
528         return res;
529
530     res = S_FALSE;
531     if(SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&urlPath))) {
532         int result = lstrcmpiW(urlPath, This->URLName);
533         CoTaskMemFree(urlPath);
534         if(result == 0)
535             res = S_OK;
536     }
537     IUnknown_Release(bind);
538     return res;
539 }
540
541
542 /******************************************************************************
543  *        URLMoniker_Hash
544  ******************************************************************************/
545 static HRESULT WINAPI URLMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
546 {
547     URLMonikerImpl *This = (URLMonikerImpl *)iface;
548     
549     int  h = 0,i,skip,len;
550     int  off = 0;
551     LPOLESTR val;
552
553     TRACE("(%p,%p)\n",This,pdwHash);
554
555     if(!pdwHash)
556         return E_INVALIDARG;
557
558     val = This->URLName;
559     len = lstrlenW(val);
560
561     if(len < 16) {
562         for(i = len ; i > 0; i--) {
563             h = (h * 37) + val[off++];
564         }
565     }
566     else {
567         /* only sample some characters */
568         skip = len / 8;
569         for(i = len; i > 0; i -= skip, off += skip) {
570             h = (h * 39) + val[off];
571         }
572     }
573     *pdwHash = h;
574     return S_OK;
575 }
576
577 /******************************************************************************
578  *        URLMoniker_IsRunning
579  ******************************************************************************/
580 static HRESULT WINAPI URLMonikerImpl_IsRunning(IMoniker* iface,
581                                                IBindCtx* pbc,
582                                                IMoniker* pmkToLeft,
583                                                IMoniker* pmkNewlyRunning)
584 {
585     URLMonikerImpl *This = (URLMonikerImpl *)iface;
586     FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,pmkNewlyRunning);
587
588     return E_NOTIMPL;
589 }
590
591 /******************************************************************************
592  *        URLMoniker_GetTimeOfLastChange
593  ******************************************************************************/
594 static HRESULT WINAPI URLMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
595                                                          IBindCtx* pbc,
596                                                          IMoniker* pmkToLeft,
597                                                          FILETIME* pFileTime)
598 {
599     URLMonikerImpl *This = (URLMonikerImpl *)iface;
600     FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,pFileTime);
601
602     return E_NOTIMPL;
603 }
604
605 /******************************************************************************
606  *        URLMoniker_Inverse
607  ******************************************************************************/
608 static HRESULT WINAPI URLMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
609 {
610     URLMonikerImpl *This = (URLMonikerImpl *)iface;
611     TRACE("(%p,%p)\n",This,ppmk);
612
613     return MK_E_NOINVERSE;
614 }
615
616 /******************************************************************************
617  *        URLMoniker_CommonPrefixWith
618  ******************************************************************************/
619 static HRESULT WINAPI URLMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
620 {
621     URLMonikerImpl *This = (URLMonikerImpl *)iface;
622     FIXME("(%p)->(%p,%p): stub\n",This,pmkOther,ppmkPrefix);
623
624     return E_NOTIMPL;
625 }
626
627 /******************************************************************************
628  *        URLMoniker_RelativePathTo
629  ******************************************************************************/
630 static HRESULT WINAPI URLMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
631 {
632     URLMonikerImpl *This = (URLMonikerImpl *)iface;
633     FIXME("(%p)->(%p,%p): stub\n",This,pmOther,ppmkRelPath);
634
635     return E_NOTIMPL;
636 }
637
638 /******************************************************************************
639  *        URLMoniker_GetDisplayName
640  ******************************************************************************/
641 static HRESULT WINAPI URLMonikerImpl_GetDisplayName(IMoniker* iface,
642                                                     IBindCtx* pbc,
643                                                     IMoniker* pmkToLeft,
644                                                     LPOLESTR *ppszDisplayName)
645 {
646     URLMonikerImpl *This = (URLMonikerImpl *)iface;
647     
648     int len;
649     
650     TRACE("(%p,%p,%p,%p)\n",This,pbc,pmkToLeft,ppszDisplayName);
651     
652     if(!ppszDisplayName)
653         return E_INVALIDARG;
654     
655     /* FIXME: If this is a partial URL, try and get a URL moniker from SZ_URLCONTEXT in the bind context,
656         then look at pmkToLeft to try and complete the URL
657     */
658     len = lstrlenW(This->URLName)+1;
659     *ppszDisplayName = CoTaskMemAlloc(len*sizeof(WCHAR));
660     if(!*ppszDisplayName)
661         return E_OUTOFMEMORY;
662     lstrcpyW(*ppszDisplayName, This->URLName);
663     return S_OK;
664 }
665
666 /******************************************************************************
667  *        URLMoniker_ParseDisplayName
668  ******************************************************************************/
669 static HRESULT WINAPI URLMonikerImpl_ParseDisplayName(IMoniker* iface,
670                                                       IBindCtx* pbc,
671                                                       IMoniker* pmkToLeft,
672                                                       LPOLESTR pszDisplayName,
673                                                       ULONG* pchEaten,
674                                                       IMoniker** ppmkOut)
675 {
676     URLMonikerImpl *This = (URLMonikerImpl *)iface;
677     FIXME("(%p)->(%p,%p,%p,%p,%p): stub\n",This,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
678
679     return E_NOTIMPL;
680 }
681
682 /******************************************************************************
683  *        URLMoniker_IsSystemMoniker
684  ******************************************************************************/
685 static HRESULT WINAPI URLMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
686 {
687     URLMonikerImpl *This = (URLMonikerImpl *)iface;
688     TRACE("(%p,%p)\n",This,pwdMksys);
689
690     if(!pwdMksys)
691         return E_INVALIDARG;
692
693     *pwdMksys = MKSYS_URLMONIKER;
694     return S_OK;
695 }
696
697 static HRESULT WINAPI URLMonikerImpl_IBinding_QueryInterface(IBinding* iface,REFIID riid,void** ppvObject)
698 {
699     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
700
701     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObject);
702
703     /* Perform a sanity check on the parameters.*/
704     if ( (This==0) || (ppvObject==0) )
705         return E_INVALIDARG;
706
707     /* Initialize the return parameter */
708     *ppvObject = 0;
709
710     /* Compare the riid with the interface IDs implemented by this object.*/
711     if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IBinding, riid))
712         *ppvObject = iface;
713
714     /* Check that we obtained an interface.*/
715     if ((*ppvObject)==0)
716         return E_NOINTERFACE;
717
718     /* Query Interface always increases the reference count by one when it is successful */
719     IBinding_AddRef(iface);
720
721     return S_OK;
722
723 }
724
725 static ULONG WINAPI URLMonikerImpl_IBinding_AddRef(IBinding* iface)
726 {
727     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
728     TRACE("(%p)\n",This);
729
730     return URLMonikerImpl_AddRef((IMoniker*)This);
731 }
732
733 static ULONG WINAPI URLMonikerImpl_IBinding_Release(IBinding* iface)
734 {
735     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
736     TRACE("(%p)\n",This);
737
738     return URLMonikerImpl_Release((IMoniker*)This);
739 }
740
741 static HRESULT WINAPI URLMonikerImpl_IBinding_Abort(IBinding* iface)
742 {
743     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
744     FIXME("(%p): stub\n", This);
745
746     return E_NOTIMPL;
747 }
748
749 static HRESULT WINAPI URLMonikerImpl_IBinding_GetBindResult(IBinding* iface, CLSID* pclsidProtocol, DWORD* pdwResult, LPOLESTR* pszResult, DWORD* pdwReserved)
750 {
751     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
752     FIXME("(%p)->(%p, %p, %p, %p): stub\n", This, pclsidProtocol, pdwResult, pszResult, pdwReserved);
753
754     return E_NOTIMPL;
755 }
756
757 static HRESULT WINAPI URLMonikerImpl_IBinding_GetPriority(IBinding* iface, LONG* pnPriority)
758 {
759     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
760     FIXME("(%p)->(%p): stub\n", This, pnPriority);
761
762     return E_NOTIMPL;
763 }
764
765 static HRESULT WINAPI URLMonikerImpl_IBinding_Resume(IBinding* iface)
766 {
767     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
768     FIXME("(%p): stub\n", This);
769
770     return E_NOTIMPL;
771 }
772
773 static HRESULT WINAPI URLMonikerImpl_IBinding_SetPriority(IBinding* iface, LONG nPriority)
774 {
775     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
776     FIXME("(%p)->(%ld): stub\n", This, nPriority);
777
778     return E_NOTIMPL;
779 }
780
781 static HRESULT WINAPI URLMonikerImpl_IBinding_Suspend(IBinding* iface)
782 {
783     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
784     FIXME("(%p): stub\n", This);
785
786     return E_NOTIMPL;
787 }
788
789 /********************************************************************************/
790 /* Virtual function table for the URLMonikerImpl class which  include IPersist,*/
791 /* IPersistStream and IMoniker functions.                                       */
792 static IMonikerVtbl VT_URLMonikerImpl =
793 {
794     URLMonikerImpl_QueryInterface,
795     URLMonikerImpl_AddRef,
796     URLMonikerImpl_Release,
797     URLMonikerImpl_GetClassID,
798     URLMonikerImpl_IsDirty,
799     URLMonikerImpl_Load,
800     URLMonikerImpl_Save,
801     URLMonikerImpl_GetSizeMax,
802     URLMonikerImpl_BindToObject,
803     URLMonikerImpl_BindToStorage,
804     URLMonikerImpl_Reduce,
805     URLMonikerImpl_ComposeWith,
806     URLMonikerImpl_Enum,
807     URLMonikerImpl_IsEqual,
808     URLMonikerImpl_Hash,
809     URLMonikerImpl_IsRunning,
810     URLMonikerImpl_GetTimeOfLastChange,
811     URLMonikerImpl_Inverse,
812     URLMonikerImpl_CommonPrefixWith,
813     URLMonikerImpl_RelativePathTo,
814     URLMonikerImpl_GetDisplayName,
815     URLMonikerImpl_ParseDisplayName,
816     URLMonikerImpl_IsSystemMoniker
817 };
818
819 static IBindingVtbl VTBinding_URLMonikerImpl =
820 {
821     URLMonikerImpl_IBinding_QueryInterface,
822     URLMonikerImpl_IBinding_AddRef,
823     URLMonikerImpl_IBinding_Release,
824     URLMonikerImpl_IBinding_Abort,
825     URLMonikerImpl_IBinding_Suspend,
826     URLMonikerImpl_IBinding_Resume,
827     URLMonikerImpl_IBinding_SetPriority,
828     URLMonikerImpl_IBinding_GetPriority,
829     URLMonikerImpl_IBinding_GetBindResult
830 };
831
832 /******************************************************************************
833  *         URLMoniker_Construct (local function)
834  *******************************************************************************/
835 static HRESULT URLMonikerImpl_Construct(URLMonikerImpl* This, LPCOLESTR lpszLeftURLName, LPCOLESTR lpszURLName)
836 {
837     HRESULT hres;
838     DWORD sizeStr;
839
840     TRACE("(%p,%s,%s)\n",This,debugstr_w(lpszLeftURLName),debugstr_w(lpszURLName));
841     memset(This, 0, sizeof(*This));
842
843     /* Initialize the virtual function table. */
844     This->lpvtbl1      = &VT_URLMonikerImpl;
845     This->lpvtbl2      = &VTBinding_URLMonikerImpl;
846     This->ref          = 0;
847
848     if(lpszLeftURLName) {
849         hres = UrlCombineW(lpszLeftURLName, lpszURLName, NULL, &sizeStr, 0);
850         if(FAILED(hres)) {
851             return hres;
852         }
853         sizeStr++;
854     }
855     else
856         sizeStr = lstrlenW(lpszURLName)+1;
857
858     This->URLName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr));
859
860     if (This->URLName==NULL)
861         return E_OUTOFMEMORY;
862
863     if(lpszLeftURLName) {
864         hres = UrlCombineW(lpszLeftURLName, lpszURLName, This->URLName, &sizeStr, 0);
865         if(FAILED(hres)) {
866             HeapFree(GetProcessHeap(), 0, This->URLName);
867             return hres;
868         }
869     }
870     else
871         strcpyW(This->URLName,lpszURLName);
872
873     return S_OK;
874 }
875
876 /***********************************************************************
877  *           CreateAsyncBindCtx (URLMON.@)
878  */
879 HRESULT WINAPI CreateAsyncBindCtx(DWORD reserved, IBindStatusCallback *callback,
880     IEnumFORMATETC *format, IBindCtx **pbind)
881 {
882     HRESULT hres;
883     BIND_OPTS bindopts;
884     IBindCtx *bctx;
885
886     TRACE("(%08lx %p %p %p)\n", reserved, callback, format, pbind);
887
888     if(!callback)
889         return E_INVALIDARG;
890     if(format)
891         FIXME("format is not supported yet\n");
892
893     hres = CreateBindCtx(0, &bctx);
894     if(FAILED(hres))
895         return hres;
896
897     bindopts.cbStruct = sizeof(BIND_OPTS);
898     bindopts.grfFlags = BIND_MAYBOTHERUSER;
899     bindopts.grfMode = STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
900     bindopts.dwTickCountDeadline = 0;
901     IBindCtx_SetBindOptions(bctx, &bindopts);
902
903     hres = IBindCtx_RegisterObjectParam(bctx, (LPOLESTR)BSCBHolder, (IUnknown*)callback);
904     if(FAILED(hres)) {
905         IBindCtx_Release(bctx);
906         return hres;
907     }
908
909     *pbind = bctx;
910
911     return S_OK;
912 }
913 /***********************************************************************
914  *           CreateAsyncBindCtxEx (URLMON.@)
915  *
916  * Create an asynchronous bind context.
917  *
918  * FIXME
919  *   Not implemented.
920  */ 
921 HRESULT WINAPI CreateAsyncBindCtxEx(IBindCtx *ibind, DWORD options,
922     IBindStatusCallback *callback, IEnumFORMATETC *format, IBindCtx** pbind,
923     DWORD reserved)
924 {
925      FIXME("stub, returns failure\n");
926      return E_INVALIDARG;
927 }
928
929
930 /***********************************************************************
931  *           CreateURLMoniker (URLMON.@)
932  *
933  * Create a url moniker.
934  *
935  * PARAMS
936  *    pmkContext [I] Context
937  *    szURL      [I] Url to create the moniker for
938  *    ppmk       [O] Destination for created moniker.
939  *
940  * RETURNS
941  *    Success: S_OK. ppmk contains the created IMoniker object.
942  *    Failure: MK_E_SYNTAX if szURL is not a valid url, or
943  *             E_OUTOFMEMORY if memory allocation fails.
944  */
945 HRESULT WINAPI CreateURLMoniker(IMoniker *pmkContext, LPCWSTR szURL, IMoniker **ppmk)
946 {
947     URLMonikerImpl *obj;
948     HRESULT hres;
949     IID iid = IID_IMoniker;
950     LPOLESTR lefturl = NULL;
951
952     TRACE("(%p, %s, %p)\n", pmkContext, debugstr_w(szURL), ppmk);
953
954     if(!(obj = HeapAlloc(GetProcessHeap(), 0, sizeof(*obj))))
955         return E_OUTOFMEMORY;
956
957     if(pmkContext) {
958         CLSID clsid;
959         IBindCtx* bind;
960         IMoniker_GetClassID(pmkContext, &clsid);
961         if(IsEqualCLSID(&clsid, &CLSID_StdURLMoniker) && SUCCEEDED(CreateBindCtx(0, &bind))) {
962             URLMonikerImpl_GetDisplayName(pmkContext, bind, NULL, &lefturl);
963             IBindCtx_Release(bind);
964         }
965     }
966         
967     hres = URLMonikerImpl_Construct(obj, lefturl, szURL);
968     CoTaskMemFree(lefturl);
969     if(SUCCEEDED(hres))
970         hres = URLMonikerImpl_QueryInterface((IMoniker*)obj, &iid, (void**)ppmk);
971     else
972         HeapFree(GetProcessHeap(), 0, obj);
973     return hres;
974 }
975
976
977 /***********************************************************************
978  *           CoInternetGetSession (URLMON.@)
979  *
980  * Create a new internet session and return an IInternetSession interface
981  * representing it.
982  *
983  * PARAMS
984  *    dwSessionMode      [I] Mode for the internet session
985  *    ppIInternetSession [O] Destination for creates IInternetSession object
986  *    dwReserved         [I] Reserved, must be 0.
987  *
988  * RETURNS
989  *    Success: S_OK. ppIInternetSession contains the IInternetSession interface.
990  *    Failure: E_INVALIDARG, if any argument is invalid, or
991  *             E_OUTOFMEMORY if memory allocation fails.
992  */
993 HRESULT WINAPI CoInternetGetSession(DWORD dwSessionMode, IInternetSession **ppIInternetSession, DWORD dwReserved)
994 {
995     FIXME("(%ld, %p, %ld): stub\n", dwSessionMode, ppIInternetSession, dwReserved);
996
997     if(dwSessionMode) {
998       ERR("dwSessionMode: %ld, must be zero\n", dwSessionMode);
999     }
1000
1001     if(dwReserved) {
1002       ERR("dwReserved: %ld, must be zero\n", dwReserved);
1003     }
1004
1005     *ppIInternetSession=NULL;
1006     return E_OUTOFMEMORY;
1007 }
1008
1009 /***********************************************************************
1010  *           CoInternetQueryInfo (URLMON.@)
1011  *
1012  * Retrieves information relevant to a specified URL
1013  *
1014  * RETURNS
1015  *    S_OK                      success
1016  *    S_FALSE                   buffer too small
1017  *    INET_E_QUERYOPTIONUNKNOWN invalid option
1018  *
1019  */
1020 HRESULT WINAPI CoInternetQueryInfo(LPCWSTR pwzUrl, QUERYOPTION QueryOption,
1021   DWORD dwQueryFlags, LPVOID pvBuffer, DWORD cbBuffer, DWORD * pcbBuffer,
1022   DWORD dwReserved)
1023 {
1024   FIXME("(%s, %x, %lx, %p, %lx, %p, %lx): stub\n", debugstr_w(pwzUrl),
1025     QueryOption, dwQueryFlags, pvBuffer, cbBuffer, pcbBuffer, dwReserved);
1026   return S_OK;
1027 }
1028
1029 static BOOL URLMON_IsBinary(LPVOID pBuffer, DWORD cbSize)
1030 {
1031     unsigned int i, binarycount = 0;
1032     unsigned char *buff = pBuffer;
1033     for(i=0; i<cbSize; i++) {
1034         if(buff[i] < 32)
1035             binarycount++;
1036     }
1037     return binarycount > (cbSize-binarycount);
1038 }
1039
1040 /***********************************************************************
1041  *           FindMimeFromData (URLMON.@)
1042  *
1043  * Determines the Multipurpose Internet Mail Extensions (MIME) type from the data provided.
1044  *
1045  * NOTE
1046  *  See http://msdn.microsoft.com/workshop/networking/moniker/overview/appendix_a.asp
1047  */
1048 HRESULT WINAPI FindMimeFromData(LPBC pBC, LPCWSTR pwzUrl, LPVOID pBuffer,
1049    DWORD cbSize, LPCWSTR pwzMimeProposed, DWORD dwMimeFlags,
1050    LPWSTR* ppwzMimeOut, DWORD dwReserved)
1051 {
1052     static const WCHAR szBinaryMime[] = {'a','p','p','l','i','c','a','t','i','o','n','/','o','c','t','e','t','-','s','t','r','e','a','m','\0'};
1053     static const WCHAR szTextMime[] = {'t','e','x','t','/','p','l','a','i','n','\0'};
1054     static const WCHAR szContentType[] = {'C','o','n','t','e','n','t',' ','T','y','p','e','\0'};
1055     WCHAR szTmpMime[256];
1056     LPCWSTR mimeType = NULL;
1057     HKEY hKey = NULL;
1058
1059     TRACE("(%p,%s,%p,%ld,%s,0x%lx,%p,0x%lx)\n", pBC, debugstr_w(pwzUrl), pBuffer, cbSize,
1060           debugstr_w(pwzMimeProposed), dwMimeFlags, ppwzMimeOut, dwReserved);
1061
1062     if((!pwzUrl && (!pBuffer || cbSize <= 0)) || !ppwzMimeOut)
1063         return E_INVALIDARG;
1064
1065     if(pwzMimeProposed)
1066         mimeType = pwzMimeProposed;
1067     else {
1068         /* Try and find the mime type in the registry */
1069         if(pwzUrl) {
1070             LPWSTR ext = strrchrW(pwzUrl, '.');
1071             if(ext) {
1072                 DWORD dwSize;
1073                 if(!RegOpenKeyExW(HKEY_CLASSES_ROOT, ext, 0, 0, &hKey)) {
1074                     if(!RegQueryValueExW(hKey, szContentType, NULL, NULL, (LPBYTE)szTmpMime, &dwSize)) {
1075                         mimeType = szTmpMime;
1076                     }
1077                     RegCloseKey(hKey);
1078                 }
1079             }
1080         }
1081     }
1082     if(!mimeType && pBuffer && cbSize > 0)
1083         mimeType = URLMON_IsBinary(pBuffer, cbSize)?szBinaryMime:szTextMime;
1084
1085     TRACE("Using %s\n", debugstr_w(mimeType));
1086     *ppwzMimeOut = CoTaskMemAlloc((lstrlenW(mimeType)+1)*sizeof(WCHAR));
1087     if(!*ppwzMimeOut) return E_OUTOFMEMORY;
1088     lstrcpyW(*ppwzMimeOut, mimeType);
1089     return S_OK;
1090 }
1091
1092 /***********************************************************************
1093  *           IsAsyncMoniker (URLMON.@)
1094  */
1095 HRESULT WINAPI IsAsyncMoniker(IMoniker *pmk)
1096 {
1097     IUnknown *am;
1098     
1099     TRACE("(%p)\n", pmk);
1100     if(!pmk)
1101         return E_INVALIDARG;
1102     if(SUCCEEDED(IMoniker_QueryInterface(pmk, &IID_IAsyncMoniker, (void**)&am))) {
1103         IUnknown_Release(am);
1104         return S_OK;
1105     }
1106     return S_FALSE;
1107 }
1108
1109 /***********************************************************************
1110  *           RegisterBindStatusCallback (URLMON.@)
1111  *
1112  * Register a bind status callback.
1113  *
1114  * PARAMS
1115  *  pbc           [I] Binding context
1116  *  pbsc          [I] Callback to register
1117  *  ppbscPrevious [O] Destination for previous callback
1118  *  dwReserved    [I] Reserved, must be 0.
1119  *
1120  * RETURNS
1121  *    Success: S_OK.
1122  *    Failure: E_INVALIDARG, if any argument is invalid, or
1123  *             E_OUTOFMEMORY if memory allocation fails.
1124  */
1125 HRESULT WINAPI RegisterBindStatusCallback(
1126     IBindCtx *pbc,
1127     IBindStatusCallback *pbsc,
1128     IBindStatusCallback **ppbscPrevious,
1129     DWORD dwReserved)
1130 {
1131     IBindStatusCallback *prev;
1132
1133     TRACE("(%p,%p,%p,%lu)\n", pbc, pbsc, ppbscPrevious, dwReserved);
1134
1135     if (pbc == NULL || pbsc == NULL)
1136         return E_INVALIDARG;
1137
1138     if (SUCCEEDED(IBindCtx_GetObjectParam(pbc, (LPOLESTR)BSCBHolder, (IUnknown **)&prev)))
1139     {
1140         IBindCtx_RevokeObjectParam(pbc, (LPOLESTR)BSCBHolder);
1141         if (ppbscPrevious)
1142             *ppbscPrevious = prev;
1143         else
1144             IBindStatusCallback_Release(prev);
1145     }
1146
1147         return IBindCtx_RegisterObjectParam(pbc, (LPOLESTR)BSCBHolder, (IUnknown *)pbsc);
1148 }
1149
1150 /***********************************************************************
1151  *           RevokeBindStatusCallback (URLMON.@)
1152  *
1153  * Unregister a bind status callback.
1154  *
1155  *  pbc           [I] Binding context
1156  *  pbsc          [I] Callback to unregister
1157  *
1158  * RETURNS
1159  *    Success: S_OK.
1160  *    Failure: E_INVALIDARG, if any argument is invalid, or
1161  *             E_FAIL if pbsc wasn't registered with pbc.
1162  */
1163 HRESULT WINAPI RevokeBindStatusCallback(
1164     IBindCtx *pbc,
1165     IBindStatusCallback *pbsc)
1166 {
1167     IBindStatusCallback *callback;
1168     HRESULT hr = E_FAIL;
1169
1170         TRACE("(%p,%p)\n", pbc, pbsc);
1171
1172     if (pbc == NULL || pbsc == NULL)
1173         return E_INVALIDARG;
1174
1175     if (SUCCEEDED(IBindCtx_GetObjectParam(pbc, (LPOLESTR)BSCBHolder, (IUnknown **)&callback)))
1176     {
1177         if (callback == pbsc)
1178         {
1179             IBindCtx_RevokeObjectParam(pbc, (LPOLESTR)BSCBHolder);
1180             hr = S_OK;
1181         }
1182         IBindStatusCallback_Release(pbsc);
1183     }
1184
1185     return hr;
1186 }
1187
1188 /***********************************************************************
1189  *           ReleaseBindInfo (URLMON.@)
1190  *
1191  * Release the resources used by the specified BINDINFO structure.
1192  *
1193  * PARAMS
1194  *  pbindinfo [I] BINDINFO to release.
1195  *
1196  * RETURNS
1197  *  Nothing.
1198  */
1199 void WINAPI ReleaseBindInfo(BINDINFO* pbindinfo)
1200 {
1201     FIXME("(%p)stub!\n", pbindinfo);
1202 }
1203
1204 /***********************************************************************
1205  *           URLDownloadToFileA (URLMON.@)
1206  *
1207  * Downloads URL szURL to rile szFileName and call lpfnCB callback to
1208  * report progress.
1209  *
1210  * PARAMS
1211  *  pCaller    [I] controlling IUnknown interface.
1212  *  szURL      [I] URL of the file to download
1213  *  szFileName [I] file name to store the content of the URL
1214  *  dwReserved [I] reserved - set to 0
1215  *  lpfnCB     [I] callback for progress report
1216  *
1217  * RETURNS
1218  *  S_OK on success
1219  *  E_OUTOFMEMORY when going out of memory
1220  */
1221 HRESULT WINAPI URLDownloadToFileA(LPUNKNOWN pCaller,
1222                                   LPCSTR szURL,
1223                                   LPCSTR szFileName,
1224                                   DWORD dwReserved,
1225                                   LPBINDSTATUSCALLBACK lpfnCB)
1226 {
1227     UNICODE_STRING szURL_w, szFileName_w;
1228
1229     if ((szURL == NULL) || (szFileName == NULL)) {
1230         FIXME("(%p,%s,%s,%08lx,%p) cannot accept NULL strings !\n", pCaller, debugstr_a(szURL), debugstr_a(szFileName), dwReserved, lpfnCB);
1231         return E_INVALIDARG; /* The error code is not specified in this case... */
1232     }
1233     
1234     if (RtlCreateUnicodeStringFromAsciiz(&szURL_w, szURL)) {
1235         if (RtlCreateUnicodeStringFromAsciiz(&szFileName_w, szFileName)) {
1236             HRESULT ret = URLDownloadToFileW(pCaller, szURL_w.Buffer, szFileName_w.Buffer, dwReserved, lpfnCB);
1237
1238             RtlFreeUnicodeString(&szURL_w);
1239             RtlFreeUnicodeString(&szFileName_w);
1240             
1241             return ret;
1242         } else {
1243             RtlFreeUnicodeString(&szURL_w);
1244         }
1245     }
1246     
1247     FIXME("(%p,%s,%s,%08lx,%p) could not allocate W strings !\n", pCaller, szURL, szFileName, dwReserved, lpfnCB);
1248     return E_OUTOFMEMORY;
1249 }
1250
1251 /***********************************************************************
1252  *           URLDownloadToFileW (URLMON.@)
1253  *
1254  * Downloads URL szURL to rile szFileName and call lpfnCB callback to
1255  * report progress.
1256  *
1257  * PARAMS
1258  *  pCaller    [I] controlling IUnknown interface.
1259  *  szURL      [I] URL of the file to download
1260  *  szFileName [I] file name to store the content of the URL
1261  *  dwReserved [I] reserved - set to 0
1262  *  lpfnCB     [I] callback for progress report
1263  *
1264  * RETURNS
1265  *  S_OK on success
1266  *  E_OUTOFMEMORY when going out of memory
1267  */
1268 HRESULT WINAPI URLDownloadToFileW(LPUNKNOWN pCaller,
1269                                   LPCWSTR szURL,
1270                                   LPCWSTR szFileName,
1271                                   DWORD dwReserved,
1272                                   LPBINDSTATUSCALLBACK lpfnCB)
1273 {
1274     HINTERNET hinternet, hcon, hreq;
1275     BOOL r;
1276     CHAR buffer[0x1000];
1277     DWORD sz, total, written;
1278     DWORD total_size = 0xFFFFFFFF, arg_size = sizeof(total_size);
1279     URL_COMPONENTSW url;
1280     WCHAR host[0x80], path[0x100];
1281     HANDLE hfile;
1282     static const WCHAR wszAppName[]={'u','r','l','m','o','n','.','d','l','l',0};
1283
1284     /* Note: all error codes would need to be checked agains real Windows behaviour... */
1285     TRACE("(%p,%s,%s,%08lx,%p) stub!\n", pCaller, debugstr_w(szURL), debugstr_w(szFileName), dwReserved, lpfnCB);
1286
1287     if ((szURL == NULL) || (szFileName == NULL)) {
1288         FIXME(" cannot accept NULL strings !\n");
1289         return E_INVALIDARG;
1290     }
1291
1292     /* Would be better to use the application name here rather than 'urlmon' :-/ */
1293     hinternet = InternetOpenW(wszAppName, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1294     if (hinternet == NULL) {
1295         return E_OUTOFMEMORY;
1296     }                                                                                                                             
1297
1298     memset(&url, 0, sizeof(url));
1299     url.dwStructSize = sizeof(url);
1300     url.lpszHostName = host;
1301     url.dwHostNameLength = sizeof(host);
1302     url.lpszUrlPath = path;
1303     url.dwUrlPathLength = sizeof(path);
1304
1305     if (!InternetCrackUrlW(szURL, 0, 0, &url)) {
1306         InternetCloseHandle(hinternet);
1307         return E_OUTOFMEMORY;
1308     }
1309
1310     if (lpfnCB) {
1311         if (IBindStatusCallback_OnProgress(lpfnCB, 0, 0, BINDSTATUS_CONNECTING, url.lpszHostName) == E_ABORT) {
1312             InternetCloseHandle(hinternet);
1313             return S_OK;
1314         }
1315     }
1316     
1317     hcon = InternetConnectW(hinternet, url.lpszHostName, url.nPort,
1318                             url.lpszUserName, url.lpszPassword,
1319                             INTERNET_SERVICE_HTTP, 0, 0);
1320     if (!hcon) {
1321         InternetCloseHandle(hinternet);
1322         return E_OUTOFMEMORY;
1323     }
1324     
1325     hreq = HttpOpenRequestW(hcon, NULL, url.lpszUrlPath, NULL, NULL, NULL, 0, 0);
1326     if (!hreq) {
1327         InternetCloseHandle(hinternet);
1328         InternetCloseHandle(hcon);
1329         return E_OUTOFMEMORY;
1330     }                                                                                                                             
1331
1332     if (!HttpSendRequestW(hreq, NULL, 0, NULL, 0)) {
1333         InternetCloseHandle(hinternet);
1334         InternetCloseHandle(hcon);
1335         InternetCloseHandle(hreq);
1336         return E_OUTOFMEMORY;
1337     }
1338     
1339     if (HttpQueryInfoW(hreq, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
1340                        &total_size, &arg_size, NULL)) {
1341         TRACE(" total size : %ld\n", total_size);
1342     }
1343     
1344     hfile = CreateFileW(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
1345                         FILE_ATTRIBUTE_NORMAL, NULL );
1346     if (hfile == INVALID_HANDLE_VALUE) {
1347         return E_ACCESSDENIED;
1348     }
1349     
1350     if (lpfnCB) {
1351         if (IBindStatusCallback_OnProgress(lpfnCB, 0, total_size != 0xFFFFFFFF ? total_size : 0,
1352                                            BINDSTATUS_BEGINDOWNLOADDATA, szURL) == E_ABORT) {
1353             InternetCloseHandle(hreq);
1354             InternetCloseHandle(hcon);
1355             InternetCloseHandle(hinternet);
1356             CloseHandle(hfile);
1357             return S_OK;
1358         }
1359     }
1360     
1361     total = 0;
1362     while (1) {
1363         r = InternetReadFile(hreq, buffer, sizeof(buffer), &sz);
1364         if (!r) {
1365             InternetCloseHandle(hreq);
1366             InternetCloseHandle(hcon);
1367             InternetCloseHandle(hinternet);
1368             
1369             CloseHandle(hfile);
1370             return E_OUTOFMEMORY;           
1371         }
1372         if (!sz)
1373             break;
1374         
1375         total += sz;
1376
1377         if (lpfnCB) {
1378             if (IBindStatusCallback_OnProgress(lpfnCB, total, total_size != 0xFFFFFFFF ? total_size : 0,
1379                                                BINDSTATUS_DOWNLOADINGDATA, szURL) == E_ABORT) {
1380                 InternetCloseHandle(hreq);
1381                 InternetCloseHandle(hcon);
1382                 InternetCloseHandle(hinternet);
1383                 CloseHandle(hfile);
1384                 return S_OK;
1385             }
1386         }
1387         
1388         if (!WriteFile(hfile, buffer, sz, &written, NULL)) {
1389             InternetCloseHandle(hreq);
1390             InternetCloseHandle(hcon);
1391             InternetCloseHandle(hinternet);
1392             
1393             CloseHandle(hfile);
1394             return E_OUTOFMEMORY;
1395         }
1396     }
1397
1398     if (lpfnCB) {
1399         if (IBindStatusCallback_OnProgress(lpfnCB, total, total_size != 0xFFFFFFFF ? total_size : 0,
1400                                            BINDSTATUS_ENDDOWNLOADDATA, szURL) == E_ABORT) {
1401             InternetCloseHandle(hreq);
1402             InternetCloseHandle(hcon);
1403             InternetCloseHandle(hinternet);
1404             CloseHandle(hfile);
1405             return S_OK;
1406         }
1407     }
1408     
1409     InternetCloseHandle(hreq);
1410     InternetCloseHandle(hcon);
1411     InternetCloseHandle(hinternet);
1412     
1413     CloseHandle(hfile);
1414
1415     return S_OK;
1416 }
1417
1418 /***********************************************************************
1419  *           HlinkSimpleNavigateToString (URLMON.@)
1420  */
1421 HRESULT WINAPI HlinkSimpleNavigateToString( LPCWSTR szTarget,
1422     LPCWSTR szLocation, LPCWSTR szTargetFrameName, IUnknown *pUnk,
1423     IBindCtx *pbc, IBindStatusCallback *pbsc, DWORD grfHLNF, DWORD dwReserved)
1424 {
1425     FIXME("%s\n", debugstr_w( szTarget ) );
1426     return E_NOTIMPL;
1427 }
1428
1429 /***********************************************************************
1430  *           HlinkNavigateString (URLMON.@)
1431  */
1432 HRESULT WINAPI HlinkNavigateString( IUnknown *pUnk, LPCWSTR szTarget )
1433 {
1434     TRACE("%p %s\n", pUnk, debugstr_w( szTarget ) );
1435     return HlinkSimpleNavigateToString( 
1436                szTarget, NULL, NULL, pUnk, NULL, NULL, 0, 0 );
1437 }