VirtualBox

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

Last change on this file since 14475 was 13962, checked in by vboxsync, 16 years ago

Added Accelerate3D xml setting and IMachine property.

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