1 /*
2 * Copyright (c) 2025 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_live_form_extension_impl.h"
17 #include "js_ui_extension_content_session.h"
18 #include "napi/native_api.h"
19 #include "napi/native_node_api.h"
20 #include "napi_common_want.h"
21 #include "fms_log_wrapper.h"
22 #include "js_live_form_extension_context.h"
23
24 namespace OHOS {
25 namespace AbilityRuntime {
26 namespace {
27 constexpr size_t ARGC_ONE = 1;
28 } // namespace
29
AttachUIExtensionContext(napi_env env,void * value,void *)30 napi_value AttachUIExtensionContext(napi_env env, void *value, void *)
31 {
32 HILOG_DEBUG("called");
33 if (value == nullptr) {
34 HILOG_ERROR("Invalid param value");
35 return nullptr;
36 }
37
38 auto ptr = reinterpret_cast<std::weak_ptr<LiveFormExtensionContext> *>(value)->lock();
39 if (ptr == nullptr) {
40 HILOG_ERROR("Invalid context");
41 return nullptr;
42 }
43 napi_value object = JsLiveFormExtensionContext::CreateJsLiveFormExtensionContext(env, ptr);
44 auto contextRef =
45 JsRuntime::LoadSystemModuleByEngine(env, "application.LiveFormExtensionContext", &object, ARGC_ONE);
46 if (contextRef == nullptr) {
47 HILOG_ERROR("Fail to load module");
48 return nullptr;
49 }
50 auto contextObj = contextRef->GetNapiValue();
51 if (contextObj == nullptr) {
52 HILOG_ERROR("Load context error");
53 return nullptr;
54 }
55 napi_coerce_to_native_binding_object(env, contextObj, DetachCallbackFunc, AttachUIExtensionContext, value,
56 nullptr);
57 auto workContext = new (std::nothrow) std::weak_ptr<LiveFormExtensionContext>(ptr);
58 napi_status status = napi_wrap(env, contextObj, workContext,
59 [](napi_env, void *data, void *) {
60 HILOG_DEBUG("Finalizer called");
61 if (data == nullptr) {
62 HILOG_ERROR("Finalizer for weak_ptr is nullptr");
63 return;
64 }
65 delete static_cast<std::weak_ptr<LiveFormExtensionContext> *>(data);
66 }, nullptr, nullptr);
67 if (status != napi_ok && workContext != nullptr) {
68 HILOG_ERROR("napi_wrap Failed: %{public}d", status);
69 delete workContext;
70 return nullptr;
71 }
72
73 return contextObj;
74 }
75
JsLiveFormExtensionImpl(const std::unique_ptr<Runtime> & runtime)76 JsLiveFormExtensionImpl::JsLiveFormExtensionImpl(const std::unique_ptr<Runtime> &runtime)
77 : JsUIExtensionBase(runtime)
78 {}
79
BindContext()80 void JsLiveFormExtensionImpl::BindContext()
81 {
82 HandleScope handleScope(jsRuntime_);
83 if (jsObj_ == nullptr) {
84 HILOG_ERROR("null jsObj_");
85 return;
86 }
87
88 napi_env env = jsRuntime_.GetNapiEnv();
89 napi_value obj = jsObj_->GetNapiValue();
90 if (!CheckTypeForNapiValue(env, obj, napi_object)) {
91 HILOG_ERROR("obj is not object");
92 return;
93 }
94 if (context_ == nullptr) {
95 HILOG_ERROR("null Context");
96 return;
97 }
98 HILOG_DEBUG("BindContext CreateJsLiveFormExtensionContext");
99 napi_value contextObj = JsLiveFormExtensionContext::CreateJsLiveFormExtensionContext(env, context_);
100 if (contextObj == nullptr) {
101 HILOG_ERROR("Create js ui extension context error");
102 return;
103 }
104
105 shellContextRef_ =
106 JsRuntime::LoadSystemModuleByEngine(env, "application.LiveFormExtensionContext", &contextObj, ARGC_ONE);
107 if (shellContextRef_ == nullptr) {
108 HILOG_ERROR("Fail to get loadSystemModuleByEngine");
109 return;
110 }
111 contextObj = shellContextRef_->GetNapiValue();
112 if (!CheckTypeForNapiValue(env, contextObj, napi_object)) {
113 HILOG_ERROR("Fail to get context native object");
114 return;
115 }
116
117 auto workContext = new (std::nothrow) std::shared_ptr<LiveFormExtensionContext>(context_);
118 napi_coerce_to_native_binding_object(env, contextObj, DetachCallbackFunc, AttachUIExtensionContext,
119 workContext, nullptr);
120 context_->Bind(jsRuntime_, shellContextRef_.get());
121 napi_set_named_property(env, obj, "context", contextObj);
122 napi_status status = napi_wrap(env, contextObj, workContext,
123 [](napi_env, void *data, void *) {
124 if (data == nullptr) {
125 HILOG_ERROR("Finalizer for weak_ptr is nullptr");
126 return;
127 }
128 delete static_cast<std::weak_ptr<LiveFormExtensionContext> *>(data);
129 }, nullptr, nullptr);
130 if (status != napi_ok && workContext != nullptr) {
131 HILOG_ERROR("napi_wrap Failed: %{public}d", status);
132 delete workContext;
133 }
134
135 HILOG_DEBUG("Bind context end");
136 }
137 } // namespace AbilityRuntime
138 } // namespace OHOS