crash bugfix

Signed-off-by: ruan-shaofei <ruanshaofei2020@163.com>
This commit is contained in:
ruan-shaofei 2024-08-23 16:13:38 +08:00
parent f57ebd63aa
commit 9f7d62a520
21 changed files with 191 additions and 125 deletions

View File

@ -49,9 +49,9 @@ public:
LBSLOGE(CACHED_LOCATIONS_CALLBACK, "context == nullptr.");
return false;
}
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_ref(env_, handlerCb_, &refCount);
context->env = env_;
callbackValid_ = handlerCb_ == nullptr ? false : true;
context->callbackValid = &callbackValid_;
context->callback[SUCCESS_CALLBACK] = handlerCb_;
return true;
}
@ -90,8 +90,6 @@ private:
napi_env env_;
napi_ref handlerCb_;
bool remoteDied_;
std::mutex mutex_;
bool callbackValid_;
};
} // namespace Location
} // namespace OHOS

View File

@ -45,17 +45,15 @@ public:
LBSLOGE(COUNTRY_CODE_CALLBACK, "context == nullptr.");
return false;
}
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_ref(env_, handlerCb_, &refCount);
context->env = env_;
callbackValid_ = handlerCb_ == nullptr ? false : true;
context->callbackValid = &callbackValid_;
context->callback[SUCCESS_CALLBACK] = handlerCb_;
return true;
}
private:
napi_env env_;
napi_ref handlerCb_;
std::mutex mutex_;
bool callbackValid_;
};
} // namespace Location
} // namespace OHOS

View File

@ -47,9 +47,9 @@ public:
LBSLOGE(GNSS_STATUS_CALLBACK, "context == nullptr.");
return false;
}
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_ref(env_, handlerCb_, &refCount);
context->env = env_;
callbackValid_ = handlerCb_ == nullptr ? false : true;
context->callbackValid = &callbackValid_;
context->callback[SUCCESS_CALLBACK] = handlerCb_;
return true;
}
@ -88,8 +88,6 @@ private:
napi_env env_;
napi_ref handlerCb_;
bool remoteDied_;
std::mutex mutex_;
bool callbackValid_;
};
} // namespace Location
} // namespace OHOS

View File

@ -54,9 +54,9 @@ public:
LBSLOGE(LOCATING_DATA_CALLBACK, "context == nullptr.");
return false;
}
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_ref(env_, handlerCb_, &refCount);
context->env = env_;
callbackValid_ = handlerCb_ == nullptr ? false : true;
context->callbackValid = &callbackValid_;
context->callback[SUCCESS_CALLBACK] = handlerCb_;
return true;
}
@ -107,12 +107,10 @@ public:
fixNumber_ = fixNumber;
}
private:
bool callbackValid_;
int fixNumber_;
napi_env env_;
napi_ref handlerCb_;
bool remoteDied_;
std::mutex mutex_;
std::mutex singleResultMutex_;
CountDownLatch* latch_;
std::vector<std::shared_ptr<LocatingRequiredData>> singleResult_;

View File

@ -47,9 +47,9 @@ public:
LBSLOGE(LOCATION_ERR_CALLBACK, "context == nullptr.");
return false;
}
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_ref(env_, handlerCb_, &refCount);
context->env = env_;
callbackValid_ = handlerCb_ == nullptr ? false : true;
context->callbackValid = &callbackValid_;
context->callback[SUCCESS_CALLBACK] = handlerCb_;
return true;
}
@ -77,8 +77,6 @@ public:
private:
napi_env env_;
napi_ref handlerCb_;
std::mutex mutex_;
bool callbackValid_;
};
} // namespace Location
} // namespace OHOS

View File

@ -46,9 +46,9 @@ public:
LBSLOGE(SWITCH_CALLBACK, "context == nullptr.");
return false;
}
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_ref(env_, handlerCb_, &refCount);
context->env = env_;
callbackValid_ = handlerCb_ == nullptr ? false : true;
context->callbackValid = &callbackValid_;
context->callback[SUCCESS_CALLBACK] = handlerCb_;
return true;
}
@ -87,8 +87,6 @@ private:
napi_env env_;
napi_ref handlerCb_;
bool remoteDied_;
std::mutex mutex_;
bool callbackValid_;
};
} // namespace Location
} // namespace OHOS

View File

@ -61,13 +61,16 @@ public:
return false;
}
context->env = env_;
callbackValid_ = handlerCb_ == nullptr ? false : true;
context->callbackValid = &callbackValid_;
uint32_t refCount = INVALID_REF_COUNT;
if (IsSystemGeoLocationApi()) {
napi_reference_ref(env_, successHandlerCb_, &refCount);
napi_reference_ref(env_, failHandlerCb_, &refCount);
napi_reference_ref(env_, completeHandlerCb_, &refCount);
context->callback[SUCCESS_CALLBACK] = successHandlerCb_;
context->callback[FAIL_CALLBACK] = failHandlerCb_;
context->callback[COMPLETE_CALLBACK] = completeHandlerCb_;
} else {
napi_reference_ref(env_, handlerCb_, &refCount);
context->callback[SUCCESS_CALLBACK] = handlerCb_;
}
return true;
@ -145,7 +148,7 @@ public:
inline std::shared_ptr<Location> GetSingleLocation()
{
std::unique_lock<std::mutex> guard(mutex_);
std::unique_lock<std::mutex> guard(locationMutex_);
return singleLocation_;
}
bool NeedSetSingleLocation(const std::unique_ptr<Location>& location);
@ -159,12 +162,11 @@ private:
napi_ref failHandlerCb_;
napi_ref completeHandlerCb_;
int fixNumber_;
std::mutex mutex_;
std::mutex locationMutex_;
CountDownLatch* latch_;
std::shared_ptr<Location> singleLocation_;
int locationPriority_;
bool inHdArea_;
bool callbackValid_;
};
} // namespace Location
} // namespace OHOS

View File

@ -49,9 +49,9 @@ public:
LBSLOGE(NMEA_MESSAGE_CALLBACK, "context == nullptr.");
return false;
}
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_ref(env_, handlerCb_, &refCount);
context->env = env_;
callbackValid_ = handlerCb_ == nullptr ? false : true;
context->callbackValid = &callbackValid_;
context->callback[SUCCESS_CALLBACK] = handlerCb_;
return true;
}
@ -90,8 +90,6 @@ private:
napi_env env_;
napi_ref handlerCb_;
bool remoteDied_;
std::mutex mutex_;
bool callbackValid_;
};
} // namespace Location
} // namespace OHOS

View File

@ -23,12 +23,12 @@
namespace OHOS {
namespace Location {
static std::mutex g_mutex;
CachedLocationsCallbackNapi::CachedLocationsCallbackNapi()
{
env_ = nullptr;
handlerCb_ = nullptr;
remoteDied_ = false;
callbackValid_ = false;
}
CachedLocationsCallbackNapi::~CachedLocationsCallbackNapi()
@ -75,7 +75,7 @@ bool CachedLocationsCallbackNapi::IsRemoteDied()
bool CachedLocationsCallbackNapi::Send(std::vector<std::unique_ptr<Location>>& locations)
{
std::unique_lock<std::mutex> guard(mutex_);
std::unique_lock<std::mutex> guard(g_mutex);
uv_loop_s *loop = nullptr;
NAPI_CALL_BASE(env_, napi_get_uv_event_loop(env_, &loop), false);
if (loop == nullptr) {
@ -123,7 +123,7 @@ void CachedLocationsCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
return;
}
context = static_cast<CachedLocationAsyncContext *>(work->data);
if (context == nullptr || context->env == nullptr || context->callbackValid == nullptr) {
if (context == nullptr || context->env == nullptr) {
LBSLOGE(CACHED_LOCATIONS_CALLBACK, "context is nullptr");
delete work;
return;
@ -135,11 +135,12 @@ void CachedLocationsCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
delete work;
return;
}
std::unique_lock<std::mutex> guard(g_mutex);
napi_value jsEvent = nullptr;
CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_create_object(context->env, &jsEvent),
scope, context, work);
LocationsToJs(context->env, context->locationList, jsEvent);
if (context->callback[0] != nullptr && *(context->callbackValid)) {
if (context->callback[0] != nullptr) {
napi_value undefine;
napi_value handler = nullptr;
CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_get_undefined(context->env, &undefine),
@ -152,6 +153,11 @@ void CachedLocationsCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
}
}
NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope));
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_unref(context->env, context->callback[0], &refCount);
if (refCount == 0) {
NAPI_CALL_RETURN_VOID(context->env, napi_delete_reference(context->env, context->callback[0]));
}
delete context;
delete work;
});
@ -164,14 +170,17 @@ void CachedLocationsCallbackNapi::OnCacheLocationsReport(const std::vector<std::
void CachedLocationsCallbackNapi::DeleteHandler()
{
std::unique_lock<std::mutex> guard(mutex_);
std::unique_lock<std::mutex> guard(g_mutex);
if (handlerCb_ == nullptr || env_ == nullptr) {
LBSLOGE(CACHED_LOCATIONS_CALLBACK, "handler or env is nullptr.");
return;
}
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_unref(env_, handlerCb_, &refCount);
if (refCount == 0) {
NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
}
handlerCb_ = nullptr;
callbackValid_ = false;
}
} // namespace Location
} // namespace OHOS

View File

@ -24,11 +24,11 @@
namespace OHOS {
namespace Location {
static std::mutex g_mutex;
CountryCodeCallbackNapi::CountryCodeCallbackNapi()
{
env_ = nullptr;
handlerCb_ = nullptr;
callbackValid_ = false;
}
CountryCodeCallbackNapi::~CountryCodeCallbackNapi()
@ -60,7 +60,7 @@ int CountryCodeCallbackNapi::OnRemoteRequest(
bool CountryCodeCallbackNapi::Send(const std::shared_ptr<CountryCode>& country)
{
std::unique_lock<std::mutex> guard(mutex_);
std::unique_lock<std::mutex> guard(g_mutex);
uv_loop_s *loop = nullptr;
if (env_ == nullptr) {
LBSLOGE(COUNTRY_CODE_CALLBACK, "env_ == nullptr.");
@ -114,7 +114,7 @@ void CountryCodeCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
return;
}
context = static_cast<CountryCodeContext *>(work->data);
if (context == nullptr || context->env == nullptr || context->callbackValid == nullptr) {
if (context == nullptr || context->env == nullptr) {
LBSLOGE(LOCATOR_CALLBACK, "context is nullptr!");
delete work;
return;
@ -126,6 +126,7 @@ void CountryCodeCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
delete work;
return;
}
std::unique_lock<std::mutex> guard(g_mutex);
napi_value jsEvent;
CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_create_object(context->env, &jsEvent),
scope, context, work);
@ -134,7 +135,7 @@ void CountryCodeCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
} else {
LBSLOGE(LOCATOR_STANDARD, "country is nullptr!");
}
if (context->callback[0] != nullptr && *(context->callbackValid)) {
if (context->callback[0] != nullptr) {
napi_value undefine;
napi_value handler = nullptr;
CHK_NAPI_ERR_CLOSE_SCOPE(context->env,
@ -147,6 +148,11 @@ void CountryCodeCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
}
}
NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope));
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_unref(context->env, context->callback[0], &refCount);
if (refCount == 0) {
NAPI_CALL_RETURN_VOID(context->env, napi_delete_reference(context->env, context->callback[0]));
}
delete context;
delete work;
});
@ -160,26 +166,29 @@ void CountryCodeCallbackNapi::OnCountryCodeChange(const std::shared_ptr<CountryC
void CountryCodeCallbackNapi::SetEnv(napi_env env)
{
std::unique_lock<std::mutex> guard(mutex_);
std::unique_lock<std::mutex> guard(g_mutex);
env_ = env;
}
void CountryCodeCallbackNapi::SetCallback(napi_ref cb)
{
std::unique_lock<std::mutex> guard(mutex_);
std::unique_lock<std::mutex> guard(g_mutex);
handlerCb_ = cb;
}
void CountryCodeCallbackNapi::DeleteHandler()
{
std::unique_lock<std::mutex> guard(mutex_);
std::unique_lock<std::mutex> guard(g_mutex);
if (handlerCb_ == nullptr || env_ == nullptr) {
LBSLOGE(COUNTRY_CODE_CALLBACK, "handler or env is nullptr.");
return;
}
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_unref(env_, handlerCb_, &refCount);
if (refCount == 0) {
NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
}
handlerCb_ = nullptr;
callbackValid_ = false;
}
} // namespace Location
} // namespace OHOS

View File

@ -25,12 +25,12 @@
namespace OHOS {
namespace Location {
static std::mutex g_mutex;
GnssStatusCallbackNapi::GnssStatusCallbackNapi()
{
env_ = nullptr;
handlerCb_ = nullptr;
remoteDied_ = false;
callbackValid_ = false;
}
GnssStatusCallbackNapi::~GnssStatusCallbackNapi()
@ -71,7 +71,7 @@ bool GnssStatusCallbackNapi::IsRemoteDied()
bool GnssStatusCallbackNapi::Send(std::unique_ptr<SatelliteStatus>& statusInfo)
{
std::unique_lock<std::mutex> guard(mutex_);
std::unique_lock<std::mutex> guard(g_mutex);
uv_loop_s *loop = nullptr;
NAPI_CALL_BASE(env_, napi_get_uv_event_loop(env_, &loop), false);
if (loop == nullptr) {
@ -117,7 +117,7 @@ void GnssStatusCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
return;
}
context = static_cast<GnssStatusAsyncContext *>(work->data);
if (context == nullptr || context->env == nullptr || context->callbackValid == nullptr) {
if (context == nullptr || context->env == nullptr) {
LBSLOGE(LOCATOR_CALLBACK, "context is nullptr!");
delete work;
return;
@ -129,25 +129,30 @@ void GnssStatusCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
delete work;
return;
}
std::unique_lock<std::mutex> guard(g_mutex);
napi_value jsEvent = nullptr;
if (context->statusInfo != nullptr) {
CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_create_object(context->env, &jsEvent),
scope, context, work);
SatelliteStatusToJs(context->env, context->statusInfo, jsEvent);
}
if (context->callback[0] != nullptr && *(context->callbackValid)) {
if (context->callback[0] != nullptr) {
napi_value undefine;
napi_value handler = nullptr;
CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_get_undefined(context->env, &undefine),
scope, context, work);
CHK_NAPI_ERR_CLOSE_SCOPE(context->env,
napi_get_reference_value(context->env, context->callback[0], &handler), scope, context, work);
if (napi_call_function(context->env, nullptr, handler, 1,
&jsEvent, &undefine) != napi_ok) {
if (napi_call_function(context->env, nullptr, handler, 1, &jsEvent, &undefine) != napi_ok) {
LBSLOGE(GNSS_STATUS_CALLBACK, "Report event failed");
}
}
NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope));
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_unref(context->env, context->callback[0], &refCount);
if (refCount == 0) {
NAPI_CALL_RETURN_VOID(context->env, napi_delete_reference(context->env, context->callback[0]));
}
delete context;
delete work;
});
@ -160,14 +165,17 @@ void GnssStatusCallbackNapi::OnStatusChange(const std::unique_ptr<SatelliteStatu
void GnssStatusCallbackNapi::DeleteHandler()
{
std::unique_lock<std::mutex> guard(mutex_);
std::unique_lock<std::mutex> guard(g_mutex);
if (handlerCb_ == nullptr || env_ == nullptr) {
LBSLOGE(GNSS_STATUS_CALLBACK, "handler or env is nullptr.");
return;
}
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_unref(env_, handlerCb_, &refCount);
if (refCount == 0) {
NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
}
handlerCb_ = nullptr;
callbackValid_ = false;
}
} // namespace Location
} // namespace OHOS

View File

@ -23,6 +23,7 @@
namespace OHOS {
namespace Location {
static std::mutex g_mutex;
LocatingRequiredDataCallbackNapi::LocatingRequiredDataCallbackNapi()
{
env_ = nullptr;
@ -30,7 +31,6 @@ LocatingRequiredDataCallbackNapi::LocatingRequiredDataCallbackNapi()
remoteDied_ = false;
fixNumber_ = 0;
InitLatch();
callbackValid_ = false;
}
LocatingRequiredDataCallbackNapi::~LocatingRequiredDataCallbackNapi()
@ -95,7 +95,7 @@ bool LocatingRequiredDataCallbackNapi::Send(const std::vector<std::shared_ptr<Lo
if (IsSingleLocationRequest()) {
return false;
}
std::unique_lock<std::mutex> guard(mutex_);
std::unique_lock<std::mutex> guard(g_mutex);
uv_loop_s *loop = nullptr;
NAPI_CALL_BASE(env_, napi_get_uv_event_loop(env_, &loop), false);
if (loop == nullptr) {
@ -141,7 +141,7 @@ void LocatingRequiredDataCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* w
return;
}
context = static_cast<LocatingRequiredDataAsyncContext *>(work->data);
if (context == nullptr || context->env == nullptr || context->callbackValid == nullptr) {
if (context == nullptr || context->env == nullptr) {
LBSLOGE(LOCATING_DATA_CALLBACK, "context is nullptr");
delete work;
return;
@ -153,12 +153,13 @@ void LocatingRequiredDataCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* w
delete work;
return;
}
std::unique_lock<std::mutex> guard(g_mutex);
napi_value jsEvent = nullptr;
CHK_NAPI_ERR_CLOSE_SCOPE(context->env,
napi_create_array_with_length(context->env, context->locatingRequiredDataList_.size(), &jsEvent),
scope, context, work);
LocatingRequiredDataToJsObj(context->env, context->locatingRequiredDataList_, jsEvent);
if (context->callback[0] != nullptr && *(context->callbackValid)) {
if (context->callback[0] != nullptr) {
napi_value undefine;
napi_value handler = nullptr;
CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_get_undefined(context->env, &undefine),
@ -171,6 +172,11 @@ void LocatingRequiredDataCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* w
}
}
NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope));
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_unref(context->env, context->callback[0], &refCount);
if (refCount == 0) {
NAPI_CALL_RETURN_VOID(context->env, napi_delete_reference(context->env, context->callback[0]));
}
delete context;
delete work;
});
@ -185,14 +191,17 @@ void LocatingRequiredDataCallbackNapi::OnLocatingDataChange(
void LocatingRequiredDataCallbackNapi::DeleteHandler()
{
std::unique_lock<std::mutex> guard(mutex_);
std::unique_lock<std::mutex> guard(g_mutex);
if (handlerCb_ == nullptr || env_ == nullptr) {
LBSLOGE(LOCATING_DATA_CALLBACK, "handler or env is nullptr.");
return;
}
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_unref(env_, handlerCb_, &refCount);
if (refCount == 0) {
NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
}
handlerCb_ = nullptr;
callbackValid_ = false;
}
bool LocatingRequiredDataCallbackNapi::IsSingleLocationRequest()

View File

@ -23,11 +23,11 @@
namespace OHOS {
namespace Location {
static std::mutex g_mutex;
LocationErrorCallbackNapi::LocationErrorCallbackNapi()
{
env_ = nullptr;
handlerCb_ = nullptr;
callbackValid_ = false;
}
LocationErrorCallbackNapi::~LocationErrorCallbackNapi()
@ -59,7 +59,7 @@ int LocationErrorCallbackNapi::OnRemoteRequest(
bool LocationErrorCallbackNapi::Send(int32_t errorCode)
{
LBSLOGI(LOCATION_ERR_CALLBACK, "LocatorCallbackNapi::OnRemoteRequest! errorCode = %{public}d", errorCode);
std::unique_lock<std::mutex> guard(mutex_);
std::unique_lock<std::mutex> guard(g_mutex);
uv_loop_s *loop = nullptr;
NAPI_CALL_BASE(env_, napi_get_uv_event_loop(env_, &loop), false);
if (loop == nullptr) {
@ -105,7 +105,7 @@ void LocationErrorCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
return;
}
context = static_cast<LocationErrorAsyncContext *>(work->data);
if (context == nullptr || context->env == nullptr || context->callbackValid == nullptr) {
if (context == nullptr || context->env == nullptr) {
LBSLOGE(LOCATION_ERR_CALLBACK, "context is nullptr!");
delete work;
return;
@ -120,7 +120,8 @@ void LocationErrorCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
delete work;
return;
}
if (context->callback[0] != nullptr && *(context->callbackValid)) {
std::unique_lock<std::mutex> guard(g_mutex);
if (context->callback[0] != nullptr) {
napi_value undefine;
napi_value handler = nullptr;
CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_get_undefined(context->env, &undefine),
@ -133,6 +134,11 @@ void LocationErrorCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
}
}
NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope));
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_unref(context->env, context->callback[0], &refCount);
if (refCount == 0) {
NAPI_CALL_RETURN_VOID(context->env, napi_delete_reference(context->env, context->callback[0]));
}
delete context;
delete work;
});
@ -154,14 +160,17 @@ void LocationErrorCallbackNapi::OnErrorReport(const int errorCode)
void LocationErrorCallbackNapi::DeleteHandler()
{
std::unique_lock<std::mutex> guard(mutex_);
std::unique_lock<std::mutex> guard(g_mutex);
if (handlerCb_ == nullptr || env_ == nullptr) {
LBSLOGE(LOCATION_ERR_CALLBACK, "handler or env is nullptr.");
return;
}
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_unref(env_, handlerCb_, &refCount);
if (refCount == 0) {
NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
}
handlerCb_ = nullptr;
callbackValid_ = false;
}
} // namespace Location
} // namespace OHOS

View File

@ -26,6 +26,7 @@
namespace OHOS {
namespace Location {
static std::mutex g_mutex;
LocationGnssGeofenceCallbackNapi::LocationGnssGeofenceCallbackNapi()
{
env_ = nullptr;
@ -34,7 +35,6 @@ LocationGnssGeofenceCallbackNapi::LocationGnssGeofenceCallbackNapi()
fenceId_ = -1;
type_ = GNSS_GEOFENCE_OPT_TYPE_ADD;
result_ = GNSS_GEOFENCE_OPERATION_SUCCESS;
callbackValid_ = false;
InitLatch();
}
@ -92,7 +92,7 @@ int LocationGnssGeofenceCallbackNapi::OnRemoteRequest(
void LocationGnssGeofenceCallbackNapi::OnTransitionStatusChange(
GeofenceTransition transition)
{
std::unique_lock<std::mutex> guard(mutex_);
std::unique_lock<std::mutex> guard(g_mutex);
uv_loop_s *loop = nullptr;
NAPI_CALL_RETURN_VOID(env_, napi_get_uv_event_loop(env_, &loop));
if (loop == nullptr) {
@ -155,7 +155,7 @@ void LocationGnssGeofenceCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* w
return;
}
context = static_cast<GnssGeofenceAsyncContext *>(work->data);
if (context == nullptr || context->env == nullptr || context->callbackValid == nullptr) {
if (context == nullptr || context->env == nullptr) {
LBSLOGE(LOCATION_GNSS_GEOFENCE_CALLBACK, "context is nullptr");
delete work;
return;
@ -167,13 +167,14 @@ void LocationGnssGeofenceCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* w
delete work;
return;
}
std::unique_lock<std::mutex> guard(g_mutex);
napi_value jsEvent[PARAM2];
CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_create_object(context->env, &jsEvent[PARAM1]),
scope, context, work);
CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_get_undefined(context->env, &jsEvent[PARAM0]),
scope, context, work);
GeofenceTransitionToJs(context->env, context->transition_, jsEvent[PARAM1]);
if (context->callback[SUCCESS_CALLBACK] != nullptr && *(context->callbackValid)) {
if (context->callback[SUCCESS_CALLBACK] != nullptr) {
napi_value undefine;
napi_value handler = nullptr;
CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_get_undefined(context->env, &undefine),
@ -187,6 +188,11 @@ void LocationGnssGeofenceCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* w
}
}
NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope));
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_unref(context->env, context->callback[0], &refCount);
if (refCount == 0) {
NAPI_CALL_RETURN_VOID(context->env, napi_delete_reference(context->env, context->callback[0]));
}
delete context;
delete work;
});
@ -194,14 +200,17 @@ void LocationGnssGeofenceCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* w
void LocationGnssGeofenceCallbackNapi::DeleteHandler()
{
std::unique_lock<std::mutex> guard(mutex_);
std::unique_lock<std::mutex> guard(g_mutex);
if (handlerCb_ == nullptr || env_ == nullptr) {
LBSLOGE(LOCATION_GNSS_GEOFENCE_CALLBACK, "handler or env is nullptr.");
return;
}
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_unref(env_, handlerCb_, &refCount);
if (refCount == 0) {
NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
}
handlerCb_ = nullptr;
callbackValid_ = false;
}
void LocationGnssGeofenceCallbackNapi::CountDown()

View File

@ -20,12 +20,12 @@
namespace OHOS {
namespace Location {
static std::mutex g_mutex;
LocationSwitchCallbackNapi::LocationSwitchCallbackNapi()
{
env_ = nullptr;
handlerCb_ = nullptr;
remoteDied_ = false;
callbackValid_ = false;
}
LocationSwitchCallbackNapi::~LocationSwitchCallbackNapi()
@ -72,7 +72,7 @@ napi_value LocationSwitchCallbackNapi::PackResult(bool switchState)
bool LocationSwitchCallbackNapi::Send(int switchState)
{
std::unique_lock<std::mutex> guard(mutex_);
std::unique_lock<std::mutex> guard(g_mutex);
uv_loop_s *loop = nullptr;
NAPI_CALL_BASE(env_, napi_get_uv_event_loop(env_, &loop), false);
if (loop == nullptr) {
@ -118,7 +118,7 @@ void LocationSwitchCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
return;
}
context = static_cast<SwitchAsyncContext *>(work->data);
if (context == nullptr || context->env == nullptr || context->callbackValid == nullptr) {
if (context == nullptr || context->env == nullptr) {
LBSLOGE(LOCATOR_CALLBACK, "context is nullptr!");
delete work;
return;
@ -133,7 +133,8 @@ void LocationSwitchCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
delete work;
return;
}
if (context->callback[0] != nullptr && *(context->callbackValid)) {
std::unique_lock<std::mutex> guard(g_mutex);
if (context->callback[0] != nullptr) {
napi_value undefine;
napi_value handler = nullptr;
CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_get_undefined(context->env, &undefine),
@ -146,6 +147,11 @@ void LocationSwitchCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
}
}
NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope));
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_unref(context->env, context->callback[0], &refCount);
if (refCount == 0) {
NAPI_CALL_RETURN_VOID(context->env, napi_delete_reference(context->env, context->callback[0]));
}
delete context;
delete work;
});
@ -159,14 +165,17 @@ void LocationSwitchCallbackNapi::OnSwitchChange(int switchState)
void LocationSwitchCallbackNapi::DeleteHandler()
{
std::unique_lock<std::mutex> guard(mutex_);
std::unique_lock<std::mutex> guard(g_mutex);
if (handlerCb_ == nullptr || env_ == nullptr) {
LBSLOGE(SWITCH_CALLBACK, "handler or env is nullptr.");
return;
}
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_unref(env_, handlerCb_, &refCount);
if (refCount == 0) {
NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
}
handlerCb_ = nullptr;
callbackValid_ = false;
}
} // namespace Location
} // namespace OHOS

View File

@ -33,6 +33,7 @@
namespace OHOS {
namespace Location {
static std::mutex g_mutex;
LocatorCallbackNapi::LocatorCallbackNapi()
{
env_ = nullptr;
@ -43,7 +44,6 @@ LocatorCallbackNapi::LocatorCallbackNapi()
fixNumber_ = 0;
inHdArea_ = true;
singleLocation_ = nullptr;
callbackValid_ = false;
locationPriority_ = 0;
InitLatch();
}
@ -121,7 +121,7 @@ void LocatorCallbackNapi::DoSendWork(uv_loop_s*& loop, uv_work_t*& work)
delete work;
return;
}
if (context->env == nullptr || context->loc == nullptr || context->callbackValid == nullptr) {
if (context->env == nullptr || context->loc == nullptr) {
delete context;
delete work;
return;
@ -131,6 +131,7 @@ void LocatorCallbackNapi::DoSendWork(uv_loop_s*& loop, uv_work_t*& work)
DELETE_SCOPE_CONTEXT_WORK(context->env, scope, context, work);
return;
}
std::unique_lock<std::mutex> guard(g_mutex);
napi_value jsEvent = nullptr;
CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_create_object(context->env, &jsEvent), scope, context, work);
if (context->callback[1]) {
@ -138,7 +139,7 @@ void LocatorCallbackNapi::DoSendWork(uv_loop_s*& loop, uv_work_t*& work)
} else {
LocationToJs(context->env, context->loc, jsEvent);
}
if (context->callback[0] != nullptr && *(context->callbackValid)) {
if (context->callback[0] != nullptr) {
napi_value undefine = nullptr;
napi_value handler = nullptr;
CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_get_undefined(context->env, &undefine),
@ -149,7 +150,14 @@ void LocatorCallbackNapi::DoSendWork(uv_loop_s*& loop, uv_work_t*& work)
LBSLOGE(LOCATOR_CALLBACK, "Report location failed");
}
}
DELETE_SCOPE_CONTEXT_WORK(context->env, scope, context, work);
NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope));
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_unref(context->env, context->callback[0], &refCount);
if (refCount == 0) {
NAPI_CALL_RETURN_VOID(context->env, napi_delete_reference(context->env, context->callback[0]));
}
delete context;
delete work;
});
}
@ -164,7 +172,7 @@ void LocatorCallbackNapi::DoSendErrorCode(uv_loop_s *&loop, uv_work_t *&work)
return;
}
context = static_cast<AsyncContext *>(work->data);
if (context == nullptr || context->env == nullptr || context->callbackValid == nullptr) {
if (context == nullptr || context->env == nullptr) {
LBSLOGE(LOCATOR_CALLBACK, "context is nullptr");
delete work;
return;
@ -176,7 +184,8 @@ void LocatorCallbackNapi::DoSendErrorCode(uv_loop_s *&loop, uv_work_t *&work)
delete work;
return;
}
if (context->callback[FAIL_CALLBACK] != nullptr && *(context->callbackValid)) {
std::unique_lock<std::mutex> guard(g_mutex);
if (context->callback[FAIL_CALLBACK] != nullptr) {
napi_value undefine;
napi_value handler = nullptr;
CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_get_undefined(context->env, &undefine),
@ -192,6 +201,12 @@ void LocatorCallbackNapi::DoSendErrorCode(uv_loop_s *&loop, uv_work_t *&work)
}
}
NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope));
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_unref(context->env, context->callback[FAIL_CALLBACK], &refCount);
if (refCount == 0) {
NAPI_CALL_RETURN_VOID(context->env,
napi_delete_reference(context->env, context->callback[FAIL_CALLBACK]));
}
delete context;
delete work;
});
@ -199,7 +214,7 @@ void LocatorCallbackNapi::DoSendErrorCode(uv_loop_s *&loop, uv_work_t *&work)
bool LocatorCallbackNapi::SendErrorCode(const int& errorCode)
{
std::unique_lock<std::mutex> guard(mutex_);
std::unique_lock<std::mutex> guard(g_mutex);
if (!IsSystemGeoLocationApi() && !IsSingleLocationRequest()) {
LBSLOGE(LOCATOR_CALLBACK, "this is Callback type,cant send error msg.");
return false;
@ -240,7 +255,7 @@ bool LocatorCallbackNapi::SendErrorCode(const int& errorCode)
void LocatorCallbackNapi::OnLocationReport(const std::unique_ptr<Location>& location)
{
std::unique_lock<std::mutex> guard(mutex_);
std::unique_lock<std::mutex> guard(g_mutex);
uv_loop_s *loop = nullptr;
if (env_ == nullptr) {
LBSLOGD(LOCATOR_CALLBACK, "env_ is nullptr.");
@ -291,31 +306,35 @@ void LocatorCallbackNapi::DeleteAllCallbacks()
void LocatorCallbackNapi::DeleteHandler()
{
LBSLOGD(LOCATOR_CALLBACK, "before DeleteHandler");
std::unique_lock<std::mutex> guard(mutex_);
std::unique_lock<std::mutex> guard(g_mutex);
if (env_ == nullptr) {
LBSLOGE(LOCATOR_CALLBACK, "env is nullptr.");
return;
}
uint32_t refCount = INVALID_REF_COUNT;
if (IsSystemGeoLocationApi()) {
if (successHandlerCb_ != nullptr) {
napi_reference_unref(env_, successHandlerCb_, &refCount);
if (successHandlerCb_ != nullptr && refCount == 0) {
NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, successHandlerCb_));
successHandlerCb_ = nullptr;
}
if (failHandlerCb_ != nullptr) {
napi_reference_unref(env_, failHandlerCb_, &refCount);
if (failHandlerCb_ != nullptr && refCount == 0) {
NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, failHandlerCb_));
failHandlerCb_ = nullptr;
}
if (completeHandlerCb_ != nullptr) {
napi_reference_unref(env_, completeHandlerCb_, &refCount);
if (completeHandlerCb_ != nullptr && refCount == 0) {
NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, completeHandlerCb_));
completeHandlerCb_ = nullptr;
}
} else {
if (handlerCb_ != nullptr) {
napi_reference_unref(env_, handlerCb_, &refCount);
if (handlerCb_ != nullptr && refCount == 0) {
NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
handlerCb_ = nullptr;
}
}
callbackValid_ = false;
}
bool LocatorCallbackNapi::IsSystemGeoLocationApi()
@ -382,7 +401,7 @@ bool LocatorCallbackNapi::IfReportAccuracyLocation()
void LocatorCallbackNapi::SetSingleLocation(const std::unique_ptr<Location>& location)
{
std::unique_lock<std::mutex> guard(mutex_);
std::unique_lock<std::mutex> guard(locationMutex_);
singleLocation_ = std::make_shared<Location>(*location);
}
} // namespace Location

View File

@ -1037,7 +1037,7 @@ void DeleteCallbackHandler(uv_loop_s *&loop, uv_work_t *&work)
return;
}
context = static_cast<AsyncContext *>(work->data);
if (context == nullptr || context->env == nullptr || context->callbackValid == nullptr) {
if (context == nullptr || context->env == nullptr) {
LBSLOGE(LOCATOR_CALLBACK, "context is nullptr");
delete work;
return;
@ -1049,13 +1049,6 @@ void DeleteCallbackHandler(uv_loop_s *&loop, uv_work_t *&work)
delete work;
return;
}
if (*(context->callbackValid) == false) {
LBSLOGE(LOCATOR_CALLBACK, "callbackValid is false");
NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope));
delete context;
delete work;
return;
}
if (context->callback[SUCCESS_CALLBACK] != nullptr) {
CHK_NAPI_ERR_CLOSE_SCOPE(context->env,
napi_delete_reference(context->env, context->callback[SUCCESS_CALLBACK]),
@ -1071,7 +1064,6 @@ void DeleteCallbackHandler(uv_loop_s *&loop, uv_work_t *&work)
napi_delete_reference(context->env, context->callback[COMPLETE_CALLBACK]),
scope, context, work);
}
*(context->callbackValid) = false;
NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope));
delete context;
delete work;

View File

@ -24,12 +24,12 @@
namespace OHOS {
namespace Location {
static std::mutex g_mutex;
NmeaMessageCallbackNapi::NmeaMessageCallbackNapi()
{
env_ = nullptr;
handlerCb_ = nullptr;
remoteDied_ = false;
callbackValid_ = false;
}
NmeaMessageCallbackNapi::~NmeaMessageCallbackNapi()
@ -78,7 +78,7 @@ napi_value NmeaMessageCallbackNapi::PackResult(const std::string msg)
bool NmeaMessageCallbackNapi::Send(const std::string msg)
{
std::unique_lock<std::mutex> guard(mutex_);
std::unique_lock<std::mutex> guard(g_mutex);
uv_loop_s *loop = nullptr;
NAPI_CALL_BASE(env_, napi_get_uv_event_loop(env_, &loop), false);
if (loop == nullptr) {
@ -125,7 +125,7 @@ void NmeaMessageCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
return;
}
context = static_cast<NmeaAsyncContext *>(work->data);
if (context == nullptr || context->env == nullptr || context->callbackValid == nullptr) {
if (context == nullptr || context->env == nullptr) {
LBSLOGE(LOCATOR_CALLBACK, "context is nullptr!");
delete work;
return;
@ -137,11 +137,12 @@ void NmeaMessageCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
delete work;
return;
}
std::unique_lock<std::mutex> guard(g_mutex);
napi_value jsEvent;
CHK_NAPI_ERR_CLOSE_SCOPE(context->env,
napi_create_string_utf8(context->env, context->msg.c_str(), NAPI_AUTO_LENGTH, &jsEvent),
scope, context, work);
if (context->callback[0] != nullptr && *(context->callbackValid)) {
if (context->callback[0] != nullptr) {
napi_value undefine;
napi_value handler = nullptr;
CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_get_undefined(context->env, &undefine),
@ -149,12 +150,16 @@ void NmeaMessageCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
CHK_NAPI_ERR_CLOSE_SCOPE(context->env,
napi_get_reference_value(context->env, context->callback[0], &handler),
scope, context, work);
if (napi_call_function(context->env, nullptr, handler, 1,
&jsEvent, &undefine) != napi_ok) {
if (napi_call_function(context->env, nullptr, handler, 1, &jsEvent, &undefine) != napi_ok) {
LBSLOGE(NMEA_MESSAGE_CALLBACK, "Report event failed");
}
}
NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope));
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_unref(context->env, context->callback[0], &refCount);
if (refCount == 0) {
NAPI_CALL_RETURN_VOID(context->env, napi_delete_reference(context->env, context->callback[0]));
}
delete context;
delete work;
});
@ -168,14 +173,17 @@ void NmeaMessageCallbackNapi::OnMessageChange(int64_t timestamp, const std::stri
void NmeaMessageCallbackNapi::DeleteHandler()
{
std::unique_lock<std::mutex> guard(mutex_);
std::unique_lock<std::mutex> guard(g_mutex);
if (handlerCb_ == nullptr || env_ == nullptr) {
LBSLOGE(NMEA_MESSAGE_CALLBACK, "handler or env is nullptr.");
return;
}
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_unref(env_, handlerCb_, &refCount);
if (refCount == 0) {
NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
}
handlerCb_ = nullptr;
callbackValid_ = false;
}
} // namespace Location
} // namespace OHOS

View File

@ -27,7 +27,6 @@ public:
napi_async_work work;
napi_deferred deferred;
napi_ref callback[MAX_CALLBACK_NUM] = { 0 };
bool* callbackValid;
std::function<void(void*)> executeFunc;
std::function<void(void*)> completeFunc;
napi_value resourceName;
@ -44,7 +43,6 @@ public:
resourceName = nullptr;
result[PARAM0] = nullptr;
result[PARAM1] = nullptr;
callbackValid = nullptr;
errCode = 0;
}

View File

@ -47,6 +47,7 @@ const int32_t UNKNOW_USER_ID = -1;
const int32_t SUBSCRIBE_TIME = 5;
const int32_t DEFAULT_TIME_INTERVAL = 30 * 60; // app receive location every 30 minutes in frozen state
const int32_t REQUESTS_NUM_MAX = 1;
constexpr uint32_t INVALID_REF_COUNT = 0xFF;
constexpr const char* FEATURE_SWITCH_PROP = "ro.config.locator_background";
constexpr const char* TIME_INTERVAL_PROP = "ro.config.locator_background.timeInterval";
constexpr const char* PROC_NAME = "system";

View File

@ -57,9 +57,9 @@ public:
LBSLOGE(LOCATION_GNSS_GEOFENCE_CALLBACK, "context == nullptr.");
return false;
}
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_ref(env_, handlerCb_, &refCount);
context->env = env_;
callbackValid_ = handlerCb_ == nullptr ? false : true;
context->callbackValid = &callbackValid_;
context->callback[SUCCESS_CALLBACK] = handlerCb_;
return true;
}
@ -101,11 +101,9 @@ private:
napi_env env_;
napi_ref handlerCb_;
bool remoteDied_;
std::mutex mutex_;
std::mutex operationResultMutex_;
CountDownLatch* latch_;
int fenceId_;
bool callbackValid_;
GnssGeofenceOperateType type_;
GnssGeofenceOperateResult result_;
};