1 | /* $Id: ErrorInfo.cpp 26186 2010-02-03 13:07:12Z 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 | Utf8Str message;
|
---|
120 | rc = ex->GetMessage(message.asOutParam());
|
---|
121 | message.jolt();
|
---|
122 | gotSomething |= NS_SUCCEEDED(rc);
|
---|
123 | if (NS_SUCCEEDED(rc))
|
---|
124 | mText = message;
|
---|
125 |
|
---|
126 | if (gotSomething)
|
---|
127 | mIsBasicAvailable = true;
|
---|
128 |
|
---|
129 | AssertMsg (gotSomething, ("Nothing to fetch!\n"));
|
---|
130 | }
|
---|
131 |
|
---|
132 | // set the exception to NULL (to emulate Win32 behavior)
|
---|
133 | em->SetCurrentException (NULL);
|
---|
134 |
|
---|
135 | rc = NS_OK;
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | AssertComRC (rc);
|
---|
141 |
|
---|
142 | #endif // !defined (VBOX_WITH_XPCOM)
|
---|
143 | }
|
---|
144 |
|
---|
145 | void ErrorInfo::init (IUnknown *aI, const GUID &aIID, bool aKeepObj /* = false */)
|
---|
146 | {
|
---|
147 | Assert(aI);
|
---|
148 | if (!aI)
|
---|
149 | return;
|
---|
150 |
|
---|
151 | #if !defined (VBOX_WITH_XPCOM)
|
---|
152 |
|
---|
153 | ComPtr<IUnknown> iface = aI;
|
---|
154 | ComPtr<ISupportErrorInfo> serr;
|
---|
155 | HRESULT rc = iface.queryInterfaceTo(serr.asOutParam());
|
---|
156 | if (SUCCEEDED(rc))
|
---|
157 | {
|
---|
158 | rc = serr->InterfaceSupportsErrorInfo (aIID);
|
---|
159 | if (SUCCEEDED(rc))
|
---|
160 | init (aKeepObj);
|
---|
161 | }
|
---|
162 |
|
---|
163 | #else
|
---|
164 |
|
---|
165 | init (aKeepObj);
|
---|
166 |
|
---|
167 | #endif
|
---|
168 |
|
---|
169 | if (mIsBasicAvailable)
|
---|
170 | {
|
---|
171 | mCalleeIID = aIID;
|
---|
172 | GetInterfaceNameByIID (aIID, mCalleeName.asOutParam());
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | void ErrorInfo::init (IVirtualBoxErrorInfo *info)
|
---|
177 | {
|
---|
178 | AssertReturnVoid (info);
|
---|
179 |
|
---|
180 | HRESULT rc = E_FAIL;
|
---|
181 | bool gotSomething = false;
|
---|
182 | bool gotAll = true;
|
---|
183 | LONG lrc;
|
---|
184 |
|
---|
185 | rc = info->COMGETTER(ResultCode) (&lrc); mResultCode = lrc;
|
---|
186 | gotSomething |= SUCCEEDED(rc);
|
---|
187 | gotAll &= SUCCEEDED(rc);
|
---|
188 |
|
---|
189 | Bstr iid;
|
---|
190 | rc = info->COMGETTER(InterfaceID) (iid.asOutParam());
|
---|
191 | gotSomething |= SUCCEEDED(rc);
|
---|
192 | gotAll &= SUCCEEDED(rc);
|
---|
193 | if (SUCCEEDED(rc))
|
---|
194 | {
|
---|
195 | mInterfaceID = iid;
|
---|
196 | GetInterfaceNameByIID (mInterfaceID, mInterfaceName.asOutParam());
|
---|
197 | }
|
---|
198 |
|
---|
199 | rc = info->COMGETTER(Component) (mComponent.asOutParam());
|
---|
200 | gotSomething |= SUCCEEDED(rc);
|
---|
201 | gotAll &= SUCCEEDED(rc);
|
---|
202 |
|
---|
203 | rc = info->COMGETTER(Text) (mText.asOutParam());
|
---|
204 | gotSomething |= SUCCEEDED(rc);
|
---|
205 | gotAll &= SUCCEEDED(rc);
|
---|
206 |
|
---|
207 | ComPtr<IVirtualBoxErrorInfo> next;
|
---|
208 | rc = info->COMGETTER(Next) (next.asOutParam());
|
---|
209 | if (SUCCEEDED(rc) && !next.isNull())
|
---|
210 | {
|
---|
211 | mNext.reset (new ErrorInfo (next));
|
---|
212 | Assert(mNext.get());
|
---|
213 | if (!mNext.get())
|
---|
214 | rc = E_OUTOFMEMORY;
|
---|
215 | }
|
---|
216 | else
|
---|
217 | mNext.reset();
|
---|
218 | gotSomething |= SUCCEEDED(rc);
|
---|
219 | gotAll &= SUCCEEDED(rc);
|
---|
220 |
|
---|
221 | mIsBasicAvailable = gotSomething;
|
---|
222 | mIsFullAvailable = gotAll;
|
---|
223 |
|
---|
224 | AssertMsg (gotSomething, ("Nothing to fetch!\n"));
|
---|
225 | }
|
---|
226 |
|
---|
227 | ErrorInfo::~ErrorInfo()
|
---|
228 | {
|
---|
229 | }
|
---|
230 |
|
---|
231 | // ProgressErrorInfo class
|
---|
232 | ////////////////////////////////////////////////////////////////////////////////
|
---|
233 |
|
---|
234 | ProgressErrorInfo::ProgressErrorInfo (IProgress *progress) :
|
---|
235 | ErrorInfo (false /* aDummy */)
|
---|
236 | {
|
---|
237 | Assert(progress);
|
---|
238 | if (!progress)
|
---|
239 | return;
|
---|
240 |
|
---|
241 | ComPtr<IVirtualBoxErrorInfo> info;
|
---|
242 | HRESULT rc = progress->COMGETTER(ErrorInfo) (info.asOutParam());
|
---|
243 | if (SUCCEEDED(rc) && info)
|
---|
244 | init (info);
|
---|
245 | }
|
---|
246 |
|
---|
247 | // ErrorInfoKeeper class
|
---|
248 | ////////////////////////////////////////////////////////////////////////////////
|
---|
249 |
|
---|
250 | HRESULT ErrorInfoKeeper::restore()
|
---|
251 | {
|
---|
252 | if (mForgot)
|
---|
253 | return S_OK;
|
---|
254 |
|
---|
255 | HRESULT rc = S_OK;
|
---|
256 |
|
---|
257 | #if !defined (VBOX_WITH_XPCOM)
|
---|
258 |
|
---|
259 | ComPtr<IErrorInfo> err;
|
---|
260 | if (!mErrorInfo.isNull())
|
---|
261 | {
|
---|
262 | rc = mErrorInfo.queryInterfaceTo(err.asOutParam());
|
---|
263 | AssertComRC (rc);
|
---|
264 | }
|
---|
265 | rc = ::SetErrorInfo (0, err);
|
---|
266 |
|
---|
267 | #else // !defined (VBOX_WITH_XPCOM)
|
---|
268 |
|
---|
269 | nsCOMPtr <nsIExceptionService> es;
|
---|
270 | es = do_GetService (NS_EXCEPTIONSERVICE_CONTRACTID, &rc);
|
---|
271 | if (NS_SUCCEEDED(rc))
|
---|
272 | {
|
---|
273 | nsCOMPtr <nsIExceptionManager> em;
|
---|
274 | rc = es->GetCurrentExceptionManager (getter_AddRefs (em));
|
---|
275 | if (NS_SUCCEEDED(rc))
|
---|
276 | {
|
---|
277 | ComPtr<nsIException> ex;
|
---|
278 | if (!mErrorInfo.isNull())
|
---|
279 | {
|
---|
280 | rc = mErrorInfo.queryInterfaceTo(ex.asOutParam());
|
---|
281 | AssertComRC (rc);
|
---|
282 | }
|
---|
283 | rc = em->SetCurrentException (ex);
|
---|
284 | }
|
---|
285 | }
|
---|
286 |
|
---|
287 | #endif // !defined (VBOX_WITH_XPCOM)
|
---|
288 |
|
---|
289 | if (SUCCEEDED(rc))
|
---|
290 | {
|
---|
291 | mErrorInfo.setNull();
|
---|
292 | mForgot = true;
|
---|
293 | }
|
---|
294 |
|
---|
295 | return rc;
|
---|
296 | }
|
---|
297 |
|
---|
298 | } /* namespace com */
|
---|
299 |
|
---|