[13580] | 1 | /* $Id: ProgressImpl.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
[1] | 2 | /** @file
|
---|
[18252] | 3 | * VirtualBox Progress COM class implementation
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
| 11 | *
|
---|
| 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
| 25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[1] | 26 | */
|
---|
| 27 |
|
---|
[76592] | 28 | #define LOG_GROUP LOG_GROUP_MAIN_PROGRESS
|
---|
[13915] | 29 | #include <iprt/types.h>
|
---|
| 30 |
|
---|
[32780] | 31 | #if defined(VBOX_WITH_XPCOM)
|
---|
[1] | 32 | #include <nsIServiceManager.h>
|
---|
| 33 | #include <nsIExceptionService.h>
|
---|
| 34 | #include <nsCOMPtr.h>
|
---|
[32780] | 35 | #endif /* defined(VBOX_WITH_XPCOM) */
|
---|
[1] | 36 |
|
---|
[44124] | 37 | #include "ProgressImpl.h"
|
---|
[13580] | 38 |
|
---|
[46820] | 39 | #if !defined(VBOX_COM_INPROC)
|
---|
| 40 | # include "VirtualBoxImpl.h"
|
---|
| 41 | #endif
|
---|
[1] | 42 | #include "VirtualBoxErrorInfoImpl.h"
|
---|
| 43 |
|
---|
| 44 | #include <iprt/time.h>
|
---|
| 45 | #include <iprt/semaphore.h>
|
---|
[30681] | 46 | #include <iprt/cpp/utils.h>
|
---|
[1] | 47 |
|
---|
[76474] | 48 | #include <iprt/errcore.h>
|
---|
[76592] | 49 |
|
---|
[44124] | 50 | #include "AutoCaller.h"
|
---|
[76592] | 51 | #include "LoggingNew.h"
|
---|
[69002] | 52 | #include "VBoxEvents.h"
|
---|
[1] | 53 |
|
---|
[76592] | 54 |
|
---|
[44288] | 55 | Progress::Progress()
|
---|
[32780] | 56 | #if !defined(VBOX_COM_INPROC)
|
---|
[27607] | 57 | : mParent(NULL)
|
---|
| 58 | #endif
|
---|
| 59 | {
|
---|
| 60 | }
|
---|
[13580] | 61 |
|
---|
[44288] | 62 | Progress::~Progress()
|
---|
[27607] | 63 | {
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 |
|
---|
[1] | 67 | HRESULT Progress::FinalConstruct()
|
---|
| 68 | {
|
---|
[44288] | 69 | mCancelable = FALSE;
|
---|
| 70 | mCompleted = FALSE;
|
---|
| 71 | mCanceled = FALSE;
|
---|
| 72 | mResultCode = S_OK;
|
---|
| 73 |
|
---|
| 74 | m_cOperations
|
---|
| 75 | = m_ulTotalOperationsWeight
|
---|
| 76 | = m_ulOperationsCompletedWeight
|
---|
| 77 | = m_ulCurrentOperation
|
---|
| 78 | = m_ulCurrentOperationWeight
|
---|
| 79 | = m_ulOperationPercent
|
---|
| 80 | = m_cMsTimeout
|
---|
| 81 | = 0;
|
---|
| 82 |
|
---|
| 83 | // get creation timestamp
|
---|
| 84 | m_ullTimestamp = RTTimeMilliTS();
|
---|
| 85 |
|
---|
| 86 | m_pfnCancelCallback = NULL;
|
---|
| 87 | m_pvCancelUserArg = NULL;
|
---|
| 88 |
|
---|
[351] | 89 | mCompletedSem = NIL_RTSEMEVENTMULTI;
|
---|
[1] | 90 | mWaitersCount = 0;
|
---|
| 91 |
|
---|
[54992] | 92 | return Progress::BaseFinalConstruct();
|
---|
[1] | 93 | }
|
---|
| 94 |
|
---|
| 95 | void Progress::FinalRelease()
|
---|
| 96 | {
|
---|
| 97 | uninit();
|
---|
[35638] | 98 | BaseFinalRelease();
|
---|
[1] | 99 | }
|
---|
| 100 |
|
---|
| 101 | // public initializer/uninitializer for internal purposes only
|
---|
| 102 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 103 |
|
---|
| 104 | /**
|
---|
[18276] | 105 | * Initializes the normal progress object. With this variant, one can have
|
---|
| 106 | * an arbitrary number of sub-operation which IProgress can analyze to
|
---|
| 107 | * have a weighted progress computed.
|
---|
[1] | 108 | *
|
---|
[18276] | 109 | * For example, say that one IProgress is supposed to track the cloning
|
---|
| 110 | * of two hard disk images, which are 100 MB and 1000 MB in size, respectively,
|
---|
| 111 | * and each of these hard disks should be one sub-operation of the IProgress.
|
---|
| 112 | *
|
---|
| 113 | * Obviously the progress would be misleading if the progress displayed 50%
|
---|
| 114 | * after the smaller image was cloned and would then take much longer for
|
---|
| 115 | * the second half.
|
---|
| 116 | *
|
---|
| 117 | * With weighted progress, one can invoke the following calls:
|
---|
| 118 | *
|
---|
| 119 | * 1) create progress object with cOperations = 2 and ulTotalOperationsWeight =
|
---|
| 120 | * 1100 (100 MB plus 1100, but really the weights can be any ULONG); pass
|
---|
| 121 | * in ulFirstOperationWeight = 100 for the first sub-operation
|
---|
| 122 | *
|
---|
| 123 | * 2) Then keep calling setCurrentOperationProgress() with a percentage
|
---|
| 124 | * for the first image; the total progress will increase up to a value
|
---|
| 125 | * of 9% (100MB / 1100MB * 100%).
|
---|
| 126 | *
|
---|
| 127 | * 3) Then call setNextOperation with the second weight (1000 for the megabytes
|
---|
| 128 | * of the second disk).
|
---|
| 129 | *
|
---|
| 130 | * 4) Then keep calling setCurrentOperationProgress() with a percentage for
|
---|
| 131 | * the second image, where 100% of the operation will then yield a 100%
|
---|
| 132 | * progress of the entire task.
|
---|
| 133 | *
|
---|
| 134 | * Weighting is optional; you can simply assign a weight of 1 to each operation
|
---|
| 135 | * and pass ulTotalOperationsWeight == cOperations to this constructor (but
|
---|
| 136 | * for that variant and for backwards-compatibility a simpler constructor exists
|
---|
| 137 | * in ProgressImpl.h as well).
|
---|
| 138 | *
|
---|
| 139 | * Even simpler, if you need no sub-operations at all, pass in cOperations =
|
---|
| 140 | * ulTotalOperationsWeight = ulFirstOperationWeight = 1.
|
---|
| 141 | *
|
---|
[51687] | 142 | * @param aParent Parent object (only for server-side Progress objects).
|
---|
| 143 | * @param aInitiator Initiator of the task (for server-side objects. Can be
|
---|
| 144 | * NULL which means initiator = parent, otherwise must not
|
---|
| 145 | * be NULL).
|
---|
| 146 | * @param aDescription Overall task description.
|
---|
| 147 | * @param aCancelable Flag whether the task maybe canceled.
|
---|
| 148 | * @param cOperations Number of operations within this task (at least 1).
|
---|
[18276] | 149 | * @param ulTotalOperationsWeight Total weight of operations; must be the sum of ulFirstOperationWeight and
|
---|
| 150 | * what is later passed with each subsequent setNextOperation() call.
|
---|
[65103] | 151 | * @param aFirstOperationDescription Description of the first operation.
|
---|
[18276] | 152 | * @param ulFirstOperationWeight Weight of first sub-operation.
|
---|
[1] | 153 | */
|
---|
[32780] | 154 | HRESULT Progress::init(
|
---|
| 155 | #if !defined(VBOX_COM_INPROC)
|
---|
| 156 | VirtualBox *aParent,
|
---|
[1] | 157 | #endif
|
---|
[32780] | 158 | IUnknown *aInitiator,
|
---|
[73824] | 159 | const Utf8Str &aDescription,
|
---|
[32780] | 160 | BOOL aCancelable,
|
---|
| 161 | ULONG cOperations,
|
---|
| 162 | ULONG ulTotalOperationsWeight,
|
---|
[73824] | 163 | const Utf8Str &aFirstOperationDescription,
|
---|
[51687] | 164 | ULONG ulFirstOperationWeight)
|
---|
[1] | 165 | {
|
---|
[50952] | 166 | LogFlowThisFunc(("aDescription=\"%s\", cOperations=%d, ulTotalOperationsWeight=%d, aFirstOperationDescription=\"%s\", ulFirstOperationWeight=%d\n",
|
---|
[50874] | 167 | aDescription.c_str(),
|
---|
[23223] | 168 | cOperations,
|
---|
| 169 | ulTotalOperationsWeight,
|
---|
[50874] | 170 | aFirstOperationDescription.c_str(),
|
---|
[23223] | 171 | ulFirstOperationWeight));
|
---|
[1] | 172 |
|
---|
[18269] | 173 | AssertReturn(ulTotalOperationsWeight >= 1, E_INVALIDARG);
|
---|
[1] | 174 |
|
---|
[13580] | 175 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
[21878] | 176 | AutoInitSpan autoInitSpan(this);
|
---|
| 177 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
[1] | 178 |
|
---|
[94906] | 179 | HRESULT hrc = unconst(pEventSource).createObject();
|
---|
| 180 | if (FAILED(hrc))
|
---|
| 181 | return hrc;
|
---|
[1] | 182 |
|
---|
[94906] | 183 | hrc = pEventSource->init();
|
---|
| 184 | if (FAILED(hrc))
|
---|
| 185 | return hrc;
|
---|
[69002] | 186 |
|
---|
[32780] | 187 | #if !defined(VBOX_COM_INPROC)
|
---|
[44288] | 188 | AssertReturn(aParent, E_INVALIDARG);
|
---|
| 189 | #else
|
---|
| 190 | AssertReturn(aInitiator, E_INVALIDARG);
|
---|
[1] | 191 | #endif
|
---|
[44288] | 192 |
|
---|
| 193 | #if !defined(VBOX_COM_INPROC)
|
---|
| 194 | /* share parent weakly */
|
---|
| 195 | unconst(mParent) = aParent;
|
---|
| 196 | #endif
|
---|
| 197 |
|
---|
| 198 | #if !defined(VBOX_COM_INPROC)
|
---|
| 199 | /* assign (and therefore addref) initiator only if it is not VirtualBox
|
---|
[51687] | 200 | * (to avoid cycling); otherwise mInitiator will remain null which means
|
---|
| 201 | * that it is the same as the parent */
|
---|
[44288] | 202 | if (aInitiator)
|
---|
| 203 | {
|
---|
| 204 | ComObjPtr<VirtualBox> pVirtualBox(mParent);
|
---|
| 205 | if (!(pVirtualBox == aInitiator))
|
---|
| 206 | unconst(mInitiator) = aInitiator;
|
---|
| 207 | }
|
---|
| 208 | #else
|
---|
| 209 | unconst(mInitiator) = aInitiator;
|
---|
| 210 | #endif
|
---|
| 211 |
|
---|
| 212 | unconst(mId).create();
|
---|
| 213 |
|
---|
[73853] | 214 | #if !defined(VBOX_COM_INPROC)
|
---|
[44288] | 215 | /* add to the global collection of progress operations (note: after
|
---|
[51687] | 216 | * creating mId) */
|
---|
[50355] | 217 | mParent->i_addProgress(this);
|
---|
[44288] | 218 | #endif
|
---|
| 219 |
|
---|
| 220 | unconst(mDescription) = aDescription;
|
---|
| 221 |
|
---|
[13580] | 222 | mCancelable = aCancelable;
|
---|
[1] | 223 |
|
---|
[18269] | 224 | m_cOperations = cOperations;
|
---|
| 225 | m_ulTotalOperationsWeight = ulTotalOperationsWeight;
|
---|
| 226 | m_ulOperationsCompletedWeight = 0;
|
---|
| 227 | m_ulCurrentOperation = 0;
|
---|
[50874] | 228 | m_operationDescription = aFirstOperationDescription;
|
---|
[18269] | 229 | m_ulCurrentOperationWeight = ulFirstOperationWeight;
|
---|
| 230 | m_ulOperationPercent = 0;
|
---|
[1] | 231 |
|
---|
[32780] | 232 | int vrc = RTSemEventMultiCreate(&mCompletedSem);
|
---|
| 233 | ComAssertRCRet(vrc, E_FAIL);
|
---|
[1] | 234 |
|
---|
[32780] | 235 | RTSemEventMultiReset(mCompletedSem);
|
---|
[1] | 236 |
|
---|
[74759] | 237 | /* Confirm a successful initialization. */
|
---|
| 238 | autoInitSpan.setSucceeded();
|
---|
[1] | 239 |
|
---|
[74759] | 240 | return S_OK;
|
---|
[1] | 241 | }
|
---|
| 242 |
|
---|
[13580] | 243 | /**
|
---|
| 244 | * Initializes the sub-progress object that represents a specific operation of
|
---|
| 245 | * the whole task.
|
---|
| 246 | *
|
---|
| 247 | * Objects initialized with this method are then combined together into the
|
---|
[44288] | 248 | * single task using a Progress instance, so it doesn't require the
|
---|
[13580] | 249 | * parent, initiator, description and doesn't create an ID. Note that calling
|
---|
| 250 | * respective getter methods on an object initialized with this method is
|
---|
| 251 | * useless. Such objects are used only to provide a separate wait semaphore and
|
---|
| 252 | * store individual operation descriptions.
|
---|
| 253 | *
|
---|
| 254 | * @param aCancelable Flag whether the task maybe canceled.
|
---|
| 255 | * @param aOperationCount Number of sub-operations within this task (at least 1).
|
---|
| 256 | * @param aOperationDescription Description of the individual operation.
|
---|
| 257 | */
|
---|
[18269] | 258 | HRESULT Progress::init(BOOL aCancelable,
|
---|
| 259 | ULONG aOperationCount,
|
---|
[73824] | 260 | const Utf8Str &aOperationDescription)
|
---|
[1] | 261 | {
|
---|
[50874] | 262 | LogFlowThisFunc(("aOperationDescription=\"%s\"\n", aOperationDescription.c_str()));
|
---|
[1] | 263 |
|
---|
[13580] | 264 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
[21878] | 265 | AutoInitSpan autoInitSpan(this);
|
---|
| 266 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
[1] | 267 |
|
---|
[13580] | 268 | mCancelable = aCancelable;
|
---|
[1] | 269 |
|
---|
[18269] | 270 | // for this variant we assume for now that all operations are weighed "1"
|
---|
| 271 | // and equal total weight = operation count
|
---|
| 272 | m_cOperations = aOperationCount;
|
---|
| 273 | m_ulTotalOperationsWeight = aOperationCount;
|
---|
| 274 | m_ulOperationsCompletedWeight = 0;
|
---|
| 275 | m_ulCurrentOperation = 0;
|
---|
[50874] | 276 | m_operationDescription = aOperationDescription;
|
---|
[18269] | 277 | m_ulCurrentOperationWeight = 1;
|
---|
| 278 | m_ulOperationPercent = 0;
|
---|
[1] | 279 |
|
---|
[32780] | 280 | int vrc = RTSemEventMultiCreate(&mCompletedSem);
|
---|
| 281 | ComAssertRCRet(vrc, E_FAIL);
|
---|
[1] | 282 |
|
---|
[32780] | 283 | RTSemEventMultiReset(mCompletedSem);
|
---|
[1] | 284 |
|
---|
[74759] | 285 | /* Confirm a successful initialization. */
|
---|
| 286 | autoInitSpan.setSucceeded();
|
---|
[1] | 287 |
|
---|
[74759] | 288 | return S_OK;
|
---|
[1] | 289 | }
|
---|
| 290 |
|
---|
[44288] | 291 |
|
---|
[1] | 292 | /**
|
---|
[13580] | 293 | * Uninitializes the instance and sets the ready flag to FALSE.
|
---|
| 294 | *
|
---|
| 295 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
[1] | 296 | */
|
---|
| 297 | void Progress::uninit()
|
---|
| 298 | {
|
---|
[21878] | 299 | LogFlowThisFunc(("\n"));
|
---|
[1] | 300 |
|
---|
[13580] | 301 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
[21878] | 302 | AutoUninitSpan autoUninitSpan(this);
|
---|
[13580] | 303 | if (autoUninitSpan.uninitDone())
|
---|
[1] | 304 | return;
|
---|
| 305 |
|
---|
[13580] | 306 | /* wake up all threads still waiting on occasion */
|
---|
[1] | 307 | if (mWaitersCount > 0)
|
---|
| 308 | {
|
---|
[50874] | 309 | LogFlow(("WARNING: There are still %d threads waiting for '%s' completion!\n",
|
---|
| 310 | mWaitersCount, mDescription.c_str()));
|
---|
[32780] | 311 | RTSemEventMultiSignal(mCompletedSem);
|
---|
[1] | 312 | }
|
---|
| 313 |
|
---|
[32780] | 314 | RTSemEventMultiDestroy(mCompletedSem);
|
---|
[1] | 315 |
|
---|
[54992] | 316 | /* release initiator (effective only if mInitiator has been assigned in init()) */
|
---|
[44288] | 317 | unconst(mInitiator).setNull();
|
---|
| 318 |
|
---|
[73853] | 319 | #if !defined(VBOX_COM_INPROC)
|
---|
[44288] | 320 | if (mParent)
|
---|
| 321 | {
|
---|
| 322 | /* remove the added progress on failure to complete the initialization */
|
---|
| 323 | if (autoUninitSpan.initFailed() && mId.isValid() && !mId.isZero())
|
---|
[50355] | 324 | mParent->i_removeProgress(mId.ref());
|
---|
[44288] | 325 |
|
---|
| 326 | unconst(mParent) = NULL;
|
---|
| 327 | }
|
---|
| 328 | #endif
|
---|
[1] | 329 | }
|
---|
| 330 |
|
---|
[44288] | 331 |
|
---|
[54992] | 332 | // public methods only for internal purposes
|
---|
| 333 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 334 |
|
---|
| 335 | /**
|
---|
| 336 | * Marks the whole task as complete and sets the result code.
|
---|
| 337 | *
|
---|
| 338 | * If the result code indicates a failure (|FAILED(@a aResultCode)|) then this
|
---|
| 339 | * method will import the error info from the current thread and assign it to
|
---|
| 340 | * the errorInfo attribute (it will return an error if no info is available in
|
---|
| 341 | * such case).
|
---|
| 342 | *
|
---|
| 343 | * If the result code indicates a success (|SUCCEEDED(@a aResultCode)|) then
|
---|
| 344 | * the current operation is set to the last.
|
---|
| 345 | *
|
---|
| 346 | * Note that this method may be called only once for the given Progress object.
|
---|
| 347 | * Subsequent calls will assert.
|
---|
| 348 | *
|
---|
| 349 | * @param aResultCode Operation result code.
|
---|
| 350 | */
|
---|
| 351 | HRESULT Progress::i_notifyComplete(HRESULT aResultCode)
|
---|
| 352 | {
|
---|
[94906] | 353 | HRESULT hrc;
|
---|
[55190] | 354 | ComPtr<IVirtualBoxErrorInfo> errorInfo;
|
---|
[54992] | 355 | if (FAILED(aResultCode))
|
---|
| 356 | {
|
---|
| 357 | /* try to import error info from the current thread */
|
---|
| 358 | #if !defined(VBOX_WITH_XPCOM)
|
---|
| 359 | ComPtr<IErrorInfo> err;
|
---|
[94906] | 360 | hrc = ::GetErrorInfo(0, err.asOutParam());
|
---|
| 361 | if (hrc == S_OK && err)
|
---|
| 362 | hrc = err.queryInterfaceTo(errorInfo.asOutParam());
|
---|
[54992] | 363 | #else /* !defined(VBOX_WITH_XPCOM) */
|
---|
| 364 | nsCOMPtr<nsIExceptionService> es;
|
---|
[94906] | 365 | es = do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID, &hrc);
|
---|
| 366 | if (NS_SUCCEEDED(hrc))
|
---|
[54992] | 367 | {
|
---|
| 368 | nsCOMPtr <nsIExceptionManager> em;
|
---|
[94906] | 369 | hrc = es->GetCurrentExceptionManager(getter_AddRefs(em));
|
---|
| 370 | if (NS_SUCCEEDED(hrc))
|
---|
[54992] | 371 | {
|
---|
| 372 | ComPtr<nsIException> ex;
|
---|
[94906] | 373 | hrc = em->GetCurrentException(ex.asOutParam());
|
---|
| 374 | if (NS_SUCCEEDED(hrc) && ex)
|
---|
| 375 | hrc = ex.queryInterfaceTo(errorInfo.asOutParam());
|
---|
[54992] | 376 | }
|
---|
| 377 | }
|
---|
| 378 | #endif /* !defined(VBOX_WITH_XPCOM) */
|
---|
| 379 | }
|
---|
| 380 |
|
---|
[85251] | 381 | return i_notifyCompleteWorker(aResultCode, errorInfo);
|
---|
[54992] | 382 | }
|
---|
| 383 |
|
---|
| 384 | /**
|
---|
| 385 | * Wrapper around Progress:notifyCompleteV.
|
---|
| 386 | */
|
---|
| 387 | HRESULT Progress::i_notifyComplete(HRESULT aResultCode,
|
---|
| 388 | const GUID &aIID,
|
---|
| 389 | const char *pcszComponent,
|
---|
| 390 | const char *aText,
|
---|
| 391 | ...)
|
---|
| 392 | {
|
---|
| 393 | va_list va;
|
---|
| 394 | va_start(va, aText);
|
---|
| 395 | HRESULT hrc = i_notifyCompleteV(aResultCode, aIID, pcszComponent, aText, va);
|
---|
| 396 | va_end(va);
|
---|
| 397 | return hrc;
|
---|
| 398 | }
|
---|
| 399 |
|
---|
| 400 | /**
|
---|
| 401 | * Marks the operation as complete and attaches full error info.
|
---|
| 402 | *
|
---|
| 403 | * @param aResultCode Operation result (error) code, must not be S_OK.
|
---|
| 404 | * @param aIID IID of the interface that defines the error.
|
---|
[65103] | 405 | * @param pcszComponent Name of the component that generates the error.
|
---|
[54992] | 406 | * @param aText Error message (must not be null), an RTStrPrintf-like
|
---|
| 407 | * format string in UTF-8 encoding.
|
---|
| 408 | * @param va List of arguments for the format string.
|
---|
| 409 | */
|
---|
| 410 | HRESULT Progress::i_notifyCompleteV(HRESULT aResultCode,
|
---|
| 411 | const GUID &aIID,
|
---|
| 412 | const char *pcszComponent,
|
---|
| 413 | const char *aText,
|
---|
| 414 | va_list va)
|
---|
| 415 | {
|
---|
| 416 | /* expected to be used only in case of error */
|
---|
| 417 | Assert(FAILED(aResultCode));
|
---|
| 418 |
|
---|
| 419 | Utf8Str text(aText, va);
|
---|
| 420 | ComObjPtr<VirtualBoxErrorInfo> errorInfo;
|
---|
[94906] | 421 | HRESULT hrc = errorInfo.createObject();
|
---|
| 422 | AssertComRCReturnRC(hrc);
|
---|
[54992] | 423 | errorInfo->init(aResultCode, aIID, pcszComponent, text);
|
---|
| 424 |
|
---|
[85251] | 425 | return i_notifyCompleteWorker(aResultCode, errorInfo);
|
---|
[54992] | 426 | }
|
---|
| 427 |
|
---|
| 428 | /**
|
---|
[78666] | 429 | * Wrapper around Progress:notifyCompleteBothV.
|
---|
| 430 | */
|
---|
| 431 | HRESULT Progress::i_notifyCompleteBoth(HRESULT aResultCode,
|
---|
| 432 | int vrc,
|
---|
| 433 | const GUID &aIID,
|
---|
| 434 | const char *pcszComponent,
|
---|
| 435 | const char *aText,
|
---|
| 436 | ...)
|
---|
| 437 | {
|
---|
| 438 | va_list va;
|
---|
| 439 | va_start(va, aText);
|
---|
| 440 | HRESULT hrc = i_notifyCompleteBothV(aResultCode, vrc, aIID, pcszComponent, aText, va);
|
---|
| 441 | va_end(va);
|
---|
| 442 | return hrc;
|
---|
| 443 | }
|
---|
| 444 |
|
---|
| 445 | /**
|
---|
| 446 | * Marks the operation as complete and attaches full error info.
|
---|
| 447 | *
|
---|
| 448 | * @param aResultCode Operation result (error) code, must not be S_OK.
|
---|
| 449 | * @param vrc VBox status code to associate with the error.
|
---|
| 450 | * @param aIID IID of the interface that defines the error.
|
---|
| 451 | * @param pszComponent Name of the component that generates the error.
|
---|
| 452 | * @param pszFormat Error message (must not be null), an RTStrPrintf-like
|
---|
| 453 | * format string in UTF-8 encoding.
|
---|
| 454 | * @param va List of arguments for the format string.
|
---|
| 455 | */
|
---|
| 456 | HRESULT Progress::i_notifyCompleteBothV(HRESULT aResultCode,
|
---|
| 457 | int vrc,
|
---|
| 458 | const GUID &aIID,
|
---|
| 459 | const char *pszComponent,
|
---|
| 460 | const char *pszFormat,
|
---|
| 461 | va_list va)
|
---|
| 462 | {
|
---|
| 463 | /* expected to be used only in case of error */
|
---|
| 464 | Assert(FAILED(aResultCode));
|
---|
| 465 |
|
---|
| 466 | Utf8Str text(pszFormat, va);
|
---|
| 467 | ComObjPtr<VirtualBoxErrorInfo> errorInfo;
|
---|
[94906] | 468 | HRESULT hrc = errorInfo.createObject();
|
---|
| 469 | AssertComRCReturnRC(hrc);
|
---|
[78666] | 470 | errorInfo->initEx(aResultCode, vrc, aIID, pszComponent, text);
|
---|
| 471 |
|
---|
[85251] | 472 | return i_notifyCompleteWorker(aResultCode, errorInfo);
|
---|
[78666] | 473 | }
|
---|
| 474 |
|
---|
| 475 | /**
|
---|
[54992] | 476 | * Sets the cancelation callback, checking for cancelation first.
|
---|
| 477 | *
|
---|
| 478 | * @returns Success indicator.
|
---|
| 479 | * @retval true on success.
|
---|
| 480 | * @retval false if the progress object has already been canceled or is in an
|
---|
| 481 | * invalid state
|
---|
| 482 | *
|
---|
| 483 | * @param pfnCallback The function to be called upon cancelation.
|
---|
| 484 | * @param pvUser The callback argument.
|
---|
| 485 | */
|
---|
| 486 | bool Progress::i_setCancelCallback(void (*pfnCallback)(void *), void *pvUser)
|
---|
| 487 | {
|
---|
| 488 | AutoCaller autoCaller(this);
|
---|
[94906] | 489 | AssertReturn(autoCaller.isOk(), false);
|
---|
[54992] | 490 |
|
---|
| 491 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
| 492 |
|
---|
| 493 | i_checkForAutomaticTimeout();
|
---|
| 494 | if (mCanceled)
|
---|
| 495 | return false;
|
---|
| 496 |
|
---|
| 497 | m_pvCancelUserArg = pvUser;
|
---|
| 498 | m_pfnCancelCallback = pfnCallback;
|
---|
| 499 | return true;
|
---|
| 500 | }
|
---|
| 501 |
|
---|
[67230] | 502 | /**
|
---|
[67242] | 503 | * @callback_method_impl{FNRTPROGRESS,
|
---|
| 504 | * Works the progress of the current operation.}
|
---|
[67230] | 505 | */
|
---|
[67242] | 506 | /*static*/ DECLCALLBACK(int) Progress::i_iprtProgressCallback(unsigned uPercentage, void *pvUser)
|
---|
[67230] | 507 | {
|
---|
| 508 | Progress *pThis = (Progress *)pvUser;
|
---|
[54992] | 509 |
|
---|
[67230] | 510 | /*
|
---|
| 511 | * Same as setCurrentOperationProgress, except we don't fail on mCompleted.
|
---|
| 512 | */
|
---|
| 513 | AutoWriteLock alock(pThis COMMA_LOCKVAL_SRC_POS);
|
---|
| 514 | int vrc = VINF_SUCCESS;
|
---|
| 515 | if (!pThis->mCompleted)
|
---|
| 516 | {
|
---|
| 517 | pThis->i_checkForAutomaticTimeout();
|
---|
| 518 | if (!pThis->mCanceled)
|
---|
[67242] | 519 | {
|
---|
| 520 | if (uPercentage > pThis->m_ulOperationPercent)
|
---|
[69331] | 521 | pThis->setCurrentOperationProgress(uPercentage);
|
---|
[67242] | 522 | }
|
---|
[67230] | 523 | else
|
---|
| 524 | {
|
---|
| 525 | Assert(pThis->mCancelable);
|
---|
| 526 | vrc = VERR_CANCELLED;
|
---|
| 527 | }
|
---|
| 528 | }
|
---|
| 529 | /* else ignored */
|
---|
| 530 | return vrc;
|
---|
| 531 | }
|
---|
| 532 |
|
---|
[67242] | 533 | /**
|
---|
| 534 | * @callback_method_impl{FNVDPROGRESS,
|
---|
| 535 | * Progress::i_iprtProgressCallback with parameters switched around.}
|
---|
| 536 | */
|
---|
| 537 | /*static*/ DECLCALLBACK(int) Progress::i_vdProgressCallback(void *pvUser, unsigned uPercentage)
|
---|
| 538 | {
|
---|
| 539 | return i_iprtProgressCallback(uPercentage, pvUser);
|
---|
| 540 | }
|
---|
[67230] | 541 |
|
---|
[74804] | 542 |
|
---|
[1] | 543 | // IProgress properties
|
---|
| 544 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 545 |
|
---|
[54992] | 546 | HRESULT Progress::getId(com::Guid &aId)
|
---|
| 547 | {
|
---|
| 548 | /* mId is constant during life time, no need to lock */
|
---|
| 549 | aId = mId;
|
---|
| 550 |
|
---|
| 551 | return S_OK;
|
---|
| 552 | }
|
---|
| 553 |
|
---|
| 554 | HRESULT Progress::getDescription(com::Utf8Str &aDescription)
|
---|
| 555 | {
|
---|
| 556 | /* mDescription is constant during life time, no need to lock */
|
---|
| 557 | aDescription = mDescription;
|
---|
| 558 |
|
---|
| 559 | return S_OK;
|
---|
| 560 | }
|
---|
| 561 | HRESULT Progress::getInitiator(ComPtr<IUnknown> &aInitiator)
|
---|
| 562 | {
|
---|
| 563 | /* mInitiator/mParent are constant during life time, no need to lock */
|
---|
| 564 | #if !defined(VBOX_COM_INPROC)
|
---|
| 565 | if (mInitiator)
|
---|
| 566 | mInitiator.queryInterfaceTo(aInitiator.asOutParam());
|
---|
| 567 | else
|
---|
| 568 | {
|
---|
| 569 | ComObjPtr<VirtualBox> pVirtualBox(mParent);
|
---|
| 570 | pVirtualBox.queryInterfaceTo(aInitiator.asOutParam());
|
---|
| 571 | }
|
---|
| 572 | #else
|
---|
| 573 | mInitiator.queryInterfaceTo(aInitiator.asOutParam());
|
---|
| 574 | #endif
|
---|
| 575 |
|
---|
| 576 | return S_OK;
|
---|
| 577 | }
|
---|
| 578 |
|
---|
| 579 | HRESULT Progress::getCancelable(BOOL *aCancelable)
|
---|
| 580 | {
|
---|
| 581 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
| 582 |
|
---|
| 583 | *aCancelable = mCancelable;
|
---|
| 584 |
|
---|
| 585 | return S_OK;
|
---|
| 586 | }
|
---|
| 587 |
|
---|
| 588 | HRESULT Progress::getPercent(ULONG *aPercent)
|
---|
| 589 | {
|
---|
| 590 | /* i_checkForAutomaticTimeout requires a write lock. */
|
---|
| 591 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
| 592 |
|
---|
| 593 | if (mCompleted && SUCCEEDED(mResultCode))
|
---|
| 594 | *aPercent = 100;
|
---|
| 595 | else
|
---|
| 596 | {
|
---|
| 597 | ULONG ulPercent = (ULONG)i_calcTotalPercent();
|
---|
| 598 | // do not report 100% until we're really really done with everything
|
---|
| 599 | // as the Qt GUI dismisses progress dialogs in that case
|
---|
| 600 | if ( ulPercent == 100
|
---|
| 601 | && ( m_ulOperationPercent < 100
|
---|
| 602 | || (m_ulCurrentOperation < m_cOperations -1)
|
---|
| 603 | )
|
---|
| 604 | )
|
---|
| 605 | *aPercent = 99;
|
---|
| 606 | else
|
---|
| 607 | *aPercent = ulPercent;
|
---|
| 608 | }
|
---|
| 609 |
|
---|
| 610 | i_checkForAutomaticTimeout();
|
---|
| 611 |
|
---|
| 612 | return S_OK;
|
---|
| 613 | }
|
---|
| 614 |
|
---|
| 615 | HRESULT Progress::getTimeRemaining(LONG *aTimeRemaining)
|
---|
| 616 | {
|
---|
| 617 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
| 618 |
|
---|
| 619 | if (mCompleted)
|
---|
| 620 | *aTimeRemaining = 0;
|
---|
| 621 | else
|
---|
| 622 | {
|
---|
| 623 | double dPercentDone = i_calcTotalPercent();
|
---|
| 624 | if (dPercentDone < 1)
|
---|
| 625 | *aTimeRemaining = -1; // unreliable, or avoid division by 0 below
|
---|
| 626 | else
|
---|
| 627 | {
|
---|
| 628 | uint64_t ullTimeNow = RTTimeMilliTS();
|
---|
| 629 | uint64_t ullTimeElapsed = ullTimeNow - m_ullTimestamp;
|
---|
[62363] | 630 | uint64_t ullTimeTotal = (uint64_t)((double)ullTimeElapsed * 100 / dPercentDone);
|
---|
[77252] | 631 | uint64_t ullTimeRemaining = (ullTimeTotal < ullTimeElapsed) ? 0 : ullTimeTotal - ullTimeElapsed;
|
---|
[54992] | 632 |
|
---|
| 633 | // LogFunc(("dPercentDone = %RI32, ullTimeNow = %RI64, ullTimeElapsed = %RI64, ullTimeTotal = %RI64, ullTimeRemaining = %RI64\n",
|
---|
| 634 | // (uint32_t)dPercentDone, ullTimeNow, ullTimeElapsed, ullTimeTotal, ullTimeRemaining));
|
---|
| 635 |
|
---|
[77252] | 636 | *aTimeRemaining = (LONG)(RT_MIN(ullTimeRemaining, RT_MS_1HOUR_64*24*365) / 1000);
|
---|
[54992] | 637 | }
|
---|
| 638 | }
|
---|
| 639 |
|
---|
| 640 | return S_OK;
|
---|
| 641 | }
|
---|
| 642 |
|
---|
| 643 | HRESULT Progress::getCompleted(BOOL *aCompleted)
|
---|
| 644 | {
|
---|
| 645 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
| 646 |
|
---|
| 647 | *aCompleted = mCompleted;
|
---|
| 648 |
|
---|
| 649 | return S_OK;
|
---|
| 650 | }
|
---|
| 651 |
|
---|
| 652 | HRESULT Progress::getCanceled(BOOL *aCanceled)
|
---|
| 653 | {
|
---|
| 654 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
| 655 |
|
---|
| 656 | *aCanceled = mCanceled;
|
---|
| 657 |
|
---|
| 658 | return S_OK;
|
---|
| 659 | }
|
---|
| 660 |
|
---|
| 661 | HRESULT Progress::getResultCode(LONG *aResultCode)
|
---|
| 662 | {
|
---|
| 663 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
| 664 |
|
---|
| 665 | if (!mCompleted)
|
---|
[73003] | 666 | return setError(E_FAIL, tr("Result code is not available, operation is still in progress"));
|
---|
[54992] | 667 |
|
---|
[85214] | 668 | *aResultCode = (LONG)mResultCode;
|
---|
[54992] | 669 |
|
---|
| 670 | return S_OK;
|
---|
| 671 | }
|
---|
| 672 |
|
---|
| 673 | HRESULT Progress::getErrorInfo(ComPtr<IVirtualBoxErrorInfo> &aErrorInfo)
|
---|
| 674 | {
|
---|
| 675 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
| 676 |
|
---|
| 677 | if (!mCompleted)
|
---|
[73003] | 678 | return setError(E_FAIL, tr("Error info is not available, operation is still in progress"));
|
---|
[54992] | 679 |
|
---|
| 680 | mErrorInfo.queryInterfaceTo(aErrorInfo.asOutParam());
|
---|
| 681 |
|
---|
| 682 | return S_OK;
|
---|
| 683 | }
|
---|
| 684 |
|
---|
| 685 | HRESULT Progress::getOperationCount(ULONG *aOperationCount)
|
---|
| 686 | {
|
---|
| 687 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
| 688 |
|
---|
| 689 | *aOperationCount = m_cOperations;
|
---|
| 690 |
|
---|
| 691 | return S_OK;
|
---|
| 692 | }
|
---|
| 693 |
|
---|
| 694 | HRESULT Progress::getOperation(ULONG *aOperation)
|
---|
| 695 | {
|
---|
| 696 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
| 697 |
|
---|
| 698 | *aOperation = m_ulCurrentOperation;
|
---|
| 699 |
|
---|
| 700 | return S_OK;
|
---|
| 701 | }
|
---|
| 702 |
|
---|
| 703 | HRESULT Progress::getOperationDescription(com::Utf8Str &aOperationDescription)
|
---|
| 704 | {
|
---|
| 705 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
| 706 |
|
---|
| 707 | aOperationDescription = m_operationDescription;
|
---|
| 708 |
|
---|
| 709 | return S_OK;
|
---|
| 710 | }
|
---|
| 711 |
|
---|
| 712 | HRESULT Progress::getOperationPercent(ULONG *aOperationPercent)
|
---|
| 713 | {
|
---|
| 714 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
| 715 |
|
---|
| 716 | if (mCompleted && SUCCEEDED(mResultCode))
|
---|
| 717 | *aOperationPercent = 100;
|
---|
| 718 | else
|
---|
| 719 | *aOperationPercent = m_ulOperationPercent;
|
---|
| 720 |
|
---|
| 721 | return S_OK;
|
---|
| 722 | }
|
---|
| 723 |
|
---|
| 724 | HRESULT Progress::getOperationWeight(ULONG *aOperationWeight)
|
---|
| 725 | {
|
---|
| 726 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
| 727 |
|
---|
| 728 | *aOperationWeight = m_ulCurrentOperationWeight;
|
---|
| 729 |
|
---|
| 730 | return S_OK;
|
---|
| 731 | }
|
---|
| 732 |
|
---|
| 733 | HRESULT Progress::getTimeout(ULONG *aTimeout)
|
---|
| 734 | {
|
---|
| 735 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
| 736 |
|
---|
| 737 | *aTimeout = m_cMsTimeout;
|
---|
| 738 |
|
---|
| 739 | return S_OK;
|
---|
| 740 | }
|
---|
| 741 |
|
---|
| 742 | HRESULT Progress::setTimeout(ULONG aTimeout)
|
---|
| 743 | {
|
---|
| 744 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
| 745 |
|
---|
| 746 | if (!mCancelable)
|
---|
[73003] | 747 | return setError(VBOX_E_INVALID_OBJECT_STATE, tr("Operation cannot be canceled"));
|
---|
[54992] | 748 | m_cMsTimeout = aTimeout;
|
---|
| 749 |
|
---|
| 750 | return S_OK;
|
---|
| 751 | }
|
---|
| 752 |
|
---|
[74804] | 753 | HRESULT Progress::getEventSource(ComPtr<IEventSource> &aEventSource)
|
---|
[54992] | 754 | {
|
---|
[74804] | 755 | /* event source is const, no need to lock */
|
---|
| 756 | pEventSource.queryInterfaceTo(aEventSource.asOutParam());
|
---|
[54992] | 757 | return S_OK;
|
---|
| 758 | }
|
---|
| 759 |
|
---|
| 760 |
|
---|
[74804] | 761 | // IProgress methods
|
---|
| 762 | /////////////////////////////////////////////////////////////////////////////
|
---|
[54992] | 763 |
|
---|
| 764 | /**
|
---|
[18643] | 765 | * @note XPCOM: when this method is not called on the main XPCOM thread, it
|
---|
[13580] | 766 | * simply blocks the thread until mCompletedSem is signalled. If the
|
---|
| 767 | * thread has its own event queue (hmm, what for?) that it must run, then
|
---|
[18643] | 768 | * calling this method will definitely freeze event processing.
|
---|
[1] | 769 | */
|
---|
[50874] | 770 | HRESULT Progress::waitForCompletion(LONG aTimeout)
|
---|
[1] | 771 | {
|
---|
[13580] | 772 | LogFlowThisFuncEnter();
|
---|
[21878] | 773 | LogFlowThisFunc(("aTimeout=%d\n", aTimeout));
|
---|
[1] | 774 |
|
---|
[25310] | 775 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
[1] | 776 |
|
---|
[13580] | 777 | /* if we're already completed, take a shortcut */
|
---|
[85214] | 778 | if (!mCompleted && aTimeout != 0)
|
---|
[1] | 779 | {
|
---|
[85214] | 780 | RTMSINTERVAL cMsWait = aTimeout < 0 ? RT_INDEFINITE_WAIT : (RTMSINTERVAL)aTimeout;
|
---|
| 781 | uint64_t msLast = aTimeout < 0 ? 0 : RTTimeMilliTS();
|
---|
[1] | 782 |
|
---|
[85214] | 783 | for (;;)
|
---|
[1] | 784 | {
|
---|
[23223] | 785 | mWaitersCount++;
|
---|
[40257] | 786 | alock.release();
|
---|
[85214] | 787 | int vrc = RTSemEventMultiWait(mCompletedSem, cMsWait);
|
---|
[40257] | 788 | alock.acquire();
|
---|
[23223] | 789 | mWaitersCount--;
|
---|
[1] | 790 |
|
---|
[13580] | 791 | /* the last waiter resets the semaphore */
|
---|
[1] | 792 | if (mWaitersCount == 0)
|
---|
[23223] | 793 | RTSemEventMultiReset(mCompletedSem);
|
---|
[1] | 794 |
|
---|
[21878] | 795 | if (RT_FAILURE(vrc) && vrc != VERR_TIMEOUT)
|
---|
[85214] | 796 | return setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Failed to wait for the task completion (%Rrc)"), vrc);
|
---|
| 797 |
|
---|
| 798 | if (mCompleted)
|
---|
[1] | 799 | break;
|
---|
| 800 |
|
---|
[85214] | 801 | if (aTimeout >= 0)
|
---|
[1] | 802 | {
|
---|
[85214] | 803 | uint64_t msNow = RTTimeMilliTS();
|
---|
| 804 | uint64_t cMsElapsed = msNow - msLast;
|
---|
| 805 | if (cMsWait <= cMsElapsed)
|
---|
| 806 | break;
|
---|
| 807 | cMsWait -= (RTMSINTERVAL)cMsElapsed;
|
---|
| 808 | msLast = msNow;
|
---|
[1] | 809 | }
|
---|
| 810 | }
|
---|
| 811 | }
|
---|
| 812 |
|
---|
[13580] | 813 | LogFlowThisFuncLeave();
|
---|
[1] | 814 | return S_OK;
|
---|
| 815 | }
|
---|
| 816 |
|
---|
| 817 | /**
|
---|
[18643] | 818 | * @note XPCOM: when this method is not called on the main XPCOM thread, it
|
---|
[13580] | 819 | * simply blocks the thread until mCompletedSem is signalled. If the
|
---|
| 820 | * thread has its own event queue (hmm, what for?) that it must run, then
|
---|
[18643] | 821 | * calling this method will definitely freeze event processing.
|
---|
[1] | 822 | */
|
---|
[50874] | 823 | HRESULT Progress::waitForOperationCompletion(ULONG aOperation, LONG aTimeout)
|
---|
| 824 |
|
---|
[1] | 825 | {
|
---|
[13580] | 826 | LogFlowThisFuncEnter();
|
---|
[21878] | 827 | LogFlowThisFunc(("aOperation=%d, aTimeout=%d\n", aOperation, aTimeout));
|
---|
[1] | 828 |
|
---|
[25310] | 829 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
[1] | 830 |
|
---|
[18269] | 831 | CheckComArgExpr(aOperation, aOperation < m_cOperations);
|
---|
[1] | 832 |
|
---|
[13580] | 833 | /* if we're already completed or if the given operation is already done,
|
---|
| 834 | * then take a shortcut */
|
---|
[18269] | 835 | if ( !mCompleted
|
---|
[85214] | 836 | && aOperation >= m_ulCurrentOperation
|
---|
| 837 | && aTimeout != 0)
|
---|
[1] | 838 | {
|
---|
[85214] | 839 | RTMSINTERVAL cMsWait = aTimeout < 0 ? RT_INDEFINITE_WAIT : (RTMSINTERVAL)aTimeout;
|
---|
| 840 | uint64_t msLast = aTimeout < 0 ? 0 : RTTimeMilliTS();
|
---|
[1] | 841 |
|
---|
[85214] | 842 | for (;;)
|
---|
[1] | 843 | {
|
---|
| 844 | mWaitersCount ++;
|
---|
[40257] | 845 | alock.release();
|
---|
[85214] | 846 | int vrc = RTSemEventMultiWait(mCompletedSem, cMsWait);
|
---|
[40257] | 847 | alock.acquire();
|
---|
[23223] | 848 | mWaitersCount--;
|
---|
[1] | 849 |
|
---|
[13580] | 850 | /* the last waiter resets the semaphore */
|
---|
[1] | 851 | if (mWaitersCount == 0)
|
---|
[23223] | 852 | RTSemEventMultiReset(mCompletedSem);
|
---|
[1] | 853 |
|
---|
[21878] | 854 | if (RT_FAILURE(vrc) && vrc != VERR_TIMEOUT)
|
---|
[85214] | 855 | return setErrorBoth(E_FAIL, vrc, tr("Failed to wait for the operation completion (%Rrc)"), vrc);
|
---|
| 856 |
|
---|
| 857 | if (mCompleted || aOperation >= m_ulCurrentOperation)
|
---|
[1] | 858 | break;
|
---|
| 859 |
|
---|
[85214] | 860 | if (aTimeout >= 0)
|
---|
[1] | 861 | {
|
---|
[85214] | 862 | uint64_t msNow = RTTimeMilliTS();
|
---|
| 863 | uint64_t cMsElapsed = msNow - msLast;
|
---|
| 864 | if (cMsWait <= cMsElapsed)
|
---|
| 865 | break;
|
---|
| 866 | cMsWait -= (RTMSINTERVAL)cMsElapsed;
|
---|
| 867 | msLast = msNow;
|
---|
[1] | 868 | }
|
---|
| 869 | }
|
---|
| 870 | }
|
---|
| 871 |
|
---|
[13580] | 872 | LogFlowThisFuncLeave();
|
---|
[1] | 873 | return S_OK;
|
---|
| 874 | }
|
---|
| 875 |
|
---|
[50874] | 876 | HRESULT Progress::cancel()
|
---|
[1] | 877 | {
|
---|
[25310] | 878 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
[1] | 879 |
|
---|
| 880 | if (!mCancelable)
|
---|
[73003] | 881 | return setError(VBOX_E_INVALID_OBJECT_STATE, tr("Operation cannot be canceled"));
|
---|
[1] | 882 |
|
---|
[23810] | 883 | if (!mCanceled)
|
---|
| 884 | {
|
---|
[29881] | 885 | LogThisFunc(("Canceling\n"));
|
---|
[23810] | 886 | mCanceled = TRUE;
|
---|
| 887 | if (m_pfnCancelCallback)
|
---|
| 888 | m_pfnCancelCallback(m_pvCancelUserArg);
|
---|
| 889 |
|
---|
| 890 | }
|
---|
[29881] | 891 | else
|
---|
| 892 | LogThisFunc(("Already canceled\n"));
|
---|
| 893 |
|
---|
[18120] | 894 | return S_OK;
|
---|
[1] | 895 | }
|
---|
| 896 |
|
---|
[74804] | 897 |
|
---|
| 898 | // IInternalProgressControl methods
|
---|
| 899 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 900 |
|
---|
| 901 | /**
|
---|
| 902 | * Updates the percentage value of the current operation.
|
---|
| 903 | *
|
---|
| 904 | * @param aPercent New percentage value of the operation in progress
|
---|
| 905 | * (in range [0, 100]).
|
---|
| 906 | */
|
---|
| 907 | HRESULT Progress::setCurrentOperationProgress(ULONG aPercent)
|
---|
[69002] | 908 | {
|
---|
[74804] | 909 | AssertMsgReturn(aPercent <= 100, ("%u\n", aPercent), E_INVALIDARG);
|
---|
| 910 |
|
---|
| 911 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
| 912 |
|
---|
| 913 | i_checkForAutomaticTimeout();
|
---|
| 914 | if (mCancelable && mCanceled)
|
---|
| 915 | AssertReturn(!mCompleted, E_FAIL);
|
---|
| 916 | AssertReturn(!mCompleted && !mCanceled, E_FAIL);
|
---|
| 917 |
|
---|
| 918 | if (m_ulOperationPercent != aPercent)
|
---|
| 919 | {
|
---|
| 920 | m_ulOperationPercent = aPercent;
|
---|
| 921 | ULONG actualPercent = 0;
|
---|
| 922 | getPercent(&actualPercent);
|
---|
[85307] | 923 | ::FireProgressPercentageChangedEvent(pEventSource, mId.toString(), (LONG)actualPercent);
|
---|
[74804] | 924 | }
|
---|
| 925 |
|
---|
[69002] | 926 | return S_OK;
|
---|
| 927 | }
|
---|
[1] | 928 |
|
---|
[74804] | 929 | HRESULT Progress::waitForOtherProgressCompletion(const ComPtr<IProgress> &aProgressOther,
|
---|
| 930 | ULONG aTimeoutMS)
|
---|
| 931 | {
|
---|
| 932 | LogFlowThisFuncEnter();
|
---|
| 933 |
|
---|
| 934 | /* Note: no locking needed, because we just use public methods. */
|
---|
| 935 |
|
---|
| 936 | BOOL fCancelable = FALSE;
|
---|
| 937 | BOOL fCompleted = FALSE;
|
---|
| 938 | BOOL fCanceled = FALSE;
|
---|
| 939 | ULONG prevPercent = UINT32_MAX;
|
---|
| 940 | ULONG currentPercent = 0;
|
---|
| 941 | ULONG cOp = 0;
|
---|
| 942 | /* Is the async process cancelable? */
|
---|
[94906] | 943 | HRESULT hrc = aProgressOther->COMGETTER(Cancelable)(&fCancelable);
|
---|
| 944 | if (FAILED(hrc)) return hrc;
|
---|
[74804] | 945 |
|
---|
| 946 | uint64_t u64StopTime = UINT64_MAX;
|
---|
| 947 | if (aTimeoutMS > 0)
|
---|
| 948 | u64StopTime = RTTimeMilliTS() + aTimeoutMS;
|
---|
| 949 | /* Loop as long as the sync process isn't completed. */
|
---|
| 950 | while (SUCCEEDED(aProgressOther->COMGETTER(Completed(&fCompleted))))
|
---|
| 951 | {
|
---|
| 952 | /* We can forward any cancel request to the async process only when
|
---|
| 953 | * it is cancelable. */
|
---|
| 954 | if (fCancelable)
|
---|
| 955 | {
|
---|
[94906] | 956 | hrc = COMGETTER(Canceled)(&fCanceled);
|
---|
| 957 | if (FAILED(hrc)) return hrc;
|
---|
[74804] | 958 | if (fCanceled)
|
---|
| 959 | {
|
---|
[94906] | 960 | hrc = aProgressOther->Cancel();
|
---|
| 961 | if (FAILED(hrc)) return hrc;
|
---|
[74804] | 962 | }
|
---|
| 963 | }
|
---|
| 964 | /* Even if the user canceled the process, we have to wait until the
|
---|
| 965 | async task has finished his work (cleanup and such). Otherwise there
|
---|
| 966 | will be sync trouble (still wrong state, dead locks, ...) on the
|
---|
| 967 | used objects. So just do nothing, but wait for the complete
|
---|
| 968 | notification. */
|
---|
| 969 | if (!fCanceled)
|
---|
| 970 | {
|
---|
| 971 | /* Check if the current operation has changed. It is also possible that
|
---|
| 972 | * in the meantime more than one async operation was finished. So we
|
---|
| 973 | * have to loop as long as we reached the same operation count. */
|
---|
| 974 | ULONG curOp;
|
---|
| 975 | for (;;)
|
---|
| 976 | {
|
---|
[94906] | 977 | hrc = aProgressOther->COMGETTER(Operation(&curOp));
|
---|
| 978 | if (FAILED(hrc)) return hrc;
|
---|
[74804] | 979 | if (cOp != curOp)
|
---|
| 980 | {
|
---|
| 981 | Bstr bstr;
|
---|
| 982 | ULONG currentWeight;
|
---|
[94906] | 983 | hrc = aProgressOther->COMGETTER(OperationDescription(bstr.asOutParam()));
|
---|
| 984 | if (FAILED(hrc)) return hrc;
|
---|
| 985 | hrc = aProgressOther->COMGETTER(OperationWeight(¤tWeight));
|
---|
| 986 | if (FAILED(hrc)) return hrc;
|
---|
| 987 | hrc = SetNextOperation(bstr.raw(), currentWeight);
|
---|
| 988 | if (FAILED(hrc)) return hrc;
|
---|
[74804] | 989 | ++cOp;
|
---|
| 990 | }
|
---|
| 991 | else
|
---|
| 992 | break;
|
---|
| 993 | }
|
---|
| 994 |
|
---|
[94906] | 995 | hrc = aProgressOther->COMGETTER(OperationPercent(¤tPercent));
|
---|
| 996 | if (FAILED(hrc)) return hrc;
|
---|
[74804] | 997 | if (currentPercent != prevPercent)
|
---|
| 998 | {
|
---|
| 999 | prevPercent = currentPercent;
|
---|
[94906] | 1000 | hrc = SetCurrentOperationProgress(currentPercent);
|
---|
| 1001 | if (FAILED(hrc)) return hrc;
|
---|
[74804] | 1002 | }
|
---|
| 1003 | }
|
---|
| 1004 | if (fCompleted)
|
---|
| 1005 | break;
|
---|
| 1006 |
|
---|
| 1007 | if (aTimeoutMS != 0)
|
---|
| 1008 | {
|
---|
| 1009 | /* Make sure the loop is not too tight */
|
---|
| 1010 | uint64_t u64Now = RTTimeMilliTS();
|
---|
| 1011 | uint64_t u64RemainingMS = u64StopTime - u64Now;
|
---|
| 1012 | if (u64RemainingMS < 10)
|
---|
| 1013 | u64RemainingMS = 10;
|
---|
| 1014 | else if (u64RemainingMS > 200)
|
---|
| 1015 | u64RemainingMS = 200;
|
---|
[94906] | 1016 | hrc = aProgressOther->WaitForCompletion((LONG)u64RemainingMS);
|
---|
| 1017 | if (FAILED(hrc)) return hrc;
|
---|
[74804] | 1018 |
|
---|
| 1019 | if (RTTimeMilliTS() >= u64StopTime)
|
---|
| 1020 | return VBOX_E_TIMEOUT;
|
---|
| 1021 | }
|
---|
| 1022 | else
|
---|
| 1023 | {
|
---|
| 1024 | /* Make sure the loop is not too tight */
|
---|
[94906] | 1025 | hrc = aProgressOther->WaitForCompletion(200);
|
---|
| 1026 | if (FAILED(hrc)) return hrc;
|
---|
[74804] | 1027 | }
|
---|
| 1028 | }
|
---|
| 1029 |
|
---|
| 1030 | /* Transfer error information if applicable and report the error status
|
---|
| 1031 | * back to the caller to make this as easy as possible. */
|
---|
| 1032 | LONG iRc;
|
---|
[94906] | 1033 | hrc = aProgressOther->COMGETTER(ResultCode)(&iRc);
|
---|
| 1034 | if (FAILED(hrc)) return hrc;
|
---|
[85214] | 1035 | if (FAILED((HRESULT)iRc))
|
---|
[74804] | 1036 | {
|
---|
| 1037 | setError(ProgressErrorInfo(aProgressOther));
|
---|
[94906] | 1038 | hrc = (HRESULT)iRc;
|
---|
[74804] | 1039 | }
|
---|
| 1040 |
|
---|
| 1041 | LogFlowThisFuncLeave();
|
---|
[94906] | 1042 | return hrc;
|
---|
[74804] | 1043 | }
|
---|
| 1044 |
|
---|
| 1045 | /**
|
---|
| 1046 | * Signals that the current operation is successfully completed and advances to
|
---|
| 1047 | * the next operation. The operation percentage is reset to 0.
|
---|
| 1048 | *
|
---|
| 1049 | * @param aNextOperationDescription Description of the next operation.
|
---|
| 1050 | * @param aNextOperationsWeight Weight of the next operation.
|
---|
| 1051 | *
|
---|
| 1052 | * @note The current operation must not be the last one.
|
---|
| 1053 | */
|
---|
| 1054 | HRESULT Progress::setNextOperation(const com::Utf8Str &aNextOperationDescription, ULONG aNextOperationsWeight)
|
---|
| 1055 | {
|
---|
| 1056 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
| 1057 |
|
---|
| 1058 | if (mCanceled)
|
---|
| 1059 | return E_FAIL;
|
---|
| 1060 | AssertReturn(!mCompleted, E_FAIL);
|
---|
| 1061 | AssertReturn(m_ulCurrentOperation + 1 < m_cOperations, E_FAIL);
|
---|
| 1062 |
|
---|
| 1063 | ++m_ulCurrentOperation;
|
---|
| 1064 | m_ulOperationsCompletedWeight += m_ulCurrentOperationWeight;
|
---|
| 1065 |
|
---|
| 1066 | m_operationDescription = aNextOperationDescription;
|
---|
| 1067 | m_ulCurrentOperationWeight = aNextOperationsWeight;
|
---|
| 1068 | m_ulOperationPercent = 0;
|
---|
| 1069 |
|
---|
[86650] | 1070 | LogThisFunc(("%s: aNextOperationsWeight = %d; m_ulCurrentOperation is now %d, m_ulOperationsCompletedWeight is now %d\n",
|
---|
[74804] | 1071 | m_operationDescription.c_str(), aNextOperationsWeight, m_ulCurrentOperation, m_ulOperationsCompletedWeight));
|
---|
| 1072 |
|
---|
| 1073 | /* wake up all waiting threads */
|
---|
| 1074 | if (mWaitersCount > 0)
|
---|
| 1075 | RTSemEventMultiSignal(mCompletedSem);
|
---|
| 1076 |
|
---|
| 1077 | ULONG actualPercent = 0;
|
---|
| 1078 | getPercent(&actualPercent);
|
---|
[85307] | 1079 | ::FireProgressPercentageChangedEvent(pEventSource, mId.toString(), (LONG)actualPercent);
|
---|
[74804] | 1080 |
|
---|
| 1081 | return S_OK;
|
---|
| 1082 | }
|
---|
| 1083 |
|
---|
| 1084 | /**
|
---|
| 1085 | * Notify the progress object that we're almost at the point of no return.
|
---|
| 1086 | *
|
---|
| 1087 | * This atomically checks for and disables cancelation. Calls to
|
---|
| 1088 | * IProgress::Cancel() made after a successful call to this method will fail
|
---|
| 1089 | * and the user can be told. While this isn't entirely clean behavior, it
|
---|
| 1090 | * prevents issues with an irreversible actually operation succeeding while the
|
---|
| 1091 | * user believe it was rolled back.
|
---|
| 1092 | *
|
---|
| 1093 | * @returns COM error status.
|
---|
| 1094 | * @retval S_OK on success.
|
---|
| 1095 | * @retval E_FAIL if the progress object has already been canceled or is in an
|
---|
| 1096 | * invalid state
|
---|
| 1097 | */
|
---|
| 1098 | HRESULT Progress::notifyPointOfNoReturn(void)
|
---|
| 1099 | {
|
---|
| 1100 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
| 1101 |
|
---|
| 1102 | if (mCanceled)
|
---|
| 1103 | {
|
---|
| 1104 | LogThisFunc(("returns failure\n"));
|
---|
| 1105 | return E_FAIL;
|
---|
| 1106 | }
|
---|
| 1107 |
|
---|
| 1108 | mCancelable = FALSE;
|
---|
| 1109 | LogThisFunc(("returns success\n"));
|
---|
| 1110 | return S_OK;
|
---|
| 1111 | }
|
---|
| 1112 |
|
---|
| 1113 | /**
|
---|
| 1114 | * Marks the operation as complete and attaches full error info.
|
---|
| 1115 | *
|
---|
| 1116 | * This is where the actual work is done, the related methods all end up here.
|
---|
| 1117 | *
|
---|
| 1118 | * @param aResultCode Operation result (error) code, must not be S_OK.
|
---|
| 1119 | * @param aErrorInfo List of arguments for the format string.
|
---|
| 1120 | */
|
---|
| 1121 | HRESULT Progress::notifyComplete(LONG aResultCode, const ComPtr<IVirtualBoxErrorInfo> &aErrorInfo)
|
---|
| 1122 | {
|
---|
[85251] | 1123 | return i_notifyCompleteWorker((HRESULT)aResultCode, aErrorInfo);
|
---|
| 1124 | }
|
---|
| 1125 |
|
---|
| 1126 |
|
---|
| 1127 | // private internal helpers
|
---|
| 1128 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 1129 |
|
---|
| 1130 | /**
|
---|
| 1131 | * Marks the operation as complete and attaches full error info.
|
---|
| 1132 | *
|
---|
| 1133 | * This is where the actual work is done, the related methods all end up here.
|
---|
| 1134 | *
|
---|
| 1135 | * @param aResultCode Operation result (error) code, must not be S_OK.
|
---|
| 1136 | * @param aErrorInfo List of arguments for the format string.
|
---|
| 1137 | *
|
---|
| 1138 | * @note This is just notifyComplete with the correct aResultCode type.
|
---|
| 1139 | */
|
---|
| 1140 | HRESULT Progress::i_notifyCompleteWorker(HRESULT aResultCode, const ComPtr<IVirtualBoxErrorInfo> &aErrorInfo)
|
---|
| 1141 | {
|
---|
| 1142 | LogThisFunc(("aResultCode=%Rhrc\n", aResultCode));
|
---|
[74804] | 1143 | /* on failure we expect error info, on success there must be none */
|
---|
[85251] | 1144 | AssertMsg(FAILED(aResultCode) ^ aErrorInfo.isNull(),
|
---|
| 1145 | ("No error info but trying to set a failed result (%08X/%Rhrc)!\n", aResultCode, aResultCode));
|
---|
[74804] | 1146 |
|
---|
| 1147 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
| 1148 |
|
---|
| 1149 | AssertReturn(mCompleted == FALSE, E_FAIL);
|
---|
| 1150 |
|
---|
[85251] | 1151 | if (mCanceled && SUCCEEDED(aResultCode))
|
---|
| 1152 | aResultCode = E_FAIL;
|
---|
[74804] | 1153 |
|
---|
| 1154 | mCompleted = TRUE;
|
---|
[85251] | 1155 | mResultCode = aResultCode;
|
---|
| 1156 | if (SUCCEEDED(aResultCode))
|
---|
[74804] | 1157 | {
|
---|
| 1158 | m_ulCurrentOperation = m_cOperations - 1; /* last operation */
|
---|
| 1159 | m_ulOperationPercent = 100;
|
---|
| 1160 | }
|
---|
| 1161 | mErrorInfo = aErrorInfo;
|
---|
| 1162 |
|
---|
| 1163 | #if !defined(VBOX_COM_INPROC)
|
---|
| 1164 | /* remove from the global collection of pending progress operations */
|
---|
| 1165 | if (mParent)
|
---|
| 1166 | mParent->i_removeProgress(mId.ref());
|
---|
| 1167 | #endif
|
---|
| 1168 |
|
---|
| 1169 | /* wake up all waiting threads */
|
---|
| 1170 | if (mWaitersCount > 0)
|
---|
| 1171 | RTSemEventMultiSignal(mCompletedSem);
|
---|
| 1172 |
|
---|
[85307] | 1173 | ::FireProgressTaskCompletedEvent(pEventSource, mId.toString());
|
---|
[74804] | 1174 |
|
---|
| 1175 | return S_OK;
|
---|
| 1176 | }
|
---|
| 1177 |
|
---|
[1] | 1178 | /**
|
---|
[54992] | 1179 | * Internal helper to compute the total percent value based on the member values and
|
---|
| 1180 | * returns it as a "double". This is used both by GetPercent (which returns it as a
|
---|
| 1181 | * rounded ULONG) and GetTimeRemaining().
|
---|
[1] | 1182 | *
|
---|
[54992] | 1183 | * Requires locking by the caller!
|
---|
[23223] | 1184 | *
|
---|
[54992] | 1185 | * @return fractional percentage as a double value.
|
---|
[23223] | 1186 | */
|
---|
[54992] | 1187 | double Progress::i_calcTotalPercent()
|
---|
[23223] | 1188 | {
|
---|
[54992] | 1189 | // avoid division by zero
|
---|
| 1190 | if (m_ulTotalOperationsWeight == 0)
|
---|
| 1191 | return 0.0;
|
---|
[23223] | 1192 |
|
---|
[54992] | 1193 | double dPercent = ( (double)m_ulOperationsCompletedWeight // weight of operations that have been completed
|
---|
| 1194 | + ((double)m_ulOperationPercent *
|
---|
| 1195 | (double)m_ulCurrentOperationWeight / 100.0) // plus partial weight of the current operation
|
---|
| 1196 | ) * 100.0 / (double)m_ulTotalOperationsWeight;
|
---|
[23223] | 1197 |
|
---|
[54992] | 1198 | return dPercent;
|
---|
[1] | 1199 | }
|
---|
| 1200 |
|
---|
| 1201 | /**
|
---|
[54992] | 1202 | * Internal helper for automatically timing out the operation.
|
---|
[1] | 1203 | *
|
---|
[54992] | 1204 | * The caller must hold the object write lock.
|
---|
[1] | 1205 | */
|
---|
[54992] | 1206 | void Progress::i_checkForAutomaticTimeout(void)
|
---|
[1] | 1207 | {
|
---|
[54992] | 1208 | AssertReturnVoid(isWriteLockOnCurrentThread());
|
---|
[7992] | 1209 |
|
---|
[54992] | 1210 | if ( m_cMsTimeout
|
---|
| 1211 | && mCancelable
|
---|
| 1212 | && !mCanceled
|
---|
| 1213 | && RTTimeMilliTS() - m_ullTimestamp > m_cMsTimeout)
|
---|
| 1214 | Cancel();
|
---|
[1] | 1215 | }
|
---|