From f197de534e8400210a36450d22e8454f5d7e7f90 Mon Sep 17 00:00:00 2001 From: zhong_ning Date: Sat, 31 Jul 2021 11:14:03 +0800 Subject: [PATCH 1/2] update updaterservice to master Signed-off-by: zhong_ning --- client/update_client.cpp | 23 +++++++--- client/update_client.h | 4 ++ client/update_module.cpp | 8 ++-- client/update_session.cpp | 38 +++++++++++------ client/update_session.h | 14 +++---- engine/include/progress_thread.h | 14 +++---- engine/src/progress_thread.cpp | 42 +++++++++++-------- engine/src/update_service.cpp | 3 -- .../engine/update_service_kits_impl.cpp | 3 ++ .../kits/js/declaration/@ohos.update.d.ts | 2 +- 10 files changed, 94 insertions(+), 57 deletions(-) diff --git a/client/update_client.cpp b/client/update_client.cpp index 8249a603..e69d9552 100644 --- a/client/update_client.cpp +++ b/client/update_client.cpp @@ -75,7 +75,9 @@ UpdateClient::~UpdateClient() UpdateSession *UpdateClient::RemoveSession(uint32_t sessionId) { CLIENT_LOGI("RemoveSession sess"); +#ifndef UPDATER_API_TEST std::lock_guard guard(sessionMutex_); +#endif UpdateSession *sess = nullptr; auto iter = sessions_.find(sessionId); if (iter != sessions_.end()) { @@ -88,7 +90,9 @@ UpdateSession *UpdateClient::RemoveSession(uint32_t sessionId) void UpdateClient::AddSession(std::shared_ptr session) { CLIENT_CHECK(session != nullptr, return, "Invalid param"); +#ifndef UPDATER_API_TEST std::lock_guard guard(sessionMutex_); +#endif sessions_.insert(make_pair(session->GetSessionId(), session)); } @@ -176,7 +180,7 @@ napi_value UpdateClient::GetUpdater(napi_env env, napi_callback_info info, int32 napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); CLIENT_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info"); CLIENT_CHECK_NAPI_CALL(env, argc >= 1, return nullptr, "Invalid param"); - CLIENT_CHECK_NAPI_CALL(env, !isInit, return nullptr, "Has beed init"); + CLIENT_CHECK_NAPI_CALL(env, !isInit, return result, "Has beed init"); int ret = GetStringValue(env, args[0], context_.upgradeFile); CLIENT_CHECK_NAPI_CALL(env, ret == napi_ok && CheckUpgradeFile(context_.upgradeFile), @@ -201,7 +205,7 @@ napi_value UpdateClient::GetUpdater(napi_env env, napi_callback_info info, int32 }; UpdateServiceKits::GetInstance().RegisterUpdateCallback(context_, callback); isInit = true; - return nullptr; + return result; } napi_value UpdateClient::StartSession(napi_env env, @@ -212,7 +216,8 @@ napi_value UpdateClient::StartSession(napi_env env, napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); CLIENT_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info"); - CLIENT_LOGE("StartSession type %d argc %zu callbackStartIndex %d", type, argc, callbackStartIndex); + CLIENT_LOGE("StartSession type %d argc %zu callbackStartIndex %d", type, argc, + static_cast(callbackStartIndex)); std::shared_ptr sess = nullptr; if (argc > callbackStartIndex) { sess = std::make_shared(this, type, argc, 1); @@ -243,7 +248,7 @@ napi_value UpdateClient::CancelUpgrade(napi_env env, napi_callback_info info) size_t argc = MAX_ARGC; napi_value args[MAX_ARGC] = {0}; napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); - CLIENT_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Error get cb info"); + CLIENT_CHECK_NAPI_CALL(env, status == napi_ok && argc == 0, return nullptr, "Error get cb info"); CLIENT_LOGE("CancelUpgrade"); std::shared_ptr sess = nullptr; sess = std::make_shared(this, SESSION_CANCEL_UPGRADE, argc, 0); @@ -360,7 +365,9 @@ napi_value UpdateClient::ApplyNewVersion(napi_env env, napi_callback_info info) { napi_value retValue = StartSession(env, info, SESSION_APPLY_NEW_VERSION, 0, [&](int32_t type, void *context) -> int { +#ifndef UPDATER_API_TEST result_ = UpdateServiceKits::GetInstance().RebootAndInstall(MISC_FILE, UPDATER_PKG_NAME); + #endif return result_; }); CLIENT_CHECK(retValue != nullptr, return nullptr, "Failed to GetNewVersionInfo."); @@ -371,7 +378,9 @@ napi_value UpdateClient::RebootAndClean(napi_env env, napi_callback_info info) { napi_value retValue = StartSession(env, info, SESSION_REBOOT_AND_CLEAN, 0, [&](int32_t type, void *context) -> int { +#ifndef UPDATER_API_TEST result_ = UpdateServiceKits::GetInstance().RebootAndClean(MISC_FILE, CMD_WIPE_DATA); +#endif return result_; }); CLIENT_CHECK(retValue != nullptr, return nullptr, "Failed to GetNewVersionInfo."); @@ -445,7 +454,7 @@ napi_value UpdateClient::UnsubscribeEvent(napi_env env, napi_callback_info info) int ret = UpdateClient::GetStringValue(env, args[0], eventType); CLIENT_CHECK_NAPI_CALL(env, ret == napi_ok, return nullptr, "Failed to get event type"); - CLIENT_LOGI("UnsubscribeEvent %s argc %d", eventType.c_str(), argc); + CLIENT_LOGI("UnsubscribeEvent %s argc %d", eventType.c_str(), static_cast(argc)); if (argc >= MID_ARGC) { napi_valuetype valuetype; napi_status status = napi_typeof(env, args[1], &valuetype); @@ -518,7 +527,9 @@ UpdateSession *UpdateClient::FindSessionByHandle(napi_env env, const std::string bool UpdateClient::GetNextSessionId(uint32_t &sessionId) { +#ifndef UPDATER_API_TEST std::lock_guard guard(sessionMutex_); +#endif { auto iter = sessions_.find(sessionId); if (iter == sessions_.end()) { @@ -535,7 +546,9 @@ bool UpdateClient::GetNextSessionId(uint32_t &sessionId) bool UpdateClient::GetFirstSessionId(uint32_t &sessionId) { +#ifndef UPDATER_API_TEST std::lock_guard guard(sessionMutex_); +#endif { if (sessions_.empty()) { return false; diff --git a/client/update_client.h b/client/update_client.h index 07abab07..dcb56648 100644 --- a/client/update_client.h +++ b/client/update_client.h @@ -148,7 +148,9 @@ public: #ifdef UPDATER_UT UpdateSession *GetUpdateSession(uint32_t sessId) { + #ifndef UPDATER_API_TEST std::lock_guard guard(sessionMutex_); + #endif auto iter = sessions_.find(sessId); if (iter != sessions_.end()) { return iter->second.get(); @@ -198,7 +200,9 @@ private: napi_env env_ {}; napi_ref thisReference_ {}; std::map> sessions_ {}; +#ifndef UPDATER_API_TEST std::mutex sessionMutex_; +#endif bool isInit = false; int32_t result_ = 0; std::string upgradeFile_; diff --git a/client/update_module.cpp b/client/update_module.cpp index af302cc0..69671972 100644 --- a/client/update_module.cpp +++ b/client/update_module.cpp @@ -83,7 +83,9 @@ napi_value GetUpdater(napi_env env, napi_callback_info info) return obj; } } - return obj; + napi_remove_wrap(env, obj, (void**)&client); + delete client; + return nullptr; } napi_value GetUpdaterForOther(napi_env env, napi_callback_info info) @@ -233,7 +235,7 @@ static napi_value UpdateClientInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("getUpdatePolicy", GetUpdatePolicy), DECLARE_NAPI_FUNCTION("getUpgradeStatus", GetUpgradeStatus), - DECLARE_NAPI_FUNCTION("cancelUpgrade", CancelUpgrade), + DECLARE_NAPI_FUNCTION("cancel", CancelUpgrade), DECLARE_NAPI_FUNCTION("download", DownloadVersion), DECLARE_NAPI_FUNCTION("upgrade", UpgradeVersion), @@ -251,7 +253,7 @@ static napi_value UpdateClientInit(napi_env env, napi_value exports) napi_set_named_property(env, exports, CLASS_NAME.c_str(), result); napi_status status = napi_create_reference(env, result, REF_COUNT, &g_reference); CLIENT_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "Failed to create_reference"); - + CLIENT_LOGI("UpdateClient g_reference %p", g_reference); return exports; } diff --git a/client/update_session.cpp b/client/update_session.cpp index e15b427d..0ad9042f 100644 --- a/client/update_session.cpp +++ b/client/update_session.cpp @@ -14,16 +14,11 @@ */ #include "update_session.h" - -#include -#include -#include -#include #include #include +#include #include #include -#include #include #include "node_api.h" @@ -48,7 +43,7 @@ int32_t UpdateSession::CreateReference(napi_env env, napi_value arg, uint32_t re napi_valuetype valuetype; napi_status status = napi_typeof(env, arg, &valuetype); CLIENT_CHECK(status == napi_ok, return status, "Failed to napi_typeof"); - CLIENT_CHECK(valuetype == napi_function, return status, "Invalid callback type"); + CLIENT_CHECK(valuetype == napi_function, return -1, "Invalid callback type"); status = napi_create_reference(env, arg, refcount, &reference); CLIENT_CHECK(status == napi_ok, return status, "Failed to create reference"); @@ -102,7 +97,7 @@ napi_value UpdateAsyncession::StartWork(napi_env env, size_t startIndex, const n // Check whether a callback exists. Only one callback is allowed. for (size_t i = 0; (i < (totalArgc_ - startIndex)) && (i < callbackNumber_); i++) { - CLIENT_LOGI("CreateReference index:%u", i + startIndex); + CLIENT_LOGI("CreateReference index:%u", static_cast(i + startIndex)); int32_t ret = CreateReference(env, args[i + startIndex], 1, callbackRef_[i]); CLIENT_CHECK_NAPI_CALL(env, ret == napi_ok, return nullptr, "Failed to create reference"); } @@ -210,23 +205,40 @@ napi_value UpdateListener::StartWork(napi_env env, size_t startIndex, const napi return result; } -void UpdateListener::NotifyJS(napi_env env, napi_value thisVar, int32_t retcode, const UpdateResult &result) const +void UpdateListener::NotifyJS(napi_env env, napi_value thisVar, int32_t retcode, const UpdateResult &result) { CLIENT_LOGI("NotifyJS retcode:%d", retcode); napi_value jsEvent; - result.buildJSObject(env, jsEvent, result); napi_value handler = nullptr; napi_value callResult; - napi_get_reference_value(env, handlerRef_, &handler); + int32_t ret = result.buildJSObject(env, jsEvent, result); + CLIENT_CHECK_NAPI_CALL(env, ret == napi_ok, return, "Failed to build json"); + { + std::unique_lock lock(mutex_); + CLIENT_CHECK_NAPI_CALL(env, handlerRef_ != nullptr, return, "handlerRef_ has beed freed"); + 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"); napi_call_function(env, thisVar, handler, 1, &jsEvent, &callResult); } -bool UpdateListener::CheckEqual(napi_env env, napi_value handler, const std::string &type) const +bool UpdateListener::CheckEqual(napi_env env, napi_value handler, const std::string &type) { + std::unique_lock lock(mutex_); bool isEquals = false; napi_value handlerTemp = nullptr; - napi_get_reference_value(env, handlerRef_, &handlerTemp); + napi_status status = napi_get_reference_value(env, handlerRef_, &handlerTemp); + CLIENT_CHECK_NAPI_CALL(env, status == napi_ok, return false, "Failed to get reference"); napi_strict_equals(env, handler, handlerTemp, &isEquals); return isEquals && (type.compare(eventType_) == 0); } + +void UpdateListener::RemoveHandlerRef(napi_env env) +{ + std::unique_lock lock(mutex_); + CLIENT_LOGI("RemoveHandlerRef handlerRef_:%{public}p %{public}u", handlerRef_, GetSessionId()); + napi_delete_reference(env, handlerRef_); + handlerRef_ = nullptr; +} } // namespace updateClient diff --git a/client/update_session.h b/client/update_session.h index 99d92ec8..cb8f64bd 100644 --- a/client/update_session.h +++ b/client/update_session.h @@ -19,7 +19,9 @@ #include #include #include +#include #include +#include #include #include "iupdate_service.h" @@ -60,7 +62,7 @@ public: virtual void CompleteWork(napi_env env, napi_status status) {} virtual void ExecuteWork(napi_env env); virtual napi_value StartWork(napi_env env, size_t startIndex, const napi_value *args) = 0; - virtual void NotifyJS(napi_env env, napi_value thisVar, int32_t retcode, const UpdateResult &result) const + virtual void NotifyJS(napi_env env, napi_value thisVar, int32_t retcode, const UpdateResult &result) { return; } @@ -153,7 +155,7 @@ public: napi_value StartWork(napi_env env, size_t startIndex, const napi_value *args) override; - void NotifyJS(napi_env env, napi_value thisVar, int32_t retcode, const UpdateResult &result) const override; + void NotifyJS(napi_env env, napi_value thisVar, int32_t retcode, const UpdateResult &result) override; bool IsOnce() const { @@ -165,16 +167,14 @@ public: return eventType_; } - bool CheckEqual(napi_env env, napi_value handler, const std::string &type) const; + bool CheckEqual(napi_env env, napi_value handler, const std::string &type); - void RemoveHandlerRef(napi_env env) - { - napi_delete_reference(env, handlerRef_); - } + void RemoveHandlerRef(napi_env env); private: bool isOnce_ = false; std::string eventType_; napi_ref handlerRef_ = nullptr; + std::mutex mutex_; }; } // namespace updateClient #endif // UPDATE_SESSION_H diff --git a/engine/include/progress_thread.h b/engine/include/progress_thread.h index 19d5e525..d8a54482 100755 --- a/engine/include/progress_thread.h +++ b/engine/include/progress_thread.h @@ -50,14 +50,11 @@ constexpr int32_t TIMEOUT_FOR_CONNECT = 1; class ProgressThread { public: ProgressThread() = default; - virtual ~ProgressThread() - { - StopProgress(); - } - + virtual ~ProgressThread(); protected: int32_t StartProgress(); void StopProgress(); + void ExitThread(); void ExecuteThreadFunc(); virtual bool ProcessThreadExecute() = 0; @@ -75,7 +72,10 @@ class DownloadThread : public ProgressThread { public: using ProgressCallback = std::function; DownloadThread(ProgressCallback callback) : ProgressThread(), callback_(callback) {} - ~DownloadThread() override {} + ~DownloadThread() override + { + ProgressThread::ExitThread(); + } int32_t StartDownload(const std::string &fileName, const std::string &url); void StopDownload(); @@ -106,4 +106,4 @@ private: }; } } // namespace OHOS -#endif // UPDATER_THREAD_H \ No newline at end of file +#endif // UPDATER_THREAD_H diff --git a/engine/src/progress_thread.cpp b/engine/src/progress_thread.cpp index 316dce89..6c1a11c5 100755 --- a/engine/src/progress_thread.cpp +++ b/engine/src/progress_thread.cpp @@ -22,14 +22,31 @@ namespace OHOS { namespace update_engine { +ProgressThread::~ProgressThread() {} + +void ProgressThread::ExitThread() +{ + { + std::unique_lock lock(mutex_); + isWake_ = true; + isExitThread_ = true; + condition_.notify_one(); + } + if (pDealThread_ != nullptr) { + pDealThread_->join(); + delete pDealThread_; + pDealThread_ = nullptr; + } +} + int32_t ProgressThread::StartProgress() { + std::unique_lock lock(mutex_); if (pDealThread_ == nullptr) { pDealThread_ = new (std::nothrow)std::thread(&ProgressThread::ExecuteThreadFunc, this); ENGINE_CHECK(pDealThread_ != nullptr, return -1, "Failed to create thread"); } ENGINE_LOGI("StartProgress"); - std::unique_lock lock(mutex_); isWake_ = true; condition_.notify_one(); return 0; @@ -37,18 +54,10 @@ int32_t ProgressThread::StartProgress() void ProgressThread::StopProgress() { - { - std::unique_lock lock(mutex_); - isWake_ = true; - isExitThread_ = true; - condition_.notify_one(); - } - - if (pDealThread_ != nullptr) { - pDealThread_->join(); - delete pDealThread_; - pDealThread_ = nullptr; - } + std::unique_lock lock(mutex_); + isWake_ = true; + isExitThread_ = false; + condition_.notify_one(); } void ProgressThread::ExecuteThreadFunc() @@ -65,10 +74,7 @@ void ProgressThread::ExecuteThreadFunc() } isWake_ = false; } - bool isExit = ProcessThreadExecute(); - if (isExit) { - break; - } + ProcessThreadExecute(); } // thread exit and release resource ProcessThreadExit(); @@ -205,4 +211,4 @@ size_t DownloadThread::GetLocalFileLength(const std::string &fileName) return length; } } -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/engine/src/update_service.cpp b/engine/src/update_service.cpp index 5c848a29..f275cbbc 100644 --- a/engine/src/update_service.cpp +++ b/engine/src/update_service.cpp @@ -462,9 +462,6 @@ int32_t UpdateService::Cancel(int32_t service) ENGINE_LOGI("Cancel %d", service); if (downloadThread_ != nullptr && service == DOWNLOAD) { downloadThread_->StopDownload(); - ENGINE_LOGI("StopDownload"); - delete downloadThread_; - downloadThread_ = nullptr; } return 0; } diff --git a/interfaces/innerkits/engine/update_service_kits_impl.cpp b/interfaces/innerkits/engine/update_service_kits_impl.cpp index 1fc6001c..6f983fa0 100755 --- a/interfaces/innerkits/engine/update_service_kits_impl.cpp +++ b/interfaces/innerkits/engine/update_service_kits_impl.cpp @@ -218,7 +218,10 @@ int32_t UpdateServiceKitsImpl::RebootAndClean(const std::string &miscFile, const ENGINE_LOGI("UpdateServiceKitsImpl::RebootAndCleanUserData"); auto updateService = GetService(); ENGINE_CHECK(updateService != nullptr, return -1, "Get updateService failed"); +#ifndef UPDATER_API_TEST return updateService->RebootAndClean(miscFile, cmd); +#endif + return 1; } int32_t UpdateServiceKitsImpl::RebootAndInstall(const std::string &miscFile, const std::string &packageName) diff --git a/interfaces/kits/js/declaration/@ohos.update.d.ts b/interfaces/kits/js/declaration/@ohos.update.d.ts index b516414d..3282d89b 100644 --- a/interfaces/kits/js/declaration/@ohos.update.d.ts +++ b/interfaces/kits/js/declaration/@ohos.update.d.ts @@ -406,7 +406,7 @@ declare namespace update { off(eventType: 'upgradeProgress', callback?: UpdateProgressCallback): void; off(eventType: 'verifyProgress', callback?: UpdateProgressCallback): void; - /** + /** * cancel download packages for the device. * * @since 6 -- Gitee From 3b0153e3209eab04981b069160d815c7ae439299 Mon Sep 17 00:00:00 2001 From: zhong_ning Date: Sat, 31 Jul 2021 11:16:33 +0800 Subject: [PATCH 2/2] fix code style Signed-off-by: zhong_ning --- client/update_client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/update_client.cpp b/client/update_client.cpp index e69d9552..dbce456d 100644 --- a/client/update_client.cpp +++ b/client/update_client.cpp @@ -367,7 +367,7 @@ napi_value UpdateClient::ApplyNewVersion(napi_env env, napi_callback_info info) [&](int32_t type, void *context) -> int { #ifndef UPDATER_API_TEST result_ = UpdateServiceKits::GetInstance().RebootAndInstall(MISC_FILE, UPDATER_PKG_NAME); - #endif +#endif return result_; }); CLIENT_CHECK(retValue != nullptr, return nullptr, "Failed to GetNewVersionInfo."); -- Gitee