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