• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_form_edit_extension_context.h"
17 #include "hilog_tag_wrapper.h"
18 #include "js_ui_extension_context.h"
19 #include "js_runtime.h"
20 #include "js_runtime_utils.h"
21 #include "napi_common_util.h"
22 #include "napi_common_want.h"
23 #include "form_constants.h"
24 #include "element_name.h"
25 #include "js_error_utils.h"
26 
27 namespace OHOS {
28 namespace AbilityRuntime {
29 namespace {
30 constexpr size_t ARGC_TWO = 2;
31 constexpr const char *ERR_MSG_PARAMS_ERROR = "Params error";
32 constexpr const char *ERR_MSG_INTERNAL_ERROR = "Internal error";
33 constexpr int32_t INDEX_ONE = 1;
34 } // namespace
35 using namespace OHOS::AppExecFwk;
36 
Finalizer(napi_env env,void * data,void * hint)37 void JsFormEditExtensionContext::Finalizer(napi_env env, void *data, void *hint)
38 {
39     TAG_LOGD(AAFwkTag::UI_EXT, "called");
40     std::unique_ptr<JsFormEditExtensionContext>(static_cast<JsFormEditExtensionContext *>(data));
41 }
42 
CreateJsFormEditExtensionContext(napi_env env,std::shared_ptr<FormEditExtensionContext> context)43 napi_value JsFormEditExtensionContext::CreateJsFormEditExtensionContext(
44     napi_env env, std::shared_ptr<FormEditExtensionContext> context)
45 {
46     TAG_LOGD(AAFwkTag::UI_EXT, "begin");
47     std::shared_ptr<OHOS::AppExecFwk::AbilityInfo> abilityInfo = nullptr;
48     if (context) {
49         abilityInfo = context->GetAbilityInfo();
50     }
51 
52     napi_value objValue = JsUIExtensionContext::CreateJsUIExtensionContext(env, context);
53     std::unique_ptr<JsFormEditExtensionContext> jsContext = std::make_unique<JsFormEditExtensionContext>(context);
54     napi_status status = napi_wrap(env, objValue, jsContext.release(), Finalizer, nullptr, nullptr);
55     if (status != napi_ok) {
56         TAG_LOGE(AAFwkTag::UI_EXT, "Failed to do napi wrap");
57     }
58 
59     const char *moduleName = "JsFormEditExtensionContext";
60     BindNativeFunction(env, objValue, "startSecondPage", moduleName, StartSecondPage);
61 
62     return objValue;
63 }
64 
StartSecondPage(napi_env env,napi_callback_info info)65 napi_value JsFormEditExtensionContext::StartSecondPage(napi_env env, napi_callback_info info)
66 {
67     TAG_LOGI(AAFwkTag::UI_EXT, "called");
68     GET_NAPI_INFO_AND_CALL(env, info, JsFormEditExtensionContext, OnStartSecondPage);
69 }
70 
OnStartSecondPage(napi_env env,NapiCallbackInfo & info)71 napi_value JsFormEditExtensionContext::OnStartSecondPage(napi_env env, NapiCallbackInfo &info)
72 {
73     TAG_LOGD(AAFwkTag::UI_EXT, "called: param size: %{public}d", static_cast<int32_t>(info.argc));
74     if (info.argc != ARGC_TWO) {
75         ThrowError(env, static_cast<int32_t>(FormEditErrorCode::ERROR_CODE_PARAM_ERROR), ERR_MSG_PARAMS_ERROR);
76         return CreateJsUndefined(env);
77     }
78 
79     auto context = context_.lock();
80     if (context == nullptr) {
81         TAG_LOGE(AAFwkTag::UI_EXT, "Context is released");
82         ThrowError(env, static_cast<int32_t>(FormEditErrorCode::ERROR_CODE_INTERNAL_ERROR), ERR_MSG_INTERNAL_ERROR);
83         return CreateJsUndefined(env);
84     }
85 
86     AAFwk::Want want;
87     OHOS::AppExecFwk::UnwrapWant(env, info.argv[0], want);
88     NapiAsyncTask::CompleteCallback complete = [weak = context_, want](napi_env env, NapiAsyncTask &task,
89                                                                       int32_t status) {
90         TAG_LOGD(AAFwkTag::UI_EXT, "OnStartSecondPage begin");
91         auto context = weak.lock();
92         if (!context) {
93             TAG_LOGE(AAFwkTag::UI_EXT, "Context is released");
94             task.Reject(env, CreateJsError(env, static_cast<int32_t>(FormEditErrorCode::ERROR_CODE_INTERNAL_ERROR)));
95             return;
96         }
97 
98         ErrCode errCode = context->StartAbilityByFms(want);
99         napi_value abilityResult = AppExecFwk::WrapAbilityResult(env, static_cast<int>(errCode), want);
100         if (abilityResult == nullptr) {
101             TAG_LOGE(AAFwkTag::UI_EXT, "Wrap abilityResult failed");
102             task.Reject(env, CreateJsError(env, static_cast<int32_t>(FormEditErrorCode::ERROR_CODE_INTERNAL_ERROR)));
103             return;
104         }
105 
106         task.Resolve(env, abilityResult);
107     };
108 
109     napi_value lastParam = (info.argc > INDEX_ONE) ? info.argv[INDEX_ONE] : nullptr;
110     napi_value result = nullptr;
111     NapiAsyncTask::ScheduleHighQos("JsFormEditExtensionContext OnStartSecondPage", env,
112         CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
113     return result;
114 }
115 } // namespace AbilityRuntime
116 } // namespace OHOS