1 | /* $Id: CloudProviderManagerImpl.cpp 85247 2020-07-11 23:09:15Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * ICloudProviderManager COM class implementations.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-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 |
|
---|
19 | #define LOG_GROUP LOG_GROUP_MAIN_CLOUDPROVIDERMANAGER
|
---|
20 | #include <VBox/com/array.h>
|
---|
21 |
|
---|
22 | #include "VirtualBoxImpl.h"
|
---|
23 | #include "CloudProviderManagerImpl.h"
|
---|
24 | #include "ExtPackManagerImpl.h"
|
---|
25 | #include "AutoCaller.h"
|
---|
26 | #include "LoggingNew.h"
|
---|
27 |
|
---|
28 |
|
---|
29 | ////////////////////////////////////////////////////////////////////////////////
|
---|
30 | //
|
---|
31 | // CloudProviderManager constructor / destructor
|
---|
32 | //
|
---|
33 | // ////////////////////////////////////////////////////////////////////////////////
|
---|
34 | CloudProviderManager::CloudProviderManager()
|
---|
35 | {
|
---|
36 | }
|
---|
37 |
|
---|
38 | CloudProviderManager::~CloudProviderManager()
|
---|
39 | {
|
---|
40 | }
|
---|
41 |
|
---|
42 |
|
---|
43 | HRESULT CloudProviderManager::FinalConstruct()
|
---|
44 | {
|
---|
45 | return BaseFinalConstruct();
|
---|
46 | }
|
---|
47 |
|
---|
48 | void CloudProviderManager::FinalRelease()
|
---|
49 | {
|
---|
50 | uninit();
|
---|
51 |
|
---|
52 | BaseFinalRelease();
|
---|
53 | }
|
---|
54 |
|
---|
55 | HRESULT CloudProviderManager::init()
|
---|
56 | {
|
---|
57 | // Enclose the state transition NotReady->InInit->Ready.
|
---|
58 | AutoInitSpan autoInitSpan(this);
|
---|
59 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
60 |
|
---|
61 | m_apCloudProviders.clear();
|
---|
62 |
|
---|
63 | autoInitSpan.setSucceeded();
|
---|
64 | return S_OK;
|
---|
65 | }
|
---|
66 |
|
---|
67 | void CloudProviderManager::uninit()
|
---|
68 | {
|
---|
69 | // Enclose the state transition Ready->InUninit->NotReady.
|
---|
70 | AutoUninitSpan autoUninitSpan(this);
|
---|
71 | if (autoUninitSpan.uninitDone())
|
---|
72 | return;
|
---|
73 | }
|
---|
74 |
|
---|
75 | #ifdef VBOX_WITH_EXTPACK
|
---|
76 |
|
---|
77 | bool CloudProviderManager::i_canRemoveExtPack(IExtPack *aExtPack)
|
---|
78 | {
|
---|
79 | AssertReturn(aExtPack, false);
|
---|
80 |
|
---|
81 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
82 |
|
---|
83 | // If any cloud provider in this extension pack fails to prepare the
|
---|
84 | // uninstall it and the cloud provider will be kept, so that the user
|
---|
85 | // can retry safely later. All other cloud providers in this extpack
|
---|
86 | // will be done as usual. No attempt is made to bring back the other
|
---|
87 | // cloud providers into working shape.
|
---|
88 |
|
---|
89 | bool fRes = true;
|
---|
90 | Bstr bstrName;
|
---|
91 | aExtPack->COMGETTER(Name)(bstrName.asOutParam());
|
---|
92 | Utf8Str strName(bstrName);
|
---|
93 | ExtPackNameCloudProviderManagerMap::iterator it = m_mapCloudProviderManagers.find(strName);
|
---|
94 | if (it != m_mapCloudProviderManagers.end())
|
---|
95 | {
|
---|
96 | ComPtr<ICloudProviderManager> pTmp(it->second);
|
---|
97 |
|
---|
98 | Assert(m_astrExtPackNames.size() == m_apCloudProviders.size());
|
---|
99 | for (size_t i = 0; i < m_astrExtPackNames.size(); )
|
---|
100 | {
|
---|
101 | if (m_astrExtPackNames[i] != strName)
|
---|
102 | {
|
---|
103 | i++;
|
---|
104 | continue;
|
---|
105 | }
|
---|
106 |
|
---|
107 | // pTmpProvider will point to an object with refcount > 0 until
|
---|
108 | // the ComPtr is removed from m_apCloudProviders.
|
---|
109 | HRESULT hrc = S_OK;
|
---|
110 | ULONG uRefCnt = 1;
|
---|
111 | ICloudProvider *pTmpProvider(m_apCloudProviders[i]);
|
---|
112 | if (pTmpProvider)
|
---|
113 | {
|
---|
114 | hrc = pTmpProvider->PrepareUninstall();
|
---|
115 | // Sanity check the refcount, it should be 1 at this point.
|
---|
116 | pTmpProvider->AddRef();
|
---|
117 | uRefCnt = pTmpProvider->Release();
|
---|
118 | Assert(uRefCnt == 1);
|
---|
119 | }
|
---|
120 | if (SUCCEEDED(hrc) && uRefCnt == 1)
|
---|
121 | {
|
---|
122 | m_astrExtPackNames.erase(m_astrExtPackNames.begin() + (ssize_t)i);
|
---|
123 | m_apCloudProviders.erase(m_apCloudProviders.begin() + (ssize_t)i);
|
---|
124 | }
|
---|
125 | else
|
---|
126 | {
|
---|
127 | LogRel(("CloudProviderManager: provider '%s' blocks extpack uninstall, result=%Rhrc, refcount=%u\n", strName.c_str(), hrc, uRefCnt));
|
---|
128 | fRes = false;
|
---|
129 | i++;
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | if (fRes)
|
---|
134 | m_mapCloudProviderManagers.erase(it);
|
---|
135 | }
|
---|
136 |
|
---|
137 | return fRes;
|
---|
138 | }
|
---|
139 |
|
---|
140 | void CloudProviderManager::i_addExtPack(IExtPack *aExtPack)
|
---|
141 | {
|
---|
142 | AssertReturnVoid(aExtPack);
|
---|
143 |
|
---|
144 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
145 |
|
---|
146 | Bstr bstrName;
|
---|
147 | aExtPack->COMGETTER(Name)(bstrName.asOutParam());
|
---|
148 | Utf8Str strName(bstrName);
|
---|
149 | ComPtr<IUnknown> pObj;
|
---|
150 | std::vector<com::Utf8Str> astrExtPackNames;
|
---|
151 | com::Guid idObj(COM_IIDOF(ICloudProviderManager));
|
---|
152 | HRESULT hrc = aExtPack->QueryObject(Bstr(idObj.toString()).raw(), pObj.asOutParam());
|
---|
153 | if (FAILED(hrc))
|
---|
154 | return;
|
---|
155 |
|
---|
156 | ComPtr<ICloudProviderManager> pTmp(pObj);
|
---|
157 | if (pTmp.isNull())
|
---|
158 | return;
|
---|
159 |
|
---|
160 | SafeIfaceArray<ICloudProvider> apProvidersFromCurrExtPack;
|
---|
161 | hrc = pTmp->COMGETTER(Providers)(ComSafeArrayAsOutParam(apProvidersFromCurrExtPack));
|
---|
162 | if (FAILED(hrc))
|
---|
163 | return;
|
---|
164 |
|
---|
165 | m_mapCloudProviderManagers[strName] = pTmp;
|
---|
166 | for (unsigned i = 0; i < apProvidersFromCurrExtPack.size(); i++)
|
---|
167 | {
|
---|
168 | // Sanity check each cloud provider by forcing a QueryInterface call,
|
---|
169 | // making sure that it implements the right interface.
|
---|
170 | ComPtr<ICloudProvider> pTmpCP1(apProvidersFromCurrExtPack[i]);
|
---|
171 | if (!pTmpCP1.isNull())
|
---|
172 | {
|
---|
173 | ComPtr<ICloudProvider> pTmpCP2;
|
---|
174 | pTmpCP1.queryInterfaceTo(pTmpCP2.asOutParam());
|
---|
175 | if (!pTmpCP2.isNull())
|
---|
176 | {
|
---|
177 | Assert(m_astrExtPackNames.size() == m_apCloudProviders.size());
|
---|
178 | m_astrExtPackNames.push_back(strName);
|
---|
179 | m_apCloudProviders.push_back(apProvidersFromCurrExtPack[i]);
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | #endif /* VBOX_WITH_EXTPACK */
|
---|
186 |
|
---|
187 | HRESULT CloudProviderManager::getProviders(std::vector<ComPtr<ICloudProvider> > &aProviders)
|
---|
188 | {
|
---|
189 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
190 | aProviders = m_apCloudProviders;
|
---|
191 | return S_OK;
|
---|
192 | }
|
---|
193 |
|
---|
194 | HRESULT CloudProviderManager::getProviderById(const com::Guid &aProviderId,
|
---|
195 | ComPtr<ICloudProvider> &aProvider)
|
---|
196 | {
|
---|
197 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
198 | for (size_t i = 0; i < m_apCloudProviders.size(); i++)
|
---|
199 | {
|
---|
200 | Bstr bstrId;
|
---|
201 | HRESULT hrc = m_apCloudProviders[i]->COMGETTER(Id)(bstrId.asOutParam());
|
---|
202 | if (SUCCEEDED(hrc) && aProviderId == bstrId)
|
---|
203 | {
|
---|
204 | aProvider = m_apCloudProviders[i];
|
---|
205 | return S_OK;
|
---|
206 | }
|
---|
207 | }
|
---|
208 | return setError(VBOX_E_OBJECT_NOT_FOUND, tr("Could not find a cloud provider with UUID {%RTuuid}"),
|
---|
209 | aProviderId.raw());
|
---|
210 | }
|
---|
211 |
|
---|
212 | HRESULT CloudProviderManager::getProviderByShortName(const com::Utf8Str &aProviderName,
|
---|
213 | ComPtr<ICloudProvider> &aProvider)
|
---|
214 | {
|
---|
215 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
216 | for (size_t i = 0; i < m_apCloudProviders.size(); i++)
|
---|
217 | {
|
---|
218 | Bstr bstrName;
|
---|
219 | HRESULT hrc = m_apCloudProviders[i]->COMGETTER(ShortName)(bstrName.asOutParam());
|
---|
220 | if (SUCCEEDED(hrc) && bstrName.equals(aProviderName))
|
---|
221 | {
|
---|
222 | aProvider = m_apCloudProviders[i];
|
---|
223 | return S_OK;
|
---|
224 | }
|
---|
225 | }
|
---|
226 | return setError(VBOX_E_OBJECT_NOT_FOUND, tr("Could not find a cloud provider with short name '%s'"),
|
---|
227 | aProviderName.c_str());
|
---|
228 | }
|
---|
229 |
|
---|
230 | HRESULT CloudProviderManager::getProviderByName(const com::Utf8Str &aProviderName,
|
---|
231 | ComPtr<ICloudProvider> &aProvider)
|
---|
232 | {
|
---|
233 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
234 | for (size_t i = 0; i < m_apCloudProviders.size(); i++)
|
---|
235 | {
|
---|
236 | Bstr bstrName;
|
---|
237 | HRESULT hrc = m_apCloudProviders[i]->COMGETTER(Name)(bstrName.asOutParam());
|
---|
238 | if (SUCCEEDED(hrc) && bstrName.equals(aProviderName))
|
---|
239 | {
|
---|
240 | aProvider = m_apCloudProviders[i];
|
---|
241 | return S_OK;
|
---|
242 | }
|
---|
243 | }
|
---|
244 | return setError(VBOX_E_OBJECT_NOT_FOUND, tr("Could not find a cloud provider with name '%s'"),
|
---|
245 | aProviderName.c_str());
|
---|
246 | }
|
---|
247 |
|
---|