VirtualBox

source: vbox/trunk/src/VBox/Main/src-all/VirtualBoxBase.cpp@ 95433

Last change on this file since 95433 was 94912, checked in by vboxsync, 3 years ago

Main/src-all: Adjust to the new rules wrt. to rc -> hrc,vrc usage, ​bugref:10223

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 30.2 KB
Line 
1/* $Id: VirtualBoxBase.cpp 94912 2022-05-08 19:05:31Z vboxsync $ */
2/** @file
3 * VirtualBox COM base classes implementation
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#define LOG_GROUP LOG_GROUP_MAIN
19#include <iprt/semaphore.h>
20#include <iprt/asm.h>
21#include <iprt/cpp/exception.h>
22
23#include <typeinfo>
24
25#if !defined(VBOX_WITH_XPCOM)
26# include <iprt/win/windows.h>
27#else /* !defined(VBOX_WITH_XPCOM) */
28/// @todo remove when VirtualBoxErrorInfo goes away from here
29# include <nsIServiceManager.h>
30# include <nsIExceptionService.h>
31#endif /* !defined(VBOX_WITH_XPCOM) */
32
33#include "VirtualBoxBase.h"
34#include "AutoCaller.h"
35#include "VirtualBoxErrorInfoImpl.h"
36#include "VirtualBoxTranslator.h"
37#include "Global.h"
38#include "LoggingNew.h"
39
40#include "VBox/com/ErrorInfo.h"
41#include "VBox/com/MultiResult.h"
42
43////////////////////////////////////////////////////////////////////////////////
44//
45// VirtualBoxBase
46//
47////////////////////////////////////////////////////////////////////////////////
48
49CLASSFACTORY_STAT g_aClassFactoryStats[CLASSFACTORYSTATS_MAX] =
50{
51 { "--- totals ---", 0 },
52 { NULL, 0 }
53};
54
55RWLockHandle *g_pClassFactoryStatsLock = NULL;
56
57
58VirtualBoxBase::VirtualBoxBase() :
59 mState(this),
60 iFactoryStat(~0U)
61{
62 mObjectLock = NULL;
63
64 if (!g_pClassFactoryStatsLock)
65 {
66 RWLockHandle *lock = new RWLockHandle(LOCKCLASS_OBJECTSTATE);
67 if (!ASMAtomicCmpXchgPtr(&g_pClassFactoryStatsLock, lock, NULL))
68 delete lock;
69 }
70 Assert(g_pClassFactoryStatsLock);
71}
72
73VirtualBoxBase::~VirtualBoxBase()
74{
75 Assert(iFactoryStat == ~0U);
76 if (mObjectLock)
77 delete mObjectLock;
78}
79
80HRESULT VirtualBoxBase::BaseFinalConstruct()
81{
82 Assert(iFactoryStat == ~0U);
83 if (g_pClassFactoryStatsLock)
84 {
85 AutoWriteLock alock(g_pClassFactoryStatsLock COMMA_LOCKVAL_SRC_POS);
86 g_aClassFactoryStats[0].current++;
87 g_aClassFactoryStats[0].overall++;
88 const char *pszName = getComponentName();
89 uint32_t i = 1;
90 while (i < CLASSFACTORYSTATS_MAX && g_aClassFactoryStats[i].psz)
91 {
92 if (g_aClassFactoryStats[i].psz == pszName)
93 break;
94 i++;
95 }
96 if (i < CLASSFACTORYSTATS_MAX)
97 {
98 if (!g_aClassFactoryStats[i].psz)
99 {
100 g_aClassFactoryStats[i].psz = pszName;
101 g_aClassFactoryStats[i].current = 0;
102 g_aClassFactoryStats[i].overall = 0;
103 }
104 iFactoryStat = i;
105 g_aClassFactoryStats[i].current++;
106 g_aClassFactoryStats[i].overall++;
107 }
108 else
109 AssertMsg(i < CLASSFACTORYSTATS_MAX, ("%u exhausts size of factory housekeeping array\n", i));
110 }
111 else
112 Assert(g_pClassFactoryStatsLock);
113
114#ifdef RT_OS_WINDOWS
115 return CoCreateFreeThreadedMarshaler(this, //GetControllingUnknown(),
116 m_pUnkMarshaler.asOutParam());
117#else
118 return S_OK;
119#endif
120}
121
122void VirtualBoxBase::BaseFinalRelease()
123{
124 if (g_pClassFactoryStatsLock)
125 {
126 AutoWriteLock alock(g_pClassFactoryStatsLock COMMA_LOCKVAL_SRC_POS);
127 g_aClassFactoryStats[0].current--;
128 const char *pszName = getComponentName();
129 if (iFactoryStat < CLASSFACTORYSTATS_MAX)
130 {
131 if (g_aClassFactoryStats[iFactoryStat].psz == pszName)
132 {
133 g_aClassFactoryStats[iFactoryStat].current--;
134 iFactoryStat = ~0U;
135 }
136 else
137 AssertMsgFailed(("could not find factory housekeeping array entry for %s (index %u contains %s)\n", pszName, iFactoryStat, g_aClassFactoryStats[iFactoryStat].psz));
138 }
139 else
140 AssertMsgFailed(("factory housekeeping array corruption, index %u is too large\n", iFactoryStat));
141 }
142 else
143 Assert(g_pClassFactoryStatsLock);
144
145#ifdef RT_OS_WINDOWS
146 m_pUnkMarshaler.setNull();
147#endif
148}
149
150void APIDumpComponentFactoryStats()
151{
152 if (g_pClassFactoryStatsLock)
153 {
154 AutoReadLock alock(g_pClassFactoryStatsLock COMMA_LOCKVAL_SRC_POS);
155 for (uint32_t i = 0; i < CLASSFACTORYSTATS_MAX && g_aClassFactoryStats[i].psz; i++)
156 LogRel(("CFS: component %-30s current %-10u total %-10u\n",
157 g_aClassFactoryStats[i].psz, g_aClassFactoryStats[i].current,
158 g_aClassFactoryStats[i].overall));
159 }
160 else
161 Assert(g_pClassFactoryStatsLock);
162}
163
164/**
165 * This virtual method returns an RWLockHandle that can be used to
166 * protect instance data. This RWLockHandle is generally referred to
167 * as the "object lock"; its locking class (for lock order validation)
168 * must be returned by another virtual method, getLockingClass(), which
169 * by default returns LOCKCLASS_OTHEROBJECT but is overridden by several
170 * subclasses such as VirtualBox, Host, Machine and others.
171 *
172 * On the first call this method lazily creates the RWLockHandle.
173 *
174 * @return
175 */
176/* virtual */
177RWLockHandle *VirtualBoxBase::lockHandle() const
178{
179 /* lazy initialization */
180 if (RT_LIKELY(mObjectLock))
181 return mObjectLock;
182
183 AssertCompile(sizeof(RWLockHandle *) == sizeof(void *));
184
185 // getLockingClass() is overridden by many subclasses to return
186 // one of the locking classes listed at the top of AutoLock.h
187 RWLockHandle *objLock = new RWLockHandle(getLockingClass());
188 if (!ASMAtomicCmpXchgPtr(&mObjectLock, objLock, NULL))
189 {
190 delete objLock;
191 objLock = ASMAtomicReadPtrT(&mObjectLock, RWLockHandle *);
192 }
193 return objLock;
194}
195
196/**
197 * Handles unexpected exceptions by turning them into COM errors in release
198 * builds or by hitting a breakpoint in the release builds.
199 *
200 * Usage pattern:
201 * @code
202 try
203 {
204 // ...
205 }
206 catch (LaLalA)
207 {
208 // ...
209 }
210 catch (...)
211 {
212 hrc = VirtualBox::handleUnexpectedExceptions(this, RT_SRC_POS);
213 }
214 * @endcode
215 *
216 * @param aThis object where the exception happened
217 * @param SRC_POS "RT_SRC_POS" macro instantiation.
218 * */
219/* static */
220HRESULT VirtualBoxBase::handleUnexpectedExceptions(VirtualBoxBase *const aThis, RT_SRC_POS_DECL)
221{
222 try
223 {
224 /* re-throw the current exception */
225 throw;
226 }
227 catch (const RTCError &err) // includes all XML exceptions
228 {
229 return setErrorInternalF(E_FAIL, aThis->getClassIID(), aThis->getComponentName(),
230 false /* aWarning */,
231 true /* aLogIt */,
232 0 /* aResultDetail */,
233 "%s.\n%s[%d] (%s)",
234 err.what(), pszFile, iLine, pszFunction);
235 }
236 catch (const std::exception &err)
237 {
238 return setErrorInternalF(E_FAIL, aThis->getClassIID(), aThis->getComponentName(),
239 false /* aWarning */,
240 true /* aLogIt */,
241 0 /* aResultDetail */,
242 tr("Unexpected exception: %s [%s]\n%s[%d] (%s)"),
243 err.what(), typeid(err).name(), pszFile, iLine, pszFunction);
244 }
245 catch (...)
246 {
247 return setErrorInternalF(E_FAIL, aThis->getClassIID(), aThis->getComponentName(),
248 false /* aWarning */,
249 true /* aLogIt */,
250 0 /* aResultDetail */,
251 tr("Unknown exception\n%s[%d] (%s)"),
252 pszFile, iLine, pszFunction);
253 }
254
255#ifndef _MSC_VER /* (unreachable) */
256 /* should not get here */
257 AssertFailed();
258 return E_FAIL;
259#endif
260}
261
262
263/**
264 * Sets error info for the current thread. This is an internal function that
265 * gets eventually called by all public variants. If @a aWarning is
266 * @c true, then the highest (31) bit in the @a aResultCode value which
267 * indicates the error severity is reset to zero to make sure the receiver will
268 * recognize that the created error info object represents a warning rather
269 * than an error.
270 */
271/* static */
272HRESULT VirtualBoxBase::setErrorInternalF(HRESULT aResultCode,
273 const GUID &aIID,
274 const char *aComponent,
275 bool aWarning,
276 bool aLogIt,
277 LONG aResultDetail,
278 const char *aText, ...)
279{
280 va_list va;
281 va_start(va, aText);
282 HRESULT hres = setErrorInternalV(aResultCode, aIID, aComponent, aText, va,
283 aWarning, aLogIt, aResultDetail);
284 va_end(va);
285 return hres;
286}
287
288/**
289 * Sets error info for the current thread. This is an internal function that
290 * gets eventually called by all public variants. If @a aWarning is
291 * @c true, then the highest (31) bit in the @a aResultCode value which
292 * indicates the error severity is reset to zero to make sure the receiver will
293 * recognize that the created error info object represents a warning rather
294 * than an error.
295 */
296/* static */
297HRESULT VirtualBoxBase::setErrorInternalV(HRESULT aResultCode,
298 const GUID &aIID,
299 const char *aComponent,
300 const char *aText,
301 va_list aArgs,
302 bool aWarning,
303 bool aLogIt,
304 LONG aResultDetail /* = 0*/)
305{
306 /* whether multi-error mode is turned on */
307 bool preserve = MultiResult::isMultiEnabled();
308
309 com::Utf8Str strText;
310 if (aLogIt)
311 {
312#ifdef VBOX_WITH_MAIN_NLS
313 strText = VirtualBoxTranslator::trSource(aText);
314#else
315 strText = aText;
316#endif
317 va_list va2;
318 va_copy(va2, aArgs);
319 LogRel(("%s [COM]: aRC=%Rhrc (%#08x) aIID={%RTuuid} aComponent={%s} aText={%N}, preserve=%RTbool aResultDetail=%d\n",
320 aWarning ? "WARNING" : "ERROR",
321 aResultCode,
322 aResultCode,
323 &aIID,
324 aComponent,
325 strText.c_str(),
326 &va2,
327 preserve,
328 aResultDetail));
329 va_end(va2);
330 }
331
332 /* these are mandatory, others -- not */
333 AssertReturn((!aWarning && FAILED(aResultCode)) ||
334 (aWarning && aResultCode != S_OK),
335 E_FAIL);
336
337 /* reset the error severity bit if it's a warning */
338 if (aWarning)
339 aResultCode &= ~0x80000000;
340
341 HRESULT hrc = S_OK;
342
343 if (aText == NULL || aText[0] == '\0')
344 {
345 /* Some default info */
346 switch (aResultCode)
347 {
348 case E_INVALIDARG: strText = tr("A parameter has an invalid value"); break;
349 case E_POINTER: strText = tr("A parameter is an invalid pointer"); break;
350 case E_UNEXPECTED: strText = tr("The result of the operation is unexpected"); break;
351 case E_ACCESSDENIED: strText = tr("The access to an object is not allowed"); break;
352 case E_OUTOFMEMORY: strText = tr("The allocation of new memory failed"); break;
353 case E_NOTIMPL: strText = tr("The requested operation is not implemented"); break;
354 case E_NOINTERFACE: strText = tr("The requested interface is not implemented"); break;
355 case E_FAIL: strText = tr("A general error occurred"); break;
356 case E_ABORT: strText = tr("The operation was canceled"); break;
357 case VBOX_E_OBJECT_NOT_FOUND: strText = tr("Object corresponding to the supplied arguments does not exist"); break;
358 case VBOX_E_INVALID_VM_STATE: strText = tr("Current virtual machine state prevents the operation"); break;
359 case VBOX_E_VM_ERROR: strText = tr("Virtual machine error occurred attempting the operation"); break;
360 case VBOX_E_FILE_ERROR: strText = tr("File not accessible or erroneous file contents"); break;
361 case VBOX_E_IPRT_ERROR: strText = tr("Runtime subsystem error"); break;
362 case VBOX_E_PDM_ERROR: strText = tr("Pluggable Device Manager error"); break;
363 case VBOX_E_INVALID_OBJECT_STATE: strText = tr("Current object state prohibits operation"); break;
364 case VBOX_E_HOST_ERROR: strText = tr("Host operating system related error"); break;
365 case VBOX_E_NOT_SUPPORTED: strText = tr("Requested operation is not supported"); break;
366 case VBOX_E_XML_ERROR: strText = tr("Invalid XML found"); break;
367 case VBOX_E_INVALID_SESSION_STATE: strText = tr("Current session state prohibits operation"); break;
368 case VBOX_E_OBJECT_IN_USE: strText = tr("Object being in use prohibits operation"); break;
369 case VBOX_E_PASSWORD_INCORRECT: strText = tr("Incorrect password provided"); break;
370 default: strText = tr("Unknown error"); break;
371 }
372 }
373 else
374 {
375 va_list va2;
376 va_copy(va2, aArgs);
377 strText = com::Utf8StrFmt("%N", aText, &va2);
378 va_end(va2);
379 }
380
381 do
382 {
383 ComObjPtr<VirtualBoxErrorInfo> info;
384 hrc = info.createObject();
385 if (FAILED(hrc)) break;
386
387#if !defined(VBOX_WITH_XPCOM)
388
389 ComPtr<IVirtualBoxErrorInfo> curInfo;
390 if (preserve)
391 {
392 /* get the current error info if any */
393 ComPtr<IErrorInfo> err;
394 hrc = ::GetErrorInfo(0, err.asOutParam());
395 if (FAILED(hrc)) break;
396 hrc = err.queryInterfaceTo(curInfo.asOutParam());
397 if (FAILED(hrc))
398 {
399 /* create a IVirtualBoxErrorInfo wrapper for the native
400 * IErrorInfo object */
401 ComObjPtr<VirtualBoxErrorInfo> wrapper;
402 hrc = wrapper.createObject();
403 if (SUCCEEDED(hrc))
404 {
405 hrc = wrapper->init(err);
406 if (SUCCEEDED(hrc))
407 curInfo = wrapper;
408 }
409 }
410 }
411 /* On failure, curInfo will stay null */
412 Assert(SUCCEEDED(hrc) || curInfo.isNull());
413
414 /* set the current error info and preserve the previous one if any */
415 hrc = info->initEx(aResultCode, aResultDetail, aIID, aComponent, strText, curInfo);
416 if (FAILED(hrc)) break;
417
418 ComPtr<IErrorInfo> err;
419 hrc = info.queryInterfaceTo(err.asOutParam());
420 if (SUCCEEDED(hrc))
421 hrc = ::SetErrorInfo(0, err);
422
423#else // !defined(VBOX_WITH_XPCOM)
424
425 nsCOMPtr <nsIExceptionService> es;
426 es = do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID, &hrc);
427 if (NS_SUCCEEDED(hrc))
428 {
429 nsCOMPtr <nsIExceptionManager> em;
430 hrc = es->GetCurrentExceptionManager(getter_AddRefs(em));
431 if (FAILED(hrc)) break;
432
433 ComPtr<IVirtualBoxErrorInfo> curInfo;
434 if (preserve)
435 {
436 /* get the current error info if any */
437 ComPtr<nsIException> ex;
438 hrc = em->GetCurrentException(ex.asOutParam());
439 if (FAILED(hrc)) break;
440 hrc = ex.queryInterfaceTo(curInfo.asOutParam());
441 if (FAILED(hrc))
442 {
443 /* create a IVirtualBoxErrorInfo wrapper for the native
444 * nsIException object */
445 ComObjPtr<VirtualBoxErrorInfo> wrapper;
446 hrc = wrapper.createObject();
447 if (SUCCEEDED(hrc))
448 {
449 hrc = wrapper->init(ex);
450 if (SUCCEEDED(hrc))
451 curInfo = wrapper;
452 }
453 }
454 }
455 /* On failure, curInfo will stay null */
456 Assert(SUCCEEDED(hrc) || curInfo.isNull());
457
458 /* set the current error info and preserve the previous one if any */
459 hrc = info->initEx(aResultCode, aResultDetail, aIID, aComponent, Bstr(strText), curInfo);
460 if (FAILED(hrc)) break;
461
462 ComPtr<nsIException> ex;
463 hrc = info.queryInterfaceTo(ex.asOutParam());
464 if (SUCCEEDED(hrc))
465 hrc = em->SetCurrentException(ex);
466 }
467 else if (hrc == NS_ERROR_UNEXPECTED)
468 {
469 /*
470 * It is possible that setError() is being called by the object
471 * after the XPCOM shutdown sequence has been initiated
472 * (for example, when XPCOM releases all instances it internally
473 * references, which can cause object's FinalConstruct() and then
474 * uninit()). In this case, do_GetService() above will return
475 * NS_ERROR_UNEXPECTED and it doesn't actually make sense to
476 * set the exception (nobody will be able to read it).
477 */
478 Log1WarningFunc(("Will not set an exception because nsIExceptionService is not available (NS_ERROR_UNEXPECTED). XPCOM is being shutdown?\n"));
479 hrc = NS_OK;
480 }
481
482#endif // !defined(VBOX_WITH_XPCOM)
483 }
484 while (0);
485
486 AssertComRC(hrc);
487
488 return SUCCEEDED(hrc) ? aResultCode : hrc;
489}
490
491/**
492 * Shortcut instance method to calling the static setErrorInternal with the
493 * class interface ID and component name inserted correctly. This uses the
494 * virtual getClassIID() and getComponentName() methods which are automatically
495 * defined by the VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT macro.
496 * @param aResultCode
497 * @return
498 */
499HRESULT VirtualBoxBase::setError(HRESULT aResultCode)
500{
501 return setErrorInternalF(aResultCode,
502 this->getClassIID(),
503 this->getComponentName(),
504 false /* aWarning */,
505 true /* aLogIt */,
506 0 /* aResultDetail */,
507 NULL);
508}
509
510/**
511 * Shortcut instance method to calling the static setErrorInternal with the
512 * class interface ID and component name inserted correctly. This uses the
513 * virtual getClassIID() and getComponentName() methods which are automatically
514 * defined by the VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT macro.
515 * @param aResultCode
516 * @param pcsz
517 * @return
518 */
519HRESULT VirtualBoxBase::setError(HRESULT aResultCode, const char *pcsz, ...)
520{
521 va_list args;
522 va_start(args, pcsz);
523 HRESULT hrc = setErrorInternalV(aResultCode,
524 this->getClassIID(),
525 this->getComponentName(),
526 pcsz, args,
527 false /* aWarning */,
528 true /* aLogIt */);
529 va_end(args);
530 return hrc;
531}
532
533/**
534 * Shortcut instance method to calling the static setErrorInternal with the
535 * class interface ID and component name inserted correctly. This uses the
536 * virtual getClassIID() and getComponentName() methods which are automatically
537 * defined by the VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT macro.
538 * @param ei
539 * @return
540 */
541HRESULT VirtualBoxBase::setError(const com::ErrorInfo &ei)
542{
543 /* whether multi-error mode is turned on */
544 bool preserve = MultiResult::isMultiEnabled();
545
546 HRESULT hrc = S_OK;
547
548 do
549 {
550 ComObjPtr<VirtualBoxErrorInfo> info;
551 hrc = info.createObject();
552 if (FAILED(hrc)) break;
553
554#if !defined(VBOX_WITH_XPCOM)
555
556 ComPtr<IVirtualBoxErrorInfo> curInfo;
557 if (preserve)
558 {
559 /* get the current error info if any */
560 ComPtr<IErrorInfo> err;
561 hrc = ::GetErrorInfo(0, err.asOutParam());
562 if (FAILED(hrc)) break;
563 hrc = err.queryInterfaceTo(curInfo.asOutParam());
564 if (FAILED(hrc))
565 {
566 /* create a IVirtualBoxErrorInfo wrapper for the native
567 * IErrorInfo object */
568 ComObjPtr<VirtualBoxErrorInfo> wrapper;
569 hrc = wrapper.createObject();
570 if (SUCCEEDED(hrc))
571 {
572 hrc = wrapper->init(err);
573 if (SUCCEEDED(hrc))
574 curInfo = wrapper;
575 }
576 }
577 }
578 /* On failure, curInfo will stay null */
579 Assert(SUCCEEDED(hrc) || curInfo.isNull());
580
581 /* set the current error info and preserve the previous one if any */
582 hrc = info->init(ei, curInfo);
583 if (FAILED(hrc)) break;
584
585 ComPtr<IErrorInfo> err;
586 hrc = info.queryInterfaceTo(err.asOutParam());
587 if (SUCCEEDED(hrc))
588 hrc = ::SetErrorInfo(0, err);
589
590#else // !defined(VBOX_WITH_XPCOM)
591
592 nsCOMPtr <nsIExceptionService> es;
593 es = do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID, &hrc);
594 if (NS_SUCCEEDED(hrc))
595 {
596 nsCOMPtr <nsIExceptionManager> em;
597 hrc = es->GetCurrentExceptionManager(getter_AddRefs(em));
598 if (FAILED(hrc)) break;
599
600 ComPtr<IVirtualBoxErrorInfo> curInfo;
601 if (preserve)
602 {
603 /* get the current error info if any */
604 ComPtr<nsIException> ex;
605 hrc = em->GetCurrentException(ex.asOutParam());
606 if (FAILED(hrc)) break;
607 hrc = ex.queryInterfaceTo(curInfo.asOutParam());
608 if (FAILED(hrc))
609 {
610 /* create a IVirtualBoxErrorInfo wrapper for the native
611 * nsIException object */
612 ComObjPtr<VirtualBoxErrorInfo> wrapper;
613 hrc = wrapper.createObject();
614 if (SUCCEEDED(hrc))
615 {
616 hrc = wrapper->init(ex);
617 if (SUCCEEDED(hrc))
618 curInfo = wrapper;
619 }
620 }
621 }
622 /* On failure, curInfo will stay null */
623 Assert(SUCCEEDED(hrc) || curInfo.isNull());
624
625 /* set the current error info and preserve the previous one if any */
626 hrc = info->init(ei, curInfo);
627 if (FAILED(hrc)) break;
628
629 ComPtr<nsIException> ex;
630 hrc = info.queryInterfaceTo(ex.asOutParam());
631 if (SUCCEEDED(hrc))
632 hrc = em->SetCurrentException(ex);
633 }
634 else if (hrc == NS_ERROR_UNEXPECTED)
635 {
636 /*
637 * It is possible that setError() is being called by the object
638 * after the XPCOM shutdown sequence has been initiated
639 * (for example, when XPCOM releases all instances it internally
640 * references, which can cause object's FinalConstruct() and then
641 * uninit()). In this case, do_GetService() above will return
642 * NS_ERROR_UNEXPECTED and it doesn't actually make sense to
643 * set the exception (nobody will be able to read it).
644 */
645 Log1WarningFunc(("Will not set an exception because nsIExceptionService is not available (NS_ERROR_UNEXPECTED). XPCOM is being shutdown?\n"));
646 hrc = NS_OK;
647 }
648
649#endif // !defined(VBOX_WITH_XPCOM)
650 }
651 while (0);
652
653 AssertComRC(hrc);
654
655 return SUCCEEDED(hrc) ? ei.getResultCode() : hrc;
656}
657
658/**
659 * Converts the VBox status code a COM one and sets the error info.
660 *
661 * The VBox status code is made available to the API user via
662 * IVirtualBoxErrorInfo::resultDetail attribute.
663 *
664 * @param vrc The VBox status code.
665 * @return COM status code appropriate for @a vrc.
666 *
667 * @sa VirtualBoxBase::setError(HRESULT)
668 */
669HRESULT VirtualBoxBase::setErrorVrc(int vrc)
670{
671 return setErrorInternalF(Global::vboxStatusCodeToCOM(vrc),
672 this->getClassIID(),
673 this->getComponentName(),
674 false /* aWarning */,
675 true /* aLogIt */,
676 vrc /* aResultDetail */,
677 Utf8StrFmt("%Rrc", vrc).c_str());
678}
679
680/**
681 * Converts the VBox status code a COM one and sets the error info.
682 *
683 * @param vrc The VBox status code.
684 * @param pcszMsgFmt Error message format string.
685 * @param va_args Error message format string.
686 * @return COM status code appropriate for @a vrc.
687 *
688 * @sa VirtualBoxBase::setError(HRESULT, const char *, ...)
689 */
690HRESULT VirtualBoxBase::setErrorVrcV(int vrc, const char *pcszMsgFmt, va_list va_args)
691{
692 return setErrorInternalV(Global::vboxStatusCodeToCOM(vrc),
693 this->getClassIID(),
694 this->getComponentName(),
695 pcszMsgFmt, va_args,
696 false /* aWarning */,
697 true /* aLogIt */,
698 vrc /* aResultDetail */);
699}
700
701/**
702 * Converts the VBox status code a COM one and sets the error info.
703 *
704 * @param vrc The VBox status code.
705 * @param pcszMsgFmt Error message format string.
706 * @param ... Argument specified in the @a pcszMsgFmt
707 * @return COM status code appropriate for @a vrc.
708 *
709 * @sa VirtualBoxBase::setError(HRESULT, const char *, ...)
710 */
711HRESULT VirtualBoxBase::setErrorVrc(int vrc, const char *pcszMsgFmt, ...)
712{
713 va_list va;
714 va_start(va, pcszMsgFmt);
715 HRESULT hrc = setErrorVrcV(vrc, pcszMsgFmt, va);
716 va_end(va);
717 return hrc;
718}
719
720/**
721 * Sets error info with both a COM status and an VBox status code.
722 *
723 * The VBox status code is made available to the API user via
724 * IVirtualBoxErrorInfo::resultDetail attribute.
725 *
726 * @param hrc The COM status code to return.
727 * @param vrc The VBox status code.
728 * @return Most likely @a hrc, see setErrorInternal.
729 *
730 * @sa VirtualBoxBase::setError(HRESULT)
731 */
732HRESULT VirtualBoxBase::setErrorBoth(HRESULT hrc, int vrc)
733{
734 return setErrorInternalF(hrc,
735 this->getClassIID(),
736 this->getComponentName(),
737 false /* aWarning */,
738 true /* aLogIt */,
739 vrc /* aResultDetail */,
740 Utf8StrFmt("%Rrc", vrc).c_str());
741}
742
743/**
744 * Sets error info with a message and both a COM status and an VBox status code.
745 *
746 * The VBox status code is made available to the API user via
747 * IVirtualBoxErrorInfo::resultDetail attribute.
748 *
749 * @param hrc The COM status code to return.
750 * @param vrc The VBox status code.
751 * @param pcszMsgFmt Error message format string.
752 * @param ... Argument specified in the @a pcszMsgFmt
753 * @return Most likely @a hrc, see setErrorInternal.
754 *
755 * @sa VirtualBoxBase::setError(HRESULT, const char *, ...)
756 */
757HRESULT VirtualBoxBase::setErrorBoth(HRESULT hrc, int vrc, const char *pcszMsgFmt, ...)
758{
759 va_list va;
760 va_start(va, pcszMsgFmt);
761 hrc = setErrorInternalV(hrc,
762 this->getClassIID(),
763 this->getComponentName(),
764 pcszMsgFmt, va,
765 false /* aWarning */,
766 true /* aLogIt */,
767 vrc /* aResultDetail */);
768 va_end(va);
769 return hrc;
770}
771
772/**
773 * Like setError(), but sets the "warning" bit in the call to setErrorInternal().
774 * @param aResultCode
775 * @param pcsz
776 * @return
777 */
778HRESULT VirtualBoxBase::setWarning(HRESULT aResultCode, const char *pcsz, ...)
779{
780 va_list args;
781 va_start(args, pcsz);
782 HRESULT hrc = setErrorInternalV(aResultCode,
783 this->getClassIID(),
784 this->getComponentName(),
785 pcsz, args,
786 true /* aWarning */,
787 true /* aLogIt */);
788 va_end(args);
789 return hrc;
790}
791
792/**
793 * Like setError(), but disables the "log" flag in the call to setErrorInternal().
794 * @param aResultCode
795 * @param pcsz
796 * @return
797 */
798HRESULT VirtualBoxBase::setErrorNoLog(HRESULT aResultCode, const char *pcsz, ...)
799{
800 va_list args;
801 va_start(args, pcsz);
802 HRESULT hrc = setErrorInternalV(aResultCode,
803 this->getClassIID(),
804 this->getComponentName(),
805 pcsz, args,
806 false /* aWarning */,
807 false /* aLogIt */);
808 va_end(args);
809 return hrc;
810}
811
812/**
813 * Clear the current error information.
814 */
815/*static*/
816void VirtualBoxBase::clearError(void)
817{
818#if !defined(VBOX_WITH_XPCOM)
819 ::SetErrorInfo(0, NULL);
820#else
821 HRESULT hrc = S_OK;
822 nsCOMPtr <nsIExceptionService> es;
823 es = do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID, &hrc);
824 if (NS_SUCCEEDED(hrc))
825 {
826 nsCOMPtr <nsIExceptionManager> em;
827 hrc = es->GetCurrentExceptionManager(getter_AddRefs(em));
828 if (SUCCEEDED(hrc))
829 em->SetCurrentException(NULL);
830 }
831#endif
832}
833
834
835////////////////////////////////////////////////////////////////////////////////
836//
837// MultiResult methods
838//
839////////////////////////////////////////////////////////////////////////////////
840
841RTTLS MultiResult::sCounter = NIL_RTTLS;
842
843/*static*/
844void MultiResult::incCounter()
845{
846 if (sCounter == NIL_RTTLS)
847 {
848 sCounter = RTTlsAlloc();
849 AssertReturnVoid(sCounter != NIL_RTTLS);
850 }
851
852 uintptr_t counter = (uintptr_t)RTTlsGet(sCounter);
853 ++counter;
854 RTTlsSet(sCounter, (void*)counter);
855}
856
857/*static*/
858void MultiResult::decCounter()
859{
860 uintptr_t counter = (uintptr_t)RTTlsGet(sCounter);
861 AssertReturnVoid(counter != 0);
862 --counter;
863 RTTlsSet(sCounter, (void*)counter);
864}
865
866/*static*/
867bool MultiResult::isMultiEnabled()
868{
869 if (sCounter == NIL_RTTLS)
870 return false;
871
872 return ((uintptr_t)RTTlsGet(MultiResult::sCounter)) > 0;
873}
874
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette