VirtualBox

source: vbox/trunk/src/VBox/Main/include/MachineImpl.h@ 3424

Last change on this file since 3424 was 3424, checked in by vboxsync, 17 years ago

Main: Removed outdated CHECK_MACHINE_MUTABILITY and CHECK_STATE macros.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 31.4 KB
Line 
1/** @file
2 *
3 * VirtualBox COM class declaration
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22#ifndef ____H_MACHINEIMPL
23#define ____H_MACHINEIMPL
24
25#include "VirtualBoxBase.h"
26#include "VirtualBoxXMLUtil.h"
27#include "ProgressImpl.h"
28#include "SnapshotImpl.h"
29#include "VRDPServerImpl.h"
30#include "DVDDriveImpl.h"
31#include "FloppyDriveImpl.h"
32#include "HardDiskAttachmentImpl.h"
33#include "Collection.h"
34#include "NetworkAdapterImpl.h"
35#include "AudioAdapterImpl.h"
36#include "BIOSSettingsImpl.h"
37
38// generated header
39#include "SchemaDefs.h"
40
41#include <VBox/types.h>
42#include <VBox/cfgldr.h>
43#include <iprt/file.h>
44#include <iprt/thread.h>
45
46#include <list>
47
48// defines
49////////////////////////////////////////////////////////////////////////////////
50
51// helper declarations
52////////////////////////////////////////////////////////////////////////////////
53
54class VirtualBox;
55class Progress;
56class CombinedProgress;
57class Keyboard;
58class Mouse;
59class Display;
60class MachineDebugger;
61class USBController;
62class Snapshot;
63class SharedFolder;
64
65class SessionMachine;
66
67// Machine class
68////////////////////////////////////////////////////////////////////////////////
69
70class ATL_NO_VTABLE Machine :
71 public VirtualBoxBaseWithChildrenNEXT,
72 public VirtualBoxXMLUtil,
73 public VirtualBoxSupportErrorInfoImpl <Machine, IMachine>,
74 public VirtualBoxSupportTranslation <Machine>,
75 public IMachine
76{
77 Q_OBJECT
78
79public:
80
81 /**
82 * Internal machine data.
83 *
84 * Only one instance of this data exists per every machine --
85 * it is shared by the Machine, SessionMachine and all SnapshotMachine
86 * instances associated with the given machine using the util::Shareable
87 * template through the mData variable.
88 *
89 * @note |const| members are persistent during lifetime so can be
90 * accessed without locking.
91 *
92 * @note There is no need to lock anything inside init() or uninit()
93 * methods, because they are always serialized (see AutoCaller).
94 */
95 struct Data
96 {
97 /**
98 * Data structure to hold information about sessions opened for the
99 * given machine.
100 */
101 struct Session
102 {
103 /** Control of the direct session opened by openSession() */
104 ComPtr <IInternalSessionControl> mDirectControl;
105
106 typedef std::list <ComPtr <IInternalSessionControl> > RemoteControlList;
107
108 /** list of controls of all opened remote sessions */
109 RemoteControlList mRemoteControls;
110
111 /** openRemoteSession() and OnSessionEnd() progress indicator */
112 ComObjPtr <Progress> mProgress;
113
114 /**
115 * PID of the session object that must be passed to openSession()
116 * to finalize the openRemoteSession() request
117 * (i.e., PID of the process created by openRemoteSession())
118 */
119 RTPROCESS mPid;
120
121 /** Current session state */
122 SessionState_T mState;
123
124 /** Session type string (for indirect sessions) */
125 Bstr mType;
126
127 /** Sesison machine object */
128 ComObjPtr <SessionMachine> mMachine;
129 };
130
131 Data();
132 ~Data();
133
134 const Guid mUuid;
135 BOOL mRegistered;
136
137 Bstr mConfigFile;
138 Bstr mConfigFileFull;
139
140 BOOL mAccessible;
141 com::ErrorInfo mAccessError;
142
143 MachineState_T mMachineState;
144 LONG64 mLastStateChange;
145
146 uint32_t mMachineStateDeps;
147 RTSEMEVENT mZeroMachineStateDepsSem;
148 BOOL mWaitingStateDeps;
149
150 BOOL mCurrentStateModified;
151
152 RTFILE mHandleCfgFile;
153
154 Session mSession;
155
156 ComObjPtr <Snapshot> mFirstSnapshot;
157 ComObjPtr <Snapshot> mCurrentSnapshot;
158 };
159
160 /**
161 * Saved state data.
162 *
163 * It's actually only the state file path string, but it needs to be
164 * separate from Data, because Machine and SessionMachine instances
165 * share it, while SnapshotMachine does not.
166 *
167 * The data variable is |mSSData|.
168 */
169 struct SSData
170 {
171 Bstr mStateFilePath;
172 };
173
174 /**
175 * User changeable machine data.
176 *
177 * This data is common for all machine snapshots, i.e. it is shared
178 * by all SnapshotMachine instances associated with the given machine
179 * using the util::Backupable template through the |mUserData| variable.
180 *
181 * SessionMachine instances can alter this data and discard changes.
182 *
183 * @note There is no need to lock anything inside init() or uninit()
184 * methods, because they are always serialized (see AutoCaller).
185 */
186 struct UserData
187 {
188 UserData();
189 ~UserData();
190
191 bool operator== (const UserData &that) const
192 {
193 return this == &that ||
194 (mName == that.mName &&
195 mNameSync == that.mNameSync &&
196 mDescription == that.mDescription &&
197 mOSTypeId == that.mOSTypeId &&
198 mSnapshotFolderFull == that.mSnapshotFolderFull);
199 }
200
201 Bstr mName;
202 BOOL mNameSync;
203 Bstr mDescription;
204 Bstr mOSTypeId;
205 Bstr mSnapshotFolder;
206 Bstr mSnapshotFolderFull;
207 };
208
209 /**
210 * Hardware data.
211 *
212 * This data is unique for a machine and for every machine snapshot.
213 * Stored using the util::Backupable template in the |mHWData| variable.
214 *
215 * SessionMachine instances can alter this data and discard changes.
216 */
217 struct HWData
218 {
219 HWData();
220 ~HWData();
221
222 bool operator== (const HWData &that) const;
223
224 ULONG mMemorySize;
225 ULONG mVRAMSize;
226 ULONG mMonitorCount;
227 TriStateBool_T mHWVirtExEnabled;
228
229 DeviceType_T mBootOrder [SchemaDefs::MaxBootPosition];
230
231 typedef std::list <ComObjPtr <SharedFolder> > SharedFolderList;
232 SharedFolderList mSharedFolders;
233 ClipboardMode_T mClipboardMode;
234 };
235
236 /**
237 * Hard disk data.
238 *
239 * The usage policy is the same as for HWData, but a separate structure
240 * is necessarym because hard disk data requires different procedures when
241 * taking or discarding snapshots, etc.
242 *
243 * The data variable is |mHWData|.
244 */
245 struct HDData
246 {
247 HDData();
248 ~HDData();
249
250 bool operator== (const HDData &that) const;
251
252 typedef std::list <ComObjPtr <HardDiskAttachment> > HDAttachmentList;
253 HDAttachmentList mHDAttachments;
254
255 /**
256 * Right after Machine::fixupHardDisks(true): |true| if hard disks
257 * were actually changed, |false| otherwise
258 */
259 bool mHDAttachmentsChanged;
260 };
261
262 enum StateDependency
263 {
264 AnyStateDep = 0, MutableStateDep, MutableOrSavedStateDep
265 };
266
267 /**
268 * Helper class that safely manages the machine state dependency by
269 * calling Machine::addStateDependency() on construction and
270 * Machine::releaseStateDependency() on destruction. Intended for Machine
271 * children. The usage pattern is:
272 *
273 * @code
274 * AutoCaller autoCaller (this);
275 * CheckComRCReturnRC (autoCaller.rc());
276 *
277 * Machine::AutoStateDependency <MutableStateDep> adep (mParent);
278 * CheckComRCReturnRC (stateDep.rc());
279 * ...
280 * // code that depends on the particular machine state
281 * ...
282 * @endcode
283 *
284 * Note that it is more convenient to use the following individual
285 * shortcut classes instead of using this template directly:
286 * AutoAnyStateDependency, AutoMutableStateDependency and
287 * AutoMutableOrSavedStateDependency. The usage pattern is exactly the
288 * same as above except that there is no need to specify the template
289 * argument because it is already done by the shortcut class.
290 *
291 * @param taDepType Dependecy type to manage.
292 */
293 template <StateDependency taDepType = AnyStateDep>
294 class AutoStateDependency
295 {
296 public:
297
298 AutoStateDependency (Machine *aThat)
299 : mThat (aThat), mRC (S_OK)
300 , mMachineState (MachineState_InvalidMachineState)
301 , mRegistered (FALSE)
302 {
303 Assert (aThat);
304 mRC = aThat->addStateDependency (taDepType, &mMachineState,
305 &mRegistered);
306 }
307 ~AutoStateDependency()
308 {
309 if (SUCCEEDED (mRC))
310 mThat->releaseStateDependency();
311 }
312
313 /** Decreases the number of dependencies before the instance is
314 * destroyed. Note that will reset #rc() to E_FAIL. */
315 void release()
316 {
317 AssertReturnVoid (SUCCEEDED (mRC));
318 mThat->releaseStateDependency();
319 mRC = E_FAIL;
320 }
321
322 /** Restores the number of callers after by #release(). #rc() will be
323 * reset to the result of calling addStateDependency() and must be
324 * rechecked to ensure the operation succeeded. */
325 void add()
326 {
327 AssertReturnVoid (!SUCCEEDED (mRC));
328 mRC = mThat->addStateDependency (taDepType, &mMachineState,
329 &mRegistered);
330 }
331
332 /** Returns the result of Machine::addStateDependency(). */
333 HRESULT rc() const { return mRC; }
334
335 /** Shortcut to SUCCEEDED (rc()). */
336 bool isOk() const { return SUCCEEDED (mRC); }
337
338 /** Returns the machine state value as returned by
339 * Machine::addStateDependency(). */
340 MachineState_T machineState() const { return mMachineState; }
341
342 /** Returns the machine state value as returned by
343 * Machine::addStateDependency(). */
344 BOOL machineRegistered() const { return mRegistered; }
345
346 protected:
347
348 Machine *mThat;
349 HRESULT mRC;
350 MachineState_T mMachineState;
351 BOOL mRegistered;
352
353 private:
354
355 DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP (AutoStateDependency)
356 DECLARE_CLS_NEW_DELETE_NOOP (AutoStateDependency)
357 };
358
359 /**
360 * Shortcut to AutoStateDependency <AnyStateDep>.
361 * See AutoStateDependency to get the usage pattern.
362 *
363 * Accepts any machine state and guarantees the state won't change before
364 * this object is destroyed. If the machine state cannot be protected (as
365 * a result of the state change currently in progress), this instance's
366 * #rc() method will indicate a failure, and the caller is not allowed to
367 * rely on any particular machine state and should return the failed
368 * result code to the upper level.
369 */
370 typedef AutoStateDependency <AnyStateDep> AutoAnyStateDependency;
371
372 /**
373 * Shortcut to AutoStateDependency <MutableStateDep>.
374 * See AutoStateDependency to get the usage pattern.
375 *
376 * Succeeds only if the machine state is in one of the mutable states, and
377 * guarantees the given mutable state won't change before this object is
378 * destroyed. If the machine is not mutable, this instance's #rc() method
379 * will indicate a failure, and the caller is not allowed to rely on any
380 * particular machine state and should return the failed result code to
381 * the upper level.
382 *
383 * Intended to be used within all setter methods of IMachine
384 * children objects (DVDDrive, NetworkAdapter, AudioAdapter, etc.) to
385 * provide data protection and consistency.
386 */
387 typedef AutoStateDependency <MutableStateDep> AutoMutableStateDependency;
388
389 /**
390 * Shortcut to AutoStateDependency <MutableOrSavedStateDep>.
391 * See AutoStateDependency to get the usage pattern.
392 *
393 * Succeeds only if the machine state is in one of the mutable states, or
394 * if the machine is in the Saved state, and guarantees the given mutable
395 * state won't change before this object is destroyed. If the machine is
396 * not mutable, this instance's #rc() method will indicate a failure, and
397 * the caller is not allowed to rely on any particular machine state and
398 * should return the failed result code to the upper level.
399 *
400 * Intended to be used within setter methods of IMachine
401 * children objects that may also operate on Saved machines.
402 */
403 typedef AutoStateDependency <MutableOrSavedStateDep> AutoMutableOrSavedStateDependency;
404
405
406 VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT (Machine)
407
408 DECLARE_NOT_AGGREGATABLE(Machine)
409
410 DECLARE_PROTECT_FINAL_CONSTRUCT()
411
412 BEGIN_COM_MAP(Machine)
413 COM_INTERFACE_ENTRY(ISupportErrorInfo)
414 COM_INTERFACE_ENTRY(IMachine)
415 END_COM_MAP()
416
417 NS_DECL_ISUPPORTS
418
419 DECLARE_EMPTY_CTOR_DTOR (Machine)
420
421 HRESULT FinalConstruct();
422 void FinalRelease();
423
424 enum InitMode { Init_New, Init_Existing, Init_Registered };
425
426 // public initializer/uninitializer for internal purposes only
427 HRESULT init (VirtualBox *aParent, const BSTR aConfigFile,
428 InitMode aMode, const BSTR aName = NULL,
429 BOOL aNameSync = TRUE, const Guid *aId = NULL);
430 void uninit();
431
432 // IMachine properties
433 STDMETHOD(COMGETTER(Parent))(IVirtualBox **aParent);
434 STDMETHOD(COMGETTER(Accessible)) (BOOL *aAccessible);
435 STDMETHOD(COMGETTER(AccessError)) (IVirtualBoxErrorInfo **aAccessError);
436 STDMETHOD(COMGETTER(Name))(BSTR *aName);
437 STDMETHOD(COMSETTER(Name))(INPTR BSTR aName);
438 STDMETHOD(COMGETTER(Description))(BSTR *aDescription);
439 STDMETHOD(COMSETTER(Description))(INPTR BSTR aDescription);
440 STDMETHOD(COMGETTER(Id))(GUIDPARAMOUT aId);
441 STDMETHOD(COMGETTER(OSTypeId)) (BSTR *aOSTypeId);
442 STDMETHOD(COMSETTER(OSTypeId)) (INPTR BSTR aOSTypeId);
443 STDMETHOD(COMGETTER(MemorySize))(ULONG *memorySize);
444 STDMETHOD(COMSETTER(MemorySize))(ULONG memorySize);
445 STDMETHOD(COMGETTER(VRAMSize))(ULONG *memorySize);
446 STDMETHOD(COMSETTER(VRAMSize))(ULONG memorySize);
447 STDMETHOD(COMGETTER(MonitorCount))(ULONG *monitorCount);
448 STDMETHOD(COMSETTER(MonitorCount))(ULONG monitorCount);
449 STDMETHOD(COMGETTER(BIOSSettings))(IBIOSSettings **biosSettings);
450 STDMETHOD(COMGETTER(HWVirtExEnabled))(TriStateBool_T *enabled);
451 STDMETHOD(COMSETTER(HWVirtExEnabled))(TriStateBool_T enabled);
452 STDMETHOD(COMGETTER(SnapshotFolder))(BSTR *aSavedStateFolder);
453 STDMETHOD(COMSETTER(SnapshotFolder))(INPTR BSTR aSavedStateFolder);
454 STDMETHOD(COMGETTER(HardDiskAttachments))(IHardDiskAttachmentCollection **attachments);
455 STDMETHOD(COMGETTER(VRDPServer))(IVRDPServer **vrdpServer);
456 STDMETHOD(COMGETTER(DVDDrive))(IDVDDrive **dvdDrive);
457 STDMETHOD(COMGETTER(FloppyDrive))(IFloppyDrive **floppyDrive);
458 STDMETHOD(COMGETTER(AudioAdapter))(IAudioAdapter **audioAdapter);
459 STDMETHOD(COMGETTER(USBController))(IUSBController * *a_ppUSBController);
460 STDMETHOD(COMGETTER(SettingsFilePath))(BSTR *filePath);
461 STDMETHOD(COMGETTER(SettingsModified))(BOOL *modified);
462 STDMETHOD(COMGETTER(SessionState))(SessionState_T *aSessionState);
463 STDMETHOD(COMGETTER(SessionType))(BSTR *aSessionType);
464 STDMETHOD(COMGETTER(SessionPid))(ULONG *aSessionPid);
465 STDMETHOD(COMGETTER(State))(MachineState_T *machineState);
466 STDMETHOD(COMGETTER(LastStateChange))(LONG64 *aLastStateChange);
467 STDMETHOD(COMGETTER(StateFilePath)) (BSTR *aStateFilePath);
468 STDMETHOD(COMGETTER(LogFolder)) (BSTR *aLogFolder);
469 STDMETHOD(COMGETTER(CurrentSnapshot)) (ISnapshot **aCurrentSnapshot);
470 STDMETHOD(COMGETTER(SnapshotCount)) (ULONG *aSnapshotCount);
471 STDMETHOD(COMGETTER(CurrentStateModified))(BOOL *aCurrentStateModified);
472 STDMETHOD(COMGETTER(SharedFolders)) (ISharedFolderCollection **aSharedFolders);
473 STDMETHOD(COMGETTER(ClipboardMode)) (ClipboardMode_T *aClipboardMode);
474 STDMETHOD(COMSETTER(ClipboardMode)) (ClipboardMode_T aClipboardMode);
475
476 // IMachine methods
477 STDMETHOD(SetBootOrder)(ULONG aPosition, DeviceType_T aDevice);
478 STDMETHOD(GetBootOrder)(ULONG aPosition, DeviceType_T *aDevice);
479 STDMETHOD(AttachHardDisk)(INPTR GUIDPARAM aId, DiskControllerType_T aCtl, LONG aDev);
480 STDMETHOD(GetHardDisk)(DiskControllerType_T aCtl, LONG aDev, IHardDisk **aHardDisk);
481 STDMETHOD(DetachHardDisk) (DiskControllerType_T aCtl, LONG aDev);
482 STDMETHOD(GetNetworkAdapter) (ULONG slot, INetworkAdapter **adapter);
483 STDMETHOD(GetNextExtraDataKey)(INPTR BSTR aKey, BSTR *aNextKey, BSTR *aNextValue);
484 STDMETHOD(GetExtraData)(INPTR BSTR aKey, BSTR *aValue);
485 STDMETHOD(SetExtraData)(INPTR BSTR aKey, INPTR BSTR aValue);
486 STDMETHOD(SaveSettings)();
487 STDMETHOD(DiscardSettings)();
488 STDMETHOD(DeleteSettings)();
489 STDMETHOD(GetSnapshot) (INPTR GUIDPARAM aId, ISnapshot **aSnapshot);
490 STDMETHOD(FindSnapshot) (INPTR BSTR aName, ISnapshot **aSnapshot);
491 STDMETHOD(SetCurrentSnapshot) (INPTR GUIDPARAM aId);
492 STDMETHOD(CreateSharedFolder) (INPTR BSTR aName, INPTR BSTR aHostPath);
493 STDMETHOD(RemoveSharedFolder) (INPTR BSTR aName);
494 STDMETHOD(CanShowConsoleWindow) (BOOL *aCanShow);
495 STDMETHOD(ShowConsoleWindow) (ULONG64 *aWinId);
496
497 // public methods only for internal purposes
498
499 /// @todo (dmik) add lock and make non-inlined after revising classes
500 // that use it. Note: they should enter Machine lock to keep the returned
501 // information valid!
502 bool isRegistered() { return !!mData->mRegistered; }
503
504 ComObjPtr <SessionMachine> sessionMachine();
505
506 // Note: the below methods are intended to be called only after adding
507 // a caller to the Machine instance and, when necessary, from under
508 // the Machine lock in appropriate mode
509
510 /// @todo (dmik) revise code using these methods: improving incapsulation
511 // should make them not necessary
512
513 const ComObjPtr <VirtualBox, ComWeakRef> &virtualBox() { return mParent; }
514
515 const Shareable <Data> &data() const { return mData; }
516 const Backupable <UserData> &userData() const { return mUserData; }
517 const Backupable <HDData> &hdData() const { return mHDData; }
518
519 const Shareable <SSData> &ssData() const { return mSSData; }
520
521 const ComObjPtr <DVDDrive> &dvdDrive() { return mDVDDrive; }
522 const ComObjPtr <FloppyDrive> &floppyDrive() { return mFloppyDrive; }
523 const ComObjPtr <USBController> &usbController() { return mUSBController; }
524
525 virtual HRESULT onDVDDriveChange() { return S_OK; }
526 virtual HRESULT onFloppyDriveChange() { return S_OK; }
527 virtual HRESULT onNetworkAdapterChange(INetworkAdapter *networkAdapter) { return S_OK; }
528 virtual HRESULT onVRDPServerChange() { return S_OK; }
529 virtual HRESULT onUSBControllerChange() { return S_OK; }
530
531 int calculateFullPath (const char *aPath, Utf8Str &aResult);
532 void calculateRelativePath (const char *aPath, Utf8Str &aResult);
533
534 void getLogFolder (Utf8Str &aLogFolder);
535
536 HRESULT openSession (IInternalSessionControl *aControl);
537 HRESULT openRemoteSession (IInternalSessionControl *aControl,
538 INPTR BSTR aType, Progress *aProgress);
539 HRESULT openExistingSession (IInternalSessionControl *aControl);
540
541 HRESULT trySetRegistered (BOOL aRegistered);
542
543 HRESULT getSharedFolder (const BSTR aName,
544 ComObjPtr <SharedFolder> &aSharedFolder,
545 bool aSetError = false)
546 {
547 AutoLock alock (this);
548 return findSharedFolder (aName, aSharedFolder, aSetError);
549 }
550
551 HRESULT addStateDependency (StateDependency aDepType = AnyStateDep,
552 MachineState_T *aState = NULL,
553 BOOL *aRegistered = NULL);
554 void releaseStateDependency();
555
556 // for VirtualBoxSupportErrorInfoImpl
557 static const wchar_t *getComponentName() { return L"Machine"; }
558
559protected:
560
561 enum InstanceType { IsMachine, IsSessionMachine, IsSnapshotMachine };
562
563 HRESULT registeredInit();
564
565 HRESULT checkStateDependency (StateDependency aDepType);
566
567 inline Machine *machine();
568
569 void uninitDataAndChildObjects();
570
571 void checkStateDependencies (AutoLock &aLock);
572
573 virtual HRESULT setMachineState (MachineState_T aMachineState);
574
575 HRESULT findSharedFolder (const BSTR aName,
576 ComObjPtr <SharedFolder> &aSharedFolder,
577 bool aSetError = false);
578
579 HRESULT loadSettings (bool aRegistered);
580 HRESULT loadSnapshot (CFGNODE aNode, const Guid &aCurSnapshotId,
581 Snapshot *aParentSnapshot);
582 HRESULT loadHardware (CFGNODE aNode);
583 HRESULT loadHardDisks (CFGNODE aNode, bool aRegistered,
584 const Guid *aSnapshotId = NULL);
585
586 HRESULT openConfigLoader (CFGHANDLE *aLoader, bool aIsNew = false);
587 HRESULT closeConfigLoader (CFGHANDLE aLoader, bool aSaveBeforeClose);
588
589 HRESULT findSnapshotNode (Snapshot *aSnapshot, CFGNODE aMachineNode,
590 CFGNODE *aSnapshotsNode, CFGNODE *aSnapshotNode);
591
592 HRESULT findSnapshot (const Guid &aId, ComObjPtr <Snapshot> &aSnapshot,
593 bool aSetError = false);
594 HRESULT findSnapshot (const BSTR aName, ComObjPtr <Snapshot> &aSnapshot,
595 bool aSetError = false);
596
597 HRESULT findHardDiskAttachment (const ComObjPtr <HardDisk> &aHd,
598 ComObjPtr <Machine> *aMachine,
599 ComObjPtr <Snapshot> *aSnapshot,
600 ComObjPtr <HardDiskAttachment> *aHda);
601
602 HRESULT prepareSaveSettings (bool &aRenamed, bool &aNew);
603 HRESULT saveSettings (bool aMarkCurStateAsModified = true,
604 bool aInformCallbacksAnyway = false);
605
606 enum
607 {
608 // ops for #saveSnapshotSettings()
609 SaveSS_NoOp = 0x00, SaveSS_AddOp = 0x01,
610 SaveSS_UpdateAttrsOp = 0x02, SaveSS_UpdateAllOp = 0x03,
611 SaveSS_OpMask = 0xF,
612 // flags for #saveSnapshotSettings()
613 SaveSS_UpdateCurStateModified = 0x40,
614 SaveSS_UpdateCurrentId = 0x80,
615 // flags for #saveStateSettings()
616 SaveSTS_CurStateModified = 0x20,
617 SaveSTS_StateFilePath = 0x40,
618 SaveSTS_StateTimeStamp = 0x80,
619 };
620
621 HRESULT saveSnapshotSettings (Snapshot *aSnapshot, int aOpFlags);
622 HRESULT saveSnapshotSettingsWorker (CFGNODE aMachineNode,
623 Snapshot *aSnapshot, int aOpFlags);
624
625 HRESULT saveSnapshot (CFGNODE aNode, Snapshot *aSnapshot, bool aAttrsOnly);
626 HRESULT saveHardware (CFGNODE aNode);
627 HRESULT saveHardDisks (CFGNODE aNode);
628
629 HRESULT saveStateSettings (int aFlags);
630
631 HRESULT wipeOutImmutableDiffs();
632
633 HRESULT fixupHardDisks (bool aCommit);
634
635 HRESULT createSnapshotDiffs (const Guid *aSnapshotId,
636 const Bstr &aFolder,
637 const ComObjPtr <Progress> &aProgress,
638 bool aOnline);
639 HRESULT deleteSnapshotDiffs (const ComObjPtr <Snapshot> &aSnapshot);
640
641 HRESULT lockConfig();
642 HRESULT unlockConfig();
643
644 /** @note This method is not thread safe */
645 BOOL isConfigLocked()
646 {
647 return !!mData && mData->mHandleCfgFile != NIL_RTFILE;
648 }
649
650 bool isInOwnDir (Utf8Str *aSettingsDir = NULL);
651
652 bool isModified();
653 bool isReallyModified (bool aIgnoreUserData = false);
654 void rollback (bool aNotify);
655 HRESULT commit();
656 void copyFrom (Machine *aThat);
657
658 const InstanceType mType;
659
660 const ComObjPtr <Machine, ComWeakRef> mPeer;
661
662 const ComObjPtr <VirtualBox, ComWeakRef> mParent;
663
664 Shareable <Data> mData;
665 Shareable <SSData> mSSData;
666
667 Backupable <UserData> mUserData;
668 Backupable <HWData> mHWData;
669 Backupable <HDData> mHDData;
670
671 // the following fields need special backup/rollback/commit handling,
672 // so they cannot be a part of HWData
673
674 const ComObjPtr <VRDPServer> mVRDPServer;
675 const ComObjPtr <DVDDrive> mDVDDrive;
676 const ComObjPtr <FloppyDrive> mFloppyDrive;
677 const ComObjPtr <AudioAdapter> mAudioAdapter;
678 const ComObjPtr <USBController> mUSBController;
679 const ComObjPtr <BIOSSettings> mBIOSSettings;
680
681 const ComObjPtr <NetworkAdapter>
682 mNetworkAdapters [SchemaDefs::NetworkAdapterCount];
683
684 friend class SessionMachine;
685 friend class SnapshotMachine;
686};
687
688// SessionMachine class
689////////////////////////////////////////////////////////////////////////////////
690
691/**
692 * @note Notes on locking objects of this class:
693 * SessionMachine shares some data with the primary Machine instance (pointed
694 * to by the |mPeer| member). In order to provide data consistency it also
695 * shares its lock handle. This means that whenever you lock a SessionMachine
696 * instance using Auto[Reader]Lock or AutoMultiLock, the corresponding Machine
697 * instance is also locked in the same lock mode. Keep it in mind.
698 */
699class ATL_NO_VTABLE SessionMachine :
700 public VirtualBoxSupportTranslation <SessionMachine>,
701 public Machine,
702 public IInternalMachineControl
703{
704public:
705
706 VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE(SessionMachine)
707
708 DECLARE_NOT_AGGREGATABLE(SessionMachine)
709
710 DECLARE_PROTECT_FINAL_CONSTRUCT()
711
712 BEGIN_COM_MAP(SessionMachine)
713 COM_INTERFACE_ENTRY(ISupportErrorInfo)
714 COM_INTERFACE_ENTRY(IMachine)
715 COM_INTERFACE_ENTRY(IInternalMachineControl)
716 END_COM_MAP()
717
718 NS_DECL_ISUPPORTS
719
720 DECLARE_EMPTY_CTOR_DTOR (SessionMachine)
721
722 HRESULT FinalConstruct();
723 void FinalRelease();
724
725 // public initializer/uninitializer for internal purposes only
726 HRESULT init (Machine *aMachine);
727 void uninit() { uninit (Uninit::Unexpected); }
728
729 // AutoLock::Lockable interface
730 AutoLock::Handle *lockHandle() const;
731
732 // IInternalMachineControl methods
733 STDMETHOD(UpdateState)(MachineState_T machineState);
734 STDMETHOD(GetIPCId)(BSTR *id);
735 STDMETHOD(RunUSBDeviceFilters) (IUSBDevice *aUSBDevice, BOOL *aMatched);
736 STDMETHOD(CaptureUSBDevice) (INPTR GUIDPARAM aId);
737 STDMETHOD(ReleaseUSBDevice) (INPTR GUIDPARAM aId);
738 STDMETHOD(AutoCaptureUSBDevices)();
739 STDMETHOD(ReleaseAllUSBDevices)();
740 STDMETHOD(OnSessionEnd)(ISession *aSession, IProgress **aProgress);
741 STDMETHOD(BeginSavingState) (IProgress *aProgress, BSTR *aStateFilePath);
742 STDMETHOD(EndSavingState) (BOOL aSuccess);
743 STDMETHOD(BeginTakingSnapshot) (IConsole *aInitiator,
744 INPTR BSTR aName, INPTR BSTR aDescription,
745 IProgress *aProgress, BSTR *aStateFilePath,
746 IProgress **aServerProgress);
747 STDMETHOD(EndTakingSnapshot) (BOOL aSuccess);
748 STDMETHOD(DiscardSnapshot) (IConsole *aInitiator, INPTR GUIDPARAM aId,
749 MachineState_T *aMachineState, IProgress **aProgress);
750 STDMETHOD(DiscardCurrentState) (
751 IConsole *aInitiator, MachineState_T *aMachineState, IProgress **aProgress);
752 STDMETHOD(DiscardCurrentSnapshotAndState) (
753 IConsole *aInitiator, MachineState_T *aMachineState, IProgress **aProgress);
754
755 // public methods only for internal purposes
756
757 bool checkForDeath();
758#ifdef __WIN__
759 HANDLE ipcSem() { return mIPCSem; }
760#endif
761
762 HRESULT onDVDDriveChange();
763 HRESULT onFloppyDriveChange();
764 HRESULT onNetworkAdapterChange(INetworkAdapter *networkAdapter);
765 HRESULT onVRDPServerChange();
766 HRESULT onUSBControllerChange();
767 HRESULT onUSBDeviceAttach (IUSBDevice *aDevice,
768 IVirtualBoxErrorInfo *aError);
769 HRESULT onUSBDeviceDetach (INPTR GUIDPARAM aId,
770 IVirtualBoxErrorInfo *aError);
771
772private:
773
774 struct SnapshotData
775 {
776 SnapshotData() : mLastState (MachineState_InvalidMachineState) {}
777
778 MachineState_T mLastState;
779
780 // used when taking snapshot
781 ComObjPtr <Snapshot> mSnapshot;
782 ComObjPtr <Progress> mServerProgress;
783 ComObjPtr <CombinedProgress> mCombinedProgress;
784
785 // used when saving state
786 Guid mProgressId;
787 Bstr mStateFilePath;
788 };
789
790 struct Uninit {
791 enum Reason { Unexpected, Abnormal, Normal };
792 };
793
794 struct Task;
795 struct TakeSnapshotTask;
796 struct DiscardSnapshotTask;
797 struct DiscardCurrentStateTask;
798
799 friend struct TakeSnapshotTask;
800 friend struct DiscardSnapshotTask;
801 friend struct DiscardCurrentStateTask;
802
803 void uninit (Uninit::Reason aReason);
804
805 HRESULT endSavingState (BOOL aSuccess);
806 HRESULT endTakingSnapshot (BOOL aSuccess);
807
808 typedef std::map <ComObjPtr <Machine>, MachineState_T> AffectedMachines;
809
810 void takeSnapshotHandler (TakeSnapshotTask &aTask);
811 void discardSnapshotHandler (DiscardSnapshotTask &aTask);
812 void discardCurrentStateHandler (DiscardCurrentStateTask &aTask);
813
814 HRESULT setMachineState (MachineState_T aMachineState);
815 HRESULT updateMachineStateOnClient();
816
817 SnapshotData mSnapshotData;
818
819 /** interprocess semaphore handle (id) for this machine */
820#if defined(__WIN__)
821 HANDLE mIPCSem;
822 Bstr mIPCSemName;
823#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
824 int mIPCSem;
825#endif
826
827 static DECLCALLBACK(int) taskHandler (RTTHREAD thread, void *pvUser);
828};
829
830// SnapshotMachine class
831////////////////////////////////////////////////////////////////////////////////
832
833/**
834 * @note Notes on locking objects of this class:
835 * SnapshotMachine shares some data with the primary Machine instance (pointed
836 * to by the |mPeer| member). In order to provide data consistency it also
837 * shares its lock handle. This means that whenever you lock a SessionMachine
838 * instance using Auto[Reader]Lock or AutoMultiLock, the corresponding Machine
839 * instance is also locked in the same lock mode. Keep it in mind.
840 */
841class ATL_NO_VTABLE SnapshotMachine :
842 public VirtualBoxSupportTranslation <SnapshotMachine>,
843 public Machine
844{
845public:
846
847 VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE(SnapshotMachine)
848
849 DECLARE_NOT_AGGREGATABLE(SnapshotMachine)
850
851 DECLARE_PROTECT_FINAL_CONSTRUCT()
852
853 BEGIN_COM_MAP(SnapshotMachine)
854 COM_INTERFACE_ENTRY(ISupportErrorInfo)
855 COM_INTERFACE_ENTRY(IMachine)
856 END_COM_MAP()
857
858 NS_DECL_ISUPPORTS
859
860 DECLARE_EMPTY_CTOR_DTOR (SnapshotMachine)
861
862 HRESULT FinalConstruct();
863 void FinalRelease();
864
865 // public initializer/uninitializer for internal purposes only
866 HRESULT init (SessionMachine *aSessionMachine,
867 INPTR GUIDPARAM aSnapshotId, INPTR BSTR aStateFilePath);
868 HRESULT init (Machine *aMachine, CFGNODE aHWNode, CFGNODE aHDAsNode,
869 INPTR GUIDPARAM aSnapshotId, INPTR BSTR aStateFilePath);
870 void uninit();
871
872 // AutoLock::Lockable interface
873 AutoLock::Handle *lockHandle() const;
874
875 // public methods only for internal purposes
876
877 HRESULT onSnapshotChange (Snapshot *aSnapshot);
878
879private:
880
881 Guid mSnapshotId;
882};
883
884////////////////////////////////////////////////////////////////////////////////
885
886/**
887 * Returns a pointer to the Machine object for this machine that acts like a
888 * parent for complex machine data objects such as shared folders, etc.
889 *
890 * For primary Machine objects and for SnapshotMachine objects, returns this
891 * object's pointer itself. For SessoinMachine objects, returns the peer
892 * (primary) machine pointer.
893 */
894inline Machine *Machine::machine()
895{
896 if (mType == IsSessionMachine)
897 return mPeer;
898 return this;
899}
900
901COM_DECL_READONLY_ENUM_AND_COLLECTION (Machine)
902
903#endif // ____H_MACHINEIMPL
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