1 | /* $Id: ErrorInfo.cpp 28316 2010-04-14 18:01:39Z vboxsync $ */
|
---|
2 |
|
---|
3 | /** @file
|
---|
4 | *
|
---|
5 | * ErrorInfo class definition
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
20 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
21 | * additional information or have any questions.
|
---|
22 | */
|
---|
23 |
|
---|
24 | #if !defined (VBOX_WITH_XPCOM)
|
---|
25 |
|
---|
26 | #else
|
---|
27 |
|
---|
28 | #include <nsIServiceManager.h>
|
---|
29 | #include <nsIExceptionService.h>
|
---|
30 | #include <nsCOMPtr.h>
|
---|
31 |
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #include "VBox/com/VirtualBox.h"
|
---|
35 | #include "VBox/com/ErrorInfo.h"
|
---|
36 | #include "VBox/com/assert.h"
|
---|
37 | #include "VBox/com/com.h"
|
---|
38 |
|
---|
39 | #include <iprt/stream.h>
|
---|
40 | #include <iprt/string.h>
|
---|
41 |
|
---|
42 | #include <VBox/err.h>
|
---|
43 |
|
---|
44 | namespace com
|
---|
45 | {
|
---|
46 |
|
---|
47 | // ErrorInfo class
|
---|
48 | ////////////////////////////////////////////////////////////////////////////////
|
---|
49 |
|
---|
50 | void ErrorInfo::init (bool aKeepObj /* = false */)
|
---|
51 | {
|
---|
52 | HRESULT rc = E_FAIL;
|
---|
53 |
|
---|
54 | #if !defined (VBOX_WITH_XPCOM)
|
---|
55 |
|
---|
56 | ComPtr<IErrorInfo> err;
|
---|
57 | rc = ::GetErrorInfo (0, err.asOutParam());
|
---|
58 | if (rc == S_OK && err)
|
---|
59 | {
|
---|
60 | if (aKeepObj)
|
---|
61 | mErrorInfo = err;
|
---|
62 |
|
---|
63 | ComPtr<IVirtualBoxErrorInfo> info;
|
---|
64 | rc = err.queryInterfaceTo(info.asOutParam());
|
---|
65 | if (SUCCEEDED(rc) && info)
|
---|
66 | init (info);
|
---|
67 |
|
---|
68 | if (!mIsFullAvailable)
|
---|
69 | {
|
---|
70 | bool gotSomething = false;
|
---|
71 |
|
---|
72 | rc = err->GetGUID (mInterfaceID.asOutParam());
|
---|
73 | gotSomething |= SUCCEEDED(rc);
|
---|
74 | if (SUCCEEDED(rc))
|
---|
75 | GetInterfaceNameByIID (mInterfaceID, mInterfaceName.asOutParam());
|
---|
76 |
|
---|
77 | rc = err->GetSource (mComponent.asOutParam());
|
---|
78 | gotSomething |= SUCCEEDED(rc);
|
---|
79 |
|
---|
80 | rc = err->GetDescription (mText.asOutParam());
|
---|
81 | gotSomething |= SUCCEEDED(rc);
|
---|
82 |
|
---|
83 | if (gotSomething)
|
---|
84 | mIsBasicAvailable = true;
|
---|
85 |
|
---|
86 | AssertMsg (gotSomething, ("Nothing to fetch!\n"));
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | #else // defined (VBOX_WITH_XPCOM)
|
---|
91 |
|
---|
92 | nsCOMPtr<nsIExceptionService> es;
|
---|
93 | es = do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID, &rc);
|
---|
94 | if (NS_SUCCEEDED(rc))
|
---|
95 | {
|
---|
96 | nsCOMPtr<nsIExceptionManager> em;
|
---|
97 | rc = es->GetCurrentExceptionManager(getter_AddRefs (em));
|
---|
98 | if (NS_SUCCEEDED(rc))
|
---|
99 | {
|
---|
100 | ComPtr<nsIException> ex;
|
---|
101 | rc = em->GetCurrentException(ex.asOutParam());
|
---|
102 | if (NS_SUCCEEDED(rc) && ex)
|
---|
103 | {
|
---|
104 | if (aKeepObj)
|
---|
105 | mErrorInfo = ex;
|
---|
106 |
|
---|
107 | ComPtr<IVirtualBoxErrorInfo> info;
|
---|
108 | rc = ex.queryInterfaceTo(info.asOutParam());
|
---|
109 | if (NS_SUCCEEDED(rc) && info)
|
---|
110 | init (info);
|
---|
111 |
|
---|
112 | if (!mIsFullAvailable)
|
---|
113 | {
|
---|
114 | bool gotSomething = false;
|
---|
115 |
|
---|
116 | rc = ex->GetResult(&mResultCode);
|
---|
117 | gotSomething |= NS_SUCCEEDED(rc);
|
---|
118 |
|
---|
119 | char *pszMsg;
|
---|
120 | rc = ex->GetMessage(&pszMsg);
|
---|
121 | gotSomething |= NS_SUCCEEDED(rc);
|
---|
122 | if (NS_SUCCEEDED(rc))
|
---|
123 | {
|
---|
124 | mText = Bstr(pszMsg);
|
---|
125 | nsMemory::Free(mText);
|
---|
126 | }
|
---|
127 |
|
---|
128 | if (gotSomething)
|
---|
129 | mIsBasicAvailable = true;
|
---|
130 |
|
---|
131 | AssertMsg (gotSomething, ("Nothing to fetch!\n"));
|
---|
132 | }
|
---|
133 |
|
---|
134 | // set the exception to NULL (to emulate Win32 behavior)
|
---|
135 | em->SetCurrentException (NULL);
|
---|
136 |
|
---|
137 | rc = NS_OK;
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | AssertComRC (rc);
|
---|
143 |
|
---|
144 | #endif // defined (VBOX_WITH_XPCOM)
|
---|
145 | }
|
---|
146 |
|
---|
147 | void ErrorInfo::init (IUnknown *aI, const GUID &aIID, bool aKeepObj /* = false */)
|
---|
148 | {
|
---|
149 | Assert(aI);
|
---|
150 | if (!aI)
|
---|
151 | return;
|
---|
152 |
|
---|
153 | #if !defined (VBOX_WITH_XPCOM)
|
---|
154 |
|
---|
155 | ComPtr<IUnknown> iface = aI;
|
---|
156 | ComPtr<ISupportErrorInfo> serr;
|
---|
157 | HRESULT rc = iface.queryInterfaceTo(serr.asOutParam());
|
---|
158 | if (SUCCEEDED(rc))
|
---|
159 | {
|
---|
160 | rc = serr->InterfaceSupportsErrorInfo (aIID);
|
---|
161 | if (SUCCEEDED(rc))
|
---|
162 | init (aKeepObj);
|
---|
163 | }
|
---|
164 |
|
---|
165 | #else
|
---|
166 |
|
---|
167 | init (aKeepObj);
|
---|
168 |
|
---|
169 | #endif
|
---|
170 |
|
---|
171 | if (mIsBasicAvailable)
|
---|
172 | {
|
---|
173 | mCalleeIID = aIID;
|
---|
174 | GetInterfaceNameByIID (aIID, mCalleeName.asOutParam());
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | void ErrorInfo::init (IVirtualBoxErrorInfo *info)
|
---|
179 | {
|
---|
180 | AssertReturnVoid (info);
|
---|
181 |
|
---|
182 | HRESULT rc = E_FAIL;
|
---|
183 | bool gotSomething = false;
|
---|
184 | bool gotAll = true;
|
---|
185 | LONG lrc;
|
---|
186 |
|
---|
187 | rc = info->COMGETTER(ResultCode) (&lrc); mResultCode = lrc;
|
---|
188 | gotSomething |= SUCCEEDED(rc);
|
---|
189 | gotAll &= SUCCEEDED(rc);
|
---|
190 |
|
---|
191 | Bstr iid;
|
---|
192 | rc = info->COMGETTER(InterfaceID) (iid.asOutParam());
|
---|
193 | gotSomething |= SUCCEEDED(rc);
|
---|
194 | gotAll &= SUCCEEDED(rc);
|
---|
195 | if (SUCCEEDED(rc))
|
---|
196 | {
|
---|
197 | mInterfaceID = iid;
|
---|
198 | GetInterfaceNameByIID (mInterfaceID, mInterfaceName.asOutParam());
|
---|
199 | }
|
---|
200 |
|
---|
201 | rc = info->COMGETTER(Component) (mComponent.asOutParam());
|
---|
202 | gotSomething |= SUCCEEDED(rc);
|
---|
203 | gotAll &= SUCCEEDED(rc);
|
---|
204 |
|
---|
205 | rc = info->COMGETTER(Text) (mText.asOutParam());
|
---|
206 | gotSomething |= SUCCEEDED(rc);
|
---|
207 | gotAll &= SUCCEEDED(rc);
|
---|
208 |
|
---|
209 | ComPtr<IVirtualBoxErrorInfo> next;
|
---|
210 | rc = info->COMGETTER(Next) (next.asOutParam());
|
---|
211 | if (SUCCEEDED(rc) && !next.isNull())
|
---|
212 | {
|
---|
213 | mNext.reset (new ErrorInfo (next));
|
---|
214 | Assert(mNext.get());
|
---|
215 | if (!mNext.get())
|
---|
216 | rc = E_OUTOFMEMORY;
|
---|
217 | }
|
---|
218 | else
|
---|
219 | mNext.reset();
|
---|
220 | gotSomething |= SUCCEEDED(rc);
|
---|
221 | gotAll &= SUCCEEDED(rc);
|
---|
222 |
|
---|
223 | mIsBasicAvailable = gotSomething;
|
---|
224 | mIsFullAvailable = gotAll;
|
---|
225 |
|
---|
226 | AssertMsg (gotSomething, ("Nothing to fetch!\n"));
|
---|
227 | }
|
---|
228 |
|
---|
229 | ErrorInfo::~ErrorInfo()
|
---|
230 | {
|
---|
231 | }
|
---|
232 |
|
---|
233 | // ProgressErrorInfo class
|
---|
234 | ////////////////////////////////////////////////////////////////////////////////
|
---|
235 |
|
---|
236 | ProgressErrorInfo::ProgressErrorInfo (IProgress *progress) :
|
---|
237 | ErrorInfo (false /* aDummy */)
|
---|
238 | {
|
---|
239 | Assert(progress);
|
---|
240 | if (!progress)
|
---|
241 | return;
|
---|
242 |
|
---|
243 | ComPtr<IVirtualBoxErrorInfo> info;
|
---|
244 | HRESULT rc = progress->COMGETTER(ErrorInfo) (info.asOutParam());
|
---|
245 | if (SUCCEEDED(rc) && info)
|
---|
246 | init (info);
|
---|
247 | }
|
---|
248 |
|
---|
249 | // ErrorInfoKeeper class
|
---|
250 | ////////////////////////////////////////////////////////////////////////////////
|
---|
251 |
|
---|
252 | HRESULT ErrorInfoKeeper::restore()
|
---|
253 | {
|
---|
254 | if (mForgot)
|
---|
255 | return S_OK;
|
---|
256 |
|
---|
257 | HRESULT rc = S_OK;
|
---|
258 |
|
---|
259 | #if !defined (VBOX_WITH_XPCOM)
|
---|
260 |
|
---|
261 | ComPtr<IErrorInfo> err;
|
---|
262 | if (!mErrorInfo.isNull())
|
---|
263 | {
|
---|
264 | rc = mErrorInfo.queryInterfaceTo(err.asOutParam());
|
---|
265 | AssertComRC (rc);
|
---|
266 | }
|
---|
267 | rc = ::SetErrorInfo (0, err);
|
---|
268 |
|
---|
269 | #else // !defined (VBOX_WITH_XPCOM)
|
---|
270 |
|
---|
271 | nsCOMPtr <nsIExceptionService> es;
|
---|
272 | es = do_GetService (NS_EXCEPTIONSERVICE_CONTRACTID, &rc);
|
---|
273 | if (NS_SUCCEEDED(rc))
|
---|
274 | {
|
---|
275 | nsCOMPtr <nsIExceptionManager> em;
|
---|
276 | rc = es->GetCurrentExceptionManager (getter_AddRefs (em));
|
---|
277 | if (NS_SUCCEEDED(rc))
|
---|
278 | {
|
---|
279 | ComPtr<nsIException> ex;
|
---|
280 | if (!mErrorInfo.isNull())
|
---|
281 | {
|
---|
282 | rc = mErrorInfo.queryInterfaceTo(ex.asOutParam());
|
---|
283 | AssertComRC (rc);
|
---|
284 | }
|
---|
285 | rc = em->SetCurrentException (ex);
|
---|
286 | }
|
---|
287 | }
|
---|
288 |
|
---|
289 | #endif // !defined (VBOX_WITH_XPCOM)
|
---|
290 |
|
---|
291 | if (SUCCEEDED(rc))
|
---|
292 | {
|
---|
293 | mErrorInfo.setNull();
|
---|
294 | mForgot = true;
|
---|
295 | }
|
---|
296 |
|
---|
297 | return rc;
|
---|
298 | }
|
---|
299 |
|
---|
300 | } /* namespace com */
|
---|
301 |
|
---|