VirtualBox

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

Last change on this file since 33868 was 33567, checked in by vboxsync, 14 years ago

VD: Move the generic virtual disk framework + backends to src/VBox/Storage and rename the files to get rid of the HDD part because it supports floppy and DVD images too

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