1 /*
2 * Copyright (c) 2024 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 "authorize_helper.h"
17
18 #include "error_multimodal.h"
19 #include "iinput_binder_client.h"
20 #include "input_binder_client_proxy.h"
21 #include "mmi_log.h"
22
23 #undef MMI_LOG_DOMAIN
24 #define MMI_LOG_DOMAIN MMI_LOG_SERVER
25 #undef MMI_LOG_TAG
26 #define MMI_LOG_TAG "AuthorizeHelper"
27
28 namespace OHOS {
29 namespace MMI {
30 namespace {
31 constexpr int32_t INVALID_AUTHORIZE_PID = -1; // The pid must be greater than 0(0 is the init process, except here)
32 } // namespace
33
34 std::mutex AuthorizeHelper::mutex_;
35 std::shared_ptr<AuthorizeHelper> AuthorizeHelper::instance_;
36
AuthorizeHelper()37 AuthorizeHelper::AuthorizeHelper() : pid_(INVALID_AUTHORIZE_PID)
38 {
39 }
40
~AuthorizeHelper()41 AuthorizeHelper::~AuthorizeHelper()
42 {
43 }
44
GetInstance()45 std::shared_ptr<AuthorizeHelper> AuthorizeHelper::GetInstance()
46 {
47 if (instance_ == nullptr) {
48 std::lock_guard<std::mutex> lock(mutex_);
49 if (instance_ == nullptr) {
50 instance_ = std::make_shared<AuthorizeHelper>();
51 }
52 }
53 return instance_;
54 }
55
GetAuthorizePid()56 int32_t AuthorizeHelper::GetAuthorizePid()
57 {
58 std::lock_guard<std::mutex> lock(mutex_);
59 auto pid = pid_;
60 return pid;
61 }
62
Init(ClientDeathHandler * clientDeathHandler)63 void AuthorizeHelper::Init(ClientDeathHandler* clientDeathHandler)
64 {
65 CALL_DEBUG_ENTER;
66 if (isInit_) {
67 MMI_HILOGD("Already initialized, no need to initialize again");
68 return;
69 }
70
71 if (clientDeathHandler == nullptr) {
72 MMI_HILOGE("clientDeathHandler is nullptr");
73 return;
74 }
75
76 clientDeathHandler->AddClientDeathCallback(CallBackType::CALLBACK_TYPE_AUTHORIZE_HELPER,
77 [&](int32_t pid) -> void { OnClientDeath(pid); });
78 clientDeathHandler_ = clientDeathHandler;
79 isInit_ = true;
80 }
81
OnClientDeath(int32_t pid)82 void AuthorizeHelper::OnClientDeath(int32_t pid)
83 {
84 CALL_DEBUG_ENTER;
85 {
86 std::lock_guard<std::mutex> lock(mutex_);
87 ClearRequestInjectionCallback(pid);
88 }
89 if (pid != pid_) {
90 MMI_HILOGD("Cancel process is inconsistent with authorize, cancel pid:%{public}d, authorize pid:%{public}d",
91 pid, pid_);
92 return;
93 }
94 AuthorizeProcessExit();
95 }
96
AuthorizeProcessExit()97 void AuthorizeHelper::AuthorizeProcessExit()
98 {
99 CALL_DEBUG_ENTER;
100 std::lock_guard<std::mutex> lock(mutex_);
101 state_ = AuthorizeState::STATE_UNAUTHORIZE;
102 if (exitCallback_ != nullptr) {
103 MMI_HILOGI("Exit callback function will be called, authorize pid:%{public}d", pid_);
104 exitCallback_(pid_);
105 }
106 pid_ = INVALID_AUTHORIZE_PID;
107 }
108
AddAuthorizeProcess(int32_t pid,AuthorizeExitCallback exitCallback,const int32_t reqId)109 int32_t AuthorizeHelper::AddAuthorizeProcess(int32_t pid, AuthorizeExitCallback exitCallback, const int32_t reqId)
110 {
111 CALL_DEBUG_ENTER;
112 if (!isInit_) {
113 MMI_HILOGI("Not init");
114 return RET_ERR;
115 }
116
117 if (pid <= 0) {
118 MMI_HILOGI("Invalid process id, pid:%{public}d", pid);
119 return RET_ERR;
120 }
121
122 std::lock_guard<std::mutex> lock(mutex_);
123 if (state_ == AuthorizeState::STATE_UNAUTHORIZE) {
124 if (pid_ != INVALID_AUTHORIZE_PID) {
125 MMI_HILOGI("Failed to authorize helper state.state:%{public}d,pid_:%{public}d,pid:%{public}d",
126 state_, pid_, pid);
127 return RET_ERR;
128 }
129 pid_ = pid;
130 state_ = AuthorizeState::STATE_SELECTION_AUTHORIZE;
131 exitCallback_ = exitCallback;
132 if (reqId > 0) {
133 mapQueryAuthorizeInfo_.insert(std::make_pair(reqId, pid_));
134 }
135 MMI_HILOGD("A process enters the authorization select state %{public}d", state_);
136 return RET_OK;
137 }
138 if (pid_ != pid) {
139 MMI_HILOGI("The process that has been authorized is different from input.pid_:%{public}d,pid:%{public}d",
140 pid_, pid);
141 return RET_ERR;
142 }
143 if (state_ == AuthorizeState::STATE_SELECTION_AUTHORIZE) {
144 state_ = AuthorizeState::STATE_AUTHORIZE;
145 NotifyRequestInjectionResult();
146 }
147 exitCallback_ = exitCallback;
148 MMI_HILOGD("A process will be authorized, authorize pid:%{public}d", pid_);
149 return RET_OK;
150 }
151
CancelAuthorize(int32_t pid)152 void AuthorizeHelper::CancelAuthorize(int32_t pid)
153 {
154 CALL_DEBUG_ENTER;
155 if (pid <= 0) {
156 MMI_HILOGI("Invalid process id, pid:%{public}d", pid);
157 return;
158 }
159 std::lock_guard<std::mutex> lock(mutex_);
160 if (pid != pid_) {
161 MMI_HILOGI("Cancel pid isn't the authorized process id, cancel pid:%{public}d, authorize pid:%{public}d", pid,
162 pid_);
163 }
164 state_ = AuthorizeState::STATE_UNAUTHORIZE;
165 pid_ = INVALID_AUTHORIZE_PID;
166 NotifyRequestInjectionResult();
167 exitCallback_ = nullptr;
168 }
169
NotifyRequestInjectionResult()170 void AuthorizeHelper::NotifyRequestInjectionResult()
171 {
172 CALL_DEBUG_ENTER;
173 if (clientDeathHandler_ == nullptr) {
174 MMI_HILOGE("clientDeathHandler is nullptr");
175 return;
176 }
177 for (auto it = mapQueryAuthorizeInfo_.begin(); it != mapQueryAuthorizeInfo_.end();) {
178 NoticeRequestInjectionResult(it->first, it->second);
179 mapQueryAuthorizeInfo_.erase(it++);
180 }
181 }
182
NoticeRequestInjectionResult(const int32_t reqId,const int32_t callingPid)183 void AuthorizeHelper::NoticeRequestInjectionResult(const int32_t reqId, const int32_t callingPid)
184 {
185 auto object = clientDeathHandler_->GetClientProxy(callingPid);
186 sptr<IInputBinderClient> pClientProxy = iface_cast<IInputBinderClient>(object);
187 if (!pClientProxy) {
188 MMI_HILOGE("pClientProxy is nullptr");
189 return;
190 }
191 auto sendStatus = AUTHORIZE_QUERY_STATE::UNAUTHORIZED;
192 if (state_ == AuthorizeState::STATE_AUTHORIZE) {
193 if (pid_ == callingPid) {
194 sendStatus = AUTHORIZE_QUERY_STATE::CURRENT_PID_AUTHORIZED;
195 }
196 }
197 MMI_HILOGD("result callback! %{public}d, %{public}d,state_:%{public}d,%{public}d",
198 pid_, sendStatus, state_, callingPid);
199 pClientProxy->NoticeRequestInjectionResult(reqId, static_cast<int32_t>(sendStatus));
200 }
201
ClearRequestInjectionCallback(int32_t callingPid)202 void AuthorizeHelper::ClearRequestInjectionCallback(int32_t callingPid)
203 {
204 CALL_DEBUG_ENTER;
205 for (auto it = mapQueryAuthorizeInfo_.begin(); it != mapQueryAuthorizeInfo_.end();) {
206 if (it->second == callingPid) {
207 mapQueryAuthorizeInfo_.erase(it++);
208 } else {
209 it++;
210 }
211 }
212 }
213 } // namespace MMI
214 } // namespace OHOS
215