!892 bugfix for conditional competition

Merge pull request !892 from ruan-shaofei/master
This commit is contained in:
openharmony_ci 2024-09-07 11:17:43 +00:00 committed by Gitee
commit a061a833a8
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
19 changed files with 160 additions and 154 deletions

View File

@ -49,8 +49,6 @@ public:
LBSLOGE(CACHED_LOCATIONS_CALLBACK, "context == nullptr."); LBSLOGE(CACHED_LOCATIONS_CALLBACK, "context == nullptr.");
return false; return false;
} }
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_ref(env_, handlerCb_, &refCount);
context->env = env_; context->env = env_;
context->callback[SUCCESS_CALLBACK] = handlerCb_; context->callback[SUCCESS_CALLBACK] = handlerCb_;
return true; return true;

View File

@ -45,8 +45,6 @@ public:
LBSLOGE(COUNTRY_CODE_CALLBACK, "context == nullptr."); LBSLOGE(COUNTRY_CODE_CALLBACK, "context == nullptr.");
return false; return false;
} }
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_ref(env_, handlerCb_, &refCount);
context->env = env_; context->env = env_;
context->callback[SUCCESS_CALLBACK] = handlerCb_; context->callback[SUCCESS_CALLBACK] = handlerCb_;
return true; return true;

View File

@ -28,6 +28,8 @@
namespace OHOS { namespace OHOS {
namespace Location { namespace Location {
bool FindGnssRegCallback(napi_ref cb);
void DeleteGnssRegCallback(napi_ref cb);
class GnssStatusCallbackNapi : public IRemoteStub<IGnssStatusCallback> { class GnssStatusCallbackNapi : public IRemoteStub<IGnssStatusCallback> {
public: public:
GnssStatusCallbackNapi(); GnssStatusCallbackNapi();
@ -39,6 +41,8 @@ public:
void OnStatusChange(const std::unique_ptr<SatelliteStatus>& statusInfo) override; void OnStatusChange(const std::unique_ptr<SatelliteStatus>& statusInfo) override;
void DeleteHandler(); void DeleteHandler();
void UvQueueWork(uv_loop_s* loop, uv_work_t* work); void UvQueueWork(uv_loop_s* loop, uv_work_t* work);
napi_ref GetHandleCb();
void SetHandleCb(const napi_ref& handlerCb);
template <typename T> template <typename T>
bool InitContext(T* context) bool InitContext(T* context)
@ -47,8 +51,6 @@ public:
LBSLOGE(GNSS_STATUS_CALLBACK, "context == nullptr."); LBSLOGE(GNSS_STATUS_CALLBACK, "context == nullptr.");
return false; return false;
} }
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_ref(env_, handlerCb_, &refCount);
context->env = env_; context->env = env_;
context->callback[SUCCESS_CALLBACK] = handlerCb_; context->callback[SUCCESS_CALLBACK] = handlerCb_;
return true; return true;
@ -64,16 +66,6 @@ public:
env_ = env; env_ = env;
} }
inline napi_ref GetHandleCb() const
{
return handlerCb_;
}
inline void SetHandleCb(const napi_ref& handlerCb)
{
handlerCb_ = handlerCb;
}
inline bool GetRemoteDied() const inline bool GetRemoteDied() const
{ {
return remoteDied_; return remoteDied_;

View File

@ -54,8 +54,6 @@ public:
LBSLOGE(LOCATING_DATA_CALLBACK, "context == nullptr."); LBSLOGE(LOCATING_DATA_CALLBACK, "context == nullptr.");
return false; return false;
} }
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_ref(env_, handlerCb_, &refCount);
context->env = env_; context->env = env_;
context->callback[SUCCESS_CALLBACK] = handlerCb_; context->callback[SUCCESS_CALLBACK] = handlerCb_;
return true; return true;

View File

@ -47,8 +47,6 @@ public:
LBSLOGE(LOCATION_ERR_CALLBACK, "context == nullptr."); LBSLOGE(LOCATION_ERR_CALLBACK, "context == nullptr.");
return false; return false;
} }
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_ref(env_, handlerCb_, &refCount);
context->env = env_; context->env = env_;
context->callback[SUCCESS_CALLBACK] = handlerCb_; context->callback[SUCCESS_CALLBACK] = handlerCb_;
return true; return true;

View File

@ -26,6 +26,8 @@
namespace OHOS { namespace OHOS {
namespace Location { namespace Location {
bool FindSwitchRegCallback(napi_ref cb);
void DeleteSwitchRegCallback(napi_ref cb);
class LocationSwitchCallbackNapi : public IRemoteStub<ISwitchCallback> { class LocationSwitchCallbackNapi : public IRemoteStub<ISwitchCallback> {
public: public:
LocationSwitchCallbackNapi(); LocationSwitchCallbackNapi();
@ -38,6 +40,8 @@ public:
void OnSwitchChange(int switchState) override; void OnSwitchChange(int switchState) override;
void DeleteHandler(); void DeleteHandler();
void UvQueueWork(uv_loop_s* loop, uv_work_t* work); void UvQueueWork(uv_loop_s* loop, uv_work_t* work);
napi_ref GetHandleCb();
void SetHandleCb(const napi_ref& handlerCb);
template <typename T> template <typename T>
bool InitContext(T* context) bool InitContext(T* context)
@ -46,8 +50,6 @@ public:
LBSLOGE(SWITCH_CALLBACK, "context == nullptr."); LBSLOGE(SWITCH_CALLBACK, "context == nullptr.");
return false; return false;
} }
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_ref(env_, handlerCb_, &refCount);
context->env = env_; context->env = env_;
context->callback[SUCCESS_CALLBACK] = handlerCb_; context->callback[SUCCESS_CALLBACK] = handlerCb_;
return true; return true;
@ -63,16 +65,6 @@ public:
env_ = env; env_ = env;
} }
inline napi_ref GetHandleCb() const
{
return handlerCb_;
}
inline void SetHandleCb(const napi_ref& handlerCb)
{
handlerCb_ = handlerCb;
}
inline bool GetRemoteDied() const inline bool GetRemoteDied() const
{ {
return remoteDied_; return remoteDied_;

View File

@ -27,6 +27,8 @@
namespace OHOS { namespace OHOS {
namespace Location { namespace Location {
bool FindRegCallback(napi_ref cb);
void DeleteRegCallback(napi_ref cb);
class LocatorCallbackNapi : public IRemoteStub<ILocatorCallback> { class LocatorCallbackNapi : public IRemoteStub<ILocatorCallback> {
public: public:
LocatorCallbackNapi(); LocatorCallbackNapi();
@ -52,6 +54,8 @@ public:
void Wait(int time); void Wait(int time);
int GetCount(); int GetCount();
void SetCount(int count); void SetCount(int count);
napi_ref GetHandleCb();
void SetHandleCb(const napi_ref& handlerCb);
template <typename T> template <typename T>
bool InitContext(T* context) bool InitContext(T* context)
@ -66,8 +70,6 @@ public:
context->callback[FAIL_CALLBACK] = failHandlerCb_; context->callback[FAIL_CALLBACK] = failHandlerCb_;
context->callback[COMPLETE_CALLBACK] = completeHandlerCb_; context->callback[COMPLETE_CALLBACK] = completeHandlerCb_;
} else { } else {
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_ref(env_, handlerCb_, &refCount);
context->callback[SUCCESS_CALLBACK] = handlerCb_; context->callback[SUCCESS_CALLBACK] = handlerCb_;
} }
return true; return true;
@ -83,16 +85,6 @@ public:
env_ = env; env_ = env;
} }
inline napi_ref GetHandleCb() const
{
return handlerCb_;
}
inline void SetHandleCb(const napi_ref& handlerCb)
{
handlerCb_ = handlerCb;
}
inline napi_ref GetSuccHandleCb() const inline napi_ref GetSuccHandleCb() const
{ {
return successHandlerCb_; return successHandlerCb_;

View File

@ -49,8 +49,6 @@ public:
LBSLOGE(NMEA_MESSAGE_CALLBACK, "context == nullptr."); LBSLOGE(NMEA_MESSAGE_CALLBACK, "context == nullptr.");
return false; return false;
} }
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_ref(env_, handlerCb_, &refCount);
context->env = env_; context->env = env_;
context->callback[SUCCESS_CALLBACK] = handlerCb_; context->callback[SUCCESS_CALLBACK] = handlerCb_;
return true; return true;

View File

@ -151,11 +151,6 @@ void CachedLocationsCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
} }
} }
NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope)); 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 context;
delete work; delete work;
}); });
@ -173,12 +168,8 @@ void CachedLocationsCallbackNapi::DeleteHandler()
LBSLOGE(CACHED_LOCATIONS_CALLBACK, "handler or env is nullptr."); LBSLOGE(CACHED_LOCATIONS_CALLBACK, "handler or env is nullptr.");
return; return;
} }
uint32_t refCount = INVALID_REF_COUNT; NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
napi_reference_unref(env_, handlerCb_, &refCount); handlerCb_ = nullptr;
if (refCount == 0) {
NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
handlerCb_ = nullptr;
}
} }
} // namespace Location } // namespace Location
} // namespace OHOS } // namespace OHOS

View File

@ -146,11 +146,6 @@ void CountryCodeCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
} }
} }
NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope)); 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 context;
delete work; delete work;
}); });
@ -181,12 +176,8 @@ void CountryCodeCallbackNapi::DeleteHandler()
LBSLOGE(COUNTRY_CODE_CALLBACK, "handler or env is nullptr."); LBSLOGE(COUNTRY_CODE_CALLBACK, "handler or env is nullptr.");
return; return;
} }
uint32_t refCount = INVALID_REF_COUNT; NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
napi_reference_unref(env_, handlerCb_, &refCount); handlerCb_ = nullptr;
if (refCount == 0) {
NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
handlerCb_ = nullptr;
}
} }
} // namespace Location } // namespace Location
} // namespace OHOS } // namespace OHOS

View File

@ -25,6 +25,8 @@
namespace OHOS { namespace OHOS {
namespace Location { namespace Location {
static std::mutex g_regCallbackMutex;
static std::vector<napi_ref> g_regHandleCallbacks;
GnssStatusCallbackNapi::GnssStatusCallbackNapi() GnssStatusCallbackNapi::GnssStatusCallbackNapi()
{ {
env_ = nullptr; env_ = nullptr;
@ -63,6 +65,39 @@ int GnssStatusCallbackNapi::OnRemoteRequest(
return 0; return 0;
} }
napi_ref GnssStatusCallbackNapi::GetHandleCb()
{
return handlerCb_;
}
void GnssStatusCallbackNapi::SetHandleCb(const napi_ref& handlerCb)
{
std::unique_lock<std::mutex> guard(g_regCallbackMutex);
handlerCb_ = handlerCb;
g_regHandleCallbacks.emplace_back(handlerCb);
}
bool FindGnssRegCallback(napi_ref cb)
{
std::unique_lock<std::mutex> guard(g_regCallbackMutex);
auto iter = std::find(g_regHandleCallbacks.begin(), g_regHandleCallbacks.end(), cb);
if (iter == g_regHandleCallbacks.end()) {
return false;
}
return true;
}
void DeleteGnssRegCallback(napi_ref cb)
{
std::unique_lock<std::mutex> guard(g_regCallbackMutex);
for (auto iter = g_regHandleCallbacks.begin(); iter != g_regHandleCallbacks.end(); iter++) {
if (*iter == cb) {
iter = g_regHandleCallbacks.erase(iter);
break;
}
}
}
bool GnssStatusCallbackNapi::IsRemoteDied() bool GnssStatusCallbackNapi::IsRemoteDied()
{ {
return remoteDied_; return remoteDied_;
@ -121,6 +156,12 @@ void GnssStatusCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
delete work; delete work;
return; return;
} }
if (!FindGnssRegCallback(context->callback[0])) {
LBSLOGE(GNSS_STATUS_CALLBACK, "no valid callback");
delete context;
delete work;
return;
}
NAPI_CALL_RETURN_VOID(context->env, napi_open_handle_scope(context->env, &scope)); NAPI_CALL_RETURN_VOID(context->env, napi_open_handle_scope(context->env, &scope));
if (scope == nullptr) { if (scope == nullptr) {
LBSLOGE(GNSS_STATUS_CALLBACK, "scope is nullptr"); LBSLOGE(GNSS_STATUS_CALLBACK, "scope is nullptr");
@ -146,11 +187,6 @@ void GnssStatusCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
} }
} }
NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope)); 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 context;
delete work; delete work;
}); });
@ -168,12 +204,9 @@ void GnssStatusCallbackNapi::DeleteHandler()
LBSLOGE(GNSS_STATUS_CALLBACK, "handler or env is nullptr."); LBSLOGE(GNSS_STATUS_CALLBACK, "handler or env is nullptr.");
return; return;
} }
uint32_t refCount = INVALID_REF_COUNT; DeleteGnssRegCallback(handlerCb_);
napi_reference_unref(env_, handlerCb_, &refCount); NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
if (refCount == 0) { handlerCb_ = nullptr;
NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
handlerCb_ = nullptr;
}
} }
} // namespace Location } // namespace Location
} // namespace OHOS } // namespace OHOS

View File

@ -170,11 +170,6 @@ void LocatingRequiredDataCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* w
} }
} }
NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope)); 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 context;
delete work; delete work;
}); });
@ -194,12 +189,8 @@ void LocatingRequiredDataCallbackNapi::DeleteHandler()
LBSLOGE(LOCATING_DATA_CALLBACK, "handler or env is nullptr."); LBSLOGE(LOCATING_DATA_CALLBACK, "handler or env is nullptr.");
return; return;
} }
uint32_t refCount = INVALID_REF_COUNT; NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
napi_reference_unref(env_, handlerCb_, &refCount); handlerCb_ = nullptr;
if (refCount == 0) {
NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
handlerCb_ = nullptr;
}
} }
bool LocatingRequiredDataCallbackNapi::IsSingleLocationRequest() bool LocatingRequiredDataCallbackNapi::IsSingleLocationRequest()

View File

@ -132,11 +132,6 @@ void LocationErrorCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
} }
} }
NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope)); 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 context;
delete work; delete work;
}); });
@ -163,12 +158,8 @@ void LocationErrorCallbackNapi::DeleteHandler()
LBSLOGE(LOCATION_ERR_CALLBACK, "handler or env is nullptr."); LBSLOGE(LOCATION_ERR_CALLBACK, "handler or env is nullptr.");
return; return;
} }
uint32_t refCount = INVALID_REF_COUNT; NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
napi_reference_unref(env_, handlerCb_, &refCount); handlerCb_ = nullptr;
if (refCount == 0) {
NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
handlerCb_ = nullptr;
}
} }
} // namespace Location } // namespace Location
} // namespace OHOS } // namespace OHOS

View File

@ -185,12 +185,6 @@ void LocationGnssGeofenceCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* w
} }
} }
NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope)); 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[SUCCESS_CALLBACK], &refCount);
if (refCount == 0) {
NAPI_CALL_RETURN_VOID(context->env,
napi_delete_reference(context->env, context->callback[SUCCESS_CALLBACK]));
}
delete context; delete context;
delete work; delete work;
}); });
@ -203,12 +197,8 @@ void LocationGnssGeofenceCallbackNapi::DeleteHandler()
LBSLOGE(LOCATION_GNSS_GEOFENCE_CALLBACK, "handler or env is nullptr."); LBSLOGE(LOCATION_GNSS_GEOFENCE_CALLBACK, "handler or env is nullptr.");
return; return;
} }
uint32_t refCount = INVALID_REF_COUNT; NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
napi_reference_unref(env_, handlerCb_, &refCount); handlerCb_ = nullptr;
if (refCount == 0) {
NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
handlerCb_ = nullptr;
}
} }
void LocationGnssGeofenceCallbackNapi::CountDown() void LocationGnssGeofenceCallbackNapi::CountDown()

View File

@ -20,6 +20,8 @@
namespace OHOS { namespace OHOS {
namespace Location { namespace Location {
static std::mutex g_regCallbackMutex;
static std::vector<napi_ref> g_regHandleCallbacks;
LocationSwitchCallbackNapi::LocationSwitchCallbackNapi() LocationSwitchCallbackNapi::LocationSwitchCallbackNapi()
{ {
env_ = nullptr; env_ = nullptr;
@ -57,6 +59,39 @@ int LocationSwitchCallbackNapi::OnRemoteRequest(
return 0; return 0;
} }
napi_ref LocationSwitchCallbackNapi::GetHandleCb()
{
return handlerCb_;
}
void LocationSwitchCallbackNapi::SetHandleCb(const napi_ref& handlerCb)
{
std::unique_lock<std::mutex> guard(g_regCallbackMutex);
handlerCb_ = handlerCb;
g_regHandleCallbacks.emplace_back(handlerCb);
}
bool FindSwitchRegCallback(napi_ref cb)
{
std::unique_lock<std::mutex> guard(g_regCallbackMutex);
auto iter = std::find(g_regHandleCallbacks.begin(), g_regHandleCallbacks.end(), cb);
if (iter == g_regHandleCallbacks.end()) {
return false;
}
return true;
}
void DeleteSwitchRegCallback(napi_ref cb)
{
std::unique_lock<std::mutex> guard(g_regCallbackMutex);
for (auto iter = g_regHandleCallbacks.begin(); iter != g_regHandleCallbacks.end(); iter++) {
if (*iter == cb) {
iter = g_regHandleCallbacks.erase(iter);
break;
}
}
}
bool LocationSwitchCallbackNapi::IsRemoteDied() bool LocationSwitchCallbackNapi::IsRemoteDied()
{ {
return remoteDied_; return remoteDied_;
@ -113,12 +148,18 @@ void LocationSwitchCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
SwitchAsyncContext *context = nullptr; SwitchAsyncContext *context = nullptr;
napi_handle_scope scope = nullptr; napi_handle_scope scope = nullptr;
if (work == nullptr) { if (work == nullptr) {
LBSLOGE(LOCATOR_CALLBACK, "work is nullptr!"); LBSLOGE(SWITCH_CALLBACK, "work is nullptr!");
return; return;
} }
context = static_cast<SwitchAsyncContext *>(work->data); context = static_cast<SwitchAsyncContext *>(work->data);
if (context == nullptr || context->env == nullptr) { if (context == nullptr || context->env == nullptr) {
LBSLOGE(LOCATOR_CALLBACK, "context is nullptr!"); LBSLOGE(SWITCH_CALLBACK, "context is nullptr!");
delete work;
return;
}
if (!FindSwitchRegCallback(context->callback[0])) {
LBSLOGE(SWITCH_CALLBACK, "no valid callback");
delete context;
delete work; delete work;
return; return;
} }
@ -145,11 +186,6 @@ void LocationSwitchCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
} }
} }
NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope)); 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 context;
delete work; delete work;
}); });
@ -168,12 +204,9 @@ void LocationSwitchCallbackNapi::DeleteHandler()
LBSLOGE(SWITCH_CALLBACK, "handler or env is nullptr."); LBSLOGE(SWITCH_CALLBACK, "handler or env is nullptr.");
return; return;
} }
uint32_t refCount = INVALID_REF_COUNT; DeleteSwitchRegCallback(handlerCb_);
napi_reference_unref(env_, handlerCb_, &refCount); NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
if (refCount == 0) { handlerCb_ = nullptr;
NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
handlerCb_ = nullptr;
}
} }
} // namespace Location } // namespace Location
} // namespace OHOS } // namespace OHOS

View File

@ -33,6 +33,8 @@
namespace OHOS { namespace OHOS {
namespace Location { namespace Location {
static std::mutex g_regCallbackMutex;
static std::vector<napi_ref> g_regHandleCallbacks;
LocatorCallbackNapi::LocatorCallbackNapi() LocatorCallbackNapi::LocatorCallbackNapi()
{ {
env_ = nullptr; env_ = nullptr;
@ -108,6 +110,39 @@ int LocatorCallbackNapi::OnRemoteRequest(uint32_t code,
return 0; return 0;
} }
napi_ref LocatorCallbackNapi::GetHandleCb()
{
return handlerCb_;
}
void LocatorCallbackNapi::SetHandleCb(const napi_ref& handlerCb)
{
std::unique_lock<std::mutex> guard(g_regCallbackMutex);
handlerCb_ = handlerCb;
g_regHandleCallbacks.emplace_back(handlerCb);
}
bool FindRegCallback(napi_ref cb)
{
std::unique_lock<std::mutex> guard(g_regCallbackMutex);
auto iter = std::find(g_regHandleCallbacks.begin(), g_regHandleCallbacks.end(), cb);
if (iter == g_regHandleCallbacks.end()) {
return false;
}
return true;
}
void DeleteRegCallback(napi_ref cb)
{
std::unique_lock<std::mutex> guard(g_regCallbackMutex);
for (auto iter = g_regHandleCallbacks.begin(); iter != g_regHandleCallbacks.end(); iter++) {
if (*iter == cb) {
iter = g_regHandleCallbacks.erase(iter);
break;
}
}
}
void LocatorCallbackNapi::DoSendWork(uv_loop_s*& loop, uv_work_t*& work) void LocatorCallbackNapi::DoSendWork(uv_loop_s*& loop, uv_work_t*& work)
{ {
uv_queue_work(loop, work, [](uv_work_t* work) {}, [](uv_work_t* work, int status) { uv_queue_work(loop, work, [](uv_work_t* work) {}, [](uv_work_t* work, int status) {
@ -125,6 +160,12 @@ void LocatorCallbackNapi::DoSendWork(uv_loop_s*& loop, uv_work_t*& work)
delete work; delete work;
return; return;
} }
if (!FindRegCallback(context->callback[0])) {
LBSLOGE(LOCATOR_CALLBACK, "no valid callback");
delete context;
delete work;
return;
}
napi_open_handle_scope(context->env, &scope); napi_open_handle_scope(context->env, &scope);
if (scope == nullptr) { if (scope == nullptr) {
DELETE_SCOPE_CONTEXT_WORK(context->env, scope, context, work); DELETE_SCOPE_CONTEXT_WORK(context->env, scope, context, work);
@ -149,11 +190,6 @@ void LocatorCallbackNapi::DoSendWork(uv_loop_s*& loop, uv_work_t*& work)
} }
} }
NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope)); 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 context;
delete work; delete work;
}); });
@ -302,26 +338,22 @@ void LocatorCallbackNapi::DeleteHandler()
LBSLOGE(LOCATOR_CALLBACK, "env is nullptr."); LBSLOGE(LOCATOR_CALLBACK, "env is nullptr.");
return; return;
} }
uint32_t refCount = INVALID_REF_COUNT; DeleteRegCallback(handlerCb_);
if (IsSystemGeoLocationApi()) { if (IsSystemGeoLocationApi()) {
napi_reference_unref(env_, successHandlerCb_, &refCount); if (successHandlerCb_ != nullptr) {
if (successHandlerCb_ != nullptr && refCount == 0) {
NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, successHandlerCb_)); NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, successHandlerCb_));
successHandlerCb_ = nullptr; successHandlerCb_ = nullptr;
} }
napi_reference_unref(env_, failHandlerCb_, &refCount); if (failHandlerCb_ != nullptr) {
if (failHandlerCb_ != nullptr && refCount == 0) {
NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, failHandlerCb_)); NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, failHandlerCb_));
failHandlerCb_ = nullptr; failHandlerCb_ = nullptr;
} }
napi_reference_unref(env_, completeHandlerCb_, &refCount); if (completeHandlerCb_ != nullptr) {
if (completeHandlerCb_ != nullptr && refCount == 0) {
NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, completeHandlerCb_)); NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, completeHandlerCb_));
completeHandlerCb_ = nullptr; completeHandlerCb_ = nullptr;
} }
} else { } else {
napi_reference_unref(env_, handlerCb_, &refCount); if (handlerCb_ != nullptr) {
if (handlerCb_ != nullptr && refCount == 0) {
NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_)); NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
handlerCb_ = nullptr; handlerCb_ = nullptr;
} }

View File

@ -153,11 +153,6 @@ void NmeaMessageCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
} }
} }
NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope)); 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 context;
delete work; delete work;
}); });
@ -176,12 +171,8 @@ void NmeaMessageCallbackNapi::DeleteHandler()
LBSLOGE(NMEA_MESSAGE_CALLBACK, "handler or env is nullptr."); LBSLOGE(NMEA_MESSAGE_CALLBACK, "handler or env is nullptr.");
return; return;
} }
uint32_t refCount = INVALID_REF_COUNT; NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
napi_reference_unref(env_, handlerCb_, &refCount); handlerCb_ = nullptr;
if (refCount == 0) {
NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
handlerCb_ = nullptr;
}
} }
} // namespace Location } // namespace Location
} // namespace OHOS } // namespace OHOS

View File

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

View File

@ -57,8 +57,6 @@ public:
LBSLOGE(LOCATION_GNSS_GEOFENCE_CALLBACK, "context == nullptr."); LBSLOGE(LOCATION_GNSS_GEOFENCE_CALLBACK, "context == nullptr.");
return false; return false;
} }
uint32_t refCount = INVALID_REF_COUNT;
napi_reference_ref(env_, handlerCb_, &refCount);
context->env = env_; context->env = env_;
context->callback[SUCCESS_CALLBACK] = handlerCb_; context->callback[SUCCESS_CALLBACK] = handlerCb_;
return true; return true;