!831 修改napi crash

Merge pull request !831 from mashaoyin/master
This commit is contained in:
openharmony_ci 2023-08-11 11:18:24 +00:00 committed by Gitee
commit 0e8a18a6dc
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 30 additions and 6 deletions

View File

@ -27,7 +27,10 @@ config("inputmethod_js_common_config") {
config("inputmethod_js_common_public_config") {
visibility = [ "./*" ]
include_dirs = [ "./" ]
include_dirs = [
"./",
"${inputmethod_path}/frameworks/common",
]
}
ohos_static_library("inputmethod_js_common") {

View File

@ -13,10 +13,13 @@
* limitations under the License.
*/
#include <uv.h>
#include "js_callback_object.h"
namespace OHOS {
namespace MiscServices {
constexpr int32_t MAX_TIMEOUT = 2000;
JSCallbackObject::JSCallbackObject(napi_env env, napi_value callback, std::thread::id threadId)
: env_(env), threadId_(threadId)
{
@ -26,7 +29,26 @@ JSCallbackObject::JSCallbackObject(napi_env env, napi_value callback, std::threa
JSCallbackObject::~JSCallbackObject()
{
if (callback_ != nullptr) {
napi_delete_reference(env_, callback_);
if (threadId_ == std::this_thread::get_id()) {
napi_delete_reference(env_, callback_);
env_ = nullptr;
return;
}
std::shared_ptr<uv_work_t> work = std::make_shared<uv_work_t>();
isDone_ = std::make_shared<BlockData<bool>>(MAX_TIMEOUT, false);
work->data = this;
uv_loop_s *loop = nullptr;
napi_get_uv_event_loop(env_, &loop);
uv_queue_work_with_qos(
loop, work.get(), [](uv_work_t *work) {},
[](uv_work_t *work, int status) {
JSCallbackObject *jsObject = static_cast<JSCallbackObject *>(work->data);
napi_delete_reference(jsObject->env_, jsObject->callback_);
bool isFinish = true;
jsObject->isDone_->SetValue(isFinish);
},
uv_qos_user_initiated);
isDone_->GetValue();
}
env_ = nullptr;
}

View File

@ -17,6 +17,7 @@
#include <thread>
#include "block_data.h"
#include "napi/native_api.h"
#include "napi/native_node_api.h"
@ -29,6 +30,7 @@ public:
napi_ref callback_ = nullptr;
napi_env env_{};
std::thread::id threadId_;
std::shared_ptr<BlockData<bool>> isDone_;
};
} // namespace MiscServices
} // namespace OHOS

View File

@ -35,12 +35,10 @@ std::shared_ptr<PanelListenerImpl> PanelListenerImpl::GetInstance()
PanelListenerImpl::~PanelListenerImpl()
{
env_ = nullptr;
}
void PanelListenerImpl::SaveInfo(napi_env env, const std::string &type, napi_value callback, uint32_t windowId)
{
env_ = env;
std::shared_ptr<JSCallbackObject> cbObject =
std::make_shared<JSCallbackObject>(env, callback, std::this_thread::get_id());
auto result = callbacks_.Find(windowId);
@ -93,7 +91,7 @@ void PanelListenerImpl::OnPanelStatus(uint32_t windowId, bool isShow)
}
work->data = new (std::nothrow) UvEntry(callback.second);
uv_loop_s *loop = nullptr;
napi_get_uv_event_loop(env_, &loop);
napi_get_uv_event_loop(callback.second->env_, &loop);
uv_queue_work_with_qos(
loop, work, [](uv_work_t *work) {},
[](uv_work_t *work, int status) {

View File

@ -40,7 +40,6 @@ public:
std::shared_ptr<JSCallbackObject> cbCopy;
explicit UvEntry(const std::shared_ptr<JSCallbackObject> &cb) : cbCopy(cb) {}
};
napi_env env_ = nullptr;
ConcurrentMap<uint32_t, ConcurrentMap<std::string, std::shared_ptr<JSCallbackObject>>> callbacks_;
static std::mutex listenerMutex_;
static std::shared_ptr<PanelListenerImpl> instance_;