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 "mmi_log.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 "AuthorizeHelper"
24
25 namespace OHOS {
26 namespace MMI {
27 namespace {
28 constexpr int32_t INVALID_AUTHORIZE_PID = -1; // The pid must be greater than 0(0 is the init process, except here)
29 } // namespace
30
31 std::mutex AuthorizeHelper::mutex_;
32 std::shared_ptr<AuthorizeHelper> AuthorizeHelper::instance_;
33
AuthorizeHelper()34 AuthorizeHelper::AuthorizeHelper() : pid_(INVALID_AUTHORIZE_PID)
35 {
36 }
37
~AuthorizeHelper()38 AuthorizeHelper::~AuthorizeHelper()
39 {
40 }
41
GetInstance()42 std::shared_ptr<AuthorizeHelper> AuthorizeHelper::GetInstance()
43 {
44 if (instance_ == nullptr) {
45 std::lock_guard<std::mutex> lock(mutex_);
46 if (instance_ == nullptr) {
47 instance_ = std::make_shared<AuthorizeHelper>();
48 }
49 }
50 return instance_;
51 }
52
GetAuthorizePid()53 int32_t AuthorizeHelper::GetAuthorizePid()
54 {
55 std::lock_guard<std::mutex> lock(mutex_);
56 auto pid = pid_;
57 return pid;
58 }
59
Init(ClientDeathHandler & clientDeathHandler)60 void AuthorizeHelper::Init(ClientDeathHandler &clientDeathHandler)
61 {
62 CALL_DEBUG_ENTER;
63 if (isInit_) {
64 MMI_HILOGD("Already initialized, no need to initialize again");
65 return;
66 }
67
68 clientDeathHandler.AddClientDeathCallback(CallBackType::CALLBACK_TYPE_AUTHORIZE_HELPER,
69 [&](int32_t pid) -> void { OnClientDeath(pid); });
70 isInit_ = true;
71 }
72
OnClientDeath(int32_t pid)73 void AuthorizeHelper::OnClientDeath(int32_t pid)
74 {
75 CALL_DEBUG_ENTER;
76 if (pid != pid_) {
77 MMI_HILOGD("Cancel process is inconsistent with authorize, cancel pid:%{public}d, authorize pid:%{public}d",
78 pid, pid_);
79 return;
80 }
81 AuthorizeProcessExit();
82 }
83
AuthorizeProcessExit()84 void AuthorizeHelper::AuthorizeProcessExit()
85 {
86 CALL_DEBUG_ENTER;
87 std::lock_guard<std::mutex> lock(mutex_);
88 state_ = AuthorizeState::STATE_UNAUTHORIZE;
89 if (exitCallback_ != nullptr) {
90 MMI_HILOGI("Exit callback function will be called, authorize pid:%{public}d", pid_);
91 exitCallback_(pid_);
92 }
93 pid_ = INVALID_AUTHORIZE_PID;
94 }
95
AddAuthorizeProcess(int32_t pid,AuthorizeExitCallback exitCallback)96 int32_t AuthorizeHelper::AddAuthorizeProcess(int32_t pid, AuthorizeExitCallback exitCallback)
97 {
98 CALL_DEBUG_ENTER;
99 if (!isInit_) {
100 MMI_HILOGI("Not init");
101 return RET_ERR;
102 }
103
104 if (pid <= 0) {
105 MMI_HILOGI("Invalid process id, pid:%{public}d", pid);
106 return RET_ERR;
107 }
108
109 std::lock_guard<std::mutex> lock(mutex_);
110 if (state_ == AuthorizeState::STATE_UNAUTHORIZE) {
111 if (pid_ != INVALID_AUTHORIZE_PID) {
112 MMI_HILOGI("Failed to authorize helper state.state:%{public}d,pid_:%{public}d,pid:%{public}d",
113 state_, pid_, pid);
114 return RET_ERR;
115 }
116 pid_ = pid;
117 state_ = AuthorizeState::STATE_SELECTION_AUTHORIZE;
118 exitCallback_ = exitCallback;
119 MMI_HILOGD("A process enters the authorization select state %{public}d", state_);
120 return RET_OK;
121 }
122 if (pid_ != pid) {
123 MMI_HILOGI("The process that has been authorized is different from input.pid_:%{public}d,pid:%{public}d",
124 pid_, pid);
125 return RET_ERR;
126 }
127 if (state_ == AuthorizeState::STATE_SELECTION_AUTHORIZE) {
128 state_ = AuthorizeState::STATE_AUTHORIZE;
129 }
130 exitCallback_ = exitCallback;
131 MMI_HILOGD("A process will be authorized, authorize pid:%{public}d", pid_);
132 return RET_OK;
133 }
134
CancelAuthorize(int32_t pid)135 void AuthorizeHelper::CancelAuthorize(int32_t pid)
136 {
137 CALL_DEBUG_ENTER;
138 if (pid <= 0) {
139 MMI_HILOGI("Invalid process id, pid:%{public}d", pid);
140 return;
141 }
142 std::lock_guard<std::mutex> lock(mutex_);
143 if (pid != pid_) {
144 MMI_HILOGI("Cancel pid isn't the authorized process id, cancel pid:%{public}d, authorize pid:%{public}d", pid,
145 pid_);
146 }
147 state_ = AuthorizeState::STATE_UNAUTHORIZE;
148 pid_ = INVALID_AUTHORIZE_PID;
149 exitCallback_ = nullptr;
150 }
151 } // namespace MMI
152 } // namespace OHOS
153