update services/location_locator/locator/source/request_manager.cpp.

Signed-off-by: smilebear <245252081@qq.com>
This commit is contained in:
smilebear 2024-06-18 02:26:17 +00:00 committed by 李文龙
parent b3bd3c1047
commit a738b5263e
10 changed files with 125 additions and 47 deletions

View File

@ -128,7 +128,7 @@ bool CommonUtils::GetCurrentUserId(int &userId)
std::vector<int> activeIds;
int ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeIds);
if (ret != 0) {
LBSLOGE(COMMON_UTILS, "QueryActiveOsAccountIds failed ret:%{public}d", ret);
LBSLOGI(COMMON_UTILS, "GetCurrentUserId failed ret:%{public}d", ret);
return false;
}
if (activeIds.empty()) {
@ -402,14 +402,18 @@ std::string CommonUtils::GenerateUuid()
};
return ss.str();
}
bool CommonUtils::CheckAppForUser(int32_t uid)
{
int currentUserId = 0;
int userId = 0;
if (!GetCurrentUserId(currentUserId)) {
return true;
currentUserId = 100;
}
auto result = AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, userId);
if (result != ERR_OK) {
return false;
}
AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, userId);
if (userId != currentUserId) {
return false;
}

View File

@ -21,10 +21,11 @@
namespace OHOS {
namespace Location {
const int DEFAULT_USERID = 100;
const int DEFAULT_SWITCHMODE = 2;
const int UNKNOW_ERROR = -1;
const int MAX_SIZE = 100;
const char* LOCATION_SWITCH_MODE = "persist.location.switch_mode";
std::mutex LocationDataRdbManager::mutex_;
std::string LocationDataRdbManager::GetLocationDataUri(std::string key)
{
int userId = 0;
@ -97,6 +98,7 @@ int LocationDataRdbManager::GetSwitchMode()
{
char result[MAX_SIZE] = {0};
std::string value = "";
std::unique_lock<std::mutex> lock(mutex_);
auto res = GetParameter(LOCATION_SWITCH_MODE, "", result, MAX_SIZE);
if (res <= 0 || strlen(result) == 0) {
LBSLOGE(COMMON_UTILS, "%{public}s get para value failed, res: %{public}d", __func__, res);
@ -119,6 +121,7 @@ bool LocationDataRdbManager::SetSwitchMode(int value)
{
char valueArray[MAX_SIZE] = {0};
(void)sprintf_s(valueArray, sizeof(valueArray), "%d", value);
std::unique_lock<std::mutex> lock(mutex_);
int res = SetParameter(LOCATION_SWITCH_MODE, valueArray);
if (res < 0) {
LBSLOGE(COMMON_UTILS, "%{public}s failed, res: %{public}d", __func__, res);
@ -130,11 +133,10 @@ bool LocationDataRdbManager::SetSwitchMode(int value)
bool LocationDataRdbManager::ClearSwitchMode()
{
char valueArray[MAX_SIZE] = {0};
(void)sprintf_s(valueArray, sizeof(valueArray), "%s", "");
(void)sprintf_s(valueArray, sizeof(valueArray), "%d", DEFAULT_SWITCHMODE);
std::unique_lock<std::mutex> lock(mutex_);
int res = SetParameter(LOCATION_SWITCH_MODE, valueArray);
LBSLOGE(COMMON_UTILS, "ClearSwitchMode valueArray value: %{public}s", valueArray);
if (res < 0) {
LBSLOGE(COMMON_UTILS, "%{public}s failed, res: %{public}d", __func__, res);
return false;
}
return true;

View File

@ -17,6 +17,7 @@
#define LOCATION_DATA_RDB_MANAGER_H
#include <unistd.h>
#include <string>
#include <mutex>
#include "constant_definition.h"
@ -44,6 +45,8 @@ public:
static std::string GetLocationDataSecureUri(std::string key);
static bool SetLocationEnhanceStatus(int32_t state);
static bool GetLocationEnhanceStatus(int32_t& state);
private:
static std::mutex mutex_;
};
} // namespace Location
} // namespace OHOS

View File

@ -114,6 +114,8 @@ private:
LocatorInterfaceCode code, MessageParcel &data, MessageParcel &reply, AppIdentity &identity);
void ConstructLocatorHandleMap();
void ConstructGeocodeHandleMap();
bool IsStopAction(uint32_t code);
bool CheckRequestAvailable(uint32_t code, AppIdentity &identity);
void ConstructGnssHandleMap();
sptr<IRemoteObject::DeathRecipient> scanRecipient_ = new (std::nothrow) ScanCallbackDeathRecipient();
};

View File

@ -24,6 +24,7 @@
#include "i_locator_callback.h"
#include "location.h"
#include "request.h"
#include <mutex>
namespace OHOS {
namespace Location {
@ -49,9 +50,10 @@ public:
private:
struct timespec lastUpdateTime_;
double offsetRandom_;
Location lastLocation_;
std::map<int, std::shared_ptr<Location>> lastLocationsMap_;
Location cacheGnssLocation_;
Location cacheNlpLocation_;
std::mutex lastLocationMutex_;
std::unique_ptr<Location> ApproximatelyLocation(const std::unique_ptr<Location>& location);
bool ProcessRequestForReport(std::shared_ptr<Request>& request,
std::unique_ptr<std::list<std::shared_ptr<Request>>>& deadRequests,
@ -60,6 +62,7 @@ private:
const std::unique_ptr<Location>& location);
std::unique_ptr<Location> ExecuteReportProcess(std::shared_ptr<Request>& request,
std::unique_ptr<Location>& location, std::string abilityName);
void UpdateLastLocation(const std::unique_ptr<Location>& location);
};
} // namespace OHOS
} // namespace Location

View File

@ -57,6 +57,7 @@ private:
sptr<IRemoteObject> GetRemoteObject(std::string abilityName);
bool IsUidInProcessing(int32_t uid);
void UpdateUsingApproximatelyPermission(std::shared_ptr<Request> request);
void UpdateUsingApproximatelyStatus(std::shared_ptr<Request> request, bool isActive);
bool ActiveLocatingStrategies(const std::shared_ptr<Request>& request);
bool AddRequestToWorkRecord(std::string abilityName, std::shared_ptr<Request>& request,
std::shared_ptr<WorkRecord>& workRecord);

View File

@ -343,9 +343,9 @@ void LocatorBackgroundProxy::OnUserSwitch(int32_t userId)
{
UpdateListOnUserSwitch(userId);
LocationDataRdbManager::ClearSwitchMode();
auto requestManager = RequestManager::GetInstance();
if (requestManager != nullptr) {
requestManager->HandleRequest();
auto locatorAbility = LocatorAbility::GetInstance();
if (locatorAbility != nullptr) {
locatorAbility->ApplyRequests(1);
}
if (!requestsList_->empty()) {
StartLocator();
@ -359,11 +359,6 @@ void LocatorBackgroundProxy::OnUserRemove(int32_t userId)
{
// if user is removed, remove the requestList from the user in requestsMap
std::unique_lock lock(requestListMutex_);
LocationDataRdbManager::ClearSwitchMode();
auto requestManager = RequestManager::GetInstance();
if (requestManager != nullptr) {
requestManager->HandleRequest();
}
auto iter = requestsMap_->find(userId);
if (iter != requestsMap_->end()) {
requestsMap_->erase(iter);

View File

@ -1177,25 +1177,45 @@ int LocatorAbilityStub::PreReportLocationError(MessageParcel &data, MessageParce
return ERRCODE_SUCCESS;
}
bool LocatorAbilityStub::IsStopAction(uint32_t code)
{
if (code == static_cast<uint32_t>(LocatorInterfaceCode::UNREG_SWITCH_CALLBACK) ||
code == static_cast<uint32_t>(LocatorInterfaceCode::STOP_LOCATING) ||
code == static_cast<uint32_t>(LocatorInterfaceCode::STOP_LOCATING) ||
code == static_cast<uint32_t>(LocatorInterfaceCode::DISABLE_LOCATION_MOCK) ||
code == static_cast<uint32_t>(LocatorInterfaceCode::UNREG_LOCATION_ERROR) ||
code == static_cast<uint32_t>(LocatorInterfaceCode::UNREG_LOCATING_REQUIRED_DATA_CALLBACK)) {
return true;
}
return false;
}
bool LocatorAbilityStub::CheckRequestAvailable(uint32_t code, AppIdentity &identity)
{
if (IsStopAction(code)) {
return true;
}
if (PermissionManager::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
return true;
}
if (!CommonUtils::CheckAppForUser(identity.GetUid())) {
return false;
}
return true;
}
int32_t LocatorAbilityStub::OnRemoteRequest(uint32_t code,
MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
int ret = ERRCODE_SUCCESS;
pid_t callingPid = IPCSkeleton::GetCallingPid();
pid_t callingUid = IPCSkeleton::GetCallingUid();
uint32_t callingTokenId = IPCSkeleton::GetCallingTokenID();
uint64_t callingTokenIdEx = IPCSkeleton::GetCallingFullTokenID();
uint32_t callingFirstTokenid = IPCSkeleton::GetFirstTokenID();
AppIdentity identity;
identity.SetPid(callingPid);
identity.SetUid(callingUid);
identity.SetTokenId(callingTokenId);
identity.SetTokenIdEx(callingTokenIdEx);
identity.SetFirstTokenId(callingFirstTokenid);
identity.SetPid(IPCSkeleton::GetCallingPid());
identity.SetUid(IPCSkeleton::GetCallingUid());
identity.SetTokenId(IPCSkeleton::GetCallingTokenID());
identity.SetTokenIdEx(IPCSkeleton::GetCallingFullTokenID());
identity.SetFirstTokenId(IPCSkeleton::GetFirstTokenID());
std::string bundleName = "";
if (!CommonUtils::GetBundleNameByUid(callingUid, bundleName)) {
LBSLOGD(LOCATOR, "Fail to Get bundle name: uid = %{public}d.", callingUid);
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)) {
@ -1205,7 +1225,6 @@ int32_t LocatorAbilityStub::OnRemoteRequest(uint32_t code,
std::to_string(CommonUtils::GetCurrentTimeStamp()).c_str());
}
std::string callingIdentity = IPCSkeleton::ResetCallingIdentity();
if (data.ReadInterfaceToken() != GetDescriptor()) {
LBSLOGE(LOCATOR, "invalid token.");
IPCSkeleton::SetCallingIdentity(callingIdentity);
@ -1213,7 +1232,10 @@ int32_t LocatorAbilityStub::OnRemoteRequest(uint32_t code,
return ERRCODE_SERVICE_UNAVAILABLE;
}
RemoveUnloadTask(code);
if (!CheckRequestAvailable(code, identity)) {
reply.WriteInt32(ERRCODE_PERMISSION_DENIED);
return ERRCODE_PERMISSION_DENIED;
}
auto handleFunc = locatorHandleMap_.find(static_cast<LocatorInterfaceCode>(code));
if (handleFunc != locatorHandleMap_.end() && handleFunc->second != nullptr) {
auto memberFunc = handleFunc->second;

View File

@ -210,8 +210,6 @@ std::unique_ptr<Location> ReportManager::GetPermittedLocation(const std::shared_
}
if (!PermissionManager::CheckSystemPermission(tokenId, tokenIdEx) &&
!CommonUtils::CheckAppForUser(uid)) {
PrivacyKit::StopUsingPermission(tokenId, ACCESS_APPROXIMATELY_LOCATION);
LBSLOGE(REPORT_MANAGER, "GetPermittedLocation uid: %{public}d CheckAppForUser fail", tokenId);
return nullptr;
}
std::unique_ptr<Location> finalLocation = std::make_unique<Location>(*location);
@ -306,25 +304,42 @@ void ReportManager::UpdateCacheLocation(const std::unique_ptr<Location>& locatio
if (abilityName == GNSS_ABILITY) {
if (HookUtils::CheckGnssLocationValidity(location)) {
cacheGnssLocation_ = *location;
lastLocation_ = *location;
UpdateLastLocation(location);
}
} else if (abilityName == NETWORK_ABILITY &&
location->GetLocationSourceType() != LocationSourceType::INDOOR_TYPE) {
cacheNlpLocation_ = *location;
lastLocation_ = *location;
UpdateLastLocation(location);
} else {
lastLocation_ = *location;
UpdateLastLocation(location);
}
}
void ReportManager::UpdateLastLocation(const std::unique_ptr<Location>& location)
{
int currentUserId = 0;
if (CommonUtils::GetCurrentUserId(currentUserId)) {
std::unique_lock<std::mutex> lock(lastLocationMutex_);
lastLocationsMap_.insert(std::make_pair(currentUserId, std::make_shared<Location>(*location)));
}
}
std::unique_ptr<Location> ReportManager::GetLastLocation()
{
auto lastLocation = std::make_unique<Location>(lastLocation_);
if (CommonUtils::DoubleEqual(lastLocation->GetLatitude(), MIN_LATITUDE - 1)) {
LBSLOGE(REPORT_MANAGER, "%{public}s no valid cache location", __func__);
return nullptr;
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;
}
return lastLocation;
return nullptr;
}
std::unique_ptr<Location> ReportManager::GetCacheLocation(const std::shared_ptr<Request>& request)

View File

@ -111,6 +111,31 @@ void RequestManager::UpdateUsingApproximatelyPermission(std::shared_ptr<Request>
}
}
void RequestManager::UpdateUsingApproximatelyStatus(std::shared_ptr<Request> request, bool isActive)
{
std::unique_lock<std::mutex> lock(permissionRecordMutex_, std::defer_lock);
lock.lock();
if (request == nullptr) {
LBSLOGE(REQUEST_MANAGER, "request is null");
lock.unlock();
return;
}
uint32_t callingTokenId = request->GetTokenId();
if (isActive) {
if (!request->GetApproximatelyPermState()) {
PrivacyKit::StartUsingPermission(callingTokenId, ACCESS_APPROXIMATELY_LOCATION);
request->SetApproximatelyPermState(true);
}
} else {
if (request->GetApproximatelyPermState()) {
PrivacyKit::StopUsingPermission(callingTokenId, ACCESS_APPROXIMATELY_LOCATION);
request->SetApproximatelyPermState(false);
}
}
lock.unlock();
}
void RequestManager::HandleStartLocating(std::shared_ptr<Request> request)
{
auto locatorAbility = LocatorAbility::GetInstance();
@ -360,9 +385,10 @@ void RequestManager::HandleRequest(std::string abilityName, std::list<std::share
for (auto iter = list.begin(); iter != list.end(); iter++) {
auto request = *iter;
if (!AddRequestToWorkRecord(abilityName, request, workRecord)) {
WriteLocationInnerEvent(REMOVE_REQUEST, {"PackageName", request->GetPackageName(),
"abilityName", abilityName, "requestAddress", request->GetUuid()});
UpdateUsingApproximatelyStatus(request, false);
continue;
} else {
UpdateUsingApproximatelyStatus(request, true);
}
if (!ActiveLocatingStrategies(request)) {
continue;
@ -401,6 +427,9 @@ bool RequestManager::ActiveLocatingStrategies(const std::shared_ptr<Request>& re
*/
bool RequestManager::IsRequestAvailable(std::shared_ptr<Request>& request)
{
if (!request->GetIsRequesting()) {
return false;
}
// for frozen app, do not add to workRecord
if (LocatorAbility::GetInstance()->IsProxyPid(request->GetPid())) {
return false;
@ -436,8 +465,11 @@ bool RequestManager::AddRequestToWorkRecord(std::string abilityName, std::shared
if (request == nullptr) {
return false;
}
UpdateUsingPermission(request);
if (!request->GetIsRequesting() || !IsRequestAvailable(request) {
if (!request->GetIsRequesting()) {
return false;
}
if (!IsRequestAvailable(request)) {
return false;
}
auto locationErrorCallback = request->GetLocationErrorCallBack();
@ -486,8 +518,7 @@ bool RequestManager::AddRequestToWorkRecord(std::string abilityName, std::shared
if (!PermissionManager::CheckSystemPermission(tokenId, request->GetTokenIdEx()) &&
!CommonUtils::CheckAppForUser(uid)) {
PrivacyKit::StopUsingPermission(tokenId, ACCESS_APPROXIMATELY_LOCATION);
LBSLOGE(REPORT_MANAGER, "AddRequestToWorkRecord uid: %{public}d ,CheckAppIsCurrentUser fail", uid);
LBSLOGD(REPORT_MANAGER, "AddRequestToWorkRecord uid: %{public}d ,CheckAppIsCurrentUser fail", uid);
return false;
}