VirtualBox

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

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

InnoTek -> innotek: all the headers and comments.

  • 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-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/**
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(LogFolder)) (BSTR *aLogFolder);
439 STDMETHOD(COMGETTER(CurrentSnapshot)) (ISnapshot **aCurrentSnapshot);
440 STDMETHOD(COMGETTER(SnapshotCount)) (ULONG *aSnapshotCount);
441 STDMETHOD(COMGETTER(CurrentStateModified))(BOOL *aCurrentStateModified);
442 STDMETHOD(COMGETTER(SharedFolders)) (ISharedFolderCollection **aSharedFolders);
443 STDMETHOD(COMGETTER(ClipboardMode)) (ClipboardMode_T *aClipboardMode);
444 STDMETHOD(COMSETTER(ClipboardMode)) (ClipboardMode_T aClipboardMode);
445
446 // IMachine methods
447 STDMETHOD(SetBootOrder)(ULONG aPosition, DeviceType_T aDevice);
448 STDMETHOD(GetBootOrder)(ULONG aPosition, DeviceType_T *aDevice);
449 STDMETHOD(AttachHardDisk)(INPTR GUIDPARAM aId, DiskControllerType_T aCtl, LONG aDev);
450 STDMETHOD(GetHardDisk)(DiskControllerType_T aCtl, LONG aDev, IHardDisk **aHardDisk);
451 STDMETHOD(DetachHardDisk) (DiskControllerType_T aCtl, LONG aDev);
452 STDMETHOD(GetNetworkAdapter) (ULONG slot, INetworkAdapter **adapter);
453 STDMETHOD(GetNextExtraDataKey)(INPTR BSTR aKey, BSTR *aNextKey, BSTR *aNextValue);
454 STDMETHOD(GetExtraData)(INPTR BSTR aKey, BSTR *aValue);
455 STDMETHOD(SetExtraData)(INPTR BSTR aKey, INPTR BSTR aValue);
456 STDMETHOD(SaveSettings)();
457 STDMETHOD(DiscardSettings)();
458 STDMETHOD(DeleteSettings)();
459 STDMETHOD(GetSnapshot) (INPTR GUIDPARAM aId, ISnapshot **aSnapshot);
460 STDMETHOD(FindSnapshot) (INPTR BSTR aName, ISnapshot **aSnapshot);
461 STDMETHOD(SetCurrentSnapshot) (INPTR GUIDPARAM aId);
462 STDMETHOD(CreateSharedFolder) (INPTR BSTR aName, INPTR BSTR aHostPath);
463 STDMETHOD(RemoveSharedFolder) (INPTR BSTR aName);
464 STDMETHOD(CanShowConsoleWindow) (BOOL *aCanShow);
465 STDMETHOD(ShowConsoleWindow) (ULONG64 *aWinId);
466
467 // public methods only for internal purposes
468
469 /// @todo (dmik) add lock and make non-inlined after revising classes
470 // that use it (actually, the CHECK_MACHINE_MUTABILITY macro).
471 // Note: these classes should enter Machine lock to keep the returned
472 // information valid!
473 /// @todo replace with AutoStateDependency
474 bool isMutable()
475 {
476 return ((!mData->mRegistered) ||
477 (mType == IsSessionMachine &&
478 mData->mMachineState <= MachineState_Paused &&
479 mData->mMachineState != MachineState_Saved));
480 }
481
482 /// @todo (dmik) add lock and make non-inlined after revising classes
483 // that use it (actually, the CHECK_MACHINE_MUTABILITY_IGNORING_SAVED macro).
484 // Note: these classes should enter Machine lock to keep the returned
485 // information valid!
486 /// @todo replace with AutoStateDependency
487 bool isMutableIgnoringSavedState()
488 {
489 return ((!mData->mRegistered) ||
490 (mType == IsSessionMachine &&
491 mData->mMachineState <= MachineState_Paused));
492 }
493
494 /// @todo (dmik) add lock and make non-inlined after revising classes
495 // that use it. Note: they should enter Machine lock to keep the returned
496 // information valid!
497 bool isRegistered() { return !!mData->mRegistered; }
498
499 ComObjPtr <SessionMachine> sessionMachine();
500
501 // Note: the below methods are intended to be called only after adding
502 // a caller to the Machine instance and, when necessary, from under
503 // the Machine lock in appropriate mode
504
505 /// @todo (dmik) revise code using these methods: improving incapsulation
506 // should make them not necessary
507
508 const ComObjPtr <VirtualBox, ComWeakRef> &virtualBox() { return mParent; }
509
510 const Shareable <Data> &data() const { return mData; }
511 const Backupable <UserData> &userData() const { return mUserData; }
512 const Backupable <HDData> &hdData() const { return mHDData; }
513
514 const Shareable <SSData> &ssData() const { return mSSData; }
515
516 const ComObjPtr <DVDDrive> &dvdDrive() { return mDVDDrive; }
517 const ComObjPtr <FloppyDrive> &floppyDrive() { return mFloppyDrive; }
518 const ComObjPtr <USBController> &usbController() { return mUSBController; }
519
520 virtual HRESULT onDVDDriveChange() { return S_OK; }
521 virtual HRESULT onFloppyDriveChange() { return S_OK; }
522 virtual HRESULT onNetworkAdapterChange(INetworkAdapter *networkAdapter) { return S_OK; }
523 virtual HRESULT onVRDPServerChange() { return S_OK; }
524 virtual HRESULT onUSBControllerChange() { return S_OK; }
525
526 int calculateFullPath (const char *aPath, Utf8Str &aResult);
527 void calculateRelativePath (const char *aPath, Utf8Str &aResult);
528
529 void getLogFolder (Utf8Str &aLogFolder);
530
531 HRESULT openSession (IInternalSessionControl *aControl);
532 HRESULT openRemoteSession (IInternalSessionControl *aControl,
533 INPTR BSTR aType, Progress *aProgress);
534 HRESULT openExistingSession (IInternalSessionControl *aControl);
535
536 HRESULT trySetRegistered (BOOL aRegistered);
537
538 HRESULT getSharedFolder (const BSTR aName,
539 ComObjPtr <SharedFolder> &aSharedFolder,
540 bool aSetError = false)
541 {
542 AutoLock alock (this);
543 return findSharedFolder (aName, aSharedFolder, aSetError);
544 }
545
546 HRESULT addStateDependency (StateDependency aDepType = AnyStateDep,
547 MachineState_T *aState = NULL,
548 BOOL *aRegistered = NULL);
549 void releaseStateDependency();
550
551 // for VirtualBoxSupportErrorInfoImpl
552 static const wchar_t *getComponentName() { return L"Machine"; }
553
554protected:
555
556 enum InstanceType { IsMachine, IsSessionMachine, IsSnapshotMachine };
557
558 HRESULT registeredInit();
559
560 inline Machine *machine();
561
562 void uninitDataAndChildObjects();
563
564 void checkStateDependencies (AutoLock &aLock);
565
566 virtual HRESULT setMachineState (MachineState_T aMachineState);
567
568 HRESULT findSharedFolder (const BSTR aName,
569 ComObjPtr <SharedFolder> &aSharedFolder,
570 bool aSetError = false);
571
572 HRESULT loadSettings (bool aRegistered);
573 HRESULT loadSnapshot (CFGNODE aNode, const Guid &aCurSnapshotId,
574 Snapshot *aParentSnapshot);
575 HRESULT loadHardware (CFGNODE aNode);
576 HRESULT loadHardDisks (CFGNODE aNode, bool aRegistered,
577 const Guid *aSnapshotId = NULL);
578
579 HRESULT openConfigLoader (CFGHANDLE *aLoader, bool aIsNew = false);
580 HRESULT closeConfigLoader (CFGHANDLE aLoader, bool aSaveBeforeClose);
581
582 HRESULT findSnapshotNode (Snapshot *aSnapshot, CFGNODE aMachineNode,
583 CFGNODE *aSnapshotsNode, CFGNODE *aSnapshotNode);
584
585 HRESULT findSnapshot (const Guid &aId, ComObjPtr <Snapshot> &aSnapshot,
586 bool aSetError = false);
587 HRESULT findSnapshot (const BSTR aName, ComObjPtr <Snapshot> &aSnapshot,
588 bool aSetError = false);
589
590 HRESULT findHardDiskAttachment (const ComObjPtr <HardDisk> &aHd,
591 ComObjPtr <Machine> *aMachine,
592 ComObjPtr <Snapshot> *aSnapshot,
593 ComObjPtr <HardDiskAttachment> *aHda);
594
595 HRESULT prepareSaveSettings (bool &aRenamed, bool &aNew);
596 HRESULT saveSettings (bool aMarkCurStateAsModified = true,
597 bool aInformCallbacksAnyway = false);
598
599 enum
600 {
601 // ops for #saveSnapshotSettings()
602 SaveSS_NoOp = 0x00, SaveSS_AddOp = 0x01,
603 SaveSS_UpdateAttrsOp = 0x02, SaveSS_UpdateAllOp = 0x03,
604 SaveSS_OpMask = 0xF,
605 // flags for #saveSnapshotSettings()
606 SaveSS_UpdateCurStateModified = 0x40,
607 SaveSS_UpdateCurrentId = 0x80,
608 // flags for #saveStateSettings()
609 SaveSTS_CurStateModified = 0x20,
610 SaveSTS_StateFilePath = 0x40,
611 SaveSTS_StateTimeStamp = 0x80,
612 };
613
614 HRESULT saveSnapshotSettings (Snapshot *aSnapshot, int aOpFlags);
615 HRESULT saveSnapshotSettingsWorker (CFGNODE aMachineNode,
616 Snapshot *aSnapshot, int aOpFlags);
617
618 HRESULT saveSnapshot (CFGNODE aNode, Snapshot *aSnapshot, bool aAttrsOnly);
619 HRESULT saveHardware (CFGNODE aNode);
620 HRESULT saveHardDisks (CFGNODE aNode);
621
622 HRESULT saveStateSettings (int aFlags);
623
624 HRESULT wipeOutImmutableDiffs();
625
626 HRESULT fixupHardDisks (bool aCommit);
627
628 HRESULT createSnapshotDiffs (const Guid *aSnapshotId,
629 const Bstr &aFolder,
630 const ComObjPtr <Progress> &aProgress,
631 bool aOnline);
632 HRESULT deleteSnapshotDiffs (const ComObjPtr <Snapshot> &aSnapshot);
633
634 HRESULT lockConfig();
635 HRESULT unlockConfig();
636
637 /** @note This method is not thread safe */
638 BOOL isConfigLocked()
639 {
640 return !!mData && mData->mHandleCfgFile != NIL_RTFILE;
641 }
642
643 bool isInOwnDir (Utf8Str *aSettingsDir = NULL);
644
645 bool isModified();
646 bool isReallyModified (bool aIgnoreUserData = false);
647 void rollback (bool aNotify);
648 HRESULT commit();
649 void copyFrom (Machine *aThat);
650
651 const InstanceType mType;
652
653 const ComObjPtr <Machine, ComWeakRef> mPeer;
654
655 const ComObjPtr <VirtualBox, ComWeakRef> mParent;
656
657 Shareable <Data> mData;
658 Shareable <SSData> mSSData;
659
660 Backupable <UserData> mUserData;
661 Backupable <HWData> mHWData;
662 Backupable <HDData> mHDData;
663
664 // the following fields need special backup/rollback/commit handling,
665 // so they cannot be a part of HWData
666
667 const ComObjPtr <VRDPServer> mVRDPServer;
668 const ComObjPtr <DVDDrive> mDVDDrive;
669 const ComObjPtr <FloppyDrive> mFloppyDrive;
670 const ComObjPtr <AudioAdapter> mAudioAdapter;
671 const ComObjPtr <USBController> mUSBController;
672 const ComObjPtr <BIOSSettings> mBIOSSettings;
673
674 const ComObjPtr <NetworkAdapter>
675 mNetworkAdapters [SchemaDefs::NetworkAdapterCount];
676
677 friend class SessionMachine;
678 friend class SnapshotMachine;
679};
680
681// SessionMachine class
682////////////////////////////////////////////////////////////////////////////////
683
684/**
685 * @note Notes on locking objects of this class:
686 * SessionMachine shares some data with the primary Machine instance (pointed
687 * to by the |mPeer| member). In order to provide data consistency it also
688 * shares its lock handle. This means that whenever you lock a SessionMachine
689 * instance using Auto[Reader]Lock or AutoMultiLock, the corresponding Machine
690 * instance is also locked in the same lock mode. Keep it in mind.
691 */
692class ATL_NO_VTABLE SessionMachine :
693 public VirtualBoxSupportTranslation <SessionMachine>,
694 public Machine,
695 public IInternalMachineControl
696{
697public:
698
699 VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE(SessionMachine)
700
701 DECLARE_NOT_AGGREGATABLE(SessionMachine)
702
703 DECLARE_PROTECT_FINAL_CONSTRUCT()
704
705 BEGIN_COM_MAP(SessionMachine)
706 COM_INTERFACE_ENTRY(ISupportErrorInfo)
707 COM_INTERFACE_ENTRY(IMachine)
708 COM_INTERFACE_ENTRY(IInternalMachineControl)
709 END_COM_MAP()
710
711 NS_DECL_ISUPPORTS
712
713 DECLARE_EMPTY_CTOR_DTOR (SessionMachine)
714
715 HRESULT FinalConstruct();
716 void FinalRelease();
717
718 // public initializer/uninitializer for internal purposes only
719 HRESULT init (Machine *aMachine);
720 void uninit() { uninit (Uninit::Unexpected); }
721
722 // AutoLock::Lockable interface
723 AutoLock::Handle *lockHandle() const;
724
725 // IInternalMachineControl methods
726 STDMETHOD(UpdateState)(MachineState_T machineState);
727 STDMETHOD(GetIPCId)(BSTR *id);
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