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