VirtualBox

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

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

Biggest check-in ever. New source code headers for all (C) innotek files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.3 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 IMachine **aMachine);
127 STDMETHOD(CreateLegacyMachine) (INPTR BSTR aSettingsFile, INPTR BSTR aName,
128 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 STDMETHOD(RegisterCallback) (IVirtualBoxCallback *callback);
176 STDMETHOD(UnregisterCallback) (IVirtualBoxCallback *callback);
177
178 /* public methods only for internal purposes */
179
180 HRESULT postEvent (Event *event);
181
182 HRESULT addProgress (IProgress *aProgress);
183 HRESULT removeProgress (INPTR GUIDPARAM aId);
184
185#ifdef RT_OS_WINDOWS
186 typedef DECLCALLBACKPTR (HRESULT, SVCHelperClientFunc)
187 (SVCHlpClient *aClient, Progress *aProgress, void *aUser, int *aVrc);
188 HRESULT startSVCHelperClient (bool aPrivileged,
189 SVCHelperClientFunc aFunc,
190 void *aUser, Progress *aProgress);
191#endif
192
193 void addProcessToReap (RTPROCESS pid);
194 void updateClientWatcher();
195
196 void onMachineStateChange (const Guid &aId, MachineState_T aState);
197 void onMachineDataChange (const Guid &aId);
198 BOOL onExtraDataCanChange(const Guid &aId, INPTR BSTR aKey, INPTR BSTR aValue,
199 Bstr &aError);
200 void onExtraDataChange(const Guid &aId, INPTR BSTR aKey, INPTR BSTR aValue);
201 void onMachineRegistered (const Guid &aId, BOOL aRegistered);
202 void onSessionStateChange (const Guid &aId, SessionState_T aState);
203
204 void onSnapshotTaken (const Guid &aMachineId, const Guid &aSnapshotId);
205 void onSnapshotDiscarded (const Guid &aMachineId, const Guid &aSnapshotId);
206 void onSnapshotChange (const Guid &aMachineId, const Guid &aSnapshotId);
207
208 ComObjPtr <GuestOSType> getUnknownOSType();
209
210 typedef std::vector <ComObjPtr <SessionMachine> > SessionMachineVector;
211 void getOpenedMachines (SessionMachineVector &aVector);
212
213 bool isMachineIdValid (const Guid &aId)
214 {
215 return SUCCEEDED (findMachine (aId, false /* aSetError */, NULL));
216 }
217
218 /// @todo (dmik) remove and make findMachine() public instead
219 // after switching to VirtualBoxBaseNEXT
220 HRESULT getMachine (const Guid &aId, ComObjPtr <Machine> &aMachine,
221 bool aSetError = false)
222 {
223 return findMachine (aId, aSetError, &aMachine);
224 }
225
226 /// @todo (dmik) remove and make findHardDisk() public instead
227 // after switching to VirtualBoxBaseNEXT
228 HRESULT getHardDisk (const Guid &aId, ComObjPtr <HardDisk> &aHardDisk)
229 {
230 return findHardDisk (&aId, NULL, true /* aDoSetError */, &aHardDisk);
231 }
232
233 BOOL getDVDImageUsage (const Guid &id, ResourceUsage_T usage,
234 Bstr *machineIDs = NULL);
235 BOOL getFloppyImageUsage (const Guid &id, ResourceUsage_T usage,
236 Bstr *machineIDs = NULL);
237
238 const ComObjPtr <Host> &host() { return mData.mHost; }
239 const ComObjPtr <SystemProperties> &systemProperties() { return mData.mSystemProperties; }
240
241 /** Returns the VirtualBox home directory */
242 const Utf8Str &homeDir() { return mData.mHomeDir; }
243
244 void calculateRelativePath (const char *aPath, Utf8Str &aResult);
245
246 enum RHD_Flags { RHD_Internal, RHD_External, RHD_OnStartUp };
247 HRESULT registerHardDisk (HardDisk *aHardDisk, RHD_Flags aFlags);
248 HRESULT unregisterHardDisk (HardDisk *aHardDisk);
249 HRESULT unregisterDiffHardDisk (HardDisk *aHardDisk);
250
251 HRESULT saveSettings() { return saveConfig(); }
252 HRESULT updateSettings (const char *aOldPath, const char *aNewPath);
253
254 const Bstr &settingsFileName() { return mData.mCfgFile.mName; }
255
256 /* for VirtualBoxSupportErrorInfoImpl */
257 static const wchar_t *getComponentName() { return L"VirtualBox"; }
258
259private:
260
261 typedef std::list <ComObjPtr <Machine> > MachineList;
262 typedef std::list <ComObjPtr <GuestOSType> > GuestOSTypeList;
263 typedef std::list <ComPtr <IProgress> > ProgressList;
264
265 typedef std::list <ComObjPtr <HardDisk> > HardDiskList;
266 typedef std::list <ComObjPtr <DVDImage> > DVDImageList;
267 typedef std::list <ComObjPtr <FloppyImage> > FloppyImageList;
268 typedef std::list <ComObjPtr <SharedFolder> > SharedFolderList;
269
270 typedef std::map <Guid, ComObjPtr <HardDisk> > HardDiskMap;
271
272 HRESULT findMachine (const Guid &aId, bool aSetError,
273 ComObjPtr <Machine> *machine = NULL);
274
275 HRESULT findHardDisk (const Guid *aId, const BSTR aLocation,
276 bool aSetError, ComObjPtr <HardDisk> *aHardDisk = NULL);
277
278 HRESULT findVirtualDiskImage (const Guid *aId, const BSTR aFilePathFull,
279 bool aSetError, ComObjPtr <HVirtualDiskImage> *aImage = NULL);
280 HRESULT findDVDImage (const Guid *aId, const BSTR aFilePathFull,
281 bool aSetError, ComObjPtr <DVDImage> *aImage = NULL);
282 HRESULT findFloppyImage (const Guid *aId, const BSTR aFilePathFull,
283 bool aSetError, ComObjPtr <FloppyImage> *aImage = NULL);
284
285 HRESULT checkMediaForConflicts (HardDisk *aHardDisk,
286 const Guid *aId, const BSTR aFilePathFull);
287
288 HRESULT loadMachines (CFGNODE aGlobal);
289 HRESULT loadDisks (CFGNODE aGlobal);
290 HRESULT loadHardDisks (CFGNODE aNode);
291
292 HRESULT saveConfig();
293 HRESULT saveHardDisks (CFGNODE aNode);
294
295 HRESULT registerMachine (Machine *aMachine);
296
297 HRESULT registerDVDImage (DVDImage *aImage, bool aOnStartUp);
298 HRESULT registerFloppyImage (FloppyImage *aImage, bool aOnStartUp);
299 HRESULT registerGuestOSTypes();
300
301 HRESULT lockConfig();
302 HRESULT unlockConfig();
303
304 /** @note This method is not thread safe */
305 bool isConfigLocked() { return mData.mCfgFile.mHandle != NIL_RTFILE; }
306
307 /**
308 * Main VirtualBox data structure.
309 * @note |const| members are persistent during lifetime so can be accessed
310 * without locking.
311 */
312 struct Data
313 {
314 Data();
315
316 struct CfgFile
317 {
318 CfgFile() : mHandle (NIL_RTFILE) {}
319
320 const Bstr mName;
321 RTFILE mHandle;
322 };
323
324 // const data members not requiring locking
325 const Utf8Str mHomeDir;
326
327 // const objects not requiring locking
328 const ComObjPtr <Host> mHost;
329 const ComObjPtr <SystemProperties> mSystemProperties;
330
331 CfgFile mCfgFile;
332
333 MachineList mMachines;
334 GuestOSTypeList mGuestOSTypes;
335
336 ProgressList mProgressOperations;
337 HardDiskList mHardDisks;
338 DVDImageList mDVDImages;
339 FloppyImageList mFloppyImages;
340 SharedFolderList mSharedFolders;
341
342 HardDiskMap mHardDiskMap;
343
344 CallbackList mCallbacks;
345 };
346
347 Data mData;
348
349 /** Client watcher thread data structure */
350 struct ClientWatcherData
351 {
352 ClientWatcherData()
353#if defined(RT_OS_WINDOWS)
354 : mUpdateReq (NULL)
355#elif defined(RT_OS_OS2)
356 : mUpdateReq (NIL_RTSEMEVENT)
357#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
358 : mUpdateReq (NIL_RTSEMEVENT)
359#else
360# error "Port me!"
361#endif
362 , mThread (NIL_RTTHREAD) {}
363
364 // const objects not requiring locking
365#if defined(RT_OS_WINDOWS)
366 const HANDLE mUpdateReq;
367#elif defined(RT_OS_OS2)
368 const RTSEMEVENT mUpdateReq;
369#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
370 const RTSEMEVENT mUpdateReq;
371#else
372# error "Port me!"
373#endif
374 const RTTHREAD mThread;
375
376 typedef std::list <RTPROCESS> ProcessList;
377 ProcessList mProcesses;
378 };
379
380 ClientWatcherData mWatcherData;
381
382 const RTTHREAD mAsyncEventThread;
383 EventQueue * const mAsyncEventQ;
384 /** Lock for calling EventQueue->post() */
385 AutoLock::Handle mAsyncEventQLock;
386
387 static Bstr sVersion;
388
389 static DECLCALLBACK(int) ClientWatcher (RTTHREAD thread, void *pvUser);
390 static DECLCALLBACK(int) AsyncEventHandler (RTTHREAD thread, void *pvUser);
391
392#ifdef RT_OS_WINDOWS
393 static DECLCALLBACK(int) SVCHelperClientThread (RTTHREAD aThread, void *aUser);
394#endif
395};
396
397////////////////////////////////////////////////////////////////////////////////
398
399/**
400 * Abstract callback event class to asynchronously call VirtualBox callbacks
401 * on a dedicated event thread. Subclasses reimplement #handleCallback()
402 * to call appropriate IVirtualBoxCallback methods depending on the event
403 * to be dispatched.
404 *
405 * @note The VirtualBox instance passed to the constructor is strongly
406 * referenced, so that the VirtualBox singleton won't be released until the
407 * event gets handled by the event thread.
408 */
409class VirtualBox::CallbackEvent : public Event
410{
411public:
412
413 CallbackEvent (VirtualBox *aVirtualBox) : mVirtualBox (aVirtualBox)
414 {
415 Assert (aVirtualBox);
416 }
417
418 void *handler();
419
420 virtual void handleCallback (const ComPtr <IVirtualBoxCallback> &aCallback) = 0;
421
422private:
423
424 /*
425 * Note that this is a weak ref -- the CallbackEvent handler thread
426 * is bound to the lifetime of the VirtualBox instance, so it's safe.
427 */
428 ComObjPtr <VirtualBox, ComWeakRef> mVirtualBox;
429};
430
431#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