1 | /** @file
|
---|
2 | *
|
---|
3 | * ErrorInfo class definition
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #if !defined (VBOX_WITH_XPCOM)
|
---|
23 |
|
---|
24 | #else
|
---|
25 |
|
---|
26 | #include <nsIServiceManager.h>
|
---|
27 | #include <nsIExceptionService.h>
|
---|
28 | #include <nsCOMPtr.h>
|
---|
29 |
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #include "VBox/com/VirtualBox.h"
|
---|
33 | #include "VBox/com/ErrorInfo.h"
|
---|
34 | #include "VBox/com/assert.h"
|
---|
35 | #include "VBox/com/com.h"
|
---|
36 |
|
---|
37 | #include <iprt/stream.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 |
|
---|
40 | #include <VBox/err.h>
|
---|
41 |
|
---|
42 | namespace com
|
---|
43 | {
|
---|
44 |
|
---|
45 | // ErrorInfo class
|
---|
46 | ////////////////////////////////////////////////////////////////////////////////
|
---|
47 |
|
---|
48 | void ErrorInfo::init (bool aKeepObj /* = false */)
|
---|
49 | {
|
---|
50 | HRESULT rc = E_FAIL;
|
---|
51 |
|
---|
52 | #if !defined (VBOX_WITH_XPCOM)
|
---|
53 |
|
---|
54 | ComPtr <IErrorInfo> err;
|
---|
55 | rc = ::GetErrorInfo (0, err.asOutParam());
|
---|
56 | if (rc == S_OK && err)
|
---|
57 | {
|
---|
58 | if (aKeepObj)
|
---|
59 | mErrorInfo = err;
|
---|
60 |
|
---|
61 | ComPtr <IVirtualBoxErrorInfo> info;
|
---|
62 | rc = err.queryInterfaceTo (info.asOutParam());
|
---|
63 | if (SUCCEEDED (rc) && info)
|
---|
64 | init (info);
|
---|
65 |
|
---|
66 | if (!mIsFullAvailable)
|
---|
67 | {
|
---|
68 | bool gotSomething = false;
|
---|
69 |
|
---|
70 | rc = err->GetGUID (mInterfaceID.asOutParam());
|
---|
71 | gotSomething |= SUCCEEDED (rc);
|
---|
72 | if (SUCCEEDED (rc))
|
---|
73 | GetInterfaceNameByIID (mInterfaceID, mInterfaceName.asOutParam());
|
---|
74 |
|
---|
75 | rc = err->GetSource (mComponent.asOutParam());
|
---|
76 | gotSomething |= SUCCEEDED (rc);
|
---|
77 |
|
---|
78 | rc = err->GetDescription (mText.asOutParam());
|
---|
79 | gotSomething |= SUCCEEDED (rc);
|
---|
80 |
|
---|
81 | if (gotSomething)
|
---|
82 | mIsBasicAvailable = true;
|
---|
83 |
|
---|
84 | AssertMsg (gotSomething, ("Nothing to fetch!\n"));
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | #else // !defined (VBOX_WITH_XPCOM)
|
---|
89 |
|
---|
90 | nsCOMPtr <nsIExceptionService> es;
|
---|
91 | es = do_GetService (NS_EXCEPTIONSERVICE_CONTRACTID, &rc);
|
---|
92 | if (NS_SUCCEEDED (rc))
|
---|
93 | {
|
---|
94 | nsCOMPtr <nsIExceptionManager> em;
|
---|
95 | rc = es->GetCurrentExceptionManager (getter_AddRefs (em));
|
---|
96 | if (NS_SUCCEEDED (rc))
|
---|
97 | {
|
---|
98 | ComPtr <nsIException> ex;
|
---|
99 | rc = em->GetCurrentException (ex.asOutParam());
|
---|
100 | if (NS_SUCCEEDED (rc) && ex)
|
---|
101 | {
|
---|
102 | if (aKeepObj)
|
---|
103 | mErrorInfo = ex;
|
---|
104 |
|
---|
105 | ComPtr <IVirtualBoxErrorInfo> info;
|
---|
106 | rc = ex.queryInterfaceTo (info.asOutParam());
|
---|
107 | if (NS_SUCCEEDED (rc) && info)
|
---|
108 | init (info);
|
---|
109 |
|
---|
110 | if (!mIsFullAvailable)
|
---|
111 | {
|
---|
112 | bool gotSomething = false;
|
---|
113 |
|
---|
114 | rc = ex->GetResult (&mResultCode);
|
---|
115 | gotSomething |= NS_SUCCEEDED (rc);
|
---|
116 |
|
---|
117 | Utf8Str message;
|
---|
118 | rc = ex->GetMessage (message.asOutParam());
|
---|
119 | gotSomething |= NS_SUCCEEDED (rc);
|
---|
120 | if (NS_SUCCEEDED (rc))
|
---|
121 | mText = message;
|
---|
122 |
|
---|
123 | if (gotSomething)
|
---|
124 | mIsBasicAvailable = true;
|
---|
125 |
|
---|
126 | AssertMsg (gotSomething, ("Nothing to fetch!\n"));
|
---|
127 | }
|
---|
128 |
|
---|
129 | // set the exception to NULL (to emulate Win32 behavior)
|
---|
130 | em->SetCurrentException (NULL);
|
---|
131 |
|
---|
132 | rc = NS_OK;
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | AssertComRC (rc);
|
---|
138 |
|
---|
139 | #endif // !defined (VBOX_WITH_XPCOM)
|
---|
140 | }
|
---|
141 |
|
---|
142 | void ErrorInfo::init (IUnknown *aI, const GUID &aIID, bool aKeepObj /* = false */)
|
---|
143 | {
|
---|
144 | Assert (aI);
|
---|
145 | if (!aI)
|
---|
146 | return;
|
---|
147 |
|
---|
148 | #if !defined (VBOX_WITH_XPCOM)
|
---|
149 |
|
---|
150 | ComPtr <IUnknown> iface = aI;
|
---|
151 | ComPtr <ISupportErrorInfo> serr;
|
---|
152 | HRESULT rc = iface.queryInterfaceTo (serr.asOutParam());
|
---|
153 | if (SUCCEEDED (rc))
|
---|
154 | {
|
---|
155 | rc = serr->InterfaceSupportsErrorInfo (aIID);
|
---|
156 | if (SUCCEEDED (rc))
|
---|
157 | init (aKeepObj);
|
---|
158 | }
|
---|
159 |
|
---|
160 | #else
|
---|
161 |
|
---|
162 | init (aKeepObj);
|
---|
163 |
|
---|
164 | #endif
|
---|
165 |
|
---|
166 | if (mIsBasicAvailable)
|
---|
167 | {
|
---|
168 | mCalleeIID = aIID;
|
---|
169 | GetInterfaceNameByIID (aIID, mCalleeName.asOutParam());
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | void ErrorInfo::init (IVirtualBoxErrorInfo *info)
|
---|
174 | {
|
---|
175 | AssertReturnVoid (info);
|
---|
176 |
|
---|
177 | HRESULT rc = E_FAIL;
|
---|
178 | bool gotSomething = false;
|
---|
179 | bool gotAll = true;
|
---|
180 |
|
---|
181 | rc = info->COMGETTER(ResultCode) (&mResultCode);
|
---|
182 | gotSomething |= SUCCEEDED (rc);
|
---|
183 | gotAll &= SUCCEEDED (rc);
|
---|
184 |
|
---|
185 | rc = info->COMGETTER(InterfaceID) (mInterfaceID.asOutParam());
|
---|
186 | gotSomething |= SUCCEEDED (rc);
|
---|
187 | gotAll &= SUCCEEDED (rc);
|
---|
188 | if (SUCCEEDED (rc))
|
---|
189 | GetInterfaceNameByIID (mInterfaceID, mInterfaceName.asOutParam());
|
---|
190 |
|
---|
191 | rc = info->COMGETTER(Component) (mComponent.asOutParam());
|
---|
192 | gotSomething |= SUCCEEDED (rc);
|
---|
193 | gotAll &= SUCCEEDED (rc);
|
---|
194 |
|
---|
195 | rc = info->COMGETTER(Text) (mText.asOutParam());
|
---|
196 | gotSomething |= SUCCEEDED (rc);
|
---|
197 | gotAll &= SUCCEEDED (rc);
|
---|
198 |
|
---|
199 | ComPtr <IVirtualBoxErrorInfo> next;
|
---|
200 | rc = info->COMGETTER(Next) (next.asOutParam());
|
---|
201 | if (SUCCEEDED (rc) && !next.isNull())
|
---|
202 | {
|
---|
203 | mNext.reset (new ErrorInfo (next));
|
---|
204 | Assert (mNext.get());
|
---|
205 | if (!mNext.get())
|
---|
206 | rc = E_OUTOFMEMORY;
|
---|
207 | }
|
---|
208 | else
|
---|
209 | mNext.reset();
|
---|
210 | gotSomething |= SUCCEEDED (rc);
|
---|
211 | gotAll &= SUCCEEDED (rc);
|
---|
212 |
|
---|
213 | mIsBasicAvailable = gotSomething;
|
---|
214 | mIsFullAvailable = gotAll;
|
---|
215 |
|
---|
216 | AssertMsg (gotSomething, ("Nothing to fetch!\n"));
|
---|
217 | }
|
---|
218 |
|
---|
219 | ErrorInfo::~ErrorInfo()
|
---|
220 | {
|
---|
221 | }
|
---|
222 |
|
---|
223 | void ErrorInfo::print (const char *aPrefix /* = NULL */)
|
---|
224 | {
|
---|
225 | if (aPrefix == NULL)
|
---|
226 | aPrefix = "";
|
---|
227 |
|
---|
228 | RTPrintf ("%sFull error info present: %RTbool, basic error info present: %RTbool\n", aPrefix,
|
---|
229 | mIsFullAvailable, mIsBasicAvailable);
|
---|
230 | if (mIsFullAvailable || mIsBasicAvailable)
|
---|
231 | {
|
---|
232 | RTPrintf ("%sResult Code = %Rwa\n", aPrefix, mResultCode);
|
---|
233 | RTPrintf ("%sText = %ls\n", aPrefix, mText.raw());
|
---|
234 | RTPrintf ("%sComponent = %ls, Interface: %ls, {%s}\n", aPrefix,
|
---|
235 | mComponent.raw(), mInterfaceName.raw(), mInterfaceID.toString().raw());
|
---|
236 | RTPrintf ("%sCallee = %ls, {%s}\n", aPrefix, mCalleeName.raw(), mCalleeIID.toString().raw());
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Sets the given error info object for the current thread. If @a aPreserve
|
---|
242 | * is @c true, then the current error info set on the thread before this
|
---|
243 | * method is called will be preserved in the IVirtualBoxErrorInfo::next
|
---|
244 | * attribute of the new error info object that will be then set as the
|
---|
245 | * current error info object.
|
---|
246 | */
|
---|
247 |
|
---|
248 | //static
|
---|
249 | HRESULT setError (IVirtualBoxErrorInfo *aInfo);
|
---|
250 |
|
---|
251 | // ProgressErrorInfo class
|
---|
252 | ////////////////////////////////////////////////////////////////////////////////
|
---|
253 |
|
---|
254 | ProgressErrorInfo::ProgressErrorInfo (IProgress *progress) :
|
---|
255 | ErrorInfo (false /* aDummy */)
|
---|
256 | {
|
---|
257 | Assert (progress);
|
---|
258 | if (!progress)
|
---|
259 | return;
|
---|
260 |
|
---|
261 | ComPtr <IVirtualBoxErrorInfo> info;
|
---|
262 | HRESULT rc = progress->COMGETTER(ErrorInfo) (info.asOutParam());
|
---|
263 | if (SUCCEEDED (rc) && info)
|
---|
264 | init (info);
|
---|
265 | }
|
---|
266 |
|
---|
267 | // ErrorInfoKeeper class
|
---|
268 | ////////////////////////////////////////////////////////////////////////////////
|
---|
269 |
|
---|
270 | HRESULT ErrorInfoKeeper::restore()
|
---|
271 | {
|
---|
272 | if (mForgot)
|
---|
273 | return S_OK;
|
---|
274 |
|
---|
275 | HRESULT rc = S_OK;
|
---|
276 |
|
---|
277 | #if !defined (VBOX_WITH_XPCOM)
|
---|
278 |
|
---|
279 | ComPtr <IErrorInfo> err;
|
---|
280 | if (!mErrorInfo.isNull())
|
---|
281 | {
|
---|
282 | rc = mErrorInfo.queryInterfaceTo (err.asOutParam());
|
---|
283 | AssertComRC (rc);
|
---|
284 | }
|
---|
285 | rc = ::SetErrorInfo (0, err);
|
---|
286 |
|
---|
287 | #else // !defined (VBOX_WITH_XPCOM)
|
---|
288 |
|
---|
289 | nsCOMPtr <nsIExceptionService> es;
|
---|
290 | es = do_GetService (NS_EXCEPTIONSERVICE_CONTRACTID, &rc);
|
---|
291 | if (NS_SUCCEEDED (rc))
|
---|
292 | {
|
---|
293 | nsCOMPtr <nsIExceptionManager> em;
|
---|
294 | rc = es->GetCurrentExceptionManager (getter_AddRefs (em));
|
---|
295 | if (NS_SUCCEEDED (rc))
|
---|
296 | {
|
---|
297 | ComPtr <nsIException> ex;
|
---|
298 | if (!mErrorInfo.isNull())
|
---|
299 | {
|
---|
300 | rc = mErrorInfo.queryInterfaceTo (ex.asOutParam());
|
---|
301 | AssertComRC (rc);
|
---|
302 | }
|
---|
303 | rc = em->SetCurrentException (ex);
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | #endif // !defined (VBOX_WITH_XPCOM)
|
---|
308 |
|
---|
309 | if (SUCCEEDED (rc))
|
---|
310 | {
|
---|
311 | mErrorInfo.setNull();
|
---|
312 | mForgot = true;
|
---|
313 | }
|
---|
314 |
|
---|
315 | return rc;
|
---|
316 | }
|
---|
317 |
|
---|
318 | }; // namespace com
|
---|
319 |
|
---|