diff --git a/frameworks/native/ability/native/child_process_manager/child_process.cpp b/frameworks/native/ability/native/child_process_manager/child_process.cpp index dc116d7055..af31d41c2b 100644 --- a/frameworks/native/ability/native/child_process_manager/child_process.cpp +++ b/frameworks/native/ability/native/child_process_manager/child_process.cpp @@ -32,9 +32,10 @@ std::shared_ptr ChildProcess::Create(const std::unique_ptr &info) +bool ChildProcess::Init(const std::shared_ptr &info) { processStartInfo_ = info; + return true; } void ChildProcess::OnStart() {} diff --git a/frameworks/native/ability/native/child_process_manager/child_process_manager.cpp b/frameworks/native/ability/native/child_process_manager/child_process_manager.cpp index 933f647392..496834ff58 100644 --- a/frameworks/native/ability/native/child_process_manager/child_process_manager.cpp +++ b/frameworks/native/ability/native/child_process_manager/child_process_manager.cpp @@ -81,6 +81,10 @@ ChildProcessManagerErrorCode ChildProcessManager::StartChildProcessBySelfFork(co } std::shared_ptr applicationContext = AbilityRuntime::ApplicationContext::GetInstance(); + if (applicationContext == nullptr) { + HILOG_ERROR("Get applicationContext failed."); + return ChildProcessManagerErrorCode::ERR_GET_APPLICATION_CONTEXT_FAILED; + } std::string bundleName = applicationContext->GetBundleName(); AppExecFwk::HapModuleInfo hapModuleInfo; if (!GetHapModuleInfo(bundleName, hapModuleInfo)) { @@ -129,6 +133,10 @@ bool ChildProcessManager::IsChildProcess() void ChildProcessManager::HandleChildProcess(const std::string &srcEntry, AppExecFwk::HapModuleInfo &hapModuleInfo) { std::shared_ptr eventRunner = AppExecFwk::EventRunner::GetMainEventRunner(); + if (eventRunner == nullptr) { + HILOG_ERROR("Get main eventRunner failed."); + return; + } eventRunner->Stop(); auto runtime = CreateRuntime(hapModuleInfo); @@ -146,14 +154,26 @@ void ChildProcessManager::HandleChildProcess(const std::string &srcEntry, AppExe processStartInfo->isEsModule = (hapModuleInfo.compileMode == AppExecFwk::CompileMode::ES_MODULE); auto process = ChildProcess::Create(runtime); - process->Init(processStartInfo); + if (process == nullptr) { + HILOG_ERROR("Failed to create ChildProcess."); + return; + } + bool ret = process->Init(processStartInfo); + if (!ret) { + HILOG_ERROR("JsChildProcess init failed."); + return; + } process->OnStart(); } bool ChildProcessManager::GetHapModuleInfo(const std::string &bundleName, AppExecFwk::HapModuleInfo &hapModuleInfo) { - auto bundleObj = - DelayedSingleton::GetInstance()->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + auto sysMrgClient = DelayedSingleton::GetInstance(); + if (sysMrgClient == nullptr) { + HILOG_ERROR("Failed to get SysMrgClient."); + return false; + } + auto bundleObj = sysMrgClient->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); if (bundleObj == nullptr) { HILOG_ERROR("Failed to get bundle manager service."); return false; @@ -198,6 +218,10 @@ std::unique_ptr ChildProcessManager::CreateRuntime(AppE { std::shared_ptr applicationContext = AbilityRuntime::ApplicationContext::GetInstance(); + if (applicationContext == nullptr) { + HILOG_ERROR("Get applicationContext failed."); + return nullptr; + } std::shared_ptr applicationInfo = applicationContext->GetApplicationInfo(); if (applicationInfo == nullptr) { HILOG_ERROR("applicationInfo is nullptr"); diff --git a/frameworks/native/ability/native/child_process_manager/js_child_process.cpp b/frameworks/native/ability/native/child_process_manager/js_child_process.cpp index 0e196b455d..637b1523cf 100644 --- a/frameworks/native/ability/native/child_process_manager/js_child_process.cpp +++ b/frameworks/native/ability/native/child_process_manager/js_child_process.cpp @@ -31,15 +31,24 @@ JsChildProcess::JsChildProcess(JsRuntime &jsRuntime) : jsRuntime_(jsRuntime) {} JsChildProcess::~JsChildProcess() { HILOG_DEBUG("JsChildProcess destructor."); + jsRuntime_.FreeNativeReference(std::move(jsChildProcessObj_)); } -void JsChildProcess::Init(const std::shared_ptr &info) +bool JsChildProcess::Init(const std::shared_ptr &info) { HILOG_INFO("JsChildProcess Init called"); - ChildProcess::Init(info); + if (info == nullptr) { + HILOG_ERROR("info is nullptr."); + return false; + } + bool ret = ChildProcess::Init(info); + if (!ret) { + HILOG_ERROR("ChildProcess init failed."); + return false; + } if (info->srcEntry.empty()) { HILOG_ERROR("ChildProcessStartInfo srcEntry is empty"); - return; + return false; } std::string srcPath; srcPath.append(info->moduleName).append("/"); @@ -55,8 +64,9 @@ void JsChildProcess::Init(const std::shared_ptr &info) jsChildProcessObj_ = jsRuntime_.LoadModule(moduleName, srcPath, info->hapPath, info->isEsModule); if (jsChildProcessObj_ == nullptr) { HILOG_ERROR("Failed to get ChildProcess object"); - return; + return false; } + return true; } void JsChildProcess::OnStart() diff --git a/interfaces/inner_api/child_process_manager/include/child_process.h b/interfaces/inner_api/child_process_manager/include/child_process.h index 7c7a2a9f76..645468ebe4 100644 --- a/interfaces/inner_api/child_process_manager/include/child_process.h +++ b/interfaces/inner_api/child_process_manager/include/child_process.h @@ -30,7 +30,7 @@ public: static std::shared_ptr Create(const std::unique_ptr &runtime); - virtual void Init(const std::shared_ptr &info); + virtual bool Init(const std::shared_ptr &info); virtual void OnStart(); protected: diff --git a/interfaces/inner_api/child_process_manager/include/child_process_manager_error_utils.h b/interfaces/inner_api/child_process_manager/include/child_process_manager_error_utils.h index 83eb764eaa..64ca14e950 100644 --- a/interfaces/inner_api/child_process_manager/include/child_process_manager_error_utils.h +++ b/interfaces/inner_api/child_process_manager/include/child_process_manager_error_utils.h @@ -28,6 +28,7 @@ enum class ChildProcessManagerErrorCode { ERR_ALREADY_IN_CHILD_PROCESS = 2, ERR_GET_HAP_INFO_FAILED = 3, ERR_FORK_FAILED = 4, + ERR_GET_APPLICATION_CONTEXT_FAILED = 5, }; const std::map INTERNAL_ERR_CODE_MAP = { @@ -38,6 +39,7 @@ const std::map INTERNAL_ERR_CODE AbilityErrorCode::ERROR_CODE_OPERATION_NOT_SUPPORTED }, { ChildProcessManagerErrorCode::ERR_GET_HAP_INFO_FAILED, AbilityErrorCode::ERROR_CODE_INNER }, { ChildProcessManagerErrorCode::ERR_FORK_FAILED, AbilityErrorCode::ERROR_CODE_INNER }, + { ChildProcessManagerErrorCode::ERR_GET_APPLICATION_CONTEXT_FAILED, AbilityErrorCode::ERROR_CODE_INNER }, }; class ChildProcessManagerErrorUtil { diff --git a/interfaces/inner_api/child_process_manager/include/js_child_process.h b/interfaces/inner_api/child_process_manager/include/js_child_process.h index 260b1bbb3f..7239a01094 100644 --- a/interfaces/inner_api/child_process_manager/include/js_child_process.h +++ b/interfaces/inner_api/child_process_manager/include/js_child_process.h @@ -29,8 +29,8 @@ public: static std::shared_ptr Create(const std::unique_ptr &runtime); - virtual void Init(const std::shared_ptr &info) override; - virtual void OnStart() override; + bool Init(const std::shared_ptr &info) override; + void OnStart() override; private: napi_value CallObjectMethod(const char *name, napi_value const *argv = nullptr, size_t argc = 0);