• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "os_account_adapter.h"
17 
18 #include <vector>
19 #include "account_subscriber.h"
20 #include "common_defs.h"
21 #include "common_event_manager.h"
22 #include "common_event_support.h"
23 #include "device_auth.h"
24 #include "hc_log.h"
25 #include "iservice_registry.h"
26 #include "matching_skills.h"
27 #include "net_observer.h"
28 #include "os_account_info.h"
29 #include "os_account_manager.h"
30 #include "sa_subscriber.h"
31 #include "system_ability_definition.h"
32 
33 typedef struct {
34     EventCallbackId callbackId;
35     OsAccountCallbackFunc onOsAccountUnlocked;
36     OsAccountCallbackFunc onOsAccountRemoved;
37 } OsAccountEventCallback;
38 DECLARE_HC_VECTOR(EventCallbackVec, OsAccountEventCallback)
39 IMPLEMENT_HC_VECTOR(EventCallbackVec, OsAccountEventCallback, 1)
40 
41 static std::shared_ptr<OHOS::DevAuth::AccountSubscriber> g_accountSubscriber = nullptr;
42 static OHOS::sptr<OHOS::DevAuth::SaSubscriber> g_eventSaSubscriber = nullptr;
43 static OHOS::sptr<OHOS::DevAuth::SaSubscriber> g_netSaSubscriber = nullptr;
44 static EventCallbackVec g_callbackVec;
45 static bool g_isInitialized = false;
46 static bool g_isCommonEventSubscribed = false;
47 static bool g_isNetObserverSubscribed = false;
48 static bool g_isEventSaSubscribed = false;
49 static bool g_isNetSaSubscribed = false;
50 static const int32_t SYSTEM_DEFAULT_USER = 100;
51 static OHOS::DevAuth::OsAccountEventNotifier g_accountEventNotifier;
52 static OHOS::DevAuth::SaEventNotifier g_saEventNotifier;
53 static OHOS::sptr<NetObserver> g_observer = nullptr;
54 
NotifyOsAccountUnlocked(int32_t osAccountId)55 void NotifyOsAccountUnlocked(int32_t osAccountId)
56 {
57     if (!g_isInitialized) {
58         return;
59     }
60     uint32_t index;
61     OsAccountEventCallback *callback;
62     FOR_EACH_HC_VECTOR(g_callbackVec, index, callback) {
63         callback->onOsAccountUnlocked(osAccountId);
64     }
65 }
66 
NotifyOsAccountRemoved(int32_t osAccountId)67 void NotifyOsAccountRemoved(int32_t osAccountId)
68 {
69     if (!g_isInitialized) {
70         return;
71     }
72     uint32_t index;
73     OsAccountEventCallback *callback;
74     FOR_EACH_HC_VECTOR(g_callbackVec, index, callback) {
75         callback->onOsAccountRemoved(osAccountId);
76     }
77 }
78 
SubscribeCommonEvent(void)79 static void SubscribeCommonEvent(void)
80 {
81     if (g_isCommonEventSubscribed) {
82         return;
83     }
84     if (g_accountSubscriber == nullptr) {
85         g_accountEventNotifier.notifyOsAccountUnlocked = NotifyOsAccountUnlocked;
86         g_accountEventNotifier.notifyOsAccountRemoved = NotifyOsAccountRemoved;
87         OHOS::EventFwk::MatchingSkills matchingSkills;
88         matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED);
89         matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
90         matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_DISTRIBUTED_ACCOUNT_LOGIN);
91         matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_DISTRIBUTED_ACCOUNT_LOGOUT);
92         matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_BLUETOOTH_HOST_STATE_UPDATE);
93         matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
94         OHOS::EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
95         g_accountSubscriber = std::make_shared<OHOS::DevAuth::AccountSubscriber>(subscribeInfo, g_accountEventNotifier);
96     }
97     if (OHOS::EventFwk::CommonEventManager::SubscribeCommonEvent(g_accountSubscriber)) {
98         LOGI("[OsAccountAdapter]: subscribe common event succeed!");
99         g_isCommonEventSubscribed = true;
100     } else {
101         LOGE("[OsAccountAdapter]: subscribe common event failed!");
102     }
103 }
104 
UnSubscribeCommonEvent(void)105 static void UnSubscribeCommonEvent(void)
106 {
107     if (!g_isCommonEventSubscribed) {
108         return;
109     }
110     if (OHOS::EventFwk::CommonEventManager::UnSubscribeCommonEvent(g_accountSubscriber)) {
111         g_isCommonEventSubscribed = false;
112         LOGI("[OsAccountAdapter]: unsubscribe common event succeed!");
113     } else {
114         LOGE("[OsAccountAdapter]: unsubscribe common event failed!");
115     }
116 }
117 
UnSubscribeSystemAbility(void)118 static void UnSubscribeSystemAbility(void)
119 {
120     OHOS::sptr<OHOS::ISystemAbilityManager> sysMgr =
121         OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
122     if (sysMgr == nullptr) {
123         LOGE("[OsAccountAdapter]: system ability manager is null!");
124         return;
125     }
126     if (g_isEventSaSubscribed) {
127         if (sysMgr->UnSubscribeSystemAbility(OHOS::COMMON_EVENT_SERVICE_ID, g_eventSaSubscriber) == OHOS::ERR_OK) {
128             LOGI("[OsAccountAdapter]: unsubscribe common event sa succeed!");
129             g_isEventSaSubscribed = false;
130         } else {
131             LOGE("[OsAccountAdapter]: unsubscribe common event sa failed!");
132         }
133     }
134     if (g_isNetSaSubscribed) {
135         if (sysMgr->UnSubscribeSystemAbility(OHOS::COMM_NET_CONN_MANAGER_SYS_ABILITY_ID, g_netSaSubscriber) ==
136             OHOS::ERR_OK) {
137             LOGI("[OsAccountAdapter]: unsubscribe net connection manager sa succeed!");
138             g_isNetSaSubscribed = false;
139         } else {
140             LOGE("[OsAccountAdapter]: unsubscribe net connection manager sa failed!");
141         }
142     }
143 }
144 
StartNetObserver(void)145 static void StartNetObserver(void)
146 {
147     if (g_isNetObserverSubscribed) {
148         return;
149     }
150     g_observer = new(std::nothrow) NetObserver();
151     if (g_observer == nullptr) {
152         LOGE("[OsAccountAdapter]: failed to create net observer!");
153         return;
154     }
155     g_observer->StartObserver();
156     g_isNetObserverSubscribed = true;
157 }
158 
StopNetObserver(void)159 static void StopNetObserver(void)
160 {
161     if (!g_isNetObserverSubscribed) {
162         return;
163     }
164     g_observer->StopObserver();
165     g_observer = nullptr;
166     g_isNetObserverSubscribed = false;
167 }
168 
NotifySystemAbilityAdded(int32_t systemAbilityId)169 static void NotifySystemAbilityAdded(int32_t systemAbilityId)
170 {
171     if (systemAbilityId == OHOS::COMMON_EVENT_SERVICE_ID) {
172         LOGI("[OsAccountAdapter]: common event sa added, try to subscribe common event.");
173         SubscribeCommonEvent();
174     } else if (systemAbilityId == OHOS::COMM_NET_CONN_MANAGER_SYS_ABILITY_ID) {
175         LOGI("[OsAccountAdapter]: net connection manager sa added, try to subscribe net observer.");
176         StartNetObserver();
177     } else {
178         LOGE("[OsAccountAdapter]: invalid system ability!");
179     }
180 }
181 
SubscribeSystemAbility(void)182 static void SubscribeSystemAbility(void)
183 {
184     OHOS::sptr<OHOS::ISystemAbilityManager> sysMgr =
185         OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
186     if (sysMgr == nullptr) {
187         LOGE("[OsAccountAdapter]: system ability manager is null!");
188         return;
189     }
190     g_saEventNotifier.notifySystemAbilityAdded = NotifySystemAbilityAdded;
191     if (!g_isEventSaSubscribed) {
192         if (g_eventSaSubscriber == nullptr) {
193             g_eventSaSubscriber = new OHOS::DevAuth::SaSubscriber(g_saEventNotifier);
194         }
195         if (sysMgr->SubscribeSystemAbility(OHOS::COMMON_EVENT_SERVICE_ID, g_eventSaSubscriber) == OHOS::ERR_OK) {
196             LOGI("[OsAccountAdapter]: subscribe common event sa succeed!");
197             g_isEventSaSubscribed = true;
198         } else {
199             LOGE("[OsAccountAdapter]: subscribe common event sa failed!");
200         }
201     }
202     if (!g_isNetSaSubscribed) {
203         if (g_netSaSubscriber == nullptr) {
204             g_netSaSubscriber = new OHOS::DevAuth::SaSubscriber(g_saEventNotifier);
205         }
206         if (sysMgr->SubscribeSystemAbility(OHOS::COMM_NET_CONN_MANAGER_SYS_ABILITY_ID, g_netSaSubscriber) ==
207             OHOS::ERR_OK) {
208             LOGI("[OsAccountAdapter]: subscribe net connection manager sa succeed!");
209             g_isNetSaSubscribed = true;
210         } else {
211             LOGE("[OsAccountAdapter]: subscribe net connection manager sa failed!");
212         }
213     }
214 }
215 
IsCallbackExist(EventCallbackId callbackId)216 static bool IsCallbackExist(EventCallbackId callbackId)
217 {
218     uint32_t index;
219     OsAccountEventCallback *callback;
220     FOR_EACH_HC_VECTOR(g_callbackVec, index, callback) {
221         if (callback->callbackId == callbackId) {
222             return true;
223         }
224     }
225     return false;
226 }
227 
GetCurrentActiveOsAccountId(void)228 int32_t GetCurrentActiveOsAccountId(void)
229 {
230     std::vector<int> activatedOsAccountIds;
231     OHOS::ErrCode res = OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(activatedOsAccountIds);
232     if ((res != OHOS::ERR_OK) || (activatedOsAccountIds.size() <= 0)) {
233         LOGE("[OsAccountNativeFwk][QueryActiveOsAccountIds]: fail. [Res]: %" LOG_PUB "d", res);
234         return INVALID_OS_ACCOUNT;
235     }
236     int osAccountId = activatedOsAccountIds[0];
237     if (osAccountId != SYSTEM_DEFAULT_USER) {
238         LOGI("[OsAccountNativeFwk][QueryActiveOsAccountIds]: Current active os accountId: %" LOG_PUB "d", osAccountId);
239     }
240     return osAccountId;
241 }
242 
InitOsAccountAdapter(void)243 void InitOsAccountAdapter(void)
244 {
245     if (g_isInitialized) {
246         return;
247     }
248     g_callbackVec = CREATE_HC_VECTOR(EventCallbackVec);
249     SubscribeSystemAbility();
250     g_isInitialized = true;
251 }
252 
DestroyOsAccountAdapter(void)253 void DestroyOsAccountAdapter(void)
254 {
255     if (!g_isInitialized) {
256         return;
257     }
258     g_isInitialized = false;
259     UnSubscribeSystemAbility();
260     UnSubscribeCommonEvent();
261     StopNetObserver();
262     DESTROY_HC_VECTOR(EventCallbackVec, &g_callbackVec);
263 }
264 
IsOsAccountUnlocked(int32_t osAccountId)265 bool IsOsAccountUnlocked(int32_t osAccountId)
266 {
267     bool isUnlocked = false;
268     OHOS::ErrCode res = OHOS::AccountSA::OsAccountManager::IsOsAccountVerified(osAccountId, isUnlocked);
269     if (res != OHOS::ERR_OK) {
270         LOGE("[OsAccountNativeFwk][IsOsAccountVerified]: Check account verify status failed, res: %" LOG_PUB
271             "d, accountId: %" LOG_PUB "d", res, osAccountId);
272         return false;
273     }
274     return isUnlocked;
275 }
276 
GetAllOsAccountIds(int32_t ** osAccountIds,uint32_t * size)277 int32_t GetAllOsAccountIds(int32_t **osAccountIds, uint32_t *size)
278 {
279     if (osAccountIds == nullptr || size == nullptr) {
280         LOGE("[OsAccountAdapter]: invalid input params!");
281         return HC_ERR_INVALID_PARAMS;
282     }
283     std::vector<OHOS::AccountSA::OsAccountInfo> osAccountInfos;
284     OHOS::ErrCode res = OHOS::AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos);
285     uint32_t accountSize = osAccountInfos.size();
286     if ((res != OHOS::ERR_OK) || (accountSize <= 0)) {
287         LOGE("[OsAccountNativeFwk][QueryAllCreatedOsAccounts]: failed. [Res]: %" LOG_PUB "d", res);
288         return HC_ERROR;
289     }
290     *osAccountIds = (int32_t *)HcMalloc(accountSize * sizeof(int32_t), 0);
291     if (*osAccountIds == nullptr) {
292         LOGE("[OsAccountAdapter]: Failed to alloc memory for osAccountIds!");
293         return HC_ERR_ALLOC_MEMORY;
294     }
295     for (uint32_t index = 0; index < accountSize; index++) {
296         (*osAccountIds)[index] = osAccountInfos[index].GetLocalId();
297     }
298     *size = accountSize;
299     return HC_SUCCESS;
300 }
301 
DevAuthGetRealOsAccountLocalId(int32_t inputId)302 int32_t DevAuthGetRealOsAccountLocalId(int32_t inputId)
303 {
304     if (inputId == ANY_OS_ACCOUNT) {
305         return GetCurrentActiveOsAccountId();
306     } else if (inputId >= SYSTEM_DEFAULT_USER) {
307         if (inputId != SYSTEM_DEFAULT_USER) {
308             LOGI("[OsAccountAdapter]: Use input os account! [Id]: %" LOG_PUB "d", inputId);
309         }
310         return inputId;
311     } else {
312         LOGE("[OsAccountAdapter]: The input os account is invalid! [Id]: %" LOG_PUB "d", inputId);
313         return INVALID_OS_ACCOUNT;
314     }
315 }
316 
CheckIsForegroundOsAccountId(int32_t inputOsAccountId)317 bool CheckIsForegroundOsAccountId(int32_t inputOsAccountId)
318 {
319     int32_t foregroundOsAccountId = GetCurrentActiveOsAccountId();
320     if (foregroundOsAccountId == INVALID_OS_ACCOUNT) {
321         LOGE("[OsAccountAdapter]: get foreground osAccountId fail!");
322         return false;
323     }
324     if (inputOsAccountId != foregroundOsAccountId) {
325         LOGE("[OsAccountAdapter]: input osAccountId is not same as foreground osAccountId!");
326         return false;
327     }
328     return true;
329 }
330 
AddOsAccountEventCallback(EventCallbackId callbackId,OsAccountCallbackFunc unlockFunc,OsAccountCallbackFunc removeFunc)331 void AddOsAccountEventCallback(EventCallbackId callbackId, OsAccountCallbackFunc unlockFunc,
332     OsAccountCallbackFunc removeFunc)
333 {
334     if (!g_isInitialized) {
335         LOGE("[OsAccountAdapter]: Not initialized!");
336         return;
337     }
338     if (unlockFunc == nullptr || removeFunc == nullptr) {
339         LOGE("[OsAccountAdapter]: Invalid input param!");
340         return;
341     }
342     if (IsCallbackExist(callbackId)) {
343         LOGE("[OsAccountAdapter]: Callback already exist!");
344         return;
345     }
346     OsAccountEventCallback eventCallback;
347     eventCallback.callbackId = callbackId;
348     eventCallback.onOsAccountUnlocked = unlockFunc;
349     eventCallback.onOsAccountRemoved = removeFunc;
350     if (g_callbackVec.pushBackT(&g_callbackVec, eventCallback) == nullptr) {
351         LOGE("[OsAccountAdapter]: Failed to add event callback!");
352     }
353 }
354 
RemoveOsAccountEventCallback(EventCallbackId callbackId)355 void RemoveOsAccountEventCallback(EventCallbackId callbackId)
356 {
357     if (!g_isInitialized) {
358         return;
359     }
360     uint32_t index;
361     OsAccountEventCallback *callback;
362     FOR_EACH_HC_VECTOR(g_callbackVec, index, callback) {
363         if (callback->callbackId == callbackId) {
364             OsAccountEventCallback deleteCallback;
365             HC_VECTOR_POPELEMENT(&g_callbackVec, &deleteCallback, index);
366             return;
367         }
368     }
369 }
370 
IsOsAccountSupported(void)371 bool IsOsAccountSupported(void)
372 {
373     return true;
374 }