VirtualBox

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

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

Main: move libxml2 to IPRT unconditionally (remove VBOX_WITH_LIBXML2_IN_VBOXRT); move xml classes to IPRT; introduce IPRT ministring class as base for both Utf8Str and xml.cpp, with better performance; introduce some Utf8Str helpers to avoid string buffer hacks in Main code; remove std::auto_ptr<> from some headers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.4 KB
Line 
1/* $Id: ErrorInfo.cpp 21079 2009-06-30 15:59:22Z 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
44namespace com
45{
46
47// ErrorInfo class
48////////////////////////////////////////////////////////////////////////////////
49
50void 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
145void 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
176void 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 rc = info->COMGETTER(InterfaceID) (mInterfaceID.asOutParam());
190 gotSomething |= SUCCEEDED (rc);
191 gotAll &= SUCCEEDED (rc);
192 if (SUCCEEDED (rc))
193 GetInterfaceNameByIID (mInterfaceID, mInterfaceName.asOutParam());
194
195 rc = info->COMGETTER(Component) (mComponent.asOutParam());
196 gotSomething |= SUCCEEDED (rc);
197 gotAll &= SUCCEEDED (rc);
198
199 rc = info->COMGETTER(Text) (mText.asOutParam());
200 gotSomething |= SUCCEEDED (rc);
201 gotAll &= SUCCEEDED (rc);
202
203 ComPtr <IVirtualBoxErrorInfo> next;
204 rc = info->COMGETTER(Next) (next.asOutParam());
205 if (SUCCEEDED (rc) && !next.isNull())
206 {
207 mNext.reset (new ErrorInfo (next));
208 Assert (mNext.get());
209 if (!mNext.get())
210 rc = E_OUTOFMEMORY;
211 }
212 else
213 mNext.reset();
214 gotSomething |= SUCCEEDED (rc);
215 gotAll &= SUCCEEDED (rc);
216
217 mIsBasicAvailable = gotSomething;
218 mIsFullAvailable = gotAll;
219
220 AssertMsg (gotSomething, ("Nothing to fetch!\n"));
221}
222
223ErrorInfo::~ErrorInfo()
224{
225}
226
227// ProgressErrorInfo class
228////////////////////////////////////////////////////////////////////////////////
229
230ProgressErrorInfo::ProgressErrorInfo (IProgress *progress) :
231 ErrorInfo (false /* aDummy */)
232{
233 Assert (progress);
234 if (!progress)
235 return;
236
237 ComPtr <IVirtualBoxErrorInfo> info;
238 HRESULT rc = progress->COMGETTER(ErrorInfo) (info.asOutParam());
239 if (SUCCEEDED (rc) && info)
240 init (info);
241}
242
243// ErrorInfoKeeper class
244////////////////////////////////////////////////////////////////////////////////
245
246HRESULT ErrorInfoKeeper::restore()
247{
248 if (mForgot)
249 return S_OK;
250
251 HRESULT rc = S_OK;
252
253#if !defined (VBOX_WITH_XPCOM)
254
255 ComPtr <IErrorInfo> err;
256 if (!mErrorInfo.isNull())
257 {
258 rc = mErrorInfo.queryInterfaceTo (err.asOutParam());
259 AssertComRC (rc);
260 }
261 rc = ::SetErrorInfo (0, err);
262
263#else // !defined (VBOX_WITH_XPCOM)
264
265 nsCOMPtr <nsIExceptionService> es;
266 es = do_GetService (NS_EXCEPTIONSERVICE_CONTRACTID, &rc);
267 if (NS_SUCCEEDED (rc))
268 {
269 nsCOMPtr <nsIExceptionManager> em;
270 rc = es->GetCurrentExceptionManager (getter_AddRefs (em));
271 if (NS_SUCCEEDED (rc))
272 {
273 ComPtr <nsIException> ex;
274 if (!mErrorInfo.isNull())
275 {
276 rc = mErrorInfo.queryInterfaceTo (ex.asOutParam());
277 AssertComRC (rc);
278 }
279 rc = em->SetCurrentException (ex);
280 }
281 }
282
283#endif // !defined (VBOX_WITH_XPCOM)
284
285 if (SUCCEEDED (rc))
286 {
287 mErrorInfo.setNull();
288 mForgot = true;
289 }
290
291 return rc;
292}
293
294} /* namespace com */
295
Note: See TracBrowser for help on using the repository browser.

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