• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Subscribing to State Changes of a Remote Object
2
3IPC/RPC allows you to subscribe to the state changes of a remote stub object. When the remote stub object dies, a death notification will be sent to your local proxy object. Such subscription and unsubscription are controlled by APIs. To be specific, you need to implement the **DeathRecipient** interface and the **onRemoteDied** API to clear resources. This callback is invoked when the process accommodating the remote stub object dies, or the device accommodating the remote stub object leaves the network. It is worth noting that these APIs should be called in the following order: The proxy object must first subscribe to death notifications of the stub object. If the stub object is in the normal state, the proxy object can cancel the subscription as required. If the process of the stub object exits or the device hosting the stub object goes offline, subsequent operations customized by the proxy object will be automatically triggered.
4
5## Scenarios
6
7This subscription mechanism is applicable when the local proxy object needs to detect death of the process hosting the remote stub object or network detach of the device hosting the remote stub object. When the proxy detects death of the remote stub object, the proxy can clear local resources. Currently, IPC supports death notification for anonymous objects, but RPC does not. That is, you can only subscribe to death notifications of services that have been registered with SAMgr.
8
9
10## **Development Using Native APIs**
11
12| Name| Return Value Type| Description|
13| -------- | -------- | -------- |
14| AddDeathRecipient(const sptr\<DeathRecipient> &recipient); | bool | Adds a recipient for death notifications of a remote stub object.|
15| RemoveDeathRecipient(const sptr\<DeathRecipient> &recipient); | bool | Removes the recipient for death notifications of a remote stub object.|
16| OnRemoteDied(const wptr\<IRemoteObject> &object); | void | Called when the remote stub object dies.|
17
18### Sample Code
19
20```C++
21#include "iremote_broker.h"
22#include "iremote_stub.h"
23
24// Define message codes.
25enum {
26    TRANS_ID_PING_ABILITY = 5,
27    TRANS_ID_REVERSED_MONITOR
28};
29
30const std::string DESCRIPTOR = "test.ITestAbility";
31
32class ITestService : public IRemoteBroker {
33public:
34    // DECLARE_INTERFACE_DESCRIPTOR is mandatory, and the input parameter is std::u16string.
35    DECLARE_INTERFACE_DESCRIPTOR(to_utf16(DESCRIPTOR));
36    virtual int TestPingAbility(const std::u16string &dummy) = 0; // Define functions.
37};
38
39class TestServiceProxy : public IRemoteProxy<ITestAbility> {
40public:
41    explicit TestAbilityProxy(const sptr<IRemoteObject> &impl);
42    virtual int TestPingAbility(const std::u16string &dummy) override;
43    int TestAnonymousStub();
44private:
45    static inline BrokerDelegator<TestAbilityProxy> delegator_; // For use of the iface_cast macro at a later time
46};
47
48TestServiceProxy::TestServiceProxy(const sptr<IRemoteObject> &impl)
49    : IRemoteProxy<ITestAbility>(impl)
50{
51}
52
53int TestServiceProxy::TestPingAbility(const std::u16string &dummy){
54    MessageOption option;
55    MessageParcel dataParcel, replyParcel;
56    dataParcel.WriteString16(dummy);
57    int error = PeerHolder::Remote()->SendRequest(TRANS_ID_PING_ABILITY, dataParcel, replyParcel, option);
58    int result = (error == ERR_NONE) ? replyParcel.ReadInt32() : -1;
59    return result;
60}
61```
62
63```c++
64#include "iremote_object.h"
65
66class TestDeathRecipient : public IRemoteObject::DeathRecipient {
67public:
68    virtual void OnRemoteDied(const wptr<IRemoteObject>& remoteObject);
69}
70
71void TestDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remoteObject)
72{
73}
74```
75
76```c++
77sptr<IPCObjectProxy> object = new IPCObjectProxy(1, to_utf16(DESCRIPTOR));
78sptr<IRemoteObject::DeathRecipient> deathRecipient (new TestDeathRecipient());// Construct a death notification recipient.
79bool result = object->AddDeathRecipient(deathRecipient); // Add a recipient for death notifications.
80result = object->RemoveDeathRecipient(deathRecipient); // Remove the recipient for death notifications.
81```
82
83## **Development Using JS APIs**
84
85| Name                  | Return Value Type| Description                                                         |
86| ------------------------ | ---------- | ----------------------------------------------------------------- |
87| registerDeathRecipient   | void       | Adds a recipient for death notifications of the remote object, including death notifications of the remote proxy.|
88| unregisterDeathRecipient | void       | Removes the recipient for death notifications of the remote object.                             |
89| onRemoteDied             | void       | Called to perform subsequent operations when a death notification of the remote object is received.   |
90
91### Obtaining the Context
92
93If you use the stage model, you need to obtain the context before connecting to an ability.
94
95```ts
96import UIAbility from '@ohos.app.ability.UIAbility';
97import Want from '@ohos.app.ability.Want';
98import AbilityConstant from '@ohos.app.ability.AbilityConstant';
99import window from '@ohos.window';
100
101export default class MainAbility extends UIAbility {
102    onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
103        console.log("[Demo] MainAbility onCreate");
104        let context = this.context;
105    }
106    onDestroy() {
107        console.log("[Demo] MainAbility onDestroy");
108    }
109    onWindowStageCreate(windowStage: window.WindowStage) {
110        // Main window is created, set main page for this ability
111        console.log("[Demo] MainAbility onWindowStageCreate");
112    }
113    onWindowStageDestroy() {
114        // Main window is destroyed, release UI related resources
115        console.log("[Demo] MainAbility onWindowStageDestroy");
116    }
117    onForeground() {
118        // Ability has brought to foreground
119        console.log("[Demo] MainAbility onForeground");
120    }
121    onBackground() {
122        // Ability has back to background
123        console.log("[Demo] MainAbility onBackground");
124    }
125}
126```
127
128### Sample Code
129
130```ts
131// Import @ohos.ability.featureAbility only for the application developed based on the FA model.
132// import FA from "@ohos.ability.featureAbility";
133import Want from '@ohos.app.ability.Want';
134import common from '@ohos.app.ability.common';
135import rpc from '@ohos.rpc';
136
137let proxy: rpc.IRemoteObject | undefined = undefined;
138let connect: common.ConnectOptions = {
139  onConnect: (elementName, remoteProxy) => {
140    console.log("RpcClient: js onConnect called.");
141    proxy = remoteProxy;
142  },
143  onDisconnect: (elementName) => {
144    console.log("RpcClient: onDisconnect");
145  },
146  onFailed: () => {
147    console.log("RpcClient: onFailed");
148  }
149};
150let want: Want = {
151  bundleName: "com.ohos.server",
152  abilityName: "com.ohos.server.EntryAbility",
153};
154// Use this method to connect to the ability in the FA model.
155// FA.connectAbility(want, connect);
156
157this.context.connectServiceExtensionAbility(want, connect);
158```
159
160The proxy object in the **onConnect** callback can be assigned a value only after the ability is connected asynchronously. Then, **unregisterDeathRecipient()** of the proxy object is called to unregister the callback for receiving the death notification of the remote object.
161
162```ts
163import Want from '@ohos.app.ability.Want';
164import common from '@ohos.app.ability.common';
165import rpc from '@ohos.rpc';
166class MyDeathRecipient implements rpc.DeathRecipient{
167    onRemoteDied() {
168        console.log("server died");
169    }
170}
171let deathRecipient = new MyDeathRecipient();
172let proxy: rpc.IRemoteObject | undefined = undefined;
173proxy.registerDeathRecipient(deathRecipient, 0);
174proxy.unregisterDeathRecipient(deathRecipient, 0);
175```
176
177## Reverse Death Notification (Anonymous Stub)
178
179Forward dead notification is a mechanism that allows the proxy to detect death notifications of the stub. To achieve reverse dead notification, we can leverage the forward dead notification mechanism to allow the stub to detect death notifications of the proxy. Suppose there are two processes, A (the process hosting the original stub) and B (the process hosting the original proxy). After obtaining the proxy object of process A, process B creates an anonymous stub object (that is, a stub object not registered with SAMgr), which can be called a callback stub. Then, process B calls **SendRequest** to send the callback stub to the original stub of process A. As a result, process A obtains the callback proxy of process B. When process B dies or the device hosting process B detaches from the network, the callback stub dies. The callback proxy detects the death of the callback stub and sends a death notification to the original stub. In this way, reverse death notification is implemented.
180
181**NOTE**
182
183> Reverse death notification can only be used for cross-process communication within a device.
184
185> When an anonymous stub object is not pointed by any proxy, the kernel automatically reclaims the object.
186
187### Sample Code
188
189```c++
190//Proxy
191int TestAbilityProxy::TestAnonymousStub()
192{
193    MessageOption option;
194    MessageParcel dataParcel, replyParcel;
195    dataParcel.UpdateDataVersion(Remote());
196    dataParcel.WriteRemoteObject(new TestAbilityStub());
197    int error = Remote()->SendRequest(TRANS_ID_REVERSED_MONITOR,dataParcel, replyParcel, option);
198    int result = (error == ERR_NONE) ? replyParcel.ReadInt32() : -1;
199    return result;
200}
201
202//Stub
203
204int TestAbilityStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
205{
206    switch (code) {
207        case TRANS_ID_REVERSED_MONITOR: {
208            sptr<IRemoteObject> obj = data.ReadRemoteObject();
209            if (obj == nullptr) {
210                reply.WriteInt32(ERR_NULL_OBJECT);
211                return ERR_NULL_OBJECT;
212            }
213            bool result = obj->AddDeathRecipient(new TestDeathRecipient());
214            result ? reply.WriteInt32(ERR_NONE) : reply.WriteInt32(-1);
215            break;
216        }
217        default:
218            break;
219    }
220    return ERR_NONE;
221}
222```
223