1 | /* $Id: UIGlobalSession.cpp 103766 2024-03-11 13:56:54Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIGlobalSession class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | /* Qt includes: */
|
---|
29 | #include <QApplication>
|
---|
30 |
|
---|
31 | /* GUI includes: */
|
---|
32 | #include "UIGlobalSession.h"
|
---|
33 | #include "UIGuestOSType.h"
|
---|
34 | #include "UIMessageCenter.h"
|
---|
35 | #include "UIVirtualBoxClientEventHandler.h"
|
---|
36 |
|
---|
37 | /* Other VBox includes: */
|
---|
38 | #ifdef VBOX_WITH_XPCOM
|
---|
39 | # include <iprt/path.h>
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | /* VirtualBox interface declarations: */
|
---|
43 | #include <VBox/com/VirtualBox.h> // for CLSID_VirtualBoxClient
|
---|
44 |
|
---|
45 |
|
---|
46 | /* static */
|
---|
47 | UIGlobalSession *UIGlobalSession::s_pInstance = 0;
|
---|
48 |
|
---|
49 | /* static */
|
---|
50 | void UIGlobalSession::create()
|
---|
51 | {
|
---|
52 | /* Make sure instance is NOT created yet: */
|
---|
53 | AssertReturnVoid(!s_pInstance);
|
---|
54 |
|
---|
55 | /* Create instance: */
|
---|
56 | new UIGlobalSession;
|
---|
57 | }
|
---|
58 |
|
---|
59 | /* static */
|
---|
60 | void UIGlobalSession::destroy()
|
---|
61 | {
|
---|
62 | /* Make sure instance is NOT destroyed yet: */
|
---|
63 | AssertPtrReturnVoid(s_pInstance);
|
---|
64 |
|
---|
65 | /* Destroy instance: */
|
---|
66 | delete s_pInstance;
|
---|
67 | s_pInstance = 0;
|
---|
68 | }
|
---|
69 |
|
---|
70 | bool UIGlobalSession::prepare()
|
---|
71 | {
|
---|
72 | /* Prepare guest OS type manager before COM stuff: */
|
---|
73 | m_pGuestOSTypeManager = new UIGuestOSTypeManager;
|
---|
74 |
|
---|
75 | /* Init COM: */
|
---|
76 | HRESULT rc = COMBase::InitializeCOM(true);
|
---|
77 | if (FAILED(rc))
|
---|
78 | {
|
---|
79 | #ifdef VBOX_WITH_XPCOM
|
---|
80 | if (rc == NS_ERROR_FILE_ACCESS_DENIED)
|
---|
81 | {
|
---|
82 | char szHome[RTPATH_MAX] = "";
|
---|
83 | com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome));
|
---|
84 | msgCenter().cannotInitUserHome(QString(szHome));
|
---|
85 | }
|
---|
86 | else
|
---|
87 | #endif
|
---|
88 | msgCenter().cannotInitCOM(rc);
|
---|
89 | return false;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /* Make sure VirtualBoxClient instance created: */
|
---|
93 | m_comVBoxClient.createInstance(CLSID_VirtualBoxClient);
|
---|
94 | if (!m_comVBoxClient.isOk())
|
---|
95 | {
|
---|
96 | msgCenter().cannotCreateVirtualBoxClient(m_comVBoxClient);
|
---|
97 | return false;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /* Init wrappers: */
|
---|
101 | comWrappersReinit();
|
---|
102 |
|
---|
103 | /* Watch for the VBoxSVC availability changes: */
|
---|
104 | connect(gVBoxClientEvents, &UIVirtualBoxClientEventHandler::sigVBoxSVCAvailabilityChange,
|
---|
105 | this, &UIGlobalSession::sltHandleVBoxSVCAvailabilityChange);
|
---|
106 |
|
---|
107 | /* Success finally: */
|
---|
108 | return true;
|
---|
109 | }
|
---|
110 |
|
---|
111 | void UIGlobalSession::cleanup()
|
---|
112 | {
|
---|
113 | /* Cleanup guest OS type manager before COM stuff: */
|
---|
114 | delete m_pGuestOSTypeManager;
|
---|
115 | m_pGuestOSTypeManager = 0;
|
---|
116 |
|
---|
117 | /* Starting COM cleanup: */
|
---|
118 | m_comCleanupProtectionToken.lockForWrite();
|
---|
119 |
|
---|
120 | /* Detach COM wrappers: */
|
---|
121 | m_comHost.detach();
|
---|
122 | m_comVBox.detach();
|
---|
123 | m_comVBoxClient.detach();
|
---|
124 |
|
---|
125 | /* There may be COM related event instances still in the message queue
|
---|
126 | * which reference COM objects. Remove them to release those objects
|
---|
127 | * before uninitializing the COM subsystem. */
|
---|
128 | QApplication::removePostedEvents(this);
|
---|
129 |
|
---|
130 | /* Finally cleanup COM itself: */
|
---|
131 | COMBase::CleanupCOM();
|
---|
132 |
|
---|
133 | /* Finishing COM cleanup: */
|
---|
134 | m_comCleanupProtectionToken.unlock();
|
---|
135 | }
|
---|
136 |
|
---|
137 | const UIGuestOSTypeManager &UIGlobalSession::guestOSTypeManager()
|
---|
138 | {
|
---|
139 | /* Handle exceptional and undesired case!
|
---|
140 | * This object is created and destroyed within own timeframe.
|
---|
141 | * If pointer isn't yet initialized or already cleaned up,
|
---|
142 | * something is definitely wrong. */
|
---|
143 | AssertPtr(m_pGuestOSTypeManager);
|
---|
144 | if (!m_pGuestOSTypeManager)
|
---|
145 | {
|
---|
146 | m_pGuestOSTypeManager = new UIGuestOSTypeManager;
|
---|
147 | m_pGuestOSTypeManager->reCacheGuestOSTypes();
|
---|
148 | }
|
---|
149 |
|
---|
150 | /* Return an object instance: */
|
---|
151 | return *m_pGuestOSTypeManager;
|
---|
152 | }
|
---|
153 |
|
---|
154 | UIGlobalSession::UIGlobalSession()
|
---|
155 | : m_fWrappersValid(false)
|
---|
156 | , m_fVBoxSVCAvailable(true)
|
---|
157 | , m_pGuestOSTypeManager(0)
|
---|
158 | {
|
---|
159 | /* Assign instance: */
|
---|
160 | s_pInstance = this;
|
---|
161 | }
|
---|
162 |
|
---|
163 | UIGlobalSession::~UIGlobalSession()
|
---|
164 | {
|
---|
165 | /* Unassign instance: */
|
---|
166 | s_pInstance = 0;
|
---|
167 | }
|
---|
168 |
|
---|
169 | void UIGlobalSession::sltHandleVBoxSVCAvailabilityChange(bool fAvailable)
|
---|
170 | {
|
---|
171 | /* Make sure the VBoxSVC availability changed: */
|
---|
172 | if (m_fVBoxSVCAvailable == fAvailable)
|
---|
173 | return;
|
---|
174 |
|
---|
175 | /* Cache the new VBoxSVC availability value: */
|
---|
176 | m_fVBoxSVCAvailable = fAvailable;
|
---|
177 |
|
---|
178 | /* If VBoxSVC is not available: */
|
---|
179 | if (!m_fVBoxSVCAvailable)
|
---|
180 | {
|
---|
181 | /* Mark wrappers invalid: */
|
---|
182 | m_fWrappersValid = false;
|
---|
183 |
|
---|
184 | /* Re-fetch corresponding CVirtualBox to restart VBoxSVC: */
|
---|
185 | CVirtualBoxClient comVBoxClient = virtualBoxClient();
|
---|
186 | m_comVBox = comVBoxClient.GetVirtualBox();
|
---|
187 | if (!comVBoxClient.isOk())
|
---|
188 | {
|
---|
189 | // The proper behavior would be to show the message and to exit the app, e.g.:
|
---|
190 | // msgCenter().cannotAcquireVirtualBox(m_comVBoxClient);
|
---|
191 | // return QApplication::quit();
|
---|
192 | // But CVirtualBox is still NULL in current Main implementation,
|
---|
193 | // and this call do not restart anything, so we are waiting
|
---|
194 | // for subsequent event about VBoxSVC is available again.
|
---|
195 | }
|
---|
196 | }
|
---|
197 | /* If VBoxSVC is available: */
|
---|
198 | else
|
---|
199 | {
|
---|
200 | /* Try to re-init wrappers, quit if failed: */
|
---|
201 | if ( !m_fWrappersValid
|
---|
202 | && !comWrappersReinit())
|
---|
203 | return QApplication::quit();
|
---|
204 | }
|
---|
205 |
|
---|
206 | /* Notify listeners about the VBoxSVC availability change: */
|
---|
207 | emit sigVBoxSVCAvailabilityChange(m_fVBoxSVCAvailable);
|
---|
208 | }
|
---|
209 |
|
---|
210 | bool UIGlobalSession::comWrappersReinit()
|
---|
211 | {
|
---|
212 | /* Make sure VirtualBox instance acquired: */
|
---|
213 | CVirtualBoxClient comVBoxClient = virtualBoxClient();
|
---|
214 | m_comVBox = comVBoxClient.GetVirtualBox();
|
---|
215 | if (!comVBoxClient.isOk())
|
---|
216 | {
|
---|
217 | msgCenter().cannotAcquireVirtualBox(comVBoxClient);
|
---|
218 | return false;
|
---|
219 | }
|
---|
220 |
|
---|
221 | /* Acquire host: */
|
---|
222 | CVirtualBox comVBox = virtualBox();
|
---|
223 | m_comHost = comVBox.GetHost();
|
---|
224 | // if (!comVBox.isOk())
|
---|
225 | // {
|
---|
226 | // msgCenter().cannotAcquireVirtualBoxParameter(comVBoxClient);
|
---|
227 | // return false;
|
---|
228 | // }
|
---|
229 |
|
---|
230 | /* Acquire home folder: */
|
---|
231 | m_strHomeFolder = comVBox.GetHomeFolder();
|
---|
232 | // if (!comVBox.isOk())
|
---|
233 | // {
|
---|
234 | // msgCenter().cannotAcquireVirtualBoxParameter(comVBoxClient);
|
---|
235 | // return false;
|
---|
236 | // }
|
---|
237 |
|
---|
238 | /* Re-initialize guest OS type database: */
|
---|
239 | if (m_pGuestOSTypeManager)
|
---|
240 | m_pGuestOSTypeManager->reCacheGuestOSTypes();
|
---|
241 |
|
---|
242 | /* Mark wrappers valid: */
|
---|
243 | m_fWrappersValid = true;
|
---|
244 |
|
---|
245 | /* Success finally: */
|
---|
246 | return true;
|
---|
247 | }
|
---|