VirtualBox

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

最後變更 在這個檔案從30714是 30714,由 vboxsync 提交於 15 年 前

Main: remove SupportErrorInfo template magic

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

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