VirtualBox

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

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

replace underscore symbols in Main/

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