VirtualBox

source: vbox/trunk/src/VBox/Main/include/VirtualBoxImpl.h@ 5606

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

Main: Implemented IConsole::AdoptSavedState(); Prototyped IVirtualBox::WaitForPropertyChange().

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.5 KB
Line 
1/** @file
2 *
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#ifndef ____H_VIRTUALBOXIMPL
19#define ____H_VIRTUALBOXIMPL
20
21#include "VirtualBoxBase.h"
22#include "VirtualBoxXMLUtil.h"
23
24#include "VBox/com/EventQueue.h"
25
26#include <VBox/cfgldr.h>
27
28#include <list>
29#include <vector>
30#include <map>
31
32#ifdef RT_OS_WINDOWS
33#include "win32/resource.h"
34#endif
35
36class Machine;
37class SessionMachine;
38class HardDisk;
39class HVirtualDiskImage;
40class DVDImage;
41class FloppyImage;
42class MachineCollection;
43class HardDiskCollection;
44class DVDImageCollection;
45class FloppyImageCollection;
46class GuestOSType;
47class GuestOSTypeCollection;
48class SharedFolder;
49class Progress;
50class ProgressCollection;
51class Host;
52class SystemProperties;
53
54#ifdef RT_OS_WINDOWS
55class SVCHlpClient;
56#endif
57
58struct VMClientWatcherData;
59
60class ATL_NO_VTABLE VirtualBox :
61 public VirtualBoxBaseWithChildrenNEXT,
62 public VirtualBoxXMLUtil,
63 public VirtualBoxSupportErrorInfoImpl <VirtualBox, IVirtualBox>,
64 public VirtualBoxSupportTranslation <VirtualBox>,
65#ifdef RT_OS_WINDOWS
66 public IDispatchImpl<IVirtualBox, &IID_IVirtualBox, &LIBID_VirtualBox,
67 kTypeLibraryMajorVersion, kTypeLibraryMinorVersion>,
68 public CComCoClass<VirtualBox, &CLSID_VirtualBox>
69#else
70 public IVirtualBox
71#endif
72{
73
74public:
75
76 VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT (VirtualBox)
77
78 typedef std::list <ComPtr <IVirtualBoxCallback> > CallbackList;
79 typedef std::vector <ComPtr <IVirtualBoxCallback> > CallbackVector;
80
81 class CallbackEvent;
82 friend class CallbackEvent;
83
84 DECLARE_CLASSFACTORY_SINGLETON(VirtualBox)
85
86 DECLARE_REGISTRY_RESOURCEID(IDR_VIRTUALBOX)
87 DECLARE_NOT_AGGREGATABLE(VirtualBox)
88
89 DECLARE_PROTECT_FINAL_CONSTRUCT()
90
91 BEGIN_COM_MAP(VirtualBox)
92 COM_INTERFACE_ENTRY(IDispatch)
93 COM_INTERFACE_ENTRY(ISupportErrorInfo)
94 COM_INTERFACE_ENTRY(IVirtualBox)
95 END_COM_MAP()
96
97 NS_DECL_ISUPPORTS
98
99 /* to postpone generation of the default ctor/dtor */
100 VirtualBox();
101 ~VirtualBox();
102
103 HRESULT FinalConstruct();
104 void FinalRelease();
105
106 /* public initializer/uninitializer for internal purposes only */
107 HRESULT init();
108 void uninit();
109
110 /* IVirtualBox properties */
111 STDMETHOD(COMGETTER(Version)) (BSTR *aVersion);
112 STDMETHOD(COMGETTER(HomeFolder)) (BSTR *aHomeFolder);
113 STDMETHOD(COMGETTER(Host)) (IHost **aHost);
114 STDMETHOD(COMGETTER(SystemProperties)) (ISystemProperties **aSystemProperties);
115 STDMETHOD(COMGETTER(Machines)) (IMachineCollection **aMachines);
116 STDMETHOD(COMGETTER(HardDisks)) (IHardDiskCollection **aHardDisks);
117 STDMETHOD(COMGETTER(DVDImages)) (IDVDImageCollection **aDVDImages);
118 STDMETHOD(COMGETTER(FloppyImages)) (IFloppyImageCollection **aFloppyImages);
119 STDMETHOD(COMGETTER(ProgressOperations)) (IProgressCollection **aOperations);
120 STDMETHOD(COMGETTER(GuestOSTypes)) (IGuestOSTypeCollection **aGuestOSTypes);
121 STDMETHOD(COMGETTER(SharedFolders)) (ISharedFolderCollection **aSharedFolders);
122
123 /* IVirtualBox methods */
124
125 STDMETHOD(CreateMachine) (INPTR BSTR aBaseFolder, INPTR BSTR aName,
126 INPTR GUIDPARAM aId, IMachine **aMachine);
127 STDMETHOD(CreateLegacyMachine) (INPTR BSTR aSettingsFile, INPTR BSTR aName,
128 INPTR GUIDPARAM aId, IMachine **aMachine);
129 STDMETHOD(OpenMachine) (INPTR BSTR aSettingsFile, IMachine **aMachine);
130 STDMETHOD(RegisterMachine) (IMachine *aMachine);
131 STDMETHOD(GetMachine) (INPTR GUIDPARAM aId, IMachine **aMachine);
132 STDMETHOD(FindMachine) (INPTR BSTR aName, IMachine **aMachine);
133 STDMETHOD(UnregisterMachine) (INPTR GUIDPARAM aId, IMachine **aMachine);
134
135 STDMETHOD(CreateHardDisk) (HardDiskStorageType_T aStorageType, IHardDisk **aHardDisk);
136 STDMETHOD(OpenHardDisk) (INPTR BSTR aLocation, IHardDisk **aHardDisk);
137 STDMETHOD(OpenVirtualDiskImage) (INPTR BSTR aFilePath, IVirtualDiskImage **aImage);
138 STDMETHOD(RegisterHardDisk) (IHardDisk *aHardDisk);
139 STDMETHOD(GetHardDisk) (INPTR GUIDPARAM aId, IHardDisk **aHardDisk);
140 STDMETHOD(FindHardDisk) (INPTR BSTR aLocation, IHardDisk **aHardDisk);
141 STDMETHOD(FindVirtualDiskImage) (INPTR BSTR aFilePath, IVirtualDiskImage **aImage);
142 STDMETHOD(UnregisterHardDisk) (INPTR GUIDPARAM aId, IHardDisk **aHardDisk);
143
144 STDMETHOD(OpenDVDImage) (INPTR BSTR aFilePath, INPTR GUIDPARAM aId,
145 IDVDImage **aDVDImage);
146 STDMETHOD(RegisterDVDImage) (IDVDImage *aDVDImage);
147 STDMETHOD(GetDVDImage) (INPTR GUIDPARAM aId, IDVDImage **aDVDImage);
148 STDMETHOD(FindDVDImage) (INPTR BSTR aFilePath, IDVDImage **aDVDImage);
149 STDMETHOD(GetDVDImageUsage) (INPTR GUIDPARAM aId,
150 ResourceUsage_T aUsage,
151 BSTR *aMachineIDs);
152 STDMETHOD(UnregisterDVDImage) (INPTR GUIDPARAM aId, IDVDImage **aDVDImage);
153
154 STDMETHOD(OpenFloppyImage) (INPTR BSTR aFilePath, INPTR GUIDPARAM aId,
155 IFloppyImage **aFloppyImage);
156 STDMETHOD(RegisterFloppyImage) (IFloppyImage *aFloppyImage);
157 STDMETHOD(GetFloppyImage) (INPTR GUIDPARAM id, IFloppyImage **aFloppyImage);
158 STDMETHOD(FindFloppyImage) (INPTR BSTR aFilePath, IFloppyImage **aFloppyImage);
159 STDMETHOD(GetFloppyImageUsage) (INPTR GUIDPARAM aId,
160 ResourceUsage_T aUsage,
161 BSTR *aMachineIDs);
162 STDMETHOD(UnregisterFloppyImage) (INPTR GUIDPARAM aId, IFloppyImage **aFloppyImage);
163
164 STDMETHOD(GetGuestOSType) (INPTR BSTR aId, IGuestOSType **aType);
165 STDMETHOD(CreateSharedFolder) (INPTR BSTR aName, INPTR BSTR aHostPath);
166 STDMETHOD(RemoveSharedFolder) (INPTR BSTR aName);
167 STDMETHOD(GetNextExtraDataKey) (INPTR BSTR aKey, BSTR *aNextKey, BSTR *aNextValue);
168 STDMETHOD(GetExtraData) (INPTR BSTR aKey, BSTR *aValue);
169 STDMETHOD(SetExtraData) (INPTR BSTR aKey, INPTR BSTR aValue);
170 STDMETHOD(OpenSession) (ISession *aSession, INPTR GUIDPARAM aMachineId);
171 STDMETHOD(OpenRemoteSession) (ISession *aSession, INPTR GUIDPARAM aMachineId,
172 INPTR BSTR aType, INPTR BSTR aEnvironment,
173 IProgress **aProgress);
174 STDMETHOD(OpenExistingSession) (ISession *aSession, INPTR GUIDPARAM aMachineId);
175
176 STDMETHOD(RegisterCallback) (IVirtualBoxCallback *aCallback);
177 STDMETHOD(UnregisterCallback) (IVirtualBoxCallback *aCallback);
178
179 STDMETHOD(WaitForPropertyChange) (INPTR BSTR aWhat, ULONG aTimeout,
180 BSTR *aChanged, BSTR *aValues);
181
182 /* public methods only for internal purposes */
183
184 HRESULT postEvent (Event *event);
185
186 HRESULT addProgress (IProgress *aProgress);
187 HRESULT removeProgress (INPTR GUIDPARAM aId);
188
189#ifdef RT_OS_WINDOWS
190 typedef DECLCALLBACKPTR (HRESULT, SVCHelperClientFunc)
191 (SVCHlpClient *aClient, Progress *aProgress, void *aUser, int *aVrc);
192 HRESULT startSVCHelperClient (bool aPrivileged,
193 SVCHelperClientFunc aFunc,
194 void *aUser, Progress *aProgress);
195#endif
196
197 void addProcessToReap (RTPROCESS pid);
198 void updateClientWatcher();
199
200 void onMachineStateChange (const Guid &aId, MachineState_T aState);
201 void onMachineDataChange (const Guid &aId);
202 BOOL onExtraDataCanChange(const Guid &aId, INPTR BSTR aKey, INPTR BSTR aValue,
203 Bstr &aError);
204 void onExtraDataChange(const Guid &aId, INPTR BSTR aKey, INPTR BSTR aValue);
205 void onMachineRegistered (const Guid &aId, BOOL aRegistered);
206 void onSessionStateChange (const Guid &aId, SessionState_T aState);
207
208 void onSnapshotTaken (const Guid &aMachineId, const Guid &aSnapshotId);
209 void onSnapshotDiscarded (const Guid &aMachineId, const Guid &aSnapshotId);
210 void onSnapshotChange (const Guid &aMachineId, const Guid &aSnapshotId);
211
212 ComObjPtr <GuestOSType> getUnknownOSType();
213
214 typedef std::vector <ComObjPtr <SessionMachine> > SessionMachineVector;
215 void getOpenedMachines (SessionMachineVector &aVector);
216
217 bool isMachineIdValid (const Guid &aId)
218 {
219 return SUCCEEDED (findMachine (aId, false /* aSetError */, NULL));
220 }
221
222 /// @todo (dmik) remove and make findMachine() public instead
223 // after switching to VirtualBoxBaseNEXT
224 HRESULT getMachine (const Guid &aId, ComObjPtr <Machine> &aMachine,
225 bool aSetError = false)
226 {
227 return findMachine (aId, aSetError, &aMachine);
228 }
229
230 /// @todo (dmik) remove and make findHardDisk() public instead
231 // after switching to VirtualBoxBaseNEXT
232 HRESULT getHardDisk (const Guid &aId, ComObjPtr <HardDisk> &aHardDisk)
233 {
234 return findHardDisk (&aId, NULL, true /* aDoSetError */, &aHardDisk);
235 }
236
237 bool getDVDImageUsage (const Guid &aId, ResourceUsage_T aUsage,
238 Bstr *aMachineIDs = NULL);
239 bool getFloppyImageUsage (const Guid &aId, ResourceUsage_T aUsage,
240 Bstr *aMachineIDs = NULL);
241
242 const ComObjPtr <Host> &host() { return mData.mHost; }
243 const ComObjPtr <SystemProperties> &systemProperties() { return mData.mSystemProperties; }
244
245 /** Returns the VirtualBox home directory */
246 const Utf8Str &homeDir() { return mData.mHomeDir; }
247
248 void calculateRelativePath (const char *aPath, Utf8Str &aResult);
249
250 enum RHD_Flags { RHD_Internal, RHD_External, RHD_OnStartUp };
251 HRESULT registerHardDisk (HardDisk *aHardDisk, RHD_Flags aFlags);
252 HRESULT unregisterHardDisk (HardDisk *aHardDisk);
253 HRESULT unregisterDiffHardDisk (HardDisk *aHardDisk);
254
255 HRESULT saveSettings() { return saveConfig(); }
256 HRESULT updateSettings (const char *aOldPath, const char *aNewPath);
257
258 const Bstr &settingsFileName() { return mData.mCfgFile.mName; }
259
260 /* for VirtualBoxSupportErrorInfoImpl */
261 static const wchar_t *getComponentName() { return L"VirtualBox"; }
262
263private:
264
265 typedef std::list <ComObjPtr <Machine> > MachineList;
266 typedef std::list <ComObjPtr <GuestOSType> > GuestOSTypeList;
267 typedef std::list <ComPtr <IProgress> > ProgressList;
268
269 typedef std::list <ComObjPtr <HardDisk> > HardDiskList;
270 typedef std::list <ComObjPtr <DVDImage> > DVDImageList;
271 typedef std::list <ComObjPtr <FloppyImage> > FloppyImageList;
272 typedef std::list <ComObjPtr <SharedFolder> > SharedFolderList;
273
274 typedef std::map <Guid, ComObjPtr <HardDisk> > HardDiskMap;
275
276 HRESULT findMachine (const Guid &aId, bool aSetError,
277 ComObjPtr <Machine> *machine = NULL);
278
279 HRESULT findHardDisk (const Guid *aId, const BSTR aLocation,
280 bool aSetError, ComObjPtr <HardDisk> *aHardDisk = NULL);
281
282 HRESULT findVirtualDiskImage (const Guid *aId, const BSTR aFilePathFull,
283 bool aSetError, ComObjPtr <HVirtualDiskImage> *aImage = NULL);
284 HRESULT findDVDImage (const Guid *aId, const BSTR aFilePathFull,
285 bool aSetError, ComObjPtr <DVDImage> *aImage = NULL);
286 HRESULT findFloppyImage (const Guid *aId, const BSTR aFilePathFull,
287 bool aSetError, ComObjPtr <FloppyImage> *aImage = NULL);
288
289 HRESULT checkMediaForConflicts (HardDisk *aHardDisk,
290 const Guid *aId, const BSTR aFilePathFull);
291
292 HRESULT loadMachines (CFGNODE aGlobal);
293 HRESULT loadDisks (CFGNODE aGlobal);
294 HRESULT loadHardDisks (CFGNODE aNode);
295
296 HRESULT saveConfig();
297 HRESULT saveHardDisks (CFGNODE aNode);
298
299 HRESULT registerMachine (Machine *aMachine);
300
301 HRESULT registerDVDImage (DVDImage *aImage, bool aOnStartUp);
302 HRESULT registerFloppyImage (FloppyImage *aImage, bool aOnStartUp);
303 HRESULT registerGuestOSTypes();
304
305 HRESULT lockConfig();
306 HRESULT unlockConfig();
307
308 /** @note This method is not thread safe */
309 bool isConfigLocked() { return mData.mCfgFile.mHandle != NIL_RTFILE; }
310
311 /**
312 * Main VirtualBox data structure.
313 * @note |const| members are persistent during lifetime so can be accessed
314 * without locking.
315 */
316 struct Data
317 {
318 Data();
319
320 struct CfgFile
321 {
322 CfgFile() : mHandle (NIL_RTFILE) {}
323
324 const Bstr mName;
325 RTFILE mHandle;
326 };
327
328 // const data members not requiring locking
329 const Utf8Str mHomeDir;
330
331 // const objects not requiring locking
332 const ComObjPtr <Host> mHost;
333 const ComObjPtr <SystemProperties> mSystemProperties;
334
335 CfgFile mCfgFile;
336
337 MachineList mMachines;
338 GuestOSTypeList mGuestOSTypes;
339
340 ProgressList mProgressOperations;
341 HardDiskList mHardDisks;
342 DVDImageList mDVDImages;
343 FloppyImageList mFloppyImages;
344 SharedFolderList mSharedFolders;
345
346 HardDiskMap mHardDiskMap;
347
348 CallbackList mCallbacks;
349 };
350
351 Data mData;
352
353 /** Client watcher thread data structure */
354 struct ClientWatcherData
355 {
356 ClientWatcherData()
357#if defined(RT_OS_WINDOWS)
358 : mUpdateReq (NULL)
359#elif defined(RT_OS_OS2)
360 : mUpdateReq (NIL_RTSEMEVENT)
361#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
362 : mUpdateReq (NIL_RTSEMEVENT)
363#else
364# error "Port me!"
365#endif
366 , mThread (NIL_RTTHREAD) {}
367
368 // const objects not requiring locking
369#if defined(RT_OS_WINDOWS)
370 const HANDLE mUpdateReq;
371#elif defined(RT_OS_OS2)
372 const RTSEMEVENT mUpdateReq;
373#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
374 const RTSEMEVENT mUpdateReq;
375#else
376# error "Port me!"
377#endif
378 const RTTHREAD mThread;
379
380 typedef std::list <RTPROCESS> ProcessList;
381 ProcessList mProcesses;
382 };
383
384 ClientWatcherData mWatcherData;
385
386 const RTTHREAD mAsyncEventThread;
387 EventQueue * const mAsyncEventQ;
388 /** Lock for calling EventQueue->post() */
389 AutoLock::Handle mAsyncEventQLock;
390
391 static Bstr sVersion;
392
393 static DECLCALLBACK(int) ClientWatcher (RTTHREAD thread, void *pvUser);
394 static DECLCALLBACK(int) AsyncEventHandler (RTTHREAD thread, void *pvUser);
395
396#ifdef RT_OS_WINDOWS
397 static DECLCALLBACK(int) SVCHelperClientThread (RTTHREAD aThread, void *aUser);
398#endif
399};
400
401////////////////////////////////////////////////////////////////////////////////
402
403/**
404 * Abstract callback event class to asynchronously call VirtualBox callbacks
405 * on a dedicated event thread. Subclasses reimplement #handleCallback()
406 * to call appropriate IVirtualBoxCallback methods depending on the event
407 * to be dispatched.
408 *
409 * @note The VirtualBox instance passed to the constructor is strongly
410 * referenced, so that the VirtualBox singleton won't be released until the
411 * event gets handled by the event thread.
412 */
413class VirtualBox::CallbackEvent : public Event
414{
415public:
416
417 CallbackEvent (VirtualBox *aVirtualBox) : mVirtualBox (aVirtualBox)
418 {
419 Assert (aVirtualBox);
420 }
421
422 void *handler();
423
424 virtual void handleCallback (const ComPtr <IVirtualBoxCallback> &aCallback) = 0;
425
426private:
427
428 /*
429 * Note that this is a weak ref -- the CallbackEvent handler thread
430 * is bound to the lifetime of the VirtualBox instance, so it's safe.
431 */
432 ComObjPtr <VirtualBox, ComWeakRef> mVirtualBox;
433};
434
435#endif // ____H_VIRTUALBOXIMPL
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