1 /*
2  * Copyright (C) 2021-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 "running_lock.h"
17 
18 #include <cstdint>
19 #include <list>
20 #include <iosfwd>
21 #include <string>
22 #include <timer.h>
23 #include "errors.h"
24 #include "new"
25 #include "refbase.h"
26 #include "power_log.h"
27 #include "power_mgr_errors.h"
28 #include "running_lock_token_stub.h"
29 #include <bundle_mgr_client.h>
30 
31 namespace OHOS {
32 namespace PowerMgr {
33 constexpr int32_t DEFAULT_TIMEOUT = 3000;
34 constexpr int32_t NOT_USE_TIMEOUT = -1;
RunningLock(const wptr<IPowerMgr> & proxy,const std::string & name,RunningLockType type)35 RunningLock::RunningLock(const wptr<IPowerMgr>& proxy, const std::string& name, RunningLockType type)
36     : proxy_(proxy)
37 {
38     runningLockInfo_.name = name;
39     runningLockInfo_.type = type;
40 }
41 
~RunningLock()42 RunningLock::~RunningLock()
43 {
44     if (token_ != nullptr) {
45         Release();
46     }
47 }
48 
Init()49 PowerErrors RunningLock::Init()
50 {
51     token_ = new (std::nothrow)RunningLockTokenStub();
52     if (token_ == nullptr) {
53         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Failed to create the RunningLockTokenStub");
54         return PowerErrors::ERR_CONNECTION_FAIL;
55     }
56     return Create();
57 }
58 
Create()59 PowerErrors RunningLock::Create()
60 {
61     sptr<IPowerMgr> proxy = proxy_.promote();
62     if (proxy == nullptr) {
63         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Proxy is a null pointer");
64         return PowerErrors::ERR_CONNECTION_FAIL;
65     }
66     return proxy->CreateRunningLock(token_, runningLockInfo_);
67 }
68 
Recover(const wptr<IPowerMgr> & proxy)69 PowerErrors RunningLock::Recover(const wptr<IPowerMgr>& proxy)
70 {
71     POWER_HILOGI(FEATURE_RUNNING_LOCK, "recover running lock name %{public}s type %{public}d",
72         runningLockInfo_.name.c_str(), runningLockInfo_.type);
73     proxy_ = proxy;
74     return Create();
75 }
76 
UpdateWorkSource(const std::vector<int32_t> & workSources)77 ErrCode RunningLock::UpdateWorkSource(const std::vector<int32_t>& workSources)
78 {
79     sptr<IPowerMgr> proxy = proxy_.promote();
80     if (proxy == nullptr) {
81         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Proxy is a null pointer");
82         return E_GET_POWER_SERVICE_FAILED;
83     }
84     std::map<int32_t, std::string> wks;
85     for (const auto& uid : workSources) {
86         std::string bundleName = GetBundleNameByUid(uid);
87         wks.emplace(std::make_pair(uid, bundleName));
88     }
89     if (!proxy->UpdateWorkSource(token_, wks)) {
90         return E_INNER_ERR;
91     }
92     return ERR_OK;
93 }
94 
Lock(int32_t timeOutMs)95 ErrCode RunningLock::Lock(int32_t timeOutMs)
96 {
97     sptr<IPowerMgr> proxy = proxy_.promote();
98     if (proxy == nullptr) {
99         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Proxy is a null pointer");
100         return E_GET_POWER_SERVICE_FAILED;
101     }
102     POWER_HILOGD(FEATURE_RUNNING_LOCK, "Service side Lock call, timeOutMs=%{public}d", timeOutMs);
103     if (runningLockInfo_.type == RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL) {
104         timeOutMs = NOT_USE_TIMEOUT;
105         POWER_HILOGW(FEATURE_RUNNING_LOCK, "PROXIMITY not use timeout");
106     }
107     if (timeOutMs == 0) {
108         timeOutMs = DEFAULT_TIMEOUT;
109         POWER_HILOGW(FEATURE_RUNNING_LOCK, "use default timeout");
110     }
111     PowerErrors error = proxy->Lock(token_, timeOutMs);
112     if (error != PowerErrors::ERR_OK) {
113         return error == PowerErrors::ERR_PERMISSION_DENIED ? E_PERMISSION_DENIED : E_INNER_ERR;
114     }
115     return ERR_OK;
116 }
117 
UnLock()118 ErrCode RunningLock::UnLock()
119 {
120     sptr<IPowerMgr> proxy = proxy_.promote();
121     if (proxy == nullptr) {
122         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Proxy is a null pointer");
123         return E_GET_POWER_SERVICE_FAILED;
124     }
125     POWER_HILOGD(FEATURE_RUNNING_LOCK, "Service side UnLock call");
126     PowerErrors error = proxy->UnLock(token_, runningLockInfo_.name);
127     if (error != PowerErrors::ERR_OK) {
128         return error == PowerErrors::ERR_PERMISSION_DENIED ? E_PERMISSION_DENIED : E_INNER_ERR;
129     }
130     return ERR_OK;
131 }
132 
IsUsed()133 bool RunningLock::IsUsed()
134 {
135     sptr<IPowerMgr> proxy = proxy_.promote();
136     if (proxy == nullptr) {
137         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Proxy is a null pointer");
138         return false;
139     }
140     bool ret = proxy->IsUsed(token_);
141     POWER_HILOGD(FEATURE_RUNNING_LOCK, "Is Used: %{public}d", ret);
142     return ret;
143 }
144 
Release()145 void RunningLock::Release()
146 {
147     sptr<IPowerMgr> proxy = proxy_.promote();
148     if (proxy == nullptr) {
149         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Proxy is a null pointer");
150         return;
151     }
152     POWER_HILOGI(FEATURE_RUNNING_LOCK, "ReleaseRunningLock name=%{public}s", runningLockInfo_.name.c_str());
153     proxy->ReleaseRunningLock(token_, runningLockInfo_.name);
154 }
155 
GetBundleNameByUid(const int32_t uid)156 std::string RunningLock::GetBundleNameByUid(const int32_t uid)
157 {
158     std::string bundleName = "";
159     if (uid < OHOS::AppExecFwk::Constants::BASE_APP_UID) {
160         POWER_HILOGE(FEATURE_RUNNING_LOCK, "GetBundleNameByUid Invalid for uid=%{public}d", uid);
161         return bundleName;
162     }
163     AppExecFwk::BundleMgrClient bundleObj;
164     ErrCode res = bundleObj.GetNameForUid(uid, bundleName);
165     if (res != ERR_OK) {
166         POWER_HILOGE(FEATURE_RUNNING_LOCK, "GetBundleNameByUid failed for uid=%{public}d, ErrCode=%{public}d",
167             uid, static_cast<int32_t>(res));
168     }
169     POWER_HILOGD(FEATURE_RUNNING_LOCK, "GetBundleNameByUid for uid=%{public}d, name=%{public}s",
170         uid, bundleName.c_str());
171     return bundleName;
172 }
173 } // namespace PowerMgr
174 } // namespace OHOS
175