summaryrefslogtreecommitdiffstats
path: root/src/VBox/Main/src-client/AdditionsFacilityImpl.cpp
blob: 59ef98b5728c46794180759cf24587df3b54de7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* $Id: AdditionsFacilityImpl.cpp $ */
/** @file
 * VirtualBox Main - Additions facility class.
 */

/*
 * Copyright (C) 2014-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#define LOG_GROUP LOG_GROUP_MAIN_ADDITIONSFACILITY
#include "LoggingNew.h"

#include "AdditionsFacilityImpl.h"
#include "Global.h"

#include "AutoCaller.h"


/**
 * @note We ASSUME that unknown is the first entry!
 */
/* static */
const AdditionsFacility::FacilityInfo AdditionsFacility::s_aFacilityInfo[8] =
{
    { "Unknown",                        AdditionsFacilityType_None,             AdditionsFacilityClass_None },
    { "VirtualBox Base Driver",         AdditionsFacilityType_VBoxGuestDriver,  AdditionsFacilityClass_Driver },
    { "Auto Logon",                     AdditionsFacilityType_AutoLogon,        AdditionsFacilityClass_Feature },
    { "VirtualBox System Service",      AdditionsFacilityType_VBoxService,      AdditionsFacilityClass_Service },
    { "VirtualBox Desktop Integration", AdditionsFacilityType_VBoxTrayClient,   AdditionsFacilityClass_Program },
    { "Seamless Mode",                  AdditionsFacilityType_Seamless,         AdditionsFacilityClass_Feature },
    { "Graphics Mode",                  AdditionsFacilityType_Graphics,         AdditionsFacilityClass_Feature },
    { "Guest Monitor Attach",           AdditionsFacilityType_MonitorAttach,    AdditionsFacilityClass_Feature },
};

// constructor / destructor
/////////////////////////////////////////////////////////////////////////////

DEFINE_EMPTY_CTOR_DTOR(AdditionsFacility)

HRESULT AdditionsFacility::FinalConstruct()
{
    LogFlowThisFunc(("\n"));
    return BaseFinalConstruct();
}

void AdditionsFacility::FinalRelease()
{
    LogFlowThisFuncEnter();
    uninit();
    BaseFinalRelease();
    LogFlowThisFuncLeave();
}

// public initializer/uninitializer for internal purposes only
/////////////////////////////////////////////////////////////////////////////

HRESULT AdditionsFacility::init(Guest *a_pParent, AdditionsFacilityType_T a_enmFacility, AdditionsFacilityStatus_T a_enmStatus,
                                uint32_t a_fFlags, PCRTTIMESPEC a_pTimeSpecTS)
{
    RT_NOREF(a_pParent); /** @todo r=bird: For locking perhaps? */
    LogFlowThisFunc(("a_pParent=%p\n", a_pParent));

    /* Enclose the state transition NotReady->InInit->Ready. */
    AutoInitSpan autoInitSpan(this);
    AssertReturn(autoInitSpan.isOk(), E_FAIL);

    /* Initialize the data: */
    mData.mType         = a_enmFacility;
    mData.mStatus       = a_enmStatus;
    mData.mTimestamp    = *a_pTimeSpecTS;
    mData.mfFlags       = a_fFlags;
    mData.midxInfo      = 0;
    for (size_t i = 0; i < RT_ELEMENTS(s_aFacilityInfo); ++i)
        if (s_aFacilityInfo[i].mType == a_enmFacility)
        {
            mData.midxInfo = i;
            break;
        }

    /* Confirm a successful initialization when it's the case. */
    autoInitSpan.setSucceeded();

    return S_OK;
}

/**
 * Uninitializes the instance.
 *
 * Called from FinalRelease().
 */
void AdditionsFacility::uninit()
{
    LogFlowThisFunc(("\n"));

    /* Enclose the state transition Ready->InUninit->NotReady. */
    AutoUninitSpan autoUninitSpan(this);
    if (autoUninitSpan.uninitDone())
        return;
}

HRESULT AdditionsFacility::getClassType(AdditionsFacilityClass_T *aClassType)
{
    LogFlowThisFuncEnter();

    /* midxInfo is static, so no need to lock anything. */
    size_t idxInfo = mData.midxInfo;
    AssertStmt(idxInfo < RT_ELEMENTS(s_aFacilityInfo), idxInfo = 0);
    *aClassType = s_aFacilityInfo[idxInfo].mClass;
    return S_OK;
}

HRESULT AdditionsFacility::getName(com::Utf8Str &aName)
{
    LogFlowThisFuncEnter();

    /* midxInfo is static, so no need to lock anything. */
    size_t idxInfo = mData.midxInfo;
    AssertStmt(idxInfo < RT_ELEMENTS(s_aFacilityInfo), idxInfo = 0);
    int vrc = aName.assignNoThrow(s_aFacilityInfo[idxInfo].mName);
    return RT_SUCCESS(vrc) ? S_OK : E_OUTOFMEMORY;
}

HRESULT AdditionsFacility::getLastUpdated(LONG64 *aLastUpdated)
{
    LogFlowThisFuncEnter();

    /** @todo r=bird: Should take parent (Guest) lock here, see i_update(). */
    AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
    *aLastUpdated = RTTimeSpecGetMilli(&mData.mTimestamp);
    return S_OK;
}

HRESULT AdditionsFacility::getStatus(AdditionsFacilityStatus_T *aStatus)
{
    LogFlowThisFuncEnter();

    /** @todo r=bird: Should take parent (Guest) lock here, see i_update(). */
    AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
    *aStatus = mData.mStatus;
    return S_OK;
}

HRESULT AdditionsFacility::getType(AdditionsFacilityType_T *aType)
{
    LogFlowThisFuncEnter();

    /* mType is static, so no need to lock anything. */
    *aType = mData.mType;
    return S_OK;
}

#if 0 /* unused */

AdditionsFacilityType_T AdditionsFacility::i_getType() const
{
    return mData.mType;
}

AdditionsFacilityClass_T AdditionsFacility::i_getClass() const
{
    size_t idxInfo = mData.midxInfo;
    AssertStmt(idxInfo < RT_ELEMENTS(s_aFacilityInfo), idxInfo = 0);
    return s_aFacilityInfo[idxInfo].mClass;
}

const char *AdditionsFacility::i_getName() const
{
    size_t idxInfo = mData.midxInfo;
    AssertStmt(idxInfo < RT_ELEMENTS(s_aFacilityInfo), idxInfo = 0);
    return s_aFacilityInfo[idxInfo].mName;
}

#endif /* unused */

/**
 * @note Caller should read lock the Guest object.
 */
LONG64 AdditionsFacility::i_getLastUpdated() const
{
    return RTTimeSpecGetMilli(&mData.mTimestamp);
}

/**
 * @note Caller should read lock the Guest object.
 */
AdditionsFacilityStatus_T AdditionsFacility::i_getStatus() const
{
    return mData.mStatus;
}

/**
 * Method used by IGuest::facilityUpdate to make updates.
 *
 * @returns change indicator.
 *
 * @todo    r=bird: Locking here isn't quite sane.  While updating is serialized
 *          by the caller holding down the Guest object lock, this code doesn't
 *          serialize with this object.  So, the read locking done in the getter
 *          methods is utterly pointless.  OTOH, the getter methods only get
 *          single values, so there isn't really much to be worried about here,
 *          especially with 32-bit hosts no longer being supported.
 */
bool AdditionsFacility::i_update(AdditionsFacilityStatus_T a_enmStatus, uint32_t a_fFlags, PCRTTIMESPEC a_pTimeSpecTS)
{
    bool const fChanged = mData.mStatus != a_enmStatus;

    mData.mTimestamp = *a_pTimeSpecTS;
    mData.mStatus    = a_enmStatus;
    mData.mfFlags    = a_fFlags;

    return fChanged;
}