1 /*
2 * Copyright (c) 2023-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 "ipc_skeleton.h"
17
18 #include "nap_process.h"
19 #include "input_event_handler.h"
20
21 namespace OHOS {
22 namespace MMI {
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "NapProcess" };
25 constexpr int32_t REMOVE_OBSERVER = -2;
26 constexpr int32_t NAP_EVENT = 0;
27 constexpr int32_t SUBSCRIBED = 1;
28 constexpr int32_t ACTIVE_EVENT = 2;
29 } // namespace
30
31 NapProcess *NapProcess::instance_ = new (std::nothrow) NapProcess();
GetInstance()32 NapProcess *NapProcess::GetInstance()
33 {
34 return instance_;
35 }
36
Init(UDSServer & udsServer)37 void NapProcess::Init(UDSServer& udsServer)
38 {
39 udsServer_ = &udsServer;
40 CHKPV(udsServer_);
41 }
42
NotifyBundleName(NapStatusData data,int32_t syncState)43 int32_t NapProcess::NotifyBundleName(NapStatusData data, int32_t syncState)
44 {
45 CALL_DEBUG_ENTER;
46 if (napClientPid_ < 0) {
47 MMI_HILOGE("Client pid is unavailable!");
48 return RET_ERR;
49 }
50 MMI_HILOGD("NotifyBundle info is : %{public}d, %{public}d, %{public}s, %{public}d",
51 data.pid, data.uid, data.bundleName.c_str(), syncState);
52 NetPacket pkt(MmiMessageId::NOTIFY_BUNDLE_NAME);
53 pkt << data.pid;
54 pkt << data.uid;
55 pkt << data.bundleName;
56 pkt << syncState;
57 CHKPR(udsServer_, RET_ERR);
58 int32_t fd = udsServer_->GetClientFd(napClientPid_);
59 auto udsServer = InputHandler->GetUDSServer();
60 CHKPR(udsServer, RET_ERR);
61 auto session = udsServer->GetSession(fd);
62 if (!udsServer->SendMsg(fd, pkt)) {
63 MMI_HILOGE("Sending structure of EventTouch failed! errCode:%{public}d", MSG_SEND_FAIL);
64 return RET_ERR;
65 }
66 return RET_OK;
67 }
68
IsNeedNotify(const NapStatusData & data)69 bool NapProcess::IsNeedNotify(const NapStatusData& data)
70 {
71 CALL_DEBUG_ENTER;
72 std::lock_guard guard(mapMtx_);
73 for (const auto& map : napMap_) {
74 if (data.pid == map.first.pid && data.uid == map.first.uid && data.bundleName == map.first.bundleName) {
75 bool IsNeedSendMessage = napMap_[data] == SUBSCRIBED || napMap_[data] == NAP_EVENT;
76 return IsNeedSendMessage;
77 }
78 }
79 return false;
80 }
81
SetNapStatus(int32_t pid,int32_t uid,std::string bundleName,int32_t napStatus)82 int32_t NapProcess::SetNapStatus(int32_t pid, int32_t uid, std::string bundleName, int32_t napStatus)
83 {
84 CALL_DEBUG_ENTER;
85 NapStatusData napData;
86 napData.pid = pid;
87 napData.uid = uid;
88 napData.bundleName = bundleName;
89 if (napStatus == ACTIVE_EVENT) {
90 RemoveMmiSubscribedEventData(napData);
91 MMI_HILOGD("Remove active event from napMap, pid = %{public}d, uid = %{public}d, bundleName = %{public}s",
92 pid, uid, bundleName.c_str());
93 }
94 if (napStatus == NAP_EVENT) {
95 AddMmiSubscribedEventData(napData, napStatus);
96 MMI_HILOGD("Add nap process to napMap, pid = %{public}d, uid = %{public}d, bundleName = %{public}s",
97 pid, uid, bundleName.c_str());
98 }
99 return RET_OK;
100 }
101
AddMmiSubscribedEventData(const NapStatusData & napData,int32_t syncState)102 int32_t NapProcess::AddMmiSubscribedEventData(const NapStatusData& napData, int32_t syncState)
103 {
104 CALL_DEBUG_ENTER;
105 std::lock_guard guard(mapMtx_);
106 if (napMap_.find(napData) == napMap_.end()) {
107 napMap_.emplace(napData, syncState);
108 } else {
109 napMap_[napData] = syncState;
110 }
111 return RET_OK;
112 }
113
RemoveMmiSubscribedEventData(const NapStatusData & napData)114 int32_t NapProcess::RemoveMmiSubscribedEventData(const NapStatusData& napData)
115 {
116 CALL_DEBUG_ENTER;
117 std::lock_guard guard(mapMtx_);
118 if (napMap_.find(napData) != napMap_.end()) {
119 napMap_.erase(napData);
120 }
121 return RET_OK;
122 }
123
GetNapClientPid()124 int32_t NapProcess::GetNapClientPid()
125 {
126 return napClientPid_;
127 }
128
NotifyNapOnline()129 int32_t NapProcess::NotifyNapOnline()
130 {
131 CALL_DEBUG_ENTER;
132 int32_t pid = IPCSkeleton::GetCallingPid();
133 napClientPid_ = pid;
134 MMI_HILOGD("NotifyNapOnline pid is %{public}d", pid);
135 return RET_OK;
136 }
137
RemoveInputEventObserver()138 int32_t NapProcess::RemoveInputEventObserver()
139 {
140 CALL_DEBUG_ENTER;
141 std::lock_guard guard(mapMtx_);
142 napMap_.clear();
143 napClientPid_ = REMOVE_OBSERVER;
144 return RET_OK;
145 }
146
GetAllMmiSubscribedEvents(std::map<std::tuple<int32_t,int32_t,std::string>,int32_t> & datas)147 int32_t NapProcess::GetAllMmiSubscribedEvents(std::map<std::tuple<int32_t, int32_t, std::string>, int32_t> &datas)
148 {
149 CALL_DEBUG_ENTER;
150 std::lock_guard guard(mapMtx_);
151 for (auto map : napMap_) {
152 int32_t getPid = map.first.pid;
153 int32_t getUid = map.first.uid;
154 std::string getName = map.first.bundleName;
155 int32_t getStatus = map.second;
156 std::tuple<int32_t, int32_t, std::string> tuple(getPid, getUid, getName);
157 datas.emplace(tuple, getStatus);
158 }
159 return RET_OK;
160 }
161 } // namespace MMI
162 } // namespace OHOS
163