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
25 #include "urlmon_main.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
37 const IMonikerVtbl *lpIMonikerVtbl;
41 LPOLESTR URLName; /* URL string identified by this URLmoniker */
44 #define MONIKER_THIS(iface) DEFINE_THIS(URLMoniker, IMoniker, iface)
46 static HRESULT WINAPI URLMoniker_QueryInterface(IMoniker *iface, REFIID riid, void **ppv)
48 URLMoniker *This = MONIKER_THIS(iface);
53 if(IsEqualIID(&IID_IUnknown, riid)) {
54 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
56 }else if(IsEqualIID(&IID_IPersist, riid)) {
57 TRACE("(%p)->(IID_IPersist %p)\n", This, ppv);
59 }else if(IsEqualIID(&IID_IPersistStream,riid)) {
60 TRACE("(%p)->(IID_IPersistStream %p)\n", This, ppv);
62 }else if(IsEqualIID(&IID_IMoniker, riid)) {
63 TRACE("(%p)->(IID_IMoniker %p)\n", This, ppv);
65 }else if(IsEqualIID(&IID_IAsyncMoniker, riid)) {
66 TRACE("(%p)->(IID_IAsyncMoniker %p)\n", This, ppv);
69 WARN("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppv);
74 IMoniker_AddRef((IUnknown*)*ppv);
78 static ULONG WINAPI URLMoniker_AddRef(IMoniker *iface)
80 URLMoniker *This = MONIKER_THIS(iface);
81 ULONG refCount = InterlockedIncrement(&This->ref);
83 TRACE("(%p) ref=%u\n",This, refCount);
88 static ULONG WINAPI URLMoniker_Release(IMoniker *iface)
90 URLMoniker *This = MONIKER_THIS(iface);
91 ULONG refCount = InterlockedDecrement(&This->ref);
93 TRACE("(%p) ref=%u\n",This, refCount);
96 heap_free(This->URLName);
99 URLMON_UnlockModule();
105 static HRESULT WINAPI URLMoniker_GetClassID(IMoniker *iface, CLSID *pClassID)
107 URLMoniker *This = MONIKER_THIS(iface);
109 TRACE("(%p,%p)\n", This, pClassID);
114 /* Windows always returns CLSID_StdURLMoniker */
115 *pClassID = CLSID_StdURLMoniker;
119 static HRESULT WINAPI URLMoniker_IsDirty(IMoniker *iface)
121 URLMoniker *This = MONIKER_THIS(iface);
123 TRACE("(%p)\n",This);
125 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
126 method in the OLE-provided moniker interfaces always return S_FALSE because
127 their internal state never changes. */
131 static HRESULT WINAPI URLMoniker_Load(IMoniker* iface,IStream* pStm)
133 URLMoniker *This = MONIKER_THIS(iface);
138 TRACE("(%p,%p)\n",This,pStm);
145 * Writes a ULONG containing length of unicode string, followed
146 * by that many unicode characters
148 res = IStream_Read(pStm, &size, sizeof(ULONG), &got);
150 if(got == sizeof(ULONG)) {
151 heap_free(This->URLName);
152 This->URLName = heap_alloc(size);
156 res = IStream_Read(pStm, This->URLName, size, NULL);
157 This->URLName[size/sizeof(WCHAR) - 1] = 0;
167 static HRESULT WINAPI URLMoniker_Save(IMoniker *iface, IStream* pStm, BOOL fClearDirty)
169 URLMoniker *This = MONIKER_THIS(iface);
173 TRACE("(%p,%p,%d)\n", This, pStm, fClearDirty);
178 size = (strlenW(This->URLName) + 1)*sizeof(WCHAR);
179 res=IStream_Write(pStm,&size,sizeof(ULONG),NULL);
181 res=IStream_Write(pStm,This->URLName,size,NULL);
187 static HRESULT WINAPI URLMoniker_GetSizeMax(IMoniker* iface, ULARGE_INTEGER *pcbSize)
189 URLMoniker *This = MONIKER_THIS(iface);
191 TRACE("(%p,%p)\n",This,pcbSize);
196 pcbSize->QuadPart = sizeof(ULONG) + ((strlenW(This->URLName)+1) * sizeof(WCHAR));
200 static HRESULT WINAPI URLMoniker_BindToObject(IMoniker *iface, IBindCtx* pbc, IMoniker *pmkToLeft,
201 REFIID riid, void **ppv)
203 URLMoniker *This = MONIKER_THIS(iface);
204 IRunningObjectTable *obj_tbl;
207 TRACE("(%p)->(%p,%p,%s,%p): stub\n", This, pbc, pmkToLeft, debugstr_guid(riid), ppv);
209 hres = IBindCtx_GetRunningObjectTable(pbc, &obj_tbl);
210 if(SUCCEEDED(hres)) {
211 FIXME("use running object table\n");
212 IRunningObjectTable_Release(obj_tbl);
215 return bind_to_object(iface, This->URLName, pbc, riid, ppv);
218 static HRESULT WINAPI URLMoniker_BindToStorage(IMoniker* iface, IBindCtx* pbc,
219 IMoniker* pmkToLeft, REFIID riid, void **ppvObject)
221 URLMoniker *This = MONIKER_THIS(iface);
223 TRACE("(%p)->(%p %p %s %p)\n", This, pbc, pmkToLeft, debugstr_guid(riid), ppvObject);
226 FIXME("Unsupported pmkToLeft\n");
228 return bind_to_storage(This->URLName, pbc, riid, ppvObject);
231 static HRESULT WINAPI URLMoniker_Reduce(IMoniker *iface, IBindCtx *pbc,
232 DWORD dwReduceHowFar, IMoniker **ppmkToLeft, IMoniker **ppmkReduced)
234 URLMoniker *This = MONIKER_THIS(iface);
236 TRACE("(%p,%p,%d,%p,%p)\n", This, pbc, dwReduceHowFar, ppmkToLeft, ppmkReduced);
241 IMoniker_AddRef(iface);
242 *ppmkReduced = iface;
243 return MK_S_REDUCED_TO_SELF;
246 static HRESULT WINAPI URLMoniker_ComposeWith(IMoniker *iface, IMoniker *pmkRight,
247 BOOL fOnlyIfNotGeneric, IMoniker **ppmkComposite)
249 URLMoniker *This = MONIKER_THIS(iface);
250 FIXME("(%p)->(%p,%d,%p): stub\n",This,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
254 static HRESULT WINAPI URLMoniker_Enum(IMoniker *iface, BOOL fForward, IEnumMoniker **ppenumMoniker)
256 URLMoniker *This = MONIKER_THIS(iface);
258 TRACE("(%p,%d,%p)\n", This, fForward, ppenumMoniker);
263 /* Does not support sub-monikers */
264 *ppenumMoniker = NULL;
268 static HRESULT WINAPI URLMoniker_IsEqual(IMoniker *iface, IMoniker *pmkOtherMoniker)
270 URLMoniker *This = MONIKER_THIS(iface);
276 TRACE("(%p,%p)\n",This, pmkOtherMoniker);
278 if(pmkOtherMoniker==NULL)
281 IMoniker_GetClassID(pmkOtherMoniker,&clsid);
283 if(!IsEqualCLSID(&clsid,&CLSID_StdURLMoniker))
286 res = CreateBindCtx(0,&bind);
291 if(SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&urlPath))) {
292 int result = lstrcmpiW(urlPath, This->URLName);
293 CoTaskMemFree(urlPath);
297 IUnknown_Release(bind);
302 static HRESULT WINAPI URLMoniker_Hash(IMoniker *iface, DWORD *pdwHash)
304 URLMoniker *This = MONIKER_THIS(iface);
305 int h = 0,i,skip,len;
309 TRACE("(%p,%p)\n",This,pdwHash);
318 for(i = len ; i > 0; i--) {
319 h = (h * 37) + val[off++];
322 /* only sample some characters */
324 for(i = len; i > 0; i -= skip, off += skip) {
325 h = (h * 39) + val[off];
332 static HRESULT WINAPI URLMoniker_IsRunning(IMoniker* iface, IBindCtx* pbc,
333 IMoniker *pmkToLeft, IMoniker *pmkNewlyRunning)
335 URLMoniker *This = MONIKER_THIS(iface);
336 FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,pmkNewlyRunning);
340 static HRESULT WINAPI URLMoniker_GetTimeOfLastChange(IMoniker *iface,
341 IBindCtx *pbc, IMoniker *pmkToLeft, FILETIME *pFileTime)
343 URLMoniker *This = MONIKER_THIS(iface);
344 FIXME("(%p)->(%p,%p,%p): stub\n", This, pbc, pmkToLeft, pFileTime);
348 static HRESULT WINAPI URLMoniker_Inverse(IMoniker *iface, IMoniker **ppmk)
350 URLMoniker *This = MONIKER_THIS(iface);
351 TRACE("(%p,%p)\n",This,ppmk);
352 return MK_E_NOINVERSE;
355 static HRESULT WINAPI URLMoniker_CommonPrefixWith(IMoniker *iface, IMoniker *pmkOther, IMoniker **ppmkPrefix)
357 URLMoniker *This = MONIKER_THIS(iface);
358 FIXME("(%p)->(%p,%p): stub\n",This,pmkOther,ppmkPrefix);
362 static HRESULT WINAPI URLMoniker_RelativePathTo(IMoniker *iface, IMoniker *pmOther, IMoniker **ppmkRelPath)
364 URLMoniker *This = MONIKER_THIS(iface);
365 FIXME("(%p)->(%p,%p): stub\n",This,pmOther,ppmkRelPath);
369 static HRESULT WINAPI URLMoniker_GetDisplayName(IMoniker *iface, IBindCtx *pbc, IMoniker *pmkToLeft,
370 LPOLESTR *ppszDisplayName)
372 URLMoniker *This = MONIKER_THIS(iface);
375 TRACE("(%p,%p,%p,%p)\n", This, pbc, pmkToLeft, ppszDisplayName);
381 return E_OUTOFMEMORY;
383 /* FIXME: If this is a partial URL, try and get a URL moniker from SZ_URLCONTEXT in the bind context,
384 then look at pmkToLeft to try and complete the URL
386 len = lstrlenW(This->URLName)+1;
387 *ppszDisplayName = CoTaskMemAlloc(len*sizeof(WCHAR));
388 if(!*ppszDisplayName)
389 return E_OUTOFMEMORY;
390 lstrcpyW(*ppszDisplayName, This->URLName);
394 static HRESULT WINAPI URLMoniker_ParseDisplayName(IMoniker *iface, IBindCtx *pbc, IMoniker *pmkToLeft,
395 LPOLESTR pszDisplayName, ULONG *pchEaten, IMoniker **ppmkOut)
397 URLMoniker *This = MONIKER_THIS(iface);
398 FIXME("(%p)->(%p,%p,%p,%p,%p): stub\n",This,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
402 static HRESULT WINAPI URLMoniker_IsSystemMoniker(IMoniker *iface, DWORD *pwdMksys)
404 URLMoniker *This = MONIKER_THIS(iface);
406 TRACE("(%p,%p)\n",This,pwdMksys);
411 *pwdMksys = MKSYS_URLMONIKER;
415 static const IMonikerVtbl URLMonikerVtbl =
417 URLMoniker_QueryInterface,
420 URLMoniker_GetClassID,
424 URLMoniker_GetSizeMax,
425 URLMoniker_BindToObject,
426 URLMoniker_BindToStorage,
428 URLMoniker_ComposeWith,
432 URLMoniker_IsRunning,
433 URLMoniker_GetTimeOfLastChange,
435 URLMoniker_CommonPrefixWith,
436 URLMoniker_RelativePathTo,
437 URLMoniker_GetDisplayName,
438 URLMoniker_ParseDisplayName,
439 URLMoniker_IsSystemMoniker
442 static URLMoniker *alloc_moniker(void)
446 ret = heap_alloc(sizeof(URLMoniker));
450 ret->lpIMonikerVtbl = &URLMonikerVtbl;
457 static HRESULT URLMoniker_Init(URLMoniker *This, LPCOLESTR lpszLeftURLName, LPCOLESTR lpszURLName)
462 TRACE("(%p,%s,%s)\n",This,debugstr_w(lpszLeftURLName),debugstr_w(lpszURLName));
464 This->URLName = heap_alloc(INTERNET_MAX_URL_LENGTH*sizeof(WCHAR));
467 hres = CoInternetCombineUrl(lpszLeftURLName, lpszURLName, URL_FILE_USE_PATHURL,
468 This->URLName, INTERNET_MAX_URL_LENGTH, &sizeStr, 0);
470 hres = CoInternetParseUrl(lpszURLName, PARSE_CANONICALIZE, URL_FILE_USE_PATHURL,
471 This->URLName, INTERNET_MAX_URL_LENGTH, &sizeStr, 0);
474 heap_free(This->URLName);
480 if(sizeStr != INTERNET_MAX_URL_LENGTH)
481 This->URLName = heap_realloc(This->URLName, (sizeStr+1)*sizeof(WCHAR));
483 TRACE("URLName = %s\n", debugstr_w(This->URLName));
488 HRESULT StdURLMoniker_Construct(IUnknown *outer, void **ppv)
490 TRACE("(%p %p)\n", outer, ppv);
492 *ppv = alloc_moniker();
493 return *ppv ? S_OK : E_OUTOFMEMORY;
496 /***********************************************************************
497 * CreateURLMonikerEx (URLMON.@)
499 * Create a url moniker.
502 * pmkContext [I] Context
503 * szURL [I] Url to create the moniker for
504 * ppmk [O] Destination for created moniker.
508 * Success: S_OK. ppmk contains the created IMoniker object.
509 * Failure: MK_E_SYNTAX if szURL is not a valid url, or
510 * E_OUTOFMEMORY if memory allocation fails.
512 HRESULT WINAPI CreateURLMonikerEx(IMoniker *pmkContext, LPCWSTR szURL, IMoniker **ppmk, DWORD dwFlags)
516 LPOLESTR lefturl = NULL;
518 TRACE("(%p, %s, %p, %08x)\n", pmkContext, debugstr_w(szURL), ppmk, dwFlags);
520 if (dwFlags & URL_MK_UNIFORM) FIXME("ignoring flag URL_MK_UNIFORM\n");
522 if(!(obj = alloc_moniker()))
523 return E_OUTOFMEMORY;
528 IMoniker_IsSystemMoniker(pmkContext, &dwMksys);
529 if(dwMksys == MKSYS_URLMONIKER && SUCCEEDED(CreateBindCtx(0, &bind))) {
530 IMoniker_GetDisplayName(pmkContext, bind, NULL, &lefturl);
531 TRACE("lefturl = %s\n", debugstr_w(lefturl));
532 IBindCtx_Release(bind);
536 hres = URLMoniker_Init(obj, lefturl, szURL);
537 CoTaskMemFree(lefturl);
539 hres = URLMoniker_QueryInterface((IMoniker*)obj, &IID_IMoniker, (void**)ppmk);
540 IMoniker_Release((IMoniker*)obj);
544 /**********************************************************************
545 * CreateURLMoniker (URLMON.@)
547 * Create a url moniker.
550 * pmkContext [I] Context
551 * szURL [I] Url to create the moniker for
552 * ppmk [O] Destination for created moniker.
555 * Success: S_OK. ppmk contains the created IMoniker object.
556 * Failure: MK_E_SYNTAX if szURL is not a valid url, or
557 * E_OUTOFMEMORY if memory allocation fails.
559 HRESULT WINAPI CreateURLMoniker(IMoniker *pmkContext, LPCWSTR szURL, IMoniker **ppmk)
561 return CreateURLMonikerEx(pmkContext, szURL, ppmk, URL_MK_LEGACY);
564 /***********************************************************************
565 * IsAsyncMoniker (URLMON.@)
567 HRESULT WINAPI IsAsyncMoniker(IMoniker *pmk)
571 TRACE("(%p)\n", pmk);
574 if(SUCCEEDED(IMoniker_QueryInterface(pmk, &IID_IAsyncMoniker, (void**)&am))) {
575 IUnknown_Release(am);
581 /***********************************************************************
582 * BindAsyncMoniker (URLMON.@)
584 * Bind a bind status callback to an asynchronous URL Moniker.
587 * pmk [I] Moniker object to bind status callback to
588 * grfOpt [I] Options, seems not used
589 * pbsc [I] Status callback to bind
590 * iidResult [I] Interface to return
591 * ppvResult [O] Resulting asynchronous moniker object
595 * Failure: E_INVALIDARG, if any argument is invalid, or
596 * E_OUTOFMEMORY if memory allocation fails.
598 HRESULT WINAPI BindAsyncMoniker(IMoniker *pmk, DWORD grfOpt, IBindStatusCallback *pbsc, REFIID iidResult, LPVOID *ppvResult)
601 HRESULT hr = E_INVALIDARG;
603 TRACE("(%p %08x %p %s %p)\n", pmk, grfOpt, pbsc, debugstr_guid(iidResult), ppvResult);
605 if (pmk && ppvResult)
609 hr = CreateAsyncBindCtx(0, pbsc, NULL, &pbc);
612 hr = IMoniker_BindToObject(pmk, pbc, NULL, iidResult, ppvResult);
613 IBindCtx_Release(pbc);
619 /***********************************************************************
620 * MkParseDisplayNameEx (URLMON.@)
622 HRESULT WINAPI MkParseDisplayNameEx(IBindCtx *pbc, LPCWSTR szDisplayName, ULONG *pchEaten, LPMONIKER *ppmk)
624 TRACE("(%p %s %p %p)\n", pbc, debugstr_w(szDisplayName), pchEaten, ppmk);
626 if(is_registered_protocol(szDisplayName)) {
629 hres = CreateURLMoniker(NULL, szDisplayName, ppmk);
630 if(SUCCEEDED(hres)) {
631 *pchEaten = strlenW(szDisplayName);
636 return MkParseDisplayName(pbc, szDisplayName, pchEaten, ppmk);
640 /***********************************************************************
641 * URLDownloadToCacheFileA (URLMON.@)
643 HRESULT WINAPI URLDownloadToCacheFileA(LPUNKNOWN lpUnkCaller, LPCSTR szURL, LPSTR szFileName,
644 DWORD dwBufLength, DWORD dwReserved, LPBINDSTATUSCALLBACK pBSC)
646 LPWSTR url = NULL, file_name = NULL;
650 TRACE("(%p %s %p %d %d %p)\n", lpUnkCaller, debugstr_a(szURL), szFileName,
651 dwBufLength, dwReserved, pBSC);
654 len = MultiByteToWideChar(CP_ACP, 0, szURL, -1, NULL, 0);
655 url = heap_alloc(len*sizeof(WCHAR));
656 MultiByteToWideChar(CP_ACP, 0, szURL, -1, url, len);
660 file_name = heap_alloc(dwBufLength*sizeof(WCHAR));
662 hres = URLDownloadToCacheFileW(lpUnkCaller, url, file_name, dwBufLength*sizeof(WCHAR),
665 if(SUCCEEDED(hres) && file_name)
666 WideCharToMultiByte(CP_ACP, 0, file_name, -1, szFileName, dwBufLength, NULL, NULL);
669 heap_free(file_name);
674 /***********************************************************************
675 * URLDownloadToCacheFileW (URLMON.@)
677 HRESULT WINAPI URLDownloadToCacheFileW(LPUNKNOWN lpUnkCaller, LPCWSTR szURL, LPWSTR szFileName,
678 DWORD dwBufLength, DWORD dwReserved, LPBINDSTATUSCALLBACK pBSC)
680 WCHAR cache_path[MAX_PATH + 1];
681 FILETIME expire, modified;
685 static WCHAR header[] = {
686 'H','T','T','P','/','1','.','0',' ','2','0','0',' ',
687 'O','K','\\','r','\\','n','\\','r','\\','n',0
690 TRACE("(%p, %s, %p, %d, %d, %p)\n", lpUnkCaller, debugstr_w(szURL),
691 szFileName, dwBufLength, dwReserved, pBSC);
693 if (!szURL || !szFileName)
696 ext = PathFindExtensionW(szURL);
698 if (!CreateUrlCacheEntryW(szURL, 0, ext, cache_path, 0))
701 hr = URLDownloadToFileW(lpUnkCaller, szURL, cache_path, 0, pBSC);
705 expire.dwHighDateTime = 0;
706 expire.dwLowDateTime = 0;
707 modified.dwHighDateTime = 0;
708 modified.dwLowDateTime = 0;
710 if (!CommitUrlCacheEntryW(szURL, cache_path, expire, modified, NORMAL_CACHE_ENTRY,
711 header, sizeof(header), NULL, NULL))
714 if (strlenW(cache_path) > dwBufLength)
715 return E_OUTOFMEMORY;
717 lstrcpyW(szFileName, cache_path);
722 /***********************************************************************
723 * HlinkSimpleNavigateToMoniker (URLMON.@)
725 HRESULT WINAPI HlinkSimpleNavigateToMoniker(IMoniker *pmkTarget,
726 LPCWSTR szLocation, LPCWSTR szTargetFrameName, IUnknown *pUnk,
727 IBindCtx *pbc, IBindStatusCallback *pbsc, DWORD grfHLNF, DWORD dwReserved)
733 /***********************************************************************
734 * HlinkSimpleNavigateToString (URLMON.@)
736 HRESULT WINAPI HlinkSimpleNavigateToString( LPCWSTR szTarget,
737 LPCWSTR szLocation, LPCWSTR szTargetFrameName, IUnknown *pUnk,
738 IBindCtx *pbc, IBindStatusCallback *pbsc, DWORD grfHLNF, DWORD dwReserved)
740 FIXME("%s\n", debugstr_w( szTarget ) );
744 /***********************************************************************
745 * HlinkNavigateString (URLMON.@)
747 HRESULT WINAPI HlinkNavigateString( IUnknown *pUnk, LPCWSTR szTarget )
749 TRACE("%p %s\n", pUnk, debugstr_w( szTarget ) );
750 return HlinkSimpleNavigateToString(
751 szTarget, NULL, NULL, pUnk, NULL, NULL, 0, 0 );
754 /***********************************************************************
755 * GetSoftwareUpdateInfo (URLMON.@)
757 HRESULT WINAPI GetSoftwareUpdateInfo( LPCWSTR szDistUnit, LPSOFTDISTINFO psdi )
759 FIXME("%s %p\n", debugstr_w(szDistUnit), psdi );