• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     ThreadLockInfo *lockInfo;
44     int result;
45 };
46 
47 struct OperateJsRefParam {
48     napi_env env;
49     napi_ref thisVarRef;
50     ThreadLockInfo *lockInfo;
51 };
52 
53 class NAPIRemoteObject : public IPCObjectStub {
54 public:
55     NAPIRemoteObject(std::thread::id jsThreadId, napi_env env, napi_ref jsObjectRef, const std::u16string &descriptor);
56 
57     ~NAPIRemoteObject() override;
58 
59     bool CheckObjectLegality() const override;
60 
61     int GetObjectType() const override;
62 
63     int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
64 
65     napi_ref GetJsObjectRef() const;
66 
67     void ResetJsEnv();
68 private:
69     napi_env env_ = nullptr;
70     std::thread::id jsThreadId_;
71     static napi_value ThenCallback(napi_env env, napi_callback_info info);
72     static napi_value CatchCallback(napi_env env, napi_callback_info info);
73     napi_ref thisVarRef_ = nullptr;
74     int OnJsRemoteRequest(CallbackParam *jsParam);
75 };
76 
77 struct SendRequestParam {
78     sptr<IRemoteObject> target;
79     uint32_t code;
80     std::shared_ptr<MessageParcel> data;
81     std::shared_ptr<MessageParcel> reply;
82     MessageOption &option;
83     napi_async_work asyncWork;
84     napi_deferred deferred;
85     int errCode;
86     napi_ref jsCodeRef;
87     napi_ref jsDataRef;
88     napi_ref jsReplyRef;
89     napi_ref jsOptionRef;
90     napi_ref callback;
91     napi_env env;
92     std::string traceValue;
93     int32_t traceId;
94 };
95 
96 napi_value MakeSendRequestResult(SendRequestParam *param);
97 } // namespace OHOS
98 #endif // NAPI_IPC_OHOS_REMOTE_OBJECT_H
99