1 | /* $Id: ProgressImpl.cpp 29914 2010-05-31 14:53:33Z 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 | int vrc = VINF_SUCCESS;
|
---|
838 | bool fForever = aTimeout < 0;
|
---|
839 | int64_t timeLeft = aTimeout;
|
---|
840 | int64_t lastTime = RTTimeMilliTS();
|
---|
841 |
|
---|
842 | while (!mCompleted && (fForever || timeLeft > 0))
|
---|
843 | {
|
---|
844 | mWaitersCount++;
|
---|
845 | alock.leave();
|
---|
846 | vrc = RTSemEventMultiWait(mCompletedSem,
|
---|
847 | fForever ? RT_INDEFINITE_WAIT : (RTMSINTERVAL)timeLeft);
|
---|
848 | alock.enter();
|
---|
849 | mWaitersCount--;
|
---|
850 |
|
---|
851 | /* the last waiter resets the semaphore */
|
---|
852 | if (mWaitersCount == 0)
|
---|
853 | RTSemEventMultiReset(mCompletedSem);
|
---|
854 |
|
---|
855 | if (RT_FAILURE(vrc) && vrc != VERR_TIMEOUT)
|
---|
856 | break;
|
---|
857 |
|
---|
858 | if (!fForever)
|
---|
859 | {
|
---|
860 | int64_t now = RTTimeMilliTS();
|
---|
861 | timeLeft -= now - lastTime;
|
---|
862 | lastTime = now;
|
---|
863 | }
|
---|
864 | }
|
---|
865 |
|
---|
866 | if (RT_FAILURE(vrc) && vrc != VERR_TIMEOUT)
|
---|
867 | return setError(VBOX_E_IPRT_ERROR,
|
---|
868 | tr("Failed to wait for the task completion (%Rrc)"),
|
---|
869 | vrc);
|
---|
870 | }
|
---|
871 |
|
---|
872 | LogFlowThisFuncLeave();
|
---|
873 |
|
---|
874 | return S_OK;
|
---|
875 | }
|
---|
876 |
|
---|
877 | /**
|
---|
878 | * @note XPCOM: when this method is not called on the main XPCOM thread, it
|
---|
879 | * simply blocks the thread until mCompletedSem is signalled. If the
|
---|
880 | * thread has its own event queue (hmm, what for?) that it must run, then
|
---|
881 | * calling this method will definitely freeze event processing.
|
---|
882 | */
|
---|
883 | STDMETHODIMP Progress::WaitForOperationCompletion(ULONG aOperation, LONG aTimeout)
|
---|
884 | {
|
---|
885 | LogFlowThisFuncEnter();
|
---|
886 | LogFlowThisFunc(("aOperation=%d, aTimeout=%d\n", aOperation, aTimeout));
|
---|
887 |
|
---|
888 | AutoCaller autoCaller(this);
|
---|
889 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
890 |
|
---|
891 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
892 |
|
---|
893 | CheckComArgExpr(aOperation, aOperation < m_cOperations);
|
---|
894 |
|
---|
895 | /* if we're already completed or if the given operation is already done,
|
---|
896 | * then take a shortcut */
|
---|
897 | if ( !mCompleted
|
---|
898 | && aOperation >= m_ulCurrentOperation)
|
---|
899 | {
|
---|
900 | int vrc = VINF_SUCCESS;
|
---|
901 | bool fForever = aTimeout < 0;
|
---|
902 | int64_t timeLeft = aTimeout;
|
---|
903 | int64_t lastTime = RTTimeMilliTS();
|
---|
904 |
|
---|
905 | while ( !mCompleted && aOperation >= m_ulCurrentOperation
|
---|
906 | && (fForever || timeLeft > 0))
|
---|
907 | {
|
---|
908 | mWaitersCount ++;
|
---|
909 | alock.leave();
|
---|
910 | vrc = RTSemEventMultiWait(mCompletedSem,
|
---|
911 | fForever ? RT_INDEFINITE_WAIT : (unsigned) timeLeft);
|
---|
912 | alock.enter();
|
---|
913 | mWaitersCount--;
|
---|
914 |
|
---|
915 | /* the last waiter resets the semaphore */
|
---|
916 | if (mWaitersCount == 0)
|
---|
917 | RTSemEventMultiReset(mCompletedSem);
|
---|
918 |
|
---|
919 | if (RT_FAILURE(vrc) && vrc != VERR_TIMEOUT)
|
---|
920 | break;
|
---|
921 |
|
---|
922 | if (!fForever)
|
---|
923 | {
|
---|
924 | int64_t now = RTTimeMilliTS();
|
---|
925 | timeLeft -= now - lastTime;
|
---|
926 | lastTime = now;
|
---|
927 | }
|
---|
928 | }
|
---|
929 |
|
---|
930 | if (RT_FAILURE(vrc) && vrc != VERR_TIMEOUT)
|
---|
931 | return setError(E_FAIL,
|
---|
932 | tr("Failed to wait for the operation completion (%Rrc)"),
|
---|
933 | vrc);
|
---|
934 | }
|
---|
935 |
|
---|
936 | LogFlowThisFuncLeave();
|
---|
937 |
|
---|
938 | return S_OK;
|
---|
939 | }
|
---|
940 |
|
---|
941 | STDMETHODIMP Progress::Cancel()
|
---|
942 | {
|
---|
943 | AutoCaller autoCaller(this);
|
---|
944 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
945 |
|
---|
946 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
947 |
|
---|
948 | if (!mCancelable)
|
---|
949 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
950 | tr("Operation cannot be canceled"));
|
---|
951 |
|
---|
952 | if (!mCanceled)
|
---|
953 | {
|
---|
954 | LogThisFunc(("Canceling\n"));
|
---|
955 | mCanceled = TRUE;
|
---|
956 | if (m_pfnCancelCallback)
|
---|
957 | m_pfnCancelCallback(m_pvCancelUserArg);
|
---|
958 |
|
---|
959 | }
|
---|
960 | else
|
---|
961 | LogThisFunc(("Already canceled\n"));
|
---|
962 |
|
---|
963 | return S_OK;
|
---|
964 | }
|
---|
965 |
|
---|
966 | /**
|
---|
967 | * Updates the percentage value of the current operation.
|
---|
968 | *
|
---|
969 | * @param aPercent New percentage value of the operation in progress
|
---|
970 | * (in range [0, 100]).
|
---|
971 | */
|
---|
972 | STDMETHODIMP Progress::SetCurrentOperationProgress(ULONG aPercent)
|
---|
973 | {
|
---|
974 | AutoCaller autoCaller(this);
|
---|
975 | AssertComRCReturnRC(autoCaller.rc());
|
---|
976 |
|
---|
977 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
978 |
|
---|
979 | AssertReturn(aPercent <= 100, E_INVALIDARG);
|
---|
980 |
|
---|
981 | checkForAutomaticTimeout();
|
---|
982 | if (mCancelable && mCanceled)
|
---|
983 | {
|
---|
984 | Assert(!mCompleted);
|
---|
985 | return E_FAIL;
|
---|
986 | }
|
---|
987 | AssertReturn(!mCompleted && !mCanceled, E_FAIL);
|
---|
988 |
|
---|
989 | m_ulOperationPercent = aPercent;
|
---|
990 |
|
---|
991 | return S_OK;
|
---|
992 | }
|
---|
993 |
|
---|
994 | /**
|
---|
995 | * Signals that the current operation is successfully completed and advances to
|
---|
996 | * the next operation. The operation percentage is reset to 0.
|
---|
997 | *
|
---|
998 | * @param aOperationDescription Description of the next operation.
|
---|
999 | *
|
---|
1000 | * @note The current operation must not be the last one.
|
---|
1001 | */
|
---|
1002 | STDMETHODIMP Progress::SetNextOperation(IN_BSTR bstrNextOperationDescription, ULONG ulNextOperationsWeight)
|
---|
1003 | {
|
---|
1004 | AssertReturn(bstrNextOperationDescription, E_INVALIDARG);
|
---|
1005 |
|
---|
1006 | AutoCaller autoCaller(this);
|
---|
1007 | AssertComRCReturnRC(autoCaller.rc());
|
---|
1008 |
|
---|
1009 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1010 |
|
---|
1011 | AssertReturn(!mCompleted && !mCanceled, E_FAIL);
|
---|
1012 | AssertReturn(m_ulCurrentOperation + 1 < m_cOperations, E_FAIL);
|
---|
1013 |
|
---|
1014 | ++m_ulCurrentOperation;
|
---|
1015 | m_ulOperationsCompletedWeight += m_ulCurrentOperationWeight;
|
---|
1016 |
|
---|
1017 | m_bstrOperationDescription = bstrNextOperationDescription;
|
---|
1018 | m_ulCurrentOperationWeight = ulNextOperationsWeight;
|
---|
1019 | m_ulOperationPercent = 0;
|
---|
1020 |
|
---|
1021 | Log(("Progress::setNextOperation(%ls): ulNextOperationsWeight = %d; m_ulCurrentOperation is now %d, m_ulOperationsCompletedWeight is now %d\n",
|
---|
1022 | m_bstrOperationDescription.raw(), ulNextOperationsWeight, m_ulCurrentOperation, m_ulOperationsCompletedWeight));
|
---|
1023 |
|
---|
1024 | /* wake up all waiting threads */
|
---|
1025 | if (mWaitersCount > 0)
|
---|
1026 | RTSemEventMultiSignal(mCompletedSem);
|
---|
1027 |
|
---|
1028 | return S_OK;
|
---|
1029 | }
|
---|
1030 |
|
---|
1031 | // public methods only for internal purposes
|
---|
1032 | /////////////////////////////////////////////////////////////////////////////
|
---|
1033 |
|
---|
1034 | /**
|
---|
1035 | * Sets the internal result code and attempts to retrieve additional error
|
---|
1036 | * info from the current thread. Gets called from Progress::notifyComplete(),
|
---|
1037 | * but can be called again to override a previous result set with
|
---|
1038 | * notifyComplete().
|
---|
1039 | *
|
---|
1040 | * @param aResultCode
|
---|
1041 | */
|
---|
1042 | HRESULT Progress::setResultCode(HRESULT aResultCode)
|
---|
1043 | {
|
---|
1044 | AutoCaller autoCaller(this);
|
---|
1045 | AssertComRCReturnRC(autoCaller.rc());
|
---|
1046 |
|
---|
1047 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1048 |
|
---|
1049 | mResultCode = aResultCode;
|
---|
1050 |
|
---|
1051 | HRESULT rc = S_OK;
|
---|
1052 |
|
---|
1053 | if (FAILED(aResultCode))
|
---|
1054 | {
|
---|
1055 | /* try to import error info from the current thread */
|
---|
1056 |
|
---|
1057 | #if !defined (VBOX_WITH_XPCOM)
|
---|
1058 |
|
---|
1059 | ComPtr<IErrorInfo> err;
|
---|
1060 | rc = ::GetErrorInfo(0, err.asOutParam());
|
---|
1061 | if (rc == S_OK && err)
|
---|
1062 | {
|
---|
1063 | rc = err.queryInterfaceTo(mErrorInfo.asOutParam());
|
---|
1064 | if (SUCCEEDED(rc) && !mErrorInfo)
|
---|
1065 | rc = E_FAIL;
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 | #else /* !defined (VBOX_WITH_XPCOM) */
|
---|
1069 |
|
---|
1070 | nsCOMPtr<nsIExceptionService> es;
|
---|
1071 | es = do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID, &rc);
|
---|
1072 | if (NS_SUCCEEDED(rc))
|
---|
1073 | {
|
---|
1074 | nsCOMPtr <nsIExceptionManager> em;
|
---|
1075 | rc = es->GetCurrentExceptionManager(getter_AddRefs(em));
|
---|
1076 | if (NS_SUCCEEDED(rc))
|
---|
1077 | {
|
---|
1078 | ComPtr<nsIException> ex;
|
---|
1079 | rc = em->GetCurrentException(ex.asOutParam());
|
---|
1080 | if (NS_SUCCEEDED(rc) && ex)
|
---|
1081 | {
|
---|
1082 | rc = ex.queryInterfaceTo(mErrorInfo.asOutParam());
|
---|
1083 | if (NS_SUCCEEDED(rc) && !mErrorInfo)
|
---|
1084 | rc = E_FAIL;
|
---|
1085 | }
|
---|
1086 | }
|
---|
1087 | }
|
---|
1088 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
1089 |
|
---|
1090 | AssertMsg (rc == S_OK, ("Couldn't get error info (rc=%08X) while trying "
|
---|
1091 | "to set a failed result (%08X)!\n", rc, aResultCode));
|
---|
1092 | }
|
---|
1093 |
|
---|
1094 | return rc;
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 | /**
|
---|
1098 | * Marks the whole task as complete and sets the result code.
|
---|
1099 | *
|
---|
1100 | * If the result code indicates a failure (|FAILED(@a aResultCode)|) then this
|
---|
1101 | * method will import the error info from the current thread and assign it to
|
---|
1102 | * the errorInfo attribute (it will return an error if no info is available in
|
---|
1103 | * such case).
|
---|
1104 | *
|
---|
1105 | * If the result code indicates a success (|SUCCEEDED(@a aResultCode)|) then
|
---|
1106 | * the current operation is set to the last.
|
---|
1107 | *
|
---|
1108 | * Note that this method may be called only once for the given Progress object.
|
---|
1109 | * Subsequent calls will assert.
|
---|
1110 | *
|
---|
1111 | * @param aResultCode Operation result code.
|
---|
1112 | */
|
---|
1113 | HRESULT Progress::notifyComplete(HRESULT aResultCode)
|
---|
1114 | {
|
---|
1115 | AutoCaller autoCaller(this);
|
---|
1116 | AssertComRCReturnRC(autoCaller.rc());
|
---|
1117 |
|
---|
1118 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1119 |
|
---|
1120 | AssertReturn(mCompleted == FALSE, E_FAIL);
|
---|
1121 |
|
---|
1122 | if (mCanceled && SUCCEEDED(aResultCode))
|
---|
1123 | aResultCode = E_FAIL;
|
---|
1124 |
|
---|
1125 | HRESULT rc = setResultCode(aResultCode);
|
---|
1126 |
|
---|
1127 | mCompleted = TRUE;
|
---|
1128 |
|
---|
1129 | if (!FAILED(aResultCode))
|
---|
1130 | {
|
---|
1131 | m_ulCurrentOperation = m_cOperations - 1; /* last operation */
|
---|
1132 | m_ulOperationPercent = 100;
|
---|
1133 | }
|
---|
1134 |
|
---|
1135 | #if !defined VBOX_COM_INPROC
|
---|
1136 | /* remove from the global collection of pending progress operations */
|
---|
1137 | if (mParent)
|
---|
1138 | mParent->removeProgress (mId);
|
---|
1139 | #endif
|
---|
1140 |
|
---|
1141 | /* wake up all waiting threads */
|
---|
1142 | if (mWaitersCount > 0)
|
---|
1143 | RTSemEventMultiSignal (mCompletedSem);
|
---|
1144 |
|
---|
1145 | return rc;
|
---|
1146 | }
|
---|
1147 |
|
---|
1148 | /**
|
---|
1149 | * Wrapper around Progress:notifyCompleteV.
|
---|
1150 | */
|
---|
1151 | HRESULT Progress::notifyComplete(HRESULT aResultCode,
|
---|
1152 | const GUID &aIID,
|
---|
1153 | const Bstr &aComponent,
|
---|
1154 | const char *aText,
|
---|
1155 | ...)
|
---|
1156 | {
|
---|
1157 | va_list va;
|
---|
1158 | va_start(va, aText);
|
---|
1159 | HRESULT hrc = notifyCompleteV(aResultCode, aIID, aComponent, aText, va);
|
---|
1160 | va_end(va);
|
---|
1161 | return hrc;
|
---|
1162 | }
|
---|
1163 |
|
---|
1164 | /**
|
---|
1165 | * Marks the operation as complete and attaches full error info.
|
---|
1166 | *
|
---|
1167 | * See com::SupportErrorInfoImpl::setError(HRESULT, const GUID &, const wchar_t
|
---|
1168 | * *, const char *, ...) for more info.
|
---|
1169 | *
|
---|
1170 | * @param aResultCode Operation result (error) code, must not be S_OK.
|
---|
1171 | * @param aIID IID of the interface that defines the error.
|
---|
1172 | * @param aComponent Name of the component that generates the error.
|
---|
1173 | * @param aText Error message (must not be null), an RTStrPrintf-like
|
---|
1174 | * format string in UTF-8 encoding.
|
---|
1175 | * @param va List of arguments for the format string.
|
---|
1176 | */
|
---|
1177 | HRESULT Progress::notifyCompleteV(HRESULT aResultCode,
|
---|
1178 | const GUID &aIID,
|
---|
1179 | const Bstr &aComponent,
|
---|
1180 | const char *aText,
|
---|
1181 | va_list va)
|
---|
1182 | {
|
---|
1183 | Utf8Str text = Utf8StrFmtVA(aText, va);
|
---|
1184 |
|
---|
1185 | AutoCaller autoCaller(this);
|
---|
1186 | AssertComRCReturnRC(autoCaller.rc());
|
---|
1187 |
|
---|
1188 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1189 |
|
---|
1190 | AssertReturn(mCompleted == FALSE, E_FAIL);
|
---|
1191 |
|
---|
1192 | if (mCanceled && SUCCEEDED(aResultCode))
|
---|
1193 | aResultCode = E_FAIL;
|
---|
1194 |
|
---|
1195 | mCompleted = TRUE;
|
---|
1196 | mResultCode = aResultCode;
|
---|
1197 |
|
---|
1198 | AssertReturn(FAILED(aResultCode), E_FAIL);
|
---|
1199 |
|
---|
1200 | ComObjPtr<VirtualBoxErrorInfo> errorInfo;
|
---|
1201 | HRESULT rc = errorInfo.createObject();
|
---|
1202 | AssertComRC (rc);
|
---|
1203 | if (SUCCEEDED(rc))
|
---|
1204 | {
|
---|
1205 | errorInfo->init(aResultCode, aIID, aComponent, Bstr(text));
|
---|
1206 | errorInfo.queryInterfaceTo(mErrorInfo.asOutParam());
|
---|
1207 | }
|
---|
1208 |
|
---|
1209 | #if !defined VBOX_COM_INPROC
|
---|
1210 | /* remove from the global collection of pending progress operations */
|
---|
1211 | if (mParent)
|
---|
1212 | mParent->removeProgress (mId);
|
---|
1213 | #endif
|
---|
1214 |
|
---|
1215 | /* wake up all waiting threads */
|
---|
1216 | if (mWaitersCount > 0)
|
---|
1217 | RTSemEventMultiSignal(mCompletedSem);
|
---|
1218 |
|
---|
1219 | return rc;
|
---|
1220 | }
|
---|
1221 |
|
---|
1222 | /**
|
---|
1223 | * Notify the progress object that we're almost at the point of no return.
|
---|
1224 | *
|
---|
1225 | * This atomically checks for and disables cancelation. Calls to
|
---|
1226 | * IProgress::Cancel() made after a successfull call to this method will fail
|
---|
1227 | * and the user can be told. While this isn't entirely clean behavior, it
|
---|
1228 | * prevents issues with an irreversible actually operation succeeding while the
|
---|
1229 | * user belive it was rolled back.
|
---|
1230 | *
|
---|
1231 | * @returns Success indicator.
|
---|
1232 | * @retval true on success.
|
---|
1233 | * @retval false if the progress object has already been canceled or is in an
|
---|
1234 | * invalid state
|
---|
1235 | */
|
---|
1236 | bool Progress::notifyPointOfNoReturn(void)
|
---|
1237 | {
|
---|
1238 | AutoCaller autoCaller(this);
|
---|
1239 | AssertComRCReturn(autoCaller.rc(), false);
|
---|
1240 |
|
---|
1241 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1242 |
|
---|
1243 | if (mCanceled)
|
---|
1244 | {
|
---|
1245 | LogThisFunc(("returns false\n"));
|
---|
1246 | return false;
|
---|
1247 | }
|
---|
1248 |
|
---|
1249 | mCancelable = FALSE;
|
---|
1250 | LogThisFunc(("returns true\n"));
|
---|
1251 | return true;
|
---|
1252 | }
|
---|
1253 |
|
---|
1254 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1255 | // CombinedProgress class
|
---|
1256 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1257 |
|
---|
1258 | HRESULT CombinedProgress::FinalConstruct()
|
---|
1259 | {
|
---|
1260 | HRESULT rc = ProgressBase::FinalConstruct();
|
---|
1261 | if (FAILED(rc)) return rc;
|
---|
1262 |
|
---|
1263 | mProgress = 0;
|
---|
1264 | mCompletedOperations = 0;
|
---|
1265 |
|
---|
1266 | return S_OK;
|
---|
1267 | }
|
---|
1268 |
|
---|
1269 | void CombinedProgress::FinalRelease()
|
---|
1270 | {
|
---|
1271 | uninit();
|
---|
1272 | }
|
---|
1273 |
|
---|
1274 | // public initializer/uninitializer for internal purposes only
|
---|
1275 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1276 |
|
---|
1277 | /**
|
---|
1278 | * Initializes this object based on individual combined progresses.
|
---|
1279 | * Must be called only from #init()!
|
---|
1280 | *
|
---|
1281 | * @param aAutoInitSpan AutoInitSpan object instantiated by a subclass.
|
---|
1282 | * @param aParent See ProgressBase::init().
|
---|
1283 | * @param aInitiator See ProgressBase::init().
|
---|
1284 | * @param aDescription See ProgressBase::init().
|
---|
1285 | * @param aId See ProgressBase::init().
|
---|
1286 | */
|
---|
1287 | HRESULT CombinedProgress::protectedInit (AutoInitSpan &aAutoInitSpan,
|
---|
1288 | #if !defined (VBOX_COM_INPROC)
|
---|
1289 | VirtualBox *aParent,
|
---|
1290 | #endif
|
---|
1291 | IUnknown *aInitiator,
|
---|
1292 | CBSTR aDescription, OUT_GUID aId)
|
---|
1293 | {
|
---|
1294 | LogFlowThisFunc(("aDescription={%ls} mProgresses.size()=%d\n",
|
---|
1295 | aDescription, mProgresses.size()));
|
---|
1296 |
|
---|
1297 | HRESULT rc = S_OK;
|
---|
1298 |
|
---|
1299 | rc = ProgressBase::protectedInit (aAutoInitSpan,
|
---|
1300 | #if !defined (VBOX_COM_INPROC)
|
---|
1301 | aParent,
|
---|
1302 | #endif
|
---|
1303 | aInitiator, aDescription, aId);
|
---|
1304 | if (FAILED(rc)) return rc;
|
---|
1305 |
|
---|
1306 | mProgress = 0; /* the first object */
|
---|
1307 | mCompletedOperations = 0;
|
---|
1308 |
|
---|
1309 | mCompleted = FALSE;
|
---|
1310 | mCancelable = TRUE; /* until any progress returns FALSE */
|
---|
1311 | mCanceled = FALSE;
|
---|
1312 |
|
---|
1313 | m_cOperations = 0; /* will be calculated later */
|
---|
1314 |
|
---|
1315 | m_ulCurrentOperation = 0;
|
---|
1316 | rc = mProgresses[0]->COMGETTER(OperationDescription)(m_bstrOperationDescription.asOutParam());
|
---|
1317 | if (FAILED(rc)) return rc;
|
---|
1318 |
|
---|
1319 | for (size_t i = 0; i < mProgresses.size(); i ++)
|
---|
1320 | {
|
---|
1321 | if (mCancelable)
|
---|
1322 | {
|
---|
1323 | BOOL cancelable = FALSE;
|
---|
1324 | rc = mProgresses[i]->COMGETTER(Cancelable)(&cancelable);
|
---|
1325 | if (FAILED(rc)) return rc;
|
---|
1326 |
|
---|
1327 | if (!cancelable)
|
---|
1328 | mCancelable = FALSE;
|
---|
1329 | }
|
---|
1330 |
|
---|
1331 | {
|
---|
1332 | ULONG opCount = 0;
|
---|
1333 | rc = mProgresses[i]->COMGETTER(OperationCount)(&opCount);
|
---|
1334 | if (FAILED(rc)) return rc;
|
---|
1335 |
|
---|
1336 | m_cOperations += opCount;
|
---|
1337 | }
|
---|
1338 | }
|
---|
1339 |
|
---|
1340 | rc = checkProgress();
|
---|
1341 | if (FAILED(rc)) return rc;
|
---|
1342 |
|
---|
1343 | return rc;
|
---|
1344 | }
|
---|
1345 |
|
---|
1346 | /**
|
---|
1347 | * Initializes the combined progress object given two normal progress
|
---|
1348 | * objects.
|
---|
1349 | *
|
---|
1350 | * @param aParent See ProgressBase::init().
|
---|
1351 | * @param aInitiator See ProgressBase::init().
|
---|
1352 | * @param aDescription See ProgressBase::init().
|
---|
1353 | * @param aProgress1 First normal progress object.
|
---|
1354 | * @param aProgress2 Second normal progress object.
|
---|
1355 | * @param aId See ProgressBase::init().
|
---|
1356 | */
|
---|
1357 | HRESULT CombinedProgress::init(
|
---|
1358 | #if !defined (VBOX_COM_INPROC)
|
---|
1359 | VirtualBox *aParent,
|
---|
1360 | #endif
|
---|
1361 | IUnknown *aInitiator,
|
---|
1362 | CBSTR aDescription,
|
---|
1363 | IProgress *aProgress1,
|
---|
1364 | IProgress *aProgress2,
|
---|
1365 | OUT_GUID aId /* = NULL */)
|
---|
1366 | {
|
---|
1367 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1368 | AutoInitSpan autoInitSpan(this);
|
---|
1369 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1370 |
|
---|
1371 | mProgresses.resize(2);
|
---|
1372 | mProgresses[0] = aProgress1;
|
---|
1373 | mProgresses[1] = aProgress2;
|
---|
1374 |
|
---|
1375 | HRESULT rc = protectedInit(autoInitSpan,
|
---|
1376 | #if !defined (VBOX_COM_INPROC)
|
---|
1377 | aParent,
|
---|
1378 | #endif
|
---|
1379 | aInitiator,
|
---|
1380 | aDescription,
|
---|
1381 | aId);
|
---|
1382 |
|
---|
1383 | /* Confirm a successful initialization when it's the case */
|
---|
1384 | if (SUCCEEDED(rc))
|
---|
1385 | autoInitSpan.setSucceeded();
|
---|
1386 |
|
---|
1387 | return rc;
|
---|
1388 | }
|
---|
1389 |
|
---|
1390 | /**
|
---|
1391 | * Uninitializes the instance and sets the ready flag to FALSE.
|
---|
1392 | *
|
---|
1393 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
1394 | */
|
---|
1395 | void CombinedProgress::uninit()
|
---|
1396 | {
|
---|
1397 | LogFlowThisFunc(("\n"));
|
---|
1398 |
|
---|
1399 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
1400 | AutoUninitSpan autoUninitSpan(this);
|
---|
1401 | if (autoUninitSpan.uninitDone())
|
---|
1402 | return;
|
---|
1403 |
|
---|
1404 | mProgress = 0;
|
---|
1405 | mProgresses.clear();
|
---|
1406 |
|
---|
1407 | ProgressBase::protectedUninit (autoUninitSpan);
|
---|
1408 | }
|
---|
1409 |
|
---|
1410 | // IProgress properties
|
---|
1411 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1412 |
|
---|
1413 | STDMETHODIMP CombinedProgress::COMGETTER(Percent)(ULONG *aPercent)
|
---|
1414 | {
|
---|
1415 | CheckComArgOutPointerValid(aPercent);
|
---|
1416 |
|
---|
1417 | AutoCaller autoCaller(this);
|
---|
1418 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1419 |
|
---|
1420 | /* checkProgress needs a write lock */
|
---|
1421 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1422 |
|
---|
1423 | if (mCompleted && SUCCEEDED(mResultCode))
|
---|
1424 | *aPercent = 100;
|
---|
1425 | else
|
---|
1426 | {
|
---|
1427 | HRESULT rc = checkProgress();
|
---|
1428 | if (FAILED(rc)) return rc;
|
---|
1429 |
|
---|
1430 | /* global percent =
|
---|
1431 | * (100 / m_cOperations) * mOperation +
|
---|
1432 | * ((100 / m_cOperations) / 100) * m_ulOperationPercent */
|
---|
1433 | *aPercent = (100 * m_ulCurrentOperation + m_ulOperationPercent) / m_cOperations;
|
---|
1434 | }
|
---|
1435 |
|
---|
1436 | return S_OK;
|
---|
1437 | }
|
---|
1438 |
|
---|
1439 | STDMETHODIMP CombinedProgress::COMGETTER(Completed) (BOOL *aCompleted)
|
---|
1440 | {
|
---|
1441 | CheckComArgOutPointerValid(aCompleted);
|
---|
1442 |
|
---|
1443 | AutoCaller autoCaller(this);
|
---|
1444 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1445 |
|
---|
1446 | /* checkProgress needs a write lock */
|
---|
1447 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1448 |
|
---|
1449 | HRESULT rc = checkProgress();
|
---|
1450 | if (FAILED(rc)) return rc;
|
---|
1451 |
|
---|
1452 | return ProgressBase::COMGETTER(Completed) (aCompleted);
|
---|
1453 | }
|
---|
1454 |
|
---|
1455 | STDMETHODIMP CombinedProgress::COMGETTER(Canceled) (BOOL *aCanceled)
|
---|
1456 | {
|
---|
1457 | CheckComArgOutPointerValid(aCanceled);
|
---|
1458 |
|
---|
1459 | AutoCaller autoCaller(this);
|
---|
1460 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1461 |
|
---|
1462 | /* checkProgress needs a write lock */
|
---|
1463 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1464 |
|
---|
1465 | HRESULT rc = checkProgress();
|
---|
1466 | if (FAILED(rc)) return rc;
|
---|
1467 |
|
---|
1468 | return ProgressBase::COMGETTER(Canceled) (aCanceled);
|
---|
1469 | }
|
---|
1470 |
|
---|
1471 | STDMETHODIMP CombinedProgress::COMGETTER(ResultCode) (LONG *aResultCode)
|
---|
1472 | {
|
---|
1473 | CheckComArgOutPointerValid(aResultCode);
|
---|
1474 |
|
---|
1475 | AutoCaller autoCaller(this);
|
---|
1476 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1477 |
|
---|
1478 | /* checkProgress needs a write lock */
|
---|
1479 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1480 |
|
---|
1481 | HRESULT rc = checkProgress();
|
---|
1482 | if (FAILED(rc)) return rc;
|
---|
1483 |
|
---|
1484 | return ProgressBase::COMGETTER(ResultCode) (aResultCode);
|
---|
1485 | }
|
---|
1486 |
|
---|
1487 | STDMETHODIMP CombinedProgress::COMGETTER(ErrorInfo) (IVirtualBoxErrorInfo **aErrorInfo)
|
---|
1488 | {
|
---|
1489 | CheckComArgOutPointerValid(aErrorInfo);
|
---|
1490 |
|
---|
1491 | AutoCaller autoCaller(this);
|
---|
1492 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1493 |
|
---|
1494 | /* checkProgress needs a write lock */
|
---|
1495 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1496 |
|
---|
1497 | HRESULT rc = checkProgress();
|
---|
1498 | if (FAILED(rc)) return rc;
|
---|
1499 |
|
---|
1500 | return ProgressBase::COMGETTER(ErrorInfo) (aErrorInfo);
|
---|
1501 | }
|
---|
1502 |
|
---|
1503 | STDMETHODIMP CombinedProgress::COMGETTER(Operation) (ULONG *aOperation)
|
---|
1504 | {
|
---|
1505 | CheckComArgOutPointerValid(aOperation);
|
---|
1506 |
|
---|
1507 | AutoCaller autoCaller(this);
|
---|
1508 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1509 |
|
---|
1510 | /* checkProgress needs a write lock */
|
---|
1511 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1512 |
|
---|
1513 | HRESULT rc = checkProgress();
|
---|
1514 | if (FAILED(rc)) return rc;
|
---|
1515 |
|
---|
1516 | return ProgressBase::COMGETTER(Operation) (aOperation);
|
---|
1517 | }
|
---|
1518 |
|
---|
1519 | STDMETHODIMP CombinedProgress::COMGETTER(OperationDescription) (BSTR *aOperationDescription)
|
---|
1520 | {
|
---|
1521 | CheckComArgOutPointerValid(aOperationDescription);
|
---|
1522 |
|
---|
1523 | AutoCaller autoCaller(this);
|
---|
1524 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1525 |
|
---|
1526 | /* checkProgress needs a write lock */
|
---|
1527 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1528 |
|
---|
1529 | HRESULT rc = checkProgress();
|
---|
1530 | if (FAILED(rc)) return rc;
|
---|
1531 |
|
---|
1532 | return ProgressBase::COMGETTER(OperationDescription) (aOperationDescription);
|
---|
1533 | }
|
---|
1534 |
|
---|
1535 | STDMETHODIMP CombinedProgress::COMGETTER(OperationPercent)(ULONG *aOperationPercent)
|
---|
1536 | {
|
---|
1537 | CheckComArgOutPointerValid(aOperationPercent);
|
---|
1538 |
|
---|
1539 | AutoCaller autoCaller(this);
|
---|
1540 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1541 |
|
---|
1542 | /* checkProgress needs a write lock */
|
---|
1543 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1544 |
|
---|
1545 | HRESULT rc = checkProgress();
|
---|
1546 | if (FAILED(rc)) return rc;
|
---|
1547 |
|
---|
1548 | return ProgressBase::COMGETTER(OperationPercent) (aOperationPercent);
|
---|
1549 | }
|
---|
1550 |
|
---|
1551 | STDMETHODIMP CombinedProgress::COMSETTER(Timeout)(ULONG aTimeout)
|
---|
1552 | {
|
---|
1553 | NOREF(aTimeout);
|
---|
1554 | AssertFailed();
|
---|
1555 | return E_NOTIMPL;
|
---|
1556 | }
|
---|
1557 |
|
---|
1558 | STDMETHODIMP CombinedProgress::COMGETTER(Timeout)(ULONG *aTimeout)
|
---|
1559 | {
|
---|
1560 | CheckComArgOutPointerValid(aTimeout);
|
---|
1561 |
|
---|
1562 | AssertFailed();
|
---|
1563 | return E_NOTIMPL;
|
---|
1564 | }
|
---|
1565 |
|
---|
1566 | // IProgress methods
|
---|
1567 | /////////////////////////////////////////////////////////////////////////////
|
---|
1568 |
|
---|
1569 | /**
|
---|
1570 | * @note XPCOM: when this method is called not on the main XPCOM thread, it
|
---|
1571 | * simply blocks the thread until mCompletedSem is signalled. If the
|
---|
1572 | * thread has its own event queue (hmm, what for?) that it must run, then
|
---|
1573 | * calling this method will definitely freeze event processing.
|
---|
1574 | */
|
---|
1575 | STDMETHODIMP CombinedProgress::WaitForCompletion (LONG aTimeout)
|
---|
1576 | {
|
---|
1577 | LogFlowThisFuncEnter();
|
---|
1578 | LogFlowThisFunc(("aTtimeout=%d\n", aTimeout));
|
---|
1579 |
|
---|
1580 | AutoCaller autoCaller(this);
|
---|
1581 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1582 |
|
---|
1583 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1584 |
|
---|
1585 | /* if we're already completed, take a shortcut */
|
---|
1586 | if (!mCompleted)
|
---|
1587 | {
|
---|
1588 | HRESULT rc = S_OK;
|
---|
1589 | bool forever = aTimeout < 0;
|
---|
1590 | int64_t timeLeft = aTimeout;
|
---|
1591 | int64_t lastTime = RTTimeMilliTS();
|
---|
1592 |
|
---|
1593 | while (!mCompleted && (forever || timeLeft > 0))
|
---|
1594 | {
|
---|
1595 | alock.leave();
|
---|
1596 | rc = mProgresses.back()->WaitForCompletion(forever ? -1 : (LONG) timeLeft);
|
---|
1597 | alock.enter();
|
---|
1598 |
|
---|
1599 | if (SUCCEEDED(rc))
|
---|
1600 | rc = checkProgress();
|
---|
1601 |
|
---|
1602 | if (FAILED(rc)) break;
|
---|
1603 |
|
---|
1604 | if (!forever)
|
---|
1605 | {
|
---|
1606 | int64_t now = RTTimeMilliTS();
|
---|
1607 | timeLeft -= now - lastTime;
|
---|
1608 | lastTime = now;
|
---|
1609 | }
|
---|
1610 | }
|
---|
1611 |
|
---|
1612 | if (FAILED(rc)) return rc;
|
---|
1613 | }
|
---|
1614 |
|
---|
1615 | LogFlowThisFuncLeave();
|
---|
1616 |
|
---|
1617 | return S_OK;
|
---|
1618 | }
|
---|
1619 |
|
---|
1620 | /**
|
---|
1621 | * @note XPCOM: when this method is called not on the main XPCOM thread, it
|
---|
1622 | * simply blocks the thread until mCompletedSem is signalled. If the
|
---|
1623 | * thread has its own event queue (hmm, what for?) that it must run, then
|
---|
1624 | * calling this method will definitely freeze event processing.
|
---|
1625 | */
|
---|
1626 | STDMETHODIMP CombinedProgress::WaitForOperationCompletion (ULONG aOperation, LONG aTimeout)
|
---|
1627 | {
|
---|
1628 | LogFlowThisFuncEnter();
|
---|
1629 | LogFlowThisFunc(("aOperation=%d, aTimeout=%d\n", aOperation, aTimeout));
|
---|
1630 |
|
---|
1631 | AutoCaller autoCaller(this);
|
---|
1632 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1633 |
|
---|
1634 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1635 |
|
---|
1636 | if (aOperation >= m_cOperations)
|
---|
1637 | return setError(E_FAIL,
|
---|
1638 | tr("Operation number must be in range [0, %d]"), m_ulCurrentOperation - 1);
|
---|
1639 |
|
---|
1640 | /* if we're already completed or if the given operation is already done,
|
---|
1641 | * then take a shortcut */
|
---|
1642 | if (!mCompleted && aOperation >= m_ulCurrentOperation)
|
---|
1643 | {
|
---|
1644 | HRESULT rc = S_OK;
|
---|
1645 |
|
---|
1646 | /* find the right progress object to wait for */
|
---|
1647 | size_t progress = mProgress;
|
---|
1648 | ULONG operation = 0, completedOps = mCompletedOperations;
|
---|
1649 | do
|
---|
1650 | {
|
---|
1651 | ULONG opCount = 0;
|
---|
1652 | rc = mProgresses[progress]->COMGETTER(OperationCount)(&opCount);
|
---|
1653 | if (FAILED(rc))
|
---|
1654 | return rc;
|
---|
1655 |
|
---|
1656 | if (completedOps + opCount > aOperation)
|
---|
1657 | {
|
---|
1658 | /* found the right progress object */
|
---|
1659 | operation = aOperation - completedOps;
|
---|
1660 | break;
|
---|
1661 | }
|
---|
1662 |
|
---|
1663 | completedOps += opCount;
|
---|
1664 | progress ++;
|
---|
1665 | ComAssertRet(progress < mProgresses.size(), E_FAIL);
|
---|
1666 | }
|
---|
1667 | while (1);
|
---|
1668 |
|
---|
1669 | LogFlowThisFunc(("will wait for mProgresses [%d] (%d)\n",
|
---|
1670 | progress, operation));
|
---|
1671 |
|
---|
1672 | bool forever = aTimeout < 0;
|
---|
1673 | int64_t timeLeft = aTimeout;
|
---|
1674 | int64_t lastTime = RTTimeMilliTS();
|
---|
1675 |
|
---|
1676 | while (!mCompleted && aOperation >= m_ulCurrentOperation &&
|
---|
1677 | (forever || timeLeft > 0))
|
---|
1678 | {
|
---|
1679 | alock.leave();
|
---|
1680 | /* wait for the appropriate progress operation completion */
|
---|
1681 | rc = mProgresses[progress]-> WaitForOperationCompletion(operation,
|
---|
1682 | forever ? -1 : (LONG) timeLeft);
|
---|
1683 | alock.enter();
|
---|
1684 |
|
---|
1685 | if (SUCCEEDED(rc))
|
---|
1686 | rc = checkProgress();
|
---|
1687 |
|
---|
1688 | if (FAILED(rc)) break;
|
---|
1689 |
|
---|
1690 | if (!forever)
|
---|
1691 | {
|
---|
1692 | int64_t now = RTTimeMilliTS();
|
---|
1693 | timeLeft -= now - lastTime;
|
---|
1694 | lastTime = now;
|
---|
1695 | }
|
---|
1696 | }
|
---|
1697 |
|
---|
1698 | if (FAILED(rc)) return rc;
|
---|
1699 | }
|
---|
1700 |
|
---|
1701 | LogFlowThisFuncLeave();
|
---|
1702 |
|
---|
1703 | return S_OK;
|
---|
1704 | }
|
---|
1705 |
|
---|
1706 | STDMETHODIMP CombinedProgress::Cancel()
|
---|
1707 | {
|
---|
1708 | AutoCaller autoCaller(this);
|
---|
1709 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1710 |
|
---|
1711 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1712 |
|
---|
1713 | if (!mCancelable)
|
---|
1714 | return setError(E_FAIL, tr("Operation cannot be canceled"));
|
---|
1715 |
|
---|
1716 | if (!mCanceled)
|
---|
1717 | {
|
---|
1718 | LogThisFunc(("Canceling\n"));
|
---|
1719 | mCanceled = TRUE;
|
---|
1720 | /** @todo Teleportation: Shouldn't this be propagated to mProgresses? If
|
---|
1721 | * powerUp creates passes a combined progress object to the client, I
|
---|
1722 | * won't get called back since I'm only getting the powerupProgress ...
|
---|
1723 | * Or what? */
|
---|
1724 | if (m_pfnCancelCallback)
|
---|
1725 | m_pfnCancelCallback(m_pvCancelUserArg);
|
---|
1726 |
|
---|
1727 | }
|
---|
1728 | else
|
---|
1729 | LogThisFunc(("Already canceled\n"));
|
---|
1730 |
|
---|
1731 | return S_OK;
|
---|
1732 | }
|
---|
1733 |
|
---|
1734 | // private methods
|
---|
1735 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1736 |
|
---|
1737 | /**
|
---|
1738 | * Fetches the properties of the current progress object and, if it is
|
---|
1739 | * successfully completed, advances to the next uncompleted or unsuccessfully
|
---|
1740 | * completed object in the vector of combined progress objects.
|
---|
1741 | *
|
---|
1742 | * @note Must be called from under this object's write lock!
|
---|
1743 | */
|
---|
1744 | HRESULT CombinedProgress::checkProgress()
|
---|
1745 | {
|
---|
1746 | /* do nothing if we're already marked ourselves as completed */
|
---|
1747 | if (mCompleted)
|
---|
1748 | return S_OK;
|
---|
1749 |
|
---|
1750 | AssertReturn(mProgress < mProgresses.size(), E_FAIL);
|
---|
1751 |
|
---|
1752 | ComPtr<IProgress> progress = mProgresses[mProgress];
|
---|
1753 | ComAssertRet(!progress.isNull(), E_FAIL);
|
---|
1754 |
|
---|
1755 | HRESULT rc = S_OK;
|
---|
1756 | BOOL fCompleted = FALSE;
|
---|
1757 |
|
---|
1758 | do
|
---|
1759 | {
|
---|
1760 | rc = progress->COMGETTER(Completed)(&fCompleted);
|
---|
1761 | if (FAILED(rc))
|
---|
1762 | return rc;
|
---|
1763 |
|
---|
1764 | if (fCompleted)
|
---|
1765 | {
|
---|
1766 | rc = progress->COMGETTER(Canceled)(&mCanceled);
|
---|
1767 | if (FAILED(rc))
|
---|
1768 | return rc;
|
---|
1769 |
|
---|
1770 | LONG iRc;
|
---|
1771 | rc = progress->COMGETTER(ResultCode)(&iRc);
|
---|
1772 | if (FAILED(rc))
|
---|
1773 | return rc;
|
---|
1774 | mResultCode = iRc;
|
---|
1775 |
|
---|
1776 | if (FAILED(mResultCode))
|
---|
1777 | {
|
---|
1778 | rc = progress->COMGETTER(ErrorInfo) (mErrorInfo.asOutParam());
|
---|
1779 | if (FAILED(rc))
|
---|
1780 | return rc;
|
---|
1781 | }
|
---|
1782 |
|
---|
1783 | if (FAILED(mResultCode) || mCanceled)
|
---|
1784 | {
|
---|
1785 | mCompleted = TRUE;
|
---|
1786 | }
|
---|
1787 | else
|
---|
1788 | {
|
---|
1789 | ULONG opCount = 0;
|
---|
1790 | rc = progress->COMGETTER(OperationCount) (&opCount);
|
---|
1791 | if (FAILED(rc))
|
---|
1792 | return rc;
|
---|
1793 |
|
---|
1794 | mCompletedOperations += opCount;
|
---|
1795 | mProgress ++;
|
---|
1796 |
|
---|
1797 | if (mProgress < mProgresses.size())
|
---|
1798 | progress = mProgresses[mProgress];
|
---|
1799 | else
|
---|
1800 | mCompleted = TRUE;
|
---|
1801 | }
|
---|
1802 | }
|
---|
1803 | }
|
---|
1804 | while (fCompleted && !mCompleted);
|
---|
1805 |
|
---|
1806 | rc = progress->COMGETTER(OperationPercent) (&m_ulOperationPercent);
|
---|
1807 | if (SUCCEEDED(rc))
|
---|
1808 | {
|
---|
1809 | ULONG operation = 0;
|
---|
1810 | rc = progress->COMGETTER(Operation) (&operation);
|
---|
1811 | if (SUCCEEDED(rc) && mCompletedOperations + operation > m_ulCurrentOperation)
|
---|
1812 | {
|
---|
1813 | m_ulCurrentOperation = mCompletedOperations + operation;
|
---|
1814 | rc = progress->COMGETTER(OperationDescription) (
|
---|
1815 | m_bstrOperationDescription.asOutParam());
|
---|
1816 | }
|
---|
1817 | }
|
---|
1818 |
|
---|
1819 | return rc;
|
---|
1820 | }
|
---|
1821 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|