VirtualBox

source: vbox/trunk/src/VBox/Main/glue/ErrorInfo.cpp@ 30670

Last change on this file since 30670 was 30670, checked in by vboxsync, 15 years ago

Main: COM header cleanup (remove obscure and unused templates)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.5 KB
Line 
1/* $Id: ErrorInfo.cpp 30670 2010-07-06 14:37:09Z vboxsync $ */
2
3/** @file
4 *
5 * ErrorInfo class definition
6 */
7
8/*
9 * Copyright (C) 2006-2007 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.215389.xyz. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20#if !defined (VBOX_WITH_XPCOM)
21
22#else
23
24#include <nsIServiceManager.h>
25#include <nsIExceptionService.h>
26#include <nsCOMPtr.h>
27
28#endif
29
30#include "VBox/com/VirtualBox.h"
31#include "VBox/com/ErrorInfo.h"
32#include "VBox/com/assert.h"
33#include "VBox/com/com.h"
34
35#include <iprt/stream.h>
36#include <iprt/string.h>
37
38#include <VBox/err.h>
39
40namespace com
41{
42
43// ErrorInfo class
44////////////////////////////////////////////////////////////////////////////////
45
46void ErrorInfo::init(bool aKeepObj /* = false */)
47{
48 HRESULT rc = E_FAIL;
49
50#if !defined (VBOX_WITH_XPCOM)
51
52 ComPtr<IErrorInfo> err;
53 rc = ::GetErrorInfo (0, err.asOutParam());
54 if (rc == S_OK && err)
55 {
56 if (aKeepObj)
57 mErrorInfo = err;
58
59 ComPtr<IVirtualBoxErrorInfo> info;
60 rc = err.queryInterfaceTo(info.asOutParam());
61 if (SUCCEEDED(rc) && info)
62 init (info);
63
64 if (!mIsFullAvailable)
65 {
66 bool gotSomething = false;
67
68 rc = err->GetGUID (mInterfaceID.asOutParam());
69 gotSomething |= SUCCEEDED(rc);
70 if (SUCCEEDED(rc))
71 GetInterfaceNameByIID (mInterfaceID, mInterfaceName.asOutParam());
72
73 rc = err->GetSource (mComponent.asOutParam());
74 gotSomething |= SUCCEEDED(rc);
75
76 rc = err->GetDescription (mText.asOutParam());
77 gotSomething |= SUCCEEDED(rc);
78
79 if (gotSomething)
80 mIsBasicAvailable = true;
81
82 AssertMsg (gotSomething, ("Nothing to fetch!\n"));
83 }
84 }
85
86#else // defined (VBOX_WITH_XPCOM)
87
88 nsCOMPtr<nsIExceptionService> es;
89 es = do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID, &rc);
90 if (NS_SUCCEEDED(rc))
91 {
92 nsCOMPtr<nsIExceptionManager> em;
93 rc = es->GetCurrentExceptionManager(getter_AddRefs (em));
94 if (NS_SUCCEEDED(rc))
95 {
96 ComPtr<nsIException> ex;
97 rc = em->GetCurrentException(ex.asOutParam());
98 if (NS_SUCCEEDED(rc) && ex)
99 {
100 if (aKeepObj)
101 mErrorInfo = ex;
102
103 ComPtr<IVirtualBoxErrorInfo> info;
104 rc = ex.queryInterfaceTo(info.asOutParam());
105 if (NS_SUCCEEDED(rc) && info)
106 init (info);
107
108 if (!mIsFullAvailable)
109 {
110 bool gotSomething = false;
111
112 rc = ex->GetResult(&mResultCode);
113 gotSomething |= NS_SUCCEEDED(rc);
114
115 char *pszMsg;
116 rc = ex->GetMessage(&pszMsg);
117 gotSomething |= NS_SUCCEEDED(rc);
118 if (NS_SUCCEEDED(rc))
119 {
120 mText = Bstr(pszMsg);
121 nsMemory::Free(mText);
122 }
123
124 if (gotSomething)
125 mIsBasicAvailable = true;
126
127 AssertMsg (gotSomething, ("Nothing to fetch!\n"));
128 }
129
130 // set the exception to NULL (to emulate Win32 behavior)
131 em->SetCurrentException (NULL);
132
133 rc = NS_OK;
134 }
135 }
136 }
137 /* Ignore failure when called after nsComponentManagerImpl::Shutdown(). */
138 else if (rc == NS_ERROR_UNEXPECTED)
139 rc = NS_OK;
140
141 AssertComRC (rc);
142
143#endif // defined (VBOX_WITH_XPCOM)
144}
145
146void ErrorInfo::init(IUnknown *aI,
147 const GUID &aIID,
148 bool aKeepObj /* = false */)
149{
150 Assert(aI);
151 if (!aI)
152 return;
153
154#if !defined (VBOX_WITH_XPCOM)
155
156 ComPtr<IUnknown> iface = aI;
157 ComPtr<ISupportErrorInfo> serr;
158 HRESULT rc = iface.queryInterfaceTo(serr.asOutParam());
159 if (SUCCEEDED(rc))
160 {
161 rc = serr->InterfaceSupportsErrorInfo (aIID);
162 if (SUCCEEDED(rc))
163 init (aKeepObj);
164 }
165
166#else
167
168 init (aKeepObj);
169
170#endif
171
172 if (mIsBasicAvailable)
173 {
174 mCalleeIID = aIID;
175 GetInterfaceNameByIID (aIID, mCalleeName.asOutParam());
176 }
177}
178
179void ErrorInfo::init(IVirtualBoxErrorInfo *info)
180{
181 AssertReturnVoid (info);
182
183 HRESULT rc = E_FAIL;
184 bool gotSomething = false;
185 bool gotAll = true;
186 LONG lrc;
187
188 rc = info->COMGETTER(ResultCode) (&lrc); mResultCode = lrc;
189 gotSomething |= SUCCEEDED(rc);
190 gotAll &= SUCCEEDED(rc);
191
192 Bstr iid;
193 rc = info->COMGETTER(InterfaceID) (iid.asOutParam());
194 gotSomething |= SUCCEEDED(rc);
195 gotAll &= SUCCEEDED(rc);
196 if (SUCCEEDED(rc))
197 {
198 mInterfaceID = iid;
199 GetInterfaceNameByIID (mInterfaceID, mInterfaceName.asOutParam());
200 }
201
202 rc = info->COMGETTER(Component) (mComponent.asOutParam());
203 gotSomething |= SUCCEEDED(rc);
204 gotAll &= SUCCEEDED(rc);
205
206 rc = info->COMGETTER(Text) (mText.asOutParam());
207 gotSomething |= SUCCEEDED(rc);
208 gotAll &= SUCCEEDED(rc);
209
210 if (m_pNext)
211 {
212 delete m_pNext;
213 m_pNext = NULL;
214 }
215
216 ComPtr<IVirtualBoxErrorInfo> next;
217 rc = info->COMGETTER(Next) (next.asOutParam());
218 if (SUCCEEDED(rc) && !next.isNull())
219 {
220 m_pNext = new ErrorInfo(next);
221 Assert(m_pNext);
222 if (!m_pNext)
223 rc = E_OUTOFMEMORY;
224 }
225
226 gotSomething |= SUCCEEDED(rc);
227 gotAll &= SUCCEEDED(rc);
228
229 mIsBasicAvailable = gotSomething;
230 mIsFullAvailable = gotAll;
231
232 AssertMsg (gotSomething, ("Nothing to fetch!\n"));
233}
234
235ErrorInfo::~ErrorInfo()
236{
237 if (m_pNext)
238 {
239 delete m_pNext;
240 m_pNext = NULL;
241 }
242}
243
244// ProgressErrorInfo class
245////////////////////////////////////////////////////////////////////////////////
246
247ProgressErrorInfo::ProgressErrorInfo (IProgress *progress) :
248 ErrorInfo (false /* aDummy */)
249{
250 Assert(progress);
251 if (!progress)
252 return;
253
254 ComPtr<IVirtualBoxErrorInfo> info;
255 HRESULT rc = progress->COMGETTER(ErrorInfo) (info.asOutParam());
256 if (SUCCEEDED(rc) && info)
257 init (info);
258}
259
260// ErrorInfoKeeper class
261////////////////////////////////////////////////////////////////////////////////
262
263HRESULT ErrorInfoKeeper::restore()
264{
265 if (mForgot)
266 return S_OK;
267
268 HRESULT rc = S_OK;
269
270#if !defined (VBOX_WITH_XPCOM)
271
272 ComPtr<IErrorInfo> err;
273 if (!mErrorInfo.isNull())
274 {
275 rc = mErrorInfo.queryInterfaceTo(err.asOutParam());
276 AssertComRC (rc);
277 }
278 rc = ::SetErrorInfo (0, err);
279
280#else // !defined (VBOX_WITH_XPCOM)
281
282 nsCOMPtr <nsIExceptionService> es;
283 es = do_GetService (NS_EXCEPTIONSERVICE_CONTRACTID, &rc);
284 if (NS_SUCCEEDED(rc))
285 {
286 nsCOMPtr <nsIExceptionManager> em;
287 rc = es->GetCurrentExceptionManager (getter_AddRefs (em));
288 if (NS_SUCCEEDED(rc))
289 {
290 ComPtr<nsIException> ex;
291 if (!mErrorInfo.isNull())
292 {
293 rc = mErrorInfo.queryInterfaceTo(ex.asOutParam());
294 AssertComRC (rc);
295 }
296 rc = em->SetCurrentException (ex);
297 }
298 }
299
300#endif // !defined (VBOX_WITH_XPCOM)
301
302 if (SUCCEEDED(rc))
303 {
304 mErrorInfo.setNull();
305 mForgot = true;
306 }
307
308 return rc;
309}
310
311} /* namespace com */
312
Note: See TracBrowser for help on using the repository browser.

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