VirtualBox

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

Last change on this file since 8768 was 8765, checked in by vboxsync, 17 years ago

win32 -> win.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.1 KB
Line 
1/* $Id: VirtualBoxImpl.h 8765 2008-05-12 00:59:03Z vboxsync $ */
2
3/** @file
4 *
5 * VirtualBox COM class implementation
6 */
7
8/*
9 * Copyright (C) 2006-2007 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
39class Machine;
40class SessionMachine;
41class HardDisk;
42class HVirtualDiskImage;
43class DVDImage;
44class FloppyImage;
45class MachineCollection;
46class HardDiskCollection;
47class DVDImageCollection;
48class FloppyImageCollection;
49class GuestOSType;
50class GuestOSTypeCollection;
51class SharedFolder;
52class Progress;
53class ProgressCollection;
54class Host;
55class SystemProperties;
56
57#ifdef RT_OS_WINDOWS
58class SVCHlpClient;
59#endif
60
61struct VMClientWatcherData;
62
63class ATL_NO_VTABLE VirtualBox :
64 public VirtualBoxBaseWithChildrenNEXT,
65 public VirtualBoxSupportErrorInfoImpl <VirtualBox, IVirtualBox>,
66 public VirtualBoxSupportTranslation <VirtualBox>,
67#ifdef RT_OS_WINDOWS
68 public IDispatchImpl<IVirtualBox, &IID_IVirtualBox, &LIBID_VirtualBox,
69 kTypeLibraryMajorVersion, kTypeLibraryMinorVersion>,
70 public CComCoClass<VirtualBox, &CLSID_VirtualBox>
71#else
72 public IVirtualBox
73#endif
74{
75
76public:
77
78 VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT (VirtualBox)
79
80 typedef std::list <ComPtr <IVirtualBoxCallback> > CallbackList;
81 typedef std::vector <ComPtr <IVirtualBoxCallback> > CallbackVector;
82
83 class CallbackEvent;
84 friend class CallbackEvent;
85
86 DECLARE_CLASSFACTORY_SINGLETON(VirtualBox)
87
88 DECLARE_REGISTRY_RESOURCEID(IDR_VIRTUALBOX)
89 DECLARE_NOT_AGGREGATABLE(VirtualBox)
90
91 DECLARE_PROTECT_FINAL_CONSTRUCT()
92
93 BEGIN_COM_MAP(VirtualBox)
94 COM_INTERFACE_ENTRY(IDispatch)
95 COM_INTERFACE_ENTRY(ISupportErrorInfo)
96 COM_INTERFACE_ENTRY(IVirtualBox)
97 END_COM_MAP()
98
99 NS_DECL_ISUPPORTS
100
101 /* to postpone generation of the default ctor/dtor */
102 VirtualBox();
103 ~VirtualBox();
104
105 HRESULT FinalConstruct();
106 void FinalRelease();
107
108 /* public initializer/uninitializer for internal purposes only */
109 HRESULT init();
110 void uninit();
111
112 /* IVirtualBox properties */
113 STDMETHOD(COMGETTER(Version)) (BSTR *aVersion);
114 STDMETHOD(COMGETTER(HomeFolder)) (BSTR *aHomeFolder);
115 STDMETHOD(COMGETTER(SettingsFilePath)) (BSTR *aSettingsFilePath);
116 STDMETHOD(COMGETTER(SettingsFileVersion)) (BSTR *aSettingsFileVersion);
117 STDMETHOD(COMGETTER(SettingsFormatVersion)) (BSTR *aSettingsFormatVersion);
118 STDMETHOD(COMGETTER(Host)) (IHost **aHost);
119 STDMETHOD(COMGETTER(SystemProperties)) (ISystemProperties **aSystemProperties);
120 STDMETHOD(COMGETTER(Machines)) (IMachineCollection **aMachines);
121 STDMETHOD(COMGETTER(Machines2)) (ComSafeArrayOut (IMachine *, aMachines));
122 STDMETHOD(COMGETTER(HardDisks)) (IHardDiskCollection **aHardDisks);
123 STDMETHOD(COMGETTER(DVDImages)) (IDVDImageCollection **aDVDImages);
124 STDMETHOD(COMGETTER(FloppyImages)) (IFloppyImageCollection **aFloppyImages);
125 STDMETHOD(COMGETTER(ProgressOperations)) (IProgressCollection **aOperations);
126 STDMETHOD(COMGETTER(GuestOSTypes)) (IGuestOSTypeCollection **aGuestOSTypes);
127 STDMETHOD(COMGETTER(SharedFolders)) (ISharedFolderCollection **aSharedFolders);
128
129 /* IVirtualBox methods */
130
131 STDMETHOD(CreateMachine) (INPTR BSTR aBaseFolder, INPTR BSTR aName,
132 INPTR GUIDPARAM aId, IMachine **aMachine);
133 STDMETHOD(CreateLegacyMachine) (INPTR BSTR aSettingsFile, INPTR BSTR aName,
134 INPTR GUIDPARAM aId, IMachine **aMachine);
135 STDMETHOD(OpenMachine) (INPTR BSTR aSettingsFile, IMachine **aMachine);
136 STDMETHOD(RegisterMachine) (IMachine *aMachine);
137 STDMETHOD(GetMachine) (INPTR GUIDPARAM aId, IMachine **aMachine);
138 STDMETHOD(FindMachine) (INPTR BSTR aName, IMachine **aMachine);
139 STDMETHOD(UnregisterMachine) (INPTR GUIDPARAM aId, IMachine **aMachine);
140
141 STDMETHOD(CreateHardDisk) (HardDiskStorageType_T aStorageType, IHardDisk **aHardDisk);
142 STDMETHOD(OpenHardDisk) (INPTR BSTR aLocation, IHardDisk **aHardDisk);
143 STDMETHOD(OpenVirtualDiskImage) (INPTR BSTR aFilePath, IVirtualDiskImage **aImage);
144 STDMETHOD(RegisterHardDisk) (IHardDisk *aHardDisk);
145 STDMETHOD(GetHardDisk) (INPTR GUIDPARAM aId, IHardDisk **aHardDisk);
146 STDMETHOD(FindHardDisk) (INPTR BSTR aLocation, IHardDisk **aHardDisk);
147 STDMETHOD(FindVirtualDiskImage) (INPTR BSTR aFilePath, IVirtualDiskImage **aImage);
148 STDMETHOD(UnregisterHardDisk) (INPTR GUIDPARAM aId, IHardDisk **aHardDisk);
149
150 STDMETHOD(OpenDVDImage) (INPTR BSTR aFilePath, INPTR GUIDPARAM aId,
151 IDVDImage **aDVDImage);
152 STDMETHOD(RegisterDVDImage) (IDVDImage *aDVDImage);
153 STDMETHOD(GetDVDImage) (INPTR GUIDPARAM aId, IDVDImage **aDVDImage);
154 STDMETHOD(FindDVDImage) (INPTR BSTR aFilePath, IDVDImage **aDVDImage);
155 STDMETHOD(GetDVDImageUsage) (INPTR GUIDPARAM aId,
156 ResourceUsage_T aUsage,
157 BSTR *aMachineIDs);
158 STDMETHOD(UnregisterDVDImage) (INPTR GUIDPARAM aId, IDVDImage **aDVDImage);
159
160 STDMETHOD(OpenFloppyImage) (INPTR BSTR aFilePath, INPTR GUIDPARAM aId,
161 IFloppyImage **aFloppyImage);
162 STDMETHOD(RegisterFloppyImage) (IFloppyImage *aFloppyImage);
163 STDMETHOD(GetFloppyImage) (INPTR GUIDPARAM id, IFloppyImage **aFloppyImage);
164 STDMETHOD(FindFloppyImage) (INPTR BSTR aFilePath, IFloppyImage **aFloppyImage);
165 STDMETHOD(GetFloppyImageUsage) (INPTR GUIDPARAM aId,
166 ResourceUsage_T aUsage,
167 BSTR *aMachineIDs);
168 STDMETHOD(UnregisterFloppyImage) (INPTR GUIDPARAM aId, IFloppyImage **aFloppyImage);
169
170 STDMETHOD(GetGuestOSType) (INPTR BSTR aId, IGuestOSType **aType);
171 STDMETHOD(CreateSharedFolder) (INPTR BSTR aName, INPTR BSTR aHostPath, BOOL aWritable);
172 STDMETHOD(RemoveSharedFolder) (INPTR BSTR aName);
173 STDMETHOD(GetNextExtraDataKey) (INPTR BSTR aKey, BSTR *aNextKey, BSTR *aNextValue);
174 STDMETHOD(GetExtraData) (INPTR BSTR aKey, BSTR *aValue);
175 STDMETHOD(SetExtraData) (INPTR BSTR aKey, INPTR BSTR aValue);
176 STDMETHOD(OpenSession) (ISession *aSession, INPTR GUIDPARAM aMachineId);
177 STDMETHOD(OpenRemoteSession) (ISession *aSession, INPTR GUIDPARAM aMachineId,
178 INPTR BSTR aType, INPTR BSTR aEnvironment,
179 IProgress **aProgress);
180 STDMETHOD(OpenExistingSession) (ISession *aSession, INPTR GUIDPARAM aMachineId);
181
182 STDMETHOD(RegisterCallback) (IVirtualBoxCallback *aCallback);
183 STDMETHOD(UnregisterCallback) (IVirtualBoxCallback *aCallback);
184
185 STDMETHOD(WaitForPropertyChange) (INPTR BSTR aWhat, ULONG aTimeout,
186 BSTR *aChanged, BSTR *aValues);
187
188 STDMETHOD(SaveSettings)();
189 STDMETHOD(SaveSettingsWithBackup) (BSTR *aBakFileName);
190
191 /* public methods only for internal purposes */
192
193 HRESULT postEvent (Event *event);
194
195 HRESULT addProgress (IProgress *aProgress);
196 HRESULT removeProgress (INPTR GUIDPARAM aId);
197
198#ifdef RT_OS_WINDOWS
199 typedef DECLCALLBACKPTR (HRESULT, SVCHelperClientFunc)
200 (SVCHlpClient *aClient, Progress *aProgress, void *aUser, int *aVrc);
201 HRESULT startSVCHelperClient (bool aPrivileged,
202 SVCHelperClientFunc aFunc,
203 void *aUser, Progress *aProgress);
204#endif
205
206 void addProcessToReap (RTPROCESS pid);
207 void updateClientWatcher();
208
209 void onMachineStateChange (const Guid &aId, MachineState_T aState);
210 void onMachineDataChange (const Guid &aId);
211 BOOL onExtraDataCanChange(const Guid &aId, INPTR BSTR aKey, INPTR BSTR aValue,
212 Bstr &aError);
213 void onExtraDataChange(const Guid &aId, INPTR BSTR aKey, INPTR BSTR aValue);
214 void onMachineRegistered (const Guid &aId, BOOL aRegistered);
215 void onSessionStateChange (const Guid &aId, SessionState_T aState);
216
217 void onSnapshotTaken (const Guid &aMachineId, const Guid &aSnapshotId);
218 void onSnapshotDiscarded (const Guid &aMachineId, const Guid &aSnapshotId);
219 void onSnapshotChange (const Guid &aMachineId, const Guid &aSnapshotId);
220
221 ComObjPtr <GuestOSType> getUnknownOSType();
222
223 typedef std::vector <ComObjPtr <SessionMachine> > SessionMachineVector;
224 void getOpenedMachines (SessionMachineVector &aVector);
225
226 bool isMachineIdValid (const Guid &aId)
227 {
228 return SUCCEEDED (findMachine (aId, false /* aSetError */, NULL));
229 }
230
231 /// @todo (dmik) remove and make findMachine() public instead
232 // after switching to VirtualBoxBaseNEXT
233 HRESULT getMachine (const Guid &aId, ComObjPtr <Machine> &aMachine,
234 bool aSetError = false)
235 {
236 return findMachine (aId, aSetError, &aMachine);
237 }
238
239 /// @todo (dmik) remove and make findHardDisk() public instead
240 // after switching to VirtualBoxBaseNEXT
241 HRESULT getHardDisk (const Guid &aId, ComObjPtr <HardDisk> &aHardDisk)
242 {
243 return findHardDisk (&aId, NULL, true /* aDoSetError */, &aHardDisk);
244 }
245
246 bool getDVDImageUsage (const Guid &aId, ResourceUsage_T aUsage,
247 Bstr *aMachineIDs = NULL);
248 bool getFloppyImageUsage (const Guid &aId, ResourceUsage_T aUsage,
249 Bstr *aMachineIDs = NULL);
250
251 const ComObjPtr <Host> &host() { return mData.mHost; }
252 const ComObjPtr <SystemProperties> &systemProperties() { return mData.mSystemProperties; }
253
254 /** Returns the VirtualBox home directory */
255 const Utf8Str &homeDir() { return mData.mHomeDir; }
256
257 void calculateRelativePath (const char *aPath, Utf8Str &aResult);
258
259 enum RHD_Flags { RHD_Internal, RHD_External, RHD_OnStartUp };
260 HRESULT registerHardDisk (HardDisk *aHardDisk, RHD_Flags aFlags);
261 HRESULT unregisterHardDisk (HardDisk *aHardDisk);
262 HRESULT unregisterDiffHardDisk (HardDisk *aHardDisk);
263
264 HRESULT saveSettings();
265 HRESULT updateSettings (const char *aOldPath, const char *aNewPath);
266
267 const Bstr &settingsFileName() { return mData.mCfgFile.mName; }
268
269 class SettingsTreeHelper : public settings::XmlTreeBackend::InputResolver
270 , public settings::XmlTreeBackend::AutoConverter
271 {
272 public:
273
274 // InputResolver interface
275 settings::Input *resolveEntity (const char *aURI, const char *aID);
276
277 // AutoConverter interface
278 bool needsConversion (const settings::Key &aRoot, char **aOldVersion) const;
279 const char *templateUri() const;
280 };
281
282 static HRESULT loadSettingsTree (settings::XmlTreeBackend &aTree,
283 settings::File &aFile,
284 bool aValidate,
285 bool aCatchLoadErrors,
286 bool aAddDefaults,
287 Utf8Str *aFormatVersion = NULL);
288
289 /**
290 * Shortcut to loadSettingsTree (aTree, aFile, true, true, true).
291 *
292 * Used when the settings file is to be loaded for the first time for the
293 * given object in order to recreate it from the stored settings.
294 *
295 * @param aFormatVersion Where to store the current format version of the
296 * loaded settings tree.
297 */
298 static HRESULT loadSettingsTree_FirstTime (settings::XmlTreeBackend &aTree,
299 settings::File &aFile,
300 Utf8Str &aFormatVersion)
301 {
302 return loadSettingsTree (aTree, aFile, true, true, true,
303 &aFormatVersion);
304 }
305
306 /**
307 * Shortcut to loadSettingsTree (aTree, aFile, true, false, true).
308 *
309 * Used when the settings file is loaded again (after it has been fully
310 * checked and validated by #loadSettingsTree_FirstTime()) in order to
311 * look at settings that don't have any representation within object's
312 * data fields.
313 */
314 static HRESULT loadSettingsTree_Again (settings::XmlTreeBackend &aTree,
315 settings::File &aFile)
316 {
317 return loadSettingsTree (aTree, aFile, true, false, true);
318 }
319
320 /**
321 * Shortcut to loadSettingsTree (aTree, aFile, true, false, false).
322 *
323 * Used when the settings file is loaded again (after it has been fully
324 * checked and validated by #loadSettingsTree_FirstTime()) in order to
325 * update some settings and then save them back.
326 */
327 static HRESULT loadSettingsTree_ForUpdate (settings::XmlTreeBackend &aTree,
328 settings::File &aFile)
329 {
330 return loadSettingsTree (aTree, aFile, true, false, false);
331 }
332
333 static HRESULT saveSettingsTree (settings::TreeBackend &aTree,
334 settings::File &aFile,
335 Utf8Str &aFormatVersion);
336
337 static HRESULT backupSettingsFile (const Bstr &aFileName,
338 const Utf8Str &aOldFormat,
339 Bstr &aBakFileName);
340
341 static HRESULT handleUnexpectedExceptions (RT_SRC_POS_DECL);
342
343 /* for VirtualBoxSupportErrorInfoImpl */
344 static const wchar_t *getComponentName() { return L"VirtualBox"; }
345
346private:
347
348 typedef std::list <ComObjPtr <Machine> > MachineList;
349 typedef std::list <ComObjPtr <GuestOSType> > GuestOSTypeList;
350 typedef std::list <ComPtr <IProgress> > ProgressList;
351
352 typedef std::list <ComObjPtr <HardDisk> > HardDiskList;
353 typedef std::list <ComObjPtr <DVDImage> > DVDImageList;
354 typedef std::list <ComObjPtr <FloppyImage> > FloppyImageList;
355 typedef std::list <ComObjPtr <SharedFolder> > SharedFolderList;
356
357 typedef std::map <Guid, ComObjPtr <HardDisk> > HardDiskMap;
358
359 HRESULT findMachine (const Guid &aId, bool aSetError,
360 ComObjPtr <Machine> *machine = NULL);
361
362 HRESULT findHardDisk (const Guid *aId, const BSTR aLocation,
363 bool aSetError, ComObjPtr <HardDisk> *aHardDisk = NULL);
364
365 HRESULT findVirtualDiskImage (const Guid *aId, const BSTR aFilePathFull,
366 bool aSetError, ComObjPtr <HVirtualDiskImage> *aImage = NULL);
367 HRESULT findDVDImage (const Guid *aId, const BSTR aFilePathFull,
368 bool aSetError, ComObjPtr <DVDImage> *aImage = NULL);
369 HRESULT findFloppyImage (const Guid *aId, const BSTR aFilePathFull,
370 bool aSetError, ComObjPtr <FloppyImage> *aImage = NULL);
371
372 HRESULT checkMediaForConflicts (HardDisk *aHardDisk,
373 const Guid *aId, const BSTR aFilePathFull);
374
375 HRESULT loadMachines (const settings::Key &aGlobal);
376 HRESULT loadDisks (const settings::Key &aGlobal);
377 HRESULT loadHardDisks (const settings::Key &aNode);
378
379 HRESULT saveHardDisks (settings::Key &aNode);
380
381 HRESULT registerMachine (Machine *aMachine);
382
383 HRESULT registerDVDImage (DVDImage *aImage, bool aOnStartUp);
384 HRESULT registerFloppyImage (FloppyImage *aImage, bool aOnStartUp);
385 HRESULT registerGuestOSTypes();
386
387 HRESULT lockConfig();
388 HRESULT unlockConfig();
389
390 /** @note This method is not thread safe */
391 bool isConfigLocked() { return mData.mCfgFile.mHandle != NIL_RTFILE; }
392
393 /**
394 * Main VirtualBox data structure.
395 * @note |const| members are persistent during lifetime so can be accessed
396 * without locking.
397 */
398 struct Data
399 {
400 Data();
401
402 struct CfgFile
403 {
404 CfgFile() : mHandle (NIL_RTFILE) {}
405
406 const Bstr mName;
407 RTFILE mHandle;
408 };
409
410 // const data members not requiring locking
411 const Utf8Str mHomeDir;
412
413 // const objects not requiring locking
414 const ComObjPtr <Host> mHost;
415 const ComObjPtr <SystemProperties> mSystemProperties;
416
417 CfgFile mCfgFile;
418
419 Utf8Str mSettingsFileVersion;
420
421 MachineList mMachines;
422 GuestOSTypeList mGuestOSTypes;
423
424 ProgressList mProgressOperations;
425 HardDiskList mHardDisks;
426 DVDImageList mDVDImages;
427 FloppyImageList mFloppyImages;
428 SharedFolderList mSharedFolders;
429
430 HardDiskMap mHardDiskMap;
431
432 CallbackList mCallbacks;
433 };
434
435 Data mData;
436
437 /** Client watcher thread data structure */
438 struct ClientWatcherData
439 {
440 ClientWatcherData()
441#if defined(RT_OS_WINDOWS)
442 : mUpdateReq (NULL)
443#elif defined(RT_OS_OS2)
444 : mUpdateReq (NIL_RTSEMEVENT)
445#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
446 : mUpdateReq (NIL_RTSEMEVENT)
447#else
448# error "Port me!"
449#endif
450 , mThread (NIL_RTTHREAD) {}
451
452 // const objects not requiring locking
453#if defined(RT_OS_WINDOWS)
454 const HANDLE mUpdateReq;
455#elif defined(RT_OS_OS2)
456 const RTSEMEVENT mUpdateReq;
457#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
458 const RTSEMEVENT mUpdateReq;
459#else
460# error "Port me!"
461#endif
462 const RTTHREAD mThread;
463
464 typedef std::list <RTPROCESS> ProcessList;
465 ProcessList mProcesses;
466 };
467
468 ClientWatcherData mWatcherData;
469
470 const RTTHREAD mAsyncEventThread;
471 EventQueue * const mAsyncEventQ;
472 /** Lock for calling EventQueue->post() */
473 RWLockHandle mAsyncEventQLock;
474
475 static Bstr sVersion;
476 static Bstr sSettingsFormatVersion;
477
478 static DECLCALLBACK(int) ClientWatcher (RTTHREAD thread, void *pvUser);
479 static DECLCALLBACK(int) AsyncEventHandler (RTTHREAD thread, void *pvUser);
480
481#ifdef RT_OS_WINDOWS
482 static DECLCALLBACK(int) SVCHelperClientThread (RTTHREAD aThread, void *aUser);
483#endif
484};
485
486////////////////////////////////////////////////////////////////////////////////
487
488/**
489 * Abstract callback event class to asynchronously call VirtualBox callbacks
490 * on a dedicated event thread. Subclasses reimplement #handleCallback()
491 * to call appropriate IVirtualBoxCallback methods depending on the event
492 * to be dispatched.
493 *
494 * @note The VirtualBox instance passed to the constructor is strongly
495 * referenced, so that the VirtualBox singleton won't be released until the
496 * event gets handled by the event thread.
497 */
498class VirtualBox::CallbackEvent : public Event
499{
500public:
501
502 CallbackEvent (VirtualBox *aVirtualBox) : mVirtualBox (aVirtualBox)
503 {
504 Assert (aVirtualBox);
505 }
506
507 void *handler();
508
509 virtual void handleCallback (const ComPtr <IVirtualBoxCallback> &aCallback) = 0;
510
511private:
512
513 /*
514 * Note that this is a weak ref -- the CallbackEvent handler thread
515 * is bound to the lifetime of the VirtualBox instance, so it's safe.
516 */
517 ComObjPtr <VirtualBox, ComWeakRef> mVirtualBox;
518};
519
520#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