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 "request_info.h"
17
18 #include "hilog_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(NativeEngine & engine,RequestInfo * request)42 NativeValue* RequestInfo::WrapRequestInfo(NativeEngine &engine, RequestInfo *request)
43 {
44 HILOG_DEBUG("WrapRequestInfo called.");
45 if (request == nullptr) {
46 HILOG_ERROR("request is nullptr.");
47 return nullptr;
48 }
49
50 NativeCallback callback = [](NativeEngine* engine, NativeCallbackInfo* info) -> NativeValue* {
51 return info->thisVar;
52 };
53
54 NativeValue* requestInfoClass = engine.DefineClass("RequestInfoClass", callback, nullptr, nullptr, 0);
55 NativeValue* result = engine.CreateInstance(requestInfoClass, nullptr, 0);
56 if (result == nullptr) {
57 HILOG_ERROR("create instance failed.");
58 return nullptr;
59 }
60
61 NativeObject* nativeObject = reinterpret_cast<NativeObject*>(result->GetInterface(NativeObject::INTERFACE_ID));
62 if (nativeObject == nullptr) {
63 HILOG_ERROR("get nativeObject failed.");
64 return nullptr;
65 }
66
67 NativeFinalize nativeFinalize = [](NativeEngine* engine, void* data, void* hint) {
68 HILOG_INFO("Js RequestInfo finalizer is called");
69 auto requestInfo = static_cast<RequestInfo*>(data);
70 if (requestInfo) {
71 delete requestInfo;
72 requestInfo = nullptr;
73 }
74 };
75
76 nativeObject->SetNativePointer(reinterpret_cast<void*>(request), nativeFinalize, nullptr);
77 nativeObject->SetProperty("windowRect",
78 CreateJsWindowRect(engine, request->left_, request->top_, request->width_, request->height_));
79 return result;
80 }
81
CreateJsWindowRect(NativeEngine & engine,int32_t left,int32_t top,int32_t width,int32_t height)82 NativeValue* RequestInfo::CreateJsWindowRect(
83 NativeEngine& engine, int32_t left, int32_t top, int32_t width, int32_t height)
84 {
85 HILOG_DEBUG("left: %{public}d, top: %{public}d, width: %{public}d, height: %{public}d",
86 left, top, width, height);
87 NativeValue* objValue = engine.CreateObject();
88 NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
89 if (object == nullptr) {
90 HILOG_ERROR("Native object is nullptr.");
91 return objValue;
92 }
93 object->SetProperty("left", CreateJsValue(engine, left));
94 object->SetProperty("top", CreateJsValue(engine, top));
95 object->SetProperty("width", CreateJsValue(engine, width));
96 object->SetProperty("height", CreateJsValue(engine, height));
97 return objValue;
98 }
99
UnwrapRequestInfo(NativeEngine & engine,NativeValue * jsParam)100 std::shared_ptr<RequestInfo> RequestInfo::UnwrapRequestInfo(NativeEngine &engine, NativeValue *jsParam)
101 {
102 HILOG_INFO("UnwrapRequestInfo called.");
103 if (jsParam == nullptr) {
104 HILOG_ERROR("jsParam is nullptr");
105 return nullptr;
106 }
107
108 if (jsParam->TypeOf() != NATIVE_OBJECT) {
109 HILOG_ERROR("UnwrapRequestInfo jsParam type error!");
110 return nullptr;
111 }
112
113 NativeObject *nativeObject = reinterpret_cast<NativeObject*>(jsParam->GetInterface(NativeObject::INTERFACE_ID));
114 if (nativeObject == nullptr) {
115 HILOG_ERROR("UnwrapRequestInfo reinterpret_cast failed!");
116 return nullptr;
117 }
118 HILOG_INFO("UnwrapRequestInfo success.");
119
120 RequestInfo *info = static_cast<RequestInfo*>(nativeObject->GetNativePointer());
121 return std::make_shared<RequestInfo>(*info);
122 }
123
124 } // namespace AbilityRuntime
125 } // namespace OHOS