VirtualBox

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

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

Main: more header cleanup

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.5 KB
Line 
1/* $Id: VirtualBoxImpl.h 23319 2009-09-25 09:20:29Z vboxsync $ */
2
3/** @file
4 *
5 * VirtualBox COM class implementation
6 */
7
8/*
9 * Copyright (C) 2006-2009 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_VIRTUALBOXIMPL
25#define ____H_VIRTUALBOXIMPL
26
27#include "VirtualBoxBase.h"
28
29#include <vector>
30
31#ifdef RT_OS_WINDOWS
32# include "win/resource.h"
33#endif
34
35#ifdef VBOX_WITH_RESOURCE_USAGE_API
36#include "PerformanceImpl.h"
37#endif /* VBOX_WITH_RESOURCE_USAGE_API */
38
39namespace com
40{
41 class Event;
42 class EventQueue;
43}
44
45class Machine;
46class SessionMachine;
47class Medium;
48class GuestOSType;
49class SharedFolder;
50class Progress;
51class Host;
52class SystemProperties;
53class DHCPServer;
54
55#ifdef RT_OS_WINDOWS
56class SVCHlpClient;
57#endif
58
59struct VMClientWatcherData;
60
61namespace settings
62{
63 class MainConfigFile;
64}
65
66class ATL_NO_VTABLE VirtualBox :
67 public VirtualBoxBaseWithChildrenNEXT,
68 public VirtualBoxSupportErrorInfoImpl<VirtualBox, IVirtualBox>,
69 public VirtualBoxSupportTranslation<VirtualBox>,
70 VBOX_SCRIPTABLE_IMPL(IVirtualBox)
71#ifdef RT_OS_WINDOWS
72 , public CComCoClass<VirtualBox, &CLSID_VirtualBox>
73#endif
74{
75
76public:
77
78 typedef std::list< ComPtr<IVirtualBoxCallback> > CallbackList;
79 typedef std::vector< ComObjPtr<SessionMachine> > SessionMachineVector;
80 typedef std::vector< ComPtr<IInternalSessionControl> > InternalControlVector;
81
82 class CallbackEvent;
83 friend class CallbackEvent;
84
85 VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT (VirtualBox)
86
87 DECLARE_CLASSFACTORY_SINGLETON(VirtualBox)
88
89 DECLARE_REGISTRY_RESOURCEID(IDR_VIRTUALBOX)
90 DECLARE_NOT_AGGREGATABLE(VirtualBox)
91
92 DECLARE_PROTECT_FINAL_CONSTRUCT()
93
94 BEGIN_COM_MAP(VirtualBox)
95 COM_INTERFACE_ENTRY(IDispatch)
96 COM_INTERFACE_ENTRY(ISupportErrorInfo)
97 COM_INTERFACE_ENTRY(IVirtualBox)
98 END_COM_MAP()
99
100 // to postpone generation of the default ctor/dtor
101 VirtualBox();
102 ~VirtualBox();
103
104 HRESULT FinalConstruct();
105 void FinalRelease();
106
107 /* public initializer/uninitializer for internal purposes only */
108 HRESULT init();
109 HRESULT initMachines();
110 HRESULT initMedia();
111 void uninit();
112
113 /* IVirtualBox properties */
114 STDMETHOD(COMGETTER(Version)) (BSTR *aVersion);
115 STDMETHOD(COMGETTER(Revision)) (ULONG *aRevision);
116 STDMETHOD(COMGETTER(PackageType)) (BSTR *aPackageType);
117 STDMETHOD(COMGETTER(HomeFolder)) (BSTR *aHomeFolder);
118 STDMETHOD(COMGETTER(SettingsFilePath)) (BSTR *aSettingsFilePath);
119 STDMETHOD(COMGETTER(Host)) (IHost **aHost);
120 STDMETHOD(COMGETTER(SystemProperties)) (ISystemProperties **aSystemProperties);
121 STDMETHOD(COMGETTER(Machines)) (ComSafeArrayOut (IMachine *, aMachines));
122 STDMETHOD(COMGETTER(HardDisks)) (ComSafeArrayOut (IMedium *, aHardDisks));
123 STDMETHOD(COMGETTER(DVDImages)) (ComSafeArrayOut (IMedium *, aDVDImages));
124 STDMETHOD(COMGETTER(FloppyImages)) (ComSafeArrayOut (IMedium *, aFloppyImages));
125 STDMETHOD(COMGETTER(ProgressOperations)) (ComSafeArrayOut (IProgress *, aOperations));
126 STDMETHOD(COMGETTER(GuestOSTypes)) (ComSafeArrayOut (IGuestOSType *, aGuestOSTypes));
127 STDMETHOD(COMGETTER(SharedFolders)) (ComSafeArrayOut (ISharedFolder *, aSharedFolders));
128 STDMETHOD(COMGETTER(PerformanceCollector)) (IPerformanceCollector **aPerformanceCollector);
129 STDMETHOD(COMGETTER(DHCPServers)) (ComSafeArrayOut (IDHCPServer *, aDHCPServers));
130
131 /* IVirtualBox methods */
132
133 STDMETHOD(CreateMachine) (IN_BSTR aName, IN_BSTR aOsTypeId, IN_BSTR aBaseFolder,
134 IN_BSTR aId, IMachine **aMachine);
135 STDMETHOD(CreateLegacyMachine) (IN_BSTR aName, IN_BSTR aOsTypeId, IN_BSTR aSettingsFile,
136 IN_BSTR aId, IMachine **aMachine);
137 STDMETHOD(OpenMachine) (IN_BSTR aSettingsFile, IMachine **aMachine);
138 STDMETHOD(RegisterMachine) (IMachine *aMachine);
139 STDMETHOD(GetMachine) (IN_BSTR aId, IMachine **aMachine);
140 STDMETHOD(FindMachine) (IN_BSTR aName, IMachine **aMachine);
141 STDMETHOD(UnregisterMachine) (IN_BSTR aId, IMachine **aMachine);
142 STDMETHOD(CreateAppliance) (IAppliance **anAppliance);
143
144 STDMETHOD(CreateHardDisk)(IN_BSTR aFormat, IN_BSTR aLocation,
145 IMedium **aHardDisk);
146 STDMETHOD(OpenHardDisk) (IN_BSTR aLocation, AccessMode_T accessMode,
147 BOOL aSetImageId, IN_BSTR aImageId,
148 BOOL aSetParentId, IN_BSTR aParentId,
149 IMedium **aHardDisk);
150 STDMETHOD(GetHardDisk) (IN_BSTR aId, IMedium **aHardDisk);
151 STDMETHOD(FindHardDisk) (IN_BSTR aLocation, IMedium **aHardDisk);
152
153 STDMETHOD(OpenDVDImage) (IN_BSTR aLocation, IN_BSTR aId,
154 IMedium **aDVDImage);
155 STDMETHOD(GetDVDImage) (IN_BSTR aId, IMedium **aDVDImage);
156 STDMETHOD(FindDVDImage) (IN_BSTR aLocation, IMedium **aDVDImage);
157
158 STDMETHOD(OpenFloppyImage) (IN_BSTR aLocation, IN_BSTR aId,
159 IMedium **aFloppyImage);
160 STDMETHOD(GetFloppyImage) (IN_BSTR aId, IMedium **aFloppyImage);
161 STDMETHOD(FindFloppyImage) (IN_BSTR aLocation, IMedium **aFloppyImage);
162
163 STDMETHOD(GetGuestOSType) (IN_BSTR aId, IGuestOSType **aType);
164 STDMETHOD(CreateSharedFolder) (IN_BSTR aName, IN_BSTR aHostPath, BOOL aWritable);
165 STDMETHOD(RemoveSharedFolder) (IN_BSTR aName);
166 STDMETHOD(GetExtraDataKeys) (ComSafeArrayOut(BSTR, aKeys));
167 STDMETHOD(GetExtraData) (IN_BSTR aKey, BSTR *aValue);
168 STDMETHOD(SetExtraData) (IN_BSTR aKey, IN_BSTR aValue);
169 STDMETHOD(OpenSession) (ISession *aSession, IN_BSTR aMachineId);
170 STDMETHOD(OpenRemoteSession) (ISession *aSession, IN_BSTR aMachineId,
171 IN_BSTR aType, IN_BSTR aEnvironment,
172 IProgress **aProgress);
173 STDMETHOD(OpenExistingSession) (ISession *aSession, IN_BSTR aMachineId);
174
175 STDMETHOD(RegisterCallback) (IVirtualBoxCallback *aCallback);
176 STDMETHOD(UnregisterCallback) (IVirtualBoxCallback *aCallback);
177
178 STDMETHOD(WaitForPropertyChange) (IN_BSTR aWhat, ULONG aTimeout,
179 BSTR *aChanged, BSTR *aValues);
180
181// STDMETHOD(CreateDHCPServerForInterface) (/*IHostNetworkInterface * aIinterface, */IDHCPServer ** aServer);
182 STDMETHOD(CreateDHCPServer) (IN_BSTR aName, IDHCPServer ** aServer);
183// STDMETHOD(FindDHCPServerForInterface) (IHostNetworkInterface * aIinterface, IDHCPServer ** aServer);
184 STDMETHOD(FindDHCPServerByNetworkName) (IN_BSTR aName, IDHCPServer ** aServer);
185 STDMETHOD(RemoveDHCPServer) (IDHCPServer * aServer);
186
187 /* public methods only for internal purposes */
188
189 HRESULT postEvent (Event *event);
190
191 HRESULT addProgress (IProgress *aProgress);
192 HRESULT removeProgress (IN_GUID aId);
193
194#ifdef RT_OS_WINDOWS
195 typedef DECLCALLBACKPTR (HRESULT, SVCHelperClientFunc)
196 (SVCHlpClient *aClient, Progress *aProgress, void *aUser, int *aVrc);
197 HRESULT startSVCHelperClient (bool aPrivileged,
198 SVCHelperClientFunc aFunc,
199 void *aUser, Progress *aProgress);
200#endif
201
202 void addProcessToReap (RTPROCESS pid);
203 void updateClientWatcher();
204
205 void onMachineStateChange (const Guid &aId, MachineState_T aState);
206 void onMachineDataChange (const Guid &aId);
207 BOOL onExtraDataCanChange(const Guid &aId, IN_BSTR aKey, IN_BSTR aValue,
208 Bstr &aError);
209 void onExtraDataChange(const Guid &aId, IN_BSTR aKey, IN_BSTR aValue);
210 void onMachineRegistered (const Guid &aId, BOOL aRegistered);
211 void onSessionStateChange (const Guid &aId, SessionState_T aState);
212
213 void onSnapshotTaken (const Guid &aMachineId, const Guid &aSnapshotId);
214 void onSnapshotDiscarded (const Guid &aMachineId, const Guid &aSnapshotId);
215 void onSnapshotChange (const Guid &aMachineId, const Guid &aSnapshotId);
216 void onGuestPropertyChange (const Guid &aMachineId, IN_BSTR aName, IN_BSTR aValue,
217 IN_BSTR aFlags);
218
219 ComObjPtr<GuestOSType> getUnknownOSType();
220
221 void getOpenedMachines (SessionMachineVector &aMachines,
222 InternalControlVector *aControls = NULL);
223
224 /** Shortcut to #getOpenedMachines (aMachines, &aControls). */
225 void getOpenedMachinesAndControls (SessionMachineVector &aMachines,
226 InternalControlVector &aControls)
227 { getOpenedMachines (aMachines, &aControls); }
228
229 bool isMachineIdValid (const Guid &aId)
230 {
231 return SUCCEEDED (findMachine (aId, false /* aSetError */, NULL));
232 }
233
234 HRESULT findMachine (const Guid &aId, bool aSetError,
235 ComObjPtr<Machine> *machine = NULL);
236
237 HRESULT findHardDisk(const Guid *aId, CBSTR aLocation,
238 bool aSetError, ComObjPtr<Medium> *aHardDisk = NULL);
239 HRESULT findDVDImage(const Guid *aId, CBSTR aLocation,
240 bool aSetError, ComObjPtr<Medium> *aImage = NULL);
241 HRESULT findFloppyImage(const Guid *aId, CBSTR aLocation,
242 bool aSetError, ComObjPtr<Medium> *aImage = NULL);
243
244 const ComObjPtr<Host> &host() { return mData.mHost; }
245 const ComObjPtr<SystemProperties> &systemProperties()
246 { return mData.mSystemProperties; }
247#ifdef VBOX_WITH_RESOURCE_USAGE_API
248 const ComObjPtr<PerformanceCollector> &performanceCollector()
249 { return mData.mPerformanceCollector; }
250#endif /* VBOX_WITH_RESOURCE_USAGE_API */
251
252 const Utf8Str& getDefaultMachineFolder() const;
253 const Utf8Str& getDefaultHardDiskFolder() const;
254 const Utf8Str& getDefaultHardDiskFormat() const;
255
256 /** Returns the VirtualBox home directory */
257 const Utf8Str &homeDir() { return mData.mHomeDir; }
258
259 int calculateFullPath(const Utf8Str &strPath, Utf8Str &aResult);
260 void calculateRelativePath(const Utf8Str &strPath, Utf8Str &aResult);
261
262 HRESULT registerHardDisk(Medium *aHardDisk, bool aSaveRegistry = true);
263 HRESULT unregisterHardDisk(Medium *aHardDisk, bool aSaveRegistry = true);
264
265 HRESULT registerDVDImage(Medium *aImage, bool aSaveRegistry = true);
266 HRESULT unregisterDVDImage(Medium *aImage, bool aSaveRegistry = true);
267
268 HRESULT registerFloppyImage (Medium *aImage, bool aSaveRegistry = true);
269 HRESULT unregisterFloppyImage (Medium *aImage, bool aSaveRegistry = true);
270
271 HRESULT cast (IMedium *aFrom, ComObjPtr<Medium> &aTo);
272
273 HRESULT saveSettings();
274 HRESULT updateSettings(const char *aOldPath, const char *aNewPath);
275
276 static HRESULT ensureFilePathExists(const Utf8Str &strFileName);
277
278 static HRESULT handleUnexpectedExceptions (RT_SRC_POS_DECL);
279
280 const Utf8Str& settingsFilePath()
281 {
282 return m_strSettingsFilePath;
283 }
284
285 /**
286 * Returns a lock handle used to protect changes to the hard disk hierarchy
287 * (e.g. serialize access to the Medium::mParent fields and methods
288 * adding/removing children). When using this lock, the following rules must
289 * be obeyed:
290 *
291 * 1. The write lock on this handle must be either held alone on the thread
292 * or requested *after* the VirtualBox object lock. Mixing with other
293 * locks is prohibited.
294 *
295 * 2. The read lock on this handle may be intermixed with any other lock
296 * with the exception that it must be requested *after* the VirtualBox
297 * object lock.
298 */
299 RWLockHandle *hardDiskTreeLockHandle() { return &mHardDiskTreeLockHandle; }
300
301 /* for VirtualBoxSupportErrorInfoImpl */
302 static const wchar_t *getComponentName() { return L"VirtualBox"; }
303
304private:
305
306 typedef std::list< ComObjPtr<Machine> > MachineList;
307 typedef std::list< ComObjPtr<GuestOSType> > GuestOSTypeList;
308
309 typedef std::map<Guid, ComPtr<IProgress> > ProgressMap;
310
311 typedef std::list <ComObjPtr<Medium> > HardDiskList;
312 typedef std::list <ComObjPtr<Medium> > DVDImageList;
313 typedef std::list <ComObjPtr<Medium> > FloppyImageList;
314 typedef std::list <ComObjPtr<SharedFolder> > SharedFolderList;
315 typedef std::list <ComObjPtr<DHCPServer> > DHCPServerList;
316
317 typedef std::map<Guid, ComObjPtr<Medium> > HardDiskMap;
318
319 /**
320 * Reimplements VirtualBoxWithTypedChildren::childrenLock() to return a
321 * dedicated lock instead of the main object lock. The dedicated lock for
322 * child map operations frees callers of init() methods of these children
323 * from acquiring a write parent (VirtualBox) lock (which would be mandatory
324 * otherwise). Since VirtualBox has a lot of heterogenous children which
325 * init() methods are called here and there, it definitely makes sense.
326 */
327 RWLockHandle *childrenLock() { return &mChildrenMapLockHandle; }
328
329 HRESULT checkMediaForConflicts2 (const Guid &aId, const Bstr &aLocation,
330 Utf8Str &aConflictType);
331
332 HRESULT registerMachine (Machine *aMachine);
333
334 HRESULT registerDHCPServer(DHCPServer *aDHCPServer,
335 bool aSaveRegistry = true);
336 HRESULT unregisterDHCPServer(DHCPServer *aDHCPServer,
337 bool aSaveRegistry = true);
338
339 // VirtualBox main settings file
340 const Utf8Str m_strSettingsFilePath;
341 settings::MainConfigFile *m_pMainConfigFile;
342
343 /**
344 * Main VirtualBox data structure.
345 * @note |const| members are persistent during lifetime so can be accessed
346 * without locking.
347 */
348 struct Data
349 {
350 Data();
351
352 // const data members not requiring locking
353 const Utf8Str mHomeDir;
354
355 // const objects not requiring locking
356 const ComObjPtr<Host> mHost;
357 const ComObjPtr<SystemProperties> mSystemProperties;
358#ifdef VBOX_WITH_RESOURCE_USAGE_API
359 const ComObjPtr<PerformanceCollector> mPerformanceCollector;
360#endif /* VBOX_WITH_RESOURCE_USAGE_API */
361
362 MachineList mMachines;
363 GuestOSTypeList mGuestOSTypes;
364
365 ProgressMap mProgressOperations;
366
367 HardDiskList mHardDisks;
368 DVDImageList mDVDImages;
369 FloppyImageList mFloppyImages;
370 SharedFolderList mSharedFolders;
371 DHCPServerList mDHCPServers;
372
373 /// @todo NEWMEDIA do we really need this map? Used only in
374 /// find() it seems
375 HardDiskMap mHardDiskMap;
376
377 CallbackList mCallbacks;
378 };
379
380 Data mData;
381
382#if defined(RT_OS_WINDOWS)
383 #define UPDATEREQARG NULL
384 #define UPDATEREQTYPE HANDLE
385#elif defined(RT_OS_OS2)
386 #define UPDATEREQARG NIL_RTSEMEVENT
387 #define UPDATEREQTYPE RTSEMEVENT
388#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
389 #define UPDATEREQARG
390 #define UPDATEREQTYPE RTSEMEVENT
391#else
392# error "Port me!"
393#endif
394
395 /** Client watcher thread data structure */
396 struct ClientWatcherData
397 {
398 ClientWatcherData()
399 : mUpdateReq(UPDATEREQARG),
400 mThread(NIL_RTTHREAD)
401 {}
402
403 // const objects not requiring locking
404 const UPDATEREQTYPE mUpdateReq;
405 const RTTHREAD mThread;
406
407 typedef std::list <RTPROCESS> ProcessList;
408 ProcessList mProcesses;
409 };
410
411 ClientWatcherData mWatcherData;
412
413 const RTTHREAD mAsyncEventThread;
414 EventQueue * const mAsyncEventQ;
415
416 /**
417 * "Safe" lock. May only be used if guaranteed that no other locks are
418 * requested while holding it and no functions that may do so are called.
419 * Currently, protects the following:
420 *
421 * - mProgressOperations
422 */
423 RWLockHandle mSafeLock;
424
425 RWLockHandle mHardDiskTreeLockHandle;
426 RWLockHandle mChildrenMapLockHandle;
427
428 /* static variables (defined in VirtualBoxImpl.cpp) */
429 static Bstr sVersion;
430 static ULONG sRevision;
431 static Bstr sPackageType;
432
433 static DECLCALLBACK(int) ClientWatcher (RTTHREAD thread, void *pvUser);
434 static DECLCALLBACK(int) AsyncEventHandler (RTTHREAD thread, void *pvUser);
435
436#ifdef RT_OS_WINDOWS
437 static DECLCALLBACK(int) SVCHelperClientThread (RTTHREAD aThread, void *aUser);
438#endif
439};
440
441////////////////////////////////////////////////////////////////////////////////
442
443#endif // ____H_VIRTUALBOXIMPL
444
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette