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