VirtualBox

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

Last change on this file since 15091 was 15051, checked in by vboxsync, 16 years ago

Main: Cleaned up the long standing const BSTR = const (OLECHAR *) on WIn32 vs (const PRunichar) * on XPCOM clash. Cleaned up BSTR/GUID macros (IN_BSTR replaces INPTR BSTR, IN_GUID replaces INPTR GUIDPARAM, OUT_GUID replaces GUIDPARAMOUT).

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