VirtualBox

source: vbox/trunk/src/VBox/Main/MediumFormatImpl.cpp@ 32694

Last change on this file since 32694 was 32682, checked in by vboxsync, 14 years ago

Main: fixed incorrect parameters checking in MediumFormat implementation

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 KB
Line 
1/* $Id: MediumFormatImpl.cpp 32682 2010-09-22 06:59:46Z vboxsync $ */
2
3/** @file
4 *
5 * VirtualBox COM class implementation
6 */
7
8/*
9 * Copyright (C) 2008-2010 Oracle Corporation
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
20#include "MediumFormatImpl.h"
21#include "AutoCaller.h"
22#include "Logging.h"
23
24#include <VBox/VBoxHDD.h>
25
26#include <iprt/cpp/utils.h>
27
28// constructor / destructor
29/////////////////////////////////////////////////////////////////////////////
30
31DEFINE_EMPTY_CTOR_DTOR(MediumFormat)
32
33HRESULT MediumFormat::FinalConstruct()
34{
35 return S_OK;
36}
37
38void MediumFormat::FinalRelease()
39{
40 uninit();
41}
42
43// public initializer/uninitializer for internal purposes only
44/////////////////////////////////////////////////////////////////////////////
45
46/**
47 * Initializes the hard disk format object.
48 *
49 * @param aVDInfo Pointer to a backend info object.
50 */
51HRESULT MediumFormat::init(const VDBACKENDINFO *aVDInfo)
52{
53 LogFlowThisFunc(("aVDInfo=%p\n", aVDInfo));
54
55 ComAssertRet(aVDInfo, E_INVALIDARG);
56
57 /* Enclose the state transition NotReady->InInit->Ready */
58 AutoInitSpan autoInitSpan(this);
59 AssertReturn(autoInitSpan.isOk(), E_FAIL);
60
61 /* The ID of the backend */
62 unconst(m.strId) = aVDInfo->pszBackend;
63 /* The Name of the backend */
64 /* Use id for now as long as VDBACKENDINFO hasn't any extra
65 * name/description field. */
66 unconst(m.strName) = aVDInfo->pszBackend;
67 /* The capabilities of the backend */
68 unconst(m.capabilities) = aVDInfo->uBackendCaps;
69 /* Save the supported file extensions in a list */
70 if (aVDInfo->papszFileExtensions)
71 {
72 const char *const *papsz = aVDInfo->papszFileExtensions;
73 while (*papsz != NULL)
74 {
75 unconst(m.llFileExtensions).push_back(*papsz);
76 ++papsz;
77 }
78 }
79 /* Save a list of configure properties */
80 if (aVDInfo->paConfigInfo)
81 {
82 PCVDCONFIGINFO pa = aVDInfo->paConfigInfo;
83 /* Walk through all available keys */
84 while (pa->pszKey != NULL)
85 {
86 Utf8Str defaultValue("");
87 DataType_T dt;
88 ULONG flags = static_cast <ULONG>(pa->uKeyFlags);
89 /* Check for the configure data type */
90 switch (pa->enmValueType)
91 {
92 case VDCFGVALUETYPE_INTEGER:
93 {
94 dt = DataType_Int32;
95 /* If there is a default value get them in the right format */
96 if (pa->pszDefaultValue)
97 defaultValue = pa->pszDefaultValue;
98 break;
99 }
100 case VDCFGVALUETYPE_BYTES:
101 {
102 dt = DataType_Int8;
103 /* If there is a default value get them in the right format */
104 if (pa->pszDefaultValue)
105 {
106 /* Copy the bytes over - treated simply as a string */
107 defaultValue = pa->pszDefaultValue;
108 flags |= DataFlags_Array;
109 }
110 break;
111 }
112 case VDCFGVALUETYPE_STRING:
113 {
114 dt = DataType_String;
115 /* If there is a default value get them in the right format */
116 if (pa->pszDefaultValue)
117 defaultValue = pa->pszDefaultValue;
118 break;
119 }
120
121 default:
122 AssertMsgFailed(("Invalid enm type %d!\n", pa->enmValueType));
123 return E_INVALIDARG;
124 }
125
126 /// @todo add extendedFlags to Property when we reach the 32 bit
127 /// limit (or make the argument ULONG64 after checking that COM is
128 /// capable of defining enums (used to represent bit flags) that
129 /// contain 64-bit values)
130 ComAssertRet(pa->uKeyFlags == ((ULONG)pa->uKeyFlags), E_FAIL);
131
132 /* Create one property structure */
133 const Property prop = { Utf8Str(pa->pszKey),
134 Utf8Str(""),
135 dt,
136 flags,
137 defaultValue };
138 unconst(m.llProperties).push_back(prop);
139 ++pa;
140 }
141 }
142
143 /* Confirm a successful initialization */
144 autoInitSpan.setSucceeded();
145
146 return S_OK;
147}
148
149/**
150 * Uninitializes the instance and sets the ready flag to FALSE.
151 * Called either from FinalRelease() or by the parent when it gets destroyed.
152 */
153void MediumFormat::uninit()
154{
155 LogFlowThisFunc(("\n"));
156
157 /* Enclose the state transition Ready->InUninit->NotReady */
158 AutoUninitSpan autoUninitSpan(this);
159 if (autoUninitSpan.uninitDone())
160 return;
161
162 unconst(m.llProperties).clear();
163 unconst(m.llFileExtensions).clear();
164 unconst(m.capabilities) = 0;
165 unconst(m.strName).setNull();
166 unconst(m.strId).setNull();
167}
168
169// IMediumFormat properties
170/////////////////////////////////////////////////////////////////////////////
171
172STDMETHODIMP MediumFormat::COMGETTER(Id)(BSTR *aId)
173{
174 CheckComArgOutPointerValid(aId);
175
176 AutoCaller autoCaller(this);
177 if (FAILED(autoCaller.rc())) return autoCaller.rc();
178
179 /* this is const, no need to lock */
180 m.strId.cloneTo(aId);
181
182 return S_OK;
183}
184
185STDMETHODIMP MediumFormat::COMGETTER(Name)(BSTR *aName)
186{
187 CheckComArgOutPointerValid(aName);
188
189 AutoCaller autoCaller(this);
190 if (FAILED(autoCaller.rc())) return autoCaller.rc();
191
192 /* this is const, no need to lock */
193 m.strName.cloneTo(aName);
194
195 return S_OK;
196}
197
198STDMETHODIMP MediumFormat::COMGETTER(FileExtensions)(ComSafeArrayOut(BSTR, aFileExtensions))
199{
200 CheckComArgOutSafeArrayPointerValid(aFileExtensions);
201
202 AutoCaller autoCaller(this);
203 if (FAILED(autoCaller.rc())) return autoCaller.rc();
204
205 /* this is const, no need to lock */
206 com::SafeArray<BSTR> fileExtentions(m.llFileExtensions.size());
207 int i = 0;
208 for (StrList::const_iterator it = m.llFileExtensions.begin();
209 it != m.llFileExtensions.end();
210 ++it, ++i)
211 (*it).cloneTo(&fileExtentions[i]);
212 fileExtentions.detachTo(ComSafeArrayOutArg(aFileExtensions));
213
214 return S_OK;
215}
216
217STDMETHODIMP MediumFormat::COMGETTER(Capabilities)(ULONG *aCaps)
218{
219 CheckComArgOutPointerValid(aCaps);
220
221 AutoCaller autoCaller(this);
222 if (FAILED(autoCaller.rc())) return autoCaller.rc();
223
224 /* m.capabilities is const, no need to lock */
225
226 /// @todo add COMGETTER(ExtendedCapabilities) when we reach the 32 bit
227 /// limit (or make the argument ULONG64 after checking that COM is capable
228 /// of defining enums (used to represent bit flags) that contain 64-bit
229 /// values)
230 ComAssertRet(m.capabilities == ((ULONG)m.capabilities), E_FAIL);
231
232 *aCaps = (ULONG) m.capabilities;
233
234 return S_OK;
235}
236
237STDMETHODIMP MediumFormat::DescribeProperties(ComSafeArrayOut(BSTR, aNames),
238 ComSafeArrayOut(BSTR, aDescriptions),
239 ComSafeArrayOut(DataType_T, aTypes),
240 ComSafeArrayOut(ULONG, aFlags),
241 ComSafeArrayOut(BSTR, aDefaults))
242{
243 CheckComArgOutSafeArrayPointerValid(aNames);
244 CheckComArgOutSafeArrayPointerValid(aDescriptions);
245 CheckComArgOutSafeArrayPointerValid(aTypes);
246 CheckComArgOutSafeArrayPointerValid(aFlags);
247 CheckComArgOutSafeArrayPointerValid(aDefaults);
248
249 AutoCaller autoCaller(this);
250 if (FAILED(autoCaller.rc())) return autoCaller.rc();
251
252 /* this is const, no need to lock */
253 size_t c = m.llProperties.size();
254 com::SafeArray<BSTR> propertyNames(c);
255 com::SafeArray<BSTR> propertyDescriptions(c);
256 com::SafeArray<DataType_T> propertyTypes(c);
257 com::SafeArray<ULONG> propertyFlags(c);
258 com::SafeArray<BSTR> propertyDefaults(c);
259
260 int i = 0;
261 for (PropertyList::const_iterator it = m.llProperties.begin();
262 it != m.llProperties.end();
263 ++it, ++i)
264 {
265 const Property &prop = (*it);
266 prop.strName.cloneTo(&propertyNames[i]);
267 prop.strDescription.cloneTo(&propertyDescriptions[i]);
268 propertyTypes[i] = prop.type;
269 propertyFlags[i] = prop.flags;
270 prop.strDefaultValue.cloneTo(&propertyDefaults[i]);
271 }
272
273 propertyNames.detachTo(ComSafeArrayOutArg(aNames));
274 propertyDescriptions.detachTo(ComSafeArrayOutArg(aDescriptions));
275 propertyTypes.detachTo(ComSafeArrayOutArg(aTypes));
276 propertyFlags.detachTo(ComSafeArrayOutArg(aFlags));
277 propertyDefaults.detachTo(ComSafeArrayOutArg(aDefaults));
278
279 return S_OK;
280}
281
282// IMediumFormat methods
283/////////////////////////////////////////////////////////////////////////////
284
285// public methods only for internal purposes
286/////////////////////////////////////////////////////////////////////////////
287/* vi: set tabstop=4 shiftwidth=4 expandtab: */
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