VirtualBox

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

Last change on this file since 17982 was 17911, checked in by vboxsync, 16 years ago

#3686: “Main: fix unused var warnings”

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