VirtualBox

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

Last change on this file since 7207 was 6909, checked in by vboxsync, 17 years ago

Main: Added IMachine::machines2 that returns the array instead of the collection (will supercede IMachine::machines after web services are fixed).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.7 KB
Line 
1/* $Id: VirtualBoxImpl.h 6909 2008-02-11 20:00:12Z 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(Host)) (IHost **aHost);
112 STDMETHOD(COMGETTER(SystemProperties)) (ISystemProperties **aSystemProperties);
113 STDMETHOD(COMGETTER(Machines)) (IMachineCollection **aMachines);
114 STDMETHOD(COMGETTER(Machines2)) (ComSafeArrayOut (IMachine *, aMachines));
115 STDMETHOD(COMGETTER(HardDisks)) (IHardDiskCollection **aHardDisks);
116 STDMETHOD(COMGETTER(DVDImages)) (IDVDImageCollection **aDVDImages);
117 STDMETHOD(COMGETTER(FloppyImages)) (IFloppyImageCollection **aFloppyImages);
118 STDMETHOD(COMGETTER(ProgressOperations)) (IProgressCollection **aOperations);
119 STDMETHOD(COMGETTER(GuestOSTypes)) (IGuestOSTypeCollection **aGuestOSTypes);
120 STDMETHOD(COMGETTER(SharedFolders)) (ISharedFolderCollection **aSharedFolders);
121
122 /* IVirtualBox methods */
123
124 STDMETHOD(CreateMachine) (INPTR BSTR aBaseFolder, INPTR BSTR aName,
125 INPTR GUIDPARAM aId, IMachine **aMachine);
126 STDMETHOD(CreateLegacyMachine) (INPTR BSTR aSettingsFile, INPTR BSTR aName,
127 INPTR GUIDPARAM aId, IMachine **aMachine);
128 STDMETHOD(OpenMachine) (INPTR BSTR aSettingsFile, IMachine **aMachine);
129 STDMETHOD(RegisterMachine) (IMachine *aMachine);
130 STDMETHOD(GetMachine) (INPTR GUIDPARAM aId, IMachine **aMachine);
131 STDMETHOD(FindMachine) (INPTR BSTR aName, IMachine **aMachine);
132 STDMETHOD(UnregisterMachine) (INPTR GUIDPARAM aId, IMachine **aMachine);
133
134 STDMETHOD(CreateHardDisk) (HardDiskStorageType_T aStorageType, IHardDisk **aHardDisk);
135 STDMETHOD(OpenHardDisk) (INPTR BSTR aLocation, IHardDisk **aHardDisk);
136 STDMETHOD(OpenVirtualDiskImage) (INPTR BSTR aFilePath, IVirtualDiskImage **aImage);
137 STDMETHOD(RegisterHardDisk) (IHardDisk *aHardDisk);
138 STDMETHOD(GetHardDisk) (INPTR GUIDPARAM aId, IHardDisk **aHardDisk);
139 STDMETHOD(FindHardDisk) (INPTR BSTR aLocation, IHardDisk **aHardDisk);
140 STDMETHOD(FindVirtualDiskImage) (INPTR BSTR aFilePath, IVirtualDiskImage **aImage);
141 STDMETHOD(UnregisterHardDisk) (INPTR GUIDPARAM aId, IHardDisk **aHardDisk);
142
143 STDMETHOD(OpenDVDImage) (INPTR BSTR aFilePath, INPTR GUIDPARAM aId,
144 IDVDImage **aDVDImage);
145 STDMETHOD(RegisterDVDImage) (IDVDImage *aDVDImage);
146 STDMETHOD(GetDVDImage) (INPTR GUIDPARAM aId, IDVDImage **aDVDImage);
147 STDMETHOD(FindDVDImage) (INPTR BSTR aFilePath, IDVDImage **aDVDImage);
148 STDMETHOD(GetDVDImageUsage) (INPTR GUIDPARAM aId,
149 ResourceUsage_T aUsage,
150 BSTR *aMachineIDs);
151 STDMETHOD(UnregisterDVDImage) (INPTR GUIDPARAM aId, IDVDImage **aDVDImage);
152
153 STDMETHOD(OpenFloppyImage) (INPTR BSTR aFilePath, INPTR GUIDPARAM aId,
154 IFloppyImage **aFloppyImage);
155 STDMETHOD(RegisterFloppyImage) (IFloppyImage *aFloppyImage);
156 STDMETHOD(GetFloppyImage) (INPTR GUIDPARAM id, IFloppyImage **aFloppyImage);
157 STDMETHOD(FindFloppyImage) (INPTR BSTR aFilePath, IFloppyImage **aFloppyImage);
158 STDMETHOD(GetFloppyImageUsage) (INPTR GUIDPARAM aId,
159 ResourceUsage_T aUsage,
160 BSTR *aMachineIDs);
161 STDMETHOD(UnregisterFloppyImage) (INPTR GUIDPARAM aId, IFloppyImage **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 /* public methods only for internal purposes */
182
183 HRESULT postEvent (Event *event);
184
185 HRESULT addProgress (IProgress *aProgress);
186 HRESULT removeProgress (INPTR GUIDPARAM aId);
187
188#ifdef RT_OS_WINDOWS
189 typedef DECLCALLBACKPTR (HRESULT, SVCHelperClientFunc)
190 (SVCHlpClient *aClient, Progress *aProgress, void *aUser, int *aVrc);
191 HRESULT startSVCHelperClient (bool aPrivileged,
192 SVCHelperClientFunc aFunc,
193 void *aUser, Progress *aProgress);
194#endif
195
196 void addProcessToReap (RTPROCESS pid);
197 void updateClientWatcher();
198
199 void onMachineStateChange (const Guid &aId, MachineState_T aState);
200 void onMachineDataChange (const Guid &aId);
201 BOOL onExtraDataCanChange(const Guid &aId, INPTR BSTR aKey, INPTR BSTR aValue,
202 Bstr &aError);
203 void onExtraDataChange(const Guid &aId, INPTR BSTR aKey, INPTR BSTR aValue);
204 void onMachineRegistered (const Guid &aId, BOOL aRegistered);
205 void onSessionStateChange (const Guid &aId, SessionState_T aState);
206
207 void onSnapshotTaken (const Guid &aMachineId, const Guid &aSnapshotId);
208 void onSnapshotDiscarded (const Guid &aMachineId, const Guid &aSnapshotId);
209 void onSnapshotChange (const Guid &aMachineId, const Guid &aSnapshotId);
210
211 ComObjPtr <GuestOSType> getUnknownOSType();
212
213 typedef std::vector <ComObjPtr <SessionMachine> > SessionMachineVector;
214 void getOpenedMachines (SessionMachineVector &aVector);
215
216 bool isMachineIdValid (const Guid &aId)
217 {
218 return SUCCEEDED (findMachine (aId, false /* aSetError */, NULL));
219 }
220
221 /// @todo (dmik) remove and make findMachine() public instead
222 // after switching to VirtualBoxBaseNEXT
223 HRESULT getMachine (const Guid &aId, ComObjPtr <Machine> &aMachine,
224 bool aSetError = false)
225 {
226 return findMachine (aId, aSetError, &aMachine);
227 }
228
229 /// @todo (dmik) remove and make findHardDisk() public instead
230 // after switching to VirtualBoxBaseNEXT
231 HRESULT getHardDisk (const Guid &aId, ComObjPtr <HardDisk> &aHardDisk)
232 {
233 return findHardDisk (&aId, NULL, true /* aDoSetError */, &aHardDisk);
234 }
235
236 bool getDVDImageUsage (const Guid &aId, ResourceUsage_T aUsage,
237 Bstr *aMachineIDs = NULL);
238 bool getFloppyImageUsage (const Guid &aId, ResourceUsage_T aUsage,
239 Bstr *aMachineIDs = NULL);
240
241 const ComObjPtr <Host> &host() { return mData.mHost; }
242 const ComObjPtr <SystemProperties> &systemProperties() { return mData.mSystemProperties; }
243
244 /** Returns the VirtualBox home directory */
245 const Utf8Str &homeDir() { return mData.mHomeDir; }
246
247 void calculateRelativePath (const char *aPath, Utf8Str &aResult);
248
249 enum RHD_Flags { RHD_Internal, RHD_External, RHD_OnStartUp };
250 HRESULT registerHardDisk (HardDisk *aHardDisk, RHD_Flags aFlags);
251 HRESULT unregisterHardDisk (HardDisk *aHardDisk);
252 HRESULT unregisterDiffHardDisk (HardDisk *aHardDisk);
253
254 HRESULT saveSettings();
255 HRESULT updateSettings (const char *aOldPath, const char *aNewPath);
256
257 const Bstr &settingsFileName() { return mData.mCfgFile.mName; }
258
259 class SettingsInputResolver : public settings::XmlTreeBackend::InputResolver
260 {
261 public:
262
263 settings::Input *resolveEntity (const char *aURI, const char *aID);
264 };
265
266 static HRESULT loadSettingsTree (settings::XmlTreeBackend &aTree,
267 settings::File &aFile,
268 bool aValidate,
269 bool aCatchLoadErrors,
270 bool aAddDefaults);
271
272 /**
273 * Shortcut to loadSettingsTree (aTree, aFile, true, true, true).
274 *
275 * Used when the settings file is to be loaded for the first time for the
276 * given object in order to recreate it from the stored settings.
277 */
278 static HRESULT loadSettingsTree_FirstTime (settings::XmlTreeBackend &aTree,
279 settings::File &aFile)
280 {
281 return loadSettingsTree (aTree, aFile, true, true, true);
282 }
283
284 /**
285 * Shortcut to loadSettingsTree (aTree, aFile, true, false, true).
286 *
287 * Used when the settings file is loaded again (after it has been fully
288 * checked and validated by #loadSettingsTree_FirstTime()) in order to
289 * look at settings that don't have any representation within object's
290 * data fields.
291 */
292 static HRESULT loadSettingsTree_Again (settings::XmlTreeBackend &aTree,
293 settings::File &aFile)
294 {
295 return loadSettingsTree (aTree, aFile, true, false, true);
296 }
297
298 /**
299 * Shortcut to loadSettingsTree (aTree, aFile, true, false, false).
300 *
301 * Used when the settings file is loaded again (after it has been fully
302 * checked and validated by #loadSettingsTree_FirstTime()) in order to
303 * update some settings and then save them back.
304 */
305 static HRESULT loadSettingsTree_ForUpdate (settings::XmlTreeBackend &aTree,
306 settings::File &aFile)
307 {
308 return loadSettingsTree (aTree, aFile, true, false, false);
309 }
310
311 static HRESULT saveSettingsTree (settings::TreeBackend &aTree,
312 settings::File &aFile);
313
314 static HRESULT handleUnexpectedExceptions (RT_SRC_POS_DECL);
315
316 /* for VirtualBoxSupportErrorInfoImpl */
317 static const wchar_t *getComponentName() { return L"VirtualBox"; }
318
319private:
320
321 typedef std::list <ComObjPtr <Machine> > MachineList;
322 typedef std::list <ComObjPtr <GuestOSType> > GuestOSTypeList;
323 typedef std::list <ComPtr <IProgress> > ProgressList;
324
325 typedef std::list <ComObjPtr <HardDisk> > HardDiskList;
326 typedef std::list <ComObjPtr <DVDImage> > DVDImageList;
327 typedef std::list <ComObjPtr <FloppyImage> > FloppyImageList;
328 typedef std::list <ComObjPtr <SharedFolder> > SharedFolderList;
329
330 typedef std::map <Guid, ComObjPtr <HardDisk> > HardDiskMap;
331
332 HRESULT findMachine (const Guid &aId, bool aSetError,
333 ComObjPtr <Machine> *machine = NULL);
334
335 HRESULT findHardDisk (const Guid *aId, const BSTR aLocation,
336 bool aSetError, ComObjPtr <HardDisk> *aHardDisk = NULL);
337
338 HRESULT findVirtualDiskImage (const Guid *aId, const BSTR aFilePathFull,
339 bool aSetError, ComObjPtr <HVirtualDiskImage> *aImage = NULL);
340 HRESULT findDVDImage (const Guid *aId, const BSTR aFilePathFull,
341 bool aSetError, ComObjPtr <DVDImage> *aImage = NULL);
342 HRESULT findFloppyImage (const Guid *aId, const BSTR aFilePathFull,
343 bool aSetError, ComObjPtr <FloppyImage> *aImage = NULL);
344
345 HRESULT checkMediaForConflicts (HardDisk *aHardDisk,
346 const Guid *aId, const BSTR aFilePathFull);
347
348 HRESULT loadMachines (const settings::Key &aGlobal);
349 HRESULT loadDisks (const settings::Key &aGlobal);
350 HRESULT loadHardDisks (const settings::Key &aNode);
351
352 HRESULT saveHardDisks (settings::Key &aNode);
353
354 HRESULT registerMachine (Machine *aMachine);
355
356 HRESULT registerDVDImage (DVDImage *aImage, bool aOnStartUp);
357 HRESULT registerFloppyImage (FloppyImage *aImage, bool aOnStartUp);
358 HRESULT registerGuestOSTypes();
359
360 HRESULT lockConfig();
361 HRESULT unlockConfig();
362
363 /** @note This method is not thread safe */
364 bool isConfigLocked() { return mData.mCfgFile.mHandle != NIL_RTFILE; }
365
366 /**
367 * Main VirtualBox data structure.
368 * @note |const| members are persistent during lifetime so can be accessed
369 * without locking.
370 */
371 struct Data
372 {
373 Data();
374
375 struct CfgFile
376 {
377 CfgFile() : mHandle (NIL_RTFILE) {}
378
379 const Bstr mName;
380 RTFILE mHandle;
381 };
382
383 // const data members not requiring locking
384 const Utf8Str mHomeDir;
385
386 // const objects not requiring locking
387 const ComObjPtr <Host> mHost;
388 const ComObjPtr <SystemProperties> mSystemProperties;
389
390 CfgFile mCfgFile;
391
392 MachineList mMachines;
393 GuestOSTypeList mGuestOSTypes;
394
395 ProgressList mProgressOperations;
396 HardDiskList mHardDisks;
397 DVDImageList mDVDImages;
398 FloppyImageList mFloppyImages;
399 SharedFolderList mSharedFolders;
400
401 HardDiskMap mHardDiskMap;
402
403 CallbackList mCallbacks;
404 };
405
406 Data mData;
407
408 /** Client watcher thread data structure */
409 struct ClientWatcherData
410 {
411 ClientWatcherData()
412#if defined(RT_OS_WINDOWS)
413 : mUpdateReq (NULL)
414#elif defined(RT_OS_OS2)
415 : mUpdateReq (NIL_RTSEMEVENT)
416#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
417 : mUpdateReq (NIL_RTSEMEVENT)
418#else
419# error "Port me!"
420#endif
421 , mThread (NIL_RTTHREAD) {}
422
423 // const objects not requiring locking
424#if defined(RT_OS_WINDOWS)
425 const HANDLE mUpdateReq;
426#elif defined(RT_OS_OS2)
427 const RTSEMEVENT mUpdateReq;
428#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
429 const RTSEMEVENT mUpdateReq;
430#else
431# error "Port me!"
432#endif
433 const RTTHREAD mThread;
434
435 typedef std::list <RTPROCESS> ProcessList;
436 ProcessList mProcesses;
437 };
438
439 ClientWatcherData mWatcherData;
440
441 const RTTHREAD mAsyncEventThread;
442 EventQueue * const mAsyncEventQ;
443 /** Lock for calling EventQueue->post() */
444 AutoLock::Handle mAsyncEventQLock;
445
446 static Bstr sVersion;
447
448 static DECLCALLBACK(int) ClientWatcher (RTTHREAD thread, void *pvUser);
449 static DECLCALLBACK(int) AsyncEventHandler (RTTHREAD thread, void *pvUser);
450
451#ifdef RT_OS_WINDOWS
452 static DECLCALLBACK(int) SVCHelperClientThread (RTTHREAD aThread, void *aUser);
453#endif
454};
455
456////////////////////////////////////////////////////////////////////////////////
457
458/**
459 * Abstract callback event class to asynchronously call VirtualBox callbacks
460 * on a dedicated event thread. Subclasses reimplement #handleCallback()
461 * to call appropriate IVirtualBoxCallback methods depending on the event
462 * to be dispatched.
463 *
464 * @note The VirtualBox instance passed to the constructor is strongly
465 * referenced, so that the VirtualBox singleton won't be released until the
466 * event gets handled by the event thread.
467 */
468class VirtualBox::CallbackEvent : public Event
469{
470public:
471
472 CallbackEvent (VirtualBox *aVirtualBox) : mVirtualBox (aVirtualBox)
473 {
474 Assert (aVirtualBox);
475 }
476
477 void *handler();
478
479 virtual void handleCallback (const ComPtr <IVirtualBoxCallback> &aCallback) = 0;
480
481private:
482
483 /*
484 * Note that this is a weak ref -- the CallbackEvent handler thread
485 * is bound to the lifetime of the VirtualBox instance, so it's safe.
486 */
487 ComObjPtr <VirtualBox, ComWeakRef> mVirtualBox;
488};
489
490#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