VirtualBox

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

Last change on this file since 22309 was 22305, checked in by vboxsync, 15 years ago

Main: generic callback wrappers mechanism

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