VirtualBox

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

Last change on this file since 1313 was 953, checked in by vboxsync, 18 years ago

Main: Prototyped IVMDKImage.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.9 KB
Line 
1/** @file
2 *
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22#ifndef ____H_VIRTUALBOXIMPL
23#define ____H_VIRTUALBOXIMPL
24
25#include "VirtualBoxBase.h"
26#include "VirtualBoxXMLUtil.h"
27
28#include "VBox/com/EventQueue.h"
29
30#include <VBox/cfgldr.h>
31
32#include <list>
33#include <vector>
34#include <map>
35
36#ifdef __WIN__
37#include "win32/resource.h"
38#endif
39
40class Machine;
41class SessionMachine;
42class HardDisk;
43class HVirtualDiskImage;
44class DVDImage;
45class FloppyImage;
46class MachineCollection;
47class HardDiskCollection;
48class DVDImageCollection;
49class FloppyImageCollection;
50class GuestOSType;
51class GuestOSTypeCollection;
52class SharedFolder;
53class Progress;
54class ProgressCollection;
55class Host;
56class SystemProperties;
57
58#ifdef __WIN__
59class SVCHlpClient;
60#endif
61
62struct VMClientWatcherData;
63
64class ATL_NO_VTABLE VirtualBox :
65 public VirtualBoxBaseWithChildrenNEXT,
66 public VirtualBoxXMLUtil,
67 public VirtualBoxSupportErrorInfoImpl <VirtualBox, IVirtualBox>,
68 public VirtualBoxSupportTranslation <VirtualBox>,
69#ifdef __WIN__
70 public IDispatchImpl<IVirtualBox, &IID_IVirtualBox, &LIBID_VirtualBox,
71 kTypeLibraryMajorVersion, kTypeLibraryMinorVersion>,
72 public CComCoClass<VirtualBox, &CLSID_VirtualBox>
73#else
74 public IVirtualBox
75#endif
76{
77
78public:
79
80 VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT (VirtualBox)
81
82 typedef std::list <ComPtr <IVirtualBoxCallback> > CallbackList;
83 typedef std::vector <ComPtr <IVirtualBoxCallback> > CallbackVector;
84
85 class CallbackEvent;
86 friend class CallbackEvent;
87
88 DECLARE_CLASSFACTORY_SINGLETON(VirtualBox)
89
90 DECLARE_REGISTRY_RESOURCEID(IDR_VIRTUALBOX)
91 DECLARE_NOT_AGGREGATABLE(VirtualBox)
92
93 DECLARE_PROTECT_FINAL_CONSTRUCT()
94
95 BEGIN_COM_MAP(VirtualBox)
96 COM_INTERFACE_ENTRY(IDispatch)
97 COM_INTERFACE_ENTRY(ISupportErrorInfo)
98 COM_INTERFACE_ENTRY(IVirtualBox)
99 END_COM_MAP()
100
101 NS_DECL_ISUPPORTS
102
103 /* to postpone generation of the default ctor/dtor */
104 VirtualBox();
105 ~VirtualBox();
106
107 HRESULT FinalConstruct();
108 void FinalRelease();
109
110 /* public initializer/uninitializer for internal purposes only */
111 HRESULT init();
112 void uninit();
113
114 /* IVirtualBox properties */
115 STDMETHOD(COMGETTER(Version)) (BSTR *aVersion);
116 STDMETHOD(COMGETTER(HomeFolder)) (BSTR *aHomeFolder);
117 STDMETHOD(COMGETTER(Host)) (IHost **aHost);
118 STDMETHOD(COMGETTER(SystemProperties)) (ISystemProperties **aSystemProperties);
119 STDMETHOD(COMGETTER(Machines)) (IMachineCollection **aMachines);
120 STDMETHOD(COMGETTER(HardDisks)) (IHardDiskCollection **aHardDisks);
121 STDMETHOD(COMGETTER(DVDImages)) (IDVDImageCollection **aDVDImages);
122 STDMETHOD(COMGETTER(FloppyImages)) (IFloppyImageCollection **aFloppyImages);
123 STDMETHOD(COMGETTER(ProgressOperations)) (IProgressCollection **aOperations);
124 STDMETHOD(COMGETTER(GuestOSTypes)) (IGuestOSTypeCollection **aGuestOSTypes);
125 STDMETHOD(COMGETTER(SharedFolders)) (ISharedFolderCollection **aSharedFolders);
126
127 /* IVirtualBox methods */
128
129 STDMETHOD(CreateMachine) (INPTR BSTR aBaseFolder, INPTR BSTR aName,
130 IMachine **aMachine);
131 STDMETHOD(CreateLegacyMachine) (INPTR BSTR aSettingsFile, INPTR BSTR aName,
132 IMachine **aMachine);
133 STDMETHOD(OpenMachine) (INPTR BSTR aSettingsFile, IMachine **aMachine);
134 STDMETHOD(RegisterMachine) (IMachine *aMachine);
135 STDMETHOD(GetMachine) (INPTR GUIDPARAM aId, IMachine **aMachine);
136 STDMETHOD(FindMachine) (INPTR BSTR aName, IMachine **aMachine);
137 STDMETHOD(UnregisterMachine) (INPTR GUIDPARAM aId, IMachine **aMachine);
138
139 STDMETHOD(CreateHardDisk) (HardDiskStorageType_T aStorageType, IHardDisk **aHardDisk);
140 STDMETHOD(OpenHardDisk) (INPTR BSTR aLocation, IHardDisk **aHardDisk);
141 STDMETHOD(OpenVirtualDiskImage) (INPTR BSTR aFilePath, IVirtualDiskImage **aImage);
142 STDMETHOD(RegisterHardDisk) (IHardDisk *aHardDisk);
143 STDMETHOD(GetHardDisk) (INPTR GUIDPARAM id, IHardDisk **hardDisk);
144 STDMETHOD(FindVirtualDiskImage) (INPTR BSTR aFilePath, IVirtualDiskImage **aImage);
145 STDMETHOD(UnregisterHardDisk) (INPTR GUIDPARAM aId, IHardDisk **aHardDisk);
146
147 STDMETHOD(OpenDVDImage) (INPTR BSTR aFilePath, INPTR GUIDPARAM aId,
148 IDVDImage **aDVDImage);
149 STDMETHOD(RegisterDVDImage) (IDVDImage *aDVDImage);
150 STDMETHOD(GetDVDImage) (INPTR GUIDPARAM aId, IDVDImage **aDVDImage);
151 STDMETHOD(FindDVDImage) (INPTR BSTR aFilePath, IDVDImage **aDVDImage);
152 STDMETHOD(GetDVDImageUsage) (INPTR GUIDPARAM aId,
153 ResourceUsage_T aUsage,
154 BSTR *aMachineIDs);
155 STDMETHOD(UnregisterDVDImage) (INPTR GUIDPARAM aId, IDVDImage **aDVDImage);
156
157 STDMETHOD(OpenFloppyImage) (INPTR BSTR aFilePath, INPTR GUIDPARAM aId,
158 IFloppyImage **aFloppyImage);
159 STDMETHOD(RegisterFloppyImage) (IFloppyImage *aFloppyImage);
160 STDMETHOD(GetFloppyImage) (INPTR GUIDPARAM id, IFloppyImage **aFloppyImage);
161 STDMETHOD(FindFloppyImage) (INPTR BSTR aFilePath, IFloppyImage **aFloppyImage);
162 STDMETHOD(GetFloppyImageUsage) (INPTR GUIDPARAM aId,
163 ResourceUsage_T aUsage,
164 BSTR *aMachineIDs);
165 STDMETHOD(UnregisterFloppyImage) (INPTR GUIDPARAM aId, IFloppyImage **aFloppyImage);
166
167 STDMETHOD(FindGuestOSType)(INPTR BSTR aId, IGuestOSType **aType);
168 STDMETHOD(CreateSharedFolder)(INPTR BSTR aName, INPTR BSTR aHostPath);
169 STDMETHOD(RemoveSharedFolder)(INPTR BSTR aName);
170 STDMETHOD(GetNextExtraDataKey)(INPTR BSTR aKey, BSTR *aNextKey, BSTR *aNextValue);
171 STDMETHOD(GetExtraData)(INPTR BSTR aKey, BSTR *aValue);
172 STDMETHOD(SetExtraData)(INPTR BSTR aKey, INPTR BSTR aValue);
173 STDMETHOD(OpenSession) (ISession *aSession, INPTR GUIDPARAM aMachineId);
174 STDMETHOD(OpenRemoteSession) (ISession *aSession, INPTR GUIDPARAM aMachineId,
175 INPTR BSTR aType, IProgress **aProgress);
176 STDMETHOD(OpenExistingSession) (ISession *aSession, INPTR GUIDPARAM aMachineId);
177 STDMETHOD(RegisterCallback)(IVirtualBoxCallback *callback);
178 STDMETHOD(UnregisterCallback)(IVirtualBoxCallback *callback);
179
180 /* public methods only for internal purposes */
181
182 HRESULT postEvent (Event *event);
183
184 HRESULT addProgress (IProgress *aProgress);
185 HRESULT removeProgress (INPTR GUIDPARAM aId);
186
187#ifdef __WIN__
188 typedef DECLCALLBACKPTR (HRESULT, SVCHelperClientFunc)
189 (SVCHlpClient *aClient, Progress *aProgress, void *aUser, int *aVrc);
190 HRESULT startSVCHelperClient (bool aPrivileged,
191 SVCHelperClientFunc aFunc,
192 void *aUser, Progress *aProgress);
193#endif
194
195 void addProcessToReap (RTPROCESS pid);
196 void updateClientWatcher();
197
198 void onMachineStateChange (const Guid &id, MachineState_T state);
199 void onMachineDataChange (const Guid &id);
200 BOOL onExtraDataCanChange(const Guid &id, INPTR BSTR key, INPTR BSTR value);
201 void onExtraDataChange(const Guid &id, INPTR BSTR key, INPTR BSTR value);
202 void onMachineRegistered (const Guid &aId, BOOL aRegistered);
203 void onSessionStateChange (const Guid &id, SessionState_T state);
204
205 void onSnapshotTaken (const Guid &aMachineId, const Guid &aSnapshotId);
206 void onSnapshotDiscarded (const Guid &aMachineId, const Guid &aSnapshotId);
207 void onSnapshotChange (const Guid &aMachineId, const Guid &aSnapshotId);
208
209 ComPtr <IGuestOSType> getUnknownOSType();
210
211 typedef std::vector <ComObjPtr <SessionMachine> > SessionMachineVector;
212 void getOpenedMachines (SessionMachineVector &aVector);
213
214 bool isMachineIdValid (const Guid &aId)
215 {
216 return SUCCEEDED (findMachine (aId, false /* aSetError */, NULL));
217 }
218
219 /// @todo (dmik) remove and make findMachine() public instead
220 // after switching to VirtualBoxBaseNEXT
221 HRESULT getMachine (const Guid &aId, ComObjPtr <Machine> &aMachine,
222 bool aSetError = false)
223 {
224 return findMachine (aId, aSetError, &aMachine);
225 }
226
227 /// @todo (dmik) remove and make findHardDisk() public instead
228 // after switching to VirtualBoxBaseNEXT
229 HRESULT getHardDisk (const Guid &aId, ComObjPtr <HardDisk> &aHardDisk)
230 {
231 return findHardDisk (aId, true /* aDoSetError */, &aHardDisk);
232 }
233
234 BOOL getDVDImageUsage (const Guid &id, ResourceUsage_T usage,
235 Bstr *machineIDs = NULL);
236 BOOL getFloppyImageUsage (const Guid &id, ResourceUsage_T usage,
237 Bstr *machineIDs = NULL);
238
239 const ComObjPtr <Host> &host() { return mData.mHost; }
240 const ComObjPtr <SystemProperties> &systemProperties() { return mData.mSystemProperties; }
241
242 /** Returns the VirtualBox home directory */
243 const Utf8Str &homeDir() { return mData.mHomeDir; }
244
245 void calculateRelativePath (const char *aPath, Utf8Str &aResult);
246
247 enum RHD_Flags { RHD_Internal, RHD_External, RHD_OnStartUp };
248 HRESULT registerHardDisk (HardDisk *aHardDisk, RHD_Flags aFlags);
249 HRESULT unregisterHardDisk (HardDisk *aHardDisk);
250 HRESULT unregisterDiffHardDisk (HardDisk *aHardDisk);
251
252 HRESULT saveSettings() { return saveConfig(); }
253 HRESULT updateSettings (const char *aOldPath, const char *aNewPath);
254
255 const Bstr &settingsFileName() { return mData.mCfgFile.mName; }
256
257 /* for VirtualBoxSupportErrorInfoImpl */
258 static const wchar_t *getComponentName() { return L"VirtualBox"; }
259
260private:
261
262 typedef std::list <ComObjPtr <Machine> > MachineList;
263 typedef std::list <ComObjPtr <GuestOSType> > GuestOSTypeList;
264 typedef std::list <ComPtr <IProgress> > ProgressList;
265
266 typedef std::list <ComObjPtr <HardDisk> > HardDiskList;
267 typedef std::list <ComObjPtr <DVDImage> > DVDImageList;
268 typedef std::list <ComObjPtr <FloppyImage> > FloppyImageList;
269 typedef std::list <ComObjPtr <SharedFolder> > SharedFolderList;
270
271 typedef std::map <Guid, ComObjPtr <HardDisk> > HardDiskMap;
272
273 HRESULT findMachine (const Guid &aId, bool aSetError,
274 ComObjPtr <Machine> *machine = NULL);
275
276 HRESULT findHardDisk (const Guid &aId, bool aSetError,
277 ComObjPtr <HardDisk> *aHardDisk = NULL);
278
279 HRESULT findVirtualDiskImage (const Guid *aId, const BSTR aFilePathFull,
280 bool aSetError, ComObjPtr <HVirtualDiskImage> *aImage = NULL);
281 HRESULT findDVDImage (const Guid *aId, const BSTR aFilePathFull,
282 bool aSetError, ComObjPtr <DVDImage> *aImage = NULL);
283 HRESULT findFloppyImage (const Guid *aId, const BSTR aFilePathFull,
284 bool aSetError, ComObjPtr <FloppyImage> *aImage = NULL);
285
286 HRESULT checkMediaForConflicts (HardDisk *aHardDisk,
287 const Guid *aId, const BSTR aFilePathFull);
288
289 HRESULT loadMachines (CFGNODE aGlobal);
290 HRESULT loadDisks (CFGNODE aGlobal);
291 HRESULT loadHardDisks (CFGNODE aNode);
292
293 HRESULT saveConfig();
294 HRESULT saveHardDisks (CFGNODE aNode);
295
296 HRESULT registerMachine (Machine *aMachine);
297
298 HRESULT registerDVDImage (DVDImage *aImage, bool aOnStartUp);
299 HRESULT registerFloppyImage (FloppyImage *aImage, bool aOnStartUp);
300 HRESULT registerGuestOSTypes();
301
302 HRESULT lockConfig();
303 HRESULT unlockConfig();
304
305 /** @note This method is not thread safe */
306 bool isConfigLocked() { return mData.mCfgFile.mHandle != NIL_RTFILE; }
307
308 /**
309 * Main VirtualBox data structure.
310 * @note |const| members are persistent during lifetime so can be accessed
311 * without locking.
312 */
313 struct Data
314 {
315 Data();
316
317 struct CfgFile
318 {
319 CfgFile() : mHandle (NIL_RTFILE) {}
320
321 const Bstr mName;
322 RTFILE mHandle;
323 };
324
325 // const data members not requiring locking
326 const Utf8Str mHomeDir;
327
328 // const objects not requiring locking
329 const ComObjPtr <Host> mHost;
330 const ComObjPtr <SystemProperties> mSystemProperties;
331
332 CfgFile mCfgFile;
333
334 MachineList mMachines;
335 GuestOSTypeList mGuestOSTypes;
336
337 ProgressList mProgressOperations;
338 HardDiskList mHardDisks;
339 DVDImageList mDVDImages;
340 FloppyImageList mFloppyImages;
341 SharedFolderList mSharedFolders;
342
343 HardDiskMap mHardDiskMap;
344
345 CallbackList mCallbacks;
346 };
347
348 Data mData;
349
350 /** Client watcher thread data structure */
351 struct ClientWatcherData
352 {
353 ClientWatcherData()
354#if defined(__WIN__)
355 : mUpdateReq (NULL)
356#else
357 : mUpdateReq (NIL_RTSEMEVENT)
358#endif
359 , mThread (NIL_RTTHREAD) {}
360
361 // const objects not requiring locking
362#if defined(__WIN__)
363 const HANDLE mUpdateReq;
364#else
365 const RTSEMEVENT mUpdateReq;
366#endif
367 const RTTHREAD mThread;
368
369 typedef std::list <RTPROCESS> ProcessList;
370 ProcessList mProcesses;
371 };
372
373 ClientWatcherData mWatcherData;
374
375 const RTTHREAD mAsyncEventThread;
376 EventQueue * const mAsyncEventQ;
377 /** Lock for calling EventQueue->post() */
378 AutoLock::Handle mAsyncEventQLock;
379
380 static Bstr sVersion;
381
382 static DECLCALLBACK(int) clientWatcher (RTTHREAD thread, void *pvUser);
383 static DECLCALLBACK(int) asyncEventHandler (RTTHREAD thread, void *pvUser);
384
385#ifdef __WIN__
386 static DECLCALLBACK(int) SVCHelperClientThread (RTTHREAD aThread, void *aUser);
387#endif
388};
389
390////////////////////////////////////////////////////////////////////////////////
391
392/**
393 * Abstract callback event class to asynchronously call VirtualBox callbacks
394 * on a dedicated event thread. Subclasses reimplement #handleCallback()
395 * to call appropriate IVirtualBoxCallback methods depending on the event
396 * to be dispatched.
397 *
398 * @note The VirtualBox instance passed to the constructor is strongly
399 * referenced, so that the VirtualBox singleton won't be released until the
400 * event gets handled by the event thread.
401 */
402class VirtualBox::CallbackEvent : public Event
403{
404public:
405
406 CallbackEvent (VirtualBox *aVirtualBox) : mVirtualBox (aVirtualBox)
407 {
408 Assert (aVirtualBox);
409 }
410
411 void *handler();
412
413 virtual void handleCallback (const ComPtr <IVirtualBoxCallback> &aCallback) = 0;
414
415private:
416
417 /*
418 * Note that this is a weak ref -- the CallbackEvent handler thread
419 * is bound to the lifetime of the VirtualBox instance, so it's safe.
420 */
421 ComObjPtr <VirtualBox, ComWeakRef> mVirtualBox;
422};
423
424#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