1 /*
2 * Copyright (c) 2023 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 "account_subscriber.h"
17
18 #include "account_task_manager.h"
19 #include "common_defs.h"
20 #include "common_event_support.h"
21 #include "device_auth.h"
22 #include "hc_log.h"
23 #include "json_utils.h"
24 #include "want.h"
25 #include "critical_handler.h"
26 #include "unload_handler.h"
27
28 namespace OHOS {
29 namespace DevAuth {
AccountSubscriber(const EventFwk::CommonEventSubscribeInfo & subscriberInfo,const OsAccountEventNotifier & notifier)30 AccountSubscriber::AccountSubscriber(const EventFwk::CommonEventSubscribeInfo &subscriberInfo,
31 const OsAccountEventNotifier ¬ifier)
32 : EventFwk::CommonEventSubscriber(subscriberInfo), notifier_(notifier)
33 {}
34
OnReceiveEvent(const EventFwk::CommonEventData & eventData)35 void AccountSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
36 {
37 IncreaseCriticalCnt(ADD_ONE);
38 ResponseCommonEvent(eventData);
39 DecreaseCriticalCnt();
40 }
41
ResponseCommonEvent(const EventFwk::CommonEventData & eventData)42 void AccountSubscriber::ResponseCommonEvent(const EventFwk::CommonEventData &eventData)
43 {
44 DelayUnload();
45 const OHOS::AAFwk::Want& want = eventData.GetWant();
46 std::string action = want.GetAction();
47 LOGI("[AccountSubscriber]: OnReceiveEvent action: %" LOG_PUB "s.", action.c_str());
48 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) {
49 LOGI("[AccountSubscriber]: user unlocked, userId: %" LOG_PUB "d.", eventData.GetCode());
50 notifier_.notifyOsAccountUnlocked(eventData.GetCode());
51 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
52 LOGI("[AccountSubscriber]: user removed, userId: %" LOG_PUB "d.", eventData.GetCode());
53 notifier_.notifyOsAccountRemoved(eventData.GetCode());
54 } else {
55 LOGI("[AccountSubscriber]: receive other event.");
56 }
57 CJson *cmdParamJson = CreateJson();
58 if (cmdParamJson == nullptr) {
59 LOGE("[AccountSubscriber]: Failed to create cmd params json!");
60 return;
61 }
62 if (AddStringToJson(cmdParamJson, FIELD_COMMON_EVENT_NAME, action.c_str()) != HC_SUCCESS) {
63 LOGE("[AccountSubscriber]: Failed to add common event name to json!");
64 FreeJson(cmdParamJson);
65 return;
66 }
67 if (AddIntToJson(cmdParamJson, FIELD_COMMON_EVENT_CODE, eventData.GetCode()) != HC_SUCCESS) {
68 LOGE("[AccountSubscriber]: Failed to add common event code to json!");
69 FreeJson(cmdParamJson);
70 return;
71 }
72 int32_t res = ExecuteAccountAuthCmd(DEFAULT_OS_ACCOUNT, HANDLE_COMMON_EVENT, cmdParamJson, nullptr);
73 FreeJson(cmdParamJson);
74 LOGI("[AccountSubscriber]: handle common event res: %" LOG_PUB "d", res);
75 }
76
77 } // namespace DevAuth
78 } // namespace OHOS
79