• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 RPC_ANI_CLASS_H
17 #define RPC_ANI_CLASS_H
18 
19 #include <ani.h>
20 #include <array>
21 #include <cstring>
22 #include "ani_remote_object.h"
23 #include "ani_rpc_error.h"
24 #include "ani_utils.h"
25 #include "ipc_debug.h"
26 #include "ipc_object_proxy.h"
27 #include "ipc_object_stub.h"
28 #include "iremote_object.h"
29 #include "log_tags.h"
30 #include "message_parcel.h"
31 #include "string_ex.h"
32 
33 namespace OHOS {
34 
35 class IPCAniStub : public IPCObjectStub {
36 public:
37     IPCAniStub(ani_env *env, ani_object remoteObject, const std::u16string &descriptor);
38 
39     int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
40 
41     ~IPCAniStub();
42 
43 private:
44     ani_env *env_ = nullptr;
45     ani_ref saveRemote_;
46 };
47 
48 class IPCObjectRemoteHolder : public NativeObject {
49 public:
IPCObjectRemoteHolder(ani_env * env,ani_object remoteObject,const std::u16string & descriptor)50     IPCObjectRemoteHolder(ani_env *env, ani_object remoteObject, const std::u16string &descriptor)
51         : env_(env), remoteObject_(remoteObject), descriptor_(descriptor)
52     {
53         if (env_ == nullptr) {
54             return;
55         }
56         if (ANI_OK != env->GlobalReference_Create(reinterpret_cast<ani_ref>(remoteObject),
57             reinterpret_cast<ani_ref*>(&remoteObject_))) {
58             return;
59         }
60     }
61 
GetDescriptor()62     std::string GetDescriptor()
63     {
64         std::string ret = Str16ToStr8(descriptor_);
65         return ret;
66     }
67 
Get(ani_env * env)68     sptr<IRemoteObject> Get(ani_env *env)
69     {
70         sptr<IRemoteObject> tmp = object_.promote();
71         if (nullptr == tmp && nullptr != env) {
72             tmp = sptr<IPCAniStub>::MakeSptr(env, remoteObject_, descriptor_);
73             object_ = tmp;
74         }
75         return tmp;
76     }
77 
~IPCObjectRemoteHolder()78     ~IPCObjectRemoteHolder()
79     {
80         if (env_ == nullptr) {
81             return;
82         }
83         if (ANI_OK != env_->GlobalReference_Delete(remoteObject_)) {
84             return;
85         }
86     }
87 
88 private:
89     ani_env *env_ = nullptr;
90     ani_object remoteObject_;
91     std::u16string descriptor_;
92     wptr<IRemoteObject> object_;
93 };
94 
95 class IPCObjectProxyHolder {
96 public:
GetDescriptor()97     std::string GetDescriptor()
98     {
99         if (!object_) {
100             return "";
101         }
102         return OHOS::Str16ToStr8(object_->GetInterfaceDescriptor());
103     }
104 
105 private:
106     sptr<IPCObjectProxy> object_;
107 };
108 }  // namespace OHOS
109 #endif  // RPC_ANI_CLASS_H