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
20 namespace OHOS {
21 namespace AbilityRuntime {
22
RequestInfo(const sptr<IRemoteObject> & token)23 RequestInfo::RequestInfo(const sptr<IRemoteObject> &token)
24 {
25 callerToken_ = token;
26 }
27
~RequestInfo()28 RequestInfo::~RequestInfo()
29 {
30 }
31
GetToken()32 sptr<IRemoteObject> RequestInfo::GetToken()
33 {
34 return callerToken_;
35 }
36
WrapRequestInfo(NativeEngine & engine,RequestInfo * request)37 NativeValue* RequestInfo::WrapRequestInfo(NativeEngine &engine, RequestInfo *request)
38 {
39 HILOG_DEBUG("WrapRequestInfo called.");
40 if (request == nullptr) {
41 HILOG_ERROR("request is nullptr.");
42 return nullptr;
43 }
44
45 NativeCallback callback = [](NativeEngine* engine, NativeCallbackInfo* info) -> NativeValue* {
46 return info->thisVar;
47 };
48
49 NativeValue* requestInfoClass = engine.DefineClass("RequestInfoClass", callback, nullptr, nullptr, 0);
50 NativeValue* result = engine.CreateInstance(requestInfoClass, nullptr, 0);
51 if (result == nullptr) {
52 HILOG_ERROR("create instance failed.");
53 return nullptr;
54 }
55
56 NativeObject* nativeObject = reinterpret_cast<NativeObject*>(result->GetInterface(NativeObject::INTERFACE_ID));
57 if (nativeObject == nullptr) {
58 HILOG_ERROR("get nativeObject failed.");
59 return nullptr;
60 }
61
62 NativeFinalize nativeFinalize = [](NativeEngine* engine, void* data, void* hint) {
63 HILOG_INFO("Js RequestInfo finalizer is called");
64 auto requestInfo = static_cast<RequestInfo*>(data);
65 if (requestInfo) {
66 delete requestInfo;
67 requestInfo = nullptr;
68 }
69 };
70
71 nativeObject->SetNativePointer(reinterpret_cast<void*>(request), nativeFinalize, nullptr);
72 return result;
73 }
74
UnwrapRequestInfo(NativeEngine & engine,NativeValue * jsParam)75 std::shared_ptr<RequestInfo> RequestInfo::UnwrapRequestInfo(NativeEngine &engine, NativeValue *jsParam)
76 {
77 HILOG_INFO("UnwrapRequestInfo called.");
78 if (jsParam == nullptr) {
79 HILOG_ERROR("jsParam is nullptr");
80 return nullptr;
81 }
82
83 if (jsParam->TypeOf() != NATIVE_OBJECT) {
84 HILOG_ERROR("UnwrapRequestInfo jsParam type error!");
85 return nullptr;
86 }
87
88 NativeObject *nativeObject = reinterpret_cast<NativeObject*>(jsParam->GetInterface(NativeObject::INTERFACE_ID));
89 if (nativeObject == nullptr) {
90 HILOG_ERROR("UnwrapRequestInfo reinterpret_cast failed!");
91 return nullptr;
92 }
93 HILOG_INFO("UnwrapRequestInfo success.");
94
95 RequestInfo *info = static_cast<RequestInfo*>(nativeObject->GetNativePointer());
96 return std::make_shared<RequestInfo>(*info);
97 }
98 } // namespace AbilityRuntime
99 } // namespace OHOS