1 | /* $Id: MediumImpl.h 13580 2008-10-27 14:04:18Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * VirtualBox COM class implementation
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2008 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
19 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
20 | * additional information or have any questions.
|
---|
21 | */
|
---|
22 |
|
---|
23 | #ifndef ____H_MEDIUMIMPL
|
---|
24 | #define ____H_MEDIUMIMPL
|
---|
25 |
|
---|
26 | #include "VirtualBoxBase.h"
|
---|
27 |
|
---|
28 | #include <VBox/com/SupportErrorInfo.h>
|
---|
29 |
|
---|
30 | #include <list>
|
---|
31 | #include <algorithm>
|
---|
32 |
|
---|
33 | class VirtualBox;
|
---|
34 |
|
---|
35 | ////////////////////////////////////////////////////////////////////////////////
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Base component class for all media types.
|
---|
39 | *
|
---|
40 | * Provides the basic implementation of the IMedium interface.
|
---|
41 | *
|
---|
42 | * @note Subclasses must initialize the mVirtualBox data member in their init()
|
---|
43 | * implementations with the valid VirtualBox instance because some
|
---|
44 | * MediaBase methods call its methods.
|
---|
45 | */
|
---|
46 | class ATL_NO_VTABLE MediumBase :
|
---|
47 | virtual public VirtualBoxBaseProto,
|
---|
48 | public com::SupportErrorInfoBase,
|
---|
49 | public VirtualBoxSupportTranslation <MediumBase>,
|
---|
50 | public IMedium
|
---|
51 | {
|
---|
52 | public:
|
---|
53 |
|
---|
54 | VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT (MediumBase)
|
---|
55 |
|
---|
56 | DECLARE_EMPTY_CTOR_DTOR (MediumBase)
|
---|
57 |
|
---|
58 | /** Describes how a machine refers to this image. */
|
---|
59 | struct BackRef
|
---|
60 | {
|
---|
61 | /** Equality predicate for stdc++. */
|
---|
62 | struct EqualsTo : public std::unary_function <BackRef, bool>
|
---|
63 | {
|
---|
64 | explicit EqualsTo (const Guid &aMachineId) : machineId (aMachineId) {}
|
---|
65 |
|
---|
66 | bool operator() (const argument_type &aThat) const
|
---|
67 | {
|
---|
68 | return aThat.machineId == machineId;
|
---|
69 | }
|
---|
70 |
|
---|
71 | const Guid machineId;
|
---|
72 | };
|
---|
73 |
|
---|
74 | typedef std::list <Guid> GuidList;
|
---|
75 |
|
---|
76 | BackRef() : inCurState (false) {}
|
---|
77 |
|
---|
78 | BackRef (const Guid &aMachineId, const Guid &aSnapshotId = Guid::Empty)
|
---|
79 | : machineId (aMachineId)
|
---|
80 | , inCurState (aSnapshotId.isEmpty())
|
---|
81 | {
|
---|
82 | if (!aSnapshotId.isEmpty())
|
---|
83 | snapshotIds.push_back (aSnapshotId);
|
---|
84 | }
|
---|
85 |
|
---|
86 | Guid machineId;
|
---|
87 | bool inCurState : 1;
|
---|
88 | GuidList snapshotIds;
|
---|
89 | };
|
---|
90 |
|
---|
91 | typedef std::list <BackRef> BackRefList;
|
---|
92 |
|
---|
93 | // IMedium properties
|
---|
94 | STDMETHOD(COMGETTER(Id)) (GUIDPARAMOUT aId);
|
---|
95 | STDMETHOD(COMGETTER(Description)) (BSTR *aDescription);
|
---|
96 | STDMETHOD(COMSETTER(Description)) (INPTR BSTR aDescription);
|
---|
97 | STDMETHOD(COMGETTER(State)) (MediaState_T *aState);
|
---|
98 | STDMETHOD(COMGETTER(Location)) (BSTR *aLocation);
|
---|
99 | STDMETHOD(COMSETTER(Location)) (INPTR BSTR aLocation);
|
---|
100 | STDMETHOD(COMGETTER(Name)) (BSTR *aName);
|
---|
101 | STDMETHOD(COMGETTER(Size)) (ULONG64 *aSize);
|
---|
102 | STDMETHOD(COMGETTER(LastAccessError)) (BSTR *aLastAccessError);
|
---|
103 | STDMETHOD(COMGETTER(MachineIds)) (ComSafeGUIDArrayOut (aMachineIds));
|
---|
104 |
|
---|
105 | // IMedium methods
|
---|
106 | STDMETHOD(GetSnapshotIds) (INPTR GUIDPARAM aMachineId,
|
---|
107 | ComSafeGUIDArrayOut (aSnapshotIds));
|
---|
108 | STDMETHOD(LockRead) (MediaState_T *aState);
|
---|
109 | STDMETHOD(UnlockRead) (MediaState_T *aState);
|
---|
110 | STDMETHOD(LockWrite) (MediaState_T *aState);
|
---|
111 | STDMETHOD(UnlockWrite) (MediaState_T *aState);
|
---|
112 | STDMETHOD(Close)();
|
---|
113 |
|
---|
114 | // public methods for internal purposes only
|
---|
115 |
|
---|
116 | HRESULT updatePath (const char *aOldPath, const char *aNewPath);
|
---|
117 |
|
---|
118 | HRESULT attachTo (const Guid &aMachineId,
|
---|
119 | const Guid &aSnapshotId = Guid::Empty);
|
---|
120 | HRESULT detachFrom (const Guid &aMachineId,
|
---|
121 | const Guid &aSnapshotId = Guid::Empty);
|
---|
122 |
|
---|
123 | // unsafe inline public methods for internal purposes only (ensure there is
|
---|
124 | // a caller and a read lock before calling them!)
|
---|
125 |
|
---|
126 | const Guid &id() const { return m.id; }
|
---|
127 | MediaState_T state() const { return m.state; }
|
---|
128 | const Bstr &location() const { return m.location; }
|
---|
129 | const Bstr &locationFull() const { return m.locationFull; }
|
---|
130 | const BackRefList &backRefs() const { return m.backRefs; }
|
---|
131 |
|
---|
132 | bool isAttachedTo (const Guid &aMachineId)
|
---|
133 | {
|
---|
134 | BackRefList::iterator it =
|
---|
135 | std::find_if (m.backRefs.begin(), m.backRefs.end(),
|
---|
136 | BackRef::EqualsTo (aMachineId));
|
---|
137 | return it != m.backRefs.end() && it->inCurState;
|
---|
138 | }
|
---|
139 |
|
---|
140 | protected:
|
---|
141 |
|
---|
142 | virtual Utf8Str name();
|
---|
143 |
|
---|
144 | virtual HRESULT setLocation (const BSTR aLocation);
|
---|
145 | virtual HRESULT queryInfo();
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Performs extra checks if the medium can be closed and returns S_OK in
|
---|
149 | * this case. Otherwise, returns a respective error message. Called by
|
---|
150 | * Close() from within this object's AutoMayUninitSpan and from under
|
---|
151 | * mVirtualBox write lock.
|
---|
152 | */
|
---|
153 | virtual HRESULT canClose() { return S_OK; }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Performs extra checks if the medium can be attached to the specified
|
---|
157 | * VM and shapshot at the given time and returns S_OK in this case.
|
---|
158 | * Otherwise, returns a respective error message. Called by attachTo() from
|
---|
159 | * within this object's AutoWriteLock.
|
---|
160 | */
|
---|
161 | virtual HRESULT canAttach (const Guid &aMachineId,
|
---|
162 | const Guid &aSnapshotId)
|
---|
163 | { return S_OK; }
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Unregisters this medium with mVirtualBox. Called by Close() from within
|
---|
167 | * this object's AutoMayUninitSpan and from under mVirtualBox write lock.
|
---|
168 | */
|
---|
169 | virtual HRESULT unregisterWithVirtualBox() = 0;
|
---|
170 |
|
---|
171 | HRESULT setStateError();
|
---|
172 |
|
---|
173 | /** weak VirualBox parent */
|
---|
174 | const ComObjPtr <VirtualBox, ComWeakRef> mVirtualBox;
|
---|
175 |
|
---|
176 | struct Data
|
---|
177 | {
|
---|
178 | Data() : state (MediaState_NotCreated), size (0), readers (0)
|
---|
179 | , queryInfoSem (NIL_RTSEMEVENTMULTI)
|
---|
180 | , queryInfoCallers (0), accessibleInLock (false) {}
|
---|
181 |
|
---|
182 | const Guid id;
|
---|
183 | Bstr description;
|
---|
184 | MediaState_T state;
|
---|
185 | Bstr location;
|
---|
186 | Bstr locationFull;
|
---|
187 | uint64_t size;
|
---|
188 | Bstr lastAccessError;
|
---|
189 |
|
---|
190 | BackRefList backRefs;
|
---|
191 |
|
---|
192 | size_t readers;
|
---|
193 |
|
---|
194 | RTSEMEVENTMULTI queryInfoSem;
|
---|
195 | size_t queryInfoCallers;
|
---|
196 |
|
---|
197 | bool accessibleInLock : 1;
|
---|
198 | };
|
---|
199 |
|
---|
200 | Data m;
|
---|
201 | };
|
---|
202 |
|
---|
203 | ////////////////////////////////////////////////////////////////////////////////
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Base component class for simple image file based media such as CD/DVD ISO
|
---|
207 | * images or Floppy images.
|
---|
208 | *
|
---|
209 | * Adds specific protectedInit() and saveSettings() methods that can load image
|
---|
210 | * data from the settings files.
|
---|
211 | */
|
---|
212 | class ATL_NO_VTABLE ImageMediumBase
|
---|
213 | : public MediumBase
|
---|
214 | , public VirtualBoxBaseNEXT
|
---|
215 | {
|
---|
216 | public:
|
---|
217 |
|
---|
218 | HRESULT FinalConstruct() { return S_OK; }
|
---|
219 | void FinalRelease() { uninit(); }
|
---|
220 |
|
---|
221 | protected:
|
---|
222 |
|
---|
223 | // protected initializer/uninitializer for internal purposes only
|
---|
224 | HRESULT protectedInit (VirtualBox *aVirtualBox, const BSTR aLocation,
|
---|
225 | const Guid &aId);
|
---|
226 | HRESULT protectedInit (VirtualBox *aVirtualBox, const settings::Key &aImageNode);
|
---|
227 | void protectedUninit();
|
---|
228 |
|
---|
229 | public:
|
---|
230 |
|
---|
231 | // public methods for internal purposes only
|
---|
232 | HRESULT saveSettings (settings::Key &aImagesNode);
|
---|
233 | };
|
---|
234 |
|
---|
235 | ////////////////////////////////////////////////////////////////////////////////
|
---|
236 |
|
---|
237 | /**
|
---|
238 | * The DVDImage2 component class implements the IDVDImage2 interface.
|
---|
239 | */
|
---|
240 | class ATL_NO_VTABLE DVDImage2
|
---|
241 | : public com::SupportErrorInfoDerived <ImageMediumBase, DVDImage2, IDVDImage2>
|
---|
242 | , public VirtualBoxSupportTranslation <DVDImage2>
|
---|
243 | , public IDVDImage2
|
---|
244 | {
|
---|
245 | public:
|
---|
246 |
|
---|
247 | COM_FORWARD_IMedium_TO_BASE (ImageMediumBase)
|
---|
248 |
|
---|
249 | VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE (DVDImage2)
|
---|
250 |
|
---|
251 | DECLARE_NOT_AGGREGATABLE (DVDImage2)
|
---|
252 |
|
---|
253 | DECLARE_PROTECT_FINAL_CONSTRUCT()
|
---|
254 |
|
---|
255 | BEGIN_COM_MAP (DVDImage2)
|
---|
256 | COM_INTERFACE_ENTRY (ISupportErrorInfo)
|
---|
257 | COM_INTERFACE_ENTRY2 (IMedium, ImageMediumBase)
|
---|
258 | COM_INTERFACE_ENTRY (IDVDImage2)
|
---|
259 | END_COM_MAP()
|
---|
260 |
|
---|
261 | NS_DECL_ISUPPORTS
|
---|
262 |
|
---|
263 | DECLARE_EMPTY_CTOR_DTOR (DVDImage2)
|
---|
264 |
|
---|
265 | // public initializer/uninitializer for internal purposes only
|
---|
266 |
|
---|
267 | HRESULT init (VirtualBox *aParent, const BSTR aFilePath,
|
---|
268 | const Guid &aId)
|
---|
269 | {
|
---|
270 | return protectedInit (aParent, aFilePath, aId);
|
---|
271 | }
|
---|
272 |
|
---|
273 | HRESULT init (VirtualBox *aParent, const settings::Key &aImageNode)
|
---|
274 | {
|
---|
275 | return protectedInit (aParent, aImageNode);
|
---|
276 | }
|
---|
277 |
|
---|
278 | void uninit() { protectedUninit(); }
|
---|
279 |
|
---|
280 | /** For com::SupportErrorInfoImpl. */
|
---|
281 | static const char *ComponentName() { return "DVDImage2"; }
|
---|
282 |
|
---|
283 | private:
|
---|
284 |
|
---|
285 | HRESULT unregisterWithVirtualBox();
|
---|
286 | };
|
---|
287 |
|
---|
288 | ////////////////////////////////////////////////////////////////////////////////
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * The FloppyImage2 component class implements the IFloppyImage2 interface.
|
---|
292 | */
|
---|
293 | class ATL_NO_VTABLE FloppyImage2
|
---|
294 | : public com::SupportErrorInfoDerived <ImageMediumBase, FloppyImage2, IFloppyImage2>
|
---|
295 | , public VirtualBoxSupportTranslation <FloppyImage2>
|
---|
296 | , public IFloppyImage2
|
---|
297 | {
|
---|
298 | public:
|
---|
299 |
|
---|
300 | COM_FORWARD_IMedium_TO_BASE (ImageMediumBase)
|
---|
301 |
|
---|
302 | VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE (FloppyImage2)
|
---|
303 |
|
---|
304 | DECLARE_NOT_AGGREGATABLE (FloppyImage2)
|
---|
305 |
|
---|
306 | DECLARE_PROTECT_FINAL_CONSTRUCT()
|
---|
307 |
|
---|
308 | BEGIN_COM_MAP (FloppyImage2)
|
---|
309 | COM_INTERFACE_ENTRY (ISupportErrorInfo)
|
---|
310 | COM_INTERFACE_ENTRY2 (IMedium, ImageMediumBase)
|
---|
311 | COM_INTERFACE_ENTRY (IFloppyImage2)
|
---|
312 | END_COM_MAP()
|
---|
313 |
|
---|
314 | NS_DECL_ISUPPORTS
|
---|
315 |
|
---|
316 | DECLARE_EMPTY_CTOR_DTOR (FloppyImage2)
|
---|
317 |
|
---|
318 | // public initializer/uninitializer for internal purposes only
|
---|
319 |
|
---|
320 | HRESULT init (VirtualBox *aParent, const BSTR aFilePath,
|
---|
321 | const Guid &aId)
|
---|
322 | {
|
---|
323 | return protectedInit (aParent, aFilePath, aId);
|
---|
324 | }
|
---|
325 |
|
---|
326 | HRESULT init (VirtualBox *aParent, const settings::Key &aImageNode)
|
---|
327 | {
|
---|
328 | return protectedInit (aParent, aImageNode);
|
---|
329 | }
|
---|
330 |
|
---|
331 | void uninit() { protectedUninit(); }
|
---|
332 |
|
---|
333 | /** For com::SupportErrorInfoImpl. */
|
---|
334 | static const char *ComponentName() { return "FloppyImage2"; }
|
---|
335 |
|
---|
336 | private:
|
---|
337 |
|
---|
338 | HRESULT unregisterWithVirtualBox();
|
---|
339 | };
|
---|
340 |
|
---|
341 | #endif /* ____H_MEDIUMIMPL */
|
---|
342 |
|
---|