VirtualBox

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

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

Ported s2 branch (r37120:38456).

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