4 * Copyright 1999 Ulrich Czekalla for Corel Corporation
5 * Copyright 2002 Huw D M Davies for CodeWeavers
6 * Copyright 2005 Jacek Caban for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "urlmon_main.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
33 const IMonikerVtbl *lpIMonikerVtbl;
37 LPOLESTR URLName; /* URL string identified by this URLmoniker */
40 #define MONIKER_THIS(iface) DEFINE_THIS(URLMoniker, IMoniker, iface)
42 static HRESULT WINAPI URLMoniker_QueryInterface(IMoniker *iface, REFIID riid, void **ppv)
44 URLMoniker *This = MONIKER_THIS(iface);
49 if(IsEqualIID(&IID_IUnknown, riid)) {
50 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
52 }else if(IsEqualIID(&IID_IPersist, riid)) {
53 TRACE("(%p)->(IID_IPersist %p)\n", This, ppv);
55 }else if(IsEqualIID(&IID_IPersistStream,riid)) {
56 TRACE("(%p)->(IID_IPersistStream %p)\n", This, ppv);
58 }else if(IsEqualIID(&IID_IMoniker, riid)) {
59 TRACE("(%p)->(IID_IMoniker %p)\n", This, ppv);
61 }else if(IsEqualIID(&IID_IAsyncMoniker, riid)) {
62 TRACE("(%p)->(IID_IAsyncMoniker %p)\n", This, ppv);
65 WARN("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppv);
70 IMoniker_AddRef((IUnknown*)*ppv);
74 static ULONG WINAPI URLMoniker_AddRef(IMoniker *iface)
76 URLMoniker *This = MONIKER_THIS(iface);
77 ULONG refCount = InterlockedIncrement(&This->ref);
79 TRACE("(%p) ref=%u\n",This, refCount);
84 static ULONG WINAPI URLMoniker_Release(IMoniker *iface)
86 URLMoniker *This = MONIKER_THIS(iface);
87 ULONG refCount = InterlockedDecrement(&This->ref);
89 TRACE("(%p) ref=%u\n",This, refCount);
92 heap_free(This->URLName);
95 URLMON_UnlockModule();
101 static HRESULT WINAPI URLMoniker_GetClassID(IMoniker *iface, CLSID *pClassID)
103 URLMoniker *This = MONIKER_THIS(iface);
105 TRACE("(%p,%p)\n", This, pClassID);
110 /* Windows always returns CLSID_StdURLMoniker */
111 *pClassID = CLSID_StdURLMoniker;
115 static HRESULT WINAPI URLMoniker_IsDirty(IMoniker *iface)
117 URLMoniker *This = MONIKER_THIS(iface);
119 TRACE("(%p)\n",This);
121 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
122 method in the OLE-provided moniker interfaces always return S_FALSE because
123 their internal state never changes. */
127 static HRESULT WINAPI URLMoniker_Load(IMoniker* iface,IStream* pStm)
129 URLMoniker *This = MONIKER_THIS(iface);
134 TRACE("(%p,%p)\n",This,pStm);
141 * Writes a ULONG containing length of unicode string, followed
142 * by that many unicode characters
144 res = IStream_Read(pStm, &size, sizeof(ULONG), &got);
146 if(got == sizeof(ULONG)) {
147 heap_free(This->URLName);
148 This->URLName = heap_alloc(size);
152 res = IStream_Read(pStm, This->URLName, size, NULL);
153 This->URLName[size/sizeof(WCHAR) - 1] = 0;
163 static HRESULT WINAPI URLMoniker_Save(IMoniker *iface, IStream* pStm, BOOL fClearDirty)
165 URLMoniker *This = MONIKER_THIS(iface);
169 TRACE("(%p,%p,%d)\n", This, pStm, fClearDirty);
174 size = (strlenW(This->URLName) + 1)*sizeof(WCHAR);
175 res=IStream_Write(pStm,&size,sizeof(ULONG),NULL);
177 res=IStream_Write(pStm,This->URLName,size,NULL);
183 static HRESULT WINAPI URLMoniker_GetSizeMax(IMoniker* iface, ULARGE_INTEGER *pcbSize)
185 URLMoniker *This = MONIKER_THIS(iface);
187 TRACE("(%p,%p)\n",This,pcbSize);
192 pcbSize->QuadPart = sizeof(ULONG) + ((strlenW(This->URLName)+1) * sizeof(WCHAR));
196 static HRESULT WINAPI URLMoniker_BindToObject(IMoniker *iface, IBindCtx* pbc, IMoniker *pmkToLeft,
197 REFIID riid, void **ppv)
199 URLMoniker *This = MONIKER_THIS(iface);
200 IRunningObjectTable *obj_tbl;
203 TRACE("(%p)->(%p,%p,%s,%p): stub\n", This, pbc, pmkToLeft, debugstr_guid(riid), ppv);
205 hres = IBindCtx_GetRunningObjectTable(pbc, &obj_tbl);
206 if(SUCCEEDED(hres)) {
207 FIXME("use running object table\n");
208 IRunningObjectTable_Release(obj_tbl);
211 return bind_to_object(iface, This->URLName, pbc, riid, ppv);
214 static HRESULT WINAPI URLMoniker_BindToStorage(IMoniker* iface, IBindCtx* pbc,
215 IMoniker* pmkToLeft, REFIID riid, void **ppvObject)
217 URLMoniker *This = MONIKER_THIS(iface);
219 TRACE("(%p)->(%p %p %s %p)\n", This, pbc, pmkToLeft, debugstr_guid(riid), ppvObject);
222 FIXME("Unsupported pmkToLeft\n");
224 return bind_to_storage(This->URLName, pbc, riid, ppvObject);
227 static HRESULT WINAPI URLMoniker_Reduce(IMoniker *iface, IBindCtx *pbc,
228 DWORD dwReduceHowFar, IMoniker **ppmkToLeft, IMoniker **ppmkReduced)
230 URLMoniker *This = MONIKER_THIS(iface);
232 TRACE("(%p,%p,%d,%p,%p)\n", This, pbc, dwReduceHowFar, ppmkToLeft, ppmkReduced);
237 IMoniker_AddRef(iface);
238 *ppmkReduced = iface;
239 return MK_S_REDUCED_TO_SELF;
242 static HRESULT WINAPI URLMoniker_ComposeWith(IMoniker *iface, IMoniker *pmkRight,
243 BOOL fOnlyIfNotGeneric, IMoniker **ppmkComposite)
245 URLMoniker *This = MONIKER_THIS(iface);
246 FIXME("(%p)->(%p,%d,%p): stub\n",This,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
250 static HRESULT WINAPI URLMoniker_Enum(IMoniker *iface, BOOL fForward, IEnumMoniker **ppenumMoniker)
252 URLMoniker *This = MONIKER_THIS(iface);
254 TRACE("(%p,%d,%p)\n", This, fForward, ppenumMoniker);
259 /* Does not support sub-monikers */
260 *ppenumMoniker = NULL;
264 static HRESULT WINAPI URLMoniker_IsEqual(IMoniker *iface, IMoniker *pmkOtherMoniker)
266 URLMoniker *This = MONIKER_THIS(iface);
272 TRACE("(%p,%p)\n",This, pmkOtherMoniker);
274 if(pmkOtherMoniker==NULL)
277 IMoniker_GetClassID(pmkOtherMoniker,&clsid);
279 if(!IsEqualCLSID(&clsid,&CLSID_StdURLMoniker))
282 res = CreateBindCtx(0,&bind);
287 if(SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&urlPath))) {
288 int result = lstrcmpiW(urlPath, This->URLName);
289 CoTaskMemFree(urlPath);
293 IUnknown_Release(bind);
298 static HRESULT WINAPI URLMoniker_Hash(IMoniker *iface, DWORD *pdwHash)
300 URLMoniker *This = MONIKER_THIS(iface);
301 int h = 0,i,skip,len;
305 TRACE("(%p,%p)\n",This,pdwHash);
314 for(i = len ; i > 0; i--) {
315 h = (h * 37) + val[off++];
318 /* only sample some characters */
320 for(i = len; i > 0; i -= skip, off += skip) {
321 h = (h * 39) + val[off];
328 static HRESULT WINAPI URLMoniker_IsRunning(IMoniker* iface, IBindCtx* pbc,
329 IMoniker *pmkToLeft, IMoniker *pmkNewlyRunning)
331 URLMoniker *This = MONIKER_THIS(iface);
332 FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,pmkNewlyRunning);
336 static HRESULT WINAPI URLMoniker_GetTimeOfLastChange(IMoniker *iface,
337 IBindCtx *pbc, IMoniker *pmkToLeft, FILETIME *pFileTime)
339 URLMoniker *This = MONIKER_THIS(iface);
340 FIXME("(%p)->(%p,%p,%p): stub\n", This, pbc, pmkToLeft, pFileTime);
344 static HRESULT WINAPI URLMoniker_Inverse(IMoniker *iface, IMoniker **ppmk)
346 URLMoniker *This = MONIKER_THIS(iface);
347 TRACE("(%p,%p)\n",This,ppmk);
348 return MK_E_NOINVERSE;
351 static HRESULT WINAPI URLMoniker_CommonPrefixWith(IMoniker *iface, IMoniker *pmkOther, IMoniker **ppmkPrefix)
353 URLMoniker *This = MONIKER_THIS(iface);
354 FIXME("(%p)->(%p,%p): stub\n",This,pmkOther,ppmkPrefix);
358 static HRESULT WINAPI URLMoniker_RelativePathTo(IMoniker *iface, IMoniker *pmOther, IMoniker **ppmkRelPath)
360 URLMoniker *This = MONIKER_THIS(iface);
361 FIXME("(%p)->(%p,%p): stub\n",This,pmOther,ppmkRelPath);
365 static HRESULT WINAPI URLMoniker_GetDisplayName(IMoniker *iface, IBindCtx *pbc, IMoniker *pmkToLeft,
366 LPOLESTR *ppszDisplayName)
368 URLMoniker *This = MONIKER_THIS(iface);
371 TRACE("(%p,%p,%p,%p)\n", This, pbc, pmkToLeft, ppszDisplayName);
377 return E_OUTOFMEMORY;
379 /* FIXME: If this is a partial URL, try and get a URL moniker from SZ_URLCONTEXT in the bind context,
380 then look at pmkToLeft to try and complete the URL
382 len = lstrlenW(This->URLName)+1;
383 *ppszDisplayName = CoTaskMemAlloc(len*sizeof(WCHAR));
384 if(!*ppszDisplayName)
385 return E_OUTOFMEMORY;
386 lstrcpyW(*ppszDisplayName, This->URLName);
390 static HRESULT WINAPI URLMoniker_ParseDisplayName(IMoniker *iface, IBindCtx *pbc, IMoniker *pmkToLeft,
391 LPOLESTR pszDisplayName, ULONG *pchEaten, IMoniker **ppmkOut)
393 URLMoniker *This = MONIKER_THIS(iface);
394 FIXME("(%p)->(%p,%p,%p,%p,%p): stub\n",This,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
398 static HRESULT WINAPI URLMoniker_IsSystemMoniker(IMoniker *iface, DWORD *pwdMksys)
400 URLMoniker *This = MONIKER_THIS(iface);
402 TRACE("(%p,%p)\n",This,pwdMksys);
407 *pwdMksys = MKSYS_URLMONIKER;
411 static const IMonikerVtbl URLMonikerVtbl =
413 URLMoniker_QueryInterface,
416 URLMoniker_GetClassID,
420 URLMoniker_GetSizeMax,
421 URLMoniker_BindToObject,
422 URLMoniker_BindToStorage,
424 URLMoniker_ComposeWith,
428 URLMoniker_IsRunning,
429 URLMoniker_GetTimeOfLastChange,
431 URLMoniker_CommonPrefixWith,
432 URLMoniker_RelativePathTo,
433 URLMoniker_GetDisplayName,
434 URLMoniker_ParseDisplayName,
435 URLMoniker_IsSystemMoniker
438 static URLMoniker *alloc_moniker(void)
442 ret = heap_alloc(sizeof(URLMoniker));
446 ret->lpIMonikerVtbl = &URLMonikerVtbl;
453 static HRESULT URLMoniker_Init(URLMoniker *This, LPCOLESTR lpszLeftURLName, LPCOLESTR lpszURLName)
458 TRACE("(%p,%s,%s)\n",This,debugstr_w(lpszLeftURLName),debugstr_w(lpszURLName));
460 This->URLName = heap_alloc(INTERNET_MAX_URL_LENGTH*sizeof(WCHAR));
463 hres = CoInternetCombineUrl(lpszLeftURLName, lpszURLName, URL_FILE_USE_PATHURL,
464 This->URLName, INTERNET_MAX_URL_LENGTH, &sizeStr, 0);
466 hres = CoInternetParseUrl(lpszURLName, PARSE_CANONICALIZE, URL_FILE_USE_PATHURL,
467 This->URLName, INTERNET_MAX_URL_LENGTH, &sizeStr, 0);
470 heap_free(This->URLName);
476 if(sizeStr != INTERNET_MAX_URL_LENGTH)
477 This->URLName = heap_realloc(This->URLName, (sizeStr+1)*sizeof(WCHAR));
479 TRACE("URLName = %s\n", debugstr_w(This->URLName));
484 HRESULT StdURLMoniker_Construct(IUnknown *outer, void **ppv)
486 TRACE("(%p %p)\n", outer, ppv);
488 *ppv = alloc_moniker();
489 return *ppv ? S_OK : E_OUTOFMEMORY;
492 /***********************************************************************
493 * CreateURLMonikerEx (URLMON.@)
495 * Create a url moniker.
498 * pmkContext [I] Context
499 * szURL [I] Url to create the moniker for
500 * ppmk [O] Destination for created moniker.
504 * Success: S_OK. ppmk contains the created IMoniker object.
505 * Failure: MK_E_SYNTAX if szURL is not a valid url, or
506 * E_OUTOFMEMORY if memory allocation fails.
508 HRESULT WINAPI CreateURLMonikerEx(IMoniker *pmkContext, LPCWSTR szURL, IMoniker **ppmk, DWORD dwFlags)
512 LPOLESTR lefturl = NULL;
514 TRACE("(%p, %s, %p, %08x)\n", pmkContext, debugstr_w(szURL), ppmk, dwFlags);
516 if (dwFlags & URL_MK_UNIFORM) FIXME("ignoring flag URL_MK_UNIFORM\n");
518 if(!(obj = alloc_moniker()))
519 return E_OUTOFMEMORY;
524 IMoniker_IsSystemMoniker(pmkContext, &dwMksys);
525 if(dwMksys == MKSYS_URLMONIKER && SUCCEEDED(CreateBindCtx(0, &bind))) {
526 IMoniker_GetDisplayName(pmkContext, bind, NULL, &lefturl);
527 TRACE("lefturl = %s\n", debugstr_w(lefturl));
528 IBindCtx_Release(bind);
532 hres = URLMoniker_Init(obj, lefturl, szURL);
533 CoTaskMemFree(lefturl);
535 hres = URLMoniker_QueryInterface((IMoniker*)obj, &IID_IMoniker, (void**)ppmk);
536 IMoniker_Release((IMoniker*)obj);
540 /**********************************************************************
541 * CreateURLMoniker (URLMON.@)
543 * Create a url moniker.
546 * pmkContext [I] Context
547 * szURL [I] Url to create the moniker for
548 * ppmk [O] Destination for created moniker.
551 * Success: S_OK. ppmk contains the created IMoniker object.
552 * Failure: MK_E_SYNTAX if szURL is not a valid url, or
553 * E_OUTOFMEMORY if memory allocation fails.
555 HRESULT WINAPI CreateURLMoniker(IMoniker *pmkContext, LPCWSTR szURL, IMoniker **ppmk)
557 return CreateURLMonikerEx(pmkContext, szURL, ppmk, URL_MK_LEGACY);
560 /***********************************************************************
561 * IsAsyncMoniker (URLMON.@)
563 HRESULT WINAPI IsAsyncMoniker(IMoniker *pmk)
567 TRACE("(%p)\n", pmk);
570 if(SUCCEEDED(IMoniker_QueryInterface(pmk, &IID_IAsyncMoniker, (void**)&am))) {
571 IUnknown_Release(am);
577 /***********************************************************************
578 * BindAsyncMoniker (URLMON.@)
580 * Bind a bind status callback to an asynchronous URL Moniker.
583 * pmk [I] Moniker object to bind status callback to
584 * grfOpt [I] Options, seems not used
585 * pbsc [I] Status callback to bind
586 * iidResult [I] Interface to return
587 * ppvResult [O] Resulting asynchronous moniker object
591 * Failure: E_INVALIDARG, if any argument is invalid, or
592 * E_OUTOFMEMORY if memory allocation fails.
594 HRESULT WINAPI BindAsyncMoniker(IMoniker *pmk, DWORD grfOpt, IBindStatusCallback *pbsc, REFIID iidResult, LPVOID *ppvResult)
597 HRESULT hr = E_INVALIDARG;
599 TRACE("(%p %08x %p %s %p)\n", pmk, grfOpt, pbsc, debugstr_guid(iidResult), ppvResult);
601 if (pmk && ppvResult)
605 hr = CreateAsyncBindCtx(0, pbsc, NULL, &pbc);
608 hr = IMoniker_BindToObject(pmk, pbc, NULL, iidResult, ppvResult);
609 IBindCtx_Release(pbc);
615 /***********************************************************************
616 * MkParseDisplayNameEx (URLMON.@)
618 HRESULT WINAPI MkParseDisplayNameEx(IBindCtx *pbc, LPCWSTR szDisplayName, ULONG *pchEaten, LPMONIKER *ppmk)
620 TRACE("(%p %s %p %p)\n", pbc, debugstr_w(szDisplayName), pchEaten, ppmk);
622 if(is_registered_protocol(szDisplayName)) {
625 hres = CreateURLMoniker(NULL, szDisplayName, ppmk);
626 if(SUCCEEDED(hres)) {
627 *pchEaten = strlenW(szDisplayName);
632 return MkParseDisplayName(pbc, szDisplayName, pchEaten, ppmk);
636 /***********************************************************************
637 * URLDownloadToCacheFileA (URLMON.@)
639 HRESULT WINAPI URLDownloadToCacheFileA(LPUNKNOWN lpUnkCaller, LPCSTR szURL, LPSTR szFileName,
640 DWORD dwBufLength, DWORD dwReserved, LPBINDSTATUSCALLBACK pBSC)
642 LPWSTR url = NULL, file_name = NULL;
646 TRACE("(%p %s %p %d %d %p)\n", lpUnkCaller, debugstr_a(szURL), szFileName,
647 dwBufLength, dwReserved, pBSC);
650 len = MultiByteToWideChar(CP_ACP, 0, szURL, -1, NULL, 0);
651 url = heap_alloc(len*sizeof(WCHAR));
652 MultiByteToWideChar(CP_ACP, 0, szURL, -1, url, len);
656 file_name = heap_alloc(dwBufLength*sizeof(WCHAR));
658 hres = URLDownloadToCacheFileW(lpUnkCaller, url, file_name, dwBufLength*sizeof(WCHAR),
661 if(SUCCEEDED(hres) && file_name)
662 WideCharToMultiByte(CP_ACP, 0, file_name, -1, szFileName, dwBufLength, NULL, NULL);
665 heap_free(file_name);
670 /***********************************************************************
671 * URLDownloadToCacheFileW (URLMON.@)
673 HRESULT WINAPI URLDownloadToCacheFileW(LPUNKNOWN lpUnkCaller, LPCWSTR szURL, LPWSTR szFileName,
674 DWORD dwBufLength, DWORD dwReserved, LPBINDSTATUSCALLBACK pBSC)
676 WCHAR cache_path[MAX_PATH + 1];
677 FILETIME expire, modified;
681 static WCHAR header[] = {
682 'H','T','T','P','/','1','.','0',' ','2','0','0',' ',
683 'O','K','\\','r','\\','n','\\','r','\\','n',0
686 TRACE("(%p, %s, %p, %d, %d, %p)\n", lpUnkCaller, debugstr_w(szURL),
687 szFileName, dwBufLength, dwReserved, pBSC);
689 if (!szURL || !szFileName)
692 ext = PathFindExtensionW(szURL);
694 if (!CreateUrlCacheEntryW(szURL, 0, ext, cache_path, 0))
697 hr = URLDownloadToFileW(lpUnkCaller, szURL, cache_path, 0, pBSC);
701 expire.dwHighDateTime = 0;
702 expire.dwLowDateTime = 0;
703 modified.dwHighDateTime = 0;
704 modified.dwLowDateTime = 0;
706 if (!CommitUrlCacheEntryW(szURL, cache_path, expire, modified, NORMAL_CACHE_ENTRY,
707 header, sizeof(header), NULL, NULL))
710 if (strlenW(cache_path) > dwBufLength)
711 return E_OUTOFMEMORY;
713 lstrcpyW(szFileName, cache_path);
718 /***********************************************************************
719 * HlinkSimpleNavigateToMoniker (URLMON.@)
721 HRESULT WINAPI HlinkSimpleNavigateToMoniker(IMoniker *pmkTarget,
722 LPCWSTR szLocation, LPCWSTR szTargetFrameName, IUnknown *pUnk,
723 IBindCtx *pbc, IBindStatusCallback *pbsc, DWORD grfHLNF, DWORD dwReserved)
729 /***********************************************************************
730 * HlinkSimpleNavigateToString (URLMON.@)
732 HRESULT WINAPI HlinkSimpleNavigateToString( LPCWSTR szTarget,
733 LPCWSTR szLocation, LPCWSTR szTargetFrameName, IUnknown *pUnk,
734 IBindCtx *pbc, IBindStatusCallback *pbsc, DWORD grfHLNF, DWORD dwReserved)
736 FIXME("%s\n", debugstr_w( szTarget ) );
740 /***********************************************************************
741 * HlinkNavigateString (URLMON.@)
743 HRESULT WINAPI HlinkNavigateString( IUnknown *pUnk, LPCWSTR szTarget )
745 TRACE("%p %s\n", pUnk, debugstr_w( szTarget ) );
746 return HlinkSimpleNavigateToString(
747 szTarget, NULL, NULL, pUnk, NULL, NULL, 0, 0 );
750 /***********************************************************************
751 * GetSoftwareUpdateInfo (URLMON.@)
753 HRESULT WINAPI GetSoftwareUpdateInfo( LPCWSTR szDistUnit, LPSOFTDISTINFO psdi )
755 FIXME("%s %p\n", debugstr_w(szDistUnit), psdi );