• 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 #ifndef IPC_RUST_CXX_REMOTE_OBJECT_H
17 #define IPC_RUST_CXX_REMOTE_OBJECT_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <string>
22 
23 #include "cxx.h"
24 #include "ipc_object_stub.h"
25 #include "iremote_object.h"
26 #include "message_option.h"
27 #include "message_parcel.h"
28 #include "refbase.h"
29 
30 namespace OHOS {
31 
32 typedef sptr<IRemoteObject> SptrIRemoteObject;
33 
34 namespace IpcRust {
35 struct ClosureWrapper;
36 struct RemoteObj;
37 struct RemoteStubWrapper;
38 struct DeathRecipientRemoveHandler;
39 
40 class IRemoteObjectWrapper {
41 public:
42     IRemoteObjectWrapper();
43 
44     ~IRemoteObjectWrapper() = default;
45 
46     int32_t SendRequest(const uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) const;
47     rust::string GetInterfaceDescriptor() const;
48     rust::string GetObjectDescriptor() const;
49     int32_t GetObjectRefCount() const;
50     bool IsProxyObject() const;
51     bool IsObjectDead() const;
52     bool CheckObjectLegality() const;
53     int Dump(int fd, const rust::Slice<const rust::string> args) const;
54 
55     std::unique_ptr<DeathRecipientRemoveHandler> AddDeathRecipient(rust::Box<ClosureWrapper>) const;
56 
57     IRemoteObject *GetInner() const;
58 
59     bool is_raw_ = false;
60     sptr<IRemoteObject> sptr_;
61     IRemoteObject *raw_;
62 };
63 
64 struct DeathRecipientWrapper : public virtual IRemoteObject::DeathRecipient {
65 public:
66     DeathRecipientWrapper(rust::Box<ClosureWrapper> cb);
67     virtual void OnRemoteDied(const OHOS::wptr<OHOS::IRemoteObject> &object) override;
68 
69 private:
70     rust::Box<ClosureWrapper> inner_;
71 };
72 
73 struct DeathRecipientRemoveHandler {
74 public:
75     DeathRecipientRemoveHandler(sptr<IRemoteObject> remote, sptr<IRemoteObject::DeathRecipient> recipient);
76     void remove() const;
77 
78 private:
79     sptr<IRemoteObject> remote_;
80     sptr<IRemoteObject::DeathRecipient> inner_;
81 };
82 
83 struct RemoteServiceStub : public IPCObjectStub {
84 public:
85     explicit RemoteServiceStub(RemoteStubWrapper *stub, std::u16string);
86     ~RemoteServiceStub();
87 
88     int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
89 
90     int Dump(int fd, const std::vector<std::u16string> &args) override;
91 
92 private:
93     RemoteStubWrapper *inner_;
94 };
95 
96 std::unique_ptr<IRemoteObjectWrapper> FromSptrRemote(std::unique_ptr<sptr<IRemoteObject>> remote);
97 
98 std::unique_ptr<IRemoteObjectWrapper> CloneRemoteObj(const IRemoteObjectWrapper &remote);
99 
100 std::unique_ptr<IRemoteObjectWrapper> FromRemoteStub(rust::Box<RemoteStubWrapper> stub);
101 
102 std::unique_ptr<IRemoteObjectWrapper> FromCIRemoteObject(IRemoteObject *stub);
103 
104 } // namespace IpcRust
105 } // namespace OHOS
106 
107 #endif