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 "request_info.h"
17
18 #include "hilog_tag_wrapper.h"
19 #include "js_runtime_utils.h"
20
21 namespace OHOS {
22 namespace AbilityRuntime {
23
RequestInfo(const sptr<IRemoteObject> & token,int32_t left,int32_t top,int32_t width,int32_t height)24 RequestInfo::RequestInfo(const sptr<IRemoteObject> &token, int32_t left, int32_t top, int32_t width, int32_t height)
25 {
26 callerToken_ = token;
27 left_ = left;
28 top_ = top;
29 width_ = width;
30 height_ = height;
31 }
32
~RequestInfo()33 RequestInfo::~RequestInfo()
34 {
35 }
36
GetToken()37 sptr<IRemoteObject> RequestInfo::GetToken()
38 {
39 return callerToken_;
40 }
41
WrapRequestInfo(napi_env env,RequestInfo * request)42 napi_value RequestInfo::WrapRequestInfo(napi_env env, RequestInfo *request)
43 {
44 TAG_LOGD(AAFwkTag::DIALOG, "call");
45 if (request == nullptr) {
46 TAG_LOGE(AAFwkTag::DIALOG, "null request");
47 return nullptr;
48 }
49
50 auto callback = [](napi_env env, napi_callback_info info) -> napi_value {
51 napi_value thisVar = nullptr;
52 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
53 return thisVar;
54 };
55
56 napi_value requestInfoClass = nullptr;
57 napi_define_class(
58 env, "RequestInfoClass", NAPI_AUTO_LENGTH, callback, nullptr, 0, nullptr, &requestInfoClass);
59 napi_value result = nullptr;
60 napi_new_instance(env, requestInfoClass, 0, nullptr, &result);
61 if (result == nullptr) {
62 TAG_LOGE(AAFwkTag::DIALOG, "create instance failed");
63 return nullptr;
64 }
65
66 if (!CheckTypeForNapiValue(env, result, napi_object)) {
67 TAG_LOGE(AAFwkTag::DIALOG, "UnwrapRequestInfo result type error");
68 return nullptr;
69 }
70
71 auto nativeFinalize = [](napi_env env, void* data, void* hint) {
72 TAG_LOGI(AAFwkTag::DIALOG, "finalizer call");
73 auto requestInfo = static_cast<RequestInfo*>(data);
74 if (requestInfo) {
75 delete requestInfo;
76 requestInfo = nullptr;
77 }
78 };
79 napi_status status = napi_wrap(env, result, request, nativeFinalize, nullptr, nullptr);
80 if (status != napi_ok && request != nullptr) {
81 TAG_LOGE(AAFwkTag::DIALOG, "napi_wrap Failed: %{public}d", status);
82 delete request;
83 request = nullptr;
84 return nullptr;
85 }
86 napi_set_named_property(env, result, "windowRect",
87 CreateJsWindowRect(env, request->left_, request->top_, request->width_, request->height_));
88 return result;
89 }
90
CreateJsWindowRect(napi_env env,int32_t left,int32_t top,int32_t width,int32_t height)91 napi_value RequestInfo::CreateJsWindowRect(
92 napi_env env, int32_t left, int32_t top, int32_t width, int32_t height)
93 {
94 TAG_LOGD(AAFwkTag::DIALOG, "left:%{public}d, top:%{public}d, width:%{public}d, height:%{public}d",
95 left, top, width, height);
96 napi_value objValue = nullptr;
97 napi_create_object(env, &objValue);
98 if (objValue == nullptr) {
99 TAG_LOGE(AAFwkTag::DIALOG, "null obj");
100 return objValue;
101 }
102 napi_set_named_property(env, objValue, "left", CreateJsValue(env, left));
103 napi_set_named_property(env, objValue, "top", CreateJsValue(env, top));
104 napi_set_named_property(env, objValue, "width", CreateJsValue(env, width));
105 napi_set_named_property(env, objValue, "height", CreateJsValue(env, height));
106 return objValue;
107 }
108
UnwrapRequestInfo(napi_env env,napi_value jsParam)109 std::shared_ptr<RequestInfo> RequestInfo::UnwrapRequestInfo(napi_env env, napi_value jsParam)
110 {
111 TAG_LOGI(AAFwkTag::DIALOG, "call");
112 if (jsParam == nullptr) {
113 TAG_LOGE(AAFwkTag::DIALOG, "null jsParam");
114 return nullptr;
115 }
116
117 if (!CheckTypeForNapiValue(env, jsParam, napi_object)) {
118 TAG_LOGE(AAFwkTag::DIALOG, "jsParam type error");
119 return nullptr;
120 }
121 void* result = nullptr;
122 napi_unwrap(env, jsParam, &result);
123 RequestInfo *info = static_cast<RequestInfo*>(result);
124 return std::make_shared<RequestInfo>(*info);
125 }
126
127 } // namespace AbilityRuntime
128 } // namespace OHOS