1
/* $Id: DVDDriveImpl.cpp $ */
5
* VirtualBox COM class implementation
9
* Copyright (C) 2006-2009 Sun Microsystems, Inc.
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.
19
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
20
* Clara, CA 95054 USA or visit http://www.sun.com if you need
21
* additional information or have any questions.
24
#include "DVDDriveImpl.h"
26
#include "MachineImpl.h"
28
#include "HostDVDDriveImpl.h"
29
#include "VirtualBoxImpl.h"
35
#include <iprt/string.h>
36
#include <iprt/cpputils.h>
38
#include <VBox/settings.h>
40
// constructor / destructor
41
////////////////////////////////////////////////////////////////////////////////
43
DEFINE_EMPTY_CTOR_DTOR (DVDDrive)
45
HRESULT DVDDrive::FinalConstruct()
50
void DVDDrive::FinalRelease()
55
// public initializer/uninitializer for internal purposes only
56
////////////////////////////////////////////////////////////////////////////////
59
* Initializes the DVD drive object.
61
* @param aParent Handle of the parent object.
63
HRESULT DVDDrive::init (Machine *aParent)
65
LogFlowThisFunc (("aParent=%p\n", aParent));
67
ComAssertRet (aParent, E_INVALIDARG);
69
/* Enclose the state transition NotReady->InInit->Ready */
70
AutoInitSpan autoInitSpan (this);
71
AssertReturn (autoInitSpan.isOk(), E_FAIL);
73
unconst (mParent) = aParent;
74
/* mPeer is left null */
78
/* Confirm a successful initialization */
79
autoInitSpan.setSucceeded();
85
* Initializes the DVD drive object given another DVD drive object
86
* (a kind of copy constructor). This object shares data with
87
* the object passed as an argument.
89
* @note This object must be destroyed before the original object
90
* it shares data with is destroyed.
92
* @note Locks @a aThat object for reading.
94
HRESULT DVDDrive::init (Machine *aParent, DVDDrive *aThat)
96
LogFlowThisFunc (("aParent=%p, aThat=%p\n", aParent, aThat));
98
ComAssertRet (aParent && aThat, E_INVALIDARG);
100
/* Enclose the state transition NotReady->InInit->Ready */
101
AutoInitSpan autoInitSpan (this);
102
AssertReturn (autoInitSpan.isOk(), E_FAIL);
104
unconst (mParent) = aParent;
105
unconst (mPeer) = aThat;
107
AutoCaller thatCaller (aThat);
108
AssertComRCReturnRC (thatCaller.rc());
110
AutoReadLock thatLock (aThat);
113
/* Confirm a successful initialization */
114
autoInitSpan.setSucceeded();
120
* Initializes the DVD drive object given another DVD drive object
121
* (a kind of copy constructor). This object makes a private copy of data
122
* of the original object passed as an argument.
124
* @note Locks @a aThat object for reading.
126
HRESULT DVDDrive::initCopy (Machine *aParent, DVDDrive *aThat)
128
LogFlowThisFunc (("aParent=%p, aThat=%p\n", aParent, aThat));
130
ComAssertRet (aParent && aThat, E_INVALIDARG);
132
/* Enclose the state transition NotReady->InInit->Ready */
133
AutoInitSpan autoInitSpan (this);
134
AssertReturn (autoInitSpan.isOk(), E_FAIL);
136
unconst (mParent) = aParent;
137
/* mPeer is left null */
139
AutoCaller thatCaller (aThat);
140
AssertComRCReturnRC (thatCaller.rc());
142
AutoReadLock thatLock (aThat);
143
m.attachCopy (aThat->m);
145
/* at present, this must be a snapshot machine */
146
Assert (!aParent->snapshotId().isEmpty());
148
if (m->state == DriveState_ImageMounted)
150
/* associate the DVD image media with the snapshot */
151
HRESULT rc = m->image->attachTo (aParent->id(),
152
aParent->snapshotId());
156
/* Confirm a successful initialization */
157
autoInitSpan.setSucceeded();
163
* Uninitializes the instance and sets the ready flag to FALSE.
164
* Called either from FinalRelease() or by the parent when it gets destroyed.
166
void DVDDrive::uninit()
168
LogFlowThisFunc (("\n"));
170
/* Enclose the state transition Ready->InUninit->NotReady */
171
AutoUninitSpan autoUninitSpan (this);
172
if (autoUninitSpan.uninitDone())
175
if ((mParent->type() == Machine::IsMachine ||
176
mParent->type() == Machine::IsSnapshotMachine) &&
177
m->state == DriveState_ImageMounted)
179
/* Deassociate the DVD image (only when mParent is a real Machine or a
180
* SnapshotMachine instance; SessionMachine instances
181
* refer to real Machine hard disks). This is necessary for a clean
182
* re-initialization of the VM after successfully re-checking the
183
* accessibility state. */
184
HRESULT rc = m->image->detachFrom (mParent->id(),
185
mParent->snapshotId());
191
unconst (mPeer).setNull();
192
unconst (mParent).setNull();
195
// IDVDDrive properties
196
////////////////////////////////////////////////////////////////////////////////
198
STDMETHODIMP DVDDrive::COMGETTER(State) (DriveState_T *aState)
200
CheckComArgOutPointerValid(aState);
202
AutoCaller autoCaller (this);
203
CheckComRCReturnRC (autoCaller.rc());
205
AutoReadLock alock (this);
212
STDMETHODIMP DVDDrive::COMGETTER(Passthrough) (BOOL *aPassthrough)
214
CheckComArgOutPointerValid(aPassthrough);
216
AutoCaller autoCaller (this);
217
CheckComRCReturnRC (autoCaller.rc());
219
AutoReadLock alock (this);
221
*aPassthrough = m->passthrough;
226
STDMETHODIMP DVDDrive::COMSETTER(Passthrough) (BOOL aPassthrough)
228
AutoCaller autoCaller (this);
229
CheckComRCReturnRC (autoCaller.rc());
231
/* the machine needs to be mutable */
232
Machine::AutoMutableStateDependency adep (mParent);
233
CheckComRCReturnRC (adep.rc());
235
AutoWriteLock alock (this);
237
if (m->passthrough != aPassthrough)
240
m->passthrough = aPassthrough;
247
////////////////////////////////////////////////////////////////////////////////
249
STDMETHODIMP DVDDrive::MountImage (IN_BSTR aImageId)
251
Guid imageId(aImageId);
252
CheckComArgExpr(aImageId, !imageId.isEmpty());
254
AutoCaller autoCaller (this);
255
CheckComRCReturnRC (autoCaller.rc());
257
/* the machine needs to be mutable */
258
Machine::AutoMutableStateDependency adep (mParent);
259
CheckComRCReturnRC (adep.rc());
261
AutoWriteLock alock (this);
265
/* Our lifetime is bound to mParent's lifetime, so we don't add caller.
266
* We also don't lock mParent since its mParent field is const. */
268
ComObjPtr<DVDImage> image;
269
rc = mParent->virtualBox()->findDVDImage(&imageId, NULL,
270
true /* aSetError */, &image);
274
if (m->state != DriveState_ImageMounted ||
275
!m->image.equalsTo (image))
277
rc = image->attachTo (mParent->id(), mParent->snapshotId());
280
/* umount() will backup data */
285
/* lock the image for reading if the VM is online. It will
286
* be unlocked either when unmounted from this drive or by
287
* SessionMachine::setMachineState() when the VM is
289
if (Global::IsOnline (adep.machineState()))
290
rc = image->LockRead (NULL);
296
m->state = DriveState_ImageMounted;
298
/* leave the lock before informing callbacks */
301
mParent->onDVDDriveChange();
310
STDMETHODIMP DVDDrive::CaptureHostDrive (IHostDVDDrive *aHostDVDDrive)
312
CheckComArgNotNull(aHostDVDDrive);
314
AutoCaller autoCaller (this);
315
CheckComRCReturnRC (autoCaller.rc());
317
/* the machine needs to be mutable */
318
Machine::AutoMutableStateDependency adep (mParent);
319
CheckComRCReturnRC (adep.rc());
321
AutoWriteLock alock (this);
323
if (m->state != DriveState_HostDriveCaptured ||
324
!m->hostDrive.equalsTo (aHostDVDDrive))
326
/* umount() will backup data */
327
HRESULT rc = unmount();
330
m->hostDrive = aHostDVDDrive;
331
m->state = DriveState_HostDriveCaptured;
333
/* leave the lock before informing callbacks */
336
mParent->onDVDDriveChange();
343
STDMETHODIMP DVDDrive::Unmount()
345
AutoCaller autoCaller (this);
346
CheckComRCReturnRC (autoCaller.rc());
348
/* the machine needs to be mutable */
349
Machine::AutoMutableStateDependency adep (mParent);
350
CheckComRCReturnRC (adep.rc());
352
AutoWriteLock alock (this);
354
if (m->state != DriveState_NotMounted)
356
/* umount() will backup data */
357
HRESULT rc = unmount();
360
m->state = DriveState_NotMounted;
362
/* leave the lock before informing callbacks */
365
rc = mParent->onDVDDriveChange();
374
STDMETHODIMP DVDDrive::GetImage (IDVDImage **aDVDImage)
376
CheckComArgOutPointerValid(aDVDImage);
378
AutoCaller autoCaller (this);
379
CheckComRCReturnRC (autoCaller.rc());
381
AutoReadLock alock (this);
383
m->image.queryInterfaceTo (aDVDImage);
388
STDMETHODIMP DVDDrive::GetHostDrive(IHostDVDDrive **aHostDrive)
390
CheckComArgOutPointerValid(aHostDrive);
392
AutoCaller autoCaller (this);
393
CheckComRCReturnRC (autoCaller.rc());
395
AutoReadLock alock (this);
397
m->hostDrive.queryInterfaceTo (aHostDrive);
402
// public methods only for internal purposes
403
////////////////////////////////////////////////////////////////////////////////
406
* Loads settings from the given machine node. May be called once right after
407
* this object creation.
409
* @param aMachineNode <Machine> node.
411
* @note Locks this object for writing.
413
HRESULT DVDDrive::loadSettings (const settings::Key &aMachineNode)
415
using namespace settings;
417
AssertReturn (!aMachineNode.isNull(), E_FAIL);
419
AutoCaller autoCaller (this);
420
AssertComRCReturnRC (autoCaller.rc());
422
AutoWriteLock alock (this);
424
/* Note: we assume that the default values for attributes of optional
425
* nodes are assigned in the Data::Data() constructor and don't do it
426
* here. It implies that this method may only be called after constructing
427
* a new BIOSSettings object while all its data fields are in the default
428
* values. Exceptions are fields whose creation time defaults don't match
429
* values that should be applied when these fields are not explicitly set
430
* in the settings file (for backwards compatibility reasons). This takes
431
* place when a setting of a newly created object must default to A while
432
* the same setting of an object loaded from the old settings file must
437
/* DVD drive (required, contains either Image or HostDrive or nothing) */
438
Key dvdDriveNode = aMachineNode.key ("DVDDrive");
440
/* optional, defaults to false */
441
m->passthrough = dvdDriveNode.value <bool> ("passthrough");
445
if (!(typeNode = dvdDriveNode.findKey ("Image")).isNull())
447
Guid uuid = typeNode.value <Guid> ("uuid");
448
rc = MountImage (uuid.toUtf16());
449
CheckComRCReturnRC (rc);
451
else if (!(typeNode = dvdDriveNode.findKey ("HostDrive")).isNull())
454
Bstr src = typeNode.stringValue ("src");
456
/* find the corresponding object */
457
ComObjPtr <Host> host = mParent->virtualBox()->host();
459
com::SafeIfaceArray <IHostDVDDrive> coll;
460
rc = host->COMGETTER(DVDDrives) (ComSafeArrayAsOutParam(coll));
463
ComPtr <IHostDVDDrive> drive;
464
rc = host->FindHostDVDDrive (src, drive.asOutParam());
468
rc = CaptureHostDrive (drive);
469
CheckComRCReturnRC (rc);
471
else if (rc == E_INVALIDARG)
473
/* the host DVD drive is not currently available. we
474
* assume it will be available later and create an
475
* extra object now */
476
ComObjPtr <HostDVDDrive> hostDrive;
477
hostDrive.createObject();
478
rc = hostDrive->init (src);
480
rc = CaptureHostDrive (hostDrive);
481
CheckComRCReturnRC (rc);
491
* Saves settings to the given machine node.
493
* @param aMachineNode <Machine> node.
495
* @note Locks this object for reading.
497
HRESULT DVDDrive::saveSettings (settings::Key &aMachineNode)
499
using namespace settings;
501
AssertReturn (!aMachineNode.isNull(), E_FAIL);
503
AutoCaller autoCaller (this);
504
AssertComRCReturnRC (autoCaller.rc());
506
AutoReadLock alock (this);
508
Key node = aMachineNode.createKey ("DVDDrive");
510
node.setValue <bool> ("passthrough", !!m->passthrough);
514
case DriveState_ImageMounted:
516
Assert (!m->image.isNull());
519
HRESULT rc = m->image->COMGETTER(Id) (id.asOutParam());
521
Assert (!id.isEmpty());
523
Key imageNode = node.createKey ("Image");
524
imageNode.setValue <Guid> ("uuid", Guid(id));
527
case DriveState_HostDriveCaptured:
529
Assert (!m->hostDrive.isNull());
532
HRESULT rc = m->hostDrive->COMGETTER(Name) (name.asOutParam());
534
Assert (!name.isEmpty());
536
Key hostDriveNode = node.createKey ("HostDrive");
537
hostDriveNode.setValue <Bstr> ("src", name);
540
case DriveState_NotMounted:
541
/* do nothing, i.e.leave the drive node empty */
544
ComAssertMsgFailedRet (("Invalid drive state: %d", m->state),
552
* @note Locks this object for writing.
554
bool DVDDrive::rollback()
557
AutoCaller autoCaller (this);
558
AssertComRCReturn (autoCaller.rc(), false);
560
/* we need adep for the state check */
561
Machine::AutoAnyStateDependency adep (mParent);
562
AssertComRCReturn (adep.rc(), false);
564
AutoWriteLock alock (this);
566
bool changed = false;
570
/* we need to check all data to see whether anything will be changed
572
changed = m.hasActualChanges();
576
Data *oldData = m.backedUpData();
578
if (!m->image.isNull() &&
579
!oldData->image.equalsTo (m->image))
581
/* detach the current image that will go away after rollback */
582
m->image->detachFrom (mParent->id(), mParent->snapshotId());
584
/* unlock the image for reading if the VM is online */
585
if (Global::IsOnline (adep.machineState()))
587
HRESULT rc = m->image->UnlockRead (NULL);
592
if (!oldData->image.isNull() &&
593
!oldData->image.equalsTo (m->image))
595
/* Reattach from the old image. */
596
HRESULT rc = oldData->image->attachTo(mParent->id(), mParent->snapshotId());
598
if (Global::IsOnline (adep.machineState()))
600
/* Lock from the old image. */
601
rc = oldData->image->LockRead (NULL);
614
* @note Locks this object for writing, together with the peer object (also for
615
* writing) if there is one.
617
void DVDDrive::commit()
620
AutoCaller autoCaller (this);
621
AssertComRCReturnVoid (autoCaller.rc());
624
AutoCaller peerCaller (mPeer);
625
AssertComRCReturnVoid (peerCaller.rc());
627
/* lock both for writing since we modify both (mPeer is "master" so locked
629
AutoMultiWriteLock2 alock (mPeer, this);
636
/* attach new data to the peer and reshare it */
643
* @note Locks this object for writing, together with the peer object
644
* represented by @a aThat (locked for reading).
646
void DVDDrive::copyFrom (DVDDrive *aThat)
648
AssertReturnVoid (aThat != NULL);
651
AutoCaller autoCaller (this);
652
AssertComRCReturnVoid (autoCaller.rc());
655
AutoCaller thatCaller (aThat);
656
AssertComRCReturnVoid (thatCaller.rc());
658
/* peer is not modified, lock it for reading (aThat is "master" so locked
660
AutoMultiLock2 alock (aThat->rlock(), this->wlock());
662
/* this will back up current data */
663
m.assignCopy (aThat->m);
667
* Helper to unmount a drive.
669
* @note Must be called from under this object's write lock.
671
HRESULT DVDDrive::unmount()
673
AssertReturn (isWriteLockOnCurrentThread(), E_FAIL);
675
/* we need adep for the state check */
676
Machine::AutoAnyStateDependency adep (mParent);
677
AssertComRCReturn (adep.rc(), E_FAIL);
679
if (!m->image.isNull())
681
HRESULT rc = m->image->detachFrom (mParent->id(), mParent->snapshotId());
683
if (Global::IsOnline (adep.machineState()))
685
rc = m->image->UnlockRead (NULL);
695
m->hostDrive.setNull();
697
m->state = DriveState_NotMounted;
703
////////////////////////////////////////////////////////////////////////////////
705
/* vi: set tabstop=4 shiftwidth=4 expandtab: */