VirtualBox

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

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

The Big Sun Rebranding Header Change

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