qmgr.c 6.01 KB
Newer Older
1 2 3
/*
 * Queue Manager (BITS) core functions
 *
4
 * Copyright 2007, 2008 Google (Roy Shea, Dan Hipschman)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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);

26
BackgroundCopyManagerImpl globalMgr;
27

28
static HRESULT WINAPI BackgroundCopyManager_QueryInterface(IBackgroundCopyManager *iface,
29
        REFIID riid, void **ppv)
30
{
31
    TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
32

33
    if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_IBackgroundCopyManager))
34
    {
35 36
        *ppv = iface;
        IBackgroundCopyManager_AddRef(iface);
37 38 39
        return S_OK;
    }

40
    *ppv = NULL;
41 42 43
    return E_NOINTERFACE;
}

44
static ULONG WINAPI BackgroundCopyManager_AddRef(IBackgroundCopyManager *iface)
45 46 47 48
{
    return 2;
}

49
static ULONG WINAPI BackgroundCopyManager_Release(IBackgroundCopyManager *iface)
50
{
51
    return 1;
52 53 54 55
}

/*** IBackgroundCopyManager interface methods ***/

56
static HRESULT WINAPI BackgroundCopyManager_CreateJob(IBackgroundCopyManager *iface,
57
        LPCWSTR DisplayName, BG_JOB_TYPE Type, GUID *pJobId, IBackgroundCopyJob **ppJob)
58
{
59 60
    BackgroundCopyJobImpl *job;
    HRESULT hres;
61 62

    TRACE("(%s %d %p %p)\n", debugstr_w(DisplayName), Type, pJobId, ppJob);
63

64
    hres = BackgroundCopyJobConstructor(DisplayName, Type, pJobId, &job);
65 66 67 68
    if (FAILED(hres))
        return hres;

    /* Add a reference to the job to job list */
69
    *ppJob = (IBackgroundCopyJob *)&job->IBackgroundCopyJob4_iface;
70
    IBackgroundCopyJob_AddRef(*ppJob);
71 72 73
    EnterCriticalSection(&globalMgr.cs);
    list_add_head(&globalMgr.jobs, &job->entryFromQmgr);
    LeaveCriticalSection(&globalMgr.cs);
74
    return S_OK;
75 76
}

77
static HRESULT WINAPI BackgroundCopyManager_GetJob(IBackgroundCopyManager *iface,
78
        REFGUID jobID, IBackgroundCopyJob **job)
79
{
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    BackgroundCopyManagerImpl *qmgr = &globalMgr;
    HRESULT hr = BG_E_NOT_FOUND;
    BackgroundCopyJobImpl *cur;

    TRACE("(%s %p)\n", debugstr_guid(jobID), job);

    if (!job || !jobID) return E_INVALIDARG;

    *job = NULL;

    EnterCriticalSection(&qmgr->cs);

    LIST_FOR_EACH_ENTRY(cur, &qmgr->jobs, BackgroundCopyJobImpl, entryFromQmgr)
    {
        if (IsEqualGUID(&cur->jobId, jobID))
        {
96 97
            *job = (IBackgroundCopyJob *)&cur->IBackgroundCopyJob4_iface;
            IBackgroundCopyJob_AddRef(*job);
98 99 100 101 102 103 104 105
            hr = S_OK;
            break;
        }
    }

    LeaveCriticalSection(&qmgr->cs);

    return hr;
106 107
}

108 109
static HRESULT WINAPI BackgroundCopyManager_EnumJobs(IBackgroundCopyManager *iface,
        DWORD flags, IEnumBackgroundCopyJobs **ppEnum)
110
{
111
    TRACE("(0x%lx %p)\n", flags, ppEnum);
112
    return enum_copy_job_create(&globalMgr, ppEnum);
113 114
}

115 116
static HRESULT WINAPI BackgroundCopyManager_GetErrorDescription(IBackgroundCopyManager *iface,
        HRESULT hr, DWORD langid, LPWSTR *error_description)
117
{
118
    FIXME("(0x%08lx 0x%lx %p): stub\n", hr, langid, error_description);
119 120 121
    return E_NOTIMPL;
}

122
static const IBackgroundCopyManagerVtbl BackgroundCopyManagerVtbl =
123
{
124 125 126 127 128 129 130
    BackgroundCopyManager_QueryInterface,
    BackgroundCopyManager_AddRef,
    BackgroundCopyManager_Release,
    BackgroundCopyManager_CreateJob,
    BackgroundCopyManager_GetJob,
    BackgroundCopyManager_EnumJobs,
    BackgroundCopyManager_GetErrorDescription
131 132
};

133
BackgroundCopyManagerImpl globalMgr = {
134
    { &BackgroundCopyManagerVtbl },
135
    { NULL, -1, 0, 0, 0, 0 },
136
    NULL,
137 138 139
    LIST_INIT(globalMgr.jobs)
};

140
/* Constructor for instances of background copy manager */
141
HRESULT BackgroundCopyManagerConstructor(LPVOID *ppObj)
142
{
143
    TRACE("(%p)\n", ppObj);
144
    *ppObj = &globalMgr;
145 146
    return S_OK;
}
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

DWORD WINAPI fileTransfer(void *param)
{
    BackgroundCopyManagerImpl *qmgr = &globalMgr;
    HANDLE events[2];

    events[0] = stop_event;
    events[1] = qmgr->jobEvent;

    for (;;)
    {
        BackgroundCopyJobImpl *job, *jobCur;
        BOOL haveJob = FALSE;

        /* Check if it's the stop_event */
        if (WaitForMultipleObjects(2, events, FALSE, INFINITE) == WAIT_OBJECT_0)
Rob Shearman's avatar
Rob Shearman committed
163 164 165 166
        {
            LIST_FOR_EACH_ENTRY_SAFE(job, jobCur, &qmgr->jobs, BackgroundCopyJobImpl, entryFromQmgr)
            {
                list_remove(&job->entryFromQmgr);
167
                IBackgroundCopyJob4_Release(&job->IBackgroundCopyJob4_iface);
Rob Shearman's avatar
Rob Shearman committed
168
            }
169
            return 0;
Rob Shearman's avatar
Rob Shearman committed
170
        }
171 172 173 174 175 176 177 178 179 180 181

        /* Note that other threads may add files to the job list, but only
           this thread ever deletes them so we don't need to worry about jobs
           magically disappearing from the list.  */
        EnterCriticalSection(&qmgr->cs);

        LIST_FOR_EACH_ENTRY_SAFE(job, jobCur, &qmgr->jobs, BackgroundCopyJobImpl, entryFromQmgr)
        {
            if (job->state == BG_JOB_STATE_ACKNOWLEDGED || job->state == BG_JOB_STATE_CANCELLED)
            {
                list_remove(&job->entryFromQmgr);
182
                IBackgroundCopyJob4_Release(&job->IBackgroundCopyJob4_iface);
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
            }
            else if (job->state == BG_JOB_STATE_QUEUED)
            {
                haveJob = TRUE;
                break;
            }
            else if (job->state == BG_JOB_STATE_CONNECTING
                     || job->state == BG_JOB_STATE_TRANSFERRING)
            {
                ERR("Invalid state for job %p: %d\n", job, job->state);
            }
        }

        if (!haveJob)
            ResetEvent(qmgr->jobEvent);

        LeaveCriticalSection(&qmgr->cs);

        if (haveJob)
202
            processJob(job);
203 204
    }
}