• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 "cache_common_event_handler.h"
17 #include "iservice_registry.h"
18 #include "system_ability_definition.h"
19 #include "matching_skills.h"
20 #include "common_event_manager.h"
21 #include "common_event_support.h"
22 #include "hc_log.h"
23 #include <vector>
24 #include "system_ability_ondemand_reason.h"
25 #include "os_account_adapter.h"
26 #include "account_task_manager.h"
27 #include "json_utils.h"
28 #include "common_defs.h"
29 
30 static const std::string COMMON_EVENT_ACTION_NAME = "common_event_action_name";
31 
HandleCacheCommonEventInner(const char * eventName,int32_t eventCode)32 static void HandleCacheCommonEventInner(const char *eventName, int32_t eventCode)
33 {
34     CJson *cmdParamJson = CreateJson();
35     if (cmdParamJson == nullptr) {
36         LOGE("[CacheCommonEvent]: Failed to create cmd params json.");
37         return;
38     }
39     if (AddStringToJson(cmdParamJson, FIELD_COMMON_EVENT_NAME, eventName)
40         != HC_SUCCESS) {
41         LOGE("[CacheCommonEvent]: Failed to add common event name to json.");
42         FreeJson(cmdParamJson);
43         return;
44     }
45     if (AddIntToJson(cmdParamJson, FIELD_COMMON_EVENT_CODE, eventCode) != HC_SUCCESS) {
46         LOGE("[CacheCommonEvent]: Failed to add common event code to json.");
47         FreeJson(cmdParamJson);
48         return;
49     }
50     LOGI("[CacheCommonEvent]: start handle common event.");
51     int32_t res = ExecuteAccountAuthCmd(DEFAULT_OS_ACCOUNT, HANDLE_COMMON_EVENT, cmdParamJson, nullptr);
52     FreeJson(cmdParamJson);
53     LOGI("[CacheCommonEvent]: handle common event res: %" LOG_PUB "d.", res);
54 }
55 
HandleCacheCommonEvent(void)56 void HandleCacheCommonEvent(void)
57 {
58     auto saMgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
59     if (saMgr == nullptr) {
60         LOGE("[CacheCommonEvent]: system ability manager is null!");
61         return;
62     }
63     std::vector<int64_t> extraDataIdList;
64     int32_t ret = saMgr->GetCommonEventExtraDataIdlist(OHOS::DEVICE_AUTH_SERVICE_ID, extraDataIdList);
65     if (ret != OHOS::ERR_OK) {
66         LOGE("GetCommonEventExtraDataIdlist failed ret is %" LOG_PUB "d.", ret);
67         return;
68     }
69     for (auto &item : extraDataIdList) {
70         OHOS::MessageParcel extraDataParcel;
71         ret = saMgr->GetOnDemandReasonExtraData(item, extraDataParcel);
72         if (ret != OHOS::ERR_OK) {
73             LOGE("get extra data failed.");
74             continue;
75         }
76         auto extraData = extraDataParcel.ReadParcelable<OHOS::OnDemandReasonExtraData>();
77         if (extraData == nullptr) {
78             LOGE("get extra data read parcel failed.");
79             continue;
80         }
81         LOGI("code: %" LOG_PUB "d, data: %" LOG_PUB "s", extraData->GetCode(), extraData->GetData().c_str());
82         auto want = extraData->GetWant();
83         auto it = want.find(COMMON_EVENT_ACTION_NAME);
84         if (it == want.end()) {
85             LOGW("common event not found.");
86             delete extraData;
87             continue;
88         }
89         LOGI("common event name: %" LOG_PUB "s.", it->second.c_str());
90         if (it->second == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) {
91             LOGI("[CacheCommonEvent]: user unlocked, userId %" LOG_PUB "d.", extraData->GetCode());
92             NotifyOsAccountUnlocked(extraData->GetCode());
93         } else if (it->second == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
94             LOGI("[CacheCommonEvent]: user removed, userId %" LOG_PUB "d.", extraData->GetCode());
95             NotifyOsAccountRemoved(extraData->GetCode());
96         } else {
97             LOGI("[CacheCommonEvent]: receive other event.");
98         }
99         HandleCacheCommonEventInner(it->second.c_str(), extraData->GetCode());
100         delete extraData;
101     }
102     return;
103 }