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