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 = Remote()->SendRequest(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 = Remote()->SendRequest(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
AbilityConnectionStub()95 AbilityConnectionStub::AbilityConnectionStub()
96 {}
97
~AbilityConnectionStub()98 AbilityConnectionStub::~AbilityConnectionStub()
99 {}
100
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)101 int AbilityConnectionStub::OnRemoteRequest(
102 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
103 {
104 HILOG_INFO("AbilityConnectionStub::OnRemoteRequest code: %{public}u", code);
105 std::u16string descriptor = AbilityConnectionStub::GetDescriptor();
106 std::u16string remoteDescriptor = data.ReadInterfaceToken();
107 if (descriptor != remoteDescriptor) {
108 HILOG_ERROR("Local descriptor is not equal to remote");
109 return ERR_INVALID_STATE;
110 }
111
112 auto element = data.ReadParcelable<AppExecFwk::ElementName>();
113 if (element == nullptr) {
114 HILOG_ERROR("callback stub receive element is nullptr");
115 return ERR_INVALID_VALUE;
116 }
117 HILOG_INFO("AbilityConnectionStub Call callback");
118 switch (code) {
119 case IAbilityConnection::ON_ABILITY_CONNECT_DONE: {
120 auto remoteObject = data.ReadRemoteObject();
121 if (remoteObject == nullptr) {
122 HILOG_ERROR("callback stub receive remoteObject is nullptr");
123 delete element;
124 return ERR_INVALID_VALUE;
125 }
126 auto resultCode = data.ReadInt32();
127 HILOG_INFO("AbilityConnectionStub ON_ABILITY_CONNECT_DONE");
128 OnAbilityConnectDone(*element, remoteObject, resultCode);
129 HILOG_INFO("AbilityConnectionStub ON_ABILITY_CONNECT_DONE end");
130 delete element;
131 return NO_ERROR;
132 }
133 case IAbilityConnection::ON_ABILITY_DISCONNECT_DONE: {
134 auto resultCode = data.ReadInt32();
135 HILOG_INFO("AbilityConnectionStub ON_ABILITY_DISCONNECT_DONE");
136 OnAbilityDisconnectDone(*element, resultCode);
137 delete element;
138 return NO_ERROR;
139 }
140 case IAbilityConnection::ON_REMOTE_STATE_CHANGED: {
141 int32_t abilityState = data.ReadInt32();
142 HILOG_INFO("AbilityConnectionStub ON_REMOTE_STATE_CHANGED");
143 OnRemoteStateChanged(*element, abilityState);
144 delete element;
145 return NO_ERROR;
146 }
147 default: {
148 HILOG_INFO("AbilityConnectionStub default");
149 if (element != nullptr) {
150 delete element;
151 }
152 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
153 }
154 }
155 }
156
OnRemoteDied(const wptr<IRemoteObject> & remote)157 void AbilityConnectCallbackRecipient::OnRemoteDied(const wptr<IRemoteObject> &__attribute__((unused)) remote)
158 {
159 HILOG_ERROR("On remote died.");
160 if (handler_) {
161 handler_(remote);
162 }
163 }
164
AbilityConnectCallbackRecipient(RemoteDiedHandler handler)165 AbilityConnectCallbackRecipient::AbilityConnectCallbackRecipient(RemoteDiedHandler handler) : handler_(handler)
166 {}
167
~AbilityConnectCallbackRecipient()168 AbilityConnectCallbackRecipient::~AbilityConnectCallbackRecipient()
169 {}
170 } // namespace AAFwk
171 } // namespace OHOS
172