VirtualBox

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

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

Continued Main-VMMDev work on memory ballooning.

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