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 #ifndef NAPI_IPC_OHOS_REMOTE_OBJECT_INTERNAL_H 17 #define NAPI_IPC_OHOS_REMOTE_OBJECT_INTERNAL_H 18 19 #include <thread> 20 21 #include "ipc_object_stub.h" 22 #include "iremote_object.h" 23 #include "message_parcel.h" 24 #include "message_option.h" 25 #include "napi/native_api.h" 26 #include "napi_remote_object.h" 27 28 namespace OHOS { 29 struct ThreadLockInfo { 30 std::mutex mutex; 31 std::condition_variable condition; 32 bool ready = false; 33 }; 34 35 struct CallbackParam { 36 napi_env env; 37 napi_ref thisVarRef; 38 uint32_t code; 39 MessageParcel *data; 40 MessageParcel *reply; 41 MessageOption *option; 42 CallingInfo callingInfo; 43 CallingInfo oldCallingInfo; 44 ThreadLockInfo *lockInfo; 45 int result; 46 }; 47 48 struct OperateJsRefParam { 49 napi_env env; 50 napi_ref thisVarRef; 51 ThreadLockInfo *lockInfo; 52 }; 53 54 class NAPIRemoteObject : public IPCObjectStub { 55 public: 56 NAPIRemoteObject(std::thread::id jsThreadId, napi_env env, napi_ref jsObjectRef, const std::u16string &descriptor); 57 58 ~NAPIRemoteObject() override; 59 60 bool CheckObjectLegality() const override; 61 62 int GetObjectType() const override; 63 64 int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; 65 66 napi_ref GetJsObjectRef() const; 67 68 void ResetJsEnv(); 69 private: 70 napi_env env_ = nullptr; 71 std::thread::id jsThreadId_; 72 static napi_value ThenCallback(napi_env env, napi_callback_info info); 73 static napi_value CatchCallback(napi_env env, napi_callback_info info); 74 napi_ref thisVarRef_ = nullptr; 75 int OnJsRemoteRequest(CallbackParam *jsParam); 76 }; 77 78 struct SendRequestParam { 79 sptr<IRemoteObject> target; 80 uint32_t code; 81 std::shared_ptr<MessageParcel> data; 82 std::shared_ptr<MessageParcel> reply; 83 MessageOption &option; 84 napi_async_work asyncWork; 85 napi_deferred deferred; 86 int errCode; 87 napi_ref jsCodeRef; 88 napi_ref jsDataRef; 89 napi_ref jsReplyRef; 90 napi_ref jsOptionRef; 91 napi_ref callback; 92 napi_env env; 93 std::string traceValue; 94 int32_t traceId; 95 }; 96 97 napi_value MakeSendRequestResult(SendRequestParam *param); 98 } // namespace OHOS 99 #endif // NAPI_IPC_OHOS_REMOTE_OBJECT_H 100