enum_jobs.c 5.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Queue Manager (BITS) Job Enumerator
 *
 * Copyright 2007 Google (Roy Shea)
 *
 * 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 "qmgr.h"
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(qmgr);

static void EnumBackgroundCopyJobsDestructor(EnumBackgroundCopyJobsImpl *This)
{
28 29 30 31 32 33
    ULONG i;

    for(i = 0; i < This->numJobs; i++)
        IBackgroundCopyJob_Release(This->jobs[i]);

    HeapFree(GetProcessHeap(), 0, This->jobs);
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    HeapFree(GetProcessHeap(), 0, This);
}

static ULONG WINAPI BITS_IEnumBackgroundCopyJobs_AddRef(
    IEnumBackgroundCopyJobs* iface)
{
    EnumBackgroundCopyJobsImpl *This = (EnumBackgroundCopyJobsImpl *) iface;
    return InterlockedIncrement(&This->ref);
}

static HRESULT WINAPI BITS_IEnumBackgroundCopyJobs_QueryInterface(
    IEnumBackgroundCopyJobs* iface,
    REFIID riid,
    void **ppvObject)
{
    EnumBackgroundCopyJobsImpl *This = (EnumBackgroundCopyJobsImpl *) iface;
    TRACE("IID: %s\n", debugstr_guid(riid));

    if (IsEqualGUID(riid, &IID_IUnknown)
        || IsEqualGUID(riid, &IID_IEnumBackgroundCopyJobs))
    {
        *ppvObject = &This->lpVtbl;
        BITS_IEnumBackgroundCopyJobs_AddRef(iface);
        return S_OK;
    }

    *ppvObject = NULL;
    return E_NOINTERFACE;
}

static ULONG WINAPI BITS_IEnumBackgroundCopyJobs_Release(
    IEnumBackgroundCopyJobs* iface)
{
    EnumBackgroundCopyJobsImpl *This = (EnumBackgroundCopyJobsImpl *) iface;
    ULONG ref = InterlockedDecrement(&This->ref);

    if (ref == 0)
        EnumBackgroundCopyJobsDestructor(This);

    return ref;
}

static HRESULT WINAPI BITS_IEnumBackgroundCopyJobs_Next(
    IEnumBackgroundCopyJobs* iface,
    ULONG celt,
    IBackgroundCopyJob **rgelt,
    ULONG *pceltFetched)
{
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    EnumBackgroundCopyJobsImpl *This = (EnumBackgroundCopyJobsImpl *) iface;
    ULONG fetched;
    ULONG i;
    IBackgroundCopyJob *job;

    fetched = min(celt, This->numJobs - This->indexJobs);
    if (pceltFetched)
        *pceltFetched = fetched;
    else
    {
        /* We need to initialize this array if the caller doesn't request
           the length because length_is will default to celt.  */
        for (i = 0; i < celt; ++i)
            rgelt[i] = NULL;

        /* pceltFetched can only be NULL if celt is 1 */
        if (celt != 1)
            return E_INVALIDARG;
    }

    /* Fill in the array of objects */
    for (i = 0; i < fetched; ++i)
    {
        job = This->jobs[This->indexJobs++];
        IBackgroundCopyJob_AddRef(job);
        rgelt[i] = job;
    }

    return fetched == celt ? S_OK : S_FALSE;
111 112 113 114 115 116
}

static HRESULT WINAPI BITS_IEnumBackgroundCopyJobs_Skip(
    IEnumBackgroundCopyJobs* iface,
    ULONG celt)
{
117 118 119 120 121 122 123 124 125 126
    EnumBackgroundCopyJobsImpl *This = (EnumBackgroundCopyJobsImpl *) iface;

    if (This->numJobs - This->indexJobs < celt)
    {
        This->indexJobs = This->numJobs;
        return S_FALSE;
    }

    This->indexJobs += celt;
    return S_OK;
127 128 129 130 131
}

static HRESULT WINAPI BITS_IEnumBackgroundCopyJobs_Reset(
    IEnumBackgroundCopyJobs* iface)
{
132 133 134
    EnumBackgroundCopyJobsImpl *This = (EnumBackgroundCopyJobsImpl *) iface;
    This->indexJobs = 0;
    return S_OK;
135 136 137 138 139 140 141 142 143 144 145 146 147 148
}

static HRESULT WINAPI BITS_IEnumBackgroundCopyJobs_Clone(
    IEnumBackgroundCopyJobs* iface,
    IEnumBackgroundCopyJobs **ppenum)
{
    FIXME("Not implemented\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI BITS_IEnumBackgroundCopyJobs_GetCount(
    IEnumBackgroundCopyJobs* iface,
    ULONG *puCount)
{
149 150 151
    EnumBackgroundCopyJobsImpl *This = (EnumBackgroundCopyJobsImpl *) iface;
    *puCount = This->numJobs;
    return S_OK;
152 153 154 155 156 157 158 159 160 161 162 163 164
}

static const IEnumBackgroundCopyJobsVtbl BITS_IEnumBackgroundCopyJobs_Vtbl =
{
    BITS_IEnumBackgroundCopyJobs_QueryInterface,
    BITS_IEnumBackgroundCopyJobs_AddRef,
    BITS_IEnumBackgroundCopyJobs_Release,
    BITS_IEnumBackgroundCopyJobs_Next,
    BITS_IEnumBackgroundCopyJobs_Skip,
    BITS_IEnumBackgroundCopyJobs_Reset,
    BITS_IEnumBackgroundCopyJobs_Clone,
    BITS_IEnumBackgroundCopyJobs_GetCount
};
165 166 167 168

HRESULT EnumBackgroundCopyJobsConstructor(LPVOID *ppObj,
                                          IBackgroundCopyManager* copyManager)
{
169
    BackgroundCopyManagerImpl *qmgr = (BackgroundCopyManagerImpl *) copyManager;
170
    EnumBackgroundCopyJobsImpl *This;
171 172
    BackgroundCopyJobImpl *job;
    ULONG i;
173 174 175 176 177 178 179 180 181

    TRACE("%p, %p)\n", ppObj, copyManager);

    This = HeapAlloc(GetProcessHeap(), 0, sizeof *This);
    if (!This)
        return E_OUTOFMEMORY;
    This->lpVtbl = &BITS_IEnumBackgroundCopyJobs_Vtbl;
    This->ref = 1;

182 183
    /* Create array of jobs */
    This->indexJobs = 0;
184 185

    EnterCriticalSection(&qmgr->cs);
186 187 188 189 190 191 192 193
    This->numJobs = list_count(&qmgr->jobs);

    if (0 < This->numJobs)
    {
        This->jobs = HeapAlloc(GetProcessHeap(), 0,
                               This->numJobs * sizeof *This->jobs);
        if (!This->jobs)
        {
194
            LeaveCriticalSection(&qmgr->cs);
195 196 197 198 199 200 201 202 203 204 205 206 207 208
            HeapFree(GetProcessHeap(), 0, This);
            return E_OUTOFMEMORY;
        }
    }
    else
        This->jobs = NULL;

    i = 0;
    LIST_FOR_EACH_ENTRY(job, &qmgr->jobs, BackgroundCopyJobImpl, entryFromQmgr)
    {
        IBackgroundCopyJob *iJob = (IBackgroundCopyJob *) job;
        IBackgroundCopyJob_AddRef(iJob);
        This->jobs[i++] = iJob;
    }
209
    LeaveCriticalSection(&qmgr->cs);
210

211 212 213
    *ppObj = &This->lpVtbl;
    return S_OK;
}