• 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_REMOTE_PROXY_HOLDER_H
17 #define NAPI_REMOTE_PROXY_HOLDER_H
18 
19 #include <mutex>
20 #include <refbase.h>
21 #include <set>
22 
23 #include "napi_remote_object_internal.h"
24 
25 #include "napi/native_api.h"
26 
27 namespace OHOS {
28 class NAPIDeathRecipient : public IRemoteObject::DeathRecipient {
29 public:
30     explicit NAPIDeathRecipient(napi_env env, napi_value jsDeathRecipient);
31 
32     void OnRemoteDied(const wptr<IRemoteObject> &object) override;
33 
34     bool Matches(napi_value object);
35 
36 protected:
37     virtual ~NAPIDeathRecipient() = default;
38 
39 private:
40     struct OnRemoteDiedParam {
41         napi_env env;
42         napi_ref deathRecipientRef;
43     };
44     napi_env env_ = nullptr;
45     napi_ref deathRecipientRef_ = nullptr;
46 };
47 
48 class NAPIDeathRecipientList : public RefBase {
49 public:
50     NAPIDeathRecipientList();
51 
52     ~NAPIDeathRecipientList();
53 
54     bool Add(const sptr<NAPIDeathRecipient> &recipient);
55 
56     bool Remove(const sptr<NAPIDeathRecipient> &recipient);
57 
58     sptr<NAPIDeathRecipient> Find(napi_value jsRecipient);
59 
60 private:
61     std::mutex mutex_;
62     std::set<sptr<NAPIDeathRecipient>> set_;
63 };
64 
65 class NAPIRemoteProxyHolder {
66 public:
67     NAPIRemoteProxyHolder();
68     ~NAPIRemoteProxyHolder();
69     sptr<NAPIDeathRecipientList> list_;
70     sptr<IRemoteObject> object_;
71 };
72 
73 NAPIRemoteProxyHolder *NAPI_ohos_rpc_getRemoteProxyHolder(napi_env env, napi_value jsRemoteProxy);
74 } // namespace OHOS
75 #endif // NAPI_REMOTE_PROXY_HOLDER_H