diff --git a/client/update_client.cpp b/client/update_client.cpp index 47a2c7f61dac29544cac82fb34bfc87dae792746..a6378dfffdd277e54b04de098affae7c997f9bf6 100644 --- a/client/update_client.cpp +++ b/client/update_client.cpp @@ -26,7 +26,9 @@ #include #include #include +#include #include +#include #include #include "iupdate_service.h" @@ -52,6 +54,16 @@ const std::string MISC_FILE = "/dev/block/platform/soc/10100000.himci.eMMC/by-na const std::string CMD_WIPE_DATA = "--user_wipe_data"; const std::string UPDATER_PKG_NAME = "/data/updater/updater.zip"; +struct NotifyInput { + NotifyInput() = default; + NotifyInput(UpdateClient* iclient, const string &itype, int32_t iretcode, UpdateResult *iresult) + :client(iclient), type(itype), retcode(iretcode), result(iresult) {} + UpdateClient* client; + string type; + int32_t retcode; + UpdateResult *result; +}; + UpdateClient::UpdateClient(napi_env env, napi_value thisVar) { thisReference_ = nullptr; @@ -558,7 +570,7 @@ bool UpdateClient::GetFirstSessionId(uint32_t &sessionId) } } -void UpdateClient::Emit(const std::string &type, int32_t retcode, const UpdateResult &result) +void UpdateClient::PublishToJS(const std::string &type, int32_t retcode, const UpdateResult &result) { napi_handle_scope scope; napi_status status = napi_open_handle_scope(env_, &scope); @@ -594,6 +606,52 @@ void UpdateClient::Emit(const std::string &type, int32_t retcode, const UpdateRe napi_close_handle_scope(env_, scope); } +void UpdateClient::Emit(const std::string &type, int32_t retCode, const UpdateResult &result) +{ + auto freeUpdateResult = [](const UpdateResult *lres) { + if (lres != nullptr) { + delete lres->result.progress; + } + delete lres; + }; + UpdateResult *res = new(std::nothrow) UpdateResult(); + CLIENT_CHECK(res != nullptr, return, "copy update result failed."); + res->type = result.type; + res->buildJSObject = result.buildJSObject; + res->result.progress = new(std::nothrow) Progress(); + CLIENT_CHECK(res != nullptr, + freeUpdateResult(res); + return, "copy update result failed."); + res->result.progress->status = result.result.progress->status; + res->result.progress->endReason = result.result.progress->endReason; + res->result.progress->percent = result.result.progress->percent; + + uv_loop_s *loop = nullptr; + napi_get_uv_event_loop(env_, &loop); + CLIENT_CHECK(loop != nullptr, + freeUpdateResult(res); + return, "get event loop failed."); + uv_work_t *work = new(std::nothrow) uv_work_t; + CLIENT_CHECK(work != nullptr, freeUpdateResult(res); return, "alloc work failed."); + work->data = (void*)new(std::nothrow) NotifyInput(this, type, retCode, res); + CLIENT_CHECK(work != nullptr, + freeUpdateResult(res); + delete work; + return, "alloc work data failed."); + + uv_queue_work( + loop, + work, + [](uv_work_t *work) {}, // run in C++ thread + [](uv_work_t *work, int status) { + NotifyInput *input = (NotifyInput *)work->data; + input->client->PublishToJS(input->type, input->retCode, *input->result); + delete input->result->result.progress; + delete input->result; + delete input; + }); +} + void UpdateClient::NotifyDownloadProgress(const Progress &progress) { CLIENT_LOGI("NotifyDownloadProgress status %d %d", progress.status, progress.percent); diff --git a/client/update_client.h b/client/update_client.h index dcb5664858353c436bea42e81ad973e7c1c81958..bc7a1bb10017db79d0baecc0ec8567a424c34d9d 100644 --- a/client/update_client.h +++ b/client/update_client.h @@ -134,6 +134,7 @@ public: UpdateSession *RemoveSession(uint32_t sessionId); void AddSession(std::shared_ptr session); + void PublishToJS(const std::string &type, int32_t retCode, const UpdateResult &result); void Emit(const std::string &type, int32_t retCode, const UpdateResult &result); static int32_t GetStringValue(napi_env env, napi_value arg, std::string &strValue); diff --git a/client/update_session.cpp b/client/update_session.cpp index 0ad9042fb23ca40339856e917de421284eb7fddf..0a2950c8023247cedd3f30f106525b35d3a571ab 100644 --- a/client/update_session.cpp +++ b/client/update_session.cpp @@ -219,7 +219,7 @@ void UpdateListener::NotifyJS(napi_env env, napi_value thisVar, int32_t retcode, napi_status status = napi_get_reference_value(env, handlerRef_, &handler); CLIENT_CHECK_NAPI_CALL(env, status == napi_ok && handler != nullptr, return, "Failed to get reference"); } - CLIENT_CHECK_NAPI_CALL(env, handler != nullptr, return, "handlerRef_ has beed freed"); + CLIENT_CHECK_NAPI_CALL(env, handler != nullptr, return, "handlerRef_ has beed freed"); napi_call_function(env, thisVar, handler, 1, &jsEvent, &callResult); }