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
26 namespace OHOS {
27 namespace DevAuth {
AccountSubscriber(const EventFwk::CommonEventSubscribeInfo & subscriberInfo,const OsAccountEventNotifier & notifier)28 AccountSubscriber::AccountSubscriber(const EventFwk::CommonEventSubscribeInfo &subscriberInfo,
29 const OsAccountEventNotifier ¬ifier)
30 : EventFwk::CommonEventSubscriber(subscriberInfo), notifier_(notifier)
31 {}
32
OnReceiveEvent(const EventFwk::CommonEventData & eventData)33 void AccountSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
34 {
35 const OHOS::AAFwk::Want& want = eventData.GetWant();
36 std::string action = want.GetAction();
37 LOGI("[AccountSubscriber]: OnReceiveEvent action: %" LOG_PUB "s.", action.c_str());
38
39 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) {
40 LOGI("[AccountSubscriber]: user unlocked, userId: %" LOG_PUB "d.", eventData.GetCode());
41 notifier_.notifyOsAccountUnlocked(eventData.GetCode());
42 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
43 LOGI("[AccountSubscriber]: user removed, userId: %" LOG_PUB "d.", eventData.GetCode());
44 notifier_.notifyOsAccountRemoved(eventData.GetCode());
45 } else {
46 LOGI("[AccountSubscriber]: receive other event.");
47 }
48 CJson *cmdParamJson = CreateJson();
49 if (cmdParamJson == nullptr) {
50 LOGE("[AccountSubscriber]: Failed to create cmd params json!");
51 return;
52 }
53 if (AddStringToJson(cmdParamJson, FIELD_COMMON_EVENT_NAME, action.c_str()) != HC_SUCCESS) {
54 LOGE("[AccountSubscriber]: Failed to add common event name to json!");
55 FreeJson(cmdParamJson);
56 return;
57 }
58 if (AddIntToJson(cmdParamJson, FIELD_COMMON_EVENT_CODE, eventData.GetCode()) != HC_SUCCESS) {
59 LOGE("[AccountSubscriber]: Failed to add common event code to json!");
60 FreeJson(cmdParamJson);
61 return;
62 }
63 int32_t res = ExecuteAccountAuthCmd(DEFAULT_OS_ACCOUNT, HANDLE_COMMON_EVENT, cmdParamJson, nullptr);
64 FreeJson(cmdParamJson);
65 LOGI("[AccountSubscriber]: handle common event res: %" LOG_PUB "d", res);
66 }
67 } // namespace DevAuth
68 } // namespace OHOS
69