VirtualBox

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

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

Main/Frontends: Pass the value of the DISPLAY variable of the starting application to the VM process (#2101).

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette