1
2 /*
3 * Copyright (c) 2021 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "ability_connect_callback_stub.h"
18
19 #include "ability_connect_callback_proxy.h"
20 #include "hilog_wrapper.h"
21 #include "ipc_types.h"
22 #include "message_parcel.h"
23 #include "want.h"
24
25 namespace OHOS {
26 namespace AAFwk {
WriteInterfaceToken(MessageParcel & data)27 bool AbilityConnectionProxy::WriteInterfaceToken(MessageParcel &data)
28 {
29 if (!data.WriteInterfaceToken(AbilityConnectionProxy::GetDescriptor())) {
30 HILOG_ERROR("Write interface token failed.");
31 return false;
32 }
33 return true;
34 }
35
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)36 void AbilityConnectionProxy::OnAbilityConnectDone(
37 const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode)
38 {
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 int error;
74 MessageParcel data;
75 MessageParcel reply;
76 MessageOption option(MessageOption::TF_ASYNC);
77
78 if (!WriteInterfaceToken(data)) {
79 HILOG_ERROR("Write interface token failed.");
80 return;
81 }
82 if (!data.WriteParcelable(&element) || !data.WriteInt32(resultCode)) {
83 HILOG_ERROR("Disconnect done data write error.");
84 return;
85 }
86
87 error = Remote()->SendRequest(IAbilityConnection::ON_ABILITY_DISCONNECT_DONE, data, reply, option);
88 if (error != NO_ERROR) {
89 HILOG_ERROR("Disconnect done fail, error: %d", error);
90 return;
91 }
92 }
93
AbilityConnectionStub()94 AbilityConnectionStub::AbilityConnectionStub()
95 {}
96
~AbilityConnectionStub()97 AbilityConnectionStub::~AbilityConnectionStub()
98 {}
99
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)100 int AbilityConnectionStub::OnRemoteRequest(
101 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
102 {
103 HILOG_DEBUG("AbilityConnectionStub::OnRemoteRequest OnAbilityConnectDone called.");
104 std::u16string descriptor = AbilityConnectionStub::GetDescriptor();
105 std::u16string remoteDescriptor = data.ReadInterfaceToken();
106 if (descriptor != remoteDescriptor) {
107 HILOG_INFO("Local descriptor is not equal to remote");
108 return ERR_INVALID_STATE;
109 }
110
111 auto element = data.ReadParcelable<AppExecFwk::ElementName>();
112 switch (code) {
113 case IAbilityConnection::ON_ABILITY_CONNECT_DONE: {
114 if (element == nullptr) {
115 HILOG_ERROR("callback stub receive element is nullptr");
116 return ERR_INVALID_VALUE;
117 }
118 auto remoteObject = data.ReadRemoteObject();
119 if (remoteObject == nullptr) {
120 HILOG_ERROR("callback stub receive remoteObject is nullptr");
121 delete element;
122 return ERR_INVALID_VALUE;
123 }
124 auto resultCode = data.ReadInt32();
125 OnAbilityConnectDone(*element, remoteObject, resultCode);
126 delete element;
127 return NO_ERROR;
128 }
129 case IAbilityConnection::ON_ABILITY_DISCONNECT_DONE: {
130 if (element == nullptr) {
131 HILOG_ERROR("callback stub receive element is nullptr");
132 return ERR_INVALID_VALUE;
133 }
134 auto resultCode = data.ReadInt32();
135 OnAbilityDisconnectDone(*element, resultCode);
136 delete element;
137 return NO_ERROR;
138 }
139 default: {
140 if (element != nullptr) {
141 delete element;
142 }
143 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
144 }
145 }
146 }
147
OnRemoteDied(const wptr<IRemoteObject> & remote)148 void AbilityConnectCallbackRecipient::OnRemoteDied(const wptr<IRemoteObject> &__attribute__((unused)) remote)
149 {
150 HILOG_ERROR("On remote died.");
151 if (handler_) {
152 handler_(remote);
153 }
154 }
155
AbilityConnectCallbackRecipient(RemoteDiedHandler handler)156 AbilityConnectCallbackRecipient::AbilityConnectCallbackRecipient(RemoteDiedHandler handler) : handler_(handler)
157 {}
158
~AbilityConnectCallbackRecipient()159 AbilityConnectCallbackRecipient::~AbilityConnectCallbackRecipient()
160 {}
161 } // namespace AAFwk
162 } // namespace OHOS
163