• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 #include "remote_object_wrapper.h"
17 
18 #include <iostream>
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include "cxx.h"
24 #include "iremote_object.h"
25 #include "message_parcel.h"
26 #include "refbase.h"
27 #include "remote/wrapper.rs.h"
28 #include "string_ex.h"
29 
30 namespace OHOS {
31 namespace IpcRust {
IRemoteObjectWrapper()32 IRemoteObjectWrapper::IRemoteObjectWrapper() : raw_(nullptr) {}
33 
SendRequest(const uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option) const34 int32_t IRemoteObjectWrapper::SendRequest(const uint32_t code, MessageParcel &data, MessageParcel &reply,
35     MessageOption &option) const
36 {
37     return GetInner()->SendRequest(code, data, reply, option);
38 }
39 
GetInner() const40 IRemoteObject *IRemoteObjectWrapper::GetInner() const
41 {
42     if (is_raw_) {
43         return raw_;
44     } else {
45         return sptr_;
46     }
47 }
48 
GetInterfaceDescriptor() const49 rust::string IRemoteObjectWrapper::GetInterfaceDescriptor() const
50 {
51     return GetInner()->GetInterfaceDescriptor().data();
52 }
53 
GetObjectDescriptor() const54 rust::string IRemoteObjectWrapper::GetObjectDescriptor() const
55 {
56     return GetInner()->GetObjectDescriptor().data();
57 }
58 
AddDeathRecipient(rust::Box<ClosureWrapper> callback) const59 std::unique_ptr<DeathRecipientRemoveHandler> IRemoteObjectWrapper::AddDeathRecipient(
60     rust::Box<ClosureWrapper> callback) const
61 {
62     auto *raw_recipient = new (std::nothrow) DeathRecipientWrapper(std::move(callback));
63     if (!raw_recipient) {
64         return nullptr;
65     }
66     sptr<IRemoteObject::DeathRecipient> recipient(raw_recipient);
67     bool res = sptr_->AddDeathRecipient(recipient);
68     if (!res) {
69         return nullptr;
70     }
71     return std::make_unique<DeathRecipientRemoveHandler>(sptr(sptr_), sptr(recipient));
72 }
73 
GetObjectRefCount() const74 int32_t IRemoteObjectWrapper::GetObjectRefCount() const
75 {
76     return GetInner()->GetObjectRefCount();
77 }
78 
IsProxyObject() const79 bool IRemoteObjectWrapper::IsProxyObject() const
80 {
81     return GetInner()->IsProxyObject();
82 }
IsObjectDead() const83 bool IRemoteObjectWrapper::IsObjectDead() const
84 {
85     return GetInner()->IsObjectDead();
86 }
CheckObjectLegality() const87 bool IRemoteObjectWrapper::CheckObjectLegality() const
88 {
89     return GetInner()->CheckObjectLegality();
90 }
91 
Dump(int fd,const rust::Slice<const rust::string> args) const92 int IRemoteObjectWrapper::Dump(int fd, const rust::Slice<const rust::string> args) const
93 {
94     std::vector<std::u16string> res;
95     for (auto rust_s : args) {
96         std::u16string s_u16 = Str8ToStr16(std::string(rust_s));
97         res.push_back(s_u16);
98     }
99     return GetInner()->Dump(fd, res);
100 }
101 
DeathRecipientWrapper(rust::Box<ClosureWrapper> cb)102 DeathRecipientWrapper::DeathRecipientWrapper(rust::Box<ClosureWrapper> cb) : inner_(std::move(cb)) {}
103 
OnRemoteDied(const OHOS::wptr<OHOS::IRemoteObject> & object)104 void DeathRecipientWrapper::OnRemoteDied(const OHOS::wptr<OHOS::IRemoteObject> &object)
105 {
106     auto obj = object.promote();
107     if (obj == nullptr) {
108         return;
109     }
110 
111     auto wrapper = std::make_unique<IRemoteObjectWrapper>();
112 
113     wrapper->is_raw_ = false;
114     wrapper->sptr_ = obj;
115 
116     auto rust_remote_obj = new_remote_obj(std::move(wrapper));
117     inner_->execute(std::move(rust_remote_obj));
118 }
119 
DeathRecipientRemoveHandler(sptr<IRemoteObject> remote,sptr<IRemoteObject::DeathRecipient> recipient)120 DeathRecipientRemoveHandler::DeathRecipientRemoveHandler(sptr<IRemoteObject> remote,
121     sptr<IRemoteObject::DeathRecipient> recipient)
122 {
123     this->remote_ = std::move(remote);
124     this->inner_ = std::move(recipient);
125 }
126 
remove() const127 void DeathRecipientRemoveHandler::remove() const
128 {
129     remote_->RemoveDeathRecipient(inner_);
130 }
131 
RemoteServiceStub(RemoteStubWrapper * ability,std::u16string descriptor)132 RemoteServiceStub::RemoteServiceStub(RemoteStubWrapper *ability, std::u16string descriptor) : IPCObjectStub(descriptor)
133 {
134     this->inner_ = ability;
135 }
136 
~RemoteServiceStub()137 RemoteServiceStub::~RemoteServiceStub()
138 {
139     auto ability = rust::Box<RemoteStubWrapper>::from_raw(this->inner_);
140 }
141 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)142 int RemoteServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
143 {
144     return inner_->on_remote_request(code, data, reply);
145 }
146 
Dump(int fd,const std::vector<std::u16string> & args)147 int RemoteServiceStub::Dump(int fd, const std::vector<std::u16string> &args)
148 {
149     auto v = rust::vec<rust::string>();
150     for (auto arg : args) {
151         v.push_back(rust::string(arg.data()));
152     }
153     return inner_->dump(fd, v);
154 }
155 
FromSptrRemote(std::unique_ptr<sptr<IRemoteObject>> remote)156 std::unique_ptr<IRemoteObjectWrapper> FromSptrRemote(std::unique_ptr<sptr<IRemoteObject>> remote)
157 {
158     if (remote == nullptr) {
159         return nullptr;
160     }
161     auto wrapper = std::make_unique<IRemoteObjectWrapper>();
162 
163     wrapper->is_raw_ = false;
164     wrapper->sptr_ = std::move(*remote.get());
165 
166     return wrapper;
167 }
168 
CloneRemoteObj(const IRemoteObjectWrapper & remote)169 std::unique_ptr<IRemoteObjectWrapper> CloneRemoteObj(const IRemoteObjectWrapper &remote)
170 {
171     if (remote.is_raw_) {
172         auto raw_ptr = remote.raw_;
173         if (raw_ptr == nullptr) {
174             return nullptr;
175         }
176         return FromCIRemoteObject(raw_ptr);
177     } else {
178         auto sptr = remote.sptr_;
179         if (sptr == nullptr) {
180             return nullptr;
181         }
182         auto wrapper = std::make_unique<IRemoteObjectWrapper>();
183 
184         wrapper->is_raw_ = false;
185         wrapper->sptr_ = sptr;
186         return wrapper;
187     }
188 }
189 
FromRemoteStub(rust::Box<RemoteStubWrapper> stub)190 std::unique_ptr<IRemoteObjectWrapper> FromRemoteStub(rust::Box<RemoteStubWrapper> stub)
191 {
192     auto raw = stub.into_raw();
193     auto rust_s = raw->descriptor();
194     std::string s = std::string(rust_s);
195     std::u16string descriptor = Str8ToStr16(s);
196 
197     auto stub_sptr = sptr<RemoteServiceStub>::MakeSptr(raw, descriptor);
198 
199     auto wrapper = std::make_unique<IRemoteObjectWrapper>();
200 
201     wrapper->is_raw_ = false;
202     wrapper->sptr_ = stub_sptr;
203 
204     return wrapper;
205 }
206 
FromCIRemoteObject(IRemoteObject * stub)207 std::unique_ptr<IRemoteObjectWrapper> FromCIRemoteObject(IRemoteObject *stub)
208 {
209     if (stub == nullptr) {
210         return nullptr;
211     }
212     auto wrapper = std::make_unique<IRemoteObjectWrapper>();
213 
214     wrapper->is_raw_ = true;
215     wrapper->raw_ = stub;
216 
217     return wrapper;
218 }
219 
220 } // namespace IpcRust
221 } // namespace OHOS