VirtualBox

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

Last change on this file since 85574 was 85309, checked in by vboxsync, 4 years ago

Main: Try harder using the Utf8Str versions of the event stuff where possible. bugref:9790

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.9 KB
Line 
1/* $Id: VirtualBoxImpl.h 85309 2020-07-13 12:56:56Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
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 (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#ifndef MAIN_INCLUDED_VirtualBoxImpl_h
19#define MAIN_INCLUDED_VirtualBoxImpl_h
20#ifndef RT_WITHOUT_PRAGMA_ONCE
21# pragma once
22#endif
23
24#include "VirtualBoxBase.h"
25#include "objectslist.h"
26#include "VirtualBoxWrap.h"
27
28#ifdef RT_OS_WINDOWS
29# include "win/resource.h"
30#endif
31
32//#ifdef DEBUG_bird
33//# define VBOXSVC_WITH_CLIENT_WATCHER
34//#endif
35
36namespace com
37{
38 class Event;
39 class EventQueue;
40}
41
42class SessionMachine;
43class GuestOSType;
44class Progress;
45class Host;
46class SystemProperties;
47class DHCPServer;
48class PerformanceCollector;
49class CloudProviderManager;
50#ifdef VBOX_WITH_EXTPACK
51class ExtPackManager;
52#endif
53class AutostartDb;
54class NATNetwork;
55#ifdef VBOX_WITH_CLOUD_NET
56class CloudNetwork;
57#endif /* VBOX_WITH_CLOUD_NET */
58
59
60typedef std::list<ComObjPtr<SessionMachine> > SessionMachinesList;
61
62#ifdef RT_OS_WINDOWS
63class SVCHlpClient;
64#endif
65
66namespace settings
67{
68 class MainConfigFile;
69 struct MediaRegistry;
70}
71
72
73#if defined(VBOX_WITH_SDS) && !defined(VBOX_WITH_XPCOM)
74class VirtualBoxClassFactory; /* See ../src-server/win/svcmain.cpp */
75#endif
76
77#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
78struct SharedClipboardAreaData;
79#endif
80
81class ATL_NO_VTABLE VirtualBox :
82 public VirtualBoxWrap
83#ifdef RT_OS_WINDOWS
84 , public ATL::CComCoClass<VirtualBox, &CLSID_VirtualBox>
85#endif
86{
87
88public:
89
90 typedef std::list<ComPtr<IInternalSessionControl> > InternalControlList;
91 typedef ObjectsList<Machine> MachinesOList;
92
93#if 0 /* obsoleted by AsyncEvent */
94 class CallbackEvent;
95 friend class CallbackEvent;
96#endif
97 class AsyncEvent;
98 friend class AsyncEvent;
99
100#ifndef VBOX_WITH_XPCOM
101# ifdef VBOX_WITH_SDS
102 DECLARE_CLASSFACTORY_EX(VirtualBoxClassFactory)
103# else
104 DECLARE_CLASSFACTORY_SINGLETON(VirtualBox)
105# endif
106#endif
107
108 // Do not use any ATL registry support.
109 //DECLARE_REGISTRY_RESOURCEID(IDR_VIRTUALBOX)
110
111 // Kind of redundant (VirtualBoxWrap declares itself not aggregatable and
112 // CComCoClass<VirtualBox, &CLSID_VirtualBox> as aggregatable, the former
113 // is the first inheritance), but the C++ multiple inheritance rules and
114 // the class factory in svcmain.cpp needs this to disambiguate.
115 DECLARE_NOT_AGGREGATABLE(VirtualBox)
116
117 // to postpone generation of the default ctor/dtor
118 DECLARE_EMPTY_CTOR_DTOR(VirtualBox)
119
120 HRESULT FinalConstruct();
121 void FinalRelease();
122
123 // public initializer/uninitializer for internal purposes only
124 HRESULT init();
125 HRESULT initMachines();
126 HRESULT initMedia(const Guid &uuidMachineRegistry,
127 const settings::MediaRegistry &mediaRegistry,
128 const Utf8Str &strMachineFolder);
129 void uninit();
130
131 // public methods only for internal purposes
132
133 /**
134 * Override of the default locking class to be used for validating lock
135 * order with the standard member lock handle.
136 */
137 virtual VBoxLockingClass getLockingClass() const
138 {
139 return LOCKCLASS_VIRTUALBOXOBJECT;
140 }
141
142#ifdef DEBUG
143 void i_dumpAllBackRefs();
144#endif
145
146 HRESULT i_postEvent(Event *event);
147
148 HRESULT i_addProgress(IProgress *aProgress);
149 HRESULT i_removeProgress(IN_GUID aId);
150
151#ifdef RT_OS_WINDOWS
152 typedef DECLCALLBACKPTR(HRESULT, SVCHelperClientFunc,(SVCHlpClient *aClient, Progress *aProgress, void *aUser, int *aVrc));
153 HRESULT i_startSVCHelperClient(bool aPrivileged,
154 SVCHelperClientFunc aFunc,
155 void *aUser, Progress *aProgress);
156#endif
157
158 void i_addProcessToReap(RTPROCESS pid);
159 void i_updateClientWatcher();
160
161 int i_loadVDPlugin(const char *pszPluginLibrary);
162 int i_unloadVDPlugin(const char *pszPluginLibrary);
163
164 void i_onMediumRegistered(const Guid &aMediumId, const DeviceType_T aDevType, const BOOL aRegistered);
165 void i_onMediumConfigChanged(IMedium *aMedium);
166 void i_onMediumChanged(IMediumAttachment* aMediumAttachment);
167 void i_onStorageControllerChanged(const Guid &aMachineId, const com::Utf8Str &aControllerName);
168 void i_onStorageDeviceChanged(IMediumAttachment* aStorageDevice, const BOOL fRemoved, const BOOL fSilent);
169 void i_onMachineStateChanged(const Guid &aId, MachineState_T aState);
170 void i_onMachineDataChanged(const Guid &aId, BOOL aTemporary = FALSE);
171 BOOL i_onExtraDataCanChange(const Guid &aId, const Utf8Str &aKey, const Utf8Str &aValue, Bstr &aError);
172 void i_onExtraDataChanged(const Guid &aId, const Utf8Str &aKey, const Utf8Str &aValue);
173 void i_onMachineRegistered(const Guid &aId, BOOL aRegistered);
174 void i_onSessionStateChanged(const Guid &aId, SessionState_T aState);
175
176 void i_onSnapshotTaken(const Guid &aMachineId, const Guid &aSnapshotId);
177 void i_onSnapshotDeleted(const Guid &aMachineId, const Guid &aSnapshotId);
178 void i_onSnapshotRestored(const Guid &aMachineId, const Guid &aSnapshotId);
179 void i_onSnapshotChanged(const Guid &aMachineId, const Guid &aSnapshotId);
180
181#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
182 int i_clipboardAreaCreate(ULONG uAreaID, uint32_t fFlags, SharedClipboardAreaData **ppAreaData);
183 int i_clipboardAreaDestroy(SharedClipboardAreaData *pAreaData);
184
185 HRESULT i_onClipboardAreaRegister(const std::vector<com::Utf8Str> &aParms, ULONG *aID);
186 HRESULT i_onClipboardAreaUnregister(ULONG aID);
187 HRESULT i_onClipboardAreaAttach(ULONG aID);
188 HRESULT i_onClipboardAreaDetach(ULONG aID);
189 ULONG i_onClipboardAreaGetMostRecent(void);
190 ULONG i_onClipboardAreaGetRefCount(ULONG aID);
191#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
192
193 void i_onGuestPropertyChanged(const Guid &aMachineId, const Utf8Str &aName, const Utf8Str &aValue, const Utf8Str &aFlags);
194 void i_onNatRedirectChanged(const Guid &aMachineId, ULONG ulSlot, bool fRemove, const Utf8Str &aName,
195 NATProtocol_T aProto, const Utf8Str &aHostIp, uint16_t aHostPort,
196 const Utf8Str &aGuestIp, uint16_t aGuestPort);
197 void i_onNATNetworkChanged(const Utf8Str &aNetworkName);
198 void i_onNATNetworkStartStop(const Utf8Str &aNetworkName, BOOL aStart);
199 void i_onNATNetworkSetting(const Utf8Str &aNetworkName, BOOL aEnabled, const Utf8Str &aNetwork,
200 const Utf8Str &aGateway, BOOL aAdvertiseDefaultIpv6RouteEnabled,
201 BOOL fNeedDhcpServer);
202 void i_onNATNetworkPortForward(const Utf8Str &aNetworkName, BOOL create, BOOL fIpv6,
203 const Utf8Str &aRuleName, NATProtocol_T proto,
204 const Utf8Str &aHostIp, LONG aHostPort,
205 const Utf8Str &aGuestIp, LONG aGuestPort);
206 void i_onHostNameResolutionConfigurationChange();
207
208 int i_natNetworkRefInc(const Utf8Str &aNetworkName);
209 int i_natNetworkRefDec(const Utf8Str &aNetworkName);
210
211#ifdef VBOX_WITH_CLOUD_NET
212 HRESULT i_findCloudNetworkByName(const com::Utf8Str &aNetworkName,
213 ComObjPtr<CloudNetwork> *aNetwork = NULL);
214 HRESULT i_getEventSource(ComPtr<IEventSource>& aSource);
215#endif /* VBOX_WITH_CLOUD_NET */
216
217 ComObjPtr<GuestOSType> i_getUnknownOSType();
218
219 void i_getOpenedMachines(SessionMachinesList &aMachines,
220 InternalControlList *aControls = NULL);
221 MachinesOList &i_getMachinesList();
222
223 HRESULT i_findMachine(const Guid &aId,
224 bool fPermitInaccessible,
225 bool aSetError,
226 ComObjPtr<Machine> *aMachine = NULL);
227
228 HRESULT i_findMachineByName(const Utf8Str &aName,
229 bool aSetError,
230 ComObjPtr<Machine> *aMachine = NULL);
231
232 HRESULT i_validateMachineGroup(const com::Utf8Str &aGroup, bool fPrimary);
233 HRESULT i_convertMachineGroups(const std::vector<com::Utf8Str> aMachineGroups, StringsList *pllMachineGroups);
234
235 HRESULT i_findHardDiskById(const Guid &id,
236 bool aSetError,
237 ComObjPtr<Medium> *aHardDisk = NULL);
238 HRESULT i_findHardDiskByLocation(const Utf8Str &strLocation,
239 bool aSetError,
240 ComObjPtr<Medium> *aHardDisk = NULL);
241 HRESULT i_findDVDOrFloppyImage(DeviceType_T mediumType,
242 const Guid *aId,
243 const Utf8Str &aLocation,
244 bool aSetError,
245 ComObjPtr<Medium> *aImage = NULL);
246 HRESULT i_findRemoveableMedium(DeviceType_T mediumType,
247 const Guid &uuid,
248 bool fRefresh,
249 bool aSetError,
250 ComObjPtr<Medium> &pMedium);
251
252 HRESULT i_findGuestOSType(const Utf8Str &strOSType,
253 ComObjPtr<GuestOSType> &guestOSType);
254
255 const Guid &i_getGlobalRegistryId() const;
256
257 const ComObjPtr<Host> &i_host() const;
258 SystemProperties *i_getSystemProperties() const;
259 CloudProviderManager *i_getCloudProviderManager() const;
260#ifdef VBOX_WITH_EXTPACK
261 ExtPackManager *i_getExtPackManager() const;
262#endif
263#ifdef VBOX_WITH_RESOURCE_USAGE_API
264 const ComObjPtr<PerformanceCollector> &i_performanceCollector() const;
265#endif /* VBOX_WITH_RESOURCE_USAGE_API */
266
267 void i_getDefaultMachineFolder(Utf8Str &str) const;
268 void i_getDefaultHardDiskFormat(Utf8Str &str) const;
269
270 /** Returns the VirtualBox home directory */
271 const Utf8Str &i_homeDir() const;
272 int i_calculateFullPath(const Utf8Str &strPath, Utf8Str &aResult);
273 void i_copyPathRelativeToConfig(const Utf8Str &strSource, Utf8Str &strTarget);
274 HRESULT i_registerMedium(const ComObjPtr<Medium> &pMedium, ComObjPtr<Medium> *ppMedium,
275 AutoWriteLock &mediaTreeLock);
276 HRESULT i_unregisterMedium(Medium *pMedium);
277 void i_pushMediumToListWithChildren(MediaList &llMedia, Medium *pMedium);
278 HRESULT i_unregisterMachineMedia(const Guid &id);
279 HRESULT i_unregisterMachine(Machine *pMachine, const Guid &id);
280 void i_rememberMachineNameChangeForMedia(const Utf8Str &strOldConfigDir,
281 const Utf8Str &strNewConfigDir);
282 void i_saveMediaRegistry(settings::MediaRegistry &mediaRegistry,
283 const Guid &uuidRegistry,
284 const Utf8Str &strMachineFolder);
285 HRESULT i_saveSettings();
286 void i_markRegistryModified(const Guid &uuid);
287 void i_unmarkRegistryModified(const Guid &uuid);
288 void i_saveModifiedRegistries();
289 static const com::Utf8Str &i_getVersionNormalized();
290 static HRESULT i_ensureFilePathExists(const Utf8Str &strFileName, bool fCreate);
291 const Utf8Str& i_settingsFilePath();
292 AutostartDb* i_getAutostartDb() const;
293 RWLockHandle& i_getMachinesListLockHandle();
294 RWLockHandle& i_getMediaTreeLockHandle();
295 int i_encryptSetting(const Utf8Str &aPlaintext, Utf8Str *aCiphertext);
296 int i_decryptSetting(Utf8Str *aPlaintext, const Utf8Str &aCiphertext);
297 void i_storeSettingsKey(const Utf8Str &aKey);
298 bool i_isMediaUuidInUse(const Guid &aId, DeviceType_T deviceType);
299
300
301
302private:
303 class ClientWatcher;
304
305 // wrapped IVirtualBox properties
306 HRESULT getVersion(com::Utf8Str &aVersion);
307 HRESULT getVersionNormalized(com::Utf8Str &aVersionNormalized);
308 HRESULT getRevision(ULONG *aRevision);
309 HRESULT getPackageType(com::Utf8Str &aPackageType);
310 HRESULT getAPIVersion(com::Utf8Str &aAPIVersion);
311 HRESULT getAPIRevision(LONG64 *aAPIRevision);
312 HRESULT getHomeFolder(com::Utf8Str &aHomeFolder);
313 HRESULT getSettingsFilePath(com::Utf8Str &aSettingsFilePath);
314 HRESULT getHost(ComPtr<IHost> &aHost);
315 HRESULT getSystemProperties(ComPtr<ISystemProperties> &aSystemProperties);
316 HRESULT getMachines(std::vector<ComPtr<IMachine> > &aMachines);
317 HRESULT getMachineGroups(std::vector<com::Utf8Str> &aMachineGroups);
318 HRESULT getHardDisks(std::vector<ComPtr<IMedium> > &aHardDisks);
319 HRESULT getDVDImages(std::vector<ComPtr<IMedium> > &aDVDImages);
320 HRESULT getFloppyImages(std::vector<ComPtr<IMedium> > &aFloppyImages);
321 HRESULT getProgressOperations(std::vector<ComPtr<IProgress> > &aProgressOperations);
322 HRESULT getGuestOSTypes(std::vector<ComPtr<IGuestOSType> > &aGuestOSTypes);
323 HRESULT getSharedFolders(std::vector<ComPtr<ISharedFolder> > &aSharedFolders);
324 HRESULT getPerformanceCollector(ComPtr<IPerformanceCollector> &aPerformanceCollector);
325 HRESULT getDHCPServers(std::vector<ComPtr<IDHCPServer> > &aDHCPServers);
326 HRESULT getNATNetworks(std::vector<ComPtr<INATNetwork> > &aNATNetworks);
327 HRESULT getEventSource(ComPtr<IEventSource> &aEventSource);
328 HRESULT getExtensionPackManager(ComPtr<IExtPackManager> &aExtensionPackManager);
329 HRESULT getInternalNetworks(std::vector<com::Utf8Str> &aInternalNetworks);
330 HRESULT getGenericNetworkDrivers(std::vector<com::Utf8Str> &aGenericNetworkDrivers);
331 HRESULT getCloudNetworks(std::vector<ComPtr<ICloudNetwork> > &aCloudNetworks);
332 HRESULT getCloudProviderManager(ComPtr<ICloudProviderManager> &aCloudProviderManager);
333
334 // wrapped IVirtualBox methods
335 HRESULT composeMachineFilename(const com::Utf8Str &aName,
336 const com::Utf8Str &aGroup,
337 const com::Utf8Str &aCreateFlags,
338 const com::Utf8Str &aBaseFolder,
339 com::Utf8Str &aFile);
340 HRESULT createMachine(const com::Utf8Str &aSettingsFile,
341 const com::Utf8Str &aName,
342 const std::vector<com::Utf8Str> &aGroups,
343 const com::Utf8Str &aOsTypeId,
344 const com::Utf8Str &aFlags,
345 ComPtr<IMachine> &aMachine);
346 HRESULT openMachine(const com::Utf8Str &aSettingsFile,
347 ComPtr<IMachine> &aMachine);
348 HRESULT registerMachine(const ComPtr<IMachine> &aMachine);
349 HRESULT findMachine(const com::Utf8Str &aNameOrId,
350 ComPtr<IMachine> &aMachine);
351 HRESULT getMachinesByGroups(const std::vector<com::Utf8Str> &aGroups,
352 std::vector<ComPtr<IMachine> > &aMachines);
353 HRESULT getMachineStates(const std::vector<ComPtr<IMachine> > &aMachines,
354 std::vector<MachineState_T> &aStates);
355 HRESULT createAppliance(ComPtr<IAppliance> &aAppliance);
356 HRESULT createUnattendedInstaller(ComPtr<IUnattended> &aUnattended);
357 HRESULT createMedium(const com::Utf8Str &aFormat,
358 const com::Utf8Str &aLocation,
359 AccessMode_T aAccessMode,
360 DeviceType_T aDeviceType,
361 ComPtr<IMedium> &aMedium);
362 HRESULT openMedium(const com::Utf8Str &aLocation,
363 DeviceType_T aDeviceType,
364 AccessMode_T aAccessMode,
365 BOOL aForceNewUuid,
366 ComPtr<IMedium> &aMedium);
367 HRESULT getGuestOSType(const com::Utf8Str &aId,
368 ComPtr<IGuestOSType> &aType);
369 HRESULT createSharedFolder(const com::Utf8Str &aName,
370 const com::Utf8Str &aHostPath,
371 BOOL aWritable,
372 BOOL aAutomount,
373 const com::Utf8Str &aAutoMountPoint);
374 HRESULT removeSharedFolder(const com::Utf8Str &aName);
375 HRESULT getExtraDataKeys(std::vector<com::Utf8Str> &aKeys);
376 HRESULT getExtraData(const com::Utf8Str &aKey,
377 com::Utf8Str &aValue);
378 HRESULT setExtraData(const com::Utf8Str &aKey,
379 const com::Utf8Str &aValue);
380 HRESULT setSettingsSecret(const com::Utf8Str &aPassword);
381 HRESULT createDHCPServer(const com::Utf8Str &aName,
382 ComPtr<IDHCPServer> &aServer);
383 HRESULT findDHCPServerByNetworkName(const com::Utf8Str &aName,
384 ComPtr<IDHCPServer> &aServer);
385 HRESULT removeDHCPServer(const ComPtr<IDHCPServer> &aServer);
386 HRESULT createNATNetwork(const com::Utf8Str &aNetworkName,
387 ComPtr<INATNetwork> &aNetwork);
388 HRESULT findNATNetworkByName(const com::Utf8Str &aNetworkName,
389 ComPtr<INATNetwork> &aNetwork);
390 HRESULT removeNATNetwork(const ComPtr<INATNetwork> &aNetwork);
391 HRESULT createCloudNetwork(const com::Utf8Str &aNetworkName,
392 ComPtr<ICloudNetwork> &aNetwork);
393 HRESULT findCloudNetworkByName(const com::Utf8Str &aNetworkName,
394 ComPtr<ICloudNetwork> &aNetwork);
395 HRESULT removeCloudNetwork(const ComPtr<ICloudNetwork> &aNetwork);
396 HRESULT checkFirmwarePresent(FirmwareType_T aFirmwareType,
397 const com::Utf8Str &aVersion,
398 com::Utf8Str &aUrl,
399 com::Utf8Str &aFile,
400 BOOL *aResult);
401
402 static HRESULT i_setErrorStaticBoth(HRESULT aResultCode, int vrc, const Utf8Str &aText)
403 {
404 return setErrorInternal(aResultCode, getStaticClassIID(), getStaticComponentName(), aText, false, true, vrc);
405 }
406
407 HRESULT i_registerMachine(Machine *aMachine);
408 HRESULT i_registerDHCPServer(DHCPServer *aDHCPServer,
409 bool aSaveRegistry = true);
410 HRESULT i_unregisterDHCPServer(DHCPServer *aDHCPServer);
411 HRESULT i_registerNATNetwork(NATNetwork *aNATNetwork,
412 bool aSaveRegistry = true);
413 HRESULT i_unregisterNATNetwork(NATNetwork *aNATNetwork,
414 bool aSaveRegistry = true);
415 HRESULT i_checkMediaForConflicts(const Guid &aId,
416 const Utf8Str &aLocation,
417 Utf8Str &aConflictType,
418 ComObjPtr<Medium> *pDupMedium);
419 int i_decryptSettings();
420 int i_decryptMediumSettings(Medium *pMedium);
421 int i_decryptSettingBytes(uint8_t *aPlaintext,
422 const uint8_t *aCiphertext,
423 size_t aCiphertextSize) const;
424 int i_encryptSettingBytes(const uint8_t *aPlaintext,
425 uint8_t *aCiphertext,
426 size_t aPlaintextSize,
427 size_t aCiphertextSize) const;
428 void i_reportDriverVersions(void);
429
430 struct Data; // opaque data structure, defined in VirtualBoxImpl.cpp
431
432 Data *m;
433
434 /* static variables (defined in VirtualBoxImpl.cpp) */
435 static com::Utf8Str sVersion;
436 static com::Utf8Str sVersionNormalized;
437 static ULONG sRevision;
438 static com::Utf8Str sPackageType;
439 static com::Utf8Str sAPIVersion;
440 static std::map<com::Utf8Str, int> sNatNetworkNameToRefCount;
441 static RWLockHandle* spMtxNatNetworkNameToRefCountLock;
442
443 static DECLCALLBACK(int) AsyncEventHandler(RTTHREAD thread, void *pvUser);
444
445#ifdef RT_OS_WINDOWS
446 friend class StartSVCHelperClientData;
447 static void i_SVCHelperClientThreadTask(StartSVCHelperClientData *pTask);
448#endif
449
450#if defined(RT_OS_WINDOWS) && defined(VBOXSVC_WITH_CLIENT_WATCHER)
451protected:
452 void i_callHook(const char *a_pszFunction) RT_OVERRIDE;
453 bool i_watchClientProcess(RTPROCESS a_pidClient, const char *a_pszFunction);
454public:
455 static void i_logCaller(const char *a_pszFormat, ...);
456private:
457
458#endif
459};
460
461////////////////////////////////////////////////////////////////////////////////
462
463#endif /* !MAIN_INCLUDED_VirtualBoxImpl_h */
464
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