1 /*
2  * Copyright (c) 2021-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 "ability_connect_callback_stub.h"
17 
18 #include "ability_connect_callback_proxy.h"
19 #include "hilog_tag_wrapper.h"
20 
21 namespace OHOS {
22 namespace AAFwk {
WriteInterfaceToken(MessageParcel & data)23 bool AbilityConnectionProxy::WriteInterfaceToken(MessageParcel &data)
24 {
25     if (!data.WriteInterfaceToken(AbilityConnectionProxy::GetDescriptor())) {
26         TAG_LOGE(AAFwkTag::ABILITYMGR, "Write interface token failed.");
27         return false;
28     }
29     return true;
30 }
31 
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)32 void AbilityConnectionProxy::OnAbilityConnectDone(
33     const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode)
34 {
35     TAG_LOGD(AAFwkTag::ABILITYMGR, "OnAbilityConnectDone resultCode: %{public}d", resultCode);
36     int error;
37     MessageParcel data;
38     MessageParcel reply;
39     MessageOption option(MessageOption::TF_ASYNC);
40 
41     if (!WriteInterfaceToken(data)) {
42         TAG_LOGE(AAFwkTag::ABILITYMGR, "Write interface token failed.");
43         return;
44     }
45 
46     if (!data.WriteParcelable(&element)) {
47         TAG_LOGE(AAFwkTag::ABILITYMGR, "Connect done element error.");
48         return;
49     }
50 
51     if (!data.WriteRemoteObject(remoteObject)) {
52         TAG_LOGE(AAFwkTag::ABILITYMGR, "Connect done remote object error.");
53         return;
54     }
55 
56     if (!data.WriteInt32(resultCode)) {
57         TAG_LOGE(AAFwkTag::ABILITYMGR, "Connect done result code error.");
58         return;
59     }
60 
61     error = SendTransactCmd(IAbilityConnection::ON_ABILITY_CONNECT_DONE, data, reply, option);
62     if (error != NO_ERROR) {
63         TAG_LOGE(AAFwkTag::ABILITYMGR, "Connect done fail, error: %{public}d", error);
64         return;
65     }
66 }
67 
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)68 void AbilityConnectionProxy::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode)
69 {
70     TAG_LOGD(AAFwkTag::ABILITYMGR, "OnAbilityDisconnectDone resultCode: %{public}d", resultCode);
71     int error;
72     MessageParcel data;
73     MessageParcel reply;
74     MessageOption option(MessageOption::TF_ASYNC);
75 
76     if (!WriteInterfaceToken(data)) {
77         TAG_LOGE(AAFwkTag::ABILITYMGR, "Write interface token failed.");
78         return;
79     }
80     if (!data.WriteParcelable(&element) || !data.WriteInt32(resultCode)) {
81         TAG_LOGE(AAFwkTag::ABILITYMGR, "Disconnect done data write error.");
82         return;
83     }
84 
85     error = SendTransactCmd(IAbilityConnection::ON_ABILITY_DISCONNECT_DONE, data, reply, option);
86     if (error != NO_ERROR) {
87         TAG_LOGE(AAFwkTag::ABILITYMGR, "Disconnect done fail, error: %d", error);
88         return;
89     }
90 }
91 
SendTransactCmd(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)92 int32_t AbilityConnectionProxy::SendTransactCmd(uint32_t code, MessageParcel &data,
93     MessageParcel &reply, MessageOption &option)
94 {
95     sptr<IRemoteObject> remote = Remote();
96     if (remote == nullptr) {
97         TAG_LOGE(AAFwkTag::ABILITYMGR, "remote object is nullptr.");
98         return ERR_NULL_OBJECT;
99     }
100 
101     int32_t ret = remote->SendRequest(code, data, reply, option);
102     if (ret != NO_ERROR) {
103         TAG_LOGE(AAFwkTag::ABILITYMGR, "SendRequest failed. code is %{public}d, ret is %{public}d.", code, ret);
104         return ret;
105     }
106     return NO_ERROR;
107 }
108 
AbilityConnectionStub()109 AbilityConnectionStub::AbilityConnectionStub()
110 {}
111 
~AbilityConnectionStub()112 AbilityConnectionStub::~AbilityConnectionStub()
113 {}
114 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)115 int AbilityConnectionStub::OnRemoteRequest(
116     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
117 {
118     TAG_LOGD(AAFwkTag::ABILITYMGR, "code: %{public}u", code);
119     std::u16string descriptor = AbilityConnectionStub::GetDescriptor();
120     std::u16string remoteDescriptor = data.ReadInterfaceToken();
121     if (descriptor != remoteDescriptor) {
122         TAG_LOGE(AAFwkTag::ABILITYMGR, "Local descriptor is not equal to remote");
123         return ERR_INVALID_STATE;
124     }
125 
126     std::unique_ptr<AppExecFwk::ElementName> element(data.ReadParcelable<AppExecFwk::ElementName>());
127     if (element == nullptr) {
128         TAG_LOGE(AAFwkTag::ABILITYMGR, "callback stub receive element is nullptr");
129         return ERR_INVALID_VALUE;
130     }
131     TAG_LOGD(AAFwkTag::ABILITYMGR, "Call callback");
132     switch (code) {
133         case IAbilityConnection::ON_ABILITY_CONNECT_DONE: {
134             auto remoteObject = data.ReadRemoteObject();
135             if (remoteObject == nullptr) {
136                 TAG_LOGE(AAFwkTag::ABILITYMGR, "callback stub receive remoteObject is nullptr");
137                 return ERR_INVALID_VALUE;
138             }
139             auto resultCode = data.ReadInt32();
140             TAG_LOGD(AAFwkTag::ABILITYMGR, "AbilityConnectionStub ON_ABILITY_CONNECT_DONE");
141             OnAbilityConnectDone(*element, remoteObject, resultCode);
142             TAG_LOGI(AAFwkTag::ABILITYMGR, "AbilityConnectionStub ON_ABILITY_CONNECT_DONE end");
143             return NO_ERROR;
144         }
145         case IAbilityConnection::ON_ABILITY_DISCONNECT_DONE: {
146             auto resultCode = data.ReadInt32();
147             TAG_LOGD(AAFwkTag::ABILITYMGR, "AbilityConnectionStub ON_ABILITY_DISCONNECT_DONE");
148             OnAbilityDisconnectDone(*element, resultCode);
149             TAG_LOGI(AAFwkTag::ABILITYMGR, "AbilityConnectionStub ON_ABILITY_DISCONNECT_DONE end");
150             return NO_ERROR;
151         }
152         case IAbilityConnection::ON_REMOTE_STATE_CHANGED: {
153             int32_t abilityState = data.ReadInt32();
154             TAG_LOGI(AAFwkTag::ABILITYMGR, "AbilityConnectionStub ON_REMOTE_STATE_CHANGED");
155             OnRemoteStateChanged(*element, abilityState);
156             return NO_ERROR;
157         }
158         default: {
159             TAG_LOGI(AAFwkTag::ABILITYMGR, "AbilityConnectionStub default");
160             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
161         }
162     }
163 }
164 
OnRemoteDied(const wptr<IRemoteObject> & remote)165 void AbilityConnectCallbackRecipient::OnRemoteDied(const wptr<IRemoteObject> &__attribute__((unused)) remote)
166 {
167     TAG_LOGD(AAFwkTag::ABILITYMGR, "called");
168     if (handler_) {
169         handler_(remote);
170     }
171 }
172 
AbilityConnectCallbackRecipient(RemoteDiedHandler handler)173 AbilityConnectCallbackRecipient::AbilityConnectCallbackRecipient(RemoteDiedHandler handler) : handler_(handler)
174 {}
175 
~AbilityConnectCallbackRecipient()176 AbilityConnectCallbackRecipient::~AbilityConnectCallbackRecipient()
177 {}
178 }  // namespace AAFwk
179 }  // namespace OHOS
180