VirtualBox

source: vbox/trunk/src/VBox/Main/DVDDriveImpl.cpp@ 22480

Last change on this file since 22480 was 22173, checked in by vboxsync, 15 years ago

Main: the big XML settings rework. Move XML reading/writing out of interface implementation code into separate layer so it can handle individual settings versions in the future.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.4 KB
Line 
1/* $Id: DVDDriveImpl.cpp 22173 2009-08-11 15:38:59Z vboxsync $ */
2
3/** @file
4 *
5 * VirtualBox COM class implementation
6 */
7
8/*
9 * Copyright (C) 2006-2009 Sun Microsystems, Inc.
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 * 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.
22 */
23
24#include "DVDDriveImpl.h"
25
26#include "MachineImpl.h"
27#include "HostImpl.h"
28#include "HostDVDDriveImpl.h"
29#include "VirtualBoxImpl.h"
30
31#include "Global.h"
32
33#include "Logging.h"
34
35#include <iprt/string.h>
36#include <iprt/cpputils.h>
37
38#include <VBox/settings.h>
39
40// constructor / destructor
41////////////////////////////////////////////////////////////////////////////////
42
43DEFINE_EMPTY_CTOR_DTOR (DVDDrive)
44
45HRESULT DVDDrive::FinalConstruct()
46{
47 return S_OK;
48}
49
50void DVDDrive::FinalRelease()
51{
52 uninit();
53}
54
55// public initializer/uninitializer for internal purposes only
56////////////////////////////////////////////////////////////////////////////////
57
58/**
59 * Initializes the DVD drive object.
60 *
61 * @param aParent Handle of the parent object.
62 */
63HRESULT DVDDrive::init (Machine *aParent)
64{
65 LogFlowThisFunc(("aParent=%p\n", aParent));
66
67 ComAssertRet (aParent, E_INVALIDARG);
68
69 /* Enclose the state transition NotReady->InInit->Ready */
70 AutoInitSpan autoInitSpan(this);
71 AssertReturn(autoInitSpan.isOk(), E_FAIL);
72
73 unconst(mParent) = aParent;
74 /* mPeer is left null */
75
76 m.allocate();
77
78 /* Confirm a successful initialization */
79 autoInitSpan.setSucceeded();
80
81 return S_OK;
82}
83
84/**
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.
88 *
89 * @note This object must be destroyed before the original object
90 * it shares data with is destroyed.
91 *
92 * @note Locks @a aThat object for reading.
93 */
94HRESULT DVDDrive::init (Machine *aParent, DVDDrive *aThat)
95{
96 LogFlowThisFunc(("aParent=%p, aThat=%p\n", aParent, aThat));
97
98 ComAssertRet (aParent && aThat, E_INVALIDARG);
99
100 /* Enclose the state transition NotReady->InInit->Ready */
101 AutoInitSpan autoInitSpan(this);
102 AssertReturn(autoInitSpan.isOk(), E_FAIL);
103
104 unconst(mParent) = aParent;
105 unconst(mPeer) = aThat;
106
107 AutoCaller thatCaller (aThat);
108 AssertComRCReturnRC(thatCaller.rc());
109
110 AutoReadLock thatLock (aThat);
111 m.share (aThat->m);
112
113 /* Confirm a successful initialization */
114 autoInitSpan.setSucceeded();
115
116 return S_OK;
117}
118
119/**
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.
123 *
124 * @note Locks @a aThat object for reading.
125 */
126HRESULT DVDDrive::initCopy (Machine *aParent, DVDDrive *aThat)
127{
128 LogFlowThisFunc(("aParent=%p, aThat=%p\n", aParent, aThat));
129
130 ComAssertRet (aParent && aThat, E_INVALIDARG);
131
132 /* Enclose the state transition NotReady->InInit->Ready */
133 AutoInitSpan autoInitSpan(this);
134 AssertReturn(autoInitSpan.isOk(), E_FAIL);
135
136 unconst(mParent) = aParent;
137 /* mPeer is left null */
138
139 AutoCaller thatCaller (aThat);
140 AssertComRCReturnRC(thatCaller.rc());
141
142 AutoReadLock thatLock (aThat);
143 m.attachCopy (aThat->m);
144
145 /* at present, this must be a snapshot machine */
146 Assert (!aParent->snapshotId().isEmpty());
147
148 if (m->state == DriveState_ImageMounted)
149 {
150 /* associate the DVD image media with the snapshot */
151 HRESULT rc = m->image->attachTo (aParent->id(),
152 aParent->snapshotId());
153 AssertComRC (rc);
154 }
155
156 /* Confirm a successful initialization */
157 autoInitSpan.setSucceeded();
158
159 return S_OK;
160}
161
162/**
163 * Uninitializes the instance and sets the ready flag to FALSE.
164 * Called either from FinalRelease() or by the parent when it gets destroyed.
165 */
166void DVDDrive::uninit()
167{
168 LogFlowThisFunc(("\n"));
169
170 /* Enclose the state transition Ready->InUninit->NotReady */
171 AutoUninitSpan autoUninitSpan(this);
172 if (autoUninitSpan.uninitDone())
173 return;
174
175 if ((mParent->type() == Machine::IsMachine ||
176 mParent->type() == Machine::IsSnapshotMachine) &&
177 m->state == DriveState_ImageMounted)
178 {
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());
186 AssertComRC (rc);
187 }
188
189 m.free();
190
191 unconst(mPeer).setNull();
192 unconst(mParent).setNull();
193}
194
195// IDVDDrive properties
196////////////////////////////////////////////////////////////////////////////////
197
198STDMETHODIMP DVDDrive::COMGETTER(State) (DriveState_T *aState)
199{
200 CheckComArgOutPointerValid(aState);
201
202 AutoCaller autoCaller(this);
203 CheckComRCReturnRC(autoCaller.rc());
204
205 AutoReadLock alock(this);
206
207 *aState = m->state;
208
209 return S_OK;
210}
211
212STDMETHODIMP DVDDrive::COMGETTER(Passthrough) (BOOL *aPassthrough)
213{
214 CheckComArgOutPointerValid(aPassthrough);
215
216 AutoCaller autoCaller(this);
217 CheckComRCReturnRC(autoCaller.rc());
218
219 AutoReadLock alock(this);
220
221 *aPassthrough = m->passthrough;
222
223 return S_OK;
224}
225
226STDMETHODIMP DVDDrive::COMSETTER(Passthrough) (BOOL aPassthrough)
227{
228 AutoCaller autoCaller(this);
229 CheckComRCReturnRC(autoCaller.rc());
230
231 /* the machine needs to be mutable */
232 Machine::AutoMutableStateDependency adep (mParent);
233 CheckComRCReturnRC(adep.rc());
234
235 AutoWriteLock alock(this);
236
237 if (m->passthrough != aPassthrough)
238 {
239 m.backup();
240 m->passthrough = aPassthrough;
241 }
242
243 return S_OK;
244}
245
246// IDVDDrive methods
247////////////////////////////////////////////////////////////////////////////////
248
249STDMETHODIMP DVDDrive::MountImage (IN_BSTR aImageId)
250{
251 Guid imageId(aImageId);
252 CheckComArgExpr(aImageId, !imageId.isEmpty());
253
254 AutoCaller autoCaller(this);
255 CheckComRCReturnRC(autoCaller.rc());
256
257 /* the machine needs to be mutable */
258 Machine::AutoMutableStateDependency adep (mParent);
259 CheckComRCReturnRC(adep.rc());
260
261 AutoWriteLock alock(this);
262
263 HRESULT rc = E_FAIL;
264
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. */
267
268 ComObjPtr<DVDImage> image;
269 rc = mParent->virtualBox()->findDVDImage(&imageId, NULL,
270 true /* aSetError */, &image);
271
272 if (SUCCEEDED(rc))
273 {
274 if (m->state != DriveState_ImageMounted ||
275 !m->image.equalsTo (image))
276 {
277 rc = image->attachTo (mParent->id(), mParent->snapshotId());
278 if (SUCCEEDED(rc))
279 {
280 /* umount() will backup data */
281 rc = unmount();
282
283 if (SUCCEEDED(rc))
284 {
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
288 * terminated */
289 if (Global::IsOnline (adep.machineState()))
290 rc = image->LockRead (NULL);
291 }
292
293 if (SUCCEEDED(rc))
294 {
295 m->image = image;
296 m->state = DriveState_ImageMounted;
297
298 /* leave the lock before informing callbacks */
299 alock.unlock();
300
301 mParent->onDVDDriveChange();
302 }
303 }
304 }
305 }
306
307 return rc;
308}
309
310STDMETHODIMP DVDDrive::CaptureHostDrive (IHostDVDDrive *aHostDVDDrive)
311{
312 CheckComArgNotNull(aHostDVDDrive);
313
314 AutoCaller autoCaller(this);
315 CheckComRCReturnRC(autoCaller.rc());
316
317 /* the machine needs to be mutable */
318 Machine::AutoMutableStateDependency adep (mParent);
319 CheckComRCReturnRC(adep.rc());
320
321 AutoWriteLock alock(this);
322
323 if (m->state != DriveState_HostDriveCaptured ||
324 !m->hostDrive.equalsTo (aHostDVDDrive))
325 {
326 /* umount() will backup data */
327 HRESULT rc = unmount();
328 if (SUCCEEDED(rc))
329 {
330 m->hostDrive = aHostDVDDrive;
331 m->state = DriveState_HostDriveCaptured;
332
333 /* leave the lock before informing callbacks */
334 alock.unlock();
335
336 mParent->onDVDDriveChange();
337 }
338 }
339
340 return S_OK;
341}
342
343STDMETHODIMP DVDDrive::Unmount()
344{
345 AutoCaller autoCaller(this);
346 CheckComRCReturnRC(autoCaller.rc());
347
348 /* the machine needs to be mutable */
349 Machine::AutoMutableStateDependency adep (mParent);
350 CheckComRCReturnRC(adep.rc());
351
352 AutoWriteLock alock(this);
353
354 if (m->state != DriveState_NotMounted)
355 {
356 /* umount() will backup data */
357 HRESULT rc = unmount();
358 if (SUCCEEDED(rc))
359 {
360 m->state = DriveState_NotMounted;
361
362 /* leave the lock before informing callbacks */
363 alock.unlock();
364
365 rc = mParent->onDVDDriveChange();
366 if (FAILED (rc))
367 rollback();
368 }
369 }
370
371 return S_OK;
372}
373
374STDMETHODIMP DVDDrive::GetImage (IDVDImage **aDVDImage)
375{
376 CheckComArgOutPointerValid(aDVDImage);
377
378 AutoCaller autoCaller(this);
379 CheckComRCReturnRC(autoCaller.rc());
380
381 AutoReadLock alock(this);
382
383 m->image.queryInterfaceTo(aDVDImage);
384
385 return S_OK;
386}
387
388STDMETHODIMP DVDDrive::GetHostDrive(IHostDVDDrive **aHostDrive)
389{
390 CheckComArgOutPointerValid(aHostDrive);
391
392 AutoCaller autoCaller(this);
393 CheckComRCReturnRC(autoCaller.rc());
394
395 AutoReadLock alock(this);
396
397 m->hostDrive.queryInterfaceTo(aHostDrive);
398
399 return S_OK;
400}
401
402// public methods only for internal purposes
403////////////////////////////////////////////////////////////////////////////////
404
405/**
406 * Loads settings from the given machine node. May be called once right after
407 * this object creation.
408 *
409 * @param aMachineNode <Machine> node.
410 *
411 * @note Locks this object for writing.
412 */
413HRESULT DVDDrive::loadSettings(const settings::DVDDrive &data)
414{
415 AutoCaller autoCaller(this);
416 AssertComRCReturnRC(autoCaller.rc());
417
418 AutoWriteLock alock(this);
419
420 /* Note: we assume that the default values for attributes of optional
421 * nodes are assigned in the Data::Data() constructor and don't do it
422 * here. It implies that this method may only be called after constructing
423 * a new BIOSSettings object while all its data fields are in the default
424 * values. Exceptions are fields whose creation time defaults don't match
425 * values that should be applied when these fields are not explicitly set
426 * in the settings file (for backwards compatibility reasons). This takes
427 * place when a setting of a newly created object must default to A while
428 * the same setting of an object loaded from the old settings file must
429 * default to B. */
430
431 HRESULT rc = S_OK;
432
433 /* optional, defaults to false */
434 m->passthrough = data.fPassThrough;
435
436 if (!data.uuid.isEmpty())
437 {
438 rc = MountImage(data.uuid.toUtf16());
439 CheckComRCReturnRC (rc);
440 }
441 else if (!data.strHostDriveSrc.isEmpty())
442 {
443 Bstr src = data.strHostDriveSrc;
444
445 /* find the corresponding object */
446 ComObjPtr<Host> host = mParent->virtualBox()->host();
447
448 com::SafeIfaceArray<IHostDVDDrive> coll;
449 rc = host->COMGETTER(DVDDrives)(ComSafeArrayAsOutParam(coll));
450 AssertComRC (rc);
451
452 ComPtr<IHostDVDDrive> drive;
453 rc = host->FindHostDVDDrive(src, drive.asOutParam());
454
455 if (SUCCEEDED(rc))
456 {
457 rc = CaptureHostDrive(drive);
458 CheckComRCReturnRC(rc);
459 }
460 else if (rc == E_INVALIDARG)
461 {
462 /* the host DVD drive is not currently available. we
463 * assume it will be available later and create an
464 * extra object now */
465 ComObjPtr<HostDVDDrive> hostDrive;
466 hostDrive.createObject();
467 rc = hostDrive->init (src);
468 AssertComRC(rc);
469 rc = CaptureHostDrive (hostDrive);
470 CheckComRCReturnRC(rc);
471 }
472 else if (rc == VBOX_E_OBJECT_NOT_FOUND)
473 {
474 /* dvd drive mapping was not found anymore - can
475 happen when disabling/hiding the drive created by
476 a daemon tools-like program */
477 ComAssertMsgFailedRet(("DVD drive %s does not exist!\n", src.raw()), E_FAIL);
478 }
479 else
480 AssertComRC(rc);
481 }
482
483 return S_OK;
484}
485
486/**
487 * Saves settings to the given machine node.
488 *
489 * @param aMachineNode <Machine> node.
490 *
491 * @note Locks this object for reading.
492 */
493HRESULT DVDDrive::saveSettings(settings::DVDDrive &data)
494{
495 AutoCaller autoCaller(this);
496 AssertComRCReturnRC(autoCaller.rc());
497
498 AutoReadLock alock(this);
499
500 data.fPassThrough = !!m->passthrough;
501
502 HRESULT rc = S_OK;
503
504 switch (m->state)
505 {
506 case DriveState_ImageMounted:
507 {
508 Assert (!m->image.isNull());
509
510 Bstr id;
511 rc = m->image->COMGETTER(Id) (id.asOutParam());
512 AssertComRC (rc);
513 Assert (!id.isEmpty());
514
515 data.uuid = Guid(id);
516 data.strHostDriveSrc.setNull();
517 break;
518 }
519 case DriveState_HostDriveCaptured:
520 {
521 Assert (!m->hostDrive.isNull());
522
523 Bstr name;
524 rc = m->hostDrive->COMGETTER(Name)(name.asOutParam());
525 AssertComRC (rc);
526 Assert (!name.isEmpty());
527
528 data.uuid.clear();
529 data.strHostDriveSrc = name;
530 break;
531 }
532 case DriveState_NotMounted:
533 /* do nothing, i.e.leave the drive node empty */
534 data.uuid.clear();
535 data.strHostDriveSrc.setNull();
536 break;
537 default:
538 ComAssertMsgFailedRet (("Invalid drive state: %d", m->state),
539 E_FAIL);
540 }
541
542 return S_OK;
543}
544
545/**
546 * @note Locks this object for writing.
547 */
548bool DVDDrive::rollback()
549{
550 /* sanity */
551 AutoCaller autoCaller(this);
552 AssertComRCReturn (autoCaller.rc(), false);
553
554 /* we need adep for the state check */
555 Machine::AutoAnyStateDependency adep (mParent);
556 AssertComRCReturn (adep.rc(), false);
557
558 AutoWriteLock alock(this);
559
560 bool changed = false;
561
562 if (m.isBackedUp())
563 {
564 /* we need to check all data to see whether anything will be changed
565 * after rollback */
566 changed = m.hasActualChanges();
567
568 if (changed)
569 {
570 Data *oldData = m.backedUpData();
571
572 if (!m->image.isNull() &&
573 !oldData->image.equalsTo (m->image))
574 {
575 /* detach the current image that will go away after rollback */
576 m->image->detachFrom (mParent->id(), mParent->snapshotId());
577
578 /* unlock the image for reading if the VM is online */
579 if (Global::IsOnline (adep.machineState()))
580 {
581 HRESULT rc = m->image->UnlockRead (NULL);
582 AssertComRC (rc);
583 }
584 }
585
586 if (!oldData->image.isNull() &&
587 !oldData->image.equalsTo (m->image))
588 {
589 /* Reattach from the old image. */
590 HRESULT rc = oldData->image->attachTo(mParent->id(), mParent->snapshotId());
591 AssertComRC (rc);
592 if (Global::IsOnline (adep.machineState()))
593 {
594 /* Lock from the old image. */
595 rc = oldData->image->LockRead (NULL);
596 AssertComRC (rc);
597 }
598 }
599 }
600
601 m.rollback();
602 }
603
604 return changed;
605}
606
607/**
608 * @note Locks this object for writing, together with the peer object (also for
609 * writing) if there is one.
610 */
611void DVDDrive::commit()
612{
613 /* sanity */
614 AutoCaller autoCaller(this);
615 AssertComRCReturnVoid (autoCaller.rc());
616
617 /* sanity too */
618 AutoCaller peerCaller (mPeer);
619 AssertComRCReturnVoid (peerCaller.rc());
620
621 /* lock both for writing since we modify both (mPeer is "master" so locked
622 * first) */
623 AutoMultiWriteLock2 alock (mPeer, this);
624
625 if (m.isBackedUp())
626 {
627 m.commit();
628 if (mPeer)
629 {
630 /* attach new data to the peer and reshare it */
631 mPeer->m.attach (m);
632 }
633 }
634}
635
636/**
637 * @note Locks this object for writing, together with the peer object
638 * represented by @a aThat (locked for reading).
639 */
640void DVDDrive::copyFrom (DVDDrive *aThat)
641{
642 AssertReturnVoid (aThat != NULL);
643
644 /* sanity */
645 AutoCaller autoCaller(this);
646 AssertComRCReturnVoid (autoCaller.rc());
647
648 /* sanity too */
649 AutoCaller thatCaller (aThat);
650 AssertComRCReturnVoid (thatCaller.rc());
651
652 /* peer is not modified, lock it for reading (aThat is "master" so locked
653 * first) */
654 AutoMultiLock2 alock (aThat->rlock(), this->wlock());
655
656 /* this will back up current data */
657 m.assignCopy (aThat->m);
658}
659
660/**
661 * Helper to unmount a drive.
662 *
663 * @note Must be called from under this object's write lock.
664 */
665HRESULT DVDDrive::unmount()
666{
667 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
668
669 /* we need adep for the state check */
670 Machine::AutoAnyStateDependency adep (mParent);
671 AssertComRCReturn (adep.rc(), E_FAIL);
672
673 if (!m->image.isNull())
674 {
675 HRESULT rc = m->image->detachFrom (mParent->id(), mParent->snapshotId());
676 AssertComRC (rc);
677 if (Global::IsOnline (adep.machineState()))
678 {
679 rc = m->image->UnlockRead (NULL);
680 AssertComRC (rc);
681 }
682 }
683
684 m.backup();
685
686 if (m->image)
687 m->image.setNull();
688 if (m->hostDrive)
689 m->hostDrive.setNull();
690
691 m->state = DriveState_NotMounted;
692
693 return S_OK;
694}
695
696// private methods
697////////////////////////////////////////////////////////////////////////////////
698
699/* 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