mk.c 11.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright 2007 Jacek Caban for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "urlmon_main.h"
#include "wine/debug.h"

22 23 24
#define NO_SHLWAPI_REG
#include "shlwapi.h"

25 26 27
WINE_DEFAULT_DEBUG_CHANNEL(urlmon);

typedef struct {
28
    IUnknown            IUnknown_inner;
29
    IInternetProtocolEx IInternetProtocolEx_iface;
30

31
    LONG ref;
32
    IUnknown *outer;
33 34

    IStream *stream;
35 36
} MkProtocol;

37 38
static inline MkProtocol *impl_from_IUnknown(IUnknown *iface)
{
39
    return CONTAINING_RECORD(iface, MkProtocol, IUnknown_inner);
40 41
}

42
static inline MkProtocol *impl_from_IInternetProtocolEx(IInternetProtocolEx *iface)
43
{
44
    return CONTAINING_RECORD(iface, MkProtocol, IInternetProtocolEx_iface);
45
}
46

47
static HRESULT WINAPI MkProtocolUnk_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
48
{
49
    MkProtocol *This = impl_from_IUnknown(iface);
50 51 52

    if(IsEqualGUID(&IID_IUnknown, riid)) {
        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
53
        *ppv = &This->IUnknown_inner;
54 55
    }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
        TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
56
        *ppv = &This->IInternetProtocolEx_iface;
57 58
    }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
        TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
59 60 61 62
        *ppv = &This->IInternetProtocolEx_iface;
    }else if(IsEqualGUID(&IID_IInternetProtocolEx, riid)) {
        TRACE("(%p)->(IID_IInternetProtocolEx %p)\n", This, ppv);
        *ppv = &This->IInternetProtocolEx_iface;
63 64 65 66
    }else {
        *ppv = NULL;
        WARN("not supported interface %s\n", debugstr_guid(riid));
        return E_NOINTERFACE;
67 68
    }

69 70
    IUnknown_AddRef((IUnknown*)*ppv);
    return S_OK;
71 72
}

73
static ULONG WINAPI MkProtocolUnk_AddRef(IUnknown *iface)
74
{
75
    MkProtocol *This = impl_from_IUnknown(iface);
76
    LONG ref = InterlockedIncrement(&This->ref);
77
    TRACE("(%p) ref=%ld\n", This, ref);
78 79 80
    return ref;
}

81
static ULONG WINAPI MkProtocolUnk_Release(IUnknown *iface)
82
{
83
    MkProtocol *This = impl_from_IUnknown(iface);
84 85
    LONG ref = InterlockedDecrement(&This->ref);

86
    TRACE("(%p) ref=%ld\n", This, ref);
87 88

    if(!ref) {
89 90 91
        if(This->stream)
            IStream_Release(This->stream);

92
        free(This);
93 94 95 96 97 98 99

        URLMON_UnlockModule();
    }

    return ref;
}

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
static const IUnknownVtbl MkProtocolUnkVtbl = {
    MkProtocolUnk_QueryInterface,
    MkProtocolUnk_AddRef,
    MkProtocolUnk_Release
};

static HRESULT WINAPI MkProtocol_QueryInterface(IInternetProtocolEx *iface, REFIID riid, void **ppv)
{
    MkProtocol *This = impl_from_IInternetProtocolEx(iface);
    TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
    return IUnknown_QueryInterface(This->outer, riid, ppv);
}

static ULONG WINAPI MkProtocol_AddRef(IInternetProtocolEx *iface)
{
    MkProtocol *This = impl_from_IInternetProtocolEx(iface);
    TRACE("(%p)\n", This);
    return IUnknown_AddRef(This->outer);
}

static ULONG WINAPI MkProtocol_Release(IInternetProtocolEx *iface)
{
    MkProtocol *This = impl_from_IInternetProtocolEx(iface);
    TRACE("(%p)\n", This);
    return IUnknown_Release(This->outer);
}

127 128 129 130 131 132
static HRESULT report_result(IInternetProtocolSink *sink, HRESULT hres, DWORD dwError)
{
    IInternetProtocolSink_ReportResult(sink, hres, dwError, NULL);
    return hres;
}

133
static HRESULT WINAPI MkProtocol_Start(IInternetProtocolEx *iface, LPCWSTR szUrl,
134
        IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
135
        DWORD grfPI, HANDLE_PTR dwReserved)
136
{
137 138 139 140
    MkProtocol *This = impl_from_IInternetProtocolEx(iface);
    HRESULT hres;
    IUri *uri;

141
    TRACE("(%p)->(%s %p %p %08lx %Ix)\n", This, debugstr_w(szUrl), pOIProtSink,
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
            pOIBindInfo, grfPI, dwReserved);

    hres = CreateUri(szUrl, 0, 0, &uri);
    if(FAILED(hres))
        return hres;

    hres = IInternetProtocolEx_StartEx(&This->IInternetProtocolEx_iface, uri, pOIProtSink,
            pOIBindInfo, grfPI, (HANDLE*)dwReserved);

    IUri_Release(uri);
    return hres;
}

static HRESULT WINAPI MkProtocol_Continue(IInternetProtocolEx *iface, PROTOCOLDATA *pProtocolData)
{
    MkProtocol *This = impl_from_IInternetProtocolEx(iface);
    FIXME("(%p)->(%p)\n", This, pProtocolData);
    return E_NOTIMPL;
}

static HRESULT WINAPI MkProtocol_Abort(IInternetProtocolEx *iface, HRESULT hrReason,
        DWORD dwOptions)
{
    MkProtocol *This = impl_from_IInternetProtocolEx(iface);
166
    FIXME("(%p)->(%08lx %08lx)\n", This, hrReason, dwOptions);
167 168 169 170 171 172 173
    return E_NOTIMPL;
}

static HRESULT WINAPI MkProtocol_Terminate(IInternetProtocolEx *iface, DWORD dwOptions)
{
    MkProtocol *This = impl_from_IInternetProtocolEx(iface);

174
    TRACE("(%p)->(%08lx)\n", This, dwOptions);
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

    return S_OK;
}

static HRESULT WINAPI MkProtocol_Suspend(IInternetProtocolEx *iface)
{
    MkProtocol *This = impl_from_IInternetProtocolEx(iface);
    FIXME("(%p)\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI MkProtocol_Resume(IInternetProtocolEx *iface)
{
    MkProtocol *This = impl_from_IInternetProtocolEx(iface);
    FIXME("(%p)\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI MkProtocol_Read(IInternetProtocolEx *iface, void *pv,
        ULONG cb, ULONG *pcbRead)
{
    MkProtocol *This = impl_from_IInternetProtocolEx(iface);

198
    TRACE("(%p)->(%p %lu %p)\n", This, pv, cb, pcbRead);
199 200 201 202 203 204 205 206 207 208 209

    if(!This->stream)
        return E_FAIL;

    return IStream_Read(This->stream, pv, cb, pcbRead);
}

static HRESULT WINAPI MkProtocol_Seek(IInternetProtocolEx *iface, LARGE_INTEGER dlibMove,
        DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
{
    MkProtocol *This = impl_from_IInternetProtocolEx(iface);
210
    FIXME("(%p)->(%ld %ld %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
211 212 213 214 215 216 217
    return E_NOTIMPL;
}

static HRESULT WINAPI MkProtocol_LockRequest(IInternetProtocolEx *iface, DWORD dwOptions)
{
    MkProtocol *This = impl_from_IInternetProtocolEx(iface);

218
    TRACE("(%p)->(%08lx)\n", This, dwOptions);
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

    return S_OK;
}

static HRESULT WINAPI MkProtocol_UnlockRequest(IInternetProtocolEx *iface)
{
    MkProtocol *This = impl_from_IInternetProtocolEx(iface);

    TRACE("(%p)\n", This);

    return S_OK;
}

static HRESULT WINAPI MkProtocol_StartEx(IInternetProtocolEx *iface, IUri *pUri,
        IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
        DWORD grfPI, HANDLE *dwReserved)
{
    MkProtocol *This = impl_from_IInternetProtocolEx(iface);
    LPWSTR mime, progid, display_name, colon_ptr;
    DWORD bindf=0, eaten=0, scheme=0, len;
239
    BSTR url, path = NULL;
240 241 242
    IParseDisplayName *pdn;
    BINDINFO bindinfo;
    STATSTG statstg;
243
    IMoniker *mon;
244
    HRESULT hres;
245
    CLSID clsid;
246

247
    TRACE("(%p)->(%p %p %p %08lx %p)\n", This, pUri, pOIProtSink,
248
            pOIBindInfo, grfPI, dwReserved);
249

250 251 252 253
    hres = IUri_GetScheme(pUri, &scheme);
    if(FAILED(hres))
        return hres;
    if(scheme != URL_SCHEME_MK)
254 255
        return INET_E_INVALID_URL;

256 257 258 259
    memset(&bindinfo, 0, sizeof(bindinfo));
    bindinfo.cbSize = sizeof(BINDINFO);
    hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &bindf, &bindinfo);
    if(FAILED(hres)) {
260
        WARN("GetBindInfo failed: %08lx\n", hres);
261 262 263 264 265 266 267
        return hres;
    }

    ReleaseBindInfo(&bindinfo);

    IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST, NULL);

268 269 270 271 272
    hres = IUri_GetDisplayUri(pUri, &url);
    if(FAILED(hres))
        return hres;
    hres = FindMimeFromData(NULL, url, NULL, 0, NULL, 0, &mime, 0);
    SysFreeString(url);
273 274 275 276 277
    if(SUCCEEDED(hres)) {
        IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, mime);
        CoTaskMemFree(mime);
    }

278
    hres = IUri_GetPath(pUri, &path);
279 280
    if(FAILED(hres))
        return hres;
281 282 283 284
    len = SysStringLen(path)+1;
    hres = UrlUnescapeW(path, NULL, &len, URL_UNESCAPE_INPLACE);
    if (FAILED(hres)) {
        SysFreeString(path);
285 286
        return report_result(pOIProtSink, INET_E_RESOURCE_NOT_FOUND, ERROR_INVALID_PARAMETER);
    }
287

288
    progid = path+1; /* skip '@' symbol */
289
    colon_ptr = wcschr(path, ':');
290 291
    if(!colon_ptr) {
        SysFreeString(path);
292
        return report_result(pOIProtSink, INET_E_RESOURCE_NOT_FOUND, ERROR_INVALID_PARAMETER);
293 294
    }

295
    len = lstrlenW(path);
296
    display_name = malloc((len + 1) * sizeof(WCHAR));
297
    memcpy(display_name, path, (len+1)*sizeof(WCHAR));
298

299
    progid[colon_ptr-progid] = 0; /* overwrite ':' with NULL terminator */
300
    hres = CLSIDFromProgID(progid, &clsid);
301
    SysFreeString(path);
302
    if(FAILED(hres))
303
    {
304
        free(display_name);
305
        return report_result(pOIProtSink, INET_E_RESOURCE_NOT_FOUND, ERROR_INVALID_PARAMETER);
306
    }
307 308 309 310 311

    hres = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
            &IID_IParseDisplayName, (void**)&pdn);
    if(FAILED(hres)) {
        WARN("Could not create object %s\n", debugstr_guid(&clsid));
312
        free(display_name);
313 314 315 316
        return report_result(pOIProtSink, hres, ERROR_INVALID_PARAMETER);
    }

    hres = IParseDisplayName_ParseDisplayName(pdn, NULL /* FIXME */, display_name, &eaten, &mon);
317
    free(display_name);
318 319
    IParseDisplayName_Release(pdn);
    if(FAILED(hres)) {
320
        WARN("ParseDisplayName failed: %08lx\n", hres);
321 322 323 324 325 326 327 328 329 330 331
        return report_result(pOIProtSink, hres, ERROR_INVALID_PARAMETER);
    }

    if(This->stream) {
        IStream_Release(This->stream);
        This->stream = NULL;
    }

    hres = IMoniker_BindToStorage(mon, NULL /* FIXME */, NULL, &IID_IStream, (void**)&This->stream);
    IMoniker_Release(mon);
    if(FAILED(hres)) {
332
        WARN("BindToStorage failed: %08lx\n", hres);
333 334 335 336 337
        return report_result(pOIProtSink, hres, ERROR_INVALID_PARAMETER);
    }

    hres = IStream_Stat(This->stream, &statstg, STATFLAG_NONAME);
    if(FAILED(hres)) {
338
        WARN("Stat failed: %08lx\n", hres);
339 340 341 342 343 344 345
        return report_result(pOIProtSink, hres, ERROR_INVALID_PARAMETER);
    }

    IInternetProtocolSink_ReportData(pOIProtSink,
            BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION,
            statstg.cbSize.u.LowPart, statstg.cbSize.u.LowPart);
    return report_result(pOIProtSink, S_OK, ERROR_SUCCESS);
346 347
}

348
static const IInternetProtocolExVtbl MkProtocolVtbl = {
349 350 351 352 353 354 355 356 357 358 359 360
    MkProtocol_QueryInterface,
    MkProtocol_AddRef,
    MkProtocol_Release,
    MkProtocol_Start,
    MkProtocol_Continue,
    MkProtocol_Abort,
    MkProtocol_Terminate,
    MkProtocol_Suspend,
    MkProtocol_Resume,
    MkProtocol_Read,
    MkProtocol_Seek,
    MkProtocol_LockRequest,
361 362
    MkProtocol_UnlockRequest,
    MkProtocol_StartEx
363 364
};

365
HRESULT MkProtocol_Construct(IUnknown *outer, void **ppv)
366 367 368
{
    MkProtocol *ret;

369
    TRACE("(%p %p)\n", outer, ppv);
370 371 372

    URLMON_LockModule();

373
    ret = malloc(sizeof(MkProtocol));
374

375
    ret->IUnknown_inner.lpVtbl = &MkProtocolUnkVtbl;
376
    ret->IInternetProtocolEx_iface.lpVtbl = &MkProtocolVtbl;
377
    ret->ref = 1;
378
    ret->outer = outer ? outer : &ret->IUnknown_inner;
379
    ret->stream = NULL;
380 381

    /* NOTE:
382
     * Native returns NULL ppobj and S_OK in CreateInstance if called with IID_IUnknown riid and no outer.
383
     */
384
    *ppv = &ret->IUnknown_inner;
385 386
    return S_OK;
}