VirtualBox

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

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

added package type property

  • 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 10333 2008-07-07 15:16:04Z 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 typedef std::list <ComPtr <IVirtualBoxCallback> > CallbackList;
79 typedef std::vector <ComPtr <IVirtualBoxCallback> > CallbackVector;
80
81 class CallbackEvent;
82 friend class CallbackEvent;
83
84 VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT (VirtualBox)
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(PackageType)) (BSTR *aPackageType);
115 STDMETHOD(COMGETTER(HomeFolder)) (BSTR *aHomeFolder);
116 STDMETHOD(COMGETTER(SettingsFilePath)) (BSTR *aSettingsFilePath);
117 STDMETHOD(COMGETTER(SettingsFileVersion)) (BSTR *aSettingsFileVersion);
118 STDMETHOD(COMGETTER(SettingsFormatVersion)) (BSTR *aSettingsFormatVersion);
119 STDMETHOD(COMGETTER(Host)) (IHost **aHost);
120 STDMETHOD(COMGETTER(SystemProperties)) (ISystemProperties **aSystemProperties);
121 STDMETHOD(COMGETTER(Machines)) (IMachineCollection **aMachines);
122 STDMETHOD(COMGETTER(Machines2)) (ComSafeArrayOut (IMachine *, aMachines));
123 STDMETHOD(COMGETTER(HardDisks)) (IHardDiskCollection **aHardDisks);
124 STDMETHOD(COMGETTER(DVDImages)) (IDVDImageCollection **aDVDImages);
125 STDMETHOD(COMGETTER(FloppyImages)) (IFloppyImageCollection **aFloppyImages);
126 STDMETHOD(COMGETTER(ProgressOperations)) (IProgressCollection **aOperations);
127 STDMETHOD(COMGETTER(GuestOSTypes)) (IGuestOSTypeCollection **aGuestOSTypes);
128 STDMETHOD(COMGETTER(SharedFolders)) (ISharedFolderCollection **aSharedFolders);
129
130 /* IVirtualBox methods */
131
132 STDMETHOD(CreateMachine) (INPTR BSTR aBaseFolder, INPTR BSTR aName,
133 INPTR GUIDPARAM aId, IMachine **aMachine);
134 STDMETHOD(CreateLegacyMachine) (INPTR BSTR aSettingsFile, INPTR BSTR aName,
135 INPTR GUIDPARAM aId, IMachine **aMachine);
136 STDMETHOD(OpenMachine) (INPTR BSTR aSettingsFile, IMachine **aMachine);
137 STDMETHOD(RegisterMachine) (IMachine *aMachine);
138 STDMETHOD(GetMachine) (INPTR GUIDPARAM aId, IMachine **aMachine);
139 STDMETHOD(FindMachine) (INPTR BSTR aName, IMachine **aMachine);
140 STDMETHOD(UnregisterMachine) (INPTR GUIDPARAM aId, IMachine **aMachine);
141
142 STDMETHOD(CreateHardDisk) (HardDiskStorageType_T aStorageType, IHardDisk **aHardDisk);
143 STDMETHOD(OpenHardDisk) (INPTR BSTR aLocation, IHardDisk **aHardDisk);
144 STDMETHOD(OpenVirtualDiskImage) (INPTR BSTR aFilePath, IVirtualDiskImage **aImage);
145 STDMETHOD(RegisterHardDisk) (IHardDisk *aHardDisk);
146 STDMETHOD(GetHardDisk) (INPTR GUIDPARAM aId, IHardDisk **aHardDisk);
147 STDMETHOD(FindHardDisk) (INPTR BSTR aLocation, IHardDisk **aHardDisk);
148 STDMETHOD(FindVirtualDiskImage) (INPTR BSTR aFilePath, IVirtualDiskImage **aImage);
149 STDMETHOD(UnregisterHardDisk) (INPTR GUIDPARAM aId, IHardDisk **aHardDisk);
150
151 STDMETHOD(OpenDVDImage) (INPTR BSTR aFilePath, INPTR GUIDPARAM aId,
152 IDVDImage **aDVDImage);
153 STDMETHOD(RegisterDVDImage) (IDVDImage *aDVDImage);
154 STDMETHOD(GetDVDImage) (INPTR GUIDPARAM aId, IDVDImage **aDVDImage);
155 STDMETHOD(FindDVDImage) (INPTR BSTR aFilePath, IDVDImage **aDVDImage);
156 STDMETHOD(GetDVDImageUsage) (INPTR GUIDPARAM aId,
157 ResourceUsage_T aUsage,
158 BSTR *aMachineIDs);
159 STDMETHOD(UnregisterDVDImage) (INPTR GUIDPARAM aId, IDVDImage **aDVDImage);
160
161 STDMETHOD(OpenFloppyImage) (INPTR BSTR aFilePath, INPTR GUIDPARAM aId,
162 IFloppyImage **aFloppyImage);
163 STDMETHOD(RegisterFloppyImage) (IFloppyImage *aFloppyImage);
164 STDMETHOD(GetFloppyImage) (INPTR GUIDPARAM id, IFloppyImage **aFloppyImage);
165 STDMETHOD(FindFloppyImage) (INPTR BSTR aFilePath, IFloppyImage **aFloppyImage);
166 STDMETHOD(GetFloppyImageUsage) (INPTR GUIDPARAM aId,
167 ResourceUsage_T aUsage,
168 BSTR *aMachineIDs);
169 STDMETHOD(UnregisterFloppyImage) (INPTR GUIDPARAM aId, IFloppyImage **aFloppyImage);
170
171 STDMETHOD(GetGuestOSType) (INPTR BSTR aId, IGuestOSType **aType);
172 STDMETHOD(CreateSharedFolder) (INPTR BSTR aName, INPTR BSTR aHostPath, BOOL aWritable);
173 STDMETHOD(RemoveSharedFolder) (INPTR BSTR aName);
174 STDMETHOD(GetNextExtraDataKey) (INPTR BSTR aKey, BSTR *aNextKey, BSTR *aNextValue);
175 STDMETHOD(GetExtraData) (INPTR BSTR aKey, BSTR *aValue);
176 STDMETHOD(SetExtraData) (INPTR BSTR aKey, INPTR BSTR aValue);
177 STDMETHOD(OpenSession) (ISession *aSession, INPTR GUIDPARAM aMachineId);
178 STDMETHOD(OpenRemoteSession) (ISession *aSession, INPTR GUIDPARAM aMachineId,
179 INPTR BSTR aType, INPTR BSTR aEnvironment,
180 IProgress **aProgress);
181 STDMETHOD(OpenExistingSession) (ISession *aSession, INPTR GUIDPARAM aMachineId);
182
183 STDMETHOD(RegisterCallback) (IVirtualBoxCallback *aCallback);
184 STDMETHOD(UnregisterCallback) (IVirtualBoxCallback *aCallback);
185
186 STDMETHOD(WaitForPropertyChange) (INPTR BSTR aWhat, ULONG aTimeout,
187 BSTR *aChanged, BSTR *aValues);
188
189 STDMETHOD(SaveSettings)();
190 STDMETHOD(SaveSettingsWithBackup) (BSTR *aBakFileName);
191
192 /* public methods only for internal purposes */
193
194 HRESULT postEvent (Event *event);
195
196 HRESULT addProgress (IProgress *aProgress);
197 HRESULT removeProgress (INPTR GUIDPARAM aId);
198
199#ifdef RT_OS_WINDOWS
200 typedef DECLCALLBACKPTR (HRESULT, SVCHelperClientFunc)
201 (SVCHlpClient *aClient, Progress *aProgress, void *aUser, int *aVrc);
202 HRESULT startSVCHelperClient (bool aPrivileged,
203 SVCHelperClientFunc aFunc,
204 void *aUser, Progress *aProgress);
205#endif
206
207 void addProcessToReap (RTPROCESS pid);
208 void updateClientWatcher();
209
210 void onMachineStateChange (const Guid &aId, MachineState_T aState);
211 void onMachineDataChange (const Guid &aId);
212 BOOL onExtraDataCanChange(const Guid &aId, INPTR BSTR aKey, INPTR BSTR aValue,
213 Bstr &aError);
214 void onExtraDataChange(const Guid &aId, INPTR BSTR aKey, INPTR BSTR aValue);
215 void onMachineRegistered (const Guid &aId, BOOL aRegistered);
216 void onSessionStateChange (const Guid &aId, SessionState_T aState);
217
218 void onSnapshotTaken (const Guid &aMachineId, const Guid &aSnapshotId);
219 void onSnapshotDiscarded (const Guid &aMachineId, const Guid &aSnapshotId);
220 void onSnapshotChange (const Guid &aMachineId, const Guid &aSnapshotId);
221
222 ComObjPtr <GuestOSType> getUnknownOSType();
223
224 typedef std::vector <ComObjPtr <SessionMachine> > SessionMachineVector;
225 void getOpenedMachines (SessionMachineVector &aVector);
226
227 bool isMachineIdValid (const Guid &aId)
228 {
229 return SUCCEEDED (findMachine (aId, false /* aSetError */, NULL));
230 }
231
232 /// @todo (dmik) remove and make findMachine() public instead
233 // after switching to VirtualBoxBaseNEXT
234 HRESULT getMachine (const Guid &aId, ComObjPtr <Machine> &aMachine,
235 bool aSetError = false)
236 {
237 return findMachine (aId, aSetError, &aMachine);
238 }
239
240 /// @todo (dmik) remove and make findHardDisk() public instead
241 // after switching to VirtualBoxBaseNEXT
242 HRESULT getHardDisk (const Guid &aId, ComObjPtr <HardDisk> &aHardDisk)
243 {
244 return findHardDisk (&aId, NULL, true /* aDoSetError */, &aHardDisk);
245 }
246
247 bool getDVDImageUsage (const Guid &aId, ResourceUsage_T aUsage,
248 Bstr *aMachineIDs = NULL);
249 bool getFloppyImageUsage (const Guid &aId, ResourceUsage_T aUsage,
250 Bstr *aMachineIDs = NULL);
251
252 const ComObjPtr <Host> &host() { return mData.mHost; }
253 const ComObjPtr <SystemProperties> &systemProperties() { return mData.mSystemProperties; }
254
255 /** Returns the VirtualBox home directory */
256 const Utf8Str &homeDir() { return mData.mHomeDir; }
257
258 void calculateRelativePath (const char *aPath, Utf8Str &aResult);
259
260 enum RHD_Flags { RHD_Internal, RHD_External, RHD_OnStartUp };
261 HRESULT registerHardDisk (HardDisk *aHardDisk, RHD_Flags aFlags);
262 HRESULT unregisterHardDisk (HardDisk *aHardDisk);
263 HRESULT unregisterDiffHardDisk (HardDisk *aHardDisk);
264
265 HRESULT saveSettings();
266 HRESULT updateSettings (const char *aOldPath, const char *aNewPath);
267
268 const Bstr &settingsFileName() { return mData.mCfgFile.mName; }
269
270 class SettingsTreeHelper : public settings::XmlTreeBackend::InputResolver
271 , public settings::XmlTreeBackend::AutoConverter
272 {
273 public:
274
275 // InputResolver interface
276 settings::Input *resolveEntity (const char *aURI, const char *aID);
277
278 // AutoConverter interface
279 bool needsConversion (const settings::Key &aRoot, char **aOldVersion) const;
280 const char *templateUri() const;
281 };
282
283 static HRESULT loadSettingsTree (settings::XmlTreeBackend &aTree,
284 settings::File &aFile,
285 bool aValidate,
286 bool aCatchLoadErrors,
287 bool aAddDefaults,
288 Utf8Str *aFormatVersion = NULL);
289
290 /**
291 * Shortcut to loadSettingsTree (aTree, aFile, true, true, true).
292 *
293 * Used when the settings file is to be loaded for the first time for the
294 * given object in order to recreate it from the stored settings.
295 *
296 * @param aFormatVersion Where to store the current format version of the
297 * loaded settings tree.
298 */
299 static HRESULT loadSettingsTree_FirstTime (settings::XmlTreeBackend &aTree,
300 settings::File &aFile,
301 Utf8Str &aFormatVersion)
302 {
303 return loadSettingsTree (aTree, aFile, true, true, true,
304 &aFormatVersion);
305 }
306
307 /**
308 * Shortcut to loadSettingsTree (aTree, aFile, true, false, true).
309 *
310 * Used when the settings file is loaded again (after it has been fully
311 * checked and validated by #loadSettingsTree_FirstTime()) in order to
312 * look at settings that don't have any representation within object's
313 * data fields.
314 */
315 static HRESULT loadSettingsTree_Again (settings::XmlTreeBackend &aTree,
316 settings::File &aFile)
317 {
318 return loadSettingsTree (aTree, aFile, true, false, true);
319 }
320
321 /**
322 * Shortcut to loadSettingsTree (aTree, aFile, true, false, false).
323 *
324 * Used when the settings file is loaded again (after it has been fully
325 * checked and validated by #loadSettingsTree_FirstTime()) in order to
326 * update some settings and then save them back.
327 */
328 static HRESULT loadSettingsTree_ForUpdate (settings::XmlTreeBackend &aTree,
329 settings::File &aFile)
330 {
331 return loadSettingsTree (aTree, aFile, true, false, false);
332 }
333
334 static HRESULT saveSettingsTree (settings::TreeBackend &aTree,
335 settings::File &aFile,
336 Utf8Str &aFormatVersion);
337
338 static HRESULT backupSettingsFile (const Bstr &aFileName,
339 const Utf8Str &aOldFormat,
340 Bstr &aBakFileName);
341
342 static HRESULT handleUnexpectedExceptions (RT_SRC_POS_DECL);
343
344 /* for VirtualBoxSupportErrorInfoImpl */
345 static const wchar_t *getComponentName() { return L"VirtualBox"; }
346
347private:
348
349 typedef std::list <ComObjPtr <Machine> > MachineList;
350 typedef std::list <ComObjPtr <GuestOSType> > GuestOSTypeList;
351 typedef std::list <ComPtr <IProgress> > ProgressList;
352
353 typedef std::list <ComObjPtr <HardDisk> > HardDiskList;
354 typedef std::list <ComObjPtr <DVDImage> > DVDImageList;
355 typedef std::list <ComObjPtr <FloppyImage> > FloppyImageList;
356 typedef std::list <ComObjPtr <SharedFolder> > SharedFolderList;
357
358 typedef std::map <Guid, ComObjPtr <HardDisk> > HardDiskMap;
359
360 HRESULT findMachine (const Guid &aId, bool aSetError,
361 ComObjPtr <Machine> *machine = NULL);
362
363 HRESULT findHardDisk (const Guid *aId, const BSTR aLocation,
364 bool aSetError, ComObjPtr <HardDisk> *aHardDisk = NULL);
365
366 HRESULT findVirtualDiskImage (const Guid *aId, const BSTR aFilePathFull,
367 bool aSetError, ComObjPtr <HVirtualDiskImage> *aImage = NULL);
368 HRESULT findDVDImage (const Guid *aId, const BSTR aFilePathFull,
369 bool aSetError, ComObjPtr <DVDImage> *aImage = NULL);
370 HRESULT findFloppyImage (const Guid *aId, const BSTR aFilePathFull,
371 bool aSetError, ComObjPtr <FloppyImage> *aImage = NULL);
372
373 HRESULT checkMediaForConflicts (HardDisk *aHardDisk,
374 const Guid *aId, const BSTR aFilePathFull);
375
376 HRESULT loadMachines (const settings::Key &aGlobal);
377 HRESULT loadDisks (const settings::Key &aGlobal);
378 HRESULT loadHardDisks (const settings::Key &aNode);
379
380 HRESULT saveHardDisks (settings::Key &aNode);
381
382 HRESULT registerMachine (Machine *aMachine);
383
384 HRESULT registerDVDImage (DVDImage *aImage, bool aOnStartUp);
385 HRESULT registerFloppyImage (FloppyImage *aImage, bool aOnStartUp);
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 sPackageType;
477 static Bstr sSettingsFormatVersion;
478
479 static DECLCALLBACK(int) ClientWatcher (RTTHREAD thread, void *pvUser);
480 static DECLCALLBACK(int) AsyncEventHandler (RTTHREAD thread, void *pvUser);
481
482#ifdef RT_OS_WINDOWS
483 static DECLCALLBACK(int) SVCHelperClientThread (RTTHREAD aThread, void *aUser);
484#endif
485};
486
487////////////////////////////////////////////////////////////////////////////////
488
489/**
490 * Abstract callback event class to asynchronously call VirtualBox callbacks
491 * on a dedicated event thread. Subclasses reimplement #handleCallback()
492 * to call appropriate IVirtualBoxCallback methods depending on the event
493 * to be dispatched.
494 *
495 * @note The VirtualBox instance passed to the constructor is strongly
496 * referenced, so that the VirtualBox singleton won't be released until the
497 * event gets handled by the event thread.
498 */
499class VirtualBox::CallbackEvent : public Event
500{
501public:
502
503 CallbackEvent (VirtualBox *aVirtualBox) : mVirtualBox (aVirtualBox)
504 {
505 Assert (aVirtualBox);
506 }
507
508 void *handler();
509
510 virtual void handleCallback (const ComPtr <IVirtualBoxCallback> &aCallback) = 0;
511
512private:
513
514 /*
515 * Note that this is a weak ref -- the CallbackEvent handler thread
516 * is bound to the lifetime of the VirtualBox instance, so it's safe.
517 */
518 ComObjPtr <VirtualBox, ComWeakRef> mVirtualBox;
519};
520
521#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