2 * Queue Manager (BITS) File
4 * Copyright 2007, 2008 Google (Roy Shea, Dan Hipschman)
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
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(qmgr
);
38 static inline BackgroundCopyFileImpl
*impl_from_IBackgroundCopyFile(IBackgroundCopyFile
*iface
)
40 return CONTAINING_RECORD(iface
, BackgroundCopyFileImpl
, IBackgroundCopyFile_iface
);
43 static HRESULT WINAPI
BITS_IBackgroundCopyFile_QueryInterface(
44 IBackgroundCopyFile
* iface
,
48 BackgroundCopyFileImpl
*This
= impl_from_IBackgroundCopyFile(iface
);
50 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), obj
);
52 if (IsEqualGUID(riid
, &IID_IUnknown
)
53 || IsEqualGUID(riid
, &IID_IBackgroundCopyFile
))
56 IBackgroundCopyFile_AddRef(iface
);
64 static ULONG WINAPI
BITS_IBackgroundCopyFile_AddRef(IBackgroundCopyFile
* iface
)
66 BackgroundCopyFileImpl
*This
= impl_from_IBackgroundCopyFile(iface
);
67 ULONG ref
= InterlockedIncrement(&This
->ref
);
68 TRACE("(%p)->(%d)\n", This
, ref
);
72 static ULONG WINAPI
BITS_IBackgroundCopyFile_Release(
73 IBackgroundCopyFile
* iface
)
75 BackgroundCopyFileImpl
*This
= impl_from_IBackgroundCopyFile(iface
);
76 ULONG ref
= InterlockedDecrement(&This
->ref
);
78 TRACE("(%p)->(%d)\n", This
, ref
);
82 IBackgroundCopyJob2_Release(&This
->owner
->IBackgroundCopyJob2_iface
);
83 HeapFree(GetProcessHeap(), 0, This
->info
.LocalName
);
84 HeapFree(GetProcessHeap(), 0, This
->info
.RemoteName
);
85 HeapFree(GetProcessHeap(), 0, This
);
91 /* Get the remote name of a background copy file */
92 static HRESULT WINAPI
BITS_IBackgroundCopyFile_GetRemoteName(
93 IBackgroundCopyFile
* iface
,
96 BackgroundCopyFileImpl
*This
= impl_from_IBackgroundCopyFile(iface
);
97 int n
= (lstrlenW(This
->info
.RemoteName
) + 1) * sizeof(WCHAR
);
99 *pVal
= CoTaskMemAlloc(n
);
101 return E_OUTOFMEMORY
;
103 memcpy(*pVal
, This
->info
.RemoteName
, n
);
107 static HRESULT WINAPI
BITS_IBackgroundCopyFile_GetLocalName(
108 IBackgroundCopyFile
* iface
,
111 BackgroundCopyFileImpl
*This
= impl_from_IBackgroundCopyFile(iface
);
112 int n
= (lstrlenW(This
->info
.LocalName
) + 1) * sizeof(WCHAR
);
114 *pVal
= CoTaskMemAlloc(n
);
116 return E_OUTOFMEMORY
;
118 memcpy(*pVal
, This
->info
.LocalName
, n
);
122 static HRESULT WINAPI
BITS_IBackgroundCopyFile_GetProgress(
123 IBackgroundCopyFile
* iface
,
124 BG_FILE_PROGRESS
*pVal
)
126 BackgroundCopyFileImpl
*This
= impl_from_IBackgroundCopyFile(iface
);
128 EnterCriticalSection(&This
->owner
->cs
);
129 pVal
->BytesTotal
= This
->fileProgress
.BytesTotal
;
130 pVal
->BytesTransferred
= This
->fileProgress
.BytesTransferred
;
131 pVal
->Completed
= This
->fileProgress
.Completed
;
132 LeaveCriticalSection(&This
->owner
->cs
);
137 static const IBackgroundCopyFileVtbl BITS_IBackgroundCopyFile_Vtbl
=
139 BITS_IBackgroundCopyFile_QueryInterface
,
140 BITS_IBackgroundCopyFile_AddRef
,
141 BITS_IBackgroundCopyFile_Release
,
142 BITS_IBackgroundCopyFile_GetRemoteName
,
143 BITS_IBackgroundCopyFile_GetLocalName
,
144 BITS_IBackgroundCopyFile_GetProgress
147 HRESULT
BackgroundCopyFileConstructor(BackgroundCopyJobImpl
*owner
,
148 LPCWSTR remoteName
, LPCWSTR localName
,
149 BackgroundCopyFileImpl
**file
)
151 BackgroundCopyFileImpl
*This
;
154 TRACE("(%s, %s, %p)\n", debugstr_w(remoteName
), debugstr_w(localName
), file
);
156 This
= HeapAlloc(GetProcessHeap(), 0, sizeof *This
);
158 return E_OUTOFMEMORY
;
160 n
= (lstrlenW(remoteName
) + 1) * sizeof(WCHAR
);
161 This
->info
.RemoteName
= HeapAlloc(GetProcessHeap(), 0, n
);
162 if (!This
->info
.RemoteName
)
164 HeapFree(GetProcessHeap(), 0, This
);
165 return E_OUTOFMEMORY
;
167 memcpy(This
->info
.RemoteName
, remoteName
, n
);
169 n
= (lstrlenW(localName
) + 1) * sizeof(WCHAR
);
170 This
->info
.LocalName
= HeapAlloc(GetProcessHeap(), 0, n
);
171 if (!This
->info
.LocalName
)
173 HeapFree(GetProcessHeap(), 0, This
->info
.RemoteName
);
174 HeapFree(GetProcessHeap(), 0, This
);
175 return E_OUTOFMEMORY
;
177 memcpy(This
->info
.LocalName
, localName
, n
);
179 This
->IBackgroundCopyFile_iface
.lpVtbl
= &BITS_IBackgroundCopyFile_Vtbl
;
182 This
->fileProgress
.BytesTotal
= BG_SIZE_UNKNOWN
;
183 This
->fileProgress
.BytesTransferred
= 0;
184 This
->fileProgress
.Completed
= FALSE
;
186 IBackgroundCopyJob2_AddRef(&owner
->IBackgroundCopyJob2_iface
);
192 static DWORD CALLBACK
copyProgressCallback(LARGE_INTEGER totalSize
,
193 LARGE_INTEGER totalTransferred
,
194 LARGE_INTEGER streamSize
,
195 LARGE_INTEGER streamTransferred
,
202 BackgroundCopyFileImpl
*file
= obj
;
203 BackgroundCopyJobImpl
*job
= file
->owner
;
206 EnterCriticalSection(&job
->cs
);
207 diff
= (file
->fileProgress
.BytesTotal
== BG_SIZE_UNKNOWN
208 ? totalTransferred
.QuadPart
209 : totalTransferred
.QuadPart
- file
->fileProgress
.BytesTransferred
);
210 file
->fileProgress
.BytesTotal
= totalSize
.QuadPart
;
211 file
->fileProgress
.BytesTransferred
= totalTransferred
.QuadPart
;
212 job
->jobProgress
.BytesTransferred
+= diff
;
213 LeaveCriticalSection(&job
->cs
);
215 return (job
->state
== BG_JOB_STATE_TRANSFERRING
222 IBindStatusCallback IBindStatusCallback_iface
;
223 BackgroundCopyFileImpl
*file
;
225 } DLBindStatusCallback
;
227 static inline DLBindStatusCallback
*impl_from_IBindStatusCallback(IBindStatusCallback
*iface
)
229 return CONTAINING_RECORD(iface
, DLBindStatusCallback
, IBindStatusCallback_iface
);
232 static ULONG WINAPI
DLBindStatusCallback_AddRef(IBindStatusCallback
*iface
)
234 DLBindStatusCallback
*This
= impl_from_IBindStatusCallback(iface
);
235 return InterlockedIncrement(&This
->ref
);
238 static ULONG WINAPI
DLBindStatusCallback_Release(IBindStatusCallback
*iface
)
240 DLBindStatusCallback
*This
= impl_from_IBindStatusCallback(iface
);
241 ULONG ref
= InterlockedDecrement(&This
->ref
);
245 IBackgroundCopyFile_Release(&This
->file
->IBackgroundCopyFile_iface
);
246 HeapFree(GetProcessHeap(), 0, This
);
252 static HRESULT WINAPI
DLBindStatusCallback_QueryInterface(
253 IBindStatusCallback
*iface
,
257 DLBindStatusCallback
*This
= impl_from_IBindStatusCallback(iface
);
259 if (IsEqualGUID(riid
, &IID_IUnknown
)
260 || IsEqualGUID(riid
, &IID_IBindStatusCallback
))
262 *ppvObject
= &This
->IBindStatusCallback_iface
;
263 DLBindStatusCallback_AddRef(iface
);
268 return E_NOINTERFACE
;
271 static HRESULT WINAPI
DLBindStatusCallback_GetBindInfo(
272 IBindStatusCallback
*iface
,
279 static HRESULT WINAPI
DLBindStatusCallback_GetPriority(
280 IBindStatusCallback
*iface
,
286 static HRESULT WINAPI
DLBindStatusCallback_OnDataAvailable(
287 IBindStatusCallback
*iface
,
290 FORMATETC
*pformatetc
,
296 static HRESULT WINAPI
DLBindStatusCallback_OnLowResource(
297 IBindStatusCallback
*iface
,
303 static HRESULT WINAPI
DLBindStatusCallback_OnObjectAvailable(
304 IBindStatusCallback
*iface
,
311 static HRESULT WINAPI
DLBindStatusCallback_OnProgress(
312 IBindStatusCallback
*iface
,
318 DLBindStatusCallback
*This
= impl_from_IBindStatusCallback(iface
);
319 BackgroundCopyFileImpl
*file
= This
->file
;
320 BackgroundCopyJobImpl
*job
= file
->owner
;
323 EnterCriticalSection(&job
->cs
);
324 diff
= (file
->fileProgress
.BytesTotal
== BG_SIZE_UNKNOWN
326 : progress
- file
->fileProgress
.BytesTransferred
);
327 file
->fileProgress
.BytesTotal
= progressMax
? progressMax
: BG_SIZE_UNKNOWN
;
328 file
->fileProgress
.BytesTransferred
= progress
;
329 job
->jobProgress
.BytesTransferred
+= diff
;
330 LeaveCriticalSection(&job
->cs
);
335 static HRESULT WINAPI
DLBindStatusCallback_OnStartBinding(
336 IBindStatusCallback
*iface
,
343 static HRESULT WINAPI
DLBindStatusCallback_OnStopBinding(
344 IBindStatusCallback
*iface
,
351 static const IBindStatusCallbackVtbl DLBindStatusCallback_Vtbl
=
353 DLBindStatusCallback_QueryInterface
,
354 DLBindStatusCallback_AddRef
,
355 DLBindStatusCallback_Release
,
356 DLBindStatusCallback_OnStartBinding
,
357 DLBindStatusCallback_GetPriority
,
358 DLBindStatusCallback_OnLowResource
,
359 DLBindStatusCallback_OnProgress
,
360 DLBindStatusCallback_OnStopBinding
,
361 DLBindStatusCallback_GetBindInfo
,
362 DLBindStatusCallback_OnDataAvailable
,
363 DLBindStatusCallback_OnObjectAvailable
366 static DLBindStatusCallback
*DLBindStatusCallbackConstructor(
367 BackgroundCopyFileImpl
*file
)
369 DLBindStatusCallback
*This
= HeapAlloc(GetProcessHeap(), 0, sizeof *This
);
373 This
->IBindStatusCallback_iface
.lpVtbl
= &DLBindStatusCallback_Vtbl
;
374 IBackgroundCopyFile_AddRef(&file
->IBackgroundCopyFile_iface
);
380 BOOL
processFile(BackgroundCopyFileImpl
*file
, BackgroundCopyJobImpl
*job
)
382 static const WCHAR prefix
[] = {'B','I','T', 0};
383 DLBindStatusCallback
*callbackObj
;
384 WCHAR tmpDir
[MAX_PATH
];
385 WCHAR tmpName
[MAX_PATH
];
388 if (!GetTempPathW(MAX_PATH
, tmpDir
))
390 ERR("Couldn't create temp file name: %d\n", GetLastError());
391 /* Guessing on what state this should give us */
392 transitionJobState(job
, BG_JOB_STATE_QUEUED
, BG_JOB_STATE_TRANSIENT_ERROR
);
396 if (!GetTempFileNameW(tmpDir
, prefix
, 0, tmpName
))
398 ERR("Couldn't create temp file: %d\n", GetLastError());
399 /* Guessing on what state this should give us */
400 transitionJobState(job
, BG_JOB_STATE_QUEUED
, BG_JOB_STATE_TRANSIENT_ERROR
);
404 callbackObj
= DLBindStatusCallbackConstructor(file
);
407 ERR("Out of memory\n");
408 transitionJobState(job
, BG_JOB_STATE_QUEUED
, BG_JOB_STATE_TRANSIENT_ERROR
);
412 EnterCriticalSection(&job
->cs
);
413 file
->fileProgress
.BytesTotal
= BG_SIZE_UNKNOWN
;
414 file
->fileProgress
.BytesTransferred
= 0;
415 file
->fileProgress
.Completed
= FALSE
;
416 LeaveCriticalSection(&job
->cs
);
418 TRACE("Transferring: %s -> %s -> %s\n",
419 debugstr_w(file
->info
.RemoteName
),
421 debugstr_w(file
->info
.LocalName
));
423 transitionJobState(job
, BG_JOB_STATE_QUEUED
, BG_JOB_STATE_TRANSFERRING
);
425 DeleteUrlCacheEntryW(file
->info
.RemoteName
);
426 hr
= URLDownloadToFileW(NULL
, file
->info
.RemoteName
, tmpName
, 0,
427 &callbackObj
->IBindStatusCallback_iface
);
428 IBindStatusCallback_Release(&callbackObj
->IBindStatusCallback_iface
);
429 if (hr
== INET_E_DOWNLOAD_FAILURE
)
431 TRACE("URLDownload failed, trying local file copy\n");
432 if (!CopyFileExW(file
->info
.RemoteName
, tmpName
, copyProgressCallback
,
435 ERR("Local file copy failed: error %d\n", GetLastError());
436 transitionJobState(job
, BG_JOB_STATE_TRANSFERRING
, BG_JOB_STATE_ERROR
);
442 ERR("URLDownload failed: eh 0x%08x\n", hr
);
443 transitionJobState(job
, BG_JOB_STATE_TRANSFERRING
, BG_JOB_STATE_ERROR
);
447 if (transitionJobState(job
, BG_JOB_STATE_TRANSFERRING
, BG_JOB_STATE_QUEUED
))
449 lstrcpyW(file
->tempFileName
, tmpName
);
451 EnterCriticalSection(&job
->cs
);
452 file
->fileProgress
.Completed
= TRUE
;
453 job
->jobProgress
.FilesTransferred
++;
454 LeaveCriticalSection(&job
->cs
);
460 DeleteFileW(tmpName
);