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