• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_agent.h"
17 
18 #include <cinttypes>
19 #include <vector>
20 
21 #include "fms_log_wrapper.h"
22 #include "form_mgr_errors.h"
23 #include "form_mgr.h"
24 #include "ipc_skeleton.h"
25 #include "js_runtime_utils.h"
26 #include "napi_common_util.h"
27 #include "napi_common_want.h"
28 #include "napi_form_util.h"
29 #include "napi/native_api.h"
30 #include "napi/native_node_api.h"
31 #include "runtime.h"
32 
33 namespace OHOS {
34 namespace AbilityRuntime {
35 using namespace OHOS;
36 using namespace OHOS::AAFwk;
37 using namespace OHOS::AppExecFwk;
38 namespace {
39 constexpr size_t ARGS_SIZE_ONE = 1;
40 constexpr size_t ARGS_SIZE_TWO = 2;
41 const std::string IS_FORM_AGENT = "isFormAgent";
42 }
43 
Finalizer(napi_env env,void * data,void * hint)44 void JsFormAgent::Finalizer(napi_env env, void *data, void *hint)
45 {
46     HILOG_INFO("call");
47     std::unique_ptr<JsFormAgent>(static_cast<JsFormAgent *>(data));
48 }
49 
RequestPublishForm(napi_env env,napi_callback_info info)50 napi_value JsFormAgent::RequestPublishForm(napi_env env, napi_callback_info info)
51 {
52     GET_CB_INFO_AND_CALL(env, info, JsFormAgent, OnRequestPublishForm);
53 }
54 
OnRequestPublishForm(napi_env env,size_t argc,napi_value * argv)55 napi_value JsFormAgent::OnRequestPublishForm(napi_env env, size_t argc, napi_value* argv)
56 {
57     HILOG_INFO("call");
58     if (env == nullptr || argc < ARGS_SIZE_ONE || argc > ARGS_SIZE_TWO) {
59         HILOG_ERROR("invalid argc");
60         NapiFormUtil::ThrowParamNumError(env, std::to_string(argc), "1 or 2");
61         return CreateJsUndefined(env);
62     }
63 
64     auto asyncCallbackInfo = std::make_shared<RequestPublishFormCallbackInfo>();
65     decltype(argc) convertArgc = 0;
66     napi_valuetype paramZeroType = napi_undefined;
67     napi_typeof(env, argv[0], &paramZeroType);
68     if (paramZeroType != napi_object) {
69         HILOG_ERROR("formId not napi_object");
70         NapiFormUtil::ThrowParamTypeError(env, "want", "Want");
71         return CreateJsUndefined(env);
72     }
73 
74     if (!AppExecFwk::UnwrapWant(env, argv[PARAM0], asyncCallbackInfo->want)) {
75         HILOG_ERROR("fail convert want");
76         NapiFormUtil::ThrowParamError(env, "Failed to convert want.");
77         return CreateJsUndefined(env);
78     }
79 
80     convertArgc++;
81     auto apiResult = std::make_shared<int32_t>();
82     auto formId = std::make_shared<int64_t>();
83     NapiAsyncTask::ExecuteCallback execute = [asyncCallbackInfo, cardId = formId, ret = apiResult]() {
84         asyncCallbackInfo->want.SetParam(IS_FORM_AGENT, true);
85         *ret = FormMgr::GetInstance().RequestPublishForm(asyncCallbackInfo->want, false,
86             asyncCallbackInfo->formProviderData, *cardId, asyncCallbackInfo->formDataProxies);
87         if (*ret != ERR_OK) {
88             HILOG_ERROR("fail RequestPublishForm startAbility");
89             return;
90         }
91         *ret = FormMgr::GetInstance().AcquireAddFormResult(*cardId);
92     };
93 
94     NapiAsyncTask::CompleteCallback complete =
95         [formId, ret = apiResult](napi_env env, NapiAsyncTask &task, int32_t status) {
96         if (*ret == ERR_OK) {
97             HILOG_INFO("Sucess");
98             task.ResolveWithNoError(env, CreateJsValue(env, std::to_string(*formId)));
99         } else {
100             HILOG_ERROR("fail");
101             task.Reject(env, NapiFormUtil::CreateErrorByInternalErrorCode(env, *ret));
102         }
103     };
104     napi_value lastParam = (argc <= convertArgc) ? nullptr : argv[convertArgc];
105     napi_value result = nullptr;
106     NapiAsyncTask::ScheduleWithDefaultQos("JsFormAgent::OnRequestPublishForm",
107         env, CreateAsyncTaskWithLastParam(env, lastParam, std::move(execute), std::move(complete), &result));
108     return result;
109 }
110 }  // namespace AbilityRuntime
111 }  // namespace OHOS
112