VirtualBox

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

Last change on this file since 17467 was 16555, checked in by vboxsync, 16 years ago

export

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