!990 优化getlastLocation的耗时

Merge pull request !990 from smilebear/master
This commit is contained in:
openharmony_ci 2024-11-21 12:15:24 +00:00 committed by Gitee
commit 0015f3dab5
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
8 changed files with 97 additions and 35 deletions

View File

@ -131,10 +131,12 @@ bool CommonUtils::GetCurrentUserId(int &userId)
std::vector<int> activeIds;
int ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeIds);
if (ret != 0) {
userId = DEFAULT_USERID;
LBSLOGI(COMMON_UTILS, "GetCurrentUserId failed ret:%{public}d", ret);
return false;
}
if (activeIds.empty()) {
userId = DEFAULT_USERID;
LBSLOGE(COMMON_UTILS, "QueryActiveOsAccountIds activeIds empty");
return false;
}
@ -412,23 +414,29 @@ std::string CommonUtils::GenerateUuid()
bool CommonUtils::CheckAppForUser(int32_t uid, std::string& bundleName)
{
if (HookUtils::ExecuteHookWhenCheckAppForUser(bundleName)) {
return true;
}
int currentUserId = 0;
int userId = 0;
if (!GetCurrentUserId(currentUserId)) {
currentUserId = DEFAULT_USERID;
}
auto result = AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, userId);
if (result != ERR_OK) {
LBSLOGE(COMMON_UTILS, "CheckAppForUser GetOsAccountLocalIdFromUid fail");
return false;
return CommonUtils::CheckAppForUser(uid, currentUserId, bundleName);
}
bool CommonUtils::CheckAppForUser(int32_t uid, int32_t currentUserId, std::string& bundleName)
{
int userId = 0;
AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, userId);
if (userId == currentUserId) {
return true;
}
if (userId != 0 && userId != currentUserId) {
return false;
if (bundleName.length() == 0) {
if (!CommonUtils::GetBundleNameByUid(uid, bundleName)) {
LBSLOGD(REPORT_MANAGER, "Fail to Get bundle name: uid = %{public}d.", uid);
}
}
return true;
if (bundleName.length() >0 && HookUtils::ExecuteHookWhenCheckAppForUser(bundleName)) {
return true;
}
return false;
}
int64_t CommonUtils::GetSinceBootTime()
@ -443,16 +451,24 @@ int64_t CommonUtils::GetSinceBootTime()
}
}
bool CommonUtils::IsAppBelongCurrentAccount(AppIdentity &identity)
bool CommonUtils::IsAppBelongCurrentAccount(AppIdentity &identity, int32_t currentUserId)
{
if (PermissionManager::CheckIsSystemSa(identity.GetTokenId())) {
std::string bundleName = identity.GetBundleName();
if (CommonUtils::CheckAppForUser(identity.GetUid(), currentUserId, bundleName)) {
return true;
}
std::string bundleName = identity.GetBundleName();
if (CommonUtils::CheckAppForUser(identity.GetUid(), bundleName)) {
if (PermissionManager::CheckIsSystemSa(identity.GetTokenId())) {
return true;
}
return false;
}
bool CommonUtils::IsAppBelongCurrentAccount(AppIdentity &identity)
{
int currentUserId = 0;
if (!CommonUtils::GetCurrentUserId(currentUserId)) {
}
return CommonUtils::IsAppBelongCurrentAccount(identity, currentUserId);
}
} // namespace Location
} // namespace OHOS

View File

@ -180,8 +180,10 @@ public:
static bool GetEdmPolicy(std::string& name);
static std::string GenerateUuid();
static bool CheckAppForUser(int32_t uid, std::string& bundleName);
static bool CheckAppForUser(int32_t uid, int32_t currentUserId, std::string& bundleName);
static int64_t GetSinceBootTime();
static bool GetAllUserId(std::vector<int>& activeIds);
static bool IsAppBelongCurrentAccount(AppIdentity &identity, int32_t currentUserId);
static bool IsAppBelongCurrentAccount(AppIdentity &identity);
};

View File

@ -54,6 +54,9 @@ public:
bool UnregisterAppStateObserver();
bool IsAppInLocationContinuousTasks(pid_t uid, pid_t pid);
bool IsAppHasFormVisible(uint32_t tokenId, uint64_t tokenIdEx);
int32_t getCurrentUserId();
bool IsAppBackground(int uid, std::string bundleName);
void UpdateBackgroundAppStatues(int32_t uid, int32_t status);
private:
void StartLocator();
void StopLocator();
@ -114,6 +117,8 @@ private:
std::shared_ptr<std::list<std::shared_ptr<Request>>> requestsList_;
static std::mutex requestListMutex_;
static std::mutex locatorMutex_;
static std::mutex foregroundAppMutex_;
std::map<int32_t, int32_t> foregroundAppMap_;
sptr<AppExecFwk::IAppMgr> iAppMgr_ = nullptr;
sptr<AppStateChangeCallback> appStateObserver_ = nullptr;
};

View File

@ -46,8 +46,11 @@
namespace OHOS {
namespace Location {
const int BACKGROUNDAPP_STATUS = 4;
const int FOREGROUPAPP_STATUS = 2;
std::mutex LocatorBackgroundProxy::requestListMutex_;
std::mutex LocatorBackgroundProxy::locatorMutex_;
std::mutex LocatorBackgroundProxy::foregroundAppMutex_;
LocatorBackgroundProxy* LocatorBackgroundProxy::GetInstance()
{
static LocatorBackgroundProxy data;
@ -234,6 +237,11 @@ bool LocatorBackgroundProxy::IsCallbackInProxy(const sptr<ILocatorCallback>& cal
return false;
}
int32_t LocatorBackgroundProxy::getCurrentUserId()
{
return curUserId_;
}
int32_t LocatorBackgroundProxy::GetUserId(int32_t uid) const
{
int userId = 0;
@ -386,6 +394,30 @@ bool LocatorBackgroundProxy::IsAppBackground(std::string bundleName)
return true;
}
bool LocatorBackgroundProxy::IsAppBackground(int uid, std::string bundleName)
{
std::unique_lock lock(foregroundAppMutex_);
auto iter = foregroundAppMap_.find(uid);
if (iter == foregroundAppMap_.end()) {
return IsAppBackground(bundleName);
}
return false;
}
void LocatorBackgroundProxy::UpdateBackgroundAppStatues(int32_t uid, int32_t status)
{
std::unique_lock lock(foregroundAppMutex_);
if (status == FOREGROUPAPP_STATUS) {
foregroundAppMap_[uid] = status;
} else if (status == BACKGROUNDAPP_STATUS) {
auto iter = foregroundAppMap_.find(uid);
if (iter != foregroundAppMap_.end()) {
foregroundAppMap_.erase(iter);
}
}
LBSLOGD(REQUEST_MANAGER, "UpdateBackgroundApp uid = %{public}d, state = %{public}d", uid, status);
}
bool LocatorBackgroundProxy::RegisterAppStateObserver()
{
if (appStateObserver_ != nullptr) {
@ -482,6 +514,8 @@ void AppStateChangeCallback::OnForegroundApplicationChanged(const AppExecFwk::Ap
LBSLOGD(REQUEST_MANAGER,
"The state of App changed, uid = %{public}d, pid = %{public}d, state = %{public}d", uid, pid, state);
requestManager->HandlePowerSuspendChanged(pid, uid, state);
auto locatorBackkProxy = LocatorBackgroundProxy::GetInstance();
locatorBackkProxy->UpdateBackgroundAppStatues(uid, state);
}
} // namespace OHOS
} // namespace Location

View File

@ -35,6 +35,7 @@
#include "permission_manager.h"
#include "hook_utils.h"
#include "location_data_rdb_manager.h"
#include "locator_background_proxy.h"
namespace OHOS {
namespace Location {
@ -1204,7 +1205,8 @@ bool LocatorAbilityStub::CheckRequestAvailable(uint32_t code, AppIdentity &ident
if (IsStopAction(code)) {
return true;
}
if (CommonUtils::IsAppBelongCurrentAccount(identity)) {
int currentUserId = LocatorBackgroundProxy::GetInstance()->getCurrentUserId();
if (CommonUtils::IsAppBelongCurrentAccount(identity, currentUserId)) {
return true;
}
LBSLOGD(LOCATOR, "CheckRequestAvailable fail uid:%{public}d", identity.GetUid());
@ -1228,8 +1230,10 @@ int32_t LocatorAbilityStub::OnRemoteRequest(uint32_t code,
}
std::string bundleName = "";
if (!CommonUtils::GetBundleNameByUid(identity.GetUid(), bundleName)) {
LBSLOGD(LOCATOR, "Fail to Get bundle name: uid = %{public}d.", identity.GetUid());
if (static_cast<LocatorInterfaceCode>(code) != LocatorInterfaceCode::GET_CACHE_LOCATION) {
if (!CommonUtils::GetBundleNameByUid(identity.GetUid(), bundleName)) {
LBSLOGD(LOCATOR, "Fail to Get bundle name: uid = %{public}d.", identity.GetUid());
}
}
identity.SetBundleName(bundleName);
if (code != static_cast<uint32_t>(LocatorInterfaceCode::PROXY_PID_FOR_FREEZE)) {

View File

@ -231,7 +231,8 @@ std::unique_ptr<Location> ReportManager::GetPermittedLocation(const std::shared_
identity.SetUid(request->GetUid());
identity.SetTokenId(request->GetTokenId());
identity.SetBundleName(bundleName);
if (!CommonUtils::IsAppBelongCurrentAccount(identity)) {
int currentUserId = LocatorBackgroundProxy::GetInstance()->getCurrentUserId();
if (!CommonUtils::IsAppBelongCurrentAccount(identity, currentUserId)) {
//app is not in current user, not need to report
LBSLOGI(REPORT_MANAGER, "GetPermittedLocation uid: %{public}d CheckAppForUser fail", tokenId);
auto locationErrorCallback = request->GetLocationErrorCallBack();
@ -349,20 +350,19 @@ void ReportManager::UpdateLastLocation(const std::unique_ptr<Location>& location
std::unique_ptr<Location> ReportManager::GetLastLocation()
{
int currentUserId = 0;
if (CommonUtils::GetCurrentUserId(currentUserId)) {
std::unique_lock<std::mutex> lock(lastLocationMutex_);
auto iter = lastLocationsMap_.find(currentUserId);
if (iter == lastLocationsMap_.end()) {
return nullptr;
}
std::unique_ptr<Location> lastLocation = std::make_unique<Location>(*(iter->second));
if (CommonUtils::DoubleEqual(lastLocation->GetLatitude(), MIN_LATITUDE - 1)) {
return nullptr;
}
return lastLocation;
auto locatorBackgroundProxy = LocatorBackgroundProxy::GetInstance();
int currentUserId = locatorBackgroundProxy->getCurrentUserId();
LBSLOGI(LOCATOR_STANDARD, "GetCacheLocation GetLastLocation currentUserId = %{public}d ", currentUserId);
std::unique_lock<std::mutex> lock(lastLocationMutex_);
auto iter = lastLocationsMap_.find(currentUserId);
if (iter == lastLocationsMap_.end()) {
return nullptr;
}
return nullptr;
std::unique_ptr<Location> lastLocation = std::make_unique<Location>(*(iter->second));
if (CommonUtils::DoubleEqual(lastLocation->GetLatitude(), MIN_LATITUDE - 1)) {
return nullptr;
}
return lastLocation;
}
std::unique_ptr<Location> ReportManager::GetCacheLocation(const std::shared_ptr<Request>& request)
@ -481,7 +481,7 @@ void ReportManager::WriteNetWorkReportEvent(std::string abilityName, const std::
bool ReportManager::IsAppBackground(std::string bundleName, uint32_t tokenId, uint64_t tokenIdEx, pid_t uid, pid_t pid)
{
auto locatorBackgroundProxy = LocatorBackgroundProxy::GetInstance();
if (!locatorBackgroundProxy->IsAppBackground(bundleName)) {
if (!locatorBackgroundProxy->IsAppBackground(uid, bundleName)) {
return false;
}
if (locatorBackgroundProxy->IsAppHasFormVisible(tokenId, tokenIdEx)) {

View File

@ -461,7 +461,8 @@ bool RequestManager::IsRequestAvailable(std::shared_ptr<Request>& request)
AppIdentity identity;
identity.SetUid(request->GetUid());
identity.SetTokenId(request->GetTokenId());
if (!CommonUtils::IsAppBelongCurrentAccount(identity)) {
int currentUserId = LocatorBackgroundProxy::GetInstance()->getCurrentUserId();
if (!CommonUtils::IsAppBelongCurrentAccount(identity, currentUserId)) {
LBSLOGD(REPORT_MANAGER, "AddRequestToWorkRecord uid: %{public}d ,CheckAppIsCurrentUser fail",
request->GetUid());
return false;