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 <chrono>
19 
20 #include "define_multimodal.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     clientDeathHandler.AddClientDeathCallback(CallBackType::CALLBACK_TYPE_AUTHORIZE_HELPER,
72         [&](int32_t pid) -> void { OnClientDeath(pid); });
73     isInit_ = true;
74 }
75 
OnClientDeath(int32_t pid)76 void AuthorizeHelper::OnClientDeath(int32_t pid)
77 {
78     CALL_DEBUG_ENTER;
79     if (pid != pid_) {
80         MMI_HILOGD("Cancel process is inconsistent with authorize, cancel pid:%{public}d, authorize pid:%{public}d",
81             pid, pid_);
82         return;
83     }
84     AuthorizeProcessExit();
85 }
86 
AuthorizeProcessExit()87 void AuthorizeHelper::AuthorizeProcessExit()
88 {
89     CALL_DEBUG_ENTER;
90     std::lock_guard<std::mutex> lock(mutex_);
91     state_ = AuthorizeState::STATE_UNAUTHORIZE;
92     if (exitCallback_ != nullptr) {
93         MMI_HILOGI("Exit callback function will be called, authorize pid:%{public}d", pid_);
94         exitCallback_(pid_);
95     }
96     pid_ = INVALID_AUTHORIZE_PID;
97 }
98 
AddAuthorizeProcess(int32_t pid,AuthorizeExitCallback exitCallback)99 int32_t AuthorizeHelper::AddAuthorizeProcess(int32_t pid, AuthorizeExitCallback exitCallback)
100 {
101     CALL_DEBUG_ENTER;
102     if (!isInit_) {
103         MMI_HILOGI("Not init");
104         return RET_ERR;
105     }
106 
107     if (pid <= 0) {
108         MMI_HILOGI("Invalid process id, pid:%{public}d", pid);
109         return RET_ERR;
110     }
111 
112     std::lock_guard<std::mutex> lock(mutex_);
113     if (state_ == AuthorizeState::STATE_UNAUTHORIZE) {
114         if (pid_ != INVALID_AUTHORIZE_PID) {
115             MMI_HILOGI("Failed to authorize helper state.state:%{public}d,pid_:%{public}d,pid:%{public}d",
116                 state_, pid_, pid);
117             return RET_ERR;
118         }
119         pid_ = pid;
120         state_ = AuthorizeState::STATE_SELECTION_AUTHORIZE;
121         exitCallback_ = exitCallback;
122         MMI_HILOGD("A process enters the authorization select state %{public}d", state_);
123         return RET_OK;
124     }
125     if (pid_ != pid) {
126         MMI_HILOGI("The process that has been authorized is different from input.pid_:%{public}d,pid:%{public}d",
127             pid_, pid);
128         return RET_ERR;
129     }
130     if (state_ == AuthorizeState::STATE_SELECTION_AUTHORIZE) {
131         state_ = AuthorizeState::STATE_AUTHORIZE;
132     }
133     exitCallback_ = exitCallback;
134     MMI_HILOGD("A process will be authorized, authorize pid:%{public}d", pid_);
135     return RET_OK;
136 }
137 
CancelAuthorize(int32_t pid)138 void AuthorizeHelper::CancelAuthorize(int32_t pid)
139 {
140     CALL_DEBUG_ENTER;
141     if (pid <= 0) {
142         MMI_HILOGI("Invalid process id, pid:%{public}d", pid);
143         return;
144     }
145     std::lock_guard<std::mutex> lock(mutex_);
146     if (pid != pid_) {
147         MMI_HILOGI("Cancel pid isn't the authorized process id, cancel pid:%{public}d, authorize pid:%{public}d", pid,
148             pid_);
149     }
150     state_ = AuthorizeState::STATE_UNAUTHORIZE;
151     pid_ = INVALID_AUTHORIZE_PID;
152     exitCallback_ = nullptr;
153 }
154 } // namespace MMI
155 } // namespace OHOS
156