1 /*
2 * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "js_child_process.h"
17
18 #include "child_process.h"
19 #include "hilog_tag_wrapper.h"
20 #include "js_runtime.h"
21 #include "js_runtime_utils.h"
22 #include "napi_common_child_process_param.h"
23
24 namespace OHOS {
25 namespace AbilityRuntime {
Create(const std::unique_ptr<Runtime> & runtime)26 std::shared_ptr<ChildProcess> JsChildProcess::Create(const std::unique_ptr<Runtime>& runtime)
27 {
28 return std::make_shared<JsChildProcess>(static_cast<JsRuntime &>(*runtime));
29 }
30
JsChildProcess(JsRuntime & jsRuntime)31 JsChildProcess::JsChildProcess(JsRuntime &jsRuntime) : jsRuntime_(jsRuntime) {}
~JsChildProcess()32 JsChildProcess::~JsChildProcess()
33 {
34 TAG_LOGD(AAFwkTag::PROCESSMGR, "Called");
35 jsRuntime_.FreeNativeReference(std::move(jsChildProcessObj_));
36 }
37
Init(const std::shared_ptr<ChildProcessStartInfo> & info)38 bool JsChildProcess::Init(const std::shared_ptr<ChildProcessStartInfo> &info)
39 {
40 TAG_LOGI(AAFwkTag::PROCESSMGR, "called");
41 if (info == nullptr) {
42 TAG_LOGE(AAFwkTag::PROCESSMGR, "null info");
43 return false;
44 }
45 bool ret = ChildProcess::Init(info);
46 if (!ret) {
47 TAG_LOGE(AAFwkTag::PROCESSMGR, "ChildProcess init failed");
48 return false;
49 }
50 if (info->srcEntry.empty()) {
51 TAG_LOGE(AAFwkTag::PROCESSMGR, "Empty info srcEntry");
52 return false;
53 }
54 std::string srcPath = info->srcEntry;
55 if (srcPath.rfind(".") != std::string::npos) {
56 srcPath.erase(srcPath.rfind("."));
57 }
58 srcPath.append(".abc");
59 std::string moduleName(info->moduleName);
60 moduleName.append("::").append(info->name);
61
62 HandleScope handleScope(jsRuntime_);
63 jsChildProcessObj_ = jsRuntime_.LoadModule(moduleName, srcPath, info->hapPath, info->isEsModule);
64 if (jsChildProcessObj_ == nullptr) {
65 TAG_LOGE(AAFwkTag::PROCESSMGR, "null jsChildProcessObj_");
66 return false;
67 }
68 return true;
69 }
70
OnStart()71 void JsChildProcess::OnStart()
72 {
73 TAG_LOGI(AAFwkTag::PROCESSMGR, "called");
74 ChildProcess::OnStart();
75 CallObjectMethod("onStart");
76 }
77
OnStart(std::shared_ptr<AppExecFwk::ChildProcessArgs> args)78 void JsChildProcess::OnStart(std::shared_ptr<AppExecFwk::ChildProcessArgs> args)
79 {
80 TAG_LOGI(AAFwkTag::PROCESSMGR, "JsChildProcess OnStart called");
81 if (!args) {
82 TAG_LOGE(AAFwkTag::PROCESSMGR, "args is nullptr");
83 return;
84 }
85 ChildProcess::OnStart(args);
86
87 HandleScope handleScope(jsRuntime_);
88 auto env = jsRuntime_.GetNapiEnv();
89
90 napi_value jsArgs = WrapChildProcessArgs(env, *args);
91 napi_value argv[] = { jsArgs };
92 CallObjectMethod("onStart", argv, ArraySize(argv));
93 }
94
CallObjectMethod(const char * name,napi_value const * argv,size_t argc)95 napi_value JsChildProcess::CallObjectMethod(const char *name, napi_value const *argv, size_t argc)
96 {
97 TAG_LOGD(AAFwkTag::PROCESSMGR, "called, name:%{public}s", name);
98 if (jsChildProcessObj_ == nullptr) {
99 TAG_LOGE(AAFwkTag::PROCESSMGR, "null jsChildProcessObj_");
100 return nullptr;
101 }
102
103 HandleScope handleScope(jsRuntime_);
104 auto env = jsRuntime_.GetNapiEnv();
105
106 napi_value obj = jsChildProcessObj_->GetNapiValue();
107 if (!CheckTypeForNapiValue(env, obj, napi_object)) {
108 TAG_LOGE(AAFwkTag::PROCESSMGR, "Get ChildProcess obj failed");
109 return nullptr;
110 }
111
112 napi_value methodOnCreate = nullptr;
113 napi_get_named_property(env, obj, name, &methodOnCreate);
114 if (methodOnCreate == nullptr) {
115 TAG_LOGE(AAFwkTag::PROCESSMGR, "Get '%{public}s' failed", name);
116 return nullptr;
117 }
118 napi_call_function(env, obj, methodOnCreate, argc, argv, nullptr);
119 return nullptr;
120 }
121 } // namespace AbilityRuntime
122 } // namespace OHOS