VirtualBox

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

Last change on this file since 10898 was 10868, checked in by vboxsync, 16 years ago

PerfAPI: Windows collector re-worked to extract raw counters via WMI. Filtering for queries added. Security initialization is added to svcmain to access perf enumerators.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 35.6 KB
Line 
1/* $Id: MachineImpl.h 10868 2008-07-24 18:34:35Z 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, ULONG64 *aTimestamp, BSTR *aFlags);
526 STDMETHOD(GetGuestPropertyValue) (INPTR BSTR aKey, BSTR *aValue);
527 STDMETHOD(GetGuestPropertyTimestamp) (INPTR BSTR aKey, ULONG64 *aTimestamp);
528 STDMETHOD(SetGuestProperty) (INPTR BSTR aKey, INPTR BSTR aValue, INPTR BSTR aFlags);
529 STDMETHOD(SetGuestPropertyValue) (INPTR BSTR aKey, INPTR BSTR aValue);
530 STDMETHOD(EnumerateGuestProperties) (INPTR BSTR aPattern, ComSafeArrayOut(BSTR, aKeys), ComSafeArrayOut(BSTR, aValues), ComSafeArrayOut(ULONG64, aTimestamps), ComSafeArrayOut(BSTR, aFlags));
531
532 // public methods only for internal purposes
533
534 /// @todo (dmik) add lock and make non-inlined after revising classes
535 // that use it. Note: they should enter Machine lock to keep the returned
536 // information valid!
537 bool isRegistered() { return !!mData->mRegistered; }
538
539 ComObjPtr <SessionMachine> sessionMachine();
540
541 /**
542 * Returns the VirtualBox object this machine belongs to.
543 *
544 * @note This method doesn't check this object's readiness as it is
545 * intended to be used only by Machine children where it is guaranteed
546 * that this object still exists in memory.
547 */
548 const ComObjPtr <VirtualBox, ComWeakRef> &virtualBox() const { return mParent; }
549
550 /**
551 * Returns this machine's name.
552 *
553 * @note This method doesn't check this object's readiness as it is
554 * intended to be used only after adding a caller to this object (that
555 * guarantees that the object is ready or at least limited).
556 */
557 const Guid &uuid() const { return mData->mUuid; }
558
559 /**
560 * Returns this machine's full settings file path.
561 *
562 * @note This method doesn't lock this object or check its readiness as
563 * it is intended to be used only after adding a caller to this object
564 * (that guarantees that the object is ready) and locking it for reading.
565 */
566 const Bstr &settingsFileFull() const { return mData->mConfigFileFull; }
567
568 /**
569 * Returns this machine's name.
570 *
571 * @note This method doesn't lock this object or check its readiness as
572 * it is intended to be used only after adding a caller to this object
573 * (that guarantees that the object is ready) and locking it for reading.
574 */
575 const Bstr &name() const { return mUserData->mName; }
576
577 // callback handlers
578 virtual HRESULT onDVDDriveChange() { return S_OK; }
579 virtual HRESULT onFloppyDriveChange() { return S_OK; }
580 virtual HRESULT onNetworkAdapterChange(INetworkAdapter *networkAdapter) { return S_OK; }
581 virtual HRESULT onSerialPortChange(ISerialPort *serialPort) { return S_OK; }
582 virtual HRESULT onParallelPortChange(IParallelPort *ParallelPort) { return S_OK; }
583 virtual HRESULT onVRDPServerChange() { return S_OK; }
584 virtual HRESULT onUSBControllerChange() { return S_OK; }
585 virtual HRESULT onSATAControllerChange() { return S_OK; }
586 virtual HRESULT onSharedFolderChange() { return S_OK; }
587
588 HRESULT saveRegistryEntry (settings::Key &aEntryNode);
589
590 int calculateFullPath (const char *aPath, Utf8Str &aResult);
591 void calculateRelativePath (const char *aPath, Utf8Str &aResult);
592
593 void getLogFolder (Utf8Str &aLogFolder);
594
595 bool isDVDImageUsed (const Guid &aId, ResourceUsage_T aUsage);
596 bool isFloppyImageUsed (const Guid &aId, ResourceUsage_T aUsage);
597
598 HRESULT openSession (IInternalSessionControl *aControl);
599 HRESULT openRemoteSession (IInternalSessionControl *aControl,
600 INPTR BSTR aType, INPTR BSTR aEnvironment,
601 Progress *aProgress);
602 HRESULT openExistingSession (IInternalSessionControl *aControl);
603
604 HRESULT trySetRegistered (BOOL aRegistered);
605
606 HRESULT getSharedFolder (const BSTR aName,
607 ComObjPtr <SharedFolder> &aSharedFolder,
608 bool aSetError = false)
609 {
610 AutoWriteLock alock (this);
611 return findSharedFolder (aName, aSharedFolder, aSetError);
612 }
613
614 HRESULT addStateDependency (StateDependency aDepType = AnyStateDep,
615 MachineState_T *aState = NULL,
616 BOOL *aRegistered = NULL);
617 void releaseStateDependency();
618
619 // for VirtualBoxSupportErrorInfoImpl
620 static const wchar_t *getComponentName() { return L"Machine"; }
621
622protected:
623
624 enum InstanceType { IsMachine, IsSessionMachine, IsSnapshotMachine };
625
626 HRESULT registeredInit();
627
628 HRESULT checkStateDependency (StateDependency aDepType);
629
630 inline Machine *machine();
631
632 HRESULT initDataAndChildObjects();
633 void uninitDataAndChildObjects();
634
635 void ensureNoStateDependencies (AutoWriteLock &aLock);
636
637 virtual HRESULT setMachineState (MachineState_T aMachineState);
638
639 HRESULT findSharedFolder (const BSTR aName,
640 ComObjPtr <SharedFolder> &aSharedFolder,
641 bool aSetError = false);
642
643 HRESULT loadSettings (bool aRegistered);
644 HRESULT loadSnapshot (const settings::Key &aNode, const Guid &aCurSnapshotId,
645 Snapshot *aParentSnapshot);
646 HRESULT loadHardware (const settings::Key &aNode);
647 HRESULT loadHardDisks (const settings::Key &aNode, bool aRegistered,
648 const Guid *aSnapshotId = NULL);
649
650 HRESULT findSnapshotNode (Snapshot *aSnapshot, settings::Key &aMachineNode,
651 settings::Key *aSnapshotsNode,
652 settings::Key *aSnapshotNode);
653
654 HRESULT findSnapshot (const Guid &aId, ComObjPtr <Snapshot> &aSnapshot,
655 bool aSetError = false);
656 HRESULT findSnapshot (const BSTR aName, ComObjPtr <Snapshot> &aSnapshot,
657 bool aSetError = false);
658
659 HRESULT findHardDiskAttachment (const ComObjPtr <HardDisk> &aHd,
660 ComObjPtr <Machine> *aMachine,
661 ComObjPtr <Snapshot> *aSnapshot,
662 ComObjPtr <HardDiskAttachment> *aHda);
663
664 HRESULT prepareSaveSettings (bool &aRenamed, bool &aNew);
665 HRESULT saveSettings (bool aMarkCurStateAsModified = true,
666 bool aInformCallbacksAnyway = false);
667
668 enum
669 {
670 // ops for #saveSnapshotSettings()
671 SaveSS_NoOp = 0x00, SaveSS_AddOp = 0x01,
672 SaveSS_UpdateAttrsOp = 0x02, SaveSS_UpdateAllOp = 0x03,
673 SaveSS_OpMask = 0xF,
674 // flags for #saveSnapshotSettings()
675 SaveSS_UpdateCurStateModified = 0x40,
676 SaveSS_UpdateCurrentId = 0x80,
677 // flags for #saveStateSettings()
678 SaveSTS_CurStateModified = 0x20,
679 SaveSTS_StateFilePath = 0x40,
680 SaveSTS_StateTimeStamp = 0x80,
681 };
682
683 HRESULT saveSnapshotSettings (Snapshot *aSnapshot, int aOpFlags);
684 HRESULT saveSnapshotSettingsWorker (settings::Key &aMachineNode,
685 Snapshot *aSnapshot, int aOpFlags);
686
687 HRESULT saveSnapshot (settings::Key &aNode, Snapshot *aSnapshot, bool aAttrsOnly);
688 HRESULT saveHardware (settings::Key &aNode);
689 HRESULT saveHardDisks (settings::Key &aNode);
690
691 HRESULT saveStateSettings (int aFlags);
692
693 HRESULT wipeOutImmutableDiffs();
694
695 HRESULT fixupHardDisks (bool aCommit);
696
697 HRESULT createSnapshotDiffs (const Guid *aSnapshotId,
698 const Bstr &aFolder,
699 const ComObjPtr <Progress> &aProgress,
700 bool aOnline);
701 HRESULT deleteSnapshotDiffs (const ComObjPtr <Snapshot> &aSnapshot);
702
703 HRESULT lockConfig();
704 HRESULT unlockConfig();
705
706 /** @note This method is not thread safe */
707 BOOL isConfigLocked()
708 {
709 return !!mData && mData->mHandleCfgFile != NIL_RTFILE;
710 }
711
712 bool isInOwnDir (Utf8Str *aSettingsDir = NULL);
713
714 bool isModified();
715 bool isReallyModified (bool aIgnoreUserData = false);
716 void rollback (bool aNotify);
717 HRESULT commit();
718 void copyFrom (Machine *aThat);
719
720#ifdef VBOX_WITH_RESOURCE_USAGE_API
721 void registerMetrics (PerformanceCollector *aCollector, RTPROCESS pid);
722 void unregisterMetrics (PerformanceCollector *aCollector);
723#endif /* VBOX_WITH_RESOURCE_USAGE_API */
724
725 const InstanceType mType;
726
727 const ComObjPtr <Machine, ComWeakRef> mPeer;
728
729 const ComObjPtr <VirtualBox, ComWeakRef> mParent;
730
731 Shareable <Data> mData;
732 Shareable <SSData> mSSData;
733
734 Backupable <UserData> mUserData;
735 Backupable <HWData> mHWData;
736 Backupable <HDData> mHDData;
737
738 // the following fields need special backup/rollback/commit handling,
739 // so they cannot be a part of HWData
740
741 const ComObjPtr <VRDPServer> mVRDPServer;
742 const ComObjPtr <DVDDrive> mDVDDrive;
743 const ComObjPtr <FloppyDrive> mFloppyDrive;
744 const ComObjPtr <SerialPort>
745 mSerialPorts [SchemaDefs::SerialPortCount];
746 const ComObjPtr <ParallelPort>
747 mParallelPorts [SchemaDefs::ParallelPortCount];
748 const ComObjPtr <AudioAdapter> mAudioAdapter;
749 const ComObjPtr <USBController> mUSBController;
750 const ComObjPtr <SATAController> mSATAController;
751 const ComObjPtr <BIOSSettings> mBIOSSettings;
752 const ComObjPtr <NetworkAdapter>
753 mNetworkAdapters [SchemaDefs::NetworkAdapterCount];
754
755 friend class SessionMachine;
756 friend class SnapshotMachine;
757};
758
759// SessionMachine class
760////////////////////////////////////////////////////////////////////////////////
761
762/**
763 * @note Notes on locking objects of this class:
764 * SessionMachine shares some data with the primary Machine instance (pointed
765 * to by the |mPeer| member). In order to provide data consistency it also
766 * shares its lock handle. This means that whenever you lock a SessionMachine
767 * instance using Auto[Reader]Lock or AutoMultiLock, the corresponding Machine
768 * instance is also locked in the same lock mode. Keep it in mind.
769 */
770class ATL_NO_VTABLE SessionMachine :
771 public VirtualBoxSupportTranslation <SessionMachine>,
772 public Machine,
773 public IInternalMachineControl
774{
775public:
776
777 VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE(SessionMachine)
778
779 DECLARE_NOT_AGGREGATABLE(SessionMachine)
780
781 DECLARE_PROTECT_FINAL_CONSTRUCT()
782
783 BEGIN_COM_MAP(SessionMachine)
784 COM_INTERFACE_ENTRY(ISupportErrorInfo)
785 COM_INTERFACE_ENTRY(IMachine)
786 COM_INTERFACE_ENTRY(IInternalMachineControl)
787 END_COM_MAP()
788
789 NS_DECL_ISUPPORTS
790
791 DECLARE_EMPTY_CTOR_DTOR (SessionMachine)
792
793 HRESULT FinalConstruct();
794 void FinalRelease();
795
796 // public initializer/uninitializer for internal purposes only
797 HRESULT init (Machine *aMachine);
798 void uninit() { uninit (Uninit::Unexpected); }
799
800 // util::Lockable interface
801 RWLockHandle *lockHandle() const;
802
803 // IInternalMachineControl methods
804 STDMETHOD(UpdateState)(MachineState_T machineState);
805 STDMETHOD(GetIPCId)(BSTR *id);
806 STDMETHOD(RunUSBDeviceFilters) (IUSBDevice *aUSBDevice, BOOL *aMatched, ULONG *aMaskedIfs);
807 STDMETHOD(CaptureUSBDevice) (INPTR GUIDPARAM aId);
808 STDMETHOD(DetachUSBDevice) (INPTR GUIDPARAM aId, BOOL aDone);
809 STDMETHOD(AutoCaptureUSBDevices)();
810 STDMETHOD(DetachAllUSBDevices)(BOOL aDone);
811 STDMETHOD(OnSessionEnd)(ISession *aSession, IProgress **aProgress);
812 STDMETHOD(BeginSavingState) (IProgress *aProgress, BSTR *aStateFilePath);
813 STDMETHOD(EndSavingState) (BOOL aSuccess);
814 STDMETHOD(AdoptSavedState) (INPTR BSTR aSavedStateFile);
815 STDMETHOD(BeginTakingSnapshot) (IConsole *aInitiator,
816 INPTR BSTR aName, INPTR BSTR aDescription,
817 IProgress *aProgress, BSTR *aStateFilePath,
818 IProgress **aServerProgress);
819 STDMETHOD(EndTakingSnapshot) (BOOL aSuccess);
820 STDMETHOD(DiscardSnapshot) (IConsole *aInitiator, INPTR GUIDPARAM aId,
821 MachineState_T *aMachineState, IProgress **aProgress);
822 STDMETHOD(DiscardCurrentState) (
823 IConsole *aInitiator, MachineState_T *aMachineState, IProgress **aProgress);
824 STDMETHOD(DiscardCurrentSnapshotAndState) (
825 IConsole *aInitiator, MachineState_T *aMachineState, IProgress **aProgress);
826
827 // public methods only for internal purposes
828
829 bool checkForDeath();
830
831#if defined (RT_OS_WINDOWS)
832 HANDLE ipcSem() { return mIPCSem; }
833#elif defined (RT_OS_OS2)
834 HMTX ipcSem() { return mIPCSem; }
835#endif
836
837 HRESULT onDVDDriveChange();
838 HRESULT onFloppyDriveChange();
839 HRESULT onNetworkAdapterChange(INetworkAdapter *networkAdapter);
840 HRESULT onSerialPortChange(ISerialPort *serialPort);
841 HRESULT onParallelPortChange(IParallelPort *parallelPort);
842 HRESULT onVRDPServerChange();
843 HRESULT onUSBControllerChange();
844 HRESULT onUSBDeviceAttach (IUSBDevice *aDevice,
845 IVirtualBoxErrorInfo *aError,
846 ULONG aMaskedIfs);
847 HRESULT onUSBDeviceDetach (INPTR GUIDPARAM aId,
848 IVirtualBoxErrorInfo *aError);
849 HRESULT onSharedFolderChange();
850
851 bool hasMatchingUSBFilter (const ComObjPtr <HostUSBDevice> &aDevice, ULONG *aMaskedIfs);
852
853private:
854
855 struct SnapshotData
856 {
857 SnapshotData() : mLastState (MachineState_Null) {}
858
859 MachineState_T mLastState;
860
861 // used when taking snapshot
862 ComObjPtr <Snapshot> mSnapshot;
863 ComObjPtr <Progress> mServerProgress;
864 ComObjPtr <CombinedProgress> mCombinedProgress;
865
866 // used when saving state
867 Guid mProgressId;
868 Bstr mStateFilePath;
869 };
870
871 struct Uninit {
872 enum Reason { Unexpected, Abnormal, Normal };
873 };
874
875 struct Task;
876 struct TakeSnapshotTask;
877 struct DiscardSnapshotTask;
878 struct DiscardCurrentStateTask;
879
880 friend struct TakeSnapshotTask;
881 friend struct DiscardSnapshotTask;
882 friend struct DiscardCurrentStateTask;
883
884 void uninit (Uninit::Reason aReason);
885
886 HRESULT endSavingState (BOOL aSuccess);
887 HRESULT endTakingSnapshot (BOOL aSuccess);
888
889 typedef std::map <ComObjPtr <Machine>, MachineState_T> AffectedMachines;
890
891 void takeSnapshotHandler (TakeSnapshotTask &aTask);
892 void discardSnapshotHandler (DiscardSnapshotTask &aTask);
893 void discardCurrentStateHandler (DiscardCurrentStateTask &aTask);
894
895 HRESULT setMachineState (MachineState_T aMachineState);
896 HRESULT updateMachineStateOnClient();
897
898 SnapshotData mSnapshotData;
899
900 /** interprocess semaphore handle (id) for this machine */
901#if defined(RT_OS_WINDOWS)
902 HANDLE mIPCSem;
903 Bstr mIPCSemName;
904#elif defined(RT_OS_OS2)
905 HMTX mIPCSem;
906 Bstr mIPCSemName;
907#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
908 int mIPCSem;
909#else
910# error "Port me!"
911#endif
912
913 static DECLCALLBACK(int) taskHandler (RTTHREAD thread, void *pvUser);
914};
915
916// SnapshotMachine class
917////////////////////////////////////////////////////////////////////////////////
918
919/**
920 * @note Notes on locking objects of this class:
921 * SnapshotMachine shares some data with the primary Machine instance (pointed
922 * to by the |mPeer| member). In order to provide data consistency it also
923 * shares its lock handle. This means that whenever you lock a SessionMachine
924 * instance using Auto[Reader]Lock or AutoMultiLock, the corresponding Machine
925 * instance is also locked in the same lock mode. Keep it in mind.
926 */
927class ATL_NO_VTABLE SnapshotMachine :
928 public VirtualBoxSupportTranslation <SnapshotMachine>,
929 public Machine
930{
931public:
932
933 VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE(SnapshotMachine)
934
935 DECLARE_NOT_AGGREGATABLE(SnapshotMachine)
936
937 DECLARE_PROTECT_FINAL_CONSTRUCT()
938
939 BEGIN_COM_MAP(SnapshotMachine)
940 COM_INTERFACE_ENTRY(ISupportErrorInfo)
941 COM_INTERFACE_ENTRY(IMachine)
942 END_COM_MAP()
943
944 NS_DECL_ISUPPORTS
945
946 DECLARE_EMPTY_CTOR_DTOR (SnapshotMachine)
947
948 HRESULT FinalConstruct();
949 void FinalRelease();
950
951 // public initializer/uninitializer for internal purposes only
952 HRESULT init (SessionMachine *aSessionMachine,
953 INPTR GUIDPARAM aSnapshotId, INPTR BSTR aStateFilePath);
954 HRESULT init (Machine *aMachine,
955 const settings::Key &aHWNode, const settings::Key &aHDAsNode,
956 INPTR GUIDPARAM aSnapshotId, INPTR BSTR aStateFilePath);
957 void uninit();
958
959 // util::Lockable interface
960 RWLockHandle *lockHandle() const;
961
962 // public methods only for internal purposes
963
964 HRESULT onSnapshotChange (Snapshot *aSnapshot);
965
966private:
967
968 Guid mSnapshotId;
969
970 friend class Snapshot;
971};
972
973////////////////////////////////////////////////////////////////////////////////
974
975/**
976 * Returns a pointer to the Machine object for this machine that acts like a
977 * parent for complex machine data objects such as shared folders, etc.
978 *
979 * For primary Machine objects and for SnapshotMachine objects, returns this
980 * object's pointer itself. For SessoinMachine objects, returns the peer
981 * (primary) machine pointer.
982 */
983inline Machine *Machine::machine()
984{
985 if (mType == IsSessionMachine)
986 return mPeer;
987 return this;
988}
989
990COM_DECL_READONLY_ENUM_AND_COLLECTION (Machine)
991
992#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