include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / dlls / qmgr / job.c
blobd3b2675619813c4a55069f279b8e3b6503ff9c3c
1 /*
2 * Background Copy Job Interface for BITS
4 * Copyright 2007 Google (Roy Shea)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "qmgr.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(qmgr);
30 BOOL transitionJobState(BackgroundCopyJobImpl *job, BG_JOB_STATE from, BG_JOB_STATE to)
32 BOOL ret = FALSE;
34 EnterCriticalSection(&globalMgr.cs);
35 if (job->state == from)
37 job->state = to;
38 ret = TRUE;
40 LeaveCriticalSection(&globalMgr.cs);
41 return ret;
44 struct copy_error
46 IBackgroundCopyError IBackgroundCopyError_iface;
47 LONG refs;
48 BG_ERROR_CONTEXT context;
49 HRESULT code;
50 IBackgroundCopyFile2 *file;
53 static inline struct copy_error *impl_from_IBackgroundCopyError(IBackgroundCopyError *iface)
55 return CONTAINING_RECORD(iface, struct copy_error, IBackgroundCopyError_iface);
58 static HRESULT WINAPI copy_error_QueryInterface(
59 IBackgroundCopyError *iface,
60 REFIID riid,
61 void **obj)
63 struct copy_error *error = impl_from_IBackgroundCopyError(iface);
65 TRACE("(%p)->(%s %p)\n", error, debugstr_guid(riid), obj);
67 if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_IBackgroundCopyError))
69 *obj = &error->IBackgroundCopyError_iface;
71 else
73 *obj = NULL;
74 WARN("interface %s not supported\n", debugstr_guid(riid));
75 return E_NOINTERFACE;
78 IBackgroundCopyError_AddRef(iface);
79 return S_OK;
82 static ULONG WINAPI copy_error_AddRef(
83 IBackgroundCopyError *iface)
85 struct copy_error *error = impl_from_IBackgroundCopyError(iface);
86 LONG refs = InterlockedIncrement(&error->refs);
87 TRACE("(%p)->(%ld)\n", error, refs);
88 return refs;
91 static ULONG WINAPI copy_error_Release(
92 IBackgroundCopyError *iface)
94 struct copy_error *error = impl_from_IBackgroundCopyError(iface);
95 LONG refs = InterlockedDecrement(&error->refs);
97 TRACE("(%p)->(%ld)\n", error, refs);
99 if (!refs)
101 if (error->file) IBackgroundCopyFile2_Release(error->file);
102 free(error);
104 return refs;
107 static HRESULT WINAPI copy_error_GetError(
108 IBackgroundCopyError *iface,
109 BG_ERROR_CONTEXT *pContext,
110 HRESULT *pCode)
112 struct copy_error *error = impl_from_IBackgroundCopyError(iface);
114 TRACE("(%p)->(%p %p)\n", error, pContext, pCode);
116 *pContext = error->context;
117 *pCode = error->code;
119 TRACE("returning context %u error code 0x%08lx\n", error->context, error->code);
120 return S_OK;
123 static HRESULT WINAPI copy_error_GetFile(
124 IBackgroundCopyError *iface,
125 IBackgroundCopyFile **pVal)
127 struct copy_error *error = impl_from_IBackgroundCopyError(iface);
129 TRACE("(%p)->(%p)\n", error, pVal);
131 if (error->file)
133 IBackgroundCopyFile2_AddRef(error->file);
134 *pVal = (IBackgroundCopyFile *)error->file;
135 return S_OK;
137 *pVal = NULL;
138 return BG_E_FILE_NOT_AVAILABLE;
141 static HRESULT WINAPI copy_error_GetErrorDescription(
142 IBackgroundCopyError *iface,
143 DWORD LanguageId,
144 LPWSTR *pErrorDescription)
146 struct copy_error *error = impl_from_IBackgroundCopyError(iface);
147 FIXME("(%p)->(%p)\n", error, pErrorDescription);
148 return E_NOTIMPL;
151 static HRESULT WINAPI copy_error_GetErrorContextDescription(
152 IBackgroundCopyError *iface,
153 DWORD LanguageId,
154 LPWSTR *pContextDescription)
156 struct copy_error *error = impl_from_IBackgroundCopyError(iface);
157 FIXME("(%p)->(%p)\n", error, pContextDescription);
158 return E_NOTIMPL;
161 static HRESULT WINAPI copy_error_GetProtocol(
162 IBackgroundCopyError *iface,
163 LPWSTR *pProtocol)
165 struct copy_error *error = impl_from_IBackgroundCopyError(iface);
166 FIXME("(%p)->(%p)\n", error, pProtocol);
167 return E_NOTIMPL;
170 static const IBackgroundCopyErrorVtbl copy_error_vtbl =
172 copy_error_QueryInterface,
173 copy_error_AddRef,
174 copy_error_Release,
175 copy_error_GetError,
176 copy_error_GetFile,
177 copy_error_GetErrorDescription,
178 copy_error_GetErrorContextDescription,
179 copy_error_GetProtocol
182 static HRESULT create_copy_error(
183 BG_ERROR_CONTEXT context,
184 HRESULT code,
185 IBackgroundCopyFile2 *file,
186 IBackgroundCopyError **obj)
188 struct copy_error *error;
190 TRACE("context %u code %08lx file %p\n", context, code, file);
192 if (!(error = malloc(sizeof(*error) ))) return E_OUTOFMEMORY;
193 error->IBackgroundCopyError_iface.lpVtbl = &copy_error_vtbl;
194 error->refs = 1;
195 error->context = context;
196 error->code = code;
197 error->file = file;
198 if (error->file) IBackgroundCopyFile2_AddRef(error->file);
200 *obj = &error->IBackgroundCopyError_iface;
201 TRACE("returning iface %p\n", *obj);
202 return S_OK;
205 static inline BOOL is_job_done(const BackgroundCopyJobImpl *job)
207 return job->state == BG_JOB_STATE_CANCELLED || job->state == BG_JOB_STATE_ACKNOWLEDGED;
210 static inline BackgroundCopyJobImpl *impl_from_IBackgroundCopyJob4(IBackgroundCopyJob4 *iface)
212 return CONTAINING_RECORD(iface, BackgroundCopyJobImpl, IBackgroundCopyJob4_iface);
215 static HRESULT WINAPI BackgroundCopyJob_QueryInterface(IBackgroundCopyJob4 *iface, REFIID riid, void **obj)
217 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJob4(iface);
219 TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), obj);
221 if (IsEqualGUID(riid, &IID_IUnknown) ||
222 IsEqualGUID(riid, &IID_IBackgroundCopyJob) ||
223 IsEqualGUID(riid, &IID_IBackgroundCopyJob2) ||
224 IsEqualGUID(riid, &IID_IBackgroundCopyJob3) ||
225 IsEqualGUID(riid, &IID_IBackgroundCopyJob4))
227 *obj = &job->IBackgroundCopyJob4_iface;
229 else if (IsEqualGUID(riid, &IID_IBackgroundCopyJobHttpOptions))
231 *obj = &job->IBackgroundCopyJobHttpOptions_iface;
233 else
235 WARN("Unsupported interface %s.\n", debugstr_guid(riid));
236 *obj = NULL;
237 return E_NOINTERFACE;
240 IBackgroundCopyJob4_AddRef(iface);
241 return S_OK;
244 static ULONG WINAPI BackgroundCopyJob_AddRef(IBackgroundCopyJob4 *iface)
246 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJob4(iface);
247 ULONG refcount = InterlockedIncrement(&job->ref);
248 TRACE("%p, refcount %ld.\n", iface, refcount);
249 return refcount;
252 static ULONG WINAPI BackgroundCopyJob_Release(IBackgroundCopyJob4 *iface)
254 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJob4(iface);
255 ULONG i, j, ref = InterlockedDecrement(&job->ref);
257 TRACE("%p, refcount %ld.\n", iface, ref);
259 if (!ref)
261 job->cs.DebugInfo->Spare[0] = 0;
262 DeleteCriticalSection(&job->cs);
263 if (job->callback)
264 IBackgroundCopyCallback2_Release(job->callback);
265 free(job->displayName);
266 free(job->description);
267 free(job->http_options.headers);
268 for (i = 0; i < BG_AUTH_TARGET_PROXY; i++)
270 for (j = 0; j < BG_AUTH_SCHEME_PASSPORT; j++)
272 BG_AUTH_CREDENTIALS *cred = &job->http_options.creds[i][j];
273 free(cred->Credentials.Basic.UserName);
274 free(cred->Credentials.Basic.Password);
277 CloseHandle(job->wait);
278 CloseHandle(job->cancel);
279 CloseHandle(job->done);
280 free(job);
283 return ref;
286 /*** IBackgroundCopyJob methods ***/
288 static HRESULT WINAPI BackgroundCopyJob_AddFileSet(IBackgroundCopyJob4 *iface, ULONG cFileCount, BG_FILE_INFO *pFileSet)
290 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJob4(iface);
291 HRESULT hr = S_OK;
292 ULONG i;
294 TRACE("%p, %lu, %p.\n", iface, cFileCount, pFileSet);
296 EnterCriticalSection(&job->cs);
298 for (i = 0; i < cFileCount; ++i)
300 BackgroundCopyFileImpl *file;
302 /* We should return E_INVALIDARG in these cases. */
303 FIXME("Check for valid filenames and supported protocols\n");
305 hr = BackgroundCopyFileConstructor(job, pFileSet[i].RemoteName, pFileSet[i].LocalName, &file);
306 if (hr != S_OK) break;
308 /* Add a reference to the file to file list */
309 list_add_head(&job->files, &file->entryFromJob);
310 job->jobProgress.BytesTotal = BG_SIZE_UNKNOWN;
311 ++job->jobProgress.FilesTotal;
314 LeaveCriticalSection(&job->cs);
316 return hr;
319 static HRESULT WINAPI BackgroundCopyJob_AddFile(IBackgroundCopyJob4 *iface, LPCWSTR RemoteUrl, LPCWSTR LocalName)
321 BG_FILE_INFO file;
323 TRACE("%p, %s, %s.\n", iface, debugstr_w(RemoteUrl), debugstr_w(LocalName));
325 file.RemoteName = (LPWSTR)RemoteUrl;
326 file.LocalName = (LPWSTR)LocalName;
327 return IBackgroundCopyJob4_AddFileSet(iface, 1, &file);
330 static HRESULT WINAPI BackgroundCopyJob_EnumFiles(IBackgroundCopyJob4 *iface, IEnumBackgroundCopyFiles **enum_files)
332 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJob4(iface);
333 TRACE("%p, %p.\n", iface, enum_files);
334 return EnumBackgroundCopyFilesConstructor(job, enum_files);
337 static HRESULT WINAPI BackgroundCopyJob_Suspend(IBackgroundCopyJob4 *iface)
339 FIXME("(%p): stub\n", iface);
340 return E_NOTIMPL;
343 static HRESULT WINAPI BackgroundCopyJob_Resume(IBackgroundCopyJob4 *iface)
345 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJob4(iface);
346 HRESULT hr = S_OK;
348 TRACE("%p.\n", iface);
350 EnterCriticalSection(&globalMgr.cs);
351 if (is_job_done(job))
353 hr = BG_E_INVALID_STATE;
355 else if (job->jobProgress.FilesTransferred == job->jobProgress.FilesTotal)
357 hr = BG_E_EMPTY;
359 else if (job->state != BG_JOB_STATE_CONNECTING
360 && job->state != BG_JOB_STATE_TRANSFERRING)
362 job->state = BG_JOB_STATE_QUEUED;
363 job->error.context = 0;
364 job->error.code = S_OK;
365 if (job->error.file)
367 IBackgroundCopyFile2_Release(job->error.file);
368 job->error.file = NULL;
370 SetEvent(globalMgr.jobEvent);
372 LeaveCriticalSection(&globalMgr.cs);
374 return hr;
377 static HRESULT WINAPI BackgroundCopyJob_Cancel(IBackgroundCopyJob4 *iface)
379 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJob4(iface);
380 HRESULT hr = S_OK;
382 TRACE("%p.\n", iface);
384 EnterCriticalSection(&job->cs);
386 if (is_job_done(job))
388 hr = BG_E_INVALID_STATE;
390 else
392 BackgroundCopyFileImpl *file;
394 if (job->state == BG_JOB_STATE_CONNECTING || job->state == BG_JOB_STATE_TRANSFERRING)
396 job->state = BG_JOB_STATE_CANCELLED;
397 SetEvent(job->cancel);
399 LeaveCriticalSection(&job->cs);
400 WaitForSingleObject(job->done, INFINITE);
401 EnterCriticalSection(&job->cs);
404 LIST_FOR_EACH_ENTRY(file, &job->files, BackgroundCopyFileImpl, entryFromJob)
406 if (file->tempFileName[0] && !DeleteFileW(file->tempFileName))
408 WARN("Couldn't delete %s (%lu)\n", debugstr_w(file->tempFileName), GetLastError());
409 hr = BG_S_UNABLE_TO_DELETE_FILES;
411 if (file->info.LocalName && !DeleteFileW(file->info.LocalName))
413 WARN("Couldn't delete %s (%lu)\n", debugstr_w(file->info.LocalName), GetLastError());
414 hr = BG_S_UNABLE_TO_DELETE_FILES;
417 job->state = BG_JOB_STATE_CANCELLED;
420 LeaveCriticalSection(&job->cs);
421 return hr;
424 static HRESULT WINAPI BackgroundCopyJob_Complete(IBackgroundCopyJob4 *iface)
426 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJob4(iface);
427 HRESULT hr = S_OK;
429 TRACE("%p.\n", iface);
431 EnterCriticalSection(&job->cs);
433 if (is_job_done(job))
435 hr = BG_E_INVALID_STATE;
437 else
439 BackgroundCopyFileImpl *file;
440 LIST_FOR_EACH_ENTRY(file, &job->files, BackgroundCopyFileImpl, entryFromJob)
442 if (file->fileProgress.Completed)
444 if (!MoveFileExW(file->tempFileName, file->info.LocalName,
445 (MOVEFILE_COPY_ALLOWED
446 | MOVEFILE_REPLACE_EXISTING
447 | MOVEFILE_WRITE_THROUGH)))
449 ERR("Couldn't rename file %s -> %s\n",
450 debugstr_w(file->tempFileName),
451 debugstr_w(file->info.LocalName));
452 hr = BG_S_PARTIAL_COMPLETE;
455 else
456 hr = BG_S_PARTIAL_COMPLETE;
460 job->state = BG_JOB_STATE_ACKNOWLEDGED;
461 LeaveCriticalSection(&job->cs);
463 return hr;
466 static HRESULT WINAPI BackgroundCopyJob_GetId(IBackgroundCopyJob4 *iface, GUID *id)
468 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJob4(iface);
470 TRACE("%p, %p.\n", iface, id);
472 *id = job->jobId;
474 return S_OK;
477 static HRESULT WINAPI BackgroundCopyJob_GetType(IBackgroundCopyJob4 *iface, BG_JOB_TYPE *job_type)
479 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJob4(iface);
481 TRACE("%p, %p.\n", iface, job_type);
483 if (!job_type)
484 return E_INVALIDARG;
486 *job_type = job->type;
487 return S_OK;
490 static HRESULT WINAPI BackgroundCopyJob_GetProgress(IBackgroundCopyJob4 *iface, BG_JOB_PROGRESS *progress)
492 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJob4(iface);
494 TRACE("%p, %p.\n", iface, progress);
496 if (!progress)
497 return E_INVALIDARG;
499 EnterCriticalSection(&job->cs);
500 *progress = job->jobProgress;
501 LeaveCriticalSection(&job->cs);
503 return S_OK;
506 static HRESULT WINAPI BackgroundCopyJob_GetTimes(IBackgroundCopyJob4 *iface, BG_JOB_TIMES *pVal)
508 FIXME("%p, %p: stub\n", iface, pVal);
509 return E_NOTIMPL;
512 static HRESULT WINAPI BackgroundCopyJob_GetState(IBackgroundCopyJob4 *iface, BG_JOB_STATE *state)
514 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJob4(iface);
516 TRACE("%p, %p.\n", iface, state);
518 if (!state)
519 return E_INVALIDARG;
521 /* Don't think we need a critical section for this */
522 *state = job->state;
523 return S_OK;
526 static HRESULT WINAPI BackgroundCopyJob_GetError(IBackgroundCopyJob4 *iface, IBackgroundCopyError **ppError)
528 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJob4(iface);
530 TRACE("%p, %p.\n", iface, ppError);
532 if (!job->error.context) return BG_E_ERROR_INFORMATION_UNAVAILABLE;
534 return create_copy_error(job->error.context, job->error.code, job->error.file, ppError);
537 static HRESULT WINAPI BackgroundCopyJob_GetOwner(IBackgroundCopyJob4 *iface, LPWSTR *owner)
539 FIXME("%p, %p: stub\n", iface, owner);
540 return E_NOTIMPL;
543 static HRESULT WINAPI BackgroundCopyJob_SetDisplayName(IBackgroundCopyJob4 *iface, LPCWSTR name)
545 FIXME("%p, %s: stub\n", iface, debugstr_w(name));
546 return E_NOTIMPL;
549 static HRESULT WINAPI BackgroundCopyJob_GetDisplayName(IBackgroundCopyJob4 *iface, LPWSTR *name)
551 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJob4(iface);
553 TRACE("%p, %p.\n", iface, name);
555 return return_strval(job->displayName, name);
558 static HRESULT WINAPI BackgroundCopyJob_SetDescription(IBackgroundCopyJob4 *iface, LPCWSTR desc)
560 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJob4(iface);
561 static const int max_description_len = 1024;
562 HRESULT hr = S_OK;
563 int len;
565 TRACE("%p, %s.\n", iface, debugstr_w(desc));
567 if (!desc)
568 return E_INVALIDARG;
570 len = lstrlenW(desc);
571 if (len > max_description_len) return BG_E_STRING_TOO_LONG;
573 EnterCriticalSection(&job->cs);
575 if (is_job_done(job))
577 hr = BG_E_INVALID_STATE;
579 else
581 free(job->description);
582 if (!(job->description = wcsdup(desc)))
583 hr = E_OUTOFMEMORY;
586 LeaveCriticalSection(&job->cs);
588 return hr;
591 static HRESULT WINAPI BackgroundCopyJob_GetDescription(IBackgroundCopyJob4 *iface, LPWSTR *desc)
593 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJob4(iface);
595 TRACE("%p, %p.\n", iface, desc);
597 return return_strval(job->description, desc);
600 static HRESULT WINAPI BackgroundCopyJob_SetPriority(IBackgroundCopyJob4 *iface, BG_JOB_PRIORITY priority)
602 FIXME("%p, %d: stub\n", iface, priority);
603 return S_OK;
606 static HRESULT WINAPI BackgroundCopyJob_GetPriority(IBackgroundCopyJob4 *iface, BG_JOB_PRIORITY *priority)
608 FIXME("%p, %p: stub\n", iface, priority);
609 return E_NOTIMPL;
612 static HRESULT WINAPI BackgroundCopyJob_SetNotifyFlags(IBackgroundCopyJob4 *iface, ULONG flags)
614 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJob4(iface);
615 static const ULONG valid_flags = BG_NOTIFY_JOB_TRANSFERRED |
616 BG_NOTIFY_JOB_ERROR |
617 BG_NOTIFY_DISABLE |
618 BG_NOTIFY_JOB_MODIFICATION |
619 BG_NOTIFY_FILE_TRANSFERRED;
621 TRACE("%p, %#lx.\n", iface, flags);
623 if (is_job_done(job)) return BG_E_INVALID_STATE;
624 if (flags & ~valid_flags) return E_NOTIMPL;
625 job->notify_flags = flags;
626 return S_OK;
629 static HRESULT WINAPI BackgroundCopyJob_GetNotifyFlags(IBackgroundCopyJob4 *iface, ULONG *flags)
631 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJob4(iface);
633 TRACE("%p, %p.\n", iface, flags);
635 if (!flags) return E_INVALIDARG;
637 *flags = job->notify_flags;
639 return S_OK;
642 static HRESULT WINAPI BackgroundCopyJob_SetNotifyInterface(IBackgroundCopyJob4 *iface, IUnknown *callback)
644 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJob4(iface);
645 HRESULT hr = S_OK;
647 TRACE("%p, %p.\n", iface, callback);
649 if (is_job_done(job)) return BG_E_INVALID_STATE;
651 if (job->callback)
653 IBackgroundCopyCallback2_Release(job->callback);
654 job->callback = NULL;
655 job->callback2 = FALSE;
658 if (callback)
660 hr = IUnknown_QueryInterface(callback, &IID_IBackgroundCopyCallback2, (void **)&job->callback);
661 if (FAILED(hr))
662 hr = IUnknown_QueryInterface(callback, &IID_IBackgroundCopyCallback, (void**)&job->callback);
663 else
664 job->callback2 = TRUE;
667 return hr;
670 static HRESULT WINAPI BackgroundCopyJob_GetNotifyInterface(IBackgroundCopyJob4 *iface, IUnknown **callback)
672 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJob4(iface);
674 TRACE("%p, %p.\n", iface, callback);
676 if (!callback) return E_INVALIDARG;
678 *callback = (IUnknown *)job->callback;
679 if (*callback)
680 IUnknown_AddRef(*callback);
682 return S_OK;
685 static HRESULT WINAPI BackgroundCopyJob_SetMinimumRetryDelay(IBackgroundCopyJob4 *iface, ULONG delay)
687 FIXME("%p, %lu.\n", iface, delay);
688 return S_OK;
691 static HRESULT WINAPI BackgroundCopyJob_GetMinimumRetryDelay(IBackgroundCopyJob4 *iface, ULONG *delay)
693 FIXME("%p, %p: stub\n", iface, delay);
694 *delay = 30;
695 return S_OK;
698 static HRESULT WINAPI BackgroundCopyJob_SetNoProgressTimeout(IBackgroundCopyJob4 *iface, ULONG timeout)
700 FIXME("%p, %lu.: stub\n", iface, timeout);
701 return S_OK;
704 static HRESULT WINAPI BackgroundCopyJob_GetNoProgressTimeout(IBackgroundCopyJob4 *iface, ULONG *timeout)
706 FIXME("%p, %p: stub\n", iface, timeout);
707 *timeout = 900;
708 return S_OK;
711 static HRESULT WINAPI BackgroundCopyJob_GetErrorCount(IBackgroundCopyJob4 *iface, ULONG *count)
713 FIXME("%p, %p: stub\n", iface, count);
714 return E_NOTIMPL;
717 static HRESULT WINAPI BackgroundCopyJob_SetProxySettings(IBackgroundCopyJob4 *iface, BG_JOB_PROXY_USAGE proxy_usage,
718 const WCHAR *proxy_list, const WCHAR *proxy_bypass_list)
720 FIXME("%p, %d, %s, %s: stub\n", iface, proxy_usage, debugstr_w(proxy_list), debugstr_w(proxy_bypass_list));
721 return E_NOTIMPL;
724 static HRESULT WINAPI BackgroundCopyJob_GetProxySettings(IBackgroundCopyJob4 *iface, BG_JOB_PROXY_USAGE *proxy_usage,
725 LPWSTR *proxy_list, LPWSTR *proxy_bypass_list)
727 FIXME("%p, %p, %p, %p: stub\n", iface, proxy_usage, proxy_list, proxy_bypass_list);
728 return E_NOTIMPL;
731 static HRESULT WINAPI BackgroundCopyJob_TakeOwnership(IBackgroundCopyJob4 *iface)
733 FIXME("%p: stub\n", iface);
734 return E_NOTIMPL;
737 static HRESULT WINAPI BackgroundCopyJob_SetNotifyCmdLine(IBackgroundCopyJob4 *iface, LPCWSTR prog, LPCWSTR params)
739 FIXME("%p, %s, %s: stub\n", iface, debugstr_w(prog), debugstr_w(params));
740 return E_NOTIMPL;
743 static HRESULT WINAPI BackgroundCopyJob_GetNotifyCmdLine(IBackgroundCopyJob4 *iface, LPWSTR *prog, LPWSTR *params)
745 FIXME("%p, %p, %p: stub\n", iface, prog, params);
746 return E_NOTIMPL;
749 static HRESULT WINAPI BackgroundCopyJob_GetReplyProgress(IBackgroundCopyJob4 *iface, BG_JOB_REPLY_PROGRESS *progress)
751 FIXME("%p, %p: stub\n", iface, progress);
752 return E_NOTIMPL;
755 static HRESULT WINAPI BackgroundCopyJob_GetReplyData(IBackgroundCopyJob4 *iface, byte **buffer, UINT64 *length)
757 FIXME("%p, %p, %p: stub\n", iface, buffer, length);
758 return E_NOTIMPL;
761 static HRESULT WINAPI BackgroundCopyJob_SetReplyFileName(IBackgroundCopyJob4 *iface, LPCWSTR filename)
763 FIXME("%p, %s: stub\n", iface, debugstr_w(filename));
764 return E_NOTIMPL;
767 static HRESULT WINAPI BackgroundCopyJob_GetReplyFileName(IBackgroundCopyJob4 *iface, LPWSTR *filename)
769 FIXME("%p, %p: stub\n", iface, filename);
770 return E_NOTIMPL;
773 static int index_from_target(BG_AUTH_TARGET target)
775 if (!target || target > BG_AUTH_TARGET_PROXY) return -1;
776 return target - 1;
779 static int index_from_scheme(BG_AUTH_SCHEME scheme)
781 if (!scheme || scheme > BG_AUTH_SCHEME_PASSPORT) return -1;
782 return scheme - 1;
785 static HRESULT WINAPI BackgroundCopyJob_SetCredentials(IBackgroundCopyJob4 *iface, BG_AUTH_CREDENTIALS *cred)
787 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJob4(iface);
788 BG_AUTH_CREDENTIALS *new_cred;
789 int idx_target, idx_scheme;
791 TRACE("%p, %p.\n", iface, cred);
793 if ((idx_target = index_from_target(cred->Target)) < 0) return BG_E_INVALID_AUTH_TARGET;
794 if ((idx_scheme = index_from_scheme(cred->Scheme)) < 0) return BG_E_INVALID_AUTH_SCHEME;
795 new_cred = &job->http_options.creds[idx_target][idx_scheme];
797 EnterCriticalSection(&job->cs);
799 new_cred->Target = cred->Target;
800 new_cred->Scheme = cred->Scheme;
802 if (cred->Credentials.Basic.UserName)
804 free(new_cred->Credentials.Basic.UserName);
805 new_cred->Credentials.Basic.UserName = wcsdup(cred->Credentials.Basic.UserName);
807 if (cred->Credentials.Basic.Password)
809 free(new_cred->Credentials.Basic.Password);
810 new_cred->Credentials.Basic.Password = wcsdup(cred->Credentials.Basic.Password);
813 LeaveCriticalSection(&job->cs);
814 return S_OK;
817 static HRESULT WINAPI BackgroundCopyJob_RemoveCredentials(
818 IBackgroundCopyJob4 *iface,
819 BG_AUTH_TARGET target,
820 BG_AUTH_SCHEME scheme)
822 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJob4(iface);
823 BG_AUTH_CREDENTIALS *new_cred;
824 int idx_target, idx_scheme;
826 TRACE("%p, %u, %u.\n", iface, target, scheme);
828 if ((idx_target = index_from_target(target)) < 0) return BG_E_INVALID_AUTH_TARGET;
829 if ((idx_scheme = index_from_scheme(scheme)) < 0) return BG_E_INVALID_AUTH_SCHEME;
830 new_cred = &job->http_options.creds[idx_target][idx_scheme];
832 EnterCriticalSection(&job->cs);
834 new_cred->Target = 0;
835 new_cred->Scheme = 0;
836 free(new_cred->Credentials.Basic.UserName);
837 new_cred->Credentials.Basic.UserName = NULL;
838 free(new_cred->Credentials.Basic.Password);
839 new_cred->Credentials.Basic.Password = NULL;
841 LeaveCriticalSection(&job->cs);
842 return S_OK;
845 static HRESULT WINAPI BackgroundCopyJob_ReplaceRemotePrefix(
846 IBackgroundCopyJob4 *iface,
847 LPCWSTR OldPrefix,
848 LPCWSTR NewPrefix)
850 FIXME("%p, %s, %s: stub\n", iface, debugstr_w(OldPrefix), debugstr_w(NewPrefix));
851 return S_OK;
854 static HRESULT WINAPI BackgroundCopyJob_AddFileWithRanges(
855 IBackgroundCopyJob4 *iface,
856 LPCWSTR RemoteUrl,
857 LPCWSTR LocalName,
858 DWORD RangeCount,
859 BG_FILE_RANGE Ranges[])
861 FIXME("%p, %s, %s, %lu, %p: stub\n", iface, debugstr_w(RemoteUrl), debugstr_w(LocalName), RangeCount, Ranges);
862 return S_OK;
865 static HRESULT WINAPI BackgroundCopyJob_SetFileACLFlags(IBackgroundCopyJob4 *iface, DWORD flags)
867 FIXME("%p, %#lx: stub\n", iface, flags);
868 return S_OK;
871 static HRESULT WINAPI BackgroundCopyJob_GetFileACLFlags(IBackgroundCopyJob4 *iface, DWORD *flags)
873 FIXME("%p, %p: stub\n", iface, flags);
874 return S_OK;
877 static HRESULT WINAPI BackgroundCopyJob_SetPeerCachingFlags(IBackgroundCopyJob4 *iface, DWORD flags)
879 FIXME("%p, %#lx.\n", iface, flags);
880 return S_OK;
883 static HRESULT WINAPI BackgroundCopyJob_GetPeerCachingFlags(IBackgroundCopyJob4 *iface, DWORD *flags)
885 FIXME("%p, %p.\n", iface, flags);
886 return E_NOTIMPL;
889 static HRESULT WINAPI BackgroundCopyJob_GetOwnerIntegrityLevel(IBackgroundCopyJob4 *iface, ULONG *level)
891 FIXME("%p, %p.\n", iface, level);
892 return E_NOTIMPL;
895 static HRESULT WINAPI BackgroundCopyJob_GetOwnerElevationState(IBackgroundCopyJob4 *iface, BOOL *elevated)
897 FIXME("%p, %p.\n", iface, elevated);
898 return E_NOTIMPL;
901 static HRESULT WINAPI BackgroundCopyJob_SetMaximumDownloadTime(IBackgroundCopyJob4 *iface, ULONG timeout)
903 FIXME("%p, %lu.\n", iface, timeout);
904 return S_OK;
907 static HRESULT WINAPI BackgroundCopyJob_GetMaximumDownloadTime(IBackgroundCopyJob4 *iface, ULONG *timeout)
909 FIXME("%p, %p.\n", iface, timeout);
910 return E_NOTIMPL;
913 static const IBackgroundCopyJob4Vtbl BackgroundCopyJobVtbl =
915 BackgroundCopyJob_QueryInterface,
916 BackgroundCopyJob_AddRef,
917 BackgroundCopyJob_Release,
918 BackgroundCopyJob_AddFileSet,
919 BackgroundCopyJob_AddFile,
920 BackgroundCopyJob_EnumFiles,
921 BackgroundCopyJob_Suspend,
922 BackgroundCopyJob_Resume,
923 BackgroundCopyJob_Cancel,
924 BackgroundCopyJob_Complete,
925 BackgroundCopyJob_GetId,
926 BackgroundCopyJob_GetType,
927 BackgroundCopyJob_GetProgress,
928 BackgroundCopyJob_GetTimes,
929 BackgroundCopyJob_GetState,
930 BackgroundCopyJob_GetError,
931 BackgroundCopyJob_GetOwner,
932 BackgroundCopyJob_SetDisplayName,
933 BackgroundCopyJob_GetDisplayName,
934 BackgroundCopyJob_SetDescription,
935 BackgroundCopyJob_GetDescription,
936 BackgroundCopyJob_SetPriority,
937 BackgroundCopyJob_GetPriority,
938 BackgroundCopyJob_SetNotifyFlags,
939 BackgroundCopyJob_GetNotifyFlags,
940 BackgroundCopyJob_SetNotifyInterface,
941 BackgroundCopyJob_GetNotifyInterface,
942 BackgroundCopyJob_SetMinimumRetryDelay,
943 BackgroundCopyJob_GetMinimumRetryDelay,
944 BackgroundCopyJob_SetNoProgressTimeout,
945 BackgroundCopyJob_GetNoProgressTimeout,
946 BackgroundCopyJob_GetErrorCount,
947 BackgroundCopyJob_SetProxySettings,
948 BackgroundCopyJob_GetProxySettings,
949 BackgroundCopyJob_TakeOwnership,
950 BackgroundCopyJob_SetNotifyCmdLine,
951 BackgroundCopyJob_GetNotifyCmdLine,
952 BackgroundCopyJob_GetReplyProgress,
953 BackgroundCopyJob_GetReplyData,
954 BackgroundCopyJob_SetReplyFileName,
955 BackgroundCopyJob_GetReplyFileName,
956 BackgroundCopyJob_SetCredentials,
957 BackgroundCopyJob_RemoveCredentials,
958 BackgroundCopyJob_ReplaceRemotePrefix,
959 BackgroundCopyJob_AddFileWithRanges,
960 BackgroundCopyJob_SetFileACLFlags,
961 BackgroundCopyJob_GetFileACLFlags,
962 BackgroundCopyJob_SetPeerCachingFlags,
963 BackgroundCopyJob_GetPeerCachingFlags,
964 BackgroundCopyJob_GetOwnerIntegrityLevel,
965 BackgroundCopyJob_GetOwnerElevationState,
966 BackgroundCopyJob_SetMaximumDownloadTime,
967 BackgroundCopyJob_GetMaximumDownloadTime,
970 static inline BackgroundCopyJobImpl *impl_from_IBackgroundCopyJobHttpOptions(
971 IBackgroundCopyJobHttpOptions *iface)
973 return CONTAINING_RECORD(iface, BackgroundCopyJobImpl, IBackgroundCopyJobHttpOptions_iface);
976 static HRESULT WINAPI http_options_QueryInterface(
977 IBackgroundCopyJobHttpOptions *iface,
978 REFIID riid,
979 void **ppvObject)
981 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJobHttpOptions(iface);
982 return IBackgroundCopyJob4_QueryInterface(&job->IBackgroundCopyJob4_iface, riid, ppvObject);
985 static ULONG WINAPI http_options_AddRef(
986 IBackgroundCopyJobHttpOptions *iface)
988 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJobHttpOptions(iface);
989 return IBackgroundCopyJob4_AddRef(&job->IBackgroundCopyJob4_iface);
992 static ULONG WINAPI http_options_Release(
993 IBackgroundCopyJobHttpOptions *iface)
995 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJobHttpOptions(iface);
996 return IBackgroundCopyJob4_Release(&job->IBackgroundCopyJob4_iface);
999 static HRESULT WINAPI http_options_SetClientCertificateByID(
1000 IBackgroundCopyJobHttpOptions *iface,
1001 BG_CERT_STORE_LOCATION StoreLocation,
1002 LPCWSTR StoreName,
1003 BYTE *pCertHashBlob)
1005 FIXME("\n");
1006 return E_NOTIMPL;
1009 static HRESULT WINAPI http_options_SetClientCertificateByName(
1010 IBackgroundCopyJobHttpOptions *iface,
1011 BG_CERT_STORE_LOCATION StoreLocation,
1012 LPCWSTR StoreName,
1013 LPCWSTR SubjectName)
1015 FIXME("\n");
1016 return E_NOTIMPL;
1019 static HRESULT WINAPI http_options_RemoveClientCertificate(
1020 IBackgroundCopyJobHttpOptions *iface)
1022 FIXME("\n");
1023 return E_NOTIMPL;
1026 static HRESULT WINAPI http_options_GetClientCertificate(
1027 IBackgroundCopyJobHttpOptions *iface,
1028 BG_CERT_STORE_LOCATION *pStoreLocation,
1029 LPWSTR *pStoreName,
1030 BYTE **ppCertHashBlob,
1031 LPWSTR *pSubjectName)
1033 FIXME("\n");
1034 return E_NOTIMPL;
1037 static HRESULT WINAPI http_options_SetCustomHeaders(
1038 IBackgroundCopyJobHttpOptions *iface,
1039 LPCWSTR RequestHeaders)
1041 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJobHttpOptions(iface);
1043 TRACE("(%p)->(%s)\n", iface, debugstr_w(RequestHeaders));
1045 EnterCriticalSection(&job->cs);
1047 if (RequestHeaders)
1049 WCHAR *headers = wcsdup(RequestHeaders);
1050 if (!headers)
1052 LeaveCriticalSection(&job->cs);
1053 return E_OUTOFMEMORY;
1055 free(job->http_options.headers);
1056 job->http_options.headers = headers;
1058 else
1060 free(job->http_options.headers);
1061 job->http_options.headers = NULL;
1064 LeaveCriticalSection(&job->cs);
1065 return S_OK;
1068 static HRESULT WINAPI http_options_GetCustomHeaders(
1069 IBackgroundCopyJobHttpOptions *iface,
1070 LPWSTR *pRequestHeaders)
1072 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJobHttpOptions(iface);
1074 TRACE("(%p)->(%p)\n", iface, pRequestHeaders);
1076 EnterCriticalSection(&job->cs);
1078 if (job->http_options.headers)
1080 WCHAR *headers = co_strdupW(job->http_options.headers);
1081 if (!headers)
1083 LeaveCriticalSection(&job->cs);
1084 return E_OUTOFMEMORY;
1086 *pRequestHeaders = headers;
1087 LeaveCriticalSection(&job->cs);
1088 return S_OK;
1091 *pRequestHeaders = NULL;
1092 LeaveCriticalSection(&job->cs);
1093 return S_FALSE;
1096 static HRESULT WINAPI http_options_SetSecurityFlags(
1097 IBackgroundCopyJobHttpOptions *iface,
1098 ULONG Flags)
1100 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJobHttpOptions(iface);
1102 TRACE("(%p)->(0x%08lx)\n", iface, Flags);
1104 job->http_options.flags = Flags;
1105 return S_OK;
1108 static HRESULT WINAPI http_options_GetSecurityFlags(
1109 IBackgroundCopyJobHttpOptions *iface,
1110 ULONG *pFlags)
1112 BackgroundCopyJobImpl *job = impl_from_IBackgroundCopyJobHttpOptions(iface);
1114 TRACE("(%p)->(%p)\n", iface, pFlags);
1116 *pFlags = job->http_options.flags;
1117 return S_OK;
1120 static const IBackgroundCopyJobHttpOptionsVtbl http_options_vtbl =
1122 http_options_QueryInterface,
1123 http_options_AddRef,
1124 http_options_Release,
1125 http_options_SetClientCertificateByID,
1126 http_options_SetClientCertificateByName,
1127 http_options_RemoveClientCertificate,
1128 http_options_GetClientCertificate,
1129 http_options_SetCustomHeaders,
1130 http_options_GetCustomHeaders,
1131 http_options_SetSecurityFlags,
1132 http_options_GetSecurityFlags
1135 HRESULT BackgroundCopyJobConstructor(LPCWSTR displayName, BG_JOB_TYPE type, GUID *job_id, BackgroundCopyJobImpl **job)
1137 HRESULT hr;
1138 BackgroundCopyJobImpl *This;
1140 TRACE("(%s,%d,%p)\n", debugstr_w(displayName), type, job);
1142 This = malloc(sizeof(*This));
1143 if (!This)
1144 return E_OUTOFMEMORY;
1146 This->IBackgroundCopyJob4_iface.lpVtbl = &BackgroundCopyJobVtbl;
1147 This->IBackgroundCopyJobHttpOptions_iface.lpVtbl = &http_options_vtbl;
1148 InitializeCriticalSectionEx(&This->cs, 0, RTL_CRITICAL_SECTION_FLAG_FORCE_DEBUG_INFO);
1149 This->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": BackgroundCopyJobImpl.cs");
1151 This->ref = 1;
1152 This->type = type;
1154 This->displayName = wcsdup(displayName);
1155 if (!This->displayName)
1157 This->cs.DebugInfo->Spare[0] = 0;
1158 DeleteCriticalSection(&This->cs);
1159 free(This);
1160 return E_OUTOFMEMORY;
1163 hr = CoCreateGuid(&This->jobId);
1164 if (FAILED(hr))
1166 This->cs.DebugInfo->Spare[0] = 0;
1167 DeleteCriticalSection(&This->cs);
1168 free(This->displayName);
1169 free(This);
1170 return hr;
1172 *job_id = This->jobId;
1174 list_init(&This->files);
1175 This->jobProgress.BytesTotal = 0;
1176 This->jobProgress.BytesTransferred = 0;
1177 This->jobProgress.FilesTotal = 0;
1178 This->jobProgress.FilesTransferred = 0;
1180 This->state = BG_JOB_STATE_SUSPENDED;
1181 This->description = NULL;
1182 This->notify_flags = BG_NOTIFY_JOB_ERROR | BG_NOTIFY_JOB_TRANSFERRED;
1183 This->callback = NULL;
1184 This->callback2 = FALSE;
1186 This->error.context = 0;
1187 This->error.code = S_OK;
1188 This->error.file = NULL;
1190 memset(&This->http_options, 0, sizeof(This->http_options));
1192 This->wait = CreateEventW(NULL, FALSE, FALSE, NULL);
1193 This->cancel = CreateEventW(NULL, FALSE, FALSE, NULL);
1194 This->done = CreateEventW(NULL, FALSE, FALSE, NULL);
1196 *job = This;
1198 TRACE("created job %s:%p\n", debugstr_guid(&This->jobId), This);
1200 return S_OK;
1203 void processJob(BackgroundCopyJobImpl *job)
1205 for (;;)
1207 BackgroundCopyFileImpl *file;
1208 BOOL done = TRUE;
1210 EnterCriticalSection(&job->cs);
1211 LIST_FOR_EACH_ENTRY(file, &job->files, BackgroundCopyFileImpl, entryFromJob)
1212 if (!file->fileProgress.Completed)
1214 done = FALSE;
1215 break;
1217 LeaveCriticalSection(&job->cs);
1218 if (done)
1220 transitionJobState(job, BG_JOB_STATE_QUEUED, BG_JOB_STATE_TRANSFERRED);
1221 if (job->callback && (job->notify_flags & BG_NOTIFY_JOB_TRANSFERRED))
1223 TRACE("Calling JobTransferred -->\n");
1224 IBackgroundCopyCallback2_JobTransferred(job->callback, (IBackgroundCopyJob*)&job->IBackgroundCopyJob4_iface);
1225 TRACE("Called JobTransferred <--\n");
1227 return;
1230 if (!processFile(file, job))
1231 return;