diff --git a/bundle.json b/bundle.json index 10d428cf5d7fb0f3ae56e184a9d2c738916add5c..3c93d13e7d1bdef9342dff285a9f805fd233675a 100644 --- a/bundle.json +++ b/bundle.json @@ -58,6 +58,7 @@ "build": { "modules": [ "//base/update/updateservice/frameworks/js/napi/update:update", + "//base/update/updateservice/frameworks/js/napi/session:update_session", "//base/update/updateservice/interfaces/inner_api/engine:updateservicekits", "//base/update/updateservice/interfaces/inner_api/modulemgr:update_module_mgr", "//base/update/updateservice/services/engine:dupdate_config.json", @@ -86,6 +87,13 @@ "header_files": [] }, "name": "//base/update/updateservice/interfaces/inner_api/modulemgr:update_module_mgr" + }, + { + "header": { + "header_base": "//base/update/updateservice/frameworks/js/napi/session/include", + "header_files": [] + }, + "name": "//base/update/updateservice/frameworks/js/napi/session:update_session" } ], "test": [ diff --git a/frameworks/js/napi/session/BUILD.gn b/frameworks/js/napi/session/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..4539c68613d4c986a9f3858ae33941009f10a47d --- /dev/null +++ b/frameworks/js/napi/session/BUILD.gn @@ -0,0 +1,36 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("../../../js/napi/session/update_session.gni") + +config("update_session_library_native_config") { + include_dirs = session_include_dirs +} + +ohos_shared_library("update_session") { + sources = session_sources + include_dirs = session_include_dirs + include_dirs += [ + "$root_path/foundations/ability/define/include", + "$root_path/foundations/ability/log/include", + "$root_path/foundations/ability/utils/include", + "$root_path/foundations/model/include", + ] + + external_deps = session_external_deps + part_name = "update_service" + subsystem_name = "updater" + cflags = session_cflags + public_configs = [ ":update_session_library_native_config" ] +} diff --git a/frameworks/js/napi/session/update_session.gni b/frameworks/js/napi/session/update_session.gni index 369c6a9d1009ddd2da97b2acfb59cd5b4ef1c59b..3457fc90aeba9e6916024bcc02499e4f7216f75c 100644 --- a/frameworks/js/napi/session/update_session.gni +++ b/frameworks/js/napi/session/update_session.gni @@ -10,6 +10,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +import("//base/update/updateservice/updateengine.gni") root_path = "//base/update/updateservice" session_sources = [ @@ -18,3 +19,20 @@ session_sources = [ ] session_include_dirs = [ "$root_path/frameworks/js/napi/session/include" ] + +session_external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libtokenid_sdk", + "hilog:libhilog", + "ipc:ipc_core", + "json:nlohmann_json_static", + "napi:ace_napi", +] + +session_cflags = [ + "-fPIC", + "-Os", + "-Werror", + "-DNAPI_VERSION=8", + "-fstack-protector-strong", +] diff --git a/napi/include/client_contxt.h b/napi/include/client_contxt.h deleted file mode 100644 index fdce6cdb7fcbd4f95ec7e533bb933f832f28e563..0000000000000000000000000000000000000000 --- a/napi/include/client_contxt.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef CLIENT_CONTEXT_H -#define CLIENT_CONTEXT_H - -#include - -#include "napi/native_api.h" -#include "napi/native_node_api.h" - -#include "common_error_define.h" - -namespace OHOS::UpdateEngine { -template -struct ClientContext { - typedef napi_value (*GetNapiParam)(napi_env env, napi_callback_info info, std::unique_ptr &clientContext); - typedef void (*GetIpcBusinessError)(const std::string &funcName, int32_t ipcRequestCode, - BusinessError &businessError); - typedef napi_value (*CreateNapiValue)(napi_env env, const T &context); - - ClientContext(std::string method, GetNapiParam getNapiParam, napi_async_execute_callback executeFunc, - std::vector> paramInfos, GetIpcBusinessError getIpcBusinessError) - : method_(std::move(method)), - getNapiParam_(getNapiParam), - executeFunc_(executeFunc), - paramInfos_(std::move(paramInfos)), - getIpcBusinessError_(getIpcBusinessError) - { - ENGINE_LOGD("ClientContext construct"); - } - - ~ClientContext() - { - ENGINE_LOGD("~ClientContext destruct"); - } - - std::string method_; // 执行的接口名 - BusinessError businessError_; - int32_t ipcRequestCode_ = 0; - - GetNapiParam getNapiParam_ = nullptr; // napi获取参数 - CreateNapiValue createValueFunc_ = nullptr; // 通过ipc返回结果, 构建napi结果对象函数 - - napi_async_execute_callback executeFunc_; // 异步执行函数 - napi_ref callbackRef_ = nullptr; // callback 回调 - napi_deferred deferred_ = nullptr; // promise deferred对象 - napi_async_work work_ = nullptr; - - std::vector> paramInfos_; // 入参校验异常返回结果 - GetIpcBusinessError getIpcBusinessError_; // 通过ipc返回异常,构造BusinessError -}; -} // namespace OHOS::UpdateEngine -#endif // CLIENT_CONTEXT_H \ No newline at end of file diff --git a/napi/include/napi_auto_register.h b/napi/include/napi_auto_register.h deleted file mode 100644 index f18f7d49070338fbb9b8171b139eeb3b97fdde83..0000000000000000000000000000000000000000 --- a/napi/include/napi_auto_register.h +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef AUTO_REGISTER_H -#define AUTO_REGISTER_H - -#include -#include -#include -#include - -namespace OHOS::UpdateEngine { -template class Container { -public: - using FuncType = std::function()>; - - ~Container() = default; - // 单例模式 - static Container &Instance() - { - static Container instance; // c++11 静态局部变量保证线程安全 - return instance; - } - - bool RegisterType(uint32_t functionType, FuncType type) - { - if (createMap_.find(functionType) != createMap_.end()) { - return false; - } - return createMap_.emplace(functionType, type).second; - } - - std::shared_ptr GetPtr(uint32_t functionType) - { - if (createMap_.find(functionType) == createMap_.end()) { - return nullptr; - } - FuncType function = createMap_[functionType]; - // 获取容器中对象时实例化 - return function(); - } - -private: - Container() = default; - Container(const Container &) = delete; - Container(Container &&) = delete; - -private: - // 存储注入对象的回调函数 - std::unordered_map createMap_; -}; - -template class NapiAutoRegister { -public: - explicit NapiAutoRegister(uint32_t FuncType) - { - Container::Instance().RegisterType(FuncType, []() { return std::make_shared(); }); - } -}; -} // namespace OHOS::UpdateEngine -#endif // AUTO_REGISTER_H \ No newline at end of file diff --git a/napi/include/napi_base.h b/napi/include/napi_base.h deleted file mode 100644 index 959f10dcb91e9702caaf148b223880bbf5730f9d..0000000000000000000000000000000000000000 --- a/napi/include/napi_base.h +++ /dev/null @@ -1,184 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef NAPI_BASE_H -#define NAPI_BASE_H - -#include - -#include "napi_common_utils.h" -#include "napi/native_api.h" -#include "napi/native_common.h" - -namespace OHOS::UpdateEngine { -template class NapiBase { -#define GET_PARAMS(env, info, num) \ - size_t argc = num; \ - napi_value args[num] = {nullptr}; \ - napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) - -public: - NapiBase() = default; - ~NapiBase() = default; - - static napi_value HandleFunc(napi_env env, napi_callback_info info, - std::unique_ptr &clientContext) - { - if (clientContext == nullptr) { - ENGINE_LOGI("HandleFunc clientContext is null"); - return nullptr; - } - std::string method = clientContext->method_; - ENGINE_LOGI("HandleFunc method: %{public}s", method.c_str()); - napi_value result = clientContext->getNapiParam_(env, info, clientContext); - if (result == nullptr) { - ENGINE_LOGE("HandleFunc GetMigrateStatusParam fail"); - return nullptr; - } - if (!Execute(env, clientContext)) { - ENGINE_LOGE("HandleFunc Execute error"); - return result; - } - ENGINE_LOGI("HandleFunc method: %{public}s complete", method.c_str()); - return result; - } - - static napi_value GetCallbackParam(napi_env env, uint32_t argNum, size_t argc, napi_value args[], - std::unique_ptr &clientContext) - { - // 接口调用返回值,非返回内容 - napi_value result = nullptr; - if (argc >= argNum) { - PARAM_CHECK(argNum >= 1, return nullptr, "argNum is less than 1"); - uint32_t callbackPosition = argNum - 1; - napi_valuetype callbackValueType; - napi_typeof(env, args[callbackPosition], &callbackValueType); - std::vector> paramInfos; - paramInfos.emplace_back("callback", "napi_function"); - PARAM_CHECK(callbackValueType == napi_function, NapiCommonUtils::NapiThrowParamError(env, paramInfos); - return nullptr, "Failed to GetCallbackParam"); - napi_create_reference(env, args[callbackPosition], 1, &clientContext->callbackRef_); - napi_get_undefined(env, &result); // 创建接口返回值对象 - } else { - napi_create_promise(env, &clientContext->deferred_, &result); - } - return result; - } - - static bool Execute(napi_env env, std::unique_ptr &clientContext) - { - napi_value workName; - napi_create_string_utf8(env, clientContext->method_.c_str(), NAPI_AUTO_LENGTH, &workName); - if (napi_create_async_work(env, nullptr, workName, clientContext->executeFunc_, NapiBase::Complete, - static_cast(clientContext.get()), &clientContext->work_) != napi_ok) { - ENGINE_LOGE("Failed to create async work for %{public}s", clientContext->method_.c_str()); - return false; - } - if (napi_queue_async_work_with_qos(env, clientContext->work_, napi_qos_default) != napi_ok) { - ENGINE_LOGE("Failed to queue async work for %{public}s", clientContext->method_.c_str()); - return false; - } - ENGINE_LOGI("Execute finish"); - clientContext.release(); // unique_ptr release之后,释放指针的控制权,后续由complete里面释放指针内容 - return true; - } - - static void Complete(napi_env env, napi_status status, void *data) - { - if (data == nullptr) { - ENGINE_LOGE("Complete, data is null"); - return; - } - constexpr size_t resultLen = 2; - T *clientContext = static_cast(data); - if (clientContext == nullptr) { - ENGINE_LOGE("Complete clientContext is null"); - return; - } - - napi_value finalResult = nullptr; - if (clientContext->createValueFunc_ != nullptr) { - // 执行结果转换函数 - finalResult = clientContext->createValueFunc_(env, *clientContext); - } - - if (clientContext->ipcRequestCode_ != 0) { - // ipc失败,获取失败原因 - clientContext->getIpcBusinessError_(clientContext->method_, clientContext->ipcRequestCode_, - clientContext->businessError_); - } - - napi_value result[resultLen] = { nullptr, nullptr }; - bool isSuccess = BuildResult(env, clientContext, finalResult, result); - if (clientContext->deferred_) { // promise调用 - ExecutePromiseFunc(env, clientContext, result, resultLen, isSuccess); - } else { - ExecuteCallbackFunc(env, clientContext, result, resultLen); - } - napi_delete_async_work(env, clientContext->work_); - delete clientContext; // Execute 中释放控制权的指针,在此处释放 - clientContext = nullptr; - } - - static void ExecutePromiseFunc(napi_env env, T *clientContext, napi_value const * result, size_t len, - bool isSuccess) - { - constexpr size_t resultLength = 2; - if (len < resultLength) { - ENGINE_LOGE("length error:%{public}zu", len); - return; - } - napi_status callbackStatus = isSuccess ? napi_resolve_deferred(env, clientContext->deferred_, result[1]) : - napi_reject_deferred(env, clientContext->deferred_, result[0]); - if (callbackStatus != napi_ok) { - ENGINE_LOGE("ExecutePromiseFunc error: %{public}d", callbackStatus); - } - } - - static void ExecuteCallbackFunc(napi_env env, T *clientContext, napi_value *result, size_t len) - { - napi_value callback = nullptr; - napi_status resultStatus = napi_get_reference_value(env, clientContext->callbackRef_, &callback); - if (resultStatus != napi_ok) { - ENGINE_LOGE("napi_get_reference_value failed result=%{public}d", resultStatus); - return; - } - napi_value userRet = nullptr; - resultStatus = napi_call_function(env, nullptr, callback, len, result, &userRet); - if (resultStatus != napi_ok) { - ENGINE_LOGE("napi_call_function failed result=%{public}d", resultStatus); - return; - } - resultStatus = napi_delete_reference(env, clientContext->callbackRef_); - if (resultStatus != napi_ok) { - ENGINE_LOGE("napi_delete_reference failed result=%{public}d", resultStatus); - } - } - - static bool BuildResult(napi_env env, const T *clientContext, napi_value finalResult, napi_value *result) - { - bool isSuccess = clientContext->businessError_.errorNum == CallResult::SUCCESS; - if (isSuccess) { - napi_get_undefined(env, &result[0]); - result[1] = finalResult; - } else { - NapiCommonUtils::BuildBusinessError(env, result[0], clientContext->businessError_); - napi_get_undefined(env, &result[1]); - } - return isSuccess; - } -}; -} // namespace OHOS::UpdateEngine -#endif // NAPI_BASE_H \ No newline at end of file diff --git a/napi/include/napi_client_interface.h b/napi/include/napi_client_interface.h deleted file mode 100644 index 8e97756ce6e05c0fb18b5881eb4d19477b097e17..0000000000000000000000000000000000000000 --- a/napi/include/napi_client_interface.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef NAPI_CLIENT_INTERFACE_H -#define NAPI_CLIENT_INTERFACE_H - -#include - -#include "napi/native_api.h" -#include "napi/native_node_api.h" - -namespace OHOS::UpdateEngine { -class NapiClientInterface { -public: - virtual ~NapiClientInterface() = default; - virtual napi_value HandleFunc(napi_env env, napi_callback_info info) = 0; -}; -} // OHOS::UpdateEngine -#endif // NAPI_CLIENT_INTERFACE_H \ No newline at end of file