diff --git a/services/location_locator/locator/include/report_manager.h b/services/location_locator/locator/include/report_manager.h index 0c4b2feb..99352a0d 100644 --- a/services/location_locator/locator/include/report_manager.h +++ b/services/location_locator/locator/include/report_manager.h @@ -55,6 +55,7 @@ private: Location cacheGnssLocation_; Location cacheNlpLocation_; std::mutex lastLocationMutex_; + std::atomic lastResetRecordTime_; std::unique_ptr ApproximatelyLocation(const std::unique_ptr& location); bool ProcessRequestForReport(std::shared_ptr& request, std::unique_ptr>>& deadRequests, @@ -64,6 +65,8 @@ private: std::unique_ptr ExecuteReportProcess(std::shared_ptr& request, std::unique_ptr& location, std::string abilityName); void UpdateLastLocation(const std::unique_ptr& location); + void LocationReportDelayTimeCheck(const std::unique_ptr& location, + const std::shared_ptr& request); }; } // namespace OHOS } // namespace Location diff --git a/services/location_locator/locator/source/locator_ability.cpp b/services/location_locator/locator/source/locator_ability.cpp index 31ade5c8..b9ab3621 100644 --- a/services/location_locator/locator/source/locator_ability.cpp +++ b/services/location_locator/locator/source/locator_ability.cpp @@ -112,6 +112,7 @@ const int LOCATIONHUB_STATE_UNLOAD = 0; const int LOCATIONHUB_STATE_LOAD = 1; const int MAX_SIZE = 100; const int TIMEOUT_WATCHDOG = 60; // s +const int INVALID_REQUESTS_SIZE = 20; LocatorAbility* LocatorAbility::GetInstance() { @@ -1597,6 +1598,9 @@ LocationErrCode LocatorAbility::RemoveInvalidRequests() #endif } LBSLOGI(LOCATOR, "request num : %{public}d, invalid request num: %{public}d", requestNum, invalidRequestNum); + if (invalidRequestList.size() > INVALID_REQUESTS_SIZE) { + return ERRCODE_SUCCESS; + } for (auto& item : invalidRequestList) { sptr callback = item->GetLocatorCallBack(); StopLocating(callback); diff --git a/services/location_locator/locator/source/report_manager.cpp b/services/location_locator/locator/source/report_manager.cpp index be76bfe6..017bb387 100644 --- a/services/location_locator/locator/source/report_manager.cpp +++ b/services/location_locator/locator/source/report_manager.cpp @@ -26,7 +26,6 @@ #include "location_log_event_ids.h" #include "common_hisysevent.h" #include "permission_manager.h" - #include "hook_utils.h" namespace OHOS { @@ -37,6 +36,8 @@ static constexpr double MAXIMUM_FUZZY_LOCATION_DISTANCE = 4000.0; // Unit m static constexpr double MINIMUM_FUZZY_LOCATION_DISTANCE = 3000.0; // Unit m static constexpr int CACHED_TIME = 25; static constexpr int LONG_CACHE_DURATION = 60; +static constexpr int MAX_LOCATION_REPORT_DELAY_TIME = 30000; // Unit ms +static constexpr int MIN_RESET_TIME_THRESHOLD = 1 * 60 * 60 * 1000; // Unit ms ReportManager* ReportManager::GetInstance() { @@ -48,6 +49,7 @@ ReportManager::ReportManager() { clock_gettime(CLOCK_REALTIME, &lastUpdateTime_); offsetRandom_ = CommonUtils::DoubleRandom(0, 1); + lastResetRecordTime_ = CommonUtils::GetSinceBootTime(); } ReportManager::~ReportManager() {} @@ -133,6 +135,7 @@ bool ReportManager::ProcessRequestForReport(std::shared_ptr& request, ACCESS_APPROXIMATELY_LOCATION, permUsedType, 0, 1); return false; } + LocationReportDelayTimeCheck(finalLocation, request); UpdateLocationByRequest(request->GetTokenId(), request->GetTokenIdEx(), finalLocation); finalLocation = ExecuteReportProcess(request, finalLocation, abilityName); if (finalLocation == nullptr) { @@ -165,6 +168,26 @@ bool ReportManager::ProcessRequestForReport(std::shared_ptr& request, return true; } +void ReportManager::LocationReportDelayTimeCheck(const std::unique_ptr& location, + const std::shared_ptr& request) +{ + if (location == nullptr || request == nullptr) { + return; + } + int64_t currentTime = CommonUtils::GetSinceBootTime(); + long deltaMs = (currentTime - location->GetTimeSinceBoot()) / NANOS_PER_MILLI; + if (deltaMs > MAX_LOCATION_REPORT_DELAY_TIME) { + long recordDeltaMs = (currentTime - lastResetRecordTime_) / NANOS_PER_MILLI; + if (recordDeltaMs < MIN_RESET_TIME_THRESHOLD) { + return; + } + LBSLOGE(REPORT_MANAGER, "%{public}s: %{public}d check fail, current deltaMs = %{public}ld", __func__, + request->GetTokenId(), deltaMs); + lastResetRecordTime_ = currentTime; + _exit(0); + } +} + std::unique_ptr ReportManager::ExecuteReportProcess(std::shared_ptr& request, std::unique_ptr& location, std::string abilityName) { diff --git a/services/location_locator/locator/source/request_manager.cpp b/services/location_locator/locator/source/request_manager.cpp index 23186506..498296c9 100644 --- a/services/location_locator/locator/source/request_manager.cpp +++ b/services/location_locator/locator/source/request_manager.cpp @@ -227,8 +227,8 @@ bool RequestManager::RestorRequest(std::shared_ptr newRequest) } sptr newConfig = newRequest->GetRequestConfig(); - std::list> requestWithSameCallback = iterator->second; - for (auto iter = requestWithSameCallback.begin(); iter != requestWithSameCallback.end(); ++iter) { + auto requestWithSameCallback = &(iterator->second); + for (auto iter = requestWithSameCallback->begin(); iter != requestWithSameCallback->end(); ++iter) { auto request = *iter; if (request == nullptr) { continue; @@ -243,7 +243,7 @@ bool RequestManager::RestorRequest(std::shared_ptr newRequest) return false; } } - requestWithSameCallback.push_back(newRequest); + requestWithSameCallback->push_back(newRequest); LBSLOGD(REQUEST_MANAGER, "add new receiver with old callback"); return true; } @@ -340,7 +340,7 @@ void RequestManager::HandleStopLocating(sptr callback) LBSLOGD(REQUEST_MANAGER, "stop callback"); auto iterator = receivers->find(deadCallback); if (iterator == receivers->end()) { - LBSLOGD(REQUEST_MANAGER, "this callback has no record in receiver map"); + LBSLOGE(REQUEST_MANAGER, "this callback has no record in receiver map"); lock.unlock(); return; }