!539 fix: 支持RunningLock代理机制

Merge pull request !539 from yangziyong/runninglock_proxy
This commit is contained in:
openharmony_ci 2023-04-08 00:48:36 +00:00 committed by Gitee
commit 8e7e581d3a
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
37 changed files with 699 additions and 522 deletions

View File

@ -218,6 +218,12 @@ std::shared_ptr<RunningLock> PowerMgrClient::CreateRunningLock(const std::string
return runningLock;
}
bool PowerMgrClient::SetRunningLockProxy(bool isProxied, pid_t pid, pid_t uid)
{
RETURN_IF_WITH_RET(Connect() != ERR_OK, false);
return proxy_->SetRunningLockProxy(isProxied, pid, uid);
}
bool PowerMgrClient::RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback)
{
RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);

View File

@ -71,7 +71,7 @@ public:
virtual bool Lock(const sptr<IRemoteObject>& remoteObj, int32_t timeOutMs) = 0;
virtual bool UnLock(const sptr<IRemoteObject>& remoteObj) = 0;
virtual bool IsUsed(const sptr<IRemoteObject>& remoteObj) = 0;
virtual bool ProxyRunningLock(bool proxyLock, pid_t uid, pid_t pid) = 0;
virtual bool SetRunningLockProxy(bool isProxied, pid_t pid, pid_t uid) = 0;
// Used for power state machine.
virtual PowerErrors RebootDevice(const std::string& reason) = 0;

View File

@ -121,6 +121,7 @@ public:
PowerMode GetDeviceMode();
std::shared_ptr<RunningLock> CreateRunningLock(const std::string& name, RunningLockType type);
bool SetRunningLockProxy(bool isProxied, pid_t pid, pid_t uid);
bool RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback);
bool UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback);
bool RegisterShutdownCallback(const sptr<IShutdownCallback>& callback,

View File

@ -18,6 +18,7 @@ RUNNINGLOCK:
PID: {type: INT32, desc: application pid}
UID: {type: INT32, desc: application uid}
STATE: {type: INT32, desc: runninglock state}
PROXY_STATE: {type: BOOL, desc: runninglock proxy state}
TYPE: {type: INT32, desc: runninglock type}
NAME: {type: STRING, desc: runninglock name}
LOG_LEVEL: {type: INT32, desc: runninglock log level}

View File

@ -31,52 +31,8 @@ public:
IRunningLockAction() = default;
virtual ~IRunningLockAction() = default;
static inline std::string GetLockTag(RunningLockType type)
{
return LOCK_TAGS[ToUnderlying(type)];
}
virtual void Acquire(RunningLockType type);
virtual void Release(RunningLockType type);
virtual void Lock(const RunningLockParam& param) = 0;
virtual void Unlock(const RunningLockParam& param) = 0;
private:
class RunningLockDesc {
public:
inline void IncRef()
{
refCnt++;
}
inline void DecRef()
{
refCnt--;
}
inline bool IsRefNone() const
{
return refCnt == 0;
}
inline uint32_t GetRefCnt() const
{
return refCnt;
}
private:
uint32_t refCnt{0};
};
static inline bool IsValidType(RunningLockType type)
{
return type < RunningLockType::RUNNINGLOCK_BUTT;
}
RunningLockParam FillRunningLockParam(RunningLockType type);
static const std::array<std::string, ToUnderlying(RunningLockType::RUNNINGLOCK_BUTT)> LOCK_TAGS;
std::array<RunningLockDesc, ToUnderlying(RunningLockType::RUNNINGLOCK_BUTT)> lockDescs_;
virtual int32_t Lock(const RunningLockParam& param) = 0;
virtual int32_t Unlock(const RunningLockParam& param) = 0;
};
} // namespace PowerMgr
} // namespace OHOS

View File

@ -20,6 +20,28 @@
namespace OHOS {
namespace PowerMgr {
/**
* Enumerates runninglock operation return status
*/
enum RunningLockStatus : int32_t {
/**
* RunningLock status: runninglock operation is successful
*/
RUNNINGLOCK_SUCCESS = 0,
/**
* RunningLock status: runninglock operation is failed
*/
RUNNINGLOCK_FAILURE = -1,
/**
* RunningLock status: runninglock operation is not supported, not found the runninglock
*/
RUNNINGLOCK_NOT_SUPPORT = -2,
/**
* RunningLock status: invalid input parameter for runninglock operation
*/
RUNNINGLOCK_INVALID_PARAM = -3,
};
/**
* Runninglock parameter: used to convert to HDI RunningLockInfo
*/

View File

@ -64,7 +64,7 @@ public:
virtual bool UnLock(const sptr<IRemoteObject>& remoteObj) override;
virtual void ForceUnLock(const sptr<IRemoteObject>& remoteObj);
virtual bool IsUsed(const sptr<IRemoteObject>& remoteObj) override;
virtual bool ProxyRunningLock(bool proxyLock, pid_t uid, pid_t pid) override;
virtual bool SetRunningLockProxy(bool isProxied, pid_t pid, pid_t uid) override;
virtual bool RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback) override;
virtual bool UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback) override;
virtual bool RegisterShutdownCallback(IShutdownCallback::ShutdownPriority priority,

View File

@ -48,25 +48,41 @@ public:
{
return runningLockParam_.uid;
}
void SetRunningLockTimeOutMs(int32_t timeoutMs)
{
runningLockParam_.timeoutMs = timeoutMs;
}
int32_t GetRunningLockTimeOutMs() const
{
return runningLockParam_.timeoutMs;
}
const RunningLockParam& GetRunningLockParam() const
{
return runningLockParam_;
}
void SetDisabled(bool disabled)
void SetEnabled(bool isEnabled)
{
disabled_ = disabled;
isEnabled_ = isEnabled;
}
bool GetDisabled() const
bool GetEnabled() const
{
return disabled_;
return isEnabled_;
}
void SetReallyLocked(bool reallyLocked)
void SetProxied(bool isProxied)
{
reallyLocked_ = reallyLocked;
isProxied_ = isProxied;
}
bool GetReallyLocked() const
bool GetProxied() const
{
return reallyLocked_;
return isProxied_;
}
void SetNeedRestoreLock(bool need)
{
needRestoreLock_ = need;
}
bool GetNeedRestoreLock() const
{
return needRestoreLock_;
}
void SetOverTimeFlag(bool overTimeFlag)
{
@ -84,8 +100,9 @@ public:
private:
std::mutex mutex_;
RunningLockParam runningLockParam_;
bool disabled_ {true};
bool reallyLocked_ {false};
bool isEnabled_ {false};
bool isProxied_ {false};
bool needRestoreLock_ {false};
bool overTimeFlag_ {false};
int64_t lockTimeMs_ {0};
};

View File

@ -63,7 +63,7 @@ public:
{
return proxyMap_;
}
void ProxyRunningLock(bool proxyLock, pid_t uid, pid_t pid);
bool SetRunningLockProxy(bool isProxied, pid_t pid, pid_t uid);
bool IsUsed(const sptr<IRemoteObject>& remoteObj);
static constexpr uint32_t CHECK_TIMEOUT_INTERVAL_MS = 60 * 1000;
void CheckOverTime();
@ -165,12 +165,12 @@ private:
};
bool InitLocks();
bool MatchProxyMap(const int32_t pid, const int32_t uid);
void SetRunningLockDisableFlag(std::shared_ptr<RunningLockInner>& lockInner, bool forceRefresh = false);
void LockReally(const sptr<IRemoteObject>& remoteObj, std::shared_ptr<RunningLockInner>& lockInner);
void UnLockReally(const sptr<IRemoteObject>& remoteObj, std::shared_ptr<RunningLockInner>& lockInner);
void ProxyRunningLockInner(bool proxyLock);
void SetRunningLockProxiedFlag(std::shared_ptr<RunningLockInner>& lockInner);
void SetRunningLockProxyInner(bool isProxied);
void RemoveAndPostUnlockTask(const sptr<IRemoteObject>& remoteObj, int32_t timeOutMS = -1);
bool IsSceneRunningLockType(RunningLockType type);
void UnlockInnerByProxy(const sptr<IRemoteObject>& remoteObj, std::shared_ptr<RunningLockInner>& lockInner);
void LockInnerByProxy(const sptr<IRemoteObject>& remoteObj, std::shared_ptr<RunningLockInner>& lockInner);
const wptr<PowerMgrService> pms_;
ProximityController proximityController_;
std::weak_ptr<PowermsEventHandler> handler_;

View File

@ -14,34 +14,11 @@
import("//base/powermgr/power_manager/powermgr.gni")
import("actions.gni")
config("powermgr_actions_common_public_config") {
include_dirs = [
"${powermgr_innerkits}/native/include",
"${powermgr_service_path}/native/include",
]
}
ohos_source_set("powermgr_actions_common") {
sources = [ "irunning_lock_action.cpp" ]
public_configs = [
":powermgr_actions_common_public_config",
"${powermgr_utils_path}:utils_config",
]
external_deps = [ "hiviewdfx_hilog_native:libhilog" ]
part_name = "${powermgr_part_name}"
}
actions_target = powermgr_actions_target
if (system_type == powermgr_actions_default) {
actions_target = powermgr_actions_default_target
}
group("powermgr_actions") {
public_deps = [
":powermgr_actions_common",
"${system_type}:${actions_target}",
]
public_deps = [ "${system_type}:${actions_target}" ]
}

View File

@ -17,10 +17,18 @@ import("../actions.gni")
config("powermgr_actions_impl_public_config") {
include_dirs = [ "." ]
}
config("powermgr_display_actions_impl_public_config") {
include_dirs = [ "./display" ]
}
config("powermgr_actions_common_config") {
include_dirs = [
"${powermgr_innerkits}/native/include",
"${powermgr_service_path}/native/include",
]
}
ohos_source_set("${powermgr_actions_default_target}") {
sources = [
"device_power_action.cpp",
@ -30,9 +38,12 @@ ohos_source_set("${powermgr_actions_default_target}") {
"system_suspend_controller.cpp",
]
public_configs = [ ":powermgr_actions_impl_public_config" ]
configs = [
":powermgr_actions_common_config",
"${powermgr_utils_path}:utils_config",
]
deps = [ "../:powermgr_actions_common" ]
public_configs = [ ":powermgr_actions_impl_public_config" ]
external_deps = [
"c_utils:utils",
@ -50,7 +61,6 @@ ohos_source_set("${powermgr_actions_default_target}") {
if (has_display_manager_part) {
sources += [ "display/device_state_action.cpp" ]
public_configs += [ ":powermgr_display_actions_impl_public_config" ]
deps += [ "//foundation/window/window_manager/utils:libwmutil" ]
external_deps += [
"display_manager:displaymgr",
"window_manager:libdm",

View File

@ -21,14 +21,14 @@ using namespace std;
namespace OHOS {
namespace PowerMgr {
void RunningLockAction::Lock(const RunningLockParam& param)
int32_t RunningLockAction::Lock(const RunningLockParam& param)
{
SystemSuspendController::GetInstance().AcquireRunningLock(param);
return SystemSuspendController::GetInstance().AcquireRunningLock(param);
}
void RunningLockAction::Unlock(const RunningLockParam& param)
int32_t RunningLockAction::Unlock(const RunningLockParam& param)
{
SystemSuspendController::GetInstance().ReleaseRunningLock(param);
return SystemSuspendController::GetInstance().ReleaseRunningLock(param);
}
} // namespace PowerMgr
} // namespace OHOS

View File

@ -22,8 +22,8 @@ namespace OHOS {
namespace PowerMgr {
class RunningLockAction : public IRunningLockAction {
public:
void Lock(const RunningLockParam& param) override;
void Unlock(const RunningLockParam& param) override;
int32_t Lock(const RunningLockParam& param) override;
int32_t Unlock(const RunningLockParam& param) override;
};
} // namespace PowerMgr
} // namespace OHOS

View File

@ -136,35 +136,39 @@ OHOS::HDI::Power::V1_1::RunningLockInfo SystemSuspendController::FillRunningLock
return filledInfo;
}
void SystemSuspendController::AcquireRunningLock(const RunningLockParam& param)
int32_t SystemSuspendController::AcquireRunningLock(const RunningLockParam& param)
{
#ifndef POWER_SUSPEND_NO_HDI
int32_t status = RUNNINGLOCK_FAILURE;
if (powerInterface_ == nullptr) {
POWER_HILOGE(COMP_SVC, "The hdf interface is null");
return;
return status;
}
if (param.type == RunningLockType::RUNNINGLOCK_BACKGROUND) {
powerInterface_->SuspendBlock(param.name);
status = powerInterface_->SuspendBlock(param.name);
} else {
OHOS::HDI::Power::V1_1::RunningLockInfo filledInfo = FillRunningLockInfo(param);
powerInterface_->HoldRunningLock(filledInfo);
status = powerInterface_->HoldRunningLock(filledInfo);
}
return status;
#endif
}
void SystemSuspendController::ReleaseRunningLock(const RunningLockParam& param)
int32_t SystemSuspendController::ReleaseRunningLock(const RunningLockParam& param)
{
#ifndef POWER_SUSPEND_NO_HDI
int32_t status = RUNNINGLOCK_FAILURE;
if (powerInterface_ == nullptr) {
POWER_HILOGE(COMP_SVC, "The hdf interface is null");
return;
return status;
}
if (param.type == RunningLockType::RUNNINGLOCK_BACKGROUND) {
powerInterface_->SuspendUnblock(param.name);
status = powerInterface_->SuspendUnblock(param.name);
} else {
OHOS::HDI::Power::V1_1::RunningLockInfo filledInfo = FillRunningLockInfo(param);
powerInterface_->UnholdRunningLock(filledInfo);
status = powerInterface_->UnholdRunningLock(filledInfo);
}
return status;
#endif
}

View File

@ -35,8 +35,8 @@ class SystemSuspendController : public DelayedRefSingleton<SystemSuspendControll
public:
void Suspend(const std::function<void()>& onSuspend, const std::function<void()>& onWakeup, bool force);
void Wakeup();
void AcquireRunningLock(const RunningLockParam& param);
void ReleaseRunningLock(const RunningLockParam& param);
int32_t AcquireRunningLock(const RunningLockParam& param);
int32_t ReleaseRunningLock(const RunningLockParam& param);
void Dump(std::string& info);
void RegisterHdiStatusListener(const std::shared_ptr<PowermsEventHandler>& handler);
void RegisterPowerHdiCallback();

View File

@ -1,75 +0,0 @@
/*
* Copyright (c) 2021-2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "actions/irunning_lock_action.h"
#include "power_log.h"
using namespace std;
namespace OHOS {
namespace PowerMgr {
const array<string, ToUnderlying(RunningLockType::RUNNINGLOCK_BUTT)> IRunningLockAction::LOCK_TAGS {
"OHOSPowerMgr.Screen", "OHOSPowerMgr.Background", "OHOSPowerMgr.Proximity",
};
void IRunningLockAction::Acquire(RunningLockType type)
{
if (!IsValidType(type)) {
POWER_HILOGE(FEATURE_RUNNING_LOCK, "Invalid runninglock type");
return;
}
auto t = ToUnderlying(type);
RunningLockDesc& desc = lockDescs_[t];
if (desc.IsRefNone()) {
Lock(FillRunningLockParam(type));
}
desc.IncRef();
POWER_HILOGD(FEATURE_RUNNING_LOCK, "Acquire runninglock, type: %{public}u, refCnt: %{public}u", t,
desc.GetRefCnt());
}
void IRunningLockAction::Release(RunningLockType type)
{
if (!IsValidType(type)) {
POWER_HILOGE(FEATURE_RUNNING_LOCK, "Invalid runninglock type");
return;
}
auto t = ToUnderlying(type);
RunningLockDesc& desc = lockDescs_[t];
if (desc.IsRefNone()) {
POWER_HILOGE(FEATURE_RUNNING_LOCK, "Invalid refCnt of wakelock: %{public}u", t);
return;
}
desc.DecRef();
POWER_HILOGD(FEATURE_RUNNING_LOCK, "Release runninglock, type: %{public}u, refCnt: %{public}u", t,
desc.GetRefCnt());
if (desc.IsRefNone()) {
Unlock(FillRunningLockParam(type));
}
}
RunningLockParam IRunningLockAction::FillRunningLockParam(RunningLockType type)
{
RunningLockParam filledParam {};
filledParam.name = GetLockTag(type);
filledParam.type = type;
return filledParam;
}
} // namespace PowerMgr
} // namespace OHOS

View File

@ -808,14 +808,13 @@ void PowerMgrService::NotifyRunningLockChanged(bool isUnLock)
}
}
bool PowerMgrService::ProxyRunningLock(bool proxyLock, pid_t uid, pid_t pid)
bool PowerMgrService::SetRunningLockProxy(bool isProxied, pid_t pid, pid_t uid)
{
std::lock_guard lock(lockMutex_);
if (!Permission::IsSystem()) {
return false;
}
runningLockMgr_->ProxyRunningLock(proxyLock, uid, pid);
return true;
return runningLockMgr_->SetRunningLockProxy(isProxied, pid, uid);
}
bool PowerMgrService::RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback)

View File

@ -38,7 +38,7 @@ namespace OHOS {
namespace PowerMgr {
namespace {
const string TASK_RUNNINGLOCK_FORCEUNLOCK = "RunningLock_ForceUnLock";
constexpr int32_t INVALID_PID = -1;
constexpr int32_t VALID_PID_LIMIT = 1;
}
RunningLockMgr::~RunningLockMgr() {}
@ -212,6 +212,7 @@ std::shared_ptr<RunningLockInner> RunningLockMgr::CreateRunningLock(const sptr<I
if (lockInner == nullptr) {
return nullptr;
}
SetRunningLockProxiedFlag(lockInner);
POWER_HILOGD(FEATURE_RUNNING_LOCK, "Create lock success, name=%{public}s, type=%{public}d",
runningLockParam.name.c_str(), runningLockParam.type);
@ -275,8 +276,12 @@ void RunningLockMgr::Lock(const sptr<IRemoteObject>& remoteObj, int32_t timeOutM
POWER_HILOGE(FEATURE_RUNNING_LOCK, "LockInner is nullptr");
return;
}
if (lockInner->GetProxied()) {
POWER_HILOGE(FEATURE_RUNNING_LOCK, "Runninglock is proxied");
return;
}
lockInner->SetRunningLockTimeOutMs(timeOutMS);
RunningLockParam lockInnerParam = lockInner->GetRunningLockParam();
lockInnerParam.timeoutMs = timeOutMS;
POWER_HILOGD(FEATURE_RUNNING_LOCK, "name=%{public}s, type=%{public}d, timeoutMs=%{public}d",
lockInnerParam.name.c_str(), lockInnerParam.type, timeOutMS);
if (IsSceneRunningLockType(lockInnerParam.type)) {
@ -285,7 +290,7 @@ void RunningLockMgr::Lock(const sptr<IRemoteObject>& remoteObj, int32_t timeOutM
return;
}
if (!lockInner->GetDisabled()) {
if (lockInner->GetEnabled()) {
POWER_HILOGD(FEATURE_RUNNING_LOCK, "Lock is already enabled");
return;
}
@ -296,7 +301,7 @@ void RunningLockMgr::Lock(const sptr<IRemoteObject>& remoteObj, int32_t timeOutM
}
std::shared_ptr<LockCounter> counter = iterator->second;
counter->Increase(remoteObj, lockInner);
lockInner->SetDisabled(false);
lockInner->SetEnabled(true);
POWER_HILOGD(FEATURE_RUNNING_LOCK, "LockCounter type=%{public}d, count=%{public}d", lockInnerParam.type,
counter->GetCount());
if (timeOutMS > 0) {
@ -313,6 +318,10 @@ void RunningLockMgr::UnLock(const sptr<IRemoteObject> remoteObj)
if (lockInner == nullptr) {
return;
}
if (lockInner->GetProxied()) {
POWER_HILOGE(FEATURE_RUNNING_LOCK, "Runninglock is proxied");
return;
}
auto lockInnerParam = lockInner->GetRunningLockParam();
POWER_HILOGD(
FEATURE_RUNNING_LOCK, "name=%{public}s, type=%{public}d", lockInnerParam.name.c_str(), lockInnerParam.type);
@ -322,12 +331,11 @@ void RunningLockMgr::UnLock(const sptr<IRemoteObject> remoteObj)
return;
}
if (lockInner->GetDisabled()) {
POWER_HILOGD(FEATURE_RUNNING_LOCK, "Lock is already disabled, name=%{public}s",
if (!lockInner->GetEnabled()) {
POWER_HILOGD(FEATURE_RUNNING_LOCK, "Lock is not enabled, name=%{public}s",
lockInner->GetRunningLockName().c_str());
return;
}
RemoveAndPostUnlockTask(remoteObj);
auto iterator = lockCounters_.find(lockInnerParam.type);
if (iterator == lockCounters_.end()) {
@ -335,9 +343,10 @@ void RunningLockMgr::UnLock(const sptr<IRemoteObject> remoteObj)
lockInnerParam.type);
return;
}
RemoveAndPostUnlockTask(remoteObj);
std::shared_ptr<LockCounter> counter = iterator->second;
counter->Decrease(remoteObj, lockInner);
lockInner->SetDisabled(true);
lockInner->SetEnabled(false);
POWER_HILOGD(FEATURE_RUNNING_LOCK, "LockCounter type=%{public}d, count=%{public}d", lockInnerParam.type,
counter->GetCount());
FinishTrace(HITRACE_TAG_POWER);
@ -346,7 +355,7 @@ void RunningLockMgr::UnLock(const sptr<IRemoteObject> remoteObj)
bool RunningLockMgr::IsUsed(const sptr<IRemoteObject>& remoteObj)
{
auto lockInner = GetRunningLockInner(remoteObj);
if (lockInner == nullptr || lockInner->GetDisabled()) {
if (lockInner == nullptr || !lockInner->GetEnabled()) {
return false;
}
return true;
@ -380,7 +389,7 @@ bool RunningLockMgr::ExistValidRunningLock()
std::lock_guard<std::mutex> lock(mutex_);
for (auto it = runningLocks_.begin(); it != runningLocks_.end(); it++) {
auto& lockinner = it->second;
if (lockinner->GetDisabled() == false) {
if (lockinner->GetEnabled() == true) {
return true;
}
}
@ -415,7 +424,7 @@ void RunningLockMgr::CheckOverTime()
int64_t nextDetectTime = INT_MAX;
for (auto& it : runningLocks_) {
auto lockInner = it.second;
if (!lockInner->GetDisabled() && (!lockInner->GetOverTimeFlag())) {
if (lockInner->GetEnabled() && (!lockInner->GetOverTimeFlag())) {
if (lockInner->GetLockTimeMs() < detectTime) {
lockInner->SetOverTimeFlag(true);
NotifyRunningLockChanged(it.first, lockInner, NOTIFY_RUNNINGLOCK_OVERTIME);
@ -474,162 +483,130 @@ void RunningLockMgr::NotifyRunningLockChanged(const sptr<IRemoteObject>& remoteO
bool RunningLockMgr::MatchProxyMap(const int32_t pid, const int32_t uid)
{
if (proxyMap_.empty()) {
POWER_HILOGW(FEATURE_RUNNING_LOCK, "ProxyMap_ is empty, uid = %{public}d, pid = %{public}d", uid, pid);
POWER_HILOGD(FEATURE_RUNNING_LOCK,
"Runnninglock proxy map is empty, uid=%{public}d, pid=%{public}d", uid, pid);
return false;
}
auto it = proxyMap_.find(uid);
// 1. Find pidset by uid.
if (it == proxyMap_.end()) {
POWER_HILOGW(FEATURE_RUNNING_LOCK, "Pid set not match, uid = %{public}d, pid = %{public}d", uid, pid);
POWER_HILOGD(FEATURE_RUNNING_LOCK,
"Uid not match runnninglock proxy map, uid=%{public}d, pid=%{public}d", uid, pid);
return false;
}
auto& pidset = it->second;
// 2. Count by owner pid.
if (pidset.count(pid) > 0) {
POWER_HILOGD(FEATURE_RUNNING_LOCK, "Pid set match and count > 0, uid = %{public}d, pid = %{public}d", uid, pid);
return true;
}
POWER_HILOGD(FEATURE_RUNNING_LOCK, "Pid set match and count(-1) = %{public}d, uid = %{public}d, pid = %{public}d",
static_cast<unsigned int>(pidset.count(INVALID_PID)), uid, pid);
// 3. Count by INVALID_PID, return true when proxy (uid, -1).
return (pidset.count(INVALID_PID) > 0);
return false;
}
void RunningLockMgr::SetRunningLockDisableFlag(
std::shared_ptr<RunningLockInner>& lockInner,
bool forceRefresh)
void RunningLockMgr::SetRunningLockProxiedFlag(std::shared_ptr<RunningLockInner>& lockInner)
{
if (proxyMap_.empty() && (!forceRefresh)) {
/**
* Generally when proxymap empty keep the default value false.
* When PGManager cancel proxy uid and pid,
* because we update the proxy map before, so we should refresh
* all of the runninglock disable flag always.
*/
POWER_HILOGD(FEATURE_RUNNING_LOCK, "Set lock enable, proxyMap_ is empty and forceRefresh is false");
lockInner->SetDisabled(false);
RunningLockType runninglockType = lockInner->GetRunningLockType();
if (proxyMap_.empty() ||
runninglockType == RunningLockType::RUNNINGLOCK_SCREEN ||
runninglockType == RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL) {
lockInner->SetProxied(false);
return;
}
const int32_t pid = lockInner->GetRunningLockPid();
const int32_t uid = lockInner->GetRunningLockUid();
bool matched = MatchProxyMap(pid, uid);
if (matched) {
// Matched the lock owner pid and uid, set disabled directly.
lockInner->SetDisabled(true);
POWER_HILOGD(FEATURE_RUNNING_LOCK, "Lock and ipc matched, uid = %{public}d, pid = %{public}d", uid, pid);
lockInner->SetProxied(true);
POWER_HILOGD(FEATURE_RUNNING_LOCK, "Runninglock and ipc matched, uid=%{public}d, pid=%{public}d", uid, pid);
return;
}
lockInner->SetDisabled(false);
lockInner->SetProxied(false);
}
void RunningLockMgr::LockReally(const sptr<IRemoteObject>& remoteObj,
void RunningLockMgr::LockInnerByProxy(const sptr<IRemoteObject>& remoteObj,
std::shared_ptr<RunningLockInner>& lockInner)
{
POWER_HILOGD(FEATURE_RUNNING_LOCK, "Start");
if (lockInner->GetReallyLocked()) {
POWER_HILOGD(FEATURE_RUNNING_LOCK, "lockInner->GetReallyLocked() is true, return");
if (lockInner->GetProxied() || !lockInner->GetNeedRestoreLock()) {
return;
}
if (lockInner->GetDisabled()) {
POWER_HILOGD(FEATURE_RUNNING_LOCK, "lockInner->GetDisabled() is true, return");
return;
}
runningLockAction_->Acquire(lockInner->GetRunningLockType());
lockInner->SetReallyLocked(true);
NotifyRunningLockChanged(remoteObj, lockInner, NOTIFY_RUNNINGLOCK_ADD);
POWER_HILOGD(FEATURE_RUNNING_LOCK, "Finish");
}
void RunningLockMgr::UnLockReally(const sptr<IRemoteObject>& remoteObj,
std::shared_ptr<RunningLockInner>& lockInner)
{
/**
* Case 1: Firstly PGManager ProxyRunningLock by uid and pid,
* secondly application lock by the same uid and
* pid, when call Lock() we matched the proxymap, and no really locked to kernel.
* So we don't need to unlocked to kernel.
* Case 2: Firstly application create the runninglock and call Lock(),
* and then PGManager Proxyed it,
* At this time, lock should be unlocked to kernel because we have locked to kernel for it.
*/
POWER_HILOGD(FEATURE_RUNNING_LOCK, "Start");
if (!lockInner->GetReallyLocked()) {
POWER_HILOGD(FEATURE_RUNNING_LOCK, "lockInner->GetReallyLocked() is false, return");
return;
}
// If disabled, unlock to the kernel.
if (!lockInner->GetDisabled()) {
POWER_HILOGD(FEATURE_RUNNING_LOCK, "lockInner->GetDisabled() is false, return");
return;
}
runningLockAction_->Release(lockInner->GetRunningLockType());
lockInner->SetReallyLocked(false);
NotifyRunningLockChanged(remoteObj, lockInner, NOTIFY_RUNNINGLOCK_REMOVE);
POWER_HILOGD(FEATURE_RUNNING_LOCK, "Finish");
int32_t lastTimeOutMs = lockInner->GetRunningLockTimeOutMs();
Lock(remoteObj, lastTimeOutMs);
lockInner->SetNeedRestoreLock(false);
return;
}
void RunningLockMgr::ProxyRunningLockInner(bool proxyLock)
void RunningLockMgr::UnlockInnerByProxy(const sptr<IRemoteObject>& remoteObj,
std::shared_ptr<RunningLockInner>& lockInner)
{
if (proxyLock) {
for (auto& it : runningLocks_) {
SetRunningLockDisableFlag(it.second);
UnLockReally(it.first, it.second);
if (!lockInner->GetProxied()) {
return;
}
auto lockParam = lockInner->GetRunningLockParam();
if (IsSceneRunningLockType(lockInner->GetRunningLockType())) {
if (runningLockAction_->Unlock(lockParam) == RUNNINGLOCK_SUCCESS) {
lockInner->SetNeedRestoreLock(true);
NotifyRunningLockChanged(remoteObj, lockInner, NOTIFY_RUNNINGLOCK_REMOVE);
}
} else {
for (auto& it : runningLocks_) {
SetRunningLockDisableFlag(it.second, true);
LockReally(it.first, it.second);
return;
}
if (!lockInner->GetEnabled() ||
lockInner->GetRunningLockType() != RunningLockType::RUNNINGLOCK_BACKGROUND) {
return;
}
auto counterIter = lockCounters_.find(lockParam.type);
if (counterIter == lockCounters_.end()) {
return;
}
RemoveAndPostUnlockTask(remoteObj);
counterIter->second->Decrease(remoteObj, lockInner);
lockInner->SetEnabled(false);
lockInner->SetNeedRestoreLock(true);
return;
}
void RunningLockMgr::SetRunningLockProxyInner(bool isProxied)
{
for (auto& it : runningLocks_) {
SetRunningLockProxiedFlag(it.second);
if (isProxied) {
UnlockInnerByProxy(it.first, it.second);
} else {
LockInnerByProxy(it.first, it.second);
}
}
}
void RunningLockMgr::ProxyRunningLock(bool proxyLock, pid_t uid, pid_t pid)
bool RunningLockMgr::SetRunningLockProxy(bool isProxied, pid_t pid, pid_t uid)
{
POWER_HILOGD(FEATURE_RUNNING_LOCK, "proxyLock = %{public}d, uid = %{public}d, pid = %{public}d",
proxyLock, uid, pid);
if (pid < VALID_PID_LIMIT) {
POWER_HILOGI(FEATURE_RUNNING_LOCK, "Set runninglock failed, pid=%{public}d is invalid", pid);
return false;
}
auto it = proxyMap_.find(uid);
if (proxyLock) {
// PGManager insert proxy info.
if (isProxied) {
if (it == proxyMap_.end()) {
unordered_set<pid_t> pidset({pid});
proxyMap_.emplace(uid, pidset);
POWER_HILOGD(FEATURE_RUNNING_LOCK, "Emplace first proxyMap_ {uid = %{public}d, pid = %{public}d}",
uid, pid);
} else {
if (pid == INVALID_PID) {
// Insert uid, pid = -1,remove other pid
proxyMap_.erase(uid);
unordered_set<pid_t> pidset({pid});
proxyMap_.emplace(uid, pidset);
POWER_HILOGD(FEATURE_RUNNING_LOCK, "Re-emplace proxyMap_ {uid = %{public}d, pid = %{public}d}",
uid, pid);
} else {
auto& pidset = it->second;
pidset.insert(pid);
POWER_HILOGD(FEATURE_RUNNING_LOCK, "Insert proxyMap_ {uid = %{public}d, pid = %{public}d}", uid, pid);
}
}
// 1. Set the matched runninglock inner disabled flag.
} else {
if (it == proxyMap_.end()) {
// No insert proxy info, nothing to erase.
POWER_HILOGD(FEATURE_RUNNING_LOCK, "No uid in proxyMap_, uid = %{public}d", uid);
return;
}
// 1. Clear the runninglock inner disabled flag 2.removed from proxyMap_
if (pid == INVALID_PID) {
proxyMap_.erase(uid);
POWER_HILOGD(FEATURE_RUNNING_LOCK, "pid = -1, erase from proxyMap_, uid = %{public}d", uid);
POWER_HILOGD(FEATURE_RUNNING_LOCK,
"Add first runninglock proxy, uid=%{public}d, pid=%{public}d", uid, pid);
} else {
auto& pidset = it->second;
pidset.erase(pid);
POWER_HILOGD(FEATURE_RUNNING_LOCK, "Erase from proxyMap_, uid = %{public}d, pid = %{public}d", uid, pid);
if (pidset.size() == 0) {
proxyMap_.erase(uid);
POWER_HILOGD(FEATURE_RUNNING_LOCK, "Pidset is empty erase uid from proxyMap_, uid = %{public}d", uid);
}
pidset.insert(pid);
POWER_HILOGD(FEATURE_RUNNING_LOCK, "Insert runninglock proxy, uid=%{public}d, pid=%{public}d", uid, pid);
}
} else {
if (it == proxyMap_.end()) {
POWER_HILOGD(FEATURE_RUNNING_LOCK, "Runninglock proxy map is empty");
return false;
}
auto& pidset = it->second;
pidset.erase(pid);
POWER_HILOGD(FEATURE_RUNNING_LOCK, "Erase runninglock proxy, uid=%{public}d, pid=%{public}d", uid, pid);
if (pidset.size() == 0) {
proxyMap_.erase(uid);
POWER_HILOGD(FEATURE_RUNNING_LOCK,
"Pid list is empty and erase uid from rrunninglock map, uid=%{public}d", uid);
}
}
ProxyRunningLockInner(proxyLock);
SetRunningLockProxyInner(isProxied);
return true;
}
void RunningLockMgr::NotifyHiView(RunningLockChangedType changeType,
@ -637,14 +614,15 @@ void RunningLockMgr::NotifyHiView(RunningLockChangedType changeType,
{
int32_t pid = lockInner.GetRunningLockPid();
int32_t uid = lockInner.GetRunningLockUid();
bool state = lockInner.GetDisabled();
bool state = lockInner.GetEnabled();
bool isProxied = lockInner.GetProxied();
int32_t type = static_cast <int32_t>(lockInner.GetRunningLockType());
string name = lockInner.GetRunningLockName();
const int logLevel = 2;
const string &tag = runninglockNotifyStr_.at(changeType);
HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::POWER, "RUNNINGLOCK",
HiviewDFX::HiSysEvent::EventType::STATISTIC,
"PID", pid, "UID", uid, "STATE", state, "TYPE", type, "NAME", name,
"PID", pid, "UID", uid, "STATE", state, "PROXY_STATE", isProxied, "TYPE", type, "NAME", name,
"LOG_LEVEL", logLevel, "TAG", tag, "MESSAGE", msg);
POWER_HILOGD(FEATURE_RUNNING_LOCK, "pid = %{public}d, uid= %{public}d, tag=%{public}s, msg=%{public}s",
pid, uid, tag.c_str(), msg.c_str());
@ -731,7 +709,7 @@ void RunningLockMgr::DumpInfo(std::string& result)
.append(" name=").append(lockParam.name)
.append(" uid=").append(ToString(lockInner->GetRunningLockUid()))
.append(" pid=").append(ToString(lockInner->GetRunningLockPid()))
.append(" disable=").append(ToString(lockInner->GetDisabled()))
.append(" enable=").append(ToString(lockInner->GetEnabled()))
.append("\n");
}

View File

@ -44,7 +44,7 @@ public:
bool IsRunningLockTypeSupported(RunningLockType type);
bool Lock(const sptr<IRemoteObject>& remoteObj, int32_t timeOutMs);
bool UnLock(const sptr<IRemoteObject>& remoteObj);
bool ProxyRunningLock(bool proxyLock, pid_t uid, pid_t pid);
bool SetRunningLockProxy(bool isProxied, pid_t pid, pid_t uid);
bool IsUsed(const sptr<IRemoteObject>& remoteObj);
PowerErrors SuspendDevice(int64_t callTimeMs,
SuspendDeviceType reason = SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION,

View File

@ -25,10 +25,8 @@ class MockLockAction : public IRunningLockAction {
public:
MockLockAction() = default;
virtual ~MockLockAction() = default;
MOCK_METHOD1(Acquire, void(RunningLockType type));
MOCK_METHOD1(Release, void(RunningLockType type));
MOCK_METHOD1(Lock, void(const RunningLockParam& param));
MOCK_METHOD1(Unlock, void(const RunningLockParam& param));
MOCK_METHOD1(Lock, int32_t(const RunningLockParam& param));
MOCK_METHOD1(Unlock, int32_t(const RunningLockParam& param));
};
} // namespace PowerMgr
} // namespace OHOS

View File

@ -101,7 +101,7 @@ HWTEST_F(PowerMockObjectTest, PowerMockObjectTest002, TestSize.Level2)
EXPECT_EQ(sptrProxy->SetDeviceMode(mode), PowerErrors::ERR_CONNECTION_FAIL);
auto ret = sptrProxy->GetDeviceMode();
EXPECT_TRUE(ret == mode);
EXPECT_FALSE(sptrProxy->ProxyRunningLock(true, uid, pid));
EXPECT_FALSE(sptrProxy->SetRunningLockProxy(true, pid, uid));
EXPECT_FALSE(sptrProxy->IsRunningLockTypeSupported(RunningLockType::RUNNINGLOCK_BACKGROUND));
EXPECT_FALSE(sptrProxy->OverrideScreenOffTime(200));
EXPECT_FALSE(sptrProxy->RestoreScreenOffTime());

View File

@ -137,7 +137,7 @@ HWTEST_F(MockParcelTest, PowerMockParcelTest004, TestSize.Level2)
RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN);
sptrProxy->CreateRunningLock(token, info);
sptrProxy->ReleaseRunningLock(token);
sptrProxy->ProxyRunningLock(true, uid, pid);
sptrProxy->SetRunningLockProxy(true, pid, uid);
EXPECT_FALSE(sptrProxy->IsRunningLockTypeSupported(RunningLockType::RUNNINGLOCK_BUTT));
sptrProxy->Lock(token, 0);
sptrProxy->UnLock(token);

View File

@ -111,7 +111,7 @@ HWTEST_F(MockPeerTest, MockPeerTest001, TestSize.Level2)
EXPECT_FALSE(sptrProxy->Lock(token, 0));
EXPECT_FALSE(sptrProxy->UnLock(token));
EXPECT_FALSE(sptrProxy->IsUsed(token));
EXPECT_FALSE(sptrProxy->ProxyRunningLock(true, uid, pid));
EXPECT_FALSE(sptrProxy->SetRunningLockProxy(true, pid, uid));
}
/**

View File

@ -69,7 +69,7 @@ HWTEST_F(PowerMockProxyTest, PowerMockProxyTest001, TestSize.Level2)
EXPECT_FALSE(sptrProxy->Lock(token, 0));
EXPECT_FALSE(sptrProxy->UnLock(token));
EXPECT_FALSE(sptrProxy->IsUsed(token));
EXPECT_FALSE(sptrProxy->ProxyRunningLock(true, uid, pid));
EXPECT_FALSE(sptrProxy->SetRunningLockProxy(true, pid, uid));
}
/**

View File

@ -90,11 +90,13 @@ HWTEST_F (RunningLockMockTest, RunningLockMockTest001, TestSize.Level2)
EXPECT_EQ(param.type, RunningLockType::RUNNINGLOCK_BACKGROUND);
EXPECT_EQ(param.timeoutMs, RUNNINGLOCKPARAM_TIMEOUTMS_DEF);
lockActionCount++;
return RUNNINGLOCK_SUCCESS;
});
EXPECT_CALL(*g_lockAction, Unlock(_)).WillRepeatedly([&](const RunningLockParam& param) {
EXPECT_EQ(param.name, RUNNINGLOCK_BACKGROUND_NAME);
EXPECT_EQ(param.type, RunningLockType::RUNNINGLOCK_BACKGROUND);
unlockActionCount++;
return RUNNINGLOCK_SUCCESS;
});
sptr<IRemoteObject> runninglockToken = new RunningLockTokenStub();
@ -142,11 +144,13 @@ HWTEST_F (RunningLockMockTest, RunningLockMockTest002, TestSize.Level2)
EXPECT_EQ(param.type, RunningLockType::RUNNINGLOCK_BACKGROUND);
EXPECT_EQ(param.timeoutMs, RUNNINGLOCKPARAM_TIMEOUTMS_DEF);
lockActionCount++;
return RUNNINGLOCK_SUCCESS;
});
EXPECT_CALL(*g_lockAction, Unlock(_)).WillRepeatedly([&](const RunningLockParam& param) {
EXPECT_EQ(param.name, RUNNINGLOCK_BACKGROUND_NAME);
EXPECT_EQ(param.type, RunningLockType::RUNNINGLOCK_BACKGROUND);
unlockActionCount++;
return RUNNINGLOCK_SUCCESS;
});
sptr<IRemoteObject> runninglockToken = new RunningLockTokenStub();
@ -190,12 +194,14 @@ HWTEST_F (RunningLockMockTest, RunningLockMockTest003, TestSize.Level2)
EXPECT_EQ(param.type, runninglockInfo.type);
EXPECT_EQ(param.timeoutMs, timeoutMs);
lockActionCount++;
return RUNNINGLOCK_SUCCESS;
});
EXPECT_CALL(*g_lockAction, Unlock(_)).WillRepeatedly([&](const RunningLockParam& param) {
RunningLockInfo runninglockInfo = GetRunningLockInfo(param.type);
EXPECT_EQ(param.name, runninglockInfo.name);
EXPECT_EQ(param.type, runninglockInfo.type);
unlockActionCount++;
return RUNNINGLOCK_SUCCESS;
});
sptr<IRemoteObject> phoneToken = new RunningLockTokenStub();
@ -245,12 +251,14 @@ HWTEST_F (RunningLockMockTest, RunningLockMockTest004, TestSize.Level2)
EXPECT_EQ(param.type, runninglockInfo.type);
EXPECT_EQ(param.timeoutMs, timeoutMs);
lockActionCount++;
return RUNNINGLOCK_SUCCESS;
});
EXPECT_CALL(*g_lockAction, Unlock(_)).WillRepeatedly([&](const RunningLockParam& param) {
RunningLockInfo runninglockInfo = GetRunningLockInfo(param.type);
EXPECT_EQ(param.name, runninglockInfo.name);
EXPECT_EQ(param.type, runninglockInfo.type);
unlockActionCount++;
return RUNNINGLOCK_SUCCESS;
});
sptr<IRemoteObject> audioToken = new RunningLockTokenStub();
@ -300,12 +308,14 @@ HWTEST_F (RunningLockMockTest, RunningLockMockTest005, TestSize.Level2)
EXPECT_EQ(param.type, runninglockInfo.type);
EXPECT_EQ(param.timeoutMs, timeoutMs);
lockActionCount++;
return RUNNINGLOCK_SUCCESS;
});
EXPECT_CALL(*g_lockAction, Unlock(_)).WillRepeatedly([&](const RunningLockParam& param) {
auto runninglockInfo = GetRunningLockInfo(param.type);
EXPECT_EQ(param.name, runninglockInfo.name);
EXPECT_EQ(param.type, runninglockInfo.type);
unlockActionCount++;
return RUNNINGLOCK_SUCCESS;
});
sptr<IRemoteObject> naviToken = new RunningLockTokenStub();
@ -360,12 +370,14 @@ HWTEST_F (RunningLockMockTest, RunningLockMockTest006, TestSize.Level2)
EXPECT_CALL(*g_lockAction, Lock(_)).WillRepeatedly([&](const RunningLockParam& param) {
lockActionCount++;
return RUNNINGLOCK_SUCCESS;
});
EXPECT_CALL(*g_lockAction, Unlock(_)).WillRepeatedly([&](const RunningLockParam& param) {
RunningLockInfo runninglockInfo = GetRunningLockInfo(param.type);
EXPECT_EQ(param.name, runninglockInfo.name);
EXPECT_EQ(param.type, runninglockInfo.type);
unlockActionCount++;
return RUNNINGLOCK_SUCCESS;
});
sptr<IRemoteObject> phoneToken = new RunningLockTokenStub();
@ -422,12 +434,14 @@ HWTEST_F (RunningLockMockTest, RunningLockMockTest007, TestSize.Level2)
EXPECT_CALL(*g_lockAction, Lock(_)).WillRepeatedly([&](const RunningLockParam& param) {
lockActionCount++;
return RUNNINGLOCK_SUCCESS;
});
EXPECT_CALL(*g_lockAction, Unlock(_)).WillRepeatedly([&](const RunningLockParam& param) {
RunningLockInfo runninglockInfo = GetRunningLockInfo(param.type);
EXPECT_EQ(param.name, runninglockInfo.name);
EXPECT_EQ(param.type, runninglockInfo.type);
unlockActionCount++;
return RUNNINGLOCK_SUCCESS;
});
sptr<IRemoteObject> sportToken = new RunningLockTokenStub();
@ -449,4 +463,289 @@ HWTEST_F (RunningLockMockTest, RunningLockMockTest007, TestSize.Level2)
EXPECT_EQ(lockActionCount, 3);
EXPECT_EQ(unlockActionCount, 3);
}
/**
* @tc.name: RunningLockMockTest008
* @tc.desc: Test SetRunningLockProxy function, test Background runninglock
* @tc.type: FUNC
* @tc.require: issueI6S0YY
*/
HWTEST_F (RunningLockMockTest, RunningLockMockTest008, TestSize.Level2)
{
ASSERT_NE(g_powerService, nullptr);
ASSERT_NE(g_lockAction, nullptr);
RunningLockInfo runninglockInfo("RunningLockMockBackground8.1", RunningLockType::RUNNINGLOCK_BACKGROUND);
auto runningLockMgr = g_powerService->GetRunningLockMgr();
uint32_t lockActionCount = 0;
uint32_t unlockActionCount = 0;
pid_t curUid = getuid();
pid_t curPid = getpid();
EXPECT_CALL(*g_lockAction, Lock(_)).WillRepeatedly([&](const RunningLockParam& param) {
EXPECT_EQ(param.name, RUNNINGLOCK_BACKGROUND_NAME);
EXPECT_EQ(param.type, runninglockInfo.type);
EXPECT_EQ(param.timeoutMs, RUNNINGLOCKPARAM_TIMEOUTMS_DEF);
lockActionCount++;
return RUNNINGLOCK_SUCCESS;
});
EXPECT_CALL(*g_lockAction, Unlock(_)).WillRepeatedly([&](const RunningLockParam& param) {
EXPECT_EQ(param.name, RUNNINGLOCK_BACKGROUND_NAME);
EXPECT_EQ(param.type, runninglockInfo.type);
unlockActionCount++;
return RUNNINGLOCK_SUCCESS;
});
sptr<IRemoteObject> runninglockToken = new RunningLockTokenStub();
EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(runninglockToken, runninglockInfo));
auto backgroundLock = runningLockMgr->GetRunningLockInner(runninglockToken);
ASSERT_NE(backgroundLock, nullptr);
g_powerService->Lock(runninglockToken, RUNNINGLOCKPARAM_TIMEOUTMS_DEF);
EXPECT_TRUE(backgroundLock->GetEnabled());
EXPECT_FALSE(backgroundLock->GetNeedRestoreLock());
EXPECT_EQ(lockActionCount, 1);
EXPECT_TRUE(g_powerService->SetRunningLockProxy(true, curPid, curUid));
EXPECT_FALSE(backgroundLock->GetEnabled());
EXPECT_TRUE(backgroundLock->GetNeedRestoreLock());
EXPECT_EQ(unlockActionCount, 1);
EXPECT_TRUE(g_powerService->SetRunningLockProxy(false, curPid, curUid));
EXPECT_TRUE(backgroundLock->GetEnabled());
EXPECT_FALSE(backgroundLock->GetNeedRestoreLock());
EXPECT_EQ(lockActionCount, 2);
g_powerService->ReleaseRunningLock(runninglockToken);
EXPECT_EQ(unlockActionCount, 2);
}
/**
* @tc.name: RunningLockMockTest009
* @tc.desc: Test SetRunningLockProxy function, test Scene runninglock
* @tc.type: FUNC
* @tc.require: issueI6S0YY
*/
HWTEST_F (RunningLockMockTest, RunningLockMockTest009, TestSize.Level2)
{
ASSERT_NE(g_powerService, nullptr);
ASSERT_NE(g_lockAction, nullptr);
RunningLockInfo runninglockPhone("RunningLockMockPhone9.1", RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE);
RunningLockInfo runninglockNotify("RunningLockMockNotify9.1", RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION);
uint32_t lockActionCount = 0;
uint32_t unlockActionCount = 0;
pid_t curUid = getuid();
pid_t curPid = getpid();
auto GetRunningLockInfo = [&](RunningLockType type) {
if (type == RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE) {
return runninglockPhone;
}
return runninglockNotify;
};
EXPECT_CALL(*g_lockAction, Lock(_)).WillRepeatedly([&](const RunningLockParam& param) {
RunningLockInfo runninglockInfo = GetRunningLockInfo(param.type);
EXPECT_EQ(param.name, runninglockInfo.name);
EXPECT_EQ(param.type, runninglockInfo.type);
EXPECT_EQ(param.timeoutMs, RUNNINGLOCKPARAM_TIMEOUTMS_DEF);
lockActionCount++;
return RUNNINGLOCK_SUCCESS;
});
EXPECT_CALL(*g_lockAction, Unlock(_)).WillRepeatedly([&](const RunningLockParam& param) {
RunningLockInfo runninglockInfo = GetRunningLockInfo(param.type);
EXPECT_EQ(param.name, runninglockInfo.name);
EXPECT_EQ(param.type, runninglockInfo.type);
unlockActionCount++;
return RUNNINGLOCK_NOT_SUPPORT;
});
sptr<IRemoteObject> phoneToken = new RunningLockTokenStub();
sptr<IRemoteObject> notifyToken = new RunningLockTokenStub();
EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(phoneToken, runninglockPhone));
EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(notifyToken, runninglockNotify));
EXPECT_TRUE(g_powerService->SetRunningLockProxy(true, curPid, curUid));
EXPECT_EQ(unlockActionCount, 2);
g_powerService->Lock(phoneToken, RUNNINGLOCKPARAM_TIMEOUTMS_DEF);
g_powerService->Lock(notifyToken, RUNNINGLOCKPARAM_TIMEOUTMS_DEF);
g_powerService->UnLock(phoneToken);
g_powerService->UnLock(notifyToken);
EXPECT_EQ(lockActionCount, 0);
EXPECT_EQ(unlockActionCount, 2);
EXPECT_TRUE(g_powerService->SetRunningLockProxy(false, curPid, curUid));
EXPECT_EQ(lockActionCount, 0);
g_powerService->ReleaseRunningLock(phoneToken);
g_powerService->ReleaseRunningLock(notifyToken);
}
/**
* @tc.name: RunningLockMockTest010
* @tc.desc: Test SetRunningLockProxy function, test Scene runninglock
* @tc.type: FUNC
* @tc.require: issueI6S0YY
*/
HWTEST_F (RunningLockMockTest, RunningLockMockTest010, TestSize.Level2)
{
ASSERT_NE(g_powerService, nullptr);
ASSERT_NE(g_lockAction, nullptr);
RunningLockInfo runninglockAudio("RunningLockMockAudio10.1", RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO);
RunningLockInfo runninglockSport("RunningLockMockSport10.1", RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT);
auto runningLockMgr = g_powerService->GetRunningLockMgr();
uint32_t lockActionCount = 0;
uint32_t unlockActionCount = 0;
auto GetRunningLockInfo = [&](RunningLockType type) {
return type == RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO ? runninglockAudio : runninglockSport;
};
EXPECT_CALL(*g_lockAction, Lock(_)).WillRepeatedly([&](const RunningLockParam& param) {
RunningLockInfo runninglockInfo = GetRunningLockInfo(param.type);
EXPECT_EQ(param.name, runninglockInfo.name);
EXPECT_EQ(param.type, runninglockInfo.type);
EXPECT_EQ(param.timeoutMs, RUNNINGLOCKPARAM_TIMEOUTMS_DEF);
lockActionCount++;
return RUNNINGLOCK_SUCCESS;
});
EXPECT_CALL(*g_lockAction, Unlock(_)).WillRepeatedly([&](const RunningLockParam& param) {
RunningLockInfo runninglockInfo = GetRunningLockInfo(param.type);
EXPECT_EQ(param.name, runninglockInfo.name);
EXPECT_EQ(param.type, runninglockInfo.type);
unlockActionCount++;
return RUNNINGLOCK_SUCCESS;
});
sptr<IRemoteObject> audioToken = new RunningLockTokenStub();
sptr<IRemoteObject> sportToken = new RunningLockTokenStub();
EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(audioToken, runninglockAudio));
EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(sportToken, runninglockSport));
auto audioLock = runningLockMgr->GetRunningLockInner(audioToken);
auto sportLock = runningLockMgr->GetRunningLockInner(sportToken);
g_powerService->Lock(audioToken, RUNNINGLOCKPARAM_TIMEOUTMS_DEF);
g_powerService->Lock(sportToken, RUNNINGLOCKPARAM_TIMEOUTMS_DEF);
EXPECT_EQ(lockActionCount, 2);
EXPECT_FALSE(audioLock->GetNeedRestoreLock());
EXPECT_FALSE(sportLock->GetNeedRestoreLock());
EXPECT_TRUE(g_powerService->SetRunningLockProxy(true, getpid(), getuid()));
EXPECT_EQ(unlockActionCount, 2);
EXPECT_TRUE(audioLock->GetNeedRestoreLock());
EXPECT_TRUE(sportLock->GetNeedRestoreLock());
EXPECT_TRUE(g_powerService->SetRunningLockProxy(false, getpid(), getuid()));
EXPECT_EQ(lockActionCount, 4);
EXPECT_FALSE(audioLock->GetNeedRestoreLock());
EXPECT_FALSE(sportLock->GetNeedRestoreLock());
g_powerService->ReleaseRunningLock(audioToken);
g_powerService->ReleaseRunningLock(sportToken);
}
/**
* @tc.name: RunningLockMockTest011
* @tc.desc: Test SetRunningLockProxy function, test Scene runninglock, HDI unlock() return RUNNINGLOCK_NOT_SUPPORT
* @tc.type: FUNC
* @tc.require: issueI6S0YY
*/
HWTEST_F (RunningLockMockTest, RunningLockMockTest011, TestSize.Level2)
{
ASSERT_NE(g_powerService, nullptr);
ASSERT_NE(g_lockAction, nullptr);
RunningLockInfo runninglockNavi("RunningLockMockNavi11.1", RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION);
auto runningLockMgr = g_powerService->GetRunningLockMgr();
uint32_t lockActionCount = 0;
uint32_t unlockActionCount = 0;
int32_t timeoutMs = 100;
pid_t curUid = getuid();
pid_t curPid = getpid();
EXPECT_CALL(*g_lockAction, Lock(_)).WillRepeatedly([&](const RunningLockParam& param) {
EXPECT_EQ(param.name, runninglockNavi.name);
EXPECT_EQ(param.type, runninglockNavi.type);
EXPECT_EQ(param.timeoutMs, timeoutMs);
lockActionCount++;
return RUNNINGLOCK_SUCCESS;
});
EXPECT_CALL(*g_lockAction, Unlock(_)).WillRepeatedly([&](const RunningLockParam& param) {
EXPECT_EQ(param.name, runninglockNavi.name);
EXPECT_EQ(param.type, runninglockNavi.type);
unlockActionCount++;
return RUNNINGLOCK_NOT_SUPPORT;
});
sptr<IRemoteObject> naviToken = new RunningLockTokenStub();
EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(naviToken, runninglockNavi));
auto naviLock = runningLockMgr->GetRunningLockInner(naviToken);
g_powerService->Lock(naviToken, timeoutMs);
EXPECT_EQ(lockActionCount, 1);
EXPECT_FALSE(naviLock->GetNeedRestoreLock());
EXPECT_TRUE(g_powerService->SetRunningLockProxy(true, curPid, curUid));
EXPECT_EQ(unlockActionCount, 1);
EXPECT_FALSE(naviLock->GetNeedRestoreLock());
EXPECT_TRUE(g_powerService->SetRunningLockProxy(false, curPid, curUid));
EXPECT_EQ(lockActionCount, 1);
g_powerService->ReleaseRunningLock(naviToken);
}
/**
* @tc.name: RunningLockMockTest012
* @tc.desc: Test SetRunningLockProxy function, test Scene runninglock, HDI unlock() return RUNNINGLOCK_SUCCESS
* @tc.type: FUNC
* @tc.require: issueI6S0YY
*/
HWTEST_F (RunningLockMockTest, RunningLockMockTest012, TestSize.Level2)
{
ASSERT_NE(g_powerService, nullptr);
ASSERT_NE(g_lockAction, nullptr);
RunningLockInfo runninglockTask("RunningLockMockTask12.1", RunningLockType::RUNNINGLOCK_BACKGROUND_TASK);
auto runningLockMgr = g_powerService->GetRunningLockMgr();
uint32_t lockActionCount = 0;
uint32_t unlockActionCount = 0;
int32_t timeoutMs = 100;
pid_t curUid = getuid();
pid_t curPid = getpid();
EXPECT_CALL(*g_lockAction, Lock(_)).WillRepeatedly([&](const RunningLockParam& param) {
EXPECT_EQ(param.name, runninglockTask.name);
EXPECT_EQ(param.type, runninglockTask.type);
EXPECT_EQ(param.timeoutMs, timeoutMs);
lockActionCount++;
return RUNNINGLOCK_SUCCESS;
});
EXPECT_CALL(*g_lockAction, Unlock(_)).WillRepeatedly([&](const RunningLockParam& param) {
EXPECT_EQ(param.name, runninglockTask.name);
EXPECT_EQ(param.type, runninglockTask.type);
unlockActionCount++;
return RUNNINGLOCK_SUCCESS;
});
sptr<IRemoteObject> taskToken = new RunningLockTokenStub();
EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(taskToken, runninglockTask));
auto taskLock = runningLockMgr->GetRunningLockInner(taskToken);
g_powerService->Lock(taskToken, timeoutMs);
EXPECT_EQ(lockActionCount, 1);
EXPECT_FALSE(taskLock->GetNeedRestoreLock());
EXPECT_TRUE(g_powerService->SetRunningLockProxy(true, curPid, curUid));
EXPECT_EQ(unlockActionCount, 1);
EXPECT_TRUE(taskLock->GetNeedRestoreLock());
EXPECT_TRUE(g_powerService->SetRunningLockProxy(false, curPid, curUid));
EXPECT_EQ(lockActionCount, 2);
g_powerService->ReleaseRunningLock(taskToken);
}
}

View File

@ -122,13 +122,13 @@ HWTEST_F (RunningLockNativeTest, RunningLockNative002, TestSize.Level0)
runningLockMgr->CheckOverTime();
runningLockMgr->CheckOverTime();
runningLockMgr->ProxyRunningLock(false, uid, pid);
runningLockMgr->ProxyRunningLock(true, uid, pid);
runningLockMgr->ProxyRunningLock(true, uid, pid);
runningLockMgr->ProxyRunningLock(true, UID, UNPID);
runningLockMgr->ProxyRunningLock(true, UID, UNPID);
runningLockMgr->ProxyRunningLock(false, uid, pid);
runningLockMgr->ProxyRunningLock(false, UID, UNPID);
runningLockMgr->SetRunningLockProxy(false, pid, uid);
runningLockMgr->SetRunningLockProxy(true, pid, uid);
runningLockMgr->SetRunningLockProxy(true, pid, uid);
runningLockMgr->SetRunningLockProxy(true, UNPID, UID);
runningLockMgr->SetRunningLockProxy(true, UNPID, UID);
runningLockMgr->SetRunningLockProxy(false, pid, uid);
runningLockMgr->SetRunningLockProxy(false, UNPID, UID);
runningLockMgr->UnLock(remoteObj);
EXPECT_FALSE(runningLockMgr->ReleaseLock(remoteObj));
POWER_HILOGI(LABEL_TEST, "RunningLockNative002 end");

View File

@ -23,16 +23,18 @@ using namespace testing::ext;
using namespace OHOS::PowerMgr;
using namespace OHOS;
using namespace std;
sptr<PowerMgrService> RunningLockTest::pmsTest_ = nullptr;
std::shared_ptr<RunningLockMgr> RunningLockTest::runningLockMgr_ = nullptr;
namespace {
constexpr int32_t US_PER_MS = 1000;
}
namespace {
/**
* @tc.name: RunningLockInnerKit000
* @tc.name: RunningLockTest001
* @tc.desc: Test RunningLockInnerKit function, connect PowerMgrService and call member function.
* @tc.type: FUNC
*/
HWTEST_F (RunningLockTest, RunningLockInnerKit000, TestSize.Level0)
HWTEST_F (RunningLockTest, RunningLockTest001, TestSize.Level0)
{
auto& powerMgrClient = PowerMgrClient::GetInstance();
auto runningLock1 = powerMgrClient.CreateRunningLock("runninglock1", RunningLockType::RUNNINGLOCK_SCREEN);
@ -48,11 +50,11 @@ HWTEST_F (RunningLockTest, RunningLockInnerKit000, TestSize.Level0)
}
/**
* @tc.name: RunningLockInnerKit002
* @tc.name: RunningLockTest002
* @tc.desc: Test RunningLockInnerKit function, timeout lock.
* @tc.type: FUNC
*/
HWTEST_F (RunningLockTest, RunningLockInnerKit002, TestSize.Level1)
HWTEST_F (RunningLockTest, RunningLockTest002, TestSize.Level1)
{
auto& powerMgrClient = PowerMgrClient::GetInstance();
auto runningLock1 = powerMgrClient.CreateRunningLock("runninglock1", RunningLockType::RUNNINGLOCK_SCREEN);
@ -78,11 +80,11 @@ HWTEST_F (RunningLockTest, RunningLockInnerKit002, TestSize.Level1)
}
/**
* @tc.name: RunningLockInnerKit004
* @tc.name: RunningLockTest003
* @tc.desc: Test RunningLockInnerKit function, timeout lock.
* @tc.type: FUNC
*/
HWTEST_F (RunningLockTest, RunningLockInnerKit004, TestSize.Level1)
HWTEST_F (RunningLockTest, RunningLockTest003, TestSize.Level1)
{
if (false) {
auto& powerMgrClient = PowerMgrClient::GetInstance();
@ -119,12 +121,12 @@ HWTEST_F (RunningLockTest, RunningLockInnerKit004, TestSize.Level1)
}
/**
* @tc.name: RunningLockInnerKit005
* @tc.name: RunningLockTest004
* @tc.desc: Test CreateRunningLock function.
* @tc.type: FUNC
* @tc.require: issueI6NWQD
*/
HWTEST_F (RunningLockTest, RunningLockInnerKit005, TestSize.Level1)
HWTEST_F (RunningLockTest, RunningLockTest004, TestSize.Level1)
{
auto& powerMgrClient = PowerMgrClient::GetInstance();
std::shared_ptr<RunningLock> runningLock = powerMgrClient.CreateRunningLock(
@ -155,195 +157,156 @@ HWTEST_F (RunningLockTest, RunningLockInnerKit005, TestSize.Level1)
EXPECT_EQ(runningLock, nullptr);
}
#ifdef IPC_AVAILABLE
/**
* @tc.name: RunningLockInnerKit003
* @tc.desc: Test RunningLockInnerKit function, timeout lock.
* @tc.name: RunningLockTest005
* @tc.desc: Test SetRunningLockProxy function.
* @tc.type: FUNC
* @tc.require: issueI6S0YY
*/
HWTEST_F (RunningLockTest, RunningLockInnerKit003, TestSize.Level0)
HWTEST_F (RunningLockTest, SetRunningLockProxy, TestSize.Level1)
{
auto& powerMgrClient = PowerMgrClient::GetInstance();
auto runningLock1 = powerMgrClient.CreateRunningLock("runninglock2", RunningLockType::RUNNINGLOCK_SCREEN);
ASSERT_TRUE(runningLock1 != nullptr);
ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false";
// after 8ms unlock
runningLock1->Lock(30);
runningLock1->Lock(80);
POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit004 1");
usleep(50000);
ASSERT_TRUE(runningLock1->IsUsed()) << "runningLock1->IsUsed() != true";
usleep(50000);
ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false";
// no unlock
POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit004 2");
runningLock1->Lock(2);
runningLock1->Lock(3);
runningLock1->Lock();
POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit004 3");
usleep(8000);
ASSERT_TRUE(runningLock1->IsUsed()) << "runningLock1->IsUsed() != true";
// after 3ms unlock
runningLock1->Lock(30);
POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit004 4");
usleep(50000);
ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false";
runningLock1->Lock(5);
runningLock1->UnLock();
ASSERT_TRUE(!runningLock1->IsUsed()) << "runningLock1->IsUsed() != false";
POWER_HILOGD(LABEL_TEST, "PowerMgrUnitTest::RunningLockInnerKit004 5");
std::shared_ptr<RunningLock> runningLock = powerMgrClient.CreateRunningLock(
"background.test005", RunningLockType::RUNNINGLOCK_BACKGROUND);
EXPECT_NE(runningLock, nullptr);
pid_t curUid = getuid();
pid_t curPid = getpid();
EXPECT_TRUE(powerMgrClient.SetRunningLockProxy(true, curPid, curUid));
runningLock->Lock();
EXPECT_FALSE(runningLock->IsUsed());
EXPECT_TRUE(powerMgrClient.SetRunningLockProxy(false, curPid, curUid));
}
/**
* @tc.name: RunningLockMgr001
* @tc.desc: Test RunningLockMgr function, connect PowerMgrService and call member function.
* @tc.name: RunningLockTest006
* @tc.desc: Test SetRunningLockProxy function.
* @tc.type: FUNC
* @tc.require: issueI6S0YY
*/
HWTEST_F (RunningLockTest, RunningLockMgr001, TestSize.Level0)
HWTEST_F (RunningLockTest, RunningLockTest006, TestSize.Level1)
{
sptr<IRemoteObject> token = new RunningLockTokenStub();
ASSERT_TRUE(token != nullptr);
sptr<IRemoteObject> token2 = new RunningLockTokenStub();
ASSERT_TRUE(token2 != nullptr);
RunningLockInfo runningLockInfo1 {"runninglockTest1", RunningLockType::RUNNINGLOCK_SCREEN};
UserIPCInfo userIPCinfo {IPCSkeleton::GetCallingUid(), IPCSkeleton::GetCallingPid()};
{
runningLockMgr_->Lock(token, runningLockInfo1, userIPCinfo);
TestRunningLockInnerExisit(token, runningLockInfo1);
ASSERT_TRUE(1 == runningLockMgr_->GetRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
ASSERT_TRUE(0 == runningLockMgr_->GetRunningLockNum(RunningLockType::RUNNINGLOCK_BACKGROUND));
}
RunningLockInfo runningLockInfo2 {"runninglockTest2", RunningLockType::RUNNINGLOCK_BACKGROUND};
{
runningLockMgr_->Lock(token2, runningLockInfo2, userIPCinfo);
TestRunningLockInnerExisit(token2, runningLockInfo2);
ASSERT_TRUE(1 == runningLockMgr_->GetRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
ASSERT_TRUE(1 == runningLockMgr_->GetRunningLockNum(RunningLockType::RUNNINGLOCK_BACKGROUND));
}
{
runningLockMgr_->UnLock(token);
TestRunningLockInnerNoExisit(token);
ASSERT_TRUE(0 == runningLockMgr_->GetRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
ASSERT_TRUE(1 == runningLockMgr_->GetRunningLockNum(RunningLockType::RUNNINGLOCK_BACKGROUND));
}
{
runningLockMgr_->UnLock(token2);
TestRunningLockInnerNoExisit(token2);
ASSERT_TRUE(0 == runningLockMgr_->GetRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
ASSERT_TRUE(0 == runningLockMgr_->GetRunningLockNum(RunningLockType::RUNNINGLOCK_BACKGROUND));
}
POWER_HILOGD(LABEL_TEST, "RunningLockTest::RunningLockMgr001 end");
auto& powerMgrClient = PowerMgrClient::GetInstance();
std::shared_ptr<RunningLock> runningLock = powerMgrClient.CreateRunningLock(
"background.test006", RunningLockType::RUNNINGLOCK_BACKGROUND);
EXPECT_NE(runningLock, nullptr);
pid_t curUid = getuid();
pid_t curPid = getpid();
runningLock->Lock();
EXPECT_TRUE(runningLock->IsUsed());
EXPECT_TRUE(powerMgrClient.SetRunningLockProxy(true, curPid, curUid));
EXPECT_FALSE(runningLock->IsUsed());
EXPECT_TRUE(powerMgrClient.SetRunningLockProxy(false, curPid, curUid));
EXPECT_TRUE(runningLock->IsUsed());
}
/**
* @tc.name: RunningLockMgr003
* @tc.desc: Test RunningLockMgr Proxy function.
* @tc.name: RunningLockTest007
* @tc.desc: Test SetRunningLockProxy function.
* @tc.type: FUNC
* @tc.require: issueI6S0YY
*/
HWTEST_F (RunningLockTest, RunningLockMgr003, TestSize.Level0)
HWTEST_F (RunningLockTest, RunningLockTest007, TestSize.Level1)
{
sptr<IRemoteObject> token1 = new RunningLockTokenStub();
sptr<IRemoteObject> token2 = new RunningLockTokenStub();
RunningLockInfo runningLockInfo1 {"runninglocktest1", RunningLockType::RUNNINGLOCK_SCREEN};
RunningLockInfo runningLockInfo2 {"runninglocktest2", RunningLockType::RUNNINGLOCK_SCREEN};
UserIPCInfo userIPCinfo1 {1, 1};
UserIPCInfo userIPCinfo2 {2, 2};
runningLockMgr_->Lock(token1, runningLockInfo1, userIPCinfo1);
runningLockMgr_->Lock(token2, runningLockInfo2, userIPCinfo2);
TestRunningLockInnerExisit(token1, runningLockInfo1);
TestRunningLockInnerExisit(token2, runningLockInfo2);
ASSERT_TRUE(2 == runningLockMgr_->GetRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
ASSERT_TRUE(2 == runningLockMgr_->GetValidRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
auto& proxymap = runningLockMgr_->GetRunningLockProxyMap();
ASSERT_TRUE(proxymap.empty());
{
// proxy by userIPCinfo1, lockinner1 disabled
runningLockMgr_->ProxyRunningLock(true, userIPCinfo1.uid, userIPCinfo1.pid);
auto lockInner1 = runningLockMgr_->GetRunningLockInner(token1);
ASSERT_TRUE(!lockInner1->GetReallyLocked());
ASSERT_TRUE(lockInner1->GetDisabled());
ASSERT_TRUE(!proxymap.empty() && (proxymap.count(userIPCinfo1.uid) > 0));
auto& powerMgrClient = PowerMgrClient::GetInstance();
std::shared_ptr<RunningLock> runningLock = powerMgrClient.CreateRunningLock(
"background.test007", RunningLockType::RUNNINGLOCK_BACKGROUND);
EXPECT_NE(runningLock, nullptr);
runningLockMgr_->ProxyRunningLock(true, userIPCinfo2.uid, userIPCinfo2.pid);
auto lockInner2 = runningLockMgr_->GetRunningLockInner(token2);
ASSERT_TRUE(!lockInner2->GetReallyLocked());
ASSERT_TRUE(lockInner2->GetDisabled());
ASSERT_TRUE(!proxymap.empty() && (proxymap.count(userIPCinfo2.uid) > 0));
ASSERT_TRUE(2 == runningLockMgr_->GetRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
ASSERT_TRUE(0 == runningLockMgr_->GetValidRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
pid_t curUid = getuid();
pid_t curPid = getpid();
int32_t timeoutMs = 50;
runningLockMgr_->ProxyRunningLock(false, userIPCinfo1.uid, userIPCinfo1.pid);
ASSERT_TRUE(lockInner1->GetReallyLocked());
ASSERT_TRUE(!lockInner1->GetDisabled());
ASSERT_TRUE(!proxymap.empty() && (proxymap.count(userIPCinfo1.uid) == 0));
runningLock->Lock(timeoutMs);
EXPECT_TRUE(runningLock->IsUsed());
runningLockMgr_->ProxyRunningLock(false, userIPCinfo2.uid, userIPCinfo2.pid);
ASSERT_TRUE(lockInner2->GetReallyLocked());
ASSERT_TRUE(!lockInner2->GetDisabled());
ASSERT_TRUE(proxymap.empty());
ASSERT_TRUE(2 == runningLockMgr_->GetRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
ASSERT_TRUE(2 == runningLockMgr_->GetValidRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
}
runningLockMgr_->UnLock(token1);
runningLockMgr_->UnLock(token2);
POWER_HILOGD(LABEL_TEST, "RunningLockTest::RunningLockMgr003 end");
EXPECT_TRUE(powerMgrClient.SetRunningLockProxy(true, curPid, curUid));
EXPECT_FALSE(runningLock->IsUsed());
usleep(timeoutMs * US_PER_MS);
EXPECT_TRUE(powerMgrClient.SetRunningLockProxy(false, curPid, curUid));
EXPECT_TRUE(runningLock->IsUsed());
usleep(timeoutMs / 2 * US_PER_MS);
EXPECT_TRUE(runningLock->IsUsed());
usleep(timeoutMs * US_PER_MS);
EXPECT_FALSE(runningLock->IsUsed());
}
/**
* @tc.name: RunningLockMgr004
* @tc.desc: Test RunningLockMgr Proxy function.
* @tc.name: RunningLockTest008
* @tc.desc: Test SetRunningLockProxy function.
* @tc.type: FUNC
* @tc.require: issueI6S0YY
*/
HWTEST_F (RunningLockTest, RunningLockMgr004, TestSize.Level0)
HWTEST_F (RunningLockTest, RunningLockTest008, TestSize.Level1)
{
sptr<IRemoteObject> token3 = new RunningLockTokenStub();
sptr<IRemoteObject> token4 = new RunningLockTokenStub();
RunningLockInfo runningLockInfo3 {"runninglocktest3", RunningLockType::RUNNINGLOCK_SCREEN};
RunningLockInfo runningLockInfo4 {"runninglocktest4", RunningLockType::RUNNINGLOCK_SCREEN};
UserIPCInfo userIPCinfo3 {3, 3};
UserIPCInfo userIPCinfo4 {3, 4};
runningLockMgr_->Lock(token3, runningLockInfo3, userIPCinfo3);
runningLockMgr_->Lock(token4, runningLockInfo4, userIPCinfo4);
TestRunningLockInnerExisit(token3, runningLockInfo3);
TestRunningLockInnerExisit(token4, runningLockInfo4);
ASSERT_TRUE(2 == runningLockMgr_->GetValidRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
auto& proxymap = runningLockMgr_->GetRunningLockProxyMap();
ASSERT_TRUE(proxymap.empty());
{
// lockinner3 and lockinner4 have same pid, save uid key, pid set {uid3,{pid3, pid4}}
runningLockMgr_->ProxyRunningLock(true, userIPCinfo3.uid, userIPCinfo3.pid);
runningLockMgr_->ProxyRunningLock(true, userIPCinfo4.uid, userIPCinfo4.pid);
auto lockInner3 = runningLockMgr_->GetRunningLockInner(token3);
ASSERT_TRUE(!lockInner3->GetReallyLocked());
ASSERT_TRUE(lockInner3->GetDisabled());
ASSERT_TRUE(!proxymap.empty() && (proxymap.count(userIPCinfo3.uid) > 0));
{
auto it = proxymap.find(userIPCinfo3.uid);
ASSERT_TRUE(it != proxymap.end());
auto& pidset = it->second;
ASSERT_TRUE(pidset.count(userIPCinfo3.pid) == 1);
}
auto lockInner4 = runningLockMgr_->GetRunningLockInner(token4);
ASSERT_TRUE(lockInner4 != nullptr);
ASSERT_TRUE(!lockInner4->GetReallyLocked());
ASSERT_TRUE(lockInner4->GetDisabled());
ASSERT_TRUE(!proxymap.empty() && (proxymap.count(userIPCinfo4.uid) > 0));
{
auto it = proxymap.find(userIPCinfo4.uid);
ASSERT_TRUE(it != proxymap.end());
auto& pidset = it->second;
ASSERT_TRUE(pidset.count(userIPCinfo4.pid) == 1);
}
ASSERT_TRUE(0 == runningLockMgr_->GetValidRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
runningLockMgr_->ProxyRunningLock(false, userIPCinfo3.uid, INVALID_PID);
ASSERT_TRUE(proxymap.empty());
ASSERT_TRUE(lockInner3->GetReallyLocked() && !lockInner3->GetDisabled());
ASSERT_TRUE(lockInner4->GetReallyLocked() && !lockInner4->GetDisabled());
ASSERT_TRUE(2 == runningLockMgr_->GetValidRunningLockNum(RunningLockType::RUNNINGLOCK_SCREEN));
}
runningLockMgr_->UnLock(token3);
runningLockMgr_->UnLock(token4);
POWER_HILOGD(LABEL_TEST, "RunningLockTest::RunningLockMgr004 end");
auto& powerMgrClient = PowerMgrClient::GetInstance();
pid_t curUid = getuid();
pid_t curPid = getpid();
EXPECT_TRUE(powerMgrClient.SetRunningLockProxy(true, curPid, curUid));
std::shared_ptr<RunningLock> runningLock = powerMgrClient.CreateRunningLock(
"background.test008", RunningLockType::RUNNINGLOCK_BACKGROUND);
EXPECT_NE(runningLock, nullptr);
runningLock->Lock();
EXPECT_FALSE(runningLock->IsUsed());
EXPECT_TRUE(powerMgrClient.SetRunningLockProxy(false, curPid, curUid));
EXPECT_FALSE(runningLock->IsUsed());
}
/**
* @tc.name: RunningLockTest009
* @tc.desc: Test SetRunningLockProxy function.
* @tc.type: FUNC
* @tc.require: issueI6S0YY
*/
HWTEST_F (RunningLockTest, RunningLockTest009, TestSize.Level1)
{
auto& powerMgrClient = PowerMgrClient::GetInstance();
std::shared_ptr<RunningLock> screenRunningLock = powerMgrClient.CreateRunningLock(
"screen.test009", RunningLockType::RUNNINGLOCK_SCREEN);
EXPECT_NE(screenRunningLock, nullptr);
std::shared_ptr<RunningLock> proximityRunningLock = powerMgrClient.CreateRunningLock(
"proximity.test009", RunningLockType::RUNNINGLOCK_SCREEN);
EXPECT_NE(proximityRunningLock, nullptr);
pid_t curUid = getuid();
pid_t curPid = getpid();
EXPECT_TRUE(powerMgrClient.SetRunningLockProxy(true, curPid, curUid));
screenRunningLock->Lock();
EXPECT_TRUE(screenRunningLock->IsUsed());
screenRunningLock->UnLock();
EXPECT_FALSE(screenRunningLock->IsUsed());
proximityRunningLock->Lock();
EXPECT_TRUE(proximityRunningLock->IsUsed());
proximityRunningLock->UnLock();
EXPECT_FALSE(proximityRunningLock->IsUsed());
EXPECT_TRUE(powerMgrClient.SetRunningLockProxy(false, curPid, curUid));
}
/**
* @tc.name: RunningLockTest010
* @tc.desc: Test SetRunningLockProxy function, pid is invalid
* @tc.type: FUNC
* @tc.require: issueI6S0YY
*/
HWTEST_F (RunningLockTest, RunningLockTest010, TestSize.Level1)
{
pid_t curUid = 1;
pid_t curPid = -1;
auto& powerMgrClient = PowerMgrClient::GetInstance();
EXPECT_FALSE(powerMgrClient.SetRunningLockProxy(true, curPid, curUid));
EXPECT_FALSE(powerMgrClient.SetRunningLockProxy(false, curPid, curUid));
}
#endif // IPC_AVAILABLE
}

View File

@ -89,7 +89,7 @@ HWTEST_F(PowerMgrServiceMockParcelTest, PowerMgrServiceMockParcelTest001, TestSi
EXPECT_FALSE(g_powerMgrServiceProxy->Lock(token, timeOutMs));
EXPECT_FALSE(g_powerMgrServiceProxy->UnLock(token));
EXPECT_FALSE(g_powerMgrServiceProxy->IsUsed(token));
EXPECT_FALSE(g_powerMgrServiceProxy->ProxyRunningLock(true, uid, pid));
EXPECT_FALSE(g_powerMgrServiceProxy->SetRunningLockProxy(true, pid, uid));
}
/**

View File

@ -280,7 +280,7 @@ HWTEST_F(PowerMgrServiceNativeTest, PowerMgrServiceNativeTest010, TestSize.Level
pid_t pid = 0;
auto error = g_powerMgrServiceProxy->CreateRunningLock(token, runningLockInfo);
EXPECT_TRUE(error == PowerErrors::ERR_OK);
EXPECT_TRUE(g_powerMgrServiceProxy->ProxyRunningLock(true, uid, pid));
EXPECT_FALSE(g_powerMgrServiceProxy->SetRunningLockProxy(true, pid, uid));
g_powerMgrServiceProxy->OverrideScreenOffTime(time);
g_powerMgrServiceProxy->Lock(token, timeOutMs);
EXPECT_EQ(g_powerMgrServiceProxy->IsUsed(token), true);

View File

@ -188,7 +188,7 @@ bool PowerMgrServiceTestProxy::IsUsed(const sptr<IRemoteObject>& remoteObj)
return used;
}
bool PowerMgrServiceTestProxy::ProxyRunningLock(bool proxyLock, pid_t uid, pid_t pid)
bool PowerMgrServiceTestProxy::SetRunningLockProxy(bool isProxied, pid_t pid, pid_t uid)
{
RETURN_IF_WITH_RET(stub_ == nullptr, false);
@ -201,9 +201,9 @@ bool PowerMgrServiceTestProxy::ProxyRunningLock(bool proxyLock, pid_t uid, pid_t
return false;
}
data.WriteBool(proxyLock);
data.WriteInt32(uid);
data.WriteBool(isProxied);
data.WriteInt32(pid);
data.WriteInt32(uid);
int ret = stub_->OnRemoteRequest(static_cast<int>(IPowerMgr::PROXY_RUNNINGLOCK),
data, reply, option);
@ -211,7 +211,9 @@ bool PowerMgrServiceTestProxy::ProxyRunningLock(bool proxyLock, pid_t uid, pid_t
POWER_HILOGE(FEATURE_RUNNING_LOCK, "SendRequest is failed, ret: %{public}d", ret);
return false;
}
return true;
bool succ = false;
READ_PARCEL_WITH_RET(reply, Bool, succ, false);
return succ;
}
PowerErrors PowerMgrServiceTestProxy::RebootDevice(const std::string& reason)

View File

@ -49,7 +49,7 @@ public:
virtual bool IsRunningLockTypeSupported(RunningLockType type) override;
virtual bool Lock(const sptr<IRemoteObject>& remoteObj, int32_t timeOutMs) override;
virtual bool UnLock(const sptr<IRemoteObject>& remoteObj) override;
virtual bool ProxyRunningLock(bool proxyLock, pid_t uid, pid_t pid) override;
virtual bool SetRunningLockProxy(bool isProxied, pid_t pid, pid_t uid) override;
virtual bool IsUsed(const sptr<IRemoteObject>& remoteObj) override;
// Use for PowerStateMachine
virtual PowerErrors SuspendDevice(int64_t callTimeMs, SuspendDeviceType reason, bool suspendImmed) override;

View File

@ -39,7 +39,7 @@ private:
int32_t GetStateStub(MessageParcel& reply);
int32_t IsScreeOnStub(MessageParcel& reply);
int32_t ForceSuspendDeviceStub(MessageParcel& data, MessageParcel& reply);
int32_t ProxyRunningLockStub(MessageParcel& data);
int32_t SetRunningLockProxyStub(MessageParcel& data, MessageParcel& reply);
int32_t CreateRunningLockStub(MessageParcel& data, MessageParcel& reply);
int32_t ReleaseRunningLockStub(MessageParcel& data);
int32_t IsRunningLockTypeSupportedStub(MessageParcel& data, MessageParcel& reply);

View File

@ -184,7 +184,7 @@ bool PowerMgrProxy::IsUsed(const sptr<IRemoteObject>& remoteObj)
return used;
}
bool PowerMgrProxy::ProxyRunningLock(bool proxyLock, pid_t uid, pid_t pid)
bool PowerMgrProxy::SetRunningLockProxy(bool isProxied, pid_t pid, pid_t uid)
{
sptr<IRemoteObject> remote = Remote();
RETURN_IF_WITH_RET(remote == nullptr, false);
@ -198,16 +198,18 @@ bool PowerMgrProxy::ProxyRunningLock(bool proxyLock, pid_t uid, pid_t pid)
return false;
}
WRITE_PARCEL_WITH_RET(data, Bool, proxyLock, false);
WRITE_PARCEL_WITH_RET(data, Int32, uid, false);
WRITE_PARCEL_WITH_RET(data, Bool, isProxied, false);
WRITE_PARCEL_WITH_RET(data, Int32, pid, false);
WRITE_PARCEL_WITH_RET(data, Int32, uid, false);
int ret = remote->SendRequest(static_cast<int>(IPowerMgr::PROXY_RUNNINGLOCK),
data, reply, option);
if (ret != ERR_OK) {
POWER_HILOGE(FEATURE_RUNNING_LOCK, "SendRequest is failed, ret: %{public}d", ret);
return false;
}
return true;
bool succ = false;
READ_PARCEL_WITH_RET(reply, Bool, succ, false);
return succ;
}
PowerErrors PowerMgrProxy::RebootDevice(const std::string& reason)

View File

@ -95,7 +95,7 @@ int PowerMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
ret = UnLockStub(data);
break;
case static_cast<int>(IPowerMgr::PROXY_RUNNINGLOCK):
ret = ProxyRunningLockStub(data);
ret = SetRunningLockProxyStub(data, reply);
break;
case static_cast<int>(IPowerMgr::RUNNINGLOCK_ISUSED):
ret = IsUsedStub(data, reply);
@ -196,15 +196,16 @@ int32_t PowerMgrStub::IsUsedStub(MessageParcel& data, MessageParcel& reply)
return ERR_OK;
}
int32_t PowerMgrStub::ProxyRunningLockStub(MessageParcel& data)
int32_t PowerMgrStub::SetRunningLockProxyStub(MessageParcel& data, MessageParcel& reply)
{
bool proxyLock = false;
bool isProxied = false;
pid_t uid;
pid_t pid;
READ_PARCEL_WITH_RET(data, Bool, proxyLock, E_READ_PARCEL_ERROR);
READ_PARCEL_WITH_RET(data, Int32, uid, E_READ_PARCEL_ERROR);
READ_PARCEL_WITH_RET(data, Bool, isProxied, E_READ_PARCEL_ERROR);
READ_PARCEL_WITH_RET(data, Int32, pid, E_READ_PARCEL_ERROR);
ProxyRunningLock(proxyLock, uid, pid);
READ_PARCEL_WITH_RET(data, Int32, uid, E_READ_PARCEL_ERROR);
bool ret = SetRunningLockProxy(isProxied, pid, uid);
WRITE_PARCEL_WITH_RET(reply, Bool, ret, E_WRITE_PARCEL_ERROR);
return ERR_OK;
}

View File

@ -164,6 +164,20 @@ static void RegisterShutdownCallback(const uint8_t* data)
g_powerMgrClient.UnRegisterShutdownCallback(callback);
}
static void SetRunningLockProxy(const uint8_t* data)
{
int32_t type[1];
int32_t idSize = 4;
if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) {
return;
}
bool isProxied = static_cast <bool>(type[0]);
pid_t pid = static_cast <pid_t>(type[0]);
pid_t uid = static_cast <pid_t>(type[0]);
g_powerMgrClient.SetRunningLockProxy(isProxied, pid, uid);
}
static void Lock(const uint8_t* data)
{
int32_t type[1];
@ -313,6 +327,9 @@ bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
case ApiNumber::NUM_TWENTY_ONE:
RegisterShutdownCallback(data);
break;
case ApiNumber::NUM_TWENTY_TWO:
SetRunningLockProxy(data);
break;
default:
break;
}

View File

@ -43,6 +43,7 @@ enum class ApiNumber {
NUM_NINETEEN,
NUM_TWENTY,
NUM_TWENTY_ONE,
NUM_TWENTY_TWO,
NUM_END
};

View File

@ -25,10 +25,8 @@ class MockLockAction : public IRunningLockAction {
public:
MockLockAction() = default;
virtual ~MockLockAction() = default;
MOCK_METHOD1(Acquire, void(RunningLockType type));
MOCK_METHOD1(Release, void(RunningLockType type));
MOCK_METHOD1(Lock, void(const RunningLockParam& param));
MOCK_METHOD1(Unlock, void(const RunningLockParam& param));
MOCK_METHOD1(Lock, int32_t(const RunningLockParam& param));
MOCK_METHOD1(Unlock, int32_t(const RunningLockParam& param));
};
} // namespace PowerMgr
} // namespace OHOS