VirtualBox

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

Last change on this file since 10770 was 10693, checked in by vboxsync, 16 years ago

Added setting for nested paging (default on).

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