1 | /* $Id: ProgressImpl.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * VirtualBox Progress COM class implementation
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2009 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 | #include <iprt/types.h>
|
---|
20 |
|
---|
21 | #if defined (VBOX_WITH_XPCOM)
|
---|
22 | #include <nsIServiceManager.h>
|
---|
23 | #include <nsIExceptionService.h>
|
---|
24 | #include <nsCOMPtr.h>
|
---|
25 | #endif /* defined (VBOX_WITH_XPCOM) */
|
---|
26 |
|
---|
27 | #include "ProgressCombinedImpl.h"
|
---|
28 |
|
---|
29 | #include "VirtualBoxImpl.h"
|
---|
30 | #include "VirtualBoxErrorInfoImpl.h"
|
---|
31 |
|
---|
32 | #include "Logging.h"
|
---|
33 |
|
---|
34 | #include <iprt/time.h>
|
---|
35 | #include <iprt/semaphore.h>
|
---|
36 |
|
---|
37 | #include <VBox/err.h>
|
---|
38 |
|
---|
39 | ////////////////////////////////////////////////////////////////////////////////
|
---|
40 | // ProgressBase class
|
---|
41 | ////////////////////////////////////////////////////////////////////////////////
|
---|
42 |
|
---|
43 | // constructor / destructor
|
---|
44 | ////////////////////////////////////////////////////////////////////////////////
|
---|
45 |
|
---|
46 | ProgressBase::ProgressBase()
|
---|
47 | #if !defined (VBOX_COM_INPROC)
|
---|
48 | : mParent(NULL)
|
---|
49 | #endif
|
---|
50 | {
|
---|
51 | }
|
---|
52 |
|
---|
53 | ProgressBase::~ProgressBase()
|
---|
54 | {
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Subclasses must call this method from their FinalConstruct() implementations.
|
---|
60 | */
|
---|
61 | HRESULT ProgressBase::FinalConstruct()
|
---|
62 | {
|
---|
63 | mCancelable = FALSE;
|
---|
64 | mCompleted = FALSE;
|
---|
65 | mCanceled = FALSE;
|
---|
66 | mResultCode = S_OK;
|
---|
67 |
|
---|
68 | m_cOperations
|
---|
69 | = m_ulTotalOperationsWeight
|
---|
70 | = m_ulOperationsCompletedWeight
|
---|
71 | = m_ulCurrentOperation
|
---|
72 | = m_ulCurrentOperationWeight
|
---|
73 | = m_ulOperationPercent
|
---|
74 | = m_cMsTimeout
|
---|
75 | = 0;
|
---|
76 |
|
---|
77 | // get creation timestamp
|
---|
78 | m_ullTimestamp = RTTimeMilliTS();
|
---|
79 |
|
---|
80 | m_pfnCancelCallback = NULL;
|
---|
81 | m_pvCancelUserArg = NULL;
|
---|
82 |
|
---|
83 | return S_OK;
|
---|
84 | }
|
---|
85 |
|
---|
86 | // protected initializer/uninitializer for internal purposes only
|
---|
87 | ////////////////////////////////////////////////////////////////////////////////
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Initializes the progress base object.
|
---|
91 | *
|
---|
92 | * Subclasses should call this or any other #protectedInit() method from their
|
---|
93 | * init() implementations.
|
---|
94 | *
|
---|
95 | * @param aAutoInitSpan AutoInitSpan object instantiated by a subclass.
|
---|
96 | * @param aParent Parent object (only for server-side Progress objects).
|
---|
97 | * @param aInitiator Initiator of the task (for server-side objects. Can be
|
---|
98 | * NULL which means initiator = parent, otherwise must not
|
---|
99 | * be NULL).
|
---|
100 | * @param aDescription ask description.
|
---|
101 | * @param aID Address of result GUID structure (optional).
|
---|
102 | *
|
---|
103 | * @return COM result indicator.
|
---|
104 | */
|
---|
105 | HRESULT ProgressBase::protectedInit (AutoInitSpan &aAutoInitSpan,
|
---|
106 | #if !defined (VBOX_COM_INPROC)
|
---|
107 | VirtualBox *aParent,
|
---|
108 | #endif
|
---|
109 | IUnknown *aInitiator,
|
---|
110 | CBSTR aDescription, OUT_GUID aId /* = NULL */)
|
---|
111 | {
|
---|
112 | /* Guarantees subclasses call this method at the proper time */
|
---|
113 | NOREF (aAutoInitSpan);
|
---|
114 |
|
---|
115 | AutoCaller autoCaller(this);
|
---|
116 | AssertReturn(autoCaller.state() == InInit, E_FAIL);
|
---|
117 |
|
---|
118 | #if !defined (VBOX_COM_INPROC)
|
---|
119 | AssertReturn(aParent, E_INVALIDARG);
|
---|
120 | #else
|
---|
121 | AssertReturn(aInitiator, E_INVALIDARG);
|
---|
122 | #endif
|
---|
123 |
|
---|
124 | AssertReturn(aDescription, E_INVALIDARG);
|
---|
125 |
|
---|
126 | #if !defined (VBOX_COM_INPROC)
|
---|
127 | /* share parent weakly */
|
---|
128 | unconst(mParent) = aParent;
|
---|
129 | #endif
|
---|
130 |
|
---|
131 | #if !defined (VBOX_COM_INPROC)
|
---|
132 | /* assign (and therefore addref) initiator only if it is not VirtualBox
|
---|
133 | * (to avoid cycling); otherwise mInitiator will remain null which means
|
---|
134 | * that it is the same as the parent */
|
---|
135 | if (aInitiator)
|
---|
136 | {
|
---|
137 | ComObjPtr<VirtualBox> pVirtualBox(mParent);
|
---|
138 | if (!pVirtualBox.equalsTo(aInitiator))
|
---|
139 | unconst(mInitiator) = aInitiator;
|
---|
140 | }
|
---|
141 | #else
|
---|
142 | unconst(mInitiator) = aInitiator;
|
---|
143 | #endif
|
---|
144 |
|
---|
145 | unconst(mId).create();
|
---|
146 | if (aId)
|
---|
147 | mId.cloneTo(aId);
|
---|
148 |
|
---|
149 | #if !defined (VBOX_COM_INPROC)
|
---|
150 | /* add to the global collection of progress operations (note: after
|
---|
151 | * creating mId) */
|
---|
152 | mParent->addProgress(this);
|
---|
153 | #endif
|
---|
154 |
|
---|
155 | unconst(mDescription) = aDescription;
|
---|
156 |
|
---|
157 | return S_OK;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Initializes the progress base object.
|
---|
162 | *
|
---|
163 | * This is a special initializer that doesn't initialize any field. Used by one
|
---|
164 | * of the Progress::init() forms to create sub-progress operations combined
|
---|
165 | * together using a CombinedProgress instance, so it doesn't require the parent,
|
---|
166 | * initiator, description and doesn't create an ID.
|
---|
167 | *
|
---|
168 | * Subclasses should call this or any other #protectedInit() method from their
|
---|
169 | * init() implementations.
|
---|
170 | *
|
---|
171 | * @param aAutoInitSpan AutoInitSpan object instantiated by a subclass.
|
---|
172 | */
|
---|
173 | HRESULT ProgressBase::protectedInit (AutoInitSpan &aAutoInitSpan)
|
---|
174 | {
|
---|
175 | /* Guarantees subclasses call this method at the proper time */
|
---|
176 | NOREF (aAutoInitSpan);
|
---|
177 |
|
---|
178 | return S_OK;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Uninitializes the instance.
|
---|
183 | *
|
---|
184 | * Subclasses should call this from their uninit() implementations.
|
---|
185 | *
|
---|
186 | * @param aAutoUninitSpan AutoUninitSpan object instantiated by a subclass.
|
---|
187 | *
|
---|
188 | * @note Using the mParent member after this method returns is forbidden.
|
---|
189 | */
|
---|
190 | void ProgressBase::protectedUninit (AutoUninitSpan &aAutoUninitSpan)
|
---|
191 | {
|
---|
192 | /* release initiator (effective only if mInitiator has been assigned in
|
---|
193 | * init()) */
|
---|
194 | unconst(mInitiator).setNull();
|
---|
195 |
|
---|
196 | #if !defined (VBOX_COM_INPROC)
|
---|
197 | if (mParent)
|
---|
198 | {
|
---|
199 | /* remove the added progress on failure to complete the initialization */
|
---|
200 | if (aAutoUninitSpan.initFailed() && !mId.isEmpty())
|
---|
201 | mParent->removeProgress (mId);
|
---|
202 |
|
---|
203 | unconst(mParent) = NULL;
|
---|
204 | }
|
---|
205 | #endif
|
---|
206 | }
|
---|
207 |
|
---|
208 | // IProgress properties
|
---|
209 | /////////////////////////////////////////////////////////////////////////////
|
---|
210 |
|
---|
211 | STDMETHODIMP ProgressBase::COMGETTER(Id) (BSTR *aId)
|
---|
212 | {
|
---|
213 | CheckComArgOutPointerValid(aId);
|
---|
214 |
|
---|
215 | AutoCaller autoCaller(this);
|
---|
216 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
217 |
|
---|
218 | /* mId is constant during life time, no need to lock */
|
---|
219 | mId.toUtf16().cloneTo(aId);
|
---|
220 |
|
---|
221 | return S_OK;
|
---|
222 | }
|
---|
223 |
|
---|
224 | STDMETHODIMP ProgressBase::COMGETTER(Description) (BSTR *aDescription)
|
---|
225 | {
|
---|
226 | CheckComArgOutPointerValid(aDescription);
|
---|
227 |
|
---|
228 | AutoCaller autoCaller(this);
|
---|
229 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
230 |
|
---|
231 | /* mDescription is constant during life time, no need to lock */
|
---|
232 | mDescription.cloneTo(aDescription);
|
---|
233 |
|
---|
234 | return S_OK;
|
---|
235 | }
|
---|
236 |
|
---|
237 | STDMETHODIMP ProgressBase::COMGETTER(Initiator) (IUnknown **aInitiator)
|
---|
238 | {
|
---|
239 | CheckComArgOutPointerValid(aInitiator);
|
---|
240 |
|
---|
241 | AutoCaller autoCaller(this);
|
---|
242 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
243 |
|
---|
244 | /* mInitiator/mParent are constant during life time, no need to lock */
|
---|
245 |
|
---|
246 | #if !defined (VBOX_COM_INPROC)
|
---|
247 | if (mInitiator)
|
---|
248 | mInitiator.queryInterfaceTo(aInitiator);
|
---|
249 | else
|
---|
250 | {
|
---|
251 | ComObjPtr<VirtualBox> pVirtualBox(mParent);
|
---|
252 | pVirtualBox.queryInterfaceTo(aInitiator);
|
---|
253 | }
|
---|
254 | #else
|
---|
255 | mInitiator.queryInterfaceTo(aInitiator);
|
---|
256 | #endif
|
---|
257 |
|
---|
258 | return S_OK;
|
---|
259 | }
|
---|
260 |
|
---|
261 | STDMETHODIMP ProgressBase::COMGETTER(Cancelable) (BOOL *aCancelable)
|
---|
262 | {
|
---|
263 | CheckComArgOutPointerValid(aCancelable);
|
---|
264 |
|
---|
265 | AutoCaller autoCaller(this);
|
---|
266 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
267 |
|
---|
268 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
269 |
|
---|
270 | *aCancelable = mCancelable;
|
---|
271 |
|
---|
272 | return S_OK;
|
---|
273 | }
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Internal helper to compute the total percent value based on the member values and
|
---|
277 | * returns it as a "double". This is used both by GetPercent (which returns it as a
|
---|
278 | * rounded ULONG) and GetTimeRemaining().
|
---|
279 | *
|
---|
280 | * Requires locking by the caller!
|
---|
281 | *
|
---|
282 | * @return fractional percentage as a double value.
|
---|
283 | */
|
---|
284 | double ProgressBase::calcTotalPercent()
|
---|
285 | {
|
---|
286 | // avoid division by zero
|
---|
287 | if (m_ulTotalOperationsWeight == 0)
|
---|
288 | return 0;
|
---|
289 |
|
---|
290 | double dPercent = ( (double)m_ulOperationsCompletedWeight // weight of operations that have been completed
|
---|
291 | + ((double)m_ulOperationPercent * (double)m_ulCurrentOperationWeight / (double)100) // plus partial weight of the current operation
|
---|
292 | ) * (double)100 / (double)m_ulTotalOperationsWeight;
|
---|
293 |
|
---|
294 | return dPercent;
|
---|
295 | }
|
---|
296 |
|
---|
297 | /**
|
---|
298 | * Internal helper for automatically timing out the operation.
|
---|
299 | *
|
---|
300 | * The caller should hold the object write lock.
|
---|
301 | */
|
---|
302 | void ProgressBase::checkForAutomaticTimeout(void)
|
---|
303 | {
|
---|
304 | if ( m_cMsTimeout
|
---|
305 | && mCancelable
|
---|
306 | && !mCanceled
|
---|
307 | && RTTimeMilliTS() - m_ullTimestamp > m_cMsTimeout
|
---|
308 | )
|
---|
309 | Cancel();
|
---|
310 | }
|
---|
311 |
|
---|
312 |
|
---|
313 | STDMETHODIMP ProgressBase::COMGETTER(TimeRemaining)(LONG *aTimeRemaining)
|
---|
314 | {
|
---|
315 | CheckComArgOutPointerValid(aTimeRemaining);
|
---|
316 |
|
---|
317 | AutoCaller autoCaller(this);
|
---|
318 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
319 |
|
---|
320 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
321 |
|
---|
322 | if (mCompleted)
|
---|
323 | *aTimeRemaining = 0;
|
---|
324 | else
|
---|
325 | {
|
---|
326 | double dPercentDone = calcTotalPercent();
|
---|
327 | if (dPercentDone < 1)
|
---|
328 | *aTimeRemaining = -1; // unreliable, or avoid division by 0 below
|
---|
329 | else
|
---|
330 | {
|
---|
331 | uint64_t ullTimeNow = RTTimeMilliTS();
|
---|
332 | uint64_t ullTimeElapsed = ullTimeNow - m_ullTimestamp;
|
---|
333 | uint64_t ullTimeTotal = (uint64_t)(ullTimeElapsed / dPercentDone * 100);
|
---|
334 | uint64_t ullTimeRemaining = ullTimeTotal - ullTimeElapsed;
|
---|
335 |
|
---|
336 | // Log(("ProgressBase::GetTimeRemaining: dPercentDone %RI32, ullTimeNow = %RI64, ullTimeElapsed = %RI64, ullTimeTotal = %RI64, ullTimeRemaining = %RI64\n",
|
---|
337 | // (uint32_t)dPercentDone, ullTimeNow, ullTimeElapsed, ullTimeTotal, ullTimeRemaining));
|
---|
338 |
|
---|
339 | *aTimeRemaining = (LONG)(ullTimeRemaining / 1000);
|
---|
340 | }
|
---|
341 | }
|
---|
342 |
|
---|
343 | return S_OK;
|
---|
344 | }
|
---|
345 |
|
---|
346 | STDMETHODIMP ProgressBase::COMGETTER(Percent)(ULONG *aPercent)
|
---|
347 | {
|
---|
348 | CheckComArgOutPointerValid(aPercent);
|
---|
349 |
|
---|
350 | AutoCaller autoCaller(this);
|
---|
351 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
352 |
|
---|
353 | checkForAutomaticTimeout();
|
---|
354 |
|
---|
355 | /* checkForAutomaticTimeout requires a write lock. */
|
---|
356 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
357 |
|
---|
358 | if (mCompleted && SUCCEEDED(mResultCode))
|
---|
359 | *aPercent = 100;
|
---|
360 | else
|
---|
361 | {
|
---|
362 | ULONG ulPercent = (ULONG)calcTotalPercent();
|
---|
363 | // do not report 100% until we're really really done with everything as the Qt GUI dismisses progress dialogs in that case
|
---|
364 | if ( ulPercent == 100
|
---|
365 | && ( m_ulOperationPercent < 100
|
---|
366 | || (m_ulCurrentOperation < m_cOperations -1)
|
---|
367 | )
|
---|
368 | )
|
---|
369 | *aPercent = 99;
|
---|
370 | else
|
---|
371 | *aPercent = ulPercent;
|
---|
372 | }
|
---|
373 |
|
---|
374 | checkForAutomaticTimeout();
|
---|
375 |
|
---|
376 | return S_OK;
|
---|
377 | }
|
---|
378 |
|
---|
379 | STDMETHODIMP ProgressBase::COMGETTER(Completed) (BOOL *aCompleted)
|
---|
380 | {
|
---|
381 | CheckComArgOutPointerValid(aCompleted);
|
---|
382 |
|
---|
383 | AutoCaller autoCaller(this);
|
---|
384 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
385 |
|
---|
386 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
387 |
|
---|
388 | *aCompleted = mCompleted;
|
---|
389 |
|
---|
390 | return S_OK;
|
---|
391 | }
|
---|
392 |
|
---|
393 | STDMETHODIMP ProgressBase::COMGETTER(Canceled) (BOOL *aCanceled)
|
---|
394 | {
|
---|
395 | CheckComArgOutPointerValid(aCanceled);
|
---|
396 |
|
---|
397 | AutoCaller autoCaller(this);
|
---|
398 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
399 |
|
---|
400 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
401 |
|
---|
402 | *aCanceled = mCanceled;
|
---|
403 |
|
---|
404 | return S_OK;
|
---|
405 | }
|
---|
406 |
|
---|
407 | STDMETHODIMP ProgressBase::COMGETTER(ResultCode) (LONG *aResultCode)
|
---|
408 | {
|
---|
409 | CheckComArgOutPointerValid(aResultCode);
|
---|
410 |
|
---|
411 | AutoCaller autoCaller(this);
|
---|
412 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
413 |
|
---|
414 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
415 |
|
---|
416 | if (!mCompleted)
|
---|
417 | return setError(E_FAIL,
|
---|
418 | tr("Result code is not available, operation is still in progress"));
|
---|
419 |
|
---|
420 | *aResultCode = mResultCode;
|
---|
421 |
|
---|
422 | return S_OK;
|
---|
423 | }
|
---|
424 |
|
---|
425 | STDMETHODIMP ProgressBase::COMGETTER(ErrorInfo) (IVirtualBoxErrorInfo **aErrorInfo)
|
---|
426 | {
|
---|
427 | CheckComArgOutPointerValid(aErrorInfo);
|
---|
428 |
|
---|
429 | AutoCaller autoCaller(this);
|
---|
430 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
431 |
|
---|
432 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
433 |
|
---|
434 | if (!mCompleted)
|
---|
435 | return setError(E_FAIL,
|
---|
436 | tr("Error info is not available, operation is still in progress"));
|
---|
437 |
|
---|
438 | mErrorInfo.queryInterfaceTo(aErrorInfo);
|
---|
439 |
|
---|
440 | return S_OK;
|
---|
441 | }
|
---|
442 |
|
---|
443 | STDMETHODIMP ProgressBase::COMGETTER(OperationCount) (ULONG *aOperationCount)
|
---|
444 | {
|
---|
445 | CheckComArgOutPointerValid(aOperationCount);
|
---|
446 |
|
---|
447 | AutoCaller autoCaller(this);
|
---|
448 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
449 |
|
---|
450 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
451 |
|
---|
452 | *aOperationCount = m_cOperations;
|
---|
453 |
|
---|
454 | return S_OK;
|
---|
455 | }
|
---|
456 |
|
---|
457 | STDMETHODIMP ProgressBase::COMGETTER(Operation) (ULONG *aOperation)
|
---|
458 | {
|
---|
459 | CheckComArgOutPointerValid(aOperation);
|
---|
460 |
|
---|
461 | AutoCaller autoCaller(this);
|
---|
462 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
463 |
|
---|
464 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
465 |
|
---|
466 | *aOperation = m_ulCurrentOperation;
|
---|
467 |
|
---|
468 | return S_OK;
|
---|
469 | }
|
---|
470 |
|
---|
471 | STDMETHODIMP ProgressBase::COMGETTER(OperationDescription) (BSTR *aOperationDescription)
|
---|
472 | {
|
---|
473 | CheckComArgOutPointerValid(aOperationDescription);
|
---|
474 |
|
---|
475 | AutoCaller autoCaller(this);
|
---|
476 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
477 |
|
---|
478 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
479 |
|
---|
480 | m_bstrOperationDescription.cloneTo(aOperationDescription);
|
---|
481 |
|
---|
482 | return S_OK;
|
---|
483 | }
|
---|
484 |
|
---|
485 | STDMETHODIMP ProgressBase::COMGETTER(OperationPercent)(ULONG *aOperationPercent)
|
---|
486 | {
|
---|
487 | CheckComArgOutPointerValid(aOperationPercent);
|
---|
488 |
|
---|
489 | AutoCaller autoCaller(this);
|
---|
490 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
491 |
|
---|
492 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
493 |
|
---|
494 | if (mCompleted && SUCCEEDED(mResultCode))
|
---|
495 | *aOperationPercent = 100;
|
---|
496 | else
|
---|
497 | *aOperationPercent = m_ulOperationPercent;
|
---|
498 |
|
---|
499 | return S_OK;
|
---|
500 | }
|
---|
501 |
|
---|
502 | STDMETHODIMP ProgressBase::COMSETTER(Timeout)(ULONG aTimeout)
|
---|
503 | {
|
---|
504 | AutoCaller autoCaller(this);
|
---|
505 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
506 |
|
---|
507 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
508 |
|
---|
509 | if (!mCancelable)
|
---|
510 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
511 | tr("Operation cannot be canceled"));
|
---|
512 |
|
---|
513 | LogThisFunc(("%#x => %#x\n", m_cMsTimeout, aTimeout));
|
---|
514 | m_cMsTimeout = aTimeout;
|
---|
515 | return S_OK;
|
---|
516 | }
|
---|
517 |
|
---|
518 | STDMETHODIMP ProgressBase::COMGETTER(Timeout)(ULONG *aTimeout)
|
---|
519 | {
|
---|
520 | CheckComArgOutPointerValid(aTimeout);
|
---|
521 |
|
---|
522 | AutoCaller autoCaller(this);
|
---|
523 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
524 |
|
---|
525 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
526 |
|
---|
527 | *aTimeout = m_cMsTimeout;
|
---|
528 | return S_OK;
|
---|
529 | }
|
---|
530 |
|
---|
531 | // public methods only for internal purposes
|
---|
532 | ////////////////////////////////////////////////////////////////////////////////
|
---|
533 |
|
---|
534 | /**
|
---|
535 | * Sets the error info stored in the given progress object as the error info on
|
---|
536 | * the current thread.
|
---|
537 | *
|
---|
538 | * This method is useful if some other COM method uses IProgress to wait for
|
---|
539 | * something and then wants to return a failed result of the operation it was
|
---|
540 | * waiting for as its own result retaining the extended error info.
|
---|
541 | *
|
---|
542 | * If the operation tracked by this progress object is completed successfully
|
---|
543 | * and returned S_OK, this method does nothing but returns S_OK. Otherwise, the
|
---|
544 | * failed warning or error result code specified at progress completion is
|
---|
545 | * returned and the extended error info object (if any) is set on the current
|
---|
546 | * thread.
|
---|
547 | *
|
---|
548 | * Note that the given progress object must be completed, otherwise this method
|
---|
549 | * will assert and fail.
|
---|
550 | */
|
---|
551 | /* static */
|
---|
552 | HRESULT ProgressBase::setErrorInfoOnThread (IProgress *aProgress)
|
---|
553 | {
|
---|
554 | AssertReturn(aProgress != NULL, E_INVALIDARG);
|
---|
555 |
|
---|
556 | LONG iRc;
|
---|
557 | HRESULT rc = aProgress->COMGETTER(ResultCode) (&iRc);
|
---|
558 | AssertComRCReturnRC(rc);
|
---|
559 | HRESULT resultCode = iRc;
|
---|
560 |
|
---|
561 | if (resultCode == S_OK)
|
---|
562 | return resultCode;
|
---|
563 |
|
---|
564 | ComPtr<IVirtualBoxErrorInfo> errorInfo;
|
---|
565 | rc = aProgress->COMGETTER(ErrorInfo) (errorInfo.asOutParam());
|
---|
566 | AssertComRCReturnRC(rc);
|
---|
567 |
|
---|
568 | if (!errorInfo.isNull())
|
---|
569 | setErrorInfo (errorInfo);
|
---|
570 |
|
---|
571 | return resultCode;
|
---|
572 | }
|
---|
573 |
|
---|
574 | /**
|
---|
575 | * Sets the cancelation callback, checking for cancelation first.
|
---|
576 | *
|
---|
577 | * @returns Success indicator.
|
---|
578 | * @retval true on success.
|
---|
579 | * @retval false if the progress object has already been canceled or is in an
|
---|
580 | * invalid state
|
---|
581 | *
|
---|
582 | * @param pfnCallback The function to be called upon cancelation.
|
---|
583 | * @param pvUser The callback argument.
|
---|
584 | */
|
---|
585 | bool ProgressBase::setCancelCallback(void (*pfnCallback)(void *), void *pvUser)
|
---|
586 | {
|
---|
587 | AutoCaller autoCaller(this);
|
---|
588 | AssertComRCReturn(autoCaller.rc(), false);
|
---|
589 |
|
---|
590 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
591 |
|
---|
592 | checkForAutomaticTimeout();
|
---|
593 | if (mCanceled)
|
---|
594 | return false;
|
---|
595 |
|
---|
596 | m_pvCancelUserArg = pvUser;
|
---|
597 | m_pfnCancelCallback = pfnCallback;
|
---|
598 | return true;
|
---|
599 | }
|
---|
600 |
|
---|
601 | ////////////////////////////////////////////////////////////////////////////////
|
---|
602 | // Progress class
|
---|
603 | ////////////////////////////////////////////////////////////////////////////////
|
---|
604 |
|
---|
605 | HRESULT Progress::FinalConstruct()
|
---|
606 | {
|
---|
607 | HRESULT rc = ProgressBase::FinalConstruct();
|
---|
608 | if (FAILED(rc)) return rc;
|
---|
609 |
|
---|
610 | mCompletedSem = NIL_RTSEMEVENTMULTI;
|
---|
611 | mWaitersCount = 0;
|
---|
612 |
|
---|
613 | return S_OK;
|
---|
614 | }
|
---|
615 |
|
---|
616 | void Progress::FinalRelease()
|
---|
617 | {
|
---|
618 | uninit();
|
---|
619 | }
|
---|
620 |
|
---|
621 | // public initializer/uninitializer for internal purposes only
|
---|
622 | ////////////////////////////////////////////////////////////////////////////////
|
---|
623 |
|
---|
624 | /**
|
---|
625 | * Initializes the normal progress object. With this variant, one can have
|
---|
626 | * an arbitrary number of sub-operation which IProgress can analyze to
|
---|
627 | * have a weighted progress computed.
|
---|
628 | *
|
---|
629 | * For example, say that one IProgress is supposed to track the cloning
|
---|
630 | * of two hard disk images, which are 100 MB and 1000 MB in size, respectively,
|
---|
631 | * and each of these hard disks should be one sub-operation of the IProgress.
|
---|
632 | *
|
---|
633 | * Obviously the progress would be misleading if the progress displayed 50%
|
---|
634 | * after the smaller image was cloned and would then take much longer for
|
---|
635 | * the second half.
|
---|
636 | *
|
---|
637 | * With weighted progress, one can invoke the following calls:
|
---|
638 | *
|
---|
639 | * 1) create progress object with cOperations = 2 and ulTotalOperationsWeight =
|
---|
640 | * 1100 (100 MB plus 1100, but really the weights can be any ULONG); pass
|
---|
641 | * in ulFirstOperationWeight = 100 for the first sub-operation
|
---|
642 | *
|
---|
643 | * 2) Then keep calling setCurrentOperationProgress() with a percentage
|
---|
644 | * for the first image; the total progress will increase up to a value
|
---|
645 | * of 9% (100MB / 1100MB * 100%).
|
---|
646 | *
|
---|
647 | * 3) Then call setNextOperation with the second weight (1000 for the megabytes
|
---|
648 | * of the second disk).
|
---|
649 | *
|
---|
650 | * 4) Then keep calling setCurrentOperationProgress() with a percentage for
|
---|
651 | * the second image, where 100% of the operation will then yield a 100%
|
---|
652 | * progress of the entire task.
|
---|
653 | *
|
---|
654 | * Weighting is optional; you can simply assign a weight of 1 to each operation
|
---|
655 | * and pass ulTotalOperationsWeight == cOperations to this constructor (but
|
---|
656 | * for that variant and for backwards-compatibility a simpler constructor exists
|
---|
657 | * in ProgressImpl.h as well).
|
---|
658 | *
|
---|
659 | * Even simpler, if you need no sub-operations at all, pass in cOperations =
|
---|
660 | * ulTotalOperationsWeight = ulFirstOperationWeight = 1.
|
---|
661 | *
|
---|
662 | * @param aParent See ProgressBase::init().
|
---|
663 | * @param aInitiator See ProgressBase::init().
|
---|
664 | * @param aDescription See ProgressBase::init().
|
---|
665 | * @param aCancelable Flag whether the task maybe canceled.
|
---|
666 | * @param cOperations Number of operations within this task (at least 1).
|
---|
667 | * @param ulTotalOperationsWeight Total weight of operations; must be the sum of ulFirstOperationWeight and
|
---|
668 | * what is later passed with each subsequent setNextOperation() call.
|
---|
669 | * @param bstrFirstOperationDescription Description of the first operation.
|
---|
670 | * @param ulFirstOperationWeight Weight of first sub-operation.
|
---|
671 | * @param aId See ProgressBase::init().
|
---|
672 | */
|
---|
673 | HRESULT Progress::init (
|
---|
674 | #if !defined (VBOX_COM_INPROC)
|
---|
675 | VirtualBox *aParent,
|
---|
676 | #endif
|
---|
677 | IUnknown *aInitiator,
|
---|
678 | CBSTR aDescription,
|
---|
679 | BOOL aCancelable,
|
---|
680 | ULONG cOperations,
|
---|
681 | ULONG ulTotalOperationsWeight,
|
---|
682 | CBSTR bstrFirstOperationDescription,
|
---|
683 | ULONG ulFirstOperationWeight,
|
---|
684 | OUT_GUID aId /* = NULL */)
|
---|
685 | {
|
---|
686 | LogFlowThisFunc(("aDescription=\"%ls\", cOperations=%d, ulTotalOperationsWeight=%d, bstrFirstOperationDescription=\"%ls\", ulFirstOperationWeight=%d\n",
|
---|
687 | aDescription,
|
---|
688 | cOperations,
|
---|
689 | ulTotalOperationsWeight,
|
---|
690 | bstrFirstOperationDescription,
|
---|
691 | ulFirstOperationWeight));
|
---|
692 |
|
---|
693 | AssertReturn(bstrFirstOperationDescription, E_INVALIDARG);
|
---|
694 | AssertReturn(ulTotalOperationsWeight >= 1, E_INVALIDARG);
|
---|
695 |
|
---|
696 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
697 | AutoInitSpan autoInitSpan(this);
|
---|
698 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
699 |
|
---|
700 | HRESULT rc = S_OK;
|
---|
701 |
|
---|
702 | rc = ProgressBase::protectedInit (autoInitSpan,
|
---|
703 | #if !defined (VBOX_COM_INPROC)
|
---|
704 | aParent,
|
---|
705 | #endif
|
---|
706 | aInitiator, aDescription, aId);
|
---|
707 | if (FAILED(rc)) return rc;
|
---|
708 |
|
---|
709 | mCancelable = aCancelable;
|
---|
710 |
|
---|
711 | m_cOperations = cOperations;
|
---|
712 | m_ulTotalOperationsWeight = ulTotalOperationsWeight;
|
---|
713 | m_ulOperationsCompletedWeight = 0;
|
---|
714 | m_ulCurrentOperation = 0;
|
---|
715 | m_bstrOperationDescription = bstrFirstOperationDescription;
|
---|
716 | m_ulCurrentOperationWeight = ulFirstOperationWeight;
|
---|
717 | m_ulOperationPercent = 0;
|
---|
718 |
|
---|
719 | int vrc = RTSemEventMultiCreate (&mCompletedSem);
|
---|
720 | ComAssertRCRet (vrc, E_FAIL);
|
---|
721 |
|
---|
722 | RTSemEventMultiReset (mCompletedSem);
|
---|
723 |
|
---|
724 | /* Confirm a successful initialization when it's the case */
|
---|
725 | if (SUCCEEDED(rc))
|
---|
726 | autoInitSpan.setSucceeded();
|
---|
727 |
|
---|
728 | return rc;
|
---|
729 | }
|
---|
730 |
|
---|
731 | /**
|
---|
732 | * Initializes the sub-progress object that represents a specific operation of
|
---|
733 | * the whole task.
|
---|
734 | *
|
---|
735 | * Objects initialized with this method are then combined together into the
|
---|
736 | * single task using a CombinedProgress instance, so it doesn't require the
|
---|
737 | * parent, initiator, description and doesn't create an ID. Note that calling
|
---|
738 | * respective getter methods on an object initialized with this method is
|
---|
739 | * useless. Such objects are used only to provide a separate wait semaphore and
|
---|
740 | * store individual operation descriptions.
|
---|
741 | *
|
---|
742 | * @param aCancelable Flag whether the task maybe canceled.
|
---|
743 | * @param aOperationCount Number of sub-operations within this task (at least 1).
|
---|
744 | * @param aOperationDescription Description of the individual operation.
|
---|
745 | */
|
---|
746 | HRESULT Progress::init(BOOL aCancelable,
|
---|
747 | ULONG aOperationCount,
|
---|
748 | CBSTR aOperationDescription)
|
---|
749 | {
|
---|
750 | LogFlowThisFunc(("aOperationDescription=\"%ls\"\n", aOperationDescription));
|
---|
751 |
|
---|
752 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
753 | AutoInitSpan autoInitSpan(this);
|
---|
754 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
755 |
|
---|
756 | HRESULT rc = S_OK;
|
---|
757 |
|
---|
758 | rc = ProgressBase::protectedInit (autoInitSpan);
|
---|
759 | if (FAILED(rc)) return rc;
|
---|
760 |
|
---|
761 | mCancelable = aCancelable;
|
---|
762 |
|
---|
763 | // for this variant we assume for now that all operations are weighed "1"
|
---|
764 | // and equal total weight = operation count
|
---|
765 | m_cOperations = aOperationCount;
|
---|
766 | m_ulTotalOperationsWeight = aOperationCount;
|
---|
767 | m_ulOperationsCompletedWeight = 0;
|
---|
768 | m_ulCurrentOperation = 0;
|
---|
769 | m_bstrOperationDescription = aOperationDescription;
|
---|
770 | m_ulCurrentOperationWeight = 1;
|
---|
771 | m_ulOperationPercent = 0;
|
---|
772 |
|
---|
773 | int vrc = RTSemEventMultiCreate (&mCompletedSem);
|
---|
774 | ComAssertRCRet (vrc, E_FAIL);
|
---|
775 |
|
---|
776 | RTSemEventMultiReset (mCompletedSem);
|
---|
777 |
|
---|
778 | /* Confirm a successful initialization when it's the case */
|
---|
779 | if (SUCCEEDED(rc))
|
---|
780 | autoInitSpan.setSucceeded();
|
---|
781 |
|
---|
782 | return rc;
|
---|
783 | }
|
---|
784 |
|
---|
785 | /**
|
---|
786 | * Uninitializes the instance and sets the ready flag to FALSE.
|
---|
787 | *
|
---|
788 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
789 | */
|
---|
790 | void Progress::uninit()
|
---|
791 | {
|
---|
792 | LogFlowThisFunc(("\n"));
|
---|
793 |
|
---|
794 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
795 | AutoUninitSpan autoUninitSpan(this);
|
---|
796 | if (autoUninitSpan.uninitDone())
|
---|
797 | return;
|
---|
798 |
|
---|
799 | /* wake up all threads still waiting on occasion */
|
---|
800 | if (mWaitersCount > 0)
|
---|
801 | {
|
---|
802 | LogFlow (("WARNING: There are still %d threads waiting for '%ls' completion!\n",
|
---|
803 | mWaitersCount, mDescription.raw()));
|
---|
804 | RTSemEventMultiSignal (mCompletedSem);
|
---|
805 | }
|
---|
806 |
|
---|
807 | RTSemEventMultiDestroy (mCompletedSem);
|
---|
808 |
|
---|
809 | ProgressBase::protectedUninit (autoUninitSpan);
|
---|
810 | }
|
---|
811 |
|
---|
812 | // IProgress properties
|
---|
813 | /////////////////////////////////////////////////////////////////////////////
|
---|
814 |
|
---|
815 | // IProgress methods
|
---|
816 | /////////////////////////////////////////////////////////////////////////////
|
---|
817 |
|
---|
818 | /**
|
---|
819 | * @note XPCOM: when this method is not called on the main XPCOM thread, it
|
---|
820 | * simply blocks the thread until mCompletedSem is signalled. If the
|
---|
821 | * thread has its own event queue (hmm, what for?) that it must run, then
|
---|
822 | * calling this method will definitely freeze event processing.
|
---|
823 | */
|
---|
824 | STDMETHODIMP Progress::WaitForCompletion (LONG aTimeout)
|
---|
825 | {
|
---|
826 | LogFlowThisFuncEnter();
|
---|
827 | LogFlowThisFunc(("aTimeout=%d\n", aTimeout));
|
---|
828 |
|
---|
829 | AutoCaller autoCaller(this);
|
---|
830 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
831 |
|
---|
832 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
833 |
|
---|
834 | /* if we're already completed, take a shortcut */
|
---|
835 | if (!mCompleted)
|
---|
836 | {
|
---|
837 | RTTIMESPEC time;
|
---|
838 | RTTimeNow(&time); /** @todo r=bird: Use monotonic time (RTTimeMilliTS()) here because of daylight saving and things like that. */
|
---|
839 |
|
---|
840 | int vrc = VINF_SUCCESS;
|
---|
841 | bool fForever = aTimeout < 0;
|
---|
842 | int64_t timeLeft = aTimeout;
|
---|
843 | int64_t lastTime = RTTimeSpecGetMilli(&time);
|
---|
844 |
|
---|
845 | while (!mCompleted && (fForever || timeLeft > 0))
|
---|
846 | {
|
---|
847 | mWaitersCount++;
|
---|
848 | alock.leave();
|
---|
849 | vrc = RTSemEventMultiWait(mCompletedSem,
|
---|
850 | fForever ? RT_INDEFINITE_WAIT : (unsigned)timeLeft);
|
---|
851 | alock.enter();
|
---|
852 | mWaitersCount--;
|
---|
853 |
|
---|
854 | /* the last waiter resets the semaphore */
|
---|
855 | if (mWaitersCount == 0)
|
---|
856 | RTSemEventMultiReset(mCompletedSem);
|
---|
857 |
|
---|
858 | if (RT_FAILURE(vrc) && vrc != VERR_TIMEOUT)
|
---|
859 | break;
|
---|
860 |
|
---|
861 | if (!fForever)
|
---|
862 | {
|
---|
863 | RTTimeNow (&time);
|
---|
864 | timeLeft -= RTTimeSpecGetMilli(&time) - lastTime;
|
---|
865 | lastTime = RTTimeSpecGetMilli(&time);
|
---|
866 | }
|
---|
867 | }
|
---|
868 |
|
---|
869 | if (RT_FAILURE(vrc) && vrc != VERR_TIMEOUT)
|
---|
870 | return setError(VBOX_E_IPRT_ERROR,
|
---|
871 | tr("Failed to wait for the task completion (%Rrc)"),
|
---|
872 | vrc);
|
---|
873 | }
|
---|
874 |
|
---|
875 | LogFlowThisFuncLeave();
|
---|
876 |
|
---|
877 | return S_OK;
|
---|
878 | }
|
---|
879 |
|
---|
880 | /**
|
---|
881 | * @note XPCOM: when this method is not called on the main XPCOM thread, it
|
---|
882 | * simply blocks the thread until mCompletedSem is signalled. If the
|
---|
883 | * thread has its own event queue (hmm, what for?) that it must run, then
|
---|
884 | * calling this method will definitely freeze event processing.
|
---|
885 | */
|
---|
886 | STDMETHODIMP Progress::WaitForOperationCompletion(ULONG aOperation, LONG aTimeout)
|
---|
887 | {
|
---|
888 | LogFlowThisFuncEnter();
|
---|
889 | LogFlowThisFunc(("aOperation=%d, aTimeout=%d\n", aOperation, aTimeout));
|
---|
890 |
|
---|
891 | AutoCaller autoCaller(this);
|
---|
892 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
893 |
|
---|
894 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
895 |
|
---|
896 | CheckComArgExpr(aOperation, aOperation < m_cOperations);
|
---|
897 |
|
---|
898 | /* if we're already completed or if the given operation is already done,
|
---|
899 | * then take a shortcut */
|
---|
900 | if ( !mCompleted
|
---|
901 | && aOperation >= m_ulCurrentOperation)
|
---|
902 | {
|
---|
903 | RTTIMESPEC time;
|
---|
904 | RTTimeNow (&time);
|
---|
905 |
|
---|
906 | int vrc = VINF_SUCCESS;
|
---|
907 | bool fForever = aTimeout < 0;
|
---|
908 | int64_t timeLeft = aTimeout;
|
---|
909 | int64_t lastTime = RTTimeSpecGetMilli (&time);
|
---|
910 |
|
---|
911 | while ( !mCompleted && aOperation >= m_ulCurrentOperation
|
---|
912 | && (fForever || timeLeft > 0))
|
---|
913 | {
|
---|
914 | mWaitersCount ++;
|
---|
915 | alock.leave();
|
---|
916 | vrc = RTSemEventMultiWait(mCompletedSem,
|
---|
917 | fForever ? RT_INDEFINITE_WAIT : (unsigned) timeLeft);
|
---|
918 | alock.enter();
|
---|
919 | mWaitersCount--;
|
---|
920 |
|
---|
921 | /* the last waiter resets the semaphore */
|
---|
922 | if (mWaitersCount == 0)
|
---|
923 | RTSemEventMultiReset(mCompletedSem);
|
---|
924 |
|
---|
925 | if (RT_FAILURE(vrc) && vrc != VERR_TIMEOUT)
|
---|
926 | break;
|
---|
927 |
|
---|
928 | if (!fForever)
|
---|
929 | {
|
---|
930 | RTTimeNow(&time);
|
---|
931 | timeLeft -= RTTimeSpecGetMilli(&time) - lastTime;
|
---|
932 | lastTime = RTTimeSpecGetMilli(&time);
|
---|
933 | }
|
---|
934 | }
|
---|
935 |
|
---|
936 | if (RT_FAILURE(vrc) && vrc != VERR_TIMEOUT)
|
---|
937 | return setError(E_FAIL,
|
---|
938 | tr("Failed to wait for the operation completion (%Rrc)"),
|
---|
939 | vrc);
|
---|
940 | }
|
---|
941 |
|
---|
942 | LogFlowThisFuncLeave();
|
---|
943 |
|
---|
944 | return S_OK;
|
---|
945 | }
|
---|
946 |
|
---|
947 | STDMETHODIMP Progress::Cancel()
|
---|
948 | {
|
---|
949 | AutoCaller autoCaller(this);
|
---|
950 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
951 |
|
---|
952 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
953 |
|
---|
954 | if (!mCancelable)
|
---|
955 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
956 | tr("Operation cannot be canceled"));
|
---|
957 |
|
---|
958 | if (!mCanceled)
|
---|
959 | {
|
---|
960 | mCanceled = TRUE;
|
---|
961 | if (m_pfnCancelCallback)
|
---|
962 | m_pfnCancelCallback(m_pvCancelUserArg);
|
---|
963 |
|
---|
964 | }
|
---|
965 | return S_OK;
|
---|
966 | }
|
---|
967 |
|
---|
968 | /**
|
---|
969 | * Updates the percentage value of the current operation.
|
---|
970 | *
|
---|
971 | * @param aPercent New percentage value of the operation in progress
|
---|
972 | * (in range [0, 100]).
|
---|
973 | */
|
---|
974 | STDMETHODIMP Progress::SetCurrentOperationProgress(ULONG aPercent)
|
---|
975 | {
|
---|
976 | AutoCaller autoCaller(this);
|
---|
977 | AssertComRCReturnRC(autoCaller.rc());
|
---|
978 |
|
---|
979 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
980 |
|
---|
981 | AssertReturn(aPercent <= 100, E_INVALIDARG);
|
---|
982 |
|
---|
983 | checkForAutomaticTimeout();
|
---|
984 | if (mCancelable && mCanceled)
|
---|
985 | {
|
---|
986 | Assert(!mCompleted);
|
---|
987 | return E_FAIL;
|
---|
988 | }
|
---|
989 | AssertReturn(!mCompleted && !mCanceled, E_FAIL);
|
---|
990 |
|
---|
991 | m_ulOperationPercent = aPercent;
|
---|
992 |
|
---|
993 | return S_OK;
|
---|
994 | }
|
---|
995 |
|
---|
996 | /**
|
---|
997 | * Signals that the current operation is successfully completed and advances to
|
---|
998 | * the next operation. The operation percentage is reset to 0.
|
---|
999 | *
|
---|
1000 | * @param aOperationDescription Description of the next operation.
|
---|
1001 | *
|
---|
1002 | * @note The current operation must not be the last one.
|
---|
1003 | */
|
---|
1004 | STDMETHODIMP Progress::SetNextOperation(IN_BSTR bstrNextOperationDescription, ULONG ulNextOperationsWeight)
|
---|
1005 | {
|
---|
1006 | AssertReturn(bstrNextOperationDescription, E_INVALIDARG);
|
---|
1007 |
|
---|
1008 | AutoCaller autoCaller(this);
|
---|
1009 | AssertComRCReturnRC(autoCaller.rc());
|
---|
1010 |
|
---|
1011 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1012 |
|
---|
1013 | AssertReturn(!mCompleted && !mCanceled, E_FAIL);
|
---|
1014 | AssertReturn(m_ulCurrentOperation + 1 < m_cOperations, E_FAIL);
|
---|
1015 |
|
---|
1016 | ++m_ulCurrentOperation;
|
---|
1017 | m_ulOperationsCompletedWeight += m_ulCurrentOperationWeight;
|
---|
1018 |
|
---|
1019 | m_bstrOperationDescription = bstrNextOperationDescription;
|
---|
1020 | m_ulCurrentOperationWeight = ulNextOperationsWeight;
|
---|
1021 | m_ulOperationPercent = 0;
|
---|
1022 |
|
---|
1023 | Log(("Progress::setNextOperation(%ls): ulNextOperationsWeight = %d; m_ulCurrentOperation is now %d, m_ulOperationsCompletedWeight is now %d\n",
|
---|
1024 | m_bstrOperationDescription.raw(), ulNextOperationsWeight, m_ulCurrentOperation, m_ulOperationsCompletedWeight));
|
---|
1025 |
|
---|
1026 | /* wake up all waiting threads */
|
---|
1027 | if (mWaitersCount > 0)
|
---|
1028 | RTSemEventMultiSignal(mCompletedSem);
|
---|
1029 |
|
---|
1030 | return S_OK;
|
---|
1031 | }
|
---|
1032 |
|
---|
1033 | // public methods only for internal purposes
|
---|
1034 | /////////////////////////////////////////////////////////////////////////////
|
---|
1035 |
|
---|
1036 | /**
|
---|
1037 | * Sets the internal result code and attempts to retrieve additional error
|
---|
1038 | * info from the current thread. Gets called from Progress::notifyComplete(),
|
---|
1039 | * but can be called again to override a previous result set with
|
---|
1040 | * notifyComplete().
|
---|
1041 | *
|
---|
1042 | * @param aResultCode
|
---|
1043 | */
|
---|
1044 | HRESULT Progress::setResultCode(HRESULT aResultCode)
|
---|
1045 | {
|
---|
1046 | AutoCaller autoCaller(this);
|
---|
1047 | AssertComRCReturnRC(autoCaller.rc());
|
---|
1048 |
|
---|
1049 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1050 |
|
---|
1051 | mResultCode = aResultCode;
|
---|
1052 |
|
---|
1053 | HRESULT rc = S_OK;
|
---|
1054 |
|
---|
1055 | if (FAILED(aResultCode))
|
---|
1056 | {
|
---|
1057 | /* try to import error info from the current thread */
|
---|
1058 |
|
---|
1059 | #if !defined (VBOX_WITH_XPCOM)
|
---|
1060 |
|
---|
1061 | ComPtr<IErrorInfo> err;
|
---|
1062 | rc = ::GetErrorInfo(0, err.asOutParam());
|
---|
1063 | if (rc == S_OK && err)
|
---|
1064 | {
|
---|
1065 | rc = err.queryInterfaceTo(mErrorInfo.asOutParam());
|
---|
1066 | if (SUCCEEDED(rc) && !mErrorInfo)
|
---|
1067 | rc = E_FAIL;
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | #else /* !defined (VBOX_WITH_XPCOM) */
|
---|
1071 |
|
---|
1072 | nsCOMPtr<nsIExceptionService> es;
|
---|
1073 | es = do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID, &rc);
|
---|
1074 | if (NS_SUCCEEDED(rc))
|
---|
1075 | {
|
---|
1076 | nsCOMPtr <nsIExceptionManager> em;
|
---|
1077 | rc = es->GetCurrentExceptionManager(getter_AddRefs(em));
|
---|
1078 | if (NS_SUCCEEDED(rc))
|
---|
1079 | {
|
---|
1080 | ComPtr<nsIException> ex;
|
---|
1081 | rc = em->GetCurrentException(ex.asOutParam());
|
---|
1082 | if (NS_SUCCEEDED(rc) && ex)
|
---|
1083 | {
|
---|
1084 | rc = ex.queryInterfaceTo(mErrorInfo.asOutParam());
|
---|
1085 | if (NS_SUCCEEDED(rc) && !mErrorInfo)
|
---|
1086 | rc = E_FAIL;
|
---|
1087 | }
|
---|
1088 | }
|
---|
1089 | }
|
---|
1090 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
1091 |
|
---|
1092 | AssertMsg (rc == S_OK, ("Couldn't get error info (rc=%08X) while trying "
|
---|
1093 | "to set a failed result (%08X)!\n", rc, aResultCode));
|
---|
1094 | }
|
---|
1095 |
|
---|
1096 | return rc;
|
---|
1097 | }
|
---|
1098 |
|
---|
1099 | /**
|
---|
1100 | * Marks the whole task as complete and sets the result code.
|
---|
1101 | *
|
---|
1102 | * If the result code indicates a failure (|FAILED(@a aResultCode)|) then this
|
---|
1103 | * method will import the error info from the current thread and assign it to
|
---|
1104 | * the errorInfo attribute (it will return an error if no info is available in
|
---|
1105 | * such case).
|
---|
1106 | *
|
---|
1107 | * If the result code indicates a success (|SUCCEEDED(@a aResultCode)|) then
|
---|
1108 | * the current operation is set to the last.
|
---|
1109 | *
|
---|
1110 | * Note that this method may be called only once for the given Progress object.
|
---|
1111 | * Subsequent calls will assert.
|
---|
1112 | *
|
---|
1113 | * @param aResultCode Operation result code.
|
---|
1114 | */
|
---|
1115 | HRESULT Progress::notifyComplete(HRESULT aResultCode)
|
---|
1116 | {
|
---|
1117 | AutoCaller autoCaller(this);
|
---|
1118 | AssertComRCReturnRC(autoCaller.rc());
|
---|
1119 |
|
---|
1120 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1121 |
|
---|
1122 | AssertReturn(mCompleted == FALSE, E_FAIL);
|
---|
1123 |
|
---|
1124 | if (mCanceled && SUCCEEDED(aResultCode))
|
---|
1125 | aResultCode = E_FAIL;
|
---|
1126 |
|
---|
1127 | HRESULT rc = setResultCode(aResultCode);
|
---|
1128 |
|
---|
1129 | mCompleted = TRUE;
|
---|
1130 |
|
---|
1131 | if (!FAILED(aResultCode))
|
---|
1132 | {
|
---|
1133 | m_ulCurrentOperation = m_cOperations - 1; /* last operation */
|
---|
1134 | m_ulOperationPercent = 100;
|
---|
1135 | }
|
---|
1136 |
|
---|
1137 | #if !defined VBOX_COM_INPROC
|
---|
1138 | /* remove from the global collection of pending progress operations */
|
---|
1139 | if (mParent)
|
---|
1140 | mParent->removeProgress (mId);
|
---|
1141 | #endif
|
---|
1142 |
|
---|
1143 | /* wake up all waiting threads */
|
---|
1144 | if (mWaitersCount > 0)
|
---|
1145 | RTSemEventMultiSignal (mCompletedSem);
|
---|
1146 |
|
---|
1147 | return rc;
|
---|
1148 | }
|
---|
1149 |
|
---|
1150 | /**
|
---|
1151 | * Marks the operation as complete and attaches full error info.
|
---|
1152 | *
|
---|
1153 | * See com::SupportErrorInfoImpl::setError(HRESULT, const GUID &, const wchar_t
|
---|
1154 | * *, const char *, ...) for more info.
|
---|
1155 | *
|
---|
1156 | * @param aResultCode Operation result (error) code, must not be S_OK.
|
---|
1157 | * @param aIID IID of the interface that defines the error.
|
---|
1158 | * @param aComponent Name of the component that generates the error.
|
---|
1159 | * @param aText Error message (must not be null), an RTStrPrintf-like
|
---|
1160 | * format string in UTF-8 encoding.
|
---|
1161 | * @param ... List of arguments for the format string.
|
---|
1162 | */
|
---|
1163 | HRESULT Progress::notifyComplete(HRESULT aResultCode,
|
---|
1164 | const GUID &aIID,
|
---|
1165 | const Bstr &aComponent,
|
---|
1166 | const char *aText,
|
---|
1167 | ...)
|
---|
1168 | {
|
---|
1169 | va_list args;
|
---|
1170 | va_start(args, aText);
|
---|
1171 | Utf8Str text = Utf8StrFmtVA(aText, args);
|
---|
1172 | va_end (args);
|
---|
1173 |
|
---|
1174 | AutoCaller autoCaller(this);
|
---|
1175 | AssertComRCReturnRC(autoCaller.rc());
|
---|
1176 |
|
---|
1177 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1178 |
|
---|
1179 | AssertReturn(mCompleted == FALSE, E_FAIL);
|
---|
1180 |
|
---|
1181 | if (mCanceled && SUCCEEDED(aResultCode))
|
---|
1182 | aResultCode = E_FAIL;
|
---|
1183 |
|
---|
1184 | mCompleted = TRUE;
|
---|
1185 | mResultCode = aResultCode;
|
---|
1186 |
|
---|
1187 | AssertReturn(FAILED(aResultCode), E_FAIL);
|
---|
1188 |
|
---|
1189 | ComObjPtr<VirtualBoxErrorInfo> errorInfo;
|
---|
1190 | HRESULT rc = errorInfo.createObject();
|
---|
1191 | AssertComRC (rc);
|
---|
1192 | if (SUCCEEDED(rc))
|
---|
1193 | {
|
---|
1194 | errorInfo->init(aResultCode, aIID, aComponent, Bstr(text));
|
---|
1195 | errorInfo.queryInterfaceTo(mErrorInfo.asOutParam());
|
---|
1196 | }
|
---|
1197 |
|
---|
1198 | #if !defined VBOX_COM_INPROC
|
---|
1199 | /* remove from the global collection of pending progress operations */
|
---|
1200 | if (mParent)
|
---|
1201 | mParent->removeProgress (mId);
|
---|
1202 | #endif
|
---|
1203 |
|
---|
1204 | /* wake up all waiting threads */
|
---|
1205 | if (mWaitersCount > 0)
|
---|
1206 | RTSemEventMultiSignal(mCompletedSem);
|
---|
1207 |
|
---|
1208 | return rc;
|
---|
1209 | }
|
---|
1210 |
|
---|
1211 | /**
|
---|
1212 | * Notify the progress object that we're almost at the point of no return.
|
---|
1213 | *
|
---|
1214 | * This atomically checks for and disables cancelation. Calls to
|
---|
1215 | * IProgress::Cancel() made after a successfull call to this method will fail
|
---|
1216 | * and the user can be told. While this isn't entirely clean behavior, it
|
---|
1217 | * prevents issues with an irreversible actually operation succeeding while the
|
---|
1218 | * user belive it was rolled back.
|
---|
1219 | *
|
---|
1220 | * @returns Success indicator.
|
---|
1221 | * @retval true on success.
|
---|
1222 | * @retval false if the progress object has already been canceled or is in an
|
---|
1223 | * invalid state
|
---|
1224 | */
|
---|
1225 | bool Progress::notifyPointOfNoReturn(void)
|
---|
1226 | {
|
---|
1227 | AutoCaller autoCaller(this);
|
---|
1228 | AssertComRCReturn(autoCaller.rc(), false);
|
---|
1229 |
|
---|
1230 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1231 |
|
---|
1232 | if (mCanceled)
|
---|
1233 | return false;
|
---|
1234 |
|
---|
1235 | mCancelable = FALSE;
|
---|
1236 | return true;
|
---|
1237 | }
|
---|
1238 |
|
---|
1239 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1240 | // CombinedProgress class
|
---|
1241 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1242 |
|
---|
1243 | HRESULT CombinedProgress::FinalConstruct()
|
---|
1244 | {
|
---|
1245 | HRESULT rc = ProgressBase::FinalConstruct();
|
---|
1246 | if (FAILED(rc)) return rc;
|
---|
1247 |
|
---|
1248 | mProgress = 0;
|
---|
1249 | mCompletedOperations = 0;
|
---|
1250 |
|
---|
1251 | return S_OK;
|
---|
1252 | }
|
---|
1253 |
|
---|
1254 | void CombinedProgress::FinalRelease()
|
---|
1255 | {
|
---|
1256 | uninit();
|
---|
1257 | }
|
---|
1258 |
|
---|
1259 | // public initializer/uninitializer for internal purposes only
|
---|
1260 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1261 |
|
---|
1262 | /**
|
---|
1263 | * Initializes this object based on individual combined progresses.
|
---|
1264 | * Must be called only from #init()!
|
---|
1265 | *
|
---|
1266 | * @param aAutoInitSpan AutoInitSpan object instantiated by a subclass.
|
---|
1267 | * @param aParent See ProgressBase::init().
|
---|
1268 | * @param aInitiator See ProgressBase::init().
|
---|
1269 | * @param aDescription See ProgressBase::init().
|
---|
1270 | * @param aId See ProgressBase::init().
|
---|
1271 | */
|
---|
1272 | HRESULT CombinedProgress::protectedInit (AutoInitSpan &aAutoInitSpan,
|
---|
1273 | #if !defined (VBOX_COM_INPROC)
|
---|
1274 | VirtualBox *aParent,
|
---|
1275 | #endif
|
---|
1276 | IUnknown *aInitiator,
|
---|
1277 | CBSTR aDescription, OUT_GUID aId)
|
---|
1278 | {
|
---|
1279 | LogFlowThisFunc(("aDescription={%ls} mProgresses.size()=%d\n",
|
---|
1280 | aDescription, mProgresses.size()));
|
---|
1281 |
|
---|
1282 | HRESULT rc = S_OK;
|
---|
1283 |
|
---|
1284 | rc = ProgressBase::protectedInit (aAutoInitSpan,
|
---|
1285 | #if !defined (VBOX_COM_INPROC)
|
---|
1286 | aParent,
|
---|
1287 | #endif
|
---|
1288 | aInitiator, aDescription, aId);
|
---|
1289 | if (FAILED(rc)) return rc;
|
---|
1290 |
|
---|
1291 | mProgress = 0; /* the first object */
|
---|
1292 | mCompletedOperations = 0;
|
---|
1293 |
|
---|
1294 | mCompleted = FALSE;
|
---|
1295 | mCancelable = TRUE; /* until any progress returns FALSE */
|
---|
1296 | mCanceled = FALSE;
|
---|
1297 |
|
---|
1298 | m_cOperations = 0; /* will be calculated later */
|
---|
1299 |
|
---|
1300 | m_ulCurrentOperation = 0;
|
---|
1301 | rc = mProgresses[0]->COMGETTER(OperationDescription)(m_bstrOperationDescription.asOutParam());
|
---|
1302 | if (FAILED(rc)) return rc;
|
---|
1303 |
|
---|
1304 | for (size_t i = 0; i < mProgresses.size(); i ++)
|
---|
1305 | {
|
---|
1306 | if (mCancelable)
|
---|
1307 | {
|
---|
1308 | BOOL cancelable = FALSE;
|
---|
1309 | rc = mProgresses[i]->COMGETTER(Cancelable)(&cancelable);
|
---|
1310 | if (FAILED(rc)) return rc;
|
---|
1311 |
|
---|
1312 | if (!cancelable)
|
---|
1313 | mCancelable = FALSE;
|
---|
1314 | }
|
---|
1315 |
|
---|
1316 | {
|
---|
1317 | ULONG opCount = 0;
|
---|
1318 | rc = mProgresses[i]->COMGETTER(OperationCount)(&opCount);
|
---|
1319 | if (FAILED(rc)) return rc;
|
---|
1320 |
|
---|
1321 | m_cOperations += opCount;
|
---|
1322 | }
|
---|
1323 | }
|
---|
1324 |
|
---|
1325 | rc = checkProgress();
|
---|
1326 | if (FAILED(rc)) return rc;
|
---|
1327 |
|
---|
1328 | return rc;
|
---|
1329 | }
|
---|
1330 |
|
---|
1331 | /**
|
---|
1332 | * Initializes the combined progress object given two normal progress
|
---|
1333 | * objects.
|
---|
1334 | *
|
---|
1335 | * @param aParent See ProgressBase::init().
|
---|
1336 | * @param aInitiator See ProgressBase::init().
|
---|
1337 | * @param aDescription See ProgressBase::init().
|
---|
1338 | * @param aProgress1 First normal progress object.
|
---|
1339 | * @param aProgress2 Second normal progress object.
|
---|
1340 | * @param aId See ProgressBase::init().
|
---|
1341 | */
|
---|
1342 | HRESULT CombinedProgress::init(
|
---|
1343 | #if !defined (VBOX_COM_INPROC)
|
---|
1344 | VirtualBox *aParent,
|
---|
1345 | #endif
|
---|
1346 | IUnknown *aInitiator,
|
---|
1347 | CBSTR aDescription,
|
---|
1348 | IProgress *aProgress1,
|
---|
1349 | IProgress *aProgress2,
|
---|
1350 | OUT_GUID aId /* = NULL */)
|
---|
1351 | {
|
---|
1352 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1353 | AutoInitSpan autoInitSpan(this);
|
---|
1354 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1355 |
|
---|
1356 | mProgresses.resize(2);
|
---|
1357 | mProgresses[0] = aProgress1;
|
---|
1358 | mProgresses[1] = aProgress2;
|
---|
1359 |
|
---|
1360 | HRESULT rc = protectedInit(autoInitSpan,
|
---|
1361 | #if !defined (VBOX_COM_INPROC)
|
---|
1362 | aParent,
|
---|
1363 | #endif
|
---|
1364 | aInitiator,
|
---|
1365 | aDescription,
|
---|
1366 | aId);
|
---|
1367 |
|
---|
1368 | /* Confirm a successful initialization when it's the case */
|
---|
1369 | if (SUCCEEDED(rc))
|
---|
1370 | autoInitSpan.setSucceeded();
|
---|
1371 |
|
---|
1372 | return rc;
|
---|
1373 | }
|
---|
1374 |
|
---|
1375 | /**
|
---|
1376 | * Uninitializes the instance and sets the ready flag to FALSE.
|
---|
1377 | *
|
---|
1378 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
1379 | */
|
---|
1380 | void CombinedProgress::uninit()
|
---|
1381 | {
|
---|
1382 | LogFlowThisFunc(("\n"));
|
---|
1383 |
|
---|
1384 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
1385 | AutoUninitSpan autoUninitSpan(this);
|
---|
1386 | if (autoUninitSpan.uninitDone())
|
---|
1387 | return;
|
---|
1388 |
|
---|
1389 | mProgress = 0;
|
---|
1390 | mProgresses.clear();
|
---|
1391 |
|
---|
1392 | ProgressBase::protectedUninit (autoUninitSpan);
|
---|
1393 | }
|
---|
1394 |
|
---|
1395 | // IProgress properties
|
---|
1396 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1397 |
|
---|
1398 | STDMETHODIMP CombinedProgress::COMGETTER(Percent)(ULONG *aPercent)
|
---|
1399 | {
|
---|
1400 | CheckComArgOutPointerValid(aPercent);
|
---|
1401 |
|
---|
1402 | AutoCaller autoCaller(this);
|
---|
1403 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1404 |
|
---|
1405 | /* checkProgress needs a write lock */
|
---|
1406 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1407 |
|
---|
1408 | if (mCompleted && SUCCEEDED(mResultCode))
|
---|
1409 | *aPercent = 100;
|
---|
1410 | else
|
---|
1411 | {
|
---|
1412 | HRESULT rc = checkProgress();
|
---|
1413 | if (FAILED(rc)) return rc;
|
---|
1414 |
|
---|
1415 | /* global percent =
|
---|
1416 | * (100 / m_cOperations) * mOperation +
|
---|
1417 | * ((100 / m_cOperations) / 100) * m_ulOperationPercent */
|
---|
1418 | *aPercent = (100 * m_ulCurrentOperation + m_ulOperationPercent) / m_cOperations;
|
---|
1419 | }
|
---|
1420 |
|
---|
1421 | return S_OK;
|
---|
1422 | }
|
---|
1423 |
|
---|
1424 | STDMETHODIMP CombinedProgress::COMGETTER(Completed) (BOOL *aCompleted)
|
---|
1425 | {
|
---|
1426 | CheckComArgOutPointerValid(aCompleted);
|
---|
1427 |
|
---|
1428 | AutoCaller autoCaller(this);
|
---|
1429 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1430 |
|
---|
1431 | /* checkProgress needs a write lock */
|
---|
1432 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1433 |
|
---|
1434 | HRESULT rc = checkProgress();
|
---|
1435 | if (FAILED(rc)) return rc;
|
---|
1436 |
|
---|
1437 | return ProgressBase::COMGETTER(Completed) (aCompleted);
|
---|
1438 | }
|
---|
1439 |
|
---|
1440 | STDMETHODIMP CombinedProgress::COMGETTER(Canceled) (BOOL *aCanceled)
|
---|
1441 | {
|
---|
1442 | CheckComArgOutPointerValid(aCanceled);
|
---|
1443 |
|
---|
1444 | AutoCaller autoCaller(this);
|
---|
1445 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1446 |
|
---|
1447 | /* checkProgress needs a write lock */
|
---|
1448 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1449 |
|
---|
1450 | HRESULT rc = checkProgress();
|
---|
1451 | if (FAILED(rc)) return rc;
|
---|
1452 |
|
---|
1453 | return ProgressBase::COMGETTER(Canceled) (aCanceled);
|
---|
1454 | }
|
---|
1455 |
|
---|
1456 | STDMETHODIMP CombinedProgress::COMGETTER(ResultCode) (LONG *aResultCode)
|
---|
1457 | {
|
---|
1458 | CheckComArgOutPointerValid(aResultCode);
|
---|
1459 |
|
---|
1460 | AutoCaller autoCaller(this);
|
---|
1461 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1462 |
|
---|
1463 | /* checkProgress needs a write lock */
|
---|
1464 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1465 |
|
---|
1466 | HRESULT rc = checkProgress();
|
---|
1467 | if (FAILED(rc)) return rc;
|
---|
1468 |
|
---|
1469 | return ProgressBase::COMGETTER(ResultCode) (aResultCode);
|
---|
1470 | }
|
---|
1471 |
|
---|
1472 | STDMETHODIMP CombinedProgress::COMGETTER(ErrorInfo) (IVirtualBoxErrorInfo **aErrorInfo)
|
---|
1473 | {
|
---|
1474 | CheckComArgOutPointerValid(aErrorInfo);
|
---|
1475 |
|
---|
1476 | AutoCaller autoCaller(this);
|
---|
1477 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1478 |
|
---|
1479 | /* checkProgress needs a write lock */
|
---|
1480 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1481 |
|
---|
1482 | HRESULT rc = checkProgress();
|
---|
1483 | if (FAILED(rc)) return rc;
|
---|
1484 |
|
---|
1485 | return ProgressBase::COMGETTER(ErrorInfo) (aErrorInfo);
|
---|
1486 | }
|
---|
1487 |
|
---|
1488 | STDMETHODIMP CombinedProgress::COMGETTER(Operation) (ULONG *aOperation)
|
---|
1489 | {
|
---|
1490 | CheckComArgOutPointerValid(aOperation);
|
---|
1491 |
|
---|
1492 | AutoCaller autoCaller(this);
|
---|
1493 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1494 |
|
---|
1495 | /* checkProgress needs a write lock */
|
---|
1496 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1497 |
|
---|
1498 | HRESULT rc = checkProgress();
|
---|
1499 | if (FAILED(rc)) return rc;
|
---|
1500 |
|
---|
1501 | return ProgressBase::COMGETTER(Operation) (aOperation);
|
---|
1502 | }
|
---|
1503 |
|
---|
1504 | STDMETHODIMP CombinedProgress::COMGETTER(OperationDescription) (BSTR *aOperationDescription)
|
---|
1505 | {
|
---|
1506 | CheckComArgOutPointerValid(aOperationDescription);
|
---|
1507 |
|
---|
1508 | AutoCaller autoCaller(this);
|
---|
1509 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1510 |
|
---|
1511 | /* checkProgress needs a write lock */
|
---|
1512 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1513 |
|
---|
1514 | HRESULT rc = checkProgress();
|
---|
1515 | if (FAILED(rc)) return rc;
|
---|
1516 |
|
---|
1517 | return ProgressBase::COMGETTER(OperationDescription) (aOperationDescription);
|
---|
1518 | }
|
---|
1519 |
|
---|
1520 | STDMETHODIMP CombinedProgress::COMGETTER(OperationPercent)(ULONG *aOperationPercent)
|
---|
1521 | {
|
---|
1522 | CheckComArgOutPointerValid(aOperationPercent);
|
---|
1523 |
|
---|
1524 | AutoCaller autoCaller(this);
|
---|
1525 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1526 |
|
---|
1527 | /* checkProgress needs a write lock */
|
---|
1528 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1529 |
|
---|
1530 | HRESULT rc = checkProgress();
|
---|
1531 | if (FAILED(rc)) return rc;
|
---|
1532 |
|
---|
1533 | return ProgressBase::COMGETTER(OperationPercent) (aOperationPercent);
|
---|
1534 | }
|
---|
1535 |
|
---|
1536 | STDMETHODIMP CombinedProgress::COMSETTER(Timeout)(ULONG aTimeout)
|
---|
1537 | {
|
---|
1538 | NOREF(aTimeout);
|
---|
1539 | AssertFailed();
|
---|
1540 | return E_NOTIMPL;
|
---|
1541 | }
|
---|
1542 |
|
---|
1543 | STDMETHODIMP CombinedProgress::COMGETTER(Timeout)(ULONG *aTimeout)
|
---|
1544 | {
|
---|
1545 | CheckComArgOutPointerValid(aTimeout);
|
---|
1546 |
|
---|
1547 | AssertFailed();
|
---|
1548 | return E_NOTIMPL;
|
---|
1549 | }
|
---|
1550 |
|
---|
1551 | // IProgress methods
|
---|
1552 | /////////////////////////////////////////////////////////////////////////////
|
---|
1553 |
|
---|
1554 | /**
|
---|
1555 | * @note XPCOM: when this method is called not on the main XPCOM thread, it
|
---|
1556 | * simply blocks the thread until mCompletedSem is signalled. If the
|
---|
1557 | * thread has its own event queue (hmm, what for?) that it must run, then
|
---|
1558 | * calling this method will definitely freeze event processing.
|
---|
1559 | */
|
---|
1560 | STDMETHODIMP CombinedProgress::WaitForCompletion (LONG aTimeout)
|
---|
1561 | {
|
---|
1562 | LogFlowThisFuncEnter();
|
---|
1563 | LogFlowThisFunc(("aTtimeout=%d\n", aTimeout));
|
---|
1564 |
|
---|
1565 | AutoCaller autoCaller(this);
|
---|
1566 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1567 |
|
---|
1568 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1569 |
|
---|
1570 | /* if we're already completed, take a shortcut */
|
---|
1571 | if (!mCompleted)
|
---|
1572 | {
|
---|
1573 | RTTIMESPEC time;
|
---|
1574 | RTTimeNow(&time);
|
---|
1575 |
|
---|
1576 | HRESULT rc = S_OK;
|
---|
1577 | bool forever = aTimeout < 0;
|
---|
1578 | int64_t timeLeft = aTimeout;
|
---|
1579 | int64_t lastTime = RTTimeSpecGetMilli(&time);
|
---|
1580 |
|
---|
1581 | while (!mCompleted && (forever || timeLeft > 0))
|
---|
1582 | {
|
---|
1583 | alock.leave();
|
---|
1584 | rc = mProgresses.back()->WaitForCompletion(forever ? -1 : (LONG) timeLeft);
|
---|
1585 | alock.enter();
|
---|
1586 |
|
---|
1587 | if (SUCCEEDED(rc))
|
---|
1588 | rc = checkProgress();
|
---|
1589 |
|
---|
1590 | if (FAILED(rc)) break;
|
---|
1591 |
|
---|
1592 | if (!forever)
|
---|
1593 | {
|
---|
1594 | RTTimeNow(&time);
|
---|
1595 | timeLeft -= RTTimeSpecGetMilli(&time) - lastTime;
|
---|
1596 | lastTime = RTTimeSpecGetMilli(&time);
|
---|
1597 | }
|
---|
1598 | }
|
---|
1599 |
|
---|
1600 | if (FAILED(rc)) return rc;
|
---|
1601 | }
|
---|
1602 |
|
---|
1603 | LogFlowThisFuncLeave();
|
---|
1604 |
|
---|
1605 | return S_OK;
|
---|
1606 | }
|
---|
1607 |
|
---|
1608 | /**
|
---|
1609 | * @note XPCOM: when this method is called not on the main XPCOM thread, it
|
---|
1610 | * simply blocks the thread until mCompletedSem is signalled. If the
|
---|
1611 | * thread has its own event queue (hmm, what for?) that it must run, then
|
---|
1612 | * calling this method will definitely freeze event processing.
|
---|
1613 | */
|
---|
1614 | STDMETHODIMP CombinedProgress::WaitForOperationCompletion (ULONG aOperation, LONG aTimeout)
|
---|
1615 | {
|
---|
1616 | LogFlowThisFuncEnter();
|
---|
1617 | LogFlowThisFunc(("aOperation=%d, aTimeout=%d\n", aOperation, aTimeout));
|
---|
1618 |
|
---|
1619 | AutoCaller autoCaller(this);
|
---|
1620 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1621 |
|
---|
1622 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1623 |
|
---|
1624 | if (aOperation >= m_cOperations)
|
---|
1625 | return setError(E_FAIL,
|
---|
1626 | tr("Operation number must be in range [0, %d]"), m_ulCurrentOperation - 1);
|
---|
1627 |
|
---|
1628 | /* if we're already completed or if the given operation is already done,
|
---|
1629 | * then take a shortcut */
|
---|
1630 | if (!mCompleted && aOperation >= m_ulCurrentOperation)
|
---|
1631 | {
|
---|
1632 | HRESULT rc = S_OK;
|
---|
1633 |
|
---|
1634 | /* find the right progress object to wait for */
|
---|
1635 | size_t progress = mProgress;
|
---|
1636 | ULONG operation = 0, completedOps = mCompletedOperations;
|
---|
1637 | do
|
---|
1638 | {
|
---|
1639 | ULONG opCount = 0;
|
---|
1640 | rc = mProgresses[progress]->COMGETTER(OperationCount)(&opCount);
|
---|
1641 | if (FAILED(rc))
|
---|
1642 | return rc;
|
---|
1643 |
|
---|
1644 | if (completedOps + opCount > aOperation)
|
---|
1645 | {
|
---|
1646 | /* found the right progress object */
|
---|
1647 | operation = aOperation - completedOps;
|
---|
1648 | break;
|
---|
1649 | }
|
---|
1650 |
|
---|
1651 | completedOps += opCount;
|
---|
1652 | progress ++;
|
---|
1653 | ComAssertRet(progress < mProgresses.size(), E_FAIL);
|
---|
1654 | }
|
---|
1655 | while (1);
|
---|
1656 |
|
---|
1657 | LogFlowThisFunc(("will wait for mProgresses [%d] (%d)\n",
|
---|
1658 | progress, operation));
|
---|
1659 |
|
---|
1660 | RTTIMESPEC time;
|
---|
1661 | RTTimeNow (&time);
|
---|
1662 |
|
---|
1663 | bool forever = aTimeout < 0;
|
---|
1664 | int64_t timeLeft = aTimeout;
|
---|
1665 | int64_t lastTime = RTTimeSpecGetMilli (&time);
|
---|
1666 |
|
---|
1667 | while (!mCompleted && aOperation >= m_ulCurrentOperation &&
|
---|
1668 | (forever || timeLeft > 0))
|
---|
1669 | {
|
---|
1670 | alock.leave();
|
---|
1671 | /* wait for the appropriate progress operation completion */
|
---|
1672 | rc = mProgresses[progress]-> WaitForOperationCompletion(operation,
|
---|
1673 | forever ? -1 : (LONG) timeLeft);
|
---|
1674 | alock.enter();
|
---|
1675 |
|
---|
1676 | if (SUCCEEDED(rc))
|
---|
1677 | rc = checkProgress();
|
---|
1678 |
|
---|
1679 | if (FAILED(rc)) break;
|
---|
1680 |
|
---|
1681 | if (!forever)
|
---|
1682 | {
|
---|
1683 | RTTimeNow(&time);
|
---|
1684 | timeLeft -= RTTimeSpecGetMilli(&time) - lastTime;
|
---|
1685 | lastTime = RTTimeSpecGetMilli(&time);
|
---|
1686 | }
|
---|
1687 | }
|
---|
1688 |
|
---|
1689 | if (FAILED(rc)) return rc;
|
---|
1690 | }
|
---|
1691 |
|
---|
1692 | LogFlowThisFuncLeave();
|
---|
1693 |
|
---|
1694 | return S_OK;
|
---|
1695 | }
|
---|
1696 |
|
---|
1697 | STDMETHODIMP CombinedProgress::Cancel()
|
---|
1698 | {
|
---|
1699 | AutoCaller autoCaller(this);
|
---|
1700 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1701 |
|
---|
1702 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1703 |
|
---|
1704 | if (!mCancelable)
|
---|
1705 | return setError(E_FAIL, tr("Operation cannot be canceled"));
|
---|
1706 |
|
---|
1707 | if (!mCanceled)
|
---|
1708 | {
|
---|
1709 | mCanceled = TRUE;
|
---|
1710 | /** @todo Teleportation: Shouldn't this be propagated to mProgresses? If
|
---|
1711 | * powerUp creates passes a combined progress object to the client, I
|
---|
1712 | * won't get called back since I'm only getting the powerupProgress ...
|
---|
1713 | * Or what? */
|
---|
1714 | if (m_pfnCancelCallback)
|
---|
1715 | m_pfnCancelCallback(m_pvCancelUserArg);
|
---|
1716 |
|
---|
1717 | }
|
---|
1718 | return S_OK;
|
---|
1719 | }
|
---|
1720 |
|
---|
1721 | // private methods
|
---|
1722 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1723 |
|
---|
1724 | /**
|
---|
1725 | * Fetches the properties of the current progress object and, if it is
|
---|
1726 | * successfully completed, advances to the next uncompleted or unsuccessfully
|
---|
1727 | * completed object in the vector of combined progress objects.
|
---|
1728 | *
|
---|
1729 | * @note Must be called from under this object's write lock!
|
---|
1730 | */
|
---|
1731 | HRESULT CombinedProgress::checkProgress()
|
---|
1732 | {
|
---|
1733 | /* do nothing if we're already marked ourselves as completed */
|
---|
1734 | if (mCompleted)
|
---|
1735 | return S_OK;
|
---|
1736 |
|
---|
1737 | AssertReturn(mProgress < mProgresses.size(), E_FAIL);
|
---|
1738 |
|
---|
1739 | ComPtr<IProgress> progress = mProgresses[mProgress];
|
---|
1740 | ComAssertRet(!progress.isNull(), E_FAIL);
|
---|
1741 |
|
---|
1742 | HRESULT rc = S_OK;
|
---|
1743 | BOOL fCompleted = FALSE;
|
---|
1744 |
|
---|
1745 | do
|
---|
1746 | {
|
---|
1747 | rc = progress->COMGETTER(Completed)(&fCompleted);
|
---|
1748 | if (FAILED(rc))
|
---|
1749 | return rc;
|
---|
1750 |
|
---|
1751 | if (fCompleted)
|
---|
1752 | {
|
---|
1753 | rc = progress->COMGETTER(Canceled)(&mCanceled);
|
---|
1754 | if (FAILED(rc))
|
---|
1755 | return rc;
|
---|
1756 |
|
---|
1757 | LONG iRc;
|
---|
1758 | rc = progress->COMGETTER(ResultCode)(&iRc);
|
---|
1759 | if (FAILED(rc))
|
---|
1760 | return rc;
|
---|
1761 | mResultCode = iRc;
|
---|
1762 |
|
---|
1763 | if (FAILED(mResultCode))
|
---|
1764 | {
|
---|
1765 | rc = progress->COMGETTER(ErrorInfo) (mErrorInfo.asOutParam());
|
---|
1766 | if (FAILED(rc))
|
---|
1767 | return rc;
|
---|
1768 | }
|
---|
1769 |
|
---|
1770 | if (FAILED(mResultCode) || mCanceled)
|
---|
1771 | {
|
---|
1772 | mCompleted = TRUE;
|
---|
1773 | }
|
---|
1774 | else
|
---|
1775 | {
|
---|
1776 | ULONG opCount = 0;
|
---|
1777 | rc = progress->COMGETTER(OperationCount) (&opCount);
|
---|
1778 | if (FAILED(rc))
|
---|
1779 | return rc;
|
---|
1780 |
|
---|
1781 | mCompletedOperations += opCount;
|
---|
1782 | mProgress ++;
|
---|
1783 |
|
---|
1784 | if (mProgress < mProgresses.size())
|
---|
1785 | progress = mProgresses[mProgress];
|
---|
1786 | else
|
---|
1787 | mCompleted = TRUE;
|
---|
1788 | }
|
---|
1789 | }
|
---|
1790 | }
|
---|
1791 | while (fCompleted && !mCompleted);
|
---|
1792 |
|
---|
1793 | rc = progress->COMGETTER(OperationPercent) (&m_ulOperationPercent);
|
---|
1794 | if (SUCCEEDED(rc))
|
---|
1795 | {
|
---|
1796 | ULONG operation = 0;
|
---|
1797 | rc = progress->COMGETTER(Operation) (&operation);
|
---|
1798 | if (SUCCEEDED(rc) && mCompletedOperations + operation > m_ulCurrentOperation)
|
---|
1799 | {
|
---|
1800 | m_ulCurrentOperation = mCompletedOperations + operation;
|
---|
1801 | rc = progress->COMGETTER(OperationDescription) (
|
---|
1802 | m_bstrOperationDescription.asOutParam());
|
---|
1803 | }
|
---|
1804 | }
|
---|
1805 |
|
---|
1806 | return rc;
|
---|
1807 | }
|
---|
1808 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|