1 | /* $Id: ErrorInfo.cpp 30739 2010-07-08 12:27:42Z 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.virtualbox.org. 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 | #else
|
---|
22 | #include <nsIServiceManager.h>
|
---|
23 | #include <nsIExceptionService.h>
|
---|
24 | #include <nsCOMPtr.h>
|
---|
25 | #endif
|
---|
26 |
|
---|
27 | #include "VBox/com/VirtualBox.h"
|
---|
28 | #include "VBox/com/ErrorInfo.h"
|
---|
29 | #include "VBox/com/assert.h"
|
---|
30 | #include "VBox/com/com.h"
|
---|
31 | #include "VBox/com/MultiResult.h"
|
---|
32 |
|
---|
33 | #include <iprt/stream.h>
|
---|
34 | #include <iprt/string.h>
|
---|
35 |
|
---|
36 | #include <VBox/err.h>
|
---|
37 |
|
---|
38 | namespace com
|
---|
39 | {
|
---|
40 |
|
---|
41 | ////////////////////////////////////////////////////////////////////////////////
|
---|
42 | //
|
---|
43 | // ErrorInfo class
|
---|
44 | //
|
---|
45 | ////////////////////////////////////////////////////////////////////////////////
|
---|
46 |
|
---|
47 | void ErrorInfo::copyFrom(const ErrorInfo &x)
|
---|
48 | {
|
---|
49 | mIsBasicAvailable = x.mIsBasicAvailable;
|
---|
50 | mIsFullAvailable = x.mIsFullAvailable;
|
---|
51 |
|
---|
52 | mResultCode = x.mResultCode;
|
---|
53 | mInterfaceID = x.mInterfaceID;
|
---|
54 | mComponent = x.mComponent;
|
---|
55 | mText = x.mText;
|
---|
56 |
|
---|
57 | if (x.m_pNext != NULL)
|
---|
58 | m_pNext = new ErrorInfo(x.m_pNext);
|
---|
59 | else
|
---|
60 | m_pNext = NULL;
|
---|
61 |
|
---|
62 | mInterfaceName = x.mInterfaceName;
|
---|
63 | mCalleeIID = x.mCalleeIID;
|
---|
64 | mCalleeName = x.mCalleeName;
|
---|
65 |
|
---|
66 | mErrorInfo = x.mErrorInfo;
|
---|
67 | }
|
---|
68 |
|
---|
69 | void ErrorInfo::cleanup()
|
---|
70 | {
|
---|
71 | mIsBasicAvailable = false;
|
---|
72 | mIsFullAvailable = false;
|
---|
73 |
|
---|
74 | if (m_pNext)
|
---|
75 | {
|
---|
76 | delete m_pNext;
|
---|
77 | m_pNext = NULL;
|
---|
78 | }
|
---|
79 |
|
---|
80 | mResultCode = S_OK;
|
---|
81 | mInterfaceID.clear();
|
---|
82 | mComponent.setNull();
|
---|
83 | mText.setNull();
|
---|
84 | mInterfaceName.setNull();
|
---|
85 | mCalleeIID.clear();
|
---|
86 | mCalleeName.setNull();
|
---|
87 | mErrorInfo.setNull();
|
---|
88 | }
|
---|
89 |
|
---|
90 | void ErrorInfo::init(bool aKeepObj /* = false */)
|
---|
91 | {
|
---|
92 | HRESULT rc = E_FAIL;
|
---|
93 |
|
---|
94 | #if !defined (VBOX_WITH_XPCOM)
|
---|
95 |
|
---|
96 | ComPtr<IErrorInfo> err;
|
---|
97 | rc = ::GetErrorInfo (0, err.asOutParam());
|
---|
98 | if (rc == S_OK && err)
|
---|
99 | {
|
---|
100 | if (aKeepObj)
|
---|
101 | mErrorInfo = err;
|
---|
102 |
|
---|
103 | ComPtr<IVirtualBoxErrorInfo> info;
|
---|
104 | rc = err.queryInterfaceTo(info.asOutParam());
|
---|
105 | if (SUCCEEDED(rc) && info)
|
---|
106 | init (info);
|
---|
107 |
|
---|
108 | if (!mIsFullAvailable)
|
---|
109 | {
|
---|
110 | bool gotSomething = false;
|
---|
111 |
|
---|
112 | rc = err->GetGUID (mInterfaceID.asOutParam());
|
---|
113 | gotSomething |= SUCCEEDED(rc);
|
---|
114 | if (SUCCEEDED(rc))
|
---|
115 | GetInterfaceNameByIID (mInterfaceID, mInterfaceName.asOutParam());
|
---|
116 |
|
---|
117 | rc = err->GetSource (mComponent.asOutParam());
|
---|
118 | gotSomething |= SUCCEEDED(rc);
|
---|
119 |
|
---|
120 | rc = err->GetDescription (mText.asOutParam());
|
---|
121 | gotSomething |= SUCCEEDED(rc);
|
---|
122 |
|
---|
123 | if (gotSomething)
|
---|
124 | mIsBasicAvailable = true;
|
---|
125 |
|
---|
126 | AssertMsg (gotSomething, ("Nothing to fetch!\n"));
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | #else // defined (VBOX_WITH_XPCOM)
|
---|
131 |
|
---|
132 | nsCOMPtr<nsIExceptionService> es;
|
---|
133 | es = do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID, &rc);
|
---|
134 | if (NS_SUCCEEDED(rc))
|
---|
135 | {
|
---|
136 | nsCOMPtr<nsIExceptionManager> em;
|
---|
137 | rc = es->GetCurrentExceptionManager(getter_AddRefs (em));
|
---|
138 | if (NS_SUCCEEDED(rc))
|
---|
139 | {
|
---|
140 | ComPtr<nsIException> ex;
|
---|
141 | rc = em->GetCurrentException(ex.asOutParam());
|
---|
142 | if (NS_SUCCEEDED(rc) && ex)
|
---|
143 | {
|
---|
144 | if (aKeepObj)
|
---|
145 | mErrorInfo = ex;
|
---|
146 |
|
---|
147 | ComPtr<IVirtualBoxErrorInfo> info;
|
---|
148 | rc = ex.queryInterfaceTo(info.asOutParam());
|
---|
149 | if (NS_SUCCEEDED(rc) && info)
|
---|
150 | init (info);
|
---|
151 |
|
---|
152 | if (!mIsFullAvailable)
|
---|
153 | {
|
---|
154 | bool gotSomething = false;
|
---|
155 |
|
---|
156 | rc = ex->GetResult(&mResultCode);
|
---|
157 | gotSomething |= NS_SUCCEEDED(rc);
|
---|
158 |
|
---|
159 | char *pszMsg;
|
---|
160 | rc = ex->GetMessage(&pszMsg);
|
---|
161 | gotSomething |= NS_SUCCEEDED(rc);
|
---|
162 | if (NS_SUCCEEDED(rc))
|
---|
163 | {
|
---|
164 | mText = Bstr(pszMsg);
|
---|
165 | nsMemory::Free(mText);
|
---|
166 | }
|
---|
167 |
|
---|
168 | if (gotSomething)
|
---|
169 | mIsBasicAvailable = true;
|
---|
170 |
|
---|
171 | AssertMsg (gotSomething, ("Nothing to fetch!\n"));
|
---|
172 | }
|
---|
173 |
|
---|
174 | // set the exception to NULL (to emulate Win32 behavior)
|
---|
175 | em->SetCurrentException (NULL);
|
---|
176 |
|
---|
177 | rc = NS_OK;
|
---|
178 | }
|
---|
179 | }
|
---|
180 | }
|
---|
181 | /* Ignore failure when called after nsComponentManagerImpl::Shutdown(). */
|
---|
182 | else if (rc == NS_ERROR_UNEXPECTED)
|
---|
183 | rc = NS_OK;
|
---|
184 |
|
---|
185 | AssertComRC (rc);
|
---|
186 |
|
---|
187 | #endif // defined (VBOX_WITH_XPCOM)
|
---|
188 | }
|
---|
189 |
|
---|
190 | void ErrorInfo::init(IUnknown *aI,
|
---|
191 | const GUID &aIID,
|
---|
192 | bool aKeepObj /* = false */)
|
---|
193 | {
|
---|
194 | Assert(aI);
|
---|
195 | if (!aI)
|
---|
196 | return;
|
---|
197 |
|
---|
198 | #if !defined (VBOX_WITH_XPCOM)
|
---|
199 |
|
---|
200 | ComPtr<IUnknown> iface = aI;
|
---|
201 | ComPtr<ISupportErrorInfo> serr;
|
---|
202 | HRESULT rc = iface.queryInterfaceTo(serr.asOutParam());
|
---|
203 | if (SUCCEEDED(rc))
|
---|
204 | {
|
---|
205 | rc = serr->InterfaceSupportsErrorInfo (aIID);
|
---|
206 | if (SUCCEEDED(rc))
|
---|
207 | init (aKeepObj);
|
---|
208 | }
|
---|
209 |
|
---|
210 | #else
|
---|
211 |
|
---|
212 | init (aKeepObj);
|
---|
213 |
|
---|
214 | #endif
|
---|
215 |
|
---|
216 | if (mIsBasicAvailable)
|
---|
217 | {
|
---|
218 | mCalleeIID = aIID;
|
---|
219 | GetInterfaceNameByIID (aIID, mCalleeName.asOutParam());
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | void ErrorInfo::init(IVirtualBoxErrorInfo *info)
|
---|
224 | {
|
---|
225 | AssertReturnVoid (info);
|
---|
226 |
|
---|
227 | HRESULT rc = E_FAIL;
|
---|
228 | bool gotSomething = false;
|
---|
229 | bool gotAll = true;
|
---|
230 | LONG lrc;
|
---|
231 |
|
---|
232 | rc = info->COMGETTER(ResultCode) (&lrc); mResultCode = lrc;
|
---|
233 | gotSomething |= SUCCEEDED(rc);
|
---|
234 | gotAll &= SUCCEEDED(rc);
|
---|
235 |
|
---|
236 | Bstr iid;
|
---|
237 | rc = info->COMGETTER(InterfaceID) (iid.asOutParam());
|
---|
238 | gotSomething |= SUCCEEDED(rc);
|
---|
239 | gotAll &= SUCCEEDED(rc);
|
---|
240 | if (SUCCEEDED(rc))
|
---|
241 | {
|
---|
242 | mInterfaceID = iid;
|
---|
243 | GetInterfaceNameByIID (mInterfaceID, mInterfaceName.asOutParam());
|
---|
244 | }
|
---|
245 |
|
---|
246 | rc = info->COMGETTER(Component) (mComponent.asOutParam());
|
---|
247 | gotSomething |= SUCCEEDED(rc);
|
---|
248 | gotAll &= SUCCEEDED(rc);
|
---|
249 |
|
---|
250 | rc = info->COMGETTER(Text) (mText.asOutParam());
|
---|
251 | gotSomething |= SUCCEEDED(rc);
|
---|
252 | gotAll &= SUCCEEDED(rc);
|
---|
253 |
|
---|
254 | m_pNext = NULL;
|
---|
255 |
|
---|
256 | ComPtr<IVirtualBoxErrorInfo> next;
|
---|
257 | rc = info->COMGETTER(Next) (next.asOutParam());
|
---|
258 | if (SUCCEEDED(rc) && !next.isNull())
|
---|
259 | {
|
---|
260 | m_pNext = new ErrorInfo(next);
|
---|
261 | Assert(m_pNext != NULL);
|
---|
262 | if (!m_pNext)
|
---|
263 | rc = E_OUTOFMEMORY;
|
---|
264 | }
|
---|
265 |
|
---|
266 | gotSomething |= SUCCEEDED(rc);
|
---|
267 | gotAll &= SUCCEEDED(rc);
|
---|
268 |
|
---|
269 | mIsBasicAvailable = gotSomething;
|
---|
270 | mIsFullAvailable = gotAll;
|
---|
271 |
|
---|
272 | AssertMsg (gotSomething, ("Nothing to fetch!\n"));
|
---|
273 | }
|
---|
274 |
|
---|
275 | ////////////////////////////////////////////////////////////////////////////////
|
---|
276 | //
|
---|
277 | // ProgressErrorInfo class
|
---|
278 | //
|
---|
279 | ////////////////////////////////////////////////////////////////////////////////
|
---|
280 |
|
---|
281 | ProgressErrorInfo::ProgressErrorInfo (IProgress *progress) :
|
---|
282 | ErrorInfo (false /* aDummy */)
|
---|
283 | {
|
---|
284 | Assert(progress);
|
---|
285 | if (!progress)
|
---|
286 | return;
|
---|
287 |
|
---|
288 | ComPtr<IVirtualBoxErrorInfo> info;
|
---|
289 | HRESULT rc = progress->COMGETTER(ErrorInfo) (info.asOutParam());
|
---|
290 | if (SUCCEEDED(rc) && info)
|
---|
291 | init (info);
|
---|
292 | }
|
---|
293 |
|
---|
294 | ////////////////////////////////////////////////////////////////////////////////
|
---|
295 | //
|
---|
296 | // ErrorInfoKeeper class
|
---|
297 | //
|
---|
298 | ////////////////////////////////////////////////////////////////////////////////
|
---|
299 |
|
---|
300 | HRESULT ErrorInfoKeeper::restore()
|
---|
301 | {
|
---|
302 | if (mForgot)
|
---|
303 | return S_OK;
|
---|
304 |
|
---|
305 | HRESULT rc = S_OK;
|
---|
306 |
|
---|
307 | #if !defined (VBOX_WITH_XPCOM)
|
---|
308 |
|
---|
309 | ComPtr<IErrorInfo> err;
|
---|
310 | if (!mErrorInfo.isNull())
|
---|
311 | {
|
---|
312 | rc = mErrorInfo.queryInterfaceTo(err.asOutParam());
|
---|
313 | AssertComRC (rc);
|
---|
314 | }
|
---|
315 | rc = ::SetErrorInfo (0, err);
|
---|
316 |
|
---|
317 | #else // !defined (VBOX_WITH_XPCOM)
|
---|
318 |
|
---|
319 | nsCOMPtr <nsIExceptionService> es;
|
---|
320 | es = do_GetService (NS_EXCEPTIONSERVICE_CONTRACTID, &rc);
|
---|
321 | if (NS_SUCCEEDED(rc))
|
---|
322 | {
|
---|
323 | nsCOMPtr <nsIExceptionManager> em;
|
---|
324 | rc = es->GetCurrentExceptionManager (getter_AddRefs (em));
|
---|
325 | if (NS_SUCCEEDED(rc))
|
---|
326 | {
|
---|
327 | ComPtr<nsIException> ex;
|
---|
328 | if (!mErrorInfo.isNull())
|
---|
329 | {
|
---|
330 | rc = mErrorInfo.queryInterfaceTo(ex.asOutParam());
|
---|
331 | AssertComRC (rc);
|
---|
332 | }
|
---|
333 | rc = em->SetCurrentException (ex);
|
---|
334 | }
|
---|
335 | }
|
---|
336 |
|
---|
337 | #endif // !defined (VBOX_WITH_XPCOM)
|
---|
338 |
|
---|
339 | if (SUCCEEDED(rc))
|
---|
340 | {
|
---|
341 | mErrorInfo.setNull();
|
---|
342 | mForgot = true;
|
---|
343 | }
|
---|
344 |
|
---|
345 | return rc;
|
---|
346 | }
|
---|
347 |
|
---|
348 | } /* namespace com */
|
---|
349 |
|
---|