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 NapiScope { 55 public: 56 NapiScope(napi_env env); 57 ~NapiScope(); 58 59 bool IsValid(); 60 private: 61 napi_env env_; 62 napi_handle_scope scope_; 63 bool isValid_ = false; 64 }; 65 66 class NAPIRemoteObject : public IPCObjectStub { 67 public: 68 NAPIRemoteObject(std::thread::id jsThreadId, napi_env env, napi_ref jsObjectRef, const std::u16string &descriptor); 69 70 ~NAPIRemoteObject() override; 71 72 bool CheckObjectLegality() const override; 73 74 int GetObjectType() const override; 75 76 int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; 77 78 napi_ref GetJsObjectRef() const; 79 80 void ResetJsEnv(); 81 GetJSThreadId()82 std::thread::id GetJSThreadId() const 83 { 84 return jsThreadId_; 85 } 86 private: 87 napi_env env_ = nullptr; 88 const std::thread::id jsThreadId_; 89 napi_ref thisVarRef_ = nullptr; 90 std::string desc_; 91 int OnJsRemoteRequest(CallbackParam *jsParam); 92 }; 93 94 struct SendRequestParam { 95 sptr<IRemoteObject> target; 96 uint32_t code; 97 std::shared_ptr<MessageParcel> data; 98 std::shared_ptr<MessageParcel> reply; 99 MessageOption &option; 100 napi_async_work asyncWork; 101 napi_deferred deferred; 102 int errCode; 103 napi_ref jsCodeRef; 104 napi_ref jsDataRef; 105 napi_ref jsReplyRef; 106 napi_ref jsOptionRef; 107 napi_ref callback; 108 napi_env env; 109 std::string traceValue; 110 int32_t traceId; 111 }; 112 113 napi_value MakeSendRequestResult(SendRequestParam *param); 114 } // namespace OHOS 115 #endif // NAPI_IPC_OHOS_REMOTE_OBJECT_H 116