VirtualBox

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

Last change on this file since 7784 was 7387, checked in by vboxsync, 17 years ago

Main/Settings: Perform conversion in a loop to allow for multi-step version to version updates (e.g. v1.0->v1.1->v1.2) which saves from patching all previous transformation rules when on every version change but still makes it possible to update from too old versions to the most recent one.

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