VirtualBox

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

Last change on this file since 2929 was 2804, checked in by vboxsync, 18 years ago

Main: Added Machine::AutoStateDependency smart class that helps to ensure the machine state won't unexpectedly change, in a lock-free manner.

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